OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "packer.h" | 5 #include "packer.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 #include "elf.h" | 8 #include "elf.h" |
| 9 #include "elf_traits.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
10 | 11 |
11 namespace { | 12 namespace { |
12 | 13 |
13 void AddRelocation(Elf32_Addr addr, std::vector<Elf32_Rel>* relocations) { | 14 void AddRelocation(ELF::Addr addr, std::vector<ELF::Rel>* relocations) { |
14 Elf32_Rel relocation = {addr, R_ARM_RELATIVE}; | 15 ELF::Rel relocation; |
| 16 relocation.r_offset = addr; |
| 17 relocation.r_info = ELF_R_INFO(0, ELF::kRelativeRelocationCode); |
15 relocations->push_back(relocation); | 18 relocations->push_back(relocation); |
16 } | 19 } |
17 | 20 |
18 bool CheckRelocation(Elf32_Addr addr, const Elf32_Rel& relocation) { | 21 bool CheckRelocation(ELF::Addr addr, const ELF::Rel& relocation) { |
19 return relocation.r_offset == addr && relocation.r_info == R_ARM_RELATIVE; | 22 return relocation.r_offset == addr && |
| 23 ELF_R_SYM(relocation.r_info) == 0 && |
| 24 ELF_R_TYPE(relocation.r_info) == ELF::kRelativeRelocationCode; |
20 } | 25 } |
21 | 26 |
22 } // namespace | 27 } // namespace |
23 | 28 |
24 namespace relocation_packer { | 29 namespace relocation_packer { |
25 | 30 |
26 TEST(Packer, Pack) { | 31 TEST(Packer, Pack) { |
27 std::vector<Elf32_Rel> relocations; | 32 std::vector<ELF::Rel> relocations; |
28 std::vector<uint8_t> packed; | 33 std::vector<uint8_t> packed; |
29 | 34 |
30 RelocationPacker packer; | 35 RelocationPacker packer; |
31 | 36 |
32 // Initial relocation. | 37 // Initial relocation. |
33 AddRelocation(0xd1ce0000, &relocations); | 38 AddRelocation(0xd1ce0000, &relocations); |
34 // Two more relocations, 4 byte deltas. | 39 // Two more relocations, 4 byte deltas. |
35 AddRelocation(0xd1ce0004, &relocations); | 40 AddRelocation(0xd1ce0004, &relocations); |
36 AddRelocation(0xd1ce0008, &relocations); | 41 AddRelocation(0xd1ce0008, &relocations); |
37 // Three more relocations, 8 byte deltas. | 42 // Three more relocations, 8 byte deltas. |
38 AddRelocation(0xd1ce0010, &relocations); | 43 AddRelocation(0xd1ce0010, &relocations); |
39 AddRelocation(0xd1ce0018, &relocations); | 44 AddRelocation(0xd1ce0018, &relocations); |
40 AddRelocation(0xd1ce0020, &relocations); | 45 AddRelocation(0xd1ce0020, &relocations); |
41 | 46 |
42 packed.clear(); | 47 packed.clear(); |
43 packer.PackRelativeRelocations(relocations, &packed); | 48 packer.PackRelativeRelocations(relocations, &packed); |
44 | 49 |
45 EXPECT_EQ(16u, packed.size()); | 50 EXPECT_EQ(16, packed.size()); |
46 // Identifier. | 51 // Identifier. |
47 EXPECT_EQ('A', packed[0]); | 52 EXPECT_EQ('A', packed[0]); |
48 EXPECT_EQ('P', packed[1]); | 53 EXPECT_EQ('P', packed[1]); |
49 EXPECT_EQ('R', packed[2]); | 54 EXPECT_EQ('R', packed[2]); |
50 EXPECT_EQ('1', packed[3]); | 55 EXPECT_EQ('1', packed[3]); |
51 // Count-delta pairs count. | 56 // Count-delta pairs count. |
52 EXPECT_EQ(2u, packed[4]); | 57 EXPECT_EQ(2, packed[4]); |
53 // 0xd1ce0000 | 58 // 0xd1ce0000 |
54 EXPECT_EQ(128u, packed[5]); | 59 EXPECT_EQ(128, packed[5]); |
55 EXPECT_EQ(128u, packed[6]); | 60 EXPECT_EQ(128, packed[6]); |
56 EXPECT_EQ(184u, packed[7]); | 61 EXPECT_EQ(184, packed[7]); |
57 EXPECT_EQ(142u, packed[8]); | 62 EXPECT_EQ(142, packed[8]); |
58 EXPECT_EQ(13u, packed[9]); | 63 EXPECT_EQ(13, packed[9]); |
59 // Run of two relocations, 4 byte deltas. | 64 // Run of two relocations, 4 byte deltas. |
60 EXPECT_EQ(2u, packed[10]); | 65 EXPECT_EQ(2, packed[10]); |
61 EXPECT_EQ(4u, packed[11]); | 66 EXPECT_EQ(4, packed[11]); |
62 // Run of three relocations, 8 byte deltas. | 67 // Run of three relocations, 8 byte deltas. |
63 EXPECT_EQ(3u, packed[12]); | 68 EXPECT_EQ(3, packed[12]); |
64 EXPECT_EQ(8u, packed[13]); | 69 EXPECT_EQ(8, packed[13]); |
65 // Padding. | 70 // Padding. |
66 EXPECT_EQ(0u, packed[14]); | 71 EXPECT_EQ(0, packed[14]); |
67 EXPECT_EQ(0u, packed[15]); | 72 EXPECT_EQ(0, packed[15]); |
68 } | 73 } |
69 | 74 |
70 TEST(Packer, Unpack) { | 75 TEST(Packer, Unpack) { |
71 std::vector<uint8_t> packed; | 76 std::vector<uint8_t> packed; |
72 std::vector<Elf32_Rel> relocations; | 77 std::vector<ELF::Rel> relocations; |
73 | 78 |
74 RelocationPacker packer; | 79 RelocationPacker packer; |
75 | 80 |
76 // Identifier. | 81 // Identifier. |
77 packed.push_back('A'); | 82 packed.push_back('A'); |
78 packed.push_back('P'); | 83 packed.push_back('P'); |
79 packed.push_back('R'); | 84 packed.push_back('R'); |
80 packed.push_back('1'); | 85 packed.push_back('1'); |
81 // Count-delta pairs count. | 86 // Count-delta pairs count. |
82 packed.push_back(2u); | 87 packed.push_back(2); |
83 // 0xd1ce0000 | 88 // 0xd1ce0000 |
84 packed.push_back(128u); | 89 packed.push_back(128); |
85 packed.push_back(128u); | 90 packed.push_back(128); |
86 packed.push_back(184u); | 91 packed.push_back(184); |
87 packed.push_back(142u); | 92 packed.push_back(142); |
88 packed.push_back(13u); | 93 packed.push_back(13); |
89 // Run of two relocations, 4 byte deltas. | 94 // Run of two relocations, 4 byte deltas. |
90 packed.push_back(2u); | 95 packed.push_back(2); |
91 packed.push_back(4u); | 96 packed.push_back(4); |
92 // Run of three relocations, 8 byte deltas. | 97 // Run of three relocations, 8 byte deltas. |
93 packed.push_back(3u); | 98 packed.push_back(3); |
94 packed.push_back(8u); | 99 packed.push_back(8); |
95 // Padding. | 100 // Padding. |
96 packed.push_back(0u); | 101 packed.push_back(0); |
97 packed.push_back(0u); | 102 packed.push_back(0); |
98 | 103 |
99 relocations.clear(); | 104 relocations.clear(); |
100 packer.UnpackRelativeRelocations(packed, &relocations); | 105 packer.UnpackRelativeRelocations(packed, &relocations); |
101 | 106 |
102 EXPECT_EQ(6u, relocations.size()); | 107 EXPECT_EQ(6, relocations.size()); |
103 // Initial relocation. | 108 // Initial relocation. |
104 EXPECT_TRUE(CheckRelocation(0xd1ce0000, relocations[0])); | 109 EXPECT_TRUE(CheckRelocation(0xd1ce0000, relocations[0])); |
105 // Two relocations, 4 byte deltas. | 110 // Two relocations, 4 byte deltas. |
106 EXPECT_TRUE(CheckRelocation(0xd1ce0004, relocations[1])); | 111 EXPECT_TRUE(CheckRelocation(0xd1ce0004, relocations[1])); |
107 EXPECT_TRUE(CheckRelocation(0xd1ce0008, relocations[2])); | 112 EXPECT_TRUE(CheckRelocation(0xd1ce0008, relocations[2])); |
108 // Three relocations, 8 byte deltas. | 113 // Three relocations, 8 byte deltas. |
109 EXPECT_TRUE(CheckRelocation(0xd1ce0010, relocations[3])); | 114 EXPECT_TRUE(CheckRelocation(0xd1ce0010, relocations[3])); |
110 EXPECT_TRUE(CheckRelocation(0xd1ce0018, relocations[4])); | 115 EXPECT_TRUE(CheckRelocation(0xd1ce0018, relocations[4])); |
111 EXPECT_TRUE(CheckRelocation(0xd1ce0020, relocations[5])); | 116 EXPECT_TRUE(CheckRelocation(0xd1ce0020, relocations[5])); |
112 } | 117 } |
113 | 118 |
114 } // namespace relocation_packer | 119 } // namespace relocation_packer |
OLD | NEW |