blob: 8e30612e550601a474d114b8e36d7a9dc8192984 [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#include "packer.h"
6
7#include <vector>
8
9#include "debug.h"
10#include "delta_encoder.h"
11#include "elf_traits.h"
12#include "leb128.h"
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080013#include "sleb128.h"
14
15namespace relocation_packer {
16
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080017// Pack relocations into a group encoded packed representation.
18template <typename ELF>
19void RelocationPacker<ELF>::PackRelocations(const std::vector<typename ELF::Rela>& relocations,
20 std::vector<uint8_t>* packed) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080021 // Run-length encode.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080022 std::vector<typename ELF::Addr> packed_words;
23 RelocationDeltaCodec<ELF> codec;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080024 codec.Encode(relocations, &packed_words);
25
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080026 // If insufficient data do nothing.
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080027 if (packed_words.empty())
28 return;
29
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080030 Sleb128Encoder<typename ELF::Addr> sleb128_encoder;
31 Leb128Encoder<typename ELF::Addr> leb128_encoder;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080032
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080033 std::vector<uint8_t> leb128_packed;
34 std::vector<uint8_t> sleb128_packed;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080035
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080036 leb128_encoder.EnqueueAll(packed_words);
37 leb128_encoder.GetEncoding(&leb128_packed);
38
39 sleb128_encoder.EnqueueAll(packed_words);
40 sleb128_encoder.GetEncoding(&sleb128_packed);
41
42 // TODO (simonb): Estimate savings on current android system image and consider using
43 // one encoder for all packed relocations to reduce complexity.
44 if (leb128_packed.size() <= sleb128_packed.size()) {
45 packed->push_back('A');
46 packed->push_back('P');
47 packed->push_back('U');
48 packed->push_back('2');
49 packed->insert(packed->end(), leb128_packed.begin(), leb128_packed.end());
50 } else {
51 packed->push_back('A');
52 packed->push_back('P');
53 packed->push_back('S');
54 packed->push_back('2');
55 packed->insert(packed->end(), sleb128_packed.begin(), sleb128_packed.end());
56 }
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080057}
58
59// Unpack relative relocations from a run-length encoded packed
60// representation.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080061template <typename ELF>
62void RelocationPacker<ELF>::UnpackRelocations(
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080063 const std::vector<uint8_t>& packed,
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080064 std::vector<typename ELF::Rela>* relocations) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080065
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080066 std::vector<typename ELF::Addr> packed_words;
67 CHECK(packed.size() > 4 &&
68 packed[0] == 'A' &&
69 packed[1] == 'P' &&
70 (packed[2] == 'U' || packed[2] == 'S') &&
71 packed[3] == '2');
72
73 if (packed[2] == 'U') {
74 Leb128Decoder<typename ELF::Addr> decoder(packed, 4);
75 decoder.DequeueAll(&packed_words);
76 } else {
77 Sleb128Decoder<typename ELF::Addr> decoder(packed, 4);
78 decoder.DequeueAll(&packed_words);
79 }
80
81 RelocationDeltaCodec<ELF> codec;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080082 codec.Decode(packed_words, relocations);
83}
84
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080085template class RelocationPacker<ELF32_traits>;
86template class RelocationPacker<ELF64_traits>;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080087
88} // namespace relocation_packer