Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // object.cc -- support for an object file for linking in gold |
2 | ||
ebdbb458 | 3 | // Copyright 2006, 2007, 2008 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 | |
14bfc3f5 | 31 | #include "target-select.h" |
5c2c6c95 | 32 | #include "dwarf_reader.h" |
a2fb1b05 | 33 | #include "layout.h" |
61ba1cf9 | 34 | #include "output.h" |
f6ce93d6 | 35 | #include "symtab.h" |
4c50553d | 36 | #include "reloc.h" |
f6ce93d6 ILT |
37 | #include "object.h" |
38 | #include "dynobj.h" | |
bae7f79e ILT |
39 | |
40 | namespace gold | |
41 | { | |
42 | ||
d491d34e ILT |
43 | // Class Xindex. |
44 | ||
45 | // Initialize the symtab_xindex_ array. Find the SHT_SYMTAB_SHNDX | |
46 | // section and read it in. SYMTAB_SHNDX is the index of the symbol | |
47 | // table we care about. | |
48 | ||
49 | template<int size, bool big_endian> | |
50 | void | |
51 | Xindex::initialize_symtab_xindex(Object* object, unsigned int symtab_shndx) | |
52 | { | |
53 | if (!this->symtab_xindex_.empty()) | |
54 | return; | |
55 | ||
56 | gold_assert(symtab_shndx != 0); | |
57 | ||
58 | // Look through the sections in reverse order, on the theory that it | |
59 | // is more likely to be near the end than the beginning. | |
60 | unsigned int i = object->shnum(); | |
61 | while (i > 0) | |
62 | { | |
63 | --i; | |
64 | if (object->section_type(i) == elfcpp::SHT_SYMTAB_SHNDX | |
65 | && this->adjust_shndx(object->section_link(i)) == symtab_shndx) | |
66 | { | |
67 | this->read_symtab_xindex<size, big_endian>(object, i, NULL); | |
68 | return; | |
69 | } | |
70 | } | |
71 | ||
72 | object->error(_("missing SHT_SYMTAB_SHNDX section")); | |
73 | } | |
74 | ||
75 | // Read in the symtab_xindex_ array, given the section index of the | |
76 | // SHT_SYMTAB_SHNDX section. If PSHDRS is not NULL, it points at the | |
77 | // section headers. | |
78 | ||
79 | template<int size, bool big_endian> | |
80 | void | |
81 | Xindex::read_symtab_xindex(Object* object, unsigned int xindex_shndx, | |
82 | const unsigned char* pshdrs) | |
83 | { | |
84 | section_size_type bytecount; | |
85 | const unsigned char* contents; | |
86 | if (pshdrs == NULL) | |
87 | contents = object->section_contents(xindex_shndx, &bytecount, false); | |
88 | else | |
89 | { | |
90 | const unsigned char* p = (pshdrs | |
91 | + (xindex_shndx | |
92 | * elfcpp::Elf_sizes<size>::shdr_size)); | |
93 | typename elfcpp::Shdr<size, big_endian> shdr(p); | |
94 | bytecount = convert_to_section_size_type(shdr.get_sh_size()); | |
95 | contents = object->get_view(shdr.get_sh_offset(), bytecount, true, false); | |
96 | } | |
97 | ||
98 | gold_assert(this->symtab_xindex_.empty()); | |
99 | this->symtab_xindex_.reserve(bytecount / 4); | |
100 | for (section_size_type i = 0; i < bytecount; i += 4) | |
101 | { | |
102 | unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(contents + i); | |
103 | // We preadjust the section indexes we save. | |
104 | this->symtab_xindex_.push_back(this->adjust_shndx(shndx)); | |
105 | } | |
106 | } | |
107 | ||
108 | // Symbol symndx has a section of SHN_XINDEX; return the real section | |
109 | // index. | |
110 | ||
111 | unsigned int | |
112 | Xindex::sym_xindex_to_shndx(Object* object, unsigned int symndx) | |
113 | { | |
114 | if (symndx >= this->symtab_xindex_.size()) | |
115 | { | |
116 | object->error(_("symbol %u out of range for SHT_SYMTAB_SHNDX section"), | |
117 | symndx); | |
118 | return elfcpp::SHN_UNDEF; | |
119 | } | |
120 | unsigned int shndx = this->symtab_xindex_[symndx]; | |
121 | if (shndx < elfcpp::SHN_LORESERVE || shndx >= object->shnum()) | |
122 | { | |
123 | object->error(_("extended index for symbol %u out of range: %u"), | |
124 | symndx, shndx); | |
125 | return elfcpp::SHN_UNDEF; | |
126 | } | |
127 | return shndx; | |
128 | } | |
129 | ||
645f8123 ILT |
130 | // Class Object. |
131 | ||
dbe717ef ILT |
132 | // Set the target based on fields in the ELF file header. |
133 | ||
134 | void | |
135 | Object::set_target(int machine, int size, bool big_endian, int osabi, | |
136 | int abiversion) | |
137 | { | |
138 | Target* target = select_target(machine, size, big_endian, osabi, abiversion); | |
139 | if (target == NULL) | |
75f2446e ILT |
140 | gold_fatal(_("%s: unsupported ELF machine number %d"), |
141 | this->name().c_str(), machine); | |
dbe717ef ILT |
142 | this->target_ = target; |
143 | } | |
144 | ||
75f2446e ILT |
145 | // Report an error for this object file. This is used by the |
146 | // elfcpp::Elf_file interface, and also called by the Object code | |
147 | // itself. | |
645f8123 ILT |
148 | |
149 | void | |
75f2446e | 150 | Object::error(const char* format, ...) const |
645f8123 ILT |
151 | { |
152 | va_list args; | |
645f8123 | 153 | va_start(args, format); |
75f2446e ILT |
154 | char* buf = NULL; |
155 | if (vasprintf(&buf, format, args) < 0) | |
156 | gold_nomem(); | |
645f8123 | 157 | va_end(args); |
75f2446e ILT |
158 | gold_error(_("%s: %s"), this->name().c_str(), buf); |
159 | free(buf); | |
645f8123 ILT |
160 | } |
161 | ||
162 | // Return a view of the contents of a section. | |
163 | ||
164 | const unsigned char* | |
8383303e ILT |
165 | Object::section_contents(unsigned int shndx, section_size_type* plen, |
166 | bool cache) | |
645f8123 ILT |
167 | { |
168 | Location loc(this->do_section_contents(shndx)); | |
8383303e | 169 | *plen = convert_to_section_size_type(loc.data_size); |
39d0cb0e | 170 | return this->get_view(loc.file_offset, *plen, true, cache); |
645f8123 ILT |
171 | } |
172 | ||
dbe717ef ILT |
173 | // Read the section data into SD. This is code common to Sized_relobj |
174 | // and Sized_dynobj, so we put it into Object. | |
175 | ||
176 | template<int size, bool big_endian> | |
177 | void | |
178 | Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file, | |
179 | Read_symbols_data* sd) | |
180 | { | |
181 | const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; | |
182 | ||
183 | // Read the section headers. | |
184 | const off_t shoff = elf_file->shoff(); | |
185 | const unsigned int shnum = this->shnum(); | |
39d0cb0e ILT |
186 | sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size, |
187 | true, true); | |
dbe717ef ILT |
188 | |
189 | // Read the section names. | |
190 | const unsigned char* pshdrs = sd->section_headers->data(); | |
191 | const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size; | |
192 | typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames); | |
193 | ||
194 | if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB) | |
75f2446e ILT |
195 | this->error(_("section name section has wrong type: %u"), |
196 | static_cast<unsigned int>(shdrnames.get_sh_type())); | |
dbe717ef | 197 | |
8383303e ILT |
198 | sd->section_names_size = |
199 | convert_to_section_size_type(shdrnames.get_sh_size()); | |
dbe717ef | 200 | sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(), |
39d0cb0e ILT |
201 | sd->section_names_size, false, |
202 | false); | |
dbe717ef ILT |
203 | } |
204 | ||
205 | // If NAME is the name of a special .gnu.warning section, arrange for | |
206 | // the warning to be issued. SHNDX is the section index. Return | |
207 | // whether it is a warning section. | |
208 | ||
209 | bool | |
210 | Object::handle_gnu_warning_section(const char* name, unsigned int shndx, | |
211 | Symbol_table* symtab) | |
212 | { | |
213 | const char warn_prefix[] = ".gnu.warning."; | |
214 | const int warn_prefix_len = sizeof warn_prefix - 1; | |
215 | if (strncmp(name, warn_prefix, warn_prefix_len) == 0) | |
216 | { | |
cb295612 ILT |
217 | // Read the section contents to get the warning text. It would |
218 | // be nicer if we only did this if we have to actually issue a | |
219 | // warning. Unfortunately, warnings are issued as we relocate | |
220 | // sections. That means that we can not lock the object then, | |
221 | // as we might try to issue the same warning multiple times | |
222 | // simultaneously. | |
223 | section_size_type len; | |
224 | const unsigned char* contents = this->section_contents(shndx, &len, | |
225 | false); | |
226 | std::string warning(reinterpret_cast<const char*>(contents), len); | |
227 | symtab->add_warning(name + warn_prefix_len, this, warning); | |
dbe717ef ILT |
228 | return true; |
229 | } | |
230 | return false; | |
231 | } | |
232 | ||
f6ce93d6 | 233 | // Class Sized_relobj. |
bae7f79e ILT |
234 | |
235 | template<int size, bool big_endian> | |
f6ce93d6 | 236 | Sized_relobj<size, big_endian>::Sized_relobj( |
bae7f79e ILT |
237 | const std::string& name, |
238 | Input_file* input_file, | |
239 | off_t offset, | |
240 | const elfcpp::Ehdr<size, big_endian>& ehdr) | |
f6ce93d6 | 241 | : Relobj(name, input_file, offset), |
645f8123 | 242 | elf_file_(this, ehdr), |
dbe717ef | 243 | symtab_shndx_(-1U), |
61ba1cf9 ILT |
244 | local_symbol_count_(0), |
245 | output_local_symbol_count_(0), | |
7bf1f802 | 246 | output_local_dynsym_count_(0), |
730cdc88 | 247 | symbols_(), |
61ba1cf9 | 248 | local_symbol_offset_(0), |
7bf1f802 | 249 | local_dynsym_offset_(0), |
e727fa71 | 250 | local_values_(), |
730cdc88 ILT |
251 | local_got_offsets_(), |
252 | has_eh_frame_(false) | |
bae7f79e | 253 | { |
bae7f79e ILT |
254 | } |
255 | ||
256 | template<int size, bool big_endian> | |
f6ce93d6 | 257 | Sized_relobj<size, big_endian>::~Sized_relobj() |
bae7f79e ILT |
258 | { |
259 | } | |
260 | ||
645f8123 | 261 | // Set up an object file based on the file header. This sets up the |
bae7f79e ILT |
262 | // target and reads the section information. |
263 | ||
264 | template<int size, bool big_endian> | |
265 | void | |
f6ce93d6 | 266 | Sized_relobj<size, big_endian>::setup( |
bae7f79e ILT |
267 | const elfcpp::Ehdr<size, big_endian>& ehdr) |
268 | { | |
dbe717ef ILT |
269 | this->set_target(ehdr.get_e_machine(), size, big_endian, |
270 | ehdr.get_e_ident()[elfcpp::EI_OSABI], | |
271 | ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]); | |
12e14209 | 272 | |
dbe717ef | 273 | const unsigned int shnum = this->elf_file_.shnum(); |
a2fb1b05 | 274 | this->set_shnum(shnum); |
dbe717ef | 275 | } |
12e14209 | 276 | |
dbe717ef ILT |
277 | // Find the SHT_SYMTAB section, given the section headers. The ELF |
278 | // standard says that maybe in the future there can be more than one | |
279 | // SHT_SYMTAB section. Until somebody figures out how that could | |
280 | // work, we assume there is only one. | |
12e14209 | 281 | |
dbe717ef ILT |
282 | template<int size, bool big_endian> |
283 | void | |
284 | Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs) | |
285 | { | |
286 | const unsigned int shnum = this->shnum(); | |
287 | this->symtab_shndx_ = 0; | |
288 | if (shnum > 0) | |
bae7f79e | 289 | { |
dbe717ef ILT |
290 | // Look through the sections in reverse order, since gas tends |
291 | // to put the symbol table at the end. | |
292 | const unsigned char* p = pshdrs + shnum * This::shdr_size; | |
293 | unsigned int i = shnum; | |
d491d34e ILT |
294 | unsigned int xindex_shndx = 0; |
295 | unsigned int xindex_link = 0; | |
dbe717ef | 296 | while (i > 0) |
bae7f79e | 297 | { |
dbe717ef ILT |
298 | --i; |
299 | p -= This::shdr_size; | |
300 | typename This::Shdr shdr(p); | |
301 | if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB) | |
302 | { | |
303 | this->symtab_shndx_ = i; | |
d491d34e ILT |
304 | if (xindex_shndx > 0 && xindex_link == i) |
305 | { | |
306 | Xindex* xindex = | |
307 | new Xindex(this->elf_file_.large_shndx_offset()); | |
308 | xindex->read_symtab_xindex<size, big_endian>(this, | |
309 | xindex_shndx, | |
310 | pshdrs); | |
311 | this->set_xindex(xindex); | |
312 | } | |
dbe717ef ILT |
313 | break; |
314 | } | |
d491d34e ILT |
315 | |
316 | // Try to pick up the SHT_SYMTAB_SHNDX section, if there is | |
317 | // one. This will work if it follows the SHT_SYMTAB | |
318 | // section. | |
319 | if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB_SHNDX) | |
320 | { | |
321 | xindex_shndx = i; | |
322 | xindex_link = this->adjust_shndx(shdr.get_sh_link()); | |
323 | } | |
bae7f79e | 324 | } |
bae7f79e ILT |
325 | } |
326 | } | |
327 | ||
d491d34e ILT |
328 | // Return the Xindex structure to use for object with lots of |
329 | // sections. | |
330 | ||
331 | template<int size, bool big_endian> | |
332 | Xindex* | |
333 | Sized_relobj<size, big_endian>::do_initialize_xindex() | |
334 | { | |
335 | gold_assert(this->symtab_shndx_ != -1U); | |
336 | Xindex* xindex = new Xindex(this->elf_file_.large_shndx_offset()); | |
337 | xindex->initialize_symtab_xindex<size, big_endian>(this, this->symtab_shndx_); | |
338 | return xindex; | |
339 | } | |
340 | ||
730cdc88 ILT |
341 | // Return whether SHDR has the right type and flags to be a GNU |
342 | // .eh_frame section. | |
343 | ||
344 | template<int size, bool big_endian> | |
345 | bool | |
346 | Sized_relobj<size, big_endian>::check_eh_frame_flags( | |
347 | const elfcpp::Shdr<size, big_endian>* shdr) const | |
348 | { | |
2c38906f | 349 | return (shdr->get_sh_type() == elfcpp::SHT_PROGBITS |
1650c4ff | 350 | && (shdr->get_sh_flags() & elfcpp::SHF_ALLOC) != 0); |
730cdc88 ILT |
351 | } |
352 | ||
353 | // Return whether there is a GNU .eh_frame section, given the section | |
354 | // headers and the section names. | |
355 | ||
356 | template<int size, bool big_endian> | |
357 | bool | |
8383303e ILT |
358 | Sized_relobj<size, big_endian>::find_eh_frame( |
359 | const unsigned char* pshdrs, | |
360 | const char* names, | |
361 | section_size_type names_size) const | |
730cdc88 ILT |
362 | { |
363 | const unsigned int shnum = this->shnum(); | |
364 | const unsigned char* p = pshdrs + This::shdr_size; | |
365 | for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size) | |
366 | { | |
367 | typename This::Shdr shdr(p); | |
368 | if (this->check_eh_frame_flags(&shdr)) | |
369 | { | |
370 | if (shdr.get_sh_name() >= names_size) | |
371 | { | |
372 | this->error(_("bad section name offset for section %u: %lu"), | |
373 | i, static_cast<unsigned long>(shdr.get_sh_name())); | |
374 | continue; | |
375 | } | |
376 | ||
377 | const char* name = names + shdr.get_sh_name(); | |
378 | if (strcmp(name, ".eh_frame") == 0) | |
379 | return true; | |
380 | } | |
381 | } | |
382 | return false; | |
383 | } | |
384 | ||
12e14209 | 385 | // Read the sections and symbols from an object file. |
bae7f79e ILT |
386 | |
387 | template<int size, bool big_endian> | |
12e14209 | 388 | void |
f6ce93d6 | 389 | Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd) |
bae7f79e | 390 | { |
dbe717ef | 391 | this->read_section_data(&this->elf_file_, sd); |
12e14209 | 392 | |
dbe717ef ILT |
393 | const unsigned char* const pshdrs = sd->section_headers->data(); |
394 | ||
395 | this->find_symtab(pshdrs); | |
12e14209 | 396 | |
730cdc88 ILT |
397 | const unsigned char* namesu = sd->section_names->data(); |
398 | const char* names = reinterpret_cast<const char*>(namesu); | |
1650c4ff ILT |
399 | if (memmem(names, sd->section_names_size, ".eh_frame", 10) != NULL) |
400 | { | |
401 | if (this->find_eh_frame(pshdrs, names, sd->section_names_size)) | |
402 | this->has_eh_frame_ = true; | |
403 | } | |
730cdc88 | 404 | |
75f2446e ILT |
405 | sd->symbols = NULL; |
406 | sd->symbols_size = 0; | |
730cdc88 | 407 | sd->external_symbols_offset = 0; |
75f2446e ILT |
408 | sd->symbol_names = NULL; |
409 | sd->symbol_names_size = 0; | |
410 | ||
645f8123 | 411 | if (this->symtab_shndx_ == 0) |
bae7f79e ILT |
412 | { |
413 | // No symbol table. Weird but legal. | |
12e14209 | 414 | return; |
bae7f79e ILT |
415 | } |
416 | ||
12e14209 ILT |
417 | // Get the symbol table section header. |
418 | typename This::Shdr symtabshdr(pshdrs | |
645f8123 | 419 | + this->symtab_shndx_ * This::shdr_size); |
a3ad94ed | 420 | gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); |
bae7f79e | 421 | |
730cdc88 ILT |
422 | // If this object has a .eh_frame section, we need all the symbols. |
423 | // Otherwise we only need the external symbols. While it would be | |
424 | // simpler to just always read all the symbols, I've seen object | |
425 | // files with well over 2000 local symbols, which for a 64-bit | |
426 | // object file format is over 5 pages that we don't need to read | |
427 | // now. | |
428 | ||
75f65a3e | 429 | const int sym_size = This::sym_size; |
92e059d8 ILT |
430 | const unsigned int loccount = symtabshdr.get_sh_info(); |
431 | this->local_symbol_count_ = loccount; | |
7bf1f802 | 432 | this->local_values_.resize(loccount); |
8383303e | 433 | section_offset_type locsize = loccount * sym_size; |
730cdc88 | 434 | off_t dataoff = symtabshdr.get_sh_offset(); |
8383303e ILT |
435 | section_size_type datasize = |
436 | convert_to_section_size_type(symtabshdr.get_sh_size()); | |
730cdc88 | 437 | off_t extoff = dataoff + locsize; |
8383303e | 438 | section_size_type extsize = datasize - locsize; |
75f65a3e | 439 | |
730cdc88 | 440 | off_t readoff = this->has_eh_frame_ ? dataoff : extoff; |
8383303e | 441 | section_size_type readsize = this->has_eh_frame_ ? datasize : extsize; |
730cdc88 | 442 | |
39d0cb0e | 443 | File_view* fvsymtab = this->get_lasting_view(readoff, readsize, true, false); |
bae7f79e ILT |
444 | |
445 | // Read the section header for the symbol names. | |
d491d34e | 446 | unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link()); |
dbe717ef | 447 | if (strtab_shndx >= this->shnum()) |
bae7f79e | 448 | { |
75f2446e ILT |
449 | this->error(_("invalid symbol table name index: %u"), strtab_shndx); |
450 | return; | |
bae7f79e | 451 | } |
dbe717ef | 452 | typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size); |
bae7f79e ILT |
453 | if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB) |
454 | { | |
75f2446e ILT |
455 | this->error(_("symbol table name section has wrong type: %u"), |
456 | static_cast<unsigned int>(strtabshdr.get_sh_type())); | |
457 | return; | |
bae7f79e ILT |
458 | } |
459 | ||
460 | // Read the symbol names. | |
461 | File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(), | |
39d0cb0e ILT |
462 | strtabshdr.get_sh_size(), |
463 | false, true); | |
bae7f79e | 464 | |
12e14209 | 465 | sd->symbols = fvsymtab; |
730cdc88 ILT |
466 | sd->symbols_size = readsize; |
467 | sd->external_symbols_offset = this->has_eh_frame_ ? locsize : 0; | |
12e14209 | 468 | sd->symbol_names = fvstrtab; |
8383303e ILT |
469 | sd->symbol_names_size = |
470 | convert_to_section_size_type(strtabshdr.get_sh_size()); | |
a2fb1b05 ILT |
471 | } |
472 | ||
730cdc88 | 473 | // Return the section index of symbol SYM. Set *VALUE to its value in |
d491d34e ILT |
474 | // the object file. Set *IS_ORDINARY if this is an ordinary section |
475 | // index. not a special cod between SHN_LORESERVE and SHN_HIRESERVE. | |
476 | // Note that for a symbol which is not defined in this object file, | |
477 | // this will set *VALUE to 0 and return SHN_UNDEF; it will not return | |
478 | // the final value of the symbol in the link. | |
730cdc88 ILT |
479 | |
480 | template<int size, bool big_endian> | |
481 | unsigned int | |
482 | Sized_relobj<size, big_endian>::symbol_section_and_value(unsigned int sym, | |
d491d34e ILT |
483 | Address* value, |
484 | bool* is_ordinary) | |
730cdc88 | 485 | { |
8383303e | 486 | section_size_type symbols_size; |
730cdc88 ILT |
487 | const unsigned char* symbols = this->section_contents(this->symtab_shndx_, |
488 | &symbols_size, | |
489 | false); | |
490 | ||
491 | const size_t count = symbols_size / This::sym_size; | |
492 | gold_assert(sym < count); | |
493 | ||
494 | elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size); | |
495 | *value = elfsym.get_st_value(); | |
d491d34e ILT |
496 | |
497 | return this->adjust_sym_shndx(sym, elfsym.get_st_shndx(), is_ordinary); | |
730cdc88 ILT |
498 | } |
499 | ||
a2fb1b05 ILT |
500 | // Return whether to include a section group in the link. LAYOUT is |
501 | // used to keep track of which section groups we have already seen. | |
502 | // INDEX is the index of the section group and SHDR is the section | |
503 | // header. If we do not want to include this group, we set bits in | |
504 | // OMIT for each section which should be discarded. | |
505 | ||
506 | template<int size, bool big_endian> | |
507 | bool | |
f6ce93d6 | 508 | Sized_relobj<size, big_endian>::include_section_group( |
6a74a719 | 509 | Symbol_table* symtab, |
a2fb1b05 ILT |
510 | Layout* layout, |
511 | unsigned int index, | |
6a74a719 | 512 | const char* name, |
a2fb1b05 ILT |
513 | const elfcpp::Shdr<size, big_endian>& shdr, |
514 | std::vector<bool>* omit) | |
515 | { | |
516 | // Read the section contents. | |
517 | const unsigned char* pcon = this->get_view(shdr.get_sh_offset(), | |
39d0cb0e | 518 | shdr.get_sh_size(), true, false); |
a2fb1b05 ILT |
519 | const elfcpp::Elf_Word* pword = |
520 | reinterpret_cast<const elfcpp::Elf_Word*>(pcon); | |
521 | ||
522 | // The first word contains flags. We only care about COMDAT section | |
523 | // groups. Other section groups are always included in the link | |
524 | // just like ordinary sections. | |
f6ce93d6 | 525 | elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword); |
a2fb1b05 ILT |
526 | |
527 | // Look up the group signature, which is the name of a symbol. This | |
528 | // is a lot of effort to go to to read a string. Why didn't they | |
6a74a719 ILT |
529 | // just have the group signature point into the string table, rather |
530 | // than indirect through a symbol? | |
a2fb1b05 ILT |
531 | |
532 | // Get the appropriate symbol table header (this will normally be | |
533 | // the single SHT_SYMTAB section, but in principle it need not be). | |
d491d34e | 534 | const unsigned int link = this->adjust_shndx(shdr.get_sh_link()); |
645f8123 | 535 | typename This::Shdr symshdr(this, this->elf_file_.section_header(link)); |
a2fb1b05 ILT |
536 | |
537 | // Read the symbol table entry. | |
d491d34e ILT |
538 | unsigned int symndx = shdr.get_sh_info(); |
539 | if (symndx >= symshdr.get_sh_size() / This::sym_size) | |
a2fb1b05 | 540 | { |
75f2446e | 541 | this->error(_("section group %u info %u out of range"), |
d491d34e | 542 | index, symndx); |
75f2446e | 543 | return false; |
a2fb1b05 | 544 | } |
d491d34e | 545 | off_t symoff = symshdr.get_sh_offset() + symndx * This::sym_size; |
39d0cb0e ILT |
546 | const unsigned char* psym = this->get_view(symoff, This::sym_size, true, |
547 | false); | |
a2fb1b05 ILT |
548 | elfcpp::Sym<size, big_endian> sym(psym); |
549 | ||
a2fb1b05 | 550 | // Read the symbol table names. |
8383303e | 551 | section_size_type symnamelen; |
645f8123 | 552 | const unsigned char* psymnamesu; |
d491d34e ILT |
553 | psymnamesu = this->section_contents(this->adjust_shndx(symshdr.get_sh_link()), |
554 | &symnamelen, true); | |
a2fb1b05 ILT |
555 | const char* psymnames = reinterpret_cast<const char*>(psymnamesu); |
556 | ||
557 | // Get the section group signature. | |
645f8123 | 558 | if (sym.get_st_name() >= symnamelen) |
a2fb1b05 | 559 | { |
75f2446e | 560 | this->error(_("symbol %u name offset %u out of range"), |
d491d34e | 561 | symndx, sym.get_st_name()); |
75f2446e | 562 | return false; |
a2fb1b05 ILT |
563 | } |
564 | ||
565 | const char* signature = psymnames + sym.get_st_name(); | |
566 | ||
ead1e424 ILT |
567 | // It seems that some versions of gas will create a section group |
568 | // associated with a section symbol, and then fail to give a name to | |
569 | // the section symbol. In such a case, use the name of the section. | |
645f8123 ILT |
570 | std::string secname; |
571 | if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION) | |
ead1e424 | 572 | { |
d491d34e ILT |
573 | bool is_ordinary; |
574 | unsigned int sym_shndx = this->adjust_sym_shndx(symndx, | |
575 | sym.get_st_shndx(), | |
576 | &is_ordinary); | |
577 | if (!is_ordinary || sym_shndx >= this->shnum()) | |
578 | { | |
579 | this->error(_("symbol %u invalid section index %u"), | |
580 | symndx, sym_shndx); | |
581 | return false; | |
582 | } | |
583 | secname = this->section_name(sym_shndx); | |
645f8123 | 584 | signature = secname.c_str(); |
ead1e424 ILT |
585 | } |
586 | ||
a2fb1b05 ILT |
587 | // Record this section group, and see whether we've already seen one |
588 | // with the same signature. | |
6a74a719 ILT |
589 | |
590 | if ((flags & elfcpp::GRP_COMDAT) == 0 | |
591 | || layout->add_comdat(signature, true)) | |
592 | { | |
8851ecca | 593 | if (parameters->options().relocatable()) |
6a74a719 ILT |
594 | layout->layout_group(symtab, this, index, name, signature, shdr, |
595 | pword); | |
596 | return true; | |
597 | } | |
a2fb1b05 ILT |
598 | |
599 | // This is a duplicate. We want to discard the sections in this | |
600 | // group. | |
601 | size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word); | |
602 | for (size_t i = 1; i < count; ++i) | |
603 | { | |
f6ce93d6 ILT |
604 | elfcpp::Elf_Word secnum = |
605 | elfcpp::Swap<32, big_endian>::readval(pword + i); | |
a2fb1b05 ILT |
606 | if (secnum >= this->shnum()) |
607 | { | |
75f2446e ILT |
608 | this->error(_("section %u in section group %u out of range"), |
609 | secnum, index); | |
610 | continue; | |
a2fb1b05 | 611 | } |
55438702 ILT |
612 | |
613 | // Check for an earlier section number, since we're going to get | |
614 | // it wrong--we may have already decided to include the section. | |
615 | if (secnum < index) | |
616 | this->error(_("invalid section group %u refers to earlier section %u"), | |
617 | index, secnum); | |
618 | ||
a2fb1b05 ILT |
619 | (*omit)[secnum] = true; |
620 | } | |
621 | ||
622 | return false; | |
623 | } | |
624 | ||
625 | // Whether to include a linkonce section in the link. NAME is the | |
626 | // name of the section and SHDR is the section header. | |
627 | ||
628 | // Linkonce sections are a GNU extension implemented in the original | |
629 | // GNU linker before section groups were defined. The semantics are | |
630 | // that we only include one linkonce section with a given name. The | |
631 | // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME, | |
632 | // where T is the type of section and SYMNAME is the name of a symbol. | |
633 | // In an attempt to make linkonce sections interact well with section | |
634 | // groups, we try to identify SYMNAME and use it like a section group | |
635 | // signature. We want to block section groups with that signature, | |
636 | // but not other linkonce sections with that signature. We also use | |
637 | // the full name of the linkonce section as a normal section group | |
638 | // signature. | |
639 | ||
640 | template<int size, bool big_endian> | |
641 | bool | |
f6ce93d6 | 642 | Sized_relobj<size, big_endian>::include_linkonce_section( |
a2fb1b05 ILT |
643 | Layout* layout, |
644 | const char* name, | |
645 | const elfcpp::Shdr<size, big_endian>&) | |
646 | { | |
ad435a24 ILT |
647 | // In general the symbol name we want will be the string following |
648 | // the last '.'. However, we have to handle the case of | |
649 | // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by | |
650 | // some versions of gcc. So we use a heuristic: if the name starts | |
651 | // with ".gnu.linkonce.t.", we use everything after that. Otherwise | |
652 | // we look for the last '.'. We can't always simply skip | |
653 | // ".gnu.linkonce.X", because we have to deal with cases like | |
654 | // ".gnu.linkonce.d.rel.ro.local". | |
655 | const char* const linkonce_t = ".gnu.linkonce.t."; | |
656 | const char* symname; | |
657 | if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0) | |
658 | symname = name + strlen(linkonce_t); | |
659 | else | |
660 | symname = strrchr(name, '.') + 1; | |
a783673b ILT |
661 | bool include1 = layout->add_comdat(symname, false); |
662 | bool include2 = layout->add_comdat(name, true); | |
663 | return include1 && include2; | |
a2fb1b05 ILT |
664 | } |
665 | ||
666 | // Lay out the input sections. We walk through the sections and check | |
667 | // whether they should be included in the link. If they should, we | |
668 | // pass them to the Layout object, which will return an output section | |
669 | // and an offset. | |
670 | ||
671 | template<int size, bool big_endian> | |
672 | void | |
7e1edb90 | 673 | Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab, |
f6ce93d6 | 674 | Layout* layout, |
12e14209 | 675 | Read_symbols_data* sd) |
a2fb1b05 | 676 | { |
dbe717ef | 677 | const unsigned int shnum = this->shnum(); |
12e14209 ILT |
678 | if (shnum == 0) |
679 | return; | |
a2fb1b05 ILT |
680 | |
681 | // Get the section headers. | |
12e14209 | 682 | const unsigned char* pshdrs = sd->section_headers->data(); |
a2fb1b05 ILT |
683 | |
684 | // Get the section names. | |
12e14209 | 685 | const unsigned char* pnamesu = sd->section_names->data(); |
a2fb1b05 ILT |
686 | const char* pnames = reinterpret_cast<const char*>(pnamesu); |
687 | ||
730cdc88 ILT |
688 | // For each section, record the index of the reloc section if any. |
689 | // Use 0 to mean that there is no reloc section, -1U to mean that | |
690 | // there is more than one. | |
691 | std::vector<unsigned int> reloc_shndx(shnum, 0); | |
692 | std::vector<unsigned int> reloc_type(shnum, elfcpp::SHT_NULL); | |
693 | // Skip the first, dummy, section. | |
694 | pshdrs += This::shdr_size; | |
695 | for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size) | |
696 | { | |
697 | typename This::Shdr shdr(pshdrs); | |
698 | ||
699 | unsigned int sh_type = shdr.get_sh_type(); | |
700 | if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA) | |
701 | { | |
d491d34e | 702 | unsigned int target_shndx = this->adjust_shndx(shdr.get_sh_info()); |
730cdc88 ILT |
703 | if (target_shndx == 0 || target_shndx >= shnum) |
704 | { | |
705 | this->error(_("relocation section %u has bad info %u"), | |
706 | i, target_shndx); | |
707 | continue; | |
708 | } | |
709 | ||
710 | if (reloc_shndx[target_shndx] != 0) | |
711 | reloc_shndx[target_shndx] = -1U; | |
712 | else | |
713 | { | |
714 | reloc_shndx[target_shndx] = i; | |
715 | reloc_type[target_shndx] = sh_type; | |
716 | } | |
717 | } | |
718 | } | |
719 | ||
a2fb1b05 | 720 | std::vector<Map_to_output>& map_sections(this->map_to_output()); |
61ba1cf9 | 721 | map_sections.resize(shnum); |
a2fb1b05 | 722 | |
88dd47ac ILT |
723 | // If we are only linking for symbols, then there is nothing else to |
724 | // do here. | |
725 | if (this->input_file()->just_symbols()) | |
726 | { | |
727 | delete sd->section_headers; | |
728 | sd->section_headers = NULL; | |
729 | delete sd->section_names; | |
730 | sd->section_names = NULL; | |
731 | return; | |
732 | } | |
733 | ||
35cdfc9a ILT |
734 | // Whether we've seen a .note.GNU-stack section. |
735 | bool seen_gnu_stack = false; | |
736 | // The flags of a .note.GNU-stack section. | |
737 | uint64_t gnu_stack_flags = 0; | |
738 | ||
a2fb1b05 ILT |
739 | // Keep track of which sections to omit. |
740 | std::vector<bool> omit(shnum, false); | |
741 | ||
7019cd25 | 742 | // Keep track of reloc sections when emitting relocations. |
8851ecca ILT |
743 | const bool relocatable = parameters->options().relocatable(); |
744 | const bool emit_relocs = (relocatable | |
745 | || parameters->options().emit_relocs()); | |
6a74a719 ILT |
746 | std::vector<unsigned int> reloc_sections; |
747 | ||
730cdc88 ILT |
748 | // Keep track of .eh_frame sections. |
749 | std::vector<unsigned int> eh_frame_sections; | |
750 | ||
f6ce93d6 | 751 | // Skip the first, dummy, section. |
730cdc88 | 752 | pshdrs = sd->section_headers->data() + This::shdr_size; |
f6ce93d6 | 753 | for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size) |
a2fb1b05 | 754 | { |
75f65a3e | 755 | typename This::Shdr shdr(pshdrs); |
a2fb1b05 | 756 | |
12e14209 | 757 | if (shdr.get_sh_name() >= sd->section_names_size) |
a2fb1b05 | 758 | { |
75f2446e ILT |
759 | this->error(_("bad section name offset for section %u: %lu"), |
760 | i, static_cast<unsigned long>(shdr.get_sh_name())); | |
761 | return; | |
a2fb1b05 ILT |
762 | } |
763 | ||
764 | const char* name = pnames + shdr.get_sh_name(); | |
765 | ||
dbe717ef | 766 | if (this->handle_gnu_warning_section(name, i, symtab)) |
f6ce93d6 | 767 | { |
8851ecca | 768 | if (!relocatable) |
f6ce93d6 ILT |
769 | omit[i] = true; |
770 | } | |
771 | ||
35cdfc9a ILT |
772 | // The .note.GNU-stack section is special. It gives the |
773 | // protection flags that this object file requires for the stack | |
774 | // in memory. | |
775 | if (strcmp(name, ".note.GNU-stack") == 0) | |
776 | { | |
777 | seen_gnu_stack = true; | |
778 | gnu_stack_flags |= shdr.get_sh_flags(); | |
779 | omit[i] = true; | |
780 | } | |
781 | ||
a2fb1b05 ILT |
782 | bool discard = omit[i]; |
783 | if (!discard) | |
784 | { | |
785 | if (shdr.get_sh_type() == elfcpp::SHT_GROUP) | |
786 | { | |
6a74a719 ILT |
787 | if (!this->include_section_group(symtab, layout, i, name, shdr, |
788 | &omit)) | |
a2fb1b05 ILT |
789 | discard = true; |
790 | } | |
cba134d6 ILT |
791 | else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0 |
792 | && Layout::is_linkonce(name)) | |
a2fb1b05 ILT |
793 | { |
794 | if (!this->include_linkonce_section(layout, name, shdr)) | |
795 | discard = true; | |
796 | } | |
797 | } | |
798 | ||
799 | if (discard) | |
800 | { | |
801 | // Do not include this section in the link. | |
802 | map_sections[i].output_section = NULL; | |
803 | continue; | |
804 | } | |
805 | ||
6a74a719 ILT |
806 | // When doing a relocatable link we are going to copy input |
807 | // reloc sections into the output. We only want to copy the | |
808 | // ones associated with sections which are not being discarded. | |
809 | // However, we don't know that yet for all sections. So save | |
810 | // reloc sections and process them later. | |
7019cd25 | 811 | if (emit_relocs |
6a74a719 ILT |
812 | && (shdr.get_sh_type() == elfcpp::SHT_REL |
813 | || shdr.get_sh_type() == elfcpp::SHT_RELA)) | |
814 | { | |
815 | reloc_sections.push_back(i); | |
816 | continue; | |
817 | } | |
818 | ||
8851ecca | 819 | if (relocatable && shdr.get_sh_type() == elfcpp::SHT_GROUP) |
6a74a719 ILT |
820 | continue; |
821 | ||
730cdc88 ILT |
822 | // The .eh_frame section is special. It holds exception frame |
823 | // information that we need to read in order to generate the | |
824 | // exception frame header. We process these after all the other | |
825 | // sections so that the exception frame reader can reliably | |
826 | // determine which sections are being discarded, and discard the | |
827 | // corresponding information. | |
8851ecca | 828 | if (!relocatable |
730cdc88 ILT |
829 | && strcmp(name, ".eh_frame") == 0 |
830 | && this->check_eh_frame_flags(&shdr)) | |
831 | { | |
832 | eh_frame_sections.push_back(i); | |
833 | continue; | |
834 | } | |
835 | ||
a2fb1b05 | 836 | off_t offset; |
730cdc88 ILT |
837 | Output_section* os = layout->layout(this, i, name, shdr, |
838 | reloc_shndx[i], reloc_type[i], | |
839 | &offset); | |
a2fb1b05 ILT |
840 | |
841 | map_sections[i].output_section = os; | |
842 | map_sections[i].offset = offset; | |
730cdc88 ILT |
843 | |
844 | // If this section requires special handling, and if there are | |
845 | // relocs that apply to it, then we must do the special handling | |
846 | // before we apply the relocs. | |
847 | if (offset == -1 && reloc_shndx[i] != 0) | |
848 | this->set_relocs_must_follow_section_writes(); | |
12e14209 ILT |
849 | } |
850 | ||
35cdfc9a ILT |
851 | layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags); |
852 | ||
6a74a719 ILT |
853 | // When doing a relocatable link handle the reloc sections at the |
854 | // end. | |
7019cd25 | 855 | if (emit_relocs) |
6a74a719 ILT |
856 | this->size_relocatable_relocs(); |
857 | for (std::vector<unsigned int>::const_iterator p = reloc_sections.begin(); | |
858 | p != reloc_sections.end(); | |
859 | ++p) | |
860 | { | |
861 | unsigned int i = *p; | |
862 | const unsigned char* pshdr; | |
863 | pshdr = sd->section_headers->data() + i * This::shdr_size; | |
864 | typename This::Shdr shdr(pshdr); | |
865 | ||
d491d34e | 866 | unsigned int data_shndx = this->adjust_shndx(shdr.get_sh_info()); |
6a74a719 ILT |
867 | if (data_shndx >= shnum) |
868 | { | |
869 | // We already warned about this above. | |
870 | continue; | |
871 | } | |
872 | ||
873 | Output_section* data_section = map_sections[data_shndx].output_section; | |
874 | if (data_section == NULL) | |
875 | { | |
876 | map_sections[i].output_section = NULL; | |
877 | continue; | |
878 | } | |
879 | ||
880 | Relocatable_relocs* rr = new Relocatable_relocs(); | |
881 | this->set_relocatable_relocs(i, rr); | |
882 | ||
883 | Output_section* os = layout->layout_reloc(this, i, shdr, data_section, | |
884 | rr); | |
885 | map_sections[i].output_section = os; | |
886 | map_sections[i].offset = -1; | |
887 | } | |
888 | ||
730cdc88 ILT |
889 | // Handle the .eh_frame sections at the end. |
890 | for (std::vector<unsigned int>::const_iterator p = eh_frame_sections.begin(); | |
891 | p != eh_frame_sections.end(); | |
892 | ++p) | |
893 | { | |
894 | gold_assert(this->has_eh_frame_); | |
895 | gold_assert(sd->external_symbols_offset != 0); | |
896 | ||
897 | unsigned int i = *p; | |
898 | const unsigned char *pshdr; | |
899 | pshdr = sd->section_headers->data() + i * This::shdr_size; | |
900 | typename This::Shdr shdr(pshdr); | |
901 | ||
902 | off_t offset; | |
903 | Output_section* os = layout->layout_eh_frame(this, | |
904 | sd->symbols->data(), | |
905 | sd->symbols_size, | |
906 | sd->symbol_names->data(), | |
907 | sd->symbol_names_size, | |
908 | i, shdr, | |
909 | reloc_shndx[i], | |
910 | reloc_type[i], | |
911 | &offset); | |
912 | map_sections[i].output_section = os; | |
913 | map_sections[i].offset = offset; | |
914 | ||
915 | // If this section requires special handling, and if there are | |
916 | // relocs that apply to it, then we must do the special handling | |
917 | // before we apply the relocs. | |
918 | if (offset == -1 && reloc_shndx[i] != 0) | |
919 | this->set_relocs_must_follow_section_writes(); | |
920 | } | |
921 | ||
12e14209 ILT |
922 | delete sd->section_headers; |
923 | sd->section_headers = NULL; | |
924 | delete sd->section_names; | |
925 | sd->section_names = NULL; | |
926 | } | |
927 | ||
928 | // Add the symbols to the symbol table. | |
929 | ||
930 | template<int size, bool big_endian> | |
931 | void | |
f6ce93d6 | 932 | Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab, |
12e14209 ILT |
933 | Read_symbols_data* sd) |
934 | { | |
935 | if (sd->symbols == NULL) | |
936 | { | |
a3ad94ed | 937 | gold_assert(sd->symbol_names == NULL); |
12e14209 ILT |
938 | return; |
939 | } | |
a2fb1b05 | 940 | |
12e14209 | 941 | const int sym_size = This::sym_size; |
730cdc88 ILT |
942 | size_t symcount = ((sd->symbols_size - sd->external_symbols_offset) |
943 | / sym_size); | |
8383303e | 944 | if (symcount * sym_size != sd->symbols_size - sd->external_symbols_offset) |
12e14209 | 945 | { |
75f2446e ILT |
946 | this->error(_("size of symbols is not multiple of symbol size")); |
947 | return; | |
a2fb1b05 | 948 | } |
12e14209 | 949 | |
730cdc88 | 950 | this->symbols_.resize(symcount); |
12e14209 | 951 | |
12e14209 ILT |
952 | const char* sym_names = |
953 | reinterpret_cast<const char*>(sd->symbol_names->data()); | |
730cdc88 ILT |
954 | symtab->add_from_relobj(this, |
955 | sd->symbols->data() + sd->external_symbols_offset, | |
7fcd3aa9 | 956 | symcount, this->local_symbol_count_, |
d491d34e | 957 | sym_names, sd->symbol_names_size, |
730cdc88 | 958 | &this->symbols_); |
12e14209 ILT |
959 | |
960 | delete sd->symbols; | |
961 | sd->symbols = NULL; | |
962 | delete sd->symbol_names; | |
963 | sd->symbol_names = NULL; | |
bae7f79e ILT |
964 | } |
965 | ||
cb295612 ILT |
966 | // First pass over the local symbols. Here we add their names to |
967 | // *POOL and *DYNPOOL, and we store the symbol value in | |
968 | // THIS->LOCAL_VALUES_. This function is always called from a | |
969 | // singleton thread. This is followed by a call to | |
970 | // finalize_local_symbols. | |
75f65a3e ILT |
971 | |
972 | template<int size, bool big_endian> | |
7bf1f802 ILT |
973 | void |
974 | Sized_relobj<size, big_endian>::do_count_local_symbols(Stringpool* pool, | |
975 | Stringpool* dynpool) | |
75f65a3e | 976 | { |
a3ad94ed | 977 | gold_assert(this->symtab_shndx_ != -1U); |
645f8123 | 978 | if (this->symtab_shndx_ == 0) |
61ba1cf9 ILT |
979 | { |
980 | // This object has no symbols. Weird but legal. | |
7bf1f802 | 981 | return; |
61ba1cf9 ILT |
982 | } |
983 | ||
75f65a3e | 984 | // Read the symbol table section header. |
645f8123 ILT |
985 | const unsigned int symtab_shndx = this->symtab_shndx_; |
986 | typename This::Shdr symtabshdr(this, | |
987 | this->elf_file_.section_header(symtab_shndx)); | |
a3ad94ed | 988 | gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); |
75f65a3e ILT |
989 | |
990 | // Read the local symbols. | |
75f65a3e | 991 | const int sym_size = This::sym_size; |
92e059d8 | 992 | const unsigned int loccount = this->local_symbol_count_; |
a3ad94ed | 993 | gold_assert(loccount == symtabshdr.get_sh_info()); |
75f65a3e ILT |
994 | off_t locsize = loccount * sym_size; |
995 | const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(), | |
39d0cb0e | 996 | locsize, true, true); |
75f65a3e | 997 | |
75f65a3e | 998 | // Read the symbol names. |
d491d34e ILT |
999 | const unsigned int strtab_shndx = |
1000 | this->adjust_shndx(symtabshdr.get_sh_link()); | |
8383303e | 1001 | section_size_type strtab_size; |
645f8123 | 1002 | const unsigned char* pnamesu = this->section_contents(strtab_shndx, |
9eb9fa57 ILT |
1003 | &strtab_size, |
1004 | true); | |
75f65a3e ILT |
1005 | const char* pnames = reinterpret_cast<const char*>(pnamesu); |
1006 | ||
1007 | // Loop over the local symbols. | |
1008 | ||
c06b7b0b | 1009 | const std::vector<Map_to_output>& mo(this->map_to_output()); |
75f65a3e | 1010 | unsigned int shnum = this->shnum(); |
61ba1cf9 | 1011 | unsigned int count = 0; |
7bf1f802 | 1012 | unsigned int dyncount = 0; |
75f65a3e ILT |
1013 | // Skip the first, dummy, symbol. |
1014 | psyms += sym_size; | |
61ba1cf9 | 1015 | for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size) |
75f65a3e ILT |
1016 | { |
1017 | elfcpp::Sym<size, big_endian> sym(psyms); | |
1018 | ||
b8e6aad9 ILT |
1019 | Symbol_value<size>& lv(this->local_values_[i]); |
1020 | ||
d491d34e ILT |
1021 | bool is_ordinary; |
1022 | unsigned int shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(), | |
1023 | &is_ordinary); | |
1024 | lv.set_input_shndx(shndx, is_ordinary); | |
75f65a3e | 1025 | |
063f12a8 ILT |
1026 | if (sym.get_st_type() == elfcpp::STT_SECTION) |
1027 | lv.set_is_section_symbol(); | |
7bf1f802 ILT |
1028 | else if (sym.get_st_type() == elfcpp::STT_TLS) |
1029 | lv.set_is_tls_symbol(); | |
1030 | ||
1031 | // Save the input symbol value for use in do_finalize_local_symbols(). | |
1032 | lv.set_input_value(sym.get_st_value()); | |
1033 | ||
1034 | // Decide whether this symbol should go into the output file. | |
063f12a8 | 1035 | |
7bf1f802 ILT |
1036 | if (shndx < shnum && mo[shndx].output_section == NULL) |
1037 | { | |
1038 | lv.set_no_output_symtab_entry(); | |
dceae3c1 | 1039 | gold_assert(!lv.needs_output_dynsym_entry()); |
7bf1f802 ILT |
1040 | continue; |
1041 | } | |
1042 | ||
1043 | if (sym.get_st_type() == elfcpp::STT_SECTION) | |
1044 | { | |
1045 | lv.set_no_output_symtab_entry(); | |
dceae3c1 | 1046 | gold_assert(!lv.needs_output_dynsym_entry()); |
7bf1f802 ILT |
1047 | continue; |
1048 | } | |
1049 | ||
1050 | if (sym.get_st_name() >= strtab_size) | |
1051 | { | |
1052 | this->error(_("local symbol %u section name out of range: %u >= %u"), | |
1053 | i, sym.get_st_name(), | |
1054 | static_cast<unsigned int>(strtab_size)); | |
1055 | lv.set_no_output_symtab_entry(); | |
1056 | continue; | |
1057 | } | |
1058 | ||
1059 | // Add the symbol to the symbol table string pool. | |
1060 | const char* name = pnames + sym.get_st_name(); | |
1061 | pool->add(name, true, NULL); | |
1062 | ++count; | |
1063 | ||
1064 | // If needed, add the symbol to the dynamic symbol table string pool. | |
1065 | if (lv.needs_output_dynsym_entry()) | |
1066 | { | |
1067 | dynpool->add(name, true, NULL); | |
1068 | ++dyncount; | |
1069 | } | |
1070 | } | |
1071 | ||
1072 | this->output_local_symbol_count_ = count; | |
1073 | this->output_local_dynsym_count_ = dyncount; | |
1074 | } | |
1075 | ||
cb295612 | 1076 | // Finalize the local symbols. Here we set the final value in |
7bf1f802 | 1077 | // THIS->LOCAL_VALUES_ and set their output symbol table indexes. |
17a1d0a9 | 1078 | // This function is always called from a singleton thread. The actual |
7bf1f802 ILT |
1079 | // output of the local symbols will occur in a separate task. |
1080 | ||
1081 | template<int size, bool big_endian> | |
1082 | unsigned int | |
1083 | Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index, | |
d491d34e | 1084 | off_t off) |
7bf1f802 ILT |
1085 | { |
1086 | gold_assert(off == static_cast<off_t>(align_address(off, size >> 3))); | |
1087 | ||
1088 | const unsigned int loccount = this->local_symbol_count_; | |
1089 | this->local_symbol_offset_ = off; | |
1090 | ||
1091 | const std::vector<Map_to_output>& mo(this->map_to_output()); | |
1092 | unsigned int shnum = this->shnum(); | |
1093 | ||
1094 | for (unsigned int i = 1; i < loccount; ++i) | |
1095 | { | |
1096 | Symbol_value<size>& lv(this->local_values_[i]); | |
1097 | ||
d491d34e ILT |
1098 | bool is_ordinary; |
1099 | unsigned int shndx = lv.input_shndx(&is_ordinary); | |
7bf1f802 ILT |
1100 | |
1101 | // Set the output symbol value. | |
1102 | ||
d491d34e | 1103 | if (!is_ordinary) |
75f65a3e | 1104 | { |
0dfbdef4 | 1105 | if (shndx == elfcpp::SHN_ABS || shndx == elfcpp::SHN_COMMON) |
7bf1f802 | 1106 | lv.set_output_value(lv.input_value()); |
61ba1cf9 | 1107 | else |
75f65a3e | 1108 | { |
75f2446e ILT |
1109 | this->error(_("unknown section index %u for local symbol %u"), |
1110 | shndx, i); | |
1111 | lv.set_output_value(0); | |
75f65a3e | 1112 | } |
75f65a3e ILT |
1113 | } |
1114 | else | |
1115 | { | |
1116 | if (shndx >= shnum) | |
1117 | { | |
75f2446e ILT |
1118 | this->error(_("local symbol %u section index %u out of range"), |
1119 | i, shndx); | |
1120 | shndx = 0; | |
75f65a3e ILT |
1121 | } |
1122 | ||
b8e6aad9 ILT |
1123 | Output_section* os = mo[shndx].output_section; |
1124 | ||
1125 | if (os == NULL) | |
61ba1cf9 | 1126 | { |
b8e6aad9 | 1127 | lv.set_output_value(0); |
61ba1cf9 ILT |
1128 | continue; |
1129 | } | |
7bf1f802 ILT |
1130 | else if (mo[shndx].offset == -1) |
1131 | { | |
a9a60db6 ILT |
1132 | // This is a SHF_MERGE section or one which otherwise |
1133 | // requires special handling. We get the output address | |
1134 | // of the start of the merged section. If this is not a | |
1135 | // section symbol, we can then determine the final | |
1136 | // value. If it is a section symbol, we can not, as in | |
1137 | // that case we have to consider the addend to determine | |
1138 | // the value to use in a relocation. | |
a9a60db6 | 1139 | if (!lv.is_section_symbol()) |
8d32f935 ILT |
1140 | lv.set_output_value(os->output_address(this, shndx, |
1141 | lv.input_value())); | |
a9a60db6 ILT |
1142 | else |
1143 | { | |
8d32f935 ILT |
1144 | section_offset_type start = |
1145 | os->starting_output_address(this, shndx); | |
a9a60db6 ILT |
1146 | Merged_symbol_value<size>* msv = |
1147 | new Merged_symbol_value<size>(lv.input_value(), start); | |
1148 | lv.set_merged_symbol_value(msv); | |
1149 | } | |
7bf1f802 ILT |
1150 | } |
1151 | else if (lv.is_tls_symbol()) | |
a9a60db6 | 1152 | lv.set_output_value(os->tls_offset() |
7bf1f802 ILT |
1153 | + mo[shndx].offset |
1154 | + lv.input_value()); | |
b8e6aad9 | 1155 | else |
a9a60db6 | 1156 | lv.set_output_value(os->address() |
b8e6aad9 | 1157 | + mo[shndx].offset |
7bf1f802 | 1158 | + lv.input_value()); |
75f65a3e ILT |
1159 | } |
1160 | ||
7bf1f802 ILT |
1161 | if (lv.needs_output_symtab_entry()) |
1162 | { | |
1163 | lv.set_output_symtab_index(index); | |
1164 | ++index; | |
1165 | } | |
1166 | } | |
1167 | return index; | |
1168 | } | |
645f8123 | 1169 | |
7bf1f802 | 1170 | // Set the output dynamic symbol table indexes for the local variables. |
c06b7b0b | 1171 | |
7bf1f802 ILT |
1172 | template<int size, bool big_endian> |
1173 | unsigned int | |
1174 | Sized_relobj<size, big_endian>::do_set_local_dynsym_indexes(unsigned int index) | |
1175 | { | |
1176 | const unsigned int loccount = this->local_symbol_count_; | |
1177 | for (unsigned int i = 1; i < loccount; ++i) | |
1178 | { | |
1179 | Symbol_value<size>& lv(this->local_values_[i]); | |
1180 | if (lv.needs_output_dynsym_entry()) | |
1181 | { | |
1182 | lv.set_output_dynsym_index(index); | |
1183 | ++index; | |
1184 | } | |
75f65a3e | 1185 | } |
7bf1f802 ILT |
1186 | return index; |
1187 | } | |
75f65a3e | 1188 | |
7bf1f802 ILT |
1189 | // Set the offset where local dynamic symbol information will be stored. |
1190 | // Returns the count of local symbols contributed to the symbol table by | |
1191 | // this object. | |
61ba1cf9 | 1192 | |
7bf1f802 ILT |
1193 | template<int size, bool big_endian> |
1194 | unsigned int | |
1195 | Sized_relobj<size, big_endian>::do_set_local_dynsym_offset(off_t off) | |
1196 | { | |
1197 | gold_assert(off == static_cast<off_t>(align_address(off, size >> 3))); | |
1198 | this->local_dynsym_offset_ = off; | |
1199 | return this->output_local_dynsym_count_; | |
75f65a3e ILT |
1200 | } |
1201 | ||
61ba1cf9 ILT |
1202 | // Write out the local symbols. |
1203 | ||
1204 | template<int size, bool big_endian> | |
1205 | void | |
17a1d0a9 ILT |
1206 | Sized_relobj<size, big_endian>::write_local_symbols( |
1207 | Output_file* of, | |
1208 | const Stringpool* sympool, | |
d491d34e ILT |
1209 | const Stringpool* dynpool, |
1210 | Output_symtab_xindex* symtab_xindex, | |
1211 | Output_symtab_xindex* dynsym_xindex) | |
61ba1cf9 | 1212 | { |
8851ecca ILT |
1213 | if (parameters->options().strip_all() |
1214 | && this->output_local_dynsym_count_ == 0) | |
9e2dcb77 ILT |
1215 | return; |
1216 | ||
a3ad94ed | 1217 | gold_assert(this->symtab_shndx_ != -1U); |
645f8123 | 1218 | if (this->symtab_shndx_ == 0) |
61ba1cf9 ILT |
1219 | { |
1220 | // This object has no symbols. Weird but legal. | |
1221 | return; | |
1222 | } | |
1223 | ||
1224 | // Read the symbol table section header. | |
645f8123 ILT |
1225 | const unsigned int symtab_shndx = this->symtab_shndx_; |
1226 | typename This::Shdr symtabshdr(this, | |
1227 | this->elf_file_.section_header(symtab_shndx)); | |
a3ad94ed | 1228 | gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); |
92e059d8 | 1229 | const unsigned int loccount = this->local_symbol_count_; |
a3ad94ed | 1230 | gold_assert(loccount == symtabshdr.get_sh_info()); |
61ba1cf9 ILT |
1231 | |
1232 | // Read the local symbols. | |
1233 | const int sym_size = This::sym_size; | |
92e059d8 | 1234 | off_t locsize = loccount * sym_size; |
61ba1cf9 | 1235 | const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(), |
39d0cb0e | 1236 | locsize, true, false); |
61ba1cf9 | 1237 | |
61ba1cf9 | 1238 | // Read the symbol names. |
d491d34e ILT |
1239 | const unsigned int strtab_shndx = |
1240 | this->adjust_shndx(symtabshdr.get_sh_link()); | |
8383303e | 1241 | section_size_type strtab_size; |
645f8123 | 1242 | const unsigned char* pnamesu = this->section_contents(strtab_shndx, |
9eb9fa57 | 1243 | &strtab_size, |
cb295612 | 1244 | false); |
61ba1cf9 ILT |
1245 | const char* pnames = reinterpret_cast<const char*>(pnamesu); |
1246 | ||
7bf1f802 ILT |
1247 | // Get views into the output file for the portions of the symbol table |
1248 | // and the dynamic symbol table that we will be writing. | |
61ba1cf9 | 1249 | off_t output_size = this->output_local_symbol_count_ * sym_size; |
f2619d6c | 1250 | unsigned char* oview = NULL; |
7bf1f802 ILT |
1251 | if (output_size > 0) |
1252 | oview = of->get_output_view(this->local_symbol_offset_, output_size); | |
1253 | ||
1254 | off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size; | |
1255 | unsigned char* dyn_oview = NULL; | |
1256 | if (dyn_output_size > 0) | |
1257 | dyn_oview = of->get_output_view(this->local_dynsym_offset_, | |
1258 | dyn_output_size); | |
61ba1cf9 | 1259 | |
c06b7b0b ILT |
1260 | const std::vector<Map_to_output>& mo(this->map_to_output()); |
1261 | ||
a3ad94ed | 1262 | gold_assert(this->local_values_.size() == loccount); |
61ba1cf9 | 1263 | |
61ba1cf9 | 1264 | unsigned char* ov = oview; |
7bf1f802 | 1265 | unsigned char* dyn_ov = dyn_oview; |
c06b7b0b | 1266 | psyms += sym_size; |
92e059d8 | 1267 | for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size) |
61ba1cf9 ILT |
1268 | { |
1269 | elfcpp::Sym<size, big_endian> isym(psyms); | |
f6ce93d6 | 1270 | |
d491d34e ILT |
1271 | Symbol_value<size>& lv(this->local_values_[i]); |
1272 | ||
1273 | bool is_ordinary; | |
1274 | unsigned int st_shndx = this->adjust_sym_shndx(i, isym.get_st_shndx(), | |
1275 | &is_ordinary); | |
1276 | if (is_ordinary) | |
61ba1cf9 | 1277 | { |
a3ad94ed | 1278 | gold_assert(st_shndx < mo.size()); |
61ba1cf9 ILT |
1279 | if (mo[st_shndx].output_section == NULL) |
1280 | continue; | |
ead1e424 | 1281 | st_shndx = mo[st_shndx].output_section->out_shndx(); |
d491d34e ILT |
1282 | if (st_shndx >= elfcpp::SHN_LORESERVE) |
1283 | { | |
1284 | if (lv.needs_output_symtab_entry()) | |
1285 | symtab_xindex->add(lv.output_symtab_index(), st_shndx); | |
1286 | if (lv.needs_output_dynsym_entry()) | |
1287 | dynsym_xindex->add(lv.output_dynsym_index(), st_shndx); | |
1288 | st_shndx = elfcpp::SHN_XINDEX; | |
1289 | } | |
61ba1cf9 ILT |
1290 | } |
1291 | ||
7bf1f802 | 1292 | // Write the symbol to the output symbol table. |
8851ecca | 1293 | if (!parameters->options().strip_all() |
d491d34e | 1294 | && lv.needs_output_symtab_entry()) |
7bf1f802 ILT |
1295 | { |
1296 | elfcpp::Sym_write<size, big_endian> osym(ov); | |
1297 | ||
1298 | gold_assert(isym.get_st_name() < strtab_size); | |
1299 | const char* name = pnames + isym.get_st_name(); | |
1300 | osym.put_st_name(sympool->get_offset(name)); | |
1301 | osym.put_st_value(this->local_values_[i].value(this, 0)); | |
1302 | osym.put_st_size(isym.get_st_size()); | |
1303 | osym.put_st_info(isym.get_st_info()); | |
1304 | osym.put_st_other(isym.get_st_other()); | |
1305 | osym.put_st_shndx(st_shndx); | |
1306 | ||
1307 | ov += sym_size; | |
1308 | } | |
1309 | ||
1310 | // Write the symbol to the output dynamic symbol table. | |
d491d34e | 1311 | if (lv.needs_output_dynsym_entry()) |
7bf1f802 ILT |
1312 | { |
1313 | gold_assert(dyn_ov < dyn_oview + dyn_output_size); | |
1314 | elfcpp::Sym_write<size, big_endian> osym(dyn_ov); | |
1315 | ||
1316 | gold_assert(isym.get_st_name() < strtab_size); | |
1317 | const char* name = pnames + isym.get_st_name(); | |
1318 | osym.put_st_name(dynpool->get_offset(name)); | |
1319 | osym.put_st_value(this->local_values_[i].value(this, 0)); | |
1320 | osym.put_st_size(isym.get_st_size()); | |
1321 | osym.put_st_info(isym.get_st_info()); | |
1322 | osym.put_st_other(isym.get_st_other()); | |
1323 | osym.put_st_shndx(st_shndx); | |
1324 | ||
1325 | dyn_ov += sym_size; | |
1326 | } | |
1327 | } | |
f6ce93d6 | 1328 | |
61ba1cf9 | 1329 | |
7bf1f802 ILT |
1330 | if (output_size > 0) |
1331 | { | |
1332 | gold_assert(ov - oview == output_size); | |
1333 | of->write_output_view(this->local_symbol_offset_, output_size, oview); | |
61ba1cf9 ILT |
1334 | } |
1335 | ||
7bf1f802 ILT |
1336 | if (dyn_output_size > 0) |
1337 | { | |
1338 | gold_assert(dyn_ov - dyn_oview == dyn_output_size); | |
1339 | of->write_output_view(this->local_dynsym_offset_, dyn_output_size, | |
1340 | dyn_oview); | |
1341 | } | |
61ba1cf9 ILT |
1342 | } |
1343 | ||
f7e2ee48 ILT |
1344 | // Set *INFO to symbolic information about the offset OFFSET in the |
1345 | // section SHNDX. Return true if we found something, false if we | |
1346 | // found nothing. | |
1347 | ||
1348 | template<int size, bool big_endian> | |
1349 | bool | |
1350 | Sized_relobj<size, big_endian>::get_symbol_location_info( | |
1351 | unsigned int shndx, | |
1352 | off_t offset, | |
1353 | Symbol_location_info* info) | |
1354 | { | |
1355 | if (this->symtab_shndx_ == 0) | |
1356 | return false; | |
1357 | ||
8383303e | 1358 | section_size_type symbols_size; |
f7e2ee48 ILT |
1359 | const unsigned char* symbols = this->section_contents(this->symtab_shndx_, |
1360 | &symbols_size, | |
1361 | false); | |
1362 | ||
d491d34e ILT |
1363 | unsigned int symbol_names_shndx = |
1364 | this->adjust_shndx(this->section_link(this->symtab_shndx_)); | |
8383303e | 1365 | section_size_type names_size; |
f7e2ee48 ILT |
1366 | const unsigned char* symbol_names_u = |
1367 | this->section_contents(symbol_names_shndx, &names_size, false); | |
1368 | const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u); | |
1369 | ||
1370 | const int sym_size = This::sym_size; | |
1371 | const size_t count = symbols_size / sym_size; | |
1372 | ||
1373 | const unsigned char* p = symbols; | |
1374 | for (size_t i = 0; i < count; ++i, p += sym_size) | |
1375 | { | |
1376 | elfcpp::Sym<size, big_endian> sym(p); | |
1377 | ||
1378 | if (sym.get_st_type() == elfcpp::STT_FILE) | |
1379 | { | |
1380 | if (sym.get_st_name() >= names_size) | |
1381 | info->source_file = "(invalid)"; | |
1382 | else | |
1383 | info->source_file = symbol_names + sym.get_st_name(); | |
d491d34e | 1384 | continue; |
f7e2ee48 | 1385 | } |
d491d34e ILT |
1386 | |
1387 | bool is_ordinary; | |
1388 | unsigned int st_shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(), | |
1389 | &is_ordinary); | |
1390 | if (is_ordinary | |
1391 | && st_shndx == shndx | |
1392 | && static_cast<off_t>(sym.get_st_value()) <= offset | |
1393 | && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size()) | |
1394 | > offset)) | |
f7e2ee48 ILT |
1395 | { |
1396 | if (sym.get_st_name() > names_size) | |
1397 | info->enclosing_symbol_name = "(invalid)"; | |
1398 | else | |
a2b1aa12 ILT |
1399 | { |
1400 | info->enclosing_symbol_name = symbol_names + sym.get_st_name(); | |
086a1841 | 1401 | if (parameters->options().do_demangle()) |
a2b1aa12 ILT |
1402 | { |
1403 | char* demangled_name = cplus_demangle( | |
1404 | info->enclosing_symbol_name.c_str(), | |
1405 | DMGL_ANSI | DMGL_PARAMS); | |
1406 | if (demangled_name != NULL) | |
1407 | { | |
1408 | info->enclosing_symbol_name.assign(demangled_name); | |
1409 | free(demangled_name); | |
1410 | } | |
1411 | } | |
1412 | } | |
f7e2ee48 ILT |
1413 | return true; |
1414 | } | |
1415 | } | |
1416 | ||
1417 | return false; | |
1418 | } | |
1419 | ||
54dc6425 ILT |
1420 | // Input_objects methods. |
1421 | ||
008db82e ILT |
1422 | // Add a regular relocatable object to the list. Return false if this |
1423 | // object should be ignored. | |
f6ce93d6 | 1424 | |
008db82e | 1425 | bool |
54dc6425 ILT |
1426 | Input_objects::add_object(Object* obj) |
1427 | { | |
fbfba508 | 1428 | // Set the global target from the first object file we recognize. |
019cdb1a | 1429 | Target* target = obj->target(); |
8851ecca | 1430 | if (!parameters->target_valid()) |
fbfba508 | 1431 | set_parameters_target(target); |
8851ecca | 1432 | else if (target != ¶meters->target()) |
019cdb1a | 1433 | { |
fbfba508 | 1434 | obj->error(_("incompatible target")); |
019cdb1a ILT |
1435 | return false; |
1436 | } | |
1437 | ||
c5818ff1 CC |
1438 | // Print the filename if the -t/--trace option is selected. |
1439 | if (parameters->options().trace()) | |
1440 | gold_info("%s", obj->name().c_str()); | |
1441 | ||
008db82e | 1442 | if (!obj->is_dynamic()) |
f6ce93d6 | 1443 | this->relobj_list_.push_back(static_cast<Relobj*>(obj)); |
008db82e ILT |
1444 | else |
1445 | { | |
1446 | // See if this is a duplicate SONAME. | |
1447 | Dynobj* dynobj = static_cast<Dynobj*>(obj); | |
9a2d6984 | 1448 | const char* soname = dynobj->soname(); |
008db82e ILT |
1449 | |
1450 | std::pair<Unordered_set<std::string>::iterator, bool> ins = | |
9a2d6984 | 1451 | this->sonames_.insert(soname); |
008db82e ILT |
1452 | if (!ins.second) |
1453 | { | |
1454 | // We have already seen a dynamic object with this soname. | |
1455 | return false; | |
1456 | } | |
1457 | ||
1458 | this->dynobj_list_.push_back(dynobj); | |
9a2d6984 ILT |
1459 | |
1460 | // If this is -lc, remember the directory in which we found it. | |
1461 | // We use this when issuing warnings about undefined symbols: as | |
1462 | // a heuristic, we don't warn about system libraries found in | |
1463 | // the same directory as -lc. | |
1464 | if (strncmp(soname, "libc.so", 7) == 0) | |
1465 | { | |
1466 | const char* object_name = dynobj->name().c_str(); | |
1467 | const char* base = lbasename(object_name); | |
1468 | if (base != object_name) | |
1469 | this->system_library_directory_.assign(object_name, | |
1470 | base - 1 - object_name); | |
1471 | } | |
008db82e | 1472 | } |
75f65a3e | 1473 | |
008db82e | 1474 | return true; |
54dc6425 ILT |
1475 | } |
1476 | ||
9a2d6984 ILT |
1477 | // Return whether an object was found in the system library directory. |
1478 | ||
1479 | bool | |
1480 | Input_objects::found_in_system_library_directory(const Object* object) const | |
1481 | { | |
1482 | return (!this->system_library_directory_.empty() | |
1483 | && object->name().compare(0, | |
1484 | this->system_library_directory_.size(), | |
1485 | this->system_library_directory_) == 0); | |
1486 | } | |
1487 | ||
e2827e5f ILT |
1488 | // For each dynamic object, record whether we've seen all of its |
1489 | // explicit dependencies. | |
1490 | ||
1491 | void | |
1492 | Input_objects::check_dynamic_dependencies() const | |
1493 | { | |
1494 | for (Dynobj_list::const_iterator p = this->dynobj_list_.begin(); | |
1495 | p != this->dynobj_list_.end(); | |
1496 | ++p) | |
1497 | { | |
1498 | const Dynobj::Needed& needed((*p)->needed()); | |
1499 | bool found_all = true; | |
1500 | for (Dynobj::Needed::const_iterator pneeded = needed.begin(); | |
1501 | pneeded != needed.end(); | |
1502 | ++pneeded) | |
1503 | { | |
1504 | if (this->sonames_.find(*pneeded) == this->sonames_.end()) | |
1505 | { | |
1506 | found_all = false; | |
1507 | break; | |
1508 | } | |
1509 | } | |
1510 | (*p)->set_has_unknown_needed_entries(!found_all); | |
1511 | } | |
1512 | } | |
1513 | ||
92e059d8 ILT |
1514 | // Relocate_info methods. |
1515 | ||
1516 | // Return a string describing the location of a relocation. This is | |
1517 | // only used in error messages. | |
1518 | ||
1519 | template<int size, bool big_endian> | |
1520 | std::string | |
f7e2ee48 | 1521 | Relocate_info<size, big_endian>::location(size_t, off_t offset) const |
92e059d8 | 1522 | { |
5c2c6c95 ILT |
1523 | // See if we can get line-number information from debugging sections. |
1524 | std::string filename; | |
1525 | std::string file_and_lineno; // Better than filename-only, if available. | |
4c50553d | 1526 | |
a55ce7fe | 1527 | Sized_dwarf_line_info<size, big_endian> line_info(this->object); |
24badc65 ILT |
1528 | // This will be "" if we failed to parse the debug info for any reason. |
1529 | file_and_lineno = line_info.addr2line(this->data_shndx, offset); | |
4c50553d | 1530 | |
92e059d8 | 1531 | std::string ret(this->object->name()); |
f7e2ee48 ILT |
1532 | ret += ':'; |
1533 | Symbol_location_info info; | |
1534 | if (this->object->get_symbol_location_info(this->data_shndx, offset, &info)) | |
1535 | { | |
1536 | ret += " in function "; | |
1537 | ret += info.enclosing_symbol_name; | |
1538 | ret += ":"; | |
5c2c6c95 ILT |
1539 | filename = info.source_file; |
1540 | } | |
1541 | ||
1542 | if (!file_and_lineno.empty()) | |
1543 | ret += file_and_lineno; | |
1544 | else | |
1545 | { | |
1546 | if (!filename.empty()) | |
1547 | ret += filename; | |
1548 | ret += "("; | |
1549 | ret += this->object->section_name(this->data_shndx); | |
1550 | char buf[100]; | |
1551 | // Offsets into sections have to be positive. | |
1552 | snprintf(buf, sizeof(buf), "+0x%lx", static_cast<long>(offset)); | |
1553 | ret += buf; | |
1554 | ret += ")"; | |
f7e2ee48 | 1555 | } |
92e059d8 ILT |
1556 | return ret; |
1557 | } | |
1558 | ||
bae7f79e ILT |
1559 | } // End namespace gold. |
1560 | ||
1561 | namespace | |
1562 | { | |
1563 | ||
1564 | using namespace gold; | |
1565 | ||
1566 | // Read an ELF file with the header and return the appropriate | |
1567 | // instance of Object. | |
1568 | ||
1569 | template<int size, bool big_endian> | |
1570 | Object* | |
1571 | make_elf_sized_object(const std::string& name, Input_file* input_file, | |
1572 | off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr) | |
1573 | { | |
1574 | int et = ehdr.get_e_type(); | |
bae7f79e ILT |
1575 | if (et == elfcpp::ET_REL) |
1576 | { | |
f6ce93d6 ILT |
1577 | Sized_relobj<size, big_endian>* obj = |
1578 | new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr); | |
bae7f79e ILT |
1579 | obj->setup(ehdr); |
1580 | return obj; | |
1581 | } | |
dbe717ef ILT |
1582 | else if (et == elfcpp::ET_DYN) |
1583 | { | |
1584 | Sized_dynobj<size, big_endian>* obj = | |
1585 | new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr); | |
1586 | obj->setup(ehdr); | |
1587 | return obj; | |
1588 | } | |
bae7f79e ILT |
1589 | else |
1590 | { | |
75f2446e ILT |
1591 | gold_error(_("%s: unsupported ELF file type %d"), |
1592 | name.c_str(), et); | |
1593 | return NULL; | |
bae7f79e ILT |
1594 | } |
1595 | } | |
1596 | ||
1597 | } // End anonymous namespace. | |
1598 | ||
1599 | namespace gold | |
1600 | { | |
1601 | ||
1602 | // Read an ELF file and return the appropriate instance of Object. | |
1603 | ||
1604 | Object* | |
1605 | make_elf_object(const std::string& name, Input_file* input_file, off_t offset, | |
8383303e | 1606 | const unsigned char* p, section_offset_type bytes) |
bae7f79e ILT |
1607 | { |
1608 | if (bytes < elfcpp::EI_NIDENT) | |
1609 | { | |
75f2446e ILT |
1610 | gold_error(_("%s: ELF file too short"), name.c_str()); |
1611 | return NULL; | |
bae7f79e ILT |
1612 | } |
1613 | ||
1614 | int v = p[elfcpp::EI_VERSION]; | |
1615 | if (v != elfcpp::EV_CURRENT) | |
1616 | { | |
1617 | if (v == elfcpp::EV_NONE) | |
75f2446e | 1618 | gold_error(_("%s: invalid ELF version 0"), name.c_str()); |
bae7f79e | 1619 | else |
75f2446e ILT |
1620 | gold_error(_("%s: unsupported ELF version %d"), name.c_str(), v); |
1621 | return NULL; | |
bae7f79e ILT |
1622 | } |
1623 | ||
1624 | int c = p[elfcpp::EI_CLASS]; | |
1625 | if (c == elfcpp::ELFCLASSNONE) | |
1626 | { | |
75f2446e ILT |
1627 | gold_error(_("%s: invalid ELF class 0"), name.c_str()); |
1628 | return NULL; | |
bae7f79e ILT |
1629 | } |
1630 | else if (c != elfcpp::ELFCLASS32 | |
1631 | && c != elfcpp::ELFCLASS64) | |
1632 | { | |
75f2446e ILT |
1633 | gold_error(_("%s: unsupported ELF class %d"), name.c_str(), c); |
1634 | return NULL; | |
bae7f79e ILT |
1635 | } |
1636 | ||
1637 | int d = p[elfcpp::EI_DATA]; | |
1638 | if (d == elfcpp::ELFDATANONE) | |
1639 | { | |
75f2446e ILT |
1640 | gold_error(_("%s: invalid ELF data encoding"), name.c_str()); |
1641 | return NULL; | |
bae7f79e ILT |
1642 | } |
1643 | else if (d != elfcpp::ELFDATA2LSB | |
1644 | && d != elfcpp::ELFDATA2MSB) | |
1645 | { | |
75f2446e ILT |
1646 | gold_error(_("%s: unsupported ELF data encoding %d"), name.c_str(), d); |
1647 | return NULL; | |
bae7f79e ILT |
1648 | } |
1649 | ||
1650 | bool big_endian = d == elfcpp::ELFDATA2MSB; | |
1651 | ||
1652 | if (c == elfcpp::ELFCLASS32) | |
1653 | { | |
1654 | if (bytes < elfcpp::Elf_sizes<32>::ehdr_size) | |
1655 | { | |
75f2446e ILT |
1656 | gold_error(_("%s: ELF file too short"), name.c_str()); |
1657 | return NULL; | |
bae7f79e ILT |
1658 | } |
1659 | if (big_endian) | |
1660 | { | |
193a53d9 | 1661 | #ifdef HAVE_TARGET_32_BIG |
bae7f79e ILT |
1662 | elfcpp::Ehdr<32, true> ehdr(p); |
1663 | return make_elf_sized_object<32, true>(name, input_file, | |
1664 | offset, ehdr); | |
193a53d9 | 1665 | #else |
75f2446e ILT |
1666 | gold_error(_("%s: not configured to support " |
1667 | "32-bit big-endian object"), | |
1668 | name.c_str()); | |
1669 | return NULL; | |
193a53d9 | 1670 | #endif |
bae7f79e ILT |
1671 | } |
1672 | else | |
1673 | { | |
193a53d9 | 1674 | #ifdef HAVE_TARGET_32_LITTLE |
bae7f79e ILT |
1675 | elfcpp::Ehdr<32, false> ehdr(p); |
1676 | return make_elf_sized_object<32, false>(name, input_file, | |
1677 | offset, ehdr); | |
193a53d9 | 1678 | #else |
75f2446e ILT |
1679 | gold_error(_("%s: not configured to support " |
1680 | "32-bit little-endian object"), | |
1681 | name.c_str()); | |
1682 | return NULL; | |
193a53d9 | 1683 | #endif |
bae7f79e ILT |
1684 | } |
1685 | } | |
1686 | else | |
1687 | { | |
c165fb93 | 1688 | if (bytes < elfcpp::Elf_sizes<64>::ehdr_size) |
bae7f79e | 1689 | { |
75f2446e ILT |
1690 | gold_error(_("%s: ELF file too short"), name.c_str()); |
1691 | return NULL; | |
bae7f79e ILT |
1692 | } |
1693 | if (big_endian) | |
1694 | { | |
193a53d9 | 1695 | #ifdef HAVE_TARGET_64_BIG |
bae7f79e ILT |
1696 | elfcpp::Ehdr<64, true> ehdr(p); |
1697 | return make_elf_sized_object<64, true>(name, input_file, | |
1698 | offset, ehdr); | |
193a53d9 | 1699 | #else |
75f2446e ILT |
1700 | gold_error(_("%s: not configured to support " |
1701 | "64-bit big-endian object"), | |
1702 | name.c_str()); | |
1703 | return NULL; | |
193a53d9 | 1704 | #endif |
bae7f79e ILT |
1705 | } |
1706 | else | |
1707 | { | |
193a53d9 | 1708 | #ifdef HAVE_TARGET_64_LITTLE |
bae7f79e ILT |
1709 | elfcpp::Ehdr<64, false> ehdr(p); |
1710 | return make_elf_sized_object<64, false>(name, input_file, | |
1711 | offset, ehdr); | |
193a53d9 | 1712 | #else |
75f2446e ILT |
1713 | gold_error(_("%s: not configured to support " |
1714 | "64-bit little-endian object"), | |
1715 | name.c_str()); | |
1716 | return NULL; | |
193a53d9 | 1717 | #endif |
bae7f79e ILT |
1718 | } |
1719 | } | |
1720 | } | |
1721 | ||
04bf7072 ILT |
1722 | // Instantiate the templates we need. |
1723 | ||
1724 | #ifdef HAVE_TARGET_32_LITTLE | |
1725 | template | |
1726 | void | |
1727 | Object::read_section_data<32, false>(elfcpp::Elf_file<32, false, Object>*, | |
1728 | Read_symbols_data*); | |
1729 | #endif | |
1730 | ||
1731 | #ifdef HAVE_TARGET_32_BIG | |
1732 | template | |
1733 | void | |
1734 | Object::read_section_data<32, true>(elfcpp::Elf_file<32, true, Object>*, | |
1735 | Read_symbols_data*); | |
1736 | #endif | |
1737 | ||
1738 | #ifdef HAVE_TARGET_64_LITTLE | |
1739 | template | |
1740 | void | |
1741 | Object::read_section_data<64, false>(elfcpp::Elf_file<64, false, Object>*, | |
1742 | Read_symbols_data*); | |
1743 | #endif | |
1744 | ||
1745 | #ifdef HAVE_TARGET_64_BIG | |
1746 | template | |
1747 | void | |
1748 | Object::read_section_data<64, true>(elfcpp::Elf_file<64, true, Object>*, | |
1749 | Read_symbols_data*); | |
1750 | #endif | |
bae7f79e | 1751 | |
193a53d9 | 1752 | #ifdef HAVE_TARGET_32_LITTLE |
bae7f79e | 1753 | template |
f6ce93d6 | 1754 | class Sized_relobj<32, false>; |
193a53d9 | 1755 | #endif |
bae7f79e | 1756 | |
193a53d9 | 1757 | #ifdef HAVE_TARGET_32_BIG |
bae7f79e | 1758 | template |
f6ce93d6 | 1759 | class Sized_relobj<32, true>; |
193a53d9 | 1760 | #endif |
bae7f79e | 1761 | |
193a53d9 | 1762 | #ifdef HAVE_TARGET_64_LITTLE |
bae7f79e | 1763 | template |
f6ce93d6 | 1764 | class Sized_relobj<64, false>; |
193a53d9 | 1765 | #endif |
bae7f79e | 1766 | |
193a53d9 | 1767 | #ifdef HAVE_TARGET_64_BIG |
bae7f79e | 1768 | template |
f6ce93d6 | 1769 | class Sized_relobj<64, true>; |
193a53d9 | 1770 | #endif |
bae7f79e | 1771 | |
193a53d9 | 1772 | #ifdef HAVE_TARGET_32_LITTLE |
92e059d8 ILT |
1773 | template |
1774 | struct Relocate_info<32, false>; | |
193a53d9 | 1775 | #endif |
92e059d8 | 1776 | |
193a53d9 | 1777 | #ifdef HAVE_TARGET_32_BIG |
92e059d8 ILT |
1778 | template |
1779 | struct Relocate_info<32, true>; | |
193a53d9 | 1780 | #endif |
92e059d8 | 1781 | |
193a53d9 | 1782 | #ifdef HAVE_TARGET_64_LITTLE |
92e059d8 ILT |
1783 | template |
1784 | struct Relocate_info<64, false>; | |
193a53d9 | 1785 | #endif |
92e059d8 | 1786 | |
193a53d9 | 1787 | #ifdef HAVE_TARGET_64_BIG |
92e059d8 ILT |
1788 | template |
1789 | struct Relocate_info<64, true>; | |
193a53d9 | 1790 | #endif |
92e059d8 | 1791 | |
bae7f79e | 1792 | } // End namespace gold. |