Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // object.cc -- support for an object file for linking in gold |
2 | ||
3 | #include "gold.h" | |
4 | ||
5 | #include <cerrno> | |
6 | #include <cstring> | |
7 | #include <cassert> | |
645f8123 | 8 | #include <cstdarg> |
bae7f79e | 9 | |
14bfc3f5 | 10 | #include "target-select.h" |
a2fb1b05 | 11 | #include "layout.h" |
61ba1cf9 | 12 | #include "output.h" |
f6ce93d6 ILT |
13 | #include "symtab.h" |
14 | #include "object.h" | |
15 | #include "dynobj.h" | |
bae7f79e ILT |
16 | |
17 | namespace gold | |
18 | { | |
19 | ||
645f8123 ILT |
20 | // Class Object. |
21 | ||
dbe717ef ILT |
22 | // Set the target based on fields in the ELF file header. |
23 | ||
24 | void | |
25 | Object::set_target(int machine, int size, bool big_endian, int osabi, | |
26 | int abiversion) | |
27 | { | |
28 | Target* target = select_target(machine, size, big_endian, osabi, abiversion); | |
29 | if (target == NULL) | |
30 | { | |
31 | fprintf(stderr, _("%s: %s: unsupported ELF machine number %d\n"), | |
32 | program_name, this->name().c_str(), machine); | |
33 | gold_exit(false); | |
34 | } | |
35 | this->target_ = target; | |
36 | } | |
37 | ||
645f8123 ILT |
38 | // Report an error for the elfcpp::Elf_file interface. |
39 | ||
40 | void | |
41 | Object::error(const char* format, ...) | |
42 | { | |
43 | va_list args; | |
44 | ||
45 | fprintf(stderr, "%s: %s: ", program_name, this->name().c_str()); | |
46 | va_start(args, format); | |
47 | vfprintf(stderr, format, args); | |
48 | va_end(args); | |
49 | putc('\n', stderr); | |
50 | ||
51 | gold_exit(false); | |
52 | } | |
53 | ||
54 | // Return a view of the contents of a section. | |
55 | ||
56 | const unsigned char* | |
57 | Object::section_contents(unsigned int shndx, off_t* plen) | |
58 | { | |
59 | Location loc(this->do_section_contents(shndx)); | |
60 | *plen = loc.data_size; | |
61 | return this->get_view(loc.file_offset, loc.data_size); | |
62 | } | |
63 | ||
dbe717ef ILT |
64 | // Read the section data into SD. This is code common to Sized_relobj |
65 | // and Sized_dynobj, so we put it into Object. | |
66 | ||
67 | template<int size, bool big_endian> | |
68 | void | |
69 | Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file, | |
70 | Read_symbols_data* sd) | |
71 | { | |
72 | const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; | |
73 | ||
74 | // Read the section headers. | |
75 | const off_t shoff = elf_file->shoff(); | |
76 | const unsigned int shnum = this->shnum(); | |
77 | sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size); | |
78 | ||
79 | // Read the section names. | |
80 | const unsigned char* pshdrs = sd->section_headers->data(); | |
81 | const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size; | |
82 | typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames); | |
83 | ||
84 | if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB) | |
85 | { | |
86 | fprintf(stderr, | |
87 | _("%s: %s: section name section has wrong type: %u\n"), | |
88 | program_name, this->name().c_str(), | |
89 | static_cast<unsigned int>(shdrnames.get_sh_type())); | |
90 | gold_exit(false); | |
91 | } | |
92 | ||
93 | sd->section_names_size = shdrnames.get_sh_size(); | |
94 | sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(), | |
95 | sd->section_names_size); | |
96 | } | |
97 | ||
98 | // If NAME is the name of a special .gnu.warning section, arrange for | |
99 | // the warning to be issued. SHNDX is the section index. Return | |
100 | // whether it is a warning section. | |
101 | ||
102 | bool | |
103 | Object::handle_gnu_warning_section(const char* name, unsigned int shndx, | |
104 | Symbol_table* symtab) | |
105 | { | |
106 | const char warn_prefix[] = ".gnu.warning."; | |
107 | const int warn_prefix_len = sizeof warn_prefix - 1; | |
108 | if (strncmp(name, warn_prefix, warn_prefix_len) == 0) | |
109 | { | |
110 | symtab->add_warning(name + warn_prefix_len, this, shndx); | |
111 | return true; | |
112 | } | |
113 | return false; | |
114 | } | |
115 | ||
f6ce93d6 | 116 | // Class Sized_relobj. |
bae7f79e ILT |
117 | |
118 | template<int size, bool big_endian> | |
f6ce93d6 | 119 | Sized_relobj<size, big_endian>::Sized_relobj( |
bae7f79e ILT |
120 | const std::string& name, |
121 | Input_file* input_file, | |
122 | off_t offset, | |
123 | const elfcpp::Ehdr<size, big_endian>& ehdr) | |
f6ce93d6 | 124 | : Relobj(name, input_file, offset), |
645f8123 | 125 | elf_file_(this, ehdr), |
dbe717ef | 126 | symtab_shndx_(-1U), |
61ba1cf9 ILT |
127 | local_symbol_count_(0), |
128 | output_local_symbol_count_(0), | |
75f65a3e | 129 | symbols_(NULL), |
61ba1cf9 | 130 | local_symbol_offset_(0), |
c06b7b0b ILT |
131 | local_values_(), |
132 | local_indexes_() | |
bae7f79e | 133 | { |
bae7f79e ILT |
134 | } |
135 | ||
136 | template<int size, bool big_endian> | |
f6ce93d6 | 137 | Sized_relobj<size, big_endian>::~Sized_relobj() |
bae7f79e ILT |
138 | { |
139 | } | |
140 | ||
645f8123 | 141 | // Set up an object file based on the file header. This sets up the |
bae7f79e ILT |
142 | // target and reads the section information. |
143 | ||
144 | template<int size, bool big_endian> | |
145 | void | |
f6ce93d6 | 146 | Sized_relobj<size, big_endian>::setup( |
bae7f79e ILT |
147 | const elfcpp::Ehdr<size, big_endian>& ehdr) |
148 | { | |
dbe717ef ILT |
149 | this->set_target(ehdr.get_e_machine(), size, big_endian, |
150 | ehdr.get_e_ident()[elfcpp::EI_OSABI], | |
151 | ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]); | |
12e14209 | 152 | |
dbe717ef | 153 | const unsigned int shnum = this->elf_file_.shnum(); |
a2fb1b05 | 154 | this->set_shnum(shnum); |
dbe717ef | 155 | } |
12e14209 | 156 | |
dbe717ef ILT |
157 | // Find the SHT_SYMTAB section, given the section headers. The ELF |
158 | // standard says that maybe in the future there can be more than one | |
159 | // SHT_SYMTAB section. Until somebody figures out how that could | |
160 | // work, we assume there is only one. | |
12e14209 | 161 | |
dbe717ef ILT |
162 | template<int size, bool big_endian> |
163 | void | |
164 | Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs) | |
165 | { | |
166 | const unsigned int shnum = this->shnum(); | |
167 | this->symtab_shndx_ = 0; | |
168 | if (shnum > 0) | |
bae7f79e | 169 | { |
dbe717ef ILT |
170 | // Look through the sections in reverse order, since gas tends |
171 | // to put the symbol table at the end. | |
172 | const unsigned char* p = pshdrs + shnum * This::shdr_size; | |
173 | unsigned int i = shnum; | |
174 | while (i > 0) | |
bae7f79e | 175 | { |
dbe717ef ILT |
176 | --i; |
177 | p -= This::shdr_size; | |
178 | typename This::Shdr shdr(p); | |
179 | if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB) | |
180 | { | |
181 | this->symtab_shndx_ = i; | |
182 | break; | |
183 | } | |
bae7f79e | 184 | } |
bae7f79e ILT |
185 | } |
186 | } | |
187 | ||
12e14209 | 188 | // Read the sections and symbols from an object file. |
bae7f79e ILT |
189 | |
190 | template<int size, bool big_endian> | |
12e14209 | 191 | void |
f6ce93d6 | 192 | Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd) |
bae7f79e | 193 | { |
dbe717ef | 194 | this->read_section_data(&this->elf_file_, sd); |
12e14209 | 195 | |
dbe717ef ILT |
196 | const unsigned char* const pshdrs = sd->section_headers->data(); |
197 | ||
198 | this->find_symtab(pshdrs); | |
12e14209 | 199 | |
645f8123 | 200 | if (this->symtab_shndx_ == 0) |
bae7f79e ILT |
201 | { |
202 | // No symbol table. Weird but legal. | |
12e14209 ILT |
203 | sd->symbols = NULL; |
204 | sd->symbols_size = 0; | |
205 | sd->symbol_names = NULL; | |
206 | sd->symbol_names_size = 0; | |
207 | return; | |
bae7f79e ILT |
208 | } |
209 | ||
12e14209 ILT |
210 | // Get the symbol table section header. |
211 | typename This::Shdr symtabshdr(pshdrs | |
645f8123 | 212 | + this->symtab_shndx_ * This::shdr_size); |
bae7f79e ILT |
213 | assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); |
214 | ||
75f65a3e ILT |
215 | // We only need the external symbols. |
216 | const int sym_size = This::sym_size; | |
92e059d8 ILT |
217 | const unsigned int loccount = symtabshdr.get_sh_info(); |
218 | this->local_symbol_count_ = loccount; | |
219 | off_t locsize = loccount * sym_size; | |
75f65a3e ILT |
220 | off_t extoff = symtabshdr.get_sh_offset() + locsize; |
221 | off_t extsize = symtabshdr.get_sh_size() - locsize; | |
222 | ||
bae7f79e | 223 | // Read the symbol table. |
75f65a3e | 224 | File_view* fvsymtab = this->get_lasting_view(extoff, extsize); |
bae7f79e ILT |
225 | |
226 | // Read the section header for the symbol names. | |
dbe717ef ILT |
227 | unsigned int strtab_shndx = symtabshdr.get_sh_link(); |
228 | if (strtab_shndx >= this->shnum()) | |
bae7f79e ILT |
229 | { |
230 | fprintf(stderr, _("%s: %s: invalid symbol table name index: %u\n"), | |
dbe717ef | 231 | program_name, this->name().c_str(), strtab_shndx); |
bae7f79e ILT |
232 | gold_exit(false); |
233 | } | |
dbe717ef | 234 | typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size); |
bae7f79e ILT |
235 | if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB) |
236 | { | |
237 | fprintf(stderr, | |
238 | _("%s: %s: symbol table name section has wrong type: %u\n"), | |
239 | program_name, this->name().c_str(), | |
240 | static_cast<unsigned int>(strtabshdr.get_sh_type())); | |
241 | gold_exit(false); | |
242 | } | |
243 | ||
244 | // Read the symbol names. | |
245 | File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(), | |
246 | strtabshdr.get_sh_size()); | |
247 | ||
12e14209 ILT |
248 | sd->symbols = fvsymtab; |
249 | sd->symbols_size = extsize; | |
250 | sd->symbol_names = fvstrtab; | |
251 | sd->symbol_names_size = strtabshdr.get_sh_size(); | |
a2fb1b05 ILT |
252 | } |
253 | ||
254 | // Return whether to include a section group in the link. LAYOUT is | |
255 | // used to keep track of which section groups we have already seen. | |
256 | // INDEX is the index of the section group and SHDR is the section | |
257 | // header. If we do not want to include this group, we set bits in | |
258 | // OMIT for each section which should be discarded. | |
259 | ||
260 | template<int size, bool big_endian> | |
261 | bool | |
f6ce93d6 | 262 | Sized_relobj<size, big_endian>::include_section_group( |
a2fb1b05 ILT |
263 | Layout* layout, |
264 | unsigned int index, | |
265 | const elfcpp::Shdr<size, big_endian>& shdr, | |
266 | std::vector<bool>* omit) | |
267 | { | |
268 | // Read the section contents. | |
269 | const unsigned char* pcon = this->get_view(shdr.get_sh_offset(), | |
270 | shdr.get_sh_size()); | |
271 | const elfcpp::Elf_Word* pword = | |
272 | reinterpret_cast<const elfcpp::Elf_Word*>(pcon); | |
273 | ||
274 | // The first word contains flags. We only care about COMDAT section | |
275 | // groups. Other section groups are always included in the link | |
276 | // just like ordinary sections. | |
f6ce93d6 | 277 | elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword); |
a2fb1b05 ILT |
278 | if ((flags & elfcpp::GRP_COMDAT) == 0) |
279 | return true; | |
280 | ||
281 | // Look up the group signature, which is the name of a symbol. This | |
282 | // is a lot of effort to go to to read a string. Why didn't they | |
283 | // just use the name of the SHT_GROUP section as the group | |
284 | // signature? | |
285 | ||
286 | // Get the appropriate symbol table header (this will normally be | |
287 | // the single SHT_SYMTAB section, but in principle it need not be). | |
645f8123 ILT |
288 | const unsigned int link = shdr.get_sh_link(); |
289 | typename This::Shdr symshdr(this, this->elf_file_.section_header(link)); | |
a2fb1b05 ILT |
290 | |
291 | // Read the symbol table entry. | |
292 | if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size) | |
293 | { | |
294 | fprintf(stderr, _("%s: %s: section group %u info %u out of range\n"), | |
295 | program_name, this->name().c_str(), index, shdr.get_sh_info()); | |
296 | gold_exit(false); | |
297 | } | |
298 | off_t symoff = symshdr.get_sh_offset() + shdr.get_sh_info() * This::sym_size; | |
299 | const unsigned char* psym = this->get_view(symoff, This::sym_size); | |
300 | elfcpp::Sym<size, big_endian> sym(psym); | |
301 | ||
a2fb1b05 | 302 | // Read the symbol table names. |
645f8123 ILT |
303 | off_t symnamelen; |
304 | const unsigned char* psymnamesu; | |
305 | psymnamesu = this->section_contents(symshdr.get_sh_link(), &symnamelen); | |
a2fb1b05 ILT |
306 | const char* psymnames = reinterpret_cast<const char*>(psymnamesu); |
307 | ||
308 | // Get the section group signature. | |
645f8123 | 309 | if (sym.get_st_name() >= symnamelen) |
a2fb1b05 ILT |
310 | { |
311 | fprintf(stderr, _("%s: %s: symbol %u name offset %u out of range\n"), | |
312 | program_name, this->name().c_str(), shdr.get_sh_info(), | |
313 | sym.get_st_name()); | |
314 | gold_exit(false); | |
315 | } | |
316 | ||
317 | const char* signature = psymnames + sym.get_st_name(); | |
318 | ||
ead1e424 ILT |
319 | // It seems that some versions of gas will create a section group |
320 | // associated with a section symbol, and then fail to give a name to | |
321 | // the section symbol. In such a case, use the name of the section. | |
322 | // FIXME. | |
645f8123 ILT |
323 | std::string secname; |
324 | if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION) | |
ead1e424 | 325 | { |
645f8123 ILT |
326 | secname = this->section_name(sym.get_st_shndx()); |
327 | signature = secname.c_str(); | |
ead1e424 ILT |
328 | } |
329 | ||
a2fb1b05 ILT |
330 | // Record this section group, and see whether we've already seen one |
331 | // with the same signature. | |
332 | if (layout->add_comdat(signature, true)) | |
333 | return true; | |
334 | ||
335 | // This is a duplicate. We want to discard the sections in this | |
336 | // group. | |
337 | size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word); | |
338 | for (size_t i = 1; i < count; ++i) | |
339 | { | |
f6ce93d6 ILT |
340 | elfcpp::Elf_Word secnum = |
341 | elfcpp::Swap<32, big_endian>::readval(pword + i); | |
a2fb1b05 ILT |
342 | if (secnum >= this->shnum()) |
343 | { | |
344 | fprintf(stderr, | |
345 | _("%s: %s: section %u in section group %u out of range"), | |
346 | program_name, this->name().c_str(), secnum, | |
347 | index); | |
348 | gold_exit(false); | |
349 | } | |
350 | (*omit)[secnum] = true; | |
351 | } | |
352 | ||
353 | return false; | |
354 | } | |
355 | ||
356 | // Whether to include a linkonce section in the link. NAME is the | |
357 | // name of the section and SHDR is the section header. | |
358 | ||
359 | // Linkonce sections are a GNU extension implemented in the original | |
360 | // GNU linker before section groups were defined. The semantics are | |
361 | // that we only include one linkonce section with a given name. The | |
362 | // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME, | |
363 | // where T is the type of section and SYMNAME is the name of a symbol. | |
364 | // In an attempt to make linkonce sections interact well with section | |
365 | // groups, we try to identify SYMNAME and use it like a section group | |
366 | // signature. We want to block section groups with that signature, | |
367 | // but not other linkonce sections with that signature. We also use | |
368 | // the full name of the linkonce section as a normal section group | |
369 | // signature. | |
370 | ||
371 | template<int size, bool big_endian> | |
372 | bool | |
f6ce93d6 | 373 | Sized_relobj<size, big_endian>::include_linkonce_section( |
a2fb1b05 ILT |
374 | Layout* layout, |
375 | const char* name, | |
376 | const elfcpp::Shdr<size, big_endian>&) | |
377 | { | |
378 | const char* symname = strrchr(name, '.') + 1; | |
a783673b ILT |
379 | bool include1 = layout->add_comdat(symname, false); |
380 | bool include2 = layout->add_comdat(name, true); | |
381 | return include1 && include2; | |
a2fb1b05 ILT |
382 | } |
383 | ||
384 | // Lay out the input sections. We walk through the sections and check | |
385 | // whether they should be included in the link. If they should, we | |
386 | // pass them to the Layout object, which will return an output section | |
387 | // and an offset. | |
388 | ||
389 | template<int size, bool big_endian> | |
390 | void | |
f6ce93d6 ILT |
391 | Sized_relobj<size, big_endian>::do_layout(const General_options& options, |
392 | Symbol_table* symtab, | |
393 | Layout* layout, | |
12e14209 | 394 | Read_symbols_data* sd) |
a2fb1b05 | 395 | { |
dbe717ef | 396 | const unsigned int shnum = this->shnum(); |
12e14209 ILT |
397 | if (shnum == 0) |
398 | return; | |
a2fb1b05 ILT |
399 | |
400 | // Get the section headers. | |
12e14209 | 401 | const unsigned char* pshdrs = sd->section_headers->data(); |
a2fb1b05 ILT |
402 | |
403 | // Get the section names. | |
12e14209 | 404 | const unsigned char* pnamesu = sd->section_names->data(); |
a2fb1b05 ILT |
405 | const char* pnames = reinterpret_cast<const char*>(pnamesu); |
406 | ||
407 | std::vector<Map_to_output>& map_sections(this->map_to_output()); | |
61ba1cf9 | 408 | map_sections.resize(shnum); |
a2fb1b05 ILT |
409 | |
410 | // Keep track of which sections to omit. | |
411 | std::vector<bool> omit(shnum, false); | |
412 | ||
f6ce93d6 ILT |
413 | // Skip the first, dummy, section. |
414 | pshdrs += This::shdr_size; | |
415 | for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size) | |
a2fb1b05 | 416 | { |
75f65a3e | 417 | typename This::Shdr shdr(pshdrs); |
a2fb1b05 | 418 | |
12e14209 | 419 | if (shdr.get_sh_name() >= sd->section_names_size) |
a2fb1b05 ILT |
420 | { |
421 | fprintf(stderr, | |
422 | _("%s: %s: bad section name offset for section %u: %lu\n"), | |
423 | program_name, this->name().c_str(), i, | |
424 | static_cast<unsigned long>(shdr.get_sh_name())); | |
425 | gold_exit(false); | |
426 | } | |
427 | ||
428 | const char* name = pnames + shdr.get_sh_name(); | |
429 | ||
dbe717ef | 430 | if (this->handle_gnu_warning_section(name, i, symtab)) |
f6ce93d6 | 431 | { |
f6ce93d6 ILT |
432 | if (!options.is_relocatable()) |
433 | omit[i] = true; | |
434 | } | |
435 | ||
a2fb1b05 ILT |
436 | bool discard = omit[i]; |
437 | if (!discard) | |
438 | { | |
439 | if (shdr.get_sh_type() == elfcpp::SHT_GROUP) | |
440 | { | |
441 | if (!this->include_section_group(layout, i, shdr, &omit)) | |
442 | discard = true; | |
443 | } | |
444 | else if (Layout::is_linkonce(name)) | |
445 | { | |
446 | if (!this->include_linkonce_section(layout, name, shdr)) | |
447 | discard = true; | |
448 | } | |
449 | } | |
450 | ||
451 | if (discard) | |
452 | { | |
453 | // Do not include this section in the link. | |
454 | map_sections[i].output_section = NULL; | |
455 | continue; | |
456 | } | |
457 | ||
458 | off_t offset; | |
ead1e424 | 459 | Output_section* os = layout->layout(this, i, name, shdr, &offset); |
a2fb1b05 ILT |
460 | |
461 | map_sections[i].output_section = os; | |
462 | map_sections[i].offset = offset; | |
12e14209 ILT |
463 | } |
464 | ||
465 | delete sd->section_headers; | |
466 | sd->section_headers = NULL; | |
467 | delete sd->section_names; | |
468 | sd->section_names = NULL; | |
469 | } | |
470 | ||
471 | // Add the symbols to the symbol table. | |
472 | ||
473 | template<int size, bool big_endian> | |
474 | void | |
f6ce93d6 | 475 | Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab, |
12e14209 ILT |
476 | Read_symbols_data* sd) |
477 | { | |
478 | if (sd->symbols == NULL) | |
479 | { | |
480 | assert(sd->symbol_names == NULL); | |
481 | return; | |
482 | } | |
a2fb1b05 | 483 | |
12e14209 ILT |
484 | const int sym_size = This::sym_size; |
485 | size_t symcount = sd->symbols_size / sym_size; | |
486 | if (symcount * sym_size != sd->symbols_size) | |
487 | { | |
488 | fprintf(stderr, | |
489 | _("%s: %s: size of symbols is not multiple of symbol size\n"), | |
490 | program_name, this->name().c_str()); | |
491 | gold_exit(false); | |
a2fb1b05 | 492 | } |
12e14209 ILT |
493 | |
494 | this->symbols_ = new Symbol*[symcount]; | |
495 | ||
12e14209 ILT |
496 | const char* sym_names = |
497 | reinterpret_cast<const char*>(sd->symbol_names->data()); | |
dbe717ef ILT |
498 | symtab->add_from_relobj(this, sd->symbols->data(), symcount, sym_names, |
499 | sd->symbol_names_size, this->symbols_); | |
12e14209 ILT |
500 | |
501 | delete sd->symbols; | |
502 | sd->symbols = NULL; | |
503 | delete sd->symbol_names; | |
504 | sd->symbol_names = NULL; | |
bae7f79e ILT |
505 | } |
506 | ||
75f65a3e | 507 | // Finalize the local symbols. Here we record the file offset at |
61ba1cf9 | 508 | // which they should be output, we add their names to *POOL, and we |
c06b7b0b ILT |
509 | // add their values to THIS->LOCAL_VALUES_ and their indexes in the |
510 | // output symbol table to THIS->LOCAL_INDEXES_. Return the symbol | |
511 | // index. This function is always called from the main thread. The | |
512 | // actual output of the local symbols will occur in a separate task. | |
75f65a3e ILT |
513 | |
514 | template<int size, bool big_endian> | |
c06b7b0b ILT |
515 | unsigned int |
516 | Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index, | |
517 | off_t off, | |
75f65a3e ILT |
518 | Stringpool* pool) |
519 | { | |
dbe717ef | 520 | assert(this->symtab_shndx_ != -1U); |
645f8123 | 521 | if (this->symtab_shndx_ == 0) |
61ba1cf9 ILT |
522 | { |
523 | // This object has no symbols. Weird but legal. | |
c06b7b0b | 524 | return index; |
61ba1cf9 ILT |
525 | } |
526 | ||
c06b7b0b | 527 | assert(off == static_cast<off_t>(align_address(off, size >> 3))); |
61ba1cf9 | 528 | |
75f65a3e ILT |
529 | this->local_symbol_offset_ = off; |
530 | ||
531 | // Read the symbol table section header. | |
645f8123 ILT |
532 | const unsigned int symtab_shndx = this->symtab_shndx_; |
533 | typename This::Shdr symtabshdr(this, | |
534 | this->elf_file_.section_header(symtab_shndx)); | |
75f65a3e ILT |
535 | assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); |
536 | ||
537 | // Read the local symbols. | |
75f65a3e | 538 | const int sym_size = This::sym_size; |
92e059d8 ILT |
539 | const unsigned int loccount = this->local_symbol_count_; |
540 | assert(loccount == symtabshdr.get_sh_info()); | |
75f65a3e ILT |
541 | off_t locsize = loccount * sym_size; |
542 | const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(), | |
543 | locsize); | |
544 | ||
c06b7b0b ILT |
545 | this->local_values_.resize(loccount); |
546 | this->local_indexes_.resize(loccount); | |
61ba1cf9 | 547 | |
75f65a3e | 548 | // Read the symbol names. |
645f8123 ILT |
549 | const unsigned int strtab_shndx = symtabshdr.get_sh_link(); |
550 | off_t strtab_size; | |
551 | const unsigned char* pnamesu = this->section_contents(strtab_shndx, | |
552 | &strtab_size); | |
75f65a3e ILT |
553 | const char* pnames = reinterpret_cast<const char*>(pnamesu); |
554 | ||
555 | // Loop over the local symbols. | |
556 | ||
c06b7b0b | 557 | const std::vector<Map_to_output>& mo(this->map_to_output()); |
75f65a3e | 558 | unsigned int shnum = this->shnum(); |
61ba1cf9 | 559 | unsigned int count = 0; |
75f65a3e ILT |
560 | // Skip the first, dummy, symbol. |
561 | psyms += sym_size; | |
61ba1cf9 | 562 | for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size) |
75f65a3e ILT |
563 | { |
564 | elfcpp::Sym<size, big_endian> sym(psyms); | |
565 | ||
566 | unsigned int shndx = sym.get_st_shndx(); | |
567 | ||
568 | if (shndx >= elfcpp::SHN_LORESERVE) | |
569 | { | |
61ba1cf9 | 570 | if (shndx == elfcpp::SHN_ABS) |
c06b7b0b | 571 | this->local_values_[i] = sym.get_st_value(); |
61ba1cf9 | 572 | else |
75f65a3e | 573 | { |
61ba1cf9 | 574 | // FIXME: Handle SHN_XINDEX. |
75f65a3e ILT |
575 | fprintf(stderr, |
576 | _("%s: %s: unknown section index %u " | |
577 | "for local symbol %u\n"), | |
578 | program_name, this->name().c_str(), shndx, i); | |
579 | gold_exit(false); | |
580 | } | |
75f65a3e ILT |
581 | } |
582 | else | |
583 | { | |
584 | if (shndx >= shnum) | |
585 | { | |
586 | fprintf(stderr, | |
587 | _("%s: %s: local symbol %u section index %u " | |
588 | "out of range\n"), | |
589 | program_name, this->name().c_str(), i, shndx); | |
590 | gold_exit(false); | |
591 | } | |
592 | ||
593 | if (mo[shndx].output_section == NULL) | |
61ba1cf9 | 594 | { |
c06b7b0b ILT |
595 | this->local_values_[i] = 0; |
596 | this->local_indexes_[i] = -1U; | |
61ba1cf9 ILT |
597 | continue; |
598 | } | |
599 | ||
c06b7b0b ILT |
600 | this->local_values_[i] = (mo[shndx].output_section->address() |
601 | + mo[shndx].offset | |
602 | + sym.get_st_value()); | |
75f65a3e ILT |
603 | } |
604 | ||
c06b7b0b ILT |
605 | // Decide whether this symbol should go into the output file. |
606 | ||
607 | if (sym.get_st_type() == elfcpp::STT_SECTION) | |
f6ce93d6 | 608 | { |
c06b7b0b ILT |
609 | this->local_indexes_[i] = -1U; |
610 | continue; | |
611 | } | |
645f8123 | 612 | |
c06b7b0b ILT |
613 | if (sym.get_st_name() >= strtab_size) |
614 | { | |
615 | fprintf(stderr, | |
616 | _("%s: %s: local symbol %u section name " | |
617 | "out of range: %u >= %u\n"), | |
618 | program_name, this->name().c_str(), | |
619 | i, sym.get_st_name(), | |
620 | static_cast<unsigned int>(strtab_size)); | |
621 | gold_exit(false); | |
f6ce93d6 | 622 | } |
c06b7b0b ILT |
623 | |
624 | const char* name = pnames + sym.get_st_name(); | |
625 | pool->add(name, NULL); | |
626 | this->local_indexes_[i] = index; | |
627 | ++index; | |
628 | off += sym_size; | |
629 | ++count; | |
75f65a3e ILT |
630 | } |
631 | ||
61ba1cf9 ILT |
632 | this->output_local_symbol_count_ = count; |
633 | ||
c06b7b0b | 634 | return index; |
75f65a3e ILT |
635 | } |
636 | ||
61ba1cf9 ILT |
637 | // Write out the local symbols. |
638 | ||
639 | template<int size, bool big_endian> | |
640 | void | |
f6ce93d6 | 641 | Sized_relobj<size, big_endian>::write_local_symbols(Output_file* of, |
61ba1cf9 ILT |
642 | const Stringpool* sympool) |
643 | { | |
dbe717ef | 644 | assert(this->symtab_shndx_ != -1U); |
645f8123 | 645 | if (this->symtab_shndx_ == 0) |
61ba1cf9 ILT |
646 | { |
647 | // This object has no symbols. Weird but legal. | |
648 | return; | |
649 | } | |
650 | ||
651 | // Read the symbol table section header. | |
645f8123 ILT |
652 | const unsigned int symtab_shndx = this->symtab_shndx_; |
653 | typename This::Shdr symtabshdr(this, | |
654 | this->elf_file_.section_header(symtab_shndx)); | |
61ba1cf9 | 655 | assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); |
92e059d8 ILT |
656 | const unsigned int loccount = this->local_symbol_count_; |
657 | assert(loccount == symtabshdr.get_sh_info()); | |
61ba1cf9 ILT |
658 | |
659 | // Read the local symbols. | |
660 | const int sym_size = This::sym_size; | |
92e059d8 | 661 | off_t locsize = loccount * sym_size; |
61ba1cf9 ILT |
662 | const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(), |
663 | locsize); | |
664 | ||
61ba1cf9 | 665 | // Read the symbol names. |
645f8123 ILT |
666 | const unsigned int strtab_shndx = symtabshdr.get_sh_link(); |
667 | off_t strtab_size; | |
668 | const unsigned char* pnamesu = this->section_contents(strtab_shndx, | |
669 | &strtab_size); | |
61ba1cf9 ILT |
670 | const char* pnames = reinterpret_cast<const char*>(pnamesu); |
671 | ||
672 | // Get a view into the output file. | |
673 | off_t output_size = this->output_local_symbol_count_ * sym_size; | |
674 | unsigned char* oview = of->get_output_view(this->local_symbol_offset_, | |
675 | output_size); | |
676 | ||
c06b7b0b ILT |
677 | const std::vector<Map_to_output>& mo(this->map_to_output()); |
678 | ||
679 | assert(this->local_values_.size() == loccount); | |
680 | assert(this->local_indexes_.size() == loccount); | |
61ba1cf9 | 681 | |
61ba1cf9 | 682 | unsigned char* ov = oview; |
c06b7b0b | 683 | psyms += sym_size; |
92e059d8 | 684 | for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size) |
61ba1cf9 ILT |
685 | { |
686 | elfcpp::Sym<size, big_endian> isym(psyms); | |
f6ce93d6 | 687 | |
c06b7b0b | 688 | if (this->local_indexes_[i] == -1U) |
f6ce93d6 | 689 | continue; |
c06b7b0b | 690 | assert(this->local_indexes_[i] != 0); |
61ba1cf9 ILT |
691 | |
692 | unsigned int st_shndx = isym.get_st_shndx(); | |
693 | if (st_shndx < elfcpp::SHN_LORESERVE) | |
694 | { | |
695 | assert(st_shndx < mo.size()); | |
696 | if (mo[st_shndx].output_section == NULL) | |
697 | continue; | |
ead1e424 | 698 | st_shndx = mo[st_shndx].output_section->out_shndx(); |
61ba1cf9 ILT |
699 | } |
700 | ||
f6ce93d6 ILT |
701 | elfcpp::Sym_write<size, big_endian> osym(ov); |
702 | ||
645f8123 | 703 | assert(isym.get_st_name() < strtab_size); |
c06b7b0b ILT |
704 | const char* name = pnames + isym.get_st_name(); |
705 | osym.put_st_name(sympool->get_offset(name)); | |
706 | osym.put_st_value(this->local_values_[i]); | |
61ba1cf9 ILT |
707 | osym.put_st_size(isym.get_st_size()); |
708 | osym.put_st_info(isym.get_st_info()); | |
709 | osym.put_st_other(isym.get_st_other()); | |
710 | osym.put_st_shndx(st_shndx); | |
711 | ||
712 | ov += sym_size; | |
713 | } | |
714 | ||
715 | assert(ov - oview == output_size); | |
716 | ||
717 | of->write_output_view(this->local_symbol_offset_, output_size, oview); | |
718 | } | |
719 | ||
54dc6425 ILT |
720 | // Input_objects methods. |
721 | ||
f6ce93d6 ILT |
722 | // Add a regular relocatable object to the list. |
723 | ||
54dc6425 ILT |
724 | void |
725 | Input_objects::add_object(Object* obj) | |
726 | { | |
f6ce93d6 ILT |
727 | if (obj->is_dynamic()) |
728 | this->dynobj_list_.push_back(static_cast<Dynobj*>(obj)); | |
729 | else | |
730 | this->relobj_list_.push_back(static_cast<Relobj*>(obj)); | |
75f65a3e ILT |
731 | |
732 | Target* target = obj->target(); | |
733 | if (this->target_ == NULL) | |
734 | this->target_ = target; | |
735 | else if (this->target_ != target) | |
736 | { | |
737 | fprintf(stderr, "%s: %s: incompatible target\n", | |
738 | program_name, obj->name().c_str()); | |
739 | gold_exit(false); | |
740 | } | |
54dc6425 ILT |
741 | } |
742 | ||
92e059d8 ILT |
743 | // Relocate_info methods. |
744 | ||
745 | // Return a string describing the location of a relocation. This is | |
746 | // only used in error messages. | |
747 | ||
748 | template<int size, bool big_endian> | |
749 | std::string | |
750 | Relocate_info<size, big_endian>::location(size_t relnum, off_t) const | |
751 | { | |
752 | std::string ret(this->object->name()); | |
753 | ret += ": reloc "; | |
754 | char buf[100]; | |
755 | snprintf(buf, sizeof buf, "%zu", relnum); | |
756 | ret += buf; | |
757 | ret += " in reloc section "; | |
758 | snprintf(buf, sizeof buf, "%u", this->reloc_shndx); | |
759 | ret += buf; | |
760 | ret += " (" + this->object->section_name(this->reloc_shndx); | |
761 | ret += ") for section "; | |
762 | snprintf(buf, sizeof buf, "%u", this->data_shndx); | |
763 | ret += buf; | |
764 | ret += " (" + this->object->section_name(this->data_shndx) + ")"; | |
765 | return ret; | |
766 | } | |
767 | ||
bae7f79e ILT |
768 | } // End namespace gold. |
769 | ||
770 | namespace | |
771 | { | |
772 | ||
773 | using namespace gold; | |
774 | ||
775 | // Read an ELF file with the header and return the appropriate | |
776 | // instance of Object. | |
777 | ||
778 | template<int size, bool big_endian> | |
779 | Object* | |
780 | make_elf_sized_object(const std::string& name, Input_file* input_file, | |
781 | off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr) | |
782 | { | |
783 | int et = ehdr.get_e_type(); | |
bae7f79e ILT |
784 | if (et == elfcpp::ET_REL) |
785 | { | |
f6ce93d6 ILT |
786 | Sized_relobj<size, big_endian>* obj = |
787 | new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr); | |
bae7f79e ILT |
788 | obj->setup(ehdr); |
789 | return obj; | |
790 | } | |
dbe717ef ILT |
791 | else if (et == elfcpp::ET_DYN) |
792 | { | |
793 | Sized_dynobj<size, big_endian>* obj = | |
794 | new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr); | |
795 | obj->setup(ehdr); | |
796 | return obj; | |
797 | } | |
bae7f79e ILT |
798 | else |
799 | { | |
dbe717ef ILT |
800 | fprintf(stderr, _("%s: %s: unsupported ELF file type %d\n"), |
801 | program_name, name.c_str(), et); | |
bae7f79e | 802 | gold_exit(false); |
bae7f79e ILT |
803 | } |
804 | } | |
805 | ||
806 | } // End anonymous namespace. | |
807 | ||
808 | namespace gold | |
809 | { | |
810 | ||
811 | // Read an ELF file and return the appropriate instance of Object. | |
812 | ||
813 | Object* | |
814 | make_elf_object(const std::string& name, Input_file* input_file, off_t offset, | |
815 | const unsigned char* p, off_t bytes) | |
816 | { | |
817 | if (bytes < elfcpp::EI_NIDENT) | |
818 | { | |
819 | fprintf(stderr, _("%s: %s: ELF file too short\n"), | |
820 | program_name, name.c_str()); | |
821 | gold_exit(false); | |
822 | } | |
823 | ||
824 | int v = p[elfcpp::EI_VERSION]; | |
825 | if (v != elfcpp::EV_CURRENT) | |
826 | { | |
827 | if (v == elfcpp::EV_NONE) | |
828 | fprintf(stderr, _("%s: %s: invalid ELF version 0\n"), | |
829 | program_name, name.c_str()); | |
830 | else | |
831 | fprintf(stderr, _("%s: %s: unsupported ELF version %d\n"), | |
832 | program_name, name.c_str(), v); | |
833 | gold_exit(false); | |
834 | } | |
835 | ||
836 | int c = p[elfcpp::EI_CLASS]; | |
837 | if (c == elfcpp::ELFCLASSNONE) | |
838 | { | |
839 | fprintf(stderr, _("%s: %s: invalid ELF class 0\n"), | |
840 | program_name, name.c_str()); | |
841 | gold_exit(false); | |
842 | } | |
843 | else if (c != elfcpp::ELFCLASS32 | |
844 | && c != elfcpp::ELFCLASS64) | |
845 | { | |
846 | fprintf(stderr, _("%s: %s: unsupported ELF class %d\n"), | |
847 | program_name, name.c_str(), c); | |
848 | gold_exit(false); | |
849 | } | |
850 | ||
851 | int d = p[elfcpp::EI_DATA]; | |
852 | if (d == elfcpp::ELFDATANONE) | |
853 | { | |
854 | fprintf(stderr, _("%s: %s: invalid ELF data encoding\n"), | |
855 | program_name, name.c_str()); | |
856 | gold_exit(false); | |
857 | } | |
858 | else if (d != elfcpp::ELFDATA2LSB | |
859 | && d != elfcpp::ELFDATA2MSB) | |
860 | { | |
861 | fprintf(stderr, _("%s: %s: unsupported ELF data encoding %d\n"), | |
862 | program_name, name.c_str(), d); | |
863 | gold_exit(false); | |
864 | } | |
865 | ||
866 | bool big_endian = d == elfcpp::ELFDATA2MSB; | |
867 | ||
868 | if (c == elfcpp::ELFCLASS32) | |
869 | { | |
870 | if (bytes < elfcpp::Elf_sizes<32>::ehdr_size) | |
871 | { | |
872 | fprintf(stderr, _("%s: %s: ELF file too short\n"), | |
873 | program_name, name.c_str()); | |
874 | gold_exit(false); | |
875 | } | |
876 | if (big_endian) | |
877 | { | |
878 | elfcpp::Ehdr<32, true> ehdr(p); | |
879 | return make_elf_sized_object<32, true>(name, input_file, | |
880 | offset, ehdr); | |
881 | } | |
882 | else | |
883 | { | |
884 | elfcpp::Ehdr<32, false> ehdr(p); | |
885 | return make_elf_sized_object<32, false>(name, input_file, | |
886 | offset, ehdr); | |
887 | } | |
888 | } | |
889 | else | |
890 | { | |
891 | if (bytes < elfcpp::Elf_sizes<32>::ehdr_size) | |
892 | { | |
893 | fprintf(stderr, _("%s: %s: ELF file too short\n"), | |
894 | program_name, name.c_str()); | |
895 | gold_exit(false); | |
896 | } | |
897 | if (big_endian) | |
898 | { | |
899 | elfcpp::Ehdr<64, true> ehdr(p); | |
900 | return make_elf_sized_object<64, true>(name, input_file, | |
901 | offset, ehdr); | |
902 | } | |
903 | else | |
904 | { | |
905 | elfcpp::Ehdr<64, false> ehdr(p); | |
906 | return make_elf_sized_object<64, false>(name, input_file, | |
907 | offset, ehdr); | |
908 | } | |
909 | } | |
910 | } | |
911 | ||
912 | // Instantiate the templates we need. We could use the configure | |
913 | // script to restrict this to only the ones for implemented targets. | |
914 | ||
915 | template | |
f6ce93d6 | 916 | class Sized_relobj<32, false>; |
bae7f79e ILT |
917 | |
918 | template | |
f6ce93d6 | 919 | class Sized_relobj<32, true>; |
bae7f79e ILT |
920 | |
921 | template | |
f6ce93d6 | 922 | class Sized_relobj<64, false>; |
bae7f79e ILT |
923 | |
924 | template | |
f6ce93d6 | 925 | class Sized_relobj<64, true>; |
bae7f79e | 926 | |
92e059d8 ILT |
927 | template |
928 | struct Relocate_info<32, false>; | |
929 | ||
930 | template | |
931 | struct Relocate_info<32, true>; | |
932 | ||
933 | template | |
934 | struct Relocate_info<64, false>; | |
935 | ||
936 | template | |
937 | struct Relocate_info<64, true>; | |
938 | ||
bae7f79e | 939 | } // End namespace gold. |