1 // int_encoding.h -- variable length and unaligned integers -*- C++ -*-
3 // Copyright 2009 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.
25 #ifndef GOLD_INT_ENCODING_H
26 #define GOLD_INT_ENCODING_H
31 #include "parameters.h"
37 // LEB 128 encoding support.
40 // Read a ULEB 128 encoded integer from BUFFER. Return the length of the
41 // encoded integer at the location PLEN.
44 read_unsigned_LEB_128(const unsigned char* buffer
, size_t* plen
);
46 // Read an SLEB 128 encoded integer from BUFFER. Return the length of the
47 // encoded integer at the location PLEN.
50 read_signed_LEB_128(const unsigned char* buffer
, size_t* plen
);
52 // Write a ULEB 128 encoded VALUE to BUFFER.
55 write_unsigned_LEB_128(std::vector
<unsigned char>* buffer
, uint64_t value
);
57 // Return the ULEB 128 encoded size of VALUE.
60 get_length_as_unsigned_LEB_128(uint64_t value
);
63 // Unaligned integer encoding support.
66 // Insert VALSIZE-bit integer VALUE into DESTINATION.
68 template <int valsize
>
69 void insert_into_vector(std::vector
<unsigned char>* destination
,
70 typename
elfcpp::Valtype_base
<valsize
>::Valtype value
)
72 unsigned char buffer
[valsize
/ 8];
73 if (parameters
->target().is_big_endian())
74 elfcpp::Swap_unaligned
<valsize
, true>::writeval(buffer
, value
);
76 elfcpp::Swap_unaligned
<valsize
, false>::writeval(buffer
, value
);
77 destination
->insert(destination
->end(), buffer
, buffer
+ valsize
/ 8);
80 // Read a possibly unaligned integer of SIZE. Update SOURCE after read.
82 template <int valsize
>
83 typename
elfcpp::Valtype_base
<valsize
>::Valtype
84 read_from_pointer(unsigned char** source
)
86 typename
elfcpp::Valtype_base
<valsize
>::Valtype return_value
;
87 if (parameters
->target().is_big_endian())
88 return_value
= elfcpp::Swap_unaligned
<valsize
, true>::readval(*source
);
90 return_value
= elfcpp::Swap_unaligned
<valsize
, false>::readval(*source
);
91 *source
+= valsize
/ 8;
95 // Same as the above except for use with const unsigned char data.
97 template <int valsize
>
98 typename
elfcpp::Valtype_base
<valsize
>::Valtype
99 read_from_pointer(const unsigned char** source
)
101 typename
elfcpp::Valtype_base
<valsize
>::Valtype return_value
;
102 if (parameters
->target().is_big_endian())
103 return_value
= elfcpp::Swap_unaligned
<valsize
, true>::readval(*source
);
105 return_value
= elfcpp::Swap_unaligned
<valsize
, false>::readval(*source
);
106 *source
+= valsize
/ 8;
110 } // End namespace gold.
112 #endif // !defined(GOLD_INT_ENCODING_H)