1 // output.cc -- manage the output file for gold
3 // Copyright 2006, 2007, 2008, 2009 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 #include "libiberty.h"
35 #include "parameters.h"
40 #include "descriptors.h"
43 // Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS
45 # define MAP_ANONYMOUS MAP_ANON
48 #ifndef HAVE_POSIX_FALLOCATE
49 // A dummy, non general, version of posix_fallocate. Here we just set
50 // the file size and hope that there is enough disk space. FIXME: We
51 // could allocate disk space by walking block by block and writing a
52 // zero byte into each block.
54 posix_fallocate(int o, off_t offset, off_t len)
56 return ftruncate(o, offset + len);
58 #endif // !defined(HAVE_POSIX_FALLOCATE)
63 // Output_data variables.
65 bool Output_data::allocated_sizes_are_fixed;
67 // Output_data methods.
69 Output_data::~Output_data()
73 // Return the default alignment for the target size.
76 Output_data::default_alignment()
78 return Output_data::default_alignment_for_size(
79 parameters->target().get_size());
82 // Return the default alignment for a size--32 or 64.
85 Output_data::default_alignment_for_size(int size)
95 // Output_section_header methods. This currently assumes that the
96 // segment and section lists are complete at construction time.
98 Output_section_headers::Output_section_headers(
100 const Layout::Segment_list* segment_list,
101 const Layout::Section_list* section_list,
102 const Layout::Section_list* unattached_section_list,
103 const Stringpool* secnamepool,
104 const Output_section* shstrtab_section)
106 segment_list_(segment_list),
107 section_list_(section_list),
108 unattached_section_list_(unattached_section_list),
109 secnamepool_(secnamepool),
110 shstrtab_section_(shstrtab_section)
114 // Compute the current data size.
117 Output_section_headers::do_size() const
119 // Count all the sections. Start with 1 for the null section.
121 if (!parameters->options().relocatable())
123 for (Layout::Segment_list::const_iterator p =
124 this->segment_list_->begin();
125 p != this->segment_list_->end();
127 if ((*p)->type() == elfcpp::PT_LOAD)
128 count += (*p)->output_section_count();
132 for (Layout::Section_list::const_iterator p =
133 this->section_list_->begin();
134 p != this->section_list_->end();
136 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
139 count += this->unattached_section_list_->size();
141 const int size = parameters->target().get_size();
144 shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
146 shdr_size = elfcpp::Elf_sizes<64>::shdr_size;
150 return count * shdr_size;
153 // Write out the section headers.
156 Output_section_headers::do_write(Output_file* of)
158 switch (parameters->size_and_endianness())
160 #ifdef HAVE_TARGET_32_LITTLE
161 case Parameters::TARGET_32_LITTLE:
162 this->do_sized_write<32, false>(of);
165 #ifdef HAVE_TARGET_32_BIG
166 case Parameters::TARGET_32_BIG:
167 this->do_sized_write<32, true>(of);
170 #ifdef HAVE_TARGET_64_LITTLE
171 case Parameters::TARGET_64_LITTLE:
172 this->do_sized_write<64, false>(of);
175 #ifdef HAVE_TARGET_64_BIG
176 case Parameters::TARGET_64_BIG:
177 this->do_sized_write<64, true>(of);
185 template<int size, bool big_endian>
187 Output_section_headers::do_sized_write(Output_file* of)
189 off_t all_shdrs_size = this->data_size();
190 unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size);
192 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
193 unsigned char* v = view;
196 typename elfcpp::Shdr_write<size, big_endian> oshdr(v);
197 oshdr.put_sh_name(0);
198 oshdr.put_sh_type(elfcpp::SHT_NULL);
199 oshdr.put_sh_flags(0);
200 oshdr.put_sh_addr(0);
201 oshdr.put_sh_offset(0);
203 size_t section_count = (this->data_size()
204 / elfcpp::Elf_sizes<size>::shdr_size);
205 if (section_count < elfcpp::SHN_LORESERVE)
206 oshdr.put_sh_size(0);
208 oshdr.put_sh_size(section_count);
210 unsigned int shstrndx = this->shstrtab_section_->out_shndx();
211 if (shstrndx < elfcpp::SHN_LORESERVE)
212 oshdr.put_sh_link(0);
214 oshdr.put_sh_link(shstrndx);
216 oshdr.put_sh_info(0);
217 oshdr.put_sh_addralign(0);
218 oshdr.put_sh_entsize(0);
223 unsigned int shndx = 1;
224 if (!parameters->options().relocatable())
226 for (Layout::Segment_list::const_iterator p =
227 this->segment_list_->begin();
228 p != this->segment_list_->end();
230 v = (*p)->write_section_headers<size, big_endian>(this->layout_,
237 for (Layout::Section_list::const_iterator p =
238 this->section_list_->begin();
239 p != this->section_list_->end();
242 // We do unallocated sections below, except that group
243 // sections have to come first.
244 if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
245 && (*p)->type() != elfcpp::SHT_GROUP)
247 gold_assert(shndx == (*p)->out_shndx());
248 elfcpp::Shdr_write<size, big_endian> oshdr(v);
249 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
255 for (Layout::Section_list::const_iterator p =
256 this->unattached_section_list_->begin();
257 p != this->unattached_section_list_->end();
260 // For a relocatable link, we did unallocated group sections
261 // above, since they have to come first.
262 if ((*p)->type() == elfcpp::SHT_GROUP
263 && parameters->options().relocatable())
265 gold_assert(shndx == (*p)->out_shndx());
266 elfcpp::Shdr_write<size, big_endian> oshdr(v);
267 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
272 of->write_output_view(this->offset(), all_shdrs_size, view);
275 // Output_segment_header methods.
277 Output_segment_headers::Output_segment_headers(
278 const Layout::Segment_list& segment_list)
279 : segment_list_(segment_list)
284 Output_segment_headers::do_write(Output_file* of)
286 switch (parameters->size_and_endianness())
288 #ifdef HAVE_TARGET_32_LITTLE
289 case Parameters::TARGET_32_LITTLE:
290 this->do_sized_write<32, false>(of);
293 #ifdef HAVE_TARGET_32_BIG
294 case Parameters::TARGET_32_BIG:
295 this->do_sized_write<32, true>(of);
298 #ifdef HAVE_TARGET_64_LITTLE
299 case Parameters::TARGET_64_LITTLE:
300 this->do_sized_write<64, false>(of);
303 #ifdef HAVE_TARGET_64_BIG
304 case Parameters::TARGET_64_BIG:
305 this->do_sized_write<64, true>(of);
313 template<int size, bool big_endian>
315 Output_segment_headers::do_sized_write(Output_file* of)
317 const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size;
318 off_t all_phdrs_size = this->segment_list_.size() * phdr_size;
319 gold_assert(all_phdrs_size == this->data_size());
320 unsigned char* view = of->get_output_view(this->offset(),
322 unsigned char* v = view;
323 for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
324 p != this->segment_list_.end();
327 elfcpp::Phdr_write<size, big_endian> ophdr(v);
328 (*p)->write_header(&ophdr);
332 gold_assert(v - view == all_phdrs_size);
334 of->write_output_view(this->offset(), all_phdrs_size, view);
338 Output_segment_headers::do_size() const
340 const int size = parameters->target().get_size();
343 phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
345 phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
349 return this->segment_list_.size() * phdr_size;
352 // Output_file_header methods.
354 Output_file_header::Output_file_header(const Target* target,
355 const Symbol_table* symtab,
356 const Output_segment_headers* osh,
360 segment_header_(osh),
361 section_header_(NULL),
365 this->set_data_size(this->do_size());
368 // Set the section table information for a file header.
371 Output_file_header::set_section_info(const Output_section_headers* shdrs,
372 const Output_section* shstrtab)
374 this->section_header_ = shdrs;
375 this->shstrtab_ = shstrtab;
378 // Write out the file header.
381 Output_file_header::do_write(Output_file* of)
383 gold_assert(this->offset() == 0);
385 switch (parameters->size_and_endianness())
387 #ifdef HAVE_TARGET_32_LITTLE
388 case Parameters::TARGET_32_LITTLE:
389 this->do_sized_write<32, false>(of);
392 #ifdef HAVE_TARGET_32_BIG
393 case Parameters::TARGET_32_BIG:
394 this->do_sized_write<32, true>(of);
397 #ifdef HAVE_TARGET_64_LITTLE
398 case Parameters::TARGET_64_LITTLE:
399 this->do_sized_write<64, false>(of);
402 #ifdef HAVE_TARGET_64_BIG
403 case Parameters::TARGET_64_BIG:
404 this->do_sized_write<64, true>(of);
412 // Write out the file header with appropriate size and endianess.
414 template<int size, bool big_endian>
416 Output_file_header::do_sized_write(Output_file* of)
418 gold_assert(this->offset() == 0);
420 int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
421 unsigned char* view = of->get_output_view(0, ehdr_size);
422 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
424 unsigned char e_ident[elfcpp::EI_NIDENT];
425 memset(e_ident, 0, elfcpp::EI_NIDENT);
426 e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
427 e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
428 e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
429 e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
431 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
433 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
436 e_ident[elfcpp::EI_DATA] = (big_endian
437 ? elfcpp::ELFDATA2MSB
438 : elfcpp::ELFDATA2LSB);
439 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
440 oehdr.put_e_ident(e_ident);
443 if (parameters->options().relocatable())
444 e_type = elfcpp::ET_REL;
445 else if (parameters->options().output_is_position_independent())
446 e_type = elfcpp::ET_DYN;
448 e_type = elfcpp::ET_EXEC;
449 oehdr.put_e_type(e_type);
451 oehdr.put_e_machine(this->target_->machine_code());
452 oehdr.put_e_version(elfcpp::EV_CURRENT);
454 oehdr.put_e_entry(this->entry<size>());
456 if (this->segment_header_ == NULL)
457 oehdr.put_e_phoff(0);
459 oehdr.put_e_phoff(this->segment_header_->offset());
461 oehdr.put_e_shoff(this->section_header_->offset());
462 oehdr.put_e_flags(this->target_->processor_specific_flags());
463 oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
465 if (this->segment_header_ == NULL)
467 oehdr.put_e_phentsize(0);
468 oehdr.put_e_phnum(0);
472 oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
473 oehdr.put_e_phnum(this->segment_header_->data_size()
474 / elfcpp::Elf_sizes<size>::phdr_size);
477 oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
478 size_t section_count = (this->section_header_->data_size()
479 / elfcpp::Elf_sizes<size>::shdr_size);
481 if (section_count < elfcpp::SHN_LORESERVE)
482 oehdr.put_e_shnum(this->section_header_->data_size()
483 / elfcpp::Elf_sizes<size>::shdr_size);
485 oehdr.put_e_shnum(0);
487 unsigned int shstrndx = this->shstrtab_->out_shndx();
488 if (shstrndx < elfcpp::SHN_LORESERVE)
489 oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
491 oehdr.put_e_shstrndx(elfcpp::SHN_XINDEX);
493 // Let the target adjust the ELF header, e.g., to set EI_OSABI in
494 // the e_ident field.
495 parameters->target().adjust_elf_header(view, ehdr_size);
497 of->write_output_view(0, ehdr_size, view);
500 // Return the value to use for the entry address. THIS->ENTRY_ is the
501 // symbol specified on the command line, if any.
504 typename elfcpp::Elf_types<size>::Elf_Addr
505 Output_file_header::entry()
507 const bool should_issue_warning = (this->entry_ != NULL
508 && !parameters->options().relocatable()
509 && !parameters->options().shared());
511 // FIXME: Need to support target specific entry symbol.
512 const char* entry = this->entry_;
516 Symbol* sym = this->symtab_->lookup(entry);
518 typename Sized_symbol<size>::Value_type v;
521 Sized_symbol<size>* ssym;
522 ssym = this->symtab_->get_sized_symbol<size>(sym);
523 if (!ssym->is_defined() && should_issue_warning)
524 gold_warning("entry symbol '%s' exists but is not defined", entry);
529 // We couldn't find the entry symbol. See if we can parse it as
530 // a number. This supports, e.g., -e 0x1000.
532 v = strtoull(entry, &endptr, 0);
535 if (should_issue_warning)
536 gold_warning("cannot find entry symbol '%s'", entry);
544 // Compute the current data size.
547 Output_file_header::do_size() const
549 const int size = parameters->target().get_size();
551 return elfcpp::Elf_sizes<32>::ehdr_size;
553 return elfcpp::Elf_sizes<64>::ehdr_size;
558 // Output_data_const methods.
561 Output_data_const::do_write(Output_file* of)
563 of->write(this->offset(), this->data_.data(), this->data_.size());
566 // Output_data_const_buffer methods.
569 Output_data_const_buffer::do_write(Output_file* of)
571 of->write(this->offset(), this->p_, this->data_size());
574 // Output_section_data methods.
576 // Record the output section, and set the entry size and such.
579 Output_section_data::set_output_section(Output_section* os)
581 gold_assert(this->output_section_ == NULL);
582 this->output_section_ = os;
583 this->do_adjust_output_section(os);
586 // Return the section index of the output section.
589 Output_section_data::do_out_shndx() const
591 gold_assert(this->output_section_ != NULL);
592 return this->output_section_->out_shndx();
595 // Set the alignment, which means we may need to update the alignment
596 // of the output section.
599 Output_section_data::set_addralign(uint64_t addralign)
601 this->addralign_ = addralign;
602 if (this->output_section_ != NULL
603 && this->output_section_->addralign() < addralign)
604 this->output_section_->set_addralign(addralign);
607 // Output_data_strtab methods.
609 // Set the final data size.
612 Output_data_strtab::set_final_data_size()
614 this->strtab_->set_string_offsets();
615 this->set_data_size(this->strtab_->get_strtab_size());
618 // Write out a string table.
621 Output_data_strtab::do_write(Output_file* of)
623 this->strtab_->write(of, this->offset());
626 // Output_reloc methods.
628 // A reloc against a global symbol.
630 template<bool dynamic, int size, bool big_endian>
631 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
637 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
638 is_relative_(is_relative), is_section_symbol_(false), shndx_(INVALID_CODE)
640 // this->type_ is a bitfield; make sure TYPE fits.
641 gold_assert(this->type_ == type);
642 this->u1_.gsym = gsym;
645 this->set_needs_dynsym_index();
648 template<bool dynamic, int size, bool big_endian>
649 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
652 Sized_relobj<size, big_endian>* relobj,
656 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
657 is_relative_(is_relative), is_section_symbol_(false), shndx_(shndx)
659 gold_assert(shndx != INVALID_CODE);
660 // this->type_ is a bitfield; make sure TYPE fits.
661 gold_assert(this->type_ == type);
662 this->u1_.gsym = gsym;
663 this->u2_.relobj = relobj;
665 this->set_needs_dynsym_index();
668 // A reloc against a local symbol.
670 template<bool dynamic, int size, bool big_endian>
671 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
672 Sized_relobj<size, big_endian>* relobj,
673 unsigned int local_sym_index,
678 bool is_section_symbol)
679 : address_(address), local_sym_index_(local_sym_index), type_(type),
680 is_relative_(is_relative), is_section_symbol_(is_section_symbol),
683 gold_assert(local_sym_index != GSYM_CODE
684 && local_sym_index != INVALID_CODE);
685 // this->type_ is a bitfield; make sure TYPE fits.
686 gold_assert(this->type_ == type);
687 this->u1_.relobj = relobj;
690 this->set_needs_dynsym_index();
693 template<bool dynamic, int size, bool big_endian>
694 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
695 Sized_relobj<size, big_endian>* relobj,
696 unsigned int local_sym_index,
701 bool is_section_symbol)
702 : address_(address), local_sym_index_(local_sym_index), type_(type),
703 is_relative_(is_relative), is_section_symbol_(is_section_symbol),
706 gold_assert(local_sym_index != GSYM_CODE
707 && local_sym_index != INVALID_CODE);
708 gold_assert(shndx != INVALID_CODE);
709 // this->type_ is a bitfield; make sure TYPE fits.
710 gold_assert(this->type_ == type);
711 this->u1_.relobj = relobj;
712 this->u2_.relobj = relobj;
714 this->set_needs_dynsym_index();
717 // A reloc against the STT_SECTION symbol of an output section.
719 template<bool dynamic, int size, bool big_endian>
720 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
725 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
726 is_relative_(false), is_section_symbol_(true), shndx_(INVALID_CODE)
728 // this->type_ is a bitfield; make sure TYPE fits.
729 gold_assert(this->type_ == type);
733 this->set_needs_dynsym_index();
735 os->set_needs_symtab_index();
738 template<bool dynamic, int size, bool big_endian>
739 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
742 Sized_relobj<size, big_endian>* relobj,
745 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
746 is_relative_(false), is_section_symbol_(true), shndx_(shndx)
748 gold_assert(shndx != INVALID_CODE);
749 // this->type_ is a bitfield; make sure TYPE fits.
750 gold_assert(this->type_ == type);
752 this->u2_.relobj = relobj;
754 this->set_needs_dynsym_index();
756 os->set_needs_symtab_index();
759 // Record that we need a dynamic symbol index for this relocation.
761 template<bool dynamic, int size, bool big_endian>
763 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
764 set_needs_dynsym_index()
766 if (this->is_relative_)
768 switch (this->local_sym_index_)
774 this->u1_.gsym->set_needs_dynsym_entry();
778 this->u1_.os->set_needs_dynsym_index();
786 const unsigned int lsi = this->local_sym_index_;
787 if (!this->is_section_symbol_)
788 this->u1_.relobj->set_needs_output_dynsym_entry(lsi);
790 this->u1_.relobj->output_section(lsi)->set_needs_dynsym_index();
796 // Get the symbol index of a relocation.
798 template<bool dynamic, int size, bool big_endian>
800 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
804 switch (this->local_sym_index_)
810 if (this->u1_.gsym == NULL)
813 index = this->u1_.gsym->dynsym_index();
815 index = this->u1_.gsym->symtab_index();
820 index = this->u1_.os->dynsym_index();
822 index = this->u1_.os->symtab_index();
826 // Relocations without symbols use a symbol index of 0.
832 const unsigned int lsi = this->local_sym_index_;
833 if (!this->is_section_symbol_)
836 index = this->u1_.relobj->dynsym_index(lsi);
838 index = this->u1_.relobj->symtab_index(lsi);
842 Output_section* os = this->u1_.relobj->output_section(lsi);
843 gold_assert(os != NULL);
845 index = os->dynsym_index();
847 index = os->symtab_index();
852 gold_assert(index != -1U);
856 // For a local section symbol, get the address of the offset ADDEND
857 // within the input section.
859 template<bool dynamic, int size, bool big_endian>
860 typename elfcpp::Elf_types<size>::Elf_Addr
861 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
862 local_section_offset(Addend addend) const
864 gold_assert(this->local_sym_index_ != GSYM_CODE
865 && this->local_sym_index_ != SECTION_CODE
866 && this->local_sym_index_ != INVALID_CODE
867 && this->is_section_symbol_);
868 const unsigned int lsi = this->local_sym_index_;
869 Output_section* os = this->u1_.relobj->output_section(lsi);
870 gold_assert(os != NULL);
871 Address offset = this->u1_.relobj->get_output_section_offset(lsi);
872 if (offset != invalid_address)
873 return offset + addend;
874 // This is a merge section.
875 offset = os->output_address(this->u1_.relobj, lsi, addend);
876 gold_assert(offset != invalid_address);
880 // Get the output address of a relocation.
882 template<bool dynamic, int size, bool big_endian>
883 typename elfcpp::Elf_types<size>::Elf_Addr
884 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_address() const
886 Address address = this->address_;
887 if (this->shndx_ != INVALID_CODE)
889 Output_section* os = this->u2_.relobj->output_section(this->shndx_);
890 gold_assert(os != NULL);
891 Address off = this->u2_.relobj->get_output_section_offset(this->shndx_);
892 if (off != invalid_address)
893 address += os->address() + off;
896 address = os->output_address(this->u2_.relobj, this->shndx_,
898 gold_assert(address != invalid_address);
901 else if (this->u2_.od != NULL)
902 address += this->u2_.od->address();
906 // Write out the offset and info fields of a Rel or Rela relocation
909 template<bool dynamic, int size, bool big_endian>
910 template<typename Write_rel>
912 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
915 wr->put_r_offset(this->get_address());
916 unsigned int sym_index = this->is_relative_ ? 0 : this->get_symbol_index();
917 wr->put_r_info(elfcpp::elf_r_info<size>(sym_index, this->type_));
920 // Write out a Rel relocation.
922 template<bool dynamic, int size, bool big_endian>
924 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
925 unsigned char* pov) const
927 elfcpp::Rel_write<size, big_endian> orel(pov);
928 this->write_rel(&orel);
931 // Get the value of the symbol referred to by a Rel relocation.
933 template<bool dynamic, int size, bool big_endian>
934 typename elfcpp::Elf_types<size>::Elf_Addr
935 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value(
938 if (this->local_sym_index_ == GSYM_CODE)
940 const Sized_symbol<size>* sym;
941 sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym);
942 return sym->value() + addend;
944 gold_assert(this->local_sym_index_ != SECTION_CODE
945 && this->local_sym_index_ != INVALID_CODE
946 && !this->is_section_symbol_);
947 const unsigned int lsi = this->local_sym_index_;
948 const Symbol_value<size>* symval = this->u1_.relobj->local_symbol(lsi);
949 return symval->value(this->u1_.relobj, addend);
952 // Reloc comparison. This function sorts the dynamic relocs for the
953 // benefit of the dynamic linker. First we sort all relative relocs
954 // to the front. Among relative relocs, we sort by output address.
955 // Among non-relative relocs, we sort by symbol index, then by output
958 template<bool dynamic, int size, bool big_endian>
960 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
961 compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
964 if (this->is_relative_)
966 if (!r2.is_relative_)
968 // Otherwise sort by reloc address below.
970 else if (r2.is_relative_)
974 unsigned int sym1 = this->get_symbol_index();
975 unsigned int sym2 = r2.get_symbol_index();
978 else if (sym1 > sym2)
980 // Otherwise sort by reloc address.
983 section_offset_type addr1 = this->get_address();
984 section_offset_type addr2 = r2.get_address();
987 else if (addr1 > addr2)
990 // Final tie breaker, in order to generate the same output on any
992 unsigned int type1 = this->type_;
993 unsigned int type2 = r2.type_;
996 else if (type1 > type2)
999 // These relocs appear to be exactly the same.
1003 // Write out a Rela relocation.
1005 template<bool dynamic, int size, bool big_endian>
1007 Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
1008 unsigned char* pov) const
1010 elfcpp::Rela_write<size, big_endian> orel(pov);
1011 this->rel_.write_rel(&orel);
1012 Addend addend = this->addend_;
1013 if (this->rel_.is_relative())
1014 addend = this->rel_.symbol_value(addend);
1015 else if (this->rel_.is_local_section_symbol())
1016 addend = this->rel_.local_section_offset(addend);
1017 orel.put_r_addend(addend);
1020 // Output_data_reloc_base methods.
1022 // Adjust the output section.
1024 template<int sh_type, bool dynamic, int size, bool big_endian>
1026 Output_data_reloc_base<sh_type, dynamic, size, big_endian>
1027 ::do_adjust_output_section(Output_section* os)
1029 if (sh_type == elfcpp::SHT_REL)
1030 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
1031 else if (sh_type == elfcpp::SHT_RELA)
1032 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
1036 os->set_should_link_to_dynsym();
1038 os->set_should_link_to_symtab();
1041 // Write out relocation data.
1043 template<int sh_type, bool dynamic, int size, bool big_endian>
1045 Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
1048 const off_t off = this->offset();
1049 const off_t oview_size = this->data_size();
1050 unsigned char* const oview = of->get_output_view(off, oview_size);
1052 if (this->sort_relocs_)
1054 gold_assert(dynamic);
1055 std::sort(this->relocs_.begin(), this->relocs_.end(),
1056 Sort_relocs_comparison());
1059 unsigned char* pov = oview;
1060 for (typename Relocs::const_iterator p = this->relocs_.begin();
1061 p != this->relocs_.end();
1068 gold_assert(pov - oview == oview_size);
1070 of->write_output_view(off, oview_size, oview);
1072 // We no longer need the relocation entries.
1073 this->relocs_.clear();
1076 // Class Output_relocatable_relocs.
1078 template<int sh_type, int size, bool big_endian>
1080 Output_relocatable_relocs<sh_type, size, big_endian>::set_final_data_size()
1082 this->set_data_size(this->rr_->output_reloc_count()
1083 * Reloc_types<sh_type, size, big_endian>::reloc_size);
1086 // class Output_data_group.
1088 template<int size, bool big_endian>
1089 Output_data_group<size, big_endian>::Output_data_group(
1090 Sized_relobj<size, big_endian>* relobj,
1091 section_size_type entry_count,
1092 elfcpp::Elf_Word flags,
1093 std::vector<unsigned int>* input_shndxes)
1094 : Output_section_data(entry_count * 4, 4, false),
1098 this->input_shndxes_.swap(*input_shndxes);
1101 // Write out the section group, which means translating the section
1102 // indexes to apply to the output file.
1104 template<int size, bool big_endian>
1106 Output_data_group<size, big_endian>::do_write(Output_file* of)
1108 const off_t off = this->offset();
1109 const section_size_type oview_size =
1110 convert_to_section_size_type(this->data_size());
1111 unsigned char* const oview = of->get_output_view(off, oview_size);
1113 elfcpp::Elf_Word* contents = reinterpret_cast<elfcpp::Elf_Word*>(oview);
1114 elfcpp::Swap<32, big_endian>::writeval(contents, this->flags_);
1117 for (std::vector<unsigned int>::const_iterator p =
1118 this->input_shndxes_.begin();
1119 p != this->input_shndxes_.end();
1122 Output_section* os = this->relobj_->output_section(*p);
1124 unsigned int output_shndx;
1126 output_shndx = os->out_shndx();
1129 this->relobj_->error(_("section group retained but "
1130 "group element discarded"));
1134 elfcpp::Swap<32, big_endian>::writeval(contents, output_shndx);
1137 size_t wrote = reinterpret_cast<unsigned char*>(contents) - oview;
1138 gold_assert(wrote == oview_size);
1140 of->write_output_view(off, oview_size, oview);
1142 // We no longer need this information.
1143 this->input_shndxes_.clear();
1146 // Output_data_got::Got_entry methods.
1148 // Write out the entry.
1150 template<int size, bool big_endian>
1152 Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
1156 switch (this->local_sym_index_)
1160 // If the symbol is resolved locally, we need to write out the
1161 // link-time value, which will be relocated dynamically by a
1162 // RELATIVE relocation.
1163 Symbol* gsym = this->u_.gsym;
1164 Sized_symbol<size>* sgsym;
1165 // This cast is a bit ugly. We don't want to put a
1166 // virtual method in Symbol, because we want Symbol to be
1167 // as small as possible.
1168 sgsym = static_cast<Sized_symbol<size>*>(gsym);
1169 val = sgsym->value();
1174 val = this->u_.constant;
1179 const unsigned int lsi = this->local_sym_index_;
1180 const Symbol_value<size>* symval = this->u_.object->local_symbol(lsi);
1181 val = symval->value(this->u_.object, 0);
1186 elfcpp::Swap<size, big_endian>::writeval(pov, val);
1189 // Output_data_got methods.
1191 // Add an entry for a global symbol to the GOT. This returns true if
1192 // this is a new GOT entry, false if the symbol already had a GOT
1195 template<int size, bool big_endian>
1197 Output_data_got<size, big_endian>::add_global(
1199 unsigned int got_type)
1201 if (gsym->has_got_offset(got_type))
1204 this->entries_.push_back(Got_entry(gsym));
1205 this->set_got_size();
1206 gsym->set_got_offset(got_type, this->last_got_offset());
1210 // Add an entry for a global symbol to the GOT, and add a dynamic
1211 // relocation of type R_TYPE for the GOT entry.
1212 template<int size, bool big_endian>
1214 Output_data_got<size, big_endian>::add_global_with_rel(
1216 unsigned int got_type,
1218 unsigned int r_type)
1220 if (gsym->has_got_offset(got_type))
1223 this->entries_.push_back(Got_entry());
1224 this->set_got_size();
1225 unsigned int got_offset = this->last_got_offset();
1226 gsym->set_got_offset(got_type, got_offset);
1227 rel_dyn->add_global(gsym, r_type, this, got_offset);
1230 template<int size, bool big_endian>
1232 Output_data_got<size, big_endian>::add_global_with_rela(
1234 unsigned int got_type,
1236 unsigned int r_type)
1238 if (gsym->has_got_offset(got_type))
1241 this->entries_.push_back(Got_entry());
1242 this->set_got_size();
1243 unsigned int got_offset = this->last_got_offset();
1244 gsym->set_got_offset(got_type, got_offset);
1245 rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
1248 // Add a pair of entries for a global symbol to the GOT, and add
1249 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1250 // If R_TYPE_2 == 0, add the second entry with no relocation.
1251 template<int size, bool big_endian>
1253 Output_data_got<size, big_endian>::add_global_pair_with_rel(
1255 unsigned int got_type,
1257 unsigned int r_type_1,
1258 unsigned int r_type_2)
1260 if (gsym->has_got_offset(got_type))
1263 this->entries_.push_back(Got_entry());
1264 unsigned int got_offset = this->last_got_offset();
1265 gsym->set_got_offset(got_type, got_offset);
1266 rel_dyn->add_global(gsym, r_type_1, this, got_offset);
1268 this->entries_.push_back(Got_entry());
1271 got_offset = this->last_got_offset();
1272 rel_dyn->add_global(gsym, r_type_2, this, got_offset);
1275 this->set_got_size();
1278 template<int size, bool big_endian>
1280 Output_data_got<size, big_endian>::add_global_pair_with_rela(
1282 unsigned int got_type,
1284 unsigned int r_type_1,
1285 unsigned int r_type_2)
1287 if (gsym->has_got_offset(got_type))
1290 this->entries_.push_back(Got_entry());
1291 unsigned int got_offset = this->last_got_offset();
1292 gsym->set_got_offset(got_type, got_offset);
1293 rela_dyn->add_global(gsym, r_type_1, this, got_offset, 0);
1295 this->entries_.push_back(Got_entry());
1298 got_offset = this->last_got_offset();
1299 rela_dyn->add_global(gsym, r_type_2, this, got_offset, 0);
1302 this->set_got_size();
1305 // Add an entry for a local symbol to the GOT. This returns true if
1306 // this is a new GOT entry, false if the symbol already has a GOT
1309 template<int size, bool big_endian>
1311 Output_data_got<size, big_endian>::add_local(
1312 Sized_relobj<size, big_endian>* object,
1313 unsigned int symndx,
1314 unsigned int got_type)
1316 if (object->local_has_got_offset(symndx, got_type))
1319 this->entries_.push_back(Got_entry(object, symndx));
1320 this->set_got_size();
1321 object->set_local_got_offset(symndx, got_type, this->last_got_offset());
1325 // Add an entry for a local symbol to the GOT, and add a dynamic
1326 // relocation of type R_TYPE for the GOT entry.
1327 template<int size, bool big_endian>
1329 Output_data_got<size, big_endian>::add_local_with_rel(
1330 Sized_relobj<size, big_endian>* object,
1331 unsigned int symndx,
1332 unsigned int got_type,
1334 unsigned int r_type)
1336 if (object->local_has_got_offset(symndx, got_type))
1339 this->entries_.push_back(Got_entry());
1340 this->set_got_size();
1341 unsigned int got_offset = this->last_got_offset();
1342 object->set_local_got_offset(symndx, got_type, got_offset);
1343 rel_dyn->add_local(object, symndx, r_type, this, got_offset);
1346 template<int size, bool big_endian>
1348 Output_data_got<size, big_endian>::add_local_with_rela(
1349 Sized_relobj<size, big_endian>* object,
1350 unsigned int symndx,
1351 unsigned int got_type,
1353 unsigned int r_type)
1355 if (object->local_has_got_offset(symndx, got_type))
1358 this->entries_.push_back(Got_entry());
1359 this->set_got_size();
1360 unsigned int got_offset = this->last_got_offset();
1361 object->set_local_got_offset(symndx, got_type, got_offset);
1362 rela_dyn->add_local(object, symndx, r_type, this, got_offset, 0);
1365 // Add a pair of entries for a local symbol to the GOT, and add
1366 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1367 // If R_TYPE_2 == 0, add the second entry with no relocation.
1368 template<int size, bool big_endian>
1370 Output_data_got<size, big_endian>::add_local_pair_with_rel(
1371 Sized_relobj<size, big_endian>* object,
1372 unsigned int symndx,
1374 unsigned int got_type,
1376 unsigned int r_type_1,
1377 unsigned int r_type_2)
1379 if (object->local_has_got_offset(symndx, got_type))
1382 this->entries_.push_back(Got_entry());
1383 unsigned int got_offset = this->last_got_offset();
1384 object->set_local_got_offset(symndx, got_type, got_offset);
1385 Output_section* os = object->output_section(shndx);
1386 rel_dyn->add_output_section(os, r_type_1, this, got_offset);
1388 this->entries_.push_back(Got_entry(object, symndx));
1391 got_offset = this->last_got_offset();
1392 rel_dyn->add_output_section(os, r_type_2, this, got_offset);
1395 this->set_got_size();
1398 template<int size, bool big_endian>
1400 Output_data_got<size, big_endian>::add_local_pair_with_rela(
1401 Sized_relobj<size, big_endian>* object,
1402 unsigned int symndx,
1404 unsigned int got_type,
1406 unsigned int r_type_1,
1407 unsigned int r_type_2)
1409 if (object->local_has_got_offset(symndx, got_type))
1412 this->entries_.push_back(Got_entry());
1413 unsigned int got_offset = this->last_got_offset();
1414 object->set_local_got_offset(symndx, got_type, got_offset);
1415 Output_section* os = object->output_section(shndx);
1416 rela_dyn->add_output_section(os, r_type_1, this, got_offset, 0);
1418 this->entries_.push_back(Got_entry(object, symndx));
1421 got_offset = this->last_got_offset();
1422 rela_dyn->add_output_section(os, r_type_2, this, got_offset, 0);
1425 this->set_got_size();
1428 // Write out the GOT.
1430 template<int size, bool big_endian>
1432 Output_data_got<size, big_endian>::do_write(Output_file* of)
1434 const int add = size / 8;
1436 const off_t off = this->offset();
1437 const off_t oview_size = this->data_size();
1438 unsigned char* const oview = of->get_output_view(off, oview_size);
1440 unsigned char* pov = oview;
1441 for (typename Got_entries::const_iterator p = this->entries_.begin();
1442 p != this->entries_.end();
1449 gold_assert(pov - oview == oview_size);
1451 of->write_output_view(off, oview_size, oview);
1453 // We no longer need the GOT entries.
1454 this->entries_.clear();
1457 // Output_data_dynamic::Dynamic_entry methods.
1459 // Write out the entry.
1461 template<int size, bool big_endian>
1463 Output_data_dynamic::Dynamic_entry::write(
1465 const Stringpool* pool) const
1467 typename elfcpp::Elf_types<size>::Elf_WXword val;
1468 switch (this->offset_)
1470 case DYNAMIC_NUMBER:
1474 case DYNAMIC_SECTION_SIZE:
1475 val = this->u_.od->data_size();
1478 case DYNAMIC_SYMBOL:
1480 const Sized_symbol<size>* s =
1481 static_cast<const Sized_symbol<size>*>(this->u_.sym);
1486 case DYNAMIC_STRING:
1487 val = pool->get_offset(this->u_.str);
1491 val = this->u_.od->address() + this->offset_;
1495 elfcpp::Dyn_write<size, big_endian> dw(pov);
1496 dw.put_d_tag(this->tag_);
1500 // Output_data_dynamic methods.
1502 // Adjust the output section to set the entry size.
1505 Output_data_dynamic::do_adjust_output_section(Output_section* os)
1507 if (parameters->target().get_size() == 32)
1508 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
1509 else if (parameters->target().get_size() == 64)
1510 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1515 // Set the final data size.
1518 Output_data_dynamic::set_final_data_size()
1520 // Add the terminating entry if it hasn't been added.
1521 // Because of relaxation, we can run this multiple times.
1522 if (this->entries_.empty()
1523 || this->entries_.rbegin()->tag() != elfcpp::DT_NULL)
1524 this->add_constant(elfcpp::DT_NULL, 0);
1527 if (parameters->target().get_size() == 32)
1528 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
1529 else if (parameters->target().get_size() == 64)
1530 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1533 this->set_data_size(this->entries_.size() * dyn_size);
1536 // Write out the dynamic entries.
1539 Output_data_dynamic::do_write(Output_file* of)
1541 switch (parameters->size_and_endianness())
1543 #ifdef HAVE_TARGET_32_LITTLE
1544 case Parameters::TARGET_32_LITTLE:
1545 this->sized_write<32, false>(of);
1548 #ifdef HAVE_TARGET_32_BIG
1549 case Parameters::TARGET_32_BIG:
1550 this->sized_write<32, true>(of);
1553 #ifdef HAVE_TARGET_64_LITTLE
1554 case Parameters::TARGET_64_LITTLE:
1555 this->sized_write<64, false>(of);
1558 #ifdef HAVE_TARGET_64_BIG
1559 case Parameters::TARGET_64_BIG:
1560 this->sized_write<64, true>(of);
1568 template<int size, bool big_endian>
1570 Output_data_dynamic::sized_write(Output_file* of)
1572 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1574 const off_t offset = this->offset();
1575 const off_t oview_size = this->data_size();
1576 unsigned char* const oview = of->get_output_view(offset, oview_size);
1578 unsigned char* pov = oview;
1579 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1580 p != this->entries_.end();
1583 p->write<size, big_endian>(pov, this->pool_);
1587 gold_assert(pov - oview == oview_size);
1589 of->write_output_view(offset, oview_size, oview);
1591 // We no longer need the dynamic entries.
1592 this->entries_.clear();
1595 // Class Output_symtab_xindex.
1598 Output_symtab_xindex::do_write(Output_file* of)
1600 const off_t offset = this->offset();
1601 const off_t oview_size = this->data_size();
1602 unsigned char* const oview = of->get_output_view(offset, oview_size);
1604 memset(oview, 0, oview_size);
1606 if (parameters->target().is_big_endian())
1607 this->endian_do_write<true>(oview);
1609 this->endian_do_write<false>(oview);
1611 of->write_output_view(offset, oview_size, oview);
1613 // We no longer need the data.
1614 this->entries_.clear();
1617 template<bool big_endian>
1619 Output_symtab_xindex::endian_do_write(unsigned char* const oview)
1621 for (Xindex_entries::const_iterator p = this->entries_.begin();
1622 p != this->entries_.end();
1625 unsigned int symndx = p->first;
1626 gold_assert(symndx * 4 < this->data_size());
1627 elfcpp::Swap<32, big_endian>::writeval(oview + symndx * 4, p->second);
1631 // Output_section::Input_section methods.
1633 // Return the data size. For an input section we store the size here.
1634 // For an Output_section_data, we have to ask it for the size.
1637 Output_section::Input_section::data_size() const
1639 if (this->is_input_section())
1640 return this->u1_.data_size;
1642 return this->u2_.posd->data_size();
1645 // Set the address and file offset.
1648 Output_section::Input_section::set_address_and_file_offset(
1651 off_t section_file_offset)
1653 if (this->is_input_section())
1654 this->u2_.object->set_section_offset(this->shndx_,
1655 file_offset - section_file_offset);
1657 this->u2_.posd->set_address_and_file_offset(address, file_offset);
1660 // Reset the address and file offset.
1663 Output_section::Input_section::reset_address_and_file_offset()
1665 if (!this->is_input_section())
1666 this->u2_.posd->reset_address_and_file_offset();
1669 // Finalize the data size.
1672 Output_section::Input_section::finalize_data_size()
1674 if (!this->is_input_section())
1675 this->u2_.posd->finalize_data_size();
1678 // Try to turn an input offset into an output offset. We want to
1679 // return the output offset relative to the start of this
1680 // Input_section in the output section.
1683 Output_section::Input_section::output_offset(
1684 const Relobj* object,
1686 section_offset_type offset,
1687 section_offset_type *poutput) const
1689 if (!this->is_input_section())
1690 return this->u2_.posd->output_offset(object, shndx, offset, poutput);
1693 if (this->shndx_ != shndx || this->u2_.object != object)
1700 // Return whether this is the merge section for the input section
1704 Output_section::Input_section::is_merge_section_for(const Relobj* object,
1705 unsigned int shndx) const
1707 if (this->is_input_section())
1709 return this->u2_.posd->is_merge_section_for(object, shndx);
1712 // Write out the data. We don't have to do anything for an input
1713 // section--they are handled via Object::relocate--but this is where
1714 // we write out the data for an Output_section_data.
1717 Output_section::Input_section::write(Output_file* of)
1719 if (!this->is_input_section())
1720 this->u2_.posd->write(of);
1723 // Write the data to a buffer. As for write(), we don't have to do
1724 // anything for an input section.
1727 Output_section::Input_section::write_to_buffer(unsigned char* buffer)
1729 if (!this->is_input_section())
1730 this->u2_.posd->write_to_buffer(buffer);
1733 // Print to a map file.
1736 Output_section::Input_section::print_to_mapfile(Mapfile* mapfile) const
1738 switch (this->shndx_)
1740 case OUTPUT_SECTION_CODE:
1741 case MERGE_DATA_SECTION_CODE:
1742 case MERGE_STRING_SECTION_CODE:
1743 this->u2_.posd->print_to_mapfile(mapfile);
1746 case RELAXED_INPUT_SECTION_CODE:
1748 Output_relaxed_input_section* relaxed_section =
1749 this->relaxed_input_section();
1750 mapfile->print_input_section(relaxed_section->relobj(),
1751 relaxed_section->shndx());
1755 mapfile->print_input_section(this->u2_.object, this->shndx_);
1760 // Output_section methods.
1762 // Construct an Output_section. NAME will point into a Stringpool.
1764 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
1765 elfcpp::Elf_Xword flags)
1770 link_section_(NULL),
1772 info_section_(NULL),
1781 first_input_offset_(0),
1783 postprocessing_buffer_(NULL),
1784 needs_symtab_index_(false),
1785 needs_dynsym_index_(false),
1786 should_link_to_symtab_(false),
1787 should_link_to_dynsym_(false),
1788 after_input_sections_(false),
1789 requires_postprocessing_(false),
1790 found_in_sections_clause_(false),
1791 has_load_address_(false),
1792 info_uses_section_index_(false),
1793 may_sort_attached_input_sections_(false),
1794 must_sort_attached_input_sections_(false),
1795 attached_input_sections_are_sorted_(false),
1797 is_relro_local_(false),
1798 is_small_section_(false),
1799 is_large_section_(false),
1801 is_dynamic_linker_section_(false),
1802 generate_code_fills_at_write_(false),
1803 is_entsize_zero_(false),
1806 merge_section_map_(),
1807 merge_section_by_properties_map_(),
1808 relaxed_input_section_map_(),
1809 is_relaxed_input_section_map_valid_(true)
1811 // An unallocated section has no address. Forcing this means that
1812 // we don't need special treatment for symbols defined in debug
1814 if ((flags & elfcpp::SHF_ALLOC) == 0)
1815 this->set_address(0);
1818 Output_section::~Output_section()
1820 delete this->checkpoint_;
1823 // Set the entry size.
1826 Output_section::set_entsize(uint64_t v)
1828 if (this->is_entsize_zero_)
1830 else if (this->entsize_ == 0)
1832 else if (this->entsize_ != v)
1835 this->is_entsize_zero_ = 1;
1839 // Add the input section SHNDX, with header SHDR, named SECNAME, in
1840 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
1841 // relocation section which applies to this section, or 0 if none, or
1842 // -1U if more than one. Return the offset of the input section
1843 // within the output section. Return -1 if the input section will
1844 // receive special handling. In the normal case we don't always keep
1845 // track of input sections for an Output_section. Instead, each
1846 // Object keeps track of the Output_section for each of its input
1847 // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
1848 // track of input sections here; this is used when SECTIONS appears in
1851 template<int size, bool big_endian>
1853 Output_section::add_input_section(Sized_relobj<size, big_endian>* object,
1855 const char* secname,
1856 const elfcpp::Shdr<size, big_endian>& shdr,
1857 unsigned int reloc_shndx,
1858 bool have_sections_script)
1860 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
1861 if ((addralign & (addralign - 1)) != 0)
1863 object->error(_("invalid alignment %lu for section \"%s\""),
1864 static_cast<unsigned long>(addralign), secname);
1868 if (addralign > this->addralign_)
1869 this->addralign_ = addralign;
1871 typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
1872 uint64_t entsize = shdr.get_sh_entsize();
1874 // .debug_str is a mergeable string section, but is not always so
1875 // marked by compilers. Mark manually here so we can optimize.
1876 if (strcmp(secname, ".debug_str") == 0)
1878 sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
1882 this->update_flags_for_input_section(sh_flags);
1883 this->set_entsize(entsize);
1885 // If this is a SHF_MERGE section, we pass all the input sections to
1886 // a Output_data_merge. We don't try to handle relocations for such
1887 // a section. We don't try to handle empty merge sections--they
1888 // mess up the mappings, and are useless anyhow.
1889 if ((sh_flags & elfcpp::SHF_MERGE) != 0
1891 && shdr.get_sh_size() > 0)
1893 if (this->add_merge_input_section(object, shndx, sh_flags,
1894 entsize, addralign))
1896 // Tell the relocation routines that they need to call the
1897 // output_offset method to determine the final address.
1902 off_t offset_in_section = this->current_data_size_for_child();
1903 off_t aligned_offset_in_section = align_address(offset_in_section,
1906 // Determine if we want to delay code-fill generation until the output
1907 // section is written. When the target is relaxing, we want to delay fill
1908 // generating to avoid adjusting them during relaxation.
1909 if (!this->generate_code_fills_at_write_
1910 && !have_sections_script
1911 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
1912 && parameters->target().has_code_fill()
1913 && parameters->target().may_relax())
1915 gold_assert(this->fills_.empty());
1916 this->generate_code_fills_at_write_ = true;
1919 if (aligned_offset_in_section > offset_in_section
1920 && !this->generate_code_fills_at_write_
1921 && !have_sections_script
1922 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
1923 && parameters->target().has_code_fill())
1925 // We need to add some fill data. Using fill_list_ when
1926 // possible is an optimization, since we will often have fill
1927 // sections without input sections.
1928 off_t fill_len = aligned_offset_in_section - offset_in_section;
1929 if (this->input_sections_.empty())
1930 this->fills_.push_back(Fill(offset_in_section, fill_len));
1933 std::string fill_data(parameters->target().code_fill(fill_len));
1934 Output_data_const* odc = new Output_data_const(fill_data, 1);
1935 this->input_sections_.push_back(Input_section(odc));
1939 this->set_current_data_size_for_child(aligned_offset_in_section
1940 + shdr.get_sh_size());
1942 // We need to keep track of this section if we are already keeping
1943 // track of sections, or if we are relaxing. Also, if this is a
1944 // section which requires sorting, or which may require sorting in
1945 // the future, we keep track of the sections.
1946 if (have_sections_script
1947 || !this->input_sections_.empty()
1948 || this->may_sort_attached_input_sections()
1949 || this->must_sort_attached_input_sections()
1950 || parameters->options().user_set_Map()
1951 || parameters->target().may_relax())
1952 this->input_sections_.push_back(Input_section(object, shndx,
1956 return aligned_offset_in_section;
1959 // Add arbitrary data to an output section.
1962 Output_section::add_output_section_data(Output_section_data* posd)
1964 Input_section inp(posd);
1965 this->add_output_section_data(&inp);
1967 if (posd->is_data_size_valid())
1969 off_t offset_in_section = this->current_data_size_for_child();
1970 off_t aligned_offset_in_section = align_address(offset_in_section,
1972 this->set_current_data_size_for_child(aligned_offset_in_section
1973 + posd->data_size());
1977 // Add a relaxed input section.
1980 Output_section::add_relaxed_input_section(Output_relaxed_input_section* poris)
1982 Input_section inp(poris);
1983 this->add_output_section_data(&inp);
1984 if (this->is_relaxed_input_section_map_valid_)
1986 Input_section_specifier iss(poris->relobj(), poris->shndx());
1987 this->relaxed_input_section_map_[iss] = poris;
1990 // For a relaxed section, we use the current data size. Linker scripts
1991 // get all the input sections, including relaxed one from an output
1992 // section and add them back to them same output section to compute the
1993 // output section size. If we do not account for sizes of relaxed input
1994 // sections, an output section would be incorrectly sized.
1995 off_t offset_in_section = this->current_data_size_for_child();
1996 off_t aligned_offset_in_section = align_address(offset_in_section,
1997 poris->addralign());
1998 this->set_current_data_size_for_child(aligned_offset_in_section
1999 + poris->current_data_size());
2002 // Add arbitrary data to an output section by Input_section.
2005 Output_section::add_output_section_data(Input_section* inp)
2007 if (this->input_sections_.empty())
2008 this->first_input_offset_ = this->current_data_size_for_child();
2010 this->input_sections_.push_back(*inp);
2012 uint64_t addralign = inp->addralign();
2013 if (addralign > this->addralign_)
2014 this->addralign_ = addralign;
2016 inp->set_output_section(this);
2019 // Add a merge section to an output section.
2022 Output_section::add_output_merge_section(Output_section_data* posd,
2023 bool is_string, uint64_t entsize)
2025 Input_section inp(posd, is_string, entsize);
2026 this->add_output_section_data(&inp);
2029 // Add an input section to a SHF_MERGE section.
2032 Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
2033 uint64_t flags, uint64_t entsize,
2036 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
2038 // We only merge strings if the alignment is not more than the
2039 // character size. This could be handled, but it's unusual.
2040 if (is_string && addralign > entsize)
2043 // We cannot restore merged input section states.
2044 gold_assert(this->checkpoint_ == NULL);
2046 // Look up merge sections by required properties.
2047 Merge_section_properties msp(is_string, entsize, addralign);
2048 Merge_section_by_properties_map::const_iterator p =
2049 this->merge_section_by_properties_map_.find(msp);
2050 if (p != this->merge_section_by_properties_map_.end())
2052 Output_merge_base* merge_section = p->second;
2053 merge_section->add_input_section(object, shndx);
2054 gold_assert(merge_section->is_string() == is_string
2055 && merge_section->entsize() == entsize
2056 && merge_section->addralign() == addralign);
2058 // Link input section to found merge section.
2059 Input_section_specifier iss(object, shndx);
2060 this->merge_section_map_[iss] = merge_section;
2064 // We handle the actual constant merging in Output_merge_data or
2065 // Output_merge_string_data.
2066 Output_merge_base* pomb;
2068 pomb = new Output_merge_data(entsize, addralign);
2074 pomb = new Output_merge_string<char>(addralign);
2077 pomb = new Output_merge_string<uint16_t>(addralign);
2080 pomb = new Output_merge_string<uint32_t>(addralign);
2087 // Add new merge section to this output section and link merge section
2088 // properties to new merge section in map.
2089 this->add_output_merge_section(pomb, is_string, entsize);
2090 this->merge_section_by_properties_map_[msp] = pomb;
2092 // Add input section to new merge section and link input section to new
2093 // merge section in map.
2094 pomb->add_input_section(object, shndx);
2095 Input_section_specifier iss(object, shndx);
2096 this->merge_section_map_[iss] = pomb;
2101 // Build a relaxation map to speed up relaxation of existing input sections.
2102 // Look up to the first LIMIT elements in INPUT_SECTIONS.
2105 Output_section::build_relaxation_map(
2106 const Input_section_list& input_sections,
2108 Relaxation_map* relaxation_map) const
2110 for (size_t i = 0; i < limit; ++i)
2112 const Input_section& is(input_sections[i]);
2113 if (is.is_input_section() || is.is_relaxed_input_section())
2115 Input_section_specifier iss(is.relobj(), is.shndx());
2116 (*relaxation_map)[iss] = i;
2121 // Convert regular input sections in INPUT_SECTIONS into relaxed input
2122 // sections in RELAXED_SECTIONS. MAP is a prebuilt map from input section
2123 // specifier to indices of INPUT_SECTIONS.
2126 Output_section::convert_input_sections_in_list_to_relaxed_sections(
2127 const std::vector<Output_relaxed_input_section*>& relaxed_sections,
2128 const Relaxation_map& map,
2129 Input_section_list* input_sections)
2131 for (size_t i = 0; i < relaxed_sections.size(); ++i)
2133 Output_relaxed_input_section* poris = relaxed_sections[i];
2134 Input_section_specifier iss(poris->relobj(), poris->shndx());
2135 Relaxation_map::const_iterator p = map.find(iss);
2136 gold_assert(p != map.end());
2137 gold_assert((*input_sections)[p->second].is_input_section());
2138 (*input_sections)[p->second] = Input_section(poris);
2142 // Convert regular input sections into relaxed input sections. RELAXED_SECTIONS
2143 // is a vector of pointers to Output_relaxed_input_section or its derived
2144 // classes. The relaxed sections must correspond to existing input sections.
2147 Output_section::convert_input_sections_to_relaxed_sections(
2148 const std::vector<Output_relaxed_input_section*>& relaxed_sections)
2150 gold_assert(parameters->target().may_relax());
2152 // We want to make sure that restore_states does not undo the effect of
2153 // this. If there is no checkpoint active, just search the current
2154 // input section list and replace the sections there. If there is
2155 // a checkpoint, also replace the sections there.
2157 // By default, we look at the whole list.
2158 size_t limit = this->input_sections_.size();
2160 if (this->checkpoint_ != NULL)
2162 // Replace input sections with relaxed input section in the saved
2163 // copy of the input section list.
2164 if (this->checkpoint_->input_sections_saved())
2167 this->build_relaxation_map(
2168 *(this->checkpoint_->input_sections()),
2169 this->checkpoint_->input_sections()->size(),
2171 this->convert_input_sections_in_list_to_relaxed_sections(
2174 this->checkpoint_->input_sections());
2178 // We have not copied the input section list yet. Instead, just
2179 // look at the portion that would be saved.
2180 limit = this->checkpoint_->input_sections_size();
2184 // Convert input sections in input_section_list.
2186 this->build_relaxation_map(this->input_sections_, limit, &map);
2187 this->convert_input_sections_in_list_to_relaxed_sections(
2190 &this->input_sections_);
2193 // Update the output section flags based on input section flags.
2196 Output_section::update_flags_for_input_section(elfcpp::Elf_Xword flags)
2198 // If we created the section with SHF_ALLOC clear, we set the
2199 // address. If we are now setting the SHF_ALLOC flag, we need to
2201 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0
2202 && (flags & elfcpp::SHF_ALLOC) != 0)
2203 this->mark_address_invalid();
2205 this->flags_ |= (flags
2206 & (elfcpp::SHF_WRITE
2208 | elfcpp::SHF_EXECINSTR));
2210 if ((flags & elfcpp::SHF_MERGE) == 0)
2211 this->flags_ &=~ elfcpp::SHF_MERGE;
2214 if (this->current_data_size_for_child() == 0)
2215 this->flags_ |= elfcpp::SHF_MERGE;
2218 if ((flags & elfcpp::SHF_STRINGS) == 0)
2219 this->flags_ &=~ elfcpp::SHF_STRINGS;
2222 if (this->current_data_size_for_child() == 0)
2223 this->flags_ |= elfcpp::SHF_STRINGS;
2227 // Find the merge section into which an input section with index SHNDX in
2228 // OBJECT has been added. Return NULL if none found.
2230 Output_section_data*
2231 Output_section::find_merge_section(const Relobj* object,
2232 unsigned int shndx) const
2234 Input_section_specifier iss(object, shndx);
2235 Output_section_data_by_input_section_map::const_iterator p =
2236 this->merge_section_map_.find(iss);
2237 if (p != this->merge_section_map_.end())
2239 Output_section_data* posd = p->second;
2240 gold_assert(posd->is_merge_section_for(object, shndx));
2247 // Find an relaxed input section corresponding to an input section
2248 // in OBJECT with index SHNDX.
2250 const Output_relaxed_input_section*
2251 Output_section::find_relaxed_input_section(const Relobj* object,
2252 unsigned int shndx) const
2254 // Be careful that the map may not be valid due to input section export
2255 // to scripts or a check-point restore.
2256 if (!this->is_relaxed_input_section_map_valid_)
2258 // Rebuild the map as needed.
2259 this->relaxed_input_section_map_.clear();
2260 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2261 p != this->input_sections_.end();
2263 if (p->is_relaxed_input_section())
2265 Input_section_specifier iss(p->relobj(), p->shndx());
2266 this->relaxed_input_section_map_[iss] =
2267 p->relaxed_input_section();
2269 this->is_relaxed_input_section_map_valid_ = true;
2272 Input_section_specifier iss(object, shndx);
2273 Output_relaxed_input_section_by_input_section_map::const_iterator p =
2274 this->relaxed_input_section_map_.find(iss);
2275 if (p != this->relaxed_input_section_map_.end())
2281 // Given an address OFFSET relative to the start of input section
2282 // SHNDX in OBJECT, return whether this address is being included in
2283 // the final link. This should only be called if SHNDX in OBJECT has
2284 // a special mapping.
2287 Output_section::is_input_address_mapped(const Relobj* object,
2291 // Look at the Output_section_data_maps first.
2292 const Output_section_data* posd = this->find_merge_section(object, shndx);
2294 posd = this->find_relaxed_input_section(object, shndx);
2298 section_offset_type output_offset;
2299 bool found = posd->output_offset(object, shndx, offset, &output_offset);
2301 return output_offset != -1;
2304 // Fall back to the slow look-up.
2305 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2306 p != this->input_sections_.end();
2309 section_offset_type output_offset;
2310 if (p->output_offset(object, shndx, offset, &output_offset))
2311 return output_offset != -1;
2314 // By default we assume that the address is mapped. This should
2315 // only be called after we have passed all sections to Layout. At
2316 // that point we should know what we are discarding.
2320 // Given an address OFFSET relative to the start of input section
2321 // SHNDX in object OBJECT, return the output offset relative to the
2322 // start of the input section in the output section. This should only
2323 // be called if SHNDX in OBJECT has a special mapping.
2326 Output_section::output_offset(const Relobj* object, unsigned int shndx,
2327 section_offset_type offset) const
2329 // This can only be called meaningfully when we know the data size
2331 gold_assert(this->is_data_size_valid());
2333 // Look at the Output_section_data_maps first.
2334 const Output_section_data* posd = this->find_merge_section(object, shndx);
2336 posd = this->find_relaxed_input_section(object, shndx);
2339 section_offset_type output_offset;
2340 bool found = posd->output_offset(object, shndx, offset, &output_offset);
2342 return output_offset;
2345 // Fall back to the slow look-up.
2346 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2347 p != this->input_sections_.end();
2350 section_offset_type output_offset;
2351 if (p->output_offset(object, shndx, offset, &output_offset))
2352 return output_offset;
2357 // Return the output virtual address of OFFSET relative to the start
2358 // of input section SHNDX in object OBJECT.
2361 Output_section::output_address(const Relobj* object, unsigned int shndx,
2364 uint64_t addr = this->address() + this->first_input_offset_;
2366 // Look at the Output_section_data_maps first.
2367 const Output_section_data* posd = this->find_merge_section(object, shndx);
2369 posd = this->find_relaxed_input_section(object, shndx);
2370 if (posd != NULL && posd->is_address_valid())
2372 section_offset_type output_offset;
2373 bool found = posd->output_offset(object, shndx, offset, &output_offset);
2375 return posd->address() + output_offset;
2378 // Fall back to the slow look-up.
2379 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2380 p != this->input_sections_.end();
2383 addr = align_address(addr, p->addralign());
2384 section_offset_type output_offset;
2385 if (p->output_offset(object, shndx, offset, &output_offset))
2387 if (output_offset == -1)
2389 return addr + output_offset;
2391 addr += p->data_size();
2394 // If we get here, it means that we don't know the mapping for this
2395 // input section. This might happen in principle if
2396 // add_input_section were called before add_output_section_data.
2397 // But it should never actually happen.
2402 // Find the output address of the start of the merged section for
2403 // input section SHNDX in object OBJECT.
2406 Output_section::find_starting_output_address(const Relobj* object,
2408 uint64_t* paddr) const
2410 // FIXME: This becomes a bottle-neck if we have many relaxed sections.
2411 // Looking up the merge section map does not always work as we sometimes
2412 // find a merge section without its address set.
2413 uint64_t addr = this->address() + this->first_input_offset_;
2414 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2415 p != this->input_sections_.end();
2418 addr = align_address(addr, p->addralign());
2420 // It would be nice if we could use the existing output_offset
2421 // method to get the output offset of input offset 0.
2422 // Unfortunately we don't know for sure that input offset 0 is
2424 if (p->is_merge_section_for(object, shndx))
2430 addr += p->data_size();
2433 // We couldn't find a merge output section for this input section.
2437 // Set the data size of an Output_section. This is where we handle
2438 // setting the addresses of any Output_section_data objects.
2441 Output_section::set_final_data_size()
2443 if (this->input_sections_.empty())
2445 this->set_data_size(this->current_data_size_for_child());
2449 if (this->must_sort_attached_input_sections())
2450 this->sort_attached_input_sections();
2452 uint64_t address = this->address();
2453 off_t startoff = this->offset();
2454 off_t off = startoff + this->first_input_offset_;
2455 for (Input_section_list::iterator p = this->input_sections_.begin();
2456 p != this->input_sections_.end();
2459 off = align_address(off, p->addralign());
2460 p->set_address_and_file_offset(address + (off - startoff), off,
2462 off += p->data_size();
2465 this->set_data_size(off - startoff);
2468 // Reset the address and file offset.
2471 Output_section::do_reset_address_and_file_offset()
2473 // An unallocated section has no address. Forcing this means that
2474 // we don't need special treatment for symbols defined in debug
2475 // sections. We do the same in the constructor.
2476 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0)
2477 this->set_address(0);
2479 for (Input_section_list::iterator p = this->input_sections_.begin();
2480 p != this->input_sections_.end();
2482 p->reset_address_and_file_offset();
2485 // Return true if address and file offset have the values after reset.
2488 Output_section::do_address_and_file_offset_have_reset_values() const
2490 if (this->is_offset_valid())
2493 // An unallocated section has address 0 after its construction or a reset.
2494 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0)
2495 return this->is_address_valid() && this->address() == 0;
2497 return !this->is_address_valid();
2500 // Set the TLS offset. Called only for SHT_TLS sections.
2503 Output_section::do_set_tls_offset(uint64_t tls_base)
2505 this->tls_offset_ = this->address() - tls_base;
2508 // In a few cases we need to sort the input sections attached to an
2509 // output section. This is used to implement the type of constructor
2510 // priority ordering implemented by the GNU linker, in which the
2511 // priority becomes part of the section name and the sections are
2512 // sorted by name. We only do this for an output section if we see an
2513 // attached input section matching ".ctor.*", ".dtor.*",
2514 // ".init_array.*" or ".fini_array.*".
2516 class Output_section::Input_section_sort_entry
2519 Input_section_sort_entry()
2520 : input_section_(), index_(-1U), section_has_name_(false),
2524 Input_section_sort_entry(const Input_section& input_section,
2526 : input_section_(input_section), index_(index),
2527 section_has_name_(input_section.is_input_section()
2528 || input_section.is_relaxed_input_section())
2530 if (this->section_has_name_)
2532 // This is only called single-threaded from Layout::finalize,
2533 // so it is OK to lock. Unfortunately we have no way to pass
2535 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
2536 Object* obj = (input_section.is_input_section()
2537 ? input_section.relobj()
2538 : input_section.relaxed_input_section()->relobj());
2539 Task_lock_obj<Object> tl(dummy_task, obj);
2541 // This is a slow operation, which should be cached in
2542 // Layout::layout if this becomes a speed problem.
2543 this->section_name_ = obj->section_name(input_section.shndx());
2547 // Return the Input_section.
2548 const Input_section&
2549 input_section() const
2551 gold_assert(this->index_ != -1U);
2552 return this->input_section_;
2555 // The index of this entry in the original list. This is used to
2556 // make the sort stable.
2560 gold_assert(this->index_ != -1U);
2561 return this->index_;
2564 // Whether there is a section name.
2566 section_has_name() const
2567 { return this->section_has_name_; }
2569 // The section name.
2571 section_name() const
2573 gold_assert(this->section_has_name_);
2574 return this->section_name_;
2577 // Return true if the section name has a priority. This is assumed
2578 // to be true if it has a dot after the initial dot.
2580 has_priority() const
2582 gold_assert(this->section_has_name_);
2583 return this->section_name_.find('.', 1);
2586 // Return true if this an input file whose base name matches
2587 // FILE_NAME. The base name must have an extension of ".o", and
2588 // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
2589 // This is to match crtbegin.o as well as crtbeginS.o without
2590 // getting confused by other possibilities. Overall matching the
2591 // file name this way is a dreadful hack, but the GNU linker does it
2592 // in order to better support gcc, and we need to be compatible.
2594 match_file_name(const char* match_file_name) const
2596 const std::string& file_name(this->input_section_.relobj()->name());
2597 const char* base_name = lbasename(file_name.c_str());
2598 size_t match_len = strlen(match_file_name);
2599 if (strncmp(base_name, match_file_name, match_len) != 0)
2601 size_t base_len = strlen(base_name);
2602 if (base_len != match_len + 2 && base_len != match_len + 3)
2604 return memcmp(base_name + base_len - 2, ".o", 2) == 0;
2608 // The Input_section we are sorting.
2609 Input_section input_section_;
2610 // The index of this Input_section in the original list.
2611 unsigned int index_;
2612 // Whether this Input_section has a section name--it won't if this
2613 // is some random Output_section_data.
2614 bool section_has_name_;
2615 // The section name if there is one.
2616 std::string section_name_;
2619 // Return true if S1 should come before S2 in the output section.
2622 Output_section::Input_section_sort_compare::operator()(
2623 const Output_section::Input_section_sort_entry& s1,
2624 const Output_section::Input_section_sort_entry& s2) const
2626 // crtbegin.o must come first.
2627 bool s1_begin = s1.match_file_name("crtbegin");
2628 bool s2_begin = s2.match_file_name("crtbegin");
2629 if (s1_begin || s2_begin)
2635 return s1.index() < s2.index();
2638 // crtend.o must come last.
2639 bool s1_end = s1.match_file_name("crtend");
2640 bool s2_end = s2.match_file_name("crtend");
2641 if (s1_end || s2_end)
2647 return s1.index() < s2.index();
2650 // We sort all the sections with no names to the end.
2651 if (!s1.section_has_name() || !s2.section_has_name())
2653 if (s1.section_has_name())
2655 if (s2.section_has_name())
2657 return s1.index() < s2.index();
2660 // A section with a priority follows a section without a priority.
2661 // The GNU linker does this for all but .init_array sections; until
2662 // further notice we'll assume that that is an mistake.
2663 bool s1_has_priority = s1.has_priority();
2664 bool s2_has_priority = s2.has_priority();
2665 if (s1_has_priority && !s2_has_priority)
2667 if (!s1_has_priority && s2_has_priority)
2670 // Otherwise we sort by name.
2671 int compare = s1.section_name().compare(s2.section_name());
2675 // Otherwise we keep the input order.
2676 return s1.index() < s2.index();
2679 // Sort the input sections attached to an output section.
2682 Output_section::sort_attached_input_sections()
2684 if (this->attached_input_sections_are_sorted_)
2687 if (this->checkpoint_ != NULL
2688 && !this->checkpoint_->input_sections_saved())
2689 this->checkpoint_->save_input_sections();
2691 // The only thing we know about an input section is the object and
2692 // the section index. We need the section name. Recomputing this
2693 // is slow but this is an unusual case. If this becomes a speed
2694 // problem we can cache the names as required in Layout::layout.
2696 // We start by building a larger vector holding a copy of each
2697 // Input_section, plus its current index in the list and its name.
2698 std::vector<Input_section_sort_entry> sort_list;
2701 for (Input_section_list::iterator p = this->input_sections_.begin();
2702 p != this->input_sections_.end();
2704 sort_list.push_back(Input_section_sort_entry(*p, i));
2706 // Sort the input sections.
2707 std::sort(sort_list.begin(), sort_list.end(), Input_section_sort_compare());
2709 // Copy the sorted input sections back to our list.
2710 this->input_sections_.clear();
2711 for (std::vector<Input_section_sort_entry>::iterator p = sort_list.begin();
2712 p != sort_list.end();
2714 this->input_sections_.push_back(p->input_section());
2716 // Remember that we sorted the input sections, since we might get
2718 this->attached_input_sections_are_sorted_ = true;
2721 // Write the section header to *OSHDR.
2723 template<int size, bool big_endian>
2725 Output_section::write_header(const Layout* layout,
2726 const Stringpool* secnamepool,
2727 elfcpp::Shdr_write<size, big_endian>* oshdr) const
2729 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
2730 oshdr->put_sh_type(this->type_);
2732 elfcpp::Elf_Xword flags = this->flags_;
2733 if (this->info_section_ != NULL && this->info_uses_section_index_)
2734 flags |= elfcpp::SHF_INFO_LINK;
2735 oshdr->put_sh_flags(flags);
2737 oshdr->put_sh_addr(this->address());
2738 oshdr->put_sh_offset(this->offset());
2739 oshdr->put_sh_size(this->data_size());
2740 if (this->link_section_ != NULL)
2741 oshdr->put_sh_link(this->link_section_->out_shndx());
2742 else if (this->should_link_to_symtab_)
2743 oshdr->put_sh_link(layout->symtab_section()->out_shndx());
2744 else if (this->should_link_to_dynsym_)
2745 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
2747 oshdr->put_sh_link(this->link_);
2749 elfcpp::Elf_Word info;
2750 if (this->info_section_ != NULL)
2752 if (this->info_uses_section_index_)
2753 info = this->info_section_->out_shndx();
2755 info = this->info_section_->symtab_index();
2757 else if (this->info_symndx_ != NULL)
2758 info = this->info_symndx_->symtab_index();
2761 oshdr->put_sh_info(info);
2763 oshdr->put_sh_addralign(this->addralign_);
2764 oshdr->put_sh_entsize(this->entsize_);
2767 // Write out the data. For input sections the data is written out by
2768 // Object::relocate, but we have to handle Output_section_data objects
2772 Output_section::do_write(Output_file* of)
2774 gold_assert(!this->requires_postprocessing());
2776 // If the target performs relaxation, we delay filler generation until now.
2777 gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
2779 off_t output_section_file_offset = this->offset();
2780 for (Fill_list::iterator p = this->fills_.begin();
2781 p != this->fills_.end();
2784 std::string fill_data(parameters->target().code_fill(p->length()));
2785 of->write(output_section_file_offset + p->section_offset(),
2786 fill_data.data(), fill_data.size());
2789 off_t off = this->offset() + this->first_input_offset_;
2790 for (Input_section_list::iterator p = this->input_sections_.begin();
2791 p != this->input_sections_.end();
2794 off_t aligned_off = align_address(off, p->addralign());
2795 if (this->generate_code_fills_at_write_ && (off != aligned_off))
2797 size_t fill_len = aligned_off - off;
2798 std::string fill_data(parameters->target().code_fill(fill_len));
2799 of->write(off, fill_data.data(), fill_data.size());
2803 off = aligned_off + p->data_size();
2807 // If a section requires postprocessing, create the buffer to use.
2810 Output_section::create_postprocessing_buffer()
2812 gold_assert(this->requires_postprocessing());
2814 if (this->postprocessing_buffer_ != NULL)
2817 if (!this->input_sections_.empty())
2819 off_t off = this->first_input_offset_;
2820 for (Input_section_list::iterator p = this->input_sections_.begin();
2821 p != this->input_sections_.end();
2824 off = align_address(off, p->addralign());
2825 p->finalize_data_size();
2826 off += p->data_size();
2828 this->set_current_data_size_for_child(off);
2831 off_t buffer_size = this->current_data_size_for_child();
2832 this->postprocessing_buffer_ = new unsigned char[buffer_size];
2835 // Write all the data of an Output_section into the postprocessing
2836 // buffer. This is used for sections which require postprocessing,
2837 // such as compression. Input sections are handled by
2838 // Object::Relocate.
2841 Output_section::write_to_postprocessing_buffer()
2843 gold_assert(this->requires_postprocessing());
2845 // If the target performs relaxation, we delay filler generation until now.
2846 gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
2848 unsigned char* buffer = this->postprocessing_buffer();
2849 for (Fill_list::iterator p = this->fills_.begin();
2850 p != this->fills_.end();
2853 std::string fill_data(parameters->target().code_fill(p->length()));
2854 memcpy(buffer + p->section_offset(), fill_data.data(),
2858 off_t off = this->first_input_offset_;
2859 for (Input_section_list::iterator p = this->input_sections_.begin();
2860 p != this->input_sections_.end();
2863 off_t aligned_off = align_address(off, p->addralign());
2864 if (this->generate_code_fills_at_write_ && (off != aligned_off))
2866 size_t fill_len = aligned_off - off;
2867 std::string fill_data(parameters->target().code_fill(fill_len));
2868 memcpy(buffer + off, fill_data.data(), fill_data.size());
2871 p->write_to_buffer(buffer + aligned_off);
2872 off = aligned_off + p->data_size();
2876 // Get the input sections for linker script processing. We leave
2877 // behind the Output_section_data entries. Note that this may be
2878 // slightly incorrect for merge sections. We will leave them behind,
2879 // but it is possible that the script says that they should follow
2880 // some other input sections, as in:
2881 // .rodata { *(.rodata) *(.rodata.cst*) }
2882 // For that matter, we don't handle this correctly:
2883 // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
2884 // With luck this will never matter.
2887 Output_section::get_input_sections(
2889 const std::string& fill,
2890 std::list<Simple_input_section>* input_sections)
2892 if (this->checkpoint_ != NULL
2893 && !this->checkpoint_->input_sections_saved())
2894 this->checkpoint_->save_input_sections();
2896 // Invalidate the relaxed input section map.
2897 this->is_relaxed_input_section_map_valid_ = false;
2899 uint64_t orig_address = address;
2901 address = align_address(address, this->addralign());
2903 Input_section_list remaining;
2904 for (Input_section_list::iterator p = this->input_sections_.begin();
2905 p != this->input_sections_.end();
2908 if (p->is_input_section())
2909 input_sections->push_back(Simple_input_section(p->relobj(),
2911 else if (p->is_relaxed_input_section())
2912 input_sections->push_back(
2913 Simple_input_section(p->relaxed_input_section()));
2916 uint64_t aligned_address = align_address(address, p->addralign());
2917 if (aligned_address != address && !fill.empty())
2919 section_size_type length =
2920 convert_to_section_size_type(aligned_address - address);
2921 std::string this_fill;
2922 this_fill.reserve(length);
2923 while (this_fill.length() + fill.length() <= length)
2925 if (this_fill.length() < length)
2926 this_fill.append(fill, 0, length - this_fill.length());
2928 Output_section_data* posd = new Output_data_const(this_fill, 0);
2929 remaining.push_back(Input_section(posd));
2931 address = aligned_address;
2933 remaining.push_back(*p);
2935 p->finalize_data_size();
2936 address += p->data_size();
2940 this->input_sections_.swap(remaining);
2941 this->first_input_offset_ = 0;
2943 uint64_t data_size = address - orig_address;
2944 this->set_current_data_size_for_child(data_size);
2948 // Add an input section from a script.
2951 Output_section::add_input_section_for_script(const Simple_input_section& sis,
2955 if (addralign > this->addralign_)
2956 this->addralign_ = addralign;
2958 off_t offset_in_section = this->current_data_size_for_child();
2959 off_t aligned_offset_in_section = align_address(offset_in_section,
2962 this->set_current_data_size_for_child(aligned_offset_in_section
2966 (sis.is_relaxed_input_section()
2967 ? Input_section(sis.relaxed_input_section())
2968 : Input_section(sis.relobj(), sis.shndx(), data_size, addralign));
2969 this->input_sections_.push_back(is);
2975 Output_section::save_states()
2977 gold_assert(this->checkpoint_ == NULL);
2978 Checkpoint_output_section* checkpoint =
2979 new Checkpoint_output_section(this->addralign_, this->flags_,
2980 this->input_sections_,
2981 this->first_input_offset_,
2982 this->attached_input_sections_are_sorted_);
2983 this->checkpoint_ = checkpoint;
2984 gold_assert(this->fills_.empty());
2988 Output_section::restore_states()
2990 gold_assert(this->checkpoint_ != NULL);
2991 Checkpoint_output_section* checkpoint = this->checkpoint_;
2993 this->addralign_ = checkpoint->addralign();
2994 this->flags_ = checkpoint->flags();
2995 this->first_input_offset_ = checkpoint->first_input_offset();
2997 if (!checkpoint->input_sections_saved())
2999 // If we have not copied the input sections, just resize it.
3000 size_t old_size = checkpoint->input_sections_size();
3001 gold_assert(this->input_sections_.size() >= old_size);
3002 this->input_sections_.resize(old_size);
3006 // We need to copy the whole list. This is not efficient for
3007 // extremely large output with hundreads of thousands of input
3008 // objects. We may need to re-think how we should pass sections
3010 this->input_sections_ = *checkpoint->input_sections();
3013 this->attached_input_sections_are_sorted_ =
3014 checkpoint->attached_input_sections_are_sorted();
3016 // Simply invalidate the relaxed input section map since we do not keep
3018 this->is_relaxed_input_section_map_valid_ = false;
3021 // Print to the map file.
3024 Output_section::do_print_to_mapfile(Mapfile* mapfile) const
3026 mapfile->print_output_section(this);
3028 for (Input_section_list::const_iterator p = this->input_sections_.begin();
3029 p != this->input_sections_.end();
3031 p->print_to_mapfile(mapfile);
3034 // Print stats for merge sections to stderr.
3037 Output_section::print_merge_stats()
3039 Input_section_list::iterator p;
3040 for (p = this->input_sections_.begin();
3041 p != this->input_sections_.end();
3043 p->print_merge_stats(this->name_);
3046 // Output segment methods.
3048 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
3060 is_max_align_known_(false),
3061 are_addresses_set_(false),
3062 is_large_data_segment_(false)
3066 // Add an Output_section to an Output_segment.
3069 Output_segment::add_output_section(Output_section* os,
3070 elfcpp::Elf_Word seg_flags,
3073 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
3074 gold_assert(!this->is_max_align_known_);
3075 gold_assert(os->is_large_data_section() == this->is_large_data_segment());
3076 gold_assert(this->type() == elfcpp::PT_LOAD || !do_sort);
3078 // Update the segment flags.
3079 this->flags_ |= seg_flags;
3081 Output_segment::Output_data_list* pdl;
3082 if (os->type() == elfcpp::SHT_NOBITS)
3083 pdl = &this->output_bss_;
3085 pdl = &this->output_data_;
3087 // Note that while there may be many input sections in an output
3088 // section, there are normally only a few output sections in an
3089 // output segment. The loops below are expected to be fast.
3091 // So that PT_NOTE segments will work correctly, we need to ensure
3092 // that all SHT_NOTE sections are adjacent.
3093 if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
3095 Output_segment::Output_data_list::iterator p = pdl->end();
3099 if ((*p)->is_section_type(elfcpp::SHT_NOTE))
3106 while (p != pdl->begin());
3109 // Similarly, so that PT_TLS segments will work, we need to group
3110 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
3111 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
3112 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
3113 // correctly. SHF_TLS sections get added to both a PT_LOAD segment
3114 // and the PT_TLS segment; we do this grouping only for the PT_LOAD
3116 if (this->type_ != elfcpp::PT_TLS
3117 && (os->flags() & elfcpp::SHF_TLS) != 0)
3119 pdl = &this->output_data_;
3122 bool nobits = os->type() == elfcpp::SHT_NOBITS;
3123 bool sawtls = false;
3124 Output_segment::Output_data_list::iterator p = pdl->end();
3125 gold_assert(p != pdl->begin());
3130 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
3133 // Put a NOBITS section after the first TLS section.
3134 // Put a PROGBITS section after the first
3135 // TLS/PROGBITS section.
3136 insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS);
3140 // If we've gone past the TLS sections, but we've
3141 // seen a TLS section, then we need to insert this
3153 while (p != pdl->begin());
3156 // There are no TLS sections yet; put this one at the requested
3157 // location in the section list.
3160 // For the PT_GNU_RELRO segment, we need to group relro sections,
3161 // and we need to put them before any non-relro sections. Also,
3162 // relro local sections go before relro non-local sections.
3163 if (parameters->options().relro() && os->is_relro())
3165 gold_assert(pdl == &this->output_data_);
3166 Output_segment::Output_data_list::iterator p;
3167 for (p = pdl->begin(); p != pdl->end(); ++p)
3169 if (!(*p)->is_section())
3172 Output_section* pos = (*p)->output_section();
3173 if (!pos->is_relro()
3174 || (os->is_relro_local() && !pos->is_relro_local()))
3182 // Small data sections go at the end of the list of data sections.
3183 // If OS is not small, and there are small sections, we have to
3184 // insert it before the first small section.
3185 if (os->type() != elfcpp::SHT_NOBITS
3186 && !os->is_small_section()
3188 && pdl->back()->is_section()
3189 && pdl->back()->output_section()->is_small_section())
3191 for (Output_segment::Output_data_list::iterator p = pdl->begin();
3195 if ((*p)->is_section()
3196 && (*p)->output_section()->is_small_section())
3205 // A small BSS section goes at the start of the BSS sections, after
3206 // other small BSS sections.
3207 if (os->type() == elfcpp::SHT_NOBITS && os->is_small_section())
3209 for (Output_segment::Output_data_list::iterator p = pdl->begin();
3213 if (!(*p)->is_section()
3214 || !(*p)->output_section()->is_small_section())
3222 // A large BSS section goes at the end of the BSS sections, which
3223 // means that one that is not large must come before the first large
3225 if (os->type() == elfcpp::SHT_NOBITS
3226 && !os->is_large_section()
3228 && pdl->back()->is_section()
3229 && pdl->back()->output_section()->is_large_section())
3231 for (Output_segment::Output_data_list::iterator p = pdl->begin();
3235 if ((*p)->is_section()
3236 && (*p)->output_section()->is_large_section())
3245 // We do some further output section sorting in order to make the
3246 // generated program run more efficiently. We should only do this
3247 // when not using a linker script, so it is controled by the DO_SORT
3251 // FreeBSD requires the .interp section to be in the first page
3252 // of the executable. That is a more efficient location anyhow
3253 // for any OS, since it means that the kernel will have the data
3254 // handy after it reads the program headers.
3255 if (os->is_interp() && !pdl->empty())
3257 pdl->insert(pdl->begin(), os);
3261 // Put loadable non-writable notes immediately after the .interp
3262 // sections, so that the PT_NOTE segment is on the first page of
3264 if (os->type() == elfcpp::SHT_NOTE
3265 && (os->flags() & elfcpp::SHF_WRITE) == 0
3268 Output_segment::Output_data_list::iterator p = pdl->begin();
3269 if ((*p)->is_section() && (*p)->output_section()->is_interp())
3275 // If this section is used by the dynamic linker, and it is not
3276 // writable, then put it first, after the .interp section and
3277 // any loadable notes. This makes it more likely that the
3278 // dynamic linker will have to read less data from the disk.
3279 if (os->is_dynamic_linker_section()
3281 && (os->flags() & elfcpp::SHF_WRITE) == 0)
3283 bool is_reloc = (os->type() == elfcpp::SHT_REL
3284 || os->type() == elfcpp::SHT_RELA);
3285 Output_segment::Output_data_list::iterator p = pdl->begin();
3286 while (p != pdl->end()
3287 && (*p)->is_section()
3288 && ((*p)->output_section()->is_dynamic_linker_section()
3289 || (*p)->output_section()->type() == elfcpp::SHT_NOTE))
3291 // Put reloc sections after the other ones. Putting the
3292 // dynamic reloc sections first confuses BFD, notably
3293 // objcopy and strip.
3295 && ((*p)->output_section()->type() == elfcpp::SHT_REL
3296 || (*p)->output_section()->type() == elfcpp::SHT_RELA))
3305 // If there were no constraints on the output section, just add it
3306 // to the end of the list.
3310 // Remove an Output_section from this segment. It is an error if it
3314 Output_segment::remove_output_section(Output_section* os)
3316 // We only need this for SHT_PROGBITS.
3317 gold_assert(os->type() == elfcpp::SHT_PROGBITS);
3318 for (Output_data_list::iterator p = this->output_data_.begin();
3319 p != this->output_data_.end();
3324 this->output_data_.erase(p);
3331 // Add an Output_data (which is not an Output_section) to the start of
3335 Output_segment::add_initial_output_data(Output_data* od)
3337 gold_assert(!this->is_max_align_known_);
3338 this->output_data_.push_front(od);
3341 // Return whether the first data section is a relro section.
3344 Output_segment::is_first_section_relro() const
3346 return (!this->output_data_.empty()
3347 && this->output_data_.front()->is_section()
3348 && this->output_data_.front()->output_section()->is_relro());
3351 // Return the maximum alignment of the Output_data in Output_segment.
3354 Output_segment::maximum_alignment()
3356 if (!this->is_max_align_known_)
3360 addralign = Output_segment::maximum_alignment_list(&this->output_data_);
3361 if (addralign > this->max_align_)
3362 this->max_align_ = addralign;
3364 addralign = Output_segment::maximum_alignment_list(&this->output_bss_);
3365 if (addralign > this->max_align_)
3366 this->max_align_ = addralign;
3368 // If -z relro is in effect, and the first section in this
3369 // segment is a relro section, then the segment must be aligned
3370 // to at least the common page size. This ensures that the
3371 // PT_GNU_RELRO segment will start at a page boundary.
3372 if (this->type_ == elfcpp::PT_LOAD
3373 && parameters->options().relro()
3374 && this->is_first_section_relro())
3376 addralign = parameters->target().common_pagesize();
3377 if (addralign > this->max_align_)
3378 this->max_align_ = addralign;
3381 this->is_max_align_known_ = true;
3384 return this->max_align_;
3387 // Return the maximum alignment of a list of Output_data.
3390 Output_segment::maximum_alignment_list(const Output_data_list* pdl)
3393 for (Output_data_list::const_iterator p = pdl->begin();
3397 uint64_t addralign = (*p)->addralign();
3398 if (addralign > ret)
3404 // Return the number of dynamic relocs applied to this segment.
3407 Output_segment::dynamic_reloc_count() const
3409 return (this->dynamic_reloc_count_list(&this->output_data_)
3410 + this->dynamic_reloc_count_list(&this->output_bss_));
3413 // Return the number of dynamic relocs applied to an Output_data_list.
3416 Output_segment::dynamic_reloc_count_list(const Output_data_list* pdl) const
3418 unsigned int count = 0;
3419 for (Output_data_list::const_iterator p = pdl->begin();
3422 count += (*p)->dynamic_reloc_count();
3426 // Set the section addresses for an Output_segment. If RESET is true,
3427 // reset the addresses first. ADDR is the address and *POFF is the
3428 // file offset. Set the section indexes starting with *PSHNDX.
3429 // Return the address of the immediately following segment. Update
3430 // *POFF and *PSHNDX.
3433 Output_segment::set_section_addresses(const Layout* layout, bool reset,
3434 uint64_t addr, off_t* poff,
3435 unsigned int* pshndx)
3437 gold_assert(this->type_ == elfcpp::PT_LOAD);
3439 if (!reset && this->are_addresses_set_)
3441 gold_assert(this->paddr_ == addr);
3442 addr = this->vaddr_;
3446 this->vaddr_ = addr;
3447 this->paddr_ = addr;
3448 this->are_addresses_set_ = true;
3451 bool in_tls = false;
3453 bool in_relro = (parameters->options().relro()
3454 && this->is_first_section_relro());
3456 off_t orig_off = *poff;
3457 this->offset_ = orig_off;
3459 addr = this->set_section_list_addresses(layout, reset, &this->output_data_,
3460 addr, poff, pshndx, &in_tls,
3462 this->filesz_ = *poff - orig_off;
3466 uint64_t ret = this->set_section_list_addresses(layout, reset,
3469 &in_tls, &in_relro);
3471 // If the last section was a TLS section, align upward to the
3472 // alignment of the TLS segment, so that the overall size of the TLS
3473 // segment is aligned.
3476 uint64_t segment_align = layout->tls_segment()->maximum_alignment();
3477 *poff = align_address(*poff, segment_align);
3480 // If all the sections were relro sections, align upward to the
3481 // common page size.
3484 uint64_t page_align = parameters->target().common_pagesize();
3485 *poff = align_address(*poff, page_align);
3488 this->memsz_ = *poff - orig_off;
3490 // Ignore the file offset adjustments made by the BSS Output_data
3497 // Set the addresses and file offsets in a list of Output_data
3501 Output_segment::set_section_list_addresses(const Layout* layout, bool reset,
3502 Output_data_list* pdl,
3503 uint64_t addr, off_t* poff,
3504 unsigned int* pshndx,
3505 bool* in_tls, bool* in_relro)
3507 off_t startoff = *poff;
3509 off_t off = startoff;
3510 for (Output_data_list::iterator p = pdl->begin();
3515 (*p)->reset_address_and_file_offset();
3517 // When using a linker script the section will most likely
3518 // already have an address.
3519 if (!(*p)->is_address_valid())
3521 uint64_t align = (*p)->addralign();
3523 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
3525 // Give the first TLS section the alignment of the
3526 // entire TLS segment. Otherwise the TLS segment as a
3527 // whole may be misaligned.
3530 Output_segment* tls_segment = layout->tls_segment();
3531 gold_assert(tls_segment != NULL);
3532 uint64_t segment_align = tls_segment->maximum_alignment();
3533 gold_assert(segment_align >= align);
3534 align = segment_align;
3541 // If this is the first section after the TLS segment,
3542 // align it to at least the alignment of the TLS
3543 // segment, so that the size of the overall TLS segment
3547 uint64_t segment_align =
3548 layout->tls_segment()->maximum_alignment();
3549 if (segment_align > align)
3550 align = segment_align;
3556 // If this is a non-relro section after a relro section,
3557 // align it to a common page boundary so that the dynamic
3558 // linker has a page to mark as read-only.
3560 && (!(*p)->is_section()
3561 || !(*p)->output_section()->is_relro()))
3563 uint64_t page_align = parameters->target().common_pagesize();
3564 if (page_align > align)
3569 off = align_address(off, align);
3570 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
3574 // The script may have inserted a skip forward, but it
3575 // better not have moved backward.
3576 if ((*p)->address() >= addr + (off - startoff))
3577 off += (*p)->address() - (addr + (off - startoff));
3580 if (!layout->script_options()->saw_sections_clause())
3584 Output_section* os = (*p)->output_section();
3586 // Cast to unsigned long long to avoid format warnings.
3587 unsigned long long previous_dot =
3588 static_cast<unsigned long long>(addr + (off - startoff));
3589 unsigned long long dot =
3590 static_cast<unsigned long long>((*p)->address());
3593 gold_error(_("dot moves backward in linker script "
3594 "from 0x%llx to 0x%llx"), previous_dot, dot);
3596 gold_error(_("address of section '%s' moves backward "
3597 "from 0x%llx to 0x%llx"),
3598 os->name(), previous_dot, dot);
3601 (*p)->set_file_offset(off);
3602 (*p)->finalize_data_size();
3605 // We want to ignore the size of a SHF_TLS or SHT_NOBITS
3606 // section. Such a section does not affect the size of a
3608 if (!(*p)->is_section_flag_set(elfcpp::SHF_TLS)
3609 || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
3610 off += (*p)->data_size();
3612 if ((*p)->is_section())
3614 (*p)->set_out_shndx(*pshndx);
3620 return addr + (off - startoff);
3623 // For a non-PT_LOAD segment, set the offset from the sections, if
3627 Output_segment::set_offset()
3629 gold_assert(this->type_ != elfcpp::PT_LOAD);
3631 gold_assert(!this->are_addresses_set_);
3633 if (this->output_data_.empty() && this->output_bss_.empty())
3637 this->are_addresses_set_ = true;
3639 this->min_p_align_ = 0;
3645 const Output_data* first;
3646 if (this->output_data_.empty())
3647 first = this->output_bss_.front();
3649 first = this->output_data_.front();
3650 this->vaddr_ = first->address();
3651 this->paddr_ = (first->has_load_address()
3652 ? first->load_address()
3654 this->are_addresses_set_ = true;
3655 this->offset_ = first->offset();
3657 if (this->output_data_.empty())
3661 const Output_data* last_data = this->output_data_.back();
3662 this->filesz_ = (last_data->address()
3663 + last_data->data_size()
3667 const Output_data* last;
3668 if (this->output_bss_.empty())
3669 last = this->output_data_.back();
3671 last = this->output_bss_.back();
3672 this->memsz_ = (last->address()
3676 // If this is a TLS segment, align the memory size. The code in
3677 // set_section_list ensures that the section after the TLS segment
3678 // is aligned to give us room.
3679 if (this->type_ == elfcpp::PT_TLS)
3681 uint64_t segment_align = this->maximum_alignment();
3682 gold_assert(this->vaddr_ == align_address(this->vaddr_, segment_align));
3683 this->memsz_ = align_address(this->memsz_, segment_align);
3686 // If this is a RELRO segment, align the memory size. The code in
3687 // set_section_list ensures that the section after the RELRO segment
3688 // is aligned to give us room.
3689 if (this->type_ == elfcpp::PT_GNU_RELRO)
3691 uint64_t page_align = parameters->target().common_pagesize();
3692 gold_assert(this->vaddr_ == align_address(this->vaddr_, page_align));
3693 this->memsz_ = align_address(this->memsz_, page_align);
3697 // Set the TLS offsets of the sections in the PT_TLS segment.
3700 Output_segment::set_tls_offsets()
3702 gold_assert(this->type_ == elfcpp::PT_TLS);
3704 for (Output_data_list::iterator p = this->output_data_.begin();
3705 p != this->output_data_.end();
3707 (*p)->set_tls_offset(this->vaddr_);
3709 for (Output_data_list::iterator p = this->output_bss_.begin();
3710 p != this->output_bss_.end();
3712 (*p)->set_tls_offset(this->vaddr_);
3715 // Return the address of the first section.
3718 Output_segment::first_section_load_address() const
3720 for (Output_data_list::const_iterator p = this->output_data_.begin();
3721 p != this->output_data_.end();
3723 if ((*p)->is_section())
3724 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
3726 for (Output_data_list::const_iterator p = this->output_bss_.begin();
3727 p != this->output_bss_.end();
3729 if ((*p)->is_section())
3730 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
3735 // Return the number of Output_sections in an Output_segment.
3738 Output_segment::output_section_count() const
3740 return (this->output_section_count_list(&this->output_data_)
3741 + this->output_section_count_list(&this->output_bss_));
3744 // Return the number of Output_sections in an Output_data_list.
3747 Output_segment::output_section_count_list(const Output_data_list* pdl) const
3749 unsigned int count = 0;
3750 for (Output_data_list::const_iterator p = pdl->begin();
3754 if ((*p)->is_section())
3760 // Return the section attached to the list segment with the lowest
3761 // load address. This is used when handling a PHDRS clause in a
3765 Output_segment::section_with_lowest_load_address() const
3767 Output_section* found = NULL;
3768 uint64_t found_lma = 0;
3769 this->lowest_load_address_in_list(&this->output_data_, &found, &found_lma);
3771 Output_section* found_data = found;
3772 this->lowest_load_address_in_list(&this->output_bss_, &found, &found_lma);
3773 if (found != found_data && found_data != NULL)
3775 gold_error(_("nobits section %s may not precede progbits section %s "
3777 found->name(), found_data->name());
3784 // Look through a list for a section with a lower load address.
3787 Output_segment::lowest_load_address_in_list(const Output_data_list* pdl,
3788 Output_section** found,
3789 uint64_t* found_lma) const
3791 for (Output_data_list::const_iterator p = pdl->begin();
3795 if (!(*p)->is_section())
3797 Output_section* os = static_cast<Output_section*>(*p);
3798 uint64_t lma = (os->has_load_address()
3799 ? os->load_address()
3801 if (*found == NULL || lma < *found_lma)
3809 // Write the segment data into *OPHDR.
3811 template<int size, bool big_endian>
3813 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
3815 ophdr->put_p_type(this->type_);
3816 ophdr->put_p_offset(this->offset_);
3817 ophdr->put_p_vaddr(this->vaddr_);
3818 ophdr->put_p_paddr(this->paddr_);
3819 ophdr->put_p_filesz(this->filesz_);
3820 ophdr->put_p_memsz(this->memsz_);
3821 ophdr->put_p_flags(this->flags_);
3822 ophdr->put_p_align(std::max(this->min_p_align_, this->maximum_alignment()));
3825 // Write the section headers into V.
3827 template<int size, bool big_endian>
3829 Output_segment::write_section_headers(const Layout* layout,
3830 const Stringpool* secnamepool,
3832 unsigned int *pshndx) const
3834 // Every section that is attached to a segment must be attached to a
3835 // PT_LOAD segment, so we only write out section headers for PT_LOAD
3837 if (this->type_ != elfcpp::PT_LOAD)
3840 v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
3841 &this->output_data_,
3843 v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
3849 template<int size, bool big_endian>
3851 Output_segment::write_section_headers_list(const Layout* layout,
3852 const Stringpool* secnamepool,
3853 const Output_data_list* pdl,
3855 unsigned int* pshndx) const
3857 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
3858 for (Output_data_list::const_iterator p = pdl->begin();
3862 if ((*p)->is_section())
3864 const Output_section* ps = static_cast<const Output_section*>(*p);
3865 gold_assert(*pshndx == ps->out_shndx());
3866 elfcpp::Shdr_write<size, big_endian> oshdr(v);
3867 ps->write_header(layout, secnamepool, &oshdr);
3875 // Print the output sections to the map file.
3878 Output_segment::print_sections_to_mapfile(Mapfile* mapfile) const
3880 if (this->type() != elfcpp::PT_LOAD)
3882 this->print_section_list_to_mapfile(mapfile, &this->output_data_);
3883 this->print_section_list_to_mapfile(mapfile, &this->output_bss_);
3886 // Print an output section list to the map file.
3889 Output_segment::print_section_list_to_mapfile(Mapfile* mapfile,
3890 const Output_data_list* pdl) const
3892 for (Output_data_list::const_iterator p = pdl->begin();
3895 (*p)->print_to_mapfile(mapfile);
3898 // Output_file methods.
3900 Output_file::Output_file(const char* name)
3905 map_is_anonymous_(false),
3906 is_temporary_(false)
3910 // Try to open an existing file. Returns false if the file doesn't
3911 // exist, has a size of 0 or can't be mmapped.
3914 Output_file::open_for_modification()
3916 // The name "-" means "stdout".
3917 if (strcmp(this->name_, "-") == 0)
3920 // Don't bother opening files with a size of zero.
3922 if (::stat(this->name_, &s) != 0 || s.st_size == 0)
3925 int o = open_descriptor(-1, this->name_, O_RDWR, 0);
3927 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
3929 this->file_size_ = s.st_size;
3931 // If the file can't be mmapped, copying the content to an anonymous
3932 // map will probably negate the performance benefits of incremental
3933 // linking. This could be helped by using views and loading only
3934 // the necessary parts, but this is not supported as of now.
3935 if (!this->map_no_anonymous())
3937 release_descriptor(o, true);
3939 this->file_size_ = 0;
3946 // Open the output file.
3949 Output_file::open(off_t file_size)
3951 this->file_size_ = file_size;
3953 // Unlink the file first; otherwise the open() may fail if the file
3954 // is busy (e.g. it's an executable that's currently being executed).
3956 // However, the linker may be part of a system where a zero-length
3957 // file is created for it to write to, with tight permissions (gcc
3958 // 2.95 did something like this). Unlinking the file would work
3959 // around those permission controls, so we only unlink if the file
3960 // has a non-zero size. We also unlink only regular files to avoid
3961 // trouble with directories/etc.
3963 // If we fail, continue; this command is merely a best-effort attempt
3964 // to improve the odds for open().
3966 // We let the name "-" mean "stdout"
3967 if (!this->is_temporary_)
3969 if (strcmp(this->name_, "-") == 0)
3970 this->o_ = STDOUT_FILENO;
3974 if (::stat(this->name_, &s) == 0
3975 && (S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
3978 ::unlink(this->name_);
3979 else if (!parameters->options().relocatable())
3981 // If we don't unlink the existing file, add execute
3982 // permission where read permissions already exist
3983 // and where the umask permits.
3984 int mask = ::umask(0);
3986 s.st_mode |= (s.st_mode & 0444) >> 2;
3987 ::chmod(this->name_, s.st_mode & ~mask);
3991 int mode = parameters->options().relocatable() ? 0666 : 0777;
3992 int o = open_descriptor(-1, this->name_, O_RDWR | O_CREAT | O_TRUNC,
3995 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
4003 // Resize the output file.
4006 Output_file::resize(off_t file_size)
4008 // If the mmap is mapping an anonymous memory buffer, this is easy:
4009 // just mremap to the new size. If it's mapping to a file, we want
4010 // to unmap to flush to the file, then remap after growing the file.
4011 if (this->map_is_anonymous_)
4013 void* base = ::mremap(this->base_, this->file_size_, file_size,
4015 if (base == MAP_FAILED)
4016 gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
4017 this->base_ = static_cast<unsigned char*>(base);
4018 this->file_size_ = file_size;
4023 this->file_size_ = file_size;
4024 if (!this->map_no_anonymous())
4025 gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
4029 // Map an anonymous block of memory which will later be written to the
4030 // file. Return whether the map succeeded.
4033 Output_file::map_anonymous()
4035 void* base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
4036 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
4037 if (base != MAP_FAILED)
4039 this->map_is_anonymous_ = true;
4040 this->base_ = static_cast<unsigned char*>(base);
4046 // Map the file into memory. Return whether the mapping succeeded.
4049 Output_file::map_no_anonymous()
4051 const int o = this->o_;
4053 // If the output file is not a regular file, don't try to mmap it;
4054 // instead, we'll mmap a block of memory (an anonymous buffer), and
4055 // then later write the buffer to the file.
4057 struct stat statbuf;
4058 if (o == STDOUT_FILENO || o == STDERR_FILENO
4059 || ::fstat(o, &statbuf) != 0
4060 || !S_ISREG(statbuf.st_mode)
4061 || this->is_temporary_)
4064 // Ensure that we have disk space available for the file. If we
4065 // don't do this, it is possible that we will call munmap, close,
4066 // and exit with dirty buffers still in the cache with no assigned
4067 // disk blocks. If the disk is out of space at that point, the
4068 // output file will wind up incomplete, but we will have already
4069 // exited. The alternative to fallocate would be to use fdatasync,
4070 // but that would be a more significant performance hit.
4071 if (::posix_fallocate(o, 0, this->file_size_) < 0)
4072 gold_fatal(_("%s: %s"), this->name_, strerror(errno));
4074 // Map the file into memory.
4075 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
4078 // The mmap call might fail because of file system issues: the file
4079 // system might not support mmap at all, or it might not support
4080 // mmap with PROT_WRITE.
4081 if (base == MAP_FAILED)
4084 this->map_is_anonymous_ = false;
4085 this->base_ = static_cast<unsigned char*>(base);
4089 // Map the file into memory.
4094 if (this->map_no_anonymous())
4097 // The mmap call might fail because of file system issues: the file
4098 // system might not support mmap at all, or it might not support
4099 // mmap with PROT_WRITE. I'm not sure which errno values we will
4100 // see in all cases, so if the mmap fails for any reason and we
4101 // don't care about file contents, try for an anonymous map.
4102 if (this->map_anonymous())
4105 gold_fatal(_("%s: mmap: failed to allocate %lu bytes for output file: %s"),
4106 this->name_, static_cast<unsigned long>(this->file_size_),
4110 // Unmap the file from memory.
4113 Output_file::unmap()
4115 if (::munmap(this->base_, this->file_size_) < 0)
4116 gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
4120 // Close the output file.
4123 Output_file::close()
4125 // If the map isn't file-backed, we need to write it now.
4126 if (this->map_is_anonymous_ && !this->is_temporary_)
4128 size_t bytes_to_write = this->file_size_;
4130 while (bytes_to_write > 0)
4132 ssize_t bytes_written = ::write(this->o_, this->base_ + offset,
4134 if (bytes_written == 0)
4135 gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
4136 else if (bytes_written < 0)
4137 gold_error(_("%s: write: %s"), this->name_, strerror(errno));
4140 bytes_to_write -= bytes_written;
4141 offset += bytes_written;
4147 // We don't close stdout or stderr
4148 if (this->o_ != STDOUT_FILENO
4149 && this->o_ != STDERR_FILENO
4150 && !this->is_temporary_)
4151 if (::close(this->o_) < 0)
4152 gold_error(_("%s: close: %s"), this->name_, strerror(errno));
4156 // Instantiate the templates we need. We could use the configure
4157 // script to restrict this to only the ones for implemented targets.
4159 #ifdef HAVE_TARGET_32_LITTLE
4162 Output_section::add_input_section<32, false>(
4163 Sized_relobj<32, false>* object,
4165 const char* secname,
4166 const elfcpp::Shdr<32, false>& shdr,
4167 unsigned int reloc_shndx,
4168 bool have_sections_script);
4171 #ifdef HAVE_TARGET_32_BIG
4174 Output_section::add_input_section<32, true>(
4175 Sized_relobj<32, true>* object,
4177 const char* secname,
4178 const elfcpp::Shdr<32, true>& shdr,
4179 unsigned int reloc_shndx,
4180 bool have_sections_script);
4183 #ifdef HAVE_TARGET_64_LITTLE
4186 Output_section::add_input_section<64, false>(
4187 Sized_relobj<64, false>* object,
4189 const char* secname,
4190 const elfcpp::Shdr<64, false>& shdr,
4191 unsigned int reloc_shndx,
4192 bool have_sections_script);
4195 #ifdef HAVE_TARGET_64_BIG
4198 Output_section::add_input_section<64, true>(
4199 Sized_relobj<64, true>* object,
4201 const char* secname,
4202 const elfcpp::Shdr<64, true>& shdr,
4203 unsigned int reloc_shndx,
4204 bool have_sections_script);
4207 #ifdef HAVE_TARGET_32_LITTLE
4209 class Output_reloc<elfcpp::SHT_REL, false, 32, false>;
4212 #ifdef HAVE_TARGET_32_BIG
4214 class Output_reloc<elfcpp::SHT_REL, false, 32, true>;
4217 #ifdef HAVE_TARGET_64_LITTLE
4219 class Output_reloc<elfcpp::SHT_REL, false, 64, false>;
4222 #ifdef HAVE_TARGET_64_BIG
4224 class Output_reloc<elfcpp::SHT_REL, false, 64, true>;
4227 #ifdef HAVE_TARGET_32_LITTLE
4229 class Output_reloc<elfcpp::SHT_REL, true, 32, false>;
4232 #ifdef HAVE_TARGET_32_BIG
4234 class Output_reloc<elfcpp::SHT_REL, true, 32, true>;
4237 #ifdef HAVE_TARGET_64_LITTLE
4239 class Output_reloc<elfcpp::SHT_REL, true, 64, false>;
4242 #ifdef HAVE_TARGET_64_BIG
4244 class Output_reloc<elfcpp::SHT_REL, true, 64, true>;
4247 #ifdef HAVE_TARGET_32_LITTLE
4249 class Output_reloc<elfcpp::SHT_RELA, false, 32, false>;
4252 #ifdef HAVE_TARGET_32_BIG
4254 class Output_reloc<elfcpp::SHT_RELA, false, 32, true>;
4257 #ifdef HAVE_TARGET_64_LITTLE
4259 class Output_reloc<elfcpp::SHT_RELA, false, 64, false>;
4262 #ifdef HAVE_TARGET_64_BIG
4264 class Output_reloc<elfcpp::SHT_RELA, false, 64, true>;
4267 #ifdef HAVE_TARGET_32_LITTLE
4269 class Output_reloc<elfcpp::SHT_RELA, true, 32, false>;
4272 #ifdef HAVE_TARGET_32_BIG
4274 class Output_reloc<elfcpp::SHT_RELA, true, 32, true>;
4277 #ifdef HAVE_TARGET_64_LITTLE
4279 class Output_reloc<elfcpp::SHT_RELA, true, 64, false>;
4282 #ifdef HAVE_TARGET_64_BIG
4284 class Output_reloc<elfcpp::SHT_RELA, true, 64, true>;
4287 #ifdef HAVE_TARGET_32_LITTLE
4289 class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
4292 #ifdef HAVE_TARGET_32_BIG
4294 class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
4297 #ifdef HAVE_TARGET_64_LITTLE
4299 class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
4302 #ifdef HAVE_TARGET_64_BIG
4304 class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
4307 #ifdef HAVE_TARGET_32_LITTLE
4309 class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
4312 #ifdef HAVE_TARGET_32_BIG
4314 class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
4317 #ifdef HAVE_TARGET_64_LITTLE
4319 class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
4322 #ifdef HAVE_TARGET_64_BIG
4324 class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
4327 #ifdef HAVE_TARGET_32_LITTLE
4329 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
4332 #ifdef HAVE_TARGET_32_BIG
4334 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
4337 #ifdef HAVE_TARGET_64_LITTLE
4339 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
4342 #ifdef HAVE_TARGET_64_BIG
4344 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
4347 #ifdef HAVE_TARGET_32_LITTLE
4349 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
4352 #ifdef HAVE_TARGET_32_BIG
4354 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
4357 #ifdef HAVE_TARGET_64_LITTLE
4359 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
4362 #ifdef HAVE_TARGET_64_BIG
4364 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
4367 #ifdef HAVE_TARGET_32_LITTLE
4369 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, false>;
4372 #ifdef HAVE_TARGET_32_BIG
4374 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, true>;
4377 #ifdef HAVE_TARGET_64_LITTLE
4379 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, false>;
4382 #ifdef HAVE_TARGET_64_BIG
4384 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, true>;
4387 #ifdef HAVE_TARGET_32_LITTLE
4389 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, false>;
4392 #ifdef HAVE_TARGET_32_BIG
4394 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, true>;
4397 #ifdef HAVE_TARGET_64_LITTLE
4399 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, false>;
4402 #ifdef HAVE_TARGET_64_BIG
4404 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, true>;
4407 #ifdef HAVE_TARGET_32_LITTLE
4409 class Output_data_group<32, false>;
4412 #ifdef HAVE_TARGET_32_BIG
4414 class Output_data_group<32, true>;
4417 #ifdef HAVE_TARGET_64_LITTLE
4419 class Output_data_group<64, false>;
4422 #ifdef HAVE_TARGET_64_BIG
4424 class Output_data_group<64, true>;
4427 #ifdef HAVE_TARGET_32_LITTLE
4429 class Output_data_got<32, false>;
4432 #ifdef HAVE_TARGET_32_BIG
4434 class Output_data_got<32, true>;
4437 #ifdef HAVE_TARGET_64_LITTLE
4439 class Output_data_got<64, false>;
4442 #ifdef HAVE_TARGET_64_BIG
4444 class Output_data_got<64, true>;
4447 } // End namespace gold.