1176434Skaiw// errors.cc -- handle errors for gold
2176434Skaiw
3176434Skaiw// Copyright (C) 2006-2020 Free Software Foundation, Inc.
4176434Skaiw// Written by Ian Lance Taylor <iant@google.com>.
5176434Skaiw
6176434Skaiw// This file is part of gold.
7176434Skaiw
8176434Skaiw// This program is free software; you can redistribute it and/or modify
9176434Skaiw// it under the terms of the GNU General Public License as published by
10176434Skaiw// the Free Software Foundation; either version 3 of the License, or
11176434Skaiw// (at your option) any later version.
12176434Skaiw
13176434Skaiw// This program is distributed in the hope that it will be useful,
14176434Skaiw// but WITHOUT ANY WARRANTY; without even the implied warranty of
15176434Skaiw// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16176434Skaiw// GNU General Public License for more details.
17176434Skaiw
18176434Skaiw// You should have received a copy of the GNU General Public License
19176434Skaiw// along with this program; if not, write to the Free Software
20176434Skaiw// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21176434Skaiw// MA 02110-1301, USA.
22176434Skaiw
23176434Skaiw#include "gold.h"
24176434Skaiw
25176434Skaiw#include <cstdarg>
26176434Skaiw#include <cstdio>
27176434Skaiw
28176434Skaiw#include "gold-threads.h"
29176434Skaiw#include "parameters.h"
30176434Skaiw#include "object.h"
31176434Skaiw#include "symtab.h"
32176434Skaiw#include "errors.h"
33176434Skaiw
34176434Skaiwnamespace gold
35176434Skaiw{
36176434Skaiw
37176434Skaiw// Class Errors.
38176434Skaiw
39176434Skaiwconst int Errors::max_undefined_error_report;
40176434Skaiw
41176434SkaiwErrors::Errors(const char* program_name)
42176434Skaiw  : program_name_(program_name), lock_(NULL), initialize_lock_(&this->lock_),
43176434Skaiw    error_count_(0), warning_count_(0), undefined_symbols_()
44176434Skaiw{
45176434Skaiw}
46176434Skaiw
47176434Skaiw// Initialize the lock_ field.  If we have not yet processed the
48176434Skaiw// parameters, then we can't initialize, since we don't yet know
49176434Skaiw// whether we are using threads.  That is OK, since if we haven't
50176434Skaiw// processed the parameters, we haven't created any threads, and we
51176434Skaiw// don't need a lock.  Return true if the lock is now initialized.
52176434Skaiw
53176434Skaiwbool
54176434SkaiwErrors::initialize_lock()
55176434Skaiw{
56176434Skaiw  return this->initialize_lock_.initialize();
57176434Skaiw}
58176434Skaiw
59176434Skaiw// Increment a counter, holding the lock if available.
60176434Skaiw
61176434Skaiwvoid
62176434SkaiwErrors::increment_counter(int *counter)
63176434Skaiw{
64176434Skaiw  if (!this->initialize_lock())
65176434Skaiw    {
66176434Skaiw      // The lock does not exist, which means that we don't need it.
67176434Skaiw      ++*counter;
68176434Skaiw    }
69176434Skaiw  else
70176434Skaiw    {
71176434Skaiw      Hold_lock h(*this->lock_);
72176434Skaiw      ++*counter;
73176434Skaiw    }
74176434Skaiw}
75176434Skaiw
76176434Skaiw// Report a fatal error.
77176434Skaiw
78176434Skaiwvoid
79176434SkaiwErrors::fatal(const char* format, va_list args)
80176434Skaiw{
81176434Skaiw  fprintf(stderr, _("%s: fatal error: "), this->program_name_);
82176434Skaiw  vfprintf(stderr, format, args);
83176434Skaiw  fputc('\n', stderr);
84176434Skaiw  gold_exit(GOLD_ERR);
85176434Skaiw}
86176434Skaiw
87176434Skaiw// Report a fallback error.
88176434Skaiw
89176434Skaiwvoid
90201166SkientzleErrors::fallback(const char* format, va_list args)
91248612Smm{
92176434Skaiw  fprintf(stderr, _("%s: fatal error: "), this->program_name_);
93176434Skaiw  vfprintf(stderr, format, args);
94176434Skaiw  fputc('\n', stderr);
95176434Skaiw  gold_exit(GOLD_FALLBACK);
96176434Skaiw}
97176434Skaiw
98176434Skaiw// Report an error.
99176434Skaiw
100176434Skaiwvoid
101176434SkaiwErrors::error(const char* format, va_list args)
102176434Skaiw{
103176434Skaiw  fprintf(stderr, _("%s: error: "), this->program_name_);
104176434Skaiw  vfprintf(stderr, format, args);
105176434Skaiw  fputc('\n', stderr);
106176434Skaiw
107176434Skaiw  this->increment_counter(&this->error_count_);
108176434Skaiw}
109176434Skaiw
110176434Skaiw// Report a warning.
111176434Skaiw
112176434Skaiwvoid
113176434SkaiwErrors::warning(const char* format, va_list args)
114176434Skaiw{
115176434Skaiw  fprintf(stderr, _("%s: warning: "), this->program_name_);
116176434Skaiw  vfprintf(stderr, format, args);
117176434Skaiw  fputc('\n', stderr);
118176434Skaiw
119176434Skaiw  this->increment_counter(&this->warning_count_);
120176434Skaiw}
121176434Skaiw
122176434Skaiw// Print an informational message.
123176434Skaiw
124176434Skaiwvoid
125176434SkaiwErrors::info(const char* format, va_list args)
126176434Skaiw{
127176434Skaiw  vfprintf(stderr, format, args);
128176434Skaiw  fputc('\n', stderr);
129176434Skaiw}
130176434Skaiw
131176434Skaiw// Report an error at a reloc location.
132176434Skaiw
133176434Skaiwtemplate<int size, bool big_endian>
134176434Skaiwvoid
135176434SkaiwErrors::error_at_location(const Relocate_info<size, big_endian>* relinfo,
136176434Skaiw			  size_t relnum, off_t reloffset,
137176434Skaiw			  const char* format, va_list args)
138176434Skaiw{
139176434Skaiw  fprintf(stderr, _("%s: error: "),
140176434Skaiw	  relinfo->location(relnum, reloffset).c_str());
141176434Skaiw  vfprintf(stderr, format, args);
142176434Skaiw  fputc('\n', stderr);
143176434Skaiw
144176434Skaiw  this->increment_counter(&this->error_count_);
145176434Skaiw}
146176434Skaiw
147176434Skaiw// Report a warning at a reloc location.
148176434Skaiw
149176434Skaiwtemplate<int size, bool big_endian>
150176434Skaiwvoid
151176434SkaiwErrors::warning_at_location(const Relocate_info<size, big_endian>* relinfo,
152176434Skaiw			    size_t relnum, off_t reloffset,
153176434Skaiw			    const char* format, va_list args)
154176434Skaiw{
155176434Skaiw  fprintf(stderr, _("%s: warning: "),
156176434Skaiw	  relinfo->location(relnum, reloffset).c_str());
157176434Skaiw  vfprintf(stderr, format, args);
158176434Skaiw  fputc('\n', stderr);
159176434Skaiw
160176434Skaiw  this->increment_counter(&this->warning_count_);
161176434Skaiw}
162176434Skaiw
163176434Skaiw// Issue an undefined symbol error with a caller-supplied location string.
164176434Skaiw
165176434Skaiwvoid
166176434SkaiwErrors::undefined_symbol(const Symbol* sym, const std::string& location)
167176434Skaiw{
168176434Skaiw  bool initialized = this->initialize_lock();
169176434Skaiw  gold_assert(initialized);
170176434Skaiw
171176434Skaiw  const char* zmsg;
172176434Skaiw  {
173176434Skaiw    Hold_lock h(*this->lock_);
174176434Skaiw    if (++this->undefined_symbols_[sym] >= max_undefined_error_report)
175176434Skaiw      return;
176176434Skaiw    if (parameters->options().warn_unresolved_symbols())
177176434Skaiw      {
178176434Skaiw	++this->warning_count_;
179176434Skaiw	zmsg = _("warning");
180176434Skaiw      }
181176434Skaiw    else
182176434Skaiw      {
183176434Skaiw	++this->error_count_;
184176434Skaiw	zmsg = _("error");
185176434Skaiw      }
186176434Skaiw  }
187176434Skaiw
188176434Skaiw  const char* const version = sym->version();
189176434Skaiw  if (version == NULL)
190176434Skaiw    fprintf(stderr, _("%s: %s: undefined reference to '%s'\n"),
191176434Skaiw	    location.c_str(), zmsg, sym->demangled_name().c_str());
192176434Skaiw  else
193176434Skaiw    fprintf(stderr,
194176434Skaiw            _("%s: %s: undefined reference to '%s', version '%s'\n"),
195176434Skaiw	    location.c_str(), zmsg, sym->demangled_name().c_str(), version);
196176434Skaiw
197176434Skaiw  if (sym->is_cxx_vtable())
198176434Skaiw    gold_info(_("%s: the vtable symbol may be undefined because "
199176434Skaiw		"the class is missing its key function"),
200176434Skaiw	      program_name);
201176434Skaiw  if (sym->is_placeholder())
202248612Smm    gold_info(_("%s: the symbol should have been defined by a plugin"),
203176434Skaiw	      program_name);
204}
205
206// Issue a debugging message.
207
208void
209Errors::debug(const char* format, ...)
210{
211  fprintf(stderr, _("%s: "), this->program_name_);
212
213  va_list args;
214  va_start(args, format);
215  vfprintf(stderr, format, args);
216  va_end(args);
217
218  fputc('\n', stderr);
219}
220
221// The functions which the rest of the code actually calls.
222
223// Report a fatal error.
224
225void
226gold_fatal(const char* format, ...)
227{
228  va_list args;
229  va_start(args, format);
230  parameters->errors()->fatal(format, args);
231  va_end(args);
232}
233
234// Report a fallback error.
235
236void
237gold_fallback(const char* format, ...)
238{
239  va_list args;
240  va_start(args, format);
241  parameters->errors()->fallback(format, args);
242  va_end(args);
243}
244
245// Report an error.
246
247void
248gold_error(const char* format, ...)
249{
250  va_list args;
251  va_start(args, format);
252  parameters->errors()->error(format, args);
253  va_end(args);
254}
255
256// Report a warning.
257
258void
259gold_warning(const char* format, ...)
260{
261  va_list args;
262  va_start(args, format);
263  parameters->errors()->warning(format, args);
264  va_end(args);
265}
266
267// Print an informational message.
268
269void
270gold_info(const char* format, ...)
271{
272  va_list args;
273  va_start(args, format);
274  parameters->errors()->info(format, args);
275  va_end(args);
276}
277
278// Report an error at a location.
279
280template<int size, bool big_endian>
281void
282gold_error_at_location(const Relocate_info<size, big_endian>* relinfo,
283		       size_t relnum, off_t reloffset,
284		       const char* format, ...)
285{
286  va_list args;
287  va_start(args, format);
288  parameters->errors()->error_at_location(relinfo, relnum, reloffset,
289					  format, args);
290  va_end(args);
291}
292
293// Report a warning at a location.
294
295template<int size, bool big_endian>
296void
297gold_warning_at_location(const Relocate_info<size, big_endian>* relinfo,
298			 size_t relnum, off_t reloffset,
299			 const char* format, ...)
300{
301  va_list args;
302  va_start(args, format);
303  parameters->errors()->warning_at_location(relinfo, relnum, reloffset,
304					    format, args);
305  va_end(args);
306}
307
308// Report an undefined symbol.
309
310void
311gold_undefined_symbol(const Symbol* sym)
312{
313  parameters->errors()->undefined_symbol(sym, sym->object()->name().c_str());
314}
315
316// Report an undefined symbol at a reloc location
317
318template<int size, bool big_endian>
319void
320gold_undefined_symbol_at_location(const Symbol* sym,
321		      const Relocate_info<size, big_endian>* relinfo,
322		      size_t relnum, off_t reloffset)
323{
324  parameters->errors()->undefined_symbol(sym,
325                                         relinfo->location(relnum, reloffset));
326}
327
328#ifdef HAVE_TARGET_32_LITTLE
329template
330void
331gold_error_at_location<32, false>(const Relocate_info<32, false>* relinfo,
332				  size_t relnum, off_t reloffset,
333				  const char* format, ...);
334#endif
335
336#ifdef HAVE_TARGET_32_BIG
337template
338void
339gold_error_at_location<32, true>(const Relocate_info<32, true>* relinfo,
340				 size_t relnum, off_t reloffset,
341				 const char* format, ...);
342#endif
343
344#ifdef HAVE_TARGET_64_LITTLE
345template
346void
347gold_error_at_location<64, false>(const Relocate_info<64, false>* relinfo,
348				  size_t relnum, off_t reloffset,
349				  const char* format, ...);
350#endif
351
352#ifdef HAVE_TARGET_64_BIG
353template
354void
355gold_error_at_location<64, true>(const Relocate_info<64, true>* relinfo,
356				 size_t relnum, off_t reloffset,
357				 const char* format, ...);
358#endif
359
360#ifdef HAVE_TARGET_32_LITTLE
361template
362void
363gold_warning_at_location<32, false>(const Relocate_info<32, false>* relinfo,
364				    size_t relnum, off_t reloffset,
365				    const char* format, ...);
366#endif
367
368#ifdef HAVE_TARGET_32_BIG
369template
370void
371gold_warning_at_location<32, true>(const Relocate_info<32, true>* relinfo,
372				   size_t relnum, off_t reloffset,
373				   const char* format, ...);
374#endif
375
376#ifdef HAVE_TARGET_64_LITTLE
377template
378void
379gold_warning_at_location<64, false>(const Relocate_info<64, false>* relinfo,
380				    size_t relnum, off_t reloffset,
381				    const char* format, ...);
382#endif
383
384#ifdef HAVE_TARGET_64_BIG
385template
386void
387gold_warning_at_location<64, true>(const Relocate_info<64, true>* relinfo,
388				   size_t relnum, off_t reloffset,
389				   const char* format, ...);
390#endif
391
392#ifdef HAVE_TARGET_32_LITTLE
393template
394void
395gold_undefined_symbol_at_location<32, false>(
396    const Symbol* sym,
397    const Relocate_info<32, false>* relinfo,
398    size_t relnum, off_t reloffset);
399#endif
400
401#ifdef HAVE_TARGET_32_BIG
402template
403void
404gold_undefined_symbol_at_location<32, true>(
405    const Symbol* sym,
406    const Relocate_info<32, true>* relinfo,
407    size_t relnum, off_t reloffset);
408#endif
409
410#ifdef HAVE_TARGET_64_LITTLE
411template
412void
413gold_undefined_symbol_at_location<64, false>(
414    const Symbol* sym,
415    const Relocate_info<64, false>* relinfo,
416    size_t relnum, off_t reloffset);
417#endif
418
419#ifdef HAVE_TARGET_64_BIG
420template
421void
422gold_undefined_symbol_at_location<64, true>(
423    const Symbol* sym,
424    const Relocate_info<64, true>* relinfo,
425    size_t relnum, off_t reloffset);
426#endif
427
428} // End namespace gold.
429