Add static version of constructor test.
[deliverable/binutils-gdb.git] / gold / symtab.h
CommitLineData
bae7f79e
ILT
1// symtab.h -- the gold symbol table -*- C++ -*-
2
6cb15b7f
ILT
3// Copyright 2006, 2007 Free Software Foundation, Inc.
4// Written by Ian Lance Taylor <iant@google.com>.
5
6// This file is part of gold.
7
8// This program is free software; you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation; either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17
18// You should have received a copy of the GNU General Public License
19// along with this program; if not, write to the Free Software
20// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21// MA 02110-1301, USA.
22
bae7f79e
ILT
23// Symbol_table
24// The symbol table.
25
bae7f79e
ILT
26#include <string>
27#include <utility>
ead1e424 28#include <vector>
bae7f79e
ILT
29
30#include "elfcpp.h"
7e1edb90 31#include "parameters.h"
14bfc3f5 32#include "stringpool.h"
f6ce93d6 33#include "object.h"
bae7f79e
ILT
34
35#ifndef GOLD_SYMTAB_H
36#define GOLD_SYMTAB_H
37
38namespace gold
39{
40
14bfc3f5 41class Object;
f6ce93d6 42class Relobj;
dbe717ef
ILT
43template<int size, bool big_endian>
44class Sized_relobj;
f6ce93d6 45class Dynobj;
dbe717ef
ILT
46template<int size, bool big_endian>
47class Sized_dynobj;
14b31740 48class Versions;
ead1e424 49class Output_data;
a3ad94ed 50class Output_section;
ead1e424 51class Output_segment;
61ba1cf9
ILT
52class Output_file;
53class Target;
14bfc3f5 54
14bfc3f5
ILT
55// The base class of an entry in the symbol table. The symbol table
56// can have a lot of entries, so we don't want this class to big.
57// Size dependent fields can be found in the template class
58// Sized_symbol. Targets may support their own derived classes.
bae7f79e 59
bae7f79e
ILT
60class Symbol
61{
62 public:
ead1e424
ILT
63 // Because we want the class to be small, we don't use any virtual
64 // functions. But because symbols can be defined in different
65 // places, we need to classify them. This enum is the different
66 // sources of symbols we support.
67 enum Source
68 {
f6ce93d6
ILT
69 // Symbol defined in a relocatable or dynamic input file--this is
70 // the most common case.
ead1e424
ILT
71 FROM_OBJECT,
72 // Symbol defined in an Output_data, a special section created by
73 // the target.
74 IN_OUTPUT_DATA,
75 // Symbol defined in an Output_segment, with no associated
76 // section.
77 IN_OUTPUT_SEGMENT,
78 // Symbol value is constant.
79 CONSTANT
80 };
81
82 // When the source is IN_OUTPUT_SEGMENT, we need to describe what
83 // the offset means.
84 enum Segment_offset_base
85 {
86 // From the start of the segment.
87 SEGMENT_START,
88 // From the end of the segment.
89 SEGMENT_END,
90 // From the filesz of the segment--i.e., after the loaded bytes
91 // but before the bytes which are allocated but zeroed.
92 SEGMENT_BSS
93 };
94
14bfc3f5
ILT
95 // Return the symbol name.
96 const char*
97 name() const
98 { return this->name_; }
99
100 // Return the symbol version. This will return NULL for an
101 // unversioned symbol.
102 const char*
103 version() const
104 { return this->version_; }
105
ead1e424
ILT
106 // Return the symbol source.
107 Source
108 source() const
109 { return this->source_; }
110
14bfc3f5
ILT
111 // Return the object with which this symbol is associated.
112 Object*
113 object() const
ead1e424 114 {
a3ad94ed 115 gold_assert(this->source_ == FROM_OBJECT);
ead1e424
ILT
116 return this->u_.from_object.object;
117 }
118
f6ce93d6
ILT
119 // Return the index of the section in the input relocatable or
120 // dynamic object file.
ead1e424 121 unsigned int
16649710 122 shndx() const
ead1e424 123 {
a3ad94ed 124 gold_assert(this->source_ == FROM_OBJECT);
16649710 125 return this->u_.from_object.shndx;
ead1e424
ILT
126 }
127
128 // Return the output data section with which this symbol is
129 // associated, if the symbol was specially defined with respect to
130 // an output data section.
131 Output_data*
132 output_data() const
133 {
a3ad94ed 134 gold_assert(this->source_ == IN_OUTPUT_DATA);
ead1e424
ILT
135 return this->u_.in_output_data.output_data;
136 }
137
138 // If this symbol was defined with respect to an output data
139 // section, return whether the value is an offset from end.
140 bool
141 offset_is_from_end() const
142 {
a3ad94ed 143 gold_assert(this->source_ == IN_OUTPUT_DATA);
ead1e424
ILT
144 return this->u_.in_output_data.offset_is_from_end;
145 }
146
147 // Return the output segment with which this symbol is associated,
148 // if the symbol was specially defined with respect to an output
149 // segment.
150 Output_segment*
151 output_segment() const
152 {
a3ad94ed 153 gold_assert(this->source_ == IN_OUTPUT_SEGMENT);
ead1e424
ILT
154 return this->u_.in_output_segment.output_segment;
155 }
156
157 // If this symbol was defined with respect to an output segment,
158 // return the offset base.
159 Segment_offset_base
160 offset_base() const
161 {
a3ad94ed 162 gold_assert(this->source_ == IN_OUTPUT_SEGMENT);
ead1e424
ILT
163 return this->u_.in_output_segment.offset_base;
164 }
14bfc3f5
ILT
165
166 // Return the symbol binding.
167 elfcpp::STB
168 binding() const
169 { return this->binding_; }
170
1564db8d
ILT
171 // Return the symbol type.
172 elfcpp::STT
173 type() const
174 { return this->type_; }
175
176 // Return the symbol visibility.
177 elfcpp::STV
178 visibility() const
179 { return this->visibility_; }
180
181 // Return the non-visibility part of the st_other field.
182 unsigned char
ead1e424
ILT
183 nonvis() const
184 { return this->nonvis_; }
14bfc3f5 185
1564db8d
ILT
186 // Return whether this symbol is a forwarder. This will never be
187 // true of a symbol found in the hash table, but may be true of
188 // symbol pointers attached to object files.
189 bool
190 is_forwarder() const
191 { return this->is_forwarder_; }
192
193 // Mark this symbol as a forwarder.
194 void
195 set_forwarder()
196 { this->is_forwarder_ = true; }
197
c06b7b0b
ILT
198 // Return whether this symbol needs an entry in the dynamic symbol
199 // table.
200 bool
201 needs_dynsym_entry() const
429c1569
ILT
202 {
203 return (this->needs_dynsym_entry_
204 || (this->in_reg() && this->in_dyn()));
205 }
c06b7b0b
ILT
206
207 // Mark this symbol as needing an entry in the dynamic symbol table.
208 void
209 set_needs_dynsym_entry()
210 { this->needs_dynsym_entry_ = true; }
211
008db82e
ILT
212 // Return whether this symbol has been seen in a regular object.
213 bool
214 in_reg() const
215 { return this->in_reg_; }
216
217 // Mark this symbol as having been seen in a regular object.
218 void
219 set_in_reg()
220 { this->in_reg_ = true; }
221
1ebd95fd
ILT
222 // Return whether this symbol has been seen in a dynamic object.
223 bool
224 in_dyn() const
225 { return this->in_dyn_; }
226
f6ce93d6 227 // Mark this symbol as having been seen in a dynamic object.
1564db8d
ILT
228 void
229 set_in_dyn()
230 { this->in_dyn_ = true; }
231
c06b7b0b
ILT
232 // Return the index of this symbol in the output file symbol table.
233 // A value of -1U means that this symbol is not going into the
234 // output file. This starts out as zero, and is set to a non-zero
235 // value by Symbol_table::finalize. It is an error to ask for the
236 // symbol table index before it has been set.
237 unsigned int
238 symtab_index() const
239 {
a3ad94ed 240 gold_assert(this->symtab_index_ != 0);
c06b7b0b
ILT
241 return this->symtab_index_;
242 }
243
244 // Set the index of the symbol in the output file symbol table.
245 void
246 set_symtab_index(unsigned int index)
247 {
a3ad94ed 248 gold_assert(index != 0);
c06b7b0b
ILT
249 this->symtab_index_ = index;
250 }
251
a3ad94ed
ILT
252 // Return whether this symbol already has an index in the output
253 // file symbol table.
254 bool
255 has_symtab_index() const
256 { return this->symtab_index_ != 0; }
257
c06b7b0b
ILT
258 // Return the index of this symbol in the dynamic symbol table. A
259 // value of -1U means that this symbol is not going into the dynamic
260 // symbol table. This starts out as zero, and is set to a non-zero
261 // during Layout::finalize. It is an error to ask for the dynamic
262 // symbol table index before it has been set.
263 unsigned int
264 dynsym_index() const
265 {
a3ad94ed 266 gold_assert(this->dynsym_index_ != 0);
c06b7b0b
ILT
267 return this->dynsym_index_;
268 }
269
270 // Set the index of the symbol in the dynamic symbol table.
271 void
272 set_dynsym_index(unsigned int index)
273 {
a3ad94ed 274 gold_assert(index != 0);
c06b7b0b
ILT
275 this->dynsym_index_ = index;
276 }
277
16649710
ILT
278 // Return whether this symbol already has an index in the dynamic
279 // symbol table.
280 bool
281 has_dynsym_index() const
282 { return this->dynsym_index_ != 0; }
283
ead1e424 284 // Return whether this symbol has an entry in the GOT section.
92e059d8 285 bool
ead1e424
ILT
286 has_got_offset() const
287 { return this->has_got_offset_; }
288
289 // Return the offset into the GOT section of this symbol.
290 unsigned int
291 got_offset() const
292 {
a3ad94ed 293 gold_assert(this->has_got_offset());
ead1e424
ILT
294 return this->got_offset_;
295 }
296
297 // Set the GOT offset of this symbol.
298 void
299 set_got_offset(unsigned int got_offset)
300 {
301 this->has_got_offset_ = true;
302 this->got_offset_ = got_offset;
303 }
304
a3ad94ed 305 // Return whether this symbol has an entry in the PLT section.
ead1e424 306 bool
a3ad94ed
ILT
307 has_plt_offset() const
308 { return this->has_plt_offset_; }
309
310 // Return the offset into the PLT section of this symbol.
311 unsigned int
312 plt_offset() const
313 {
314 gold_assert(this->has_plt_offset());
315 return this->plt_offset_;
316 }
317
318 // Set the PLT offset of this symbol.
319 void
320 set_plt_offset(unsigned int plt_offset)
321 {
322 this->has_plt_offset_ = true;
323 this->plt_offset_ = plt_offset;
324 }
325
326 // Return true if the final value of this symbol is known at link
327 // time.
328 bool
7e1edb90 329 final_value_is_known() const
a3ad94ed 330 {
7e1edb90 331 if (parameters->output_is_shared())
a3ad94ed
ILT
332 return false;
333 return this->source_ != FROM_OBJECT || !this->object()->is_dynamic();
334 }
ead1e424 335
f6ce93d6
ILT
336 // Return whether this is a defined symbol (not undefined or
337 // common).
338 bool
339 is_defined() const
340 {
341 return (this->source_ != FROM_OBJECT
16649710
ILT
342 || (this->shndx() != elfcpp::SHN_UNDEF
343 && this->shndx() != elfcpp::SHN_COMMON));
a3ad94ed
ILT
344 }
345
14b31740 346 // Return true if this symbol is from a dynamic object.
a3ad94ed 347 bool
14b31740 348 is_from_dynobj() const
a3ad94ed 349 {
14b31740 350 return this->source_ == FROM_OBJECT && this->object()->is_dynamic();
f6ce93d6
ILT
351 }
352
ead1e424
ILT
353 // Return whether this is an undefined symbol.
354 bool
355 is_undefined() const
356 {
16649710 357 return this->source_ == FROM_OBJECT && this->shndx() == elfcpp::SHN_UNDEF;
ead1e424
ILT
358 }
359
360 // Return whether this is a common symbol.
361 bool
362 is_common() const
363 {
f6ce93d6 364 return (this->source_ == FROM_OBJECT
16649710 365 && (this->shndx() == elfcpp::SHN_COMMON
f6ce93d6 366 || this->type_ == elfcpp::STT_COMMON));
ead1e424 367 }
92e059d8 368
a6badf5a
ILT
369 // Return whether this symbol can be seen outside this object.
370 bool
371 is_externally_visible() const
372 {
373 return (this->visibility_ == elfcpp::STV_DEFAULT
374 || this->visibility_ == elfcpp::STV_PROTECTED);
375 }
376
f6ce93d6
ILT
377 // Return whether there should be a warning for references to this
378 // symbol.
379 bool
380 has_warning() const
381 { return this->has_warning_; }
382
383 // Mark this symbol as having a warning.
384 void
385 set_has_warning()
386 { this->has_warning_ = true; }
387
14bfc3f5
ILT
388 protected:
389 // Instances of this class should always be created at a specific
390 // size.
391 Symbol()
f6ce93d6 392 { memset(this, 0, sizeof *this); }
14bfc3f5 393
ead1e424
ILT
394 // Initialize the general fields.
395 void
396 init_fields(const char* name, const char* version,
397 elfcpp::STT type, elfcpp::STB binding,
398 elfcpp::STV visibility, unsigned char nonvis);
399
14bfc3f5
ILT
400 // Initialize fields from an ELF symbol in OBJECT.
401 template<int size, bool big_endian>
402 void
403 init_base(const char *name, const char* version, Object* object,
404 const elfcpp::Sym<size, big_endian>&);
bae7f79e 405
ead1e424
ILT
406 // Initialize fields for an Output_data.
407 void
408 init_base(const char* name, Output_data*, elfcpp::STT, elfcpp::STB,
409 elfcpp::STV, unsigned char nonvis, bool offset_is_from_end);
410
411 // Initialize fields for an Output_segment.
412 void
413 init_base(const char* name, Output_segment* os, elfcpp::STT type,
414 elfcpp::STB binding, elfcpp::STV visibility,
415 unsigned char nonvis, Segment_offset_base offset_base);
416
417 // Initialize fields for a constant.
418 void
419 init_base(const char* name, elfcpp::STT type, elfcpp::STB binding,
420 elfcpp::STV visibility, unsigned char nonvis);
421
1564db8d
ILT
422 // Override existing symbol.
423 template<int size, bool big_endian>
424 void
14b31740
ILT
425 override_base(const elfcpp::Sym<size, big_endian>&, Object* object,
426 const char* version);
1564db8d 427
86f2e683
ILT
428 // Override existing symbol with a special symbol.
429 void
430 override_base_with_special(const Symbol* from);
431
bae7f79e 432 private:
14bfc3f5
ILT
433 Symbol(const Symbol&);
434 Symbol& operator=(const Symbol&);
435
436 // Symbol name (expected to point into a Stringpool).
437 const char* name_;
438 // Symbol version (expected to point into a Stringpool). This may
439 // be NULL.
bae7f79e 440 const char* version_;
ead1e424
ILT
441
442 union
443 {
444 // This struct is used if SOURCE_ == FROM_OBJECT.
445 struct
446 {
447 // Object in which symbol is defined, or in which it was first
448 // seen.
449 Object* object;
450 // Section number in object_ in which symbol is defined.
16649710 451 unsigned int shndx;
ead1e424
ILT
452 } from_object;
453
454 // This struct is used if SOURCE_ == IN_OUTPUT_DATA.
455 struct
456 {
457 // Output_data in which symbol is defined. Before
458 // Layout::finalize the symbol's value is an offset within the
459 // Output_data.
460 Output_data* output_data;
461 // True if the offset is from the end, false if the offset is
462 // from the beginning.
463 bool offset_is_from_end;
464 } in_output_data;
465
466 // This struct is used if SOURCE_ == IN_OUTPUT_SEGMENT.
467 struct
468 {
469 // Output_segment in which the symbol is defined. Before
470 // Layout::finalize the symbol's value is an offset.
471 Output_segment* output_segment;
472 // The base to use for the offset before Layout::finalize.
473 Segment_offset_base offset_base;
474 } in_output_segment;
475 } u_;
476
c06b7b0b
ILT
477 // The index of this symbol in the output file. If the symbol is
478 // not going into the output file, this value is -1U. This field
479 // starts as always holding zero. It is set to a non-zero value by
480 // Symbol_table::finalize.
481 unsigned int symtab_index_;
482
483 // The index of this symbol in the dynamic symbol table. If the
484 // symbol is not going into the dynamic symbol table, this value is
485 // -1U. This field starts as always holding zero. It is set to a
486 // non-zero value during Layout::finalize.
487 unsigned int dynsym_index_;
488
ead1e424 489 // If this symbol has an entry in the GOT section (has_got_offset_
c06b7b0b 490 // is true), this is the offset from the start of the GOT section.
ead1e424 491 unsigned int got_offset_;
c06b7b0b 492
a3ad94ed
ILT
493 // If this symbol has an entry in the PLT section (has_plt_offset_
494 // is true), then this is the offset from the start of the PLT
495 // section.
496 unsigned int plt_offset_;
497
14bfc3f5 498 // Symbol type.
bae7f79e 499 elfcpp::STT type_ : 4;
14bfc3f5 500 // Symbol binding.
bae7f79e 501 elfcpp::STB binding_ : 4;
14bfc3f5
ILT
502 // Symbol visibility.
503 elfcpp::STV visibility_ : 2;
504 // Rest of symbol st_other field.
ead1e424
ILT
505 unsigned int nonvis_ : 6;
506 // The type of symbol.
f6ce93d6 507 Source source_ : 3;
14bfc3f5
ILT
508 // True if this symbol always requires special target-specific
509 // handling.
ead1e424 510 bool is_target_special_ : 1;
14bfc3f5 511 // True if this is the default version of the symbol.
1564db8d 512 bool is_def_ : 1;
14bfc3f5
ILT
513 // True if this symbol really forwards to another symbol. This is
514 // used when we discover after the fact that two different entries
515 // in the hash table really refer to the same symbol. This will
516 // never be set for a symbol found in the hash table, but may be set
517 // for a symbol found in the list of symbols attached to an Object.
518 // It forwards to the symbol found in the forwarders_ map of
519 // Symbol_table.
1564db8d 520 bool is_forwarder_ : 1;
c06b7b0b
ILT
521 // True if this symbol needs to be in the dynamic symbol table.
522 bool needs_dynsym_entry_ : 1;
008db82e
ILT
523 // True if we've seen this symbol in a regular object.
524 bool in_reg_ : 1;
1564db8d
ILT
525 // True if we've seen this symbol in a dynamic object.
526 bool in_dyn_ : 1;
ead1e424
ILT
527 // True if the symbol has an entry in the GOT section.
528 bool has_got_offset_ : 1;
a3ad94ed
ILT
529 // True if the symbol has an entry in the PLT section.
530 bool has_plt_offset_ : 1;
f6ce93d6
ILT
531 // True if there is a warning for this symbol.
532 bool has_warning_ : 1;
bae7f79e
ILT
533};
534
14bfc3f5
ILT
535// The parts of a symbol which are size specific. Using a template
536// derived class like this helps us use less space on a 32-bit system.
bae7f79e
ILT
537
538template<int size>
14bfc3f5
ILT
539class Sized_symbol : public Symbol
540{
541 public:
1564db8d
ILT
542 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value_type;
543 typedef typename elfcpp::Elf_types<size>::Elf_WXword Size_type;
544
14bfc3f5
ILT
545 Sized_symbol()
546 { }
547
548 // Initialize fields from an ELF symbol in OBJECT.
549 template<bool big_endian>
550 void
551 init(const char *name, const char* version, Object* object,
552 const elfcpp::Sym<size, big_endian>&);
553
ead1e424
ILT
554 // Initialize fields for an Output_data.
555 void
556 init(const char* name, Output_data*, Value_type value, Size_type symsize,
557 elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis,
558 bool offset_is_from_end);
559
560 // Initialize fields for an Output_segment.
561 void
562 init(const char* name, Output_segment*, Value_type value, Size_type symsize,
563 elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis,
564 Segment_offset_base offset_base);
565
566 // Initialize fields for a constant.
567 void
568 init(const char* name, Value_type value, Size_type symsize,
569 elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis);
570
1564db8d
ILT
571 // Override existing symbol.
572 template<bool big_endian>
573 void
14b31740
ILT
574 override(const elfcpp::Sym<size, big_endian>&, Object* object,
575 const char* version);
1564db8d 576
86f2e683
ILT
577 // Override existing symbol with a special symbol.
578 void
579 override_with_special(const Sized_symbol<size>*);
580
1564db8d
ILT
581 // Return the symbol's value.
582 Value_type
583 value() const
584 { return this->value_; }
585
586 // Return the symbol's size (we can't call this 'size' because that
587 // is a template parameter).
588 Size_type
589 symsize() const
ead1e424
ILT
590 { return this->symsize_; }
591
592 // Set the symbol size. This is used when resolving common symbols.
593 void
594 set_symsize(Size_type symsize)
595 { this->symsize_ = symsize; }
1564db8d 596
75f65a3e
ILT
597 // Set the symbol value. This is called when we store the final
598 // values of the symbols into the symbol table.
599 void
600 set_value(Value_type value)
601 { this->value_ = value; }
602
14bfc3f5
ILT
603 private:
604 Sized_symbol(const Sized_symbol&);
605 Sized_symbol& operator=(const Sized_symbol&);
606
ead1e424
ILT
607 // Symbol value. Before Layout::finalize this is the offset in the
608 // input section. This is set to the final value during
609 // Layout::finalize.
1564db8d 610 Value_type value_;
14bfc3f5 611 // Symbol size.
ead1e424
ILT
612 Size_type symsize_;
613};
614
615// A struct describing a symbol defined by the linker, where the value
616// of the symbol is defined based on an output section. This is used
617// for symbols defined by the linker, like "_init_array_start".
618
619struct Define_symbol_in_section
620{
621 // The symbol name.
622 const char* name;
623 // The name of the output section with which this symbol should be
624 // associated. If there is no output section with that name, the
625 // symbol will be defined as zero.
626 const char* output_section;
627 // The offset of the symbol within the output section. This is an
628 // offset from the start of the output section, unless start_at_end
629 // is true, in which case this is an offset from the end of the
630 // output section.
631 uint64_t value;
632 // The size of the symbol.
633 uint64_t size;
634 // The symbol type.
635 elfcpp::STT type;
636 // The symbol binding.
637 elfcpp::STB binding;
638 // The symbol visibility.
639 elfcpp::STV visibility;
640 // The rest of the st_other field.
641 unsigned char nonvis;
642 // If true, the value field is an offset from the end of the output
643 // section.
644 bool offset_is_from_end;
645 // If true, this symbol is defined only if we see a reference to it.
646 bool only_if_ref;
647};
648
649// A struct describing a symbol defined by the linker, where the value
650// of the symbol is defined based on a segment. This is used for
651// symbols defined by the linker, like "_end". We describe the
652// segment with which the symbol should be associated by its
653// characteristics. If no segment meets these characteristics, the
654// symbol will be defined as zero. If there is more than one segment
655// which meets these characteristics, we will use the first one.
656
657struct Define_symbol_in_segment
658{
659 // The symbol name.
660 const char* name;
661 // The segment type where the symbol should be defined, typically
662 // PT_LOAD.
663 elfcpp::PT segment_type;
664 // Bitmask of segment flags which must be set.
665 elfcpp::PF segment_flags_set;
666 // Bitmask of segment flags which must be clear.
667 elfcpp::PF segment_flags_clear;
668 // The offset of the symbol within the segment. The offset is
669 // calculated from the position set by offset_base.
670 uint64_t value;
671 // The size of the symbol.
672 uint64_t size;
673 // The symbol type.
674 elfcpp::STT type;
675 // The symbol binding.
676 elfcpp::STB binding;
677 // The symbol visibility.
678 elfcpp::STV visibility;
679 // The rest of the st_other field.
680 unsigned char nonvis;
681 // The base from which we compute the offset.
682 Symbol::Segment_offset_base offset_base;
683 // If true, this symbol is defined only if we see a reference to it.
684 bool only_if_ref;
14bfc3f5
ILT
685};
686
f6ce93d6
ILT
687// This class manages warnings. Warnings are a GNU extension. When
688// we see a section named .gnu.warning.SYM in an object file, and if
689// we wind using the definition of SYM from that object file, then we
690// will issue a warning for any relocation against SYM from a
691// different object file. The text of the warning is the contents of
692// the section. This is not precisely the definition used by the old
693// GNU linker; the old GNU linker treated an occurrence of
694// .gnu.warning.SYM as defining a warning symbol. A warning symbol
695// would trigger a warning on any reference. However, it was
696// inconsistent in that a warning in a dynamic object only triggered
697// if there was no definition in a regular object. This linker is
698// different in that we only issue a warning if we use the symbol
699// definition from the same object file as the warning section.
700
701class Warnings
702{
703 public:
704 Warnings()
705 : warnings_()
706 { }
707
708 // Add a warning for symbol NAME in section SHNDX in object OBJ.
709 void
710 add_warning(Symbol_table* symtab, const char* name, Object* obj,
711 unsigned int shndx);
712
713 // For each symbol for which we should give a warning, make a note
714 // on the symbol.
715 void
716 note_warnings(Symbol_table* symtab);
717
718 // Issue a warning for a reference to SYM at LOCATION.
719 void
c06b7b0b 720 issue_warning(const Symbol* sym, const std::string& location) const;
f6ce93d6
ILT
721
722 private:
723 Warnings(const Warnings&);
724 Warnings& operator=(const Warnings&);
725
726 // What we need to know to get the warning text.
727 struct Warning_location
728 {
729 // The object the warning is in.
730 Object* object;
731 // The index of the warning section.
732 unsigned int shndx;
733 // The warning text if we have already loaded it.
734 std::string text;
735
736 Warning_location()
737 : object(NULL), shndx(0), text()
738 { }
739
740 void
741 set(Object* o, unsigned int s)
742 {
743 this->object = o;
744 this->shndx = s;
745 }
746
747 void
748 set_text(const char* t, off_t l)
749 { this->text.assign(t, l); }
750 };
751
752 // A mapping from warning symbol names (canonicalized in
753 // Symbol_table's namepool_ field) to
754 typedef Unordered_map<const char*, Warning_location> Warning_table;
755
756 Warning_table warnings_;
757};
758
14bfc3f5
ILT
759// The main linker symbol table.
760
bae7f79e
ILT
761class Symbol_table
762{
763 public:
764 Symbol_table();
765
1564db8d 766 ~Symbol_table();
bae7f79e 767
dbe717ef 768 // Add COUNT external symbols from the relocatable object RELOBJ to
f6ce93d6
ILT
769 // the symbol table. SYMS is the symbols, SYM_NAMES is their names,
770 // SYM_NAME_SIZE is the size of SYM_NAMES. This sets SYMPOINTERS to
771 // point to the symbols in the symbol table.
14bfc3f5
ILT
772 template<int size, bool big_endian>
773 void
dbe717ef
ILT
774 add_from_relobj(Sized_relobj<size, big_endian>* relobj,
775 const unsigned char* syms, size_t count,
776 const char* sym_names, size_t sym_name_size,
14bfc3f5
ILT
777 Symbol** sympointers);
778
dbe717ef
ILT
779 // Add COUNT dynamic symbols from the dynamic object DYNOBJ to the
780 // symbol table. SYMS is the symbols. SYM_NAMES is their names.
781 // SYM_NAME_SIZE is the size of SYM_NAMES. The other parameters are
782 // symbol version data.
783 template<int size, bool big_endian>
784 void
785 add_from_dynobj(Sized_dynobj<size, big_endian>* dynobj,
786 const unsigned char* syms, size_t count,
787 const char* sym_names, size_t sym_name_size,
788 const unsigned char* versym, size_t versym_size,
789 const std::vector<const char*>*);
790
ead1e424
ILT
791 // Define a special symbol based on an Output_data. It is a
792 // multiple definition error if this symbol is already defined.
14b31740
ILT
793 Symbol*
794 define_in_output_data(const Target*, const char* name, const char* version,
795 Output_data*, uint64_t value, uint64_t symsize,
ead1e424
ILT
796 elfcpp::STT type, elfcpp::STB binding,
797 elfcpp::STV visibility, unsigned char nonvis,
798 bool offset_is_from_end, bool only_if_ref);
799
800 // Define a special symbol based on an Output_segment. It is a
801 // multiple definition error if this symbol is already defined.
14b31740
ILT
802 Symbol*
803 define_in_output_segment(const Target*, const char* name,
804 const char* version, Output_segment*,
ead1e424
ILT
805 uint64_t value, uint64_t symsize,
806 elfcpp::STT type, elfcpp::STB binding,
807 elfcpp::STV visibility, unsigned char nonvis,
808 Symbol::Segment_offset_base, bool only_if_ref);
809
810 // Define a special symbol with a constant value. It is a multiple
811 // definition error if this symbol is already defined.
14b31740
ILT
812 Symbol*
813 define_as_constant(const Target*, const char* name, const char* version,
814 uint64_t value, uint64_t symsize, elfcpp::STT type,
815 elfcpp::STB binding, elfcpp::STV visibility,
816 unsigned char nonvis, bool only_if_ref);
ead1e424
ILT
817
818 // Define a set of symbols in output sections.
819 void
14b31740 820 define_symbols(const Layout*, const Target*, int count,
ead1e424
ILT
821 const Define_symbol_in_section*);
822
823 // Define a set of symbols in output segments.
824 void
14b31740 825 define_symbols(const Layout*, const Target*, int count,
ead1e424
ILT
826 const Define_symbol_in_segment*);
827
61ba1cf9
ILT
828 // Look up a symbol.
829 Symbol*
830 lookup(const char*, const char* version = NULL) const;
831
14bfc3f5 832 // Return the real symbol associated with the forwarder symbol FROM.
bae7f79e 833 Symbol*
c06b7b0b 834 resolve_forwards(const Symbol* from) const;
bae7f79e 835
a3ad94ed 836 // Return the bitsize (32 or 64) of the symbols in the table.
14bfc3f5
ILT
837 int
838 get_size() const
839 { return this->size_; }
bae7f79e 840
1564db8d
ILT
841 // Return the sized version of a symbol in this table.
842 template<int size>
843 Sized_symbol<size>*
5482377d 844 get_sized_symbol(Symbol* ACCEPT_SIZE) const;
1564db8d
ILT
845
846 template<int size>
847 const Sized_symbol<size>*
5482377d 848 get_sized_symbol(const Symbol* ACCEPT_SIZE) const;
54dc6425 849
ead1e424
ILT
850 // Return the count of undefined symbols seen.
851 int
852 saw_undefined() const
853 { return this->saw_undefined_; }
854
855 // Allocate the common symbols
856 void
857 allocate_commons(const General_options&, Layout*);
858
f6ce93d6
ILT
859 // Add a warning for symbol NAME in section SHNDX in object OBJ.
860 void
861 add_warning(const char* name, Object* obj, unsigned int shndx)
862 { this->warnings_.add_warning(this, name, obj, shndx); }
863
864 // Canonicalize a symbol name for use in the hash table.
865 const char*
866 canonicalize_name(const char* name)
f0641a0b 867 { return this->namepool_.add(name, NULL); }
f6ce93d6
ILT
868
869 // Possibly issue a warning for a reference to SYM at LOCATION which
870 // is in OBJ.
871 void
c06b7b0b 872 issue_warning(const Symbol* sym, const std::string& location) const
f6ce93d6
ILT
873 { this->warnings_.issue_warning(sym, location); }
874
a3ad94ed
ILT
875 // Set the dynamic symbol indexes. INDEX is the index of the first
876 // global dynamic symbol. Pointers to the symbols are stored into
877 // the vector. The names are stored into the Stringpool. This
878 // returns an updated dynamic symbol index.
879 unsigned int
14b31740
ILT
880 set_dynsym_indexes(const General_options*, const Target*, unsigned int index,
881 std::vector<Symbol*>*, Stringpool*, Versions*);
a3ad94ed 882
75f65a3e 883 // Finalize the symbol table after we have set the final addresses
c06b7b0b
ILT
884 // of all the input sections. This sets the final symbol indexes,
885 // values and adds the names to *POOL. INDEX is the index of the
16649710
ILT
886 // first global symbol. OFF is the file offset of the global symbol
887 // table, DYNOFF is the offset of the globals in the dynamic symbol
888 // table, DYN_GLOBAL_INDEX is the index of the first global dynamic
889 // symbol, and DYNCOUNT is the number of global dynamic symbols.
890 // This records the parameters, and returns the new file offset.
75f65a3e 891 off_t
16649710
ILT
892 finalize(unsigned int index, off_t off, off_t dynoff,
893 size_t dyn_global_index, size_t dyncount, Stringpool* pool);
1564db8d 894
61ba1cf9
ILT
895 // Write out the global symbols.
896 void
16649710
ILT
897 write_globals(const Target*, const Stringpool*, const Stringpool*,
898 Output_file*) const;
61ba1cf9 899
a3ad94ed
ILT
900 // Write out a section symbol. Return the updated offset.
901 void
902 write_section_symbol(const Target*, const Output_section*, Output_file*,
903 off_t) const;
904
bae7f79e
ILT
905 private:
906 Symbol_table(const Symbol_table&);
907 Symbol_table& operator=(const Symbol_table&);
908
a3ad94ed 909 // Set the size (32 or 64) of the symbols in the table.
14bfc3f5
ILT
910 void
911 set_size(int size)
912 { this->size_ = size; }
913
914 // Make FROM a forwarder symbol to TO.
915 void
916 make_forwarder(Symbol* from, Symbol* to);
917
918 // Add a symbol.
919 template<int size, bool big_endian>
920 Symbol*
f0641a0b
ILT
921 add_from_object(Object*, const char *name, Stringpool::Key name_key,
922 const char *version, Stringpool::Key version_key,
923 bool def, const elfcpp::Sym<size, big_endian>& sym);
14bfc3f5
ILT
924
925 // Resolve symbols.
926 template<int size, bool big_endian>
927 static void
1564db8d
ILT
928 resolve(Sized_symbol<size>* to,
929 const elfcpp::Sym<size, big_endian>& sym,
14b31740 930 Object*, const char* version);
14bfc3f5 931
1564db8d 932 template<int size, bool big_endian>
14bfc3f5 933 static void
14b31740
ILT
934 resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from,
935 const char* version ACCEPT_SIZE_ENDIAN);
936
86f2e683
ILT
937 // Whether we should override a symbol, based on flags in
938 // resolve.cc.
939 static bool
940 should_override(const Symbol*, unsigned int, bool*);
941
942 // Whether we should override a symbol with a special symbol which
943 // is automatically defined by the linker.
944 static bool
945 should_override_with_special(const Symbol*);
946
14b31740
ILT
947 // Define a special symbol.
948 template<int size, bool big_endian>
949 Sized_symbol<size>*
306d9ef0
ILT
950 define_special_symbol(const Target* target, const char** pname,
951 const char** pversion, bool only_if_ref,
86f2e683 952 Sized_symbol<size>** poldsym ACCEPT_SIZE_ENDIAN);
14bfc3f5 953
ead1e424
ILT
954 // Define a symbol in an Output_data, sized version.
955 template<int size>
14b31740
ILT
956 Sized_symbol<size>*
957 do_define_in_output_data(const Target*, const char* name,
958 const char* version, Output_data*,
ead1e424
ILT
959 typename elfcpp::Elf_types<size>::Elf_Addr value,
960 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
961 elfcpp::STT type, elfcpp::STB binding,
962 elfcpp::STV visibility, unsigned char nonvis,
963 bool offset_is_from_end, bool only_if_ref);
964
965 // Define a symbol in an Output_segment, sized version.
966 template<int size>
14b31740 967 Sized_symbol<size>*
ead1e424 968 do_define_in_output_segment(
14b31740 969 const Target*, const char* name, const char* version, Output_segment* os,
ead1e424
ILT
970 typename elfcpp::Elf_types<size>::Elf_Addr value,
971 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
972 elfcpp::STT type, elfcpp::STB binding,
973 elfcpp::STV visibility, unsigned char nonvis,
974 Symbol::Segment_offset_base offset_base, bool only_if_ref);
975
976 // Define a symbol as a constant, sized version.
977 template<int size>
14b31740 978 Sized_symbol<size>*
ead1e424 979 do_define_as_constant(
14b31740 980 const Target*, const char* name, const char* version,
ead1e424
ILT
981 typename elfcpp::Elf_types<size>::Elf_Addr value,
982 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
983 elfcpp::STT type, elfcpp::STB binding,
984 elfcpp::STV visibility, unsigned char nonvis,
985 bool only_if_ref);
986
987 // Allocate the common symbols, sized version.
988 template<int size>
989 void
990 do_allocate_commons(const General_options&, Layout*);
991
75f65a3e
ILT
992 // Finalize symbols specialized for size.
993 template<int size>
994 off_t
c06b7b0b 995 sized_finalize(unsigned int, off_t, Stringpool*);
75f65a3e 996
61ba1cf9
ILT
997 // Write globals specialized for size and endianness.
998 template<int size, bool big_endian>
999 void
16649710
ILT
1000 sized_write_globals(const Target*, const Stringpool*, const Stringpool*,
1001 Output_file*) const;
1002
1003 // Write out a symbol to P.
1004 template<int size, bool big_endian>
1005 void
1006 sized_write_symbol(Sized_symbol<size>*, unsigned int shndx,
6a469986
ILT
1007 const Stringpool*, unsigned char* p
1008 ACCEPT_SIZE_ENDIAN) const;
61ba1cf9 1009
a3ad94ed
ILT
1010 // Write out a section symbol, specialized for size and endianness.
1011 template<int size, bool big_endian>
1012 void
1013 sized_write_section_symbol(const Output_section*, Output_file*, off_t) const;
1014
54dc6425
ILT
1015 // The type of the symbol hash table.
1016
f0641a0b 1017 typedef std::pair<Stringpool::Key, Stringpool::Key> Symbol_table_key;
14bfc3f5
ILT
1018
1019 struct Symbol_table_hash
1020 {
1021 size_t
1022 operator()(const Symbol_table_key&) const;
1023 };
1024
1025 struct Symbol_table_eq
1026 {
1027 bool
1028 operator()(const Symbol_table_key&, const Symbol_table_key&) const;
1029 };
1030
1031 typedef Unordered_map<Symbol_table_key, Symbol*, Symbol_table_hash,
1032 Symbol_table_eq> Symbol_table_type;
1033
ead1e424
ILT
1034 // The type of the list of common symbols.
1035
1036 typedef std::vector<Symbol*> Commons_type;
1037
14bfc3f5
ILT
1038 // The size of the symbols in the symbol table (32 or 64).
1039 int size_;
1040
ead1e424
ILT
1041 // We increment this every time we see a new undefined symbol, for
1042 // use in archive groups.
1043 int saw_undefined_;
1044
c06b7b0b
ILT
1045 // The index of the first global symbol in the output file.
1046 unsigned int first_global_index_;
1047
75f65a3e
ILT
1048 // The file offset within the output symtab section where we should
1049 // write the table.
1050 off_t offset_;
1051
61ba1cf9
ILT
1052 // The number of global symbols we want to write out.
1053 size_t output_count_;
1054
16649710
ILT
1055 // The file offset of the global dynamic symbols, or 0 if none.
1056 off_t dynamic_offset_;
1057
1058 // The index of the first global dynamic symbol.
1059 unsigned int first_dynamic_global_index_;
1060
1061 // The number of global dynamic symbols, or 0 if none.
1062 off_t dynamic_count_;
1063
54dc6425 1064 // The symbol hash table.
14bfc3f5
ILT
1065 Symbol_table_type table_;
1066
54dc6425
ILT
1067 // A pool of symbol names. This is used for all global symbols.
1068 // Entries in the hash table point into this pool.
14bfc3f5 1069 Stringpool namepool_;
bae7f79e 1070
14bfc3f5 1071 // Forwarding symbols.
c06b7b0b 1072 Unordered_map<const Symbol*, Symbol*> forwarders_;
ead1e424
ILT
1073
1074 // We don't expect there to be very many common symbols, so we keep
1075 // a list of them. When we find a common symbol we add it to this
1076 // list. It is possible that by the time we process the list the
1077 // symbol is no longer a common symbol. It may also have become a
1078 // forwarder.
1079 Commons_type commons_;
f6ce93d6
ILT
1080
1081 // Manage symbol warnings.
1082 Warnings warnings_;
bae7f79e
ILT
1083};
1084
1564db8d
ILT
1085// We inline get_sized_symbol for efficiency.
1086
1087template<int size>
1088Sized_symbol<size>*
5482377d 1089Symbol_table::get_sized_symbol(Symbol* sym ACCEPT_SIZE) const
1564db8d 1090{
a3ad94ed 1091 gold_assert(size == this->get_size());
1564db8d
ILT
1092 return static_cast<Sized_symbol<size>*>(sym);
1093}
1094
1095template<int size>
1096const Sized_symbol<size>*
5482377d 1097Symbol_table::get_sized_symbol(const Symbol* sym ACCEPT_SIZE) const
1564db8d 1098{
a3ad94ed 1099 gold_assert(size == this->get_size());
1564db8d
ILT
1100 return static_cast<const Sized_symbol<size>*>(sym);
1101}
1102
bae7f79e
ILT
1103} // End namespace gold.
1104
1105#endif // !defined(GOLD_SYMTAB_H)
This page took 0.111273 seconds and 4 git commands to generate.