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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [binutils-2.20.1/] [gold/] [symtab.cc] - Blame information for rev 252

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

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

powered by: WebSVN 2.1.0

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