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> | |
8 | ||
9 | #include "object.h" | |
14bfc3f5 | 10 | #include "target-select.h" |
a2fb1b05 | 11 | #include "layout.h" |
bae7f79e ILT |
12 | |
13 | namespace gold | |
14 | { | |
15 | ||
16 | // Class Object. | |
17 | ||
18 | const unsigned char* | |
19 | Object::get_view(off_t start, off_t size) | |
20 | { | |
21 | return this->input_file_->file().get_view(start + this->offset_, size); | |
22 | } | |
23 | ||
24 | void | |
25 | Object::read(off_t start, off_t size, void* p) | |
26 | { | |
27 | this->input_file_->file().read(start + this->offset_, size, p); | |
28 | } | |
29 | ||
30 | File_view* | |
31 | Object::get_lasting_view(off_t start, off_t size) | |
32 | { | |
33 | return this->input_file_->file().get_lasting_view(start + this->offset_, | |
34 | size); | |
35 | } | |
36 | ||
37 | // Class Sized_object. | |
38 | ||
39 | template<int size, bool big_endian> | |
40 | Sized_object<size, big_endian>::Sized_object( | |
41 | const std::string& name, | |
42 | Input_file* input_file, | |
43 | off_t offset, | |
44 | const elfcpp::Ehdr<size, big_endian>& ehdr) | |
14bfc3f5 | 45 | : Object(name, input_file, false, offset), |
bae7f79e | 46 | flags_(ehdr.get_e_flags()), |
bae7f79e | 47 | shoff_(ehdr.get_e_shoff()), |
bae7f79e | 48 | shstrndx_(0), |
14bfc3f5 | 49 | symtab_shnum_(0), |
75f65a3e ILT |
50 | symbols_(NULL), |
51 | local_symbol_offset_(0) | |
bae7f79e | 52 | { |
a2fb1b05 | 53 | if (ehdr.get_e_ehsize() != This::ehdr_size) |
bae7f79e ILT |
54 | { |
55 | fprintf(stderr, _("%s: %s: bad e_ehsize field (%d != %d)\n"), | |
56 | program_name, this->name().c_str(), ehdr.get_e_ehsize(), | |
a2fb1b05 | 57 | This::ehdr_size); |
bae7f79e ILT |
58 | gold_exit(false); |
59 | } | |
a2fb1b05 | 60 | if (ehdr.get_e_shentsize() != This::shdr_size) |
bae7f79e ILT |
61 | { |
62 | fprintf(stderr, _("%s: %s: bad e_shentsize field (%d != %d)\n"), | |
63 | program_name, this->name().c_str(), ehdr.get_e_shentsize(), | |
a2fb1b05 | 64 | This::shdr_size); |
bae7f79e ILT |
65 | gold_exit(false); |
66 | } | |
67 | } | |
68 | ||
69 | template<int size, bool big_endian> | |
70 | Sized_object<size, big_endian>::~Sized_object() | |
71 | { | |
72 | } | |
73 | ||
75f65a3e ILT |
74 | // Read the section header for section SHNUM. |
75 | ||
76 | template<int size, bool big_endian> | |
77 | const unsigned char* | |
78 | Sized_object<size, big_endian>::section_header(unsigned int shnum) | |
79 | { | |
80 | off_t symtabshdroff = this->shoff_ + shnum * This::shdr_size; | |
81 | return this->get_view(symtabshdroff, This::shdr_size); | |
82 | } | |
83 | ||
bae7f79e ILT |
84 | // Set up an object file bsaed on the file header. This sets up the |
85 | // target and reads the section information. | |
86 | ||
87 | template<int size, bool big_endian> | |
88 | void | |
89 | Sized_object<size, big_endian>::setup( | |
90 | const elfcpp::Ehdr<size, big_endian>& ehdr) | |
91 | { | |
a2fb1b05 ILT |
92 | int machine = ehdr.get_e_machine(); |
93 | Target* target = select_target(machine, size, big_endian, | |
94 | ehdr.get_e_ident()[elfcpp::EI_OSABI], | |
95 | ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]); | |
14bfc3f5 ILT |
96 | if (target == NULL) |
97 | { | |
98 | fprintf(stderr, _("%s: %s: unsupported ELF machine number %d\n"), | |
a2fb1b05 | 99 | program_name, this->name().c_str(), machine); |
14bfc3f5 ILT |
100 | gold_exit(false); |
101 | } | |
102 | this->set_target(target); | |
bae7f79e ILT |
103 | unsigned int shnum = ehdr.get_e_shnum(); |
104 | unsigned int shstrndx = ehdr.get_e_shstrndx(); | |
105 | if ((shnum == 0 || shstrndx == elfcpp::SHN_XINDEX) | |
106 | && this->shoff_ != 0) | |
107 | { | |
75f65a3e | 108 | typename This::Shdr shdr(this->section_header(0)); |
bae7f79e ILT |
109 | if (shnum == 0) |
110 | shnum = shdr.get_sh_size(); | |
111 | if (shstrndx == elfcpp::SHN_XINDEX) | |
112 | shstrndx = shdr.get_sh_link(); | |
113 | } | |
a2fb1b05 | 114 | this->set_shnum(shnum); |
bae7f79e ILT |
115 | this->shstrndx_ = shstrndx; |
116 | ||
117 | if (shnum == 0) | |
118 | return; | |
119 | ||
120 | // Find the SHT_SYMTAB section. | |
a2fb1b05 ILT |
121 | const unsigned char* p = this->get_view (this->shoff_, |
122 | shnum * This::shdr_size); | |
bae7f79e | 123 | // Skip the first section, which is always empty. |
a2fb1b05 | 124 | p += This::shdr_size; |
bae7f79e ILT |
125 | for (unsigned int i = 1; i < shnum; ++i) |
126 | { | |
75f65a3e | 127 | typename This::Shdr shdr(p); |
bae7f79e ILT |
128 | if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB) |
129 | { | |
130 | this->symtab_shnum_ = i; | |
131 | break; | |
132 | } | |
a2fb1b05 | 133 | p += This::shdr_size; |
bae7f79e ILT |
134 | } |
135 | } | |
136 | ||
137 | // Read the symbols and relocations from an object file. | |
138 | ||
139 | template<int size, bool big_endian> | |
140 | Read_symbols_data | |
141 | Sized_object<size, big_endian>::do_read_symbols() | |
142 | { | |
143 | if (this->symtab_shnum_ == 0) | |
144 | { | |
145 | // No symbol table. Weird but legal. | |
146 | Read_symbols_data ret; | |
147 | ret.symbols = NULL; | |
148 | ret.symbols_size = 0; | |
149 | ret.symbol_names = NULL; | |
150 | ret.symbol_names_size = 0; | |
151 | return ret; | |
152 | } | |
153 | ||
bae7f79e | 154 | // Read the symbol table section header. |
75f65a3e | 155 | typename This::Shdr symtabshdr(this->section_header(this->symtab_shnum_)); |
bae7f79e ILT |
156 | assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); |
157 | ||
75f65a3e ILT |
158 | // We only need the external symbols. |
159 | const int sym_size = This::sym_size; | |
160 | off_t locsize = symtabshdr.get_sh_info() * sym_size; | |
161 | off_t extoff = symtabshdr.get_sh_offset() + locsize; | |
162 | off_t extsize = symtabshdr.get_sh_size() - locsize; | |
163 | ||
bae7f79e | 164 | // Read the symbol table. |
75f65a3e | 165 | File_view* fvsymtab = this->get_lasting_view(extoff, extsize); |
bae7f79e ILT |
166 | |
167 | // Read the section header for the symbol names. | |
168 | unsigned int strtab_shnum = symtabshdr.get_sh_link(); | |
a2fb1b05 | 169 | if (strtab_shnum == 0 || strtab_shnum >= this->shnum()) |
bae7f79e ILT |
170 | { |
171 | fprintf(stderr, _("%s: %s: invalid symbol table name index: %u\n"), | |
172 | program_name, this->name().c_str(), strtab_shnum); | |
173 | gold_exit(false); | |
174 | } | |
75f65a3e | 175 | typename This::Shdr strtabshdr(this->section_header(strtab_shnum)); |
bae7f79e ILT |
176 | if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB) |
177 | { | |
178 | fprintf(stderr, | |
179 | _("%s: %s: symbol table name section has wrong type: %u\n"), | |
180 | program_name, this->name().c_str(), | |
181 | static_cast<unsigned int>(strtabshdr.get_sh_type())); | |
182 | gold_exit(false); | |
183 | } | |
184 | ||
185 | // Read the symbol names. | |
186 | File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(), | |
187 | strtabshdr.get_sh_size()); | |
188 | ||
189 | Read_symbols_data ret; | |
190 | ret.symbols = fvsymtab; | |
75f65a3e | 191 | ret.symbols_size = extsize; |
bae7f79e ILT |
192 | ret.symbol_names = fvstrtab; |
193 | ret.symbol_names_size = strtabshdr.get_sh_size(); | |
194 | ||
195 | return ret; | |
196 | } | |
197 | ||
198 | // Add the symbols to the symbol table. | |
199 | ||
200 | template<int size, bool big_endian> | |
201 | void | |
14bfc3f5 ILT |
202 | Sized_object<size, big_endian>::do_add_symbols(Symbol_table* symtab, |
203 | Read_symbols_data sd) | |
bae7f79e ILT |
204 | { |
205 | if (sd.symbols == NULL) | |
206 | { | |
207 | assert(sd.symbol_names == NULL); | |
208 | return; | |
209 | } | |
210 | ||
a2fb1b05 | 211 | const int sym_size = This::sym_size; |
14bfc3f5 ILT |
212 | size_t symcount = sd.symbols_size / sym_size; |
213 | if (symcount * sym_size != sd.symbols_size) | |
bae7f79e | 214 | { |
14bfc3f5 ILT |
215 | fprintf(stderr, |
216 | _("%s: %s: size of symbols is not multiple of symbol size\n"), | |
217 | program_name, this->name().c_str()); | |
218 | gold_exit(false); | |
bae7f79e | 219 | } |
14bfc3f5 | 220 | |
75f65a3e | 221 | this->symbols_ = new Symbol*[symcount]; |
54dc6425 | 222 | |
75f65a3e | 223 | const elfcpp::Sym<size, big_endian>* syms = |
54dc6425 | 224 | reinterpret_cast<const elfcpp::Sym<size, big_endian>*>(sd.symbols->data()); |
75f65a3e ILT |
225 | const char* sym_names = |
226 | reinterpret_cast<const char*>(sd.symbol_names->data()); | |
227 | symtab->add_from_object(this, syms, symcount, sym_names, | |
228 | sd.symbol_names_size, this->symbols_); | |
a2fb1b05 ILT |
229 | |
230 | delete sd.symbols; | |
231 | delete sd.symbol_names; | |
232 | } | |
233 | ||
234 | // Return whether to include a section group in the link. LAYOUT is | |
235 | // used to keep track of which section groups we have already seen. | |
236 | // INDEX is the index of the section group and SHDR is the section | |
237 | // header. If we do not want to include this group, we set bits in | |
238 | // OMIT for each section which should be discarded. | |
239 | ||
240 | template<int size, bool big_endian> | |
241 | bool | |
242 | Sized_object<size, big_endian>::include_section_group( | |
243 | Layout* layout, | |
244 | unsigned int index, | |
245 | const elfcpp::Shdr<size, big_endian>& shdr, | |
246 | std::vector<bool>* omit) | |
247 | { | |
248 | // Read the section contents. | |
249 | const unsigned char* pcon = this->get_view(shdr.get_sh_offset(), | |
250 | shdr.get_sh_size()); | |
251 | const elfcpp::Elf_Word* pword = | |
252 | reinterpret_cast<const elfcpp::Elf_Word*>(pcon); | |
253 | ||
254 | // The first word contains flags. We only care about COMDAT section | |
255 | // groups. Other section groups are always included in the link | |
256 | // just like ordinary sections. | |
257 | elfcpp::Elf_Word flags = elfcpp::read_elf_word<big_endian>(pword); | |
258 | if ((flags & elfcpp::GRP_COMDAT) == 0) | |
259 | return true; | |
260 | ||
261 | // Look up the group signature, which is the name of a symbol. This | |
262 | // is a lot of effort to go to to read a string. Why didn't they | |
263 | // just use the name of the SHT_GROUP section as the group | |
264 | // signature? | |
265 | ||
266 | // Get the appropriate symbol table header (this will normally be | |
267 | // the single SHT_SYMTAB section, but in principle it need not be). | |
268 | if (shdr.get_sh_link() >= this->shnum()) | |
269 | { | |
270 | fprintf(stderr, _("%s: %s: section group %u link %u out of range\n"), | |
271 | program_name, this->name().c_str(), index, shdr.get_sh_link()); | |
272 | gold_exit(false); | |
273 | } | |
75f65a3e ILT |
274 | |
275 | typename This::Shdr symshdr(this->section_header(shdr.get_sh_link())); | |
a2fb1b05 ILT |
276 | |
277 | // Read the symbol table entry. | |
278 | if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size) | |
279 | { | |
280 | fprintf(stderr, _("%s: %s: section group %u info %u out of range\n"), | |
281 | program_name, this->name().c_str(), index, shdr.get_sh_info()); | |
282 | gold_exit(false); | |
283 | } | |
284 | off_t symoff = symshdr.get_sh_offset() + shdr.get_sh_info() * This::sym_size; | |
285 | const unsigned char* psym = this->get_view(symoff, This::sym_size); | |
286 | elfcpp::Sym<size, big_endian> sym(psym); | |
287 | ||
288 | // Read the section header for the symbol table names. | |
289 | if (symshdr.get_sh_link() >= this->shnum()) | |
290 | { | |
291 | fprintf(stderr, _("%s; %s: symtab section %u link %u out of range\n"), | |
292 | program_name, this->name().c_str(), shdr.get_sh_link(), | |
293 | symshdr.get_sh_link()); | |
294 | gold_exit(false); | |
295 | } | |
75f65a3e ILT |
296 | |
297 | typename This::Shdr symnamehdr(this->section_header(symshdr.get_sh_link())); | |
a2fb1b05 ILT |
298 | |
299 | // Read the symbol table names. | |
300 | const unsigned char *psymnamesu = this->get_view(symnamehdr.get_sh_offset(), | |
301 | symnamehdr.get_sh_size()); | |
302 | const char* psymnames = reinterpret_cast<const char*>(psymnamesu); | |
303 | ||
304 | // Get the section group signature. | |
305 | if (sym.get_st_name() >= symnamehdr.get_sh_size()) | |
306 | { | |
307 | fprintf(stderr, _("%s: %s: symbol %u name offset %u out of range\n"), | |
308 | program_name, this->name().c_str(), shdr.get_sh_info(), | |
309 | sym.get_st_name()); | |
310 | gold_exit(false); | |
311 | } | |
312 | ||
313 | const char* signature = psymnames + sym.get_st_name(); | |
314 | ||
315 | // Record this section group, and see whether we've already seen one | |
316 | // with the same signature. | |
317 | if (layout->add_comdat(signature, true)) | |
318 | return true; | |
319 | ||
320 | // This is a duplicate. We want to discard the sections in this | |
321 | // group. | |
322 | size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word); | |
323 | for (size_t i = 1; i < count; ++i) | |
324 | { | |
325 | elfcpp::Elf_Word secnum = elfcpp::read_elf_word<big_endian>(pword + i); | |
326 | if (secnum >= this->shnum()) | |
327 | { | |
328 | fprintf(stderr, | |
329 | _("%s: %s: section %u in section group %u out of range"), | |
330 | program_name, this->name().c_str(), secnum, | |
331 | index); | |
332 | gold_exit(false); | |
333 | } | |
334 | (*omit)[secnum] = true; | |
335 | } | |
336 | ||
337 | return false; | |
338 | } | |
339 | ||
340 | // Whether to include a linkonce section in the link. NAME is the | |
341 | // name of the section and SHDR is the section header. | |
342 | ||
343 | // Linkonce sections are a GNU extension implemented in the original | |
344 | // GNU linker before section groups were defined. The semantics are | |
345 | // that we only include one linkonce section with a given name. The | |
346 | // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME, | |
347 | // where T is the type of section and SYMNAME is the name of a symbol. | |
348 | // In an attempt to make linkonce sections interact well with section | |
349 | // groups, we try to identify SYMNAME and use it like a section group | |
350 | // signature. We want to block section groups with that signature, | |
351 | // but not other linkonce sections with that signature. We also use | |
352 | // the full name of the linkonce section as a normal section group | |
353 | // signature. | |
354 | ||
355 | template<int size, bool big_endian> | |
356 | bool | |
357 | Sized_object<size, big_endian>::include_linkonce_section( | |
358 | Layout* layout, | |
359 | const char* name, | |
360 | const elfcpp::Shdr<size, big_endian>&) | |
361 | { | |
362 | const char* symname = strrchr(name, '.') + 1; | |
363 | bool omit1 = layout->add_comdat(symname, false); | |
364 | bool omit2 = layout->add_comdat(name, true); | |
365 | return omit1 || omit2; | |
366 | } | |
367 | ||
368 | // Lay out the input sections. We walk through the sections and check | |
369 | // whether they should be included in the link. If they should, we | |
370 | // pass them to the Layout object, which will return an output section | |
371 | // and an offset. | |
372 | ||
373 | template<int size, bool big_endian> | |
374 | void | |
375 | Sized_object<size, big_endian>::do_layout(Layout* layout) | |
376 | { | |
377 | // This is always called from the main thread. Lock the file to | |
378 | // keep the error checks happy. | |
379 | Task_locker_obj<File_read> frl(this->input_file()->file()); | |
380 | ||
381 | // Get the section headers. | |
382 | unsigned int shnum = this->shnum(); | |
383 | const unsigned char* pshdrs = this->get_view(this->shoff_, | |
384 | shnum * This::shdr_size); | |
385 | ||
386 | // Get the section names. | |
387 | const unsigned char* pshdrnames = pshdrs + this->shstrndx_ * This::shdr_size; | |
75f65a3e | 388 | typename This::Shdr shdrnames(pshdrnames); |
a2fb1b05 ILT |
389 | typename elfcpp::Elf_types<size>::Elf_WXword names_size = |
390 | shdrnames.get_sh_size(); | |
391 | const unsigned char* pnamesu = this->get_view(shdrnames.get_sh_offset(), | |
392 | shdrnames.get_sh_size()); | |
393 | const char* pnames = reinterpret_cast<const char*>(pnamesu); | |
394 | ||
395 | std::vector<Map_to_output>& map_sections(this->map_to_output()); | |
396 | map_sections.reserve(shnum); | |
397 | ||
398 | // Keep track of which sections to omit. | |
399 | std::vector<bool> omit(shnum, false); | |
400 | ||
401 | for (unsigned int i = 0; i < shnum; ++i) | |
402 | { | |
75f65a3e | 403 | typename This::Shdr shdr(pshdrs); |
a2fb1b05 ILT |
404 | |
405 | if (shdr.get_sh_name() >= names_size) | |
406 | { | |
407 | fprintf(stderr, | |
408 | _("%s: %s: bad section name offset for section %u: %lu\n"), | |
409 | program_name, this->name().c_str(), i, | |
410 | static_cast<unsigned long>(shdr.get_sh_name())); | |
411 | gold_exit(false); | |
412 | } | |
413 | ||
414 | const char* name = pnames + shdr.get_sh_name(); | |
415 | ||
416 | bool discard = omit[i]; | |
417 | if (!discard) | |
418 | { | |
419 | if (shdr.get_sh_type() == elfcpp::SHT_GROUP) | |
420 | { | |
421 | if (!this->include_section_group(layout, i, shdr, &omit)) | |
422 | discard = true; | |
423 | } | |
424 | else if (Layout::is_linkonce(name)) | |
425 | { | |
426 | if (!this->include_linkonce_section(layout, name, shdr)) | |
427 | discard = true; | |
428 | } | |
429 | } | |
430 | ||
431 | if (discard) | |
432 | { | |
433 | // Do not include this section in the link. | |
434 | map_sections[i].output_section = NULL; | |
435 | continue; | |
436 | } | |
437 | ||
438 | off_t offset; | |
439 | Output_section* os = layout->layout(this, name, shdr, &offset); | |
440 | ||
441 | map_sections[i].output_section = os; | |
442 | map_sections[i].offset = offset; | |
443 | ||
444 | pshdrs += This::shdr_size; | |
445 | } | |
bae7f79e ILT |
446 | } |
447 | ||
75f65a3e ILT |
448 | // Finalize the local symbols. Here we record the file offset at |
449 | // which they should be output and we add their names to *POOL. | |
450 | // Return the new file offset. This function is always called from | |
451 | // the main thread. The actual output of the local symbols will occur | |
452 | // in a separate task. | |
453 | ||
454 | template<int size, bool big_endian> | |
455 | off_t | |
456 | Sized_object<size, big_endian>::do_finalize_local_symbols(off_t off, | |
457 | Stringpool* pool) | |
458 | { | |
459 | this->local_symbol_offset_ = off; | |
460 | ||
461 | // Read the symbol table section header. | |
462 | typename This::Shdr symtabshdr(this->section_header(this->symtab_shnum_)); | |
463 | assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); | |
464 | ||
465 | // Read the local symbols. | |
466 | unsigned int loccount = symtabshdr.get_sh_info(); | |
467 | const int sym_size = This::sym_size; | |
468 | off_t locsize = loccount * sym_size; | |
469 | const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(), | |
470 | locsize); | |
471 | ||
472 | // Read the section header for the symbol names. | |
473 | typename This::Shdr strtabshdr( | |
474 | this->section_header(symtabshdr.get_sh_link())); | |
475 | assert(strtabshdr.get_sh_type() == elfcpp::SHT_STRTAB); | |
476 | ||
477 | // Read the symbol names. | |
478 | const unsigned char* pnamesu = this->get_view(strtabshdr.get_sh_offset(), | |
479 | strtabshdr.get_sh_size()); | |
480 | const char* pnames = reinterpret_cast<const char*>(pnamesu); | |
481 | ||
482 | // Loop over the local symbols. | |
483 | ||
484 | std::vector<Map_to_output>& mo(this->map_to_output()); | |
485 | unsigned int shnum = this->shnum(); | |
486 | // Skip the first, dummy, symbol. | |
487 | psyms += sym_size; | |
488 | for (unsigned int i = 1; i < loccount; ++i) | |
489 | { | |
490 | elfcpp::Sym<size, big_endian> sym(psyms); | |
491 | ||
492 | unsigned int shndx = sym.get_st_shndx(); | |
493 | ||
494 | if (shndx >= elfcpp::SHN_LORESERVE) | |
495 | { | |
496 | if (shndx != elfcpp::SHN_ABS) | |
497 | { | |
498 | fprintf(stderr, | |
499 | _("%s: %s: unknown section index %u " | |
500 | "for local symbol %u\n"), | |
501 | program_name, this->name().c_str(), shndx, i); | |
502 | gold_exit(false); | |
503 | } | |
504 | // FIXME: Handle SHN_XINDEX. | |
505 | } | |
506 | else | |
507 | { | |
508 | if (shndx >= shnum) | |
509 | { | |
510 | fprintf(stderr, | |
511 | _("%s: %s: local symbol %u section index %u " | |
512 | "out of range\n"), | |
513 | program_name, this->name().c_str(), i, shndx); | |
514 | gold_exit(false); | |
515 | } | |
516 | ||
517 | if (mo[shndx].output_section == NULL) | |
518 | continue; | |
519 | } | |
520 | ||
521 | pool->add(pnames + sym.get_st_name()); | |
522 | off += sym_size; | |
523 | ||
524 | psyms += sym_size; | |
525 | } | |
526 | ||
527 | return off; | |
528 | } | |
529 | ||
54dc6425 ILT |
530 | // Input_objects methods. |
531 | ||
532 | void | |
533 | Input_objects::add_object(Object* obj) | |
534 | { | |
535 | this->object_list_.push_back(obj); | |
75f65a3e ILT |
536 | |
537 | Target* target = obj->target(); | |
538 | if (this->target_ == NULL) | |
539 | this->target_ = target; | |
540 | else if (this->target_ != target) | |
541 | { | |
542 | fprintf(stderr, "%s: %s: incompatible target\n", | |
543 | program_name, obj->name().c_str()); | |
544 | gold_exit(false); | |
545 | } | |
546 | ||
54dc6425 ILT |
547 | if (obj->is_dynamic()) |
548 | this->any_dynamic_ = true; | |
549 | } | |
550 | ||
bae7f79e ILT |
551 | } // End namespace gold. |
552 | ||
553 | namespace | |
554 | { | |
555 | ||
556 | using namespace gold; | |
557 | ||
558 | // Read an ELF file with the header and return the appropriate | |
559 | // instance of Object. | |
560 | ||
561 | template<int size, bool big_endian> | |
562 | Object* | |
563 | make_elf_sized_object(const std::string& name, Input_file* input_file, | |
564 | off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr) | |
565 | { | |
566 | int et = ehdr.get_e_type(); | |
567 | if (et != elfcpp::ET_REL && et != elfcpp::ET_DYN) | |
568 | { | |
569 | fprintf(stderr, "%s: %s: unsupported ELF type %d\n", | |
570 | program_name, name.c_str(), static_cast<int>(et)); | |
571 | gold_exit(false); | |
572 | } | |
573 | ||
574 | if (et == elfcpp::ET_REL) | |
575 | { | |
576 | Sized_object<size, big_endian>* obj = | |
577 | new Sized_object<size, big_endian>(name, input_file, offset, ehdr); | |
578 | obj->setup(ehdr); | |
579 | return obj; | |
580 | } | |
581 | else | |
582 | { | |
583 | // elfcpp::ET_DYN | |
584 | fprintf(stderr, _("%s: %s: dynamic objects are not yet supported\n"), | |
585 | program_name, name.c_str()); | |
586 | gold_exit(false); | |
587 | // Sized_dynobj<size, big_endian>* obj = | |
588 | // new Sized_dynobj<size, big_endian>(this->input_.name(), input_file, | |
589 | // offset, ehdr); | |
590 | // obj->setup(ehdr); | |
591 | // return obj; | |
592 | } | |
593 | } | |
594 | ||
595 | } // End anonymous namespace. | |
596 | ||
597 | namespace gold | |
598 | { | |
599 | ||
600 | // Read an ELF file and return the appropriate instance of Object. | |
601 | ||
602 | Object* | |
603 | make_elf_object(const std::string& name, Input_file* input_file, off_t offset, | |
604 | const unsigned char* p, off_t bytes) | |
605 | { | |
606 | if (bytes < elfcpp::EI_NIDENT) | |
607 | { | |
608 | fprintf(stderr, _("%s: %s: ELF file too short\n"), | |
609 | program_name, name.c_str()); | |
610 | gold_exit(false); | |
611 | } | |
612 | ||
613 | int v = p[elfcpp::EI_VERSION]; | |
614 | if (v != elfcpp::EV_CURRENT) | |
615 | { | |
616 | if (v == elfcpp::EV_NONE) | |
617 | fprintf(stderr, _("%s: %s: invalid ELF version 0\n"), | |
618 | program_name, name.c_str()); | |
619 | else | |
620 | fprintf(stderr, _("%s: %s: unsupported ELF version %d\n"), | |
621 | program_name, name.c_str(), v); | |
622 | gold_exit(false); | |
623 | } | |
624 | ||
625 | int c = p[elfcpp::EI_CLASS]; | |
626 | if (c == elfcpp::ELFCLASSNONE) | |
627 | { | |
628 | fprintf(stderr, _("%s: %s: invalid ELF class 0\n"), | |
629 | program_name, name.c_str()); | |
630 | gold_exit(false); | |
631 | } | |
632 | else if (c != elfcpp::ELFCLASS32 | |
633 | && c != elfcpp::ELFCLASS64) | |
634 | { | |
635 | fprintf(stderr, _("%s: %s: unsupported ELF class %d\n"), | |
636 | program_name, name.c_str(), c); | |
637 | gold_exit(false); | |
638 | } | |
639 | ||
640 | int d = p[elfcpp::EI_DATA]; | |
641 | if (d == elfcpp::ELFDATANONE) | |
642 | { | |
643 | fprintf(stderr, _("%s: %s: invalid ELF data encoding\n"), | |
644 | program_name, name.c_str()); | |
645 | gold_exit(false); | |
646 | } | |
647 | else if (d != elfcpp::ELFDATA2LSB | |
648 | && d != elfcpp::ELFDATA2MSB) | |
649 | { | |
650 | fprintf(stderr, _("%s: %s: unsupported ELF data encoding %d\n"), | |
651 | program_name, name.c_str(), d); | |
652 | gold_exit(false); | |
653 | } | |
654 | ||
655 | bool big_endian = d == elfcpp::ELFDATA2MSB; | |
656 | ||
657 | if (c == elfcpp::ELFCLASS32) | |
658 | { | |
659 | if (bytes < elfcpp::Elf_sizes<32>::ehdr_size) | |
660 | { | |
661 | fprintf(stderr, _("%s: %s: ELF file too short\n"), | |
662 | program_name, name.c_str()); | |
663 | gold_exit(false); | |
664 | } | |
665 | if (big_endian) | |
666 | { | |
667 | elfcpp::Ehdr<32, true> ehdr(p); | |
668 | return make_elf_sized_object<32, true>(name, input_file, | |
669 | offset, ehdr); | |
670 | } | |
671 | else | |
672 | { | |
673 | elfcpp::Ehdr<32, false> ehdr(p); | |
674 | return make_elf_sized_object<32, false>(name, input_file, | |
675 | offset, ehdr); | |
676 | } | |
677 | } | |
678 | else | |
679 | { | |
680 | if (bytes < elfcpp::Elf_sizes<32>::ehdr_size) | |
681 | { | |
682 | fprintf(stderr, _("%s: %s: ELF file too short\n"), | |
683 | program_name, name.c_str()); | |
684 | gold_exit(false); | |
685 | } | |
686 | if (big_endian) | |
687 | { | |
688 | elfcpp::Ehdr<64, true> ehdr(p); | |
689 | return make_elf_sized_object<64, true>(name, input_file, | |
690 | offset, ehdr); | |
691 | } | |
692 | else | |
693 | { | |
694 | elfcpp::Ehdr<64, false> ehdr(p); | |
695 | return make_elf_sized_object<64, false>(name, input_file, | |
696 | offset, ehdr); | |
697 | } | |
698 | } | |
699 | } | |
700 | ||
701 | // Instantiate the templates we need. We could use the configure | |
702 | // script to restrict this to only the ones for implemented targets. | |
703 | ||
704 | template | |
705 | class Sized_object<32, false>; | |
706 | ||
707 | template | |
708 | class Sized_object<32, true>; | |
709 | ||
710 | template | |
711 | class Sized_object<64, false>; | |
712 | ||
713 | template | |
714 | class Sized_object<64, true>; | |
715 | ||
716 | } // End namespace gold. |