1 // output.cc -- manage the output file for gold
3 // Copyright 2006, 2007, 2008 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(
65 parameters->target().get_size());
68 // Return the default alignment for a size--32 or 64.
71 Output_data::default_alignment_for_size(int size)
81 // Output_section_header methods. This currently assumes that the
82 // segment and section lists are complete at construction time.
84 Output_section_headers::Output_section_headers(
86 const Layout::Segment_list* segment_list,
87 const Layout::Section_list* section_list,
88 const Layout::Section_list* unattached_section_list,
89 const Stringpool* secnamepool)
91 segment_list_(segment_list),
92 section_list_(section_list),
93 unattached_section_list_(unattached_section_list),
94 secnamepool_(secnamepool)
96 // Count all the sections. Start with 1 for the null section.
98 if (!parameters->options().relocatable())
100 for (Layout::Segment_list::const_iterator p = segment_list->begin();
101 p != segment_list->end();
103 if ((*p)->type() == elfcpp::PT_LOAD)
104 count += (*p)->output_section_count();
108 for (Layout::Section_list::const_iterator p = section_list->begin();
109 p != section_list->end();
111 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
114 count += unattached_section_list->size();
116 const int size = parameters->target().get_size();
119 shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
121 shdr_size = elfcpp::Elf_sizes<64>::shdr_size;
125 this->set_data_size(count * shdr_size);
128 // Write out the section headers.
131 Output_section_headers::do_write(Output_file* of)
133 switch (parameters->size_and_endianness())
135 #ifdef HAVE_TARGET_32_LITTLE
136 case Parameters::TARGET_32_LITTLE:
137 this->do_sized_write<32, false>(of);
140 #ifdef HAVE_TARGET_32_BIG
141 case Parameters::TARGET_32_BIG:
142 this->do_sized_write<32, true>(of);
145 #ifdef HAVE_TARGET_64_LITTLE
146 case Parameters::TARGET_64_LITTLE:
147 this->do_sized_write<64, false>(of);
150 #ifdef HAVE_TARGET_64_BIG
151 case Parameters::TARGET_64_BIG:
152 this->do_sized_write<64, true>(of);
160 template<int size, bool big_endian>
162 Output_section_headers::do_sized_write(Output_file* of)
164 off_t all_shdrs_size = this->data_size();
165 unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size);
167 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
168 unsigned char* v = view;
171 typename elfcpp::Shdr_write<size, big_endian> oshdr(v);
172 oshdr.put_sh_name(0);
173 oshdr.put_sh_type(elfcpp::SHT_NULL);
174 oshdr.put_sh_flags(0);
175 oshdr.put_sh_addr(0);
176 oshdr.put_sh_offset(0);
177 oshdr.put_sh_size(0);
178 oshdr.put_sh_link(0);
179 oshdr.put_sh_info(0);
180 oshdr.put_sh_addralign(0);
181 oshdr.put_sh_entsize(0);
186 unsigned int shndx = 1;
187 if (!parameters->options().relocatable())
189 for (Layout::Segment_list::const_iterator p =
190 this->segment_list_->begin();
191 p != this->segment_list_->end();
193 v = (*p)->write_section_headers<size, big_endian>(this->layout_,
200 for (Layout::Section_list::const_iterator p =
201 this->section_list_->begin();
202 p != this->section_list_->end();
205 // We do unallocated sections below, except that group
206 // sections have to come first.
207 if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
208 && (*p)->type() != elfcpp::SHT_GROUP)
210 gold_assert(shndx == (*p)->out_shndx());
211 elfcpp::Shdr_write<size, big_endian> oshdr(v);
212 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
218 for (Layout::Section_list::const_iterator p =
219 this->unattached_section_list_->begin();
220 p != this->unattached_section_list_->end();
223 // For a relocatable link, we did unallocated group sections
224 // above, since they have to come first.
225 if ((*p)->type() == elfcpp::SHT_GROUP
226 && parameters->options().relocatable())
228 gold_assert(shndx == (*p)->out_shndx());
229 elfcpp::Shdr_write<size, big_endian> oshdr(v);
230 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
235 of->write_output_view(this->offset(), all_shdrs_size, view);
238 // Output_segment_header methods.
240 Output_segment_headers::Output_segment_headers(
241 const Layout::Segment_list& segment_list)
242 : segment_list_(segment_list)
244 const int size = parameters->target().get_size();
247 phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
249 phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
253 this->set_data_size(segment_list.size() * phdr_size);
257 Output_segment_headers::do_write(Output_file* of)
259 switch (parameters->size_and_endianness())
261 #ifdef HAVE_TARGET_32_LITTLE
262 case Parameters::TARGET_32_LITTLE:
263 this->do_sized_write<32, false>(of);
266 #ifdef HAVE_TARGET_32_BIG
267 case Parameters::TARGET_32_BIG:
268 this->do_sized_write<32, true>(of);
271 #ifdef HAVE_TARGET_64_LITTLE
272 case Parameters::TARGET_64_LITTLE:
273 this->do_sized_write<64, false>(of);
276 #ifdef HAVE_TARGET_64_BIG
277 case Parameters::TARGET_64_BIG:
278 this->do_sized_write<64, true>(of);
286 template<int size, bool big_endian>
288 Output_segment_headers::do_sized_write(Output_file* of)
290 const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size;
291 off_t all_phdrs_size = this->segment_list_.size() * phdr_size;
292 gold_assert(all_phdrs_size == this->data_size());
293 unsigned char* view = of->get_output_view(this->offset(),
295 unsigned char* v = view;
296 for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
297 p != this->segment_list_.end();
300 elfcpp::Phdr_write<size, big_endian> ophdr(v);
301 (*p)->write_header(&ophdr);
305 gold_assert(v - view == all_phdrs_size);
307 of->write_output_view(this->offset(), all_phdrs_size, view);
310 // Output_file_header methods.
312 Output_file_header::Output_file_header(const Target* target,
313 const Symbol_table* symtab,
314 const Output_segment_headers* osh,
318 segment_header_(osh),
319 section_header_(NULL),
323 const int size = parameters->target().get_size();
326 ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
328 ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
332 this->set_data_size(ehdr_size);
335 // Set the section table information for a file header.
338 Output_file_header::set_section_info(const Output_section_headers* shdrs,
339 const Output_section* shstrtab)
341 this->section_header_ = shdrs;
342 this->shstrtab_ = shstrtab;
345 // Write out the file header.
348 Output_file_header::do_write(Output_file* of)
350 gold_assert(this->offset() == 0);
352 switch (parameters->size_and_endianness())
354 #ifdef HAVE_TARGET_32_LITTLE
355 case Parameters::TARGET_32_LITTLE:
356 this->do_sized_write<32, false>(of);
359 #ifdef HAVE_TARGET_32_BIG
360 case Parameters::TARGET_32_BIG:
361 this->do_sized_write<32, true>(of);
364 #ifdef HAVE_TARGET_64_LITTLE
365 case Parameters::TARGET_64_LITTLE:
366 this->do_sized_write<64, false>(of);
369 #ifdef HAVE_TARGET_64_BIG
370 case Parameters::TARGET_64_BIG:
371 this->do_sized_write<64, true>(of);
379 // Write out the file header with appropriate size and endianess.
381 template<int size, bool big_endian>
383 Output_file_header::do_sized_write(Output_file* of)
385 gold_assert(this->offset() == 0);
387 int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
388 unsigned char* view = of->get_output_view(0, ehdr_size);
389 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
391 unsigned char e_ident[elfcpp::EI_NIDENT];
392 memset(e_ident, 0, elfcpp::EI_NIDENT);
393 e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
394 e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
395 e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
396 e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
398 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
400 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
403 e_ident[elfcpp::EI_DATA] = (big_endian
404 ? elfcpp::ELFDATA2MSB
405 : elfcpp::ELFDATA2LSB);
406 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
407 // FIXME: Some targets may need to set EI_OSABI and EI_ABIVERSION.
408 oehdr.put_e_ident(e_ident);
411 if (parameters->options().relocatable())
412 e_type = elfcpp::ET_REL;
413 else if (parameters->options().shared())
414 e_type = elfcpp::ET_DYN;
416 e_type = elfcpp::ET_EXEC;
417 oehdr.put_e_type(e_type);
419 oehdr.put_e_machine(this->target_->machine_code());
420 oehdr.put_e_version(elfcpp::EV_CURRENT);
422 oehdr.put_e_entry(this->entry<size>());
424 if (this->segment_header_ == NULL)
425 oehdr.put_e_phoff(0);
427 oehdr.put_e_phoff(this->segment_header_->offset());
429 oehdr.put_e_shoff(this->section_header_->offset());
431 // FIXME: The target needs to set the flags.
432 oehdr.put_e_flags(0);
434 oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
436 if (this->segment_header_ == NULL)
438 oehdr.put_e_phentsize(0);
439 oehdr.put_e_phnum(0);
443 oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
444 oehdr.put_e_phnum(this->segment_header_->data_size()
445 / elfcpp::Elf_sizes<size>::phdr_size);
448 oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
449 oehdr.put_e_shnum(this->section_header_->data_size()
450 / elfcpp::Elf_sizes<size>::shdr_size);
451 oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
453 of->write_output_view(0, ehdr_size, view);
456 // Return the value to use for the entry address. THIS->ENTRY_ is the
457 // symbol specified on the command line, if any.
460 typename elfcpp::Elf_types<size>::Elf_Addr
461 Output_file_header::entry()
463 const bool should_issue_warning = (this->entry_ != NULL
464 && !parameters->options().relocatable()
465 && !parameters->options().shared());
467 // FIXME: Need to support target specific entry symbol.
468 const char* entry = this->entry_;
472 Symbol* sym = this->symtab_->lookup(entry);
474 typename Sized_symbol<size>::Value_type v;
477 Sized_symbol<size>* ssym;
478 ssym = this->symtab_->get_sized_symbol<size>(sym);
479 if (!ssym->is_defined() && should_issue_warning)
480 gold_warning("entry symbol '%s' exists but is not defined", entry);
485 // We couldn't find the entry symbol. See if we can parse it as
486 // a number. This supports, e.g., -e 0x1000.
488 v = strtoull(entry, &endptr, 0);
491 if (should_issue_warning)
492 gold_warning("cannot find entry symbol '%s'", entry);
500 // Output_data_const methods.
503 Output_data_const::do_write(Output_file* of)
505 of->write(this->offset(), this->data_.data(), this->data_.size());
508 // Output_data_const_buffer methods.
511 Output_data_const_buffer::do_write(Output_file* of)
513 of->write(this->offset(), this->p_, this->data_size());
516 // Output_section_data methods.
518 // Record the output section, and set the entry size and such.
521 Output_section_data::set_output_section(Output_section* os)
523 gold_assert(this->output_section_ == NULL);
524 this->output_section_ = os;
525 this->do_adjust_output_section(os);
528 // Return the section index of the output section.
531 Output_section_data::do_out_shndx() const
533 gold_assert(this->output_section_ != NULL);
534 return this->output_section_->out_shndx();
537 // Output_data_strtab methods.
539 // Set the final data size.
542 Output_data_strtab::set_final_data_size()
544 this->strtab_->set_string_offsets();
545 this->set_data_size(this->strtab_->get_strtab_size());
548 // Write out a string table.
551 Output_data_strtab::do_write(Output_file* of)
553 this->strtab_->write(of, this->offset());
556 // Output_reloc methods.
558 // A reloc against a global symbol.
560 template<bool dynamic, int size, bool big_endian>
561 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), is_section_symbol_(false), shndx_(INVALID_CODE)
570 // this->type_ is a bitfield; make sure TYPE fits.
571 gold_assert(this->type_ == type);
572 this->u1_.gsym = gsym;
575 this->set_needs_dynsym_index();
578 template<bool dynamic, int size, bool big_endian>
579 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
586 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
587 is_relative_(is_relative), is_section_symbol_(false), shndx_(shndx)
589 gold_assert(shndx != INVALID_CODE);
590 // this->type_ is a bitfield; make sure TYPE fits.
591 gold_assert(this->type_ == type);
592 this->u1_.gsym = gsym;
593 this->u2_.relobj = relobj;
595 this->set_needs_dynsym_index();
598 // A reloc against a local symbol.
600 template<bool dynamic, int size, bool big_endian>
601 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
602 Sized_relobj<size, big_endian>* relobj,
603 unsigned int local_sym_index,
608 bool is_section_symbol)
609 : address_(address), local_sym_index_(local_sym_index), type_(type),
610 is_relative_(is_relative), is_section_symbol_(is_section_symbol),
613 gold_assert(local_sym_index != GSYM_CODE
614 && local_sym_index != INVALID_CODE);
615 // this->type_ is a bitfield; make sure TYPE fits.
616 gold_assert(this->type_ == type);
617 this->u1_.relobj = relobj;
620 this->set_needs_dynsym_index();
623 template<bool dynamic, int size, bool big_endian>
624 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
625 Sized_relobj<size, big_endian>* relobj,
626 unsigned int local_sym_index,
631 bool is_section_symbol)
632 : address_(address), local_sym_index_(local_sym_index), type_(type),
633 is_relative_(is_relative), is_section_symbol_(is_section_symbol),
636 gold_assert(local_sym_index != GSYM_CODE
637 && local_sym_index != INVALID_CODE);
638 gold_assert(shndx != INVALID_CODE);
639 // this->type_ is a bitfield; make sure TYPE fits.
640 gold_assert(this->type_ == type);
641 this->u1_.relobj = relobj;
642 this->u2_.relobj = relobj;
644 this->set_needs_dynsym_index();
647 // A reloc against the STT_SECTION symbol of an output section.
649 template<bool dynamic, int size, bool big_endian>
650 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
655 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
656 is_relative_(false), is_section_symbol_(true), shndx_(INVALID_CODE)
658 // this->type_ is a bitfield; make sure TYPE fits.
659 gold_assert(this->type_ == type);
663 this->set_needs_dynsym_index();
665 os->set_needs_symtab_index();
668 template<bool dynamic, int size, bool big_endian>
669 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
675 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
676 is_relative_(false), is_section_symbol_(true), shndx_(shndx)
678 gold_assert(shndx != INVALID_CODE);
679 // this->type_ is a bitfield; make sure TYPE fits.
680 gold_assert(this->type_ == type);
682 this->u2_.relobj = relobj;
684 this->set_needs_dynsym_index();
686 os->set_needs_symtab_index();
689 // Record that we need a dynamic symbol index for this relocation.
691 template<bool dynamic, int size, bool big_endian>
693 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
694 set_needs_dynsym_index()
696 if (this->is_relative_)
698 switch (this->local_sym_index_)
704 this->u1_.gsym->set_needs_dynsym_entry();
708 this->u1_.os->set_needs_dynsym_index();
716 const unsigned int lsi = this->local_sym_index_;
717 if (!this->is_section_symbol_)
718 this->u1_.relobj->set_needs_output_dynsym_entry(lsi);
721 section_offset_type dummy;
722 Output_section* os = this->u1_.relobj->output_section(lsi, &dummy);
723 gold_assert(os != NULL);
724 os->set_needs_dynsym_index();
731 // Get the symbol index of a relocation.
733 template<bool dynamic, int size, bool big_endian>
735 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
739 switch (this->local_sym_index_)
745 if (this->u1_.gsym == NULL)
748 index = this->u1_.gsym->dynsym_index();
750 index = this->u1_.gsym->symtab_index();
755 index = this->u1_.os->dynsym_index();
757 index = this->u1_.os->symtab_index();
761 // Relocations without symbols use a symbol index of 0.
767 const unsigned int lsi = this->local_sym_index_;
768 if (!this->is_section_symbol_)
771 index = this->u1_.relobj->dynsym_index(lsi);
773 index = this->u1_.relobj->symtab_index(lsi);
777 section_offset_type dummy;
778 Output_section* os = this->u1_.relobj->output_section(lsi, &dummy);
779 gold_assert(os != NULL);
781 index = os->dynsym_index();
783 index = os->symtab_index();
788 gold_assert(index != -1U);
792 // For a local section symbol, get the section offset of the input
793 // section within the output section.
795 template<bool dynamic, int size, bool big_endian>
797 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
798 local_section_offset() const
800 const unsigned int lsi = this->local_sym_index_;
801 section_offset_type offset;
802 Output_section* os = this->u1_.relobj->output_section(lsi, &offset);
803 gold_assert(os != NULL && offset != -1);
807 // Write out the offset and info fields of a Rel or Rela relocation
810 template<bool dynamic, int size, bool big_endian>
811 template<typename Write_rel>
813 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
816 Address address = this->address_;
817 if (this->shndx_ != INVALID_CODE)
819 section_offset_type off;
820 Output_section* os = this->u2_.relobj->output_section(this->shndx_,
822 gold_assert(os != NULL);
824 address += os->address() + off;
827 address = os->output_address(this->u2_.relobj, this->shndx_,
829 gold_assert(address != -1U);
832 else if (this->u2_.od != NULL)
833 address += this->u2_.od->address();
834 wr->put_r_offset(address);
835 unsigned int sym_index = this->is_relative_ ? 0 : this->get_symbol_index();
836 wr->put_r_info(elfcpp::elf_r_info<size>(sym_index, this->type_));
839 // Write out a Rel relocation.
841 template<bool dynamic, int size, bool big_endian>
843 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
844 unsigned char* pov) const
846 elfcpp::Rel_write<size, big_endian> orel(pov);
847 this->write_rel(&orel);
850 // Get the value of the symbol referred to by a Rel relocation.
852 template<bool dynamic, int size, bool big_endian>
853 typename elfcpp::Elf_types<size>::Elf_Addr
854 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value(
855 Address addend) const
857 if (this->local_sym_index_ == GSYM_CODE)
859 const Sized_symbol<size>* sym;
860 sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym);
861 return sym->value() + addend;
863 gold_assert(this->local_sym_index_ != SECTION_CODE
864 && this->local_sym_index_ != INVALID_CODE
865 && !this->is_section_symbol_);
866 const unsigned int lsi = this->local_sym_index_;
867 const Symbol_value<size>* symval = this->u1_.relobj->local_symbol(lsi);
868 return symval->value(this->u1_.relobj, addend);
871 // Write out a Rela relocation.
873 template<bool dynamic, int size, bool big_endian>
875 Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
876 unsigned char* pov) const
878 elfcpp::Rela_write<size, big_endian> orel(pov);
879 this->rel_.write_rel(&orel);
880 Addend addend = this->addend_;
881 if (this->rel_.is_relative())
882 addend = this->rel_.symbol_value(addend);
883 else if (this->rel_.is_local_section_symbol())
884 addend += this->rel_.local_section_offset();
885 orel.put_r_addend(addend);
888 // Output_data_reloc_base methods.
890 // Adjust the output section.
892 template<int sh_type, bool dynamic, int size, bool big_endian>
894 Output_data_reloc_base<sh_type, dynamic, size, big_endian>
895 ::do_adjust_output_section(Output_section* os)
897 if (sh_type == elfcpp::SHT_REL)
898 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
899 else if (sh_type == elfcpp::SHT_RELA)
900 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
904 os->set_should_link_to_dynsym();
906 os->set_should_link_to_symtab();
909 // Write out relocation data.
911 template<int sh_type, bool dynamic, int size, bool big_endian>
913 Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
916 const off_t off = this->offset();
917 const off_t oview_size = this->data_size();
918 unsigned char* const oview = of->get_output_view(off, oview_size);
920 unsigned char* pov = oview;
921 for (typename Relocs::const_iterator p = this->relocs_.begin();
922 p != this->relocs_.end();
929 gold_assert(pov - oview == oview_size);
931 of->write_output_view(off, oview_size, oview);
933 // We no longer need the relocation entries.
934 this->relocs_.clear();
937 // Class Output_relocatable_relocs.
939 template<int sh_type, int size, bool big_endian>
941 Output_relocatable_relocs<sh_type, size, big_endian>::set_final_data_size()
943 this->set_data_size(this->rr_->output_reloc_count()
944 * Reloc_types<sh_type, size, big_endian>::reloc_size);
947 // class Output_data_group.
949 template<int size, bool big_endian>
950 Output_data_group<size, big_endian>::Output_data_group(
951 Sized_relobj<size, big_endian>* relobj,
952 section_size_type entry_count,
953 const elfcpp::Elf_Word* contents)
954 : Output_section_data(entry_count * 4, 4),
957 this->flags_ = elfcpp::Swap<32, big_endian>::readval(contents);
958 for (section_size_type i = 1; i < entry_count; ++i)
960 unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(contents + i);
961 this->input_sections_.push_back(shndx);
965 // Write out the section group, which means translating the section
966 // indexes to apply to the output file.
968 template<int size, bool big_endian>
970 Output_data_group<size, big_endian>::do_write(Output_file* of)
972 const off_t off = this->offset();
973 const section_size_type oview_size =
974 convert_to_section_size_type(this->data_size());
975 unsigned char* const oview = of->get_output_view(off, oview_size);
977 elfcpp::Elf_Word* contents = reinterpret_cast<elfcpp::Elf_Word*>(oview);
978 elfcpp::Swap<32, big_endian>::writeval(contents, this->flags_);
981 for (std::vector<unsigned int>::const_iterator p =
982 this->input_sections_.begin();
983 p != this->input_sections_.end();
986 section_offset_type dummy;
987 Output_section* os = this->relobj_->output_section(*p, &dummy);
989 unsigned int output_shndx;
991 output_shndx = os->out_shndx();
994 this->relobj_->error(_("section group retained but "
995 "group element discarded"));
999 elfcpp::Swap<32, big_endian>::writeval(contents, output_shndx);
1002 size_t wrote = reinterpret_cast<unsigned char*>(contents) - oview;
1003 gold_assert(wrote == oview_size);
1005 of->write_output_view(off, oview_size, oview);
1007 // We no longer need this information.
1008 this->input_sections_.clear();
1011 // Output_data_got::Got_entry methods.
1013 // Write out the entry.
1015 template<int size, bool big_endian>
1017 Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
1021 switch (this->local_sym_index_)
1025 // If the symbol is resolved locally, we need to write out the
1026 // link-time value, which will be relocated dynamically by a
1027 // RELATIVE relocation.
1028 Symbol* gsym = this->u_.gsym;
1029 Sized_symbol<size>* sgsym;
1030 // This cast is a bit ugly. We don't want to put a
1031 // virtual method in Symbol, because we want Symbol to be
1032 // as small as possible.
1033 sgsym = static_cast<Sized_symbol<size>*>(gsym);
1034 val = sgsym->value();
1039 val = this->u_.constant;
1044 const unsigned int lsi = this->local_sym_index_;
1045 const Symbol_value<size>* symval = this->u_.object->local_symbol(lsi);
1046 val = symval->value(this->u_.object, 0);
1051 elfcpp::Swap<size, big_endian>::writeval(pov, val);
1054 // Output_data_got methods.
1056 // Add an entry for a global symbol to the GOT. This returns true if
1057 // this is a new GOT entry, false if the symbol already had a GOT
1060 template<int size, bool big_endian>
1062 Output_data_got<size, big_endian>::add_global(Symbol* gsym)
1064 if (gsym->has_got_offset())
1067 this->entries_.push_back(Got_entry(gsym));
1068 this->set_got_size();
1069 gsym->set_got_offset(this->last_got_offset());
1073 // Add an entry for a global symbol to the GOT, and add a dynamic
1074 // relocation of type R_TYPE for the GOT entry.
1075 template<int size, bool big_endian>
1077 Output_data_got<size, big_endian>::add_global_with_rel(
1080 unsigned int r_type)
1082 if (gsym->has_got_offset())
1085 this->entries_.push_back(Got_entry());
1086 this->set_got_size();
1087 unsigned int got_offset = this->last_got_offset();
1088 gsym->set_got_offset(got_offset);
1089 rel_dyn->add_global(gsym, r_type, this, got_offset);
1092 template<int size, bool big_endian>
1094 Output_data_got<size, big_endian>::add_global_with_rela(
1097 unsigned int r_type)
1099 if (gsym->has_got_offset())
1102 this->entries_.push_back(Got_entry());
1103 this->set_got_size();
1104 unsigned int got_offset = this->last_got_offset();
1105 gsym->set_got_offset(got_offset);
1106 rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
1109 // Add an entry for a local symbol to the GOT. This returns true if
1110 // this is a new GOT entry, false if the symbol already has a GOT
1113 template<int size, bool big_endian>
1115 Output_data_got<size, big_endian>::add_local(
1116 Sized_relobj<size, big_endian>* object,
1117 unsigned int symndx)
1119 if (object->local_has_got_offset(symndx))
1122 this->entries_.push_back(Got_entry(object, symndx));
1123 this->set_got_size();
1124 object->set_local_got_offset(symndx, this->last_got_offset());
1128 // Add an entry for a local symbol to the GOT, and add a dynamic
1129 // relocation of type R_TYPE for the GOT entry.
1130 template<int size, bool big_endian>
1132 Output_data_got<size, big_endian>::add_local_with_rel(
1133 Sized_relobj<size, big_endian>* object,
1134 unsigned int symndx,
1136 unsigned int r_type)
1138 if (object->local_has_got_offset(symndx))
1141 this->entries_.push_back(Got_entry());
1142 this->set_got_size();
1143 unsigned int got_offset = this->last_got_offset();
1144 object->set_local_got_offset(symndx, got_offset);
1145 rel_dyn->add_local(object, symndx, r_type, this, got_offset);
1148 template<int size, bool big_endian>
1150 Output_data_got<size, big_endian>::add_local_with_rela(
1151 Sized_relobj<size, big_endian>* object,
1152 unsigned int symndx,
1154 unsigned int r_type)
1156 if (object->local_has_got_offset(symndx))
1159 this->entries_.push_back(Got_entry());
1160 this->set_got_size();
1161 unsigned int got_offset = this->last_got_offset();
1162 object->set_local_got_offset(symndx, got_offset);
1163 rela_dyn->add_local(object, symndx, r_type, this, got_offset, 0);
1166 // Add an entry (or a pair of entries) for a global TLS symbol to the GOT.
1167 // In a pair of entries, the first value in the pair will be used for the
1168 // module index, and the second value will be used for the dtv-relative
1169 // offset. This returns true if this is a new GOT entry, false if the symbol
1170 // already has a GOT entry.
1172 template<int size, bool big_endian>
1174 Output_data_got<size, big_endian>::add_global_tls(Symbol* gsym, bool need_pair)
1176 if (gsym->has_tls_got_offset(need_pair))
1179 this->entries_.push_back(Got_entry(gsym));
1180 gsym->set_tls_got_offset(this->last_got_offset(), need_pair);
1182 this->entries_.push_back(Got_entry(gsym));
1183 this->set_got_size();
1187 // Add an entry for a global TLS symbol to the GOT, and add a dynamic
1188 // relocation of type R_TYPE.
1189 template<int size, bool big_endian>
1191 Output_data_got<size, big_endian>::add_global_tls_with_rel(
1194 unsigned int r_type)
1196 if (gsym->has_tls_got_offset(false))
1199 this->entries_.push_back(Got_entry());
1200 this->set_got_size();
1201 unsigned int got_offset = this->last_got_offset();
1202 gsym->set_tls_got_offset(got_offset, false);
1203 rel_dyn->add_global(gsym, r_type, this, got_offset);
1206 template<int size, bool big_endian>
1208 Output_data_got<size, big_endian>::add_global_tls_with_rela(
1211 unsigned int r_type)
1213 if (gsym->has_tls_got_offset(false))
1216 this->entries_.push_back(Got_entry());
1217 this->set_got_size();
1218 unsigned int got_offset = this->last_got_offset();
1219 gsym->set_tls_got_offset(got_offset, false);
1220 rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
1223 // Add a pair of entries for a global TLS symbol to the GOT, and add
1224 // dynamic relocations of type MOD_R_TYPE and DTV_R_TYPE, respectively.
1225 template<int size, bool big_endian>
1227 Output_data_got<size, big_endian>::add_global_tls_with_rel(
1230 unsigned int mod_r_type,
1231 unsigned int dtv_r_type)
1233 if (gsym->has_tls_got_offset(true))
1236 this->entries_.push_back(Got_entry());
1237 unsigned int got_offset = this->last_got_offset();
1238 gsym->set_tls_got_offset(got_offset, true);
1239 rel_dyn->add_global(gsym, mod_r_type, this, got_offset);
1241 this->entries_.push_back(Got_entry());
1242 this->set_got_size();
1243 got_offset = this->last_got_offset();
1244 rel_dyn->add_global(gsym, dtv_r_type, this, got_offset);
1247 template<int size, bool big_endian>
1249 Output_data_got<size, big_endian>::add_global_tls_with_rela(
1252 unsigned int mod_r_type,
1253 unsigned int dtv_r_type)
1255 if (gsym->has_tls_got_offset(true))
1258 this->entries_.push_back(Got_entry());
1259 unsigned int got_offset = this->last_got_offset();
1260 gsym->set_tls_got_offset(got_offset, true);
1261 rela_dyn->add_global(gsym, mod_r_type, this, got_offset, 0);
1263 this->entries_.push_back(Got_entry());
1264 this->set_got_size();
1265 got_offset = this->last_got_offset();
1266 rela_dyn->add_global(gsym, dtv_r_type, this, got_offset, 0);
1269 // Add an entry (or a pair of entries) for a local TLS symbol to the GOT.
1270 // In a pair of entries, the first value in the pair will be used for the
1271 // module index, and the second value will be used for the dtv-relative
1272 // offset. This returns true if this is a new GOT entry, false if the symbol
1273 // already has a GOT entry.
1275 template<int size, bool big_endian>
1277 Output_data_got<size, big_endian>::add_local_tls(
1278 Sized_relobj<size, big_endian>* object,
1279 unsigned int symndx,
1282 if (object->local_has_tls_got_offset(symndx, need_pair))
1285 this->entries_.push_back(Got_entry(object, symndx));
1286 object->set_local_tls_got_offset(symndx, this->last_got_offset(), need_pair);
1288 this->entries_.push_back(Got_entry(object, symndx));
1289 this->set_got_size();
1293 // Add an entry (or pair of entries) for a local TLS symbol to the GOT,
1294 // and add a dynamic relocation of type R_TYPE for the first GOT entry.
1295 // Because this is a local symbol, the first GOT entry can be relocated
1296 // relative to a section symbol, and the second GOT entry will have an
1297 // dtv-relative value that can be computed at link time.
1298 template<int size, bool big_endian>
1300 Output_data_got<size, big_endian>::add_local_tls_with_rel(
1301 Sized_relobj<size, big_endian>* object,
1302 unsigned int symndx,
1306 unsigned int r_type)
1308 if (object->local_has_tls_got_offset(symndx, need_pair))
1311 this->entries_.push_back(Got_entry());
1312 unsigned int got_offset = this->last_got_offset();
1313 object->set_local_tls_got_offset(symndx, got_offset, need_pair);
1314 section_offset_type off;
1315 Output_section* os = object->output_section(shndx, &off);
1316 rel_dyn->add_output_section(os, r_type, this, got_offset);
1318 // The second entry of the pair will be statically initialized
1319 // with the TLS offset of the symbol.
1321 this->entries_.push_back(Got_entry(object, symndx));
1323 this->set_got_size();
1326 template<int size, bool big_endian>
1328 Output_data_got<size, big_endian>::add_local_tls_with_rela(
1329 Sized_relobj<size, big_endian>* object,
1330 unsigned int symndx,
1334 unsigned int r_type)
1336 if (object->local_has_tls_got_offset(symndx, need_pair))
1339 this->entries_.push_back(Got_entry());
1340 unsigned int got_offset = this->last_got_offset();
1341 object->set_local_tls_got_offset(symndx, got_offset, need_pair);
1342 section_offset_type off;
1343 Output_section* os = object->output_section(shndx, &off);
1344 rela_dyn->add_output_section(os, r_type, this, got_offset, 0);
1346 // The second entry of the pair will be statically initialized
1347 // with the TLS offset of the symbol.
1349 this->entries_.push_back(Got_entry(object, symndx));
1351 this->set_got_size();
1354 // Write out the GOT.
1356 template<int size, bool big_endian>
1358 Output_data_got<size, big_endian>::do_write(Output_file* of)
1360 const int add = size / 8;
1362 const off_t off = this->offset();
1363 const off_t oview_size = this->data_size();
1364 unsigned char* const oview = of->get_output_view(off, oview_size);
1366 unsigned char* pov = oview;
1367 for (typename Got_entries::const_iterator p = this->entries_.begin();
1368 p != this->entries_.end();
1375 gold_assert(pov - oview == oview_size);
1377 of->write_output_view(off, oview_size, oview);
1379 // We no longer need the GOT entries.
1380 this->entries_.clear();
1383 // Output_data_dynamic::Dynamic_entry methods.
1385 // Write out the entry.
1387 template<int size, bool big_endian>
1389 Output_data_dynamic::Dynamic_entry::write(
1391 const Stringpool* pool) const
1393 typename elfcpp::Elf_types<size>::Elf_WXword val;
1394 switch (this->classification_)
1396 case DYNAMIC_NUMBER:
1400 case DYNAMIC_SECTION_ADDRESS:
1401 val = this->u_.od->address();
1404 case DYNAMIC_SECTION_SIZE:
1405 val = this->u_.od->data_size();
1408 case DYNAMIC_SYMBOL:
1410 const Sized_symbol<size>* s =
1411 static_cast<const Sized_symbol<size>*>(this->u_.sym);
1416 case DYNAMIC_STRING:
1417 val = pool->get_offset(this->u_.str);
1424 elfcpp::Dyn_write<size, big_endian> dw(pov);
1425 dw.put_d_tag(this->tag_);
1429 // Output_data_dynamic methods.
1431 // Adjust the output section to set the entry size.
1434 Output_data_dynamic::do_adjust_output_section(Output_section* os)
1436 if (parameters->target().get_size() == 32)
1437 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
1438 else if (parameters->target().get_size() == 64)
1439 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1444 // Set the final data size.
1447 Output_data_dynamic::set_final_data_size()
1449 // Add the terminating entry.
1450 this->add_constant(elfcpp::DT_NULL, 0);
1453 if (parameters->target().get_size() == 32)
1454 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
1455 else if (parameters->target().get_size() == 64)
1456 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1459 this->set_data_size(this->entries_.size() * dyn_size);
1462 // Write out the dynamic entries.
1465 Output_data_dynamic::do_write(Output_file* of)
1467 switch (parameters->size_and_endianness())
1469 #ifdef HAVE_TARGET_32_LITTLE
1470 case Parameters::TARGET_32_LITTLE:
1471 this->sized_write<32, false>(of);
1474 #ifdef HAVE_TARGET_32_BIG
1475 case Parameters::TARGET_32_BIG:
1476 this->sized_write<32, true>(of);
1479 #ifdef HAVE_TARGET_64_LITTLE
1480 case Parameters::TARGET_64_LITTLE:
1481 this->sized_write<64, false>(of);
1484 #ifdef HAVE_TARGET_64_BIG
1485 case Parameters::TARGET_64_BIG:
1486 this->sized_write<64, true>(of);
1494 template<int size, bool big_endian>
1496 Output_data_dynamic::sized_write(Output_file* of)
1498 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1500 const off_t offset = this->offset();
1501 const off_t oview_size = this->data_size();
1502 unsigned char* const oview = of->get_output_view(offset, oview_size);
1504 unsigned char* pov = oview;
1505 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1506 p != this->entries_.end();
1509 p->write<size, big_endian>(pov, this->pool_);
1513 gold_assert(pov - oview == oview_size);
1515 of->write_output_view(offset, oview_size, oview);
1517 // We no longer need the dynamic entries.
1518 this->entries_.clear();
1521 // Output_section::Input_section methods.
1523 // Return the data size. For an input section we store the size here.
1524 // For an Output_section_data, we have to ask it for the size.
1527 Output_section::Input_section::data_size() const
1529 if (this->is_input_section())
1530 return this->u1_.data_size;
1532 return this->u2_.posd->data_size();
1535 // Set the address and file offset.
1538 Output_section::Input_section::set_address_and_file_offset(
1541 off_t section_file_offset)
1543 if (this->is_input_section())
1544 this->u2_.object->set_section_offset(this->shndx_,
1545 file_offset - section_file_offset);
1547 this->u2_.posd->set_address_and_file_offset(address, file_offset);
1550 // Reset the address and file offset.
1553 Output_section::Input_section::reset_address_and_file_offset()
1555 if (!this->is_input_section())
1556 this->u2_.posd->reset_address_and_file_offset();
1559 // Finalize the data size.
1562 Output_section::Input_section::finalize_data_size()
1564 if (!this->is_input_section())
1565 this->u2_.posd->finalize_data_size();
1568 // Try to turn an input offset into an output offset. We want to
1569 // return the output offset relative to the start of this
1570 // Input_section in the output section.
1573 Output_section::Input_section::output_offset(
1574 const Relobj* object,
1576 section_offset_type offset,
1577 section_offset_type *poutput) const
1579 if (!this->is_input_section())
1580 return this->u2_.posd->output_offset(object, shndx, offset, poutput);
1583 if (this->shndx_ != shndx || this->u2_.object != object)
1590 // Return whether this is the merge section for the input section
1594 Output_section::Input_section::is_merge_section_for(const Relobj* object,
1595 unsigned int shndx) const
1597 if (this->is_input_section())
1599 return this->u2_.posd->is_merge_section_for(object, shndx);
1602 // Write out the data. We don't have to do anything for an input
1603 // section--they are handled via Object::relocate--but this is where
1604 // we write out the data for an Output_section_data.
1607 Output_section::Input_section::write(Output_file* of)
1609 if (!this->is_input_section())
1610 this->u2_.posd->write(of);
1613 // Write the data to a buffer. As for write(), we don't have to do
1614 // anything for an input section.
1617 Output_section::Input_section::write_to_buffer(unsigned char* buffer)
1619 if (!this->is_input_section())
1620 this->u2_.posd->write_to_buffer(buffer);
1623 // Output_section methods.
1625 // Construct an Output_section. NAME will point into a Stringpool.
1627 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
1628 elfcpp::Elf_Xword flags)
1633 link_section_(NULL),
1635 info_section_(NULL),
1644 first_input_offset_(0),
1646 postprocessing_buffer_(NULL),
1647 needs_symtab_index_(false),
1648 needs_dynsym_index_(false),
1649 should_link_to_symtab_(false),
1650 should_link_to_dynsym_(false),
1651 after_input_sections_(false),
1652 requires_postprocessing_(false),
1653 found_in_sections_clause_(false),
1654 has_load_address_(false),
1655 info_uses_section_index_(false),
1658 // An unallocated section has no address. Forcing this means that
1659 // we don't need special treatment for symbols defined in debug
1661 if ((flags & elfcpp::SHF_ALLOC) == 0)
1662 this->set_address(0);
1665 Output_section::~Output_section()
1669 // Set the entry size.
1672 Output_section::set_entsize(uint64_t v)
1674 if (this->entsize_ == 0)
1677 gold_assert(this->entsize_ == v);
1680 // Add the input section SHNDX, with header SHDR, named SECNAME, in
1681 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
1682 // relocation section which applies to this section, or 0 if none, or
1683 // -1U if more than one. Return the offset of the input section
1684 // within the output section. Return -1 if the input section will
1685 // receive special handling. In the normal case we don't always keep
1686 // track of input sections for an Output_section. Instead, each
1687 // Object keeps track of the Output_section for each of its input
1688 // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
1689 // track of input sections here; this is used when SECTIONS appears in
1692 template<int size, bool big_endian>
1694 Output_section::add_input_section(Sized_relobj<size, big_endian>* object,
1696 const char* secname,
1697 const elfcpp::Shdr<size, big_endian>& shdr,
1698 unsigned int reloc_shndx,
1699 bool have_sections_script)
1701 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
1702 if ((addralign & (addralign - 1)) != 0)
1704 object->error(_("invalid alignment %lu for section \"%s\""),
1705 static_cast<unsigned long>(addralign), secname);
1709 if (addralign > this->addralign_)
1710 this->addralign_ = addralign;
1712 typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
1713 this->flags_ |= (sh_flags
1714 & (elfcpp::SHF_WRITE
1716 | elfcpp::SHF_EXECINSTR));
1718 uint64_t entsize = shdr.get_sh_entsize();
1720 // .debug_str is a mergeable string section, but is not always so
1721 // marked by compilers. Mark manually here so we can optimize.
1722 if (strcmp(secname, ".debug_str") == 0)
1724 sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
1728 // If this is a SHF_MERGE section, we pass all the input sections to
1729 // a Output_data_merge. We don't try to handle relocations for such
1731 if ((sh_flags & elfcpp::SHF_MERGE) != 0
1732 && reloc_shndx == 0)
1734 if (this->add_merge_input_section(object, shndx, sh_flags,
1735 entsize, addralign))
1737 // Tell the relocation routines that they need to call the
1738 // output_offset method to determine the final address.
1743 off_t offset_in_section = this->current_data_size_for_child();
1744 off_t aligned_offset_in_section = align_address(offset_in_section,
1747 if (aligned_offset_in_section > offset_in_section
1748 && !have_sections_script
1749 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
1750 && object->target()->has_code_fill())
1752 // We need to add some fill data. Using fill_list_ when
1753 // possible is an optimization, since we will often have fill
1754 // sections without input sections.
1755 off_t fill_len = aligned_offset_in_section - offset_in_section;
1756 if (this->input_sections_.empty())
1757 this->fills_.push_back(Fill(offset_in_section, fill_len));
1760 // FIXME: When relaxing, the size needs to adjust to
1761 // maintain a constant alignment.
1762 std::string fill_data(object->target()->code_fill(fill_len));
1763 Output_data_const* odc = new Output_data_const(fill_data, 1);
1764 this->input_sections_.push_back(Input_section(odc));
1768 this->set_current_data_size_for_child(aligned_offset_in_section
1769 + shdr.get_sh_size());
1771 // We need to keep track of this section if we are already keeping
1772 // track of sections, or if we are relaxing. FIXME: Add test for
1774 if (have_sections_script || !this->input_sections_.empty())
1775 this->input_sections_.push_back(Input_section(object, shndx,
1779 return aligned_offset_in_section;
1782 // Add arbitrary data to an output section.
1785 Output_section::add_output_section_data(Output_section_data* posd)
1787 Input_section inp(posd);
1788 this->add_output_section_data(&inp);
1790 if (posd->is_data_size_valid())
1792 off_t offset_in_section = this->current_data_size_for_child();
1793 off_t aligned_offset_in_section = align_address(offset_in_section,
1795 this->set_current_data_size_for_child(aligned_offset_in_section
1796 + posd->data_size());
1800 // Add arbitrary data to an output section by Input_section.
1803 Output_section::add_output_section_data(Input_section* inp)
1805 if (this->input_sections_.empty())
1806 this->first_input_offset_ = this->current_data_size_for_child();
1808 this->input_sections_.push_back(*inp);
1810 uint64_t addralign = inp->addralign();
1811 if (addralign > this->addralign_)
1812 this->addralign_ = addralign;
1814 inp->set_output_section(this);
1817 // Add a merge section to an output section.
1820 Output_section::add_output_merge_section(Output_section_data* posd,
1821 bool is_string, uint64_t entsize)
1823 Input_section inp(posd, is_string, entsize);
1824 this->add_output_section_data(&inp);
1827 // Add an input section to a SHF_MERGE section.
1830 Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
1831 uint64_t flags, uint64_t entsize,
1834 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
1836 // We only merge strings if the alignment is not more than the
1837 // character size. This could be handled, but it's unusual.
1838 if (is_string && addralign > entsize)
1841 Input_section_list::iterator p;
1842 for (p = this->input_sections_.begin();
1843 p != this->input_sections_.end();
1845 if (p->is_merge_section(is_string, entsize, addralign))
1847 p->add_input_section(object, shndx);
1851 // We handle the actual constant merging in Output_merge_data or
1852 // Output_merge_string_data.
1853 Output_section_data* posd;
1855 posd = new Output_merge_data(entsize, addralign);
1861 posd = new Output_merge_string<char>(addralign);
1864 posd = new Output_merge_string<uint16_t>(addralign);
1867 posd = new Output_merge_string<uint32_t>(addralign);
1874 this->add_output_merge_section(posd, is_string, entsize);
1875 posd->add_input_section(object, shndx);
1880 // Given an address OFFSET relative to the start of input section
1881 // SHNDX in OBJECT, return whether this address is being included in
1882 // the final link. This should only be called if SHNDX in OBJECT has
1883 // a special mapping.
1886 Output_section::is_input_address_mapped(const Relobj* object,
1890 gold_assert(object->is_section_specially_mapped(shndx));
1892 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1893 p != this->input_sections_.end();
1896 section_offset_type output_offset;
1897 if (p->output_offset(object, shndx, offset, &output_offset))
1898 return output_offset != -1;
1901 // By default we assume that the address is mapped. This should
1902 // only be called after we have passed all sections to Layout. At
1903 // that point we should know what we are discarding.
1907 // Given an address OFFSET relative to the start of input section
1908 // SHNDX in object OBJECT, return the output offset relative to the
1909 // start of the input section in the output section. This should only
1910 // be called if SHNDX in OBJECT has a special mapping.
1913 Output_section::output_offset(const Relobj* object, unsigned int shndx,
1914 section_offset_type offset) const
1916 gold_assert(object->is_section_specially_mapped(shndx));
1917 // This can only be called meaningfully when layout is complete.
1918 gold_assert(Output_data::is_layout_complete());
1920 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1921 p != this->input_sections_.end();
1924 section_offset_type output_offset;
1925 if (p->output_offset(object, shndx, offset, &output_offset))
1926 return output_offset;
1931 // Return the output virtual address of OFFSET relative to the start
1932 // of input section SHNDX in object OBJECT.
1935 Output_section::output_address(const Relobj* object, unsigned int shndx,
1938 gold_assert(object->is_section_specially_mapped(shndx));
1940 uint64_t addr = this->address() + this->first_input_offset_;
1941 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1942 p != this->input_sections_.end();
1945 addr = align_address(addr, p->addralign());
1946 section_offset_type output_offset;
1947 if (p->output_offset(object, shndx, offset, &output_offset))
1949 if (output_offset == -1)
1951 return addr + output_offset;
1953 addr += p->data_size();
1956 // If we get here, it means that we don't know the mapping for this
1957 // input section. This might happen in principle if
1958 // add_input_section were called before add_output_section_data.
1959 // But it should never actually happen.
1964 // Return the output address of the start of the merged section for
1965 // input section SHNDX in object OBJECT.
1968 Output_section::starting_output_address(const Relobj* object,
1969 unsigned int shndx) const
1971 gold_assert(object->is_section_specially_mapped(shndx));
1973 uint64_t addr = this->address() + this->first_input_offset_;
1974 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1975 p != this->input_sections_.end();
1978 addr = align_address(addr, p->addralign());
1980 // It would be nice if we could use the existing output_offset
1981 // method to get the output offset of input offset 0.
1982 // Unfortunately we don't know for sure that input offset 0 is
1984 if (p->is_merge_section_for(object, shndx))
1987 addr += p->data_size();
1992 // Set the data size of an Output_section. This is where we handle
1993 // setting the addresses of any Output_section_data objects.
1996 Output_section::set_final_data_size()
1998 if (this->input_sections_.empty())
2000 this->set_data_size(this->current_data_size_for_child());
2004 uint64_t address = this->address();
2005 off_t startoff = this->offset();
2006 off_t off = startoff + this->first_input_offset_;
2007 for (Input_section_list::iterator p = this->input_sections_.begin();
2008 p != this->input_sections_.end();
2011 off = align_address(off, p->addralign());
2012 p->set_address_and_file_offset(address + (off - startoff), off,
2014 off += p->data_size();
2017 this->set_data_size(off - startoff);
2020 // Reset the address and file offset.
2023 Output_section::do_reset_address_and_file_offset()
2025 for (Input_section_list::iterator p = this->input_sections_.begin();
2026 p != this->input_sections_.end();
2028 p->reset_address_and_file_offset();
2031 // Set the TLS offset. Called only for SHT_TLS sections.
2034 Output_section::do_set_tls_offset(uint64_t tls_base)
2036 this->tls_offset_ = this->address() - tls_base;
2039 // Write the section header to *OSHDR.
2041 template<int size, bool big_endian>
2043 Output_section::write_header(const Layout* layout,
2044 const Stringpool* secnamepool,
2045 elfcpp::Shdr_write<size, big_endian>* oshdr) const
2047 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
2048 oshdr->put_sh_type(this->type_);
2050 elfcpp::Elf_Xword flags = this->flags_;
2051 if (this->info_section_ != NULL && this->info_uses_section_index_)
2052 flags |= elfcpp::SHF_INFO_LINK;
2053 oshdr->put_sh_flags(flags);
2055 oshdr->put_sh_addr(this->address());
2056 oshdr->put_sh_offset(this->offset());
2057 oshdr->put_sh_size(this->data_size());
2058 if (this->link_section_ != NULL)
2059 oshdr->put_sh_link(this->link_section_->out_shndx());
2060 else if (this->should_link_to_symtab_)
2061 oshdr->put_sh_link(layout->symtab_section()->out_shndx());
2062 else if (this->should_link_to_dynsym_)
2063 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
2065 oshdr->put_sh_link(this->link_);
2067 elfcpp::Elf_Word info;
2068 if (this->info_section_ != NULL)
2070 if (this->info_uses_section_index_)
2071 info = this->info_section_->out_shndx();
2073 info = this->info_section_->symtab_index();
2075 else if (this->info_symndx_ != NULL)
2076 info = this->info_symndx_->symtab_index();
2079 oshdr->put_sh_info(info);
2081 oshdr->put_sh_addralign(this->addralign_);
2082 oshdr->put_sh_entsize(this->entsize_);
2085 // Write out the data. For input sections the data is written out by
2086 // Object::relocate, but we have to handle Output_section_data objects
2090 Output_section::do_write(Output_file* of)
2092 gold_assert(!this->requires_postprocessing());
2094 off_t output_section_file_offset = this->offset();
2095 for (Fill_list::iterator p = this->fills_.begin();
2096 p != this->fills_.end();
2099 std::string fill_data(parameters->target().code_fill(p->length()));
2100 of->write(output_section_file_offset + p->section_offset(),
2101 fill_data.data(), fill_data.size());
2104 for (Input_section_list::iterator p = this->input_sections_.begin();
2105 p != this->input_sections_.end();
2110 // If a section requires postprocessing, create the buffer to use.
2113 Output_section::create_postprocessing_buffer()
2115 gold_assert(this->requires_postprocessing());
2117 if (this->postprocessing_buffer_ != NULL)
2120 if (!this->input_sections_.empty())
2122 off_t off = this->first_input_offset_;
2123 for (Input_section_list::iterator p = this->input_sections_.begin();
2124 p != this->input_sections_.end();
2127 off = align_address(off, p->addralign());
2128 p->finalize_data_size();
2129 off += p->data_size();
2131 this->set_current_data_size_for_child(off);
2134 off_t buffer_size = this->current_data_size_for_child();
2135 this->postprocessing_buffer_ = new unsigned char[buffer_size];
2138 // Write all the data of an Output_section into the postprocessing
2139 // buffer. This is used for sections which require postprocessing,
2140 // such as compression. Input sections are handled by
2141 // Object::Relocate.
2144 Output_section::write_to_postprocessing_buffer()
2146 gold_assert(this->requires_postprocessing());
2148 unsigned char* buffer = this->postprocessing_buffer();
2149 for (Fill_list::iterator p = this->fills_.begin();
2150 p != this->fills_.end();
2153 std::string fill_data(parameters->target().code_fill(p->length()));
2154 memcpy(buffer + p->section_offset(), fill_data.data(),
2158 off_t off = this->first_input_offset_;
2159 for (Input_section_list::iterator p = this->input_sections_.begin();
2160 p != this->input_sections_.end();
2163 off = align_address(off, p->addralign());
2164 p->write_to_buffer(buffer + off);
2165 off += p->data_size();
2169 // Get the input sections for linker script processing. We leave
2170 // behind the Output_section_data entries. Note that this may be
2171 // slightly incorrect for merge sections. We will leave them behind,
2172 // but it is possible that the script says that they should follow
2173 // some other input sections, as in:
2174 // .rodata { *(.rodata) *(.rodata.cst*) }
2175 // For that matter, we don't handle this correctly:
2176 // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
2177 // With luck this will never matter.
2180 Output_section::get_input_sections(
2182 const std::string& fill,
2183 std::list<std::pair<Relobj*, unsigned int> >* input_sections)
2185 uint64_t orig_address = address;
2187 address = align_address(address, this->addralign());
2189 Input_section_list remaining;
2190 for (Input_section_list::iterator p = this->input_sections_.begin();
2191 p != this->input_sections_.end();
2194 if (p->is_input_section())
2195 input_sections->push_back(std::make_pair(p->relobj(), p->shndx()));
2198 uint64_t aligned_address = align_address(address, p->addralign());
2199 if (aligned_address != address && !fill.empty())
2201 section_size_type length =
2202 convert_to_section_size_type(aligned_address - address);
2203 std::string this_fill;
2204 this_fill.reserve(length);
2205 while (this_fill.length() + fill.length() <= length)
2207 if (this_fill.length() < length)
2208 this_fill.append(fill, 0, length - this_fill.length());
2210 Output_section_data* posd = new Output_data_const(this_fill, 0);
2211 remaining.push_back(Input_section(posd));
2213 address = aligned_address;
2215 remaining.push_back(*p);
2217 p->finalize_data_size();
2218 address += p->data_size();
2222 this->input_sections_.swap(remaining);
2223 this->first_input_offset_ = 0;
2225 uint64_t data_size = address - orig_address;
2226 this->set_current_data_size_for_child(data_size);
2230 // Add an input section from a script.
2233 Output_section::add_input_section_for_script(Relobj* object,
2238 if (addralign > this->addralign_)
2239 this->addralign_ = addralign;
2241 off_t offset_in_section = this->current_data_size_for_child();
2242 off_t aligned_offset_in_section = align_address(offset_in_section,
2245 this->set_current_data_size_for_child(aligned_offset_in_section
2248 this->input_sections_.push_back(Input_section(object, shndx,
2249 data_size, addralign));
2252 // Print stats for merge sections to stderr.
2255 Output_section::print_merge_stats()
2257 Input_section_list::iterator p;
2258 for (p = this->input_sections_.begin();
2259 p != this->input_sections_.end();
2261 p->print_merge_stats(this->name_);
2264 // Output segment methods.
2266 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
2278 is_max_align_known_(false),
2279 are_addresses_set_(false)
2283 // Add an Output_section to an Output_segment.
2286 Output_segment::add_output_section(Output_section* os,
2287 elfcpp::Elf_Word seg_flags,
2290 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
2291 gold_assert(!this->is_max_align_known_);
2293 // Update the segment flags.
2294 this->flags_ |= seg_flags;
2296 Output_segment::Output_data_list* pdl;
2297 if (os->type() == elfcpp::SHT_NOBITS)
2298 pdl = &this->output_bss_;
2300 pdl = &this->output_data_;
2302 // So that PT_NOTE segments will work correctly, we need to ensure
2303 // that all SHT_NOTE sections are adjacent. This will normally
2304 // happen automatically, because all the SHT_NOTE input sections
2305 // will wind up in the same output section. However, it is possible
2306 // for multiple SHT_NOTE input sections to have different section
2307 // flags, and thus be in different output sections, but for the
2308 // different section flags to map into the same segment flags and
2309 // thus the same output segment.
2311 // Note that while there may be many input sections in an output
2312 // section, there are normally only a few output sections in an
2313 // output segment. This loop is expected to be fast.
2315 if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
2317 Output_segment::Output_data_list::iterator p = pdl->end();
2321 if ((*p)->is_section_type(elfcpp::SHT_NOTE))
2323 // We don't worry about the FRONT parameter.
2329 while (p != pdl->begin());
2332 // Similarly, so that PT_TLS segments will work, we need to group
2333 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
2334 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
2335 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
2336 // correctly. SHF_TLS sections get added to both a PT_LOAD segment
2337 // and the PT_TLS segment -- we do this grouping only for the
2339 if (this->type_ != elfcpp::PT_TLS
2340 && (os->flags() & elfcpp::SHF_TLS) != 0
2341 && !this->output_data_.empty())
2343 pdl = &this->output_data_;
2344 bool nobits = os->type() == elfcpp::SHT_NOBITS;
2345 bool sawtls = false;
2346 Output_segment::Output_data_list::iterator p = pdl->end();
2351 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
2354 // Put a NOBITS section after the first TLS section.
2355 // But a PROGBITS section after the first TLS/PROGBITS
2357 insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS);
2361 // If we've gone past the TLS sections, but we've seen a
2362 // TLS section, then we need to insert this section now.
2368 // We don't worry about the FRONT parameter.
2374 while (p != pdl->begin());
2376 // There are no TLS sections yet; put this one at the requested
2377 // location in the section list.
2381 pdl->push_front(os);
2386 // Remove an Output_section from this segment. It is an error if it
2390 Output_segment::remove_output_section(Output_section* os)
2392 // We only need this for SHT_PROGBITS.
2393 gold_assert(os->type() == elfcpp::SHT_PROGBITS);
2394 for (Output_data_list::iterator p = this->output_data_.begin();
2395 p != this->output_data_.end();
2400 this->output_data_.erase(p);
2407 // Add an Output_data (which is not an Output_section) to the start of
2411 Output_segment::add_initial_output_data(Output_data* od)
2413 gold_assert(!this->is_max_align_known_);
2414 this->output_data_.push_front(od);
2417 // Return the maximum alignment of the Output_data in Output_segment.
2420 Output_segment::maximum_alignment()
2422 if (!this->is_max_align_known_)
2426 addralign = Output_segment::maximum_alignment_list(&this->output_data_);
2427 if (addralign > this->max_align_)
2428 this->max_align_ = addralign;
2430 addralign = Output_segment::maximum_alignment_list(&this->output_bss_);
2431 if (addralign > this->max_align_)
2432 this->max_align_ = addralign;
2434 this->is_max_align_known_ = true;
2437 return this->max_align_;
2440 // Return the maximum alignment of a list of Output_data.
2443 Output_segment::maximum_alignment_list(const Output_data_list* pdl)
2446 for (Output_data_list::const_iterator p = pdl->begin();
2450 uint64_t addralign = (*p)->addralign();
2451 if (addralign > ret)
2457 // Return the number of dynamic relocs applied to this segment.
2460 Output_segment::dynamic_reloc_count() const
2462 return (this->dynamic_reloc_count_list(&this->output_data_)
2463 + this->dynamic_reloc_count_list(&this->output_bss_));
2466 // Return the number of dynamic relocs applied to an Output_data_list.
2469 Output_segment::dynamic_reloc_count_list(const Output_data_list* pdl) const
2471 unsigned int count = 0;
2472 for (Output_data_list::const_iterator p = pdl->begin();
2475 count += (*p)->dynamic_reloc_count();
2479 // Set the section addresses for an Output_segment. If RESET is true,
2480 // reset the addresses first. ADDR is the address and *POFF is the
2481 // file offset. Set the section indexes starting with *PSHNDX.
2482 // Return the address of the immediately following segment. Update
2483 // *POFF and *PSHNDX.
2486 Output_segment::set_section_addresses(const Layout* layout, bool reset,
2487 uint64_t addr, off_t* poff,
2488 unsigned int* pshndx)
2490 gold_assert(this->type_ == elfcpp::PT_LOAD);
2492 if (!reset && this->are_addresses_set_)
2494 gold_assert(this->paddr_ == addr);
2495 addr = this->vaddr_;
2499 this->vaddr_ = addr;
2500 this->paddr_ = addr;
2501 this->are_addresses_set_ = true;
2504 bool in_tls = false;
2506 off_t orig_off = *poff;
2507 this->offset_ = orig_off;
2509 addr = this->set_section_list_addresses(layout, reset, &this->output_data_,
2510 addr, poff, pshndx, &in_tls);
2511 this->filesz_ = *poff - orig_off;
2515 uint64_t ret = this->set_section_list_addresses(layout, reset,
2520 // If the last section was a TLS section, align upward to the
2521 // alignment of the TLS segment, so that the overall size of the TLS
2522 // segment is aligned.
2525 uint64_t segment_align = layout->tls_segment()->maximum_alignment();
2526 *poff = align_address(*poff, segment_align);
2529 this->memsz_ = *poff - orig_off;
2531 // Ignore the file offset adjustments made by the BSS Output_data
2538 // Set the addresses and file offsets in a list of Output_data
2542 Output_segment::set_section_list_addresses(const Layout* layout, bool reset,
2543 Output_data_list* pdl,
2544 uint64_t addr, off_t* poff,
2545 unsigned int* pshndx,
2548 off_t startoff = *poff;
2550 off_t off = startoff;
2551 for (Output_data_list::iterator p = pdl->begin();
2556 (*p)->reset_address_and_file_offset();
2558 // When using a linker script the section will most likely
2559 // already have an address.
2560 if (!(*p)->is_address_valid())
2562 uint64_t align = (*p)->addralign();
2564 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
2566 // Give the first TLS section the alignment of the
2567 // entire TLS segment. Otherwise the TLS segment as a
2568 // whole may be misaligned.
2571 Output_segment* tls_segment = layout->tls_segment();
2572 gold_assert(tls_segment != NULL);
2573 uint64_t segment_align = tls_segment->maximum_alignment();
2574 gold_assert(segment_align >= align);
2575 align = segment_align;
2582 // If this is the first section after the TLS segment,
2583 // align it to at least the alignment of the TLS
2584 // segment, so that the size of the overall TLS segment
2588 uint64_t segment_align =
2589 layout->tls_segment()->maximum_alignment();
2590 if (segment_align > align)
2591 align = segment_align;
2597 off = align_address(off, align);
2598 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
2602 // The script may have inserted a skip forward, but it
2603 // better not have moved backward.
2604 gold_assert((*p)->address() >= addr + (off - startoff));
2605 off += (*p)->address() - (addr + (off - startoff));
2606 (*p)->set_file_offset(off);
2607 (*p)->finalize_data_size();
2610 // We want to ignore the size of a SHF_TLS or SHT_NOBITS
2611 // section. Such a section does not affect the size of a
2613 if (!(*p)->is_section_flag_set(elfcpp::SHF_TLS)
2614 || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
2615 off += (*p)->data_size();
2617 if ((*p)->is_section())
2619 (*p)->set_out_shndx(*pshndx);
2625 return addr + (off - startoff);
2628 // For a non-PT_LOAD segment, set the offset from the sections, if
2632 Output_segment::set_offset()
2634 gold_assert(this->type_ != elfcpp::PT_LOAD);
2636 gold_assert(!this->are_addresses_set_);
2638 if (this->output_data_.empty() && this->output_bss_.empty())
2642 this->are_addresses_set_ = true;
2644 this->min_p_align_ = 0;
2650 const Output_data* first;
2651 if (this->output_data_.empty())
2652 first = this->output_bss_.front();
2654 first = this->output_data_.front();
2655 this->vaddr_ = first->address();
2656 this->paddr_ = (first->has_load_address()
2657 ? first->load_address()
2659 this->are_addresses_set_ = true;
2660 this->offset_ = first->offset();
2662 if (this->output_data_.empty())
2666 const Output_data* last_data = this->output_data_.back();
2667 this->filesz_ = (last_data->address()
2668 + last_data->data_size()
2672 const Output_data* last;
2673 if (this->output_bss_.empty())
2674 last = this->output_data_.back();
2676 last = this->output_bss_.back();
2677 this->memsz_ = (last->address()
2681 // If this is a TLS segment, align the memory size. The code in
2682 // set_section_list ensures that the section after the TLS segment
2683 // is aligned to give us room.
2684 if (this->type_ == elfcpp::PT_TLS)
2686 uint64_t segment_align = this->maximum_alignment();
2687 gold_assert(this->vaddr_ == align_address(this->vaddr_, segment_align));
2688 this->memsz_ = align_address(this->memsz_, segment_align);
2692 // Set the TLS offsets of the sections in the PT_TLS segment.
2695 Output_segment::set_tls_offsets()
2697 gold_assert(this->type_ == elfcpp::PT_TLS);
2699 for (Output_data_list::iterator p = this->output_data_.begin();
2700 p != this->output_data_.end();
2702 (*p)->set_tls_offset(this->vaddr_);
2704 for (Output_data_list::iterator p = this->output_bss_.begin();
2705 p != this->output_bss_.end();
2707 (*p)->set_tls_offset(this->vaddr_);
2710 // Return the address of the first section.
2713 Output_segment::first_section_load_address() const
2715 for (Output_data_list::const_iterator p = this->output_data_.begin();
2716 p != this->output_data_.end();
2718 if ((*p)->is_section())
2719 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
2721 for (Output_data_list::const_iterator p = this->output_bss_.begin();
2722 p != this->output_bss_.end();
2724 if ((*p)->is_section())
2725 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
2730 // Return the number of Output_sections in an Output_segment.
2733 Output_segment::output_section_count() const
2735 return (this->output_section_count_list(&this->output_data_)
2736 + this->output_section_count_list(&this->output_bss_));
2739 // Return the number of Output_sections in an Output_data_list.
2742 Output_segment::output_section_count_list(const Output_data_list* pdl) const
2744 unsigned int count = 0;
2745 for (Output_data_list::const_iterator p = pdl->begin();
2749 if ((*p)->is_section())
2755 // Return the section attached to the list segment with the lowest
2756 // load address. This is used when handling a PHDRS clause in a
2760 Output_segment::section_with_lowest_load_address() const
2762 Output_section* found = NULL;
2763 uint64_t found_lma = 0;
2764 this->lowest_load_address_in_list(&this->output_data_, &found, &found_lma);
2766 Output_section* found_data = found;
2767 this->lowest_load_address_in_list(&this->output_bss_, &found, &found_lma);
2768 if (found != found_data && found_data != NULL)
2770 gold_error(_("nobits section %s may not precede progbits section %s "
2772 found->name(), found_data->name());
2779 // Look through a list for a section with a lower load address.
2782 Output_segment::lowest_load_address_in_list(const Output_data_list* pdl,
2783 Output_section** found,
2784 uint64_t* found_lma) const
2786 for (Output_data_list::const_iterator p = pdl->begin();
2790 if (!(*p)->is_section())
2792 Output_section* os = static_cast<Output_section*>(*p);
2793 uint64_t lma = (os->has_load_address()
2794 ? os->load_address()
2796 if (*found == NULL || lma < *found_lma)
2804 // Write the segment data into *OPHDR.
2806 template<int size, bool big_endian>
2808 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
2810 ophdr->put_p_type(this->type_);
2811 ophdr->put_p_offset(this->offset_);
2812 ophdr->put_p_vaddr(this->vaddr_);
2813 ophdr->put_p_paddr(this->paddr_);
2814 ophdr->put_p_filesz(this->filesz_);
2815 ophdr->put_p_memsz(this->memsz_);
2816 ophdr->put_p_flags(this->flags_);
2817 ophdr->put_p_align(std::max(this->min_p_align_, this->maximum_alignment()));
2820 // Write the section headers into V.
2822 template<int size, bool big_endian>
2824 Output_segment::write_section_headers(const Layout* layout,
2825 const Stringpool* secnamepool,
2827 unsigned int *pshndx) const
2829 // Every section that is attached to a segment must be attached to a
2830 // PT_LOAD segment, so we only write out section headers for PT_LOAD
2832 if (this->type_ != elfcpp::PT_LOAD)
2835 v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
2836 &this->output_data_,
2838 v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
2844 template<int size, bool big_endian>
2846 Output_segment::write_section_headers_list(const Layout* layout,
2847 const Stringpool* secnamepool,
2848 const Output_data_list* pdl,
2850 unsigned int* pshndx) const
2852 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2853 for (Output_data_list::const_iterator p = pdl->begin();
2857 if ((*p)->is_section())
2859 const Output_section* ps = static_cast<const Output_section*>(*p);
2860 gold_assert(*pshndx == ps->out_shndx());
2861 elfcpp::Shdr_write<size, big_endian> oshdr(v);
2862 ps->write_header(layout, secnamepool, &oshdr);
2870 // Output_file methods.
2872 Output_file::Output_file(const char* name)
2877 map_is_anonymous_(false),
2878 is_temporary_(false)
2882 // Open the output file.
2885 Output_file::open(off_t file_size)
2887 this->file_size_ = file_size;
2889 // Unlink the file first; otherwise the open() may fail if the file
2890 // is busy (e.g. it's an executable that's currently being executed).
2892 // However, the linker may be part of a system where a zero-length
2893 // file is created for it to write to, with tight permissions (gcc
2894 // 2.95 did something like this). Unlinking the file would work
2895 // around those permission controls, so we only unlink if the file
2896 // has a non-zero size. We also unlink only regular files to avoid
2897 // trouble with directories/etc.
2899 // If we fail, continue; this command is merely a best-effort attempt
2900 // to improve the odds for open().
2902 // We let the name "-" mean "stdout"
2903 if (!this->is_temporary_)
2905 if (strcmp(this->name_, "-") == 0)
2906 this->o_ = STDOUT_FILENO;
2910 if (::stat(this->name_, &s) == 0 && s.st_size != 0)
2911 unlink_if_ordinary(this->name_);
2913 int mode = parameters->options().relocatable() ? 0666 : 0777;
2914 int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode);
2916 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
2924 // Resize the output file.
2927 Output_file::resize(off_t file_size)
2929 // If the mmap is mapping an anonymous memory buffer, this is easy:
2930 // just mremap to the new size. If it's mapping to a file, we want
2931 // to unmap to flush to the file, then remap after growing the file.
2932 if (this->map_is_anonymous_)
2934 void* base = ::mremap(this->base_, this->file_size_, file_size,
2936 if (base == MAP_FAILED)
2937 gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
2938 this->base_ = static_cast<unsigned char*>(base);
2939 this->file_size_ = file_size;
2944 this->file_size_ = file_size;
2949 // Map the file into memory.
2954 const int o = this->o_;
2956 // If the output file is not a regular file, don't try to mmap it;
2957 // instead, we'll mmap a block of memory (an anonymous buffer), and
2958 // then later write the buffer to the file.
2960 struct stat statbuf;
2961 if (o == STDOUT_FILENO || o == STDERR_FILENO
2962 || ::fstat(o, &statbuf) != 0
2963 || !S_ISREG(statbuf.st_mode)
2964 || this->is_temporary_)
2966 this->map_is_anonymous_ = true;
2967 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
2968 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
2972 // Write out one byte to make the file the right size.
2973 if (::lseek(o, this->file_size_ - 1, SEEK_SET) < 0)
2974 gold_fatal(_("%s: lseek: %s"), this->name_, strerror(errno));
2976 if (::write(o, &b, 1) != 1)
2977 gold_fatal(_("%s: write: %s"), this->name_, strerror(errno));
2979 // Map the file into memory.
2980 this->map_is_anonymous_ = false;
2981 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
2984 if (base == MAP_FAILED)
2985 gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
2986 this->base_ = static_cast<unsigned char*>(base);
2989 // Unmap the file from memory.
2992 Output_file::unmap()
2994 if (::munmap(this->base_, this->file_size_) < 0)
2995 gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
2999 // Close the output file.
3002 Output_file::close()
3004 // If the map isn't file-backed, we need to write it now.
3005 if (this->map_is_anonymous_ && !this->is_temporary_)
3007 size_t bytes_to_write = this->file_size_;
3008 while (bytes_to_write > 0)
3010 ssize_t bytes_written = ::write(this->o_, this->base_, bytes_to_write);
3011 if (bytes_written == 0)
3012 gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
3013 else if (bytes_written < 0)
3014 gold_error(_("%s: write: %s"), this->name_, strerror(errno));
3016 bytes_to_write -= bytes_written;
3021 // We don't close stdout or stderr
3022 if (this->o_ != STDOUT_FILENO
3023 && this->o_ != STDERR_FILENO
3024 && !this->is_temporary_)
3025 if (::close(this->o_) < 0)
3026 gold_error(_("%s: close: %s"), this->name_, strerror(errno));
3030 // Instantiate the templates we need. We could use the configure
3031 // script to restrict this to only the ones for implemented targets.
3033 #ifdef HAVE_TARGET_32_LITTLE
3036 Output_section::add_input_section<32, false>(
3037 Sized_relobj<32, false>* object,
3039 const char* secname,
3040 const elfcpp::Shdr<32, false>& shdr,
3041 unsigned int reloc_shndx,
3042 bool have_sections_script);
3045 #ifdef HAVE_TARGET_32_BIG
3048 Output_section::add_input_section<32, true>(
3049 Sized_relobj<32, true>* object,
3051 const char* secname,
3052 const elfcpp::Shdr<32, true>& shdr,
3053 unsigned int reloc_shndx,
3054 bool have_sections_script);
3057 #ifdef HAVE_TARGET_64_LITTLE
3060 Output_section::add_input_section<64, false>(
3061 Sized_relobj<64, false>* object,
3063 const char* secname,
3064 const elfcpp::Shdr<64, false>& shdr,
3065 unsigned int reloc_shndx,
3066 bool have_sections_script);
3069 #ifdef HAVE_TARGET_64_BIG
3072 Output_section::add_input_section<64, true>(
3073 Sized_relobj<64, true>* object,
3075 const char* secname,
3076 const elfcpp::Shdr<64, true>& shdr,
3077 unsigned int reloc_shndx,
3078 bool have_sections_script);
3081 #ifdef HAVE_TARGET_32_LITTLE
3083 class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
3086 #ifdef HAVE_TARGET_32_BIG
3088 class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
3091 #ifdef HAVE_TARGET_64_LITTLE
3093 class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
3096 #ifdef HAVE_TARGET_64_BIG
3098 class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
3101 #ifdef HAVE_TARGET_32_LITTLE
3103 class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
3106 #ifdef HAVE_TARGET_32_BIG
3108 class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
3111 #ifdef HAVE_TARGET_64_LITTLE
3113 class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
3116 #ifdef HAVE_TARGET_64_BIG
3118 class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
3121 #ifdef HAVE_TARGET_32_LITTLE
3123 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
3126 #ifdef HAVE_TARGET_32_BIG
3128 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
3131 #ifdef HAVE_TARGET_64_LITTLE
3133 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
3136 #ifdef HAVE_TARGET_64_BIG
3138 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
3141 #ifdef HAVE_TARGET_32_LITTLE
3143 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
3146 #ifdef HAVE_TARGET_32_BIG
3148 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
3151 #ifdef HAVE_TARGET_64_LITTLE
3153 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
3156 #ifdef HAVE_TARGET_64_BIG
3158 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
3161 #ifdef HAVE_TARGET_32_LITTLE
3163 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, false>;
3166 #ifdef HAVE_TARGET_32_BIG
3168 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, true>;
3171 #ifdef HAVE_TARGET_64_LITTLE
3173 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, false>;
3176 #ifdef HAVE_TARGET_64_BIG
3178 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, true>;
3181 #ifdef HAVE_TARGET_32_LITTLE
3183 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, false>;
3186 #ifdef HAVE_TARGET_32_BIG
3188 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, true>;
3191 #ifdef HAVE_TARGET_64_LITTLE
3193 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, false>;
3196 #ifdef HAVE_TARGET_64_BIG
3198 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, true>;
3201 #ifdef HAVE_TARGET_32_LITTLE
3203 class Output_data_group<32, false>;
3206 #ifdef HAVE_TARGET_32_BIG
3208 class Output_data_group<32, true>;
3211 #ifdef HAVE_TARGET_64_LITTLE
3213 class Output_data_group<64, false>;
3216 #ifdef HAVE_TARGET_64_BIG
3218 class Output_data_group<64, true>;
3221 #ifdef HAVE_TARGET_32_LITTLE
3223 class Output_data_got<32, false>;
3226 #ifdef HAVE_TARGET_32_BIG
3228 class Output_data_got<32, true>;
3231 #ifdef HAVE_TARGET_64_LITTLE
3233 class Output_data_got<64, false>;
3236 #ifdef HAVE_TARGET_64_BIG
3238 class Output_data_got<64, true>;
3241 } // End namespace gold.