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