OpenCores
URL https://opencores.org/ocsvn/open8_urisc/open8_urisc/trunk

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [gnu/] [binutils/] [gold/] [symtab.cc] - Blame information for rev 309

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 khays
// symtab.cc -- the gold symbol table
2
 
3
// Copyright 2006, 2007, 2008, 2009, 2010, 2011 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 <cstring>
26
#include <stdint.h>
27
#include <algorithm>
28
#include <set>
29
#include <string>
30
#include <utility>
31
#include "demangle.h"
32
 
33
#include "gc.h"
34
#include "object.h"
35
#include "dwarf_reader.h"
36
#include "dynobj.h"
37
#include "output.h"
38
#include "target.h"
39
#include "workqueue.h"
40
#include "symtab.h"
41
#include "script.h"
42
#include "plugin.h"
43
#include "incremental.h"
44
 
45
namespace gold
46
{
47
 
48
// Class Symbol.
49
 
50
// Initialize fields in Symbol.  This initializes everything except u_
51
// and source_.
52
 
53
void
54
Symbol::init_fields(const char* name, const char* version,
55
                    elfcpp::STT type, elfcpp::STB binding,
56
                    elfcpp::STV visibility, unsigned char nonvis)
57
{
58
  this->name_ = name;
59
  this->version_ = version;
60
  this->symtab_index_ = 0;
61
  this->dynsym_index_ = 0;
62
  this->got_offsets_.init();
63
  this->plt_offset_ = -1U;
64
  this->type_ = type;
65
  this->binding_ = binding;
66
  this->visibility_ = visibility;
67
  this->nonvis_ = nonvis;
68
  this->is_def_ = false;
69
  this->is_forwarder_ = false;
70
  this->has_alias_ = false;
71
  this->needs_dynsym_entry_ = false;
72
  this->in_reg_ = false;
73
  this->in_dyn_ = false;
74
  this->has_warning_ = false;
75
  this->is_copied_from_dynobj_ = false;
76
  this->is_forced_local_ = false;
77
  this->is_ordinary_shndx_ = false;
78
  this->in_real_elf_ = false;
79
  this->is_defined_in_discarded_section_ = false;
80
  this->undef_binding_set_ = false;
81
  this->undef_binding_weak_ = false;
82 148 khays
  this->is_predefined_ = false;
83 27 khays
}
84
 
85
// Return the demangled version of the symbol's name, but only
86
// if the --demangle flag was set.
87
 
88
static std::string
89
demangle(const char* name)
90
{
91
  if (!parameters->options().do_demangle())
92
    return name;
93
 
94
  // cplus_demangle allocates memory for the result it returns,
95
  // and returns NULL if the name is already demangled.
96
  char* demangled_name = cplus_demangle(name, DMGL_ANSI | DMGL_PARAMS);
97
  if (demangled_name == NULL)
98
    return name;
99
 
100
  std::string retval(demangled_name);
101
  free(demangled_name);
102
  return retval;
103
}
104
 
105
std::string
106
Symbol::demangled_name() const
107
{
108
  return demangle(this->name());
109
}
110
 
111
// Initialize the fields in the base class Symbol for SYM in OBJECT.
112
 
113
template<int size, bool big_endian>
114
void
115
Symbol::init_base_object(const char* name, const char* version, Object* object,
116
                         const elfcpp::Sym<size, big_endian>& sym,
117
                         unsigned int st_shndx, bool is_ordinary)
118
{
119
  this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(),
120
                    sym.get_st_visibility(), sym.get_st_nonvis());
121
  this->u_.from_object.object = object;
122
  this->u_.from_object.shndx = st_shndx;
123
  this->is_ordinary_shndx_ = is_ordinary;
124
  this->source_ = FROM_OBJECT;
125
  this->in_reg_ = !object->is_dynamic();
126
  this->in_dyn_ = object->is_dynamic();
127
  this->in_real_elf_ = object->pluginobj() == NULL;
128
}
129
 
130
// Initialize the fields in the base class Symbol for a symbol defined
131
// in an Output_data.
132
 
133
void
134
Symbol::init_base_output_data(const char* name, const char* version,
135
                              Output_data* od, elfcpp::STT type,
136
                              elfcpp::STB binding, elfcpp::STV visibility,
137 148 khays
                              unsigned char nonvis, bool offset_is_from_end,
138
                              bool is_predefined)
139 27 khays
{
140
  this->init_fields(name, version, type, binding, visibility, nonvis);
141
  this->u_.in_output_data.output_data = od;
142
  this->u_.in_output_data.offset_is_from_end = offset_is_from_end;
143
  this->source_ = IN_OUTPUT_DATA;
144
  this->in_reg_ = true;
145
  this->in_real_elf_ = true;
146 148 khays
  this->is_predefined_ = is_predefined;
147 27 khays
}
148
 
149
// Initialize the fields in the base class Symbol for a symbol defined
150
// in an Output_segment.
151
 
152
void
153
Symbol::init_base_output_segment(const char* name, const char* version,
154
                                 Output_segment* os, elfcpp::STT type,
155
                                 elfcpp::STB binding, elfcpp::STV visibility,
156
                                 unsigned char nonvis,
157 148 khays
                                 Segment_offset_base offset_base,
158
                                 bool is_predefined)
159 27 khays
{
160
  this->init_fields(name, version, type, binding, visibility, nonvis);
161
  this->u_.in_output_segment.output_segment = os;
162
  this->u_.in_output_segment.offset_base = offset_base;
163
  this->source_ = IN_OUTPUT_SEGMENT;
164
  this->in_reg_ = true;
165
  this->in_real_elf_ = true;
166 148 khays
  this->is_predefined_ = is_predefined;
167 27 khays
}
168
 
169
// Initialize the fields in the base class Symbol for a symbol defined
170
// as a constant.
171
 
172
void
173
Symbol::init_base_constant(const char* name, const char* version,
174
                           elfcpp::STT type, elfcpp::STB binding,
175 148 khays
                           elfcpp::STV visibility, unsigned char nonvis,
176
                           bool is_predefined)
177 27 khays
{
178
  this->init_fields(name, version, type, binding, visibility, nonvis);
179
  this->source_ = IS_CONSTANT;
180
  this->in_reg_ = true;
181
  this->in_real_elf_ = true;
182 148 khays
  this->is_predefined_ = is_predefined;
183 27 khays
}
184
 
185
// Initialize the fields in the base class Symbol for an undefined
186
// symbol.
187
 
188
void
189
Symbol::init_base_undefined(const char* name, const char* version,
190
                            elfcpp::STT type, elfcpp::STB binding,
191
                            elfcpp::STV visibility, unsigned char nonvis)
192
{
193
  this->init_fields(name, version, type, binding, visibility, nonvis);
194
  this->dynsym_index_ = -1U;
195
  this->source_ = IS_UNDEFINED;
196
  this->in_reg_ = true;
197
  this->in_real_elf_ = true;
198
}
199
 
200
// Allocate a common symbol in the base.
201
 
202
void
203
Symbol::allocate_base_common(Output_data* od)
204
{
205
  gold_assert(this->is_common());
206
  this->source_ = IN_OUTPUT_DATA;
207
  this->u_.in_output_data.output_data = od;
208
  this->u_.in_output_data.offset_is_from_end = false;
209
}
210
 
211
// Initialize the fields in Sized_symbol for SYM in OBJECT.
212
 
213
template<int size>
214
template<bool big_endian>
215
void
216
Sized_symbol<size>::init_object(const char* name, const char* version,
217
                                Object* object,
218
                                const elfcpp::Sym<size, big_endian>& sym,
219
                                unsigned int st_shndx, bool is_ordinary)
220
{
221
  this->init_base_object(name, version, object, sym, st_shndx, is_ordinary);
222
  this->value_ = sym.get_st_value();
223
  this->symsize_ = sym.get_st_size();
224
}
225
 
226
// Initialize the fields in Sized_symbol for a symbol defined in an
227
// Output_data.
228
 
229
template<int size>
230
void
231
Sized_symbol<size>::init_output_data(const char* name, const char* version,
232
                                     Output_data* od, Value_type value,
233
                                     Size_type symsize, elfcpp::STT type,
234
                                     elfcpp::STB binding,
235
                                     elfcpp::STV visibility,
236
                                     unsigned char nonvis,
237 148 khays
                                     bool offset_is_from_end,
238
                                     bool is_predefined)
239 27 khays
{
240
  this->init_base_output_data(name, version, od, type, binding, visibility,
241 148 khays
                              nonvis, offset_is_from_end, is_predefined);
242 27 khays
  this->value_ = value;
243
  this->symsize_ = symsize;
244
}
245
 
246
// Initialize the fields in Sized_symbol for a symbol defined in an
247
// Output_segment.
248
 
249
template<int size>
250
void
251
Sized_symbol<size>::init_output_segment(const char* name, const char* version,
252
                                        Output_segment* os, Value_type value,
253
                                        Size_type symsize, elfcpp::STT type,
254
                                        elfcpp::STB binding,
255
                                        elfcpp::STV visibility,
256
                                        unsigned char nonvis,
257 148 khays
                                        Segment_offset_base offset_base,
258
                                        bool is_predefined)
259 27 khays
{
260
  this->init_base_output_segment(name, version, os, type, binding, visibility,
261 148 khays
                                 nonvis, offset_base, is_predefined);
262 27 khays
  this->value_ = value;
263
  this->symsize_ = symsize;
264
}
265
 
266
// Initialize the fields in Sized_symbol for a symbol defined as a
267
// constant.
268
 
269
template<int size>
270
void
271
Sized_symbol<size>::init_constant(const char* name, const char* version,
272
                                  Value_type value, Size_type symsize,
273
                                  elfcpp::STT type, elfcpp::STB binding,
274 148 khays
                                  elfcpp::STV visibility, unsigned char nonvis,
275
                                  bool is_predefined)
276 27 khays
{
277 148 khays
  this->init_base_constant(name, version, type, binding, visibility, nonvis,
278
                           is_predefined);
279 27 khays
  this->value_ = value;
280
  this->symsize_ = symsize;
281
}
282
 
283
// Initialize the fields in Sized_symbol for an undefined symbol.
284
 
285
template<int size>
286
void
287
Sized_symbol<size>::init_undefined(const char* name, const char* version,
288
                                   elfcpp::STT type, elfcpp::STB binding,
289
                                   elfcpp::STV visibility, unsigned char nonvis)
290
{
291
  this->init_base_undefined(name, version, type, binding, visibility, nonvis);
292
  this->value_ = 0;
293
  this->symsize_ = 0;
294
}
295
 
296 159 khays
// Return an allocated string holding the symbol's name as
297
// name@version.  This is used for relocatable links.
298
 
299
std::string
300
Symbol::versioned_name() const
301
{
302
  gold_assert(this->version_ != NULL);
303
  std::string ret = this->name_;
304
  ret.push_back('@');
305
  if (this->is_def_)
306
    ret.push_back('@');
307
  ret += this->version_;
308
  return ret;
309
}
310
 
311 27 khays
// Return true if SHNDX represents a common symbol.
312
 
313
bool
314
Symbol::is_common_shndx(unsigned int shndx)
315
{
316
  return (shndx == elfcpp::SHN_COMMON
317
          || shndx == parameters->target().small_common_shndx()
318
          || shndx == parameters->target().large_common_shndx());
319
}
320
 
321
// Allocate a common symbol.
322
 
323
template<int size>
324
void
325
Sized_symbol<size>::allocate_common(Output_data* od, Value_type value)
326
{
327
  this->allocate_base_common(od);
328
  this->value_ = value;
329
}
330
 
331
// The ""'s around str ensure str is a string literal, so sizeof works.
332
#define strprefix(var, str)   (strncmp(var, str, sizeof("" str "") - 1) == 0)
333
 
334
// Return true if this symbol should be added to the dynamic symbol
335
// table.
336
 
337
inline bool
338
Symbol::should_add_dynsym_entry(Symbol_table* symtab) const
339
{
340
  // If the symbol is only present on plugin files, the plugin decided we
341
  // don't need it.
342
  if (!this->in_real_elf())
343
    return false;
344
 
345
  // If the symbol is used by a dynamic relocation, we need to add it.
346
  if (this->needs_dynsym_entry())
347
    return true;
348
 
349
  // If this symbol's section is not added, the symbol need not be added. 
350
  // The section may have been GCed.  Note that export_dynamic is being 
351
  // overridden here.  This should not be done for shared objects.
352
  if (parameters->options().gc_sections()
353
      && !parameters->options().shared()
354
      && this->source() == Symbol::FROM_OBJECT
355
      && !this->object()->is_dynamic())
356
    {
357
      Relobj* relobj = static_cast<Relobj*>(this->object());
358
      bool is_ordinary;
359
      unsigned int shndx = this->shndx(&is_ordinary);
360
      if (is_ordinary && shndx != elfcpp::SHN_UNDEF
361
          && !relobj->is_section_included(shndx)
362
          && !symtab->is_section_folded(relobj, shndx))
363
        return false;
364
    }
365
 
366
  // If the symbol was forced local in a version script, do not add it.
367
  if (this->is_forced_local())
368
    return false;
369
 
370
  // If the symbol was forced dynamic in a --dynamic-list file, add it.
371
  if (parameters->options().in_dynamic_list(this->name()))
372
    return true;
373
 
374
  // If dynamic-list-data was specified, add any STT_OBJECT.
375
  if (parameters->options().dynamic_list_data()
376
      && !this->is_from_dynobj()
377
      && this->type() == elfcpp::STT_OBJECT)
378
    return true;
379
 
380
  // If --dynamic-list-cpp-new was specified, add any new/delete symbol.
381
  // If --dynamic-list-cpp-typeinfo was specified, add any typeinfo symbols.
382
  if ((parameters->options().dynamic_list_cpp_new()
383
       || parameters->options().dynamic_list_cpp_typeinfo())
384
      && !this->is_from_dynobj())
385
    {
386
      // TODO(csilvers): We could probably figure out if we're an operator
387
      //                 new/delete or typeinfo without the need to demangle.
388
      char* demangled_name = cplus_demangle(this->name(),
389
                                            DMGL_ANSI | DMGL_PARAMS);
390
      if (demangled_name == NULL)
391
        {
392
          // Not a C++ symbol, so it can't satisfy these flags
393
        }
394
      else if (parameters->options().dynamic_list_cpp_new()
395
               && (strprefix(demangled_name, "operator new")
396
                   || strprefix(demangled_name, "operator delete")))
397
        {
398
          free(demangled_name);
399
          return true;
400
        }
401
      else if (parameters->options().dynamic_list_cpp_typeinfo()
402
               && (strprefix(demangled_name, "typeinfo name for")
403
                   || strprefix(demangled_name, "typeinfo for")))
404
        {
405
          free(demangled_name);
406
          return true;
407
        }
408
      else
409
        free(demangled_name);
410
    }
411
 
412
  // If exporting all symbols or building a shared library,
413
  // and the symbol is defined in a regular object and is
414
  // externally visible, we need to add it.
415
  if ((parameters->options().export_dynamic() || parameters->options().shared())
416
      && !this->is_from_dynobj()
417 159 khays
      && !this->is_undefined()
418 27 khays
      && this->is_externally_visible())
419
    return true;
420
 
421
  return false;
422
}
423
 
424
// Return true if the final value of this symbol is known at link
425
// time.
426
 
427
bool
428
Symbol::final_value_is_known() const
429
{
430
  // If we are not generating an executable, then no final values are
431
  // known, since they will change at runtime.
432
  if (parameters->options().output_is_position_independent()
433
      || parameters->options().relocatable())
434
    return false;
435
 
436
  // If the symbol is not from an object file, and is not undefined,
437
  // then it is defined, and known.
438
  if (this->source_ != FROM_OBJECT)
439
    {
440
      if (this->source_ != IS_UNDEFINED)
441
        return true;
442
    }
443
  else
444
    {
445
      // If the symbol is from a dynamic object, then the final value
446
      // is not known.
447
      if (this->object()->is_dynamic())
448
        return false;
449
 
450
      // If the symbol is not undefined (it is defined or common),
451
      // then the final value is known.
452
      if (!this->is_undefined())
453
        return true;
454
    }
455
 
456
  // If the symbol is undefined, then whether the final value is known
457
  // depends on whether we are doing a static link.  If we are doing a
458
  // dynamic link, then the final value could be filled in at runtime.
459
  // This could reasonably be the case for a weak undefined symbol.
460
  return parameters->doing_static_link();
461
}
462
 
463
// Return the output section where this symbol is defined.
464
 
