gold.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // gold.h -- general definitions for gold -*- C++ -*-
  2. // Copyright (C) 2006-2022 Free Software Foundation, Inc.
  3. // Written by Ian Lance Taylor <iant@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. #ifndef GOLD_GOLD_H
  18. #define GOLD_GOLD_H
  19. #include "config.h"
  20. #include "ansidecl.h"
  21. #include <cstddef>
  22. #include <cstdlib>
  23. #include <cstring>
  24. #include <stdint.h>
  25. #include <sys/types.h>
  26. #include "system.h"
  27. namespace gold
  28. {
  29. // General declarations.
  30. class General_options;
  31. class Command_line;
  32. class Dirsearch;
  33. class Input_objects;
  34. class Mapfile;
  35. class Symbol;
  36. class Symbol_table;
  37. class Layout;
  38. class Task;
  39. class Workqueue;
  40. class Output_file;
  41. template<int size, bool big_endian>
  42. struct Relocate_info;
  43. // Exit status codes.
  44. enum Exit_status
  45. {
  46. GOLD_OK = EXIT_SUCCESS,
  47. GOLD_ERR = EXIT_FAILURE,
  48. GOLD_FALLBACK = EXIT_FAILURE + 1
  49. };
  50. // Some basic types. For these we use lower case initial letters.
  51. // For an offset in an input or output file, use off_t. Note that
  52. // this will often be a 64-bit type even for a 32-bit build.
  53. // The size of a section if we are going to look at the contents.
  54. typedef size_t section_size_type;
  55. // An offset within a section when we are looking at the contents.
  56. typedef ptrdiff_t section_offset_type;
  57. // The name of the program as used in error messages.
  58. extern const char* program_name;
  59. // This function is called to exit the program. Status is true to
  60. // exit success (0) and false to exit failure (1).
  61. extern void
  62. gold_exit(Exit_status status) ATTRIBUTE_NORETURN;
  63. // This function is called to emit an error message and then
  64. // immediately exit with failure.
  65. extern void
  66. gold_fatal(const char* format, ...) ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF_1;
  67. // This function is called to issue an error. This will cause gold to
  68. // eventually exit with failure.
  69. extern void
  70. gold_error(const char* msg, ...) ATTRIBUTE_PRINTF_1;
  71. // This function is called to issue a warning.
  72. extern void
  73. gold_warning(const char* msg, ...) ATTRIBUTE_PRINTF_1;
  74. // This function is called to print an informational message.
  75. extern void
  76. gold_info(const char* msg, ...) ATTRIBUTE_PRINTF_1;
  77. // This function is called to print a trace message.
  78. extern void
  79. gold_trace(const char* msg, ...) ATTRIBUTE_PRINTF_1;
  80. // This function is called to emit an error message and then
  81. // immediately exit with fallback status (e.g., when
  82. // --incremental-update fails and the link needs to be restarted
  83. // with --incremental-full).
  84. extern void
  85. gold_fallback(const char* format, ...) ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF_1;
  86. // Work around a bug in gcc 4.3.0. http://gcc.gnu.org/PR35546 . This
  87. // can probably be removed after the bug has been fixed for a while.
  88. #ifdef HAVE_TEMPLATE_ATTRIBUTES
  89. #define TEMPLATE_ATTRIBUTE_PRINTF_4 ATTRIBUTE_PRINTF_4
  90. #else
  91. #define TEMPLATE_ATTRIBUTE_PRINTF_4
  92. #endif
  93. // This function is called to issue an error at the location of a
  94. // reloc.
  95. template<int size, bool big_endian>
  96. extern void
  97. gold_error_at_location(const Relocate_info<size, big_endian>*,
  98. size_t, off_t, const char* format, ...)
  99. TEMPLATE_ATTRIBUTE_PRINTF_4;
  100. // This function is called to issue a warning at the location of a
  101. // reloc.
  102. template<int size, bool big_endian>
  103. extern void
  104. gold_warning_at_location(const Relocate_info<size, big_endian>*,
  105. size_t, off_t, const char* format, ...)
  106. TEMPLATE_ATTRIBUTE_PRINTF_4;
  107. // This function is called to report an undefined symbol without
  108. // a relocation (e.g., referenced by a dynamic object). SYM is
  109. // the undefined symbol. The file name associated with the SYM
  110. // is used to print a location for the undefined symbol.
  111. extern void
  112. gold_undefined_symbol(const Symbol*);
  113. // This function is called to report an undefined symbol resulting
  114. // from a relocation. SYM is the undefined symbol. RELINFO is the
  115. // general relocation info. RELNUM is the number of the reloc,
  116. // and RELOFFSET is the reloc's offset.
  117. template<int size, bool big_endian>
  118. extern void
  119. gold_undefined_symbol_at_location(const Symbol*,
  120. const Relocate_info<size, big_endian>*,
  121. size_t, off_t);
  122. // This is function is called in some cases if we run out of memory.
  123. extern void
  124. gold_nomem() ATTRIBUTE_NORETURN;
  125. // In versions of gcc before 4.3, using __FUNCTION__ in a template
  126. // function can cause gcc to get confused about whether or not the
  127. // function can return. See http://gcc.gnu.org/PR30988. Use a macro
  128. // to avoid the problem. This can be removed when we no longer need
  129. // to care about gcc versions before 4.3.
  130. #if defined(__GNUC__) && GCC_VERSION < 4003
  131. #define FUNCTION_NAME static_cast<const char*>(__FUNCTION__)
  132. #else
  133. #define FUNCTION_NAME __FUNCTION__
  134. #endif
  135. // This macro and function are used in cases which can not arise if
  136. // the code is written correctly.
  137. #define gold_unreachable() \
  138. (gold::do_gold_unreachable(__FILE__, __LINE__, FUNCTION_NAME))
  139. extern void do_gold_unreachable(const char*, int, const char*)
  140. ATTRIBUTE_NORETURN;
  141. // Assertion check.
  142. #define gold_assert(expr) ((void)(!(expr) ? gold_unreachable(), 0 : 0))
  143. // Print version information.
  144. extern void
  145. print_version(bool print_short);
  146. // Get the version string.
  147. extern const char*
  148. get_version_string();
  149. // Convert numeric types without unnoticed loss of precision.
  150. template<typename To, typename From>
  151. inline To
  152. convert_types(const From from)
  153. {
  154. To to = from;
  155. gold_assert(static_cast<From>(to) == from);
  156. return to;
  157. }
  158. // A common case of convert_types<>: convert to section_size_type.
  159. template<typename From>
  160. inline section_size_type
  161. convert_to_section_size_type(const From from)
  162. { return convert_types<section_size_type, From>(from); }
  163. // Queue up the first set of tasks.
  164. extern void
  165. queue_initial_tasks(const General_options&,
  166. Dirsearch&,
  167. const Command_line&,
  168. Workqueue*,
  169. Input_objects*,
  170. Symbol_table*,
  171. Layout*,
  172. Mapfile*);
  173. // Queue up the set of tasks to be done before
  174. // the middle set of tasks. Only used when garbage
  175. // collection is to be done.
  176. extern void
  177. queue_middle_gc_tasks(const General_options&,
  178. const Task*,
  179. const Input_objects*,
  180. Symbol_table*,
  181. Layout*,
  182. Workqueue*,
  183. Mapfile*);
  184. // Queue up the middle set of tasks.
  185. extern void
  186. queue_middle_tasks(const General_options&,
  187. const Task*,
  188. const Input_objects*,
  189. Symbol_table*,
  190. Layout*,
  191. Workqueue*,
  192. Mapfile*);
  193. // Queue up the final set of tasks.
  194. extern void
  195. queue_final_tasks(const General_options&,
  196. const Input_objects*,
  197. const Symbol_table*,
  198. Layout*,
  199. Workqueue*,
  200. Output_file* of);
  201. inline bool
  202. is_prefix_of(const char* prefix, const char* str)
  203. {
  204. return strncmp(prefix, str, strlen(prefix)) == 0;
  205. }
  206. const char* const cident_section_start_prefix = "__start_";
  207. const char* const cident_section_stop_prefix = "__stop_";
  208. // Returns true if the name is a valid C identifier
  209. inline bool
  210. is_cident(const char* name)
  211. {
  212. return (name[strspn(name,
  213. ("0123456789"
  214. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  215. "abcdefghijklmnopqrstuvwxyz"
  216. "_"))]
  217. == '\0');
  218. }
  219. // We sometimes need to hash strings. Ideally we should use std::tr1::hash or
  220. // __gnu_cxx::hash on some systems but there is no guarantee that either
  221. // one is available. For portability, we define simple string hash functions.
  222. template<typename Char_type>
  223. inline size_t
  224. string_hash(const Char_type* s, size_t length)
  225. {
  226. // This is the hash function used by the dynamic linker for
  227. // DT_GNU_HASH entries. I compared this to a Fowler/Noll/Vo hash
  228. // for a C++ program with 385,775 global symbols. This hash
  229. // function was very slightly worse. However, it is much faster to
  230. // compute. Overall wall clock time was a win.
  231. const unsigned char* p = reinterpret_cast<const unsigned char*>(s);
  232. size_t h = 5381;
  233. for (size_t i = 0; i < length * sizeof(Char_type); ++i)
  234. h = h * 33 + *p++;
  235. return h;
  236. }
  237. // Same as above except we expect the string to be zero terminated.
  238. template<typename Char_type>
  239. inline size_t
  240. string_hash(const Char_type* s)
  241. {
  242. const unsigned char* p = reinterpret_cast<const unsigned char*>(s);
  243. size_t h = 5381;
  244. for (size_t i = 0; s[i] != 0; ++i)
  245. {
  246. for (size_t j = 0; j < sizeof(Char_type); j++)
  247. h = h * 33 + *p++;
  248. }
  249. return h;
  250. }
  251. // Return whether STRING contains a wildcard character. This is used
  252. // to speed up matching.
  253. inline bool
  254. is_wildcard_string(const char* s)
  255. {
  256. return strpbrk(s, "?*[") != NULL;
  257. }
  258. } // End namespace gold.
  259. #endif // !defined(GOLD_GOLD_H)