| 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 // TODO(simonb): Extend for 64-bit target libraries. | |
| 6 | |
| 7 #include "packer.h" | 5 #include "packer.h" |
| 8 | 6 |
| 9 #include <string.h> | |
| 10 #include <string> | |
| 11 #include <vector> | 7 #include <vector> |
| 12 | 8 |
| 13 #include "debug.h" | 9 #include "debug.h" |
| 10 #include "elf_traits.h" |
| 14 #include "leb128.h" | 11 #include "leb128.h" |
| 15 #include "run_length_encoder.h" | 12 #include "run_length_encoder.h" |
| 16 | 13 |
| 17 namespace relocation_packer { | 14 namespace relocation_packer { |
| 18 | 15 |
| 19 // Pack R_ARM_RELATIVE relocations into a run-length encoded packed | 16 // Pack ARM relative relocations into a run-length encoded packed |
| 20 // representation. | 17 // representation. |
| 21 void RelocationPacker::PackRelativeRelocations( | 18 void RelocationPacker::PackRelativeRelocations( |
| 22 const std::vector<Elf32_Rel>& relocations, | 19 const std::vector<ELF::Rel>& relocations, |
| 23 std::vector<uint8_t>* packed) { | 20 std::vector<uint8_t>* packed) { |
| 24 | 21 |
| 25 // Run-length encode. | 22 // Run-length encode. |
| 26 std::vector<Elf32_Word> packed_words; | 23 std::vector<ELF::Xword> packed_words; |
| 27 RelocationRunLengthCodec codec; | 24 RelocationRunLengthCodec codec; |
| 28 codec.Encode(relocations, &packed_words); | 25 codec.Encode(relocations, &packed_words); |
| 29 | 26 |
| 30 // If insufficient data to run-length encode, do nothing. | 27 // If insufficient data to run-length encode, do nothing. |
| 31 if (packed_words.empty()) | 28 if (packed_words.empty()) |
| 32 return; | 29 return; |
| 33 | 30 |
| 34 // LEB128 encode, with "APR1" prefix. | 31 // LEB128 encode, with "APR1" prefix. |
| 35 Leb128Encoder encoder; | 32 Leb128Encoder encoder; |
| 36 encoder.Enqueue('A'); | 33 encoder.Enqueue('A'); |
| 37 encoder.Enqueue('P'); | 34 encoder.Enqueue('P'); |
| 38 encoder.Enqueue('R'); | 35 encoder.Enqueue('R'); |
| 39 encoder.Enqueue('1'); | 36 encoder.Enqueue('1'); |
| 40 encoder.EnqueueAll(packed_words); | 37 encoder.EnqueueAll(packed_words); |
| 41 | 38 |
| 42 encoder.GetEncoding(packed); | 39 encoder.GetEncoding(packed); |
| 43 | 40 |
| 44 // Pad packed to a whole number of words. This padding will decode as | 41 // Pad packed to a whole number of words. This padding will decode as |
| 45 // LEB128 zeroes. Run-length decoding ignores it because encoding | 42 // LEB128 zeroes. Run-length decoding ignores it because encoding |
| 46 // embeds the pairs count in the stream itself. | 43 // embeds the pairs count in the stream itself. |
| 47 while (packed->size() % sizeof(uint32_t)) | 44 while (packed->size() % sizeof(ELF::Word)) |
| 48 packed->push_back(0); | 45 packed->push_back(0); |
| 49 } | 46 } |
| 50 | 47 |
| 51 // Unpack R_ARM_RELATIVE relocations from a run-length encoded packed | 48 // Unpack ARM relative relocations from a run-length encoded packed |
| 52 // representation. | 49 // representation. |
| 53 void RelocationPacker::UnpackRelativeRelocations( | 50 void RelocationPacker::UnpackRelativeRelocations( |
| 54 const std::vector<uint8_t>& packed, | 51 const std::vector<uint8_t>& packed, |
| 55 std::vector<Elf32_Rel>* relocations) { | 52 std::vector<ELF::Rel>* relocations) { |
| 56 | 53 |
| 57 // LEB128 decode, after checking and stripping "APR1" prefix. | 54 // LEB128 decode, after checking and stripping "APR1" prefix. |
| 58 std::vector<Elf32_Word> packed_words; | 55 std::vector<ELF::Xword> packed_words; |
| 59 Leb128Decoder decoder(packed); | 56 Leb128Decoder decoder(packed); |
| 60 CHECK(decoder.Dequeue() == 'A' && decoder.Dequeue() == 'P' && | 57 CHECK(decoder.Dequeue() == 'A' && decoder.Dequeue() == 'P' && |
| 61 decoder.Dequeue() == 'R' && decoder.Dequeue() == '1'); | 58 decoder.Dequeue() == 'R' && decoder.Dequeue() == '1'); |
| 62 decoder.DequeueAll(&packed_words); | 59 decoder.DequeueAll(&packed_words); |
| 63 | 60 |
| 64 // Run-length decode. | 61 // Run-length decode. |
| 65 RelocationRunLengthCodec codec; | 62 RelocationRunLengthCodec codec; |
| 66 codec.Decode(packed_words, relocations); | 63 codec.Decode(packed_words, relocations); |
| 67 } | 64 } |
| 68 | 65 |
| 69 } // namespace relocation_packer | 66 } // namespace relocation_packer |
| OLD | NEW |