465
Output_section*
466
Symbol::output_section() const
467
{
468
  switch (this->source_)
469
    {
470
    case FROM_OBJECT:
471
      {
472
        unsigned int shndx = this->u_.from_object.shndx;
473
        if (shndx != elfcpp::SHN_UNDEF && this->is_ordinary_shndx_)
474
          {
475
            gold_assert(!this->u_.from_object.object->is_dynamic());
476
            gold_assert(this->u_.from_object.object->pluginobj() == NULL);
477
            Relobj* relobj = static_cast<Relobj*>(this->u_.from_object.object);
478
            return relobj->output_section(shndx);
479
          }
480
        return NULL;
481
      }
482
 
483
    case IN_OUTPUT_DATA:
484
      return this->u_.in_output_data.output_data->output_section();
485
 
486
    case IN_OUTPUT_SEGMENT:
487
    case IS_CONSTANT:
488
    case IS_UNDEFINED:
489
      return NULL;
490
 
491
    default:
492
      gold_unreachable();
493
    }
494
}
495
 
496
// Set the symbol's output section.  This is used for symbols defined
497
// in scripts.  This should only be called after the symbol table has
498
// been finalized.
499
 
500
void
501
Symbol::set_output_section(Output_section* os)
502
{
503
  switch (this->source_)
504
    {
505
    case FROM_OBJECT:
506
    case IN_OUTPUT_DATA:
507
      gold_assert(this->output_section() == os);
508
      break;
509
    case IS_CONSTANT:
510
      this->source_ = IN_OUTPUT_DATA;
511
      this->u_.in_output_data.output_data = os;
512
      this->u_.in_output_data.offset_is_from_end = false;
513
      break;
514
    case IN_OUTPUT_SEGMENT:
515
    case IS_UNDEFINED:
516
    default:
517
      gold_unreachable();
518
    }
519
}
520
 
521
// Class Symbol_table.
522
 
523
Symbol_table::Symbol_table(unsigned int count,
524
                           const Version_script_info& version_script)
525
  : saw_undefined_(0), offset_(0), table_(count), namepool_(),
526
    forwarders_(), commons_(), tls_commons_(), small_commons_(),
527
    large_commons_(), forced_locals_(), warnings_(),
528
    version_script_(version_script), gc_(NULL), icf_(NULL)
529
{
530
  namepool_.reserve(count);
531
}
532
 
533
Symbol_table::~Symbol_table()
534
{
535
}
536
 
537
// The symbol table key equality function.  This is called with
538
// Stringpool keys.
539
 
540
inline bool
541
Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1,
542
                                          const Symbol_table_key& k2) const
543
{
544
  return k1.first == k2.first && k1.second == k2.second;
545
}
546
 
547
bool
548
Symbol_table::is_section_folded(Object* obj, unsigned int shndx) const
549
{
550
  return (parameters->options().icf_enabled()
551
          && this->icf_->is_section_folded(obj, shndx));
552
}
553
 
554
// For symbols that have been listed with -u option, add them to the
555
// work list to avoid gc'ing them.
556
 
557
void
558
Symbol_table::gc_mark_undef_symbols(Layout* layout)
559
{
560
  for (options::String_set::const_iterator p =
561
         parameters->options().undefined_begin();
562
       p != parameters->options().undefined_end();
563
       ++p)
564
    {
565
      const char* name = p->c_str();
566
      Symbol* sym = this->lookup(name);
567
      gold_assert(sym != NULL);
568
      if (sym->source() == Symbol::FROM_OBJECT
569
          && !sym->object()->is_dynamic())
570
        {
571
          Relobj* obj = static_cast<Relobj*>(sym->object());
572
          bool is_ordinary;
573
          unsigned int shndx = sym->shndx(&is_ordinary);
574
          if (is_ordinary)
575
            {
576
              gold_assert(this->gc_ != NULL);
577
              this->gc_->worklist().push(Section_id(obj, shndx));
578
            }
579
        }
580
    }
581
 
582
  for (Script_options::referenced_const_iterator p =
583
         layout->script_options()->referenced_begin();
584
       p != layout->script_options()->referenced_end();
585
       ++p)
586
    {
587
      Symbol* sym = this->lookup(p->c_str());
588
      gold_assert(sym != NULL);
589
      if (sym->source() == Symbol::FROM_OBJECT
590
          && !sym->object()->is_dynamic())
591
        {
592
          Relobj* obj = static_cast<Relobj*>(sym->object());
593
          bool is_ordinary;
594
          unsigned int shndx = sym->shndx(&is_ordinary);
595
          if (is_ordinary)
596
            {
597
              gold_assert(this->gc_ != NULL);
598
              this->gc_->worklist().push(Section_id(obj, shndx));
599
            }
600
        }
601
    }
602
}
603
 
604
void
605 163 khays
Symbol_table::gc_mark_symbol(Symbol* sym)
606 27 khays
{
607 163 khays
  // Add the object and section to the work list.
608
  Relobj* obj = static_cast<Relobj*>(sym->object());
609
  bool is_ordinary;
610
  unsigned int shndx = sym->shndx(&is_ordinary);
611
  if (is_ordinary && shndx != elfcpp::SHN_UNDEF)
612 27 khays
    {
613 163 khays
      gold_assert(this->gc_!= NULL);
614
      this->gc_->worklist().push(Section_id(obj, shndx));
615 27 khays
    }
616
}
617
 
618
// When doing garbage collection, keep symbols that have been seen in
619
// dynamic objects.
620
inline void
621
Symbol_table::gc_mark_dyn_syms(Symbol* sym)
622
{
623
  if (sym->in_dyn() && sym->source() == Symbol::FROM_OBJECT
624
      && !sym->object()->is_dynamic())
625 163 khays
    this->gc_mark_symbol(sym);
626 27 khays
}
627
 
628
// Make TO a symbol which forwards to FROM.
629
 
630
void
631
Symbol_table::make_forwarder(Symbol* from, Symbol* to)
632
{
633
  gold_assert(from != to);
634
  gold_assert(!from->is_forwarder() && !to->is_forwarder());
635
  this->forwarders_[from] = to;
636
  from->set_forwarder();
637
}
638
 
639
// Resolve the forwards from FROM, returning the real symbol.
640
 
641
Symbol*
642
Symbol_table::resolve_forwards(const Symbol* from) const
643
{
644
  gold_assert(from->is_forwarder());
645
  Unordered_map<const Symbol*, Symbol*>::const_iterator p =
646
    this->forwarders_.find(from);
647
  gold_assert(p != this->forwarders_.end());
648
  return p->second;
649
}
650
 
651
// Look up a symbol by name.
652
 
653
Symbol*
654
Symbol_table::lookup(const char* name, const char* version) const
655
{
656
  Stringpool::Key name_key;
657
  name = this->namepool_.find(name, &name_key);
658
  if (name == NULL)
659
    return NULL;
660
 
661
  Stringpool::Key version_key = 0;
662
  if (version != NULL)
663
    {
664
      version = this->namepool_.find(version, &version_key);
665
      if (version == NULL)
666
        return NULL;
667
    }
668
 
669
  Symbol_table_key key(name_key, version_key);
670
  Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key);
671
  if (p == this->table_.end())
672
    return NULL;
673
  return p->second;
674
}
675
 
676
// Resolve a Symbol with another Symbol.  This is only used in the
677
// unusual case where there are references to both an unversioned
678
// symbol and a symbol with a version, and we then discover that that
679
// version is the default version.  Because this is unusual, we do
680
// this the slow way, by converting back to an ELF symbol.
681
 
682
template<int size, bool big_endian>
683
void
684
Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from)
685
{
686
  unsigned char buf[elfcpp::Elf_sizes<size>::sym_size];
687
  elfcpp::Sym_write<size, big_endian> esym(buf);
688
  // We don't bother to set the st_name or the st_shndx field.
689
  esym.put_st_value(from->value());
690
  esym.put_st_size(from->symsize());
691
  esym.put_st_info(from->binding(), from->type());
692
  esym.put_st_other(from->visibility(), from->nonvis());
693
  bool is_ordinary;
694
  unsigned int shndx = from->shndx(&is_ordinary);
695
  this->resolve(to, esym.sym(), shndx, is_ordinary, shndx, from->object(),
696
                from->version());
697
  if (from->in_reg())
698
    to->set_in_reg();
699
  if (from->in_dyn())
700
    to->set_in_dyn();
701
  if (parameters->options().gc_sections())
702
    this->gc_mark_dyn_syms(to);
703
}
704
 
705
// Record that a symbol is forced to be local by a version script or
706
// by visibility.
707
 
708
void
709
Symbol_table::force_local(Symbol* sym)
710
{
711
  if (!sym->is_defined() && !sym->is_common())
712
    return;
713
  if (sym->is_forced_local())
714
    {
715
      // We already got this one.
716
      return;
717
    }
718
  sym->set_is_forced_local();
719
  this->forced_locals_.push_back(sym);
720
}
721
 
722
// Adjust NAME for wrapping, and update *NAME_KEY if necessary.  This
723
// is only called for undefined symbols, when at least one --wrap
724
// option was used.
725
 
726
const char*
727
Symbol_table::wrap_symbol(const char* name, Stringpool::Key* name_key)
728
{
729
  // For some targets, we need to ignore a specific character when
730
  // wrapping, and add it back later.
731
  char prefix = '\0';
732
  if (name[0] == parameters->target().wrap_char())
733
    {
734
      prefix = name[0];
735
      ++name;
736
    }
737
 
738
  if (parameters->options().is_wrap(name))
739
    {
740
      // Turn NAME into __wrap_NAME.
741
      std::string s;
742
      if (prefix != '\0')
743
        s += prefix;
744
      s += "__wrap_";
745
      s += name;
746
 
747
      // This will give us both the old and new name in NAMEPOOL_, but
748
      // that is OK.  Only the versions we need will wind up in the
749
      // real string table in the output file.
750
      return this->namepool_.add(s.c_str(), true, name_key);
751
    }
752
 
753
  const char* const real_prefix = "__real_";
754
  const size_t real_prefix_length = strlen(real_prefix);
755
  if (strncmp(name, real_prefix, real_prefix_length) == 0
756
      && parameters->options().is_wrap(name + real_prefix_length))
757
    {
758
      // Turn __real_NAME into NAME.
759
      std::string s;
760
      if (prefix != '\0')
761
        s += prefix;
762
      s += name + real_prefix_length;
763
      return this->namepool_.add(s.c_str(), true, name_key);
764
    }
765
 
766
  return name;
767
}
768
 
769
// This is called when we see a symbol NAME/VERSION, and the symbol
770
// already exists in the symbol table, and VERSION is marked as being
771
// the default version.  SYM is the NAME/VERSION symbol we just added.
772
// DEFAULT_IS_NEW is true if this is the first time we have seen the
773
// symbol NAME/NULL.  PDEF points to the entry for NAME/NULL.
774
 
775
template<int size, bool big_endian>
776
void
777
Symbol_table::define_default_version(Sized_symbol<size>* sym,
778
                                     bool default_is_new,
779
                                     Symbol_table_type::iterator pdef)
780
{
781
  if (default_is_new)
782
    {
783
      // This is the first time we have seen NAME/NULL.  Make
784
      // NAME/NULL point to NAME/VERSION, and mark SYM as the default
785
      // version.
786
      pdef->second = sym;
787
      sym->set_is_default();
788
    }
789
  else if (pdef->second == sym)
790
    {
791
      // NAME/NULL already points to NAME/VERSION.  Don't mark the
792
      // symbol as the default if it is not already the default.
793
    }
794
  else
795
    {
796
      // This is the unfortunate case where we already have entries
797
      // for both NAME/VERSION and NAME/NULL.  We now see a symbol
798
      // NAME/VERSION where VERSION is the default version.  We have
799
      // already resolved this new symbol with the existing
800
      // NAME/VERSION symbol.
801
 
802
      // It's possible that NAME/NULL and NAME/VERSION are both
803
      // defined in regular objects.  This can only happen if one
804
      // object file defines foo and another defines foo@@ver.  This
805
      // is somewhat obscure, but we call it a multiple definition
806
      // error.
807
 
808
      // It's possible that NAME/NULL actually has a version, in which
809
      // case it won't be the same as VERSION.  This happens with
810
      // ver_test_7.so in the testsuite for the symbol t2_2.  We see
811
      // t2_2@@VER2, so we define both t2_2/VER2 and t2_2/NULL.  We
812
      // then see an unadorned t2_2 in an object file and give it
813
      // version VER1 from the version script.  This looks like a
814
      // default definition for VER1, so it looks like we should merge
815
      // t2_2/NULL with t2_2/VER1.  That doesn't make sense, but it's
816
      // not obvious that this is an error, either.  So we just punt.
817
 
818
      // If one of the symbols has non-default visibility, and the
819
      // other is defined in a shared object, then they are different
820
      // symbols.
821
 
822
      // Otherwise, we just resolve the symbols as though they were
823
      // the same.
824
 
825
      if (pdef->second->version() != NULL)
826
        gold_assert(pdef->second->version() != sym->version());
827
      else if (sym->visibility() != elfcpp::STV_DEFAULT
828
               && pdef->second->is_from_dynobj())
829
        ;
830
      else if (pdef->second->visibility() != elfcpp::STV_DEFAULT
831
               && sym->is_from_dynobj())
832
        ;
833
      else
834
        {
835
          const Sized_symbol<size>* symdef;
836
          symdef = this->get_sized_symbol<size>(pdef->second);
837
          Symbol_table::resolve<size, big_endian>(sym, symdef);
838
          this->make_forwarder(pdef->second, sym);
839
          pdef->second = sym;
840
          sym->set_is_default();
841
        }
842
    }
843
}
844
 
845
// Add one symbol from OBJECT to the symbol table.  NAME is symbol
846
// name and VERSION is the version; both are canonicalized.  DEF is
847
// whether this is the default version.  ST_SHNDX is the symbol's
848
// section index; IS_ORDINARY is whether this is a normal section
849
// rather than a special code.
850
 
851
// If IS_DEFAULT_VERSION is true, then this is the definition of a
852
// default version of a symbol.  That means that any lookup of
853
// NAME/NULL and any lookup of NAME/VERSION should always return the
854
// same symbol.  This is obvious for references, but in particular we
855
// want to do this for definitions: overriding NAME/NULL should also
856
// override NAME/VERSION.  If we don't do that, it would be very hard
857
// to override functions in a shared library which uses versioning.
858
 
859
// We implement this by simply making both entries in the hash table
860
// point to the same Symbol structure.  That is easy enough if this is
861
// the first time we see NAME/NULL or NAME/VERSION, but it is possible
862
// that we have seen both already, in which case they will both have
863
// independent entries in the symbol table.  We can't simply change
864
// the symbol table entry, because we have pointers to the entries
865
// attached to the object files.  So we mark the entry attached to the
866
// object file as a forwarder, and record it in the forwarders_ map.
867
// Note that entries in the hash table will never be marked as
868
// forwarders.
869
//
870
// ORIG_ST_SHNDX and ST_SHNDX are almost always the same.
871
// ORIG_ST_SHNDX is the section index in the input file, or SHN_UNDEF
872
// for a special section code.  ST_SHNDX may be modified if the symbol
873
// is defined in a section being discarded.
874
 
875
template<int size, bool big_endian>
876
Sized_symbol<size>*
877
Symbol_table::add_from_object(Object* object,
878
                              const char* name,
879
                              Stringpool::Key name_key,
880
                              const char* version,
881
                              Stringpool::Key version_key,
882
                              bool is_default_version,
883
                              const elfcpp::Sym<size, big_endian>& sym,
884
                              unsigned int st_shndx,
885
                              bool is_ordinary,
886
                              unsigned int orig_st_shndx)
