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