Dmitriy Ivanov | 87a0617 | 2015-02-06 10:56:28 -0800 | [diff] [blame] | 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 | #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 Ivanov | 87a0617 | 2015-02-06 10:56:28 -0800 | [diff] [blame] | 13 | #include "sleb128.h" |
| 14 | |
| 15 | namespace relocation_packer { |
| 16 | |
Dmitriy Ivanov | f8ff6b1 | 2015-01-27 19:32:56 -0800 | [diff] [blame] | 17 | // Pack relocations into a group encoded packed representation. |
| 18 | template <typename ELF> |
| 19 | void RelocationPacker<ELF>::PackRelocations(const std::vector<typename ELF::Rela>& relocations, |
| 20 | std::vector<uint8_t>* packed) { |
Dmitriy Ivanov | 87a0617 | 2015-02-06 10:56:28 -0800 | [diff] [blame] | 21 | // Run-length encode. |
Dmitriy Ivanov | f8ff6b1 | 2015-01-27 19:32:56 -0800 | [diff] [blame] | 22 | std::vector<typename ELF::Addr> packed_words; |
| 23 | RelocationDeltaCodec<ELF> codec; |
Dmitriy Ivanov | 87a0617 | 2015-02-06 10:56:28 -0800 | [diff] [blame] | 24 | codec.Encode(relocations, &packed_words); |
| 25 | |
Dmitriy Ivanov | f8ff6b1 | 2015-01-27 19:32:56 -0800 | [diff] [blame] | 26 | // If insufficient data do nothing. |
Dmitriy Ivanov | 87a0617 | 2015-02-06 10:56:28 -0800 | [diff] [blame] | 27 | if (packed_words.empty()) |
| 28 | return; |
| 29 | |
Dmitriy Ivanov | f8ff6b1 | 2015-01-27 19:32:56 -0800 | [diff] [blame] | 30 | Sleb128Encoder<typename ELF::Addr> sleb128_encoder; |
| 31 | Leb128Encoder<typename ELF::Addr> leb128_encoder; |
Dmitriy Ivanov | 87a0617 | 2015-02-06 10:56:28 -0800 | [diff] [blame] | 32 | |
Dmitriy Ivanov | f8ff6b1 | 2015-01-27 19:32:56 -0800 | [diff] [blame] | 33 | std::vector<uint8_t> leb128_packed; |
| 34 | std::vector<uint8_t> sleb128_packed; |
Dmitriy Ivanov | 87a0617 | 2015-02-06 10:56:28 -0800 | [diff] [blame] | 35 | |
Dmitriy Ivanov | f8ff6b1 | 2015-01-27 19:32:56 -0800 | [diff] [blame] | 36 | 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 Ivanov | 87a0617 | 2015-02-06 10:56:28 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | // Unpack relative relocations from a run-length encoded packed |
| 60 | // representation. |
Dmitriy Ivanov | f8ff6b1 | 2015-01-27 19:32:56 -0800 | [diff] [blame] | 61 | template <typename ELF> |
| 62 | void RelocationPacker<ELF>::UnpackRelocations( |
Dmitriy Ivanov | 87a0617 | 2015-02-06 10:56:28 -0800 | [diff] [blame] | 63 | const std::vector<uint8_t>& packed, |
Dmitriy Ivanov | f8ff6b1 | 2015-01-27 19:32:56 -0800 | [diff] [blame] | 64 | std::vector<typename ELF::Rela>* relocations) { |
Dmitriy Ivanov | 87a0617 | 2015-02-06 10:56:28 -0800 | [diff] [blame] | 65 | |
Dmitriy Ivanov | f8ff6b1 | 2015-01-27 19:32:56 -0800 | [diff] [blame] | 66 | 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 Ivanov | 87a0617 | 2015-02-06 10:56:28 -0800 | [diff] [blame] | 82 | codec.Decode(packed_words, relocations); |
| 83 | } |
| 84 | |
Dmitriy Ivanov | f8ff6b1 | 2015-01-27 19:32:56 -0800 | [diff] [blame] | 85 | template class RelocationPacker<ELF32_traits>; |
| 86 | template class RelocationPacker<ELF64_traits>; |
Dmitriy Ivanov | 87a0617 | 2015-02-06 10:56:28 -0800 | [diff] [blame] | 87 | |
| 88 | } // namespace relocation_packer |