Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // symtab.h -- the gold symbol table -*- C++ -*- |
2 | ||
3 | // Symbol_table | |
4 | // The symbol table. | |
5 | ||
bae7f79e ILT |
6 | #include <string> |
7 | #include <utility> | |
ead1e424 | 8 | #include <vector> |
54dc6425 | 9 | #include <cassert> |
bae7f79e ILT |
10 | |
11 | #include "elfcpp.h" | |
14bfc3f5 | 12 | #include "stringpool.h" |
f6ce93d6 | 13 | #include "object.h" |
bae7f79e ILT |
14 | |
15 | #ifndef GOLD_SYMTAB_H | |
16 | #define GOLD_SYMTAB_H | |
17 | ||
18 | namespace gold | |
19 | { | |
20 | ||
14bfc3f5 | 21 | class Object; |
f6ce93d6 | 22 | class Relobj; |
dbe717ef ILT |
23 | template<int size, bool big_endian> |
24 | class Sized_relobj; | |
f6ce93d6 | 25 | class Dynobj; |
dbe717ef ILT |
26 | template<int size, bool big_endian> |
27 | class Sized_dynobj; | |
ead1e424 ILT |
28 | class Output_data; |
29 | class Output_segment; | |
61ba1cf9 ILT |
30 | class Output_file; |
31 | class Target; | |
14bfc3f5 | 32 | |
14bfc3f5 ILT |
33 | // The base class of an entry in the symbol table. The symbol table |
34 | // can have a lot of entries, so we don't want this class to big. | |
35 | // Size dependent fields can be found in the template class | |
36 | // Sized_symbol. Targets may support their own derived classes. | |
bae7f79e | 37 | |
bae7f79e ILT |
38 | class Symbol |
39 | { | |
40 | public: | |
ead1e424 ILT |
41 | // Because we want the class to be small, we don't use any virtual |
42 | // functions. But because symbols can be defined in different | |
43 | // places, we need to classify them. This enum is the different | |
44 | // sources of symbols we support. | |
45 | enum Source | |
46 | { | |
f6ce93d6 ILT |
47 | // Symbol defined in a relocatable or dynamic input file--this is |
48 | // the most common case. | |
ead1e424 ILT |
49 | FROM_OBJECT, |
50 | // Symbol defined in an Output_data, a special section created by | |
51 | // the target. | |
52 | IN_OUTPUT_DATA, | |
53 | // Symbol defined in an Output_segment, with no associated | |
54 | // section. | |
55 | IN_OUTPUT_SEGMENT, | |
56 | // Symbol value is constant. | |
57 | CONSTANT | |
58 | }; | |
59 | ||
60 | // When the source is IN_OUTPUT_SEGMENT, we need to describe what | |
61 | // the offset means. | |
62 | enum Segment_offset_base | |
63 | { | |
64 | // From the start of the segment. | |
65 | SEGMENT_START, | |
66 | // From the end of the segment. | |
67 | SEGMENT_END, | |
68 | // From the filesz of the segment--i.e., after the loaded bytes | |
69 | // but before the bytes which are allocated but zeroed. | |
70 | SEGMENT_BSS | |
71 | }; | |
72 | ||
14bfc3f5 ILT |
73 | // Return the symbol name. |
74 | const char* | |
75 | name() const | |
76 | { return this->name_; } | |
77 | ||
78 | // Return the symbol version. This will return NULL for an | |
79 | // unversioned symbol. | |
80 | const char* | |
81 | version() const | |
82 | { return this->version_; } | |
83 | ||
ead1e424 ILT |
84 | // Return the symbol source. |
85 | Source | |
86 | source() const | |
87 | { return this->source_; } | |
88 | ||
14bfc3f5 ILT |
89 | // Return the object with which this symbol is associated. |
90 | Object* | |
91 | object() const | |
ead1e424 ILT |
92 | { |
93 | assert(this->source_ == FROM_OBJECT); | |
94 | return this->u_.from_object.object; | |
95 | } | |
96 | ||
f6ce93d6 ILT |
97 | // Return the index of the section in the input relocatable or |
98 | // dynamic object file. | |
ead1e424 ILT |
99 | unsigned int |
100 | shnum() const | |
101 | { | |
102 | assert(this->source_ == FROM_OBJECT); | |
103 | return this->u_.from_object.shnum; | |
104 | } | |
105 | ||
106 | // Return the output data section with which this symbol is | |
107 | // associated, if the symbol was specially defined with respect to | |
108 | // an output data section. | |
109 | Output_data* | |
110 | output_data() const | |
111 | { | |
112 | assert(this->source_ == IN_OUTPUT_DATA); | |
113 | return this->u_.in_output_data.output_data; | |
114 | } | |
115 | ||
116 | // If this symbol was defined with respect to an output data | |
117 | // section, return whether the value is an offset from end. | |
118 | bool | |
119 | offset_is_from_end() const | |
120 | { | |
121 | assert(this->source_ == IN_OUTPUT_DATA); | |
122 | return this->u_.in_output_data.offset_is_from_end; | |
123 | } | |
124 | ||
125 | // Return the output segment with which this symbol is associated, | |
126 | // if the symbol was specially defined with respect to an output | |
127 | // segment. | |
128 | Output_segment* | |
129 | output_segment() const | |
130 | { | |
131 | assert(this->source_ == IN_OUTPUT_SEGMENT); | |
132 | return this->u_.in_output_segment.output_segment; | |
133 | } | |
134 | ||
135 | // If this symbol was defined with respect to an output segment, | |
136 | // return the offset base. | |
137 | Segment_offset_base | |
138 | offset_base() const | |
139 | { | |
140 | assert(this->source_ == IN_OUTPUT_SEGMENT); | |
141 | return this->u_.in_output_segment.offset_base; | |
142 | } | |
14bfc3f5 ILT |
143 | |
144 | // Return the symbol binding. | |
145 | elfcpp::STB | |
146 | binding() const | |
147 | { return this->binding_; } | |
148 | ||
1564db8d ILT |
149 | // Return the symbol type. |
150 | elfcpp::STT | |
151 | type() const | |
152 | { return this->type_; } | |
153 | ||
154 | // Return the symbol visibility. | |
155 | elfcpp::STV | |
156 | visibility() const | |
157 | { return this->visibility_; } | |
158 | ||
159 | // Return the non-visibility part of the st_other field. | |
160 | unsigned char | |
ead1e424 ILT |
161 | nonvis() const |
162 | { return this->nonvis_; } | |
14bfc3f5 | 163 | |
1564db8d ILT |
164 | // Return whether this symbol is a forwarder. This will never be |
165 | // true of a symbol found in the hash table, but may be true of | |
166 | // symbol pointers attached to object files. | |
167 | bool | |
168 | is_forwarder() const | |
169 | { return this->is_forwarder_; } | |
170 | ||
171 | // Mark this symbol as a forwarder. | |
172 | void | |
173 | set_forwarder() | |
174 | { this->is_forwarder_ = true; } | |
175 | ||
c06b7b0b ILT |
176 | // Return whether this symbol needs an entry in the dynamic symbol |
177 | // table. | |
178 | bool | |
179 | needs_dynsym_entry() const | |
180 | { return this->needs_dynsym_entry_; } | |
181 | ||
182 | // Mark this symbol as needing an entry in the dynamic symbol table. | |
183 | void | |
184 | set_needs_dynsym_entry() | |
185 | { this->needs_dynsym_entry_ = true; } | |
186 | ||
f6ce93d6 | 187 | // Return whether this symbol was ever seen in a dynamic object. |
1564db8d ILT |
188 | bool |
189 | in_dyn() const | |
190 | { return this->in_dyn_; } | |
191 | ||
f6ce93d6 | 192 | // Mark this symbol as having been seen in a dynamic object. |
1564db8d ILT |
193 | void |
194 | set_in_dyn() | |
195 | { this->in_dyn_ = true; } | |
196 | ||
c06b7b0b ILT |
197 | // Return the index of this symbol in the output file symbol table. |
198 | // A value of -1U means that this symbol is not going into the | |
199 | // output file. This starts out as zero, and is set to a non-zero | |
200 | // value by Symbol_table::finalize. It is an error to ask for the | |
201 | // symbol table index before it has been set. | |
202 | unsigned int | |
203 | symtab_index() const | |
204 | { | |
205 | assert(this->symtab_index_ != 0); | |
206 | return this->symtab_index_; | |
207 | } | |
208 | ||
209 | // Set the index of the symbol in the output file symbol table. | |
210 | void | |
211 | set_symtab_index(unsigned int index) | |
212 | { | |
213 | assert(index != 0); | |
214 | this->symtab_index_ = index; | |
215 | } | |
216 | ||
217 | // Return the index of this symbol in the dynamic symbol table. A | |
218 | // value of -1U means that this symbol is not going into the dynamic | |
219 | // symbol table. This starts out as zero, and is set to a non-zero | |
220 | // during Layout::finalize. It is an error to ask for the dynamic | |
221 | // symbol table index before it has been set. | |
222 | unsigned int | |
223 | dynsym_index() const | |
224 | { | |
225 | assert(this->dynsym_index_ != 0); | |
226 | return this->dynsym_index_; | |
227 | } | |
228 | ||
229 | // Set the index of the symbol in the dynamic symbol table. | |
230 | void | |
231 | set_dynsym_index(unsigned int index) | |
232 | { | |
233 | assert(index != 0); | |
234 | this->dynsym_index_ = index; | |
235 | } | |
236 | ||
ead1e424 | 237 | // Return whether this symbol has an entry in the GOT section. |
92e059d8 | 238 | bool |
ead1e424 ILT |
239 | has_got_offset() const |
240 | { return this->has_got_offset_; } | |
241 | ||
242 | // Return the offset into the GOT section of this symbol. | |
243 | unsigned int | |
244 | got_offset() const | |
245 | { | |
246 | assert(this->has_got_offset()); | |
247 | return this->got_offset_; | |
248 | } | |
249 | ||
250 | // Set the GOT offset of this symbol. | |
251 | void | |
252 | set_got_offset(unsigned int got_offset) | |
253 | { | |
254 | this->has_got_offset_ = true; | |
255 | this->got_offset_ = got_offset; | |
256 | } | |
257 | ||
258 | // Return whether this symbol is resolved locally. This is always | |
259 | // true when linking statically. It is true for a symbol defined in | |
260 | // this object when using -Bsymbolic. It is true for a symbol | |
261 | // marked local in a version file. FIXME: This needs to be | |
262 | // completed. | |
263 | bool | |
264 | is_resolved_locally() const | |
265 | { return !this->in_dyn_; } | |
266 | ||
f6ce93d6 ILT |
267 | // Return whether this is a defined symbol (not undefined or |
268 | // common). | |
269 | bool | |
270 | is_defined() const | |
271 | { | |
272 | return (this->source_ != FROM_OBJECT | |
273 | || (this->u_.from_object.shnum != elfcpp::SHN_UNDEF | |
274 | && this->u_.from_object.shnum != elfcpp::SHN_COMMON)); | |
275 | } | |
276 | ||
ead1e424 ILT |
277 | // Return whether this is an undefined symbol. |
278 | bool | |
279 | is_undefined() const | |
280 | { | |
281 | return this->source_ == FROM_OBJECT && this->shnum() == elfcpp::SHN_UNDEF; | |
282 | } | |
283 | ||
284 | // Return whether this is a common symbol. | |
285 | bool | |
286 | is_common() const | |
287 | { | |
f6ce93d6 ILT |
288 | return (this->source_ == FROM_OBJECT |
289 | && (this->u_.from_object.shnum == elfcpp::SHN_COMMON | |
290 | || this->type_ == elfcpp::STT_COMMON)); | |
ead1e424 | 291 | } |
92e059d8 | 292 | |
f6ce93d6 ILT |
293 | // Return whether there should be a warning for references to this |
294 | // symbol. | |
295 | bool | |
296 | has_warning() const | |
297 | { return this->has_warning_; } | |
298 | ||
299 | // Mark this symbol as having a warning. | |
300 | void | |
301 | set_has_warning() | |
302 | { this->has_warning_ = true; } | |
303 | ||
14bfc3f5 ILT |
304 | protected: |
305 | // Instances of this class should always be created at a specific | |
306 | // size. | |
307 | Symbol() | |
f6ce93d6 | 308 | { memset(this, 0, sizeof *this); } |
14bfc3f5 | 309 | |
ead1e424 ILT |
310 | // Initialize the general fields. |
311 | void | |
312 | init_fields(const char* name, const char* version, | |
313 | elfcpp::STT type, elfcpp::STB binding, | |
314 | elfcpp::STV visibility, unsigned char nonvis); | |
315 | ||
14bfc3f5 ILT |
316 | // Initialize fields from an ELF symbol in OBJECT. |
317 | template<int size, bool big_endian> | |
318 | void | |
319 | init_base(const char *name, const char* version, Object* object, | |
320 | const elfcpp::Sym<size, big_endian>&); | |
bae7f79e | 321 | |
ead1e424 ILT |
322 | // Initialize fields for an Output_data. |
323 | void | |
324 | init_base(const char* name, Output_data*, elfcpp::STT, elfcpp::STB, | |
325 | elfcpp::STV, unsigned char nonvis, bool offset_is_from_end); | |
326 | ||
327 | // Initialize fields for an Output_segment. | |
328 | void | |
329 | init_base(const char* name, Output_segment* os, elfcpp::STT type, | |
330 | elfcpp::STB binding, elfcpp::STV visibility, | |
331 | unsigned char nonvis, Segment_offset_base offset_base); | |
332 | ||
333 | // Initialize fields for a constant. | |
334 | void | |
335 | init_base(const char* name, elfcpp::STT type, elfcpp::STB binding, | |
336 | elfcpp::STV visibility, unsigned char nonvis); | |
337 | ||
1564db8d ILT |
338 | // Override existing symbol. |
339 | template<int size, bool big_endian> | |
340 | void | |
341 | override_base(const elfcpp::Sym<size, big_endian>&, Object* object); | |
342 | ||
bae7f79e | 343 | private: |
14bfc3f5 ILT |
344 | Symbol(const Symbol&); |
345 | Symbol& operator=(const Symbol&); | |
346 | ||
347 | // Symbol name (expected to point into a Stringpool). | |
348 | const char* name_; | |
349 | // Symbol version (expected to point into a Stringpool). This may | |
350 | // be NULL. | |
bae7f79e | 351 | const char* version_; |
ead1e424 ILT |
352 | |
353 | union | |
354 | { | |
355 | // This struct is used if SOURCE_ == FROM_OBJECT. | |
356 | struct | |
357 | { | |
358 | // Object in which symbol is defined, or in which it was first | |
359 | // seen. | |
360 | Object* object; | |
361 | // Section number in object_ in which symbol is defined. | |
362 | unsigned int shnum; | |
363 | } from_object; | |
364 | ||
365 | // This struct is used if SOURCE_ == IN_OUTPUT_DATA. | |
366 | struct | |
367 | { | |
368 | // Output_data in which symbol is defined. Before | |
369 | // Layout::finalize the symbol's value is an offset within the | |
370 | // Output_data. | |
371 | Output_data* output_data; | |
372 | // True if the offset is from the end, false if the offset is | |
373 | // from the beginning. | |
374 | bool offset_is_from_end; | |
375 | } in_output_data; | |
376 | ||
377 | // This struct is used if SOURCE_ == IN_OUTPUT_SEGMENT. | |
378 | struct | |
379 | { | |
380 | // Output_segment in which the symbol is defined. Before | |
381 | // Layout::finalize the symbol's value is an offset. | |
382 | Output_segment* output_segment; | |
383 | // The base to use for the offset before Layout::finalize. | |
384 | Segment_offset_base offset_base; | |
385 | } in_output_segment; | |
386 | } u_; | |
387 | ||
c06b7b0b ILT |
388 | // The index of this symbol in the output file. If the symbol is |
389 | // not going into the output file, this value is -1U. This field | |
390 | // starts as always holding zero. It is set to a non-zero value by | |
391 | // Symbol_table::finalize. | |
392 | unsigned int symtab_index_; | |
393 | ||
394 | // The index of this symbol in the dynamic symbol table. If the | |
395 | // symbol is not going into the dynamic symbol table, this value is | |
396 | // -1U. This field starts as always holding zero. It is set to a | |
397 | // non-zero value during Layout::finalize. | |
398 | unsigned int dynsym_index_; | |
399 | ||
ead1e424 | 400 | // If this symbol has an entry in the GOT section (has_got_offset_ |
c06b7b0b | 401 | // is true), this is the offset from the start of the GOT section. |
ead1e424 | 402 | unsigned int got_offset_; |
c06b7b0b | 403 | |
14bfc3f5 | 404 | // Symbol type. |
bae7f79e | 405 | elfcpp::STT type_ : 4; |
14bfc3f5 | 406 | // Symbol binding. |
bae7f79e | 407 | elfcpp::STB binding_ : 4; |
14bfc3f5 ILT |
408 | // Symbol visibility. |
409 | elfcpp::STV visibility_ : 2; | |
410 | // Rest of symbol st_other field. | |
ead1e424 ILT |
411 | unsigned int nonvis_ : 6; |
412 | // The type of symbol. | |
f6ce93d6 | 413 | Source source_ : 3; |
14bfc3f5 ILT |
414 | // True if this symbol always requires special target-specific |
415 | // handling. | |
ead1e424 | 416 | bool is_target_special_ : 1; |
14bfc3f5 | 417 | // True if this is the default version of the symbol. |
1564db8d | 418 | bool is_def_ : 1; |
14bfc3f5 ILT |
419 | // True if this symbol really forwards to another symbol. This is |
420 | // used when we discover after the fact that two different entries | |
421 | // in the hash table really refer to the same symbol. This will | |
422 | // never be set for a symbol found in the hash table, but may be set | |
423 | // for a symbol found in the list of symbols attached to an Object. | |
424 | // It forwards to the symbol found in the forwarders_ map of | |
425 | // Symbol_table. | |
1564db8d | 426 | bool is_forwarder_ : 1; |
c06b7b0b ILT |
427 | // True if this symbol needs to be in the dynamic symbol table. |
428 | bool needs_dynsym_entry_ : 1; | |
1564db8d ILT |
429 | // True if we've seen this symbol in a dynamic object. |
430 | bool in_dyn_ : 1; | |
ead1e424 ILT |
431 | // True if the symbol has an entry in the GOT section. |
432 | bool has_got_offset_ : 1; | |
f6ce93d6 ILT |
433 | // True if there is a warning for this symbol. |
434 | bool has_warning_ : 1; | |
bae7f79e ILT |
435 | }; |
436 | ||
14bfc3f5 ILT |
437 | // The parts of a symbol which are size specific. Using a template |
438 | // derived class like this helps us use less space on a 32-bit system. | |
bae7f79e ILT |
439 | |
440 | template<int size> | |
14bfc3f5 ILT |
441 | class Sized_symbol : public Symbol |
442 | { | |
443 | public: | |
1564db8d ILT |
444 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Value_type; |
445 | typedef typename elfcpp::Elf_types<size>::Elf_WXword Size_type; | |
446 | ||
14bfc3f5 ILT |
447 | Sized_symbol() |
448 | { } | |
449 | ||
450 | // Initialize fields from an ELF symbol in OBJECT. | |
451 | template<bool big_endian> | |
452 | void | |
453 | init(const char *name, const char* version, Object* object, | |
454 | const elfcpp::Sym<size, big_endian>&); | |
455 | ||
ead1e424 ILT |
456 | // Initialize fields for an Output_data. |
457 | void | |
458 | init(const char* name, Output_data*, Value_type value, Size_type symsize, | |
459 | elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis, | |
460 | bool offset_is_from_end); | |
461 | ||
462 | // Initialize fields for an Output_segment. | |
463 | void | |
464 | init(const char* name, Output_segment*, Value_type value, Size_type symsize, | |
465 | elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis, | |
466 | Segment_offset_base offset_base); | |
467 | ||
468 | // Initialize fields for a constant. | |
469 | void | |
470 | init(const char* name, Value_type value, Size_type symsize, | |
471 | elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis); | |
472 | ||
1564db8d ILT |
473 | // Override existing symbol. |
474 | template<bool big_endian> | |
475 | void | |
476 | override(const elfcpp::Sym<size, big_endian>&, Object* object); | |
477 | ||
478 | // Return the symbol's value. | |
479 | Value_type | |
480 | value() const | |
481 | { return this->value_; } | |
482 | ||
483 | // Return the symbol's size (we can't call this 'size' because that | |
484 | // is a template parameter). | |
485 | Size_type | |
486 | symsize() const | |
ead1e424 ILT |
487 | { return this->symsize_; } |
488 | ||
489 | // Set the symbol size. This is used when resolving common symbols. | |
490 | void | |
491 | set_symsize(Size_type symsize) | |
492 | { this->symsize_ = symsize; } | |
1564db8d | 493 | |
75f65a3e ILT |
494 | // Set the symbol value. This is called when we store the final |
495 | // values of the symbols into the symbol table. | |
496 | void | |
497 | set_value(Value_type value) | |
498 | { this->value_ = value; } | |
499 | ||
14bfc3f5 ILT |
500 | private: |
501 | Sized_symbol(const Sized_symbol&); | |
502 | Sized_symbol& operator=(const Sized_symbol&); | |
503 | ||
ead1e424 ILT |
504 | // Symbol value. Before Layout::finalize this is the offset in the |
505 | // input section. This is set to the final value during | |
506 | // Layout::finalize. | |
1564db8d | 507 | Value_type value_; |
14bfc3f5 | 508 | // Symbol size. |
ead1e424 ILT |
509 | Size_type symsize_; |
510 | }; | |
511 | ||
512 | // A struct describing a symbol defined by the linker, where the value | |
513 | // of the symbol is defined based on an output section. This is used | |
514 | // for symbols defined by the linker, like "_init_array_start". | |
515 | ||
516 | struct Define_symbol_in_section | |
517 | { | |
518 | // The symbol name. | |
519 | const char* name; | |
520 | // The name of the output section with which this symbol should be | |
521 | // associated. If there is no output section with that name, the | |
522 | // symbol will be defined as zero. | |
523 | const char* output_section; | |
524 | // The offset of the symbol within the output section. This is an | |
525 | // offset from the start of the output section, unless start_at_end | |
526 | // is true, in which case this is an offset from the end of the | |
527 | // output section. | |
528 | uint64_t value; | |
529 | // The size of the symbol. | |
530 | uint64_t size; | |
531 | // The symbol type. | |
532 | elfcpp::STT type; | |
533 | // The symbol binding. | |
534 | elfcpp::STB binding; | |
535 | // The symbol visibility. | |
536 | elfcpp::STV visibility; | |
537 | // The rest of the st_other field. | |
538 | unsigned char nonvis; | |
539 | // If true, the value field is an offset from the end of the output | |
540 | // section. | |
541 | bool offset_is_from_end; | |
542 | // If true, this symbol is defined only if we see a reference to it. | |
543 | bool only_if_ref; | |
544 | }; | |
545 | ||
546 | // A struct describing a symbol defined by the linker, where the value | |
547 | // of the symbol is defined based on a segment. This is used for | |
548 | // symbols defined by the linker, like "_end". We describe the | |
549 | // segment with which the symbol should be associated by its | |
550 | // characteristics. If no segment meets these characteristics, the | |
551 | // symbol will be defined as zero. If there is more than one segment | |
552 | // which meets these characteristics, we will use the first one. | |
553 | ||
554 | struct Define_symbol_in_segment | |
555 | { | |
556 | // The symbol name. | |
557 | const char* name; | |
558 | // The segment type where the symbol should be defined, typically | |
559 | // PT_LOAD. | |
560 | elfcpp::PT segment_type; | |
561 | // Bitmask of segment flags which must be set. | |
562 | elfcpp::PF segment_flags_set; | |
563 | // Bitmask of segment flags which must be clear. | |
564 | elfcpp::PF segment_flags_clear; | |
565 | // The offset of the symbol within the segment. The offset is | |
566 | // calculated from the position set by offset_base. | |
567 | uint64_t value; | |
568 | // The size of the symbol. | |
569 | uint64_t size; | |
570 | // The symbol type. | |
571 | elfcpp::STT type; | |
572 | // The symbol binding. | |
573 | elfcpp::STB binding; | |
574 | // The symbol visibility. | |
575 | elfcpp::STV visibility; | |
576 | // The rest of the st_other field. | |
577 | unsigned char nonvis; | |
578 | // The base from which we compute the offset. | |
579 | Symbol::Segment_offset_base offset_base; | |
580 | // If true, this symbol is defined only if we see a reference to it. | |
581 | bool only_if_ref; | |
14bfc3f5 ILT |
582 | }; |
583 | ||
f6ce93d6 ILT |
584 | // This class manages warnings. Warnings are a GNU extension. When |
585 | // we see a section named .gnu.warning.SYM in an object file, and if | |
586 | // we wind using the definition of SYM from that object file, then we | |
587 | // will issue a warning for any relocation against SYM from a | |
588 | // different object file. The text of the warning is the contents of | |
589 | // the section. This is not precisely the definition used by the old | |
590 | // GNU linker; the old GNU linker treated an occurrence of | |
591 | // .gnu.warning.SYM as defining a warning symbol. A warning symbol | |
592 | // would trigger a warning on any reference. However, it was | |
593 | // inconsistent in that a warning in a dynamic object only triggered | |
594 | // if there was no definition in a regular object. This linker is | |
595 | // different in that we only issue a warning if we use the symbol | |
596 | // definition from the same object file as the warning section. | |
597 | ||
598 | class Warnings | |
599 | { | |
600 | public: | |
601 | Warnings() | |
602 | : warnings_() | |
603 | { } | |
604 | ||
605 | // Add a warning for symbol NAME in section SHNDX in object OBJ. | |
606 | void | |
607 | add_warning(Symbol_table* symtab, const char* name, Object* obj, | |
608 | unsigned int shndx); | |
609 | ||
610 | // For each symbol for which we should give a warning, make a note | |
611 | // on the symbol. | |
612 | void | |
613 | note_warnings(Symbol_table* symtab); | |
614 | ||
615 | // Issue a warning for a reference to SYM at LOCATION. | |
616 | void | |
c06b7b0b | 617 | issue_warning(const Symbol* sym, const std::string& location) const; |
f6ce93d6 ILT |
618 | |
619 | private: | |
620 | Warnings(const Warnings&); | |
621 | Warnings& operator=(const Warnings&); | |
622 | ||
623 | // What we need to know to get the warning text. | |
624 | struct Warning_location | |
625 | { | |
626 | // The object the warning is in. | |
627 | Object* object; | |
628 | // The index of the warning section. | |
629 | unsigned int shndx; | |
630 | // The warning text if we have already loaded it. | |
631 | std::string text; | |
632 | ||
633 | Warning_location() | |
634 | : object(NULL), shndx(0), text() | |
635 | { } | |
636 | ||
637 | void | |
638 | set(Object* o, unsigned int s) | |
639 | { | |
640 | this->object = o; | |
641 | this->shndx = s; | |
642 | } | |
643 | ||
644 | void | |
645 | set_text(const char* t, off_t l) | |
646 | { this->text.assign(t, l); } | |
647 | }; | |
648 | ||
649 | // A mapping from warning symbol names (canonicalized in | |
650 | // Symbol_table's namepool_ field) to | |
651 | typedef Unordered_map<const char*, Warning_location> Warning_table; | |
652 | ||
653 | Warning_table warnings_; | |
654 | }; | |
655 | ||
14bfc3f5 ILT |
656 | // The main linker symbol table. |
657 | ||
bae7f79e ILT |
658 | class Symbol_table |
659 | { | |
660 | public: | |
661 | Symbol_table(); | |
662 | ||
1564db8d | 663 | ~Symbol_table(); |
bae7f79e | 664 | |
dbe717ef | 665 | // Add COUNT external symbols from the relocatable object RELOBJ to |
f6ce93d6 ILT |
666 | // the symbol table. SYMS is the symbols, SYM_NAMES is their names, |
667 | // SYM_NAME_SIZE is the size of SYM_NAMES. This sets SYMPOINTERS to | |
668 | // point to the symbols in the symbol table. | |
14bfc3f5 ILT |
669 | template<int size, bool big_endian> |
670 | void | |
dbe717ef ILT |
671 | add_from_relobj(Sized_relobj<size, big_endian>* relobj, |
672 | const unsigned char* syms, size_t count, | |
673 | const char* sym_names, size_t sym_name_size, | |
14bfc3f5 ILT |
674 | Symbol** sympointers); |
675 | ||
dbe717ef ILT |
676 | // Add COUNT dynamic symbols from the dynamic object DYNOBJ to the |
677 | // symbol table. SYMS is the symbols. SYM_NAMES is their names. | |
678 | // SYM_NAME_SIZE is the size of SYM_NAMES. The other parameters are | |
679 | // symbol version data. | |
680 | template<int size, bool big_endian> | |
681 | void | |
682 | add_from_dynobj(Sized_dynobj<size, big_endian>* dynobj, | |
683 | const unsigned char* syms, size_t count, | |
684 | const char* sym_names, size_t sym_name_size, | |
685 | const unsigned char* versym, size_t versym_size, | |
686 | const std::vector<const char*>*); | |
687 | ||
ead1e424 ILT |
688 | // Define a special symbol. |
689 | template<int size, bool big_endian> | |
690 | Sized_symbol<size>* | |
593f47df ILT |
691 | define_special_symbol(Target* target, const char* name, bool only_if_ref |
692 | ACCEPT_SIZE_ENDIAN); | |
ead1e424 ILT |
693 | |
694 | // Define a special symbol based on an Output_data. It is a | |
695 | // multiple definition error if this symbol is already defined. | |
696 | void | |
697 | define_in_output_data(Target*, const char* name, Output_data*, | |
698 | uint64_t value, uint64_t symsize, | |
699 | elfcpp::STT type, elfcpp::STB binding, | |
700 | elfcpp::STV visibility, unsigned char nonvis, | |
701 | bool offset_is_from_end, bool only_if_ref); | |
702 | ||
703 | // Define a special symbol based on an Output_segment. It is a | |
704 | // multiple definition error if this symbol is already defined. | |
705 | void | |
706 | define_in_output_segment(Target*, const char* name, Output_segment*, | |
707 | uint64_t value, uint64_t symsize, | |
708 | elfcpp::STT type, elfcpp::STB binding, | |
709 | elfcpp::STV visibility, unsigned char nonvis, | |
710 | Symbol::Segment_offset_base, bool only_if_ref); | |
711 | ||
712 | // Define a special symbol with a constant value. It is a multiple | |
713 | // definition error if this symbol is already defined. | |
714 | void | |
715 | define_as_constant(Target*, const char* name, uint64_t value, | |
716 | uint64_t symsize, elfcpp::STT type, elfcpp::STB binding, | |
717 | elfcpp::STV visibility, unsigned char nonvis, | |
718 | bool only_if_ref); | |
719 | ||
720 | // Define a set of symbols in output sections. | |
721 | void | |
722 | define_symbols(const Layout*, Target*, int count, | |
723 | const Define_symbol_in_section*); | |
724 | ||
725 | // Define a set of symbols in output segments. | |
726 | void | |
727 | define_symbols(const Layout*, Target*, int count, | |
728 | const Define_symbol_in_segment*); | |
729 | ||
61ba1cf9 ILT |
730 | // Look up a symbol. |
731 | Symbol* | |
732 | lookup(const char*, const char* version = NULL) const; | |
733 | ||
14bfc3f5 | 734 | // Return the real symbol associated with the forwarder symbol FROM. |
bae7f79e | 735 | Symbol* |
c06b7b0b | 736 | resolve_forwards(const Symbol* from) const; |
bae7f79e | 737 | |
14bfc3f5 ILT |
738 | // Return the size of the symbols in the table. |
739 | int | |
740 | get_size() const | |
741 | { return this->size_; } | |
bae7f79e | 742 | |
1564db8d ILT |
743 | // Return the sized version of a symbol in this table. |
744 | template<int size> | |
745 | Sized_symbol<size>* | |
5482377d | 746 | get_sized_symbol(Symbol* ACCEPT_SIZE) const; |
1564db8d ILT |
747 | |
748 | template<int size> | |
749 | const Sized_symbol<size>* | |
5482377d | 750 | get_sized_symbol(const Symbol* ACCEPT_SIZE) const; |
54dc6425 | 751 | |
ead1e424 ILT |
752 | // Return the count of undefined symbols seen. |
753 | int | |
754 | saw_undefined() const | |
755 | { return this->saw_undefined_; } | |
756 | ||
757 | // Allocate the common symbols | |
758 | void | |
759 | allocate_commons(const General_options&, Layout*); | |
760 | ||
f6ce93d6 ILT |
761 | // Add a warning for symbol NAME in section SHNDX in object OBJ. |
762 | void | |
763 | add_warning(const char* name, Object* obj, unsigned int shndx) | |
764 | { this->warnings_.add_warning(this, name, obj, shndx); } | |
765 | ||
766 | // Canonicalize a symbol name for use in the hash table. | |
767 | const char* | |
768 | canonicalize_name(const char* name) | |
f0641a0b | 769 | { return this->namepool_.add(name, NULL); } |
f6ce93d6 ILT |
770 | |
771 | // Possibly issue a warning for a reference to SYM at LOCATION which | |
772 | // is in OBJ. | |
773 | void | |
c06b7b0b | 774 | issue_warning(const Symbol* sym, const std::string& location) const |
f6ce93d6 ILT |
775 | { this->warnings_.issue_warning(sym, location); } |
776 | ||
75f65a3e | 777 | // Finalize the symbol table after we have set the final addresses |
c06b7b0b ILT |
778 | // of all the input sections. This sets the final symbol indexes, |
779 | // values and adds the names to *POOL. INDEX is the index of the | |
780 | // first global symbol. This records the file offset OFF, and | |
75f65a3e ILT |
781 | // returns the new file offset. |
782 | off_t | |
c06b7b0b | 783 | finalize(unsigned int index, off_t off, Stringpool* pool); |
1564db8d | 784 | |
61ba1cf9 ILT |
785 | // Write out the global symbols. |
786 | void | |
787 | write_globals(const Target*, const Stringpool*, Output_file*) const; | |
788 | ||
bae7f79e ILT |
789 | private: |
790 | Symbol_table(const Symbol_table&); | |
791 | Symbol_table& operator=(const Symbol_table&); | |
792 | ||
14bfc3f5 ILT |
793 | // Set the size of the symbols in the table. |
794 | void | |
795 | set_size(int size) | |
796 | { this->size_ = size; } | |
797 | ||
798 | // Make FROM a forwarder symbol to TO. | |
799 | void | |
800 | make_forwarder(Symbol* from, Symbol* to); | |
801 | ||
802 | // Add a symbol. | |
803 | template<int size, bool big_endian> | |
804 | Symbol* | |
f0641a0b ILT |
805 | add_from_object(Object*, const char *name, Stringpool::Key name_key, |
806 | const char *version, Stringpool::Key version_key, | |
807 | bool def, const elfcpp::Sym<size, big_endian>& sym); | |
14bfc3f5 ILT |
808 | |
809 | // Resolve symbols. | |
810 | template<int size, bool big_endian> | |
811 | static void | |
1564db8d ILT |
812 | resolve(Sized_symbol<size>* to, |
813 | const elfcpp::Sym<size, big_endian>& sym, | |
814 | Object*); | |
14bfc3f5 | 815 | |
1564db8d | 816 | template<int size, bool big_endian> |
14bfc3f5 | 817 | static void |
5482377d ILT |
818 | resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from |
819 | ACCEPT_SIZE_ENDIAN); | |
14bfc3f5 | 820 | |
ead1e424 ILT |
821 | // Define a symbol in an Output_data, sized version. |
822 | template<int size> | |
823 | void | |
824 | do_define_in_output_data(Target*, const char* name, Output_data*, | |
825 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
826 | typename elfcpp::Elf_types<size>::Elf_WXword ssize, | |
827 | elfcpp::STT type, elfcpp::STB binding, | |
828 | elfcpp::STV visibility, unsigned char nonvis, | |
829 | bool offset_is_from_end, bool only_if_ref); | |
830 | ||
831 | // Define a symbol in an Output_segment, sized version. | |
832 | template<int size> | |
833 | void | |
834 | do_define_in_output_segment( | |
835 | Target*, const char* name, Output_segment* os, | |
836 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
837 | typename elfcpp::Elf_types<size>::Elf_WXword ssize, | |
838 | elfcpp::STT type, elfcpp::STB binding, | |
839 | elfcpp::STV visibility, unsigned char nonvis, | |
840 | Symbol::Segment_offset_base offset_base, bool only_if_ref); | |
841 | ||
842 | // Define a symbol as a constant, sized version. | |
843 | template<int size> | |
844 | void | |
845 | do_define_as_constant( | |
846 | Target*, const char* name, | |
847 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
848 | typename elfcpp::Elf_types<size>::Elf_WXword ssize, | |
849 | elfcpp::STT type, elfcpp::STB binding, | |
850 | elfcpp::STV visibility, unsigned char nonvis, | |
851 | bool only_if_ref); | |
852 | ||
853 | // Allocate the common symbols, sized version. | |
854 | template<int size> | |
855 | void | |
856 | do_allocate_commons(const General_options&, Layout*); | |
857 | ||
75f65a3e ILT |
858 | // Finalize symbols specialized for size. |
859 | template<int size> | |
860 | off_t | |
c06b7b0b | 861 | sized_finalize(unsigned int, off_t, Stringpool*); |
75f65a3e | 862 | |
61ba1cf9 ILT |
863 | // Write globals specialized for size and endianness. |
864 | template<int size, bool big_endian> | |
865 | void | |
866 | sized_write_globals(const Target*, const Stringpool*, Output_file*) const; | |
867 | ||
54dc6425 ILT |
868 | // The type of the symbol hash table. |
869 | ||
f0641a0b | 870 | typedef std::pair<Stringpool::Key, Stringpool::Key> Symbol_table_key; |
14bfc3f5 ILT |
871 | |
872 | struct Symbol_table_hash | |
873 | { | |
874 | size_t | |
875 | operator()(const Symbol_table_key&) const; | |
876 | }; | |
877 | ||
878 | struct Symbol_table_eq | |
879 | { | |
880 | bool | |
881 | operator()(const Symbol_table_key&, const Symbol_table_key&) const; | |
882 | }; | |
883 | ||
884 | typedef Unordered_map<Symbol_table_key, Symbol*, Symbol_table_hash, | |
885 | Symbol_table_eq> Symbol_table_type; | |
886 | ||
ead1e424 ILT |
887 | // The type of the list of common symbols. |
888 | ||
889 | typedef std::vector<Symbol*> Commons_type; | |
890 | ||
14bfc3f5 ILT |
891 | // The size of the symbols in the symbol table (32 or 64). |
892 | int size_; | |
893 | ||
ead1e424 ILT |
894 | // We increment this every time we see a new undefined symbol, for |
895 | // use in archive groups. | |
896 | int saw_undefined_; | |
897 | ||
c06b7b0b ILT |
898 | // The index of the first global symbol in the output file. |
899 | unsigned int first_global_index_; | |
900 | ||
75f65a3e ILT |
901 | // The file offset within the output symtab section where we should |
902 | // write the table. | |
903 | off_t offset_; | |
904 | ||
61ba1cf9 ILT |
905 | // The number of global symbols we want to write out. |
906 | size_t output_count_; | |
907 | ||
54dc6425 | 908 | // The symbol hash table. |
14bfc3f5 ILT |
909 | Symbol_table_type table_; |
910 | ||
54dc6425 ILT |
911 | // A pool of symbol names. This is used for all global symbols. |
912 | // Entries in the hash table point into this pool. | |
14bfc3f5 | 913 | Stringpool namepool_; |
bae7f79e | 914 | |
14bfc3f5 | 915 | // Forwarding symbols. |
c06b7b0b | 916 | Unordered_map<const Symbol*, Symbol*> forwarders_; |
ead1e424 ILT |
917 | |
918 | // We don't expect there to be very many common symbols, so we keep | |
919 | // a list of them. When we find a common symbol we add it to this | |
920 | // list. It is possible that by the time we process the list the | |
921 | // symbol is no longer a common symbol. It may also have become a | |
922 | // forwarder. | |
923 | Commons_type commons_; | |
f6ce93d6 ILT |
924 | |
925 | // Manage symbol warnings. | |
926 | Warnings warnings_; | |
bae7f79e ILT |
927 | }; |
928 | ||
1564db8d ILT |
929 | // We inline get_sized_symbol for efficiency. |
930 | ||
931 | template<int size> | |
932 | Sized_symbol<size>* | |
5482377d | 933 | Symbol_table::get_sized_symbol(Symbol* sym ACCEPT_SIZE) const |
1564db8d ILT |
934 | { |
935 | assert(size == this->get_size()); | |
936 | return static_cast<Sized_symbol<size>*>(sym); | |
937 | } | |
938 | ||
939 | template<int size> | |
940 | const Sized_symbol<size>* | |
5482377d | 941 | Symbol_table::get_sized_symbol(const Symbol* sym ACCEPT_SIZE) const |
1564db8d ILT |
942 | { |
943 | assert(size == this->get_size()); | |
944 | return static_cast<const Sized_symbol<size>*>(sym); | |
945 | } | |
946 | ||
bae7f79e ILT |
947 | } // End namespace gold. |
948 | ||
949 | #endif // !defined(GOLD_SYMTAB_H) |