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 "run_length_encoder.h" | 5 #include "run_length_encoder.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(Rle, Encode) { | 31 TEST(RunLength, Encode) { |
27 std::vector<Elf32_Rel> relocations; | 32 std::vector<ELF::Rel> relocations; |
28 std::vector<Elf32_Word> packed; | 33 std::vector<ELF::Xword> packed; |
29 | 34 |
30 RelocationRunLengthCodec codec; | 35 RelocationRunLengthCodec codec; |
31 | 36 |
32 packed.clear(); | 37 packed.clear(); |
33 codec.Encode(relocations, &packed); | 38 codec.Encode(relocations, &packed); |
34 | 39 |
35 EXPECT_EQ(0u, packed.size()); | 40 EXPECT_EQ(0, packed.size()); |
36 | 41 |
37 // Add one relocation (insufficient data to encode). | 42 // Add one relocation (insufficient data to encode). |
38 AddRelocation(0xf00d0000, &relocations); | 43 AddRelocation(0xf00d0000, &relocations); |
39 | 44 |
40 packed.clear(); | 45 packed.clear(); |
41 codec.Encode(relocations, &packed); | 46 codec.Encode(relocations, &packed); |
42 | 47 |
43 EXPECT_EQ(0u, packed.size()); | 48 EXPECT_EQ(0, packed.size()); |
44 | 49 |
45 // Add a second relocation, 4 byte delta (minimum data to encode). | 50 // Add a second relocation, 4 byte delta (minimum data to encode). |
46 AddRelocation(0xf00d0004, &relocations); | 51 AddRelocation(0xf00d0004, &relocations); |
47 | 52 |
48 packed.clear(); | 53 packed.clear(); |
49 codec.Encode(relocations, &packed); | 54 codec.Encode(relocations, &packed); |
50 | 55 |
51 EXPECT_EQ(4u, packed.size()); | 56 EXPECT_EQ(4, packed.size()); |
52 // One count-delta pair present. | 57 // One count-delta pair present. |
53 EXPECT_EQ(1u, packed[0]); | 58 EXPECT_EQ(1, packed[0]); |
54 // Initial relocation. | 59 // Initial relocation. |
55 EXPECT_EQ(0xf00d0000, packed[1]); | 60 EXPECT_EQ(0xf00d0000, packed[1]); |
56 // Run of a single relocation, 4 byte delta. | 61 // Run of a single relocation, 4 byte delta. |
57 EXPECT_EQ(1u, packed[2]); | 62 EXPECT_EQ(1, packed[2]); |
58 EXPECT_EQ(4u, packed[3]); | 63 EXPECT_EQ(4, packed[3]); |
59 | 64 |
60 // Add a third relocation, 4 byte delta. | 65 // Add a third relocation, 4 byte delta. |
61 AddRelocation(0xf00d0008, &relocations); | 66 AddRelocation(0xf00d0008, &relocations); |
62 | 67 |
63 // Add three more relocations, 8 byte deltas. | 68 // Add three more relocations, 8 byte deltas. |
64 AddRelocation(0xf00d0010, &relocations); | 69 AddRelocation(0xf00d0010, &relocations); |
65 AddRelocation(0xf00d0018, &relocations); | 70 AddRelocation(0xf00d0018, &relocations); |
66 AddRelocation(0xf00d0020, &relocations); | 71 AddRelocation(0xf00d0020, &relocations); |
67 | 72 |
68 packed.clear(); | 73 packed.clear(); |
69 codec.Encode(relocations, &packed); | 74 codec.Encode(relocations, &packed); |
70 | 75 |
71 EXPECT_EQ(6u, packed.size()); | 76 EXPECT_EQ(6, packed.size()); |
72 // Two count-delta pairs present. | 77 // Two count-delta pairs present. |
73 EXPECT_EQ(2u, packed[0]); | 78 EXPECT_EQ(2, packed[0]); |
74 // Initial relocation. | 79 // Initial relocation. |
75 EXPECT_EQ(0xf00d0000, packed[1]); | 80 EXPECT_EQ(0xf00d0000, packed[1]); |
76 // Run of two relocations, 4 byte deltas. | 81 // Run of two relocations, 4 byte deltas. |
77 EXPECT_EQ(2u, packed[2]); | 82 EXPECT_EQ(2, packed[2]); |
78 EXPECT_EQ(4u, packed[3]); | 83 EXPECT_EQ(4, packed[3]); |
79 // Run of three relocations, 8 byte deltas. | 84 // Run of three relocations, 8 byte deltas. |
80 EXPECT_EQ(3u, packed[4]); | 85 EXPECT_EQ(3, packed[4]); |
81 EXPECT_EQ(8u, packed[5]); | 86 EXPECT_EQ(8, packed[5]); |
82 } | 87 } |
83 | 88 |
84 TEST(Rle, Decode) { | 89 TEST(RunLength, Decode) { |
85 std::vector<Elf32_Word> packed; | 90 std::vector<ELF::Xword> packed; |
86 std::vector<Elf32_Rel> relocations; | 91 std::vector<ELF::Rel> relocations; |
87 | 92 |
88 RelocationRunLengthCodec codec; | 93 RelocationRunLengthCodec codec; |
89 codec.Decode(packed, &relocations); | 94 codec.Decode(packed, &relocations); |
90 | 95 |
91 EXPECT_EQ(0u, relocations.size()); | 96 EXPECT_EQ(0, relocations.size()); |
92 | 97 |
93 // Two count-delta pairs. | 98 // Two count-delta pairs. |
94 packed.push_back(2u); | 99 packed.push_back(2); |
95 // Initial relocation. | 100 // Initial relocation. |
96 packed.push_back(0xc0de0000); | 101 packed.push_back(0xc0de0000); |
97 // Run of two relocations, 4 byte deltas. | 102 // Run of two relocations, 4 byte deltas. |
98 packed.push_back(2u); | 103 packed.push_back(2); |
99 packed.push_back(4u); | 104 packed.push_back(4); |
100 // Run of three relocations, 8 byte deltas. | 105 // Run of three relocations, 8 byte deltas. |
101 packed.push_back(3u); | 106 packed.push_back(3); |
102 packed.push_back(8u); | 107 packed.push_back(8); |
103 | 108 |
104 relocations.clear(); | 109 relocations.clear(); |
105 codec.Decode(packed, &relocations); | 110 codec.Decode(packed, &relocations); |
106 | 111 |
107 EXPECT_EQ(6u, relocations.size()); | 112 EXPECT_EQ(6, relocations.size()); |
108 // Initial relocation. | 113 // Initial relocation. |
109 EXPECT_TRUE(CheckRelocation(0xc0de0000, relocations[0])); | 114 EXPECT_TRUE(CheckRelocation(0xc0de0000, relocations[0])); |
110 // Two relocations, 4 byte deltas. | 115 // Two relocations, 4 byte deltas. |
111 EXPECT_TRUE(CheckRelocation(0xc0de0004, relocations[1])); | 116 EXPECT_TRUE(CheckRelocation(0xc0de0004, relocations[1])); |
112 EXPECT_TRUE(CheckRelocation(0xc0de0008, relocations[2])); | 117 EXPECT_TRUE(CheckRelocation(0xc0de0008, relocations[2])); |
113 // Three relocations, 8 byte deltas. | 118 // Three relocations, 8 byte deltas. |
114 EXPECT_TRUE(CheckRelocation(0xc0de0010, relocations[3])); | 119 EXPECT_TRUE(CheckRelocation(0xc0de0010, relocations[3])); |
115 EXPECT_TRUE(CheckRelocation(0xc0de0018, relocations[4])); | 120 EXPECT_TRUE(CheckRelocation(0xc0de0018, relocations[4])); |
116 EXPECT_TRUE(CheckRelocation(0xc0de0020, relocations[5])); | 121 EXPECT_TRUE(CheckRelocation(0xc0de0020, relocations[5])); |
117 } | 122 } |
118 | 123 |
119 } // namespace relocation_packer | 124 } // namespace relocation_packer |
OLD | NEW |