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