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)
300 segment_header_(osh),
301 section_header_(NULL),
304 const int size = parameters->get_size();
307 ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
309 ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
313 this->set_data_size(ehdr_size);
316 // Set the section table information for a file header.
319 Output_file_header::set_section_info(const Output_section_headers* shdrs,
320 const Output_section* shstrtab)
322 this->section_header_ = shdrs;
323 this->shstrtab_ = shstrtab;
326 // Write out the file header.
329 Output_file_header::do_write(Output_file* of)
331 gold_assert(this->offset() == 0);
333 if (parameters->get_size() == 32)
335 if (parameters->is_big_endian())
337 #ifdef HAVE_TARGET_32_BIG
338 this->do_sized_write<32, true>(of);
345 #ifdef HAVE_TARGET_32_LITTLE
346 this->do_sized_write<32, false>(of);
352 else if (parameters->get_size() == 64)
354 if (parameters->is_big_endian())
356 #ifdef HAVE_TARGET_64_BIG
357 this->do_sized_write<64, true>(of);
364 #ifdef HAVE_TARGET_64_LITTLE
365 this->do_sized_write<64, false>(of);
375 // Write out the file header with appropriate size and endianess.
377 template<int size, bool big_endian>
379 Output_file_header::do_sized_write(Output_file* of)
381 gold_assert(this->offset() == 0);
383 int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
384 unsigned char* view = of->get_output_view(0, ehdr_size);
385 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
387 unsigned char e_ident[elfcpp::EI_NIDENT];
388 memset(e_ident, 0, elfcpp::EI_NIDENT);
389 e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
390 e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
391 e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
392 e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
394 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
396 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
399 e_ident[elfcpp::EI_DATA] = (big_endian
400 ? elfcpp::ELFDATA2MSB
401 : elfcpp::ELFDATA2LSB);
402 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
403 // FIXME: Some targets may need to set EI_OSABI and EI_ABIVERSION.
404 oehdr.put_e_ident(e_ident);
407 if (parameters->output_is_object())
408 e_type = elfcpp::ET_REL;
409 else if (parameters->output_is_shared())
410 e_type = elfcpp::ET_DYN;
412 e_type = elfcpp::ET_EXEC;
413 oehdr.put_e_type(e_type);
415 oehdr.put_e_machine(this->target_->machine_code());
416 oehdr.put_e_version(elfcpp::EV_CURRENT);
418 // FIXME: Need to support -e, and target specific entry symbol.
419 Symbol* sym = this->symtab_->lookup("_start");
420 typename Sized_symbol<size>::Value_type v;
425 Sized_symbol<size>* ssym;
426 ssym = this->symtab_->get_sized_symbol SELECT_SIZE_NAME(size) (
427 sym SELECT_SIZE(size));
430 oehdr.put_e_entry(v);
432 oehdr.put_e_phoff(this->segment_header_->offset());
433 oehdr.put_e_shoff(this->section_header_->offset());
435 // FIXME: The target needs to set the flags.
436 oehdr.put_e_flags(0);
438 oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
439 oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
440 oehdr.put_e_phnum(this->segment_header_->data_size()
441 / elfcpp::Elf_sizes<size>::phdr_size);
442 oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
443 oehdr.put_e_shnum(this->section_header_->data_size()
444 / elfcpp::Elf_sizes<size>::shdr_size);
445 oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
447 of->write_output_view(0, ehdr_size, view);
450 // Output_data_const methods.
453 Output_data_const::do_write(Output_file* of)
455 of->write(this->offset(), this->data_.data(), this->data_.size());
458 // Output_data_const_buffer methods.
461 Output_data_const_buffer::do_write(Output_file* of)
463 of->write(this->offset(), this->p_, this->data_size());
466 // Output_section_data methods.
468 // Record the output section, and set the entry size and such.
471 Output_section_data::set_output_section(Output_section* os)
473 gold_assert(this->output_section_ == NULL);
474 this->output_section_ = os;
475 this->do_adjust_output_section(os);
478 // Return the section index of the output section.
481 Output_section_data::do_out_shndx() const
483 gold_assert(this->output_section_ != NULL);
484 return this->output_section_->out_shndx();
487 // Output_data_strtab methods.
489 // Set the final data size.
492 Output_data_strtab::set_final_data_size()
494 this->strtab_->set_string_offsets();
495 this->set_data_size(this->strtab_->get_strtab_size());
498 // Write out a string table.
501 Output_data_strtab::do_write(Output_file* of)
503 this->strtab_->write(of, this->offset());
506 // Output_reloc methods.
508 // A reloc against a global symbol.
510 template<bool dynamic, int size, bool big_endian>
511 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
517 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
518 is_relative_(is_relative), shndx_(INVALID_CODE)
520 this->u1_.gsym = gsym;
522 if (dynamic && !is_relative)
523 gsym->set_needs_dynsym_entry();
526 template<bool dynamic, int size, bool big_endian>
527 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
534 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
535 is_relative_(is_relative), shndx_(shndx)
537 gold_assert(shndx != INVALID_CODE);
538 this->u1_.gsym = gsym;
539 this->u2_.relobj = relobj;
540 if (dynamic && !is_relative)
541 gsym->set_needs_dynsym_entry();
544 // A reloc against a local symbol.
546 template<bool dynamic, int size, bool big_endian>
547 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
548 Sized_relobj<size, big_endian>* relobj,
549 unsigned int local_sym_index,
554 : address_(address), local_sym_index_(local_sym_index), type_(type),
555 is_relative_(is_relative), shndx_(INVALID_CODE)
557 gold_assert(local_sym_index != GSYM_CODE
558 && local_sym_index != INVALID_CODE);
559 this->u1_.relobj = relobj;
561 if (dynamic && !is_relative)
562 relobj->set_needs_output_dynsym_entry(local_sym_index);
565 template<bool dynamic, int size, bool big_endian>
566 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
567 Sized_relobj<size, big_endian>* relobj,
568 unsigned int local_sym_index,
573 : address_(address), local_sym_index_(local_sym_index), type_(type),
574 is_relative_(is_relative), shndx_(shndx)
576 gold_assert(local_sym_index != GSYM_CODE
577 && local_sym_index != INVALID_CODE);
578 gold_assert(shndx != INVALID_CODE);
579 this->u1_.relobj = relobj;
580 this->u2_.relobj = relobj;
581 if (dynamic && !is_relative)
582 relobj->set_needs_output_dynsym_entry(local_sym_index);
585 // A reloc against the STT_SECTION symbol of an output section.
587 template<bool dynamic, int size, bool big_endian>
588 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
593 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
594 is_relative_(false), shndx_(INVALID_CODE)
599 os->set_needs_dynsym_index();
602 template<bool dynamic, int size, bool big_endian>
603 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
609 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
610 is_relative_(false), shndx_(shndx)
612 gold_assert(shndx != INVALID_CODE);
614 this->u2_.relobj = relobj;
616 os->set_needs_dynsym_index();
619 // Get the symbol index of a relocation.
621 template<bool dynamic, int size, bool big_endian>
623 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
627 switch (this->local_sym_index_)
633 if (this->u1_.gsym == NULL)
636 index = this->u1_.gsym->dynsym_index();
638 index = this->u1_.gsym->symtab_index();
643 index = this->u1_.os->dynsym_index();
645 index = this->u1_.os->symtab_index();
649 // Relocations without symbols use a symbol index of 0.
655 index = this->u1_.relobj->dynsym_index(this->local_sym_index_);
657 index = this->u1_.relobj->symtab_index(this->local_sym_index_);
660 gold_assert(index != -1U);
664 // Write out the offset and info fields of a Rel or Rela relocation
667 template<bool dynamic, int size, bool big_endian>
668 template<typename Write_rel>
670 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
673 Address address = this->address_;
674 if (this->shndx_ != INVALID_CODE)
676 section_offset_type off;
677 Output_section* os = this->u2_.relobj->output_section(this->shndx_,
679 gold_assert(os != NULL);
681 address += os->address() + off;
684 address = os->output_address(this->u2_.relobj, this->shndx_,
686 gold_assert(address != -1U);
689 else if (this->u2_.od != NULL)
690 address += this->u2_.od->address();
691 wr->put_r_offset(address);
692 unsigned int sym_index = this->is_relative_ ? 0 : this->get_symbol_index();
693 wr->put_r_info(elfcpp::elf_r_info<size>(sym_index, this->type_));
696 // Write out a Rel relocation.
698 template<bool dynamic, int size, bool big_endian>
700 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
701 unsigned char* pov) const
703 elfcpp::Rel_write<size, big_endian> orel(pov);
704 this->write_rel(&orel);
707 // Get the value of the symbol referred to by a Rel relocation.
709 template<bool dynamic, int size, bool big_endian>
710 typename elfcpp::Elf_types<size>::Elf_Addr
711 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value() const
713 if (this->local_sym_index_ == GSYM_CODE)
715 const Sized_symbol<size>* sym;
716 sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym);
719 gold_assert(this->local_sym_index_ != SECTION_CODE
720 && this->local_sym_index_ != INVALID_CODE);
721 const Sized_relobj<size, big_endian>* relobj = this->u1_.relobj;
722 return relobj->local_symbol_value(this->local_sym_index_);
725 // Write out a Rela relocation.
727 template<bool dynamic, int size, bool big_endian>
729 Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
730 unsigned char* pov) const
732 elfcpp::Rela_write<size, big_endian> orel(pov);
733 this->rel_.write_rel(&orel);
734 Addend addend = this->addend_;
735 if (rel_.is_relative())
736 addend += rel_.symbol_value();
737 orel.put_r_addend(addend);
740 // Output_data_reloc_base methods.
742 // Adjust the output section.
744 template<int sh_type, bool dynamic, int size, bool big_endian>
746 Output_data_reloc_base<sh_type, dynamic, size, big_endian>
747 ::do_adjust_output_section(Output_section* os)
749 if (sh_type == elfcpp::SHT_REL)
750 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
751 else if (sh_type == elfcpp::SHT_RELA)
752 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
756 os->set_should_link_to_dynsym();
758 os->set_should_link_to_symtab();
761 // Write out relocation data.
763 template<int sh_type, bool dynamic, int size, bool big_endian>
765 Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
768 const off_t off = this->offset();
769 const off_t oview_size = this->data_size();
770 unsigned char* const oview = of->get_output_view(off, oview_size);
772 unsigned char* pov = oview;
773 for (typename Relocs::const_iterator p = this->relocs_.begin();
774 p != this->relocs_.end();
781 gold_assert(pov - oview == oview_size);
783 of->write_output_view(off, oview_size, oview);
785 // We no longer need the relocation entries.
786 this->relocs_.clear();
789 // Output_data_got::Got_entry methods.
791 // Write out the entry.
793 template<int size, bool big_endian>
795 Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
799 switch (this->local_sym_index_)
803 // If the symbol is resolved locally, we need to write out the
804 // link-time value, which will be relocated dynamically by a
805 // RELATIVE relocation.
806 Symbol* gsym = this->u_.gsym;
807 Sized_symbol<size>* sgsym;
808 // This cast is a bit ugly. We don't want to put a
809 // virtual method in Symbol, because we want Symbol to be
810 // as small as possible.
811 sgsym = static_cast<Sized_symbol<size>*>(gsym);
812 val = sgsym->value();
817 val = this->u_.constant;
821 val = this->u_.object->local_symbol_value(this->local_sym_index_);
825 elfcpp::Swap<size, big_endian>::writeval(pov, val);
828 // Output_data_got methods.
830 // Add an entry for a global symbol to the GOT. This returns true if
831 // this is a new GOT entry, false if the symbol already had a GOT
834 template<int size, bool big_endian>
836 Output_data_got<size, big_endian>::add_global(Symbol* gsym)
838 if (gsym->has_got_offset())
841 this->entries_.push_back(Got_entry(gsym));
842 this->set_got_size();
843 gsym->set_got_offset(this->last_got_offset());
847 // Add an entry for a global symbol to the GOT, and add a dynamic
848 // relocation of type R_TYPE for the GOT entry.
849 template<int size, bool big_endian>
851 Output_data_got<size, big_endian>::add_global_with_rel(
856 if (gsym->has_got_offset())
859 this->entries_.push_back(Got_entry());
860 this->set_got_size();
861 unsigned int got_offset = this->last_got_offset();
862 gsym->set_got_offset(got_offset);
863 rel_dyn->add_global(gsym, r_type, this, got_offset);
866 template<int size, bool big_endian>
868 Output_data_got<size, big_endian>::add_global_with_rela(
873 if (gsym->has_got_offset())
876 this->entries_.push_back(Got_entry());
877 this->set_got_size();
878 unsigned int got_offset = this->last_got_offset();
879 gsym->set_got_offset(got_offset);
880 rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
883 // Add an entry for a local symbol to the GOT. This returns true if
884 // this is a new GOT entry, false if the symbol already has a GOT
887 template<int size, bool big_endian>
889 Output_data_got<size, big_endian>::add_local(
890 Sized_relobj<size, big_endian>* object,
893 if (object->local_has_got_offset(symndx))
896 this->entries_.push_back(Got_entry(object, symndx));
897 this->set_got_size();
898 object->set_local_got_offset(symndx, this->last_got_offset());
902 // Add an entry for a local symbol to the GOT, and add a dynamic
903 // relocation of type R_TYPE for the GOT entry.
904 template<int size, bool big_endian>
906 Output_data_got<size, big_endian>::add_local_with_rel(
907 Sized_relobj<size, big_endian>* object,
912 if (object->local_has_got_offset(symndx))
915 this->entries_.push_back(Got_entry());
916 this->set_got_size();
917 unsigned int got_offset = this->last_got_offset();
918 object->set_local_got_offset(symndx, got_offset);
919 rel_dyn->add_local(object, symndx, r_type, this, got_offset);
922 template<int size, bool big_endian>
924 Output_data_got<size, big_endian>::add_local_with_rela(
925 Sized_relobj<size, big_endian>* object,
930 if (object->local_has_got_offset(symndx))
933 this->entries_.push_back(Got_entry());
934 this->set_got_size();
935 unsigned int got_offset = this->last_got_offset();
936 object->set_local_got_offset(symndx, got_offset);
937 rela_dyn->add_local(object, symndx, r_type, this, got_offset, 0);
940 // Add an entry (or a pair of entries) for a global TLS symbol to the GOT.
941 // In a pair of entries, the first value in the pair will be used for the
942 // module index, and the second value will be used for the dtv-relative
943 // offset. This returns true if this is a new GOT entry, false if the symbol
944 // already has a GOT entry.
946 template<int size, bool big_endian>
948 Output_data_got<size, big_endian>::add_global_tls(Symbol* gsym, bool need_pair)
950 if (gsym->has_tls_got_offset(need_pair))
953 this->entries_.push_back(Got_entry(gsym));
954 gsym->set_tls_got_offset(this->last_got_offset(), need_pair);
956 this->entries_.push_back(Got_entry(gsym));
957 this->set_got_size();
961 // Add an entry for a global TLS symbol to the GOT, and add a dynamic
962 // relocation of type R_TYPE.
963 template<int size, bool big_endian>
965 Output_data_got<size, big_endian>::add_global_tls_with_rel(
970 if (gsym->has_tls_got_offset(false))
973 this->entries_.push_back(Got_entry());
974 this->set_got_size();
975 unsigned int got_offset = this->last_got_offset();
976 gsym->set_tls_got_offset(got_offset, false);
977 rel_dyn->add_global(gsym, r_type, this, got_offset);
980 template<int size, bool big_endian>
982 Output_data_got<size, big_endian>::add_global_tls_with_rela(
987 if (gsym->has_tls_got_offset(false))
990 this->entries_.push_back(Got_entry());
991 this->set_got_size();
992 unsigned int got_offset = this->last_got_offset();
993 gsym->set_tls_got_offset(got_offset, false);
994 rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
997 // Add a pair of entries for a global TLS symbol to the GOT, and add
998 // dynamic relocations of type MOD_R_TYPE and DTV_R_TYPE, respectively.
999 template<int size, bool big_endian>
1001 Output_data_got<size, big_endian>::add_global_tls_with_rel(
1004 unsigned int mod_r_type,
1005 unsigned int dtv_r_type)
1007 if (gsym->has_tls_got_offset(true))
1010 this->entries_.push_back(Got_entry());
1011 unsigned int got_offset = this->last_got_offset();
1012 gsym->set_tls_got_offset(got_offset, true);
1013 rel_dyn->add_global(gsym, mod_r_type, this, got_offset);
1015 this->entries_.push_back(Got_entry());
1016 this->set_got_size();
1017 got_offset = this->last_got_offset();
1018 rel_dyn->add_global(gsym, dtv_r_type, this, got_offset);
1021 template<int size, bool big_endian>
1023 Output_data_got<size, big_endian>::add_global_tls_with_rela(
1026 unsigned int mod_r_type,
1027 unsigned int dtv_r_type)
1029 if (gsym->has_tls_got_offset(true))
1032 this->entries_.push_back(Got_entry());
1033 unsigned int got_offset = this->last_got_offset();
1034 gsym->set_tls_got_offset(got_offset, true);
1035 rela_dyn->add_global(gsym, mod_r_type, this, got_offset, 0);
1037 this->entries_.push_back(Got_entry());
1038 this->set_got_size();
1039 got_offset = this->last_got_offset();
1040 rela_dyn->add_global(gsym, dtv_r_type, this, got_offset, 0);
1043 // Add an entry (or a pair of entries) for a local TLS symbol to the GOT.
1044 // In a pair of entries, the first value in the pair will be used for the
1045 // module index, and the second value will be used for the dtv-relative
1046 // offset. This returns true if this is a new GOT entry, false if the symbol
1047 // already has a GOT entry.
1049 template<int size, bool big_endian>
1051 Output_data_got<size, big_endian>::add_local_tls(
1052 Sized_relobj<size, big_endian>* object,
1053 unsigned int symndx,
1056 if (object->local_has_tls_got_offset(symndx, need_pair))
1059 this->entries_.push_back(Got_entry(object, symndx));
1060 object->set_local_tls_got_offset(symndx, this->last_got_offset(), need_pair);
1062 this->entries_.push_back(Got_entry(object, symndx));
1063 this->set_got_size();
1067 // Add an entry (or pair of entries) for a local TLS symbol to the GOT,
1068 // and add a dynamic relocation of type R_TYPE for the first GOT entry.
1069 // Because this is a local symbol, the first GOT entry can be relocated
1070 // relative to a section symbol, and the second GOT entry will have an
1071 // dtv-relative value that can be computed at link time.
1072 template<int size, bool big_endian>
1074 Output_data_got<size, big_endian>::add_local_tls_with_rel(
1075 Sized_relobj<size, big_endian>* object,
1076 unsigned int symndx,
1080 unsigned int r_type)
1082 if (object->local_has_tls_got_offset(symndx, need_pair))
1085 this->entries_.push_back(Got_entry());
1086 unsigned int got_offset = this->last_got_offset();
1087 object->set_local_tls_got_offset(symndx, got_offset, need_pair);
1088 section_offset_type off;
1089 Output_section* os = object->output_section(shndx, &off);
1090 rel_dyn->add_output_section(os, r_type, this, got_offset);
1092 // The second entry of the pair will be statically initialized
1093 // with the TLS offset of the symbol.
1095 this->entries_.push_back(Got_entry(object, symndx));
1097 this->set_got_size();
1100 template<int size, bool big_endian>
1102 Output_data_got<size, big_endian>::add_local_tls_with_rela(
1103 Sized_relobj<size, big_endian>* object,
1104 unsigned int symndx,
1108 unsigned int r_type)
1110 if (object->local_has_tls_got_offset(symndx, need_pair))
1113 this->entries_.push_back(Got_entry());
1114 unsigned int got_offset = this->last_got_offset();
1115 object->set_local_tls_got_offset(symndx, got_offset, need_pair);
1116 section_offset_type off;
1117 Output_section* os = object->output_section(shndx, &off);
1118 rela_dyn->add_output_section(os, r_type, this, got_offset, 0);
1120 // The second entry of the pair will be statically initialized
1121 // with the TLS offset of the symbol.
1123 this->entries_.push_back(Got_entry(object, symndx));
1125 this->set_got_size();
1128 // Write out the GOT.
1130 template<int size, bool big_endian>
1132 Output_data_got<size, big_endian>::do_write(Output_file* of)
1134 const int add = size / 8;
1136 const off_t off = this->offset();
1137 const off_t oview_size = this->data_size();
1138 unsigned char* const oview = of->get_output_view(off, oview_size);
1140 unsigned char* pov = oview;
1141 for (typename Got_entries::const_iterator p = this->entries_.begin();
1142 p != this->entries_.end();
1149 gold_assert(pov - oview == oview_size);
1151 of->write_output_view(off, oview_size, oview);
1153 // We no longer need the GOT entries.
1154 this->entries_.clear();
1157 // Output_data_dynamic::Dynamic_entry methods.
1159 // Write out the entry.
1161 template<int size, bool big_endian>
1163 Output_data_dynamic::Dynamic_entry::write(
1165 const Stringpool* pool
1166 ACCEPT_SIZE_ENDIAN) const
1168 typename elfcpp::Elf_types<size>::Elf_WXword val;
1169 switch (this->classification_)
1171 case DYNAMIC_NUMBER:
1175 case DYNAMIC_SECTION_ADDRESS:
1176 val = this->u_.od->address();
1179 case DYNAMIC_SECTION_SIZE:
1180 val = this->u_.od->data_size();
1183 case DYNAMIC_SYMBOL:
1185 const Sized_symbol<size>* s =
1186 static_cast<const Sized_symbol<size>*>(this->u_.sym);
1191 case DYNAMIC_STRING:
1192 val = pool->get_offset(this->u_.str);
1199 elfcpp::Dyn_write<size, big_endian> dw(pov);
1200 dw.put_d_tag(this->tag_);
1204 // Output_data_dynamic methods.
1206 // Adjust the output section to set the entry size.
1209 Output_data_dynamic::do_adjust_output_section(Output_section* os)
1211 if (parameters->get_size() == 32)
1212 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
1213 else if (parameters->get_size() == 64)
1214 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1219 // Set the final data size.
1222 Output_data_dynamic::set_final_data_size()
1224 // Add the terminating entry.
1225 this->add_constant(elfcpp::DT_NULL, 0);
1228 if (parameters->get_size() == 32)
1229 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
1230 else if (parameters->get_size() == 64)
1231 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1234 this->set_data_size(this->entries_.size() * dyn_size);
1237 // Write out the dynamic entries.
1240 Output_data_dynamic::do_write(Output_file* of)
1242 if (parameters->get_size() == 32)
1244 if (parameters->is_big_endian())
1246 #ifdef HAVE_TARGET_32_BIG
1247 this->sized_write<32, true>(of);
1254 #ifdef HAVE_TARGET_32_LITTLE
1255 this->sized_write<32, false>(of);
1261 else if (parameters->get_size() == 64)
1263 if (parameters->is_big_endian())
1265 #ifdef HAVE_TARGET_64_BIG
1266 this->sized_write<64, true>(of);
1273 #ifdef HAVE_TARGET_64_LITTLE
1274 this->sized_write<64, false>(of);
1284 template<int size, bool big_endian>
1286 Output_data_dynamic::sized_write(Output_file* of)
1288 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1290 const off_t offset = this->offset();
1291 const off_t oview_size = this->data_size();
1292 unsigned char* const oview = of->get_output_view(offset, oview_size);
1294 unsigned char* pov = oview;
1295 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1296 p != this->entries_.end();
1299 p->write SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1300 pov, this->pool_ SELECT_SIZE_ENDIAN(size, big_endian));
1304 gold_assert(pov - oview == oview_size);
1306 of->write_output_view(offset, oview_size, oview);
1308 // We no longer need the dynamic entries.
1309 this->entries_.clear();
1312 // Output_section::Input_section methods.
1314 // Return the data size. For an input section we store the size here.
1315 // For an Output_section_data, we have to ask it for the size.
1318 Output_section::Input_section::data_size() const
1320 if (this->is_input_section())
1321 return this->u1_.data_size;
1323 return this->u2_.posd->data_size();
1326 // Set the address and file offset.
1329 Output_section::Input_section::set_address_and_file_offset(
1332 off_t section_file_offset)
1334 if (this->is_input_section())
1335 this->u2_.object->set_section_offset(this->shndx_,
1336 file_offset - section_file_offset);
1338 this->u2_.posd->set_address_and_file_offset(address, file_offset);
1341 // Finalize the data size.
1344 Output_section::Input_section::finalize_data_size()
1346 if (!this->is_input_section())
1347 this->u2_.posd->finalize_data_size();
1350 // Try to turn an input offset into an output offset.
1353 Output_section::Input_section::output_offset(
1354 const Relobj* object,
1356 section_offset_type offset,
1357 section_offset_type *poutput) const
1359 if (!this->is_input_section())
1360 return this->u2_.posd->output_offset(object, shndx, offset, poutput);
1363 if (this->shndx_ != shndx || this->u2_.object != object)
1365 section_offset_type output_offset;
1366 Output_section* os = object->output_section(shndx, &output_offset);
1367 gold_assert(os != NULL);
1368 gold_assert(output_offset != -1);
1369 *poutput = output_offset + offset;
1374 // Write out the data. We don't have to do anything for an input
1375 // section--they are handled via Object::relocate--but this is where
1376 // we write out the data for an Output_section_data.
1379 Output_section::Input_section::write(Output_file* of)
1381 if (!this->is_input_section())
1382 this->u2_.posd->write(of);
1385 // Write the data to a buffer. As for write(), we don't have to do
1386 // anything for an input section.
1389 Output_section::Input_section::write_to_buffer(unsigned char* buffer)
1391 if (!this->is_input_section())
1392 this->u2_.posd->write_to_buffer(buffer);
1395 // Output_section methods.
1397 // Construct an Output_section. NAME will point into a Stringpool.
1399 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
1400 elfcpp::Elf_Xword flags)
1404 link_section_(NULL),
1406 info_section_(NULL),
1414 first_input_offset_(0),
1416 postprocessing_buffer_(NULL),
1417 needs_symtab_index_(false),
1418 needs_dynsym_index_(false),
1419 should_link_to_symtab_(false),
1420 should_link_to_dynsym_(false),
1421 after_input_sections_(false),
1422 requires_postprocessing_(false),
1425 // An unallocated section has no address. Forcing this means that
1426 // we don't need special treatment for symbols defined in debug
1428 if ((flags & elfcpp::SHF_ALLOC) == 0)
1429 this->set_address(0);
1432 Output_section::~Output_section()
1436 // Set the entry size.
1439 Output_section::set_entsize(uint64_t v)
1441 if (this->entsize_ == 0)
1444 gold_assert(this->entsize_ == v);
1447 // Add the input section SHNDX, with header SHDR, named SECNAME, in
1448 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
1449 // relocation section which applies to this section, or 0 if none, or
1450 // -1U if more than one. Return the offset of the input section
1451 // within the output section. Return -1 if the input section will
1452 // receive special handling. In the normal case we don't always keep
1453 // track of input sections for an Output_section. Instead, each
1454 // Object keeps track of the Output_section for each of its input
1457 template<int size, bool big_endian>
1459 Output_section::add_input_section(Sized_relobj<size, big_endian>* object,
1461 const char* secname,
1462 const elfcpp::Shdr<size, big_endian>& shdr,
1463 unsigned int reloc_shndx)
1465 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
1466 if ((addralign & (addralign - 1)) != 0)
1468 object->error(_("invalid alignment %lu for section \"%s\""),
1469 static_cast<unsigned long>(addralign), secname);
1473 if (addralign > this->addralign_)
1474 this->addralign_ = addralign;
1476 typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
1477 uint64_t entsize = shdr.get_sh_entsize();
1479 // .debug_str is a mergeable string section, but is not always so
1480 // marked by compilers. Mark manually here so we can optimize.
1481 if (strcmp(secname, ".debug_str") == 0)
1483 sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
1487 // If this is a SHF_MERGE section, we pass all the input sections to
1488 // a Output_data_merge. We don't try to handle relocations for such
1490 if ((sh_flags & elfcpp::SHF_MERGE) != 0
1491 && reloc_shndx == 0)
1493 if (this->add_merge_input_section(object, shndx, sh_flags,
1494 entsize, addralign))
1496 // Tell the relocation routines that they need to call the
1497 // output_offset method to determine the final address.
1502 off_t offset_in_section = this->current_data_size_for_child();
1503 off_t aligned_offset_in_section = align_address(offset_in_section,
1506 if (aligned_offset_in_section > offset_in_section
1507 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
1508 && object->target()->has_code_fill())
1510 // We need to add some fill data. Using fill_list_ when
1511 // possible is an optimization, since we will often have fill
1512 // sections without input sections.
1513 off_t fill_len = aligned_offset_in_section - offset_in_section;
1514 if (this->input_sections_.empty())
1515 this->fills_.push_back(Fill(offset_in_section, fill_len));
1518 // FIXME: When relaxing, the size needs to adjust to
1519 // maintain a constant alignment.
1520 std::string fill_data(object->target()->code_fill(fill_len));
1521 Output_data_const* odc = new Output_data_const(fill_data, 1);
1522 this->input_sections_.push_back(Input_section(odc));
1526 this->set_current_data_size_for_child(aligned_offset_in_section
1527 + shdr.get_sh_size());
1529 // We need to keep track of this section if we are already keeping
1530 // track of sections, or if we are relaxing. FIXME: Add test for
1532 if (!this->input_sections_.empty())
1533 this->input_sections_.push_back(Input_section(object, shndx,
1537 return aligned_offset_in_section;
1540 // Add arbitrary data to an output section.
1543 Output_section::add_output_section_data(Output_section_data* posd)
1545 Input_section inp(posd);
1546 this->add_output_section_data(&inp);
1549 // Add arbitrary data to an output section by Input_section.
1552 Output_section::add_output_section_data(Input_section* inp)
1554 if (this->input_sections_.empty())
1555 this->first_input_offset_ = this->current_data_size_for_child();
1557 this->input_sections_.push_back(*inp);
1559 uint64_t addralign = inp->addralign();
1560 if (addralign > this->addralign_)
1561 this->addralign_ = addralign;
1563 inp->set_output_section(this);
1566 // Add a merge section to an output section.
1569 Output_section::add_output_merge_section(Output_section_data* posd,
1570 bool is_string, uint64_t entsize)
1572 Input_section inp(posd, is_string, entsize);
1573 this->add_output_section_data(&inp);
1576 // Add an input section to a SHF_MERGE section.
1579 Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
1580 uint64_t flags, uint64_t entsize,
1583 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
1585 // We only merge strings if the alignment is not more than the
1586 // character size. This could be handled, but it's unusual.
1587 if (is_string && addralign > entsize)
1590 Input_section_list::iterator p;
1591 for (p = this->input_sections_.begin();
1592 p != this->input_sections_.end();
1594 if (p->is_merge_section(is_string, entsize, addralign))
1596 p->add_input_section(object, shndx);
1600 // We handle the actual constant merging in Output_merge_data or
1601 // Output_merge_string_data.
1602 Output_section_data* posd;
1604 posd = new Output_merge_data(entsize, addralign);
1610 posd = new Output_merge_string<char>(addralign);
1613 posd = new Output_merge_string<uint16_t>(addralign);
1616 posd = new Output_merge_string<uint32_t>(addralign);
1623 this->add_output_merge_section(posd, is_string, entsize);
1624 posd->add_input_section(object, shndx);
1629 // Given an address OFFSET relative to the start of input section
1630 // SHNDX in OBJECT, return whether this address is being included in
1631 // the final link. This should only be called if SHNDX in OBJECT has
1632 // a special mapping.
1635 Output_section::is_input_address_mapped(const Relobj* object,
1639 gold_assert(object->is_section_specially_mapped(shndx));
1641 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1642 p != this->input_sections_.end();
1645 section_offset_type output_offset;
1646 if (p->output_offset(object, shndx, offset, &output_offset))
1647 return output_offset != -1;
1650 // By default we assume that the address is mapped. This should
1651 // only be called after we have passed all sections to Layout. At
1652 // that point we should know what we are discarding.
1656 // Given an address OFFSET relative to the start of input section
1657 // SHNDX in object OBJECT, return the output offset relative to the
1658 // start of the section. This should only be called if SHNDX in
1659 // OBJECT has a special mapping.
1662 Output_section::output_offset(const Relobj* object, unsigned int shndx,
1663 section_offset_type offset) const
1665 gold_assert(object->is_section_specially_mapped(shndx));
1666 // This can only be called meaningfully when layout is complete.
1667 gold_assert(Output_data::is_layout_complete());
1669 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1670 p != this->input_sections_.end();
1673 section_offset_type output_offset;
1674 if (p->output_offset(object, shndx, offset, &output_offset))
1675 return output_offset;
1680 // Return the output virtual address of OFFSET relative to the start
1681 // of input section SHNDX in object OBJECT.
1684 Output_section::output_address(const Relobj* object, unsigned int shndx,
1687 gold_assert(object->is_section_specially_mapped(shndx));
1688 // This can only be called meaningfully when layout is complete.
1689 gold_assert(Output_data::is_layout_complete());
1691 uint64_t addr = this->address() + this->first_input_offset_;
1692 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1693 p != this->input_sections_.end();
1696 addr = align_address(addr, p->addralign());
1697 section_offset_type output_offset;
1698 if (p->output_offset(object, shndx, offset, &output_offset))
1700 if (output_offset == -1)
1702 return addr + output_offset;
1704 addr += p->data_size();
1707 // If we get here, it means that we don't know the mapping for this
1708 // input section. This might happen in principle if
1709 // add_input_section were called before add_output_section_data.
1710 // But it should never actually happen.
1715 // Set the data size of an Output_section. This is where we handle
1716 // setting the addresses of any Output_section_data objects.
1719 Output_section::set_final_data_size()
1721 if (this->input_sections_.empty())
1723 this->set_data_size(this->current_data_size_for_child());
1727 uint64_t address = this->address();
1728 off_t startoff = this->offset();
1729 off_t off = startoff + this->first_input_offset_;
1730 for (Input_section_list::iterator p = this->input_sections_.begin();
1731 p != this->input_sections_.end();
1734 off = align_address(off, p->addralign());
1735 p->set_address_and_file_offset(address + (off - startoff), off,
1737 off += p->data_size();
1740 this->set_data_size(off - startoff);
1743 // Set the TLS offset. Called only for SHT_TLS sections.
1746 Output_section::do_set_tls_offset(uint64_t tls_base)
1748 this->tls_offset_ = this->address() - tls_base;
1751 // Write the section header to *OSHDR.
1753 template<int size, bool big_endian>
1755 Output_section::write_header(const Layout* layout,
1756 const Stringpool* secnamepool,
1757 elfcpp::Shdr_write<size, big_endian>* oshdr) const
1759 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
1760 oshdr->put_sh_type(this->type_);
1761 oshdr->put_sh_flags(this->flags_);
1762 oshdr->put_sh_addr(this->address());
1763 oshdr->put_sh_offset(this->offset());
1764 oshdr->put_sh_size(this->data_size());
1765 if (this->link_section_ != NULL)
1766 oshdr->put_sh_link(this->link_section_->out_shndx());
1767 else if (this->should_link_to_symtab_)
1768 oshdr->put_sh_link(layout->symtab_section()->out_shndx());
1769 else if (this->should_link_to_dynsym_)
1770 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
1772 oshdr->put_sh_link(this->link_);
1773 if (this->info_section_ != NULL)
1774 oshdr->put_sh_info(this->info_section_->out_shndx());
1776 oshdr->put_sh_info(this->info_);
1777 oshdr->put_sh_addralign(this->addralign_);
1778 oshdr->put_sh_entsize(this->entsize_);
1781 // Write out the data. For input sections the data is written out by
1782 // Object::relocate, but we have to handle Output_section_data objects
1786 Output_section::do_write(Output_file* of)
1788 gold_assert(!this->requires_postprocessing());
1790 off_t output_section_file_offset = this->offset();
1791 for (Fill_list::iterator p = this->fills_.begin();
1792 p != this->fills_.end();
1795 std::string fill_data(of->target()->code_fill(p->length()));
1796 of->write(output_section_file_offset + p->section_offset(),
1797 fill_data.data(), fill_data.size());
1800 for (Input_section_list::iterator p = this->input_sections_.begin();
1801 p != this->input_sections_.end();
1806 // If a section requires postprocessing, create the buffer to use.
1809 Output_section::create_postprocessing_buffer()
1811 gold_assert(this->requires_postprocessing());
1812 gold_assert(this->postprocessing_buffer_ == NULL);
1814 if (!this->input_sections_.empty())
1816 off_t off = this->first_input_offset_;
1817 for (Input_section_list::iterator p = this->input_sections_.begin();
1818 p != this->input_sections_.end();
1821 off = align_address(off, p->addralign());
1822 p->finalize_data_size();
1823 off += p->data_size();
1825 this->set_current_data_size_for_child(off);
1828 off_t buffer_size = this->current_data_size_for_child();
1829 this->postprocessing_buffer_ = new unsigned char[buffer_size];
1832 // Write all the data of an Output_section into the postprocessing
1833 // buffer. This is used for sections which require postprocessing,
1834 // such as compression. Input sections are handled by
1835 // Object::Relocate.
1838 Output_section::write_to_postprocessing_buffer()
1840 gold_assert(this->requires_postprocessing());
1842 Target* target = parameters->target();
1843 unsigned char* buffer = this->postprocessing_buffer();
1844 for (Fill_list::iterator p = this->fills_.begin();
1845 p != this->fills_.end();
1848 std::string fill_data(target->code_fill(p->length()));
1849 memcpy(buffer + p->section_offset(), fill_data.data(), fill_data.size());
1852 off_t off = this->first_input_offset_;
1853 for (Input_section_list::iterator p = this->input_sections_.begin();
1854 p != this->input_sections_.end();
1857 off = align_address(off, p->addralign());
1858 p->write_to_buffer(buffer + off);
1859 off += p->data_size();
1863 // Print stats for merge sections to stderr.
1866 Output_section::print_merge_stats()
1868 Input_section_list::iterator p;
1869 for (p = this->input_sections_.begin();
1870 p != this->input_sections_.end();
1872 p->print_merge_stats(this->name_);
1875 // Output segment methods.
1877 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
1888 is_align_known_(false)
1892 // Add an Output_section to an Output_segment.
1895 Output_segment::add_output_section(Output_section* os,
1896 elfcpp::Elf_Word seg_flags,
1899 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
1900 gold_assert(!this->is_align_known_);
1902 // Update the segment flags.
1903 this->flags_ |= seg_flags;
1905 Output_segment::Output_data_list* pdl;
1906 if (os->type() == elfcpp::SHT_NOBITS)
1907 pdl = &this->output_bss_;
1909 pdl = &this->output_data_;
1911 // So that PT_NOTE segments will work correctly, we need to ensure
1912 // that all SHT_NOTE sections are adjacent. This will normally
1913 // happen automatically, because all the SHT_NOTE input sections
1914 // will wind up in the same output section. However, it is possible
1915 // for multiple SHT_NOTE input sections to have different section
1916 // flags, and thus be in different output sections, but for the
1917 // different section flags to map into the same segment flags and
1918 // thus the same output segment.
1920 // Note that while there may be many input sections in an output
1921 // section, there are normally only a few output sections in an
1922 // output segment. This loop is expected to be fast.
1924 if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
1926 Output_segment::Output_data_list::iterator p = pdl->end();
1930 if ((*p)->is_section_type(elfcpp::SHT_NOTE))
1932 // We don't worry about the FRONT parameter.
1938 while (p != pdl->begin());
1941 // Similarly, so that PT_TLS segments will work, we need to group
1942 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
1943 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
1944 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
1945 // correctly. SHF_TLS sections get added to both a PT_LOAD segment
1946 // and the PT_TLS segment -- we do this grouping only for the
1948 if (this->type_ != elfcpp::PT_TLS
1949 && (os->flags() & elfcpp::SHF_TLS) != 0
1950 && !this->output_data_.empty())
1952 pdl = &this->output_data_;
1953 bool nobits = os->type() == elfcpp::SHT_NOBITS;
1954 bool sawtls = false;
1955 Output_segment::Output_data_list::iterator p = pdl->end();
1960 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
1963 // Put a NOBITS section after the first TLS section.
1964 // But a PROGBITS section after the first TLS/PROGBITS
1966 insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS);
1970 // If we've gone past the TLS sections, but we've seen a
1971 // TLS section, then we need to insert this section now.
1977 // We don't worry about the FRONT parameter.
1983 while (p != pdl->begin());
1985 // There are no TLS sections yet; put this one at the requested
1986 // location in the section list.
1990 pdl->push_front(os);
1995 // Add an Output_data (which is not an Output_section) to the start of
1999 Output_segment::add_initial_output_data(Output_data* od)
2001 gold_assert(!this->is_align_known_);
2002 this->output_data_.push_front(od);
2005 // Return the maximum alignment of the Output_data in Output_segment.
2006 // Once we compute this, we prohibit new sections from being added.
2009 Output_segment::addralign()
2011 if (!this->is_align_known_)
2015 addralign = Output_segment::maximum_alignment(&this->output_data_);
2016 if (addralign > this->align_)
2017 this->align_ = addralign;
2019 addralign = Output_segment::maximum_alignment(&this->output_bss_);
2020 if (addralign > this->align_)
2021 this->align_ = addralign;
2023 this->is_align_known_ = true;
2026 return this->align_;
2029 // Return the maximum alignment of a list of Output_data.
2032 Output_segment::maximum_alignment(const Output_data_list* pdl)
2035 for (Output_data_list::const_iterator p = pdl->begin();
2039 uint64_t addralign = (*p)->addralign();
2040 if (addralign > ret)
2046 // Return the number of dynamic relocs applied to this segment.
2049 Output_segment::dynamic_reloc_count() const
2051 return (this->dynamic_reloc_count_list(&this->output_data_)
2052 + this->dynamic_reloc_count_list(&this->output_bss_));
2055 // Return the number of dynamic relocs applied to an Output_data_list.
2058 Output_segment::dynamic_reloc_count_list(const Output_data_list* pdl) const
2060 unsigned int count = 0;
2061 for (Output_data_list::const_iterator p = pdl->begin();
2064 count += (*p)->dynamic_reloc_count();
2068 // Set the section addresses for an Output_segment. ADDR is the
2069 // address and *POFF is the file offset. Set the section indexes
2070 // starting with *PSHNDX. Return the address of the immediately
2071 // following segment. Update *POFF and *PSHNDX.
2074 Output_segment::set_section_addresses(uint64_t addr, off_t* poff,
2075 unsigned int* pshndx)
2077 gold_assert(this->type_ == elfcpp::PT_LOAD);
2079 this->vaddr_ = addr;
2080 this->paddr_ = addr;
2082 off_t orig_off = *poff;
2083 this->offset_ = orig_off;
2085 *poff = align_address(*poff, this->addralign());
2087 addr = this->set_section_list_addresses(&this->output_data_, addr, poff,
2089 this->filesz_ = *poff - orig_off;
2093 uint64_t ret = this->set_section_list_addresses(&this->output_bss_, addr,
2095 this->memsz_ = *poff - orig_off;
2097 // Ignore the file offset adjustments made by the BSS Output_data
2104 // Set the addresses and file offsets in a list of Output_data
2108 Output_segment::set_section_list_addresses(Output_data_list* pdl,
2109 uint64_t addr, off_t* poff,
2110 unsigned int* pshndx)
2112 off_t startoff = *poff;
2114 off_t off = startoff;
2115 for (Output_data_list::iterator p = pdl->begin();
2119 off = align_address(off, (*p)->addralign());
2120 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
2122 // Unless this is a PT_TLS segment, we want to ignore the size
2123 // of a SHF_TLS/SHT_NOBITS section. Such a section does not
2124 // affect the size of a PT_LOAD segment.
2125 if (this->type_ == elfcpp::PT_TLS
2126 || !(*p)->is_section_flag_set(elfcpp::SHF_TLS)
2127 || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
2128 off += (*p)->data_size();
2130 if ((*p)->is_section())
2132 (*p)->set_out_shndx(*pshndx);
2138 return addr + (off - startoff);
2141 // For a non-PT_LOAD segment, set the offset from the sections, if
2145 Output_segment::set_offset()
2147 gold_assert(this->type_ != elfcpp::PT_LOAD);
2149 if (this->output_data_.empty() && this->output_bss_.empty())
2160 const Output_data* first;
2161 if (this->output_data_.empty())
2162 first = this->output_bss_.front();
2164 first = this->output_data_.front();
2165 this->vaddr_ = first->address();
2166 this->paddr_ = this->vaddr_;
2167 this->offset_ = first->offset();
2169 if (this->output_data_.empty())
2173 const Output_data* last_data = this->output_data_.back();
2174 this->filesz_ = (last_data->address()
2175 + last_data->data_size()
2179 const Output_data* last;
2180 if (this->output_bss_.empty())
2181 last = this->output_data_.back();
2183 last = this->output_bss_.back();
2184 this->memsz_ = (last->address()
2189 // Set the TLS offsets of the sections in the PT_TLS segment.
2192 Output_segment::set_tls_offsets()
2194 gold_assert(this->type_ == elfcpp::PT_TLS);
2196 for (Output_data_list::iterator p = this->output_data_.begin();
2197 p != this->output_data_.end();
2199 (*p)->set_tls_offset(this->vaddr_);
2201 for (Output_data_list::iterator p = this->output_bss_.begin();
2202 p != this->output_bss_.end();
2204 (*p)->set_tls_offset(this->vaddr_);
2207 // Return the number of Output_sections in an Output_segment.
2210 Output_segment::output_section_count() const
2212 return (this->output_section_count_list(&this->output_data_)
2213 + this->output_section_count_list(&this->output_bss_));
2216 // Return the number of Output_sections in an Output_data_list.
2219 Output_segment::output_section_count_list(const Output_data_list* pdl) const
2221 unsigned int count = 0;
2222 for (Output_data_list::const_iterator p = pdl->begin();
2226 if ((*p)->is_section())
2232 // Write the segment data into *OPHDR.
2234 template<int size, bool big_endian>
2236 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
2238 ophdr->put_p_type(this->type_);
2239 ophdr->put_p_offset(this->offset_);
2240 ophdr->put_p_vaddr(this->vaddr_);
2241 ophdr->put_p_paddr(this->paddr_);
2242 ophdr->put_p_filesz(this->filesz_);
2243 ophdr->put_p_memsz(this->memsz_);
2244 ophdr->put_p_flags(this->flags_);
2245 ophdr->put_p_align(this->addralign());
2248 // Write the section headers into V.
2250 template<int size, bool big_endian>
2252 Output_segment::write_section_headers(const Layout* layout,
2253 const Stringpool* secnamepool,
2255 unsigned int *pshndx
2256 ACCEPT_SIZE_ENDIAN) const
2258 // Every section that is attached to a segment must be attached to a
2259 // PT_LOAD segment, so we only write out section headers for PT_LOAD
2261 if (this->type_ != elfcpp::PT_LOAD)
2264 v = this->write_section_headers_list
2265 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
2266 layout, secnamepool, &this->output_data_, v, pshndx
2267 SELECT_SIZE_ENDIAN(size, big_endian));
2268 v = this->write_section_headers_list
2269 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
2270 layout, secnamepool, &this->output_bss_, v, pshndx
2271 SELECT_SIZE_ENDIAN(size, big_endian));
2275 template<int size, bool big_endian>
2277 Output_segment::write_section_headers_list(const Layout* layout,
2278 const Stringpool* secnamepool,
2279 const Output_data_list* pdl,
2281 unsigned int* pshndx
2282 ACCEPT_SIZE_ENDIAN) const
2284 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2285 for (Output_data_list::const_iterator p = pdl->begin();
2289 if ((*p)->is_section())
2291 const Output_section* ps = static_cast<const Output_section*>(*p);
2292 gold_assert(*pshndx == ps->out_shndx());
2293 elfcpp::Shdr_write<size, big_endian> oshdr(v);
2294 ps->write_header(layout, secnamepool, &oshdr);
2302 // Output_file methods.
2304 Output_file::Output_file(const General_options& options, Target* target)
2305 : options_(options),
2307 name_(options.output_file_name()),
2311 map_is_anonymous_(false)
2315 // Open the output file.
2318 Output_file::open(off_t file_size)
2320 this->file_size_ = file_size;
2322 // Unlink the file first; otherwise the open() may fail if the file
2323 // is busy (e.g. it's an executable that's currently being executed).
2325 // However, the linker may be part of a system where a zero-length
2326 // file is created for it to write to, with tight permissions (gcc
2327 // 2.95 did something like this). Unlinking the file would work
2328 // around those permission controls, so we only unlink if the file
2329 // has a non-zero size. We also unlink only regular files to avoid
2330 // trouble with directories/etc.
2332 // If we fail, continue; this command is merely a best-effort attempt
2333 // to improve the odds for open().
2335 // We let the name "-" mean "stdout"
2336 if (strcmp(this->name_, "-") == 0)
2337 this->o_ = STDOUT_FILENO;
2341 if (::stat(this->name_, &s) == 0 && s.st_size != 0)
2342 unlink_if_ordinary(this->name_);
2344 int mode = parameters->output_is_object() ? 0666 : 0777;
2345 int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode);
2347 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
2354 // Resize the output file.
2357 Output_file::resize(off_t file_size)
2359 // If the mmap is mapping an anonymous memory buffer, this is easy:
2360 // just mremap to the new size. If it's mapping to a file, we want
2361 // to unmap to flush to the file, then remap after growing the file.
2362 if (this->map_is_anonymous_)
2364 void* base = ::mremap(this->base_, this->file_size_, file_size,
2366 if (base == MAP_FAILED)
2367 gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
2368 this->base_ = static_cast<unsigned char*>(base);
2369 this->file_size_ = file_size;
2374 this->file_size_ = file_size;
2379 // Map the file into memory.
2384 const int o = this->o_;
2386 // If the output file is not a regular file, don't try to mmap it;
2387 // instead, we'll mmap a block of memory (an anonymous buffer), and
2388 // then later write the buffer to the file.
2390 struct stat statbuf;
2391 if (o == STDOUT_FILENO || o == STDERR_FILENO
2392 || ::fstat(o, &statbuf) != 0
2393 || !S_ISREG(statbuf.st_mode))
2395 this->map_is_anonymous_ = true;
2396 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
2397 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
2401 // Write out one byte to make the file the right size.
2402 if (::lseek(o, this->file_size_ - 1, SEEK_SET) < 0)
2403 gold_fatal(_("%s: lseek: %s"), this->name_, strerror(errno));
2405 if (::write(o, &b, 1) != 1)
2406 gold_fatal(_("%s: write: %s"), this->name_, strerror(errno));
2408 // Map the file into memory.
2409 this->map_is_anonymous_ = false;
2410 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
2413 if (base == MAP_FAILED)
2414 gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
2415 this->base_ = static_cast<unsigned char*>(base);
2418 // Unmap the file from memory.
2421 Output_file::unmap()
2423 if (::munmap(this->base_, this->file_size_) < 0)
2424 gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
2428 // Close the output file.
2431 Output_file::close()
2433 // If the map isn't file-backed, we need to write it now.
2434 if (this->map_is_anonymous_)
2436 size_t bytes_to_write = this->file_size_;
2437 while (bytes_to_write > 0)
2439 ssize_t bytes_written = ::write(this->o_, this->base_, bytes_to_write);
2440 if (bytes_written == 0)
2441 gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
2442 else if (bytes_written < 0)
2443 gold_error(_("%s: write: %s"), this->name_, strerror(errno));
2445 bytes_to_write -= bytes_written;
2450 // We don't close stdout or stderr
2451 if (this->o_ != STDOUT_FILENO && this->o_ != STDERR_FILENO)
2452 if (::close(this->o_) < 0)
2453 gold_error(_("%s: close: %s"), this->name_, strerror(errno));
2457 // Instantiate the templates we need. We could use the configure
2458 // script to restrict this to only the ones for implemented targets.
2460 #ifdef HAVE_TARGET_32_LITTLE
2463 Output_section::add_input_section<32, false>(
2464 Sized_relobj<32, false>* object,
2466 const char* secname,
2467 const elfcpp::Shdr<32, false>& shdr,
2468 unsigned int reloc_shndx);
2471 #ifdef HAVE_TARGET_32_BIG
2474 Output_section::add_input_section<32, true>(
2475 Sized_relobj<32, true>* object,
2477 const char* secname,
2478 const elfcpp::Shdr<32, true>& shdr,
2479 unsigned int reloc_shndx);
2482 #ifdef HAVE_TARGET_64_LITTLE
2485 Output_section::add_input_section<64, false>(
2486 Sized_relobj<64, false>* object,
2488 const char* secname,
2489 const elfcpp::Shdr<64, false>& shdr,
2490 unsigned int reloc_shndx);
2493 #ifdef HAVE_TARGET_64_BIG
2496 Output_section::add_input_section<64, true>(
2497 Sized_relobj<64, true>* object,
2499 const char* secname,
2500 const elfcpp::Shdr<64, true>& shdr,
2501 unsigned int reloc_shndx);
2504 #ifdef HAVE_TARGET_32_LITTLE
2506 class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
2509 #ifdef HAVE_TARGET_32_BIG
2511 class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
2514 #ifdef HAVE_TARGET_64_LITTLE
2516 class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
2519 #ifdef HAVE_TARGET_64_BIG
2521 class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
2524 #ifdef HAVE_TARGET_32_LITTLE
2526 class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
2529 #ifdef HAVE_TARGET_32_BIG
2531 class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
2534 #ifdef HAVE_TARGET_64_LITTLE
2536 class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
2539 #ifdef HAVE_TARGET_64_BIG
2541 class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
2544 #ifdef HAVE_TARGET_32_LITTLE
2546 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
2549 #ifdef HAVE_TARGET_32_BIG
2551 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
2554 #ifdef HAVE_TARGET_64_LITTLE
2556 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
2559 #ifdef HAVE_TARGET_64_BIG
2561 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
2564 #ifdef HAVE_TARGET_32_LITTLE
2566 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
2569 #ifdef HAVE_TARGET_32_BIG
2571 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
2574 #ifdef HAVE_TARGET_64_LITTLE
2576 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
2579 #ifdef HAVE_TARGET_64_BIG
2581 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
2584 #ifdef HAVE_TARGET_32_LITTLE
2586 class Output_data_got<32, false>;
2589 #ifdef HAVE_TARGET_32_BIG
2591 class Output_data_got<32, true>;
2594 #ifdef HAVE_TARGET_64_LITTLE
2596 class Output_data_got<64, false>;
2599 #ifdef HAVE_TARGET_64_BIG
2601 class Output_data_got<64, true>;
2604 } // End namespace gold.