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