Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1000)

Side by Side Diff: third_party/android_platform/bionic/tools/relocation_packer/src/packer_unittest.cc

Issue 1099343004: Synchronize Android relocation packer source with AOSP. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "elf_traits.h"
10 #include "gtest/gtest.h" 10 #include "gtest/gtest.h"
(...skipping 21 matching lines...) Expand all
32 relocation.r_info == info && 32 relocation.r_info == info &&
33 relocation.r_addend == addend; 33 relocation.r_addend == addend;
34 } 34 }
35 35
36 namespace relocation_packer { 36 namespace relocation_packer {
37 37
38 template <typename ELF> 38 template <typename ELF>
39 static void DoPackNoAddend() { 39 static void DoPackNoAddend() {
40 std::vector<typename ELF::Rela> relocations; 40 std::vector<typename ELF::Rela> relocations;
41 std::vector<uint8_t> packed; 41 std::vector<uint8_t> packed;
42 bool is_32 = sizeof(typename ELF::Addr) == 4;
42 // Initial relocation. 43 // Initial relocation.
43 AddRelocation<ELF>(0xd1ce0000, 0x11, 0, &relocations); 44 AddRelocation<ELF>(0xd1ce0000, 0x11, 0, &relocations);
44 // Two more relocations, 4 byte deltas. 45 // Two more relocations, 4 byte deltas.
45 AddRelocation<ELF>(0xd1ce0004, 0x11, 0, &relocations); 46 AddRelocation<ELF>(0xd1ce0004, 0x11, 0, &relocations);
46 AddRelocation<ELF>(0xd1ce0008, 0x11, 0, &relocations); 47 AddRelocation<ELF>(0xd1ce0008, 0x11, 0, &relocations);
47 // Three more relocations, 8 byte deltas. 48 // Three more relocations, 8 byte deltas.
48 AddRelocation<ELF>(0xd1ce0010, 0x11, 0, &relocations); 49 AddRelocation<ELF>(0xd1ce0010, 0x11, 0, &relocations);
49 AddRelocation<ELF>(0xd1ce0018, 0x11, 0, &relocations); 50 AddRelocation<ELF>(0xd1ce0018, 0x11, 0, &relocations);
50 AddRelocation<ELF>(0xd1ce0020, 0x11, 0, &relocations); 51 AddRelocation<ELF>(0xd1ce0020, 0x11, 0, &relocations);
51 52
52 RelocationPacker<ELF> packer; 53 RelocationPacker<ELF> packer;
53 54
54 packed.clear(); 55 packed.clear();
55 packer.PackRelocations(relocations, &packed); 56 packer.PackRelocations(relocations, &packed);
56 57
57 ASSERT_EQ(18U, packed.size()); 58 ASSERT_EQ(18U, packed.size());
58 // Identifier. 59 // Identifier.
59 size_t ndx = 0; 60 size_t ndx = 0;
60 EXPECT_EQ('A', packed[ndx++]); 61 EXPECT_EQ('A', packed[ndx++]);
61 EXPECT_EQ('P', packed[ndx++]); 62 EXPECT_EQ('P', packed[ndx++]);
62 EXPECT_EQ('U', packed[ndx++]); 63 EXPECT_EQ('S', packed[ndx++]);
63 EXPECT_EQ('2', packed[ndx++]); 64 EXPECT_EQ('2', packed[ndx++]);
64 // relocation count 65 // relocation count
65 EXPECT_EQ(6, packed[ndx++]); 66 EXPECT_EQ(6, packed[ndx++]);
66 // base relocation = 0xd1cdfffc -> fc, ff, b7, 8e, 0d 67 // base relocation = 0xd1cdfffc -> fc, ff, b7, 8e, 7d/0d (32/64bit)
67 EXPECT_EQ(0xfc, packed[ndx++]); 68 EXPECT_EQ(0xfc, packed[ndx++]);
68 EXPECT_EQ(0xff, packed[ndx++]); 69 EXPECT_EQ(0xff, packed[ndx++]);
69 EXPECT_EQ(0xb7, packed[ndx++]); 70 EXPECT_EQ(0xb7, packed[ndx++]);
70 EXPECT_EQ(0x8e, packed[ndx++]); 71 EXPECT_EQ(0x8e, packed[ndx++]);
71 EXPECT_EQ(0x0d, packed[ndx++]); 72 EXPECT_EQ(is_32 ? 0x7d : 0x0d, packed[ndx++]);
72 // first group 73 // first group
73 EXPECT_EQ(3, packed[ndx++]); // size 74 EXPECT_EQ(3, packed[ndx++]); // size
74 EXPECT_EQ(3, packed[ndx++]); // flags 75 EXPECT_EQ(3, packed[ndx++]); // flags
75 EXPECT_EQ(4, packed[ndx++]); // r_offset_delta 76 EXPECT_EQ(4, packed[ndx++]); // r_offset_delta
76 EXPECT_EQ(0x11, packed[ndx++]); // r_info 77 EXPECT_EQ(0x11, packed[ndx++]); // r_info
77 // second group 78 // second group
78 EXPECT_EQ(3, packed[ndx++]); // size 79 EXPECT_EQ(3, packed[ndx++]); // size
79 EXPECT_EQ(3, packed[ndx++]); // flags 80 EXPECT_EQ(3, packed[ndx++]); // flags
80 EXPECT_EQ(8, packed[ndx++]); // r_offset_delta 81 EXPECT_EQ(8, packed[ndx++]); // r_offset_delta
81 EXPECT_EQ(0x11, packed[ndx++]); // r_info 82 EXPECT_EQ(0x11, packed[ndx++]); // r_info
82 83
83 EXPECT_EQ(ndx, packed.size()); 84 EXPECT_EQ(ndx, packed.size());
84 } 85 }
85 86
86 TEST(Packer, PackNoAddend) { 87 TEST(Packer, PackNoAddend32) {
87 DoPackNoAddend<ELF32_traits>(); 88 DoPackNoAddend<ELF32_traits>();
89 }
90
91 TEST(Packer, PackNoAddend64) {
88 DoPackNoAddend<ELF64_traits>(); 92 DoPackNoAddend<ELF64_traits>();
89 } 93 }
90 94
91 template <typename ELF> 95 template <typename ELF>
92 static void DoUnpackNoAddend() { 96 static void DoUnpackNoAddend() {
93 std::vector<typename ELF::Rela> relocations; 97 std::vector<typename ELF::Rela> relocations;
94 std::vector<uint8_t> packed; 98 std::vector<uint8_t> packed;
99 bool is_32 = sizeof(typename ELF::Addr) == 4;
95 packed.push_back('A'); 100 packed.push_back('A');
96 packed.push_back('P'); 101 packed.push_back('P');
97 packed.push_back('U'); 102 packed.push_back('S');
98 packed.push_back('2'); 103 packed.push_back('2');
99 // relocation count 104 // relocation count
100 packed.push_back(6); 105 packed.push_back(6);
101 // base relocation = 0xd1cdfffc -> fc, ff, b7, 8e, 0d 106 // base relocation = 0xd1cdfffc -> fc, ff, b7, 8e, 7d/0d (32/64bit)
102 packed.push_back(0xfc); 107 packed.push_back(0xfc);
103 packed.push_back(0xff); 108 packed.push_back(0xff);
104 packed.push_back(0xb7); 109 packed.push_back(0xb7);
105 packed.push_back(0x8e); 110 packed.push_back(0x8e);
106 packed.push_back(0x0d); 111 packed.push_back(is_32 ? 0x7d : 0x0d);
107 // first group 112 // first group
108 packed.push_back(3); // size 113 packed.push_back(3); // size
109 packed.push_back(3); // flags 114 packed.push_back(3); // flags
110 packed.push_back(4); // r_offset_delta 115 packed.push_back(4); // r_offset_delta
111 packed.push_back(0x11); // r_info 116 packed.push_back(0x11); // r_info
112 // second group 117 // second group
113 packed.push_back(3); // size 118 packed.push_back(3); // size
114 packed.push_back(3); // flags 119 packed.push_back(3); // flags
115 packed.push_back(8); // r_offset_delta 120 packed.push_back(8); // r_offset_delta
116 packed.push_back(0x11); // r_info 121 packed.push_back(0x11); // r_info
117 122
118 RelocationPacker<ELF> packer; 123 RelocationPacker<ELF> packer;
119 packer.UnpackRelocations(packed, &relocations); 124 packer.UnpackRelocations(packed, &relocations);
120 125
121 size_t ndx = 0; 126 size_t ndx = 0;
122 EXPECT_EQ(6U, relocations.size()); 127 EXPECT_EQ(6U, relocations.size());
123 EXPECT_TRUE(CheckRelocation<ELF>(0xd1ce0000, 0x11, 0, relocations[ndx++])); 128 EXPECT_TRUE(CheckRelocation<ELF>(0xd1ce0000, 0x11, 0, relocations[ndx++]));
124 EXPECT_TRUE(CheckRelocation<ELF>(0xd1ce0004, 0x11, 0, relocations[ndx++])); 129 EXPECT_TRUE(CheckRelocation<ELF>(0xd1ce0004, 0x11, 0, relocations[ndx++]));
125 EXPECT_TRUE(CheckRelocation<ELF>(0xd1ce0008, 0x11, 0, relocations[ndx++])); 130 EXPECT_TRUE(CheckRelocation<ELF>(0xd1ce0008, 0x11, 0, relocations[ndx++]));
126 131
127 EXPECT_TRUE(CheckRelocation<ELF>(0xd1ce0010, 0x11, 0, relocations[ndx++])); 132 EXPECT_TRUE(CheckRelocation<ELF>(0xd1ce0010, 0x11, 0, relocations[ndx++]));
128 EXPECT_TRUE(CheckRelocation<ELF>(0xd1ce0018, 0x11, 0, relocations[ndx++])); 133 EXPECT_TRUE(CheckRelocation<ELF>(0xd1ce0018, 0x11, 0, relocations[ndx++]));
129 EXPECT_TRUE(CheckRelocation<ELF>(0xd1ce0020, 0x11, 0, relocations[ndx++])); 134 EXPECT_TRUE(CheckRelocation<ELF>(0xd1ce0020, 0x11, 0, relocations[ndx++]));
130 135
131 EXPECT_EQ(ndx, relocations.size()); 136 EXPECT_EQ(ndx, relocations.size());
132 } 137 }
133 138
134 TEST(Packer, UnpackNoAddend) { 139 TEST(Packer, UnpackNoAddend32) {
135 DoUnpackNoAddend<ELF32_traits>(); 140 DoUnpackNoAddend<ELF32_traits>();
141 }
142
143 TEST(Packer, UnpackNoAddend64) {
136 DoUnpackNoAddend<ELF64_traits>(); 144 DoUnpackNoAddend<ELF64_traits>();
137 } 145 }
138 146
139 template <typename ELF> 147 template <typename ELF>
140 static void DoPackWithAddend() { 148 static void DoPackWithAddend() {
141 std::vector<typename ELF::Rela> relocations; 149 std::vector<typename ELF::Rela> relocations;
142 150
143 // Initial relocation. 151 // Initial relocation.
144 AddRelocation<ELF>(0xd1ce0000, 0x01, 10024, &relocations); 152 AddRelocation<ELF>(0xd1ce0000, 0x01, 10024, &relocations);
145 // Two more relocations, 4 byte offset deltas, 12 byte addend deltas. 153 // Two more relocations, 4 byte offset deltas, 12 byte addend deltas.
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 291
284 EXPECT_EQ(ndx, relocations.size()); 292 EXPECT_EQ(ndx, relocations.size());
285 } 293 }
286 294
287 TEST(Packer, UnpackWithAddend) { 295 TEST(Packer, UnpackWithAddend) {
288 DoUnpackWithAddend<ELF32_traits>(); 296 DoUnpackWithAddend<ELF32_traits>();
289 DoUnpackWithAddend<ELF64_traits>(); 297 DoUnpackWithAddend<ELF64_traits>();
290 } 298 }
291 299
292 } // namespace relocation_packer 300 } // namespace relocation_packer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698