1 // object.cc -- support for an object file for linking in gold
10 #include "target-select.h"
19 Object::get_view(off_t start, off_t size)
21 return this->input_file_->file().get_view(start + this->offset_, size);
25 Object::read(off_t start, off_t size, void* p)
27 this->input_file_->file().read(start + this->offset_, size, p);
31 Object::get_lasting_view(off_t start, off_t size)
33 return this->input_file_->file().get_lasting_view(start + this->offset_,
37 // Class Sized_object.
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,
44 const elfcpp::Ehdr<size, big_endian>& ehdr)
45 : Object(name, input_file, false, offset),
46 flags_(ehdr.get_e_flags()),
47 shoff_(ehdr.get_e_shoff()),
52 if (ehdr.get_e_ehsize() != This::ehdr_size)
54 fprintf(stderr, _("%s: %s: bad e_ehsize field (%d != %d)\n"),
55 program_name, this->name().c_str(), ehdr.get_e_ehsize(),
59 if (ehdr.get_e_shentsize() != This::shdr_size)
61 fprintf(stderr, _("%s: %s: bad e_shentsize field (%d != %d)\n"),
62 program_name, this->name().c_str(), ehdr.get_e_shentsize(),
68 template<int size, bool big_endian>
69 Sized_object<size, big_endian>::~Sized_object()
73 // Set up an object file bsaed on the file header. This sets up the
74 // target and reads the section information.
76 template<int size, bool big_endian>
78 Sized_object<size, big_endian>::setup(
79 const elfcpp::Ehdr<size, big_endian>& ehdr)
81 int machine = ehdr.get_e_machine();
82 Target* target = select_target(machine, size, big_endian,
83 ehdr.get_e_ident()[elfcpp::EI_OSABI],
84 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
87 fprintf(stderr, _("%s: %s: unsupported ELF machine number %d\n"),
88 program_name, this->name().c_str(), machine);
91 this->set_target(target);
92 unsigned int shnum = ehdr.get_e_shnum();
93 unsigned int shstrndx = ehdr.get_e_shstrndx();
94 if ((shnum == 0 || shstrndx == elfcpp::SHN_XINDEX)
97 const unsigned char* p = this->get_view (this->shoff_, This::shdr_size);
98 elfcpp::Shdr<size, big_endian> shdr(p);
100 shnum = shdr.get_sh_size();
101 if (shstrndx == elfcpp::SHN_XINDEX)
102 shstrndx = shdr.get_sh_link();
104 this->set_shnum(shnum);
105 this->shstrndx_ = shstrndx;
110 // Find the SHT_SYMTAB section.
111 const unsigned char* p = this->get_view (this->shoff_,
112 shnum * This::shdr_size);
113 // Skip the first section, which is always empty.
114 p += This::shdr_size;
115 for (unsigned int i = 1; i < shnum; ++i)
117 elfcpp::Shdr<size, big_endian> shdr(p);
118 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
120 this->symtab_shnum_ = i;
123 p += This::shdr_size;
127 // Read the symbols and relocations from an object file.
129 template<int size, bool big_endian>
131 Sized_object<size, big_endian>::do_read_symbols()
133 if (this->symtab_shnum_ == 0)
135 // No symbol table. Weird but legal.
136 Read_symbols_data ret;
138 ret.symbols_size = 0;
139 ret.symbol_names = NULL;
140 ret.symbol_names_size = 0;
144 const int shdr_size = This::shdr_size;
146 // Read the symbol table section header.
147 off_t symtabshdroff = this->shoff_ + (this->symtab_shnum_ * shdr_size);
148 const unsigned char* psymtabshdr = this->get_view(symtabshdroff, shdr_size);
149 elfcpp::Shdr<size, big_endian> symtabshdr(psymtabshdr);
150 assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
152 // We only need the external symbols.
153 const int sym_size = This::sym_size;
154 off_t locsize = symtabshdr.get_sh_info() * sym_size;
155 off_t extoff = symtabshdr.get_sh_offset() + locsize;
156 off_t extsize = symtabshdr.get_sh_size() - locsize;
158 // Read the symbol table.
159 File_view* fvsymtab = this->get_lasting_view(extoff, extsize);
161 // Read the section header for the symbol names.
162 unsigned int strtab_shnum = symtabshdr.get_sh_link();
163 if (strtab_shnum == 0 || strtab_shnum >= this->shnum())
165 fprintf(stderr, _("%s: %s: invalid symbol table name index: %u\n"),
166 program_name, this->name().c_str(), strtab_shnum);
169 off_t strtabshdroff = this->shoff_ + (strtab_shnum * shdr_size);
170 const unsigned char *pstrtabshdr = this->get_view(strtabshdroff, shdr_size);
171 elfcpp::Shdr<size, big_endian> strtabshdr(pstrtabshdr);
172 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
175 _("%s: %s: symbol table name section has wrong type: %u\n"),
176 program_name, this->name().c_str(),
177 static_cast<unsigned int>(strtabshdr.get_sh_type()));
181 // Read the symbol names.
182 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
183 strtabshdr.get_sh_size());
185 Read_symbols_data ret;
186 ret.symbols = fvsymtab;
187 ret.symbols_size = extsize;
188 ret.symbol_names = fvstrtab;
189 ret.symbol_names_size = strtabshdr.get_sh_size();
194 // Add the symbols to the symbol table.
196 template<int size, bool big_endian>
198 Sized_object<size, big_endian>::do_add_symbols(Symbol_table* symtab,
199 Read_symbols_data sd)
201 if (sd.symbols == NULL)
203 assert(sd.symbol_names == NULL);
207 const int sym_size = This::sym_size;
208 size_t symcount = sd.symbols_size / sym_size;
209 if (symcount * sym_size != sd.symbols_size)
212 _("%s: %s: size of symbols is not multiple of symbol size\n"),
213 program_name, this->name().c_str());
217 this->symbols_ = new Symbol*[symcount];
219 const elfcpp::Sym<size, big_endian>* syms =
220 reinterpret_cast<const elfcpp::Sym<size, big_endian>*>(sd.symbols->data());
221 const char* sym_names =
222 reinterpret_cast<const char*>(sd.symbol_names->data());
223 symtab->add_from_object(this, syms, symcount, sym_names,
224 sd.symbol_names_size, this->symbols_);
227 delete sd.symbol_names;
230 // Return whether to include a section group in the link. LAYOUT is
231 // used to keep track of which section groups we have already seen.
232 // INDEX is the index of the section group and SHDR is the section
233 // header. If we do not want to include this group, we set bits in
234 // OMIT for each section which should be discarded.
236 template<int size, bool big_endian>
238 Sized_object<size, big_endian>::include_section_group(
241 const elfcpp::Shdr<size, big_endian>& shdr,
242 std::vector<bool>* omit)
244 // Read the section contents.
245 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
247 const elfcpp::Elf_Word* pword =
248 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
250 // The first word contains flags. We only care about COMDAT section
251 // groups. Other section groups are always included in the link
252 // just like ordinary sections.
253 elfcpp::Elf_Word flags = elfcpp::read_elf_word<big_endian>(pword);
254 if ((flags & elfcpp::GRP_COMDAT) == 0)
257 // Look up the group signature, which is the name of a symbol. This
258 // is a lot of effort to go to to read a string. Why didn't they
259 // just use the name of the SHT_GROUP section as the group
262 // Get the appropriate symbol table header (this will normally be
263 // the single SHT_SYMTAB section, but in principle it need not be).
264 if (shdr.get_sh_link() >= this->shnum())
266 fprintf(stderr, _("%s: %s: section group %u link %u out of range\n"),
267 program_name, this->name().c_str(), index, shdr.get_sh_link());
270 off_t off = this->shoff_ + shdr.get_sh_link() * This::shdr_size;
271 const unsigned char* psymshdr = this->get_view(off, This::shdr_size);
272 elfcpp::Shdr<size, big_endian> symshdr(psymshdr);
274 // Read the symbol table entry.
275 if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size)
277 fprintf(stderr, _("%s: %s: section group %u info %u out of range\n"),
278 program_name, this->name().c_str(), index, shdr.get_sh_info());
281 off_t symoff = symshdr.get_sh_offset() + shdr.get_sh_info() * This::sym_size;
282 const unsigned char* psym = this->get_view(symoff, This::sym_size);
283 elfcpp::Sym<size, big_endian> sym(psym);
285 // Read the section header for the symbol table names.
286 if (symshdr.get_sh_link() >= this->shnum())
288 fprintf(stderr, _("%s; %s: symtab section %u link %u out of range\n"),
289 program_name, this->name().c_str(), shdr.get_sh_link(),
290 symshdr.get_sh_link());
293 off_t symnameoff = this->shoff_ + symshdr.get_sh_link() * This::shdr_size;
294 const unsigned char* psymnamehdr = this->get_view(symnameoff,
296 elfcpp::Shdr<size, big_endian> symnamehdr(psymnamehdr);
298 // Read the symbol table names.
299 const unsigned char *psymnamesu = this->get_view(symnamehdr.get_sh_offset(),
300 symnamehdr.get_sh_size());
301 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
303 // Get the section group signature.
304 if (sym.get_st_name() >= symnamehdr.get_sh_size())
306 fprintf(stderr, _("%s: %s: symbol %u name offset %u out of range\n"),
307 program_name, this->name().c_str(), shdr.get_sh_info(),
312 const char* signature = psymnames + sym.get_st_name();
314 // Record this section group, and see whether we've already seen one
315 // with the same signature.
316 if (layout->add_comdat(signature, true))
319 // This is a duplicate. We want to discard the sections in this
321 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
322 for (size_t i = 1; i < count; ++i)
324 elfcpp::Elf_Word secnum = elfcpp::read_elf_word<big_endian>(pword + i);
325 if (secnum >= this->shnum())
328 _("%s: %s: section %u in section group %u out of range"),
329 program_name, this->name().c_str(), secnum,
333 (*omit)[secnum] = true;
339 // Whether to include a linkonce section in the link. NAME is the
340 // name of the section and SHDR is the section header.
342 // Linkonce sections are a GNU extension implemented in the original
343 // GNU linker before section groups were defined. The semantics are
344 // that we only include one linkonce section with a given name. The
345 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
346 // where T is the type of section and SYMNAME is the name of a symbol.
347 // In an attempt to make linkonce sections interact well with section
348 // groups, we try to identify SYMNAME and use it like a section group
349 // signature. We want to block section groups with that signature,
350 // but not other linkonce sections with that signature. We also use
351 // the full name of the linkonce section as a normal section group
354 template<int size, bool big_endian>
356 Sized_object<size, big_endian>::include_linkonce_section(
359 const elfcpp::Shdr<size, big_endian>&)
361 const char* symname = strrchr(name, '.') + 1;
362 bool omit1 = layout->add_comdat(symname, false);
363 bool omit2 = layout->add_comdat(name, true);
364 return omit1 || omit2;
367 // Lay out the input sections. We walk through the sections and check
368 // whether they should be included in the link. If they should, we
369 // pass them to the Layout object, which will return an output section
372 template<int size, bool big_endian>
374 Sized_object<size, big_endian>::do_layout(Layout* layout)
376 // This is always called from the main thread. Lock the file to
377 // keep the error checks happy.
378 Task_locker_obj<File_read> frl(this->input_file()->file());
380 // Get the section headers.
381 unsigned int shnum = this->shnum();
382 const unsigned char* pshdrs = this->get_view(this->shoff_,
383 shnum * This::shdr_size);
385 // Get the section names.
386 const unsigned char* pshdrnames = pshdrs + this->shstrndx_ * This::shdr_size;
387 elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
388 typename elfcpp::Elf_types<size>::Elf_WXword names_size =
389 shdrnames.get_sh_size();
390 const unsigned char* pnamesu = this->get_view(shdrnames.get_sh_offset(),
391 shdrnames.get_sh_size());
392 const char* pnames = reinterpret_cast<const char*>(pnamesu);
394 std::vector<Map_to_output>& map_sections(this->map_to_output());
395 map_sections.reserve(shnum);
397 // Keep track of which sections to omit.
398 std::vector<bool> omit(shnum, false);
400 for (unsigned int i = 0; i < shnum; ++i)
402 elfcpp::Shdr<size, big_endian> shdr(pshdrs);
404 if (shdr.get_sh_name() >= names_size)
407 _("%s: %s: bad section name offset for section %u: %lu\n"),
408 program_name, this->name().c_str(), i,
409 static_cast<unsigned long>(shdr.get_sh_name()));
413 const char* name = pnames + shdr.get_sh_name();
415 bool discard = omit[i];
418 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
420 if (!this->include_section_group(layout, i, shdr, &omit))
423 else if (Layout::is_linkonce(name))
425 if (!this->include_linkonce_section(layout, name, shdr))
432 // Do not include this section in the link.
433 map_sections[i].output_section = NULL;
438 Output_section* os = layout->layout(this, name, shdr, &offset);
440 map_sections[i].output_section = os;
441 map_sections[i].offset = offset;
443 pshdrs += This::shdr_size;
447 } // End namespace gold.
452 using namespace gold;
454 // Read an ELF file with the header and return the appropriate
455 // instance of Object.
457 template<int size, bool big_endian>
459 make_elf_sized_object(const std::string& name, Input_file* input_file,
460 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
462 int et = ehdr.get_e_type();
463 if (et != elfcpp::ET_REL && et != elfcpp::ET_DYN)
465 fprintf(stderr, "%s: %s: unsupported ELF type %d\n",
466 program_name, name.c_str(), static_cast<int>(et));
470 if (et == elfcpp::ET_REL)
472 Sized_object<size, big_endian>* obj =
473 new Sized_object<size, big_endian>(name, input_file, offset, ehdr);
480 fprintf(stderr, _("%s: %s: dynamic objects are not yet supported\n"),
481 program_name, name.c_str());
483 // Sized_dynobj<size, big_endian>* obj =
484 // new Sized_dynobj<size, big_endian>(this->input_.name(), input_file,
491 } // End anonymous namespace.
496 // Read an ELF file and return the appropriate instance of Object.
499 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
500 const unsigned char* p, off_t bytes)
502 if (bytes < elfcpp::EI_NIDENT)
504 fprintf(stderr, _("%s: %s: ELF file too short\n"),
505 program_name, name.c_str());
509 int v = p[elfcpp::EI_VERSION];
510 if (v != elfcpp::EV_CURRENT)
512 if (v == elfcpp::EV_NONE)
513 fprintf(stderr, _("%s: %s: invalid ELF version 0\n"),
514 program_name, name.c_str());
516 fprintf(stderr, _("%s: %s: unsupported ELF version %d\n"),
517 program_name, name.c_str(), v);
521 int c = p[elfcpp::EI_CLASS];
522 if (c == elfcpp::ELFCLASSNONE)
524 fprintf(stderr, _("%s: %s: invalid ELF class 0\n"),
525 program_name, name.c_str());
528 else if (c != elfcpp::ELFCLASS32
529 && c != elfcpp::ELFCLASS64)
531 fprintf(stderr, _("%s: %s: unsupported ELF class %d\n"),
532 program_name, name.c_str(), c);
536 int d = p[elfcpp::EI_DATA];
537 if (d == elfcpp::ELFDATANONE)
539 fprintf(stderr, _("%s: %s: invalid ELF data encoding\n"),
540 program_name, name.c_str());
543 else if (d != elfcpp::ELFDATA2LSB
544 && d != elfcpp::ELFDATA2MSB)
546 fprintf(stderr, _("%s: %s: unsupported ELF data encoding %d\n"),
547 program_name, name.c_str(), d);
551 bool big_endian = d == elfcpp::ELFDATA2MSB;
553 if (c == elfcpp::ELFCLASS32)
555 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
557 fprintf(stderr, _("%s: %s: ELF file too short\n"),
558 program_name, name.c_str());
563 elfcpp::Ehdr<32, true> ehdr(p);
564 return make_elf_sized_object<32, true>(name, input_file,
569 elfcpp::Ehdr<32, false> ehdr(p);
570 return make_elf_sized_object<32, false>(name, input_file,
576 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
578 fprintf(stderr, _("%s: %s: ELF file too short\n"),
579 program_name, name.c_str());
584 elfcpp::Ehdr<64, true> ehdr(p);
585 return make_elf_sized_object<64, true>(name, input_file,
590 elfcpp::Ehdr<64, false> ehdr(p);
591 return make_elf_sized_object<64, false>(name, input_file,
597 // Instantiate the templates we need. We could use the configure
598 // script to restrict this to only the ones for implemented targets.
601 class Sized_object<32, false>;
604 class Sized_object<32, true>;
607 class Sized_object<64, false>;
610 class Sized_object<64, true>;
612 } // End namespace gold.