887
{
888
  // Print a message if this symbol is being traced.
889
  if (parameters->options().is_trace_symbol(name))
890
    {
891
      if (orig_st_shndx == elfcpp::SHN_UNDEF)
892
        gold_info(_("%s: reference to %s"), object->name().c_str(), name);
893
      else
894
        gold_info(_("%s: definition of %s"), object->name().c_str(), name);
895
    }
896
 
897
  // For an undefined symbol, we may need to adjust the name using
898
  // --wrap.
899
  if (orig_st_shndx == elfcpp::SHN_UNDEF
900
      && parameters->options().any_wrap())
901
    {
902
      const char* wrap_name = this->wrap_symbol(name, &name_key);
903
      if (wrap_name != name)
904
        {
905
          // If we see a reference to malloc with version GLIBC_2.0,
906
          // and we turn it into a reference to __wrap_malloc, then we
907
          // discard the version number.  Otherwise the user would be
908
          // required to specify the correct version for
909
          // __wrap_malloc.
910
          version = NULL;
911
          version_key = 0;
912
          name = wrap_name;
913
        }
914
    }
915
 
916
  Symbol* const snull = NULL;
917
  std::pair<typename Symbol_table_type::iterator, bool> ins =
918
    this->table_.insert(std::make_pair(std::make_pair(name_key, version_key),
919
                                       snull));
920
 
921
  std::pair<typename Symbol_table_type::iterator, bool> insdefault =
922
    std::make_pair(this->table_.end(), false);
923
  if (is_default_version)
924
    {
925
      const Stringpool::Key vnull_key = 0;
926
      insdefault = this->table_.insert(std::make_pair(std::make_pair(name_key,
927
                                                                     vnull_key),
928
                                                      snull));
929
    }
930
 
931
  // ins.first: an iterator, which is a pointer to a pair.
932
  // ins.first->first: the key (a pair of name and version).
933
  // ins.first->second: the value (Symbol*).
934
  // ins.second: true if new entry was inserted, false if not.
935
 
936
  Sized_symbol<size>* ret;
937
  bool was_undefined;
938
  bool was_common;
939
  if (!ins.second)
940
    {
941
      // We already have an entry for NAME/VERSION.
942
      ret = this->get_sized_symbol<size>(ins.first->second);
943
      gold_assert(ret != NULL);
944
 
945
      was_undefined = ret->is_undefined();
946
      was_common = ret->is_common();
947
 
948
      this->resolve(ret, sym, st_shndx, is_ordinary, orig_st_shndx, object,
949
                    version);
950
      if (parameters->options().gc_sections())
951
        this->gc_mark_dyn_syms(ret);
952
 
953
      if (is_default_version)
954
        this->define_default_version<size, big_endian>(ret, insdefault.second,
955
                                                       insdefault.first);
956
    }
957
  else
958
    {
959
      // This is the first time we have seen NAME/VERSION.
960
      gold_assert(ins.first->second == NULL);
961
 
962
      if (is_default_version && !insdefault.second)
963
        {
964
          // We already have an entry for NAME/NULL.  If we override
965
          // it, then change it to NAME/VERSION.
966
          ret = this->get_sized_symbol<size>(insdefault.first->second);
967
 
968
          was_undefined = ret->is_undefined();
969
          was_common = ret->is_common();
970
 
971
          this->resolve(ret, sym, st_shndx, is_ordinary, orig_st_shndx, object,
972
                        version);
973
          if (parameters->options().gc_sections())
974
            this->gc_mark_dyn_syms(ret);
975
          ins.first->second = ret;
976
        }
977
      else
978
        {
979
          was_undefined = false;
980
          was_common = false;
981
 
982
          Sized_target<size, big_endian>* target =
983
            parameters->sized_target<size, big_endian>();
984
          if (!target->has_make_symbol())
985
            ret = new Sized_symbol<size>();
986
          else
987
            {
988
              ret = target->make_symbol();
989
              if (ret == NULL)
990
                {
991
                  // This means that we don't want a symbol table
992
                  // entry after all.
993
                  if (!is_default_version)
994
                    this->table_.erase(ins.first);
995
                  else
996
                    {
997
                      this->table_.erase(insdefault.first);
998
                      // Inserting INSDEFAULT invalidated INS.
999
                      this->table_.erase(std::make_pair(name_key,
1000
                                                        version_key));
1001
                    }
1002
                  return NULL;
1003
                }
1004
            }
1005
 
1006
          ret->init_object(name, version, object, sym, st_shndx, is_ordinary);
1007
 
1008
          ins.first->second = ret;
1009
          if (is_default_version)
1010
            {
1011
              // This is the first time we have seen NAME/NULL.  Point
1012
              // it at the new entry for NAME/VERSION.
1013
              gold_assert(insdefault.second);
1014
              insdefault.first->second = ret;
1015
            }
1016
        }
1017
 
1018
      if (is_default_version)
1019
        ret->set_is_default();
1020
    }
1021
 
1022
  // Record every time we see a new undefined symbol, to speed up
1023
  // archive groups.
1024
  if (!was_undefined && ret->is_undefined())
1025
    {
1026
      ++this->saw_undefined_;
1027
      if (parameters->options().has_plugins())
1028
        parameters->options().plugins()->new_undefined_symbol(ret);
1029
    }
1030
 
1031
  // Keep track of common symbols, to speed up common symbol
1032
  // allocation.
1033
  if (!was_common && ret->is_common())
1034
    {
1035
      if (ret->type() == elfcpp::STT_TLS)
1036
        this->tls_commons_.push_back(ret);
1037
      else if (!is_ordinary
1038
               && st_shndx == parameters->target().small_common_shndx())
1039
        this->small_commons_.push_back(ret);
1040
      else if (!is_ordinary
1041
               && st_shndx == parameters->target().large_common_shndx())
1042
        this->large_commons_.push_back(ret);
1043
      else
1044
        this->commons_.push_back(ret);
1045
    }
1046
 
1047
  // If we're not doing a relocatable link, then any symbol with
1048
  // hidden or internal visibility is local.
1049
  if ((ret->visibility() == elfcpp::STV_HIDDEN
1050
       || ret->visibility() == elfcpp::STV_INTERNAL)
1051
      && (ret->binding() == elfcpp::STB_GLOBAL
1052
          || ret->binding() == elfcpp::STB_GNU_UNIQUE
1053
          || ret->binding() == elfcpp::STB_WEAK)
1054
      && !parameters->options().relocatable())
1055
    this->force_local(ret);
1056
 
1057
  return ret;
1058
}
1059
 
1060
// Add all the symbols in a relocatable object to the hash table.
1061
 
1062
template<int size, bool big_endian>
1063
void
1064
Symbol_table::add_from_relobj(
1065
    Sized_relobj_file<size, big_endian>* relobj,
1066
    const unsigned char* syms,
1067
    size_t count,
1068
    size_t symndx_offset,
1069
    const char* sym_names,
1070
    size_t sym_name_size,
1071
    typename Sized_relobj_file<size, big_endian>::Symbols* sympointers,
1072
    size_t* defined)
1073
{
1074
  *defined = 0;
1075
 
1076
  gold_assert(size == parameters->target().get_size());
1077
 
1078
  const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1079
 
1080
  const bool just_symbols = relobj->just_symbols();
1081
 
1082
  const unsigned char* p = syms;
1083
  for (size_t i = 0; i < count; ++i, p += sym_size)
1084
    {
1085
      (*sympointers)[i] = NULL;
1086
 
1087
      elfcpp::Sym<size, big_endian> sym(p);
1088
 
1089
      unsigned int st_name = sym.get_st_name();
1090
      if (st_name >= sym_name_size)
1091
        {
1092
          relobj->error(_("bad global symbol name offset %u at %zu"),
1093
                        st_name, i);
1094
          continue;
1095
        }
1096
 
1097
      const char* name = sym_names + st_name;
1098
 
1099
      bool is_ordinary;
1100
      unsigned int st_shndx = relobj->adjust_sym_shndx(i + symndx_offset,
1101
                                                       sym.get_st_shndx(),
1102
                                                       &is_ordinary);
1103
      unsigned int orig_st_shndx = st_shndx;
1104
      if (!is_ordinary)
1105
        orig_st_shndx = elfcpp::SHN_UNDEF;
1106
 
1107
      if (st_shndx != elfcpp::SHN_UNDEF)
1108
        ++*defined;
1109
 
1110
      // A symbol defined in a section which we are not including must
1111
      // be treated as an undefined symbol.
1112
      bool is_defined_in_discarded_section = false;
1113
      if (st_shndx != elfcpp::SHN_UNDEF
1114
          && is_ordinary
1115
          && !relobj->is_section_included(st_shndx)
1116
          && !this->is_section_folded(relobj, st_shndx))
1117
        {
1118
          st_shndx = elfcpp::SHN_UNDEF;
1119
          is_defined_in_discarded_section = true;
1120
        }
1121
 
1122
      // In an object file, an '@' in the name separates the symbol
1123
      // name from the version name.  If there are two '@' characters,
1124
      // this is the default version.
1125
      const char* ver = strchr(name, '@');
1126
      Stringpool::Key ver_key = 0;
1127
      int namelen = 0;
1128
      // IS_DEFAULT_VERSION: is the version default?
1129
      // IS_FORCED_LOCAL: is the symbol forced local?
1130
      bool is_default_version = false;
1131
      bool is_forced_local = false;
1132
 
1133 163 khays
      // FIXME: For incremental links, we don't store version information,
1134
      // so we need to ignore version symbols for now.
1135
      if (parameters->incremental_update() && ver != NULL)
1136
        {
1137
          namelen = ver - name;
1138
          ver = NULL;
1139
        }
1140
 
1141 27 khays
      if (ver != NULL)
1142
        {
1143
          // The symbol name is of the form foo@VERSION or foo@@VERSION
1144
          namelen = ver - name;
1145
          ++ver;
1146
          if (*ver == '@')
1147
            {
1148
              is_default_version = true;
1149
              ++ver;
1150
            }
1151
          ver = this->namepool_.add(ver, true, &ver_key);
1152
        }
1153
      // We don't want to assign a version to an undefined symbol,
1154
      // even if it is listed in the version script.  FIXME: What
1155
      // about a common symbol?
1156
      else
1157
        {
1158
          namelen = strlen(name);
1159
          if (!this->version_script_.empty()
1160
              && st_shndx != elfcpp::SHN_UNDEF)
1161
            {
1162
              // The symbol name did not have a version, but the
1163
              // version script may assign a version anyway.
1164
              std::string version;
1165
              bool is_global;
1166
              if (this->version_script_.get_symbol_version(name, &version,
1167
                                                           &is_global))
1168
                {
1169
                  if (!is_global)
1170
                    is_forced_local = true;
1171
                  else if (!version.empty())
1172
                    {
1173
                      ver = this->namepool_.add_with_length(version.c_str(),
1174
                                                            version.length(),
1175
                                                            true,
1176
                                                            &ver_key);
1177
                      is_default_version = true;
1178
                    }
1179
                }
1180
            }
1181
        }
1182
 
1183
      elfcpp::Sym<size, big_endian>* psym = &sym;
1184
      unsigned char symbuf[sym_size];
1185
      elfcpp::Sym<size, big_endian> sym2(symbuf);
1186
      if (just_symbols)
1187
        {
1188
          memcpy(symbuf, p, sym_size);
1189
          elfcpp::Sym_write<size, big_endian> sw(symbuf);
1190 159 khays
          if (orig_st_shndx != elfcpp::SHN_UNDEF
1191
              && is_ordinary
1192
              && relobj->e_type() == elfcpp::ET_REL)
1193 27 khays
            {
1194 159 khays
              // Symbol values in relocatable object files are section
1195
              // relative.  This is normally what we want, but since here
1196
              // we are converting the symbol to absolute we need to add
1197
              // the section address.  The section address in an object
1198 27 khays
              // file is normally zero, but people can use a linker
1199
              // script to change it.
1200
              sw.put_st_value(sym.get_st_value()
1201
                              + relobj->section_address(orig_st_shndx));
1202
            }
1203
          st_shndx = elfcpp::SHN_ABS;
1204
          is_ordinary = false;
1205
          psym = &sym2;
1206
        }
1207
 
1208
      // Fix up visibility if object has no-export set.
1209
      if (relobj->no_export()
1210
          && (orig_st_shndx != elfcpp::SHN_UNDEF || !is_ordinary))
1211
        {
1212
          // We may have copied symbol already above.
1213
          if (psym != &sym2)
1214
            {
1215
              memcpy(symbuf, p, sym_size);
1216
              psym = &sym2;
1217
            }
1218
 
1219
          elfcpp::STV visibility = sym2.get_st_visibility();
1220
          if (visibility == elfcpp::STV_DEFAULT
1221
              || visibility == elfcpp::STV_PROTECTED)
1222
            {
1223
              elfcpp::Sym_write<size, big_endian> sw(symbuf);
1224
              unsigned char nonvis = sym2.get_st_nonvis();
1225
              sw.put_st_other(elfcpp::STV_HIDDEN, nonvis);
1226
            }
1227
        }
1228
 
1229
      Stringpool::Key name_key;
1230
      name = this->namepool_.add_with_length(name, namelen, true,
1231
                                             &name_key);
1232
 
1233
      Sized_symbol<size>* res;
1234
      res = this->add_from_object(relobj, name, name_key, ver, ver_key,
1235
                                  is_default_version, *psym, st_shndx,
1236
                                  is_ordinary, orig_st_shndx);
1237
 
1238 159 khays
      if (is_forced_local)
1239
        this->force_local(res);
1240
 
1241 163 khays
      // Do not treat this symbol as garbage if this symbol will be
1242
      // exported to the dynamic symbol table.  This is true when
1243
      // building a shared library or using --export-dynamic and
1244
      // the symbol is externally visible.
1245
      if (parameters->options().gc_sections()
1246
          && res->is_externally_visible()
1247
          && !res->is_from_dynobj()
1248
          && (parameters->options().shared()
1249
              || parameters->options().export_dynamic()))
1250
        this->gc_mark_symbol(res);
1251 27 khays
 
1252
      if (is_defined_in_discarded_section)
1253
        res->set_is_defined_in_discarded_section();
1254
 
1255
      (*sympointers)[i] = res;
1256
    }
1257
}
1258
 
1259
// Add a symbol from a plugin-claimed file.
1260
 
1261
template<int size, bool big_endian>
1262
Symbol*
1263
Symbol_table::add_from_pluginobj(
1264
    Sized_pluginobj<size, big_endian>* obj,
1265
    const char* name,
1266
    const char* ver,
1267
    elfcpp::Sym<size, big_endian>* sym)
1268
{
1269
  unsigned int st_shndx = sym->get_st_shndx();
1270
  bool is_ordinary = st_shndx < elfcpp::SHN_LORESERVE;
1271
 
1272
  Stringpool::Key ver_key = 0;
1273
  bool is_default_version = false;
1274
  bool is_forced_local = false;
1275
 
1276
  if (ver != NULL)
1277
    {
1278
      ver = this->namepool_.add(ver, true, &ver_key);
1279
    }
1280
  // We don't want to assign a version to an undefined symbol,
1281
  // even if it is listed in the version script.  FIXME: What
1282
  // about a common symbol?
1283
  else
1284
    {
1285
      if (!this->version_script_.empty()
1286
          && st_shndx != elfcpp::SHN_UNDEF)
1287
        {
1288
          // The symbol name did not have a version, but the
1289
          // version script may assign a version anyway.
1290
          std::string version;
1291
          bool is_global;
1292
          if (this->version_script_.get_symbol_version(name, &version,
1293
                                                       &is_global))
1294
            {
1295
              if (!is_global)
1296
                is_forced_local = true;
1297
              else if (!version.empty())
1298
                {
1299
                  ver = this->namepool_.add_with_length(version.c_str(),
1300
                                                        version.length(),
1301
                                                        true,
1302
                                                        &ver_key);
1303
                  is_default_version = true;
1304
                }
1305
            }
1306
        }
1307
    }
1308
 
1309
  Stringpool::Key name_key;
1310
  name = this->namepool_.add(name, true, &name_key);
1311
 
1312
  Sized_symbol<size>* res;
1313
  res = this->add_from_object(obj, name, name_key, ver, ver_key,
1314
                              is_default_version, *sym, st_shndx,
1315
                              is_ordinary, st_shndx);
1316
 
1317
  if (is_forced_local)
1318
    this->force_local(res);
1319
 
1320
  return res;
1321
}
1322
 
1323
// Add all the symbols in a dynamic object to the hash table.
1324
 
1325
template<int size, bool big_endian>
1326
void
1327
Symbol_table::add_from_dynobj(
1328
    Sized_dynobj<size, big_endian>* dynobj,
1329
    const unsigned char* syms,
1330
    size_t count,
1331
    const char* sym_names,
1332
    size_t sym_name_size,
1333
    const unsigned char* versym,
1334
    size_t versym_size,
1335
    const std::vector<const char*>* version_map,
1336
    typename Sized_relobj_file<size, big_endian>::Symbols* sympointers,
1337
    size_t* defined)
