1 // output.cc -- manage the output file for gold
3 // Copyright 2006, 2007 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.
32 #include "libiberty.h" // for unlink_if_ordinary()
34 #include "parameters.h"
41 // Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS
43 # define MAP_ANONYMOUS MAP_ANON
49 // Output_data variables.
51 bool Output_data::allocated_sizes_are_fixed;
53 // Output_data methods.
55 Output_data::~Output_data()
59 // Return the default alignment for the target size.
62 Output_data::default_alignment()
64 return Output_data::default_alignment_for_size(parameters->get_size());
67 // Return the default alignment for a size--32 or 64.
70 Output_data::default_alignment_for_size(int size)
80 // Output_section_header methods. This currently assumes that the
81 // segment and section lists are complete at construction time.
83 Output_section_headers::Output_section_headers(
85 const Layout::Segment_list* segment_list,
86 const Layout::Section_list* unattached_section_list,
87 const Stringpool* secnamepool)
89 segment_list_(segment_list),
90 unattached_section_list_(unattached_section_list),
91 secnamepool_(secnamepool)
93 // Count all the sections. Start with 1 for the null section.
95 for (Layout::Segment_list::const_iterator p = segment_list->begin();
96 p != segment_list->end();
98 if ((*p)->type() == elfcpp::PT_LOAD)
99 count += (*p)->output_section_count();
100 count += unattached_section_list->size();
102 const int size = parameters->get_size();
105 shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
107 shdr_size = elfcpp::Elf_sizes<64>::shdr_size;
111 this->set_data_size(count * shdr_size);
114 // Write out the section headers.
117 Output_section_headers::do_write(Output_file* of)
119 if (parameters->get_size() == 32)
121 if (parameters->is_big_endian())
123 #ifdef HAVE_TARGET_32_BIG
124 this->do_sized_write<32, true>(of);
131 #ifdef HAVE_TARGET_32_LITTLE
132 this->do_sized_write<32, false>(of);
138 else if (parameters->get_size() == 64)
140 if (parameters->is_big_endian())
142 #ifdef HAVE_TARGET_64_BIG
143 this->do_sized_write<64, true>(of);
150 #ifdef HAVE_TARGET_64_LITTLE
151 this->do_sized_write<64, false>(of);
161 template<int size, bool big_endian>
163 Output_section_headers::do_sized_write(Output_file* of)
165 off_t all_shdrs_size = this->data_size();
166 unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size);
168 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
169 unsigned char* v = view;
172 typename elfcpp::Shdr_write<size, big_endian> oshdr(v);
173 oshdr.put_sh_name(0);
174 oshdr.put_sh_type(elfcpp::SHT_NULL);
175 oshdr.put_sh_flags(0);
176 oshdr.put_sh_addr(0);
177 oshdr.put_sh_offset(0);
178 oshdr.put_sh_size(0);
179 oshdr.put_sh_link(0);
180 oshdr.put_sh_info(0);
181 oshdr.put_sh_addralign(0);
182 oshdr.put_sh_entsize(0);
188 for (Layout::Segment_list::const_iterator p = this->segment_list_->begin();
189 p != this->segment_list_->end();
191 v = (*p)->write_section_headers SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
192 this->layout_, this->secnamepool_, v, &shndx
193 SELECT_SIZE_ENDIAN(size, big_endian));
194 for (Layout::Section_list::const_iterator p =
195 this->unattached_section_list_->begin();
196 p != this->unattached_section_list_->end();
199 gold_assert(shndx == (*p)->out_shndx());
200 elfcpp::Shdr_write<size, big_endian> oshdr(v);
201 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
206 of->write_output_view(this->offset(), all_shdrs_size, view);
209 // Output_segment_header methods.
211 Output_segment_headers::Output_segment_headers(
212 const Layout::Segment_list& segment_list)
213 : segment_list_(segment_list)
215 const int size = parameters->get_size();
218 phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
220 phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
224 this->set_data_size(segment_list.size() * phdr_size);
228 Output_segment_headers::do_write(Output_file* of)
230 if (parameters->get_size() == 32)
232 if (parameters->is_big_endian())
234 #ifdef HAVE_TARGET_32_BIG
235 this->do_sized_write<32, true>(of);
242 #ifdef HAVE_TARGET_32_LITTLE
243 this->do_sized_write<32, false>(of);
249 else if (parameters->get_size() == 64)
251 if (parameters->is_big_endian())
253 #ifdef HAVE_TARGET_64_BIG
254 this->do_sized_write<64, true>(of);
261 #ifdef HAVE_TARGET_64_LITTLE
262 this->do_sized_write<64, false>(of);
272 template<int size, bool big_endian>
274 Output_segment_headers::do_sized_write(Output_file* of)
276 const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size;
277 off_t all_phdrs_size = this->segment_list_.size() * phdr_size;
278 unsigned char* view = of->get_output_view(this->offset(),
280 unsigned char* v = view;
281 for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
282 p != this->segment_list_.end();
285 elfcpp::Phdr_write<size, big_endian> ophdr(v);
286 (*p)->write_header(&ophdr);
290 of->write_output_view(this->offset(), all_phdrs_size, view);
293 // Output_file_header methods.
295 Output_file_header::Output_file_header(const Target* target,
296 const Symbol_table* symtab,
297 const Output_segment_headers* osh,
301 segment_header_(osh),
302 section_header_(NULL),
306 const int size = parameters->get_size();
309 ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
311 ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
315 this->set_data_size(ehdr_size);
318 // Set the section table information for a file header.
321 Output_file_header::set_section_info(const Output_section_headers* shdrs,
322 const Output_section* shstrtab)
324 this->section_header_ = shdrs;
325 this->shstrtab_ = shstrtab;
328 // Write out the file header.
331 Output_file_header::do_write(Output_file* of)
333 gold_assert(this->offset() == 0);
335 if (parameters->get_size() == 32)
337 if (parameters->is_big_endian())
339 #ifdef HAVE_TARGET_32_BIG
340 this->do_sized_write<32, true>(of);
347 #ifdef HAVE_TARGET_32_LITTLE
348 this->do_sized_write<32, false>(of);
354 else if (parameters->get_size() == 64)
356 if (parameters->is_big_endian())
358 #ifdef HAVE_TARGET_64_BIG
359 this->do_sized_write<64, true>(of);
366 #ifdef HAVE_TARGET_64_LITTLE
367 this->do_sized_write<64, false>(of);
377 // Write out the file header with appropriate size and endianess.
379 template<int size, bool big_endian>
381 Output_file_header::do_sized_write(Output_file* of)
383 gold_assert(this->offset() == 0);
385 int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
386 unsigned char* view = of->get_output_view(0, ehdr_size);
387 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
389 unsigned char e_ident[elfcpp::EI_NIDENT];
390 memset(e_ident, 0, elfcpp::EI_NIDENT);
391 e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
392 e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
393 e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
394 e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
396 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
398 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
401 e_ident[elfcpp::EI_DATA] = (big_endian
402 ? elfcpp::ELFDATA2MSB
403 : elfcpp::ELFDATA2LSB);
404 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
405 // FIXME: Some targets may need to set EI_OSABI and EI_ABIVERSION.
406 oehdr.put_e_ident(e_ident);
409 if (parameters->output_is_object())
410 e_type = elfcpp::ET_REL;
411 else if (parameters->output_is_shared())
412 e_type = elfcpp::ET_DYN;
414 e_type = elfcpp::ET_EXEC;
415 oehdr.put_e_type(e_type);
417 oehdr.put_e_machine(this->target_->machine_code());
418 oehdr.put_e_version(elfcpp::EV_CURRENT);
420 oehdr.put_e_entry(this->entry<size>());
422 oehdr.put_e_phoff(this->segment_header_->offset());
423 oehdr.put_e_shoff(this->section_header_->offset());
425 // FIXME: The target needs to set the flags.
426 oehdr.put_e_flags(0);
428 oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
429 oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
430 oehdr.put_e_phnum(this->segment_header_->data_size()
431 / elfcpp::Elf_sizes<size>::phdr_size);
432 oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
433 oehdr.put_e_shnum(this->section_header_->data_size()
434 / elfcpp::Elf_sizes<size>::shdr_size);
435 oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
437 of->write_output_view(0, ehdr_size, view);
440 // Return the value to use for the entry address. THIS->ENTRY_ is the
441 // symbol specified on the command line, if any.
444 typename elfcpp::Elf_types<size>::Elf_Addr
445 Output_file_header::entry()
447 const bool should_issue_warning = (this->entry_ != NULL
448 && parameters->output_is_executable());
450 // FIXME: Need to support target specific entry symbol.
451 const char* entry = this->entry_;
455 Symbol* sym = this->symtab_->lookup(entry);
457 typename Sized_symbol<size>::Value_type v;
460 Sized_symbol<size>* ssym;
461 ssym = this->symtab_->get_sized_symbol<size>(sym);
462 if (!ssym->is_defined() && should_issue_warning)
463 gold_warning("entry symbol '%s' exists but is not defined", entry);
468 // We couldn't find the entry symbol. See if we can parse it as
469 // a number. This supports, e.g., -e 0x1000.
471 v = strtoull(entry, &endptr, 0);
474 if (should_issue_warning)
475 gold_warning("cannot find entry symbol '%s'", entry);
483 // Output_data_const methods.
486 Output_data_const::do_write(Output_file* of)
488 of->write(this->offset(), this->data_.data(), this->data_.size());
491 // Output_data_const_buffer methods.
494 Output_data_const_buffer::do_write(Output_file* of)
496 of->write(this->offset(), this->p_, this->data_size());
499 // Output_section_data methods.
501 // Record the output section, and set the entry size and such.
504 Output_section_data::set_output_section(Output_section* os)
506 gold_assert(this->output_section_ == NULL);
507 this->output_section_ = os;
508 this->do_adjust_output_section(os);
511 // Return the section index of the output section.
514 Output_section_data::do_out_shndx() const
516 gold_assert(this->output_section_ != NULL);
517 return this->output_section_->out_shndx();
520 // Output_data_strtab methods.
522 // Set the final data size.
525 Output_data_strtab::set_final_data_size()
527 this->strtab_->set_string_offsets();
528 this->set_data_size(this->strtab_->get_strtab_size());
531 // Write out a string table.
534 Output_data_strtab::do_write(Output_file* of)
536 this->strtab_->write(of, this->offset());
539 // Output_reloc methods.
541 // A reloc against a global symbol.
543 template<bool dynamic, int size, bool big_endian>
544 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
550 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
551 is_relative_(is_relative), shndx_(INVALID_CODE)
553 this->u1_.gsym = gsym;
555 if (dynamic && !is_relative)
556 gsym->set_needs_dynsym_entry();
559 template<bool dynamic, int size, bool big_endian>
560 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
567 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
568 is_relative_(is_relative), shndx_(shndx)
570 gold_assert(shndx != INVALID_CODE);
571 this->u1_.gsym = gsym;
572 this->u2_.relobj = relobj;
573 if (dynamic && !is_relative)
574 gsym->set_needs_dynsym_entry();
577 // A reloc against a local symbol.
579 template<bool dynamic, int size, bool big_endian>
580 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
581 Sized_relobj<size, big_endian>* relobj,
582 unsigned int local_sym_index,
587 : address_(address), local_sym_index_(local_sym_index), type_(type),
588 is_relative_(is_relative), shndx_(INVALID_CODE)
590 gold_assert(local_sym_index != GSYM_CODE
591 && local_sym_index != INVALID_CODE);
592 this->u1_.relobj = relobj;
594 if (dynamic && !is_relative)
595 relobj->set_needs_output_dynsym_entry(local_sym_index);
598 template<bool dynamic, int size, bool big_endian>
599 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
600 Sized_relobj<size, big_endian>* relobj,
601 unsigned int local_sym_index,
606 : address_(address), local_sym_index_(local_sym_index), type_(type),
607 is_relative_(is_relative), shndx_(shndx)
609 gold_assert(local_sym_index != GSYM_CODE
610 && local_sym_index != INVALID_CODE);
611 gold_assert(shndx != INVALID_CODE);
612 this->u1_.relobj = relobj;
613 this->u2_.relobj = relobj;
614 if (dynamic && !is_relative)
615 relobj->set_needs_output_dynsym_entry(local_sym_index);
618 // A reloc against the STT_SECTION symbol of an output section.
620 template<bool dynamic, int size, bool big_endian>
621 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
626 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
627 is_relative_(false), shndx_(INVALID_CODE)
632 os->set_needs_dynsym_index();
635 template<bool dynamic, int size, bool big_endian>
636 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
642 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
643 is_relative_(false), shndx_(shndx)
645 gold_assert(shndx != INVALID_CODE);
647 this->u2_.relobj = relobj;
649 os->set_needs_dynsym_index();
652 // Get the symbol index of a relocation.
654 template<bool dynamic, int size, bool big_endian>
656 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
660 switch (this->local_sym_index_)
666 if (this->u1_.gsym == NULL)
669 index = this->u1_.gsym->dynsym_index();
671 index = this->u1_.gsym->symtab_index();
676 index = this->u1_.os->dynsym_index();
678 index = this->u1_.os->symtab_index();
682 // Relocations without symbols use a symbol index of 0.
688 index = this->u1_.relobj->dynsym_index(this->local_sym_index_);
690 index = this->u1_.relobj->symtab_index(this->local_sym_index_);
693 gold_assert(index != -1U);
697 // Write out the offset and info fields of a Rel or Rela relocation
700 template<bool dynamic, int size, bool big_endian>
701 template<typename Write_rel>
703 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
706 Address address = this->address_;
707 if (this->shndx_ != INVALID_CODE)
709 section_offset_type off;
710 Output_section* os = this->u2_.relobj->output_section(this->shndx_,
712 gold_assert(os != NULL);
714 address += os->address() + off;
717 address = os->output_address(this->u2_.relobj, this->shndx_,
719 gold_assert(address != -1U);
722 else if (this->u2_.od != NULL)
723 address += this->u2_.od->address();
724 wr->put_r_offset(address);
725 unsigned int sym_index = this->is_relative_ ? 0 : this->get_symbol_index();
726 wr->put_r_info(elfcpp::elf_r_info<size>(sym_index, this->type_));
729 // Write out a Rel relocation.
731 template<bool dynamic, int size, bool big_endian>
733 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
734 unsigned char* pov) const
736 elfcpp::Rel_write<size, big_endian> orel(pov);
737 this->write_rel(&orel);
740 // Get the value of the symbol referred to by a Rel relocation.
742 template<bool dynamic, int size, bool big_endian>
743 typename elfcpp::Elf_types<size>::Elf_Addr
744 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value() const
746 if (this->local_sym_index_ == GSYM_CODE)
748 const Sized_symbol<size>* sym;
749 sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym);
752 gold_assert(this->local_sym_index_ != SECTION_CODE
753 && this->local_sym_index_ != INVALID_CODE);
754 const Sized_relobj<size, big_endian>* relobj = this->u1_.relobj;
755 return relobj->local_symbol_value(this->local_sym_index_);
758 // Write out a Rela relocation.
760 template<bool dynamic, int size, bool big_endian>
762 Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
763 unsigned char* pov) const
765 elfcpp::Rela_write<size, big_endian> orel(pov);
766 this->rel_.write_rel(&orel);
767 Addend addend = this->addend_;
768 if (rel_.is_relative())
769 addend += rel_.symbol_value();
770 orel.put_r_addend(addend);
773 // Output_data_reloc_base methods.
775 // Adjust the output section.
777 template<int sh_type, bool dynamic, int size, bool big_endian>
779 Output_data_reloc_base<sh_type, dynamic, size, big_endian>
780 ::do_adjust_output_section(Output_section* os)
782 if (sh_type == elfcpp::SHT_REL)
783 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
784 else if (sh_type == elfcpp::SHT_RELA)
785 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
789 os->set_should_link_to_dynsym();
791 os->set_should_link_to_symtab();
794 // Write out relocation data.
796 template<int sh_type, bool dynamic, int size, bool big_endian>
798 Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
801 const off_t off = this->offset();
802 const off_t oview_size = this->data_size();
803 unsigned char* const oview = of->get_output_view(off, oview_size);
805 unsigned char* pov = oview;
806 for (typename Relocs::const_iterator p = this->relocs_.begin();
807 p != this->relocs_.end();
814 gold_assert(pov - oview == oview_size);
816 of->write_output_view(off, oview_size, oview);
818 // We no longer need the relocation entries.
819 this->relocs_.clear();
822 // Output_data_got::Got_entry methods.
824 // Write out the entry.
826 template<int size, bool big_endian>
828 Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
832 switch (this->local_sym_index_)
836 // If the symbol is resolved locally, we need to write out the
837 // link-time value, which will be relocated dynamically by a
838 // RELATIVE relocation.
839 Symbol* gsym = this->u_.gsym;
840 Sized_symbol<size>* sgsym;
841 // This cast is a bit ugly. We don't want to put a
842 // virtual method in Symbol, because we want Symbol to be
843 // as small as possible.
844 sgsym = static_cast<Sized_symbol<size>*>(gsym);
845 val = sgsym->value();
850 val = this->u_.constant;
854 val = this->u_.object->local_symbol_value(this->local_sym_index_);
858 elfcpp::Swap<size, big_endian>::writeval(pov, val);
861 // Output_data_got methods.
863 // Add an entry for a global symbol to the GOT. This returns true if
864 // this is a new GOT entry, false if the symbol already had a GOT
867 template<int size, bool big_endian>
869 Output_data_got<size, big_endian>::add_global(Symbol* gsym)
871 if (gsym->has_got_offset())
874 this->entries_.push_back(Got_entry(gsym));
875 this->set_got_size();
876 gsym->set_got_offset(this->last_got_offset());
880 // Add an entry for a global symbol to the GOT, and add a dynamic
881 // relocation of type R_TYPE for the GOT entry.
882 template<int size, bool big_endian>
884 Output_data_got<size, big_endian>::add_global_with_rel(
889 if (gsym->has_got_offset())
892 this->entries_.push_back(Got_entry());
893 this->set_got_size();
894 unsigned int got_offset = this->last_got_offset();
895 gsym->set_got_offset(got_offset);
896 rel_dyn->add_global(gsym, r_type, this, got_offset);
899 template<int size, bool big_endian>
901 Output_data_got<size, big_endian>::add_global_with_rela(
906 if (gsym->has_got_offset())
909 this->entries_.push_back(Got_entry());
910 this->set_got_size();
911 unsigned int got_offset = this->last_got_offset();
912 gsym->set_got_offset(got_offset);
913 rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
916 // Add an entry for a local symbol to the GOT. This returns true if
917 // this is a new GOT entry, false if the symbol already has a GOT
920 template<int size, bool big_endian>
922 Output_data_got<size, big_endian>::add_local(
923 Sized_relobj<size, big_endian>* object,
926 if (object->local_has_got_offset(symndx))
929 this->entries_.push_back(Got_entry(object, symndx));
930 this->set_got_size();
931 object->set_local_got_offset(symndx, this->last_got_offset());
935 // Add an entry for a local symbol to the GOT, and add a dynamic
936 // relocation of type R_TYPE for the GOT entry.
937 template<int size, bool big_endian>
939 Output_data_got<size, big_endian>::add_local_with_rel(
940 Sized_relobj<size, big_endian>* object,
945 if (object->local_has_got_offset(symndx))
948 this->entries_.push_back(Got_entry());
949 this->set_got_size();
950 unsigned int got_offset = this->last_got_offset();
951 object->set_local_got_offset(symndx, got_offset);
952 rel_dyn->add_local(object, symndx, r_type, this, got_offset);
955 template<int size, bool big_endian>
957 Output_data_got<size, big_endian>::add_local_with_rela(
958 Sized_relobj<size, big_endian>* object,
963 if (object->local_has_got_offset(symndx))
966 this->entries_.push_back(Got_entry());
967 this->set_got_size();
968 unsigned int got_offset = this->last_got_offset();
969 object->set_local_got_offset(symndx, got_offset);
970 rela_dyn->add_local(object, symndx, r_type, this, got_offset, 0);
973 // Add an entry (or a pair of entries) for a global TLS symbol to the GOT.
974 // In a pair of entries, the first value in the pair will be used for the
975 // module index, and the second value will be used for the dtv-relative
976 // offset. This returns true if this is a new GOT entry, false if the symbol
977 // already has a GOT entry.
979 template<int size, bool big_endian>
981 Output_data_got<size, big_endian>::add_global_tls(Symbol* gsym, bool need_pair)
983 if (gsym->has_tls_got_offset(need_pair))
986 this->entries_.push_back(Got_entry(gsym));
987 gsym->set_tls_got_offset(this->last_got_offset(), need_pair);
989 this->entries_.push_back(Got_entry(gsym));
990 this->set_got_size();
994 // Add an entry for a global TLS symbol to the GOT, and add a dynamic
995 // relocation of type R_TYPE.
996 template<int size, bool big_endian>
998 Output_data_got<size, big_endian>::add_global_tls_with_rel(
1001 unsigned int r_type)
1003 if (gsym->has_tls_got_offset(false))
1006 this->entries_.push_back(Got_entry());
1007 this->set_got_size();
1008 unsigned int got_offset = this->last_got_offset();
1009 gsym->set_tls_got_offset(got_offset, false);
1010 rel_dyn->add_global(gsym, r_type, this, got_offset);
1013 template<int size, bool big_endian>
1015 Output_data_got<size, big_endian>::add_global_tls_with_rela(
1018 unsigned int r_type)
1020 if (gsym->has_tls_got_offset(false))
1023 this->entries_.push_back(Got_entry());
1024 this->set_got_size();
1025 unsigned int got_offset = this->last_got_offset();
1026 gsym->set_tls_got_offset(got_offset, false);
1027 rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
1030 // Add a pair of entries for a global TLS symbol to the GOT, and add
1031 // dynamic relocations of type MOD_R_TYPE and DTV_R_TYPE, respectively.
1032 template<int size, bool big_endian>
1034 Output_data_got<size, big_endian>::add_global_tls_with_rel(
1037 unsigned int mod_r_type,
1038 unsigned int dtv_r_type)
1040 if (gsym->has_tls_got_offset(true))
1043 this->entries_.push_back(Got_entry());
1044 unsigned int got_offset = this->last_got_offset();
1045 gsym->set_tls_got_offset(got_offset, true);
1046 rel_dyn->add_global(gsym, mod_r_type, this, got_offset);
1048 this->entries_.push_back(Got_entry());
1049 this->set_got_size();
1050 got_offset = this->last_got_offset();
1051 rel_dyn->add_global(gsym, dtv_r_type, this, got_offset);
1054 template<int size, bool big_endian>
1056 Output_data_got<size, big_endian>::add_global_tls_with_rela(
1059 unsigned int mod_r_type,
1060 unsigned int dtv_r_type)
1062 if (gsym->has_tls_got_offset(true))
1065 this->entries_.push_back(Got_entry());
1066 unsigned int got_offset = this->last_got_offset();
1067 gsym->set_tls_got_offset(got_offset, true);
1068 rela_dyn->add_global(gsym, mod_r_type, this, got_offset, 0);
1070 this->entries_.push_back(Got_entry());
1071 this->set_got_size();
1072 got_offset = this->last_got_offset();
1073 rela_dyn->add_global(gsym, dtv_r_type, this, got_offset, 0);
1076 // Add an entry (or a pair of entries) for a local TLS symbol to the GOT.
1077 // In a pair of entries, the first value in the pair will be used for the
1078 // module index, and the second value will be used for the dtv-relative
1079 // offset. This returns true if this is a new GOT entry, false if the symbol
1080 // already has a GOT entry.
1082 template<int size, bool big_endian>
1084 Output_data_got<size, big_endian>::add_local_tls(
1085 Sized_relobj<size, big_endian>* object,
1086 unsigned int symndx,
1089 if (object->local_has_tls_got_offset(symndx, need_pair))
1092 this->entries_.push_back(Got_entry(object, symndx));
1093 object->set_local_tls_got_offset(symndx, this->last_got_offset(), need_pair);
1095 this->entries_.push_back(Got_entry(object, symndx));
1096 this->set_got_size();
1100 // Add an entry (or pair of entries) for a local TLS symbol to the GOT,
1101 // and add a dynamic relocation of type R_TYPE for the first GOT entry.
1102 // Because this is a local symbol, the first GOT entry can be relocated
1103 // relative to a section symbol, and the second GOT entry will have an
1104 // dtv-relative value that can be computed at link time.
1105 template<int size, bool big_endian>
1107 Output_data_got<size, big_endian>::add_local_tls_with_rel(
1108 Sized_relobj<size, big_endian>* object,
1109 unsigned int symndx,
1113 unsigned int r_type)
1115 if (object->local_has_tls_got_offset(symndx, need_pair))
1118 this->entries_.push_back(Got_entry());
1119 unsigned int got_offset = this->last_got_offset();
1120 object->set_local_tls_got_offset(symndx, got_offset, need_pair);
1121 section_offset_type off;
1122 Output_section* os = object->output_section(shndx, &off);
1123 rel_dyn->add_output_section(os, r_type, this, got_offset);
1125 // The second entry of the pair will be statically initialized
1126 // with the TLS offset of the symbol.
1128 this->entries_.push_back(Got_entry(object, symndx));
1130 this->set_got_size();
1133 template<int size, bool big_endian>
1135 Output_data_got<size, big_endian>::add_local_tls_with_rela(
1136 Sized_relobj<size, big_endian>* object,
1137 unsigned int symndx,
1141 unsigned int r_type)
1143 if (object->local_has_tls_got_offset(symndx, need_pair))
1146 this->entries_.push_back(Got_entry());
1147 unsigned int got_offset = this->last_got_offset();
1148 object->set_local_tls_got_offset(symndx, got_offset, need_pair);
1149 section_offset_type off;
1150 Output_section* os = object->output_section(shndx, &off);
1151 rela_dyn->add_output_section(os, r_type, this, got_offset, 0);
1153 // The second entry of the pair will be statically initialized
1154 // with the TLS offset of the symbol.
1156 this->entries_.push_back(Got_entry(object, symndx));
1158 this->set_got_size();
1161 // Write out the GOT.
1163 template<int size, bool big_endian>
1165 Output_data_got<size, big_endian>::do_write(Output_file* of)
1167 const int add = size / 8;
1169 const off_t off = this->offset();
1170 const off_t oview_size = this->data_size();
1171 unsigned char* const oview = of->get_output_view(off, oview_size);
1173 unsigned char* pov = oview;
1174 for (typename Got_entries::const_iterator p = this->entries_.begin();
1175 p != this->entries_.end();
1182 gold_assert(pov - oview == oview_size);
1184 of->write_output_view(off, oview_size, oview);
1186 // We no longer need the GOT entries.
1187 this->entries_.clear();
1190 // Output_data_dynamic::Dynamic_entry methods.
1192 // Write out the entry.
1194 template<int size, bool big_endian>
1196 Output_data_dynamic::Dynamic_entry::write(
1198 const Stringpool* pool
1199 ACCEPT_SIZE_ENDIAN) const
1201 typename elfcpp::Elf_types<size>::Elf_WXword val;
1202 switch (this->classification_)
1204 case DYNAMIC_NUMBER:
1208 case DYNAMIC_SECTION_ADDRESS:
1209 val = this->u_.od->address();
1212 case DYNAMIC_SECTION_SIZE:
1213 val = this->u_.od->data_size();
1216 case DYNAMIC_SYMBOL:
1218 const Sized_symbol<size>* s =
1219 static_cast<const Sized_symbol<size>*>(this->u_.sym);
1224 case DYNAMIC_STRING:
1225 val = pool->get_offset(this->u_.str);
1232 elfcpp::Dyn_write<size, big_endian> dw(pov);
1233 dw.put_d_tag(this->tag_);
1237 // Output_data_dynamic methods.
1239 // Adjust the output section to set the entry size.
1242 Output_data_dynamic::do_adjust_output_section(Output_section* os)
1244 if (parameters->get_size() == 32)
1245 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
1246 else if (parameters->get_size() == 64)
1247 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1252 // Set the final data size.
1255 Output_data_dynamic::set_final_data_size()
1257 // Add the terminating entry.
1258 this->add_constant(elfcpp::DT_NULL, 0);
1261 if (parameters->get_size() == 32)
1262 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
1263 else if (parameters->get_size() == 64)
1264 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1267 this->set_data_size(this->entries_.size() * dyn_size);
1270 // Write out the dynamic entries.
1273 Output_data_dynamic::do_write(Output_file* of)
1275 if (parameters->get_size() == 32)
1277 if (parameters->is_big_endian())
1279 #ifdef HAVE_TARGET_32_BIG
1280 this->sized_write<32, true>(of);
1287 #ifdef HAVE_TARGET_32_LITTLE
1288 this->sized_write<32, false>(of);
1294 else if (parameters->get_size() == 64)
1296 if (parameters->is_big_endian())
1298 #ifdef HAVE_TARGET_64_BIG
1299 this->sized_write<64, true>(of);
1306 #ifdef HAVE_TARGET_64_LITTLE
1307 this->sized_write<64, false>(of);
1317 template<int size, bool big_endian>
1319 Output_data_dynamic::sized_write(Output_file* of)
1321 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1323 const off_t offset = this->offset();
1324 const off_t oview_size = this->data_size();
1325 unsigned char* const oview = of->get_output_view(offset, oview_size);
1327 unsigned char* pov = oview;
1328 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1329 p != this->entries_.end();
1332 p->write SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1333 pov, this->pool_ SELECT_SIZE_ENDIAN(size, big_endian));
1337 gold_assert(pov - oview == oview_size);
1339 of->write_output_view(offset, oview_size, oview);
1341 // We no longer need the dynamic entries.
1342 this->entries_.clear();
1345 // Output_section::Input_section methods.
1347 // Return the data size. For an input section we store the size here.
1348 // For an Output_section_data, we have to ask it for the size.
1351 Output_section::Input_section::data_size() const
1353 if (this->is_input_section())
1354 return this->u1_.data_size;
1356 return this->u2_.posd->data_size();
1359 // Set the address and file offset.
1362 Output_section::Input_section::set_address_and_file_offset(
1365 off_t section_file_offset)
1367 if (this->is_input_section())
1368 this->u2_.object->set_section_offset(this->shndx_,
1369 file_offset - section_file_offset);
1371 this->u2_.posd->set_address_and_file_offset(address, file_offset);
1374 // Finalize the data size.
1377 Output_section::Input_section::finalize_data_size()
1379 if (!this->is_input_section())
1380 this->u2_.posd->finalize_data_size();
1383 // Try to turn an input offset into an output offset. We want to
1384 // return the output offset relative to the start of this
1385 // Input_section in the output section.
1388 Output_section::Input_section::output_offset(
1389 const Relobj* object,
1391 section_offset_type offset,
1392 section_offset_type *poutput) const
1394 if (!this->is_input_section())
1395 return this->u2_.posd->output_offset(object, shndx, offset, poutput);
1398 if (this->shndx_ != shndx || this->u2_.object != object)
1405 // Return whether this is the merge section for the input section
1409 Output_section::Input_section::is_merge_section_for(const Relobj* object,
1410 unsigned int shndx) const
1412 if (this->is_input_section())
1414 return this->u2_.posd->is_merge_section_for(object, shndx);
1417 // Write out the data. We don't have to do anything for an input
1418 // section--they are handled via Object::relocate--but this is where
1419 // we write out the data for an Output_section_data.
1422 Output_section::Input_section::write(Output_file* of)
1424 if (!this->is_input_section())
1425 this->u2_.posd->write(of);
1428 // Write the data to a buffer. As for write(), we don't have to do
1429 // anything for an input section.
1432 Output_section::Input_section::write_to_buffer(unsigned char* buffer)
1434 if (!this->is_input_section())
1435 this->u2_.posd->write_to_buffer(buffer);
1438 // Output_section methods.
1440 // Construct an Output_section. NAME will point into a Stringpool.
1442 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
1443 elfcpp::Elf_Xword flags)
1447 link_section_(NULL),
1449 info_section_(NULL),
1457 first_input_offset_(0),
1459 postprocessing_buffer_(NULL),
1460 needs_symtab_index_(false),
1461 needs_dynsym_index_(false),
1462 should_link_to_symtab_(false),
1463 should_link_to_dynsym_(false),
1464 after_input_sections_(false),
1465 requires_postprocessing_(false),
1468 // An unallocated section has no address. Forcing this means that
1469 // we don't need special treatment for symbols defined in debug
1471 if ((flags & elfcpp::SHF_ALLOC) == 0)
1472 this->set_address(0);
1475 Output_section::~Output_section()
1479 // Set the entry size.
1482 Output_section::set_entsize(uint64_t v)
1484 if (this->entsize_ == 0)
1487 gold_assert(this->entsize_ == v);
1490 // Add the input section SHNDX, with header SHDR, named SECNAME, in
1491 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
1492 // relocation section which applies to this section, or 0 if none, or
1493 // -1U if more than one. Return the offset of the input section
1494 // within the output section. Return -1 if the input section will
1495 // receive special handling. In the normal case we don't always keep
1496 // track of input sections for an Output_section. Instead, each
1497 // Object keeps track of the Output_section for each of its input
1500 template<int size, bool big_endian>
1502 Output_section::add_input_section(Sized_relobj<size, big_endian>* object,
1504 const char* secname,
1505 const elfcpp::Shdr<size, big_endian>& shdr,
1506 unsigned int reloc_shndx)
1508 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
1509 if ((addralign & (addralign - 1)) != 0)
1511 object->error(_("invalid alignment %lu for section \"%s\""),
1512 static_cast<unsigned long>(addralign), secname);
1516 if (addralign > this->addralign_)
1517 this->addralign_ = addralign;
1519 typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
1520 uint64_t entsize = shdr.get_sh_entsize();
1522 // .debug_str is a mergeable string section, but is not always so
1523 // marked by compilers. Mark manually here so we can optimize.
1524 if (strcmp(secname, ".debug_str") == 0)
1526 sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
1530 // If this is a SHF_MERGE section, we pass all the input sections to
1531 // a Output_data_merge. We don't try to handle relocations for such
1533 if ((sh_flags & elfcpp::SHF_MERGE) != 0
1534 && reloc_shndx == 0)
1536 if (this->add_merge_input_section(object, shndx, sh_flags,
1537 entsize, addralign))
1539 // Tell the relocation routines that they need to call the
1540 // output_offset method to determine the final address.
1545 off_t offset_in_section = this->current_data_size_for_child();
1546 off_t aligned_offset_in_section = align_address(offset_in_section,
1549 if (aligned_offset_in_section > offset_in_section
1550 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
1551 && object->target()->has_code_fill())
1553 // We need to add some fill data. Using fill_list_ when
1554 // possible is an optimization, since we will often have fill
1555 // sections without input sections.
1556 off_t fill_len = aligned_offset_in_section - offset_in_section;
1557 if (this->input_sections_.empty())
1558 this->fills_.push_back(Fill(offset_in_section, fill_len));
1561 // FIXME: When relaxing, the size needs to adjust to
1562 // maintain a constant alignment.
1563 std::string fill_data(object->target()->code_fill(fill_len));
1564 Output_data_const* odc = new Output_data_const(fill_data, 1);
1565 this->input_sections_.push_back(Input_section(odc));
1569 this->set_current_data_size_for_child(aligned_offset_in_section
1570 + shdr.get_sh_size());
1572 // We need to keep track of this section if we are already keeping
1573 // track of sections, or if we are relaxing. FIXME: Add test for
1575 if (!this->input_sections_.empty())
1576 this->input_sections_.push_back(Input_section(object, shndx,
1580 return aligned_offset_in_section;
1583 // Add arbitrary data to an output section.
1586 Output_section::add_output_section_data(Output_section_data* posd)
1588 Input_section inp(posd);
1589 this->add_output_section_data(&inp);
1592 // Add arbitrary data to an output section by Input_section.
1595 Output_section::add_output_section_data(Input_section* inp)
1597 if (this->input_sections_.empty())
1598 this->first_input_offset_ = this->current_data_size_for_child();
1600 this->input_sections_.push_back(*inp);
1602 uint64_t addralign = inp->addralign();
1603 if (addralign > this->addralign_)
1604 this->addralign_ = addralign;
1606 inp->set_output_section(this);
1609 // Add a merge section to an output section.
1612 Output_section::add_output_merge_section(Output_section_data* posd,
1613 bool is_string, uint64_t entsize)
1615 Input_section inp(posd, is_string, entsize);
1616 this->add_output_section_data(&inp);
1619 // Add an input section to a SHF_MERGE section.
1622 Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
1623 uint64_t flags, uint64_t entsize,
1626 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
1628 // We only merge strings if the alignment is not more than the
1629 // character size. This could be handled, but it's unusual.
1630 if (is_string && addralign > entsize)
1633 Input_section_list::iterator p;
1634 for (p = this->input_sections_.begin();
1635 p != this->input_sections_.end();
1637 if (p->is_merge_section(is_string, entsize, addralign))
1639 p->add_input_section(object, shndx);
1643 // We handle the actual constant merging in Output_merge_data or
1644 // Output_merge_string_data.
1645 Output_section_data* posd;
1647 posd = new Output_merge_data(entsize, addralign);
1653 posd = new Output_merge_string<char>(addralign);
1656 posd = new Output_merge_string<uint16_t>(addralign);
1659 posd = new Output_merge_string<uint32_t>(addralign);
1666 this->add_output_merge_section(posd, is_string, entsize);
1667 posd->add_input_section(object, shndx);
1672 // Given an address OFFSET relative to the start of input section
1673 // SHNDX in OBJECT, return whether this address is being included in
1674 // the final link. This should only be called if SHNDX in OBJECT has
1675 // a special mapping.
1678 Output_section::is_input_address_mapped(const Relobj* object,
1682 gold_assert(object->is_section_specially_mapped(shndx));
1684 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1685 p != this->input_sections_.end();
1688 section_offset_type output_offset;
1689 if (p->output_offset(object, shndx, offset, &output_offset))
1690 return output_offset != -1;
1693 // By default we assume that the address is mapped. This should
1694 // only be called after we have passed all sections to Layout. At
1695 // that point we should know what we are discarding.
1699 // Given an address OFFSET relative to the start of input section
1700 // SHNDX in object OBJECT, return the output offset relative to the
1701 // start of the input section in the output section. This should only
1702 // be called if SHNDX in OBJECT has a special mapping.
1705 Output_section::output_offset(const Relobj* object, unsigned int shndx,
1706 section_offset_type offset) const
1708 gold_assert(object->is_section_specially_mapped(shndx));
1709 // This can only be called meaningfully when layout is complete.
1710 gold_assert(Output_data::is_layout_complete());
1712 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1713 p != this->input_sections_.end();
1716 section_offset_type output_offset;
1717 if (p->output_offset(object, shndx, offset, &output_offset))
1718 return output_offset;
1723 // Return the output virtual address of OFFSET relative to the start
1724 // of input section SHNDX in object OBJECT.
1727 Output_section::output_address(const Relobj* object, unsigned int shndx,
1730 gold_assert(object->is_section_specially_mapped(shndx));
1732 uint64_t addr = this->address() + this->first_input_offset_;
1733 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1734 p != this->input_sections_.end();
1737 addr = align_address(addr, p->addralign());
1738 section_offset_type output_offset;
1739 if (p->output_offset(object, shndx, offset, &output_offset))
1741 if (output_offset == -1)
1743 return addr + output_offset;
1745 addr += p->data_size();
1748 // If we get here, it means that we don't know the mapping for this
1749 // input section. This might happen in principle if
1750 // add_input_section were called before add_output_section_data.
1751 // But it should never actually happen.
1756 // Return the output address of the start of the merged section for
1757 // input section SHNDX in object OBJECT.
1760 Output_section::starting_output_address(const Relobj* object,
1761 unsigned int shndx) const
1763 gold_assert(object->is_section_specially_mapped(shndx));
1765 uint64_t addr = this->address() + this->first_input_offset_;
1766 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1767 p != this->input_sections_.end();
1770 addr = align_address(addr, p->addralign());
1772 // It would be nice if we could use the existing output_offset
1773 // method to get the output offset of input offset 0.
1774 // Unfortunately we don't know for sure that input offset 0 is
1776 if (p->is_merge_section_for(object, shndx))
1779 addr += p->data_size();
1784 // Set the data size of an Output_section. This is where we handle
1785 // setting the addresses of any Output_section_data objects.
1788 Output_section::set_final_data_size()
1790 if (this->input_sections_.empty())
1792 this->set_data_size(this->current_data_size_for_child());
1796 uint64_t address = this->address();
1797 off_t startoff = this->offset();
1798 off_t off = startoff + this->first_input_offset_;
1799 for (Input_section_list::iterator p = this->input_sections_.begin();
1800 p != this->input_sections_.end();
1803 off = align_address(off, p->addralign());
1804 p->set_address_and_file_offset(address + (off - startoff), off,
1806 off += p->data_size();
1809 this->set_data_size(off - startoff);
1812 // Set the TLS offset. Called only for SHT_TLS sections.
1815 Output_section::do_set_tls_offset(uint64_t tls_base)
1817 this->tls_offset_ = this->address() - tls_base;
1820 // Write the section header to *OSHDR.
1822 template<int size, bool big_endian>
1824 Output_section::write_header(const Layout* layout,
1825 const Stringpool* secnamepool,
1826 elfcpp::Shdr_write<size, big_endian>* oshdr) const
1828 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
1829 oshdr->put_sh_type(this->type_);
1830 oshdr->put_sh_flags(this->flags_);
1831 oshdr->put_sh_addr(this->address());
1832 oshdr->put_sh_offset(this->offset());
1833 oshdr->put_sh_size(this->data_size());
1834 if (this->link_section_ != NULL)
1835 oshdr->put_sh_link(this->link_section_->out_shndx());
1836 else if (this->should_link_to_symtab_)
1837 oshdr->put_sh_link(layout->symtab_section()->out_shndx());
1838 else if (this->should_link_to_dynsym_)
1839 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
1841 oshdr->put_sh_link(this->link_);
1842 if (this->info_section_ != NULL)
1843 oshdr->put_sh_info(this->info_section_->out_shndx());
1845 oshdr->put_sh_info(this->info_);
1846 oshdr->put_sh_addralign(this->addralign_);
1847 oshdr->put_sh_entsize(this->entsize_);
1850 // Write out the data. For input sections the data is written out by
1851 // Object::relocate, but we have to handle Output_section_data objects
1855 Output_section::do_write(Output_file* of)
1857 gold_assert(!this->requires_postprocessing());
1859 off_t output_section_file_offset = this->offset();
1860 for (Fill_list::iterator p = this->fills_.begin();
1861 p != this->fills_.end();
1864 std::string fill_data(of->target()->code_fill(p->length()));
1865 of->write(output_section_file_offset + p->section_offset(),
1866 fill_data.data(), fill_data.size());
1869 for (Input_section_list::iterator p = this->input_sections_.begin();
1870 p != this->input_sections_.end();
1875 // If a section requires postprocessing, create the buffer to use.
1878 Output_section::create_postprocessing_buffer()
1880 gold_assert(this->requires_postprocessing());
1882 if (this->postprocessing_buffer_ != NULL)
1885 if (!this->input_sections_.empty())
1887 off_t off = this->first_input_offset_;
1888 for (Input_section_list::iterator p = this->input_sections_.begin();
1889 p != this->input_sections_.end();
1892 off = align_address(off, p->addralign());
1893 p->finalize_data_size();
1894 off += p->data_size();
1896 this->set_current_data_size_for_child(off);
1899 off_t buffer_size = this->current_data_size_for_child();
1900 this->postprocessing_buffer_ = new unsigned char[buffer_size];
1903 // Write all the data of an Output_section into the postprocessing
1904 // buffer. This is used for sections which require postprocessing,
1905 // such as compression. Input sections are handled by
1906 // Object::Relocate.
1909 Output_section::write_to_postprocessing_buffer()
1911 gold_assert(this->requires_postprocessing());
1913 Target* target = parameters->target();
1914 unsigned char* buffer = this->postprocessing_buffer();
1915 for (Fill_list::iterator p = this->fills_.begin();
1916 p != this->fills_.end();
1919 std::string fill_data(target->code_fill(p->length()));
1920 memcpy(buffer + p->section_offset(), fill_data.data(), fill_data.size());
1923 off_t off = this->first_input_offset_;
1924 for (Input_section_list::iterator p = this->input_sections_.begin();
1925 p != this->input_sections_.end();
1928 off = align_address(off, p->addralign());
1929 p->write_to_buffer(buffer + off);
1930 off += p->data_size();
1934 // Print stats for merge sections to stderr.
1937 Output_section::print_merge_stats()
1939 Input_section_list::iterator p;
1940 for (p = this->input_sections_.begin();
1941 p != this->input_sections_.end();
1943 p->print_merge_stats(this->name_);
1946 // Output segment methods.
1948 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
1959 is_align_known_(false)
1963 // Add an Output_section to an Output_segment.
1966 Output_segment::add_output_section(Output_section* os,
1967 elfcpp::Elf_Word seg_flags,
1970 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
1971 gold_assert(!this->is_align_known_);
1973 // Update the segment flags.
1974 this->flags_ |= seg_flags;
1976 Output_segment::Output_data_list* pdl;
1977 if (os->type() == elfcpp::SHT_NOBITS)
1978 pdl = &this->output_bss_;
1980 pdl = &this->output_data_;
1982 // So that PT_NOTE segments will work correctly, we need to ensure
1983 // that all SHT_NOTE sections are adjacent. This will normally
1984 // happen automatically, because all the SHT_NOTE input sections
1985 // will wind up in the same output section. However, it is possible
1986 // for multiple SHT_NOTE input sections to have different section
1987 // flags, and thus be in different output sections, but for the
1988 // different section flags to map into the same segment flags and
1989 // thus the same output segment.
1991 // Note that while there may be many input sections in an output
1992 // section, there are normally only a few output sections in an
1993 // output segment. This loop is expected to be fast.
1995 if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
1997 Output_segment::Output_data_list::iterator p = pdl->end();
2001 if ((*p)->is_section_type(elfcpp::SHT_NOTE))
2003 // We don't worry about the FRONT parameter.
2009 while (p != pdl->begin());
2012 // Similarly, so that PT_TLS segments will work, we need to group
2013 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
2014 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
2015 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
2016 // correctly. SHF_TLS sections get added to both a PT_LOAD segment
2017 // and the PT_TLS segment -- we do this grouping only for the
2019 if (this->type_ != elfcpp::PT_TLS
2020 && (os->flags() & elfcpp::SHF_TLS) != 0
2021 && !this->output_data_.empty())
2023 pdl = &this->output_data_;
2024 bool nobits = os->type() == elfcpp::SHT_NOBITS;
2025 bool sawtls = false;
2026 Output_segment::Output_data_list::iterator p = pdl->end();
2031 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
2034 // Put a NOBITS section after the first TLS section.
2035 // But a PROGBITS section after the first TLS/PROGBITS
2037 insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS);
2041 // If we've gone past the TLS sections, but we've seen a
2042 // TLS section, then we need to insert this section now.
2048 // We don't worry about the FRONT parameter.
2054 while (p != pdl->begin());
2056 // There are no TLS sections yet; put this one at the requested
2057 // location in the section list.
2061 pdl->push_front(os);
2066 // Add an Output_data (which is not an Output_section) to the start of
2070 Output_segment::add_initial_output_data(Output_data* od)
2072 gold_assert(!this->is_align_known_);
2073 this->output_data_.push_front(od);
2076 // Return the maximum alignment of the Output_data in Output_segment.
2077 // Once we compute this, we prohibit new sections from being added.
2080 Output_segment::addralign()
2082 if (!this->is_align_known_)
2086 addralign = Output_segment::maximum_alignment(&this->output_data_);
2087 if (addralign > this->align_)
2088 this->align_ = addralign;
2090 addralign = Output_segment::maximum_alignment(&this->output_bss_);
2091 if (addralign > this->align_)
2092 this->align_ = addralign;
2094 this->is_align_known_ = true;
2097 return this->align_;
2100 // Return the maximum alignment of a list of Output_data.
2103 Output_segment::maximum_alignment(const Output_data_list* pdl)
2106 for (Output_data_list::const_iterator p = pdl->begin();
2110 uint64_t addralign = (*p)->addralign();
2111 if (addralign > ret)
2117 // Return the number of dynamic relocs applied to this segment.
2120 Output_segment::dynamic_reloc_count() const
2122 return (this->dynamic_reloc_count_list(&this->output_data_)
2123 + this->dynamic_reloc_count_list(&this->output_bss_));
2126 // Return the number of dynamic relocs applied to an Output_data_list.
2129 Output_segment::dynamic_reloc_count_list(const Output_data_list* pdl) const
2131 unsigned int count = 0;
2132 for (Output_data_list::const_iterator p = pdl->begin();
2135 count += (*p)->dynamic_reloc_count();
2139 // Set the section addresses for an Output_segment. ADDR is the
2140 // address and *POFF is the file offset. Set the section indexes
2141 // starting with *PSHNDX. Return the address of the immediately
2142 // following segment. Update *POFF and *PSHNDX.
2145 Output_segment::set_section_addresses(uint64_t addr, off_t* poff,
2146 unsigned int* pshndx)
2148 gold_assert(this->type_ == elfcpp::PT_LOAD);
2150 this->vaddr_ = addr;
2151 this->paddr_ = addr;
2153 off_t orig_off = *poff;
2154 this->offset_ = orig_off;
2156 *poff = align_address(*poff, this->addralign());
2158 addr = this->set_section_list_addresses(&this->output_data_, addr, poff,
2160 this->filesz_ = *poff - orig_off;
2164 uint64_t ret = this->set_section_list_addresses(&this->output_bss_, addr,
2166 this->memsz_ = *poff - orig_off;
2168 // Ignore the file offset adjustments made by the BSS Output_data
2175 // Set the addresses and file offsets in a list of Output_data
2179 Output_segment::set_section_list_addresses(Output_data_list* pdl,
2180 uint64_t addr, off_t* poff,
2181 unsigned int* pshndx)
2183 off_t startoff = *poff;
2185 off_t off = startoff;
2186 for (Output_data_list::iterator p = pdl->begin();
2190 off = align_address(off, (*p)->addralign());
2191 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
2193 // Unless this is a PT_TLS segment, we want to ignore the size
2194 // of a SHF_TLS/SHT_NOBITS section. Such a section does not
2195 // affect the size of a PT_LOAD segment.
2196 if (this->type_ == elfcpp::PT_TLS
2197 || !(*p)->is_section_flag_set(elfcpp::SHF_TLS)
2198 || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
2199 off += (*p)->data_size();
2201 if ((*p)->is_section())
2203 (*p)->set_out_shndx(*pshndx);
2209 return addr + (off - startoff);
2212 // For a non-PT_LOAD segment, set the offset from the sections, if
2216 Output_segment::set_offset()
2218 gold_assert(this->type_ != elfcpp::PT_LOAD);
2220 if (this->output_data_.empty() && this->output_bss_.empty())
2231 const Output_data* first;
2232 if (this->output_data_.empty())
2233 first = this->output_bss_.front();
2235 first = this->output_data_.front();
2236 this->vaddr_ = first->address();
2237 this->paddr_ = this->vaddr_;
2238 this->offset_ = first->offset();
2240 if (this->output_data_.empty())
2244 const Output_data* last_data = this->output_data_.back();
2245 this->filesz_ = (last_data->address()
2246 + last_data->data_size()
2250 const Output_data* last;
2251 if (this->output_bss_.empty())
2252 last = this->output_data_.back();
2254 last = this->output_bss_.back();
2255 this->memsz_ = (last->address()
2260 // Set the TLS offsets of the sections in the PT_TLS segment.
2263 Output_segment::set_tls_offsets()
2265 gold_assert(this->type_ == elfcpp::PT_TLS);
2267 for (Output_data_list::iterator p = this->output_data_.begin();
2268 p != this->output_data_.end();
2270 (*p)->set_tls_offset(this->vaddr_);
2272 for (Output_data_list::iterator p = this->output_bss_.begin();
2273 p != this->output_bss_.end();
2275 (*p)->set_tls_offset(this->vaddr_);
2278 // Return the number of Output_sections in an Output_segment.
2281 Output_segment::output_section_count() const
2283 return (this->output_section_count_list(&this->output_data_)
2284 + this->output_section_count_list(&this->output_bss_));
2287 // Return the number of Output_sections in an Output_data_list.
2290 Output_segment::output_section_count_list(const Output_data_list* pdl) const
2292 unsigned int count = 0;
2293 for (Output_data_list::const_iterator p = pdl->begin();
2297 if ((*p)->is_section())
2303 // Write the segment data into *OPHDR.
2305 template<int size, bool big_endian>
2307 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
2309 ophdr->put_p_type(this->type_);
2310 ophdr->put_p_offset(this->offset_);
2311 ophdr->put_p_vaddr(this->vaddr_);
2312 ophdr->put_p_paddr(this->paddr_);
2313 ophdr->put_p_filesz(this->filesz_);
2314 ophdr->put_p_memsz(this->memsz_);
2315 ophdr->put_p_flags(this->flags_);
2316 ophdr->put_p_align(this->addralign());
2319 // Write the section headers into V.
2321 template<int size, bool big_endian>
2323 Output_segment::write_section_headers(const Layout* layout,
2324 const Stringpool* secnamepool,
2326 unsigned int *pshndx
2327 ACCEPT_SIZE_ENDIAN) const
2329 // Every section that is attached to a segment must be attached to a
2330 // PT_LOAD segment, so we only write out section headers for PT_LOAD
2332 if (this->type_ != elfcpp::PT_LOAD)
2335 v = this->write_section_headers_list
2336 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
2337 layout, secnamepool, &this->output_data_, v, pshndx
2338 SELECT_SIZE_ENDIAN(size, big_endian));
2339 v = this->write_section_headers_list
2340 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
2341 layout, secnamepool, &this->output_bss_, v, pshndx
2342 SELECT_SIZE_ENDIAN(size, big_endian));
2346 template<int size, bool big_endian>
2348 Output_segment::write_section_headers_list(const Layout* layout,
2349 const Stringpool* secnamepool,
2350 const Output_data_list* pdl,
2352 unsigned int* pshndx
2353 ACCEPT_SIZE_ENDIAN) const
2355 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2356 for (Output_data_list::const_iterator p = pdl->begin();
2360 if ((*p)->is_section())
2362 const Output_section* ps = static_cast<const Output_section*>(*p);
2363 gold_assert(*pshndx == ps->out_shndx());
2364 elfcpp::Shdr_write<size, big_endian> oshdr(v);
2365 ps->write_header(layout, secnamepool, &oshdr);
2373 // Output_file methods.
2375 Output_file::Output_file(const General_options& options, Target* target)
2376 : options_(options),
2378 name_(options.output_file_name()),
2382 map_is_anonymous_(false)
2386 // Open the output file.
2389 Output_file::open(off_t file_size)
2391 this->file_size_ = file_size;
2393 // Unlink the file first; otherwise the open() may fail if the file
2394 // is busy (e.g. it's an executable that's currently being executed).
2396 // However, the linker may be part of a system where a zero-length
2397 // file is created for it to write to, with tight permissions (gcc
2398 // 2.95 did something like this). Unlinking the file would work
2399 // around those permission controls, so we only unlink if the file
2400 // has a non-zero size. We also unlink only regular files to avoid
2401 // trouble with directories/etc.
2403 // If we fail, continue; this command is merely a best-effort attempt
2404 // to improve the odds for open().
2406 // We let the name "-" mean "stdout"
2407 if (strcmp(this->name_, "-") == 0)
2408 this->o_ = STDOUT_FILENO;
2412 if (::stat(this->name_, &s) == 0 && s.st_size != 0)
2413 unlink_if_ordinary(this->name_);
2415 int mode = parameters->output_is_object() ? 0666 : 0777;
2416 int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode);
2418 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
2425 // Resize the output file.
2428 Output_file::resize(off_t file_size)
2430 // If the mmap is mapping an anonymous memory buffer, this is easy:
2431 // just mremap to the new size. If it's mapping to a file, we want
2432 // to unmap to flush to the file, then remap after growing the file.
2433 if (this->map_is_anonymous_)
2435 void* base = ::mremap(this->base_, this->file_size_, file_size,
2437 if (base == MAP_FAILED)
2438 gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
2439 this->base_ = static_cast<unsigned char*>(base);
2440 this->file_size_ = file_size;
2445 this->file_size_ = file_size;
2450 // Map the file into memory.
2455 const int o = this->o_;
2457 // If the output file is not a regular file, don't try to mmap it;
2458 // instead, we'll mmap a block of memory (an anonymous buffer), and
2459 // then later write the buffer to the file.
2461 struct stat statbuf;
2462 if (o == STDOUT_FILENO || o == STDERR_FILENO
2463 || ::fstat(o, &statbuf) != 0
2464 || !S_ISREG(statbuf.st_mode))
2466 this->map_is_anonymous_ = true;
2467 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
2468 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
2472 // Write out one byte to make the file the right size.
2473 if (::lseek(o, this->file_size_ - 1, SEEK_SET) < 0)
2474 gold_fatal(_("%s: lseek: %s"), this->name_, strerror(errno));
2476 if (::write(o, &b, 1) != 1)
2477 gold_fatal(_("%s: write: %s"), this->name_, strerror(errno));
2479 // Map the file into memory.
2480 this->map_is_anonymous_ = false;
2481 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
2484 if (base == MAP_FAILED)
2485 gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
2486 this->base_ = static_cast<unsigned char*>(base);
2489 // Unmap the file from memory.
2492 Output_file::unmap()
2494 if (::munmap(this->base_, this->file_size_) < 0)
2495 gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
2499 // Close the output file.
2502 Output_file::close()
2504 // If the map isn't file-backed, we need to write it now.
2505 if (this->map_is_anonymous_)
2507 size_t bytes_to_write = this->file_size_;
2508 while (bytes_to_write > 0)
2510 ssize_t bytes_written = ::write(this->o_, this->base_, bytes_to_write);
2511 if (bytes_written == 0)
2512 gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
2513 else if (bytes_written < 0)
2514 gold_error(_("%s: write: %s"), this->name_, strerror(errno));
2516 bytes_to_write -= bytes_written;
2521 // We don't close stdout or stderr
2522 if (this->o_ != STDOUT_FILENO && this->o_ != STDERR_FILENO)
2523 if (::close(this->o_) < 0)
2524 gold_error(_("%s: close: %s"), this->name_, strerror(errno));
2528 // Instantiate the templates we need. We could use the configure
2529 // script to restrict this to only the ones for implemented targets.
2531 #ifdef HAVE_TARGET_32_LITTLE
2534 Output_section::add_input_section<32, false>(
2535 Sized_relobj<32, false>* object,
2537 const char* secname,
2538 const elfcpp::Shdr<32, false>& shdr,
2539 unsigned int reloc_shndx);
2542 #ifdef HAVE_TARGET_32_BIG
2545 Output_section::add_input_section<32, true>(
2546 Sized_relobj<32, true>* object,
2548 const char* secname,
2549 const elfcpp::Shdr<32, true>& shdr,
2550 unsigned int reloc_shndx);
2553 #ifdef HAVE_TARGET_64_LITTLE
2556 Output_section::add_input_section<64, false>(
2557 Sized_relobj<64, false>* object,
2559 const char* secname,
2560 const elfcpp::Shdr<64, false>& shdr,
2561 unsigned int reloc_shndx);
2564 #ifdef HAVE_TARGET_64_BIG
2567 Output_section::add_input_section<64, true>(
2568 Sized_relobj<64, true>* object,
2570 const char* secname,
2571 const elfcpp::Shdr<64, true>& shdr,
2572 unsigned int reloc_shndx);
2575 #ifdef HAVE_TARGET_32_LITTLE
2577 class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
2580 #ifdef HAVE_TARGET_32_BIG
2582 class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
2585 #ifdef HAVE_TARGET_64_LITTLE
2587 class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
2590 #ifdef HAVE_TARGET_64_BIG
2592 class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
2595 #ifdef HAVE_TARGET_32_LITTLE
2597 class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
2600 #ifdef HAVE_TARGET_32_BIG
2602 class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
2605 #ifdef HAVE_TARGET_64_LITTLE
2607 class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
2610 #ifdef HAVE_TARGET_64_BIG
2612 class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
2615 #ifdef HAVE_TARGET_32_LITTLE
2617 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
2620 #ifdef HAVE_TARGET_32_BIG
2622 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
2625 #ifdef HAVE_TARGET_64_LITTLE
2627 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
2630 #ifdef HAVE_TARGET_64_BIG
2632 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
2635 #ifdef HAVE_TARGET_32_LITTLE
2637 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
2640 #ifdef HAVE_TARGET_32_BIG
2642 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
2645 #ifdef HAVE_TARGET_64_LITTLE
2647 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
2650 #ifdef HAVE_TARGET_64_BIG
2652 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
2655 #ifdef HAVE_TARGET_32_LITTLE
2657 class Output_data_got<32, false>;
2660 #ifdef HAVE_TARGET_32_BIG
2662 class Output_data_got<32, true>;
2665 #ifdef HAVE_TARGET_64_LITTLE
2667 class Output_data_got<64, false>;
2670 #ifdef HAVE_TARGET_64_BIG
2672 class Output_data_got<64, true>;
2675 } // End namespace gold.