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