1338
{
1339
  *defined = 0;
1340
 
1341
  gold_assert(size == parameters->target().get_size());
1342
 
1343
  if (dynobj->just_symbols())
1344
    {
1345
      gold_error(_("--just-symbols does not make sense with a shared object"));
1346
      return;
1347
    }
1348
 
1349 163 khays
  // FIXME: For incremental links, we don't store version information,
1350
  // so we need to ignore version symbols for now.
1351
  if (parameters->incremental_update())
1352
    versym = NULL;
1353
 
1354 27 khays
  if (versym != NULL && versym_size / 2 < count)
1355
    {
1356
      dynobj->error(_("too few symbol versions"));
1357
      return;
1358
    }
1359
 
1360
  const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1361
 
1362
  // We keep a list of all STT_OBJECT symbols, so that we can resolve
1363
  // weak aliases.  This is necessary because if the dynamic object
1364
  // provides the same variable under two names, one of which is a
1365
  // weak definition, and the regular object refers to the weak
1366
  // definition, we have to put both the weak definition and the
1367
  // strong definition into the dynamic symbol table.  Given a weak
1368
  // definition, the only way that we can find the corresponding
1369
  // strong definition, if any, is to search the symbol table.
1370
  std::vector<Sized_symbol<size>*> object_symbols;
1371
 
1372
  const unsigned char* p = syms;
1373
  const unsigned char* vs = versym;
1374
  for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2)
1375
    {
1376
      elfcpp::Sym<size, big_endian> sym(p);
1377
 
1378
      if (sympointers != NULL)
1379
        (*sympointers)[i] = NULL;
1380
 
1381
      // Ignore symbols with local binding or that have
1382
      // internal or hidden visibility.
1383
      if (sym.get_st_bind() == elfcpp::STB_LOCAL
1384
          || sym.get_st_visibility() == elfcpp::STV_INTERNAL
1385
          || sym.get_st_visibility() == elfcpp::STV_HIDDEN)
1386
        continue;
1387
 
1388
      // A protected symbol in a shared library must be treated as a
1389
      // normal symbol when viewed from outside the shared library.
1390
      // Implement this by overriding the visibility here.
1391
      elfcpp::Sym<size, big_endian>* psym = &sym;
1392
      unsigned char symbuf[sym_size];
1393
      elfcpp::Sym<size, big_endian> sym2(symbuf);
1394
      if (sym.get_st_visibility() == elfcpp::STV_PROTECTED)
1395
        {
1396
          memcpy(symbuf, p, sym_size);
1397
          elfcpp::Sym_write<size, big_endian> sw(symbuf);
1398
          sw.put_st_other(elfcpp::STV_DEFAULT, sym.get_st_nonvis());
1399
          psym = &sym2;
1400
        }
1401
 
1402
      unsigned int st_name = psym->get_st_name();
1403
      if (st_name >= sym_name_size)
1404
        {
1405
          dynobj->error(_("bad symbol name offset %u at %zu"),
1406
                        st_name, i);
1407
          continue;
1408
        }
1409
 
1410
      const char* name = sym_names + st_name;
1411
 
1412
      bool is_ordinary;
1413
      unsigned int st_shndx = dynobj->adjust_sym_shndx(i, psym->get_st_shndx(),
1414
                                                       &is_ordinary);
1415
 
1416
      if (st_shndx != elfcpp::SHN_UNDEF)
1417
        ++*defined;
1418
 
1419
      Sized_symbol<size>* res;
1420
 
1421
      if (versym == NULL)
1422
        {
1423
          Stringpool::Key name_key;
1424
          name = this->namepool_.add(name, true, &name_key);
1425
          res = this->add_from_object(dynobj, name, name_key, NULL, 0,
1426
                                      false, *psym, st_shndx, is_ordinary,
1427
                                      st_shndx);
1428
        }
1429
      else
1430
        {
1431
          // Read the version information.
1432
 
1433
          unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs);
1434
 
1435
          bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
1436
          v &= elfcpp::VERSYM_VERSION;
1437
 
1438
          // The Sun documentation says that V can be VER_NDX_LOCAL,
1439
          // or VER_NDX_GLOBAL, or a version index.  The meaning of
1440
          // VER_NDX_LOCAL is defined as "Symbol has local scope."
1441
          // The old GNU linker will happily generate VER_NDX_LOCAL
1442
          // for an undefined symbol.  I don't know what the Sun
1443
          // linker will generate.
1444
 
1445
          if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
1446
              && st_shndx != elfcpp::SHN_UNDEF)
1447
            {
1448
              // This symbol should not be visible outside the object.
1449
              continue;
1450
            }
1451
 
1452
          // At this point we are definitely going to add this symbol.
1453
          Stringpool::Key name_key;
1454
          name = this->namepool_.add(name, true, &name_key);
1455
 
1456
          if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
1457
              || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
1458
            {
1459
              // This symbol does not have a version.
1460
              res = this->add_from_object(dynobj, name, name_key, NULL, 0,
1461
                                          false, *psym, st_shndx, is_ordinary,
1462
                                          st_shndx);
1463
            }
1464
          else
1465
            {
1466
              if (v >= version_map->size())
1467
                {
1468
                  dynobj->error(_("versym for symbol %zu out of range: %u"),
1469
                                i, v);
1470
                  continue;
1471
                }
1472
 
1473
              const char* version = (*version_map)[v];
1474
              if (version == NULL)
1475
                {
1476
                  dynobj->error(_("versym for symbol %zu has no name: %u"),
1477
                                i, v);
1478
                  continue;
1479
                }
1480
 
1481
              Stringpool::Key version_key;
1482
              version = this->namepool_.add(version, true, &version_key);
1483
 
1484
              // If this is an absolute symbol, and the version name
1485
              // and symbol name are the same, then this is the
1486
              // version definition symbol.  These symbols exist to
1487
              // support using -u to pull in particular versions.  We
1488
              // do not want to record a version for them.
1489
              if (st_shndx == elfcpp::SHN_ABS
1490
                  && !is_ordinary
1491
                  && name_key == version_key)
1492
                res = this->add_from_object(dynobj, name, name_key, NULL, 0,
1493
                                            false, *psym, st_shndx, is_ordinary,
1494
                                            st_shndx);
1495
              else
1496
                {
1497
                  const bool is_default_version =
1498
                    !hidden && st_shndx != elfcpp::SHN_UNDEF;
1499
                  res = this->add_from_object(dynobj, name, name_key, version,
1500
                                              version_key, is_default_version,
1501
                                              *psym, st_shndx,
1502
                                              is_ordinary, st_shndx);
1503
                }
1504
            }
1505
        }
1506
 
1507
      // Note that it is possible that RES was overridden by an
1508
      // earlier object, in which case it can't be aliased here.
1509
      if (st_shndx != elfcpp::SHN_UNDEF
1510
          && is_ordinary
1511
          && psym->get_st_type() == elfcpp::STT_OBJECT
1512
          && res->source() == Symbol::FROM_OBJECT
1513
          && res->object() == dynobj)
1514
        object_symbols.push_back(res);
1515
 
1516
      if (sympointers != NULL)
1517
        (*sympointers)[i] = res;
1518
    }
1519
 
1520
  this->record_weak_aliases(&object_symbols);
1521
}
1522
 
1523
// Add a symbol from a incremental object file.
1524
 
1525
template<int size, bool big_endian>
1526 148 khays
Sized_symbol<size>*
1527 27 khays
Symbol_table::add_from_incrobj(
1528
    Object* obj,
1529
    const char* name,
1530
    const char* ver,
1531
    elfcpp::Sym<size, big_endian>* sym)
1532
{
1533
  unsigned int st_shndx = sym->get_st_shndx();
1534
  bool is_ordinary = st_shndx < elfcpp::SHN_LORESERVE;
1535
 
1536
  Stringpool::Key ver_key = 0;
1537
  bool is_default_version = false;
1538
  bool is_forced_local = false;
1539
 
1540
  Stringpool::Key name_key;
1541
  name = this->namepool_.add(name, true, &name_key);
1542
 
1543
  Sized_symbol<size>* res;
1544
  res = this->add_from_object(obj, name, name_key, ver, ver_key,
1545
                              is_default_version, *sym, st_shndx,
1546
                              is_ordinary, st_shndx);
1547
 
1548
  if (is_forced_local)
1549
    this->force_local(res);
1550
 
1551
  return res;
1552
}
1553
 
1554
// This is used to sort weak aliases.  We sort them first by section
1555
// index, then by offset, then by weak ahead of strong.
1556
 
1557
template<int size>
1558
class Weak_alias_sorter
1559
{
1560
 public:
1561
  bool operator()(const Sized_symbol<size>*, const Sized_symbol<size>*) const;
1562
};
1563
 
1564
template<int size>
1565
bool
1566
Weak_alias_sorter<size>::operator()(const Sized_symbol<size>* s1,
1567
                                    const Sized_symbol<size>* s2) const
1568
{
1569
  bool is_ordinary;
1570
  unsigned int s1_shndx = s1->shndx(&is_ordinary);
1571
  gold_assert(is_ordinary);
1572
  unsigned int s2_shndx = s2->shndx(&is_ordinary);
1573
  gold_assert(is_ordinary);
1574
  if (s1_shndx != s2_shndx)
1575
    return s1_shndx < s2_shndx;
1576
 
1577
  if (s1->value() != s2->value())
1578
    return s1->value() < s2->value();
1579
  if (s1->binding() != s2->binding())
1580
    {
1581
      if (s1->binding() == elfcpp::STB_WEAK)
1582
        return true;
1583
      if (s2->binding() == elfcpp::STB_WEAK)
1584
        return false;
1585
    }
1586
  return std::string(s1->name()) < std::string(s2->name());
1587
}
1588
 
1589
// SYMBOLS is a list of object symbols from a dynamic object.  Look
1590
// for any weak aliases, and record them so that if we add the weak
1591
// alias to the dynamic symbol table, we also add the corresponding
1592
// strong symbol.
1593
 
1594
template<int size>
1595
void
1596
Symbol_table::record_weak_aliases(std::vector<Sized_symbol<size>*>* symbols)
1597
{
1598
  // Sort the vector by section index, then by offset, then by weak
1599
  // ahead of strong.
1600
  std::sort(symbols->begin(), symbols->end(), Weak_alias_sorter<size>());
1601
 
1602
  // Walk through the vector.  For each weak definition, record
1603
  // aliases.
1604
  for (typename std::vector<Sized_symbol<size>*>::const_iterator p =
1605
         symbols->begin();
1606
       p != symbols->end();
1607
       ++p)
1608
    {
1609
      if ((*p)->binding() != elfcpp::STB_WEAK)
1610
        continue;
1611
 
1612
      // Build a circular list of weak aliases.  Each symbol points to
1613
      // the next one in the circular list.
1614
 
1615
      Sized_symbol<size>* from_sym = *p;
1616
      typename std::vector<Sized_symbol<size>*>::const_iterator q;
1617
      for (q = p + 1; q != symbols->end(); ++q)
1618
        {
1619
          bool dummy;
1620
          if ((*q)->shndx(&dummy) != from_sym->shndx(&dummy)
1621
              || (*q)->value() != from_sym->value())
1622
            break;
1623
 
1624
          this->weak_aliases_[from_sym] = *q;
1625
          from_sym->set_has_alias();
1626
          from_sym = *q;
1627
        }
1628
 
1629
      if (from_sym != *p)
1630
        {
1631
          this->weak_aliases_[from_sym] = *p;
1632
          from_sym->set_has_alias();
1633
        }
1634
 
1635
      p = q - 1;
1636
    }
1637
}
1638
 
1639
// Create and return a specially defined symbol.  If ONLY_IF_REF is
1640
// true, then only create the symbol if there is a reference to it.
1641
// If this does not return NULL, it sets *POLDSYM to the existing
1642
// symbol if there is one.  This sets *RESOLVE_OLDSYM if we should
1643
// resolve the newly created symbol to the old one.  This
1644
// canonicalizes *PNAME and *PVERSION.
1645
 
1646
template<int size, bool big_endian>
1647
Sized_symbol<size>*
1648
Symbol_table::define_special_symbol(const char** pname, const char** pversion,
1649
                                    bool only_if_ref,
1650
                                    Sized_symbol<size>** poldsym,
1651
                                    bool* resolve_oldsym)
1652
{
1653
  *resolve_oldsym = false;
1654
 
1655
  // If the caller didn't give us a version, see if we get one from
1656
  // the version script.
1657
  std::string v;
1658
  bool is_default_version = false;
1659
  if (*pversion == NULL)
1660
    {
1661
      bool is_global;
1662
      if (this->version_script_.get_symbol_version(*pname, &v, &is_global))
1663
        {
1664
          if (is_global && !v.empty())
1665
            {
1666
              *pversion = v.c_str();
1667
              // If we get the version from a version script, then we
1668
              // are also the default version.
1669
              is_default_version = true;
1670
            }
1671
        }
1672
    }
1673
 
1674
  Symbol* oldsym;
1675
  Sized_symbol<size>* sym;
1676
 
1677
  bool add_to_table = false;
1678
  typename Symbol_table_type::iterator add_loc = this->table_.end();
1679
  bool add_def_to_table = false;
1680
  typename Symbol_table_type::iterator add_def_loc = this->table_.end();
1681
 
1682
  if (only_if_ref)
1683
    {
1684
      oldsym = this->lookup(*pname, *pversion);
1685
      if (oldsym == NULL && is_default_version)
1686
        oldsym = this->lookup(*pname, NULL);
1687
      if (oldsym == NULL || !oldsym->is_undefined())
1688
        return NULL;
1689
 
1690
      *pname = oldsym->name();
1691 163 khays
      if (is_default_version)
1692
        *pversion = this->namepool_.add(*pversion, true, NULL);
1693
      else
1694 27 khays
        *pversion = oldsym->version();
1695
    }
1696
  else
1697
    {
1698
      // Canonicalize NAME and VERSION.
1699
      Stringpool::Key name_key;
1700
      *pname = this->namepool_.add(*pname, true, &name_key);
1701
 
1702
      Stringpool::Key version_key = 0;
1703
      if (*pversion != NULL)
1704
        *pversion = this->namepool_.add(*pversion, true, &version_key);
1705
 
1706
      Symbol* const snull = NULL;
1707
      std::pair<typename Symbol_table_type::iterator, bool> ins =
1708
        this->table_.insert(std::make_pair(std::make_pair(name_key,
1709
                                                          version_key),
1710
                                           snull));
1711
 
1712
      std::pair<typename Symbol_table_type::iterator, bool> insdefault =
1713
        std::make_pair(this->table_.end(), false);
1714
      if (is_default_version)
1715
        {
1716
          const Stringpool::Key vnull = 0;
1717
          insdefault =
1718
            this->table_.insert(std::make_pair(std::make_pair(name_key,
1719
                                                              vnull),
1720
                                               snull));
1721
        }
1722
 
1723
      if (!ins.second)
1724
        {
1725
          // We already have a symbol table entry for NAME/VERSION.
1726
          oldsym = ins.first->second;
1727
          gold_assert(oldsym != NULL);
1728
 
1729
          if (is_default_version)
1730
            {
1731
              Sized_symbol<size>* soldsym =
1732
                this->get_sized_symbol<size>(oldsym);
1733
              this->define_default_version<size, big_endian>(soldsym,
1734
                                                             insdefault.second,
1735
                                                             insdefault.first);
1736
            }
1737
        }
1738
      else
1739
        {
1740
          // We haven't seen this symbol before.
1741
          gold_assert(ins.first->second == NULL);
1742
 
1743
          add_to_table = true;
1744
          add_loc = ins.first;
1745
 
1746
          if (is_default_version && !insdefault.second)
1747
            {
1748
              // We are adding NAME/VERSION, and it is the default
1749
              // version.  We already have an entry for NAME/NULL.
1750
              oldsym = insdefault.first->second;
1751
              *resolve_oldsym = true;
1752
            }
1753
          else
1754
            {
1755
              oldsym = NULL;
1756
 
1757
              if (is_default_version)
1758
                {
1759
                  add_def_to_table = true;
1760
                  add_def_loc = insdefault.first;
1761
                }
1762
            }
1763
        }
1764
    }
1765
 
1766
  const Target& target = parameters->target();
1767
  if (!target.has_make_symbol())
1768
    sym = new Sized_symbol<size>();
1769
  else
1770
    {
1771
      Sized_target<size, big_endian>* sized_target =
1772
        parameters->sized_target<size, big_endian>();
1773
      sym = sized_target->make_symbol();
1774
      if (sym == NULL)
1775
        return NULL;
1776
    }
1777
 
1778
  if (add_to_table)
1779
    add_loc->second = sym;
1780
  else
1781
    gold_assert(oldsym != NULL);
1782
 
1783
  if (add_def_to_table)
1784
    add_def_loc->second = sym;
1785
 
1786
  *poldsym = this->get_sized_symbol<size>(oldsym);
1787
 
1788
  return sym;
1789
}
1790
 
1791
// Define a symbol based on an Output_data.
1792
 
