Commit | Line | Data |
---|---|---|
14bfc3f5 ILT |
1 | // symtab.cc -- the gold symbol table |
2 | ||
3 | #include "gold.h" | |
4 | ||
5 | #include <cassert> | |
6 | #include <stdint.h> | |
7 | #include <string> | |
8 | #include <utility> | |
9 | ||
10 | #include "object.h" | |
dbe717ef | 11 | #include "dynobj.h" |
75f65a3e | 12 | #include "output.h" |
61ba1cf9 | 13 | #include "target.h" |
645f8123 | 14 | #include "workqueue.h" |
14bfc3f5 ILT |
15 | #include "symtab.h" |
16 | ||
17 | namespace gold | |
18 | { | |
19 | ||
20 | // Class Symbol. | |
21 | ||
ead1e424 ILT |
22 | // Initialize fields in Symbol. This initializes everything except u_ |
23 | // and source_. | |
14bfc3f5 | 24 | |
14bfc3f5 | 25 | void |
ead1e424 ILT |
26 | Symbol::init_fields(const char* name, const char* version, |
27 | elfcpp::STT type, elfcpp::STB binding, | |
28 | elfcpp::STV visibility, unsigned char nonvis) | |
14bfc3f5 ILT |
29 | { |
30 | this->name_ = name; | |
31 | this->version_ = version; | |
c06b7b0b ILT |
32 | this->symtab_index_ = 0; |
33 | this->dynsym_index_ = 0; | |
ead1e424 ILT |
34 | this->got_offset_ = 0; |
35 | this->type_ = type; | |
36 | this->binding_ = binding; | |
37 | this->visibility_ = visibility; | |
38 | this->nonvis_ = nonvis; | |
39 | this->is_target_special_ = false; | |
1564db8d ILT |
40 | this->is_def_ = false; |
41 | this->is_forwarder_ = false; | |
c06b7b0b | 42 | this->needs_dynsym_entry_ = false; |
ead1e424 ILT |
43 | this->in_dyn_ = false; |
44 | this->has_got_offset_ = false; | |
f6ce93d6 | 45 | this->has_warning_ = false; |
ead1e424 ILT |
46 | } |
47 | ||
48 | // Initialize the fields in the base class Symbol for SYM in OBJECT. | |
49 | ||
50 | template<int size, bool big_endian> | |
51 | void | |
52 | Symbol::init_base(const char* name, const char* version, Object* object, | |
53 | const elfcpp::Sym<size, big_endian>& sym) | |
54 | { | |
55 | this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(), | |
56 | sym.get_st_visibility(), sym.get_st_nonvis()); | |
57 | this->u_.from_object.object = object; | |
58 | // FIXME: Handle SHN_XINDEX. | |
59 | this->u_.from_object.shnum = sym.get_st_shndx(); | |
60 | this->source_ = FROM_OBJECT; | |
1564db8d | 61 | this->in_dyn_ = object->is_dynamic(); |
14bfc3f5 ILT |
62 | } |
63 | ||
ead1e424 ILT |
64 | // Initialize the fields in the base class Symbol for a symbol defined |
65 | // in an Output_data. | |
66 | ||
67 | void | |
68 | Symbol::init_base(const char* name, Output_data* od, elfcpp::STT type, | |
69 | elfcpp::STB binding, elfcpp::STV visibility, | |
70 | unsigned char nonvis, bool offset_is_from_end) | |
71 | { | |
72 | this->init_fields(name, NULL, type, binding, visibility, nonvis); | |
73 | this->u_.in_output_data.output_data = od; | |
74 | this->u_.in_output_data.offset_is_from_end = offset_is_from_end; | |
75 | this->source_ = IN_OUTPUT_DATA; | |
76 | } | |
77 | ||
78 | // Initialize the fields in the base class Symbol for a symbol defined | |
79 | // in an Output_segment. | |
80 | ||
81 | void | |
82 | Symbol::init_base(const char* name, Output_segment* os, elfcpp::STT type, | |
83 | elfcpp::STB binding, elfcpp::STV visibility, | |
84 | unsigned char nonvis, Segment_offset_base offset_base) | |
85 | { | |
86 | this->init_fields(name, NULL, type, binding, visibility, nonvis); | |
87 | this->u_.in_output_segment.output_segment = os; | |
88 | this->u_.in_output_segment.offset_base = offset_base; | |
89 | this->source_ = IN_OUTPUT_SEGMENT; | |
90 | } | |
91 | ||
92 | // Initialize the fields in the base class Symbol for a symbol defined | |
93 | // as a constant. | |
94 | ||
95 | void | |
96 | Symbol::init_base(const char* name, elfcpp::STT type, | |
97 | elfcpp::STB binding, elfcpp::STV visibility, | |
98 | unsigned char nonvis) | |
99 | { | |
100 | this->init_fields(name, NULL, type, binding, visibility, nonvis); | |
101 | this->source_ = CONSTANT; | |
102 | } | |
103 | ||
104 | // Initialize the fields in Sized_symbol for SYM in OBJECT. | |
14bfc3f5 ILT |
105 | |
106 | template<int size> | |
107 | template<bool big_endian> | |
108 | void | |
109 | Sized_symbol<size>::init(const char* name, const char* version, Object* object, | |
110 | const elfcpp::Sym<size, big_endian>& sym) | |
111 | { | |
112 | this->init_base(name, version, object, sym); | |
113 | this->value_ = sym.get_st_value(); | |
ead1e424 ILT |
114 | this->symsize_ = sym.get_st_size(); |
115 | } | |
116 | ||
117 | // Initialize the fields in Sized_symbol for a symbol defined in an | |
118 | // Output_data. | |
119 | ||
120 | template<int size> | |
121 | void | |
122 | Sized_symbol<size>::init(const char* name, Output_data* od, | |
123 | Value_type value, Size_type symsize, | |
124 | elfcpp::STT type, elfcpp::STB binding, | |
125 | elfcpp::STV visibility, unsigned char nonvis, | |
126 | bool offset_is_from_end) | |
127 | { | |
128 | this->init_base(name, od, type, binding, visibility, nonvis, | |
129 | offset_is_from_end); | |
130 | this->value_ = value; | |
131 | this->symsize_ = symsize; | |
132 | } | |
133 | ||
134 | // Initialize the fields in Sized_symbol for a symbol defined in an | |
135 | // Output_segment. | |
136 | ||
137 | template<int size> | |
138 | void | |
139 | Sized_symbol<size>::init(const char* name, Output_segment* os, | |
140 | Value_type value, Size_type symsize, | |
141 | elfcpp::STT type, elfcpp::STB binding, | |
142 | elfcpp::STV visibility, unsigned char nonvis, | |
143 | Segment_offset_base offset_base) | |
144 | { | |
145 | this->init_base(name, os, type, binding, visibility, nonvis, offset_base); | |
146 | this->value_ = value; | |
147 | this->symsize_ = symsize; | |
148 | } | |
149 | ||
150 | // Initialize the fields in Sized_symbol for a symbol defined as a | |
151 | // constant. | |
152 | ||
153 | template<int size> | |
154 | void | |
155 | Sized_symbol<size>::init(const char* name, Value_type value, Size_type symsize, | |
156 | elfcpp::STT type, elfcpp::STB binding, | |
157 | elfcpp::STV visibility, unsigned char nonvis) | |
158 | { | |
159 | this->init_base(name, type, binding, visibility, nonvis); | |
160 | this->value_ = value; | |
161 | this->symsize_ = symsize; | |
14bfc3f5 ILT |
162 | } |
163 | ||
164 | // Class Symbol_table. | |
165 | ||
166 | Symbol_table::Symbol_table() | |
ead1e424 | 167 | : size_(0), saw_undefined_(0), offset_(0), table_(), namepool_(), |
f6ce93d6 | 168 | forwarders_(), commons_(), warnings_() |
14bfc3f5 ILT |
169 | { |
170 | } | |
171 | ||
172 | Symbol_table::~Symbol_table() | |
173 | { | |
174 | } | |
175 | ||
176 | // The hash function. The key is always canonicalized, so we use a | |
177 | // simple combination of the pointers. | |
178 | ||
179 | size_t | |
180 | Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const | |
181 | { | |
f0641a0b | 182 | return key.first ^ key.second; |
14bfc3f5 ILT |
183 | } |
184 | ||
185 | // The symbol table key equality function. This is only called with | |
186 | // canonicalized name and version strings, so we can use pointer | |
187 | // comparison. | |
188 | ||
189 | bool | |
190 | Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1, | |
191 | const Symbol_table_key& k2) const | |
192 | { | |
193 | return k1.first == k2.first && k1.second == k2.second; | |
194 | } | |
195 | ||
196 | // Make TO a symbol which forwards to FROM. | |
197 | ||
198 | void | |
199 | Symbol_table::make_forwarder(Symbol* from, Symbol* to) | |
200 | { | |
dbe717ef | 201 | assert(from != to); |
14bfc3f5 ILT |
202 | assert(!from->is_forwarder() && !to->is_forwarder()); |
203 | this->forwarders_[from] = to; | |
204 | from->set_forwarder(); | |
205 | } | |
206 | ||
61ba1cf9 ILT |
207 | // Resolve the forwards from FROM, returning the real symbol. |
208 | ||
14bfc3f5 | 209 | Symbol* |
c06b7b0b | 210 | Symbol_table::resolve_forwards(const Symbol* from) const |
14bfc3f5 ILT |
211 | { |
212 | assert(from->is_forwarder()); | |
c06b7b0b | 213 | Unordered_map<const Symbol*, Symbol*>::const_iterator p = |
14bfc3f5 ILT |
214 | this->forwarders_.find(from); |
215 | assert(p != this->forwarders_.end()); | |
216 | return p->second; | |
217 | } | |
218 | ||
61ba1cf9 ILT |
219 | // Look up a symbol by name. |
220 | ||
221 | Symbol* | |
222 | Symbol_table::lookup(const char* name, const char* version) const | |
223 | { | |
f0641a0b ILT |
224 | Stringpool::Key name_key; |
225 | name = this->namepool_.find(name, &name_key); | |
61ba1cf9 ILT |
226 | if (name == NULL) |
227 | return NULL; | |
f0641a0b ILT |
228 | |
229 | Stringpool::Key version_key = 0; | |
61ba1cf9 ILT |
230 | if (version != NULL) |
231 | { | |
f0641a0b | 232 | version = this->namepool_.find(version, &version_key); |
61ba1cf9 ILT |
233 | if (version == NULL) |
234 | return NULL; | |
235 | } | |
236 | ||
f0641a0b | 237 | Symbol_table_key key(name_key, version_key); |
61ba1cf9 ILT |
238 | Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key); |
239 | if (p == this->table_.end()) | |
240 | return NULL; | |
241 | return p->second; | |
242 | } | |
243 | ||
14bfc3f5 ILT |
244 | // Resolve a Symbol with another Symbol. This is only used in the |
245 | // unusual case where there are references to both an unversioned | |
246 | // symbol and a symbol with a version, and we then discover that that | |
1564db8d ILT |
247 | // version is the default version. Because this is unusual, we do |
248 | // this the slow way, by converting back to an ELF symbol. | |
14bfc3f5 | 249 | |
1564db8d | 250 | template<int size, bool big_endian> |
14bfc3f5 | 251 | void |
5482377d ILT |
252 | Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from |
253 | ACCEPT_SIZE_ENDIAN) | |
14bfc3f5 | 254 | { |
1564db8d ILT |
255 | unsigned char buf[elfcpp::Elf_sizes<size>::sym_size]; |
256 | elfcpp::Sym_write<size, big_endian> esym(buf); | |
257 | // We don't bother to set the st_name field. | |
258 | esym.put_st_value(from->value()); | |
259 | esym.put_st_size(from->symsize()); | |
260 | esym.put_st_info(from->binding(), from->type()); | |
ead1e424 | 261 | esym.put_st_other(from->visibility(), from->nonvis()); |
1564db8d ILT |
262 | esym.put_st_shndx(from->shnum()); |
263 | Symbol_table::resolve(to, esym.sym(), from->object()); | |
14bfc3f5 ILT |
264 | } |
265 | ||
266 | // Add one symbol from OBJECT to the symbol table. NAME is symbol | |
267 | // name and VERSION is the version; both are canonicalized. DEF is | |
268 | // whether this is the default version. | |
269 | ||
270 | // If DEF is true, then this is the definition of a default version of | |
271 | // a symbol. That means that any lookup of NAME/NULL and any lookup | |
272 | // of NAME/VERSION should always return the same symbol. This is | |
273 | // obvious for references, but in particular we want to do this for | |
274 | // definitions: overriding NAME/NULL should also override | |
275 | // NAME/VERSION. If we don't do that, it would be very hard to | |
276 | // override functions in a shared library which uses versioning. | |
277 | ||
278 | // We implement this by simply making both entries in the hash table | |
279 | // point to the same Symbol structure. That is easy enough if this is | |
280 | // the first time we see NAME/NULL or NAME/VERSION, but it is possible | |
281 | // that we have seen both already, in which case they will both have | |
282 | // independent entries in the symbol table. We can't simply change | |
283 | // the symbol table entry, because we have pointers to the entries | |
284 | // attached to the object files. So we mark the entry attached to the | |
285 | // object file as a forwarder, and record it in the forwarders_ map. | |
286 | // Note that entries in the hash table will never be marked as | |
287 | // forwarders. | |
288 | ||
289 | template<int size, bool big_endian> | |
290 | Symbol* | |
f6ce93d6 | 291 | Symbol_table::add_from_object(Object* object, |
14bfc3f5 | 292 | const char *name, |
f0641a0b ILT |
293 | Stringpool::Key name_key, |
294 | const char *version, | |
295 | Stringpool::Key version_key, | |
296 | bool def, | |
14bfc3f5 ILT |
297 | const elfcpp::Sym<size, big_endian>& sym) |
298 | { | |
299 | Symbol* const snull = NULL; | |
300 | std::pair<typename Symbol_table_type::iterator, bool> ins = | |
f0641a0b ILT |
301 | this->table_.insert(std::make_pair(std::make_pair(name_key, version_key), |
302 | snull)); | |
14bfc3f5 ILT |
303 | |
304 | std::pair<typename Symbol_table_type::iterator, bool> insdef = | |
305 | std::make_pair(this->table_.end(), false); | |
306 | if (def) | |
307 | { | |
f0641a0b ILT |
308 | const Stringpool::Key vnull_key = 0; |
309 | insdef = this->table_.insert(std::make_pair(std::make_pair(name_key, | |
310 | vnull_key), | |
14bfc3f5 ILT |
311 | snull)); |
312 | } | |
313 | ||
314 | // ins.first: an iterator, which is a pointer to a pair. | |
315 | // ins.first->first: the key (a pair of name and version). | |
316 | // ins.first->second: the value (Symbol*). | |
317 | // ins.second: true if new entry was inserted, false if not. | |
318 | ||
1564db8d | 319 | Sized_symbol<size>* ret; |
ead1e424 ILT |
320 | bool was_undefined; |
321 | bool was_common; | |
14bfc3f5 ILT |
322 | if (!ins.second) |
323 | { | |
324 | // We already have an entry for NAME/VERSION. | |
593f47df ILT |
325 | ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (ins.first->second |
326 | SELECT_SIZE(size)); | |
14bfc3f5 | 327 | assert(ret != NULL); |
ead1e424 ILT |
328 | |
329 | was_undefined = ret->is_undefined(); | |
330 | was_common = ret->is_common(); | |
331 | ||
14bfc3f5 ILT |
332 | Symbol_table::resolve(ret, sym, object); |
333 | ||
334 | if (def) | |
335 | { | |
336 | if (insdef.second) | |
337 | { | |
338 | // This is the first time we have seen NAME/NULL. Make | |
339 | // NAME/NULL point to NAME/VERSION. | |
340 | insdef.first->second = ret; | |
341 | } | |
dbe717ef | 342 | else if (insdef.first->second != ret) |
14bfc3f5 ILT |
343 | { |
344 | // This is the unfortunate case where we already have | |
345 | // entries for both NAME/VERSION and NAME/NULL. | |
274e99f9 | 346 | const Sized_symbol<size>* sym2; |
593f47df | 347 | sym2 = this->get_sized_symbol SELECT_SIZE_NAME(size) ( |
5482377d ILT |
348 | insdef.first->second |
349 | SELECT_SIZE(size)); | |
593f47df | 350 | Symbol_table::resolve SELECT_SIZE_ENDIAN_NAME(size, big_endian) ( |
5482377d | 351 | ret, sym2 SELECT_SIZE_ENDIAN(size, big_endian)); |
14bfc3f5 ILT |
352 | this->make_forwarder(insdef.first->second, ret); |
353 | insdef.first->second = ret; | |
354 | } | |
355 | } | |
356 | } | |
357 | else | |
358 | { | |
359 | // This is the first time we have seen NAME/VERSION. | |
360 | assert(ins.first->second == NULL); | |
ead1e424 ILT |
361 | |
362 | was_undefined = false; | |
363 | was_common = false; | |
364 | ||
14bfc3f5 ILT |
365 | if (def && !insdef.second) |
366 | { | |
367 | // We already have an entry for NAME/NULL. Make | |
368 | // NAME/VERSION point to it. | |
593f47df ILT |
369 | ret = this->get_sized_symbol SELECT_SIZE_NAME(size) ( |
370 | insdef.first->second | |
371 | SELECT_SIZE(size)); | |
14bfc3f5 ILT |
372 | Symbol_table::resolve(ret, sym, object); |
373 | ins.first->second = ret; | |
374 | } | |
375 | else | |
376 | { | |
f6ce93d6 ILT |
377 | Sized_target<size, big_endian>* target = |
378 | object->sized_target SELECT_SIZE_ENDIAN_NAME(size, big_endian) ( | |
379 | SELECT_SIZE_ENDIAN_ONLY(size, big_endian)); | |
1564db8d ILT |
380 | if (!target->has_make_symbol()) |
381 | ret = new Sized_symbol<size>(); | |
382 | else | |
14bfc3f5 | 383 | { |
1564db8d ILT |
384 | ret = target->make_symbol(); |
385 | if (ret == NULL) | |
14bfc3f5 ILT |
386 | { |
387 | // This means that we don't want a symbol table | |
388 | // entry after all. | |
389 | if (!def) | |
390 | this->table_.erase(ins.first); | |
391 | else | |
392 | { | |
393 | this->table_.erase(insdef.first); | |
394 | // Inserting insdef invalidated ins. | |
f0641a0b ILT |
395 | this->table_.erase(std::make_pair(name_key, |
396 | version_key)); | |
14bfc3f5 ILT |
397 | } |
398 | return NULL; | |
399 | } | |
400 | } | |
14bfc3f5 | 401 | |
1564db8d ILT |
402 | ret->init(name, version, object, sym); |
403 | ||
14bfc3f5 ILT |
404 | ins.first->second = ret; |
405 | if (def) | |
406 | { | |
407 | // This is the first time we have seen NAME/NULL. Point | |
408 | // it at the new entry for NAME/VERSION. | |
409 | assert(insdef.second); | |
410 | insdef.first->second = ret; | |
411 | } | |
412 | } | |
413 | } | |
414 | ||
ead1e424 ILT |
415 | // Record every time we see a new undefined symbol, to speed up |
416 | // archive groups. | |
417 | if (!was_undefined && ret->is_undefined()) | |
418 | ++this->saw_undefined_; | |
419 | ||
420 | // Keep track of common symbols, to speed up common symbol | |
421 | // allocation. | |
422 | if (!was_common && ret->is_common()) | |
423 | this->commons_.push_back(ret); | |
424 | ||
14bfc3f5 ILT |
425 | return ret; |
426 | } | |
427 | ||
f6ce93d6 | 428 | // Add all the symbols in a relocatable object to the hash table. |
14bfc3f5 ILT |
429 | |
430 | template<int size, bool big_endian> | |
431 | void | |
dbe717ef ILT |
432 | Symbol_table::add_from_relobj( |
433 | Sized_relobj<size, big_endian>* relobj, | |
f6ce93d6 | 434 | const unsigned char* syms, |
14bfc3f5 ILT |
435 | size_t count, |
436 | const char* sym_names, | |
437 | size_t sym_name_size, | |
438 | Symbol** sympointers) | |
439 | { | |
440 | // We take the size from the first object we see. | |
441 | if (this->get_size() == 0) | |
442 | this->set_size(size); | |
443 | ||
dbe717ef | 444 | if (size != this->get_size() || size != relobj->target()->get_size()) |
14bfc3f5 ILT |
445 | { |
446 | fprintf(stderr, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"), | |
dbe717ef | 447 | program_name, relobj->name().c_str()); |
14bfc3f5 ILT |
448 | gold_exit(false); |
449 | } | |
450 | ||
a783673b ILT |
451 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; |
452 | ||
f6ce93d6 | 453 | const unsigned char* p = syms; |
a783673b | 454 | for (size_t i = 0; i < count; ++i, p += sym_size) |
14bfc3f5 ILT |
455 | { |
456 | elfcpp::Sym<size, big_endian> sym(p); | |
a783673b | 457 | elfcpp::Sym<size, big_endian>* psym = &sym; |
14bfc3f5 | 458 | |
a783673b | 459 | unsigned int st_name = psym->get_st_name(); |
14bfc3f5 ILT |
460 | if (st_name >= sym_name_size) |
461 | { | |
54dc6425 ILT |
462 | fprintf(stderr, |
463 | _("%s: %s: bad global symbol name offset %u at %lu\n"), | |
dbe717ef | 464 | program_name, relobj->name().c_str(), st_name, |
14bfc3f5 ILT |
465 | static_cast<unsigned long>(i)); |
466 | gold_exit(false); | |
467 | } | |
468 | ||
dbe717ef ILT |
469 | const char* name = sym_names + st_name; |
470 | ||
a783673b ILT |
471 | // A symbol defined in a section which we are not including must |
472 | // be treated as an undefined symbol. | |
473 | unsigned char symbuf[sym_size]; | |
474 | elfcpp::Sym<size, big_endian> sym2(symbuf); | |
475 | unsigned int st_shndx = psym->get_st_shndx(); | |
476 | if (st_shndx != elfcpp::SHN_UNDEF | |
477 | && st_shndx < elfcpp::SHN_LORESERVE | |
dbe717ef | 478 | && !relobj->is_section_included(st_shndx)) |
a783673b ILT |
479 | { |
480 | memcpy(symbuf, p, sym_size); | |
481 | elfcpp::Sym_write<size, big_endian> sw(symbuf); | |
482 | sw.put_st_shndx(elfcpp::SHN_UNDEF); | |
483 | psym = &sym2; | |
484 | } | |
485 | ||
14bfc3f5 ILT |
486 | // In an object file, an '@' in the name separates the symbol |
487 | // name from the version name. If there are two '@' characters, | |
488 | // this is the default version. | |
489 | const char* ver = strchr(name, '@'); | |
490 | ||
491 | Symbol* res; | |
492 | if (ver == NULL) | |
493 | { | |
f0641a0b ILT |
494 | Stringpool::Key name_key; |
495 | name = this->namepool_.add(name, &name_key); | |
dbe717ef | 496 | res = this->add_from_object(relobj, name, name_key, NULL, 0, |
f0641a0b | 497 | false, *psym); |
14bfc3f5 ILT |
498 | } |
499 | else | |
500 | { | |
f0641a0b ILT |
501 | Stringpool::Key name_key; |
502 | name = this->namepool_.add(name, ver - name, &name_key); | |
503 | ||
14bfc3f5 ILT |
504 | bool def = false; |
505 | ++ver; | |
506 | if (*ver == '@') | |
507 | { | |
508 | def = true; | |
509 | ++ver; | |
510 | } | |
f0641a0b ILT |
511 | |
512 | Stringpool::Key ver_key; | |
513 | ver = this->namepool_.add(ver, &ver_key); | |
514 | ||
dbe717ef | 515 | res = this->add_from_object(relobj, name, name_key, ver, ver_key, |
f0641a0b | 516 | def, *psym); |
14bfc3f5 ILT |
517 | } |
518 | ||
519 | *sympointers++ = res; | |
14bfc3f5 ILT |
520 | } |
521 | } | |
522 | ||
dbe717ef ILT |
523 | // Add all the symbols in a dynamic object to the hash table. |
524 | ||
525 | template<int size, bool big_endian> | |
526 | void | |
527 | Symbol_table::add_from_dynobj( | |
528 | Sized_dynobj<size, big_endian>* dynobj, | |
529 | const unsigned char* syms, | |
530 | size_t count, | |
531 | const char* sym_names, | |
532 | size_t sym_name_size, | |
533 | const unsigned char* versym, | |
534 | size_t versym_size, | |
535 | const std::vector<const char*>* version_map) | |
536 | { | |
537 | // We take the size from the first object we see. | |
538 | if (this->get_size() == 0) | |
539 | this->set_size(size); | |
540 | ||
541 | if (size != this->get_size() || size != dynobj->target()->get_size()) | |
542 | { | |
543 | fprintf(stderr, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"), | |
544 | program_name, dynobj->name().c_str()); | |
545 | gold_exit(false); | |
546 | } | |
547 | ||
548 | if (versym != NULL && versym_size / 2 < count) | |
549 | { | |
550 | fprintf(stderr, _("%s: %s: too few symbol versions\n"), | |
551 | program_name, dynobj->name().c_str()); | |
552 | gold_exit(false); | |
553 | } | |
554 | ||
555 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
556 | ||
557 | const unsigned char* p = syms; | |
558 | const unsigned char* vs = versym; | |
559 | for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2) | |
560 | { | |
561 | elfcpp::Sym<size, big_endian> sym(p); | |
562 | ||
563 | // Ignore symbols with local binding. | |
564 | if (sym.get_st_bind() == elfcpp::STB_LOCAL) | |
565 | continue; | |
566 | ||
567 | unsigned int st_name = sym.get_st_name(); | |
568 | if (st_name >= sym_name_size) | |
569 | { | |
570 | fprintf(stderr, _("%s: %s: bad symbol name offset %u at %lu\n"), | |
571 | program_name, dynobj->name().c_str(), st_name, | |
572 | static_cast<unsigned long>(i)); | |
573 | gold_exit(false); | |
574 | } | |
575 | ||
576 | const char* name = sym_names + st_name; | |
577 | ||
578 | if (versym == NULL) | |
579 | { | |
580 | Stringpool::Key name_key; | |
581 | name = this->namepool_.add(name, &name_key); | |
582 | this->add_from_object(dynobj, name, name_key, NULL, 0, | |
583 | false, sym); | |
584 | continue; | |
585 | } | |
586 | ||
587 | // Read the version information. | |
588 | ||
589 | unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs); | |
590 | ||
591 | bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0; | |
592 | v &= elfcpp::VERSYM_VERSION; | |
593 | ||
594 | if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)) | |
595 | { | |
596 | // This symbol should not be visible outside the object. | |
597 | continue; | |
598 | } | |
599 | ||
600 | // At this point we are definitely going to add this symbol. | |
601 | Stringpool::Key name_key; | |
602 | name = this->namepool_.add(name, &name_key); | |
603 | ||
604 | if (v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL)) | |
605 | { | |
606 | // This symbol does not have a version. | |
607 | this->add_from_object(dynobj, name, name_key, NULL, 0, false, sym); | |
608 | continue; | |
609 | } | |
610 | ||
611 | if (v >= version_map->size()) | |
612 | { | |
613 | fprintf(stderr, | |
614 | _("%s: %s: versym for symbol %zu out of range: %u\n"), | |
615 | program_name, dynobj->name().c_str(), i, v); | |
616 | gold_exit(false); | |
617 | } | |
618 | ||
619 | const char* version = (*version_map)[v]; | |
620 | if (version == NULL) | |
621 | { | |
622 | fprintf(stderr, _("%s: %s: versym for symbol %zu has no name: %u\n"), | |
623 | program_name, dynobj->name().c_str(), i, v); | |
624 | gold_exit(false); | |
625 | } | |
626 | ||
627 | Stringpool::Key version_key; | |
628 | version = this->namepool_.add(version, &version_key); | |
629 | ||
630 | // If this is an absolute symbol, and the version name and | |
631 | // symbol name are the same, then this is the version definition | |
632 | // symbol. These symbols exist to support using -u to pull in | |
633 | // particular versions. We do not want to record a version for | |
634 | // them. | |
635 | if (sym.get_st_shndx() == elfcpp::SHN_ABS && name_key == version_key) | |
636 | { | |
637 | this->add_from_object(dynobj, name, name_key, NULL, 0, false, sym); | |
638 | continue; | |
639 | } | |
640 | ||
641 | const bool def = !hidden && sym.get_st_shndx() != elfcpp::SHN_UNDEF; | |
642 | ||
643 | this->add_from_object(dynobj, name, name_key, version, version_key, | |
644 | def, sym); | |
645 | } | |
646 | } | |
647 | ||
ead1e424 ILT |
648 | // Create and return a specially defined symbol. If ONLY_IF_REF is |
649 | // true, then only create the symbol if there is a reference to it. | |
650 | ||
651 | template<int size, bool big_endian> | |
652 | Sized_symbol<size>* | |
653 | Symbol_table::define_special_symbol(Target* target, const char* name, | |
593f47df ILT |
654 | bool only_if_ref |
655 | ACCEPT_SIZE_ENDIAN) | |
ead1e424 ILT |
656 | { |
657 | assert(this->size_ == size); | |
658 | ||
659 | Symbol* oldsym; | |
660 | Sized_symbol<size>* sym; | |
661 | ||
662 | if (only_if_ref) | |
663 | { | |
664 | oldsym = this->lookup(name, NULL); | |
f6ce93d6 | 665 | if (oldsym == NULL || !oldsym->is_undefined()) |
ead1e424 ILT |
666 | return NULL; |
667 | sym = NULL; | |
668 | ||
669 | // Canonicalize NAME. | |
670 | name = oldsym->name(); | |
671 | } | |
672 | else | |
673 | { | |
674 | // Canonicalize NAME. | |
f0641a0b ILT |
675 | Stringpool::Key name_key; |
676 | name = this->namepool_.add(name, &name_key); | |
ead1e424 ILT |
677 | |
678 | Symbol* const snull = NULL; | |
f0641a0b | 679 | const Stringpool::Key ver_key = 0; |
ead1e424 | 680 | std::pair<typename Symbol_table_type::iterator, bool> ins = |
f0641a0b | 681 | this->table_.insert(std::make_pair(std::make_pair(name_key, ver_key), |
ead1e424 ILT |
682 | snull)); |
683 | ||
684 | if (!ins.second) | |
685 | { | |
686 | // We already have a symbol table entry for NAME. | |
687 | oldsym = ins.first->second; | |
688 | assert(oldsym != NULL); | |
689 | sym = NULL; | |
690 | } | |
691 | else | |
692 | { | |
693 | // We haven't seen this symbol before. | |
694 | assert(ins.first->second == NULL); | |
695 | ||
696 | if (!target->has_make_symbol()) | |
697 | sym = new Sized_symbol<size>(); | |
698 | else | |
699 | { | |
700 | assert(target->get_size() == size); | |
701 | assert(target->is_big_endian() ? big_endian : !big_endian); | |
702 | typedef Sized_target<size, big_endian> My_target; | |
703 | My_target* sized_target = static_cast<My_target*>(target); | |
704 | sym = sized_target->make_symbol(); | |
705 | if (sym == NULL) | |
706 | return NULL; | |
707 | } | |
708 | ||
709 | ins.first->second = sym; | |
710 | oldsym = NULL; | |
711 | } | |
712 | } | |
713 | ||
714 | if (oldsym != NULL) | |
715 | { | |
716 | assert(sym == NULL); | |
717 | ||
593f47df ILT |
718 | sym = this->get_sized_symbol SELECT_SIZE_NAME(size) (oldsym |
719 | SELECT_SIZE(size)); | |
ead1e424 ILT |
720 | assert(sym->source() == Symbol::FROM_OBJECT); |
721 | const int old_shnum = sym->shnum(); | |
722 | if (old_shnum != elfcpp::SHN_UNDEF | |
723 | && old_shnum != elfcpp::SHN_COMMON | |
724 | && !sym->object()->is_dynamic()) | |
725 | { | |
726 | fprintf(stderr, "%s: linker defined: multiple definition of %s\n", | |
727 | program_name, name); | |
728 | // FIXME: Report old location. Record that we have seen an | |
729 | // error. | |
730 | return NULL; | |
731 | } | |
732 | ||
733 | // Our new definition is going to override the old reference. | |
734 | } | |
735 | ||
736 | return sym; | |
737 | } | |
738 | ||
739 | // Define a symbol based on an Output_data. | |
740 | ||
741 | void | |
742 | Symbol_table::define_in_output_data(Target* target, const char* name, | |
743 | Output_data* od, | |
744 | uint64_t value, uint64_t symsize, | |
745 | elfcpp::STT type, elfcpp::STB binding, | |
746 | elfcpp::STV visibility, | |
747 | unsigned char nonvis, | |
748 | bool offset_is_from_end, | |
749 | bool only_if_ref) | |
750 | { | |
751 | assert(target->get_size() == this->size_); | |
752 | if (this->size_ == 32) | |
753 | this->do_define_in_output_data<32>(target, name, od, value, symsize, | |
754 | type, binding, visibility, nonvis, | |
755 | offset_is_from_end, only_if_ref); | |
756 | else if (this->size_ == 64) | |
757 | this->do_define_in_output_data<64>(target, name, od, value, symsize, | |
758 | type, binding, visibility, nonvis, | |
759 | offset_is_from_end, only_if_ref); | |
760 | else | |
761 | abort(); | |
762 | } | |
763 | ||
764 | // Define a symbol in an Output_data, sized version. | |
765 | ||
766 | template<int size> | |
767 | void | |
768 | Symbol_table::do_define_in_output_data( | |
769 | Target* target, | |
770 | const char* name, | |
771 | Output_data* od, | |
772 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
773 | typename elfcpp::Elf_types<size>::Elf_WXword symsize, | |
774 | elfcpp::STT type, | |
775 | elfcpp::STB binding, | |
776 | elfcpp::STV visibility, | |
777 | unsigned char nonvis, | |
778 | bool offset_is_from_end, | |
779 | bool only_if_ref) | |
780 | { | |
781 | Sized_symbol<size>* sym; | |
782 | ||
783 | if (target->is_big_endian()) | |
593f47df ILT |
784 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) ( |
785 | target, name, only_if_ref | |
786 | SELECT_SIZE_ENDIAN(size, true)); | |
ead1e424 | 787 | else |
593f47df ILT |
788 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) ( |
789 | target, name, only_if_ref | |
790 | SELECT_SIZE_ENDIAN(size, false)); | |
ead1e424 ILT |
791 | |
792 | if (sym == NULL) | |
793 | return; | |
794 | ||
795 | sym->init(name, od, value, symsize, type, binding, visibility, nonvis, | |
796 | offset_is_from_end); | |
797 | } | |
798 | ||
799 | // Define a symbol based on an Output_segment. | |
800 | ||
801 | void | |
802 | Symbol_table::define_in_output_segment(Target* target, const char* name, | |
803 | Output_segment* os, | |
804 | uint64_t value, uint64_t symsize, | |
805 | elfcpp::STT type, elfcpp::STB binding, | |
806 | elfcpp::STV visibility, | |
807 | unsigned char nonvis, | |
808 | Symbol::Segment_offset_base offset_base, | |
809 | bool only_if_ref) | |
810 | { | |
811 | assert(target->get_size() == this->size_); | |
812 | if (this->size_ == 32) | |
813 | this->do_define_in_output_segment<32>(target, name, os, value, symsize, | |
814 | type, binding, visibility, nonvis, | |
815 | offset_base, only_if_ref); | |
816 | else if (this->size_ == 64) | |
817 | this->do_define_in_output_segment<64>(target, name, os, value, symsize, | |
818 | type, binding, visibility, nonvis, | |
819 | offset_base, only_if_ref); | |
820 | else | |
821 | abort(); | |
822 | } | |
823 | ||
824 | // Define a symbol in an Output_segment, sized version. | |
825 | ||
826 | template<int size> | |
827 | void | |
828 | Symbol_table::do_define_in_output_segment( | |
829 | Target* target, | |
830 | const char* name, | |
831 | Output_segment* os, | |
832 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
833 | typename elfcpp::Elf_types<size>::Elf_WXword symsize, | |
834 | elfcpp::STT type, | |
835 | elfcpp::STB binding, | |
836 | elfcpp::STV visibility, | |
837 | unsigned char nonvis, | |
838 | Symbol::Segment_offset_base offset_base, | |
839 | bool only_if_ref) | |
840 | { | |
841 | Sized_symbol<size>* sym; | |
842 | ||
843 | if (target->is_big_endian()) | |
593f47df ILT |
844 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) ( |
845 | target, name, only_if_ref | |
846 | SELECT_SIZE_ENDIAN(size, true)); | |
ead1e424 | 847 | else |
593f47df ILT |
848 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) ( |
849 | target, name, only_if_ref | |
850 | SELECT_SIZE_ENDIAN(size, false)); | |
ead1e424 ILT |
851 | |
852 | if (sym == NULL) | |
853 | return; | |
854 | ||
855 | sym->init(name, os, value, symsize, type, binding, visibility, nonvis, | |
856 | offset_base); | |
857 | } | |
858 | ||
859 | // Define a special symbol with a constant value. It is a multiple | |
860 | // definition error if this symbol is already defined. | |
861 | ||
862 | void | |
863 | Symbol_table::define_as_constant(Target* target, const char* name, | |
864 | uint64_t value, uint64_t symsize, | |
865 | elfcpp::STT type, elfcpp::STB binding, | |
866 | elfcpp::STV visibility, unsigned char nonvis, | |
867 | bool only_if_ref) | |
868 | { | |
869 | assert(target->get_size() == this->size_); | |
870 | if (this->size_ == 32) | |
871 | this->do_define_as_constant<32>(target, name, value, symsize, | |
872 | type, binding, visibility, nonvis, | |
873 | only_if_ref); | |
874 | else if (this->size_ == 64) | |
875 | this->do_define_as_constant<64>(target, name, value, symsize, | |
876 | type, binding, visibility, nonvis, | |
877 | only_if_ref); | |
878 | else | |
879 | abort(); | |
880 | } | |
881 | ||
882 | // Define a symbol as a constant, sized version. | |
883 | ||
884 | template<int size> | |
885 | void | |
886 | Symbol_table::do_define_as_constant( | |
887 | Target* target, | |
888 | const char* name, | |
889 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
890 | typename elfcpp::Elf_types<size>::Elf_WXword symsize, | |
891 | elfcpp::STT type, | |
892 | elfcpp::STB binding, | |
893 | elfcpp::STV visibility, | |
894 | unsigned char nonvis, | |
895 | bool only_if_ref) | |
896 | { | |
897 | Sized_symbol<size>* sym; | |
898 | ||
899 | if (target->is_big_endian()) | |
593f47df ILT |
900 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) ( |
901 | target, name, only_if_ref | |
902 | SELECT_SIZE_ENDIAN(size, true)); | |
ead1e424 | 903 | else |
593f47df ILT |
904 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) ( |
905 | target, name, only_if_ref | |
906 | SELECT_SIZE_ENDIAN(size, false)); | |
ead1e424 ILT |
907 | |
908 | if (sym == NULL) | |
909 | return; | |
910 | ||
911 | sym->init(name, value, symsize, type, binding, visibility, nonvis); | |
912 | } | |
913 | ||
914 | // Define a set of symbols in output sections. | |
915 | ||
916 | void | |
917 | Symbol_table::define_symbols(const Layout* layout, Target* target, int count, | |
918 | const Define_symbol_in_section* p) | |
919 | { | |
920 | for (int i = 0; i < count; ++i, ++p) | |
921 | { | |
922 | Output_section* os = layout->find_output_section(p->output_section); | |
923 | if (os != NULL) | |
924 | this->define_in_output_data(target, p->name, os, p->value, p->size, | |
925 | p->type, p->binding, p->visibility, | |
926 | p->nonvis, p->offset_is_from_end, | |
927 | p->only_if_ref); | |
928 | else | |
929 | this->define_as_constant(target, p->name, 0, p->size, p->type, | |
930 | p->binding, p->visibility, p->nonvis, | |
931 | p->only_if_ref); | |
932 | } | |
933 | } | |
934 | ||
935 | // Define a set of symbols in output segments. | |
936 | ||
937 | void | |
938 | Symbol_table::define_symbols(const Layout* layout, Target* target, int count, | |
939 | const Define_symbol_in_segment* p) | |
940 | { | |
941 | for (int i = 0; i < count; ++i, ++p) | |
942 | { | |
943 | Output_segment* os = layout->find_output_segment(p->segment_type, | |
944 | p->segment_flags_set, | |
945 | p->segment_flags_clear); | |
946 | if (os != NULL) | |
947 | this->define_in_output_segment(target, p->name, os, p->value, p->size, | |
948 | p->type, p->binding, p->visibility, | |
949 | p->nonvis, p->offset_base, | |
950 | p->only_if_ref); | |
951 | else | |
952 | this->define_as_constant(target, p->name, 0, p->size, p->type, | |
953 | p->binding, p->visibility, p->nonvis, | |
954 | p->only_if_ref); | |
955 | } | |
956 | } | |
957 | ||
c06b7b0b ILT |
958 | // Set the final values for all the symbols. The index of the first |
959 | // global symbol in the output file is INDEX. Record the file offset | |
75f65a3e | 960 | // OFF. Add their names to POOL. Return the new file offset. |
54dc6425 | 961 | |
75f65a3e | 962 | off_t |
c06b7b0b | 963 | Symbol_table::finalize(unsigned int index, off_t off, Stringpool* pool) |
54dc6425 | 964 | { |
f6ce93d6 ILT |
965 | off_t ret; |
966 | ||
c06b7b0b ILT |
967 | assert(index != 0); |
968 | this->first_global_index_ = index; | |
969 | ||
75f65a3e | 970 | if (this->size_ == 32) |
c06b7b0b | 971 | ret = this->sized_finalize<32>(index, off, pool); |
61ba1cf9 | 972 | else if (this->size_ == 64) |
c06b7b0b | 973 | ret = this->sized_finalize<64>(index, off, pool); |
61ba1cf9 ILT |
974 | else |
975 | abort(); | |
f6ce93d6 ILT |
976 | |
977 | // Now that we have the final symbol table, we can reliably note | |
978 | // which symbols should get warnings. | |
979 | this->warnings_.note_warnings(this); | |
980 | ||
981 | return ret; | |
75f65a3e ILT |
982 | } |
983 | ||
ead1e424 ILT |
984 | // Set the final value for all the symbols. This is called after |
985 | // Layout::finalize, so all the output sections have their final | |
986 | // address. | |
75f65a3e ILT |
987 | |
988 | template<int size> | |
989 | off_t | |
c06b7b0b | 990 | Symbol_table::sized_finalize(unsigned index, off_t off, Stringpool* pool) |
75f65a3e | 991 | { |
ead1e424 | 992 | off = align_address(off, size >> 3); |
75f65a3e ILT |
993 | this->offset_ = off; |
994 | ||
c06b7b0b ILT |
995 | size_t orig_index = index; |
996 | ||
75f65a3e | 997 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; |
c06b7b0b ILT |
998 | for (Symbol_table_type::iterator p = this->table_.begin(); |
999 | p != this->table_.end(); | |
1000 | ++p) | |
54dc6425 | 1001 | { |
75f65a3e | 1002 | Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second); |
54dc6425 | 1003 | |
75f65a3e ILT |
1004 | // FIXME: Here we need to decide which symbols should go into |
1005 | // the output file. | |
1006 | ||
ead1e424 | 1007 | typename Sized_symbol<size>::Value_type value; |
75f65a3e | 1008 | |
ead1e424 | 1009 | switch (sym->source()) |
75f65a3e | 1010 | { |
ead1e424 ILT |
1011 | case Symbol::FROM_OBJECT: |
1012 | { | |
1013 | unsigned int shnum = sym->shnum(); | |
1014 | ||
1015 | // FIXME: We need some target specific support here. | |
1016 | if (shnum >= elfcpp::SHN_LORESERVE | |
1017 | && shnum != elfcpp::SHN_ABS) | |
1018 | { | |
1019 | fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"), | |
1020 | program_name, sym->name(), shnum); | |
1021 | gold_exit(false); | |
1022 | } | |
1023 | ||
f6ce93d6 ILT |
1024 | Object* symobj = sym->object(); |
1025 | if (symobj->is_dynamic()) | |
1026 | { | |
1027 | value = 0; | |
1028 | shnum = elfcpp::SHN_UNDEF; | |
1029 | } | |
1030 | else if (shnum == elfcpp::SHN_UNDEF) | |
ead1e424 ILT |
1031 | value = 0; |
1032 | else if (shnum == elfcpp::SHN_ABS) | |
1033 | value = sym->value(); | |
1034 | else | |
1035 | { | |
f6ce93d6 | 1036 | Relobj* relobj = static_cast<Relobj*>(symobj); |
ead1e424 | 1037 | off_t secoff; |
f6ce93d6 | 1038 | Output_section* os = relobj->output_section(shnum, &secoff); |
ead1e424 ILT |
1039 | |
1040 | if (os == NULL) | |
1041 | { | |
c06b7b0b | 1042 | sym->set_symtab_index(-1U); |
ead1e424 ILT |
1043 | continue; |
1044 | } | |
1045 | ||
1046 | value = sym->value() + os->address() + secoff; | |
1047 | } | |
1048 | } | |
1049 | break; | |
1050 | ||
1051 | case Symbol::IN_OUTPUT_DATA: | |
1052 | { | |
1053 | Output_data* od = sym->output_data(); | |
1054 | value = sym->value() + od->address(); | |
1055 | if (sym->offset_is_from_end()) | |
1056 | value += od->data_size(); | |
1057 | } | |
1058 | break; | |
1059 | ||
1060 | case Symbol::IN_OUTPUT_SEGMENT: | |
1061 | { | |
1062 | Output_segment* os = sym->output_segment(); | |
1063 | value = sym->value() + os->vaddr(); | |
1064 | switch (sym->offset_base()) | |
1065 | { | |
1066 | case Symbol::SEGMENT_START: | |
1067 | break; | |
1068 | case Symbol::SEGMENT_END: | |
1069 | value += os->memsz(); | |
1070 | break; | |
1071 | case Symbol::SEGMENT_BSS: | |
1072 | value += os->filesz(); | |
1073 | break; | |
1074 | default: | |
1075 | abort(); | |
1076 | } | |
1077 | } | |
1078 | break; | |
1079 | ||
1080 | case Symbol::CONSTANT: | |
1081 | value = sym->value(); | |
1082 | break; | |
1083 | ||
1084 | default: | |
1085 | abort(); | |
54dc6425 | 1086 | } |
ead1e424 ILT |
1087 | |
1088 | sym->set_value(value); | |
c06b7b0b | 1089 | sym->set_symtab_index(index); |
f0641a0b | 1090 | pool->add(sym->name(), NULL); |
c06b7b0b | 1091 | ++index; |
ead1e424 | 1092 | off += sym_size; |
54dc6425 | 1093 | } |
75f65a3e | 1094 | |
c06b7b0b | 1095 | this->output_count_ = index - orig_index; |
61ba1cf9 | 1096 | |
75f65a3e | 1097 | return off; |
54dc6425 ILT |
1098 | } |
1099 | ||
61ba1cf9 ILT |
1100 | // Write out the global symbols. |
1101 | ||
1102 | void | |
1103 | Symbol_table::write_globals(const Target* target, const Stringpool* sympool, | |
1104 | Output_file* of) const | |
1105 | { | |
1106 | if (this->size_ == 32) | |
1107 | { | |
1108 | if (target->is_big_endian()) | |
1109 | this->sized_write_globals<32, true>(target, sympool, of); | |
1110 | else | |
1111 | this->sized_write_globals<32, false>(target, sympool, of); | |
1112 | } | |
1113 | else if (this->size_ == 64) | |
1114 | { | |
1115 | if (target->is_big_endian()) | |
1116 | this->sized_write_globals<64, true>(target, sympool, of); | |
1117 | else | |
1118 | this->sized_write_globals<64, false>(target, sympool, of); | |
1119 | } | |
1120 | else | |
1121 | abort(); | |
1122 | } | |
1123 | ||
1124 | // Write out the global symbols. | |
1125 | ||
1126 | template<int size, bool big_endian> | |
1127 | void | |
1128 | Symbol_table::sized_write_globals(const Target*, | |
1129 | const Stringpool* sympool, | |
1130 | Output_file* of) const | |
1131 | { | |
1132 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
c06b7b0b ILT |
1133 | unsigned int index = this->first_global_index_; |
1134 | const off_t oview_size = this->output_count_ * sym_size; | |
1135 | unsigned char* psyms = of->get_output_view(this->offset_, oview_size); | |
1136 | ||
61ba1cf9 ILT |
1137 | unsigned char* ps = psyms; |
1138 | for (Symbol_table_type::const_iterator p = this->table_.begin(); | |
1139 | p != this->table_.end(); | |
1140 | ++p) | |
1141 | { | |
1142 | Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second); | |
1143 | ||
c06b7b0b ILT |
1144 | if (sym->symtab_index() == -1U) |
1145 | continue; | |
1146 | ||
ead1e424 ILT |
1147 | unsigned int shndx; |
1148 | switch (sym->source()) | |
1149 | { | |
1150 | case Symbol::FROM_OBJECT: | |
1151 | { | |
1152 | unsigned int shnum = sym->shnum(); | |
1153 | ||
1154 | // FIXME: We need some target specific support here. | |
1155 | if (shnum >= elfcpp::SHN_LORESERVE | |
1156 | && shnum != elfcpp::SHN_ABS) | |
1157 | { | |
1158 | fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"), | |
1159 | program_name, sym->name(), sym->shnum()); | |
1160 | gold_exit(false); | |
1161 | } | |
1162 | ||
f6ce93d6 ILT |
1163 | Object* symobj = sym->object(); |
1164 | if (symobj->is_dynamic()) | |
1165 | { | |
1166 | // FIXME. | |
1167 | shndx = elfcpp::SHN_UNDEF; | |
1168 | } | |
1169 | else if (shnum == elfcpp::SHN_UNDEF || shnum == elfcpp::SHN_ABS) | |
ead1e424 ILT |
1170 | shndx = shnum; |
1171 | else | |
1172 | { | |
f6ce93d6 | 1173 | Relobj* relobj = static_cast<Relobj*>(symobj); |
ead1e424 | 1174 | off_t secoff; |
f6ce93d6 | 1175 | Output_section* os = relobj->output_section(shnum, &secoff); |
c06b7b0b | 1176 | assert(os != NULL); |
ead1e424 ILT |
1177 | shndx = os->out_shndx(); |
1178 | } | |
1179 | } | |
1180 | break; | |
1181 | ||
1182 | case Symbol::IN_OUTPUT_DATA: | |
1183 | shndx = sym->output_data()->out_shndx(); | |
1184 | break; | |
1185 | ||
1186 | case Symbol::IN_OUTPUT_SEGMENT: | |
1187 | shndx = elfcpp::SHN_ABS; | |
1188 | break; | |
1189 | ||
1190 | case Symbol::CONSTANT: | |
1191 | shndx = elfcpp::SHN_ABS; | |
1192 | break; | |
1193 | ||
1194 | default: | |
1195 | abort(); | |
1196 | } | |
61ba1cf9 | 1197 | |
c06b7b0b ILT |
1198 | assert(sym->symtab_index() == index); |
1199 | ++index; | |
1200 | ||
61ba1cf9 ILT |
1201 | elfcpp::Sym_write<size, big_endian> osym(ps); |
1202 | osym.put_st_name(sympool->get_offset(sym->name())); | |
1203 | osym.put_st_value(sym->value()); | |
1204 | osym.put_st_size(sym->symsize()); | |
1205 | osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type())); | |
ead1e424 ILT |
1206 | osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), |
1207 | sym->nonvis())); | |
1208 | osym.put_st_shndx(shndx); | |
61ba1cf9 ILT |
1209 | |
1210 | ps += sym_size; | |
1211 | } | |
1212 | ||
c06b7b0b ILT |
1213 | assert(ps - psyms == oview_size); |
1214 | ||
1215 | of->write_output_view(this->offset_, oview_size, psyms); | |
61ba1cf9 ILT |
1216 | } |
1217 | ||
f6ce93d6 ILT |
1218 | // Warnings functions. |
1219 | ||
1220 | // Add a new warning. | |
1221 | ||
1222 | void | |
1223 | Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj, | |
1224 | unsigned int shndx) | |
1225 | { | |
1226 | name = symtab->canonicalize_name(name); | |
1227 | this->warnings_[name].set(obj, shndx); | |
1228 | } | |
1229 | ||
1230 | // Look through the warnings and mark the symbols for which we should | |
1231 | // warn. This is called during Layout::finalize when we know the | |
1232 | // sources for all the symbols. | |
1233 | ||
1234 | void | |
1235 | Warnings::note_warnings(Symbol_table* symtab) | |
1236 | { | |
1237 | for (Warning_table::iterator p = this->warnings_.begin(); | |
1238 | p != this->warnings_.end(); | |
1239 | ++p) | |
1240 | { | |
1241 | Symbol* sym = symtab->lookup(p->first, NULL); | |
1242 | if (sym != NULL | |
1243 | && sym->source() == Symbol::FROM_OBJECT | |
1244 | && sym->object() == p->second.object) | |
1245 | { | |
1246 | sym->set_has_warning(); | |
1247 | ||
1248 | // Read the section contents to get the warning text. It | |
1249 | // would be nicer if we only did this if we have to actually | |
1250 | // issue a warning. Unfortunately, warnings are issued as | |
1251 | // we relocate sections. That means that we can not lock | |
1252 | // the object then, as we might try to issue the same | |
1253 | // warning multiple times simultaneously. | |
645f8123 ILT |
1254 | { |
1255 | Task_locker_obj<Object> tl(*p->second.object); | |
1256 | const unsigned char* c; | |
1257 | off_t len; | |
1258 | c = p->second.object->section_contents(p->second.shndx, &len); | |
1259 | p->second.set_text(reinterpret_cast<const char*>(c), len); | |
1260 | } | |
f6ce93d6 ILT |
1261 | } |
1262 | } | |
1263 | } | |
1264 | ||
1265 | // Issue a warning. This is called when we see a relocation against a | |
1266 | // symbol for which has a warning. | |
1267 | ||
1268 | void | |
c06b7b0b | 1269 | Warnings::issue_warning(const Symbol* sym, const std::string& location) const |
f6ce93d6 ILT |
1270 | { |
1271 | assert(sym->has_warning()); | |
1272 | Warning_table::const_iterator p = this->warnings_.find(sym->name()); | |
1273 | assert(p != this->warnings_.end()); | |
1274 | fprintf(stderr, _("%s: %s: warning: %s\n"), program_name, location.c_str(), | |
1275 | p->second.text.c_str()); | |
1276 | } | |
1277 | ||
14bfc3f5 ILT |
1278 | // Instantiate the templates we need. We could use the configure |
1279 | // script to restrict this to only the ones needed for implemented | |
1280 | // targets. | |
1281 | ||
1282 | template | |
1283 | void | |
dbe717ef ILT |
1284 | Symbol_table::add_from_relobj<32, true>( |
1285 | Sized_relobj<32, true>* relobj, | |
f6ce93d6 | 1286 | const unsigned char* syms, |
14bfc3f5 ILT |
1287 | size_t count, |
1288 | const char* sym_names, | |
1289 | size_t sym_name_size, | |
1290 | Symbol** sympointers); | |
1291 | ||
1292 | template | |
1293 | void | |
dbe717ef ILT |
1294 | Symbol_table::add_from_relobj<32, false>( |
1295 | Sized_relobj<32, false>* relobj, | |
f6ce93d6 | 1296 | const unsigned char* syms, |
14bfc3f5 ILT |
1297 | size_t count, |
1298 | const char* sym_names, | |
1299 | size_t sym_name_size, | |
1300 | Symbol** sympointers); | |
1301 | ||
1302 | template | |
1303 | void | |
dbe717ef ILT |
1304 | Symbol_table::add_from_relobj<64, true>( |
1305 | Sized_relobj<64, true>* relobj, | |
f6ce93d6 | 1306 | const unsigned char* syms, |
14bfc3f5 ILT |
1307 | size_t count, |
1308 | const char* sym_names, | |
1309 | size_t sym_name_size, | |
1310 | Symbol** sympointers); | |
1311 | ||
1312 | template | |
1313 | void | |
dbe717ef ILT |
1314 | Symbol_table::add_from_relobj<64, false>( |
1315 | Sized_relobj<64, false>* relobj, | |
f6ce93d6 | 1316 | const unsigned char* syms, |
14bfc3f5 ILT |
1317 | size_t count, |
1318 | const char* sym_names, | |
1319 | size_t sym_name_size, | |
1320 | Symbol** sympointers); | |
1321 | ||
dbe717ef ILT |
1322 | template |
1323 | void | |
1324 | Symbol_table::add_from_dynobj<32, true>( | |
1325 | Sized_dynobj<32, true>* dynobj, | |
1326 | const unsigned char* syms, | |
1327 | size_t count, | |
1328 | const char* sym_names, | |
1329 | size_t sym_name_size, | |
1330 | const unsigned char* versym, | |
1331 | size_t versym_size, | |
1332 | const std::vector<const char*>* version_map); | |
1333 | ||
1334 | template | |
1335 | void | |
1336 | Symbol_table::add_from_dynobj<32, false>( | |
1337 | Sized_dynobj<32, false>* dynobj, | |
1338 | const unsigned char* syms, | |
1339 | size_t count, | |
1340 | const char* sym_names, | |
1341 | size_t sym_name_size, | |
1342 | const unsigned char* versym, | |
1343 | size_t versym_size, | |
1344 | const std::vector<const char*>* version_map); | |
1345 | ||
1346 | template | |
1347 | void | |
1348 | Symbol_table::add_from_dynobj<64, true>( | |
1349 | Sized_dynobj<64, true>* dynobj, | |
1350 | const unsigned char* syms, | |
1351 | size_t count, | |
1352 | const char* sym_names, | |
1353 | size_t sym_name_size, | |
1354 | const unsigned char* versym, | |
1355 | size_t versym_size, | |
1356 | const std::vector<const char*>* version_map); | |
1357 | ||
1358 | template | |
1359 | void | |
1360 | Symbol_table::add_from_dynobj<64, false>( | |
1361 | Sized_dynobj<64, false>* dynobj, | |
1362 | const unsigned char* syms, | |
1363 | size_t count, | |
1364 | const char* sym_names, | |
1365 | size_t sym_name_size, | |
1366 | const unsigned char* versym, | |
1367 | size_t versym_size, | |
1368 | const std::vector<const char*>* version_map); | |
1369 | ||
14bfc3f5 | 1370 | } // End namespace gold. |