Chromium Code Reviews| 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 | 8 |
| 9 #include "debug.h" | 9 #include "debug.h" |
| 10 #include "delta_encoder.h" | |
| 10 #include "elf_traits.h" | 11 #include "elf_traits.h" |
| 11 #include "leb128.h" | 12 #include "leb128.h" |
| 12 #include "run_length_encoder.h" | 13 #include "run_length_encoder.h" |
| 14 #include "sleb128.h" | |
| 13 | 15 |
| 14 namespace relocation_packer { | 16 namespace relocation_packer { |
| 15 | 17 |
| 16 // Pack ARM relative relocations into a run-length encoded packed | 18 // Pack relative relocations into a run-length encoded packed |
| 17 // representation. | 19 // representation. |
| 18 void RelocationPacker::PackRelativeRelocations( | 20 void RelocationPacker::PackRelativeRelocations( |
| 19 const std::vector<ELF::Rel>& relocations, | 21 const std::vector<ELF::Rel>& relocations, |
| 20 std::vector<uint8_t>* packed) { | 22 std::vector<uint8_t>* packed) { |
| 21 | |
| 22 // Run-length encode. | 23 // Run-length encode. |
| 23 std::vector<ELF::Xword> packed_words; | 24 std::vector<ELF::Xword> packed_words; |
| 24 RelocationRunLengthCodec codec; | 25 RelocationRunLengthCodec codec; |
| 25 codec.Encode(relocations, &packed_words); | 26 codec.Encode(relocations, &packed_words); |
| 26 | 27 |
| 27 // If insufficient data to run-length encode, do nothing. | 28 // If insufficient data to run-length encode, do nothing. |
| 28 if (packed_words.empty()) | 29 if (packed_words.empty()) |
| 29 return; | 30 return; |
| 30 | 31 |
| 31 // LEB128 encode, with "APR1" prefix. | 32 // LEB128 encode, with "APR1" prefix. |
| 32 Leb128Encoder encoder; | 33 Leb128Encoder encoder; |
| 33 encoder.Enqueue('A'); | 34 encoder.Enqueue('A'); |
| 34 encoder.Enqueue('P'); | 35 encoder.Enqueue('P'); |
| 35 encoder.Enqueue('R'); | 36 encoder.Enqueue('R'); |
| 36 encoder.Enqueue('1'); | 37 encoder.Enqueue('1'); |
| 37 encoder.EnqueueAll(packed_words); | 38 encoder.EnqueueAll(packed_words); |
| 38 | 39 |
| 39 encoder.GetEncoding(packed); | 40 encoder.GetEncoding(packed); |
| 40 | 41 |
| 41 // Pad packed to a whole number of words. This padding will decode as | 42 // Pad packed to a whole number of words. This padding will decode as |
| 42 // LEB128 zeroes. Run-length decoding ignores it because encoding | 43 // LEB128 zeroes. Run-length decoding ignores it because encoding |
| 43 // embeds the pairs count in the stream itself. | 44 // embeds the pairs count in the stream itself. |
| 44 while (packed->size() % sizeof(ELF::Word)) | 45 while (packed->size() % sizeof(ELF::Word)) |
| 45 packed->push_back(0); | 46 packed->push_back(0); |
| 46 } | 47 } |
| 47 | 48 |
| 48 // Unpack ARM relative relocations from a run-length encoded packed | 49 // Unpack relative relocations from a run-length encoded packed |
| 49 // representation. | 50 // representation. |
| 50 void RelocationPacker::UnpackRelativeRelocations( | 51 void RelocationPacker::UnpackRelativeRelocations( |
| 51 const std::vector<uint8_t>& packed, | 52 const std::vector<uint8_t>& packed, |
| 52 std::vector<ELF::Rel>* relocations) { | 53 std::vector<ELF::Rel>* relocations) { |
| 53 | |
| 54 // LEB128 decode, after checking and stripping "APR1" prefix. | 54 // LEB128 decode, after checking and stripping "APR1" prefix. |
| 55 std::vector<ELF::Xword> packed_words; | 55 std::vector<ELF::Xword> packed_words; |
| 56 Leb128Decoder decoder(packed); | 56 Leb128Decoder decoder(packed); |
| 57 CHECK(decoder.Dequeue() == 'A' && decoder.Dequeue() == 'P' && | 57 CHECK(decoder.Dequeue() == 'A' && decoder.Dequeue() == 'P' && |
| 58 decoder.Dequeue() == 'R' && decoder.Dequeue() == '1'); | 58 decoder.Dequeue() == 'R' && decoder.Dequeue() == '1'); |
| 59 decoder.DequeueAll(&packed_words); | 59 decoder.DequeueAll(&packed_words); |
| 60 | 60 |
| 61 // Run-length decode. | 61 // Run-length decode. |
| 62 RelocationRunLengthCodec codec; | 62 RelocationRunLengthCodec codec; |
| 63 codec.Decode(packed_words, relocations); | 63 codec.Decode(packed_words, relocations); |
| 64 } | 64 } |
| 65 | 65 |
| 66 // Pack relative relocations with addends into a delta encoded packed | |
| 67 // representation. | |
| 68 void RelocationPacker::PackRelativeRelocations( | |
| 69 const std::vector<ELF::Rela>& relocations, | |
| 70 std::vector<uint8_t>* packed) { | |
| 71 // Delta encode. | |
| 72 std::vector<ELF::Sxword> packed_words; | |
| 73 RelocationDeltaCodec codec; | |
| 74 codec.Encode(relocations, &packed_words); | |
| 75 | |
| 76 // If insufficient data to delta encode, do nothing. | |
| 77 if (packed_words.empty()) | |
| 78 return; | |
| 79 | |
| 80 // Signed LEB128 encode, with "APA1" prefix. ASCII does not encode as | |
| 81 // itself under signed LEB128, so we have to treat it specially. | |
| 82 Sleb128Encoder encoder; | |
| 83 encoder.EnqueueAll(packed_words); | |
| 84 encoder.GetEncoding(packed); | |
| 85 | |
| 86 std::vector<uint8_t> identifier; | |
| 87 identifier.push_back('A'); | |
| 88 identifier.push_back('P'); | |
| 89 identifier.push_back('A'); | |
| 90 identifier.push_back('1'); | |
| 91 packed->insert(packed->begin(), identifier.begin(), identifier.end()); | |
|
rmcilroy
2014/07/28 10:08:44
nit - can you insert the identifier before sleb12
simonb (inactive)
2014/07/28 12:20:56
Done.
| |
| 92 | |
| 93 // Pad packed to a whole number of words. This padding will decode as | |
| 94 // signed LEB128 zeroes. Delta decoding ignores it because encoding | |
| 95 // embeds the pairs count in the stream itself. | |
| 96 while (packed->size() % sizeof(ELF::Word)) | |
| 97 packed->push_back(0); | |
| 98 } | |
| 99 | |
| 100 // Unpack relative relocations with addends from a delta encoded | |
| 101 // packed representation. | |
| 102 void RelocationPacker::UnpackRelativeRelocations( | |
| 103 const std::vector<uint8_t>& packed, | |
| 104 std::vector<ELF::Rela>* relocations) { | |
| 105 // Check "APA1" prefix. | |
| 106 CHECK(packed.at(0) == 'A' && packed.at(1) == 'P' && | |
| 107 packed.at(2) == 'A' && packed.at(3) == '1'); | |
| 108 | |
| 109 // Signed LEB128 decode, after stripping "APA1" prefix. | |
| 110 std::vector<ELF::Sxword> packed_words; | |
| 111 std::vector<uint8_t> stripped(packed.begin() + 4, packed.end()); | |
| 112 Sleb128Decoder decoder(stripped); | |
| 113 decoder.DequeueAll(&packed_words); | |
| 114 | |
| 115 // Delta decode. | |
| 116 RelocationDeltaCodec codec; | |
| 117 codec.Decode(packed_words, relocations); | |
| 118 } | |
| 119 | |
| 66 } // namespace relocation_packer | 120 } // namespace relocation_packer |
| OLD | NEW |