Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // object.cc -- support for an object file for linking in gold |
2 | ||
6d03d481 | 3 | // Copyright 2006, 2007, 2008, 2009 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" |
bae7f79e ILT |
42 | |
43 | namespace gold | |
44 | { | |
45 | ||
d491d34e ILT |
46 | // Class Xindex. |
47 | ||
48 | // Initialize the symtab_xindex_ array. Find the SHT_SYMTAB_SHNDX | |
49 | // section and read it in. SYMTAB_SHNDX is the index of the symbol | |
50 | // table we care about. | |
51 | ||
52 | template<int size, bool big_endian> | |
53 | void | |
54 | Xindex::initialize_symtab_xindex(Object* object, unsigned int symtab_shndx) | |
55 | { | |
56 | if (!this->symtab_xindex_.empty()) | |
57 | return; | |
58 | ||
59 | gold_assert(symtab_shndx != 0); | |
60 | ||
61 | // Look through the sections in reverse order, on the theory that it | |
62 | // is more likely to be near the end than the beginning. | |
63 | unsigned int i = object->shnum(); | |
64 | while (i > 0) | |
65 | { | |
66 | --i; | |
67 | if (object->section_type(i) == elfcpp::SHT_SYMTAB_SHNDX | |
68 | && this->adjust_shndx(object->section_link(i)) == symtab_shndx) | |
69 | { | |
70 | this->read_symtab_xindex<size, big_endian>(object, i, NULL); | |
71 | return; | |
72 | } | |
73 | } | |
74 | ||
75 | object->error(_("missing SHT_SYMTAB_SHNDX section")); | |
76 | } | |
77 | ||
78 | // Read in the symtab_xindex_ array, given the section index of the | |
79 | // SHT_SYMTAB_SHNDX section. If PSHDRS is not NULL, it points at the | |
80 | // section headers. | |
81 | ||
82 | template<int size, bool big_endian> | |
83 | void | |
84 | Xindex::read_symtab_xindex(Object* object, unsigned int xindex_shndx, | |
85 | const unsigned char* pshdrs) | |
86 | { | |
87 | section_size_type bytecount; | |
88 | const unsigned char* contents; | |
89 | if (pshdrs == NULL) | |
90 | contents = object->section_contents(xindex_shndx, &bytecount, false); | |
91 | else | |
92 | { | |
93 | const unsigned char* p = (pshdrs | |
94 | + (xindex_shndx | |
95 | * elfcpp::Elf_sizes<size>::shdr_size)); | |
96 | typename elfcpp::Shdr<size, big_endian> shdr(p); | |
97 | bytecount = convert_to_section_size_type(shdr.get_sh_size()); | |
98 | contents = object->get_view(shdr.get_sh_offset(), bytecount, true, false); | |
99 | } | |
100 | ||
101 | gold_assert(this->symtab_xindex_.empty()); | |
102 | this->symtab_xindex_.reserve(bytecount / 4); | |
103 | for (section_size_type i = 0; i < bytecount; i += 4) | |
104 | { | |
105 | unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(contents + i); | |
106 | // We preadjust the section indexes we save. | |
107 | this->symtab_xindex_.push_back(this->adjust_shndx(shndx)); | |
108 | } | |
109 | } | |
110 | ||
111 | // Symbol symndx has a section of SHN_XINDEX; return the real section | |
112 | // index. | |
113 | ||
114 | unsigned int | |
115 | Xindex::sym_xindex_to_shndx(Object* object, unsigned int symndx) | |
116 | { | |
117 | if (symndx >= this->symtab_xindex_.size()) | |
118 | { | |
119 | object->error(_("symbol %u out of range for SHT_SYMTAB_SHNDX section"), | |
120 | symndx); | |
121 | return elfcpp::SHN_UNDEF; | |
122 | } | |
123 | unsigned int shndx = this->symtab_xindex_[symndx]; | |
124 | if (shndx < elfcpp::SHN_LORESERVE || shndx >= object->shnum()) | |
125 | { | |
126 | object->error(_("extended index for symbol %u out of range: %u"), | |
127 | symndx, shndx); | |
128 | return elfcpp::SHN_UNDEF; | |
129 | } | |
130 | return shndx; | |
131 | } | |
132 | ||
645f8123 ILT |
133 | // Class Object. |
134 | ||
dbe717ef ILT |
135 | // Set the target based on fields in the ELF file header. |
136 | ||
137 | void | |
138 | Object::set_target(int machine, int size, bool big_endian, int osabi, | |
139 | int abiversion) | |
140 | { | |
141 | Target* target = select_target(machine, size, big_endian, osabi, abiversion); | |
142 | if (target == NULL) | |
75f2446e ILT |
143 | gold_fatal(_("%s: unsupported ELF machine number %d"), |
144 | this->name().c_str(), machine); | |
dbe717ef ILT |
145 | this->target_ = target; |
146 | } | |
147 | ||
75f2446e ILT |
148 | // Report an error for this object file. This is used by the |
149 | // elfcpp::Elf_file interface, and also called by the Object code | |
150 | // itself. | |
645f8123 ILT |
151 | |
152 | void | |
75f2446e | 153 | Object::error(const char* format, ...) const |
645f8123 ILT |
154 | { |
155 | va_list args; | |
645f8123 | 156 | va_start(args, format); |
75f2446e ILT |
157 | char* buf = NULL; |
158 | if (vasprintf(&buf, format, args) < 0) | |
159 | gold_nomem(); | |
645f8123 | 160 | va_end(args); |
75f2446e ILT |
161 | gold_error(_("%s: %s"), this->name().c_str(), buf); |
162 | free(buf); | |
645f8123 ILT |
163 | } |
164 | ||
165 | // Return a view of the contents of a section. | |
166 | ||
167 | const unsigned char* | |
8383303e ILT |
168 | Object::section_contents(unsigned int shndx, section_size_type* plen, |
169 | bool cache) | |
645f8123 ILT |
170 | { |
171 | Location loc(this->do_section_contents(shndx)); | |
8383303e | 172 | *plen = convert_to_section_size_type(loc.data_size); |
8d63875c ILT |
173 | if (*plen == 0) |
174 | { | |
175 | static const unsigned char empty[1] = { '\0' }; | |
176 | return empty; | |
177 | } | |
39d0cb0e | 178 | return this->get_view(loc.file_offset, *plen, true, cache); |
645f8123 ILT |
179 | } |
180 | ||
dbe717ef ILT |
181 | // Read the section data into SD. This is code common to Sized_relobj |
182 | // and Sized_dynobj, so we put it into Object. | |
183 | ||
184 | template<int size, bool big_endian> | |
185 | void | |
186 | Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file, | |
187 | Read_symbols_data* sd) | |
188 | { | |
189 | const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; | |
190 | ||
191 | // Read the section headers. | |
192 | const off_t shoff = elf_file->shoff(); | |
193 | const unsigned int shnum = this->shnum(); | |
39d0cb0e ILT |
194 | sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size, |
195 | true, true); | |
dbe717ef ILT |
196 | |
197 | // Read the section names. | |
198 | const unsigned char* pshdrs = sd->section_headers->data(); | |
199 | const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size; | |
200 | typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames); | |
201 | ||
202 | if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB) | |
75f2446e ILT |
203 | this->error(_("section name section has wrong type: %u"), |
204 | static_cast<unsigned int>(shdrnames.get_sh_type())); | |
dbe717ef | 205 | |
8383303e ILT |
206 | sd->section_names_size = |
207 | convert_to_section_size_type(shdrnames.get_sh_size()); | |
dbe717ef | 208 | sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(), |
39d0cb0e ILT |
209 | sd->section_names_size, false, |
210 | false); | |
dbe717ef ILT |
211 | } |
212 | ||
213 | // If NAME is the name of a special .gnu.warning section, arrange for | |
214 | // the warning to be issued. SHNDX is the section index. Return | |
215 | // whether it is a warning section. | |
216 | ||
217 | bool | |
218 | Object::handle_gnu_warning_section(const char* name, unsigned int shndx, | |
219 | Symbol_table* symtab) | |
220 | { | |
221 | const char warn_prefix[] = ".gnu.warning."; | |
222 | const int warn_prefix_len = sizeof warn_prefix - 1; | |
223 | if (strncmp(name, warn_prefix, warn_prefix_len) == 0) | |
224 | { | |
cb295612 ILT |
225 | // Read the section contents to get the warning text. It would |
226 | // be nicer if we only did this if we have to actually issue a | |
227 | // warning. Unfortunately, warnings are issued as we relocate | |
228 | // sections. That means that we can not lock the object then, | |
229 | // as we might try to issue the same warning multiple times | |
230 | // simultaneously. | |
231 | section_size_type len; | |
232 | const unsigned char* contents = this->section_contents(shndx, &len, | |
233 | false); | |
8d63875c ILT |
234 | if (len == 0) |
235 | { | |
236 | const char* warning = name + warn_prefix_len; | |
237 | contents = reinterpret_cast<const unsigned char*>(warning); | |
238 | len = strlen(warning); | |
239 | } | |
cb295612 ILT |
240 | std::string warning(reinterpret_cast<const char*>(contents), len); |
241 | symtab->add_warning(name + warn_prefix_len, this, warning); | |
dbe717ef ILT |
242 | return true; |
243 | } | |
244 | return false; | |
245 | } | |
246 | ||
6d03d481 ST |
247 | // Class Relobj |
248 | ||
249 | // To copy the symbols data read from the file to a local data structure. | |
250 | // This function is called from do_layout only while doing garbage | |
251 | // collection. | |
252 | ||
253 | void | |
254 | Relobj::copy_symbols_data(Symbols_data* gc_sd, Read_symbols_data* sd, | |
255 | unsigned int section_header_size) | |
256 | { | |
257 | gc_sd->section_headers_data = | |
258 | new unsigned char[(section_header_size)]; | |
259 | memcpy(gc_sd->section_headers_data, sd->section_headers->data(), | |
260 | section_header_size); | |
261 | gc_sd->section_names_data = | |
262 | new unsigned char[sd->section_names_size]; | |
263 | memcpy(gc_sd->section_names_data, sd->section_names->data(), | |
264 | sd->section_names_size); | |
265 | gc_sd->section_names_size = sd->section_names_size; | |
266 | if (sd->symbols != NULL) | |
267 | { | |
268 | gc_sd->symbols_data = | |
269 | new unsigned char[sd->symbols_size]; | |
270 | memcpy(gc_sd->symbols_data, sd->symbols->data(), | |
271 | sd->symbols_size); | |
272 | } | |
273 | else | |
274 | { | |
275 | gc_sd->symbols_data = NULL; | |
276 | } | |
277 | gc_sd->symbols_size = sd->symbols_size; | |
278 | gc_sd->external_symbols_offset = sd->external_symbols_offset; | |
279 | if (sd->symbol_names != NULL) | |
280 | { | |
281 | gc_sd->symbol_names_data = | |
282 | new unsigned char[sd->symbol_names_size]; | |
283 | memcpy(gc_sd->symbol_names_data, sd->symbol_names->data(), | |
284 | sd->symbol_names_size); | |
285 | } | |
286 | else | |
287 | { | |
288 | gc_sd->symbol_names_data = NULL; | |
289 | } | |
290 | gc_sd->symbol_names_size = sd->symbol_names_size; | |
291 | } | |
292 | ||
293 | // This function determines if a particular section name must be included | |
294 | // in the link. This is used during garbage collection to determine the | |
295 | // roots of the worklist. | |
296 | ||
297 | bool | |
298 | Relobj::is_section_name_included(const char* name) | |
299 | { | |
300 | if (is_prefix_of(".ctors", name) | |
301 | || is_prefix_of(".dtors", name) | |
302 | || is_prefix_of(".note", name) | |
303 | || is_prefix_of(".init", name) | |
304 | || is_prefix_of(".fini", name) | |
305 | || is_prefix_of(".gcc_except_table", name) | |
306 | || is_prefix_of(".jcr", name) | |
307 | || is_prefix_of(".preinit_array", name) | |
308 | || (is_prefix_of(".text", name) | |
309 | && strstr(name, "personality")) | |
310 | || (is_prefix_of(".data", name) | |
311 | && strstr(name, "personality")) | |
312 | || (is_prefix_of(".gnu.linkonce.d", name) && | |
313 | strstr(name, "personality"))) | |
314 | { | |
315 | return true; | |
316 | } | |
317 | return false; | |
318 | } | |
319 | ||
f6ce93d6 | 320 | // Class Sized_relobj. |
bae7f79e ILT |
321 | |
322 | template<int size, bool big_endian> | |
f6ce93d6 | 323 | Sized_relobj<size, big_endian>::Sized_relobj( |
bae7f79e ILT |
324 | const std::string& name, |
325 | Input_file* input_file, | |
326 | off_t offset, | |
327 | const elfcpp::Ehdr<size, big_endian>& ehdr) | |
f6ce93d6 | 328 | : Relobj(name, input_file, offset), |
645f8123 | 329 | elf_file_(this, ehdr), |
dbe717ef | 330 | symtab_shndx_(-1U), |
61ba1cf9 ILT |
331 | local_symbol_count_(0), |
332 | output_local_symbol_count_(0), | |
7bf1f802 | 333 | output_local_dynsym_count_(0), |
730cdc88 | 334 | symbols_(), |
92de84a6 | 335 | defined_count_(0), |
61ba1cf9 | 336 | local_symbol_offset_(0), |
7bf1f802 | 337 | local_dynsym_offset_(0), |
e727fa71 | 338 | local_values_(), |
730cdc88 | 339 | local_got_offsets_(), |
ef9beddf | 340 | kept_comdat_sections_(), |
805bb01c DK |
341 | has_eh_frame_(false), |
342 | discarded_eh_frame_shndx_(-1U) | |
bae7f79e | 343 | { |
bae7f79e ILT |
344 | } |
345 | ||
346 | template<int size, bool big_endian> | |
f6ce93d6 | 347 | Sized_relobj<size, big_endian>::~Sized_relobj() |
bae7f79e ILT |
348 | { |
349 | } | |
350 | ||
645f8123 | 351 | // Set up an object file based on the file header. This sets up the |
bae7f79e ILT |
352 | // target and reads the section information. |
353 | ||
354 | template<int size, bool big_endian> | |
355 | void | |
f6ce93d6 | 356 | Sized_relobj<size, big_endian>::setup( |
bae7f79e ILT |
357 | const elfcpp::Ehdr<size, big_endian>& ehdr) |
358 | { | |
dbe717ef ILT |
359 | this->set_target(ehdr.get_e_machine(), size, big_endian, |
360 | ehdr.get_e_ident()[elfcpp::EI_OSABI], | |
361 | ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]); | |
12e14209 | 362 | |
dbe717ef | 363 | const unsigned int shnum = this->elf_file_.shnum(); |
a2fb1b05 | 364 | this->set_shnum(shnum); |
dbe717ef | 365 | } |
12e14209 | 366 | |
dbe717ef ILT |
367 | // Find the SHT_SYMTAB section, given the section headers. The ELF |
368 | // standard says that maybe in the future there can be more than one | |
369 | // SHT_SYMTAB section. Until somebody figures out how that could | |
370 | // work, we assume there is only one. | |
12e14209 | 371 | |
dbe717ef ILT |
372 | template<int size, bool big_endian> |
373 | void | |
374 | Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs) | |
375 | { | |
376 | const unsigned int shnum = this->shnum(); | |
377 | this->symtab_shndx_ = 0; | |
378 | if (shnum > 0) | |
bae7f79e | 379 | { |
dbe717ef ILT |
380 | // Look through the sections in reverse order, since gas tends |
381 | // to put the symbol table at the end. | |
382 | const unsigned char* p = pshdrs + shnum * This::shdr_size; | |
383 | unsigned int i = shnum; | |
d491d34e ILT |
384 | unsigned int xindex_shndx = 0; |
385 | unsigned int xindex_link = 0; | |
dbe717ef | 386 | while (i > 0) |
bae7f79e | 387 | { |
dbe717ef ILT |
388 | --i; |
389 | p -= This::shdr_size; | |
390 | typename This::Shdr shdr(p); | |
391 | if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB) | |
392 | { | |
393 | this->symtab_shndx_ = i; | |
d491d34e ILT |
394 | if (xindex_shndx > 0 && xindex_link == i) |
395 | { | |
396 | Xindex* xindex = | |
397 | new Xindex(this->elf_file_.large_shndx_offset()); | |
398 | xindex->read_symtab_xindex<size, big_endian>(this, | |
399 | xindex_shndx, | |
400 | pshdrs); | |
401 | this->set_xindex(xindex); | |
402 | } | |
dbe717ef ILT |
403 | break; |
404 | } | |
d491d34e ILT |
405 | |
406 | // Try to pick up the SHT_SYMTAB_SHNDX section, if there is | |
407 | // one. This will work if it follows the SHT_SYMTAB | |
408 | // section. | |
409 | if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB_SHNDX) | |
410 | { | |
411 | xindex_shndx = i; | |
412 | xindex_link = this->adjust_shndx(shdr.get_sh_link()); | |
413 | } | |
bae7f79e | 414 | } |
bae7f79e ILT |
415 | } |
416 | } | |
417 | ||
d491d34e ILT |
418 | // Return the Xindex structure to use for object with lots of |
419 | // sections. | |
420 | ||
421 | template<int size, bool big_endian> | |
422 | Xindex* | |
423 | Sized_relobj<size, big_endian>::do_initialize_xindex() | |
424 | { | |
425 | gold_assert(this->symtab_shndx_ != -1U); | |
426 | Xindex* xindex = new Xindex(this->elf_file_.large_shndx_offset()); | |
427 | xindex->initialize_symtab_xindex<size, big_endian>(this, this->symtab_shndx_); | |
428 | return xindex; | |
429 | } | |
430 | ||
730cdc88 ILT |
431 | // Return whether SHDR has the right type and flags to be a GNU |
432 | // .eh_frame section. | |
433 | ||
434 | template<int size, bool big_endian> | |
435 | bool | |
436 | Sized_relobj<size, big_endian>::check_eh_frame_flags( | |
437 | const elfcpp::Shdr<size, big_endian>* shdr) const | |
438 | { | |
2c38906f | 439 | return (shdr->get_sh_type() == elfcpp::SHT_PROGBITS |
1650c4ff | 440 | && (shdr->get_sh_flags() & elfcpp::SHF_ALLOC) != 0); |
730cdc88 ILT |
441 | } |
442 | ||
443 | // Return whether there is a GNU .eh_frame section, given the section | |
444 | // headers and the section names. | |
445 | ||
446 | template<int size, bool big_endian> | |
447 | bool | |
8383303e ILT |
448 | Sized_relobj<size, big_endian>::find_eh_frame( |
449 | const unsigned char* pshdrs, | |
450 | const char* names, | |
451 | section_size_type names_size) const | |
730cdc88 ILT |
452 | { |
453 | const unsigned int shnum = this->shnum(); | |
454 | const unsigned char* p = pshdrs + This::shdr_size; | |
455 | for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size) | |
456 | { | |
457 | typename This::Shdr shdr(p); | |
458 | if (this->check_eh_frame_flags(&shdr)) | |
459 | { | |
460 | if (shdr.get_sh_name() >= names_size) | |
461 | { | |
462 | this->error(_("bad section name offset for section %u: %lu"), | |
463 | i, static_cast<unsigned long>(shdr.get_sh_name())); | |
464 | continue; | |
465 | } | |
466 | ||
467 | const char* name = names + shdr.get_sh_name(); | |
468 | if (strcmp(name, ".eh_frame") == 0) | |
469 | return true; | |
470 | } | |
471 | } | |
472 | return false; | |
473 | } | |
474 | ||
12e14209 | 475 | // Read the sections and symbols from an object file. |
bae7f79e ILT |
476 | |
477 | template<int size, bool big_endian> | |
12e14209 | 478 | void |
f6ce93d6 | 479 | Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd) |
bae7f79e | 480 | { |
dbe717ef | 481 | this->read_section_data(&this->elf_file_, sd); |
12e14209 | 482 | |
dbe717ef ILT |
483 | const unsigned char* const pshdrs = sd->section_headers->data(); |
484 | ||
485 | this->find_symtab(pshdrs); | |
12e14209 | 486 | |
730cdc88 ILT |
487 | const unsigned char* namesu = sd->section_names->data(); |
488 | const char* names = reinterpret_cast<const char*>(namesu); | |
1650c4ff ILT |
489 | if (memmem(names, sd->section_names_size, ".eh_frame", 10) != NULL) |
490 | { | |
491 | if (this->find_eh_frame(pshdrs, names, sd->section_names_size)) | |
492 | this->has_eh_frame_ = true; | |
493 | } | |
730cdc88 | 494 | |
75f2446e ILT |
495 | sd->symbols = NULL; |
496 | sd->symbols_size = 0; | |
730cdc88 | 497 | sd->external_symbols_offset = 0; |
75f2446e ILT |
498 | sd->symbol_names = NULL; |
499 | sd->symbol_names_size = 0; | |
500 | ||
645f8123 | 501 | if (this->symtab_shndx_ == 0) |
bae7f79e ILT |
502 | { |
503 | // No symbol table. Weird but legal. | |
12e14209 | 504 | return; |
bae7f79e ILT |
505 | } |
506 | ||
12e14209 ILT |
507 | // Get the symbol table section header. |
508 | typename This::Shdr symtabshdr(pshdrs | |
645f8123 | 509 | + this->symtab_shndx_ * This::shdr_size); |
a3ad94ed | 510 | gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); |
bae7f79e | 511 | |
730cdc88 ILT |
512 | // If this object has a .eh_frame section, we need all the symbols. |
513 | // Otherwise we only need the external symbols. While it would be | |
514 | // simpler to just always read all the symbols, I've seen object | |
515 | // files with well over 2000 local symbols, which for a 64-bit | |
516 | // object file format is over 5 pages that we don't need to read | |
517 | // now. | |
518 | ||
75f65a3e | 519 | const int sym_size = This::sym_size; |
92e059d8 ILT |
520 | const unsigned int loccount = symtabshdr.get_sh_info(); |
521 | this->local_symbol_count_ = loccount; | |
7bf1f802 | 522 | this->local_values_.resize(loccount); |
8383303e | 523 | section_offset_type locsize = loccount * sym_size; |
730cdc88 | 524 | off_t dataoff = symtabshdr.get_sh_offset(); |
8383303e ILT |
525 | section_size_type datasize = |
526 | convert_to_section_size_type(symtabshdr.get_sh_size()); | |
730cdc88 | 527 | off_t extoff = dataoff + locsize; |
8383303e | 528 | section_size_type extsize = datasize - locsize; |
75f65a3e | 529 | |
730cdc88 | 530 | off_t readoff = this->has_eh_frame_ ? dataoff : extoff; |
8383303e | 531 | section_size_type readsize = this->has_eh_frame_ ? datasize : extsize; |
730cdc88 | 532 | |
3f2e6a2d CC |
533 | if (readsize == 0) |
534 | { | |
535 | // No external symbols. Also weird but also legal. | |
536 | return; | |
537 | } | |
538 | ||
39d0cb0e | 539 | File_view* fvsymtab = this->get_lasting_view(readoff, readsize, true, false); |
bae7f79e ILT |
540 | |
541 | // Read the section header for the symbol names. | |
d491d34e | 542 | unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link()); |
dbe717ef | 543 | if (strtab_shndx >= this->shnum()) |
bae7f79e | 544 | { |
75f2446e ILT |
545 | this->error(_("invalid symbol table name index: %u"), strtab_shndx); |
546 | return; | |
bae7f79e | 547 | } |
dbe717ef | 548 | typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size); |
bae7f79e ILT |
549 | if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB) |
550 | { | |
75f2446e ILT |
551 | this->error(_("symbol table name section has wrong type: %u"), |
552 | static_cast<unsigned int>(strtabshdr.get_sh_type())); | |
553 | return; | |
bae7f79e ILT |
554 | } |
555 | ||
556 | // Read the symbol names. | |
557 | File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(), | |
39d0cb0e ILT |
558 | strtabshdr.get_sh_size(), |
559 | false, true); | |
bae7f79e | 560 | |
12e14209 | 561 | sd->symbols = fvsymtab; |
730cdc88 ILT |
562 | sd->symbols_size = readsize; |
563 | sd->external_symbols_offset = this->has_eh_frame_ ? locsize : 0; | |
12e14209 | 564 | sd->symbol_names = fvstrtab; |
8383303e ILT |
565 | sd->symbol_names_size = |
566 | convert_to_section_size_type(strtabshdr.get_sh_size()); | |
a2fb1b05 ILT |
567 | } |
568 | ||
730cdc88 | 569 | // Return the section index of symbol SYM. Set *VALUE to its value in |
d491d34e ILT |
570 | // the object file. Set *IS_ORDINARY if this is an ordinary section |
571 | // index. not a special cod between SHN_LORESERVE and SHN_HIRESERVE. | |
572 | // Note that for a symbol which is not defined in this object file, | |
573 | // this will set *VALUE to 0 and return SHN_UNDEF; it will not return | |
574 | // the final value of the symbol in the link. | |
730cdc88 ILT |
575 | |
576 | template<int size, bool big_endian> | |
577 | unsigned int | |
578 | Sized_relobj<size, big_endian>::symbol_section_and_value(unsigned int sym, | |
d491d34e ILT |
579 | Address* value, |
580 | bool* is_ordinary) | |
730cdc88 | 581 | { |
8383303e | 582 | section_size_type symbols_size; |
730cdc88 ILT |
583 | const unsigned char* symbols = this->section_contents(this->symtab_shndx_, |
584 | &symbols_size, | |
585 | false); | |
586 | ||
587 | const size_t count = symbols_size / This::sym_size; | |
588 | gold_assert(sym < count); | |
589 | ||
590 | elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size); | |
591 | *value = elfsym.get_st_value(); | |
d491d34e ILT |
592 | |
593 | return this->adjust_sym_shndx(sym, elfsym.get_st_shndx(), is_ordinary); | |
730cdc88 ILT |
594 | } |
595 | ||
a2fb1b05 ILT |
596 | // Return whether to include a section group in the link. LAYOUT is |
597 | // used to keep track of which section groups we have already seen. | |
598 | // INDEX is the index of the section group and SHDR is the section | |
599 | // header. If we do not want to include this group, we set bits in | |
600 | // OMIT for each section which should be discarded. | |
601 | ||
602 | template<int size, bool big_endian> | |
603 | bool | |
f6ce93d6 | 604 | Sized_relobj<size, big_endian>::include_section_group( |
6a74a719 | 605 | Symbol_table* symtab, |
a2fb1b05 ILT |
606 | Layout* layout, |
607 | unsigned int index, | |
6a74a719 | 608 | const char* name, |
e94cf127 CC |
609 | const unsigned char* shdrs, |
610 | const char* section_names, | |
611 | section_size_type section_names_size, | |
a2fb1b05 ILT |
612 | std::vector<bool>* omit) |
613 | { | |
614 | // Read the section contents. | |
e94cf127 | 615 | typename This::Shdr shdr(shdrs + index * This::shdr_size); |
a2fb1b05 | 616 | const unsigned char* pcon = this->get_view(shdr.get_sh_offset(), |
39d0cb0e | 617 | shdr.get_sh_size(), true, false); |
a2fb1b05 ILT |
618 | const elfcpp::Elf_Word* pword = |
619 | reinterpret_cast<const elfcpp::Elf_Word*>(pcon); | |
620 | ||
621 | // The first word contains flags. We only care about COMDAT section | |
622 | // groups. Other section groups are always included in the link | |
623 | // just like ordinary sections. | |
f6ce93d6 | 624 | elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword); |
a2fb1b05 ILT |
625 | |
626 | // Look up the group signature, which is the name of a symbol. This | |
627 | // is a lot of effort to go to to read a string. Why didn't they | |
6a74a719 ILT |
628 | // just have the group signature point into the string table, rather |
629 | // than indirect through a symbol? | |
a2fb1b05 ILT |
630 | |
631 | // Get the appropriate symbol table header (this will normally be | |
632 | // the single SHT_SYMTAB section, but in principle it need not be). | |
d491d34e | 633 | const unsigned int link = this->adjust_shndx(shdr.get_sh_link()); |
645f8123 | 634 | typename This::Shdr symshdr(this, this->elf_file_.section_header(link)); |
a2fb1b05 ILT |
635 | |
636 | // Read the symbol table entry. | |
d491d34e ILT |
637 | unsigned int symndx = shdr.get_sh_info(); |
638 | if (symndx >= symshdr.get_sh_size() / This::sym_size) | |
a2fb1b05 | 639 | { |
75f2446e | 640 | this->error(_("section group %u info %u out of range"), |
d491d34e | 641 | index, symndx); |
75f2446e | 642 | return false; |
a2fb1b05 | 643 | } |
d491d34e | 644 | off_t symoff = symshdr.get_sh_offset() + symndx * This::sym_size; |
39d0cb0e ILT |
645 | const unsigned char* psym = this->get_view(symoff, This::sym_size, true, |
646 | false); | |
a2fb1b05 ILT |
647 | elfcpp::Sym<size, big_endian> sym(psym); |
648 | ||
a2fb1b05 | 649 | // Read the symbol table names. |
8383303e | 650 | section_size_type symnamelen; |
645f8123 | 651 | const unsigned char* psymnamesu; |
d491d34e ILT |
652 | psymnamesu = this->section_contents(this->adjust_shndx(symshdr.get_sh_link()), |
653 | &symnamelen, true); | |
a2fb1b05 ILT |
654 | const char* psymnames = reinterpret_cast<const char*>(psymnamesu); |
655 | ||
656 | // Get the section group signature. | |
645f8123 | 657 | if (sym.get_st_name() >= symnamelen) |
a2fb1b05 | 658 | { |
75f2446e | 659 | this->error(_("symbol %u name offset %u out of range"), |
d491d34e | 660 | symndx, sym.get_st_name()); |
75f2446e | 661 | return false; |
a2fb1b05 ILT |
662 | } |
663 | ||
e94cf127 | 664 | std::string signature(psymnames + sym.get_st_name()); |
a2fb1b05 | 665 | |
ead1e424 ILT |
666 | // It seems that some versions of gas will create a section group |
667 | // associated with a section symbol, and then fail to give a name to | |
668 | // the section symbol. In such a case, use the name of the section. | |
645f8123 | 669 | if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION) |
ead1e424 | 670 | { |
d491d34e ILT |
671 | bool is_ordinary; |
672 | unsigned int sym_shndx = this->adjust_sym_shndx(symndx, | |
673 | sym.get_st_shndx(), | |
674 | &is_ordinary); | |
675 | if (!is_ordinary || sym_shndx >= this->shnum()) | |
676 | { | |
677 | this->error(_("symbol %u invalid section index %u"), | |
678 | symndx, sym_shndx); | |
679 | return false; | |
680 | } | |
e94cf127 CC |
681 | typename This::Shdr member_shdr(shdrs + sym_shndx * This::shdr_size); |
682 | if (member_shdr.get_sh_name() < section_names_size) | |
683 | signature = section_names + member_shdr.get_sh_name(); | |
ead1e424 ILT |
684 | } |
685 | ||
e94cf127 CC |
686 | // Record this section group in the layout, and see whether we've already |
687 | // seen one with the same signature. | |
8a4c0b0d | 688 | bool include_group; |
ef9beddf | 689 | Sized_relobj<size, big_endian>* kept_object = NULL; |
8a4c0b0d | 690 | Kept_section::Comdat_group* kept_group = NULL; |
6a74a719 | 691 | |
8a4c0b0d ILT |
692 | if ((flags & elfcpp::GRP_COMDAT) == 0) |
693 | include_group = true; | |
694 | else | |
e94cf127 | 695 | { |
8a4c0b0d ILT |
696 | Kept_section this_group(this, index, true); |
697 | Kept_section *kept_section_group; | |
698 | include_group = layout->find_or_add_kept_section(signature, | |
699 | &this_group, | |
700 | &kept_section_group); | |
701 | if (include_group) | |
702 | kept_section_group->group_sections = new Kept_section::Comdat_group; | |
703 | ||
704 | kept_group = kept_section_group->group_sections; | |
705 | kept_object = (static_cast<Sized_relobj<size, big_endian>*> | |
706 | (kept_section_group->object)); | |
6a74a719 | 707 | } |
a2fb1b05 | 708 | |
a2fb1b05 | 709 | size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word); |
8825ac63 ILT |
710 | |
711 | std::vector<unsigned int> shndxes; | |
712 | bool relocate_group = include_group && parameters->options().relocatable(); | |
713 | if (relocate_group) | |
714 | shndxes.reserve(count - 1); | |
715 | ||
a2fb1b05 ILT |
716 | for (size_t i = 1; i < count; ++i) |
717 | { | |
f6ce93d6 | 718 | elfcpp::Elf_Word secnum = |
8825ac63 ILT |
719 | this->adjust_shndx(elfcpp::Swap<32, big_endian>::readval(pword + i)); |
720 | ||
721 | if (relocate_group) | |
722 | shndxes.push_back(secnum); | |
723 | ||
a2fb1b05 ILT |
724 | if (secnum >= this->shnum()) |
725 | { | |
75f2446e ILT |
726 | this->error(_("section %u in section group %u out of range"), |
727 | secnum, index); | |
728 | continue; | |
a2fb1b05 | 729 | } |
55438702 ILT |
730 | |
731 | // Check for an earlier section number, since we're going to get | |
732 | // it wrong--we may have already decided to include the section. | |
733 | if (secnum < index) | |
734 | this->error(_("invalid section group %u refers to earlier section %u"), | |
735 | index, secnum); | |
736 | ||
e94cf127 CC |
737 | // Get the name of the member section. |
738 | typename This::Shdr member_shdr(shdrs + secnum * This::shdr_size); | |
739 | if (member_shdr.get_sh_name() >= section_names_size) | |
740 | { | |
741 | // This is an error, but it will be diagnosed eventually | |
742 | // in do_layout, so we don't need to do anything here but | |
743 | // ignore it. | |
744 | continue; | |
745 | } | |
746 | std::string mname(section_names + member_shdr.get_sh_name()); | |
747 | ||
748 | if (!include_group) | |
749 | { | |
750 | (*omit)[secnum] = true; | |
751 | if (kept_group != NULL) | |
752 | { | |
753 | // Find the corresponding kept section, and store that info | |
754 | // in the discarded section table. | |
8a4c0b0d ILT |
755 | Kept_section::Comdat_group::const_iterator p = |
756 | kept_group->find(mname); | |
e94cf127 CC |
757 | if (p != kept_group->end()) |
758 | { | |
759 | Kept_comdat_section* kept = | |
760 | new Kept_comdat_section(kept_object, p->second); | |
761 | this->set_kept_comdat_section(secnum, kept); | |
762 | } | |
763 | } | |
764 | } | |
765 | else if (flags & elfcpp::GRP_COMDAT) | |
766 | { | |
767 | // Add the section to the kept group table. | |
768 | gold_assert(kept_group != NULL); | |
769 | kept_group->insert(std::make_pair(mname, secnum)); | |
770 | } | |
a2fb1b05 ILT |
771 | } |
772 | ||
8825ac63 ILT |
773 | if (relocate_group) |
774 | layout->layout_group(symtab, this, index, name, signature.c_str(), | |
775 | shdr, flags, &shndxes); | |
776 | ||
e94cf127 | 777 | return include_group; |
a2fb1b05 ILT |
778 | } |
779 | ||
780 | // Whether to include a linkonce section in the link. NAME is the | |
781 | // name of the section and SHDR is the section header. | |
782 | ||
783 | // Linkonce sections are a GNU extension implemented in the original | |
784 | // GNU linker before section groups were defined. The semantics are | |
785 | // that we only include one linkonce section with a given name. The | |
786 | // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME, | |
787 | // where T is the type of section and SYMNAME is the name of a symbol. | |
788 | // In an attempt to make linkonce sections interact well with section | |
789 | // groups, we try to identify SYMNAME and use it like a section group | |
790 | // signature. We want to block section groups with that signature, | |
791 | // but not other linkonce sections with that signature. We also use | |
792 | // the full name of the linkonce section as a normal section group | |
793 | // signature. | |
794 | ||
795 | template<int size, bool big_endian> | |
796 | bool | |
f6ce93d6 | 797 | Sized_relobj<size, big_endian>::include_linkonce_section( |
a2fb1b05 | 798 | Layout* layout, |
e94cf127 | 799 | unsigned int index, |
a2fb1b05 ILT |
800 | const char* name, |
801 | const elfcpp::Shdr<size, big_endian>&) | |
802 | { | |
ad435a24 ILT |
803 | // In general the symbol name we want will be the string following |
804 | // the last '.'. However, we have to handle the case of | |
805 | // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by | |
806 | // some versions of gcc. So we use a heuristic: if the name starts | |
807 | // with ".gnu.linkonce.t.", we use everything after that. Otherwise | |
808 | // we look for the last '.'. We can't always simply skip | |
809 | // ".gnu.linkonce.X", because we have to deal with cases like | |
810 | // ".gnu.linkonce.d.rel.ro.local". | |
811 | const char* const linkonce_t = ".gnu.linkonce.t."; | |
812 | const char* symname; | |
813 | if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0) | |
814 | symname = name + strlen(linkonce_t); | |
815 | else | |
816 | symname = strrchr(name, '.') + 1; | |
e94cf127 CC |
817 | std::string sig1(symname); |
818 | std::string sig2(name); | |
8a4c0b0d ILT |
819 | Kept_section candidate1(this, index, false); |
820 | Kept_section candidate2(this, index, true); | |
821 | Kept_section* kept1; | |
822 | Kept_section* kept2; | |
823 | bool include1 = layout->find_or_add_kept_section(sig1, &candidate1, &kept1); | |
824 | bool include2 = layout->find_or_add_kept_section(sig2, &candidate2, &kept2); | |
e94cf127 CC |
825 | |
826 | if (!include2) | |
827 | { | |
828 | // The section is being discarded on the basis of its section | |
829 | // name (i.e., the kept section was also a linkonce section). | |
830 | // In this case, the section index stored with the layout object | |
831 | // is the linkonce section that was kept. | |
8a4c0b0d ILT |
832 | unsigned int kept_group_index = kept2->shndx; |
833 | Relobj* kept_relobj = kept2->object; | |
ef9beddf | 834 | if (kept_relobj != NULL) |
e94cf127 | 835 | { |
8a4c0b0d ILT |
836 | Sized_relobj<size, big_endian>* kept_object = |
837 | static_cast<Sized_relobj<size, big_endian>*>(kept_relobj); | |
e94cf127 CC |
838 | Kept_comdat_section* kept = |
839 | new Kept_comdat_section(kept_object, kept_group_index); | |
840 | this->set_kept_comdat_section(index, kept); | |
841 | } | |
842 | } | |
843 | else if (!include1) | |
844 | { | |
845 | // The section is being discarded on the basis of its symbol | |
846 | // name. This means that the corresponding kept section was | |
847 | // part of a comdat group, and it will be difficult to identify | |
848 | // the specific section within that group that corresponds to | |
849 | // this linkonce section. We'll handle the simple case where | |
850 | // the group has only one member section. Otherwise, it's not | |
851 | // worth the effort. | |
8a4c0b0d | 852 | Relobj* kept_relobj = kept1->object; |
ef9beddf | 853 | if (kept_relobj != NULL) |
e94cf127 | 854 | { |
ef9beddf | 855 | Sized_relobj<size, big_endian>* kept_object = |
8a4c0b0d ILT |
856 | static_cast<Sized_relobj<size, big_endian>*>(kept_relobj); |
857 | Kept_section::Comdat_group* kept_group = kept1->group_sections; | |
e94cf127 CC |
858 | if (kept_group != NULL && kept_group->size() == 1) |
859 | { | |
8a4c0b0d ILT |
860 | Kept_section::Comdat_group::const_iterator p = |
861 | kept_group->begin(); | |
e94cf127 CC |
862 | gold_assert(p != kept_group->end()); |
863 | Kept_comdat_section* kept = | |
864 | new Kept_comdat_section(kept_object, p->second); | |
865 | this->set_kept_comdat_section(index, kept); | |
866 | } | |
867 | } | |
868 | } | |
869 | ||
a783673b | 870 | return include1 && include2; |
a2fb1b05 ILT |
871 | } |
872 | ||
5995b570 CC |
873 | // Layout an input section. |
874 | ||
875 | template<int size, bool big_endian> | |
876 | inline void | |
877 | Sized_relobj<size, big_endian>::layout_section(Layout* layout, | |
878 | unsigned int shndx, | |
879 | const char* name, | |
880 | typename This::Shdr& shdr, | |
881 | unsigned int reloc_shndx, | |
882 | unsigned int reloc_type) | |
883 | { | |
884 | off_t offset; | |
885 | Output_section* os = layout->layout(this, shndx, name, shdr, | |
886 | reloc_shndx, reloc_type, &offset); | |
887 | ||
888 | this->output_sections()[shndx] = os; | |
889 | if (offset == -1) | |
890 | this->section_offsets_[shndx] = invalid_address; | |
891 | else | |
892 | this->section_offsets_[shndx] = convert_types<Address, off_t>(offset); | |
893 | ||
894 | // If this section requires special handling, and if there are | |
895 | // relocs that apply to it, then we must do the special handling | |
896 | // before we apply the relocs. | |
897 | if (offset == -1 && reloc_shndx != 0) | |
898 | this->set_relocs_must_follow_section_writes(); | |
899 | } | |
900 | ||
a2fb1b05 ILT |
901 | // Lay out the input sections. We walk through the sections and check |
902 | // whether they should be included in the link. If they should, we | |
903 | // pass them to the Layout object, which will return an output section | |
6d03d481 ST |
904 | // and an offset. |
905 | // During garbage collection (gc-sections), this function is called | |
906 | // twice. When it is called the first time, it is for setting up some | |
907 | // sections as roots to a work-list and to do comdat processing. Actual | |
908 | // layout happens the second time around after all the relevant sections | |
909 | // have been determined. The first time, is_worklist_ready is false. | |
910 | // It is then set to true after the worklist is processed and the relevant | |
911 | // sections are determined. Then, this function is called again to | |
912 | // layout the sections. | |
a2fb1b05 ILT |
913 | |
914 | template<int size, bool big_endian> | |
915 | void | |
7e1edb90 | 916 | Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab, |
f6ce93d6 | 917 | Layout* layout, |
12e14209 | 918 | Read_symbols_data* sd) |
a2fb1b05 | 919 | { |
dbe717ef | 920 | const unsigned int shnum = this->shnum(); |
6d03d481 ST |
921 | bool is_gc_pass_one = (parameters->options().gc_sections() |
922 | && !symtab->gc()->is_worklist_ready()); | |
923 | bool is_gc_pass_two = (parameters->options().gc_sections() | |
924 | && symtab->gc()->is_worklist_ready()); | |
12e14209 ILT |
925 | if (shnum == 0) |
926 | return; | |
e0ebcf42 | 927 | Symbols_data* gc_sd = NULL; |
6d03d481 ST |
928 | if (is_gc_pass_one) |
929 | { | |
930 | // During garbage collection save the symbols data to use it when | |
931 | // re-entering this function. | |
932 | gc_sd = new Symbols_data; | |
933 | this->copy_symbols_data(gc_sd, sd, This::shdr_size * shnum); | |
934 | this->set_symbols_data(gc_sd); | |
935 | } | |
936 | else if (is_gc_pass_two) | |
937 | { | |
938 | gc_sd = this->get_symbols_data(); | |
939 | } | |
940 | ||
941 | const unsigned char* section_headers_data = NULL; | |
942 | section_size_type section_names_size; | |
943 | const unsigned char* symbols_data = NULL; | |
944 | section_size_type symbols_size; | |
945 | section_offset_type external_symbols_offset; | |
946 | const unsigned char* symbol_names_data = NULL; | |
947 | section_size_type symbol_names_size; | |
948 | ||
949 | if (parameters->options().gc_sections()) | |
950 | { | |
951 | section_headers_data = gc_sd->section_headers_data; | |
952 | section_names_size = gc_sd->section_names_size; | |
953 | symbols_data = gc_sd->symbols_data; | |
954 | symbols_size = gc_sd->symbols_size; | |
955 | external_symbols_offset = gc_sd->external_symbols_offset; | |
956 | symbol_names_data = gc_sd->symbol_names_data; | |
957 | symbol_names_size = gc_sd->symbol_names_size; | |
958 | } | |
959 | else | |
960 | { | |
961 | section_headers_data = sd->section_headers->data(); | |
962 | section_names_size = sd->section_names_size; | |
963 | if (sd->symbols != NULL) | |
964 | symbols_data = sd->symbols->data(); | |
965 | symbols_size = sd->symbols_size; | |
966 | external_symbols_offset = sd->external_symbols_offset; | |
967 | if (sd->symbol_names != NULL) | |
968 | symbol_names_data = sd->symbol_names->data(); | |
969 | symbol_names_size = sd->symbol_names_size; | |
970 | } | |
a2fb1b05 ILT |
971 | |
972 | // Get the section headers. | |
6d03d481 | 973 | const unsigned char* shdrs = section_headers_data; |
e94cf127 | 974 | const unsigned char* pshdrs; |
a2fb1b05 ILT |
975 | |
976 | // Get the section names. | |
6d03d481 ST |
977 | const unsigned char* pnamesu = parameters->options().gc_sections() ? |
978 | gc_sd->section_names_data : | |
979 | sd->section_names->data(); | |
a2fb1b05 ILT |
980 | const char* pnames = reinterpret_cast<const char*>(pnamesu); |
981 | ||
5995b570 CC |
982 | // If any input files have been claimed by plugins, we need to defer |
983 | // actual layout until the replacement files have arrived. | |
984 | const bool should_defer_layout = | |
985 | (parameters->options().has_plugins() | |
986 | && parameters->options().plugins()->should_defer_layout()); | |
987 | unsigned int num_sections_to_defer = 0; | |
988 | ||
730cdc88 ILT |
989 | // For each section, record the index of the reloc section if any. |
990 | // Use 0 to mean that there is no reloc section, -1U to mean that | |
991 | // there is more than one. | |
992 | std::vector<unsigned int> reloc_shndx(shnum, 0); | |
993 | std::vector<unsigned int> reloc_type(shnum, elfcpp::SHT_NULL); | |
994 | // Skip the first, dummy, section. | |
e94cf127 | 995 | pshdrs = shdrs + This::shdr_size; |
730cdc88 ILT |
996 | for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size) |
997 | { | |
998 | typename This::Shdr shdr(pshdrs); | |
999 | ||
5995b570 CC |
1000 | // Count the number of sections whose layout will be deferred. |
1001 | if (should_defer_layout && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC)) | |
1002 | ++num_sections_to_defer; | |
1003 | ||
730cdc88 ILT |
1004 | unsigned int sh_type = shdr.get_sh_type(); |
1005 | if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA) | |
1006 | { | |
d491d34e | 1007 | unsigned int target_shndx = this->adjust_shndx(shdr.get_sh_info()); |
730cdc88 ILT |
1008 | if (target_shndx == 0 || target_shndx >= shnum) |
1009 | { | |
1010 | this->error(_("relocation section %u has bad info %u"), | |
1011 | i, target_shndx); | |
1012 | continue; | |
1013 | } | |
1014 | ||
1015 | if (reloc_shndx[target_shndx] != 0) | |
1016 | reloc_shndx[target_shndx] = -1U; | |
1017 | else | |
1018 | { | |
1019 | reloc_shndx[target_shndx] = i; | |
1020 | reloc_type[target_shndx] = sh_type; | |
1021 | } | |
1022 | } | |
1023 | } | |
1024 | ||
ef9beddf ILT |
1025 | Output_sections& out_sections(this->output_sections()); |
1026 | std::vector<Address>& out_section_offsets(this->section_offsets_); | |
1027 | ||
6d03d481 ST |
1028 | if (!is_gc_pass_two) |
1029 | { | |
1030 | out_sections.resize(shnum); | |
1031 | out_section_offsets.resize(shnum); | |
1032 | } | |
a2fb1b05 | 1033 | |
88dd47ac ILT |
1034 | // If we are only linking for symbols, then there is nothing else to |
1035 | // do here. | |
1036 | if (this->input_file()->just_symbols()) | |
1037 | { | |
6d03d481 ST |
1038 | if (!is_gc_pass_two) |
1039 | { | |
1040 | delete sd->section_headers; | |
1041 | sd->section_headers = NULL; | |
1042 | delete sd->section_names; | |
1043 | sd->section_names = NULL; | |
1044 | } | |
88dd47ac ILT |
1045 | return; |
1046 | } | |
1047 | ||
5995b570 CC |
1048 | if (num_sections_to_defer > 0) |
1049 | { | |
1050 | parameters->options().plugins()->add_deferred_layout_object(this); | |
1051 | this->deferred_layout_.reserve(num_sections_to_defer); | |
1052 | } | |
1053 | ||
35cdfc9a ILT |
1054 | // Whether we've seen a .note.GNU-stack section. |
1055 | bool seen_gnu_stack = false; | |
1056 | // The flags of a .note.GNU-stack section. | |
1057 | uint64_t gnu_stack_flags = 0; | |
1058 | ||
a2fb1b05 ILT |
1059 | // Keep track of which sections to omit. |
1060 | std::vector<bool> omit(shnum, false); | |
1061 | ||
7019cd25 | 1062 | // Keep track of reloc sections when emitting relocations. |
8851ecca ILT |
1063 | const bool relocatable = parameters->options().relocatable(); |
1064 | const bool emit_relocs = (relocatable | |
1065 | || parameters->options().emit_relocs()); | |
6a74a719 ILT |
1066 | std::vector<unsigned int> reloc_sections; |
1067 | ||
730cdc88 ILT |
1068 | // Keep track of .eh_frame sections. |
1069 | std::vector<unsigned int> eh_frame_sections; | |
1070 | ||
f6ce93d6 | 1071 | // Skip the first, dummy, section. |
e94cf127 | 1072 | pshdrs = shdrs + This::shdr_size; |
f6ce93d6 | 1073 | for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size) |
a2fb1b05 | 1074 | { |
75f65a3e | 1075 | typename This::Shdr shdr(pshdrs); |
a2fb1b05 | 1076 | |
6d03d481 | 1077 | if (shdr.get_sh_name() >= section_names_size) |
a2fb1b05 | 1078 | { |
75f2446e ILT |
1079 | this->error(_("bad section name offset for section %u: %lu"), |
1080 | i, static_cast<unsigned long>(shdr.get_sh_name())); | |
1081 | return; | |
a2fb1b05 ILT |
1082 | } |
1083 | ||
1084 | const char* name = pnames + shdr.get_sh_name(); | |
1085 | ||
6d03d481 ST |
1086 | if (!is_gc_pass_two) |
1087 | { | |
1088 | if (this->handle_gnu_warning_section(name, i, symtab)) | |
1089 | { | |
1090 | if (!relocatable) | |
1091 | omit[i] = true; | |
1092 | } | |
f6ce93d6 | 1093 | |
6d03d481 ST |
1094 | // The .note.GNU-stack section is special. It gives the |
1095 | // protection flags that this object file requires for the stack | |
1096 | // in memory. | |
1097 | if (strcmp(name, ".note.GNU-stack") == 0) | |
1098 | { | |
1099 | seen_gnu_stack = true; | |
1100 | gnu_stack_flags |= shdr.get_sh_flags(); | |
1101 | omit[i] = true; | |
1102 | } | |
35cdfc9a | 1103 | |
6d03d481 ST |
1104 | bool discard = omit[i]; |
1105 | if (!discard) | |
1106 | { | |
1107 | if (shdr.get_sh_type() == elfcpp::SHT_GROUP) | |
1108 | { | |
1109 | if (!this->include_section_group(symtab, layout, i, name, | |
1110 | shdrs, pnames, | |
1111 | section_names_size, | |
1112 | &omit)) | |
1113 | discard = true; | |
1114 | } | |
1115 | else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0 | |
1116 | && Layout::is_linkonce(name)) | |
1117 | { | |
1118 | if (!this->include_linkonce_section(layout, i, name, shdr)) | |
1119 | discard = true; | |
1120 | } | |
a2fb1b05 | 1121 | } |
a2fb1b05 | 1122 | |
6d03d481 ST |
1123 | if (discard) |
1124 | { | |
1125 | // Do not include this section in the link. | |
1126 | out_sections[i] = NULL; | |
1127 | out_section_offsets[i] = invalid_address; | |
1128 | continue; | |
1129 | } | |
1130 | } | |
1131 | ||
1132 | if (is_gc_pass_one) | |
1133 | { | |
1134 | if (is_section_name_included(name) | |
1135 | || shdr.get_sh_type() == elfcpp::SHT_INIT_ARRAY | |
1136 | || shdr.get_sh_type() == elfcpp::SHT_FINI_ARRAY) | |
1137 | { | |
1138 | symtab->gc()->worklist().push(Section_id(this, i)); | |
1139 | } | |
1140 | } | |
a2fb1b05 | 1141 | |
6a74a719 ILT |
1142 | // When doing a relocatable link we are going to copy input |
1143 | // reloc sections into the output. We only want to copy the | |
1144 | // ones associated with sections which are not being discarded. | |
1145 | // However, we don't know that yet for all sections. So save | |
6d03d481 ST |
1146 | // reloc sections and process them later. Garbage collection is |
1147 | // not triggered when relocatable code is desired. | |
7019cd25 | 1148 | if (emit_relocs |
6a74a719 ILT |
1149 | && (shdr.get_sh_type() == elfcpp::SHT_REL |
1150 | || shdr.get_sh_type() == elfcpp::SHT_RELA)) | |
1151 | { | |
1152 | reloc_sections.push_back(i); | |
1153 | continue; | |
1154 | } | |
1155 | ||
8851ecca | 1156 | if (relocatable && shdr.get_sh_type() == elfcpp::SHT_GROUP) |
6a74a719 ILT |
1157 | continue; |
1158 | ||
730cdc88 ILT |
1159 | // The .eh_frame section is special. It holds exception frame |
1160 | // information that we need to read in order to generate the | |
1161 | // exception frame header. We process these after all the other | |
1162 | // sections so that the exception frame reader can reliably | |
1163 | // determine which sections are being discarded, and discard the | |
1164 | // corresponding information. | |
8851ecca | 1165 | if (!relocatable |
6d03d481 ST |
1166 | && strcmp(name, ".eh_frame") == 0 |
1167 | && this->check_eh_frame_flags(&shdr)) | |
1168 | { | |
1169 | if (is_gc_pass_one) | |
1170 | { | |
1171 | out_sections[i] = reinterpret_cast<Output_section*>(1); | |
1172 | out_section_offsets[i] = invalid_address; | |
1173 | } | |
1174 | else | |
1175 | eh_frame_sections.push_back(i); | |
1176 | continue; | |
1177 | } | |
730cdc88 | 1178 | |
6d03d481 ST |
1179 | if (is_gc_pass_two) |
1180 | { | |
1181 | // This is executed during the second pass of garbage | |
1182 | // collection. do_layout has been called before and some | |
1183 | // sections have been already discarded. Simply ignore | |
1184 | // such sections this time around. | |
1185 | if (out_sections[i] == NULL) | |
1186 | { | |
1187 | gold_assert(out_section_offsets[i] == invalid_address); | |
1188 | continue; | |
1189 | } | |
1190 | if ((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0) | |
1191 | if (symtab->gc()->referenced_list().find(Section_id(this,i)) | |
1192 | == symtab->gc()->referenced_list().end()) | |
1193 | { | |
1194 | if (parameters->options().print_gc_sections()) | |
89dd1680 | 1195 | gold_info(_("%s: removing unused section from '%s'" |
6d03d481 ST |
1196 | " in file '%s"), |
1197 | program_name, this->section_name(i).c_str(), | |
1198 | this->name().c_str()); | |
1199 | out_sections[i] = NULL; | |
1200 | out_section_offsets[i] = invalid_address; | |
1201 | continue; | |
1202 | } | |
1203 | } | |
1204 | // Defer layout here if input files are claimed by plugins. When gc | |
1205 | // is turned on this function is called twice. For the second call | |
1206 | // should_defer_layout should be false. | |
5995b570 CC |
1207 | if (should_defer_layout && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC)) |
1208 | { | |
6d03d481 ST |
1209 | gold_assert(!is_gc_pass_two); |
1210 | this->deferred_layout_.push_back(Deferred_layout(i, name, | |
1211 | pshdrs, | |
5995b570 CC |
1212 | reloc_shndx[i], |
1213 | reloc_type[i])); | |
5995b570 CC |
1214 | // Put dummy values here; real values will be supplied by |
1215 | // do_layout_deferred_sections. | |
6d03d481 ST |
1216 | out_sections[i] = reinterpret_cast<Output_section*>(2); |
1217 | out_section_offsets[i] = invalid_address; | |
1218 | continue; | |
1219 | } | |
1220 | // During gc_pass_two if a section that was previously deferred is | |
1221 | // found, do not layout the section as layout_deferred_sections will | |
1222 | // do it later from gold.cc. | |
1223 | if (is_gc_pass_two | |
1224 | && (out_sections[i] == reinterpret_cast<Output_section*>(2))) | |
1225 | continue; | |
1226 | ||
1227 | if (is_gc_pass_one) | |
1228 | { | |
1229 | // This is during garbage collection. The out_sections are | |
1230 | // assigned in the second call to this function. | |
5995b570 CC |
1231 | out_sections[i] = reinterpret_cast<Output_section*>(1); |
1232 | out_section_offsets[i] = invalid_address; | |
1233 | } | |
ef9beddf | 1234 | else |
5995b570 | 1235 | { |
6d03d481 ST |
1236 | // When garbage collection is switched on the actual layout |
1237 | // only happens in the second call. | |
5995b570 CC |
1238 | this->layout_section(layout, i, name, shdr, reloc_shndx[i], |
1239 | reloc_type[i]); | |
1240 | } | |
12e14209 ILT |
1241 | } |
1242 | ||
6d03d481 ST |
1243 | if (!is_gc_pass_one) |
1244 | layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags); | |
35cdfc9a | 1245 | |
6a74a719 | 1246 | // When doing a relocatable link handle the reloc sections at the |
6d03d481 | 1247 | // end. Garbage collection is not turned on for relocatable code. |
7019cd25 | 1248 | if (emit_relocs) |
6a74a719 | 1249 | this->size_relocatable_relocs(); |
6d03d481 | 1250 | gold_assert(!parameters->options().gc_sections() || reloc_sections.empty()); |
6a74a719 ILT |
1251 | for (std::vector<unsigned int>::const_iterator p = reloc_sections.begin(); |
1252 | p != reloc_sections.end(); | |
1253 | ++p) | |
1254 | { | |
1255 | unsigned int i = *p; | |
1256 | const unsigned char* pshdr; | |
6d03d481 | 1257 | pshdr = section_headers_data + i * This::shdr_size; |
6a74a719 ILT |
1258 | typename This::Shdr shdr(pshdr); |
1259 | ||
d491d34e | 1260 | unsigned int data_shndx = this->adjust_shndx(shdr.get_sh_info()); |
6a74a719 ILT |
1261 | if (data_shndx >= shnum) |
1262 | { | |
1263 | // We already warned about this above. | |
1264 | continue; | |
1265 | } | |
1266 | ||
ef9beddf | 1267 | Output_section* data_section = out_sections[data_shndx]; |
6a74a719 ILT |
1268 | if (data_section == NULL) |
1269 | { | |
ef9beddf | 1270 | out_sections[i] = NULL; |
eff45813 | 1271 | out_section_offsets[i] = invalid_address; |
6a74a719 ILT |
1272 | continue; |
1273 | } | |
1274 | ||
1275 | Relocatable_relocs* rr = new Relocatable_relocs(); | |
1276 | this->set_relocatable_relocs(i, rr); | |
1277 | ||
1278 | Output_section* os = layout->layout_reloc(this, i, shdr, data_section, | |
1279 | rr); | |
ef9beddf | 1280 | out_sections[i] = os; |
eff45813 | 1281 | out_section_offsets[i] = invalid_address; |
6a74a719 ILT |
1282 | } |
1283 | ||
730cdc88 | 1284 | // Handle the .eh_frame sections at the end. |
6d03d481 | 1285 | gold_assert(!is_gc_pass_one || eh_frame_sections.empty()); |
730cdc88 ILT |
1286 | for (std::vector<unsigned int>::const_iterator p = eh_frame_sections.begin(); |
1287 | p != eh_frame_sections.end(); | |
1288 | ++p) | |
1289 | { | |
1290 | gold_assert(this->has_eh_frame_); | |
6d03d481 | 1291 | gold_assert(external_symbols_offset != 0); |
730cdc88 ILT |
1292 | |
1293 | unsigned int i = *p; | |
1294 | const unsigned char *pshdr; | |
6d03d481 | 1295 | pshdr = section_headers_data + i * This::shdr_size; |
730cdc88 ILT |
1296 | typename This::Shdr shdr(pshdr); |
1297 | ||
1298 | off_t offset; | |
1299 | Output_section* os = layout->layout_eh_frame(this, | |
6d03d481 ST |
1300 | symbols_data, |
1301 | symbols_size, | |
1302 | symbol_names_data, | |
1303 | symbol_names_size, | |
730cdc88 ILT |
1304 | i, shdr, |
1305 | reloc_shndx[i], | |
1306 | reloc_type[i], | |
1307 | &offset); | |
ef9beddf ILT |
1308 | out_sections[i] = os; |
1309 | if (offset == -1) | |
805bb01c DK |
1310 | { |
1311 | // An object can contain at most one section holding exception | |
1312 | // frame information. | |
1313 | gold_assert(this->discarded_eh_frame_shndx_ == -1U); | |
1314 | this->discarded_eh_frame_shndx_ = i; | |
1315 | out_section_offsets[i] = invalid_address; | |
1316 | } | |
ef9beddf ILT |
1317 | else |
1318 | out_section_offsets[i] = convert_types<Address, off_t>(offset); | |
730cdc88 ILT |
1319 | |
1320 | // If this section requires special handling, and if there are | |
1321 | // relocs that apply to it, then we must do the special handling | |
1322 | // before we apply the relocs. | |
1323 | if (offset == -1 && reloc_shndx[i] != 0) | |
1324 | this->set_relocs_must_follow_section_writes(); | |
1325 | } | |
1326 | ||
6d03d481 ST |
1327 | if (is_gc_pass_two) |
1328 | { | |
1329 | delete[] gc_sd->section_headers_data; | |
1330 | delete[] gc_sd->section_names_data; | |
1331 | delete[] gc_sd->symbols_data; | |
1332 | delete[] gc_sd->symbol_names_data; | |
1333 | } | |
1334 | else | |
1335 | { | |
1336 | delete sd->section_headers; | |
1337 | sd->section_headers = NULL; | |
1338 | delete sd->section_names; | |
1339 | sd->section_names = NULL; | |
1340 | } | |
12e14209 ILT |
1341 | } |
1342 | ||
5995b570 CC |
1343 | // Layout sections whose layout was deferred while waiting for |
1344 | // input files from a plugin. | |
1345 | ||
1346 | template<int size, bool big_endian> | |
1347 | void | |
1348 | Sized_relobj<size, big_endian>::do_layout_deferred_sections(Layout* layout) | |
1349 | { | |
1350 | typename std::vector<Deferred_layout>::iterator deferred; | |
1351 | ||
1352 | for (deferred = this->deferred_layout_.begin(); | |
1353 | deferred != this->deferred_layout_.end(); | |
1354 | ++deferred) | |
1355 | { | |
1356 | typename This::Shdr shdr(deferred->shdr_data_); | |
1357 | this->layout_section(layout, deferred->shndx_, deferred->name_.c_str(), | |
1358 | shdr, deferred->reloc_shndx_, deferred->reloc_type_); | |
1359 | } | |
1360 | ||
1361 | this->deferred_layout_.clear(); | |
1362 | } | |
1363 | ||
12e14209 ILT |
1364 | // Add the symbols to the symbol table. |
1365 | ||
1366 | template<int size, bool big_endian> | |
1367 | void | |
f6ce93d6 | 1368 | Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab, |
f488e4b0 CC |
1369 | Read_symbols_data* sd, |
1370 | Layout*) | |
12e14209 ILT |
1371 | { |
1372 | if (sd->symbols == NULL) | |
1373 | { | |
a3ad94ed | 1374 | gold_assert(sd->symbol_names == NULL); |
12e14209 ILT |
1375 | return; |
1376 | } | |
a2fb1b05 | 1377 | |
12e14209 | 1378 | const int sym_size = This::sym_size; |
730cdc88 ILT |
1379 | size_t symcount = ((sd->symbols_size - sd->external_symbols_offset) |
1380 | / sym_size); | |
8383303e | 1381 | if (symcount * sym_size != sd->symbols_size - sd->external_symbols_offset) |
12e14209 | 1382 | { |
75f2446e ILT |
1383 | this->error(_("size of symbols is not multiple of symbol size")); |
1384 | return; | |
a2fb1b05 | 1385 | } |
12e14209 | 1386 | |
730cdc88 | 1387 | this->symbols_.resize(symcount); |
12e14209 | 1388 | |
12e14209 ILT |
1389 | const char* sym_names = |
1390 | reinterpret_cast<const char*>(sd->symbol_names->data()); | |
730cdc88 ILT |
1391 | symtab->add_from_relobj(this, |
1392 | sd->symbols->data() + sd->external_symbols_offset, | |
7fcd3aa9 | 1393 | symcount, this->local_symbol_count_, |
d491d34e | 1394 | sym_names, sd->symbol_names_size, |
92de84a6 ILT |
1395 | &this->symbols_, |
1396 | &this->defined_count_); | |
12e14209 ILT |
1397 | |
1398 | delete sd->symbols; | |
1399 | sd->symbols = NULL; | |
1400 | delete sd->symbol_names; | |
1401 | sd->symbol_names = NULL; | |
bae7f79e ILT |
1402 | } |
1403 | ||
cb295612 ILT |
1404 | // First pass over the local symbols. Here we add their names to |
1405 | // *POOL and *DYNPOOL, and we store the symbol value in | |
1406 | // THIS->LOCAL_VALUES_. This function is always called from a | |
1407 | // singleton thread. This is followed by a call to | |
1408 | // finalize_local_symbols. | |
75f65a3e ILT |
1409 | |
1410 | template<int size, bool big_endian> | |
7bf1f802 ILT |
1411 | void |
1412 | Sized_relobj<size, big_endian>::do_count_local_symbols(Stringpool* pool, | |
1413 | Stringpool* dynpool) | |
75f65a3e | 1414 | { |
a3ad94ed | 1415 | gold_assert(this->symtab_shndx_ != -1U); |
645f8123 | 1416 | if (this->symtab_shndx_ == 0) |
61ba1cf9 ILT |
1417 | { |
1418 | // This object has no symbols. Weird but legal. | |
7bf1f802 | 1419 | return; |
61ba1cf9 ILT |
1420 | } |
1421 | ||
75f65a3e | 1422 | // Read the symbol table section header. |
645f8123 ILT |
1423 | const unsigned int symtab_shndx = this->symtab_shndx_; |
1424 | typename This::Shdr symtabshdr(this, | |
1425 | this->elf_file_.section_header(symtab_shndx)); | |
a3ad94ed | 1426 | gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); |
75f65a3e ILT |
1427 | |
1428 | // Read the local symbols. | |
75f65a3e | 1429 | const int sym_size = This::sym_size; |
92e059d8 | 1430 | const unsigned int loccount = this->local_symbol_count_; |
a3ad94ed | 1431 | gold_assert(loccount == symtabshdr.get_sh_info()); |
75f65a3e ILT |
1432 | off_t locsize = loccount * sym_size; |
1433 | const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(), | |
39d0cb0e | 1434 | locsize, true, true); |
75f65a3e | 1435 | |
75f65a3e | 1436 | // Read the symbol names. |
d491d34e ILT |
1437 | const unsigned int strtab_shndx = |
1438 | this->adjust_shndx(symtabshdr.get_sh_link()); | |
8383303e | 1439 | section_size_type strtab_size; |
645f8123 | 1440 | const unsigned char* pnamesu = this->section_contents(strtab_shndx, |
9eb9fa57 ILT |
1441 | &strtab_size, |
1442 | true); | |
75f65a3e ILT |
1443 | const char* pnames = reinterpret_cast<const char*>(pnamesu); |
1444 | ||
1445 | // Loop over the local symbols. | |
1446 | ||
ef9beddf | 1447 | const Output_sections& out_sections(this->output_sections()); |
75f65a3e | 1448 | unsigned int shnum = this->shnum(); |
61ba1cf9 | 1449 | unsigned int count = 0; |
7bf1f802 | 1450 | unsigned int dyncount = 0; |
75f65a3e ILT |
1451 | // Skip the first, dummy, symbol. |
1452 | psyms += sym_size; | |
bb04269c | 1453 | bool discard_locals = parameters->options().discard_locals(); |
61ba1cf9 | 1454 | for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size) |
75f65a3e ILT |
1455 | { |
1456 | elfcpp::Sym<size, big_endian> sym(psyms); | |
1457 | ||
b8e6aad9 ILT |
1458 | Symbol_value<size>& lv(this->local_values_[i]); |
1459 | ||
d491d34e ILT |
1460 | bool is_ordinary; |
1461 | unsigned int shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(), | |
1462 | &is_ordinary); | |
1463 | lv.set_input_shndx(shndx, is_ordinary); | |
75f65a3e | 1464 | |
063f12a8 ILT |
1465 | if (sym.get_st_type() == elfcpp::STT_SECTION) |
1466 | lv.set_is_section_symbol(); | |
7bf1f802 ILT |
1467 | else if (sym.get_st_type() == elfcpp::STT_TLS) |
1468 | lv.set_is_tls_symbol(); | |
1469 | ||
1470 | // Save the input symbol value for use in do_finalize_local_symbols(). | |
1471 | lv.set_input_value(sym.get_st_value()); | |
1472 | ||
1473 | // Decide whether this symbol should go into the output file. | |
063f12a8 | 1474 | |
805bb01c DK |
1475 | if ((shndx < shnum && out_sections[shndx] == NULL) |
1476 | || (shndx == this->discarded_eh_frame_shndx_)) | |
7bf1f802 ILT |
1477 | { |
1478 | lv.set_no_output_symtab_entry(); | |
dceae3c1 | 1479 | gold_assert(!lv.needs_output_dynsym_entry()); |
7bf1f802 ILT |
1480 | continue; |
1481 | } | |
1482 | ||
1483 | if (sym.get_st_type() == elfcpp::STT_SECTION) | |
1484 | { | |
1485 | lv.set_no_output_symtab_entry(); | |
dceae3c1 | 1486 | gold_assert(!lv.needs_output_dynsym_entry()); |
7bf1f802 ILT |
1487 | continue; |
1488 | } | |
1489 | ||
1490 | if (sym.get_st_name() >= strtab_size) | |
1491 | { | |
1492 | this->error(_("local symbol %u section name out of range: %u >= %u"), | |
1493 | i, sym.get_st_name(), | |
1494 | static_cast<unsigned int>(strtab_size)); | |
1495 | lv.set_no_output_symtab_entry(); | |
1496 | continue; | |
1497 | } | |
1498 | ||
bb04269c DK |
1499 | // If --discard-locals option is used, discard all temporary local |
1500 | // symbols. These symbols start with system-specific local label | |
1501 | // prefixes, typically .L for ELF system. We want to be compatible | |
1502 | // with GNU ld so here we essentially use the same check in | |
1503 | // bfd_is_local_label(). The code is different because we already | |
1504 | // know that: | |
1505 | // | |
1506 | // - the symbol is local and thus cannot have global or weak binding. | |
1507 | // - the symbol is not a section symbol. | |
1508 | // - the symbol has a name. | |
1509 | // | |
1510 | // We do not discard a symbol if it needs a dynamic symbol entry. | |
7bf1f802 | 1511 | const char* name = pnames + sym.get_st_name(); |
bb04269c DK |
1512 | if (discard_locals |
1513 | && sym.get_st_type() != elfcpp::STT_FILE | |
1514 | && !lv.needs_output_dynsym_entry() | |
1515 | && parameters->target().is_local_label_name(name)) | |
1516 | { | |
1517 | lv.set_no_output_symtab_entry(); | |
1518 | continue; | |
1519 | } | |
1520 | ||
1521 | // Add the symbol to the symbol table string pool. | |
7bf1f802 ILT |
1522 | pool->add(name, true, NULL); |
1523 | ++count; | |
1524 | ||
1525 | // If needed, add the symbol to the dynamic symbol table string pool. | |
1526 | if (lv.needs_output_dynsym_entry()) | |
1527 | { | |
1528 | dynpool->add(name, true, NULL); | |
1529 | ++dyncount; | |
1530 | } | |
1531 | } | |
1532 | ||
1533 | this->output_local_symbol_count_ = count; | |
1534 | this->output_local_dynsym_count_ = dyncount; | |
1535 | } | |
1536 | ||
cb295612 | 1537 | // Finalize the local symbols. Here we set the final value in |
7bf1f802 | 1538 | // THIS->LOCAL_VALUES_ and set their output symbol table indexes. |
17a1d0a9 | 1539 | // This function is always called from a singleton thread. The actual |
7bf1f802 ILT |
1540 | // output of the local symbols will occur in a separate task. |
1541 | ||
1542 | template<int size, bool big_endian> | |
1543 | unsigned int | |
1544 | Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index, | |
d491d34e | 1545 | off_t off) |
7bf1f802 ILT |
1546 | { |
1547 | gold_assert(off == static_cast<off_t>(align_address(off, size >> 3))); | |
1548 | ||
1549 | const unsigned int loccount = this->local_symbol_count_; | |
1550 | this->local_symbol_offset_ = off; | |
1551 | ||
b4ecf66b | 1552 | const bool relocatable = parameters->options().relocatable(); |
ef9beddf ILT |
1553 | const Output_sections& out_sections(this->output_sections()); |
1554 | const std::vector<Address>& out_offsets(this->section_offsets_); | |
7bf1f802 ILT |
1555 | unsigned int shnum = this->shnum(); |
1556 | ||
1557 | for (unsigned int i = 1; i < loccount; ++i) | |
1558 | { | |
1559 | Symbol_value<size>& lv(this->local_values_[i]); | |
1560 | ||
d491d34e ILT |
1561 | bool is_ordinary; |
1562 | unsigned int shndx = lv.input_shndx(&is_ordinary); | |
7bf1f802 ILT |
1563 | |
1564 | // Set the output symbol value. | |
ef9beddf | 1565 | |
d491d34e | 1566 | if (!is_ordinary) |
75f65a3e | 1567 | { |
8a5e3e08 | 1568 | if (shndx == elfcpp::SHN_ABS || Symbol::is_common_shndx(shndx)) |
7bf1f802 | 1569 | lv.set_output_value(lv.input_value()); |
61ba1cf9 | 1570 | else |
75f65a3e | 1571 | { |
75f2446e ILT |
1572 | this->error(_("unknown section index %u for local symbol %u"), |
1573 | shndx, i); | |
1574 | lv.set_output_value(0); | |
75f65a3e | 1575 | } |
75f65a3e ILT |
1576 | } |
1577 | else | |
1578 | { | |
1579 | if (shndx >= shnum) | |
1580 | { | |
75f2446e ILT |
1581 | this->error(_("local symbol %u section index %u out of range"), |
1582 | i, shndx); | |
1583 | shndx = 0; | |
75f65a3e ILT |
1584 | } |
1585 | ||
ef9beddf | 1586 | Output_section* os = out_sections[shndx]; |
b8e6aad9 ILT |
1587 | |
1588 | if (os == NULL) | |
61ba1cf9 | 1589 | { |
e94cf127 CC |
1590 | // This local symbol belongs to a section we are discarding. |
1591 | // In some cases when applying relocations later, we will | |
1592 | // attempt to match it to the corresponding kept section, | |
1593 | // so we leave the input value unchanged here. | |
61ba1cf9 ILT |
1594 | continue; |
1595 | } | |
eff45813 | 1596 | else if (out_offsets[shndx] == invalid_address) |
7bf1f802 | 1597 | { |
e29e076a ILT |
1598 | uint64_t start; |
1599 | ||
a9a60db6 | 1600 | // This is a SHF_MERGE section or one which otherwise |
e29e076a | 1601 | // requires special handling. |
805bb01c DK |
1602 | if (shndx == this->discarded_eh_frame_shndx_) |
1603 | { | |
1604 | // This local symbol belongs to a discarded .eh_frame | |
1605 | // section. Just treat it like the case in which | |
1606 | // os == NULL above. | |
1607 | gold_assert(this->has_eh_frame_); | |
1608 | continue; | |
1609 | } | |
1610 | else if (!lv.is_section_symbol()) | |
e29e076a ILT |
1611 | { |
1612 | // This is not a section symbol. We can determine | |
1613 | // the final value now. | |
1614 | lv.set_output_value(os->output_address(this, shndx, | |
1615 | lv.input_value())); | |
1616 | } | |
1617 | else if (!os->find_starting_output_address(this, shndx, &start)) | |
1618 | { | |
1619 | // This is a section symbol, but apparently not one | |
1620 | // in a merged section. Just use the start of the | |
1621 | // output section. This happens with relocatable | |
1622 | // links when the input object has section symbols | |
1623 | // for arbitrary non-merge sections. | |
1624 | lv.set_output_value(os->address()); | |
1625 | } | |
a9a60db6 ILT |
1626 | else |
1627 | { | |
e29e076a ILT |
1628 | // We have to consider the addend to determine the |
1629 | // value to use in a relocation. START is the start | |
1630 | // of this input section. | |
a9a60db6 ILT |
1631 | Merged_symbol_value<size>* msv = |
1632 | new Merged_symbol_value<size>(lv.input_value(), start); | |
1633 | lv.set_merged_symbol_value(msv); | |
1634 | } | |
7bf1f802 ILT |
1635 | } |
1636 | else if (lv.is_tls_symbol()) | |
a9a60db6 | 1637 | lv.set_output_value(os->tls_offset() |
ef9beddf | 1638 | + out_offsets[shndx] |
7bf1f802 | 1639 | + lv.input_value()); |
b8e6aad9 | 1640 | else |
b4ecf66b | 1641 | lv.set_output_value((relocatable ? 0 : os->address()) |
ef9beddf | 1642 | + out_offsets[shndx] |
7bf1f802 | 1643 | + lv.input_value()); |
75f65a3e ILT |
1644 | } |
1645 | ||
7bf1f802 ILT |
1646 | if (lv.needs_output_symtab_entry()) |
1647 | { | |
1648 | lv.set_output_symtab_index(index); | |
1649 | ++index; | |
1650 | } | |
1651 | } | |
1652 | return index; | |
1653 | } | |
645f8123 | 1654 | |
7bf1f802 | 1655 | // Set the output dynamic symbol table indexes for the local variables. |
c06b7b0b | 1656 | |
7bf1f802 ILT |
1657 | template<int size, bool big_endian> |
1658 | unsigned int | |
1659 | Sized_relobj<size, big_endian>::do_set_local_dynsym_indexes(unsigned int index) | |
1660 | { | |
1661 | const unsigned int loccount = this->local_symbol_count_; | |
1662 | for (unsigned int i = 1; i < loccount; ++i) | |
1663 | { | |
1664 | Symbol_value<size>& lv(this->local_values_[i]); | |
1665 | if (lv.needs_output_dynsym_entry()) | |
1666 | { | |
1667 | lv.set_output_dynsym_index(index); | |
1668 | ++index; | |
1669 | } | |
75f65a3e | 1670 | } |
7bf1f802 ILT |
1671 | return index; |
1672 | } | |
75f65a3e | 1673 | |
7bf1f802 ILT |
1674 | // Set the offset where local dynamic symbol information will be stored. |
1675 | // Returns the count of local symbols contributed to the symbol table by | |
1676 | // this object. | |
61ba1cf9 | 1677 | |
7bf1f802 ILT |
1678 | template<int size, bool big_endian> |
1679 | unsigned int | |
1680 | Sized_relobj<size, big_endian>::do_set_local_dynsym_offset(off_t off) | |
1681 | { | |
1682 | gold_assert(off == static_cast<off_t>(align_address(off, size >> 3))); | |
1683 | this->local_dynsym_offset_ = off; | |
1684 | return this->output_local_dynsym_count_; | |
75f65a3e ILT |
1685 | } |
1686 | ||
61ba1cf9 ILT |
1687 | // Write out the local symbols. |
1688 | ||
1689 | template<int size, bool big_endian> | |
1690 | void | |
17a1d0a9 ILT |
1691 | Sized_relobj<size, big_endian>::write_local_symbols( |
1692 | Output_file* of, | |
1693 | const Stringpool* sympool, | |
d491d34e ILT |
1694 | const Stringpool* dynpool, |
1695 | Output_symtab_xindex* symtab_xindex, | |
1696 | Output_symtab_xindex* dynsym_xindex) | |
61ba1cf9 | 1697 | { |
99e9a495 ILT |
1698 | const bool strip_all = parameters->options().strip_all(); |
1699 | if (strip_all) | |
1700 | { | |
1701 | if (this->output_local_dynsym_count_ == 0) | |
1702 | return; | |
1703 | this->output_local_symbol_count_ = 0; | |
1704 | } | |
9e2dcb77 | 1705 | |
a3ad94ed | 1706 | gold_assert(this->symtab_shndx_ != -1U); |
645f8123 | 1707 | if (this->symtab_shndx_ == 0) |
61ba1cf9 ILT |
1708 | { |
1709 | // This object has no symbols. Weird but legal. | |
1710 | return; | |
1711 | } | |
1712 | ||
1713 | // Read the symbol table section header. | |
645f8123 ILT |
1714 | const unsigned int symtab_shndx = this->symtab_shndx_; |
1715 | typename This::Shdr symtabshdr(this, | |
1716 | this->elf_file_.section_header(symtab_shndx)); | |
a3ad94ed | 1717 | gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); |
92e059d8 | 1718 | const unsigned int loccount = this->local_symbol_count_; |
a3ad94ed | 1719 | gold_assert(loccount == symtabshdr.get_sh_info()); |
61ba1cf9 ILT |
1720 | |
1721 | // Read the local symbols. | |
1722 | const int sym_size = This::sym_size; | |
92e059d8 | 1723 | off_t locsize = loccount * sym_size; |
61ba1cf9 | 1724 | const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(), |
39d0cb0e | 1725 | locsize, true, false); |
61ba1cf9 | 1726 | |
61ba1cf9 | 1727 | // Read the symbol names. |
d491d34e ILT |
1728 | const unsigned int strtab_shndx = |
1729 | this->adjust_shndx(symtabshdr.get_sh_link()); | |
8383303e | 1730 | section_size_type strtab_size; |
645f8123 | 1731 | const unsigned char* pnamesu = this->section_contents(strtab_shndx, |
9eb9fa57 | 1732 | &strtab_size, |
cb295612 | 1733 | false); |
61ba1cf9 ILT |
1734 | const char* pnames = reinterpret_cast<const char*>(pnamesu); |
1735 | ||
7bf1f802 ILT |
1736 | // Get views into the output file for the portions of the symbol table |
1737 | // and the dynamic symbol table that we will be writing. | |
61ba1cf9 | 1738 | off_t output_size = this->output_local_symbol_count_ * sym_size; |
f2619d6c | 1739 | unsigned char* oview = NULL; |
7bf1f802 ILT |
1740 | if (output_size > 0) |
1741 | oview = of->get_output_view(this->local_symbol_offset_, output_size); | |
1742 | ||
1743 | off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size; | |
1744 | unsigned char* dyn_oview = NULL; | |
1745 | if (dyn_output_size > 0) | |
1746 | dyn_oview = of->get_output_view(this->local_dynsym_offset_, | |
1747 | dyn_output_size); | |
61ba1cf9 | 1748 | |
ef9beddf | 1749 | const Output_sections out_sections(this->output_sections()); |
c06b7b0b | 1750 | |
a3ad94ed | 1751 | gold_assert(this->local_values_.size() == loccount); |
61ba1cf9 | 1752 | |
61ba1cf9 | 1753 | unsigned char* ov = oview; |
7bf1f802 | 1754 | unsigned char* dyn_ov = dyn_oview; |
c06b7b0b | 1755 | psyms += sym_size; |
92e059d8 | 1756 | for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size) |
61ba1cf9 ILT |
1757 | { |
1758 | elfcpp::Sym<size, big_endian> isym(psyms); | |
f6ce93d6 | 1759 | |
d491d34e ILT |
1760 | Symbol_value<size>& lv(this->local_values_[i]); |
1761 | ||
1762 | bool is_ordinary; | |
1763 | unsigned int st_shndx = this->adjust_sym_shndx(i, isym.get_st_shndx(), | |
1764 | &is_ordinary); | |
1765 | if (is_ordinary) | |
61ba1cf9 | 1766 | { |
ef9beddf ILT |
1767 | gold_assert(st_shndx < out_sections.size()); |
1768 | if (out_sections[st_shndx] == NULL) | |
61ba1cf9 | 1769 | continue; |
ef9beddf | 1770 | st_shndx = out_sections[st_shndx]->out_shndx(); |
d491d34e ILT |
1771 | if (st_shndx >= elfcpp::SHN_LORESERVE) |
1772 | { | |
99e9a495 | 1773 | if (lv.needs_output_symtab_entry() && !strip_all) |
d491d34e ILT |
1774 | symtab_xindex->add(lv.output_symtab_index(), st_shndx); |
1775 | if (lv.needs_output_dynsym_entry()) | |
1776 | dynsym_xindex->add(lv.output_dynsym_index(), st_shndx); | |
1777 | st_shndx = elfcpp::SHN_XINDEX; | |
1778 | } | |
61ba1cf9 ILT |
1779 | } |
1780 | ||
7bf1f802 | 1781 | // Write the symbol to the output symbol table. |
99e9a495 | 1782 | if (!strip_all && lv.needs_output_symtab_entry()) |
7bf1f802 ILT |
1783 | { |
1784 | elfcpp::Sym_write<size, big_endian> osym(ov); | |
1785 | ||
1786 | gold_assert(isym.get_st_name() < strtab_size); | |
1787 | const char* name = pnames + isym.get_st_name(); | |
1788 | osym.put_st_name(sympool->get_offset(name)); | |
1789 | osym.put_st_value(this->local_values_[i].value(this, 0)); | |
1790 | osym.put_st_size(isym.get_st_size()); | |
1791 | osym.put_st_info(isym.get_st_info()); | |
1792 | osym.put_st_other(isym.get_st_other()); | |
1793 | osym.put_st_shndx(st_shndx); | |
1794 | ||
1795 | ov += sym_size; | |
1796 | } | |
1797 | ||
1798 | // Write the symbol to the output dynamic symbol table. | |
d491d34e | 1799 | if (lv.needs_output_dynsym_entry()) |
7bf1f802 ILT |
1800 | { |
1801 | gold_assert(dyn_ov < dyn_oview + dyn_output_size); | |
1802 | elfcpp::Sym_write<size, big_endian> osym(dyn_ov); | |
1803 | ||
1804 | gold_assert(isym.get_st_name() < strtab_size); | |
1805 | const char* name = pnames + isym.get_st_name(); | |
1806 | osym.put_st_name(dynpool->get_offset(name)); | |
1807 | osym.put_st_value(this->local_values_[i].value(this, 0)); | |
1808 | osym.put_st_size(isym.get_st_size()); | |
1809 | osym.put_st_info(isym.get_st_info()); | |
1810 | osym.put_st_other(isym.get_st_other()); | |
1811 | osym.put_st_shndx(st_shndx); | |
1812 | ||
1813 | dyn_ov += sym_size; | |
1814 | } | |
1815 | } | |
f6ce93d6 | 1816 | |
61ba1cf9 | 1817 | |
7bf1f802 ILT |
1818 | if (output_size > 0) |
1819 | { | |
1820 | gold_assert(ov - oview == output_size); | |
1821 | of->write_output_view(this->local_symbol_offset_, output_size, oview); | |
61ba1cf9 ILT |
1822 | } |
1823 | ||
7bf1f802 ILT |
1824 | if (dyn_output_size > 0) |
1825 | { | |
1826 | gold_assert(dyn_ov - dyn_oview == dyn_output_size); | |
1827 | of->write_output_view(this->local_dynsym_offset_, dyn_output_size, | |
1828 | dyn_oview); | |
1829 | } | |
61ba1cf9 ILT |
1830 | } |
1831 | ||
f7e2ee48 ILT |
1832 | // Set *INFO to symbolic information about the offset OFFSET in the |
1833 | // section SHNDX. Return true if we found something, false if we | |
1834 | // found nothing. | |
1835 | ||
1836 | template<int size, bool big_endian> | |
1837 | bool | |
1838 | Sized_relobj<size, big_endian>::get_symbol_location_info( | |
1839 | unsigned int shndx, | |
1840 | off_t offset, | |
1841 | Symbol_location_info* info) | |
1842 | { | |
1843 | if (this->symtab_shndx_ == 0) | |
1844 | return false; | |
1845 | ||
8383303e | 1846 | section_size_type symbols_size; |
f7e2ee48 ILT |
1847 | const unsigned char* symbols = this->section_contents(this->symtab_shndx_, |
1848 | &symbols_size, | |
1849 | false); | |
1850 | ||
d491d34e ILT |
1851 | unsigned int symbol_names_shndx = |
1852 | this->adjust_shndx(this->section_link(this->symtab_shndx_)); | |
8383303e | 1853 | section_size_type names_size; |
f7e2ee48 ILT |
1854 | const unsigned char* symbol_names_u = |
1855 | this->section_contents(symbol_names_shndx, &names_size, false); | |
1856 | const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u); | |
1857 | ||
1858 | const int sym_size = This::sym_size; | |
1859 | const size_t count = symbols_size / sym_size; | |
1860 | ||
1861 | const unsigned char* p = symbols; | |
1862 | for (size_t i = 0; i < count; ++i, p += sym_size) | |
1863 | { | |
1864 | elfcpp::Sym<size, big_endian> sym(p); | |
1865 | ||
1866 | if (sym.get_st_type() == elfcpp::STT_FILE) | |
1867 | { | |
1868 | if (sym.get_st_name() >= names_size) | |
1869 | info->source_file = "(invalid)"; | |
1870 | else | |
1871 | info->source_file = symbol_names + sym.get_st_name(); | |
d491d34e | 1872 | continue; |
f7e2ee48 | 1873 | } |
d491d34e ILT |
1874 | |
1875 | bool is_ordinary; | |
1876 | unsigned int st_shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(), | |
1877 | &is_ordinary); | |
1878 | if (is_ordinary | |
1879 | && st_shndx == shndx | |
1880 | && static_cast<off_t>(sym.get_st_value()) <= offset | |
1881 | && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size()) | |
1882 | > offset)) | |
f7e2ee48 ILT |
1883 | { |
1884 | if (sym.get_st_name() > names_size) | |
1885 | info->enclosing_symbol_name = "(invalid)"; | |
1886 | else | |
a2b1aa12 ILT |
1887 | { |
1888 | info->enclosing_symbol_name = symbol_names + sym.get_st_name(); | |
086a1841 | 1889 | if (parameters->options().do_demangle()) |
a2b1aa12 ILT |
1890 | { |
1891 | char* demangled_name = cplus_demangle( | |
1892 | info->enclosing_symbol_name.c_str(), | |
1893 | DMGL_ANSI | DMGL_PARAMS); | |
1894 | if (demangled_name != NULL) | |
1895 | { | |
1896 | info->enclosing_symbol_name.assign(demangled_name); | |
1897 | free(demangled_name); | |
1898 | } | |
1899 | } | |
1900 | } | |
f7e2ee48 ILT |
1901 | return true; |
1902 | } | |
1903 | } | |
1904 | ||
1905 | return false; | |
1906 | } | |
1907 | ||
e94cf127 CC |
1908 | // Look for a kept section corresponding to the given discarded section, |
1909 | // and return its output address. This is used only for relocations in | |
1910 | // debugging sections. If we can't find the kept section, return 0. | |
1911 | ||
1912 | template<int size, bool big_endian> | |
1913 | typename Sized_relobj<size, big_endian>::Address | |
1914 | Sized_relobj<size, big_endian>::map_to_kept_section( | |
1915 | unsigned int shndx, | |
1916 | bool* found) const | |
1917 | { | |
1918 | Kept_comdat_section *kept = this->get_kept_comdat_section(shndx); | |
1919 | if (kept != NULL) | |
1920 | { | |
1921 | gold_assert(kept->object_ != NULL); | |
1922 | *found = true; | |
ef9beddf ILT |
1923 | Output_section* os = kept->object_->output_section(kept->shndx_); |
1924 | Address offset = kept->object_->get_output_section_offset(kept->shndx_); | |
531813ad ST |
1925 | if (os != NULL && offset != invalid_address) |
1926 | return os->address() + offset; | |
e94cf127 CC |
1927 | } |
1928 | *found = false; | |
1929 | return 0; | |
1930 | } | |
1931 | ||
92de84a6 ILT |
1932 | // Get symbol counts. |
1933 | ||
1934 | template<int size, bool big_endian> | |
1935 | void | |
1936 | Sized_relobj<size, big_endian>::do_get_global_symbol_counts( | |
1937 | const Symbol_table*, | |
1938 | size_t* defined, | |
1939 | size_t* used) const | |
1940 | { | |
1941 | *defined = this->defined_count_; | |
1942 | size_t count = 0; | |
1943 | for (Symbols::const_iterator p = this->symbols_.begin(); | |
1944 | p != this->symbols_.end(); | |
1945 | ++p) | |
1946 | if (*p != NULL | |
1947 | && (*p)->source() == Symbol::FROM_OBJECT | |
1948 | && (*p)->object() == this | |
1949 | && (*p)->is_defined()) | |
1950 | ++count; | |
1951 | *used = count; | |
1952 | } | |
1953 | ||
54dc6425 ILT |
1954 | // Input_objects methods. |
1955 | ||
008db82e ILT |
1956 | // Add a regular relocatable object to the list. Return false if this |
1957 | // object should be ignored. | |
f6ce93d6 | 1958 | |
008db82e | 1959 | bool |
54dc6425 ILT |
1960 | Input_objects::add_object(Object* obj) |
1961 | { | |
fbfba508 | 1962 | // Set the global target from the first object file we recognize. |
019cdb1a | 1963 | Target* target = obj->target(); |
8851ecca | 1964 | if (!parameters->target_valid()) |
fbfba508 | 1965 | set_parameters_target(target); |
8851ecca | 1966 | else if (target != ¶meters->target()) |
019cdb1a | 1967 | { |
fbfba508 | 1968 | obj->error(_("incompatible target")); |
019cdb1a ILT |
1969 | return false; |
1970 | } | |
1971 | ||
c5818ff1 CC |
1972 | // Print the filename if the -t/--trace option is selected. |
1973 | if (parameters->options().trace()) | |
1974 | gold_info("%s", obj->name().c_str()); | |
1975 | ||
008db82e | 1976 | if (!obj->is_dynamic()) |
f6ce93d6 | 1977 | this->relobj_list_.push_back(static_cast<Relobj*>(obj)); |
008db82e ILT |
1978 | else |
1979 | { | |
1980 | // See if this is a duplicate SONAME. | |
1981 | Dynobj* dynobj = static_cast<Dynobj*>(obj); | |
9a2d6984 | 1982 | const char* soname = dynobj->soname(); |
008db82e ILT |
1983 | |
1984 | std::pair<Unordered_set<std::string>::iterator, bool> ins = | |
9a2d6984 | 1985 | this->sonames_.insert(soname); |
008db82e ILT |
1986 | if (!ins.second) |
1987 | { | |
1988 | // We have already seen a dynamic object with this soname. | |
1989 | return false; | |
1990 | } | |
1991 | ||
1992 | this->dynobj_list_.push_back(dynobj); | |
1993 | } | |
75f65a3e | 1994 | |
92de84a6 ILT |
1995 | // Add this object to the cross-referencer if requested. |
1996 | if (parameters->options().user_set_print_symbol_counts()) | |
1997 | { | |
1998 | if (this->cref_ == NULL) | |
1999 | this->cref_ = new Cref(); | |
2000 | this->cref_->add_object(obj); | |
2001 | } | |
2002 | ||
008db82e | 2003 | return true; |
54dc6425 ILT |
2004 | } |
2005 | ||
e2827e5f ILT |
2006 | // For each dynamic object, record whether we've seen all of its |
2007 | // explicit dependencies. | |
2008 | ||
2009 | void | |
2010 | Input_objects::check_dynamic_dependencies() const | |
2011 | { | |
2012 | for (Dynobj_list::const_iterator p = this->dynobj_list_.begin(); | |
2013 | p != this->dynobj_list_.end(); | |
2014 | ++p) | |
2015 | { | |
2016 | const Dynobj::Needed& needed((*p)->needed()); | |
2017 | bool found_all = true; | |
2018 | for (Dynobj::Needed::const_iterator pneeded = needed.begin(); | |
2019 | pneeded != needed.end(); | |
2020 | ++pneeded) | |
2021 | { | |
2022 | if (this->sonames_.find(*pneeded) == this->sonames_.end()) | |
2023 | { | |
2024 | found_all = false; | |
2025 | break; | |
2026 | } | |
2027 | } | |
2028 | (*p)->set_has_unknown_needed_entries(!found_all); | |
2029 | } | |
2030 | } | |
2031 | ||
92de84a6 ILT |
2032 | // Start processing an archive. |
2033 | ||
2034 | void | |
2035 | Input_objects::archive_start(Archive* archive) | |
2036 | { | |
2037 | if (parameters->options().user_set_print_symbol_counts()) | |
2038 | { | |
2039 | if (this->cref_ == NULL) | |
2040 | this->cref_ = new Cref(); | |
2041 | this->cref_->add_archive_start(archive); | |
2042 | } | |
2043 | } | |
2044 | ||
2045 | // Stop processing an archive. | |
2046 | ||
2047 | void | |
2048 | Input_objects::archive_stop(Archive* archive) | |
2049 | { | |
2050 | if (parameters->options().user_set_print_symbol_counts()) | |
2051 | this->cref_->add_archive_stop(archive); | |
2052 | } | |
2053 | ||
2054 | // Print symbol counts | |
2055 | ||
2056 | void | |
2057 | Input_objects::print_symbol_counts(const Symbol_table* symtab) const | |
2058 | { | |
2059 | if (parameters->options().user_set_print_symbol_counts() | |
2060 | && this->cref_ != NULL) | |
2061 | this->cref_->print_symbol_counts(symtab); | |
2062 | } | |
2063 | ||
92e059d8 ILT |
2064 | // Relocate_info methods. |
2065 | ||
2066 | // Return a string describing the location of a relocation. This is | |
2067 | // only used in error messages. | |
2068 | ||
2069 | template<int size, bool big_endian> | |
2070 | std::string | |
f7e2ee48 | 2071 | Relocate_info<size, big_endian>::location(size_t, off_t offset) const |
92e059d8 | 2072 | { |
5c2c6c95 ILT |
2073 | // See if we can get line-number information from debugging sections. |
2074 | std::string filename; | |
2075 | std::string file_and_lineno; // Better than filename-only, if available. | |
4c50553d | 2076 | |
a55ce7fe | 2077 | Sized_dwarf_line_info<size, big_endian> line_info(this->object); |
24badc65 ILT |
2078 | // This will be "" if we failed to parse the debug info for any reason. |
2079 | file_and_lineno = line_info.addr2line(this->data_shndx, offset); | |
4c50553d | 2080 | |
92e059d8 | 2081 | std::string ret(this->object->name()); |
f7e2ee48 ILT |
2082 | ret += ':'; |
2083 | Symbol_location_info info; | |
2084 | if (this->object->get_symbol_location_info(this->data_shndx, offset, &info)) | |
2085 | { | |
2086 | ret += " in function "; | |
2087 | ret += info.enclosing_symbol_name; | |
2088 | ret += ":"; | |
5c2c6c95 ILT |
2089 | filename = info.source_file; |
2090 | } | |
2091 | ||
2092 | if (!file_and_lineno.empty()) | |
2093 | ret += file_and_lineno; | |
2094 | else | |
2095 | { | |
2096 | if (!filename.empty()) | |
2097 | ret += filename; | |
2098 | ret += "("; | |
2099 | ret += this->object->section_name(this->data_shndx); | |
2100 | char buf[100]; | |
2101 | // Offsets into sections have to be positive. | |
2102 | snprintf(buf, sizeof(buf), "+0x%lx", static_cast<long>(offset)); | |
2103 | ret += buf; | |
2104 | ret += ")"; | |
f7e2ee48 | 2105 | } |
92e059d8 ILT |
2106 | return ret; |
2107 | } | |
2108 | ||
bae7f79e ILT |
2109 | } // End namespace gold. |
2110 | ||
2111 | namespace | |
2112 | { | |
2113 | ||
2114 | using namespace gold; | |
2115 | ||
2116 | // Read an ELF file with the header and return the appropriate | |
2117 | // instance of Object. | |
2118 | ||
2119 | template<int size, bool big_endian> | |
2120 | Object* | |
2121 | make_elf_sized_object(const std::string& name, Input_file* input_file, | |
2122 | off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr) | |
2123 | { | |
2124 | int et = ehdr.get_e_type(); | |
bae7f79e ILT |
2125 | if (et == elfcpp::ET_REL) |
2126 | { | |
f6ce93d6 ILT |
2127 | Sized_relobj<size, big_endian>* obj = |
2128 | new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr); | |
bae7f79e ILT |
2129 | obj->setup(ehdr); |
2130 | return obj; | |
2131 | } | |
dbe717ef ILT |
2132 | else if (et == elfcpp::ET_DYN) |
2133 | { | |
2134 | Sized_dynobj<size, big_endian>* obj = | |
2135 | new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr); | |
2136 | obj->setup(ehdr); | |
2137 | return obj; | |
2138 | } | |
bae7f79e ILT |
2139 | else |
2140 | { | |
75f2446e ILT |
2141 | gold_error(_("%s: unsupported ELF file type %d"), |
2142 | name.c_str(), et); | |
2143 | return NULL; | |
bae7f79e ILT |
2144 | } |
2145 | } | |
2146 | ||
2147 | } // End anonymous namespace. | |
2148 | ||
2149 | namespace gold | |
2150 | { | |
2151 | ||
f6060a4d ILT |
2152 | // Return whether INPUT_FILE is an ELF object. |
2153 | ||
2154 | bool | |
2155 | is_elf_object(Input_file* input_file, off_t offset, | |
2156 | const unsigned char** start, int *read_size) | |
2157 | { | |
2158 | off_t filesize = input_file->file().filesize(); | |
2159 | int want = elfcpp::Elf_sizes<64>::ehdr_size; | |
2160 | if (filesize - offset < want) | |
2161 | want = filesize - offset; | |
2162 | ||
2163 | const unsigned char* p = input_file->file().get_view(offset, 0, want, | |
2164 | true, false); | |
2165 | *start = p; | |
2166 | *read_size = want; | |
2167 | ||
2168 | if (want < 4) | |
2169 | return false; | |
2170 | ||
2171 | static unsigned char elfmagic[4] = | |
2172 | { | |
2173 | elfcpp::ELFMAG0, elfcpp::ELFMAG1, | |
2174 | elfcpp::ELFMAG2, elfcpp::ELFMAG3 | |
2175 | }; | |
2176 | return memcmp(p, elfmagic, 4) == 0; | |
2177 | } | |
2178 | ||
bae7f79e ILT |
2179 | // Read an ELF file and return the appropriate instance of Object. |
2180 | ||
2181 | Object* | |
2182 | make_elf_object(const std::string& name, Input_file* input_file, off_t offset, | |
15f8229b ILT |
2183 | const unsigned char* p, section_offset_type bytes, |
2184 | bool* punconfigured) | |
bae7f79e | 2185 | { |
15f8229b ILT |
2186 | if (punconfigured != NULL) |
2187 | *punconfigured = false; | |
2188 | ||
bae7f79e ILT |
2189 | if (bytes < elfcpp::EI_NIDENT) |
2190 | { | |
75f2446e ILT |
2191 | gold_error(_("%s: ELF file too short"), name.c_str()); |
2192 | return NULL; | |
bae7f79e ILT |
2193 | } |
2194 | ||
2195 | int v = p[elfcpp::EI_VERSION]; | |
2196 | if (v != elfcpp::EV_CURRENT) | |
2197 | { | |
2198 | if (v == elfcpp::EV_NONE) | |
75f2446e | 2199 | gold_error(_("%s: invalid ELF version 0"), name.c_str()); |
bae7f79e | 2200 | else |
75f2446e ILT |
2201 | gold_error(_("%s: unsupported ELF version %d"), name.c_str(), v); |
2202 | return NULL; | |
bae7f79e ILT |
2203 | } |
2204 | ||
2205 | int c = p[elfcpp::EI_CLASS]; | |
2206 | if (c == elfcpp::ELFCLASSNONE) | |
2207 | { | |
75f2446e ILT |
2208 | gold_error(_("%s: invalid ELF class 0"), name.c_str()); |
2209 | return NULL; | |
bae7f79e ILT |
2210 | } |
2211 | else if (c != elfcpp::ELFCLASS32 | |
2212 | && c != elfcpp::ELFCLASS64) | |
2213 | { | |
75f2446e ILT |
2214 | gold_error(_("%s: unsupported ELF class %d"), name.c_str(), c); |
2215 | return NULL; | |
bae7f79e ILT |
2216 | } |
2217 | ||
2218 | int d = p[elfcpp::EI_DATA]; | |
2219 | if (d == elfcpp::ELFDATANONE) | |
2220 | { | |
75f2446e ILT |
2221 | gold_error(_("%s: invalid ELF data encoding"), name.c_str()); |
2222 | return NULL; | |
bae7f79e ILT |
2223 | } |
2224 | else if (d != elfcpp::ELFDATA2LSB | |
2225 | && d != elfcpp::ELFDATA2MSB) | |
2226 | { | |
75f2446e ILT |
2227 | gold_error(_("%s: unsupported ELF data encoding %d"), name.c_str(), d); |
2228 | return NULL; | |
bae7f79e ILT |
2229 | } |
2230 | ||
2231 | bool big_endian = d == elfcpp::ELFDATA2MSB; | |
2232 | ||
2233 | if (c == elfcpp::ELFCLASS32) | |
2234 | { | |
2235 | if (bytes < elfcpp::Elf_sizes<32>::ehdr_size) | |
2236 | { | |
75f2446e ILT |
2237 | gold_error(_("%s: ELF file too short"), name.c_str()); |
2238 | return NULL; | |
bae7f79e ILT |
2239 | } |
2240 | if (big_endian) | |
2241 | { | |
193a53d9 | 2242 | #ifdef HAVE_TARGET_32_BIG |
bae7f79e ILT |
2243 | elfcpp::Ehdr<32, true> ehdr(p); |
2244 | return make_elf_sized_object<32, true>(name, input_file, | |
2245 | offset, ehdr); | |
193a53d9 | 2246 | #else |
15f8229b ILT |
2247 | if (punconfigured != NULL) |
2248 | *punconfigured = true; | |
2249 | else | |
2250 | gold_error(_("%s: not configured to support " | |
2251 | "32-bit big-endian object"), | |
2252 | name.c_str()); | |
75f2446e | 2253 | return NULL; |
193a53d9 | 2254 | #endif |
bae7f79e ILT |
2255 | } |
2256 | else | |
2257 | { | |
193a53d9 | 2258 | #ifdef HAVE_TARGET_32_LITTLE |
bae7f79e ILT |
2259 | elfcpp::Ehdr<32, false> ehdr(p); |
2260 | return make_elf_sized_object<32, false>(name, input_file, | |
2261 | offset, ehdr); | |
193a53d9 | 2262 | #else |
15f8229b ILT |
2263 | if (punconfigured != NULL) |
2264 | *punconfigured = true; | |
2265 | else | |
2266 | gold_error(_("%s: not configured to support " | |
2267 | "32-bit little-endian object"), | |
2268 | name.c_str()); | |
75f2446e | 2269 | return NULL; |
193a53d9 | 2270 | #endif |
bae7f79e ILT |
2271 | } |
2272 | } | |
2273 | else | |
2274 | { | |
c165fb93 | 2275 | if (bytes < elfcpp::Elf_sizes<64>::ehdr_size) |
bae7f79e | 2276 | { |
75f2446e ILT |
2277 | gold_error(_("%s: ELF file too short"), name.c_str()); |
2278 | return NULL; | |
bae7f79e ILT |
2279 | } |
2280 | if (big_endian) | |
2281 | { | |
193a53d9 | 2282 | #ifdef HAVE_TARGET_64_BIG |
bae7f79e ILT |
2283 | elfcpp::Ehdr<64, true> ehdr(p); |
2284 | return make_elf_sized_object<64, true>(name, input_file, | |
2285 | offset, ehdr); | |
193a53d9 | 2286 | #else |
15f8229b ILT |
2287 | if (punconfigured != NULL) |
2288 | *punconfigured = true; | |
2289 | else | |
2290 | gold_error(_("%s: not configured to support " | |
2291 | "64-bit big-endian object"), | |
2292 | name.c_str()); | |
75f2446e | 2293 | return NULL; |
193a53d9 | 2294 | #endif |
bae7f79e ILT |
2295 | } |
2296 | else | |
2297 | { | |
193a53d9 | 2298 | #ifdef HAVE_TARGET_64_LITTLE |
bae7f79e ILT |
2299 | elfcpp::Ehdr<64, false> ehdr(p); |
2300 | return make_elf_sized_object<64, false>(name, input_file, | |
2301 | offset, ehdr); | |
193a53d9 | 2302 | #else |
15f8229b ILT |
2303 | if (punconfigured != NULL) |
2304 | *punconfigured = true; | |
2305 | else | |
2306 | gold_error(_("%s: not configured to support " | |
2307 | "64-bit little-endian object"), | |
2308 | name.c_str()); | |
75f2446e | 2309 | return NULL; |
193a53d9 | 2310 | #endif |
bae7f79e ILT |
2311 | } |
2312 | } | |
2313 | } | |
2314 | ||
04bf7072 ILT |
2315 | // Instantiate the templates we need. |
2316 | ||
2317 | #ifdef HAVE_TARGET_32_LITTLE | |
2318 | template | |
2319 | void | |
2320 | Object::read_section_data<32, false>(elfcpp::Elf_file<32, false, Object>*, | |
2321 | Read_symbols_data*); | |
2322 | #endif | |
2323 | ||
2324 | #ifdef HAVE_TARGET_32_BIG | |
2325 | template | |
2326 | void | |
2327 | Object::read_section_data<32, true>(elfcpp::Elf_file<32, true, Object>*, | |
2328 | Read_symbols_data*); | |
2329 | #endif | |
2330 | ||
2331 | #ifdef HAVE_TARGET_64_LITTLE | |
2332 | template | |
2333 | void | |
2334 | Object::read_section_data<64, false>(elfcpp::Elf_file<64, false, Object>*, | |
2335 | Read_symbols_data*); | |
2336 | #endif | |
2337 | ||
2338 | #ifdef HAVE_TARGET_64_BIG | |
2339 | template | |
2340 | void | |
2341 | Object::read_section_data<64, true>(elfcpp::Elf_file<64, true, Object>*, | |
2342 | Read_symbols_data*); | |
2343 | #endif | |
bae7f79e | 2344 | |
193a53d9 | 2345 | #ifdef HAVE_TARGET_32_LITTLE |
bae7f79e | 2346 | template |
f6ce93d6 | 2347 | class Sized_relobj<32, false>; |
193a53d9 | 2348 | #endif |
bae7f79e | 2349 | |
193a53d9 | 2350 | #ifdef HAVE_TARGET_32_BIG |
bae7f79e | 2351 | template |
f6ce93d6 | 2352 | class Sized_relobj<32, true>; |
193a53d9 | 2353 | #endif |
bae7f79e | 2354 | |
193a53d9 | 2355 | #ifdef HAVE_TARGET_64_LITTLE |
bae7f79e | 2356 | template |
f6ce93d6 | 2357 | class Sized_relobj<64, false>; |
193a53d9 | 2358 | #endif |
bae7f79e | 2359 | |
193a53d9 | 2360 | #ifdef HAVE_TARGET_64_BIG |
bae7f79e | 2361 | template |
f6ce93d6 | 2362 | class Sized_relobj<64, true>; |
193a53d9 | 2363 | #endif |
bae7f79e | 2364 | |
193a53d9 | 2365 | #ifdef HAVE_TARGET_32_LITTLE |
92e059d8 ILT |
2366 | template |
2367 | struct Relocate_info<32, false>; | |
193a53d9 | 2368 | #endif |
92e059d8 | 2369 | |
193a53d9 | 2370 | #ifdef HAVE_TARGET_32_BIG |
92e059d8 ILT |
2371 | template |
2372 | struct Relocate_info<32, true>; | |
193a53d9 | 2373 | #endif |
92e059d8 | 2374 | |
193a53d9 | 2375 | #ifdef HAVE_TARGET_64_LITTLE |
92e059d8 ILT |
2376 | template |
2377 | struct Relocate_info<64, false>; | |
193a53d9 | 2378 | #endif |
92e059d8 | 2379 | |
193a53d9 | 2380 | #ifdef HAVE_TARGET_64_BIG |
92e059d8 ILT |
2381 | template |
2382 | struct Relocate_info<64, true>; | |
193a53d9 | 2383 | #endif |
92e059d8 | 2384 | |
bae7f79e | 2385 | } // End namespace gold. |