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