Chromium Code Reviews| Index: tools/relocation_packer/src/packer.cc |
| diff --git a/tools/relocation_packer/src/packer.cc b/tools/relocation_packer/src/packer.cc |
| index c1d986260c4044dcf1987fa224b40d0055fa5199..e79ab1e9683243a104fa8d13a624daa3f5588a7c 100644 |
| --- a/tools/relocation_packer/src/packer.cc |
| +++ b/tools/relocation_packer/src/packer.cc |
| @@ -7,18 +7,19 @@ |
| #include <vector> |
| #include "debug.h" |
| +#include "delta_encoder.h" |
| #include "elf_traits.h" |
| #include "leb128.h" |
| #include "run_length_encoder.h" |
| +#include "sleb128.h" |
| namespace relocation_packer { |
| -// Pack ARM relative relocations into a run-length encoded packed |
| +// Pack relative relocations into a run-length encoded packed |
| // representation. |
| void RelocationPacker::PackRelativeRelocations( |
| const std::vector<ELF::Rel>& relocations, |
| std::vector<uint8_t>* packed) { |
| - |
| // Run-length encode. |
| std::vector<ELF::Xword> packed_words; |
| RelocationRunLengthCodec codec; |
| @@ -45,12 +46,11 @@ void RelocationPacker::PackRelativeRelocations( |
| packed->push_back(0); |
| } |
| -// Unpack ARM relative relocations from a run-length encoded packed |
| +// Unpack relative relocations from a run-length encoded packed |
| // representation. |
| void RelocationPacker::UnpackRelativeRelocations( |
| const std::vector<uint8_t>& packed, |
| std::vector<ELF::Rel>* relocations) { |
| - |
| // LEB128 decode, after checking and stripping "APR1" prefix. |
| std::vector<ELF::Xword> packed_words; |
| Leb128Decoder decoder(packed); |
| @@ -63,4 +63,58 @@ void RelocationPacker::UnpackRelativeRelocations( |
| codec.Decode(packed_words, relocations); |
| } |
| +// Pack relative relocations with addends into a delta encoded packed |
| +// representation. |
| +void RelocationPacker::PackRelativeRelocations( |
| + const std::vector<ELF::Rela>& relocations, |
| + std::vector<uint8_t>* packed) { |
| + // Delta encode. |
| + std::vector<ELF::Sxword> packed_words; |
| + RelocationDeltaCodec codec; |
| + codec.Encode(relocations, &packed_words); |
| + |
| + // If insufficient data to delta encode, do nothing. |
| + if (packed_words.empty()) |
| + return; |
| + |
| + // Signed LEB128 encode, with "APA1" prefix. ASCII does not encode as |
| + // itself under signed LEB128, so we have to treat it specially. |
| + Sleb128Encoder encoder; |
| + encoder.EnqueueAll(packed_words); |
| + encoder.GetEncoding(packed); |
| + |
| + std::vector<uint8_t> identifier; |
| + identifier.push_back('A'); |
| + identifier.push_back('P'); |
| + identifier.push_back('A'); |
| + identifier.push_back('1'); |
| + 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.
|
| + |
| + // Pad packed to a whole number of words. This padding will decode as |
| + // signed LEB128 zeroes. Delta decoding ignores it because encoding |
| + // embeds the pairs count in the stream itself. |
| + while (packed->size() % sizeof(ELF::Word)) |
| + packed->push_back(0); |
| +} |
| + |
| +// Unpack relative relocations with addends from a delta encoded |
| +// packed representation. |
| +void RelocationPacker::UnpackRelativeRelocations( |
| + const std::vector<uint8_t>& packed, |
| + std::vector<ELF::Rela>* relocations) { |
| + // Check "APA1" prefix. |
| + CHECK(packed.at(0) == 'A' && packed.at(1) == 'P' && |
| + packed.at(2) == 'A' && packed.at(3) == '1'); |
| + |
| + // Signed LEB128 decode, after stripping "APA1" prefix. |
| + std::vector<ELF::Sxword> packed_words; |
| + std::vector<uint8_t> stripped(packed.begin() + 4, packed.end()); |
| + Sleb128Decoder decoder(stripped); |
| + decoder.DequeueAll(&packed_words); |
| + |
| + // Delta decode. |
| + RelocationDeltaCodec codec; |
| + codec.Decode(packed_words, relocations); |
| +} |
| + |
| } // namespace relocation_packer |