Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Dmitriy Ivanov | 18870d3 | 2015-04-22 13:10:04 -0700 | [diff] [blame] | 17 | #ifndef _LINKER_SLEB128_H |
| 18 | #define _LINKER_SLEB128_H |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 19 | |
| 20 | #include <stdint.h> |
| 21 | |
Dimitry Ivanov | fa4aeed | 2016-04-05 13:29:50 -0700 | [diff] [blame^] | 22 | #include "linker_debug.h" |
| 23 | |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 24 | // Helper classes for decoding LEB128, used in packed relocation data. |
| 25 | // http://en.wikipedia.org/wiki/LEB128 |
| 26 | |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 27 | class sleb128_decoder { |
| 28 | public: |
| 29 | sleb128_decoder(const uint8_t* buffer, size_t count) |
| 30 | : current_(buffer), end_(buffer+count) { } |
| 31 | |
| 32 | size_t pop_front() { |
| 33 | size_t value = 0; |
| 34 | static const size_t size = CHAR_BIT * sizeof(value); |
| 35 | |
| 36 | size_t shift = 0; |
| 37 | uint8_t byte; |
| 38 | |
| 39 | do { |
| 40 | if (current_ >= end_) { |
Dmitriy Ivanov | 18870d3 | 2015-04-22 13:10:04 -0700 | [diff] [blame] | 41 | __libc_fatal("sleb128_decoder ran out of bounds"); |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 42 | } |
| 43 | byte = *current_++; |
| 44 | value |= (static_cast<size_t>(byte & 127) << shift); |
| 45 | shift += 7; |
| 46 | } while (byte & 128); |
| 47 | |
| 48 | if (shift < size && (byte & 64)) { |
| 49 | value |= -(static_cast<size_t>(1) << shift); |
| 50 | } |
| 51 | |
| 52 | return value; |
| 53 | } |
| 54 | |
| 55 | private: |
| 56 | const uint8_t* current_; |
| 57 | const uint8_t* const end_; |
| 58 | }; |
| 59 | |
Dmitriy Ivanov | 18870d3 | 2015-04-22 13:10:04 -0700 | [diff] [blame] | 60 | #endif // __LINKER_SLEB128_H |