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