| 1 | // errors.cc -- handle errors for gold |
| 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 | #include "gold.h" |
| 24 | |
| 25 | #include <cstdarg> |
| 26 | #include <cstdio> |
| 27 | |
| 28 | #include "gold-threads.h" |
| 29 | #include "parameters.h" |
| 30 | #include "object.h" |
| 31 | #include "symtab.h" |
| 32 | #include "errors.h" |
| 33 | |
| 34 | namespace gold |
| 35 | { |
| 36 | |
| 37 | // Class Errors. |
| 38 | |
| 39 | const int Errors::max_undefined_error_report; |
| 40 | |
| 41 | Errors::Errors(const char* program_name) |
| 42 | : program_name_(program_name), lock_(NULL), initialize_lock_(&this->lock_), |
| 43 | error_count_(0), warning_count_(0), undefined_symbols_() |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | // Initialize the lock_ field. If we have not yet processed the |
| 48 | // parameters, then we can't initialize, since we don't yet know |
| 49 | // whether we are using threads. That is OK, since if we haven't |
| 50 | // processed the parameters, we haven't created any threads, and we |
| 51 | // don't need a lock. Return true if the lock is now initialized. |
| 52 | |
| 53 | bool |
| 54 | Errors::initialize_lock() |
| 55 | { |
| 56 | return this->initialize_lock_.initialize(); |
| 57 | } |
| 58 | |
| 59 | // Increment a counter, holding the lock if available. |
| 60 | |
| 61 | void |
| 62 | Errors::increment_counter(int *counter) |
| 63 | { |
| 64 | if (!this->initialize_lock()) |
| 65 | { |
| 66 | // The lock does not exist, which means that we don't need it. |
| 67 | ++*counter; |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | Hold_lock h(*this->lock_); |
| 72 | ++*counter; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // Report a fatal error. |
| 77 | |
| 78 | void |
| 79 | Errors::fatal(const char* format, va_list args) |
| 80 | { |
| 81 | fprintf(stderr, _("%s: fatal error: "), this->program_name_); |
| 82 | vfprintf(stderr, format, args); |
| 83 | fputc('\n', stderr); |
| 84 | gold_exit(false); |
| 85 | } |
| 86 | |
| 87 | // Report an error. |
| 88 | |
| 89 | void |
| 90 | Errors::error(const char* format, va_list args) |
| 91 | { |
| 92 | fprintf(stderr, _("%s: error: "), this->program_name_); |
| 93 | vfprintf(stderr, format, args); |
| 94 | fputc('\n', stderr); |
| 95 | |
| 96 | this->increment_counter(&this->error_count_); |
| 97 | } |
| 98 | |
| 99 | // Report a warning. |
| 100 | |
| 101 | void |
| 102 | Errors::warning(const char* format, va_list args) |
| 103 | { |
| 104 | fprintf(stderr, _("%s: warning: "), this->program_name_); |
| 105 | vfprintf(stderr, format, args); |
| 106 | fputc('\n', stderr); |
| 107 | |
| 108 | this->increment_counter(&this->warning_count_); |
| 109 | } |
| 110 | |
| 111 | // Print an informational message. |
| 112 | |
| 113 | void |
| 114 | Errors::info(const char* format, va_list args) |
| 115 | { |
| 116 | vfprintf(stderr, format, args); |
| 117 | fputc('\n', stderr); |
| 118 | } |
| 119 | |
| 120 | // Report an error at a reloc location. |
| 121 | |
| 122 | template<int size, bool big_endian> |
| 123 | void |
| 124 | Errors::error_at_location(const Relocate_info<size, big_endian>* relinfo, |
| 125 | size_t relnum, off_t reloffset, |
| 126 | const char* format, va_list args) |
| 127 | { |
| 128 | fprintf(stderr, _("%s: %s: error: "), this->program_name_, |
| 129 | relinfo->location(relnum, reloffset).c_str()); |
| 130 | vfprintf(stderr, format, args); |
| 131 | fputc('\n', stderr); |
| 132 | |
| 133 | this->increment_counter(&this->error_count_); |
| 134 | } |
| 135 | |
| 136 | // Report a warning at a reloc location. |
| 137 | |
| 138 | template<int size, bool big_endian> |
| 139 | void |
| 140 | Errors::warning_at_location(const Relocate_info<size, big_endian>* relinfo, |
| 141 | size_t relnum, off_t reloffset, |
| 142 | const char* format, va_list args) |
| 143 | { |
| 144 | fprintf(stderr, _("%s: %s: warning: "), this->program_name_, |
| 145 | relinfo->location(relnum, reloffset).c_str()); |
| 146 | vfprintf(stderr, format, args); |
| 147 | fputc('\n', stderr); |
| 148 | |
| 149 | this->increment_counter(&this->warning_count_); |
| 150 | } |
| 151 | |
| 152 | // Issue an undefined symbol error with a caller-supplied location string. |
| 153 | |
| 154 | void |
| 155 | Errors::undefined_symbol(const Symbol* sym, const std::string& location) |
| 156 | { |
| 157 | bool initialized = this->initialize_lock(); |
| 158 | gold_assert(initialized); |
| 159 | |
| 160 | const char* zmsg; |
| 161 | { |
| 162 | Hold_lock h(*this->lock_); |
| 163 | if (++this->undefined_symbols_[sym] >= max_undefined_error_report) |
| 164 | return; |
| 165 | if (parameters->options().warn_unresolved_symbols()) |
| 166 | { |
| 167 | ++this->warning_count_; |
| 168 | zmsg = _("warning"); |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | ++this->error_count_; |
| 173 | zmsg = _("error"); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | const char* const version = sym->version(); |
| 178 | if (version == NULL) |
| 179 | fprintf(stderr, _("%s: %s: %s: undefined reference to '%s'\n"), |
| 180 | this->program_name_, location.c_str(), zmsg, |
| 181 | sym->demangled_name().c_str()); |
| 182 | else |
| 183 | fprintf(stderr, |
| 184 | _("%s: %s: %s: undefined reference to '%s', version '%s'\n"), |
| 185 | this->program_name_, location.c_str(), zmsg, |
| 186 | sym->demangled_name().c_str(), version); |
| 187 | } |
| 188 | |
| 189 | // Issue a debugging message. |
| 190 | |
| 191 | void |
| 192 | Errors::debug(const char* format, ...) |
| 193 | { |
| 194 | fprintf(stderr, _("%s: "), this->program_name_); |
| 195 | |
| 196 | va_list args; |
| 197 | va_start(args, format); |
| 198 | vfprintf(stderr, format, args); |
| 199 | va_end(args); |
| 200 | |
| 201 | fputc('\n', stderr); |
| 202 | } |
| 203 | |
| 204 | // The functions which the rest of the code actually calls. |
| 205 | |
| 206 | // Report a fatal error. |
| 207 | |
| 208 | void |
| 209 | gold_fatal(const char* format, ...) |
| 210 | { |
| 211 | va_list args; |
| 212 | va_start(args, format); |
| 213 | parameters->errors()->fatal(format, args); |
| 214 | va_end(args); |
| 215 | } |
| 216 | |
| 217 | // Report an error. |
| 218 | |
| 219 | void |
| 220 | gold_error(const char* format, ...) |
| 221 | { |
| 222 | va_list args; |
| 223 | va_start(args, format); |
| 224 | parameters->errors()->error(format, args); |
| 225 | va_end(args); |
| 226 | } |
| 227 | |
| 228 | // Report a warning. |
| 229 | |
| 230 | void |
| 231 | gold_warning(const char* format, ...) |
| 232 | { |
| 233 | va_list args; |
| 234 | va_start(args, format); |
| 235 | parameters->errors()->warning(format, args); |
| 236 | va_end(args); |
| 237 | } |
| 238 | |
| 239 | // Print an informational message. |
| 240 | |
| 241 | void |
| 242 | gold_info(const char* format, ...) |
| 243 | { |
| 244 | va_list args; |
| 245 | va_start(args, format); |
| 246 | parameters->errors()->info(format, args); |
| 247 | va_end(args); |
| 248 | } |
| 249 | |
| 250 | // Report an error at a location. |
| 251 | |
| 252 | template<int size, bool big_endian> |
| 253 | void |
| 254 | gold_error_at_location(const Relocate_info<size, big_endian>* relinfo, |
| 255 | size_t relnum, off_t reloffset, |
| 256 | const char* format, ...) |
| 257 | { |
| 258 | va_list args; |
| 259 | va_start(args, format); |
| 260 | parameters->errors()->error_at_location(relinfo, relnum, reloffset, |
| 261 | format, args); |
| 262 | va_end(args); |
| 263 | } |
| 264 | |
| 265 | // Report a warning at a location. |
| 266 | |
| 267 | template<int size, bool big_endian> |
| 268 | void |
| 269 | gold_warning_at_location(const Relocate_info<size, big_endian>* relinfo, |
| 270 | size_t relnum, off_t reloffset, |
| 271 | const char* format, ...) |
| 272 | { |
| 273 | va_list args; |
| 274 | va_start(args, format); |
| 275 | parameters->errors()->warning_at_location(relinfo, relnum, reloffset, |
| 276 | format, args); |
| 277 | va_end(args); |
| 278 | } |
| 279 | |
| 280 | // Report an undefined symbol. |
| 281 | |
| 282 | void |
| 283 | gold_undefined_symbol(const Symbol* sym) |
| 284 | { |
| 285 | parameters->errors()->undefined_symbol(sym, sym->object()->name().c_str()); |
| 286 | } |
| 287 | |
| 288 | // Report an undefined symbol at a reloc location |
| 289 | |
| 290 | template<int size, bool big_endian> |
| 291 | void |
| 292 | gold_undefined_symbol_at_location(const Symbol* sym, |
| 293 | const Relocate_info<size, big_endian>* relinfo, |
| 294 | size_t relnum, off_t reloffset) |
| 295 | { |
| 296 | parameters->errors()->undefined_symbol(sym, |
| 297 | relinfo->location(relnum, reloffset)); |
| 298 | } |
| 299 | |
| 300 | #ifdef HAVE_TARGET_32_LITTLE |
| 301 | template |
| 302 | void |
| 303 | gold_error_at_location<32, false>(const Relocate_info<32, false>* relinfo, |
| 304 | size_t relnum, off_t reloffset, |
| 305 | const char* format, ...); |
| 306 | #endif |
| 307 | |
| 308 | #ifdef HAVE_TARGET_32_BIG |
| 309 | template |
| 310 | void |
| 311 | gold_error_at_location<32, true>(const Relocate_info<32, true>* relinfo, |
| 312 | size_t relnum, off_t reloffset, |
| 313 | const char* format, ...); |
| 314 | #endif |
| 315 | |
| 316 | #ifdef HAVE_TARGET_64_LITTLE |
| 317 | template |
| 318 | void |
| 319 | gold_error_at_location<64, false>(const Relocate_info<64, false>* relinfo, |
| 320 | size_t relnum, off_t reloffset, |
| 321 | const char* format, ...); |
| 322 | #endif |
| 323 | |
| 324 | #ifdef HAVE_TARGET_64_BIG |
| 325 | template |
| 326 | void |
| 327 | gold_error_at_location<64, true>(const Relocate_info<64, true>* relinfo, |
| 328 | size_t relnum, off_t reloffset, |
| 329 | const char* format, ...); |
| 330 | #endif |
| 331 | |
| 332 | #ifdef HAVE_TARGET_32_LITTLE |
| 333 | template |
| 334 | void |
| 335 | gold_warning_at_location<32, false>(const Relocate_info<32, false>* relinfo, |
| 336 | size_t relnum, off_t reloffset, |
| 337 | const char* format, ...); |
| 338 | #endif |
| 339 | |
| 340 | #ifdef HAVE_TARGET_32_BIG |
| 341 | template |
| 342 | void |
| 343 | gold_warning_at_location<32, true>(const Relocate_info<32, true>* relinfo, |
| 344 | size_t relnum, off_t reloffset, |
| 345 | const char* format, ...); |
| 346 | #endif |
| 347 | |
| 348 | #ifdef HAVE_TARGET_64_LITTLE |
| 349 | template |
| 350 | void |
| 351 | gold_warning_at_location<64, false>(const Relocate_info<64, false>* relinfo, |
| 352 | size_t relnum, off_t reloffset, |
| 353 | const char* format, ...); |
| 354 | #endif |
| 355 | |
| 356 | #ifdef HAVE_TARGET_64_BIG |
| 357 | template |
| 358 | void |
| 359 | gold_warning_at_location<64, true>(const Relocate_info<64, true>* relinfo, |
| 360 | size_t relnum, off_t reloffset, |
| 361 | const char* format, ...); |
| 362 | #endif |
| 363 | |
| 364 | #ifdef HAVE_TARGET_32_LITTLE |
| 365 | template |
| 366 | void |
| 367 | gold_undefined_symbol_at_location<32, false>( |
| 368 | const Symbol* sym, |
| 369 | const Relocate_info<32, false>* relinfo, |
| 370 | size_t relnum, off_t reloffset); |
| 371 | #endif |
| 372 | |
| 373 | #ifdef HAVE_TARGET_32_BIG |
| 374 | template |
| 375 | void |
| 376 | gold_undefined_symbol_at_location<32, true>( |
| 377 | const Symbol* sym, |
| 378 | const Relocate_info<32, true>* relinfo, |
| 379 | size_t relnum, off_t reloffset); |
| 380 | #endif |
| 381 | |
| 382 | #ifdef HAVE_TARGET_64_LITTLE |
| 383 | template |
| 384 | void |
| 385 | gold_undefined_symbol_at_location<64, false>( |
| 386 | const Symbol* sym, |
| 387 | const Relocate_info<64, false>* relinfo, |
| 388 | size_t relnum, off_t reloffset); |
| 389 | #endif |
| 390 | |
| 391 | #ifdef HAVE_TARGET_64_BIG |
| 392 | template |
| 393 | void |
| 394 | gold_undefined_symbol_at_location<64, true>( |
| 395 | const Symbol* sym, |
| 396 | const Relocate_info<64, true>* relinfo, |
| 397 | size_t relnum, off_t reloffset); |
| 398 | #endif |
| 399 | |
| 400 | } // End namespace gold. |