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