]>
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 <[email protected]>. |
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) | |
61 | # define textdomain(Domainname) while (0) /* nothing */ | |
62 | # define bindtextdomain(Domainname, Dirname) while (0) /* nothing */ | |
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 | ||
d288e464 | 83 | #elif defined(HAVE_EXT_HASH_MAP) && defined(HAVE_EXT_HASH_SET) |
54dc6425 ILT |
84 | |
85 | #include <ext/hash_map> | |
86 | #include <ext/hash_set> | |
274e99f9 | 87 | #include <string> |
54dc6425 ILT |
88 | |
89 | #define Unordered_set __gnu_cxx::hash_set | |
90 | #define Unordered_map __gnu_cxx::hash_map | |
ef15dade | 91 | #define Unordered_multimap __gnu_cxx::hash_multimap |
54dc6425 | 92 | |
274e99f9 ILT |
93 | namespace __gnu_cxx |
94 | { | |
95 | ||
96 | template<> | |
97 | struct hash<std::string> | |
98 | { | |
99 | size_t | |
100 | operator()(std::string s) const | |
101 | { return __stl_hash_string(s.c_str()); } | |
102 | }; | |
103 | ||
104 | template<typename T> | |
105 | struct hash<T*> | |
106 | { | |
107 | size_t | |
108 | operator()(T* p) const | |
109 | { return reinterpret_cast<size_t>(p); } | |
110 | }; | |
111 | ||
112 | } | |
113 | ||
e55bde5e ILT |
114 | #define reserve_unordered_map(map, n) ((map)->resize(n)) |
115 | ||
54dc6425 ILT |
116 | #else |
117 | ||
118 | // The fallback is to just use set and map. | |
119 | ||
120 | #include <set> | |
121 | #include <map> | |
122 | ||
123 | #define Unordered_set std::set | |
124 | #define Unordered_map std::map | |
e1e82ea4 | 125 | #define Unordered_multimap std::multimap |
54dc6425 | 126 | |
e55bde5e ILT |
127 | #define reserve_unordered_map(map, n) |
128 | ||
54dc6425 ILT |
129 | #endif |
130 | ||
82dcae9d ILT |
131 | #ifndef HAVE_PREAD |
132 | extern "C" ssize_t pread(int, void*, size_t, off_t); | |
133 | #endif | |
134 | ||
9201d894 ILT |
135 | #ifndef HAVE_FTRUNCATE |
136 | extern "C" int ftruncate(int, off_t); | |
137 | #endif | |
138 | ||
fd03461a ILT |
139 | #ifndef HAVE_FFSLL |
140 | extern "C" int ffsll(long long); | |
2f35ab9b ILT |
141 | #endif |
142 | ||
3d857b98 DK |
143 | #if !HAVE_DECL_MEMMEM |
144 | extern "C" void *memmem(const void *, size_t, const void *, size_t); | |
145 | #endif | |
146 | ||
147 | #if !HAVE_DECL_STRNDUP | |
148 | extern "C" char *strndup(const char *, size_t); | |
149 | #endif | |
150 | ||
5482377d ILT |
151 | namespace gold |
152 | { | |
5482377d | 153 | |
8383303e | 154 | // General declarations. |
bae7f79e | 155 | |
61ba1cf9 | 156 | class General_options; |
5a6f7e2d | 157 | class Command_line; |
92e059d8 | 158 | class Dirsearch; |
61ba1cf9 | 159 | class Input_objects; |
7d9e3d98 | 160 | class Mapfile; |
75f2446e | 161 | class Symbol; |
61ba1cf9 ILT |
162 | class Symbol_table; |
163 | class Layout; | |
17a1d0a9 | 164 | class Task; |
61ba1cf9 ILT |
165 | class Workqueue; |
166 | class Output_file; | |
75f2446e ILT |
167 | template<int size, bool big_endian> |
168 | struct Relocate_info; | |
61ba1cf9 | 169 | |
e6455dfb CC |
170 | // Exit status codes. |
171 | ||
172 | enum Exit_status | |
173 | { | |
174 | GOLD_OK = EXIT_SUCCESS, | |
175 | GOLD_ERR = EXIT_FAILURE, | |
176 | GOLD_FALLBACK = EXIT_FAILURE + 1 | |
177 | }; | |
178 | ||
8383303e ILT |
179 | // Some basic types. For these we use lower case initial letters. |
180 | ||
181 | // For an offset in an input or output file, use off_t. Note that | |
182 | // this will often be a 64-bit type even for a 32-bit build. | |
183 | ||
184 | // The size of a section if we are going to look at the contents. | |
185 | typedef size_t section_size_type; | |
186 | ||
187 | // An offset within a section when we are looking at the contents. | |
188 | typedef ptrdiff_t section_offset_type; | |
189 | ||
bae7f79e ILT |
190 | // The name of the program as used in error messages. |
191 | extern const char* program_name; | |
192 | ||
193 | // This function is called to exit the program. Status is true to | |
194 | // exit success (0) and false to exit failure (1). | |
195 | extern void | |
e6455dfb | 196 | gold_exit(Exit_status status) ATTRIBUTE_NORETURN; |
bae7f79e | 197 | |
75f2446e ILT |
198 | // This function is called to emit an error message and then |
199 | // immediately exit with failure. | |
200 | extern void | |
201 | gold_fatal(const char* format, ...) ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF_1; | |
202 | ||
203 | // This function is called to issue an error. This will cause gold to | |
204 | // eventually exit with failure. | |
205 | extern void | |
206 | gold_error(const char* msg, ...) ATTRIBUTE_PRINTF_1; | |
207 | ||
208 | // This function is called to issue a warning. | |
209 | extern void | |
210 | gold_warning(const char* msg, ...) ATTRIBUTE_PRINTF_1; | |
211 | ||
c5818ff1 CC |
212 | // This function is called to print an informational message. |
213 | extern void | |
214 | gold_info(const char* msg, ...) ATTRIBUTE_PRINTF_1; | |
215 | ||
e6455dfb CC |
216 | // This function is called to emit an error message and then |
217 | // immediately exit with fallback status (e.g., when | |
218 | // --incremental-update fails and the link needs to be restarted | |
219 | // with --incremental-full). | |
220 | extern void | |
221 | gold_fallback(const char* format, ...) ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF_1; | |
222 | ||
04bf7072 ILT |
223 | // Work around a bug in gcc 4.3.0. http://gcc.gnu.org/PR35546 . This |
224 | // can probably be removed after the bug has been fixed for a while. | |
225 | #ifdef HAVE_TEMPLATE_ATTRIBUTES | |
226 | #define TEMPLATE_ATTRIBUTE_PRINTF_4 ATTRIBUTE_PRINTF_4 | |
227 | #else | |
228 | #define TEMPLATE_ATTRIBUTE_PRINTF_4 | |
229 | #endif | |
230 | ||
75f2446e ILT |
231 | // This function is called to issue an error at the location of a |
232 | // reloc. | |
233 | template<int size, bool big_endian> | |
234 | extern void | |
235 | gold_error_at_location(const Relocate_info<size, big_endian>*, | |
236 | size_t, off_t, const char* format, ...) | |
04bf7072 | 237 | TEMPLATE_ATTRIBUTE_PRINTF_4; |
75f2446e ILT |
238 | |
239 | // This function is called to issue a warning at the location of a | |
240 | // reloc. | |
241 | template<int size, bool big_endian> | |
242 | extern void | |
243 | gold_warning_at_location(const Relocate_info<size, big_endian>*, | |
244 | size_t, off_t, const char* format, ...) | |
04bf7072 | 245 | TEMPLATE_ATTRIBUTE_PRINTF_4; |
75f2446e | 246 | |
f073bbf7 CD |
247 | // This function is called to report an undefined symbol without |
248 | // a relocation (e.g., referenced by a dynamic object). SYM is | |
249 | // the undefined symbol. The file name associated with the SYM | |
250 | // is used to print a location for the undefined symbol. | |
251 | extern void | |
252 | gold_undefined_symbol(const Symbol*); | |
253 | ||
254 | // This function is called to report an undefined symbol resulting | |
255 | // from a relocation. SYM is the undefined symbol. RELINFO is the | |
256 | // general relocation info. RELNUM is the number of the reloc, | |
257 | // and RELOFFSET is the reloc's offset. | |
75f2446e | 258 | template<int size, bool big_endian> |
bae7f79e | 259 | extern void |
f073bbf7 CD |
260 | gold_undefined_symbol_at_location(const Symbol*, |
261 | const Relocate_info<size, big_endian>*, | |
262 | size_t, off_t); | |
bae7f79e ILT |
263 | |
264 | // This is function is called in some cases if we run out of memory. | |
265 | extern void | |
266 | gold_nomem() ATTRIBUTE_NORETURN; | |
267 | ||
cc70f101 ILT |
268 | // In versions of gcc before 4.3, using __FUNCTION__ in a template |
269 | // function can cause gcc to get confused about whether or not the | |
270 | // function can return. See http://gcc.gnu.org/PR30988. Use a macro | |
271 | // to avoid the problem. This can be removed when we no longer need | |
272 | // to care about gcc versions before 4.3. | |
273 | #if defined(__GNUC__) && GCC_VERSION < 4003 | |
274 | #define FUNCTION_NAME static_cast<const char*>(__FUNCTION__) | |
275 | #else | |
276 | #define FUNCTION_NAME __FUNCTION__ | |
277 | #endif | |
278 | ||
a3ad94ed ILT |
279 | // This macro and function are used in cases which can not arise if |
280 | // the code is written correctly. | |
281 | ||
282 | #define gold_unreachable() \ | |
cc70f101 | 283 | (gold::do_gold_unreachable(__FILE__, __LINE__, FUNCTION_NAME)) |
a3ad94ed ILT |
284 | |
285 | extern void do_gold_unreachable(const char*, int, const char*) | |
286 | ATTRIBUTE_NORETURN; | |
287 | ||
288 | // Assertion check. | |
289 | ||
290 | #define gold_assert(expr) ((void)(!(expr) ? gold_unreachable(), 0 : 0)) | |
bae7f79e | 291 | |
8486ee48 ILT |
292 | // Print version information. |
293 | extern void | |
294 | print_version(bool print_short); | |
295 | ||
4f211c8b ILT |
296 | // Get the version string. |
297 | extern const char* | |
298 | get_version_string(); | |
299 | ||
8383303e ILT |
300 | // Convert numeric types without unnoticed loss of precision. |
301 | template<typename To, typename From> | |
302 | inline To | |
303 | convert_types(const From from) | |
304 | { | |
305 | To to = from; | |
9bb53bf8 | 306 | gold_assert(static_cast<From>(to) == from); |
8383303e ILT |
307 | return to; |
308 | } | |
309 | ||
310 | // A common case of convert_types<>: convert to section_size_type. | |
311 | template<typename From> | |
312 | inline section_size_type | |
313 | convert_to_section_size_type(const From from) | |
314 | { return convert_types<section_size_type, From>(from); } | |
315 | ||
92e059d8 ILT |
316 | // Queue up the first set of tasks. |
317 | extern void | |
318 | queue_initial_tasks(const General_options&, | |
17a1d0a9 | 319 | Dirsearch&, |
5a6f7e2d | 320 | const Command_line&, |
92e059d8 ILT |
321 | Workqueue*, |
322 | Input_objects*, | |
323 | Symbol_table*, | |
7d9e3d98 ILT |
324 | Layout*, |
325 | Mapfile*); | |
92e059d8 | 326 | |
6d03d481 ST |
327 | // Queue up the set of tasks to be done before |
328 | // the middle set of tasks. Only used when garbage | |
072fe7ce | 329 | // collection is to be done. |
6d03d481 ST |
330 | extern void |
331 | queue_middle_gc_tasks(const General_options&, | |
332 | const Task*, | |
333 | const Input_objects*, | |
334 | Symbol_table*, | |
335 | Layout*, | |
336 | Workqueue*, | |
337 | Mapfile*); | |
338 | ||
92e059d8 ILT |
339 | // Queue up the middle set of tasks. |
340 | extern void | |
341 | queue_middle_tasks(const General_options&, | |
17a1d0a9 | 342 | const Task*, |
92e059d8 ILT |
343 | const Input_objects*, |
344 | Symbol_table*, | |
345 | Layout*, | |
7d9e3d98 ILT |
346 | Workqueue*, |
347 | Mapfile*); | |
92e059d8 ILT |
348 | |
349 | // Queue up the final set of tasks. | |
61ba1cf9 ILT |
350 | extern void |
351 | queue_final_tasks(const General_options&, | |
352 | const Input_objects*, | |
353 | const Symbol_table*, | |
27bc2bce | 354 | Layout*, |
61ba1cf9 ILT |
355 | Workqueue*, |
356 | Output_file* of); | |
357 | ||
6d03d481 ST |
358 | inline bool |
359 | is_prefix_of(const char* prefix, const char* str) | |
360 | { | |
361 | return strncmp(prefix, str, strlen(prefix)) == 0; | |
362 | } | |
363 | ||
f1ec9ded ST |
364 | const char* const cident_section_start_prefix = "__start_"; |
365 | const char* const cident_section_stop_prefix = "__stop_"; | |
366 | ||
367 | // Returns true if the name is a valid C identifier | |
368 | inline bool | |
369 | is_cident(const char* name) | |
370 | { | |
371 | return (name[strspn(name, | |
372 | ("0123456789" | |
373 | "ABCDEFGHIJKLMNOPWRSTUVWXYZ" | |
374 | "abcdefghijklmnopqrstuvwxyz" | |
375 | "_"))] | |
376 | == '\0'); | |
377 | } | |
378 | ||
b569affa DK |
379 | // We sometimes need to hash strings. Ideally we should use std::tr1::hash or |
380 | // __gnu_cxx::hash on some systems but there is no guarantee that either | |
381 | // one is available. For portability, we define simple string hash functions. | |
382 | ||
383 | template<typename Char_type> | |
384 | inline size_t | |
385 | string_hash(const Char_type* s, size_t length) | |
386 | { | |
387 | // This is the hash function used by the dynamic linker for | |
388 | // DT_GNU_HASH entries. I compared this to a Fowler/Noll/Vo hash | |
389 | // for a C++ program with 385,775 global symbols. This hash | |
390 | // function was very slightly worse. However, it is much faster to | |
391 | // compute. Overall wall clock time was a win. | |
392 | const unsigned char* p = reinterpret_cast<const unsigned char*>(s); | |
393 | size_t h = 5381; | |
394 | for (size_t i = 0; i < length * sizeof(Char_type); ++i) | |
395 | h = h * 33 + *p++; | |
396 | return h; | |
397 | } | |
398 | ||
399 | // Same as above except we expect the string to be zero terminated. | |
400 | ||
401 | template<typename Char_type> | |
402 | inline size_t | |
403 | string_hash(const Char_type* s) | |
404 | { | |
405 | const unsigned char* p = reinterpret_cast<const unsigned char*>(s); | |
406 | size_t h = 5381; | |
407 | for (size_t i = 0; s[i] != 0; ++i) | |
408 | { | |
409 | for (size_t j = 0; j < sizeof(Char_type); j++) | |
410 | h = h * 33 + *p++; | |
411 | } | |
412 | ||
413 | return h; | |
414 | } | |
415 | ||
6e9ba2ca ST |
416 | // Return whether STRING contains a wildcard character. This is used |
417 | // to speed up matching. | |
418 | ||
419 | inline bool | |
420 | is_wildcard_string(const char* s) | |
421 | { | |
422 | return strpbrk(s, "?*[") != NULL; | |
423 | } | |
424 | ||
bae7f79e ILT |
425 | } // End namespace gold. |
426 | ||
427 | #endif // !defined(GOLD_GOLD_H) |