| 1 | // errors.cc -- handle errors for gold |
| 2 | |
| 3 | // Copyright 2006, 2007 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), error_count_(0), |
| 43 | warning_count_(0), undefined_symbols_() |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | // Initialize the lock_ field. |
| 48 | |
| 49 | void |
| 50 | Errors::initialize_lock() |
| 51 | { |
| 52 | if (this->lock_ == NULL) |
| 53 | this->lock_ = new Lock; |
| 54 | } |
| 55 | |
| 56 | // Report a fatal error. |
| 57 | |
| 58 | void |
| 59 | Errors::fatal(const char* format, va_list args) |
| 60 | { |
| 61 | fprintf(stderr, "%s: ", this->program_name_); |
| 62 | vfprintf(stderr, format, args); |
| 63 | fputc('\n', stderr); |
| 64 | gold_exit(false); |
| 65 | } |
| 66 | |
| 67 | // Report an error. |
| 68 | |
| 69 | void |
| 70 | Errors::error(const char* format, va_list args) |
| 71 | { |
| 72 | fprintf(stderr, "%s: ", this->program_name_); |
| 73 | vfprintf(stderr, format, args); |
| 74 | fputc('\n', stderr); |
| 75 | |
| 76 | this->initialize_lock(); |
| 77 | { |
| 78 | Hold_lock h(*this->lock_); |
| 79 | ++this->error_count_; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // Report a warning. |
| 84 | |
| 85 | void |
| 86 | Errors::warning(const char* format, va_list args) |
| 87 | { |
| 88 | fprintf(stderr, _("%s: warning: "), this->program_name_); |
| 89 | vfprintf(stderr, format, args); |
| 90 | fputc('\n', stderr); |
| 91 | |
| 92 | this->initialize_lock(); |
| 93 | { |
| 94 | Hold_lock h(*this->lock_); |
| 95 | ++this->warning_count_; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Report an error at a reloc location. |
| 100 | |
| 101 | template<int size, bool big_endian> |
| 102 | void |
| 103 | Errors::error_at_location(const Relocate_info<size, big_endian>* relinfo, |
| 104 | size_t relnum, off_t reloffset, |
| 105 | const char* format, va_list args) |
| 106 | { |
| 107 | fprintf(stderr, "%s: %s: ", this->program_name_, |
| 108 | relinfo->location(relnum, reloffset).c_str()); |
| 109 | vfprintf(stderr, format, args); |
| 110 | fputc('\n', stderr); |
| 111 | |
| 112 | this->initialize_lock(); |
| 113 | { |
| 114 | Hold_lock h(*this->lock_); |
| 115 | ++this->error_count_; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // Report a warning at a reloc location. |
| 120 | |
| 121 | template<int size, bool big_endian> |
| 122 | void |
| 123 | Errors::warning_at_location(const Relocate_info<size, big_endian>* relinfo, |
| 124 | size_t relnum, off_t reloffset, |
| 125 | const char* format, va_list args) |
| 126 | { |
| 127 | fprintf(stderr, _("%s: %s: warning: "), this->program_name_, |
| 128 | relinfo->location(relnum, reloffset).c_str()); |
| 129 | vfprintf(stderr, format, args); |
| 130 | fputc('\n', stderr); |
| 131 | |
| 132 | this->initialize_lock(); |
| 133 | { |
| 134 | Hold_lock h(*this->lock_); |
| 135 | ++this->warning_count_; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // Issue an undefined symbol error. |
| 140 | |
| 141 | template<int size, bool big_endian> |
| 142 | void |
| 143 | Errors::undefined_symbol(const Symbol* sym, |
| 144 | const Relocate_info<size, big_endian>* relinfo, |
| 145 | size_t relnum, off_t reloffset) |
| 146 | { |
| 147 | this->initialize_lock(); |
| 148 | { |
| 149 | Hold_lock h(*this->lock_); |
| 150 | if (++this->undefined_symbols_[sym] >= max_undefined_error_report) |
| 151 | return; |
| 152 | ++this->error_count_; |
| 153 | } |
| 154 | fprintf(stderr, _("%s: %s: undefined reference to '%s'\n"), |
| 155 | this->program_name_, relinfo->location(relnum, reloffset).c_str(), |
| 156 | sym->demangled_name().c_str()); |
| 157 | } |
| 158 | |
| 159 | // Issue a debugging message. |
| 160 | |
| 161 | void |
| 162 | Errors::debug(const char* format, ...) |
| 163 | { |
| 164 | fprintf(stderr, _("%s: "), this->program_name_); |
| 165 | |
| 166 | va_list args; |
| 167 | va_start(args, format); |
| 168 | vfprintf(stderr, format, args); |
| 169 | va_end(args); |
| 170 | |
| 171 | fputc('\n', stderr); |
| 172 | } |
| 173 | |
| 174 | // The functions which the rest of the code actually calls. |
| 175 | |
| 176 | // Report a fatal error. |
| 177 | |
| 178 | void |
| 179 | gold_fatal(const char* format, ...) |
| 180 | { |
| 181 | va_list args; |
| 182 | va_start(args, format); |
| 183 | parameters->errors()->fatal(format, args); |
| 184 | va_end(args); |
| 185 | } |
| 186 | |
| 187 | // Report an error. |
| 188 | |
| 189 | void |
| 190 | gold_error(const char* format, ...) |
| 191 | { |
| 192 | va_list args; |
| 193 | va_start(args, format); |
| 194 | parameters->errors()->error(format, args); |
| 195 | va_end(args); |
| 196 | } |
| 197 | |
| 198 | // Report a warning. |
| 199 | |
| 200 | void |
| 201 | gold_warning(const char* format, ...) |
| 202 | { |
| 203 | va_list args; |
| 204 | va_start(args, format); |
| 205 | parameters->errors()->warning(format, args); |
| 206 | va_end(args); |
| 207 | } |
| 208 | |
| 209 | // Report an error at a location. |
| 210 | |
| 211 | template<int size, bool big_endian> |
| 212 | void |
| 213 | gold_error_at_location(const Relocate_info<size, big_endian>* relinfo, |
| 214 | size_t relnum, off_t reloffset, |
| 215 | const char* format, ...) |
| 216 | { |
| 217 | va_list args; |
| 218 | va_start(args, format); |
| 219 | parameters->errors()->error_at_location(relinfo, relnum, reloffset, |
| 220 | format, args); |
| 221 | va_end(args); |
| 222 | } |
| 223 | |
| 224 | // Report a warning at a location. |
| 225 | |
| 226 | template<int size, bool big_endian> |
| 227 | void |
| 228 | gold_warning_at_location(const Relocate_info<size, big_endian>* relinfo, |
| 229 | size_t relnum, off_t reloffset, |
| 230 | const char* format, ...) |
| 231 | { |
| 232 | va_list args; |
| 233 | va_start(args, format); |
| 234 | parameters->errors()->warning_at_location(relinfo, relnum, reloffset, |
| 235 | format, args); |
| 236 | va_end(args); |
| 237 | } |
| 238 | |
| 239 | // Report an undefined symbol. |
| 240 | |
| 241 | template<int size, bool big_endian> |
| 242 | void |
| 243 | gold_undefined_symbol(const Symbol* sym, |
| 244 | const Relocate_info<size, big_endian>* relinfo, |
| 245 | size_t relnum, off_t reloffset) |
| 246 | { |
| 247 | parameters->errors()->undefined_symbol(sym, relinfo, relnum, reloffset); |
| 248 | } |
| 249 | |
| 250 | #ifdef HAVE_TARGET_32_LITTLE |
| 251 | template |
| 252 | void |
| 253 | gold_error_at_location<32, false>(const Relocate_info<32, false>* relinfo, |
| 254 | size_t relnum, off_t reloffset, |
| 255 | const char* format, ...); |
| 256 | #endif |
| 257 | |
| 258 | #ifdef HAVE_TARGET_32_BIG |
| 259 | template |
| 260 | void |
| 261 | gold_error_at_location<32, true>(const Relocate_info<32, true>* relinfo, |
| 262 | size_t relnum, off_t reloffset, |
| 263 | const char* format, ...); |
| 264 | #endif |
| 265 | |
| 266 | #ifdef HAVE_TARGET_64_LITTLE |
| 267 | template |
| 268 | void |
| 269 | gold_error_at_location<64, false>(const Relocate_info<64, false>* relinfo, |
| 270 | size_t relnum, off_t reloffset, |
| 271 | const char* format, ...); |
| 272 | #endif |
| 273 | |
| 274 | #ifdef HAVE_TARGET_64_BIG |
| 275 | template |
| 276 | void |
| 277 | gold_error_at_location<64, true>(const Relocate_info<64, true>* relinfo, |
| 278 | size_t relnum, off_t reloffset, |
| 279 | const char* format, ...); |
| 280 | #endif |
| 281 | |
| 282 | #ifdef HAVE_TARGET_32_LITTLE |
| 283 | template |
| 284 | void |
| 285 | gold_warning_at_location<32, false>(const Relocate_info<32, false>* relinfo, |
| 286 | size_t relnum, off_t reloffset, |
| 287 | const char* format, ...); |
| 288 | #endif |
| 289 | |
| 290 | #ifdef HAVE_TARGET_32_BIG |
| 291 | template |
| 292 | void |
| 293 | gold_warning_at_location<32, true>(const Relocate_info<32, true>* relinfo, |
| 294 | size_t relnum, off_t reloffset, |
| 295 | const char* format, ...); |
| 296 | #endif |
| 297 | |
| 298 | #ifdef HAVE_TARGET_64_LITTLE |
| 299 | template |
| 300 | void |
| 301 | gold_warning_at_location<64, false>(const Relocate_info<64, false>* relinfo, |
| 302 | size_t relnum, off_t reloffset, |
| 303 | const char* format, ...); |
| 304 | #endif |
| 305 | |
| 306 | #ifdef HAVE_TARGET_64_BIG |
| 307 | template |
| 308 | void |
| 309 | gold_warning_at_location<64, true>(const Relocate_info<64, true>* relinfo, |
| 310 | size_t relnum, off_t reloffset, |
| 311 | const char* format, ...); |
| 312 | #endif |
| 313 | |
| 314 | #ifdef HAVE_TARGET_32_LITTLE |
| 315 | template |
| 316 | void |
| 317 | gold_undefined_symbol<32, false>(const Symbol* sym, |
| 318 | const Relocate_info<32, false>* relinfo, |
| 319 | size_t relnum, off_t reloffset); |
| 320 | #endif |
| 321 | |
| 322 | #ifdef HAVE_TARGET_32_BIG |
| 323 | template |
| 324 | void |
| 325 | gold_undefined_symbol<32, true>(const Symbol* sym, |
| 326 | const Relocate_info<32, true>* relinfo, |
| 327 | size_t relnum, off_t reloffset); |
| 328 | #endif |
| 329 | |
| 330 | #ifdef HAVE_TARGET_64_LITTLE |
| 331 | template |
| 332 | void |
| 333 | gold_undefined_symbol<64, false>(const Symbol* sym, |
| 334 | const Relocate_info<64, false>* relinfo, |
| 335 | size_t relnum, off_t reloffset); |
| 336 | #endif |
| 337 | |
| 338 | #ifdef HAVE_TARGET_64_BIG |
| 339 | template |
| 340 | void |
| 341 | gold_undefined_symbol<64, true>(const Symbol* sym, |
| 342 | const Relocate_info<64, true>* relinfo, |
| 343 | size_t relnum, off_t reloffset); |
| 344 | #endif |
| 345 | |
| 346 | } // End namespace gold. |