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" | |
75f65a3e | 11 | #include "output.h" |
61ba1cf9 | 12 | #include "target.h" |
14bfc3f5 ILT |
13 | #include "symtab.h" |
14 | ||
15 | namespace gold | |
16 | { | |
17 | ||
18 | // Class Symbol. | |
19 | ||
ead1e424 ILT |
20 | // Initialize fields in Symbol. This initializes everything except u_ |
21 | // and source_. | |
14bfc3f5 | 22 | |
14bfc3f5 | 23 | void |
ead1e424 ILT |
24 | Symbol::init_fields(const char* name, const char* version, |
25 | elfcpp::STT type, elfcpp::STB binding, | |
26 | elfcpp::STV visibility, unsigned char nonvis) | |
14bfc3f5 ILT |
27 | { |
28 | this->name_ = name; | |
29 | this->version_ = version; | |
ead1e424 ILT |
30 | this->got_offset_ = 0; |
31 | this->type_ = type; | |
32 | this->binding_ = binding; | |
33 | this->visibility_ = visibility; | |
34 | this->nonvis_ = nonvis; | |
35 | this->is_target_special_ = false; | |
1564db8d ILT |
36 | this->is_def_ = false; |
37 | this->is_forwarder_ = false; | |
ead1e424 ILT |
38 | this->in_dyn_ = false; |
39 | this->has_got_offset_ = false; | |
40 | } | |
41 | ||
42 | // Initialize the fields in the base class Symbol for SYM in OBJECT. | |
43 | ||
44 | template<int size, bool big_endian> | |
45 | void | |
46 | Symbol::init_base(const char* name, const char* version, Object* object, | |
47 | const elfcpp::Sym<size, big_endian>& sym) | |
48 | { | |
49 | this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(), | |
50 | sym.get_st_visibility(), sym.get_st_nonvis()); | |
51 | this->u_.from_object.object = object; | |
52 | // FIXME: Handle SHN_XINDEX. | |
53 | this->u_.from_object.shnum = sym.get_st_shndx(); | |
54 | this->source_ = FROM_OBJECT; | |
1564db8d | 55 | this->in_dyn_ = object->is_dynamic(); |
14bfc3f5 ILT |
56 | } |
57 | ||
ead1e424 ILT |
58 | // Initialize the fields in the base class Symbol for a symbol defined |
59 | // in an Output_data. | |
60 | ||
61 | void | |
62 | Symbol::init_base(const char* name, Output_data* od, elfcpp::STT type, | |
63 | elfcpp::STB binding, elfcpp::STV visibility, | |
64 | unsigned char nonvis, bool offset_is_from_end) | |
65 | { | |
66 | this->init_fields(name, NULL, type, binding, visibility, nonvis); | |
67 | this->u_.in_output_data.output_data = od; | |
68 | this->u_.in_output_data.offset_is_from_end = offset_is_from_end; | |
69 | this->source_ = IN_OUTPUT_DATA; | |
70 | } | |
71 | ||
72 | // Initialize the fields in the base class Symbol for a symbol defined | |
73 | // in an Output_segment. | |
74 | ||
75 | void | |
76 | Symbol::init_base(const char* name, Output_segment* os, elfcpp::STT type, | |
77 | elfcpp::STB binding, elfcpp::STV visibility, | |
78 | unsigned char nonvis, Segment_offset_base offset_base) | |
79 | { | |
80 | this->init_fields(name, NULL, type, binding, visibility, nonvis); | |
81 | this->u_.in_output_segment.output_segment = os; | |
82 | this->u_.in_output_segment.offset_base = offset_base; | |
83 | this->source_ = IN_OUTPUT_SEGMENT; | |
84 | } | |
85 | ||
86 | // Initialize the fields in the base class Symbol for a symbol defined | |
87 | // as a constant. | |
88 | ||
89 | void | |
90 | Symbol::init_base(const char* name, elfcpp::STT type, | |
91 | elfcpp::STB binding, elfcpp::STV visibility, | |
92 | unsigned char nonvis) | |
93 | { | |
94 | this->init_fields(name, NULL, type, binding, visibility, nonvis); | |
95 | this->source_ = CONSTANT; | |
96 | } | |
97 | ||
98 | // Initialize the fields in Sized_symbol for SYM in OBJECT. | |
14bfc3f5 ILT |
99 | |
100 | template<int size> | |
101 | template<bool big_endian> | |
102 | void | |
103 | Sized_symbol<size>::init(const char* name, const char* version, Object* object, | |
104 | const elfcpp::Sym<size, big_endian>& sym) | |
105 | { | |
106 | this->init_base(name, version, object, sym); | |
107 | this->value_ = sym.get_st_value(); | |
ead1e424 ILT |
108 | this->symsize_ = sym.get_st_size(); |
109 | } | |
110 | ||
111 | // Initialize the fields in Sized_symbol for a symbol defined in an | |
112 | // Output_data. | |
113 | ||
114 | template<int size> | |
115 | void | |
116 | Sized_symbol<size>::init(const char* name, Output_data* od, | |
117 | Value_type value, Size_type symsize, | |
118 | elfcpp::STT type, elfcpp::STB binding, | |
119 | elfcpp::STV visibility, unsigned char nonvis, | |
120 | bool offset_is_from_end) | |
121 | { | |
122 | this->init_base(name, od, type, binding, visibility, nonvis, | |
123 | offset_is_from_end); | |
124 | this->value_ = value; | |
125 | this->symsize_ = symsize; | |
126 | } | |
127 | ||
128 | // Initialize the fields in Sized_symbol for a symbol defined in an | |
129 | // Output_segment. | |
130 | ||
131 | template<int size> | |
132 | void | |
133 | Sized_symbol<size>::init(const char* name, Output_segment* os, | |
134 | Value_type value, Size_type symsize, | |
135 | elfcpp::STT type, elfcpp::STB binding, | |
136 | elfcpp::STV visibility, unsigned char nonvis, | |
137 | Segment_offset_base offset_base) | |
138 | { | |
139 | this->init_base(name, os, type, binding, visibility, nonvis, offset_base); | |
140 | this->value_ = value; | |
141 | this->symsize_ = symsize; | |
142 | } | |
143 | ||
144 | // Initialize the fields in Sized_symbol for a symbol defined as a | |
145 | // constant. | |
146 | ||
147 | template<int size> | |
148 | void | |
149 | Sized_symbol<size>::init(const char* name, Value_type value, Size_type symsize, | |
150 | elfcpp::STT type, elfcpp::STB binding, | |
151 | elfcpp::STV visibility, unsigned char nonvis) | |
152 | { | |
153 | this->init_base(name, type, binding, visibility, nonvis); | |
154 | this->value_ = value; | |
155 | this->symsize_ = symsize; | |
14bfc3f5 ILT |
156 | } |
157 | ||
158 | // Class Symbol_table. | |
159 | ||
160 | Symbol_table::Symbol_table() | |
ead1e424 ILT |
161 | : size_(0), saw_undefined_(0), offset_(0), table_(), namepool_(), |
162 | forwarders_(), commons_() | |
14bfc3f5 ILT |
163 | { |
164 | } | |
165 | ||
166 | Symbol_table::~Symbol_table() | |
167 | { | |
168 | } | |
169 | ||
170 | // The hash function. The key is always canonicalized, so we use a | |
171 | // simple combination of the pointers. | |
172 | ||
173 | size_t | |
174 | Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const | |
175 | { | |
176 | return (reinterpret_cast<size_t>(key.first) | |
177 | ^ reinterpret_cast<size_t>(key.second)); | |
178 | } | |
179 | ||
180 | // The symbol table key equality function. This is only called with | |
181 | // canonicalized name and version strings, so we can use pointer | |
182 | // comparison. | |
183 | ||
184 | bool | |
185 | Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1, | |
186 | const Symbol_table_key& k2) const | |
187 | { | |
188 | return k1.first == k2.first && k1.second == k2.second; | |
189 | } | |
190 | ||
191 | // Make TO a symbol which forwards to FROM. | |
192 | ||
193 | void | |
194 | Symbol_table::make_forwarder(Symbol* from, Symbol* to) | |
195 | { | |
196 | assert(!from->is_forwarder() && !to->is_forwarder()); | |
197 | this->forwarders_[from] = to; | |
198 | from->set_forwarder(); | |
199 | } | |
200 | ||
61ba1cf9 ILT |
201 | // Resolve the forwards from FROM, returning the real symbol. |
202 | ||
14bfc3f5 ILT |
203 | Symbol* |
204 | Symbol_table::resolve_forwards(Symbol* from) const | |
205 | { | |
206 | assert(from->is_forwarder()); | |
207 | Unordered_map<Symbol*, Symbol*>::const_iterator p = | |
208 | this->forwarders_.find(from); | |
209 | assert(p != this->forwarders_.end()); | |
210 | return p->second; | |
211 | } | |
212 | ||
61ba1cf9 ILT |
213 | // Look up a symbol by name. |
214 | ||
215 | Symbol* | |
216 | Symbol_table::lookup(const char* name, const char* version) const | |
217 | { | |
218 | name = this->namepool_.find(name); | |
219 | if (name == NULL) | |
220 | return NULL; | |
221 | if (version != NULL) | |
222 | { | |
223 | version = this->namepool_.find(version); | |
224 | if (version == NULL) | |
225 | return NULL; | |
226 | } | |
227 | ||
228 | Symbol_table_key key(name, version); | |
229 | Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key); | |
230 | if (p == this->table_.end()) | |
231 | return NULL; | |
232 | return p->second; | |
233 | } | |
234 | ||
14bfc3f5 ILT |
235 | // Resolve a Symbol with another Symbol. This is only used in the |
236 | // unusual case where there are references to both an unversioned | |
237 | // symbol and a symbol with a version, and we then discover that that | |
1564db8d ILT |
238 | // version is the default version. Because this is unusual, we do |
239 | // this the slow way, by converting back to an ELF symbol. | |
14bfc3f5 | 240 | |
1564db8d | 241 | template<int size, bool big_endian> |
14bfc3f5 | 242 | void |
5482377d ILT |
243 | Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from |
244 | ACCEPT_SIZE_ENDIAN) | |
14bfc3f5 | 245 | { |
1564db8d ILT |
246 | unsigned char buf[elfcpp::Elf_sizes<size>::sym_size]; |
247 | elfcpp::Sym_write<size, big_endian> esym(buf); | |
248 | // We don't bother to set the st_name field. | |
249 | esym.put_st_value(from->value()); | |
250 | esym.put_st_size(from->symsize()); | |
251 | esym.put_st_info(from->binding(), from->type()); | |
ead1e424 | 252 | esym.put_st_other(from->visibility(), from->nonvis()); |
1564db8d ILT |
253 | esym.put_st_shndx(from->shnum()); |
254 | Symbol_table::resolve(to, esym.sym(), from->object()); | |
14bfc3f5 ILT |
255 | } |
256 | ||
257 | // Add one symbol from OBJECT to the symbol table. NAME is symbol | |
258 | // name and VERSION is the version; both are canonicalized. DEF is | |
259 | // whether this is the default version. | |
260 | ||
261 | // If DEF is true, then this is the definition of a default version of | |
262 | // a symbol. That means that any lookup of NAME/NULL and any lookup | |
263 | // of NAME/VERSION should always return the same symbol. This is | |
264 | // obvious for references, but in particular we want to do this for | |
265 | // definitions: overriding NAME/NULL should also override | |
266 | // NAME/VERSION. If we don't do that, it would be very hard to | |
267 | // override functions in a shared library which uses versioning. | |
268 | ||
269 | // We implement this by simply making both entries in the hash table | |
270 | // point to the same Symbol structure. That is easy enough if this is | |
271 | // the first time we see NAME/NULL or NAME/VERSION, but it is possible | |
272 | // that we have seen both already, in which case they will both have | |
273 | // independent entries in the symbol table. We can't simply change | |
274 | // the symbol table entry, because we have pointers to the entries | |
275 | // attached to the object files. So we mark the entry attached to the | |
276 | // object file as a forwarder, and record it in the forwarders_ map. | |
277 | // Note that entries in the hash table will never be marked as | |
278 | // forwarders. | |
279 | ||
280 | template<int size, bool big_endian> | |
281 | Symbol* | |
282 | Symbol_table::add_from_object(Sized_object<size, big_endian>* object, | |
283 | const char *name, | |
284 | const char *version, bool def, | |
285 | const elfcpp::Sym<size, big_endian>& sym) | |
286 | { | |
287 | Symbol* const snull = NULL; | |
288 | std::pair<typename Symbol_table_type::iterator, bool> ins = | |
289 | this->table_.insert(std::make_pair(std::make_pair(name, version), snull)); | |
290 | ||
291 | std::pair<typename Symbol_table_type::iterator, bool> insdef = | |
292 | std::make_pair(this->table_.end(), false); | |
293 | if (def) | |
294 | { | |
295 | const char* const vnull = NULL; | |
296 | insdef = this->table_.insert(std::make_pair(std::make_pair(name, vnull), | |
297 | snull)); | |
298 | } | |
299 | ||
300 | // ins.first: an iterator, which is a pointer to a pair. | |
301 | // ins.first->first: the key (a pair of name and version). | |
302 | // ins.first->second: the value (Symbol*). | |
303 | // ins.second: true if new entry was inserted, false if not. | |
304 | ||
1564db8d | 305 | Sized_symbol<size>* ret; |
ead1e424 ILT |
306 | bool was_undefined; |
307 | bool was_common; | |
14bfc3f5 ILT |
308 | if (!ins.second) |
309 | { | |
310 | // We already have an entry for NAME/VERSION. | |
5482377d ILT |
311 | ret = this->get_sized_symbol SELECT_SIZE_NAME (ins.first->second |
312 | SELECT_SIZE(size)); | |
14bfc3f5 | 313 | assert(ret != NULL); |
ead1e424 ILT |
314 | |
315 | was_undefined = ret->is_undefined(); | |
316 | was_common = ret->is_common(); | |
317 | ||
14bfc3f5 ILT |
318 | Symbol_table::resolve(ret, sym, object); |
319 | ||
320 | if (def) | |
321 | { | |
322 | if (insdef.second) | |
323 | { | |
324 | // This is the first time we have seen NAME/NULL. Make | |
325 | // NAME/NULL point to NAME/VERSION. | |
326 | insdef.first->second = ret; | |
327 | } | |
328 | else | |
329 | { | |
330 | // This is the unfortunate case where we already have | |
331 | // entries for both NAME/VERSION and NAME/NULL. | |
274e99f9 | 332 | const Sized_symbol<size>* sym2; |
5482377d ILT |
333 | sym2 = this->get_sized_symbol SELECT_SIZE_NAME ( |
334 | insdef.first->second | |
335 | SELECT_SIZE(size)); | |
336 | Symbol_table::resolve SELECT_SIZE_ENDIAN_NAME ( | |
337 | ret, sym2 SELECT_SIZE_ENDIAN(size, big_endian)); | |
14bfc3f5 ILT |
338 | this->make_forwarder(insdef.first->second, ret); |
339 | insdef.first->second = ret; | |
340 | } | |
341 | } | |
342 | } | |
343 | else | |
344 | { | |
345 | // This is the first time we have seen NAME/VERSION. | |
346 | assert(ins.first->second == NULL); | |
ead1e424 ILT |
347 | |
348 | was_undefined = false; | |
349 | was_common = false; | |
350 | ||
14bfc3f5 ILT |
351 | if (def && !insdef.second) |
352 | { | |
353 | // We already have an entry for NAME/NULL. Make | |
354 | // NAME/VERSION point to it. | |
5482377d ILT |
355 | ret = this->get_sized_symbol SELECT_SIZE_NAME (insdef.first->second |
356 | SELECT_SIZE(size)); | |
14bfc3f5 ILT |
357 | Symbol_table::resolve(ret, sym, object); |
358 | ins.first->second = ret; | |
359 | } | |
360 | else | |
361 | { | |
14bfc3f5 | 362 | Sized_target<size, big_endian>* target = object->sized_target(); |
1564db8d ILT |
363 | if (!target->has_make_symbol()) |
364 | ret = new Sized_symbol<size>(); | |
365 | else | |
14bfc3f5 | 366 | { |
1564db8d ILT |
367 | ret = target->make_symbol(); |
368 | if (ret == NULL) | |
14bfc3f5 ILT |
369 | { |
370 | // This means that we don't want a symbol table | |
371 | // entry after all. | |
372 | if (!def) | |
373 | this->table_.erase(ins.first); | |
374 | else | |
375 | { | |
376 | this->table_.erase(insdef.first); | |
377 | // Inserting insdef invalidated ins. | |
378 | this->table_.erase(std::make_pair(name, version)); | |
379 | } | |
380 | return NULL; | |
381 | } | |
382 | } | |
14bfc3f5 | 383 | |
1564db8d ILT |
384 | ret->init(name, version, object, sym); |
385 | ||
14bfc3f5 ILT |
386 | ins.first->second = ret; |
387 | if (def) | |
388 | { | |
389 | // This is the first time we have seen NAME/NULL. Point | |
390 | // it at the new entry for NAME/VERSION. | |
391 | assert(insdef.second); | |
392 | insdef.first->second = ret; | |
393 | } | |
394 | } | |
395 | } | |
396 | ||
ead1e424 ILT |
397 | // Record every time we see a new undefined symbol, to speed up |
398 | // archive groups. | |
399 | if (!was_undefined && ret->is_undefined()) | |
400 | ++this->saw_undefined_; | |
401 | ||
402 | // Keep track of common symbols, to speed up common symbol | |
403 | // allocation. | |
404 | if (!was_common && ret->is_common()) | |
405 | this->commons_.push_back(ret); | |
406 | ||
14bfc3f5 ILT |
407 | return ret; |
408 | } | |
409 | ||
410 | // Add all the symbols in an object to the hash table. | |
411 | ||
412 | template<int size, bool big_endian> | |
413 | void | |
414 | Symbol_table::add_from_object( | |
415 | Sized_object<size, big_endian>* object, | |
416 | const elfcpp::Sym<size, big_endian>* syms, | |
417 | size_t count, | |
418 | const char* sym_names, | |
419 | size_t sym_name_size, | |
420 | Symbol** sympointers) | |
421 | { | |
422 | // We take the size from the first object we see. | |
423 | if (this->get_size() == 0) | |
424 | this->set_size(size); | |
425 | ||
426 | if (size != this->get_size() || size != object->target()->get_size()) | |
427 | { | |
428 | fprintf(stderr, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"), | |
429 | program_name, object->name().c_str()); | |
430 | gold_exit(false); | |
431 | } | |
432 | ||
a783673b ILT |
433 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; |
434 | ||
14bfc3f5 | 435 | const unsigned char* p = reinterpret_cast<const unsigned char*>(syms); |
a783673b | 436 | for (size_t i = 0; i < count; ++i, p += sym_size) |
14bfc3f5 ILT |
437 | { |
438 | elfcpp::Sym<size, big_endian> sym(p); | |
a783673b | 439 | elfcpp::Sym<size, big_endian>* psym = &sym; |
14bfc3f5 | 440 | |
a783673b | 441 | unsigned int st_name = psym->get_st_name(); |
14bfc3f5 ILT |
442 | if (st_name >= sym_name_size) |
443 | { | |
54dc6425 ILT |
444 | fprintf(stderr, |
445 | _("%s: %s: bad global symbol name offset %u at %lu\n"), | |
14bfc3f5 ILT |
446 | program_name, object->name().c_str(), st_name, |
447 | static_cast<unsigned long>(i)); | |
448 | gold_exit(false); | |
449 | } | |
450 | ||
a783673b ILT |
451 | // A symbol defined in a section which we are not including must |
452 | // be treated as an undefined symbol. | |
453 | unsigned char symbuf[sym_size]; | |
454 | elfcpp::Sym<size, big_endian> sym2(symbuf); | |
455 | unsigned int st_shndx = psym->get_st_shndx(); | |
456 | if (st_shndx != elfcpp::SHN_UNDEF | |
457 | && st_shndx < elfcpp::SHN_LORESERVE | |
458 | && !object->is_section_included(st_shndx)) | |
459 | { | |
460 | memcpy(symbuf, p, sym_size); | |
461 | elfcpp::Sym_write<size, big_endian> sw(symbuf); | |
462 | sw.put_st_shndx(elfcpp::SHN_UNDEF); | |
463 | psym = &sym2; | |
464 | } | |
465 | ||
14bfc3f5 ILT |
466 | const char* name = sym_names + st_name; |
467 | ||
468 | // In an object file, an '@' in the name separates the symbol | |
469 | // name from the version name. If there are two '@' characters, | |
470 | // this is the default version. | |
471 | const char* ver = strchr(name, '@'); | |
472 | ||
473 | Symbol* res; | |
474 | if (ver == NULL) | |
475 | { | |
476 | name = this->namepool_.add(name); | |
a783673b | 477 | res = this->add_from_object(object, name, NULL, false, *psym); |
14bfc3f5 ILT |
478 | } |
479 | else | |
480 | { | |
481 | name = this->namepool_.add(name, ver - name); | |
482 | bool def = false; | |
483 | ++ver; | |
484 | if (*ver == '@') | |
485 | { | |
486 | def = true; | |
487 | ++ver; | |
488 | } | |
489 | ver = this->namepool_.add(ver); | |
a783673b | 490 | res = this->add_from_object(object, name, ver, def, *psym); |
14bfc3f5 ILT |
491 | } |
492 | ||
493 | *sympointers++ = res; | |
14bfc3f5 ILT |
494 | } |
495 | } | |
496 | ||
ead1e424 ILT |
497 | // Create and return a specially defined symbol. If ONLY_IF_REF is |
498 | // true, then only create the symbol if there is a reference to it. | |
499 | ||
500 | template<int size, bool big_endian> | |
501 | Sized_symbol<size>* | |
502 | Symbol_table::define_special_symbol(Target* target, const char* name, | |
503 | bool only_if_ref) | |
504 | { | |
505 | assert(this->size_ == size); | |
506 | ||
507 | Symbol* oldsym; | |
508 | Sized_symbol<size>* sym; | |
509 | ||
510 | if (only_if_ref) | |
511 | { | |
512 | oldsym = this->lookup(name, NULL); | |
513 | if (oldsym == NULL) | |
514 | return NULL; | |
515 | sym = NULL; | |
516 | ||
517 | // Canonicalize NAME. | |
518 | name = oldsym->name(); | |
519 | } | |
520 | else | |
521 | { | |
522 | // Canonicalize NAME. | |
523 | name = this->namepool_.add(name); | |
524 | ||
525 | Symbol* const snull = NULL; | |
526 | const char* const vnull = NULL; | |
527 | std::pair<typename Symbol_table_type::iterator, bool> ins = | |
528 | this->table_.insert(std::make_pair(std::make_pair(name, vnull), | |
529 | snull)); | |
530 | ||
531 | if (!ins.second) | |
532 | { | |
533 | // We already have a symbol table entry for NAME. | |
534 | oldsym = ins.first->second; | |
535 | assert(oldsym != NULL); | |
536 | sym = NULL; | |
537 | } | |
538 | else | |
539 | { | |
540 | // We haven't seen this symbol before. | |
541 | assert(ins.first->second == NULL); | |
542 | ||
543 | if (!target->has_make_symbol()) | |
544 | sym = new Sized_symbol<size>(); | |
545 | else | |
546 | { | |
547 | assert(target->get_size() == size); | |
548 | assert(target->is_big_endian() ? big_endian : !big_endian); | |
549 | typedef Sized_target<size, big_endian> My_target; | |
550 | My_target* sized_target = static_cast<My_target*>(target); | |
551 | sym = sized_target->make_symbol(); | |
552 | if (sym == NULL) | |
553 | return NULL; | |
554 | } | |
555 | ||
556 | ins.first->second = sym; | |
557 | oldsym = NULL; | |
558 | } | |
559 | } | |
560 | ||
561 | if (oldsym != NULL) | |
562 | { | |
563 | assert(sym == NULL); | |
564 | ||
565 | sym = this->get_sized_symbol SELECT_SIZE_NAME (oldsym | |
566 | SELECT_SIZE(size)); | |
567 | assert(sym->source() == Symbol::FROM_OBJECT); | |
568 | const int old_shnum = sym->shnum(); | |
569 | if (old_shnum != elfcpp::SHN_UNDEF | |
570 | && old_shnum != elfcpp::SHN_COMMON | |
571 | && !sym->object()->is_dynamic()) | |
572 | { | |
573 | fprintf(stderr, "%s: linker defined: multiple definition of %s\n", | |
574 | program_name, name); | |
575 | // FIXME: Report old location. Record that we have seen an | |
576 | // error. | |
577 | return NULL; | |
578 | } | |
579 | ||
580 | // Our new definition is going to override the old reference. | |
581 | } | |
582 | ||
583 | return sym; | |
584 | } | |
585 | ||
586 | // Define a symbol based on an Output_data. | |
587 | ||
588 | void | |
589 | Symbol_table::define_in_output_data(Target* target, const char* name, | |
590 | Output_data* od, | |
591 | uint64_t value, uint64_t symsize, | |
592 | elfcpp::STT type, elfcpp::STB binding, | |
593 | elfcpp::STV visibility, | |
594 | unsigned char nonvis, | |
595 | bool offset_is_from_end, | |
596 | bool only_if_ref) | |
597 | { | |
598 | assert(target->get_size() == this->size_); | |
599 | if (this->size_ == 32) | |
600 | this->do_define_in_output_data<32>(target, name, od, value, symsize, | |
601 | type, binding, visibility, nonvis, | |
602 | offset_is_from_end, only_if_ref); | |
603 | else if (this->size_ == 64) | |
604 | this->do_define_in_output_data<64>(target, name, od, value, symsize, | |
605 | type, binding, visibility, nonvis, | |
606 | offset_is_from_end, only_if_ref); | |
607 | else | |
608 | abort(); | |
609 | } | |
610 | ||
611 | // Define a symbol in an Output_data, sized version. | |
612 | ||
613 | template<int size> | |
614 | void | |
615 | Symbol_table::do_define_in_output_data( | |
616 | Target* target, | |
617 | const char* name, | |
618 | Output_data* od, | |
619 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
620 | typename elfcpp::Elf_types<size>::Elf_WXword symsize, | |
621 | elfcpp::STT type, | |
622 | elfcpp::STB binding, | |
623 | elfcpp::STV visibility, | |
624 | unsigned char nonvis, | |
625 | bool offset_is_from_end, | |
626 | bool only_if_ref) | |
627 | { | |
628 | Sized_symbol<size>* sym; | |
629 | ||
630 | if (target->is_big_endian()) | |
631 | sym = this->define_special_symbol<size, true>(target, name, only_if_ref); | |
632 | else | |
633 | sym = this->define_special_symbol<size, false>(target, name, only_if_ref); | |
634 | ||
635 | if (sym == NULL) | |
636 | return; | |
637 | ||
638 | sym->init(name, od, value, symsize, type, binding, visibility, nonvis, | |
639 | offset_is_from_end); | |
640 | } | |
641 | ||
642 | // Define a symbol based on an Output_segment. | |
643 | ||
644 | void | |
645 | Symbol_table::define_in_output_segment(Target* target, const char* name, | |
646 | Output_segment* os, | |
647 | uint64_t value, uint64_t symsize, | |
648 | elfcpp::STT type, elfcpp::STB binding, | |
649 | elfcpp::STV visibility, | |
650 | unsigned char nonvis, | |
651 | Symbol::Segment_offset_base offset_base, | |
652 | bool only_if_ref) | |
653 | { | |
654 | assert(target->get_size() == this->size_); | |
655 | if (this->size_ == 32) | |
656 | this->do_define_in_output_segment<32>(target, name, os, value, symsize, | |
657 | type, binding, visibility, nonvis, | |
658 | offset_base, only_if_ref); | |
659 | else if (this->size_ == 64) | |
660 | this->do_define_in_output_segment<64>(target, name, os, value, symsize, | |
661 | type, binding, visibility, nonvis, | |
662 | offset_base, only_if_ref); | |
663 | else | |
664 | abort(); | |
665 | } | |
666 | ||
667 | // Define a symbol in an Output_segment, sized version. | |
668 | ||
669 | template<int size> | |
670 | void | |
671 | Symbol_table::do_define_in_output_segment( | |
672 | Target* target, | |
673 | const char* name, | |
674 | Output_segment* os, | |
675 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
676 | typename elfcpp::Elf_types<size>::Elf_WXword symsize, | |
677 | elfcpp::STT type, | |
678 | elfcpp::STB binding, | |
679 | elfcpp::STV visibility, | |
680 | unsigned char nonvis, | |
681 | Symbol::Segment_offset_base offset_base, | |
682 | bool only_if_ref) | |
683 | { | |
684 | Sized_symbol<size>* sym; | |
685 | ||
686 | if (target->is_big_endian()) | |
687 | sym = this->define_special_symbol<size, true>(target, name, only_if_ref); | |
688 | else | |
689 | sym = this->define_special_symbol<size, false>(target, name, only_if_ref); | |
690 | ||
691 | if (sym == NULL) | |
692 | return; | |
693 | ||
694 | sym->init(name, os, value, symsize, type, binding, visibility, nonvis, | |
695 | offset_base); | |
696 | } | |
697 | ||
698 | // Define a special symbol with a constant value. It is a multiple | |
699 | // definition error if this symbol is already defined. | |
700 | ||
701 | void | |
702 | Symbol_table::define_as_constant(Target* target, const char* name, | |
703 | uint64_t value, uint64_t symsize, | |
704 | elfcpp::STT type, elfcpp::STB binding, | |
705 | elfcpp::STV visibility, unsigned char nonvis, | |
706 | bool only_if_ref) | |
707 | { | |
708 | assert(target->get_size() == this->size_); | |
709 | if (this->size_ == 32) | |
710 | this->do_define_as_constant<32>(target, name, value, symsize, | |
711 | type, binding, visibility, nonvis, | |
712 | only_if_ref); | |
713 | else if (this->size_ == 64) | |
714 | this->do_define_as_constant<64>(target, name, value, symsize, | |
715 | type, binding, visibility, nonvis, | |
716 | only_if_ref); | |
717 | else | |
718 | abort(); | |
719 | } | |
720 | ||
721 | // Define a symbol as a constant, sized version. | |
722 | ||
723 | template<int size> | |
724 | void | |
725 | Symbol_table::do_define_as_constant( | |
726 | Target* target, | |
727 | const char* name, | |
728 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
729 | typename elfcpp::Elf_types<size>::Elf_WXword symsize, | |
730 | elfcpp::STT type, | |
731 | elfcpp::STB binding, | |
732 | elfcpp::STV visibility, | |
733 | unsigned char nonvis, | |
734 | bool only_if_ref) | |
735 | { | |
736 | Sized_symbol<size>* sym; | |
737 | ||
738 | if (target->is_big_endian()) | |
739 | sym = this->define_special_symbol<size, true>(target, name, only_if_ref); | |
740 | else | |
741 | sym = this->define_special_symbol<size, false>(target, name, only_if_ref); | |
742 | ||
743 | if (sym == NULL) | |
744 | return; | |
745 | ||
746 | sym->init(name, value, symsize, type, binding, visibility, nonvis); | |
747 | } | |
748 | ||
749 | // Define a set of symbols in output sections. | |
750 | ||
751 | void | |
752 | Symbol_table::define_symbols(const Layout* layout, Target* target, int count, | |
753 | const Define_symbol_in_section* p) | |
754 | { | |
755 | for (int i = 0; i < count; ++i, ++p) | |
756 | { | |
757 | Output_section* os = layout->find_output_section(p->output_section); | |
758 | if (os != NULL) | |
759 | this->define_in_output_data(target, p->name, os, p->value, p->size, | |
760 | p->type, p->binding, p->visibility, | |
761 | p->nonvis, p->offset_is_from_end, | |
762 | p->only_if_ref); | |
763 | else | |
764 | this->define_as_constant(target, p->name, 0, p->size, p->type, | |
765 | p->binding, p->visibility, p->nonvis, | |
766 | p->only_if_ref); | |
767 | } | |
768 | } | |
769 | ||
770 | // Define a set of symbols in output segments. | |
771 | ||
772 | void | |
773 | Symbol_table::define_symbols(const Layout* layout, Target* target, int count, | |
774 | const Define_symbol_in_segment* p) | |
775 | { | |
776 | for (int i = 0; i < count; ++i, ++p) | |
777 | { | |
778 | Output_segment* os = layout->find_output_segment(p->segment_type, | |
779 | p->segment_flags_set, | |
780 | p->segment_flags_clear); | |
781 | if (os != NULL) | |
782 | this->define_in_output_segment(target, p->name, os, p->value, p->size, | |
783 | p->type, p->binding, p->visibility, | |
784 | p->nonvis, p->offset_base, | |
785 | p->only_if_ref); | |
786 | else | |
787 | this->define_as_constant(target, p->name, 0, p->size, p->type, | |
788 | p->binding, p->visibility, p->nonvis, | |
789 | p->only_if_ref); | |
790 | } | |
791 | } | |
792 | ||
75f65a3e ILT |
793 | // Set the final values for all the symbols. Record the file offset |
794 | // OFF. Add their names to POOL. Return the new file offset. | |
54dc6425 | 795 | |
75f65a3e ILT |
796 | off_t |
797 | Symbol_table::finalize(off_t off, Stringpool* pool) | |
54dc6425 | 798 | { |
75f65a3e ILT |
799 | if (this->size_ == 32) |
800 | return this->sized_finalize<32>(off, pool); | |
61ba1cf9 | 801 | else if (this->size_ == 64) |
75f65a3e | 802 | return this->sized_finalize<64>(off, pool); |
61ba1cf9 ILT |
803 | else |
804 | abort(); | |
75f65a3e ILT |
805 | } |
806 | ||
ead1e424 ILT |
807 | // Set the final value for all the symbols. This is called after |
808 | // Layout::finalize, so all the output sections have their final | |
809 | // address. | |
75f65a3e ILT |
810 | |
811 | template<int size> | |
812 | off_t | |
813 | Symbol_table::sized_finalize(off_t off, Stringpool* pool) | |
814 | { | |
ead1e424 | 815 | off = align_address(off, size >> 3); |
75f65a3e ILT |
816 | this->offset_ = off; |
817 | ||
818 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
819 | Symbol_table_type::iterator p = this->table_.begin(); | |
61ba1cf9 | 820 | size_t count = 0; |
75f65a3e | 821 | while (p != this->table_.end()) |
54dc6425 | 822 | { |
75f65a3e | 823 | Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second); |
54dc6425 | 824 | |
75f65a3e ILT |
825 | // FIXME: Here we need to decide which symbols should go into |
826 | // the output file. | |
827 | ||
ead1e424 | 828 | typename Sized_symbol<size>::Value_type value; |
75f65a3e | 829 | |
ead1e424 | 830 | switch (sym->source()) |
75f65a3e | 831 | { |
ead1e424 ILT |
832 | case Symbol::FROM_OBJECT: |
833 | { | |
834 | unsigned int shnum = sym->shnum(); | |
835 | ||
836 | // FIXME: We need some target specific support here. | |
837 | if (shnum >= elfcpp::SHN_LORESERVE | |
838 | && shnum != elfcpp::SHN_ABS) | |
839 | { | |
840 | fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"), | |
841 | program_name, sym->name(), shnum); | |
842 | gold_exit(false); | |
843 | } | |
844 | ||
845 | if (shnum == elfcpp::SHN_UNDEF) | |
846 | value = 0; | |
847 | else if (shnum == elfcpp::SHN_ABS) | |
848 | value = sym->value(); | |
849 | else | |
850 | { | |
851 | off_t secoff; | |
852 | Output_section* os = sym->object()->output_section(shnum, | |
853 | &secoff); | |
854 | ||
855 | if (os == NULL) | |
856 | { | |
857 | // We should be able to erase this symbol from the | |
858 | // symbol table, but at least with gcc 4.0.2 | |
859 | // std::unordered_map::erase doesn't appear to return | |
860 | // the new iterator. | |
861 | // p = this->table_.erase(p); | |
862 | ++p; | |
863 | continue; | |
864 | } | |
865 | ||
866 | value = sym->value() + os->address() + secoff; | |
867 | } | |
868 | } | |
869 | break; | |
870 | ||
871 | case Symbol::IN_OUTPUT_DATA: | |
872 | { | |
873 | Output_data* od = sym->output_data(); | |
874 | value = sym->value() + od->address(); | |
875 | if (sym->offset_is_from_end()) | |
876 | value += od->data_size(); | |
877 | } | |
878 | break; | |
879 | ||
880 | case Symbol::IN_OUTPUT_SEGMENT: | |
881 | { | |
882 | Output_segment* os = sym->output_segment(); | |
883 | value = sym->value() + os->vaddr(); | |
884 | switch (sym->offset_base()) | |
885 | { | |
886 | case Symbol::SEGMENT_START: | |
887 | break; | |
888 | case Symbol::SEGMENT_END: | |
889 | value += os->memsz(); | |
890 | break; | |
891 | case Symbol::SEGMENT_BSS: | |
892 | value += os->filesz(); | |
893 | break; | |
894 | default: | |
895 | abort(); | |
896 | } | |
897 | } | |
898 | break; | |
899 | ||
900 | case Symbol::CONSTANT: | |
901 | value = sym->value(); | |
902 | break; | |
903 | ||
904 | default: | |
905 | abort(); | |
54dc6425 | 906 | } |
ead1e424 ILT |
907 | |
908 | sym->set_value(value); | |
909 | pool->add(sym->name()); | |
910 | ++count; | |
911 | off += sym_size; | |
912 | ++p; | |
54dc6425 | 913 | } |
75f65a3e | 914 | |
61ba1cf9 ILT |
915 | this->output_count_ = count; |
916 | ||
75f65a3e | 917 | return off; |
54dc6425 ILT |
918 | } |
919 | ||
61ba1cf9 ILT |
920 | // Write out the global symbols. |
921 | ||
922 | void | |
923 | Symbol_table::write_globals(const Target* target, const Stringpool* sympool, | |
924 | Output_file* of) const | |
925 | { | |
926 | if (this->size_ == 32) | |
927 | { | |
928 | if (target->is_big_endian()) | |
929 | this->sized_write_globals<32, true>(target, sympool, of); | |
930 | else | |
931 | this->sized_write_globals<32, false>(target, sympool, of); | |
932 | } | |
933 | else if (this->size_ == 64) | |
934 | { | |
935 | if (target->is_big_endian()) | |
936 | this->sized_write_globals<64, true>(target, sympool, of); | |
937 | else | |
938 | this->sized_write_globals<64, false>(target, sympool, of); | |
939 | } | |
940 | else | |
941 | abort(); | |
942 | } | |
943 | ||
944 | // Write out the global symbols. | |
945 | ||
946 | template<int size, bool big_endian> | |
947 | void | |
948 | Symbol_table::sized_write_globals(const Target*, | |
949 | const Stringpool* sympool, | |
950 | Output_file* of) const | |
951 | { | |
952 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
953 | unsigned char* psyms = of->get_output_view(this->offset_, | |
954 | this->output_count_ * sym_size); | |
955 | unsigned char* ps = psyms; | |
956 | for (Symbol_table_type::const_iterator p = this->table_.begin(); | |
957 | p != this->table_.end(); | |
958 | ++p) | |
959 | { | |
960 | Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second); | |
961 | ||
962 | // FIXME: This repeats sized_finalize(). | |
963 | ||
ead1e424 ILT |
964 | unsigned int shndx; |
965 | switch (sym->source()) | |
966 | { | |
967 | case Symbol::FROM_OBJECT: | |
968 | { | |
969 | unsigned int shnum = sym->shnum(); | |
970 | ||
971 | // FIXME: We need some target specific support here. | |
972 | if (shnum >= elfcpp::SHN_LORESERVE | |
973 | && shnum != elfcpp::SHN_ABS) | |
974 | { | |
975 | fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"), | |
976 | program_name, sym->name(), sym->shnum()); | |
977 | gold_exit(false); | |
978 | } | |
979 | ||
980 | if (shnum == elfcpp::SHN_UNDEF || shnum == elfcpp::SHN_ABS) | |
981 | shndx = shnum; | |
982 | else | |
983 | { | |
984 | off_t secoff; | |
985 | Output_section* os = sym->object()->output_section(shnum, | |
986 | &secoff); | |
987 | if (os == NULL) | |
988 | continue; | |
989 | ||
990 | shndx = os->out_shndx(); | |
991 | } | |
992 | } | |
993 | break; | |
994 | ||
995 | case Symbol::IN_OUTPUT_DATA: | |
996 | shndx = sym->output_data()->out_shndx(); | |
997 | break; | |
998 | ||
999 | case Symbol::IN_OUTPUT_SEGMENT: | |
1000 | shndx = elfcpp::SHN_ABS; | |
1001 | break; | |
1002 | ||
1003 | case Symbol::CONSTANT: | |
1004 | shndx = elfcpp::SHN_ABS; | |
1005 | break; | |
1006 | ||
1007 | default: | |
1008 | abort(); | |
1009 | } | |
61ba1cf9 ILT |
1010 | |
1011 | elfcpp::Sym_write<size, big_endian> osym(ps); | |
1012 | osym.put_st_name(sympool->get_offset(sym->name())); | |
1013 | osym.put_st_value(sym->value()); | |
1014 | osym.put_st_size(sym->symsize()); | |
1015 | osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type())); | |
ead1e424 ILT |
1016 | osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), |
1017 | sym->nonvis())); | |
1018 | osym.put_st_shndx(shndx); | |
61ba1cf9 ILT |
1019 | |
1020 | ps += sym_size; | |
1021 | } | |
1022 | ||
1023 | of->write_output_view(this->offset_, this->output_count_ * sym_size, psyms); | |
1024 | } | |
1025 | ||
14bfc3f5 ILT |
1026 | // Instantiate the templates we need. We could use the configure |
1027 | // script to restrict this to only the ones needed for implemented | |
1028 | // targets. | |
1029 | ||
1030 | template | |
1031 | void | |
1032 | Symbol_table::add_from_object<32, true>( | |
1033 | Sized_object<32, true>* object, | |
1034 | const elfcpp::Sym<32, true>* syms, | |
1035 | size_t count, | |
1036 | const char* sym_names, | |
1037 | size_t sym_name_size, | |
1038 | Symbol** sympointers); | |
1039 | ||
1040 | template | |
1041 | void | |
1042 | Symbol_table::add_from_object<32, false>( | |
1043 | Sized_object<32, false>* object, | |
1044 | const elfcpp::Sym<32, false>* syms, | |
1045 | size_t count, | |
1046 | const char* sym_names, | |
1047 | size_t sym_name_size, | |
1048 | Symbol** sympointers); | |
1049 | ||
1050 | template | |
1051 | void | |
1052 | Symbol_table::add_from_object<64, true>( | |
1053 | Sized_object<64, true>* object, | |
1054 | const elfcpp::Sym<64, true>* syms, | |
1055 | size_t count, | |
1056 | const char* sym_names, | |
1057 | size_t sym_name_size, | |
1058 | Symbol** sympointers); | |
1059 | ||
1060 | template | |
1061 | void | |
1062 | Symbol_table::add_from_object<64, false>( | |
1063 | Sized_object<64, false>* object, | |
1064 | const elfcpp::Sym<64, false>* syms, | |
1065 | size_t count, | |
1066 | const char* sym_names, | |
1067 | size_t sym_name_size, | |
1068 | Symbol** sympointers); | |
1069 | ||
1070 | } // End namespace gold. |