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