int_encoding.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // int_encoding.h -- variable length and unaligned integers -*- C++ -*-
  2. // Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. // Written by Doug Kwan <dougkwan@google.com> by refactoring scattered
  4. // contents from other files in gold. Original code written by Ian
  5. // Lance Taylor <iant@google.com> and Caleb Howe <cshowe@google.com>.
  6. // This file is part of gold.
  7. // This program is free software; you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public License as published by
  9. // the Free Software Foundation; either version 3 of the License, or
  10. // (at your option) any later version.
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  18. // MA 02110-1301, USA.
  19. #ifndef GOLD_INT_ENCODING_H
  20. #define GOLD_INT_ENCODING_H
  21. #include <vector>
  22. #include "elfcpp.h"
  23. #include "target.h"
  24. #include "parameters.h"
  25. namespace gold
  26. {
  27. //
  28. // LEB 128 encoding support.
  29. //
  30. // Read a ULEB 128 encoded integer from BUFFER. Return the length of the
  31. // encoded integer at the location PLEN. The common case of a single-byte
  32. // value is handled inline, and multi-byte values are processed by the _x
  33. // routine, where BYTE is the first byte of the value.
  34. uint64_t
  35. read_unsigned_LEB_128_x(const unsigned char* buffer, size_t* plen,
  36. unsigned char byte);
  37. inline uint64_t
  38. read_unsigned_LEB_128(const unsigned char* buffer, size_t* plen)
  39. {
  40. unsigned char byte = *buffer++;
  41. if ((byte & 0x80) != 0)
  42. return read_unsigned_LEB_128_x(buffer, plen, byte);
  43. *plen = 1;
  44. return static_cast<uint64_t>(byte);
  45. }
  46. // Read an SLEB 128 encoded integer from BUFFER. Return the length of the
  47. // encoded integer at the location PLEN. The common case of a single-byte
  48. // value is handled inline, and multi-byte values are processed by the _x
  49. // routine, where BYTE is the first byte of the value.
  50. int64_t
  51. read_signed_LEB_128_x(const unsigned char* buffer, size_t* plen,
  52. unsigned char byte);
  53. inline int64_t
  54. read_signed_LEB_128(const unsigned char* buffer, size_t* plen)
  55. {
  56. unsigned char byte = *buffer++;
  57. if ((byte & 0x80) != 0)
  58. return read_signed_LEB_128_x(buffer, plen, byte);
  59. *plen = 1;
  60. if (byte & 0x40)
  61. return -(static_cast<int64_t>(1) << 7) | static_cast<int64_t>(byte);
  62. return static_cast<int64_t>(byte);
  63. }
  64. // Write a ULEB 128 encoded VALUE to BUFFER.
  65. void
  66. write_unsigned_LEB_128(std::vector<unsigned char>* buffer, uint64_t value);
  67. // Return the ULEB 128 encoded size of VALUE.
  68. size_t
  69. get_length_as_unsigned_LEB_128(uint64_t value);
  70. //
  71. // Unaligned integer encoding support.
  72. //
  73. // Insert VALSIZE-bit integer VALUE into DESTINATION.
  74. template <int valsize>
  75. void insert_into_vector(std::vector<unsigned char>* destination,
  76. typename elfcpp::Valtype_base<valsize>::Valtype value)
  77. {
  78. unsigned char buffer[valsize / 8];
  79. if (parameters->target().is_big_endian())
  80. elfcpp::Swap_unaligned<valsize, true>::writeval(buffer, value);
  81. else
  82. elfcpp::Swap_unaligned<valsize, false>::writeval(buffer, value);
  83. destination->insert(destination->end(), buffer, buffer + valsize / 8);
  84. }
  85. // Read a possibly unaligned integer of SIZE from SOURCE.
  86. template <int valsize>
  87. typename elfcpp::Valtype_base<valsize>::Valtype
  88. read_from_pointer(const unsigned char* source)
  89. {
  90. typename elfcpp::Valtype_base<valsize>::Valtype return_value;
  91. if (parameters->target().is_big_endian())
  92. return_value = elfcpp::Swap_unaligned<valsize, true>::readval(source);
  93. else
  94. return_value = elfcpp::Swap_unaligned<valsize, false>::readval(source);
  95. return return_value;
  96. }
  97. // Read a possibly unaligned integer of SIZE. Update SOURCE after read.
  98. template <int valsize>
  99. typename elfcpp::Valtype_base<valsize>::Valtype
  100. read_from_pointer(unsigned char** source)
  101. {
  102. typename elfcpp::Valtype_base<valsize>::Valtype return_value;
  103. if (parameters->target().is_big_endian())
  104. return_value = elfcpp::Swap_unaligned<valsize, true>::readval(*source);
  105. else
  106. return_value = elfcpp::Swap_unaligned<valsize, false>::readval(*source);
  107. *source += valsize / 8;
  108. return return_value;
  109. }
  110. // Same as the above except for use with const unsigned char data.
  111. template <int valsize>
  112. typename elfcpp::Valtype_base<valsize>::Valtype
  113. read_from_pointer(const unsigned char** source)
  114. {
  115. typename elfcpp::Valtype_base<valsize>::Valtype return_value;
  116. if (parameters->target().is_big_endian())
  117. return_value = elfcpp::Swap_unaligned<valsize, true>::readval(*source);
  118. else
  119. return_value = elfcpp::Swap_unaligned<valsize, false>::readval(*source);
  120. *source += valsize / 8;
  121. return return_value;
  122. }
  123. } // End namespace gold.
  124. #endif // !defined(GOLD_INT_ENCODING_H)