1*fae548d3Szrj // symtab.h -- the gold symbol table -*- C++ -*-
2*fae548d3Szrj
3*fae548d3Szrj // Copyright (C) 2006-2020 Free Software Foundation, Inc.
4*fae548d3Szrj // Written by Ian Lance Taylor <iant@google.com>.
5*fae548d3Szrj
6*fae548d3Szrj // This file is part of gold.
7*fae548d3Szrj
8*fae548d3Szrj // This program is free software; you can redistribute it and/or modify
9*fae548d3Szrj // it under the terms of the GNU General Public License as published by
10*fae548d3Szrj // the Free Software Foundation; either version 3 of the License, or
11*fae548d3Szrj // (at your option) any later version.
12*fae548d3Szrj
13*fae548d3Szrj // This program is distributed in the hope that it will be useful,
14*fae548d3Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
15*fae548d3Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16*fae548d3Szrj // GNU General Public License for more details.
17*fae548d3Szrj
18*fae548d3Szrj // You should have received a copy of the GNU General Public License
19*fae548d3Szrj // along with this program; if not, write to the Free Software
20*fae548d3Szrj // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21*fae548d3Szrj // MA 02110-1301, USA.
22*fae548d3Szrj
23*fae548d3Szrj // Symbol_table
24*fae548d3Szrj // The symbol table.
25*fae548d3Szrj
26*fae548d3Szrj #ifndef GOLD_SYMTAB_H
27*fae548d3Szrj #define GOLD_SYMTAB_H
28*fae548d3Szrj
29*fae548d3Szrj #include <string>
30*fae548d3Szrj #include <utility>
31*fae548d3Szrj #include <vector>
32*fae548d3Szrj
33*fae548d3Szrj #include "elfcpp.h"
34*fae548d3Szrj #include "parameters.h"
35*fae548d3Szrj #include "stringpool.h"
36*fae548d3Szrj #include "object.h"
37*fae548d3Szrj
38*fae548d3Szrj namespace gold
39*fae548d3Szrj {
40*fae548d3Szrj
41*fae548d3Szrj class Mapfile;
42*fae548d3Szrj class Object;
43*fae548d3Szrj class Relobj;
44*fae548d3Szrj template<int size, bool big_endian>
45*fae548d3Szrj class Sized_relobj_file;
46*fae548d3Szrj template<int size, bool big_endian>
47*fae548d3Szrj class Sized_pluginobj;
48*fae548d3Szrj class Dynobj;
49*fae548d3Szrj template<int size, bool big_endian>
50*fae548d3Szrj class Sized_dynobj;
51*fae548d3Szrj template<int size, bool big_endian>
52*fae548d3Szrj class Sized_incrobj;
53*fae548d3Szrj class Versions;
54*fae548d3Szrj class Version_script_info;
55*fae548d3Szrj class Input_objects;
56*fae548d3Szrj class Output_data;
57*fae548d3Szrj class Output_section;
58*fae548d3Szrj class Output_segment;
59*fae548d3Szrj class Output_file;
60*fae548d3Szrj class Output_symtab_xindex;
61*fae548d3Szrj class Garbage_collection;
62*fae548d3Szrj class Icf;
63*fae548d3Szrj
64*fae548d3Szrj // The base class of an entry in the symbol table. The symbol table
65*fae548d3Szrj // can have a lot of entries, so we don't want this class too big.
66*fae548d3Szrj // Size dependent fields can be found in the template class
67*fae548d3Szrj // Sized_symbol. Targets may support their own derived classes.
68*fae548d3Szrj
69*fae548d3Szrj class Symbol
70*fae548d3Szrj {
71*fae548d3Szrj public:
72*fae548d3Szrj // Because we want the class to be small, we don't use any virtual
73*fae548d3Szrj // functions. But because symbols can be defined in different
74*fae548d3Szrj // places, we need to classify them. This enum is the different
75*fae548d3Szrj // sources of symbols we support.
76*fae548d3Szrj enum Source
77*fae548d3Szrj {
78*fae548d3Szrj // Symbol defined in a relocatable or dynamic input file--this is
79*fae548d3Szrj // the most common case.
80*fae548d3Szrj FROM_OBJECT,
81*fae548d3Szrj // Symbol defined in an Output_data, a special section created by
82*fae548d3Szrj // the target.
83*fae548d3Szrj IN_OUTPUT_DATA,
84*fae548d3Szrj // Symbol defined in an Output_segment, with no associated
85*fae548d3Szrj // section.
86*fae548d3Szrj IN_OUTPUT_SEGMENT,
87*fae548d3Szrj // Symbol value is constant.
88*fae548d3Szrj IS_CONSTANT,
89*fae548d3Szrj // Symbol is undefined.
90*fae548d3Szrj IS_UNDEFINED
91*fae548d3Szrj };
92*fae548d3Szrj
93*fae548d3Szrj // When the source is IN_OUTPUT_SEGMENT, we need to describe what
94*fae548d3Szrj // the offset means.
95*fae548d3Szrj enum Segment_offset_base
96*fae548d3Szrj {
97*fae548d3Szrj // From the start of the segment.
98*fae548d3Szrj SEGMENT_START,
99*fae548d3Szrj // From the end of the segment.
100*fae548d3Szrj SEGMENT_END,
101*fae548d3Szrj // From the filesz of the segment--i.e., after the loaded bytes
102*fae548d3Szrj // but before the bytes which are allocated but zeroed.
103*fae548d3Szrj SEGMENT_BSS
104*fae548d3Szrj };
105*fae548d3Szrj
106*fae548d3Szrj // Return the symbol name.
107*fae548d3Szrj const char*
name()108*fae548d3Szrj name() const
109*fae548d3Szrj { return this->name_; }
110*fae548d3Szrj
111*fae548d3Szrj // Return the (ANSI) demangled version of the name, if
112*fae548d3Szrj // parameters.demangle() is true. Otherwise, return the name. This
113*fae548d3Szrj // is intended to be used only for logging errors, so it's not
114*fae548d3Szrj // super-efficient.
115*fae548d3Szrj std::string
116*fae548d3Szrj demangled_name() const;
117*fae548d3Szrj
118*fae548d3Szrj // Return the symbol version. This will return NULL for an
119*fae548d3Szrj // unversioned symbol.
120*fae548d3Szrj const char*
version()121*fae548d3Szrj version() const
122*fae548d3Szrj { return this->version_; }
123*fae548d3Szrj
124*fae548d3Szrj void
clear_version()125*fae548d3Szrj clear_version()
126*fae548d3Szrj { this->version_ = NULL; }
127*fae548d3Szrj
128*fae548d3Szrj // Return whether this version is the default for this symbol name
129*fae548d3Szrj // (eg, "foo@@V2" is a default version; "foo@V1" is not). Only
130*fae548d3Szrj // meaningful for versioned symbols.
131*fae548d3Szrj bool
is_default()132*fae548d3Szrj is_default() const
133*fae548d3Szrj {
134*fae548d3Szrj gold_assert(this->version_ != NULL);
135*fae548d3Szrj return this->is_def_;
136*fae548d3Szrj }
137*fae548d3Szrj
138*fae548d3Szrj // Set that this version is the default for this symbol name.
139*fae548d3Szrj void
set_is_default()140*fae548d3Szrj set_is_default()
141*fae548d3Szrj { this->is_def_ = true; }
142*fae548d3Szrj
143*fae548d3Szrj // Set that this version is not the default for this symbol name.
144*fae548d3Szrj void
set_is_not_default()145*fae548d3Szrj set_is_not_default()
146*fae548d3Szrj { this->is_def_ = false; }
147*fae548d3Szrj
148*fae548d3Szrj // Return the symbol's name as name@version (or name@@version).
149*fae548d3Szrj std::string
150*fae548d3Szrj versioned_name() const;
151*fae548d3Szrj
152*fae548d3Szrj // Return the symbol source.
153*fae548d3Szrj Source
source()154*fae548d3Szrj source() const
155*fae548d3Szrj { return this->source_; }
156*fae548d3Szrj
157*fae548d3Szrj // Return the object with which this symbol is associated.
158*fae548d3Szrj Object*
object()159*fae548d3Szrj object() const
160*fae548d3Szrj {
161*fae548d3Szrj gold_assert(this->source_ == FROM_OBJECT);
162*fae548d3Szrj return this->u1_.object;
163*fae548d3Szrj }
164*fae548d3Szrj
165*fae548d3Szrj // Return the index of the section in the input relocatable or
166*fae548d3Szrj // dynamic object file.
167*fae548d3Szrj unsigned int
shndx(bool * is_ordinary)168*fae548d3Szrj shndx(bool* is_ordinary) const
169*fae548d3Szrj {
170*fae548d3Szrj gold_assert(this->source_ == FROM_OBJECT);
171*fae548d3Szrj *is_ordinary = this->is_ordinary_shndx_;
172*fae548d3Szrj return this->u2_.shndx;
173*fae548d3Szrj }
174*fae548d3Szrj
175*fae548d3Szrj // Return the output data section with which this symbol is
176*fae548d3Szrj // associated, if the symbol was specially defined with respect to
177*fae548d3Szrj // an output data section.
178*fae548d3Szrj Output_data*
output_data()179*fae548d3Szrj output_data() const
180*fae548d3Szrj {
181*fae548d3Szrj gold_assert(this->source_ == IN_OUTPUT_DATA);
182*fae548d3Szrj return this->u1_.output_data;
183*fae548d3Szrj }
184*fae548d3Szrj
185*fae548d3Szrj // If this symbol was defined with respect to an output data
186*fae548d3Szrj // section, return whether the value is an offset from end.
187*fae548d3Szrj bool
offset_is_from_end()188*fae548d3Szrj offset_is_from_end() const
189*fae548d3Szrj {
190*fae548d3Szrj gold_assert(this->source_ == IN_OUTPUT_DATA);
191*fae548d3Szrj return this->u2_.offset_is_from_end;
192*fae548d3Szrj }
193*fae548d3Szrj
194*fae548d3Szrj // Return the output segment with which this symbol is associated,
195*fae548d3Szrj // if the symbol was specially defined with respect to an output
196*fae548d3Szrj // segment.
197*fae548d3Szrj Output_segment*
output_segment()198*fae548d3Szrj output_segment() const
199*fae548d3Szrj {
200*fae548d3Szrj gold_assert(this->source_ == IN_OUTPUT_SEGMENT);
201*fae548d3Szrj return this->u1_.output_segment;
202*fae548d3Szrj }
203*fae548d3Szrj
204*fae548d3Szrj // If this symbol was defined with respect to an output segment,
205*fae548d3Szrj // return the offset base.
206*fae548d3Szrj Segment_offset_base
offset_base()207*fae548d3Szrj offset_base() const
208*fae548d3Szrj {
209*fae548d3Szrj gold_assert(this->source_ == IN_OUTPUT_SEGMENT);
210*fae548d3Szrj return this->u2_.offset_base;
211*fae548d3Szrj }
212*fae548d3Szrj
213*fae548d3Szrj // Return the symbol binding.
214*fae548d3Szrj elfcpp::STB
binding()215*fae548d3Szrj binding() const
216*fae548d3Szrj { return this->binding_; }
217*fae548d3Szrj
218*fae548d3Szrj // Return the symbol type.
219*fae548d3Szrj elfcpp::STT
type()220*fae548d3Szrj type() const
221*fae548d3Szrj { return this->type_; }
222*fae548d3Szrj
223*fae548d3Szrj // Set the symbol type.
224*fae548d3Szrj void
set_type(elfcpp::STT type)225*fae548d3Szrj set_type(elfcpp::STT type)
226*fae548d3Szrj { this->type_ = type; }
227*fae548d3Szrj
228*fae548d3Szrj // Return true for function symbol.
229*fae548d3Szrj bool
is_func()230*fae548d3Szrj is_func() const
231*fae548d3Szrj {
232*fae548d3Szrj return (this->type_ == elfcpp::STT_FUNC
233*fae548d3Szrj || this->type_ == elfcpp::STT_GNU_IFUNC);
234*fae548d3Szrj }
235*fae548d3Szrj
236*fae548d3Szrj // Return the symbol visibility.
237*fae548d3Szrj elfcpp::STV
visibility()238*fae548d3Szrj visibility() const
239*fae548d3Szrj { return this->visibility_; }
240*fae548d3Szrj
241*fae548d3Szrj // Set the visibility.
242*fae548d3Szrj void
set_visibility(elfcpp::STV visibility)243*fae548d3Szrj set_visibility(elfcpp::STV visibility)
244*fae548d3Szrj { this->visibility_ = visibility; }
245*fae548d3Szrj
246*fae548d3Szrj // Override symbol visibility.
247*fae548d3Szrj void
248*fae548d3Szrj override_visibility(elfcpp::STV);
249*fae548d3Szrj
250*fae548d3Szrj // Set whether the symbol was originally a weak undef or a regular undef
251*fae548d3Szrj // when resolved by a dynamic def or by a special symbol.
252*fae548d3Szrj inline void
set_undef_binding(elfcpp::STB bind)253*fae548d3Szrj set_undef_binding(elfcpp::STB bind)
254*fae548d3Szrj {
255*fae548d3Szrj if (!this->undef_binding_set_ || this->undef_binding_weak_)
256*fae548d3Szrj {
257*fae548d3Szrj this->undef_binding_weak_ = bind == elfcpp::STB_WEAK;
258*fae548d3Szrj this->undef_binding_set_ = true;
259*fae548d3Szrj }
260*fae548d3Szrj }
261*fae548d3Szrj
262*fae548d3Szrj // Return TRUE if a weak undef was resolved by a dynamic def or
263*fae548d3Szrj // by a special symbol.
264*fae548d3Szrj inline bool
is_undef_binding_weak()265*fae548d3Szrj is_undef_binding_weak() const
266*fae548d3Szrj { return this->undef_binding_weak_; }
267*fae548d3Szrj
268*fae548d3Szrj // Return the non-visibility part of the st_other field.
269*fae548d3Szrj unsigned char
nonvis()270*fae548d3Szrj nonvis() const
271*fae548d3Szrj { return this->nonvis_; }
272*fae548d3Szrj
273*fae548d3Szrj // Set the non-visibility part of the st_other field.
274*fae548d3Szrj void
set_nonvis(unsigned int nonvis)275*fae548d3Szrj set_nonvis(unsigned int nonvis)
276*fae548d3Szrj { this->nonvis_ = nonvis; }
277*fae548d3Szrj
278*fae548d3Szrj // Return whether this symbol is a forwarder. This will never be
279*fae548d3Szrj // true of a symbol found in the hash table, but may be true of
280*fae548d3Szrj // symbol pointers attached to object files.
281*fae548d3Szrj bool
is_forwarder()282*fae548d3Szrj is_forwarder() const
283*fae548d3Szrj { return this->is_forwarder_; }
284*fae548d3Szrj
285*fae548d3Szrj // Mark this symbol as a forwarder.
286*fae548d3Szrj void
set_forwarder()287*fae548d3Szrj set_forwarder()
288*fae548d3Szrj { this->is_forwarder_ = true; }
289*fae548d3Szrj
290*fae548d3Szrj // Return whether this symbol has an alias in the weak aliases table
291*fae548d3Szrj // in Symbol_table.
292*fae548d3Szrj bool
has_alias()293*fae548d3Szrj has_alias() const
294*fae548d3Szrj { return this->has_alias_; }
295*fae548d3Szrj
296*fae548d3Szrj // Mark this symbol as having an alias.
297*fae548d3Szrj void
set_has_alias()298*fae548d3Szrj set_has_alias()
299*fae548d3Szrj { this->has_alias_ = true; }
300*fae548d3Szrj
301*fae548d3Szrj // Return whether this symbol needs an entry in the dynamic symbol
302*fae548d3Szrj // table.
303*fae548d3Szrj bool
needs_dynsym_entry()304*fae548d3Szrj needs_dynsym_entry() const
305*fae548d3Szrj {
306*fae548d3Szrj return (this->needs_dynsym_entry_
307*fae548d3Szrj || (this->in_reg()
308*fae548d3Szrj && this->in_dyn()
309*fae548d3Szrj && this->is_externally_visible()));
310*fae548d3Szrj }
311*fae548d3Szrj
312*fae548d3Szrj // Mark this symbol as needing an entry in the dynamic symbol table.
313*fae548d3Szrj void
set_needs_dynsym_entry()314*fae548d3Szrj set_needs_dynsym_entry()
315*fae548d3Szrj { this->needs_dynsym_entry_ = true; }
316*fae548d3Szrj
317*fae548d3Szrj // Return whether this symbol should be added to the dynamic symbol
318*fae548d3Szrj // table.
319*fae548d3Szrj bool
320*fae548d3Szrj should_add_dynsym_entry(Symbol_table*) const;
321*fae548d3Szrj
322*fae548d3Szrj // Return whether this symbol has been seen in a regular object.
323*fae548d3Szrj bool
in_reg()324*fae548d3Szrj in_reg() const
325*fae548d3Szrj { return this->in_reg_; }
326*fae548d3Szrj
327*fae548d3Szrj // Mark this symbol as having been seen in a regular object.
328*fae548d3Szrj void
set_in_reg()329*fae548d3Szrj set_in_reg()
330*fae548d3Szrj { this->in_reg_ = true; }
331*fae548d3Szrj
332*fae548d3Szrj // Forget this symbol was seen in a regular object.
333*fae548d3Szrj void
clear_in_reg()334*fae548d3Szrj clear_in_reg()
335*fae548d3Szrj { this->in_reg_ = false; }
336*fae548d3Szrj
337*fae548d3Szrj // Return whether this symbol has been seen in a dynamic object.
338*fae548d3Szrj bool
in_dyn()339*fae548d3Szrj in_dyn() const
340*fae548d3Szrj { return this->in_dyn_; }
341*fae548d3Szrj
342*fae548d3Szrj // Mark this symbol as having been seen in a dynamic object.
343*fae548d3Szrj void
set_in_dyn()344*fae548d3Szrj set_in_dyn()
345*fae548d3Szrj { this->in_dyn_ = true; }
346*fae548d3Szrj
347*fae548d3Szrj // Return whether this symbol is defined in a dynamic object.
348*fae548d3Szrj bool
from_dyn()349*fae548d3Szrj from_dyn() const
350*fae548d3Szrj { return this->source_ == FROM_OBJECT && this->object()->is_dynamic(); }
351*fae548d3Szrj
352*fae548d3Szrj // Return whether this symbol has been seen in a real ELF object.
353*fae548d3Szrj // (IN_REG will return TRUE if the symbol has been seen in either
354*fae548d3Szrj // a real ELF object or an object claimed by a plugin.)
355*fae548d3Szrj bool
in_real_elf()356*fae548d3Szrj in_real_elf() const
357*fae548d3Szrj { return this->in_real_elf_; }
358*fae548d3Szrj
359*fae548d3Szrj // Mark this symbol as having been seen in a real ELF object.
360*fae548d3Szrj void
set_in_real_elf()361*fae548d3Szrj set_in_real_elf()
362*fae548d3Szrj { this->in_real_elf_ = true; }
363*fae548d3Szrj
364*fae548d3Szrj // Return whether this symbol was defined in a section that was
365*fae548d3Szrj // discarded from the link. This is used to control some error
366*fae548d3Szrj // reporting.
367*fae548d3Szrj bool
is_defined_in_discarded_section()368*fae548d3Szrj is_defined_in_discarded_section() const
369*fae548d3Szrj { return this->is_defined_in_discarded_section_; }
370*fae548d3Szrj
371*fae548d3Szrj // Mark this symbol as having been defined in a discarded section.
372*fae548d3Szrj void
set_is_defined_in_discarded_section()373*fae548d3Szrj set_is_defined_in_discarded_section()
374*fae548d3Szrj { this->is_defined_in_discarded_section_ = true; }
375*fae548d3Szrj
376*fae548d3Szrj // Return the index of this symbol in the output file symbol table.
377*fae548d3Szrj // A value of -1U means that this symbol is not going into the
378*fae548d3Szrj // output file. This starts out as zero, and is set to a non-zero
379*fae548d3Szrj // value by Symbol_table::finalize. It is an error to ask for the
380*fae548d3Szrj // symbol table index before it has been set.
381*fae548d3Szrj unsigned int
symtab_index()382*fae548d3Szrj symtab_index() const
383*fae548d3Szrj {
384*fae548d3Szrj gold_assert(this->symtab_index_ != 0);
385*fae548d3Szrj return this->symtab_index_;
386*fae548d3Szrj }
387*fae548d3Szrj
388*fae548d3Szrj // Set the index of the symbol in the output file symbol table.
389*fae548d3Szrj void
set_symtab_index(unsigned int index)390*fae548d3Szrj set_symtab_index(unsigned int index)
391*fae548d3Szrj {
392*fae548d3Szrj gold_assert(index != 0);
393*fae548d3Szrj this->symtab_index_ = index;
394*fae548d3Szrj }
395*fae548d3Szrj
396*fae548d3Szrj // Return whether this symbol already has an index in the output
397*fae548d3Szrj // file symbol table.
398*fae548d3Szrj bool
has_symtab_index()399*fae548d3Szrj has_symtab_index() const
400*fae548d3Szrj { return this->symtab_index_ != 0; }
401*fae548d3Szrj
402*fae548d3Szrj // Return the index of this symbol in the dynamic symbol table. A
403*fae548d3Szrj // value of -1U means that this symbol is not going into the dynamic
404*fae548d3Szrj // symbol table. This starts out as zero, and is set to a non-zero
405*fae548d3Szrj // during Layout::finalize. It is an error to ask for the dynamic
406*fae548d3Szrj // symbol table index before it has been set.
407*fae548d3Szrj unsigned int
dynsym_index()408*fae548d3Szrj dynsym_index() const
409*fae548d3Szrj {
410*fae548d3Szrj gold_assert(this->dynsym_index_ != 0);
411*fae548d3Szrj return this->dynsym_index_;
412*fae548d3Szrj }
413*fae548d3Szrj
414*fae548d3Szrj // Set the index of the symbol in the dynamic symbol table.
415*fae548d3Szrj void
set_dynsym_index(unsigned int index)416*fae548d3Szrj set_dynsym_index(unsigned int index)
417*fae548d3Szrj {
418*fae548d3Szrj gold_assert(index != 0);
419*fae548d3Szrj this->dynsym_index_ = index;
420*fae548d3Szrj }
421*fae548d3Szrj
422*fae548d3Szrj // Return whether this symbol already has an index in the dynamic
423*fae548d3Szrj // symbol table.
424*fae548d3Szrj bool
has_dynsym_index()425*fae548d3Szrj has_dynsym_index() const
426*fae548d3Szrj { return this->dynsym_index_ != 0; }
427*fae548d3Szrj
428*fae548d3Szrj // Return whether this symbol has an entry in the GOT section.
429*fae548d3Szrj // For a TLS symbol, this GOT entry will hold its tp-relative offset.
430*fae548d3Szrj bool
has_got_offset(unsigned int got_type)431*fae548d3Szrj has_got_offset(unsigned int got_type) const
432*fae548d3Szrj { return this->got_offsets_.get_offset(got_type) != -1U; }
433*fae548d3Szrj
434*fae548d3Szrj // Return the offset into the GOT section of this symbol.
435*fae548d3Szrj unsigned int
got_offset(unsigned int got_type)436*fae548d3Szrj got_offset(unsigned int got_type) const
437*fae548d3Szrj {
438*fae548d3Szrj unsigned int got_offset = this->got_offsets_.get_offset(got_type);
439*fae548d3Szrj gold_assert(got_offset != -1U);
440*fae548d3Szrj return got_offset;
441*fae548d3Szrj }
442*fae548d3Szrj
443*fae548d3Szrj // Set the GOT offset of this symbol.
444*fae548d3Szrj void
set_got_offset(unsigned int got_type,unsigned int got_offset)445*fae548d3Szrj set_got_offset(unsigned int got_type, unsigned int got_offset)
446*fae548d3Szrj { this->got_offsets_.set_offset(got_type, got_offset); }
447*fae548d3Szrj
448*fae548d3Szrj // Return the GOT offset list.
449*fae548d3Szrj const Got_offset_list*
got_offset_list()450*fae548d3Szrj got_offset_list() const
451*fae548d3Szrj { return this->got_offsets_.get_list(); }
452*fae548d3Szrj
453*fae548d3Szrj // Return whether this symbol has an entry in the PLT section.
454*fae548d3Szrj bool
has_plt_offset()455*fae548d3Szrj has_plt_offset() const
456*fae548d3Szrj { return this->plt_offset_ != -1U; }
457*fae548d3Szrj
458*fae548d3Szrj // Return the offset into the PLT section of this symbol.
459*fae548d3Szrj unsigned int
plt_offset()460*fae548d3Szrj plt_offset() const
461*fae548d3Szrj {
462*fae548d3Szrj gold_assert(this->has_plt_offset());
463*fae548d3Szrj return this->plt_offset_;
464*fae548d3Szrj }
465*fae548d3Szrj
466*fae548d3Szrj // Set the PLT offset of this symbol.
467*fae548d3Szrj void
set_plt_offset(unsigned int plt_offset)468*fae548d3Szrj set_plt_offset(unsigned int plt_offset)
469*fae548d3Szrj {
470*fae548d3Szrj gold_assert(plt_offset != -1U);
471*fae548d3Szrj this->plt_offset_ = plt_offset;
472*fae548d3Szrj }
473*fae548d3Szrj
474*fae548d3Szrj // Return whether this dynamic symbol needs a special value in the
475*fae548d3Szrj // dynamic symbol table.
476*fae548d3Szrj bool
needs_dynsym_value()477*fae548d3Szrj needs_dynsym_value() const
478*fae548d3Szrj { return this->needs_dynsym_value_; }
479*fae548d3Szrj
480*fae548d3Szrj // Set that this dynamic symbol needs a special value in the dynamic
481*fae548d3Szrj // symbol table.
482*fae548d3Szrj void
set_needs_dynsym_value()483*fae548d3Szrj set_needs_dynsym_value()
484*fae548d3Szrj {
485*fae548d3Szrj gold_assert(this->object()->is_dynamic());
486*fae548d3Szrj this->needs_dynsym_value_ = true;
487*fae548d3Szrj }
488*fae548d3Szrj
489*fae548d3Szrj // Return true if the final value of this symbol is known at link
490*fae548d3Szrj // time.
491*fae548d3Szrj bool
492*fae548d3Szrj final_value_is_known() const;
493*fae548d3Szrj
494*fae548d3Szrj // Return true if SHNDX represents a common symbol. This depends on
495*fae548d3Szrj // the target.
496*fae548d3Szrj static bool
497*fae548d3Szrj is_common_shndx(unsigned int shndx);
498*fae548d3Szrj
499*fae548d3Szrj // Return whether this is a defined symbol (not undefined or
500*fae548d3Szrj // common).
501*fae548d3Szrj bool
is_defined()502*fae548d3Szrj is_defined() const
503*fae548d3Szrj {
504*fae548d3Szrj bool is_ordinary;
505*fae548d3Szrj if (this->source_ != FROM_OBJECT)
506*fae548d3Szrj return this->source_ != IS_UNDEFINED;
507*fae548d3Szrj unsigned int shndx = this->shndx(&is_ordinary);
508*fae548d3Szrj return (is_ordinary
509*fae548d3Szrj ? shndx != elfcpp::SHN_UNDEF
510*fae548d3Szrj : !Symbol::is_common_shndx(shndx));
511*fae548d3Szrj }
512*fae548d3Szrj
513*fae548d3Szrj // Return true if this symbol is from a dynamic object.
514*fae548d3Szrj bool
is_from_dynobj()515*fae548d3Szrj is_from_dynobj() const
516*fae548d3Szrj {
517*fae548d3Szrj return this->source_ == FROM_OBJECT && this->object()->is_dynamic();
518*fae548d3Szrj }
519*fae548d3Szrj
520*fae548d3Szrj // Return whether this is a placeholder symbol from a plugin object.
521*fae548d3Szrj bool
is_placeholder()522*fae548d3Szrj is_placeholder() const
523*fae548d3Szrj {
524*fae548d3Szrj return this->source_ == FROM_OBJECT && this->object()->pluginobj() != NULL;
525*fae548d3Szrj }
526*fae548d3Szrj
527*fae548d3Szrj // Return whether this is an undefined symbol.
528*fae548d3Szrj bool
is_undefined()529*fae548d3Szrj is_undefined() const
530*fae548d3Szrj {
531*fae548d3Szrj bool is_ordinary;
532*fae548d3Szrj return ((this->source_ == FROM_OBJECT
533*fae548d3Szrj && this->shndx(&is_ordinary) == elfcpp::SHN_UNDEF
534*fae548d3Szrj && is_ordinary)
535*fae548d3Szrj || this->source_ == IS_UNDEFINED);
536*fae548d3Szrj }
537*fae548d3Szrj
538*fae548d3Szrj // Return whether this is a weak undefined symbol.
539*fae548d3Szrj bool
is_weak_undefined()540*fae548d3Szrj is_weak_undefined() const
541*fae548d3Szrj {
542*fae548d3Szrj return (this->is_undefined()
543*fae548d3Szrj && (this->binding() == elfcpp::STB_WEAK
544*fae548d3Szrj || this->is_undef_binding_weak()
545*fae548d3Szrj || parameters->options().weak_unresolved_symbols()));
546*fae548d3Szrj }
547*fae548d3Szrj
548*fae548d3Szrj // Return whether this is a strong undefined symbol.
549*fae548d3Szrj bool
is_strong_undefined()550*fae548d3Szrj is_strong_undefined() const
551*fae548d3Szrj {
552*fae548d3Szrj return (this->is_undefined()
553*fae548d3Szrj && this->binding() != elfcpp::STB_WEAK
554*fae548d3Szrj && !this->is_undef_binding_weak()
555*fae548d3Szrj && !parameters->options().weak_unresolved_symbols());
556*fae548d3Szrj }
557*fae548d3Szrj
558*fae548d3Szrj // Return whether this is an absolute symbol.
559*fae548d3Szrj bool
is_absolute()560*fae548d3Szrj is_absolute() const
561*fae548d3Szrj {
562*fae548d3Szrj bool is_ordinary;
563*fae548d3Szrj return ((this->source_ == FROM_OBJECT
564*fae548d3Szrj && this->shndx(&is_ordinary) == elfcpp::SHN_ABS
565*fae548d3Szrj && !is_ordinary)
566*fae548d3Szrj || this->source_ == IS_CONSTANT);
567*fae548d3Szrj }
568*fae548d3Szrj
569*fae548d3Szrj // Return whether this is a common symbol.
570*fae548d3Szrj bool
is_common()571*fae548d3Szrj is_common() const
572*fae548d3Szrj {
573*fae548d3Szrj if (this->source_ != FROM_OBJECT)
574*fae548d3Szrj return false;
575*fae548d3Szrj bool is_ordinary;
576*fae548d3Szrj unsigned int shndx = this->shndx(&is_ordinary);
577*fae548d3Szrj return !is_ordinary && Symbol::is_common_shndx(shndx);
578*fae548d3Szrj }
579*fae548d3Szrj
580*fae548d3Szrj // Return whether this symbol can be seen outside this object.
581*fae548d3Szrj bool
is_externally_visible()582*fae548d3Szrj is_externally_visible() const
583*fae548d3Szrj {
584*fae548d3Szrj return ((this->visibility_ == elfcpp::STV_DEFAULT
585*fae548d3Szrj || this->visibility_ == elfcpp::STV_PROTECTED)
586*fae548d3Szrj && !this->is_forced_local_);
587*fae548d3Szrj }
588*fae548d3Szrj
589*fae548d3Szrj // Return true if this symbol can be preempted by a definition in
590*fae548d3Szrj // another link unit.
591*fae548d3Szrj bool
is_preemptible()592*fae548d3Szrj is_preemptible() const
593*fae548d3Szrj {
594*fae548d3Szrj // It doesn't make sense to ask whether a symbol defined in
595*fae548d3Szrj // another object is preemptible.
596*fae548d3Szrj gold_assert(!this->is_from_dynobj());
597*fae548d3Szrj
598*fae548d3Szrj // It doesn't make sense to ask whether an undefined symbol
599*fae548d3Szrj // is preemptible.
600*fae548d3Szrj gold_assert(!this->is_undefined());
601*fae548d3Szrj
602*fae548d3Szrj // If a symbol does not have default visibility, it can not be
603*fae548d3Szrj // seen outside this link unit and therefore is not preemptible.
604*fae548d3Szrj if (this->visibility_ != elfcpp::STV_DEFAULT)
605*fae548d3Szrj return false;
606*fae548d3Szrj
607*fae548d3Szrj // If this symbol has been forced to be a local symbol by a
608*fae548d3Szrj // version script, then it is not visible outside this link unit
609*fae548d3Szrj // and is not preemptible.
610*fae548d3Szrj if (this->is_forced_local_)
611*fae548d3Szrj return false;
612*fae548d3Szrj
613*fae548d3Szrj // If we are not producing a shared library, then nothing is
614*fae548d3Szrj // preemptible.
615*fae548d3Szrj if (!parameters->options().shared())
616*fae548d3Szrj return false;
617*fae548d3Szrj
618*fae548d3Szrj // If the symbol was named in a --dynamic-list script, it is preemptible.
619*fae548d3Szrj if (parameters->options().in_dynamic_list(this->name()))
620*fae548d3Szrj return true;
621*fae548d3Szrj
622*fae548d3Szrj // If the user used -Bsymbolic, then nothing (else) is preemptible.
623*fae548d3Szrj if (parameters->options().Bsymbolic())
624*fae548d3Szrj return false;
625*fae548d3Szrj
626*fae548d3Szrj // If the user used -Bsymbolic-functions, then functions are not
627*fae548d3Szrj // preemptible. We explicitly check for not being STT_OBJECT,
628*fae548d3Szrj // rather than for being STT_FUNC, because that is what the GNU
629*fae548d3Szrj // linker does.
630*fae548d3Szrj if (this->type() != elfcpp::STT_OBJECT
631*fae548d3Szrj && parameters->options().Bsymbolic_functions())
632*fae548d3Szrj return false;
633*fae548d3Szrj
634*fae548d3Szrj // Otherwise the symbol is preemptible.
635*fae548d3Szrj return true;
636*fae548d3Szrj }
637*fae548d3Szrj
638*fae548d3Szrj // Return true if this symbol is a function that needs a PLT entry.
639*fae548d3Szrj bool
needs_plt_entry()640*fae548d3Szrj needs_plt_entry() const
641*fae548d3Szrj {
642*fae548d3Szrj // An undefined symbol from an executable does not need a PLT entry.
643*fae548d3Szrj if (this->is_undefined() && !parameters->options().shared())
644*fae548d3Szrj return false;
645*fae548d3Szrj
646*fae548d3Szrj // An STT_GNU_IFUNC symbol always needs a PLT entry, even when
647*fae548d3Szrj // doing a static link.
648*fae548d3Szrj if (this->type() == elfcpp::STT_GNU_IFUNC)
649*fae548d3Szrj return true;
650*fae548d3Szrj
651*fae548d3Szrj // We only need a PLT entry for a function.
652*fae548d3Szrj if (!this->is_func())
653*fae548d3Szrj return false;
654*fae548d3Szrj
655*fae548d3Szrj // If we're doing a static link or a -pie link, we don't create
656*fae548d3Szrj // PLT entries.
657*fae548d3Szrj if (parameters->doing_static_link()
658*fae548d3Szrj || parameters->options().pie())
659*fae548d3Szrj return false;
660*fae548d3Szrj
661*fae548d3Szrj // We need a PLT entry if the function is defined in a dynamic
662*fae548d3Szrj // object, or is undefined when building a shared object, or if it
663*fae548d3Szrj // is subject to pre-emption.
664*fae548d3Szrj return (this->is_from_dynobj()
665*fae548d3Szrj || this->is_undefined()
666*fae548d3Szrj || this->is_preemptible());
667*fae548d3Szrj }
668*fae548d3Szrj
669*fae548d3Szrj // When determining whether a reference to a symbol needs a dynamic
670*fae548d3Szrj // relocation, we need to know several things about the reference.
671*fae548d3Szrj // These flags may be or'ed together. 0 means that the symbol
672*fae548d3Szrj // isn't referenced at all.
673*fae548d3Szrj enum Reference_flags
674*fae548d3Szrj {
675*fae548d3Szrj // A reference to the symbol's absolute address. This includes
676*fae548d3Szrj // references that cause an absolute address to be stored in the GOT.
677*fae548d3Szrj ABSOLUTE_REF = 1,
678*fae548d3Szrj // A reference that calculates the offset of the symbol from some
679*fae548d3Szrj // anchor point, such as the PC or GOT.
680*fae548d3Szrj RELATIVE_REF = 2,
681*fae548d3Szrj // A TLS-related reference.
682*fae548d3Szrj TLS_REF = 4,
683*fae548d3Szrj // A reference that can always be treated as a function call.
684*fae548d3Szrj FUNCTION_CALL = 8,
685*fae548d3Szrj // When set, says that dynamic relocations are needed even if a
686*fae548d3Szrj // symbol has a plt entry.
687*fae548d3Szrj FUNC_DESC_ABI = 16,
688*fae548d3Szrj };
689*fae548d3Szrj
690*fae548d3Szrj // Given a direct absolute or pc-relative static relocation against
691*fae548d3Szrj // the global symbol, this function returns whether a dynamic relocation
692*fae548d3Szrj // is needed.
693*fae548d3Szrj
694*fae548d3Szrj bool
needs_dynamic_reloc(int flags)695*fae548d3Szrj needs_dynamic_reloc(int flags) const
696*fae548d3Szrj {
697*fae548d3Szrj // No dynamic relocations in a static link!
698*fae548d3Szrj if (parameters->doing_static_link())
699*fae548d3Szrj return false;
700*fae548d3Szrj
701*fae548d3Szrj // A reference to an undefined symbol from an executable should be
702*fae548d3Szrj // statically resolved to 0, and does not need a dynamic relocation.
703*fae548d3Szrj // This matches gnu ld behavior.
704*fae548d3Szrj if (this->is_undefined() && !parameters->options().shared())
705*fae548d3Szrj return false;
706*fae548d3Szrj
707*fae548d3Szrj // A reference to an absolute symbol does not need a dynamic relocation.
708*fae548d3Szrj if (this->is_absolute())
709*fae548d3Szrj return false;
710*fae548d3Szrj
711*fae548d3Szrj // An absolute reference within a position-independent output file
712*fae548d3Szrj // will need a dynamic relocation.
713*fae548d3Szrj if ((flags & ABSOLUTE_REF)
714*fae548d3Szrj && parameters->options().output_is_position_independent())
715*fae548d3Szrj return true;
716*fae548d3Szrj
717*fae548d3Szrj // A function call that can branch to a local PLT entry does not need
718*fae548d3Szrj // a dynamic relocation.
719*fae548d3Szrj if ((flags & FUNCTION_CALL) && this->has_plt_offset())
720*fae548d3Szrj return false;
721*fae548d3Szrj
722*fae548d3Szrj // A reference to any PLT entry in a non-position-independent executable
723*fae548d3Szrj // does not need a dynamic relocation.
724*fae548d3Szrj if (!(flags & FUNC_DESC_ABI)
725*fae548d3Szrj && !parameters->options().output_is_position_independent()
726*fae548d3Szrj && this->has_plt_offset())
727*fae548d3Szrj return false;
728*fae548d3Szrj
729*fae548d3Szrj // A reference to a symbol defined in a dynamic object or to a
730*fae548d3Szrj // symbol that is preemptible will need a dynamic relocation.
731*fae548d3Szrj if (this->is_from_dynobj()
732*fae548d3Szrj || this->is_undefined()
733*fae548d3Szrj || this->is_preemptible())
734*fae548d3Szrj return true;
735*fae548d3Szrj
736*fae548d3Szrj // For all other cases, return FALSE.
737*fae548d3Szrj return false;
738*fae548d3Szrj }
739*fae548d3Szrj
740*fae548d3Szrj // Whether we should use the PLT offset associated with a symbol for
741*fae548d3Szrj // a relocation. FLAGS is a set of Reference_flags.
742*fae548d3Szrj
743*fae548d3Szrj bool
use_plt_offset(int flags)744*fae548d3Szrj use_plt_offset(int flags) const
745*fae548d3Szrj {
746*fae548d3Szrj // If the symbol doesn't have a PLT offset, then naturally we
747*fae548d3Szrj // don't want to use it.
748*fae548d3Szrj if (!this->has_plt_offset())
749*fae548d3Szrj return false;
750*fae548d3Szrj
751*fae548d3Szrj // For a STT_GNU_IFUNC symbol we always have to use the PLT entry.
752*fae548d3Szrj if (this->type() == elfcpp::STT_GNU_IFUNC)
753*fae548d3Szrj return true;
754*fae548d3Szrj
755*fae548d3Szrj // If we are going to generate a dynamic relocation, then we will
756*fae548d3Szrj // wind up using that, so no need to use the PLT entry.
757*fae548d3Szrj if (this->needs_dynamic_reloc(flags))
758*fae548d3Szrj return false;
759*fae548d3Szrj
760*fae548d3Szrj // If the symbol is from a dynamic object, we need to use the PLT
761*fae548d3Szrj // entry.
762*fae548d3Szrj if (this->is_from_dynobj())
763*fae548d3Szrj return true;
764*fae548d3Szrj
765*fae548d3Szrj // If we are generating a shared object, and this symbol is
766*fae548d3Szrj // undefined or preemptible, we need to use the PLT entry.
767*fae548d3Szrj if (parameters->options().shared()
768*fae548d3Szrj && (this->is_undefined() || this->is_preemptible()))
769*fae548d3Szrj return true;
770*fae548d3Szrj
771*fae548d3Szrj // If this is a call to a weak undefined symbol, we need to use
772*fae548d3Szrj // the PLT entry; the symbol may be defined by a library loaded
773*fae548d3Szrj // at runtime.
774*fae548d3Szrj if ((flags & FUNCTION_CALL) && this->is_weak_undefined())
775*fae548d3Szrj return true;
776*fae548d3Szrj
777*fae548d3Szrj // Otherwise we can use the regular definition.
778*fae548d3Szrj return false;
779*fae548d3Szrj }
780*fae548d3Szrj
781*fae548d3Szrj // Given a direct absolute static relocation against
782*fae548d3Szrj // the global symbol, where a dynamic relocation is needed, this
783*fae548d3Szrj // function returns whether a relative dynamic relocation can be used.
784*fae548d3Szrj // The caller must determine separately whether the static relocation
785*fae548d3Szrj // is compatible with a relative relocation.
786*fae548d3Szrj
787*fae548d3Szrj bool
can_use_relative_reloc(bool is_function_call)788*fae548d3Szrj can_use_relative_reloc(bool is_function_call) const
789*fae548d3Szrj {
790*fae548d3Szrj // A function call that can branch to a local PLT entry can
791*fae548d3Szrj // use a RELATIVE relocation.
792*fae548d3Szrj if (is_function_call && this->has_plt_offset())
793*fae548d3Szrj return true;
794*fae548d3Szrj
795*fae548d3Szrj // A reference to a symbol defined in a dynamic object or to a
796*fae548d3Szrj // symbol that is preemptible can not use a RELATIVE relocation.
797*fae548d3Szrj if (this->is_from_dynobj()
798*fae548d3Szrj || this->is_undefined()
799*fae548d3Szrj || this->is_preemptible())
800*fae548d3Szrj return false;
801*fae548d3Szrj
802*fae548d3Szrj // For all other cases, return TRUE.
803*fae548d3Szrj return true;
804*fae548d3Szrj }
805*fae548d3Szrj
806*fae548d3Szrj // Return the output section where this symbol is defined. Return
807*fae548d3Szrj // NULL if the symbol has an absolute value.
808*fae548d3Szrj Output_section*
809*fae548d3Szrj output_section() const;
810*fae548d3Szrj
811*fae548d3Szrj // Set the symbol's output section. This is used for symbols
812*fae548d3Szrj // defined in scripts. This should only be called after the symbol
813*fae548d3Szrj // table has been finalized.
814*fae548d3Szrj void
815*fae548d3Szrj set_output_section(Output_section*);
816*fae548d3Szrj
817*fae548d3Szrj // Set the symbol's output segment. This is used for pre-defined
818*fae548d3Szrj // symbols whose segments aren't known until after layout is done
819*fae548d3Szrj // (e.g., __ehdr_start).
820*fae548d3Szrj void
821*fae548d3Szrj set_output_segment(Output_segment*, Segment_offset_base);
822*fae548d3Szrj
823*fae548d3Szrj // Set the symbol to undefined. This is used for pre-defined
824*fae548d3Szrj // symbols whose segments aren't known until after layout is done
825*fae548d3Szrj // (e.g., __ehdr_start).
826*fae548d3Szrj void
827*fae548d3Szrj set_undefined();
828*fae548d3Szrj
829*fae548d3Szrj // Return whether there should be a warning for references to this
830*fae548d3Szrj // symbol.
831*fae548d3Szrj bool
has_warning()832*fae548d3Szrj has_warning() const
833*fae548d3Szrj { return this->has_warning_; }
834*fae548d3Szrj
835*fae548d3Szrj // Mark this symbol as having a warning.
836*fae548d3Szrj void
set_has_warning()837*fae548d3Szrj set_has_warning()
838*fae548d3Szrj { this->has_warning_ = true; }
839*fae548d3Szrj
840*fae548d3Szrj // Return whether this symbol is defined by a COPY reloc from a
841*fae548d3Szrj // dynamic object.
842*fae548d3Szrj bool
is_copied_from_dynobj()843*fae548d3Szrj is_copied_from_dynobj() const
844*fae548d3Szrj { return this->is_copied_from_dynobj_; }
845*fae548d3Szrj
846*fae548d3Szrj // Mark this symbol as defined by a COPY reloc.
847*fae548d3Szrj void
set_is_copied_from_dynobj()848*fae548d3Szrj set_is_copied_from_dynobj()
849*fae548d3Szrj { this->is_copied_from_dynobj_ = true; }
850*fae548d3Szrj
851*fae548d3Szrj // Return whether this symbol is forced to visibility STB_LOCAL
852*fae548d3Szrj // by a "local:" entry in a version script.
853*fae548d3Szrj bool
is_forced_local()854*fae548d3Szrj is_forced_local() const
855*fae548d3Szrj { return this->is_forced_local_; }
856*fae548d3Szrj
857*fae548d3Szrj // Mark this symbol as forced to STB_LOCAL visibility.
858*fae548d3Szrj void
set_is_forced_local()859*fae548d3Szrj set_is_forced_local()
860*fae548d3Szrj { this->is_forced_local_ = true; }
861*fae548d3Szrj
862*fae548d3Szrj // Return true if this may need a COPY relocation.
863*fae548d3Szrj // References from an executable object to non-function symbols
864*fae548d3Szrj // defined in a dynamic object may need a COPY relocation.
865*fae548d3Szrj bool
may_need_copy_reloc()866*fae548d3Szrj may_need_copy_reloc() const
867*fae548d3Szrj {
868*fae548d3Szrj return (parameters->options().copyreloc()
869*fae548d3Szrj && this->is_from_dynobj()
870*fae548d3Szrj && !this->is_func());
871*fae548d3Szrj }
872*fae548d3Szrj
873*fae548d3Szrj // Return true if this symbol was predefined by the linker.
874*fae548d3Szrj bool
is_predefined()875*fae548d3Szrj is_predefined() const
876*fae548d3Szrj { return this->is_predefined_; }
877*fae548d3Szrj
878*fae548d3Szrj // Return true if this is a C++ vtable symbol.
879*fae548d3Szrj bool
is_cxx_vtable()880*fae548d3Szrj is_cxx_vtable() const
881*fae548d3Szrj { return is_prefix_of("_ZTV", this->name_); }
882*fae548d3Szrj
883*fae548d3Szrj // Return true if this symbol is protected in a shared object.
884*fae548d3Szrj // This is not the same as checking if visibility() == elfcpp::STV_PROTECTED,
885*fae548d3Szrj // because the visibility_ field reflects the symbol's visibility from
886*fae548d3Szrj // outside the shared object.
887*fae548d3Szrj bool
is_protected()888*fae548d3Szrj is_protected() const
889*fae548d3Szrj { return this->is_protected_; }
890*fae548d3Szrj
891*fae548d3Szrj // Mark this symbol as protected in a shared object.
892*fae548d3Szrj void
set_is_protected()893*fae548d3Szrj set_is_protected()
894*fae548d3Szrj { this->is_protected_ = true; }
895*fae548d3Szrj
896*fae548d3Szrj // Return state of PowerPC64 ELFv2 specific flag.
897*fae548d3Szrj bool
non_zero_localentry()898*fae548d3Szrj non_zero_localentry() const
899*fae548d3Szrj { return this->non_zero_localentry_; }
900*fae548d3Szrj
901*fae548d3Szrj // Set PowerPC64 ELFv2 specific flag.
902*fae548d3Szrj void
set_non_zero_localentry()903*fae548d3Szrj set_non_zero_localentry()
904*fae548d3Szrj { this->non_zero_localentry_ = true; }
905*fae548d3Szrj
906*fae548d3Szrj // Completely override existing symbol. Everything bar name_,
907*fae548d3Szrj // version_, and is_forced_local_ flag are copied. version_ is
908*fae548d3Szrj // cleared if from->version_ is clear. Returns true if this symbol
909*fae548d3Szrj // should be forced local.
910*fae548d3Szrj bool
911*fae548d3Szrj clone(const Symbol* from);
912*fae548d3Szrj
913*fae548d3Szrj protected:
914*fae548d3Szrj // Instances of this class should always be created at a specific
915*fae548d3Szrj // size.
Symbol()916*fae548d3Szrj Symbol()
917*fae548d3Szrj { memset(static_cast<void*>(this), 0, sizeof *this); }
918*fae548d3Szrj
919*fae548d3Szrj // Initialize the general fields.
920*fae548d3Szrj void
921*fae548d3Szrj init_fields(const char* name, const char* version,
922*fae548d3Szrj elfcpp::STT type, elfcpp::STB binding,
923*fae548d3Szrj elfcpp::STV visibility, unsigned char nonvis);
924*fae548d3Szrj
925*fae548d3Szrj // Initialize fields from an ELF symbol in OBJECT. ST_SHNDX is the
926*fae548d3Szrj // section index, IS_ORDINARY is whether it is a normal section
927*fae548d3Szrj // index rather than a special code.
928*fae548d3Szrj template<int size, bool big_endian>
929*fae548d3Szrj void
930*fae548d3Szrj init_base_object(const char* name, const char* version, Object* object,
931*fae548d3Szrj const elfcpp::Sym<size, big_endian>&, unsigned int st_shndx,
932*fae548d3Szrj bool is_ordinary);
933*fae548d3Szrj
934*fae548d3Szrj // Initialize fields for an Output_data.
935*fae548d3Szrj void
936*fae548d3Szrj init_base_output_data(const char* name, const char* version, Output_data*,
937*fae548d3Szrj elfcpp::STT, elfcpp::STB, elfcpp::STV,
938*fae548d3Szrj unsigned char nonvis, bool offset_is_from_end,
939*fae548d3Szrj bool is_predefined);
940*fae548d3Szrj
941*fae548d3Szrj // Initialize fields for an Output_segment.
942*fae548d3Szrj void
943*fae548d3Szrj init_base_output_segment(const char* name, const char* version,
944*fae548d3Szrj Output_segment* os, elfcpp::STT type,
945*fae548d3Szrj elfcpp::STB binding, elfcpp::STV visibility,
946*fae548d3Szrj unsigned char nonvis,
947*fae548d3Szrj Segment_offset_base offset_base,
948*fae548d3Szrj bool is_predefined);
949*fae548d3Szrj
950*fae548d3Szrj // Initialize fields for a constant.
951*fae548d3Szrj void
952*fae548d3Szrj init_base_constant(const char* name, const char* version, elfcpp::STT type,
953*fae548d3Szrj elfcpp::STB binding, elfcpp::STV visibility,
954*fae548d3Szrj unsigned char nonvis, bool is_predefined);
955*fae548d3Szrj
956*fae548d3Szrj // Initialize fields for an undefined symbol.
957*fae548d3Szrj void
958*fae548d3Szrj init_base_undefined(const char* name, const char* version, elfcpp::STT type,
959*fae548d3Szrj elfcpp::STB binding, elfcpp::STV visibility,
960*fae548d3Szrj unsigned char nonvis);
961*fae548d3Szrj
962*fae548d3Szrj // Override existing symbol.
963*fae548d3Szrj template<int size, bool big_endian>
964*fae548d3Szrj void
965*fae548d3Szrj override_base(const elfcpp::Sym<size, big_endian>&, unsigned int st_shndx,
966*fae548d3Szrj bool is_ordinary, Object* object, const char* version);
967*fae548d3Szrj
968*fae548d3Szrj // Override existing symbol with a special symbol.
969*fae548d3Szrj void
970*fae548d3Szrj override_base_with_special(const Symbol* from);
971*fae548d3Szrj
972*fae548d3Szrj // Override symbol version.
973*fae548d3Szrj void
974*fae548d3Szrj override_version(const char* version);
975*fae548d3Szrj
976*fae548d3Szrj // Allocate a common symbol by giving it a location in the output
977*fae548d3Szrj // file.
978*fae548d3Szrj void
979*fae548d3Szrj allocate_base_common(Output_data*);
980*fae548d3Szrj
981*fae548d3Szrj private:
982*fae548d3Szrj Symbol(const Symbol&);
983*fae548d3Szrj Symbol& operator=(const Symbol&);
984*fae548d3Szrj
985*fae548d3Szrj // Symbol name (expected to point into a Stringpool).
986*fae548d3Szrj const char* name_;
987*fae548d3Szrj // Symbol version (expected to point into a Stringpool). This may
988*fae548d3Szrj // be NULL.
989*fae548d3Szrj const char* version_;
990*fae548d3Szrj
991*fae548d3Szrj union
992*fae548d3Szrj {
993*fae548d3Szrj // This is used if SOURCE_ == FROM_OBJECT.
994*fae548d3Szrj // Object in which symbol is defined, or in which it was first
995*fae548d3Szrj // seen.
996*fae548d3Szrj Object* object;
997*fae548d3Szrj
998*fae548d3Szrj // This is used if SOURCE_ == IN_OUTPUT_DATA.
999*fae548d3Szrj // Output_data in which symbol is defined. Before
1000*fae548d3Szrj // Layout::finalize the symbol's value is an offset within the
1001*fae548d3Szrj // Output_data.
1002*fae548d3Szrj Output_data* output_data;
1003*fae548d3Szrj
1004*fae548d3Szrj // This is used if SOURCE_ == IN_OUTPUT_SEGMENT.
1005*fae548d3Szrj // Output_segment in which the symbol is defined. Before
1006*fae548d3Szrj // Layout::finalize the symbol's value is an offset.
1007*fae548d3Szrj Output_segment* output_segment;
1008*fae548d3Szrj } u1_;
1009*fae548d3Szrj
1010*fae548d3Szrj union
1011*fae548d3Szrj {
1012*fae548d3Szrj // This is used if SOURCE_ == FROM_OBJECT.
1013*fae548d3Szrj // Section number in object in which symbol is defined.
1014*fae548d3Szrj unsigned int shndx;
1015*fae548d3Szrj
1016*fae548d3Szrj // This is used if SOURCE_ == IN_OUTPUT_DATA.
1017*fae548d3Szrj // True if the offset is from the end, false if the offset is
1018*fae548d3Szrj // from the beginning.
1019*fae548d3Szrj bool offset_is_from_end;
1020*fae548d3Szrj
1021*fae548d3Szrj // This is used if SOURCE_ == IN_OUTPUT_SEGMENT.
1022*fae548d3Szrj // The base to use for the offset before Layout::finalize.
1023*fae548d3Szrj Segment_offset_base offset_base;
1024*fae548d3Szrj } u2_;
1025*fae548d3Szrj
1026*fae548d3Szrj // The index of this symbol in the output file. If the symbol is
1027*fae548d3Szrj // not going into the output file, this value is -1U. This field
1028*fae548d3Szrj // starts as always holding zero. It is set to a non-zero value by
1029*fae548d3Szrj // Symbol_table::finalize.
1030*fae548d3Szrj unsigned int symtab_index_;
1031*fae548d3Szrj
1032*fae548d3Szrj // The index of this symbol in the dynamic symbol table. If the
1033*fae548d3Szrj // symbol is not going into the dynamic symbol table, this value is
1034*fae548d3Szrj // -1U. This field starts as always holding zero. It is set to a
1035*fae548d3Szrj // non-zero value during Layout::finalize.
1036*fae548d3Szrj unsigned int dynsym_index_;
1037*fae548d3Szrj
1038*fae548d3Szrj // If this symbol has an entry in the PLT section, then this is the
1039*fae548d3Szrj // offset from the start of the PLT section. This is -1U if there
1040*fae548d3Szrj // is no PLT entry.
1041*fae548d3Szrj unsigned int plt_offset_;
1042*fae548d3Szrj
1043*fae548d3Szrj // The GOT section entries for this symbol. A symbol may have more
1044*fae548d3Szrj // than one GOT offset (e.g., when mixing modules compiled with two
1045*fae548d3Szrj // different TLS models), but will usually have at most one.
1046*fae548d3Szrj Got_offset_list got_offsets_;
1047*fae548d3Szrj
1048*fae548d3Szrj // Symbol type (bits 0 to 3).
1049*fae548d3Szrj elfcpp::STT type_ : 4;
1050*fae548d3Szrj // Symbol binding (bits 4 to 7).
1051*fae548d3Szrj elfcpp::STB binding_ : 4;
1052*fae548d3Szrj // Symbol visibility (bits 8 to 9).
1053*fae548d3Szrj elfcpp::STV visibility_ : 2;
1054*fae548d3Szrj // Rest of symbol st_other field (bits 10 to 15).
1055*fae548d3Szrj unsigned int nonvis_ : 6;
1056*fae548d3Szrj // The type of symbol (bits 16 to 18).
1057*fae548d3Szrj Source source_ : 3;
1058*fae548d3Szrj // True if this is the default version of the symbol (bit 19).
1059*fae548d3Szrj bool is_def_ : 1;
1060*fae548d3Szrj // True if this symbol really forwards to another symbol. This is
1061*fae548d3Szrj // used when we discover after the fact that two different entries
1062*fae548d3Szrj // in the hash table really refer to the same symbol. This will
1063*fae548d3Szrj // never be set for a symbol found in the hash table, but may be set
1064*fae548d3Szrj // for a symbol found in the list of symbols attached to an Object.
1065*fae548d3Szrj // It forwards to the symbol found in the forwarders_ map of
1066*fae548d3Szrj // Symbol_table (bit 20).
1067*fae548d3Szrj bool is_forwarder_ : 1;
1068*fae548d3Szrj // True if the symbol has an alias in the weak_aliases table in
1069*fae548d3Szrj // Symbol_table (bit 21).
1070*fae548d3Szrj bool has_alias_ : 1;
1071*fae548d3Szrj // True if this symbol needs to be in the dynamic symbol table (bit
1072*fae548d3Szrj // 22).
1073*fae548d3Szrj bool needs_dynsym_entry_ : 1;
1074*fae548d3Szrj // True if we've seen this symbol in a regular object (bit 23).
1075*fae548d3Szrj bool in_reg_ : 1;
1076*fae548d3Szrj // True if we've seen this symbol in a dynamic object (bit 24).
1077*fae548d3Szrj bool in_dyn_ : 1;
1078*fae548d3Szrj // True if this is a dynamic symbol which needs a special value in
1079*fae548d3Szrj // the dynamic symbol table (bit 25).
1080*fae548d3Szrj bool needs_dynsym_value_ : 1;
1081*fae548d3Szrj // True if there is a warning for this symbol (bit 26).
1082*fae548d3Szrj bool has_warning_ : 1;
1083*fae548d3Szrj // True if we are using a COPY reloc for this symbol, so that the
1084*fae548d3Szrj // real definition lives in a dynamic object (bit 27).
1085*fae548d3Szrj bool is_copied_from_dynobj_ : 1;
1086*fae548d3Szrj // True if this symbol was forced to local visibility by a version
1087*fae548d3Szrj // script (bit 28).
1088*fae548d3Szrj bool is_forced_local_ : 1;
1089*fae548d3Szrj // True if the field u2_.shndx is an ordinary section
1090*fae548d3Szrj // index, not one of the special codes from SHN_LORESERVE to
1091*fae548d3Szrj // SHN_HIRESERVE (bit 29).
1092*fae548d3Szrj bool is_ordinary_shndx_ : 1;
1093*fae548d3Szrj // True if we've seen this symbol in a "real" ELF object (bit 30).
1094*fae548d3Szrj // If the symbol has been seen in a relocatable, non-IR, object file,
1095*fae548d3Szrj // it's known to be referenced from outside the IR. A reference from
1096*fae548d3Szrj // a dynamic object doesn't count as a "real" ELF, and we'll simply
1097*fae548d3Szrj // mark the symbol as "visible" from outside the IR. The compiler
1098*fae548d3Szrj // can use this distinction to guide its handling of COMDAT symbols.
1099*fae548d3Szrj bool in_real_elf_ : 1;
1100*fae548d3Szrj // True if this symbol is defined in a section which was discarded
1101*fae548d3Szrj // (bit 31).
1102*fae548d3Szrj bool is_defined_in_discarded_section_ : 1;
1103*fae548d3Szrj // True if UNDEF_BINDING_WEAK_ has been set (bit 32).
1104*fae548d3Szrj bool undef_binding_set_ : 1;
1105*fae548d3Szrj // True if this symbol was a weak undef resolved by a dynamic def
1106*fae548d3Szrj // or by a special symbol (bit 33).
1107*fae548d3Szrj bool undef_binding_weak_ : 1;
1108*fae548d3Szrj // True if this symbol is a predefined linker symbol (bit 34).
1109*fae548d3Szrj bool is_predefined_ : 1;
1110*fae548d3Szrj // True if this symbol has protected visibility in a shared object (bit 35).
1111*fae548d3Szrj // The visibility_ field will be STV_DEFAULT in this case because we
1112*fae548d3Szrj // must treat it as such from outside the shared object.
1113*fae548d3Szrj bool is_protected_ : 1;
1114*fae548d3Szrj // Used by PowerPC64 ELFv2 to track st_other localentry (bit 36).
1115*fae548d3Szrj bool non_zero_localentry_ : 1;
1116*fae548d3Szrj };
1117*fae548d3Szrj
1118*fae548d3Szrj // The parts of a symbol which are size specific. Using a template
1119*fae548d3Szrj // derived class like this helps us use less space on a 32-bit system.
1120*fae548d3Szrj
1121*fae548d3Szrj template<int size>
1122*fae548d3Szrj class Sized_symbol : public Symbol
1123*fae548d3Szrj {
1124*fae548d3Szrj public:
1125*fae548d3Szrj typedef typename elfcpp::Elf_types<size>::Elf_Addr Value_type;
1126*fae548d3Szrj typedef typename elfcpp::Elf_types<size>::Elf_WXword Size_type;
1127*fae548d3Szrj
Sized_symbol()1128*fae548d3Szrj Sized_symbol()
1129*fae548d3Szrj { }
1130*fae548d3Szrj
1131*fae548d3Szrj // Initialize fields from an ELF symbol in OBJECT. ST_SHNDX is the
1132*fae548d3Szrj // section index, IS_ORDINARY is whether it is a normal section
1133*fae548d3Szrj // index rather than a special code.
1134*fae548d3Szrj template<bool big_endian>
1135*fae548d3Szrj void
1136*fae548d3Szrj init_object(const char* name, const char* version, Object* object,
1137*fae548d3Szrj const elfcpp::Sym<size, big_endian>&, unsigned int st_shndx,
1138*fae548d3Szrj bool is_ordinary);
1139*fae548d3Szrj
1140*fae548d3Szrj // Initialize fields for an Output_data.
1141*fae548d3Szrj void
1142*fae548d3Szrj init_output_data(const char* name, const char* version, Output_data*,
1143*fae548d3Szrj Value_type value, Size_type symsize, elfcpp::STT,
1144*fae548d3Szrj elfcpp::STB, elfcpp::STV, unsigned char nonvis,
1145*fae548d3Szrj bool offset_is_from_end, bool is_predefined);
1146*fae548d3Szrj
1147*fae548d3Szrj // Initialize fields for an Output_segment.
1148*fae548d3Szrj void
1149*fae548d3Szrj init_output_segment(const char* name, const char* version, Output_segment*,
1150*fae548d3Szrj Value_type value, Size_type symsize, elfcpp::STT,
1151*fae548d3Szrj elfcpp::STB, elfcpp::STV, unsigned char nonvis,
1152*fae548d3Szrj Segment_offset_base offset_base, bool is_predefined);
1153*fae548d3Szrj
1154*fae548d3Szrj // Initialize fields for a constant.
1155*fae548d3Szrj void
1156*fae548d3Szrj init_constant(const char* name, const char* version, Value_type value,
1157*fae548d3Szrj Size_type symsize, elfcpp::STT, elfcpp::STB, elfcpp::STV,
1158*fae548d3Szrj unsigned char nonvis, bool is_predefined);
1159*fae548d3Szrj
1160*fae548d3Szrj // Initialize fields for an undefined symbol.
1161*fae548d3Szrj void
1162*fae548d3Szrj init_undefined(const char* name, const char* version, Value_type value,
1163*fae548d3Szrj elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis);
1164*fae548d3Szrj
1165*fae548d3Szrj // Override existing symbol.
1166*fae548d3Szrj template<bool big_endian>
1167*fae548d3Szrj void
1168*fae548d3Szrj override(const elfcpp::Sym<size, big_endian>&, unsigned int st_shndx,
1169*fae548d3Szrj bool is_ordinary, Object* object, const char* version);
1170*fae548d3Szrj
1171*fae548d3Szrj // Override existing symbol with a special symbol.
1172*fae548d3Szrj void
1173*fae548d3Szrj override_with_special(const Sized_symbol<size>*);
1174*fae548d3Szrj
1175*fae548d3Szrj // Return the symbol's value.
1176*fae548d3Szrj Value_type
value()1177*fae548d3Szrj value() const
1178*fae548d3Szrj { return this->value_; }
1179*fae548d3Szrj
1180*fae548d3Szrj // Return the symbol's size (we can't call this 'size' because that
1181*fae548d3Szrj // is a template parameter).
1182*fae548d3Szrj Size_type
symsize()1183*fae548d3Szrj symsize() const
1184*fae548d3Szrj { return this->symsize_; }
1185*fae548d3Szrj
1186*fae548d3Szrj // Set the symbol size. This is used when resolving common symbols.
1187*fae548d3Szrj void
set_symsize(Size_type symsize)1188*fae548d3Szrj set_symsize(Size_type symsize)
1189*fae548d3Szrj { this->symsize_ = symsize; }
1190*fae548d3Szrj
1191*fae548d3Szrj // Set the symbol value. This is called when we store the final
1192*fae548d3Szrj // values of the symbols into the symbol table.
1193*fae548d3Szrj void
set_value(Value_type value)1194*fae548d3Szrj set_value(Value_type value)
1195*fae548d3Szrj { this->value_ = value; }
1196*fae548d3Szrj
1197*fae548d3Szrj // Allocate a common symbol by giving it a location in the output
1198*fae548d3Szrj // file.
1199*fae548d3Szrj void
1200*fae548d3Szrj allocate_common(Output_data*, Value_type value);
1201*fae548d3Szrj
1202*fae548d3Szrj // Completely override existing symbol. Everything bar name_,
1203*fae548d3Szrj // version_, and is_forced_local_ flag are copied. version_ is
1204*fae548d3Szrj // cleared if from->version_ is clear. Returns true if this symbol
1205*fae548d3Szrj // should be forced local.
1206*fae548d3Szrj bool
1207*fae548d3Szrj clone(const Sized_symbol<size>* from);
1208*fae548d3Szrj
1209*fae548d3Szrj private:
1210*fae548d3Szrj Sized_symbol(const Sized_symbol&);
1211*fae548d3Szrj Sized_symbol& operator=(const Sized_symbol&);
1212*fae548d3Szrj
1213*fae548d3Szrj // Symbol value. Before Layout::finalize this is the offset in the
1214*fae548d3Szrj // input section. This is set to the final value during
1215*fae548d3Szrj // Layout::finalize.
1216*fae548d3Szrj Value_type value_;
1217*fae548d3Szrj // Symbol size.
1218*fae548d3Szrj Size_type symsize_;
1219*fae548d3Szrj };
1220*fae548d3Szrj
1221*fae548d3Szrj // A struct describing a symbol defined by the linker, where the value
1222*fae548d3Szrj // of the symbol is defined based on an output section. This is used
1223*fae548d3Szrj // for symbols defined by the linker, like "_init_array_start".
1224*fae548d3Szrj
1225*fae548d3Szrj struct Define_symbol_in_section
1226*fae548d3Szrj {
1227*fae548d3Szrj // The symbol name.
1228*fae548d3Szrj const char* name;
1229*fae548d3Szrj // The name of the output section with which this symbol should be
1230*fae548d3Szrj // associated. If there is no output section with that name, the
1231*fae548d3Szrj // symbol will be defined as zero.
1232*fae548d3Szrj const char* output_section;
1233*fae548d3Szrj // The offset of the symbol within the output section. This is an
1234*fae548d3Szrj // offset from the start of the output section, unless start_at_end
1235*fae548d3Szrj // is true, in which case this is an offset from the end of the
1236*fae548d3Szrj // output section.
1237*fae548d3Szrj uint64_t value;
1238*fae548d3Szrj // The size of the symbol.
1239*fae548d3Szrj uint64_t size;
1240*fae548d3Szrj // The symbol type.
1241*fae548d3Szrj elfcpp::STT type;
1242*fae548d3Szrj // The symbol binding.
1243*fae548d3Szrj elfcpp::STB binding;
1244*fae548d3Szrj // The symbol visibility.
1245*fae548d3Szrj elfcpp::STV visibility;
1246*fae548d3Szrj // The rest of the st_other field.
1247*fae548d3Szrj unsigned char nonvis;
1248*fae548d3Szrj // If true, the value field is an offset from the end of the output
1249*fae548d3Szrj // section.
1250*fae548d3Szrj bool offset_is_from_end;
1251*fae548d3Szrj // If true, this symbol is defined only if we see a reference to it.
1252*fae548d3Szrj bool only_if_ref;
1253*fae548d3Szrj };
1254*fae548d3Szrj
1255*fae548d3Szrj // A struct describing a symbol defined by the linker, where the value
1256*fae548d3Szrj // of the symbol is defined based on a segment. This is used for
1257*fae548d3Szrj // symbols defined by the linker, like "_end". We describe the
1258*fae548d3Szrj // segment with which the symbol should be associated by its
1259*fae548d3Szrj // characteristics. If no segment meets these characteristics, the
1260*fae548d3Szrj // symbol will be defined as zero. If there is more than one segment
1261*fae548d3Szrj // which meets these characteristics, we will use the first one.
1262*fae548d3Szrj
1263*fae548d3Szrj struct Define_symbol_in_segment
1264*fae548d3Szrj {
1265*fae548d3Szrj // The symbol name.
1266*fae548d3Szrj const char* name;
1267*fae548d3Szrj // The segment type where the symbol should be defined, typically
1268*fae548d3Szrj // PT_LOAD.
1269*fae548d3Szrj elfcpp::PT segment_type;
1270*fae548d3Szrj // Bitmask of segment flags which must be set.
1271*fae548d3Szrj elfcpp::PF segment_flags_set;
1272*fae548d3Szrj // Bitmask of segment flags which must be clear.
1273*fae548d3Szrj elfcpp::PF segment_flags_clear;
1274*fae548d3Szrj // The offset of the symbol within the segment. The offset is
1275*fae548d3Szrj // calculated from the position set by offset_base.
1276*fae548d3Szrj uint64_t value;
1277*fae548d3Szrj // The size of the symbol.
1278*fae548d3Szrj uint64_t size;
1279*fae548d3Szrj // The symbol type.
1280*fae548d3Szrj elfcpp::STT type;
1281*fae548d3Szrj // The symbol binding.
1282*fae548d3Szrj elfcpp::STB binding;
1283*fae548d3Szrj // The symbol visibility.
1284*fae548d3Szrj elfcpp::STV visibility;
1285*fae548d3Szrj // The rest of the st_other field.
1286*fae548d3Szrj unsigned char nonvis;
1287*fae548d3Szrj // The base from which we compute the offset.
1288*fae548d3Szrj Symbol::Segment_offset_base offset_base;
1289*fae548d3Szrj // If true, this symbol is defined only if we see a reference to it.
1290*fae548d3Szrj bool only_if_ref;
1291*fae548d3Szrj };
1292*fae548d3Szrj
1293*fae548d3Szrj // Specify an object/section/offset location. Used by ODR code.
1294*fae548d3Szrj
1295*fae548d3Szrj struct Symbol_location
1296*fae548d3Szrj {
1297*fae548d3Szrj // Object where the symbol is defined.
1298*fae548d3Szrj Object* object;
1299*fae548d3Szrj // Section-in-object where the symbol is defined.
1300*fae548d3Szrj unsigned int shndx;
1301*fae548d3Szrj // For relocatable objects, offset-in-section where the symbol is defined.
1302*fae548d3Szrj // For dynamic objects, address where the symbol is defined.
1303*fae548d3Szrj off_t offset;
1304*fae548d3Szrj bool operator==(const Symbol_location& that) const
1305*fae548d3Szrj {
1306*fae548d3Szrj return (this->object == that.object
1307*fae548d3Szrj && this->shndx == that.shndx
1308*fae548d3Szrj && this->offset == that.offset);
1309*fae548d3Szrj }
1310*fae548d3Szrj };
1311*fae548d3Szrj
1312*fae548d3Szrj // A map from symbol name (as a pointer into the namepool) to all
1313*fae548d3Szrj // the locations the symbols is (weakly) defined (and certain other
1314*fae548d3Szrj // conditions are met). This map will be used later to detect
1315*fae548d3Szrj // possible One Definition Rule (ODR) violations.
1316*fae548d3Szrj struct Symbol_location_hash
1317*fae548d3Szrj {
operatorSymbol_location_hash1318*fae548d3Szrj size_t operator()(const Symbol_location& loc) const
1319*fae548d3Szrj { return reinterpret_cast<uintptr_t>(loc.object) ^ loc.offset ^ loc.shndx; }
1320*fae548d3Szrj };
1321*fae548d3Szrj
1322*fae548d3Szrj // This class manages warnings. Warnings are a GNU extension. When
1323*fae548d3Szrj // we see a section named .gnu.warning.SYM in an object file, and if
1324*fae548d3Szrj // we wind using the definition of SYM from that object file, then we
1325*fae548d3Szrj // will issue a warning for any relocation against SYM from a
1326*fae548d3Szrj // different object file. The text of the warning is the contents of
1327*fae548d3Szrj // the section. This is not precisely the definition used by the old
1328*fae548d3Szrj // GNU linker; the old GNU linker treated an occurrence of
1329*fae548d3Szrj // .gnu.warning.SYM as defining a warning symbol. A warning symbol
1330*fae548d3Szrj // would trigger a warning on any reference. However, it was
1331*fae548d3Szrj // inconsistent in that a warning in a dynamic object only triggered
1332*fae548d3Szrj // if there was no definition in a regular object. This linker is
1333*fae548d3Szrj // different in that we only issue a warning if we use the symbol
1334*fae548d3Szrj // definition from the same object file as the warning section.
1335*fae548d3Szrj
1336*fae548d3Szrj class Warnings
1337*fae548d3Szrj {
1338*fae548d3Szrj public:
Warnings()1339*fae548d3Szrj Warnings()
1340*fae548d3Szrj : warnings_()
1341*fae548d3Szrj { }
1342*fae548d3Szrj
1343*fae548d3Szrj // Add a warning for symbol NAME in object OBJ. WARNING is the text
1344*fae548d3Szrj // of the warning.
1345*fae548d3Szrj void
1346*fae548d3Szrj add_warning(Symbol_table* symtab, const char* name, Object* obj,
1347*fae548d3Szrj const std::string& warning);
1348*fae548d3Szrj
1349*fae548d3Szrj // For each symbol for which we should give a warning, make a note
1350*fae548d3Szrj // on the symbol.
1351*fae548d3Szrj void
1352*fae548d3Szrj note_warnings(Symbol_table* symtab);
1353*fae548d3Szrj
1354*fae548d3Szrj // Issue a warning for a reference to SYM at RELINFO's location.
1355*fae548d3Szrj template<int size, bool big_endian>
1356*fae548d3Szrj void
1357*fae548d3Szrj issue_warning(const Symbol* sym, const Relocate_info<size, big_endian>*,
1358*fae548d3Szrj size_t relnum, off_t reloffset) const;
1359*fae548d3Szrj
1360*fae548d3Szrj private:
1361*fae548d3Szrj Warnings(const Warnings&);
1362*fae548d3Szrj Warnings& operator=(const Warnings&);
1363*fae548d3Szrj
1364*fae548d3Szrj // What we need to know to get the warning text.
1365*fae548d3Szrj struct Warning_location
1366*fae548d3Szrj {
1367*fae548d3Szrj // The object the warning is in.
1368*fae548d3Szrj Object* object;
1369*fae548d3Szrj // The warning text.
1370*fae548d3Szrj std::string text;
1371*fae548d3Szrj
Warning_locationWarning_location1372*fae548d3Szrj Warning_location()
1373*fae548d3Szrj : object(NULL), text()
1374*fae548d3Szrj { }
1375*fae548d3Szrj
1376*fae548d3Szrj void
setWarning_location1377*fae548d3Szrj set(Object* o, const std::string& t)
1378*fae548d3Szrj {
1379*fae548d3Szrj this->object = o;
1380*fae548d3Szrj this->text = t;
1381*fae548d3Szrj }
1382*fae548d3Szrj };
1383*fae548d3Szrj
1384*fae548d3Szrj // A mapping from warning symbol names (canonicalized in
1385*fae548d3Szrj // Symbol_table's namepool_ field) to warning information.
1386*fae548d3Szrj typedef Unordered_map<const char*, Warning_location> Warning_table;
1387*fae548d3Szrj
1388*fae548d3Szrj Warning_table warnings_;
1389*fae548d3Szrj };
1390*fae548d3Szrj
1391*fae548d3Szrj // The main linker symbol table.
1392*fae548d3Szrj
1393*fae548d3Szrj class Symbol_table
1394*fae548d3Szrj {
1395*fae548d3Szrj public:
1396*fae548d3Szrj // The different places where a symbol definition can come from.
1397*fae548d3Szrj enum Defined
1398*fae548d3Szrj {
1399*fae548d3Szrj // Defined in an object file--the normal case.
1400*fae548d3Szrj OBJECT,
1401*fae548d3Szrj // Defined for a COPY reloc.
1402*fae548d3Szrj COPY,
1403*fae548d3Szrj // Defined on the command line using --defsym.
1404*fae548d3Szrj DEFSYM,
1405*fae548d3Szrj // Defined (so to speak) on the command line using -u.
1406*fae548d3Szrj UNDEFINED,
1407*fae548d3Szrj // Defined in a linker script.
1408*fae548d3Szrj SCRIPT,
1409*fae548d3Szrj // Predefined by the linker.
1410*fae548d3Szrj PREDEFINED,
1411*fae548d3Szrj // Defined by the linker during an incremental base link, but not
1412*fae548d3Szrj // a predefined symbol (e.g., common, defined in script).
1413*fae548d3Szrj INCREMENTAL_BASE,
1414*fae548d3Szrj };
1415*fae548d3Szrj
1416*fae548d3Szrj // The order in which we sort common symbols.
1417*fae548d3Szrj enum Sort_commons_order
1418*fae548d3Szrj {
1419*fae548d3Szrj SORT_COMMONS_BY_SIZE_DESCENDING,
1420*fae548d3Szrj SORT_COMMONS_BY_ALIGNMENT_DESCENDING,
1421*fae548d3Szrj SORT_COMMONS_BY_ALIGNMENT_ASCENDING
1422*fae548d3Szrj };
1423*fae548d3Szrj
1424*fae548d3Szrj // COUNT is an estimate of how many symbols will be inserted in the
1425*fae548d3Szrj // symbol table. It's ok to put 0 if you don't know; a correct
1426*fae548d3Szrj // guess will just save some CPU by reducing hashtable resizes.
1427*fae548d3Szrj Symbol_table(unsigned int count, const Version_script_info& version_script);
1428*fae548d3Szrj
1429*fae548d3Szrj ~Symbol_table();
1430*fae548d3Szrj
1431*fae548d3Szrj void
set_icf(Icf * icf)1432*fae548d3Szrj set_icf(Icf* icf)
1433*fae548d3Szrj { this->icf_ = icf;}
1434*fae548d3Szrj
1435*fae548d3Szrj Icf*
icf()1436*fae548d3Szrj icf() const
1437*fae548d3Szrj { return this->icf_; }
1438*fae548d3Szrj
1439*fae548d3Szrj // Returns true if ICF determined that this is a duplicate section.
1440*fae548d3Szrj bool
1441*fae548d3Szrj is_section_folded(Relobj* obj, unsigned int shndx) const;
1442*fae548d3Szrj
1443*fae548d3Szrj void
set_gc(Garbage_collection * gc)1444*fae548d3Szrj set_gc(Garbage_collection* gc)
1445*fae548d3Szrj { this->gc_ = gc; }
1446*fae548d3Szrj
1447*fae548d3Szrj Garbage_collection*
gc()1448*fae548d3Szrj gc() const
1449*fae548d3Szrj { return this->gc_; }
1450*fae548d3Szrj
1451*fae548d3Szrj // During garbage collection, this keeps undefined symbols.
1452*fae548d3Szrj void
1453*fae548d3Szrj gc_mark_undef_symbols(Layout*);
1454*fae548d3Szrj
1455*fae548d3Szrj // This tells garbage collection that this symbol is referenced.
1456*fae548d3Szrj void
1457*fae548d3Szrj gc_mark_symbol(Symbol* sym);
1458*fae548d3Szrj
1459*fae548d3Szrj // During garbage collection, this keeps sections that correspond to
1460*fae548d3Szrj // symbols seen in dynamic objects.
1461*fae548d3Szrj inline void
1462*fae548d3Szrj gc_mark_dyn_syms(Symbol* sym);
1463*fae548d3Szrj
1464*fae548d3Szrj // Add COUNT external symbols from the relocatable object RELOBJ to
1465*fae548d3Szrj // the symbol table. SYMS is the symbols, SYMNDX_OFFSET is the
1466*fae548d3Szrj // offset in the symbol table of the first symbol, SYM_NAMES is
1467*fae548d3Szrj // their names, SYM_NAME_SIZE is the size of SYM_NAMES. This sets
1468*fae548d3Szrj // SYMPOINTERS to point to the symbols in the symbol table. It sets
1469*fae548d3Szrj // *DEFINED to the number of defined symbols.
1470*fae548d3Szrj template<int size, bool big_endian>
1471*fae548d3Szrj void
1472*fae548d3Szrj add_from_relobj(Sized_relobj_file<size, big_endian>* relobj,
1473*fae548d3Szrj const unsigned char* syms, size_t count,
1474*fae548d3Szrj size_t symndx_offset, const char* sym_names,
1475*fae548d3Szrj size_t sym_name_size,
1476*fae548d3Szrj typename Sized_relobj_file<size, big_endian>::Symbols*,
1477*fae548d3Szrj size_t* defined);
1478*fae548d3Szrj
1479*fae548d3Szrj // Add one external symbol from the plugin object OBJ to the symbol table.
1480*fae548d3Szrj // Returns a pointer to the resolved symbol in the symbol table.
1481*fae548d3Szrj template<int size, bool big_endian>
1482*fae548d3Szrj Symbol*
1483*fae548d3Szrj add_from_pluginobj(Sized_pluginobj<size, big_endian>* obj,
1484*fae548d3Szrj const char* name, const char* ver,
1485*fae548d3Szrj elfcpp::Sym<size, big_endian>* sym);
1486*fae548d3Szrj
1487*fae548d3Szrj // Add COUNT dynamic symbols from the dynamic object DYNOBJ to the
1488*fae548d3Szrj // symbol table. SYMS is the symbols. SYM_NAMES is their names.
1489*fae548d3Szrj // SYM_NAME_SIZE is the size of SYM_NAMES. The other parameters are
1490*fae548d3Szrj // symbol version data.
1491*fae548d3Szrj template<int size, bool big_endian>
1492*fae548d3Szrj void
1493*fae548d3Szrj add_from_dynobj(Sized_dynobj<size, big_endian>* dynobj,
1494*fae548d3Szrj const unsigned char* syms, size_t count,
1495*fae548d3Szrj const char* sym_names, size_t sym_name_size,
1496*fae548d3Szrj const unsigned char* versym, size_t versym_size,
1497*fae548d3Szrj const std::vector<const char*>*,
1498*fae548d3Szrj typename Sized_relobj_file<size, big_endian>::Symbols*,
1499*fae548d3Szrj size_t* defined);
1500*fae548d3Szrj
1501*fae548d3Szrj // Add one external symbol from the incremental object OBJ to the symbol
1502*fae548d3Szrj // table. Returns a pointer to the resolved symbol in the symbol table.
1503*fae548d3Szrj template<int size, bool big_endian>
1504*fae548d3Szrj Sized_symbol<size>*
1505*fae548d3Szrj add_from_incrobj(Object* obj, const char* name,
1506*fae548d3Szrj const char* ver, elfcpp::Sym<size, big_endian>* sym);
1507*fae548d3Szrj
1508*fae548d3Szrj // Define a special symbol based on an Output_data. It is a
1509*fae548d3Szrj // multiple definition error if this symbol is already defined.
1510*fae548d3Szrj Symbol*
1511*fae548d3Szrj define_in_output_data(const char* name, const char* version, Defined,
1512*fae548d3Szrj Output_data*, uint64_t value, uint64_t symsize,
1513*fae548d3Szrj elfcpp::STT type, elfcpp::STB binding,
1514*fae548d3Szrj elfcpp::STV visibility, unsigned char nonvis,
1515*fae548d3Szrj bool offset_is_from_end, bool only_if_ref);
1516*fae548d3Szrj
1517*fae548d3Szrj // Define a special symbol based on an Output_segment. It is a
1518*fae548d3Szrj // multiple definition error if this symbol is already defined.
1519*fae548d3Szrj Symbol*
1520*fae548d3Szrj define_in_output_segment(const char* name, const char* version, Defined,
1521*fae548d3Szrj Output_segment*, uint64_t value, uint64_t symsize,
1522*fae548d3Szrj elfcpp::STT type, elfcpp::STB binding,
1523*fae548d3Szrj elfcpp::STV visibility, unsigned char nonvis,
1524*fae548d3Szrj Symbol::Segment_offset_base, bool only_if_ref);
1525*fae548d3Szrj
1526*fae548d3Szrj // Define a special symbol with a constant value. It is a multiple
1527*fae548d3Szrj // definition error if this symbol is already defined.
1528*fae548d3Szrj Symbol*
1529*fae548d3Szrj define_as_constant(const char* name, const char* version, Defined,
1530*fae548d3Szrj uint64_t value, uint64_t symsize, elfcpp::STT type,
1531*fae548d3Szrj elfcpp::STB binding, elfcpp::STV visibility,
1532*fae548d3Szrj unsigned char nonvis, bool only_if_ref,
1533*fae548d3Szrj bool force_override);
1534*fae548d3Szrj
1535*fae548d3Szrj // Define a set of symbols in output sections. If ONLY_IF_REF is
1536*fae548d3Szrj // true, only define them if they are referenced.
1537*fae548d3Szrj void
1538*fae548d3Szrj define_symbols(const Layout*, int count, const Define_symbol_in_section*,
1539*fae548d3Szrj bool only_if_ref);
1540*fae548d3Szrj
1541*fae548d3Szrj // Define a set of symbols in output segments. If ONLY_IF_REF is
1542*fae548d3Szrj // true, only defined them if they are referenced.
1543*fae548d3Szrj void
1544*fae548d3Szrj define_symbols(const Layout*, int count, const Define_symbol_in_segment*,
1545*fae548d3Szrj bool only_if_ref);
1546*fae548d3Szrj
1547*fae548d3Szrj // Add a target-specific global symbol.
1548*fae548d3Szrj // (Used by SPARC backend to add STT_SPARC_REGISTER symbols.)
1549*fae548d3Szrj void
add_target_global_symbol(Symbol * sym)1550*fae548d3Szrj add_target_global_symbol(Symbol* sym)
1551*fae548d3Szrj { this->target_symbols_.push_back(sym); }
1552*fae548d3Szrj
1553*fae548d3Szrj // Define SYM using a COPY reloc. POSD is the Output_data where the
1554*fae548d3Szrj // symbol should be defined--typically a .dyn.bss section. VALUE is
1555*fae548d3Szrj // the offset within POSD.
1556*fae548d3Szrj template<int size>
1557*fae548d3Szrj void
1558*fae548d3Szrj define_with_copy_reloc(Sized_symbol<size>* sym, Output_data* posd,
1559*fae548d3Szrj typename elfcpp::Elf_types<size>::Elf_Addr);
1560*fae548d3Szrj
1561*fae548d3Szrj // Look up a symbol.
1562*fae548d3Szrj Symbol*
1563*fae548d3Szrj lookup(const char*, const char* version = NULL) const;
1564*fae548d3Szrj
1565*fae548d3Szrj // Return the real symbol associated with the forwarder symbol FROM.
1566*fae548d3Szrj Symbol*
1567*fae548d3Szrj resolve_forwards(const Symbol* from) const;
1568*fae548d3Szrj
1569*fae548d3Szrj // Return the sized version of a symbol in this table.
1570*fae548d3Szrj template<int size>
1571*fae548d3Szrj Sized_symbol<size>*
1572*fae548d3Szrj get_sized_symbol(Symbol*) const;
1573*fae548d3Szrj
1574*fae548d3Szrj template<int size>
1575*fae548d3Szrj const Sized_symbol<size>*
1576*fae548d3Szrj get_sized_symbol(const Symbol*) const;
1577*fae548d3Szrj
1578*fae548d3Szrj // Return the count of undefined symbols seen.
1579*fae548d3Szrj size_t
saw_undefined()1580*fae548d3Szrj saw_undefined() const
1581*fae548d3Szrj { return this->saw_undefined_; }
1582*fae548d3Szrj
1583*fae548d3Szrj void
set_has_gnu_output()1584*fae548d3Szrj set_has_gnu_output()
1585*fae548d3Szrj { this->has_gnu_output_ = true; }
1586*fae548d3Szrj
1587*fae548d3Szrj // Allocate the common symbols
1588*fae548d3Szrj void
1589*fae548d3Szrj allocate_commons(Layout*, Mapfile*);
1590*fae548d3Szrj
1591*fae548d3Szrj // Add a warning for symbol NAME in object OBJ. WARNING is the text
1592*fae548d3Szrj // of the warning.
1593*fae548d3Szrj void
add_warning(const char * name,Object * obj,const std::string & warning)1594*fae548d3Szrj add_warning(const char* name, Object* obj, const std::string& warning)
1595*fae548d3Szrj { this->warnings_.add_warning(this, name, obj, warning); }
1596*fae548d3Szrj
1597*fae548d3Szrj // Canonicalize a symbol name for use in the hash table.
1598*fae548d3Szrj const char*
canonicalize_name(const char * name)1599*fae548d3Szrj canonicalize_name(const char* name)
1600*fae548d3Szrj { return this->namepool_.add(name, true, NULL); }
1601*fae548d3Szrj
1602*fae548d3Szrj // Possibly issue a warning for a reference to SYM at LOCATION which
1603*fae548d3Szrj // is in OBJ.
1604*fae548d3Szrj template<int size, bool big_endian>
1605*fae548d3Szrj void
issue_warning(const Symbol * sym,const Relocate_info<size,big_endian> * relinfo,size_t relnum,off_t reloffset)1606*fae548d3Szrj issue_warning(const Symbol* sym,
1607*fae548d3Szrj const Relocate_info<size, big_endian>* relinfo,
1608*fae548d3Szrj size_t relnum, off_t reloffset) const
1609*fae548d3Szrj { this->warnings_.issue_warning(sym, relinfo, relnum, reloffset); }
1610*fae548d3Szrj
1611*fae548d3Szrj // Check candidate_odr_violations_ to find symbols with the same name
1612*fae548d3Szrj // but apparently different definitions (different source-file/line-no).
1613*fae548d3Szrj void
1614*fae548d3Szrj detect_odr_violations(const Task*, const char* output_file_name) const;
1615*fae548d3Szrj
1616*fae548d3Szrj // Add any undefined symbols named on the command line to the symbol
1617*fae548d3Szrj // table.
1618*fae548d3Szrj void
1619*fae548d3Szrj add_undefined_symbols_from_command_line(Layout*);
1620*fae548d3Szrj
1621*fae548d3Szrj // SYM is defined using a COPY reloc. Return the dynamic object
1622*fae548d3Szrj // where the original definition was found.
1623*fae548d3Szrj Dynobj*
1624*fae548d3Szrj get_copy_source(const Symbol* sym) const;
1625*fae548d3Szrj
1626*fae548d3Szrj // Set the dynamic symbol indexes. INDEX is the index of the first
1627*fae548d3Szrj // global dynamic symbol. Return the count of forced-local symbols in
1628*fae548d3Szrj // *PFORCED_LOCAL_COUNT. Pointers to the symbols are stored into
1629*fae548d3Szrj // the vector. The names are stored into the Stringpool. This
1630*fae548d3Szrj // returns an updated dynamic symbol index.
1631*fae548d3Szrj unsigned int
1632*fae548d3Szrj set_dynsym_indexes(unsigned int index, unsigned int* pforced_local_count,
1633*fae548d3Szrj std::vector<Symbol*>*, Stringpool*, Versions*);
1634*fae548d3Szrj
1635*fae548d3Szrj // Finalize the symbol table after we have set the final addresses
1636*fae548d3Szrj // of all the input sections. This sets the final symbol indexes,
1637*fae548d3Szrj // values and adds the names to *POOL. *PLOCAL_SYMCOUNT is the
1638*fae548d3Szrj // index of the first global symbol. OFF is the file offset of the
1639*fae548d3Szrj // global symbol table, DYNOFF is the offset of the globals in the
1640*fae548d3Szrj // dynamic symbol table, DYN_GLOBAL_INDEX is the index of the first
1641*fae548d3Szrj // global dynamic symbol, and DYNCOUNT is the number of global
1642*fae548d3Szrj // dynamic symbols. This records the parameters, and returns the
1643*fae548d3Szrj // new file offset. It updates *PLOCAL_SYMCOUNT if it created any
1644*fae548d3Szrj // local symbols.
1645*fae548d3Szrj off_t
1646*fae548d3Szrj finalize(off_t off, off_t dynoff, size_t dyn_global_index, size_t dyncount,
1647*fae548d3Szrj Stringpool* pool, unsigned int* plocal_symcount);
1648*fae548d3Szrj
1649*fae548d3Szrj // Set the final file offset of the symbol table.
1650*fae548d3Szrj void
set_file_offset(off_t off)1651*fae548d3Szrj set_file_offset(off_t off)
1652*fae548d3Szrj { this->offset_ = off; }
1653*fae548d3Szrj
1654*fae548d3Szrj // Status code of Symbol_table::compute_final_value.
1655*fae548d3Szrj enum Compute_final_value_status
1656*fae548d3Szrj {
1657*fae548d3Szrj // No error.
1658*fae548d3Szrj CFVS_OK,
1659*fae548d3Szrj // Unsupported symbol section.
1660*fae548d3Szrj CFVS_UNSUPPORTED_SYMBOL_SECTION,
1661*fae548d3Szrj // No output section.
1662*fae548d3Szrj CFVS_NO_OUTPUT_SECTION
1663*fae548d3Szrj };
1664*fae548d3Szrj
1665*fae548d3Szrj // Compute the final value of SYM and store status in location PSTATUS.
1666*fae548d3Szrj // During relaxation, this may be called multiple times for a symbol to
1667*fae548d3Szrj // compute its would-be final value in each relaxation pass.
1668*fae548d3Szrj
1669*fae548d3Szrj template<int size>
1670*fae548d3Szrj typename Sized_symbol<size>::Value_type
1671*fae548d3Szrj compute_final_value(const Sized_symbol<size>* sym,
1672*fae548d3Szrj Compute_final_value_status* pstatus) const;
1673*fae548d3Szrj
1674*fae548d3Szrj // Return the index of the first global symbol.
1675*fae548d3Szrj unsigned int
first_global_index()1676*fae548d3Szrj first_global_index() const
1677*fae548d3Szrj { return this->first_global_index_; }
1678*fae548d3Szrj
1679*fae548d3Szrj // Return the total number of symbols in the symbol table.
1680*fae548d3Szrj unsigned int
output_count()1681*fae548d3Szrj output_count() const
1682*fae548d3Szrj { return this->output_count_; }
1683*fae548d3Szrj
1684*fae548d3Szrj // Write out the global symbols.
1685*fae548d3Szrj void
1686*fae548d3Szrj write_globals(const Stringpool*, const Stringpool*,
1687*fae548d3Szrj Output_symtab_xindex*, Output_symtab_xindex*,
1688*fae548d3Szrj Output_file*) const;
1689*fae548d3Szrj
1690*fae548d3Szrj // Write out a section symbol. Return the updated offset.
1691*fae548d3Szrj void
1692*fae548d3Szrj write_section_symbol(const Output_section*, Output_symtab_xindex*,
1693*fae548d3Szrj Output_file*, off_t) const;
1694*fae548d3Szrj
1695*fae548d3Szrj // Loop over all symbols, applying the function F to each.
1696*fae548d3Szrj template<int size, typename F>
1697*fae548d3Szrj void
for_all_symbols(F f)1698*fae548d3Szrj for_all_symbols(F f) const
1699*fae548d3Szrj {
1700*fae548d3Szrj for (Symbol_table_type::const_iterator p = this->table_.begin();
1701*fae548d3Szrj p != this->table_.end();
1702*fae548d3Szrj ++p)
1703*fae548d3Szrj {
1704*fae548d3Szrj Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
1705*fae548d3Szrj f(sym);
1706*fae548d3Szrj }
1707*fae548d3Szrj }
1708*fae548d3Szrj
1709*fae548d3Szrj // Dump statistical information to stderr.
1710*fae548d3Szrj void
1711*fae548d3Szrj print_stats() const;
1712*fae548d3Szrj
1713*fae548d3Szrj // Return the version script information.
1714*fae548d3Szrj const Version_script_info&
version_script()1715*fae548d3Szrj version_script() const
1716*fae548d3Szrj { return version_script_; }
1717*fae548d3Szrj
1718*fae548d3Szrj // Completely override existing symbol.
1719*fae548d3Szrj template<int size>
1720*fae548d3Szrj void
clone(Sized_symbol<size> * to,const Sized_symbol<size> * from)1721*fae548d3Szrj clone(Sized_symbol<size>* to, const Sized_symbol<size>* from)
1722*fae548d3Szrj {
1723*fae548d3Szrj if (to->clone(from))
1724*fae548d3Szrj this->force_local(to);
1725*fae548d3Szrj }
1726*fae548d3Szrj
1727*fae548d3Szrj private:
1728*fae548d3Szrj Symbol_table(const Symbol_table&);
1729*fae548d3Szrj Symbol_table& operator=(const Symbol_table&);
1730*fae548d3Szrj
1731*fae548d3Szrj // The type of the list of common symbols.
1732*fae548d3Szrj typedef std::vector<Symbol*> Commons_type;
1733*fae548d3Szrj
1734*fae548d3Szrj // The type of the symbol hash table.
1735*fae548d3Szrj
1736*fae548d3Szrj typedef std::pair<Stringpool::Key, Stringpool::Key> Symbol_table_key;
1737*fae548d3Szrj
1738*fae548d3Szrj // The hash function. The key values are Stringpool keys.
1739*fae548d3Szrj struct Symbol_table_hash
1740*fae548d3Szrj {
1741*fae548d3Szrj inline size_t
operatorSymbol_table_hash1742*fae548d3Szrj operator()(const Symbol_table_key& key) const
1743*fae548d3Szrj {
1744*fae548d3Szrj return key.first ^ key.second;
1745*fae548d3Szrj }
1746*fae548d3Szrj };
1747*fae548d3Szrj
1748*fae548d3Szrj struct Symbol_table_eq
1749*fae548d3Szrj {
1750*fae548d3Szrj bool
1751*fae548d3Szrj operator()(const Symbol_table_key&, const Symbol_table_key&) const;
1752*fae548d3Szrj };
1753*fae548d3Szrj
1754*fae548d3Szrj typedef Unordered_map<Symbol_table_key, Symbol*, Symbol_table_hash,
1755*fae548d3Szrj Symbol_table_eq> Symbol_table_type;
1756*fae548d3Szrj
1757*fae548d3Szrj typedef Unordered_map<const char*,
1758*fae548d3Szrj Unordered_set<Symbol_location, Symbol_location_hash> >
1759*fae548d3Szrj Odr_map;
1760*fae548d3Szrj
1761*fae548d3Szrj // Make FROM a forwarder symbol to TO.
1762*fae548d3Szrj void
1763*fae548d3Szrj make_forwarder(Symbol* from, Symbol* to);
1764*fae548d3Szrj
1765*fae548d3Szrj // Add a symbol.
1766*fae548d3Szrj template<int size, bool big_endian>
1767*fae548d3Szrj Sized_symbol<size>*
1768*fae548d3Szrj add_from_object(Object*, const char* name, Stringpool::Key name_key,
1769*fae548d3Szrj const char* version, Stringpool::Key version_key,
1770*fae548d3Szrj bool def, const elfcpp::Sym<size, big_endian>& sym,
1771*fae548d3Szrj unsigned int st_shndx, bool is_ordinary,
1772*fae548d3Szrj unsigned int orig_st_shndx);
1773*fae548d3Szrj
1774*fae548d3Szrj // Define a default symbol.
1775*fae548d3Szrj template<int size, bool big_endian>
1776*fae548d3Szrj void
1777*fae548d3Szrj define_default_version(Sized_symbol<size>*, bool,
1778*fae548d3Szrj Symbol_table_type::iterator);
1779*fae548d3Szrj
1780*fae548d3Szrj // Resolve symbols.
1781*fae548d3Szrj template<int size, bool big_endian>
1782*fae548d3Szrj void
1783*fae548d3Szrj resolve(Sized_symbol<size>* to,
1784*fae548d3Szrj const elfcpp::Sym<size, big_endian>& sym,
1785*fae548d3Szrj unsigned int st_shndx, bool is_ordinary,
1786*fae548d3Szrj unsigned int orig_st_shndx,
1787*fae548d3Szrj Object*, const char* version,
1788*fae548d3Szrj bool is_default_version);
1789*fae548d3Szrj
1790*fae548d3Szrj template<int size, bool big_endian>
1791*fae548d3Szrj void
1792*fae548d3Szrj resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from);
1793*fae548d3Szrj
1794*fae548d3Szrj // Record that a symbol is forced to be local by a version script or
1795*fae548d3Szrj // by visibility.
1796*fae548d3Szrj void
1797*fae548d3Szrj force_local(Symbol*);
1798*fae548d3Szrj
1799*fae548d3Szrj // Adjust NAME and *NAME_KEY for wrapping.
1800*fae548d3Szrj const char*
1801*fae548d3Szrj wrap_symbol(const char* name, Stringpool::Key* name_key);
1802*fae548d3Szrj
1803*fae548d3Szrj // Whether we should override a symbol, based on flags in
1804*fae548d3Szrj // resolve.cc.
1805*fae548d3Szrj static bool
1806*fae548d3Szrj should_override(const Symbol*, unsigned int, elfcpp::STT, Defined,
1807*fae548d3Szrj Object*, bool*, bool*, bool);
1808*fae548d3Szrj
1809*fae548d3Szrj // Report a problem in symbol resolution.
1810*fae548d3Szrj static void
1811*fae548d3Szrj report_resolve_problem(bool is_error, const char* msg, const Symbol* to,
1812*fae548d3Szrj Defined, Object* object);
1813*fae548d3Szrj
1814*fae548d3Szrj // Override a symbol.
1815*fae548d3Szrj template<int size, bool big_endian>
1816*fae548d3Szrj void
1817*fae548d3Szrj override(Sized_symbol<size>* tosym,
1818*fae548d3Szrj const elfcpp::Sym<size, big_endian>& fromsym,
1819*fae548d3Szrj unsigned int st_shndx, bool is_ordinary,
1820*fae548d3Szrj Object* object, const char* version);
1821*fae548d3Szrj
1822*fae548d3Szrj // Whether we should override a symbol with a special symbol which
1823*fae548d3Szrj // is automatically defined by the linker.
1824*fae548d3Szrj static bool
1825*fae548d3Szrj should_override_with_special(const Symbol*, elfcpp::STT, Defined);
1826*fae548d3Szrj
1827*fae548d3Szrj // Override a symbol with a special symbol.
1828*fae548d3Szrj template<int size>
1829*fae548d3Szrj void
1830*fae548d3Szrj override_with_special(Sized_symbol<size>* tosym,
1831*fae548d3Szrj const Sized_symbol<size>* fromsym);
1832*fae548d3Szrj
1833*fae548d3Szrj // Record all weak alias sets for a dynamic object.
1834*fae548d3Szrj template<int size>
1835*fae548d3Szrj void
1836*fae548d3Szrj record_weak_aliases(std::vector<Sized_symbol<size>*>*);
1837*fae548d3Szrj
1838*fae548d3Szrj // Define a special symbol.
1839*fae548d3Szrj template<int size, bool big_endian>
1840*fae548d3Szrj Sized_symbol<size>*
1841*fae548d3Szrj define_special_symbol(const char** pname, const char** pversion,
1842*fae548d3Szrj bool only_if_ref, elfcpp::STV visibility,
1843*fae548d3Szrj Sized_symbol<size>** poldsym,
1844*fae548d3Szrj bool* resolve_oldsym, bool is_forced_local);
1845*fae548d3Szrj
1846*fae548d3Szrj // Define a symbol in an Output_data, sized version.
1847*fae548d3Szrj template<int size>
1848*fae548d3Szrj Sized_symbol<size>*
1849*fae548d3Szrj do_define_in_output_data(const char* name, const char* version, Defined,
1850*fae548d3Szrj Output_data*,
1851*fae548d3Szrj typename elfcpp::Elf_types<size>::Elf_Addr value,
1852*fae548d3Szrj typename elfcpp::Elf_types<size>::Elf_WXword ssize,
1853*fae548d3Szrj elfcpp::STT type, elfcpp::STB binding,
1854*fae548d3Szrj elfcpp::STV visibility, unsigned char nonvis,
1855*fae548d3Szrj bool offset_is_from_end, bool only_if_ref);
1856*fae548d3Szrj
1857*fae548d3Szrj // Define a symbol in an Output_segment, sized version.
1858*fae548d3Szrj template<int size>
1859*fae548d3Szrj Sized_symbol<size>*
1860*fae548d3Szrj do_define_in_output_segment(
1861*fae548d3Szrj const char* name, const char* version, Defined, Output_segment* os,
1862*fae548d3Szrj typename elfcpp::Elf_types<size>::Elf_Addr value,
1863*fae548d3Szrj typename elfcpp::Elf_types<size>::Elf_WXword ssize,
1864*fae548d3Szrj elfcpp::STT type, elfcpp::STB binding,
1865*fae548d3Szrj elfcpp::STV visibility, unsigned char nonvis,
1866*fae548d3Szrj Symbol::Segment_offset_base offset_base, bool only_if_ref);
1867*fae548d3Szrj
1868*fae548d3Szrj // Define a symbol as a constant, sized version.
1869*fae548d3Szrj template<int size>
1870*fae548d3Szrj Sized_symbol<size>*
1871*fae548d3Szrj do_define_as_constant(
1872*fae548d3Szrj const char* name, const char* version, Defined,
1873*fae548d3Szrj typename elfcpp::Elf_types<size>::Elf_Addr value,
1874*fae548d3Szrj typename elfcpp::Elf_types<size>::Elf_WXword ssize,
1875*fae548d3Szrj elfcpp::STT type, elfcpp::STB binding,
1876*fae548d3Szrj elfcpp::STV visibility, unsigned char nonvis,
1877*fae548d3Szrj bool only_if_ref, bool force_override);
1878*fae548d3Szrj
1879*fae548d3Szrj // Add any undefined symbols named on the command line to the symbol
1880*fae548d3Szrj // table, sized version.
1881*fae548d3Szrj template<int size>
1882*fae548d3Szrj void
1883*fae548d3Szrj do_add_undefined_symbols_from_command_line(Layout*);
1884*fae548d3Szrj
1885*fae548d3Szrj // Add one undefined symbol.
1886*fae548d3Szrj template<int size>
1887*fae548d3Szrj void
1888*fae548d3Szrj add_undefined_symbol_from_command_line(const char* name);
1889*fae548d3Szrj
1890*fae548d3Szrj // Types of common symbols.
1891*fae548d3Szrj
1892*fae548d3Szrj enum Commons_section_type
1893*fae548d3Szrj {
1894*fae548d3Szrj COMMONS_NORMAL,
1895*fae548d3Szrj COMMONS_TLS,
1896*fae548d3Szrj COMMONS_SMALL,
1897*fae548d3Szrj COMMONS_LARGE
1898*fae548d3Szrj };
1899*fae548d3Szrj
1900*fae548d3Szrj // Allocate the common symbols, sized version.
1901*fae548d3Szrj template<int size>
1902*fae548d3Szrj void
1903*fae548d3Szrj do_allocate_commons(Layout*, Mapfile*, Sort_commons_order);
1904*fae548d3Szrj
1905*fae548d3Szrj // Allocate the common symbols from one list.
1906*fae548d3Szrj template<int size>
1907*fae548d3Szrj void
1908*fae548d3Szrj do_allocate_commons_list(Layout*, Commons_section_type, Commons_type*,
1909*fae548d3Szrj Mapfile*, Sort_commons_order);
1910*fae548d3Szrj
1911*fae548d3Szrj // Returns all of the lines attached to LOC, not just the one the
1912*fae548d3Szrj // instruction actually came from. This helps the ODR checker avoid
1913*fae548d3Szrj // false positives.
1914*fae548d3Szrj static std::vector<std::string>
1915*fae548d3Szrj linenos_from_loc(const Task* task, const Symbol_location& loc);
1916*fae548d3Szrj
1917*fae548d3Szrj // Implement detect_odr_violations.
1918*fae548d3Szrj template<int size, bool big_endian>
1919*fae548d3Szrj void
1920*fae548d3Szrj sized_detect_odr_violations() const;
1921*fae548d3Szrj
1922*fae548d3Szrj // Finalize symbols specialized for size.
1923*fae548d3Szrj template<int size>
1924*fae548d3Szrj off_t
1925*fae548d3Szrj sized_finalize(off_t, Stringpool*, unsigned int*);
1926*fae548d3Szrj
1927*fae548d3Szrj // Finalize a symbol. Return whether it should be added to the
1928*fae548d3Szrj // symbol table.
1929*fae548d3Szrj template<int size>
1930*fae548d3Szrj bool
1931*fae548d3Szrj sized_finalize_symbol(Symbol*);
1932*fae548d3Szrj
1933*fae548d3Szrj // Add a symbol the final symtab by setting its index.
1934*fae548d3Szrj template<int size>
1935*fae548d3Szrj void
1936*fae548d3Szrj add_to_final_symtab(Symbol*, Stringpool*, unsigned int* pindex, off_t* poff);
1937*fae548d3Szrj
1938*fae548d3Szrj // Write globals specialized for size and endianness.
1939*fae548d3Szrj template<int size, bool big_endian>
1940*fae548d3Szrj void
1941*fae548d3Szrj sized_write_globals(const Stringpool*, const Stringpool*,
1942*fae548d3Szrj Output_symtab_xindex*, Output_symtab_xindex*,
1943*fae548d3Szrj Output_file*) const;
1944*fae548d3Szrj
1945*fae548d3Szrj // Write out a symbol to P.
1946*fae548d3Szrj template<int size, bool big_endian>
1947*fae548d3Szrj void
1948*fae548d3Szrj sized_write_symbol(Sized_symbol<size>*,
1949*fae548d3Szrj typename elfcpp::Elf_types<size>::Elf_Addr value,
1950*fae548d3Szrj unsigned int shndx, elfcpp::STB,
1951*fae548d3Szrj const Stringpool*, unsigned char* p) const;
1952*fae548d3Szrj
1953*fae548d3Szrj // Possibly warn about an undefined symbol from a dynamic object.
1954*fae548d3Szrj void
1955*fae548d3Szrj warn_about_undefined_dynobj_symbol(Symbol*) const;
1956*fae548d3Szrj
1957*fae548d3Szrj // Write out a section symbol, specialized for size and endianness.
1958*fae548d3Szrj template<int size, bool big_endian>
1959*fae548d3Szrj void
1960*fae548d3Szrj sized_write_section_symbol(const Output_section*, Output_symtab_xindex*,
1961*fae548d3Szrj Output_file*, off_t) const;
1962*fae548d3Szrj
1963*fae548d3Szrj // The type of the list of symbols which have been forced local.
1964*fae548d3Szrj typedef std::vector<Symbol*> Forced_locals;
1965*fae548d3Szrj
1966*fae548d3Szrj // A map from symbols with COPY relocs to the dynamic objects where
1967*fae548d3Szrj // they are defined.
1968*fae548d3Szrj typedef Unordered_map<const Symbol*, Dynobj*> Copied_symbol_dynobjs;
1969*fae548d3Szrj
1970*fae548d3Szrj // We increment this every time we see a new undefined symbol, for
1971*fae548d3Szrj // use in archive groups.
1972*fae548d3Szrj size_t saw_undefined_;
1973*fae548d3Szrj // The index of the first global symbol in the output file.
1974*fae548d3Szrj unsigned int first_global_index_;
1975*fae548d3Szrj // The file offset within the output symtab section where we should
1976*fae548d3Szrj // write the table.
1977*fae548d3Szrj off_t offset_;
1978*fae548d3Szrj // The number of global symbols we want to write out.
1979*fae548d3Szrj unsigned int output_count_;
1980*fae548d3Szrj // The file offset of the global dynamic symbols, or 0 if none.
1981*fae548d3Szrj off_t dynamic_offset_;
1982*fae548d3Szrj // The index of the first global dynamic symbol (including
1983*fae548d3Szrj // forced-local symbols).
1984*fae548d3Szrj unsigned int first_dynamic_global_index_;
1985*fae548d3Szrj // The number of global dynamic symbols (including forced-local symbols),
1986*fae548d3Szrj // or 0 if none.
1987*fae548d3Szrj unsigned int dynamic_count_;
1988*fae548d3Szrj // Set if a STT_GNU_IFUNC or STB_GNU_UNIQUE symbol will be output.
1989*fae548d3Szrj bool has_gnu_output_;
1990*fae548d3Szrj // The symbol hash table.
1991*fae548d3Szrj Symbol_table_type table_;
1992*fae548d3Szrj // A pool of symbol names. This is used for all global symbols.
1993*fae548d3Szrj // Entries in the hash table point into this pool.
1994*fae548d3Szrj Stringpool namepool_;
1995*fae548d3Szrj // Forwarding symbols.
1996*fae548d3Szrj Unordered_map<const Symbol*, Symbol*> forwarders_;
1997*fae548d3Szrj // Weak aliases. A symbol in this list points to the next alias.
1998*fae548d3Szrj // The aliases point to each other in a circular list.
1999*fae548d3Szrj Unordered_map<Symbol*, Symbol*> weak_aliases_;
2000*fae548d3Szrj // We don't expect there to be very many common symbols, so we keep
2001*fae548d3Szrj // a list of them. When we find a common symbol we add it to this
2002*fae548d3Szrj // list. It is possible that by the time we process the list the
2003*fae548d3Szrj // symbol is no longer a common symbol. It may also have become a
2004*fae548d3Szrj // forwarder.
2005*fae548d3Szrj Commons_type commons_;
2006*fae548d3Szrj // This is like the commons_ field, except that it holds TLS common
2007*fae548d3Szrj // symbols.
2008*fae548d3Szrj Commons_type tls_commons_;
2009*fae548d3Szrj // This is for small common symbols.
2010*fae548d3Szrj Commons_type small_commons_;
2011*fae548d3Szrj // This is for large common symbols.
2012*fae548d3Szrj Commons_type large_commons_;
2013*fae548d3Szrj // A list of symbols which have been forced to be local. We don't
2014*fae548d3Szrj // expect there to be very many of them, so we keep a list of them
2015*fae548d3Szrj // rather than walking the whole table to find them.
2016*fae548d3Szrj Forced_locals forced_locals_;
2017*fae548d3Szrj // Manage symbol warnings.
2018*fae548d3Szrj Warnings warnings_;
2019*fae548d3Szrj // Manage potential One Definition Rule (ODR) violations.
2020*fae548d3Szrj Odr_map candidate_odr_violations_;
2021*fae548d3Szrj
2022*fae548d3Szrj // When we emit a COPY reloc for a symbol, we define it in an
2023*fae548d3Szrj // Output_data. When it's time to emit version information for it,
2024*fae548d3Szrj // we need to know the dynamic object in which we found the original
2025*fae548d3Szrj // definition. This maps symbols with COPY relocs to the dynamic
2026*fae548d3Szrj // object where they were defined.
2027*fae548d3Szrj Copied_symbol_dynobjs copied_symbol_dynobjs_;
2028*fae548d3Szrj // Information parsed from the version script, if any.
2029*fae548d3Szrj const Version_script_info& version_script_;
2030*fae548d3Szrj Garbage_collection* gc_;
2031*fae548d3Szrj Icf* icf_;
2032*fae548d3Szrj // Target-specific symbols, if any.
2033*fae548d3Szrj std::vector<Symbol*> target_symbols_;
2034*fae548d3Szrj };
2035*fae548d3Szrj
2036*fae548d3Szrj // We inline get_sized_symbol for efficiency.
2037*fae548d3Szrj
2038*fae548d3Szrj template<int size>
2039*fae548d3Szrj Sized_symbol<size>*
get_sized_symbol(Symbol * sym)2040*fae548d3Szrj Symbol_table::get_sized_symbol(Symbol* sym) const
2041*fae548d3Szrj {
2042*fae548d3Szrj gold_assert(size == parameters->target().get_size());
2043*fae548d3Szrj return static_cast<Sized_symbol<size>*>(sym);
2044*fae548d3Szrj }
2045*fae548d3Szrj
2046*fae548d3Szrj template<int size>
2047*fae548d3Szrj const Sized_symbol<size>*
get_sized_symbol(const Symbol * sym)2048*fae548d3Szrj Symbol_table::get_sized_symbol(const Symbol* sym) const
2049*fae548d3Szrj {
2050*fae548d3Szrj gold_assert(size == parameters->target().get_size());
2051*fae548d3Szrj return static_cast<const Sized_symbol<size>*>(sym);
2052*fae548d3Szrj }
2053*fae548d3Szrj
2054*fae548d3Szrj } // End namespace gold.
2055*fae548d3Szrj
2056*fae548d3Szrj #endif // !defined(GOLD_SYMTAB_H)
2057