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