Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // gold.h -- general definitions for gold -*- C++ -*- |
2 | ||
88597d34 | 3 | // Copyright 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. |
6cb15b7f ILT |
4 | // Written by Ian Lance Taylor <iant@google.com>. |
5 | ||
6 | // This file is part of gold. | |
7 | ||
8 | // This program is free software; you can redistribute it and/or modify | |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
11 | // (at your option) any later version. | |
12 | ||
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
22 | ||
bae7f79e | 23 | #ifndef GOLD_GOLD_H |
6724bacc | 24 | #define GOLD_GOLD_H |
bae7f79e ILT |
25 | |
26 | #include "config.h" | |
27 | #include "ansidecl.h" | |
28 | ||
cbb93e63 | 29 | #include <cstddef> |
e6455dfb | 30 | #include <cstdlib> |
e0ebcf42 ILT |
31 | #include <cstring> |
32 | #include <stdint.h> | |
cbb93e63 ILT |
33 | #include <sys/types.h> |
34 | ||
801647d1 ILT |
35 | #ifndef ENABLE_NLS |
36 | // The Solaris version of locale.h always includes libintl.h. If we | |
37 | // have been configured with --disable-nls then ENABLE_NLS will not | |
38 | // be defined and the dummy definitions of bindtextdomain (et al) | |
9b547ce6 | 39 | // below will conflict with the definitions in libintl.h. So we |
801647d1 ILT |
40 | // define these values to prevent the bogus inclusion of libintl.h. |
41 | # define _LIBINTL_H | |
42 | # define _LIBGETTEXT_H | |
43 | #endif | |
44 | ||
45 | // Always include <clocale> first to avoid conflicts with the macros | |
46 | // used when ENABLE_NLS is not defined. | |
47 | #include <clocale> | |
48 | ||
bae7f79e ILT |
49 | #ifdef ENABLE_NLS |
50 | # include <libintl.h> | |
51 | # define _(String) gettext (String) | |
52 | # ifdef gettext_noop | |
53 | # define N_(String) gettext_noop (String) | |
54 | # else | |
55 | # define N_(String) (String) | |
56 | # endif | |
57 | #else | |
58 | # define gettext(Msgid) (Msgid) | |
59 | # define dgettext(Domainname, Msgid) (Msgid) | |
60 | # define dcgettext(Domainname, Msgid, Category) (Msgid) | |
2a49eb69 DK |
61 | # define textdomain(Domainname) do {} while (0) /* nothing */ |
62 | # define bindtextdomain(Domainname, Dirname) do {} while (0) /* nothing */ | |
bae7f79e ILT |
63 | # define _(String) (String) |
64 | # define N_(String) (String) | |
65 | #endif | |
66 | ||
54dc6425 ILT |
67 | // Figure out how to get a hash set and a hash map. |
68 | ||
40fde488 CD |
69 | #if defined(HAVE_TR1_UNORDERED_SET) && defined(HAVE_TR1_UNORDERED_MAP) \ |
70 | && defined(HAVE_TR1_UNORDERED_MAP_REHASH) | |
bae7f79e ILT |
71 | |
72 | #include <tr1/unordered_set> | |
73 | #include <tr1/unordered_map> | |
74 | ||
75 | // We need a template typedef here. | |
76 | ||
77 | #define Unordered_set std::tr1::unordered_set | |
78 | #define Unordered_map std::tr1::unordered_map | |
ef15dade | 79 | #define Unordered_multimap std::tr1::unordered_multimap |
bae7f79e | 80 | |
e55bde5e ILT |
81 | #define reserve_unordered_map(map, n) ((map)->rehash(n)) |
82 | ||
81c82a68 ILT |
83 | #ifndef HAVE_TR1_HASH_OFF_T |
84 | // The library does not support hashes of off_t values. Add support | |
85 | // here. This is likely to be specific to libstdc++. This issue | |
86 | // arises with GCC 4.1.x when compiling in 32-bit mode with a 64-bit | |
87 | // off_t type. | |
88 | namespace std { namespace tr1 { | |
89 | template<> | |
90 | struct hash<off_t> : public std::unary_function<off_t, std::size_t> | |
91 | { | |
92 | std::size_t | |
93 | operator()(off_t val) const | |
94 | { return static_cast<std::size_t>(val); } | |
95 | }; | |
96 | } } // Close namespaces. | |
97 | #endif // !defined(HAVE_TR1_HASH_OFF_T) | |
98 | ||
d288e464 | 99 | #elif defined(HAVE_EXT_HASH_MAP) && defined(HAVE_EXT_HASH_SET) |
54dc6425 ILT |
100 | |
101 | #include <ext/hash_map> | |
102 | #include <ext/hash_set> | |
274e99f9 | 103 | #include <string> |
54dc6425 ILT |
104 | |
105 | #define Unordered_set __gnu_cxx::hash_set | |
106 | #define Unordered_map __gnu_cxx::hash_map | |
ef15dade | 107 | #define Unordered_multimap __gnu_cxx::hash_multimap |
54dc6425 | 108 | |
274e99f9 ILT |
109 | namespace __gnu_cxx |
110 | { | |
111 | ||
112 | template<> | |
113 | struct hash<std::string> | |
114 | { | |
115 | size_t | |
116 | operator()(std::string s) const | |
117 | { return __stl_hash_string(s.c_str()); } | |
118 | }; | |
119 | ||
120 | template<typename T> | |
121 | struct hash<T*> | |
122 | { | |
123 | size_t | |
124 | operator()(T* p) const | |
125 | { return reinterpret_cast<size_t>(p); } | |
126 | }; | |
127 | ||
128 | } | |
129 | ||
e55bde5e ILT |
130 | #define reserve_unordered_map(map, n) ((map)->resize(n)) |
131 | ||
54dc6425 ILT |
132 | #else |
133 | ||
134 | // The fallback is to just use set and map. | |
135 | ||
136 | #include <set> | |
137 | #include <map> | |
138 | ||
139 | #define Unordered_set std::set | |
140 | #define Unordered_map std::map | |
e1e82ea4 | 141 | #define Unordered_multimap std::multimap |
54dc6425 | 142 | |
e55bde5e ILT |
143 | #define reserve_unordered_map(map, n) |
144 | ||
54dc6425 ILT |
145 | #endif |
146 | ||
82dcae9d ILT |
147 | #ifndef HAVE_PREAD |
148 | extern "C" ssize_t pread(int, void*, size_t, off_t); | |
149 | #endif | |
150 | ||
9201d894 ILT |
151 | #ifndef HAVE_FTRUNCATE |
152 | extern "C" int ftruncate(int, off_t); | |
153 | #endif | |
154 | ||
fd03461a ILT |
155 | #ifndef HAVE_FFSLL |
156 | extern "C" int ffsll(long long); | |
2f35ab9b ILT |
157 | #endif |
158 | ||
3d857b98 DK |
159 | #if !HAVE_DECL_MEMMEM |
160 | extern "C" void *memmem(const void *, size_t, const void *, size_t); | |
161 | #endif | |
162 | ||
163 | #if !HAVE_DECL_STRNDUP | |
164 | extern "C" char *strndup(const char *, size_t); | |
165 | #endif | |
166 | ||
5482377d ILT |
167 | namespace gold |
168 | { | |
5482377d | 169 | |
8383303e | 170 | // General declarations. |
bae7f79e | 171 | |
61ba1cf9 | 172 | class General_options; |
5a6f7e2d | 173 | class Command_line; |
92e059d8 | 174 | class Dirsearch; |
61ba1cf9 | 175 | class Input_objects; |
7d9e3d98 | 176 | class Mapfile; |
75f2446e | 177 | class Symbol; |
61ba1cf9 ILT |
178 | class Symbol_table; |
179 | class Layout; | |
17a1d0a9 | 180 | class Task; |
61ba1cf9 ILT |
181 | class Workqueue; |
182 | class Output_file; | |
75f2446e ILT |
183 | template<int size, bool big_endian> |
184 | struct Relocate_info; | |
61ba1cf9 | 185 | |
e6455dfb CC |
186 | // Exit status codes. |
187 | ||
188 | enum Exit_status | |
189 | { | |
190 | GOLD_OK = EXIT_SUCCESS, | |
191 | GOLD_ERR = EXIT_FAILURE, | |
192 | GOLD_FALLBACK = EXIT_FAILURE + 1 | |
193 | }; | |
194 | ||
8383303e ILT |
195 | // Some basic types. For these we use lower case initial letters. |
196 | ||
197 | // For an offset in an input or output file, use off_t. Note that | |
198 | // this will often be a 64-bit type even for a 32-bit build. | |
199 | ||
200 | // The size of a section if we are going to look at the contents. | |
201 | typedef size_t section_size_type; | |
202 | ||
203 | // An offset within a section when we are looking at the contents. | |
204 | typedef ptrdiff_t section_offset_type; | |
205 | ||
bae7f79e ILT |
206 | // The name of the program as used in error messages. |
207 | extern const char* program_name; | |
208 | ||
209 | // This function is called to exit the program. Status is true to | |
210 | // exit success (0) and false to exit failure (1). | |
211 | extern void | |
e6455dfb | 212 | gold_exit(Exit_status status) ATTRIBUTE_NORETURN; |
bae7f79e | 213 | |
75f2446e ILT |
214 | // This function is called to emit an error message and then |
215 | // immediately exit with failure. | |
216 | extern void | |
217 | gold_fatal(const char* format, ...) ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF_1; | |
218 | ||
219 | // This function is called to issue an error. This will cause gold to | |
220 | // eventually exit with failure. | |
221 | extern void | |
222 | gold_error(const char* msg, ...) ATTRIBUTE_PRINTF_1; | |
223 | ||
224 | // This function is called to issue a warning. | |
225 | extern void | |
226 | gold_warning(const char* msg, ...) ATTRIBUTE_PRINTF_1; | |
227 | ||
c5818ff1 CC |
228 | // This function is called to print an informational message. |
229 | extern void | |
230 | gold_info(const char* msg, ...) ATTRIBUTE_PRINTF_1; | |
231 | ||
e6455dfb CC |
232 | // This function is called to emit an error message and then |
233 | // immediately exit with fallback status (e.g., when | |
234 | // --incremental-update fails and the link needs to be restarted | |
235 | // with --incremental-full). | |
236 | extern void | |
237 | gold_fallback(const char* format, ...) ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF_1; | |
238 | ||
04bf7072 ILT |
239 | // Work around a bug in gcc 4.3.0. http://gcc.gnu.org/PR35546 . This |
240 | // can probably be removed after the bug has been fixed for a while. | |
241 | #ifdef HAVE_TEMPLATE_ATTRIBUTES | |
242 | #define TEMPLATE_ATTRIBUTE_PRINTF_4 ATTRIBUTE_PRINTF_4 | |
243 | #else | |
244 | #define TEMPLATE_ATTRIBUTE_PRINTF_4 | |
245 | #endif | |
246 | ||
75f2446e ILT |
247 | // This function is called to issue an error at the location of a |
248 | // reloc. | |
249 | template<int size, bool big_endian> | |
250 | extern void | |
251 | gold_error_at_location(const Relocate_info<size, big_endian>*, | |
252 | size_t, off_t, const char* format, ...) | |
04bf7072 | 253 | TEMPLATE_ATTRIBUTE_PRINTF_4; |
75f2446e ILT |
254 | |
255 | // This function is called to issue a warning at the location of a | |
256 | // reloc. | |
257 | template<int size, bool big_endian> | |
258 | extern void | |
259 | gold_warning_at_location(const Relocate_info<size, big_endian>*, | |
260 | size_t, off_t, const char* format, ...) | |
04bf7072 | 261 | TEMPLATE_ATTRIBUTE_PRINTF_4; |
75f2446e | 262 | |
f073bbf7 CD |
263 | // This function is called to report an undefined symbol without |
264 | // a relocation (e.g., referenced by a dynamic object). SYM is | |
265 | // the undefined symbol. The file name associated with the SYM | |
266 | // is used to print a location for the undefined symbol. | |
267 | extern void | |
268 | gold_undefined_symbol(const Symbol*); | |
269 | ||
270 | // This function is called to report an undefined symbol resulting | |
271 | // from a relocation. SYM is the undefined symbol. RELINFO is the | |
272 | // general relocation info. RELNUM is the number of the reloc, | |
273 | // and RELOFFSET is the reloc's offset. | |
75f2446e | 274 | template<int size, bool big_endian> |
bae7f79e | 275 | extern void |
f073bbf7 CD |
276 | gold_undefined_symbol_at_location(const Symbol*, |
277 | const Relocate_info<size, big_endian>*, | |
278 | size_t, off_t); | |
bae7f79e ILT |
279 | |
280 | // This is function is called in some cases if we run out of memory. | |
281 | extern void | |
282 | gold_nomem() ATTRIBUTE_NORETURN; | |
283 | ||
cc70f101 ILT |
284 | // In versions of gcc before 4.3, using __FUNCTION__ in a template |
285 | // function can cause gcc to get confused about whether or not the | |
286 | // function can return. See http://gcc.gnu.org/PR30988. Use a macro | |
287 | // to avoid the problem. This can be removed when we no longer need | |
288 | // to care about gcc versions before 4.3. | |
289 | #if defined(__GNUC__) && GCC_VERSION < 4003 | |
290 | #define FUNCTION_NAME static_cast<const char*>(__FUNCTION__) | |
291 | #else | |
292 | #define FUNCTION_NAME __FUNCTION__ | |
293 | #endif | |
294 | ||
a3ad94ed ILT |
295 | // This macro and function are used in cases which can not arise if |
296 | // the code is written correctly. | |
297 | ||
298 | #define gold_unreachable() \ | |
cc70f101 | 299 | (gold::do_gold_unreachable(__FILE__, __LINE__, FUNCTION_NAME)) |
a3ad94ed ILT |
300 | |
301 | extern void do_gold_unreachable(const char*, int, const char*) | |
302 | ATTRIBUTE_NORETURN; | |
303 | ||
304 | // Assertion check. | |
305 | ||
306 | #define gold_assert(expr) ((void)(!(expr) ? gold_unreachable(), 0 : 0)) | |
bae7f79e | 307 | |
8486ee48 ILT |
308 | // Print version information. |
309 | extern void | |
310 | print_version(bool print_short); | |
311 | ||
4f211c8b ILT |
312 | // Get the version string. |
313 | extern const char* | |
314 | get_version_string(); | |
315 | ||
8383303e ILT |
316 | // Convert numeric types without unnoticed loss of precision. |
317 | template<typename To, typename From> | |
318 | inline To | |
319 | convert_types(const From from) | |
320 | { | |
321 | To to = from; | |
9bb53bf8 | 322 | gold_assert(static_cast<From>(to) == from); |
8383303e ILT |
323 | return to; |
324 | } | |
325 | ||
326 | // A common case of convert_types<>: convert to section_size_type. | |
327 | template<typename From> | |
328 | inline section_size_type | |
329 | convert_to_section_size_type(const From from) | |
330 | { return convert_types<section_size_type, From>(from); } | |
331 | ||
92e059d8 ILT |
332 | // Queue up the first set of tasks. |
333 | extern void | |
334 | queue_initial_tasks(const General_options&, | |
17a1d0a9 | 335 | Dirsearch&, |
5a6f7e2d | 336 | const Command_line&, |
92e059d8 ILT |
337 | Workqueue*, |
338 | Input_objects*, | |
339 | Symbol_table*, | |
7d9e3d98 ILT |
340 | Layout*, |
341 | Mapfile*); | |
92e059d8 | 342 | |
6d03d481 ST |
343 | // Queue up the set of tasks to be done before |
344 | // the middle set of tasks. Only used when garbage | |
072fe7ce | 345 | // collection is to be done. |
6d03d481 ST |
346 | extern void |
347 | queue_middle_gc_tasks(const General_options&, | |
348 | const Task*, | |
349 | const Input_objects*, | |
350 | Symbol_table*, | |
351 | Layout*, | |
352 | Workqueue*, | |
353 | Mapfile*); | |
354 | ||
92e059d8 ILT |
355 | // Queue up the middle set of tasks. |
356 | extern void | |
357 | queue_middle_tasks(const General_options&, | |
17a1d0a9 | 358 | const Task*, |
92e059d8 ILT |
359 | const Input_objects*, |
360 | Symbol_table*, | |
361 | Layout*, | |
7d9e3d98 ILT |
362 | Workqueue*, |
363 | Mapfile*); | |
92e059d8 ILT |
364 | |
365 | // Queue up the final set of tasks. | |
61ba1cf9 ILT |
366 | extern void |
367 | queue_final_tasks(const General_options&, | |
368 | const Input_objects*, | |
369 | const Symbol_table*, | |
27bc2bce | 370 | Layout*, |
61ba1cf9 ILT |
371 | Workqueue*, |
372 | Output_file* of); | |
373 | ||
6d03d481 ST |
374 | inline bool |
375 | is_prefix_of(const char* prefix, const char* str) | |
376 | { | |
377 | return strncmp(prefix, str, strlen(prefix)) == 0; | |
378 | } | |
379 | ||
f1ec9ded ST |
380 | const char* const cident_section_start_prefix = "__start_"; |
381 | const char* const cident_section_stop_prefix = "__stop_"; | |
382 | ||
383 | // Returns true if the name is a valid C identifier | |
384 | inline bool | |
385 | is_cident(const char* name) | |
386 | { | |
387 | return (name[strspn(name, | |
388 | ("0123456789" | |
389 | "ABCDEFGHIJKLMNOPWRSTUVWXYZ" | |
390 | "abcdefghijklmnopqrstuvwxyz" | |
391 | "_"))] | |
392 | == '\0'); | |
393 | } | |
394 | ||
b569affa DK |
395 | // We sometimes need to hash strings. Ideally we should use std::tr1::hash or |
396 | // __gnu_cxx::hash on some systems but there is no guarantee that either | |
397 | // one is available. For portability, we define simple string hash functions. | |
398 | ||
399 | template<typename Char_type> | |
400 | inline size_t | |
401 | string_hash(const Char_type* s, size_t length) | |
402 | { | |
403 | // This is the hash function used by the dynamic linker for | |
404 | // DT_GNU_HASH entries. I compared this to a Fowler/Noll/Vo hash | |
405 | // for a C++ program with 385,775 global symbols. This hash | |
406 | // function was very slightly worse. However, it is much faster to | |
407 | // compute. Overall wall clock time was a win. | |
408 | const unsigned char* p = reinterpret_cast<const unsigned char*>(s); | |
409 | size_t h = 5381; | |
410 | for (size_t i = 0; i < length * sizeof(Char_type); ++i) | |
411 | h = h * 33 + *p++; | |
412 | return h; | |
413 | } | |
414 | ||
415 | // Same as above except we expect the string to be zero terminated. | |
416 | ||
417 | template<typename Char_type> | |
418 | inline size_t | |
419 | string_hash(const Char_type* s) | |
420 | { | |
421 | const unsigned char* p = reinterpret_cast<const unsigned char*>(s); | |
422 | size_t h = 5381; | |
423 | for (size_t i = 0; s[i] != 0; ++i) | |
424 | { | |
425 | for (size_t j = 0; j < sizeof(Char_type); j++) | |
426 | h = h * 33 + *p++; | |
427 | } | |
428 | ||
429 | return h; | |
430 | } | |
431 | ||
6e9ba2ca ST |
432 | // Return whether STRING contains a wildcard character. This is used |
433 | // to speed up matching. | |
434 | ||
435 | inline bool | |
436 | is_wildcard_string(const char* s) | |
437 | { | |
438 | return strpbrk(s, "?*[") != NULL; | |
439 | } | |
440 | ||
bae7f79e ILT |
441 | } // End namespace gold. |
442 | ||
443 | #endif // !defined(GOLD_GOLD_H) |