2009-02-27 Phil Muldoon <pmuldoon@redhat.com>
[deliverable/binutils-gdb.git] / gold / symtab.h
CommitLineData
bae7f79e
ILT
1// symtab.h -- the gold symbol table -*- C++ -*-
2
ebdbb458 3// Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
6cb15b7f
ILT
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 29
6d03d481 30#include "gc.h"
bae7f79e 31#include "elfcpp.h"
7e1edb90 32#include "parameters.h"
14bfc3f5 33#include "stringpool.h"
f6ce93d6 34#include "object.h"
bae7f79e
ILT
35
36#ifndef GOLD_SYMTAB_H
37#define GOLD_SYMTAB_H
38
39namespace gold
40{
41
7d9e3d98 42class Mapfile;
14bfc3f5 43class Object;
f6ce93d6 44class Relobj;
dbe717ef
ILT
45template<int size, bool big_endian>
46class Sized_relobj;
89fc3421
CC
47template<int size, bool big_endian>
48class Sized_pluginobj;
f6ce93d6 49class Dynobj;
dbe717ef
ILT
50template<int size, bool big_endian>
51class Sized_dynobj;
14b31740 52class Versions;
09124467 53class Version_script_info;
9a2d6984 54class Input_objects;
ead1e424 55class Output_data;
a3ad94ed 56class Output_section;
ead1e424 57class Output_segment;
61ba1cf9 58class Output_file;
d491d34e 59class Output_symtab_xindex;
6d03d481 60class Garbage_collection;
14bfc3f5 61
14bfc3f5
ILT
62// The base class of an entry in the symbol table. The symbol table
63// can have a lot of entries, so we don't want this class to big.
64// Size dependent fields can be found in the template class
65// Sized_symbol. Targets may support their own derived classes.
bae7f79e 66
bae7f79e
ILT
67class Symbol
68{
69 public:
ead1e424
ILT
70 // Because we want the class to be small, we don't use any virtual
71 // functions. But because symbols can be defined in different
72 // places, we need to classify them. This enum is the different
73 // sources of symbols we support.
74 enum Source
75 {
f6ce93d6
ILT
76 // Symbol defined in a relocatable or dynamic input file--this is
77 // the most common case.
ead1e424
ILT
78 FROM_OBJECT,
79 // Symbol defined in an Output_data, a special section created by
80 // the target.
81 IN_OUTPUT_DATA,
82 // Symbol defined in an Output_segment, with no associated
83 // section.
84 IN_OUTPUT_SEGMENT,
85 // Symbol value is constant.
f3e9c5c5
ILT
86 IS_CONSTANT,
87 // Symbol is undefined.
88 IS_UNDEFINED
ead1e424
ILT
89 };
90
91 // When the source is IN_OUTPUT_SEGMENT, we need to describe what
92 // the offset means.
93 enum Segment_offset_base
94 {
95 // From the start of the segment.
96 SEGMENT_START,
97 // From the end of the segment.
98 SEGMENT_END,
99 // From the filesz of the segment--i.e., after the loaded bytes
100 // but before the bytes which are allocated but zeroed.
101 SEGMENT_BSS
102 };
103
14bfc3f5
ILT
104 // Return the symbol name.
105 const char*
106 name() const
107 { return this->name_; }
108
a2b1aa12
ILT
109 // Return the (ANSI) demangled version of the name, if
110 // parameters.demangle() is true. Otherwise, return the name. This
111 // is intended to be used only for logging errors, so it's not
112 // super-efficient.
113 std::string
114 demangled_name() const;
115
14bfc3f5
ILT
116 // Return the symbol version. This will return NULL for an
117 // unversioned symbol.
118 const char*
119 version() const
120 { return this->version_; }
121
09124467
ILT
122 // Return whether this version is the default for this symbol name
123 // (eg, "foo@@V2" is a default version; "foo@V1" is not). Only
124 // meaningful for versioned symbols.
125 bool
126 is_default() const
127 {
128 gold_assert(this->version_ != NULL);
129 return this->is_def_;
130 }
131
be3e6201 132 // Set that this version is the default for this symbol name.
09124467 133 void
be3e6201
ILT
134 set_is_default()
135 { this->is_def_ = true; }
09124467 136
ead1e424
ILT
137 // Return the symbol source.
138 Source
139 source() const
140 { return this->source_; }
141
14bfc3f5
ILT
142 // Return the object with which this symbol is associated.
143 Object*
144 object() const
ead1e424 145 {
a3ad94ed 146 gold_assert(this->source_ == FROM_OBJECT);
ead1e424
ILT
147 return this->u_.from_object.object;
148 }
149
f6ce93d6
ILT
150 // Return the index of the section in the input relocatable or
151 // dynamic object file.
ead1e424 152 unsigned int
d491d34e 153 shndx(bool* is_ordinary) const
ead1e424 154 {
a3ad94ed 155 gold_assert(this->source_ == FROM_OBJECT);
d491d34e 156 *is_ordinary = this->is_ordinary_shndx_;
16649710 157 return this->u_.from_object.shndx;
ead1e424
ILT
158 }
159
160 // Return the output data section with which this symbol is
161 // associated, if the symbol was specially defined with respect to
162 // an output data section.
163 Output_data*
164 output_data() const
165 {
a3ad94ed 166 gold_assert(this->source_ == IN_OUTPUT_DATA);
ead1e424
ILT
167 return this->u_.in_output_data.output_data;
168 }
169
170 // If this symbol was defined with respect to an output data
171 // section, return whether the value is an offset from end.
172 bool
173 offset_is_from_end() const
174 {
a3ad94ed 175 gold_assert(this->source_ == IN_OUTPUT_DATA);
ead1e424
ILT
176 return this->u_.in_output_data.offset_is_from_end;
177 }
178
179 // Return the output segment with which this symbol is associated,
180 // if the symbol was specially defined with respect to an output
181 // segment.
182 Output_segment*
183 output_segment() const
184 {
a3ad94ed 185 gold_assert(this->source_ == IN_OUTPUT_SEGMENT);
ead1e424
ILT
186 return this->u_.in_output_segment.output_segment;
187 }
188
189 // If this symbol was defined with respect to an output segment,
190 // return the offset base.
191 Segment_offset_base
192 offset_base() const
193 {
a3ad94ed 194 gold_assert(this->source_ == IN_OUTPUT_SEGMENT);
ead1e424
ILT
195 return this->u_.in_output_segment.offset_base;
196 }
14bfc3f5
ILT
197
198 // Return the symbol binding.
199 elfcpp::STB
200 binding() const
201 { return this->binding_; }
202
1564db8d
ILT
203 // Return the symbol type.
204 elfcpp::STT
205 type() const
206 { return this->type_; }
207
208 // Return the symbol visibility.
209 elfcpp::STV
210 visibility() const
211 { return this->visibility_; }
212
213 // Return the non-visibility part of the st_other field.
214 unsigned char
ead1e424
ILT
215 nonvis() const
216 { return this->nonvis_; }
14bfc3f5 217
1564db8d
ILT
218 // Return whether this symbol is a forwarder. This will never be
219 // true of a symbol found in the hash table, but may be true of
220 // symbol pointers attached to object files.
221 bool
222 is_forwarder() const
223 { return this->is_forwarder_; }
224
225 // Mark this symbol as a forwarder.
226 void
227 set_forwarder()
228 { this->is_forwarder_ = true; }
229
aeddab66
ILT
230 // Return whether this symbol has an alias in the weak aliases table
231 // in Symbol_table.
232 bool
233 has_alias() const
234 { return this->has_alias_; }
235
236 // Mark this symbol as having an alias.
237 void
238 set_has_alias()
239 { this->has_alias_ = true; }
240
c06b7b0b
ILT
241 // Return whether this symbol needs an entry in the dynamic symbol
242 // table.
243 bool
244 needs_dynsym_entry() const
429c1569
ILT
245 {
246 return (this->needs_dynsym_entry_
247 || (this->in_reg() && this->in_dyn()));
248 }
c06b7b0b
ILT
249
250 // Mark this symbol as needing an entry in the dynamic symbol table.
251 void
252 set_needs_dynsym_entry()
253 { this->needs_dynsym_entry_ = true; }
254
436ca963
ILT
255 // Return whether this symbol should be added to the dynamic symbol
256 // table.
257 bool
258 should_add_dynsym_entry() const;
259
008db82e
ILT
260 // Return whether this symbol has been seen in a regular object.
261 bool
262 in_reg() const
263 { return this->in_reg_; }
264
265 // Mark this symbol as having been seen in a regular object.
266 void
267 set_in_reg()
268 { this->in_reg_ = true; }
269
1ebd95fd
ILT
270 // Return whether this symbol has been seen in a dynamic object.
271 bool
272 in_dyn() const
273 { return this->in_dyn_; }
274
f6ce93d6 275 // Mark this symbol as having been seen in a dynamic object.
1564db8d
ILT
276 void
277 set_in_dyn()
278 { this->in_dyn_ = true; }
279
89fc3421
CC
280 // Return whether this symbol has been seen in a real ELF object.
281 // (IN_REG will return TRUE if the symbol has been seen in either
282 // a real ELF object or an object claimed by a plugin.)
283 bool
284 in_real_elf() const
285 { return this->in_real_elf_; }
286
287 // Mark this symbol as having been seen in a real ELF object.
288 void
289 set_in_real_elf()
290 { this->in_real_elf_ = true; }
291
c06b7b0b
ILT
292 // Return the index of this symbol in the output file symbol table.
293 // A value of -1U means that this symbol is not going into the
294 // output file. This starts out as zero, and is set to a non-zero
295 // value by Symbol_table::finalize. It is an error to ask for the
296 // symbol table index before it has been set.
297 unsigned int
298 symtab_index() const
299 {
a3ad94ed 300 gold_assert(this->symtab_index_ != 0);
c06b7b0b
ILT
301 return this->symtab_index_;
302 }
303
304 // Set the index of the symbol in the output file symbol table.
305 void
306 set_symtab_index(unsigned int index)
307 {
a3ad94ed 308 gold_assert(index != 0);
c06b7b0b
ILT
309 this->symtab_index_ = index;
310 }
311
a3ad94ed
ILT
312 // Return whether this symbol already has an index in the output
313 // file symbol table.
314 bool
315 has_symtab_index() const
316 { return this->symtab_index_ != 0; }
317
c06b7b0b
ILT
318 // Return the index of this symbol in the dynamic symbol table. A
319 // value of -1U means that this symbol is not going into the dynamic
320 // symbol table. This starts out as zero, and is set to a non-zero
321 // during Layout::finalize. It is an error to ask for the dynamic
322 // symbol table index before it has been set.
323 unsigned int
324 dynsym_index() const
325 {
a3ad94ed 326 gold_assert(this->dynsym_index_ != 0);
c06b7b0b
ILT
327 return this->dynsym_index_;
328 }
329
330 // Set the index of the symbol in the dynamic symbol table.
331 void
332 set_dynsym_index(unsigned int index)
333 {
a3ad94ed 334 gold_assert(index != 0);
c06b7b0b
ILT
335 this->dynsym_index_ = index;
336 }
337
16649710
ILT
338 // Return whether this symbol already has an index in the dynamic
339 // symbol table.
340 bool
341 has_dynsym_index() const
342 { return this->dynsym_index_ != 0; }
343
ead1e424 344 // Return whether this symbol has an entry in the GOT section.
07f397ab 345 // For a TLS symbol, this GOT entry will hold its tp-relative offset.
92e059d8 346 bool
0a65a3a7
CC
347 has_got_offset(unsigned int got_type) const
348 { return this->got_offsets_.get_offset(got_type) != -1U; }
ead1e424
ILT
349
350 // Return the offset into the GOT section of this symbol.
351 unsigned int
0a65a3a7 352 got_offset(unsigned int got_type) const
ead1e424 353 {
0a65a3a7
CC
354 unsigned int got_offset = this->got_offsets_.get_offset(got_type);
355 gold_assert(got_offset != -1U);
356 return got_offset;
ead1e424
ILT
357 }
358
359 // Set the GOT offset of this symbol.
360 void
0a65a3a7
CC
361 set_got_offset(unsigned int got_type, unsigned int got_offset)
362 { this->got_offsets_.set_offset(got_type, got_offset); }
07f397ab 363
a3ad94ed 364 // Return whether this symbol has an entry in the PLT section.
ead1e424 365 bool
a3ad94ed
ILT
366 has_plt_offset() const
367 { return this->has_plt_offset_; }
368
369 // Return the offset into the PLT section of this symbol.
370 unsigned int
371 plt_offset() const
372 {
373 gold_assert(this->has_plt_offset());
374 return this->plt_offset_;
375 }
376
377 // Set the PLT offset of this symbol.
378 void
379 set_plt_offset(unsigned int plt_offset)
380 {
381 this->has_plt_offset_ = true;
382 this->plt_offset_ = plt_offset;
383 }
384
ab5c9e90
ILT
385 // Return whether this dynamic symbol needs a special value in the
386 // dynamic symbol table.
387 bool
388 needs_dynsym_value() const
389 { return this->needs_dynsym_value_; }
390
391 // Set that this dynamic symbol needs a special value in the dynamic
392 // symbol table.
393 void
394 set_needs_dynsym_value()
395 {
396 gold_assert(this->object()->is_dynamic());
397 this->needs_dynsym_value_ = true;
398 }
399
a3ad94ed
ILT
400 // Return true if the final value of this symbol is known at link
401 // time.
402 bool
b3b74ddc 403 final_value_is_known() const;
ead1e424 404
f6ce93d6
ILT
405 // Return whether this is a defined symbol (not undefined or
406 // common).
407 bool
408 is_defined() const
409 {
d491d34e
ILT
410 bool is_ordinary;
411 if (this->source_ != FROM_OBJECT)
f3e9c5c5 412 return this->source_ != IS_UNDEFINED;
d491d34e
ILT
413 unsigned int shndx = this->shndx(&is_ordinary);
414 return (is_ordinary
415 ? shndx != elfcpp::SHN_UNDEF
416 : shndx != elfcpp::SHN_COMMON);
a3ad94ed
ILT
417 }
418
14b31740 419 // Return true if this symbol is from a dynamic object.
a3ad94ed 420 bool
14b31740 421 is_from_dynobj() const
a3ad94ed 422 {
14b31740 423 return this->source_ == FROM_OBJECT && this->object()->is_dynamic();
f6ce93d6
ILT
424 }
425
ead1e424
ILT
426 // Return whether this is an undefined symbol.
427 bool
428 is_undefined() const
429 {
d491d34e 430 bool is_ordinary;
f3e9c5c5
ILT
431 return ((this->source_ == FROM_OBJECT
432 && this->shndx(&is_ordinary) == elfcpp::SHN_UNDEF
433 && is_ordinary)
434 || this->source_ == IS_UNDEFINED);
ead1e424
ILT
435 }
436
86925eef
CC
437 // Return whether this is a weak undefined symbol.
438 bool
439 is_weak_undefined() const
f3e9c5c5 440 { return this->is_undefined() && this->binding() == elfcpp::STB_WEAK; }
86925eef 441
86925eef
CC
442 // Return whether this is an absolute symbol.
443 bool
444 is_absolute() const
445 {
d491d34e 446 bool is_ordinary;
f3e9c5c5
ILT
447 return ((this->source_ == FROM_OBJECT
448 && this->shndx(&is_ordinary) == elfcpp::SHN_ABS
449 && !is_ordinary)
450 || this->source_ == IS_CONSTANT);
86925eef
CC
451 }
452
ead1e424
ILT
453 // Return whether this is a common symbol.
454 bool
455 is_common() const
456 {
d491d34e 457 bool is_ordinary;
f6ce93d6 458 return (this->source_ == FROM_OBJECT
d491d34e
ILT
459 && ((this->shndx(&is_ordinary) == elfcpp::SHN_COMMON
460 && !is_ordinary)
f6ce93d6 461 || this->type_ == elfcpp::STT_COMMON));
ead1e424 462 }
92e059d8 463
a6badf5a
ILT
464 // Return whether this symbol can be seen outside this object.
465 bool
466 is_externally_visible() const
467 {
468 return (this->visibility_ == elfcpp::STV_DEFAULT
469 || this->visibility_ == elfcpp::STV_PROTECTED);
470 }
471
436ca963
ILT
472 // Return true if this symbol can be preempted by a definition in
473 // another link unit.
474 bool
475 is_preemptible() const
476 {
386c048c
ILT
477 // It doesn't make sense to ask whether a symbol defined in
478 // another object is preemptible.
479 gold_assert(!this->is_from_dynobj());
480
8fc19601
ILT
481 // It doesn't make sense to ask whether an undefined symbol
482 // is preemptible.
483 gold_assert(!this->is_undefined());
484
f1f70eae
ILT
485 // If a symbol does not have default visibility, it can not be
486 // seen outside this link unit and therefore is not preemptible.
487 if (this->visibility_ != elfcpp::STV_DEFAULT)
488 return false;
489
490 // If this symbol has been forced to be a local symbol by a
491 // version script, then it is not visible outside this link unit
492 // and is not preemptible.
493 if (this->is_forced_local_)
494 return false;
495
496 // If we are not producing a shared library, then nothing is
497 // preemptible.
498 if (!parameters->options().shared())
499 return false;
500
501 // If the user used -Bsymbolic, then nothing is preemptible.
502 if (parameters->options().Bsymbolic())
503 return false;
504
505 // If the user used -Bsymbolic-functions, then functions are not
506 // preemptible. We explicitly check for not being STT_OBJECT,
507 // rather than for being STT_FUNC, because that is what the GNU
508 // linker does.
509 if (this->type() != elfcpp::STT_OBJECT
510 && parameters->options().Bsymbolic_functions())
511 return false;
512
513 // Otherwise the symbol is preemptible.
514 return true;
436ca963
ILT
515 }
516
d61c6bd4
ILT
517 // Return true if this symbol is a function that needs a PLT entry.
518 // If the symbol is defined in a dynamic object or if it is subject
8fc19601
ILT
519 // to pre-emption, we need to make a PLT entry. If we're doing a
520 // static link, we don't create PLT entries.
d61c6bd4
ILT
521 bool
522 needs_plt_entry() const
523 {
8fc19601
ILT
524 return (!parameters->doing_static_link()
525 && this->type() == elfcpp::STT_FUNC
526 && (this->is_from_dynobj()
f3c69fca 527 || this->is_undefined()
8fc19601 528 || this->is_preemptible()));
d61c6bd4
ILT
529 }
530
0700cf32
ILT
531 // When determining whether a reference to a symbol needs a dynamic
532 // relocation, we need to know several things about the reference.
533 // These flags may be or'ed together.
534 enum Reference_flags
535 {
536 // Reference to the symbol's absolute address.
537 ABSOLUTE_REF = 1,
538 // A non-PIC reference.
539 NON_PIC_REF = 2,
540 // A function call.
541 FUNCTION_CALL = 4
542 };
543
d61c6bd4
ILT
544 // Given a direct absolute or pc-relative static relocation against
545 // the global symbol, this function returns whether a dynamic relocation
546 // is needed.
547
548 bool
0700cf32 549 needs_dynamic_reloc(int flags) const
d61c6bd4 550 {
8fc19601
ILT
551 // No dynamic relocations in a static link!
552 if (parameters->doing_static_link())
553 return false;
554
f3c69fca
CC
555 // A reference to a weak undefined symbol from an executable should be
556 // statically resolved to 0, and does not need a dynamic relocation.
557 // This matches gnu ld behavior.
558 if (this->is_weak_undefined() && !parameters->options().shared())
559 return false;
560
561 // A reference to an absolute symbol does not need a dynamic relocation.
562 if (this->is_absolute())
86925eef
CC
563 return false;
564
d61c6bd4 565 // An absolute reference within a position-independent output file
0700cf32
ILT
566 // will need a dynamic relocation.
567 if ((flags & ABSOLUTE_REF)
8851ecca 568 && parameters->options().output_is_position_independent())
d61c6bd4
ILT
569 return true;
570
571 // A function call that can branch to a local PLT entry does not need
5240d12a
ILT
572 // a dynamic relocation. A non-pic pc-relative function call in a
573 // shared library cannot use a PLT entry.
0700cf32 574 if ((flags & FUNCTION_CALL)
5240d12a 575 && this->has_plt_offset()
8851ecca 576 && !((flags & NON_PIC_REF) && parameters->options().shared()))
d61c6bd4
ILT
577 return false;
578
579 // A reference to any PLT entry in a non-position-independent executable
580 // does not need a dynamic relocation.
8851ecca 581 if (!parameters->options().output_is_position_independent()
d61c6bd4
ILT
582 && this->has_plt_offset())
583 return false;
584
585 // A reference to a symbol defined in a dynamic object or to a
586 // symbol that is preemptible will need a dynamic relocation.
8fc19601
ILT
587 if (this->is_from_dynobj()
588 || this->is_undefined()
589 || this->is_preemptible())
d61c6bd4
ILT
590 return true;
591
592 // For all other cases, return FALSE.
593 return false;
594 }
595
de4c45bd
ILT
596 // Whether we should use the PLT offset associated with a symbol for
597 // a relocation. IS_NON_PIC_REFERENCE is true if this is a non-PIC
598 // reloc--the same set of relocs for which we would pass NON_PIC_REF
599 // to the needs_dynamic_reloc function.
600
601 bool
602 use_plt_offset(bool is_non_pic_reference) const
603 {
604 // If the symbol doesn't have a PLT offset, then naturally we
605 // don't want to use it.
606 if (!this->has_plt_offset())
607 return false;
608
609 // If we are going to generate a dynamic relocation, then we will
610 // wind up using that, so no need to use the PLT entry.
611 if (this->needs_dynamic_reloc(FUNCTION_CALL
612 | (is_non_pic_reference
613 ? NON_PIC_REF
614 : 0)))
615 return false;
616
617 // If the symbol is from a dynamic object, we need to use the PLT
618 // entry.
619 if (this->is_from_dynobj())
620 return true;
621
622 // If we are generating a shared object, and this symbol is
623 // undefined or preemptible, we need to use the PLT entry.
624 if (parameters->options().shared()
625 && (this->is_undefined() || this->is_preemptible()))
626 return true;
627
628 // If this is a weak undefined symbol, we need to use the PLT
629 // entry; the symbol may be defined by a library loaded at
630 // runtime.
631 if (this->is_weak_undefined())
632 return true;
633
634 // Otherwise we can use the regular definition.
635 return false;
636 }
637
d61c6bd4
ILT
638 // Given a direct absolute static relocation against
639 // the global symbol, where a dynamic relocation is needed, this
640 // function returns whether a relative dynamic relocation can be used.
641 // The caller must determine separately whether the static relocation
642 // is compatible with a relative relocation.
643
644 bool
645 can_use_relative_reloc(bool is_function_call) const
646 {
647 // A function call that can branch to a local PLT entry can
648 // use a RELATIVE relocation.
649 if (is_function_call && this->has_plt_offset())
650 return true;
651
652 // A reference to a symbol defined in a dynamic object or to a
653 // symbol that is preemptible can not use a RELATIVE relocaiton.
8fc19601
ILT
654 if (this->is_from_dynobj()
655 || this->is_undefined()
656 || this->is_preemptible())
d61c6bd4
ILT
657 return false;
658
659 // For all other cases, return TRUE.
660 return true;
661 }
662
77e65537
ILT
663 // Return the output section where this symbol is defined. Return
664 // NULL if the symbol has an absolute value.
665 Output_section*
666 output_section() const;
667
668 // Set the symbol's output section. This is used for symbols
669 // defined in scripts. This should only be called after the symbol
670 // table has been finalized.
671 void
672 set_output_section(Output_section*);
a445fddf 673
f6ce93d6
ILT
674 // Return whether there should be a warning for references to this
675 // symbol.
676 bool
677 has_warning() const
678 { return this->has_warning_; }
679
680 // Mark this symbol as having a warning.
681 void
682 set_has_warning()
683 { this->has_warning_ = true; }
684
46fe1623
ILT
685 // Return whether this symbol is defined by a COPY reloc from a
686 // dynamic object.
687 bool
688 is_copied_from_dynobj() const
689 { return this->is_copied_from_dynobj_; }
690
691 // Mark this symbol as defined by a COPY reloc.
692 void
693 set_is_copied_from_dynobj()
694 { this->is_copied_from_dynobj_ = true; }
695
55a93433
ILT
696 // Return whether this symbol is forced to visibility STB_LOCAL
697 // by a "local:" entry in a version script.
698 bool
699 is_forced_local() const
700 { return this->is_forced_local_; }
701
702 // Mark this symbol as forced to STB_LOCAL visibility.
703 void
704 set_is_forced_local()
705 { this->is_forced_local_ = true; }
706
14bfc3f5
ILT
707 protected:
708 // Instances of this class should always be created at a specific
709 // size.
710 Symbol()
f6ce93d6 711 { memset(this, 0, sizeof *this); }
14bfc3f5 712
ead1e424
ILT
713 // Initialize the general fields.
714 void
715 init_fields(const char* name, const char* version,
716 elfcpp::STT type, elfcpp::STB binding,
717 elfcpp::STV visibility, unsigned char nonvis);
718
d491d34e
ILT
719 // Initialize fields from an ELF symbol in OBJECT. ST_SHNDX is the
720 // section index, IS_ORDINARY is whether it is a normal section
721 // index rather than a special code.
14bfc3f5
ILT
722 template<int size, bool big_endian>
723 void
f3e9c5c5
ILT
724 init_base_object(const char *name, const char* version, Object* object,
725 const elfcpp::Sym<size, big_endian>&, unsigned int st_shndx,
726 bool is_ordinary);
bae7f79e 727
ead1e424
ILT
728 // Initialize fields for an Output_data.
729 void
75517b77
ILT
730 init_base_output_data(const char* name, const char* version, Output_data*,
731 elfcpp::STT, elfcpp::STB, elfcpp::STV,
732 unsigned char nonvis, bool offset_is_from_end);
ead1e424
ILT
733
734 // Initialize fields for an Output_segment.
735 void
75517b77
ILT
736 init_base_output_segment(const char* name, const char* version,
737 Output_segment* os, elfcpp::STT type,
738 elfcpp::STB binding, elfcpp::STV visibility,
739 unsigned char nonvis,
f3e9c5c5 740 Segment_offset_base offset_base);
ead1e424
ILT
741
742 // Initialize fields for a constant.
743 void
75517b77
ILT
744 init_base_constant(const char* name, const char* version, elfcpp::STT type,
745 elfcpp::STB binding, elfcpp::STV visibility,
746 unsigned char nonvis);
f3e9c5c5
ILT
747
748 // Initialize fields for an undefined symbol.
749 void
75517b77
ILT
750 init_base_undefined(const char* name, const char* version, elfcpp::STT type,
751 elfcpp::STB binding, elfcpp::STV visibility,
752 unsigned char nonvis);
ead1e424 753
1564db8d
ILT
754 // Override existing symbol.
755 template<int size, bool big_endian>
756 void
d491d34e
ILT
757 override_base(const elfcpp::Sym<size, big_endian>&, unsigned int st_shndx,
758 bool is_ordinary, Object* object, const char* version);
1564db8d 759
86f2e683
ILT
760 // Override existing symbol with a special symbol.
761 void
762 override_base_with_special(const Symbol* from);
763
75517b77
ILT
764 // Override symbol version.
765 void
766 override_version(const char* version);
767
c7912668
ILT
768 // Allocate a common symbol by giving it a location in the output
769 // file.
770 void
771 allocate_base_common(Output_data*);
772
bae7f79e 773 private:
14bfc3f5
ILT
774 Symbol(const Symbol&);
775 Symbol& operator=(const Symbol&);
776
777 // Symbol name (expected to point into a Stringpool).
778 const char* name_;
779 // Symbol version (expected to point into a Stringpool). This may
780 // be NULL.
bae7f79e 781 const char* version_;
ead1e424
ILT
782
783 union
784 {
785 // This struct is used if SOURCE_ == FROM_OBJECT.
786 struct
787 {
788 // Object in which symbol is defined, or in which it was first
789 // seen.
790 Object* object;
791 // Section number in object_ in which symbol is defined.
16649710 792 unsigned int shndx;
ead1e424
ILT
793 } from_object;
794
795 // This struct is used if SOURCE_ == IN_OUTPUT_DATA.
796 struct
797 {
798 // Output_data in which symbol is defined. Before
799 // Layout::finalize the symbol's value is an offset within the
800 // Output_data.
801 Output_data* output_data;
802 // True if the offset is from the end, false if the offset is
803 // from the beginning.
804 bool offset_is_from_end;
805 } in_output_data;
806
807 // This struct is used if SOURCE_ == IN_OUTPUT_SEGMENT.
808 struct
809 {
810 // Output_segment in which the symbol is defined. Before
811 // Layout::finalize the symbol's value is an offset.
812 Output_segment* output_segment;
813 // The base to use for the offset before Layout::finalize.
814 Segment_offset_base offset_base;
815 } in_output_segment;
816 } u_;
817
c06b7b0b
ILT
818 // The index of this symbol in the output file. If the symbol is
819 // not going into the output file, this value is -1U. This field
820 // starts as always holding zero. It is set to a non-zero value by
821 // Symbol_table::finalize.
822 unsigned int symtab_index_;
823
824 // The index of this symbol in the dynamic symbol table. If the
825 // symbol is not going into the dynamic symbol table, this value is
826 // -1U. This field starts as always holding zero. It is set to a
827 // non-zero value during Layout::finalize.
828 unsigned int dynsym_index_;
829
ead1e424 830 // If this symbol has an entry in the GOT section (has_got_offset_
0a65a3a7
CC
831 // is true), this holds the offset from the start of the GOT section.
832 // A symbol may have more than one GOT offset (e.g., when mixing
833 // modules compiled with two different TLS models), but will usually
834 // have at most one.
835 Got_offset_list got_offsets_;
07f397ab 836
a3ad94ed
ILT
837 // If this symbol has an entry in the PLT section (has_plt_offset_
838 // is true), then this is the offset from the start of the PLT
839 // section.
840 unsigned int plt_offset_;
841
d491d34e 842 // Symbol type (bits 0 to 3).
bae7f79e 843 elfcpp::STT type_ : 4;
d491d34e 844 // Symbol binding (bits 4 to 7).
bae7f79e 845 elfcpp::STB binding_ : 4;
d491d34e 846 // Symbol visibility (bits 8 to 9).
14bfc3f5 847 elfcpp::STV visibility_ : 2;
d491d34e 848 // Rest of symbol st_other field (bits 10 to 15).
ead1e424 849 unsigned int nonvis_ : 6;
d491d34e 850 // The type of symbol (bits 16 to 18).
f6ce93d6 851 Source source_ : 3;
14bfc3f5 852 // True if this symbol always requires special target-specific
d491d34e 853 // handling (bit 19).
ead1e424 854 bool is_target_special_ : 1;
d491d34e 855 // True if this is the default version of the symbol (bit 20).
1564db8d 856 bool is_def_ : 1;
14bfc3f5
ILT
857 // True if this symbol really forwards to another symbol. This is
858 // used when we discover after the fact that two different entries
859 // in the hash table really refer to the same symbol. This will
860 // never be set for a symbol found in the hash table, but may be set
861 // for a symbol found in the list of symbols attached to an Object.
862 // It forwards to the symbol found in the forwarders_ map of
d491d34e 863 // Symbol_table (bit 21).
1564db8d 864 bool is_forwarder_ : 1;
aeddab66 865 // True if the symbol has an alias in the weak_aliases table in
d491d34e 866 // Symbol_table (bit 22).
aeddab66 867 bool has_alias_ : 1;
d491d34e
ILT
868 // True if this symbol needs to be in the dynamic symbol table (bit
869 // 23).
c06b7b0b 870 bool needs_dynsym_entry_ : 1;
d491d34e 871 // True if we've seen this symbol in a regular object (bit 24).
008db82e 872 bool in_reg_ : 1;
d491d34e 873 // True if we've seen this symbol in a dynamic object (bit 25).
1564db8d 874 bool in_dyn_ : 1;
d491d34e 875 // True if the symbol has an entry in the PLT section (bit 26).
a3ad94ed 876 bool has_plt_offset_ : 1;
ab5c9e90 877 // True if this is a dynamic symbol which needs a special value in
d491d34e 878 // the dynamic symbol table (bit 27).
ab5c9e90 879 bool needs_dynsym_value_ : 1;
d491d34e 880 // True if there is a warning for this symbol (bit 28).
f6ce93d6 881 bool has_warning_ : 1;
46fe1623 882 // True if we are using a COPY reloc for this symbol, so that the
d491d34e 883 // real definition lives in a dynamic object (bit 29).
46fe1623 884 bool is_copied_from_dynobj_ : 1;
55a93433 885 // True if this symbol was forced to local visibility by a version
d491d34e 886 // script (bit 30).
55a93433 887 bool is_forced_local_ : 1;
d491d34e
ILT
888 // True if the field u_.from_object.shndx is an ordinary section
889 // index, not one of the special codes from SHN_LORESERVE to
89fc3421 890 // SHN_HIRESERVE (bit 31).
d491d34e 891 bool is_ordinary_shndx_ : 1;
89fc3421
CC
892 // True if we've seen this symbol in a real ELF object.
893 bool in_real_elf_ : 1;
bae7f79e
ILT
894};
895
14bfc3f5
ILT
896// The parts of a symbol which are size specific. Using a template
897// derived class like this helps us use less space on a 32-bit system.
bae7f79e
ILT
898
899template<int size>
14bfc3f5
ILT
900class Sized_symbol : public Symbol
901{
902 public:
1564db8d
ILT
903 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value_type;
904 typedef typename elfcpp::Elf_types<size>::Elf_WXword Size_type;
905
14bfc3f5
ILT
906 Sized_symbol()
907 { }
908
d491d34e
ILT
909 // Initialize fields from an ELF symbol in OBJECT. ST_SHNDX is the
910 // section index, IS_ORDINARY is whether it is a normal section
911 // index rather than a special code.
14bfc3f5
ILT
912 template<bool big_endian>
913 void
f3e9c5c5
ILT
914 init_object(const char *name, const char* version, Object* object,
915 const elfcpp::Sym<size, big_endian>&, unsigned int st_shndx,
916 bool is_ordinary);
14bfc3f5 917
ead1e424
ILT
918 // Initialize fields for an Output_data.
919 void
75517b77
ILT
920 init_output_data(const char* name, const char* version, Output_data*,
921 Value_type value, Size_type symsize, elfcpp::STT,
922 elfcpp::STB, elfcpp::STV, unsigned char nonvis,
923 bool offset_is_from_end);
ead1e424
ILT
924
925 // Initialize fields for an Output_segment.
926 void
75517b77
ILT
927 init_output_segment(const char* name, const char* version, Output_segment*,
928 Value_type value, Size_type symsize, elfcpp::STT,
929 elfcpp::STB, elfcpp::STV, unsigned char nonvis,
930 Segment_offset_base offset_base);
ead1e424
ILT
931
932 // Initialize fields for a constant.
933 void
75517b77
ILT
934 init_constant(const char* name, const char* version, Value_type value,
935 Size_type symsize, elfcpp::STT, elfcpp::STB, elfcpp::STV,
936 unsigned char nonvis);
f3e9c5c5
ILT
937
938 // Initialize fields for an undefined symbol.
939 void
75517b77
ILT
940 init_undefined(const char* name, const char* version, elfcpp::STT,
941 elfcpp::STB, elfcpp::STV, unsigned char nonvis);
ead1e424 942
1564db8d
ILT
943 // Override existing symbol.
944 template<bool big_endian>
945 void
d491d34e
ILT
946 override(const elfcpp::Sym<size, big_endian>&, unsigned int st_shndx,
947 bool is_ordinary, Object* object, const char* version);
1564db8d 948
86f2e683
ILT
949 // Override existing symbol with a special symbol.
950 void
951 override_with_special(const Sized_symbol<size>*);
952
1564db8d
ILT
953 // Return the symbol's value.
954 Value_type
955 value() const
956 { return this->value_; }
957
958 // Return the symbol's size (we can't call this 'size' because that
959 // is a template parameter).
960 Size_type
961 symsize() const
ead1e424
ILT
962 { return this->symsize_; }
963
964 // Set the symbol size. This is used when resolving common symbols.
965 void
966 set_symsize(Size_type symsize)
967 { this->symsize_ = symsize; }
1564db8d 968
75f65a3e
ILT
969 // Set the symbol value. This is called when we store the final
970 // values of the symbols into the symbol table.
971 void
972 set_value(Value_type value)
973 { this->value_ = value; }
974
c7912668
ILT
975 // Allocate a common symbol by giving it a location in the output
976 // file.
977 void
978 allocate_common(Output_data*, Value_type value);
979
14bfc3f5
ILT
980 private:
981 Sized_symbol(const Sized_symbol&);
982 Sized_symbol& operator=(const Sized_symbol&);
983
ead1e424
ILT
984 // Symbol value. Before Layout::finalize this is the offset in the
985 // input section. This is set to the final value during
986 // Layout::finalize.
1564db8d 987 Value_type value_;
14bfc3f5 988 // Symbol size.
ead1e424
ILT
989 Size_type symsize_;
990};
991
992// A struct describing a symbol defined by the linker, where the value
993// of the symbol is defined based on an output section. This is used
994// for symbols defined by the linker, like "_init_array_start".
995
996struct Define_symbol_in_section
997{
998 // The symbol name.
999 const char* name;
1000 // The name of the output section with which this symbol should be
1001 // associated. If there is no output section with that name, the
1002 // symbol will be defined as zero.
1003 const char* output_section;
1004 // The offset of the symbol within the output section. This is an
1005 // offset from the start of the output section, unless start_at_end
1006 // is true, in which case this is an offset from the end of the
1007 // output section.
1008 uint64_t value;
1009 // The size of the symbol.
1010 uint64_t size;
1011 // The symbol type.
1012 elfcpp::STT type;
1013 // The symbol binding.
1014 elfcpp::STB binding;
1015 // The symbol visibility.
1016 elfcpp::STV visibility;
1017 // The rest of the st_other field.
1018 unsigned char nonvis;
1019 // If true, the value field is an offset from the end of the output
1020 // section.
1021 bool offset_is_from_end;
1022 // If true, this symbol is defined only if we see a reference to it.
1023 bool only_if_ref;
1024};
1025
1026// A struct describing a symbol defined by the linker, where the value
1027// of the symbol is defined based on a segment. This is used for
1028// symbols defined by the linker, like "_end". We describe the
1029// segment with which the symbol should be associated by its
1030// characteristics. If no segment meets these characteristics, the
1031// symbol will be defined as zero. If there is more than one segment
1032// which meets these characteristics, we will use the first one.
1033
1034struct Define_symbol_in_segment
1035{
1036 // The symbol name.
1037 const char* name;
1038 // The segment type where the symbol should be defined, typically
1039 // PT_LOAD.
1040 elfcpp::PT segment_type;
1041 // Bitmask of segment flags which must be set.
1042 elfcpp::PF segment_flags_set;
1043 // Bitmask of segment flags which must be clear.
1044 elfcpp::PF segment_flags_clear;
1045 // The offset of the symbol within the segment. The offset is
1046 // calculated from the position set by offset_base.
1047 uint64_t value;
1048 // The size of the symbol.
1049 uint64_t size;
1050 // The symbol type.
1051 elfcpp::STT type;
1052 // The symbol binding.
1053 elfcpp::STB binding;
1054 // The symbol visibility.
1055 elfcpp::STV visibility;
1056 // The rest of the st_other field.
1057 unsigned char nonvis;
1058 // The base from which we compute the offset.
1059 Symbol::Segment_offset_base offset_base;
1060 // If true, this symbol is defined only if we see a reference to it.
1061 bool only_if_ref;
14bfc3f5
ILT
1062};
1063
f6ce93d6
ILT
1064// This class manages warnings. Warnings are a GNU extension. When
1065// we see a section named .gnu.warning.SYM in an object file, and if
1066// we wind using the definition of SYM from that object file, then we
1067// will issue a warning for any relocation against SYM from a
1068// different object file. The text of the warning is the contents of
1069// the section. This is not precisely the definition used by the old
1070// GNU linker; the old GNU linker treated an occurrence of
1071// .gnu.warning.SYM as defining a warning symbol. A warning symbol
1072// would trigger a warning on any reference. However, it was
1073// inconsistent in that a warning in a dynamic object only triggered
1074// if there was no definition in a regular object. This linker is
1075// different in that we only issue a warning if we use the symbol
1076// definition from the same object file as the warning section.
1077
1078class Warnings
1079{
1080 public:
1081 Warnings()
1082 : warnings_()
1083 { }
1084
cb295612
ILT
1085 // Add a warning for symbol NAME in object OBJ. WARNING is the text
1086 // of the warning.
f6ce93d6
ILT
1087 void
1088 add_warning(Symbol_table* symtab, const char* name, Object* obj,
cb295612 1089 const std::string& warning);
f6ce93d6
ILT
1090
1091 // For each symbol for which we should give a warning, make a note
1092 // on the symbol.
1093 void
cb295612 1094 note_warnings(Symbol_table* symtab);
f6ce93d6 1095
75f2446e
ILT
1096 // Issue a warning for a reference to SYM at RELINFO's location.
1097 template<int size, bool big_endian>
f6ce93d6 1098 void
75f2446e
ILT
1099 issue_warning(const Symbol* sym, const Relocate_info<size, big_endian>*,
1100 size_t relnum, off_t reloffset) const;
f6ce93d6
ILT
1101
1102 private:
1103 Warnings(const Warnings&);
1104 Warnings& operator=(const Warnings&);
1105
1106 // What we need to know to get the warning text.
1107 struct Warning_location
1108 {
1109 // The object the warning is in.
1110 Object* object;
cb295612 1111 // The warning text.
f6ce93d6
ILT
1112 std::string text;
1113
1114 Warning_location()
cb295612 1115 : object(NULL), text()
f6ce93d6
ILT
1116 { }
1117
1118 void
cb295612 1119 set(Object* o, const std::string& t)
f6ce93d6
ILT
1120 {
1121 this->object = o;
cb295612 1122 this->text = t;
f6ce93d6 1123 }
f6ce93d6
ILT
1124 };
1125
1126 // A mapping from warning symbol names (canonicalized in
70e654ba 1127 // Symbol_table's namepool_ field) to warning information.
f6ce93d6
ILT
1128 typedef Unordered_map<const char*, Warning_location> Warning_table;
1129
1130 Warning_table warnings_;
1131};
1132
14bfc3f5
ILT
1133// The main linker symbol table.
1134
bae7f79e
ILT
1135class Symbol_table
1136{
1137 public:
6d013333
ILT
1138 // COUNT is an estimate of how many symbosl will be inserted in the
1139 // symbol table. It's ok to put 0 if you don't know; a correct
1140 // guess will just save some CPU by reducing hashtable resizes.
09124467 1141 Symbol_table(unsigned int count, const Version_script_info& version_script);
bae7f79e 1142
1564db8d 1143 ~Symbol_table();
bae7f79e 1144
6d03d481
ST
1145 void
1146 set_gc(Garbage_collection* gc)
1147 { this->gc_ = gc; }
1148
1149 Garbage_collection*
1150 gc()
1151 { return this->gc_; }
1152
1153 // During garbage collection, this keeps undefined symbols.
1154 void
1155 gc_mark_undef_symbols();
1156
1157 // During garbage collection, this ensures externally visible symbols
1158 // are not treated as garbage while building shared objects.
1159 void
1160 gc_mark_symbol_for_shlib(Symbol* sym);
1161
1162 // During garbage collection, this keeps sections that correspond to
1163 // symbols seen in dynamic objects.
1164 inline void
1165 gc_mark_dyn_syms(Symbol* sym);
1166
dbe717ef 1167 // Add COUNT external symbols from the relocatable object RELOBJ to
d491d34e
ILT
1168 // the symbol table. SYMS is the symbols, SYMNDX_OFFSET is the
1169 // offset in the symbol table of the first symbol, SYM_NAMES is
1170 // their names, SYM_NAME_SIZE is the size of SYM_NAMES. This sets
92de84a6
ILT
1171 // SYMPOINTERS to point to the symbols in the symbol table. It sets
1172 // *DEFINED to the number of defined symbols.
14bfc3f5
ILT
1173 template<int size, bool big_endian>
1174 void
dbe717ef
ILT
1175 add_from_relobj(Sized_relobj<size, big_endian>* relobj,
1176 const unsigned char* syms, size_t count,
d491d34e
ILT
1177 size_t symndx_offset, const char* sym_names,
1178 size_t sym_name_size,
92de84a6
ILT
1179 typename Sized_relobj<size, big_endian>::Symbols*,
1180 size_t* defined);
89fc3421
CC
1181
1182 // Add one external symbol from the plugin object OBJ to the symbol table.
1183 // Returns a pointer to the resolved symbol in the symbol table.
1184 template<int size, bool big_endian>
1185 Symbol*
1186 add_from_pluginobj(Sized_pluginobj<size, big_endian>* obj,
1187 const char* name, const char* ver,
1188 elfcpp::Sym<size, big_endian>* sym);
14bfc3f5 1189
dbe717ef
ILT
1190 // Add COUNT dynamic symbols from the dynamic object DYNOBJ to the
1191 // symbol table. SYMS is the symbols. SYM_NAMES is their names.
1192 // SYM_NAME_SIZE is the size of SYM_NAMES. The other parameters are
1193 // symbol version data.
1194 template<int size, bool big_endian>
1195 void
1196 add_from_dynobj(Sized_dynobj<size, big_endian>* dynobj,
1197 const unsigned char* syms, size_t count,
1198 const char* sym_names, size_t sym_name_size,
1199 const unsigned char* versym, size_t versym_size,
92de84a6
ILT
1200 const std::vector<const char*>*,
1201 typename Sized_relobj<size, big_endian>::Symbols*,
1202 size_t* defined);
dbe717ef 1203
ead1e424
ILT
1204 // Define a special symbol based on an Output_data. It is a
1205 // multiple definition error if this symbol is already defined.
14b31740 1206 Symbol*
9b07f471 1207 define_in_output_data(const char* name, const char* version,
14b31740 1208 Output_data*, uint64_t value, uint64_t symsize,
ead1e424
ILT
1209 elfcpp::STT type, elfcpp::STB binding,
1210 elfcpp::STV visibility, unsigned char nonvis,
1211 bool offset_is_from_end, bool only_if_ref);
1212
1213 // Define a special symbol based on an Output_segment. It is a
1214 // multiple definition error if this symbol is already defined.
14b31740 1215 Symbol*
9b07f471
ILT
1216 define_in_output_segment(const char* name, const char* version,
1217 Output_segment*, uint64_t value, uint64_t symsize,
ead1e424
ILT
1218 elfcpp::STT type, elfcpp::STB binding,
1219 elfcpp::STV visibility, unsigned char nonvis,
1220 Symbol::Segment_offset_base, bool only_if_ref);
1221
1222 // Define a special symbol with a constant value. It is a multiple
1223 // definition error if this symbol is already defined.
14b31740 1224 Symbol*
9b07f471 1225 define_as_constant(const char* name, const char* version,
14b31740
ILT
1226 uint64_t value, uint64_t symsize, elfcpp::STT type,
1227 elfcpp::STB binding, elfcpp::STV visibility,
caa9d5d9
ILT
1228 unsigned char nonvis, bool only_if_ref,
1229 bool force_override);
ead1e424 1230
a445fddf
ILT
1231 // Define a set of symbols in output sections. If ONLY_IF_REF is
1232 // true, only define them if they are referenced.
ead1e424 1233 void
a445fddf
ILT
1234 define_symbols(const Layout*, int count, const Define_symbol_in_section*,
1235 bool only_if_ref);
ead1e424 1236
a445fddf
ILT
1237 // Define a set of symbols in output segments. If ONLY_IF_REF is
1238 // true, only defined them if they are referenced.
ead1e424 1239 void
a445fddf
ILT
1240 define_symbols(const Layout*, int count, const Define_symbol_in_segment*,
1241 bool only_if_ref);
ead1e424 1242
46fe1623
ILT
1243 // Define SYM using a COPY reloc. POSD is the Output_data where the
1244 // symbol should be defined--typically a .dyn.bss section. VALUE is
1245 // the offset within POSD.
1246 template<int size>
1247 void
9b07f471 1248 define_with_copy_reloc(Sized_symbol<size>* sym, Output_data* posd,
fe8718a4 1249 typename elfcpp::Elf_types<size>::Elf_Addr);
46fe1623 1250
61ba1cf9
ILT
1251 // Look up a symbol.
1252 Symbol*
1253 lookup(const char*, const char* version = NULL) const;
1254
14bfc3f5 1255 // Return the real symbol associated with the forwarder symbol FROM.
bae7f79e 1256 Symbol*
c06b7b0b 1257 resolve_forwards(const Symbol* from) const;
bae7f79e 1258
1564db8d
ILT
1259 // Return the sized version of a symbol in this table.
1260 template<int size>
1261 Sized_symbol<size>*
7d1a9ebb 1262 get_sized_symbol(Symbol*) const;
1564db8d
ILT
1263
1264 template<int size>
1265 const Sized_symbol<size>*
7d1a9ebb 1266 get_sized_symbol(const Symbol*) const;
54dc6425 1267
ead1e424
ILT
1268 // Return the count of undefined symbols seen.
1269 int
1270 saw_undefined() const
1271 { return this->saw_undefined_; }
1272
1273 // Allocate the common symbols
1274 void
7d9e3d98 1275 allocate_commons(Layout*, Mapfile*);
ead1e424 1276
cb295612
ILT
1277 // Add a warning for symbol NAME in object OBJ. WARNING is the text
1278 // of the warning.
f6ce93d6 1279 void
cb295612
ILT
1280 add_warning(const char* name, Object* obj, const std::string& warning)
1281 { this->warnings_.add_warning(this, name, obj, warning); }
f6ce93d6
ILT
1282
1283 // Canonicalize a symbol name for use in the hash table.
1284 const char*
1285 canonicalize_name(const char* name)
cfd73a4e 1286 { return this->namepool_.add(name, true, NULL); }
f6ce93d6
ILT
1287
1288 // Possibly issue a warning for a reference to SYM at LOCATION which
1289 // is in OBJ.
75f2446e 1290 template<int size, bool big_endian>
f6ce93d6 1291 void
75f2446e
ILT
1292 issue_warning(const Symbol* sym,
1293 const Relocate_info<size, big_endian>* relinfo,
1294 size_t relnum, off_t reloffset) const
1295 { this->warnings_.issue_warning(sym, relinfo, relnum, reloffset); }
f6ce93d6 1296
70e654ba
ILT
1297 // Check candidate_odr_violations_ to find symbols with the same name
1298 // but apparently different definitions (different source-file/line-no).
1299 void
17a1d0a9 1300 detect_odr_violations(const Task*, const char* output_file_name) const;
70e654ba 1301
f3e9c5c5
ILT
1302 // Add any undefined symbols named on the command line to the symbol
1303 // table.
1304 void
1305 add_undefined_symbols_from_command_line();
1306
46fe1623
ILT
1307 // SYM is defined using a COPY reloc. Return the dynamic object
1308 // where the original definition was found.
1309 Dynobj*
1310 get_copy_source(const Symbol* sym) const;
1311
a3ad94ed
ILT
1312 // Set the dynamic symbol indexes. INDEX is the index of the first
1313 // global dynamic symbol. Pointers to the symbols are stored into
1314 // the vector. The names are stored into the Stringpool. This
1315 // returns an updated dynamic symbol index.
1316 unsigned int
9b07f471
ILT
1317 set_dynsym_indexes(unsigned int index, std::vector<Symbol*>*,
1318 Stringpool*, Versions*);
a3ad94ed 1319
75f65a3e 1320 // Finalize the symbol table after we have set the final addresses
c06b7b0b 1321 // of all the input sections. This sets the final symbol indexes,
55a93433
ILT
1322 // values and adds the names to *POOL. *PLOCAL_SYMCOUNT is the
1323 // index of the first global symbol. OFF is the file offset of the
1324 // global symbol table, DYNOFF is the offset of the globals in the
1325 // dynamic symbol table, DYN_GLOBAL_INDEX is the index of the first
1326 // global dynamic symbol, and DYNCOUNT is the number of global
1327 // dynamic symbols. This records the parameters, and returns the
1328 // new file offset. It updates *PLOCAL_SYMCOUNT if it created any
1329 // local symbols.
75f65a3e 1330 off_t
55a93433
ILT
1331 finalize(off_t off, off_t dynoff, size_t dyn_global_index, size_t dyncount,
1332 Stringpool* pool, unsigned int *plocal_symcount);
1564db8d 1333
61ba1cf9
ILT
1334 // Write out the global symbols.
1335 void
9a2d6984 1336 write_globals(const Input_objects*, const Stringpool*, const Stringpool*,
d491d34e 1337 Output_symtab_xindex*, Output_symtab_xindex*,
16649710 1338 Output_file*) const;
61ba1cf9 1339
a3ad94ed
ILT
1340 // Write out a section symbol. Return the updated offset.
1341 void
d491d34e
ILT
1342 write_section_symbol(const Output_section*, Output_symtab_xindex*,
1343 Output_file*, off_t) const;
a3ad94ed 1344
abaa3995
ILT
1345 // Dump statistical information to stderr.
1346 void
1347 print_stats() const;
1348
09124467
ILT
1349 // Return the version script information.
1350 const Version_script_info&
1351 version_script() const
1352 { return version_script_; }
1353
bae7f79e
ILT
1354 private:
1355 Symbol_table(const Symbol_table&);
1356 Symbol_table& operator=(const Symbol_table&);
1357
155a0dd7
ILT
1358 // The type of the list of common symbols.
1359 typedef std::vector<Symbol*> Commons_type;
1360
14bfc3f5
ILT
1361 // Make FROM a forwarder symbol to TO.
1362 void
1363 make_forwarder(Symbol* from, Symbol* to);
1364
1365 // Add a symbol.
1366 template<int size, bool big_endian>
aeddab66 1367 Sized_symbol<size>*
f0641a0b
ILT
1368 add_from_object(Object*, const char *name, Stringpool::Key name_key,
1369 const char *version, Stringpool::Key version_key,
70e654ba 1370 bool def, const elfcpp::Sym<size, big_endian>& sym,
d491d34e
ILT
1371 unsigned int st_shndx, bool is_ordinary,
1372 unsigned int orig_st_shndx);
14bfc3f5
ILT
1373
1374 // Resolve symbols.
1375 template<int size, bool big_endian>
aeddab66 1376 void
1564db8d
ILT
1377 resolve(Sized_symbol<size>* to,
1378 const elfcpp::Sym<size, big_endian>& sym,
d491d34e
ILT
1379 unsigned int st_shndx, bool is_ordinary,
1380 unsigned int orig_st_shndx,
14b31740 1381 Object*, const char* version);
14bfc3f5 1382
1564db8d 1383 template<int size, bool big_endian>
aeddab66 1384 void
95d14cd3 1385 resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from);
14b31740 1386
55a93433
ILT
1387 // Record that a symbol is forced to be local by a version script.
1388 void
1389 force_local(Symbol*);
1390
0864d551
ILT
1391 // Adjust NAME and *NAME_KEY for wrapping.
1392 const char*
1393 wrap_symbol(Object* object, const char*, Stringpool::Key* name_key);
1394
86f2e683
ILT
1395 // Whether we should override a symbol, based on flags in
1396 // resolve.cc.
1397 static bool
d20222a1 1398 should_override(const Symbol*, unsigned int, Object*, bool*);
86f2e683 1399
aeddab66
ILT
1400 // Override a symbol.
1401 template<int size, bool big_endian>
1402 void
1403 override(Sized_symbol<size>* tosym,
1404 const elfcpp::Sym<size, big_endian>& fromsym,
d491d34e 1405 unsigned int st_shndx, bool is_ordinary,
aeddab66
ILT
1406 Object* object, const char* version);
1407
86f2e683
ILT
1408 // Whether we should override a symbol with a special symbol which
1409 // is automatically defined by the linker.
1410 static bool
1411 should_override_with_special(const Symbol*);
1412
aeddab66
ILT
1413 // Override a symbol with a special symbol.
1414 template<int size>
1415 void
1416 override_with_special(Sized_symbol<size>* tosym,
1417 const Sized_symbol<size>* fromsym);
1418
1419 // Record all weak alias sets for a dynamic object.
1420 template<int size>
1421 void
1422 record_weak_aliases(std::vector<Sized_symbol<size>*>*);
1423
14b31740
ILT
1424 // Define a special symbol.
1425 template<int size, bool big_endian>
1426 Sized_symbol<size>*
9b07f471 1427 define_special_symbol(const char** pname, const char** pversion,
7d1a9ebb 1428 bool only_if_ref, Sized_symbol<size>** poldsym);
14bfc3f5 1429
ead1e424
ILT
1430 // Define a symbol in an Output_data, sized version.
1431 template<int size>
14b31740 1432 Sized_symbol<size>*
9b07f471 1433 do_define_in_output_data(const char* name, const char* version, Output_data*,
ead1e424
ILT
1434 typename elfcpp::Elf_types<size>::Elf_Addr value,
1435 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
1436 elfcpp::STT type, elfcpp::STB binding,
1437 elfcpp::STV visibility, unsigned char nonvis,
1438 bool offset_is_from_end, bool only_if_ref);
1439
1440 // Define a symbol in an Output_segment, sized version.
1441 template<int size>
14b31740 1442 Sized_symbol<size>*
ead1e424 1443 do_define_in_output_segment(
9b07f471 1444 const char* name, const char* version, Output_segment* os,
ead1e424
ILT
1445 typename elfcpp::Elf_types<size>::Elf_Addr value,
1446 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
1447 elfcpp::STT type, elfcpp::STB binding,
1448 elfcpp::STV visibility, unsigned char nonvis,
1449 Symbol::Segment_offset_base offset_base, bool only_if_ref);
1450
1451 // Define a symbol as a constant, sized version.
1452 template<int size>
14b31740 1453 Sized_symbol<size>*
ead1e424 1454 do_define_as_constant(
9b07f471 1455 const char* name, const char* version,
ead1e424
ILT
1456 typename elfcpp::Elf_types<size>::Elf_Addr value,
1457 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
1458 elfcpp::STT type, elfcpp::STB binding,
1459 elfcpp::STV visibility, unsigned char nonvis,
caa9d5d9 1460 bool only_if_ref, bool force_override);
ead1e424 1461
f3e9c5c5
ILT
1462 // Add any undefined symbols named on the command line to the symbol
1463 // table, sized version.
1464 template<int size>
1465 void
1466 do_add_undefined_symbols_from_command_line();
1467
ead1e424
ILT
1468 // Allocate the common symbols, sized version.
1469 template<int size>
1470 void
7d9e3d98 1471 do_allocate_commons(Layout*, Mapfile*);
155a0dd7
ILT
1472
1473 // Allocate the common symbols from one list.
1474 template<int size>
1475 void
7d9e3d98 1476 do_allocate_commons_list(Layout*, bool is_tls, Commons_type*, Mapfile*);
ead1e424 1477
70e654ba
ILT
1478 // Implement detect_odr_violations.
1479 template<int size, bool big_endian>
1480 void
1481 sized_detect_odr_violations() const;
1482
75f65a3e
ILT
1483 // Finalize symbols specialized for size.
1484 template<int size>
1485 off_t
55a93433
ILT
1486 sized_finalize(off_t, Stringpool*, unsigned int*);
1487
1488 // Finalize a symbol. Return whether it should be added to the
1489 // symbol table.
1490 template<int size>
1491 bool
1492 sized_finalize_symbol(Symbol*);
1493
1494 // Add a symbol the final symtab by setting its index.
1495 template<int size>
1496 void
1497 add_to_final_symtab(Symbol*, Stringpool*, unsigned int* pindex, off_t* poff);
75f65a3e 1498
61ba1cf9
ILT
1499 // Write globals specialized for size and endianness.
1500 template<int size, bool big_endian>
1501 void
9a2d6984 1502 sized_write_globals(const Input_objects*, const Stringpool*,
d491d34e
ILT
1503 const Stringpool*, Output_symtab_xindex*,
1504 Output_symtab_xindex*, Output_file*) const;
16649710
ILT
1505
1506 // Write out a symbol to P.
1507 template<int size, bool big_endian>
1508 void
ab5c9e90
ILT
1509 sized_write_symbol(Sized_symbol<size>*,
1510 typename elfcpp::Elf_types<size>::Elf_Addr value,
1511 unsigned int shndx,
7d1a9ebb 1512 const Stringpool*, unsigned char* p) const;
61ba1cf9 1513
9a2d6984
ILT
1514 // Possibly warn about an undefined symbol from a dynamic object.
1515 void
1516 warn_about_undefined_dynobj_symbol(const Input_objects*, Symbol*) const;
1517
a3ad94ed
ILT
1518 // Write out a section symbol, specialized for size and endianness.
1519 template<int size, bool big_endian>
1520 void
d491d34e
ILT
1521 sized_write_section_symbol(const Output_section*, Output_symtab_xindex*,
1522 Output_file*, off_t) const;
a3ad94ed 1523
54dc6425
ILT
1524 // The type of the symbol hash table.
1525
f0641a0b 1526 typedef std::pair<Stringpool::Key, Stringpool::Key> Symbol_table_key;
14bfc3f5
ILT
1527
1528 struct Symbol_table_hash
1529 {
1530 size_t
1531 operator()(const Symbol_table_key&) const;
1532 };
1533
1534 struct Symbol_table_eq
1535 {
1536 bool
1537 operator()(const Symbol_table_key&, const Symbol_table_key&) const;
1538 };
1539
1540 typedef Unordered_map<Symbol_table_key, Symbol*, Symbol_table_hash,
1541 Symbol_table_eq> Symbol_table_type;
1542
55a93433
ILT
1543 // The type of the list of symbols which have been forced local.
1544 typedef std::vector<Symbol*> Forced_locals;
1545
46fe1623
ILT
1546 // A map from symbols with COPY relocs to the dynamic objects where
1547 // they are defined.
1548 typedef Unordered_map<const Symbol*, Dynobj*> Copied_symbol_dynobjs;
1549
70e654ba
ILT
1550 // A map from symbol name (as a pointer into the namepool) to all
1551 // the locations the symbols is (weakly) defined (and certain other
1552 // conditions are met). This map will be used later to detect
1553 // possible One Definition Rule (ODR) violations.
1554 struct Symbol_location
1555 {
1556 Object* object; // Object where the symbol is defined.
1557 unsigned int shndx; // Section-in-object where the symbol is defined.
1558 off_t offset; // Offset-in-section where the symbol is defined.
1559 bool operator==(const Symbol_location& that) const
1560 {
1561 return (this->object == that.object
1562 && this->shndx == that.shndx
1563 && this->offset == that.offset);
1564 }
1565 };
1566
1567 struct Symbol_location_hash
1568 {
1569 size_t operator()(const Symbol_location& loc) const
1570 { return reinterpret_cast<uintptr_t>(loc.object) ^ loc.offset ^ loc.shndx; }
1571 };
1572
1573 typedef Unordered_map<const char*,
1574 Unordered_set<Symbol_location, Symbol_location_hash> >
1575 Odr_map;
1576
ead1e424
ILT
1577 // We increment this every time we see a new undefined symbol, for
1578 // use in archive groups.
1579 int saw_undefined_;
c06b7b0b
ILT
1580 // The index of the first global symbol in the output file.
1581 unsigned int first_global_index_;
75f65a3e
ILT
1582 // The file offset within the output symtab section where we should
1583 // write the table.
1584 off_t offset_;
61ba1cf9 1585 // The number of global symbols we want to write out.
55a93433 1586 unsigned int output_count_;
16649710
ILT
1587 // The file offset of the global dynamic symbols, or 0 if none.
1588 off_t dynamic_offset_;
16649710
ILT
1589 // The index of the first global dynamic symbol.
1590 unsigned int first_dynamic_global_index_;
16649710 1591 // The number of global dynamic symbols, or 0 if none.
55a93433 1592 unsigned int dynamic_count_;
54dc6425 1593 // The symbol hash table.
14bfc3f5 1594 Symbol_table_type table_;
54dc6425
ILT
1595 // A pool of symbol names. This is used for all global symbols.
1596 // Entries in the hash table point into this pool.
14bfc3f5 1597 Stringpool namepool_;
14bfc3f5 1598 // Forwarding symbols.
c06b7b0b 1599 Unordered_map<const Symbol*, Symbol*> forwarders_;
aeddab66
ILT
1600 // Weak aliases. A symbol in this list points to the next alias.
1601 // The aliases point to each other in a circular list.
1602 Unordered_map<Symbol*, Symbol*> weak_aliases_;
ead1e424
ILT
1603 // We don't expect there to be very many common symbols, so we keep
1604 // a list of them. When we find a common symbol we add it to this
1605 // list. It is possible that by the time we process the list the
1606 // symbol is no longer a common symbol. It may also have become a
1607 // forwarder.
1608 Commons_type commons_;
155a0dd7
ILT
1609 // This is like the commons_ field, except that it holds TLS common
1610 // symbols.
1611 Commons_type tls_commons_;
55a93433
ILT
1612 // A list of symbols which have been forced to be local. We don't
1613 // expect there to be very many of them, so we keep a list of them
1614 // rather than walking the whole table to find them.
1615 Forced_locals forced_locals_;
f6ce93d6
ILT
1616 // Manage symbol warnings.
1617 Warnings warnings_;
70e654ba
ILT
1618 // Manage potential One Definition Rule (ODR) violations.
1619 Odr_map candidate_odr_violations_;
1620
46fe1623
ILT
1621 // When we emit a COPY reloc for a symbol, we define it in an
1622 // Output_data. When it's time to emit version information for it,
1623 // we need to know the dynamic object in which we found the original
1624 // definition. This maps symbols with COPY relocs to the dynamic
1625 // object where they were defined.
1626 Copied_symbol_dynobjs copied_symbol_dynobjs_;
09124467
ILT
1627 // Information parsed from the version script, if any.
1628 const Version_script_info& version_script_;
6d03d481 1629 Garbage_collection* gc_;
bae7f79e
ILT
1630};
1631
1564db8d
ILT
1632// We inline get_sized_symbol for efficiency.
1633
1634template<int size>
1635Sized_symbol<size>*
7d1a9ebb 1636Symbol_table::get_sized_symbol(Symbol* sym) const
1564db8d 1637{
8851ecca 1638 gold_assert(size == parameters->target().get_size());
1564db8d
ILT
1639 return static_cast<Sized_symbol<size>*>(sym);
1640}
1641
1642template<int size>
1643const Sized_symbol<size>*
7d1a9ebb 1644Symbol_table::get_sized_symbol(const Symbol* sym) const
1564db8d 1645{
8851ecca 1646 gold_assert(size == parameters->target().get_size());
1564db8d
ILT
1647 return static_cast<const Sized_symbol<size>*>(sym);
1648}
1649
bae7f79e
ILT
1650} // End namespace gold.
1651
1652#endif // !defined(GOLD_SYMTAB_H)
This page took 0.200942 seconds and 4 git commands to generate.