* gold.h (reserve_unordered_map): Define, three versions, one for
[platform/upstream/binutils.git] / gold / gold.h
1 // gold.h -- general definitions for gold   -*- C++ -*-
2
3 // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
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
23 #ifndef GOLD_GOLD_H
24 #define GOLD_GOLD_H
25
26 #include "config.h"
27 #include "ansidecl.h"
28
29 #include <cstddef>
30 #include <cstring>
31 #include <stdint.h>
32 #include <sys/types.h>
33
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 defintions 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
43
44 // Always include <clocale> first to avoid conflicts with the macros
45 // used when ENABLE_NLS is not defined.
46 #include <clocale>
47
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
65
66 // Figure out how to get a hash set and a hash map.
67
68 #if defined(HAVE_TR1_UNORDERED_SET) && defined(HAVE_TR1_UNORDERED_MAP)
69
70 #include <tr1/unordered_set>
71 #include <tr1/unordered_map>
72
73 // We need a template typedef here.
74
75 #define Unordered_set std::tr1::unordered_set
76 #define Unordered_map std::tr1::unordered_map
77
78 #define reserve_unordered_map(map, n) ((map)->rehash(n))
79
80 #elif defined(HAVE_EXT_HASH_MAP) && defined(HAVE_EXT_HASH_SET)
81
82 #include <ext/hash_map>
83 #include <ext/hash_set>
84 #include <string>
85
86 #define Unordered_set __gnu_cxx::hash_set
87 #define Unordered_map __gnu_cxx::hash_map
88
89 namespace __gnu_cxx
90 {
91
92 template<>
93 struct hash<std::string>
94 {
95   size_t
96   operator()(std::string s) const
97   { return __stl_hash_string(s.c_str()); }
98 };
99
100 template<typename T>
101 struct hash<T*>
102 {
103   size_t
104   operator()(T* p) const
105   { return reinterpret_cast<size_t>(p); }
106 };
107
108 }
109
110 #define reserve_unordered_map(map, n) ((map)->resize(n))
111
112 #else
113
114 // The fallback is to just use set and map.
115
116 #include <set>
117 #include <map>
118
119 #define Unordered_set std::set
120 #define Unordered_map std::map
121
122 #define reserve_unordered_map(map, n)
123
124 #endif
125
126 #ifndef HAVE_PREAD
127 extern "C" ssize_t pread(int, void*, size_t, off_t);
128 #endif
129
130 #ifndef HAVE_FTRUNCATE
131 extern "C" int ftruncate(int, off_t);
132 #endif
133
134 #ifndef HAVE_MREMAP
135 #define MREMAP_MAYMOVE 1
136 extern "C" void *mremap(void *, size_t, size_t, int, ...);
137 #endif
138
139 #ifndef HAVE_FFSLL
140 extern "C" int ffsll(long long);
141 #endif
142
143 namespace gold
144 {
145
146 // General declarations.
147
148 class General_options;
149 class Command_line;
150 class Input_argument_list;
151 class Dirsearch;
152 class Input_objects;
153 class Mapfile;
154 class Symbol;
155 class Symbol_table;
156 class Layout;
157 class Task;
158 class Workqueue;
159 class Output_file;
160 template<int size, bool big_endian>
161 struct Relocate_info;
162
163 // Some basic types.  For these we use lower case initial letters.
164
165 // For an offset in an input or output file, use off_t.  Note that
166 // this will often be a 64-bit type even for a 32-bit build.
167
168 // The size of a section if we are going to look at the contents.
169 typedef size_t section_size_type;
170
171 // An offset within a section when we are looking at the contents.
172 typedef ptrdiff_t section_offset_type;
173
174 // The name of the program as used in error messages.
175 extern const char* program_name;
176
177 // This function is called to exit the program.  Status is true to
178 // exit success (0) and false to exit failure (1).
179 extern void
180 gold_exit(bool status) ATTRIBUTE_NORETURN;
181
182 // This function is called to emit an error message and then
183 // immediately exit with failure.
184 extern void
185 gold_fatal(const char* format, ...) ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF_1;
186
187 // This function is called to issue an error.  This will cause gold to
188 // eventually exit with failure.
189 extern void
190 gold_error(const char* msg, ...) ATTRIBUTE_PRINTF_1;
191
192 // This function is called to issue a warning.
193 extern void
194 gold_warning(const char* msg, ...) ATTRIBUTE_PRINTF_1;
195
196 // This function is called to print an informational message.
197 extern void
198 gold_info(const char* msg, ...) ATTRIBUTE_PRINTF_1;
199
200 // Work around a bug in gcc 4.3.0.  http://gcc.gnu.org/PR35546 .  This
201 // can probably be removed after the bug has been fixed for a while.
202 #ifdef HAVE_TEMPLATE_ATTRIBUTES
203 #define TEMPLATE_ATTRIBUTE_PRINTF_4 ATTRIBUTE_PRINTF_4
204 #else
205 #define TEMPLATE_ATTRIBUTE_PRINTF_4
206 #endif
207
208 // This function is called to issue an error at the location of a
209 // reloc.
210 template<int size, bool big_endian>
211 extern void
212 gold_error_at_location(const Relocate_info<size, big_endian>*,
213                        size_t, off_t, const char* format, ...)
214   TEMPLATE_ATTRIBUTE_PRINTF_4;
215
216 // This function is called to issue a warning at the location of a
217 // reloc.
218 template<int size, bool big_endian>
219 extern void
220 gold_warning_at_location(const Relocate_info<size, big_endian>*,
221                          size_t, off_t, const char* format, ...)
222   TEMPLATE_ATTRIBUTE_PRINTF_4;
223
224 // This function is called to report an undefined symbol without
225 // a relocation (e.g., referenced by a dynamic object).  SYM is
226 // the undefined symbol.  The file name associated with the SYM
227 // is used to print a location for the undefined symbol.
228 extern void
229 gold_undefined_symbol(const Symbol*);
230
231 // This function is called to report an undefined symbol resulting
232 // from a relocation.  SYM is the undefined symbol.  RELINFO is the
233 // general relocation info.  RELNUM is the number of the reloc,
234 // and RELOFFSET is the reloc's offset.
235 template<int size, bool big_endian>
236 extern void
237 gold_undefined_symbol_at_location(const Symbol*,
238                                   const Relocate_info<size, big_endian>*,
239                                   size_t, off_t);
240
241 // This is function is called in some cases if we run out of memory.
242 extern void
243 gold_nomem() ATTRIBUTE_NORETURN;
244
245 // This macro and function are used in cases which can not arise if
246 // the code is written correctly.
247
248 #define gold_unreachable() \
249   (gold::do_gold_unreachable(__FILE__, __LINE__, __FUNCTION__))
250
251 extern void do_gold_unreachable(const char*, int, const char*)
252   ATTRIBUTE_NORETURN;
253
254 // Assertion check.
255
256 #define gold_assert(expr) ((void)(!(expr) ? gold_unreachable(), 0 : 0))
257
258 // Print version information.
259 extern void
260 print_version(bool print_short);
261
262 // Get the version string.
263 extern const char*
264 get_version_string();
265
266 // Convert numeric types without unnoticed loss of precision.
267 template<typename To, typename From>
268 inline To
269 convert_types(const From from)
270 {
271   To to = from;
272   gold_assert(static_cast<From>(to) == from);
273   return to;
274 }
275
276 // A common case of convert_types<>: convert to section_size_type.
277 template<typename From>
278 inline section_size_type
279 convert_to_section_size_type(const From from)
280 { return convert_types<section_size_type, From>(from); }
281
282 // Queue up the first set of tasks.
283 extern void
284 queue_initial_tasks(const General_options&,
285                     Dirsearch&,
286                     const Command_line&,
287                     Workqueue*,
288                     Input_objects*,
289                     Symbol_table*,
290                     Layout*,
291                     Mapfile*);
292
293 // Queue up the set of tasks to be done before
294 // the middle set of tasks.  Only used when garbage
295 // collection is to be done. 
296 extern void
297 queue_middle_gc_tasks(const General_options&,
298                       const Task*,
299                       const Input_objects*,
300                       Symbol_table*,
301                       Layout*,
302                       Workqueue*,
303                       Mapfile*);
304
305 // Queue up the middle set of tasks.
306 extern void
307 queue_middle_tasks(const General_options&,
308                    const Task*,
309                    const Input_objects*,
310                    Symbol_table*,
311                    Layout*,
312                    Workqueue*,
313                    Mapfile*);
314
315 // Queue up the final set of tasks.
316 extern void
317 queue_final_tasks(const General_options&,
318                   const Input_objects*,
319                   const Symbol_table*,
320                   Layout*,
321                   Workqueue*,
322                   Output_file* of);
323
324 inline bool
325 is_prefix_of(const char* prefix, const char* str)
326 {
327   return strncmp(prefix, str, strlen(prefix)) == 0;
328 }
329
330 } // End namespace gold.
331
332 #endif // !defined(GOLD_GOLD_H)