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