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 <string> | 12 #include <string> |
12 | 13 |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "courgette/assembly_program.h" | 14 #include "courgette/assembly_program.h" |
15 #include "courgette/base_test_unittest.h" | 15 #include "courgette/base_test_unittest.h" |
16 #include "courgette/image_utils.h" | 16 #include "courgette/image_utils.h" |
17 | 17 |
18 namespace courgette { | 18 namespace courgette { |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 class DisassemblerElf32X86Test : public BaseTest { | 22 class DisassemblerElf32X86Test : public BaseTest { |
23 public: | 23 public: |
24 void TestExe(const char* file_name, | 24 void TestExe(const char* file_name, |
25 size_t expected_abs_count, | 25 size_t expected_abs_count, |
26 size_t expected_rel_count) const; | 26 size_t expected_rel_count) const; |
27 }; | 27 }; |
28 | 28 |
29 void DisassemblerElf32X86Test::TestExe(const char* file_name, | 29 void DisassemblerElf32X86Test::TestExe(const char* file_name, |
30 size_t expected_abs_count, | 30 size_t expected_abs_count, |
31 size_t expected_rel_count) const { | 31 size_t expected_rel_count) const { |
32 using TypedRVA = DisassemblerElf32::TypedRVA; | 32 using TypedRVA = DisassemblerElf32::TypedRVA; |
33 std::string file1 = FileContents(file_name); | 33 std::string file1 = FileContents(file_name); |
34 | 34 |
35 scoped_ptr<DisassemblerElf32X86> disassembler( | 35 std::unique_ptr<DisassemblerElf32X86> disassembler( |
36 new DisassemblerElf32X86(file1.c_str(), file1.length())); | 36 new DisassemblerElf32X86(file1.c_str(), file1.length())); |
37 | 37 |
38 bool can_parse_header = disassembler->ParseHeader(); | 38 bool can_parse_header = disassembler->ParseHeader(); |
39 EXPECT_TRUE(can_parse_header); | 39 EXPECT_TRUE(can_parse_header); |
40 EXPECT_TRUE(disassembler->ok()); | 40 EXPECT_TRUE(disassembler->ok()); |
41 | 41 |
42 // The length of the disassembled value will be slightly smaller than the | 42 // The length of the disassembled value will be slightly smaller than the |
43 // real file, since trailing debug info is not included | 43 // real file, since trailing debug info is not included |
44 EXPECT_EQ(file1.length(), disassembler->length()); | 44 EXPECT_EQ(file1.length(), disassembler->length()); |
45 | 45 |
46 const uint8_t* offset_p = disassembler->FileOffsetToPointer(0); | 46 const uint8_t* offset_p = disassembler->FileOffsetToPointer(0); |
47 EXPECT_EQ(reinterpret_cast<const void*>(file1.c_str()), | 47 EXPECT_EQ(reinterpret_cast<const void*>(file1.c_str()), |
48 reinterpret_cast<const void*>(offset_p)); | 48 reinterpret_cast<const void*>(offset_p)); |
49 EXPECT_EQ(0x7F, offset_p[0]); | 49 EXPECT_EQ(0x7F, offset_p[0]); |
50 EXPECT_EQ('E', offset_p[1]); | 50 EXPECT_EQ('E', offset_p[1]); |
51 EXPECT_EQ('L', offset_p[2]); | 51 EXPECT_EQ('L', offset_p[2]); |
52 EXPECT_EQ('F', offset_p[3]); | 52 EXPECT_EQ('F', offset_p[3]); |
53 | 53 |
54 scoped_ptr<AssemblyProgram> program(new AssemblyProgram(EXE_ELF_32_X86)); | 54 std::unique_ptr<AssemblyProgram> program(new AssemblyProgram(EXE_ELF_32_X86)); |
55 | 55 |
56 EXPECT_TRUE(disassembler->Disassemble(program.get())); | 56 EXPECT_TRUE(disassembler->Disassemble(program.get())); |
57 | 57 |
58 const std::vector<RVA>& abs32_list = disassembler->Abs32Locations(); | 58 const std::vector<RVA>& abs32_list = disassembler->Abs32Locations(); |
59 | 59 |
60 // Flatten the list typed rel32 to a list of rel32 RVAs. | 60 // Flatten the list typed rel32 to a list of rel32 RVAs. |
61 std::vector<RVA> rel32_list; | 61 std::vector<RVA> rel32_list; |
62 rel32_list.reserve(disassembler->Rel32Locations().size()); | 62 rel32_list.reserve(disassembler->Rel32Locations().size()); |
63 for (TypedRVA* typed_rel32 : disassembler->Rel32Locations()) | 63 for (TypedRVA* typed_rel32 : disassembler->Rel32Locations()) |
64 rel32_list.push_back(typed_rel32->rva()); | 64 rel32_list.push_back(typed_rel32->rva()); |
(...skipping 21 matching lines...) Expand all Loading... |
86 EXPECT_FALSE(found_match); | 86 EXPECT_FALSE(found_match); |
87 } | 87 } |
88 | 88 |
89 } // namespace | 89 } // namespace |
90 | 90 |
91 TEST_F(DisassemblerElf32X86Test, All) { | 91 TEST_F(DisassemblerElf32X86Test, All) { |
92 TestExe("elf-32-1", 200, 3441); | 92 TestExe("elf-32-1", 200, 3441); |
93 } | 93 } |
94 | 94 |
95 } // namespace courgette | 95 } // namespace courgette |
OLD | NEW |