1793
Symbol*
1794
Symbol_table::define_in_output_data(const char* name,
1795
                                    const char* version,
1796
                                    Defined defined,
1797
                                    Output_data* od,
1798
                                    uint64_t value,
1799
                                    uint64_t symsize,
1800
                                    elfcpp::STT type,
1801
                                    elfcpp::STB binding,
1802
                                    elfcpp::STV visibility,
1803
                                    unsigned char nonvis,
1804
                                    bool offset_is_from_end,
1805
                                    bool only_if_ref)
1806
{
1807
  if (parameters->target().get_size() == 32)
1808
    {
1809
#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1810
      return this->do_define_in_output_data<32>(name, version, defined, od,
1811
                                                value, symsize, type, binding,
1812
                                                visibility, nonvis,
1813
                                                offset_is_from_end,
1814
                                                only_if_ref);
1815
#else
1816
      gold_unreachable();
1817
#endif
1818
    }
1819
  else if (parameters->target().get_size() == 64)
1820
    {
1821
#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1822
      return this->do_define_in_output_data<64>(name, version, defined, od,
1823
                                                value, symsize, type, binding,
1824
                                                visibility, nonvis,
1825
                                                offset_is_from_end,
1826
                                                only_if_ref);
1827
#else
1828
      gold_unreachable();
1829
#endif
1830
    }
1831
  else
1832
    gold_unreachable();
1833
}
1834
 
1835
// Define a symbol in an Output_data, sized version.
1836
 
1837
template<int size>
1838
Sized_symbol<size>*
1839
Symbol_table::do_define_in_output_data(
1840
    const char* name,
1841
    const char* version,
1842
    Defined defined,
1843
    Output_data* od,
1844
    typename elfcpp::Elf_types<size>::Elf_Addr value,
1845
    typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1846
    elfcpp::STT type,
1847
    elfcpp::STB binding,
1848
    elfcpp::STV visibility,
1849
    unsigned char nonvis,
1850
    bool offset_is_from_end,
1851
    bool only_if_ref)
1852
{
1853
  Sized_symbol<size>* sym;
1854
  Sized_symbol<size>* oldsym;
1855
  bool resolve_oldsym;
1856
 
1857
  if (parameters->target().is_big_endian())
1858
    {
1859
#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1860
      sym = this->define_special_symbol<size, true>(&name, &version,
1861
                                                    only_if_ref, &oldsym,
1862
                                                    &resolve_oldsym);
1863
#else
1864
      gold_unreachable();
1865
#endif
1866
    }
1867
  else
1868
    {
1869
#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1870
      sym = this->define_special_symbol<size, false>(&name, &version,
1871
                                                     only_if_ref, &oldsym,
1872
                                                     &resolve_oldsym);
1873
#else
1874
      gold_unreachable();
1875
#endif
1876
    }
1877
 
1878
  if (sym == NULL)
1879
    return NULL;
1880
 
1881
  sym->init_output_data(name, version, od, value, symsize, type, binding,
1882 148 khays
                        visibility, nonvis, offset_is_from_end,
1883
                        defined == PREDEFINED);
1884 27 khays
 
1885
  if (oldsym == NULL)
1886
    {
1887
      if (binding == elfcpp::STB_LOCAL
1888
          || this->version_script_.symbol_is_local(name))
1889
        this->force_local(sym);
1890
      else if (version != NULL)
1891
        sym->set_is_default();
1892
      return sym;
1893
    }
1894
 
1895 159 khays
  if (Symbol_table::should_override_with_special(oldsym, type, defined))
1896 27 khays
    this->override_with_special(oldsym, sym);
1897
 
1898
  if (resolve_oldsym)
1899
    return sym;
1900
  else
1901
    {
1902
      delete sym;
1903
      return oldsym;
1904
    }
1905
}
1906
 
1907
// Define a symbol based on an Output_segment.
1908
 
1909
Symbol*
1910
Symbol_table::define_in_output_segment(const char* name,
1911
                                       const char* version,
1912
                                       Defined defined,
1913
                                       Output_segment* os,
1914
                                       uint64_t value,
1915
                                       uint64_t symsize,
1916
                                       elfcpp::STT type,
1917
                                       elfcpp::STB binding,
1918
                                       elfcpp::STV visibility,
1919
                                       unsigned char nonvis,
1920
                                       Symbol::Segment_offset_base offset_base,
1921
                                       bool only_if_ref)
1922
{
1923
  if (parameters->target().get_size() == 32)
1924
    {
1925
#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1926
      return this->do_define_in_output_segment<32>(name, version, defined, os,
1927
                                                   value, symsize, type,
1928
                                                   binding, visibility, nonvis,
1929
                                                   offset_base, only_if_ref);
1930
#else
1931
      gold_unreachable();
1932
#endif
1933
    }
1934
  else if (parameters->target().get_size() == 64)
1935
    {
1936
#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1937
      return this->do_define_in_output_segment<64>(name, version, defined, os,
1938
                                                   value, symsize, type,
1939
                                                   binding, visibility, nonvis,
1940
                                                   offset_base, only_if_ref);
1941
#else
1942
      gold_unreachable();
1943
#endif
1944
    }
1945
  else
1946
    gold_unreachable();
1947
}
1948
 
1949
// Define a symbol in an Output_segment, sized version.
1950
 
1951
template<int size>
1952
Sized_symbol<size>*
1953
Symbol_table::do_define_in_output_segment(
1954
    const char* name,
1955
    const char* version,
1956
    Defined defined,
1957
    Output_segment* os,
1958
    typename elfcpp::Elf_types<size>::Elf_Addr value,
1959
    typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1960
    elfcpp::STT type,
1961
    elfcpp::STB binding,
1962
    elfcpp::STV visibility,
1963
    unsigned char nonvis,
1964
    Symbol::Segment_offset_base offset_base,
1965
    bool only_if_ref)
1966
{
1967
  Sized_symbol<size>* sym;
1968
  Sized_symbol<size>* oldsym;
1969
  bool resolve_oldsym;
1970
 
1971
  if (parameters->target().is_big_endian())
1972
    {
1973
#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1974
      sym = this->define_special_symbol<size, true>(&name, &version,
1975
                                                    only_if_ref, &oldsym,
1976
                                                    &resolve_oldsym);
1977
#else
1978
      gold_unreachable();
1979
#endif
1980
    }
1981
  else
1982
    {
1983
#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1984
      sym = this->define_special_symbol<size, false>(&name, &version,
1985
                                                     only_if_ref, &oldsym,
1986
                                                     &resolve_oldsym);
1987
#else
1988
      gold_unreachable();
1989
#endif
1990
    }
1991
 
1992
  if (sym == NULL)
1993
    return NULL;
1994
 
1995
  sym->init_output_segment(name, version, os, value, symsize, type, binding,
1996 148 khays
                           visibility, nonvis, offset_base,
1997
                           defined == PREDEFINED);
1998 27 khays
 
1999
  if (oldsym == NULL)
2000
    {
2001
      if (binding == elfcpp::STB_LOCAL
2002
          || this->version_script_.symbol_is_local(name))
2003
        this->force_local(sym);
2004
      else if (version != NULL)
2005
        sym->set_is_default();
2006
      return sym;
2007
    }
2008
 
2009 159 khays
  if (Symbol_table::should_override_with_special(oldsym, type, defined))
2010 27 khays
    this->override_with_special(oldsym, sym);
2011
 
2012
  if (resolve_oldsym)
2013
    return sym;
2014
  else
2015
    {
2016
      delete sym;
2017
      return oldsym;
2018
    }
2019
}
2020
 
2021
// Define a special symbol with a constant value.  It is a multiple
2022
// definition error if this symbol is already defined.
2023
 
2024
Symbol*
2025
Symbol_table::define_as_constant(const char* name,
2026
                                 const char* version,
2027
                                 Defined defined,
2028
                                 uint64_t value,
2029
                                 uint64_t symsize,
2030
                                 elfcpp::STT type,
2031
                                 elfcpp::STB binding,
2032
                                 elfcpp::STV visibility,
2033
                                 unsigned char nonvis,
2034
                                 bool only_if_ref,
2035
                                 bool force_override)
2036
{
2037
  if (parameters->target().get_size() == 32)
2038
    {
2039
#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2040
      return this->do_define_as_constant<32>(name, version, defined, value,
2041
                                             symsize, type, binding,
2042
                                             visibility, nonvis, only_if_ref,
2043
                                             force_override);
2044
#else
2045
      gold_unreachable();
2046
#endif
2047
    }
2048
  else if (parameters->target().get_size() == 64)
2049
    {
2050
#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2051
      return this->do_define_as_constant<64>(name, version, defined, value,
2052
                                             symsize, type, binding,
2053
                                             visibility, nonvis, only_if_ref,
2054
                                             force_override);
2055
#else
2056
      gold_unreachable();
2057
#endif
2058
    }
2059
  else
2060
    gold_unreachable();
2061
}
2062
 
2063
// Define a symbol as a constant, sized version.
2064
 
2065
template<int size>
2066
Sized_symbol<size>*
2067
Symbol_table::do_define_as_constant(
2068
    const char* name,
2069
    const char* version,
2070
    Defined defined,
2071
    typename elfcpp::Elf_types<size>::Elf_Addr value,
2072
    typename elfcpp::Elf_types<size>::Elf_WXword symsize,
2073
    elfcpp::STT type,
2074
    elfcpp::STB binding,
2075
    elfcpp::STV visibility,
2076
    unsigned char nonvis,
2077
    bool only_if_ref,
2078
    bool force_override)
2079
{
2080
  Sized_symbol<size>* sym;
2081
  Sized_symbol<size>* oldsym;
2082
  bool resolve_oldsym;
2083
 
2084
  if (parameters->target().is_big_endian())
2085
    {
2086
#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
2087
      sym = this->define_special_symbol<size, true>(&name, &version,
2088
                                                    only_if_ref, &oldsym,
2089
                                                    &resolve_oldsym);
2090
#else
2091
      gold_unreachable();
2092
#endif
2093
    }
2094
  else
2095
    {
2096
#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
2097
      sym = this->define_special_symbol<size, false>(&name, &version,
2098
                                                     only_if_ref, &oldsym,
2099
                                                     &resolve_oldsym);
2100
#else
2101
      gold_unreachable();
2102
#endif
2103
    }
2104
 
2105
  if (sym == NULL)
2106
    return NULL;
2107
 
2108
  sym->init_constant(name, version, value, symsize, type, binding, visibility,
2109 148 khays
                     nonvis, defined == PREDEFINED);
2110 27 khays
 
2111
  if (oldsym == NULL)
2112
    {
2113
      // Version symbols are absolute symbols with name == version.
2114
      // We don't want to force them to be local.
2115
      if ((version == NULL
2116
           || name != version
2117
           || value != 0)
2118
          && (binding == elfcpp::STB_LOCAL
2119
              || this->version_script_.symbol_is_local(name)))
2120
        this->force_local(sym);
2121
      else if (version != NULL
2122
               && (name != version || value != 0))
2123
        sym->set_is_default();
2124
      return sym;
2125
    }
2126
 
2127
  if (force_override
2128 159 khays
      || Symbol_table::should_override_with_special(oldsym, type, defined))
2129 27 khays
    this->override_with_special(oldsym, sym);
2130
 
2131
  if (resolve_oldsym)
2132
    return sym;
2133
  else
2134
    {
2135
      delete sym;
2136
      return oldsym;
2137
    }
2138
}
2139
 
2140
// Define a set of symbols in output sections.
2141
 
2142
void
2143
Symbol_table::define_symbols(const Layout* layout, int count,
2144
                             const Define_symbol_in_section* p,
2145
                             bool only_if_ref)
2146
{
2147
  for (int i = 0; i < count; ++i, ++p)
2148
    {
2149
      Output_section* os = layout->find_output_section(p->output_section);
2150
      if (os != NULL)
2151
        this->define_in_output_data(p->name, NULL, PREDEFINED, os, p->value,
2152
                                    p->size, p->type, p->binding,
2153
                                    p->visibility, p->nonvis,
2154
                                    p->offset_is_from_end,
2155
                                    only_if_ref || p->only_if_ref);
2156
      else
2157
        this->define_as_constant(p->name, NULL, PREDEFINED, 0, p->size,
2158
                                 p->type, p->binding, p->visibility, p->nonvis,
2159
                                 only_if_ref || p->only_if_ref,
2160
                                 false);
2161
    }
2162
}
2163
 
2164
// Define a set of symbols in output segments.
2165
 
2166
void
2167
Symbol_table::define_symbols(const Layout* layout, int count,
2168
                             const Define_symbol_in_segment* p,
2169
                             bool only_if_ref)
2170
{
2171
  for (int i = 0; i < count; ++i, ++p)
2172
    {
2173
      Output_segment* os = layout->find_output_segment(p->segment_type,
2174
                                                       p->segment_flags_set,
2175
                                                       p->segment_flags_clear);
2176
      if (os != NULL)
2177
        this->define_in_output_segment(p->name, NULL, PREDEFINED, os, p->value,
2178
                                       p->size, p->type, p->binding,
2179
                                       p->visibility, p->nonvis,
2180
                                       p->offset_base,
2181
                                       only_if_ref || p->only_if_ref);
2182
      else
2183
        this->define_as_constant(p->name, NULL, PREDEFINED, 0, p->size,
2184
                                 p->type, p->binding, p->visibility, p->nonvis,
2185
                                 only_if_ref || p->only_if_ref,
2186
                                 false);
2187
    }
2188
}
2189
 
2190
// Define CSYM using a COPY reloc.  POSD is the Output_data where the
2191
// symbol should be defined--typically a .dyn.bss section.  VALUE is
2192
// the offset within POSD.
2193
 
2194
template<int size>
2195
void
2196
Symbol_table::define_with_copy_reloc(
2197
    Sized_symbol<size>* csym,
2198
    Output_data* posd,
2199
    typename elfcpp::Elf_types<size>::Elf_Addr value)
2200
{
2201
  gold_assert(csym->is_from_dynobj());
2202
  gold_assert(!csym->is_copied_from_dynobj());
2203
  Object* object = csym->object();
2204
  gold_assert(object->is_dynamic());
2205
  Dynobj* dynobj = static_cast<Dynobj*>(object);
2206
 
2207
  // Our copied variable has to override any variable in a shared
2208
  // library.
2209
  elfcpp::STB binding = csym->binding();
2210
  if (binding == elfcpp::STB_WEAK)
2211
    binding = elfcpp::STB_GLOBAL;
2212
 
2213
  this->define_in_output_data(csym->name(), csym->version(), COPY,
2214
                              posd, value, csym->symsize(),
2215
                              csym->type(), binding,
2216
                              csym->visibility(), csym->nonvis(),
2217
                              false, false);
2218
 
2219
  csym->set_is_copied_from_dynobj();
2220
  csym->set_needs_dynsym_entry();
2221
 
2222
  this->copied_symbol_dynobjs_[csym] = dynobj;
2223
 
2224
  // We have now defined all aliases, but we have not entered them all
2225
  // in the copied_symbol_dynobjs_ map.
2226
  if (csym->has_alias())
2227
    {
2228
      Symbol* sym = csym;
2229
      while (true)
2230
        {
2231
          sym = this->weak_aliases_[sym];
2232
          if (sym == csym)
2233
            break;
2234
          gold_assert(sym->output_data() == posd);
2235
 
2236
          sym->set_is_copied_from_dynobj();
2237
          this->copied_symbol_dynobjs_[sym] = dynobj;
2238
        }
2239
    }
2240
}
2241
 
2242
// SYM is defined using a COPY reloc.  Return the dynamic object where
2243
// the original definition was found.
2244
 
2245
Dynobj*
2246
Symbol_table::get_copy_source(const Symbol* sym) const
2247
{
2248
  gold_assert(sym->is_copied_from_dynobj());
2249
  Copied_symbol_dynobjs::const_iterator p =
2250
    this->copied_symbol_dynobjs_.find(sym);
2251
  gold_assert(p != this->copied_symbol_dynobjs_.end());
2252
  return p->second;
2253
}
2254
 
2255
// Add any undefined symbols named on the command line.
2256
 
2257
void
2258
Symbol_table::add_undefined_symbols_from_command_line(Layout* layout)
2259
{
2260
  if (parameters->options().any_undefined()
2261
      || layout->script_options()->any_unreferenced())
2262
    {
2263
      if (parameters->target().get_size() == 32)
2264
        {
2265
#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2266
          this->do_add_undefined_symbols_from_command_line<32>(layout);
2267
#else
2268
          gold_unreachable();
2269
#endif
2270
        }
2271
      else if (parameters->target().get_size() == 64)
2272
        {
2273
#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2274
          this->do_add_undefined_symbols_from_command_line<64>(layout);
2275
#else
2276
          gold_unreachable();
2277
#endif
2278
        }
2279
      else
2280
        gold_unreachable();
2281
    }
2282
}
2283
 
