gold.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // gold.h -- general definitions for gold -*- C++ -*-
  2. // Copyright (C) 2006-2015 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 emit an error message and then
  78. // immediately exit with fallback status (e.g., when
  79. // --incremental-update fails and the link needs to be restarted
  80. // with --incremental-full).
  81. extern void
  82. gold_fallback(const char* format, ...) ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF_1;
  83. // Work around a bug in gcc 4.3.0. http://gcc.gnu.org/PR35546 . This
  84. // can probably be removed after the bug has been fixed for a while.
  85. #ifdef HAVE_TEMPLATE_ATTRIBUTES
  86. #define TEMPLATE_ATTRIBUTE_PRINTF_4 ATTRIBUTE_PRINTF_4
  87. #else
  88. #define TEMPLATE_ATTRIBUTE_PRINTF_4
  89. #endif
  90. // This function is called to issue an error at the location of a
  91. // reloc.
  92. template<int size, bool big_endian>
  93. extern void
  94. gold_error_at_location(const Relocate_info<size, big_endian>*,
  95. size_t, off_t, const char* format, ...)
  96. TEMPLATE_ATTRIBUTE_PRINTF_4;
  97. // This function is called to issue a warning at the location of a
  98. // reloc.
  99. template<int size, bool big_endian>
  100. extern void
  101. gold_warning_at_location(const Relocate_info<size, big_endian>*,
  102. size_t, off_t, const char* format, ...)
  103. TEMPLATE_ATTRIBUTE_PRINTF_4;
  104. // This function is called to report an undefined symbol without
  105. // a relocation (e.g., referenced by a dynamic object). SYM is
  106. // the undefined symbol. The file name associated with the SYM
  107. // is used to print a location for the undefined symbol.
  108. extern void
  109. gold_undefined_symbol(const Symbol*);
  110. // This function is called to report an undefined symbol resulting
  111. // from a relocation. SYM is the undefined symbol. RELINFO is the
  112. // general relocation info. RELNUM is the number of the reloc,
  113. // and RELOFFSET is the reloc's offset.
  114. template<int size, bool big_endian>
  115. extern void
  116. gold_undefined_symbol_at_location(const Symbol*,
  117. const Relocate_info<size, big_endian>*,
  118. size_t, off_t);
  119. // This is function is called in some cases if we run out of memory.
  120. extern void
  121. gold_nomem() ATTRIBUTE_NORETURN;
  122. // In versions of gcc before 4.3, using __FUNCTION__ in a template
  123. // function can cause gcc to get confused about whether or not the
  124. // function can return. See http://gcc.gnu.org/PR30988. Use a macro
  125. // to avoid the problem. This can be removed when we no longer need
  126. // to care about gcc versions before 4.3.
  127. #if defined(__GNUC__) && GCC_VERSION < 4003
  128. #define FUNCTION_NAME static_cast<const char*>(__FUNCTION__)
  129. #else
  130. #define FUNCTION_NAME __FUNCTION__
  131. #endif
  132. // This macro and function are used in cases which can not arise if
  133. // the code is written correctly.
  134. #define gold_unreachable() \
  135. (gold::do_gold_unreachable(__FILE__, __LINE__, FUNCTION_NAME))
  136. extern void do_gold_unreachable(const char*, int, const char*)
  137. ATTRIBUTE_NORETURN;
  138. // Assertion check.
  139. #define gold_assert(expr) ((void)(!(expr) ? gold_unreachable(), 0 : 0))
  140. // Print version information.
  141. extern void
  142. print_version(bool print_short);
  143. // Get the version string.
  144. extern const char*
  145. get_version_string();
  146. // Convert numeric types without unnoticed loss of precision.
  147. template<typename To, typename From>
  148. inline To
  149. convert_types(const From from)
  150. {
  151. To to = from;
  152. gold_assert(static_cast<From>(to) == from);
  153. return to;
  154. }
  155. // A common case of convert_types<>: convert to section_size_type.
  156. template<typename From>
  157. inline section_size_type
  158. convert_to_section_size_type(const From from)
  159. { return convert_types<section_size_type, From>(from); }
  160. // Queue up the first set of tasks.
  161. extern void
  162. queue_initial_tasks(const General_options&,
  163. Dirsearch&,
  164. const Command_line&,
  165. Workqueue*,
  166. Input_objects*,
  167. Symbol_table*,
  168. Layout*,
  169. Mapfile*);
  170. // Queue up the set of tasks to be done before
  171. // the middle set of tasks. Only used when garbage
  172. // collection is to be done.
  173. extern void
  174. queue_middle_gc_tasks(const General_options&,
  175. const Task*,
  176. const Input_objects*,
  177. Symbol_table*,
  178. Layout*,
  179. Workqueue*,
  180. Mapfile*);
  181. // Queue up the middle set of tasks.
  182. extern void
  183. queue_middle_tasks(const General_options&,
  184. const Task*,
  185. const Input_objects*,
  186. Symbol_table*,
  187. Layout*,
  188. Workqueue*,
  189. Mapfile*);
  190. // Queue up the final set of tasks.
  191. extern void
  192. queue_final_tasks(const General_options&,
  193. const Input_objects*,
  194. const Symbol_table*,
  195. Layout*,
  196. Workqueue*,
  197. Output_file* of);
  198. inline bool
  199. is_prefix_of(const char* prefix, const char* str)
  200. {
  201. return strncmp(prefix, str, strlen(prefix)) == 0;
  202. }
  203. const char* const cident_section_start_prefix = "__start_";
  204. const char* const cident_section_stop_prefix = "__stop_";
  205. // Returns true if the name is a valid C identifier
  206. inline bool
  207. is_cident(const char* name)
  208. {
  209. return (name[strspn(name,
  210. ("0123456789"
  211. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  212. "abcdefghijklmnopqrstuvwxyz"
  213. "_"))]
  214. == '\0');
  215. }
  216. // We sometimes need to hash strings. Ideally we should use std::tr1::hash or
  217. // __gnu_cxx::hash on some systems but there is no guarantee that either
  218. // one is available. For portability, we define simple string hash functions.
  219. template<typename Char_type>
  220. inline size_t
  221. string_hash(const Char_type* s, size_t length)
  222. {
  223. // This is the hash function used by the dynamic linker for
  224. // DT_GNU_HASH entries. I compared this to a Fowler/Noll/Vo hash
  225. // for a C++ program with 385,775 global symbols. This hash
  226. // function was very slightly worse. However, it is much faster to
  227. // compute. Overall wall clock time was a win.
  228. const unsigned char* p = reinterpret_cast<const unsigned char*>(s);
  229. size_t h = 5381;
  230. for (size_t i = 0; i < length * sizeof(Char_type); ++i)
  231. h = h * 33 + *p++;
  232. return h;
  233. }
  234. // Same as above except we expect the string to be zero terminated.
  235. template<typename Char_type>
  236. inline size_t
  237. string_hash(const Char_type* s)
  238. {
  239. const unsigned char* p = reinterpret_cast<const unsigned char*>(s);
  240. size_t h = 5381;
  241. for (size_t i = 0; s[i] != 0; ++i)
  242. {
  243. for (size_t j = 0; j < sizeof(Char_type); j++)
  244. h = h * 33 + *p++;
  245. }
  246. return h;
  247. }
  248. // Return whether STRING contains a wildcard character. This is used
  249. // to speed up matching.
  250. inline bool
  251. is_wildcard_string(const char* s)
  252. {
  253. return strpbrk(s, "?*[") != NULL;
  254. }
  255. } // End namespace gold.
  256. #endif // !defined(GOLD_GOLD_H)