1 // output.cc -- manage the output file for gold
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 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.
33 #ifdef HAVE_SYS_MMAN_H
37 #include "libiberty.h"
39 #include "parameters.h"
44 #include "descriptors.h"
47 // For systems without mmap support.
49 # define mmap gold_mmap
50 # define munmap gold_munmap
51 # define mremap gold_mremap
53 # define MAP_FAILED (reinterpret_cast<void*>(-1))
62 # define MAP_PRIVATE 0
64 # ifndef MAP_ANONYMOUS
65 # define MAP_ANONYMOUS 0
72 # define ENOSYS EINVAL
76 gold_mmap(void *, size_t, int, int, int, off_t)
83 gold_munmap(void *, size_t)
90 gold_mremap(void *, size_t, size_t, int)
98 #if defined(HAVE_MMAP) && !defined(HAVE_MREMAP)
99 # define mremap gold_mremap
100 extern "C" void *gold_mremap(void *, size_t, size_t, int);
103 // Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS
104 #ifndef MAP_ANONYMOUS
105 # define MAP_ANONYMOUS MAP_ANON
108 #ifndef MREMAP_MAYMOVE
109 # define MREMAP_MAYMOVE 1
112 #ifndef HAVE_POSIX_FALLOCATE
113 // A dummy, non general, version of posix_fallocate. Here we just set
114 // the file size and hope that there is enough disk space. FIXME: We
115 // could allocate disk space by walking block by block and writing a
116 // zero byte into each block.
118 posix_fallocate(int o, off_t offset, off_t len)
120 return ftruncate(o, offset + len);
122 #endif // !defined(HAVE_POSIX_FALLOCATE)
127 // Output_data variables.
129 bool Output_data::allocated_sizes_are_fixed;
131 // Output_data methods.
133 Output_data::~Output_data()
137 // Return the default alignment for the target size.
140 Output_data::default_alignment()
142 return Output_data::default_alignment_for_size(
143 parameters->target().get_size());
146 // Return the default alignment for a size--32 or 64.
149 Output_data::default_alignment_for_size(int size)
159 // Output_section_header methods. This currently assumes that the
160 // segment and section lists are complete at construction time.
162 Output_section_headers::Output_section_headers(
163 const Layout* layout,
164 const Layout::Segment_list* segment_list,
165 const Layout::Section_list* section_list,
166 const Layout::Section_list* unattached_section_list,
167 const Stringpool* secnamepool,
168 const Output_section* shstrtab_section)
170 segment_list_(segment_list),
171 section_list_(section_list),
172 unattached_section_list_(unattached_section_list),
173 secnamepool_(secnamepool),
174 shstrtab_section_(shstrtab_section)
178 // Compute the current data size.
181 Output_section_headers::do_size() const
183 // Count all the sections. Start with 1 for the null section.
185 if (!parameters->options().relocatable())
187 for (Layout::Segment_list::const_iterator p =
188 this->segment_list_->begin();
189 p != this->segment_list_->end();
191 if ((*p)->type() == elfcpp::PT_LOAD)
192 count += (*p)->output_section_count();
196 for (Layout::Section_list::const_iterator p =
197 this->section_list_->begin();
198 p != this->section_list_->end();
200 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
203 count += this->unattached_section_list_->size();
205 const int size = parameters->target().get_size();
208 shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
210 shdr_size = elfcpp::Elf_sizes<64>::shdr_size;
214 return count * shdr_size;
217 // Write out the section headers.
220 Output_section_headers::do_write(Output_file* of)
222 switch (parameters->size_and_endianness())
224 #ifdef HAVE_TARGET_32_LITTLE
225 case Parameters::TARGET_32_LITTLE:
226 this->do_sized_write<32, false>(of);
229 #ifdef HAVE_TARGET_32_BIG
230 case Parameters::TARGET_32_BIG:
231 this->do_sized_write<32, true>(of);
234 #ifdef HAVE_TARGET_64_LITTLE
235 case Parameters::TARGET_64_LITTLE:
236 this->do_sized_write<64, false>(of);
239 #ifdef HAVE_TARGET_64_BIG
240 case Parameters::TARGET_64_BIG:
241 this->do_sized_write<64, true>(of);
249 template<int size, bool big_endian>
251 Output_section_headers::do_sized_write(Output_file* of)
253 off_t all_shdrs_size = this->data_size();
254 unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size);
256 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
257 unsigned char* v = view;
260 typename elfcpp::Shdr_write<size, big_endian> oshdr(v);
261 oshdr.put_sh_name(0);
262 oshdr.put_sh_type(elfcpp::SHT_NULL);
263 oshdr.put_sh_flags(0);
264 oshdr.put_sh_addr(0);
265 oshdr.put_sh_offset(0);
267 size_t section_count = (this->data_size()
268 / elfcpp::Elf_sizes<size>::shdr_size);
269 if (section_count < elfcpp::SHN_LORESERVE)
270 oshdr.put_sh_size(0);
272 oshdr.put_sh_size(section_count);
274 unsigned int shstrndx = this->shstrtab_section_->out_shndx();
275 if (shstrndx < elfcpp::SHN_LORESERVE)
276 oshdr.put_sh_link(0);
278 oshdr.put_sh_link(shstrndx);
280 size_t segment_count = this->segment_list_->size();
281 oshdr.put_sh_info(segment_count >= elfcpp::PN_XNUM ? segment_count : 0);
283 oshdr.put_sh_addralign(0);
284 oshdr.put_sh_entsize(0);
289 unsigned int shndx = 1;
290 if (!parameters->options().relocatable())
292 for (Layout::Segment_list::const_iterator p =
293 this->segment_list_->begin();
294 p != this->segment_list_->end();
296 v = (*p)->write_section_headers<size, big_endian>(this->layout_,
303 for (Layout::Section_list::const_iterator p =
304 this->section_list_->begin();
305 p != this->section_list_->end();
308 // We do unallocated sections below, except that group
309 // sections have to come first.
310 if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
311 && (*p)->type() != elfcpp::SHT_GROUP)
313 gold_assert(shndx == (*p)->out_shndx());
314 elfcpp::Shdr_write<size, big_endian> oshdr(v);
315 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
321 for (Layout::Section_list::const_iterator p =
322 this->unattached_section_list_->begin();
323 p != this->unattached_section_list_->end();
326 // For a relocatable link, we did unallocated group sections
327 // above, since they have to come first.
328 if ((*p)->type() == elfcpp::SHT_GROUP
329 && parameters->options().relocatable())
331 gold_assert(shndx == (*p)->out_shndx());
332 elfcpp::Shdr_write<size, big_endian> oshdr(v);
333 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
338 of->write_output_view(this->offset(), all_shdrs_size, view);
341 // Output_segment_header methods.
343 Output_segment_headers::Output_segment_headers(
344 const Layout::Segment_list& segment_list)
345 : segment_list_(segment_list)
347 this->set_current_data_size_for_child(this->do_size());
351 Output_segment_headers::do_write(Output_file* of)
353 switch (parameters->size_and_endianness())
355 #ifdef HAVE_TARGET_32_LITTLE
356 case Parameters::TARGET_32_LITTLE:
357 this->do_sized_write<32, false>(of);
360 #ifdef HAVE_TARGET_32_BIG
361 case Parameters::TARGET_32_BIG:
362 this->do_sized_write<32, true>(of);
365 #ifdef HAVE_TARGET_64_LITTLE
366 case Parameters::TARGET_64_LITTLE:
367 this->do_sized_write<64, false>(of);
370 #ifdef HAVE_TARGET_64_BIG
371 case Parameters::TARGET_64_BIG:
372 this->do_sized_write<64, true>(of);
380 template<int size, bool big_endian>
382 Output_segment_headers::do_sized_write(Output_file* of)
384 const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size;
385 off_t all_phdrs_size = this->segment_list_.size() * phdr_size;
386 gold_assert(all_phdrs_size == this->data_size());
387 unsigned char* view = of->get_output_view(this->offset(),
389 unsigned char* v = view;
390 for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
391 p != this->segment_list_.end();
394 elfcpp::Phdr_write<size, big_endian> ophdr(v);
395 (*p)->write_header(&ophdr);
399 gold_assert(v - view == all_phdrs_size);
401 of->write_output_view(this->offset(), all_phdrs_size, view);
405 Output_segment_headers::do_size() const
407 const int size = parameters->target().get_size();
410 phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
412 phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
416 return this->segment_list_.size() * phdr_size;
419 // Output_file_header methods.
421 Output_file_header::Output_file_header(const Target* target,
422 const Symbol_table* symtab,
423 const Output_segment_headers* osh,
427 segment_header_(osh),
428 section_header_(NULL),
432 this->set_data_size(this->do_size());
435 // Set the section table information for a file header.
438 Output_file_header::set_section_info(const Output_section_headers* shdrs,
439 const Output_section* shstrtab)
441 this->section_header_ = shdrs;
442 this->shstrtab_ = shstrtab;
445 // Write out the file header.
448 Output_file_header::do_write(Output_file* of)
450 gold_assert(this->offset() == 0);
452 switch (parameters->size_and_endianness())
454 #ifdef HAVE_TARGET_32_LITTLE
455 case Parameters::TARGET_32_LITTLE:
456 this->do_sized_write<32, false>(of);
459 #ifdef HAVE_TARGET_32_BIG
460 case Parameters::TARGET_32_BIG:
461 this->do_sized_write<32, true>(of);
464 #ifdef HAVE_TARGET_64_LITTLE
465 case Parameters::TARGET_64_LITTLE:
466 this->do_sized_write<64, false>(of);
469 #ifdef HAVE_TARGET_64_BIG
470 case Parameters::TARGET_64_BIG:
471 this->do_sized_write<64, true>(of);
479 // Write out the file header with appropriate size and endianess.
481 template<int size, bool big_endian>
483 Output_file_header::do_sized_write(Output_file* of)
485 gold_assert(this->offset() == 0);
487 int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
488 unsigned char* view = of->get_output_view(0, ehdr_size);
489 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
491 unsigned char e_ident[elfcpp::EI_NIDENT];
492 memset(e_ident, 0, elfcpp::EI_NIDENT);
493 e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
494 e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
495 e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
496 e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
498 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
500 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
503 e_ident[elfcpp::EI_DATA] = (big_endian
504 ? elfcpp::ELFDATA2MSB
505 : elfcpp::ELFDATA2LSB);
506 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
507 oehdr.put_e_ident(e_ident);
510 if (parameters->options().relocatable())
511 e_type = elfcpp::ET_REL;
512 else if (parameters->options().output_is_position_independent())
513 e_type = elfcpp::ET_DYN;
515 e_type = elfcpp::ET_EXEC;
516 oehdr.put_e_type(e_type);
518 oehdr.put_e_machine(this->target_->machine_code());
519 oehdr.put_e_version(elfcpp::EV_CURRENT);
521 oehdr.put_e_entry(this->entry<size>());
523 if (this->segment_header_ == NULL)
524 oehdr.put_e_phoff(0);
526 oehdr.put_e_phoff(this->segment_header_->offset());
528 oehdr.put_e_shoff(this->section_header_->offset());
529 oehdr.put_e_flags(this->target_->processor_specific_flags());
530 oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
532 if (this->segment_header_ == NULL)
534 oehdr.put_e_phentsize(0);
535 oehdr.put_e_phnum(0);
539 oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
540 size_t phnum = (this->segment_header_->data_size()
541 / elfcpp::Elf_sizes<size>::phdr_size);
542 if (phnum > elfcpp::PN_XNUM)
543 phnum = elfcpp::PN_XNUM;
544 oehdr.put_e_phnum(phnum);
547 oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
548 size_t section_count = (this->section_header_->data_size()
549 / elfcpp::Elf_sizes<size>::shdr_size);
551 if (section_count < elfcpp::SHN_LORESERVE)
552 oehdr.put_e_shnum(this->section_header_->data_size()
553 / elfcpp::Elf_sizes<size>::shdr_size);
555 oehdr.put_e_shnum(0);
557 unsigned int shstrndx = this->shstrtab_->out_shndx();
558 if (shstrndx < elfcpp::SHN_LORESERVE)
559 oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
561 oehdr.put_e_shstrndx(elfcpp::SHN_XINDEX);
563 // Let the target adjust the ELF header, e.g., to set EI_OSABI in
564 // the e_ident field.
565 parameters->target().adjust_elf_header(view, ehdr_size);
567 of->write_output_view(0, ehdr_size, view);
570 // Return the value to use for the entry address. THIS->ENTRY_ is the
571 // symbol specified on the command line, if any.
574 typename elfcpp::Elf_types<size>::Elf_Addr
575 Output_file_header::entry()
577 const bool should_issue_warning = (this->entry_ != NULL
578 && !parameters->options().relocatable()
579 && !parameters->options().shared());
581 // FIXME: Need to support target specific entry symbol.
582 const char* entry = this->entry_;
586 Symbol* sym = this->symtab_->lookup(entry);
588 typename Sized_symbol<size>::Value_type v;
591 Sized_symbol<size>* ssym;
592 ssym = this->symtab_->get_sized_symbol<size>(sym);
593 if (!ssym->is_defined() && should_issue_warning)
594 gold_warning("entry symbol '%s' exists but is not defined", entry);
599 // We couldn't find the entry symbol. See if we can parse it as
600 // a number. This supports, e.g., -e 0x1000.
602 v = strtoull(entry, &endptr, 0);
605 if (should_issue_warning)
606 gold_warning("cannot find entry symbol '%s'", entry);
614 // Compute the current data size.
617 Output_file_header::do_size() const
619 const int size = parameters->target().get_size();
621 return elfcpp::Elf_sizes<32>::ehdr_size;
623 return elfcpp::Elf_sizes<64>::ehdr_size;
628 // Output_data_const methods.
631 Output_data_const::do_write(Output_file* of)
633 of->write(this->offset(), this->data_.data(), this->data_.size());
636 // Output_data_const_buffer methods.
639 Output_data_const_buffer::do_write(Output_file* of)
641 of->write(this->offset(), this->p_, this->data_size());
644 // Output_section_data methods.
646 // Record the output section, and set the entry size and such.
649 Output_section_data::set_output_section(Output_section* os)
651 gold_assert(this->output_section_ == NULL);
652 this->output_section_ = os;
653 this->do_adjust_output_section(os);
656 // Return the section index of the output section.
659 Output_section_data::do_out_shndx() const
661 gold_assert(this->output_section_ != NULL);
662 return this->output_section_->out_shndx();
665 // Set the alignment, which means we may need to update the alignment
666 // of the output section.
669 Output_section_data::set_addralign(uint64_t addralign)
671 this->addralign_ = addralign;
672 if (this->output_section_ != NULL
673 && this->output_section_->addralign() < addralign)
674 this->output_section_->set_addralign(addralign);
677 // Output_data_strtab methods.
679 // Set the final data size.
682 Output_data_strtab::set_final_data_size()
684 this->strtab_->set_string_offsets();
685 this->set_data_size(this->strtab_->get_strtab_size());
688 // Write out a string table.
691 Output_data_strtab::do_write(Output_file* of)
693 this->strtab_->write(of, this->offset());
696 // Output_reloc methods.
698 // A reloc against a global symbol.
700 template<bool dynamic, int size, bool big_endian>
701 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
708 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
709 is_relative_(is_relative), is_symbolless_(is_symbolless),
710 is_section_symbol_(false), shndx_(INVALID_CODE)
712 // this->type_ is a bitfield; make sure TYPE fits.
713 gold_assert(this->type_ == type);
714 this->u1_.gsym = gsym;
717 this->set_needs_dynsym_index();
720 template<bool dynamic, int size, bool big_endian>
721 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
724 Sized_relobj<size, big_endian>* relobj,
729 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
730 is_relative_(is_relative), is_symbolless_(is_symbolless),
731 is_section_symbol_(false), shndx_(shndx)
733 gold_assert(shndx != INVALID_CODE);
734 // this->type_ is a bitfield; make sure TYPE fits.
735 gold_assert(this->type_ == type);
736 this->u1_.gsym = gsym;
737 this->u2_.relobj = relobj;
739 this->set_needs_dynsym_index();
742 // A reloc against a local symbol.
744 template<bool dynamic, int size, bool big_endian>
745 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
746 Sized_relobj<size, big_endian>* relobj,
747 unsigned int local_sym_index,
753 bool is_section_symbol)
754 : address_(address), local_sym_index_(local_sym_index), type_(type),
755 is_relative_(is_relative), is_symbolless_(is_symbolless),
756 is_section_symbol_(is_section_symbol), shndx_(INVALID_CODE)
758 gold_assert(local_sym_index != GSYM_CODE
759 && local_sym_index != INVALID_CODE);
760 // this->type_ is a bitfield; make sure TYPE fits.
761 gold_assert(this->type_ == type);
762 this->u1_.relobj = relobj;
765 this->set_needs_dynsym_index();
768 template<bool dynamic, int size, bool big_endian>
769 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
770 Sized_relobj<size, big_endian>* relobj,
771 unsigned int local_sym_index,
777 bool is_section_symbol)
778 : address_(address), local_sym_index_(local_sym_index), type_(type),
779 is_relative_(is_relative), is_symbolless_(is_symbolless),
780 is_section_symbol_(is_section_symbol), shndx_(shndx)
782 gold_assert(local_sym_index != GSYM_CODE
783 && local_sym_index != INVALID_CODE);
784 gold_assert(shndx != INVALID_CODE);
785 // this->type_ is a bitfield; make sure TYPE fits.
786 gold_assert(this->type_ == type);
787 this->u1_.relobj = relobj;
788 this->u2_.relobj = relobj;
790 this->set_needs_dynsym_index();
793 // A reloc against the STT_SECTION symbol of an output section.
795 template<bool dynamic, int size, bool big_endian>
796 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
801 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
802 is_relative_(false), is_symbolless_(false),
803 is_section_symbol_(true), shndx_(INVALID_CODE)
805 // this->type_ is a bitfield; make sure TYPE fits.
806 gold_assert(this->type_ == type);
810 this->set_needs_dynsym_index();
812 os->set_needs_symtab_index();
815 template<bool dynamic, int size, bool big_endian>
816 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
819 Sized_relobj<size, big_endian>* relobj,
822 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
823 is_relative_(false), is_symbolless_(false),
824 is_section_symbol_(true), shndx_(shndx)
826 gold_assert(shndx != INVALID_CODE);
827 // this->type_ is a bitfield; make sure TYPE fits.
828 gold_assert(this->type_ == type);
830 this->u2_.relobj = relobj;
832 this->set_needs_dynsym_index();
834 os->set_needs_symtab_index();
837 // An absolute relocation.
839 template<bool dynamic, int size, bool big_endian>
840 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
844 : address_(address), local_sym_index_(0), type_(type),
845 is_relative_(false), is_symbolless_(false),
846 is_section_symbol_(false), shndx_(INVALID_CODE)
848 // this->type_ is a bitfield; make sure TYPE fits.
849 gold_assert(this->type_ == type);
850 this->u1_.relobj = NULL;
854 template<bool dynamic, int size, bool big_endian>
855 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
857 Sized_relobj<size, big_endian>* relobj,
860 : address_(address), local_sym_index_(0), type_(type),
861 is_relative_(false), is_symbolless_(false),
862 is_section_symbol_(false), shndx_(shndx)
864 gold_assert(shndx != INVALID_CODE);
865 // this->type_ is a bitfield; make sure TYPE fits.
866 gold_assert(this->type_ == type);
867 this->u1_.relobj = NULL;
868 this->u2_.relobj = relobj;
871 // A target specific relocation.
873 template<bool dynamic, int size, bool big_endian>
874 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
879 : address_(address), local_sym_index_(TARGET_CODE), type_(type),
880 is_relative_(false), is_symbolless_(false),
881 is_section_symbol_(false), shndx_(INVALID_CODE)
883 // this->type_ is a bitfield; make sure TYPE fits.
884 gold_assert(this->type_ == type);
889 template<bool dynamic, int size, bool big_endian>
890 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
893 Sized_relobj<size, big_endian>* relobj,
896 : address_(address), local_sym_index_(TARGET_CODE), type_(type),
897 is_relative_(false), is_symbolless_(false),
898 is_section_symbol_(false), shndx_(shndx)
900 gold_assert(shndx != INVALID_CODE);
901 // this->type_ is a bitfield; make sure TYPE fits.
902 gold_assert(this->type_ == type);
904 this->u2_.relobj = relobj;
907 // Record that we need a dynamic symbol index for this relocation.
909 template<bool dynamic, int size, bool big_endian>
911 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
912 set_needs_dynsym_index()
914 if (this->is_symbolless_)
916 switch (this->local_sym_index_)
922 this->u1_.gsym->set_needs_dynsym_entry();
926 this->u1_.os->set_needs_dynsym_index();
930 // The target must take care of this if necessary.
938 const unsigned int lsi = this->local_sym_index_;
939 if (!this->is_section_symbol_)
940 this->u1_.relobj->set_needs_output_dynsym_entry(lsi);
942 this->u1_.relobj->output_section(lsi)->set_needs_dynsym_index();
948 // Get the symbol index of a relocation.
950 template<bool dynamic, int size, bool big_endian>
952 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
956 if (this->is_symbolless_)
958 switch (this->local_sym_index_)
964 if (this->u1_.gsym == NULL)
967 index = this->u1_.gsym->dynsym_index();
969 index = this->u1_.gsym->symtab_index();
974 index = this->u1_.os->dynsym_index();
976 index = this->u1_.os->symtab_index();
980 index = parameters->target().reloc_symbol_index(this->u1_.arg,
985 // Relocations without symbols use a symbol index of 0.
991 const unsigned int lsi = this->local_sym_index_;
992 if (!this->is_section_symbol_)
995 index = this->u1_.relobj->dynsym_index(lsi);
997 index = this->u1_.relobj->symtab_index(lsi);
1001 Output_section* os = this->u1_.relobj->output_section(lsi);
1002 gold_assert(os != NULL);
1004 index = os->dynsym_index();
1006 index = os->symtab_index();
1011 gold_assert(index != -1U);
1015 // For a local section symbol, get the address of the offset ADDEND
1016 // within the input section.
1018 template<bool dynamic, int size, bool big_endian>
1019 typename elfcpp::Elf_types<size>::Elf_Addr
1020 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
1021 local_section_offset(Addend addend) const
1023 gold_assert(this->local_sym_index_ != GSYM_CODE
1024 && this->local_sym_index_ != SECTION_CODE
1025 && this->local_sym_index_ != TARGET_CODE
1026 && this->local_sym_index_ != INVALID_CODE
1027 && this->local_sym_index_ != 0
1028 && this->is_section_symbol_);
1029 const unsigned int lsi = this->local_sym_index_;
1030 Output_section* os = this->u1_.relobj->output_section(lsi);
1031 gold_assert(os != NULL);
1032 Address offset = this->u1_.relobj->get_output_section_offset(lsi);
1033 if (offset != invalid_address)
1034 return offset + addend;
1035 // This is a merge section.
1036 offset = os->output_address(this->u1_.relobj, lsi, addend);
1037 gold_assert(offset != invalid_address);
1041 // Get the output address of a relocation.
1043 template<bool dynamic, int size, bool big_endian>
1044 typename elfcpp::Elf_types<size>::Elf_Addr
1045 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_address() const
1047 Address address = this->address_;
1048 if (this->shndx_ != INVALID_CODE)
1050 Output_section* os = this->u2_.relobj->output_section(this->shndx_);
1051 gold_assert(os != NULL);
1052 Address off = this->u2_.relobj->get_output_section_offset(this->shndx_);
1053 if (off != invalid_address)
1054 address += os->address() + off;
1057 address = os->output_address(this->u2_.relobj, this->shndx_,
1059 gold_assert(address != invalid_address);
1062 else if (this->u2_.od != NULL)
1063 address += this->u2_.od->address();
1067 // Write out the offset and info fields of a Rel or Rela relocation
1070 template<bool dynamic, int size, bool big_endian>
1071 template<typename Write_rel>
1073 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
1074 Write_rel* wr) const
1076 wr->put_r_offset(this->get_address());
1077 unsigned int sym_index = this->get_symbol_index();
1078 wr->put_r_info(elfcpp::elf_r_info<size>(sym_index, this->type_));
1081 // Write out a Rel relocation.
1083 template<bool dynamic, int size, bool big_endian>
1085 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
1086 unsigned char* pov) const
1088 elfcpp::Rel_write<size, big_endian> orel(pov);
1089 this->write_rel(&orel);
1092 // Get the value of the symbol referred to by a Rel relocation.
1094 template<bool dynamic, int size, bool big_endian>
1095 typename elfcpp::Elf_types<size>::Elf_Addr
1096 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value(
1097 Addend addend) const
1099 if (this->local_sym_index_ == GSYM_CODE)
1101 const Sized_symbol<size>* sym;
1102 sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym);
1103 return sym->value() + addend;
1105 gold_assert(this->local_sym_index_ != SECTION_CODE
1106 && this->local_sym_index_ != TARGET_CODE
1107 && this->local_sym_index_ != INVALID_CODE
1108 && this->local_sym_index_ != 0
1109 && !this->is_section_symbol_);
1110 const unsigned int lsi = this->local_sym_index_;
1111 const Symbol_value<size>* symval = this->u1_.relobj->local_symbol(lsi);
1112 return symval->value(this->u1_.relobj, addend);
1115 // Reloc comparison. This function sorts the dynamic relocs for the
1116 // benefit of the dynamic linker. First we sort all relative relocs
1117 // to the front. Among relative relocs, we sort by output address.
1118 // Among non-relative relocs, we sort by symbol index, then by output
1121 template<bool dynamic, int size, bool big_endian>
1123 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
1124 compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
1127 if (this->is_relative_)
1129 if (!r2.is_relative_)
1131 // Otherwise sort by reloc address below.
1133 else if (r2.is_relative_)
1137 unsigned int sym1 = this->get_symbol_index();
1138 unsigned int sym2 = r2.get_symbol_index();
1141 else if (sym1 > sym2)
1143 // Otherwise sort by reloc address.
1146 section_offset_type addr1 = this->get_address();
1147 section_offset_type addr2 = r2.get_address();
1150 else if (addr1 > addr2)
1153 // Final tie breaker, in order to generate the same output on any
1154 // host: reloc type.
1155 unsigned int type1 = this->type_;
1156 unsigned int type2 = r2.type_;
1159 else if (type1 > type2)
1162 // These relocs appear to be exactly the same.
1166 // Write out a Rela relocation.
1168 template<bool dynamic, int size, bool big_endian>
1170 Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
1171 unsigned char* pov) const
1173 elfcpp::Rela_write<size, big_endian> orel(pov);
1174 this->rel_.write_rel(&orel);
1175 Addend addend = this->addend_;
1176 if (this->rel_.is_target_specific())
1177 addend = parameters->target().reloc_addend(this->rel_.target_arg(),
1178 this->rel_.type(), addend);
1179 else if (this->rel_.is_symbolless())
1180 addend = this->rel_.symbol_value(addend);
1181 else if (this->rel_.is_local_section_symbol())
1182 addend = this->rel_.local_section_offset(addend);
1183 orel.put_r_addend(addend);
1186 // Output_data_reloc_base methods.
1188 // Adjust the output section.
1190 template<int sh_type, bool dynamic, int size, bool big_endian>
1192 Output_data_reloc_base<sh_type, dynamic, size, big_endian>
1193 ::do_adjust_output_section(Output_section* os)
1195 if (sh_type == elfcpp::SHT_REL)
1196 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
1197 else if (sh_type == elfcpp::SHT_RELA)
1198 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
1202 // A STT_GNU_IFUNC symbol may require a IRELATIVE reloc when doing a
1203 // static link. The backends will generate a dynamic reloc section
1204 // to hold this. In that case we don't want to link to the dynsym
1205 // section, because there isn't one.
1207 os->set_should_link_to_symtab();
1208 else if (parameters->doing_static_link())
1211 os->set_should_link_to_dynsym();
1214 // Write out relocation data.
1216 template<int sh_type, bool dynamic, int size, bool big_endian>
1218 Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
1221 const off_t off = this->offset();
1222 const off_t oview_size = this->data_size();
1223 unsigned char* const oview = of->get_output_view(off, oview_size);
1225 if (this->sort_relocs())
1227 gold_assert(dynamic);
1228 std::sort(this->relocs_.begin(), this->relocs_.end(),
1229 Sort_relocs_comparison());
1232 unsigned char* pov = oview;
1233 for (typename Relocs::const_iterator p = this->relocs_.begin();
1234 p != this->relocs_.end();
1241 gold_assert(pov - oview == oview_size);
1243 of->write_output_view(off, oview_size, oview);
1245 // We no longer need the relocation entries.
1246 this->relocs_.clear();
1249 // Class Output_relocatable_relocs.
1251 template<int sh_type, int size, bool big_endian>
1253 Output_relocatable_relocs<sh_type, size, big_endian>::set_final_data_size()
1255 this->set_data_size(this->rr_->output_reloc_count()
1256 * Reloc_types<sh_type, size, big_endian>::reloc_size);
1259 // class Output_data_group.
1261 template<int size, bool big_endian>
1262 Output_data_group<size, big_endian>::Output_data_group(
1263 Sized_relobj<size, big_endian>* relobj,
1264 section_size_type entry_count,
1265 elfcpp::Elf_Word flags,
1266 std::vector<unsigned int>* input_shndxes)
1267 : Output_section_data(entry_count * 4, 4, false),
1271 this->input_shndxes_.swap(*input_shndxes);
1274 // Write out the section group, which means translating the section
1275 // indexes to apply to the output file.
1277 template<int size, bool big_endian>
1279 Output_data_group<size, big_endian>::do_write(Output_file* of)
1281 const off_t off = this->offset();
1282 const section_size_type oview_size =
1283 convert_to_section_size_type(this->data_size());
1284 unsigned char* const oview = of->get_output_view(off, oview_size);
1286 elfcpp::Elf_Word* contents = reinterpret_cast<elfcpp::Elf_Word*>(oview);
1287 elfcpp::Swap<32, big_endian>::writeval(contents, this->flags_);
1290 for (std::vector<unsigned int>::const_iterator p =
1291 this->input_shndxes_.begin();
1292 p != this->input_shndxes_.end();
1295 Output_section* os = this->relobj_->output_section(*p);
1297 unsigned int output_shndx;
1299 output_shndx = os->out_shndx();
1302 this->relobj_->error(_("section group retained but "
1303 "group element discarded"));
1307 elfcpp::Swap<32, big_endian>::writeval(contents, output_shndx);
1310 size_t wrote = reinterpret_cast<unsigned char*>(contents) - oview;
1311 gold_assert(wrote == oview_size);
1313 of->write_output_view(off, oview_size, oview);
1315 // We no longer need this information.
1316 this->input_shndxes_.clear();
1319 // Output_data_got::Got_entry methods.
1321 // Write out the entry.
1323 template<int size, bool big_endian>
1325 Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
1329 switch (this->local_sym_index_)
1333 // If the symbol is resolved locally, we need to write out the
1334 // link-time value, which will be relocated dynamically by a
1335 // RELATIVE relocation.
1336 Symbol* gsym = this->u_.gsym;
1337 if (this->use_plt_offset_ && gsym->has_plt_offset())
1338 val = (parameters->target().plt_section_for_global(gsym)->address()
1339 + gsym->plt_offset());
1342 Sized_symbol<size>* sgsym;
1343 // This cast is a bit ugly. We don't want to put a
1344 // virtual method in Symbol, because we want Symbol to be
1345 // as small as possible.
1346 sgsym = static_cast<Sized_symbol<size>*>(gsym);
1347 val = sgsym->value();
1353 val = this->u_.constant;
1358 const Sized_relobj<size, big_endian>* object = this->u_.object;
1359 const unsigned int lsi = this->local_sym_index_;
1360 const Symbol_value<size>* symval = object->local_symbol(lsi);
1361 if (!this->use_plt_offset_)
1362 val = symval->value(this->u_.object, 0);
1365 const Output_data* plt =
1366 parameters->target().plt_section_for_local(object, lsi);
1367 val = plt->address() + object->local_plt_offset(lsi);
1373 elfcpp::Swap<size, big_endian>::writeval(pov, val);
1376 // Output_data_got methods.
1378 // Add an entry for a global symbol to the GOT. This returns true if
1379 // this is a new GOT entry, false if the symbol already had a GOT
1382 template<int size, bool big_endian>
1384 Output_data_got<size, big_endian>::add_global(
1386 unsigned int got_type)
1388 if (gsym->has_got_offset(got_type))
1391 this->entries_.push_back(Got_entry(gsym, false));
1392 this->set_got_size();
1393 gsym->set_got_offset(got_type, this->last_got_offset());
1397 // Like add_global, but use the PLT offset.
1399 template<int size, bool big_endian>
1401 Output_data_got<size, big_endian>::add_global_plt(Symbol* gsym,
1402 unsigned int got_type)
1404 if (gsym->has_got_offset(got_type))
1407 this->entries_.push_back(Got_entry(gsym, true));
1408 this->set_got_size();
1409 gsym->set_got_offset(got_type, this->last_got_offset());
1413 // Add an entry for a global symbol to the GOT, and add a dynamic
1414 // relocation of type R_TYPE for the GOT entry.
1416 template<int size, bool big_endian>
1418 Output_data_got<size, big_endian>::add_global_with_rel(
1420 unsigned int got_type,
1422 unsigned int r_type)
1424 if (gsym->has_got_offset(got_type))
1427 this->entries_.push_back(Got_entry());
1428 this->set_got_size();
1429 unsigned int got_offset = this->last_got_offset();
1430 gsym->set_got_offset(got_type, got_offset);
1431 rel_dyn->add_global(gsym, r_type, this, got_offset);
1434 template<int size, bool big_endian>
1436 Output_data_got<size, big_endian>::add_global_with_rela(
1438 unsigned int got_type,
1440 unsigned int r_type)
1442 if (gsym->has_got_offset(got_type))
1445 this->entries_.push_back(Got_entry());
1446 this->set_got_size();
1447 unsigned int got_offset = this->last_got_offset();
1448 gsym->set_got_offset(got_type, got_offset);
1449 rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
1452 // Add a pair of entries for a global symbol to the GOT, and add
1453 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1454 // If R_TYPE_2 == 0, add the second entry with no relocation.
1455 template<int size, bool big_endian>
1457 Output_data_got<size, big_endian>::add_global_pair_with_rel(
1459 unsigned int got_type,
1461 unsigned int r_type_1,
1462 unsigned int r_type_2)
1464 if (gsym->has_got_offset(got_type))
1467 this->entries_.push_back(Got_entry());
1468 unsigned int got_offset = this->last_got_offset();
1469 gsym->set_got_offset(got_type, got_offset);
1470 rel_dyn->add_global(gsym, r_type_1, this, got_offset);
1472 this->entries_.push_back(Got_entry());
1475 got_offset = this->last_got_offset();
1476 rel_dyn->add_global(gsym, r_type_2, this, got_offset);
1479 this->set_got_size();
1482 template<int size, bool big_endian>
1484 Output_data_got<size, big_endian>::add_global_pair_with_rela(
1486 unsigned int got_type,
1488 unsigned int r_type_1,
1489 unsigned int r_type_2)
1491 if (gsym->has_got_offset(got_type))
1494 this->entries_.push_back(Got_entry());
1495 unsigned int got_offset = this->last_got_offset();
1496 gsym->set_got_offset(got_type, got_offset);
1497 rela_dyn->add_global(gsym, r_type_1, this, got_offset, 0);
1499 this->entries_.push_back(Got_entry());
1502 got_offset = this->last_got_offset();
1503 rela_dyn->add_global(gsym, r_type_2, this, got_offset, 0);
1506 this->set_got_size();
1509 // Add an entry for a local symbol to the GOT. This returns true if
1510 // this is a new GOT entry, false if the symbol already has a GOT
1513 template<int size, bool big_endian>
1515 Output_data_got<size, big_endian>::add_local(
1516 Sized_relobj<size, big_endian>* object,
1517 unsigned int symndx,
1518 unsigned int got_type)
1520 if (object->local_has_got_offset(symndx, got_type))
1523 this->entries_.push_back(Got_entry(object, symndx, false));
1524 this->set_got_size();
1525 object->set_local_got_offset(symndx, got_type, this->last_got_offset());
1529 // Like add_local, but use the PLT offset.
1531 template<int size, bool big_endian>
1533 Output_data_got<size, big_endian>::add_local_plt(
1534 Sized_relobj<size, big_endian>* object,
1535 unsigned int symndx,
1536 unsigned int got_type)
1538 if (object->local_has_got_offset(symndx, got_type))
1541 this->entries_.push_back(Got_entry(object, symndx, true));
1542 this->set_got_size();
1543 object->set_local_got_offset(symndx, got_type, this->last_got_offset());
1547 // Add an entry for a local symbol to the GOT, and add a dynamic
1548 // relocation of type R_TYPE for the GOT entry.
1550 template<int size, bool big_endian>
1552 Output_data_got<size, big_endian>::add_local_with_rel(
1553 Sized_relobj<size, big_endian>* object,
1554 unsigned int symndx,
1555 unsigned int got_type,
1557 unsigned int r_type)
1559 if (object->local_has_got_offset(symndx, got_type))
1562 this->entries_.push_back(Got_entry());
1563 this->set_got_size();
1564 unsigned int got_offset = this->last_got_offset();
1565 object->set_local_got_offset(symndx, got_type, got_offset);
1566 rel_dyn->add_local(object, symndx, r_type, this, got_offset);
1569 template<int size, bool big_endian>
1571 Output_data_got<size, big_endian>::add_local_with_rela(
1572 Sized_relobj<size, big_endian>* object,
1573 unsigned int symndx,
1574 unsigned int got_type,
1576 unsigned int r_type)
1578 if (object->local_has_got_offset(symndx, got_type))
1581 this->entries_.push_back(Got_entry());
1582 this->set_got_size();
1583 unsigned int got_offset = this->last_got_offset();
1584 object->set_local_got_offset(symndx, got_type, got_offset);
1585 rela_dyn->add_local(object, symndx, r_type, this, got_offset, 0);
1588 // Add a pair of entries for a local symbol to the GOT, and add
1589 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1590 // If R_TYPE_2 == 0, add the second entry with no relocation.
1591 template<int size, bool big_endian>
1593 Output_data_got<size, big_endian>::add_local_pair_with_rel(
1594 Sized_relobj<size, big_endian>* object,
1595 unsigned int symndx,
1597 unsigned int got_type,
1599 unsigned int r_type_1,
1600 unsigned int r_type_2)
1602 if (object->local_has_got_offset(symndx, got_type))
1605 this->entries_.push_back(Got_entry());
1606 unsigned int got_offset = this->last_got_offset();
1607 object->set_local_got_offset(symndx, got_type, got_offset);
1608 Output_section* os = object->output_section(shndx);
1609 rel_dyn->add_output_section(os, r_type_1, this, got_offset);
1611 this->entries_.push_back(Got_entry(object, symndx, false));
1614 got_offset = this->last_got_offset();
1615 rel_dyn->add_output_section(os, r_type_2, this, got_offset);
1618 this->set_got_size();
1621 template<int size, bool big_endian>
1623 Output_data_got<size, big_endian>::add_local_pair_with_rela(
1624 Sized_relobj<size, big_endian>* object,
1625 unsigned int symndx,
1627 unsigned int got_type,
1629 unsigned int r_type_1,
1630 unsigned int r_type_2)
1632 if (object->local_has_got_offset(symndx, got_type))
1635 this->entries_.push_back(Got_entry());
1636 unsigned int got_offset = this->last_got_offset();
1637 object->set_local_got_offset(symndx, got_type, got_offset);
1638 Output_section* os = object->output_section(shndx);
1639 rela_dyn->add_output_section(os, r_type_1, this, got_offset, 0);
1641 this->entries_.push_back(Got_entry(object, symndx, false));
1644 got_offset = this->last_got_offset();
1645 rela_dyn->add_output_section(os, r_type_2, this, got_offset, 0);
1648 this->set_got_size();
1651 // Write out the GOT.
1653 template<int size, bool big_endian>
1655 Output_data_got<size, big_endian>::do_write(Output_file* of)
1657 const int add = size / 8;
1659 const off_t off = this->offset();
1660 const off_t oview_size = this->data_size();
1661 unsigned char* const oview = of->get_output_view(off, oview_size);
1663 unsigned char* pov = oview;
1664 for (typename Got_entries::const_iterator p = this->entries_.begin();
1665 p != this->entries_.end();
1672 gold_assert(pov - oview == oview_size);
1674 of->write_output_view(off, oview_size, oview);
1676 // We no longer need the GOT entries.
1677 this->entries_.clear();
1680 // Output_data_dynamic::Dynamic_entry methods.
1682 // Write out the entry.
1684 template<int size, bool big_endian>
1686 Output_data_dynamic::Dynamic_entry::write(
1688 const Stringpool* pool) const
1690 typename elfcpp::Elf_types<size>::Elf_WXword val;
1691 switch (this->offset_)
1693 case DYNAMIC_NUMBER:
1697 case DYNAMIC_SECTION_SIZE:
1698 val = this->u_.od->data_size();
1699 if (this->od2 != NULL)
1700 val += this->od2->data_size();
1703 case DYNAMIC_SYMBOL:
1705 const Sized_symbol<size>* s =
1706 static_cast<const Sized_symbol<size>*>(this->u_.sym);
1711 case DYNAMIC_STRING:
1712 val = pool->get_offset(this->u_.str);
1716 val = this->u_.od->address() + this->offset_;
1720 elfcpp::Dyn_write<size, big_endian> dw(pov);
1721 dw.put_d_tag(this->tag_);
1725 // Output_data_dynamic methods.
1727 // Adjust the output section to set the entry size.
1730 Output_data_dynamic::do_adjust_output_section(Output_section* os)
1732 if (parameters->target().get_size() == 32)
1733 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
1734 else if (parameters->target().get_size() == 64)
1735 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1740 // Set the final data size.
1743 Output_data_dynamic::set_final_data_size()
1745 // Add the terminating entry if it hasn't been added.
1746 // Because of relaxation, we can run this multiple times.
1747 if (this->entries_.empty() || this->entries_.back().tag() != elfcpp::DT_NULL)
1749 int extra = parameters->options().spare_dynamic_tags();
1750 for (int i = 0; i < extra; ++i)
1751 this->add_constant(elfcpp::DT_NULL, 0);
1752 this->add_constant(elfcpp::DT_NULL, 0);
1756 if (parameters->target().get_size() == 32)
1757 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
1758 else if (parameters->target().get_size() == 64)
1759 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1762 this->set_data_size(this->entries_.size() * dyn_size);
1765 // Write out the dynamic entries.
1768 Output_data_dynamic::do_write(Output_file* of)
1770 switch (parameters->size_and_endianness())
1772 #ifdef HAVE_TARGET_32_LITTLE
1773 case Parameters::TARGET_32_LITTLE:
1774 this->sized_write<32, false>(of);
1777 #ifdef HAVE_TARGET_32_BIG
1778 case Parameters::TARGET_32_BIG:
1779 this->sized_write<32, true>(of);
1782 #ifdef HAVE_TARGET_64_LITTLE
1783 case Parameters::TARGET_64_LITTLE:
1784 this->sized_write<64, false>(of);
1787 #ifdef HAVE_TARGET_64_BIG
1788 case Parameters::TARGET_64_BIG:
1789 this->sized_write<64, true>(of);
1797 template<int size, bool big_endian>
1799 Output_data_dynamic::sized_write(Output_file* of)
1801 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1803 const off_t offset = this->offset();
1804 const off_t oview_size = this->data_size();
1805 unsigned char* const oview = of->get_output_view(offset, oview_size);
1807 unsigned char* pov = oview;
1808 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1809 p != this->entries_.end();
1812 p->write<size, big_endian>(pov, this->pool_);
1816 gold_assert(pov - oview == oview_size);
1818 of->write_output_view(offset, oview_size, oview);
1820 // We no longer need the dynamic entries.
1821 this->entries_.clear();
1824 // Class Output_symtab_xindex.
1827 Output_symtab_xindex::do_write(Output_file* of)
1829 const off_t offset = this->offset();
1830 const off_t oview_size = this->data_size();
1831 unsigned char* const oview = of->get_output_view(offset, oview_size);
1833 memset(oview, 0, oview_size);
1835 if (parameters->target().is_big_endian())
1836 this->endian_do_write<true>(oview);
1838 this->endian_do_write<false>(oview);
1840 of->write_output_view(offset, oview_size, oview);
1842 // We no longer need the data.
1843 this->entries_.clear();
1846 template<bool big_endian>
1848 Output_symtab_xindex::endian_do_write(unsigned char* const oview)
1850 for (Xindex_entries::const_iterator p = this->entries_.begin();
1851 p != this->entries_.end();
1854 unsigned int symndx = p->first;
1855 gold_assert(symndx * 4 < this->data_size());
1856 elfcpp::Swap<32, big_endian>::writeval(oview + symndx * 4, p->second);
1860 // Output_section::Input_section methods.
1862 // Return the current data size. For an input section we store the size here.
1863 // For an Output_section_data, we have to ask it for the size.
1866 Output_section::Input_section::current_data_size() const
1868 if (this->is_input_section())
1869 return this->u1_.data_size;
1872 this->u2_.posd->pre_finalize_data_size();
1873 return this->u2_.posd->current_data_size();
1877 // Return the data size. For an input section we store the size here.
1878 // For an Output_section_data, we have to ask it for the size.
1881 Output_section::Input_section::data_size() const
1883 if (this->is_input_section())
1884 return this->u1_.data_size;
1886 return this->u2_.posd->data_size();
1889 // Return the object for an input section.
1892 Output_section::Input_section::relobj() const
1894 if (this->is_input_section())
1895 return this->u2_.object;
1896 else if (this->is_merge_section())
1898 gold_assert(this->u2_.pomb->first_relobj() != NULL);
1899 return this->u2_.pomb->first_relobj();
1901 else if (this->is_relaxed_input_section())
1902 return this->u2_.poris->relobj();
1907 // Return the input section index for an input section.
1910 Output_section::Input_section::shndx() const
1912 if (this->is_input_section())
1913 return this->shndx_;
1914 else if (this->is_merge_section())
1916 gold_assert(this->u2_.pomb->first_relobj() != NULL);
1917 return this->u2_.pomb->first_shndx();
1919 else if (this->is_relaxed_input_section())
1920 return this->u2_.poris->shndx();
1925 // Set the address and file offset.
1928 Output_section::Input_section::set_address_and_file_offset(
1931 off_t section_file_offset)
1933 if (this->is_input_section())
1934 this->u2_.object->set_section_offset(this->shndx_,
1935 file_offset - section_file_offset);
1937 this->u2_.posd->set_address_and_file_offset(address, file_offset);
1940 // Reset the address and file offset.
1943 Output_section::Input_section::reset_address_and_file_offset()
1945 if (!this->is_input_section())
1946 this->u2_.posd->reset_address_and_file_offset();
1949 // Finalize the data size.
1952 Output_section::Input_section::finalize_data_size()
1954 if (!this->is_input_section())
1955 this->u2_.posd->finalize_data_size();
1958 // Try to turn an input offset into an output offset. We want to
1959 // return the output offset relative to the start of this
1960 // Input_section in the output section.
1963 Output_section::Input_section::output_offset(
1964 const Relobj* object,
1966 section_offset_type offset,
1967 section_offset_type* poutput) const
1969 if (!this->is_input_section())
1970 return this->u2_.posd->output_offset(object, shndx, offset, poutput);
1973 if (this->shndx_ != shndx || this->u2_.object != object)
1980 // Return whether this is the merge section for the input section
1984 Output_section::Input_section::is_merge_section_for(const Relobj* object,
1985 unsigned int shndx) const
1987 if (this->is_input_section())
1989 return this->u2_.posd->is_merge_section_for(object, shndx);
1992 // Write out the data. We don't have to do anything for an input
1993 // section--they are handled via Object::relocate--but this is where
1994 // we write out the data for an Output_section_data.
1997 Output_section::Input_section::write(Output_file* of)
1999 if (!this->is_input_section())
2000 this->u2_.posd->write(of);
2003 // Write the data to a buffer. As for write(), we don't have to do
2004 // anything for an input section.
2007 Output_section::Input_section::write_to_buffer(unsigned char* buffer)
2009 if (!this->is_input_section())
2010 this->u2_.posd->write_to_buffer(buffer);
2013 // Print to a map file.
2016 Output_section::Input_section::print_to_mapfile(Mapfile* mapfile) const
2018 switch (this->shndx_)
2020 case OUTPUT_SECTION_CODE:
2021 case MERGE_DATA_SECTION_CODE:
2022 case MERGE_STRING_SECTION_CODE:
2023 this->u2_.posd->print_to_mapfile(mapfile);
2026 case RELAXED_INPUT_SECTION_CODE:
2028 Output_relaxed_input_section* relaxed_section =
2029 this->relaxed_input_section();
2030 mapfile->print_input_section(relaxed_section->relobj(),
2031 relaxed_section->shndx());
2035 mapfile->print_input_section(this->u2_.object, this->shndx_);
2040 // Output_section methods.
2042 // Construct an Output_section. NAME will point into a Stringpool.
2044 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
2045 elfcpp::Elf_Xword flags)
2050 link_section_(NULL),
2052 info_section_(NULL),
2057 order_(ORDER_INVALID),
2062 first_input_offset_(0),
2064 postprocessing_buffer_(NULL),
2065 needs_symtab_index_(false),
2066 needs_dynsym_index_(false),
2067 should_link_to_symtab_(false),
2068 should_link_to_dynsym_(false),
2069 after_input_sections_(false),
2070 requires_postprocessing_(false),
2071 found_in_sections_clause_(false),
2072 has_load_address_(false),
2073 info_uses_section_index_(false),
2074 input_section_order_specified_(false),
2075 may_sort_attached_input_sections_(false),
2076 must_sort_attached_input_sections_(false),
2077 attached_input_sections_are_sorted_(false),
2079 is_small_section_(false),
2080 is_large_section_(false),
2081 generate_code_fills_at_write_(false),
2082 is_entsize_zero_(false),
2083 section_offsets_need_adjustment_(false),
2085 always_keeps_input_sections_(false),
2086 has_fixed_layout_(false),
2089 lookup_maps_(new Output_section_lookup_maps),
2092 // An unallocated section has no address. Forcing this means that
2093 // we don't need special treatment for symbols defined in debug
2095 if ((flags & elfcpp::SHF_ALLOC) == 0)
2096 this->set_address(0);
2099 Output_section::~Output_section()
2101 delete this->checkpoint_;
2104 // Set the entry size.
2107 Output_section::set_entsize(uint64_t v)
2109 if (this->is_entsize_zero_)
2111 else if (this->entsize_ == 0)
2113 else if (this->entsize_ != v)
2116 this->is_entsize_zero_ = 1;
2120 // Add the input section SHNDX, with header SHDR, named SECNAME, in
2121 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
2122 // relocation section which applies to this section, or 0 if none, or
2123 // -1U if more than one. Return the offset of the input section
2124 // within the output section. Return -1 if the input section will
2125 // receive special handling. In the normal case we don't always keep
2126 // track of input sections for an Output_section. Instead, each
2127 // Object keeps track of the Output_section for each of its input
2128 // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
2129 // track of input sections here; this is used when SECTIONS appears in
2132 template<int size, bool big_endian>
2134 Output_section::add_input_section(Layout* layout,
2135 Sized_relobj<size, big_endian>* object,
2137 const char* secname,
2138 const elfcpp::Shdr<size, big_endian>& shdr,
2139 unsigned int reloc_shndx,
2140 bool have_sections_script)
2142 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
2143 if ((addralign & (addralign - 1)) != 0)
2145 object->error(_("invalid alignment %lu for section \"%s\""),
2146 static_cast<unsigned long>(addralign), secname);
2150 if (addralign > this->addralign_)
2151 this->addralign_ = addralign;
2153 typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
2154 uint64_t entsize = shdr.get_sh_entsize();
2156 // .debug_str is a mergeable string section, but is not always so
2157 // marked by compilers. Mark manually here so we can optimize.
2158 if (strcmp(secname, ".debug_str") == 0)
2160 sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
2164 this->update_flags_for_input_section(sh_flags);
2165 this->set_entsize(entsize);
2167 // If this is a SHF_MERGE section, we pass all the input sections to
2168 // a Output_data_merge. We don't try to handle relocations for such
2169 // a section. We don't try to handle empty merge sections--they
2170 // mess up the mappings, and are useless anyhow.
2171 // FIXME: Need to handle merge sections during incremental update.
2172 if ((sh_flags & elfcpp::SHF_MERGE) != 0
2174 && shdr.get_sh_size() > 0
2175 && !parameters->incremental())
2177 // Keep information about merged input sections for rebuilding fast
2178 // lookup maps if we have sections-script or we do relaxation.
2179 bool keeps_input_sections = (this->always_keeps_input_sections_
2180 || have_sections_script
2181 || parameters->target().may_relax());
2183 if (this->add_merge_input_section(object, shndx, sh_flags, entsize,
2184 addralign, keeps_input_sections))
2186 // Tell the relocation routines that they need to call the
2187 // output_offset method to determine the final address.
2192 section_size_type input_section_size = shdr.get_sh_size();
2193 section_size_type uncompressed_size;
2194 if (object->section_is_compressed(shndx, &uncompressed_size))
2195 input_section_size = uncompressed_size;
2197 off_t offset_in_section;
2198 off_t aligned_offset_in_section;
2199 if (this->has_fixed_layout())
2201 // For incremental updates, find a chunk of unused space in the section.
2202 offset_in_section = this->free_list_.allocate(input_section_size,
2204 if (offset_in_section == -1)
2205 gold_fatal(_("out of patch space; relink with --incremental-full"));
2206 aligned_offset_in_section = offset_in_section;
2210 offset_in_section = this->current_data_size_for_child();
2211 aligned_offset_in_section = align_address(offset_in_section,
2213 this->set_current_data_size_for_child(aligned_offset_in_section
2214 + input_section_size);
2217 // Determine if we want to delay code-fill generation until the output
2218 // section is written. When the target is relaxing, we want to delay fill
2219 // generating to avoid adjusting them during relaxation. Also, if we are
2220 // sorting input sections we must delay fill generation.
2221 if (!this->generate_code_fills_at_write_
2222 && !have_sections_script
2223 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
2224 && parameters->target().has_code_fill()
2225 && (parameters->target().may_relax()
2226 || parameters->options().section_ordering_file()))
2228 gold_assert(this->fills_.empty());
2229 this->generate_code_fills_at_write_ = true;
2232 if (aligned_offset_in_section > offset_in_section
2233 && !this->generate_code_fills_at_write_
2234 && !have_sections_script
2235 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
2236 && parameters->target().has_code_fill())
2238 // We need to add some fill data. Using fill_list_ when
2239 // possible is an optimization, since we will often have fill
2240 // sections without input sections.
2241 off_t fill_len = aligned_offset_in_section - offset_in_section;
2242 if (this->input_sections_.empty())
2243 this->fills_.push_back(Fill(offset_in_section, fill_len));
2246 std::string fill_data(parameters->target().code_fill(fill_len));
2247 Output_data_const* odc = new Output_data_const(fill_data, 1);
2248 this->input_sections_.push_back(Input_section(odc));
2252 // We need to keep track of this section if we are already keeping
2253 // track of sections, or if we are relaxing. Also, if this is a
2254 // section which requires sorting, or which may require sorting in
2255 // the future, we keep track of the sections. If the
2256 // --section-ordering-file option is used to specify the order of
2257 // sections, we need to keep track of sections.
2258 if (this->always_keeps_input_sections_
2259 || have_sections_script
2260 || !this->input_sections_.empty()
2261 || this->may_sort_attached_input_sections()
2262 || this->must_sort_attached_input_sections()
2263 || parameters->options().user_set_Map()
2264 || parameters->target().may_relax()
2265 || parameters->options().section_ordering_file())
2267 Input_section isecn(object, shndx, input_section_size, addralign);
2268 if (parameters->options().section_ordering_file())
2270 unsigned int section_order_index =
2271 layout->find_section_order_index(std::string(secname));
2272 if (section_order_index != 0)
2274 isecn.set_section_order_index(section_order_index);
2275 this->set_input_section_order_specified();
2278 if (this->has_fixed_layout())
2280 // For incremental updates, finalize the address and offset now.
2281 uint64_t addr = this->address();
2282 isecn.set_address_and_file_offset(addr + aligned_offset_in_section,
2283 aligned_offset_in_section,
2286 this->input_sections_.push_back(isecn);
2289 return aligned_offset_in_section;
2292 // Add arbitrary data to an output section.
2295 Output_section::add_output_section_data(Output_section_data* posd)
2297 Input_section inp(posd);
2298 this->add_output_section_data(&inp);
2300 if (posd->is_data_size_valid())
2302 off_t offset_in_section;
2303 if (this->has_fixed_layout())
2305 // For incremental updates, find a chunk of unused space.
2306 offset_in_section = this->free_list_.allocate(posd->data_size(),
2307 posd->addralign(), 0);
2308 if (offset_in_section == -1)
2309 gold_fatal(_("out of patch space; relink with --incremental-full"));
2310 // Finalize the address and offset now.
2311 uint64_t addr = this->address();
2312 off_t offset = this->offset();
2313 posd->set_address_and_file_offset(addr + offset_in_section,
2314 offset + offset_in_section);
2318 offset_in_section = this->current_data_size_for_child();
2319 off_t aligned_offset_in_section = align_address(offset_in_section,
2321 this->set_current_data_size_for_child(aligned_offset_in_section
2322 + posd->data_size());
2325 else if (this->has_fixed_layout())
2327 // For incremental updates, arrange for the data to have a fixed layout.
2328 // This will mean that additions to the data must be allocated from
2329 // free space within the containing output section.
2330 uint64_t addr = this->address();
2331 posd->set_address(addr);
2332 posd->set_file_offset(0);
2333 // FIXME: Mark *POSD as part of a fixed-layout section.
2337 // Add a relaxed input section.
2340 Output_section::add_relaxed_input_section(Layout* layout,
2341 Output_relaxed_input_section* poris,
2342 const std::string& name)
2344 Input_section inp(poris);
2346 // If the --section-ordering-file option is used to specify the order of
2347 // sections, we need to keep track of sections.
2348 if (parameters->options().section_ordering_file())
2350 unsigned int section_order_index =
2351 layout->find_section_order_index(name);
2352 if (section_order_index != 0)
2354 inp.set_section_order_index(section_order_index);
2355 this->set_input_section_order_specified();
2359 this->add_output_section_data(&inp);
2360 if (this->lookup_maps_->is_valid())
2361 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2362 poris->shndx(), poris);
2364 // For a relaxed section, we use the current data size. Linker scripts
2365 // get all the input sections, including relaxed one from an output
2366 // section and add them back to them same output section to compute the
2367 // output section size. If we do not account for sizes of relaxed input
2368 // sections, an output section would be incorrectly sized.
2369 off_t offset_in_section = this->current_data_size_for_child();
2370 off_t aligned_offset_in_section = align_address(offset_in_section,
2371 poris->addralign());
2372 this->set_current_data_size_for_child(aligned_offset_in_section
2373 + poris->current_data_size());
2376 // Add arbitrary data to an output section by Input_section.
2379 Output_section::add_output_section_data(Input_section* inp)
2381 if (this->input_sections_.empty())
2382 this->first_input_offset_ = this->current_data_size_for_child();
2384 this->input_sections_.push_back(*inp);
2386 uint64_t addralign = inp->addralign();
2387 if (addralign > this->addralign_)
2388 this->addralign_ = addralign;
2390 inp->set_output_section(this);
2393 // Add a merge section to an output section.
2396 Output_section::add_output_merge_section(Output_section_data* posd,
2397 bool is_string, uint64_t entsize)
2399 Input_section inp(posd, is_string, entsize);
2400 this->add_output_section_data(&inp);
2403 // Add an input section to a SHF_MERGE section.
2406 Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
2407 uint64_t flags, uint64_t entsize,
2409 bool keeps_input_sections)
2411 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
2413 // We only merge strings if the alignment is not more than the
2414 // character size. This could be handled, but it's unusual.
2415 if (is_string && addralign > entsize)
2418 // We cannot restore merged input section states.
2419 gold_assert(this->checkpoint_ == NULL);
2421 // Look up merge sections by required properties.
2422 // Currently, we only invalidate the lookup maps in script processing
2423 // and relaxation. We should not have done either when we reach here.
2424 // So we assume that the lookup maps are valid to simply code.
2425 gold_assert(this->lookup_maps_->is_valid());
2426 Merge_section_properties msp(is_string, entsize, addralign);
2427 Output_merge_base* pomb = this->lookup_maps_->find_merge_section(msp);
2428 bool is_new = false;
2431 gold_assert(pomb->is_string() == is_string
2432 && pomb->entsize() == entsize
2433 && pomb->addralign() == addralign);
2437 // Create a new Output_merge_data or Output_merge_string_data.
2439 pomb = new Output_merge_data(entsize, addralign);
2445 pomb = new Output_merge_string<char>(addralign);
2448 pomb = new Output_merge_string<uint16_t>(addralign);
2451 pomb = new Output_merge_string<uint32_t>(addralign);
2457 // If we need to do script processing or relaxation, we need to keep
2458 // the original input sections to rebuild the fast lookup maps.
2459 if (keeps_input_sections)
2460 pomb->set_keeps_input_sections();
2464 if (pomb->add_input_section(object, shndx))
2466 // Add new merge section to this output section and link merge
2467 // section properties to new merge section in map.
2470 this->add_output_merge_section(pomb, is_string, entsize);
2471 this->lookup_maps_->add_merge_section(msp, pomb);
2474 // Add input section to new merge section and link input section to new
2475 // merge section in map.
2476 this->lookup_maps_->add_merge_input_section(object, shndx, pomb);
2481 // If add_input_section failed, delete new merge section to avoid
2482 // exporting empty merge sections in Output_section::get_input_section.
2489 // Build a relaxation map to speed up relaxation of existing input sections.
2490 // Look up to the first LIMIT elements in INPUT_SECTIONS.
2493 Output_section::build_relaxation_map(
2494 const Input_section_list& input_sections,
2496 Relaxation_map* relaxation_map) const
2498 for (size_t i = 0; i < limit; ++i)
2500 const Input_section& is(input_sections[i]);
2501 if (is.is_input_section() || is.is_relaxed_input_section())
2503 Section_id sid(is.relobj(), is.shndx());
2504 (*relaxation_map)[sid] = i;
2509 // Convert regular input sections in INPUT_SECTIONS into relaxed input
2510 // sections in RELAXED_SECTIONS. MAP is a prebuilt map from section id
2511 // indices of INPUT_SECTIONS.
2514 Output_section::convert_input_sections_in_list_to_relaxed_sections(
2515 const std::vector<Output_relaxed_input_section*>& relaxed_sections,
2516 const Relaxation_map& map,
2517 Input_section_list* input_sections)
2519 for (size_t i = 0; i < relaxed_sections.size(); ++i)
2521 Output_relaxed_input_section* poris = relaxed_sections[i];
2522 Section_id sid(poris->relobj(), poris->shndx());
2523 Relaxation_map::const_iterator p = map.find(sid);
2524 gold_assert(p != map.end());
2525 gold_assert((*input_sections)[p->second].is_input_section());
2527 // Remember section order index of original input section
2528 // if it is set. Copy it to the relaxed input section.
2530 (*input_sections)[p->second].section_order_index();
2531 (*input_sections)[p->second] = Input_section(poris);
2532 (*input_sections)[p->second].set_section_order_index(soi);
2536 // Convert regular input sections into relaxed input sections. RELAXED_SECTIONS
2537 // is a vector of pointers to Output_relaxed_input_section or its derived
2538 // classes. The relaxed sections must correspond to existing input sections.
2541 Output_section::convert_input_sections_to_relaxed_sections(
2542 const std::vector<Output_relaxed_input_section*>& relaxed_sections)
2544 gold_assert(parameters->target().may_relax());
2546 // We want to make sure that restore_states does not undo the effect of
2547 // this. If there is no checkpoint active, just search the current
2548 // input section list and replace the sections there. If there is
2549 // a checkpoint, also replace the sections there.
2551 // By default, we look at the whole list.
2552 size_t limit = this->input_sections_.size();
2554 if (this->checkpoint_ != NULL)
2556 // Replace input sections with relaxed input section in the saved
2557 // copy of the input section list.
2558 if (this->checkpoint_->input_sections_saved())
2561 this->build_relaxation_map(
2562 *(this->checkpoint_->input_sections()),
2563 this->checkpoint_->input_sections()->size(),
2565 this->convert_input_sections_in_list_to_relaxed_sections(
2568 this->checkpoint_->input_sections());
2572 // We have not copied the input section list yet. Instead, just
2573 // look at the portion that would be saved.
2574 limit = this->checkpoint_->input_sections_size();
2578 // Convert input sections in input_section_list.
2580 this->build_relaxation_map(this->input_sections_, limit, &map);
2581 this->convert_input_sections_in_list_to_relaxed_sections(
2584 &this->input_sections_);
2586 // Update fast look-up map.
2587 if (this->lookup_maps_->is_valid())
2588 for (size_t i = 0; i < relaxed_sections.size(); ++i)
2590 Output_relaxed_input_section* poris = relaxed_sections[i];
2591 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2592 poris->shndx(), poris);
2596 // Update the output section flags based on input section flags.
2599 Output_section::update_flags_for_input_section(elfcpp::Elf_Xword flags)
2601 // If we created the section with SHF_ALLOC clear, we set the
2602 // address. If we are now setting the SHF_ALLOC flag, we need to
2604 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0
2605 && (flags & elfcpp::SHF_ALLOC) != 0)
2606 this->mark_address_invalid();
2608 this->flags_ |= (flags
2609 & (elfcpp::SHF_WRITE
2611 | elfcpp::SHF_EXECINSTR));
2613 if ((flags & elfcpp::SHF_MERGE) == 0)
2614 this->flags_ &=~ elfcpp::SHF_MERGE;
2617 if (this->current_data_size_for_child() == 0)
2618 this->flags_ |= elfcpp::SHF_MERGE;
2621 if ((flags & elfcpp::SHF_STRINGS) == 0)
2622 this->flags_ &=~ elfcpp::SHF_STRINGS;
2625 if (this->current_data_size_for_child() == 0)
2626 this->flags_ |= elfcpp::SHF_STRINGS;
2630 // Find the merge section into which an input section with index SHNDX in
2631 // OBJECT has been added. Return NULL if none found.
2633 Output_section_data*
2634 Output_section::find_merge_section(const Relobj* object,
2635 unsigned int shndx) const
2637 if (!this->lookup_maps_->is_valid())
2638 this->build_lookup_maps();
2639 return this->lookup_maps_->find_merge_section(object, shndx);
2642 // Build the lookup maps for merge and relaxed sections. This is needs
2643 // to be declared as a const methods so that it is callable with a const
2644 // Output_section pointer. The method only updates states of the maps.
2647 Output_section::build_lookup_maps() const
2649 this->lookup_maps_->clear();
2650 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2651 p != this->input_sections_.end();
2654 if (p->is_merge_section())
2656 Output_merge_base* pomb = p->output_merge_base();
2657 Merge_section_properties msp(pomb->is_string(), pomb->entsize(),
2659 this->lookup_maps_->add_merge_section(msp, pomb);
2660 for (Output_merge_base::Input_sections::const_iterator is =
2661 pomb->input_sections_begin();
2662 is != pomb->input_sections_end();
2665 const Const_section_id& csid = *is;
2666 this->lookup_maps_->add_merge_input_section(csid.first,
2671 else if (p->is_relaxed_input_section())
2673 Output_relaxed_input_section* poris = p->relaxed_input_section();
2674 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2675 poris->shndx(), poris);
2680 // Find an relaxed input section corresponding to an input section
2681 // in OBJECT with index SHNDX.
2683 const Output_relaxed_input_section*
2684 Output_section::find_relaxed_input_section(const Relobj* object,
2685 unsigned int shndx) const
2687 if (!this->lookup_maps_->is_valid())
2688 this->build_lookup_maps();
2689 return this->lookup_maps_->find_relaxed_input_section(object, shndx);
2692 // Given an address OFFSET relative to the start of input section
2693 // SHNDX in OBJECT, return whether this address is being included in
2694 // the final link. This should only be called if SHNDX in OBJECT has
2695 // a special mapping.
2698 Output_section::is_input_address_mapped(const Relobj* object,
2702 // Look at the Output_section_data_maps first.
2703 const Output_section_data* posd = this->find_merge_section(object, shndx);
2705 posd = this->find_relaxed_input_section(object, shndx);
2709 section_offset_type output_offset;
2710 bool found = posd->output_offset(object, shndx, offset, &output_offset);
2712 return output_offset != -1;
2715 // Fall back to the slow look-up.
2716 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2717 p != this->input_sections_.end();
2720 section_offset_type output_offset;
2721 if (p->output_offset(object, shndx, offset, &output_offset))
2722 return output_offset != -1;
2725 // By default we assume that the address is mapped. This should
2726 // only be called after we have passed all sections to Layout. At
2727 // that point we should know what we are discarding.
2731 // Given an address OFFSET relative to the start of input section
2732 // SHNDX in object OBJECT, return the output offset relative to the
2733 // start of the input section in the output section. This should only
2734 // be called if SHNDX in OBJECT has a special mapping.
2737 Output_section::output_offset(const Relobj* object, unsigned int shndx,
2738 section_offset_type offset) const
2740 // This can only be called meaningfully when we know the data size
2742 gold_assert(this->is_data_size_valid());
2744 // Look at the Output_section_data_maps first.
2745 const Output_section_data* posd = this->find_merge_section(object, shndx);
2747 posd = this->find_relaxed_input_section(object, shndx);
2750 section_offset_type output_offset;
2751 bool found = posd->output_offset(object, shndx, offset, &output_offset);
2753 return output_offset;
2756 // Fall back to the slow look-up.
2757 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2758 p != this->input_sections_.end();
2761 section_offset_type output_offset;
2762 if (p->output_offset(object, shndx, offset, &output_offset))
2763 return output_offset;
2768 // Return the output virtual address of OFFSET relative to the start
2769 // of input section SHNDX in object OBJECT.
2772 Output_section::output_address(const Relobj* object, unsigned int shndx,
2775 uint64_t addr = this->address() + this->first_input_offset_;
2777 // Look at the Output_section_data_maps first.
2778 const Output_section_data* posd = this->find_merge_section(object, shndx);
2780 posd = this->find_relaxed_input_section(object, shndx);
2781 if (posd != NULL && posd->is_address_valid())
2783 section_offset_type output_offset;
2784 bool found = posd->output_offset(object, shndx, offset, &output_offset);
2786 return posd->address() + output_offset;
2789 // Fall back to the slow look-up.
2790 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2791 p != this->input_sections_.end();
2794 addr = align_address(addr, p->addralign());
2795 section_offset_type output_offset;
2796 if (p->output_offset(object, shndx, offset, &output_offset))
2798 if (output_offset == -1)
2800 return addr + output_offset;
2802 addr += p->data_size();
2805 // If we get here, it means that we don't know the mapping for this
2806 // input section. This might happen in principle if
2807 // add_input_section were called before add_output_section_data.
2808 // But it should never actually happen.
2813 // Find the output address of the start of the merged section for
2814 // input section SHNDX in object OBJECT.
2817 Output_section::find_starting_output_address(const Relobj* object,
2819 uint64_t* paddr) const
2821 // FIXME: This becomes a bottle-neck if we have many relaxed sections.
2822 // Looking up the merge section map does not always work as we sometimes
2823 // find a merge section without its address set.
2824 uint64_t addr = this->address() + this->first_input_offset_;
2825 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2826 p != this->input_sections_.end();
2829 addr = align_address(addr, p->addralign());
2831 // It would be nice if we could use the existing output_offset
2832 // method to get the output offset of input offset 0.
2833 // Unfortunately we don't know for sure that input offset 0 is
2835 if (p->is_merge_section_for(object, shndx))
2841 addr += p->data_size();
2844 // We couldn't find a merge output section for this input section.
2848 // Update the data size of an Output_section.
2851 Output_section::update_data_size()
2853 if (this->input_sections_.empty())
2856 if (this->must_sort_attached_input_sections()
2857 || this->input_section_order_specified())
2858 this->sort_attached_input_sections();
2860 off_t off = this->first_input_offset_;
2861 for (Input_section_list::iterator p = this->input_sections_.begin();
2862 p != this->input_sections_.end();
2865 off = align_address(off, p->addralign());
2866 off += p->current_data_size();
2869 this->set_current_data_size_for_child(off);
2872 // Set the data size of an Output_section. This is where we handle
2873 // setting the addresses of any Output_section_data objects.
2876 Output_section::set_final_data_size()
2878 if (this->input_sections_.empty())
2880 this->set_data_size(this->current_data_size_for_child());
2884 if (this->must_sort_attached_input_sections()
2885 || this->input_section_order_specified())
2886 this->sort_attached_input_sections();
2888 uint64_t address = this->address();
2889 off_t startoff = this->offset();
2890 off_t off = startoff + this->first_input_offset_;
2891 for (Input_section_list::iterator p = this->input_sections_.begin();
2892 p != this->input_sections_.end();
2895 off = align_address(off, p->addralign());
2896 p->set_address_and_file_offset(address + (off - startoff), off,
2898 off += p->data_size();
2901 this->set_data_size(off - startoff);
2904 // Reset the address and file offset.
2907 Output_section::do_reset_address_and_file_offset()
2909 // An unallocated section has no address. Forcing this means that
2910 // we don't need special treatment for symbols defined in debug
2911 // sections. We do the same in the constructor. This does not
2912 // apply to NOLOAD sections though.
2913 if (((this->flags_ & elfcpp::SHF_ALLOC) == 0) && !this->is_noload_)
2914 this->set_address(0);
2916 for (Input_section_list::iterator p = this->input_sections_.begin();
2917 p != this->input_sections_.end();
2919 p->reset_address_and_file_offset();
2922 // Return true if address and file offset have the values after reset.
2925 Output_section::do_address_and_file_offset_have_reset_values() const
2927 if (this->is_offset_valid())
2930 // An unallocated section has address 0 after its construction or a reset.
2931 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0)
2932 return this->is_address_valid() && this->address() == 0;
2934 return !this->is_address_valid();
2937 // Set the TLS offset. Called only for SHT_TLS sections.
2940 Output_section::do_set_tls_offset(uint64_t tls_base)
2942 this->tls_offset_ = this->address() - tls_base;
2945 // In a few cases we need to sort the input sections attached to an
2946 // output section. This is used to implement the type of constructor
2947 // priority ordering implemented by the GNU linker, in which the
2948 // priority becomes part of the section name and the sections are
2949 // sorted by name. We only do this for an output section if we see an
2950 // attached input section matching ".ctor.*", ".dtor.*",
2951 // ".init_array.*" or ".fini_array.*".
2953 class Output_section::Input_section_sort_entry
2956 Input_section_sort_entry()
2957 : input_section_(), index_(-1U), section_has_name_(false),
2961 Input_section_sort_entry(const Input_section& input_section,
2963 bool must_sort_attached_input_sections)
2964 : input_section_(input_section), index_(index),
2965 section_has_name_(input_section.is_input_section()
2966 || input_section.is_relaxed_input_section())
2968 if (this->section_has_name_
2969 && must_sort_attached_input_sections)
2971 // This is only called single-threaded from Layout::finalize,
2972 // so it is OK to lock. Unfortunately we have no way to pass
2974 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
2975 Object* obj = (input_section.is_input_section()
2976 ? input_section.relobj()
2977 : input_section.relaxed_input_section()->relobj());
2978 Task_lock_obj<Object> tl(dummy_task, obj);
2980 // This is a slow operation, which should be cached in
2981 // Layout::layout if this becomes a speed problem.
2982 this->section_name_ = obj->section_name(input_section.shndx());
2986 // Return the Input_section.
2987 const Input_section&
2988 input_section() const
2990 gold_assert(this->index_ != -1U);
2991 return this->input_section_;
2994 // The index of this entry in the original list. This is used to
2995 // make the sort stable.
2999 gold_assert(this->index_ != -1U);
3000 return this->index_;
3003 // Whether there is a section name.
3005 section_has_name() const
3006 { return this->section_has_name_; }
3008 // The section name.
3010 section_name() const
3012 gold_assert(this->section_has_name_);
3013 return this->section_name_;
3016 // Return true if the section name has a priority. This is assumed
3017 // to be true if it has a dot after the initial dot.
3019 has_priority() const
3021 gold_assert(this->section_has_name_);
3022 return this->section_name_.find('.', 1) != std::string::npos;
3025 // Return true if this an input file whose base name matches
3026 // FILE_NAME. The base name must have an extension of ".o", and
3027 // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
3028 // This is to match crtbegin.o as well as crtbeginS.o without
3029 // getting confused by other possibilities. Overall matching the
3030 // file name this way is a dreadful hack, but the GNU linker does it
3031 // in order to better support gcc, and we need to be compatible.
3033 match_file_name(const char* match_file_name) const
3035 const std::string& file_name(this->input_section_.relobj()->name());
3036 const char* base_name = lbasename(file_name.c_str());
3037 size_t match_len = strlen(match_file_name);
3038 if (strncmp(base_name, match_file_name, match_len) != 0)
3040 size_t base_len = strlen(base_name);
3041 if (base_len != match_len + 2 && base_len != match_len + 3)
3043 return memcmp(base_name + base_len - 2, ".o", 2) == 0;
3046 // Returns 1 if THIS should appear before S in section order, -1 if S
3047 // appears before THIS and 0 if they are not comparable.
3049 compare_section_ordering(const Input_section_sort_entry& s) const
3051 unsigned int this_secn_index = this->input_section_.section_order_index();
3052 unsigned int s_secn_index = s.input_section().section_order_index();
3053 if (this_secn_index > 0 && s_secn_index > 0)
3055 if (this_secn_index < s_secn_index)
3057 else if (this_secn_index > s_secn_index)
3064 // The Input_section we are sorting.
3065 Input_section input_section_;
3066 // The index of this Input_section in the original list.
3067 unsigned int index_;
3068 // Whether this Input_section has a section name--it won't if this
3069 // is some random Output_section_data.
3070 bool section_has_name_;
3071 // The section name if there is one.
3072 std::string section_name_;
3075 // Return true if S1 should come before S2 in the output section.
3078 Output_section::Input_section_sort_compare::operator()(
3079 const Output_section::Input_section_sort_entry& s1,
3080 const Output_section::Input_section_sort_entry& s2) const
3082 // crtbegin.o must come first.
3083 bool s1_begin = s1.match_file_name("crtbegin");
3084 bool s2_begin = s2.match_file_name("crtbegin");
3085 if (s1_begin || s2_begin)
3091 return s1.index() < s2.index();
3094 // crtend.o must come last.
3095 bool s1_end = s1.match_file_name("crtend");
3096 bool s2_end = s2.match_file_name("crtend");
3097 if (s1_end || s2_end)
3103 return s1.index() < s2.index();
3106 // We sort all the sections with no names to the end.
3107 if (!s1.section_has_name() || !s2.section_has_name())
3109 if (s1.section_has_name())
3111 if (s2.section_has_name())
3113 return s1.index() < s2.index();
3116 // A section with a priority follows a section without a priority.
3117 bool s1_has_priority = s1.has_priority();
3118 bool s2_has_priority = s2.has_priority();
3119 if (s1_has_priority && !s2_has_priority)
3121 if (!s1_has_priority && s2_has_priority)
3124 // Check if a section order exists for these sections through a section
3125 // ordering file. If sequence_num is 0, an order does not exist.
3126 int sequence_num = s1.compare_section_ordering(s2);
3127 if (sequence_num != 0)
3128 return sequence_num == 1;
3130 // Otherwise we sort by name.
3131 int compare = s1.section_name().compare(s2.section_name());
3135 // Otherwise we keep the input order.
3136 return s1.index() < s2.index();
3139 // Return true if S1 should come before S2 in an .init_array or .fini_array
3143 Output_section::Input_section_sort_init_fini_compare::operator()(
3144 const Output_section::Input_section_sort_entry& s1,
3145 const Output_section::Input_section_sort_entry& s2) const
3147 // We sort all the sections with no names to the end.
3148 if (!s1.section_has_name() || !s2.section_has_name())
3150 if (s1.section_has_name())
3152 if (s2.section_has_name())
3154 return s1.index() < s2.index();
3157 // A section without a priority follows a section with a priority.
3158 // This is the reverse of .ctors and .dtors sections.
3159 bool s1_has_priority = s1.has_priority();
3160 bool s2_has_priority = s2.has_priority();
3161 if (s1_has_priority && !s2_has_priority)
3163 if (!s1_has_priority && s2_has_priority)
3166 // Check if a section order exists for these sections through a section
3167 // ordering file. If sequence_num is 0, an order does not exist.
3168 int sequence_num = s1.compare_section_ordering(s2);
3169 if (sequence_num != 0)
3170 return sequence_num == 1;
3172 // Otherwise we sort by name.
3173 int compare = s1.section_name().compare(s2.section_name());
3177 // Otherwise we keep the input order.
3178 return s1.index() < s2.index();
3181 // Return true if S1 should come before S2. Sections that do not match
3182 // any pattern in the section ordering file are placed ahead of the sections
3183 // that match some pattern.
3186 Output_section::Input_section_sort_section_order_index_compare::operator()(
3187 const Output_section::Input_section_sort_entry& s1,
3188 const Output_section::Input_section_sort_entry& s2) const
3190 unsigned int s1_secn_index = s1.input_section().section_order_index();
3191 unsigned int s2_secn_index = s2.input_section().section_order_index();
3193 // Keep input order if section ordering cannot determine order.
3194 if (s1_secn_index == s2_secn_index)
3195 return s1.index() < s2.index();
3197 return s1_secn_index < s2_secn_index;
3200 // Sort the input sections attached to an output section.
3203 Output_section::sort_attached_input_sections()
3205 if (this->attached_input_sections_are_sorted_)
3208 if (this->checkpoint_ != NULL
3209 && !this->checkpoint_->input_sections_saved())
3210 this->checkpoint_->save_input_sections();
3212 // The only thing we know about an input section is the object and
3213 // the section index. We need the section name. Recomputing this
3214 // is slow but this is an unusual case. If this becomes a speed
3215 // problem we can cache the names as required in Layout::layout.
3217 // We start by building a larger vector holding a copy of each
3218 // Input_section, plus its current index in the list and its name.
3219 std::vector<Input_section_sort_entry> sort_list;
3222 for (Input_section_list::iterator p = this->input_sections_.begin();
3223 p != this->input_sections_.end();
3225 sort_list.push_back(Input_section_sort_entry(*p, i,
3226 this->must_sort_attached_input_sections()));
3228 // Sort the input sections.
3229 if (this->must_sort_attached_input_sections())
3231 if (this->type() == elfcpp::SHT_PREINIT_ARRAY
3232 || this->type() == elfcpp::SHT_INIT_ARRAY
3233 || this->type() == elfcpp::SHT_FINI_ARRAY)
3234 std::sort(sort_list.begin(), sort_list.end(),
3235 Input_section_sort_init_fini_compare());
3237 std::sort(sort_list.begin(), sort_list.end(),
3238 Input_section_sort_compare());
3242 gold_assert(parameters->options().section_ordering_file());
3243 std::sort(sort_list.begin(), sort_list.end(),
3244 Input_section_sort_section_order_index_compare());
3247 // Copy the sorted input sections back to our list.
3248 this->input_sections_.clear();
3249 for (std::vector<Input_section_sort_entry>::iterator p = sort_list.begin();
3250 p != sort_list.end();
3252 this->input_sections_.push_back(p->input_section());
3255 // Remember that we sorted the input sections, since we might get
3257 this->attached_input_sections_are_sorted_ = true;
3260 // Write the section header to *OSHDR.
3262 template<int size, bool big_endian>
3264 Output_section::write_header(const Layout* layout,
3265 const Stringpool* secnamepool,
3266 elfcpp::Shdr_write<size, big_endian>* oshdr) const
3268 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
3269 oshdr->put_sh_type(this->type_);
3271 elfcpp::Elf_Xword flags = this->flags_;
3272 if (this->info_section_ != NULL && this->info_uses_section_index_)
3273 flags |= elfcpp::SHF_INFO_LINK;
3274 oshdr->put_sh_flags(flags);
3276 oshdr->put_sh_addr(this->address());
3277 oshdr->put_sh_offset(this->offset());
3278 oshdr->put_sh_size(this->data_size());
3279 if (this->link_section_ != NULL)
3280 oshdr->put_sh_link(this->link_section_->out_shndx());
3281 else if (this->should_link_to_symtab_)
3282 oshdr->put_sh_link(layout->symtab_section()->out_shndx());
3283 else if (this->should_link_to_dynsym_)
3284 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
3286 oshdr->put_sh_link(this->link_);
3288 elfcpp::Elf_Word info;
3289 if (this->info_section_ != NULL)
3291 if (this->info_uses_section_index_)
3292 info = this->info_section_->out_shndx();
3294 info = this->info_section_->symtab_index();
3296 else if (this->info_symndx_ != NULL)
3297 info = this->info_symndx_->symtab_index();
3300 oshdr->put_sh_info(info);
3302 oshdr->put_sh_addralign(this->addralign_);
3303 oshdr->put_sh_entsize(this->entsize_);
3306 // Write out the data. For input sections the data is written out by
3307 // Object::relocate, but we have to handle Output_section_data objects
3311 Output_section::do_write(Output_file* of)
3313 gold_assert(!this->requires_postprocessing());
3315 // If the target performs relaxation, we delay filler generation until now.
3316 gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
3318 off_t output_section_file_offset = this->offset();
3319 for (Fill_list::iterator p = this->fills_.begin();
3320 p != this->fills_.end();
3323 std::string fill_data(parameters->target().code_fill(p->length()));
3324 of->write(output_section_file_offset + p->section_offset(),
3325 fill_data.data(), fill_data.size());
3328 off_t off = this->offset() + this->first_input_offset_;
3329 for (Input_section_list::iterator p = this->input_sections_.begin();
3330 p != this->input_sections_.end();
3333 off_t aligned_off = align_address(off, p->addralign());
3334 if (this->generate_code_fills_at_write_ && (off != aligned_off))
3336 size_t fill_len = aligned_off - off;
3337 std::string fill_data(parameters->target().code_fill(fill_len));
3338 of->write(off, fill_data.data(), fill_data.size());
3342 off = aligned_off + p->data_size();
3346 // If a section requires postprocessing, create the buffer to use.
3349 Output_section::create_postprocessing_buffer()
3351 gold_assert(this->requires_postprocessing());
3353 if (this->postprocessing_buffer_ != NULL)
3356 if (!this->input_sections_.empty())
3358 off_t off = this->first_input_offset_;
3359 for (Input_section_list::iterator p = this->input_sections_.begin();
3360 p != this->input_sections_.end();
3363 off = align_address(off, p->addralign());
3364 p->finalize_data_size();
3365 off += p->data_size();
3367 this->set_current_data_size_for_child(off);
3370 off_t buffer_size = this->current_data_size_for_child();
3371 this->postprocessing_buffer_ = new unsigned char[buffer_size];
3374 // Write all the data of an Output_section into the postprocessing
3375 // buffer. This is used for sections which require postprocessing,
3376 // such as compression. Input sections are handled by
3377 // Object::Relocate.
3380 Output_section::write_to_postprocessing_buffer()
3382 gold_assert(this->requires_postprocessing());
3384 // If the target performs relaxation, we delay filler generation until now.
3385 gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
3387 unsigned char* buffer = this->postprocessing_buffer();
3388 for (Fill_list::iterator p = this->fills_.begin();
3389 p != this->fills_.end();
3392 std::string fill_data(parameters->target().code_fill(p->length()));
3393 memcpy(buffer + p->section_offset(), fill_data.data(),
3397 off_t off = this->first_input_offset_;
3398 for (Input_section_list::iterator p = this->input_sections_.begin();
3399 p != this->input_sections_.end();
3402 off_t aligned_off = align_address(off, p->addralign());
3403 if (this->generate_code_fills_at_write_ && (off != aligned_off))
3405 size_t fill_len = aligned_off - off;
3406 std::string fill_data(parameters->target().code_fill(fill_len));
3407 memcpy(buffer + off, fill_data.data(), fill_data.size());
3410 p->write_to_buffer(buffer + aligned_off);
3411 off = aligned_off + p->data_size();
3415 // Get the input sections for linker script processing. We leave
3416 // behind the Output_section_data entries. Note that this may be
3417 // slightly incorrect for merge sections. We will leave them behind,
3418 // but it is possible that the script says that they should follow
3419 // some other input sections, as in:
3420 // .rodata { *(.rodata) *(.rodata.cst*) }
3421 // For that matter, we don't handle this correctly:
3422 // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
3423 // With luck this will never matter.
3426 Output_section::get_input_sections(
3428 const std::string& fill,
3429 std::list<Input_section>* input_sections)
3431 if (this->checkpoint_ != NULL
3432 && !this->checkpoint_->input_sections_saved())
3433 this->checkpoint_->save_input_sections();
3435 // Invalidate fast look-up maps.
3436 this->lookup_maps_->invalidate();
3438 uint64_t orig_address = address;
3440 address = align_address(address, this->addralign());
3442 Input_section_list remaining;
3443 for (Input_section_list::iterator p = this->input_sections_.begin();
3444 p != this->input_sections_.end();
3447 if (p->is_input_section()
3448 || p->is_relaxed_input_section()
3449 || p->is_merge_section())
3450 input_sections->push_back(*p);
3453 uint64_t aligned_address = align_address(address, p->addralign());
3454 if (aligned_address != address && !fill.empty())
3456 section_size_type length =
3457 convert_to_section_size_type(aligned_address - address);
3458 std::string this_fill;
3459 this_fill.reserve(length);
3460 while (this_fill.length() + fill.length() <= length)
3462 if (this_fill.length() < length)
3463 this_fill.append(fill, 0, length - this_fill.length());
3465 Output_section_data* posd = new Output_data_const(this_fill, 0);
3466 remaining.push_back(Input_section(posd));
3468 address = aligned_address;
3470 remaining.push_back(*p);
3472 p->finalize_data_size();
3473 address += p->data_size();
3477 this->input_sections_.swap(remaining);
3478 this->first_input_offset_ = 0;
3480 uint64_t data_size = address - orig_address;
3481 this->set_current_data_size_for_child(data_size);
3485 // Add a script input section. SIS is an Output_section::Input_section,
3486 // which can be either a plain input section or a special input section like
3487 // a relaxed input section. For a special input section, its size must be
3491 Output_section::add_script_input_section(const Input_section& sis)
3493 uint64_t data_size = sis.data_size();
3494 uint64_t addralign = sis.addralign();
3495 if (addralign > this->addralign_)
3496 this->addralign_ = addralign;
3498 off_t offset_in_section = this->current_data_size_for_child();
3499 off_t aligned_offset_in_section = align_address(offset_in_section,
3502 this->set_current_data_size_for_child(aligned_offset_in_section
3505 this->input_sections_.push_back(sis);
3507 // Update fast lookup maps if necessary.
3508 if (this->lookup_maps_->is_valid())
3510 if (sis.is_merge_section())
3512 Output_merge_base* pomb = sis.output_merge_base();
3513 Merge_section_properties msp(pomb->is_string(), pomb->entsize(),
3515 this->lookup_maps_->add_merge_section(msp, pomb);
3516 for (Output_merge_base::Input_sections::const_iterator p =
3517 pomb->input_sections_begin();
3518 p != pomb->input_sections_end();
3520 this->lookup_maps_->add_merge_input_section(p->first, p->second,
3523 else if (sis.is_relaxed_input_section())
3525 Output_relaxed_input_section* poris = sis.relaxed_input_section();
3526 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
3527 poris->shndx(), poris);
3532 // Save states for relaxation.
3535 Output_section::save_states()
3537 gold_assert(this->checkpoint_ == NULL);
3538 Checkpoint_output_section* checkpoint =
3539 new Checkpoint_output_section(this->addralign_, this->flags_,
3540 this->input_sections_,
3541 this->first_input_offset_,
3542 this->attached_input_sections_are_sorted_);
3543 this->checkpoint_ = checkpoint;
3544 gold_assert(this->fills_.empty());
3548 Output_section::discard_states()
3550 gold_assert(this->checkpoint_ != NULL);
3551 delete this->checkpoint_;
3552 this->checkpoint_ = NULL;
3553 gold_assert(this->fills_.empty());
3555 // Simply invalidate the fast lookup maps since we do not keep
3557 this->lookup_maps_->invalidate();
3561 Output_section::restore_states()
3563 gold_assert(this->checkpoint_ != NULL);
3564 Checkpoint_output_section* checkpoint = this->checkpoint_;
3566 this->addralign_ = checkpoint->addralign();
3567 this->flags_ = checkpoint->flags();
3568 this->first_input_offset_ = checkpoint->first_input_offset();
3570 if (!checkpoint->input_sections_saved())
3572 // If we have not copied the input sections, just resize it.
3573 size_t old_size = checkpoint->input_sections_size();
3574 gold_assert(this->input_sections_.size() >= old_size);
3575 this->input_sections_.resize(old_size);
3579 // We need to copy the whole list. This is not efficient for
3580 // extremely large output with hundreads of thousands of input
3581 // objects. We may need to re-think how we should pass sections
3583 this->input_sections_ = *checkpoint->input_sections();
3586 this->attached_input_sections_are_sorted_ =
3587 checkpoint->attached_input_sections_are_sorted();
3589 // Simply invalidate the fast lookup maps since we do not keep
3591 this->lookup_maps_->invalidate();
3594 // Update the section offsets of input sections in this. This is required if
3595 // relaxation causes some input sections to change sizes.
3598 Output_section::adjust_section_offsets()
3600 if (!this->section_offsets_need_adjustment_)
3604 for (Input_section_list::iterator p = this->input_sections_.begin();
3605 p != this->input_sections_.end();
3608 off = align_address(off, p->addralign());
3609 if (p->is_input_section())
3610 p->relobj()->set_section_offset(p->shndx(), off);
3611 off += p->data_size();
3614 this->section_offsets_need_adjustment_ = false;
3617 // Print to the map file.
3620 Output_section::do_print_to_mapfile(Mapfile* mapfile) const
3622 mapfile->print_output_section(this);
3624 for (Input_section_list::const_iterator p = this->input_sections_.begin();
3625 p != this->input_sections_.end();
3627 p->print_to_mapfile(mapfile);
3630 // Print stats for merge sections to stderr.
3633 Output_section::print_merge_stats()
3635 Input_section_list::iterator p;
3636 for (p = this->input_sections_.begin();
3637 p != this->input_sections_.end();
3639 p->print_merge_stats(this->name_);
3642 // Set a fixed layout for the section. Used for incremental update links.
3645 Output_section::set_fixed_layout(uint64_t sh_addr, off_t sh_offset,
3646 off_t sh_size, uint64_t sh_addralign)
3648 this->addralign_ = sh_addralign;
3649 this->set_current_data_size(sh_size);
3650 if ((this->flags_ & elfcpp::SHF_ALLOC) != 0)
3651 this->set_address(sh_addr);
3652 this->set_file_offset(sh_offset);
3653 this->finalize_data_size();
3654 this->free_list_.init(sh_size, false);
3655 this->has_fixed_layout_ = true;
3658 // Reserve space within the fixed layout for the section. Used for
3659 // incremental update links.
3661 Output_section::reserve(uint64_t sh_offset, uint64_t sh_size)
3663 this->free_list_.remove(sh_offset, sh_offset + sh_size);
3666 // Output segment methods.
3668 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
3678 is_max_align_known_(false),
3679 are_addresses_set_(false),
3680 is_large_data_segment_(false)
3682 // The ELF ABI specifies that a PT_TLS segment always has PF_R as
3684 if (type == elfcpp::PT_TLS)
3685 this->flags_ = elfcpp::PF_R;
3688 // Add an Output_section to a PT_LOAD Output_segment.
3691 Output_segment::add_output_section_to_load(Layout* layout,
3693 elfcpp::Elf_Word seg_flags)
3695 gold_assert(this->type() == elfcpp::PT_LOAD);
3696 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
3697 gold_assert(!this->is_max_align_known_);
3698 gold_assert(os->is_large_data_section() == this->is_large_data_segment());
3700 this->update_flags_for_output_section(seg_flags);
3702 // We don't want to change the ordering if we have a linker script
3703 // with a SECTIONS clause.
3704 Output_section_order order = os->order();
3705 if (layout->script_options()->saw_sections_clause())
3706 order = static_cast<Output_section_order>(0);
3708 gold_assert(order != ORDER_INVALID);
3710 this->output_lists_[order].push_back(os);
3713 // Add an Output_section to a non-PT_LOAD Output_segment.
3716 Output_segment::add_output_section_to_nonload(Output_section* os,
3717 elfcpp::Elf_Word seg_flags)
3719 gold_assert(this->type() != elfcpp::PT_LOAD);
3720 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
3721 gold_assert(!this->is_max_align_known_);
3723 this->update_flags_for_output_section(seg_flags);
3725 this->output_lists_[0].push_back(os);
3728 // Remove an Output_section from this segment. It is an error if it
3732 Output_segment::remove_output_section(Output_section* os)
3734 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
3736 Output_data_list* pdl = &this->output_lists_[i];
3737 for (Output_data_list::iterator p = pdl->begin(); p != pdl->end(); ++p)
3749 // Add an Output_data (which need not be an Output_section) to the
3750 // start of a segment.
3753 Output_segment::add_initial_output_data(Output_data* od)
3755 gold_assert(!this->is_max_align_known_);
3756 Output_data_list::iterator p = this->output_lists_[0].begin();
3757 this->output_lists_[0].insert(p, od);
3760 // Return true if this segment has any sections which hold actual
3761 // data, rather than being a BSS section.
3764 Output_segment::has_any_data_sections() const
3766 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
3768 const Output_data_list* pdl = &this->output_lists_[i];
3769 for (Output_data_list::const_iterator p = pdl->begin();
3773 if (!(*p)->is_section())
3775 if ((*p)->output_section()->type() != elfcpp::SHT_NOBITS)
3782 // Return whether the first data section (not counting TLS sections)
3783 // is a relro section.
3786 Output_segment::is_first_section_relro() const
3788 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
3790 if (i == static_cast<int>(ORDER_TLS_DATA)
3791 || i == static_cast<int>(ORDER_TLS_BSS))
3793 const Output_data_list* pdl = &this->output_lists_[i];
3796 Output_data* p = pdl->front();
3797 return p->is_section() && p->output_section()->is_relro();
3803 // Return the maximum alignment of the Output_data in Output_segment.
3806 Output_segment::maximum_alignment()
3808 if (!this->is_max_align_known_)
3810 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
3812 const Output_data_list* pdl = &this->output_lists_[i];
3813 uint64_t addralign = Output_segment::maximum_alignment_list(pdl);
3814 if (addralign > this->max_align_)
3815 this->max_align_ = addralign;
3817 this->is_max_align_known_ = true;
3820 return this->max_align_;
3823 // Return the maximum alignment of a list of Output_data.
3826 Output_segment::maximum_alignment_list(const Output_data_list* pdl)
3829 for (Output_data_list::const_iterator p = pdl->begin();
3833 uint64_t addralign = (*p)->addralign();
3834 if (addralign > ret)
3840 // Return whether this segment has any dynamic relocs.
3843 Output_segment::has_dynamic_reloc() const
3845 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
3846 if (this->has_dynamic_reloc_list(&this->output_lists_[i]))
3851 // Return whether this Output_data_list has any dynamic relocs.
3854 Output_segment::has_dynamic_reloc_list(const Output_data_list* pdl) const
3856 for (Output_data_list::const_iterator p = pdl->begin();
3859 if ((*p)->has_dynamic_reloc())
3864 // Set the section addresses for an Output_segment. If RESET is true,
3865 // reset the addresses first. ADDR is the address and *POFF is the
3866 // file offset. Set the section indexes starting with *PSHNDX.
3867 // INCREASE_RELRO is the size of the portion of the first non-relro
3868 // section that should be included in the PT_GNU_RELRO segment.
3869 // If this segment has relro sections, and has been aligned for
3870 // that purpose, set *HAS_RELRO to TRUE. Return the address of
3871 // the immediately following segment. Update *HAS_RELRO, *POFF,
3875 Output_segment::set_section_addresses(Layout* layout, bool reset,
3877 unsigned int* increase_relro,
3880 unsigned int* pshndx)
3882 gold_assert(this->type_ == elfcpp::PT_LOAD);
3884 uint64_t last_relro_pad = 0;
3885 off_t orig_off = *poff;
3887 bool in_tls = false;
3889 // If we have relro sections, we need to pad forward now so that the
3890 // relro sections plus INCREASE_RELRO end on a common page boundary.
3891 if (parameters->options().relro()
3892 && this->is_first_section_relro()
3893 && (!this->are_addresses_set_ || reset))
3895 uint64_t relro_size = 0;
3897 uint64_t max_align = 0;
3898 for (int i = 0; i <= static_cast<int>(ORDER_RELRO_LAST); ++i)
3900 Output_data_list* pdl = &this->output_lists_[i];
3901 Output_data_list::iterator p;
3902 for (p = pdl->begin(); p != pdl->end(); ++p)
3904 if (!(*p)->is_section())
3906 uint64_t align = (*p)->addralign();
3907 if (align > max_align)
3909 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
3913 // Align the first non-TLS section to the alignment
3914 // of the TLS segment.
3918 relro_size = align_address(relro_size, align);
3919 // Ignore the size of the .tbss section.
3920 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS)
3921 && (*p)->is_section_type(elfcpp::SHT_NOBITS))
3923 if ((*p)->is_address_valid())
3924 relro_size += (*p)->data_size();
3927 // FIXME: This could be faster.
3928 (*p)->set_address_and_file_offset(addr + relro_size,
3930 relro_size += (*p)->data_size();
3931 (*p)->reset_address_and_file_offset();
3934 if (p != pdl->end())
3937 relro_size += *increase_relro;
3938 // Pad the total relro size to a multiple of the maximum
3939 // section alignment seen.
3940 uint64_t aligned_size = align_address(relro_size, max_align);
3941 // Note the amount of padding added after the last relro section.
3942 last_relro_pad = aligned_size - relro_size;
3945 uint64_t page_align = parameters->target().common_pagesize();
3947 // Align to offset N such that (N + RELRO_SIZE) % PAGE_ALIGN == 0.
3948 uint64_t desired_align = page_align - (aligned_size % page_align);
3949 if (desired_align < *poff % page_align)
3950 *poff += page_align - *poff % page_align;
3951 *poff += desired_align - *poff % page_align;
3952 addr += *poff - orig_off;
3956 if (!reset && this->are_addresses_set_)
3958 gold_assert(this->paddr_ == addr);
3959 addr = this->vaddr_;
3963 this->vaddr_ = addr;
3964 this->paddr_ = addr;
3965 this->are_addresses_set_ = true;
3970 this->offset_ = orig_off;
3974 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
3976 if (i == static_cast<int>(ORDER_RELRO_LAST))
3978 *poff += last_relro_pad;
3979 addr += last_relro_pad;
3980 if (this->output_lists_[i].empty())
3982 // If there is nothing in the ORDER_RELRO_LAST list,
3983 // the padding will occur at the end of the relro
3984 // segment, and we need to add it to *INCREASE_RELRO.
3985 *increase_relro += last_relro_pad;
3988 addr = this->set_section_list_addresses(layout, reset,
3989 &this->output_lists_[i],
3990 addr, poff, pshndx, &in_tls);
3991 if (i < static_cast<int>(ORDER_SMALL_BSS))
3993 this->filesz_ = *poff - orig_off;
4000 // If the last section was a TLS section, align upward to the
4001 // alignment of the TLS segment, so that the overall size of the TLS
4002 // segment is aligned.
4005 uint64_t segment_align = layout->tls_segment()->maximum_alignment();
4006 *poff = align_address(*poff, segment_align);
4009 this->memsz_ = *poff - orig_off;
4011 // Ignore the file offset adjustments made by the BSS Output_data
4018 // Set the addresses and file offsets in a list of Output_data
4022 Output_segment::set_section_list_addresses(Layout* layout, bool reset,
4023 Output_data_list* pdl,
4024 uint64_t addr, off_t* poff,
4025 unsigned int* pshndx,
4028 off_t startoff = *poff;
4029 // For incremental updates, we may allocate non-fixed sections from
4030 // free space in the file. This keeps track of the high-water mark.
4031 off_t maxoff = startoff;
4033 off_t off = startoff;
4034 for (Output_data_list::iterator p = pdl->begin();
4039 (*p)->reset_address_and_file_offset();
4041 // When doing an incremental update or when using a linker script,
4042 // the section will most likely already have an address.
4043 if (!(*p)->is_address_valid())
4045 uint64_t align = (*p)->addralign();
4047 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
4049 // Give the first TLS section the alignment of the
4050 // entire TLS segment. Otherwise the TLS segment as a
4051 // whole may be misaligned.
4054 Output_segment* tls_segment = layout->tls_segment();
4055 gold_assert(tls_segment != NULL);
4056 uint64_t segment_align = tls_segment->maximum_alignment();
4057 gold_assert(segment_align >= align);
4058 align = segment_align;
4065 // If this is the first section after the TLS segment,
4066 // align it to at least the alignment of the TLS
4067 // segment, so that the size of the overall TLS segment
4071 uint64_t segment_align =
4072 layout->tls_segment()->maximum_alignment();
4073 if (segment_align > align)
4074 align = segment_align;
4080 // FIXME: Need to handle TLS and .bss with incremental update.
4081 if (!parameters->incremental_update()
4082 || (*p)->is_section_flag_set(elfcpp::SHF_TLS)
4083 || (*p)->is_section_type(elfcpp::SHT_NOBITS))
4085 off = align_address(off, align);
4086 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
4090 // Incremental update: allocate file space from free list.
4091 (*p)->pre_finalize_data_size();
4092 off_t current_size = (*p)->current_data_size();
4093 off = layout->allocate(current_size, align, startoff);
4096 gold_assert((*p)->output_section() != NULL);
4097 gold_fatal(_("out of patch space for section %s; "
4098 "relink with --incremental-full"),
4099 (*p)->output_section()->name());
4101 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
4102 if ((*p)->data_size() > current_size)
4104 gold_assert((*p)->output_section() != NULL);
4105 gold_fatal(_("%s: section changed size; "
4106 "relink with --incremental-full"),
4107 (*p)->output_section()->name());
4111 else if (parameters->incremental_update())
4113 // For incremental updates, use the fixed offset for the
4114 // high-water mark computation.
4115 off = (*p)->offset();
4119 // The script may have inserted a skip forward, but it
4120 // better not have moved backward.
4121 if ((*p)->address() >= addr + (off - startoff))
4122 off += (*p)->address() - (addr + (off - startoff));
4125 if (!layout->script_options()->saw_sections_clause())
4129 Output_section* os = (*p)->output_section();
4131 // Cast to unsigned long long to avoid format warnings.
4132 unsigned long long previous_dot =
4133 static_cast<unsigned long long>(addr + (off - startoff));
4134 unsigned long long dot =
4135 static_cast<unsigned long long>((*p)->address());
4138 gold_error(_("dot moves backward in linker script "
4139 "from 0x%llx to 0x%llx"), previous_dot, dot);
4141 gold_error(_("address of section '%s' moves backward "
4142 "from 0x%llx to 0x%llx"),
4143 os->name(), previous_dot, dot);
4146 (*p)->set_file_offset(off);
4147 (*p)->finalize_data_size();
4150 gold_debug(DEBUG_INCREMENTAL,
4151 "set_section_list_addresses: %08lx %08lx %s",
4152 static_cast<long>(off),
4153 static_cast<long>((*p)->data_size()),
4154 ((*p)->output_section() != NULL
4155 ? (*p)->output_section()->name() : "(special)"));
4157 // We want to ignore the size of a SHF_TLS or SHT_NOBITS
4158 // section. Such a section does not affect the size of a
4160 if (!(*p)->is_section_flag_set(elfcpp::SHF_TLS)
4161 || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
4162 off += (*p)->data_size();
4167 if ((*p)->is_section())
4169 (*p)->set_out_shndx(*pshndx);
4175 return addr + (maxoff - startoff);
4178 // For a non-PT_LOAD segment, set the offset from the sections, if
4179 // any. Add INCREASE to the file size and the memory size.
4182 Output_segment::set_offset(unsigned int increase)
4184 gold_assert(this->type_ != elfcpp::PT_LOAD);
4186 gold_assert(!this->are_addresses_set_);
4188 // A non-load section only uses output_lists_[0].
4190 Output_data_list* pdl = &this->output_lists_[0];
4194 gold_assert(increase == 0);
4197 this->are_addresses_set_ = true;
4199 this->min_p_align_ = 0;
4205 // Find the first and last section by address.
4206 const Output_data* first = NULL;
4207 const Output_data* last_data = NULL;
4208 const Output_data* last_bss = NULL;
4209 for (Output_data_list::const_iterator p = pdl->begin();
4214 || (*p)->address() < first->address()
4215 || ((*p)->address() == first->address()
4216 && (*p)->data_size() < first->data_size()))
4218 const Output_data** plast;
4219 if ((*p)->is_section()
4220 && (*p)->output_section()->type() == elfcpp::SHT_NOBITS)
4225 || (*p)->address() > (*plast)->address()
4226 || ((*p)->address() == (*plast)->address()
4227 && (*p)->data_size() > (*plast)->data_size()))
4231 this->vaddr_ = first->address();
4232 this->paddr_ = (first->has_load_address()
4233 ? first->load_address()
4235 this->are_addresses_set_ = true;
4236 this->offset_ = first->offset();
4238 if (last_data == NULL)
4241 this->filesz_ = (last_data->address()
4242 + last_data->data_size()
4245 const Output_data* last = last_bss != NULL ? last_bss : last_data;
4246 this->memsz_ = (last->address()
4250 this->filesz_ += increase;
4251 this->memsz_ += increase;
4253 // If this is a RELRO segment, verify that the segment ends at a
4255 if (this->type_ == elfcpp::PT_GNU_RELRO)
4257 uint64_t page_align = parameters->target().common_pagesize();
4258 uint64_t segment_end = this->vaddr_ + this->memsz_;
4259 if (parameters->incremental_update())
4261 // The INCREASE_RELRO calculation is bypassed for an incremental
4262 // update, so we need to adjust the segment size manually here.
4263 segment_end = align_address(segment_end, page_align);
4264 this->memsz_ = segment_end - this->vaddr_;
4267 gold_assert(segment_end == align_address(segment_end, page_align));
4270 // If this is a TLS segment, align the memory size. The code in
4271 // set_section_list ensures that the section after the TLS segment
4272 // is aligned to give us room.
4273 if (this->type_ == elfcpp::PT_TLS)
4275 uint64_t segment_align = this->maximum_alignment();
4276 gold_assert(this->vaddr_ == align_address(this->vaddr_, segment_align));
4277 this->memsz_ = align_address(this->memsz_, segment_align);
4281 // Set the TLS offsets of the sections in the PT_TLS segment.
4284 Output_segment::set_tls_offsets()
4286 gold_assert(this->type_ == elfcpp::PT_TLS);
4288 for (Output_data_list::iterator p = this->output_lists_[0].begin();
4289 p != this->output_lists_[0].end();
4291 (*p)->set_tls_offset(this->vaddr_);
4294 // Return the load address of the first section.
4297 Output_segment::first_section_load_address() const
4299 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4301 const Output_data_list* pdl = &this->output_lists_[i];
4302 for (Output_data_list::const_iterator p = pdl->begin();
4306 if ((*p)->is_section())
4307 return ((*p)->has_load_address()
4308 ? (*p)->load_address()
4315 // Return the number of Output_sections in an Output_segment.
4318 Output_segment::output_section_count() const
4320 unsigned int ret = 0;
4321 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4322 ret += this->output_section_count_list(&this->output_lists_[i]);
4326 // Return the number of Output_sections in an Output_data_list.
4329 Output_segment::output_section_count_list(const Output_data_list* pdl) const
4331 unsigned int count = 0;
4332 for (Output_data_list::const_iterator p = pdl->begin();
4336 if ((*p)->is_section())
4342 // Return the section attached to the list segment with the lowest
4343 // load address. This is used when handling a PHDRS clause in a
4347 Output_segment::section_with_lowest_load_address() const
4349 Output_section* found = NULL;
4350 uint64_t found_lma = 0;
4351 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4352 this->lowest_load_address_in_list(&this->output_lists_[i], &found,
4357 // Look through a list for a section with a lower load address.
4360 Output_segment::lowest_load_address_in_list(const Output_data_list* pdl,
4361 Output_section** found,
4362 uint64_t* found_lma) const
4364 for (Output_data_list::const_iterator p = pdl->begin();
4368 if (!(*p)->is_section())
4370 Output_section* os = static_cast<Output_section*>(*p);
4371 uint64_t lma = (os->has_load_address()
4372 ? os->load_address()
4374 if (*found == NULL || lma < *found_lma)
4382 // Write the segment data into *OPHDR.
4384 template<int size, bool big_endian>
4386 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
4388 ophdr->put_p_type(this->type_);
4389 ophdr->put_p_offset(this->offset_);
4390 ophdr->put_p_vaddr(this->vaddr_);
4391 ophdr->put_p_paddr(this->paddr_);
4392 ophdr->put_p_filesz(this->filesz_);
4393 ophdr->put_p_memsz(this->memsz_);
4394 ophdr->put_p_flags(this->flags_);
4395 ophdr->put_p_align(std::max(this->min_p_align_, this->maximum_alignment()));
4398 // Write the section headers into V.
4400 template<int size, bool big_endian>
4402 Output_segment::write_section_headers(const Layout* layout,
4403 const Stringpool* secnamepool,
4405 unsigned int* pshndx) const
4407 // Every section that is attached to a segment must be attached to a
4408 // PT_LOAD segment, so we only write out section headers for PT_LOAD
4410 if (this->type_ != elfcpp::PT_LOAD)
4413 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4415 const Output_data_list* pdl = &this->output_lists_[i];
4416 v = this->write_section_headers_list<size, big_endian>(layout,
4425 template<int size, bool big_endian>
4427 Output_segment::write_section_headers_list(const Layout* layout,
4428 const Stringpool* secnamepool,
4429 const Output_data_list* pdl,
4431 unsigned int* pshndx) const
4433 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
4434 for (Output_data_list::const_iterator p = pdl->begin();
4438 if ((*p)->is_section())
4440 const Output_section* ps = static_cast<const Output_section*>(*p);
4441 gold_assert(*pshndx == ps->out_shndx());
4442 elfcpp::Shdr_write<size, big_endian> oshdr(v);
4443 ps->write_header(layout, secnamepool, &oshdr);
4451 // Print the output sections to the map file.
4454 Output_segment::print_sections_to_mapfile(Mapfile* mapfile) const
4456 if (this->type() != elfcpp::PT_LOAD)
4458 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4459 this->print_section_list_to_mapfile(mapfile, &this->output_lists_[i]);
4462 // Print an output section list to the map file.
4465 Output_segment::print_section_list_to_mapfile(Mapfile* mapfile,
4466 const Output_data_list* pdl) const
4468 for (Output_data_list::const_iterator p = pdl->begin();
4471 (*p)->print_to_mapfile(mapfile);
4474 // Output_file methods.
4476 Output_file::Output_file(const char* name)
4481 map_is_anonymous_(false),
4482 map_is_allocated_(false),
4483 is_temporary_(false)
4487 // Try to open an existing file. Returns false if the file doesn't
4488 // exist, has a size of 0 or can't be mmapped.
4491 Output_file::open_for_modification()
4493 // The name "-" means "stdout".
4494 if (strcmp(this->name_, "-") == 0)
4497 // Don't bother opening files with a size of zero.
4499 if (::stat(this->name_, &s) != 0 || s.st_size == 0)
4502 int o = open_descriptor(-1, this->name_, O_RDWR, 0);
4504 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
4506 this->file_size_ = s.st_size;
4508 // If the file can't be mmapped, copying the content to an anonymous
4509 // map will probably negate the performance benefits of incremental
4510 // linking. This could be helped by using views and loading only
4511 // the necessary parts, but this is not supported as of now.
4512 if (!this->map_no_anonymous())
4514 release_descriptor(o, true);
4516 this->file_size_ = 0;
4523 // Open the output file.
4526 Output_file::open(off_t file_size)
4528 this->file_size_ = file_size;
4530 // Unlink the file first; otherwise the open() may fail if the file
4531 // is busy (e.g. it's an executable that's currently being executed).
4533 // However, the linker may be part of a system where a zero-length
4534 // file is created for it to write to, with tight permissions (gcc
4535 // 2.95 did something like this). Unlinking the file would work
4536 // around those permission controls, so we only unlink if the file
4537 // has a non-zero size. We also unlink only regular files to avoid
4538 // trouble with directories/etc.
4540 // If we fail, continue; this command is merely a best-effort attempt
4541 // to improve the odds for open().
4543 // We let the name "-" mean "stdout"
4544 if (!this->is_temporary_)
4546 if (strcmp(this->name_, "-") == 0)
4547 this->o_ = STDOUT_FILENO;
4551 if (::stat(this->name_, &s) == 0
4552 && (S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
4555 ::unlink(this->name_);
4556 else if (!parameters->options().relocatable())
4558 // If we don't unlink the existing file, add execute
4559 // permission where read permissions already exist
4560 // and where the umask permits.
4561 int mask = ::umask(0);
4563 s.st_mode |= (s.st_mode & 0444) >> 2;
4564 ::chmod(this->name_, s.st_mode & ~mask);
4568 int mode = parameters->options().relocatable() ? 0666 : 0777;
4569 int o = open_descriptor(-1, this->name_, O_RDWR | O_CREAT | O_TRUNC,
4572 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
4580 // Resize the output file.
4583 Output_file::resize(off_t file_size)
4585 // If the mmap is mapping an anonymous memory buffer, this is easy:
4586 // just mremap to the new size. If it's mapping to a file, we want
4587 // to unmap to flush to the file, then remap after growing the file.
4588 if (this->map_is_anonymous_)
4591 if (!this->map_is_allocated_)
4593 base = ::mremap(this->base_, this->file_size_, file_size,
4595 if (base == MAP_FAILED)
4596 gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
4600 base = realloc(this->base_, file_size);
4603 if (file_size > this->file_size_)
4604 memset(static_cast<char*>(base) + this->file_size_, 0,
4605 file_size - this->file_size_);
4607 this->base_ = static_cast<unsigned char*>(base);
4608 this->file_size_ = file_size;
4613 this->file_size_ = file_size;
4614 if (!this->map_no_anonymous())
4615 gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
4619 // Map an anonymous block of memory which will later be written to the
4620 // file. Return whether the map succeeded.
4623 Output_file::map_anonymous()
4625 void* base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
4626 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
4627 if (base == MAP_FAILED)
4629 base = malloc(this->file_size_);
4632 memset(base, 0, this->file_size_);
4633 this->map_is_allocated_ = true;
4635 this->base_ = static_cast<unsigned char*>(base);
4636 this->map_is_anonymous_ = true;
4640 // Map the file into memory. Return whether the mapping succeeded.
4643 Output_file::map_no_anonymous()
4645 const int o = this->o_;
4647 // If the output file is not a regular file, don't try to mmap it;
4648 // instead, we'll mmap a block of memory (an anonymous buffer), and
4649 // then later write the buffer to the file.
4651 struct stat statbuf;
4652 if (o == STDOUT_FILENO || o == STDERR_FILENO
4653 || ::fstat(o, &statbuf) != 0
4654 || !S_ISREG(statbuf.st_mode)
4655 || this->is_temporary_)
4658 // Ensure that we have disk space available for the file. If we
4659 // don't do this, it is possible that we will call munmap, close,
4660 // and exit with dirty buffers still in the cache with no assigned
4661 // disk blocks. If the disk is out of space at that point, the
4662 // output file will wind up incomplete, but we will have already
4663 // exited. The alternative to fallocate would be to use fdatasync,
4664 // but that would be a more significant performance hit.
4665 if (::posix_fallocate(o, 0, this->file_size_) < 0)
4666 gold_fatal(_("%s: %s"), this->name_, strerror(errno));
4668 // Map the file into memory.
4669 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
4672 // The mmap call might fail because of file system issues: the file
4673 // system might not support mmap at all, or it might not support
4674 // mmap with PROT_WRITE.
4675 if (base == MAP_FAILED)
4678 this->map_is_anonymous_ = false;
4679 this->base_ = static_cast<unsigned char*>(base);
4683 // Map the file into memory.
4688 if (this->map_no_anonymous())
4691 // The mmap call might fail because of file system issues: the file
4692 // system might not support mmap at all, or it might not support
4693 // mmap with PROT_WRITE. I'm not sure which errno values we will
4694 // see in all cases, so if the mmap fails for any reason and we
4695 // don't care about file contents, try for an anonymous map.
4696 if (this->map_anonymous())
4699 gold_fatal(_("%s: mmap: failed to allocate %lu bytes for output file: %s"),
4700 this->name_, static_cast<unsigned long>(this->file_size_),
4704 // Unmap the file from memory.
4707 Output_file::unmap()
4709 if (this->map_is_anonymous_)
4711 // We've already written out the data, so there is no reason to
4712 // waste time unmapping or freeing the memory.
4716 if (::munmap(this->base_, this->file_size_) < 0)
4717 gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
4722 // Close the output file.
4725 Output_file::close()
4727 // If the map isn't file-backed, we need to write it now.
4728 if (this->map_is_anonymous_ && !this->is_temporary_)
4730 size_t bytes_to_write = this->file_size_;
4732 while (bytes_to_write > 0)
4734 ssize_t bytes_written = ::write(this->o_, this->base_ + offset,
4736 if (bytes_written == 0)
4737 gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
4738 else if (bytes_written < 0)
4739 gold_error(_("%s: write: %s"), this->name_, strerror(errno));
4742 bytes_to_write -= bytes_written;
4743 offset += bytes_written;
4749 // We don't close stdout or stderr
4750 if (this->o_ != STDOUT_FILENO
4751 && this->o_ != STDERR_FILENO
4752 && !this->is_temporary_)
4753 if (::close(this->o_) < 0)
4754 gold_error(_("%s: close: %s"), this->name_, strerror(errno));
4758 // Instantiate the templates we need. We could use the configure
4759 // script to restrict this to only the ones for implemented targets.
4761 #ifdef HAVE_TARGET_32_LITTLE
4764 Output_section::add_input_section<32, false>(
4766 Sized_relobj<32, false>* object,
4768 const char* secname,
4769 const elfcpp::Shdr<32, false>& shdr,
4770 unsigned int reloc_shndx,
4771 bool have_sections_script);
4774 #ifdef HAVE_TARGET_32_BIG
4777 Output_section::add_input_section<32, true>(
4779 Sized_relobj<32, true>* object,
4781 const char* secname,
4782 const elfcpp::Shdr<32, true>& shdr,
4783 unsigned int reloc_shndx,
4784 bool have_sections_script);
4787 #ifdef HAVE_TARGET_64_LITTLE
4790 Output_section::add_input_section<64, false>(
4792 Sized_relobj<64, false>* object,
4794 const char* secname,
4795 const elfcpp::Shdr<64, false>& shdr,
4796 unsigned int reloc_shndx,
4797 bool have_sections_script);
4800 #ifdef HAVE_TARGET_64_BIG
4803 Output_section::add_input_section<64, true>(
4805 Sized_relobj<64, true>* object,
4807 const char* secname,
4808 const elfcpp::Shdr<64, true>& shdr,
4809 unsigned int reloc_shndx,
4810 bool have_sections_script);
4813 #ifdef HAVE_TARGET_32_LITTLE
4815 class Output_reloc<elfcpp::SHT_REL, false, 32, false>;
4818 #ifdef HAVE_TARGET_32_BIG
4820 class Output_reloc<elfcpp::SHT_REL, false, 32, true>;
4823 #ifdef HAVE_TARGET_64_LITTLE
4825 class Output_reloc<elfcpp::SHT_REL, false, 64, false>;
4828 #ifdef HAVE_TARGET_64_BIG
4830 class Output_reloc<elfcpp::SHT_REL, false, 64, true>;
4833 #ifdef HAVE_TARGET_32_LITTLE
4835 class Output_reloc<elfcpp::SHT_REL, true, 32, false>;
4838 #ifdef HAVE_TARGET_32_BIG
4840 class Output_reloc<elfcpp::SHT_REL, true, 32, true>;
4843 #ifdef HAVE_TARGET_64_LITTLE
4845 class Output_reloc<elfcpp::SHT_REL, true, 64, false>;
4848 #ifdef HAVE_TARGET_64_BIG
4850 class Output_reloc<elfcpp::SHT_REL, true, 64, true>;
4853 #ifdef HAVE_TARGET_32_LITTLE
4855 class Output_reloc<elfcpp::SHT_RELA, false, 32, false>;
4858 #ifdef HAVE_TARGET_32_BIG
4860 class Output_reloc<elfcpp::SHT_RELA, false, 32, true>;
4863 #ifdef HAVE_TARGET_64_LITTLE
4865 class Output_reloc<elfcpp::SHT_RELA, false, 64, false>;
4868 #ifdef HAVE_TARGET_64_BIG
4870 class Output_reloc<elfcpp::SHT_RELA, false, 64, true>;
4873 #ifdef HAVE_TARGET_32_LITTLE
4875 class Output_reloc<elfcpp::SHT_RELA, true, 32, false>;
4878 #ifdef HAVE_TARGET_32_BIG
4880 class Output_reloc<elfcpp::SHT_RELA, true, 32, true>;
4883 #ifdef HAVE_TARGET_64_LITTLE
4885 class Output_reloc<elfcpp::SHT_RELA, true, 64, false>;
4888 #ifdef HAVE_TARGET_64_BIG
4890 class Output_reloc<elfcpp::SHT_RELA, true, 64, true>;
4893 #ifdef HAVE_TARGET_32_LITTLE
4895 class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
4898 #ifdef HAVE_TARGET_32_BIG
4900 class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
4903 #ifdef HAVE_TARGET_64_LITTLE
4905 class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
4908 #ifdef HAVE_TARGET_64_BIG
4910 class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
4913 #ifdef HAVE_TARGET_32_LITTLE
4915 class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
4918 #ifdef HAVE_TARGET_32_BIG
4920 class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
4923 #ifdef HAVE_TARGET_64_LITTLE
4925 class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
4928 #ifdef HAVE_TARGET_64_BIG
4930 class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
4933 #ifdef HAVE_TARGET_32_LITTLE
4935 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
4938 #ifdef HAVE_TARGET_32_BIG
4940 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
4943 #ifdef HAVE_TARGET_64_LITTLE
4945 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
4948 #ifdef HAVE_TARGET_64_BIG
4950 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
4953 #ifdef HAVE_TARGET_32_LITTLE
4955 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
4958 #ifdef HAVE_TARGET_32_BIG
4960 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
4963 #ifdef HAVE_TARGET_64_LITTLE
4965 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
4968 #ifdef HAVE_TARGET_64_BIG
4970 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
4973 #ifdef HAVE_TARGET_32_LITTLE
4975 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, false>;
4978 #ifdef HAVE_TARGET_32_BIG
4980 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, true>;
4983 #ifdef HAVE_TARGET_64_LITTLE
4985 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, false>;
4988 #ifdef HAVE_TARGET_64_BIG
4990 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, true>;
4993 #ifdef HAVE_TARGET_32_LITTLE
4995 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, false>;
4998 #ifdef HAVE_TARGET_32_BIG
5000 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, true>;
5003 #ifdef HAVE_TARGET_64_LITTLE
5005 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, false>;
5008 #ifdef HAVE_TARGET_64_BIG
5010 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, true>;
5013 #ifdef HAVE_TARGET_32_LITTLE
5015 class Output_data_group<32, false>;
5018 #ifdef HAVE_TARGET_32_BIG
5020 class Output_data_group<32, true>;
5023 #ifdef HAVE_TARGET_64_LITTLE
5025 class Output_data_group<64, false>;
5028 #ifdef HAVE_TARGET_64_BIG
5030 class Output_data_group<64, true>;
5033 #ifdef HAVE_TARGET_32_LITTLE
5035 class Output_data_got<32, false>;
5038 #ifdef HAVE_TARGET_32_BIG
5040 class Output_data_got<32, true>;
5043 #ifdef HAVE_TARGET_64_LITTLE
5045 class Output_data_got<64, false>;
5048 #ifdef HAVE_TARGET_64_BIG
5050 class Output_data_got<64, true>;
5053 } // End namespace gold.