blob: 8a57e623b0d67b91d482b75343f15198a37f21d1 [file] [log] [blame]
Dmitriy Ivanov87a06172015-02-06 10:56:28 -08001// 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 Ivanov87a06172015-02-06 10:56:28 -080051
52namespace relocation_packer {
53
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080054// A RelocationPacker packs vectors of relocations into more
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080055// compact forms, and unpacks them to reproduce the pre-packed data.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080056template <typename ELF>
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080057class RelocationPacker {
58 public:
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080059 // Pack relocations into a more compact form.
60 // |relocations| is a vector of relocation structs.
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080061 // |packed| is the vector of packed bytes into which relocations are packed.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080062 static void PackRelocations(const std::vector<typename ELF::Rela>& relocations,
63 std::vector<uint8_t>* packed);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080064
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080065 // Unpack relocations from their more compact form.
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080066 // |packed| is the vector of packed relocations.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080067 // |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 Ivanov87a06172015-02-06 10:56:28 -080070};
71
72} // namespace relocation_packer
73
74#endif // TOOLS_RELOCATION_PACKER_SRC_PACKER_H_