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;
1362 // If we're doing an incremental update, don't touch this GOT entry.
1363 if (parameters->incremental_update())
1365 val = this->u_.constant;
1370 const Sized_relobj<size, big_endian>* object = this->u_.object;
1371 const unsigned int lsi = this->local_sym_index_;
1372 const Symbol_value<size>* symval = object->local_symbol(lsi);
1373 if (!this->use_plt_offset_)
1374 val = symval->value(this->u_.object, 0);
1377 const Output_data* plt =
1378 parameters->target().plt_section_for_local(object, lsi);
1379 val = plt->address() + object->local_plt_offset(lsi);
1385 elfcpp::Swap<size, big_endian>::writeval(pov, val);
1388 // Output_data_got methods.
1390 // Add an entry for a global symbol to the GOT. This returns true if
1391 // this is a new GOT entry, false if the symbol already had a GOT
1394 template<int size, bool big_endian>
1396 Output_data_got<size, big_endian>::add_global(
1398 unsigned int got_type)
1400 if (gsym->has_got_offset(got_type))
1403 unsigned int got_offset = this->add_got_entry(Got_entry(gsym, false));
1404 gsym->set_got_offset(got_type, got_offset);
1408 // Like add_global, but use the PLT offset.
1410 template<int size, bool big_endian>
1412 Output_data_got<size, big_endian>::add_global_plt(Symbol* gsym,
1413 unsigned int got_type)
1415 if (gsym->has_got_offset(got_type))
1418 unsigned int got_offset = this->add_got_entry(Got_entry(gsym, true));
1419 gsym->set_got_offset(got_type, got_offset);
1423 // Add an entry for a global symbol to the GOT, and add a dynamic
1424 // relocation of type R_TYPE for the GOT entry.
1426 template<int size, bool big_endian>
1428 Output_data_got<size, big_endian>::add_global_with_rel(
1430 unsigned int got_type,
1432 unsigned int r_type)
1434 if (gsym->has_got_offset(got_type))
1437 unsigned int got_offset = this->add_got_entry(Got_entry());
1438 gsym->set_got_offset(got_type, got_offset);
1439 rel_dyn->add_global(gsym, r_type, this, got_offset);
1442 template<int size, bool big_endian>
1444 Output_data_got<size, big_endian>::add_global_with_rela(
1446 unsigned int got_type,
1448 unsigned int r_type)
1450 if (gsym->has_got_offset(got_type))
1453 unsigned int got_offset = this->add_got_entry(Got_entry());
1454 gsym->set_got_offset(got_type, got_offset);
1455 rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
1458 // Add a pair of entries for a global symbol to the GOT, and add
1459 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1460 // If R_TYPE_2 == 0, add the second entry with no relocation.
1461 template<int size, bool big_endian>
1463 Output_data_got<size, big_endian>::add_global_pair_with_rel(
1465 unsigned int got_type,
1467 unsigned int r_type_1,
1468 unsigned int r_type_2)
1470 if (gsym->has_got_offset(got_type))
1473 unsigned int got_offset = this->add_got_entry_pair(Got_entry(), Got_entry());
1474 gsym->set_got_offset(got_type, got_offset);
1475 rel_dyn->add_global(gsym, r_type_1, this, got_offset);
1478 rel_dyn->add_global(gsym, r_type_2, this, got_offset + size / 8);
1481 template<int size, bool big_endian>
1483 Output_data_got<size, big_endian>::add_global_pair_with_rela(
1485 unsigned int got_type,
1487 unsigned int r_type_1,
1488 unsigned int r_type_2)
1490 if (gsym->has_got_offset(got_type))
1493 unsigned int got_offset = this->add_got_entry_pair(Got_entry(), Got_entry());
1494 gsym->set_got_offset(got_type, got_offset);
1495 rela_dyn->add_global(gsym, r_type_1, this, got_offset, 0);
1498 rela_dyn->add_global(gsym, r_type_2, this, got_offset + size / 8, 0);
1501 // Add an entry for a local symbol to the GOT. This returns true if
1502 // this is a new GOT entry, false if the symbol already has a GOT
1505 template<int size, bool big_endian>
1507 Output_data_got<size, big_endian>::add_local(
1508 Sized_relobj<size, big_endian>* object,
1509 unsigned int symndx,
1510 unsigned int got_type)
1512 if (object->local_has_got_offset(symndx, got_type))
1515 unsigned int got_offset = this->add_got_entry(Got_entry(object, symndx,
1517 object->set_local_got_offset(symndx, got_type, got_offset);
1521 // Like add_local, but use the PLT offset.
1523 template<int size, bool big_endian>
1525 Output_data_got<size, big_endian>::add_local_plt(
1526 Sized_relobj<size, big_endian>* object,
1527 unsigned int symndx,
1528 unsigned int got_type)
1530 if (object->local_has_got_offset(symndx, got_type))
1533 unsigned int got_offset = this->add_got_entry(Got_entry(object, symndx,
1535 object->set_local_got_offset(symndx, got_type, got_offset);
1539 // Add an entry for a local symbol to the GOT, and add a dynamic
1540 // relocation of type R_TYPE for the GOT entry.
1542 template<int size, bool big_endian>
1544 Output_data_got<size, big_endian>::add_local_with_rel(
1545 Sized_relobj<size, big_endian>* object,
1546 unsigned int symndx,
1547 unsigned int got_type,
1549 unsigned int r_type)
1551 if (object->local_has_got_offset(symndx, got_type))
1554 unsigned int got_offset = this->add_got_entry(Got_entry());
1555 object->set_local_got_offset(symndx, got_type, got_offset);
1556 rel_dyn->add_local(object, symndx, r_type, this, got_offset);
1559 template<int size, bool big_endian>
1561 Output_data_got<size, big_endian>::add_local_with_rela(
1562 Sized_relobj<size, big_endian>* object,
1563 unsigned int symndx,
1564 unsigned int got_type,
1566 unsigned int r_type)
1568 if (object->local_has_got_offset(symndx, got_type))
1571 unsigned int got_offset = this->add_got_entry(Got_entry());
1572 object->set_local_got_offset(symndx, got_type, got_offset);
1573 rela_dyn->add_local(object, symndx, r_type, this, got_offset, 0);
1576 // Add a pair of entries for a local symbol to the GOT, and add
1577 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1578 // If R_TYPE_2 == 0, add the second entry with no relocation.
1579 template<int size, bool big_endian>
1581 Output_data_got<size, big_endian>::add_local_pair_with_rel(
1582 Sized_relobj<size, big_endian>* object,
1583 unsigned int symndx,
1585 unsigned int got_type,
1587 unsigned int r_type_1,
1588 unsigned int r_type_2)
1590 if (object->local_has_got_offset(symndx, got_type))
1593 unsigned int got_offset =
1594 this->add_got_entry_pair(Got_entry(),
1595 Got_entry(object, symndx, false));
1596 object->set_local_got_offset(symndx, got_type, got_offset);
1597 Output_section* os = object->output_section(shndx);
1598 rel_dyn->add_output_section(os, r_type_1, this, got_offset);
1601 rel_dyn->add_output_section(os, r_type_2, this, got_offset + size / 8);
1604 template<int size, bool big_endian>
1606 Output_data_got<size, big_endian>::add_local_pair_with_rela(
1607 Sized_relobj<size, big_endian>* object,
1608 unsigned int symndx,
1610 unsigned int got_type,
1612 unsigned int r_type_1,
1613 unsigned int r_type_2)
1615 if (object->local_has_got_offset(symndx, got_type))
1618 unsigned int got_offset =
1619 this->add_got_entry_pair(Got_entry(),
1620 Got_entry(object, symndx, false));
1621 object->set_local_got_offset(symndx, got_type, got_offset);
1622 Output_section* os = object->output_section(shndx);
1623 rela_dyn->add_output_section(os, r_type_1, this, got_offset, 0);
1626 rela_dyn->add_output_section(os, r_type_2, this, got_offset + size / 8, 0);
1629 // Reserve a slot in the GOT for a local symbol or the second slot of a pair.
1631 template<int size, bool big_endian>
1633 Output_data_got<size, big_endian>::reserve_slot(unsigned int i)
1635 this->free_list_.remove(i * size / 8, (i + 1) * size / 8);
1638 // Reserve a slot in the GOT for a global symbol.
1640 template<int size, bool big_endian>
1642 Output_data_got<size, big_endian>::reserve_slot_for_global(
1645 unsigned int got_type)
1647 this->free_list_.remove(i * size / 8, (i + 1) * size / 8);
1648 gsym->set_got_offset(got_type, this->got_offset(i));
1651 // Write out the GOT.
1653 template<int size, bool big_endian>
1655 Output_data_got<size, big_endian>::do_write(Output_file* of)
1657 const int add = size / 8;
1659 const off_t off = this->offset();
1660 const off_t oview_size = this->data_size();
1661 unsigned char* const oview = of->get_output_view(off, oview_size);
1663 unsigned char* pov = oview;
1664 for (typename Got_entries::const_iterator p = this->entries_.begin();
1665 p != this->entries_.end();
1672 gold_assert(pov - oview == oview_size);
1674 of->write_output_view(off, oview_size, oview);
1676 // We no longer need the GOT entries.
1677 this->entries_.clear();
1680 // Create a new GOT entry and return its offset.
1682 template<int size, bool big_endian>
1684 Output_data_got<size, big_endian>::add_got_entry(Got_entry got_entry)
1686 if (!this->is_data_size_valid())
1688 this->entries_.push_back(got_entry);
1689 this->set_got_size();
1690 return this->last_got_offset();
1694 // For an incremental update, find an available slot.
1695 off_t got_offset = this->free_list_.allocate(size / 8, size / 8, 0);
1696 if (got_offset == -1)
1697 gold_fatal(_("out of patch space (GOT);"
1698 " relink with --incremental-full"));
1699 unsigned int got_index = got_offset / (size / 8);
1700 gold_assert(got_index < this->entries_.size());
1701 this->entries_[got_index] = got_entry;
1702 return static_cast<unsigned int>(got_offset);
1706 // Create a pair of new GOT entries and return the offset of the first.
1708 template<int size, bool big_endian>
1710 Output_data_got<size, big_endian>::add_got_entry_pair(Got_entry got_entry_1,
1711 Got_entry got_entry_2)
1713 if (!this->is_data_size_valid())
1715 unsigned int got_offset;
1716 this->entries_.push_back(got_entry_1);
1717 got_offset = this->last_got_offset();
1718 this->entries_.push_back(got_entry_2);
1719 this->set_got_size();
1724 // For an incremental update, find an available pair of slots.
1725 off_t got_offset = this->free_list_.allocate(2 * size / 8, size / 8, 0);
1726 if (got_offset == -1)
1727 gold_fatal(_("out of patch space (GOT);"
1728 " relink with --incremental-full"));
1729 unsigned int got_index = got_offset / (size / 8);
1730 gold_assert(got_index < this->entries_.size());
1731 this->entries_[got_index] = got_entry_1;
1732 this->entries_[got_index + 1] = got_entry_2;
1733 return static_cast<unsigned int>(got_offset);
1737 // Output_data_dynamic::Dynamic_entry methods.
1739 // Write out the entry.
1741 template<int size, bool big_endian>
1743 Output_data_dynamic::Dynamic_entry::write(
1745 const Stringpool* pool) const
1747 typename elfcpp::Elf_types<size>::Elf_WXword val;
1748 switch (this->offset_)
1750 case DYNAMIC_NUMBER:
1754 case DYNAMIC_SECTION_SIZE:
1755 val = this->u_.od->data_size();
1756 if (this->od2 != NULL)
1757 val += this->od2->data_size();
1760 case DYNAMIC_SYMBOL:
1762 const Sized_symbol<size>* s =
1763 static_cast<const Sized_symbol<size>*>(this->u_.sym);
1768 case DYNAMIC_STRING:
1769 val = pool->get_offset(this->u_.str);
1773 val = this->u_.od->address() + this->offset_;
1777 elfcpp::Dyn_write<size, big_endian> dw(pov);
1778 dw.put_d_tag(this->tag_);
1782 // Output_data_dynamic methods.
1784 // Adjust the output section to set the entry size.
1787 Output_data_dynamic::do_adjust_output_section(Output_section* os)
1789 if (parameters->target().get_size() == 32)
1790 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
1791 else if (parameters->target().get_size() == 64)
1792 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1797 // Set the final data size.
1800 Output_data_dynamic::set_final_data_size()
1802 // Add the terminating entry if it hasn't been added.
1803 // Because of relaxation, we can run this multiple times.
1804 if (this->entries_.empty() || this->entries_.back().tag() != elfcpp::DT_NULL)
1806 int extra = parameters->options().spare_dynamic_tags();
1807 for (int i = 0; i < extra; ++i)
1808 this->add_constant(elfcpp::DT_NULL, 0);
1809 this->add_constant(elfcpp::DT_NULL, 0);
1813 if (parameters->target().get_size() == 32)
1814 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
1815 else if (parameters->target().get_size() == 64)
1816 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1819 this->set_data_size(this->entries_.size() * dyn_size);
1822 // Write out the dynamic entries.
1825 Output_data_dynamic::do_write(Output_file* of)
1827 switch (parameters->size_and_endianness())
1829 #ifdef HAVE_TARGET_32_LITTLE
1830 case Parameters::TARGET_32_LITTLE:
1831 this->sized_write<32, false>(of);
1834 #ifdef HAVE_TARGET_32_BIG
1835 case Parameters::TARGET_32_BIG:
1836 this->sized_write<32, true>(of);
1839 #ifdef HAVE_TARGET_64_LITTLE
1840 case Parameters::TARGET_64_LITTLE:
1841 this->sized_write<64, false>(of);
1844 #ifdef HAVE_TARGET_64_BIG
1845 case Parameters::TARGET_64_BIG:
1846 this->sized_write<64, true>(of);
1854 template<int size, bool big_endian>
1856 Output_data_dynamic::sized_write(Output_file* of)
1858 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1860 const off_t offset = this->offset();
1861 const off_t oview_size = this->data_size();
1862 unsigned char* const oview = of->get_output_view(offset, oview_size);
1864 unsigned char* pov = oview;
1865 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1866 p != this->entries_.end();
1869 p->write<size, big_endian>(pov, this->pool_);
1873 gold_assert(pov - oview == oview_size);
1875 of->write_output_view(offset, oview_size, oview);
1877 // We no longer need the dynamic entries.
1878 this->entries_.clear();
1881 // Class Output_symtab_xindex.
1884 Output_symtab_xindex::do_write(Output_file* of)
1886 const off_t offset = this->offset();
1887 const off_t oview_size = this->data_size();
1888 unsigned char* const oview = of->get_output_view(offset, oview_size);
1890 memset(oview, 0, oview_size);
1892 if (parameters->target().is_big_endian())
1893 this->endian_do_write<true>(oview);
1895 this->endian_do_write<false>(oview);
1897 of->write_output_view(offset, oview_size, oview);
1899 // We no longer need the data.
1900 this->entries_.clear();
1903 template<bool big_endian>
1905 Output_symtab_xindex::endian_do_write(unsigned char* const oview)
1907 for (Xindex_entries::const_iterator p = this->entries_.begin();
1908 p != this->entries_.end();
1911 unsigned int symndx = p->first;
1912 gold_assert(symndx * 4 < this->data_size());
1913 elfcpp::Swap<32, big_endian>::writeval(oview + symndx * 4, p->second);
1917 // Output_section::Input_section methods.
1919 // Return the current data size. For an input section we store the size here.
1920 // For an Output_section_data, we have to ask it for the size.
1923 Output_section::Input_section::current_data_size() const
1925 if (this->is_input_section())
1926 return this->u1_.data_size;
1929 this->u2_.posd->pre_finalize_data_size();
1930 return this->u2_.posd->current_data_size();
1934 // Return the data size. For an input section we store the size here.
1935 // For an Output_section_data, we have to ask it for the size.
1938 Output_section::Input_section::data_size() const
1940 if (this->is_input_section())
1941 return this->u1_.data_size;
1943 return this->u2_.posd->data_size();
1946 // Return the object for an input section.
1949 Output_section::Input_section::relobj() const
1951 if (this->is_input_section())
1952 return this->u2_.object;
1953 else if (this->is_merge_section())
1955 gold_assert(this->u2_.pomb->first_relobj() != NULL);
1956 return this->u2_.pomb->first_relobj();
1958 else if (this->is_relaxed_input_section())
1959 return this->u2_.poris->relobj();
1964 // Return the input section index for an input section.
1967 Output_section::Input_section::shndx() const
1969 if (this->is_input_section())
1970 return this->shndx_;
1971 else if (this->is_merge_section())
1973 gold_assert(this->u2_.pomb->first_relobj() != NULL);
1974 return this->u2_.pomb->first_shndx();
1976 else if (this->is_relaxed_input_section())
1977 return this->u2_.poris->shndx();
1982 // Set the address and file offset.
1985 Output_section::Input_section::set_address_and_file_offset(
1988 off_t section_file_offset)
1990 if (this->is_input_section())
1991 this->u2_.object->set_section_offset(this->shndx_,
1992 file_offset - section_file_offset);
1994 this->u2_.posd->set_address_and_file_offset(address, file_offset);
1997 // Reset the address and file offset.
2000 Output_section::Input_section::reset_address_and_file_offset()
2002 if (!this->is_input_section())
2003 this->u2_.posd->reset_address_and_file_offset();
2006 // Finalize the data size.
2009 Output_section::Input_section::finalize_data_size()
2011 if (!this->is_input_section())
2012 this->u2_.posd->finalize_data_size();
2015 // Try to turn an input offset into an output offset. We want to
2016 // return the output offset relative to the start of this
2017 // Input_section in the output section.
2020 Output_section::Input_section::output_offset(
2021 const Relobj* object,
2023 section_offset_type offset,
2024 section_offset_type* poutput) const
2026 if (!this->is_input_section())
2027 return this->u2_.posd->output_offset(object, shndx, offset, poutput);
2030 if (this->shndx_ != shndx || this->u2_.object != object)
2037 // Return whether this is the merge section for the input section
2041 Output_section::Input_section::is_merge_section_for(const Relobj* object,
2042 unsigned int shndx) const
2044 if (this->is_input_section())
2046 return this->u2_.posd->is_merge_section_for(object, shndx);
2049 // Write out the data. We don't have to do anything for an input
2050 // section--they are handled via Object::relocate--but this is where
2051 // we write out the data for an Output_section_data.
2054 Output_section::Input_section::write(Output_file* of)
2056 if (!this->is_input_section())
2057 this->u2_.posd->write(of);
2060 // Write the data to a buffer. As for write(), we don't have to do
2061 // anything for an input section.
2064 Output_section::Input_section::write_to_buffer(unsigned char* buffer)
2066 if (!this->is_input_section())
2067 this->u2_.posd->write_to_buffer(buffer);
2070 // Print to a map file.
2073 Output_section::Input_section::print_to_mapfile(Mapfile* mapfile) const
2075 switch (this->shndx_)
2077 case OUTPUT_SECTION_CODE:
2078 case MERGE_DATA_SECTION_CODE:
2079 case MERGE_STRING_SECTION_CODE:
2080 this->u2_.posd->print_to_mapfile(mapfile);
2083 case RELAXED_INPUT_SECTION_CODE:
2085 Output_relaxed_input_section* relaxed_section =
2086 this->relaxed_input_section();
2087 mapfile->print_input_section(relaxed_section->relobj(),
2088 relaxed_section->shndx());
2092 mapfile->print_input_section(this->u2_.object, this->shndx_);
2097 // Output_section methods.
2099 // Construct an Output_section. NAME will point into a Stringpool.
2101 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
2102 elfcpp::Elf_Xword flags)
2107 link_section_(NULL),
2109 info_section_(NULL),
2114 order_(ORDER_INVALID),
2119 first_input_offset_(0),
2121 postprocessing_buffer_(NULL),
2122 needs_symtab_index_(false),
2123 needs_dynsym_index_(false),
2124 should_link_to_symtab_(false),
2125 should_link_to_dynsym_(false),
2126 after_input_sections_(false),
2127 requires_postprocessing_(false),
2128 found_in_sections_clause_(false),
2129 has_load_address_(false),
2130 info_uses_section_index_(false),
2131 input_section_order_specified_(false),
2132 may_sort_attached_input_sections_(false),
2133 must_sort_attached_input_sections_(false),
2134 attached_input_sections_are_sorted_(false),
2136 is_small_section_(false),
2137 is_large_section_(false),
2138 generate_code_fills_at_write_(false),
2139 is_entsize_zero_(false),
2140 section_offsets_need_adjustment_(false),
2142 always_keeps_input_sections_(false),
2143 has_fixed_layout_(false),
2146 lookup_maps_(new Output_section_lookup_maps),
2149 // An unallocated section has no address. Forcing this means that
2150 // we don't need special treatment for symbols defined in debug
2152 if ((flags & elfcpp::SHF_ALLOC) == 0)
2153 this->set_address(0);
2156 Output_section::~Output_section()
2158 delete this->checkpoint_;
2161 // Set the entry size.
2164 Output_section::set_entsize(uint64_t v)
2166 if (this->is_entsize_zero_)
2168 else if (this->entsize_ == 0)
2170 else if (this->entsize_ != v)
2173 this->is_entsize_zero_ = 1;
2177 // Add the input section SHNDX, with header SHDR, named SECNAME, in
2178 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
2179 // relocation section which applies to this section, or 0 if none, or
2180 // -1U if more than one. Return the offset of the input section
2181 // within the output section. Return -1 if the input section will
2182 // receive special handling. In the normal case we don't always keep
2183 // track of input sections for an Output_section. Instead, each
2184 // Object keeps track of the Output_section for each of its input
2185 // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
2186 // track of input sections here; this is used when SECTIONS appears in
2189 template<int size, bool big_endian>
2191 Output_section::add_input_section(Layout* layout,
2192 Sized_relobj<size, big_endian>* object,
2194 const char* secname,
2195 const elfcpp::Shdr<size, big_endian>& shdr,
2196 unsigned int reloc_shndx,
2197 bool have_sections_script)
2199 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
2200 if ((addralign & (addralign - 1)) != 0)
2202 object->error(_("invalid alignment %lu for section \"%s\""),
2203 static_cast<unsigned long>(addralign), secname);
2207 if (addralign > this->addralign_)
2208 this->addralign_ = addralign;
2210 typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
2211 uint64_t entsize = shdr.get_sh_entsize();
2213 // .debug_str is a mergeable string section, but is not always so
2214 // marked by compilers. Mark manually here so we can optimize.
2215 if (strcmp(secname, ".debug_str") == 0)
2217 sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
2221 this->update_flags_for_input_section(sh_flags);
2222 this->set_entsize(entsize);
2224 // If this is a SHF_MERGE section, we pass all the input sections to
2225 // a Output_data_merge. We don't try to handle relocations for such
2226 // a section. We don't try to handle empty merge sections--they
2227 // mess up the mappings, and are useless anyhow.
2228 // FIXME: Need to handle merge sections during incremental update.
2229 if ((sh_flags & elfcpp::SHF_MERGE) != 0
2231 && shdr.get_sh_size() > 0
2232 && !parameters->incremental())
2234 // Keep information about merged input sections for rebuilding fast
2235 // lookup maps if we have sections-script or we do relaxation.
2236 bool keeps_input_sections = (this->always_keeps_input_sections_
2237 || have_sections_script
2238 || parameters->target().may_relax());
2240 if (this->add_merge_input_section(object, shndx, sh_flags, entsize,
2241 addralign, keeps_input_sections))
2243 // Tell the relocation routines that they need to call the
2244 // output_offset method to determine the final address.
2249 section_size_type input_section_size = shdr.get_sh_size();
2250 section_size_type uncompressed_size;
2251 if (object->section_is_compressed(shndx, &uncompressed_size))
2252 input_section_size = uncompressed_size;
2254 off_t offset_in_section;
2255 off_t aligned_offset_in_section;
2256 if (this->has_fixed_layout())
2258 // For incremental updates, find a chunk of unused space in the section.
2259 offset_in_section = this->free_list_.allocate(input_section_size,
2261 if (offset_in_section == -1)
2262 gold_fatal(_("out of patch space; relink with --incremental-full"));
2263 aligned_offset_in_section = offset_in_section;
2267 offset_in_section = this->current_data_size_for_child();
2268 aligned_offset_in_section = align_address(offset_in_section,
2270 this->set_current_data_size_for_child(aligned_offset_in_section
2271 + input_section_size);
2274 // Determine if we want to delay code-fill generation until the output
2275 // section is written. When the target is relaxing, we want to delay fill
2276 // generating to avoid adjusting them during relaxation. Also, if we are
2277 // sorting input sections we must delay fill generation.
2278 if (!this->generate_code_fills_at_write_
2279 && !have_sections_script
2280 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
2281 && parameters->target().has_code_fill()
2282 && (parameters->target().may_relax()
2283 || parameters->options().section_ordering_file()))
2285 gold_assert(this->fills_.empty());
2286 this->generate_code_fills_at_write_ = true;
2289 if (aligned_offset_in_section > offset_in_section
2290 && !this->generate_code_fills_at_write_
2291 && !have_sections_script
2292 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
2293 && parameters->target().has_code_fill())
2295 // We need to add some fill data. Using fill_list_ when
2296 // possible is an optimization, since we will often have fill
2297 // sections without input sections.
2298 off_t fill_len = aligned_offset_in_section - offset_in_section;
2299 if (this->input_sections_.empty())
2300 this->fills_.push_back(Fill(offset_in_section, fill_len));
2303 std::string fill_data(parameters->target().code_fill(fill_len));
2304 Output_data_const* odc = new Output_data_const(fill_data, 1);
2305 this->input_sections_.push_back(Input_section(odc));
2309 // We need to keep track of this section if we are already keeping
2310 // track of sections, or if we are relaxing. Also, if this is a
2311 // section which requires sorting, or which may require sorting in
2312 // the future, we keep track of the sections. If the
2313 // --section-ordering-file option is used to specify the order of
2314 // sections, we need to keep track of sections.
2315 if (this->always_keeps_input_sections_
2316 || have_sections_script
2317 || !this->input_sections_.empty()
2318 || this->may_sort_attached_input_sections()
2319 || this->must_sort_attached_input_sections()
2320 || parameters->options().user_set_Map()
2321 || parameters->target().may_relax()
2322 || parameters->options().section_ordering_file())
2324 Input_section isecn(object, shndx, input_section_size, addralign);
2325 if (parameters->options().section_ordering_file())
2327 unsigned int section_order_index =
2328 layout->find_section_order_index(std::string(secname));
2329 if (section_order_index != 0)
2331 isecn.set_section_order_index(section_order_index);
2332 this->set_input_section_order_specified();
2335 if (this->has_fixed_layout())
2337 // For incremental updates, finalize the address and offset now.
2338 uint64_t addr = this->address();
2339 isecn.set_address_and_file_offset(addr + aligned_offset_in_section,
2340 aligned_offset_in_section,
2343 this->input_sections_.push_back(isecn);
2346 return aligned_offset_in_section;
2349 // Add arbitrary data to an output section.
2352 Output_section::add_output_section_data(Output_section_data* posd)
2354 Input_section inp(posd);
2355 this->add_output_section_data(&inp);
2357 if (posd->is_data_size_valid())
2359 off_t offset_in_section;
2360 if (this->has_fixed_layout())
2362 // For incremental updates, find a chunk of unused space.
2363 offset_in_section = this->free_list_.allocate(posd->data_size(),
2364 posd->addralign(), 0);
2365 if (offset_in_section == -1)
2366 gold_fatal(_("out of patch space; relink with --incremental-full"));
2367 // Finalize the address and offset now.
2368 uint64_t addr = this->address();
2369 off_t offset = this->offset();
2370 posd->set_address_and_file_offset(addr + offset_in_section,
2371 offset + offset_in_section);
2375 offset_in_section = this->current_data_size_for_child();
2376 off_t aligned_offset_in_section = align_address(offset_in_section,
2378 this->set_current_data_size_for_child(aligned_offset_in_section
2379 + posd->data_size());
2382 else if (this->has_fixed_layout())
2384 // For incremental updates, arrange for the data to have a fixed layout.
2385 // This will mean that additions to the data must be allocated from
2386 // free space within the containing output section.
2387 uint64_t addr = this->address();
2388 posd->set_address(addr);
2389 posd->set_file_offset(0);
2390 // FIXME: This should eventually be unreachable.
2391 // gold_unreachable();
2395 // Add a relaxed input section.
2398 Output_section::add_relaxed_input_section(Layout* layout,
2399 Output_relaxed_input_section* poris,
2400 const std::string& name)
2402 Input_section inp(poris);
2404 // If the --section-ordering-file option is used to specify the order of
2405 // sections, we need to keep track of sections.
2406 if (parameters->options().section_ordering_file())
2408 unsigned int section_order_index =
2409 layout->find_section_order_index(name);
2410 if (section_order_index != 0)
2412 inp.set_section_order_index(section_order_index);
2413 this->set_input_section_order_specified();
2417 this->add_output_section_data(&inp);
2418 if (this->lookup_maps_->is_valid())
2419 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2420 poris->shndx(), poris);
2422 // For a relaxed section, we use the current data size. Linker scripts
2423 // get all the input sections, including relaxed one from an output
2424 // section and add them back to them same output section to compute the
2425 // output section size. If we do not account for sizes of relaxed input
2426 // sections, an output section would be incorrectly sized.
2427 off_t offset_in_section = this->current_data_size_for_child();
2428 off_t aligned_offset_in_section = align_address(offset_in_section,
2429 poris->addralign());
2430 this->set_current_data_size_for_child(aligned_offset_in_section
2431 + poris->current_data_size());
2434 // Add arbitrary data to an output section by Input_section.
2437 Output_section::add_output_section_data(Input_section* inp)
2439 if (this->input_sections_.empty())
2440 this->first_input_offset_ = this->current_data_size_for_child();
2442 this->input_sections_.push_back(*inp);
2444 uint64_t addralign = inp->addralign();
2445 if (addralign > this->addralign_)
2446 this->addralign_ = addralign;
2448 inp->set_output_section(this);
2451 // Add a merge section to an output section.
2454 Output_section::add_output_merge_section(Output_section_data* posd,
2455 bool is_string, uint64_t entsize)
2457 Input_section inp(posd, is_string, entsize);
2458 this->add_output_section_data(&inp);
2461 // Add an input section to a SHF_MERGE section.
2464 Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
2465 uint64_t flags, uint64_t entsize,
2467 bool keeps_input_sections)
2469 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
2471 // We only merge strings if the alignment is not more than the
2472 // character size. This could be handled, but it's unusual.
2473 if (is_string && addralign > entsize)
2476 // We cannot restore merged input section states.
2477 gold_assert(this->checkpoint_ == NULL);
2479 // Look up merge sections by required properties.
2480 // Currently, we only invalidate the lookup maps in script processing
2481 // and relaxation. We should not have done either when we reach here.
2482 // So we assume that the lookup maps are valid to simply code.
2483 gold_assert(this->lookup_maps_->is_valid());
2484 Merge_section_properties msp(is_string, entsize, addralign);
2485 Output_merge_base* pomb = this->lookup_maps_->find_merge_section(msp);
2486 bool is_new = false;
2489 gold_assert(pomb->is_string() == is_string
2490 && pomb->entsize() == entsize
2491 && pomb->addralign() == addralign);
2495 // Create a new Output_merge_data or Output_merge_string_data.
2497 pomb = new Output_merge_data(entsize, addralign);
2503 pomb = new Output_merge_string<char>(addralign);
2506 pomb = new Output_merge_string<uint16_t>(addralign);
2509 pomb = new Output_merge_string<uint32_t>(addralign);
2515 // If we need to do script processing or relaxation, we need to keep
2516 // the original input sections to rebuild the fast lookup maps.
2517 if (keeps_input_sections)
2518 pomb->set_keeps_input_sections();
2522 if (pomb->add_input_section(object, shndx))
2524 // Add new merge section to this output section and link merge
2525 // section properties to new merge section in map.
2528 this->add_output_merge_section(pomb, is_string, entsize);
2529 this->lookup_maps_->add_merge_section(msp, pomb);
2532 // Add input section to new merge section and link input section to new
2533 // merge section in map.
2534 this->lookup_maps_->add_merge_input_section(object, shndx, pomb);
2539 // If add_input_section failed, delete new merge section to avoid
2540 // exporting empty merge sections in Output_section::get_input_section.
2547 // Build a relaxation map to speed up relaxation of existing input sections.
2548 // Look up to the first LIMIT elements in INPUT_SECTIONS.
2551 Output_section::build_relaxation_map(
2552 const Input_section_list& input_sections,
2554 Relaxation_map* relaxation_map) const
2556 for (size_t i = 0; i < limit; ++i)
2558 const Input_section& is(input_sections[i]);
2559 if (is.is_input_section() || is.is_relaxed_input_section())
2561 Section_id sid(is.relobj(), is.shndx());
2562 (*relaxation_map)[sid] = i;
2567 // Convert regular input sections in INPUT_SECTIONS into relaxed input
2568 // sections in RELAXED_SECTIONS. MAP is a prebuilt map from section id
2569 // indices of INPUT_SECTIONS.
2572 Output_section::convert_input_sections_in_list_to_relaxed_sections(
2573 const std::vector<Output_relaxed_input_section*>& relaxed_sections,
2574 const Relaxation_map& map,
2575 Input_section_list* input_sections)
2577 for (size_t i = 0; i < relaxed_sections.size(); ++i)
2579 Output_relaxed_input_section* poris = relaxed_sections[i];
2580 Section_id sid(poris->relobj(), poris->shndx());
2581 Relaxation_map::const_iterator p = map.find(sid);
2582 gold_assert(p != map.end());
2583 gold_assert((*input_sections)[p->second].is_input_section());
2585 // Remember section order index of original input section
2586 // if it is set. Copy it to the relaxed input section.
2588 (*input_sections)[p->second].section_order_index();
2589 (*input_sections)[p->second] = Input_section(poris);
2590 (*input_sections)[p->second].set_section_order_index(soi);
2594 // Convert regular input sections into relaxed input sections. RELAXED_SECTIONS
2595 // is a vector of pointers to Output_relaxed_input_section or its derived
2596 // classes. The relaxed sections must correspond to existing input sections.
2599 Output_section::convert_input_sections_to_relaxed_sections(
2600 const std::vector<Output_relaxed_input_section*>& relaxed_sections)
2602 gold_assert(parameters->target().may_relax());
2604 // We want to make sure that restore_states does not undo the effect of
2605 // this. If there is no checkpoint active, just search the current
2606 // input section list and replace the sections there. If there is
2607 // a checkpoint, also replace the sections there.
2609 // By default, we look at the whole list.
2610 size_t limit = this->input_sections_.size();
2612 if (this->checkpoint_ != NULL)
2614 // Replace input sections with relaxed input section in the saved
2615 // copy of the input section list.
2616 if (this->checkpoint_->input_sections_saved())
2619 this->build_relaxation_map(
2620 *(this->checkpoint_->input_sections()),
2621 this->checkpoint_->input_sections()->size(),
2623 this->convert_input_sections_in_list_to_relaxed_sections(
2626 this->checkpoint_->input_sections());
2630 // We have not copied the input section list yet. Instead, just
2631 // look at the portion that would be saved.
2632 limit = this->checkpoint_->input_sections_size();
2636 // Convert input sections in input_section_list.
2638 this->build_relaxation_map(this->input_sections_, limit, &map);
2639 this->convert_input_sections_in_list_to_relaxed_sections(
2642 &this->input_sections_);
2644 // Update fast look-up map.
2645 if (this->lookup_maps_->is_valid())
2646 for (size_t i = 0; i < relaxed_sections.size(); ++i)
2648 Output_relaxed_input_section* poris = relaxed_sections[i];
2649 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2650 poris->shndx(), poris);
2654 // Update the output section flags based on input section flags.
2657 Output_section::update_flags_for_input_section(elfcpp::Elf_Xword flags)
2659 // If we created the section with SHF_ALLOC clear, we set the
2660 // address. If we are now setting the SHF_ALLOC flag, we need to
2662 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0
2663 && (flags & elfcpp::SHF_ALLOC) != 0)
2664 this->mark_address_invalid();
2666 this->flags_ |= (flags
2667 & (elfcpp::SHF_WRITE
2669 | elfcpp::SHF_EXECINSTR));
2671 if ((flags & elfcpp::SHF_MERGE) == 0)
2672 this->flags_ &=~ elfcpp::SHF_MERGE;
2675 if (this->current_data_size_for_child() == 0)
2676 this->flags_ |= elfcpp::SHF_MERGE;
2679 if ((flags & elfcpp::SHF_STRINGS) == 0)
2680 this->flags_ &=~ elfcpp::SHF_STRINGS;
2683 if (this->current_data_size_for_child() == 0)
2684 this->flags_ |= elfcpp::SHF_STRINGS;
2688 // Find the merge section into which an input section with index SHNDX in
2689 // OBJECT has been added. Return NULL if none found.
2691 Output_section_data*
2692 Output_section::find_merge_section(const Relobj* object,
2693 unsigned int shndx) const
2695 if (!this->lookup_maps_->is_valid())
2696 this->build_lookup_maps();
2697 return this->lookup_maps_->find_merge_section(object, shndx);
2700 // Build the lookup maps for merge and relaxed sections. This is needs
2701 // to be declared as a const methods so that it is callable with a const
2702 // Output_section pointer. The method only updates states of the maps.
2705 Output_section::build_lookup_maps() const
2707 this->lookup_maps_->clear();
2708 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2709 p != this->input_sections_.end();
2712 if (p->is_merge_section())
2714 Output_merge_base* pomb = p->output_merge_base();
2715 Merge_section_properties msp(pomb->is_string(), pomb->entsize(),
2717 this->lookup_maps_->add_merge_section(msp, pomb);
2718 for (Output_merge_base::Input_sections::const_iterator is =
2719 pomb->input_sections_begin();
2720 is != pomb->input_sections_end();
2723 const Const_section_id& csid = *is;
2724 this->lookup_maps_->add_merge_input_section(csid.first,
2729 else if (p->is_relaxed_input_section())
2731 Output_relaxed_input_section* poris = p->relaxed_input_section();
2732 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2733 poris->shndx(), poris);
2738 // Find an relaxed input section corresponding to an input section
2739 // in OBJECT with index SHNDX.
2741 const Output_relaxed_input_section*
2742 Output_section::find_relaxed_input_section(const Relobj* object,
2743 unsigned int shndx) const
2745 if (!this->lookup_maps_->is_valid())
2746 this->build_lookup_maps();
2747 return this->lookup_maps_->find_relaxed_input_section(object, shndx);
2750 // Given an address OFFSET relative to the start of input section
2751 // SHNDX in OBJECT, return whether this address is being included in
2752 // the final link. This should only be called if SHNDX in OBJECT has
2753 // a special mapping.
2756 Output_section::is_input_address_mapped(const Relobj* object,
2760 // Look at the Output_section_data_maps first.
2761 const Output_section_data* posd = this->find_merge_section(object, shndx);
2763 posd = this->find_relaxed_input_section(object, shndx);
2767 section_offset_type output_offset;
2768 bool found = posd->output_offset(object, shndx, offset, &output_offset);
2770 return output_offset != -1;
2773 // Fall back to the slow look-up.
2774 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2775 p != this->input_sections_.end();
2778 section_offset_type output_offset;
2779 if (p->output_offset(object, shndx, offset, &output_offset))
2780 return output_offset != -1;
2783 // By default we assume that the address is mapped. This should
2784 // only be called after we have passed all sections to Layout. At
2785 // that point we should know what we are discarding.
2789 // Given an address OFFSET relative to the start of input section
2790 // SHNDX in object OBJECT, return the output offset relative to the
2791 // start of the input section in the output section. This should only
2792 // be called if SHNDX in OBJECT has a special mapping.
2795 Output_section::output_offset(const Relobj* object, unsigned int shndx,
2796 section_offset_type offset) const
2798 // This can only be called meaningfully when we know the data size
2800 gold_assert(this->is_data_size_valid());
2802 // Look at the Output_section_data_maps first.
2803 const Output_section_data* posd = this->find_merge_section(object, shndx);
2805 posd = this->find_relaxed_input_section(object, shndx);
2808 section_offset_type output_offset;
2809 bool found = posd->output_offset(object, shndx, offset, &output_offset);
2811 return output_offset;
2814 // Fall back to the slow look-up.
2815 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2816 p != this->input_sections_.end();
2819 section_offset_type output_offset;
2820 if (p->output_offset(object, shndx, offset, &output_offset))
2821 return output_offset;
2826 // Return the output virtual address of OFFSET relative to the start
2827 // of input section SHNDX in object OBJECT.
2830 Output_section::output_address(const Relobj* object, unsigned int shndx,
2833 uint64_t addr = this->address() + this->first_input_offset_;
2835 // Look at the Output_section_data_maps first.
2836 const Output_section_data* posd = this->find_merge_section(object, shndx);
2838 posd = this->find_relaxed_input_section(object, shndx);
2839 if (posd != NULL && posd->is_address_valid())
2841 section_offset_type output_offset;
2842 bool found = posd->output_offset(object, shndx, offset, &output_offset);
2844 return posd->address() + output_offset;
2847 // Fall back to the slow look-up.
2848 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2849 p != this->input_sections_.end();
2852 addr = align_address(addr, p->addralign());
2853 section_offset_type output_offset;
2854 if (p->output_offset(object, shndx, offset, &output_offset))
2856 if (output_offset == -1)
2858 return addr + output_offset;
2860 addr += p->data_size();
2863 // If we get here, it means that we don't know the mapping for this
2864 // input section. This might happen in principle if
2865 // add_input_section were called before add_output_section_data.
2866 // But it should never actually happen.
2871 // Find the output address of the start of the merged section for
2872 // input section SHNDX in object OBJECT.
2875 Output_section::find_starting_output_address(const Relobj* object,
2877 uint64_t* paddr) const
2879 // FIXME: This becomes a bottle-neck if we have many relaxed sections.
2880 // Looking up the merge section map does not always work as we sometimes
2881 // find a merge section without its address set.
2882 uint64_t addr = this->address() + this->first_input_offset_;
2883 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2884 p != this->input_sections_.end();
2887 addr = align_address(addr, p->addralign());
2889 // It would be nice if we could use the existing output_offset
2890 // method to get the output offset of input offset 0.
2891 // Unfortunately we don't know for sure that input offset 0 is
2893 if (p->is_merge_section_for(object, shndx))
2899 addr += p->data_size();
2902 // We couldn't find a merge output section for this input section.
2906 // Update the data size of an Output_section.
2909 Output_section::update_data_size()
2911 if (this->input_sections_.empty())
2914 if (this->must_sort_attached_input_sections()
2915 || this->input_section_order_specified())
2916 this->sort_attached_input_sections();
2918 off_t off = this->first_input_offset_;
2919 for (Input_section_list::iterator p = this->input_sections_.begin();
2920 p != this->input_sections_.end();
2923 off = align_address(off, p->addralign());
2924 off += p->current_data_size();
2927 this->set_current_data_size_for_child(off);
2930 // Set the data size of an Output_section. This is where we handle
2931 // setting the addresses of any Output_section_data objects.
2934 Output_section::set_final_data_size()
2936 if (this->input_sections_.empty())
2938 this->set_data_size(this->current_data_size_for_child());
2942 if (this->must_sort_attached_input_sections()
2943 || this->input_section_order_specified())
2944 this->sort_attached_input_sections();
2946 uint64_t address = this->address();
2947 off_t startoff = this->offset();
2948 off_t off = startoff + this->first_input_offset_;
2949 for (Input_section_list::iterator p = this->input_sections_.begin();
2950 p != this->input_sections_.end();
2953 off = align_address(off, p->addralign());
2954 p->set_address_and_file_offset(address + (off - startoff), off,
2956 off += p->data_size();
2959 this->set_data_size(off - startoff);
2962 // Reset the address and file offset.
2965 Output_section::do_reset_address_and_file_offset()
2967 // An unallocated section has no address. Forcing this means that
2968 // we don't need special treatment for symbols defined in debug
2969 // sections. We do the same in the constructor. This does not
2970 // apply to NOLOAD sections though.
2971 if (((this->flags_ & elfcpp::SHF_ALLOC) == 0) && !this->is_noload_)
2972 this->set_address(0);
2974 for (Input_section_list::iterator p = this->input_sections_.begin();
2975 p != this->input_sections_.end();
2977 p->reset_address_and_file_offset();
2980 // Return true if address and file offset have the values after reset.
2983 Output_section::do_address_and_file_offset_have_reset_values() const
2985 if (this->is_offset_valid())
2988 // An unallocated section has address 0 after its construction or a reset.
2989 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0)
2990 return this->is_address_valid() && this->address() == 0;
2992 return !this->is_address_valid();
2995 // Set the TLS offset. Called only for SHT_TLS sections.
2998 Output_section::do_set_tls_offset(uint64_t tls_base)
3000 this->tls_offset_ = this->address() - tls_base;
3003 // In a few cases we need to sort the input sections attached to an
3004 // output section. This is used to implement the type of constructor
3005 // priority ordering implemented by the GNU linker, in which the
3006 // priority becomes part of the section name and the sections are
3007 // sorted by name. We only do this for an output section if we see an
3008 // attached input section matching ".ctor.*", ".dtor.*",
3009 // ".init_array.*" or ".fini_array.*".
3011 class Output_section::Input_section_sort_entry
3014 Input_section_sort_entry()
3015 : input_section_(), index_(-1U), section_has_name_(false),
3019 Input_section_sort_entry(const Input_section& input_section,
3021 bool must_sort_attached_input_sections)
3022 : input_section_(input_section), index_(index),
3023 section_has_name_(input_section.is_input_section()
3024 || input_section.is_relaxed_input_section())
3026 if (this->section_has_name_
3027 && must_sort_attached_input_sections)
3029 // This is only called single-threaded from Layout::finalize,
3030 // so it is OK to lock. Unfortunately we have no way to pass
3032 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
3033 Object* obj = (input_section.is_input_section()
3034 ? input_section.relobj()
3035 : input_section.relaxed_input_section()->relobj());
3036 Task_lock_obj<Object> tl(dummy_task, obj);
3038 // This is a slow operation, which should be cached in
3039 // Layout::layout if this becomes a speed problem.
3040 this->section_name_ = obj->section_name(input_section.shndx());
3044 // Return the Input_section.
3045 const Input_section&
3046 input_section() const
3048 gold_assert(this->index_ != -1U);
3049 return this->input_section_;
3052 // The index of this entry in the original list. This is used to
3053 // make the sort stable.
3057 gold_assert(this->index_ != -1U);
3058 return this->index_;
3061 // Whether there is a section name.
3063 section_has_name() const
3064 { return this->section_has_name_; }
3066 // The section name.
3068 section_name() const
3070 gold_assert(this->section_has_name_);
3071 return this->section_name_;
3074 // Return true if the section name has a priority. This is assumed
3075 // to be true if it has a dot after the initial dot.
3077 has_priority() const
3079 gold_assert(this->section_has_name_);
3080 return this->section_name_.find('.', 1) != std::string::npos;
3083 // Return true if this an input file whose base name matches
3084 // FILE_NAME. The base name must have an extension of ".o", and
3085 // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
3086 // This is to match crtbegin.o as well as crtbeginS.o without
3087 // getting confused by other possibilities. Overall matching the
3088 // file name this way is a dreadful hack, but the GNU linker does it
3089 // in order to better support gcc, and we need to be compatible.
3091 match_file_name(const char* match_file_name) const
3093 const std::string& file_name(this->input_section_.relobj()->name());
3094 const char* base_name = lbasename(file_name.c_str());
3095 size_t match_len = strlen(match_file_name);
3096 if (strncmp(base_name, match_file_name, match_len) != 0)
3098 size_t base_len = strlen(base_name);
3099 if (base_len != match_len + 2 && base_len != match_len + 3)
3101 return memcmp(base_name + base_len - 2, ".o", 2) == 0;
3104 // Returns 1 if THIS should appear before S in section order, -1 if S
3105 // appears before THIS and 0 if they are not comparable.
3107 compare_section_ordering(const Input_section_sort_entry& s) const
3109 unsigned int this_secn_index = this->input_section_.section_order_index();
3110 unsigned int s_secn_index = s.input_section().section_order_index();
3111 if (this_secn_index > 0 && s_secn_index > 0)
3113 if (this_secn_index < s_secn_index)
3115 else if (this_secn_index > s_secn_index)
3122 // The Input_section we are sorting.
3123 Input_section input_section_;
3124 // The index of this Input_section in the original list.
3125 unsigned int index_;
3126 // Whether this Input_section has a section name--it won't if this
3127 // is some random Output_section_data.
3128 bool section_has_name_;
3129 // The section name if there is one.
3130 std::string section_name_;
3133 // Return true if S1 should come before S2 in the output section.
3136 Output_section::Input_section_sort_compare::operator()(
3137 const Output_section::Input_section_sort_entry& s1,
3138 const Output_section::Input_section_sort_entry& s2) const
3140 // crtbegin.o must come first.
3141 bool s1_begin = s1.match_file_name("crtbegin");
3142 bool s2_begin = s2.match_file_name("crtbegin");
3143 if (s1_begin || s2_begin)
3149 return s1.index() < s2.index();
3152 // crtend.o must come last.
3153 bool s1_end = s1.match_file_name("crtend");
3154 bool s2_end = s2.match_file_name("crtend");
3155 if (s1_end || s2_end)
3161 return s1.index() < s2.index();
3164 // We sort all the sections with no names to the end.
3165 if (!s1.section_has_name() || !s2.section_has_name())
3167 if (s1.section_has_name())
3169 if (s2.section_has_name())
3171 return s1.index() < s2.index();
3174 // A section with a priority follows a section without a priority.
3175 bool s1_has_priority = s1.has_priority();
3176 bool s2_has_priority = s2.has_priority();
3177 if (s1_has_priority && !s2_has_priority)
3179 if (!s1_has_priority && s2_has_priority)
3182 // Check if a section order exists for these sections through a section
3183 // ordering file. If sequence_num is 0, an order does not exist.
3184 int sequence_num = s1.compare_section_ordering(s2);
3185 if (sequence_num != 0)
3186 return sequence_num == 1;
3188 // Otherwise we sort by name.
3189 int compare = s1.section_name().compare(s2.section_name());
3193 // Otherwise we keep the input order.
3194 return s1.index() < s2.index();
3197 // Return true if S1 should come before S2 in an .init_array or .fini_array
3201 Output_section::Input_section_sort_init_fini_compare::operator()(
3202 const Output_section::Input_section_sort_entry& s1,
3203 const Output_section::Input_section_sort_entry& s2) const
3205 // We sort all the sections with no names to the end.
3206 if (!s1.section_has_name() || !s2.section_has_name())
3208 if (s1.section_has_name())
3210 if (s2.section_has_name())
3212 return s1.index() < s2.index();
3215 // A section without a priority follows a section with a priority.
3216 // This is the reverse of .ctors and .dtors sections.
3217 bool s1_has_priority = s1.has_priority();
3218 bool s2_has_priority = s2.has_priority();
3219 if (s1_has_priority && !s2_has_priority)
3221 if (!s1_has_priority && s2_has_priority)
3224 // Check if a section order exists for these sections through a section
3225 // ordering file. If sequence_num is 0, an order does not exist.
3226 int sequence_num = s1.compare_section_ordering(s2);
3227 if (sequence_num != 0)
3228 return sequence_num == 1;
3230 // Otherwise we sort by name.
3231 int compare = s1.section_name().compare(s2.section_name());
3235 // Otherwise we keep the input order.
3236 return s1.index() < s2.index();
3239 // Return true if S1 should come before S2. Sections that do not match
3240 // any pattern in the section ordering file are placed ahead of the sections
3241 // that match some pattern.
3244 Output_section::Input_section_sort_section_order_index_compare::operator()(
3245 const Output_section::Input_section_sort_entry& s1,
3246 const Output_section::Input_section_sort_entry& s2) const
3248 unsigned int s1_secn_index = s1.input_section().section_order_index();
3249 unsigned int s2_secn_index = s2.input_section().section_order_index();
3251 // Keep input order if section ordering cannot determine order.
3252 if (s1_secn_index == s2_secn_index)
3253 return s1.index() < s2.index();
3255 return s1_secn_index < s2_secn_index;
3258 // Sort the input sections attached to an output section.
3261 Output_section::sort_attached_input_sections()
3263 if (this->attached_input_sections_are_sorted_)
3266 if (this->checkpoint_ != NULL
3267 && !this->checkpoint_->input_sections_saved())
3268 this->checkpoint_->save_input_sections();
3270 // The only thing we know about an input section is the object and
3271 // the section index. We need the section name. Recomputing this
3272 // is slow but this is an unusual case. If this becomes a speed
3273 // problem we can cache the names as required in Layout::layout.
3275 // We start by building a larger vector holding a copy of each
3276 // Input_section, plus its current index in the list and its name.
3277 std::vector<Input_section_sort_entry> sort_list;
3280 for (Input_section_list::iterator p = this->input_sections_.begin();
3281 p != this->input_sections_.end();
3283 sort_list.push_back(Input_section_sort_entry(*p, i,
3284 this->must_sort_attached_input_sections()));
3286 // Sort the input sections.
3287 if (this->must_sort_attached_input_sections())
3289 if (this->type() == elfcpp::SHT_PREINIT_ARRAY
3290 || this->type() == elfcpp::SHT_INIT_ARRAY
3291 || this->type() == elfcpp::SHT_FINI_ARRAY)
3292 std::sort(sort_list.begin(), sort_list.end(),
3293 Input_section_sort_init_fini_compare());
3295 std::sort(sort_list.begin(), sort_list.end(),
3296 Input_section_sort_compare());
3300 gold_assert(parameters->options().section_ordering_file());
3301 std::sort(sort_list.begin(), sort_list.end(),
3302 Input_section_sort_section_order_index_compare());
3305 // Copy the sorted input sections back to our list.
3306 this->input_sections_.clear();
3307 for (std::vector<Input_section_sort_entry>::iterator p = sort_list.begin();
3308 p != sort_list.end();
3310 this->input_sections_.push_back(p->input_section());
3313 // Remember that we sorted the input sections, since we might get
3315 this->attached_input_sections_are_sorted_ = true;
3318 // Write the section header to *OSHDR.
3320 template<int size, bool big_endian>
3322 Output_section::write_header(const Layout* layout,
3323 const Stringpool* secnamepool,
3324 elfcpp::Shdr_write<size, big_endian>* oshdr) const
3326 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
3327 oshdr->put_sh_type(this->type_);
3329 elfcpp::Elf_Xword flags = this->flags_;
3330 if (this->info_section_ != NULL && this->info_uses_section_index_)
3331 flags |= elfcpp::SHF_INFO_LINK;
3332 oshdr->put_sh_flags(flags);
3334 oshdr->put_sh_addr(this->address());
3335 oshdr->put_sh_offset(this->offset());
3336 oshdr->put_sh_size(this->data_size());
3337 if (this->link_section_ != NULL)
3338 oshdr->put_sh_link(this->link_section_->out_shndx());
3339 else if (this->should_link_to_symtab_)
3340 oshdr->put_sh_link(layout->symtab_section()->out_shndx());
3341 else if (this->should_link_to_dynsym_)
3342 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
3344 oshdr->put_sh_link(this->link_);
3346 elfcpp::Elf_Word info;
3347 if (this->info_section_ != NULL)
3349 if (this->info_uses_section_index_)
3350 info = this->info_section_->out_shndx();
3352 info = this->info_section_->symtab_index();
3354 else if (this->info_symndx_ != NULL)
3355 info = this->info_symndx_->symtab_index();
3358 oshdr->put_sh_info(info);
3360 oshdr->put_sh_addralign(this->addralign_);
3361 oshdr->put_sh_entsize(this->entsize_);
3364 // Write out the data. For input sections the data is written out by
3365 // Object::relocate, but we have to handle Output_section_data objects
3369 Output_section::do_write(Output_file* of)
3371 gold_assert(!this->requires_postprocessing());
3373 // If the target performs relaxation, we delay filler generation until now.
3374 gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
3376 off_t output_section_file_offset = this->offset();
3377 for (Fill_list::iterator p = this->fills_.begin();
3378 p != this->fills_.end();
3381 std::string fill_data(parameters->target().code_fill(p->length()));
3382 of->write(output_section_file_offset + p->section_offset(),
3383 fill_data.data(), fill_data.size());
3386 off_t off = this->offset() + this->first_input_offset_;
3387 for (Input_section_list::iterator p = this->input_sections_.begin();
3388 p != this->input_sections_.end();
3391 off_t aligned_off = align_address(off, p->addralign());
3392 if (this->generate_code_fills_at_write_ && (off != aligned_off))
3394 size_t fill_len = aligned_off - off;
3395 std::string fill_data(parameters->target().code_fill(fill_len));
3396 of->write(off, fill_data.data(), fill_data.size());
3400 off = aligned_off + p->data_size();
3404 // If a section requires postprocessing, create the buffer to use.
3407 Output_section::create_postprocessing_buffer()
3409 gold_assert(this->requires_postprocessing());
3411 if (this->postprocessing_buffer_ != NULL)
3414 if (!this->input_sections_.empty())
3416 off_t off = this->first_input_offset_;
3417 for (Input_section_list::iterator p = this->input_sections_.begin();
3418 p != this->input_sections_.end();
3421 off = align_address(off, p->addralign());
3422 p->finalize_data_size();
3423 off += p->data_size();
3425 this->set_current_data_size_for_child(off);
3428 off_t buffer_size = this->current_data_size_for_child();
3429 this->postprocessing_buffer_ = new unsigned char[buffer_size];
3432 // Write all the data of an Output_section into the postprocessing
3433 // buffer. This is used for sections which require postprocessing,
3434 // such as compression. Input sections are handled by
3435 // Object::Relocate.
3438 Output_section::write_to_postprocessing_buffer()
3440 gold_assert(this->requires_postprocessing());
3442 // If the target performs relaxation, we delay filler generation until now.
3443 gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
3445 unsigned char* buffer = this->postprocessing_buffer();
3446 for (Fill_list::iterator p = this->fills_.begin();
3447 p != this->fills_.end();
3450 std::string fill_data(parameters->target().code_fill(p->length()));
3451 memcpy(buffer + p->section_offset(), fill_data.data(),
3455 off_t off = this->first_input_offset_;
3456 for (Input_section_list::iterator p = this->input_sections_.begin();
3457 p != this->input_sections_.end();
3460 off_t aligned_off = align_address(off, p->addralign());
3461 if (this->generate_code_fills_at_write_ && (off != aligned_off))
3463 size_t fill_len = aligned_off - off;
3464 std::string fill_data(parameters->target().code_fill(fill_len));
3465 memcpy(buffer + off, fill_data.data(), fill_data.size());
3468 p->write_to_buffer(buffer + aligned_off);
3469 off = aligned_off + p->data_size();
3473 // Get the input sections for linker script processing. We leave
3474 // behind the Output_section_data entries. Note that this may be
3475 // slightly incorrect for merge sections. We will leave them behind,
3476 // but it is possible that the script says that they should follow
3477 // some other input sections, as in:
3478 // .rodata { *(.rodata) *(.rodata.cst*) }
3479 // For that matter, we don't handle this correctly:
3480 // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
3481 // With luck this will never matter.
3484 Output_section::get_input_sections(
3486 const std::string& fill,
3487 std::list<Input_section>* input_sections)
3489 if (this->checkpoint_ != NULL
3490 && !this->checkpoint_->input_sections_saved())
3491 this->checkpoint_->save_input_sections();
3493 // Invalidate fast look-up maps.
3494 this->lookup_maps_->invalidate();
3496 uint64_t orig_address = address;
3498 address = align_address(address, this->addralign());
3500 Input_section_list remaining;
3501 for (Input_section_list::iterator p = this->input_sections_.begin();
3502 p != this->input_sections_.end();
3505 if (p->is_input_section()
3506 || p->is_relaxed_input_section()
3507 || p->is_merge_section())
3508 input_sections->push_back(*p);
3511 uint64_t aligned_address = align_address(address, p->addralign());
3512 if (aligned_address != address && !fill.empty())
3514 section_size_type length =
3515 convert_to_section_size_type(aligned_address - address);
3516 std::string this_fill;
3517 this_fill.reserve(length);
3518 while (this_fill.length() + fill.length() <= length)
3520 if (this_fill.length() < length)
3521 this_fill.append(fill, 0, length - this_fill.length());
3523 Output_section_data* posd = new Output_data_const(this_fill, 0);
3524 remaining.push_back(Input_section(posd));
3526 address = aligned_address;
3528 remaining.push_back(*p);
3530 p->finalize_data_size();
3531 address += p->data_size();
3535 this->input_sections_.swap(remaining);
3536 this->first_input_offset_ = 0;
3538 uint64_t data_size = address - orig_address;
3539 this->set_current_data_size_for_child(data_size);
3543 // Add a script input section. SIS is an Output_section::Input_section,
3544 // which can be either a plain input section or a special input section like
3545 // a relaxed input section. For a special input section, its size must be
3549 Output_section::add_script_input_section(const Input_section& sis)
3551 uint64_t data_size = sis.data_size();
3552 uint64_t addralign = sis.addralign();
3553 if (addralign > this->addralign_)
3554 this->addralign_ = addralign;
3556 off_t offset_in_section = this->current_data_size_for_child();
3557 off_t aligned_offset_in_section = align_address(offset_in_section,
3560 this->set_current_data_size_for_child(aligned_offset_in_section
3563 this->input_sections_.push_back(sis);
3565 // Update fast lookup maps if necessary.
3566 if (this->lookup_maps_->is_valid())
3568 if (sis.is_merge_section())
3570 Output_merge_base* pomb = sis.output_merge_base();
3571 Merge_section_properties msp(pomb->is_string(), pomb->entsize(),
3573 this->lookup_maps_->add_merge_section(msp, pomb);
3574 for (Output_merge_base::Input_sections::const_iterator p =
3575 pomb->input_sections_begin();
3576 p != pomb->input_sections_end();
3578 this->lookup_maps_->add_merge_input_section(p->first, p->second,
3581 else if (sis.is_relaxed_input_section())
3583 Output_relaxed_input_section* poris = sis.relaxed_input_section();
3584 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
3585 poris->shndx(), poris);
3590 // Save states for relaxation.
3593 Output_section::save_states()
3595 gold_assert(this->checkpoint_ == NULL);
3596 Checkpoint_output_section* checkpoint =
3597 new Checkpoint_output_section(this->addralign_, this->flags_,
3598 this->input_sections_,
3599 this->first_input_offset_,
3600 this->attached_input_sections_are_sorted_);
3601 this->checkpoint_ = checkpoint;
3602 gold_assert(this->fills_.empty());
3606 Output_section::discard_states()
3608 gold_assert(this->checkpoint_ != NULL);
3609 delete this->checkpoint_;
3610 this->checkpoint_ = NULL;
3611 gold_assert(this->fills_.empty());
3613 // Simply invalidate the fast lookup maps since we do not keep
3615 this->lookup_maps_->invalidate();
3619 Output_section::restore_states()
3621 gold_assert(this->checkpoint_ != NULL);
3622 Checkpoint_output_section* checkpoint = this->checkpoint_;
3624 this->addralign_ = checkpoint->addralign();
3625 this->flags_ = checkpoint->flags();
3626 this->first_input_offset_ = checkpoint->first_input_offset();
3628 if (!checkpoint->input_sections_saved())
3630 // If we have not copied the input sections, just resize it.
3631 size_t old_size = checkpoint->input_sections_size();
3632 gold_assert(this->input_sections_.size() >= old_size);
3633 this->input_sections_.resize(old_size);
3637 // We need to copy the whole list. This is not efficient for
3638 // extremely large output with hundreads of thousands of input
3639 // objects. We may need to re-think how we should pass sections
3641 this->input_sections_ = *checkpoint->input_sections();
3644 this->attached_input_sections_are_sorted_ =
3645 checkpoint->attached_input_sections_are_sorted();
3647 // Simply invalidate the fast lookup maps since we do not keep
3649 this->lookup_maps_->invalidate();
3652 // Update the section offsets of input sections in this. This is required if
3653 // relaxation causes some input sections to change sizes.
3656 Output_section::adjust_section_offsets()
3658 if (!this->section_offsets_need_adjustment_)
3662 for (Input_section_list::iterator p = this->input_sections_.begin();
3663 p != this->input_sections_.end();
3666 off = align_address(off, p->addralign());
3667 if (p->is_input_section())
3668 p->relobj()->set_section_offset(p->shndx(), off);
3669 off += p->data_size();
3672 this->section_offsets_need_adjustment_ = false;
3675 // Print to the map file.
3678 Output_section::do_print_to_mapfile(Mapfile* mapfile) const
3680 mapfile->print_output_section(this);
3682 for (Input_section_list::const_iterator p = this->input_sections_.begin();
3683 p != this->input_sections_.end();
3685 p->print_to_mapfile(mapfile);
3688 // Print stats for merge sections to stderr.
3691 Output_section::print_merge_stats()
3693 Input_section_list::iterator p;
3694 for (p = this->input_sections_.begin();
3695 p != this->input_sections_.end();
3697 p->print_merge_stats(this->name_);
3700 // Set a fixed layout for the section. Used for incremental update links.
3703 Output_section::set_fixed_layout(uint64_t sh_addr, off_t sh_offset,
3704 off_t sh_size, uint64_t sh_addralign)
3706 this->addralign_ = sh_addralign;
3707 this->set_current_data_size(sh_size);
3708 if ((this->flags_ & elfcpp::SHF_ALLOC) != 0)
3709 this->set_address(sh_addr);
3710 this->set_file_offset(sh_offset);
3711 this->finalize_data_size();
3712 this->free_list_.init(sh_size, false);
3713 this->has_fixed_layout_ = true;
3716 // Reserve space within the fixed layout for the section. Used for
3717 // incremental update links.
3719 Output_section::reserve(uint64_t sh_offset, uint64_t sh_size)
3721 this->free_list_.remove(sh_offset, sh_offset + sh_size);
3724 // Output segment methods.
3726 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
3736 is_max_align_known_(false),
3737 are_addresses_set_(false),
3738 is_large_data_segment_(false)
3740 // The ELF ABI specifies that a PT_TLS segment always has PF_R as
3742 if (type == elfcpp::PT_TLS)
3743 this->flags_ = elfcpp::PF_R;
3746 // Add an Output_section to a PT_LOAD Output_segment.
3749 Output_segment::add_output_section_to_load(Layout* layout,
3751 elfcpp::Elf_Word seg_flags)
3753 gold_assert(this->type() == elfcpp::PT_LOAD);
3754 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
3755 gold_assert(!this->is_max_align_known_);
3756 gold_assert(os->is_large_data_section() == this->is_large_data_segment());
3758 this->update_flags_for_output_section(seg_flags);
3760 // We don't want to change the ordering if we have a linker script
3761 // with a SECTIONS clause.
3762 Output_section_order order = os->order();
3763 if (layout->script_options()->saw_sections_clause())
3764 order = static_cast<Output_section_order>(0);
3766 gold_assert(order != ORDER_INVALID);
3768 this->output_lists_[order].push_back(os);
3771 // Add an Output_section to a non-PT_LOAD Output_segment.
3774 Output_segment::add_output_section_to_nonload(Output_section* os,
3775 elfcpp::Elf_Word seg_flags)
3777 gold_assert(this->type() != elfcpp::PT_LOAD);
3778 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
3779 gold_assert(!this->is_max_align_known_);
3781 this->update_flags_for_output_section(seg_flags);
3783 this->output_lists_[0].push_back(os);
3786 // Remove an Output_section from this segment. It is an error if it
3790 Output_segment::remove_output_section(Output_section* os)
3792 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
3794 Output_data_list* pdl = &this->output_lists_[i];
3795 for (Output_data_list::iterator p = pdl->begin(); p != pdl->end(); ++p)
3807 // Add an Output_data (which need not be an Output_section) to the
3808 // start of a segment.
3811 Output_segment::add_initial_output_data(Output_data* od)
3813 gold_assert(!this->is_max_align_known_);
3814 Output_data_list::iterator p = this->output_lists_[0].begin();
3815 this->output_lists_[0].insert(p, od);
3818 // Return true if this segment has any sections which hold actual
3819 // data, rather than being a BSS section.
3822 Output_segment::has_any_data_sections() const
3824 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
3826 const Output_data_list* pdl = &this->output_lists_[i];
3827 for (Output_data_list::const_iterator p = pdl->begin();
3831 if (!(*p)->is_section())
3833 if ((*p)->output_section()->type() != elfcpp::SHT_NOBITS)
3840 // Return whether the first data section (not counting TLS sections)
3841 // is a relro section.
3844 Output_segment::is_first_section_relro() const
3846 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
3848 if (i == static_cast<int>(ORDER_TLS_DATA)
3849 || i == static_cast<int>(ORDER_TLS_BSS))
3851 const Output_data_list* pdl = &this->output_lists_[i];
3854 Output_data* p = pdl->front();
3855 return p->is_section() && p->output_section()->is_relro();
3861 // Return the maximum alignment of the Output_data in Output_segment.
3864 Output_segment::maximum_alignment()
3866 if (!this->is_max_align_known_)
3868 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
3870 const Output_data_list* pdl = &this->output_lists_[i];
3871 uint64_t addralign = Output_segment::maximum_alignment_list(pdl);
3872 if (addralign > this->max_align_)
3873 this->max_align_ = addralign;
3875 this->is_max_align_known_ = true;
3878 return this->max_align_;
3881 // Return the maximum alignment of a list of Output_data.
3884 Output_segment::maximum_alignment_list(const Output_data_list* pdl)
3887 for (Output_data_list::const_iterator p = pdl->begin();
3891 uint64_t addralign = (*p)->addralign();
3892 if (addralign > ret)
3898 // Return whether this segment has any dynamic relocs.
3901 Output_segment::has_dynamic_reloc() const
3903 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
3904 if (this->has_dynamic_reloc_list(&this->output_lists_[i]))
3909 // Return whether this Output_data_list has any dynamic relocs.
3912 Output_segment::has_dynamic_reloc_list(const Output_data_list* pdl) const
3914 for (Output_data_list::const_iterator p = pdl->begin();
3917 if ((*p)->has_dynamic_reloc())
3922 // Set the section addresses for an Output_segment. If RESET is true,
3923 // reset the addresses first. ADDR is the address and *POFF is the
3924 // file offset. Set the section indexes starting with *PSHNDX.
3925 // INCREASE_RELRO is the size of the portion of the first non-relro
3926 // section that should be included in the PT_GNU_RELRO segment.
3927 // If this segment has relro sections, and has been aligned for
3928 // that purpose, set *HAS_RELRO to TRUE. Return the address of
3929 // the immediately following segment. Update *HAS_RELRO, *POFF,
3933 Output_segment::set_section_addresses(Layout* layout, bool reset,
3935 unsigned int* increase_relro,
3938 unsigned int* pshndx)
3940 gold_assert(this->type_ == elfcpp::PT_LOAD);
3942 uint64_t last_relro_pad = 0;
3943 off_t orig_off = *poff;
3945 bool in_tls = false;
3947 // If we have relro sections, we need to pad forward now so that the
3948 // relro sections plus INCREASE_RELRO end on a common page boundary.
3949 if (parameters->options().relro()
3950 && this->is_first_section_relro()
3951 && (!this->are_addresses_set_ || reset))
3953 uint64_t relro_size = 0;
3955 uint64_t max_align = 0;
3956 for (int i = 0; i <= static_cast<int>(ORDER_RELRO_LAST); ++i)
3958 Output_data_list* pdl = &this->output_lists_[i];
3959 Output_data_list::iterator p;
3960 for (p = pdl->begin(); p != pdl->end(); ++p)
3962 if (!(*p)->is_section())
3964 uint64_t align = (*p)->addralign();
3965 if (align > max_align)
3967 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
3971 // Align the first non-TLS section to the alignment
3972 // of the TLS segment.
3976 relro_size = align_address(relro_size, align);
3977 // Ignore the size of the .tbss section.
3978 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS)
3979 && (*p)->is_section_type(elfcpp::SHT_NOBITS))
3981 if ((*p)->is_address_valid())
3982 relro_size += (*p)->data_size();
3985 // FIXME: This could be faster.
3986 (*p)->set_address_and_file_offset(addr + relro_size,
3988 relro_size += (*p)->data_size();
3989 (*p)->reset_address_and_file_offset();
3992 if (p != pdl->end())
3995 relro_size += *increase_relro;
3996 // Pad the total relro size to a multiple of the maximum
3997 // section alignment seen.
3998 uint64_t aligned_size = align_address(relro_size, max_align);
3999 // Note the amount of padding added after the last relro section.
4000 last_relro_pad = aligned_size - relro_size;
4003 uint64_t page_align = parameters->target().common_pagesize();
4005 // Align to offset N such that (N + RELRO_SIZE) % PAGE_ALIGN == 0.
4006 uint64_t desired_align = page_align - (aligned_size % page_align);
4007 if (desired_align < *poff % page_align)
4008 *poff += page_align - *poff % page_align;
4009 *poff += desired_align - *poff % page_align;
4010 addr += *poff - orig_off;
4014 if (!reset && this->are_addresses_set_)
4016 gold_assert(this->paddr_ == addr);
4017 addr = this->vaddr_;
4021 this->vaddr_ = addr;
4022 this->paddr_ = addr;
4023 this->are_addresses_set_ = true;
4028 this->offset_ = orig_off;
4032 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4034 if (i == static_cast<int>(ORDER_RELRO_LAST))
4036 *poff += last_relro_pad;
4037 addr += last_relro_pad;
4038 if (this->output_lists_[i].empty())
4040 // If there is nothing in the ORDER_RELRO_LAST list,
4041 // the padding will occur at the end of the relro
4042 // segment, and we need to add it to *INCREASE_RELRO.
4043 *increase_relro += last_relro_pad;
4046 addr = this->set_section_list_addresses(layout, reset,
4047 &this->output_lists_[i],
4048 addr, poff, pshndx, &in_tls);
4049 if (i < static_cast<int>(ORDER_SMALL_BSS))
4051 this->filesz_ = *poff - orig_off;
4058 // If the last section was a TLS section, align upward to the
4059 // alignment of the TLS segment, so that the overall size of the TLS
4060 // segment is aligned.
4063 uint64_t segment_align = layout->tls_segment()->maximum_alignment();
4064 *poff = align_address(*poff, segment_align);
4067 this->memsz_ = *poff - orig_off;
4069 // Ignore the file offset adjustments made by the BSS Output_data
4076 // Set the addresses and file offsets in a list of Output_data
4080 Output_segment::set_section_list_addresses(Layout* layout, bool reset,
4081 Output_data_list* pdl,
4082 uint64_t addr, off_t* poff,
4083 unsigned int* pshndx,
4086 off_t startoff = *poff;
4087 // For incremental updates, we may allocate non-fixed sections from
4088 // free space in the file. This keeps track of the high-water mark.
4089 off_t maxoff = startoff;
4091 off_t off = startoff;
4092 for (Output_data_list::iterator p = pdl->begin();
4097 (*p)->reset_address_and_file_offset();
4099 // When doing an incremental update or when using a linker script,
4100 // the section will most likely already have an address.
4101 if (!(*p)->is_address_valid())
4103 uint64_t align = (*p)->addralign();
4105 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
4107 // Give the first TLS section the alignment of the
4108 // entire TLS segment. Otherwise the TLS segment as a
4109 // whole may be misaligned.
4112 Output_segment* tls_segment = layout->tls_segment();
4113 gold_assert(tls_segment != NULL);
4114 uint64_t segment_align = tls_segment->maximum_alignment();
4115 gold_assert(segment_align >= align);
4116 align = segment_align;
4123 // If this is the first section after the TLS segment,
4124 // align it to at least the alignment of the TLS
4125 // segment, so that the size of the overall TLS segment
4129 uint64_t segment_align =
4130 layout->tls_segment()->maximum_alignment();
4131 if (segment_align > align)
4132 align = segment_align;
4138 // FIXME: Need to handle TLS and .bss with incremental update.
4139 if (!parameters->incremental_update()
4140 || (*p)->is_section_flag_set(elfcpp::SHF_TLS)
4141 || (*p)->is_section_type(elfcpp::SHT_NOBITS))
4143 off = align_address(off, align);
4144 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
4148 // Incremental update: allocate file space from free list.
4149 (*p)->pre_finalize_data_size();
4150 off_t current_size = (*p)->current_data_size();
4151 off = layout->allocate(current_size, align, startoff);
4154 gold_assert((*p)->output_section() != NULL);
4155 gold_fatal(_("out of patch space for section %s; "
4156 "relink with --incremental-full"),
4157 (*p)->output_section()->name());
4159 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
4160 if ((*p)->data_size() > current_size)
4162 gold_assert((*p)->output_section() != NULL);
4163 gold_fatal(_("%s: section changed size; "
4164 "relink with --incremental-full"),
4165 (*p)->output_section()->name());
4169 else if (parameters->incremental_update())
4171 // For incremental updates, use the fixed offset for the
4172 // high-water mark computation.
4173 off = (*p)->offset();
4177 // The script may have inserted a skip forward, but it
4178 // better not have moved backward.
4179 if ((*p)->address() >= addr + (off - startoff))
4180 off += (*p)->address() - (addr + (off - startoff));
4183 if (!layout->script_options()->saw_sections_clause())
4187 Output_section* os = (*p)->output_section();
4189 // Cast to unsigned long long to avoid format warnings.
4190 unsigned long long previous_dot =
4191 static_cast<unsigned long long>(addr + (off - startoff));
4192 unsigned long long dot =
4193 static_cast<unsigned long long>((*p)->address());
4196 gold_error(_("dot moves backward in linker script "
4197 "from 0x%llx to 0x%llx"), previous_dot, dot);
4199 gold_error(_("address of section '%s' moves backward "
4200 "from 0x%llx to 0x%llx"),
4201 os->name(), previous_dot, dot);
4204 (*p)->set_file_offset(off);
4205 (*p)->finalize_data_size();
4208 gold_debug(DEBUG_INCREMENTAL,
4209 "set_section_list_addresses: %08lx %08lx %s",
4210 static_cast<long>(off),
4211 static_cast<long>((*p)->data_size()),
4212 ((*p)->output_section() != NULL
4213 ? (*p)->output_section()->name() : "(special)"));
4215 // We want to ignore the size of a SHF_TLS or SHT_NOBITS
4216 // section. Such a section does not affect the size of a
4218 if (!(*p)->is_section_flag_set(elfcpp::SHF_TLS)
4219 || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
4220 off += (*p)->data_size();
4225 if ((*p)->is_section())
4227 (*p)->set_out_shndx(*pshndx);
4233 return addr + (maxoff - startoff);
4236 // For a non-PT_LOAD segment, set the offset from the sections, if
4237 // any. Add INCREASE to the file size and the memory size.
4240 Output_segment::set_offset(unsigned int increase)
4242 gold_assert(this->type_ != elfcpp::PT_LOAD);
4244 gold_assert(!this->are_addresses_set_);
4246 // A non-load section only uses output_lists_[0].
4248 Output_data_list* pdl = &this->output_lists_[0];
4252 gold_assert(increase == 0);
4255 this->are_addresses_set_ = true;
4257 this->min_p_align_ = 0;
4263 // Find the first and last section by address.
4264 const Output_data* first = NULL;
4265 const Output_data* last_data = NULL;
4266 const Output_data* last_bss = NULL;
4267 for (Output_data_list::const_iterator p = pdl->begin();
4272 || (*p)->address() < first->address()
4273 || ((*p)->address() == first->address()
4274 && (*p)->data_size() < first->data_size()))
4276 const Output_data** plast;
4277 if ((*p)->is_section()
4278 && (*p)->output_section()->type() == elfcpp::SHT_NOBITS)
4283 || (*p)->address() > (*plast)->address()
4284 || ((*p)->address() == (*plast)->address()
4285 && (*p)->data_size() > (*plast)->data_size()))
4289 this->vaddr_ = first->address();
4290 this->paddr_ = (first->has_load_address()
4291 ? first->load_address()
4293 this->are_addresses_set_ = true;
4294 this->offset_ = first->offset();
4296 if (last_data == NULL)
4299 this->filesz_ = (last_data->address()
4300 + last_data->data_size()
4303 const Output_data* last = last_bss != NULL ? last_bss : last_data;
4304 this->memsz_ = (last->address()
4308 this->filesz_ += increase;
4309 this->memsz_ += increase;
4311 // If this is a RELRO segment, verify that the segment ends at a
4313 if (this->type_ == elfcpp::PT_GNU_RELRO)
4315 uint64_t page_align = parameters->target().common_pagesize();
4316 uint64_t segment_end = this->vaddr_ + this->memsz_;
4317 if (parameters->incremental_update())
4319 // The INCREASE_RELRO calculation is bypassed for an incremental
4320 // update, so we need to adjust the segment size manually here.
4321 segment_end = align_address(segment_end, page_align);
4322 this->memsz_ = segment_end - this->vaddr_;
4325 gold_assert(segment_end == align_address(segment_end, page_align));
4328 // If this is a TLS segment, align the memory size. The code in
4329 // set_section_list ensures that the section after the TLS segment
4330 // is aligned to give us room.
4331 if (this->type_ == elfcpp::PT_TLS)
4333 uint64_t segment_align = this->maximum_alignment();
4334 gold_assert(this->vaddr_ == align_address(this->vaddr_, segment_align));
4335 this->memsz_ = align_address(this->memsz_, segment_align);
4339 // Set the TLS offsets of the sections in the PT_TLS segment.
4342 Output_segment::set_tls_offsets()
4344 gold_assert(this->type_ == elfcpp::PT_TLS);
4346 for (Output_data_list::iterator p = this->output_lists_[0].begin();
4347 p != this->output_lists_[0].end();
4349 (*p)->set_tls_offset(this->vaddr_);
4352 // Return the load address of the first section.
4355 Output_segment::first_section_load_address() const
4357 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4359 const Output_data_list* pdl = &this->output_lists_[i];
4360 for (Output_data_list::const_iterator p = pdl->begin();
4364 if ((*p)->is_section())
4365 return ((*p)->has_load_address()
4366 ? (*p)->load_address()
4373 // Return the number of Output_sections in an Output_segment.
4376 Output_segment::output_section_count() const
4378 unsigned int ret = 0;
4379 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4380 ret += this->output_section_count_list(&this->output_lists_[i]);
4384 // Return the number of Output_sections in an Output_data_list.
4387 Output_segment::output_section_count_list(const Output_data_list* pdl) const
4389 unsigned int count = 0;
4390 for (Output_data_list::const_iterator p = pdl->begin();
4394 if ((*p)->is_section())
4400 // Return the section attached to the list segment with the lowest
4401 // load address. This is used when handling a PHDRS clause in a
4405 Output_segment::section_with_lowest_load_address() const
4407 Output_section* found = NULL;
4408 uint64_t found_lma = 0;
4409 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4410 this->lowest_load_address_in_list(&this->output_lists_[i], &found,
4415 // Look through a list for a section with a lower load address.
4418 Output_segment::lowest_load_address_in_list(const Output_data_list* pdl,
4419 Output_section** found,
4420 uint64_t* found_lma) const
4422 for (Output_data_list::const_iterator p = pdl->begin();
4426 if (!(*p)->is_section())
4428 Output_section* os = static_cast<Output_section*>(*p);
4429 uint64_t lma = (os->has_load_address()
4430 ? os->load_address()
4432 if (*found == NULL || lma < *found_lma)
4440 // Write the segment data into *OPHDR.
4442 template<int size, bool big_endian>
4444 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
4446 ophdr->put_p_type(this->type_);
4447 ophdr->put_p_offset(this->offset_);
4448 ophdr->put_p_vaddr(this->vaddr_);
4449 ophdr->put_p_paddr(this->paddr_);
4450 ophdr->put_p_filesz(this->filesz_);
4451 ophdr->put_p_memsz(this->memsz_);
4452 ophdr->put_p_flags(this->flags_);
4453 ophdr->put_p_align(std::max(this->min_p_align_, this->maximum_alignment()));
4456 // Write the section headers into V.
4458 template<int size, bool big_endian>
4460 Output_segment::write_section_headers(const Layout* layout,
4461 const Stringpool* secnamepool,
4463 unsigned int* pshndx) const
4465 // Every section that is attached to a segment must be attached to a
4466 // PT_LOAD segment, so we only write out section headers for PT_LOAD
4468 if (this->type_ != elfcpp::PT_LOAD)
4471 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4473 const Output_data_list* pdl = &this->output_lists_[i];
4474 v = this->write_section_headers_list<size, big_endian>(layout,
4483 template<int size, bool big_endian>
4485 Output_segment::write_section_headers_list(const Layout* layout,
4486 const Stringpool* secnamepool,
4487 const Output_data_list* pdl,
4489 unsigned int* pshndx) const
4491 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
4492 for (Output_data_list::const_iterator p = pdl->begin();
4496 if ((*p)->is_section())
4498 const Output_section* ps = static_cast<const Output_section*>(*p);
4499 gold_assert(*pshndx == ps->out_shndx());
4500 elfcpp::Shdr_write<size, big_endian> oshdr(v);
4501 ps->write_header(layout, secnamepool, &oshdr);
4509 // Print the output sections to the map file.
4512 Output_segment::print_sections_to_mapfile(Mapfile* mapfile) const
4514 if (this->type() != elfcpp::PT_LOAD)
4516 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4517 this->print_section_list_to_mapfile(mapfile, &this->output_lists_[i]);
4520 // Print an output section list to the map file.
4523 Output_segment::print_section_list_to_mapfile(Mapfile* mapfile,
4524 const Output_data_list* pdl) const
4526 for (Output_data_list::const_iterator p = pdl->begin();
4529 (*p)->print_to_mapfile(mapfile);
4532 // Output_file methods.
4534 Output_file::Output_file(const char* name)
4539 map_is_anonymous_(false),
4540 map_is_allocated_(false),
4541 is_temporary_(false)
4545 // Try to open an existing file. Returns false if the file doesn't
4546 // exist, has a size of 0 or can't be mmapped.
4549 Output_file::open_for_modification()
4551 // The name "-" means "stdout".
4552 if (strcmp(this->name_, "-") == 0)
4555 // Don't bother opening files with a size of zero.
4557 if (::stat(this->name_, &s) != 0 || s.st_size == 0)
4560 int o = open_descriptor(-1, this->name_, O_RDWR, 0);
4562 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
4564 this->file_size_ = s.st_size;
4566 // If the file can't be mmapped, copying the content to an anonymous
4567 // map will probably negate the performance benefits of incremental
4568 // linking. This could be helped by using views and loading only
4569 // the necessary parts, but this is not supported as of now.
4570 if (!this->map_no_anonymous())
4572 release_descriptor(o, true);
4574 this->file_size_ = 0;
4581 // Open the output file.
4584 Output_file::open(off_t file_size)
4586 this->file_size_ = file_size;
4588 // Unlink the file first; otherwise the open() may fail if the file
4589 // is busy (e.g. it's an executable that's currently being executed).
4591 // However, the linker may be part of a system where a zero-length
4592 // file is created for it to write to, with tight permissions (gcc
4593 // 2.95 did something like this). Unlinking the file would work
4594 // around those permission controls, so we only unlink if the file
4595 // has a non-zero size. We also unlink only regular files to avoid
4596 // trouble with directories/etc.
4598 // If we fail, continue; this command is merely a best-effort attempt
4599 // to improve the odds for open().
4601 // We let the name "-" mean "stdout"
4602 if (!this->is_temporary_)
4604 if (strcmp(this->name_, "-") == 0)
4605 this->o_ = STDOUT_FILENO;
4609 if (::stat(this->name_, &s) == 0
4610 && (S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
4613 ::unlink(this->name_);
4614 else if (!parameters->options().relocatable())
4616 // If we don't unlink the existing file, add execute
4617 // permission where read permissions already exist
4618 // and where the umask permits.
4619 int mask = ::umask(0);
4621 s.st_mode |= (s.st_mode & 0444) >> 2;
4622 ::chmod(this->name_, s.st_mode & ~mask);
4626 int mode = parameters->options().relocatable() ? 0666 : 0777;
4627 int o = open_descriptor(-1, this->name_, O_RDWR | O_CREAT | O_TRUNC,
4630 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
4638 // Resize the output file.
4641 Output_file::resize(off_t file_size)
4643 // If the mmap is mapping an anonymous memory buffer, this is easy:
4644 // just mremap to the new size. If it's mapping to a file, we want
4645 // to unmap to flush to the file, then remap after growing the file.
4646 if (this->map_is_anonymous_)
4649 if (!this->map_is_allocated_)
4651 base = ::mremap(this->base_, this->file_size_, file_size,
4653 if (base == MAP_FAILED)
4654 gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
4658 base = realloc(this->base_, file_size);
4661 if (file_size > this->file_size_)
4662 memset(static_cast<char*>(base) + this->file_size_, 0,
4663 file_size - this->file_size_);
4665 this->base_ = static_cast<unsigned char*>(base);
4666 this->file_size_ = file_size;
4671 this->file_size_ = file_size;
4672 if (!this->map_no_anonymous())
4673 gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
4677 // Map an anonymous block of memory which will later be written to the
4678 // file. Return whether the map succeeded.
4681 Output_file::map_anonymous()
4683 void* base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
4684 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
4685 if (base == MAP_FAILED)
4687 base = malloc(this->file_size_);
4690 memset(base, 0, this->file_size_);
4691 this->map_is_allocated_ = true;
4693 this->base_ = static_cast<unsigned char*>(base);
4694 this->map_is_anonymous_ = true;
4698 // Map the file into memory. Return whether the mapping succeeded.
4701 Output_file::map_no_anonymous()
4703 const int o = this->o_;
4705 // If the output file is not a regular file, don't try to mmap it;
4706 // instead, we'll mmap a block of memory (an anonymous buffer), and
4707 // then later write the buffer to the file.
4709 struct stat statbuf;
4710 if (o == STDOUT_FILENO || o == STDERR_FILENO
4711 || ::fstat(o, &statbuf) != 0
4712 || !S_ISREG(statbuf.st_mode)
4713 || this->is_temporary_)
4716 // Ensure that we have disk space available for the file. If we
4717 // don't do this, it is possible that we will call munmap, close,
4718 // and exit with dirty buffers still in the cache with no assigned
4719 // disk blocks. If the disk is out of space at that point, the
4720 // output file will wind up incomplete, but we will have already
4721 // exited. The alternative to fallocate would be to use fdatasync,
4722 // but that would be a more significant performance hit.
4723 if (::posix_fallocate(o, 0, this->file_size_) < 0)
4724 gold_fatal(_("%s: %s"), this->name_, strerror(errno));
4726 // Map the file into memory.
4727 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
4730 // The mmap call might fail because of file system issues: the file
4731 // system might not support mmap at all, or it might not support
4732 // mmap with PROT_WRITE.
4733 if (base == MAP_FAILED)
4736 this->map_is_anonymous_ = false;
4737 this->base_ = static_cast<unsigned char*>(base);
4741 // Map the file into memory.
4746 if (this->map_no_anonymous())
4749 // The mmap call might fail because of file system issues: the file
4750 // system might not support mmap at all, or it might not support
4751 // mmap with PROT_WRITE. I'm not sure which errno values we will
4752 // see in all cases, so if the mmap fails for any reason and we
4753 // don't care about file contents, try for an anonymous map.
4754 if (this->map_anonymous())
4757 gold_fatal(_("%s: mmap: failed to allocate %lu bytes for output file: %s"),
4758 this->name_, static_cast<unsigned long>(this->file_size_),
4762 // Unmap the file from memory.
4765 Output_file::unmap()
4767 if (this->map_is_anonymous_)
4769 // We've already written out the data, so there is no reason to
4770 // waste time unmapping or freeing the memory.
4774 if (::munmap(this->base_, this->file_size_) < 0)
4775 gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
4780 // Close the output file.
4783 Output_file::close()
4785 // If the map isn't file-backed, we need to write it now.
4786 if (this->map_is_anonymous_ && !this->is_temporary_)
4788 size_t bytes_to_write = this->file_size_;
4790 while (bytes_to_write > 0)
4792 ssize_t bytes_written = ::write(this->o_, this->base_ + offset,
4794 if (bytes_written == 0)
4795 gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
4796 else if (bytes_written < 0)
4797 gold_error(_("%s: write: %s"), this->name_, strerror(errno));
4800 bytes_to_write -= bytes_written;
4801 offset += bytes_written;
4807 // We don't close stdout or stderr
4808 if (this->o_ != STDOUT_FILENO
4809 && this->o_ != STDERR_FILENO
4810 && !this->is_temporary_)
4811 if (::close(this->o_) < 0)
4812 gold_error(_("%s: close: %s"), this->name_, strerror(errno));
4816 // Instantiate the templates we need. We could use the configure
4817 // script to restrict this to only the ones for implemented targets.
4819 #ifdef HAVE_TARGET_32_LITTLE
4822 Output_section::add_input_section<32, false>(
4824 Sized_relobj<32, false>* object,
4826 const char* secname,
4827 const elfcpp::Shdr<32, false>& shdr,
4828 unsigned int reloc_shndx,
4829 bool have_sections_script);
4832 #ifdef HAVE_TARGET_32_BIG
4835 Output_section::add_input_section<32, true>(
4837 Sized_relobj<32, true>* object,
4839 const char* secname,
4840 const elfcpp::Shdr<32, true>& shdr,
4841 unsigned int reloc_shndx,
4842 bool have_sections_script);
4845 #ifdef HAVE_TARGET_64_LITTLE
4848 Output_section::add_input_section<64, false>(
4850 Sized_relobj<64, false>* object,
4852 const char* secname,
4853 const elfcpp::Shdr<64, false>& shdr,
4854 unsigned int reloc_shndx,
4855 bool have_sections_script);
4858 #ifdef HAVE_TARGET_64_BIG
4861 Output_section::add_input_section<64, true>(
4863 Sized_relobj<64, true>* object,
4865 const char* secname,
4866 const elfcpp::Shdr<64, true>& shdr,
4867 unsigned int reloc_shndx,
4868 bool have_sections_script);
4871 #ifdef HAVE_TARGET_32_LITTLE
4873 class Output_reloc<elfcpp::SHT_REL, false, 32, false>;
4876 #ifdef HAVE_TARGET_32_BIG
4878 class Output_reloc<elfcpp::SHT_REL, false, 32, true>;
4881 #ifdef HAVE_TARGET_64_LITTLE
4883 class Output_reloc<elfcpp::SHT_REL, false, 64, false>;
4886 #ifdef HAVE_TARGET_64_BIG
4888 class Output_reloc<elfcpp::SHT_REL, false, 64, true>;
4891 #ifdef HAVE_TARGET_32_LITTLE
4893 class Output_reloc<elfcpp::SHT_REL, true, 32, false>;
4896 #ifdef HAVE_TARGET_32_BIG
4898 class Output_reloc<elfcpp::SHT_REL, true, 32, true>;
4901 #ifdef HAVE_TARGET_64_LITTLE
4903 class Output_reloc<elfcpp::SHT_REL, true, 64, false>;
4906 #ifdef HAVE_TARGET_64_BIG
4908 class Output_reloc<elfcpp::SHT_REL, true, 64, true>;
4911 #ifdef HAVE_TARGET_32_LITTLE
4913 class Output_reloc<elfcpp::SHT_RELA, false, 32, false>;
4916 #ifdef HAVE_TARGET_32_BIG
4918 class Output_reloc<elfcpp::SHT_RELA, false, 32, true>;
4921 #ifdef HAVE_TARGET_64_LITTLE
4923 class Output_reloc<elfcpp::SHT_RELA, false, 64, false>;
4926 #ifdef HAVE_TARGET_64_BIG
4928 class Output_reloc<elfcpp::SHT_RELA, false, 64, true>;
4931 #ifdef HAVE_TARGET_32_LITTLE
4933 class Output_reloc<elfcpp::SHT_RELA, true, 32, false>;
4936 #ifdef HAVE_TARGET_32_BIG
4938 class Output_reloc<elfcpp::SHT_RELA, true, 32, true>;
4941 #ifdef HAVE_TARGET_64_LITTLE
4943 class Output_reloc<elfcpp::SHT_RELA, true, 64, false>;
4946 #ifdef HAVE_TARGET_64_BIG
4948 class Output_reloc<elfcpp::SHT_RELA, true, 64, true>;
4951 #ifdef HAVE_TARGET_32_LITTLE
4953 class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
4956 #ifdef HAVE_TARGET_32_BIG
4958 class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
4961 #ifdef HAVE_TARGET_64_LITTLE
4963 class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
4966 #ifdef HAVE_TARGET_64_BIG
4968 class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
4971 #ifdef HAVE_TARGET_32_LITTLE
4973 class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
4976 #ifdef HAVE_TARGET_32_BIG
4978 class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
4981 #ifdef HAVE_TARGET_64_LITTLE
4983 class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
4986 #ifdef HAVE_TARGET_64_BIG
4988 class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
4991 #ifdef HAVE_TARGET_32_LITTLE
4993 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
4996 #ifdef HAVE_TARGET_32_BIG
4998 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
5001 #ifdef HAVE_TARGET_64_LITTLE
5003 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
5006 #ifdef HAVE_TARGET_64_BIG
5008 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
5011 #ifdef HAVE_TARGET_32_LITTLE
5013 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
5016 #ifdef HAVE_TARGET_32_BIG
5018 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
5021 #ifdef HAVE_TARGET_64_LITTLE
5023 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
5026 #ifdef HAVE_TARGET_64_BIG
5028 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
5031 #ifdef HAVE_TARGET_32_LITTLE
5033 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, false>;
5036 #ifdef HAVE_TARGET_32_BIG
5038 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, true>;
5041 #ifdef HAVE_TARGET_64_LITTLE
5043 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, false>;
5046 #ifdef HAVE_TARGET_64_BIG
5048 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, true>;
5051 #ifdef HAVE_TARGET_32_LITTLE
5053 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, false>;
5056 #ifdef HAVE_TARGET_32_BIG
5058 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, true>;
5061 #ifdef HAVE_TARGET_64_LITTLE
5063 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, false>;
5066 #ifdef HAVE_TARGET_64_BIG
5068 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, true>;
5071 #ifdef HAVE_TARGET_32_LITTLE
5073 class Output_data_group<32, false>;
5076 #ifdef HAVE_TARGET_32_BIG
5078 class Output_data_group<32, true>;
5081 #ifdef HAVE_TARGET_64_LITTLE
5083 class Output_data_group<64, false>;
5086 #ifdef HAVE_TARGET_64_BIG
5088 class Output_data_group<64, true>;
5091 #ifdef HAVE_TARGET_32_LITTLE
5093 class Output_data_got<32, false>;
5096 #ifdef HAVE_TARGET_32_BIG
5098 class Output_data_got<32, true>;
5101 #ifdef HAVE_TARGET_64_LITTLE
5103 class Output_data_got<64, false>;
5106 #ifdef HAVE_TARGET_64_BIG
5108 class Output_data_got<64, true>;
5111 } // End namespace gold.