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