Dmitriy Ivanov | 87a0617 | 2015-02-06 10:56:28 -0800 | [diff] [blame] | 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 | // Pack relative relocations into a more compact form. |
| 6 | // |
| 7 | // |
| 8 | // For relative relocations without addends (32 bit platforms) |
| 9 | // ----------------------------------------------------------- |
| 10 | // |
| 11 | // Applies two packing strategies. The first is run-length encoding, which |
| 12 | // turns a large set of relative relocations into a much smaller set |
| 13 | // of delta-count pairs, prefixed with a two-word header comprising the |
| 14 | // count of pairs and the initial relocation offset. The second is LEB128 |
| 15 | // encoding, which compresses the result of run-length encoding. |
| 16 | // |
| 17 | // Once packed, data is prefixed by an identifier that allows for any later |
| 18 | // versioning of packing strategies. |
| 19 | // |
| 20 | // A complete packed stream of relocations without addends might look |
| 21 | // something like: |
| 22 | // |
| 23 | // "APR1" pairs init_offset count1 delta1 count2 delta2 ... |
| 24 | // 41505231 f2b003 b08ac716 e001 04 01 10 ... |
| 25 | // |
| 26 | // |
| 27 | // For relative relocations with addends (64 bit platforms) |
| 28 | // -------------------------------------------------------- |
| 29 | // |
| 30 | // Applies two packing strategies. The first is delta encoding, which |
| 31 | // turns a large set of relative relocations into a smaller set |
| 32 | // of offset and addend delta pairs, prefixed with a header indicating the |
| 33 | // count of pairs. The second is signed LEB128 encoding, which compacts |
| 34 | // the result of delta encoding. |
| 35 | // |
| 36 | // Once packed, data is prefixed by an identifier that allows for any later |
| 37 | // versioning of packing strategies. |
| 38 | // |
| 39 | // A complete packed stream might look something like: |
| 40 | // |
| 41 | // "APA1" pairs offset_d1 addend_d1 offset_d2 addend_d2 ... |
| 42 | // 41505232 f2b018 04 28 08 9f01 ... |
| 43 | |
| 44 | #ifndef TOOLS_RELOCATION_PACKER_SRC_PACKER_H_ |
| 45 | #define TOOLS_RELOCATION_PACKER_SRC_PACKER_H_ |
| 46 | |
| 47 | #include <stdint.h> |
| 48 | #include <vector> |
| 49 | |
| 50 | #include "elf.h" |
Dmitriy Ivanov | 87a0617 | 2015-02-06 10:56:28 -0800 | [diff] [blame] | 51 | |
| 52 | namespace relocation_packer { |
| 53 | |
Dmitriy Ivanov | f8ff6b1 | 2015-01-27 19:32:56 -0800 | [diff] [blame] | 54 | // A RelocationPacker packs vectors of relocations into more |
Dmitriy Ivanov | 87a0617 | 2015-02-06 10:56:28 -0800 | [diff] [blame] | 55 | // compact forms, and unpacks them to reproduce the pre-packed data. |
Dmitriy Ivanov | f8ff6b1 | 2015-01-27 19:32:56 -0800 | [diff] [blame] | 56 | template <typename ELF> |
Dmitriy Ivanov | 87a0617 | 2015-02-06 10:56:28 -0800 | [diff] [blame] | 57 | class RelocationPacker { |
| 58 | public: |
Dmitriy Ivanov | f8ff6b1 | 2015-01-27 19:32:56 -0800 | [diff] [blame] | 59 | // Pack relocations into a more compact form. |
| 60 | // |relocations| is a vector of relocation structs. |
Dmitriy Ivanov | 87a0617 | 2015-02-06 10:56:28 -0800 | [diff] [blame] | 61 | // |packed| is the vector of packed bytes into which relocations are packed. |
Dmitriy Ivanov | f8ff6b1 | 2015-01-27 19:32:56 -0800 | [diff] [blame] | 62 | static void PackRelocations(const std::vector<typename ELF::Rela>& relocations, |
| 63 | std::vector<uint8_t>* packed); |
Dmitriy Ivanov | 87a0617 | 2015-02-06 10:56:28 -0800 | [diff] [blame] | 64 | |
Dmitriy Ivanov | f8ff6b1 | 2015-01-27 19:32:56 -0800 | [diff] [blame] | 65 | // Unpack relocations from their more compact form. |
Dmitriy Ivanov | 87a0617 | 2015-02-06 10:56:28 -0800 | [diff] [blame] | 66 | // |packed| is the vector of packed relocations. |
Dmitriy Ivanov | f8ff6b1 | 2015-01-27 19:32:56 -0800 | [diff] [blame] | 67 | // |relocations| is a vector of unpacked relocation structs. |
| 68 | static void UnpackRelocations(const std::vector<uint8_t>& packed, |
| 69 | std::vector<typename ELF::Rela>* relocations); |
Dmitriy Ivanov | 87a0617 | 2015-02-06 10:56:28 -0800 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | } // namespace relocation_packer |
| 73 | |
| 74 | #endif // TOOLS_RELOCATION_PACKER_SRC_PACKER_H_ |