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