blob: 433611faa9a8a66fe7c9a278a65f4229b409f70a [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"
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080012#include "sleb128.h"
13
14namespace relocation_packer {
15
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080016// Pack relocations into a group encoded packed representation.
17template <typename ELF>
18void RelocationPacker<ELF>::PackRelocations(const std::vector<typename ELF::Rela>& relocations,
19 std::vector<uint8_t>* packed) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080020 // Run-length encode.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080021 std::vector<typename ELF::Addr> packed_words;
22 RelocationDeltaCodec<ELF> codec;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080023 codec.Encode(relocations, &packed_words);
24
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080025 // If insufficient data do nothing.
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080026 if (packed_words.empty())
27 return;
28
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080029 Sleb128Encoder<typename ELF::Addr> sleb128_encoder;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080030
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080031 std::vector<uint8_t> sleb128_packed;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080032
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080033 sleb128_encoder.EnqueueAll(packed_words);
34 sleb128_encoder.GetEncoding(&sleb128_packed);
35
Dmitriy Ivanovf15ceeb2015-04-21 15:03:04 -070036 packed->push_back('A');
37 packed->push_back('P');
38 packed->push_back('S');
39 packed->push_back('2');
40 packed->insert(packed->end(), sleb128_packed.begin(), sleb128_packed.end());
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080041}
42
43// Unpack relative relocations from a run-length encoded packed
44// representation.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080045template <typename ELF>
46void RelocationPacker<ELF>::UnpackRelocations(
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080047 const std::vector<uint8_t>& packed,
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080048 std::vector<typename ELF::Rela>* relocations) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080049
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080050 std::vector<typename ELF::Addr> packed_words;
51 CHECK(packed.size() > 4 &&
52 packed[0] == 'A' &&
53 packed[1] == 'P' &&
Dmitriy Ivanovf15ceeb2015-04-21 15:03:04 -070054 packed[2] == 'S' &&
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080055 packed[3] == '2');
56
Dmitriy Ivanovf15ceeb2015-04-21 15:03:04 -070057 Sleb128Decoder<typename ELF::Addr> decoder(packed, 4);
58 decoder.DequeueAll(&packed_words);
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080059
60 RelocationDeltaCodec<ELF> codec;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080061 codec.Decode(packed_words, relocations);
62}
63
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080064template class RelocationPacker<ELF32_traits>;
65template class RelocationPacker<ELF64_traits>;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080066
67} // namespace relocation_packer