1*a9fa9459Szrj // int_encoding.cc -- variable length and unaligned integer encoding support.
2*a9fa9459Szrj
3*a9fa9459Szrj // Copyright (C) 2009-2016 Free Software Foundation, Inc.
4*a9fa9459Szrj // Written by Doug Kwan <dougkwan@google.com> by refactoring scattered
5*a9fa9459Szrj // contents from other files in gold. Original code written by Ian
6*a9fa9459Szrj // Lance Taylor <iant@google.com> and Caleb Howe <cshowe@google.com>.
7*a9fa9459Szrj
8*a9fa9459Szrj // This file is part of gold.
9*a9fa9459Szrj
10*a9fa9459Szrj // This program is free software; you can redistribute it and/or modify
11*a9fa9459Szrj // it under the terms of the GNU General Public License as published by
12*a9fa9459Szrj // the Free Software Foundation; either version 3 of the License, or
13*a9fa9459Szrj // (at your option) any later version.
14*a9fa9459Szrj
15*a9fa9459Szrj // This program is distributed in the hope that it will be useful,
16*a9fa9459Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
17*a9fa9459Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18*a9fa9459Szrj // GNU General Public License for more details.
19*a9fa9459Szrj
20*a9fa9459Szrj // You should have received a copy of the GNU General Public License
21*a9fa9459Szrj // along with this program; if not, write to the Free Software
22*a9fa9459Szrj // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
23*a9fa9459Szrj // MA 02110-1301, USA.
24*a9fa9459Szrj
25*a9fa9459Szrj #include "gold.h"
26*a9fa9459Szrj
27*a9fa9459Szrj #include <vector>
28*a9fa9459Szrj
29*a9fa9459Szrj #include "int_encoding.h"
30*a9fa9459Szrj
31*a9fa9459Szrj namespace gold {
32*a9fa9459Szrj
33*a9fa9459Szrj // Read an unsigned LEB128 number. Each byte contains 7 bits of
34*a9fa9459Szrj // information, plus one bit saying whether the number continues or
35*a9fa9459Szrj // not. BYTE contains the first byte of the number, and is guaranteed
36*a9fa9459Szrj // to have the continuation bit set.
37*a9fa9459Szrj
38*a9fa9459Szrj uint64_t
read_unsigned_LEB_128_x(const unsigned char * buffer,size_t * len,unsigned char byte)39*a9fa9459Szrj read_unsigned_LEB_128_x(const unsigned char* buffer, size_t* len,
40*a9fa9459Szrj unsigned char byte)
41*a9fa9459Szrj {
42*a9fa9459Szrj uint64_t result = static_cast<uint64_t>(byte & 0x7f);
43*a9fa9459Szrj size_t num_read = 1;
44*a9fa9459Szrj unsigned int shift = 7;
45*a9fa9459Szrj
46*a9fa9459Szrj do
47*a9fa9459Szrj {
48*a9fa9459Szrj if (num_read > 64 / 7 + 1)
49*a9fa9459Szrj {
50*a9fa9459Szrj gold_warning(_("Unusually large LEB128 decoded, "
51*a9fa9459Szrj "debug information may be corrupted"));
52*a9fa9459Szrj break;
53*a9fa9459Szrj }
54*a9fa9459Szrj byte = *buffer++;
55*a9fa9459Szrj num_read++;
56*a9fa9459Szrj result |= (static_cast<uint64_t>(byte & 0x7f)) << shift;
57*a9fa9459Szrj shift += 7;
58*a9fa9459Szrj }
59*a9fa9459Szrj while (byte & 0x80);
60*a9fa9459Szrj
61*a9fa9459Szrj *len = num_read;
62*a9fa9459Szrj
63*a9fa9459Szrj return result;
64*a9fa9459Szrj }
65*a9fa9459Szrj
66*a9fa9459Szrj // Read a signed LEB128 number. These are like regular LEB128
67*a9fa9459Szrj // numbers, except the last byte may have a sign bit set.
68*a9fa9459Szrj // BYTE contains the first byte of the number, and is guaranteed
69*a9fa9459Szrj // to have the continuation bit set.
70*a9fa9459Szrj
71*a9fa9459Szrj int64_t
read_signed_LEB_128_x(const unsigned char * buffer,size_t * len,unsigned char byte)72*a9fa9459Szrj read_signed_LEB_128_x(const unsigned char* buffer, size_t* len,
73*a9fa9459Szrj unsigned char byte)
74*a9fa9459Szrj {
75*a9fa9459Szrj int64_t result = static_cast<uint64_t>(byte & 0x7f);
76*a9fa9459Szrj int shift = 7;
77*a9fa9459Szrj size_t num_read = 1;
78*a9fa9459Szrj
79*a9fa9459Szrj do
80*a9fa9459Szrj {
81*a9fa9459Szrj if (num_read > 64 / 7 + 1)
82*a9fa9459Szrj {
83*a9fa9459Szrj gold_warning(_("Unusually large LEB128 decoded, "
84*a9fa9459Szrj "debug information may be corrupted"));
85*a9fa9459Szrj break;
86*a9fa9459Szrj }
87*a9fa9459Szrj byte = *buffer++;
88*a9fa9459Szrj num_read++;
89*a9fa9459Szrj result |= (static_cast<uint64_t>(byte & 0x7f) << shift);
90*a9fa9459Szrj shift += 7;
91*a9fa9459Szrj }
92*a9fa9459Szrj while (byte & 0x80);
93*a9fa9459Szrj
94*a9fa9459Szrj if ((shift < 8 * static_cast<int>(sizeof(result))) && (byte & 0x40))
95*a9fa9459Szrj result |= -((static_cast<int64_t>(1)) << shift);
96*a9fa9459Szrj *len = num_read;
97*a9fa9459Szrj return result;
98*a9fa9459Szrj }
99*a9fa9459Szrj
100*a9fa9459Szrj void
write_unsigned_LEB_128(std::vector<unsigned char> * buffer,uint64_t value)101*a9fa9459Szrj write_unsigned_LEB_128(std::vector<unsigned char>* buffer, uint64_t value)
102*a9fa9459Szrj {
103*a9fa9459Szrj do
104*a9fa9459Szrj {
105*a9fa9459Szrj unsigned char current_byte = value & 0x7f;
106*a9fa9459Szrj value >>= 7;
107*a9fa9459Szrj if (value != 0)
108*a9fa9459Szrj {
109*a9fa9459Szrj current_byte |= 0x80;
110*a9fa9459Szrj }
111*a9fa9459Szrj buffer->push_back(current_byte);
112*a9fa9459Szrj }
113*a9fa9459Szrj while (value != 0);
114*a9fa9459Szrj }
115*a9fa9459Szrj
116*a9fa9459Szrj size_t
get_length_as_unsigned_LEB_128(uint64_t value)117*a9fa9459Szrj get_length_as_unsigned_LEB_128(uint64_t value)
118*a9fa9459Szrj {
119*a9fa9459Szrj size_t length = 0;
120*a9fa9459Szrj do
121*a9fa9459Szrj {
122*a9fa9459Szrj unsigned char current_byte = value & 0x7f;
123*a9fa9459Szrj value >>= 7;
124*a9fa9459Szrj if (value != 0)
125*a9fa9459Szrj {
126*a9fa9459Szrj current_byte |= 0x80;
127*a9fa9459Szrj }
128*a9fa9459Szrj length++;
129*a9fa9459Szrj }
130*a9fa9459Szrj while (value != 0);
131*a9fa9459Szrj return length;
132*a9fa9459Szrj }
133*a9fa9459Szrj
134*a9fa9459Szrj } // End namespace gold.
135