]>
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. | |
593f47df ILT |
311 | ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (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; |
593f47df | 333 | sym2 = this->get_sized_symbol SELECT_SIZE_NAME(size) ( |
5482377d ILT |
334 | insdef.first->second |
335 | SELECT_SIZE(size)); | |
593f47df | 336 | Symbol_table::resolve SELECT_SIZE_ENDIAN_NAME(size, big_endian) ( |
5482377d | 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. | |
593f47df ILT |
355 | ret = this->get_sized_symbol SELECT_SIZE_NAME(size) ( |
356 | insdef.first->second | |
357 | SELECT_SIZE(size)); | |
14bfc3f5 ILT |
358 | Symbol_table::resolve(ret, sym, object); |
359 | ins.first->second = ret; | |
360 | } | |
361 | else | |
362 | { | |
14bfc3f5 | 363 | Sized_target<size, big_endian>* target = object->sized_target(); |
1564db8d ILT |
364 | if (!target->has_make_symbol()) |
365 | ret = new Sized_symbol<size>(); | |
366 | else | |
14bfc3f5 | 367 | { |
1564db8d ILT |
368 | ret = target->make_symbol(); |
369 | if (ret == NULL) | |
14bfc3f5 ILT |
370 | { |
371 | // This means that we don't want a symbol table | |
372 | // entry after all. | |
373 | if (!def) | |
374 | this->table_.erase(ins.first); | |
375 | else | |
376 | { | |
377 | this->table_.erase(insdef.first); | |
378 | // Inserting insdef invalidated ins. | |
379 | this->table_.erase(std::make_pair(name, version)); | |
380 | } | |
381 | return NULL; | |
382 | } | |
383 | } | |
14bfc3f5 | 384 | |
1564db8d ILT |
385 | ret->init(name, version, object, sym); |
386 | ||
14bfc3f5 ILT |
387 | ins.first->second = ret; |
388 | if (def) | |
389 | { | |
390 | // This is the first time we have seen NAME/NULL. Point | |
391 | // it at the new entry for NAME/VERSION. | |
392 | assert(insdef.second); | |
393 | insdef.first->second = ret; | |
394 | } | |
395 | } | |
396 | } | |
397 | ||
ead1e424 ILT |
398 | // Record every time we see a new undefined symbol, to speed up |
399 | // archive groups. | |
400 | if (!was_undefined && ret->is_undefined()) | |
401 | ++this->saw_undefined_; | |
402 | ||
403 | // Keep track of common symbols, to speed up common symbol | |
404 | // allocation. | |
405 | if (!was_common && ret->is_common()) | |
406 | this->commons_.push_back(ret); | |
407 | ||
14bfc3f5 ILT |
408 | return ret; |
409 | } | |
410 | ||
411 | // Add all the symbols in an object to the hash table. | |
412 | ||
413 | template<int size, bool big_endian> | |
414 | void | |
415 | Symbol_table::add_from_object( | |
416 | Sized_object<size, big_endian>* object, | |
417 | const elfcpp::Sym<size, big_endian>* syms, | |
418 | size_t count, | |
419 | const char* sym_names, | |
420 | size_t sym_name_size, | |
421 | Symbol** sympointers) | |
422 | { | |
423 | // We take the size from the first object we see. | |
424 | if (this->get_size() == 0) | |
425 | this->set_size(size); | |
426 | ||
427 | if (size != this->get_size() || size != object->target()->get_size()) | |
428 | { | |
429 | fprintf(stderr, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"), | |
430 | program_name, object->name().c_str()); | |
431 | gold_exit(false); | |
432 | } | |
433 | ||
a783673b ILT |
434 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; |
435 | ||
14bfc3f5 | 436 | const unsigned char* p = reinterpret_cast<const unsigned char*>(syms); |
a783673b | 437 | for (size_t i = 0; i < count; ++i, p += sym_size) |
14bfc3f5 ILT |
438 | { |
439 | elfcpp::Sym<size, big_endian> sym(p); | |
a783673b | 440 | elfcpp::Sym<size, big_endian>* psym = &sym; |
14bfc3f5 | 441 | |
a783673b | 442 | unsigned int st_name = psym->get_st_name(); |
14bfc3f5 ILT |
443 | if (st_name >= sym_name_size) |
444 | { | |
54dc6425 ILT |
445 | fprintf(stderr, |
446 | _("%s: %s: bad global symbol name offset %u at %lu\n"), | |
14bfc3f5 ILT |
447 | program_name, object->name().c_str(), st_name, |
448 | static_cast<unsigned long>(i)); | |
449 | gold_exit(false); | |
450 | } | |
451 | ||
a783673b ILT |
452 | // A symbol defined in a section which we are not including must |
453 | // be treated as an undefined symbol. | |
454 | unsigned char symbuf[sym_size]; | |
455 | elfcpp::Sym<size, big_endian> sym2(symbuf); | |
456 | unsigned int st_shndx = psym->get_st_shndx(); | |
457 | if (st_shndx != elfcpp::SHN_UNDEF | |
458 | && st_shndx < elfcpp::SHN_LORESERVE | |
459 | && !object->is_section_included(st_shndx)) | |
460 | { | |
461 | memcpy(symbuf, p, sym_size); | |
462 | elfcpp::Sym_write<size, big_endian> sw(symbuf); | |
463 | sw.put_st_shndx(elfcpp::SHN_UNDEF); | |
464 | psym = &sym2; | |
465 | } | |
466 | ||
14bfc3f5 ILT |
467 | const char* name = sym_names + st_name; |
468 | ||
469 | // In an object file, an '@' in the name separates the symbol | |
470 | // name from the version name. If there are two '@' characters, | |
471 | // this is the default version. | |
472 | const char* ver = strchr(name, '@'); | |
473 | ||
474 | Symbol* res; | |
475 | if (ver == NULL) | |
476 | { | |
477 | name = this->namepool_.add(name); | |
a783673b | 478 | res = this->add_from_object(object, name, NULL, false, *psym); |
14bfc3f5 ILT |
479 | } |
480 | else | |
481 | { | |
482 | name = this->namepool_.add(name, ver - name); | |
483 | bool def = false; | |
484 | ++ver; | |
485 | if (*ver == '@') | |
486 | { | |
487 | def = true; | |
488 | ++ver; | |
489 | } | |
490 | ver = this->namepool_.add(ver); | |
a783673b | 491 | res = this->add_from_object(object, name, ver, def, *psym); |
14bfc3f5 ILT |
492 | } |
493 | ||
494 | *sympointers++ = res; | |
14bfc3f5 ILT |
495 | } |
496 | } | |
497 | ||
ead1e424 ILT |
498 | // Create and return a specially defined symbol. If ONLY_IF_REF is |
499 | // true, then only create the symbol if there is a reference to it. | |
500 | ||
501 | template<int size, bool big_endian> | |
502 | Sized_symbol<size>* | |
503 | Symbol_table::define_special_symbol(Target* target, const char* name, | |
593f47df ILT |
504 | bool only_if_ref |
505 | ACCEPT_SIZE_ENDIAN) | |
ead1e424 ILT |
506 | { |
507 | assert(this->size_ == size); | |
508 | ||
509 | Symbol* oldsym; | |
510 | Sized_symbol<size>* sym; | |
511 | ||
512 | if (only_if_ref) | |
513 | { | |
514 | oldsym = this->lookup(name, NULL); | |
515 | if (oldsym == NULL) | |
516 | return NULL; | |
517 | sym = NULL; | |
518 | ||
519 | // Canonicalize NAME. | |
520 | name = oldsym->name(); | |
521 | } | |
522 | else | |
523 | { | |
524 | // Canonicalize NAME. | |
525 | name = this->namepool_.add(name); | |
526 | ||
527 | Symbol* const snull = NULL; | |
528 | const char* const vnull = NULL; | |
529 | std::pair<typename Symbol_table_type::iterator, bool> ins = | |
530 | this->table_.insert(std::make_pair(std::make_pair(name, vnull), | |
531 | snull)); | |
532 | ||
533 | if (!ins.second) | |
534 | { | |
535 | // We already have a symbol table entry for NAME. | |
536 | oldsym = ins.first->second; | |
537 | assert(oldsym != NULL); | |
538 | sym = NULL; | |
539 | } | |
540 | else | |
541 | { | |
542 | // We haven't seen this symbol before. | |
543 | assert(ins.first->second == NULL); | |
544 | ||
545 | if (!target->has_make_symbol()) | |
546 | sym = new Sized_symbol<size>(); | |
547 | else | |
548 | { | |
549 | assert(target->get_size() == size); | |
550 | assert(target->is_big_endian() ? big_endian : !big_endian); | |
551 | typedef Sized_target<size, big_endian> My_target; | |
552 | My_target* sized_target = static_cast<My_target*>(target); | |
553 | sym = sized_target->make_symbol(); | |
554 | if (sym == NULL) | |
555 | return NULL; | |
556 | } | |
557 | ||
558 | ins.first->second = sym; | |
559 | oldsym = NULL; | |
560 | } | |
561 | } | |
562 | ||
563 | if (oldsym != NULL) | |
564 | { | |
565 | assert(sym == NULL); | |
566 | ||
593f47df ILT |
567 | sym = this->get_sized_symbol SELECT_SIZE_NAME(size) (oldsym |
568 | SELECT_SIZE(size)); | |
ead1e424 ILT |
569 | assert(sym->source() == Symbol::FROM_OBJECT); |
570 | const int old_shnum = sym->shnum(); | |
571 | if (old_shnum != elfcpp::SHN_UNDEF | |
572 | && old_shnum != elfcpp::SHN_COMMON | |
573 | && !sym->object()->is_dynamic()) | |
574 | { | |
575 | fprintf(stderr, "%s: linker defined: multiple definition of %s\n", | |
576 | program_name, name); | |
577 | // FIXME: Report old location. Record that we have seen an | |
578 | // error. | |
579 | return NULL; | |
580 | } | |
581 | ||
582 | // Our new definition is going to override the old reference. | |
583 | } | |
584 | ||
585 | return sym; | |
586 | } | |
587 | ||
588 | // Define a symbol based on an Output_data. | |
589 | ||
590 | void | |
591 | Symbol_table::define_in_output_data(Target* target, const char* name, | |
592 | Output_data* od, | |
593 | uint64_t value, uint64_t symsize, | |
594 | elfcpp::STT type, elfcpp::STB binding, | |
595 | elfcpp::STV visibility, | |
596 | unsigned char nonvis, | |
597 | bool offset_is_from_end, | |
598 | bool only_if_ref) | |
599 | { | |
600 | assert(target->get_size() == this->size_); | |
601 | if (this->size_ == 32) | |
602 | this->do_define_in_output_data<32>(target, name, od, value, symsize, | |
603 | type, binding, visibility, nonvis, | |
604 | offset_is_from_end, only_if_ref); | |
605 | else if (this->size_ == 64) | |
606 | this->do_define_in_output_data<64>(target, name, od, value, symsize, | |
607 | type, binding, visibility, nonvis, | |
608 | offset_is_from_end, only_if_ref); | |
609 | else | |
610 | abort(); | |
611 | } | |
612 | ||
613 | // Define a symbol in an Output_data, sized version. | |
614 | ||
615 | template<int size> | |
616 | void | |
617 | Symbol_table::do_define_in_output_data( | |
618 | Target* target, | |
619 | const char* name, | |
620 | Output_data* od, | |
621 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
622 | typename elfcpp::Elf_types<size>::Elf_WXword symsize, | |
623 | elfcpp::STT type, | |
624 | elfcpp::STB binding, | |
625 | elfcpp::STV visibility, | |
626 | unsigned char nonvis, | |
627 | bool offset_is_from_end, | |
628 | bool only_if_ref) | |
629 | { | |
630 | Sized_symbol<size>* sym; | |
631 | ||
632 | if (target->is_big_endian()) | |
593f47df ILT |
633 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) ( |
634 | target, name, only_if_ref | |
635 | SELECT_SIZE_ENDIAN(size, true)); | |
ead1e424 | 636 | else |
593f47df ILT |
637 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) ( |
638 | target, name, only_if_ref | |
639 | SELECT_SIZE_ENDIAN(size, false)); | |
ead1e424 ILT |
640 | |
641 | if (sym == NULL) | |
642 | return; | |
643 | ||
644 | sym->init(name, od, value, symsize, type, binding, visibility, nonvis, | |
645 | offset_is_from_end); | |
646 | } | |
647 | ||
648 | // Define a symbol based on an Output_segment. | |
649 | ||
650 | void | |
651 | Symbol_table::define_in_output_segment(Target* target, const char* name, | |
652 | Output_segment* os, | |
653 | uint64_t value, uint64_t symsize, | |
654 | elfcpp::STT type, elfcpp::STB binding, | |
655 | elfcpp::STV visibility, | |
656 | unsigned char nonvis, | |
657 | Symbol::Segment_offset_base offset_base, | |
658 | bool only_if_ref) | |
659 | { | |
660 | assert(target->get_size() == this->size_); | |
661 | if (this->size_ == 32) | |
662 | this->do_define_in_output_segment<32>(target, name, os, value, symsize, | |
663 | type, binding, visibility, nonvis, | |
664 | offset_base, only_if_ref); | |
665 | else if (this->size_ == 64) | |
666 | this->do_define_in_output_segment<64>(target, name, os, value, symsize, | |
667 | type, binding, visibility, nonvis, | |
668 | offset_base, only_if_ref); | |
669 | else | |
670 | abort(); | |
671 | } | |
672 | ||
673 | // Define a symbol in an Output_segment, sized version. | |
674 | ||
675 | template<int size> | |
676 | void | |
677 | Symbol_table::do_define_in_output_segment( | |
678 | Target* target, | |
679 | const char* name, | |
680 | Output_segment* os, | |
681 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
682 | typename elfcpp::Elf_types<size>::Elf_WXword symsize, | |
683 | elfcpp::STT type, | |
684 | elfcpp::STB binding, | |
685 | elfcpp::STV visibility, | |
686 | unsigned char nonvis, | |
687 | Symbol::Segment_offset_base offset_base, | |
688 | bool only_if_ref) | |
689 | { | |
690 | Sized_symbol<size>* sym; | |
691 | ||
692 | if (target->is_big_endian()) | |
593f47df ILT |
693 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) ( |
694 | target, name, only_if_ref | |
695 | SELECT_SIZE_ENDIAN(size, true)); | |
ead1e424 | 696 | else |
593f47df ILT |
697 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) ( |
698 | target, name, only_if_ref | |
699 | SELECT_SIZE_ENDIAN(size, false)); | |
ead1e424 ILT |
700 | |
701 | if (sym == NULL) | |
702 | return; | |
703 | ||
704 | sym->init(name, os, value, symsize, type, binding, visibility, nonvis, | |
705 | offset_base); | |
706 | } | |
707 | ||
708 | // Define a special symbol with a constant value. It is a multiple | |
709 | // definition error if this symbol is already defined. | |
710 | ||
711 | void | |
712 | Symbol_table::define_as_constant(Target* target, const char* name, | |
713 | uint64_t value, uint64_t symsize, | |
714 | elfcpp::STT type, elfcpp::STB binding, | |
715 | elfcpp::STV visibility, unsigned char nonvis, | |
716 | bool only_if_ref) | |
717 | { | |
718 | assert(target->get_size() == this->size_); | |
719 | if (this->size_ == 32) | |
720 | this->do_define_as_constant<32>(target, name, value, symsize, | |
721 | type, binding, visibility, nonvis, | |
722 | only_if_ref); | |
723 | else if (this->size_ == 64) | |
724 | this->do_define_as_constant<64>(target, name, value, symsize, | |
725 | type, binding, visibility, nonvis, | |
726 | only_if_ref); | |
727 | else | |
728 | abort(); | |
729 | } | |
730 | ||
731 | // Define a symbol as a constant, sized version. | |
732 | ||
733 | template<int size> | |
734 | void | |
735 | Symbol_table::do_define_as_constant( | |
736 | Target* target, | |
737 | const char* name, | |
738 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
739 | typename elfcpp::Elf_types<size>::Elf_WXword symsize, | |
740 | elfcpp::STT type, | |
741 | elfcpp::STB binding, | |
742 | elfcpp::STV visibility, | |
743 | unsigned char nonvis, | |
744 | bool only_if_ref) | |
745 | { | |
746 | Sized_symbol<size>* sym; | |
747 | ||
748 | if (target->is_big_endian()) | |
593f47df ILT |
749 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) ( |
750 | target, name, only_if_ref | |
751 | SELECT_SIZE_ENDIAN(size, true)); | |
ead1e424 | 752 | else |
593f47df ILT |
753 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) ( |
754 | target, name, only_if_ref | |
755 | SELECT_SIZE_ENDIAN(size, false)); | |
ead1e424 ILT |
756 | |
757 | if (sym == NULL) | |
758 | return; | |
759 | ||
760 | sym->init(name, value, symsize, type, binding, visibility, nonvis); | |
761 | } | |
762 | ||
763 | // Define a set of symbols in output sections. | |
764 | ||
765 | void | |
766 | Symbol_table::define_symbols(const Layout* layout, Target* target, int count, | |
767 | const Define_symbol_in_section* p) | |
768 | { | |
769 | for (int i = 0; i < count; ++i, ++p) | |
770 | { | |
771 | Output_section* os = layout->find_output_section(p->output_section); | |
772 | if (os != NULL) | |
773 | this->define_in_output_data(target, p->name, os, p->value, p->size, | |
774 | p->type, p->binding, p->visibility, | |
775 | p->nonvis, p->offset_is_from_end, | |
776 | p->only_if_ref); | |
777 | else | |
778 | this->define_as_constant(target, p->name, 0, p->size, p->type, | |
779 | p->binding, p->visibility, p->nonvis, | |
780 | p->only_if_ref); | |
781 | } | |
782 | } | |
783 | ||
784 | // Define a set of symbols in output segments. | |
785 | ||
786 | void | |
787 | Symbol_table::define_symbols(const Layout* layout, Target* target, int count, | |
788 | const Define_symbol_in_segment* p) | |
789 | { | |
790 | for (int i = 0; i < count; ++i, ++p) | |
791 | { | |
792 | Output_segment* os = layout->find_output_segment(p->segment_type, | |
793 | p->segment_flags_set, | |
794 | p->segment_flags_clear); | |
795 | if (os != NULL) | |
796 | this->define_in_output_segment(target, p->name, os, p->value, p->size, | |
797 | p->type, p->binding, p->visibility, | |
798 | p->nonvis, p->offset_base, | |
799 | p->only_if_ref); | |
800 | else | |
801 | this->define_as_constant(target, p->name, 0, p->size, p->type, | |
802 | p->binding, p->visibility, p->nonvis, | |
803 | p->only_if_ref); | |
804 | } | |
805 | } | |
806 | ||
75f65a3e ILT |
807 | // Set the final values for all the symbols. Record the file offset |
808 | // OFF. Add their names to POOL. Return the new file offset. | |
54dc6425 | 809 | |
75f65a3e ILT |
810 | off_t |
811 | Symbol_table::finalize(off_t off, Stringpool* pool) | |
54dc6425 | 812 | { |
75f65a3e ILT |
813 | if (this->size_ == 32) |
814 | return this->sized_finalize<32>(off, pool); | |
61ba1cf9 | 815 | else if (this->size_ == 64) |
75f65a3e | 816 | return this->sized_finalize<64>(off, pool); |
61ba1cf9 ILT |
817 | else |
818 | abort(); | |
75f65a3e ILT |
819 | } |
820 | ||
ead1e424 ILT |
821 | // Set the final value for all the symbols. This is called after |
822 | // Layout::finalize, so all the output sections have their final | |
823 | // address. | |
75f65a3e ILT |
824 | |
825 | template<int size> | |
826 | off_t | |
827 | Symbol_table::sized_finalize(off_t off, Stringpool* pool) | |
828 | { | |
ead1e424 | 829 | off = align_address(off, size >> 3); |
75f65a3e ILT |
830 | this->offset_ = off; |
831 | ||
832 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
833 | Symbol_table_type::iterator p = this->table_.begin(); | |
61ba1cf9 | 834 | size_t count = 0; |
75f65a3e | 835 | while (p != this->table_.end()) |
54dc6425 | 836 | { |
75f65a3e | 837 | Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second); |
54dc6425 | 838 | |
75f65a3e ILT |
839 | // FIXME: Here we need to decide which symbols should go into |
840 | // the output file. | |
841 | ||
ead1e424 | 842 | typename Sized_symbol<size>::Value_type value; |
75f65a3e | 843 | |
ead1e424 | 844 | switch (sym->source()) |
75f65a3e | 845 | { |
ead1e424 ILT |
846 | case Symbol::FROM_OBJECT: |
847 | { | |
848 | unsigned int shnum = sym->shnum(); | |
849 | ||
850 | // FIXME: We need some target specific support here. | |
851 | if (shnum >= elfcpp::SHN_LORESERVE | |
852 | && shnum != elfcpp::SHN_ABS) | |
853 | { | |
854 | fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"), | |
855 | program_name, sym->name(), shnum); | |
856 | gold_exit(false); | |
857 | } | |
858 | ||
859 | if (shnum == elfcpp::SHN_UNDEF) | |
860 | value = 0; | |
861 | else if (shnum == elfcpp::SHN_ABS) | |
862 | value = sym->value(); | |
863 | else | |
864 | { | |
865 | off_t secoff; | |
866 | Output_section* os = sym->object()->output_section(shnum, | |
867 | &secoff); | |
868 | ||
869 | if (os == NULL) | |
870 | { | |
871 | // We should be able to erase this symbol from the | |
872 | // symbol table, but at least with gcc 4.0.2 | |
873 | // std::unordered_map::erase doesn't appear to return | |
874 | // the new iterator. | |
875 | // p = this->table_.erase(p); | |
876 | ++p; | |
877 | continue; | |
878 | } | |
879 | ||
880 | value = sym->value() + os->address() + secoff; | |
881 | } | |
882 | } | |
883 | break; | |
884 | ||
885 | case Symbol::IN_OUTPUT_DATA: | |
886 | { | |
887 | Output_data* od = sym->output_data(); | |
888 | value = sym->value() + od->address(); | |
889 | if (sym->offset_is_from_end()) | |
890 | value += od->data_size(); | |
891 | } | |
892 | break; | |
893 | ||
894 | case Symbol::IN_OUTPUT_SEGMENT: | |
895 | { | |
896 | Output_segment* os = sym->output_segment(); | |
897 | value = sym->value() + os->vaddr(); | |
898 | switch (sym->offset_base()) | |
899 | { | |
900 | case Symbol::SEGMENT_START: | |
901 | break; | |
902 | case Symbol::SEGMENT_END: | |
903 | value += os->memsz(); | |
904 | break; | |
905 | case Symbol::SEGMENT_BSS: | |
906 | value += os->filesz(); | |
907 | break; | |
908 | default: | |
909 | abort(); | |
910 | } | |
911 | } | |
912 | break; | |
913 | ||
914 | case Symbol::CONSTANT: | |
915 | value = sym->value(); | |
916 | break; | |
917 | ||
918 | default: | |
919 | abort(); | |
54dc6425 | 920 | } |
ead1e424 ILT |
921 | |
922 | sym->set_value(value); | |
923 | pool->add(sym->name()); | |
924 | ++count; | |
925 | off += sym_size; | |
926 | ++p; | |
54dc6425 | 927 | } |
75f65a3e | 928 | |
61ba1cf9 ILT |
929 | this->output_count_ = count; |
930 | ||
75f65a3e | 931 | return off; |
54dc6425 ILT |
932 | } |
933 | ||
61ba1cf9 ILT |
934 | // Write out the global symbols. |
935 | ||
936 | void | |
937 | Symbol_table::write_globals(const Target* target, const Stringpool* sympool, | |
938 | Output_file* of) const | |
939 | { | |
940 | if (this->size_ == 32) | |
941 | { | |
942 | if (target->is_big_endian()) | |
943 | this->sized_write_globals<32, true>(target, sympool, of); | |
944 | else | |
945 | this->sized_write_globals<32, false>(target, sympool, of); | |
946 | } | |
947 | else if (this->size_ == 64) | |
948 | { | |
949 | if (target->is_big_endian()) | |
950 | this->sized_write_globals<64, true>(target, sympool, of); | |
951 | else | |
952 | this->sized_write_globals<64, false>(target, sympool, of); | |
953 | } | |
954 | else | |
955 | abort(); | |
956 | } | |
957 | ||
958 | // Write out the global symbols. | |
959 | ||
960 | template<int size, bool big_endian> | |
961 | void | |
962 | Symbol_table::sized_write_globals(const Target*, | |
963 | const Stringpool* sympool, | |
964 | Output_file* of) const | |
965 | { | |
966 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
967 | unsigned char* psyms = of->get_output_view(this->offset_, | |
968 | this->output_count_ * sym_size); | |
969 | unsigned char* ps = psyms; | |
970 | for (Symbol_table_type::const_iterator p = this->table_.begin(); | |
971 | p != this->table_.end(); | |
972 | ++p) | |
973 | { | |
974 | Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second); | |
975 | ||
976 | // FIXME: This repeats sized_finalize(). | |
977 | ||
ead1e424 ILT |
978 | unsigned int shndx; |
979 | switch (sym->source()) | |
980 | { | |
981 | case Symbol::FROM_OBJECT: | |
982 | { | |
983 | unsigned int shnum = sym->shnum(); | |
984 | ||
985 | // FIXME: We need some target specific support here. | |
986 | if (shnum >= elfcpp::SHN_LORESERVE | |
987 | && shnum != elfcpp::SHN_ABS) | |
988 | { | |
989 | fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"), | |
990 | program_name, sym->name(), sym->shnum()); | |
991 | gold_exit(false); | |
992 | } | |
993 | ||
994 | if (shnum == elfcpp::SHN_UNDEF || shnum == elfcpp::SHN_ABS) | |
995 | shndx = shnum; | |
996 | else | |
997 | { | |
998 | off_t secoff; | |
999 | Output_section* os = sym->object()->output_section(shnum, | |
1000 | &secoff); | |
1001 | if (os == NULL) | |
1002 | continue; | |
1003 | ||
1004 | shndx = os->out_shndx(); | |
1005 | } | |
1006 | } | |
1007 | break; | |
1008 | ||
1009 | case Symbol::IN_OUTPUT_DATA: | |
1010 | shndx = sym->output_data()->out_shndx(); | |
1011 | break; | |
1012 | ||
1013 | case Symbol::IN_OUTPUT_SEGMENT: | |
1014 | shndx = elfcpp::SHN_ABS; | |
1015 | break; | |
1016 | ||
1017 | case Symbol::CONSTANT: | |
1018 | shndx = elfcpp::SHN_ABS; | |
1019 | break; | |
1020 | ||
1021 | default: | |
1022 | abort(); | |
1023 | } | |
61ba1cf9 ILT |
1024 | |
1025 | elfcpp::Sym_write<size, big_endian> osym(ps); | |
1026 | osym.put_st_name(sympool->get_offset(sym->name())); | |
1027 | osym.put_st_value(sym->value()); | |
1028 | osym.put_st_size(sym->symsize()); | |
1029 | osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type())); | |
ead1e424 ILT |
1030 | osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), |
1031 | sym->nonvis())); | |
1032 | osym.put_st_shndx(shndx); | |
61ba1cf9 ILT |
1033 | |
1034 | ps += sym_size; | |
1035 | } | |
1036 | ||
1037 | of->write_output_view(this->offset_, this->output_count_ * sym_size, psyms); | |
1038 | } | |
1039 | ||
14bfc3f5 ILT |
1040 | // Instantiate the templates we need. We could use the configure |
1041 | // script to restrict this to only the ones needed for implemented | |
1042 | // targets. | |
1043 | ||
1044 | template | |
1045 | void | |
1046 | Symbol_table::add_from_object<32, true>( | |
1047 | Sized_object<32, true>* object, | |
1048 | const elfcpp::Sym<32, true>* syms, | |
1049 | size_t count, | |
1050 | const char* sym_names, | |
1051 | size_t sym_name_size, | |
1052 | Symbol** sympointers); | |
1053 | ||
1054 | template | |
1055 | void | |
1056 | Symbol_table::add_from_object<32, false>( | |
1057 | Sized_object<32, false>* object, | |
1058 | const elfcpp::Sym<32, false>* syms, | |
1059 | size_t count, | |
1060 | const char* sym_names, | |
1061 | size_t sym_name_size, | |
1062 | Symbol** sympointers); | |
1063 | ||
1064 | template | |
1065 | void | |
1066 | Symbol_table::add_from_object<64, true>( | |
1067 | Sized_object<64, true>* object, | |
1068 | const elfcpp::Sym<64, true>* syms, | |
1069 | size_t count, | |
1070 | const char* sym_names, | |
1071 | size_t sym_name_size, | |
1072 | Symbol** sympointers); | |
1073 | ||
1074 | template | |
1075 | void | |
1076 | Symbol_table::add_from_object<64, false>( | |
1077 | Sized_object<64, false>* object, | |
1078 | const elfcpp::Sym<64, false>* syms, | |
1079 | size_t count, | |
1080 | const char* sym_names, | |
1081 | size_t sym_name_size, | |
1082 | Symbol** sympointers); | |
1083 | ||
1084 | } // End namespace gold. |