| 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 // Run-length encode and decode ARM relative relocations. | 5 // Run-length encode and decode relative relocations. |
| 6 // | 6 // |
| 7 // ARM relative relocations are the bulk of dynamic relocations (the | 7 // Relative relocations are the bulk of dynamic relocations (the |
| 8 // .rel.dyn section) in libchrome<version>.so, and the ELF standard | 8 // .rel.dyn or .rela.dyn sections) in libchrome<version>.so, and the ELF |
| 9 // representation of them is wasteful. | 9 // standard representation of them is wasteful. .rel.dyn contains |
| 10 // relocations without addends, .rela.dyn relocations with addends. |
| 10 // | 11 // |
| 11 // A relocation is 8 bytes (16 bytes on 64 bit plaforms), split into offset | 12 // A relocation with no addend is 8 bytes on 32 bit platforms and 16 bytes |
| 12 // and info fields. Offsets strictly increase, and each is commonly a | 13 // on 64 bit plaforms, split into offset and info fields. Offsets strictly |
| 13 // few bytes different from its predecessor. There are long runs where | 14 // increase, and each is commonly a few bytes different from its predecessor. |
| 14 // the difference does not change. The info field is always 0x17. Example, | 15 // There are long runs where the difference does not change. The info field |
| 15 // from 'readelf -x4 libchrome.<version>.so': | 16 // is constant. Example, from 'readelf -x4 libchrome.<version>.so' 32 bit: |
| 16 // | 17 // |
| 17 // offset info offset info | 18 // offset info offset info |
| 18 // 808fef01 17000000 848fef01 17000000 ................ | 19 // 808fef01 17000000 848fef01 17000000 ................ |
| 19 // 888fef01 17000000 8c8fef01 17000000 ................ | 20 // 888fef01 17000000 8c8fef01 17000000 ................ |
| 20 // 908fef01 17000000 948fef01 17000000 ................ | 21 // 908fef01 17000000 948fef01 17000000 ................ |
| 21 // | 22 // |
| 22 // Run length encoding packs this data more efficiently, by representing it | 23 // Run length encoding packs this data more efficiently, by representing it |
| 23 // as a delta and a count of entries each differing from its predecessor | 24 // as a delta and a count of entries each differing from its predecessor |
| 24 // by this delta. The above can be represented as a start address followed | 25 // by this delta. The above can be represented as a start address followed |
| 25 // by an encoded count of 6 and offset difference of 4: | 26 // by an encoded count of 6 and offset difference of 4: |
| 26 // | 27 // |
| 27 // start count diff | 28 // start count diff |
| 28 // 808fef01 00000006 00000004 | 29 // 01ef8f80 00000006 00000004 |
| 29 // | 30 // |
| 30 // Because ARM relative relocation offsets strictly increase, the complete | 31 // Because relative relocation offsets strictly increase, the complete |
| 31 // set of ARM relative relocations in libchrome.<version>.so can be | 32 // set of relative relocations in libchrome.<version>.so can be |
| 32 // represented by a single start address followed by one or more difference | 33 // represented by a single start address followed by one or more difference |
| 33 // and count encoded word pairs: | 34 // and count encoded word pairs: |
| 34 // | 35 // |
| 35 // start run1 count run1 diff run2 count run2 diff | 36 // start run1 count run1 diff run2 count run2 diff |
| 36 // 808fef01 00000006 00000004 00000010 00000008 ... | 37 // 01ef8f80 00000006 00000004 00000010 00000008 ... |
| 37 // | 38 // |
| 38 // Decoding regenerates ARM relative relocations beginning at address | 39 // Decoding regenerates relative relocations beginning at address |
| 39 // 'start' and for each encoded run, incrementing the address by 'difference' | 40 // 'start' and for each encoded run, incrementing the address by 'difference' |
| 40 // for 'count' iterations and emitting a new ARM relative relocation. | 41 // for 'count' iterations and emitting a new relative relocation. |
| 41 // | 42 // |
| 42 // Once encoded, data is prefixed by a single word count of packed delta and | 43 // Once encoded, data is prefixed by a single word count of packed delta and |
| 43 // count pairs. A final run-length encoded ARM relative relocations vector | 44 // count pairs. A final run-length encoded relative relocations vector |
| 44 // might therefore look something like: | 45 // might therefore look something like: |
| 45 // | 46 // |
| 46 // pairs start run 1 run 2 ... run 15 | 47 // pairs start run 1 run 2 ... run 15 |
| 47 // 0000000f 808fef01 00000006 00000004 00000010 00000008 ... | 48 // 0000000f 01ef8f80 00000006 00000004 00000010 00000008 ... |
| 48 // Interpreted as: | 49 // Interpreted as: |
| 49 // pairs=15 start=.. count=6,delta=4 count=16,delta=8 | 50 // pairs=15 start=.. count=6,delta=4 count=16,delta=8 |
| 50 | 51 |
| 51 #ifndef TOOLS_RELOCATION_PACKER_SRC_RUN_LENGTH_ENCODER_H_ | 52 #ifndef TOOLS_RELOCATION_PACKER_SRC_RUN_LENGTH_ENCODER_H_ |
| 52 #define TOOLS_RELOCATION_PACKER_SRC_RUN_LENGTH_ENCODER_H_ | 53 #define TOOLS_RELOCATION_PACKER_SRC_RUN_LENGTH_ENCODER_H_ |
| 53 | 54 |
| 54 #include <vector> | 55 #include <vector> |
| 55 | 56 |
| 56 #include "elf.h" | 57 #include "elf.h" |
| 57 #include "elf_traits.h" | 58 #include "elf_traits.h" |
| 58 | 59 |
| 59 namespace relocation_packer { | 60 namespace relocation_packer { |
| 60 | 61 |
| 61 // A RelocationRunLengthCodec packs vectors of ARM relative relocations | 62 // A RelocationRunLengthCodec packs vectors of relative relocations |
| 62 // into more compact forms, and unpacks them to reproduce the pre-packed data. | 63 // into more compact forms, and unpacks them to reproduce the pre-packed data. |
| 63 class RelocationRunLengthCodec { | 64 class RelocationRunLengthCodec { |
| 64 public: | 65 public: |
| 65 // Encode ARM relative relocations into a more compact form. | 66 // Encode relative relocations into a more compact form. |
| 66 // |relocations| is a vector of ARM relative relocation structs. | 67 // |relocations| is a vector of relative relocation structs. |
| 67 // |packed| is the vector of packed words into which relocations are packed. | 68 // |packed| is the vector of packed words into which relocations are packed. |
| 68 static void Encode(const std::vector<ELF::Rel>& relocations, | 69 static void Encode(const std::vector<ELF::Rel>& relocations, |
| 69 std::vector<ELF::Xword>* packed); | 70 std::vector<ELF::Xword>* packed); |
| 70 | 71 |
| 71 // Decode ARM relative relocations from their more compact form. | 72 // Decode relative relocations from their more compact form. |
| 72 // |packed| is the vector of packed relocations. | 73 // |packed| is the vector of packed relocations. |
| 73 // |relocations| is a vector of unpacked ARM relative relocation structs. | 74 // |relocations| is a vector of unpacked relative relocation structs. |
| 74 static void Decode(const std::vector<ELF::Xword>& packed, | 75 static void Decode(const std::vector<ELF::Xword>& packed, |
| 75 std::vector<ELF::Rel>* relocations); | 76 std::vector<ELF::Rel>* relocations); |
| 76 }; | 77 }; |
| 77 | 78 |
| 78 } // namespace relocation_packer | 79 } // namespace relocation_packer |
| 79 | 80 |
| 80 #endif // TOOLS_RELOCATION_PACKER_SRC_RUN_LENGTH_ENCODER_H_ | 81 #endif // TOOLS_RELOCATION_PACKER_SRC_RUN_LENGTH_ENCODER_H_ |
| OLD | NEW |