2284
template<int size>
2285
void
2286
Symbol_table::do_add_undefined_symbols_from_command_line(Layout* layout)
2287
{
2288
  for (options::String_set::const_iterator p =
2289
         parameters->options().undefined_begin();
2290
       p != parameters->options().undefined_end();
2291
       ++p)
2292
    this->add_undefined_symbol_from_command_line<size>(p->c_str());
2293
 
2294
  for (Script_options::referenced_const_iterator p =
2295
         layout->script_options()->referenced_begin();
2296
       p != layout->script_options()->referenced_end();
2297
       ++p)
2298
    this->add_undefined_symbol_from_command_line<size>(p->c_str());
2299
}
2300
 
2301
template<int size>
2302
void
2303
Symbol_table::add_undefined_symbol_from_command_line(const char* name)
2304
{
2305
  if (this->lookup(name) != NULL)
2306
    return;
2307
 
2308
  const char* version = NULL;
2309
 
2310
  Sized_symbol<size>* sym;
2311
  Sized_symbol<size>* oldsym;
2312
  bool resolve_oldsym;
2313
  if (parameters->target().is_big_endian())
2314
    {
2315
#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
2316
      sym = this->define_special_symbol<size, true>(&name, &version,
2317
                                                    false, &oldsym,
2318
                                                    &resolve_oldsym);
2319
#else
2320
      gold_unreachable();
2321
#endif
2322
    }
2323
  else
2324
    {
2325
#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
2326
      sym = this->define_special_symbol<size, false>(&name, &version,
2327
                                                     false, &oldsym,
2328
                                                     &resolve_oldsym);
2329
#else
2330
      gold_unreachable();
2331
#endif
2332
    }
2333
 
2334
  gold_assert(oldsym == NULL);
2335
 
2336
  sym->init_undefined(name, version, elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
2337
                      elfcpp::STV_DEFAULT, 0);
2338
  ++this->saw_undefined_;
2339
}
2340
 
2341
// Set the dynamic symbol indexes.  INDEX is the index of the first
2342
// global dynamic symbol.  Pointers to the symbols are stored into the
2343
// vector SYMS.  The names are added to DYNPOOL.  This returns an
2344
// updated dynamic symbol index.
2345
 
2346
unsigned int
2347
Symbol_table::set_dynsym_indexes(unsigned int index,
2348
                                 std::vector<Symbol*>* syms,
2349
                                 Stringpool* dynpool,
2350
                                 Versions* versions)
2351
{
2352
  for (Symbol_table_type::iterator p = this->table_.begin();
2353
       p != this->table_.end();
2354
       ++p)
2355
    {
2356
      Symbol* sym = p->second;
2357
 
2358
      // Note that SYM may already have a dynamic symbol index, since
2359
      // some symbols appear more than once in the symbol table, with
2360
      // and without a version.
2361
 
2362
      if (!sym->should_add_dynsym_entry(this))
2363
        sym->set_dynsym_index(-1U);
2364
      else if (!sym->has_dynsym_index())
2365
        {
2366
          sym->set_dynsym_index(index);
2367
          ++index;
2368
          syms->push_back(sym);
2369
          dynpool->add(sym->name(), false, NULL);
2370
 
2371
          // Record any version information.
2372
          if (sym->version() != NULL)
2373
            versions->record_version(this, dynpool, sym);
2374
 
2375
          // If the symbol is defined in a dynamic object and is
2376
          // referenced in a regular object, then mark the dynamic
2377
          // object as needed.  This is used to implement --as-needed.
2378
          if (sym->is_from_dynobj() && sym->in_reg())
2379
            sym->object()->set_is_needed();
2380
        }
2381
    }
2382
 
2383
  // Finish up the versions.  In some cases this may add new dynamic
2384
  // symbols.
2385
  index = versions->finalize(this, index, syms);
2386
 
2387
  return index;
2388
}
2389
 
2390
// Set the final values for all the symbols.  The index of the first
2391
// global symbol in the output file is *PLOCAL_SYMCOUNT.  Record the
2392
// file offset OFF.  Add their names to POOL.  Return the new file
2393
// offset.  Update *PLOCAL_SYMCOUNT if necessary.
2394
 
2395
off_t
2396
Symbol_table::finalize(off_t off, off_t dynoff, size_t dyn_global_index,
2397
                       size_t dyncount, Stringpool* pool,
2398
                       unsigned int* plocal_symcount)
2399
{
2400
  off_t ret;
2401
 
2402
  gold_assert(*plocal_symcount != 0);
2403
  this->first_global_index_ = *plocal_symcount;
2404
 
2405
  this->dynamic_offset_ = dynoff;
2406
  this->first_dynamic_global_index_ = dyn_global_index;
2407
  this->dynamic_count_ = dyncount;
2408
 
2409
  if (parameters->target().get_size() == 32)
2410
    {
2411
#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
2412
      ret = this->sized_finalize<32>(off, pool, plocal_symcount);
2413
#else
2414
      gold_unreachable();
2415
#endif
2416
    }
2417
  else if (parameters->target().get_size() == 64)
2418
    {
2419
#if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
2420
      ret = this->sized_finalize<64>(off, pool, plocal_symcount);
2421
#else
2422
      gold_unreachable();
2423
#endif
2424
    }
2425
  else
2426
    gold_unreachable();
2427
 
2428
  // Now that we have the final symbol table, we can reliably note
2429
  // which symbols should get warnings.
2430
  this->warnings_.note_warnings(this);
2431
 
2432
  return ret;
2433
}
2434
 
2435
// SYM is going into the symbol table at *PINDEX.  Add the name to
2436
// POOL, update *PINDEX and *POFF.
2437
 
2438
template<int size>
2439
void
2440
Symbol_table::add_to_final_symtab(Symbol* sym, Stringpool* pool,
2441
                                  unsigned int* pindex, off_t* poff)
2442
{
2443
  sym->set_symtab_index(*pindex);
2444 159 khays
  if (sym->version() == NULL || !parameters->options().relocatable())
2445
    pool->add(sym->name(), false, NULL);
2446
  else
2447
    pool->add(sym->versioned_name(), true, NULL);
2448 27 khays
  ++*pindex;
2449
  *poff += elfcpp::Elf_sizes<size>::sym_size;
2450
}
2451
 
2452
// Set the final value for all the symbols.  This is called after
2453
// Layout::finalize, so all the output sections have their final
2454
// address.
2455
 
2456
template<int size>
2457
off_t
2458
Symbol_table::sized_finalize(off_t off, Stringpool* pool,
2459
                             unsigned int* plocal_symcount)
2460
{
2461
  off = align_address(off, size >> 3);
2462
  this->offset_ = off;
2463
 
2464
  unsigned int index = *plocal_symcount;
2465
  const unsigned int orig_index = index;
2466
 
2467
  // First do all the symbols which have been forced to be local, as
2468
  // they must appear before all global symbols.
2469
  for (Forced_locals::iterator p = this->forced_locals_.begin();
2470
       p != this->forced_locals_.end();
2471
       ++p)
2472
    {
2473
      Symbol* sym = *p;
2474
      gold_assert(sym->is_forced_local());
2475
      if (this->sized_finalize_symbol<size>(sym))
2476
        {
2477
          this->add_to_final_symtab<size>(sym, pool, &index, &off);
2478
          ++*plocal_symcount;
2479
        }
2480
    }
2481
 
2482
  // Now do all the remaining symbols.
2483
  for (Symbol_table_type::iterator p = this->table_.begin();
2484
       p != this->table_.end();
2485
       ++p)
2486
    {
2487
      Symbol* sym = p->second;
2488
      if (this->sized_finalize_symbol<size>(sym))
2489
        this->add_to_final_symtab<size>(sym, pool, &index, &off);
2490
    }
2491
 
2492
  this->output_count_ = index - orig_index;
2493
 
2494
  return off;
2495
}
2496
 
2497
// Compute the final value of SYM and store status in location PSTATUS.
2498
// During relaxation, this may be called multiple times for a symbol to
2499
// compute its would-be final value in each relaxation pass.
2500
 
2501
template<int size>
2502
typename Sized_symbol<size>::Value_type
2503
Symbol_table::compute_final_value(
2504
    const Sized_symbol<size>* sym,
2505
    Compute_final_value_status* pstatus) const
2506
{
2507
  typedef typename Sized_symbol<size>::Value_type Value_type;
2508
  Value_type value;
2509
 
2510
  switch (sym->source())
2511
    {
2512
    case Symbol::FROM_OBJECT:
2513
      {
2514
        bool is_ordinary;
2515
        unsigned int shndx = sym->shndx(&is_ordinary);
2516
 
2517
        if (!is_ordinary
2518
            && shndx != elfcpp::SHN_ABS
2519
            && !Symbol::is_common_shndx(shndx))
2520
          {
2521
            *pstatus = CFVS_UNSUPPORTED_SYMBOL_SECTION;
2522
            return 0;
2523
          }
2524
 
2525
        Object* symobj = sym->object();
2526
        if (symobj->is_dynamic())
2527
          {
2528
            value = 0;
2529
            shndx = elfcpp::SHN_UNDEF;
2530
          }
2531
        else if (symobj->pluginobj() != NULL)
2532
          {
2533
            value = 0;
2534
            shndx = elfcpp::SHN_UNDEF;
2535
          }
2536
        else if (shndx == elfcpp::SHN_UNDEF)
2537
          value = 0;
2538
        else if (!is_ordinary
2539
                 && (shndx == elfcpp::SHN_ABS
2540
                     || Symbol::is_common_shndx(shndx)))
2541
          value = sym->value();
2542
        else
2543
          {
2544
            Relobj* relobj = static_cast<Relobj*>(symobj);
2545
            Output_section* os = relobj->output_section(shndx);
2546
 
2547
            if (this->is_section_folded(relobj, shndx))
2548
              {
2549
                gold_assert(os == NULL);
2550
                // Get the os of the section it is folded onto.
2551
                Section_id folded = this->icf_->get_folded_section(relobj,
2552
                                                                   shndx);
2553
                gold_assert(folded.first != NULL);
2554
                Relobj* folded_obj = reinterpret_cast<Relobj*>(folded.first);
2555
                unsigned folded_shndx = folded.second;
2556
 
2557
                os = folded_obj->output_section(folded_shndx);
2558
                gold_assert(os != NULL);
2559
 
2560
                // Replace (relobj, shndx) with canonical ICF input section.
2561
                shndx = folded_shndx;
2562
                relobj = folded_obj;
2563
              }
2564
 
2565
            uint64_t secoff64 = relobj->output_section_offset(shndx);
2566
            if (os == NULL)
2567
              {
2568
                bool static_or_reloc = (parameters->doing_static_link() ||
2569
                                        parameters->options().relocatable());
2570
                gold_assert(static_or_reloc || sym->dynsym_index() == -1U);
2571
 
2572
                *pstatus = CFVS_NO_OUTPUT_SECTION;
2573
                return 0;
2574
              }
2575
 
2576
            if (secoff64 == -1ULL)
2577
              {
2578
                // The section needs special handling (e.g., a merge section).
2579
 
2580
                value = os->output_address(relobj, shndx, sym->value());
2581
              }
2582
            else
2583
              {
2584
                Value_type secoff =
2585
                  convert_types<Value_type, uint64_t>(secoff64);
2586
                if (sym->type() == elfcpp::STT_TLS)
2587
                  value = sym->value() + os->tls_offset() + secoff;
2588
                else
2589
                  value = sym->value() + os->address() + secoff;
2590
              }
2591
          }
2592
      }
2593
      break;
2594
 
2595
    case Symbol::IN_OUTPUT_DATA:
2596
      {
2597
        Output_data* od = sym->output_data();
2598
        value = sym->value();
2599
        if (sym->type() != elfcpp::STT_TLS)
2600
          value += od->address();
2601
        else
2602
          {
2603
            Output_section* os = od->output_section();
2604
            gold_assert(os != NULL);
2605
            value += os->tls_offset() + (od->address() - os->address());
2606
          }
2607
        if (sym->offset_is_from_end())
2608
          value += od->data_size();
2609
      }
2610
      break;
2611
 
2612
    case Symbol::IN_OUTPUT_SEGMENT:
2613
      {
2614
        Output_segment* os = sym->output_segment();
2615
        value = sym->value();
2616
        if (sym->type() != elfcpp::STT_TLS)
2617
          value += os->vaddr();
2618
        switch (sym->offset_base())
2619
          {
2620
          case Symbol::SEGMENT_START:
2621
            break;
2622
          case Symbol::SEGMENT_END:
2623
            value += os->memsz();
2624
            break;
2625
          case Symbol::SEGMENT_BSS:
2626
            value += os->filesz();
2627
            break;
2628
          default:
2629
            gold_unreachable();
2630
          }
2631
      }
2632
      break;
2633
 
2634
    case Symbol::IS_CONSTANT:
2635
      value = sym->value();
2636
      break;
2637
 
2638
    case Symbol::IS_UNDEFINED:
2639
      value = 0;
2640
      break;
2641
 
2642
    default:
2643
      gold_unreachable();
2644
    }
2645
 
2646
  *pstatus = CFVS_OK;
2647
  return value;
2648
}
2649
 
2650
// Finalize the symbol SYM.  This returns true if the symbol should be
2651
// added to the symbol table, false otherwise.
2652
 
2653
template<int size>
2654
bool
2655
Symbol_table::sized_finalize_symbol(Symbol* unsized_sym)
2656
{
2657
  typedef typename Sized_symbol<size>::Value_type Value_type;
2658
 
2659
  Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(unsized_sym);
2660
 
2661
  // The default version of a symbol may appear twice in the symbol
2662
  // table.  We only need to finalize it once.
2663
  if (sym->has_symtab_index())
2664
    return false;
2665
 
2666
  if (!sym->in_reg())
2667
    {
2668
      gold_assert(!sym->has_symtab_index());
2669
      sym->set_symtab_index(-1U);
2670
      gold_assert(sym->dynsym_index() == -1U);
2671
      return false;
2672
    }
2673
 
2674
  // If the symbol is only present on plugin files, the plugin decided we
2675
  // don't need it.
2676
  if (!sym->in_real_elf())
2677
    {
2678
      gold_assert(!sym->has_symtab_index());
2679
      sym->set_symtab_index(-1U);
2680
      return false;
2681
    }
2682
 
2683
  // Compute final symbol value.
2684
  Compute_final_value_status status;
2685
  Value_type value = this->compute_final_value(sym, &status);
2686
 
2687
  switch (status)
2688
    {
2689
    case CFVS_OK:
2690
      break;
2691
    case CFVS_UNSUPPORTED_SYMBOL_SECTION:
2692
      {
2693
        bool is_ordinary;
2694
        unsigned int shndx = sym->shndx(&is_ordinary);
2695
        gold_error(_("%s: unsupported symbol section 0x%x"),
2696
                   sym->demangled_name().c_str(), shndx);
2697
      }
2698
      break;
2699
    case CFVS_NO_OUTPUT_SECTION:
2700
      sym->set_symtab_index(-1U);
2701
      return false;
2702
    default:
2703
      gold_unreachable();
2704
    }
2705
 
2706
  sym->set_value(value);
2707
 
2708
  if (parameters->options().strip_all()
2709
      || !parameters->options().should_retain_symbol(sym->name()))
2710
    {
2711
      sym->set_symtab_index(-1U);
2712
      return false;
2713
    }
2714
 
2715
  return true;
2716
}
2717
 
2718
// Write out the global symbols.
2719
 
2720
void
2721
Symbol_table::write_globals(const Stringpool* sympool,
2722
                            const Stringpool* dynpool,
2723
                            Output_symtab_xindex* symtab_xindex,
2724
                            Output_symtab_xindex* dynsym_xindex,
2725
                            Output_file* of) const
