xref: /netbsd-src/external/gpl3/binutils.old/dist/gold/int_encoding.cc (revision e992f068c547fd6e84b3f104dc2340adcc955732)
175fd0b74Schristos // int_encoding.cc -- variable length and unaligned integer encoding support.
275fd0b74Schristos 
3*e992f068Schristos // Copyright (C) 2009-2022 Free Software Foundation, Inc.
475fd0b74Schristos // Written by Doug Kwan <dougkwan@google.com> by refactoring scattered
575fd0b74Schristos // contents from other files in gold.  Original code written by Ian
675fd0b74Schristos // Lance Taylor <iant@google.com> and Caleb Howe <cshowe@google.com>.
775fd0b74Schristos 
875fd0b74Schristos // This file is part of gold.
975fd0b74Schristos 
1075fd0b74Schristos // This program is free software; you can redistribute it and/or modify
1175fd0b74Schristos // it under the terms of the GNU General Public License as published by
1275fd0b74Schristos // the Free Software Foundation; either version 3 of the License, or
1375fd0b74Schristos // (at your option) any later version.
1475fd0b74Schristos 
1575fd0b74Schristos // This program is distributed in the hope that it will be useful,
1675fd0b74Schristos // but WITHOUT ANY WARRANTY; without even the implied warranty of
1775fd0b74Schristos // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1875fd0b74Schristos // GNU General Public License for more details.
1975fd0b74Schristos 
2075fd0b74Schristos // You should have received a copy of the GNU General Public License
2175fd0b74Schristos // along with this program; if not, write to the Free Software
2275fd0b74Schristos // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
2375fd0b74Schristos // MA 02110-1301, USA.
2475fd0b74Schristos 
2575fd0b74Schristos #include "gold.h"
2675fd0b74Schristos 
2775fd0b74Schristos #include <vector>
2875fd0b74Schristos 
2975fd0b74Schristos #include "int_encoding.h"
3075fd0b74Schristos 
3175fd0b74Schristos namespace gold {
3275fd0b74Schristos 
3375fd0b74Schristos // Read an unsigned LEB128 number.  Each byte contains 7 bits of
3475fd0b74Schristos // information, plus one bit saying whether the number continues or
3575fd0b74Schristos // not.  BYTE contains the first byte of the number, and is guaranteed
3675fd0b74Schristos // to have the continuation bit set.
3775fd0b74Schristos 
3875fd0b74Schristos uint64_t
read_unsigned_LEB_128_x(const unsigned char * buffer,size_t * len,unsigned char byte)3975fd0b74Schristos read_unsigned_LEB_128_x(const unsigned char* buffer, size_t* len,
4075fd0b74Schristos 			unsigned char byte)
4175fd0b74Schristos {
4275fd0b74Schristos   uint64_t result = static_cast<uint64_t>(byte & 0x7f);
4375fd0b74Schristos   size_t num_read = 1;
4475fd0b74Schristos   unsigned int shift = 7;
4575fd0b74Schristos 
4675fd0b74Schristos   do
4775fd0b74Schristos     {
4875fd0b74Schristos       if (num_read > 64 / 7 + 1)
4975fd0b74Schristos         {
5075fd0b74Schristos           gold_warning(_("Unusually large LEB128 decoded, "
5175fd0b74Schristos 			 "debug information may be corrupted"));
5275fd0b74Schristos           break;
5375fd0b74Schristos         }
5475fd0b74Schristos       byte = *buffer++;
5575fd0b74Schristos       num_read++;
5675fd0b74Schristos       result |= (static_cast<uint64_t>(byte & 0x7f)) << shift;
5775fd0b74Schristos       shift += 7;
5875fd0b74Schristos     }
5975fd0b74Schristos   while (byte & 0x80);
6075fd0b74Schristos 
6175fd0b74Schristos   *len = num_read;
6275fd0b74Schristos 
6375fd0b74Schristos   return result;
6475fd0b74Schristos }
6575fd0b74Schristos 
6675fd0b74Schristos // Read a signed LEB128 number.  These are like regular LEB128
6775fd0b74Schristos // numbers, except the last byte may have a sign bit set.
6875fd0b74Schristos // BYTE contains the first byte of the number, and is guaranteed
6975fd0b74Schristos // to have the continuation bit set.
7075fd0b74Schristos 
7175fd0b74Schristos int64_t
read_signed_LEB_128_x(const unsigned char * buffer,size_t * len,unsigned char byte)7275fd0b74Schristos read_signed_LEB_128_x(const unsigned char* buffer, size_t* len,
7375fd0b74Schristos 		      unsigned char byte)
7475fd0b74Schristos {
7575fd0b74Schristos   int64_t result = static_cast<uint64_t>(byte & 0x7f);
7675fd0b74Schristos   int shift = 7;
7775fd0b74Schristos   size_t num_read = 1;
7875fd0b74Schristos 
7975fd0b74Schristos   do
8075fd0b74Schristos     {
8175fd0b74Schristos       if (num_read > 64 / 7 + 1)
8275fd0b74Schristos         {
8375fd0b74Schristos           gold_warning(_("Unusually large LEB128 decoded, "
8475fd0b74Schristos 			 "debug information may be corrupted"));
8575fd0b74Schristos           break;
8675fd0b74Schristos         }
8775fd0b74Schristos       byte = *buffer++;
8875fd0b74Schristos       num_read++;
8975fd0b74Schristos       result |= (static_cast<uint64_t>(byte & 0x7f) << shift);
9075fd0b74Schristos       shift += 7;
9175fd0b74Schristos     }
9275fd0b74Schristos   while (byte & 0x80);
9375fd0b74Schristos 
9475fd0b74Schristos   if ((shift < 8 * static_cast<int>(sizeof(result))) && (byte & 0x40))
9575fd0b74Schristos     result |= -((static_cast<int64_t>(1)) << shift);
9675fd0b74Schristos   *len = num_read;
9775fd0b74Schristos   return result;
9875fd0b74Schristos }
9975fd0b74Schristos 
10075fd0b74Schristos void
write_unsigned_LEB_128(std::vector<unsigned char> * buffer,uint64_t value)10175fd0b74Schristos write_unsigned_LEB_128(std::vector<unsigned char>* buffer, uint64_t value)
10275fd0b74Schristos {
10375fd0b74Schristos   do
10475fd0b74Schristos     {
10575fd0b74Schristos       unsigned char current_byte = value & 0x7f;
10675fd0b74Schristos       value >>= 7;
10775fd0b74Schristos       if (value != 0)
10875fd0b74Schristos         {
10975fd0b74Schristos           current_byte |= 0x80;
11075fd0b74Schristos         }
11175fd0b74Schristos       buffer->push_back(current_byte);
11275fd0b74Schristos     }
11375fd0b74Schristos   while (value != 0);
11475fd0b74Schristos }
11575fd0b74Schristos 
11675fd0b74Schristos size_t
get_length_as_unsigned_LEB_128(uint64_t value)11775fd0b74Schristos get_length_as_unsigned_LEB_128(uint64_t value)
11875fd0b74Schristos {
11975fd0b74Schristos   size_t length = 0;
12075fd0b74Schristos   do
12175fd0b74Schristos     {
12275fd0b74Schristos       value >>= 7;
12375fd0b74Schristos       length++;
12475fd0b74Schristos     }
12575fd0b74Schristos   while (value != 0);
12675fd0b74Schristos   return length;
12775fd0b74Schristos }
12875fd0b74Schristos 
12975fd0b74Schristos } // End namespace gold.
130