Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // symtab.h -- the gold symbol table -*- C++ -*- |
2 | ||
ebdbb458 | 3 | // Copyright 2006, 2007, 2008 Free Software Foundation, Inc. |
6cb15b7f ILT |
4 | // Written by Ian Lance Taylor <iant@google.com>. |
5 | ||
6 | // This file is part of gold. | |
7 | ||
8 | // This program is free software; you can redistribute it and/or modify | |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
11 | // (at your option) any later version. | |
12 | ||
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
22 | ||
bae7f79e ILT |
23 | // Symbol_table |
24 | // The symbol table. | |
25 | ||
bae7f79e ILT |
26 | #include <string> |
27 | #include <utility> | |
ead1e424 | 28 | #include <vector> |
bae7f79e ILT |
29 | |
30 | #include "elfcpp.h" | |
7e1edb90 | 31 | #include "parameters.h" |
14bfc3f5 | 32 | #include "stringpool.h" |
f6ce93d6 | 33 | #include "object.h" |
bae7f79e ILT |
34 | |
35 | #ifndef GOLD_SYMTAB_H | |
36 | #define GOLD_SYMTAB_H | |
37 | ||
38 | namespace gold | |
39 | { | |
40 | ||
14bfc3f5 | 41 | class Object; |
f6ce93d6 | 42 | class Relobj; |
dbe717ef ILT |
43 | template<int size, bool big_endian> |
44 | class Sized_relobj; | |
f6ce93d6 | 45 | class Dynobj; |
dbe717ef ILT |
46 | template<int size, bool big_endian> |
47 | class Sized_dynobj; | |
14b31740 | 48 | class Versions; |
09124467 | 49 | class Version_script_info; |
9a2d6984 | 50 | class Input_objects; |
ead1e424 | 51 | class Output_data; |
a3ad94ed | 52 | class Output_section; |
ead1e424 | 53 | class Output_segment; |
61ba1cf9 | 54 | class Output_file; |
d491d34e | 55 | class Output_symtab_xindex; |
14bfc3f5 | 56 | |
14bfc3f5 ILT |
57 | // The base class of an entry in the symbol table. The symbol table |
58 | // can have a lot of entries, so we don't want this class to big. | |
59 | // Size dependent fields can be found in the template class | |
60 | // Sized_symbol. Targets may support their own derived classes. | |
bae7f79e | 61 | |
bae7f79e ILT |
62 | class Symbol |
63 | { | |
64 | public: | |
ead1e424 ILT |
65 | // Because we want the class to be small, we don't use any virtual |
66 | // functions. But because symbols can be defined in different | |
67 | // places, we need to classify them. This enum is the different | |
68 | // sources of symbols we support. | |
69 | enum Source | |
70 | { | |
f6ce93d6 ILT |
71 | // Symbol defined in a relocatable or dynamic input file--this is |
72 | // the most common case. | |
ead1e424 ILT |
73 | FROM_OBJECT, |
74 | // Symbol defined in an Output_data, a special section created by | |
75 | // the target. | |
76 | IN_OUTPUT_DATA, | |
77 | // Symbol defined in an Output_segment, with no associated | |
78 | // section. | |
79 | IN_OUTPUT_SEGMENT, | |
80 | // Symbol value is constant. | |
81 | CONSTANT | |
82 | }; | |
83 | ||
84 | // When the source is IN_OUTPUT_SEGMENT, we need to describe what | |
85 | // the offset means. | |
86 | enum Segment_offset_base | |
87 | { | |
88 | // From the start of the segment. | |
89 | SEGMENT_START, | |
90 | // From the end of the segment. | |
91 | SEGMENT_END, | |
92 | // From the filesz of the segment--i.e., after the loaded bytes | |
93 | // but before the bytes which are allocated but zeroed. | |
94 | SEGMENT_BSS | |
95 | }; | |
96 | ||
14bfc3f5 ILT |
97 | // Return the symbol name. |
98 | const char* | |
99 | name() const | |
100 | { return this->name_; } | |
101 | ||
a2b1aa12 ILT |
102 | // Return the (ANSI) demangled version of the name, if |
103 | // parameters.demangle() is true. Otherwise, return the name. This | |
104 | // is intended to be used only for logging errors, so it's not | |
105 | // super-efficient. | |
106 | std::string | |
107 | demangled_name() const; | |
108 | ||
14bfc3f5 ILT |
109 | // Return the symbol version. This will return NULL for an |
110 | // unversioned symbol. | |
111 | const char* | |
112 | version() const | |
113 | { return this->version_; } | |
114 | ||
09124467 ILT |
115 | // Return whether this version is the default for this symbol name |
116 | // (eg, "foo@@V2" is a default version; "foo@V1" is not). Only | |
117 | // meaningful for versioned symbols. | |
118 | bool | |
119 | is_default() const | |
120 | { | |
121 | gold_assert(this->version_ != NULL); | |
122 | return this->is_def_; | |
123 | } | |
124 | ||
be3e6201 | 125 | // Set that this version is the default for this symbol name. |
09124467 | 126 | void |
be3e6201 ILT |
127 | set_is_default() |
128 | { this->is_def_ = true; } | |
09124467 | 129 | |
ead1e424 ILT |
130 | // Return the symbol source. |
131 | Source | |
132 | source() const | |
133 | { return this->source_; } | |
134 | ||
14bfc3f5 ILT |
135 | // Return the object with which this symbol is associated. |
136 | Object* | |
137 | object() const | |
ead1e424 | 138 | { |
a3ad94ed | 139 | gold_assert(this->source_ == FROM_OBJECT); |
ead1e424 ILT |
140 | return this->u_.from_object.object; |
141 | } | |
142 | ||
f6ce93d6 ILT |
143 | // Return the index of the section in the input relocatable or |
144 | // dynamic object file. | |
ead1e424 | 145 | unsigned int |
d491d34e | 146 | shndx(bool* is_ordinary) const |
ead1e424 | 147 | { |
a3ad94ed | 148 | gold_assert(this->source_ == FROM_OBJECT); |
d491d34e | 149 | *is_ordinary = this->is_ordinary_shndx_; |
16649710 | 150 | return this->u_.from_object.shndx; |
ead1e424 ILT |
151 | } |
152 | ||
153 | // Return the output data section with which this symbol is | |
154 | // associated, if the symbol was specially defined with respect to | |
155 | // an output data section. | |
156 | Output_data* | |
157 | output_data() const | |
158 | { | |
a3ad94ed | 159 | gold_assert(this->source_ == IN_OUTPUT_DATA); |
ead1e424 ILT |
160 | return this->u_.in_output_data.output_data; |
161 | } | |
162 | ||
163 | // If this symbol was defined with respect to an output data | |
164 | // section, return whether the value is an offset from end. | |
165 | bool | |
166 | offset_is_from_end() const | |
167 | { | |
a3ad94ed | 168 | gold_assert(this->source_ == IN_OUTPUT_DATA); |
ead1e424 ILT |
169 | return this->u_.in_output_data.offset_is_from_end; |
170 | } | |
171 | ||
172 | // Return the output segment with which this symbol is associated, | |
173 | // if the symbol was specially defined with respect to an output | |
174 | // segment. | |
175 | Output_segment* | |
176 | output_segment() const | |
177 | { | |
a3ad94ed | 178 | gold_assert(this->source_ == IN_OUTPUT_SEGMENT); |
ead1e424 ILT |
179 | return this->u_.in_output_segment.output_segment; |
180 | } | |
181 | ||
182 | // If this symbol was defined with respect to an output segment, | |
183 | // return the offset base. | |
184 | Segment_offset_base | |
185 | offset_base() const | |
186 | { | |
a3ad94ed | 187 | gold_assert(this->source_ == IN_OUTPUT_SEGMENT); |
ead1e424 ILT |
188 | return this->u_.in_output_segment.offset_base; |
189 | } | |
14bfc3f5 ILT |
190 | |
191 | // Return the symbol binding. | |
192 | elfcpp::STB | |
193 | binding() const | |
194 | { return this->binding_; } | |
195 | ||
1564db8d ILT |
196 | // Return the symbol type. |
197 | elfcpp::STT | |
198 | type() const | |
199 | { return this->type_; } | |
200 | ||
201 | // Return the symbol visibility. | |
202 | elfcpp::STV | |
203 | visibility() const | |
204 | { return this->visibility_; } | |
205 | ||
206 | // Return the non-visibility part of the st_other field. | |
207 | unsigned char | |
ead1e424 ILT |
208 | nonvis() const |
209 | { return this->nonvis_; } | |
14bfc3f5 | 210 | |
1564db8d ILT |
211 | // Return whether this symbol is a forwarder. This will never be |
212 | // true of a symbol found in the hash table, but may be true of | |
213 | // symbol pointers attached to object files. | |
214 | bool | |
215 | is_forwarder() const | |
216 | { return this->is_forwarder_; } | |
217 | ||
218 | // Mark this symbol as a forwarder. | |
219 | void | |
220 | set_forwarder() | |
221 | { this->is_forwarder_ = true; } | |
222 | ||
aeddab66 ILT |
223 | // Return whether this symbol has an alias in the weak aliases table |
224 | // in Symbol_table. | |
225 | bool | |
226 | has_alias() const | |
227 | { return this->has_alias_; } | |
228 | ||
229 | // Mark this symbol as having an alias. | |
230 | void | |
231 | set_has_alias() | |
232 | { this->has_alias_ = true; } | |
233 | ||
c06b7b0b ILT |
234 | // Return whether this symbol needs an entry in the dynamic symbol |
235 | // table. | |
236 | bool | |
237 | needs_dynsym_entry() const | |
429c1569 ILT |
238 | { |
239 | return (this->needs_dynsym_entry_ | |
240 | || (this->in_reg() && this->in_dyn())); | |
241 | } | |
c06b7b0b ILT |
242 | |
243 | // Mark this symbol as needing an entry in the dynamic symbol table. | |
244 | void | |
245 | set_needs_dynsym_entry() | |
246 | { this->needs_dynsym_entry_ = true; } | |
247 | ||
436ca963 ILT |
248 | // Return whether this symbol should be added to the dynamic symbol |
249 | // table. | |
250 | bool | |
251 | should_add_dynsym_entry() const; | |
252 | ||
008db82e ILT |
253 | // Return whether this symbol has been seen in a regular object. |
254 | bool | |
255 | in_reg() const | |
256 | { return this->in_reg_; } | |
257 | ||
258 | // Mark this symbol as having been seen in a regular object. | |
259 | void | |
260 | set_in_reg() | |
261 | { this->in_reg_ = true; } | |
262 | ||
1ebd95fd ILT |
263 | // Return whether this symbol has been seen in a dynamic object. |
264 | bool | |
265 | in_dyn() const | |
266 | { return this->in_dyn_; } | |
267 | ||
f6ce93d6 | 268 | // Mark this symbol as having been seen in a dynamic object. |
1564db8d ILT |
269 | void |
270 | set_in_dyn() | |
271 | { this->in_dyn_ = true; } | |
272 | ||
c06b7b0b ILT |
273 | // Return the index of this symbol in the output file symbol table. |
274 | // A value of -1U means that this symbol is not going into the | |
275 | // output file. This starts out as zero, and is set to a non-zero | |
276 | // value by Symbol_table::finalize. It is an error to ask for the | |
277 | // symbol table index before it has been set. | |
278 | unsigned int | |
279 | symtab_index() const | |
280 | { | |
a3ad94ed | 281 | gold_assert(this->symtab_index_ != 0); |
c06b7b0b ILT |
282 | return this->symtab_index_; |
283 | } | |
284 | ||
285 | // Set the index of the symbol in the output file symbol table. | |
286 | void | |
287 | set_symtab_index(unsigned int index) | |
288 | { | |
a3ad94ed | 289 | gold_assert(index != 0); |
c06b7b0b ILT |
290 | this->symtab_index_ = index; |
291 | } | |
292 | ||
a3ad94ed ILT |
293 | // Return whether this symbol already has an index in the output |
294 | // file symbol table. | |
295 | bool | |
296 | has_symtab_index() const | |
297 | { return this->symtab_index_ != 0; } | |
298 | ||
c06b7b0b ILT |
299 | // Return the index of this symbol in the dynamic symbol table. A |
300 | // value of -1U means that this symbol is not going into the dynamic | |
301 | // symbol table. This starts out as zero, and is set to a non-zero | |
302 | // during Layout::finalize. It is an error to ask for the dynamic | |
303 | // symbol table index before it has been set. | |
304 | unsigned int | |
305 | dynsym_index() const | |
306 | { | |
a3ad94ed | 307 | gold_assert(this->dynsym_index_ != 0); |
c06b7b0b ILT |
308 | return this->dynsym_index_; |
309 | } | |
310 | ||
311 | // Set the index of the symbol in the dynamic symbol table. | |
312 | void | |
313 | set_dynsym_index(unsigned int index) | |
314 | { | |
a3ad94ed | 315 | gold_assert(index != 0); |
c06b7b0b ILT |
316 | this->dynsym_index_ = index; |
317 | } | |
318 | ||
16649710 ILT |
319 | // Return whether this symbol already has an index in the dynamic |
320 | // symbol table. | |
321 | bool | |
322 | has_dynsym_index() const | |
323 | { return this->dynsym_index_ != 0; } | |
324 | ||
ead1e424 | 325 | // Return whether this symbol has an entry in the GOT section. |
07f397ab | 326 | // For a TLS symbol, this GOT entry will hold its tp-relative offset. |
92e059d8 | 327 | bool |
0a65a3a7 CC |
328 | has_got_offset(unsigned int got_type) const |
329 | { return this->got_offsets_.get_offset(got_type) != -1U; } | |
ead1e424 ILT |
330 | |
331 | // Return the offset into the GOT section of this symbol. | |
332 | unsigned int | |
0a65a3a7 | 333 | got_offset(unsigned int got_type) const |
ead1e424 | 334 | { |
0a65a3a7 CC |
335 | unsigned int got_offset = this->got_offsets_.get_offset(got_type); |
336 | gold_assert(got_offset != -1U); | |
337 | return got_offset; | |
ead1e424 ILT |
338 | } |
339 | ||
340 | // Set the GOT offset of this symbol. | |
341 | void | |
0a65a3a7 CC |
342 | set_got_offset(unsigned int got_type, unsigned int got_offset) |
343 | { this->got_offsets_.set_offset(got_type, got_offset); } | |
07f397ab | 344 | |
a3ad94ed | 345 | // Return whether this symbol has an entry in the PLT section. |
ead1e424 | 346 | bool |
a3ad94ed ILT |
347 | has_plt_offset() const |
348 | { return this->has_plt_offset_; } | |
349 | ||
350 | // Return the offset into the PLT section of this symbol. | |
351 | unsigned int | |
352 | plt_offset() const | |
353 | { | |
354 | gold_assert(this->has_plt_offset()); | |
355 | return this->plt_offset_; | |
356 | } | |
357 | ||
358 | // Set the PLT offset of this symbol. | |
359 | void | |
360 | set_plt_offset(unsigned int plt_offset) | |
361 | { | |
362 | this->has_plt_offset_ = true; | |
363 | this->plt_offset_ = plt_offset; | |
364 | } | |
365 | ||
ab5c9e90 ILT |
366 | // Return whether this dynamic symbol needs a special value in the |
367 | // dynamic symbol table. | |
368 | bool | |
369 | needs_dynsym_value() const | |
370 | { return this->needs_dynsym_value_; } | |
371 | ||
372 | // Set that this dynamic symbol needs a special value in the dynamic | |
373 | // symbol table. | |
374 | void | |
375 | set_needs_dynsym_value() | |
376 | { | |
377 | gold_assert(this->object()->is_dynamic()); | |
378 | this->needs_dynsym_value_ = true; | |
379 | } | |
380 | ||
a3ad94ed ILT |
381 | // Return true if the final value of this symbol is known at link |
382 | // time. | |
383 | bool | |
b3b74ddc | 384 | final_value_is_known() const; |
ead1e424 | 385 | |
f6ce93d6 ILT |
386 | // Return whether this is a defined symbol (not undefined or |
387 | // common). | |
388 | bool | |
389 | is_defined() const | |
390 | { | |
d491d34e ILT |
391 | bool is_ordinary; |
392 | if (this->source_ != FROM_OBJECT) | |
393 | return true; | |
394 | unsigned int shndx = this->shndx(&is_ordinary); | |
395 | return (is_ordinary | |
396 | ? shndx != elfcpp::SHN_UNDEF | |
397 | : shndx != elfcpp::SHN_COMMON); | |
a3ad94ed ILT |
398 | } |
399 | ||
14b31740 | 400 | // Return true if this symbol is from a dynamic object. |
a3ad94ed | 401 | bool |
14b31740 | 402 | is_from_dynobj() const |
a3ad94ed | 403 | { |
14b31740 | 404 | return this->source_ == FROM_OBJECT && this->object()->is_dynamic(); |
f6ce93d6 ILT |
405 | } |
406 | ||
ead1e424 ILT |
407 | // Return whether this is an undefined symbol. |
408 | bool | |
409 | is_undefined() const | |
410 | { | |
d491d34e ILT |
411 | bool is_ordinary; |
412 | return (this->source_ == FROM_OBJECT | |
413 | && this->shndx(&is_ordinary) == elfcpp::SHN_UNDEF | |
414 | && is_ordinary); | |
ead1e424 ILT |
415 | } |
416 | ||
86925eef CC |
417 | // Return whether this is a weak undefined symbol. |
418 | bool | |
419 | is_weak_undefined() const | |
420 | { | |
d491d34e | 421 | bool is_ordinary; |
86925eef CC |
422 | return (this->source_ == FROM_OBJECT |
423 | && this->binding() == elfcpp::STB_WEAK | |
d491d34e ILT |
424 | && this->shndx(&is_ordinary) == elfcpp::SHN_UNDEF |
425 | && is_ordinary); | |
86925eef CC |
426 | } |
427 | ||
86925eef CC |
428 | // Return whether this is an absolute symbol. |
429 | bool | |
430 | is_absolute() const | |
431 | { | |
d491d34e ILT |
432 | bool is_ordinary; |
433 | return (this->source_ == FROM_OBJECT | |
434 | && this->shndx(&is_ordinary) == elfcpp::SHN_ABS | |
435 | && !is_ordinary); | |
86925eef CC |
436 | } |
437 | ||
ead1e424 ILT |
438 | // Return whether this is a common symbol. |
439 | bool | |
440 | is_common() const | |
441 | { | |
d491d34e | 442 | bool is_ordinary; |
f6ce93d6 | 443 | return (this->source_ == FROM_OBJECT |
d491d34e ILT |
444 | && ((this->shndx(&is_ordinary) == elfcpp::SHN_COMMON |
445 | && !is_ordinary) | |
f6ce93d6 | 446 | || this->type_ == elfcpp::STT_COMMON)); |
ead1e424 | 447 | } |
92e059d8 | 448 | |
a6badf5a ILT |
449 | // Return whether this symbol can be seen outside this object. |
450 | bool | |
451 | is_externally_visible() const | |
452 | { | |
453 | return (this->visibility_ == elfcpp::STV_DEFAULT | |
454 | || this->visibility_ == elfcpp::STV_PROTECTED); | |
455 | } | |
456 | ||
436ca963 ILT |
457 | // Return true if this symbol can be preempted by a definition in |
458 | // another link unit. | |
459 | bool | |
460 | is_preemptible() const | |
461 | { | |
386c048c ILT |
462 | // It doesn't make sense to ask whether a symbol defined in |
463 | // another object is preemptible. | |
464 | gold_assert(!this->is_from_dynobj()); | |
465 | ||
8fc19601 ILT |
466 | // It doesn't make sense to ask whether an undefined symbol |
467 | // is preemptible. | |
468 | gold_assert(!this->is_undefined()); | |
469 | ||
436ca963 ILT |
470 | return (this->visibility_ != elfcpp::STV_INTERNAL |
471 | && this->visibility_ != elfcpp::STV_HIDDEN | |
51b08ebe | 472 | && this->visibility_ != elfcpp::STV_PROTECTED |
55a93433 | 473 | && !this->is_forced_local_ |
8851ecca ILT |
474 | && parameters->options().shared() |
475 | && !parameters->options().Bsymbolic()); | |
436ca963 ILT |
476 | } |
477 | ||
d61c6bd4 ILT |
478 | // Return true if this symbol is a function that needs a PLT entry. |
479 | // If the symbol is defined in a dynamic object or if it is subject | |
8fc19601 ILT |
480 | // to pre-emption, we need to make a PLT entry. If we're doing a |
481 | // static link, we don't create PLT entries. | |
d61c6bd4 ILT |
482 | bool |
483 | needs_plt_entry() const | |
484 | { | |
8fc19601 ILT |
485 | return (!parameters->doing_static_link() |
486 | && this->type() == elfcpp::STT_FUNC | |
487 | && (this->is_from_dynobj() | |
f3c69fca | 488 | || this->is_undefined() |
8fc19601 | 489 | || this->is_preemptible())); |
d61c6bd4 ILT |
490 | } |
491 | ||
0700cf32 ILT |
492 | // When determining whether a reference to a symbol needs a dynamic |
493 | // relocation, we need to know several things about the reference. | |
494 | // These flags may be or'ed together. | |
495 | enum Reference_flags | |
496 | { | |
497 | // Reference to the symbol's absolute address. | |
498 | ABSOLUTE_REF = 1, | |
499 | // A non-PIC reference. | |
500 | NON_PIC_REF = 2, | |
501 | // A function call. | |
502 | FUNCTION_CALL = 4 | |
503 | }; | |
504 | ||
d61c6bd4 ILT |
505 | // Given a direct absolute or pc-relative static relocation against |
506 | // the global symbol, this function returns whether a dynamic relocation | |
507 | // is needed. | |
508 | ||
509 | bool | |
0700cf32 | 510 | needs_dynamic_reloc(int flags) const |
d61c6bd4 | 511 | { |
8fc19601 ILT |
512 | // No dynamic relocations in a static link! |
513 | if (parameters->doing_static_link()) | |
514 | return false; | |
515 | ||
f3c69fca CC |
516 | // A reference to a weak undefined symbol from an executable should be |
517 | // statically resolved to 0, and does not need a dynamic relocation. | |
518 | // This matches gnu ld behavior. | |
519 | if (this->is_weak_undefined() && !parameters->options().shared()) | |
520 | return false; | |
521 | ||
522 | // A reference to an absolute symbol does not need a dynamic relocation. | |
523 | if (this->is_absolute()) | |
86925eef CC |
524 | return false; |
525 | ||
d61c6bd4 | 526 | // An absolute reference within a position-independent output file |
0700cf32 ILT |
527 | // will need a dynamic relocation. |
528 | if ((flags & ABSOLUTE_REF) | |
8851ecca | 529 | && parameters->options().output_is_position_independent()) |
d61c6bd4 ILT |
530 | return true; |
531 | ||
532 | // A function call that can branch to a local PLT entry does not need | |
5240d12a ILT |
533 | // a dynamic relocation. A non-pic pc-relative function call in a |
534 | // shared library cannot use a PLT entry. | |
0700cf32 | 535 | if ((flags & FUNCTION_CALL) |
5240d12a | 536 | && this->has_plt_offset() |
8851ecca | 537 | && !((flags & NON_PIC_REF) && parameters->options().shared())) |
d61c6bd4 ILT |
538 | return false; |
539 | ||
540 | // A reference to any PLT entry in a non-position-independent executable | |
541 | // does not need a dynamic relocation. | |
8851ecca | 542 | if (!parameters->options().output_is_position_independent() |
d61c6bd4 ILT |
543 | && this->has_plt_offset()) |
544 | return false; | |
545 | ||
546 | // A reference to a symbol defined in a dynamic object or to a | |
547 | // symbol that is preemptible will need a dynamic relocation. | |
8fc19601 ILT |
548 | if (this->is_from_dynobj() |
549 | || this->is_undefined() | |
550 | || this->is_preemptible()) | |
d61c6bd4 ILT |
551 | return true; |
552 | ||
553 | // For all other cases, return FALSE. | |
554 | return false; | |
555 | } | |
556 | ||
557 | // Given a direct absolute static relocation against | |
558 | // the global symbol, where a dynamic relocation is needed, this | |
559 | // function returns whether a relative dynamic relocation can be used. | |
560 | // The caller must determine separately whether the static relocation | |
561 | // is compatible with a relative relocation. | |
562 | ||
563 | bool | |
564 | can_use_relative_reloc(bool is_function_call) const | |
565 | { | |
566 | // A function call that can branch to a local PLT entry can | |
567 | // use a RELATIVE relocation. | |
568 | if (is_function_call && this->has_plt_offset()) | |
569 | return true; | |
570 | ||
571 | // A reference to a symbol defined in a dynamic object or to a | |
572 | // symbol that is preemptible can not use a RELATIVE relocaiton. | |
8fc19601 ILT |
573 | if (this->is_from_dynobj() |
574 | || this->is_undefined() | |
575 | || this->is_preemptible()) | |
d61c6bd4 ILT |
576 | return false; |
577 | ||
578 | // For all other cases, return TRUE. | |
579 | return true; | |
580 | } | |
581 | ||
77e65537 ILT |
582 | // Return the output section where this symbol is defined. Return |
583 | // NULL if the symbol has an absolute value. | |
584 | Output_section* | |
585 | output_section() const; | |
586 | ||
587 | // Set the symbol's output section. This is used for symbols | |
588 | // defined in scripts. This should only be called after the symbol | |
589 | // table has been finalized. | |
590 | void | |
591 | set_output_section(Output_section*); | |
a445fddf | 592 | |
f6ce93d6 ILT |
593 | // Return whether there should be a warning for references to this |
594 | // symbol. | |
595 | bool | |
596 | has_warning() const | |
597 | { return this->has_warning_; } | |
598 | ||
599 | // Mark this symbol as having a warning. | |
600 | void | |
601 | set_has_warning() | |
602 | { this->has_warning_ = true; } | |
603 | ||
46fe1623 ILT |
604 | // Return whether this symbol is defined by a COPY reloc from a |
605 | // dynamic object. | |
606 | bool | |
607 | is_copied_from_dynobj() const | |
608 | { return this->is_copied_from_dynobj_; } | |
609 | ||
610 | // Mark this symbol as defined by a COPY reloc. | |
611 | void | |
612 | set_is_copied_from_dynobj() | |
613 | { this->is_copied_from_dynobj_ = true; } | |
614 | ||
55a93433 ILT |
615 | // Return whether this symbol is forced to visibility STB_LOCAL |
616 | // by a "local:" entry in a version script. | |
617 | bool | |
618 | is_forced_local() const | |
619 | { return this->is_forced_local_; } | |
620 | ||
621 | // Mark this symbol as forced to STB_LOCAL visibility. | |
622 | void | |
623 | set_is_forced_local() | |
624 | { this->is_forced_local_ = true; } | |
625 | ||
14bfc3f5 ILT |
626 | protected: |
627 | // Instances of this class should always be created at a specific | |
628 | // size. | |
629 | Symbol() | |
f6ce93d6 | 630 | { memset(this, 0, sizeof *this); } |
14bfc3f5 | 631 | |
ead1e424 ILT |
632 | // Initialize the general fields. |
633 | void | |
634 | init_fields(const char* name, const char* version, | |
635 | elfcpp::STT type, elfcpp::STB binding, | |
636 | elfcpp::STV visibility, unsigned char nonvis); | |
637 | ||
d491d34e ILT |
638 | // Initialize fields from an ELF symbol in OBJECT. ST_SHNDX is the |
639 | // section index, IS_ORDINARY is whether it is a normal section | |
640 | // index rather than a special code. | |
14bfc3f5 ILT |
641 | template<int size, bool big_endian> |
642 | void | |
643 | init_base(const char *name, const char* version, Object* object, | |
d491d34e ILT |
644 | const elfcpp::Sym<size, big_endian>&, unsigned int st_shndx, |
645 | bool is_ordinary); | |
bae7f79e | 646 | |
ead1e424 ILT |
647 | // Initialize fields for an Output_data. |
648 | void | |
649 | init_base(const char* name, Output_data*, elfcpp::STT, elfcpp::STB, | |
650 | elfcpp::STV, unsigned char nonvis, bool offset_is_from_end); | |
651 | ||
652 | // Initialize fields for an Output_segment. | |
653 | void | |
654 | init_base(const char* name, Output_segment* os, elfcpp::STT type, | |
655 | elfcpp::STB binding, elfcpp::STV visibility, | |
656 | unsigned char nonvis, Segment_offset_base offset_base); | |
657 | ||
658 | // Initialize fields for a constant. | |
659 | void | |
660 | init_base(const char* name, elfcpp::STT type, elfcpp::STB binding, | |
661 | elfcpp::STV visibility, unsigned char nonvis); | |
662 | ||
1564db8d ILT |
663 | // Override existing symbol. |
664 | template<int size, bool big_endian> | |
665 | void | |
d491d34e ILT |
666 | override_base(const elfcpp::Sym<size, big_endian>&, unsigned int st_shndx, |
667 | bool is_ordinary, Object* object, const char* version); | |
1564db8d | 668 | |
86f2e683 ILT |
669 | // Override existing symbol with a special symbol. |
670 | void | |
671 | override_base_with_special(const Symbol* from); | |
672 | ||
c7912668 ILT |
673 | // Allocate a common symbol by giving it a location in the output |
674 | // file. | |
675 | void | |
676 | allocate_base_common(Output_data*); | |
677 | ||
bae7f79e | 678 | private: |
14bfc3f5 ILT |
679 | Symbol(const Symbol&); |
680 | Symbol& operator=(const Symbol&); | |
681 | ||
682 | // Symbol name (expected to point into a Stringpool). | |
683 | const char* name_; | |
684 | // Symbol version (expected to point into a Stringpool). This may | |
685 | // be NULL. | |
bae7f79e | 686 | const char* version_; |
ead1e424 ILT |
687 | |
688 | union | |
689 | { | |
690 | // This struct is used if SOURCE_ == FROM_OBJECT. | |
691 | struct | |
692 | { | |
693 | // Object in which symbol is defined, or in which it was first | |
694 | // seen. | |
695 | Object* object; | |
696 | // Section number in object_ in which symbol is defined. | |
16649710 | 697 | unsigned int shndx; |
ead1e424 ILT |
698 | } from_object; |
699 | ||
700 | // This struct is used if SOURCE_ == IN_OUTPUT_DATA. | |
701 | struct | |
702 | { | |
703 | // Output_data in which symbol is defined. Before | |
704 | // Layout::finalize the symbol's value is an offset within the | |
705 | // Output_data. | |
706 | Output_data* output_data; | |
707 | // True if the offset is from the end, false if the offset is | |
708 | // from the beginning. | |
709 | bool offset_is_from_end; | |
710 | } in_output_data; | |
711 | ||
712 | // This struct is used if SOURCE_ == IN_OUTPUT_SEGMENT. | |
713 | struct | |
714 | { | |
715 | // Output_segment in which the symbol is defined. Before | |
716 | // Layout::finalize the symbol's value is an offset. | |
717 | Output_segment* output_segment; | |
718 | // The base to use for the offset before Layout::finalize. | |
719 | Segment_offset_base offset_base; | |
720 | } in_output_segment; | |
721 | } u_; | |
722 | ||
c06b7b0b ILT |
723 | // The index of this symbol in the output file. If the symbol is |
724 | // not going into the output file, this value is -1U. This field | |
725 | // starts as always holding zero. It is set to a non-zero value by | |
726 | // Symbol_table::finalize. | |
727 | unsigned int symtab_index_; | |
728 | ||
729 | // The index of this symbol in the dynamic symbol table. If the | |
730 | // symbol is not going into the dynamic symbol table, this value is | |
731 | // -1U. This field starts as always holding zero. It is set to a | |
732 | // non-zero value during Layout::finalize. | |
733 | unsigned int dynsym_index_; | |
734 | ||
ead1e424 | 735 | // If this symbol has an entry in the GOT section (has_got_offset_ |
0a65a3a7 CC |
736 | // is true), this holds the offset from the start of the GOT section. |
737 | // A symbol may have more than one GOT offset (e.g., when mixing | |
738 | // modules compiled with two different TLS models), but will usually | |
739 | // have at most one. | |
740 | Got_offset_list got_offsets_; | |
07f397ab | 741 | |
a3ad94ed ILT |
742 | // If this symbol has an entry in the PLT section (has_plt_offset_ |
743 | // is true), then this is the offset from the start of the PLT | |
744 | // section. | |
745 | unsigned int plt_offset_; | |
746 | ||
d491d34e | 747 | // Symbol type (bits 0 to 3). |
bae7f79e | 748 | elfcpp::STT type_ : 4; |
d491d34e | 749 | // Symbol binding (bits 4 to 7). |
bae7f79e | 750 | elfcpp::STB binding_ : 4; |
d491d34e | 751 | // Symbol visibility (bits 8 to 9). |
14bfc3f5 | 752 | elfcpp::STV visibility_ : 2; |
d491d34e | 753 | // Rest of symbol st_other field (bits 10 to 15). |
ead1e424 | 754 | unsigned int nonvis_ : 6; |
d491d34e | 755 | // The type of symbol (bits 16 to 18). |
f6ce93d6 | 756 | Source source_ : 3; |
14bfc3f5 | 757 | // True if this symbol always requires special target-specific |
d491d34e | 758 | // handling (bit 19). |
ead1e424 | 759 | bool is_target_special_ : 1; |
d491d34e | 760 | // True if this is the default version of the symbol (bit 20). |
1564db8d | 761 | bool is_def_ : 1; |
14bfc3f5 ILT |
762 | // True if this symbol really forwards to another symbol. This is |
763 | // used when we discover after the fact that two different entries | |
764 | // in the hash table really refer to the same symbol. This will | |
765 | // never be set for a symbol found in the hash table, but may be set | |
766 | // for a symbol found in the list of symbols attached to an Object. | |
767 | // It forwards to the symbol found in the forwarders_ map of | |
d491d34e | 768 | // Symbol_table (bit 21). |
1564db8d | 769 | bool is_forwarder_ : 1; |
aeddab66 | 770 | // True if the symbol has an alias in the weak_aliases table in |
d491d34e | 771 | // Symbol_table (bit 22). |
aeddab66 | 772 | bool has_alias_ : 1; |
d491d34e ILT |
773 | // True if this symbol needs to be in the dynamic symbol table (bit |
774 | // 23). | |
c06b7b0b | 775 | bool needs_dynsym_entry_ : 1; |
d491d34e | 776 | // True if we've seen this symbol in a regular object (bit 24). |
008db82e | 777 | bool in_reg_ : 1; |
d491d34e | 778 | // True if we've seen this symbol in a dynamic object (bit 25). |
1564db8d | 779 | bool in_dyn_ : 1; |
d491d34e | 780 | // True if the symbol has an entry in the PLT section (bit 26). |
a3ad94ed | 781 | bool has_plt_offset_ : 1; |
ab5c9e90 | 782 | // True if this is a dynamic symbol which needs a special value in |
d491d34e | 783 | // the dynamic symbol table (bit 27). |
ab5c9e90 | 784 | bool needs_dynsym_value_ : 1; |
d491d34e | 785 | // True if there is a warning for this symbol (bit 28). |
f6ce93d6 | 786 | bool has_warning_ : 1; |
46fe1623 | 787 | // True if we are using a COPY reloc for this symbol, so that the |
d491d34e | 788 | // real definition lives in a dynamic object (bit 29). |
46fe1623 | 789 | bool is_copied_from_dynobj_ : 1; |
55a93433 | 790 | // True if this symbol was forced to local visibility by a version |
d491d34e | 791 | // script (bit 30). |
55a93433 | 792 | bool is_forced_local_ : 1; |
d491d34e ILT |
793 | // True if the field u_.from_object.shndx is an ordinary section |
794 | // index, not one of the special codes from SHN_LORESERVE to | |
795 | // SHN_HIRESERVE. | |
796 | bool is_ordinary_shndx_ : 1; | |
bae7f79e ILT |
797 | }; |
798 | ||
14bfc3f5 ILT |
799 | // The parts of a symbol which are size specific. Using a template |
800 | // derived class like this helps us use less space on a 32-bit system. | |
bae7f79e ILT |
801 | |
802 | template<int size> | |
14bfc3f5 ILT |
803 | class Sized_symbol : public Symbol |
804 | { | |
805 | public: | |
1564db8d ILT |
806 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Value_type; |
807 | typedef typename elfcpp::Elf_types<size>::Elf_WXword Size_type; | |
808 | ||
14bfc3f5 ILT |
809 | Sized_symbol() |
810 | { } | |
811 | ||
d491d34e ILT |
812 | // Initialize fields from an ELF symbol in OBJECT. ST_SHNDX is the |
813 | // section index, IS_ORDINARY is whether it is a normal section | |
814 | // index rather than a special code. | |
14bfc3f5 ILT |
815 | template<bool big_endian> |
816 | void | |
817 | init(const char *name, const char* version, Object* object, | |
d491d34e ILT |
818 | const elfcpp::Sym<size, big_endian>&, unsigned int st_shndx, |
819 | bool is_ordinary); | |
14bfc3f5 | 820 | |
ead1e424 ILT |
821 | // Initialize fields for an Output_data. |
822 | void | |
823 | init(const char* name, Output_data*, Value_type value, Size_type symsize, | |
824 | elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis, | |
825 | bool offset_is_from_end); | |
826 | ||
827 | // Initialize fields for an Output_segment. | |
828 | void | |
829 | init(const char* name, Output_segment*, Value_type value, Size_type symsize, | |
830 | elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis, | |
831 | Segment_offset_base offset_base); | |
832 | ||
833 | // Initialize fields for a constant. | |
834 | void | |
835 | init(const char* name, Value_type value, Size_type symsize, | |
836 | elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis); | |
837 | ||
1564db8d ILT |
838 | // Override existing symbol. |
839 | template<bool big_endian> | |
840 | void | |
d491d34e ILT |
841 | override(const elfcpp::Sym<size, big_endian>&, unsigned int st_shndx, |
842 | bool is_ordinary, Object* object, const char* version); | |
1564db8d | 843 | |
86f2e683 ILT |
844 | // Override existing symbol with a special symbol. |
845 | void | |
846 | override_with_special(const Sized_symbol<size>*); | |
847 | ||
1564db8d ILT |
848 | // Return the symbol's value. |
849 | Value_type | |
850 | value() const | |
851 | { return this->value_; } | |
852 | ||
853 | // Return the symbol's size (we can't call this 'size' because that | |
854 | // is a template parameter). | |
855 | Size_type | |
856 | symsize() const | |
ead1e424 ILT |
857 | { return this->symsize_; } |
858 | ||
859 | // Set the symbol size. This is used when resolving common symbols. | |
860 | void | |
861 | set_symsize(Size_type symsize) | |
862 | { this->symsize_ = symsize; } | |
1564db8d | 863 | |
75f65a3e ILT |
864 | // Set the symbol value. This is called when we store the final |
865 | // values of the symbols into the symbol table. | |
866 | void | |
867 | set_value(Value_type value) | |
868 | { this->value_ = value; } | |
869 | ||
c7912668 ILT |
870 | // Allocate a common symbol by giving it a location in the output |
871 | // file. | |
872 | void | |
873 | allocate_common(Output_data*, Value_type value); | |
874 | ||
14bfc3f5 ILT |
875 | private: |
876 | Sized_symbol(const Sized_symbol&); | |
877 | Sized_symbol& operator=(const Sized_symbol&); | |
878 | ||
ead1e424 ILT |
879 | // Symbol value. Before Layout::finalize this is the offset in the |
880 | // input section. This is set to the final value during | |
881 | // Layout::finalize. | |
1564db8d | 882 | Value_type value_; |
14bfc3f5 | 883 | // Symbol size. |
ead1e424 ILT |
884 | Size_type symsize_; |
885 | }; | |
886 | ||
887 | // A struct describing a symbol defined by the linker, where the value | |
888 | // of the symbol is defined based on an output section. This is used | |
889 | // for symbols defined by the linker, like "_init_array_start". | |
890 | ||
891 | struct Define_symbol_in_section | |
892 | { | |
893 | // The symbol name. | |
894 | const char* name; | |
895 | // The name of the output section with which this symbol should be | |
896 | // associated. If there is no output section with that name, the | |
897 | // symbol will be defined as zero. | |
898 | const char* output_section; | |
899 | // The offset of the symbol within the output section. This is an | |
900 | // offset from the start of the output section, unless start_at_end | |
901 | // is true, in which case this is an offset from the end of the | |
902 | // output section. | |
903 | uint64_t value; | |
904 | // The size of the symbol. | |
905 | uint64_t size; | |
906 | // The symbol type. | |
907 | elfcpp::STT type; | |
908 | // The symbol binding. | |
909 | elfcpp::STB binding; | |
910 | // The symbol visibility. | |
911 | elfcpp::STV visibility; | |
912 | // The rest of the st_other field. | |
913 | unsigned char nonvis; | |
914 | // If true, the value field is an offset from the end of the output | |
915 | // section. | |
916 | bool offset_is_from_end; | |
917 | // If true, this symbol is defined only if we see a reference to it. | |
918 | bool only_if_ref; | |
919 | }; | |
920 | ||
921 | // A struct describing a symbol defined by the linker, where the value | |
922 | // of the symbol is defined based on a segment. This is used for | |
923 | // symbols defined by the linker, like "_end". We describe the | |
924 | // segment with which the symbol should be associated by its | |
925 | // characteristics. If no segment meets these characteristics, the | |
926 | // symbol will be defined as zero. If there is more than one segment | |
927 | // which meets these characteristics, we will use the first one. | |
928 | ||
929 | struct Define_symbol_in_segment | |
930 | { | |
931 | // The symbol name. | |
932 | const char* name; | |
933 | // The segment type where the symbol should be defined, typically | |
934 | // PT_LOAD. | |
935 | elfcpp::PT segment_type; | |
936 | // Bitmask of segment flags which must be set. | |
937 | elfcpp::PF segment_flags_set; | |
938 | // Bitmask of segment flags which must be clear. | |
939 | elfcpp::PF segment_flags_clear; | |
940 | // The offset of the symbol within the segment. The offset is | |
941 | // calculated from the position set by offset_base. | |
942 | uint64_t value; | |
943 | // The size of the symbol. | |
944 | uint64_t size; | |
945 | // The symbol type. | |
946 | elfcpp::STT type; | |
947 | // The symbol binding. | |
948 | elfcpp::STB binding; | |
949 | // The symbol visibility. | |
950 | elfcpp::STV visibility; | |
951 | // The rest of the st_other field. | |
952 | unsigned char nonvis; | |
953 | // The base from which we compute the offset. | |
954 | Symbol::Segment_offset_base offset_base; | |
955 | // If true, this symbol is defined only if we see a reference to it. | |
956 | bool only_if_ref; | |
14bfc3f5 ILT |
957 | }; |
958 | ||
f6ce93d6 ILT |
959 | // This class manages warnings. Warnings are a GNU extension. When |
960 | // we see a section named .gnu.warning.SYM in an object file, and if | |
961 | // we wind using the definition of SYM from that object file, then we | |
962 | // will issue a warning for any relocation against SYM from a | |
963 | // different object file. The text of the warning is the contents of | |
964 | // the section. This is not precisely the definition used by the old | |
965 | // GNU linker; the old GNU linker treated an occurrence of | |
966 | // .gnu.warning.SYM as defining a warning symbol. A warning symbol | |
967 | // would trigger a warning on any reference. However, it was | |
968 | // inconsistent in that a warning in a dynamic object only triggered | |
969 | // if there was no definition in a regular object. This linker is | |
970 | // different in that we only issue a warning if we use the symbol | |
971 | // definition from the same object file as the warning section. | |
972 | ||
973 | class Warnings | |
974 | { | |
975 | public: | |
976 | Warnings() | |
977 | : warnings_() | |
978 | { } | |
979 | ||
cb295612 ILT |
980 | // Add a warning for symbol NAME in object OBJ. WARNING is the text |
981 | // of the warning. | |
f6ce93d6 ILT |
982 | void |
983 | add_warning(Symbol_table* symtab, const char* name, Object* obj, | |
cb295612 | 984 | const std::string& warning); |
f6ce93d6 ILT |
985 | |
986 | // For each symbol for which we should give a warning, make a note | |
987 | // on the symbol. | |
988 | void | |
cb295612 | 989 | note_warnings(Symbol_table* symtab); |
f6ce93d6 | 990 | |
75f2446e ILT |
991 | // Issue a warning for a reference to SYM at RELINFO's location. |
992 | template<int size, bool big_endian> | |
f6ce93d6 | 993 | void |
75f2446e ILT |
994 | issue_warning(const Symbol* sym, const Relocate_info<size, big_endian>*, |
995 | size_t relnum, off_t reloffset) const; | |
f6ce93d6 ILT |
996 | |
997 | private: | |
998 | Warnings(const Warnings&); | |
999 | Warnings& operator=(const Warnings&); | |
1000 | ||
1001 | // What we need to know to get the warning text. | |
1002 | struct Warning_location | |
1003 | { | |
1004 | // The object the warning is in. | |
1005 | Object* object; | |
cb295612 | 1006 | // The warning text. |
f6ce93d6 ILT |
1007 | std::string text; |
1008 | ||
1009 | Warning_location() | |
cb295612 | 1010 | : object(NULL), text() |
f6ce93d6 ILT |
1011 | { } |
1012 | ||
1013 | void | |
cb295612 | 1014 | set(Object* o, const std::string& t) |
f6ce93d6 ILT |
1015 | { |
1016 | this->object = o; | |
cb295612 | 1017 | this->text = t; |
f6ce93d6 | 1018 | } |
f6ce93d6 ILT |
1019 | }; |
1020 | ||
1021 | // A mapping from warning symbol names (canonicalized in | |
70e654ba | 1022 | // Symbol_table's namepool_ field) to warning information. |
f6ce93d6 ILT |
1023 | typedef Unordered_map<const char*, Warning_location> Warning_table; |
1024 | ||
1025 | Warning_table warnings_; | |
1026 | }; | |
1027 | ||
14bfc3f5 ILT |
1028 | // The main linker symbol table. |
1029 | ||
bae7f79e ILT |
1030 | class Symbol_table |
1031 | { | |
1032 | public: | |
6d013333 ILT |
1033 | // COUNT is an estimate of how many symbosl will be inserted in the |
1034 | // symbol table. It's ok to put 0 if you don't know; a correct | |
1035 | // guess will just save some CPU by reducing hashtable resizes. | |
09124467 | 1036 | Symbol_table(unsigned int count, const Version_script_info& version_script); |
bae7f79e | 1037 | |
1564db8d | 1038 | ~Symbol_table(); |
bae7f79e | 1039 | |
dbe717ef | 1040 | // Add COUNT external symbols from the relocatable object RELOBJ to |
d491d34e ILT |
1041 | // the symbol table. SYMS is the symbols, SYMNDX_OFFSET is the |
1042 | // offset in the symbol table of the first symbol, SYM_NAMES is | |
1043 | // their names, SYM_NAME_SIZE is the size of SYM_NAMES. This sets | |
1044 | // SYMPOINTERS to point to the symbols in the symbol table. | |
14bfc3f5 ILT |
1045 | template<int size, bool big_endian> |
1046 | void | |
dbe717ef ILT |
1047 | add_from_relobj(Sized_relobj<size, big_endian>* relobj, |
1048 | const unsigned char* syms, size_t count, | |
d491d34e ILT |
1049 | size_t symndx_offset, const char* sym_names, |
1050 | size_t sym_name_size, | |
730cdc88 | 1051 | typename Sized_relobj<size, big_endian>::Symbols*); |
14bfc3f5 | 1052 | |
dbe717ef ILT |
1053 | // Add COUNT dynamic symbols from the dynamic object DYNOBJ to the |
1054 | // symbol table. SYMS is the symbols. SYM_NAMES is their names. | |
1055 | // SYM_NAME_SIZE is the size of SYM_NAMES. The other parameters are | |
1056 | // symbol version data. | |
1057 | template<int size, bool big_endian> | |
1058 | void | |
1059 | add_from_dynobj(Sized_dynobj<size, big_endian>* dynobj, | |
1060 | const unsigned char* syms, size_t count, | |
1061 | const char* sym_names, size_t sym_name_size, | |
1062 | const unsigned char* versym, size_t versym_size, | |
1063 | const std::vector<const char*>*); | |
1064 | ||
ead1e424 ILT |
1065 | // Define a special symbol based on an Output_data. It is a |
1066 | // multiple definition error if this symbol is already defined. | |
14b31740 | 1067 | Symbol* |
9b07f471 | 1068 | define_in_output_data(const char* name, const char* version, |
14b31740 | 1069 | Output_data*, uint64_t value, uint64_t symsize, |
ead1e424 ILT |
1070 | elfcpp::STT type, elfcpp::STB binding, |
1071 | elfcpp::STV visibility, unsigned char nonvis, | |
1072 | bool offset_is_from_end, bool only_if_ref); | |
1073 | ||
1074 | // Define a special symbol based on an Output_segment. It is a | |
1075 | // multiple definition error if this symbol is already defined. | |
14b31740 | 1076 | Symbol* |
9b07f471 ILT |
1077 | define_in_output_segment(const char* name, const char* version, |
1078 | Output_segment*, uint64_t value, uint64_t symsize, | |
ead1e424 ILT |
1079 | elfcpp::STT type, elfcpp::STB binding, |
1080 | elfcpp::STV visibility, unsigned char nonvis, | |
1081 | Symbol::Segment_offset_base, bool only_if_ref); | |
1082 | ||
1083 | // Define a special symbol with a constant value. It is a multiple | |
1084 | // definition error if this symbol is already defined. | |
14b31740 | 1085 | Symbol* |
9b07f471 | 1086 | define_as_constant(const char* name, const char* version, |
14b31740 ILT |
1087 | uint64_t value, uint64_t symsize, elfcpp::STT type, |
1088 | elfcpp::STB binding, elfcpp::STV visibility, | |
caa9d5d9 ILT |
1089 | unsigned char nonvis, bool only_if_ref, |
1090 | bool force_override); | |
ead1e424 | 1091 | |
a445fddf ILT |
1092 | // Define a set of symbols in output sections. If ONLY_IF_REF is |
1093 | // true, only define them if they are referenced. | |
ead1e424 | 1094 | void |
a445fddf ILT |
1095 | define_symbols(const Layout*, int count, const Define_symbol_in_section*, |
1096 | bool only_if_ref); | |
ead1e424 | 1097 | |
a445fddf ILT |
1098 | // Define a set of symbols in output segments. If ONLY_IF_REF is |
1099 | // true, only defined them if they are referenced. | |
ead1e424 | 1100 | void |
a445fddf ILT |
1101 | define_symbols(const Layout*, int count, const Define_symbol_in_segment*, |
1102 | bool only_if_ref); | |
ead1e424 | 1103 | |
46fe1623 ILT |
1104 | // Define SYM using a COPY reloc. POSD is the Output_data where the |
1105 | // symbol should be defined--typically a .dyn.bss section. VALUE is | |
1106 | // the offset within POSD. | |
1107 | template<int size> | |
1108 | void | |
9b07f471 | 1109 | define_with_copy_reloc(Sized_symbol<size>* sym, Output_data* posd, |
fe8718a4 | 1110 | typename elfcpp::Elf_types<size>::Elf_Addr); |
46fe1623 | 1111 | |
61ba1cf9 ILT |
1112 | // Look up a symbol. |
1113 | Symbol* | |
1114 | lookup(const char*, const char* version = NULL) const; | |
1115 | ||
14bfc3f5 | 1116 | // Return the real symbol associated with the forwarder symbol FROM. |
bae7f79e | 1117 | Symbol* |
c06b7b0b | 1118 | resolve_forwards(const Symbol* from) const; |
bae7f79e | 1119 | |
1564db8d ILT |
1120 | // Return the sized version of a symbol in this table. |
1121 | template<int size> | |
1122 | Sized_symbol<size>* | |
7d1a9ebb | 1123 | get_sized_symbol(Symbol*) const; |
1564db8d ILT |
1124 | |
1125 | template<int size> | |
1126 | const Sized_symbol<size>* | |
7d1a9ebb | 1127 | get_sized_symbol(const Symbol*) const; |
54dc6425 | 1128 | |
ead1e424 ILT |
1129 | // Return the count of undefined symbols seen. |
1130 | int | |
1131 | saw_undefined() const | |
1132 | { return this->saw_undefined_; } | |
1133 | ||
1134 | // Allocate the common symbols | |
1135 | void | |
155a0dd7 | 1136 | allocate_commons(Layout*); |
ead1e424 | 1137 | |
cb295612 ILT |
1138 | // Add a warning for symbol NAME in object OBJ. WARNING is the text |
1139 | // of the warning. | |
f6ce93d6 | 1140 | void |
cb295612 ILT |
1141 | add_warning(const char* name, Object* obj, const std::string& warning) |
1142 | { this->warnings_.add_warning(this, name, obj, warning); } | |
f6ce93d6 ILT |
1143 | |
1144 | // Canonicalize a symbol name for use in the hash table. | |
1145 | const char* | |
1146 | canonicalize_name(const char* name) | |
cfd73a4e | 1147 | { return this->namepool_.add(name, true, NULL); } |
f6ce93d6 ILT |
1148 | |
1149 | // Possibly issue a warning for a reference to SYM at LOCATION which | |
1150 | // is in OBJ. | |
75f2446e | 1151 | template<int size, bool big_endian> |
f6ce93d6 | 1152 | void |
75f2446e ILT |
1153 | issue_warning(const Symbol* sym, |
1154 | const Relocate_info<size, big_endian>* relinfo, | |
1155 | size_t relnum, off_t reloffset) const | |
1156 | { this->warnings_.issue_warning(sym, relinfo, relnum, reloffset); } | |
f6ce93d6 | 1157 | |
70e654ba ILT |
1158 | // Check candidate_odr_violations_ to find symbols with the same name |
1159 | // but apparently different definitions (different source-file/line-no). | |
1160 | void | |
17a1d0a9 | 1161 | detect_odr_violations(const Task*, const char* output_file_name) const; |
70e654ba | 1162 | |
46fe1623 ILT |
1163 | // SYM is defined using a COPY reloc. Return the dynamic object |
1164 | // where the original definition was found. | |
1165 | Dynobj* | |
1166 | get_copy_source(const Symbol* sym) const; | |
1167 | ||
a3ad94ed ILT |
1168 | // Set the dynamic symbol indexes. INDEX is the index of the first |
1169 | // global dynamic symbol. Pointers to the symbols are stored into | |
1170 | // the vector. The names are stored into the Stringpool. This | |
1171 | // returns an updated dynamic symbol index. | |
1172 | unsigned int | |
9b07f471 ILT |
1173 | set_dynsym_indexes(unsigned int index, std::vector<Symbol*>*, |
1174 | Stringpool*, Versions*); | |
a3ad94ed | 1175 | |
75f65a3e | 1176 | // Finalize the symbol table after we have set the final addresses |
c06b7b0b | 1177 | // of all the input sections. This sets the final symbol indexes, |
55a93433 ILT |
1178 | // values and adds the names to *POOL. *PLOCAL_SYMCOUNT is the |
1179 | // index of the first global symbol. OFF is the file offset of the | |
1180 | // global symbol table, DYNOFF is the offset of the globals in the | |
1181 | // dynamic symbol table, DYN_GLOBAL_INDEX is the index of the first | |
1182 | // global dynamic symbol, and DYNCOUNT is the number of global | |
1183 | // dynamic symbols. This records the parameters, and returns the | |
1184 | // new file offset. It updates *PLOCAL_SYMCOUNT if it created any | |
1185 | // local symbols. | |
75f65a3e | 1186 | off_t |
55a93433 ILT |
1187 | finalize(off_t off, off_t dynoff, size_t dyn_global_index, size_t dyncount, |
1188 | Stringpool* pool, unsigned int *plocal_symcount); | |
1564db8d | 1189 | |
61ba1cf9 ILT |
1190 | // Write out the global symbols. |
1191 | void | |
9a2d6984 | 1192 | write_globals(const Input_objects*, const Stringpool*, const Stringpool*, |
d491d34e | 1193 | Output_symtab_xindex*, Output_symtab_xindex*, |
16649710 | 1194 | Output_file*) const; |
61ba1cf9 | 1195 | |
a3ad94ed ILT |
1196 | // Write out a section symbol. Return the updated offset. |
1197 | void | |
d491d34e ILT |
1198 | write_section_symbol(const Output_section*, Output_symtab_xindex*, |
1199 | Output_file*, off_t) const; | |
a3ad94ed | 1200 | |
abaa3995 ILT |
1201 | // Dump statistical information to stderr. |
1202 | void | |
1203 | print_stats() const; | |
1204 | ||
09124467 ILT |
1205 | // Return the version script information. |
1206 | const Version_script_info& | |
1207 | version_script() const | |
1208 | { return version_script_; } | |
1209 | ||
bae7f79e ILT |
1210 | private: |
1211 | Symbol_table(const Symbol_table&); | |
1212 | Symbol_table& operator=(const Symbol_table&); | |
1213 | ||
155a0dd7 ILT |
1214 | // The type of the list of common symbols. |
1215 | typedef std::vector<Symbol*> Commons_type; | |
1216 | ||
14bfc3f5 ILT |
1217 | // Make FROM a forwarder symbol to TO. |
1218 | void | |
1219 | make_forwarder(Symbol* from, Symbol* to); | |
1220 | ||
1221 | // Add a symbol. | |
1222 | template<int size, bool big_endian> | |
aeddab66 | 1223 | Sized_symbol<size>* |
f0641a0b ILT |
1224 | add_from_object(Object*, const char *name, Stringpool::Key name_key, |
1225 | const char *version, Stringpool::Key version_key, | |
70e654ba | 1226 | bool def, const elfcpp::Sym<size, big_endian>& sym, |
d491d34e ILT |
1227 | unsigned int st_shndx, bool is_ordinary, |
1228 | unsigned int orig_st_shndx); | |
14bfc3f5 ILT |
1229 | |
1230 | // Resolve symbols. | |
1231 | template<int size, bool big_endian> | |
aeddab66 | 1232 | void |
1564db8d ILT |
1233 | resolve(Sized_symbol<size>* to, |
1234 | const elfcpp::Sym<size, big_endian>& sym, | |
d491d34e ILT |
1235 | unsigned int st_shndx, bool is_ordinary, |
1236 | unsigned int orig_st_shndx, | |
14b31740 | 1237 | Object*, const char* version); |
14bfc3f5 | 1238 | |
1564db8d | 1239 | template<int size, bool big_endian> |
aeddab66 | 1240 | void |
14b31740 | 1241 | resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from, |
7d1a9ebb | 1242 | const char* version); |
14b31740 | 1243 | |
55a93433 ILT |
1244 | // Record that a symbol is forced to be local by a version script. |
1245 | void | |
1246 | force_local(Symbol*); | |
1247 | ||
0864d551 ILT |
1248 | // Adjust NAME and *NAME_KEY for wrapping. |
1249 | const char* | |
1250 | wrap_symbol(Object* object, const char*, Stringpool::Key* name_key); | |
1251 | ||
86f2e683 ILT |
1252 | // Whether we should override a symbol, based on flags in |
1253 | // resolve.cc. | |
1254 | static bool | |
d20222a1 | 1255 | should_override(const Symbol*, unsigned int, Object*, bool*); |
86f2e683 | 1256 | |
aeddab66 ILT |
1257 | // Override a symbol. |
1258 | template<int size, bool big_endian> | |
1259 | void | |
1260 | override(Sized_symbol<size>* tosym, | |
1261 | const elfcpp::Sym<size, big_endian>& fromsym, | |
d491d34e | 1262 | unsigned int st_shndx, bool is_ordinary, |
aeddab66 ILT |
1263 | Object* object, const char* version); |
1264 | ||
86f2e683 ILT |
1265 | // Whether we should override a symbol with a special symbol which |
1266 | // is automatically defined by the linker. | |
1267 | static bool | |
1268 | should_override_with_special(const Symbol*); | |
1269 | ||
aeddab66 ILT |
1270 | // Override a symbol with a special symbol. |
1271 | template<int size> | |
1272 | void | |
1273 | override_with_special(Sized_symbol<size>* tosym, | |
1274 | const Sized_symbol<size>* fromsym); | |
1275 | ||
1276 | // Record all weak alias sets for a dynamic object. | |
1277 | template<int size> | |
1278 | void | |
1279 | record_weak_aliases(std::vector<Sized_symbol<size>*>*); | |
1280 | ||
14b31740 ILT |
1281 | // Define a special symbol. |
1282 | template<int size, bool big_endian> | |
1283 | Sized_symbol<size>* | |
9b07f471 | 1284 | define_special_symbol(const char** pname, const char** pversion, |
7d1a9ebb | 1285 | bool only_if_ref, Sized_symbol<size>** poldsym); |
14bfc3f5 | 1286 | |
ead1e424 ILT |
1287 | // Define a symbol in an Output_data, sized version. |
1288 | template<int size> | |
14b31740 | 1289 | Sized_symbol<size>* |
9b07f471 | 1290 | do_define_in_output_data(const char* name, const char* version, Output_data*, |
ead1e424 ILT |
1291 | typename elfcpp::Elf_types<size>::Elf_Addr value, |
1292 | typename elfcpp::Elf_types<size>::Elf_WXword ssize, | |
1293 | elfcpp::STT type, elfcpp::STB binding, | |
1294 | elfcpp::STV visibility, unsigned char nonvis, | |
1295 | bool offset_is_from_end, bool only_if_ref); | |
1296 | ||
1297 | // Define a symbol in an Output_segment, sized version. | |
1298 | template<int size> | |
14b31740 | 1299 | Sized_symbol<size>* |
ead1e424 | 1300 | do_define_in_output_segment( |
9b07f471 | 1301 | const char* name, const char* version, Output_segment* os, |
ead1e424 ILT |
1302 | typename elfcpp::Elf_types<size>::Elf_Addr value, |
1303 | typename elfcpp::Elf_types<size>::Elf_WXword ssize, | |
1304 | elfcpp::STT type, elfcpp::STB binding, | |
1305 | elfcpp::STV visibility, unsigned char nonvis, | |
1306 | Symbol::Segment_offset_base offset_base, bool only_if_ref); | |
1307 | ||
1308 | // Define a symbol as a constant, sized version. | |
1309 | template<int size> | |
14b31740 | 1310 | Sized_symbol<size>* |
ead1e424 | 1311 | do_define_as_constant( |
9b07f471 | 1312 | const char* name, const char* version, |
ead1e424 ILT |
1313 | typename elfcpp::Elf_types<size>::Elf_Addr value, |
1314 | typename elfcpp::Elf_types<size>::Elf_WXword ssize, | |
1315 | elfcpp::STT type, elfcpp::STB binding, | |
1316 | elfcpp::STV visibility, unsigned char nonvis, | |
caa9d5d9 | 1317 | bool only_if_ref, bool force_override); |
ead1e424 ILT |
1318 | |
1319 | // Allocate the common symbols, sized version. | |
1320 | template<int size> | |
1321 | void | |
155a0dd7 ILT |
1322 | do_allocate_commons(Layout*); |
1323 | ||
1324 | // Allocate the common symbols from one list. | |
1325 | template<int size> | |
1326 | void | |
1327 | do_allocate_commons_list(Layout*, bool is_tls, Commons_type*); | |
ead1e424 | 1328 | |
70e654ba ILT |
1329 | // Implement detect_odr_violations. |
1330 | template<int size, bool big_endian> | |
1331 | void | |
1332 | sized_detect_odr_violations() const; | |
1333 | ||
75f65a3e ILT |
1334 | // Finalize symbols specialized for size. |
1335 | template<int size> | |
1336 | off_t | |
55a93433 ILT |
1337 | sized_finalize(off_t, Stringpool*, unsigned int*); |
1338 | ||
1339 | // Finalize a symbol. Return whether it should be added to the | |
1340 | // symbol table. | |
1341 | template<int size> | |
1342 | bool | |
1343 | sized_finalize_symbol(Symbol*); | |
1344 | ||
1345 | // Add a symbol the final symtab by setting its index. | |
1346 | template<int size> | |
1347 | void | |
1348 | add_to_final_symtab(Symbol*, Stringpool*, unsigned int* pindex, off_t* poff); | |
75f65a3e | 1349 | |
61ba1cf9 ILT |
1350 | // Write globals specialized for size and endianness. |
1351 | template<int size, bool big_endian> | |
1352 | void | |
9a2d6984 | 1353 | sized_write_globals(const Input_objects*, const Stringpool*, |
d491d34e ILT |
1354 | const Stringpool*, Output_symtab_xindex*, |
1355 | Output_symtab_xindex*, Output_file*) const; | |
16649710 ILT |
1356 | |
1357 | // Write out a symbol to P. | |
1358 | template<int size, bool big_endian> | |
1359 | void | |
ab5c9e90 ILT |
1360 | sized_write_symbol(Sized_symbol<size>*, |
1361 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
1362 | unsigned int shndx, | |
7d1a9ebb | 1363 | const Stringpool*, unsigned char* p) const; |
61ba1cf9 | 1364 | |
9a2d6984 ILT |
1365 | // Possibly warn about an undefined symbol from a dynamic object. |
1366 | void | |
1367 | warn_about_undefined_dynobj_symbol(const Input_objects*, Symbol*) const; | |
1368 | ||
a3ad94ed ILT |
1369 | // Write out a section symbol, specialized for size and endianness. |
1370 | template<int size, bool big_endian> | |
1371 | void | |
d491d34e ILT |
1372 | sized_write_section_symbol(const Output_section*, Output_symtab_xindex*, |
1373 | Output_file*, off_t) const; | |
a3ad94ed | 1374 | |
54dc6425 ILT |
1375 | // The type of the symbol hash table. |
1376 | ||
f0641a0b | 1377 | typedef std::pair<Stringpool::Key, Stringpool::Key> Symbol_table_key; |
14bfc3f5 ILT |
1378 | |
1379 | struct Symbol_table_hash | |
1380 | { | |
1381 | size_t | |
1382 | operator()(const Symbol_table_key&) const; | |
1383 | }; | |
1384 | ||
1385 | struct Symbol_table_eq | |
1386 | { | |
1387 | bool | |
1388 | operator()(const Symbol_table_key&, const Symbol_table_key&) const; | |
1389 | }; | |
1390 | ||
1391 | typedef Unordered_map<Symbol_table_key, Symbol*, Symbol_table_hash, | |
1392 | Symbol_table_eq> Symbol_table_type; | |
1393 | ||
55a93433 ILT |
1394 | // The type of the list of symbols which have been forced local. |
1395 | typedef std::vector<Symbol*> Forced_locals; | |
1396 | ||
46fe1623 ILT |
1397 | // A map from symbols with COPY relocs to the dynamic objects where |
1398 | // they are defined. | |
1399 | typedef Unordered_map<const Symbol*, Dynobj*> Copied_symbol_dynobjs; | |
1400 | ||
70e654ba ILT |
1401 | // A map from symbol name (as a pointer into the namepool) to all |
1402 | // the locations the symbols is (weakly) defined (and certain other | |
1403 | // conditions are met). This map will be used later to detect | |
1404 | // possible One Definition Rule (ODR) violations. | |
1405 | struct Symbol_location | |
1406 | { | |
1407 | Object* object; // Object where the symbol is defined. | |
1408 | unsigned int shndx; // Section-in-object where the symbol is defined. | |
1409 | off_t offset; // Offset-in-section where the symbol is defined. | |
1410 | bool operator==(const Symbol_location& that) const | |
1411 | { | |
1412 | return (this->object == that.object | |
1413 | && this->shndx == that.shndx | |
1414 | && this->offset == that.offset); | |
1415 | } | |
1416 | }; | |
1417 | ||
1418 | struct Symbol_location_hash | |
1419 | { | |
1420 | size_t operator()(const Symbol_location& loc) const | |
1421 | { return reinterpret_cast<uintptr_t>(loc.object) ^ loc.offset ^ loc.shndx; } | |
1422 | }; | |
1423 | ||
1424 | typedef Unordered_map<const char*, | |
1425 | Unordered_set<Symbol_location, Symbol_location_hash> > | |
1426 | Odr_map; | |
1427 | ||
ead1e424 ILT |
1428 | // We increment this every time we see a new undefined symbol, for |
1429 | // use in archive groups. | |
1430 | int saw_undefined_; | |
c06b7b0b ILT |
1431 | // The index of the first global symbol in the output file. |
1432 | unsigned int first_global_index_; | |
75f65a3e ILT |
1433 | // The file offset within the output symtab section where we should |
1434 | // write the table. | |
1435 | off_t offset_; | |
61ba1cf9 | 1436 | // The number of global symbols we want to write out. |
55a93433 | 1437 | unsigned int output_count_; |
16649710 ILT |
1438 | // The file offset of the global dynamic symbols, or 0 if none. |
1439 | off_t dynamic_offset_; | |
16649710 ILT |
1440 | // The index of the first global dynamic symbol. |
1441 | unsigned int first_dynamic_global_index_; | |
16649710 | 1442 | // The number of global dynamic symbols, or 0 if none. |
55a93433 | 1443 | unsigned int dynamic_count_; |
54dc6425 | 1444 | // The symbol hash table. |
14bfc3f5 | 1445 | Symbol_table_type table_; |
54dc6425 ILT |
1446 | // A pool of symbol names. This is used for all global symbols. |
1447 | // Entries in the hash table point into this pool. | |
14bfc3f5 | 1448 | Stringpool namepool_; |
14bfc3f5 | 1449 | // Forwarding symbols. |
c06b7b0b | 1450 | Unordered_map<const Symbol*, Symbol*> forwarders_; |
aeddab66 ILT |
1451 | // Weak aliases. A symbol in this list points to the next alias. |
1452 | // The aliases point to each other in a circular list. | |
1453 | Unordered_map<Symbol*, Symbol*> weak_aliases_; | |
ead1e424 ILT |
1454 | // We don't expect there to be very many common symbols, so we keep |
1455 | // a list of them. When we find a common symbol we add it to this | |
1456 | // list. It is possible that by the time we process the list the | |
1457 | // symbol is no longer a common symbol. It may also have become a | |
1458 | // forwarder. | |
1459 | Commons_type commons_; | |
155a0dd7 ILT |
1460 | // This is like the commons_ field, except that it holds TLS common |
1461 | // symbols. | |
1462 | Commons_type tls_commons_; | |
55a93433 ILT |
1463 | // A list of symbols which have been forced to be local. We don't |
1464 | // expect there to be very many of them, so we keep a list of them | |
1465 | // rather than walking the whole table to find them. | |
1466 | Forced_locals forced_locals_; | |
f6ce93d6 ILT |
1467 | // Manage symbol warnings. |
1468 | Warnings warnings_; | |
70e654ba ILT |
1469 | // Manage potential One Definition Rule (ODR) violations. |
1470 | Odr_map candidate_odr_violations_; | |
1471 | ||
46fe1623 ILT |
1472 | // When we emit a COPY reloc for a symbol, we define it in an |
1473 | // Output_data. When it's time to emit version information for it, | |
1474 | // we need to know the dynamic object in which we found the original | |
1475 | // definition. This maps symbols with COPY relocs to the dynamic | |
1476 | // object where they were defined. | |
1477 | Copied_symbol_dynobjs copied_symbol_dynobjs_; | |
09124467 ILT |
1478 | // Information parsed from the version script, if any. |
1479 | const Version_script_info& version_script_; | |
bae7f79e ILT |
1480 | }; |
1481 | ||
1564db8d ILT |
1482 | // We inline get_sized_symbol for efficiency. |
1483 | ||
1484 | template<int size> | |
1485 | Sized_symbol<size>* | |
7d1a9ebb | 1486 | Symbol_table::get_sized_symbol(Symbol* sym) const |
1564db8d | 1487 | { |
8851ecca | 1488 | gold_assert(size == parameters->target().get_size()); |
1564db8d ILT |
1489 | return static_cast<Sized_symbol<size>*>(sym); |
1490 | } | |
1491 | ||
1492 | template<int size> | |
1493 | const Sized_symbol<size>* | |
7d1a9ebb | 1494 | Symbol_table::get_sized_symbol(const Symbol* sym) const |
1564db8d | 1495 | { |
8851ecca | 1496 | gold_assert(size == parameters->target().get_size()); |
1564db8d ILT |
1497 | return static_cast<const Sized_symbol<size>*>(sym); |
1498 | } | |
1499 | ||
bae7f79e ILT |
1500 | } // End namespace gold. |
1501 | ||
1502 | #endif // !defined(GOLD_SYMTAB_H) |