Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // object.cc -- support for an object file for linking in gold |
2 | ||
b90efa5b | 3 | // Copyright (C) 2006-2015 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 | #include "gold.h" |
24 | ||
25 | #include <cerrno> | |
26 | #include <cstring> | |
645f8123 | 27 | #include <cstdarg> |
a2b1aa12 | 28 | #include "demangle.h" |
9a2d6984 | 29 | #include "libiberty.h" |
bae7f79e | 30 | |
6d03d481 | 31 | #include "gc.h" |
14bfc3f5 | 32 | #include "target-select.h" |
5c2c6c95 | 33 | #include "dwarf_reader.h" |
a2fb1b05 | 34 | #include "layout.h" |
61ba1cf9 | 35 | #include "output.h" |
f6ce93d6 | 36 | #include "symtab.h" |
92de84a6 | 37 | #include "cref.h" |
4c50553d | 38 | #include "reloc.h" |
f6ce93d6 ILT |
39 | #include "object.h" |
40 | #include "dynobj.h" | |
5995b570 | 41 | #include "plugin.h" |
a2e47362 | 42 | #include "compressed_output.h" |
09ec0418 | 43 | #include "incremental.h" |
dbe40a88 | 44 | #include "merge.h" |
bae7f79e ILT |
45 | |
46 | namespace gold | |
47 | { | |
48 | ||
00698fc5 CC |
49 | // Struct Read_symbols_data. |
50 | ||
d2d60eef CC |
51 | // Destroy any remaining File_view objects and buffers of decompressed |
52 | // sections. | |
00698fc5 CC |
53 | |
54 | Read_symbols_data::~Read_symbols_data() | |
55 | { | |
56 | if (this->section_headers != NULL) | |
57 | delete this->section_headers; | |
58 | if (this->section_names != NULL) | |
59 | delete this->section_names; | |
60 | if (this->symbols != NULL) | |
61 | delete this->symbols; | |
62 | if (this->symbol_names != NULL) | |
63 | delete this->symbol_names; | |
64 | if (this->versym != NULL) | |
65 | delete this->versym; | |
66 | if (this->verdef != NULL) | |
67 | delete this->verdef; | |
68 | if (this->verneed != NULL) | |
69 | delete this->verneed; | |
70 | } | |
71 | ||
d491d34e ILT |
72 | // Class Xindex. |
73 | ||
74 | // Initialize the symtab_xindex_ array. Find the SHT_SYMTAB_SHNDX | |
75 | // section and read it in. SYMTAB_SHNDX is the index of the symbol | |
76 | // table we care about. | |
77 | ||
78 | template<int size, bool big_endian> | |
79 | void | |
2ea97941 | 80 | Xindex::initialize_symtab_xindex(Object* object, unsigned int symtab_shndx) |
d491d34e ILT |
81 | { |
82 | if (!this->symtab_xindex_.empty()) | |
83 | return; | |
84 | ||
2ea97941 | 85 | gold_assert(symtab_shndx != 0); |
d491d34e ILT |
86 | |
87 | // Look through the sections in reverse order, on the theory that it | |
88 | // is more likely to be near the end than the beginning. | |
89 | unsigned int i = object->shnum(); | |
90 | while (i > 0) | |
91 | { | |
92 | --i; | |
93 | if (object->section_type(i) == elfcpp::SHT_SYMTAB_SHNDX | |
2ea97941 | 94 | && this->adjust_shndx(object->section_link(i)) == symtab_shndx) |
d491d34e ILT |
95 | { |
96 | this->read_symtab_xindex<size, big_endian>(object, i, NULL); | |
97 | return; | |
98 | } | |
99 | } | |
100 | ||
101 | object->error(_("missing SHT_SYMTAB_SHNDX section")); | |
102 | } | |
103 | ||
104 | // Read in the symtab_xindex_ array, given the section index of the | |
105 | // SHT_SYMTAB_SHNDX section. If PSHDRS is not NULL, it points at the | |
106 | // section headers. | |
107 | ||
108 | template<int size, bool big_endian> | |
109 | void | |
110 | Xindex::read_symtab_xindex(Object* object, unsigned int xindex_shndx, | |
111 | const unsigned char* pshdrs) | |
112 | { | |
113 | section_size_type bytecount; | |
114 | const unsigned char* contents; | |
115 | if (pshdrs == NULL) | |
116 | contents = object->section_contents(xindex_shndx, &bytecount, false); | |
117 | else | |
118 | { | |
119 | const unsigned char* p = (pshdrs | |
120 | + (xindex_shndx | |
121 | * elfcpp::Elf_sizes<size>::shdr_size)); | |
122 | typename elfcpp::Shdr<size, big_endian> shdr(p); | |
123 | bytecount = convert_to_section_size_type(shdr.get_sh_size()); | |
124 | contents = object->get_view(shdr.get_sh_offset(), bytecount, true, false); | |
125 | } | |
126 | ||
127 | gold_assert(this->symtab_xindex_.empty()); | |
128 | this->symtab_xindex_.reserve(bytecount / 4); | |
129 | for (section_size_type i = 0; i < bytecount; i += 4) | |
130 | { | |
131 | unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(contents + i); | |
132 | // We preadjust the section indexes we save. | |
133 | this->symtab_xindex_.push_back(this->adjust_shndx(shndx)); | |
134 | } | |
135 | } | |
136 | ||
137 | // Symbol symndx has a section of SHN_XINDEX; return the real section | |
138 | // index. | |
139 | ||
140 | unsigned int | |
141 | Xindex::sym_xindex_to_shndx(Object* object, unsigned int symndx) | |
142 | { | |
143 | if (symndx >= this->symtab_xindex_.size()) | |
144 | { | |
145 | object->error(_("symbol %u out of range for SHT_SYMTAB_SHNDX section"), | |
146 | symndx); | |
147 | return elfcpp::SHN_UNDEF; | |
148 | } | |
149 | unsigned int shndx = this->symtab_xindex_[symndx]; | |
150 | if (shndx < elfcpp::SHN_LORESERVE || shndx >= object->shnum()) | |
151 | { | |
152 | object->error(_("extended index for symbol %u out of range: %u"), | |
153 | symndx, shndx); | |
154 | return elfcpp::SHN_UNDEF; | |
155 | } | |
156 | return shndx; | |
157 | } | |
158 | ||
645f8123 ILT |
159 | // Class Object. |
160 | ||
75f2446e ILT |
161 | // Report an error for this object file. This is used by the |
162 | // elfcpp::Elf_file interface, and also called by the Object code | |
163 | // itself. | |
645f8123 ILT |
164 | |
165 | void | |
75f2446e | 166 | Object::error(const char* format, ...) const |
645f8123 ILT |
167 | { |
168 | va_list args; | |
645f8123 | 169 | va_start(args, format); |
75f2446e ILT |
170 | char* buf = NULL; |
171 | if (vasprintf(&buf, format, args) < 0) | |
172 | gold_nomem(); | |
645f8123 | 173 | va_end(args); |
75f2446e ILT |
174 | gold_error(_("%s: %s"), this->name().c_str(), buf); |
175 | free(buf); | |
645f8123 ILT |
176 | } |
177 | ||
178 | // Return a view of the contents of a section. | |
179 | ||
180 | const unsigned char* | |
8383303e ILT |
181 | Object::section_contents(unsigned int shndx, section_size_type* plen, |
182 | bool cache) | |
c1027032 | 183 | { return this->do_section_contents(shndx, plen, cache); } |
645f8123 | 184 | |
6fa2a40b | 185 | // Read the section data into SD. This is code common to Sized_relobj_file |
dbe717ef ILT |
186 | // and Sized_dynobj, so we put it into Object. |
187 | ||
188 | template<int size, bool big_endian> | |
189 | void | |
190 | Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file, | |
191 | Read_symbols_data* sd) | |
192 | { | |
193 | const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; | |
194 | ||
195 | // Read the section headers. | |
196 | const off_t shoff = elf_file->shoff(); | |
2ea97941 ILT |
197 | const unsigned int shnum = this->shnum(); |
198 | sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size, | |
39d0cb0e | 199 | true, true); |
dbe717ef ILT |
200 | |
201 | // Read the section names. | |
202 | const unsigned char* pshdrs = sd->section_headers->data(); | |
203 | const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size; | |
204 | typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames); | |
205 | ||
206 | if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB) | |
75f2446e ILT |
207 | this->error(_("section name section has wrong type: %u"), |
208 | static_cast<unsigned int>(shdrnames.get_sh_type())); | |
dbe717ef | 209 | |
8383303e ILT |
210 | sd->section_names_size = |
211 | convert_to_section_size_type(shdrnames.get_sh_size()); | |
dbe717ef | 212 | sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(), |
39d0cb0e ILT |
213 | sd->section_names_size, false, |
214 | false); | |
dbe717ef ILT |
215 | } |
216 | ||
2ea97941 | 217 | // If NAME is the name of a special .gnu.warning section, arrange for |
dbe717ef ILT |
218 | // the warning to be issued. SHNDX is the section index. Return |
219 | // whether it is a warning section. | |
220 | ||
221 | bool | |
2ea97941 | 222 | Object::handle_gnu_warning_section(const char* name, unsigned int shndx, |
dbe717ef ILT |
223 | Symbol_table* symtab) |
224 | { | |
225 | const char warn_prefix[] = ".gnu.warning."; | |
226 | const int warn_prefix_len = sizeof warn_prefix - 1; | |
2ea97941 | 227 | if (strncmp(name, warn_prefix, warn_prefix_len) == 0) |
dbe717ef | 228 | { |
cb295612 ILT |
229 | // Read the section contents to get the warning text. It would |
230 | // be nicer if we only did this if we have to actually issue a | |
231 | // warning. Unfortunately, warnings are issued as we relocate | |
232 | // sections. That means that we can not lock the object then, | |
233 | // as we might try to issue the same warning multiple times | |
234 | // simultaneously. | |
235 | section_size_type len; | |
236 | const unsigned char* contents = this->section_contents(shndx, &len, | |
237 | false); | |
8d63875c ILT |
238 | if (len == 0) |
239 | { | |
2ea97941 | 240 | const char* warning = name + warn_prefix_len; |
8d63875c ILT |
241 | contents = reinterpret_cast<const unsigned char*>(warning); |
242 | len = strlen(warning); | |
243 | } | |
cb295612 | 244 | std::string warning(reinterpret_cast<const char*>(contents), len); |
2ea97941 | 245 | symtab->add_warning(name + warn_prefix_len, this, warning); |
dbe717ef ILT |
246 | return true; |
247 | } | |
248 | return false; | |
249 | } | |
250 | ||
2ea97941 | 251 | // If NAME is the name of the special section which indicates that |
9b547ce6 | 252 | // this object was compiled with -fsplit-stack, mark it accordingly. |
364c7fa5 ILT |
253 | |
254 | bool | |
2ea97941 | 255 | Object::handle_split_stack_section(const char* name) |
364c7fa5 | 256 | { |
2ea97941 | 257 | if (strcmp(name, ".note.GNU-split-stack") == 0) |
364c7fa5 ILT |
258 | { |
259 | this->uses_split_stack_ = true; | |
260 | return true; | |
261 | } | |
2ea97941 | 262 | if (strcmp(name, ".note.GNU-no-split-stack") == 0) |
364c7fa5 ILT |
263 | { |
264 | this->has_no_split_stack_ = true; | |
265 | return true; | |
266 | } | |
267 | return false; | |
268 | } | |
269 | ||
6d03d481 ST |
270 | // Class Relobj |
271 | ||
dbe40a88 RÁE |
272 | template<int size> |
273 | void | |
274 | Relobj::initialize_input_to_output_map(unsigned int shndx, | |
275 | typename elfcpp::Elf_types<size>::Elf_Addr starting_address, | |
276 | Unordered_map<section_offset_type, | |
277 | typename elfcpp::Elf_types<size>::Elf_Addr>* output_addresses) const { | |
278 | Object_merge_map *map = this->object_merge_map_; | |
279 | map->initialize_input_to_output_map<size>(shndx, starting_address, | |
280 | output_addresses); | |
281 | } | |
282 | ||
283 | void | |
284 | Relobj::add_merge_mapping(Output_section_data *output_data, | |
285 | unsigned int shndx, section_offset_type offset, | |
286 | section_size_type length, | |
287 | section_offset_type output_offset) { | |
288 | if (this->object_merge_map_ == NULL) | |
289 | { | |
290 | this->object_merge_map_ = new Object_merge_map(); | |
291 | } | |
292 | ||
293 | this->object_merge_map_->add_mapping(output_data, shndx, offset, length, | |
294 | output_offset); | |
295 | } | |
296 | ||
297 | bool | |
298 | Relobj::merge_output_offset(unsigned int shndx, section_offset_type offset, | |
299 | section_offset_type *poutput) const { | |
300 | Object_merge_map* object_merge_map = this->object_merge_map_; | |
301 | if (object_merge_map == NULL) | |
302 | return false; | |
303 | return object_merge_map->get_output_offset(shndx, offset, poutput); | |
304 | } | |
305 | ||
306 | bool | |
307 | Relobj::is_merge_section_for(const Output_section_data* output_data, | |
308 | unsigned int shndx) const { | |
309 | Object_merge_map* object_merge_map = this->object_merge_map_; | |
310 | if (object_merge_map == NULL) | |
311 | return false; | |
312 | return object_merge_map->is_merge_section_for(output_data, shndx); | |
313 | ||
314 | } | |
315 | ||
6d03d481 | 316 | // To copy the symbols data read from the file to a local data structure. |
2e702c99 | 317 | // This function is called from do_layout only while doing garbage |
6d03d481 ST |
318 | // collection. |
319 | ||
320 | void | |
2e702c99 RM |
321 | Relobj::copy_symbols_data(Symbols_data* gc_sd, Read_symbols_data* sd, |
322 | unsigned int section_header_size) | |
6d03d481 | 323 | { |
2e702c99 RM |
324 | gc_sd->section_headers_data = |
325 | new unsigned char[(section_header_size)]; | |
6d03d481 | 326 | memcpy(gc_sd->section_headers_data, sd->section_headers->data(), |
2e702c99 RM |
327 | section_header_size); |
328 | gc_sd->section_names_data = | |
329 | new unsigned char[sd->section_names_size]; | |
6d03d481 | 330 | memcpy(gc_sd->section_names_data, sd->section_names->data(), |
2e702c99 | 331 | sd->section_names_size); |
6d03d481 ST |
332 | gc_sd->section_names_size = sd->section_names_size; |
333 | if (sd->symbols != NULL) | |
334 | { | |
2e702c99 RM |
335 | gc_sd->symbols_data = |
336 | new unsigned char[sd->symbols_size]; | |
6d03d481 | 337 | memcpy(gc_sd->symbols_data, sd->symbols->data(), |
2e702c99 | 338 | sd->symbols_size); |
6d03d481 ST |
339 | } |
340 | else | |
341 | { | |
342 | gc_sd->symbols_data = NULL; | |
343 | } | |
344 | gc_sd->symbols_size = sd->symbols_size; | |
345 | gc_sd->external_symbols_offset = sd->external_symbols_offset; | |
346 | if (sd->symbol_names != NULL) | |
347 | { | |
348 | gc_sd->symbol_names_data = | |
2e702c99 | 349 | new unsigned char[sd->symbol_names_size]; |
6d03d481 | 350 | memcpy(gc_sd->symbol_names_data, sd->symbol_names->data(), |
2e702c99 | 351 | sd->symbol_names_size); |
6d03d481 ST |
352 | } |
353 | else | |
354 | { | |
355 | gc_sd->symbol_names_data = NULL; | |
356 | } | |
357 | gc_sd->symbol_names_size = sd->symbol_names_size; | |
358 | } | |
359 | ||
360 | // This function determines if a particular section name must be included | |
361 | // in the link. This is used during garbage collection to determine the | |
362 | // roots of the worklist. | |
363 | ||
364 | bool | |
2ea97941 | 365 | Relobj::is_section_name_included(const char* name) |
6d03d481 | 366 | { |
2e702c99 RM |
367 | if (is_prefix_of(".ctors", name) |
368 | || is_prefix_of(".dtors", name) | |
369 | || is_prefix_of(".note", name) | |
370 | || is_prefix_of(".init", name) | |
371 | || is_prefix_of(".fini", name) | |
372 | || is_prefix_of(".gcc_except_table", name) | |
373 | || is_prefix_of(".jcr", name) | |
374 | || is_prefix_of(".preinit_array", name) | |
375 | || (is_prefix_of(".text", name) | |
376 | && strstr(name, "personality")) | |
377 | || (is_prefix_of(".data", name) | |
1698990d AM |
378 | && strstr(name, "personality")) |
379 | || (is_prefix_of(".sdata", name) | |
380 | && strstr(name, "personality")) | |
fa618ee4 | 381 | || (is_prefix_of(".gnu.linkonce.d", name) |
5ad9b0a7 ST |
382 | && strstr(name, "personality")) |
383 | || (is_prefix_of(".rodata", name) | |
384 | && strstr(name, "nptl_version"))) | |
6d03d481 | 385 | { |
2e702c99 | 386 | return true; |
6d03d481 ST |
387 | } |
388 | return false; | |
389 | } | |
390 | ||
09ec0418 CC |
391 | // Finalize the incremental relocation information. Allocates a block |
392 | // of relocation entries for each symbol, and sets the reloc_bases_ | |
cdc29364 CC |
393 | // array to point to the first entry in each block. If CLEAR_COUNTS |
394 | // is TRUE, also clear the per-symbol relocation counters. | |
09ec0418 CC |
395 | |
396 | void | |
cdc29364 | 397 | Relobj::finalize_incremental_relocs(Layout* layout, bool clear_counts) |
09ec0418 CC |
398 | { |
399 | unsigned int nsyms = this->get_global_symbols()->size(); | |
400 | this->reloc_bases_ = new unsigned int[nsyms]; | |
401 | ||
402 | gold_assert(this->reloc_bases_ != NULL); | |
403 | gold_assert(layout->incremental_inputs() != NULL); | |
404 | ||
405 | unsigned int rindex = layout->incremental_inputs()->get_reloc_count(); | |
406 | for (unsigned int i = 0; i < nsyms; ++i) | |
407 | { | |
408 | this->reloc_bases_[i] = rindex; | |
409 | rindex += this->reloc_counts_[i]; | |
cdc29364 CC |
410 | if (clear_counts) |
411 | this->reloc_counts_[i] = 0; | |
09ec0418 CC |
412 | } |
413 | layout->incremental_inputs()->set_reloc_count(rindex); | |
414 | } | |
415 | ||
f6ce93d6 | 416 | // Class Sized_relobj. |
bae7f79e | 417 | |
6fa2a40b CC |
418 | // Iterate over local symbols, calling a visitor class V for each GOT offset |
419 | // associated with a local symbol. | |
420 | ||
bae7f79e | 421 | template<int size, bool big_endian> |
6fa2a40b CC |
422 | void |
423 | Sized_relobj<size, big_endian>::do_for_all_local_got_entries( | |
424 | Got_offset_list::Visitor* v) const | |
425 | { | |
426 | unsigned int nsyms = this->local_symbol_count(); | |
427 | for (unsigned int i = 0; i < nsyms; i++) | |
428 | { | |
429 | Local_got_offsets::const_iterator p = this->local_got_offsets_.find(i); | |
430 | if (p != this->local_got_offsets_.end()) | |
431 | { | |
432 | const Got_offset_list* got_offsets = p->second; | |
433 | got_offsets->for_all_got_offsets(v); | |
434 | } | |
435 | } | |
436 | } | |
437 | ||
c6905c28 CC |
438 | // Get the address of an output section. |
439 | ||
440 | template<int size, bool big_endian> | |
441 | uint64_t | |
442 | Sized_relobj<size, big_endian>::do_output_section_address( | |
443 | unsigned int shndx) | |
444 | { | |
445 | // If the input file is linked as --just-symbols, the output | |
446 | // section address is the input section address. | |
447 | if (this->just_symbols()) | |
448 | return this->section_address(shndx); | |
449 | ||
450 | const Output_section* os = this->do_output_section(shndx); | |
451 | gold_assert(os != NULL); | |
452 | return os->address(); | |
453 | } | |
454 | ||
6fa2a40b CC |
455 | // Class Sized_relobj_file. |
456 | ||
457 | template<int size, bool big_endian> | |
458 | Sized_relobj_file<size, big_endian>::Sized_relobj_file( | |
2ea97941 ILT |
459 | const std::string& name, |
460 | Input_file* input_file, | |
461 | off_t offset, | |
bae7f79e | 462 | const elfcpp::Ehdr<size, big_endian>& ehdr) |
6fa2a40b | 463 | : Sized_relobj<size, big_endian>(name, input_file, offset), |
645f8123 | 464 | elf_file_(this, ehdr), |
dbe717ef | 465 | symtab_shndx_(-1U), |
61ba1cf9 ILT |
466 | local_symbol_count_(0), |
467 | output_local_symbol_count_(0), | |
7bf1f802 | 468 | output_local_dynsym_count_(0), |
730cdc88 | 469 | symbols_(), |
92de84a6 | 470 | defined_count_(0), |
61ba1cf9 | 471 | local_symbol_offset_(0), |
7bf1f802 | 472 | local_dynsym_offset_(0), |
e727fa71 | 473 | local_values_(), |
7223e9ca | 474 | local_plt_offsets_(), |
ef9beddf | 475 | kept_comdat_sections_(), |
805bb01c | 476 | has_eh_frame_(false), |
a2e47362 | 477 | discarded_eh_frame_shndx_(-1U), |
c924eb67 | 478 | is_deferred_layout_(false), |
a2e47362 | 479 | deferred_layout_(), |
0d5bbdb0 | 480 | deferred_layout_relocs_() |
bae7f79e | 481 | { |
9590bf25 | 482 | this->e_type_ = ehdr.get_e_type(); |
bae7f79e ILT |
483 | } |
484 | ||
485 | template<int size, bool big_endian> | |
6fa2a40b | 486 | Sized_relobj_file<size, big_endian>::~Sized_relobj_file() |
bae7f79e ILT |
487 | { |
488 | } | |
489 | ||
645f8123 | 490 | // Set up an object file based on the file header. This sets up the |
029ba973 | 491 | // section information. |
bae7f79e ILT |
492 | |
493 | template<int size, bool big_endian> | |
494 | void | |
6fa2a40b | 495 | Sized_relobj_file<size, big_endian>::do_setup() |
bae7f79e | 496 | { |
2ea97941 ILT |
497 | const unsigned int shnum = this->elf_file_.shnum(); |
498 | this->set_shnum(shnum); | |
dbe717ef | 499 | } |
12e14209 | 500 | |
dbe717ef ILT |
501 | // Find the SHT_SYMTAB section, given the section headers. The ELF |
502 | // standard says that maybe in the future there can be more than one | |
503 | // SHT_SYMTAB section. Until somebody figures out how that could | |
504 | // work, we assume there is only one. | |
12e14209 | 505 | |
dbe717ef ILT |
506 | template<int size, bool big_endian> |
507 | void | |
6fa2a40b | 508 | Sized_relobj_file<size, big_endian>::find_symtab(const unsigned char* pshdrs) |
dbe717ef | 509 | { |
2ea97941 | 510 | const unsigned int shnum = this->shnum(); |
dbe717ef | 511 | this->symtab_shndx_ = 0; |
2ea97941 | 512 | if (shnum > 0) |
bae7f79e | 513 | { |
dbe717ef ILT |
514 | // Look through the sections in reverse order, since gas tends |
515 | // to put the symbol table at the end. | |
2ea97941 ILT |
516 | const unsigned char* p = pshdrs + shnum * This::shdr_size; |
517 | unsigned int i = shnum; | |
d491d34e ILT |
518 | unsigned int xindex_shndx = 0; |
519 | unsigned int xindex_link = 0; | |
dbe717ef | 520 | while (i > 0) |
bae7f79e | 521 | { |
dbe717ef ILT |
522 | --i; |
523 | p -= This::shdr_size; | |
524 | typename This::Shdr shdr(p); | |
525 | if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB) | |
526 | { | |
527 | this->symtab_shndx_ = i; | |
d491d34e ILT |
528 | if (xindex_shndx > 0 && xindex_link == i) |
529 | { | |
530 | Xindex* xindex = | |
531 | new Xindex(this->elf_file_.large_shndx_offset()); | |
532 | xindex->read_symtab_xindex<size, big_endian>(this, | |
533 | xindex_shndx, | |
534 | pshdrs); | |
535 | this->set_xindex(xindex); | |
536 | } | |
dbe717ef ILT |
537 | break; |
538 | } | |
d491d34e ILT |
539 | |
540 | // Try to pick up the SHT_SYMTAB_SHNDX section, if there is | |
541 | // one. This will work if it follows the SHT_SYMTAB | |
542 | // section. | |
543 | if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB_SHNDX) | |
544 | { | |
545 | xindex_shndx = i; | |
546 | xindex_link = this->adjust_shndx(shdr.get_sh_link()); | |
547 | } | |
bae7f79e | 548 | } |
bae7f79e ILT |
549 | } |
550 | } | |
551 | ||
d491d34e ILT |
552 | // Return the Xindex structure to use for object with lots of |
553 | // sections. | |
554 | ||
555 | template<int size, bool big_endian> | |
556 | Xindex* | |
6fa2a40b | 557 | Sized_relobj_file<size, big_endian>::do_initialize_xindex() |
d491d34e ILT |
558 | { |
559 | gold_assert(this->symtab_shndx_ != -1U); | |
560 | Xindex* xindex = new Xindex(this->elf_file_.large_shndx_offset()); | |
561 | xindex->initialize_symtab_xindex<size, big_endian>(this, this->symtab_shndx_); | |
562 | return xindex; | |
563 | } | |
564 | ||
730cdc88 ILT |
565 | // Return whether SHDR has the right type and flags to be a GNU |
566 | // .eh_frame section. | |
567 | ||
568 | template<int size, bool big_endian> | |
569 | bool | |
6fa2a40b | 570 | Sized_relobj_file<size, big_endian>::check_eh_frame_flags( |
730cdc88 ILT |
571 | const elfcpp::Shdr<size, big_endian>* shdr) const |
572 | { | |
4d5e4e62 ILT |
573 | elfcpp::Elf_Word sh_type = shdr->get_sh_type(); |
574 | return ((sh_type == elfcpp::SHT_PROGBITS | |
575 | || sh_type == elfcpp::SHT_X86_64_UNWIND) | |
1650c4ff | 576 | && (shdr->get_sh_flags() & elfcpp::SHF_ALLOC) != 0); |
730cdc88 ILT |
577 | } |
578 | ||
cf43a2fe AM |
579 | // Find the section header with the given name. |
580 | ||
581 | template<int size, bool big_endian> | |
582 | const unsigned char* | |
dc3714f3 | 583 | Object::find_shdr( |
cf43a2fe AM |
584 | const unsigned char* pshdrs, |
585 | const char* name, | |
586 | const char* names, | |
587 | section_size_type names_size, | |
588 | const unsigned char* hdr) const | |
589 | { | |
dc3714f3 | 590 | const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; |
cf43a2fe | 591 | const unsigned int shnum = this->shnum(); |
dc3714f3 | 592 | const unsigned char* hdr_end = pshdrs + shdr_size * shnum; |
cf43a2fe AM |
593 | size_t sh_name = 0; |
594 | ||
595 | while (1) | |
596 | { | |
597 | if (hdr) | |
598 | { | |
599 | // We found HDR last time we were called, continue looking. | |
dc3714f3 | 600 | typename elfcpp::Shdr<size, big_endian> shdr(hdr); |
cf43a2fe AM |
601 | sh_name = shdr.get_sh_name(); |
602 | } | |
603 | else | |
604 | { | |
605 | // Look for the next occurrence of NAME in NAMES. | |
606 | // The fact that .shstrtab produced by current GNU tools is | |
607 | // string merged means we shouldn't have both .not.foo and | |
608 | // .foo in .shstrtab, and multiple .foo sections should all | |
609 | // have the same sh_name. However, this is not guaranteed | |
610 | // by the ELF spec and not all ELF object file producers may | |
611 | // be so clever. | |
612 | size_t len = strlen(name) + 1; | |
613 | const char *p = sh_name ? names + sh_name + len : names; | |
614 | p = reinterpret_cast<const char*>(memmem(p, names_size - (p - names), | |
615 | name, len)); | |
616 | if (p == NULL) | |
617 | return NULL; | |
618 | sh_name = p - names; | |
619 | hdr = pshdrs; | |
620 | if (sh_name == 0) | |
621 | return hdr; | |
622 | } | |
623 | ||
dc3714f3 | 624 | hdr += shdr_size; |
cf43a2fe AM |
625 | while (hdr < hdr_end) |
626 | { | |
dc3714f3 | 627 | typename elfcpp::Shdr<size, big_endian> shdr(hdr); |
cf43a2fe AM |
628 | if (shdr.get_sh_name() == sh_name) |
629 | return hdr; | |
dc3714f3 | 630 | hdr += shdr_size; |
cf43a2fe AM |
631 | } |
632 | hdr = NULL; | |
633 | if (sh_name == 0) | |
634 | return hdr; | |
635 | } | |
636 | } | |
637 | ||
730cdc88 ILT |
638 | // Return whether there is a GNU .eh_frame section, given the section |
639 | // headers and the section names. | |
640 | ||
641 | template<int size, bool big_endian> | |
642 | bool | |
6fa2a40b | 643 | Sized_relobj_file<size, big_endian>::find_eh_frame( |
8383303e ILT |
644 | const unsigned char* pshdrs, |
645 | const char* names, | |
646 | section_size_type names_size) const | |
730cdc88 | 647 | { |
cf43a2fe AM |
648 | const unsigned char* s = NULL; |
649 | ||
650 | while (1) | |
730cdc88 | 651 | { |
dc3714f3 AM |
652 | s = this->template find_shdr<size, big_endian>(pshdrs, ".eh_frame", |
653 | names, names_size, s); | |
cf43a2fe AM |
654 | if (s == NULL) |
655 | return false; | |
730cdc88 | 656 | |
cf43a2fe AM |
657 | typename This::Shdr shdr(s); |
658 | if (this->check_eh_frame_flags(&shdr)) | |
659 | return true; | |
730cdc88 | 660 | } |
730cdc88 ILT |
661 | } |
662 | ||
5dd8762a | 663 | // Return TRUE if this is a section whose contents will be needed in the |
c1027032 CC |
664 | // Add_symbols task. This function is only called for sections that have |
665 | // already passed the test in is_compressed_debug_section(), so we know | |
666 | // that the section name begins with ".zdebug". | |
5dd8762a CC |
667 | |
668 | static bool | |
669 | need_decompressed_section(const char* name) | |
670 | { | |
c1027032 CC |
671 | // Skip over the ".zdebug" and a quick check for the "_". |
672 | name += 7; | |
673 | if (*name++ != '_') | |
674 | return false; | |
675 | ||
676 | #ifdef ENABLE_THREADS | |
677 | // Decompressing these sections now will help only if we're | |
678 | // multithreaded. | |
679 | if (parameters->options().threads()) | |
680 | { | |
681 | // We will need .zdebug_str if this is not an incremental link | |
682 | // (i.e., we are processing string merge sections) or if we need | |
683 | // to build a gdb index. | |
684 | if ((!parameters->incremental() || parameters->options().gdb_index()) | |
685 | && strcmp(name, "str") == 0) | |
686 | return true; | |
687 | ||
688 | // We will need these other sections when building a gdb index. | |
689 | if (parameters->options().gdb_index() | |
690 | && (strcmp(name, "info") == 0 | |
691 | || strcmp(name, "types") == 0 | |
692 | || strcmp(name, "pubnames") == 0 | |
693 | || strcmp(name, "pubtypes") == 0 | |
694 | || strcmp(name, "ranges") == 0 | |
695 | || strcmp(name, "abbrev") == 0)) | |
696 | return true; | |
697 | } | |
698 | #endif | |
699 | ||
700 | // Even when single-threaded, we will need .zdebug_str if this is | |
701 | // not an incremental link and we are building a gdb index. | |
702 | // Otherwise, we would decompress the section twice: once for | |
703 | // string merge processing, and once for building the gdb index. | |
704 | if (!parameters->incremental() | |
705 | && parameters->options().gdb_index() | |
706 | && strcmp(name, "str") == 0) | |
5dd8762a CC |
707 | return true; |
708 | ||
709 | return false; | |
710 | } | |
711 | ||
a2e47362 | 712 | // Build a table for any compressed debug sections, mapping each section index |
5dd8762a | 713 | // to the uncompressed size and (if needed) the decompressed contents. |
a2e47362 CC |
714 | |
715 | template<int size, bool big_endian> | |
716 | Compressed_section_map* | |
717 | build_compressed_section_map( | |
718 | const unsigned char* pshdrs, | |
719 | unsigned int shnum, | |
720 | const char* names, | |
721 | section_size_type names_size, | |
0d5bbdb0 CC |
722 | Object* obj, |
723 | bool decompress_if_needed) | |
a2e47362 | 724 | { |
5dd8762a | 725 | Compressed_section_map* uncompressed_map = new Compressed_section_map(); |
a2e47362 CC |
726 | const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; |
727 | const unsigned char* p = pshdrs + shdr_size; | |
5dd8762a | 728 | |
a2e47362 CC |
729 | for (unsigned int i = 1; i < shnum; ++i, p += shdr_size) |
730 | { | |
731 | typename elfcpp::Shdr<size, big_endian> shdr(p); | |
732 | if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS | |
733 | && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0) | |
734 | { | |
735 | if (shdr.get_sh_name() >= names_size) | |
736 | { | |
737 | obj->error(_("bad section name offset for section %u: %lu"), | |
738 | i, static_cast<unsigned long>(shdr.get_sh_name())); | |
739 | continue; | |
740 | } | |
741 | ||
742 | const char* name = names + shdr.get_sh_name(); | |
743 | if (is_compressed_debug_section(name)) | |
744 | { | |
745 | section_size_type len; | |
746 | const unsigned char* contents = | |
747 | obj->section_contents(i, &len, false); | |
748 | uint64_t uncompressed_size = get_uncompressed_size(contents, len); | |
c1027032 CC |
749 | Compressed_section_info info; |
750 | info.size = convert_to_section_size_type(uncompressed_size); | |
751 | info.contents = NULL; | |
a2e47362 | 752 | if (uncompressed_size != -1ULL) |
5dd8762a | 753 | { |
c1027032 | 754 | unsigned char* uncompressed_data = NULL; |
0d5bbdb0 | 755 | if (decompress_if_needed && need_decompressed_section(name)) |
5dd8762a | 756 | { |
c1027032 CC |
757 | uncompressed_data = new unsigned char[uncompressed_size]; |
758 | if (decompress_input_section(contents, len, | |
759 | uncompressed_data, | |
760 | uncompressed_size)) | |
761 | info.contents = uncompressed_data; | |
762 | else | |
763 | delete[] uncompressed_data; | |
5dd8762a | 764 | } |
5dd8762a CC |
765 | (*uncompressed_map)[i] = info; |
766 | } | |
a2e47362 CC |
767 | } |
768 | } | |
769 | } | |
5dd8762a | 770 | return uncompressed_map; |
a2e47362 CC |
771 | } |
772 | ||
cf43a2fe AM |
773 | // Stash away info for a number of special sections. |
774 | // Return true if any of the sections found require local symbols to be read. | |
775 | ||
776 | template<int size, bool big_endian> | |
777 | bool | |
778 | Sized_relobj_file<size, big_endian>::do_find_special_sections( | |
779 | Read_symbols_data* sd) | |
780 | { | |
781 | const unsigned char* const pshdrs = sd->section_headers->data(); | |
782 | const unsigned char* namesu = sd->section_names->data(); | |
783 | const char* names = reinterpret_cast<const char*>(namesu); | |
784 | ||
785 | if (this->find_eh_frame(pshdrs, names, sd->section_names_size)) | |
786 | this->has_eh_frame_ = true; | |
787 | ||
788 | if (memmem(names, sd->section_names_size, ".zdebug_", 8) != NULL) | |
0d5bbdb0 CC |
789 | { |
790 | Compressed_section_map* compressed_sections = | |
791 | build_compressed_section_map<size, big_endian>( | |
792 | pshdrs, this->shnum(), names, sd->section_names_size, this, true); | |
793 | if (compressed_sections != NULL) | |
794 | this->set_compressed_sections(compressed_sections); | |
795 | } | |
796 | ||
cf43a2fe AM |
797 | return (this->has_eh_frame_ |
798 | || (!parameters->options().relocatable() | |
799 | && parameters->options().gdb_index() | |
800 | && (memmem(names, sd->section_names_size, "debug_info", 12) == 0 | |
801 | || memmem(names, sd->section_names_size, "debug_types", | |
802 | 13) == 0))); | |
803 | } | |
804 | ||
12e14209 | 805 | // Read the sections and symbols from an object file. |
bae7f79e ILT |
806 | |
807 | template<int size, bool big_endian> | |
12e14209 | 808 | void |
6fa2a40b | 809 | Sized_relobj_file<size, big_endian>::do_read_symbols(Read_symbols_data* sd) |
f35c4853 CC |
810 | { |
811 | this->base_read_symbols(sd); | |
812 | } | |
813 | ||
814 | // Read the sections and symbols from an object file. This is common | |
815 | // code for all target-specific overrides of do_read_symbols(). | |
816 | ||
817 | template<int size, bool big_endian> | |
818 | void | |
819 | Sized_relobj_file<size, big_endian>::base_read_symbols(Read_symbols_data* sd) | |
bae7f79e | 820 | { |
dbe717ef | 821 | this->read_section_data(&this->elf_file_, sd); |
12e14209 | 822 | |
dbe717ef ILT |
823 | const unsigned char* const pshdrs = sd->section_headers->data(); |
824 | ||
825 | this->find_symtab(pshdrs); | |
12e14209 | 826 | |
cf43a2fe | 827 | bool need_local_symbols = this->do_find_special_sections(sd); |
c1027032 | 828 | |
75f2446e ILT |
829 | sd->symbols = NULL; |
830 | sd->symbols_size = 0; | |
730cdc88 | 831 | sd->external_symbols_offset = 0; |
75f2446e ILT |
832 | sd->symbol_names = NULL; |
833 | sd->symbol_names_size = 0; | |
834 | ||
645f8123 | 835 | if (this->symtab_shndx_ == 0) |
bae7f79e ILT |
836 | { |
837 | // No symbol table. Weird but legal. | |
12e14209 | 838 | return; |
bae7f79e ILT |
839 | } |
840 | ||
12e14209 ILT |
841 | // Get the symbol table section header. |
842 | typename This::Shdr symtabshdr(pshdrs | |
645f8123 | 843 | + this->symtab_shndx_ * This::shdr_size); |
a3ad94ed | 844 | gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); |
bae7f79e | 845 | |
c1027032 CC |
846 | // If this object has a .eh_frame section, or if building a .gdb_index |
847 | // section and there is debug info, we need all the symbols. | |
730cdc88 ILT |
848 | // Otherwise we only need the external symbols. While it would be |
849 | // simpler to just always read all the symbols, I've seen object | |
850 | // files with well over 2000 local symbols, which for a 64-bit | |
851 | // object file format is over 5 pages that we don't need to read | |
852 | // now. | |
853 | ||
2ea97941 | 854 | const int sym_size = This::sym_size; |
92e059d8 ILT |
855 | const unsigned int loccount = symtabshdr.get_sh_info(); |
856 | this->local_symbol_count_ = loccount; | |
7bf1f802 | 857 | this->local_values_.resize(loccount); |
2ea97941 | 858 | section_offset_type locsize = loccount * sym_size; |
730cdc88 | 859 | off_t dataoff = symtabshdr.get_sh_offset(); |
8383303e ILT |
860 | section_size_type datasize = |
861 | convert_to_section_size_type(symtabshdr.get_sh_size()); | |
730cdc88 | 862 | off_t extoff = dataoff + locsize; |
8383303e | 863 | section_size_type extsize = datasize - locsize; |
75f65a3e | 864 | |
c1027032 CC |
865 | off_t readoff = need_local_symbols ? dataoff : extoff; |
866 | section_size_type readsize = need_local_symbols ? datasize : extsize; | |
730cdc88 | 867 | |
3f2e6a2d CC |
868 | if (readsize == 0) |
869 | { | |
870 | // No external symbols. Also weird but also legal. | |
871 | return; | |
872 | } | |
873 | ||
39d0cb0e | 874 | File_view* fvsymtab = this->get_lasting_view(readoff, readsize, true, false); |
bae7f79e ILT |
875 | |
876 | // Read the section header for the symbol names. | |
d491d34e | 877 | unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link()); |
dbe717ef | 878 | if (strtab_shndx >= this->shnum()) |
bae7f79e | 879 | { |
75f2446e ILT |
880 | this->error(_("invalid symbol table name index: %u"), strtab_shndx); |
881 | return; | |
bae7f79e | 882 | } |
dbe717ef | 883 | typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size); |
bae7f79e ILT |
884 | if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB) |
885 | { | |
75f2446e ILT |
886 | this->error(_("symbol table name section has wrong type: %u"), |
887 | static_cast<unsigned int>(strtabshdr.get_sh_type())); | |
888 | return; | |
bae7f79e ILT |
889 | } |
890 | ||
891 | // Read the symbol names. | |
892 | File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(), | |
39d0cb0e ILT |
893 | strtabshdr.get_sh_size(), |
894 | false, true); | |
bae7f79e | 895 | |
12e14209 | 896 | sd->symbols = fvsymtab; |
730cdc88 | 897 | sd->symbols_size = readsize; |
c1027032 | 898 | sd->external_symbols_offset = need_local_symbols ? locsize : 0; |
12e14209 | 899 | sd->symbol_names = fvstrtab; |
8383303e ILT |
900 | sd->symbol_names_size = |
901 | convert_to_section_size_type(strtabshdr.get_sh_size()); | |
a2fb1b05 ILT |
902 | } |
903 | ||
730cdc88 | 904 | // Return the section index of symbol SYM. Set *VALUE to its value in |
d491d34e | 905 | // the object file. Set *IS_ORDINARY if this is an ordinary section |
9b547ce6 | 906 | // index, not a special code between SHN_LORESERVE and SHN_HIRESERVE. |
d491d34e ILT |
907 | // Note that for a symbol which is not defined in this object file, |
908 | // this will set *VALUE to 0 and return SHN_UNDEF; it will not return | |
909 | // the final value of the symbol in the link. | |
730cdc88 ILT |
910 | |
911 | template<int size, bool big_endian> | |
912 | unsigned int | |
6fa2a40b CC |
913 | Sized_relobj_file<size, big_endian>::symbol_section_and_value(unsigned int sym, |
914 | Address* value, | |
915 | bool* is_ordinary) | |
730cdc88 | 916 | { |
8383303e | 917 | section_size_type symbols_size; |
730cdc88 ILT |
918 | const unsigned char* symbols = this->section_contents(this->symtab_shndx_, |
919 | &symbols_size, | |
920 | false); | |
921 | ||
922 | const size_t count = symbols_size / This::sym_size; | |
923 | gold_assert(sym < count); | |
924 | ||
925 | elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size); | |
926 | *value = elfsym.get_st_value(); | |
d491d34e ILT |
927 | |
928 | return this->adjust_sym_shndx(sym, elfsym.get_st_shndx(), is_ordinary); | |
730cdc88 ILT |
929 | } |
930 | ||
a2fb1b05 ILT |
931 | // Return whether to include a section group in the link. LAYOUT is |
932 | // used to keep track of which section groups we have already seen. | |
933 | // INDEX is the index of the section group and SHDR is the section | |
934 | // header. If we do not want to include this group, we set bits in | |
935 | // OMIT for each section which should be discarded. | |
936 | ||
937 | template<int size, bool big_endian> | |
938 | bool | |
6fa2a40b | 939 | Sized_relobj_file<size, big_endian>::include_section_group( |
6a74a719 | 940 | Symbol_table* symtab, |
2ea97941 | 941 | Layout* layout, |
a2fb1b05 | 942 | unsigned int index, |
2ea97941 | 943 | const char* name, |
e94cf127 CC |
944 | const unsigned char* shdrs, |
945 | const char* section_names, | |
946 | section_size_type section_names_size, | |
a2fb1b05 ILT |
947 | std::vector<bool>* omit) |
948 | { | |
949 | // Read the section contents. | |
e94cf127 | 950 | typename This::Shdr shdr(shdrs + index * This::shdr_size); |
a2fb1b05 | 951 | const unsigned char* pcon = this->get_view(shdr.get_sh_offset(), |
39d0cb0e | 952 | shdr.get_sh_size(), true, false); |
a2fb1b05 ILT |
953 | const elfcpp::Elf_Word* pword = |
954 | reinterpret_cast<const elfcpp::Elf_Word*>(pcon); | |
955 | ||
956 | // The first word contains flags. We only care about COMDAT section | |
957 | // groups. Other section groups are always included in the link | |
958 | // just like ordinary sections. | |
f6ce93d6 | 959 | elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword); |
a2fb1b05 | 960 | |
41f9cbbe ILT |
961 | // Look up the group signature, which is the name of a symbol. ELF |
962 | // uses a symbol name because some group signatures are long, and | |
963 | // the name is generally already in the symbol table, so it makes | |
964 | // sense to put the long string just once in .strtab rather than in | |
965 | // both .strtab and .shstrtab. | |
a2fb1b05 ILT |
966 | |
967 | // Get the appropriate symbol table header (this will normally be | |
968 | // the single SHT_SYMTAB section, but in principle it need not be). | |
d491d34e | 969 | const unsigned int link = this->adjust_shndx(shdr.get_sh_link()); |
645f8123 | 970 | typename This::Shdr symshdr(this, this->elf_file_.section_header(link)); |
a2fb1b05 ILT |
971 | |
972 | // Read the symbol table entry. | |
d491d34e ILT |
973 | unsigned int symndx = shdr.get_sh_info(); |
974 | if (symndx >= symshdr.get_sh_size() / This::sym_size) | |
a2fb1b05 | 975 | { |
75f2446e | 976 | this->error(_("section group %u info %u out of range"), |
d491d34e | 977 | index, symndx); |
75f2446e | 978 | return false; |
a2fb1b05 | 979 | } |
d491d34e | 980 | off_t symoff = symshdr.get_sh_offset() + symndx * This::sym_size; |
39d0cb0e ILT |
981 | const unsigned char* psym = this->get_view(symoff, This::sym_size, true, |
982 | false); | |
a2fb1b05 ILT |
983 | elfcpp::Sym<size, big_endian> sym(psym); |
984 | ||
a2fb1b05 | 985 | // Read the symbol table names. |
8383303e | 986 | section_size_type symnamelen; |
645f8123 | 987 | const unsigned char* psymnamesu; |
d491d34e ILT |
988 | psymnamesu = this->section_contents(this->adjust_shndx(symshdr.get_sh_link()), |
989 | &symnamelen, true); | |
a2fb1b05 ILT |
990 | const char* psymnames = reinterpret_cast<const char*>(psymnamesu); |
991 | ||
992 | // Get the section group signature. | |
645f8123 | 993 | if (sym.get_st_name() >= symnamelen) |
a2fb1b05 | 994 | { |
75f2446e | 995 | this->error(_("symbol %u name offset %u out of range"), |
d491d34e | 996 | symndx, sym.get_st_name()); |
75f2446e | 997 | return false; |
a2fb1b05 ILT |
998 | } |
999 | ||
e94cf127 | 1000 | std::string signature(psymnames + sym.get_st_name()); |
a2fb1b05 | 1001 | |
ead1e424 ILT |
1002 | // It seems that some versions of gas will create a section group |
1003 | // associated with a section symbol, and then fail to give a name to | |
1004 | // the section symbol. In such a case, use the name of the section. | |
645f8123 | 1005 | if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION) |
ead1e424 | 1006 | { |
d491d34e ILT |
1007 | bool is_ordinary; |
1008 | unsigned int sym_shndx = this->adjust_sym_shndx(symndx, | |
1009 | sym.get_st_shndx(), | |
1010 | &is_ordinary); | |
1011 | if (!is_ordinary || sym_shndx >= this->shnum()) | |
1012 | { | |
1013 | this->error(_("symbol %u invalid section index %u"), | |
1014 | symndx, sym_shndx); | |
1015 | return false; | |
1016 | } | |
e94cf127 CC |
1017 | typename This::Shdr member_shdr(shdrs + sym_shndx * This::shdr_size); |
1018 | if (member_shdr.get_sh_name() < section_names_size) | |
2e702c99 | 1019 | signature = section_names + member_shdr.get_sh_name(); |
ead1e424 ILT |
1020 | } |
1021 | ||
e94cf127 CC |
1022 | // Record this section group in the layout, and see whether we've already |
1023 | // seen one with the same signature. | |
8a4c0b0d | 1024 | bool include_group; |
1ef4d87f ILT |
1025 | bool is_comdat; |
1026 | Kept_section* kept_section = NULL; | |
6a74a719 | 1027 | |
8a4c0b0d | 1028 | if ((flags & elfcpp::GRP_COMDAT) == 0) |
1ef4d87f ILT |
1029 | { |
1030 | include_group = true; | |
1031 | is_comdat = false; | |
1032 | } | |
8a4c0b0d | 1033 | else |
e94cf127 | 1034 | { |
2ea97941 ILT |
1035 | include_group = layout->find_or_add_kept_section(signature, |
1036 | this, index, true, | |
1037 | true, &kept_section); | |
1ef4d87f | 1038 | is_comdat = true; |
6a74a719 | 1039 | } |
a2fb1b05 | 1040 | |
89d8a36b CC |
1041 | if (is_comdat && include_group) |
1042 | { | |
1043 | Incremental_inputs* incremental_inputs = layout->incremental_inputs(); | |
1044 | if (incremental_inputs != NULL) | |
1045 | incremental_inputs->report_comdat_group(this, signature.c_str()); | |
1046 | } | |
1047 | ||
a2fb1b05 | 1048 | size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word); |
8825ac63 ILT |
1049 | |
1050 | std::vector<unsigned int> shndxes; | |
1051 | bool relocate_group = include_group && parameters->options().relocatable(); | |
1052 | if (relocate_group) | |
1053 | shndxes.reserve(count - 1); | |
1054 | ||
a2fb1b05 ILT |
1055 | for (size_t i = 1; i < count; ++i) |
1056 | { | |
1ef4d87f | 1057 | elfcpp::Elf_Word shndx = |
8825ac63 ILT |
1058 | this->adjust_shndx(elfcpp::Swap<32, big_endian>::readval(pword + i)); |
1059 | ||
1060 | if (relocate_group) | |
1ef4d87f | 1061 | shndxes.push_back(shndx); |
8825ac63 | 1062 | |
1ef4d87f | 1063 | if (shndx >= this->shnum()) |
a2fb1b05 | 1064 | { |
75f2446e | 1065 | this->error(_("section %u in section group %u out of range"), |
1ef4d87f | 1066 | shndx, index); |
75f2446e | 1067 | continue; |
a2fb1b05 | 1068 | } |
55438702 ILT |
1069 | |
1070 | // Check for an earlier section number, since we're going to get | |
1071 | // it wrong--we may have already decided to include the section. | |
1ef4d87f | 1072 | if (shndx < index) |
2e702c99 RM |
1073 | this->error(_("invalid section group %u refers to earlier section %u"), |
1074 | index, shndx); | |
55438702 | 1075 | |
e94cf127 | 1076 | // Get the name of the member section. |
1ef4d87f | 1077 | typename This::Shdr member_shdr(shdrs + shndx * This::shdr_size); |
e94cf127 | 1078 | if (member_shdr.get_sh_name() >= section_names_size) |
2e702c99 RM |
1079 | { |
1080 | // This is an error, but it will be diagnosed eventually | |
1081 | // in do_layout, so we don't need to do anything here but | |
1082 | // ignore it. | |
1083 | continue; | |
1084 | } | |
e94cf127 CC |
1085 | std::string mname(section_names + member_shdr.get_sh_name()); |
1086 | ||
1ef4d87f ILT |
1087 | if (include_group) |
1088 | { | |
1089 | if (is_comdat) | |
1090 | kept_section->add_comdat_section(mname, shndx, | |
1091 | member_shdr.get_sh_size()); | |
1092 | } | |
1093 | else | |
2e702c99 RM |
1094 | { |
1095 | (*omit)[shndx] = true; | |
1ef4d87f ILT |
1096 | |
1097 | if (is_comdat) | |
2e702c99 | 1098 | { |
1ef4d87f ILT |
1099 | Relobj* kept_object = kept_section->object(); |
1100 | if (kept_section->is_comdat()) | |
1101 | { | |
1102 | // Find the corresponding kept section, and store | |
1103 | // that info in the discarded section table. | |
1104 | unsigned int kept_shndx; | |
1105 | uint64_t kept_size; | |
1106 | if (kept_section->find_comdat_section(mname, &kept_shndx, | |
1107 | &kept_size)) | |
1108 | { | |
1109 | // We don't keep a mapping for this section if | |
1110 | // it has a different size. The mapping is only | |
1111 | // used for relocation processing, and we don't | |
1112 | // want to treat the sections as similar if the | |
1113 | // sizes are different. Checking the section | |
1114 | // size is the approach used by the GNU linker. | |
1115 | if (kept_size == member_shdr.get_sh_size()) | |
1116 | this->set_kept_comdat_section(shndx, kept_object, | |
1117 | kept_shndx); | |
1118 | } | |
1119 | } | |
1120 | else | |
1121 | { | |
1122 | // The existing section is a linkonce section. Add | |
1123 | // a mapping if there is exactly one section in the | |
1124 | // group (which is true when COUNT == 2) and if it | |
1125 | // is the same size. | |
1126 | if (count == 2 | |
1127 | && (kept_section->linkonce_size() | |
1128 | == member_shdr.get_sh_size())) | |
1129 | this->set_kept_comdat_section(shndx, kept_object, | |
1130 | kept_section->shndx()); | |
1131 | } | |
2e702c99 RM |
1132 | } |
1133 | } | |
a2fb1b05 ILT |
1134 | } |
1135 | ||
8825ac63 | 1136 | if (relocate_group) |
2ea97941 ILT |
1137 | layout->layout_group(symtab, this, index, name, signature.c_str(), |
1138 | shdr, flags, &shndxes); | |
8825ac63 | 1139 | |
e94cf127 | 1140 | return include_group; |
a2fb1b05 ILT |
1141 | } |
1142 | ||
1143 | // Whether to include a linkonce section in the link. NAME is the | |
1144 | // name of the section and SHDR is the section header. | |
1145 | ||
1146 | // Linkonce sections are a GNU extension implemented in the original | |
1147 | // GNU linker before section groups were defined. The semantics are | |
1148 | // that we only include one linkonce section with a given name. The | |
1149 | // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME, | |
1150 | // where T is the type of section and SYMNAME is the name of a symbol. | |
1151 | // In an attempt to make linkonce sections interact well with section | |
1152 | // groups, we try to identify SYMNAME and use it like a section group | |
1153 | // signature. We want to block section groups with that signature, | |
1154 | // but not other linkonce sections with that signature. We also use | |
1155 | // the full name of the linkonce section as a normal section group | |
1156 | // signature. | |
1157 | ||
1158 | template<int size, bool big_endian> | |
1159 | bool | |
6fa2a40b | 1160 | Sized_relobj_file<size, big_endian>::include_linkonce_section( |
2ea97941 | 1161 | Layout* layout, |
e94cf127 | 1162 | unsigned int index, |
2ea97941 | 1163 | const char* name, |
1ef4d87f | 1164 | const elfcpp::Shdr<size, big_endian>& shdr) |
a2fb1b05 | 1165 | { |
1ef4d87f | 1166 | typename elfcpp::Elf_types<size>::Elf_WXword sh_size = shdr.get_sh_size(); |
ad435a24 ILT |
1167 | // In general the symbol name we want will be the string following |
1168 | // the last '.'. However, we have to handle the case of | |
1169 | // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by | |
1170 | // some versions of gcc. So we use a heuristic: if the name starts | |
1171 | // with ".gnu.linkonce.t.", we use everything after that. Otherwise | |
1172 | // we look for the last '.'. We can't always simply skip | |
1173 | // ".gnu.linkonce.X", because we have to deal with cases like | |
1174 | // ".gnu.linkonce.d.rel.ro.local". | |
1175 | const char* const linkonce_t = ".gnu.linkonce.t."; | |
1176 | const char* symname; | |
2ea97941 ILT |
1177 | if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0) |
1178 | symname = name + strlen(linkonce_t); | |
ad435a24 | 1179 | else |
2ea97941 | 1180 | symname = strrchr(name, '.') + 1; |
e94cf127 | 1181 | std::string sig1(symname); |
2ea97941 | 1182 | std::string sig2(name); |
8a4c0b0d ILT |
1183 | Kept_section* kept1; |
1184 | Kept_section* kept2; | |
2ea97941 ILT |
1185 | bool include1 = layout->find_or_add_kept_section(sig1, this, index, false, |
1186 | false, &kept1); | |
1187 | bool include2 = layout->find_or_add_kept_section(sig2, this, index, false, | |
1188 | true, &kept2); | |
e94cf127 CC |
1189 | |
1190 | if (!include2) | |
1191 | { | |
1ef4d87f ILT |
1192 | // We are not including this section because we already saw the |
1193 | // name of the section as a signature. This normally implies | |
1194 | // that the kept section is another linkonce section. If it is | |
1195 | // the same size, record it as the section which corresponds to | |
1196 | // this one. | |
1197 | if (kept2->object() != NULL | |
1198 | && !kept2->is_comdat() | |
1199 | && kept2->linkonce_size() == sh_size) | |
1200 | this->set_kept_comdat_section(index, kept2->object(), kept2->shndx()); | |
e94cf127 CC |
1201 | } |
1202 | else if (!include1) | |
1203 | { | |
1204 | // The section is being discarded on the basis of its symbol | |
1205 | // name. This means that the corresponding kept section was | |
1206 | // part of a comdat group, and it will be difficult to identify | |
1207 | // the specific section within that group that corresponds to | |
1208 | // this linkonce section. We'll handle the simple case where | |
1209 | // the group has only one member section. Otherwise, it's not | |
1210 | // worth the effort. | |
1ef4d87f ILT |
1211 | unsigned int kept_shndx; |
1212 | uint64_t kept_size; | |
1213 | if (kept1->object() != NULL | |
1214 | && kept1->is_comdat() | |
1215 | && kept1->find_single_comdat_section(&kept_shndx, &kept_size) | |
1216 | && kept_size == sh_size) | |
1217 | this->set_kept_comdat_section(index, kept1->object(), kept_shndx); | |
1218 | } | |
1219 | else | |
1220 | { | |
1221 | kept1->set_linkonce_size(sh_size); | |
1222 | kept2->set_linkonce_size(sh_size); | |
e94cf127 CC |
1223 | } |
1224 | ||
a783673b | 1225 | return include1 && include2; |
a2fb1b05 ILT |
1226 | } |
1227 | ||
5995b570 CC |
1228 | // Layout an input section. |
1229 | ||
1230 | template<int size, bool big_endian> | |
1231 | inline void | |
14788a3f ILT |
1232 | Sized_relobj_file<size, big_endian>::layout_section( |
1233 | Layout* layout, | |
1234 | unsigned int shndx, | |
1235 | const char* name, | |
1236 | const typename This::Shdr& shdr, | |
1237 | unsigned int reloc_shndx, | |
1238 | unsigned int reloc_type) | |
5995b570 | 1239 | { |
2ea97941 ILT |
1240 | off_t offset; |
1241 | Output_section* os = layout->layout(this, shndx, name, shdr, | |
1242 | reloc_shndx, reloc_type, &offset); | |
5995b570 CC |
1243 | |
1244 | this->output_sections()[shndx] = os; | |
2ea97941 | 1245 | if (offset == -1) |
6fa2a40b | 1246 | this->section_offsets()[shndx] = invalid_address; |
5995b570 | 1247 | else |
6fa2a40b | 1248 | this->section_offsets()[shndx] = convert_types<Address, off_t>(offset); |
5995b570 CC |
1249 | |
1250 | // If this section requires special handling, and if there are | |
1251 | // relocs that apply to it, then we must do the special handling | |
1252 | // before we apply the relocs. | |
2ea97941 | 1253 | if (offset == -1 && reloc_shndx != 0) |
5995b570 CC |
1254 | this->set_relocs_must_follow_section_writes(); |
1255 | } | |
1256 | ||
14788a3f ILT |
1257 | // Layout an input .eh_frame section. |
1258 | ||
1259 | template<int size, bool big_endian> | |
1260 | void | |
1261 | Sized_relobj_file<size, big_endian>::layout_eh_frame_section( | |
1262 | Layout* layout, | |
1263 | const unsigned char* symbols_data, | |
1264 | section_size_type symbols_size, | |
1265 | const unsigned char* symbol_names_data, | |
1266 | section_size_type symbol_names_size, | |
1267 | unsigned int shndx, | |
1268 | const typename This::Shdr& shdr, | |
1269 | unsigned int reloc_shndx, | |
1270 | unsigned int reloc_type) | |
1271 | { | |
1272 | gold_assert(this->has_eh_frame_); | |
1273 | ||
1274 | off_t offset; | |
1275 | Output_section* os = layout->layout_eh_frame(this, | |
1276 | symbols_data, | |
1277 | symbols_size, | |
1278 | symbol_names_data, | |
1279 | symbol_names_size, | |
1280 | shndx, | |
1281 | shdr, | |
1282 | reloc_shndx, | |
1283 | reloc_type, | |
1284 | &offset); | |
1285 | this->output_sections()[shndx] = os; | |
1286 | if (os == NULL || offset == -1) | |
1287 | { | |
1288 | // An object can contain at most one section holding exception | |
1289 | // frame information. | |
1290 | gold_assert(this->discarded_eh_frame_shndx_ == -1U); | |
1291 | this->discarded_eh_frame_shndx_ = shndx; | |
1292 | this->section_offsets()[shndx] = invalid_address; | |
1293 | } | |
1294 | else | |
1295 | this->section_offsets()[shndx] = convert_types<Address, off_t>(offset); | |
1296 | ||
1297 | // If this section requires special handling, and if there are | |
1298 | // relocs that aply to it, then we must do the special handling | |
1299 | // before we apply the relocs. | |
1300 | if (os != NULL && offset == -1 && reloc_shndx != 0) | |
1301 | this->set_relocs_must_follow_section_writes(); | |
1302 | } | |
1303 | ||
a2fb1b05 ILT |
1304 | // Lay out the input sections. We walk through the sections and check |
1305 | // whether they should be included in the link. If they should, we | |
1306 | // pass them to the Layout object, which will return an output section | |
2e702c99 | 1307 | // and an offset. |
16164a6b ST |
1308 | // This function is called twice sometimes, two passes, when mapping |
1309 | // of input sections to output sections must be delayed. | |
1310 | // This is true for the following : | |
1311 | // * Garbage collection (--gc-sections): Some input sections will be | |
1312 | // discarded and hence the assignment must wait until the second pass. | |
1313 | // In the first pass, it is for setting up some sections as roots to | |
1314 | // a work-list for --gc-sections and to do comdat processing. | |
1315 | // * Identical Code Folding (--icf=<safe,all>): Some input sections | |
1316 | // will be folded and hence the assignment must wait. | |
1317 | // * Using plugins to map some sections to unique segments: Mapping | |
1318 | // some sections to unique segments requires mapping them to unique | |
1319 | // output sections too. This can be done via plugins now and this | |
1320 | // information is not available in the first pass. | |
a2fb1b05 ILT |
1321 | |
1322 | template<int size, bool big_endian> | |
1323 | void | |
6fa2a40b CC |
1324 | Sized_relobj_file<size, big_endian>::do_layout(Symbol_table* symtab, |
1325 | Layout* layout, | |
1326 | Read_symbols_data* sd) | |
a2fb1b05 | 1327 | { |
2ea97941 | 1328 | const unsigned int shnum = this->shnum(); |
2e702c99 | 1329 | |
16164a6b ST |
1330 | /* Should this function be called twice? */ |
1331 | bool is_two_pass = (parameters->options().gc_sections() | |
1332 | || parameters->options().icf_enabled() | |
1333 | || layout->is_unique_segment_for_sections_specified()); | |
ef15dade | 1334 | |
16164a6b ST |
1335 | /* Only one of is_pass_one and is_pass_two is true. Both are false when |
1336 | a two-pass approach is not needed. */ | |
1337 | bool is_pass_one = false; | |
1338 | bool is_pass_two = false; | |
ef15dade | 1339 | |
16164a6b | 1340 | Symbols_data* gc_sd = NULL; |
ef15dade | 1341 | |
16164a6b ST |
1342 | /* Check if do_layout needs to be two-pass. If so, find out which pass |
1343 | should happen. In the first pass, the data in sd is saved to be used | |
1344 | later in the second pass. */ | |
1345 | if (is_two_pass) | |
1346 | { | |
1347 | gc_sd = this->get_symbols_data(); | |
1348 | if (gc_sd == NULL) | |
1349 | { | |
1350 | gold_assert(sd != NULL); | |
1351 | is_pass_one = true; | |
1352 | } | |
1353 | else | |
1354 | { | |
1355 | if (parameters->options().gc_sections()) | |
1356 | gold_assert(symtab->gc()->is_worklist_ready()); | |
1357 | if (parameters->options().icf_enabled()) | |
1358 | gold_assert(symtab->icf()->is_icf_ready()); | |
1359 | is_pass_two = true; | |
1360 | } | |
1361 | } | |
1362 | ||
2ea97941 | 1363 | if (shnum == 0) |
12e14209 | 1364 | return; |
16164a6b ST |
1365 | |
1366 | if (is_pass_one) | |
6d03d481 | 1367 | { |
2e702c99 RM |
1368 | // During garbage collection save the symbols data to use it when |
1369 | // re-entering this function. | |
6d03d481 | 1370 | gc_sd = new Symbols_data; |
2ea97941 | 1371 | this->copy_symbols_data(gc_sd, sd, This::shdr_size * shnum); |
6d03d481 ST |
1372 | this->set_symbols_data(gc_sd); |
1373 | } | |
6d03d481 ST |
1374 | |
1375 | const unsigned char* section_headers_data = NULL; | |
1376 | section_size_type section_names_size; | |
1377 | const unsigned char* symbols_data = NULL; | |
1378 | section_size_type symbols_size; | |
6d03d481 ST |
1379 | const unsigned char* symbol_names_data = NULL; |
1380 | section_size_type symbol_names_size; | |
2e702c99 | 1381 | |
16164a6b | 1382 | if (is_two_pass) |
6d03d481 ST |
1383 | { |
1384 | section_headers_data = gc_sd->section_headers_data; | |
1385 | section_names_size = gc_sd->section_names_size; | |
1386 | symbols_data = gc_sd->symbols_data; | |
1387 | symbols_size = gc_sd->symbols_size; | |
6d03d481 ST |
1388 | symbol_names_data = gc_sd->symbol_names_data; |
1389 | symbol_names_size = gc_sd->symbol_names_size; | |
1390 | } | |
1391 | else | |
1392 | { | |
1393 | section_headers_data = sd->section_headers->data(); | |
1394 | section_names_size = sd->section_names_size; | |
1395 | if (sd->symbols != NULL) | |
2e702c99 | 1396 | symbols_data = sd->symbols->data(); |
6d03d481 | 1397 | symbols_size = sd->symbols_size; |
6d03d481 | 1398 | if (sd->symbol_names != NULL) |
2e702c99 | 1399 | symbol_names_data = sd->symbol_names->data(); |
6d03d481 ST |
1400 | symbol_names_size = sd->symbol_names_size; |
1401 | } | |
a2fb1b05 ILT |
1402 | |
1403 | // Get the section headers. | |
6d03d481 | 1404 | const unsigned char* shdrs = section_headers_data; |
e94cf127 | 1405 | const unsigned char* pshdrs; |
a2fb1b05 ILT |
1406 | |
1407 | // Get the section names. | |
16164a6b ST |
1408 | const unsigned char* pnamesu = (is_two_pass |
1409 | ? gc_sd->section_names_data | |
1410 | : sd->section_names->data()); | |
ef15dade | 1411 | |
a2fb1b05 ILT |
1412 | const char* pnames = reinterpret_cast<const char*>(pnamesu); |
1413 | ||
5995b570 CC |
1414 | // If any input files have been claimed by plugins, we need to defer |
1415 | // actual layout until the replacement files have arrived. | |
1416 | const bool should_defer_layout = | |
1417 | (parameters->options().has_plugins() | |
1418 | && parameters->options().plugins()->should_defer_layout()); | |
1419 | unsigned int num_sections_to_defer = 0; | |
1420 | ||
730cdc88 ILT |
1421 | // For each section, record the index of the reloc section if any. |
1422 | // Use 0 to mean that there is no reloc section, -1U to mean that | |
1423 | // there is more than one. | |
2ea97941 ILT |
1424 | std::vector<unsigned int> reloc_shndx(shnum, 0); |
1425 | std::vector<unsigned int> reloc_type(shnum, elfcpp::SHT_NULL); | |
730cdc88 | 1426 | // Skip the first, dummy, section. |
e94cf127 | 1427 | pshdrs = shdrs + This::shdr_size; |
2ea97941 | 1428 | for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size) |
730cdc88 ILT |
1429 | { |
1430 | typename This::Shdr shdr(pshdrs); | |
1431 | ||
5995b570 CC |
1432 | // Count the number of sections whose layout will be deferred. |
1433 | if (should_defer_layout && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC)) | |
2e702c99 | 1434 | ++num_sections_to_defer; |
5995b570 | 1435 | |
730cdc88 ILT |
1436 | unsigned int sh_type = shdr.get_sh_type(); |
1437 | if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA) | |
1438 | { | |
d491d34e | 1439 | unsigned int target_shndx = this->adjust_shndx(shdr.get_sh_info()); |
2ea97941 | 1440 | if (target_shndx == 0 || target_shndx >= shnum) |
730cdc88 ILT |
1441 | { |
1442 | this->error(_("relocation section %u has bad info %u"), | |
1443 | i, target_shndx); | |
1444 | continue; | |
1445 | } | |
1446 | ||
1447 | if (reloc_shndx[target_shndx] != 0) | |
1448 | reloc_shndx[target_shndx] = -1U; | |
1449 | else | |
1450 | { | |
1451 | reloc_shndx[target_shndx] = i; | |
1452 | reloc_type[target_shndx] = sh_type; | |
1453 | } | |
1454 | } | |
1455 | } | |
1456 | ||
ef9beddf | 1457 | Output_sections& out_sections(this->output_sections()); |
6fa2a40b | 1458 | std::vector<Address>& out_section_offsets(this->section_offsets()); |
ef9beddf | 1459 | |
16164a6b | 1460 | if (!is_pass_two) |
6d03d481 | 1461 | { |
2ea97941 ILT |
1462 | out_sections.resize(shnum); |
1463 | out_section_offsets.resize(shnum); | |
6d03d481 | 1464 | } |
a2fb1b05 | 1465 | |
88dd47ac ILT |
1466 | // If we are only linking for symbols, then there is nothing else to |
1467 | // do here. | |
1468 | if (this->input_file()->just_symbols()) | |
1469 | { | |
16164a6b | 1470 | if (!is_pass_two) |
2e702c99 RM |
1471 | { |
1472 | delete sd->section_headers; | |
1473 | sd->section_headers = NULL; | |
1474 | delete sd->section_names; | |
1475 | sd->section_names = NULL; | |
1476 | } | |
88dd47ac ILT |
1477 | return; |
1478 | } | |
1479 | ||
5995b570 CC |
1480 | if (num_sections_to_defer > 0) |
1481 | { | |
1482 | parameters->options().plugins()->add_deferred_layout_object(this); | |
1483 | this->deferred_layout_.reserve(num_sections_to_defer); | |
c924eb67 | 1484 | this->is_deferred_layout_ = true; |
5995b570 CC |
1485 | } |
1486 | ||
35cdfc9a ILT |
1487 | // Whether we've seen a .note.GNU-stack section. |
1488 | bool seen_gnu_stack = false; | |
1489 | // The flags of a .note.GNU-stack section. | |
1490 | uint64_t gnu_stack_flags = 0; | |
1491 | ||
a2fb1b05 | 1492 | // Keep track of which sections to omit. |
2ea97941 | 1493 | std::vector<bool> omit(shnum, false); |
a2fb1b05 | 1494 | |
7019cd25 | 1495 | // Keep track of reloc sections when emitting relocations. |
8851ecca | 1496 | const bool relocatable = parameters->options().relocatable(); |
2ea97941 ILT |
1497 | const bool emit_relocs = (relocatable |
1498 | || parameters->options().emit_relocs()); | |
6a74a719 ILT |
1499 | std::vector<unsigned int> reloc_sections; |
1500 | ||
730cdc88 ILT |
1501 | // Keep track of .eh_frame sections. |
1502 | std::vector<unsigned int> eh_frame_sections; | |
1503 | ||
c1027032 CC |
1504 | // Keep track of .debug_info and .debug_types sections. |
1505 | std::vector<unsigned int> debug_info_sections; | |
1506 | std::vector<unsigned int> debug_types_sections; | |
1507 | ||
f6ce93d6 | 1508 | // Skip the first, dummy, section. |
e94cf127 | 1509 | pshdrs = shdrs + This::shdr_size; |
2ea97941 | 1510 | for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size) |
a2fb1b05 | 1511 | { |
75f65a3e | 1512 | typename This::Shdr shdr(pshdrs); |
a2fb1b05 | 1513 | |
6d03d481 | 1514 | if (shdr.get_sh_name() >= section_names_size) |
a2fb1b05 | 1515 | { |
75f2446e ILT |
1516 | this->error(_("bad section name offset for section %u: %lu"), |
1517 | i, static_cast<unsigned long>(shdr.get_sh_name())); | |
1518 | return; | |
a2fb1b05 ILT |
1519 | } |
1520 | ||
2ea97941 | 1521 | const char* name = pnames + shdr.get_sh_name(); |
a2fb1b05 | 1522 | |
16164a6b | 1523 | if (!is_pass_two) |
2e702c99 RM |
1524 | { |
1525 | if (this->handle_gnu_warning_section(name, i, symtab)) | |
1526 | { | |
1527 | if (!relocatable && !parameters->options().shared()) | |
1528 | omit[i] = true; | |
6d03d481 | 1529 | } |
f6ce93d6 | 1530 | |
2e702c99 RM |
1531 | // The .note.GNU-stack section is special. It gives the |
1532 | // protection flags that this object file requires for the stack | |
1533 | // in memory. | |
1534 | if (strcmp(name, ".note.GNU-stack") == 0) | |
1535 | { | |
6d03d481 ST |
1536 | seen_gnu_stack = true; |
1537 | gnu_stack_flags |= shdr.get_sh_flags(); | |
1538 | omit[i] = true; | |
2e702c99 | 1539 | } |
35cdfc9a | 1540 | |
364c7fa5 ILT |
1541 | // The .note.GNU-split-stack section is also special. It |
1542 | // indicates that the object was compiled with | |
1543 | // -fsplit-stack. | |
2ea97941 | 1544 | if (this->handle_split_stack_section(name)) |
364c7fa5 | 1545 | { |
e588ea8d | 1546 | if (!relocatable && !parameters->options().shared()) |
364c7fa5 ILT |
1547 | omit[i] = true; |
1548 | } | |
1549 | ||
05a352e6 | 1550 | // Skip attributes section. |
2ea97941 | 1551 | if (parameters->target().is_attributes_section(name)) |
05a352e6 DK |
1552 | { |
1553 | omit[i] = true; | |
1554 | } | |
1555 | ||
2e702c99 RM |
1556 | bool discard = omit[i]; |
1557 | if (!discard) | |
1558 | { | |
6d03d481 | 1559 | if (shdr.get_sh_type() == elfcpp::SHT_GROUP) |
2e702c99 RM |
1560 | { |
1561 | if (!this->include_section_group(symtab, layout, i, name, | |
1562 | shdrs, pnames, | |
1563 | section_names_size, | |
1564 | &omit)) | |
1565 | discard = true; | |
1566 | } | |
1567 | else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0 | |
1568 | && Layout::is_linkonce(name)) | |
1569 | { | |
1570 | if (!this->include_linkonce_section(layout, i, name, shdr)) | |
6d03d481 | 1571 | discard = true; |
2e702c99 | 1572 | } |
a2fb1b05 | 1573 | } |
a2fb1b05 | 1574 | |
09ec0418 CC |
1575 | // Add the section to the incremental inputs layout. |
1576 | Incremental_inputs* incremental_inputs = layout->incremental_inputs(); | |
cdc29364 CC |
1577 | if (incremental_inputs != NULL |
1578 | && !discard | |
aa06ae28 | 1579 | && can_incremental_update(shdr.get_sh_type())) |
4fb3a1c3 CC |
1580 | { |
1581 | off_t sh_size = shdr.get_sh_size(); | |
1582 | section_size_type uncompressed_size; | |
1583 | if (this->section_is_compressed(i, &uncompressed_size)) | |
1584 | sh_size = uncompressed_size; | |
1585 | incremental_inputs->report_input_section(this, i, name, sh_size); | |
1586 | } | |
09ec0418 | 1587 | |
2e702c99 RM |
1588 | if (discard) |
1589 | { | |
6d03d481 ST |
1590 | // Do not include this section in the link. |
1591 | out_sections[i] = NULL; | |
2e702c99 | 1592 | out_section_offsets[i] = invalid_address; |
6d03d481 | 1593 | continue; |
2e702c99 RM |
1594 | } |
1595 | } | |
1596 | ||
16164a6b | 1597 | if (is_pass_one && parameters->options().gc_sections()) |
2e702c99 RM |
1598 | { |
1599 | if (this->is_section_name_included(name) | |
b9b2ae8b | 1600 | || layout->keep_input_section (this, name) |
2e702c99 RM |
1601 | || shdr.get_sh_type() == elfcpp::SHT_INIT_ARRAY |
1602 | || shdr.get_sh_type() == elfcpp::SHT_FINI_ARRAY) | |
1603 | { | |
1604 | symtab->gc()->worklist().push(Section_id(this, i)); | |
1605 | } | |
1606 | // If the section name XXX can be represented as a C identifier | |
1607 | // it cannot be discarded if there are references to | |
1608 | // __start_XXX and __stop_XXX symbols. These need to be | |
1609 | // specially handled. | |
1610 | if (is_cident(name)) | |
1611 | { | |
1612 | symtab->gc()->add_cident_section(name, Section_id(this, i)); | |
1613 | } | |
1614 | } | |
a2fb1b05 | 1615 | |
6a74a719 ILT |
1616 | // When doing a relocatable link we are going to copy input |
1617 | // reloc sections into the output. We only want to copy the | |
1618 | // ones associated with sections which are not being discarded. | |
1619 | // However, we don't know that yet for all sections. So save | |
6d03d481 ST |
1620 | // reloc sections and process them later. Garbage collection is |
1621 | // not triggered when relocatable code is desired. | |
2ea97941 | 1622 | if (emit_relocs |
6a74a719 ILT |
1623 | && (shdr.get_sh_type() == elfcpp::SHT_REL |
1624 | || shdr.get_sh_type() == elfcpp::SHT_RELA)) | |
1625 | { | |
1626 | reloc_sections.push_back(i); | |
1627 | continue; | |
1628 | } | |
1629 | ||
8851ecca | 1630 | if (relocatable && shdr.get_sh_type() == elfcpp::SHT_GROUP) |
6a74a719 ILT |
1631 | continue; |
1632 | ||
730cdc88 ILT |
1633 | // The .eh_frame section is special. It holds exception frame |
1634 | // information that we need to read in order to generate the | |
1635 | // exception frame header. We process these after all the other | |
1636 | // sections so that the exception frame reader can reliably | |
1637 | // determine which sections are being discarded, and discard the | |
1638 | // corresponding information. | |
8851ecca | 1639 | if (!relocatable |
2e702c99 RM |
1640 | && strcmp(name, ".eh_frame") == 0 |
1641 | && this->check_eh_frame_flags(&shdr)) | |
1642 | { | |
16164a6b | 1643 | if (is_pass_one) |
2e702c99 | 1644 | { |
c924eb67 CC |
1645 | if (this->is_deferred_layout()) |
1646 | out_sections[i] = reinterpret_cast<Output_section*>(2); | |
1647 | else | |
1648 | out_sections[i] = reinterpret_cast<Output_section*>(1); | |
2e702c99 RM |
1649 | out_section_offsets[i] = invalid_address; |
1650 | } | |
c924eb67 | 1651 | else if (this->is_deferred_layout()) |
14788a3f ILT |
1652 | this->deferred_layout_.push_back(Deferred_layout(i, name, |
1653 | pshdrs, | |
1654 | reloc_shndx[i], | |
1655 | reloc_type[i])); | |
1656 | else | |
2e702c99 RM |
1657 | eh_frame_sections.push_back(i); |
1658 | continue; | |
1659 | } | |
730cdc88 | 1660 | |
16164a6b | 1661 | if (is_pass_two && parameters->options().gc_sections()) |
2e702c99 RM |
1662 | { |
1663 | // This is executed during the second pass of garbage | |
1664 | // collection. do_layout has been called before and some | |
1665 | // sections have been already discarded. Simply ignore | |
1666 | // such sections this time around. | |
1667 | if (out_sections[i] == NULL) | |
1668 | { | |
1669 | gold_assert(out_section_offsets[i] == invalid_address); | |
1670 | continue; | |
1671 | } | |
1672 | if (((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0) | |
1673 | && symtab->gc()->is_section_garbage(this, i)) | |
1674 | { | |
1675 | if (parameters->options().print_gc_sections()) | |
1676 | gold_info(_("%s: removing unused section from '%s'" | |
1677 | " in file '%s'"), | |
1678 | program_name, this->section_name(i).c_str(), | |
1679 | this->name().c_str()); | |
1680 | out_sections[i] = NULL; | |
1681 | out_section_offsets[i] = invalid_address; | |
1682 | continue; | |
1683 | } | |
1684 | } | |
ef15dade | 1685 | |
16164a6b | 1686 | if (is_pass_two && parameters->options().icf_enabled()) |
2e702c99 RM |
1687 | { |
1688 | if (out_sections[i] == NULL) | |
1689 | { | |
1690 | gold_assert(out_section_offsets[i] == invalid_address); | |
1691 | continue; | |
1692 | } | |
1693 | if (((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0) | |
1694 | && symtab->icf()->is_section_folded(this, i)) | |
1695 | { | |
1696 | if (parameters->options().print_icf_sections()) | |
1697 | { | |
1698 | Section_id folded = | |
1699 | symtab->icf()->get_folded_section(this, i); | |
1700 | Relobj* folded_obj = | |
1701 | reinterpret_cast<Relobj*>(folded.first); | |
53c66605 | 1702 | gold_info(_("%s: ICF folding section '%s' in file '%s' " |
2e702c99 RM |
1703 | "into '%s' in file '%s'"), |
1704 | program_name, this->section_name(i).c_str(), | |
1705 | this->name().c_str(), | |
1706 | folded_obj->section_name(folded.second).c_str(), | |
1707 | folded_obj->name().c_str()); | |
1708 | } | |
1709 | out_sections[i] = NULL; | |
1710 | out_section_offsets[i] = invalid_address; | |
1711 | continue; | |
1712 | } | |
1713 | } | |
ef15dade | 1714 | |
6d03d481 | 1715 | // Defer layout here if input files are claimed by plugins. When gc |
c924eb67 CC |
1716 | // is turned on this function is called twice; we only want to do this |
1717 | // on the first pass. | |
1718 | if (!is_pass_two | |
1719 | && this->is_deferred_layout() | |
1720 | && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC)) | |
2e702c99 | 1721 | { |
2e702c99 RM |
1722 | this->deferred_layout_.push_back(Deferred_layout(i, name, |
1723 | pshdrs, | |
1724 | reloc_shndx[i], | |
1725 | reloc_type[i])); | |
1726 | // Put dummy values here; real values will be supplied by | |
1727 | // do_layout_deferred_sections. | |
1728 | out_sections[i] = reinterpret_cast<Output_section*>(2); | |
1729 | out_section_offsets[i] = invalid_address; | |
1730 | continue; | |
1731 | } | |
ef15dade | 1732 | |
6d03d481 ST |
1733 | // During gc_pass_two if a section that was previously deferred is |
1734 | // found, do not layout the section as layout_deferred_sections will | |
1735 | // do it later from gold.cc. | |
16164a6b | 1736 | if (is_pass_two |
2e702c99 RM |
1737 | && (out_sections[i] == reinterpret_cast<Output_section*>(2))) |
1738 | continue; | |
6d03d481 | 1739 | |
16164a6b | 1740 | if (is_pass_one) |
2e702c99 RM |
1741 | { |
1742 | // This is during garbage collection. The out_sections are | |
1743 | // assigned in the second call to this function. | |
1744 | out_sections[i] = reinterpret_cast<Output_section*>(1); | |
1745 | out_section_offsets[i] = invalid_address; | |
1746 | } | |
ef9beddf | 1747 | else |
2e702c99 RM |
1748 | { |
1749 | // When garbage collection is switched on the actual layout | |
1750 | // only happens in the second call. | |
1751 | this->layout_section(layout, i, name, shdr, reloc_shndx[i], | |
1752 | reloc_type[i]); | |
c1027032 CC |
1753 | |
1754 | // When generating a .gdb_index section, we do additional | |
1755 | // processing of .debug_info and .debug_types sections after all | |
1756 | // the other sections for the same reason as above. | |
1757 | if (!relocatable | |
1758 | && parameters->options().gdb_index() | |
1759 | && !(shdr.get_sh_flags() & elfcpp::SHF_ALLOC)) | |
1760 | { | |
1761 | if (strcmp(name, ".debug_info") == 0 | |
1762 | || strcmp(name, ".zdebug_info") == 0) | |
1763 | debug_info_sections.push_back(i); | |
1764 | else if (strcmp(name, ".debug_types") == 0 | |
1765 | || strcmp(name, ".zdebug_types") == 0) | |
1766 | debug_types_sections.push_back(i); | |
1767 | } | |
2e702c99 | 1768 | } |
12e14209 ILT |
1769 | } |
1770 | ||
16164a6b | 1771 | if (!is_pass_two) |
83e17bd5 | 1772 | layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags, this); |
35cdfc9a | 1773 | |
1d946cb3 CC |
1774 | // Handle the .eh_frame sections after the other sections. |
1775 | gold_assert(!is_pass_one || eh_frame_sections.empty()); | |
1776 | for (std::vector<unsigned int>::const_iterator p = eh_frame_sections.begin(); | |
1777 | p != eh_frame_sections.end(); | |
1778 | ++p) | |
1779 | { | |
1780 | unsigned int i = *p; | |
1781 | const unsigned char* pshdr; | |
1782 | pshdr = section_headers_data + i * This::shdr_size; | |
1783 | typename This::Shdr shdr(pshdr); | |
1784 | ||
1785 | this->layout_eh_frame_section(layout, | |
1786 | symbols_data, | |
1787 | symbols_size, | |
1788 | symbol_names_data, | |
1789 | symbol_names_size, | |
1790 | i, | |
1791 | shdr, | |
1792 | reloc_shndx[i], | |
1793 | reloc_type[i]); | |
1794 | } | |
1795 | ||
6a74a719 | 1796 | // When doing a relocatable link handle the reloc sections at the |
2e702c99 RM |
1797 | // end. Garbage collection and Identical Code Folding is not |
1798 | // turned on for relocatable code. | |
2ea97941 | 1799 | if (emit_relocs) |
6a74a719 | 1800 | this->size_relocatable_relocs(); |
ef15dade | 1801 | |
16164a6b | 1802 | gold_assert(!is_two_pass || reloc_sections.empty()); |
ef15dade | 1803 | |
6a74a719 ILT |
1804 | for (std::vector<unsigned int>::const_iterator p = reloc_sections.begin(); |
1805 | p != reloc_sections.end(); | |
1806 | ++p) | |
1807 | { | |
1808 | unsigned int i = *p; | |
1809 | const unsigned char* pshdr; | |
6d03d481 | 1810 | pshdr = section_headers_data + i * This::shdr_size; |
6a74a719 ILT |
1811 | typename This::Shdr shdr(pshdr); |
1812 | ||
d491d34e | 1813 | unsigned int data_shndx = this->adjust_shndx(shdr.get_sh_info()); |
2ea97941 | 1814 | if (data_shndx >= shnum) |
6a74a719 ILT |
1815 | { |
1816 | // We already warned about this above. | |
1817 | continue; | |
1818 | } | |
1819 | ||
ef9beddf | 1820 | Output_section* data_section = out_sections[data_shndx]; |
f3a2388f | 1821 | if (data_section == reinterpret_cast<Output_section*>(2)) |
2e702c99 | 1822 | { |
c924eb67 CC |
1823 | if (is_pass_two) |
1824 | continue; | |
2e702c99 RM |
1825 | // The layout for the data section was deferred, so we need |
1826 | // to defer the relocation section, too. | |
f3a2388f | 1827 | const char* name = pnames + shdr.get_sh_name(); |
2e702c99 RM |
1828 | this->deferred_layout_relocs_.push_back( |
1829 | Deferred_layout(i, name, pshdr, 0, elfcpp::SHT_NULL)); | |
f3a2388f | 1830 | out_sections[i] = reinterpret_cast<Output_section*>(2); |
2e702c99 RM |
1831 | out_section_offsets[i] = invalid_address; |
1832 | continue; | |
1833 | } | |
6a74a719 ILT |
1834 | if (data_section == NULL) |
1835 | { | |
ef9beddf | 1836 | out_sections[i] = NULL; |
2e702c99 | 1837 | out_section_offsets[i] = invalid_address; |
6a74a719 ILT |
1838 | continue; |
1839 | } | |
1840 | ||
1841 | Relocatable_relocs* rr = new Relocatable_relocs(); | |
1842 | this->set_relocatable_relocs(i, rr); | |
1843 | ||
2ea97941 ILT |
1844 | Output_section* os = layout->layout_reloc(this, i, shdr, data_section, |
1845 | rr); | |
ef9beddf | 1846 | out_sections[i] = os; |
eff45813 | 1847 | out_section_offsets[i] = invalid_address; |
6a74a719 ILT |
1848 | } |
1849 | ||
c1027032 CC |
1850 | // When building a .gdb_index section, scan the .debug_info and |
1851 | // .debug_types sections. | |
16164a6b | 1852 | gold_assert(!is_pass_one |
c1027032 CC |
1853 | || (debug_info_sections.empty() && debug_types_sections.empty())); |
1854 | for (std::vector<unsigned int>::const_iterator p | |
1855 | = debug_info_sections.begin(); | |
1856 | p != debug_info_sections.end(); | |
1857 | ++p) | |
1858 | { | |
1859 | unsigned int i = *p; | |
1860 | layout->add_to_gdb_index(false, this, symbols_data, symbols_size, | |
1861 | i, reloc_shndx[i], reloc_type[i]); | |
1862 | } | |
1863 | for (std::vector<unsigned int>::const_iterator p | |
1864 | = debug_types_sections.begin(); | |
1865 | p != debug_types_sections.end(); | |
1866 | ++p) | |
1867 | { | |
1868 | unsigned int i = *p; | |
1869 | layout->add_to_gdb_index(true, this, symbols_data, symbols_size, | |
1870 | i, reloc_shndx[i], reloc_type[i]); | |
1871 | } | |
1872 | ||
16164a6b | 1873 | if (is_pass_two) |
6d03d481 ST |
1874 | { |
1875 | delete[] gc_sd->section_headers_data; | |
1876 | delete[] gc_sd->section_names_data; | |
1877 | delete[] gc_sd->symbols_data; | |
1878 | delete[] gc_sd->symbol_names_data; | |
ef15dade | 1879 | this->set_symbols_data(NULL); |
6d03d481 ST |
1880 | } |
1881 | else | |
1882 | { | |
1883 | delete sd->section_headers; | |
1884 | sd->section_headers = NULL; | |
1885 | delete sd->section_names; | |
1886 | sd->section_names = NULL; | |
1887 | } | |
12e14209 ILT |
1888 | } |
1889 | ||
5995b570 CC |
1890 | // Layout sections whose layout was deferred while waiting for |
1891 | // input files from a plugin. | |
1892 | ||
1893 | template<int size, bool big_endian> | |
1894 | void | |
6fa2a40b | 1895 | Sized_relobj_file<size, big_endian>::do_layout_deferred_sections(Layout* layout) |
5995b570 CC |
1896 | { |
1897 | typename std::vector<Deferred_layout>::iterator deferred; | |
1898 | ||
1899 | for (deferred = this->deferred_layout_.begin(); | |
1900 | deferred != this->deferred_layout_.end(); | |
1901 | ++deferred) | |
1902 | { | |
1903 | typename This::Shdr shdr(deferred->shdr_data_); | |
5e0f337e | 1904 | |
54a3d865 ILT |
1905 | if (!parameters->options().relocatable() |
1906 | && deferred->name_ == ".eh_frame" | |
1907 | && this->check_eh_frame_flags(&shdr)) | |
14788a3f | 1908 | { |
54a3d865 ILT |
1909 | // Checking is_section_included is not reliable for |
1910 | // .eh_frame sections, because they do not have an output | |
1911 | // section. This is not a problem normally because we call | |
1912 | // layout_eh_frame_section unconditionally, but when | |
1913 | // deferring sections that is not true. We don't want to | |
1914 | // keep all .eh_frame sections because that will cause us to | |
1915 | // keep all sections that they refer to, which is the wrong | |
1916 | // way around. Instead, the eh_frame code will discard | |
1917 | // .eh_frame sections that refer to discarded sections. | |
1918 | ||
14788a3f ILT |
1919 | // Reading the symbols again here may be slow. |
1920 | Read_symbols_data sd; | |
f35c4853 | 1921 | this->base_read_symbols(&sd); |
14788a3f ILT |
1922 | this->layout_eh_frame_section(layout, |
1923 | sd.symbols->data(), | |
1924 | sd.symbols_size, | |
1925 | sd.symbol_names->data(), | |
1926 | sd.symbol_names_size, | |
1927 | deferred->shndx_, | |
1928 | shdr, | |
1929 | deferred->reloc_shndx_, | |
1930 | deferred->reloc_type_); | |
54a3d865 | 1931 | continue; |
14788a3f | 1932 | } |
54a3d865 ILT |
1933 | |
1934 | // If the section is not included, it is because the garbage collector | |
1935 | // decided it is not needed. Avoid reverting that decision. | |
1936 | if (!this->is_section_included(deferred->shndx_)) | |
1937 | continue; | |
1938 | ||
1939 | this->layout_section(layout, deferred->shndx_, deferred->name_.c_str(), | |
1940 | shdr, deferred->reloc_shndx_, | |
1941 | deferred->reloc_type_); | |
5995b570 CC |
1942 | } |
1943 | ||
1944 | this->deferred_layout_.clear(); | |
f3a2388f CC |
1945 | |
1946 | // Now handle the deferred relocation sections. | |
1947 | ||
1948 | Output_sections& out_sections(this->output_sections()); | |
6fa2a40b | 1949 | std::vector<Address>& out_section_offsets(this->section_offsets()); |
f3a2388f CC |
1950 | |
1951 | for (deferred = this->deferred_layout_relocs_.begin(); | |
1952 | deferred != this->deferred_layout_relocs_.end(); | |
1953 | ++deferred) | |
1954 | { | |
1955 | unsigned int shndx = deferred->shndx_; | |
1956 | typename This::Shdr shdr(deferred->shdr_data_); | |
1957 | unsigned int data_shndx = this->adjust_shndx(shdr.get_sh_info()); | |
1958 | ||
1959 | Output_section* data_section = out_sections[data_shndx]; | |
1960 | if (data_section == NULL) | |
1961 | { | |
1962 | out_sections[shndx] = NULL; | |
2e702c99 | 1963 | out_section_offsets[shndx] = invalid_address; |
f3a2388f CC |
1964 | continue; |
1965 | } | |
1966 | ||
1967 | Relocatable_relocs* rr = new Relocatable_relocs(); | |
1968 | this->set_relocatable_relocs(shndx, rr); | |
1969 | ||
1970 | Output_section* os = layout->layout_reloc(this, shndx, shdr, | |
1971 | data_section, rr); | |
1972 | out_sections[shndx] = os; | |
1973 | out_section_offsets[shndx] = invalid_address; | |
1974 | } | |
5995b570 CC |
1975 | } |
1976 | ||
12e14209 ILT |
1977 | // Add the symbols to the symbol table. |
1978 | ||
1979 | template<int size, bool big_endian> | |
1980 | void | |
6fa2a40b CC |
1981 | Sized_relobj_file<size, big_endian>::do_add_symbols(Symbol_table* symtab, |
1982 | Read_symbols_data* sd, | |
1983 | Layout*) | |
12e14209 ILT |
1984 | { |
1985 | if (sd->symbols == NULL) | |
1986 | { | |
a3ad94ed | 1987 | gold_assert(sd->symbol_names == NULL); |
12e14209 ILT |
1988 | return; |
1989 | } | |
a2fb1b05 | 1990 | |
2ea97941 | 1991 | const int sym_size = This::sym_size; |
730cdc88 | 1992 | size_t symcount = ((sd->symbols_size - sd->external_symbols_offset) |
2ea97941 ILT |
1993 | / sym_size); |
1994 | if (symcount * sym_size != sd->symbols_size - sd->external_symbols_offset) | |
12e14209 | 1995 | { |
75f2446e ILT |
1996 | this->error(_("size of symbols is not multiple of symbol size")); |
1997 | return; | |
a2fb1b05 | 1998 | } |
12e14209 | 1999 | |
730cdc88 | 2000 | this->symbols_.resize(symcount); |
12e14209 | 2001 | |
12e14209 ILT |
2002 | const char* sym_names = |
2003 | reinterpret_cast<const char*>(sd->symbol_names->data()); | |
730cdc88 ILT |
2004 | symtab->add_from_relobj(this, |
2005 | sd->symbols->data() + sd->external_symbols_offset, | |
7fcd3aa9 | 2006 | symcount, this->local_symbol_count_, |
d491d34e | 2007 | sym_names, sd->symbol_names_size, |
92de84a6 ILT |
2008 | &this->symbols_, |
2009 | &this->defined_count_); | |
12e14209 ILT |
2010 | |
2011 | delete sd->symbols; | |
2012 | sd->symbols = NULL; | |
2013 | delete sd->symbol_names; | |
2014 | sd->symbol_names = NULL; | |
bae7f79e ILT |
2015 | } |
2016 | ||
b0193076 RÁE |
2017 | // Find out if this object, that is a member of a lib group, should be included |
2018 | // in the link. We check every symbol defined by this object. If the symbol | |
2019 | // table has a strong undefined reference to that symbol, we have to include | |
2020 | // the object. | |
2021 | ||
2022 | template<int size, bool big_endian> | |
2023 | Archive::Should_include | |
6fa2a40b CC |
2024 | Sized_relobj_file<size, big_endian>::do_should_include_member( |
2025 | Symbol_table* symtab, | |
2026 | Layout* layout, | |
2027 | Read_symbols_data* sd, | |
2028 | std::string* why) | |
b0193076 RÁE |
2029 | { |
2030 | char* tmpbuf = NULL; | |
2031 | size_t tmpbuflen = 0; | |
2032 | const char* sym_names = | |
2033 | reinterpret_cast<const char*>(sd->symbol_names->data()); | |
2034 | const unsigned char* syms = | |
2035 | sd->symbols->data() + sd->external_symbols_offset; | |
2036 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
2037 | size_t symcount = ((sd->symbols_size - sd->external_symbols_offset) | |
2e702c99 | 2038 | / sym_size); |
b0193076 RÁE |
2039 | |
2040 | const unsigned char* p = syms; | |
2041 | ||
2042 | for (size_t i = 0; i < symcount; ++i, p += sym_size) | |
2043 | { | |
2044 | elfcpp::Sym<size, big_endian> sym(p); | |
2045 | unsigned int st_shndx = sym.get_st_shndx(); | |
2046 | if (st_shndx == elfcpp::SHN_UNDEF) | |
2047 | continue; | |
2048 | ||
2049 | unsigned int st_name = sym.get_st_name(); | |
2050 | const char* name = sym_names + st_name; | |
2051 | Symbol* symbol; | |
88a4108b ILT |
2052 | Archive::Should_include t = Archive::should_include_member(symtab, |
2053 | layout, | |
2054 | name, | |
b0193076 RÁE |
2055 | &symbol, why, |
2056 | &tmpbuf, | |
2057 | &tmpbuflen); | |
2058 | if (t == Archive::SHOULD_INCLUDE_YES) | |
2059 | { | |
2060 | if (tmpbuf != NULL) | |
2061 | free(tmpbuf); | |
2062 | return t; | |
2063 | } | |
2064 | } | |
2065 | if (tmpbuf != NULL) | |
2066 | free(tmpbuf); | |
2067 | return Archive::SHOULD_INCLUDE_UNKNOWN; | |
2068 | } | |
2069 | ||
e0c52780 CC |
2070 | // Iterate over global defined symbols, calling a visitor class V for each. |
2071 | ||
2072 | template<int size, bool big_endian> | |
2073 | void | |
6fa2a40b | 2074 | Sized_relobj_file<size, big_endian>::do_for_all_global_symbols( |
e0c52780 CC |
2075 | Read_symbols_data* sd, |
2076 | Library_base::Symbol_visitor_base* v) | |
2077 | { | |
2078 | const char* sym_names = | |
2079 | reinterpret_cast<const char*>(sd->symbol_names->data()); | |
2080 | const unsigned char* syms = | |
2081 | sd->symbols->data() + sd->external_symbols_offset; | |
2082 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
2083 | size_t symcount = ((sd->symbols_size - sd->external_symbols_offset) | |
2e702c99 | 2084 | / sym_size); |
e0c52780 CC |
2085 | const unsigned char* p = syms; |
2086 | ||
2087 | for (size_t i = 0; i < symcount; ++i, p += sym_size) | |
2088 | { | |
2089 | elfcpp::Sym<size, big_endian> sym(p); | |
2090 | if (sym.get_st_shndx() != elfcpp::SHN_UNDEF) | |
2091 | v->visit(sym_names + sym.get_st_name()); | |
2092 | } | |
2093 | } | |
2094 | ||
7223e9ca ILT |
2095 | // Return whether the local symbol SYMNDX has a PLT offset. |
2096 | ||
2097 | template<int size, bool big_endian> | |
2098 | bool | |
6fa2a40b CC |
2099 | Sized_relobj_file<size, big_endian>::local_has_plt_offset( |
2100 | unsigned int symndx) const | |
7223e9ca ILT |
2101 | { |
2102 | typename Local_plt_offsets::const_iterator p = | |
2103 | this->local_plt_offsets_.find(symndx); | |
2104 | return p != this->local_plt_offsets_.end(); | |
2105 | } | |
2106 | ||
2107 | // Get the PLT offset of a local symbol. | |
2108 | ||
2109 | template<int size, bool big_endian> | |
2110 | unsigned int | |
83896202 ILT |
2111 | Sized_relobj_file<size, big_endian>::do_local_plt_offset( |
2112 | unsigned int symndx) const | |
7223e9ca ILT |
2113 | { |
2114 | typename Local_plt_offsets::const_iterator p = | |
2115 | this->local_plt_offsets_.find(symndx); | |
2116 | gold_assert(p != this->local_plt_offsets_.end()); | |
2117 | return p->second; | |
2118 | } | |
2119 | ||
2120 | // Set the PLT offset of a local symbol. | |
2121 | ||
2122 | template<int size, bool big_endian> | |
2123 | void | |
6fa2a40b CC |
2124 | Sized_relobj_file<size, big_endian>::set_local_plt_offset( |
2125 | unsigned int symndx, unsigned int plt_offset) | |
7223e9ca ILT |
2126 | { |
2127 | std::pair<typename Local_plt_offsets::iterator, bool> ins = | |
2128 | this->local_plt_offsets_.insert(std::make_pair(symndx, plt_offset)); | |
2129 | gold_assert(ins.second); | |
2130 | } | |
2131 | ||
cb295612 ILT |
2132 | // First pass over the local symbols. Here we add their names to |
2133 | // *POOL and *DYNPOOL, and we store the symbol value in | |
2134 | // THIS->LOCAL_VALUES_. This function is always called from a | |
2135 | // singleton thread. This is followed by a call to | |
2136 | // finalize_local_symbols. | |
75f65a3e ILT |
2137 | |
2138 | template<int size, bool big_endian> | |
7bf1f802 | 2139 | void |
6fa2a40b CC |
2140 | Sized_relobj_file<size, big_endian>::do_count_local_symbols(Stringpool* pool, |
2141 | Stringpool* dynpool) | |
75f65a3e | 2142 | { |
a3ad94ed | 2143 | gold_assert(this->symtab_shndx_ != -1U); |
645f8123 | 2144 | if (this->symtab_shndx_ == 0) |
61ba1cf9 ILT |
2145 | { |
2146 | // This object has no symbols. Weird but legal. | |
7bf1f802 | 2147 | return; |
61ba1cf9 ILT |
2148 | } |
2149 | ||
75f65a3e | 2150 | // Read the symbol table section header. |
2ea97941 | 2151 | const unsigned int symtab_shndx = this->symtab_shndx_; |
645f8123 | 2152 | typename This::Shdr symtabshdr(this, |
2ea97941 | 2153 | this->elf_file_.section_header(symtab_shndx)); |
a3ad94ed | 2154 | gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); |
75f65a3e ILT |
2155 | |
2156 | // Read the local symbols. | |
2ea97941 | 2157 | const int sym_size = This::sym_size; |
92e059d8 | 2158 | const unsigned int loccount = this->local_symbol_count_; |
a3ad94ed | 2159 | gold_assert(loccount == symtabshdr.get_sh_info()); |
2ea97941 | 2160 | off_t locsize = loccount * sym_size; |
75f65a3e | 2161 | const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(), |
39d0cb0e | 2162 | locsize, true, true); |
75f65a3e | 2163 | |
75f65a3e | 2164 | // Read the symbol names. |
d491d34e ILT |
2165 | const unsigned int strtab_shndx = |
2166 | this->adjust_shndx(symtabshdr.get_sh_link()); | |
8383303e | 2167 | section_size_type strtab_size; |
645f8123 | 2168 | const unsigned char* pnamesu = this->section_contents(strtab_shndx, |
9eb9fa57 ILT |
2169 | &strtab_size, |
2170 | true); | |
75f65a3e ILT |
2171 | const char* pnames = reinterpret_cast<const char*>(pnamesu); |
2172 | ||
2173 | // Loop over the local symbols. | |
2174 | ||
ef9beddf | 2175 | const Output_sections& out_sections(this->output_sections()); |
2ea97941 | 2176 | unsigned int shnum = this->shnum(); |
61ba1cf9 | 2177 | unsigned int count = 0; |
7bf1f802 | 2178 | unsigned int dyncount = 0; |
75f65a3e | 2179 | // Skip the first, dummy, symbol. |
2ea97941 | 2180 | psyms += sym_size; |
403676b5 | 2181 | bool strip_all = parameters->options().strip_all(); |
ebcc8304 | 2182 | bool discard_all = parameters->options().discard_all(); |
bb04269c | 2183 | bool discard_locals = parameters->options().discard_locals(); |
2ea97941 | 2184 | for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size) |
75f65a3e ILT |
2185 | { |
2186 | elfcpp::Sym<size, big_endian> sym(psyms); | |
2187 | ||
b8e6aad9 ILT |
2188 | Symbol_value<size>& lv(this->local_values_[i]); |
2189 | ||
d491d34e ILT |
2190 | bool is_ordinary; |
2191 | unsigned int shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(), | |
2192 | &is_ordinary); | |
2193 | lv.set_input_shndx(shndx, is_ordinary); | |
75f65a3e | 2194 | |
063f12a8 ILT |
2195 | if (sym.get_st_type() == elfcpp::STT_SECTION) |
2196 | lv.set_is_section_symbol(); | |
7bf1f802 ILT |
2197 | else if (sym.get_st_type() == elfcpp::STT_TLS) |
2198 | lv.set_is_tls_symbol(); | |
7223e9ca ILT |
2199 | else if (sym.get_st_type() == elfcpp::STT_GNU_IFUNC) |
2200 | lv.set_is_ifunc_symbol(); | |
7bf1f802 ILT |
2201 | |
2202 | // Save the input symbol value for use in do_finalize_local_symbols(). | |
2203 | lv.set_input_value(sym.get_st_value()); | |
2204 | ||
2205 | // Decide whether this symbol should go into the output file. | |
063f12a8 | 2206 | |
2ea97941 | 2207 | if ((shndx < shnum && out_sections[shndx] == NULL) |
ebcc8304 | 2208 | || shndx == this->discarded_eh_frame_shndx_) |
2e702c99 | 2209 | { |
7bf1f802 | 2210 | lv.set_no_output_symtab_entry(); |
2e702c99 RM |
2211 | gold_assert(!lv.needs_output_dynsym_entry()); |
2212 | continue; | |
2213 | } | |
7bf1f802 | 2214 | |
ec4dbad3 AM |
2215 | if (sym.get_st_type() == elfcpp::STT_SECTION |
2216 | || !this->adjust_local_symbol(&lv)) | |
7bf1f802 ILT |
2217 | { |
2218 | lv.set_no_output_symtab_entry(); | |
2e702c99 | 2219 | gold_assert(!lv.needs_output_dynsym_entry()); |
7bf1f802 ILT |
2220 | continue; |
2221 | } | |
2222 | ||
2223 | if (sym.get_st_name() >= strtab_size) | |
2224 | { | |
2225 | this->error(_("local symbol %u section name out of range: %u >= %u"), | |
2226 | i, sym.get_st_name(), | |
2227 | static_cast<unsigned int>(strtab_size)); | |
2228 | lv.set_no_output_symtab_entry(); | |
2229 | continue; | |
2230 | } | |
2231 | ||
ebcc8304 ILT |
2232 | const char* name = pnames + sym.get_st_name(); |
2233 | ||
2234 | // If needed, add the symbol to the dynamic symbol table string pool. | |
2235 | if (lv.needs_output_dynsym_entry()) | |
2e702c99 RM |
2236 | { |
2237 | dynpool->add(name, true, NULL); | |
2238 | ++dyncount; | |
2239 | } | |
ebcc8304 | 2240 | |
403676b5 CC |
2241 | if (strip_all |
2242 | || (discard_all && lv.may_be_discarded_from_output_symtab())) | |
ebcc8304 ILT |
2243 | { |
2244 | lv.set_no_output_symtab_entry(); | |
2245 | continue; | |
2246 | } | |
2247 | ||
bb04269c DK |
2248 | // If --discard-locals option is used, discard all temporary local |
2249 | // symbols. These symbols start with system-specific local label | |
2250 | // prefixes, typically .L for ELF system. We want to be compatible | |
2251 | // with GNU ld so here we essentially use the same check in | |
2252 | // bfd_is_local_label(). The code is different because we already | |
2253 | // know that: | |
2254 | // | |
2255 | // - the symbol is local and thus cannot have global or weak binding. | |
2256 | // - the symbol is not a section symbol. | |
2257 | // - the symbol has a name. | |
2258 | // | |
2259 | // We do not discard a symbol if it needs a dynamic symbol entry. | |
bb04269c DK |
2260 | if (discard_locals |
2261 | && sym.get_st_type() != elfcpp::STT_FILE | |
2262 | && !lv.needs_output_dynsym_entry() | |
d3bbad62 | 2263 | && lv.may_be_discarded_from_output_symtab() |
2ea97941 | 2264 | && parameters->target().is_local_label_name(name)) |
bb04269c DK |
2265 | { |
2266 | lv.set_no_output_symtab_entry(); | |
2267 | continue; | |
2268 | } | |
2269 | ||
8c604651 CS |
2270 | // Discard the local symbol if -retain_symbols_file is specified |
2271 | // and the local symbol is not in that file. | |
2ea97941 | 2272 | if (!parameters->options().should_retain_symbol(name)) |
2e702c99 RM |
2273 | { |
2274 | lv.set_no_output_symtab_entry(); | |
2275 | continue; | |
2276 | } | |
8c604651 | 2277 | |
bb04269c | 2278 | // Add the symbol to the symbol table string pool. |
2ea97941 | 2279 | pool->add(name, true, NULL); |
7bf1f802 | 2280 | ++count; |
7bf1f802 ILT |
2281 | } |
2282 | ||
2283 | this->output_local_symbol_count_ = count; | |
2284 | this->output_local_dynsym_count_ = dyncount; | |
2285 | } | |
2286 | ||
aa98ff75 DK |
2287 | // Compute the final value of a local symbol. |
2288 | ||
2289 | template<int size, bool big_endian> | |
6fa2a40b CC |
2290 | typename Sized_relobj_file<size, big_endian>::Compute_final_local_value_status |
2291 | Sized_relobj_file<size, big_endian>::compute_final_local_value_internal( | |
aa98ff75 DK |
2292 | unsigned int r_sym, |
2293 | const Symbol_value<size>* lv_in, | |
2294 | Symbol_value<size>* lv_out, | |
2295 | bool relocatable, | |
2296 | const Output_sections& out_sections, | |
2297 | const std::vector<Address>& out_offsets, | |
2298 | const Symbol_table* symtab) | |
2299 | { | |
2300 | // We are going to overwrite *LV_OUT, if it has a merged symbol value, | |
2301 | // we may have a memory leak. | |
2302 | gold_assert(lv_out->has_output_value()); | |
2303 | ||
2304 | bool is_ordinary; | |
2305 | unsigned int shndx = lv_in->input_shndx(&is_ordinary); | |
2e702c99 | 2306 | |
aa98ff75 | 2307 | // Set the output symbol value. |
2e702c99 | 2308 | |
aa98ff75 DK |
2309 | if (!is_ordinary) |
2310 | { | |
2311 | if (shndx == elfcpp::SHN_ABS || Symbol::is_common_shndx(shndx)) | |
2312 | lv_out->set_output_value(lv_in->input_value()); | |
2313 | else | |
2314 | { | |
2315 | this->error(_("unknown section index %u for local symbol %u"), | |
2316 | shndx, r_sym); | |
2317 | lv_out->set_output_value(0); | |
2318 | return This::CFLV_ERROR; | |
2319 | } | |
2320 | } | |
2321 | else | |
2322 | { | |
2323 | if (shndx >= this->shnum()) | |
2324 | { | |
2325 | this->error(_("local symbol %u section index %u out of range"), | |
2326 | r_sym, shndx); | |
2327 | lv_out->set_output_value(0); | |
2328 | return This::CFLV_ERROR; | |
2329 | } | |
2e702c99 | 2330 | |
aa98ff75 DK |
2331 | Output_section* os = out_sections[shndx]; |
2332 | Address secoffset = out_offsets[shndx]; | |
2333 | if (symtab->is_section_folded(this, shndx)) | |
2334 | { | |
2335 | gold_assert(os == NULL && secoffset == invalid_address); | |
2336 | // Get the os of the section it is folded onto. | |
2337 | Section_id folded = symtab->icf()->get_folded_section(this, | |
2338 | shndx); | |
2339 | gold_assert(folded.first != NULL); | |
6fa2a40b CC |
2340 | Sized_relobj_file<size, big_endian>* folded_obj = reinterpret_cast |
2341 | <Sized_relobj_file<size, big_endian>*>(folded.first); | |
aa98ff75 DK |
2342 | os = folded_obj->output_section(folded.second); |
2343 | gold_assert(os != NULL); | |
2344 | secoffset = folded_obj->get_output_section_offset(folded.second); | |
2e702c99 | 2345 | |
aa98ff75 DK |
2346 | // This could be a relaxed input section. |
2347 | if (secoffset == invalid_address) | |
2348 | { | |
2349 | const Output_relaxed_input_section* relaxed_section = | |
2350 | os->find_relaxed_input_section(folded_obj, folded.second); | |
2351 | gold_assert(relaxed_section != NULL); | |
2352 | secoffset = relaxed_section->address() - os->address(); | |
2353 | } | |
2354 | } | |
2e702c99 | 2355 | |
aa98ff75 DK |
2356 | if (os == NULL) |
2357 | { | |
2358 | // This local symbol belongs to a section we are discarding. | |
2359 | // In some cases when applying relocations later, we will | |
2360 | // attempt to match it to the corresponding kept section, | |
2361 | // so we leave the input value unchanged here. | |
2362 | return This::CFLV_DISCARDED; | |
2363 | } | |
2364 | else if (secoffset == invalid_address) | |
2365 | { | |
2366 | uint64_t start; | |
2e702c99 | 2367 | |
aa98ff75 DK |
2368 | // This is a SHF_MERGE section or one which otherwise |
2369 | // requires special handling. | |
2370 | if (shndx == this->discarded_eh_frame_shndx_) | |
2371 | { | |
2372 | // This local symbol belongs to a discarded .eh_frame | |
2373 | // section. Just treat it like the case in which | |
2374 | // os == NULL above. | |
2375 | gold_assert(this->has_eh_frame_); | |
2376 | return This::CFLV_DISCARDED; | |
2377 | } | |
2378 | else if (!lv_in->is_section_symbol()) | |
2379 | { | |
2380 | // This is not a section symbol. We can determine | |
2381 | // the final value now. | |
2382 | lv_out->set_output_value( | |
2383 | os->output_address(this, shndx, lv_in->input_value())); | |
2384 | } | |
2385 | else if (!os->find_starting_output_address(this, shndx, &start)) | |
2386 | { | |
2387 | // This is a section symbol, but apparently not one in a | |
2388 | // merged section. First check to see if this is a relaxed | |
2389 | // input section. If so, use its address. Otherwise just | |
2390 | // use the start of the output section. This happens with | |
2391 | // relocatable links when the input object has section | |
2392 | // symbols for arbitrary non-merge sections. | |
2393 | const Output_section_data* posd = | |
2394 | os->find_relaxed_input_section(this, shndx); | |
2395 | if (posd != NULL) | |
2396 | { | |
2397 | Address relocatable_link_adjustment = | |
2398 | relocatable ? os->address() : 0; | |
2399 | lv_out->set_output_value(posd->address() | |
2400 | - relocatable_link_adjustment); | |
2401 | } | |
2402 | else | |
2403 | lv_out->set_output_value(os->address()); | |
2404 | } | |
2405 | else | |
2406 | { | |
2407 | // We have to consider the addend to determine the | |
2408 | // value to use in a relocation. START is the start | |
2409 | // of this input section. If we are doing a relocatable | |
2410 | // link, use offset from start output section instead of | |
2411 | // address. | |
2412 | Address adjusted_start = | |
2413 | relocatable ? start - os->address() : start; | |
2414 | Merged_symbol_value<size>* msv = | |
2415 | new Merged_symbol_value<size>(lv_in->input_value(), | |
2416 | adjusted_start); | |
2417 | lv_out->set_merged_symbol_value(msv); | |
2418 | } | |
2419 | } | |
5efeedf6 CC |
2420 | else if (lv_in->is_tls_symbol() |
2421 | || (lv_in->is_section_symbol() | |
2422 | && (os->flags() & elfcpp::SHF_TLS))) | |
aa98ff75 DK |
2423 | lv_out->set_output_value(os->tls_offset() |
2424 | + secoffset | |
2425 | + lv_in->input_value()); | |
2426 | else | |
2427 | lv_out->set_output_value((relocatable ? 0 : os->address()) | |
2428 | + secoffset | |
2429 | + lv_in->input_value()); | |
2430 | } | |
2431 | return This::CFLV_OK; | |
2432 | } | |
2433 | ||
2434 | // Compute final local symbol value. R_SYM is the index of a local | |
2435 | // symbol in symbol table. LV points to a symbol value, which is | |
2436 | // expected to hold the input value and to be over-written by the | |
2437 | // final value. SYMTAB points to a symbol table. Some targets may want | |
2438 | // to know would-be-finalized local symbol values in relaxation. | |
2439 | // Hence we provide this method. Since this method updates *LV, a | |
2440 | // callee should make a copy of the original local symbol value and | |
2441 | // use the copy instead of modifying an object's local symbols before | |
2442 | // everything is finalized. The caller should also free up any allocated | |
2443 | // memory in the return value in *LV. | |
2444 | template<int size, bool big_endian> | |
6fa2a40b CC |
2445 | typename Sized_relobj_file<size, big_endian>::Compute_final_local_value_status |
2446 | Sized_relobj_file<size, big_endian>::compute_final_local_value( | |
aa98ff75 DK |
2447 | unsigned int r_sym, |
2448 | const Symbol_value<size>* lv_in, | |
2449 | Symbol_value<size>* lv_out, | |
2450 | const Symbol_table* symtab) | |
2451 | { | |
2452 | // This is just a wrapper of compute_final_local_value_internal. | |
2453 | const bool relocatable = parameters->options().relocatable(); | |
2454 | const Output_sections& out_sections(this->output_sections()); | |
6fa2a40b | 2455 | const std::vector<Address>& out_offsets(this->section_offsets()); |
aa98ff75 DK |
2456 | return this->compute_final_local_value_internal(r_sym, lv_in, lv_out, |
2457 | relocatable, out_sections, | |
2458 | out_offsets, symtab); | |
2459 | } | |
2460 | ||
cb295612 | 2461 | // Finalize the local symbols. Here we set the final value in |
7bf1f802 | 2462 | // THIS->LOCAL_VALUES_ and set their output symbol table indexes. |
17a1d0a9 | 2463 | // This function is always called from a singleton thread. The actual |
7bf1f802 ILT |
2464 | // output of the local symbols will occur in a separate task. |
2465 | ||
2466 | template<int size, bool big_endian> | |
2467 | unsigned int | |
6fa2a40b CC |
2468 | Sized_relobj_file<size, big_endian>::do_finalize_local_symbols( |
2469 | unsigned int index, | |
2470 | off_t off, | |
2471 | Symbol_table* symtab) | |
7bf1f802 ILT |
2472 | { |
2473 | gold_assert(off == static_cast<off_t>(align_address(off, size >> 3))); | |
2474 | ||
2475 | const unsigned int loccount = this->local_symbol_count_; | |
2476 | this->local_symbol_offset_ = off; | |
2477 | ||
b4ecf66b | 2478 | const bool relocatable = parameters->options().relocatable(); |
ef9beddf | 2479 | const Output_sections& out_sections(this->output_sections()); |
6fa2a40b | 2480 | const std::vector<Address>& out_offsets(this->section_offsets()); |
7bf1f802 ILT |
2481 | |
2482 | for (unsigned int i = 1; i < loccount; ++i) | |
2483 | { | |
aa98ff75 | 2484 | Symbol_value<size>* lv = &this->local_values_[i]; |
7bf1f802 | 2485 | |
6695e4b3 | 2486 | Compute_final_local_value_status cflv_status = |
aa98ff75 DK |
2487 | this->compute_final_local_value_internal(i, lv, lv, relocatable, |
2488 | out_sections, out_offsets, | |
2489 | symtab); | |
2490 | switch (cflv_status) | |
75f65a3e | 2491 | { |
aa98ff75 DK |
2492 | case CFLV_OK: |
2493 | if (!lv->is_output_symtab_index_set()) | |
75f65a3e | 2494 | { |
aa98ff75 DK |
2495 | lv->set_output_symtab_index(index); |
2496 | ++index; | |
75f65a3e | 2497 | } |
aa98ff75 DK |
2498 | break; |
2499 | case CFLV_DISCARDED: | |
2500 | case CFLV_ERROR: | |
2501 | // Do nothing. | |
2502 | break; | |
2503 | default: | |
2504 | gold_unreachable(); | |
75f65a3e | 2505 | } |
7bf1f802 ILT |
2506 | } |
2507 | return index; | |
2508 | } | |
645f8123 | 2509 | |
7bf1f802 | 2510 | // Set the output dynamic symbol table indexes for the local variables. |
c06b7b0b | 2511 | |
7bf1f802 ILT |
2512 | template<int size, bool big_endian> |
2513 | unsigned int | |
6fa2a40b CC |
2514 | Sized_relobj_file<size, big_endian>::do_set_local_dynsym_indexes( |
2515 | unsigned int index) | |
7bf1f802 ILT |
2516 | { |
2517 | const unsigned int loccount = this->local_symbol_count_; | |
2518 | for (unsigned int i = 1; i < loccount; ++i) | |
2519 | { | |
2520 | Symbol_value<size>& lv(this->local_values_[i]); | |
2521 | if (lv.needs_output_dynsym_entry()) | |
2e702c99 RM |
2522 | { |
2523 | lv.set_output_dynsym_index(index); | |
2524 | ++index; | |
2525 | } | |
75f65a3e | 2526 | } |
7bf1f802 ILT |
2527 | return index; |
2528 | } | |
75f65a3e | 2529 | |
7bf1f802 ILT |
2530 | // Set the offset where local dynamic symbol information will be stored. |
2531 | // Returns the count of local symbols contributed to the symbol table by | |
2532 | // this object. | |
61ba1cf9 | 2533 | |
7bf1f802 ILT |
2534 | template<int size, bool big_endian> |
2535 | unsigned int | |
6fa2a40b | 2536 | Sized_relobj_file<size, big_endian>::do_set_local_dynsym_offset(off_t off) |
7bf1f802 ILT |
2537 | { |
2538 | gold_assert(off == static_cast<off_t>(align_address(off, size >> 3))); | |
2539 | this->local_dynsym_offset_ = off; | |
2540 | return this->output_local_dynsym_count_; | |
75f65a3e ILT |
2541 | } |
2542 | ||
ef15dade ST |
2543 | // If Symbols_data is not NULL get the section flags from here otherwise |
2544 | // get it from the file. | |
2545 | ||
2546 | template<int size, bool big_endian> | |
2547 | uint64_t | |
6fa2a40b | 2548 | Sized_relobj_file<size, big_endian>::do_section_flags(unsigned int shndx) |
ef15dade ST |
2549 | { |
2550 | Symbols_data* sd = this->get_symbols_data(); | |
2551 | if (sd != NULL) | |
2552 | { | |
2553 | const unsigned char* pshdrs = sd->section_headers_data | |
2e702c99 | 2554 | + This::shdr_size * shndx; |
ef15dade | 2555 | typename This::Shdr shdr(pshdrs); |
2e702c99 | 2556 | return shdr.get_sh_flags(); |
ef15dade ST |
2557 | } |
2558 | // If sd is NULL, read the section header from the file. | |
2e702c99 | 2559 | return this->elf_file_.section_flags(shndx); |
ef15dade ST |
2560 | } |
2561 | ||
2562 | // Get the section's ent size from Symbols_data. Called by get_section_contents | |
2563 | // in icf.cc | |
2564 | ||
2565 | template<int size, bool big_endian> | |
2566 | uint64_t | |
6fa2a40b | 2567 | Sized_relobj_file<size, big_endian>::do_section_entsize(unsigned int shndx) |
ef15dade ST |
2568 | { |
2569 | Symbols_data* sd = this->get_symbols_data(); | |
ca09d69a | 2570 | gold_assert(sd != NULL); |
ef15dade ST |
2571 | |
2572 | const unsigned char* pshdrs = sd->section_headers_data | |
2e702c99 | 2573 | + This::shdr_size * shndx; |
ef15dade | 2574 | typename This::Shdr shdr(pshdrs); |
2e702c99 | 2575 | return shdr.get_sh_entsize(); |
ef15dade ST |
2576 | } |
2577 | ||
61ba1cf9 ILT |
2578 | // Write out the local symbols. |
2579 | ||
2580 | template<int size, bool big_endian> | |
2581 | void | |
6fa2a40b | 2582 | Sized_relobj_file<size, big_endian>::write_local_symbols( |
17a1d0a9 ILT |
2583 | Output_file* of, |
2584 | const Stringpool* sympool, | |
d491d34e ILT |
2585 | const Stringpool* dynpool, |
2586 | Output_symtab_xindex* symtab_xindex, | |
cdc29364 CC |
2587 | Output_symtab_xindex* dynsym_xindex, |
2588 | off_t symtab_off) | |
61ba1cf9 | 2589 | { |
99e9a495 ILT |
2590 | const bool strip_all = parameters->options().strip_all(); |
2591 | if (strip_all) | |
2592 | { | |
2593 | if (this->output_local_dynsym_count_ == 0) | |
2594 | return; | |
2595 | this->output_local_symbol_count_ = 0; | |
2596 | } | |
9e2dcb77 | 2597 | |
a3ad94ed | 2598 | gold_assert(this->symtab_shndx_ != -1U); |
645f8123 | 2599 | if (this->symtab_shndx_ == 0) |
61ba1cf9 ILT |
2600 | { |
2601 | // This object has no symbols. Weird but legal. | |
2602 | return; | |
2603 | } | |
2604 | ||
2605 | // Read the symbol table section header. | |
2ea97941 | 2606 | const unsigned int symtab_shndx = this->symtab_shndx_; |
645f8123 | 2607 | typename This::Shdr symtabshdr(this, |
2ea97941 | 2608 | this->elf_file_.section_header(symtab_shndx)); |
a3ad94ed | 2609 | gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); |
92e059d8 | 2610 | const unsigned int loccount = this->local_symbol_count_; |
a3ad94ed | 2611 | gold_assert(loccount == symtabshdr.get_sh_info()); |
61ba1cf9 ILT |
2612 | |
2613 | // Read the local symbols. | |
2ea97941 ILT |
2614 | const int sym_size = This::sym_size; |
2615 | off_t locsize = loccount * sym_size; | |
61ba1cf9 | 2616 | const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(), |
39d0cb0e | 2617 | locsize, true, false); |
61ba1cf9 | 2618 | |
61ba1cf9 | 2619 | // Read the symbol names. |
d491d34e ILT |
2620 | const unsigned int strtab_shndx = |
2621 | this->adjust_shndx(symtabshdr.get_sh_link()); | |
8383303e | 2622 | section_size_type strtab_size; |
645f8123 | 2623 | const unsigned char* pnamesu = this->section_contents(strtab_shndx, |
9eb9fa57 | 2624 | &strtab_size, |
cb295612 | 2625 | false); |
61ba1cf9 ILT |
2626 | const char* pnames = reinterpret_cast<const char*>(pnamesu); |
2627 | ||
7bf1f802 ILT |
2628 | // Get views into the output file for the portions of the symbol table |
2629 | // and the dynamic symbol table that we will be writing. | |
2ea97941 | 2630 | off_t output_size = this->output_local_symbol_count_ * sym_size; |
f2619d6c | 2631 | unsigned char* oview = NULL; |
7bf1f802 | 2632 | if (output_size > 0) |
cdc29364 CC |
2633 | oview = of->get_output_view(symtab_off + this->local_symbol_offset_, |
2634 | output_size); | |
7bf1f802 | 2635 | |
2ea97941 | 2636 | off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size; |
7bf1f802 ILT |
2637 | unsigned char* dyn_oview = NULL; |
2638 | if (dyn_output_size > 0) | |
2639 | dyn_oview = of->get_output_view(this->local_dynsym_offset_, | |
2e702c99 | 2640 | dyn_output_size); |
61ba1cf9 | 2641 | |
e0a1e121 | 2642 | const Output_sections& out_sections(this->output_sections()); |
c06b7b0b | 2643 | |
a3ad94ed | 2644 | gold_assert(this->local_values_.size() == loccount); |
61ba1cf9 | 2645 | |
61ba1cf9 | 2646 | unsigned char* ov = oview; |
7bf1f802 | 2647 | unsigned char* dyn_ov = dyn_oview; |
2ea97941 ILT |
2648 | psyms += sym_size; |
2649 | for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size) | |
61ba1cf9 ILT |
2650 | { |
2651 | elfcpp::Sym<size, big_endian> isym(psyms); | |
f6ce93d6 | 2652 | |
d491d34e ILT |
2653 | Symbol_value<size>& lv(this->local_values_[i]); |
2654 | ||
2655 | bool is_ordinary; | |
2656 | unsigned int st_shndx = this->adjust_sym_shndx(i, isym.get_st_shndx(), | |
2657 | &is_ordinary); | |
2658 | if (is_ordinary) | |
61ba1cf9 | 2659 | { |
ef9beddf ILT |
2660 | gold_assert(st_shndx < out_sections.size()); |
2661 | if (out_sections[st_shndx] == NULL) | |
61ba1cf9 | 2662 | continue; |
ef9beddf | 2663 | st_shndx = out_sections[st_shndx]->out_shndx(); |
d491d34e ILT |
2664 | if (st_shndx >= elfcpp::SHN_LORESERVE) |
2665 | { | |
d3bbad62 | 2666 | if (lv.has_output_symtab_entry()) |
d491d34e | 2667 | symtab_xindex->add(lv.output_symtab_index(), st_shndx); |
d3bbad62 | 2668 | if (lv.has_output_dynsym_entry()) |
d491d34e ILT |
2669 | dynsym_xindex->add(lv.output_dynsym_index(), st_shndx); |
2670 | st_shndx = elfcpp::SHN_XINDEX; | |
2671 | } | |
61ba1cf9 ILT |
2672 | } |
2673 | ||
7bf1f802 | 2674 | // Write the symbol to the output symbol table. |
d3bbad62 | 2675 | if (lv.has_output_symtab_entry()) |
2e702c99 RM |
2676 | { |
2677 | elfcpp::Sym_write<size, big_endian> osym(ov); | |
2678 | ||
2679 | gold_assert(isym.get_st_name() < strtab_size); | |
2680 | const char* name = pnames + isym.get_st_name(); | |
2681 | osym.put_st_name(sympool->get_offset(name)); | |
2682 | osym.put_st_value(this->local_values_[i].value(this, 0)); | |
2683 | osym.put_st_size(isym.get_st_size()); | |
2684 | osym.put_st_info(isym.get_st_info()); | |
2685 | osym.put_st_other(isym.get_st_other()); | |
2686 | osym.put_st_shndx(st_shndx); | |
2687 | ||
2688 | ov += sym_size; | |
2689 | } | |
7bf1f802 ILT |
2690 | |
2691 | // Write the symbol to the output dynamic symbol table. | |
d3bbad62 | 2692 | if (lv.has_output_dynsym_entry()) |
2e702c99 RM |
2693 | { |
2694 | gold_assert(dyn_ov < dyn_oview + dyn_output_size); | |
2695 | elfcpp::Sym_write<size, big_endian> osym(dyn_ov); | |
2696 | ||
2697 | gold_assert(isym.get_st_name() < strtab_size); | |
2698 | const char* name = pnames + isym.get_st_name(); | |
2699 | osym.put_st_name(dynpool->get_offset(name)); | |
2700 | osym.put_st_value(this->local_values_[i].value(this, 0)); | |
2701 | osym.put_st_size(isym.get_st_size()); | |
2702 | osym.put_st_info(isym.get_st_info()); | |
2703 | osym.put_st_other(isym.get_st_other()); | |
2704 | osym.put_st_shndx(st_shndx); | |
2705 | ||
2706 | dyn_ov += sym_size; | |
2707 | } | |
7bf1f802 | 2708 | } |
f6ce93d6 | 2709 | |
61ba1cf9 | 2710 | |
7bf1f802 ILT |
2711 | if (output_size > 0) |
2712 | { | |
2713 | gold_assert(ov - oview == output_size); | |
cdc29364 CC |
2714 | of->write_output_view(symtab_off + this->local_symbol_offset_, |
2715 | output_size, oview); | |
61ba1cf9 ILT |
2716 | } |
2717 | ||
7bf1f802 ILT |
2718 | if (dyn_output_size > 0) |
2719 | { | |
2720 | gold_assert(dyn_ov - dyn_oview == dyn_output_size); | |
2721 | of->write_output_view(this->local_dynsym_offset_, dyn_output_size, | |
2e702c99 | 2722 | dyn_oview); |
7bf1f802 | 2723 | } |
61ba1cf9 ILT |
2724 | } |
2725 | ||
f7e2ee48 ILT |
2726 | // Set *INFO to symbolic information about the offset OFFSET in the |
2727 | // section SHNDX. Return true if we found something, false if we | |
2728 | // found nothing. | |
2729 | ||
2730 | template<int size, bool big_endian> | |
2731 | bool | |
6fa2a40b | 2732 | Sized_relobj_file<size, big_endian>::get_symbol_location_info( |
f7e2ee48 | 2733 | unsigned int shndx, |
2ea97941 | 2734 | off_t offset, |
f7e2ee48 ILT |
2735 | Symbol_location_info* info) |
2736 | { | |
2737 | if (this->symtab_shndx_ == 0) | |
2738 | return false; | |
2739 | ||
8383303e | 2740 | section_size_type symbols_size; |
f7e2ee48 ILT |
2741 | const unsigned char* symbols = this->section_contents(this->symtab_shndx_, |
2742 | &symbols_size, | |
2743 | false); | |
2744 | ||
d491d34e ILT |
2745 | unsigned int symbol_names_shndx = |
2746 | this->adjust_shndx(this->section_link(this->symtab_shndx_)); | |
8383303e | 2747 | section_size_type names_size; |
f7e2ee48 ILT |
2748 | const unsigned char* symbol_names_u = |
2749 | this->section_contents(symbol_names_shndx, &names_size, false); | |
2750 | const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u); | |
2751 | ||
2ea97941 ILT |
2752 | const int sym_size = This::sym_size; |
2753 | const size_t count = symbols_size / sym_size; | |
f7e2ee48 ILT |
2754 | |
2755 | const unsigned char* p = symbols; | |
2ea97941 | 2756 | for (size_t i = 0; i < count; ++i, p += sym_size) |
f7e2ee48 ILT |
2757 | { |
2758 | elfcpp::Sym<size, big_endian> sym(p); | |
2759 | ||
2760 | if (sym.get_st_type() == elfcpp::STT_FILE) | |
2761 | { | |
2762 | if (sym.get_st_name() >= names_size) | |
2763 | info->source_file = "(invalid)"; | |
2764 | else | |
2765 | info->source_file = symbol_names + sym.get_st_name(); | |
d491d34e | 2766 | continue; |
f7e2ee48 | 2767 | } |
d491d34e ILT |
2768 | |
2769 | bool is_ordinary; | |
2770 | unsigned int st_shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(), | |
2771 | &is_ordinary); | |
2772 | if (is_ordinary | |
2773 | && st_shndx == shndx | |
2ea97941 | 2774 | && static_cast<off_t>(sym.get_st_value()) <= offset |
d491d34e | 2775 | && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size()) |
2ea97941 | 2776 | > offset)) |
2e702c99 | 2777 | { |
60e8b3fc | 2778 | info->enclosing_symbol_type = sym.get_st_type(); |
2e702c99 | 2779 | if (sym.get_st_name() > names_size) |
f7e2ee48 ILT |
2780 | info->enclosing_symbol_name = "(invalid)"; |
2781 | else | |
2e702c99 RM |
2782 | { |
2783 | info->enclosing_symbol_name = symbol_names + sym.get_st_name(); | |
2784 | if (parameters->options().do_demangle()) | |
2785 | { | |
2786 | char* demangled_name = cplus_demangle( | |
2787 | info->enclosing_symbol_name.c_str(), | |
2788 | DMGL_ANSI | DMGL_PARAMS); | |
2789 | if (demangled_name != NULL) | |
2790 | { | |
2791 | info->enclosing_symbol_name.assign(demangled_name); | |
2792 | free(demangled_name); | |
2793 | } | |
2794 | } | |
2795 | } | |
f7e2ee48 | 2796 | return true; |
2e702c99 | 2797 | } |
f7e2ee48 ILT |
2798 | } |
2799 | ||
2800 | return false; | |
2801 | } | |
2802 | ||
e94cf127 CC |
2803 | // Look for a kept section corresponding to the given discarded section, |
2804 | // and return its output address. This is used only for relocations in | |
2805 | // debugging sections. If we can't find the kept section, return 0. | |
2806 | ||
2807 | template<int size, bool big_endian> | |
6fa2a40b CC |
2808 | typename Sized_relobj_file<size, big_endian>::Address |
2809 | Sized_relobj_file<size, big_endian>::map_to_kept_section( | |
e94cf127 CC |
2810 | unsigned int shndx, |
2811 | bool* found) const | |
2812 | { | |
1ef4d87f ILT |
2813 | Relobj* kept_object; |
2814 | unsigned int kept_shndx; | |
2815 | if (this->get_kept_comdat_section(shndx, &kept_object, &kept_shndx)) | |
e94cf127 | 2816 | { |
6fa2a40b CC |
2817 | Sized_relobj_file<size, big_endian>* kept_relobj = |
2818 | static_cast<Sized_relobj_file<size, big_endian>*>(kept_object); | |
1ef4d87f | 2819 | Output_section* os = kept_relobj->output_section(kept_shndx); |
2ea97941 ILT |
2820 | Address offset = kept_relobj->get_output_section_offset(kept_shndx); |
2821 | if (os != NULL && offset != invalid_address) | |
1ef4d87f ILT |
2822 | { |
2823 | *found = true; | |
2ea97941 | 2824 | return os->address() + offset; |
1ef4d87f | 2825 | } |
e94cf127 CC |
2826 | } |
2827 | *found = false; | |
2828 | return 0; | |
2829 | } | |
2830 | ||
92de84a6 ILT |
2831 | // Get symbol counts. |
2832 | ||
2833 | template<int size, bool big_endian> | |
2834 | void | |
6fa2a40b | 2835 | Sized_relobj_file<size, big_endian>::do_get_global_symbol_counts( |
92de84a6 ILT |
2836 | const Symbol_table*, |
2837 | size_t* defined, | |
2838 | size_t* used) const | |
2839 | { | |
2840 | *defined = this->defined_count_; | |
2841 | size_t count = 0; | |
cdc29364 | 2842 | for (typename Symbols::const_iterator p = this->symbols_.begin(); |
92de84a6 ILT |
2843 | p != this->symbols_.end(); |
2844 | ++p) | |
2845 | if (*p != NULL | |
2846 | && (*p)->source() == Symbol::FROM_OBJECT | |
2847 | && (*p)->object() == this | |
2848 | && (*p)->is_defined()) | |
2849 | ++count; | |
2850 | *used = count; | |
2851 | } | |
2852 | ||
5dd8762a CC |
2853 | // Return a view of the decompressed contents of a section. Set *PLEN |
2854 | // to the size. Set *IS_NEW to true if the contents need to be freed | |
2855 | // by the caller. | |
2856 | ||
5dd8762a | 2857 | const unsigned char* |
0d5bbdb0 | 2858 | Object::decompressed_section_contents( |
5dd8762a CC |
2859 | unsigned int shndx, |
2860 | section_size_type* plen, | |
2861 | bool* is_new) | |
2862 | { | |
2863 | section_size_type buffer_size; | |
c1027032 CC |
2864 | const unsigned char* buffer = this->do_section_contents(shndx, &buffer_size, |
2865 | false); | |
5dd8762a CC |
2866 | |
2867 | if (this->compressed_sections_ == NULL) | |
2868 | { | |
2869 | *plen = buffer_size; | |
2870 | *is_new = false; | |
2871 | return buffer; | |
2872 | } | |
2873 | ||
2874 | Compressed_section_map::const_iterator p = | |
2875 | this->compressed_sections_->find(shndx); | |
2876 | if (p == this->compressed_sections_->end()) | |
2877 | { | |
2878 | *plen = buffer_size; | |
2879 | *is_new = false; | |
2880 | return buffer; | |
2881 | } | |
2882 | ||
2883 | section_size_type uncompressed_size = p->second.size; | |
2884 | if (p->second.contents != NULL) | |
2885 | { | |
2886 | *plen = uncompressed_size; | |
2887 | *is_new = false; | |
2888 | return p->second.contents; | |
2889 | } | |
2890 | ||
2891 | unsigned char* uncompressed_data = new unsigned char[uncompressed_size]; | |
2892 | if (!decompress_input_section(buffer, | |
2893 | buffer_size, | |
2894 | uncompressed_data, | |
2895 | uncompressed_size)) | |
2896 | this->error(_("could not decompress section %s"), | |
2897 | this->do_section_name(shndx).c_str()); | |
2898 | ||
2899 | // We could cache the results in p->second.contents and store | |
2900 | // false in *IS_NEW, but build_compressed_section_map() would | |
2901 | // have done so if it had expected it to be profitable. If | |
2902 | // we reach this point, we expect to need the contents only | |
2903 | // once in this pass. | |
2904 | *plen = uncompressed_size; | |
2905 | *is_new = true; | |
2906 | return uncompressed_data; | |
2907 | } | |
2908 | ||
2909 | // Discard any buffers of uncompressed sections. This is done | |
2910 | // at the end of the Add_symbols task. | |
2911 | ||
5dd8762a | 2912 | void |
0d5bbdb0 | 2913 | Object::discard_decompressed_sections() |
5dd8762a CC |
2914 | { |
2915 | if (this->compressed_sections_ == NULL) | |
2916 | return; | |
2917 | ||
2918 | for (Compressed_section_map::iterator p = this->compressed_sections_->begin(); | |
2919 | p != this->compressed_sections_->end(); | |
2920 | ++p) | |
2921 | { | |
2922 | if (p->second.contents != NULL) | |
2e702c99 RM |
2923 | { |
2924 | delete[] p->second.contents; | |
2925 | p->second.contents = NULL; | |
2926 | } | |
5dd8762a CC |
2927 | } |
2928 | } | |
2929 | ||
54dc6425 ILT |
2930 | // Input_objects methods. |
2931 | ||
008db82e ILT |
2932 | // Add a regular relocatable object to the list. Return false if this |
2933 | // object should be ignored. | |
f6ce93d6 | 2934 | |
008db82e | 2935 | bool |
54dc6425 ILT |
2936 | Input_objects::add_object(Object* obj) |
2937 | { | |
c5818ff1 CC |
2938 | // Print the filename if the -t/--trace option is selected. |
2939 | if (parameters->options().trace()) | |
2940 | gold_info("%s", obj->name().c_str()); | |
2941 | ||
008db82e | 2942 | if (!obj->is_dynamic()) |
f6ce93d6 | 2943 | this->relobj_list_.push_back(static_cast<Relobj*>(obj)); |
008db82e ILT |
2944 | else |
2945 | { | |
2946 | // See if this is a duplicate SONAME. | |
2947 | Dynobj* dynobj = static_cast<Dynobj*>(obj); | |
9a2d6984 | 2948 | const char* soname = dynobj->soname(); |
008db82e ILT |
2949 | |
2950 | std::pair<Unordered_set<std::string>::iterator, bool> ins = | |
9a2d6984 | 2951 | this->sonames_.insert(soname); |
008db82e ILT |
2952 | if (!ins.second) |
2953 | { | |
2954 | // We have already seen a dynamic object with this soname. | |
2955 | return false; | |
2956 | } | |
2957 | ||
2958 | this->dynobj_list_.push_back(dynobj); | |
2959 | } | |
75f65a3e | 2960 | |
92de84a6 | 2961 | // Add this object to the cross-referencer if requested. |
dde3f402 ILT |
2962 | if (parameters->options().user_set_print_symbol_counts() |
2963 | || parameters->options().cref()) | |
92de84a6 ILT |
2964 | { |
2965 | if (this->cref_ == NULL) | |
2966 | this->cref_ = new Cref(); | |
2967 | this->cref_->add_object(obj); | |
2968 | } | |
2969 | ||
008db82e | 2970 | return true; |
54dc6425 ILT |
2971 | } |
2972 | ||
e2827e5f ILT |
2973 | // For each dynamic object, record whether we've seen all of its |
2974 | // explicit dependencies. | |
2975 | ||
2976 | void | |
2977 | Input_objects::check_dynamic_dependencies() const | |
2978 | { | |
7eaea549 | 2979 | bool issued_copy_dt_needed_error = false; |
e2827e5f ILT |
2980 | for (Dynobj_list::const_iterator p = this->dynobj_list_.begin(); |
2981 | p != this->dynobj_list_.end(); | |
2982 | ++p) | |
2983 | { | |
2984 | const Dynobj::Needed& needed((*p)->needed()); | |
2985 | bool found_all = true; | |
7eaea549 ILT |
2986 | Dynobj::Needed::const_iterator pneeded; |
2987 | for (pneeded = needed.begin(); pneeded != needed.end(); ++pneeded) | |
e2827e5f ILT |
2988 | { |
2989 | if (this->sonames_.find(*pneeded) == this->sonames_.end()) | |
2990 | { | |
2991 | found_all = false; | |
2992 | break; | |
2993 | } | |
2994 | } | |
2995 | (*p)->set_has_unknown_needed_entries(!found_all); | |
7eaea549 ILT |
2996 | |
2997 | // --copy-dt-needed-entries aka --add-needed is a GNU ld option | |
612bdda1 ILT |
2998 | // that gold does not support. However, they cause no trouble |
2999 | // unless there is a DT_NEEDED entry that we don't know about; | |
3000 | // warn only in that case. | |
7eaea549 ILT |
3001 | if (!found_all |
3002 | && !issued_copy_dt_needed_error | |
3003 | && (parameters->options().copy_dt_needed_entries() | |
3004 | || parameters->options().add_needed())) | |
3005 | { | |
3006 | const char* optname; | |
3007 | if (parameters->options().copy_dt_needed_entries()) | |
3008 | optname = "--copy-dt-needed-entries"; | |
3009 | else | |
3010 | optname = "--add-needed"; | |
3011 | gold_error(_("%s is not supported but is required for %s in %s"), | |
3012 | optname, (*pneeded).c_str(), (*p)->name().c_str()); | |
3013 | issued_copy_dt_needed_error = true; | |
3014 | } | |
e2827e5f ILT |
3015 | } |
3016 | } | |
3017 | ||
92de84a6 ILT |
3018 | // Start processing an archive. |
3019 | ||
3020 | void | |
3021 | Input_objects::archive_start(Archive* archive) | |
3022 | { | |
dde3f402 ILT |
3023 | if (parameters->options().user_set_print_symbol_counts() |
3024 | || parameters->options().cref()) | |
92de84a6 ILT |
3025 | { |
3026 | if (this->cref_ == NULL) | |
3027 | this->cref_ = new Cref(); | |
3028 | this->cref_->add_archive_start(archive); | |
3029 | } | |
3030 | } | |
3031 | ||
3032 | // Stop processing an archive. | |
3033 | ||
3034 | void | |
3035 | Input_objects::archive_stop(Archive* archive) | |
3036 | { | |
dde3f402 ILT |
3037 | if (parameters->options().user_set_print_symbol_counts() |
3038 | || parameters->options().cref()) | |
92de84a6 ILT |
3039 | this->cref_->add_archive_stop(archive); |
3040 | } | |
3041 | ||
3042 | // Print symbol counts | |
3043 | ||
3044 | void | |
3045 | Input_objects::print_symbol_counts(const Symbol_table* symtab) const | |
3046 | { | |
3047 | if (parameters->options().user_set_print_symbol_counts() | |
3048 | && this->cref_ != NULL) | |
3049 | this->cref_->print_symbol_counts(symtab); | |
3050 | } | |
3051 | ||
dde3f402 ILT |
3052 | // Print a cross reference table. |
3053 | ||
3054 | void | |
3055 | Input_objects::print_cref(const Symbol_table* symtab, FILE* f) const | |
3056 | { | |
3057 | if (parameters->options().cref() && this->cref_ != NULL) | |
3058 | this->cref_->print_cref(symtab, f); | |
3059 | } | |
3060 | ||
92e059d8 ILT |
3061 | // Relocate_info methods. |
3062 | ||
308ecdc7 ILT |
3063 | // Return a string describing the location of a relocation when file |
3064 | // and lineno information is not available. This is only used in | |
3065 | // error messages. | |
92e059d8 ILT |
3066 | |
3067 | template<int size, bool big_endian> | |
3068 | std::string | |
f7e2ee48 | 3069 | Relocate_info<size, big_endian>::location(size_t, off_t offset) const |
92e059d8 | 3070 | { |
a55ce7fe | 3071 | Sized_dwarf_line_info<size, big_endian> line_info(this->object); |
308ecdc7 ILT |
3072 | std::string ret = line_info.addr2line(this->data_shndx, offset, NULL); |
3073 | if (!ret.empty()) | |
3074 | return ret; | |
3075 | ||
3076 | ret = this->object->name(); | |
4c50553d | 3077 | |
f7e2ee48 ILT |
3078 | Symbol_location_info info; |
3079 | if (this->object->get_symbol_location_info(this->data_shndx, offset, &info)) | |
3080 | { | |
308ecdc7 ILT |
3081 | if (!info.source_file.empty()) |
3082 | { | |
3083 | ret += ":"; | |
3084 | ret += info.source_file; | |
3085 | } | |
60e8b3fc CC |
3086 | ret += ":"; |
3087 | if (info.enclosing_symbol_type == elfcpp::STT_FUNC) | |
3088 | ret += _("function "); | |
3089 | ret += info.enclosing_symbol_name; | |
308ecdc7 | 3090 | return ret; |
f7e2ee48 | 3091 | } |
308ecdc7 ILT |
3092 | |
3093 | ret += "("; | |
3094 | ret += this->object->section_name(this->data_shndx); | |
3095 | char buf[100]; | |
3096 | snprintf(buf, sizeof buf, "+0x%lx)", static_cast<long>(offset)); | |
3097 | ret += buf; | |
92e059d8 ILT |
3098 | return ret; |
3099 | } | |
3100 | ||
bae7f79e ILT |
3101 | } // End namespace gold. |
3102 | ||
3103 | namespace | |
3104 | { | |
3105 | ||
3106 | using namespace gold; | |
3107 | ||
3108 | // Read an ELF file with the header and return the appropriate | |
3109 | // instance of Object. | |
3110 | ||
3111 | template<int size, bool big_endian> | |
3112 | Object* | |
3113 | make_elf_sized_object(const std::string& name, Input_file* input_file, | |
029ba973 ILT |
3114 | off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr, |
3115 | bool* punconfigured) | |
bae7f79e | 3116 | { |
2e702c99 RM |
3117 | Target* target = select_target(input_file, offset, |
3118 | ehdr.get_e_machine(), size, big_endian, | |
f733487b DK |
3119 | ehdr.get_e_ident()[elfcpp::EI_OSABI], |
3120 | ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]); | |
3121 | if (target == NULL) | |
3122 | gold_fatal(_("%s: unsupported ELF machine number %d"), | |
3123 | name.c_str(), ehdr.get_e_machine()); | |
029ba973 ILT |
3124 | |
3125 | if (!parameters->target_valid()) | |
3126 | set_parameters_target(target); | |
3127 | else if (target != ¶meters->target()) | |
3128 | { | |
3129 | if (punconfigured != NULL) | |
3130 | *punconfigured = true; | |
3131 | else | |
3132 | gold_error(_("%s: incompatible target"), name.c_str()); | |
3133 | return NULL; | |
3134 | } | |
3135 | ||
f733487b DK |
3136 | return target->make_elf_object<size, big_endian>(name, input_file, offset, |
3137 | ehdr); | |
bae7f79e ILT |
3138 | } |
3139 | ||
3140 | } // End anonymous namespace. | |
3141 | ||
3142 | namespace gold | |
3143 | { | |
3144 | ||
f6060a4d ILT |
3145 | // Return whether INPUT_FILE is an ELF object. |
3146 | ||
3147 | bool | |
3148 | is_elf_object(Input_file* input_file, off_t offset, | |
ca09d69a | 3149 | const unsigned char** start, int* read_size) |
f6060a4d ILT |
3150 | { |
3151 | off_t filesize = input_file->file().filesize(); | |
c549a694 | 3152 | int want = elfcpp::Elf_recognizer::max_header_size; |
f6060a4d ILT |
3153 | if (filesize - offset < want) |
3154 | want = filesize - offset; | |
3155 | ||
3156 | const unsigned char* p = input_file->file().get_view(offset, 0, want, | |
3157 | true, false); | |
3158 | *start = p; | |
3159 | *read_size = want; | |
3160 | ||
c549a694 | 3161 | return elfcpp::Elf_recognizer::is_elf_file(p, want); |
f6060a4d ILT |
3162 | } |
3163 | ||
bae7f79e ILT |
3164 | // Read an ELF file and return the appropriate instance of Object. |
3165 | ||
3166 | Object* | |
3167 | make_elf_object(const std::string& name, Input_file* input_file, off_t offset, | |
15f8229b ILT |
3168 | const unsigned char* p, section_offset_type bytes, |
3169 | bool* punconfigured) | |
bae7f79e | 3170 | { |
15f8229b ILT |
3171 | if (punconfigured != NULL) |
3172 | *punconfigured = false; | |
3173 | ||
c549a694 | 3174 | std::string error; |
ac33a407 DK |
3175 | bool big_endian = false; |
3176 | int size = 0; | |
c549a694 | 3177 | if (!elfcpp::Elf_recognizer::is_valid_header(p, bytes, &size, |
2e702c99 | 3178 | &big_endian, &error)) |
bae7f79e | 3179 | { |
c549a694 | 3180 | gold_error(_("%s: %s"), name.c_str(), error.c_str()); |
75f2446e | 3181 | return NULL; |
bae7f79e ILT |
3182 | } |
3183 | ||
c549a694 | 3184 | if (size == 32) |
bae7f79e | 3185 | { |
bae7f79e ILT |
3186 | if (big_endian) |
3187 | { | |
193a53d9 | 3188 | #ifdef HAVE_TARGET_32_BIG |
bae7f79e ILT |
3189 | elfcpp::Ehdr<32, true> ehdr(p); |
3190 | return make_elf_sized_object<32, true>(name, input_file, | |
029ba973 | 3191 | offset, ehdr, punconfigured); |
193a53d9 | 3192 | #else |
15f8229b ILT |
3193 | if (punconfigured != NULL) |
3194 | *punconfigured = true; | |
3195 | else | |
3196 | gold_error(_("%s: not configured to support " | |
3197 | "32-bit big-endian object"), | |
3198 | name.c_str()); | |
75f2446e | 3199 | return NULL; |
193a53d9 | 3200 | #endif |
bae7f79e ILT |
3201 | } |
3202 | else | |
3203 | { | |
193a53d9 | 3204 | #ifdef HAVE_TARGET_32_LITTLE |
bae7f79e ILT |
3205 | elfcpp::Ehdr<32, false> ehdr(p); |
3206 | return make_elf_sized_object<32, false>(name, input_file, | |
029ba973 | 3207 | offset, ehdr, punconfigured); |
193a53d9 | 3208 | #else |
15f8229b ILT |
3209 | if (punconfigured != NULL) |
3210 | *punconfigured = true; | |
3211 | else | |
3212 | gold_error(_("%s: not configured to support " | |
3213 | "32-bit little-endian object"), | |
3214 | name.c_str()); | |
75f2446e | 3215 | return NULL; |
193a53d9 | 3216 | #endif |
bae7f79e ILT |
3217 | } |
3218 | } | |
c549a694 | 3219 | else if (size == 64) |
bae7f79e | 3220 | { |
bae7f79e ILT |
3221 | if (big_endian) |
3222 | { | |
193a53d9 | 3223 | #ifdef HAVE_TARGET_64_BIG |
bae7f79e ILT |
3224 | elfcpp::Ehdr<64, true> ehdr(p); |
3225 | return make_elf_sized_object<64, true>(name, input_file, | |
029ba973 | 3226 | offset, ehdr, punconfigured); |
193a53d9 | 3227 | #else |
15f8229b ILT |
3228 | if (punconfigured != NULL) |
3229 | *punconfigured = true; | |
3230 | else | |
3231 | gold_error(_("%s: not configured to support " | |
3232 | "64-bit big-endian object"), | |
3233 | name.c_str()); | |
75f2446e | 3234 | return NULL; |
193a53d9 | 3235 | #endif |
bae7f79e ILT |
3236 | } |
3237 | else | |
3238 | { | |
193a53d9 | 3239 | #ifdef HAVE_TARGET_64_LITTLE |
bae7f79e ILT |
3240 | elfcpp::Ehdr<64, false> ehdr(p); |
3241 | return make_elf_sized_object<64, false>(name, input_file, | |
029ba973 | 3242 | offset, ehdr, punconfigured); |
193a53d9 | 3243 | #else |
15f8229b ILT |
3244 | if (punconfigured != NULL) |
3245 | *punconfigured = true; | |
3246 | else | |
3247 | gold_error(_("%s: not configured to support " | |
3248 | "64-bit little-endian object"), | |
3249 | name.c_str()); | |
75f2446e | 3250 | return NULL; |
193a53d9 | 3251 | #endif |
bae7f79e ILT |
3252 | } |
3253 | } | |
c549a694 ILT |
3254 | else |
3255 | gold_unreachable(); | |
bae7f79e ILT |
3256 | } |
3257 | ||
04bf7072 ILT |
3258 | // Instantiate the templates we need. |
3259 | ||
dbe40a88 RÁE |
3260 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) |
3261 | template | |
3262 | void | |
3263 | Relobj::initialize_input_to_output_map<64>(unsigned int shndx, | |
beb8418f | 3264 | elfcpp::Elf_types<64>::Elf_Addr starting_address, |
dbe40a88 | 3265 | Unordered_map<section_offset_type, |
beb8418f | 3266 | elfcpp::Elf_types<64>::Elf_Addr>* output_addresses) const; |
dbe40a88 RÁE |
3267 | #endif |
3268 | ||
3269 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) | |
3270 | template | |
3271 | void | |
3272 | Relobj::initialize_input_to_output_map<32>(unsigned int shndx, | |
beb8418f | 3273 | elfcpp::Elf_types<32>::Elf_Addr starting_address, |
dbe40a88 | 3274 | Unordered_map<section_offset_type, |
beb8418f | 3275 | elfcpp::Elf_types<32>::Elf_Addr>* output_addresses) const; |
dbe40a88 RÁE |
3276 | #endif |
3277 | ||
04bf7072 ILT |
3278 | #ifdef HAVE_TARGET_32_LITTLE |
3279 | template | |
3280 | void | |
3281 | Object::read_section_data<32, false>(elfcpp::Elf_file<32, false, Object>*, | |
3282 | Read_symbols_data*); | |
dc3714f3 AM |
3283 | template |
3284 | const unsigned char* | |
3285 | Object::find_shdr<32,false>(const unsigned char*, const char*, const char*, | |
3286 | section_size_type, const unsigned char*) const; | |
04bf7072 ILT |
3287 | #endif |
3288 | ||
3289 | #ifdef HAVE_TARGET_32_BIG | |
3290 | template | |
3291 | void | |
3292 | Object::read_section_data<32, true>(elfcpp::Elf_file<32, true, Object>*, | |
3293 | Read_symbols_data*); | |
dc3714f3 AM |
3294 | template |
3295 | const unsigned char* | |
3296 | Object::find_shdr<32,true>(const unsigned char*, const char*, const char*, | |
3297 | section_size_type, const unsigned char*) const; | |
04bf7072 ILT |
3298 | #endif |
3299 | ||
3300 | #ifdef HAVE_TARGET_64_LITTLE | |
3301 | template | |
3302 | void | |
3303 | Object::read_section_data<64, false>(elfcpp::Elf_file<64, false, Object>*, | |
3304 | Read_symbols_data*); | |
dc3714f3 AM |
3305 | template |
3306 | const unsigned char* | |
3307 | Object::find_shdr<64,false>(const unsigned char*, const char*, const char*, | |
3308 | section_size_type, const unsigned char*) const; | |
04bf7072 ILT |
3309 | #endif |
3310 | ||
3311 | #ifdef HAVE_TARGET_64_BIG | |
3312 | template | |
3313 | void | |
3314 | Object::read_section_data<64, true>(elfcpp::Elf_file<64, true, Object>*, | |
3315 | Read_symbols_data*); | |
dc3714f3 AM |
3316 | template |
3317 | const unsigned char* | |
3318 | Object::find_shdr<64,true>(const unsigned char*, const char*, const char*, | |
3319 | section_size_type, const unsigned char*) const; | |
04bf7072 | 3320 | #endif |
bae7f79e | 3321 | |
193a53d9 | 3322 | #ifdef HAVE_TARGET_32_LITTLE |
c6905c28 CC |
3323 | template |
3324 | class Sized_relobj<32, false>; | |
3325 | ||
bae7f79e | 3326 | template |
6fa2a40b | 3327 | class Sized_relobj_file<32, false>; |
193a53d9 | 3328 | #endif |
bae7f79e | 3329 | |
193a53d9 | 3330 | #ifdef HAVE_TARGET_32_BIG |
c6905c28 CC |
3331 | template |
3332 | class Sized_relobj<32, true>; | |
3333 | ||
bae7f79e | 3334 | template |
6fa2a40b | 3335 | class Sized_relobj_file<32, true>; |
193a53d9 | 3336 | #endif |
bae7f79e | 3337 | |
193a53d9 | 3338 | #ifdef HAVE_TARGET_64_LITTLE |
c6905c28 CC |
3339 | template |
3340 | class Sized_relobj<64, false>; | |
3341 | ||
bae7f79e | 3342 | template |
6fa2a40b | 3343 | class Sized_relobj_file<64, false>; |
193a53d9 | 3344 | #endif |
bae7f79e | 3345 | |
193a53d9 | 3346 | #ifdef HAVE_TARGET_64_BIG |
c6905c28 CC |
3347 | template |
3348 | class Sized_relobj<64, true>; | |
3349 | ||
bae7f79e | 3350 | template |
6fa2a40b | 3351 | class Sized_relobj_file<64, true>; |
193a53d9 | 3352 | #endif |
bae7f79e | 3353 | |
193a53d9 | 3354 | #ifdef HAVE_TARGET_32_LITTLE |
92e059d8 ILT |
3355 | template |
3356 | struct Relocate_info<32, false>; | |
193a53d9 | 3357 | #endif |
92e059d8 | 3358 | |
193a53d9 | 3359 | #ifdef HAVE_TARGET_32_BIG |
92e059d8 ILT |
3360 | template |
3361 | struct Relocate_info<32, true>; | |
193a53d9 | 3362 | #endif |
92e059d8 | 3363 | |
193a53d9 | 3364 | #ifdef HAVE_TARGET_64_LITTLE |
92e059d8 ILT |
3365 | template |
3366 | struct Relocate_info<64, false>; | |
193a53d9 | 3367 | #endif |
92e059d8 | 3368 | |
193a53d9 | 3369 | #ifdef HAVE_TARGET_64_BIG |
92e059d8 ILT |
3370 | template |
3371 | struct Relocate_info<64, true>; | |
193a53d9 | 3372 | #endif |
92e059d8 | 3373 | |
9d3b86f6 ILT |
3374 | #ifdef HAVE_TARGET_32_LITTLE |
3375 | template | |
3376 | void | |
3377 | Xindex::initialize_symtab_xindex<32, false>(Object*, unsigned int); | |
3378 | ||
3379 | template | |
3380 | void | |
3381 | Xindex::read_symtab_xindex<32, false>(Object*, unsigned int, | |
3382 | const unsigned char*); | |
3383 | #endif | |
3384 | ||
3385 | #ifdef HAVE_TARGET_32_BIG | |
3386 | template | |
3387 | void | |
3388 | Xindex::initialize_symtab_xindex<32, true>(Object*, unsigned int); | |
3389 | ||
3390 | template | |
3391 | void | |
3392 | Xindex::read_symtab_xindex<32, true>(Object*, unsigned int, | |
3393 | const unsigned char*); | |
3394 | #endif | |
3395 | ||
3396 | #ifdef HAVE_TARGET_64_LITTLE | |
3397 | template | |
3398 | void | |
3399 | Xindex::initialize_symtab_xindex<64, false>(Object*, unsigned int); | |
3400 | ||
3401 | template | |
3402 | void | |
3403 | Xindex::read_symtab_xindex<64, false>(Object*, unsigned int, | |
3404 | const unsigned char*); | |
3405 | #endif | |
3406 | ||
3407 | #ifdef HAVE_TARGET_64_BIG | |
3408 | template | |
3409 | void | |
3410 | Xindex::initialize_symtab_xindex<64, true>(Object*, unsigned int); | |
3411 | ||
3412 | template | |
3413 | void | |
3414 | Xindex::read_symtab_xindex<64, true>(Object*, unsigned int, | |
3415 | const unsigned char*); | |
3416 | #endif | |
3417 | ||
bae7f79e | 3418 | } // End namespace gold. |