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
3 // found in the LICENSE file.
9 #include "elf_traits.h"
10 #include "testing/gtest/include/gtest/gtest.h"
14 void AddRelocation(ELF::Addr addr
, std::vector
<ELF::Rel
>* relocations
) {
16 relocation
.r_offset
= addr
;
17 relocation
.r_info
= ELF_R_INFO(0, ELF::kRelativeRelocationCode
);
18 relocations
->push_back(relocation
);
21 bool CheckRelocation(ELF::Addr addr
, const ELF::Rel
& relocation
) {
22 return relocation
.r_offset
== addr
&&
23 ELF_R_SYM(relocation
.r_info
) == 0 &&
24 ELF_R_TYPE(relocation
.r_info
) == ELF::kRelativeRelocationCode
;
27 void AddRelocation(ELF::Addr addr
,
29 std::vector
<ELF::Rela
>* relocations
) {
31 relocation
.r_offset
= addr
;
32 relocation
.r_info
= ELF_R_INFO(0, ELF::kRelativeRelocationCode
);
33 relocation
.r_addend
= addend
;
34 relocations
->push_back(relocation
);
37 bool CheckRelocation(ELF::Addr addr
,
39 const ELF::Rela
& relocation
) {
40 return relocation
.r_offset
== addr
&&
41 ELF_R_SYM(relocation
.r_info
) == 0 &&
42 ELF_R_TYPE(relocation
.r_info
) == ELF::kRelativeRelocationCode
&&
43 relocation
.r_addend
== addend
;
48 namespace relocation_packer
{
50 TEST(Packer
, PackRel
) {
51 std::vector
<ELF::Rel
> relocations
;
52 std::vector
<uint8_t> packed
;
54 RelocationPacker packer
;
56 // Initial relocation.
57 AddRelocation(0xd1ce0000, &relocations
);
58 // Two more relocations, 4 byte deltas.
59 AddRelocation(0xd1ce0004, &relocations
);
60 AddRelocation(0xd1ce0008, &relocations
);
61 // Three more relocations, 8 byte deltas.
62 AddRelocation(0xd1ce0010, &relocations
);
63 AddRelocation(0xd1ce0018, &relocations
);
64 AddRelocation(0xd1ce0020, &relocations
);
67 packer
.PackRelativeRelocations(relocations
, &packed
);
69 EXPECT_EQ(16, packed
.size());
71 EXPECT_EQ('A', packed
[0]);
72 EXPECT_EQ('P', packed
[1]);
73 EXPECT_EQ('R', packed
[2]);
74 EXPECT_EQ('1', packed
[3]);
75 // Count-delta pairs count.
76 EXPECT_EQ(2, packed
[4]);
78 EXPECT_EQ(128, packed
[5]);
79 EXPECT_EQ(128, packed
[6]);
80 EXPECT_EQ(184, packed
[7]);
81 EXPECT_EQ(142, packed
[8]);
82 EXPECT_EQ(13, packed
[9]);
83 // Run of two relocations, 4 byte deltas.
84 EXPECT_EQ(2, packed
[10]);
85 EXPECT_EQ(4, packed
[11]);
86 // Run of three relocations, 8 byte deltas.
87 EXPECT_EQ(3, packed
[12]);
88 EXPECT_EQ(8, packed
[13]);
90 EXPECT_EQ(0, packed
[14]);
91 EXPECT_EQ(0, packed
[15]);
94 TEST(Packer
, UnpackRel
) {
95 std::vector
<uint8_t> packed
;
96 std::vector
<ELF::Rel
> relocations
;
98 RelocationPacker packer
;
101 packed
.push_back('A');
102 packed
.push_back('P');
103 packed
.push_back('R');
104 packed
.push_back('1');
105 // Count-delta pairs count.
108 packed
.push_back(128);
109 packed
.push_back(128);
110 packed
.push_back(184);
111 packed
.push_back(142);
112 packed
.push_back(13);
113 // Run of two relocations, 4 byte deltas.
116 // Run of three relocations, 8 byte deltas.
124 packer
.UnpackRelativeRelocations(packed
, &relocations
);
126 EXPECT_EQ(6, relocations
.size());
127 // Initial relocation.
128 EXPECT_TRUE(CheckRelocation(0xd1ce0000, relocations
[0]));
129 // Two relocations, 4 byte deltas.
130 EXPECT_TRUE(CheckRelocation(0xd1ce0004, relocations
[1]));
131 EXPECT_TRUE(CheckRelocation(0xd1ce0008, relocations
[2]));
132 // Three relocations, 8 byte deltas.
133 EXPECT_TRUE(CheckRelocation(0xd1ce0010, relocations
[3]));
134 EXPECT_TRUE(CheckRelocation(0xd1ce0018, relocations
[4]));
135 EXPECT_TRUE(CheckRelocation(0xd1ce0020, relocations
[5]));
138 TEST(Packer
, PackRela
) {
139 std::vector
<ELF::Rela
> relocations
;
140 std::vector
<uint8_t> packed
;
142 RelocationPacker packer
;
144 // Initial relocation.
145 AddRelocation(0xd1ce0000, 10000, &relocations
);
146 // Two more relocations, 4 byte offset deltas, 12 byte addend deltas.
147 AddRelocation(0xd1ce0004, 10012, &relocations
);
148 AddRelocation(0xd1ce0008, 10024, &relocations
);
149 // Three more relocations, 8 byte deltas, -24 byte addend deltas.
150 AddRelocation(0xd1ce0010, 10000, &relocations
);
151 AddRelocation(0xd1ce0018, 9976, &relocations
);
152 AddRelocation(0xd1ce0020, 9952, &relocations
);
155 packer
.PackRelativeRelocations(relocations
, &packed
);
157 EXPECT_EQ(24, packed
.size());
159 EXPECT_EQ('A', packed
[0]);
160 EXPECT_EQ('P', packed
[1]);
161 EXPECT_EQ('A', packed
[2]);
162 EXPECT_EQ('1', packed
[3]);
163 // Delta pairs count.
164 EXPECT_EQ(6, packed
[4]);
166 EXPECT_EQ(128, packed
[5]);
167 EXPECT_EQ(128, packed
[6]);
168 EXPECT_EQ(184, packed
[7]);
169 EXPECT_EQ(142, packed
[8]);
170 EXPECT_EQ(13, packed
[9]);
172 EXPECT_EQ(144, packed
[10]);
173 EXPECT_EQ(206, packed
[11]);
174 EXPECT_EQ(0, packed
[12]);
176 EXPECT_EQ(4, packed
[13]);
177 EXPECT_EQ(12, packed
[14]);
179 EXPECT_EQ(4, packed
[15]);
180 EXPECT_EQ(12, packed
[16]);
182 EXPECT_EQ(8, packed
[17]);
183 EXPECT_EQ(104, packed
[18]);
185 EXPECT_EQ(8, packed
[19]);
186 EXPECT_EQ(104, packed
[20]);
188 EXPECT_EQ(8, packed
[21]);
189 EXPECT_EQ(104, packed
[22]);
191 EXPECT_EQ(0, packed
[23]);
194 TEST(Packer
, UnpackRela
) {
195 std::vector
<uint8_t> packed
;
196 std::vector
<ELF::Rela
> relocations
;
198 RelocationPacker packer
;
201 packed
.push_back('A');
202 packed
.push_back('P');
203 packed
.push_back('A');
204 packed
.push_back('1');
205 // Delta pairs count.
208 packed
.push_back(128);
209 packed
.push_back(128);
210 packed
.push_back(184);
211 packed
.push_back(142);
212 packed
.push_back(13);
214 packed
.push_back(144);
215 packed
.push_back(206);
219 packed
.push_back(12);
222 packed
.push_back(12);
225 packed
.push_back(104);
228 packed
.push_back(104);
231 packed
.push_back(104);
236 packer
.UnpackRelativeRelocations(packed
, &relocations
);
238 EXPECT_EQ(6, relocations
.size());
239 // Initial relocation.
240 EXPECT_TRUE(CheckRelocation(0xd1ce0000, 10000, relocations
[0]));
241 // Two more relocations, 4 byte offset deltas, 12 byte addend deltas.
242 EXPECT_TRUE(CheckRelocation(0xd1ce0004, 10012, relocations
[1]));
243 EXPECT_TRUE(CheckRelocation(0xd1ce0008, 10024, relocations
[2]));
244 // Three more relocations, 8 byte offset deltas, -24 byte addend deltas.
245 EXPECT_TRUE(CheckRelocation(0xd1ce0010, 10000, relocations
[3]));
246 EXPECT_TRUE(CheckRelocation(0xd1ce0018, 9976, relocations
[4]));
247 EXPECT_TRUE(CheckRelocation(0xd1ce0020, 9952, relocations
[5]));
250 } // namespace relocation_packer