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