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