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)
124 // Mingw does not have S_ISLNK.
126 # define S_ISLNK(mode) 0
132 // Output_data variables.
134 bool Output_data::allocated_sizes_are_fixed;
136 // Output_data methods.
138 Output_data::~Output_data()
142 // Return the default alignment for the target size.
145 Output_data::default_alignment()
147 return Output_data::default_alignment_for_size(
148 parameters->target().get_size());
151 // Return the default alignment for a size--32 or 64.
154 Output_data::default_alignment_for_size(int size)
164 // Output_section_header methods. This currently assumes that the
165 // segment and section lists are complete at construction time.
167 Output_section_headers::Output_section_headers(
168 const Layout* layout,
169 const Layout::Segment_list* segment_list,
170 const Layout::Section_list* section_list,
171 const Layout::Section_list* unattached_section_list,
172 const Stringpool* secnamepool,
173 const Output_section* shstrtab_section)
175 segment_list_(segment_list),
176 section_list_(section_list),
177 unattached_section_list_(unattached_section_list),
178 secnamepool_(secnamepool),
179 shstrtab_section_(shstrtab_section)
183 // Compute the current data size.
186 Output_section_headers::do_size() const
188 // Count all the sections. Start with 1 for the null section.
190 if (!parameters->options().relocatable())
192 for (Layout::Segment_list::const_iterator p =
193 this->segment_list_->begin();
194 p != this->segment_list_->end();
196 if ((*p)->type() == elfcpp::PT_LOAD)
197 count += (*p)->output_section_count();
201 for (Layout::Section_list::const_iterator p =
202 this->section_list_->begin();
203 p != this->section_list_->end();
205 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
208 count += this->unattached_section_list_->size();
210 const int size = parameters->target().get_size();
213 shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
215 shdr_size = elfcpp::Elf_sizes<64>::shdr_size;
219 return count * shdr_size;
222 // Write out the section headers.
225 Output_section_headers::do_write(Output_file* of)
227 switch (parameters->size_and_endianness())
229 #ifdef HAVE_TARGET_32_LITTLE
230 case Parameters::TARGET_32_LITTLE:
231 this->do_sized_write<32, false>(of);
234 #ifdef HAVE_TARGET_32_BIG
235 case Parameters::TARGET_32_BIG:
236 this->do_sized_write<32, true>(of);
239 #ifdef HAVE_TARGET_64_LITTLE
240 case Parameters::TARGET_64_LITTLE:
241 this->do_sized_write<64, false>(of);
244 #ifdef HAVE_TARGET_64_BIG
245 case Parameters::TARGET_64_BIG:
246 this->do_sized_write<64, true>(of);
254 template<int size, bool big_endian>
256 Output_section_headers::do_sized_write(Output_file* of)
258 off_t all_shdrs_size = this->data_size();
259 unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size);
261 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
262 unsigned char* v = view;
265 typename elfcpp::Shdr_write<size, big_endian> oshdr(v);
266 oshdr.put_sh_name(0);
267 oshdr.put_sh_type(elfcpp::SHT_NULL);
268 oshdr.put_sh_flags(0);
269 oshdr.put_sh_addr(0);
270 oshdr.put_sh_offset(0);
272 size_t section_count = (this->data_size()
273 / elfcpp::Elf_sizes<size>::shdr_size);
274 if (section_count < elfcpp::SHN_LORESERVE)
275 oshdr.put_sh_size(0);
277 oshdr.put_sh_size(section_count);
279 unsigned int shstrndx = this->shstrtab_section_->out_shndx();
280 if (shstrndx < elfcpp::SHN_LORESERVE)
281 oshdr.put_sh_link(0);
283 oshdr.put_sh_link(shstrndx);
285 size_t segment_count = this->segment_list_->size();
286 oshdr.put_sh_info(segment_count >= elfcpp::PN_XNUM ? segment_count : 0);
288 oshdr.put_sh_addralign(0);
289 oshdr.put_sh_entsize(0);
294 unsigned int shndx = 1;
295 if (!parameters->options().relocatable())
297 for (Layout::Segment_list::const_iterator p =
298 this->segment_list_->begin();
299 p != this->segment_list_->end();
301 v = (*p)->write_section_headers<size, big_endian>(this->layout_,
308 for (Layout::Section_list::const_iterator p =
309 this->section_list_->begin();
310 p != this->section_list_->end();
313 // We do unallocated sections below, except that group
314 // sections have to come first.
315 if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
316 && (*p)->type() != elfcpp::SHT_GROUP)
318 gold_assert(shndx == (*p)->out_shndx());
319 elfcpp::Shdr_write<size, big_endian> oshdr(v);
320 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
326 for (Layout::Section_list::const_iterator p =
327 this->unattached_section_list_->begin();
328 p != this->unattached_section_list_->end();
331 // For a relocatable link, we did unallocated group sections
332 // above, since they have to come first.
333 if ((*p)->type() == elfcpp::SHT_GROUP
334 && parameters->options().relocatable())
336 gold_assert(shndx == (*p)->out_shndx());
337 elfcpp::Shdr_write<size, big_endian> oshdr(v);
338 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
343 of->write_output_view(this->offset(), all_shdrs_size, view);
346 // Output_segment_header methods.
348 Output_segment_headers::Output_segment_headers(
349 const Layout::Segment_list& segment_list)
350 : segment_list_(segment_list)
352 this->set_current_data_size_for_child(this->do_size());
356 Output_segment_headers::do_write(Output_file* of)
358 switch (parameters->size_and_endianness())
360 #ifdef HAVE_TARGET_32_LITTLE
361 case Parameters::TARGET_32_LITTLE:
362 this->do_sized_write<32, false>(of);
365 #ifdef HAVE_TARGET_32_BIG
366 case Parameters::TARGET_32_BIG:
367 this->do_sized_write<32, true>(of);
370 #ifdef HAVE_TARGET_64_LITTLE
371 case Parameters::TARGET_64_LITTLE:
372 this->do_sized_write<64, false>(of);
375 #ifdef HAVE_TARGET_64_BIG
376 case Parameters::TARGET_64_BIG:
377 this->do_sized_write<64, true>(of);
385 template<int size, bool big_endian>
387 Output_segment_headers::do_sized_write(Output_file* of)
389 const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size;
390 off_t all_phdrs_size = this->segment_list_.size() * phdr_size;
391 gold_assert(all_phdrs_size == this->data_size());
392 unsigned char* view = of->get_output_view(this->offset(),
394 unsigned char* v = view;
395 for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
396 p != this->segment_list_.end();
399 elfcpp::Phdr_write<size, big_endian> ophdr(v);
400 (*p)->write_header(&ophdr);
404 gold_assert(v - view == all_phdrs_size);
406 of->write_output_view(this->offset(), all_phdrs_size, view);
410 Output_segment_headers::do_size() const
412 const int size = parameters->target().get_size();
415 phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
417 phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
421 return this->segment_list_.size() * phdr_size;
424 // Output_file_header methods.
426 Output_file_header::Output_file_header(const Target* target,
427 const Symbol_table* symtab,
428 const Output_segment_headers* osh,
432 segment_header_(osh),
433 section_header_(NULL),
437 this->set_data_size(this->do_size());
440 // Set the section table information for a file header.
443 Output_file_header::set_section_info(const Output_section_headers* shdrs,
444 const Output_section* shstrtab)
446 this->section_header_ = shdrs;
447 this->shstrtab_ = shstrtab;
450 // Write out the file header.
453 Output_file_header::do_write(Output_file* of)
455 gold_assert(this->offset() == 0);
457 switch (parameters->size_and_endianness())
459 #ifdef HAVE_TARGET_32_LITTLE
460 case Parameters::TARGET_32_LITTLE:
461 this->do_sized_write<32, false>(of);
464 #ifdef HAVE_TARGET_32_BIG
465 case Parameters::TARGET_32_BIG:
466 this->do_sized_write<32, true>(of);
469 #ifdef HAVE_TARGET_64_LITTLE
470 case Parameters::TARGET_64_LITTLE:
471 this->do_sized_write<64, false>(of);
474 #ifdef HAVE_TARGET_64_BIG
475 case Parameters::TARGET_64_BIG:
476 this->do_sized_write<64, true>(of);
484 // Write out the file header with appropriate size and endianess.
486 template<int size, bool big_endian>
488 Output_file_header::do_sized_write(Output_file* of)
490 gold_assert(this->offset() == 0);
492 int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
493 unsigned char* view = of->get_output_view(0, ehdr_size);
494 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
496 unsigned char e_ident[elfcpp::EI_NIDENT];
497 memset(e_ident, 0, elfcpp::EI_NIDENT);
498 e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
499 e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
500 e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
501 e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
503 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
505 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
508 e_ident[elfcpp::EI_DATA] = (big_endian
509 ? elfcpp::ELFDATA2MSB
510 : elfcpp::ELFDATA2LSB);
511 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
512 oehdr.put_e_ident(e_ident);
515 if (parameters->options().relocatable())
516 e_type = elfcpp::ET_REL;
517 else if (parameters->options().output_is_position_independent())
518 e_type = elfcpp::ET_DYN;
520 e_type = elfcpp::ET_EXEC;
521 oehdr.put_e_type(e_type);
523 oehdr.put_e_machine(this->target_->machine_code());
524 oehdr.put_e_version(elfcpp::EV_CURRENT);
526 oehdr.put_e_entry(this->entry<size>());
528 if (this->segment_header_ == NULL)
529 oehdr.put_e_phoff(0);
531 oehdr.put_e_phoff(this->segment_header_->offset());
533 oehdr.put_e_shoff(this->section_header_->offset());
534 oehdr.put_e_flags(this->target_->processor_specific_flags());
535 oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
537 if (this->segment_header_ == NULL)
539 oehdr.put_e_phentsize(0);
540 oehdr.put_e_phnum(0);
544 oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
545 size_t phnum = (this->segment_header_->data_size()
546 / elfcpp::Elf_sizes<size>::phdr_size);
547 if (phnum > elfcpp::PN_XNUM)
548 phnum = elfcpp::PN_XNUM;
549 oehdr.put_e_phnum(phnum);
552 oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
553 size_t section_count = (this->section_header_->data_size()
554 / elfcpp::Elf_sizes<size>::shdr_size);
556 if (section_count < elfcpp::SHN_LORESERVE)
557 oehdr.put_e_shnum(this->section_header_->data_size()
558 / elfcpp::Elf_sizes<size>::shdr_size);
560 oehdr.put_e_shnum(0);
562 unsigned int shstrndx = this->shstrtab_->out_shndx();
563 if (shstrndx < elfcpp::SHN_LORESERVE)
564 oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
566 oehdr.put_e_shstrndx(elfcpp::SHN_XINDEX);
568 // Let the target adjust the ELF header, e.g., to set EI_OSABI in
569 // the e_ident field.
570 parameters->target().adjust_elf_header(view, ehdr_size);
572 of->write_output_view(0, ehdr_size, view);
575 // Return the value to use for the entry address. THIS->ENTRY_ is the
576 // symbol specified on the command line, if any.
579 typename elfcpp::Elf_types<size>::Elf_Addr
580 Output_file_header::entry()
582 const bool should_issue_warning = (this->entry_ != NULL
583 && !parameters->options().relocatable()
584 && !parameters->options().shared());
586 // FIXME: Need to support target specific entry symbol.
587 const char* entry = this->entry_;
591 Symbol* sym = this->symtab_->lookup(entry);
593 typename Sized_symbol<size>::Value_type v;
596 Sized_symbol<size>* ssym;
597 ssym = this->symtab_->get_sized_symbol<size>(sym);
598 if (!ssym->is_defined() && should_issue_warning)
599 gold_warning("entry symbol '%s' exists but is not defined", entry);
604 // We couldn't find the entry symbol. See if we can parse it as
605 // a number. This supports, e.g., -e 0x1000.
607 v = strtoull(entry, &endptr, 0);
610 if (should_issue_warning)
611 gold_warning("cannot find entry symbol '%s'", entry);
619 // Compute the current data size.
622 Output_file_header::do_size() const
624 const int size = parameters->target().get_size();
626 return elfcpp::Elf_sizes<32>::ehdr_size;
628 return elfcpp::Elf_sizes<64>::ehdr_size;
633 // Output_data_const methods.
636 Output_data_const::do_write(Output_file* of)
638 of->write(this->offset(), this->data_.data(), this->data_.size());
641 // Output_data_const_buffer methods.
644 Output_data_const_buffer::do_write(Output_file* of)
646 of->write(this->offset(), this->p_, this->data_size());
649 // Output_section_data methods.
651 // Record the output section, and set the entry size and such.
654 Output_section_data::set_output_section(Output_section* os)
656 gold_assert(this->output_section_ == NULL);
657 this->output_section_ = os;
658 this->do_adjust_output_section(os);
661 // Return the section index of the output section.
664 Output_section_data::do_out_shndx() const
666 gold_assert(this->output_section_ != NULL);
667 return this->output_section_->out_shndx();
670 // Set the alignment, which means we may need to update the alignment
671 // of the output section.
674 Output_section_data::set_addralign(uint64_t addralign)
676 this->addralign_ = addralign;
677 if (this->output_section_ != NULL
678 && this->output_section_->addralign() < addralign)
679 this->output_section_->set_addralign(addralign);
682 // Output_data_strtab methods.
684 // Set the final data size.
687 Output_data_strtab::set_final_data_size()
689 this->strtab_->set_string_offsets();
690 this->set_data_size(this->strtab_->get_strtab_size());
693 // Write out a string table.
696 Output_data_strtab::do_write(Output_file* of)
698 this->strtab_->write(of, this->offset());
701 // Output_reloc methods.
703 // A reloc against a global symbol.
705 template<bool dynamic, int size, bool big_endian>
706 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
713 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
714 is_relative_(is_relative), is_symbolless_(is_symbolless),
715 is_section_symbol_(false), shndx_(INVALID_CODE)
717 // this->type_ is a bitfield; make sure TYPE fits.
718 gold_assert(this->type_ == type);
719 this->u1_.gsym = gsym;
722 this->set_needs_dynsym_index();
725 template<bool dynamic, int size, bool big_endian>
726 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
729 Sized_relobj<size, big_endian>* relobj,
734 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
735 is_relative_(is_relative), is_symbolless_(is_symbolless),
736 is_section_symbol_(false), shndx_(shndx)
738 gold_assert(shndx != INVALID_CODE);
739 // this->type_ is a bitfield; make sure TYPE fits.
740 gold_assert(this->type_ == type);
741 this->u1_.gsym = gsym;
742 this->u2_.relobj = relobj;
744 this->set_needs_dynsym_index();
747 // A reloc against a local symbol.
749 template<bool dynamic, int size, bool big_endian>
750 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
751 Sized_relobj<size, big_endian>* relobj,
752 unsigned int local_sym_index,
758 bool is_section_symbol)
759 : address_(address), local_sym_index_(local_sym_index), type_(type),
760 is_relative_(is_relative), is_symbolless_(is_symbolless),
761 is_section_symbol_(is_section_symbol), shndx_(INVALID_CODE)
763 gold_assert(local_sym_index != GSYM_CODE
764 && local_sym_index != INVALID_CODE);
765 // this->type_ is a bitfield; make sure TYPE fits.
766 gold_assert(this->type_ == type);
767 this->u1_.relobj = relobj;
770 this->set_needs_dynsym_index();
773 template<bool dynamic, int size, bool big_endian>
774 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
775 Sized_relobj<size, big_endian>* relobj,
776 unsigned int local_sym_index,
782 bool is_section_symbol)
783 : address_(address), local_sym_index_(local_sym_index), type_(type),
784 is_relative_(is_relative), is_symbolless_(is_symbolless),
785 is_section_symbol_(is_section_symbol), shndx_(shndx)
787 gold_assert(local_sym_index != GSYM_CODE
788 && local_sym_index != INVALID_CODE);
789 gold_assert(shndx != INVALID_CODE);
790 // this->type_ is a bitfield; make sure TYPE fits.
791 gold_assert(this->type_ == type);
792 this->u1_.relobj = relobj;
793 this->u2_.relobj = relobj;
795 this->set_needs_dynsym_index();
798 // A reloc against the STT_SECTION symbol of an output section.
800 template<bool dynamic, int size, bool big_endian>
801 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
806 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
807 is_relative_(false), is_symbolless_(false),
808 is_section_symbol_(true), shndx_(INVALID_CODE)
810 // this->type_ is a bitfield; make sure TYPE fits.
811 gold_assert(this->type_ == type);
815 this->set_needs_dynsym_index();
817 os->set_needs_symtab_index();
820 template<bool dynamic, int size, bool big_endian>
821 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
824 Sized_relobj<size, big_endian>* relobj,
827 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
828 is_relative_(false), is_symbolless_(false),
829 is_section_symbol_(true), shndx_(shndx)
831 gold_assert(shndx != INVALID_CODE);
832 // this->type_ is a bitfield; make sure TYPE fits.
833 gold_assert(this->type_ == type);
835 this->u2_.relobj = relobj;
837 this->set_needs_dynsym_index();
839 os->set_needs_symtab_index();
842 // An absolute relocation.
844 template<bool dynamic, int size, bool big_endian>
845 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
849 : address_(address), local_sym_index_(0), type_(type),
850 is_relative_(false), is_symbolless_(false),
851 is_section_symbol_(false), shndx_(INVALID_CODE)
853 // this->type_ is a bitfield; make sure TYPE fits.
854 gold_assert(this->type_ == type);
855 this->u1_.relobj = NULL;
859 template<bool dynamic, int size, bool big_endian>
860 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
862 Sized_relobj<size, big_endian>* relobj,
865 : address_(address), local_sym_index_(0), type_(type),
866 is_relative_(false), is_symbolless_(false),
867 is_section_symbol_(false), shndx_(shndx)
869 gold_assert(shndx != INVALID_CODE);
870 // this->type_ is a bitfield; make sure TYPE fits.
871 gold_assert(this->type_ == type);
872 this->u1_.relobj = NULL;
873 this->u2_.relobj = relobj;
876 // A target specific relocation.
878 template<bool dynamic, int size, bool big_endian>
879 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
884 : address_(address), local_sym_index_(TARGET_CODE), type_(type),
885 is_relative_(false), is_symbolless_(false),
886 is_section_symbol_(false), shndx_(INVALID_CODE)
888 // this->type_ is a bitfield; make sure TYPE fits.
889 gold_assert(this->type_ == type);
894 template<bool dynamic, int size, bool big_endian>
895 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
898 Sized_relobj<size, big_endian>* relobj,
901 : address_(address), local_sym_index_(TARGET_CODE), type_(type),
902 is_relative_(false), is_symbolless_(false),
903 is_section_symbol_(false), shndx_(shndx)
905 gold_assert(shndx != INVALID_CODE);
906 // this->type_ is a bitfield; make sure TYPE fits.
907 gold_assert(this->type_ == type);
909 this->u2_.relobj = relobj;
912 // Record that we need a dynamic symbol index for this relocation.
914 template<bool dynamic, int size, bool big_endian>
916 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
917 set_needs_dynsym_index()
919 if (this->is_symbolless_)
921 switch (this->local_sym_index_)
927 this->u1_.gsym->set_needs_dynsym_entry();
931 this->u1_.os->set_needs_dynsym_index();
935 // The target must take care of this if necessary.
943 const unsigned int lsi = this->local_sym_index_;
944 if (!this->is_section_symbol_)
945 this->u1_.relobj->set_needs_output_dynsym_entry(lsi);
947 this->u1_.relobj->output_section(lsi)->set_needs_dynsym_index();
953 // Get the symbol index of a relocation.
955 template<bool dynamic, int size, bool big_endian>
957 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
961 if (this->is_symbolless_)
963 switch (this->local_sym_index_)
969 if (this->u1_.gsym == NULL)
972 index = this->u1_.gsym->dynsym_index();
974 index = this->u1_.gsym->symtab_index();
979 index = this->u1_.os->dynsym_index();
981 index = this->u1_.os->symtab_index();
985 index = parameters->target().reloc_symbol_index(this->u1_.arg,
990 // Relocations without symbols use a symbol index of 0.
996 const unsigned int lsi = this->local_sym_index_;
997 if (!this->is_section_symbol_)
1000 index = this->u1_.relobj->dynsym_index(lsi);
1002 index = this->u1_.relobj->symtab_index(lsi);
1006 Output_section* os = this->u1_.relobj->output_section(lsi);
1007 gold_assert(os != NULL);
1009 index = os->dynsym_index();
1011 index = os->symtab_index();
1016 gold_assert(index != -1U);
1020 // For a local section symbol, get the address of the offset ADDEND
1021 // within the input section.
1023 template<bool dynamic, int size, bool big_endian>
1024 typename elfcpp::Elf_types<size>::Elf_Addr
1025 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
1026 local_section_offset(Addend addend) const
1028 gold_assert(this->local_sym_index_ != GSYM_CODE
1029 && this->local_sym_index_ != SECTION_CODE
1030 && this->local_sym_index_ != TARGET_CODE
1031 && this->local_sym_index_ != INVALID_CODE
1032 && this->local_sym_index_ != 0
1033 && this->is_section_symbol_);
1034 const unsigned int lsi = this->local_sym_index_;
1035 Output_section* os = this->u1_.relobj->output_section(lsi);
1036 gold_assert(os != NULL);
1037 Address offset = this->u1_.relobj->get_output_section_offset(lsi);
1038 if (offset != invalid_address)
1039 return offset + addend;
1040 // This is a merge section.
1041 offset = os->output_address(this->u1_.relobj, lsi, addend);
1042 gold_assert(offset != invalid_address);
1046 // Get the output address of a relocation.
1048 template<bool dynamic, int size, bool big_endian>
1049 typename elfcpp::Elf_types<size>::Elf_Addr
1050 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_address() const
1052 Address address = this->address_;
1053 if (this->shndx_ != INVALID_CODE)
1055 Output_section* os = this->u2_.relobj->output_section(this->shndx_);
1056 gold_assert(os != NULL);
1057 Address off = this->u2_.relobj->get_output_section_offset(this->shndx_);
1058 if (off != invalid_address)
1059 address += os->address() + off;
1062 address = os->output_address(this->u2_.relobj, this->shndx_,
1064 gold_assert(address != invalid_address);
1067 else if (this->u2_.od != NULL)
1068 address += this->u2_.od->address();
1072 // Write out the offset and info fields of a Rel or Rela relocation
1075 template<bool dynamic, int size, bool big_endian>
1076 template<typename Write_rel>
1078 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
1079 Write_rel* wr) const
1081 wr->put_r_offset(this->get_address());
1082 unsigned int sym_index = this->get_symbol_index();
1083 wr->put_r_info(elfcpp::elf_r_info<size>(sym_index, this->type_));
1086 // Write out a Rel relocation.
1088 template<bool dynamic, int size, bool big_endian>
1090 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
1091 unsigned char* pov) const
1093 elfcpp::Rel_write<size, big_endian> orel(pov);
1094 this->write_rel(&orel);
1097 // Get the value of the symbol referred to by a Rel relocation.
1099 template<bool dynamic, int size, bool big_endian>
1100 typename elfcpp::Elf_types<size>::Elf_Addr
1101 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value(
1102 Addend addend) const
1104 if (this->local_sym_index_ == GSYM_CODE)
1106 const Sized_symbol<size>* sym;
1107 sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym);
1108 return sym->value() + addend;
1110 gold_assert(this->local_sym_index_ != SECTION_CODE
1111 && this->local_sym_index_ != TARGET_CODE
1112 && this->local_sym_index_ != INVALID_CODE
1113 && this->local_sym_index_ != 0
1114 && !this->is_section_symbol_);
1115 const unsigned int lsi = this->local_sym_index_;
1116 const Symbol_value<size>* symval = this->u1_.relobj->local_symbol(lsi);
1117 return symval->value(this->u1_.relobj, addend);
1120 // Reloc comparison. This function sorts the dynamic relocs for the
1121 // benefit of the dynamic linker. First we sort all relative relocs
1122 // to the front. Among relative relocs, we sort by output address.
1123 // Among non-relative relocs, we sort by symbol index, then by output
1126 template<bool dynamic, int size, bool big_endian>
1128 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
1129 compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
1132 if (this->is_relative_)
1134 if (!r2.is_relative_)
1136 // Otherwise sort by reloc address below.
1138 else if (r2.is_relative_)
1142 unsigned int sym1 = this->get_symbol_index();
1143 unsigned int sym2 = r2.get_symbol_index();
1146 else if (sym1 > sym2)
1148 // Otherwise sort by reloc address.
1151 section_offset_type addr1 = this->get_address();
1152 section_offset_type addr2 = r2.get_address();
1155 else if (addr1 > addr2)
1158 // Final tie breaker, in order to generate the same output on any
1159 // host: reloc type.
1160 unsigned int type1 = this->type_;
1161 unsigned int type2 = r2.type_;
1164 else if (type1 > type2)
1167 // These relocs appear to be exactly the same.
1171 // Write out a Rela relocation.
1173 template<bool dynamic, int size, bool big_endian>
1175 Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
1176 unsigned char* pov) const
1178 elfcpp::Rela_write<size, big_endian> orel(pov);
1179 this->rel_.write_rel(&orel);
1180 Addend addend = this->addend_;
1181 if (this->rel_.is_target_specific())
1182 addend = parameters->target().reloc_addend(this->rel_.target_arg(),
1183 this->rel_.type(), addend);
1184 else if (this->rel_.is_symbolless())
1185 addend = this->rel_.symbol_value(addend);
1186 else if (this->rel_.is_local_section_symbol())
1187 addend = this->rel_.local_section_offset(addend);
1188 orel.put_r_addend(addend);
1191 // Output_data_reloc_base methods.
1193 // Adjust the output section.
1195 template<int sh_type, bool dynamic, int size, bool big_endian>
1197 Output_data_reloc_base<sh_type, dynamic, size, big_endian>
1198 ::do_adjust_output_section(Output_section* os)
1200 if (sh_type == elfcpp::SHT_REL)
1201 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
1202 else if (sh_type == elfcpp::SHT_RELA)
1203 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
1207 // A STT_GNU_IFUNC symbol may require a IRELATIVE reloc when doing a
1208 // static link. The backends will generate a dynamic reloc section
1209 // to hold this. In that case we don't want to link to the dynsym
1210 // section, because there isn't one.
1212 os->set_should_link_to_symtab();
1213 else if (parameters->doing_static_link())
1216 os->set_should_link_to_dynsym();
1219 // Write out relocation data.
1221 template<int sh_type, bool dynamic, int size, bool big_endian>
1223 Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
1226 const off_t off = this->offset();
1227 const off_t oview_size = this->data_size();
1228 unsigned char* const oview = of->get_output_view(off, oview_size);
1230 if (this->sort_relocs())
1232 gold_assert(dynamic);
1233 std::sort(this->relocs_.begin(), this->relocs_.end(),
1234 Sort_relocs_comparison());
1237 unsigned char* pov = oview;
1238 for (typename Relocs::const_iterator p = this->relocs_.begin();
1239 p != this->relocs_.end();
1246 gold_assert(pov - oview == oview_size);
1248 of->write_output_view(off, oview_size, oview);
1250 // We no longer need the relocation entries.
1251 this->relocs_.clear();
1254 // Class Output_relocatable_relocs.
1256 template<int sh_type, int size, bool big_endian>
1258 Output_relocatable_relocs<sh_type, size, big_endian>::set_final_data_size()
1260 this->set_data_size(this->rr_->output_reloc_count()
1261 * Reloc_types<sh_type, size, big_endian>::reloc_size);
1264 // class Output_data_group.
1266 template<int size, bool big_endian>
1267 Output_data_group<size, big_endian>::Output_data_group(
1268 Sized_relobj<size, big_endian>* relobj,
1269 section_size_type entry_count,
1270 elfcpp::Elf_Word flags,
1271 std::vector<unsigned int>* input_shndxes)
1272 : Output_section_data(entry_count * 4, 4, false),
1276 this->input_shndxes_.swap(*input_shndxes);
1279 // Write out the section group, which means translating the section
1280 // indexes to apply to the output file.
1282 template<int size, bool big_endian>
1284 Output_data_group<size, big_endian>::do_write(Output_file* of)
1286 const off_t off = this->offset();
1287 const section_size_type oview_size =
1288 convert_to_section_size_type(this->data_size());
1289 unsigned char* const oview = of->get_output_view(off, oview_size);
1291 elfcpp::Elf_Word* contents = reinterpret_cast<elfcpp::Elf_Word*>(oview);
1292 elfcpp::Swap<32, big_endian>::writeval(contents, this->flags_);
1295 for (std::vector<unsigned int>::const_iterator p =
1296 this->input_shndxes_.begin();
1297 p != this->input_shndxes_.end();
1300 Output_section* os = this->relobj_->output_section(*p);
1302 unsigned int output_shndx;
1304 output_shndx = os->out_shndx();
1307 this->relobj_->error(_("section group retained but "
1308 "group element discarded"));
1312 elfcpp::Swap<32, big_endian>::writeval(contents, output_shndx);
1315 size_t wrote = reinterpret_cast<unsigned char*>(contents) - oview;
1316 gold_assert(wrote == oview_size);
1318 of->write_output_view(off, oview_size, oview);
1320 // We no longer need this information.
1321 this->input_shndxes_.clear();
1324 // Output_data_got::Got_entry methods.
1326 // Write out the entry.
1328 template<int size, bool big_endian>
1330 Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
1334 switch (this->local_sym_index_)
1338 // If the symbol is resolved locally, we need to write out the
1339 // link-time value, which will be relocated dynamically by a
1340 // RELATIVE relocation.
1341 Symbol* gsym = this->u_.gsym;
1342 if (this->use_plt_offset_ && gsym->has_plt_offset())
1343 val = (parameters->target().plt_section_for_global(gsym)->address()
1344 + gsym->plt_offset());
1347 Sized_symbol<size>* sgsym;
1348 // This cast is a bit ugly. We don't want to put a
1349 // virtual method in Symbol, because we want Symbol to be
1350 // as small as possible.
1351 sgsym = static_cast<Sized_symbol<size>*>(gsym);
1352 val = sgsym->value();
1358 val = this->u_.constant;
1363 const Sized_relobj<size, big_endian>* object = this->u_.object;
1364 const unsigned int lsi = this->local_sym_index_;
1365 const Symbol_value<size>* symval = object->local_symbol(lsi);
1366 if (!this->use_plt_offset_)
1367 val = symval->value(this->u_.object, 0);
1370 const Output_data* plt =
1371 parameters->target().plt_section_for_local(object, lsi);
1372 val = plt->address() + object->local_plt_offset(lsi);
1378 elfcpp::Swap<size, big_endian>::writeval(pov, val);
1381 // Output_data_got methods.
1383 // Add an entry for a global symbol to the GOT. This returns true if
1384 // this is a new GOT entry, false if the symbol already had a GOT
1387 template<int size, bool big_endian>
1389 Output_data_got<size, big_endian>::add_global(
1391 unsigned int got_type)
1393 if (gsym->has_got_offset(got_type))
1396 this->entries_.push_back(Got_entry(gsym, false));
1397 this->set_got_size();
1398 gsym->set_got_offset(got_type, this->last_got_offset());
1402 // Like add_global, but use the PLT offset.
1404 template<int size, bool big_endian>
1406 Output_data_got<size, big_endian>::add_global_plt(Symbol* gsym,
1407 unsigned int got_type)
1409 if (gsym->has_got_offset(got_type))
1412 this->entries_.push_back(Got_entry(gsym, true));
1413 this->set_got_size();
1414 gsym->set_got_offset(got_type, this->last_got_offset());
1418 // Add an entry for a global symbol to the GOT, and add a dynamic
1419 // relocation of type R_TYPE for the GOT entry.
1421 template<int size, bool big_endian>
1423 Output_data_got<size, big_endian>::add_global_with_rel(
1425 unsigned int got_type,
1427 unsigned int r_type)
1429 if (gsym->has_got_offset(got_type))
1432 this->entries_.push_back(Got_entry());
1433 this->set_got_size();
1434 unsigned int got_offset = this->last_got_offset();
1435 gsym->set_got_offset(got_type, got_offset);
1436 rel_dyn->add_global(gsym, r_type, this, got_offset);
1439 template<int size, bool big_endian>
1441 Output_data_got<size, big_endian>::add_global_with_rela(
1443 unsigned int got_type,
1445 unsigned int r_type)
1447 if (gsym->has_got_offset(got_type))
1450 this->entries_.push_back(Got_entry());
1451 this->set_got_size();
1452 unsigned int got_offset = this->last_got_offset();
1453 gsym->set_got_offset(got_type, got_offset);
1454 rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
1457 // Add a pair of entries for a global symbol to the GOT, and add
1458 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1459 // If R_TYPE_2 == 0, add the second entry with no relocation.
1460 template<int size, bool big_endian>
1462 Output_data_got<size, big_endian>::add_global_pair_with_rel(
1464 unsigned int got_type,
1466 unsigned int r_type_1,
1467 unsigned int r_type_2)
1469 if (gsym->has_got_offset(got_type))
1472 this->entries_.push_back(Got_entry());
1473 unsigned int got_offset = this->last_got_offset();
1474 gsym->set_got_offset(got_type, got_offset);
1475 rel_dyn->add_global(gsym, r_type_1, this, got_offset);
1477 this->entries_.push_back(Got_entry());
1480 got_offset = this->last_got_offset();
1481 rel_dyn->add_global(gsym, r_type_2, this, got_offset);
1484 this->set_got_size();
1487 template<int size, bool big_endian>
1489 Output_data_got<size, big_endian>::add_global_pair_with_rela(
1491 unsigned int got_type,
1493 unsigned int r_type_1,
1494 unsigned int r_type_2)
1496 if (gsym->has_got_offset(got_type))
1499 this->entries_.push_back(Got_entry());
1500 unsigned int got_offset = this->last_got_offset();
1501 gsym->set_got_offset(got_type, got_offset);
1502 rela_dyn->add_global(gsym, r_type_1, this, got_offset, 0);
1504 this->entries_.push_back(Got_entry());
1507 got_offset = this->last_got_offset();
1508 rela_dyn->add_global(gsym, r_type_2, this, got_offset, 0);
1511 this->set_got_size();
1514 // Add an entry for a local symbol to the GOT. This returns true if
1515 // this is a new GOT entry, false if the symbol already has a GOT
1518 template<int size, bool big_endian>
1520 Output_data_got<size, big_endian>::add_local(
1521 Sized_relobj<size, big_endian>* object,
1522 unsigned int symndx,
1523 unsigned int got_type)
1525 if (object->local_has_got_offset(symndx, got_type))
1528 this->entries_.push_back(Got_entry(object, symndx, false));
1529 this->set_got_size();
1530 object->set_local_got_offset(symndx, got_type, this->last_got_offset());
1534 // Like add_local, but use the PLT offset.
1536 template<int size, bool big_endian>
1538 Output_data_got<size, big_endian>::add_local_plt(
1539 Sized_relobj<size, big_endian>* object,
1540 unsigned int symndx,
1541 unsigned int got_type)
1543 if (object->local_has_got_offset(symndx, got_type))
1546 this->entries_.push_back(Got_entry(object, symndx, true));
1547 this->set_got_size();
1548 object->set_local_got_offset(symndx, got_type, this->last_got_offset());
1552 // Add an entry for a local symbol to the GOT, and add a dynamic
1553 // relocation of type R_TYPE for the GOT entry.
1555 template<int size, bool big_endian>
1557 Output_data_got<size, big_endian>::add_local_with_rel(
1558 Sized_relobj<size, big_endian>* object,
1559 unsigned int symndx,
1560 unsigned int got_type,
1562 unsigned int r_type)
1564 if (object->local_has_got_offset(symndx, got_type))
1567 this->entries_.push_back(Got_entry());
1568 this->set_got_size();
1569 unsigned int got_offset = this->last_got_offset();
1570 object->set_local_got_offset(symndx, got_type, got_offset);
1571 rel_dyn->add_local(object, symndx, r_type, this, got_offset);
1574 template<int size, bool big_endian>
1576 Output_data_got<size, big_endian>::add_local_with_rela(
1577 Sized_relobj<size, big_endian>* object,
1578 unsigned int symndx,
1579 unsigned int got_type,
1581 unsigned int r_type)
1583 if (object->local_has_got_offset(symndx, got_type))
1586 this->entries_.push_back(Got_entry());
1587 this->set_got_size();
1588 unsigned int got_offset = this->last_got_offset();
1589 object->set_local_got_offset(symndx, got_type, got_offset);
1590 rela_dyn->add_local(object, symndx, r_type, this, got_offset, 0);
1593 // Add a pair of entries for a local symbol to the GOT, and add
1594 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1595 // If R_TYPE_2 == 0, add the second entry with no relocation.
1596 template<int size, bool big_endian>
1598 Output_data_got<size, big_endian>::add_local_pair_with_rel(
1599 Sized_relobj<size, big_endian>* object,
1600 unsigned int symndx,
1602 unsigned int got_type,
1604 unsigned int r_type_1,
1605 unsigned int r_type_2)
1607 if (object->local_has_got_offset(symndx, got_type))
1610 this->entries_.push_back(Got_entry());
1611 unsigned int got_offset = this->last_got_offset();
1612 object->set_local_got_offset(symndx, got_type, got_offset);
1613 Output_section* os = object->output_section(shndx);
1614 rel_dyn->add_output_section(os, r_type_1, this, got_offset);
1616 this->entries_.push_back(Got_entry(object, symndx, false));
1619 got_offset = this->last_got_offset();
1620 rel_dyn->add_output_section(os, r_type_2, this, got_offset);
1623 this->set_got_size();
1626 template<int size, bool big_endian>
1628 Output_data_got<size, big_endian>::add_local_pair_with_rela(
1629 Sized_relobj<size, big_endian>* object,
1630 unsigned int symndx,
1632 unsigned int got_type,
1634 unsigned int r_type_1,
1635 unsigned int r_type_2)
1637 if (object->local_has_got_offset(symndx, got_type))
1640 this->entries_.push_back(Got_entry());
1641 unsigned int got_offset = this->last_got_offset();
1642 object->set_local_got_offset(symndx, got_type, got_offset);
1643 Output_section* os = object->output_section(shndx);
1644 rela_dyn->add_output_section(os, r_type_1, this, got_offset, 0);
1646 this->entries_.push_back(Got_entry(object, symndx, false));
1649 got_offset = this->last_got_offset();
1650 rela_dyn->add_output_section(os, r_type_2, this, got_offset, 0);
1653 this->set_got_size();
1656 // Write out the GOT.
1658 template<int size, bool big_endian>
1660 Output_data_got<size, big_endian>::do_write(Output_file* of)
1662 const int add = size / 8;
1664 const off_t off = this->offset();
1665 const off_t oview_size = this->data_size();
1666 unsigned char* const oview = of->get_output_view(off, oview_size);
1668 unsigned char* pov = oview;
1669 for (typename Got_entries::const_iterator p = this->entries_.begin();
1670 p != this->entries_.end();
1677 gold_assert(pov - oview == oview_size);
1679 of->write_output_view(off, oview_size, oview);
1681 // We no longer need the GOT entries.
1682 this->entries_.clear();
1685 // Output_data_dynamic::Dynamic_entry methods.
1687 // Write out the entry.
1689 template<int size, bool big_endian>
1691 Output_data_dynamic::Dynamic_entry::write(
1693 const Stringpool* pool) const
1695 typename elfcpp::Elf_types<size>::Elf_WXword val;
1696 switch (this->offset_)
1698 case DYNAMIC_NUMBER:
1702 case DYNAMIC_SECTION_SIZE:
1703 val = this->u_.od->data_size();
1704 if (this->od2 != NULL)
1705 val += this->od2->data_size();
1708 case DYNAMIC_SYMBOL:
1710 const Sized_symbol<size>* s =
1711 static_cast<const Sized_symbol<size>*>(this->u_.sym);
1716 case DYNAMIC_STRING:
1717 val = pool->get_offset(this->u_.str);
1721 val = this->u_.od->address() + this->offset_;
1725 elfcpp::Dyn_write<size, big_endian> dw(pov);
1726 dw.put_d_tag(this->tag_);
1730 // Output_data_dynamic methods.
1732 // Adjust the output section to set the entry size.
1735 Output_data_dynamic::do_adjust_output_section(Output_section* os)
1737 if (parameters->target().get_size() == 32)
1738 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
1739 else if (parameters->target().get_size() == 64)
1740 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1745 // Set the final data size.
1748 Output_data_dynamic::set_final_data_size()
1750 // Add the terminating entry if it hasn't been added.
1751 // Because of relaxation, we can run this multiple times.
1752 if (this->entries_.empty() || this->entries_.back().tag() != elfcpp::DT_NULL)
1754 int extra = parameters->options().spare_dynamic_tags();
1755 for (int i = 0; i < extra; ++i)
1756 this->add_constant(elfcpp::DT_NULL, 0);
1757 this->add_constant(elfcpp::DT_NULL, 0);
1761 if (parameters->target().get_size() == 32)
1762 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
1763 else if (parameters->target().get_size() == 64)
1764 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1767 this->set_data_size(this->entries_.size() * dyn_size);
1770 // Write out the dynamic entries.
1773 Output_data_dynamic::do_write(Output_file* of)
1775 switch (parameters->size_and_endianness())
1777 #ifdef HAVE_TARGET_32_LITTLE
1778 case Parameters::TARGET_32_LITTLE:
1779 this->sized_write<32, false>(of);
1782 #ifdef HAVE_TARGET_32_BIG
1783 case Parameters::TARGET_32_BIG:
1784 this->sized_write<32, true>(of);
1787 #ifdef HAVE_TARGET_64_LITTLE
1788 case Parameters::TARGET_64_LITTLE:
1789 this->sized_write<64, false>(of);
1792 #ifdef HAVE_TARGET_64_BIG
1793 case Parameters::TARGET_64_BIG:
1794 this->sized_write<64, true>(of);
1802 template<int size, bool big_endian>
1804 Output_data_dynamic::sized_write(Output_file* of)
1806 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1808 const off_t offset = this->offset();
1809 const off_t oview_size = this->data_size();
1810 unsigned char* const oview = of->get_output_view(offset, oview_size);
1812 unsigned char* pov = oview;
1813 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1814 p != this->entries_.end();
1817 p->write<size, big_endian>(pov, this->pool_);
1821 gold_assert(pov - oview == oview_size);
1823 of->write_output_view(offset, oview_size, oview);
1825 // We no longer need the dynamic entries.
1826 this->entries_.clear();
1829 // Class Output_symtab_xindex.
1832 Output_symtab_xindex::do_write(Output_file* of)
1834 const off_t offset = this->offset();
1835 const off_t oview_size = this->data_size();
1836 unsigned char* const oview = of->get_output_view(offset, oview_size);
1838 memset(oview, 0, oview_size);
1840 if (parameters->target().is_big_endian())
1841 this->endian_do_write<true>(oview);
1843 this->endian_do_write<false>(oview);
1845 of->write_output_view(offset, oview_size, oview);
1847 // We no longer need the data.
1848 this->entries_.clear();
1851 template<bool big_endian>
1853 Output_symtab_xindex::endian_do_write(unsigned char* const oview)
1855 for (Xindex_entries::const_iterator p = this->entries_.begin();
1856 p != this->entries_.end();
1859 unsigned int symndx = p->first;
1860 gold_assert(symndx * 4 < this->data_size());
1861 elfcpp::Swap<32, big_endian>::writeval(oview + symndx * 4, p->second);
1865 // Output_section::Input_section methods.
1867 // Return the current data size. For an input section we store the size here.
1868 // For an Output_section_data, we have to ask it for the size.
1871 Output_section::Input_section::current_data_size() const
1873 if (this->is_input_section())
1874 return this->u1_.data_size;
1877 this->u2_.posd->pre_finalize_data_size();
1878 return this->u2_.posd->current_data_size();
1882 // Return the data size. For an input section we store the size here.
1883 // For an Output_section_data, we have to ask it for the size.
1886 Output_section::Input_section::data_size() const
1888 if (this->is_input_section())
1889 return this->u1_.data_size;
1891 return this->u2_.posd->data_size();
1894 // Return the object for an input section.
1897 Output_section::Input_section::relobj() const
1899 if (this->is_input_section())
1900 return this->u2_.object;
1901 else if (this->is_merge_section())
1903 gold_assert(this->u2_.pomb->first_relobj() != NULL);
1904 return this->u2_.pomb->first_relobj();
1906 else if (this->is_relaxed_input_section())
1907 return this->u2_.poris->relobj();
1912 // Return the input section index for an input section.
1915 Output_section::Input_section::shndx() const
1917 if (this->is_input_section())
1918 return this->shndx_;
1919 else if (this->is_merge_section())
1921 gold_assert(this->u2_.pomb->first_relobj() != NULL);
1922 return this->u2_.pomb->first_shndx();
1924 else if (this->is_relaxed_input_section())
1925 return this->u2_.poris->shndx();
1930 // Set the address and file offset.
1933 Output_section::Input_section::set_address_and_file_offset(
1936 off_t section_file_offset)
1938 if (this->is_input_section())
1939 this->u2_.object->set_section_offset(this->shndx_,
1940 file_offset - section_file_offset);
1942 this->u2_.posd->set_address_and_file_offset(address, file_offset);
1945 // Reset the address and file offset.
1948 Output_section::Input_section::reset_address_and_file_offset()
1950 if (!this->is_input_section())
1951 this->u2_.posd->reset_address_and_file_offset();
1954 // Finalize the data size.
1957 Output_section::Input_section::finalize_data_size()
1959 if (!this->is_input_section())
1960 this->u2_.posd->finalize_data_size();
1963 // Try to turn an input offset into an output offset. We want to
1964 // return the output offset relative to the start of this
1965 // Input_section in the output section.
1968 Output_section::Input_section::output_offset(
1969 const Relobj* object,
1971 section_offset_type offset,
1972 section_offset_type* poutput) const
1974 if (!this->is_input_section())
1975 return this->u2_.posd->output_offset(object, shndx, offset, poutput);
1978 if (this->shndx_ != shndx || this->u2_.object != object)
1985 // Return whether this is the merge section for the input section
1989 Output_section::Input_section::is_merge_section_for(const Relobj* object,
1990 unsigned int shndx) const
1992 if (this->is_input_section())
1994 return this->u2_.posd->is_merge_section_for(object, shndx);
1997 // Write out the data. We don't have to do anything for an input
1998 // section--they are handled via Object::relocate--but this is where
1999 // we write out the data for an Output_section_data.
2002 Output_section::Input_section::write(Output_file* of)
2004 if (!this->is_input_section())
2005 this->u2_.posd->write(of);
2008 // Write the data to a buffer. As for write(), we don't have to do
2009 // anything for an input section.
2012 Output_section::Input_section::write_to_buffer(unsigned char* buffer)
2014 if (!this->is_input_section())
2015 this->u2_.posd->write_to_buffer(buffer);
2018 // Print to a map file.
2021 Output_section::Input_section::print_to_mapfile(Mapfile* mapfile) const
2023 switch (this->shndx_)
2025 case OUTPUT_SECTION_CODE:
2026 case MERGE_DATA_SECTION_CODE:
2027 case MERGE_STRING_SECTION_CODE:
2028 this->u2_.posd->print_to_mapfile(mapfile);
2031 case RELAXED_INPUT_SECTION_CODE:
2033 Output_relaxed_input_section* relaxed_section =
2034 this->relaxed_input_section();
2035 mapfile->print_input_section(relaxed_section->relobj(),
2036 relaxed_section->shndx());
2040 mapfile->print_input_section(this->u2_.object, this->shndx_);
2045 // Output_section methods.
2047 // Construct an Output_section. NAME will point into a Stringpool.
2049 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
2050 elfcpp::Elf_Xword flags)
2055 link_section_(NULL),
2057 info_section_(NULL),
2062 order_(ORDER_INVALID),
2067 first_input_offset_(0),
2069 postprocessing_buffer_(NULL),
2070 needs_symtab_index_(false),
2071 needs_dynsym_index_(false),
2072 should_link_to_symtab_(false),
2073 should_link_to_dynsym_(false),
2074 after_input_sections_(false),
2075 requires_postprocessing_(false),
2076 found_in_sections_clause_(false),
2077 has_load_address_(false),
2078 info_uses_section_index_(false),
2079 input_section_order_specified_(false),
2080 may_sort_attached_input_sections_(false),
2081 must_sort_attached_input_sections_(false),
2082 attached_input_sections_are_sorted_(false),
2084 is_small_section_(false),
2085 is_large_section_(false),
2086 generate_code_fills_at_write_(false),
2087 is_entsize_zero_(false),
2088 section_offsets_need_adjustment_(false),
2090 always_keeps_input_sections_(false),
2091 has_fixed_layout_(false),
2094 lookup_maps_(new Output_section_lookup_maps),
2097 // An unallocated section has no address. Forcing this means that
2098 // we don't need special treatment for symbols defined in debug
2100 if ((flags & elfcpp::SHF_ALLOC) == 0)
2101 this->set_address(0);
2104 Output_section::~Output_section()
2106 delete this->checkpoint_;
2109 // Set the entry size.
2112 Output_section::set_entsize(uint64_t v)
2114 if (this->is_entsize_zero_)
2116 else if (this->entsize_ == 0)
2118 else if (this->entsize_ != v)
2121 this->is_entsize_zero_ = 1;
2125 // Add the input section SHNDX, with header SHDR, named SECNAME, in
2126 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
2127 // relocation section which applies to this section, or 0 if none, or
2128 // -1U if more than one. Return the offset of the input section
2129 // within the output section. Return -1 if the input section will
2130 // receive special handling. In the normal case we don't always keep
2131 // track of input sections for an Output_section. Instead, each
2132 // Object keeps track of the Output_section for each of its input
2133 // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
2134 // track of input sections here; this is used when SECTIONS appears in
2137 template<int size, bool big_endian>
2139 Output_section::add_input_section(Layout* layout,
2140 Sized_relobj<size, big_endian>* object,
2142 const char* secname,
2143 const elfcpp::Shdr<size, big_endian>& shdr,
2144 unsigned int reloc_shndx,
2145 bool have_sections_script)
2147 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
2148 if ((addralign & (addralign - 1)) != 0)
2150 object->error(_("invalid alignment %lu for section \"%s\""),
2151 static_cast<unsigned long>(addralign), secname);
2155 if (addralign > this->addralign_)
2156 this->addralign_ = addralign;
2158 typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
2159 uint64_t entsize = shdr.get_sh_entsize();
2161 // .debug_str is a mergeable string section, but is not always so
2162 // marked by compilers. Mark manually here so we can optimize.
2163 if (strcmp(secname, ".debug_str") == 0)
2165 sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
2169 this->update_flags_for_input_section(sh_flags);
2170 this->set_entsize(entsize);
2172 // If this is a SHF_MERGE section, we pass all the input sections to
2173 // a Output_data_merge. We don't try to handle relocations for such
2174 // a section. We don't try to handle empty merge sections--they
2175 // mess up the mappings, and are useless anyhow.
2176 // FIXME: Need to handle merge sections during incremental update.
2177 if ((sh_flags & elfcpp::SHF_MERGE) != 0
2179 && shdr.get_sh_size() > 0
2180 && !parameters->incremental())
2182 // Keep information about merged input sections for rebuilding fast
2183 // lookup maps if we have sections-script or we do relaxation.
2184 bool keeps_input_sections = (this->always_keeps_input_sections_
2185 || have_sections_script
2186 || parameters->target().may_relax());
2188 if (this->add_merge_input_section(object, shndx, sh_flags, entsize,
2189 addralign, keeps_input_sections))
2191 // Tell the relocation routines that they need to call the
2192 // output_offset method to determine the final address.
2197 section_size_type input_section_size = shdr.get_sh_size();
2198 section_size_type uncompressed_size;
2199 if (object->section_is_compressed(shndx, &uncompressed_size))
2200 input_section_size = uncompressed_size;
2202 off_t offset_in_section;
2203 off_t aligned_offset_in_section;
2204 if (this->has_fixed_layout())
2206 // For incremental updates, find a chunk of unused space in the section.
2207 offset_in_section = this->free_list_.allocate(input_section_size,
2209 if (offset_in_section == -1)
2210 gold_fatal(_("out of patch space; relink with --incremental-full"));
2211 aligned_offset_in_section = offset_in_section;
2215 offset_in_section = this->current_data_size_for_child();
2216 aligned_offset_in_section = align_address(offset_in_section,
2218 this->set_current_data_size_for_child(aligned_offset_in_section
2219 + input_section_size);
2222 // Determine if we want to delay code-fill generation until the output
2223 // section is written. When the target is relaxing, we want to delay fill
2224 // generating to avoid adjusting them during relaxation. Also, if we are
2225 // sorting input sections we must delay fill generation.
2226 if (!this->generate_code_fills_at_write_
2227 && !have_sections_script
2228 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
2229 && parameters->target().has_code_fill()
2230 && (parameters->target().may_relax()
2231 || parameters->options().section_ordering_file()))
2233 gold_assert(this->fills_.empty());
2234 this->generate_code_fills_at_write_ = true;
2237 if (aligned_offset_in_section > offset_in_section
2238 && !this->generate_code_fills_at_write_
2239 && !have_sections_script
2240 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
2241 && parameters->target().has_code_fill())
2243 // We need to add some fill data. Using fill_list_ when
2244 // possible is an optimization, since we will often have fill
2245 // sections without input sections.
2246 off_t fill_len = aligned_offset_in_section - offset_in_section;
2247 if (this->input_sections_.empty())
2248 this->fills_.push_back(Fill(offset_in_section, fill_len));
2251 std::string fill_data(parameters->target().code_fill(fill_len));
2252 Output_data_const* odc = new Output_data_const(fill_data, 1);
2253 this->input_sections_.push_back(Input_section(odc));
2257 // We need to keep track of this section if we are already keeping
2258 // track of sections, or if we are relaxing. Also, if this is a
2259 // section which requires sorting, or which may require sorting in
2260 // the future, we keep track of the sections. If the
2261 // --section-ordering-file option is used to specify the order of
2262 // sections, we need to keep track of sections.
2263 if (this->always_keeps_input_sections_
2264 || have_sections_script
2265 || !this->input_sections_.empty()
2266 || this->may_sort_attached_input_sections()
2267 || this->must_sort_attached_input_sections()
2268 || parameters->options().user_set_Map()
2269 || parameters->target().may_relax()
2270 || parameters->options().section_ordering_file())
2272 Input_section isecn(object, shndx, input_section_size, addralign);
2273 if (parameters->options().section_ordering_file())
2275 unsigned int section_order_index =
2276 layout->find_section_order_index(std::string(secname));
2277 if (section_order_index != 0)
2279 isecn.set_section_order_index(section_order_index);
2280 this->set_input_section_order_specified();
2283 if (this->has_fixed_layout())
2285 // For incremental updates, finalize the address and offset now.
2286 uint64_t addr = this->address();
2287 isecn.set_address_and_file_offset(addr + aligned_offset_in_section,
2288 aligned_offset_in_section,
2291 this->input_sections_.push_back(isecn);
2294 return aligned_offset_in_section;
2297 // Add arbitrary data to an output section.
2300 Output_section::add_output_section_data(Output_section_data* posd)
2302 Input_section inp(posd);
2303 this->add_output_section_data(&inp);
2305 if (posd->is_data_size_valid())
2307 off_t offset_in_section;
2308 if (this->has_fixed_layout())
2310 // For incremental updates, find a chunk of unused space.
2311 offset_in_section = this->free_list_.allocate(posd->data_size(),
2312 posd->addralign(), 0);
2313 if (offset_in_section == -1)
2314 gold_fatal(_("out of patch space; relink with --incremental-full"));
2315 // Finalize the address and offset now.
2316 uint64_t addr = this->address();
2317 off_t offset = this->offset();
2318 posd->set_address_and_file_offset(addr + offset_in_section,
2319 offset + offset_in_section);
2323 offset_in_section = this->current_data_size_for_child();
2324 off_t aligned_offset_in_section = align_address(offset_in_section,
2326 this->set_current_data_size_for_child(aligned_offset_in_section
2327 + posd->data_size());
2330 else if (this->has_fixed_layout())
2332 // For incremental updates, arrange for the data to have a fixed layout.
2333 // This will mean that additions to the data must be allocated from
2334 // free space within the containing output section.
2335 uint64_t addr = this->address();
2336 posd->set_address(addr);
2337 posd->set_file_offset(0);
2338 // FIXME: Mark *POSD as part of a fixed-layout section.
2342 // Add a relaxed input section.
2345 Output_section::add_relaxed_input_section(Layout* layout,
2346 Output_relaxed_input_section* poris,
2347 const std::string& name)
2349 Input_section inp(poris);
2351 // If the --section-ordering-file option is used to specify the order of
2352 // sections, we need to keep track of sections.
2353 if (parameters->options().section_ordering_file())
2355 unsigned int section_order_index =
2356 layout->find_section_order_index(name);
2357 if (section_order_index != 0)
2359 inp.set_section_order_index(section_order_index);
2360 this->set_input_section_order_specified();
2364 this->add_output_section_data(&inp);
2365 if (this->lookup_maps_->is_valid())
2366 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2367 poris->shndx(), poris);
2369 // For a relaxed section, we use the current data size. Linker scripts
2370 // get all the input sections, including relaxed one from an output
2371 // section and add them back to them same output section to compute the
2372 // output section size. If we do not account for sizes of relaxed input
2373 // sections, an output section would be incorrectly sized.
2374 off_t offset_in_section = this->current_data_size_for_child();
2375 off_t aligned_offset_in_section = align_address(offset_in_section,
2376 poris->addralign());
2377 this->set_current_data_size_for_child(aligned_offset_in_section
2378 + poris->current_data_size());
2381 // Add arbitrary data to an output section by Input_section.
2384 Output_section::add_output_section_data(Input_section* inp)
2386 if (this->input_sections_.empty())
2387 this->first_input_offset_ = this->current_data_size_for_child();
2389 this->input_sections_.push_back(*inp);
2391 uint64_t addralign = inp->addralign();
2392 if (addralign > this->addralign_)
2393 this->addralign_ = addralign;
2395 inp->set_output_section(this);
2398 // Add a merge section to an output section.
2401 Output_section::add_output_merge_section(Output_section_data* posd,
2402 bool is_string, uint64_t entsize)
2404 Input_section inp(posd, is_string, entsize);
2405 this->add_output_section_data(&inp);
2408 // Add an input section to a SHF_MERGE section.
2411 Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
2412 uint64_t flags, uint64_t entsize,
2414 bool keeps_input_sections)
2416 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
2418 // We only merge strings if the alignment is not more than the
2419 // character size. This could be handled, but it's unusual.
2420 if (is_string && addralign > entsize)
2423 // We cannot restore merged input section states.
2424 gold_assert(this->checkpoint_ == NULL);
2426 // Look up merge sections by required properties.
2427 // Currently, we only invalidate the lookup maps in script processing
2428 // and relaxation. We should not have done either when we reach here.
2429 // So we assume that the lookup maps are valid to simply code.
2430 gold_assert(this->lookup_maps_->is_valid());
2431 Merge_section_properties msp(is_string, entsize, addralign);
2432 Output_merge_base* pomb = this->lookup_maps_->find_merge_section(msp);
2433 bool is_new = false;
2436 gold_assert(pomb->is_string() == is_string
2437 && pomb->entsize() == entsize
2438 && pomb->addralign() == addralign);
2442 // Create a new Output_merge_data or Output_merge_string_data.
2444 pomb = new Output_merge_data(entsize, addralign);
2450 pomb = new Output_merge_string<char>(addralign);
2453 pomb = new Output_merge_string<uint16_t>(addralign);
2456 pomb = new Output_merge_string<uint32_t>(addralign);
2462 // If we need to do script processing or relaxation, we need to keep
2463 // the original input sections to rebuild the fast lookup maps.
2464 if (keeps_input_sections)
2465 pomb->set_keeps_input_sections();
2469 if (pomb->add_input_section(object, shndx))
2471 // Add new merge section to this output section and link merge
2472 // section properties to new merge section in map.
2475 this->add_output_merge_section(pomb, is_string, entsize);
2476 this->lookup_maps_->add_merge_section(msp, pomb);
2479 // Add input section to new merge section and link input section to new
2480 // merge section in map.
2481 this->lookup_maps_->add_merge_input_section(object, shndx, pomb);
2486 // If add_input_section failed, delete new merge section to avoid
2487 // exporting empty merge sections in Output_section::get_input_section.
2494 // Build a relaxation map to speed up relaxation of existing input sections.
2495 // Look up to the first LIMIT elements in INPUT_SECTIONS.
2498 Output_section::build_relaxation_map(
2499 const Input_section_list& input_sections,
2501 Relaxation_map* relaxation_map) const
2503 for (size_t i = 0; i < limit; ++i)
2505 const Input_section& is(input_sections[i]);
2506 if (is.is_input_section() || is.is_relaxed_input_section())
2508 Section_id sid(is.relobj(), is.shndx());
2509 (*relaxation_map)[sid] = i;
2514 // Convert regular input sections in INPUT_SECTIONS into relaxed input
2515 // sections in RELAXED_SECTIONS. MAP is a prebuilt map from section id
2516 // indices of INPUT_SECTIONS.
2519 Output_section::convert_input_sections_in_list_to_relaxed_sections(
2520 const std::vector<Output_relaxed_input_section*>& relaxed_sections,
2521 const Relaxation_map& map,
2522 Input_section_list* input_sections)
2524 for (size_t i = 0; i < relaxed_sections.size(); ++i)
2526 Output_relaxed_input_section* poris = relaxed_sections[i];
2527 Section_id sid(poris->relobj(), poris->shndx());
2528 Relaxation_map::const_iterator p = map.find(sid);
2529 gold_assert(p != map.end());
2530 gold_assert((*input_sections)[p->second].is_input_section());
2532 // Remember section order index of original input section
2533 // if it is set. Copy it to the relaxed input section.
2535 (*input_sections)[p->second].section_order_index();
2536 (*input_sections)[p->second] = Input_section(poris);
2537 (*input_sections)[p->second].set_section_order_index(soi);
2541 // Convert regular input sections into relaxed input sections. RELAXED_SECTIONS
2542 // is a vector of pointers to Output_relaxed_input_section or its derived
2543 // classes. The relaxed sections must correspond to existing input sections.
2546 Output_section::convert_input_sections_to_relaxed_sections(
2547 const std::vector<Output_relaxed_input_section*>& relaxed_sections)
2549 gold_assert(parameters->target().may_relax());
2551 // We want to make sure that restore_states does not undo the effect of
2552 // this. If there is no checkpoint active, just search the current
2553 // input section list and replace the sections there. If there is
2554 // a checkpoint, also replace the sections there.
2556 // By default, we look at the whole list.
2557 size_t limit = this->input_sections_.size();
2559 if (this->checkpoint_ != NULL)
2561 // Replace input sections with relaxed input section in the saved
2562 // copy of the input section list.
2563 if (this->checkpoint_->input_sections_saved())
2566 this->build_relaxation_map(
2567 *(this->checkpoint_->input_sections()),
2568 this->checkpoint_->input_sections()->size(),
2570 this->convert_input_sections_in_list_to_relaxed_sections(
2573 this->checkpoint_->input_sections());
2577 // We have not copied the input section list yet. Instead, just
2578 // look at the portion that would be saved.
2579 limit = this->checkpoint_->input_sections_size();
2583 // Convert input sections in input_section_list.
2585 this->build_relaxation_map(this->input_sections_, limit, &map);
2586 this->convert_input_sections_in_list_to_relaxed_sections(
2589 &this->input_sections_);
2591 // Update fast look-up map.
2592 if (this->lookup_maps_->is_valid())
2593 for (size_t i = 0; i < relaxed_sections.size(); ++i)
2595 Output_relaxed_input_section* poris = relaxed_sections[i];
2596 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2597 poris->shndx(), poris);
2601 // Update the output section flags based on input section flags.
2604 Output_section::update_flags_for_input_section(elfcpp::Elf_Xword flags)
2606 // If we created the section with SHF_ALLOC clear, we set the
2607 // address. If we are now setting the SHF_ALLOC flag, we need to
2609 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0
2610 && (flags & elfcpp::SHF_ALLOC) != 0)
2611 this->mark_address_invalid();
2613 this->flags_ |= (flags
2614 & (elfcpp::SHF_WRITE
2616 | elfcpp::SHF_EXECINSTR));
2618 if ((flags & elfcpp::SHF_MERGE) == 0)
2619 this->flags_ &=~ elfcpp::SHF_MERGE;
2622 if (this->current_data_size_for_child() == 0)
2623 this->flags_ |= elfcpp::SHF_MERGE;
2626 if ((flags & elfcpp::SHF_STRINGS) == 0)
2627 this->flags_ &=~ elfcpp::SHF_STRINGS;
2630 if (this->current_data_size_for_child() == 0)
2631 this->flags_ |= elfcpp::SHF_STRINGS;
2635 // Find the merge section into which an input section with index SHNDX in
2636 // OBJECT has been added. Return NULL if none found.
2638 Output_section_data*
2639 Output_section::find_merge_section(const Relobj* object,
2640 unsigned int shndx) const
2642 if (!this->lookup_maps_->is_valid())
2643 this->build_lookup_maps();
2644 return this->lookup_maps_->find_merge_section(object, shndx);
2647 // Build the lookup maps for merge and relaxed sections. This is needs
2648 // to be declared as a const methods so that it is callable with a const
2649 // Output_section pointer. The method only updates states of the maps.
2652 Output_section::build_lookup_maps() const
2654 this->lookup_maps_->clear();
2655 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2656 p != this->input_sections_.end();
2659 if (p->is_merge_section())
2661 Output_merge_base* pomb = p->output_merge_base();
2662 Merge_section_properties msp(pomb->is_string(), pomb->entsize(),
2664 this->lookup_maps_->add_merge_section(msp, pomb);
2665 for (Output_merge_base::Input_sections::const_iterator is =
2666 pomb->input_sections_begin();
2667 is != pomb->input_sections_end();
2670 const Const_section_id& csid = *is;
2671 this->lookup_maps_->add_merge_input_section(csid.first,
2676 else if (p->is_relaxed_input_section())
2678 Output_relaxed_input_section* poris = p->relaxed_input_section();
2679 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2680 poris->shndx(), poris);
2685 // Find an relaxed input section corresponding to an input section
2686 // in OBJECT with index SHNDX.
2688 const Output_relaxed_input_section*
2689 Output_section::find_relaxed_input_section(const Relobj* object,
2690 unsigned int shndx) const
2692 if (!this->lookup_maps_->is_valid())
2693 this->build_lookup_maps();
2694 return this->lookup_maps_->find_relaxed_input_section(object, shndx);
2697 // Given an address OFFSET relative to the start of input section
2698 // SHNDX in OBJECT, return whether this address is being included in
2699 // the final link. This should only be called if SHNDX in OBJECT has
2700 // a special mapping.
2703 Output_section::is_input_address_mapped(const Relobj* object,
2707 // Look at the Output_section_data_maps first.
2708 const Output_section_data* posd = this->find_merge_section(object, shndx);
2710 posd = this->find_relaxed_input_section(object, shndx);
2714 section_offset_type output_offset;
2715 bool found = posd->output_offset(object, shndx, offset, &output_offset);
2717 return output_offset != -1;
2720 // Fall back to the slow look-up.
2721 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2722 p != this->input_sections_.end();
2725 section_offset_type output_offset;
2726 if (p->output_offset(object, shndx, offset, &output_offset))
2727 return output_offset != -1;
2730 // By default we assume that the address is mapped. This should
2731 // only be called after we have passed all sections to Layout. At
2732 // that point we should know what we are discarding.
2736 // Given an address OFFSET relative to the start of input section
2737 // SHNDX in object OBJECT, return the output offset relative to the
2738 // start of the input section in the output section. This should only
2739 // be called if SHNDX in OBJECT has a special mapping.
2742 Output_section::output_offset(const Relobj* object, unsigned int shndx,
2743 section_offset_type offset) const
2745 // This can only be called meaningfully when we know the data size
2747 gold_assert(this->is_data_size_valid());
2749 // Look at the Output_section_data_maps first.
2750 const Output_section_data* posd = this->find_merge_section(object, shndx);
2752 posd = this->find_relaxed_input_section(object, shndx);
2755 section_offset_type output_offset;
2756 bool found = posd->output_offset(object, shndx, offset, &output_offset);
2758 return output_offset;
2761 // Fall back to the slow look-up.
2762 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2763 p != this->input_sections_.end();
2766 section_offset_type output_offset;
2767 if (p->output_offset(object, shndx, offset, &output_offset))
2768 return output_offset;
2773 // Return the output virtual address of OFFSET relative to the start
2774 // of input section SHNDX in object OBJECT.
2777 Output_section::output_address(const Relobj* object, unsigned int shndx,
2780 uint64_t addr = this->address() + this->first_input_offset_;
2782 // Look at the Output_section_data_maps first.
2783 const Output_section_data* posd = this->find_merge_section(object, shndx);
2785 posd = this->find_relaxed_input_section(object, shndx);
2786 if (posd != NULL && posd->is_address_valid())
2788 section_offset_type output_offset;
2789 bool found = posd->output_offset(object, shndx, offset, &output_offset);
2791 return posd->address() + output_offset;
2794 // Fall back to the slow look-up.
2795 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2796 p != this->input_sections_.end();
2799 addr = align_address(addr, p->addralign());
2800 section_offset_type output_offset;
2801 if (p->output_offset(object, shndx, offset, &output_offset))
2803 if (output_offset == -1)
2805 return addr + output_offset;
2807 addr += p->data_size();
2810 // If we get here, it means that we don't know the mapping for this
2811 // input section. This might happen in principle if
2812 // add_input_section were called before add_output_section_data.
2813 // But it should never actually happen.
2818 // Find the output address of the start of the merged section for
2819 // input section SHNDX in object OBJECT.
2822 Output_section::find_starting_output_address(const Relobj* object,
2824 uint64_t* paddr) const
2826 // FIXME: This becomes a bottle-neck if we have many relaxed sections.
2827 // Looking up the merge section map does not always work as we sometimes
2828 // find a merge section without its address set.
2829 uint64_t addr = this->address() + this->first_input_offset_;
2830 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2831 p != this->input_sections_.end();
2834 addr = align_address(addr, p->addralign());
2836 // It would be nice if we could use the existing output_offset
2837 // method to get the output offset of input offset 0.
2838 // Unfortunately we don't know for sure that input offset 0 is
2840 if (p->is_merge_section_for(object, shndx))
2846 addr += p->data_size();
2849 // We couldn't find a merge output section for this input section.
2853 // Update the data size of an Output_section.
2856 Output_section::update_data_size()
2858 if (this->input_sections_.empty())
2861 if (this->must_sort_attached_input_sections()
2862 || this->input_section_order_specified())
2863 this->sort_attached_input_sections();
2865 off_t off = this->first_input_offset_;
2866 for (Input_section_list::iterator p = this->input_sections_.begin();
2867 p != this->input_sections_.end();
2870 off = align_address(off, p->addralign());
2871 off += p->current_data_size();
2874 this->set_current_data_size_for_child(off);
2877 // Set the data size of an Output_section. This is where we handle
2878 // setting the addresses of any Output_section_data objects.
2881 Output_section::set_final_data_size()
2883 if (this->input_sections_.empty())
2885 this->set_data_size(this->current_data_size_for_child());
2889 if (this->must_sort_attached_input_sections()
2890 || this->input_section_order_specified())
2891 this->sort_attached_input_sections();
2893 uint64_t address = this->address();
2894 off_t startoff = this->offset();
2895 off_t off = startoff + this->first_input_offset_;
2896 for (Input_section_list::iterator p = this->input_sections_.begin();
2897 p != this->input_sections_.end();
2900 off = align_address(off, p->addralign());
2901 p->set_address_and_file_offset(address + (off - startoff), off,
2903 off += p->data_size();
2906 this->set_data_size(off - startoff);
2909 // Reset the address and file offset.
2912 Output_section::do_reset_address_and_file_offset()
2914 // An unallocated section has no address. Forcing this means that
2915 // we don't need special treatment for symbols defined in debug
2916 // sections. We do the same in the constructor. This does not
2917 // apply to NOLOAD sections though.
2918 if (((this->flags_ & elfcpp::SHF_ALLOC) == 0) && !this->is_noload_)
2919 this->set_address(0);
2921 for (Input_section_list::iterator p = this->input_sections_.begin();
2922 p != this->input_sections_.end();
2924 p->reset_address_and_file_offset();
2927 // Return true if address and file offset have the values after reset.
2930 Output_section::do_address_and_file_offset_have_reset_values() const
2932 if (this->is_offset_valid())
2935 // An unallocated section has address 0 after its construction or a reset.
2936 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0)
2937 return this->is_address_valid() && this->address() == 0;
2939 return !this->is_address_valid();
2942 // Set the TLS offset. Called only for SHT_TLS sections.
2945 Output_section::do_set_tls_offset(uint64_t tls_base)
2947 this->tls_offset_ = this->address() - tls_base;
2950 // In a few cases we need to sort the input sections attached to an
2951 // output section. This is used to implement the type of constructor
2952 // priority ordering implemented by the GNU linker, in which the
2953 // priority becomes part of the section name and the sections are
2954 // sorted by name. We only do this for an output section if we see an
2955 // attached input section matching ".ctor.*", ".dtor.*",
2956 // ".init_array.*" or ".fini_array.*".
2958 class Output_section::Input_section_sort_entry
2961 Input_section_sort_entry()
2962 : input_section_(), index_(-1U), section_has_name_(false),
2966 Input_section_sort_entry(const Input_section& input_section,
2968 bool must_sort_attached_input_sections)
2969 : input_section_(input_section), index_(index),
2970 section_has_name_(input_section.is_input_section()
2971 || input_section.is_relaxed_input_section())
2973 if (this->section_has_name_
2974 && must_sort_attached_input_sections)
2976 // This is only called single-threaded from Layout::finalize,
2977 // so it is OK to lock. Unfortunately we have no way to pass
2979 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
2980 Object* obj = (input_section.is_input_section()
2981 ? input_section.relobj()
2982 : input_section.relaxed_input_section()->relobj());
2983 Task_lock_obj<Object> tl(dummy_task, obj);
2985 // This is a slow operation, which should be cached in
2986 // Layout::layout if this becomes a speed problem.
2987 this->section_name_ = obj->section_name(input_section.shndx());
2991 // Return the Input_section.
2992 const Input_section&
2993 input_section() const
2995 gold_assert(this->index_ != -1U);
2996 return this->input_section_;
2999 // The index of this entry in the original list. This is used to
3000 // make the sort stable.
3004 gold_assert(this->index_ != -1U);
3005 return this->index_;
3008 // Whether there is a section name.
3010 section_has_name() const
3011 { return this->section_has_name_; }
3013 // The section name.
3015 section_name() const
3017 gold_assert(this->section_has_name_);
3018 return this->section_name_;
3021 // Return true if the section name has a priority. This is assumed
3022 // to be true if it has a dot after the initial dot.
3024 has_priority() const
3026 gold_assert(this->section_has_name_);
3027 return this->section_name_.find('.', 1) != std::string::npos;
3030 // Return true if this an input file whose base name matches
3031 // FILE_NAME. The base name must have an extension of ".o", and
3032 // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
3033 // This is to match crtbegin.o as well as crtbeginS.o without
3034 // getting confused by other possibilities. Overall matching the
3035 // file name this way is a dreadful hack, but the GNU linker does it
3036 // in order to better support gcc, and we need to be compatible.
3038 match_file_name(const char* match_file_name) const
3040 const std::string& file_name(this->input_section_.relobj()->name());
3041 const char* base_name = lbasename(file_name.c_str());
3042 size_t match_len = strlen(match_file_name);
3043 if (strncmp(base_name, match_file_name, match_len) != 0)
3045 size_t base_len = strlen(base_name);
3046 if (base_len != match_len + 2 && base_len != match_len + 3)
3048 return memcmp(base_name + base_len - 2, ".o", 2) == 0;
3051 // Returns 1 if THIS should appear before S in section order, -1 if S
3052 // appears before THIS and 0 if they are not comparable.
3054 compare_section_ordering(const Input_section_sort_entry& s) const
3056 unsigned int this_secn_index = this->input_section_.section_order_index();
3057 unsigned int s_secn_index = s.input_section().section_order_index();
3058 if (this_secn_index > 0 && s_secn_index > 0)
3060 if (this_secn_index < s_secn_index)
3062 else if (this_secn_index > s_secn_index)
3069 // The Input_section we are sorting.
3070 Input_section input_section_;
3071 // The index of this Input_section in the original list.
3072 unsigned int index_;
3073 // Whether this Input_section has a section name--it won't if this
3074 // is some random Output_section_data.
3075 bool section_has_name_;
3076 // The section name if there is one.
3077 std::string section_name_;
3080 // Return true if S1 should come before S2 in the output section.
3083 Output_section::Input_section_sort_compare::operator()(
3084 const Output_section::Input_section_sort_entry& s1,
3085 const Output_section::Input_section_sort_entry& s2) const
3087 // crtbegin.o must come first.
3088 bool s1_begin = s1.match_file_name("crtbegin");
3089 bool s2_begin = s2.match_file_name("crtbegin");
3090 if (s1_begin || s2_begin)
3096 return s1.index() < s2.index();
3099 // crtend.o must come last.
3100 bool s1_end = s1.match_file_name("crtend");
3101 bool s2_end = s2.match_file_name("crtend");
3102 if (s1_end || s2_end)
3108 return s1.index() < s2.index();
3111 // We sort all the sections with no names to the end.
3112 if (!s1.section_has_name() || !s2.section_has_name())
3114 if (s1.section_has_name())
3116 if (s2.section_has_name())
3118 return s1.index() < s2.index();
3121 // A section with a priority follows a section without a priority.
3122 bool s1_has_priority = s1.has_priority();
3123 bool s2_has_priority = s2.has_priority();
3124 if (s1_has_priority && !s2_has_priority)
3126 if (!s1_has_priority && s2_has_priority)
3129 // Check if a section order exists for these sections through a section
3130 // ordering file. If sequence_num is 0, an order does not exist.
3131 int sequence_num = s1.compare_section_ordering(s2);
3132 if (sequence_num != 0)
3133 return sequence_num == 1;
3135 // Otherwise we sort by name.
3136 int compare = s1.section_name().compare(s2.section_name());
3140 // Otherwise we keep the input order.
3141 return s1.index() < s2.index();
3144 // Return true if S1 should come before S2 in an .init_array or .fini_array
3148 Output_section::Input_section_sort_init_fini_compare::operator()(
3149 const Output_section::Input_section_sort_entry& s1,
3150 const Output_section::Input_section_sort_entry& s2) const
3152 // We sort all the sections with no names to the end.
3153 if (!s1.section_has_name() || !s2.section_has_name())
3155 if (s1.section_has_name())
3157 if (s2.section_has_name())
3159 return s1.index() < s2.index();
3162 // A section without a priority follows a section with a priority.
3163 // This is the reverse of .ctors and .dtors sections.
3164 bool s1_has_priority = s1.has_priority();
3165 bool s2_has_priority = s2.has_priority();
3166 if (s1_has_priority && !s2_has_priority)
3168 if (!s1_has_priority && s2_has_priority)
3171 // Check if a section order exists for these sections through a section
3172 // ordering file. If sequence_num is 0, an order does not exist.
3173 int sequence_num = s1.compare_section_ordering(s2);
3174 if (sequence_num != 0)
3175 return sequence_num == 1;
3177 // Otherwise we sort by name.
3178 int compare = s1.section_name().compare(s2.section_name());
3182 // Otherwise we keep the input order.
3183 return s1.index() < s2.index();
3186 // Return true if S1 should come before S2. Sections that do not match
3187 // any pattern in the section ordering file are placed ahead of the sections
3188 // that match some pattern.
3191 Output_section::Input_section_sort_section_order_index_compare::operator()(
3192 const Output_section::Input_section_sort_entry& s1,
3193 const Output_section::Input_section_sort_entry& s2) const
3195 unsigned int s1_secn_index = s1.input_section().section_order_index();
3196 unsigned int s2_secn_index = s2.input_section().section_order_index();
3198 // Keep input order if section ordering cannot determine order.
3199 if (s1_secn_index == s2_secn_index)
3200 return s1.index() < s2.index();
3202 return s1_secn_index < s2_secn_index;
3205 // Sort the input sections attached to an output section.
3208 Output_section::sort_attached_input_sections()
3210 if (this->attached_input_sections_are_sorted_)
3213 if (this->checkpoint_ != NULL
3214 && !this->checkpoint_->input_sections_saved())
3215 this->checkpoint_->save_input_sections();
3217 // The only thing we know about an input section is the object and
3218 // the section index. We need the section name. Recomputing this
3219 // is slow but this is an unusual case. If this becomes a speed
3220 // problem we can cache the names as required in Layout::layout.
3222 // We start by building a larger vector holding a copy of each
3223 // Input_section, plus its current index in the list and its name.
3224 std::vector<Input_section_sort_entry> sort_list;
3227 for (Input_section_list::iterator p = this->input_sections_.begin();
3228 p != this->input_sections_.end();
3230 sort_list.push_back(Input_section_sort_entry(*p, i,
3231 this->must_sort_attached_input_sections()));
3233 // Sort the input sections.
3234 if (this->must_sort_attached_input_sections())
3236 if (this->type() == elfcpp::SHT_PREINIT_ARRAY
3237 || this->type() == elfcpp::SHT_INIT_ARRAY
3238 || this->type() == elfcpp::SHT_FINI_ARRAY)
3239 std::sort(sort_list.begin(), sort_list.end(),
3240 Input_section_sort_init_fini_compare());
3242 std::sort(sort_list.begin(), sort_list.end(),
3243 Input_section_sort_compare());
3247 gold_assert(parameters->options().section_ordering_file());
3248 std::sort(sort_list.begin(), sort_list.end(),
3249 Input_section_sort_section_order_index_compare());
3252 // Copy the sorted input sections back to our list.
3253 this->input_sections_.clear();
3254 for (std::vector<Input_section_sort_entry>::iterator p = sort_list.begin();
3255 p != sort_list.end();
3257 this->input_sections_.push_back(p->input_section());
3260 // Remember that we sorted the input sections, since we might get
3262 this->attached_input_sections_are_sorted_ = true;
3265 // Write the section header to *OSHDR.
3267 template<int size, bool big_endian>
3269 Output_section::write_header(const Layout* layout,
3270 const Stringpool* secnamepool,
3271 elfcpp::Shdr_write<size, big_endian>* oshdr) const
3273 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
3274 oshdr->put_sh_type(this->type_);
3276 elfcpp::Elf_Xword flags = this->flags_;
3277 if (this->info_section_ != NULL && this->info_uses_section_index_)
3278 flags |= elfcpp::SHF_INFO_LINK;
3279 oshdr->put_sh_flags(flags);
3281 oshdr->put_sh_addr(this->address());
3282 oshdr->put_sh_offset(this->offset());
3283 oshdr->put_sh_size(this->data_size());
3284 if (this->link_section_ != NULL)
3285 oshdr->put_sh_link(this->link_section_->out_shndx());
3286 else if (this->should_link_to_symtab_)
3287 oshdr->put_sh_link(layout->symtab_section()->out_shndx());
3288 else if (this->should_link_to_dynsym_)
3289 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
3291 oshdr->put_sh_link(this->link_);
3293 elfcpp::Elf_Word info;
3294 if (this->info_section_ != NULL)
3296 if (this->info_uses_section_index_)
3297 info = this->info_section_->out_shndx();
3299 info = this->info_section_->symtab_index();
3301 else if (this->info_symndx_ != NULL)
3302 info = this->info_symndx_->symtab_index();
3305 oshdr->put_sh_info(info);
3307 oshdr->put_sh_addralign(this->addralign_);
3308 oshdr->put_sh_entsize(this->entsize_);
3311 // Write out the data. For input sections the data is written out by
3312 // Object::relocate, but we have to handle Output_section_data objects
3316 Output_section::do_write(Output_file* of)
3318 gold_assert(!this->requires_postprocessing());
3320 // If the target performs relaxation, we delay filler generation until now.
3321 gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
3323 off_t output_section_file_offset = this->offset();
3324 for (Fill_list::iterator p = this->fills_.begin();
3325 p != this->fills_.end();
3328 std::string fill_data(parameters->target().code_fill(p->length()));
3329 of->write(output_section_file_offset + p->section_offset(),
3330 fill_data.data(), fill_data.size());
3333 off_t off = this->offset() + this->first_input_offset_;
3334 for (Input_section_list::iterator p = this->input_sections_.begin();
3335 p != this->input_sections_.end();
3338 off_t aligned_off = align_address(off, p->addralign());
3339 if (this->generate_code_fills_at_write_ && (off != aligned_off))
3341 size_t fill_len = aligned_off - off;
3342 std::string fill_data(parameters->target().code_fill(fill_len));
3343 of->write(off, fill_data.data(), fill_data.size());
3347 off = aligned_off + p->data_size();
3351 // If a section requires postprocessing, create the buffer to use.
3354 Output_section::create_postprocessing_buffer()
3356 gold_assert(this->requires_postprocessing());
3358 if (this->postprocessing_buffer_ != NULL)
3361 if (!this->input_sections_.empty())
3363 off_t off = this->first_input_offset_;
3364 for (Input_section_list::iterator p = this->input_sections_.begin();
3365 p != this->input_sections_.end();
3368 off = align_address(off, p->addralign());
3369 p->finalize_data_size();
3370 off += p->data_size();
3372 this->set_current_data_size_for_child(off);
3375 off_t buffer_size = this->current_data_size_for_child();
3376 this->postprocessing_buffer_ = new unsigned char[buffer_size];
3379 // Write all the data of an Output_section into the postprocessing
3380 // buffer. This is used for sections which require postprocessing,
3381 // such as compression. Input sections are handled by
3382 // Object::Relocate.
3385 Output_section::write_to_postprocessing_buffer()
3387 gold_assert(this->requires_postprocessing());
3389 // If the target performs relaxation, we delay filler generation until now.
3390 gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
3392 unsigned char* buffer = this->postprocessing_buffer();
3393 for (Fill_list::iterator p = this->fills_.begin();
3394 p != this->fills_.end();
3397 std::string fill_data(parameters->target().code_fill(p->length()));
3398 memcpy(buffer + p->section_offset(), fill_data.data(),
3402 off_t off = this->first_input_offset_;
3403 for (Input_section_list::iterator p = this->input_sections_.begin();
3404 p != this->input_sections_.end();
3407 off_t aligned_off = align_address(off, p->addralign());
3408 if (this->generate_code_fills_at_write_ && (off != aligned_off))
3410 size_t fill_len = aligned_off - off;
3411 std::string fill_data(parameters->target().code_fill(fill_len));
3412 memcpy(buffer + off, fill_data.data(), fill_data.size());
3415 p->write_to_buffer(buffer + aligned_off);
3416 off = aligned_off + p->data_size();
3420 // Get the input sections for linker script processing. We leave
3421 // behind the Output_section_data entries. Note that this may be
3422 // slightly incorrect for merge sections. We will leave them behind,
3423 // but it is possible that the script says that they should follow
3424 // some other input sections, as in:
3425 // .rodata { *(.rodata) *(.rodata.cst*) }
3426 // For that matter, we don't handle this correctly:
3427 // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
3428 // With luck this will never matter.
3431 Output_section::get_input_sections(
3433 const std::string& fill,
3434 std::list<Input_section>* input_sections)
3436 if (this->checkpoint_ != NULL
3437 && !this->checkpoint_->input_sections_saved())
3438 this->checkpoint_->save_input_sections();
3440 // Invalidate fast look-up maps.
3441 this->lookup_maps_->invalidate();
3443 uint64_t orig_address = address;
3445 address = align_address(address, this->addralign());
3447 Input_section_list remaining;
3448 for (Input_section_list::iterator p = this->input_sections_.begin();
3449 p != this->input_sections_.end();
3452 if (p->is_input_section()
3453 || p->is_relaxed_input_section()
3454 || p->is_merge_section())
3455 input_sections->push_back(*p);
3458 uint64_t aligned_address = align_address(address, p->addralign());
3459 if (aligned_address != address && !fill.empty())
3461 section_size_type length =
3462 convert_to_section_size_type(aligned_address - address);
3463 std::string this_fill;
3464 this_fill.reserve(length);
3465 while (this_fill.length() + fill.length() <= length)
3467 if (this_fill.length() < length)
3468 this_fill.append(fill, 0, length - this_fill.length());
3470 Output_section_data* posd = new Output_data_const(this_fill, 0);
3471 remaining.push_back(Input_section(posd));
3473 address = aligned_address;
3475 remaining.push_back(*p);
3477 p->finalize_data_size();
3478 address += p->data_size();
3482 this->input_sections_.swap(remaining);
3483 this->first_input_offset_ = 0;
3485 uint64_t data_size = address - orig_address;
3486 this->set_current_data_size_for_child(data_size);
3490 // Add a script input section. SIS is an Output_section::Input_section,
3491 // which can be either a plain input section or a special input section like
3492 // a relaxed input section. For a special input section, its size must be
3496 Output_section::add_script_input_section(const Input_section& sis)
3498 uint64_t data_size = sis.data_size();
3499 uint64_t addralign = sis.addralign();
3500 if (addralign > this->addralign_)
3501 this->addralign_ = addralign;
3503 off_t offset_in_section = this->current_data_size_for_child();
3504 off_t aligned_offset_in_section = align_address(offset_in_section,
3507 this->set_current_data_size_for_child(aligned_offset_in_section
3510 this->input_sections_.push_back(sis);
3512 // Update fast lookup maps if necessary.
3513 if (this->lookup_maps_->is_valid())
3515 if (sis.is_merge_section())
3517 Output_merge_base* pomb = sis.output_merge_base();
3518 Merge_section_properties msp(pomb->is_string(), pomb->entsize(),
3520 this->lookup_maps_->add_merge_section(msp, pomb);
3521 for (Output_merge_base::Input_sections::const_iterator p =
3522 pomb->input_sections_begin();
3523 p != pomb->input_sections_end();
3525 this->lookup_maps_->add_merge_input_section(p->first, p->second,
3528 else if (sis.is_relaxed_input_section())
3530 Output_relaxed_input_section* poris = sis.relaxed_input_section();
3531 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
3532 poris->shndx(), poris);
3537 // Save states for relaxation.
3540 Output_section::save_states()
3542 gold_assert(this->checkpoint_ == NULL);
3543 Checkpoint_output_section* checkpoint =
3544 new Checkpoint_output_section(this->addralign_, this->flags_,
3545 this->input_sections_,
3546 this->first_input_offset_,
3547 this->attached_input_sections_are_sorted_);
3548 this->checkpoint_ = checkpoint;
3549 gold_assert(this->fills_.empty());
3553 Output_section::discard_states()
3555 gold_assert(this->checkpoint_ != NULL);
3556 delete this->checkpoint_;
3557 this->checkpoint_ = NULL;
3558 gold_assert(this->fills_.empty());
3560 // Simply invalidate the fast lookup maps since we do not keep
3562 this->lookup_maps_->invalidate();
3566 Output_section::restore_states()
3568 gold_assert(this->checkpoint_ != NULL);
3569 Checkpoint_output_section* checkpoint = this->checkpoint_;
3571 this->addralign_ = checkpoint->addralign();
3572 this->flags_ = checkpoint->flags();
3573 this->first_input_offset_ = checkpoint->first_input_offset();
3575 if (!checkpoint->input_sections_saved())
3577 // If we have not copied the input sections, just resize it.
3578 size_t old_size = checkpoint->input_sections_size();
3579 gold_assert(this->input_sections_.size() >= old_size);
3580 this->input_sections_.resize(old_size);
3584 // We need to copy the whole list. This is not efficient for
3585 // extremely large output with hundreads of thousands of input
3586 // objects. We may need to re-think how we should pass sections
3588 this->input_sections_ = *checkpoint->input_sections();
3591 this->attached_input_sections_are_sorted_ =
3592 checkpoint->attached_input_sections_are_sorted();
3594 // Simply invalidate the fast lookup maps since we do not keep
3596 this->lookup_maps_->invalidate();
3599 // Update the section offsets of input sections in this. This is required if
3600 // relaxation causes some input sections to change sizes.
3603 Output_section::adjust_section_offsets()
3605 if (!this->section_offsets_need_adjustment_)
3609 for (Input_section_list::iterator p = this->input_sections_.begin();
3610 p != this->input_sections_.end();
3613 off = align_address(off, p->addralign());
3614 if (p->is_input_section())
3615 p->relobj()->set_section_offset(p->shndx(), off);
3616 off += p->data_size();
3619 this->section_offsets_need_adjustment_ = false;
3622 // Print to the map file.
3625 Output_section::do_print_to_mapfile(Mapfile* mapfile) const
3627 mapfile->print_output_section(this);
3629 for (Input_section_list::const_iterator p = this->input_sections_.begin();
3630 p != this->input_sections_.end();
3632 p->print_to_mapfile(mapfile);
3635 // Print stats for merge sections to stderr.
3638 Output_section::print_merge_stats()
3640 Input_section_list::iterator p;
3641 for (p = this->input_sections_.begin();
3642 p != this->input_sections_.end();
3644 p->print_merge_stats(this->name_);
3647 // Set a fixed layout for the section. Used for incremental update links.
3650 Output_section::set_fixed_layout(uint64_t sh_addr, off_t sh_offset,
3651 off_t sh_size, uint64_t sh_addralign)
3653 this->addralign_ = sh_addralign;
3654 this->set_current_data_size(sh_size);
3655 if ((this->flags_ & elfcpp::SHF_ALLOC) != 0)
3656 this->set_address(sh_addr);
3657 this->set_file_offset(sh_offset);
3658 this->finalize_data_size();
3659 this->free_list_.init(sh_size, false);
3660 this->has_fixed_layout_ = true;
3663 // Reserve space within the fixed layout for the section. Used for
3664 // incremental update links.
3666 Output_section::reserve(uint64_t sh_offset, uint64_t sh_size)
3668 this->free_list_.remove(sh_offset, sh_offset + sh_size);
3671 // Output segment methods.
3673 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
3683 is_max_align_known_(false),
3684 are_addresses_set_(false),
3685 is_large_data_segment_(false)
3687 // The ELF ABI specifies that a PT_TLS segment always has PF_R as
3689 if (type == elfcpp::PT_TLS)
3690 this->flags_ = elfcpp::PF_R;
3693 // Add an Output_section to a PT_LOAD Output_segment.
3696 Output_segment::add_output_section_to_load(Layout* layout,
3698 elfcpp::Elf_Word seg_flags)
3700 gold_assert(this->type() == elfcpp::PT_LOAD);
3701 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
3702 gold_assert(!this->is_max_align_known_);
3703 gold_assert(os->is_large_data_section() == this->is_large_data_segment());
3705 this->update_flags_for_output_section(seg_flags);
3707 // We don't want to change the ordering if we have a linker script
3708 // with a SECTIONS clause.
3709 Output_section_order order = os->order();
3710 if (layout->script_options()->saw_sections_clause())
3711 order = static_cast<Output_section_order>(0);
3713 gold_assert(order != ORDER_INVALID);
3715 this->output_lists_[order].push_back(os);
3718 // Add an Output_section to a non-PT_LOAD Output_segment.
3721 Output_segment::add_output_section_to_nonload(Output_section* os,
3722 elfcpp::Elf_Word seg_flags)
3724 gold_assert(this->type() != elfcpp::PT_LOAD);
3725 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
3726 gold_assert(!this->is_max_align_known_);
3728 this->update_flags_for_output_section(seg_flags);
3730 this->output_lists_[0].push_back(os);
3733 // Remove an Output_section from this segment. It is an error if it
3737 Output_segment::remove_output_section(Output_section* os)
3739 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
3741 Output_data_list* pdl = &this->output_lists_[i];
3742 for (Output_data_list::iterator p = pdl->begin(); p != pdl->end(); ++p)
3754 // Add an Output_data (which need not be an Output_section) to the
3755 // start of a segment.
3758 Output_segment::add_initial_output_data(Output_data* od)
3760 gold_assert(!this->is_max_align_known_);
3761 Output_data_list::iterator p = this->output_lists_[0].begin();
3762 this->output_lists_[0].insert(p, od);
3765 // Return true if this segment has any sections which hold actual
3766 // data, rather than being a BSS section.
3769 Output_segment::has_any_data_sections() const
3771 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
3773 const Output_data_list* pdl = &this->output_lists_[i];
3774 for (Output_data_list::const_iterator p = pdl->begin();
3778 if (!(*p)->is_section())
3780 if ((*p)->output_section()->type() != elfcpp::SHT_NOBITS)
3787 // Return whether the first data section (not counting TLS sections)
3788 // is a relro section.
3791 Output_segment::is_first_section_relro() const
3793 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
3795 if (i == static_cast<int>(ORDER_TLS_DATA)
3796 || i == static_cast<int>(ORDER_TLS_BSS))
3798 const Output_data_list* pdl = &this->output_lists_[i];
3801 Output_data* p = pdl->front();
3802 return p->is_section() && p->output_section()->is_relro();
3808 // Return the maximum alignment of the Output_data in Output_segment.
3811 Output_segment::maximum_alignment()
3813 if (!this->is_max_align_known_)
3815 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
3817 const Output_data_list* pdl = &this->output_lists_[i];
3818 uint64_t addralign = Output_segment::maximum_alignment_list(pdl);
3819 if (addralign > this->max_align_)
3820 this->max_align_ = addralign;
3822 this->is_max_align_known_ = true;
3825 return this->max_align_;
3828 // Return the maximum alignment of a list of Output_data.
3831 Output_segment::maximum_alignment_list(const Output_data_list* pdl)
3834 for (Output_data_list::const_iterator p = pdl->begin();
3838 uint64_t addralign = (*p)->addralign();
3839 if (addralign > ret)
3845 // Return whether this segment has any dynamic relocs.
3848 Output_segment::has_dynamic_reloc() const
3850 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
3851 if (this->has_dynamic_reloc_list(&this->output_lists_[i]))
3856 // Return whether this Output_data_list has any dynamic relocs.
3859 Output_segment::has_dynamic_reloc_list(const Output_data_list* pdl) const
3861 for (Output_data_list::const_iterator p = pdl->begin();
3864 if ((*p)->has_dynamic_reloc())
3869 // Set the section addresses for an Output_segment. If RESET is true,
3870 // reset the addresses first. ADDR is the address and *POFF is the
3871 // file offset. Set the section indexes starting with *PSHNDX.
3872 // INCREASE_RELRO is the size of the portion of the first non-relro
3873 // section that should be included in the PT_GNU_RELRO segment.
3874 // If this segment has relro sections, and has been aligned for
3875 // that purpose, set *HAS_RELRO to TRUE. Return the address of
3876 // the immediately following segment. Update *HAS_RELRO, *POFF,
3880 Output_segment::set_section_addresses(Layout* layout, bool reset,
3882 unsigned int* increase_relro,
3885 unsigned int* pshndx)
3887 gold_assert(this->type_ == elfcpp::PT_LOAD);
3889 uint64_t last_relro_pad = 0;
3890 off_t orig_off = *poff;
3892 bool in_tls = false;
3894 // If we have relro sections, we need to pad forward now so that the
3895 // relro sections plus INCREASE_RELRO end on a common page boundary.
3896 if (parameters->options().relro()
3897 && this->is_first_section_relro()
3898 && (!this->are_addresses_set_ || reset))
3900 uint64_t relro_size = 0;
3902 uint64_t max_align = 0;
3903 for (int i = 0; i <= static_cast<int>(ORDER_RELRO_LAST); ++i)
3905 Output_data_list* pdl = &this->output_lists_[i];
3906 Output_data_list::iterator p;
3907 for (p = pdl->begin(); p != pdl->end(); ++p)
3909 if (!(*p)->is_section())
3911 uint64_t align = (*p)->addralign();
3912 if (align > max_align)
3914 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
3918 // Align the first non-TLS section to the alignment
3919 // of the TLS segment.
3923 relro_size = align_address(relro_size, align);
3924 // Ignore the size of the .tbss section.
3925 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS)
3926 && (*p)->is_section_type(elfcpp::SHT_NOBITS))
3928 if ((*p)->is_address_valid())
3929 relro_size += (*p)->data_size();
3932 // FIXME: This could be faster.
3933 (*p)->set_address_and_file_offset(addr + relro_size,
3935 relro_size += (*p)->data_size();
3936 (*p)->reset_address_and_file_offset();
3939 if (p != pdl->end())
3942 relro_size += *increase_relro;
3943 // Pad the total relro size to a multiple of the maximum
3944 // section alignment seen.
3945 uint64_t aligned_size = align_address(relro_size, max_align);
3946 // Note the amount of padding added after the last relro section.
3947 last_relro_pad = aligned_size - relro_size;
3950 uint64_t page_align = parameters->target().common_pagesize();
3952 // Align to offset N such that (N + RELRO_SIZE) % PAGE_ALIGN == 0.
3953 uint64_t desired_align = page_align - (aligned_size % page_align);
3954 if (desired_align < *poff % page_align)
3955 *poff += page_align - *poff % page_align;
3956 *poff += desired_align - *poff % page_align;
3957 addr += *poff - orig_off;
3961 if (!reset && this->are_addresses_set_)
3963 gold_assert(this->paddr_ == addr);
3964 addr = this->vaddr_;
3968 this->vaddr_ = addr;
3969 this->paddr_ = addr;
3970 this->are_addresses_set_ = true;
3975 this->offset_ = orig_off;
3979 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
3981 if (i == static_cast<int>(ORDER_RELRO_LAST))
3983 *poff += last_relro_pad;
3984 addr += last_relro_pad;
3985 if (this->output_lists_[i].empty())
3987 // If there is nothing in the ORDER_RELRO_LAST list,
3988 // the padding will occur at the end of the relro
3989 // segment, and we need to add it to *INCREASE_RELRO.
3990 *increase_relro += last_relro_pad;
3993 addr = this->set_section_list_addresses(layout, reset,
3994 &this->output_lists_[i],
3995 addr, poff, pshndx, &in_tls);
3996 if (i < static_cast<int>(ORDER_SMALL_BSS))
3998 this->filesz_ = *poff - orig_off;
4005 // If the last section was a TLS section, align upward to the
4006 // alignment of the TLS segment, so that the overall size of the TLS
4007 // segment is aligned.
4010 uint64_t segment_align = layout->tls_segment()->maximum_alignment();
4011 *poff = align_address(*poff, segment_align);
4014 this->memsz_ = *poff - orig_off;
4016 // Ignore the file offset adjustments made by the BSS Output_data
4023 // Set the addresses and file offsets in a list of Output_data
4027 Output_segment::set_section_list_addresses(Layout* layout, bool reset,
4028 Output_data_list* pdl,
4029 uint64_t addr, off_t* poff,
4030 unsigned int* pshndx,
4033 off_t startoff = *poff;
4034 // For incremental updates, we may allocate non-fixed sections from
4035 // free space in the file. This keeps track of the high-water mark.
4036 off_t maxoff = startoff;
4038 off_t off = startoff;
4039 for (Output_data_list::iterator p = pdl->begin();
4044 (*p)->reset_address_and_file_offset();
4046 // When doing an incremental update or when using a linker script,
4047 // the section will most likely already have an address.
4048 if (!(*p)->is_address_valid())
4050 uint64_t align = (*p)->addralign();
4052 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
4054 // Give the first TLS section the alignment of the
4055 // entire TLS segment. Otherwise the TLS segment as a
4056 // whole may be misaligned.
4059 Output_segment* tls_segment = layout->tls_segment();
4060 gold_assert(tls_segment != NULL);
4061 uint64_t segment_align = tls_segment->maximum_alignment();
4062 gold_assert(segment_align >= align);
4063 align = segment_align;
4070 // If this is the first section after the TLS segment,
4071 // align it to at least the alignment of the TLS
4072 // segment, so that the size of the overall TLS segment
4076 uint64_t segment_align =
4077 layout->tls_segment()->maximum_alignment();
4078 if (segment_align > align)
4079 align = segment_align;
4085 // FIXME: Need to handle TLS and .bss with incremental update.
4086 if (!parameters->incremental_update()
4087 || (*p)->is_section_flag_set(elfcpp::SHF_TLS)
4088 || (*p)->is_section_type(elfcpp::SHT_NOBITS))
4090 off = align_address(off, align);
4091 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
4095 // Incremental update: allocate file space from free list.
4096 (*p)->pre_finalize_data_size();
4097 off_t current_size = (*p)->current_data_size();
4098 off = layout->allocate(current_size, align, startoff);
4101 gold_assert((*p)->output_section() != NULL);
4102 gold_fatal(_("out of patch space for section %s; "
4103 "relink with --incremental-full"),
4104 (*p)->output_section()->name());
4106 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
4107 if ((*p)->data_size() > current_size)
4109 gold_assert((*p)->output_section() != NULL);
4110 gold_fatal(_("%s: section changed size; "
4111 "relink with --incremental-full"),
4112 (*p)->output_section()->name());
4116 else if (parameters->incremental_update())
4118 // For incremental updates, use the fixed offset for the
4119 // high-water mark computation.
4120 off = (*p)->offset();
4124 // The script may have inserted a skip forward, but it
4125 // better not have moved backward.
4126 if ((*p)->address() >= addr + (off - startoff))
4127 off += (*p)->address() - (addr + (off - startoff));
4130 if (!layout->script_options()->saw_sections_clause())
4134 Output_section* os = (*p)->output_section();
4136 // Cast to unsigned long long to avoid format warnings.
4137 unsigned long long previous_dot =
4138 static_cast<unsigned long long>(addr + (off - startoff));
4139 unsigned long long dot =
4140 static_cast<unsigned long long>((*p)->address());
4143 gold_error(_("dot moves backward in linker script "
4144 "from 0x%llx to 0x%llx"), previous_dot, dot);
4146 gold_error(_("address of section '%s' moves backward "
4147 "from 0x%llx to 0x%llx"),
4148 os->name(), previous_dot, dot);
4151 (*p)->set_file_offset(off);
4152 (*p)->finalize_data_size();
4155 gold_debug(DEBUG_INCREMENTAL,
4156 "set_section_list_addresses: %08lx %08lx %s",
4157 static_cast<long>(off),
4158 static_cast<long>((*p)->data_size()),
4159 ((*p)->output_section() != NULL
4160 ? (*p)->output_section()->name() : "(special)"));
4162 // We want to ignore the size of a SHF_TLS or SHT_NOBITS
4163 // section. Such a section does not affect the size of a
4165 if (!(*p)->is_section_flag_set(elfcpp::SHF_TLS)
4166 || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
4167 off += (*p)->data_size();
4172 if ((*p)->is_section())
4174 (*p)->set_out_shndx(*pshndx);
4180 return addr + (maxoff - startoff);
4183 // For a non-PT_LOAD segment, set the offset from the sections, if
4184 // any. Add INCREASE to the file size and the memory size.
4187 Output_segment::set_offset(unsigned int increase)
4189 gold_assert(this->type_ != elfcpp::PT_LOAD);
4191 gold_assert(!this->are_addresses_set_);
4193 // A non-load section only uses output_lists_[0].
4195 Output_data_list* pdl = &this->output_lists_[0];
4199 gold_assert(increase == 0);
4202 this->are_addresses_set_ = true;
4204 this->min_p_align_ = 0;
4210 // Find the first and last section by address.
4211 const Output_data* first = NULL;
4212 const Output_data* last_data = NULL;
4213 const Output_data* last_bss = NULL;
4214 for (Output_data_list::const_iterator p = pdl->begin();
4219 || (*p)->address() < first->address()
4220 || ((*p)->address() == first->address()
4221 && (*p)->data_size() < first->data_size()))
4223 const Output_data** plast;
4224 if ((*p)->is_section()
4225 && (*p)->output_section()->type() == elfcpp::SHT_NOBITS)
4230 || (*p)->address() > (*plast)->address()
4231 || ((*p)->address() == (*plast)->address()
4232 && (*p)->data_size() > (*plast)->data_size()))
4236 this->vaddr_ = first->address();
4237 this->paddr_ = (first->has_load_address()
4238 ? first->load_address()
4240 this->are_addresses_set_ = true;
4241 this->offset_ = first->offset();
4243 if (last_data == NULL)
4246 this->filesz_ = (last_data->address()
4247 + last_data->data_size()
4250 const Output_data* last = last_bss != NULL ? last_bss : last_data;
4251 this->memsz_ = (last->address()
4255 this->filesz_ += increase;
4256 this->memsz_ += increase;
4258 // If this is a RELRO segment, verify that the segment ends at a
4260 if (this->type_ == elfcpp::PT_GNU_RELRO)
4262 uint64_t page_align = parameters->target().common_pagesize();
4263 uint64_t segment_end = this->vaddr_ + this->memsz_;
4264 if (parameters->incremental_update())
4266 // The INCREASE_RELRO calculation is bypassed for an incremental
4267 // update, so we need to adjust the segment size manually here.
4268 segment_end = align_address(segment_end, page_align);
4269 this->memsz_ = segment_end - this->vaddr_;
4272 gold_assert(segment_end == align_address(segment_end, page_align));
4275 // If this is a TLS segment, align the memory size. The code in
4276 // set_section_list ensures that the section after the TLS segment
4277 // is aligned to give us room.
4278 if (this->type_ == elfcpp::PT_TLS)
4280 uint64_t segment_align = this->maximum_alignment();
4281 gold_assert(this->vaddr_ == align_address(this->vaddr_, segment_align));
4282 this->memsz_ = align_address(this->memsz_, segment_align);
4286 // Set the TLS offsets of the sections in the PT_TLS segment.
4289 Output_segment::set_tls_offsets()
4291 gold_assert(this->type_ == elfcpp::PT_TLS);
4293 for (Output_data_list::iterator p = this->output_lists_[0].begin();
4294 p != this->output_lists_[0].end();
4296 (*p)->set_tls_offset(this->vaddr_);
4299 // Return the load address of the first section.
4302 Output_segment::first_section_load_address() const
4304 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4306 const Output_data_list* pdl = &this->output_lists_[i];
4307 for (Output_data_list::const_iterator p = pdl->begin();
4311 if ((*p)->is_section())
4312 return ((*p)->has_load_address()
4313 ? (*p)->load_address()
4320 // Return the number of Output_sections in an Output_segment.
4323 Output_segment::output_section_count() const
4325 unsigned int ret = 0;
4326 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4327 ret += this->output_section_count_list(&this->output_lists_[i]);
4331 // Return the number of Output_sections in an Output_data_list.
4334 Output_segment::output_section_count_list(const Output_data_list* pdl) const
4336 unsigned int count = 0;
4337 for (Output_data_list::const_iterator p = pdl->begin();
4341 if ((*p)->is_section())
4347 // Return the section attached to the list segment with the lowest
4348 // load address. This is used when handling a PHDRS clause in a
4352 Output_segment::section_with_lowest_load_address() const
4354 Output_section* found = NULL;
4355 uint64_t found_lma = 0;
4356 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4357 this->lowest_load_address_in_list(&this->output_lists_[i], &found,
4362 // Look through a list for a section with a lower load address.
4365 Output_segment::lowest_load_address_in_list(const Output_data_list* pdl,
4366 Output_section** found,
4367 uint64_t* found_lma) const
4369 for (Output_data_list::const_iterator p = pdl->begin();
4373 if (!(*p)->is_section())
4375 Output_section* os = static_cast<Output_section*>(*p);
4376 uint64_t lma = (os->has_load_address()
4377 ? os->load_address()
4379 if (*found == NULL || lma < *found_lma)
4387 // Write the segment data into *OPHDR.
4389 template<int size, bool big_endian>
4391 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
4393 ophdr->put_p_type(this->type_);
4394 ophdr->put_p_offset(this->offset_);
4395 ophdr->put_p_vaddr(this->vaddr_);
4396 ophdr->put_p_paddr(this->paddr_);
4397 ophdr->put_p_filesz(this->filesz_);
4398 ophdr->put_p_memsz(this->memsz_);
4399 ophdr->put_p_flags(this->flags_);
4400 ophdr->put_p_align(std::max(this->min_p_align_, this->maximum_alignment()));
4403 // Write the section headers into V.
4405 template<int size, bool big_endian>
4407 Output_segment::write_section_headers(const Layout* layout,
4408 const Stringpool* secnamepool,
4410 unsigned int* pshndx) const
4412 // Every section that is attached to a segment must be attached to a
4413 // PT_LOAD segment, so we only write out section headers for PT_LOAD
4415 if (this->type_ != elfcpp::PT_LOAD)
4418 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4420 const Output_data_list* pdl = &this->output_lists_[i];
4421 v = this->write_section_headers_list<size, big_endian>(layout,
4430 template<int size, bool big_endian>
4432 Output_segment::write_section_headers_list(const Layout* layout,
4433 const Stringpool* secnamepool,
4434 const Output_data_list* pdl,
4436 unsigned int* pshndx) const
4438 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
4439 for (Output_data_list::const_iterator p = pdl->begin();
4443 if ((*p)->is_section())
4445 const Output_section* ps = static_cast<const Output_section*>(*p);
4446 gold_assert(*pshndx == ps->out_shndx());
4447 elfcpp::Shdr_write<size, big_endian> oshdr(v);
4448 ps->write_header(layout, secnamepool, &oshdr);
4456 // Print the output sections to the map file.
4459 Output_segment::print_sections_to_mapfile(Mapfile* mapfile) const
4461 if (this->type() != elfcpp::PT_LOAD)
4463 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4464 this->print_section_list_to_mapfile(mapfile, &this->output_lists_[i]);
4467 // Print an output section list to the map file.
4470 Output_segment::print_section_list_to_mapfile(Mapfile* mapfile,
4471 const Output_data_list* pdl) const
4473 for (Output_data_list::const_iterator p = pdl->begin();
4476 (*p)->print_to_mapfile(mapfile);
4479 // Output_file methods.
4481 Output_file::Output_file(const char* name)
4486 map_is_anonymous_(false),
4487 map_is_allocated_(false),
4488 is_temporary_(false)
4492 // Try to open an existing file. Returns false if the file doesn't
4493 // exist, has a size of 0 or can't be mmapped.
4496 Output_file::open_for_modification()
4498 // The name "-" means "stdout".
4499 if (strcmp(this->name_, "-") == 0)
4502 // Don't bother opening files with a size of zero.
4504 if (::stat(this->name_, &s) != 0 || s.st_size == 0)
4507 int o = open_descriptor(-1, this->name_, O_RDWR, 0);
4509 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
4511 this->file_size_ = s.st_size;
4513 // If the file can't be mmapped, copying the content to an anonymous
4514 // map will probably negate the performance benefits of incremental
4515 // linking. This could be helped by using views and loading only
4516 // the necessary parts, but this is not supported as of now.
4517 if (!this->map_no_anonymous())
4519 release_descriptor(o, true);
4521 this->file_size_ = 0;
4528 // Open the output file.
4531 Output_file::open(off_t file_size)
4533 this->file_size_ = file_size;
4535 // Unlink the file first; otherwise the open() may fail if the file
4536 // is busy (e.g. it's an executable that's currently being executed).
4538 // However, the linker may be part of a system where a zero-length
4539 // file is created for it to write to, with tight permissions (gcc
4540 // 2.95 did something like this). Unlinking the file would work
4541 // around those permission controls, so we only unlink if the file
4542 // has a non-zero size. We also unlink only regular files to avoid
4543 // trouble with directories/etc.
4545 // If we fail, continue; this command is merely a best-effort attempt
4546 // to improve the odds for open().
4548 // We let the name "-" mean "stdout"
4549 if (!this->is_temporary_)
4551 if (strcmp(this->name_, "-") == 0)
4552 this->o_ = STDOUT_FILENO;
4556 if (::stat(this->name_, &s) == 0
4557 && (S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
4560 ::unlink(this->name_);
4561 else if (!parameters->options().relocatable())
4563 // If we don't unlink the existing file, add execute
4564 // permission where read permissions already exist
4565 // and where the umask permits.
4566 int mask = ::umask(0);
4568 s.st_mode |= (s.st_mode & 0444) >> 2;
4569 ::chmod(this->name_, s.st_mode & ~mask);
4573 int mode = parameters->options().relocatable() ? 0666 : 0777;
4574 int o = open_descriptor(-1, this->name_, O_RDWR | O_CREAT | O_TRUNC,
4577 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
4585 // Resize the output file.
4588 Output_file::resize(off_t file_size)
4590 // If the mmap is mapping an anonymous memory buffer, this is easy:
4591 // just mremap to the new size. If it's mapping to a file, we want
4592 // to unmap to flush to the file, then remap after growing the file.
4593 if (this->map_is_anonymous_)
4596 if (!this->map_is_allocated_)
4598 base = ::mremap(this->base_, this->file_size_, file_size,
4600 if (base == MAP_FAILED)
4601 gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
4605 base = realloc(this->base_, file_size);
4608 if (file_size > this->file_size_)
4609 memset(static_cast<char*>(base) + this->file_size_, 0,
4610 file_size - this->file_size_);
4612 this->base_ = static_cast<unsigned char*>(base);
4613 this->file_size_ = file_size;
4618 this->file_size_ = file_size;
4619 if (!this->map_no_anonymous())
4620 gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
4624 // Map an anonymous block of memory which will later be written to the
4625 // file. Return whether the map succeeded.
4628 Output_file::map_anonymous()
4630 void* base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
4631 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
4632 if (base == MAP_FAILED)
4634 base = malloc(this->file_size_);
4637 memset(base, 0, this->file_size_);
4638 this->map_is_allocated_ = true;
4640 this->base_ = static_cast<unsigned char*>(base);
4641 this->map_is_anonymous_ = true;
4645 // Map the file into memory. Return whether the mapping succeeded.
4648 Output_file::map_no_anonymous()
4650 const int o = this->o_;
4652 // If the output file is not a regular file, don't try to mmap it;
4653 // instead, we'll mmap a block of memory (an anonymous buffer), and
4654 // then later write the buffer to the file.
4656 struct stat statbuf;
4657 if (o == STDOUT_FILENO || o == STDERR_FILENO
4658 || ::fstat(o, &statbuf) != 0
4659 || !S_ISREG(statbuf.st_mode)
4660 || this->is_temporary_)
4663 // Ensure that we have disk space available for the file. If we
4664 // don't do this, it is possible that we will call munmap, close,
4665 // and exit with dirty buffers still in the cache with no assigned
4666 // disk blocks. If the disk is out of space at that point, the
4667 // output file will wind up incomplete, but we will have already
4668 // exited. The alternative to fallocate would be to use fdatasync,
4669 // but that would be a more significant performance hit.
4670 if (::posix_fallocate(o, 0, this->file_size_) < 0)
4671 gold_fatal(_("%s: %s"), this->name_, strerror(errno));
4673 // Map the file into memory.
4674 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
4677 // The mmap call might fail because of file system issues: the file
4678 // system might not support mmap at all, or it might not support
4679 // mmap with PROT_WRITE.
4680 if (base == MAP_FAILED)
4683 this->map_is_anonymous_ = false;
4684 this->base_ = static_cast<unsigned char*>(base);
4688 // Map the file into memory.
4693 if (this->map_no_anonymous())
4696 // The mmap call might fail because of file system issues: the file
4697 // system might not support mmap at all, or it might not support
4698 // mmap with PROT_WRITE. I'm not sure which errno values we will
4699 // see in all cases, so if the mmap fails for any reason and we
4700 // don't care about file contents, try for an anonymous map.
4701 if (this->map_anonymous())
4704 gold_fatal(_("%s: mmap: failed to allocate %lu bytes for output file: %s"),
4705 this->name_, static_cast<unsigned long>(this->file_size_),
4709 // Unmap the file from memory.
4712 Output_file::unmap()
4714 if (this->map_is_anonymous_)
4716 // We've already written out the data, so there is no reason to
4717 // waste time unmapping or freeing the memory.
4721 if (::munmap(this->base_, this->file_size_) < 0)
4722 gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
4727 // Close the output file.
4730 Output_file::close()
4732 // If the map isn't file-backed, we need to write it now.
4733 if (this->map_is_anonymous_ && !this->is_temporary_)
4735 size_t bytes_to_write = this->file_size_;
4737 while (bytes_to_write > 0)
4739 ssize_t bytes_written = ::write(this->o_, this->base_ + offset,
4741 if (bytes_written == 0)
4742 gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
4743 else if (bytes_written < 0)
4744 gold_error(_("%s: write: %s"), this->name_, strerror(errno));
4747 bytes_to_write -= bytes_written;
4748 offset += bytes_written;
4754 // We don't close stdout or stderr
4755 if (this->o_ != STDOUT_FILENO
4756 && this->o_ != STDERR_FILENO
4757 && !this->is_temporary_)
4758 if (::close(this->o_) < 0)
4759 gold_error(_("%s: close: %s"), this->name_, strerror(errno));
4763 // Instantiate the templates we need. We could use the configure
4764 // script to restrict this to only the ones for implemented targets.
4766 #ifdef HAVE_TARGET_32_LITTLE
4769 Output_section::add_input_section<32, false>(
4771 Sized_relobj<32, false>* object,
4773 const char* secname,
4774 const elfcpp::Shdr<32, false>& shdr,
4775 unsigned int reloc_shndx,
4776 bool have_sections_script);
4779 #ifdef HAVE_TARGET_32_BIG
4782 Output_section::add_input_section<32, true>(
4784 Sized_relobj<32, true>* object,
4786 const char* secname,
4787 const elfcpp::Shdr<32, true>& shdr,
4788 unsigned int reloc_shndx,
4789 bool have_sections_script);
4792 #ifdef HAVE_TARGET_64_LITTLE
4795 Output_section::add_input_section<64, false>(
4797 Sized_relobj<64, false>* object,
4799 const char* secname,
4800 const elfcpp::Shdr<64, false>& shdr,
4801 unsigned int reloc_shndx,
4802 bool have_sections_script);
4805 #ifdef HAVE_TARGET_64_BIG
4808 Output_section::add_input_section<64, true>(
4810 Sized_relobj<64, true>* object,
4812 const char* secname,
4813 const elfcpp::Shdr<64, true>& shdr,
4814 unsigned int reloc_shndx,
4815 bool have_sections_script);
4818 #ifdef HAVE_TARGET_32_LITTLE
4820 class Output_reloc<elfcpp::SHT_REL, false, 32, false>;
4823 #ifdef HAVE_TARGET_32_BIG
4825 class Output_reloc<elfcpp::SHT_REL, false, 32, true>;
4828 #ifdef HAVE_TARGET_64_LITTLE
4830 class Output_reloc<elfcpp::SHT_REL, false, 64, false>;
4833 #ifdef HAVE_TARGET_64_BIG
4835 class Output_reloc<elfcpp::SHT_REL, false, 64, true>;
4838 #ifdef HAVE_TARGET_32_LITTLE
4840 class Output_reloc<elfcpp::SHT_REL, true, 32, false>;
4843 #ifdef HAVE_TARGET_32_BIG
4845 class Output_reloc<elfcpp::SHT_REL, true, 32, true>;
4848 #ifdef HAVE_TARGET_64_LITTLE
4850 class Output_reloc<elfcpp::SHT_REL, true, 64, false>;
4853 #ifdef HAVE_TARGET_64_BIG
4855 class Output_reloc<elfcpp::SHT_REL, true, 64, true>;
4858 #ifdef HAVE_TARGET_32_LITTLE
4860 class Output_reloc<elfcpp::SHT_RELA, false, 32, false>;
4863 #ifdef HAVE_TARGET_32_BIG
4865 class Output_reloc<elfcpp::SHT_RELA, false, 32, true>;
4868 #ifdef HAVE_TARGET_64_LITTLE
4870 class Output_reloc<elfcpp::SHT_RELA, false, 64, false>;
4873 #ifdef HAVE_TARGET_64_BIG
4875 class Output_reloc<elfcpp::SHT_RELA, false, 64, true>;
4878 #ifdef HAVE_TARGET_32_LITTLE
4880 class Output_reloc<elfcpp::SHT_RELA, true, 32, false>;
4883 #ifdef HAVE_TARGET_32_BIG
4885 class Output_reloc<elfcpp::SHT_RELA, true, 32, true>;
4888 #ifdef HAVE_TARGET_64_LITTLE
4890 class Output_reloc<elfcpp::SHT_RELA, true, 64, false>;
4893 #ifdef HAVE_TARGET_64_BIG
4895 class Output_reloc<elfcpp::SHT_RELA, true, 64, true>;
4898 #ifdef HAVE_TARGET_32_LITTLE
4900 class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
4903 #ifdef HAVE_TARGET_32_BIG
4905 class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
4908 #ifdef HAVE_TARGET_64_LITTLE
4910 class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
4913 #ifdef HAVE_TARGET_64_BIG
4915 class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
4918 #ifdef HAVE_TARGET_32_LITTLE
4920 class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
4923 #ifdef HAVE_TARGET_32_BIG
4925 class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
4928 #ifdef HAVE_TARGET_64_LITTLE
4930 class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
4933 #ifdef HAVE_TARGET_64_BIG
4935 class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
4938 #ifdef HAVE_TARGET_32_LITTLE
4940 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
4943 #ifdef HAVE_TARGET_32_BIG
4945 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
4948 #ifdef HAVE_TARGET_64_LITTLE
4950 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
4953 #ifdef HAVE_TARGET_64_BIG
4955 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
4958 #ifdef HAVE_TARGET_32_LITTLE
4960 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
4963 #ifdef HAVE_TARGET_32_BIG
4965 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
4968 #ifdef HAVE_TARGET_64_LITTLE
4970 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
4973 #ifdef HAVE_TARGET_64_BIG
4975 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
4978 #ifdef HAVE_TARGET_32_LITTLE
4980 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, false>;
4983 #ifdef HAVE_TARGET_32_BIG
4985 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, true>;
4988 #ifdef HAVE_TARGET_64_LITTLE
4990 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, false>;
4993 #ifdef HAVE_TARGET_64_BIG
4995 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, true>;
4998 #ifdef HAVE_TARGET_32_LITTLE
5000 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, false>;
5003 #ifdef HAVE_TARGET_32_BIG
5005 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, true>;
5008 #ifdef HAVE_TARGET_64_LITTLE
5010 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, false>;
5013 #ifdef HAVE_TARGET_64_BIG
5015 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, true>;
5018 #ifdef HAVE_TARGET_32_LITTLE
5020 class Output_data_group<32, false>;
5023 #ifdef HAVE_TARGET_32_BIG
5025 class Output_data_group<32, true>;
5028 #ifdef HAVE_TARGET_64_LITTLE
5030 class Output_data_group<64, false>;
5033 #ifdef HAVE_TARGET_64_BIG
5035 class Output_data_group<64, true>;
5038 #ifdef HAVE_TARGET_32_LITTLE
5040 class Output_data_got<32, false>;
5043 #ifdef HAVE_TARGET_32_BIG
5045 class Output_data_got<32, true>;
5048 #ifdef HAVE_TARGET_64_LITTLE
5050 class Output_data_got<64, false>;
5053 #ifdef HAVE_TARGET_64_BIG
5055 class Output_data_got<64, true>;
5058 } // End namespace gold.