2726
{
2727
  switch (parameters->size_and_endianness())
2728
    {
2729
#ifdef HAVE_TARGET_32_LITTLE
2730
    case Parameters::TARGET_32_LITTLE:
2731
      this->sized_write_globals<32, false>(sympool, dynpool, symtab_xindex,
2732
                                           dynsym_xindex, of);
2733
      break;
2734
#endif
2735
#ifdef HAVE_TARGET_32_BIG
2736
    case Parameters::TARGET_32_BIG:
2737
      this->sized_write_globals<32, true>(sympool, dynpool, symtab_xindex,
2738
                                          dynsym_xindex, of);
2739
      break;
2740
#endif
2741
#ifdef HAVE_TARGET_64_LITTLE
2742
    case Parameters::TARGET_64_LITTLE:
2743
      this->sized_write_globals<64, false>(sympool, dynpool, symtab_xindex,
2744
                                           dynsym_xindex, of);
2745
      break;
2746
#endif
2747
#ifdef HAVE_TARGET_64_BIG
2748
    case Parameters::TARGET_64_BIG:
2749
      this->sized_write_globals<64, true>(sympool, dynpool, symtab_xindex,
2750
                                          dynsym_xindex, of);
2751
      break;
2752
#endif
2753
    default:
2754
      gold_unreachable();
2755
    }
2756
}
2757
 
2758
// Write out the global symbols.
2759
 
2760
template<int size, bool big_endian>
2761
void
2762
Symbol_table::sized_write_globals(const Stringpool* sympool,
2763
                                  const Stringpool* dynpool,
2764
                                  Output_symtab_xindex* symtab_xindex,
2765
                                  Output_symtab_xindex* dynsym_xindex,
2766
                                  Output_file* of) const
2767
{
2768
  const Target& target = parameters->target();
2769
 
2770
  const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2771
 
2772
  const unsigned int output_count = this->output_count_;
2773
  const section_size_type oview_size = output_count * sym_size;
2774
  const unsigned int first_global_index = this->first_global_index_;
2775
  unsigned char* psyms;
2776
  if (this->offset_ == 0 || output_count == 0)
2777
    psyms = NULL;
2778
  else
2779
    psyms = of->get_output_view(this->offset_, oview_size);
2780
 
2781
  const unsigned int dynamic_count = this->dynamic_count_;
2782
  const section_size_type dynamic_size = dynamic_count * sym_size;
2783
  const unsigned int first_dynamic_global_index =
2784
    this->first_dynamic_global_index_;
2785
  unsigned char* dynamic_view;
2786
  if (this->dynamic_offset_ == 0 || dynamic_count == 0)
2787
    dynamic_view = NULL;
2788
  else
2789
    dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
2790
 
2791
  for (Symbol_table_type::const_iterator p = this->table_.begin();
2792
       p != this->table_.end();
2793
       ++p)
2794
    {
2795
      Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
2796
 
2797
      // Possibly warn about unresolved symbols in shared libraries.
2798
      this->warn_about_undefined_dynobj_symbol(sym);
2799
 
2800
      unsigned int sym_index = sym->symtab_index();
2801
      unsigned int dynsym_index;
2802
      if (dynamic_view == NULL)
2803
        dynsym_index = -1U;
2804
      else
2805
        dynsym_index = sym->dynsym_index();
2806
 
2807
      if (sym_index == -1U && dynsym_index == -1U)
2808
        {
2809
          // This symbol is not included in the output file.
2810
          continue;
2811
        }
2812
 
2813
      unsigned int shndx;
2814
      typename elfcpp::Elf_types<size>::Elf_Addr sym_value = sym->value();
2815
      typename elfcpp::Elf_types<size>::Elf_Addr dynsym_value = sym_value;
2816
      elfcpp::STB binding = sym->binding();
2817 163 khays
 
2818
      // If --no-gnu-unique is set, change STB_GNU_UNIQUE to STB_GLOBAL.
2819
      if (binding == elfcpp::STB_GNU_UNIQUE
2820
          && !parameters->options().gnu_unique())
2821
        binding = elfcpp::STB_GLOBAL;
2822
 
2823 27 khays
      switch (sym->source())
2824
        {
2825
        case Symbol::FROM_OBJECT:
2826
          {
2827
            bool is_ordinary;
2828
            unsigned int in_shndx = sym->shndx(&is_ordinary);
2829
 
2830
            if (!is_ordinary
2831
                && in_shndx != elfcpp::SHN_ABS
2832
                && !Symbol::is_common_shndx(in_shndx))
2833
              {
2834
                gold_error(_("%s: unsupported symbol section 0x%x"),
2835
                           sym->demangled_name().c_str(), in_shndx);
2836
                shndx = in_shndx;
2837
              }
2838
            else
2839
              {
2840
                Object* symobj = sym->object();
2841
                if (symobj->is_dynamic())
2842
                  {
2843
                    if (sym->needs_dynsym_value())
2844
                      dynsym_value = target.dynsym_value(sym);
2845
                    shndx = elfcpp::SHN_UNDEF;
2846
                    if (sym->is_undef_binding_weak())
2847
                      binding = elfcpp::STB_WEAK;
2848
                    else
2849
                      binding = elfcpp::STB_GLOBAL;
2850
                  }
2851
                else if (symobj->pluginobj() != NULL)
2852
                  shndx = elfcpp::SHN_UNDEF;
2853
                else if (in_shndx == elfcpp::SHN_UNDEF
2854
                         || (!is_ordinary
2855
                             && (in_shndx == elfcpp::SHN_ABS
2856
                                 || Symbol::is_common_shndx(in_shndx))))
2857
                  shndx = in_shndx;
2858
                else
2859
                  {
2860
                    Relobj* relobj = static_cast<Relobj*>(symobj);
2861
                    Output_section* os = relobj->output_section(in_shndx);
2862
                    if (this->is_section_folded(relobj, in_shndx))
2863
                      {
2864
                        // This global symbol must be written out even though
2865
                        // it is folded.
2866
                        // Get the os of the section it is folded onto.
2867
                        Section_id folded =
2868
                             this->icf_->get_folded_section(relobj, in_shndx);
2869
                        gold_assert(folded.first !=NULL);
2870
                        Relobj* folded_obj =
2871
                          reinterpret_cast<Relobj*>(folded.first);
2872
                        os = folded_obj->output_section(folded.second);
2873
                        gold_assert(os != NULL);
2874
                      }
2875
                    gold_assert(os != NULL);
2876
                    shndx = os->out_shndx();
2877
 
2878
                    if (shndx >= elfcpp::SHN_LORESERVE)
2879
                      {
2880
                        if (sym_index != -1U)
2881
                          symtab_xindex->add(sym_index, shndx);
2882
                        if (dynsym_index != -1U)
2883
                          dynsym_xindex->add(dynsym_index, shndx);
2884
                        shndx = elfcpp::SHN_XINDEX;
2885
                      }
2886
 
2887
                    // In object files symbol values are section
2888
                    // relative.
2889
                    if (parameters->options().relocatable())
2890
                      sym_value -= os->address();
2891
                  }
2892
              }
2893
          }
2894
          break;
2895
 
2896
        case Symbol::IN_OUTPUT_DATA:
2897
          shndx = sym->output_data()->out_shndx();
2898
          if (shndx >= elfcpp::SHN_LORESERVE)
2899
            {
2900
              if (sym_index != -1U)
2901
                symtab_xindex->add(sym_index, shndx);
2902
              if (dynsym_index != -1U)
2903
                dynsym_xindex->add(dynsym_index, shndx);
2904
              shndx = elfcpp::SHN_XINDEX;
2905
            }
2906
          break;
2907
 
2908
        case Symbol::IN_OUTPUT_SEGMENT:
2909
          shndx = elfcpp::SHN_ABS;
2910
          break;
2911
 
2912
        case Symbol::IS_CONSTANT:
2913
          shndx = elfcpp::SHN_ABS;
2914
          break;
2915
 
2916
        case Symbol::IS_UNDEFINED:
2917
          shndx = elfcpp::SHN_UNDEF;
2918
          break;
2919
 
2920
        default:
2921
          gold_unreachable();
2922
        }
2923
 
2924
      if (sym_index != -1U)
2925
        {
2926
          sym_index -= first_global_index;
2927
          gold_assert(sym_index < output_count);
2928
          unsigned char* ps = psyms + (sym_index * sym_size);
2929
          this->sized_write_symbol<size, big_endian>(sym, sym_value, shndx,
2930
                                                     binding, sympool, ps);
2931
        }
2932
 
2933
      if (dynsym_index != -1U)
2934
        {
2935
          dynsym_index -= first_dynamic_global_index;
2936
          gold_assert(dynsym_index < dynamic_count);
2937
          unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
2938
          this->sized_write_symbol<size, big_endian>(sym, dynsym_value, shndx,
2939
                                                     binding, dynpool, pd);
2940
        }
2941
    }
2942
 
2943
  of->write_output_view(this->offset_, oview_size, psyms);
2944
  if (dynamic_view != NULL)
2945
    of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
2946
}
2947
 
2948
// Write out the symbol SYM, in section SHNDX, to P.  POOL is the
2949
// strtab holding the name.
2950
 
2951
template<int size, bool big_endian>
2952
void
2953
Symbol_table::sized_write_symbol(
2954
    Sized_symbol<size>* sym,
2955
    typename elfcpp::Elf_types<size>::Elf_Addr value,
2956
    unsigned int shndx,
2957
    elfcpp::STB binding,
2958
    const Stringpool* pool,
2959
    unsigned char* p) const
2960
{
2961
  elfcpp::Sym_write<size, big_endian> osym(p);
2962 159 khays
  if (sym->version() == NULL || !parameters->options().relocatable())
2963
    osym.put_st_name(pool->get_offset(sym->name()));
2964
  else
2965
    osym.put_st_name(pool->get_offset(sym->versioned_name()));
2966 27 khays
  osym.put_st_value(value);
2967
  // Use a symbol size of zero for undefined symbols from shared libraries.
2968
  if (shndx == elfcpp::SHN_UNDEF && sym->is_from_dynobj())
2969
    osym.put_st_size(0);
2970
  else
2971
    osym.put_st_size(sym->symsize());
2972
  elfcpp::STT type = sym->type();
2973
  // Turn IFUNC symbols from shared libraries into normal FUNC symbols.
2974
  if (type == elfcpp::STT_GNU_IFUNC
2975
      && sym->is_from_dynobj())
2976
    type = elfcpp::STT_FUNC;
2977
  // A version script may have overridden the default binding.
2978
  if (sym->is_forced_local())
2979
    osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL, type));
2980
  else
2981
    osym.put_st_info(elfcpp::elf_st_info(binding, type));
2982
  osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
2983
  osym.put_st_shndx(shndx);
2984
}
2985
 
2986
// Check for unresolved symbols in shared libraries.  This is
2987
// controlled by the --allow-shlib-undefined option.
2988
 
2989
// We only warn about libraries for which we have seen all the
2990
// DT_NEEDED entries.  We don't try to track down DT_NEEDED entries
2991
// which were not seen in this link.  If we didn't see a DT_NEEDED
2992
// entry, we aren't going to be able to reliably report whether the
2993
// symbol is undefined.
2994
 
2995
// We also don't warn about libraries found in a system library
2996
// directory (e.g., /lib or /usr/lib); we assume that those libraries
2997
// are OK.  This heuristic avoids problems on GNU/Linux, in which -ldl
2998
// can have undefined references satisfied by ld-linux.so.
2999
 
3000
inline void
3001
Symbol_table::warn_about_undefined_dynobj_symbol(Symbol* sym) const
3002
{
3003
  bool dummy;
3004
  if (sym->source() == Symbol::FROM_OBJECT
3005
      && sym->object()->is_dynamic()
3006
      && sym->shndx(&dummy) == elfcpp::SHN_UNDEF
3007
      && sym->binding() != elfcpp::STB_WEAK
3008
      && !parameters->options().allow_shlib_undefined()
3009
      && !parameters->target().is_defined_by_abi(sym)
3010
      && !sym->object()->is_in_system_directory())
3011
    {
3012
      // A very ugly cast.
3013
      Dynobj* dynobj = static_cast<Dynobj*>(sym->object());
3014
      if (!dynobj->has_unknown_needed_entries())
3015
        gold_undefined_symbol(sym);
3016
    }
3017
}
3018
 
3019
// Write out a section symbol.  Return the update offset.
3020
 
3021
void
3022
Symbol_table::write_section_symbol(const Output_section* os,
3023
                                   Output_symtab_xindex* symtab_xindex,
3024
                                   Output_file* of,
3025
                                   off_t offset) const
3026
{
3027
  switch (parameters->size_and_endianness())
3028
    {
3029
#ifdef HAVE_TARGET_32_LITTLE
3030
    case Parameters::TARGET_32_LITTLE:
3031
      this->sized_write_section_symbol<32, false>(os, symtab_xindex, of,
3032
                                                  offset);
3033
      break;
3034
#endif
3035
#ifdef HAVE_TARGET_32_BIG
3036
    case Parameters::TARGET_32_BIG:
3037
      this->sized_write_section_symbol<32, true>(os, symtab_xindex, of,
3038
                                                 offset);
3039
      break;
3040
#endif
3041
#ifdef HAVE_TARGET_64_LITTLE
3042
    case Parameters::TARGET_64_LITTLE:
3043
      this->sized_write_section_symbol<64, false>(os, symtab_xindex, of,
3044
                                                  offset);
3045
      break;
3046
#endif
3047
#ifdef HAVE_TARGET_64_BIG
3048
    case Parameters::TARGET_64_BIG:
3049
      this->sized_write_section_symbol<64, true>(os, symtab_xindex, of,
3050
                                                 offset);
3051
      break;
3052
#endif
3053
    default:
3054
      gold_unreachable();
3055
    }
3056
}
3057
 
3058
// Write out a section symbol, specialized for size and endianness.
3059
 
3060
template<int size, bool big_endian>
3061
void
3062
Symbol_table::sized_write_section_symbol(const Output_section* os,
3063
                                         Output_symtab_xindex* symtab_xindex,
3064
                                         Output_file* of,
3065
                                         off_t offset) const
3066
{
3067
  const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
3068
 
3069
  unsigned char* pov = of->get_output_view(offset, sym_size);
3070
 
3071
  elfcpp::Sym_write<size, big_endian> osym(pov);
3072
  osym.put_st_name(0);
3073
  if (parameters->options().relocatable())
3074
    osym.put_st_value(0);
3075
  else
3076
    osym.put_st_value(os->address());
3077
  osym.put_st_size(0);
3078
  osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
3079
                                       elfcpp::STT_SECTION));
3080
  osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
3081
 
3082
  unsigned int shndx = os->out_shndx();
3083
  if (shndx >= elfcpp::SHN_LORESERVE)
3084
    {
3085
      symtab_xindex->add(os->symtab_index(), shndx);
3086
      shndx = elfcpp::SHN_XINDEX;
3087
    }
3088
  osym.put_st_shndx(shndx);
3089
 
3090
  of->write_output_view(offset, sym_size, pov);
3091
}
3092
 
3093
// Print statistical information to stderr.  This is used for --stats.
3094
 
3095
void
3096
Symbol_table::print_stats() const
3097
{
3098
#if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
3099
  fprintf(stderr, _("%s: symbol table entries: %zu; buckets: %zu\n"),
3100
          program_name, this->table_.size(), this->table_.bucket_count());
3101
#else
3102
  fprintf(stderr, _("%s: symbol table entries: %zu\n"),
3103
          program_name, this->table_.size());
3104
#endif
3105
  this->namepool_.print_stats("symbol table stringpool");
3106
}
3107
 
3108
// We check for ODR violations by looking for symbols with the same
3109
// name for which the debugging information reports that they were
3110
// defined in disjoint source locations.  When comparing the source
3111
// location, we consider instances with the same base filename to be
3112
// the same.  This is because different object files/shared libraries
3113
// can include the same header file using different paths, and
3114
// different optimization settings can make the line number appear to
3115
// be a couple lines off, and we don't want to report an ODR violation
3116
// in those cases.
3117
 
3118
// This struct is used to compare line information, as returned by
3119
// Dwarf_line_info::one_addr2line.  It implements a < comparison
3120
// operator used with std::sort.
3121
 
3122
struct Odr_violation_compare
3123
{
3124
  bool
3125
  operator()(const std::string& s1, const std::string& s2) const
3126
  {
3127
    // Inputs should be of the form "dirname/filename:linenum" where
3128
    // "dirname/" is optional.  We want to compare just the filename:linenum.
3129
 
3130
    // Find the last '/' in each string.
3131
    std::string::size_type s1begin = s1.rfind('/');
3132
    std::string::size_type s2begin = s2.rfind('/');
3133
    // If there was no '/' in a string, start at the beginning.
3134
    if (s1begin == std::string::npos)
3135
      s1begin = 0;
3136
    if (s2begin == std::string::npos)
3137
      s2begin = 0;
3138
    return s1.compare(s1begin, std::string::npos,
3139
                      s2, s2begin, std::string::npos) < 0;
3140
  }
3141
};
3142
 
