blob: c4df2597b39f0d039a5d6b333fa50df9898fd965 [file] [log] [blame]
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08001/*
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 Ivanov18870d32015-04-22 13:10:04 -070017#ifndef _LINKER_SLEB128_H
18#define _LINKER_SLEB128_H
Dmitriy Ivanov18a69562015-02-04 16:05:30 -080019
20#include <stdint.h>
21
Dimitry Ivanovfa4aeed2016-04-05 13:29:50 -070022#include "linker_debug.h"
23
Dmitriy Ivanov18a69562015-02-04 16:05:30 -080024// Helper classes for decoding LEB128, used in packed relocation data.
25// http://en.wikipedia.org/wiki/LEB128
26
Dmitriy Ivanov18a69562015-02-04 16:05:30 -080027class 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 Ivanov18870d32015-04-22 13:10:04 -070041 __libc_fatal("sleb128_decoder ran out of bounds");
Dmitriy Ivanov18a69562015-02-04 16:05:30 -080042 }
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 Ivanov18870d32015-04-22 13:10:04 -070060#endif // __LINKER_SLEB128_H