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