daily update
[binutils.git] / gold / object.cc
blob2ecb8a9e915e93062fe01f944be5b34851df9ce4
1 // object.cc -- support for an object file for linking in gold
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #include "gold.h"
25 #include <cerrno>
26 #include <cstring>
27 #include <cstdarg>
28 #include "demangle.h"
29 #include "libiberty.h"
31 #include "target-select.h"
32 #include "dwarf_reader.h"
33 #include "layout.h"
34 #include "output.h"
35 #include "symtab.h"
36 #include "reloc.h"
37 #include "object.h"
38 #include "dynobj.h"
40 namespace gold
43 // Class Xindex.
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.
49 template<int size, bool big_endian>
50 void
51 Xindex::initialize_symtab_xindex(Object* object, unsigned int symtab_shndx)
53 if (!this->symtab_xindex_.empty())
54 return;
56 gold_assert(symtab_shndx != 0);
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)
63 --i;
64 if (object->section_type(i) == elfcpp::SHT_SYMTAB_SHNDX
65 && this->adjust_shndx(object->section_link(i)) == symtab_shndx)
67 this->read_symtab_xindex<size, big_endian>(object, i, NULL);
68 return;
72 object->error(_("missing SHT_SYMTAB_SHNDX section"));
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.
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)
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
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);
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)
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));
108 // Symbol symndx has a section of SHN_XINDEX; return the real section
109 // index.
111 unsigned int
112 Xindex::sym_xindex_to_shndx(Object* object, unsigned int symndx)
114 if (symndx >= this->symtab_xindex_.size())
116 object->error(_("symbol %u out of range for SHT_SYMTAB_SHNDX section"),
117 symndx);
118 return elfcpp::SHN_UNDEF;
120 unsigned int shndx = this->symtab_xindex_[symndx];
121 if (shndx < elfcpp::SHN_LORESERVE || shndx >= object->shnum())
123 object->error(_("extended index for symbol %u out of range: %u"),
124 symndx, shndx);
125 return elfcpp::SHN_UNDEF;
127 return shndx;
130 // Class Object.
132 // Set the target based on fields in the ELF file header.
134 void
135 Object::set_target(int machine, int size, bool big_endian, int osabi,
136 int abiversion)
138 Target* target = select_target(machine, size, big_endian, osabi, abiversion);
139 if (target == NULL)
140 gold_fatal(_("%s: unsupported ELF machine number %d"),
141 this->name().c_str(), machine);
142 this->target_ = target;
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.
149 void
150 Object::error(const char* format, ...) const
152 va_list args;
153 va_start(args, format);
154 char* buf = NULL;
155 if (vasprintf(&buf, format, args) < 0)
156 gold_nomem();
157 va_end(args);
158 gold_error(_("%s: %s"), this->name().c_str(), buf);
159 free(buf);
162 // Return a view of the contents of a section.
164 const unsigned char*
165 Object::section_contents(unsigned int shndx, section_size_type* plen,
166 bool cache)
168 Location loc(this->do_section_contents(shndx));
169 *plen = convert_to_section_size_type(loc.data_size);
170 return this->get_view(loc.file_offset, *plen, true, cache);
173 // Read the section data into SD. This is code common to Sized_relobj
174 // and Sized_dynobj, so we put it into Object.
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)
181 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
183 // Read the section headers.
184 const off_t shoff = elf_file->shoff();
185 const unsigned int shnum = this->shnum();
186 sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size,
187 true, true);
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);
194 if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
195 this->error(_("section name section has wrong type: %u"),
196 static_cast<unsigned int>(shdrnames.get_sh_type()));
198 sd->section_names_size =
199 convert_to_section_size_type(shdrnames.get_sh_size());
200 sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
201 sd->section_names_size, false,
202 false);
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.
209 bool
210 Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
211 Symbol_table* symtab)
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)
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);
228 return true;
230 return false;
233 // Class Sized_relobj.
235 template<int size, bool big_endian>
236 Sized_relobj<size, big_endian>::Sized_relobj(
237 const std::string& name,
238 Input_file* input_file,
239 off_t offset,
240 const elfcpp::Ehdr<size, big_endian>& ehdr)
241 : Relobj(name, input_file, offset),
242 elf_file_(this, ehdr),
243 symtab_shndx_(-1U),
244 local_symbol_count_(0),
245 output_local_symbol_count_(0),
246 output_local_dynsym_count_(0),
247 symbols_(),
248 local_symbol_offset_(0),
249 local_dynsym_offset_(0),
250 local_values_(),
251 local_got_offsets_(),
252 kept_comdat_sections_(),
253 comdat_groups_(),
254 has_eh_frame_(false)
258 template<int size, bool big_endian>
259 Sized_relobj<size, big_endian>::~Sized_relobj()
263 // Set up an object file based on the file header. This sets up the
264 // target and reads the section information.
266 template<int size, bool big_endian>
267 void
268 Sized_relobj<size, big_endian>::setup(
269 const elfcpp::Ehdr<size, big_endian>& ehdr)
271 this->set_target(ehdr.get_e_machine(), size, big_endian,
272 ehdr.get_e_ident()[elfcpp::EI_OSABI],
273 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
275 const unsigned int shnum = this->elf_file_.shnum();
276 this->set_shnum(shnum);
279 // Find the SHT_SYMTAB section, given the section headers. The ELF
280 // standard says that maybe in the future there can be more than one
281 // SHT_SYMTAB section. Until somebody figures out how that could
282 // work, we assume there is only one.
284 template<int size, bool big_endian>
285 void
286 Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs)
288 const unsigned int shnum = this->shnum();
289 this->symtab_shndx_ = 0;
290 if (shnum > 0)
292 // Look through the sections in reverse order, since gas tends
293 // to put the symbol table at the end.
294 const unsigned char* p = pshdrs + shnum * This::shdr_size;
295 unsigned int i = shnum;
296 unsigned int xindex_shndx = 0;
297 unsigned int xindex_link = 0;
298 while (i > 0)
300 --i;
301 p -= This::shdr_size;
302 typename This::Shdr shdr(p);
303 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
305 this->symtab_shndx_ = i;
306 if (xindex_shndx > 0 && xindex_link == i)
308 Xindex* xindex =
309 new Xindex(this->elf_file_.large_shndx_offset());
310 xindex->read_symtab_xindex<size, big_endian>(this,
311 xindex_shndx,
312 pshdrs);
313 this->set_xindex(xindex);
315 break;
318 // Try to pick up the SHT_SYMTAB_SHNDX section, if there is
319 // one. This will work if it follows the SHT_SYMTAB
320 // section.
321 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB_SHNDX)
323 xindex_shndx = i;
324 xindex_link = this->adjust_shndx(shdr.get_sh_link());
330 // Return the Xindex structure to use for object with lots of
331 // sections.
333 template<int size, bool big_endian>
334 Xindex*
335 Sized_relobj<size, big_endian>::do_initialize_xindex()
337 gold_assert(this->symtab_shndx_ != -1U);
338 Xindex* xindex = new Xindex(this->elf_file_.large_shndx_offset());
339 xindex->initialize_symtab_xindex<size, big_endian>(this, this->symtab_shndx_);
340 return xindex;
343 // Return whether SHDR has the right type and flags to be a GNU
344 // .eh_frame section.
346 template<int size, bool big_endian>
347 bool
348 Sized_relobj<size, big_endian>::check_eh_frame_flags(
349 const elfcpp::Shdr<size, big_endian>* shdr) const
351 return (shdr->get_sh_type() == elfcpp::SHT_PROGBITS
352 && (shdr->get_sh_flags() & elfcpp::SHF_ALLOC) != 0);
355 // Return whether there is a GNU .eh_frame section, given the section
356 // headers and the section names.
358 template<int size, bool big_endian>
359 bool
360 Sized_relobj<size, big_endian>::find_eh_frame(
361 const unsigned char* pshdrs,
362 const char* names,
363 section_size_type names_size) const
365 const unsigned int shnum = this->shnum();
366 const unsigned char* p = pshdrs + This::shdr_size;
367 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
369 typename This::Shdr shdr(p);
370 if (this->check_eh_frame_flags(&shdr))
372 if (shdr.get_sh_name() >= names_size)
374 this->error(_("bad section name offset for section %u: %lu"),
375 i, static_cast<unsigned long>(shdr.get_sh_name()));
376 continue;
379 const char* name = names + shdr.get_sh_name();
380 if (strcmp(name, ".eh_frame") == 0)
381 return true;
384 return false;
387 // Read the sections and symbols from an object file.
389 template<int size, bool big_endian>
390 void
391 Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
393 this->read_section_data(&this->elf_file_, sd);
395 const unsigned char* const pshdrs = sd->section_headers->data();
397 this->find_symtab(pshdrs);
399 const unsigned char* namesu = sd->section_names->data();
400 const char* names = reinterpret_cast<const char*>(namesu);
401 if (memmem(names, sd->section_names_size, ".eh_frame", 10) != NULL)
403 if (this->find_eh_frame(pshdrs, names, sd->section_names_size))
404 this->has_eh_frame_ = true;
407 sd->symbols = NULL;
408 sd->symbols_size = 0;
409 sd->external_symbols_offset = 0;
410 sd->symbol_names = NULL;
411 sd->symbol_names_size = 0;
413 if (this->symtab_shndx_ == 0)
415 // No symbol table. Weird but legal.
416 return;
419 // Get the symbol table section header.
420 typename This::Shdr symtabshdr(pshdrs
421 + this->symtab_shndx_ * This::shdr_size);
422 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
424 // If this object has a .eh_frame section, we need all the symbols.
425 // Otherwise we only need the external symbols. While it would be
426 // simpler to just always read all the symbols, I've seen object
427 // files with well over 2000 local symbols, which for a 64-bit
428 // object file format is over 5 pages that we don't need to read
429 // now.
431 const int sym_size = This::sym_size;
432 const unsigned int loccount = symtabshdr.get_sh_info();
433 this->local_symbol_count_ = loccount;
434 this->local_values_.resize(loccount);
435 section_offset_type locsize = loccount * sym_size;
436 off_t dataoff = symtabshdr.get_sh_offset();
437 section_size_type datasize =
438 convert_to_section_size_type(symtabshdr.get_sh_size());
439 off_t extoff = dataoff + locsize;
440 section_size_type extsize = datasize - locsize;
442 off_t readoff = this->has_eh_frame_ ? dataoff : extoff;
443 section_size_type readsize = this->has_eh_frame_ ? datasize : extsize;
445 if (readsize == 0)
447 // No external symbols. Also weird but also legal.
448 return;
451 File_view* fvsymtab = this->get_lasting_view(readoff, readsize, true, false);
453 // Read the section header for the symbol names.
454 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
455 if (strtab_shndx >= this->shnum())
457 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
458 return;
460 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
461 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
463 this->error(_("symbol table name section has wrong type: %u"),
464 static_cast<unsigned int>(strtabshdr.get_sh_type()));
465 return;
468 // Read the symbol names.
469 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
470 strtabshdr.get_sh_size(),
471 false, true);
473 sd->symbols = fvsymtab;
474 sd->symbols_size = readsize;
475 sd->external_symbols_offset = this->has_eh_frame_ ? locsize : 0;
476 sd->symbol_names = fvstrtab;
477 sd->symbol_names_size =
478 convert_to_section_size_type(strtabshdr.get_sh_size());
481 // Return the section index of symbol SYM. Set *VALUE to its value in
482 // the object file. Set *IS_ORDINARY if this is an ordinary section
483 // index. not a special cod between SHN_LORESERVE and SHN_HIRESERVE.
484 // Note that for a symbol which is not defined in this object file,
485 // this will set *VALUE to 0 and return SHN_UNDEF; it will not return
486 // the final value of the symbol in the link.
488 template<int size, bool big_endian>
489 unsigned int
490 Sized_relobj<size, big_endian>::symbol_section_and_value(unsigned int sym,
491 Address* value,
492 bool* is_ordinary)
494 section_size_type symbols_size;
495 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
496 &symbols_size,
497 false);
499 const size_t count = symbols_size / This::sym_size;
500 gold_assert(sym < count);
502 elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size);
503 *value = elfsym.get_st_value();
505 return this->adjust_sym_shndx(sym, elfsym.get_st_shndx(), is_ordinary);
508 // Return whether to include a section group in the link. LAYOUT is
509 // used to keep track of which section groups we have already seen.
510 // INDEX is the index of the section group and SHDR is the section
511 // header. If we do not want to include this group, we set bits in
512 // OMIT for each section which should be discarded.
514 template<int size, bool big_endian>
515 bool
516 Sized_relobj<size, big_endian>::include_section_group(
517 Symbol_table* symtab,
518 Layout* layout,
519 unsigned int index,
520 const char* name,
521 const unsigned char* shdrs,
522 const char* section_names,
523 section_size_type section_names_size,
524 std::vector<bool>* omit)
526 // Read the section contents.
527 typename This::Shdr shdr(shdrs + index * This::shdr_size);
528 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
529 shdr.get_sh_size(), true, false);
530 const elfcpp::Elf_Word* pword =
531 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
533 // The first word contains flags. We only care about COMDAT section
534 // groups. Other section groups are always included in the link
535 // just like ordinary sections.
536 elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
538 // Look up the group signature, which is the name of a symbol. This
539 // is a lot of effort to go to to read a string. Why didn't they
540 // just have the group signature point into the string table, rather
541 // than indirect through a symbol?
543 // Get the appropriate symbol table header (this will normally be
544 // the single SHT_SYMTAB section, but in principle it need not be).
545 const unsigned int link = this->adjust_shndx(shdr.get_sh_link());
546 typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
548 // Read the symbol table entry.
549 unsigned int symndx = shdr.get_sh_info();
550 if (symndx >= symshdr.get_sh_size() / This::sym_size)
552 this->error(_("section group %u info %u out of range"),
553 index, symndx);
554 return false;
556 off_t symoff = symshdr.get_sh_offset() + symndx * This::sym_size;
557 const unsigned char* psym = this->get_view(symoff, This::sym_size, true,
558 false);
559 elfcpp::Sym<size, big_endian> sym(psym);
561 // Read the symbol table names.
562 section_size_type symnamelen;
563 const unsigned char* psymnamesu;
564 psymnamesu = this->section_contents(this->adjust_shndx(symshdr.get_sh_link()),
565 &symnamelen, true);
566 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
568 // Get the section group signature.
569 if (sym.get_st_name() >= symnamelen)
571 this->error(_("symbol %u name offset %u out of range"),
572 symndx, sym.get_st_name());
573 return false;
576 std::string signature(psymnames + sym.get_st_name());
578 // It seems that some versions of gas will create a section group
579 // associated with a section symbol, and then fail to give a name to
580 // the section symbol. In such a case, use the name of the section.
581 if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
583 bool is_ordinary;
584 unsigned int sym_shndx = this->adjust_sym_shndx(symndx,
585 sym.get_st_shndx(),
586 &is_ordinary);
587 if (!is_ordinary || sym_shndx >= this->shnum())
589 this->error(_("symbol %u invalid section index %u"),
590 symndx, sym_shndx);
591 return false;
593 typename This::Shdr member_shdr(shdrs + sym_shndx * This::shdr_size);
594 if (member_shdr.get_sh_name() < section_names_size)
595 signature = section_names + member_shdr.get_sh_name();
598 // Record this section group in the layout, and see whether we've already
599 // seen one with the same signature.
600 bool include_group = ((flags & elfcpp::GRP_COMDAT) == 0
601 || layout->add_comdat(this, index, signature, true));
603 Sized_relobj<size, big_endian>* kept_object = NULL;
604 Comdat_group* kept_group = NULL;
606 if (!include_group)
608 // This group is being discarded. Find the object and group
609 // that was kept in its place.
610 unsigned int kept_group_index = 0;
611 Relobj* kept_relobj = layout->find_kept_object(signature,
612 &kept_group_index);
613 kept_object = static_cast<Sized_relobj<size, big_endian>*>(kept_relobj);
614 if (kept_object != NULL)
615 kept_group = kept_object->find_comdat_group(kept_group_index);
617 else if (flags & elfcpp::GRP_COMDAT)
619 // This group is being kept. Create the table to map section names
620 // to section indexes and add it to the table of groups.
621 kept_group = new Comdat_group();
622 this->add_comdat_group(index, kept_group);
625 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
627 std::vector<unsigned int> shndxes;
628 bool relocate_group = include_group && parameters->options().relocatable();
629 if (relocate_group)
630 shndxes.reserve(count - 1);
632 for (size_t i = 1; i < count; ++i)
634 elfcpp::Elf_Word secnum =
635 this->adjust_shndx(elfcpp::Swap<32, big_endian>::readval(pword + i));
637 if (relocate_group)
638 shndxes.push_back(secnum);
640 if (secnum >= this->shnum())
642 this->error(_("section %u in section group %u out of range"),
643 secnum, index);
644 continue;
647 // Check for an earlier section number, since we're going to get
648 // it wrong--we may have already decided to include the section.
649 if (secnum < index)
650 this->error(_("invalid section group %u refers to earlier section %u"),
651 index, secnum);
653 // Get the name of the member section.
654 typename This::Shdr member_shdr(shdrs + secnum * This::shdr_size);
655 if (member_shdr.get_sh_name() >= section_names_size)
657 // This is an error, but it will be diagnosed eventually
658 // in do_layout, so we don't need to do anything here but
659 // ignore it.
660 continue;
662 std::string mname(section_names + member_shdr.get_sh_name());
664 if (!include_group)
666 (*omit)[secnum] = true;
667 if (kept_group != NULL)
669 // Find the corresponding kept section, and store that info
670 // in the discarded section table.
671 Comdat_group::const_iterator p = kept_group->find(mname);
672 if (p != kept_group->end())
674 Kept_comdat_section* kept =
675 new Kept_comdat_section(kept_object, p->second);
676 this->set_kept_comdat_section(secnum, kept);
680 else if (flags & elfcpp::GRP_COMDAT)
682 // Add the section to the kept group table.
683 gold_assert(kept_group != NULL);
684 kept_group->insert(std::make_pair(mname, secnum));
688 if (relocate_group)
689 layout->layout_group(symtab, this, index, name, signature.c_str(),
690 shdr, flags, &shndxes);
692 return include_group;
695 // Whether to include a linkonce section in the link. NAME is the
696 // name of the section and SHDR is the section header.
698 // Linkonce sections are a GNU extension implemented in the original
699 // GNU linker before section groups were defined. The semantics are
700 // that we only include one linkonce section with a given name. The
701 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
702 // where T is the type of section and SYMNAME is the name of a symbol.
703 // In an attempt to make linkonce sections interact well with section
704 // groups, we try to identify SYMNAME and use it like a section group
705 // signature. We want to block section groups with that signature,
706 // but not other linkonce sections with that signature. We also use
707 // the full name of the linkonce section as a normal section group
708 // signature.
710 template<int size, bool big_endian>
711 bool
712 Sized_relobj<size, big_endian>::include_linkonce_section(
713 Layout* layout,
714 unsigned int index,
715 const char* name,
716 const elfcpp::Shdr<size, big_endian>&)
718 // In general the symbol name we want will be the string following
719 // the last '.'. However, we have to handle the case of
720 // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
721 // some versions of gcc. So we use a heuristic: if the name starts
722 // with ".gnu.linkonce.t.", we use everything after that. Otherwise
723 // we look for the last '.'. We can't always simply skip
724 // ".gnu.linkonce.X", because we have to deal with cases like
725 // ".gnu.linkonce.d.rel.ro.local".
726 const char* const linkonce_t = ".gnu.linkonce.t.";
727 const char* symname;
728 if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0)
729 symname = name + strlen(linkonce_t);
730 else
731 symname = strrchr(name, '.') + 1;
732 std::string sig1(symname);
733 std::string sig2(name);
734 bool include1 = layout->add_comdat(this, index, sig1, false);
735 bool include2 = layout->add_comdat(this, index, sig2, true);
737 if (!include2)
739 // The section is being discarded on the basis of its section
740 // name (i.e., the kept section was also a linkonce section).
741 // In this case, the section index stored with the layout object
742 // is the linkonce section that was kept.
743 unsigned int kept_group_index = 0;
744 Relobj* kept_relobj = layout->find_kept_object(sig2, &kept_group_index);
745 if (kept_relobj != NULL)
747 Sized_relobj<size, big_endian>* kept_object
748 = static_cast<Sized_relobj<size, big_endian>*>(kept_relobj);
749 Kept_comdat_section* kept =
750 new Kept_comdat_section(kept_object, kept_group_index);
751 this->set_kept_comdat_section(index, kept);
754 else if (!include1)
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_relobj = layout->find_kept_object(sig1, &kept_group_index);
765 if (kept_relobj != NULL)
767 Sized_relobj<size, big_endian>* kept_object =
768 static_cast<Sized_relobj<size, big_endian>*>(kept_relobj);
769 Comdat_group* kept_group =
770 kept_object->find_comdat_group(kept_group_index);
771 if (kept_group != NULL && kept_group->size() == 1)
773 Comdat_group::const_iterator p = kept_group->begin();
774 gold_assert(p != kept_group->end());
775 Kept_comdat_section* kept =
776 new Kept_comdat_section(kept_object, p->second);
777 this->set_kept_comdat_section(index, kept);
782 return include1 && include2;
785 // Lay out the input sections. We walk through the sections and check
786 // whether they should be included in the link. If they should, we
787 // pass them to the Layout object, which will return an output section
788 // and an offset.
790 template<int size, bool big_endian>
791 void
792 Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab,
793 Layout* layout,
794 Read_symbols_data* sd)
796 const unsigned int shnum = this->shnum();
797 if (shnum == 0)
798 return;
800 // Get the section headers.
801 const unsigned char* shdrs = sd->section_headers->data();
802 const unsigned char* pshdrs;
804 // Get the section names.
805 const unsigned char* pnamesu = sd->section_names->data();
806 const char* pnames = reinterpret_cast<const char*>(pnamesu);
808 // For each section, record the index of the reloc section if any.
809 // Use 0 to mean that there is no reloc section, -1U to mean that
810 // there is more than one.
811 std::vector<unsigned int> reloc_shndx(shnum, 0);
812 std::vector<unsigned int> reloc_type(shnum, elfcpp::SHT_NULL);
813 // Skip the first, dummy, section.
814 pshdrs = shdrs + This::shdr_size;
815 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
817 typename This::Shdr shdr(pshdrs);
819 unsigned int sh_type = shdr.get_sh_type();
820 if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
822 unsigned int target_shndx = this->adjust_shndx(shdr.get_sh_info());
823 if (target_shndx == 0 || target_shndx >= shnum)
825 this->error(_("relocation section %u has bad info %u"),
826 i, target_shndx);
827 continue;
830 if (reloc_shndx[target_shndx] != 0)
831 reloc_shndx[target_shndx] = -1U;
832 else
834 reloc_shndx[target_shndx] = i;
835 reloc_type[target_shndx] = sh_type;
840 Output_sections& out_sections(this->output_sections());
841 std::vector<Address>& out_section_offsets(this->section_offsets_);
843 out_sections.resize(shnum);
844 out_section_offsets.resize(shnum);
846 // If we are only linking for symbols, then there is nothing else to
847 // do here.
848 if (this->input_file()->just_symbols())
850 delete sd->section_headers;
851 sd->section_headers = NULL;
852 delete sd->section_names;
853 sd->section_names = NULL;
854 return;
857 // Whether we've seen a .note.GNU-stack section.
858 bool seen_gnu_stack = false;
859 // The flags of a .note.GNU-stack section.
860 uint64_t gnu_stack_flags = 0;
862 // Keep track of which sections to omit.
863 std::vector<bool> omit(shnum, false);
865 // Keep track of reloc sections when emitting relocations.
866 const bool relocatable = parameters->options().relocatable();
867 const bool emit_relocs = (relocatable
868 || parameters->options().emit_relocs());
869 std::vector<unsigned int> reloc_sections;
871 // Keep track of .eh_frame sections.
872 std::vector<unsigned int> eh_frame_sections;
874 // Skip the first, dummy, section.
875 pshdrs = shdrs + This::shdr_size;
876 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
878 typename This::Shdr shdr(pshdrs);
880 if (shdr.get_sh_name() >= sd->section_names_size)
882 this->error(_("bad section name offset for section %u: %lu"),
883 i, static_cast<unsigned long>(shdr.get_sh_name()));
884 return;
887 const char* name = pnames + shdr.get_sh_name();
889 if (this->handle_gnu_warning_section(name, i, symtab))
891 if (!relocatable)
892 omit[i] = true;
895 // The .note.GNU-stack section is special. It gives the
896 // protection flags that this object file requires for the stack
897 // in memory.
898 if (strcmp(name, ".note.GNU-stack") == 0)
900 seen_gnu_stack = true;
901 gnu_stack_flags |= shdr.get_sh_flags();
902 omit[i] = true;
905 bool discard = omit[i];
906 if (!discard)
908 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
910 if (!this->include_section_group(symtab, layout, i, name, shdrs,
911 pnames, sd->section_names_size,
912 &omit))
913 discard = true;
915 else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
916 && Layout::is_linkonce(name))
918 if (!this->include_linkonce_section(layout, i, name, shdr))
919 discard = true;
923 if (discard)
925 // Do not include this section in the link.
926 out_sections[i] = NULL;
927 out_section_offsets[i] = -1U;
928 continue;
931 // When doing a relocatable link we are going to copy input
932 // reloc sections into the output. We only want to copy the
933 // ones associated with sections which are not being discarded.
934 // However, we don't know that yet for all sections. So save
935 // reloc sections and process them later.
936 if (emit_relocs
937 && (shdr.get_sh_type() == elfcpp::SHT_REL
938 || shdr.get_sh_type() == elfcpp::SHT_RELA))
940 reloc_sections.push_back(i);
941 continue;
944 if (relocatable && shdr.get_sh_type() == elfcpp::SHT_GROUP)
945 continue;
947 // The .eh_frame section is special. It holds exception frame
948 // information that we need to read in order to generate the
949 // exception frame header. We process these after all the other
950 // sections so that the exception frame reader can reliably
951 // determine which sections are being discarded, and discard the
952 // corresponding information.
953 if (!relocatable
954 && strcmp(name, ".eh_frame") == 0
955 && this->check_eh_frame_flags(&shdr))
957 eh_frame_sections.push_back(i);
958 continue;
961 off_t offset;
962 Output_section* os = layout->layout(this, i, name, shdr,
963 reloc_shndx[i], reloc_type[i],
964 &offset);
966 out_sections[i] = os;
967 if (offset == -1)
968 out_section_offsets[i] = -1U;
969 else
970 out_section_offsets[i] = convert_types<Address, off_t>(offset);
972 // If this section requires special handling, and if there are
973 // relocs that apply to it, then we must do the special handling
974 // before we apply the relocs.
975 if (offset == -1 && reloc_shndx[i] != 0)
976 this->set_relocs_must_follow_section_writes();
979 layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags);
981 // When doing a relocatable link handle the reloc sections at the
982 // end.
983 if (emit_relocs)
984 this->size_relocatable_relocs();
985 for (std::vector<unsigned int>::const_iterator p = reloc_sections.begin();
986 p != reloc_sections.end();
987 ++p)
989 unsigned int i = *p;
990 const unsigned char* pshdr;
991 pshdr = sd->section_headers->data() + i * This::shdr_size;
992 typename This::Shdr shdr(pshdr);
994 unsigned int data_shndx = this->adjust_shndx(shdr.get_sh_info());
995 if (data_shndx >= shnum)
997 // We already warned about this above.
998 continue;
1001 Output_section* data_section = out_sections[data_shndx];
1002 if (data_section == NULL)
1004 out_sections[i] = NULL;
1005 out_section_offsets[i] = -1U;
1006 continue;
1009 Relocatable_relocs* rr = new Relocatable_relocs();
1010 this->set_relocatable_relocs(i, rr);
1012 Output_section* os = layout->layout_reloc(this, i, shdr, data_section,
1013 rr);
1014 out_sections[i] = os;
1015 out_section_offsets[i] = -1U;
1018 // Handle the .eh_frame sections at the end.
1019 for (std::vector<unsigned int>::const_iterator p = eh_frame_sections.begin();
1020 p != eh_frame_sections.end();
1021 ++p)
1023 gold_assert(this->has_eh_frame_);
1024 gold_assert(sd->external_symbols_offset != 0);
1026 unsigned int i = *p;
1027 const unsigned char *pshdr;
1028 pshdr = sd->section_headers->data() + i * This::shdr_size;
1029 typename This::Shdr shdr(pshdr);
1031 off_t offset;
1032 Output_section* os = layout->layout_eh_frame(this,
1033 sd->symbols->data(),
1034 sd->symbols_size,
1035 sd->symbol_names->data(),
1036 sd->symbol_names_size,
1037 i, shdr,
1038 reloc_shndx[i],
1039 reloc_type[i],
1040 &offset);
1041 out_sections[i] = os;
1042 if (offset == -1)
1043 out_section_offsets[i] = -1U;
1044 else
1045 out_section_offsets[i] = convert_types<Address, off_t>(offset);
1047 // If this section requires special handling, and if there are
1048 // relocs that apply to it, then we must do the special handling
1049 // before we apply the relocs.
1050 if (offset == -1 && reloc_shndx[i] != 0)
1051 this->set_relocs_must_follow_section_writes();
1054 delete sd->section_headers;
1055 sd->section_headers = NULL;
1056 delete sd->section_names;
1057 sd->section_names = NULL;
1060 // Add the symbols to the symbol table.
1062 template<int size, bool big_endian>
1063 void
1064 Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
1065 Read_symbols_data* sd)
1067 if (sd->symbols == NULL)
1069 gold_assert(sd->symbol_names == NULL);
1070 return;
1073 const int sym_size = This::sym_size;
1074 size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
1075 / sym_size);
1076 if (symcount * sym_size != sd->symbols_size - sd->external_symbols_offset)
1078 this->error(_("size of symbols is not multiple of symbol size"));
1079 return;
1082 this->symbols_.resize(symcount);
1084 const char* sym_names =
1085 reinterpret_cast<const char*>(sd->symbol_names->data());
1086 symtab->add_from_relobj(this,
1087 sd->symbols->data() + sd->external_symbols_offset,
1088 symcount, this->local_symbol_count_,
1089 sym_names, sd->symbol_names_size,
1090 &this->symbols_);
1092 delete sd->symbols;
1093 sd->symbols = NULL;
1094 delete sd->symbol_names;
1095 sd->symbol_names = NULL;
1098 // First pass over the local symbols. Here we add their names to
1099 // *POOL and *DYNPOOL, and we store the symbol value in
1100 // THIS->LOCAL_VALUES_. This function is always called from a
1101 // singleton thread. This is followed by a call to
1102 // finalize_local_symbols.
1104 template<int size, bool big_endian>
1105 void
1106 Sized_relobj<size, big_endian>::do_count_local_symbols(Stringpool* pool,
1107 Stringpool* dynpool)
1109 gold_assert(this->symtab_shndx_ != -1U);
1110 if (this->symtab_shndx_ == 0)
1112 // This object has no symbols. Weird but legal.
1113 return;
1116 // Read the symbol table section header.
1117 const unsigned int symtab_shndx = this->symtab_shndx_;
1118 typename This::Shdr symtabshdr(this,
1119 this->elf_file_.section_header(symtab_shndx));
1120 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1122 // Read the local symbols.
1123 const int sym_size = This::sym_size;
1124 const unsigned int loccount = this->local_symbol_count_;
1125 gold_assert(loccount == symtabshdr.get_sh_info());
1126 off_t locsize = loccount * sym_size;
1127 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1128 locsize, true, true);
1130 // Read the symbol names.
1131 const unsigned int strtab_shndx =
1132 this->adjust_shndx(symtabshdr.get_sh_link());
1133 section_size_type strtab_size;
1134 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
1135 &strtab_size,
1136 true);
1137 const char* pnames = reinterpret_cast<const char*>(pnamesu);
1139 // Loop over the local symbols.
1141 const Output_sections& out_sections(this->output_sections());
1142 unsigned int shnum = this->shnum();
1143 unsigned int count = 0;
1144 unsigned int dyncount = 0;
1145 // Skip the first, dummy, symbol.
1146 psyms += sym_size;
1147 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
1149 elfcpp::Sym<size, big_endian> sym(psyms);
1151 Symbol_value<size>& lv(this->local_values_[i]);
1153 bool is_ordinary;
1154 unsigned int shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
1155 &is_ordinary);
1156 lv.set_input_shndx(shndx, is_ordinary);
1158 if (sym.get_st_type() == elfcpp::STT_SECTION)
1159 lv.set_is_section_symbol();
1160 else if (sym.get_st_type() == elfcpp::STT_TLS)
1161 lv.set_is_tls_symbol();
1163 // Save the input symbol value for use in do_finalize_local_symbols().
1164 lv.set_input_value(sym.get_st_value());
1166 // Decide whether this symbol should go into the output file.
1168 if (shndx < shnum && out_sections[shndx] == NULL)
1170 lv.set_no_output_symtab_entry();
1171 gold_assert(!lv.needs_output_dynsym_entry());
1172 continue;
1175 if (sym.get_st_type() == elfcpp::STT_SECTION)
1177 lv.set_no_output_symtab_entry();
1178 gold_assert(!lv.needs_output_dynsym_entry());
1179 continue;
1182 if (sym.get_st_name() >= strtab_size)
1184 this->error(_("local symbol %u section name out of range: %u >= %u"),
1185 i, sym.get_st_name(),
1186 static_cast<unsigned int>(strtab_size));
1187 lv.set_no_output_symtab_entry();
1188 continue;
1191 // Add the symbol to the symbol table string pool.
1192 const char* name = pnames + sym.get_st_name();
1193 pool->add(name, true, NULL);
1194 ++count;
1196 // If needed, add the symbol to the dynamic symbol table string pool.
1197 if (lv.needs_output_dynsym_entry())
1199 dynpool->add(name, true, NULL);
1200 ++dyncount;
1204 this->output_local_symbol_count_ = count;
1205 this->output_local_dynsym_count_ = dyncount;
1208 // Finalize the local symbols. Here we set the final value in
1209 // THIS->LOCAL_VALUES_ and set their output symbol table indexes.
1210 // This function is always called from a singleton thread. The actual
1211 // output of the local symbols will occur in a separate task.
1213 template<int size, bool big_endian>
1214 unsigned int
1215 Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
1216 off_t off)
1218 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
1220 const unsigned int loccount = this->local_symbol_count_;
1221 this->local_symbol_offset_ = off;
1223 const Output_sections& out_sections(this->output_sections());
1224 const std::vector<Address>& out_offsets(this->section_offsets_);
1225 unsigned int shnum = this->shnum();
1227 for (unsigned int i = 1; i < loccount; ++i)
1229 Symbol_value<size>& lv(this->local_values_[i]);
1231 bool is_ordinary;
1232 unsigned int shndx = lv.input_shndx(&is_ordinary);
1234 // Set the output symbol value.
1236 if (!is_ordinary)
1238 if (shndx == elfcpp::SHN_ABS || shndx == elfcpp::SHN_COMMON)
1239 lv.set_output_value(lv.input_value());
1240 else
1242 this->error(_("unknown section index %u for local symbol %u"),
1243 shndx, i);
1244 lv.set_output_value(0);
1247 else
1249 if (shndx >= shnum)
1251 this->error(_("local symbol %u section index %u out of range"),
1252 i, shndx);
1253 shndx = 0;
1256 Output_section* os = out_sections[shndx];
1258 if (os == NULL)
1260 // This local symbol belongs to a section we are discarding.
1261 // In some cases when applying relocations later, we will
1262 // attempt to match it to the corresponding kept section,
1263 // so we leave the input value unchanged here.
1264 continue;
1266 else if (out_offsets[shndx] == -1U)
1268 // This is a SHF_MERGE section or one which otherwise
1269 // requires special handling. We get the output address
1270 // of the start of the merged section. If this is not a
1271 // section symbol, we can then determine the final
1272 // value. If it is a section symbol, we can not, as in
1273 // that case we have to consider the addend to determine
1274 // the value to use in a relocation.
1275 if (!lv.is_section_symbol())
1276 lv.set_output_value(os->output_address(this, shndx,
1277 lv.input_value()));
1278 else
1280 section_offset_type start =
1281 os->starting_output_address(this, shndx);
1282 Merged_symbol_value<size>* msv =
1283 new Merged_symbol_value<size>(lv.input_value(), start);
1284 lv.set_merged_symbol_value(msv);
1287 else if (lv.is_tls_symbol())
1288 lv.set_output_value(os->tls_offset()
1289 + out_offsets[shndx]
1290 + lv.input_value());
1291 else
1292 lv.set_output_value(os->address()
1293 + out_offsets[shndx]
1294 + lv.input_value());
1297 if (lv.needs_output_symtab_entry())
1299 lv.set_output_symtab_index(index);
1300 ++index;
1303 return index;
1306 // Set the output dynamic symbol table indexes for the local variables.
1308 template<int size, bool big_endian>
1309 unsigned int
1310 Sized_relobj<size, big_endian>::do_set_local_dynsym_indexes(unsigned int index)
1312 const unsigned int loccount = this->local_symbol_count_;
1313 for (unsigned int i = 1; i < loccount; ++i)
1315 Symbol_value<size>& lv(this->local_values_[i]);
1316 if (lv.needs_output_dynsym_entry())
1318 lv.set_output_dynsym_index(index);
1319 ++index;
1322 return index;
1325 // Set the offset where local dynamic symbol information will be stored.
1326 // Returns the count of local symbols contributed to the symbol table by
1327 // this object.
1329 template<int size, bool big_endian>
1330 unsigned int
1331 Sized_relobj<size, big_endian>::do_set_local_dynsym_offset(off_t off)
1333 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
1334 this->local_dynsym_offset_ = off;
1335 return this->output_local_dynsym_count_;
1338 // Write out the local symbols.
1340 template<int size, bool big_endian>
1341 void
1342 Sized_relobj<size, big_endian>::write_local_symbols(
1343 Output_file* of,
1344 const Stringpool* sympool,
1345 const Stringpool* dynpool,
1346 Output_symtab_xindex* symtab_xindex,
1347 Output_symtab_xindex* dynsym_xindex)
1349 if (parameters->options().strip_all()
1350 && this->output_local_dynsym_count_ == 0)
1351 return;
1353 gold_assert(this->symtab_shndx_ != -1U);
1354 if (this->symtab_shndx_ == 0)
1356 // This object has no symbols. Weird but legal.
1357 return;
1360 // Read the symbol table section header.
1361 const unsigned int symtab_shndx = this->symtab_shndx_;
1362 typename This::Shdr symtabshdr(this,
1363 this->elf_file_.section_header(symtab_shndx));
1364 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1365 const unsigned int loccount = this->local_symbol_count_;
1366 gold_assert(loccount == symtabshdr.get_sh_info());
1368 // Read the local symbols.
1369 const int sym_size = This::sym_size;
1370 off_t locsize = loccount * sym_size;
1371 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1372 locsize, true, false);
1374 // Read the symbol names.
1375 const unsigned int strtab_shndx =
1376 this->adjust_shndx(symtabshdr.get_sh_link());
1377 section_size_type strtab_size;
1378 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
1379 &strtab_size,
1380 false);
1381 const char* pnames = reinterpret_cast<const char*>(pnamesu);
1383 // Get views into the output file for the portions of the symbol table
1384 // and the dynamic symbol table that we will be writing.
1385 off_t output_size = this->output_local_symbol_count_ * sym_size;
1386 unsigned char* oview = NULL;
1387 if (output_size > 0)
1388 oview = of->get_output_view(this->local_symbol_offset_, output_size);
1390 off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size;
1391 unsigned char* dyn_oview = NULL;
1392 if (dyn_output_size > 0)
1393 dyn_oview = of->get_output_view(this->local_dynsym_offset_,
1394 dyn_output_size);
1396 const Output_sections out_sections(this->output_sections());
1398 gold_assert(this->local_values_.size() == loccount);
1400 unsigned char* ov = oview;
1401 unsigned char* dyn_ov = dyn_oview;
1402 psyms += sym_size;
1403 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
1405 elfcpp::Sym<size, big_endian> isym(psyms);
1407 Symbol_value<size>& lv(this->local_values_[i]);
1409 bool is_ordinary;
1410 unsigned int st_shndx = this->adjust_sym_shndx(i, isym.get_st_shndx(),
1411 &is_ordinary);
1412 if (is_ordinary)
1414 gold_assert(st_shndx < out_sections.size());
1415 if (out_sections[st_shndx] == NULL)
1416 continue;
1417 st_shndx = out_sections[st_shndx]->out_shndx();
1418 if (st_shndx >= elfcpp::SHN_LORESERVE)
1420 if (lv.needs_output_symtab_entry())
1421 symtab_xindex->add(lv.output_symtab_index(), st_shndx);
1422 if (lv.needs_output_dynsym_entry())
1423 dynsym_xindex->add(lv.output_dynsym_index(), st_shndx);
1424 st_shndx = elfcpp::SHN_XINDEX;
1428 // Write the symbol to the output symbol table.
1429 if (!parameters->options().strip_all()
1430 && lv.needs_output_symtab_entry())
1432 elfcpp::Sym_write<size, big_endian> osym(ov);
1434 gold_assert(isym.get_st_name() < strtab_size);
1435 const char* name = pnames + isym.get_st_name();
1436 osym.put_st_name(sympool->get_offset(name));
1437 osym.put_st_value(this->local_values_[i].value(this, 0));
1438 osym.put_st_size(isym.get_st_size());
1439 osym.put_st_info(isym.get_st_info());
1440 osym.put_st_other(isym.get_st_other());
1441 osym.put_st_shndx(st_shndx);
1443 ov += sym_size;
1446 // Write the symbol to the output dynamic symbol table.
1447 if (lv.needs_output_dynsym_entry())
1449 gold_assert(dyn_ov < dyn_oview + dyn_output_size);
1450 elfcpp::Sym_write<size, big_endian> osym(dyn_ov);
1452 gold_assert(isym.get_st_name() < strtab_size);
1453 const char* name = pnames + isym.get_st_name();
1454 osym.put_st_name(dynpool->get_offset(name));
1455 osym.put_st_value(this->local_values_[i].value(this, 0));
1456 osym.put_st_size(isym.get_st_size());
1457 osym.put_st_info(isym.get_st_info());
1458 osym.put_st_other(isym.get_st_other());
1459 osym.put_st_shndx(st_shndx);
1461 dyn_ov += sym_size;
1466 if (output_size > 0)
1468 gold_assert(ov - oview == output_size);
1469 of->write_output_view(this->local_symbol_offset_, output_size, oview);
1472 if (dyn_output_size > 0)
1474 gold_assert(dyn_ov - dyn_oview == dyn_output_size);
1475 of->write_output_view(this->local_dynsym_offset_, dyn_output_size,
1476 dyn_oview);
1480 // Set *INFO to symbolic information about the offset OFFSET in the
1481 // section SHNDX. Return true if we found something, false if we
1482 // found nothing.
1484 template<int size, bool big_endian>
1485 bool
1486 Sized_relobj<size, big_endian>::get_symbol_location_info(
1487 unsigned int shndx,
1488 off_t offset,
1489 Symbol_location_info* info)
1491 if (this->symtab_shndx_ == 0)
1492 return false;
1494 section_size_type symbols_size;
1495 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
1496 &symbols_size,
1497 false);
1499 unsigned int symbol_names_shndx =
1500 this->adjust_shndx(this->section_link(this->symtab_shndx_));
1501 section_size_type names_size;
1502 const unsigned char* symbol_names_u =
1503 this->section_contents(symbol_names_shndx, &names_size, false);
1504 const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u);
1506 const int sym_size = This::sym_size;
1507 const size_t count = symbols_size / sym_size;
1509 const unsigned char* p = symbols;
1510 for (size_t i = 0; i < count; ++i, p += sym_size)
1512 elfcpp::Sym<size, big_endian> sym(p);
1514 if (sym.get_st_type() == elfcpp::STT_FILE)
1516 if (sym.get_st_name() >= names_size)
1517 info->source_file = "(invalid)";
1518 else
1519 info->source_file = symbol_names + sym.get_st_name();
1520 continue;
1523 bool is_ordinary;
1524 unsigned int st_shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
1525 &is_ordinary);
1526 if (is_ordinary
1527 && st_shndx == shndx
1528 && static_cast<off_t>(sym.get_st_value()) <= offset
1529 && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size())
1530 > offset))
1532 if (sym.get_st_name() > names_size)
1533 info->enclosing_symbol_name = "(invalid)";
1534 else
1536 info->enclosing_symbol_name = symbol_names + sym.get_st_name();
1537 if (parameters->options().do_demangle())
1539 char* demangled_name = cplus_demangle(
1540 info->enclosing_symbol_name.c_str(),
1541 DMGL_ANSI | DMGL_PARAMS);
1542 if (demangled_name != NULL)
1544 info->enclosing_symbol_name.assign(demangled_name);
1545 free(demangled_name);
1549 return true;
1553 return false;
1556 // Look for a kept section corresponding to the given discarded section,
1557 // and return its output address. This is used only for relocations in
1558 // debugging sections. If we can't find the kept section, return 0.
1560 template<int size, bool big_endian>
1561 typename Sized_relobj<size, big_endian>::Address
1562 Sized_relobj<size, big_endian>::map_to_kept_section(
1563 unsigned int shndx,
1564 bool* found) const
1566 Kept_comdat_section *kept = this->get_kept_comdat_section(shndx);
1567 if (kept != NULL)
1569 gold_assert(kept->object_ != NULL);
1570 *found = true;
1571 Output_section* os = kept->object_->output_section(kept->shndx_);
1572 Address offset = kept->object_->get_output_section_offset(kept->shndx_);
1573 gold_assert(os != NULL && offset != -1U);
1574 return os->address() + offset;
1576 *found = false;
1577 return 0;
1580 // Input_objects methods.
1582 // Add a regular relocatable object to the list. Return false if this
1583 // object should be ignored.
1585 bool
1586 Input_objects::add_object(Object* obj)
1588 // Set the global target from the first object file we recognize.
1589 Target* target = obj->target();
1590 if (!parameters->target_valid())
1591 set_parameters_target(target);
1592 else if (target != &parameters->target())
1594 obj->error(_("incompatible target"));
1595 return false;
1598 // Print the filename if the -t/--trace option is selected.
1599 if (parameters->options().trace())
1600 gold_info("%s", obj->name().c_str());
1602 if (!obj->is_dynamic())
1603 this->relobj_list_.push_back(static_cast<Relobj*>(obj));
1604 else
1606 // See if this is a duplicate SONAME.
1607 Dynobj* dynobj = static_cast<Dynobj*>(obj);
1608 const char* soname = dynobj->soname();
1610 std::pair<Unordered_set<std::string>::iterator, bool> ins =
1611 this->sonames_.insert(soname);
1612 if (!ins.second)
1614 // We have already seen a dynamic object with this soname.
1615 return false;
1618 this->dynobj_list_.push_back(dynobj);
1620 // If this is -lc, remember the directory in which we found it.
1621 // We use this when issuing warnings about undefined symbols: as
1622 // a heuristic, we don't warn about system libraries found in
1623 // the same directory as -lc.
1624 if (strncmp(soname, "libc.so", 7) == 0)
1626 const char* object_name = dynobj->name().c_str();
1627 const char* base = lbasename(object_name);
1628 if (base != object_name)
1629 this->system_library_directory_.assign(object_name,
1630 base - 1 - object_name);
1634 return true;
1637 // Return whether an object was found in the system library directory.
1639 bool
1640 Input_objects::found_in_system_library_directory(const Object* object) const
1642 return (!this->system_library_directory_.empty()
1643 && object->name().compare(0,
1644 this->system_library_directory_.size(),
1645 this->system_library_directory_) == 0);
1648 // For each dynamic object, record whether we've seen all of its
1649 // explicit dependencies.
1651 void
1652 Input_objects::check_dynamic_dependencies() const
1654 for (Dynobj_list::const_iterator p = this->dynobj_list_.begin();
1655 p != this->dynobj_list_.end();
1656 ++p)
1658 const Dynobj::Needed& needed((*p)->needed());
1659 bool found_all = true;
1660 for (Dynobj::Needed::const_iterator pneeded = needed.begin();
1661 pneeded != needed.end();
1662 ++pneeded)
1664 if (this->sonames_.find(*pneeded) == this->sonames_.end())
1666 found_all = false;
1667 break;
1670 (*p)->set_has_unknown_needed_entries(!found_all);
1674 // Relocate_info methods.
1676 // Return a string describing the location of a relocation. This is
1677 // only used in error messages.
1679 template<int size, bool big_endian>
1680 std::string
1681 Relocate_info<size, big_endian>::location(size_t, off_t offset) const
1683 // See if we can get line-number information from debugging sections.
1684 std::string filename;
1685 std::string file_and_lineno; // Better than filename-only, if available.
1687 Sized_dwarf_line_info<size, big_endian> line_info(this->object);
1688 // This will be "" if we failed to parse the debug info for any reason.
1689 file_and_lineno = line_info.addr2line(this->data_shndx, offset);
1691 std::string ret(this->object->name());
1692 ret += ':';
1693 Symbol_location_info info;
1694 if (this->object->get_symbol_location_info(this->data_shndx, offset, &info))
1696 ret += " in function ";
1697 ret += info.enclosing_symbol_name;
1698 ret += ":";
1699 filename = info.source_file;
1702 if (!file_and_lineno.empty())
1703 ret += file_and_lineno;
1704 else
1706 if (!filename.empty())
1707 ret += filename;
1708 ret += "(";
1709 ret += this->object->section_name(this->data_shndx);
1710 char buf[100];
1711 // Offsets into sections have to be positive.
1712 snprintf(buf, sizeof(buf), "+0x%lx", static_cast<long>(offset));
1713 ret += buf;
1714 ret += ")";
1716 return ret;
1719 } // End namespace gold.
1721 namespace
1724 using namespace gold;
1726 // Read an ELF file with the header and return the appropriate
1727 // instance of Object.
1729 template<int size, bool big_endian>
1730 Object*
1731 make_elf_sized_object(const std::string& name, Input_file* input_file,
1732 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1734 int et = ehdr.get_e_type();
1735 if (et == elfcpp::ET_REL)
1737 Sized_relobj<size, big_endian>* obj =
1738 new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
1739 obj->setup(ehdr);
1740 return obj;
1742 else if (et == elfcpp::ET_DYN)
1744 Sized_dynobj<size, big_endian>* obj =
1745 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1746 obj->setup(ehdr);
1747 return obj;
1749 else
1751 gold_error(_("%s: unsupported ELF file type %d"),
1752 name.c_str(), et);
1753 return NULL;
1757 } // End anonymous namespace.
1759 namespace gold
1762 // Read an ELF file and return the appropriate instance of Object.
1764 Object*
1765 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
1766 const unsigned char* p, section_offset_type bytes)
1768 if (bytes < elfcpp::EI_NIDENT)
1770 gold_error(_("%s: ELF file too short"), name.c_str());
1771 return NULL;
1774 int v = p[elfcpp::EI_VERSION];
1775 if (v != elfcpp::EV_CURRENT)
1777 if (v == elfcpp::EV_NONE)
1778 gold_error(_("%s: invalid ELF version 0"), name.c_str());
1779 else
1780 gold_error(_("%s: unsupported ELF version %d"), name.c_str(), v);
1781 return NULL;
1784 int c = p[elfcpp::EI_CLASS];
1785 if (c == elfcpp::ELFCLASSNONE)
1787 gold_error(_("%s: invalid ELF class 0"), name.c_str());
1788 return NULL;
1790 else if (c != elfcpp::ELFCLASS32
1791 && c != elfcpp::ELFCLASS64)
1793 gold_error(_("%s: unsupported ELF class %d"), name.c_str(), c);
1794 return NULL;
1797 int d = p[elfcpp::EI_DATA];
1798 if (d == elfcpp::ELFDATANONE)
1800 gold_error(_("%s: invalid ELF data encoding"), name.c_str());
1801 return NULL;
1803 else if (d != elfcpp::ELFDATA2LSB
1804 && d != elfcpp::ELFDATA2MSB)
1806 gold_error(_("%s: unsupported ELF data encoding %d"), name.c_str(), d);
1807 return NULL;
1810 bool big_endian = d == elfcpp::ELFDATA2MSB;
1812 if (c == elfcpp::ELFCLASS32)
1814 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1816 gold_error(_("%s: ELF file too short"), name.c_str());
1817 return NULL;
1819 if (big_endian)
1821 #ifdef HAVE_TARGET_32_BIG
1822 elfcpp::Ehdr<32, true> ehdr(p);
1823 return make_elf_sized_object<32, true>(name, input_file,
1824 offset, ehdr);
1825 #else
1826 gold_error(_("%s: not configured to support "
1827 "32-bit big-endian object"),
1828 name.c_str());
1829 return NULL;
1830 #endif
1832 else
1834 #ifdef HAVE_TARGET_32_LITTLE
1835 elfcpp::Ehdr<32, false> ehdr(p);
1836 return make_elf_sized_object<32, false>(name, input_file,
1837 offset, ehdr);
1838 #else
1839 gold_error(_("%s: not configured to support "
1840 "32-bit little-endian object"),
1841 name.c_str());
1842 return NULL;
1843 #endif
1846 else
1848 if (bytes < elfcpp::Elf_sizes<64>::ehdr_size)
1850 gold_error(_("%s: ELF file too short"), name.c_str());
1851 return NULL;
1853 if (big_endian)
1855 #ifdef HAVE_TARGET_64_BIG
1856 elfcpp::Ehdr<64, true> ehdr(p);
1857 return make_elf_sized_object<64, true>(name, input_file,
1858 offset, ehdr);
1859 #else
1860 gold_error(_("%s: not configured to support "
1861 "64-bit big-endian object"),
1862 name.c_str());
1863 return NULL;
1864 #endif
1866 else
1868 #ifdef HAVE_TARGET_64_LITTLE
1869 elfcpp::Ehdr<64, false> ehdr(p);
1870 return make_elf_sized_object<64, false>(name, input_file,
1871 offset, ehdr);
1872 #else
1873 gold_error(_("%s: not configured to support "
1874 "64-bit little-endian object"),
1875 name.c_str());
1876 return NULL;
1877 #endif
1882 // Instantiate the templates we need.
1884 #ifdef HAVE_TARGET_32_LITTLE
1885 template
1886 void
1887 Object::read_section_data<32, false>(elfcpp::Elf_file<32, false, Object>*,
1888 Read_symbols_data*);
1889 #endif
1891 #ifdef HAVE_TARGET_32_BIG
1892 template
1893 void
1894 Object::read_section_data<32, true>(elfcpp::Elf_file<32, true, Object>*,
1895 Read_symbols_data*);
1896 #endif
1898 #ifdef HAVE_TARGET_64_LITTLE
1899 template
1900 void
1901 Object::read_section_data<64, false>(elfcpp::Elf_file<64, false, Object>*,
1902 Read_symbols_data*);
1903 #endif
1905 #ifdef HAVE_TARGET_64_BIG
1906 template
1907 void
1908 Object::read_section_data<64, true>(elfcpp::Elf_file<64, true, Object>*,
1909 Read_symbols_data*);
1910 #endif
1912 #ifdef HAVE_TARGET_32_LITTLE
1913 template
1914 class Sized_relobj<32, false>;
1915 #endif
1917 #ifdef HAVE_TARGET_32_BIG
1918 template
1919 class Sized_relobj<32, true>;
1920 #endif
1922 #ifdef HAVE_TARGET_64_LITTLE
1923 template
1924 class Sized_relobj<64, false>;
1925 #endif
1927 #ifdef HAVE_TARGET_64_BIG
1928 template
1929 class Sized_relobj<64, true>;
1930 #endif
1932 #ifdef HAVE_TARGET_32_LITTLE
1933 template
1934 struct Relocate_info<32, false>;
1935 #endif
1937 #ifdef HAVE_TARGET_32_BIG
1938 template
1939 struct Relocate_info<32, true>;
1940 #endif
1942 #ifdef HAVE_TARGET_64_LITTLE
1943 template
1944 struct Relocate_info<64, false>;
1945 #endif
1947 #ifdef HAVE_TARGET_64_BIG
1948 template
1949 struct Relocate_info<64, true>;
1950 #endif
1952 } // End namespace gold.