Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // object.h -- support for an object file for linking in gold -*- C++ -*- |
2 | ||
2571583a | 3 | // Copyright (C) 2006-2017 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 | #ifndef GOLD_OBJECT_H |
24 | #define GOLD_OBJECT_H | |
25 | ||
92e059d8 | 26 | #include <string> |
a2fb1b05 | 27 | #include <vector> |
14bfc3f5 | 28 | |
bae7f79e | 29 | #include "elfcpp.h" |
645f8123 | 30 | #include "elfcpp_file.h" |
bae7f79e | 31 | #include "fileread.h" |
14bfc3f5 | 32 | #include "target.h" |
b0193076 | 33 | #include "archive.h" |
bae7f79e ILT |
34 | |
35 | namespace gold | |
36 | { | |
37 | ||
92e059d8 | 38 | class General_options; |
17a1d0a9 | 39 | class Task; |
92de84a6 | 40 | class Cref; |
a2fb1b05 | 41 | class Layout; |
7223e9ca | 42 | class Output_data; |
61ba1cf9 | 43 | class Output_section; |
dbe40a88 | 44 | class Output_section_data; |
61ba1cf9 | 45 | class Output_file; |
d491d34e | 46 | class Output_symtab_xindex; |
89fc3421 | 47 | class Pluginobj; |
f6ce93d6 | 48 | class Dynobj; |
4625f782 | 49 | class Object_merge_map; |
6a74a719 | 50 | class Relocatable_relocs; |
2c54b4f4 | 51 | struct Symbols_data; |
a2fb1b05 | 52 | |
b8e6aad9 ILT |
53 | template<typename Stringpool_char> |
54 | class Stringpool_template; | |
55 | ||
bae7f79e ILT |
56 | // Data to pass from read_symbols() to add_symbols(). |
57 | ||
58 | struct Read_symbols_data | |
59 | { | |
00698fc5 CC |
60 | Read_symbols_data() |
61 | : section_headers(NULL), section_names(NULL), symbols(NULL), | |
62 | symbol_names(NULL), versym(NULL), verdef(NULL), verneed(NULL) | |
63 | { } | |
64 | ||
65 | ~Read_symbols_data(); | |
66 | ||
12e14209 ILT |
67 | // Section headers. |
68 | File_view* section_headers; | |
69 | // Section names. | |
70 | File_view* section_names; | |
71 | // Size of section name data in bytes. | |
8383303e | 72 | section_size_type section_names_size; |
bae7f79e ILT |
73 | // Symbol data. |
74 | File_view* symbols; | |
75 | // Size of symbol data in bytes. | |
8383303e | 76 | section_size_type symbols_size; |
730cdc88 ILT |
77 | // Offset of external symbols within symbol data. This structure |
78 | // sometimes contains only external symbols, in which case this will | |
79 | // be zero. Sometimes it contains all symbols. | |
8383303e | 80 | section_offset_type external_symbols_offset; |
bae7f79e ILT |
81 | // Symbol names. |
82 | File_view* symbol_names; | |
83 | // Size of symbol name data in bytes. | |
8383303e | 84 | section_size_type symbol_names_size; |
dbe717ef ILT |
85 | |
86 | // Version information. This is only used on dynamic objects. | |
87 | // Version symbol data (from SHT_GNU_versym section). | |
88 | File_view* versym; | |
8383303e | 89 | section_size_type versym_size; |
dbe717ef ILT |
90 | // Version definition data (from SHT_GNU_verdef section). |
91 | File_view* verdef; | |
8383303e | 92 | section_size_type verdef_size; |
dbe717ef ILT |
93 | unsigned int verdef_info; |
94 | // Needed version data (from SHT_GNU_verneed section). | |
95 | File_view* verneed; | |
8383303e | 96 | section_size_type verneed_size; |
dbe717ef | 97 | unsigned int verneed_info; |
bae7f79e ILT |
98 | }; |
99 | ||
f7e2ee48 ILT |
100 | // Information used to print error messages. |
101 | ||
102 | struct Symbol_location_info | |
103 | { | |
104 | std::string source_file; | |
105 | std::string enclosing_symbol_name; | |
60e8b3fc | 106 | elfcpp::STT enclosing_symbol_type; |
f7e2ee48 ILT |
107 | }; |
108 | ||
92e059d8 ILT |
109 | // Data about a single relocation section. This is read in |
110 | // read_relocs and processed in scan_relocs. | |
111 | ||
112 | struct Section_relocs | |
113 | { | |
00698fc5 CC |
114 | Section_relocs() |
115 | : contents(NULL) | |
116 | { } | |
117 | ||
118 | ~Section_relocs() | |
119 | { delete this->contents; } | |
120 | ||
92e059d8 ILT |
121 | // Index of reloc section. |
122 | unsigned int reloc_shndx; | |
123 | // Index of section that relocs apply to. | |
124 | unsigned int data_shndx; | |
125 | // Contents of reloc section. | |
126 | File_view* contents; | |
127 | // Reloc section type. | |
128 | unsigned int sh_type; | |
129 | // Number of reloc entries. | |
130 | size_t reloc_count; | |
730cdc88 ILT |
131 | // Output section. |
132 | Output_section* output_section; | |
133 | // Whether this section has special handling for offsets. | |
134 | bool needs_special_offset_handling; | |
7019cd25 ILT |
135 | // Whether the data section is allocated (has the SHF_ALLOC flag set). |
136 | bool is_data_section_allocated; | |
92e059d8 ILT |
137 | }; |
138 | ||
139 | // Relocations in an object file. This is read in read_relocs and | |
140 | // processed in scan_relocs. | |
141 | ||
142 | struct Read_relocs_data | |
143 | { | |
00698fc5 CC |
144 | Read_relocs_data() |
145 | : local_symbols(NULL) | |
146 | { } | |
147 | ||
148 | ~Read_relocs_data() | |
149 | { delete this->local_symbols; } | |
150 | ||
92e059d8 ILT |
151 | typedef std::vector<Section_relocs> Relocs_list; |
152 | // The relocations. | |
153 | Relocs_list relocs; | |
154 | // The local symbols. | |
155 | File_view* local_symbols; | |
156 | }; | |
157 | ||
d491d34e ILT |
158 | // The Xindex class manages section indexes for objects with more than |
159 | // 0xff00 sections. | |
160 | ||
161 | class Xindex | |
162 | { | |
163 | public: | |
164 | Xindex(int large_shndx_offset) | |
165 | : large_shndx_offset_(large_shndx_offset), symtab_xindex_() | |
166 | { } | |
167 | ||
168 | // Initialize the symtab_xindex_ array, given the object and the | |
169 | // section index of the symbol table to use. | |
170 | template<int size, bool big_endian> | |
171 | void | |
172 | initialize_symtab_xindex(Object*, unsigned int symtab_shndx); | |
173 | ||
174 | // Read in the symtab_xindex_ array, given its section index. | |
175 | // PSHDRS may optionally point to the section headers. | |
176 | template<int size, bool big_endian> | |
177 | void | |
178 | read_symtab_xindex(Object*, unsigned int xindex_shndx, | |
179 | const unsigned char* pshdrs); | |
180 | ||
181 | // Symbol SYMNDX in OBJECT has a section of SHN_XINDEX; return the | |
182 | // real section index. | |
183 | unsigned int | |
184 | sym_xindex_to_shndx(Object* object, unsigned int symndx); | |
185 | ||
186 | private: | |
187 | // The type of the array giving the real section index for symbols | |
188 | // whose st_shndx field holds SHN_XINDEX. | |
189 | typedef std::vector<unsigned int> Symtab_xindex; | |
190 | ||
191 | // Adjust a section index if necessary. This should only be called | |
192 | // for ordinary section indexes. | |
193 | unsigned int | |
194 | adjust_shndx(unsigned int shndx) | |
195 | { | |
196 | if (shndx >= elfcpp::SHN_LORESERVE) | |
197 | shndx += this->large_shndx_offset_; | |
198 | return shndx; | |
199 | } | |
200 | ||
201 | // Adjust to apply to large section indexes. | |
202 | int large_shndx_offset_; | |
203 | // The data from the SHT_SYMTAB_SHNDX section. | |
204 | Symtab_xindex symtab_xindex_; | |
205 | }; | |
206 | ||
cdc29364 CC |
207 | // A GOT offset list. A symbol may have more than one GOT offset |
208 | // (e.g., when mixing modules compiled with two different TLS models), | |
209 | // but will usually have at most one. GOT_TYPE identifies the type of | |
210 | // GOT entry; its values are specific to each target. | |
211 | ||
212 | class Got_offset_list | |
213 | { | |
214 | public: | |
215 | Got_offset_list() | |
216 | : got_type_(-1U), got_offset_(0), got_next_(NULL) | |
217 | { } | |
218 | ||
219 | Got_offset_list(unsigned int got_type, unsigned int got_offset) | |
220 | : got_type_(got_type), got_offset_(got_offset), got_next_(NULL) | |
221 | { } | |
222 | ||
223 | ~Got_offset_list() | |
224 | { | |
225 | if (this->got_next_ != NULL) | |
226 | { | |
227 | delete this->got_next_; | |
228 | this->got_next_ = NULL; | |
229 | } | |
230 | } | |
231 | ||
232 | // Initialize the fields to their default values. | |
233 | void | |
234 | init() | |
235 | { | |
236 | this->got_type_ = -1U; | |
237 | this->got_offset_ = 0; | |
238 | this->got_next_ = NULL; | |
239 | } | |
240 | ||
241 | // Set the offset for the GOT entry of type GOT_TYPE. | |
242 | void | |
243 | set_offset(unsigned int got_type, unsigned int got_offset) | |
244 | { | |
245 | if (this->got_type_ == -1U) | |
246 | { | |
247 | this->got_type_ = got_type; | |
248 | this->got_offset_ = got_offset; | |
249 | } | |
250 | else | |
251 | { | |
252 | for (Got_offset_list* g = this; g != NULL; g = g->got_next_) | |
253 | { | |
254 | if (g->got_type_ == got_type) | |
255 | { | |
256 | g->got_offset_ = got_offset; | |
257 | return; | |
258 | } | |
259 | } | |
260 | Got_offset_list* g = new Got_offset_list(got_type, got_offset); | |
261 | g->got_next_ = this->got_next_; | |
262 | this->got_next_ = g; | |
263 | } | |
264 | } | |
265 | ||
266 | // Return the offset for a GOT entry of type GOT_TYPE. | |
267 | unsigned int | |
268 | get_offset(unsigned int got_type) const | |
269 | { | |
270 | for (const Got_offset_list* g = this; g != NULL; g = g->got_next_) | |
271 | { | |
272 | if (g->got_type_ == got_type) | |
273 | return g->got_offset_; | |
274 | } | |
275 | return -1U; | |
276 | } | |
277 | ||
278 | // Return a pointer to the list, or NULL if the list is empty. | |
279 | const Got_offset_list* | |
280 | get_list() const | |
281 | { | |
282 | if (this->got_type_ == -1U) | |
283 | return NULL; | |
284 | return this; | |
285 | } | |
286 | ||
287 | // Abstract visitor class for iterating over GOT offsets. | |
288 | class Visitor | |
289 | { | |
290 | public: | |
291 | Visitor() | |
292 | { } | |
293 | ||
294 | virtual | |
295 | ~Visitor() | |
296 | { } | |
297 | ||
298 | virtual void | |
299 | visit(unsigned int, unsigned int) = 0; | |
300 | }; | |
301 | ||
302 | // Loop over all GOT offset entries, calling a visitor class V for each. | |
303 | void | |
304 | for_all_got_offsets(Visitor* v) const | |
305 | { | |
306 | if (this->got_type_ == -1U) | |
307 | return; | |
308 | for (const Got_offset_list* g = this; g != NULL; g = g->got_next_) | |
309 | v->visit(g->got_type_, g->got_offset_); | |
310 | } | |
311 | ||
312 | private: | |
313 | unsigned int got_type_; | |
314 | unsigned int got_offset_; | |
315 | Got_offset_list* got_next_; | |
316 | }; | |
317 | ||
7ef8ae7c VR |
318 | // The Local_got_entry_key used to index the GOT offsets for local |
319 | // non-TLS symbols, and tp-relative offsets for TLS symbols. | |
320 | ||
321 | class Local_got_entry_key | |
322 | { | |
323 | public: | |
324 | Local_got_entry_key(unsigned int symndx, uint64_t addend) | |
325 | : symndx_(symndx), addend_(addend) | |
326 | {} | |
327 | ||
328 | // Whether this equals to another Local_got_entry_key. | |
329 | bool | |
330 | eq(const Local_got_entry_key& key) const | |
331 | { | |
332 | return (this->symndx_ == key.symndx_ && this->addend_ == key.addend_); | |
333 | } | |
334 | ||
335 | // Compute a hash value for this using 64-bit FNV-1a hash. | |
336 | size_t | |
337 | hash_value() const | |
338 | { | |
339 | uint64_t h = 14695981039346656037ULL; // FNV offset basis. | |
340 | uint64_t prime = 1099511628211ULL; | |
341 | h = (h ^ static_cast<uint64_t>(this->symndx_)) * prime; | |
342 | h = (h ^ static_cast<uint64_t>(this->addend_)) * prime; | |
343 | return h; | |
344 | } | |
345 | ||
346 | // Functors for associative containers. | |
347 | struct equal_to | |
348 | { | |
349 | bool | |
350 | operator()(const Local_got_entry_key& key1, | |
351 | const Local_got_entry_key& key2) const | |
352 | { return key1.eq(key2); } | |
353 | }; | |
354 | ||
355 | struct hash | |
356 | { | |
357 | size_t | |
358 | operator()(const Local_got_entry_key& key) const | |
359 | { return key.hash_value(); } | |
360 | }; | |
361 | ||
362 | private: | |
363 | // The local symbol index. | |
364 | unsigned int symndx_; | |
365 | // The addend. | |
366 | uint64_t addend_; | |
367 | }; | |
368 | ||
0d5bbdb0 CC |
369 | // Type for mapping section index to uncompressed size and contents. |
370 | ||
371 | struct Compressed_section_info | |
372 | { | |
373 | section_size_type size; | |
48058663 | 374 | elfcpp::Elf_Xword flag; |
0d5bbdb0 CC |
375 | const unsigned char* contents; |
376 | }; | |
377 | typedef std::map<unsigned int, Compressed_section_info> Compressed_section_map; | |
378 | ||
379 | template<int size, bool big_endian> | |
380 | Compressed_section_map* | |
381 | build_compressed_section_map(const unsigned char* pshdrs, unsigned int shnum, | |
382 | const char* names, section_size_type names_size, | |
383 | Object* obj, bool decompress_if_needed); | |
384 | ||
f6ce93d6 ILT |
385 | // Object is an abstract base class which represents either a 32-bit |
386 | // or a 64-bit input object. This can be a regular object file | |
387 | // (ET_REL) or a shared object (ET_DYN). | |
bae7f79e ILT |
388 | |
389 | class Object | |
390 | { | |
391 | public: | |
dde3f402 ILT |
392 | typedef std::vector<Symbol*> Symbols; |
393 | ||
bae7f79e ILT |
394 | // NAME is the name of the object as we would report it to the user |
395 | // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is | |
396 | // used to read the file. OFFSET is the offset within the input | |
397 | // file--0 for a .o or .so file, something else for a .a file. | |
2ea97941 ILT |
398 | Object(const std::string& name, Input_file* input_file, bool is_dynamic, |
399 | off_t offset = 0) | |
400 | : name_(name), input_file_(input_file), offset_(offset), shnum_(-1U), | |
401 | is_dynamic_(is_dynamic), is_needed_(false), uses_split_stack_(false), | |
cdc29364 | 402 | has_no_split_stack_(false), no_export_(false), |
0d5bbdb0 CC |
403 | is_in_system_directory_(false), as_needed_(false), xindex_(NULL), |
404 | compressed_sections_(NULL) | |
cdc29364 CC |
405 | { |
406 | if (input_file != NULL) | |
407 | { | |
408 | input_file->file().add_object(); | |
409 | this->is_in_system_directory_ = input_file->is_in_system_directory(); | |
0f1c85a6 | 410 | this->as_needed_ = input_file->options().as_needed(); |
cdc29364 CC |
411 | } |
412 | } | |
bae7f79e ILT |
413 | |
414 | virtual ~Object() | |
cdc29364 CC |
415 | { |
416 | if (this->input_file_ != NULL) | |
417 | this->input_file_->file().remove_object(); | |
418 | } | |
bae7f79e | 419 | |
cf43a2fe | 420 | // Return the name of the object as we would report it to the user. |
bae7f79e ILT |
421 | const std::string& |
422 | name() const | |
423 | { return this->name_; } | |
424 | ||
cec9d2f3 ILT |
425 | // Get the offset into the file. |
426 | off_t | |
427 | offset() const | |
428 | { return this->offset_; } | |
429 | ||
14bfc3f5 ILT |
430 | // Return whether this is a dynamic object. |
431 | bool | |
432 | is_dynamic() const | |
433 | { return this->is_dynamic_; } | |
434 | ||
48058663 L |
435 | // Return the word size of the object file. |
436 | virtual int elfsize() const = 0; | |
437 | ||
438 | // Return TRUE if this is a big-endian object file. | |
439 | virtual bool is_big_endian() const = 0; | |
440 | ||
594c8e5e ILT |
441 | // Return whether this object is needed--true if it is a dynamic |
442 | // object which defines some symbol referenced by a regular object. | |
443 | // We keep the flag here rather than in Dynobj for convenience when | |
444 | // setting it. | |
445 | bool | |
446 | is_needed() const | |
447 | { return this->is_needed_; } | |
448 | ||
449 | // Record that this object is needed. | |
450 | void | |
451 | set_is_needed() | |
452 | { this->is_needed_ = true; } | |
453 | ||
364c7fa5 ILT |
454 | // Return whether this object was compiled with -fsplit-stack. |
455 | bool | |
456 | uses_split_stack() const | |
457 | { return this->uses_split_stack_; } | |
458 | ||
459 | // Return whether this object contains any functions compiled with | |
460 | // the no_split_stack attribute. | |
461 | bool | |
462 | has_no_split_stack() const | |
463 | { return this->has_no_split_stack_; } | |
464 | ||
0f1c85a6 CC |
465 | // Returns NULL for Objects that are not dynamic objects. This method |
466 | // is overridden in the Dynobj class. | |
467 | Dynobj* | |
468 | dynobj() | |
469 | { return this->do_dynobj(); } | |
470 | ||
89fc3421 CC |
471 | // Returns NULL for Objects that are not plugin objects. This method |
472 | // is overridden in the Pluginobj class. | |
473 | Pluginobj* | |
474 | pluginobj() | |
475 | { return this->do_pluginobj(); } | |
476 | ||
15f8229b ILT |
477 | // Get the file. We pass on const-ness. |
478 | Input_file* | |
479 | input_file() | |
cdc29364 CC |
480 | { |
481 | gold_assert(this->input_file_ != NULL); | |
482 | return this->input_file_; | |
483 | } | |
15f8229b ILT |
484 | |
485 | const Input_file* | |
486 | input_file() const | |
cdc29364 CC |
487 | { |
488 | gold_assert(this->input_file_ != NULL); | |
489 | return this->input_file_; | |
490 | } | |
15f8229b | 491 | |
a2fb1b05 ILT |
492 | // Lock the underlying file. |
493 | void | |
17a1d0a9 | 494 | lock(const Task* t) |
cdc29364 CC |
495 | { |
496 | if (this->input_file_ != NULL) | |
497 | this->input_file_->file().lock(t); | |
498 | } | |
a2fb1b05 ILT |
499 | |
500 | // Unlock the underlying file. | |
501 | void | |
17a1d0a9 | 502 | unlock(const Task* t) |
cdc29364 CC |
503 | { |
504 | if (this->input_file_ != NULL) | |
505 | this->input_file()->file().unlock(t); | |
506 | } | |
a2fb1b05 ILT |
507 | |
508 | // Return whether the underlying file is locked. | |
509 | bool | |
510 | is_locked() const | |
cdc29364 | 511 | { return this->input_file_ != NULL && this->input_file_->file().is_locked(); } |
a2fb1b05 | 512 | |
17a1d0a9 ILT |
513 | // Return the token, so that the task can be queued. |
514 | Task_token* | |
515 | token() | |
cdc29364 CC |
516 | { |
517 | if (this->input_file_ == NULL) | |
518 | return NULL; | |
519 | return this->input_file()->file().token(); | |
520 | } | |
17a1d0a9 ILT |
521 | |
522 | // Release the underlying file. | |
523 | void | |
524 | release() | |
cdc29364 CC |
525 | { |
526 | if (this->input_file_ != NULL) | |
527 | this->input_file()->file().release(); | |
528 | } | |
17a1d0a9 | 529 | |
88dd47ac ILT |
530 | // Return whether we should just read symbols from this file. |
531 | bool | |
532 | just_symbols() const | |
533 | { return this->input_file()->just_symbols(); } | |
534 | ||
cdc29364 CC |
535 | // Return whether this is an incremental object. |
536 | bool | |
537 | is_incremental() const | |
538 | { return this->do_is_incremental(); } | |
539 | ||
540 | // Return the last modified time of the file. | |
541 | Timespec | |
542 | get_mtime() | |
543 | { return this->do_get_mtime(); } | |
544 | ||
5a6f7e2d ILT |
545 | // Get the number of sections. |
546 | unsigned int | |
547 | shnum() const | |
548 | { return this->shnum_; } | |
a2fb1b05 | 549 | |
f6ce93d6 | 550 | // Return a view of the contents of a section. Set *PLEN to the |
9eb9fa57 | 551 | // size. CACHE is a hint as in File_read::get_view. |
f6ce93d6 | 552 | const unsigned char* |
8383303e | 553 | section_contents(unsigned int shndx, section_size_type* plen, bool cache); |
f6ce93d6 | 554 | |
d491d34e ILT |
555 | // Adjust a symbol's section index as needed. SYMNDX is the index |
556 | // of the symbol and SHNDX is the symbol's section from | |
557 | // get_st_shndx. This returns the section index. It sets | |
558 | // *IS_ORDINARY to indicate whether this is a normal section index, | |
559 | // rather than a special code between SHN_LORESERVE and | |
560 | // SHN_HIRESERVE. | |
561 | unsigned int | |
562 | adjust_sym_shndx(unsigned int symndx, unsigned int shndx, bool* is_ordinary) | |
563 | { | |
564 | if (shndx < elfcpp::SHN_LORESERVE) | |
565 | *is_ordinary = true; | |
566 | else if (shndx == elfcpp::SHN_XINDEX) | |
567 | { | |
568 | if (this->xindex_ == NULL) | |
569 | this->xindex_ = this->do_initialize_xindex(); | |
570 | shndx = this->xindex_->sym_xindex_to_shndx(this, symndx); | |
571 | *is_ordinary = true; | |
572 | } | |
573 | else | |
574 | *is_ordinary = false; | |
575 | return shndx; | |
576 | } | |
577 | ||
a445fddf ILT |
578 | // Return the size of a section given a section index. |
579 | uint64_t | |
580 | section_size(unsigned int shndx) | |
581 | { return this->do_section_size(shndx); } | |
582 | ||
583 | // Return the name of a section given a section index. | |
f6ce93d6 | 584 | std::string |
54674d38 | 585 | section_name(unsigned int shndx) const |
a3ad94ed ILT |
586 | { return this->do_section_name(shndx); } |
587 | ||
588 | // Return the section flags given a section index. | |
589 | uint64_t | |
590 | section_flags(unsigned int shndx) | |
591 | { return this->do_section_flags(shndx); } | |
f6ce93d6 | 592 | |
ef15dade ST |
593 | // Return the section entsize given a section index. |
594 | uint64_t | |
595 | section_entsize(unsigned int shndx) | |
596 | { return this->do_section_entsize(shndx); } | |
597 | ||
88dd47ac ILT |
598 | // Return the section address given a section index. |
599 | uint64_t | |
600 | section_address(unsigned int shndx) | |
601 | { return this->do_section_address(shndx); } | |
602 | ||
730cdc88 ILT |
603 | // Return the section type given a section index. |
604 | unsigned int | |
605 | section_type(unsigned int shndx) | |
606 | { return this->do_section_type(shndx); } | |
607 | ||
f7e2ee48 ILT |
608 | // Return the section link field given a section index. |
609 | unsigned int | |
610 | section_link(unsigned int shndx) | |
611 | { return this->do_section_link(shndx); } | |
612 | ||
4c50553d ILT |
613 | // Return the section info field given a section index. |
614 | unsigned int | |
615 | section_info(unsigned int shndx) | |
616 | { return this->do_section_info(shndx); } | |
617 | ||
a445fddf ILT |
618 | // Return the required section alignment given a section index. |
619 | uint64_t | |
620 | section_addralign(unsigned int shndx) | |
621 | { return this->do_section_addralign(shndx); } | |
622 | ||
09ec0418 CC |
623 | // Return the output section given a section index. |
624 | Output_section* | |
625 | output_section(unsigned int shndx) const | |
626 | { return this->do_output_section(shndx); } | |
627 | ||
c6905c28 CC |
628 | // Given a section index, return its address. |
629 | // The return value will be -1U if the section is specially mapped, | |
630 | // such as a merge section. | |
631 | uint64_t | |
632 | output_section_address(unsigned int shndx) | |
633 | { return this->do_output_section_address(shndx); } | |
634 | ||
09ec0418 CC |
635 | // Given a section index, return the offset in the Output_section. |
636 | // The return value will be -1U if the section is specially mapped, | |
637 | // such as a merge section. | |
638 | uint64_t | |
639 | output_section_offset(unsigned int shndx) const | |
640 | { return this->do_output_section_offset(shndx); } | |
641 | ||
5a6f7e2d ILT |
642 | // Read the symbol information. |
643 | void | |
644 | read_symbols(Read_symbols_data* sd) | |
645 | { return this->do_read_symbols(sd); } | |
646 | ||
647 | // Pass sections which should be included in the link to the Layout | |
648 | // object, and record where the sections go in the output file. | |
649 | void | |
2ea97941 ILT |
650 | layout(Symbol_table* symtab, Layout* layout, Read_symbols_data* sd) |
651 | { this->do_layout(symtab, layout, sd); } | |
5a6f7e2d ILT |
652 | |
653 | // Add symbol information to the global symbol table. | |
654 | void | |
2ea97941 ILT |
655 | add_symbols(Symbol_table* symtab, Read_symbols_data* sd, Layout *layout) |
656 | { this->do_add_symbols(symtab, sd, layout); } | |
5a6f7e2d | 657 | |
b0193076 RÁE |
658 | // Add symbol information to the global symbol table. |
659 | Archive::Should_include | |
88a4108b ILT |
660 | should_include_member(Symbol_table* symtab, Layout* layout, |
661 | Read_symbols_data* sd, std::string* why) | |
662 | { return this->do_should_include_member(symtab, layout, sd, why); } | |
b0193076 | 663 | |
e0c52780 CC |
664 | // Iterate over global symbols, calling a visitor class V for each. |
665 | void | |
666 | for_all_global_symbols(Read_symbols_data* sd, | |
667 | Library_base::Symbol_visitor_base* v) | |
668 | { return this->do_for_all_global_symbols(sd, v); } | |
669 | ||
cdc29364 CC |
670 | // Iterate over local symbols, calling a visitor class V for each GOT offset |
671 | // associated with a local symbol. | |
672 | void | |
673 | for_all_local_got_entries(Got_offset_list::Visitor* v) const | |
674 | { this->do_for_all_local_got_entries(v); } | |
675 | ||
645f8123 ILT |
676 | // Functions and types for the elfcpp::Elf_file interface. This |
677 | // permit us to use Object as the File template parameter for | |
678 | // elfcpp::Elf_file. | |
679 | ||
680 | // The View class is returned by view. It must support a single | |
681 | // method, data(). This is trivial, because get_view does what we | |
682 | // need. | |
683 | class View | |
684 | { | |
685 | public: | |
686 | View(const unsigned char* p) | |
687 | : p_(p) | |
688 | { } | |
689 | ||
690 | const unsigned char* | |
691 | data() const | |
692 | { return this->p_; } | |
693 | ||
694 | private: | |
695 | const unsigned char* p_; | |
696 | }; | |
697 | ||
698 | // Return a View. | |
699 | View | |
8383303e | 700 | view(off_t file_offset, section_size_type data_size) |
39d0cb0e | 701 | { return View(this->get_view(file_offset, data_size, true, true)); } |
645f8123 ILT |
702 | |
703 | // Report an error. | |
704 | void | |
75f2446e | 705 | error(const char* format, ...) const ATTRIBUTE_PRINTF_2; |
645f8123 ILT |
706 | |
707 | // A location in the file. | |
708 | struct Location | |
709 | { | |
710 | off_t file_offset; | |
711 | off_t data_size; | |
712 | ||
8383303e | 713 | Location(off_t fo, section_size_type ds) |
645f8123 ILT |
714 | : file_offset(fo), data_size(ds) |
715 | { } | |
716 | }; | |
717 | ||
718 | // Get a View given a Location. | |
719 | View view(Location loc) | |
39d0cb0e | 720 | { return View(this->get_view(loc.file_offset, loc.data_size, true, true)); } |
645f8123 | 721 | |
cb295612 ILT |
722 | // Get a view into the underlying file. |
723 | const unsigned char* | |
39d0cb0e | 724 | get_view(off_t start, section_size_type size, bool aligned, bool cache) |
cb295612 | 725 | { |
39d0cb0e ILT |
726 | return this->input_file()->file().get_view(this->offset_, start, size, |
727 | aligned, cache); | |
cb295612 ILT |
728 | } |
729 | ||
730 | // Get a lasting view into the underlying file. | |
731 | File_view* | |
39d0cb0e ILT |
732 | get_lasting_view(off_t start, section_size_type size, bool aligned, |
733 | bool cache) | |
cb295612 | 734 | { |
39d0cb0e ILT |
735 | return this->input_file()->file().get_lasting_view(this->offset_, start, |
736 | size, aligned, cache); | |
cb295612 ILT |
737 | } |
738 | ||
739 | // Read data from the underlying file. | |
740 | void | |
2a00e4fb | 741 | read(off_t start, section_size_type size, void* p) |
cb295612 ILT |
742 | { this->input_file()->file().read(start + this->offset_, size, p); } |
743 | ||
744 | // Read multiple data from the underlying file. | |
745 | void | |
746 | read_multiple(const File_read::Read_multiple& rm) | |
747 | { this->input_file()->file().read_multiple(this->offset_, rm); } | |
748 | ||
749 | // Stop caching views in the underlying file. | |
750 | void | |
751 | clear_view_cache_marks() | |
cdc29364 CC |
752 | { |
753 | if (this->input_file_ != NULL) | |
754 | this->input_file_->file().clear_view_cache_marks(); | |
755 | } | |
cb295612 | 756 | |
92de84a6 ILT |
757 | // Get the number of global symbols defined by this object, and the |
758 | // number of the symbols whose final definition came from this | |
759 | // object. | |
760 | void | |
761 | get_global_symbol_counts(const Symbol_table* symtab, size_t* defined, | |
762 | size_t* used) const | |
763 | { this->do_get_global_symbol_counts(symtab, defined, used); } | |
764 | ||
dde3f402 ILT |
765 | // Get the symbols defined in this object. |
766 | const Symbols* | |
767 | get_global_symbols() const | |
768 | { return this->do_get_global_symbols(); } | |
769 | ||
cdc29364 CC |
770 | // Set flag that this object was found in a system directory. |
771 | void | |
772 | set_is_in_system_directory() | |
773 | { this->is_in_system_directory_ = true; } | |
774 | ||
fd9d194f ILT |
775 | // Return whether this object was found in a system directory. |
776 | bool | |
777 | is_in_system_directory() const | |
cdc29364 | 778 | { return this->is_in_system_directory_; } |
fd9d194f | 779 | |
0f1c85a6 CC |
780 | // Set flag that this object was linked with --as-needed. |
781 | void | |
782 | set_as_needed() | |
783 | { this->as_needed_ = true; } | |
784 | ||
4bfacfd3 CC |
785 | // Clear flag that this object was linked with --as-needed. |
786 | void | |
787 | clear_as_needed() | |
788 | { this->as_needed_ = false; } | |
789 | ||
0f1c85a6 CC |
790 | // Return whether this object was linked with --as-needed. |
791 | bool | |
792 | as_needed() const | |
793 | { return this->as_needed_; } | |
794 | ||
15f8229b ILT |
795 | // Return whether we found this object by searching a directory. |
796 | bool | |
797 | searched_for() const | |
798 | { return this->input_file()->will_search_for(); } | |
799 | ||
65514900 CC |
800 | bool |
801 | no_export() const | |
802 | { return this->no_export_; } | |
803 | ||
804 | void | |
805 | set_no_export(bool value) | |
806 | { this->no_export_ = value; } | |
807 | ||
a2e47362 CC |
808 | bool |
809 | section_is_compressed(unsigned int shndx, | |
810 | section_size_type* uncompressed_size) const | |
0d5bbdb0 CC |
811 | { |
812 | if (this->compressed_sections_ == NULL) | |
813 | return false; | |
814 | Compressed_section_map::const_iterator p = | |
815 | this->compressed_sections_->find(shndx); | |
816 | if (p != this->compressed_sections_->end()) | |
817 | { | |
818 | if (uncompressed_size != NULL) | |
819 | *uncompressed_size = p->second.size; | |
820 | return true; | |
821 | } | |
822 | return false; | |
823 | } | |
a2e47362 | 824 | |
c1027032 | 825 | // Return a view of the decompressed contents of a section. Set *PLEN |
5dd8762a CC |
826 | // to the size. Set *IS_NEW to true if the contents need to be freed |
827 | // by the caller. | |
828 | const unsigned char* | |
829 | decompressed_section_contents(unsigned int shndx, section_size_type* plen, | |
0d5bbdb0 | 830 | bool* is_cached); |
5dd8762a CC |
831 | |
832 | // Discard any buffers of decompressed sections. This is done | |
833 | // at the end of the Add_symbols task. | |
834 | void | |
0d5bbdb0 | 835 | discard_decompressed_sections(); |
5dd8762a | 836 | |
09ec0418 CC |
837 | // Return the index of the first incremental relocation for symbol SYMNDX. |
838 | unsigned int | |
839 | get_incremental_reloc_base(unsigned int symndx) const | |
840 | { return this->do_get_incremental_reloc_base(symndx); } | |
841 | ||
842 | // Return the number of incremental relocations for symbol SYMNDX. | |
843 | unsigned int | |
844 | get_incremental_reloc_count(unsigned int symndx) const | |
845 | { return this->do_get_incremental_reloc_count(symndx); } | |
846 | ||
6b2353a5 | 847 | // Return the output view for section SHNDX. |
39040bb9 | 848 | unsigned char* |
6b2353a5 CC |
849 | get_output_view(unsigned int shndx, section_size_type* plen) const |
850 | { return this->do_get_output_view(shndx, plen); } | |
851 | ||
f6ce93d6 | 852 | protected: |
0f1c85a6 CC |
853 | // Returns NULL for Objects that are not dynamic objects. This method |
854 | // is overridden in the Dynobj class. | |
855 | virtual Dynobj* | |
856 | do_dynobj() | |
857 | { return NULL; } | |
858 | ||
89fc3421 CC |
859 | // Returns NULL for Objects that are not plugin objects. This method |
860 | // is overridden in the Pluginobj class. | |
861 | virtual Pluginobj* | |
862 | do_pluginobj() | |
863 | { return NULL; } | |
864 | ||
cdc29364 CC |
865 | // Return TRUE if this is an incremental (unchanged) input file. |
866 | // We return FALSE by default; the incremental object classes | |
867 | // override this method. | |
868 | virtual bool | |
869 | do_is_incremental() const | |
870 | { return false; } | |
871 | ||
872 | // Return the last modified time of the file. This method may be | |
873 | // overridden for subclasses that don't use an actual file (e.g., | |
874 | // Incremental objects). | |
875 | virtual Timespec | |
876 | do_get_mtime() | |
877 | { return this->input_file()->file().get_mtime(); } | |
878 | ||
f6ce93d6 ILT |
879 | // Read the symbols--implemented by child class. |
880 | virtual void | |
881 | do_read_symbols(Read_symbols_data*) = 0; | |
882 | ||
883 | // Lay out sections--implemented by child class. | |
884 | virtual void | |
7e1edb90 | 885 | do_layout(Symbol_table*, Layout*, Read_symbols_data*) = 0; |
f6ce93d6 ILT |
886 | |
887 | // Add symbol information to the global symbol table--implemented by | |
888 | // child class. | |
889 | virtual void | |
f488e4b0 | 890 | do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*) = 0; |
f6ce93d6 | 891 | |
b0193076 | 892 | virtual Archive::Should_include |
88a4108b | 893 | do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*, |
b0193076 RÁE |
894 | std::string* why) = 0; |
895 | ||
e0c52780 CC |
896 | // Iterate over global symbols, calling a visitor class V for each. |
897 | virtual void | |
898 | do_for_all_global_symbols(Read_symbols_data* sd, | |
899 | Library_base::Symbol_visitor_base* v) = 0; | |
900 | ||
cdc29364 CC |
901 | // Iterate over local symbols, calling a visitor class V for each GOT offset |
902 | // associated with a local symbol. | |
903 | virtual void | |
904 | do_for_all_local_got_entries(Got_offset_list::Visitor* v) const = 0; | |
905 | ||
645f8123 ILT |
906 | // Return the location of the contents of a section. Implemented by |
907 | // child class. | |
c1027032 CC |
908 | virtual const unsigned char* |
909 | do_section_contents(unsigned int shndx, section_size_type* plen, | |
910 | bool cache) = 0; | |
f6ce93d6 | 911 | |
a445fddf ILT |
912 | // Get the size of a section--implemented by child class. |
913 | virtual uint64_t | |
914 | do_section_size(unsigned int shndx) = 0; | |
915 | ||
f6ce93d6 ILT |
916 | // Get the name of a section--implemented by child class. |
917 | virtual std::string | |
54674d38 | 918 | do_section_name(unsigned int shndx) const = 0; |
a3ad94ed ILT |
919 | |
920 | // Get section flags--implemented by child class. | |
921 | virtual uint64_t | |
922 | do_section_flags(unsigned int shndx) = 0; | |
f6ce93d6 | 923 | |
ef15dade ST |
924 | // Get section entsize--implemented by child class. |
925 | virtual uint64_t | |
926 | do_section_entsize(unsigned int shndx) = 0; | |
927 | ||
88dd47ac ILT |
928 | // Get section address--implemented by child class. |
929 | virtual uint64_t | |
930 | do_section_address(unsigned int shndx) = 0; | |
931 | ||
730cdc88 ILT |
932 | // Get section type--implemented by child class. |
933 | virtual unsigned int | |
934 | do_section_type(unsigned int shndx) = 0; | |
935 | ||
f7e2ee48 ILT |
936 | // Get section link field--implemented by child class. |
937 | virtual unsigned int | |
938 | do_section_link(unsigned int shndx) = 0; | |
939 | ||
4c50553d ILT |
940 | // Get section info field--implemented by child class. |
941 | virtual unsigned int | |
942 | do_section_info(unsigned int shndx) = 0; | |
943 | ||
a445fddf ILT |
944 | // Get section alignment--implemented by child class. |
945 | virtual uint64_t | |
946 | do_section_addralign(unsigned int shndx) = 0; | |
947 | ||
09ec0418 CC |
948 | // Return the output section given a section index--implemented |
949 | // by child class. | |
950 | virtual Output_section* | |
951 | do_output_section(unsigned int) const | |
952 | { gold_unreachable(); } | |
953 | ||
c6905c28 CC |
954 | // Get the address of a section--implemented by child class. |
955 | virtual uint64_t | |
956 | do_output_section_address(unsigned int) | |
957 | { gold_unreachable(); } | |
958 | ||
09ec0418 CC |
959 | // Get the offset of a section--implemented by child class. |
960 | virtual uint64_t | |
961 | do_output_section_offset(unsigned int) const | |
962 | { gold_unreachable(); } | |
963 | ||
d491d34e ILT |
964 | // Return the Xindex structure to use. |
965 | virtual Xindex* | |
966 | do_initialize_xindex() = 0; | |
967 | ||
92de84a6 ILT |
968 | // Implement get_global_symbol_counts--implemented by child class. |
969 | virtual void | |
970 | do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const = 0; | |
971 | ||
dde3f402 ILT |
972 | virtual const Symbols* |
973 | do_get_global_symbols() const = 0; | |
974 | ||
dbe717ef ILT |
975 | // Set the number of sections. |
976 | void | |
2ea97941 ILT |
977 | set_shnum(int shnum) |
978 | { this->shnum_ = shnum; } | |
dbe717ef | 979 | |
6fa2a40b | 980 | // Functions used by both Sized_relobj_file and Sized_dynobj. |
dbe717ef ILT |
981 | |
982 | // Read the section data into a Read_symbols_data object. | |
983 | template<int size, bool big_endian> | |
984 | void | |
985 | read_section_data(elfcpp::Elf_file<size, big_endian, Object>*, | |
986 | Read_symbols_data*); | |
987 | ||
dc3714f3 AM |
988 | // Find the section header with the given NAME. If HDR is non-NULL |
989 | // then it is a section header returned from a previous call to this | |
990 | // function and the next section header with the same name will be | |
991 | // returned. | |
992 | template<int size, bool big_endian> | |
993 | const unsigned char* | |
994 | find_shdr(const unsigned char* pshdrs, const char* name, | |
995 | const char* names, section_size_type names_size, | |
996 | const unsigned char* hdr) const; | |
997 | ||
d491d34e ILT |
998 | // Let the child class initialize the xindex object directly. |
999 | void | |
1000 | set_xindex(Xindex* xindex) | |
1001 | { | |
1002 | gold_assert(this->xindex_ == NULL); | |
1003 | this->xindex_ = xindex; | |
1004 | } | |
1005 | ||
dbe717ef ILT |
1006 | // If NAME is the name of a special .gnu.warning section, arrange |
1007 | // for the warning to be issued. SHNDX is the section index. | |
1008 | // Return whether it is a warning section. | |
1009 | bool | |
1010 | handle_gnu_warning_section(const char* name, unsigned int shndx, | |
1011 | Symbol_table*); | |
f6ce93d6 | 1012 | |
364c7fa5 | 1013 | // If NAME is the name of the special section which indicates that |
9b547ce6 | 1014 | // this object was compiled with -fsplit-stack, mark it accordingly, |
364c7fa5 ILT |
1015 | // and return true. Otherwise return false. |
1016 | bool | |
1017 | handle_split_stack_section(const char* name); | |
1018 | ||
5dd8762a CC |
1019 | // Discard any buffers of decompressed sections. This is done |
1020 | // at the end of the Add_symbols task. | |
1021 | virtual void | |
1022 | do_discard_decompressed_sections() | |
1023 | { } | |
1024 | ||
09ec0418 CC |
1025 | // Return the index of the first incremental relocation for symbol SYMNDX-- |
1026 | // implemented by child class. | |
1027 | virtual unsigned int | |
1028 | do_get_incremental_reloc_base(unsigned int) const | |
1029 | { gold_unreachable(); } | |
1030 | ||
1031 | // Return the number of incremental relocations for symbol SYMNDX-- | |
1032 | // implemented by child class. | |
1033 | virtual unsigned int | |
1034 | do_get_incremental_reloc_count(unsigned int) const | |
1035 | { gold_unreachable(); } | |
1036 | ||
6b2353a5 | 1037 | // Return the output view for a section. |
39040bb9 | 1038 | virtual unsigned char* |
6b2353a5 CC |
1039 | do_get_output_view(unsigned int, section_size_type*) const |
1040 | { gold_unreachable(); } | |
1041 | ||
0d5bbdb0 CC |
1042 | void |
1043 | set_compressed_sections(Compressed_section_map* compressed_sections) | |
1044 | { this->compressed_sections_ = compressed_sections; } | |
1045 | ||
1046 | Compressed_section_map* | |
1047 | compressed_sections() | |
1048 | { return this->compressed_sections_; } | |
1049 | ||
f6ce93d6 ILT |
1050 | private: |
1051 | // This class may not be copied. | |
1052 | Object(const Object&); | |
1053 | Object& operator=(const Object&); | |
1054 | ||
1055 | // Name of object as printed to user. | |
1056 | std::string name_; | |
1057 | // For reading the file. | |
1058 | Input_file* input_file_; | |
1059 | // Offset within the file--0 for an object file, non-0 for an | |
1060 | // archive. | |
1061 | off_t offset_; | |
dbe717ef ILT |
1062 | // Number of input sections. |
1063 | unsigned int shnum_; | |
f6ce93d6 | 1064 | // Whether this is a dynamic object. |
594c8e5e ILT |
1065 | bool is_dynamic_ : 1; |
1066 | // Whether this object is needed. This is only set for dynamic | |
1067 | // objects, and means that the object defined a symbol which was | |
1068 | // used by a reference from a regular object. | |
1069 | bool is_needed_ : 1; | |
364c7fa5 | 1070 | // Whether this object was compiled with -fsplit-stack. |
594c8e5e | 1071 | bool uses_split_stack_ : 1; |
364c7fa5 ILT |
1072 | // Whether this object contains any functions compiled with the |
1073 | // no_split_stack attribute. | |
594c8e5e | 1074 | bool has_no_split_stack_ : 1; |
65514900 CC |
1075 | // True if exclude this object from automatic symbol export. |
1076 | // This is used only for archive objects. | |
594c8e5e | 1077 | bool no_export_ : 1; |
cdc29364 CC |
1078 | // True if the object was found in a system directory. |
1079 | bool is_in_system_directory_ : 1; | |
0f1c85a6 CC |
1080 | // True if the object was linked with --as-needed. |
1081 | bool as_needed_ : 1; | |
594c8e5e ILT |
1082 | // Many sections for objects with more than SHN_LORESERVE sections. |
1083 | Xindex* xindex_; | |
0d5bbdb0 CC |
1084 | // For compressed debug sections, map section index to uncompressed size |
1085 | // and contents. | |
1086 | Compressed_section_map* compressed_sections_; | |
f6ce93d6 ILT |
1087 | }; |
1088 | ||
f6ce93d6 | 1089 | // A regular object (ET_REL). This is an abstract base class itself. |
6fa2a40b | 1090 | // The implementation is the template class Sized_relobj_file. |
f6ce93d6 ILT |
1091 | |
1092 | class Relobj : public Object | |
1093 | { | |
1094 | public: | |
2ea97941 ILT |
1095 | Relobj(const std::string& name, Input_file* input_file, off_t offset = 0) |
1096 | : Object(name, input_file, false, offset), | |
ef9beddf | 1097 | output_sections_(), |
6a74a719 | 1098 | map_to_relocatable_relocs_(NULL), |
4625f782 | 1099 | object_merge_map_(NULL), |
ef15dade | 1100 | relocs_must_follow_section_writes_(false), |
09ec0418 CC |
1101 | sd_(NULL), |
1102 | reloc_counts_(NULL), | |
6fa2a40b CC |
1103 | reloc_bases_(NULL), |
1104 | first_dyn_reloc_(0), | |
1105 | dyn_reloc_count_(0) | |
f6ce93d6 ILT |
1106 | { } |
1107 | ||
6d03d481 ST |
1108 | // During garbage collection, the Read_symbols_data pass for |
1109 | // each object is stored as layout needs to be done after | |
1110 | // reloc processing. | |
1111 | Symbols_data* | |
1112 | get_symbols_data() | |
1113 | { return this->sd_; } | |
1114 | ||
1115 | // Decides which section names have to be included in the worklist | |
1116 | // as roots. | |
1117 | bool | |
ca09d69a | 1118 | is_section_name_included(const char* name); |
6d03d481 ST |
1119 | |
1120 | void | |
1121 | copy_symbols_data(Symbols_data* gc_sd, Read_symbols_data* sd, | |
1122 | unsigned int section_header_size); | |
1123 | ||
1124 | void | |
1125 | set_symbols_data(Symbols_data* sd) | |
1126 | { this->sd_ = sd; } | |
1127 | ||
1128 | // During garbage collection, the Read_relocs pass for all objects | |
1129 | // is done before scanning the relocs. In that case, this->rd_ is | |
1130 | // used to store the information from Read_relocs for each object. | |
1131 | // This data is also used to compute the list of relevant sections. | |
1132 | Read_relocs_data* | |
1133 | get_relocs_data() | |
1134 | { return this->rd_; } | |
1135 | ||
1136 | void | |
1137 | set_relocs_data(Read_relocs_data* rd) | |
1138 | { this->rd_ = rd; } | |
1139 | ||
1140 | virtual bool | |
1141 | is_output_section_offset_invalid(unsigned int shndx) const = 0; | |
1142 | ||
92e059d8 | 1143 | // Read the relocs. |
a2fb1b05 | 1144 | void |
92e059d8 ILT |
1145 | read_relocs(Read_relocs_data* rd) |
1146 | { return this->do_read_relocs(rd); } | |
1147 | ||
6d03d481 ST |
1148 | // Process the relocs, during garbage collection only. |
1149 | void | |
2ea97941 ILT |
1150 | gc_process_relocs(Symbol_table* symtab, Layout* layout, Read_relocs_data* rd) |
1151 | { return this->do_gc_process_relocs(symtab, layout, rd); } | |
6d03d481 | 1152 | |
92e059d8 ILT |
1153 | // Scan the relocs and adjust the symbol table. |
1154 | void | |
2ea97941 ILT |
1155 | scan_relocs(Symbol_table* symtab, Layout* layout, Read_relocs_data* rd) |
1156 | { return this->do_scan_relocs(symtab, layout, rd); } | |
a2fb1b05 | 1157 | |
83896202 ILT |
1158 | // Return the value of the local symbol whose index is SYMNDX, plus |
1159 | // ADDEND. ADDEND is passed in so that we can correctly handle the | |
1160 | // section symbol for a merge section. | |
1161 | uint64_t | |
1162 | local_symbol_value(unsigned int symndx, uint64_t addend) const | |
1163 | { return this->do_local_symbol_value(symndx, addend); } | |
1164 | ||
1165 | // Return the PLT offset for a local symbol. It is an error to call | |
1166 | // this if it doesn't have one. | |
1167 | unsigned int | |
1168 | local_plt_offset(unsigned int symndx) const | |
1169 | { return this->do_local_plt_offset(symndx); } | |
1170 | ||
1171 | // Return whether the local symbol SYMNDX has a GOT offset of type | |
1172 | // GOT_TYPE. | |
1173 | bool | |
1174 | local_has_got_offset(unsigned int symndx, unsigned int got_type) const | |
7ef8ae7c VR |
1175 | { return this->do_local_has_got_offset(symndx, got_type, 0); } |
1176 | ||
1177 | // Return whether the local symbol SYMNDX plus ADDEND has a GOT offset | |
1178 | // of type GOT_TYPE. | |
1179 | bool | |
1180 | local_has_got_offset(unsigned int symndx, unsigned int got_type, | |
1181 | uint64_t addend) const | |
1182 | { return this->do_local_has_got_offset(symndx, got_type, addend); } | |
83896202 ILT |
1183 | |
1184 | // Return the GOT offset of type GOT_TYPE of the local symbol | |
1185 | // SYMNDX. It is an error to call this if the symbol does not have | |
1186 | // a GOT offset of the specified type. | |
1187 | unsigned int | |
1188 | local_got_offset(unsigned int symndx, unsigned int got_type) const | |
7ef8ae7c VR |
1189 | { return this->do_local_got_offset(symndx, got_type, 0); } |
1190 | ||
1191 | // Return the GOT offset of type GOT_TYPE of the local symbol | |
1192 | // SYMNDX plus ADDEND. It is an error to call this if the symbol | |
1193 | // does not have a GOT offset of the specified type. | |
1194 | unsigned int | |
1195 | local_got_offset(unsigned int symndx, unsigned int got_type, | |
1196 | uint64_t addend) const | |
1197 | { return this->do_local_got_offset(symndx, got_type, addend); } | |
83896202 ILT |
1198 | |
1199 | // Set the GOT offset with type GOT_TYPE of the local symbol SYMNDX | |
1200 | // to GOT_OFFSET. | |
1201 | void | |
1202 | set_local_got_offset(unsigned int symndx, unsigned int got_type, | |
1203 | unsigned int got_offset) | |
7ef8ae7c VR |
1204 | { this->do_set_local_got_offset(symndx, got_type, got_offset, 0); } |
1205 | ||
1206 | // Set the GOT offset with type GOT_TYPE of the local symbol SYMNDX | |
1207 | // plus ADDEND to GOT_OFFSET. | |
1208 | void | |
1209 | set_local_got_offset(unsigned int symndx, unsigned int got_type, | |
1210 | unsigned int got_offset, uint64_t addend) | |
1211 | { this->do_set_local_got_offset(symndx, got_type, got_offset, addend); } | |
83896202 | 1212 | |
d77a0555 ILT |
1213 | // Return whether the local symbol SYMNDX is a TLS symbol. |
1214 | bool | |
1215 | local_is_tls(unsigned int symndx) const | |
1216 | { return this->do_local_is_tls(symndx); } | |
1217 | ||
6d013333 ILT |
1218 | // The number of local symbols in the input symbol table. |
1219 | virtual unsigned int | |
1220 | local_symbol_count() const | |
1221 | { return this->do_local_symbol_count(); } | |
1222 | ||
f0f9babf CC |
1223 | // The number of local symbols in the output symbol table. |
1224 | virtual unsigned int | |
1225 | output_local_symbol_count() const | |
1226 | { return this->do_output_local_symbol_count(); } | |
1227 | ||
1228 | // The file offset for local symbols in the output symbol table. | |
1229 | virtual off_t | |
1230 | local_symbol_offset() const | |
1231 | { return this->do_local_symbol_offset(); } | |
1232 | ||
7bf1f802 ILT |
1233 | // Initial local symbol processing: count the number of local symbols |
1234 | // in the output symbol table and dynamic symbol table; add local symbol | |
1235 | // names to *POOL and *DYNPOOL. | |
1236 | void | |
1237 | count_local_symbols(Stringpool_template<char>* pool, | |
1238 | Stringpool_template<char>* dynpool) | |
1239 | { return this->do_count_local_symbols(pool, dynpool); } | |
1240 | ||
1241 | // Set the values of the local symbols, set the output symbol table | |
1242 | // indexes for the local variables, and set the offset where local | |
1243 | // symbol information will be stored. Returns the new local symbol index. | |
1244 | unsigned int | |
ef15dade ST |
1245 | finalize_local_symbols(unsigned int index, off_t off, Symbol_table* symtab) |
1246 | { return this->do_finalize_local_symbols(index, off, symtab); } | |
7bf1f802 ILT |
1247 | |
1248 | // Set the output dynamic symbol table indexes for the local variables. | |
c06b7b0b | 1249 | unsigned int |
7bf1f802 ILT |
1250 | set_local_dynsym_indexes(unsigned int index) |
1251 | { return this->do_set_local_dynsym_indexes(index); } | |
1252 | ||
1253 | // Set the offset where local dynamic symbol information will be stored. | |
1254 | unsigned int | |
1255 | set_local_dynsym_offset(off_t off) | |
1256 | { return this->do_set_local_dynsym_offset(off); } | |
75f65a3e | 1257 | |
6fa2a40b CC |
1258 | // Record a dynamic relocation against an input section from this object. |
1259 | void | |
1260 | add_dyn_reloc(unsigned int index) | |
1261 | { | |
1262 | if (this->dyn_reloc_count_ == 0) | |
1263 | this->first_dyn_reloc_ = index; | |
1264 | ++this->dyn_reloc_count_; | |
1265 | } | |
1266 | ||
1267 | // Return the index of the first dynamic relocation. | |
1268 | unsigned int | |
1269 | first_dyn_reloc() const | |
1270 | { return this->first_dyn_reloc_; } | |
1271 | ||
1272 | // Return the count of dynamic relocations. | |
1273 | unsigned int | |
1274 | dyn_reloc_count() const | |
1275 | { return this->dyn_reloc_count_; } | |
1276 | ||
61ba1cf9 ILT |
1277 | // Relocate the input sections and write out the local symbols. |
1278 | void | |
2ea97941 ILT |
1279 | relocate(const Symbol_table* symtab, const Layout* layout, Output_file* of) |
1280 | { return this->do_relocate(symtab, layout, of); } | |
61ba1cf9 | 1281 | |
a783673b ILT |
1282 | // Return whether an input section is being included in the link. |
1283 | bool | |
5a6f7e2d | 1284 | is_section_included(unsigned int shndx) const |
a783673b | 1285 | { |
ef9beddf ILT |
1286 | gold_assert(shndx < this->output_sections_.size()); |
1287 | return this->output_sections_[shndx] != NULL; | |
a783673b ILT |
1288 | } |
1289 | ||
9aff4b7a | 1290 | // The output section of the input section with index SHNDX. |
2b328d4e DK |
1291 | // This is only used currently to remove a section from the link in |
1292 | // relaxation. | |
1293 | void | |
1294 | set_output_section(unsigned int shndx, Output_section* os) | |
1295 | { | |
1296 | gold_assert(shndx < this->output_sections_.size()); | |
1297 | this->output_sections_[shndx] = os; | |
1298 | } | |
1299 | ||
ead1e424 | 1300 | // Set the offset of an input section within its output section. |
5995b570 | 1301 | void |
ef9beddf ILT |
1302 | set_section_offset(unsigned int shndx, uint64_t off) |
1303 | { this->do_set_section_offset(shndx, off); } | |
e94cf127 | 1304 | |
730cdc88 ILT |
1305 | // Return true if we need to wait for output sections to be written |
1306 | // before we can apply relocations. This is true if the object has | |
1307 | // any relocations for sections which require special handling, such | |
1308 | // as the exception frame section. | |
1309 | bool | |
17a1d0a9 | 1310 | relocs_must_follow_section_writes() const |
730cdc88 ILT |
1311 | { return this->relocs_must_follow_section_writes_; } |
1312 | ||
0916f9e7 RÁE |
1313 | Object_merge_map* |
1314 | get_or_create_merge_map(); | |
1315 | ||
dbe40a88 RÁE |
1316 | template<int size> |
1317 | void | |
1318 | initialize_input_to_output_map(unsigned int shndx, | |
1319 | typename elfcpp::Elf_types<size>::Elf_Addr starting_address, | |
1320 | Unordered_map<section_offset_type, | |
1321 | typename elfcpp::Elf_types<size>::Elf_Addr>* output_address) const; | |
4625f782 | 1322 | |
4625f782 | 1323 | void |
dbe40a88 RÁE |
1324 | add_merge_mapping(Output_section_data *output_data, |
1325 | unsigned int shndx, section_offset_type offset, | |
1326 | section_size_type length, | |
1327 | section_offset_type output_offset); | |
1328 | ||
1329 | bool | |
1330 | merge_output_offset(unsigned int shndx, section_offset_type offset, | |
1331 | section_offset_type *poutput) const; | |
1332 | ||
67f95b96 RÁE |
1333 | const Output_section_data* |
1334 | find_merge_section(unsigned int shndx) const; | |
4625f782 | 1335 | |
6a74a719 ILT |
1336 | // Record the relocatable reloc info for an input reloc section. |
1337 | void | |
1338 | set_relocatable_relocs(unsigned int reloc_shndx, Relocatable_relocs* rr) | |
1339 | { | |
1340 | gold_assert(reloc_shndx < this->shnum()); | |
1341 | (*this->map_to_relocatable_relocs_)[reloc_shndx] = rr; | |
1342 | } | |
1343 | ||
1344 | // Get the relocatable reloc info for an input reloc section. | |
1345 | Relocatable_relocs* | |
1346 | relocatable_relocs(unsigned int reloc_shndx) | |
1347 | { | |
1348 | gold_assert(reloc_shndx < this->shnum()); | |
1349 | return (*this->map_to_relocatable_relocs_)[reloc_shndx]; | |
1350 | } | |
1351 | ||
5995b570 CC |
1352 | // Layout sections whose layout was deferred while waiting for |
1353 | // input files from a plugin. | |
1354 | void | |
2ea97941 ILT |
1355 | layout_deferred_sections(Layout* layout) |
1356 | { this->do_layout_deferred_sections(layout); } | |
5995b570 | 1357 | |
09ec0418 CC |
1358 | // Return the index of the first incremental relocation for symbol SYMNDX. |
1359 | virtual unsigned int | |
1360 | do_get_incremental_reloc_base(unsigned int symndx) const | |
1361 | { return this->reloc_bases_[symndx]; } | |
1362 | ||
1363 | // Return the number of incremental relocations for symbol SYMNDX. | |
1364 | virtual unsigned int | |
1365 | do_get_incremental_reloc_count(unsigned int symndx) const | |
1366 | { return this->reloc_counts_[symndx]; } | |
1367 | ||
9fc236f3 CC |
1368 | // Return the word size of the object file. |
1369 | int | |
1370 | elfsize() const | |
1371 | { return this->do_elfsize(); } | |
1372 | ||
1373 | // Return TRUE if this is a big-endian object file. | |
1374 | bool | |
1375 | is_big_endian() const | |
1376 | { return this->do_is_big_endian(); } | |
1377 | ||
a783673b | 1378 | protected: |
ef9beddf ILT |
1379 | // The output section to be used for each input section, indexed by |
1380 | // the input section number. The output section is NULL if the | |
1381 | // input section is to be discarded. | |
1382 | typedef std::vector<Output_section*> Output_sections; | |
e94cf127 | 1383 | |
92e059d8 ILT |
1384 | // Read the relocs--implemented by child class. |
1385 | virtual void | |
1386 | do_read_relocs(Read_relocs_data*) = 0; | |
1387 | ||
6d03d481 ST |
1388 | // Process the relocs--implemented by child class. |
1389 | virtual void | |
ad0f2072 | 1390 | do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*) = 0; |
6d03d481 | 1391 | |
92e059d8 ILT |
1392 | // Scan the relocs--implemented by child class. |
1393 | virtual void | |
ad0f2072 | 1394 | do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*) = 0; |
92e059d8 | 1395 | |
83896202 ILT |
1396 | // Return the value of a local symbol. |
1397 | virtual uint64_t | |
1398 | do_local_symbol_value(unsigned int symndx, uint64_t addend) const = 0; | |
1399 | ||
1400 | // Return the PLT offset of a local symbol. | |
1401 | virtual unsigned int | |
1402 | do_local_plt_offset(unsigned int symndx) const = 0; | |
1403 | ||
7ef8ae7c VR |
1404 | // Return whether a local symbol plus addend has a GOT offset |
1405 | // of a given type. | |
83896202 ILT |
1406 | virtual bool |
1407 | do_local_has_got_offset(unsigned int symndx, | |
7ef8ae7c | 1408 | unsigned int got_type, uint64_t addend) const = 0; |
83896202 | 1409 | |
7ef8ae7c | 1410 | // Return the GOT offset of a given type of a local symbol plus addend. |
83896202 | 1411 | virtual unsigned int |
7ef8ae7c VR |
1412 | do_local_got_offset(unsigned int symndx, unsigned int got_type, |
1413 | uint64_t addend) const = 0; | |
83896202 | 1414 | |
7ef8ae7c | 1415 | // Set the GOT offset with a given type for a local symbol plus addend. |
83896202 ILT |
1416 | virtual void |
1417 | do_set_local_got_offset(unsigned int symndx, unsigned int got_type, | |
7ef8ae7c | 1418 | unsigned int got_offset, uint64_t addend) = 0; |
83896202 | 1419 | |
d77a0555 ILT |
1420 | // Return whether local symbol SYMNDX is a TLS symbol. |
1421 | virtual bool | |
1422 | do_local_is_tls(unsigned int symndx) const = 0; | |
1423 | ||
6d013333 ILT |
1424 | // Return the number of local symbols--implemented by child class. |
1425 | virtual unsigned int | |
1426 | do_local_symbol_count() const = 0; | |
1427 | ||
f0f9babf CC |
1428 | // Return the number of output local symbols--implemented by child class. |
1429 | virtual unsigned int | |
1430 | do_output_local_symbol_count() const = 0; | |
1431 | ||
1432 | // Return the file offset for local symbols--implemented by child class. | |
1433 | virtual off_t | |
1434 | do_local_symbol_offset() const = 0; | |
1435 | ||
7bf1f802 ILT |
1436 | // Count local symbols--implemented by child class. |
1437 | virtual void | |
1438 | do_count_local_symbols(Stringpool_template<char>*, | |
6a74a719 | 1439 | Stringpool_template<char>*) = 0; |
75f65a3e | 1440 | |
6a74a719 ILT |
1441 | // Finalize the local symbols. Set the output symbol table indexes |
1442 | // for the local variables, and set the offset where local symbol | |
1443 | // information will be stored. | |
7bf1f802 | 1444 | virtual unsigned int |
ef15dade | 1445 | do_finalize_local_symbols(unsigned int, off_t, Symbol_table*) = 0; |
7bf1f802 ILT |
1446 | |
1447 | // Set the output dynamic symbol table indexes for the local variables. | |
1448 | virtual unsigned int | |
1449 | do_set_local_dynsym_indexes(unsigned int) = 0; | |
1450 | ||
1451 | // Set the offset where local dynamic symbol information will be stored. | |
1452 | virtual unsigned int | |
1453 | do_set_local_dynsym_offset(off_t) = 0; | |
1454 | ||
61ba1cf9 ILT |
1455 | // Relocate the input sections and write out the local |
1456 | // symbols--implemented by child class. | |
1457 | virtual void | |
ad0f2072 | 1458 | do_relocate(const Symbol_table* symtab, const Layout*, Output_file* of) = 0; |
92e059d8 | 1459 | |
ef9beddf ILT |
1460 | // Set the offset of a section--implemented by child class. |
1461 | virtual void | |
1462 | do_set_section_offset(unsigned int shndx, uint64_t off) = 0; | |
e94cf127 | 1463 | |
5995b570 CC |
1464 | // Layout sections whose layout was deferred while waiting for |
1465 | // input files from a plugin--implemented by child class. | |
1466 | virtual void | |
1467 | do_layout_deferred_sections(Layout*) = 0; | |
1468 | ||
09ec0418 CC |
1469 | // Given a section index, return the corresponding Output_section. |
1470 | // The return value will be NULL if the section is not included in | |
1471 | // the link. | |
1472 | Output_section* | |
1473 | do_output_section(unsigned int shndx) const | |
1474 | { | |
1475 | gold_assert(shndx < this->output_sections_.size()); | |
1476 | return this->output_sections_[shndx]; | |
1477 | } | |
1478 | ||
ef9beddf ILT |
1479 | // Return the vector mapping input sections to output sections. |
1480 | Output_sections& | |
1481 | output_sections() | |
1482 | { return this->output_sections_; } | |
e94cf127 | 1483 | |
ef9beddf ILT |
1484 | const Output_sections& |
1485 | output_sections() const | |
1486 | { return this->output_sections_; } | |
e94cf127 | 1487 | |
6a74a719 ILT |
1488 | // Set the size of the relocatable relocs array. |
1489 | void | |
1490 | size_relocatable_relocs() | |
1491 | { | |
1492 | this->map_to_relocatable_relocs_ = | |
1493 | new std::vector<Relocatable_relocs*>(this->shnum()); | |
1494 | } | |
1495 | ||
730cdc88 ILT |
1496 | // Record that we must wait for the output sections to be written |
1497 | // before applying relocations. | |
1498 | void | |
1499 | set_relocs_must_follow_section_writes() | |
1500 | { this->relocs_must_follow_section_writes_ = true; } | |
1501 | ||
09ec0418 CC |
1502 | // Allocate the array for counting incremental relocations. |
1503 | void | |
1504 | allocate_incremental_reloc_counts() | |
1505 | { | |
1506 | unsigned int nsyms = this->do_get_global_symbols()->size(); | |
1507 | this->reloc_counts_ = new unsigned int[nsyms]; | |
1508 | gold_assert(this->reloc_counts_ != NULL); | |
1509 | memset(this->reloc_counts_, 0, nsyms * sizeof(unsigned int)); | |
1510 | } | |
1511 | ||
1512 | // Record a relocation in this object referencing global symbol SYMNDX. | |
1513 | // Used for tracking incremental link information. | |
1514 | void | |
1515 | count_incremental_reloc(unsigned int symndx) | |
1516 | { | |
1517 | unsigned int nsyms = this->do_get_global_symbols()->size(); | |
1518 | gold_assert(symndx < nsyms); | |
1519 | gold_assert(this->reloc_counts_ != NULL); | |
1520 | ++this->reloc_counts_[symndx]; | |
1521 | } | |
1522 | ||
1523 | // Finalize the incremental relocation information. | |
1524 | void | |
cdc29364 | 1525 | finalize_incremental_relocs(Layout* layout, bool clear_counts); |
09ec0418 CC |
1526 | |
1527 | // Return the index of the next relocation to be written for global symbol | |
1528 | // SYMNDX. Only valid after finalize_incremental_relocs() has been called. | |
1529 | unsigned int | |
1530 | next_incremental_reloc_index(unsigned int symndx) | |
1531 | { | |
1532 | unsigned int nsyms = this->do_get_global_symbols()->size(); | |
1533 | ||
1534 | gold_assert(this->reloc_counts_ != NULL); | |
1535 | gold_assert(this->reloc_bases_ != NULL); | |
1536 | gold_assert(symndx < nsyms); | |
1537 | ||
1538 | unsigned int counter = this->reloc_counts_[symndx]++; | |
1539 | return this->reloc_bases_[symndx] + counter; | |
1540 | } | |
1541 | ||
9fc236f3 CC |
1542 | // Return the word size of the object file-- |
1543 | // implemented by child class. | |
1544 | virtual int | |
1545 | do_elfsize() const = 0; | |
1546 | ||
1547 | // Return TRUE if this is a big-endian object file-- | |
1548 | // implemented by child class. | |
1549 | virtual bool | |
1550 | do_is_big_endian() const = 0; | |
1551 | ||
bae7f79e | 1552 | private: |
a2fb1b05 | 1553 | // Mapping from input sections to output section. |
ef9beddf | 1554 | Output_sections output_sections_; |
6a74a719 ILT |
1555 | // Mapping from input section index to the information recorded for |
1556 | // the relocations. This is only used for a relocatable link. | |
1557 | std::vector<Relocatable_relocs*>* map_to_relocatable_relocs_; | |
4625f782 ILT |
1558 | // Mappings for merge sections. This is managed by the code in the |
1559 | // Merge_map class. | |
1560 | Object_merge_map* object_merge_map_; | |
730cdc88 ILT |
1561 | // Whether we need to wait for output sections to be written before |
1562 | // we can apply relocations. | |
1563 | bool relocs_must_follow_section_writes_; | |
6d03d481 ST |
1564 | // Used to store the relocs data computed by the Read_relocs pass. |
1565 | // Used during garbage collection of unused sections. | |
1566 | Read_relocs_data* rd_; | |
1567 | // Used to store the symbols data computed by the Read_symbols pass. | |
1568 | // Again used during garbage collection when laying out referenced | |
1569 | // sections. | |
ca09d69a | 1570 | gold::Symbols_data* sd_; |
09ec0418 CC |
1571 | // Per-symbol counts of relocations, for incremental links. |
1572 | unsigned int* reloc_counts_; | |
1573 | // Per-symbol base indexes of relocations, for incremental links. | |
1574 | unsigned int* reloc_bases_; | |
6fa2a40b CC |
1575 | // Index of the first dynamic relocation for this object. |
1576 | unsigned int first_dyn_reloc_; | |
1577 | // Count of dynamic relocations for this object. | |
1578 | unsigned int dyn_reloc_count_; | |
bae7f79e ILT |
1579 | }; |
1580 | ||
a9a60db6 ILT |
1581 | // This class is used to handle relocations against a section symbol |
1582 | // in an SHF_MERGE section. For such a symbol, we need to know the | |
1583 | // addend of the relocation before we can determine the final value. | |
1584 | // The addend gives us the location in the input section, and we can | |
1585 | // determine how it is mapped to the output section. For a | |
1586 | // non-section symbol, we apply the addend to the final value of the | |
1587 | // symbol; that is done in finalize_local_symbols, and does not use | |
1588 | // this class. | |
1589 | ||
1590 | template<int size> | |
1591 | class Merged_symbol_value | |
1592 | { | |
1593 | public: | |
1594 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Value; | |
1595 | ||
1596 | // We use a hash table to map offsets in the input section to output | |
1597 | // addresses. | |
1598 | typedef Unordered_map<section_offset_type, Value> Output_addresses; | |
1599 | ||
1600 | Merged_symbol_value(Value input_value, Value output_start_address) | |
1601 | : input_value_(input_value), output_start_address_(output_start_address), | |
1602 | output_addresses_() | |
1603 | { } | |
1604 | ||
1605 | // Initialize the hash table. | |
1606 | void | |
1607 | initialize_input_to_output_map(const Relobj*, unsigned int input_shndx); | |
1608 | ||
1609 | // Release the hash table to save space. | |
1610 | void | |
1611 | free_input_to_output_map() | |
1612 | { this->output_addresses_.clear(); } | |
1613 | ||
1614 | // Get the output value corresponding to an addend. The object and | |
1615 | // input section index are passed in because the caller will have | |
1616 | // them; otherwise we could store them here. | |
1617 | Value | |
1618 | value(const Relobj* object, unsigned int input_shndx, Value addend) const | |
1619 | { | |
7f649c59 ILT |
1620 | // This is a relocation against a section symbol. ADDEND is the |
1621 | // offset in the section. The result should be the start of some | |
1622 | // merge area. If the object file wants something else, it should | |
1623 | // use a regular symbol rather than a section symbol. | |
1624 | // Unfortunately, PR 6658 shows a case in which the object file | |
1625 | // refers to the section symbol, but uses a negative ADDEND to | |
1626 | // compensate for a PC relative reloc. We can't handle the | |
1627 | // general case. However, we can handle the special case of a | |
1628 | // negative addend, by assuming that it refers to the start of the | |
1629 | // section. Of course, that means that we have to guess when | |
1630 | // ADDEND is negative. It is normal to see a 32-bit value here | |
1631 | // even when the template parameter size is 64, as 64-bit object | |
1632 | // file formats have 32-bit relocations. We know this is a merge | |
1633 | // section, so we know it has to fit into memory. So we assume | |
1634 | // that we won't see a value larger than a large 32-bit unsigned | |
1635 | // value. This will break objects with very very large merge | |
1636 | // sections; they probably break in other ways anyhow. | |
1637 | Value input_offset = this->input_value_; | |
1638 | if (addend < 0xffffff00) | |
1639 | { | |
1640 | input_offset += addend; | |
1641 | addend = 0; | |
1642 | } | |
a9a60db6 ILT |
1643 | typename Output_addresses::const_iterator p = |
1644 | this->output_addresses_.find(input_offset); | |
1645 | if (p != this->output_addresses_.end()) | |
7f649c59 | 1646 | return p->second + addend; |
a9a60db6 | 1647 | |
7f649c59 ILT |
1648 | return (this->value_from_output_section(object, input_shndx, input_offset) |
1649 | + addend); | |
a9a60db6 ILT |
1650 | } |
1651 | ||
1652 | private: | |
1653 | // Get the output value for an input offset if we couldn't find it | |
1654 | // in the hash table. | |
1655 | Value | |
1656 | value_from_output_section(const Relobj*, unsigned int input_shndx, | |
1657 | Value input_offset) const; | |
1658 | ||
1659 | // The value of the section symbol in the input file. This is | |
1660 | // normally zero, but could in principle be something else. | |
1661 | Value input_value_; | |
1662 | // The start address of this merged section in the output file. | |
1663 | Value output_start_address_; | |
1664 | // A hash table which maps offsets in the input section to output | |
1665 | // addresses. This only maps specific offsets, not all offsets. | |
1666 | Output_addresses output_addresses_; | |
1667 | }; | |
1668 | ||
b8e6aad9 ILT |
1669 | // This POD class is holds the value of a symbol. This is used for |
1670 | // local symbols, and for all symbols during relocation processing. | |
730cdc88 ILT |
1671 | // For special sections, such as SHF_MERGE sections, this calls a |
1672 | // function to get the final symbol value. | |
b8e6aad9 ILT |
1673 | |
1674 | template<int size> | |
1675 | class Symbol_value | |
1676 | { | |
1677 | public: | |
1678 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Value; | |
1679 | ||
1680 | Symbol_value() | |
7bf1f802 | 1681 | : output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0), |
d491d34e | 1682 | is_ordinary_shndx_(false), is_section_symbol_(false), |
7223e9ca | 1683 | is_tls_symbol_(false), is_ifunc_symbol_(false), has_output_value_(true) |
a9a60db6 | 1684 | { this->u_.value = 0; } |
b8e6aad9 | 1685 | |
aa98ff75 DK |
1686 | ~Symbol_value() |
1687 | { | |
1688 | if (!this->has_output_value_) | |
1689 | delete this->u_.merged_symbol_value; | |
1690 | } | |
1691 | ||
b8e6aad9 ILT |
1692 | // Get the value of this symbol. OBJECT is the object in which this |
1693 | // symbol is defined, and ADDEND is an addend to add to the value. | |
1694 | template<bool big_endian> | |
1695 | Value | |
6fa2a40b | 1696 | value(const Sized_relobj_file<size, big_endian>* object, Value addend) const |
b8e6aad9 | 1697 | { |
a9a60db6 ILT |
1698 | if (this->has_output_value_) |
1699 | return this->u_.value + addend; | |
1700 | else | |
d491d34e ILT |
1701 | { |
1702 | gold_assert(this->is_ordinary_shndx_); | |
1703 | return this->u_.merged_symbol_value->value(object, this->input_shndx_, | |
1704 | addend); | |
1705 | } | |
b8e6aad9 ILT |
1706 | } |
1707 | ||
1708 | // Set the value of this symbol in the output symbol table. | |
1709 | void | |
2ea97941 ILT |
1710 | set_output_value(Value value) |
1711 | { this->u_.value = value; } | |
a9a60db6 ILT |
1712 | |
1713 | // For a section symbol in a merged section, we need more | |
1714 | // information. | |
1715 | void | |
1716 | set_merged_symbol_value(Merged_symbol_value<size>* msv) | |
b8e6aad9 | 1717 | { |
a9a60db6 ILT |
1718 | gold_assert(this->is_section_symbol_); |
1719 | this->has_output_value_ = false; | |
1720 | this->u_.merged_symbol_value = msv; | |
b8e6aad9 ILT |
1721 | } |
1722 | ||
a9a60db6 ILT |
1723 | // Initialize the input to output map for a section symbol in a |
1724 | // merged section. We also initialize the value of a non-section | |
1725 | // symbol in a merged section. | |
b8e6aad9 | 1726 | void |
a9a60db6 | 1727 | initialize_input_to_output_map(const Relobj* object) |
b8e6aad9 | 1728 | { |
a9a60db6 ILT |
1729 | if (!this->has_output_value_) |
1730 | { | |
d491d34e | 1731 | gold_assert(this->is_section_symbol_ && this->is_ordinary_shndx_); |
a9a60db6 ILT |
1732 | Merged_symbol_value<size>* msv = this->u_.merged_symbol_value; |
1733 | msv->initialize_input_to_output_map(object, this->input_shndx_); | |
1734 | } | |
b8e6aad9 ILT |
1735 | } |
1736 | ||
a9a60db6 ILT |
1737 | // Free the input to output map for a section symbol in a merged |
1738 | // section. | |
1739 | void | |
1740 | free_input_to_output_map() | |
7bf1f802 | 1741 | { |
a9a60db6 ILT |
1742 | if (!this->has_output_value_) |
1743 | this->u_.merged_symbol_value->free_input_to_output_map(); | |
7bf1f802 ILT |
1744 | } |
1745 | ||
a9a60db6 ILT |
1746 | // Set the value of the symbol from the input file. This is only |
1747 | // called by count_local_symbols, to communicate the value to | |
1748 | // finalize_local_symbols. | |
1749 | void | |
2ea97941 ILT |
1750 | set_input_value(Value value) |
1751 | { this->u_.value = value; } | |
a9a60db6 ILT |
1752 | |
1753 | // Return the input value. This is only called by | |
e94cf127 | 1754 | // finalize_local_symbols and (in special cases) relocate_section. |
a9a60db6 ILT |
1755 | Value |
1756 | input_value() const | |
1757 | { return this->u_.value; } | |
1758 | ||
d3bbad62 ILT |
1759 | // Return whether we have set the index in the output symbol table |
1760 | // yet. | |
1761 | bool | |
1762 | is_output_symtab_index_set() const | |
1763 | { | |
1764 | return (this->output_symtab_index_ != 0 | |
1765 | && this->output_symtab_index_ != -2U); | |
1766 | } | |
1767 | ||
1768 | // Return whether this symbol may be discarded from the normal | |
1769 | // symbol table. | |
1770 | bool | |
1771 | may_be_discarded_from_output_symtab() const | |
1772 | { | |
1773 | gold_assert(!this->is_output_symtab_index_set()); | |
1774 | return this->output_symtab_index_ != -2U; | |
1775 | } | |
1776 | ||
1777 | // Return whether this symbol has an entry in the output symbol | |
b8e6aad9 ILT |
1778 | // table. |
1779 | bool | |
d3bbad62 ILT |
1780 | has_output_symtab_entry() const |
1781 | { | |
1782 | gold_assert(this->is_output_symtab_index_set()); | |
1783 | return this->output_symtab_index_ != -1U; | |
1784 | } | |
b8e6aad9 ILT |
1785 | |
1786 | // Return the index in the output symbol table. | |
1787 | unsigned int | |
1788 | output_symtab_index() const | |
1789 | { | |
d3bbad62 ILT |
1790 | gold_assert(this->is_output_symtab_index_set() |
1791 | && this->output_symtab_index_ != -1U); | |
b8e6aad9 ILT |
1792 | return this->output_symtab_index_; |
1793 | } | |
1794 | ||
1795 | // Set the index in the output symbol table. | |
1796 | void | |
1797 | set_output_symtab_index(unsigned int i) | |
1798 | { | |
d3bbad62 ILT |
1799 | gold_assert(!this->is_output_symtab_index_set()); |
1800 | gold_assert(i != 0 && i != -1U && i != -2U); | |
b8e6aad9 ILT |
1801 | this->output_symtab_index_ = i; |
1802 | } | |
1803 | ||
1804 | // Record that this symbol should not go into the output symbol | |
1805 | // table. | |
1806 | void | |
1807 | set_no_output_symtab_entry() | |
1808 | { | |
1809 | gold_assert(this->output_symtab_index_ == 0); | |
1810 | this->output_symtab_index_ = -1U; | |
1811 | } | |
1812 | ||
d3bbad62 ILT |
1813 | // Record that this symbol must go into the output symbol table, |
1814 | // because it there is a relocation that uses it. | |
1815 | void | |
1816 | set_must_have_output_symtab_entry() | |
1817 | { | |
1818 | gold_assert(!this->is_output_symtab_index_set()); | |
1819 | this->output_symtab_index_ = -2U; | |
1820 | } | |
1821 | ||
7bf1f802 ILT |
1822 | // Set the index in the output dynamic symbol table. |
1823 | void | |
1824 | set_needs_output_dynsym_entry() | |
1825 | { | |
dceae3c1 | 1826 | gold_assert(!this->is_section_symbol()); |
7bf1f802 ILT |
1827 | this->output_dynsym_index_ = 0; |
1828 | } | |
1829 | ||
d3bbad62 | 1830 | // Return whether this symbol should go into the dynamic symbol |
7bf1f802 ILT |
1831 | // table. |
1832 | bool | |
1833 | needs_output_dynsym_entry() const | |
1834 | { | |
1835 | return this->output_dynsym_index_ != -1U; | |
1836 | } | |
1837 | ||
d3bbad62 ILT |
1838 | // Return whether this symbol has an entry in the dynamic symbol |
1839 | // table. | |
1840 | bool | |
1841 | has_output_dynsym_entry() const | |
1842 | { | |
1843 | gold_assert(this->output_dynsym_index_ != 0); | |
1844 | return this->output_dynsym_index_ != -1U; | |
1845 | } | |
1846 | ||
7bf1f802 ILT |
1847 | // Record that this symbol should go into the dynamic symbol table. |
1848 | void | |
1849 | set_output_dynsym_index(unsigned int i) | |
1850 | { | |
1851 | gold_assert(this->output_dynsym_index_ == 0); | |
d3bbad62 | 1852 | gold_assert(i != 0 && i != -1U); |
7bf1f802 ILT |
1853 | this->output_dynsym_index_ = i; |
1854 | } | |
1855 | ||
1856 | // Return the index in the output dynamic symbol table. | |
1857 | unsigned int | |
1858 | output_dynsym_index() const | |
1859 | { | |
dceae3c1 ILT |
1860 | gold_assert(this->output_dynsym_index_ != 0 |
1861 | && this->output_dynsym_index_ != -1U); | |
7bf1f802 ILT |
1862 | return this->output_dynsym_index_; |
1863 | } | |
1864 | ||
b8e6aad9 ILT |
1865 | // Set the index of the input section in the input file. |
1866 | void | |
d491d34e | 1867 | set_input_shndx(unsigned int i, bool is_ordinary) |
730cdc88 ILT |
1868 | { |
1869 | this->input_shndx_ = i; | |
ac1f0c21 ILT |
1870 | // input_shndx_ field is a bitfield, so make sure that the value |
1871 | // fits. | |
730cdc88 | 1872 | gold_assert(this->input_shndx_ == i); |
d491d34e | 1873 | this->is_ordinary_shndx_ = is_ordinary; |
730cdc88 | 1874 | } |
b8e6aad9 | 1875 | |
7bf1f802 ILT |
1876 | // Return the index of the input section in the input file. |
1877 | unsigned int | |
d491d34e ILT |
1878 | input_shndx(bool* is_ordinary) const |
1879 | { | |
1880 | *is_ordinary = this->is_ordinary_shndx_; | |
1881 | return this->input_shndx_; | |
1882 | } | |
7bf1f802 | 1883 | |
a9a60db6 ILT |
1884 | // Whether this is a section symbol. |
1885 | bool | |
1886 | is_section_symbol() const | |
1887 | { return this->is_section_symbol_; } | |
1888 | ||
063f12a8 ILT |
1889 | // Record that this is a section symbol. |
1890 | void | |
1891 | set_is_section_symbol() | |
dceae3c1 ILT |
1892 | { |
1893 | gold_assert(!this->needs_output_dynsym_entry()); | |
1894 | this->is_section_symbol_ = true; | |
1895 | } | |
063f12a8 | 1896 | |
7bf1f802 ILT |
1897 | // Record that this is a TLS symbol. |
1898 | void | |
1899 | set_is_tls_symbol() | |
1900 | { this->is_tls_symbol_ = true; } | |
1901 | ||
7223e9ca | 1902 | // Return true if this is a TLS symbol. |
7bf1f802 ILT |
1903 | bool |
1904 | is_tls_symbol() const | |
1905 | { return this->is_tls_symbol_; } | |
1906 | ||
7223e9ca ILT |
1907 | // Record that this is an IFUNC symbol. |
1908 | void | |
1909 | set_is_ifunc_symbol() | |
1910 | { this->is_ifunc_symbol_ = true; } | |
1911 | ||
1912 | // Return true if this is an IFUNC symbol. | |
1913 | bool | |
1914 | is_ifunc_symbol() const | |
1915 | { return this->is_ifunc_symbol_; } | |
1916 | ||
aa98ff75 DK |
1917 | // Return true if this has output value. |
1918 | bool | |
1919 | has_output_value() const | |
1920 | { return this->has_output_value_; } | |
1921 | ||
b8e6aad9 ILT |
1922 | private: |
1923 | // The index of this local symbol in the output symbol table. This | |
d3bbad62 ILT |
1924 | // will be 0 if no value has been assigned yet, and the symbol may |
1925 | // be omitted. This will be -1U if the symbol should not go into | |
1926 | // the symbol table. This will be -2U if the symbol must go into | |
1927 | // the symbol table, but no index has been assigned yet. | |
b8e6aad9 | 1928 | unsigned int output_symtab_index_; |
7bf1f802 | 1929 | // The index of this local symbol in the dynamic symbol table. This |
d3bbad62 | 1930 | // will be -1U if the symbol should not go into the symbol table. |
7bf1f802 | 1931 | unsigned int output_dynsym_index_; |
b8e6aad9 ILT |
1932 | // The section index in the input file in which this symbol is |
1933 | // defined. | |
7223e9ca | 1934 | unsigned int input_shndx_ : 27; |
d491d34e ILT |
1935 | // Whether the section index is an ordinary index, not a special |
1936 | // value. | |
1937 | bool is_ordinary_shndx_ : 1; | |
063f12a8 ILT |
1938 | // Whether this is a STT_SECTION symbol. |
1939 | bool is_section_symbol_ : 1; | |
7bf1f802 ILT |
1940 | // Whether this is a STT_TLS symbol. |
1941 | bool is_tls_symbol_ : 1; | |
7223e9ca ILT |
1942 | // Whether this is a STT_GNU_IFUNC symbol. |
1943 | bool is_ifunc_symbol_ : 1; | |
a9a60db6 ILT |
1944 | // Whether this symbol has a value for the output file. This is |
1945 | // normally set to true during Layout::finalize, by | |
1946 | // finalize_local_symbols. It will be false for a section symbol in | |
1947 | // a merge section, as for such symbols we can not determine the | |
1948 | // value to use in a relocation until we see the addend. | |
1949 | bool has_output_value_ : 1; | |
1950 | union | |
1951 | { | |
1952 | // This is used if has_output_value_ is true. Between | |
1953 | // count_local_symbols and finalize_local_symbols, this is the | |
1954 | // value in the input file. After finalize_local_symbols, it is | |
1955 | // the value in the output file. | |
1956 | Value value; | |
1957 | // This is used if has_output_value_ is false. It points to the | |
1958 | // information we need to get the value for a merge section. | |
1959 | Merged_symbol_value<size>* merged_symbol_value; | |
1960 | } u_; | |
b8e6aad9 ILT |
1961 | }; |
1962 | ||
364c7fa5 ILT |
1963 | // This type is used to modify relocations for -fsplit-stack. It is |
1964 | // indexed by relocation index, and means that the relocation at that | |
1965 | // index should use the symbol from the vector, rather than the one | |
1966 | // indicated by the relocation. | |
1967 | ||
1968 | class Reloc_symbol_changes | |
1969 | { | |
1970 | public: | |
1971 | Reloc_symbol_changes(size_t count) | |
1972 | : vec_(count, NULL) | |
1973 | { } | |
1974 | ||
1975 | void | |
1976 | set(size_t i, Symbol* sym) | |
1977 | { this->vec_[i] = sym; } | |
1978 | ||
1979 | const Symbol* | |
1980 | operator[](size_t i) const | |
1981 | { return this->vec_[i]; } | |
1982 | ||
1983 | private: | |
1984 | std::vector<Symbol*> vec_; | |
1985 | }; | |
1986 | ||
cdc29364 CC |
1987 | // Abstract base class for a regular object file, either a real object file |
1988 | // or an incremental (unchanged) object. This is size and endian specific. | |
1989 | ||
1990 | template<int size, bool big_endian> | |
6fa2a40b | 1991 | class Sized_relobj : public Relobj |
cdc29364 CC |
1992 | { |
1993 | public: | |
6fa2a40b | 1994 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; |
cdc29364 CC |
1995 | typedef Relobj::Symbols Symbols; |
1996 | ||
6fa2a40b CC |
1997 | static const Address invalid_address = static_cast<Address>(0) - 1; |
1998 | ||
1999 | Sized_relobj(const std::string& name, Input_file* input_file) | |
2000 | : Relobj(name, input_file), local_got_offsets_(), section_offsets_() | |
cdc29364 CC |
2001 | { } |
2002 | ||
6fa2a40b | 2003 | Sized_relobj(const std::string& name, Input_file* input_file, |
cdc29364 | 2004 | off_t offset) |
6fa2a40b | 2005 | : Relobj(name, input_file, offset), local_got_offsets_(), section_offsets_() |
cdc29364 CC |
2006 | { } |
2007 | ||
6fa2a40b | 2008 | ~Sized_relobj() |
cdc29364 CC |
2009 | { } |
2010 | ||
6fa2a40b CC |
2011 | // If this is a regular object, return a pointer to the Sized_relobj_file |
2012 | // object. Otherwise, return NULL. | |
2013 | virtual Sized_relobj_file<size, big_endian>* | |
2014 | sized_relobj() | |
2015 | { return NULL; } | |
2016 | ||
2017 | const virtual Sized_relobj_file<size, big_endian>* | |
2018 | sized_relobj() const | |
2019 | { return NULL; } | |
2020 | ||
2021 | // Checks if the offset of input section SHNDX within its output | |
2022 | // section is invalid. | |
2023 | bool | |
2024 | is_output_section_offset_invalid(unsigned int shndx) const | |
2025 | { return this->get_output_section_offset(shndx) == invalid_address; } | |
2026 | ||
2027 | // Get the offset of input section SHNDX within its output section. | |
2028 | // This is -1 if the input section requires a special mapping, such | |
2029 | // as a merge section. The output section can be found in the | |
2030 | // output_sections_ field of the parent class Relobj. | |
2031 | Address | |
2032 | get_output_section_offset(unsigned int shndx) const | |
2033 | { | |
2034 | gold_assert(shndx < this->section_offsets_.size()); | |
2035 | return this->section_offsets_[shndx]; | |
2036 | } | |
2037 | ||
6fa2a40b CC |
2038 | // Iterate over local symbols, calling a visitor class V for each GOT offset |
2039 | // associated with a local symbol. | |
2040 | void | |
2041 | do_for_all_local_got_entries(Got_offset_list::Visitor* v) const; | |
2042 | ||
cdc29364 CC |
2043 | protected: |
2044 | typedef Relobj::Output_sections Output_sections; | |
2045 | ||
6fa2a40b CC |
2046 | // Clear the local symbol information. |
2047 | void | |
2048 | clear_got_offsets() | |
2049 | { this->local_got_offsets_.clear(); } | |
2050 | ||
2051 | // Return the vector of section offsets. | |
2052 | std::vector<Address>& | |
2053 | section_offsets() | |
2054 | { return this->section_offsets_; } | |
2055 | ||
c6905c28 CC |
2056 | // Get the address of an output section. |
2057 | uint64_t | |
2058 | do_output_section_address(unsigned int shndx); | |
2059 | ||
6fa2a40b CC |
2060 | // Get the offset of a section. |
2061 | uint64_t | |
2062 | do_output_section_offset(unsigned int shndx) const | |
2063 | { | |
2064 | Address off = this->get_output_section_offset(shndx); | |
2065 | if (off == invalid_address) | |
2066 | return -1ULL; | |
2067 | return off; | |
2068 | } | |
2069 | ||
2070 | // Set the offset of a section. | |
2071 | void | |
2072 | do_set_section_offset(unsigned int shndx, uint64_t off) | |
2073 | { | |
2074 | gold_assert(shndx < this->section_offsets_.size()); | |
2075 | this->section_offsets_[shndx] = | |
2076 | (off == static_cast<uint64_t>(-1) | |
2077 | ? invalid_address | |
2078 | : convert_types<Address, uint64_t>(off)); | |
2079 | } | |
2080 | ||
7ef8ae7c VR |
2081 | // Return whether the local symbol SYMNDX plus ADDEND has a GOT offset |
2082 | // of type GOT_TYPE. | |
83896202 | 2083 | bool |
7ef8ae7c VR |
2084 | do_local_has_got_offset(unsigned int symndx, unsigned int got_type, |
2085 | uint64_t addend) const | |
83896202 | 2086 | { |
7ef8ae7c | 2087 | Local_got_entry_key key(symndx, addend); |
83896202 | 2088 | Local_got_offsets::const_iterator p = |
7ef8ae7c | 2089 | this->local_got_offsets_.find(key); |
83896202 ILT |
2090 | return (p != this->local_got_offsets_.end() |
2091 | && p->second->get_offset(got_type) != -1U); | |
2092 | } | |
2093 | ||
2094 | // Return the GOT offset of type GOT_TYPE of the local symbol | |
7ef8ae7c | 2095 | // SYMNDX plus ADDEND. |
83896202 | 2096 | unsigned int |
7ef8ae7c VR |
2097 | do_local_got_offset(unsigned int symndx, unsigned int got_type, |
2098 | uint64_t addend) const | |
83896202 | 2099 | { |
7ef8ae7c | 2100 | Local_got_entry_key key(symndx, addend); |
83896202 | 2101 | Local_got_offsets::const_iterator p = |
7ef8ae7c | 2102 | this->local_got_offsets_.find(key); |
83896202 ILT |
2103 | gold_assert(p != this->local_got_offsets_.end()); |
2104 | unsigned int off = p->second->get_offset(got_type); | |
2105 | gold_assert(off != -1U); | |
2106 | return off; | |
2107 | } | |
2108 | ||
2109 | // Set the GOT offset with type GOT_TYPE of the local symbol SYMNDX | |
7ef8ae7c | 2110 | // plus ADDEND to GOT_OFFSET. |
83896202 ILT |
2111 | void |
2112 | do_set_local_got_offset(unsigned int symndx, unsigned int got_type, | |
7ef8ae7c | 2113 | unsigned int got_offset, uint64_t addend) |
83896202 | 2114 | { |
7ef8ae7c | 2115 | Local_got_entry_key key(symndx, addend); |
83896202 | 2116 | Local_got_offsets::const_iterator p = |
7ef8ae7c | 2117 | this->local_got_offsets_.find(key); |
83896202 ILT |
2118 | if (p != this->local_got_offsets_.end()) |
2119 | p->second->set_offset(got_type, got_offset); | |
2120 | else | |
2121 | { | |
2122 | Got_offset_list* g = new Got_offset_list(got_type, got_offset); | |
2123 | std::pair<Local_got_offsets::iterator, bool> ins = | |
7ef8ae7c | 2124 | this->local_got_offsets_.insert(std::make_pair(key, g)); |
83896202 ILT |
2125 | gold_assert(ins.second); |
2126 | } | |
2127 | } | |
2128 | ||
9fc236f3 CC |
2129 | // Return the word size of the object file. |
2130 | virtual int | |
2131 | do_elfsize() const | |
2132 | { return size; } | |
2133 | ||
2134 | // Return TRUE if this is a big-endian object file. | |
2135 | virtual bool | |
2136 | do_is_big_endian() const | |
2137 | { return big_endian; } | |
2138 | ||
cdc29364 | 2139 | private: |
6fa2a40b CC |
2140 | // The GOT offsets of local symbols. This map also stores GOT offsets |
2141 | // for tp-relative offsets for TLS symbols. | |
7ef8ae7c VR |
2142 | typedef Unordered_map<Local_got_entry_key, Got_offset_list*, |
2143 | Local_got_entry_key::hash, | |
2144 | Local_got_entry_key::equal_to> Local_got_offsets; | |
6fa2a40b CC |
2145 | |
2146 | // GOT offsets for local non-TLS symbols, and tp-relative offsets | |
7ef8ae7c | 2147 | // for TLS symbols, indexed by local got entry key class. |
6fa2a40b CC |
2148 | Local_got_offsets local_got_offsets_; |
2149 | // For each input section, the offset of the input section in its | |
2150 | // output section. This is INVALID_ADDRESS if the input section requires a | |
2151 | // special mapping. | |
2152 | std::vector<Address> section_offsets_; | |
cdc29364 CC |
2153 | }; |
2154 | ||
14bfc3f5 | 2155 | // A regular object file. This is size and endian specific. |
bae7f79e ILT |
2156 | |
2157 | template<int size, bool big_endian> | |
6fa2a40b | 2158 | class Sized_relobj_file : public Sized_relobj<size, big_endian> |
bae7f79e ILT |
2159 | { |
2160 | public: | |
c06b7b0b | 2161 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; |
6fa2a40b | 2162 | typedef typename Sized_relobj<size, big_endian>::Symbols Symbols; |
b8e6aad9 | 2163 | typedef std::vector<Symbol_value<size> > Local_values; |
c06b7b0b | 2164 | |
eff45813 CC |
2165 | static const Address invalid_address = static_cast<Address>(0) - 1; |
2166 | ||
aa98ff75 DK |
2167 | enum Compute_final_local_value_status |
2168 | { | |
2169 | // No error. | |
2170 | CFLV_OK, | |
2171 | // An error occurred. | |
2172 | CFLV_ERROR, | |
2173 | // The local symbol has no output section. | |
2174 | CFLV_DISCARDED | |
2175 | }; | |
2176 | ||
6fa2a40b CC |
2177 | Sized_relobj_file(const std::string& name, |
2178 | Input_file* input_file, | |
2179 | off_t offset, | |
2180 | const typename elfcpp::Ehdr<size, big_endian>&); | |
bae7f79e | 2181 | |
6fa2a40b | 2182 | ~Sized_relobj_file(); |
6d03d481 | 2183 | |
f733487b | 2184 | // Set up the object file based on TARGET. |
bae7f79e | 2185 | void |
c0a62865 DK |
2186 | setup() |
2187 | { this->do_setup(); } | |
bae7f79e | 2188 | |
6fa2a40b CC |
2189 | // Return a pointer to the Sized_relobj_file object. |
2190 | Sized_relobj_file<size, big_endian>* | |
2191 | sized_relobj() | |
2192 | { return this; } | |
2193 | ||
2194 | const Sized_relobj_file<size, big_endian>* | |
2195 | sized_relobj() const | |
2196 | { return this; } | |
2197 | ||
9590bf25 CC |
2198 | // Return the ELF file type. |
2199 | int | |
2200 | e_type() const | |
2201 | { return this->e_type_; } | |
2202 | ||
7d9e3d98 ILT |
2203 | // Return the number of symbols. This is only valid after |
2204 | // Object::add_symbols has been called. | |
2205 | unsigned int | |
2206 | symbol_count() const | |
2207 | { return this->local_symbol_count_ + this->symbols_.size(); } | |
2208 | ||
730cdc88 ILT |
2209 | // If SYM is the index of a global symbol in the object file's |
2210 | // symbol table, return the Symbol object. Otherwise, return NULL. | |
2211 | Symbol* | |
2212 | global_symbol(unsigned int sym) const | |
2213 | { | |
2214 | if (sym >= this->local_symbol_count_) | |
2215 | { | |
2216 | gold_assert(sym - this->local_symbol_count_ < this->symbols_.size()); | |
2217 | return this->symbols_[sym - this->local_symbol_count_]; | |
2218 | } | |
2219 | return NULL; | |
2220 | } | |
2221 | ||
2222 | // Return the section index of symbol SYM. Set *VALUE to its value | |
d491d34e ILT |
2223 | // in the object file. Set *IS_ORDINARY if this is an ordinary |
2224 | // section index, not a special code between SHN_LORESERVE and | |
2225 | // SHN_HIRESERVE. Note that for a symbol which is not defined in | |
2226 | // this object file, this will set *VALUE to 0 and return SHN_UNDEF; | |
2227 | // it will not return the final value of the symbol in the link. | |
730cdc88 | 2228 | unsigned int |
d491d34e | 2229 | symbol_section_and_value(unsigned int sym, Address* value, bool* is_ordinary); |
730cdc88 ILT |
2230 | |
2231 | // Return a pointer to the Symbol_value structure which holds the | |
2232 | // value of a local symbol. | |
2233 | const Symbol_value<size>* | |
2234 | local_symbol(unsigned int sym) const | |
2235 | { | |
2236 | gold_assert(sym < this->local_values_.size()); | |
2237 | return &this->local_values_[sym]; | |
2238 | } | |
2239 | ||
c06b7b0b ILT |
2240 | // Return the index of local symbol SYM in the ordinary symbol |
2241 | // table. A value of -1U means that the symbol is not being output. | |
2242 | unsigned int | |
2243 | symtab_index(unsigned int sym) const | |
2244 | { | |
b8e6aad9 ILT |
2245 | gold_assert(sym < this->local_values_.size()); |
2246 | return this->local_values_[sym].output_symtab_index(); | |
c06b7b0b ILT |
2247 | } |
2248 | ||
7bf1f802 ILT |
2249 | // Return the index of local symbol SYM in the dynamic symbol |
2250 | // table. A value of -1U means that the symbol is not being output. | |
2251 | unsigned int | |
2252 | dynsym_index(unsigned int sym) const | |
2253 | { | |
2254 | gold_assert(sym < this->local_values_.size()); | |
2255 | return this->local_values_[sym].output_dynsym_index(); | |
2256 | } | |
2257 | ||
6a74a719 ILT |
2258 | // Return the input section index of local symbol SYM. |
2259 | unsigned int | |
d491d34e | 2260 | local_symbol_input_shndx(unsigned int sym, bool* is_ordinary) const |
6a74a719 ILT |
2261 | { |
2262 | gold_assert(sym < this->local_values_.size()); | |
d491d34e | 2263 | return this->local_values_[sym].input_shndx(is_ordinary); |
6a74a719 ILT |
2264 | } |
2265 | ||
d3bbad62 ILT |
2266 | // Record that local symbol SYM must be in the output symbol table. |
2267 | void | |
2268 | set_must_have_output_symtab_entry(unsigned int sym) | |
2269 | { | |
2270 | gold_assert(sym < this->local_values_.size()); | |
2271 | this->local_values_[sym].set_must_have_output_symtab_entry(); | |
2272 | } | |
2273 | ||
d1f003c6 | 2274 | // Record that local symbol SYM needs a dynamic symbol entry. |
7bf1f802 ILT |
2275 | void |
2276 | set_needs_output_dynsym_entry(unsigned int sym) | |
2277 | { | |
2278 | gold_assert(sym < this->local_values_.size()); | |
2279 | this->local_values_[sym].set_needs_output_dynsym_entry(); | |
2280 | } | |
2281 | ||
7223e9ca ILT |
2282 | // Return whether the local symbol SYMNDX has a PLT offset. |
2283 | bool | |
2284 | local_has_plt_offset(unsigned int symndx) const; | |
2285 | ||
7223e9ca ILT |
2286 | // Set the PLT offset of the local symbol SYMNDX. |
2287 | void | |
2288 | set_local_plt_offset(unsigned int symndx, unsigned int plt_offset); | |
2289 | ||
ec4dbad3 AM |
2290 | // Adjust this local symbol value. Return false if the symbol |
2291 | // should be discarded from the output file. | |
2292 | bool | |
2293 | adjust_local_symbol(Symbol_value<size>* lv) const | |
2294 | { return this->do_adjust_local_symbol(lv); } | |
2295 | ||
f7e2ee48 ILT |
2296 | // Return the name of the symbol that spans the given offset in the |
2297 | // specified section in this object. This is used only for error | |
2298 | // messages and is not particularly efficient. | |
2299 | bool | |
2300 | get_symbol_location_info(unsigned int shndx, off_t offset, | |
2301 | Symbol_location_info* info); | |
2302 | ||
e94cf127 CC |
2303 | // Look for a kept section corresponding to the given discarded section, |
2304 | // and return its output address. This is used only for relocations in | |
2305 | // debugging sections. | |
2306 | Address | |
2307 | map_to_kept_section(unsigned int shndx, bool* found) const; | |
2308 | ||
aa98ff75 DK |
2309 | // Compute final local symbol value. R_SYM is the local symbol index. |
2310 | // LV_IN points to a local symbol value containing the input value. | |
2311 | // LV_OUT points to a local symbol value storing the final output value, | |
2312 | // which must not be a merged symbol value since before calling this | |
2313 | // method to avoid memory leak. SYMTAB points to a symbol table. | |
2314 | // | |
2315 | // The method returns a status code at return. If the return status is | |
2316 | // CFLV_OK, *LV_OUT contains the final value. If the return status is | |
2317 | // CFLV_ERROR, *LV_OUT is 0. If the return status is CFLV_DISCARDED, | |
2318 | // *LV_OUT is not modified. | |
2319 | Compute_final_local_value_status | |
2320 | compute_final_local_value(unsigned int r_sym, | |
2321 | const Symbol_value<size>* lv_in, | |
2322 | Symbol_value<size>* lv_out, | |
2323 | const Symbol_table* symtab); | |
2324 | ||
c924eb67 CC |
2325 | // Return true if the layout for this object was deferred. |
2326 | bool is_deferred_layout() const | |
2327 | { return this->is_deferred_layout_; } | |
2328 | ||
6d013333 | 2329 | protected: |
6fa2a40b | 2330 | typedef typename Sized_relobj<size, big_endian>::Output_sections |
cdc29364 CC |
2331 | Output_sections; |
2332 | ||
c0a62865 DK |
2333 | // Set up. |
2334 | virtual void | |
2335 | do_setup(); | |
2336 | ||
a2fb1b05 | 2337 | // Read the symbols. |
bae7f79e | 2338 | void |
12e14209 | 2339 | do_read_symbols(Read_symbols_data*); |
14bfc3f5 | 2340 | |
f35c4853 CC |
2341 | // Read the symbols. This is common code for all target-specific |
2342 | // overrides of do_read_symbols. | |
2343 | void | |
2344 | base_read_symbols(Read_symbols_data*); | |
2345 | ||
83896202 ILT |
2346 | // Return the value of a local symbol. |
2347 | uint64_t | |
2348 | do_local_symbol_value(unsigned int symndx, uint64_t addend) const | |
2349 | { | |
2350 | const Symbol_value<size>* symval = this->local_symbol(symndx); | |
2351 | return symval->value(this, addend); | |
2352 | } | |
2353 | ||
2354 | // Return the PLT offset for a local symbol. It is an error to call | |
2355 | // this if it doesn't have one. | |
2356 | unsigned int | |
2357 | do_local_plt_offset(unsigned int symndx) const; | |
2358 | ||
d77a0555 ILT |
2359 | // Return whether local symbol SYMNDX is a TLS symbol. |
2360 | bool | |
2361 | do_local_is_tls(unsigned int symndx) const | |
2362 | { return this->local_symbol(symndx)->is_tls_symbol(); } | |
2363 | ||
6d013333 ILT |
2364 | // Return the number of local symbols. |
2365 | unsigned int | |
2366 | do_local_symbol_count() const | |
2367 | { return this->local_symbol_count_; } | |
2368 | ||
f0f9babf CC |
2369 | // Return the number of local symbols in the output symbol table. |
2370 | unsigned int | |
2371 | do_output_local_symbol_count() const | |
2372 | { return this->output_local_symbol_count_; } | |
2373 | ||
2374 | // Return the number of local symbols in the output symbol table. | |
2375 | off_t | |
2376 | do_local_symbol_offset() const | |
2377 | { return this->local_symbol_offset_; } | |
2378 | ||
dbe717ef ILT |
2379 | // Lay out the input sections. |
2380 | void | |
7e1edb90 | 2381 | do_layout(Symbol_table*, Layout*, Read_symbols_data*); |
dbe717ef | 2382 | |
5995b570 CC |
2383 | // Layout sections whose layout was deferred while waiting for |
2384 | // input files from a plugin. | |
2385 | void | |
2386 | do_layout_deferred_sections(Layout*); | |
2387 | ||
12e14209 ILT |
2388 | // Add the symbols to the symbol table. |
2389 | void | |
f488e4b0 | 2390 | do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*); |
b0193076 RÁE |
2391 | |
2392 | Archive::Should_include | |
88a4108b | 2393 | do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*, |
b0193076 | 2394 | std::string* why); |
a2fb1b05 | 2395 | |
e0c52780 CC |
2396 | // Iterate over global symbols, calling a visitor class V for each. |
2397 | void | |
2398 | do_for_all_global_symbols(Read_symbols_data* sd, | |
2399 | Library_base::Symbol_visitor_base* v); | |
2400 | ||
92e059d8 ILT |
2401 | // Read the relocs. |
2402 | void | |
2403 | do_read_relocs(Read_relocs_data*); | |
2404 | ||
6d03d481 ST |
2405 | // Process the relocs to find list of referenced sections. Used only |
2406 | // during garbage collection. | |
2407 | void | |
ad0f2072 | 2408 | do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*); |
6d03d481 | 2409 | |
92e059d8 ILT |
2410 | // Scan the relocs and adjust the symbol table. |
2411 | void | |
ad0f2072 | 2412 | do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*); |
92e059d8 | 2413 | |
7bf1f802 ILT |
2414 | // Count the local symbols. |
2415 | void | |
2416 | do_count_local_symbols(Stringpool_template<char>*, | |
2417 | Stringpool_template<char>*); | |
2418 | ||
75f65a3e | 2419 | // Finalize the local symbols. |
c06b7b0b | 2420 | unsigned int |
ef15dade | 2421 | do_finalize_local_symbols(unsigned int, off_t, Symbol_table*); |
7bf1f802 ILT |
2422 | |
2423 | // Set the offset where local dynamic symbol information will be stored. | |
2424 | unsigned int | |
2425 | do_set_local_dynsym_indexes(unsigned int); | |
2426 | ||
2427 | // Set the offset where local dynamic symbol information will be stored. | |
2428 | unsigned int | |
2429 | do_set_local_dynsym_offset(off_t); | |
75f65a3e | 2430 | |
61ba1cf9 ILT |
2431 | // Relocate the input sections and write out the local symbols. |
2432 | void | |
ad0f2072 | 2433 | do_relocate(const Symbol_table* symtab, const Layout*, Output_file* of); |
92e059d8 | 2434 | |
a445fddf ILT |
2435 | // Get the size of a section. |
2436 | uint64_t | |
2437 | do_section_size(unsigned int shndx) | |
2438 | { return this->elf_file_.section_size(shndx); } | |
2439 | ||
92e059d8 ILT |
2440 | // Get the name of a section. |
2441 | std::string | |
54674d38 | 2442 | do_section_name(unsigned int shndx) const |
645f8123 | 2443 | { return this->elf_file_.section_name(shndx); } |
61ba1cf9 | 2444 | |
645f8123 | 2445 | // Return the location of the contents of a section. |
c1027032 CC |
2446 | const unsigned char* |
2447 | do_section_contents(unsigned int shndx, section_size_type* plen, | |
2448 | bool cache) | |
2449 | { | |
2450 | Object::Location loc(this->elf_file_.section_contents(shndx)); | |
2451 | *plen = convert_to_section_size_type(loc.data_size); | |
2452 | if (*plen == 0) | |
2453 | { | |
2454 | static const unsigned char empty[1] = { '\0' }; | |
2455 | return empty; | |
2456 | } | |
2457 | return this->get_view(loc.file_offset, *plen, true, cache); | |
2458 | } | |
f6ce93d6 | 2459 | |
a3ad94ed ILT |
2460 | // Return section flags. |
2461 | uint64_t | |
ef15dade ST |
2462 | do_section_flags(unsigned int shndx); |
2463 | ||
2464 | // Return section entsize. | |
2465 | uint64_t | |
2466 | do_section_entsize(unsigned int shndx); | |
a3ad94ed | 2467 | |
88dd47ac ILT |
2468 | // Return section address. |
2469 | uint64_t | |
2470 | do_section_address(unsigned int shndx) | |
2471 | { return this->elf_file_.section_addr(shndx); } | |
2472 | ||
730cdc88 ILT |
2473 | // Return section type. |
2474 | unsigned int | |
2475 | do_section_type(unsigned int shndx) | |
2476 | { return this->elf_file_.section_type(shndx); } | |
2477 | ||
f7e2ee48 ILT |
2478 | // Return the section link field. |
2479 | unsigned int | |
2480 | do_section_link(unsigned int shndx) | |
2481 | { return this->elf_file_.section_link(shndx); } | |
2482 | ||
4c50553d ILT |
2483 | // Return the section info field. |
2484 | unsigned int | |
2485 | do_section_info(unsigned int shndx) | |
2486 | { return this->elf_file_.section_info(shndx); } | |
2487 | ||
a445fddf ILT |
2488 | // Return the section alignment. |
2489 | uint64_t | |
2490 | do_section_addralign(unsigned int shndx) | |
2491 | { return this->elf_file_.section_addralign(shndx); } | |
2492 | ||
d491d34e ILT |
2493 | // Return the Xindex structure to use. |
2494 | Xindex* | |
2495 | do_initialize_xindex(); | |
2496 | ||
92de84a6 ILT |
2497 | // Get symbol counts. |
2498 | void | |
2499 | do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const; | |
2500 | ||
dde3f402 ILT |
2501 | // Get the global symbols. |
2502 | const Symbols* | |
2503 | do_get_global_symbols() const | |
2504 | { return &this->symbols_; } | |
2505 | ||
c0a62865 DK |
2506 | // Adjust a section index if necessary. |
2507 | unsigned int | |
2508 | adjust_shndx(unsigned int shndx) | |
2509 | { | |
2510 | if (shndx >= elfcpp::SHN_LORESERVE) | |
2511 | shndx += this->elf_file_.large_shndx_offset(); | |
2512 | return shndx; | |
2513 | } | |
2514 | ||
2515 | // Initialize input to output maps for section symbols in merged | |
2516 | // sections. | |
2517 | void | |
2518 | initialize_input_to_output_maps(); | |
2519 | ||
2520 | // Free the input to output maps for section symbols in merged | |
2521 | // sections. | |
2522 | void | |
2523 | free_input_to_output_maps(); | |
2524 | ||
2525 | // Return symbol table section index. | |
2526 | unsigned int | |
2527 | symtab_shndx() const | |
2528 | { return this->symtab_shndx_; } | |
2529 | ||
2530 | // Allow a child class to access the ELF file. | |
2531 | elfcpp::Elf_file<size, big_endian, Object>* | |
2532 | elf_file() | |
2533 | { return &this->elf_file_; } | |
2534 | ||
2535 | // Allow a child class to access the local values. | |
2536 | Local_values* | |
2537 | local_values() | |
2538 | { return &this->local_values_; } | |
2539 | ||
72adc4fa DK |
2540 | // Views and sizes when relocating. |
2541 | struct View_size | |
2542 | { | |
2543 | unsigned char* view; | |
2544 | typename elfcpp::Elf_types<size>::Elf_Addr address; | |
2545 | off_t offset; | |
2546 | section_size_type view_size; | |
2547 | bool is_input_output_view; | |
2548 | bool is_postprocessing_view; | |
487b39df | 2549 | bool is_ctors_reverse_view; |
72adc4fa DK |
2550 | }; |
2551 | ||
2552 | typedef std::vector<View_size> Views; | |
2553 | ||
cf43a2fe AM |
2554 | // Stash away info for a number of special sections. |
2555 | // Return true if any of the sections found require local symbols to be read. | |
2556 | virtual bool | |
2557 | do_find_special_sections(Read_symbols_data* sd); | |
2558 | ||
72adc4fa DK |
2559 | // This may be overriden by a child class. |
2560 | virtual void | |
ad0f2072 | 2561 | do_relocate_sections(const Symbol_table* symtab, const Layout* layout, |
09ec0418 CC |
2562 | const unsigned char* pshdrs, Output_file* of, |
2563 | Views* pviews); | |
72adc4fa | 2564 | |
ec4dbad3 AM |
2565 | // Adjust this local symbol value. Return false if the symbol |
2566 | // should be discarded from the output file. | |
2567 | virtual bool | |
2568 | do_adjust_local_symbol(Symbol_value<size>*) const | |
2569 | { return true; } | |
2570 | ||
e7eca48c DK |
2571 | // Allow a child to set output local symbol count. |
2572 | void | |
2573 | set_output_local_symbol_count(unsigned int value) | |
2574 | { this->output_local_symbol_count_ = value; } | |
a2e47362 | 2575 | |
6b2353a5 | 2576 | // Return the output view for a section. |
39040bb9 | 2577 | unsigned char* |
6b2353a5 CC |
2578 | do_get_output_view(unsigned int, section_size_type*) const; |
2579 | ||
bae7f79e | 2580 | private: |
a2fb1b05 | 2581 | // For convenience. |
6fa2a40b | 2582 | typedef Sized_relobj_file<size, big_endian> This; |
a2fb1b05 ILT |
2583 | static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size; |
2584 | static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; | |
2585 | static const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
75f65a3e ILT |
2586 | typedef elfcpp::Shdr<size, big_endian> Shdr; |
2587 | ||
ef9beddf ILT |
2588 | // To keep track of discarded comdat sections, we need to map a member |
2589 | // section index to the object and section index of the corresponding | |
2590 | // kept section. | |
2591 | struct Kept_comdat_section | |
2592 | { | |
1ef4d87f ILT |
2593 | Kept_comdat_section(Relobj* a_object, unsigned int a_shndx) |
2594 | : object(a_object), shndx(a_shndx) | |
ef9beddf | 2595 | { } |
1ef4d87f ILT |
2596 | Relobj* object; |
2597 | unsigned int shndx; | |
ef9beddf | 2598 | }; |
1ef4d87f | 2599 | typedef std::map<unsigned int, Kept_comdat_section> |
ef9beddf ILT |
2600 | Kept_comdat_section_table; |
2601 | ||
dbe717ef ILT |
2602 | // Find the SHT_SYMTAB section, given the section headers. |
2603 | void | |
2604 | find_symtab(const unsigned char* pshdrs); | |
2605 | ||
730cdc88 ILT |
2606 | // Return whether SHDR has the right flags for a GNU style exception |
2607 | // frame section. | |
2608 | bool | |
2609 | check_eh_frame_flags(const elfcpp::Shdr<size, big_endian>* shdr) const; | |
2610 | ||
2611 | // Return whether there is a section named .eh_frame which might be | |
2612 | // a GNU style exception frame section. | |
2613 | bool | |
2614 | find_eh_frame(const unsigned char* pshdrs, const char* names, | |
8383303e | 2615 | section_size_type names_size) const; |
730cdc88 | 2616 | |
a2fb1b05 ILT |
2617 | // Whether to include a section group in the link. |
2618 | bool | |
6a74a719 | 2619 | include_section_group(Symbol_table*, Layout*, unsigned int, const char*, |
ca09d69a | 2620 | const unsigned char*, const char*, section_size_type, |
a2fb1b05 ILT |
2621 | std::vector<bool>*); |
2622 | ||
2623 | // Whether to include a linkonce section in the link. | |
2624 | bool | |
e94cf127 | 2625 | include_linkonce_section(Layout*, unsigned int, const char*, |
a2fb1b05 ILT |
2626 | const elfcpp::Shdr<size, big_endian>&); |
2627 | ||
5995b570 CC |
2628 | // Layout an input section. |
2629 | void | |
2630 | layout_section(Layout* layout, unsigned int shndx, const char* name, | |
14788a3f | 2631 | const typename This::Shdr& shdr, unsigned int reloc_shndx, |
5995b570 CC |
2632 | unsigned int reloc_type); |
2633 | ||
14788a3f ILT |
2634 | // Layout an input .eh_frame section. |
2635 | void | |
2636 | layout_eh_frame_section(Layout* layout, const unsigned char* symbols_data, | |
2637 | section_size_type symbols_size, | |
2638 | const unsigned char* symbol_names_data, | |
2639 | section_size_type symbol_names_size, | |
2640 | unsigned int shndx, const typename This::Shdr&, | |
2641 | unsigned int reloc_shndx, unsigned int reloc_type); | |
2642 | ||
61ba1cf9 ILT |
2643 | // Write section data to the output file. Record the views and |
2644 | // sizes in VIEWS for use when relocating. | |
2645 | void | |
487b39df ILT |
2646 | write_sections(const Layout*, const unsigned char* pshdrs, Output_file*, |
2647 | Views*); | |
61ba1cf9 ILT |
2648 | |
2649 | // Relocate the sections in the output file. | |
2650 | void | |
2ea97941 | 2651 | relocate_sections(const Symbol_table* symtab, const Layout* layout, |
09ec0418 CC |
2652 | const unsigned char* pshdrs, Output_file* of, |
2653 | Views* pviews) | |
2654 | { this->do_relocate_sections(symtab, layout, pshdrs, of, pviews); } | |
61ba1cf9 | 2655 | |
487b39df ILT |
2656 | // Reverse the words in a section. Used for .ctors sections mapped |
2657 | // to .init_array sections. | |
2658 | void | |
2659 | reverse_words(unsigned char*, section_size_type); | |
2660 | ||
7019cd25 ILT |
2661 | // Scan the input relocations for --emit-relocs. |
2662 | void | |
ad0f2072 | 2663 | emit_relocs_scan(Symbol_table*, Layout*, const unsigned char* plocal_syms, |
7019cd25 ILT |
2664 | const Read_relocs_data::Relocs_list::iterator&); |
2665 | ||
2666 | // Scan the input relocations for --emit-relocs, templatized on the | |
2667 | // type of the relocation section. | |
2668 | template<int sh_type> | |
2669 | void | |
ad0f2072 | 2670 | emit_relocs_scan_reltype(Symbol_table*, Layout*, |
7019cd25 ILT |
2671 | const unsigned char* plocal_syms, |
2672 | const Read_relocs_data::Relocs_list::iterator&, | |
2673 | Relocatable_relocs*); | |
2674 | ||
09ec0418 CC |
2675 | // Scan the input relocations for --incremental. |
2676 | void | |
2677 | incremental_relocs_scan(const Read_relocs_data::Relocs_list::iterator&); | |
2678 | ||
2679 | // Scan the input relocations for --incremental, templatized on the | |
2680 | // type of the relocation section. | |
2681 | template<int sh_type> | |
2682 | void | |
2683 | incremental_relocs_scan_reltype( | |
2684 | const Read_relocs_data::Relocs_list::iterator&); | |
2685 | ||
2686 | void | |
2687 | incremental_relocs_write(const Relocate_info<size, big_endian>*, | |
2688 | unsigned int sh_type, | |
2689 | const unsigned char* prelocs, | |
2690 | size_t reloc_count, | |
2691 | Output_section*, | |
2692 | Address output_offset, | |
2693 | Output_file*); | |
2694 | ||
2695 | template<int sh_type> | |
2696 | void | |
2697 | incremental_relocs_write_reltype(const Relocate_info<size, big_endian>*, | |
2698 | const unsigned char* prelocs, | |
2699 | size_t reloc_count, | |
2700 | Output_section*, | |
2701 | Address output_offset, | |
2702 | Output_file*); | |
2703 | ||
364c7fa5 ILT |
2704 | // A type shared by split_stack_adjust_reltype and find_functions. |
2705 | typedef std::map<section_offset_type, section_size_type> Function_offsets; | |
2706 | ||
2707 | // Check for -fsplit-stack routines calling non-split-stack routines. | |
2708 | void | |
2709 | split_stack_adjust(const Symbol_table*, const unsigned char* pshdrs, | |
2710 | unsigned int sh_type, unsigned int shndx, | |
2711 | const unsigned char* prelocs, size_t reloc_count, | |
2712 | unsigned char* view, section_size_type view_size, | |
4d625b70 CC |
2713 | Reloc_symbol_changes** reloc_map, |
2714 | const Sized_target<size, big_endian>* target); | |
364c7fa5 ILT |
2715 | |
2716 | template<int sh_type> | |
2717 | void | |
2718 | split_stack_adjust_reltype(const Symbol_table*, const unsigned char* pshdrs, | |
2719 | unsigned int shndx, const unsigned char* prelocs, | |
2720 | size_t reloc_count, unsigned char* view, | |
2721 | section_size_type view_size, | |
4d625b70 CC |
2722 | Reloc_symbol_changes** reloc_map, |
2723 | const Sized_target<size, big_endian>* target); | |
364c7fa5 ILT |
2724 | |
2725 | // Find all functions in a section. | |
2726 | void | |
2727 | find_functions(const unsigned char* pshdrs, unsigned int shndx, | |
2728 | Function_offsets*); | |
2729 | ||
61ba1cf9 ILT |
2730 | // Write out the local symbols. |
2731 | void | |
b8e6aad9 | 2732 | write_local_symbols(Output_file*, |
7bf1f802 | 2733 | const Stringpool_template<char>*, |
d491d34e ILT |
2734 | const Stringpool_template<char>*, |
2735 | Output_symtab_xindex*, | |
cdc29364 CC |
2736 | Output_symtab_xindex*, |
2737 | off_t); | |
61ba1cf9 | 2738 | |
ef9beddf ILT |
2739 | // Record a mapping from discarded section SHNDX to the corresponding |
2740 | // kept section. | |
2741 | void | |
1ef4d87f ILT |
2742 | set_kept_comdat_section(unsigned int shndx, Relobj* kept_object, |
2743 | unsigned int kept_shndx) | |
ef9beddf | 2744 | { |
1ef4d87f ILT |
2745 | Kept_comdat_section kept(kept_object, kept_shndx); |
2746 | this->kept_comdat_sections_.insert(std::make_pair(shndx, kept)); | |
ef9beddf ILT |
2747 | } |
2748 | ||
1ef4d87f ILT |
2749 | // Find the kept section corresponding to the discarded section |
2750 | // SHNDX. Return true if found. | |
2751 | bool | |
2752 | get_kept_comdat_section(unsigned int shndx, Relobj** kept_object, | |
2753 | unsigned int* kept_shndx) const | |
ef9beddf ILT |
2754 | { |
2755 | typename Kept_comdat_section_table::const_iterator p = | |
2756 | this->kept_comdat_sections_.find(shndx); | |
2757 | if (p == this->kept_comdat_sections_.end()) | |
1ef4d87f ILT |
2758 | return false; |
2759 | *kept_object = p->second.object; | |
2760 | *kept_shndx = p->second.shndx; | |
2761 | return true; | |
ef9beddf ILT |
2762 | } |
2763 | ||
aa98ff75 DK |
2764 | // Compute final local symbol value. R_SYM is the local symbol index. |
2765 | // LV_IN points to a local symbol value containing the input value. | |
2766 | // LV_OUT points to a local symbol value storing the final output value, | |
2767 | // which must not be a merged symbol value since before calling this | |
2768 | // method to avoid memory leak. RELOCATABLE indicates whether we are | |
2769 | // linking a relocatable output. OUT_SECTIONS is an array of output | |
2770 | // sections. OUT_OFFSETS is an array of offsets of the sections. SYMTAB | |
2771 | // points to a symbol table. | |
2772 | // | |
2773 | // The method returns a status code at return. If the return status is | |
2774 | // CFLV_OK, *LV_OUT contains the final value. If the return status is | |
2775 | // CFLV_ERROR, *LV_OUT is 0. If the return status is CFLV_DISCARDED, | |
2776 | // *LV_OUT is not modified. | |
2777 | inline Compute_final_local_value_status | |
2778 | compute_final_local_value_internal(unsigned int r_sym, | |
2779 | const Symbol_value<size>* lv_in, | |
2780 | Symbol_value<size>* lv_out, | |
2781 | bool relocatable, | |
2782 | const Output_sections& out_sections, | |
2783 | const std::vector<Address>& out_offsets, | |
2784 | const Symbol_table* symtab); | |
2785 | ||
7223e9ca ILT |
2786 | // The PLT offsets of local symbols. |
2787 | typedef Unordered_map<unsigned int, unsigned int> Local_plt_offsets; | |
07f397ab | 2788 | |
5995b570 CC |
2789 | // Saved information for sections whose layout was deferred. |
2790 | struct Deferred_layout | |
2791 | { | |
2792 | static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; | |
2793 | Deferred_layout(unsigned int shndx, const char* name, | |
2794 | const unsigned char* pshdr, | |
2795 | unsigned int reloc_shndx, unsigned int reloc_type) | |
2796 | : shndx_(shndx), name_(name), reloc_shndx_(reloc_shndx), | |
2797 | reloc_type_(reloc_type) | |
2798 | { | |
2799 | memcpy(this->shdr_data_, pshdr, shdr_size); | |
2800 | } | |
2801 | unsigned int shndx_; | |
2802 | std::string name_; | |
2803 | unsigned int reloc_shndx_; | |
2804 | unsigned int reloc_type_; | |
2805 | unsigned char shdr_data_[shdr_size]; | |
2806 | }; | |
2807 | ||
645f8123 ILT |
2808 | // General access to the ELF file. |
2809 | elfcpp::Elf_file<size, big_endian, Object> elf_file_; | |
9590bf25 CC |
2810 | // Type of ELF file (ET_REL or ET_EXEC). ET_EXEC files are allowed |
2811 | // as input files only for the --just-symbols option. | |
2812 | int e_type_; | |
bae7f79e | 2813 | // Index of SHT_SYMTAB section. |
645f8123 | 2814 | unsigned int symtab_shndx_; |
61ba1cf9 ILT |
2815 | // The number of local symbols. |
2816 | unsigned int local_symbol_count_; | |
2817 | // The number of local symbols which go into the output file. | |
2818 | unsigned int output_local_symbol_count_; | |
7bf1f802 ILT |
2819 | // The number of local symbols which go into the output file's dynamic |
2820 | // symbol table. | |
2821 | unsigned int output_local_dynsym_count_; | |
14bfc3f5 | 2822 | // The entries in the symbol table for the external symbols. |
730cdc88 | 2823 | Symbols symbols_; |
92de84a6 ILT |
2824 | // Number of symbols defined in object file itself. |
2825 | size_t defined_count_; | |
cdc29364 | 2826 | // File offset for local symbols (relative to start of symbol table). |
75f65a3e | 2827 | off_t local_symbol_offset_; |
cdc29364 | 2828 | // File offset for local dynamic symbols (absolute). |
7bf1f802 | 2829 | off_t local_dynsym_offset_; |
61ba1cf9 | 2830 | // Values of local symbols. |
c06b7b0b | 2831 | Local_values local_values_; |
7223e9ca ILT |
2832 | // PLT offsets for local symbols. |
2833 | Local_plt_offsets local_plt_offsets_; | |
ef9beddf ILT |
2834 | // Table mapping discarded comdat sections to corresponding kept sections. |
2835 | Kept_comdat_section_table kept_comdat_sections_; | |
730cdc88 ILT |
2836 | // Whether this object has a GNU style .eh_frame section. |
2837 | bool has_eh_frame_; | |
805bb01c DK |
2838 | // If this object has a GNU style .eh_frame section that is discarded in |
2839 | // output, record the index here. Otherwise it is -1U. | |
2840 | unsigned int discarded_eh_frame_shndx_; | |
c924eb67 CC |
2841 | // True if the layout of this object was deferred, waiting for plugin |
2842 | // replacement files. | |
2843 | bool is_deferred_layout_; | |
5995b570 CC |
2844 | // The list of sections whose layout was deferred. |
2845 | std::vector<Deferred_layout> deferred_layout_; | |
f3a2388f CC |
2846 | // The list of relocation sections whose layout was deferred. |
2847 | std::vector<Deferred_layout> deferred_layout_relocs_; | |
6b2353a5 CC |
2848 | // Pointer to the list of output views; valid only during do_relocate(). |
2849 | const Views* output_views_; | |
bae7f79e ILT |
2850 | }; |
2851 | ||
54dc6425 | 2852 | // A class to manage the list of all objects. |
a2fb1b05 | 2853 | |
54dc6425 ILT |
2854 | class Input_objects |
2855 | { | |
2856 | public: | |
2857 | Input_objects() | |
fd9d194f | 2858 | : relobj_list_(), dynobj_list_(), sonames_(), cref_(NULL) |
54dc6425 ILT |
2859 | { } |
2860 | ||
f6ce93d6 ILT |
2861 | // The type of the list of input relocateable objects. |
2862 | typedef std::vector<Relobj*> Relobj_list; | |
2863 | typedef Relobj_list::const_iterator Relobj_iterator; | |
2864 | ||
2865 | // The type of the list of input dynamic objects. | |
2866 | typedef std::vector<Dynobj*> Dynobj_list; | |
2867 | typedef Dynobj_list::const_iterator Dynobj_iterator; | |
54dc6425 | 2868 | |
008db82e ILT |
2869 | // Add an object to the list. Return true if all is well, or false |
2870 | // if this object should be ignored. | |
2871 | bool | |
54dc6425 ILT |
2872 | add_object(Object*); |
2873 | ||
92de84a6 ILT |
2874 | // Start processing an archive. |
2875 | void | |
2876 | archive_start(Archive*); | |
2877 | ||
2878 | // Stop processing an archive. | |
2879 | void | |
2880 | archive_stop(Archive*); | |
2881 | ||
e2827e5f ILT |
2882 | // For each dynamic object, check whether we've seen all of its |
2883 | // explicit dependencies. | |
2884 | void | |
2885 | check_dynamic_dependencies() const; | |
2886 | ||
9a2d6984 ILT |
2887 | // Return whether an object was found in the system library |
2888 | // directory. | |
2889 | bool | |
2890 | found_in_system_library_directory(const Object*) const; | |
2891 | ||
92de84a6 ILT |
2892 | // Print symbol counts. |
2893 | void | |
2894 | print_symbol_counts(const Symbol_table*) const; | |
2895 | ||
dde3f402 ILT |
2896 | // Print a cross reference table. |
2897 | void | |
2898 | print_cref(const Symbol_table*, FILE*) const; | |
2899 | ||
f6ce93d6 ILT |
2900 | // Iterate over all regular objects. |
2901 | ||
2902 | Relobj_iterator | |
2903 | relobj_begin() const | |
2904 | { return this->relobj_list_.begin(); } | |
2905 | ||
2906 | Relobj_iterator | |
2907 | relobj_end() const | |
2908 | { return this->relobj_list_.end(); } | |
2909 | ||
2910 | // Iterate over all dynamic objects. | |
2911 | ||
2912 | Dynobj_iterator | |
2913 | dynobj_begin() const | |
2914 | { return this->dynobj_list_.begin(); } | |
54dc6425 | 2915 | |
f6ce93d6 ILT |
2916 | Dynobj_iterator |
2917 | dynobj_end() const | |
2918 | { return this->dynobj_list_.end(); } | |
54dc6425 ILT |
2919 | |
2920 | // Return whether we have seen any dynamic objects. | |
2921 | bool | |
2922 | any_dynamic() const | |
f6ce93d6 | 2923 | { return !this->dynobj_list_.empty(); } |
54dc6425 | 2924 | |
fa17a3f4 ILT |
2925 | // Return the number of non dynamic objects. |
2926 | int | |
2927 | number_of_relobjs() const | |
2928 | { return this->relobj_list_.size(); } | |
2929 | ||
fe9a4c12 ILT |
2930 | // Return the number of input objects. |
2931 | int | |
2932 | number_of_input_objects() const | |
2933 | { return this->relobj_list_.size() + this->dynobj_list_.size(); } | |
2934 | ||
54dc6425 ILT |
2935 | private: |
2936 | Input_objects(const Input_objects&); | |
2937 | Input_objects& operator=(const Input_objects&); | |
2938 | ||
008db82e | 2939 | // The list of ordinary objects included in the link. |
f6ce93d6 | 2940 | Relobj_list relobj_list_; |
008db82e | 2941 | // The list of dynamic objects included in the link. |
f6ce93d6 | 2942 | Dynobj_list dynobj_list_; |
008db82e | 2943 | // SONAMEs that we have seen. |
4bfacfd3 | 2944 | Unordered_map<std::string, Object*> sonames_; |
92de84a6 ILT |
2945 | // Manage cross-references if requested. |
2946 | Cref* cref_; | |
54dc6425 | 2947 | }; |
a2fb1b05 | 2948 | |
92e059d8 ILT |
2949 | // Some of the information we pass to the relocation routines. We |
2950 | // group this together to avoid passing a dozen different arguments. | |
2951 | ||
2952 | template<int size, bool big_endian> | |
2953 | struct Relocate_info | |
2954 | { | |
92e059d8 ILT |
2955 | // Symbol table. |
2956 | const Symbol_table* symtab; | |
2957 | // Layout. | |
2958 | const Layout* layout; | |
2959 | // Object being relocated. | |
6fa2a40b | 2960 | Sized_relobj_file<size, big_endian>* object; |
92e059d8 ILT |
2961 | // Section index of relocation section. |
2962 | unsigned int reloc_shndx; | |
82bb573a ILT |
2963 | // Section header of relocation section. |
2964 | const unsigned char* reloc_shdr; | |
91a65d2f AM |
2965 | // Info about how relocs should be handled |
2966 | Relocatable_relocs* rr; | |
92e059d8 ILT |
2967 | // Section index of section being relocated. |
2968 | unsigned int data_shndx; | |
82bb573a ILT |
2969 | // Section header of data section. |
2970 | const unsigned char* data_shdr; | |
92e059d8 ILT |
2971 | |
2972 | // Return a string showing the location of a relocation. This is | |
2973 | // only used for error messages. | |
2974 | std::string | |
2975 | location(size_t relnum, off_t reloffset) const; | |
2976 | }; | |
2977 | ||
5ac169d4 DK |
2978 | // This is used to represent a section in an object and is used as the |
2979 | // key type for various section maps. | |
efc6fa12 | 2980 | typedef std::pair<Relobj*, unsigned int> Section_id; |
5ac169d4 DK |
2981 | |
2982 | // This is similar to Section_id but is used when the section | |
2983 | // pointers are const. | |
efc6fa12 | 2984 | typedef std::pair<const Relobj*, unsigned int> Const_section_id; |
5ac169d4 DK |
2985 | |
2986 | // The hash value is based on the address of an object in memory during | |
2987 | // linking. It is okay to use this for looking up sections but never use | |
2988 | // this in an unordered container that we want to traverse in a repeatable | |
2989 | // manner. | |
2990 | ||
2991 | struct Section_id_hash | |
2992 | { | |
2993 | size_t operator()(const Section_id& loc) const | |
2994 | { return reinterpret_cast<uintptr_t>(loc.first) ^ loc.second; } | |
2995 | }; | |
2996 | ||
2997 | struct Const_section_id_hash | |
2998 | { | |
2999 | size_t operator()(const Const_section_id& loc) const | |
3000 | { return reinterpret_cast<uintptr_t>(loc.first) ^ loc.second; } | |
3001 | }; | |
3002 | ||
f6060a4d ILT |
3003 | // Return whether INPUT_FILE contains an ELF object start at file |
3004 | // offset OFFSET. This sets *START to point to a view of the start of | |
3005 | // the file. It sets *READ_SIZE to the number of bytes in the view. | |
3006 | ||
3007 | extern bool | |
3008 | is_elf_object(Input_file* input_file, off_t offset, | |
ca09d69a | 3009 | const unsigned char** start, int* read_size); |
f6060a4d | 3010 | |
bae7f79e | 3011 | // Return an Object appropriate for the input file. P is BYTES long, |
15f8229b ILT |
3012 | // and holds the ELF header. If PUNCONFIGURED is not NULL, then if |
3013 | // this sees an object the linker is not configured to support, it | |
3014 | // sets *PUNCONFIGURED to true and returns NULL without giving an | |
3015 | // error message. | |
bae7f79e | 3016 | |
61ba1cf9 ILT |
3017 | extern Object* |
3018 | make_elf_object(const std::string& name, Input_file*, | |
3019 | off_t offset, const unsigned char* p, | |
15f8229b | 3020 | section_offset_type bytes, bool* punconfigured); |
bae7f79e ILT |
3021 | |
3022 | } // end namespace gold | |
3023 | ||
3024 | #endif // !defined(GOLD_OBJECT_H) |