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
3 // found in the LICENSE file.
5 #include "courgette/assembly_program.h"
6 #include "courgette/base_test_unittest.h"
7 #include "courgette/disassembler_elf_32_x86.h"
9 class DisassemblerElf32X86Test
: public BaseTest
{
12 void TestExe(const char* file_name
,
13 size_t expected_abs_count
,
14 size_t expected_rel_count
) const;
17 void DisassemblerElf32X86Test::TestExe(const char* file_name
,
18 size_t expected_abs_count
,
19 size_t expected_rel_count
) const {
20 std::string file1
= FileContents(file_name
);
22 scoped_ptr
<courgette::DisassemblerElf32X86
> disassembler(
23 new courgette::DisassemblerElf32X86(file1
.c_str(), file1
.length()));
25 bool can_parse_header
= disassembler
->ParseHeader();
26 EXPECT_TRUE(can_parse_header
);
27 EXPECT_TRUE(disassembler
->ok());
29 // The length of the disassembled value will be slightly smaller than the
30 // real file, since trailing debug info is not included
31 EXPECT_EQ(file1
.length(), disassembler
->length());
33 const uint8
* offset_p
= disassembler
->OffsetToPointer(0);
34 EXPECT_EQ(reinterpret_cast<const void*>(file1
.c_str()),
35 reinterpret_cast<const void*>(offset_p
));
36 EXPECT_EQ(0x7F, offset_p
[0]);
37 EXPECT_EQ('E', offset_p
[1]);
38 EXPECT_EQ('L', offset_p
[2]);
39 EXPECT_EQ('F', offset_p
[3]);
41 courgette::AssemblyProgram
* program
= new courgette::AssemblyProgram();
43 EXPECT_TRUE(disassembler
->Disassemble(program
));
45 EXPECT_EQ(disassembler
->Abs32Locations().size(), expected_abs_count
);
46 EXPECT_EQ(disassembler
->Rel32Locations().size(), expected_rel_count
);
48 // Prove that none of the rel32 RVAs overlap with abs32 RVAs
49 std::set
<courgette::RVA
> abs(disassembler
->Abs32Locations().begin(),
50 disassembler
->Abs32Locations().end());
51 std::set
<courgette::RVA
> rel(disassembler
->Rel32Locations().begin(),
52 disassembler
->Rel32Locations().end());
53 for (std::vector
<courgette::RVA
>::iterator rel32
=
54 disassembler
->Rel32Locations().begin();
55 rel32
!= disassembler
->Rel32Locations().end();
57 EXPECT_TRUE(abs
.find(*rel32
) == abs
.end());
60 for (std::vector
<courgette::RVA
>::iterator abs32
=
61 disassembler
->Abs32Locations().begin();
62 abs32
!= disassembler
->Abs32Locations().end();
64 EXPECT_TRUE(rel
.find(*abs32
) == rel
.end());
69 TEST_F(DisassemblerElf32X86Test
, All
) {
70 TestExe("elf-32-1", 200, 3442);