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