Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "courgette/disassembler_elf_32_x86.h" | 5 #include "courgette/disassembler_elf_32_x86.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <vector> | |
| 13 | 14 |
| 14 #include "courgette/assembly_program.h" | 15 #include "courgette/assembly_program.h" |
| 15 #include "courgette/base_test_unittest.h" | 16 #include "courgette/base_test_unittest.h" |
| 16 #include "courgette/image_utils.h" | 17 #include "courgette/image_utils.h" |
| 17 | 18 |
| 18 namespace courgette { | 19 namespace courgette { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 23 class TestDisassemblerElf32X86 : public DisassemblerElf32X86 { | |
| 24 public: | |
| 25 TestDisassemblerElf32X86(const void* start, size_t length) | |
| 26 : DisassemblerElf32X86(start, length) { } | |
| 27 ~TestDisassemblerElf32X86() override { } | |
| 28 | |
| 29 void TestSectionHeaderFileOffsetOrder() { | |
| 30 std::vector<FileOffset> file_offsets; | |
| 31 for (Elf32_Half section_id : section_header_file_offset_order_) { | |
| 32 const Elf32_Shdr* section_header = SectionHeader(section_id); | |
| 33 file_offsets.push_back(section_header->sh_offset); | |
| 34 } | |
| 35 EXPECT_EQ(static_cast<size_t>(SectionHeaderCount()), file_offsets.size()); | |
|
Will Harris
2016/04/19 18:36:35
brackets in wrong place?
huangs
2016/04/19 19:02:53
I looked carefully, looks alright. static_cast<>(
Will Harris
2016/04/19 19:44:25
ah yes you're right I was missing the close bracke
huangs
2016/04/19 19:59:52
Acknowledged.
| |
| 36 EXPECT_TRUE(std::is_sorted(file_offsets.begin(), file_offsets.end())); | |
| 37 } | |
| 38 }; | |
| 39 | |
| 22 class DisassemblerElf32X86Test : public BaseTest { | 40 class DisassemblerElf32X86Test : public BaseTest { |
| 23 public: | 41 public: |
| 24 void TestExe(const char* file_name, | 42 void TestExe(const char* file_name, |
| 25 size_t expected_abs_count, | 43 size_t expected_abs_count, |
| 26 size_t expected_rel_count) const; | 44 size_t expected_rel_count) const; |
| 27 }; | 45 }; |
| 28 | 46 |
| 29 void DisassemblerElf32X86Test::TestExe(const char* file_name, | 47 void DisassemblerElf32X86Test::TestExe(const char* file_name, |
| 30 size_t expected_abs_count, | 48 size_t expected_abs_count, |
| 31 size_t expected_rel_count) const { | 49 size_t expected_rel_count) const { |
| 32 using TypedRVA = DisassemblerElf32::TypedRVA; | 50 using TypedRVA = DisassemblerElf32::TypedRVA; |
| 33 std::string file1 = FileContents(file_name); | 51 std::string file1 = FileContents(file_name); |
| 34 | 52 |
| 35 std::unique_ptr<DisassemblerElf32X86> disassembler( | 53 std::unique_ptr<TestDisassemblerElf32X86> disassembler( |
| 36 new DisassemblerElf32X86(file1.c_str(), file1.length())); | 54 new TestDisassemblerElf32X86(file1.c_str(), file1.length())); |
| 37 | 55 |
| 38 bool can_parse_header = disassembler->ParseHeader(); | 56 bool can_parse_header = disassembler->ParseHeader(); |
| 39 EXPECT_TRUE(can_parse_header); | 57 EXPECT_TRUE(can_parse_header); |
| 40 EXPECT_TRUE(disassembler->ok()); | 58 EXPECT_TRUE(disassembler->ok()); |
| 41 | 59 |
| 42 // The length of the disassembled value will be slightly smaller than the | 60 // The length of the disassembled value will be slightly smaller than the |
| 43 // real file, since trailing debug info is not included | 61 // real file, since trailing debug info is not included |
| 44 EXPECT_EQ(file1.length(), disassembler->length()); | 62 EXPECT_EQ(file1.length(), disassembler->length()); |
| 45 | 63 |
| 46 const uint8_t* offset_p = disassembler->FileOffsetToPointer(0); | 64 const uint8_t* offset_p = disassembler->FileOffsetToPointer(0); |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 77 while (abs32_it != abs32_list.end() && rel32_it != rel32_list.end()) { | 95 while (abs32_it != abs32_list.end() && rel32_it != rel32_list.end()) { |
| 78 if (*abs32_it < *rel32_it) { | 96 if (*abs32_it < *rel32_it) { |
| 79 ++abs32_it; | 97 ++abs32_it; |
| 80 } else if (*abs32_it > *rel32_it) { | 98 } else if (*abs32_it > *rel32_it) { |
| 81 ++rel32_it; | 99 ++rel32_it; |
| 82 } else { | 100 } else { |
| 83 found_match = true; | 101 found_match = true; |
| 84 } | 102 } |
| 85 } | 103 } |
| 86 EXPECT_FALSE(found_match); | 104 EXPECT_FALSE(found_match); |
| 105 | |
| 106 disassembler->TestSectionHeaderFileOffsetOrder(); | |
| 87 } | 107 } |
| 88 | 108 |
| 89 } // namespace | 109 } // namespace |
| 90 | 110 |
| 91 TEST_F(DisassemblerElf32X86Test, All) { | 111 TEST_F(DisassemblerElf32X86Test, All) { |
| 92 TestExe("elf-32-1", 200, 3441); | 112 TestExe("elf-32-1", 200, 3441); |
| 113 TestExe("elf-32-high-bss", 0, 13); | |
| 93 } | 114 } |
| 94 | 115 |
| 95 } // namespace courgette | 116 } // namespace courgette |
| OLD | NEW |