1*a9fa9459Szrj // symtab.cc -- the gold symbol table
2*a9fa9459Szrj 
3*a9fa9459Szrj // Copyright (C) 2006-2016 Free Software Foundation, Inc.
4*a9fa9459Szrj // Written by Ian Lance Taylor <iant@google.com>.
5*a9fa9459Szrj 
6*a9fa9459Szrj // This file is part of gold.
7*a9fa9459Szrj 
8*a9fa9459Szrj // This program is free software; you can redistribute it and/or modify
9*a9fa9459Szrj // it under the terms of the GNU General Public License as published by
10*a9fa9459Szrj // the Free Software Foundation; either version 3 of the License, or
11*a9fa9459Szrj // (at your option) any later version.
12*a9fa9459Szrj 
13*a9fa9459Szrj // This program is distributed in the hope that it will be useful,
14*a9fa9459Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
15*a9fa9459Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*a9fa9459Szrj // GNU General Public License for more details.
17*a9fa9459Szrj 
18*a9fa9459Szrj // You should have received a copy of the GNU General Public License
19*a9fa9459Szrj // along with this program; if not, write to the Free Software
20*a9fa9459Szrj // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21*a9fa9459Szrj // MA 02110-1301, USA.
22*a9fa9459Szrj 
23*a9fa9459Szrj #include "gold.h"
24*a9fa9459Szrj 
25*a9fa9459Szrj #include <cstring>
26*a9fa9459Szrj #include <stdint.h>
27*a9fa9459Szrj #include <algorithm>
28*a9fa9459Szrj #include <set>
29*a9fa9459Szrj #include <string>
30*a9fa9459Szrj #include <utility>
31*a9fa9459Szrj #include "demangle.h"
32*a9fa9459Szrj 
33*a9fa9459Szrj #include "gc.h"
34*a9fa9459Szrj #include "object.h"
35*a9fa9459Szrj #include "dwarf_reader.h"
36*a9fa9459Szrj #include "dynobj.h"
37*a9fa9459Szrj #include "output.h"
38*a9fa9459Szrj #include "target.h"
39*a9fa9459Szrj #include "workqueue.h"
40*a9fa9459Szrj #include "symtab.h"
41*a9fa9459Szrj #include "script.h"
42*a9fa9459Szrj #include "plugin.h"
43*a9fa9459Szrj #include "incremental.h"
44*a9fa9459Szrj 
45*a9fa9459Szrj namespace gold
46*a9fa9459Szrj {
47*a9fa9459Szrj 
48*a9fa9459Szrj // Class Symbol.
49*a9fa9459Szrj 
50*a9fa9459Szrj // Initialize fields in Symbol.  This initializes everything except u_
51*a9fa9459Szrj // and source_.
52*a9fa9459Szrj 
53*a9fa9459Szrj void
init_fields(const char * name,const char * version,elfcpp::STT type,elfcpp::STB binding,elfcpp::STV visibility,unsigned char nonvis)54*a9fa9459Szrj Symbol::init_fields(const char* name, const char* version,
55*a9fa9459Szrj 		    elfcpp::STT type, elfcpp::STB binding,
56*a9fa9459Szrj 		    elfcpp::STV visibility, unsigned char nonvis)
57*a9fa9459Szrj {
58*a9fa9459Szrj   this->name_ = name;
59*a9fa9459Szrj   this->version_ = version;
60*a9fa9459Szrj   this->symtab_index_ = 0;
61*a9fa9459Szrj   this->dynsym_index_ = 0;
62*a9fa9459Szrj   this->got_offsets_.init();
63*a9fa9459Szrj   this->plt_offset_ = -1U;
64*a9fa9459Szrj   this->type_ = type;
65*a9fa9459Szrj   this->binding_ = binding;
66*a9fa9459Szrj   this->visibility_ = visibility;
67*a9fa9459Szrj   this->nonvis_ = nonvis;
68*a9fa9459Szrj   this->is_def_ = false;
69*a9fa9459Szrj   this->is_forwarder_ = false;
70*a9fa9459Szrj   this->has_alias_ = false;
71*a9fa9459Szrj   this->needs_dynsym_entry_ = false;
72*a9fa9459Szrj   this->in_reg_ = false;
73*a9fa9459Szrj   this->in_dyn_ = false;
74*a9fa9459Szrj   this->has_warning_ = false;
75*a9fa9459Szrj   this->is_copied_from_dynobj_ = false;
76*a9fa9459Szrj   this->is_forced_local_ = false;
77*a9fa9459Szrj   this->is_ordinary_shndx_ = false;
78*a9fa9459Szrj   this->in_real_elf_ = false;
79*a9fa9459Szrj   this->is_defined_in_discarded_section_ = false;
80*a9fa9459Szrj   this->undef_binding_set_ = false;
81*a9fa9459Szrj   this->undef_binding_weak_ = false;
82*a9fa9459Szrj   this->is_predefined_ = false;
83*a9fa9459Szrj   this->is_protected_ = false;
84*a9fa9459Szrj }
85*a9fa9459Szrj 
86*a9fa9459Szrj // Return the demangled version of the symbol's name, but only
87*a9fa9459Szrj // if the --demangle flag was set.
88*a9fa9459Szrj 
89*a9fa9459Szrj static std::string
demangle(const char * name)90*a9fa9459Szrj demangle(const char* name)
91*a9fa9459Szrj {
92*a9fa9459Szrj   if (!parameters->options().do_demangle())
93*a9fa9459Szrj     return name;
94*a9fa9459Szrj 
95*a9fa9459Szrj   // cplus_demangle allocates memory for the result it returns,
96*a9fa9459Szrj   // and returns NULL if the name is already demangled.
97*a9fa9459Szrj   char* demangled_name = cplus_demangle(name, DMGL_ANSI | DMGL_PARAMS);
98*a9fa9459Szrj   if (demangled_name == NULL)
99*a9fa9459Szrj     return name;
100*a9fa9459Szrj 
101*a9fa9459Szrj   std::string retval(demangled_name);
102*a9fa9459Szrj   free(demangled_name);
103*a9fa9459Szrj   return retval;
104*a9fa9459Szrj }
105*a9fa9459Szrj 
106*a9fa9459Szrj std::string
demangled_name() const107*a9fa9459Szrj Symbol::demangled_name() const
108*a9fa9459Szrj {
109*a9fa9459Szrj   return demangle(this->name());
110*a9fa9459Szrj }
111*a9fa9459Szrj 
112*a9fa9459Szrj // Initialize the fields in the base class Symbol for SYM in OBJECT.
113*a9fa9459Szrj 
114*a9fa9459Szrj template<int size, bool big_endian>
115*a9fa9459Szrj void
init_base_object(const char * name,const char * version,Object * object,const elfcpp::Sym<size,big_endian> & sym,unsigned int st_shndx,bool is_ordinary)116*a9fa9459Szrj Symbol::init_base_object(const char* name, const char* version, Object* object,
117*a9fa9459Szrj 			 const elfcpp::Sym<size, big_endian>& sym,
118*a9fa9459Szrj 			 unsigned int st_shndx, bool is_ordinary)
119*a9fa9459Szrj {
120*a9fa9459Szrj   this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(),
121*a9fa9459Szrj 		    sym.get_st_visibility(), sym.get_st_nonvis());
122*a9fa9459Szrj   this->u_.from_object.object = object;
123*a9fa9459Szrj   this->u_.from_object.shndx = st_shndx;
124*a9fa9459Szrj   this->is_ordinary_shndx_ = is_ordinary;
125*a9fa9459Szrj   this->source_ = FROM_OBJECT;
126*a9fa9459Szrj   this->in_reg_ = !object->is_dynamic();
127*a9fa9459Szrj   this->in_dyn_ = object->is_dynamic();
128*a9fa9459Szrj   this->in_real_elf_ = object->pluginobj() == NULL;
129*a9fa9459Szrj }
130*a9fa9459Szrj 
131*a9fa9459Szrj // Initialize the fields in the base class Symbol for a symbol defined
132*a9fa9459Szrj // in an Output_data.
133*a9fa9459Szrj 
134*a9fa9459Szrj void
init_base_output_data(const char * name,const char * version,Output_data * od,elfcpp::STT type,elfcpp::STB binding,elfcpp::STV visibility,unsigned char nonvis,bool offset_is_from_end,bool is_predefined)135*a9fa9459Szrj Symbol::init_base_output_data(const char* name, const char* version,
136*a9fa9459Szrj 			      Output_data* od, elfcpp::STT type,
137*a9fa9459Szrj 			      elfcpp::STB binding, elfcpp::STV visibility,
138*a9fa9459Szrj 			      unsigned char nonvis, bool offset_is_from_end,
139*a9fa9459Szrj 			      bool is_predefined)
140*a9fa9459Szrj {
141*a9fa9459Szrj   this->init_fields(name, version, type, binding, visibility, nonvis);
142*a9fa9459Szrj   this->u_.in_output_data.output_data = od;
143*a9fa9459Szrj   this->u_.in_output_data.offset_is_from_end = offset_is_from_end;
144*a9fa9459Szrj   this->source_ = IN_OUTPUT_DATA;
145*a9fa9459Szrj   this->in_reg_ = true;
146*a9fa9459Szrj   this->in_real_elf_ = true;
147*a9fa9459Szrj   this->is_predefined_ = is_predefined;
148*a9fa9459Szrj }
149*a9fa9459Szrj 
150*a9fa9459Szrj // Initialize the fields in the base class Symbol for a symbol defined
151*a9fa9459Szrj // in an Output_segment.
152*a9fa9459Szrj 
153*a9fa9459Szrj void
init_base_output_segment(const char * name,const char * version,Output_segment * os,elfcpp::STT type,elfcpp::STB binding,elfcpp::STV visibility,unsigned char nonvis,Segment_offset_base offset_base,bool is_predefined)154*a9fa9459Szrj Symbol::init_base_output_segment(const char* name, const char* version,
155*a9fa9459Szrj 				 Output_segment* os, elfcpp::STT type,
156*a9fa9459Szrj 				 elfcpp::STB binding, elfcpp::STV visibility,
157*a9fa9459Szrj 				 unsigned char nonvis,
158*a9fa9459Szrj 				 Segment_offset_base offset_base,
159*a9fa9459Szrj 				 bool is_predefined)
160*a9fa9459Szrj {
161*a9fa9459Szrj   this->init_fields(name, version, type, binding, visibility, nonvis);
162*a9fa9459Szrj   this->u_.in_output_segment.output_segment = os;
163*a9fa9459Szrj   this->u_.in_output_segment.offset_base = offset_base;
164*a9fa9459Szrj   this->source_ = IN_OUTPUT_SEGMENT;
165*a9fa9459Szrj   this->in_reg_ = true;
166*a9fa9459Szrj   this->in_real_elf_ = true;
167*a9fa9459Szrj   this->is_predefined_ = is_predefined;
168*a9fa9459Szrj }
169*a9fa9459Szrj 
170*a9fa9459Szrj // Initialize the fields in the base class Symbol for a symbol defined
171*a9fa9459Szrj // as a constant.
172*a9fa9459Szrj 
173*a9fa9459Szrj void
init_base_constant(const char * name,const char * version,elfcpp::STT type,elfcpp::STB binding,elfcpp::STV visibility,unsigned char nonvis,bool is_predefined)174*a9fa9459Szrj Symbol::init_base_constant(const char* name, const char* version,
175*a9fa9459Szrj 			   elfcpp::STT type, elfcpp::STB binding,
176*a9fa9459Szrj 			   elfcpp::STV visibility, unsigned char nonvis,
177*a9fa9459Szrj 			   bool is_predefined)
178*a9fa9459Szrj {
179*a9fa9459Szrj   this->init_fields(name, version, type, binding, visibility, nonvis);
180*a9fa9459Szrj   this->source_ = IS_CONSTANT;
181*a9fa9459Szrj   this->in_reg_ = true;
182*a9fa9459Szrj   this->in_real_elf_ = true;
183*a9fa9459Szrj   this->is_predefined_ = is_predefined;
184*a9fa9459Szrj }
185*a9fa9459Szrj 
186*a9fa9459Szrj // Initialize the fields in the base class Symbol for an undefined
187*a9fa9459Szrj // symbol.
188*a9fa9459Szrj 
189*a9fa9459Szrj void
init_base_undefined(const char * name,const char * version,elfcpp::STT type,elfcpp::STB binding,elfcpp::STV visibility,unsigned char nonvis)190*a9fa9459Szrj Symbol::init_base_undefined(const char* name, const char* version,
191*a9fa9459Szrj 			    elfcpp::STT type, elfcpp::STB binding,
192*a9fa9459Szrj 			    elfcpp::STV visibility, unsigned char nonvis)
193*a9fa9459Szrj {
194*a9fa9459Szrj   this->init_fields(name, version, type, binding, visibility, nonvis);
195*a9fa9459Szrj   this->dynsym_index_ = -1U;
196*a9fa9459Szrj   this->source_ = IS_UNDEFINED;
197*a9fa9459Szrj   this->in_reg_ = true;
198*a9fa9459Szrj   this->in_real_elf_ = true;
199*a9fa9459Szrj }
200*a9fa9459Szrj 
201*a9fa9459Szrj // Allocate a common symbol in the base.
202*a9fa9459Szrj 
203*a9fa9459Szrj void
allocate_base_common(Output_data * od)204*a9fa9459Szrj Symbol::allocate_base_common(Output_data* od)
205*a9fa9459Szrj {
206*a9fa9459Szrj   gold_assert(this->is_common());
207*a9fa9459Szrj   this->source_ = IN_OUTPUT_DATA;
208*a9fa9459Szrj   this->u_.in_output_data.output_data = od;
209*a9fa9459Szrj   this->u_.in_output_data.offset_is_from_end = false;
210*a9fa9459Szrj }
211*a9fa9459Szrj 
212*a9fa9459Szrj // Initialize the fields in Sized_symbol for SYM in OBJECT.
213*a9fa9459Szrj 
214*a9fa9459Szrj template<int size>
215*a9fa9459Szrj template<bool big_endian>
216*a9fa9459Szrj void
init_object(const char * name,const char * version,Object * object,const elfcpp::Sym<size,big_endian> & sym,unsigned int st_shndx,bool is_ordinary)217*a9fa9459Szrj Sized_symbol<size>::init_object(const char* name, const char* version,
218*a9fa9459Szrj 				Object* object,
219*a9fa9459Szrj 				const elfcpp::Sym<size, big_endian>& sym,
220*a9fa9459Szrj 				unsigned int st_shndx, bool is_ordinary)
221*a9fa9459Szrj {
222*a9fa9459Szrj   this->init_base_object(name, version, object, sym, st_shndx, is_ordinary);
223*a9fa9459Szrj   this->value_ = sym.get_st_value();
224*a9fa9459Szrj   this->symsize_ = sym.get_st_size();
225*a9fa9459Szrj }
226*a9fa9459Szrj 
227*a9fa9459Szrj // Initialize the fields in Sized_symbol for a symbol defined in an
228*a9fa9459Szrj // Output_data.
229*a9fa9459Szrj 
230*a9fa9459Szrj template<int size>
231*a9fa9459Szrj void
init_output_data(const char * name,const char * version,Output_data * od,Value_type value,Size_type symsize,elfcpp::STT type,elfcpp::STB binding,elfcpp::STV visibility,unsigned char nonvis,bool offset_is_from_end,bool is_predefined)232*a9fa9459Szrj Sized_symbol<size>::init_output_data(const char* name, const char* version,
233*a9fa9459Szrj 				     Output_data* od, Value_type value,
234*a9fa9459Szrj 				     Size_type symsize, elfcpp::STT type,
235*a9fa9459Szrj 				     elfcpp::STB binding,
236*a9fa9459Szrj 				     elfcpp::STV visibility,
237*a9fa9459Szrj 				     unsigned char nonvis,
238*a9fa9459Szrj 				     bool offset_is_from_end,
239*a9fa9459Szrj 				     bool is_predefined)
240*a9fa9459Szrj {
241*a9fa9459Szrj   this->init_base_output_data(name, version, od, type, binding, visibility,
242*a9fa9459Szrj 			      nonvis, offset_is_from_end, is_predefined);
243*a9fa9459Szrj   this->value_ = value;
244*a9fa9459Szrj   this->symsize_ = symsize;
245*a9fa9459Szrj }
246*a9fa9459Szrj 
247*a9fa9459Szrj // Initialize the fields in Sized_symbol for a symbol defined in an
248*a9fa9459Szrj // Output_segment.
249*a9fa9459Szrj 
250*a9fa9459Szrj template<int size>
251*a9fa9459Szrj void
init_output_segment(const char * name,const char * version,Output_segment * os,Value_type value,Size_type symsize,elfcpp::STT type,elfcpp::STB binding,elfcpp::STV visibility,unsigned char nonvis,Segment_offset_base offset_base,bool is_predefined)252*a9fa9459Szrj Sized_symbol<size>::init_output_segment(const char* name, const char* version,
253*a9fa9459Szrj 					Output_segment* os, Value_type value,
254*a9fa9459Szrj 					Size_type symsize, elfcpp::STT type,
255*a9fa9459Szrj 					elfcpp::STB binding,
256*a9fa9459Szrj 					elfcpp::STV visibility,
257*a9fa9459Szrj 					unsigned char nonvis,
258*a9fa9459Szrj 					Segment_offset_base offset_base,
259*a9fa9459Szrj 					bool is_predefined)
260*a9fa9459Szrj {
261*a9fa9459Szrj   this->init_base_output_segment(name, version, os, type, binding, visibility,
262*a9fa9459Szrj 				 nonvis, offset_base, is_predefined);
263*a9fa9459Szrj   this->value_ = value;
264*a9fa9459Szrj   this->symsize_ = symsize;
265*a9fa9459Szrj }
266*a9fa9459Szrj 
267*a9fa9459Szrj // Initialize the fields in Sized_symbol for a symbol defined as a
268*a9fa9459Szrj // constant.
269*a9fa9459Szrj 
270*a9fa9459Szrj template<int size>
271*a9fa9459Szrj void
init_constant(const char * name,const char * version,Value_type value,Size_type symsize,elfcpp::STT type,elfcpp::STB binding,elfcpp::STV visibility,unsigned char nonvis,bool is_predefined)272*a9fa9459Szrj Sized_symbol<size>::init_constant(const char* name, const char* version,
273*a9fa9459Szrj 				  Value_type value, Size_type symsize,
274*a9fa9459Szrj 				  elfcpp::STT type, elfcpp::STB binding,
275*a9fa9459Szrj 				  elfcpp::STV visibility, unsigned char nonvis,
276*a9fa9459Szrj 				  bool is_predefined)
277*a9fa9459Szrj {
278*a9fa9459Szrj   this->init_base_constant(name, version, type, binding, visibility, nonvis,
279*a9fa9459Szrj 			   is_predefined);
280*a9fa9459Szrj   this->value_ = value;
281*a9fa9459Szrj   this->symsize_ = symsize;
282*a9fa9459Szrj }
283*a9fa9459Szrj 
284*a9fa9459Szrj // Initialize the fields in Sized_symbol for an undefined symbol.
285*a9fa9459Szrj 
286*a9fa9459Szrj template<int size>
287*a9fa9459Szrj void
init_undefined(const char * name,const char * version,Value_type value,elfcpp::STT type,elfcpp::STB binding,elfcpp::STV visibility,unsigned char nonvis)288*a9fa9459Szrj Sized_symbol<size>::init_undefined(const char* name, const char* version,
289*a9fa9459Szrj 				   Value_type value, elfcpp::STT type,
290*a9fa9459Szrj 				   elfcpp::STB binding, elfcpp::STV visibility,
291*a9fa9459Szrj 				   unsigned char nonvis)
292*a9fa9459Szrj {
293*a9fa9459Szrj   this->init_base_undefined(name, version, type, binding, visibility, nonvis);
294*a9fa9459Szrj   this->value_ = value;
295*a9fa9459Szrj   this->symsize_ = 0;
296*a9fa9459Szrj }
297*a9fa9459Szrj 
298*a9fa9459Szrj // Return an allocated string holding the symbol's name as
299*a9fa9459Szrj // name@version.  This is used for relocatable links.
300*a9fa9459Szrj 
301*a9fa9459Szrj std::string
versioned_name() const302*a9fa9459Szrj Symbol::versioned_name() const
303*a9fa9459Szrj {
304*a9fa9459Szrj   gold_assert(this->version_ != NULL);
305*a9fa9459Szrj   std::string ret = this->name_;
306*a9fa9459Szrj   ret.push_back('@');
307*a9fa9459Szrj   if (this->is_def_)
308*a9fa9459Szrj     ret.push_back('@');
309*a9fa9459Szrj   ret += this->version_;
310*a9fa9459Szrj   return ret;
311*a9fa9459Szrj }
312*a9fa9459Szrj 
313*a9fa9459Szrj // Return true if SHNDX represents a common symbol.
314*a9fa9459Szrj 
315*a9fa9459Szrj bool
is_common_shndx(unsigned int shndx)316*a9fa9459Szrj Symbol::is_common_shndx(unsigned int shndx)
317*a9fa9459Szrj {
318*a9fa9459Szrj   return (shndx == elfcpp::SHN_COMMON
319*a9fa9459Szrj 	  || shndx == parameters->target().small_common_shndx()
320*a9fa9459Szrj 	  || shndx == parameters->target().large_common_shndx());
321*a9fa9459Szrj }
322*a9fa9459Szrj 
323*a9fa9459Szrj // Allocate a common symbol.
324*a9fa9459Szrj 
325*a9fa9459Szrj template<int size>
326*a9fa9459Szrj void
allocate_common(Output_data * od,Value_type value)327*a9fa9459Szrj Sized_symbol<size>::allocate_common(Output_data* od, Value_type value)
328*a9fa9459Szrj {
329*a9fa9459Szrj   this->allocate_base_common(od);
330*a9fa9459Szrj   this->value_ = value;
331*a9fa9459Szrj }
332*a9fa9459Szrj 
333*a9fa9459Szrj // The ""'s around str ensure str is a string literal, so sizeof works.
334*a9fa9459Szrj #define strprefix(var, str)   (strncmp(var, str, sizeof("" str "") - 1) == 0)
335*a9fa9459Szrj 
336*a9fa9459Szrj // Return true if this symbol should be added to the dynamic symbol
337*a9fa9459Szrj // table.
338*a9fa9459Szrj 
339*a9fa9459Szrj bool
should_add_dynsym_entry(Symbol_table * symtab) const340*a9fa9459Szrj Symbol::should_add_dynsym_entry(Symbol_table* symtab) const
341*a9fa9459Szrj {
342*a9fa9459Szrj   // If the symbol is only present on plugin files, the plugin decided we
343*a9fa9459Szrj   // don't need it.
344*a9fa9459Szrj   if (!this->in_real_elf())
345*a9fa9459Szrj     return false;
346*a9fa9459Szrj 
347*a9fa9459Szrj   // If the symbol is used by a dynamic relocation, we need to add it.
348*a9fa9459Szrj   if (this->needs_dynsym_entry())
349*a9fa9459Szrj     return true;
350*a9fa9459Szrj 
351*a9fa9459Szrj   // If this symbol's section is not added, the symbol need not be added.
352*a9fa9459Szrj   // The section may have been GCed.  Note that export_dynamic is being
353*a9fa9459Szrj   // overridden here.  This should not be done for shared objects.
354*a9fa9459Szrj   if (parameters->options().gc_sections()
355*a9fa9459Szrj       && !parameters->options().shared()
356*a9fa9459Szrj       && this->source() == Symbol::FROM_OBJECT
357*a9fa9459Szrj       && !this->object()->is_dynamic())
358*a9fa9459Szrj     {
359*a9fa9459Szrj       Relobj* relobj = static_cast<Relobj*>(this->object());
360*a9fa9459Szrj       bool is_ordinary;
361*a9fa9459Szrj       unsigned int shndx = this->shndx(&is_ordinary);
362*a9fa9459Szrj       if (is_ordinary && shndx != elfcpp::SHN_UNDEF
363*a9fa9459Szrj           && !relobj->is_section_included(shndx)
364*a9fa9459Szrj           && !symtab->is_section_folded(relobj, shndx))
365*a9fa9459Szrj         return false;
366*a9fa9459Szrj     }
367*a9fa9459Szrj 
368*a9fa9459Szrj   // If the symbol was forced dynamic in a --dynamic-list file
369*a9fa9459Szrj   // or an --export-dynamic-symbol option, add it.
370*a9fa9459Szrj   if (!this->is_from_dynobj()
371*a9fa9459Szrj       && (parameters->options().in_dynamic_list(this->name())
372*a9fa9459Szrj 	  || parameters->options().is_export_dynamic_symbol(this->name())))
373*a9fa9459Szrj     {
374*a9fa9459Szrj       if (!this->is_forced_local())
375*a9fa9459Szrj         return true;
376*a9fa9459Szrj       gold_warning(_("Cannot export local symbol '%s'"),
377*a9fa9459Szrj 		   this->demangled_name().c_str());
378*a9fa9459Szrj       return false;
379*a9fa9459Szrj     }
380*a9fa9459Szrj 
381*a9fa9459Szrj   // If the symbol was forced local in a version script, do not add it.
382*a9fa9459Szrj   if (this->is_forced_local())
383*a9fa9459Szrj     return false;
384*a9fa9459Szrj 
385*a9fa9459Szrj   // If dynamic-list-data was specified, add any STT_OBJECT.
386*a9fa9459Szrj   if (parameters->options().dynamic_list_data()
387*a9fa9459Szrj       && !this->is_from_dynobj()
388*a9fa9459Szrj       && this->type() == elfcpp::STT_OBJECT)
389*a9fa9459Szrj     return true;
390*a9fa9459Szrj 
391*a9fa9459Szrj   // If --dynamic-list-cpp-new was specified, add any new/delete symbol.
392*a9fa9459Szrj   // If --dynamic-list-cpp-typeinfo was specified, add any typeinfo symbols.
393*a9fa9459Szrj   if ((parameters->options().dynamic_list_cpp_new()
394*a9fa9459Szrj        || parameters->options().dynamic_list_cpp_typeinfo())
395*a9fa9459Szrj       && !this->is_from_dynobj())
396*a9fa9459Szrj     {
397*a9fa9459Szrj       // TODO(csilvers): We could probably figure out if we're an operator
398*a9fa9459Szrj       //                 new/delete or typeinfo without the need to demangle.
399*a9fa9459Szrj       char* demangled_name = cplus_demangle(this->name(),
400*a9fa9459Szrj                                             DMGL_ANSI | DMGL_PARAMS);
401*a9fa9459Szrj       if (demangled_name == NULL)
402*a9fa9459Szrj         {
403*a9fa9459Szrj           // Not a C++ symbol, so it can't satisfy these flags
404*a9fa9459Szrj         }
405*a9fa9459Szrj       else if (parameters->options().dynamic_list_cpp_new()
406*a9fa9459Szrj                && (strprefix(demangled_name, "operator new")
407*a9fa9459Szrj                    || strprefix(demangled_name, "operator delete")))
408*a9fa9459Szrj         {
409*a9fa9459Szrj           free(demangled_name);
410*a9fa9459Szrj           return true;
411*a9fa9459Szrj         }
412*a9fa9459Szrj       else if (parameters->options().dynamic_list_cpp_typeinfo()
413*a9fa9459Szrj                && (strprefix(demangled_name, "typeinfo name for")
414*a9fa9459Szrj                    || strprefix(demangled_name, "typeinfo for")))
415*a9fa9459Szrj         {
416*a9fa9459Szrj           free(demangled_name);
417*a9fa9459Szrj           return true;
418*a9fa9459Szrj         }
419*a9fa9459Szrj       else
420*a9fa9459Szrj         free(demangled_name);
421*a9fa9459Szrj     }
422*a9fa9459Szrj 
423*a9fa9459Szrj   // If exporting all symbols or building a shared library,
424*a9fa9459Szrj   // or the symbol should be globally unique (GNU_UNIQUE),
425*a9fa9459Szrj   // and the symbol is defined in a regular object and is
426*a9fa9459Szrj   // externally visible, we need to add it.
427*a9fa9459Szrj   if ((parameters->options().export_dynamic()
428*a9fa9459Szrj        || parameters->options().shared()
429*a9fa9459Szrj        || (parameters->options().gnu_unique()
430*a9fa9459Szrj            && this->binding() == elfcpp::STB_GNU_UNIQUE))
431*a9fa9459Szrj       && !this->is_from_dynobj()
432*a9fa9459Szrj       && !this->is_undefined()
433*a9fa9459Szrj       && this->is_externally_visible())
434*a9fa9459Szrj     return true;
435*a9fa9459Szrj 
436*a9fa9459Szrj   return false;
437*a9fa9459Szrj }
438*a9fa9459Szrj 
439*a9fa9459Szrj // Return true if the final value of this symbol is known at link
440*a9fa9459Szrj // time.
441*a9fa9459Szrj 
442*a9fa9459Szrj bool
final_value_is_known() const443*a9fa9459Szrj Symbol::final_value_is_known() const
444*a9fa9459Szrj {
445*a9fa9459Szrj   // If we are not generating an executable, then no final values are
446*a9fa9459Szrj   // known, since they will change at runtime, with the exception of
447*a9fa9459Szrj   // TLS symbols in a position-independent executable.
448*a9fa9459Szrj   if ((parameters->options().output_is_position_independent()
449*a9fa9459Szrj        || parameters->options().relocatable())
450*a9fa9459Szrj       && !(this->type() == elfcpp::STT_TLS
451*a9fa9459Szrj            && parameters->options().pie()))
452*a9fa9459Szrj     return false;
453*a9fa9459Szrj 
454*a9fa9459Szrj   // If the symbol is not from an object file, and is not undefined,
455*a9fa9459Szrj   // then it is defined, and known.
456*a9fa9459Szrj   if (this->source_ != FROM_OBJECT)
457*a9fa9459Szrj     {
458*a9fa9459Szrj       if (this->source_ != IS_UNDEFINED)
459*a9fa9459Szrj 	return true;
460*a9fa9459Szrj     }
461*a9fa9459Szrj   else
462*a9fa9459Szrj     {
463*a9fa9459Szrj       // If the symbol is from a dynamic object, then the final value
464*a9fa9459Szrj       // is not known.
465*a9fa9459Szrj       if (this->object()->is_dynamic())
466*a9fa9459Szrj 	return false;
467*a9fa9459Szrj 
468*a9fa9459Szrj       // If the symbol is not undefined (it is defined or common),
469*a9fa9459Szrj       // then the final value is known.
470*a9fa9459Szrj       if (!this->is_undefined())
471*a9fa9459Szrj 	return true;
472*a9fa9459Szrj     }
473*a9fa9459Szrj 
474*a9fa9459Szrj   // If the symbol is undefined, then whether the final value is known
475*a9fa9459Szrj   // depends on whether we are doing a static link.  If we are doing a
476*a9fa9459Szrj   // dynamic link, then the final value could be filled in at runtime.
477*a9fa9459Szrj   // This could reasonably be the case for a weak undefined symbol.
478*a9fa9459Szrj   return parameters->doing_static_link();
479*a9fa9459Szrj }
480*a9fa9459Szrj 
481*a9fa9459Szrj // Return the output section where this symbol is defined.
482*a9fa9459Szrj 
483*a9fa9459Szrj Output_section*
output_section() const484*a9fa9459Szrj Symbol::output_section() const
485*a9fa9459Szrj {
486*a9fa9459Szrj   switch (this->source_)
487*a9fa9459Szrj     {
488*a9fa9459Szrj     case FROM_OBJECT:
489*a9fa9459Szrj       {
490*a9fa9459Szrj 	unsigned int shndx = this->u_.from_object.shndx;
491*a9fa9459Szrj 	if (shndx != elfcpp::SHN_UNDEF && this->is_ordinary_shndx_)
492*a9fa9459Szrj 	  {
493*a9fa9459Szrj 	    gold_assert(!this->u_.from_object.object->is_dynamic());
494*a9fa9459Szrj 	    gold_assert(this->u_.from_object.object->pluginobj() == NULL);
495*a9fa9459Szrj 	    Relobj* relobj = static_cast<Relobj*>(this->u_.from_object.object);
496*a9fa9459Szrj 	    return relobj->output_section(shndx);
497*a9fa9459Szrj 	  }
498*a9fa9459Szrj 	return NULL;
499*a9fa9459Szrj       }
500*a9fa9459Szrj 
501*a9fa9459Szrj     case IN_OUTPUT_DATA:
502*a9fa9459Szrj       return this->u_.in_output_data.output_data->output_section();
503*a9fa9459Szrj 
504*a9fa9459Szrj     case IN_OUTPUT_SEGMENT:
505*a9fa9459Szrj     case IS_CONSTANT:
506*a9fa9459Szrj     case IS_UNDEFINED:
507*a9fa9459Szrj       return NULL;
508*a9fa9459Szrj 
509*a9fa9459Szrj     default:
510*a9fa9459Szrj       gold_unreachable();
511*a9fa9459Szrj     }
512*a9fa9459Szrj }
513*a9fa9459Szrj 
514*a9fa9459Szrj // Set the symbol's output section.  This is used for symbols defined
515*a9fa9459Szrj // in scripts.  This should only be called after the symbol table has
516*a9fa9459Szrj // been finalized.
517*a9fa9459Szrj 
518*a9fa9459Szrj void
set_output_section(Output_section * os)519*a9fa9459Szrj Symbol::set_output_section(Output_section* os)
520*a9fa9459Szrj {
521*a9fa9459Szrj   switch (this->source_)
522*a9fa9459Szrj     {
523*a9fa9459Szrj     case FROM_OBJECT:
524*a9fa9459Szrj     case IN_OUTPUT_DATA:
525*a9fa9459Szrj       gold_assert(this->output_section() == os);
526*a9fa9459Szrj       break;
527*a9fa9459Szrj     case IS_CONSTANT:
528*a9fa9459Szrj       this->source_ = IN_OUTPUT_DATA;
529*a9fa9459Szrj       this->u_.in_output_data.output_data = os;
530*a9fa9459Szrj       this->u_.in_output_data.offset_is_from_end = false;
531*a9fa9459Szrj       break;
532*a9fa9459Szrj     case IN_OUTPUT_SEGMENT:
533*a9fa9459Szrj     case IS_UNDEFINED:
534*a9fa9459Szrj     default:
535*a9fa9459Szrj       gold_unreachable();
536*a9fa9459Szrj     }
537*a9fa9459Szrj }
538*a9fa9459Szrj 
539*a9fa9459Szrj // Set the symbol's output segment.  This is used for pre-defined
540*a9fa9459Szrj // symbols whose segments aren't known until after layout is done
541*a9fa9459Szrj // (e.g., __ehdr_start).
542*a9fa9459Szrj 
543*a9fa9459Szrj void
set_output_segment(Output_segment * os,Segment_offset_base base)544*a9fa9459Szrj Symbol::set_output_segment(Output_segment* os, Segment_offset_base base)
545*a9fa9459Szrj {
546*a9fa9459Szrj   gold_assert(this->is_predefined_);
547*a9fa9459Szrj   this->source_ = IN_OUTPUT_SEGMENT;
548*a9fa9459Szrj   this->u_.in_output_segment.output_segment = os;
549*a9fa9459Szrj   this->u_.in_output_segment.offset_base = base;
550*a9fa9459Szrj }
551*a9fa9459Szrj 
552*a9fa9459Szrj // Set the symbol to undefined.  This is used for pre-defined
553*a9fa9459Szrj // symbols whose segments aren't known until after layout is done
554*a9fa9459Szrj // (e.g., __ehdr_start).
555*a9fa9459Szrj 
556*a9fa9459Szrj void
set_undefined()557*a9fa9459Szrj Symbol::set_undefined()
558*a9fa9459Szrj {
559*a9fa9459Szrj   this->source_ = IS_UNDEFINED;
560*a9fa9459Szrj   this->is_predefined_ = false;
561*a9fa9459Szrj }
562*a9fa9459Szrj 
563*a9fa9459Szrj // Class Symbol_table.
564*a9fa9459Szrj 
Symbol_table(unsigned int count,const Version_script_info & version_script)565*a9fa9459Szrj Symbol_table::Symbol_table(unsigned int count,
566*a9fa9459Szrj                            const Version_script_info& version_script)
567*a9fa9459Szrj   : saw_undefined_(0), offset_(0), table_(count), namepool_(),
568*a9fa9459Szrj     forwarders_(), commons_(), tls_commons_(), small_commons_(),
569*a9fa9459Szrj     large_commons_(), forced_locals_(), warnings_(),
570*a9fa9459Szrj     version_script_(version_script), gc_(NULL), icf_(NULL),
571*a9fa9459Szrj     target_symbols_()
572*a9fa9459Szrj {
573*a9fa9459Szrj   namepool_.reserve(count);
574*a9fa9459Szrj }
575*a9fa9459Szrj 
~Symbol_table()576*a9fa9459Szrj Symbol_table::~Symbol_table()
577*a9fa9459Szrj {
578*a9fa9459Szrj }
579*a9fa9459Szrj 
580*a9fa9459Szrj // The symbol table key equality function.  This is called with
581*a9fa9459Szrj // Stringpool keys.
582*a9fa9459Szrj 
583*a9fa9459Szrj inline bool
operator ()(const Symbol_table_key & k1,const Symbol_table_key & k2) const584*a9fa9459Szrj Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1,
585*a9fa9459Szrj 					  const Symbol_table_key& k2) const
586*a9fa9459Szrj {
587*a9fa9459Szrj   return k1.first == k2.first && k1.second == k2.second;
588*a9fa9459Szrj }
589*a9fa9459Szrj 
590*a9fa9459Szrj bool
is_section_folded(Relobj * obj,unsigned int shndx) const591*a9fa9459Szrj Symbol_table::is_section_folded(Relobj* obj, unsigned int shndx) const
592*a9fa9459Szrj {
593*a9fa9459Szrj   return (parameters->options().icf_enabled()
594*a9fa9459Szrj           && this->icf_->is_section_folded(obj, shndx));
595*a9fa9459Szrj }
596*a9fa9459Szrj 
597*a9fa9459Szrj // For symbols that have been listed with a -u or --export-dynamic-symbol
598*a9fa9459Szrj // option, add them to the work list to avoid gc'ing them.
599*a9fa9459Szrj 
600*a9fa9459Szrj void
gc_mark_undef_symbols(Layout * layout)601*a9fa9459Szrj Symbol_table::gc_mark_undef_symbols(Layout* layout)
602*a9fa9459Szrj {
603*a9fa9459Szrj   for (options::String_set::const_iterator p =
604*a9fa9459Szrj 	 parameters->options().undefined_begin();
605*a9fa9459Szrj        p != parameters->options().undefined_end();
606*a9fa9459Szrj        ++p)
607*a9fa9459Szrj     {
608*a9fa9459Szrj       const char* name = p->c_str();
609*a9fa9459Szrj       Symbol* sym = this->lookup(name);
610*a9fa9459Szrj       gold_assert(sym != NULL);
611*a9fa9459Szrj       if (sym->source() == Symbol::FROM_OBJECT
612*a9fa9459Szrj           && !sym->object()->is_dynamic())
613*a9fa9459Szrj         {
614*a9fa9459Szrj 	  this->gc_mark_symbol(sym);
615*a9fa9459Szrj         }
616*a9fa9459Szrj     }
617*a9fa9459Szrj 
618*a9fa9459Szrj   for (options::String_set::const_iterator p =
619*a9fa9459Szrj 	 parameters->options().export_dynamic_symbol_begin();
620*a9fa9459Szrj        p != parameters->options().export_dynamic_symbol_end();
621*a9fa9459Szrj        ++p)
622*a9fa9459Szrj     {
623*a9fa9459Szrj       const char* name = p->c_str();
624*a9fa9459Szrj       Symbol* sym = this->lookup(name);
625*a9fa9459Szrj       // It's not an error if a symbol named by --export-dynamic-symbol
626*a9fa9459Szrj       // is undefined.
627*a9fa9459Szrj       if (sym != NULL
628*a9fa9459Szrj 	  && sym->source() == Symbol::FROM_OBJECT
629*a9fa9459Szrj           && !sym->object()->is_dynamic())
630*a9fa9459Szrj         {
631*a9fa9459Szrj 	  this->gc_mark_symbol(sym);
632*a9fa9459Szrj         }
633*a9fa9459Szrj     }
634*a9fa9459Szrj 
635*a9fa9459Szrj   for (Script_options::referenced_const_iterator p =
636*a9fa9459Szrj 	 layout->script_options()->referenced_begin();
637*a9fa9459Szrj        p != layout->script_options()->referenced_end();
638*a9fa9459Szrj        ++p)
639*a9fa9459Szrj     {
640*a9fa9459Szrj       Symbol* sym = this->lookup(p->c_str());
641*a9fa9459Szrj       gold_assert(sym != NULL);
642*a9fa9459Szrj       if (sym->source() == Symbol::FROM_OBJECT
643*a9fa9459Szrj 	  && !sym->object()->is_dynamic())
644*a9fa9459Szrj 	{
645*a9fa9459Szrj 	  this->gc_mark_symbol(sym);
646*a9fa9459Szrj 	}
647*a9fa9459Szrj     }
648*a9fa9459Szrj }
649*a9fa9459Szrj 
650*a9fa9459Szrj void
gc_mark_symbol(Symbol * sym)651*a9fa9459Szrj Symbol_table::gc_mark_symbol(Symbol* sym)
652*a9fa9459Szrj {
653*a9fa9459Szrj   // Add the object and section to the work list.
654*a9fa9459Szrj   bool is_ordinary;
655*a9fa9459Szrj   unsigned int shndx = sym->shndx(&is_ordinary);
656*a9fa9459Szrj   if (is_ordinary && shndx != elfcpp::SHN_UNDEF && !sym->object()->is_dynamic())
657*a9fa9459Szrj     {
658*a9fa9459Szrj       gold_assert(this->gc_!= NULL);
659*a9fa9459Szrj       Relobj* relobj = static_cast<Relobj*>(sym->object());
660*a9fa9459Szrj       this->gc_->worklist().push_back(Section_id(relobj, shndx));
661*a9fa9459Szrj     }
662*a9fa9459Szrj   parameters->target().gc_mark_symbol(this, sym);
663*a9fa9459Szrj }
664*a9fa9459Szrj 
665*a9fa9459Szrj // When doing garbage collection, keep symbols that have been seen in
666*a9fa9459Szrj // dynamic objects.
667*a9fa9459Szrj inline void
gc_mark_dyn_syms(Symbol * sym)668*a9fa9459Szrj Symbol_table::gc_mark_dyn_syms(Symbol* sym)
669*a9fa9459Szrj {
670*a9fa9459Szrj   if (sym->in_dyn() && sym->source() == Symbol::FROM_OBJECT
671*a9fa9459Szrj       && !sym->object()->is_dynamic())
672*a9fa9459Szrj     this->gc_mark_symbol(sym);
673*a9fa9459Szrj }
674*a9fa9459Szrj 
675*a9fa9459Szrj // Make TO a symbol which forwards to FROM.
676*a9fa9459Szrj 
677*a9fa9459Szrj void
make_forwarder(Symbol * from,Symbol * to)678*a9fa9459Szrj Symbol_table::make_forwarder(Symbol* from, Symbol* to)
679*a9fa9459Szrj {
680*a9fa9459Szrj   gold_assert(from != to);
681*a9fa9459Szrj   gold_assert(!from->is_forwarder() && !to->is_forwarder());
682*a9fa9459Szrj   this->forwarders_[from] = to;
683*a9fa9459Szrj   from->set_forwarder();
684*a9fa9459Szrj }
685*a9fa9459Szrj 
686*a9fa9459Szrj // Resolve the forwards from FROM, returning the real symbol.
687*a9fa9459Szrj 
688*a9fa9459Szrj Symbol*
resolve_forwards(const Symbol * from) const689*a9fa9459Szrj Symbol_table::resolve_forwards(const Symbol* from) const
690*a9fa9459Szrj {
691*a9fa9459Szrj   gold_assert(from->is_forwarder());
692*a9fa9459Szrj   Unordered_map<const Symbol*, Symbol*>::const_iterator p =
693*a9fa9459Szrj     this->forwarders_.find(from);
694*a9fa9459Szrj   gold_assert(p != this->forwarders_.end());
695*a9fa9459Szrj   return p->second;
696*a9fa9459Szrj }
697*a9fa9459Szrj 
698*a9fa9459Szrj // Look up a symbol by name.
699*a9fa9459Szrj 
700*a9fa9459Szrj Symbol*
lookup(const char * name,const char * version) const701*a9fa9459Szrj Symbol_table::lookup(const char* name, const char* version) const
702*a9fa9459Szrj {
703*a9fa9459Szrj   Stringpool::Key name_key;
704*a9fa9459Szrj   name = this->namepool_.find(name, &name_key);
705*a9fa9459Szrj   if (name == NULL)
706*a9fa9459Szrj     return NULL;
707*a9fa9459Szrj 
708*a9fa9459Szrj   Stringpool::Key version_key = 0;
709*a9fa9459Szrj   if (version != NULL)
710*a9fa9459Szrj     {
711*a9fa9459Szrj       version = this->namepool_.find(version, &version_key);
712*a9fa9459Szrj       if (version == NULL)
713*a9fa9459Szrj 	return NULL;
714*a9fa9459Szrj     }
715*a9fa9459Szrj 
716*a9fa9459Szrj   Symbol_table_key key(name_key, version_key);
717*a9fa9459Szrj   Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key);
718*a9fa9459Szrj   if (p == this->table_.end())
719*a9fa9459Szrj     return NULL;
720*a9fa9459Szrj   return p->second;
721*a9fa9459Szrj }
722*a9fa9459Szrj 
723*a9fa9459Szrj // Resolve a Symbol with another Symbol.  This is only used in the
724*a9fa9459Szrj // unusual case where there are references to both an unversioned
725*a9fa9459Szrj // symbol and a symbol with a version, and we then discover that that
726*a9fa9459Szrj // version is the default version.  Because this is unusual, we do
727*a9fa9459Szrj // this the slow way, by converting back to an ELF symbol.
728*a9fa9459Szrj 
729*a9fa9459Szrj template<int size, bool big_endian>
730*a9fa9459Szrj void
resolve(Sized_symbol<size> * to,const Sized_symbol<size> * from)731*a9fa9459Szrj Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from)
732*a9fa9459Szrj {
733*a9fa9459Szrj   unsigned char buf[elfcpp::Elf_sizes<size>::sym_size];
734*a9fa9459Szrj   elfcpp::Sym_write<size, big_endian> esym(buf);
735*a9fa9459Szrj   // We don't bother to set the st_name or the st_shndx field.
736*a9fa9459Szrj   esym.put_st_value(from->value());
737*a9fa9459Szrj   esym.put_st_size(from->symsize());
738*a9fa9459Szrj   esym.put_st_info(from->binding(), from->type());
739*a9fa9459Szrj   esym.put_st_other(from->visibility(), from->nonvis());
740*a9fa9459Szrj   bool is_ordinary;
741*a9fa9459Szrj   unsigned int shndx = from->shndx(&is_ordinary);
742*a9fa9459Szrj   this->resolve(to, esym.sym(), shndx, is_ordinary, shndx, from->object(),
743*a9fa9459Szrj 		from->version(), true);
744*a9fa9459Szrj   if (from->in_reg())
745*a9fa9459Szrj     to->set_in_reg();
746*a9fa9459Szrj   if (from->in_dyn())
747*a9fa9459Szrj     to->set_in_dyn();
748*a9fa9459Szrj   if (parameters->options().gc_sections())
749*a9fa9459Szrj     this->gc_mark_dyn_syms(to);
750*a9fa9459Szrj }
751*a9fa9459Szrj 
752*a9fa9459Szrj // Record that a symbol is forced to be local by a version script or
753*a9fa9459Szrj // by visibility.
754*a9fa9459Szrj 
755*a9fa9459Szrj void
force_local(Symbol * sym)756*a9fa9459Szrj Symbol_table::force_local(Symbol* sym)
757*a9fa9459Szrj {
758*a9fa9459Szrj   if (!sym->is_defined() && !sym->is_common())
759*a9fa9459Szrj     return;
760*a9fa9459Szrj   if (sym->is_forced_local())
761*a9fa9459Szrj     {
762*a9fa9459Szrj       // We already got this one.
763*a9fa9459Szrj       return;
764*a9fa9459Szrj     }
765*a9fa9459Szrj   sym->set_is_forced_local();
766*a9fa9459Szrj   this->forced_locals_.push_back(sym);
767*a9fa9459Szrj }
768*a9fa9459Szrj 
769*a9fa9459Szrj // Adjust NAME for wrapping, and update *NAME_KEY if necessary.  This
770*a9fa9459Szrj // is only called for undefined symbols, when at least one --wrap
771*a9fa9459Szrj // option was used.
772*a9fa9459Szrj 
773*a9fa9459Szrj const char*
wrap_symbol(const char * name,Stringpool::Key * name_key)774*a9fa9459Szrj Symbol_table::wrap_symbol(const char* name, Stringpool::Key* name_key)
775*a9fa9459Szrj {
776*a9fa9459Szrj   // For some targets, we need to ignore a specific character when
777*a9fa9459Szrj   // wrapping, and add it back later.
778*a9fa9459Szrj   char prefix = '\0';
779*a9fa9459Szrj   if (name[0] == parameters->target().wrap_char())
780*a9fa9459Szrj     {
781*a9fa9459Szrj       prefix = name[0];
782*a9fa9459Szrj       ++name;
783*a9fa9459Szrj     }
784*a9fa9459Szrj 
785*a9fa9459Szrj   if (parameters->options().is_wrap(name))
786*a9fa9459Szrj     {
787*a9fa9459Szrj       // Turn NAME into __wrap_NAME.
788*a9fa9459Szrj       std::string s;
789*a9fa9459Szrj       if (prefix != '\0')
790*a9fa9459Szrj 	s += prefix;
791*a9fa9459Szrj       s += "__wrap_";
792*a9fa9459Szrj       s += name;
793*a9fa9459Szrj 
794*a9fa9459Szrj       // This will give us both the old and new name in NAMEPOOL_, but
795*a9fa9459Szrj       // that is OK.  Only the versions we need will wind up in the
796*a9fa9459Szrj       // real string table in the output file.
797*a9fa9459Szrj       return this->namepool_.add(s.c_str(), true, name_key);
798*a9fa9459Szrj     }
799*a9fa9459Szrj 
800*a9fa9459Szrj   const char* const real_prefix = "__real_";
801*a9fa9459Szrj   const size_t real_prefix_length = strlen(real_prefix);
802*a9fa9459Szrj   if (strncmp(name, real_prefix, real_prefix_length) == 0
803*a9fa9459Szrj       && parameters->options().is_wrap(name + real_prefix_length))
804*a9fa9459Szrj     {
805*a9fa9459Szrj       // Turn __real_NAME into NAME.
806*a9fa9459Szrj       std::string s;
807*a9fa9459Szrj       if (prefix != '\0')
808*a9fa9459Szrj 	s += prefix;
809*a9fa9459Szrj       s += name + real_prefix_length;
810*a9fa9459Szrj       return this->namepool_.add(s.c_str(), true, name_key);
811*a9fa9459Szrj     }
812*a9fa9459Szrj 
813*a9fa9459Szrj   return name;
814*a9fa9459Szrj }
815*a9fa9459Szrj 
816*a9fa9459Szrj // This is called when we see a symbol NAME/VERSION, and the symbol
817*a9fa9459Szrj // already exists in the symbol table, and VERSION is marked as being
818*a9fa9459Szrj // the default version.  SYM is the NAME/VERSION symbol we just added.
819*a9fa9459Szrj // DEFAULT_IS_NEW is true if this is the first time we have seen the
820*a9fa9459Szrj // symbol NAME/NULL.  PDEF points to the entry for NAME/NULL.
821*a9fa9459Szrj 
822*a9fa9459Szrj template<int size, bool big_endian>
823*a9fa9459Szrj void
define_default_version(Sized_symbol<size> * sym,bool default_is_new,Symbol_table_type::iterator pdef)824*a9fa9459Szrj Symbol_table::define_default_version(Sized_symbol<size>* sym,
825*a9fa9459Szrj 				     bool default_is_new,
826*a9fa9459Szrj 				     Symbol_table_type::iterator pdef)
827*a9fa9459Szrj {
828*a9fa9459Szrj   if (default_is_new)
829*a9fa9459Szrj     {
830*a9fa9459Szrj       // This is the first time we have seen NAME/NULL.  Make
831*a9fa9459Szrj       // NAME/NULL point to NAME/VERSION, and mark SYM as the default
832*a9fa9459Szrj       // version.
833*a9fa9459Szrj       pdef->second = sym;
834*a9fa9459Szrj       sym->set_is_default();
835*a9fa9459Szrj     }
836*a9fa9459Szrj   else if (pdef->second == sym)
837*a9fa9459Szrj     {
838*a9fa9459Szrj       // NAME/NULL already points to NAME/VERSION.  Don't mark the
839*a9fa9459Szrj       // symbol as the default if it is not already the default.
840*a9fa9459Szrj     }
841*a9fa9459Szrj   else
842*a9fa9459Szrj     {
843*a9fa9459Szrj       // This is the unfortunate case where we already have entries
844*a9fa9459Szrj       // for both NAME/VERSION and NAME/NULL.  We now see a symbol
845*a9fa9459Szrj       // NAME/VERSION where VERSION is the default version.  We have
846*a9fa9459Szrj       // already resolved this new symbol with the existing
847*a9fa9459Szrj       // NAME/VERSION symbol.
848*a9fa9459Szrj 
849*a9fa9459Szrj       // It's possible that NAME/NULL and NAME/VERSION are both
850*a9fa9459Szrj       // defined in regular objects.  This can only happen if one
851*a9fa9459Szrj       // object file defines foo and another defines foo@@ver.  This
852*a9fa9459Szrj       // is somewhat obscure, but we call it a multiple definition
853*a9fa9459Szrj       // error.
854*a9fa9459Szrj 
855*a9fa9459Szrj       // It's possible that NAME/NULL actually has a version, in which
856*a9fa9459Szrj       // case it won't be the same as VERSION.  This happens with
857*a9fa9459Szrj       // ver_test_7.so in the testsuite for the symbol t2_2.  We see
858*a9fa9459Szrj       // t2_2@@VER2, so we define both t2_2/VER2 and t2_2/NULL.  We
859*a9fa9459Szrj       // then see an unadorned t2_2 in an object file and give it
860*a9fa9459Szrj       // version VER1 from the version script.  This looks like a
861*a9fa9459Szrj       // default definition for VER1, so it looks like we should merge
862*a9fa9459Szrj       // t2_2/NULL with t2_2/VER1.  That doesn't make sense, but it's
863*a9fa9459Szrj       // not obvious that this is an error, either.  So we just punt.
864*a9fa9459Szrj 
865*a9fa9459Szrj       // If one of the symbols has non-default visibility, and the
866*a9fa9459Szrj       // other is defined in a shared object, then they are different
867*a9fa9459Szrj       // symbols.
868*a9fa9459Szrj 
869*a9fa9459Szrj       // If the two symbols are from different shared objects,
870*a9fa9459Szrj       // they are different symbols.
871*a9fa9459Szrj 
872*a9fa9459Szrj       // Otherwise, we just resolve the symbols as though they were
873*a9fa9459Szrj       // the same.
874*a9fa9459Szrj 
875*a9fa9459Szrj       if (pdef->second->version() != NULL)
876*a9fa9459Szrj 	gold_assert(pdef->second->version() != sym->version());
877*a9fa9459Szrj       else if (sym->visibility() != elfcpp::STV_DEFAULT
878*a9fa9459Szrj 	       && pdef->second->is_from_dynobj())
879*a9fa9459Szrj 	;
880*a9fa9459Szrj       else if (pdef->second->visibility() != elfcpp::STV_DEFAULT
881*a9fa9459Szrj 	       && sym->is_from_dynobj())
882*a9fa9459Szrj 	;
883*a9fa9459Szrj       else if (pdef->second->is_from_dynobj()
884*a9fa9459Szrj 	       && sym->is_from_dynobj()
885*a9fa9459Szrj 	       && pdef->second->object() != sym->object())
886*a9fa9459Szrj         ;
887*a9fa9459Szrj       else
888*a9fa9459Szrj 	{
889*a9fa9459Szrj 	  const Sized_symbol<size>* symdef;
890*a9fa9459Szrj 	  symdef = this->get_sized_symbol<size>(pdef->second);
891*a9fa9459Szrj 	  Symbol_table::resolve<size, big_endian>(sym, symdef);
892*a9fa9459Szrj 	  this->make_forwarder(pdef->second, sym);
893*a9fa9459Szrj 	  pdef->second = sym;
894*a9fa9459Szrj 	  sym->set_is_default();
895*a9fa9459Szrj 	}
896*a9fa9459Szrj     }
897*a9fa9459Szrj }
898*a9fa9459Szrj 
899*a9fa9459Szrj // Add one symbol from OBJECT to the symbol table.  NAME is symbol
900*a9fa9459Szrj // name and VERSION is the version; both are canonicalized.  DEF is
901*a9fa9459Szrj // whether this is the default version.  ST_SHNDX is the symbol's
902*a9fa9459Szrj // section index; IS_ORDINARY is whether this is a normal section
903*a9fa9459Szrj // rather than a special code.
904*a9fa9459Szrj 
905*a9fa9459Szrj // If IS_DEFAULT_VERSION is true, then this is the definition of a
906*a9fa9459Szrj // default version of a symbol.  That means that any lookup of
907*a9fa9459Szrj // NAME/NULL and any lookup of NAME/VERSION should always return the
908*a9fa9459Szrj // same symbol.  This is obvious for references, but in particular we
909*a9fa9459Szrj // want to do this for definitions: overriding NAME/NULL should also
910*a9fa9459Szrj // override NAME/VERSION.  If we don't do that, it would be very hard
911*a9fa9459Szrj // to override functions in a shared library which uses versioning.
912*a9fa9459Szrj 
913*a9fa9459Szrj // We implement this by simply making both entries in the hash table
914*a9fa9459Szrj // point to the same Symbol structure.  That is easy enough if this is
915*a9fa9459Szrj // the first time we see NAME/NULL or NAME/VERSION, but it is possible
916*a9fa9459Szrj // that we have seen both already, in which case they will both have
917*a9fa9459Szrj // independent entries in the symbol table.  We can't simply change
918*a9fa9459Szrj // the symbol table entry, because we have pointers to the entries
919*a9fa9459Szrj // attached to the object files.  So we mark the entry attached to the
920*a9fa9459Szrj // object file as a forwarder, and record it in the forwarders_ map.
921*a9fa9459Szrj // Note that entries in the hash table will never be marked as
922*a9fa9459Szrj // forwarders.
923*a9fa9459Szrj //
924*a9fa9459Szrj // ORIG_ST_SHNDX and ST_SHNDX are almost always the same.
925*a9fa9459Szrj // ORIG_ST_SHNDX is the section index in the input file, or SHN_UNDEF
926*a9fa9459Szrj // for a special section code.  ST_SHNDX may be modified if the symbol
927*a9fa9459Szrj // is defined in a section being discarded.
928*a9fa9459Szrj 
929*a9fa9459Szrj template<int size, bool big_endian>
930*a9fa9459Szrj Sized_symbol<size>*
add_from_object(Object * object,const char * name,Stringpool::Key name_key,const char * version,Stringpool::Key version_key,bool is_default_version,const elfcpp::Sym<size,big_endian> & sym,unsigned int st_shndx,bool is_ordinary,unsigned int orig_st_shndx)931*a9fa9459Szrj Symbol_table::add_from_object(Object* object,
932*a9fa9459Szrj 			      const char* name,
933*a9fa9459Szrj 			      Stringpool::Key name_key,
934*a9fa9459Szrj 			      const char* version,
935*a9fa9459Szrj 			      Stringpool::Key version_key,
936*a9fa9459Szrj 			      bool is_default_version,
937*a9fa9459Szrj 			      const elfcpp::Sym<size, big_endian>& sym,
938*a9fa9459Szrj 			      unsigned int st_shndx,
939*a9fa9459Szrj 			      bool is_ordinary,
940*a9fa9459Szrj 			      unsigned int orig_st_shndx)
941*a9fa9459Szrj {
942*a9fa9459Szrj   // Print a message if this symbol is being traced.
943*a9fa9459Szrj   if (parameters->options().is_trace_symbol(name))
944*a9fa9459Szrj     {
945*a9fa9459Szrj       if (orig_st_shndx == elfcpp::SHN_UNDEF)
946*a9fa9459Szrj         gold_info(_("%s: reference to %s"), object->name().c_str(), name);
947*a9fa9459Szrj       else
948*a9fa9459Szrj         gold_info(_("%s: definition of %s"), object->name().c_str(), name);
949*a9fa9459Szrj     }
950*a9fa9459Szrj 
951*a9fa9459Szrj   // For an undefined symbol, we may need to adjust the name using
952*a9fa9459Szrj   // --wrap.
953*a9fa9459Szrj   if (orig_st_shndx == elfcpp::SHN_UNDEF
954*a9fa9459Szrj       && parameters->options().any_wrap())
955*a9fa9459Szrj     {
956*a9fa9459Szrj       const char* wrap_name = this->wrap_symbol(name, &name_key);
957*a9fa9459Szrj       if (wrap_name != name)
958*a9fa9459Szrj 	{
959*a9fa9459Szrj 	  // If we see a reference to malloc with version GLIBC_2.0,
960*a9fa9459Szrj 	  // and we turn it into a reference to __wrap_malloc, then we
961*a9fa9459Szrj 	  // discard the version number.  Otherwise the user would be
962*a9fa9459Szrj 	  // required to specify the correct version for
963*a9fa9459Szrj 	  // __wrap_malloc.
964*a9fa9459Szrj 	  version = NULL;
965*a9fa9459Szrj 	  version_key = 0;
966*a9fa9459Szrj 	  name = wrap_name;
967*a9fa9459Szrj 	}
968*a9fa9459Szrj     }
969*a9fa9459Szrj 
970*a9fa9459Szrj   Symbol* const snull = NULL;
971*a9fa9459Szrj   std::pair<typename Symbol_table_type::iterator, bool> ins =
972*a9fa9459Szrj     this->table_.insert(std::make_pair(std::make_pair(name_key, version_key),
973*a9fa9459Szrj 				       snull));
974*a9fa9459Szrj 
975*a9fa9459Szrj   std::pair<typename Symbol_table_type::iterator, bool> insdefault =
976*a9fa9459Szrj     std::make_pair(this->table_.end(), false);
977*a9fa9459Szrj   if (is_default_version)
978*a9fa9459Szrj     {
979*a9fa9459Szrj       const Stringpool::Key vnull_key = 0;
980*a9fa9459Szrj       insdefault = this->table_.insert(std::make_pair(std::make_pair(name_key,
981*a9fa9459Szrj 								     vnull_key),
982*a9fa9459Szrj 						      snull));
983*a9fa9459Szrj     }
984*a9fa9459Szrj 
985*a9fa9459Szrj   // ins.first: an iterator, which is a pointer to a pair.
986*a9fa9459Szrj   // ins.first->first: the key (a pair of name and version).
987*a9fa9459Szrj   // ins.first->second: the value (Symbol*).
988*a9fa9459Szrj   // ins.second: true if new entry was inserted, false if not.
989*a9fa9459Szrj 
990*a9fa9459Szrj   Sized_symbol<size>* ret;
991*a9fa9459Szrj   bool was_undefined;
992*a9fa9459Szrj   bool was_common;
993*a9fa9459Szrj   if (!ins.second)
994*a9fa9459Szrj     {
995*a9fa9459Szrj       // We already have an entry for NAME/VERSION.
996*a9fa9459Szrj       ret = this->get_sized_symbol<size>(ins.first->second);
997*a9fa9459Szrj       gold_assert(ret != NULL);
998*a9fa9459Szrj 
999*a9fa9459Szrj       was_undefined = ret->is_undefined();
1000*a9fa9459Szrj       // Commons from plugins are just placeholders.
1001*a9fa9459Szrj       was_common = ret->is_common() && ret->object()->pluginobj() == NULL;
1002*a9fa9459Szrj 
1003*a9fa9459Szrj       this->resolve(ret, sym, st_shndx, is_ordinary, orig_st_shndx, object,
1004*a9fa9459Szrj 		    version, is_default_version);
1005*a9fa9459Szrj       if (parameters->options().gc_sections())
1006*a9fa9459Szrj         this->gc_mark_dyn_syms(ret);
1007*a9fa9459Szrj 
1008*a9fa9459Szrj       if (is_default_version)
1009*a9fa9459Szrj 	this->define_default_version<size, big_endian>(ret, insdefault.second,
1010*a9fa9459Szrj 						       insdefault.first);
1011*a9fa9459Szrj       else
1012*a9fa9459Szrj 	{
1013*a9fa9459Szrj 	  bool dummy;
1014*a9fa9459Szrj 	  if (version != NULL
1015*a9fa9459Szrj 	      && ret->source() == Symbol::FROM_OBJECT
1016*a9fa9459Szrj 	      && ret->object() == object
1017*a9fa9459Szrj 	      && is_ordinary
1018*a9fa9459Szrj 	      && ret->shndx(&dummy) == st_shndx
1019*a9fa9459Szrj 	      && ret->is_default())
1020*a9fa9459Szrj 	    {
1021*a9fa9459Szrj 	      // We have seen NAME/VERSION already, and marked it as the
1022*a9fa9459Szrj 	      // default version, but now we see a definition for
1023*a9fa9459Szrj 	      // NAME/VERSION that is not the default version. This can
1024*a9fa9459Szrj 	      // happen when the assembler generates two symbols for
1025*a9fa9459Szrj 	      // a symbol as a result of a ".symver foo,foo@VER"
1026*a9fa9459Szrj 	      // directive. We see the first unversioned symbol and
1027*a9fa9459Szrj 	      // we may mark it as the default version (from a
1028*a9fa9459Szrj 	      // version script); then we see the second versioned
1029*a9fa9459Szrj 	      // symbol and we need to override the first.
1030*a9fa9459Szrj 	      // In any other case, the two symbols should have generated
1031*a9fa9459Szrj 	      // a multiple definition error.
1032*a9fa9459Szrj 	      // (See PR gold/18703.)
1033*a9fa9459Szrj 	      ret->set_is_not_default();
1034*a9fa9459Szrj 	      const Stringpool::Key vnull_key = 0;
1035*a9fa9459Szrj 	      this->table_.erase(std::make_pair(name_key, vnull_key));
1036*a9fa9459Szrj 	    }
1037*a9fa9459Szrj 	}
1038*a9fa9459Szrj     }
1039*a9fa9459Szrj   else
1040*a9fa9459Szrj     {
1041*a9fa9459Szrj       // This is the first time we have seen NAME/VERSION.
1042*a9fa9459Szrj       gold_assert(ins.first->second == NULL);
1043*a9fa9459Szrj 
1044*a9fa9459Szrj       if (is_default_version && !insdefault.second)
1045*a9fa9459Szrj 	{
1046*a9fa9459Szrj 	  // We already have an entry for NAME/NULL.  If we override
1047*a9fa9459Szrj 	  // it, then change it to NAME/VERSION.
1048*a9fa9459Szrj 	  ret = this->get_sized_symbol<size>(insdefault.first->second);
1049*a9fa9459Szrj 
1050*a9fa9459Szrj 	  was_undefined = ret->is_undefined();
1051*a9fa9459Szrj 	  // Commons from plugins are just placeholders.
1052*a9fa9459Szrj 	  was_common = ret->is_common() && ret->object()->pluginobj() == NULL;
1053*a9fa9459Szrj 
1054*a9fa9459Szrj 	  this->resolve(ret, sym, st_shndx, is_ordinary, orig_st_shndx, object,
1055*a9fa9459Szrj 			version, is_default_version);
1056*a9fa9459Szrj           if (parameters->options().gc_sections())
1057*a9fa9459Szrj             this->gc_mark_dyn_syms(ret);
1058*a9fa9459Szrj 	  ins.first->second = ret;
1059*a9fa9459Szrj 	}
1060*a9fa9459Szrj       else
1061*a9fa9459Szrj 	{
1062*a9fa9459Szrj 	  was_undefined = false;
1063*a9fa9459Szrj 	  was_common = false;
1064*a9fa9459Szrj 
1065*a9fa9459Szrj 	  Sized_target<size, big_endian>* target =
1066*a9fa9459Szrj 	    parameters->sized_target<size, big_endian>();
1067*a9fa9459Szrj 	  if (!target->has_make_symbol())
1068*a9fa9459Szrj 	    ret = new Sized_symbol<size>();
1069*a9fa9459Szrj 	  else
1070*a9fa9459Szrj 	    {
1071*a9fa9459Szrj 	      ret = target->make_symbol(name, sym.get_st_type(), object,
1072*a9fa9459Szrj 					st_shndx, sym.get_st_value());
1073*a9fa9459Szrj 	      if (ret == NULL)
1074*a9fa9459Szrj 		{
1075*a9fa9459Szrj 		  // This means that we don't want a symbol table
1076*a9fa9459Szrj 		  // entry after all.
1077*a9fa9459Szrj 		  if (!is_default_version)
1078*a9fa9459Szrj 		    this->table_.erase(ins.first);
1079*a9fa9459Szrj 		  else
1080*a9fa9459Szrj 		    {
1081*a9fa9459Szrj 		      this->table_.erase(insdefault.first);
1082*a9fa9459Szrj 		      // Inserting INSDEFAULT invalidated INS.
1083*a9fa9459Szrj 		      this->table_.erase(std::make_pair(name_key,
1084*a9fa9459Szrj 							version_key));
1085*a9fa9459Szrj 		    }
1086*a9fa9459Szrj 		  return NULL;
1087*a9fa9459Szrj 		}
1088*a9fa9459Szrj 	    }
1089*a9fa9459Szrj 
1090*a9fa9459Szrj 	  ret->init_object(name, version, object, sym, st_shndx, is_ordinary);
1091*a9fa9459Szrj 
1092*a9fa9459Szrj 	  ins.first->second = ret;
1093*a9fa9459Szrj 	  if (is_default_version)
1094*a9fa9459Szrj 	    {
1095*a9fa9459Szrj 	      // This is the first time we have seen NAME/NULL.  Point
1096*a9fa9459Szrj 	      // it at the new entry for NAME/VERSION.
1097*a9fa9459Szrj 	      gold_assert(insdefault.second);
1098*a9fa9459Szrj 	      insdefault.first->second = ret;
1099*a9fa9459Szrj 	    }
1100*a9fa9459Szrj 	}
1101*a9fa9459Szrj 
1102*a9fa9459Szrj       if (is_default_version)
1103*a9fa9459Szrj 	ret->set_is_default();
1104*a9fa9459Szrj     }
1105*a9fa9459Szrj 
1106*a9fa9459Szrj   // Record every time we see a new undefined symbol, to speed up
1107*a9fa9459Szrj   // archive groups.
1108*a9fa9459Szrj   if (!was_undefined && ret->is_undefined())
1109*a9fa9459Szrj     {
1110*a9fa9459Szrj       ++this->saw_undefined_;
1111*a9fa9459Szrj       if (parameters->options().has_plugins())
1112*a9fa9459Szrj 	parameters->options().plugins()->new_undefined_symbol(ret);
1113*a9fa9459Szrj     }
1114*a9fa9459Szrj 
1115*a9fa9459Szrj   // Keep track of common symbols, to speed up common symbol
1116*a9fa9459Szrj   // allocation.  Don't record commons from plugin objects;
1117*a9fa9459Szrj   // we need to wait until we see the real symbol in the
1118*a9fa9459Szrj   // replacement file.
1119*a9fa9459Szrj   if (!was_common && ret->is_common() && ret->object()->pluginobj() == NULL)
1120*a9fa9459Szrj     {
1121*a9fa9459Szrj       if (ret->type() == elfcpp::STT_TLS)
1122*a9fa9459Szrj 	this->tls_commons_.push_back(ret);
1123*a9fa9459Szrj       else if (!is_ordinary
1124*a9fa9459Szrj 	       && st_shndx == parameters->target().small_common_shndx())
1125*a9fa9459Szrj 	this->small_commons_.push_back(ret);
1126*a9fa9459Szrj       else if (!is_ordinary
1127*a9fa9459Szrj 	       && st_shndx == parameters->target().large_common_shndx())
1128*a9fa9459Szrj 	this->large_commons_.push_back(ret);
1129*a9fa9459Szrj       else
1130*a9fa9459Szrj 	this->commons_.push_back(ret);
1131*a9fa9459Szrj     }
1132*a9fa9459Szrj 
1133*a9fa9459Szrj   // If we're not doing a relocatable link, then any symbol with
1134*a9fa9459Szrj   // hidden or internal visibility is local.
1135*a9fa9459Szrj   if ((ret->visibility() == elfcpp::STV_HIDDEN
1136*a9fa9459Szrj        || ret->visibility() == elfcpp::STV_INTERNAL)
1137*a9fa9459Szrj       && (ret->binding() == elfcpp::STB_GLOBAL
1138*a9fa9459Szrj 	  || ret->binding() == elfcpp::STB_GNU_UNIQUE
1139*a9fa9459Szrj 	  || ret->binding() == elfcpp::STB_WEAK)
1140*a9fa9459Szrj       && !parameters->options().relocatable())
1141*a9fa9459Szrj     this->force_local(ret);
1142*a9fa9459Szrj 
1143*a9fa9459Szrj   return ret;
1144*a9fa9459Szrj }
1145*a9fa9459Szrj 
1146*a9fa9459Szrj // Add all the symbols in a relocatable object to the hash table.
1147*a9fa9459Szrj 
1148*a9fa9459Szrj template<int size, bool big_endian>
1149*a9fa9459Szrj void
add_from_relobj(Sized_relobj_file<size,big_endian> * relobj,const unsigned char * syms,size_t count,size_t symndx_offset,const char * sym_names,size_t sym_name_size,typename Sized_relobj_file<size,big_endian>::Symbols * sympointers,size_t * defined)1150*a9fa9459Szrj Symbol_table::add_from_relobj(
1151*a9fa9459Szrj     Sized_relobj_file<size, big_endian>* relobj,
1152*a9fa9459Szrj     const unsigned char* syms,
1153*a9fa9459Szrj     size_t count,
1154*a9fa9459Szrj     size_t symndx_offset,
1155*a9fa9459Szrj     const char* sym_names,
1156*a9fa9459Szrj     size_t sym_name_size,
1157*a9fa9459Szrj     typename Sized_relobj_file<size, big_endian>::Symbols* sympointers,
1158*a9fa9459Szrj     size_t* defined)
1159*a9fa9459Szrj {
1160*a9fa9459Szrj   *defined = 0;
1161*a9fa9459Szrj 
1162*a9fa9459Szrj   gold_assert(size == parameters->target().get_size());
1163*a9fa9459Szrj 
1164*a9fa9459Szrj   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1165*a9fa9459Szrj 
1166*a9fa9459Szrj   const bool just_symbols = relobj->just_symbols();
1167*a9fa9459Szrj 
1168*a9fa9459Szrj   const unsigned char* p = syms;
1169*a9fa9459Szrj   for (size_t i = 0; i < count; ++i, p += sym_size)
1170*a9fa9459Szrj     {
1171*a9fa9459Szrj       (*sympointers)[i] = NULL;
1172*a9fa9459Szrj 
1173*a9fa9459Szrj       elfcpp::Sym<size, big_endian> sym(p);
1174*a9fa9459Szrj 
1175*a9fa9459Szrj       unsigned int st_name = sym.get_st_name();
1176*a9fa9459Szrj       if (st_name >= sym_name_size)
1177*a9fa9459Szrj 	{
1178*a9fa9459Szrj 	  relobj->error(_("bad global symbol name offset %u at %zu"),
1179*a9fa9459Szrj 			st_name, i);
1180*a9fa9459Szrj 	  continue;
1181*a9fa9459Szrj 	}
1182*a9fa9459Szrj 
1183*a9fa9459Szrj       const char* name = sym_names + st_name;
1184*a9fa9459Szrj 
1185*a9fa9459Szrj       if (!parameters->options().relocatable()
1186*a9fa9459Szrj 	  && strcmp (name, "__gnu_lto_slim") == 0)
1187*a9fa9459Szrj         gold_info(_("%s: plugin needed to handle lto object"),
1188*a9fa9459Szrj 		  relobj->name().c_str());
1189*a9fa9459Szrj 
1190*a9fa9459Szrj       bool is_ordinary;
1191*a9fa9459Szrj       unsigned int st_shndx = relobj->adjust_sym_shndx(i + symndx_offset,
1192*a9fa9459Szrj 						       sym.get_st_shndx(),
1193*a9fa9459Szrj 						       &is_ordinary);
1194*a9fa9459Szrj       unsigned int orig_st_shndx = st_shndx;
1195*a9fa9459Szrj       if (!is_ordinary)
1196*a9fa9459Szrj 	orig_st_shndx = elfcpp::SHN_UNDEF;
1197*a9fa9459Szrj 
1198*a9fa9459Szrj       if (st_shndx != elfcpp::SHN_UNDEF)
1199*a9fa9459Szrj 	++*defined;
1200*a9fa9459Szrj 
1201*a9fa9459Szrj       // A symbol defined in a section which we are not including must
1202*a9fa9459Szrj       // be treated as an undefined symbol.
1203*a9fa9459Szrj       bool is_defined_in_discarded_section = false;
1204*a9fa9459Szrj       if (st_shndx != elfcpp::SHN_UNDEF
1205*a9fa9459Szrj 	  && is_ordinary
1206*a9fa9459Szrj 	  && !relobj->is_section_included(st_shndx)
1207*a9fa9459Szrj           && !this->is_section_folded(relobj, st_shndx))
1208*a9fa9459Szrj 	{
1209*a9fa9459Szrj 	  st_shndx = elfcpp::SHN_UNDEF;
1210*a9fa9459Szrj 	  is_defined_in_discarded_section = true;
1211*a9fa9459Szrj 	}
1212*a9fa9459Szrj 
1213*a9fa9459Szrj       // In an object file, an '@' in the name separates the symbol
1214*a9fa9459Szrj       // name from the version name.  If there are two '@' characters,
1215*a9fa9459Szrj       // this is the default version.
1216*a9fa9459Szrj       const char* ver = strchr(name, '@');
1217*a9fa9459Szrj       Stringpool::Key ver_key = 0;
1218*a9fa9459Szrj       int namelen = 0;
1219*a9fa9459Szrj       // IS_DEFAULT_VERSION: is the version default?
1220*a9fa9459Szrj       // IS_FORCED_LOCAL: is the symbol forced local?
1221*a9fa9459Szrj       bool is_default_version = false;
1222*a9fa9459Szrj       bool is_forced_local = false;
1223*a9fa9459Szrj 
1224*a9fa9459Szrj       // FIXME: For incremental links, we don't store version information,
1225*a9fa9459Szrj       // so we need to ignore version symbols for now.
1226*a9fa9459Szrj       if (parameters->incremental_update() && ver != NULL)
1227*a9fa9459Szrj 	{
1228*a9fa9459Szrj 	  namelen = ver - name;
1229*a9fa9459Szrj 	  ver = NULL;
1230*a9fa9459Szrj 	}
1231*a9fa9459Szrj 
1232*a9fa9459Szrj       if (ver != NULL)
1233*a9fa9459Szrj         {
1234*a9fa9459Szrj           // The symbol name is of the form foo@VERSION or foo@@VERSION
1235*a9fa9459Szrj           namelen = ver - name;
1236*a9fa9459Szrj           ++ver;
1237*a9fa9459Szrj 	  if (*ver == '@')
1238*a9fa9459Szrj 	    {
1239*a9fa9459Szrj 	      is_default_version = true;
1240*a9fa9459Szrj 	      ++ver;
1241*a9fa9459Szrj 	    }
1242*a9fa9459Szrj 	  ver = this->namepool_.add(ver, true, &ver_key);
1243*a9fa9459Szrj         }
1244*a9fa9459Szrj       // We don't want to assign a version to an undefined symbol,
1245*a9fa9459Szrj       // even if it is listed in the version script.  FIXME: What
1246*a9fa9459Szrj       // about a common symbol?
1247*a9fa9459Szrj       else
1248*a9fa9459Szrj 	{
1249*a9fa9459Szrj 	  namelen = strlen(name);
1250*a9fa9459Szrj 	  if (!this->version_script_.empty()
1251*a9fa9459Szrj 	      && st_shndx != elfcpp::SHN_UNDEF)
1252*a9fa9459Szrj 	    {
1253*a9fa9459Szrj 	      // The symbol name did not have a version, but the
1254*a9fa9459Szrj 	      // version script may assign a version anyway.
1255*a9fa9459Szrj 	      std::string version;
1256*a9fa9459Szrj 	      bool is_global;
1257*a9fa9459Szrj 	      if (this->version_script_.get_symbol_version(name, &version,
1258*a9fa9459Szrj 							   &is_global))
1259*a9fa9459Szrj 		{
1260*a9fa9459Szrj 		  if (!is_global)
1261*a9fa9459Szrj 		    is_forced_local = true;
1262*a9fa9459Szrj 		  else if (!version.empty())
1263*a9fa9459Szrj 		    {
1264*a9fa9459Szrj 		      ver = this->namepool_.add_with_length(version.c_str(),
1265*a9fa9459Szrj 							    version.length(),
1266*a9fa9459Szrj 							    true,
1267*a9fa9459Szrj 							    &ver_key);
1268*a9fa9459Szrj 		      is_default_version = true;
1269*a9fa9459Szrj 		    }
1270*a9fa9459Szrj 		}
1271*a9fa9459Szrj 	    }
1272*a9fa9459Szrj 	}
1273*a9fa9459Szrj 
1274*a9fa9459Szrj       elfcpp::Sym<size, big_endian>* psym = &sym;
1275*a9fa9459Szrj       unsigned char symbuf[sym_size];
1276*a9fa9459Szrj       elfcpp::Sym<size, big_endian> sym2(symbuf);
1277*a9fa9459Szrj       if (just_symbols)
1278*a9fa9459Szrj 	{
1279*a9fa9459Szrj 	  memcpy(symbuf, p, sym_size);
1280*a9fa9459Szrj 	  elfcpp::Sym_write<size, big_endian> sw(symbuf);
1281*a9fa9459Szrj 	  if (orig_st_shndx != elfcpp::SHN_UNDEF
1282*a9fa9459Szrj 	      && is_ordinary
1283*a9fa9459Szrj 	      && relobj->e_type() == elfcpp::ET_REL)
1284*a9fa9459Szrj 	    {
1285*a9fa9459Szrj 	      // Symbol values in relocatable object files are section
1286*a9fa9459Szrj 	      // relative.  This is normally what we want, but since here
1287*a9fa9459Szrj 	      // we are converting the symbol to absolute we need to add
1288*a9fa9459Szrj 	      // the section address.  The section address in an object
1289*a9fa9459Szrj 	      // file is normally zero, but people can use a linker
1290*a9fa9459Szrj 	      // script to change it.
1291*a9fa9459Szrj 	      sw.put_st_value(sym.get_st_value()
1292*a9fa9459Szrj 			      + relobj->section_address(orig_st_shndx));
1293*a9fa9459Szrj 	    }
1294*a9fa9459Szrj 	  st_shndx = elfcpp::SHN_ABS;
1295*a9fa9459Szrj 	  is_ordinary = false;
1296*a9fa9459Szrj 	  psym = &sym2;
1297*a9fa9459Szrj 	}
1298*a9fa9459Szrj 
1299*a9fa9459Szrj       // Fix up visibility if object has no-export set.
1300*a9fa9459Szrj       if (relobj->no_export()
1301*a9fa9459Szrj 	  && (orig_st_shndx != elfcpp::SHN_UNDEF || !is_ordinary))
1302*a9fa9459Szrj         {
1303*a9fa9459Szrj 	  // We may have copied symbol already above.
1304*a9fa9459Szrj 	  if (psym != &sym2)
1305*a9fa9459Szrj 	    {
1306*a9fa9459Szrj 	      memcpy(symbuf, p, sym_size);
1307*a9fa9459Szrj 	      psym = &sym2;
1308*a9fa9459Szrj 	    }
1309*a9fa9459Szrj 
1310*a9fa9459Szrj 	  elfcpp::STV visibility = sym2.get_st_visibility();
1311*a9fa9459Szrj 	  if (visibility == elfcpp::STV_DEFAULT
1312*a9fa9459Szrj 	      || visibility == elfcpp::STV_PROTECTED)
1313*a9fa9459Szrj 	    {
1314*a9fa9459Szrj 	      elfcpp::Sym_write<size, big_endian> sw(symbuf);
1315*a9fa9459Szrj 	      unsigned char nonvis = sym2.get_st_nonvis();
1316*a9fa9459Szrj 	      sw.put_st_other(elfcpp::STV_HIDDEN, nonvis);
1317*a9fa9459Szrj 	    }
1318*a9fa9459Szrj         }
1319*a9fa9459Szrj 
1320*a9fa9459Szrj       Stringpool::Key name_key;
1321*a9fa9459Szrj       name = this->namepool_.add_with_length(name, namelen, true,
1322*a9fa9459Szrj 					     &name_key);
1323*a9fa9459Szrj 
1324*a9fa9459Szrj       Sized_symbol<size>* res;
1325*a9fa9459Szrj       res = this->add_from_object(relobj, name, name_key, ver, ver_key,
1326*a9fa9459Szrj 				  is_default_version, *psym, st_shndx,
1327*a9fa9459Szrj 				  is_ordinary, orig_st_shndx);
1328*a9fa9459Szrj 
1329*a9fa9459Szrj       if (is_forced_local)
1330*a9fa9459Szrj 	this->force_local(res);
1331*a9fa9459Szrj 
1332*a9fa9459Szrj       // Do not treat this symbol as garbage if this symbol will be
1333*a9fa9459Szrj       // exported to the dynamic symbol table.  This is true when
1334*a9fa9459Szrj       // building a shared library or using --export-dynamic and
1335*a9fa9459Szrj       // the symbol is externally visible.
1336*a9fa9459Szrj       if (parameters->options().gc_sections()
1337*a9fa9459Szrj 	  && res->is_externally_visible()
1338*a9fa9459Szrj 	  && !res->is_from_dynobj()
1339*a9fa9459Szrj           && (parameters->options().shared()
1340*a9fa9459Szrj 	      || parameters->options().export_dynamic()
1341*a9fa9459Szrj 	      || parameters->options().in_dynamic_list(res->name())))
1342*a9fa9459Szrj         this->gc_mark_symbol(res);
1343*a9fa9459Szrj 
1344*a9fa9459Szrj       if (is_defined_in_discarded_section)
1345*a9fa9459Szrj 	res->set_is_defined_in_discarded_section();
1346*a9fa9459Szrj 
1347*a9fa9459Szrj       (*sympointers)[i] = res;
1348*a9fa9459Szrj     }
1349*a9fa9459Szrj }
1350*a9fa9459Szrj 
1351*a9fa9459Szrj // Add a symbol from a plugin-claimed file.
1352*a9fa9459Szrj 
1353*a9fa9459Szrj template<int size, bool big_endian>
1354*a9fa9459Szrj Symbol*
add_from_pluginobj(Sized_pluginobj<size,big_endian> * obj,const char * name,const char * ver,elfcpp::Sym<size,big_endian> * sym)1355*a9fa9459Szrj Symbol_table::add_from_pluginobj(
1356*a9fa9459Szrj     Sized_pluginobj<size, big_endian>* obj,
1357*a9fa9459Szrj     const char* name,
1358*a9fa9459Szrj     const char* ver,
1359*a9fa9459Szrj     elfcpp::Sym<size, big_endian>* sym)
1360*a9fa9459Szrj {
1361*a9fa9459Szrj   unsigned int st_shndx = sym->get_st_shndx();
1362*a9fa9459Szrj   bool is_ordinary = st_shndx < elfcpp::SHN_LORESERVE;
1363*a9fa9459Szrj 
1364*a9fa9459Szrj   Stringpool::Key ver_key = 0;
1365*a9fa9459Szrj   bool is_default_version = false;
1366*a9fa9459Szrj   bool is_forced_local = false;
1367*a9fa9459Szrj 
1368*a9fa9459Szrj   if (ver != NULL)
1369*a9fa9459Szrj     {
1370*a9fa9459Szrj       ver = this->namepool_.add(ver, true, &ver_key);
1371*a9fa9459Szrj     }
1372*a9fa9459Szrj   // We don't want to assign a version to an undefined symbol,
1373*a9fa9459Szrj   // even if it is listed in the version script.  FIXME: What
1374*a9fa9459Szrj   // about a common symbol?
1375*a9fa9459Szrj   else
1376*a9fa9459Szrj     {
1377*a9fa9459Szrj       if (!this->version_script_.empty()
1378*a9fa9459Szrj           && st_shndx != elfcpp::SHN_UNDEF)
1379*a9fa9459Szrj         {
1380*a9fa9459Szrj           // The symbol name did not have a version, but the
1381*a9fa9459Szrj           // version script may assign a version anyway.
1382*a9fa9459Szrj           std::string version;
1383*a9fa9459Szrj 	  bool is_global;
1384*a9fa9459Szrj           if (this->version_script_.get_symbol_version(name, &version,
1385*a9fa9459Szrj 						       &is_global))
1386*a9fa9459Szrj             {
1387*a9fa9459Szrj 	      if (!is_global)
1388*a9fa9459Szrj 		is_forced_local = true;
1389*a9fa9459Szrj 	      else if (!version.empty())
1390*a9fa9459Szrj                 {
1391*a9fa9459Szrj                   ver = this->namepool_.add_with_length(version.c_str(),
1392*a9fa9459Szrj                                                         version.length(),
1393*a9fa9459Szrj                                                         true,
1394*a9fa9459Szrj                                                         &ver_key);
1395*a9fa9459Szrj                   is_default_version = true;
1396*a9fa9459Szrj                 }
1397*a9fa9459Szrj             }
1398*a9fa9459Szrj         }
1399*a9fa9459Szrj     }
1400*a9fa9459Szrj 
1401*a9fa9459Szrj   Stringpool::Key name_key;
1402*a9fa9459Szrj   name = this->namepool_.add(name, true, &name_key);
1403*a9fa9459Szrj 
1404*a9fa9459Szrj   Sized_symbol<size>* res;
1405*a9fa9459Szrj   res = this->add_from_object(obj, name, name_key, ver, ver_key,
1406*a9fa9459Szrj 		              is_default_version, *sym, st_shndx,
1407*a9fa9459Szrj 			      is_ordinary, st_shndx);
1408*a9fa9459Szrj 
1409*a9fa9459Szrj   if (is_forced_local)
1410*a9fa9459Szrj     this->force_local(res);
1411*a9fa9459Szrj 
1412*a9fa9459Szrj   return res;
1413*a9fa9459Szrj }
1414*a9fa9459Szrj 
1415*a9fa9459Szrj // Add all the symbols in a dynamic object to the hash table.
1416*a9fa9459Szrj 
1417*a9fa9459Szrj template<int size, bool big_endian>
1418*a9fa9459Szrj void
add_from_dynobj(Sized_dynobj<size,big_endian> * dynobj,const unsigned char * syms,size_t count,const char * sym_names,size_t sym_name_size,const unsigned char * versym,size_t versym_size,const std::vector<const char * > * version_map,typename Sized_relobj_file<size,big_endian>::Symbols * sympointers,size_t * defined)1419*a9fa9459Szrj Symbol_table::add_from_dynobj(
1420*a9fa9459Szrj     Sized_dynobj<size, big_endian>* dynobj,
1421*a9fa9459Szrj     const unsigned char* syms,
1422*a9fa9459Szrj     size_t count,
1423*a9fa9459Szrj     const char* sym_names,
1424*a9fa9459Szrj     size_t sym_name_size,
1425*a9fa9459Szrj     const unsigned char* versym,
1426*a9fa9459Szrj     size_t versym_size,
1427*a9fa9459Szrj     const std::vector<const char*>* version_map,
1428*a9fa9459Szrj     typename Sized_relobj_file<size, big_endian>::Symbols* sympointers,
1429*a9fa9459Szrj     size_t* defined)
1430*a9fa9459Szrj {
1431*a9fa9459Szrj   *defined = 0;
1432*a9fa9459Szrj 
1433*a9fa9459Szrj   gold_assert(size == parameters->target().get_size());
1434*a9fa9459Szrj 
1435*a9fa9459Szrj   if (dynobj->just_symbols())
1436*a9fa9459Szrj     {
1437*a9fa9459Szrj       gold_error(_("--just-symbols does not make sense with a shared object"));
1438*a9fa9459Szrj       return;
1439*a9fa9459Szrj     }
1440*a9fa9459Szrj 
1441*a9fa9459Szrj   // FIXME: For incremental links, we don't store version information,
1442*a9fa9459Szrj   // so we need to ignore version symbols for now.
1443*a9fa9459Szrj   if (parameters->incremental_update())
1444*a9fa9459Szrj     versym = NULL;
1445*a9fa9459Szrj 
1446*a9fa9459Szrj   if (versym != NULL && versym_size / 2 < count)
1447*a9fa9459Szrj     {
1448*a9fa9459Szrj       dynobj->error(_("too few symbol versions"));
1449*a9fa9459Szrj       return;
1450*a9fa9459Szrj     }
1451*a9fa9459Szrj 
1452*a9fa9459Szrj   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1453*a9fa9459Szrj 
1454*a9fa9459Szrj   // We keep a list of all STT_OBJECT symbols, so that we can resolve
1455*a9fa9459Szrj   // weak aliases.  This is necessary because if the dynamic object
1456*a9fa9459Szrj   // provides the same variable under two names, one of which is a
1457*a9fa9459Szrj   // weak definition, and the regular object refers to the weak
1458*a9fa9459Szrj   // definition, we have to put both the weak definition and the
1459*a9fa9459Szrj   // strong definition into the dynamic symbol table.  Given a weak
1460*a9fa9459Szrj   // definition, the only way that we can find the corresponding
1461*a9fa9459Szrj   // strong definition, if any, is to search the symbol table.
1462*a9fa9459Szrj   std::vector<Sized_symbol<size>*> object_symbols;
1463*a9fa9459Szrj 
1464*a9fa9459Szrj   const unsigned char* p = syms;
1465*a9fa9459Szrj   const unsigned char* vs = versym;
1466*a9fa9459Szrj   for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2)
1467*a9fa9459Szrj     {
1468*a9fa9459Szrj       elfcpp::Sym<size, big_endian> sym(p);
1469*a9fa9459Szrj 
1470*a9fa9459Szrj       if (sympointers != NULL)
1471*a9fa9459Szrj 	(*sympointers)[i] = NULL;
1472*a9fa9459Szrj 
1473*a9fa9459Szrj       // Ignore symbols with local binding or that have
1474*a9fa9459Szrj       // internal or hidden visibility.
1475*a9fa9459Szrj       if (sym.get_st_bind() == elfcpp::STB_LOCAL
1476*a9fa9459Szrj           || sym.get_st_visibility() == elfcpp::STV_INTERNAL
1477*a9fa9459Szrj           || sym.get_st_visibility() == elfcpp::STV_HIDDEN)
1478*a9fa9459Szrj 	continue;
1479*a9fa9459Szrj 
1480*a9fa9459Szrj       // A protected symbol in a shared library must be treated as a
1481*a9fa9459Szrj       // normal symbol when viewed from outside the shared library.
1482*a9fa9459Szrj       // Implement this by overriding the visibility here.
1483*a9fa9459Szrj       // Likewise, an IFUNC symbol in a shared library must be treated
1484*a9fa9459Szrj       // as a normal FUNC symbol.
1485*a9fa9459Szrj       elfcpp::Sym<size, big_endian>* psym = &sym;
1486*a9fa9459Szrj       unsigned char symbuf[sym_size];
1487*a9fa9459Szrj       elfcpp::Sym<size, big_endian> sym2(symbuf);
1488*a9fa9459Szrj       if (sym.get_st_visibility() == elfcpp::STV_PROTECTED
1489*a9fa9459Szrj 	  || sym.get_st_type() == elfcpp::STT_GNU_IFUNC)
1490*a9fa9459Szrj 	{
1491*a9fa9459Szrj 	  memcpy(symbuf, p, sym_size);
1492*a9fa9459Szrj 	  elfcpp::Sym_write<size, big_endian> sw(symbuf);
1493*a9fa9459Szrj 	  if (sym.get_st_visibility() == elfcpp::STV_PROTECTED)
1494*a9fa9459Szrj 	    sw.put_st_other(elfcpp::STV_DEFAULT, sym.get_st_nonvis());
1495*a9fa9459Szrj 	  if (sym.get_st_type() == elfcpp::STT_GNU_IFUNC)
1496*a9fa9459Szrj 	    sw.put_st_info(sym.get_st_bind(), elfcpp::STT_FUNC);
1497*a9fa9459Szrj 	  psym = &sym2;
1498*a9fa9459Szrj 	}
1499*a9fa9459Szrj 
1500*a9fa9459Szrj       unsigned int st_name = psym->get_st_name();
1501*a9fa9459Szrj       if (st_name >= sym_name_size)
1502*a9fa9459Szrj 	{
1503*a9fa9459Szrj 	  dynobj->error(_("bad symbol name offset %u at %zu"),
1504*a9fa9459Szrj 			st_name, i);
1505*a9fa9459Szrj 	  continue;
1506*a9fa9459Szrj 	}
1507*a9fa9459Szrj 
1508*a9fa9459Szrj       const char* name = sym_names + st_name;
1509*a9fa9459Szrj 
1510*a9fa9459Szrj       bool is_ordinary;
1511*a9fa9459Szrj       unsigned int st_shndx = dynobj->adjust_sym_shndx(i, psym->get_st_shndx(),
1512*a9fa9459Szrj 						       &is_ordinary);
1513*a9fa9459Szrj 
1514*a9fa9459Szrj       if (st_shndx != elfcpp::SHN_UNDEF)
1515*a9fa9459Szrj 	++*defined;
1516*a9fa9459Szrj 
1517*a9fa9459Szrj       Sized_symbol<size>* res;
1518*a9fa9459Szrj 
1519*a9fa9459Szrj       if (versym == NULL)
1520*a9fa9459Szrj 	{
1521*a9fa9459Szrj 	  Stringpool::Key name_key;
1522*a9fa9459Szrj 	  name = this->namepool_.add(name, true, &name_key);
1523*a9fa9459Szrj 	  res = this->add_from_object(dynobj, name, name_key, NULL, 0,
1524*a9fa9459Szrj 				      false, *psym, st_shndx, is_ordinary,
1525*a9fa9459Szrj 				      st_shndx);
1526*a9fa9459Szrj 	}
1527*a9fa9459Szrj       else
1528*a9fa9459Szrj 	{
1529*a9fa9459Szrj 	  // Read the version information.
1530*a9fa9459Szrj 
1531*a9fa9459Szrj 	  unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs);
1532*a9fa9459Szrj 
1533*a9fa9459Szrj 	  bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
1534*a9fa9459Szrj 	  v &= elfcpp::VERSYM_VERSION;
1535*a9fa9459Szrj 
1536*a9fa9459Szrj 	  // The Sun documentation says that V can be VER_NDX_LOCAL,
1537*a9fa9459Szrj 	  // or VER_NDX_GLOBAL, or a version index.  The meaning of
1538*a9fa9459Szrj 	  // VER_NDX_LOCAL is defined as "Symbol has local scope."
1539*a9fa9459Szrj 	  // The old GNU linker will happily generate VER_NDX_LOCAL
1540*a9fa9459Szrj 	  // for an undefined symbol.  I don't know what the Sun
1541*a9fa9459Szrj 	  // linker will generate.
1542*a9fa9459Szrj 
1543*a9fa9459Szrj 	  if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
1544*a9fa9459Szrj 	      && st_shndx != elfcpp::SHN_UNDEF)
1545*a9fa9459Szrj 	    {
1546*a9fa9459Szrj 	      // This symbol should not be visible outside the object.
1547*a9fa9459Szrj 	      continue;
1548*a9fa9459Szrj 	    }
1549*a9fa9459Szrj 
1550*a9fa9459Szrj 	  // At this point we are definitely going to add this symbol.
1551*a9fa9459Szrj 	  Stringpool::Key name_key;
1552*a9fa9459Szrj 	  name = this->namepool_.add(name, true, &name_key);
1553*a9fa9459Szrj 
1554*a9fa9459Szrj 	  if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
1555*a9fa9459Szrj 	      || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
1556*a9fa9459Szrj 	    {
1557*a9fa9459Szrj 	      // This symbol does not have a version.
1558*a9fa9459Szrj 	      res = this->add_from_object(dynobj, name, name_key, NULL, 0,
1559*a9fa9459Szrj 					  false, *psym, st_shndx, is_ordinary,
1560*a9fa9459Szrj 					  st_shndx);
1561*a9fa9459Szrj 	    }
1562*a9fa9459Szrj 	  else
1563*a9fa9459Szrj 	    {
1564*a9fa9459Szrj 	      if (v >= version_map->size())
1565*a9fa9459Szrj 		{
1566*a9fa9459Szrj 		  dynobj->error(_("versym for symbol %zu out of range: %u"),
1567*a9fa9459Szrj 				i, v);
1568*a9fa9459Szrj 		  continue;
1569*a9fa9459Szrj 		}
1570*a9fa9459Szrj 
1571*a9fa9459Szrj 	      const char* version = (*version_map)[v];
1572*a9fa9459Szrj 	      if (version == NULL)
1573*a9fa9459Szrj 		{
1574*a9fa9459Szrj 		  dynobj->error(_("versym for symbol %zu has no name: %u"),
1575*a9fa9459Szrj 				i, v);
1576*a9fa9459Szrj 		  continue;
1577*a9fa9459Szrj 		}
1578*a9fa9459Szrj 
1579*a9fa9459Szrj 	      Stringpool::Key version_key;
1580*a9fa9459Szrj 	      version = this->namepool_.add(version, true, &version_key);
1581*a9fa9459Szrj 
1582*a9fa9459Szrj 	      // If this is an absolute symbol, and the version name
1583*a9fa9459Szrj 	      // and symbol name are the same, then this is the
1584*a9fa9459Szrj 	      // version definition symbol.  These symbols exist to
1585*a9fa9459Szrj 	      // support using -u to pull in particular versions.  We
1586*a9fa9459Szrj 	      // do not want to record a version for them.
1587*a9fa9459Szrj 	      if (st_shndx == elfcpp::SHN_ABS
1588*a9fa9459Szrj 		  && !is_ordinary
1589*a9fa9459Szrj 		  && name_key == version_key)
1590*a9fa9459Szrj 		res = this->add_from_object(dynobj, name, name_key, NULL, 0,
1591*a9fa9459Szrj 					    false, *psym, st_shndx, is_ordinary,
1592*a9fa9459Szrj 					    st_shndx);
1593*a9fa9459Szrj 	      else
1594*a9fa9459Szrj 		{
1595*a9fa9459Szrj 		  const bool is_default_version =
1596*a9fa9459Szrj 		    !hidden && st_shndx != elfcpp::SHN_UNDEF;
1597*a9fa9459Szrj 		  res = this->add_from_object(dynobj, name, name_key, version,
1598*a9fa9459Szrj 					      version_key, is_default_version,
1599*a9fa9459Szrj 					      *psym, st_shndx,
1600*a9fa9459Szrj 					      is_ordinary, st_shndx);
1601*a9fa9459Szrj 		}
1602*a9fa9459Szrj 	    }
1603*a9fa9459Szrj 	}
1604*a9fa9459Szrj 
1605*a9fa9459Szrj       // Note that it is possible that RES was overridden by an
1606*a9fa9459Szrj       // earlier object, in which case it can't be aliased here.
1607*a9fa9459Szrj       if (st_shndx != elfcpp::SHN_UNDEF
1608*a9fa9459Szrj 	  && is_ordinary
1609*a9fa9459Szrj 	  && psym->get_st_type() == elfcpp::STT_OBJECT
1610*a9fa9459Szrj 	  && res->source() == Symbol::FROM_OBJECT
1611*a9fa9459Szrj 	  && res->object() == dynobj)
1612*a9fa9459Szrj 	object_symbols.push_back(res);
1613*a9fa9459Szrj 
1614*a9fa9459Szrj       // If the symbol has protected visibility in the dynobj,
1615*a9fa9459Szrj       // mark it as such if it was not overridden.
1616*a9fa9459Szrj       if (res->source() == Symbol::FROM_OBJECT
1617*a9fa9459Szrj           && res->object() == dynobj
1618*a9fa9459Szrj           && sym.get_st_visibility() == elfcpp::STV_PROTECTED)
1619*a9fa9459Szrj         res->set_is_protected();
1620*a9fa9459Szrj 
1621*a9fa9459Szrj       if (sympointers != NULL)
1622*a9fa9459Szrj 	(*sympointers)[i] = res;
1623*a9fa9459Szrj     }
1624*a9fa9459Szrj 
1625*a9fa9459Szrj   this->record_weak_aliases(&object_symbols);
1626*a9fa9459Szrj }
1627*a9fa9459Szrj 
1628*a9fa9459Szrj // Add a symbol from a incremental object file.
1629*a9fa9459Szrj 
1630*a9fa9459Szrj template<int size, bool big_endian>
1631*a9fa9459Szrj Sized_symbol<size>*
add_from_incrobj(Object * obj,const char * name,const char * ver,elfcpp::Sym<size,big_endian> * sym)1632*a9fa9459Szrj Symbol_table::add_from_incrobj(
1633*a9fa9459Szrj     Object* obj,
1634*a9fa9459Szrj     const char* name,
1635*a9fa9459Szrj     const char* ver,
1636*a9fa9459Szrj     elfcpp::Sym<size, big_endian>* sym)
1637*a9fa9459Szrj {
1638*a9fa9459Szrj   unsigned int st_shndx = sym->get_st_shndx();
1639*a9fa9459Szrj   bool is_ordinary = st_shndx < elfcpp::SHN_LORESERVE;
1640*a9fa9459Szrj 
1641*a9fa9459Szrj   Stringpool::Key ver_key = 0;
1642*a9fa9459Szrj   bool is_default_version = false;
1643*a9fa9459Szrj   bool is_forced_local = false;
1644*a9fa9459Szrj 
1645*a9fa9459Szrj   Stringpool::Key name_key;
1646*a9fa9459Szrj   name = this->namepool_.add(name, true, &name_key);
1647*a9fa9459Szrj 
1648*a9fa9459Szrj   Sized_symbol<size>* res;
1649*a9fa9459Szrj   res = this->add_from_object(obj, name, name_key, ver, ver_key,
1650*a9fa9459Szrj 		              is_default_version, *sym, st_shndx,
1651*a9fa9459Szrj 			      is_ordinary, st_shndx);
1652*a9fa9459Szrj 
1653*a9fa9459Szrj   if (is_forced_local)
1654*a9fa9459Szrj     this->force_local(res);
1655*a9fa9459Szrj 
1656*a9fa9459Szrj   return res;
1657*a9fa9459Szrj }
1658*a9fa9459Szrj 
1659*a9fa9459Szrj // This is used to sort weak aliases.  We sort them first by section
1660*a9fa9459Szrj // index, then by offset, then by weak ahead of strong.
1661*a9fa9459Szrj 
1662*a9fa9459Szrj template<int size>
1663*a9fa9459Szrj class Weak_alias_sorter
1664*a9fa9459Szrj {
1665*a9fa9459Szrj  public:
1666*a9fa9459Szrj   bool operator()(const Sized_symbol<size>*, const Sized_symbol<size>*) const;
1667*a9fa9459Szrj };
1668*a9fa9459Szrj 
1669*a9fa9459Szrj template<int size>
1670*a9fa9459Szrj bool
operator ()(const Sized_symbol<size> * s1,const Sized_symbol<size> * s2) const1671*a9fa9459Szrj Weak_alias_sorter<size>::operator()(const Sized_symbol<size>* s1,
1672*a9fa9459Szrj 				    const Sized_symbol<size>* s2) const
1673*a9fa9459Szrj {
1674*a9fa9459Szrj   bool is_ordinary;
1675*a9fa9459Szrj   unsigned int s1_shndx = s1->shndx(&is_ordinary);
1676*a9fa9459Szrj   gold_assert(is_ordinary);
1677*a9fa9459Szrj   unsigned int s2_shndx = s2->shndx(&is_ordinary);
1678*a9fa9459Szrj   gold_assert(is_ordinary);
1679*a9fa9459Szrj   if (s1_shndx != s2_shndx)
1680*a9fa9459Szrj     return s1_shndx < s2_shndx;
1681*a9fa9459Szrj 
1682*a9fa9459Szrj   if (s1->value() != s2->value())
1683*a9fa9459Szrj     return s1->value() < s2->value();
1684*a9fa9459Szrj   if (s1->binding() != s2->binding())
1685*a9fa9459Szrj     {
1686*a9fa9459Szrj       if (s1->binding() == elfcpp::STB_WEAK)
1687*a9fa9459Szrj 	return true;
1688*a9fa9459Szrj       if (s2->binding() == elfcpp::STB_WEAK)
1689*a9fa9459Szrj 	return false;
1690*a9fa9459Szrj     }
1691*a9fa9459Szrj   return std::string(s1->name()) < std::string(s2->name());
1692*a9fa9459Szrj }
1693*a9fa9459Szrj 
1694*a9fa9459Szrj // SYMBOLS is a list of object symbols from a dynamic object.  Look
1695*a9fa9459Szrj // for any weak aliases, and record them so that if we add the weak
1696*a9fa9459Szrj // alias to the dynamic symbol table, we also add the corresponding
1697*a9fa9459Szrj // strong symbol.
1698*a9fa9459Szrj 
1699*a9fa9459Szrj template<int size>
1700*a9fa9459Szrj void
record_weak_aliases(std::vector<Sized_symbol<size> * > * symbols)1701*a9fa9459Szrj Symbol_table::record_weak_aliases(std::vector<Sized_symbol<size>*>* symbols)
1702*a9fa9459Szrj {
1703*a9fa9459Szrj   // Sort the vector by section index, then by offset, then by weak
1704*a9fa9459Szrj   // ahead of strong.
1705*a9fa9459Szrj   std::sort(symbols->begin(), symbols->end(), Weak_alias_sorter<size>());
1706*a9fa9459Szrj 
1707*a9fa9459Szrj   // Walk through the vector.  For each weak definition, record
1708*a9fa9459Szrj   // aliases.
1709*a9fa9459Szrj   for (typename std::vector<Sized_symbol<size>*>::const_iterator p =
1710*a9fa9459Szrj 	 symbols->begin();
1711*a9fa9459Szrj        p != symbols->end();
1712*a9fa9459Szrj        ++p)
1713*a9fa9459Szrj     {
1714*a9fa9459Szrj       if ((*p)->binding() != elfcpp::STB_WEAK)
1715*a9fa9459Szrj 	continue;
1716*a9fa9459Szrj 
1717*a9fa9459Szrj       // Build a circular list of weak aliases.  Each symbol points to
1718*a9fa9459Szrj       // the next one in the circular list.
1719*a9fa9459Szrj 
1720*a9fa9459Szrj       Sized_symbol<size>* from_sym = *p;
1721*a9fa9459Szrj       typename std::vector<Sized_symbol<size>*>::const_iterator q;
1722*a9fa9459Szrj       for (q = p + 1; q != symbols->end(); ++q)
1723*a9fa9459Szrj 	{
1724*a9fa9459Szrj 	  bool dummy;
1725*a9fa9459Szrj 	  if ((*q)->shndx(&dummy) != from_sym->shndx(&dummy)
1726*a9fa9459Szrj 	      || (*q)->value() != from_sym->value())
1727*a9fa9459Szrj 	    break;
1728*a9fa9459Szrj 
1729*a9fa9459Szrj 	  this->weak_aliases_[from_sym] = *q;
1730*a9fa9459Szrj 	  from_sym->set_has_alias();
1731*a9fa9459Szrj 	  from_sym = *q;
1732*a9fa9459Szrj 	}
1733*a9fa9459Szrj 
1734*a9fa9459Szrj       if (from_sym != *p)
1735*a9fa9459Szrj 	{
1736*a9fa9459Szrj 	  this->weak_aliases_[from_sym] = *p;
1737*a9fa9459Szrj 	  from_sym->set_has_alias();
1738*a9fa9459Szrj 	}
1739*a9fa9459Szrj 
1740*a9fa9459Szrj       p = q - 1;
1741*a9fa9459Szrj     }
1742*a9fa9459Szrj }
1743*a9fa9459Szrj 
1744*a9fa9459Szrj // Create and return a specially defined symbol.  If ONLY_IF_REF is
1745*a9fa9459Szrj // true, then only create the symbol if there is a reference to it.
1746*a9fa9459Szrj // If this does not return NULL, it sets *POLDSYM to the existing
1747*a9fa9459Szrj // symbol if there is one.  This sets *RESOLVE_OLDSYM if we should
1748*a9fa9459Szrj // resolve the newly created symbol to the old one.  This
1749*a9fa9459Szrj // canonicalizes *PNAME and *PVERSION.
1750*a9fa9459Szrj 
1751*a9fa9459Szrj template<int size, bool big_endian>
1752*a9fa9459Szrj Sized_symbol<size>*
define_special_symbol(const char ** pname,const char ** pversion,bool only_if_ref,Sized_symbol<size> ** poldsym,bool * resolve_oldsym)1753*a9fa9459Szrj Symbol_table::define_special_symbol(const char** pname, const char** pversion,
1754*a9fa9459Szrj 				    bool only_if_ref,
1755*a9fa9459Szrj                                     Sized_symbol<size>** poldsym,
1756*a9fa9459Szrj 				    bool* resolve_oldsym)
1757*a9fa9459Szrj {
1758*a9fa9459Szrj   *resolve_oldsym = false;
1759*a9fa9459Szrj   *poldsym = NULL;
1760*a9fa9459Szrj 
1761*a9fa9459Szrj   // If the caller didn't give us a version, see if we get one from
1762*a9fa9459Szrj   // the version script.
1763*a9fa9459Szrj   std::string v;
1764*a9fa9459Szrj   bool is_default_version = false;
1765*a9fa9459Szrj   if (*pversion == NULL)
1766*a9fa9459Szrj     {
1767*a9fa9459Szrj       bool is_global;
1768*a9fa9459Szrj       if (this->version_script_.get_symbol_version(*pname, &v, &is_global))
1769*a9fa9459Szrj 	{
1770*a9fa9459Szrj 	  if (is_global && !v.empty())
1771*a9fa9459Szrj 	    {
1772*a9fa9459Szrj 	      *pversion = v.c_str();
1773*a9fa9459Szrj 	      // If we get the version from a version script, then we
1774*a9fa9459Szrj 	      // are also the default version.
1775*a9fa9459Szrj 	      is_default_version = true;
1776*a9fa9459Szrj 	    }
1777*a9fa9459Szrj 	}
1778*a9fa9459Szrj     }
1779*a9fa9459Szrj 
1780*a9fa9459Szrj   Symbol* oldsym;
1781*a9fa9459Szrj   Sized_symbol<size>* sym;
1782*a9fa9459Szrj 
1783*a9fa9459Szrj   bool add_to_table = false;
1784*a9fa9459Szrj   typename Symbol_table_type::iterator add_loc = this->table_.end();
1785*a9fa9459Szrj   bool add_def_to_table = false;
1786*a9fa9459Szrj   typename Symbol_table_type::iterator add_def_loc = this->table_.end();
1787*a9fa9459Szrj 
1788*a9fa9459Szrj   if (only_if_ref)
1789*a9fa9459Szrj     {
1790*a9fa9459Szrj       oldsym = this->lookup(*pname, *pversion);
1791*a9fa9459Szrj       if (oldsym == NULL && is_default_version)
1792*a9fa9459Szrj 	oldsym = this->lookup(*pname, NULL);
1793*a9fa9459Szrj       if (oldsym == NULL || !oldsym->is_undefined())
1794*a9fa9459Szrj 	return NULL;
1795*a9fa9459Szrj 
1796*a9fa9459Szrj       *pname = oldsym->name();
1797*a9fa9459Szrj       if (is_default_version)
1798*a9fa9459Szrj 	*pversion = this->namepool_.add(*pversion, true, NULL);
1799*a9fa9459Szrj       else
1800*a9fa9459Szrj 	*pversion = oldsym->version();
1801*a9fa9459Szrj     }
1802*a9fa9459Szrj   else
1803*a9fa9459Szrj     {
1804*a9fa9459Szrj       // Canonicalize NAME and VERSION.
1805*a9fa9459Szrj       Stringpool::Key name_key;
1806*a9fa9459Szrj       *pname = this->namepool_.add(*pname, true, &name_key);
1807*a9fa9459Szrj 
1808*a9fa9459Szrj       Stringpool::Key version_key = 0;
1809*a9fa9459Szrj       if (*pversion != NULL)
1810*a9fa9459Szrj 	*pversion = this->namepool_.add(*pversion, true, &version_key);
1811*a9fa9459Szrj 
1812*a9fa9459Szrj       Symbol* const snull = NULL;
1813*a9fa9459Szrj       std::pair<typename Symbol_table_type::iterator, bool> ins =
1814*a9fa9459Szrj 	this->table_.insert(std::make_pair(std::make_pair(name_key,
1815*a9fa9459Szrj 							  version_key),
1816*a9fa9459Szrj 					   snull));
1817*a9fa9459Szrj 
1818*a9fa9459Szrj       std::pair<typename Symbol_table_type::iterator, bool> insdefault =
1819*a9fa9459Szrj 	std::make_pair(this->table_.end(), false);
1820*a9fa9459Szrj       if (is_default_version)
1821*a9fa9459Szrj 	{
1822*a9fa9459Szrj 	  const Stringpool::Key vnull = 0;
1823*a9fa9459Szrj 	  insdefault =
1824*a9fa9459Szrj 	    this->table_.insert(std::make_pair(std::make_pair(name_key,
1825*a9fa9459Szrj 							      vnull),
1826*a9fa9459Szrj 					       snull));
1827*a9fa9459Szrj 	}
1828*a9fa9459Szrj 
1829*a9fa9459Szrj       if (!ins.second)
1830*a9fa9459Szrj 	{
1831*a9fa9459Szrj 	  // We already have a symbol table entry for NAME/VERSION.
1832*a9fa9459Szrj 	  oldsym = ins.first->second;
1833*a9fa9459Szrj 	  gold_assert(oldsym != NULL);
1834*a9fa9459Szrj 
1835*a9fa9459Szrj 	  if (is_default_version)
1836*a9fa9459Szrj 	    {
1837*a9fa9459Szrj 	      Sized_symbol<size>* soldsym =
1838*a9fa9459Szrj 		this->get_sized_symbol<size>(oldsym);
1839*a9fa9459Szrj 	      this->define_default_version<size, big_endian>(soldsym,
1840*a9fa9459Szrj 							     insdefault.second,
1841*a9fa9459Szrj 							     insdefault.first);
1842*a9fa9459Szrj 	    }
1843*a9fa9459Szrj 	}
1844*a9fa9459Szrj       else
1845*a9fa9459Szrj 	{
1846*a9fa9459Szrj 	  // We haven't seen this symbol before.
1847*a9fa9459Szrj 	  gold_assert(ins.first->second == NULL);
1848*a9fa9459Szrj 
1849*a9fa9459Szrj 	  add_to_table = true;
1850*a9fa9459Szrj 	  add_loc = ins.first;
1851*a9fa9459Szrj 
1852*a9fa9459Szrj 	  if (is_default_version && !insdefault.second)
1853*a9fa9459Szrj 	    {
1854*a9fa9459Szrj 	      // We are adding NAME/VERSION, and it is the default
1855*a9fa9459Szrj 	      // version.  We already have an entry for NAME/NULL.
1856*a9fa9459Szrj 	      oldsym = insdefault.first->second;
1857*a9fa9459Szrj 	      *resolve_oldsym = true;
1858*a9fa9459Szrj 	    }
1859*a9fa9459Szrj 	  else
1860*a9fa9459Szrj 	    {
1861*a9fa9459Szrj 	      oldsym = NULL;
1862*a9fa9459Szrj 
1863*a9fa9459Szrj 	      if (is_default_version)
1864*a9fa9459Szrj 		{
1865*a9fa9459Szrj 		  add_def_to_table = true;
1866*a9fa9459Szrj 		  add_def_loc = insdefault.first;
1867*a9fa9459Szrj 		}
1868*a9fa9459Szrj 	    }
1869*a9fa9459Szrj 	}
1870*a9fa9459Szrj     }
1871*a9fa9459Szrj 
1872*a9fa9459Szrj   const Target& target = parameters->target();
1873*a9fa9459Szrj   if (!target.has_make_symbol())
1874*a9fa9459Szrj     sym = new Sized_symbol<size>();
1875*a9fa9459Szrj   else
1876*a9fa9459Szrj     {
1877*a9fa9459Szrj       Sized_target<size, big_endian>* sized_target =
1878*a9fa9459Szrj 	parameters->sized_target<size, big_endian>();
1879*a9fa9459Szrj       sym = sized_target->make_symbol(*pname, elfcpp::STT_NOTYPE,
1880*a9fa9459Szrj 				      NULL, elfcpp::SHN_UNDEF, 0);
1881*a9fa9459Szrj       if (sym == NULL)
1882*a9fa9459Szrj         return NULL;
1883*a9fa9459Szrj     }
1884*a9fa9459Szrj 
1885*a9fa9459Szrj   if (add_to_table)
1886*a9fa9459Szrj     add_loc->second = sym;
1887*a9fa9459Szrj   else
1888*a9fa9459Szrj     gold_assert(oldsym != NULL);
1889*a9fa9459Szrj 
1890*a9fa9459Szrj   if (add_def_to_table)
1891*a9fa9459Szrj     add_def_loc->second = sym;
1892*a9fa9459Szrj 
1893*a9fa9459Szrj   *poldsym = this->get_sized_symbol<size>(oldsym);
1894*a9fa9459Szrj 
1895*a9fa9459Szrj   return sym;
1896*a9fa9459Szrj }
1897*a9fa9459Szrj 
1898*a9fa9459Szrj // Define a symbol based on an Output_data.
1899*a9fa9459Szrj 
1900*a9fa9459Szrj Symbol*
define_in_output_data(const char * name,const char * version,Defined defined,Output_data * od,uint64_t value,uint64_t symsize,elfcpp::STT type,elfcpp::STB binding,elfcpp::STV visibility,unsigned char nonvis,bool offset_is_from_end,bool only_if_ref)1901*a9fa9459Szrj Symbol_table::define_in_output_data(const char* name,
1902*a9fa9459Szrj 				    const char* version,
1903*a9fa9459Szrj 				    Defined defined,
1904*a9fa9459Szrj 				    Output_data* od,
1905*a9fa9459Szrj 				    uint64_t value,
1906*a9fa9459Szrj 				    uint64_t symsize,
1907*a9fa9459Szrj 				    elfcpp::STT type,
1908*a9fa9459Szrj 				    elfcpp::STB binding,
1909*a9fa9459Szrj 				    elfcpp::STV visibility,
1910*a9fa9459Szrj 				    unsigned char nonvis,
1911*a9fa9459Szrj 				    bool offset_is_from_end,
1912*a9fa9459Szrj 				    bool only_if_ref)
1913*a9fa9459Szrj {
1914*a9fa9459Szrj   if (parameters->target().get_size() == 32)
1915*a9fa9459Szrj     {
1916*a9fa9459Szrj #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1917*a9fa9459Szrj       return this->do_define_in_output_data<32>(name, version, defined, od,
1918*a9fa9459Szrj                                                 value, symsize, type, binding,
1919*a9fa9459Szrj                                                 visibility, nonvis,
1920*a9fa9459Szrj                                                 offset_is_from_end,
1921*a9fa9459Szrj                                                 only_if_ref);
1922*a9fa9459Szrj #else
1923*a9fa9459Szrj       gold_unreachable();
1924*a9fa9459Szrj #endif
1925*a9fa9459Szrj     }
1926*a9fa9459Szrj   else if (parameters->target().get_size() == 64)
1927*a9fa9459Szrj     {
1928*a9fa9459Szrj #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1929*a9fa9459Szrj       return this->do_define_in_output_data<64>(name, version, defined, od,
1930*a9fa9459Szrj                                                 value, symsize, type, binding,
1931*a9fa9459Szrj                                                 visibility, nonvis,
1932*a9fa9459Szrj                                                 offset_is_from_end,
1933*a9fa9459Szrj                                                 only_if_ref);
1934*a9fa9459Szrj #else
1935*a9fa9459Szrj       gold_unreachable();
1936*a9fa9459Szrj #endif
1937*a9fa9459Szrj     }
1938*a9fa9459Szrj   else
1939*a9fa9459Szrj     gold_unreachable();
1940*a9fa9459Szrj }
1941*a9fa9459Szrj 
1942*a9fa9459Szrj // Define a symbol in an Output_data, sized version.
1943*a9fa9459Szrj 
1944*a9fa9459Szrj template<int size>
1945*a9fa9459Szrj Sized_symbol<size>*
do_define_in_output_data(const char * name,const char * version,Defined defined,Output_data * od,typename elfcpp::Elf_types<size>::Elf_Addr value,typename elfcpp::Elf_types<size>::Elf_WXword symsize,elfcpp::STT type,elfcpp::STB binding,elfcpp::STV visibility,unsigned char nonvis,bool offset_is_from_end,bool only_if_ref)1946*a9fa9459Szrj Symbol_table::do_define_in_output_data(
1947*a9fa9459Szrj     const char* name,
1948*a9fa9459Szrj     const char* version,
1949*a9fa9459Szrj     Defined defined,
1950*a9fa9459Szrj     Output_data* od,
1951*a9fa9459Szrj     typename elfcpp::Elf_types<size>::Elf_Addr value,
1952*a9fa9459Szrj     typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1953*a9fa9459Szrj     elfcpp::STT type,
1954*a9fa9459Szrj     elfcpp::STB binding,
1955*a9fa9459Szrj     elfcpp::STV visibility,
1956*a9fa9459Szrj     unsigned char nonvis,
1957*a9fa9459Szrj     bool offset_is_from_end,
1958*a9fa9459Szrj     bool only_if_ref)
1959*a9fa9459Szrj {
1960*a9fa9459Szrj   Sized_symbol<size>* sym;
1961*a9fa9459Szrj   Sized_symbol<size>* oldsym;
1962*a9fa9459Szrj   bool resolve_oldsym;
1963*a9fa9459Szrj 
1964*a9fa9459Szrj   if (parameters->target().is_big_endian())
1965*a9fa9459Szrj     {
1966*a9fa9459Szrj #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1967*a9fa9459Szrj       sym = this->define_special_symbol<size, true>(&name, &version,
1968*a9fa9459Szrj 						    only_if_ref, &oldsym,
1969*a9fa9459Szrj 						    &resolve_oldsym);
1970*a9fa9459Szrj #else
1971*a9fa9459Szrj       gold_unreachable();
1972*a9fa9459Szrj #endif
1973*a9fa9459Szrj     }
1974*a9fa9459Szrj   else
1975*a9fa9459Szrj     {
1976*a9fa9459Szrj #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1977*a9fa9459Szrj       sym = this->define_special_symbol<size, false>(&name, &version,
1978*a9fa9459Szrj 						     only_if_ref, &oldsym,
1979*a9fa9459Szrj 						     &resolve_oldsym);
1980*a9fa9459Szrj #else
1981*a9fa9459Szrj       gold_unreachable();
1982*a9fa9459Szrj #endif
1983*a9fa9459Szrj     }
1984*a9fa9459Szrj 
1985*a9fa9459Szrj   if (sym == NULL)
1986*a9fa9459Szrj     return NULL;
1987*a9fa9459Szrj 
1988*a9fa9459Szrj   sym->init_output_data(name, version, od, value, symsize, type, binding,
1989*a9fa9459Szrj 			visibility, nonvis, offset_is_from_end,
1990*a9fa9459Szrj 			defined == PREDEFINED);
1991*a9fa9459Szrj 
1992*a9fa9459Szrj   if (oldsym == NULL)
1993*a9fa9459Szrj     {
1994*a9fa9459Szrj       if (binding == elfcpp::STB_LOCAL
1995*a9fa9459Szrj 	  || this->version_script_.symbol_is_local(name))
1996*a9fa9459Szrj 	this->force_local(sym);
1997*a9fa9459Szrj       else if (version != NULL)
1998*a9fa9459Szrj 	sym->set_is_default();
1999*a9fa9459Szrj       return sym;
2000*a9fa9459Szrj     }
2001*a9fa9459Szrj 
2002*a9fa9459Szrj   if (Symbol_table::should_override_with_special(oldsym, type, defined))
2003*a9fa9459Szrj     this->override_with_special(oldsym, sym);
2004*a9fa9459Szrj 
2005*a9fa9459Szrj   if (resolve_oldsym)
2006*a9fa9459Szrj     return sym;
2007*a9fa9459Szrj   else
2008*a9fa9459Szrj     {
2009*a9fa9459Szrj       if (defined == PREDEFINED
2010*a9fa9459Szrj 	  && (binding == elfcpp::STB_LOCAL
2011*a9fa9459Szrj 	      || this->version_script_.symbol_is_local(name)))
2012*a9fa9459Szrj 	this->force_local(oldsym);
2013*a9fa9459Szrj       delete sym;
2014*a9fa9459Szrj       return oldsym;
2015*a9fa9459Szrj     }
2016*a9fa9459Szrj }
2017*a9fa9459Szrj 
2018*a9fa9459Szrj // Define a symbol based on an Output_segment.
2019*a9fa9459Szrj 
2020*a9fa9459Szrj Symbol*
define_in_output_segment(const char * name,const char * version,Defined defined,Output_segment * os,uint64_t value,uint64_t symsize,elfcpp::STT type,elfcpp::STB binding,elfcpp::STV visibility,unsigned char nonvis,Symbol::Segment_offset_base offset_base,bool only_if_ref)2021*a9fa9459Szrj Symbol_table::define_in_output_segment(const char* name,
2022*a9fa9459Szrj 				       const char* version,
2023*a9fa9459Szrj 				       Defined defined,
2024*a9fa9459Szrj 				       Output_segment* os,
2025*a9fa9459Szrj 				       uint64_t value,
2026*a9fa9459Szrj 				       uint64_t symsize,
2027*a9fa9459Szrj 				       elfcpp::STT type,
2028*a9fa9459Szrj 				       elfcpp::STB binding,
2029*a9fa9459Szrj 				       elfcpp::STV visibility,
2030*a9fa9459Szrj 				       unsigned char nonvis,
2031*a9fa9459Szrj 				       Symbol::Segment_offset_base offset_base,
2032*a9fa9459Szrj 				       bool only_if_ref)
2033*a9fa9459Szrj {
2034*a9fa9459Szrj   if (parameters->target().get_size() == 32)
2035*a9fa9459Szrj     {
2036*a9fa9459Szrj #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2037*a9fa9459Szrj       return this->do_define_in_output_segment<32>(name, version, defined, os,
2038*a9fa9459Szrj                                                    value, symsize, type,
2039*a9fa9459Szrj                                                    binding, visibility, nonvis,
2040*a9fa9459Szrj                                                    offset_base, only_if_ref);
2041*a9fa9459Szrj #else
2042*a9fa9459Szrj       gold_unreachable();
2043*a9fa9459Szrj #endif
2044*a9fa9459Szrj     }
2045*a9fa9459Szrj   else if (parameters->target().get_size() == 64)
2046*a9fa9459Szrj     {
2047*a9fa9459Szrj #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2048*a9fa9459Szrj       return this->do_define_in_output_segment<64>(name, version, defined, os,
2049*a9fa9459Szrj                                                    value, symsize, type,
2050*a9fa9459Szrj                                                    binding, visibility, nonvis,
2051*a9fa9459Szrj                                                    offset_base, only_if_ref);
2052*a9fa9459Szrj #else
2053*a9fa9459Szrj       gold_unreachable();
2054*a9fa9459Szrj #endif
2055*a9fa9459Szrj     }
2056*a9fa9459Szrj   else
2057*a9fa9459Szrj     gold_unreachable();
2058*a9fa9459Szrj }
2059*a9fa9459Szrj 
2060*a9fa9459Szrj // Define a symbol in an Output_segment, sized version.
2061*a9fa9459Szrj 
2062*a9fa9459Szrj template<int size>
2063*a9fa9459Szrj Sized_symbol<size>*
do_define_in_output_segment(const char * name,const char * version,Defined defined,Output_segment * os,typename elfcpp::Elf_types<size>::Elf_Addr value,typename elfcpp::Elf_types<size>::Elf_WXword symsize,elfcpp::STT type,elfcpp::STB binding,elfcpp::STV visibility,unsigned char nonvis,Symbol::Segment_offset_base offset_base,bool only_if_ref)2064*a9fa9459Szrj Symbol_table::do_define_in_output_segment(
2065*a9fa9459Szrj     const char* name,
2066*a9fa9459Szrj     const char* version,
2067*a9fa9459Szrj     Defined defined,
2068*a9fa9459Szrj     Output_segment* os,
2069*a9fa9459Szrj     typename elfcpp::Elf_types<size>::Elf_Addr value,
2070*a9fa9459Szrj     typename elfcpp::Elf_types<size>::Elf_WXword symsize,
2071*a9fa9459Szrj     elfcpp::STT type,
2072*a9fa9459Szrj     elfcpp::STB binding,
2073*a9fa9459Szrj     elfcpp::STV visibility,
2074*a9fa9459Szrj     unsigned char nonvis,
2075*a9fa9459Szrj     Symbol::Segment_offset_base offset_base,
2076*a9fa9459Szrj     bool only_if_ref)
2077*a9fa9459Szrj {
2078*a9fa9459Szrj   Sized_symbol<size>* sym;
2079*a9fa9459Szrj   Sized_symbol<size>* oldsym;
2080*a9fa9459Szrj   bool resolve_oldsym;
2081*a9fa9459Szrj 
2082*a9fa9459Szrj   if (parameters->target().is_big_endian())
2083*a9fa9459Szrj     {
2084*a9fa9459Szrj #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
2085*a9fa9459Szrj       sym = this->define_special_symbol<size, true>(&name, &version,
2086*a9fa9459Szrj 						    only_if_ref, &oldsym,
2087*a9fa9459Szrj 						    &resolve_oldsym);
2088*a9fa9459Szrj #else
2089*a9fa9459Szrj       gold_unreachable();
2090*a9fa9459Szrj #endif
2091*a9fa9459Szrj     }
2092*a9fa9459Szrj   else
2093*a9fa9459Szrj     {
2094*a9fa9459Szrj #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
2095*a9fa9459Szrj       sym = this->define_special_symbol<size, false>(&name, &version,
2096*a9fa9459Szrj 						     only_if_ref, &oldsym,
2097*a9fa9459Szrj 						     &resolve_oldsym);
2098*a9fa9459Szrj #else
2099*a9fa9459Szrj       gold_unreachable();
2100*a9fa9459Szrj #endif
2101*a9fa9459Szrj     }
2102*a9fa9459Szrj 
2103*a9fa9459Szrj   if (sym == NULL)
2104*a9fa9459Szrj     return NULL;
2105*a9fa9459Szrj 
2106*a9fa9459Szrj   sym->init_output_segment(name, version, os, value, symsize, type, binding,
2107*a9fa9459Szrj 			   visibility, nonvis, offset_base,
2108*a9fa9459Szrj 			   defined == PREDEFINED);
2109*a9fa9459Szrj 
2110*a9fa9459Szrj   if (oldsym == NULL)
2111*a9fa9459Szrj     {
2112*a9fa9459Szrj       if (binding == elfcpp::STB_LOCAL
2113*a9fa9459Szrj 	  || this->version_script_.symbol_is_local(name))
2114*a9fa9459Szrj 	this->force_local(sym);
2115*a9fa9459Szrj       else if (version != NULL)
2116*a9fa9459Szrj 	sym->set_is_default();
2117*a9fa9459Szrj       return sym;
2118*a9fa9459Szrj     }
2119*a9fa9459Szrj 
2120*a9fa9459Szrj   if (Symbol_table::should_override_with_special(oldsym, type, defined))
2121*a9fa9459Szrj     this->override_with_special(oldsym, sym);
2122*a9fa9459Szrj 
2123*a9fa9459Szrj   if (resolve_oldsym)
2124*a9fa9459Szrj     return sym;
2125*a9fa9459Szrj   else
2126*a9fa9459Szrj     {
2127*a9fa9459Szrj       if (binding == elfcpp::STB_LOCAL
2128*a9fa9459Szrj 	  || this->version_script_.symbol_is_local(name))
2129*a9fa9459Szrj 	this->force_local(oldsym);
2130*a9fa9459Szrj       delete sym;
2131*a9fa9459Szrj       return oldsym;
2132*a9fa9459Szrj     }
2133*a9fa9459Szrj }
2134*a9fa9459Szrj 
2135*a9fa9459Szrj // Define a special symbol with a constant value.  It is a multiple
2136*a9fa9459Szrj // definition error if this symbol is already defined.
2137*a9fa9459Szrj 
2138*a9fa9459Szrj Symbol*
define_as_constant(const char * name,const char * version,Defined defined,uint64_t value,uint64_t symsize,elfcpp::STT type,elfcpp::STB binding,elfcpp::STV visibility,unsigned char nonvis,bool only_if_ref,bool force_override)2139*a9fa9459Szrj Symbol_table::define_as_constant(const char* name,
2140*a9fa9459Szrj 				 const char* version,
2141*a9fa9459Szrj 				 Defined defined,
2142*a9fa9459Szrj 				 uint64_t value,
2143*a9fa9459Szrj 				 uint64_t symsize,
2144*a9fa9459Szrj 				 elfcpp::STT type,
2145*a9fa9459Szrj 				 elfcpp::STB binding,
2146*a9fa9459Szrj 				 elfcpp::STV visibility,
2147*a9fa9459Szrj 				 unsigned char nonvis,
2148*a9fa9459Szrj 				 bool only_if_ref,
2149*a9fa9459Szrj                                  bool force_override)
2150*a9fa9459Szrj {
2151*a9fa9459Szrj   if (parameters->target().get_size() == 32)
2152*a9fa9459Szrj     {
2153*a9fa9459Szrj #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2154*a9fa9459Szrj       return this->do_define_as_constant<32>(name, version, defined, value,
2155*a9fa9459Szrj                                              symsize, type, binding,
2156*a9fa9459Szrj                                              visibility, nonvis, only_if_ref,
2157*a9fa9459Szrj                                              force_override);
2158*a9fa9459Szrj #else
2159*a9fa9459Szrj       gold_unreachable();
2160*a9fa9459Szrj #endif
2161*a9fa9459Szrj     }
2162*a9fa9459Szrj   else if (parameters->target().get_size() == 64)
2163*a9fa9459Szrj     {
2164*a9fa9459Szrj #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2165*a9fa9459Szrj       return this->do_define_as_constant<64>(name, version, defined, value,
2166*a9fa9459Szrj                                              symsize, type, binding,
2167*a9fa9459Szrj                                              visibility, nonvis, only_if_ref,
2168*a9fa9459Szrj                                              force_override);
2169*a9fa9459Szrj #else
2170*a9fa9459Szrj       gold_unreachable();
2171*a9fa9459Szrj #endif
2172*a9fa9459Szrj     }
2173*a9fa9459Szrj   else
2174*a9fa9459Szrj     gold_unreachable();
2175*a9fa9459Szrj }
2176*a9fa9459Szrj 
2177*a9fa9459Szrj // Define a symbol as a constant, sized version.
2178*a9fa9459Szrj 
2179*a9fa9459Szrj template<int size>
2180*a9fa9459Szrj Sized_symbol<size>*
do_define_as_constant(const char * name,const char * version,Defined defined,typename elfcpp::Elf_types<size>::Elf_Addr value,typename elfcpp::Elf_types<size>::Elf_WXword symsize,elfcpp::STT type,elfcpp::STB binding,elfcpp::STV visibility,unsigned char nonvis,bool only_if_ref,bool force_override)2181*a9fa9459Szrj Symbol_table::do_define_as_constant(
2182*a9fa9459Szrj     const char* name,
2183*a9fa9459Szrj     const char* version,
2184*a9fa9459Szrj     Defined defined,
2185*a9fa9459Szrj     typename elfcpp::Elf_types<size>::Elf_Addr value,
2186*a9fa9459Szrj     typename elfcpp::Elf_types<size>::Elf_WXword symsize,
2187*a9fa9459Szrj     elfcpp::STT type,
2188*a9fa9459Szrj     elfcpp::STB binding,
2189*a9fa9459Szrj     elfcpp::STV visibility,
2190*a9fa9459Szrj     unsigned char nonvis,
2191*a9fa9459Szrj     bool only_if_ref,
2192*a9fa9459Szrj     bool force_override)
2193*a9fa9459Szrj {
2194*a9fa9459Szrj   Sized_symbol<size>* sym;
2195*a9fa9459Szrj   Sized_symbol<size>* oldsym;
2196*a9fa9459Szrj   bool resolve_oldsym;
2197*a9fa9459Szrj 
2198*a9fa9459Szrj   if (parameters->target().is_big_endian())
2199*a9fa9459Szrj     {
2200*a9fa9459Szrj #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
2201*a9fa9459Szrj       sym = this->define_special_symbol<size, true>(&name, &version,
2202*a9fa9459Szrj 						    only_if_ref, &oldsym,
2203*a9fa9459Szrj 						    &resolve_oldsym);
2204*a9fa9459Szrj #else
2205*a9fa9459Szrj       gold_unreachable();
2206*a9fa9459Szrj #endif
2207*a9fa9459Szrj     }
2208*a9fa9459Szrj   else
2209*a9fa9459Szrj     {
2210*a9fa9459Szrj #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
2211*a9fa9459Szrj       sym = this->define_special_symbol<size, false>(&name, &version,
2212*a9fa9459Szrj 						     only_if_ref, &oldsym,
2213*a9fa9459Szrj 						     &resolve_oldsym);
2214*a9fa9459Szrj #else
2215*a9fa9459Szrj       gold_unreachable();
2216*a9fa9459Szrj #endif
2217*a9fa9459Szrj     }
2218*a9fa9459Szrj 
2219*a9fa9459Szrj   if (sym == NULL)
2220*a9fa9459Szrj     return NULL;
2221*a9fa9459Szrj 
2222*a9fa9459Szrj   sym->init_constant(name, version, value, symsize, type, binding, visibility,
2223*a9fa9459Szrj 		     nonvis, defined == PREDEFINED);
2224*a9fa9459Szrj 
2225*a9fa9459Szrj   if (oldsym == NULL)
2226*a9fa9459Szrj     {
2227*a9fa9459Szrj       // Version symbols are absolute symbols with name == version.
2228*a9fa9459Szrj       // We don't want to force them to be local.
2229*a9fa9459Szrj       if ((version == NULL
2230*a9fa9459Szrj 	   || name != version
2231*a9fa9459Szrj 	   || value != 0)
2232*a9fa9459Szrj 	  && (binding == elfcpp::STB_LOCAL
2233*a9fa9459Szrj 	      || this->version_script_.symbol_is_local(name)))
2234*a9fa9459Szrj 	this->force_local(sym);
2235*a9fa9459Szrj       else if (version != NULL
2236*a9fa9459Szrj 	       && (name != version || value != 0))
2237*a9fa9459Szrj 	sym->set_is_default();
2238*a9fa9459Szrj       return sym;
2239*a9fa9459Szrj     }
2240*a9fa9459Szrj 
2241*a9fa9459Szrj   if (force_override
2242*a9fa9459Szrj       || Symbol_table::should_override_with_special(oldsym, type, defined))
2243*a9fa9459Szrj     this->override_with_special(oldsym, sym);
2244*a9fa9459Szrj 
2245*a9fa9459Szrj   if (resolve_oldsym)
2246*a9fa9459Szrj     return sym;
2247*a9fa9459Szrj   else
2248*a9fa9459Szrj     {
2249*a9fa9459Szrj       if (binding == elfcpp::STB_LOCAL
2250*a9fa9459Szrj 	  || this->version_script_.symbol_is_local(name))
2251*a9fa9459Szrj 	this->force_local(oldsym);
2252*a9fa9459Szrj       delete sym;
2253*a9fa9459Szrj       return oldsym;
2254*a9fa9459Szrj     }
2255*a9fa9459Szrj }
2256*a9fa9459Szrj 
2257*a9fa9459Szrj // Define a set of symbols in output sections.
2258*a9fa9459Szrj 
2259*a9fa9459Szrj void
define_symbols(const Layout * layout,int count,const Define_symbol_in_section * p,bool only_if_ref)2260*a9fa9459Szrj Symbol_table::define_symbols(const Layout* layout, int count,
2261*a9fa9459Szrj 			     const Define_symbol_in_section* p,
2262*a9fa9459Szrj 			     bool only_if_ref)
2263*a9fa9459Szrj {
2264*a9fa9459Szrj   for (int i = 0; i < count; ++i, ++p)
2265*a9fa9459Szrj     {
2266*a9fa9459Szrj       Output_section* os = layout->find_output_section(p->output_section);
2267*a9fa9459Szrj       if (os != NULL)
2268*a9fa9459Szrj 	this->define_in_output_data(p->name, NULL, PREDEFINED, os, p->value,
2269*a9fa9459Szrj 				    p->size, p->type, p->binding,
2270*a9fa9459Szrj 				    p->visibility, p->nonvis,
2271*a9fa9459Szrj 				    p->offset_is_from_end,
2272*a9fa9459Szrj 				    only_if_ref || p->only_if_ref);
2273*a9fa9459Szrj       else
2274*a9fa9459Szrj 	this->define_as_constant(p->name, NULL, PREDEFINED, 0, p->size,
2275*a9fa9459Szrj 				 p->type, p->binding, p->visibility, p->nonvis,
2276*a9fa9459Szrj 				 only_if_ref || p->only_if_ref,
2277*a9fa9459Szrj                                  false);
2278*a9fa9459Szrj     }
2279*a9fa9459Szrj }
2280*a9fa9459Szrj 
2281*a9fa9459Szrj // Define a set of symbols in output segments.
2282*a9fa9459Szrj 
2283*a9fa9459Szrj void
define_symbols(const Layout * layout,int count,const Define_symbol_in_segment * p,bool only_if_ref)2284*a9fa9459Szrj Symbol_table::define_symbols(const Layout* layout, int count,
2285*a9fa9459Szrj 			     const Define_symbol_in_segment* p,
2286*a9fa9459Szrj 			     bool only_if_ref)
2287*a9fa9459Szrj {
2288*a9fa9459Szrj   for (int i = 0; i < count; ++i, ++p)
2289*a9fa9459Szrj     {
2290*a9fa9459Szrj       Output_segment* os = layout->find_output_segment(p->segment_type,
2291*a9fa9459Szrj 						       p->segment_flags_set,
2292*a9fa9459Szrj 						       p->segment_flags_clear);
2293*a9fa9459Szrj       if (os != NULL)
2294*a9fa9459Szrj 	this->define_in_output_segment(p->name, NULL, PREDEFINED, os, p->value,
2295*a9fa9459Szrj 				       p->size, p->type, p->binding,
2296*a9fa9459Szrj 				       p->visibility, p->nonvis,
2297*a9fa9459Szrj 				       p->offset_base,
2298*a9fa9459Szrj 				       only_if_ref || p->only_if_ref);
2299*a9fa9459Szrj       else
2300*a9fa9459Szrj 	this->define_as_constant(p->name, NULL, PREDEFINED, 0, p->size,
2301*a9fa9459Szrj 				 p->type, p->binding, p->visibility, p->nonvis,
2302*a9fa9459Szrj 				 only_if_ref || p->only_if_ref,
2303*a9fa9459Szrj                                  false);
2304*a9fa9459Szrj     }
2305*a9fa9459Szrj }
2306*a9fa9459Szrj 
2307*a9fa9459Szrj // Define CSYM using a COPY reloc.  POSD is the Output_data where the
2308*a9fa9459Szrj // symbol should be defined--typically a .dyn.bss section.  VALUE is
2309*a9fa9459Szrj // the offset within POSD.
2310*a9fa9459Szrj 
2311*a9fa9459Szrj template<int size>
2312*a9fa9459Szrj void
define_with_copy_reloc(Sized_symbol<size> * csym,Output_data * posd,typename elfcpp::Elf_types<size>::Elf_Addr value)2313*a9fa9459Szrj Symbol_table::define_with_copy_reloc(
2314*a9fa9459Szrj     Sized_symbol<size>* csym,
2315*a9fa9459Szrj     Output_data* posd,
2316*a9fa9459Szrj     typename elfcpp::Elf_types<size>::Elf_Addr value)
2317*a9fa9459Szrj {
2318*a9fa9459Szrj   gold_assert(csym->is_from_dynobj());
2319*a9fa9459Szrj   gold_assert(!csym->is_copied_from_dynobj());
2320*a9fa9459Szrj   Object* object = csym->object();
2321*a9fa9459Szrj   gold_assert(object->is_dynamic());
2322*a9fa9459Szrj   Dynobj* dynobj = static_cast<Dynobj*>(object);
2323*a9fa9459Szrj 
2324*a9fa9459Szrj   // Our copied variable has to override any variable in a shared
2325*a9fa9459Szrj   // library.
2326*a9fa9459Szrj   elfcpp::STB binding = csym->binding();
2327*a9fa9459Szrj   if (binding == elfcpp::STB_WEAK)
2328*a9fa9459Szrj     binding = elfcpp::STB_GLOBAL;
2329*a9fa9459Szrj 
2330*a9fa9459Szrj   this->define_in_output_data(csym->name(), csym->version(), COPY,
2331*a9fa9459Szrj 			      posd, value, csym->symsize(),
2332*a9fa9459Szrj 			      csym->type(), binding,
2333*a9fa9459Szrj 			      csym->visibility(), csym->nonvis(),
2334*a9fa9459Szrj 			      false, false);
2335*a9fa9459Szrj 
2336*a9fa9459Szrj   csym->set_is_copied_from_dynobj();
2337*a9fa9459Szrj   csym->set_needs_dynsym_entry();
2338*a9fa9459Szrj 
2339*a9fa9459Szrj   this->copied_symbol_dynobjs_[csym] = dynobj;
2340*a9fa9459Szrj 
2341*a9fa9459Szrj   // We have now defined all aliases, but we have not entered them all
2342*a9fa9459Szrj   // in the copied_symbol_dynobjs_ map.
2343*a9fa9459Szrj   if (csym->has_alias())
2344*a9fa9459Szrj     {
2345*a9fa9459Szrj       Symbol* sym = csym;
2346*a9fa9459Szrj       while (true)
2347*a9fa9459Szrj 	{
2348*a9fa9459Szrj 	  sym = this->weak_aliases_[sym];
2349*a9fa9459Szrj 	  if (sym == csym)
2350*a9fa9459Szrj 	    break;
2351*a9fa9459Szrj 	  gold_assert(sym->output_data() == posd);
2352*a9fa9459Szrj 
2353*a9fa9459Szrj 	  sym->set_is_copied_from_dynobj();
2354*a9fa9459Szrj 	  this->copied_symbol_dynobjs_[sym] = dynobj;
2355*a9fa9459Szrj 	}
2356*a9fa9459Szrj     }
2357*a9fa9459Szrj }
2358*a9fa9459Szrj 
2359*a9fa9459Szrj // SYM is defined using a COPY reloc.  Return the dynamic object where
2360*a9fa9459Szrj // the original definition was found.
2361*a9fa9459Szrj 
2362*a9fa9459Szrj Dynobj*
get_copy_source(const Symbol * sym) const2363*a9fa9459Szrj Symbol_table::get_copy_source(const Symbol* sym) const
2364*a9fa9459Szrj {
2365*a9fa9459Szrj   gold_assert(sym->is_copied_from_dynobj());
2366*a9fa9459Szrj   Copied_symbol_dynobjs::const_iterator p =
2367*a9fa9459Szrj     this->copied_symbol_dynobjs_.find(sym);
2368*a9fa9459Szrj   gold_assert(p != this->copied_symbol_dynobjs_.end());
2369*a9fa9459Szrj   return p->second;
2370*a9fa9459Szrj }
2371*a9fa9459Szrj 
2372*a9fa9459Szrj // Add any undefined symbols named on the command line.
2373*a9fa9459Szrj 
2374*a9fa9459Szrj void
add_undefined_symbols_from_command_line(Layout * layout)2375*a9fa9459Szrj Symbol_table::add_undefined_symbols_from_command_line(Layout* layout)
2376*a9fa9459Szrj {
2377*a9fa9459Szrj   if (parameters->options().any_undefined()
2378*a9fa9459Szrj       || layout->script_options()->any_unreferenced())
2379*a9fa9459Szrj     {
2380*a9fa9459Szrj       if (parameters->target().get_size() == 32)
2381*a9fa9459Szrj 	{
2382*a9fa9459Szrj #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2383*a9fa9459Szrj 	  this->do_add_undefined_symbols_from_command_line<32>(layout);
2384*a9fa9459Szrj #else
2385*a9fa9459Szrj 	  gold_unreachable();
2386*a9fa9459Szrj #endif
2387*a9fa9459Szrj 	}
2388*a9fa9459Szrj       else if (parameters->target().get_size() == 64)
2389*a9fa9459Szrj 	{
2390*a9fa9459Szrj #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2391*a9fa9459Szrj 	  this->do_add_undefined_symbols_from_command_line<64>(layout);
2392*a9fa9459Szrj #else
2393*a9fa9459Szrj 	  gold_unreachable();
2394*a9fa9459Szrj #endif
2395*a9fa9459Szrj 	}
2396*a9fa9459Szrj       else
2397*a9fa9459Szrj 	gold_unreachable();
2398*a9fa9459Szrj     }
2399*a9fa9459Szrj }
2400*a9fa9459Szrj 
2401*a9fa9459Szrj template<int size>
2402*a9fa9459Szrj void
do_add_undefined_symbols_from_command_line(Layout * layout)2403*a9fa9459Szrj Symbol_table::do_add_undefined_symbols_from_command_line(Layout* layout)
2404*a9fa9459Szrj {
2405*a9fa9459Szrj   for (options::String_set::const_iterator p =
2406*a9fa9459Szrj 	 parameters->options().undefined_begin();
2407*a9fa9459Szrj        p != parameters->options().undefined_end();
2408*a9fa9459Szrj        ++p)
2409*a9fa9459Szrj     this->add_undefined_symbol_from_command_line<size>(p->c_str());
2410*a9fa9459Szrj 
2411*a9fa9459Szrj   for (options::String_set::const_iterator p =
2412*a9fa9459Szrj 	 parameters->options().export_dynamic_symbol_begin();
2413*a9fa9459Szrj        p != parameters->options().export_dynamic_symbol_end();
2414*a9fa9459Szrj        ++p)
2415*a9fa9459Szrj     this->add_undefined_symbol_from_command_line<size>(p->c_str());
2416*a9fa9459Szrj 
2417*a9fa9459Szrj   for (Script_options::referenced_const_iterator p =
2418*a9fa9459Szrj 	 layout->script_options()->referenced_begin();
2419*a9fa9459Szrj        p != layout->script_options()->referenced_end();
2420*a9fa9459Szrj        ++p)
2421*a9fa9459Szrj     this->add_undefined_symbol_from_command_line<size>(p->c_str());
2422*a9fa9459Szrj }
2423*a9fa9459Szrj 
2424*a9fa9459Szrj template<int size>
2425*a9fa9459Szrj void
add_undefined_symbol_from_command_line(const char * name)2426*a9fa9459Szrj Symbol_table::add_undefined_symbol_from_command_line(const char* name)
2427*a9fa9459Szrj {
2428*a9fa9459Szrj   if (this->lookup(name) != NULL)
2429*a9fa9459Szrj     return;
2430*a9fa9459Szrj 
2431*a9fa9459Szrj   const char* version = NULL;
2432*a9fa9459Szrj 
2433*a9fa9459Szrj   Sized_symbol<size>* sym;
2434*a9fa9459Szrj   Sized_symbol<size>* oldsym;
2435*a9fa9459Szrj   bool resolve_oldsym;
2436*a9fa9459Szrj   if (parameters->target().is_big_endian())
2437*a9fa9459Szrj     {
2438*a9fa9459Szrj #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
2439*a9fa9459Szrj       sym = this->define_special_symbol<size, true>(&name, &version,
2440*a9fa9459Szrj 						    false, &oldsym,
2441*a9fa9459Szrj 						    &resolve_oldsym);
2442*a9fa9459Szrj #else
2443*a9fa9459Szrj       gold_unreachable();
2444*a9fa9459Szrj #endif
2445*a9fa9459Szrj     }
2446*a9fa9459Szrj   else
2447*a9fa9459Szrj     {
2448*a9fa9459Szrj #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
2449*a9fa9459Szrj       sym = this->define_special_symbol<size, false>(&name, &version,
2450*a9fa9459Szrj 						     false, &oldsym,
2451*a9fa9459Szrj 						     &resolve_oldsym);
2452*a9fa9459Szrj #else
2453*a9fa9459Szrj       gold_unreachable();
2454*a9fa9459Szrj #endif
2455*a9fa9459Szrj     }
2456*a9fa9459Szrj 
2457*a9fa9459Szrj   gold_assert(oldsym == NULL);
2458*a9fa9459Szrj 
2459*a9fa9459Szrj   sym->init_undefined(name, version, 0, elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
2460*a9fa9459Szrj 		      elfcpp::STV_DEFAULT, 0);
2461*a9fa9459Szrj   ++this->saw_undefined_;
2462*a9fa9459Szrj }
2463*a9fa9459Szrj 
2464*a9fa9459Szrj // Set the dynamic symbol indexes.  INDEX is the index of the first
2465*a9fa9459Szrj // global dynamic symbol.  Pointers to the symbols are stored into the
2466*a9fa9459Szrj // vector SYMS.  The names are added to DYNPOOL.  This returns an
2467*a9fa9459Szrj // updated dynamic symbol index.
2468*a9fa9459Szrj 
2469*a9fa9459Szrj unsigned int
set_dynsym_indexes(unsigned int index,std::vector<Symbol * > * syms,Stringpool * dynpool,Versions * versions)2470*a9fa9459Szrj Symbol_table::set_dynsym_indexes(unsigned int index,
2471*a9fa9459Szrj 				 std::vector<Symbol*>* syms,
2472*a9fa9459Szrj 				 Stringpool* dynpool,
2473*a9fa9459Szrj 				 Versions* versions)
2474*a9fa9459Szrj {
2475*a9fa9459Szrj   std::vector<Symbol*> as_needed_sym;
2476*a9fa9459Szrj 
2477*a9fa9459Szrj   // Allow a target to set dynsym indexes.
2478*a9fa9459Szrj   if (parameters->target().has_custom_set_dynsym_indexes())
2479*a9fa9459Szrj     {
2480*a9fa9459Szrj       std::vector<Symbol*> dyn_symbols;
2481*a9fa9459Szrj       for (Symbol_table_type::iterator p = this->table_.begin();
2482*a9fa9459Szrj            p != this->table_.end();
2483*a9fa9459Szrj            ++p)
2484*a9fa9459Szrj         {
2485*a9fa9459Szrj           Symbol* sym = p->second;
2486*a9fa9459Szrj           if (!sym->should_add_dynsym_entry(this))
2487*a9fa9459Szrj             sym->set_dynsym_index(-1U);
2488*a9fa9459Szrj           else
2489*a9fa9459Szrj             dyn_symbols.push_back(sym);
2490*a9fa9459Szrj         }
2491*a9fa9459Szrj 
2492*a9fa9459Szrj       return parameters->target().set_dynsym_indexes(&dyn_symbols, index, syms,
2493*a9fa9459Szrj                                                      dynpool, versions, this);
2494*a9fa9459Szrj     }
2495*a9fa9459Szrj 
2496*a9fa9459Szrj   for (Symbol_table_type::iterator p = this->table_.begin();
2497*a9fa9459Szrj        p != this->table_.end();
2498*a9fa9459Szrj        ++p)
2499*a9fa9459Szrj     {
2500*a9fa9459Szrj       Symbol* sym = p->second;
2501*a9fa9459Szrj 
2502*a9fa9459Szrj       // Note that SYM may already have a dynamic symbol index, since
2503*a9fa9459Szrj       // some symbols appear more than once in the symbol table, with
2504*a9fa9459Szrj       // and without a version.
2505*a9fa9459Szrj 
2506*a9fa9459Szrj       if (!sym->should_add_dynsym_entry(this))
2507*a9fa9459Szrj 	sym->set_dynsym_index(-1U);
2508*a9fa9459Szrj       else if (!sym->has_dynsym_index())
2509*a9fa9459Szrj 	{
2510*a9fa9459Szrj 	  sym->set_dynsym_index(index);
2511*a9fa9459Szrj 	  ++index;
2512*a9fa9459Szrj 	  syms->push_back(sym);
2513*a9fa9459Szrj 	  dynpool->add(sym->name(), false, NULL);
2514*a9fa9459Szrj 
2515*a9fa9459Szrj 	  // If the symbol is defined in a dynamic object and is
2516*a9fa9459Szrj 	  // referenced strongly in a regular object, then mark the
2517*a9fa9459Szrj 	  // dynamic object as needed.  This is used to implement
2518*a9fa9459Szrj 	  // --as-needed.
2519*a9fa9459Szrj 	  if (sym->is_from_dynobj()
2520*a9fa9459Szrj 	      && sym->in_reg()
2521*a9fa9459Szrj 	      && !sym->is_undef_binding_weak())
2522*a9fa9459Szrj 	    sym->object()->set_is_needed();
2523*a9fa9459Szrj 
2524*a9fa9459Szrj 	  // Record any version information, except those from
2525*a9fa9459Szrj 	  // as-needed libraries not seen to be needed.  Note that the
2526*a9fa9459Szrj 	  // is_needed state for such libraries can change in this loop.
2527*a9fa9459Szrj 	  if (sym->version() != NULL)
2528*a9fa9459Szrj 	    {
2529*a9fa9459Szrj 	      if (!sym->is_from_dynobj()
2530*a9fa9459Szrj 		  || !sym->object()->as_needed()
2531*a9fa9459Szrj 		  || sym->object()->is_needed())
2532*a9fa9459Szrj 		versions->record_version(this, dynpool, sym);
2533*a9fa9459Szrj 	      else
2534*a9fa9459Szrj 		as_needed_sym.push_back(sym);
2535*a9fa9459Szrj 	    }
2536*a9fa9459Szrj 	}
2537*a9fa9459Szrj     }
2538*a9fa9459Szrj 
2539*a9fa9459Szrj   // Process version information for symbols from as-needed libraries.
2540*a9fa9459Szrj   for (std::vector<Symbol*>::iterator p = as_needed_sym.begin();
2541*a9fa9459Szrj        p != as_needed_sym.end();
2542*a9fa9459Szrj        ++p)
2543*a9fa9459Szrj     {
2544*a9fa9459Szrj       Symbol* sym = *p;
2545*a9fa9459Szrj 
2546*a9fa9459Szrj       if (sym->object()->is_needed())
2547*a9fa9459Szrj 	versions->record_version(this, dynpool, sym);
2548*a9fa9459Szrj       else
2549*a9fa9459Szrj 	sym->clear_version();
2550*a9fa9459Szrj     }
2551*a9fa9459Szrj 
2552*a9fa9459Szrj   // Finish up the versions.  In some cases this may add new dynamic
2553*a9fa9459Szrj   // symbols.
2554*a9fa9459Szrj   index = versions->finalize(this, index, syms);
2555*a9fa9459Szrj 
2556*a9fa9459Szrj   // Process target-specific symbols.
2557*a9fa9459Szrj   for (std::vector<Symbol*>::iterator p = this->target_symbols_.begin();
2558*a9fa9459Szrj        p != this->target_symbols_.end();
2559*a9fa9459Szrj        ++p)
2560*a9fa9459Szrj     {
2561*a9fa9459Szrj       (*p)->set_dynsym_index(index);
2562*a9fa9459Szrj       ++index;
2563*a9fa9459Szrj       syms->push_back(*p);
2564*a9fa9459Szrj       dynpool->add((*p)->name(), false, NULL);
2565*a9fa9459Szrj     }
2566*a9fa9459Szrj 
2567*a9fa9459Szrj   return index;
2568*a9fa9459Szrj }
2569*a9fa9459Szrj 
2570*a9fa9459Szrj // Set the final values for all the symbols.  The index of the first
2571*a9fa9459Szrj // global symbol in the output file is *PLOCAL_SYMCOUNT.  Record the
2572*a9fa9459Szrj // file offset OFF.  Add their names to POOL.  Return the new file
2573*a9fa9459Szrj // offset.  Update *PLOCAL_SYMCOUNT if necessary.
2574*a9fa9459Szrj 
2575*a9fa9459Szrj off_t
finalize(off_t off,off_t dynoff,size_t dyn_global_index,size_t dyncount,Stringpool * pool,unsigned int * plocal_symcount)2576*a9fa9459Szrj Symbol_table::finalize(off_t off, off_t dynoff, size_t dyn_global_index,
2577*a9fa9459Szrj 		       size_t dyncount, Stringpool* pool,
2578*a9fa9459Szrj 		       unsigned int* plocal_symcount)
2579*a9fa9459Szrj {
2580*a9fa9459Szrj   off_t ret;
2581*a9fa9459Szrj 
2582*a9fa9459Szrj   gold_assert(*plocal_symcount != 0);
2583*a9fa9459Szrj   this->first_global_index_ = *plocal_symcount;
2584*a9fa9459Szrj 
2585*a9fa9459Szrj   this->dynamic_offset_ = dynoff;
2586*a9fa9459Szrj   this->first_dynamic_global_index_ = dyn_global_index;
2587*a9fa9459Szrj   this->dynamic_count_ = dyncount;
2588*a9fa9459Szrj 
2589*a9fa9459Szrj   if (parameters->target().get_size() == 32)
2590*a9fa9459Szrj     {
2591*a9fa9459Szrj #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
2592*a9fa9459Szrj       ret = this->sized_finalize<32>(off, pool, plocal_symcount);
2593*a9fa9459Szrj #else
2594*a9fa9459Szrj       gold_unreachable();
2595*a9fa9459Szrj #endif
2596*a9fa9459Szrj     }
2597*a9fa9459Szrj   else if (parameters->target().get_size() == 64)
2598*a9fa9459Szrj     {
2599*a9fa9459Szrj #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
2600*a9fa9459Szrj       ret = this->sized_finalize<64>(off, pool, plocal_symcount);
2601*a9fa9459Szrj #else
2602*a9fa9459Szrj       gold_unreachable();
2603*a9fa9459Szrj #endif
2604*a9fa9459Szrj     }
2605*a9fa9459Szrj   else
2606*a9fa9459Szrj     gold_unreachable();
2607*a9fa9459Szrj 
2608*a9fa9459Szrj   // Now that we have the final symbol table, we can reliably note
2609*a9fa9459Szrj   // which symbols should get warnings.
2610*a9fa9459Szrj   this->warnings_.note_warnings(this);
2611*a9fa9459Szrj 
2612*a9fa9459Szrj   return ret;
2613*a9fa9459Szrj }
2614*a9fa9459Szrj 
2615*a9fa9459Szrj // SYM is going into the symbol table at *PINDEX.  Add the name to
2616*a9fa9459Szrj // POOL, update *PINDEX and *POFF.
2617*a9fa9459Szrj 
2618*a9fa9459Szrj template<int size>
2619*a9fa9459Szrj void
add_to_final_symtab(Symbol * sym,Stringpool * pool,unsigned int * pindex,off_t * poff)2620*a9fa9459Szrj Symbol_table::add_to_final_symtab(Symbol* sym, Stringpool* pool,
2621*a9fa9459Szrj 				  unsigned int* pindex, off_t* poff)
2622*a9fa9459Szrj {
2623*a9fa9459Szrj   sym->set_symtab_index(*pindex);
2624*a9fa9459Szrj   if (sym->version() == NULL || !parameters->options().relocatable())
2625*a9fa9459Szrj     pool->add(sym->name(), false, NULL);
2626*a9fa9459Szrj   else
2627*a9fa9459Szrj     pool->add(sym->versioned_name(), true, NULL);
2628*a9fa9459Szrj   ++*pindex;
2629*a9fa9459Szrj   *poff += elfcpp::Elf_sizes<size>::sym_size;
2630*a9fa9459Szrj }
2631*a9fa9459Szrj 
2632*a9fa9459Szrj // Set the final value for all the symbols.  This is called after
2633*a9fa9459Szrj // Layout::finalize, so all the output sections have their final
2634*a9fa9459Szrj // address.
2635*a9fa9459Szrj 
2636*a9fa9459Szrj template<int size>
2637*a9fa9459Szrj off_t
sized_finalize(off_t off,Stringpool * pool,unsigned int * plocal_symcount)2638*a9fa9459Szrj Symbol_table::sized_finalize(off_t off, Stringpool* pool,
2639*a9fa9459Szrj 			     unsigned int* plocal_symcount)
2640*a9fa9459Szrj {
2641*a9fa9459Szrj   off = align_address(off, size >> 3);
2642*a9fa9459Szrj   this->offset_ = off;
2643*a9fa9459Szrj 
2644*a9fa9459Szrj   unsigned int index = *plocal_symcount;
2645*a9fa9459Szrj   const unsigned int orig_index = index;
2646*a9fa9459Szrj 
2647*a9fa9459Szrj   // First do all the symbols which have been forced to be local, as
2648*a9fa9459Szrj   // they must appear before all global symbols.
2649*a9fa9459Szrj   for (Forced_locals::iterator p = this->forced_locals_.begin();
2650*a9fa9459Szrj        p != this->forced_locals_.end();
2651*a9fa9459Szrj        ++p)
2652*a9fa9459Szrj     {
2653*a9fa9459Szrj       Symbol* sym = *p;
2654*a9fa9459Szrj       gold_assert(sym->is_forced_local());
2655*a9fa9459Szrj       if (this->sized_finalize_symbol<size>(sym))
2656*a9fa9459Szrj 	{
2657*a9fa9459Szrj 	  this->add_to_final_symtab<size>(sym, pool, &index, &off);
2658*a9fa9459Szrj 	  ++*plocal_symcount;
2659*a9fa9459Szrj 	}
2660*a9fa9459Szrj     }
2661*a9fa9459Szrj 
2662*a9fa9459Szrj   // Now do all the remaining symbols.
2663*a9fa9459Szrj   for (Symbol_table_type::iterator p = this->table_.begin();
2664*a9fa9459Szrj        p != this->table_.end();
2665*a9fa9459Szrj        ++p)
2666*a9fa9459Szrj     {
2667*a9fa9459Szrj       Symbol* sym = p->second;
2668*a9fa9459Szrj       if (this->sized_finalize_symbol<size>(sym))
2669*a9fa9459Szrj 	this->add_to_final_symtab<size>(sym, pool, &index, &off);
2670*a9fa9459Szrj     }
2671*a9fa9459Szrj 
2672*a9fa9459Szrj   // Now do target-specific symbols.
2673*a9fa9459Szrj   for (std::vector<Symbol*>::iterator p = this->target_symbols_.begin();
2674*a9fa9459Szrj        p != this->target_symbols_.end();
2675*a9fa9459Szrj        ++p)
2676*a9fa9459Szrj     {
2677*a9fa9459Szrj       this->add_to_final_symtab<size>(*p, pool, &index, &off);
2678*a9fa9459Szrj     }
2679*a9fa9459Szrj 
2680*a9fa9459Szrj   this->output_count_ = index - orig_index;
2681*a9fa9459Szrj 
2682*a9fa9459Szrj   return off;
2683*a9fa9459Szrj }
2684*a9fa9459Szrj 
2685*a9fa9459Szrj // Compute the final value of SYM and store status in location PSTATUS.
2686*a9fa9459Szrj // During relaxation, this may be called multiple times for a symbol to
2687*a9fa9459Szrj // compute its would-be final value in each relaxation pass.
2688*a9fa9459Szrj 
2689*a9fa9459Szrj template<int size>
2690*a9fa9459Szrj typename Sized_symbol<size>::Value_type
compute_final_value(const Sized_symbol<size> * sym,Compute_final_value_status * pstatus) const2691*a9fa9459Szrj Symbol_table::compute_final_value(
2692*a9fa9459Szrj     const Sized_symbol<size>* sym,
2693*a9fa9459Szrj     Compute_final_value_status* pstatus) const
2694*a9fa9459Szrj {
2695*a9fa9459Szrj   typedef typename Sized_symbol<size>::Value_type Value_type;
2696*a9fa9459Szrj   Value_type value;
2697*a9fa9459Szrj 
2698*a9fa9459Szrj   switch (sym->source())
2699*a9fa9459Szrj     {
2700*a9fa9459Szrj     case Symbol::FROM_OBJECT:
2701*a9fa9459Szrj       {
2702*a9fa9459Szrj 	bool is_ordinary;
2703*a9fa9459Szrj 	unsigned int shndx = sym->shndx(&is_ordinary);
2704*a9fa9459Szrj 
2705*a9fa9459Szrj 	if (!is_ordinary
2706*a9fa9459Szrj 	    && shndx != elfcpp::SHN_ABS
2707*a9fa9459Szrj 	    && !Symbol::is_common_shndx(shndx))
2708*a9fa9459Szrj 	  {
2709*a9fa9459Szrj 	    *pstatus = CFVS_UNSUPPORTED_SYMBOL_SECTION;
2710*a9fa9459Szrj 	    return 0;
2711*a9fa9459Szrj 	  }
2712*a9fa9459Szrj 
2713*a9fa9459Szrj 	Object* symobj = sym->object();
2714*a9fa9459Szrj 	if (symobj->is_dynamic())
2715*a9fa9459Szrj 	  {
2716*a9fa9459Szrj 	    value = 0;
2717*a9fa9459Szrj 	    shndx = elfcpp::SHN_UNDEF;
2718*a9fa9459Szrj 	  }
2719*a9fa9459Szrj 	else if (symobj->pluginobj() != NULL)
2720*a9fa9459Szrj 	  {
2721*a9fa9459Szrj 	    value = 0;
2722*a9fa9459Szrj 	    shndx = elfcpp::SHN_UNDEF;
2723*a9fa9459Szrj 	  }
2724*a9fa9459Szrj 	else if (shndx == elfcpp::SHN_UNDEF)
2725*a9fa9459Szrj 	  value = 0;
2726*a9fa9459Szrj 	else if (!is_ordinary
2727*a9fa9459Szrj 		 && (shndx == elfcpp::SHN_ABS
2728*a9fa9459Szrj 		     || Symbol::is_common_shndx(shndx)))
2729*a9fa9459Szrj 	  value = sym->value();
2730*a9fa9459Szrj 	else
2731*a9fa9459Szrj 	  {
2732*a9fa9459Szrj 	    Relobj* relobj = static_cast<Relobj*>(symobj);
2733*a9fa9459Szrj 	    Output_section* os = relobj->output_section(shndx);
2734*a9fa9459Szrj 
2735*a9fa9459Szrj             if (this->is_section_folded(relobj, shndx))
2736*a9fa9459Szrj               {
2737*a9fa9459Szrj                 gold_assert(os == NULL);
2738*a9fa9459Szrj                 // Get the os of the section it is folded onto.
2739*a9fa9459Szrj                 Section_id folded = this->icf_->get_folded_section(relobj,
2740*a9fa9459Szrj                                                                    shndx);
2741*a9fa9459Szrj                 gold_assert(folded.first != NULL);
2742*a9fa9459Szrj                 Relobj* folded_obj = reinterpret_cast<Relobj*>(folded.first);
2743*a9fa9459Szrj 		unsigned folded_shndx = folded.second;
2744*a9fa9459Szrj 
2745*a9fa9459Szrj                 os = folded_obj->output_section(folded_shndx);
2746*a9fa9459Szrj                 gold_assert(os != NULL);
2747*a9fa9459Szrj 
2748*a9fa9459Szrj 		// Replace (relobj, shndx) with canonical ICF input section.
2749*a9fa9459Szrj 		shndx = folded_shndx;
2750*a9fa9459Szrj 		relobj = folded_obj;
2751*a9fa9459Szrj               }
2752*a9fa9459Szrj 
2753*a9fa9459Szrj             uint64_t secoff64 = relobj->output_section_offset(shndx);
2754*a9fa9459Szrj  	    if (os == NULL)
2755*a9fa9459Szrj 	      {
2756*a9fa9459Szrj                 bool static_or_reloc = (parameters->doing_static_link() ||
2757*a9fa9459Szrj                                         parameters->options().relocatable());
2758*a9fa9459Szrj                 gold_assert(static_or_reloc || sym->dynsym_index() == -1U);
2759*a9fa9459Szrj 
2760*a9fa9459Szrj 		*pstatus = CFVS_NO_OUTPUT_SECTION;
2761*a9fa9459Szrj 		return 0;
2762*a9fa9459Szrj 	      }
2763*a9fa9459Szrj 
2764*a9fa9459Szrj             if (secoff64 == -1ULL)
2765*a9fa9459Szrj               {
2766*a9fa9459Szrj                 // The section needs special handling (e.g., a merge section).
2767*a9fa9459Szrj 
2768*a9fa9459Szrj 	        value = os->output_address(relobj, shndx, sym->value());
2769*a9fa9459Szrj 	      }
2770*a9fa9459Szrj             else
2771*a9fa9459Szrj               {
2772*a9fa9459Szrj                 Value_type secoff =
2773*a9fa9459Szrj                   convert_types<Value_type, uint64_t>(secoff64);
2774*a9fa9459Szrj 	        if (sym->type() == elfcpp::STT_TLS)
2775*a9fa9459Szrj 	          value = sym->value() + os->tls_offset() + secoff;
2776*a9fa9459Szrj 	        else
2777*a9fa9459Szrj 	          value = sym->value() + os->address() + secoff;
2778*a9fa9459Szrj 	      }
2779*a9fa9459Szrj 	  }
2780*a9fa9459Szrj       }
2781*a9fa9459Szrj       break;
2782*a9fa9459Szrj 
2783*a9fa9459Szrj     case Symbol::IN_OUTPUT_DATA:
2784*a9fa9459Szrj       {
2785*a9fa9459Szrj 	Output_data* od = sym->output_data();
2786*a9fa9459Szrj 	value = sym->value();
2787*a9fa9459Szrj 	if (sym->type() != elfcpp::STT_TLS)
2788*a9fa9459Szrj 	  value += od->address();
2789*a9fa9459Szrj 	else
2790*a9fa9459Szrj 	  {
2791*a9fa9459Szrj 	    Output_section* os = od->output_section();
2792*a9fa9459Szrj 	    gold_assert(os != NULL);
2793*a9fa9459Szrj 	    value += os->tls_offset() + (od->address() - os->address());
2794*a9fa9459Szrj 	  }
2795*a9fa9459Szrj 	if (sym->offset_is_from_end())
2796*a9fa9459Szrj 	  value += od->data_size();
2797*a9fa9459Szrj       }
2798*a9fa9459Szrj       break;
2799*a9fa9459Szrj 
2800*a9fa9459Szrj     case Symbol::IN_OUTPUT_SEGMENT:
2801*a9fa9459Szrj       {
2802*a9fa9459Szrj 	Output_segment* os = sym->output_segment();
2803*a9fa9459Szrj 	value = sym->value();
2804*a9fa9459Szrj         if (sym->type() != elfcpp::STT_TLS)
2805*a9fa9459Szrj 	  value += os->vaddr();
2806*a9fa9459Szrj 	switch (sym->offset_base())
2807*a9fa9459Szrj 	  {
2808*a9fa9459Szrj 	  case Symbol::SEGMENT_START:
2809*a9fa9459Szrj 	    break;
2810*a9fa9459Szrj 	  case Symbol::SEGMENT_END:
2811*a9fa9459Szrj 	    value += os->memsz();
2812*a9fa9459Szrj 	    break;
2813*a9fa9459Szrj 	  case Symbol::SEGMENT_BSS:
2814*a9fa9459Szrj 	    value += os->filesz();
2815*a9fa9459Szrj 	    break;
2816*a9fa9459Szrj 	  default:
2817*a9fa9459Szrj 	    gold_unreachable();
2818*a9fa9459Szrj 	  }
2819*a9fa9459Szrj       }
2820*a9fa9459Szrj       break;
2821*a9fa9459Szrj 
2822*a9fa9459Szrj     case Symbol::IS_CONSTANT:
2823*a9fa9459Szrj       value = sym->value();
2824*a9fa9459Szrj       break;
2825*a9fa9459Szrj 
2826*a9fa9459Szrj     case Symbol::IS_UNDEFINED:
2827*a9fa9459Szrj       value = 0;
2828*a9fa9459Szrj       break;
2829*a9fa9459Szrj 
2830*a9fa9459Szrj     default:
2831*a9fa9459Szrj       gold_unreachable();
2832*a9fa9459Szrj     }
2833*a9fa9459Szrj 
2834*a9fa9459Szrj   *pstatus = CFVS_OK;
2835*a9fa9459Szrj   return value;
2836*a9fa9459Szrj }
2837*a9fa9459Szrj 
2838*a9fa9459Szrj // Finalize the symbol SYM.  This returns true if the symbol should be
2839*a9fa9459Szrj // added to the symbol table, false otherwise.
2840*a9fa9459Szrj 
2841*a9fa9459Szrj template<int size>
2842*a9fa9459Szrj bool
sized_finalize_symbol(Symbol * unsized_sym)2843*a9fa9459Szrj Symbol_table::sized_finalize_symbol(Symbol* unsized_sym)
2844*a9fa9459Szrj {
2845*a9fa9459Szrj   typedef typename Sized_symbol<size>::Value_type Value_type;
2846*a9fa9459Szrj 
2847*a9fa9459Szrj   Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(unsized_sym);
2848*a9fa9459Szrj 
2849*a9fa9459Szrj   // The default version of a symbol may appear twice in the symbol
2850*a9fa9459Szrj   // table.  We only need to finalize it once.
2851*a9fa9459Szrj   if (sym->has_symtab_index())
2852*a9fa9459Szrj     return false;
2853*a9fa9459Szrj 
2854*a9fa9459Szrj   if (!sym->in_reg())
2855*a9fa9459Szrj     {
2856*a9fa9459Szrj       gold_assert(!sym->has_symtab_index());
2857*a9fa9459Szrj       sym->set_symtab_index(-1U);
2858*a9fa9459Szrj       gold_assert(sym->dynsym_index() == -1U);
2859*a9fa9459Szrj       return false;
2860*a9fa9459Szrj     }
2861*a9fa9459Szrj 
2862*a9fa9459Szrj   // If the symbol is only present on plugin files, the plugin decided we
2863*a9fa9459Szrj   // don't need it.
2864*a9fa9459Szrj   if (!sym->in_real_elf())
2865*a9fa9459Szrj     {
2866*a9fa9459Szrj       gold_assert(!sym->has_symtab_index());
2867*a9fa9459Szrj       sym->set_symtab_index(-1U);
2868*a9fa9459Szrj       return false;
2869*a9fa9459Szrj     }
2870*a9fa9459Szrj 
2871*a9fa9459Szrj   // Compute final symbol value.
2872*a9fa9459Szrj   Compute_final_value_status status;
2873*a9fa9459Szrj   Value_type value = this->compute_final_value(sym, &status);
2874*a9fa9459Szrj 
2875*a9fa9459Szrj   switch (status)
2876*a9fa9459Szrj     {
2877*a9fa9459Szrj     case CFVS_OK:
2878*a9fa9459Szrj       break;
2879*a9fa9459Szrj     case CFVS_UNSUPPORTED_SYMBOL_SECTION:
2880*a9fa9459Szrj       {
2881*a9fa9459Szrj 	bool is_ordinary;
2882*a9fa9459Szrj 	unsigned int shndx = sym->shndx(&is_ordinary);
2883*a9fa9459Szrj 	gold_error(_("%s: unsupported symbol section 0x%x"),
2884*a9fa9459Szrj 		   sym->demangled_name().c_str(), shndx);
2885*a9fa9459Szrj       }
2886*a9fa9459Szrj       break;
2887*a9fa9459Szrj     case CFVS_NO_OUTPUT_SECTION:
2888*a9fa9459Szrj       sym->set_symtab_index(-1U);
2889*a9fa9459Szrj       return false;
2890*a9fa9459Szrj     default:
2891*a9fa9459Szrj       gold_unreachable();
2892*a9fa9459Szrj     }
2893*a9fa9459Szrj 
2894*a9fa9459Szrj   sym->set_value(value);
2895*a9fa9459Szrj 
2896*a9fa9459Szrj   if (parameters->options().strip_all()
2897*a9fa9459Szrj       || !parameters->options().should_retain_symbol(sym->name()))
2898*a9fa9459Szrj     {
2899*a9fa9459Szrj       sym->set_symtab_index(-1U);
2900*a9fa9459Szrj       return false;
2901*a9fa9459Szrj     }
2902*a9fa9459Szrj 
2903*a9fa9459Szrj   return true;
2904*a9fa9459Szrj }
2905*a9fa9459Szrj 
2906*a9fa9459Szrj // Write out the global symbols.
2907*a9fa9459Szrj 
2908*a9fa9459Szrj void
write_globals(const Stringpool * sympool,const Stringpool * dynpool,Output_symtab_xindex * symtab_xindex,Output_symtab_xindex * dynsym_xindex,Output_file * of) const2909*a9fa9459Szrj Symbol_table::write_globals(const Stringpool* sympool,
2910*a9fa9459Szrj 			    const Stringpool* dynpool,
2911*a9fa9459Szrj 			    Output_symtab_xindex* symtab_xindex,
2912*a9fa9459Szrj 			    Output_symtab_xindex* dynsym_xindex,
2913*a9fa9459Szrj 			    Output_file* of) const
2914*a9fa9459Szrj {
2915*a9fa9459Szrj   switch (parameters->size_and_endianness())
2916*a9fa9459Szrj     {
2917*a9fa9459Szrj #ifdef HAVE_TARGET_32_LITTLE
2918*a9fa9459Szrj     case Parameters::TARGET_32_LITTLE:
2919*a9fa9459Szrj       this->sized_write_globals<32, false>(sympool, dynpool, symtab_xindex,
2920*a9fa9459Szrj 					   dynsym_xindex, of);
2921*a9fa9459Szrj       break;
2922*a9fa9459Szrj #endif
2923*a9fa9459Szrj #ifdef HAVE_TARGET_32_BIG
2924*a9fa9459Szrj     case Parameters::TARGET_32_BIG:
2925*a9fa9459Szrj       this->sized_write_globals<32, true>(sympool, dynpool, symtab_xindex,
2926*a9fa9459Szrj 					  dynsym_xindex, of);
2927*a9fa9459Szrj       break;
2928*a9fa9459Szrj #endif
2929*a9fa9459Szrj #ifdef HAVE_TARGET_64_LITTLE
2930*a9fa9459Szrj     case Parameters::TARGET_64_LITTLE:
2931*a9fa9459Szrj       this->sized_write_globals<64, false>(sympool, dynpool, symtab_xindex,
2932*a9fa9459Szrj 					   dynsym_xindex, of);
2933*a9fa9459Szrj       break;
2934*a9fa9459Szrj #endif
2935*a9fa9459Szrj #ifdef HAVE_TARGET_64_BIG
2936*a9fa9459Szrj     case Parameters::TARGET_64_BIG:
2937*a9fa9459Szrj       this->sized_write_globals<64, true>(sympool, dynpool, symtab_xindex,
2938*a9fa9459Szrj 					  dynsym_xindex, of);
2939*a9fa9459Szrj       break;
2940*a9fa9459Szrj #endif
2941*a9fa9459Szrj     default:
2942*a9fa9459Szrj       gold_unreachable();
2943*a9fa9459Szrj     }
2944*a9fa9459Szrj }
2945*a9fa9459Szrj 
2946*a9fa9459Szrj // Write out the global symbols.
2947*a9fa9459Szrj 
2948*a9fa9459Szrj template<int size, bool big_endian>
2949*a9fa9459Szrj void
sized_write_globals(const Stringpool * sympool,const Stringpool * dynpool,Output_symtab_xindex * symtab_xindex,Output_symtab_xindex * dynsym_xindex,Output_file * of) const2950*a9fa9459Szrj Symbol_table::sized_write_globals(const Stringpool* sympool,
2951*a9fa9459Szrj 				  const Stringpool* dynpool,
2952*a9fa9459Szrj 				  Output_symtab_xindex* symtab_xindex,
2953*a9fa9459Szrj 				  Output_symtab_xindex* dynsym_xindex,
2954*a9fa9459Szrj 				  Output_file* of) const
2955*a9fa9459Szrj {
2956*a9fa9459Szrj   const Target& target = parameters->target();
2957*a9fa9459Szrj 
2958*a9fa9459Szrj   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2959*a9fa9459Szrj 
2960*a9fa9459Szrj   const unsigned int output_count = this->output_count_;
2961*a9fa9459Szrj   const section_size_type oview_size = output_count * sym_size;
2962*a9fa9459Szrj   const unsigned int first_global_index = this->first_global_index_;
2963*a9fa9459Szrj   unsigned char* psyms;
2964*a9fa9459Szrj   if (this->offset_ == 0 || output_count == 0)
2965*a9fa9459Szrj     psyms = NULL;
2966*a9fa9459Szrj   else
2967*a9fa9459Szrj     psyms = of->get_output_view(this->offset_, oview_size);
2968*a9fa9459Szrj 
2969*a9fa9459Szrj   const unsigned int dynamic_count = this->dynamic_count_;
2970*a9fa9459Szrj   const section_size_type dynamic_size = dynamic_count * sym_size;
2971*a9fa9459Szrj   const unsigned int first_dynamic_global_index =
2972*a9fa9459Szrj     this->first_dynamic_global_index_;
2973*a9fa9459Szrj   unsigned char* dynamic_view;
2974*a9fa9459Szrj   if (this->dynamic_offset_ == 0 || dynamic_count == 0)
2975*a9fa9459Szrj     dynamic_view = NULL;
2976*a9fa9459Szrj   else
2977*a9fa9459Szrj     dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
2978*a9fa9459Szrj 
2979*a9fa9459Szrj   for (Symbol_table_type::const_iterator p = this->table_.begin();
2980*a9fa9459Szrj        p != this->table_.end();
2981*a9fa9459Szrj        ++p)
2982*a9fa9459Szrj     {
2983*a9fa9459Szrj       Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
2984*a9fa9459Szrj 
2985*a9fa9459Szrj       // Possibly warn about unresolved symbols in shared libraries.
2986*a9fa9459Szrj       this->warn_about_undefined_dynobj_symbol(sym);
2987*a9fa9459Szrj 
2988*a9fa9459Szrj       unsigned int sym_index = sym->symtab_index();
2989*a9fa9459Szrj       unsigned int dynsym_index;
2990*a9fa9459Szrj       if (dynamic_view == NULL)
2991*a9fa9459Szrj 	dynsym_index = -1U;
2992*a9fa9459Szrj       else
2993*a9fa9459Szrj 	dynsym_index = sym->dynsym_index();
2994*a9fa9459Szrj 
2995*a9fa9459Szrj       if (sym_index == -1U && dynsym_index == -1U)
2996*a9fa9459Szrj 	{
2997*a9fa9459Szrj 	  // This symbol is not included in the output file.
2998*a9fa9459Szrj 	  continue;
2999*a9fa9459Szrj 	}
3000*a9fa9459Szrj 
3001*a9fa9459Szrj       unsigned int shndx;
3002*a9fa9459Szrj       typename elfcpp::Elf_types<size>::Elf_Addr sym_value = sym->value();
3003*a9fa9459Szrj       typename elfcpp::Elf_types<size>::Elf_Addr dynsym_value = sym_value;
3004*a9fa9459Szrj       elfcpp::STB binding = sym->binding();
3005*a9fa9459Szrj 
3006*a9fa9459Szrj       // If --weak-unresolved-symbols is set, change binding of unresolved
3007*a9fa9459Szrj       // global symbols to STB_WEAK.
3008*a9fa9459Szrj       if (parameters->options().weak_unresolved_symbols()
3009*a9fa9459Szrj 	  && binding == elfcpp::STB_GLOBAL
3010*a9fa9459Szrj 	  && sym->is_undefined())
3011*a9fa9459Szrj 	binding = elfcpp::STB_WEAK;
3012*a9fa9459Szrj 
3013*a9fa9459Szrj       // If --no-gnu-unique is set, change STB_GNU_UNIQUE to STB_GLOBAL.
3014*a9fa9459Szrj       if (binding == elfcpp::STB_GNU_UNIQUE
3015*a9fa9459Szrj 	  && !parameters->options().gnu_unique())
3016*a9fa9459Szrj 	binding = elfcpp::STB_GLOBAL;
3017*a9fa9459Szrj 
3018*a9fa9459Szrj       switch (sym->source())
3019*a9fa9459Szrj 	{
3020*a9fa9459Szrj 	case Symbol::FROM_OBJECT:
3021*a9fa9459Szrj 	  {
3022*a9fa9459Szrj 	    bool is_ordinary;
3023*a9fa9459Szrj 	    unsigned int in_shndx = sym->shndx(&is_ordinary);
3024*a9fa9459Szrj 
3025*a9fa9459Szrj 	    if (!is_ordinary
3026*a9fa9459Szrj 		&& in_shndx != elfcpp::SHN_ABS
3027*a9fa9459Szrj 		&& !Symbol::is_common_shndx(in_shndx))
3028*a9fa9459Szrj 	      {
3029*a9fa9459Szrj 		gold_error(_("%s: unsupported symbol section 0x%x"),
3030*a9fa9459Szrj 			   sym->demangled_name().c_str(), in_shndx);
3031*a9fa9459Szrj 		shndx = in_shndx;
3032*a9fa9459Szrj 	      }
3033*a9fa9459Szrj 	    else
3034*a9fa9459Szrj 	      {
3035*a9fa9459Szrj 		Object* symobj = sym->object();
3036*a9fa9459Szrj 		if (symobj->is_dynamic())
3037*a9fa9459Szrj 		  {
3038*a9fa9459Szrj 		    if (sym->needs_dynsym_value())
3039*a9fa9459Szrj 		      dynsym_value = target.dynsym_value(sym);
3040*a9fa9459Szrj 		    shndx = elfcpp::SHN_UNDEF;
3041*a9fa9459Szrj 		    if (sym->is_undef_binding_weak())
3042*a9fa9459Szrj 		      binding = elfcpp::STB_WEAK;
3043*a9fa9459Szrj 		    else
3044*a9fa9459Szrj 		      binding = elfcpp::STB_GLOBAL;
3045*a9fa9459Szrj 		  }
3046*a9fa9459Szrj 		else if (symobj->pluginobj() != NULL)
3047*a9fa9459Szrj 		  shndx = elfcpp::SHN_UNDEF;
3048*a9fa9459Szrj 		else if (in_shndx == elfcpp::SHN_UNDEF
3049*a9fa9459Szrj 			 || (!is_ordinary
3050*a9fa9459Szrj 			     && (in_shndx == elfcpp::SHN_ABS
3051*a9fa9459Szrj 				 || Symbol::is_common_shndx(in_shndx))))
3052*a9fa9459Szrj 		  shndx = in_shndx;
3053*a9fa9459Szrj 		else
3054*a9fa9459Szrj 		  {
3055*a9fa9459Szrj 		    Relobj* relobj = static_cast<Relobj*>(symobj);
3056*a9fa9459Szrj 		    Output_section* os = relobj->output_section(in_shndx);
3057*a9fa9459Szrj                     if (this->is_section_folded(relobj, in_shndx))
3058*a9fa9459Szrj                       {
3059*a9fa9459Szrj                         // This global symbol must be written out even though
3060*a9fa9459Szrj                         // it is folded.
3061*a9fa9459Szrj                         // Get the os of the section it is folded onto.
3062*a9fa9459Szrj                         Section_id folded =
3063*a9fa9459Szrj                              this->icf_->get_folded_section(relobj, in_shndx);
3064*a9fa9459Szrj                         gold_assert(folded.first !=NULL);
3065*a9fa9459Szrj                         Relobj* folded_obj =
3066*a9fa9459Szrj                           reinterpret_cast<Relobj*>(folded.first);
3067*a9fa9459Szrj                         os = folded_obj->output_section(folded.second);
3068*a9fa9459Szrj                         gold_assert(os != NULL);
3069*a9fa9459Szrj                       }
3070*a9fa9459Szrj 		    gold_assert(os != NULL);
3071*a9fa9459Szrj 		    shndx = os->out_shndx();
3072*a9fa9459Szrj 
3073*a9fa9459Szrj 		    if (shndx >= elfcpp::SHN_LORESERVE)
3074*a9fa9459Szrj 		      {
3075*a9fa9459Szrj 			if (sym_index != -1U)
3076*a9fa9459Szrj 			  symtab_xindex->add(sym_index, shndx);
3077*a9fa9459Szrj 			if (dynsym_index != -1U)
3078*a9fa9459Szrj 			  dynsym_xindex->add(dynsym_index, shndx);
3079*a9fa9459Szrj 			shndx = elfcpp::SHN_XINDEX;
3080*a9fa9459Szrj 		      }
3081*a9fa9459Szrj 
3082*a9fa9459Szrj 		    // In object files symbol values are section
3083*a9fa9459Szrj 		    // relative.
3084*a9fa9459Szrj 		    if (parameters->options().relocatable())
3085*a9fa9459Szrj 		      sym_value -= os->address();
3086*a9fa9459Szrj 		  }
3087*a9fa9459Szrj 	      }
3088*a9fa9459Szrj 	  }
3089*a9fa9459Szrj 	  break;
3090*a9fa9459Szrj 
3091*a9fa9459Szrj 	case Symbol::IN_OUTPUT_DATA:
3092*a9fa9459Szrj 	  {
3093*a9fa9459Szrj 	    Output_data* od = sym->output_data();
3094*a9fa9459Szrj 
3095*a9fa9459Szrj 	    shndx = od->out_shndx();
3096*a9fa9459Szrj 	    if (shndx >= elfcpp::SHN_LORESERVE)
3097*a9fa9459Szrj 	      {
3098*a9fa9459Szrj 		if (sym_index != -1U)
3099*a9fa9459Szrj 		  symtab_xindex->add(sym_index, shndx);
3100*a9fa9459Szrj 		if (dynsym_index != -1U)
3101*a9fa9459Szrj 		  dynsym_xindex->add(dynsym_index, shndx);
3102*a9fa9459Szrj 		shndx = elfcpp::SHN_XINDEX;
3103*a9fa9459Szrj 	      }
3104*a9fa9459Szrj 
3105*a9fa9459Szrj 	    // In object files symbol values are section
3106*a9fa9459Szrj 	    // relative.
3107*a9fa9459Szrj 	    if (parameters->options().relocatable())
3108*a9fa9459Szrj 	      sym_value -= od->address();
3109*a9fa9459Szrj 	  }
3110*a9fa9459Szrj 	  break;
3111*a9fa9459Szrj 
3112*a9fa9459Szrj 	case Symbol::IN_OUTPUT_SEGMENT:
3113*a9fa9459Szrj 	  shndx = elfcpp::SHN_ABS;
3114*a9fa9459Szrj 	  break;
3115*a9fa9459Szrj 
3116*a9fa9459Szrj 	case Symbol::IS_CONSTANT:
3117*a9fa9459Szrj 	  shndx = elfcpp::SHN_ABS;
3118*a9fa9459Szrj 	  break;
3119*a9fa9459Szrj 
3120*a9fa9459Szrj 	case Symbol::IS_UNDEFINED:
3121*a9fa9459Szrj 	  shndx = elfcpp::SHN_UNDEF;
3122*a9fa9459Szrj 	  break;
3123*a9fa9459Szrj 
3124*a9fa9459Szrj 	default:
3125*a9fa9459Szrj 	  gold_unreachable();
3126*a9fa9459Szrj 	}
3127*a9fa9459Szrj 
3128*a9fa9459Szrj       if (sym_index != -1U)
3129*a9fa9459Szrj 	{
3130*a9fa9459Szrj 	  sym_index -= first_global_index;
3131*a9fa9459Szrj 	  gold_assert(sym_index < output_count);
3132*a9fa9459Szrj 	  unsigned char* ps = psyms + (sym_index * sym_size);
3133*a9fa9459Szrj 	  this->sized_write_symbol<size, big_endian>(sym, sym_value, shndx,
3134*a9fa9459Szrj 						     binding, sympool, ps);
3135*a9fa9459Szrj 	}
3136*a9fa9459Szrj 
3137*a9fa9459Szrj       if (dynsym_index != -1U)
3138*a9fa9459Szrj 	{
3139*a9fa9459Szrj 	  dynsym_index -= first_dynamic_global_index;
3140*a9fa9459Szrj 	  gold_assert(dynsym_index < dynamic_count);
3141*a9fa9459Szrj 	  unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
3142*a9fa9459Szrj 	  this->sized_write_symbol<size, big_endian>(sym, dynsym_value, shndx,
3143*a9fa9459Szrj 						     binding, dynpool, pd);
3144*a9fa9459Szrj           // Allow a target to adjust dynamic symbol value.
3145*a9fa9459Szrj           parameters->target().adjust_dyn_symbol(sym, pd);
3146*a9fa9459Szrj 	}
3147*a9fa9459Szrj     }
3148*a9fa9459Szrj 
3149*a9fa9459Szrj   // Write the target-specific symbols.
3150*a9fa9459Szrj   for (std::vector<Symbol*>::const_iterator p = this->target_symbols_.begin();
3151*a9fa9459Szrj        p != this->target_symbols_.end();
3152*a9fa9459Szrj        ++p)
3153*a9fa9459Szrj     {
3154*a9fa9459Szrj       Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(*p);
3155*a9fa9459Szrj 
3156*a9fa9459Szrj       unsigned int sym_index = sym->symtab_index();
3157*a9fa9459Szrj       unsigned int dynsym_index;
3158*a9fa9459Szrj       if (dynamic_view == NULL)
3159*a9fa9459Szrj 	dynsym_index = -1U;
3160*a9fa9459Szrj       else
3161*a9fa9459Szrj 	dynsym_index = sym->dynsym_index();
3162*a9fa9459Szrj 
3163*a9fa9459Szrj       unsigned int shndx;
3164*a9fa9459Szrj       switch (sym->source())
3165*a9fa9459Szrj 	{
3166*a9fa9459Szrj 	case Symbol::IS_CONSTANT:
3167*a9fa9459Szrj 	  shndx = elfcpp::SHN_ABS;
3168*a9fa9459Szrj 	  break;
3169*a9fa9459Szrj 	case Symbol::IS_UNDEFINED:
3170*a9fa9459Szrj 	  shndx = elfcpp::SHN_UNDEF;
3171*a9fa9459Szrj 	  break;
3172*a9fa9459Szrj 	default:
3173*a9fa9459Szrj 	  gold_unreachable();
3174*a9fa9459Szrj 	}
3175*a9fa9459Szrj 
3176*a9fa9459Szrj       if (sym_index != -1U)
3177*a9fa9459Szrj 	{
3178*a9fa9459Szrj 	  sym_index -= first_global_index;
3179*a9fa9459Szrj 	  gold_assert(sym_index < output_count);
3180*a9fa9459Szrj 	  unsigned char* ps = psyms + (sym_index * sym_size);
3181*a9fa9459Szrj 	  this->sized_write_symbol<size, big_endian>(sym, sym->value(), shndx,
3182*a9fa9459Szrj 						     sym->binding(), sympool,
3183*a9fa9459Szrj 						     ps);
3184*a9fa9459Szrj 	}
3185*a9fa9459Szrj 
3186*a9fa9459Szrj       if (dynsym_index != -1U)
3187*a9fa9459Szrj 	{
3188*a9fa9459Szrj 	  dynsym_index -= first_dynamic_global_index;
3189*a9fa9459Szrj 	  gold_assert(dynsym_index < dynamic_count);
3190*a9fa9459Szrj 	  unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
3191*a9fa9459Szrj 	  this->sized_write_symbol<size, big_endian>(sym, sym->value(), shndx,
3192*a9fa9459Szrj 						     sym->binding(), dynpool,
3193*a9fa9459Szrj 						     pd);
3194*a9fa9459Szrj 	}
3195*a9fa9459Szrj     }
3196*a9fa9459Szrj 
3197*a9fa9459Szrj   of->write_output_view(this->offset_, oview_size, psyms);
3198*a9fa9459Szrj   if (dynamic_view != NULL)
3199*a9fa9459Szrj     of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
3200*a9fa9459Szrj }
3201*a9fa9459Szrj 
3202*a9fa9459Szrj // Write out the symbol SYM, in section SHNDX, to P.  POOL is the
3203*a9fa9459Szrj // strtab holding the name.
3204*a9fa9459Szrj 
3205*a9fa9459Szrj template<int size, bool big_endian>
3206*a9fa9459Szrj void
sized_write_symbol(Sized_symbol<size> * sym,typename elfcpp::Elf_types<size>::Elf_Addr value,unsigned int shndx,elfcpp::STB binding,const Stringpool * pool,unsigned char * p) const3207*a9fa9459Szrj Symbol_table::sized_write_symbol(
3208*a9fa9459Szrj     Sized_symbol<size>* sym,
3209*a9fa9459Szrj     typename elfcpp::Elf_types<size>::Elf_Addr value,
3210*a9fa9459Szrj     unsigned int shndx,
3211*a9fa9459Szrj     elfcpp::STB binding,
3212*a9fa9459Szrj     const Stringpool* pool,
3213*a9fa9459Szrj     unsigned char* p) const
3214*a9fa9459Szrj {
3215*a9fa9459Szrj   elfcpp::Sym_write<size, big_endian> osym(p);
3216*a9fa9459Szrj   if (sym->version() == NULL || !parameters->options().relocatable())
3217*a9fa9459Szrj     osym.put_st_name(pool->get_offset(sym->name()));
3218*a9fa9459Szrj   else
3219*a9fa9459Szrj     osym.put_st_name(pool->get_offset(sym->versioned_name()));
3220*a9fa9459Szrj   osym.put_st_value(value);
3221*a9fa9459Szrj   // Use a symbol size of zero for undefined symbols from shared libraries.
3222*a9fa9459Szrj   if (shndx == elfcpp::SHN_UNDEF && sym->is_from_dynobj())
3223*a9fa9459Szrj     osym.put_st_size(0);
3224*a9fa9459Szrj   else
3225*a9fa9459Szrj     osym.put_st_size(sym->symsize());
3226*a9fa9459Szrj   elfcpp::STT type = sym->type();
3227*a9fa9459Szrj   gold_assert(type != elfcpp::STT_GNU_IFUNC || !sym->is_from_dynobj());
3228*a9fa9459Szrj   // A version script may have overridden the default binding.
3229*a9fa9459Szrj   if (sym->is_forced_local())
3230*a9fa9459Szrj     osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL, type));
3231*a9fa9459Szrj   else
3232*a9fa9459Szrj     osym.put_st_info(elfcpp::elf_st_info(binding, type));
3233*a9fa9459Szrj   osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
3234*a9fa9459Szrj   osym.put_st_shndx(shndx);
3235*a9fa9459Szrj }
3236*a9fa9459Szrj 
3237*a9fa9459Szrj // Check for unresolved symbols in shared libraries.  This is
3238*a9fa9459Szrj // controlled by the --allow-shlib-undefined option.
3239*a9fa9459Szrj 
3240*a9fa9459Szrj // We only warn about libraries for which we have seen all the
3241*a9fa9459Szrj // DT_NEEDED entries.  We don't try to track down DT_NEEDED entries
3242*a9fa9459Szrj // which were not seen in this link.  If we didn't see a DT_NEEDED
3243*a9fa9459Szrj // entry, we aren't going to be able to reliably report whether the
3244*a9fa9459Szrj // symbol is undefined.
3245*a9fa9459Szrj 
3246*a9fa9459Szrj // We also don't warn about libraries found in a system library
3247*a9fa9459Szrj // directory (e.g., /lib or /usr/lib); we assume that those libraries
3248*a9fa9459Szrj // are OK.  This heuristic avoids problems on GNU/Linux, in which -ldl
3249*a9fa9459Szrj // can have undefined references satisfied by ld-linux.so.
3250*a9fa9459Szrj 
3251*a9fa9459Szrj inline void
warn_about_undefined_dynobj_symbol(Symbol * sym) const3252*a9fa9459Szrj Symbol_table::warn_about_undefined_dynobj_symbol(Symbol* sym) const
3253*a9fa9459Szrj {
3254*a9fa9459Szrj   bool dummy;
3255*a9fa9459Szrj   if (sym->source() == Symbol::FROM_OBJECT
3256*a9fa9459Szrj       && sym->object()->is_dynamic()
3257*a9fa9459Szrj       && sym->shndx(&dummy) == elfcpp::SHN_UNDEF
3258*a9fa9459Szrj       && sym->binding() != elfcpp::STB_WEAK
3259*a9fa9459Szrj       && !parameters->options().allow_shlib_undefined()
3260*a9fa9459Szrj       && !parameters->target().is_defined_by_abi(sym)
3261*a9fa9459Szrj       && !sym->object()->is_in_system_directory())
3262*a9fa9459Szrj     {
3263*a9fa9459Szrj       // A very ugly cast.
3264*a9fa9459Szrj       Dynobj* dynobj = static_cast<Dynobj*>(sym->object());
3265*a9fa9459Szrj       if (!dynobj->has_unknown_needed_entries())
3266*a9fa9459Szrj         gold_undefined_symbol(sym);
3267*a9fa9459Szrj     }
3268*a9fa9459Szrj }
3269*a9fa9459Szrj 
3270*a9fa9459Szrj // Write out a section symbol.  Return the update offset.
3271*a9fa9459Szrj 
3272*a9fa9459Szrj void
write_section_symbol(const Output_section * os,Output_symtab_xindex * symtab_xindex,Output_file * of,off_t offset) const3273*a9fa9459Szrj Symbol_table::write_section_symbol(const Output_section* os,
3274*a9fa9459Szrj 				   Output_symtab_xindex* symtab_xindex,
3275*a9fa9459Szrj 				   Output_file* of,
3276*a9fa9459Szrj 				   off_t offset) const
3277*a9fa9459Szrj {
3278*a9fa9459Szrj   switch (parameters->size_and_endianness())
3279*a9fa9459Szrj     {
3280*a9fa9459Szrj #ifdef HAVE_TARGET_32_LITTLE
3281*a9fa9459Szrj     case Parameters::TARGET_32_LITTLE:
3282*a9fa9459Szrj       this->sized_write_section_symbol<32, false>(os, symtab_xindex, of,
3283*a9fa9459Szrj 						  offset);
3284*a9fa9459Szrj       break;
3285*a9fa9459Szrj #endif
3286*a9fa9459Szrj #ifdef HAVE_TARGET_32_BIG
3287*a9fa9459Szrj     case Parameters::TARGET_32_BIG:
3288*a9fa9459Szrj       this->sized_write_section_symbol<32, true>(os, symtab_xindex, of,
3289*a9fa9459Szrj 						 offset);
3290*a9fa9459Szrj       break;
3291*a9fa9459Szrj #endif
3292*a9fa9459Szrj #ifdef HAVE_TARGET_64_LITTLE
3293*a9fa9459Szrj     case Parameters::TARGET_64_LITTLE:
3294*a9fa9459Szrj       this->sized_write_section_symbol<64, false>(os, symtab_xindex, of,
3295*a9fa9459Szrj 						  offset);
3296*a9fa9459Szrj       break;
3297*a9fa9459Szrj #endif
3298*a9fa9459Szrj #ifdef HAVE_TARGET_64_BIG
3299*a9fa9459Szrj     case Parameters::TARGET_64_BIG:
3300*a9fa9459Szrj       this->sized_write_section_symbol<64, true>(os, symtab_xindex, of,
3301*a9fa9459Szrj 						 offset);
3302*a9fa9459Szrj       break;
3303*a9fa9459Szrj #endif
3304*a9fa9459Szrj     default:
3305*a9fa9459Szrj       gold_unreachable();
3306*a9fa9459Szrj     }
3307*a9fa9459Szrj }
3308*a9fa9459Szrj 
3309*a9fa9459Szrj // Write out a section symbol, specialized for size and endianness.
3310*a9fa9459Szrj 
3311*a9fa9459Szrj template<int size, bool big_endian>
3312*a9fa9459Szrj void
sized_write_section_symbol(const Output_section * os,Output_symtab_xindex * symtab_xindex,Output_file * of,off_t offset) const3313*a9fa9459Szrj Symbol_table::sized_write_section_symbol(const Output_section* os,
3314*a9fa9459Szrj 					 Output_symtab_xindex* symtab_xindex,
3315*a9fa9459Szrj 					 Output_file* of,
3316*a9fa9459Szrj 					 off_t offset) const
3317*a9fa9459Szrj {
3318*a9fa9459Szrj   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
3319*a9fa9459Szrj 
3320*a9fa9459Szrj   unsigned char* pov = of->get_output_view(offset, sym_size);
3321*a9fa9459Szrj 
3322*a9fa9459Szrj   elfcpp::Sym_write<size, big_endian> osym(pov);
3323*a9fa9459Szrj   osym.put_st_name(0);
3324*a9fa9459Szrj   if (parameters->options().relocatable())
3325*a9fa9459Szrj     osym.put_st_value(0);
3326*a9fa9459Szrj   else
3327*a9fa9459Szrj     osym.put_st_value(os->address());
3328*a9fa9459Szrj   osym.put_st_size(0);
3329*a9fa9459Szrj   osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
3330*a9fa9459Szrj 				       elfcpp::STT_SECTION));
3331*a9fa9459Szrj   osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
3332*a9fa9459Szrj 
3333*a9fa9459Szrj   unsigned int shndx = os->out_shndx();
3334*a9fa9459Szrj   if (shndx >= elfcpp::SHN_LORESERVE)
3335*a9fa9459Szrj     {
3336*a9fa9459Szrj       symtab_xindex->add(os->symtab_index(), shndx);
3337*a9fa9459Szrj       shndx = elfcpp::SHN_XINDEX;
3338*a9fa9459Szrj     }
3339*a9fa9459Szrj   osym.put_st_shndx(shndx);
3340*a9fa9459Szrj 
3341*a9fa9459Szrj   of->write_output_view(offset, sym_size, pov);
3342*a9fa9459Szrj }
3343*a9fa9459Szrj 
3344*a9fa9459Szrj // Print statistical information to stderr.  This is used for --stats.
3345*a9fa9459Szrj 
3346*a9fa9459Szrj void
print_stats() const3347*a9fa9459Szrj Symbol_table::print_stats() const
3348*a9fa9459Szrj {
3349*a9fa9459Szrj #if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
3350*a9fa9459Szrj   fprintf(stderr, _("%s: symbol table entries: %zu; buckets: %zu\n"),
3351*a9fa9459Szrj 	  program_name, this->table_.size(), this->table_.bucket_count());
3352*a9fa9459Szrj #else
3353*a9fa9459Szrj   fprintf(stderr, _("%s: symbol table entries: %zu\n"),
3354*a9fa9459Szrj 	  program_name, this->table_.size());
3355*a9fa9459Szrj #endif
3356*a9fa9459Szrj   this->namepool_.print_stats("symbol table stringpool");
3357*a9fa9459Szrj }
3358*a9fa9459Szrj 
3359*a9fa9459Szrj // We check for ODR violations by looking for symbols with the same
3360*a9fa9459Szrj // name for which the debugging information reports that they were
3361*a9fa9459Szrj // defined in disjoint source locations.  When comparing the source
3362*a9fa9459Szrj // location, we consider instances with the same base filename to be
3363*a9fa9459Szrj // the same.  This is because different object files/shared libraries
3364*a9fa9459Szrj // can include the same header file using different paths, and
3365*a9fa9459Szrj // different optimization settings can make the line number appear to
3366*a9fa9459Szrj // be a couple lines off, and we don't want to report an ODR violation
3367*a9fa9459Szrj // in those cases.
3368*a9fa9459Szrj 
3369*a9fa9459Szrj // This struct is used to compare line information, as returned by
3370*a9fa9459Szrj // Dwarf_line_info::one_addr2line.  It implements a < comparison
3371*a9fa9459Szrj // operator used with std::sort.
3372*a9fa9459Szrj 
3373*a9fa9459Szrj struct Odr_violation_compare
3374*a9fa9459Szrj {
3375*a9fa9459Szrj   bool
operator ()gold::Odr_violation_compare3376*a9fa9459Szrj   operator()(const std::string& s1, const std::string& s2) const
3377*a9fa9459Szrj   {
3378*a9fa9459Szrj     // Inputs should be of the form "dirname/filename:linenum" where
3379*a9fa9459Szrj     // "dirname/" is optional.  We want to compare just the filename:linenum.
3380*a9fa9459Szrj 
3381*a9fa9459Szrj     // Find the last '/' in each string.
3382*a9fa9459Szrj     std::string::size_type s1begin = s1.rfind('/');
3383*a9fa9459Szrj     std::string::size_type s2begin = s2.rfind('/');
3384*a9fa9459Szrj     // If there was no '/' in a string, start at the beginning.
3385*a9fa9459Szrj     if (s1begin == std::string::npos)
3386*a9fa9459Szrj       s1begin = 0;
3387*a9fa9459Szrj     if (s2begin == std::string::npos)
3388*a9fa9459Szrj       s2begin = 0;
3389*a9fa9459Szrj     return s1.compare(s1begin, std::string::npos,
3390*a9fa9459Szrj 		      s2, s2begin, std::string::npos) < 0;
3391*a9fa9459Szrj   }
3392*a9fa9459Szrj };
3393*a9fa9459Szrj 
3394*a9fa9459Szrj // Returns all of the lines attached to LOC, not just the one the
3395*a9fa9459Szrj // instruction actually came from.
3396*a9fa9459Szrj std::vector<std::string>
linenos_from_loc(const Task * task,const Symbol_location & loc)3397*a9fa9459Szrj Symbol_table::linenos_from_loc(const Task* task,
3398*a9fa9459Szrj                                const Symbol_location& loc)
3399*a9fa9459Szrj {
3400*a9fa9459Szrj   // We need to lock the object in order to read it.  This
3401*a9fa9459Szrj   // means that we have to run in a singleton Task.  If we
3402*a9fa9459Szrj   // want to run this in a general Task for better
3403*a9fa9459Szrj   // performance, we will need one Task for object, plus
3404*a9fa9459Szrj   // appropriate locking to ensure that we don't conflict with
3405*a9fa9459Szrj   // other uses of the object.  Also note, one_addr2line is not
3406*a9fa9459Szrj   // currently thread-safe.
3407*a9fa9459Szrj   Task_lock_obj<Object> tl(task, loc.object);
3408*a9fa9459Szrj 
3409*a9fa9459Szrj   std::vector<std::string> result;
3410*a9fa9459Szrj   Symbol_location code_loc = loc;
3411*a9fa9459Szrj   parameters->target().function_location(&code_loc);
3412*a9fa9459Szrj   // 16 is the size of the object-cache that one_addr2line should use.
3413*a9fa9459Szrj   std::string canonical_result = Dwarf_line_info::one_addr2line(
3414*a9fa9459Szrj       code_loc.object, code_loc.shndx, code_loc.offset, 16, &result);
3415*a9fa9459Szrj   if (!canonical_result.empty())
3416*a9fa9459Szrj     result.push_back(canonical_result);
3417*a9fa9459Szrj   return result;
3418*a9fa9459Szrj }
3419*a9fa9459Szrj 
3420*a9fa9459Szrj // OutputIterator that records if it was ever assigned to.  This
3421*a9fa9459Szrj // allows it to be used with std::set_intersection() to check for
3422*a9fa9459Szrj // intersection rather than computing the intersection.
3423*a9fa9459Szrj struct Check_intersection
3424*a9fa9459Szrj {
Check_intersectiongold::Check_intersection3425*a9fa9459Szrj   Check_intersection()
3426*a9fa9459Szrj     : value_(false)
3427*a9fa9459Szrj   {}
3428*a9fa9459Szrj 
had_intersectiongold::Check_intersection3429*a9fa9459Szrj   bool had_intersection() const
3430*a9fa9459Szrj   { return this->value_; }
3431*a9fa9459Szrj 
operator ++gold::Check_intersection3432*a9fa9459Szrj   Check_intersection& operator++()
3433*a9fa9459Szrj   { return *this; }
3434*a9fa9459Szrj 
operator *gold::Check_intersection3435*a9fa9459Szrj   Check_intersection& operator*()
3436*a9fa9459Szrj   { return *this; }
3437*a9fa9459Szrj 
3438*a9fa9459Szrj   template<typename T>
operator =gold::Check_intersection3439*a9fa9459Szrj   Check_intersection& operator=(const T&)
3440*a9fa9459Szrj   {
3441*a9fa9459Szrj     this->value_ = true;
3442*a9fa9459Szrj     return *this;
3443*a9fa9459Szrj   }
3444*a9fa9459Szrj 
3445*a9fa9459Szrj  private:
3446*a9fa9459Szrj   bool value_;
3447*a9fa9459Szrj };
3448*a9fa9459Szrj 
3449*a9fa9459Szrj // Check candidate_odr_violations_ to find symbols with the same name
3450*a9fa9459Szrj // but apparently different definitions (different source-file/line-no
3451*a9fa9459Szrj // for each line assigned to the first instruction).
3452*a9fa9459Szrj 
3453*a9fa9459Szrj void
detect_odr_violations(const Task * task,const char * output_file_name) const3454*a9fa9459Szrj Symbol_table::detect_odr_violations(const Task* task,
3455*a9fa9459Szrj 				    const char* output_file_name) const
3456*a9fa9459Szrj {
3457*a9fa9459Szrj   for (Odr_map::const_iterator it = candidate_odr_violations_.begin();
3458*a9fa9459Szrj        it != candidate_odr_violations_.end();
3459*a9fa9459Szrj        ++it)
3460*a9fa9459Szrj     {
3461*a9fa9459Szrj       const char* const symbol_name = it->first;
3462*a9fa9459Szrj 
3463*a9fa9459Szrj       std::string first_object_name;
3464*a9fa9459Szrj       std::vector<std::string> first_object_linenos;
3465*a9fa9459Szrj 
3466*a9fa9459Szrj       Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator
3467*a9fa9459Szrj           locs = it->second.begin();
3468*a9fa9459Szrj       const Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator
3469*a9fa9459Szrj           locs_end = it->second.end();
3470*a9fa9459Szrj       for (; locs != locs_end && first_object_linenos.empty(); ++locs)
3471*a9fa9459Szrj         {
3472*a9fa9459Szrj           // Save the line numbers from the first definition to
3473*a9fa9459Szrj           // compare to the other definitions.  Ideally, we'd compare
3474*a9fa9459Szrj           // every definition to every other, but we don't want to
3475*a9fa9459Szrj           // take O(N^2) time to do this.  This shortcut may cause
3476*a9fa9459Szrj           // false negatives that appear or disappear depending on the
3477*a9fa9459Szrj           // link order, but it won't cause false positives.
3478*a9fa9459Szrj           first_object_name = locs->object->name();
3479*a9fa9459Szrj           first_object_linenos = this->linenos_from_loc(task, *locs);
3480*a9fa9459Szrj         }
3481*a9fa9459Szrj       if (first_object_linenos.empty())
3482*a9fa9459Szrj 	continue;
3483*a9fa9459Szrj 
3484*a9fa9459Szrj       // Sort by Odr_violation_compare to make std::set_intersection work.
3485*a9fa9459Szrj       std::string first_object_canonical_result = first_object_linenos.back();
3486*a9fa9459Szrj       std::sort(first_object_linenos.begin(), first_object_linenos.end(),
3487*a9fa9459Szrj                 Odr_violation_compare());
3488*a9fa9459Szrj 
3489*a9fa9459Szrj       for (; locs != locs_end; ++locs)
3490*a9fa9459Szrj         {
3491*a9fa9459Szrj           std::vector<std::string> linenos =
3492*a9fa9459Szrj               this->linenos_from_loc(task, *locs);
3493*a9fa9459Szrj           // linenos will be empty if we couldn't parse the debug info.
3494*a9fa9459Szrj           if (linenos.empty())
3495*a9fa9459Szrj             continue;
3496*a9fa9459Szrj           // Sort by Odr_violation_compare to make std::set_intersection work.
3497*a9fa9459Szrj           gold_assert(!linenos.empty());
3498*a9fa9459Szrj           std::string second_object_canonical_result = linenos.back();
3499*a9fa9459Szrj           std::sort(linenos.begin(), linenos.end(), Odr_violation_compare());
3500*a9fa9459Szrj 
3501*a9fa9459Szrj           Check_intersection intersection_result =
3502*a9fa9459Szrj               std::set_intersection(first_object_linenos.begin(),
3503*a9fa9459Szrj                                     first_object_linenos.end(),
3504*a9fa9459Szrj                                     linenos.begin(),
3505*a9fa9459Szrj                                     linenos.end(),
3506*a9fa9459Szrj                                     Check_intersection(),
3507*a9fa9459Szrj                                     Odr_violation_compare());
3508*a9fa9459Szrj           if (!intersection_result.had_intersection())
3509*a9fa9459Szrj             {
3510*a9fa9459Szrj               gold_warning(_("while linking %s: symbol '%s' defined in "
3511*a9fa9459Szrj                              "multiple places (possible ODR violation):"),
3512*a9fa9459Szrj                            output_file_name, demangle(symbol_name).c_str());
3513*a9fa9459Szrj               // This only prints one location from each definition,
3514*a9fa9459Szrj               // which may not be the location we expect to intersect
3515*a9fa9459Szrj               // with another definition.  We could print the whole
3516*a9fa9459Szrj               // set of locations, but that seems too verbose.
3517*a9fa9459Szrj               fprintf(stderr, _("  %s from %s\n"),
3518*a9fa9459Szrj                       first_object_canonical_result.c_str(),
3519*a9fa9459Szrj                       first_object_name.c_str());
3520*a9fa9459Szrj               fprintf(stderr, _("  %s from %s\n"),
3521*a9fa9459Szrj                       second_object_canonical_result.c_str(),
3522*a9fa9459Szrj                       locs->object->name().c_str());
3523*a9fa9459Szrj               // Only print one broken pair, to avoid needing to
3524*a9fa9459Szrj               // compare against a list of the disjoint definition
3525*a9fa9459Szrj               // locations we've found so far.  (If we kept comparing
3526*a9fa9459Szrj               // against just the first one, we'd get a lot of
3527*a9fa9459Szrj               // redundant complaints about the second definition
3528*a9fa9459Szrj               // location.)
3529*a9fa9459Szrj               break;
3530*a9fa9459Szrj             }
3531*a9fa9459Szrj         }
3532*a9fa9459Szrj     }
3533*a9fa9459Szrj   // We only call one_addr2line() in this function, so we can clear its cache.
3534*a9fa9459Szrj   Dwarf_line_info::clear_addr2line_cache();
3535*a9fa9459Szrj }
3536*a9fa9459Szrj 
3537*a9fa9459Szrj // Warnings functions.
3538*a9fa9459Szrj 
3539*a9fa9459Szrj // Add a new warning.
3540*a9fa9459Szrj 
3541*a9fa9459Szrj void
add_warning(Symbol_table * symtab,const char * name,Object * obj,const std::string & warning)3542*a9fa9459Szrj Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
3543*a9fa9459Szrj 		      const std::string& warning)
3544*a9fa9459Szrj {
3545*a9fa9459Szrj   name = symtab->canonicalize_name(name);
3546*a9fa9459Szrj   this->warnings_[name].set(obj, warning);
3547*a9fa9459Szrj }
3548*a9fa9459Szrj 
3549*a9fa9459Szrj // Look through the warnings and mark the symbols for which we should
3550*a9fa9459Szrj // warn.  This is called during Layout::finalize when we know the
3551*a9fa9459Szrj // sources for all the symbols.
3552*a9fa9459Szrj 
3553*a9fa9459Szrj void
note_warnings(Symbol_table * symtab)3554*a9fa9459Szrj Warnings::note_warnings(Symbol_table* symtab)
3555*a9fa9459Szrj {
3556*a9fa9459Szrj   for (Warning_table::iterator p = this->warnings_.begin();
3557*a9fa9459Szrj        p != this->warnings_.end();
3558*a9fa9459Szrj        ++p)
3559*a9fa9459Szrj     {
3560*a9fa9459Szrj       Symbol* sym = symtab->lookup(p->first, NULL);
3561*a9fa9459Szrj       if (sym != NULL
3562*a9fa9459Szrj 	  && sym->source() == Symbol::FROM_OBJECT
3563*a9fa9459Szrj 	  && sym->object() == p->second.object)
3564*a9fa9459Szrj 	sym->set_has_warning();
3565*a9fa9459Szrj     }
3566*a9fa9459Szrj }
3567*a9fa9459Szrj 
3568*a9fa9459Szrj // Issue a warning.  This is called when we see a relocation against a
3569*a9fa9459Szrj // symbol for which has a warning.
3570*a9fa9459Szrj 
3571*a9fa9459Szrj template<int size, bool big_endian>
3572*a9fa9459Szrj void
issue_warning(const Symbol * sym,const Relocate_info<size,big_endian> * relinfo,size_t relnum,off_t reloffset) const3573*a9fa9459Szrj Warnings::issue_warning(const Symbol* sym,
3574*a9fa9459Szrj 			const Relocate_info<size, big_endian>* relinfo,
3575*a9fa9459Szrj 			size_t relnum, off_t reloffset) const
3576*a9fa9459Szrj {
3577*a9fa9459Szrj   gold_assert(sym->has_warning());
3578*a9fa9459Szrj 
3579*a9fa9459Szrj   // We don't want to issue a warning for a relocation against the
3580*a9fa9459Szrj   // symbol in the same object file in which the symbol is defined.
3581*a9fa9459Szrj   if (sym->object() == relinfo->object)
3582*a9fa9459Szrj     return;
3583*a9fa9459Szrj 
3584*a9fa9459Szrj   Warning_table::const_iterator p = this->warnings_.find(sym->name());
3585*a9fa9459Szrj   gold_assert(p != this->warnings_.end());
3586*a9fa9459Szrj   gold_warning_at_location(relinfo, relnum, reloffset,
3587*a9fa9459Szrj 			   "%s", p->second.text.c_str());
3588*a9fa9459Szrj }
3589*a9fa9459Szrj 
3590*a9fa9459Szrj // Instantiate the templates we need.  We could use the configure
3591*a9fa9459Szrj // script to restrict this to only the ones needed for implemented
3592*a9fa9459Szrj // targets.
3593*a9fa9459Szrj 
3594*a9fa9459Szrj #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
3595*a9fa9459Szrj template
3596*a9fa9459Szrj void
3597*a9fa9459Szrj Sized_symbol<32>::allocate_common(Output_data*, Value_type);
3598*a9fa9459Szrj #endif
3599*a9fa9459Szrj 
3600*a9fa9459Szrj #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
3601*a9fa9459Szrj template
3602*a9fa9459Szrj void
3603*a9fa9459Szrj Sized_symbol<64>::allocate_common(Output_data*, Value_type);
3604*a9fa9459Szrj #endif
3605*a9fa9459Szrj 
3606*a9fa9459Szrj #ifdef HAVE_TARGET_32_LITTLE
3607*a9fa9459Szrj template
3608*a9fa9459Szrj void
3609*a9fa9459Szrj Symbol_table::add_from_relobj<32, false>(
3610*a9fa9459Szrj     Sized_relobj_file<32, false>* relobj,
3611*a9fa9459Szrj     const unsigned char* syms,
3612*a9fa9459Szrj     size_t count,
3613*a9fa9459Szrj     size_t symndx_offset,
3614*a9fa9459Szrj     const char* sym_names,
3615*a9fa9459Szrj     size_t sym_name_size,
3616*a9fa9459Szrj     Sized_relobj_file<32, false>::Symbols* sympointers,
3617*a9fa9459Szrj     size_t* defined);
3618*a9fa9459Szrj #endif
3619*a9fa9459Szrj 
3620*a9fa9459Szrj #ifdef HAVE_TARGET_32_BIG
3621*a9fa9459Szrj template
3622*a9fa9459Szrj void
3623*a9fa9459Szrj Symbol_table::add_from_relobj<32, true>(
3624*a9fa9459Szrj     Sized_relobj_file<32, true>* relobj,
3625*a9fa9459Szrj     const unsigned char* syms,
3626*a9fa9459Szrj     size_t count,
3627*a9fa9459Szrj     size_t symndx_offset,
3628*a9fa9459Szrj     const char* sym_names,
3629*a9fa9459Szrj     size_t sym_name_size,
3630*a9fa9459Szrj     Sized_relobj_file<32, true>::Symbols* sympointers,
3631*a9fa9459Szrj     size_t* defined);
3632*a9fa9459Szrj #endif
3633*a9fa9459Szrj 
3634*a9fa9459Szrj #ifdef HAVE_TARGET_64_LITTLE
3635*a9fa9459Szrj template
3636*a9fa9459Szrj void
3637*a9fa9459Szrj Symbol_table::add_from_relobj<64, false>(
3638*a9fa9459Szrj     Sized_relobj_file<64, false>* relobj,
3639*a9fa9459Szrj     const unsigned char* syms,
3640*a9fa9459Szrj     size_t count,
3641*a9fa9459Szrj     size_t symndx_offset,
3642*a9fa9459Szrj     const char* sym_names,
3643*a9fa9459Szrj     size_t sym_name_size,
3644*a9fa9459Szrj     Sized_relobj_file<64, false>::Symbols* sympointers,
3645*a9fa9459Szrj     size_t* defined);
3646*a9fa9459Szrj #endif
3647*a9fa9459Szrj 
3648*a9fa9459Szrj #ifdef HAVE_TARGET_64_BIG
3649*a9fa9459Szrj template
3650*a9fa9459Szrj void
3651*a9fa9459Szrj Symbol_table::add_from_relobj<64, true>(
3652*a9fa9459Szrj     Sized_relobj_file<64, true>* relobj,
3653*a9fa9459Szrj     const unsigned char* syms,
3654*a9fa9459Szrj     size_t count,
3655*a9fa9459Szrj     size_t symndx_offset,
3656*a9fa9459Szrj     const char* sym_names,
3657*a9fa9459Szrj     size_t sym_name_size,
3658*a9fa9459Szrj     Sized_relobj_file<64, true>::Symbols* sympointers,
3659*a9fa9459Szrj     size_t* defined);
3660*a9fa9459Szrj #endif
3661*a9fa9459Szrj 
3662*a9fa9459Szrj #ifdef HAVE_TARGET_32_LITTLE
3663*a9fa9459Szrj template
3664*a9fa9459Szrj Symbol*
3665*a9fa9459Szrj Symbol_table::add_from_pluginobj<32, false>(
3666*a9fa9459Szrj     Sized_pluginobj<32, false>* obj,
3667*a9fa9459Szrj     const char* name,
3668*a9fa9459Szrj     const char* ver,
3669*a9fa9459Szrj     elfcpp::Sym<32, false>* sym);
3670*a9fa9459Szrj #endif
3671*a9fa9459Szrj 
3672*a9fa9459Szrj #ifdef HAVE_TARGET_32_BIG
3673*a9fa9459Szrj template
3674*a9fa9459Szrj Symbol*
3675*a9fa9459Szrj Symbol_table::add_from_pluginobj<32, true>(
3676*a9fa9459Szrj     Sized_pluginobj<32, true>* obj,
3677*a9fa9459Szrj     const char* name,
3678*a9fa9459Szrj     const char* ver,
3679*a9fa9459Szrj     elfcpp::Sym<32, true>* sym);
3680*a9fa9459Szrj #endif
3681*a9fa9459Szrj 
3682*a9fa9459Szrj #ifdef HAVE_TARGET_64_LITTLE
3683*a9fa9459Szrj template
3684*a9fa9459Szrj Symbol*
3685*a9fa9459Szrj Symbol_table::add_from_pluginobj<64, false>(
3686*a9fa9459Szrj     Sized_pluginobj<64, false>* obj,
3687*a9fa9459Szrj     const char* name,
3688*a9fa9459Szrj     const char* ver,
3689*a9fa9459Szrj     elfcpp::Sym<64, false>* sym);
3690*a9fa9459Szrj #endif
3691*a9fa9459Szrj 
3692*a9fa9459Szrj #ifdef HAVE_TARGET_64_BIG
3693*a9fa9459Szrj template
3694*a9fa9459Szrj Symbol*
3695*a9fa9459Szrj Symbol_table::add_from_pluginobj<64, true>(
3696*a9fa9459Szrj     Sized_pluginobj<64, true>* obj,
3697*a9fa9459Szrj     const char* name,
3698*a9fa9459Szrj     const char* ver,
3699*a9fa9459Szrj     elfcpp::Sym<64, true>* sym);
3700*a9fa9459Szrj #endif
3701*a9fa9459Szrj 
3702*a9fa9459Szrj #ifdef HAVE_TARGET_32_LITTLE
3703*a9fa9459Szrj template
3704*a9fa9459Szrj void
3705*a9fa9459Szrj Symbol_table::add_from_dynobj<32, false>(
3706*a9fa9459Szrj     Sized_dynobj<32, false>* dynobj,
3707*a9fa9459Szrj     const unsigned char* syms,
3708*a9fa9459Szrj     size_t count,
3709*a9fa9459Szrj     const char* sym_names,
3710*a9fa9459Szrj     size_t sym_name_size,
3711*a9fa9459Szrj     const unsigned char* versym,
3712*a9fa9459Szrj     size_t versym_size,
3713*a9fa9459Szrj     const std::vector<const char*>* version_map,
3714*a9fa9459Szrj     Sized_relobj_file<32, false>::Symbols* sympointers,
3715*a9fa9459Szrj     size_t* defined);
3716*a9fa9459Szrj #endif
3717*a9fa9459Szrj 
3718*a9fa9459Szrj #ifdef HAVE_TARGET_32_BIG
3719*a9fa9459Szrj template
3720*a9fa9459Szrj void
3721*a9fa9459Szrj Symbol_table::add_from_dynobj<32, true>(
3722*a9fa9459Szrj     Sized_dynobj<32, true>* dynobj,
3723*a9fa9459Szrj     const unsigned char* syms,
3724*a9fa9459Szrj     size_t count,
3725*a9fa9459Szrj     const char* sym_names,
3726*a9fa9459Szrj     size_t sym_name_size,
3727*a9fa9459Szrj     const unsigned char* versym,
3728*a9fa9459Szrj     size_t versym_size,
3729*a9fa9459Szrj     const std::vector<const char*>* version_map,
3730*a9fa9459Szrj     Sized_relobj_file<32, true>::Symbols* sympointers,
3731*a9fa9459Szrj     size_t* defined);
3732*a9fa9459Szrj #endif
3733*a9fa9459Szrj 
3734*a9fa9459Szrj #ifdef HAVE_TARGET_64_LITTLE
3735*a9fa9459Szrj template
3736*a9fa9459Szrj void
3737*a9fa9459Szrj Symbol_table::add_from_dynobj<64, false>(
3738*a9fa9459Szrj     Sized_dynobj<64, false>* dynobj,
3739*a9fa9459Szrj     const unsigned char* syms,
3740*a9fa9459Szrj     size_t count,
3741*a9fa9459Szrj     const char* sym_names,
3742*a9fa9459Szrj     size_t sym_name_size,
3743*a9fa9459Szrj     const unsigned char* versym,
3744*a9fa9459Szrj     size_t versym_size,
3745*a9fa9459Szrj     const std::vector<const char*>* version_map,
3746*a9fa9459Szrj     Sized_relobj_file<64, false>::Symbols* sympointers,
3747*a9fa9459Szrj     size_t* defined);
3748*a9fa9459Szrj #endif
3749*a9fa9459Szrj 
3750*a9fa9459Szrj #ifdef HAVE_TARGET_64_BIG
3751*a9fa9459Szrj template
3752*a9fa9459Szrj void
3753*a9fa9459Szrj Symbol_table::add_from_dynobj<64, true>(
3754*a9fa9459Szrj     Sized_dynobj<64, true>* dynobj,
3755*a9fa9459Szrj     const unsigned char* syms,
3756*a9fa9459Szrj     size_t count,
3757*a9fa9459Szrj     const char* sym_names,
3758*a9fa9459Szrj     size_t sym_name_size,
3759*a9fa9459Szrj     const unsigned char* versym,
3760*a9fa9459Szrj     size_t versym_size,
3761*a9fa9459Szrj     const std::vector<const char*>* version_map,
3762*a9fa9459Szrj     Sized_relobj_file<64, true>::Symbols* sympointers,
3763*a9fa9459Szrj     size_t* defined);
3764*a9fa9459Szrj #endif
3765*a9fa9459Szrj 
3766*a9fa9459Szrj #ifdef HAVE_TARGET_32_LITTLE
3767*a9fa9459Szrj template
3768*a9fa9459Szrj Sized_symbol<32>*
3769*a9fa9459Szrj Symbol_table::add_from_incrobj(
3770*a9fa9459Szrj     Object* obj,
3771*a9fa9459Szrj     const char* name,
3772*a9fa9459Szrj     const char* ver,
3773*a9fa9459Szrj     elfcpp::Sym<32, false>* sym);
3774*a9fa9459Szrj #endif
3775*a9fa9459Szrj 
3776*a9fa9459Szrj #ifdef HAVE_TARGET_32_BIG
3777*a9fa9459Szrj template
3778*a9fa9459Szrj Sized_symbol<32>*
3779*a9fa9459Szrj Symbol_table::add_from_incrobj(
3780*a9fa9459Szrj     Object* obj,
3781*a9fa9459Szrj     const char* name,
3782*a9fa9459Szrj     const char* ver,
3783*a9fa9459Szrj     elfcpp::Sym<32, true>* sym);
3784*a9fa9459Szrj #endif
3785*a9fa9459Szrj 
3786*a9fa9459Szrj #ifdef HAVE_TARGET_64_LITTLE
3787*a9fa9459Szrj template
3788*a9fa9459Szrj Sized_symbol<64>*
3789*a9fa9459Szrj Symbol_table::add_from_incrobj(
3790*a9fa9459Szrj     Object* obj,
3791*a9fa9459Szrj     const char* name,
3792*a9fa9459Szrj     const char* ver,
3793*a9fa9459Szrj     elfcpp::Sym<64, false>* sym);
3794*a9fa9459Szrj #endif
3795*a9fa9459Szrj 
3796*a9fa9459Szrj #ifdef HAVE_TARGET_64_BIG
3797*a9fa9459Szrj template
3798*a9fa9459Szrj Sized_symbol<64>*
3799*a9fa9459Szrj Symbol_table::add_from_incrobj(
3800*a9fa9459Szrj     Object* obj,
3801*a9fa9459Szrj     const char* name,
3802*a9fa9459Szrj     const char* ver,
3803*a9fa9459Szrj     elfcpp::Sym<64, true>* sym);
3804*a9fa9459Szrj #endif
3805*a9fa9459Szrj 
3806*a9fa9459Szrj #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
3807*a9fa9459Szrj template
3808*a9fa9459Szrj void
3809*a9fa9459Szrj Symbol_table::define_with_copy_reloc<32>(
3810*a9fa9459Szrj     Sized_symbol<32>* sym,
3811*a9fa9459Szrj     Output_data* posd,
3812*a9fa9459Szrj     elfcpp::Elf_types<32>::Elf_Addr value);
3813*a9fa9459Szrj #endif
3814*a9fa9459Szrj 
3815*a9fa9459Szrj #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
3816*a9fa9459Szrj template
3817*a9fa9459Szrj void
3818*a9fa9459Szrj Symbol_table::define_with_copy_reloc<64>(
3819*a9fa9459Szrj     Sized_symbol<64>* sym,
3820*a9fa9459Szrj     Output_data* posd,
3821*a9fa9459Szrj     elfcpp::Elf_types<64>::Elf_Addr value);
3822*a9fa9459Szrj #endif
3823*a9fa9459Szrj 
3824*a9fa9459Szrj #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
3825*a9fa9459Szrj template
3826*a9fa9459Szrj void
3827*a9fa9459Szrj Sized_symbol<32>::init_output_data(const char* name, const char* version,
3828*a9fa9459Szrj 				   Output_data* od, Value_type value,
3829*a9fa9459Szrj 				   Size_type symsize, elfcpp::STT type,
3830*a9fa9459Szrj 				   elfcpp::STB binding,
3831*a9fa9459Szrj 				   elfcpp::STV visibility,
3832*a9fa9459Szrj 				   unsigned char nonvis,
3833*a9fa9459Szrj 				   bool offset_is_from_end,
3834*a9fa9459Szrj 				   bool is_predefined);
3835*a9fa9459Szrj 
3836*a9fa9459Szrj template
3837*a9fa9459Szrj void
3838*a9fa9459Szrj Sized_symbol<32>::init_constant(const char* name, const char* version,
3839*a9fa9459Szrj 				Value_type value, Size_type symsize,
3840*a9fa9459Szrj 				elfcpp::STT type, elfcpp::STB binding,
3841*a9fa9459Szrj 				elfcpp::STV visibility, unsigned char nonvis,
3842*a9fa9459Szrj 				bool is_predefined);
3843*a9fa9459Szrj 
3844*a9fa9459Szrj template
3845*a9fa9459Szrj void
3846*a9fa9459Szrj Sized_symbol<32>::init_undefined(const char* name, const char* version,
3847*a9fa9459Szrj 				 Value_type value, elfcpp::STT type,
3848*a9fa9459Szrj 				 elfcpp::STB binding, elfcpp::STV visibility,
3849*a9fa9459Szrj 				 unsigned char nonvis);
3850*a9fa9459Szrj #endif
3851*a9fa9459Szrj 
3852*a9fa9459Szrj #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
3853*a9fa9459Szrj template
3854*a9fa9459Szrj void
3855*a9fa9459Szrj Sized_symbol<64>::init_output_data(const char* name, const char* version,
3856*a9fa9459Szrj 				   Output_data* od, Value_type value,
3857*a9fa9459Szrj 				   Size_type symsize, elfcpp::STT type,
3858*a9fa9459Szrj 				   elfcpp::STB binding,
3859*a9fa9459Szrj 				   elfcpp::STV visibility,
3860*a9fa9459Szrj 				   unsigned char nonvis,
3861*a9fa9459Szrj 				   bool offset_is_from_end,
3862*a9fa9459Szrj 				   bool is_predefined);
3863*a9fa9459Szrj 
3864*a9fa9459Szrj template
3865*a9fa9459Szrj void
3866*a9fa9459Szrj Sized_symbol<64>::init_constant(const char* name, const char* version,
3867*a9fa9459Szrj 				Value_type value, Size_type symsize,
3868*a9fa9459Szrj 				elfcpp::STT type, elfcpp::STB binding,
3869*a9fa9459Szrj 				elfcpp::STV visibility, unsigned char nonvis,
3870*a9fa9459Szrj 				bool is_predefined);
3871*a9fa9459Szrj 
3872*a9fa9459Szrj template
3873*a9fa9459Szrj void
3874*a9fa9459Szrj Sized_symbol<64>::init_undefined(const char* name, const char* version,
3875*a9fa9459Szrj 				 Value_type value, elfcpp::STT type,
3876*a9fa9459Szrj 				 elfcpp::STB binding, elfcpp::STV visibility,
3877*a9fa9459Szrj 				 unsigned char nonvis);
3878*a9fa9459Szrj #endif
3879*a9fa9459Szrj 
3880*a9fa9459Szrj #ifdef HAVE_TARGET_32_LITTLE
3881*a9fa9459Szrj template
3882*a9fa9459Szrj void
3883*a9fa9459Szrj Warnings::issue_warning<32, false>(const Symbol* sym,
3884*a9fa9459Szrj 				   const Relocate_info<32, false>* relinfo,
3885*a9fa9459Szrj 				   size_t relnum, off_t reloffset) const;
3886*a9fa9459Szrj #endif
3887*a9fa9459Szrj 
3888*a9fa9459Szrj #ifdef HAVE_TARGET_32_BIG
3889*a9fa9459Szrj template
3890*a9fa9459Szrj void
3891*a9fa9459Szrj Warnings::issue_warning<32, true>(const Symbol* sym,
3892*a9fa9459Szrj 				  const Relocate_info<32, true>* relinfo,
3893*a9fa9459Szrj 				  size_t relnum, off_t reloffset) const;
3894*a9fa9459Szrj #endif
3895*a9fa9459Szrj 
3896*a9fa9459Szrj #ifdef HAVE_TARGET_64_LITTLE
3897*a9fa9459Szrj template
3898*a9fa9459Szrj void
3899*a9fa9459Szrj Warnings::issue_warning<64, false>(const Symbol* sym,
3900*a9fa9459Szrj 				   const Relocate_info<64, false>* relinfo,
3901*a9fa9459Szrj 				   size_t relnum, off_t reloffset) const;
3902*a9fa9459Szrj #endif
3903*a9fa9459Szrj 
3904*a9fa9459Szrj #ifdef HAVE_TARGET_64_BIG
3905*a9fa9459Szrj template
3906*a9fa9459Szrj void
3907*a9fa9459Szrj Warnings::issue_warning<64, true>(const Symbol* sym,
3908*a9fa9459Szrj 				  const Relocate_info<64, true>* relinfo,
3909*a9fa9459Szrj 				  size_t relnum, off_t reloffset) const;
3910*a9fa9459Szrj #endif
3911*a9fa9459Szrj 
3912*a9fa9459Szrj } // End namespace gold.
3913