Port Android relocation packer to chromium build
[chromium-blink-merge.git] / third_party / android_platform / bionic / tools / relocation_packer / src / packer.h
blob8a57e623b0d67b91d482b75343f15198a37f21d1
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.
5 // Pack relative relocations into a more compact form.
6 //
7 //
8 // For relative relocations without addends (32 bit platforms)
9 // -----------------------------------------------------------
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.
17 // Once packed, data is prefixed by an identifier that allows for any later
18 // versioning of packing strategies.
20 // A complete packed stream of relocations without addends might look
21 // something like:
23 // "APR1" pairs init_offset count1 delta1 count2 delta2 ...
24 // 41505231 f2b003 b08ac716 e001 04 01 10 ...
27 // For relative relocations with addends (64 bit platforms)
28 // --------------------------------------------------------
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.
36 // Once packed, data is prefixed by an identifier that allows for any later
37 // versioning of packing strategies.
39 // A complete packed stream might look something like:
41 // "APA1" pairs offset_d1 addend_d1 offset_d2 addend_d2 ...
42 // 41505232 f2b018 04 28 08 9f01 ...
44 #ifndef TOOLS_RELOCATION_PACKER_SRC_PACKER_H_
45 #define TOOLS_RELOCATION_PACKER_SRC_PACKER_H_
47 #include <stdint.h>
48 #include <vector>
50 #include "elf.h"
52 namespace relocation_packer {
54 // A RelocationPacker packs vectors of relocations into more
55 // compact forms, and unpacks them to reproduce the pre-packed data.
56 template <typename ELF>
57 class RelocationPacker {
58 public:
59 // Pack relocations into a more compact form.
60 // |relocations| is a vector of relocation structs.
61 // |packed| is the vector of packed bytes into which relocations are packed.
62 static void PackRelocations(const std::vector<typename ELF::Rela>& relocations,
63 std::vector<uint8_t>* packed);
65 // Unpack relocations from their more compact form.
66 // |packed| is the vector of packed relocations.
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);
72 } // namespace relocation_packer
74 #endif // TOOLS_RELOCATION_PACKER_SRC_PACKER_H_