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"
51#include "elf_traits.h"
52
53namespace relocation_packer {
54
55// A RelocationPacker packs vectors of relative relocations into more
56// compact forms, and unpacks them to reproduce the pre-packed data.
57class RelocationPacker {
58 public:
59  // Pack relative relocations into a more compact form.
60  // |relocations| is a vector of relative relocation structs.
61  // |packed| is the vector of packed bytes into which relocations are packed.
62  static void PackRelativeRelocations(const std::vector<ELF::Rel>& relocations,
63                                      std::vector<uint8_t>* packed);
64  static void PackRelativeRelocations(const std::vector<ELF::Rela>& relocations,
65                                      std::vector<uint8_t>* packed);
66
67  // Unpack relative relocations from their more compact form.
68  // |packed| is the vector of packed relocations.
69  // |relocations| is a vector of unpacked relative relocation structs.
70  static void UnpackRelativeRelocations(const std::vector<uint8_t>& packed,
71                                        std::vector<ELF::Rel>* relocations);
72  static void UnpackRelativeRelocations(const std::vector<uint8_t>& packed,
73                                        std::vector<ELF::Rela>* relocations);
74};
75
76}  // namespace relocation_packer
77
78#endif  // TOOLS_RELOCATION_PACKER_SRC_PACKER_H_
79