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