1 // int_encoding.cc -- variable length and unaligned integer encoding support.
3 // Copyright 2009, 2010 Free Software Foundation, Inc.
4 // Written by Doug Kwan <dougkwan@google.com> by refactoring scattered
5 // contents from other files in gold. Original code written by Ian
6 // Lance Taylor <iant@google.com> and Caleb Howe <cshowe@google.com>.
8 // This file is part of gold.
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 3 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
23 // MA 02110-1301, USA.
29 #include "int_encoding.h"
33 // Read an unsigned LEB128 number. Each byte contains 7 bits of
34 // information, plus one bit saying whether the number continues or
35 // not. BYTE contains the first byte of the number, and is guaranteed
36 // to have the continuation bit set.
39 read_unsigned_LEB_128_x(const unsigned char* buffer
, size_t* len
,
42 uint64_t result
= static_cast<uint64_t>(byte
& 0x7f);
44 unsigned int shift
= 7;
48 if (num_read
> 64 / 7 + 1)
50 gold_warning(_("Unusually large LEB128 decoded, "
51 "debug information may be corrupted"));
56 result
|= (static_cast<uint64_t>(byte
& 0x7f)) << shift
;
66 // Read a signed LEB128 number. These are like regular LEB128
67 // numbers, except the last byte may have a sign bit set.
68 // BYTE contains the first byte of the number, and is guaranteed
69 // to have the continuation bit set.
72 read_signed_LEB_128_x(const unsigned char* buffer
, size_t* len
,
75 int64_t result
= static_cast<uint64_t>(byte
& 0x7f);
81 if (num_read
> 64 / 7 + 1)
83 gold_warning(_("Unusually large LEB128 decoded, "
84 "debug information may be corrupted"));
89 result
|= (static_cast<uint64_t>(byte
& 0x7f) << shift
);
94 if ((shift
< 8 * static_cast<int>(sizeof(result
))) && (byte
& 0x40))
95 result
|= -((static_cast<int64_t>(1)) << shift
);
101 write_unsigned_LEB_128(std::vector
<unsigned char>* buffer
, uint64_t value
)
105 unsigned char current_byte
= value
& 0x7f;
109 current_byte
|= 0x80;
111 buffer
->push_back(current_byte
);
117 get_length_as_unsigned_LEB_128(uint64_t value
)
122 unsigned char current_byte
= value
& 0x7f;
126 current_byte
|= 0x80;
134 } // End namespace gold.