3143
// Returns all of the lines attached to LOC, not just the one the
3144
// instruction actually came from.
3145
std::vector<std::string>
3146
Symbol_table::linenos_from_loc(const Task* task,
3147
                               const Symbol_location& loc)
3148
{
3149
  // We need to lock the object in order to read it.  This
3150
  // means that we have to run in a singleton Task.  If we
3151
  // want to run this in a general Task for better
3152
  // performance, we will need one Task for object, plus
3153
  // appropriate locking to ensure that we don't conflict with
3154
  // other uses of the object.  Also note, one_addr2line is not
3155
  // currently thread-safe.
3156
  Task_lock_obj<Object> tl(task, loc.object);
3157
 
3158
  std::vector<std::string> result;
3159
  // 16 is the size of the object-cache that one_addr2line should use.
3160
  std::string canonical_result = Dwarf_line_info::one_addr2line(
3161
      loc.object, loc.shndx, loc.offset, 16, &result);
3162
  if (!canonical_result.empty())
3163
    result.push_back(canonical_result);
3164
  return result;
3165
}
3166
 
3167
// OutputIterator that records if it was ever assigned to.  This
3168
// allows it to be used with std::set_intersection() to check for
3169
// intersection rather than computing the intersection.
3170
struct Check_intersection
3171
{
3172
  Check_intersection()
3173
    : value_(false)
3174
  {}
3175
 
3176
  bool had_intersection() const
3177
  { return this->value_; }
3178
 
3179
  Check_intersection& operator++()
3180
  { return *this; }
3181
 
3182
  Check_intersection& operator*()
3183
  { return *this; }
3184
 
3185
  template<typename T>
3186
  Check_intersection& operator=(const T&)
3187
  {
3188
    this->value_ = true;
3189
    return *this;
3190
  }
3191
 
3192
 private:
3193
  bool value_;
3194
};
3195
 
3196
// Check candidate_odr_violations_ to find symbols with the same name
3197
// but apparently different definitions (different source-file/line-no
3198
// for each line assigned to the first instruction).
3199
 
3200
void
3201
Symbol_table::detect_odr_violations(const Task* task,
3202
                                    const char* output_file_name) const
3203
{
3204
  for (Odr_map::const_iterator it = candidate_odr_violations_.begin();
3205
       it != candidate_odr_violations_.end();
3206
       ++it)
3207
    {
3208
      const char* const symbol_name = it->first;
3209
 
3210
      std::string first_object_name;
3211
      std::vector<std::string> first_object_linenos;
3212
 
3213
      Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator
3214
          locs = it->second.begin();
3215
      const Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator
3216
          locs_end = it->second.end();
3217
      for (; locs != locs_end && first_object_linenos.empty(); ++locs)
3218
        {
3219
          // Save the line numbers from the first definition to
3220
          // compare to the other definitions.  Ideally, we'd compare
3221
          // every definition to every other, but we don't want to
3222
          // take O(N^2) time to do this.  This shortcut may cause
3223
          // false negatives that appear or disappear depending on the
3224
          // link order, but it won't cause false positives.
3225
          first_object_name = locs->object->name();
3226
          first_object_linenos = this->linenos_from_loc(task, *locs);
3227
        }
3228
 
3229
      // Sort by Odr_violation_compare to make std::set_intersection work.
3230
      std::sort(first_object_linenos.begin(), first_object_linenos.end(),
3231
                Odr_violation_compare());
3232
 
3233
      for (; locs != locs_end; ++locs)
3234
        {
3235
          std::vector<std::string> linenos =
3236
              this->linenos_from_loc(task, *locs);
3237
          // linenos will be empty if we couldn't parse the debug info.
3238
          if (linenos.empty())
3239
            continue;
3240
          // Sort by Odr_violation_compare to make std::set_intersection work.
3241
          std::sort(linenos.begin(), linenos.end(), Odr_violation_compare());
3242
 
3243
          Check_intersection intersection_result =
3244
              std::set_intersection(first_object_linenos.begin(),
3245
                                    first_object_linenos.end(),
3246
                                    linenos.begin(),
3247
                                    linenos.end(),
3248
                                    Check_intersection(),
3249
                                    Odr_violation_compare());
3250
          if (!intersection_result.had_intersection())
3251
            {
3252
              gold_warning(_("while linking %s: symbol '%s' defined in "
3253
                             "multiple places (possible ODR violation):"),
3254
                           output_file_name, demangle(symbol_name).c_str());
3255
              // This only prints one location from each definition,
3256
              // which may not be the location we expect to intersect
3257
              // with another definition.  We could print the whole
3258
              // set of locations, but that seems too verbose.
3259
              gold_assert(!first_object_linenos.empty());
3260
              gold_assert(!linenos.empty());
3261
              fprintf(stderr, _("  %s from %s\n"),
3262
                      first_object_linenos[0].c_str(),
3263
                      first_object_name.c_str());
3264
              fprintf(stderr, _("  %s from %s\n"),
3265
                      linenos[0].c_str(),
3266
                      locs->object->name().c_str());
3267
              // Only print one broken pair, to avoid needing to
3268
              // compare against a list of the disjoint definition
3269
              // locations we've found so far.  (If we kept comparing
3270
              // against just the first one, we'd get a lot of
3271
              // redundant complaints about the second definition
3272
              // location.)
3273
              break;
3274
            }
3275
        }
3276
    }
3277
  // We only call one_addr2line() in this function, so we can clear its cache.
3278
  Dwarf_line_info::clear_addr2line_cache();
3279
}
3280
 
3281
// Warnings functions.
3282
 
3283
// Add a new warning.
3284
 
3285
void
3286
Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
3287
                      const std::string& warning)
3288
{
3289
  name = symtab->canonicalize_name(name);
3290
  this->warnings_[name].set(obj, warning);
3291
}
3292
 
3293
// Look through the warnings and mark the symbols for which we should
3294
// warn.  This is called during Layout::finalize when we know the
3295
// sources for all the symbols.
3296
 
3297
void
3298
Warnings::note_warnings(Symbol_table* symtab)
3299
{
3300
  for (Warning_table::iterator p = this->warnings_.begin();
3301
       p != this->warnings_.end();
3302
       ++p)
3303
    {
3304
      Symbol* sym = symtab->lookup(p->first, NULL);
3305
      if (sym != NULL
3306
          && sym->source() == Symbol::FROM_OBJECT
3307
          && sym->object() == p->second.object)
3308
        sym->set_has_warning();
3309
    }
3310
}
3311
 
3312
// Issue a warning.  This is called when we see a relocation against a
3313
// symbol for which has a warning.
3314
 
3315
template<int size, bool big_endian>
3316
void
3317
Warnings::issue_warning(const Symbol* sym,
3318
                        const Relocate_info<size, big_endian>* relinfo,
3319
                        size_t relnum, off_t reloffset) const
3320
{
3321
  gold_assert(sym->has_warning());
3322 159 khays
 
3323
  // We don't want to issue a warning for a relocation against the
3324
  // symbol in the same object file in which the symbol is defined.
3325
  if (sym->object() == relinfo->object)
3326
    return;
3327
 
3328 27 khays
  Warning_table::const_iterator p = this->warnings_.find(sym->name());
3329
  gold_assert(p != this->warnings_.end());
3330
  gold_warning_at_location(relinfo, relnum, reloffset,
3331
                           "%s", p->second.text.c_str());
3332
}
3333
 
3334
// Instantiate the templates we need.  We could use the configure
3335
// script to restrict this to only the ones needed for implemented
3336
// targets.
3337
 
3338
#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
3339
template
3340
void
3341
Sized_symbol<32>::allocate_common(Output_data*, Value_type);
3342
#endif
3343
 
3344
#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
3345
template
3346
void
3347
Sized_symbol<64>::allocate_common(Output_data*, Value_type);
3348
#endif
3349
 
3350
#ifdef HAVE_TARGET_32_LITTLE
3351
template
3352
void
3353
Symbol_table::add_from_relobj<32, false>(
3354
    Sized_relobj_file<32, false>* relobj,
3355
    const unsigned char* syms,
3356
    size_t count,
3357
    size_t symndx_offset,
3358
    const char* sym_names,
3359
    size_t sym_name_size,
3360
    Sized_relobj_file<32, false>::Symbols* sympointers,
3361
    size_t* defined);
3362
#endif
3363
 
3364
#ifdef HAVE_TARGET_32_BIG
3365
template
3366
void
3367
Symbol_table::add_from_relobj<32, true>(
3368
    Sized_relobj_file<32, true>* relobj,
3369
    const unsigned char* syms,
3370
    size_t count,
3371
    size_t symndx_offset,
3372
    const char* sym_names,
3373
    size_t sym_name_size,
3374
    Sized_relobj_file<32, true>::Symbols* sympointers,
3375
    size_t* defined);
3376
#endif
3377
 
3378
#ifdef HAVE_TARGET_64_LITTLE
3379
template
3380
void
3381
Symbol_table::add_from_relobj<64, false>(
3382
    Sized_relobj_file<64, false>* relobj,
3383
    const unsigned char* syms,
3384
    size_t count,
3385
    size_t symndx_offset,
3386
    const char* sym_names,
3387
    size_t sym_name_size,
3388
    Sized_relobj_file<64, false>::Symbols* sympointers,
3389
    size_t* defined);
3390
#endif
3391
 
3392
#ifdef HAVE_TARGET_64_BIG
3393
template
3394
void
3395
Symbol_table::add_from_relobj<64, true>(
3396
    Sized_relobj_file<64, true>* relobj,
3397
    const unsigned char* syms,
3398
    size_t count,
3399
    size_t symndx_offset,
3400
    const char* sym_names,
3401
    size_t sym_name_size,
3402
    Sized_relobj_file<64, true>::Symbols* sympointers,
3403
    size_t* defined);
3404
#endif
3405
 
3406
#ifdef HAVE_TARGET_32_LITTLE
3407
template
3408
Symbol*
3409
Symbol_table::add_from_pluginobj<32, false>(
3410
    Sized_pluginobj<32, false>* obj,
3411
    const char* name,
3412
    const char* ver,
3413
    elfcpp::Sym<32, false>* sym);
3414
#endif
3415
 
3416
#ifdef HAVE_TARGET_32_BIG
3417
template
3418
Symbol*
3419
Symbol_table::add_from_pluginobj<32, true>(
3420
    Sized_pluginobj<32, true>* obj,
3421
    const char* name,
3422
    const char* ver,
3423
    elfcpp::Sym<32, true>* sym);
3424
#endif
3425
 
3426
#ifdef HAVE_TARGET_64_LITTLE
3427
template
3428
Symbol*
3429
Symbol_table::add_from_pluginobj<64, false>(
3430
    Sized_pluginobj<64, false>* obj,
3431
    const char* name,
3432
    const char* ver,
3433
    elfcpp::Sym<64, false>* sym);
3434
#endif
3435
 
3436
#ifdef HAVE_TARGET_64_BIG
3437
template
3438
Symbol*
3439
Symbol_table::add_from_pluginobj<64, true>(
3440
    Sized_pluginobj<64, true>* obj,
3441
    const char* name,
3442
    const char* ver,
3443
    elfcpp::Sym<64, true>* sym);
3444
#endif
3445
 
3446
#ifdef HAVE_TARGET_32_LITTLE
3447
template
3448
void
3449
Symbol_table::add_from_dynobj<32, false>(
3450
    Sized_dynobj<32, false>* dynobj,
3451
    const unsigned char* syms,
3452
    size_t count,
3453
    const char* sym_names,
3454
    size_t sym_name_size,
3455
    const unsigned char* versym,
3456
    size_t versym_size,
3457
    const std::vector<const char*>* version_map,
3458
    Sized_relobj_file<32, false>::Symbols* sympointers,
3459
    size_t* defined);
3460
#endif
3461
 
3462
#ifdef HAVE_TARGET_32_BIG
3463
template
3464
void
3465
Symbol_table::add_from_dynobj<32, true>(
3466
    Sized_dynobj<32, true>* dynobj,
3467
    const unsigned char* syms,
3468
    size_t count,
3469
    const char* sym_names,
3470
    size_t sym_name_size,
3471
    const unsigned char* versym,
3472
    size_t versym_size,
3473
    const std::vector<const char*>* version_map,
3474
    Sized_relobj_file<32, true>::Symbols* sympointers,
3475
    size_t* defined);
3476
#endif
3477
 
3478
#ifdef HAVE_TARGET_64_LITTLE
3479
template
3480
void
3481
Symbol_table::add_from_dynobj<64, false>(
3482
    Sized_dynobj<64, false>* dynobj,
3483
    const unsigned char* syms,
3484
    size_t count,
3485
    const char* sym_names,
3486
    size_t sym_name_size,
3487
    const unsigned char* versym,
3488
    size_t versym_size,
3489
    const std::vector<const char*>* version_map,
3490
    Sized_relobj_file<64, false>::Symbols* sympointers,
3491
    size_t* defined);
3492
#endif
3493
 
3494
#ifdef HAVE_TARGET_64_BIG
3495
template
3496
void
3497
Symbol_table::add_from_dynobj<64, true>(
3498
    Sized_dynobj<64, true>* dynobj,
3499
    const unsigned char* syms,
3500
    size_t count,
3501
    const char* sym_names,
3502
    size_t sym_name_size,
3503
    const unsigned char* versym,
3504
    size_t versym_size,
3505
    const std::vector<const char*>* version_map,
3506
    Sized_relobj_file<64, true>::Symbols* sympointers,
3507
    size_t* defined);
3508
#endif
3509
 
3510
#ifdef HAVE_TARGET_32_LITTLE
3511
template
3512 148 khays
Sized_symbol<32>*
3513 27 khays
Symbol_table::add_from_incrobj(
3514
    Object* obj,
3515
    const char* name,
3516
    const char* ver,
3517
    elfcpp::Sym<32, false>* sym);
3518
#endif
3519
 
3520
#ifdef HAVE_TARGET_32_BIG
3521
template
3522 148 khays
Sized_symbol<32>*
3523 27 khays
Symbol_table::add_from_incrobj(
3524
    Object* obj,
3525
    const char* name,
3526
    const char* ver,
3527
    elfcpp::Sym<32, true>* sym);
3528
#endif
3529
 
3530
#ifdef HAVE_TARGET_64_LITTLE
3531
template
3532 148 khays
Sized_symbol<64>*
3533 27 khays
Symbol_table::add_from_incrobj(
3534
    Object* obj,
3535
    const char* name,
3536
    const char* ver,
3537
    elfcpp::Sym<64, false>* sym);
3538
#endif
3539
 
3540
#ifdef HAVE_TARGET_64_BIG
3541
template
3542 148 khays
Sized_symbol<64>*
3543 27 khays
Symbol_table::add_from_incrobj(
3544
    Object* obj,
3545
    const char* name,
3546
    const char* ver,
3547
    elfcpp::Sym<64, true>* sym);
3548
#endif
3549
 
3550
#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
3551
template
3552
void
3553
Symbol_table::define_with_copy_reloc<32>(
3554
    Sized_symbol<32>* sym,
3555
    Output_data* posd,
3556
    elfcpp::Elf_types<32>::Elf_Addr value);
3557
#endif
3558
 
3559
#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
3560
template
3561
void
3562
Symbol_table::define_with_copy_reloc<64>(
3563
    Sized_symbol<64>* sym,
3564
    Output_data* posd,
3565
    elfcpp::Elf_types<64>::Elf_Addr value);
3566
#endif
3567
 
3568
#ifdef HAVE_TARGET_32_LITTLE
3569
template
3570
void
3571
Warnings::issue_warning<32, false>(const Symbol* sym,
3572
                                   const Relocate_info<32, false>* relinfo,
3573
                                   size_t relnum, off_t reloffset) const;
3574
#endif
3575
 
3576
#ifdef HAVE_TARGET_32_BIG
3577
template
3578
void
3579
Warnings::issue_warning<32, true>(const Symbol* sym,
3580
                                  const Relocate_info<32, true>* relinfo,
3581
                                  size_t relnum, off_t reloffset) const;
3582
#endif
3583
 
3584
#ifdef HAVE_TARGET_64_LITTLE
3585
template
3586
void
3587
Warnings::issue_warning<64, false>(const Symbol* sym,
3588
                                   const Relocate_info<64, false>* relinfo,
3589
                                   size_t relnum, off_t reloffset) const;
3590
#endif
3591
 
3592
#ifdef HAVE_TARGET_64_BIG
3593
template
3594
void
3595
Warnings::issue_warning<64, true>(const Symbol* sym,
3596
                                  const Relocate_info<64, true>* relinfo,
3597
                                  size_t relnum, off_t reloffset) const;
3598
#endif
3599
 
3600
} // End namespace gold.

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.