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