* common.cc (Symbol_table::do_allocate_commons_list): For incremental
[binutils.git] / gold / gold.h
blob133a64ef25110c1bb62d58655ea4ec1e5347248b
1 // gold.h -- general definitions for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #ifndef GOLD_GOLD_H
24 #define GOLD_GOLD_H
26 #include "config.h"
27 #include "ansidecl.h"
29 #include <cstddef>
30 #include <cstring>
31 #include <stdint.h>
32 #include <sys/types.h>
34 #ifndef ENABLE_NLS
35 // The Solaris version of locale.h always includes libintl.h. If we
36 // have been configured with --disable-nls then ENABLE_NLS will not
37 // be defined and the dummy definitions of bindtextdomain (et al)
38 // below will conflict with the definitions in libintl.h. So we
39 // define these values to prevent the bogus inclusion of libintl.h.
40 # define _LIBINTL_H
41 # define _LIBGETTEXT_H
42 #endif
44 // Always include <clocale> first to avoid conflicts with the macros
45 // used when ENABLE_NLS is not defined.
46 #include <clocale>
48 #ifdef ENABLE_NLS
49 # include <libintl.h>
50 # define _(String) gettext (String)
51 # ifdef gettext_noop
52 # define N_(String) gettext_noop (String)
53 # else
54 # define N_(String) (String)
55 # endif
56 #else
57 # define gettext(Msgid) (Msgid)
58 # define dgettext(Domainname, Msgid) (Msgid)
59 # define dcgettext(Domainname, Msgid, Category) (Msgid)
60 # define textdomain(Domainname) while (0) /* nothing */
61 # define bindtextdomain(Domainname, Dirname) while (0) /* nothing */
62 # define _(String) (String)
63 # define N_(String) (String)
64 #endif
66 // Figure out how to get a hash set and a hash map.
68 #if defined(HAVE_TR1_UNORDERED_SET) && defined(HAVE_TR1_UNORDERED_MAP) \
69 && defined(HAVE_TR1_UNORDERED_MAP_REHASH)
71 #include <tr1/unordered_set>
72 #include <tr1/unordered_map>
74 // We need a template typedef here.
76 #define Unordered_set std::tr1::unordered_set
77 #define Unordered_map std::tr1::unordered_map
78 #define Unordered_multimap std::tr1::unordered_multimap
80 #define reserve_unordered_map(map, n) ((map)->rehash(n))
82 #elif defined(HAVE_EXT_HASH_MAP) && defined(HAVE_EXT_HASH_SET)
84 #include <ext/hash_map>
85 #include <ext/hash_set>
86 #include <string>
88 #define Unordered_set __gnu_cxx::hash_set
89 #define Unordered_map __gnu_cxx::hash_map
90 #define Unordered_multimap __gnu_cxx::hash_multimap
92 namespace __gnu_cxx
95 template<>
96 struct hash<std::string>
98 size_t
99 operator()(std::string s) const
100 { return __stl_hash_string(s.c_str()); }
103 template<typename T>
104 struct hash<T*>
106 size_t
107 operator()(T* p) const
108 { return reinterpret_cast<size_t>(p); }
113 #define reserve_unordered_map(map, n) ((map)->resize(n))
115 #else
117 // The fallback is to just use set and map.
119 #include <set>
120 #include <map>
122 #define Unordered_set std::set
123 #define Unordered_map std::map
124 #define Unordered_multimap std::multimap
126 #define reserve_unordered_map(map, n)
128 #endif
130 #ifndef HAVE_PREAD
131 extern "C" ssize_t pread(int, void*, size_t, off_t);
132 #endif
134 #ifndef HAVE_FTRUNCATE
135 extern "C" int ftruncate(int, off_t);
136 #endif
138 #ifndef HAVE_FFSLL
139 extern "C" int ffsll(long long);
140 #endif
142 #if !HAVE_DECL_MEMMEM
143 extern "C" void *memmem(const void *, size_t, const void *, size_t);
144 #endif
146 #if !HAVE_DECL_STRNDUP
147 extern "C" char *strndup(const char *, size_t);
148 #endif
150 namespace gold
153 // General declarations.
155 class General_options;
156 class Command_line;
157 class Dirsearch;
158 class Input_objects;
159 class Mapfile;
160 class Symbol;
161 class Symbol_table;
162 class Layout;
163 class Task;
164 class Workqueue;
165 class Output_file;
166 template<int size, bool big_endian>
167 struct Relocate_info;
169 // Some basic types. For these we use lower case initial letters.
171 // For an offset in an input or output file, use off_t. Note that
172 // this will often be a 64-bit type even for a 32-bit build.
174 // The size of a section if we are going to look at the contents.
175 typedef size_t section_size_type;
177 // An offset within a section when we are looking at the contents.
178 typedef ptrdiff_t section_offset_type;
180 // The name of the program as used in error messages.
181 extern const char* program_name;
183 // This function is called to exit the program. Status is true to
184 // exit success (0) and false to exit failure (1).
185 extern void
186 gold_exit(bool status) ATTRIBUTE_NORETURN;
188 // This function is called to emit an error message and then
189 // immediately exit with failure.
190 extern void
191 gold_fatal(const char* format, ...) ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF_1;
193 // This function is called to issue an error. This will cause gold to
194 // eventually exit with failure.
195 extern void
196 gold_error(const char* msg, ...) ATTRIBUTE_PRINTF_1;
198 // This function is called to issue a warning.
199 extern void
200 gold_warning(const char* msg, ...) ATTRIBUTE_PRINTF_1;
202 // This function is called to print an informational message.
203 extern void
204 gold_info(const char* msg, ...) ATTRIBUTE_PRINTF_1;
206 // Work around a bug in gcc 4.3.0. http://gcc.gnu.org/PR35546 . This
207 // can probably be removed after the bug has been fixed for a while.
208 #ifdef HAVE_TEMPLATE_ATTRIBUTES
209 #define TEMPLATE_ATTRIBUTE_PRINTF_4 ATTRIBUTE_PRINTF_4
210 #else
211 #define TEMPLATE_ATTRIBUTE_PRINTF_4
212 #endif
214 // This function is called to issue an error at the location of a
215 // reloc.
216 template<int size, bool big_endian>
217 extern void
218 gold_error_at_location(const Relocate_info<size, big_endian>*,
219 size_t, off_t, const char* format, ...)
220 TEMPLATE_ATTRIBUTE_PRINTF_4;
222 // This function is called to issue a warning at the location of a
223 // reloc.
224 template<int size, bool big_endian>
225 extern void
226 gold_warning_at_location(const Relocate_info<size, big_endian>*,
227 size_t, off_t, const char* format, ...)
228 TEMPLATE_ATTRIBUTE_PRINTF_4;
230 // This function is called to report an undefined symbol without
231 // a relocation (e.g., referenced by a dynamic object). SYM is
232 // the undefined symbol. The file name associated with the SYM
233 // is used to print a location for the undefined symbol.
234 extern void
235 gold_undefined_symbol(const Symbol*);
237 // This function is called to report an undefined symbol resulting
238 // from a relocation. SYM is the undefined symbol. RELINFO is the
239 // general relocation info. RELNUM is the number of the reloc,
240 // and RELOFFSET is the reloc's offset.
241 template<int size, bool big_endian>
242 extern void
243 gold_undefined_symbol_at_location(const Symbol*,
244 const Relocate_info<size, big_endian>*,
245 size_t, off_t);
247 // This is function is called in some cases if we run out of memory.
248 extern void
249 gold_nomem() ATTRIBUTE_NORETURN;
251 // In versions of gcc before 4.3, using __FUNCTION__ in a template
252 // function can cause gcc to get confused about whether or not the
253 // function can return. See http://gcc.gnu.org/PR30988. Use a macro
254 // to avoid the problem. This can be removed when we no longer need
255 // to care about gcc versions before 4.3.
256 #if defined(__GNUC__) && GCC_VERSION < 4003
257 #define FUNCTION_NAME static_cast<const char*>(__FUNCTION__)
258 #else
259 #define FUNCTION_NAME __FUNCTION__
260 #endif
262 // This macro and function are used in cases which can not arise if
263 // the code is written correctly.
265 #define gold_unreachable() \
266 (gold::do_gold_unreachable(__FILE__, __LINE__, FUNCTION_NAME))
268 extern void do_gold_unreachable(const char*, int, const char*)
269 ATTRIBUTE_NORETURN;
271 // Assertion check.
273 #define gold_assert(expr) ((void)(!(expr) ? gold_unreachable(), 0 : 0))
275 // Print version information.
276 extern void
277 print_version(bool print_short);
279 // Get the version string.
280 extern const char*
281 get_version_string();
283 // Convert numeric types without unnoticed loss of precision.
284 template<typename To, typename From>
285 inline To
286 convert_types(const From from)
288 To to = from;
289 gold_assert(static_cast<From>(to) == from);
290 return to;
293 // A common case of convert_types<>: convert to section_size_type.
294 template<typename From>
295 inline section_size_type
296 convert_to_section_size_type(const From from)
297 { return convert_types<section_size_type, From>(from); }
299 // Queue up the first set of tasks.
300 extern void
301 queue_initial_tasks(const General_options&,
302 Dirsearch&,
303 const Command_line&,
304 Workqueue*,
305 Input_objects*,
306 Symbol_table*,
307 Layout*,
308 Mapfile*);
310 // Queue up the set of tasks to be done before
311 // the middle set of tasks. Only used when garbage
312 // collection is to be done.
313 extern void
314 queue_middle_gc_tasks(const General_options&,
315 const Task*,
316 const Input_objects*,
317 Symbol_table*,
318 Layout*,
319 Workqueue*,
320 Mapfile*);
322 // Queue up the middle set of tasks.
323 extern void
324 queue_middle_tasks(const General_options&,
325 const Task*,
326 const Input_objects*,
327 Symbol_table*,
328 Layout*,
329 Workqueue*,
330 Mapfile*);
332 // Queue up the final set of tasks.
333 extern void
334 queue_final_tasks(const General_options&,
335 const Input_objects*,
336 const Symbol_table*,
337 Layout*,
338 Workqueue*,
339 Output_file* of);
341 inline bool
342 is_prefix_of(const char* prefix, const char* str)
344 return strncmp(prefix, str, strlen(prefix)) == 0;
347 const char* const cident_section_start_prefix = "__start_";
348 const char* const cident_section_stop_prefix = "__stop_";
350 // Returns true if the name is a valid C identifier
351 inline bool
352 is_cident(const char* name)
354 return (name[strspn(name,
355 ("0123456789"
356 "ABCDEFGHIJKLMNOPWRSTUVWXYZ"
357 "abcdefghijklmnopqrstuvwxyz"
358 "_"))]
359 == '\0');
362 // We sometimes need to hash strings. Ideally we should use std::tr1::hash or
363 // __gnu_cxx::hash on some systems but there is no guarantee that either
364 // one is available. For portability, we define simple string hash functions.
366 template<typename Char_type>
367 inline size_t
368 string_hash(const Char_type* s, size_t length)
370 // This is the hash function used by the dynamic linker for
371 // DT_GNU_HASH entries. I compared this to a Fowler/Noll/Vo hash
372 // for a C++ program with 385,775 global symbols. This hash
373 // function was very slightly worse. However, it is much faster to
374 // compute. Overall wall clock time was a win.
375 const unsigned char* p = reinterpret_cast<const unsigned char*>(s);
376 size_t h = 5381;
377 for (size_t i = 0; i < length * sizeof(Char_type); ++i)
378 h = h * 33 + *p++;
379 return h;
382 // Same as above except we expect the string to be zero terminated.
384 template<typename Char_type>
385 inline size_t
386 string_hash(const Char_type* s)
388 const unsigned char* p = reinterpret_cast<const unsigned char*>(s);
389 size_t h = 5381;
390 for (size_t i = 0; s[i] != 0; ++i)
392 for (size_t j = 0; j < sizeof(Char_type); j++)
393 h = h * 33 + *p++;
396 return h;
399 // Return whether STRING contains a wildcard character. This is used
400 // to speed up matching.
402 inline bool
403 is_wildcard_string(const char* s)
405 return strpbrk(s, "?*[") != NULL;
408 } // End namespace gold.
410 #endif // !defined(GOLD_GOLD_H)