1 // gold.h -- general definitions for gold -*- C++ -*- 2 3 // Copyright (C) 2006-2016 Free Software Foundation, Inc. 4 // Written by Ian Lance Taylor <iant (at) 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 <cstdlib> 31 #include <cstring> 32 #include <stdint.h> 33 #include <sys/types.h> 34 35 // __STDC_FORMAT_MACROS is needed to turn on macros in <inttypes.h>. 36 #define __STDC_FORMAT_MACROS 37 #include <inttypes.h> 38 39 #include "system.h" 40 41 namespace gold 42 { 43 44 // General declarations. 45 46 class General_options; 47 class Command_line; 48 class Dirsearch; 49 class Input_objects; 50 class Mapfile; 51 class Symbol; 52 class Symbol_table; 53 class Layout; 54 class Task; 55 class Workqueue; 56 class Output_file; 57 template<int size, bool big_endian> 58 struct Relocate_info; 59 60 // Exit status codes. 61 62 enum Exit_status 63 { 64 GOLD_OK = EXIT_SUCCESS, 65 GOLD_ERR = EXIT_FAILURE, 66 GOLD_FALLBACK = EXIT_FAILURE + 1 67 }; 68 69 // Some basic types. For these we use lower case initial letters. 70 71 // For an offset in an input or output file, use off_t. Note that 72 // this will often be a 64-bit type even for a 32-bit build. 73 74 // The size of a section if we are going to look at the contents. 75 typedef size_t section_size_type; 76 77 // An offset within a section when we are looking at the contents. 78 typedef ptrdiff_t section_offset_type; 79 80 // The name of the program as used in error messages. 81 extern const char* program_name; 82 83 // This function is called to exit the program. Status is true to 84 // exit success (0) and false to exit failure (1). 85 extern void 86 gold_exit(Exit_status status) ATTRIBUTE_NORETURN; 87 88 // This function is called to emit an error message and then 89 // immediately exit with failure. 90 extern void 91 gold_fatal(const char* format, ...) ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF_1; 92 93 // This function is called to issue an error. This will cause gold to 94 // eventually exit with failure. 95 extern void 96 gold_error(const char* msg, ...) ATTRIBUTE_PRINTF_1; 97 98 // This function is called to issue a warning. 99 extern void 100 gold_warning(const char* msg, ...) ATTRIBUTE_PRINTF_1; 101 102 // This function is called to print an informational message. 103 extern void 104 gold_info(const char* msg, ...) ATTRIBUTE_PRINTF_1; 105 106 // This function is called to emit an error message and then 107 // immediately exit with fallback status (e.g., when 108 // --incremental-update fails and the link needs to be restarted 109 // with --incremental-full). 110 extern void 111 gold_fallback(const char* format, ...) ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF_1; 112 113 // Work around a bug in gcc 4.3.0. http://gcc.gnu.org/PR35546 . This 114 // can probably be removed after the bug has been fixed for a while. 115 #ifdef HAVE_TEMPLATE_ATTRIBUTES 116 #define TEMPLATE_ATTRIBUTE_PRINTF_4 ATTRIBUTE_PRINTF_4 117 #else 118 #define TEMPLATE_ATTRIBUTE_PRINTF_4 119 #endif 120 121 // This function is called to issue an error at the location of a 122 // reloc. 123 template<int size, bool big_endian> 124 extern void 125 gold_error_at_location(const Relocate_info<size, big_endian>*, 126 size_t, off_t, const char* format, ...) 127 TEMPLATE_ATTRIBUTE_PRINTF_4; 128 129 // This function is called to issue a warning at the location of a 130 // reloc. 131 template<int size, bool big_endian> 132 extern void 133 gold_warning_at_location(const Relocate_info<size, big_endian>*, 134 size_t, off_t, const char* format, ...) 135 TEMPLATE_ATTRIBUTE_PRINTF_4; 136 137 // This function is called to report an undefined symbol without 138 // a relocation (e.g., referenced by a dynamic object). SYM is 139 // the undefined symbol. The file name associated with the SYM 140 // is used to print a location for the undefined symbol. 141 extern void 142 gold_undefined_symbol(const Symbol*); 143 144 // This function is called to report an undefined symbol resulting 145 // from a relocation. SYM is the undefined symbol. RELINFO is the 146 // general relocation info. RELNUM is the number of the reloc, 147 // and RELOFFSET is the reloc's offset. 148 template<int size, bool big_endian> 149 extern void 150 gold_undefined_symbol_at_location(const Symbol*, 151 const Relocate_info<size, big_endian>*, 152 size_t, off_t); 153 154 // This is function is called in some cases if we run out of memory. 155 extern void 156 gold_nomem() ATTRIBUTE_NORETURN; 157 158 // In versions of gcc before 4.3, using __FUNCTION__ in a template 159 // function can cause gcc to get confused about whether or not the 160 // function can return. See http://gcc.gnu.org/PR30988. Use a macro 161 // to avoid the problem. This can be removed when we no longer need 162 // to care about gcc versions before 4.3. 163 #if defined(__GNUC__) && GCC_VERSION < 4003 164 #define FUNCTION_NAME static_cast<const char*>(__FUNCTION__) 165 #else 166 #define FUNCTION_NAME __FUNCTION__ 167 #endif 168 169 // This macro and function are used in cases which can not arise if 170 // the code is written correctly. 171 172 #define gold_unreachable() \ 173 (gold::do_gold_unreachable(__FILE__, __LINE__, FUNCTION_NAME)) 174 175 extern void do_gold_unreachable(const char*, int, const char*) 176 ATTRIBUTE_NORETURN; 177 178 // Assertion check. 179 180 #define gold_assert(expr) ((void)(!(expr) ? gold_unreachable(), 0 : 0)) 181 182 // Print version information. 183 extern void 184 print_version(bool print_short); 185 186 // Get the version string. 187 extern const char* 188 get_version_string(); 189 190 // Convert numeric types without unnoticed loss of precision. 191 template<typename To, typename From> 192 inline To 193 convert_types(const From from) 194 { 195 To to = from; 196 gold_assert(static_cast<From>(to) == from); 197 return to; 198 } 199 200 // A common case of convert_types<>: convert to section_size_type. 201 template<typename From> 202 inline section_size_type 203 convert_to_section_size_type(const From from) 204 { return convert_types<section_size_type, From>(from); } 205 206 // Queue up the first set of tasks. 207 extern void 208 queue_initial_tasks(const General_options&, 209 Dirsearch&, 210 const Command_line&, 211 Workqueue*, 212 Input_objects*, 213 Symbol_table*, 214 Layout*, 215 Mapfile*); 216 217 // Queue up the set of tasks to be done before 218 // the middle set of tasks. Only used when garbage 219 // collection is to be done. 220 extern void 221 queue_middle_gc_tasks(const General_options&, 222 const Task*, 223 const Input_objects*, 224 Symbol_table*, 225 Layout*, 226 Workqueue*, 227 Mapfile*); 228 229 // Queue up the middle set of tasks. 230 extern void 231 queue_middle_tasks(const General_options&, 232 const Task*, 233 const Input_objects*, 234 Symbol_table*, 235 Layout*, 236 Workqueue*, 237 Mapfile*); 238 239 // Queue up the final set of tasks. 240 extern void 241 queue_final_tasks(const General_options&, 242 const Input_objects*, 243 const Symbol_table*, 244 Layout*, 245 Workqueue*, 246 Output_file* of); 247 248 inline bool 249 is_prefix_of(const char* prefix, const char* str) 250 { 251 return strncmp(prefix, str, strlen(prefix)) == 0; 252 } 253 254 const char* const cident_section_start_prefix = "__start_"; 255 const char* const cident_section_stop_prefix = "__stop_"; 256 257 // Returns true if the name is a valid C identifier 258 inline bool 259 is_cident(const char* name) 260 { 261 return (name[strspn(name, 262 ("0123456789" 263 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 264 "abcdefghijklmnopqrstuvwxyz" 265 "_"))] 266 == '\0'); 267 } 268 269 // We sometimes need to hash strings. Ideally we should use std::tr1::hash or 270 // __gnu_cxx::hash on some systems but there is no guarantee that either 271 // one is available. For portability, we define simple string hash functions. 272 273 template<typename Char_type> 274 inline size_t 275 string_hash(const Char_type* s, size_t length) 276 { 277 // This is the hash function used by the dynamic linker for 278 // DT_GNU_HASH entries. I compared this to a Fowler/Noll/Vo hash 279 // for a C++ program with 385,775 global symbols. This hash 280 // function was very slightly worse. However, it is much faster to 281 // compute. Overall wall clock time was a win. 282 const unsigned char* p = reinterpret_cast<const unsigned char*>(s); 283 size_t h = 5381; 284 for (size_t i = 0; i < length * sizeof(Char_type); ++i) 285 h = h * 33 + *p++; 286 return h; 287 } 288 289 // Same as above except we expect the string to be zero terminated. 290 291 template<typename Char_type> 292 inline size_t 293 string_hash(const Char_type* s) 294 { 295 const unsigned char* p = reinterpret_cast<const unsigned char*>(s); 296 size_t h = 5381; 297 for (size_t i = 0; s[i] != 0; ++i) 298 { 299 for (size_t j = 0; j < sizeof(Char_type); j++) 300 h = h * 33 + *p++; 301 } 302 303 return h; 304 } 305 306 // Return whether STRING contains a wildcard character. This is used 307 // to speed up matching. 308 309 inline bool 310 is_wildcard_string(const char* s) 311 { 312 return strpbrk(s, "?*[") != NULL; 313 } 314 315 } // End namespace gold. 316 317 #endif // !defined(GOLD_GOLD_H) 318