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"
44 // Output_data variables.
46 bool Output_data::allocated_sizes_are_fixed;
48 // Output_data methods.
50 Output_data::~Output_data()
54 // Return the default alignment for the target size.
57 Output_data::default_alignment()
59 return Output_data::default_alignment_for_size(parameters->get_size());
62 // Return the default alignment for a size--32 or 64.
65 Output_data::default_alignment_for_size(int size)
75 // Output_section_header methods. This currently assumes that the
76 // segment and section lists are complete at construction time.
78 Output_section_headers::Output_section_headers(
80 const Layout::Segment_list* segment_list,
81 const Layout::Section_list* unattached_section_list,
82 const Stringpool* secnamepool)
84 segment_list_(segment_list),
85 unattached_section_list_(unattached_section_list),
86 secnamepool_(secnamepool)
88 // Count all the sections. Start with 1 for the null section.
90 for (Layout::Segment_list::const_iterator p = segment_list->begin();
91 p != segment_list->end();
93 if ((*p)->type() == elfcpp::PT_LOAD)
94 count += (*p)->output_section_count();
95 count += unattached_section_list->size();
97 const int size = parameters->get_size();
100 shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
102 shdr_size = elfcpp::Elf_sizes<64>::shdr_size;
106 this->set_data_size(count * shdr_size);
109 // Write out the section headers.
112 Output_section_headers::do_write(Output_file* of)
114 if (parameters->get_size() == 32)
116 if (parameters->is_big_endian())
118 #ifdef HAVE_TARGET_32_BIG
119 this->do_sized_write<32, true>(of);
126 #ifdef HAVE_TARGET_32_LITTLE
127 this->do_sized_write<32, false>(of);
133 else if (parameters->get_size() == 64)
135 if (parameters->is_big_endian())
137 #ifdef HAVE_TARGET_64_BIG
138 this->do_sized_write<64, true>(of);
145 #ifdef HAVE_TARGET_64_LITTLE
146 this->do_sized_write<64, false>(of);
156 template<int size, bool big_endian>
158 Output_section_headers::do_sized_write(Output_file* of)
160 off_t all_shdrs_size = this->data_size();
161 unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size);
163 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
164 unsigned char* v = view;
167 typename elfcpp::Shdr_write<size, big_endian> oshdr(v);
168 oshdr.put_sh_name(0);
169 oshdr.put_sh_type(elfcpp::SHT_NULL);
170 oshdr.put_sh_flags(0);
171 oshdr.put_sh_addr(0);
172 oshdr.put_sh_offset(0);
173 oshdr.put_sh_size(0);
174 oshdr.put_sh_link(0);
175 oshdr.put_sh_info(0);
176 oshdr.put_sh_addralign(0);
177 oshdr.put_sh_entsize(0);
183 for (Layout::Segment_list::const_iterator p = this->segment_list_->begin();
184 p != this->segment_list_->end();
186 v = (*p)->write_section_headers SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
187 this->layout_, this->secnamepool_, v, &shndx
188 SELECT_SIZE_ENDIAN(size, big_endian));
189 for (Layout::Section_list::const_iterator p =
190 this->unattached_section_list_->begin();
191 p != this->unattached_section_list_->end();
194 gold_assert(shndx == (*p)->out_shndx());
195 elfcpp::Shdr_write<size, big_endian> oshdr(v);
196 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
201 of->write_output_view(this->offset(), all_shdrs_size, view);
204 // Output_segment_header methods.
206 Output_segment_headers::Output_segment_headers(
207 const Layout::Segment_list& segment_list)
208 : segment_list_(segment_list)
210 const int size = parameters->get_size();
213 phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
215 phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
219 this->set_data_size(segment_list.size() * phdr_size);
223 Output_segment_headers::do_write(Output_file* of)
225 if (parameters->get_size() == 32)
227 if (parameters->is_big_endian())
229 #ifdef HAVE_TARGET_32_BIG
230 this->do_sized_write<32, true>(of);
237 #ifdef HAVE_TARGET_32_LITTLE
238 this->do_sized_write<32, false>(of);
244 else if (parameters->get_size() == 64)
246 if (parameters->is_big_endian())
248 #ifdef HAVE_TARGET_64_BIG
249 this->do_sized_write<64, true>(of);
256 #ifdef HAVE_TARGET_64_LITTLE
257 this->do_sized_write<64, false>(of);
267 template<int size, bool big_endian>
269 Output_segment_headers::do_sized_write(Output_file* of)
271 const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size;
272 off_t all_phdrs_size = this->segment_list_.size() * phdr_size;
273 unsigned char* view = of->get_output_view(this->offset(),
275 unsigned char* v = view;
276 for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
277 p != this->segment_list_.end();
280 elfcpp::Phdr_write<size, big_endian> ophdr(v);
281 (*p)->write_header(&ophdr);
285 of->write_output_view(this->offset(), all_phdrs_size, view);
288 // Output_file_header methods.
290 Output_file_header::Output_file_header(const Target* target,
291 const Symbol_table* symtab,
292 const Output_segment_headers* osh)
295 segment_header_(osh),
296 section_header_(NULL),
299 const int size = parameters->get_size();
302 ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
304 ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
308 this->set_data_size(ehdr_size);
311 // Set the section table information for a file header.
314 Output_file_header::set_section_info(const Output_section_headers* shdrs,
315 const Output_section* shstrtab)
317 this->section_header_ = shdrs;
318 this->shstrtab_ = shstrtab;
321 // Write out the file header.
324 Output_file_header::do_write(Output_file* of)
326 gold_assert(this->offset() == 0);
328 if (parameters->get_size() == 32)
330 if (parameters->is_big_endian())
332 #ifdef HAVE_TARGET_32_BIG
333 this->do_sized_write<32, true>(of);
340 #ifdef HAVE_TARGET_32_LITTLE
341 this->do_sized_write<32, false>(of);
347 else if (parameters->get_size() == 64)
349 if (parameters->is_big_endian())
351 #ifdef HAVE_TARGET_64_BIG
352 this->do_sized_write<64, true>(of);
359 #ifdef HAVE_TARGET_64_LITTLE
360 this->do_sized_write<64, false>(of);
370 // Write out the file header with appropriate size and endianess.
372 template<int size, bool big_endian>
374 Output_file_header::do_sized_write(Output_file* of)
376 gold_assert(this->offset() == 0);
378 int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
379 unsigned char* view = of->get_output_view(0, ehdr_size);
380 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
382 unsigned char e_ident[elfcpp::EI_NIDENT];
383 memset(e_ident, 0, elfcpp::EI_NIDENT);
384 e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
385 e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
386 e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
387 e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
389 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
391 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
394 e_ident[elfcpp::EI_DATA] = (big_endian
395 ? elfcpp::ELFDATA2MSB
396 : elfcpp::ELFDATA2LSB);
397 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
398 // FIXME: Some targets may need to set EI_OSABI and EI_ABIVERSION.
399 oehdr.put_e_ident(e_ident);
402 if (parameters->output_is_object())
403 e_type = elfcpp::ET_REL;
404 else if (parameters->output_is_shared())
405 e_type = elfcpp::ET_DYN;
407 e_type = elfcpp::ET_EXEC;
408 oehdr.put_e_type(e_type);
410 oehdr.put_e_machine(this->target_->machine_code());
411 oehdr.put_e_version(elfcpp::EV_CURRENT);
413 // FIXME: Need to support -e, and target specific entry symbol.
414 Symbol* sym = this->symtab_->lookup("_start");
415 typename Sized_symbol<size>::Value_type v;
420 Sized_symbol<size>* ssym;
421 ssym = this->symtab_->get_sized_symbol SELECT_SIZE_NAME(size) (
422 sym SELECT_SIZE(size));
425 oehdr.put_e_entry(v);
427 oehdr.put_e_phoff(this->segment_header_->offset());
428 oehdr.put_e_shoff(this->section_header_->offset());
430 // FIXME: The target needs to set the flags.
431 oehdr.put_e_flags(0);
433 oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
434 oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
435 oehdr.put_e_phnum(this->segment_header_->data_size()
436 / elfcpp::Elf_sizes<size>::phdr_size);
437 oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
438 oehdr.put_e_shnum(this->section_header_->data_size()
439 / elfcpp::Elf_sizes<size>::shdr_size);
440 oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
442 of->write_output_view(0, ehdr_size, view);
445 // Output_data_const methods.
448 Output_data_const::do_write(Output_file* of)
450 of->write(this->offset(), this->data_.data(), this->data_.size());
453 // Output_data_const_buffer methods.
456 Output_data_const_buffer::do_write(Output_file* of)
458 of->write(this->offset(), this->p_, this->data_size());
461 // Output_section_data methods.
463 // Record the output section, and set the entry size and such.
466 Output_section_data::set_output_section(Output_section* os)
468 gold_assert(this->output_section_ == NULL);
469 this->output_section_ = os;
470 this->do_adjust_output_section(os);
473 // Return the section index of the output section.
476 Output_section_data::do_out_shndx() const
478 gold_assert(this->output_section_ != NULL);
479 return this->output_section_->out_shndx();
482 // Output_data_strtab methods.
484 // Set the final data size.
487 Output_data_strtab::set_final_data_size()
489 this->strtab_->set_string_offsets();
490 this->set_data_size(this->strtab_->get_strtab_size());
493 // Write out a string table.
496 Output_data_strtab::do_write(Output_file* of)
498 this->strtab_->write(of, this->offset());
501 // Output_reloc methods.
503 // Get the symbol index of a relocation.
505 template<bool dynamic, int size, bool big_endian>
507 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
511 switch (this->local_sym_index_)
517 if (this->u1_.gsym == NULL)
520 index = this->u1_.gsym->dynsym_index();
522 index = this->u1_.gsym->symtab_index();
527 index = this->u1_.os->dynsym_index();
529 index = this->u1_.os->symtab_index();
533 // Relocations without symbols use a symbol index of 0.
540 // FIXME: It seems that some targets may need to generate
541 // dynamic relocations against local symbols for some
542 // reasons. This will have to be addressed at some point.
546 index = this->u1_.relobj->symtab_index(this->local_sym_index_);
549 gold_assert(index != -1U);
553 // Write out the offset and info fields of a Rel or Rela relocation
556 template<bool dynamic, int size, bool big_endian>
557 template<typename Write_rel>
559 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
562 Address address = this->address_;
563 if (this->shndx_ != INVALID_CODE)
566 Output_section* os = this->u2_.relobj->output_section(this->shndx_,
568 gold_assert(os != NULL);
570 address += os->address() + off;
573 address = os->output_address(this->u2_.relobj, this->shndx_,
575 gold_assert(address != -1U);
578 else if (this->u2_.od != NULL)
579 address += this->u2_.od->address();
580 wr->put_r_offset(address);
581 wr->put_r_info(elfcpp::elf_r_info<size>(this->get_symbol_index(),
585 // Write out a Rel relocation.
587 template<bool dynamic, int size, bool big_endian>
589 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
590 unsigned char* pov) const
592 elfcpp::Rel_write<size, big_endian> orel(pov);
593 this->write_rel(&orel);
596 // Write out a Rela relocation.
598 template<bool dynamic, int size, bool big_endian>
600 Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
601 unsigned char* pov) const
603 elfcpp::Rela_write<size, big_endian> orel(pov);
604 this->rel_.write_rel(&orel);
605 orel.put_r_addend(this->addend_);
608 // Output_data_reloc_base methods.
610 // Adjust the output section.
612 template<int sh_type, bool dynamic, int size, bool big_endian>
614 Output_data_reloc_base<sh_type, dynamic, size, big_endian>
615 ::do_adjust_output_section(Output_section* os)
617 if (sh_type == elfcpp::SHT_REL)
618 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
619 else if (sh_type == elfcpp::SHT_RELA)
620 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
624 os->set_should_link_to_dynsym();
626 os->set_should_link_to_symtab();
629 // Write out relocation data.
631 template<int sh_type, bool dynamic, int size, bool big_endian>
633 Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
636 const off_t off = this->offset();
637 const off_t oview_size = this->data_size();
638 unsigned char* const oview = of->get_output_view(off, oview_size);
640 unsigned char* pov = oview;
641 for (typename Relocs::const_iterator p = this->relocs_.begin();
642 p != this->relocs_.end();
649 gold_assert(pov - oview == oview_size);
651 of->write_output_view(off, oview_size, oview);
653 // We no longer need the relocation entries.
654 this->relocs_.clear();
657 // Output_data_got::Got_entry methods.
659 // Write out the entry.
661 template<int size, bool big_endian>
663 Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
667 switch (this->local_sym_index_)
671 Symbol* gsym = this->u_.gsym;
673 // If the symbol is resolved locally, we need to write out its
674 // value. Otherwise we just write zero. The target code is
675 // responsible for creating a relocation entry to fill in the
676 // value at runtime. For non-preemptible symbols in a shared
677 // library, the target will need to record whether or not the
678 // value should be written (e.g., it may use a RELATIVE
680 if (gsym->final_value_is_known() || gsym->needs_value_in_got())
682 Sized_symbol<size>* sgsym;
683 // This cast is a bit ugly. We don't want to put a
684 // virtual method in Symbol, because we want Symbol to be
685 // as small as possible.
686 sgsym = static_cast<Sized_symbol<size>*>(gsym);
687 val = sgsym->value();
693 val = this->u_.constant;
697 val = this->u_.object->local_symbol_value(this->local_sym_index_);
701 elfcpp::Swap<size, big_endian>::writeval(pov, val);
704 // Output_data_got methods.
706 // Add an entry for a global symbol to the GOT. This returns true if
707 // this is a new GOT entry, false if the symbol already had a GOT
710 template<int size, bool big_endian>
712 Output_data_got<size, big_endian>::add_global(Symbol* gsym)
714 if (gsym->has_got_offset())
717 this->entries_.push_back(Got_entry(gsym));
718 this->set_got_size();
719 gsym->set_got_offset(this->last_got_offset());
723 // Add an entry for a local symbol to the GOT. This returns true if
724 // this is a new GOT entry, false if the symbol already has a GOT
727 template<int size, bool big_endian>
729 Output_data_got<size, big_endian>::add_local(
730 Sized_relobj<size, big_endian>* object,
733 if (object->local_has_got_offset(symndx))
736 this->entries_.push_back(Got_entry(object, symndx));
737 this->set_got_size();
738 object->set_local_got_offset(symndx, this->last_got_offset());
742 // Add an entry (or a pair of entries) for a global TLS symbol to the GOT.
743 // In a pair of entries, the first value in the pair will be used for the
744 // module index, and the second value will be used for the dtv-relative
745 // offset. This returns true if this is a new GOT entry, false if the symbol
746 // already has a GOT entry.
748 template<int size, bool big_endian>
750 Output_data_got<size, big_endian>::add_global_tls(Symbol* gsym,
753 if (gsym->has_tls_got_offset(need_pair))
756 this->entries_.push_back(Got_entry(gsym));
757 gsym->set_tls_got_offset(this->last_got_offset(), need_pair);
759 this->entries_.push_back(Got_entry(gsym));
760 this->set_got_size();
764 // Add an entry (or a pair of entries) for a local TLS symbol to the GOT.
765 // In a pair of entries, the first value in the pair will be used for the
766 // module index, and the second value will be used for the dtv-relative
767 // offset. This returns true if this is a new GOT entry, false if the symbol
768 // already has a GOT entry.
770 template<int size, bool big_endian>
772 Output_data_got<size, big_endian>::add_local_tls(
773 Sized_relobj<size, big_endian>* object,
777 if (object->local_has_tls_got_offset(symndx, need_pair))
780 this->entries_.push_back(Got_entry(object, symndx));
781 object->set_local_tls_got_offset(symndx, this->last_got_offset(), need_pair);
783 this->entries_.push_back(Got_entry(object, symndx));
784 this->set_got_size();
788 // Write out the GOT.
790 template<int size, bool big_endian>
792 Output_data_got<size, big_endian>::do_write(Output_file* of)
794 const int add = size / 8;
796 const off_t off = this->offset();
797 const off_t oview_size = this->data_size();
798 unsigned char* const oview = of->get_output_view(off, oview_size);
800 unsigned char* pov = oview;
801 for (typename Got_entries::const_iterator p = this->entries_.begin();
802 p != this->entries_.end();
809 gold_assert(pov - oview == oview_size);
811 of->write_output_view(off, oview_size, oview);
813 // We no longer need the GOT entries.
814 this->entries_.clear();
817 // Output_data_dynamic::Dynamic_entry methods.
819 // Write out the entry.
821 template<int size, bool big_endian>
823 Output_data_dynamic::Dynamic_entry::write(
825 const Stringpool* pool
826 ACCEPT_SIZE_ENDIAN) const
828 typename elfcpp::Elf_types<size>::Elf_WXword val;
829 switch (this->classification_)
835 case DYNAMIC_SECTION_ADDRESS:
836 val = this->u_.od->address();
839 case DYNAMIC_SECTION_SIZE:
840 val = this->u_.od->data_size();
845 const Sized_symbol<size>* s =
846 static_cast<const Sized_symbol<size>*>(this->u_.sym);
852 val = pool->get_offset(this->u_.str);
859 elfcpp::Dyn_write<size, big_endian> dw(pov);
860 dw.put_d_tag(this->tag_);
864 // Output_data_dynamic methods.
866 // Adjust the output section to set the entry size.
869 Output_data_dynamic::do_adjust_output_section(Output_section* os)
871 if (parameters->get_size() == 32)
872 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
873 else if (parameters->get_size() == 64)
874 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
879 // Set the final data size.
882 Output_data_dynamic::set_final_data_size()
884 // Add the terminating entry.
885 this->add_constant(elfcpp::DT_NULL, 0);
888 if (parameters->get_size() == 32)
889 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
890 else if (parameters->get_size() == 64)
891 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
894 this->set_data_size(this->entries_.size() * dyn_size);
897 // Write out the dynamic entries.
900 Output_data_dynamic::do_write(Output_file* of)
902 if (parameters->get_size() == 32)
904 if (parameters->is_big_endian())
906 #ifdef HAVE_TARGET_32_BIG
907 this->sized_write<32, true>(of);
914 #ifdef HAVE_TARGET_32_LITTLE
915 this->sized_write<32, false>(of);
921 else if (parameters->get_size() == 64)
923 if (parameters->is_big_endian())
925 #ifdef HAVE_TARGET_64_BIG
926 this->sized_write<64, true>(of);
933 #ifdef HAVE_TARGET_64_LITTLE
934 this->sized_write<64, false>(of);
944 template<int size, bool big_endian>
946 Output_data_dynamic::sized_write(Output_file* of)
948 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
950 const off_t offset = this->offset();
951 const off_t oview_size = this->data_size();
952 unsigned char* const oview = of->get_output_view(offset, oview_size);
954 unsigned char* pov = oview;
955 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
956 p != this->entries_.end();
959 p->write SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
960 pov, this->pool_ SELECT_SIZE_ENDIAN(size, big_endian));
964 gold_assert(pov - oview == oview_size);
966 of->write_output_view(offset, oview_size, oview);
968 // We no longer need the dynamic entries.
969 this->entries_.clear();
972 // Output_section::Input_section methods.
974 // Return the data size. For an input section we store the size here.
975 // For an Output_section_data, we have to ask it for the size.
978 Output_section::Input_section::data_size() const
980 if (this->is_input_section())
981 return this->u1_.data_size;
983 return this->u2_.posd->data_size();
986 // Set the address and file offset.
989 Output_section::Input_section::set_address_and_file_offset(
992 off_t section_file_offset)
994 if (this->is_input_section())
995 this->u2_.object->set_section_offset(this->shndx_,
996 file_offset - section_file_offset);
998 this->u2_.posd->set_address_and_file_offset(address, file_offset);
1001 // Finalize the data size.
1004 Output_section::Input_section::finalize_data_size()
1006 if (!this->is_input_section())
1007 this->u2_.posd->finalize_data_size();
1010 // Try to turn an input offset into an output offset.
1013 Output_section::Input_section::output_offset(const Relobj* object,
1016 off_t *poutput) const
1018 if (!this->is_input_section())
1019 return this->u2_.posd->output_offset(object, shndx, offset, poutput);
1022 if (this->shndx_ != shndx || this->u2_.object != object)
1024 off_t output_offset;
1025 Output_section* os = object->output_section(shndx, &output_offset);
1026 gold_assert(os != NULL);
1027 gold_assert(output_offset != -1);
1028 *poutput = output_offset + offset;
1033 // Write out the data. We don't have to do anything for an input
1034 // section--they are handled via Object::relocate--but this is where
1035 // we write out the data for an Output_section_data.
1038 Output_section::Input_section::write(Output_file* of)
1040 if (!this->is_input_section())
1041 this->u2_.posd->write(of);
1044 // Write the data to a buffer. As for write(), we don't have to do
1045 // anything for an input section.
1048 Output_section::Input_section::write_to_buffer(unsigned char* buffer)
1050 if (!this->is_input_section())
1051 this->u2_.posd->write_to_buffer(buffer);
1054 // Output_section methods.
1056 // Construct an Output_section. NAME will point into a Stringpool.
1058 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
1059 elfcpp::Elf_Xword flags)
1063 link_section_(NULL),
1065 info_section_(NULL),
1073 first_input_offset_(0),
1075 postprocessing_buffer_(NULL),
1076 needs_symtab_index_(false),
1077 needs_dynsym_index_(false),
1078 should_link_to_symtab_(false),
1079 should_link_to_dynsym_(false),
1080 after_input_sections_(false),
1081 requires_postprocessing_(false)
1083 // An unallocated section has no address. Forcing this means that
1084 // we don't need special treatment for symbols defined in debug
1086 if ((flags & elfcpp::SHF_ALLOC) == 0)
1087 this->set_address(0);
1090 Output_section::~Output_section()
1094 // Set the entry size.
1097 Output_section::set_entsize(uint64_t v)
1099 if (this->entsize_ == 0)
1102 gold_assert(this->entsize_ == v);
1105 // Add the input section SHNDX, with header SHDR, named SECNAME, in
1106 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
1107 // relocation section which applies to this section, or 0 if none, or
1108 // -1U if more than one. Return the offset of the input section
1109 // within the output section. Return -1 if the input section will
1110 // receive special handling. In the normal case we don't always keep
1111 // track of input sections for an Output_section. Instead, each
1112 // Object keeps track of the Output_section for each of its input
1115 template<int size, bool big_endian>
1117 Output_section::add_input_section(Sized_relobj<size, big_endian>* object,
1119 const char* secname,
1120 const elfcpp::Shdr<size, big_endian>& shdr,
1121 unsigned int reloc_shndx)
1123 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
1124 if ((addralign & (addralign - 1)) != 0)
1126 object->error(_("invalid alignment %lu for section \"%s\""),
1127 static_cast<unsigned long>(addralign), secname);
1131 if (addralign > this->addralign_)
1132 this->addralign_ = addralign;
1134 typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
1135 uint64_t entsize = shdr.get_sh_entsize();
1137 // .debug_str is a mergeable string section, but is not always so
1138 // marked by compilers. Mark manually here so we can optimize.
1139 if (strcmp(secname, ".debug_str") == 0)
1141 sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
1145 // If this is a SHF_MERGE section, we pass all the input sections to
1146 // a Output_data_merge. We don't try to handle relocations for such
1148 if ((sh_flags & elfcpp::SHF_MERGE) != 0
1149 && reloc_shndx == 0)
1151 if (this->add_merge_input_section(object, shndx, sh_flags,
1152 entsize, addralign))
1154 // Tell the relocation routines that they need to call the
1155 // output_offset method to determine the final address.
1160 off_t offset_in_section = this->current_data_size_for_child();
1161 off_t aligned_offset_in_section = align_address(offset_in_section,
1164 if (aligned_offset_in_section > offset_in_section
1165 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
1166 && object->target()->has_code_fill())
1168 // We need to add some fill data. Using fill_list_ when
1169 // possible is an optimization, since we will often have fill
1170 // sections without input sections.
1171 off_t fill_len = aligned_offset_in_section - offset_in_section;
1172 if (this->input_sections_.empty())
1173 this->fills_.push_back(Fill(offset_in_section, fill_len));
1176 // FIXME: When relaxing, the size needs to adjust to
1177 // maintain a constant alignment.
1178 std::string fill_data(object->target()->code_fill(fill_len));
1179 Output_data_const* odc = new Output_data_const(fill_data, 1);
1180 this->input_sections_.push_back(Input_section(odc));
1184 this->set_current_data_size_for_child(aligned_offset_in_section
1185 + shdr.get_sh_size());
1187 // We need to keep track of this section if we are already keeping
1188 // track of sections, or if we are relaxing. FIXME: Add test for
1190 if (!this->input_sections_.empty())
1191 this->input_sections_.push_back(Input_section(object, shndx,
1195 return aligned_offset_in_section;
1198 // Add arbitrary data to an output section.
1201 Output_section::add_output_section_data(Output_section_data* posd)
1203 Input_section inp(posd);
1204 this->add_output_section_data(&inp);
1207 // Add arbitrary data to an output section by Input_section.
1210 Output_section::add_output_section_data(Input_section* inp)
1212 if (this->input_sections_.empty())
1213 this->first_input_offset_ = this->current_data_size_for_child();
1215 this->input_sections_.push_back(*inp);
1217 uint64_t addralign = inp->addralign();
1218 if (addralign > this->addralign_)
1219 this->addralign_ = addralign;
1221 inp->set_output_section(this);
1224 // Add a merge section to an output section.
1227 Output_section::add_output_merge_section(Output_section_data* posd,
1228 bool is_string, uint64_t entsize)
1230 Input_section inp(posd, is_string, entsize);
1231 this->add_output_section_data(&inp);
1234 // Add an input section to a SHF_MERGE section.
1237 Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
1238 uint64_t flags, uint64_t entsize,
1241 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
1243 // We only merge strings if the alignment is not more than the
1244 // character size. This could be handled, but it's unusual.
1245 if (is_string && addralign > entsize)
1248 Input_section_list::iterator p;
1249 for (p = this->input_sections_.begin();
1250 p != this->input_sections_.end();
1252 if (p->is_merge_section(is_string, entsize, addralign))
1254 p->add_input_section(object, shndx);
1258 // We handle the actual constant merging in Output_merge_data or
1259 // Output_merge_string_data.
1260 Output_section_data* posd;
1262 posd = new Output_merge_data(entsize, addralign);
1268 posd = new Output_merge_string<char>(addralign);
1271 posd = new Output_merge_string<uint16_t>(addralign);
1274 posd = new Output_merge_string<uint32_t>(addralign);
1281 this->add_output_merge_section(posd, is_string, entsize);
1282 posd->add_input_section(object, shndx);
1287 // Given an address OFFSET relative to the start of input section
1288 // SHNDX in OBJECT, return whether this address is being included in
1289 // the final link. This should only be called if SHNDX in OBJECT has
1290 // a special mapping.
1293 Output_section::is_input_address_mapped(const Relobj* object,
1297 gold_assert(object->is_section_specially_mapped(shndx));
1299 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1300 p != this->input_sections_.end();
1303 off_t output_offset;
1304 if (p->output_offset(object, shndx, offset, &output_offset))
1305 return output_offset != -1;
1308 // By default we assume that the address is mapped. This should
1309 // only be called after we have passed all sections to Layout. At
1310 // that point we should know what we are discarding.
1314 // Given an address OFFSET relative to the start of input section
1315 // SHNDX in object OBJECT, return the output offset relative to the
1316 // start of the section. This should only be called if SHNDX in
1317 // OBJECT has a special mapping.
1320 Output_section::output_offset(const Relobj* object, unsigned int shndx,
1323 gold_assert(object->is_section_specially_mapped(shndx));
1324 // This can only be called meaningfully when layout is complete.
1325 gold_assert(Output_data::is_layout_complete());
1327 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1328 p != this->input_sections_.end();
1331 off_t output_offset;
1332 if (p->output_offset(object, shndx, offset, &output_offset))
1333 return output_offset;
1338 // Return the output virtual address of OFFSET relative to the start
1339 // of input section SHNDX in object OBJECT.
1342 Output_section::output_address(const Relobj* object, unsigned int shndx,
1345 gold_assert(object->is_section_specially_mapped(shndx));
1346 // This can only be called meaningfully when layout is complete.
1347 gold_assert(Output_data::is_layout_complete());
1349 uint64_t addr = this->address() + this->first_input_offset_;
1350 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1351 p != this->input_sections_.end();
1354 addr = align_address(addr, p->addralign());
1355 off_t output_offset;
1356 if (p->output_offset(object, shndx, offset, &output_offset))
1358 if (output_offset == -1)
1360 return addr + output_offset;
1362 addr += p->data_size();
1365 // If we get here, it means that we don't know the mapping for this
1366 // input section. This might happen in principle if
1367 // add_input_section were called before add_output_section_data.
1368 // But it should never actually happen.
1373 // Set the data size of an Output_section. This is where we handle
1374 // setting the addresses of any Output_section_data objects.
1377 Output_section::set_final_data_size()
1379 if (this->input_sections_.empty())
1381 this->set_data_size(this->current_data_size_for_child());
1385 uint64_t address = this->address();
1386 off_t startoff = this->offset();
1387 off_t off = startoff + this->first_input_offset_;
1388 for (Input_section_list::iterator p = this->input_sections_.begin();
1389 p != this->input_sections_.end();
1392 off = align_address(off, p->addralign());
1393 p->set_address_and_file_offset(address + (off - startoff), off,
1395 off += p->data_size();
1398 this->set_data_size(off - startoff);
1401 // Write the section header to *OSHDR.
1403 template<int size, bool big_endian>
1405 Output_section::write_header(const Layout* layout,
1406 const Stringpool* secnamepool,
1407 elfcpp::Shdr_write<size, big_endian>* oshdr) const
1409 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
1410 oshdr->put_sh_type(this->type_);
1411 oshdr->put_sh_flags(this->flags_);
1412 oshdr->put_sh_addr(this->address());
1413 oshdr->put_sh_offset(this->offset());
1414 oshdr->put_sh_size(this->data_size());
1415 if (this->link_section_ != NULL)
1416 oshdr->put_sh_link(this->link_section_->out_shndx());
1417 else if (this->should_link_to_symtab_)
1418 oshdr->put_sh_link(layout->symtab_section()->out_shndx());
1419 else if (this->should_link_to_dynsym_)
1420 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
1422 oshdr->put_sh_link(this->link_);
1423 if (this->info_section_ != NULL)
1424 oshdr->put_sh_info(this->info_section_->out_shndx());
1426 oshdr->put_sh_info(this->info_);
1427 oshdr->put_sh_addralign(this->addralign_);
1428 oshdr->put_sh_entsize(this->entsize_);
1431 // Write out the data. For input sections the data is written out by
1432 // Object::relocate, but we have to handle Output_section_data objects
1436 Output_section::do_write(Output_file* of)
1438 gold_assert(!this->requires_postprocessing());
1440 off_t output_section_file_offset = this->offset();
1441 for (Fill_list::iterator p = this->fills_.begin();
1442 p != this->fills_.end();
1445 std::string fill_data(of->target()->code_fill(p->length()));
1446 of->write(output_section_file_offset + p->section_offset(),
1447 fill_data.data(), fill_data.size());
1450 for (Input_section_list::iterator p = this->input_sections_.begin();
1451 p != this->input_sections_.end();
1456 // If a section requires postprocessing, create the buffer to use.
1459 Output_section::create_postprocessing_buffer()
1461 gold_assert(this->requires_postprocessing());
1462 gold_assert(this->postprocessing_buffer_ == NULL);
1464 if (!this->input_sections_.empty())
1466 off_t off = this->first_input_offset_;
1467 for (Input_section_list::iterator p = this->input_sections_.begin();
1468 p != this->input_sections_.end();
1471 off = align_address(off, p->addralign());
1472 p->finalize_data_size();
1473 off += p->data_size();
1475 this->set_current_data_size_for_child(off);
1478 off_t buffer_size = this->current_data_size_for_child();
1479 this->postprocessing_buffer_ = new unsigned char[buffer_size];
1482 // Write all the data of an Output_section into the postprocessing
1483 // buffer. This is used for sections which require postprocessing,
1484 // such as compression. Input sections are handled by
1485 // Object::Relocate.
1488 Output_section::write_to_postprocessing_buffer()
1490 gold_assert(this->requires_postprocessing());
1492 Target* target = parameters->target();
1493 unsigned char* buffer = this->postprocessing_buffer();
1494 for (Fill_list::iterator p = this->fills_.begin();
1495 p != this->fills_.end();
1498 std::string fill_data(target->code_fill(p->length()));
1499 memcpy(buffer + p->section_offset(), fill_data.data(), fill_data.size());
1502 off_t off = this->first_input_offset_;
1503 for (Input_section_list::iterator p = this->input_sections_.begin();
1504 p != this->input_sections_.end();
1507 off = align_address(off, p->addralign());
1508 p->write_to_buffer(buffer + off);
1509 off += p->data_size();
1513 // Output segment methods.
1515 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
1526 is_align_known_(false)
1530 // Add an Output_section to an Output_segment.
1533 Output_segment::add_output_section(Output_section* os,
1534 elfcpp::Elf_Word seg_flags,
1537 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
1538 gold_assert(!this->is_align_known_);
1540 // Update the segment flags.
1541 this->flags_ |= seg_flags;
1543 Output_segment::Output_data_list* pdl;
1544 if (os->type() == elfcpp::SHT_NOBITS)
1545 pdl = &this->output_bss_;
1547 pdl = &this->output_data_;
1549 // So that PT_NOTE segments will work correctly, we need to ensure
1550 // that all SHT_NOTE sections are adjacent. This will normally
1551 // happen automatically, because all the SHT_NOTE input sections
1552 // will wind up in the same output section. However, it is possible
1553 // for multiple SHT_NOTE input sections to have different section
1554 // flags, and thus be in different output sections, but for the
1555 // different section flags to map into the same segment flags and
1556 // thus the same output segment.
1558 // Note that while there may be many input sections in an output
1559 // section, there are normally only a few output sections in an
1560 // output segment. This loop is expected to be fast.
1562 if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
1564 Output_segment::Output_data_list::iterator p = pdl->end();
1568 if ((*p)->is_section_type(elfcpp::SHT_NOTE))
1570 // We don't worry about the FRONT parameter.
1576 while (p != pdl->begin());
1579 // Similarly, so that PT_TLS segments will work, we need to group
1580 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
1581 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
1582 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
1583 // correctly. SHF_TLS sections get added to both a PT_LOAD segment
1584 // and the PT_TLS segment -- we do this grouping only for the
1586 if (this->type_ != elfcpp::PT_TLS
1587 && (os->flags() & elfcpp::SHF_TLS) != 0
1588 && !this->output_data_.empty())
1590 pdl = &this->output_data_;
1591 bool nobits = os->type() == elfcpp::SHT_NOBITS;
1592 bool sawtls = false;
1593 Output_segment::Output_data_list::iterator p = pdl->end();
1598 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
1601 // Put a NOBITS section after the first TLS section.
1602 // But a PROGBITS section after the first TLS/PROGBITS
1604 insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS);
1608 // If we've gone past the TLS sections, but we've seen a
1609 // TLS section, then we need to insert this section now.
1615 // We don't worry about the FRONT parameter.
1621 while (p != pdl->begin());
1623 // There are no TLS sections yet; put this one at the requested
1624 // location in the section list.
1628 pdl->push_front(os);
1633 // Add an Output_data (which is not an Output_section) to the start of
1637 Output_segment::add_initial_output_data(Output_data* od)
1639 gold_assert(!this->is_align_known_);
1640 this->output_data_.push_front(od);
1643 // Return the maximum alignment of the Output_data in Output_segment.
1644 // Once we compute this, we prohibit new sections from being added.
1647 Output_segment::addralign()
1649 if (!this->is_align_known_)
1653 addralign = Output_segment::maximum_alignment(&this->output_data_);
1654 if (addralign > this->align_)
1655 this->align_ = addralign;
1657 addralign = Output_segment::maximum_alignment(&this->output_bss_);
1658 if (addralign > this->align_)
1659 this->align_ = addralign;
1661 this->is_align_known_ = true;
1664 return this->align_;
1667 // Return the maximum alignment of a list of Output_data.
1670 Output_segment::maximum_alignment(const Output_data_list* pdl)
1673 for (Output_data_list::const_iterator p = pdl->begin();
1677 uint64_t addralign = (*p)->addralign();
1678 if (addralign > ret)
1684 // Return the number of dynamic relocs applied to this segment.
1687 Output_segment::dynamic_reloc_count() const
1689 return (this->dynamic_reloc_count_list(&this->output_data_)
1690 + this->dynamic_reloc_count_list(&this->output_bss_));
1693 // Return the number of dynamic relocs applied to an Output_data_list.
1696 Output_segment::dynamic_reloc_count_list(const Output_data_list* pdl) const
1698 unsigned int count = 0;
1699 for (Output_data_list::const_iterator p = pdl->begin();
1702 count += (*p)->dynamic_reloc_count();
1706 // Set the section addresses for an Output_segment. ADDR is the
1707 // address and *POFF is the file offset. Set the section indexes
1708 // starting with *PSHNDX. Return the address of the immediately
1709 // following segment. Update *POFF and *PSHNDX.
1712 Output_segment::set_section_addresses(uint64_t addr, off_t* poff,
1713 unsigned int* pshndx)
1715 gold_assert(this->type_ == elfcpp::PT_LOAD);
1717 this->vaddr_ = addr;
1718 this->paddr_ = addr;
1720 off_t orig_off = *poff;
1721 this->offset_ = orig_off;
1723 *poff = align_address(*poff, this->addralign());
1725 addr = this->set_section_list_addresses(&this->output_data_, addr, poff,
1727 this->filesz_ = *poff - orig_off;
1731 uint64_t ret = this->set_section_list_addresses(&this->output_bss_, addr,
1733 this->memsz_ = *poff - orig_off;
1735 // Ignore the file offset adjustments made by the BSS Output_data
1742 // Set the addresses and file offsets in a list of Output_data
1746 Output_segment::set_section_list_addresses(Output_data_list* pdl,
1747 uint64_t addr, off_t* poff,
1748 unsigned int* pshndx)
1750 off_t startoff = *poff;
1752 off_t off = startoff;
1753 for (Output_data_list::iterator p = pdl->begin();
1757 off = align_address(off, (*p)->addralign());
1758 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
1760 // Unless this is a PT_TLS segment, we want to ignore the size
1761 // of a SHF_TLS/SHT_NOBITS section. Such a section does not
1762 // affect the size of a PT_LOAD segment.
1763 if (this->type_ == elfcpp::PT_TLS
1764 || !(*p)->is_section_flag_set(elfcpp::SHF_TLS)
1765 || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
1766 off += (*p)->data_size();
1768 if ((*p)->is_section())
1770 (*p)->set_out_shndx(*pshndx);
1776 return addr + (off - startoff);
1779 // For a non-PT_LOAD segment, set the offset from the sections, if
1783 Output_segment::set_offset()
1785 gold_assert(this->type_ != elfcpp::PT_LOAD);
1787 if (this->output_data_.empty() && this->output_bss_.empty())
1798 const Output_data* first;
1799 if (this->output_data_.empty())
1800 first = this->output_bss_.front();
1802 first = this->output_data_.front();
1803 this->vaddr_ = first->address();
1804 this->paddr_ = this->vaddr_;
1805 this->offset_ = first->offset();
1807 if (this->output_data_.empty())
1811 const Output_data* last_data = this->output_data_.back();
1812 this->filesz_ = (last_data->address()
1813 + last_data->data_size()
1817 const Output_data* last;
1818 if (this->output_bss_.empty())
1819 last = this->output_data_.back();
1821 last = this->output_bss_.back();
1822 this->memsz_ = (last->address()
1827 // Return the number of Output_sections in an Output_segment.
1830 Output_segment::output_section_count() const
1832 return (this->output_section_count_list(&this->output_data_)
1833 + this->output_section_count_list(&this->output_bss_));
1836 // Return the number of Output_sections in an Output_data_list.
1839 Output_segment::output_section_count_list(const Output_data_list* pdl) const
1841 unsigned int count = 0;
1842 for (Output_data_list::const_iterator p = pdl->begin();
1846 if ((*p)->is_section())
1852 // Write the segment data into *OPHDR.
1854 template<int size, bool big_endian>
1856 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
1858 ophdr->put_p_type(this->type_);
1859 ophdr->put_p_offset(this->offset_);
1860 ophdr->put_p_vaddr(this->vaddr_);
1861 ophdr->put_p_paddr(this->paddr_);
1862 ophdr->put_p_filesz(this->filesz_);
1863 ophdr->put_p_memsz(this->memsz_);
1864 ophdr->put_p_flags(this->flags_);
1865 ophdr->put_p_align(this->addralign());
1868 // Write the section headers into V.
1870 template<int size, bool big_endian>
1872 Output_segment::write_section_headers(const Layout* layout,
1873 const Stringpool* secnamepool,
1875 unsigned int *pshndx
1876 ACCEPT_SIZE_ENDIAN) const
1878 // Every section that is attached to a segment must be attached to a
1879 // PT_LOAD segment, so we only write out section headers for PT_LOAD
1881 if (this->type_ != elfcpp::PT_LOAD)
1884 v = this->write_section_headers_list
1885 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1886 layout, secnamepool, &this->output_data_, v, pshndx
1887 SELECT_SIZE_ENDIAN(size, big_endian));
1888 v = this->write_section_headers_list
1889 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1890 layout, secnamepool, &this->output_bss_, v, pshndx
1891 SELECT_SIZE_ENDIAN(size, big_endian));
1895 template<int size, bool big_endian>
1897 Output_segment::write_section_headers_list(const Layout* layout,
1898 const Stringpool* secnamepool,
1899 const Output_data_list* pdl,
1901 unsigned int* pshndx
1902 ACCEPT_SIZE_ENDIAN) const
1904 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1905 for (Output_data_list::const_iterator p = pdl->begin();
1909 if ((*p)->is_section())
1911 const Output_section* ps = static_cast<const Output_section*>(*p);
1912 gold_assert(*pshndx == ps->out_shndx());
1913 elfcpp::Shdr_write<size, big_endian> oshdr(v);
1914 ps->write_header(layout, secnamepool, &oshdr);
1922 // Output_file methods.
1924 Output_file::Output_file(const General_options& options, Target* target)
1925 : options_(options),
1927 name_(options.output_file_name()),
1934 // Open the output file.
1937 Output_file::open(off_t file_size)
1939 this->file_size_ = file_size;
1941 // Unlink the file first; otherwise the open() may fail if the file
1942 // is busy (e.g. it's an executable that's currently being executed).
1944 // However, the linker may be part of a system where a zero-length
1945 // file is created for it to write to, with tight permissions (gcc
1946 // 2.95 did something like this). Unlinking the file would work
1947 // around those permission controls, so we only unlink if the file
1948 // has a non-zero size. We also unlink only regular files to avoid
1949 // trouble with directories/etc.
1951 // If we fail, continue; this command is merely a best-effort attempt
1952 // to improve the odds for open().
1955 if (::stat(this->name_, &s) == 0 && s.st_size != 0)
1956 unlink_if_ordinary(this->name_);
1958 int mode = parameters->output_is_object() ? 0666 : 0777;
1959 int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode);
1961 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
1967 // Resize the output file.
1970 Output_file::resize(off_t file_size)
1972 if (::munmap(this->base_, this->file_size_) < 0)
1973 gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
1974 this->file_size_ = file_size;
1978 // Map the file into memory.
1985 // Write out one byte to make the file the right size.
1986 if (::lseek(o, this->file_size_ - 1, SEEK_SET) < 0)
1987 gold_fatal(_("%s: lseek: %s"), this->name_, strerror(errno));
1989 if (::write(o, &b, 1) != 1)
1990 gold_fatal(_("%s: write: %s"), this->name_, strerror(errno));
1992 // Map the file into memory.
1993 void* base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
1995 if (base == MAP_FAILED)
1996 gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
1997 this->base_ = static_cast<unsigned char*>(base);
2000 // Close the output file.
2003 Output_file::close()
2005 if (::munmap(this->base_, this->file_size_) < 0)
2006 gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
2009 if (::close(this->o_) < 0)
2010 gold_error(_("%s: close: %s"), this->name_, strerror(errno));
2014 // Instantiate the templates we need. We could use the configure
2015 // script to restrict this to only the ones for implemented targets.
2017 #ifdef HAVE_TARGET_32_LITTLE
2020 Output_section::add_input_section<32, false>(
2021 Sized_relobj<32, false>* object,
2023 const char* secname,
2024 const elfcpp::Shdr<32, false>& shdr,
2025 unsigned int reloc_shndx);
2028 #ifdef HAVE_TARGET_32_BIG
2031 Output_section::add_input_section<32, true>(
2032 Sized_relobj<32, true>* object,
2034 const char* secname,
2035 const elfcpp::Shdr<32, true>& shdr,
2036 unsigned int reloc_shndx);
2039 #ifdef HAVE_TARGET_64_LITTLE
2042 Output_section::add_input_section<64, false>(
2043 Sized_relobj<64, false>* object,
2045 const char* secname,
2046 const elfcpp::Shdr<64, false>& shdr,
2047 unsigned int reloc_shndx);
2050 #ifdef HAVE_TARGET_64_BIG
2053 Output_section::add_input_section<64, true>(
2054 Sized_relobj<64, true>* object,
2056 const char* secname,
2057 const elfcpp::Shdr<64, true>& shdr,
2058 unsigned int reloc_shndx);
2061 #ifdef HAVE_TARGET_32_LITTLE
2063 class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
2066 #ifdef HAVE_TARGET_32_BIG
2068 class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
2071 #ifdef HAVE_TARGET_64_LITTLE
2073 class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
2076 #ifdef HAVE_TARGET_64_BIG
2078 class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
2081 #ifdef HAVE_TARGET_32_LITTLE
2083 class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
2086 #ifdef HAVE_TARGET_32_BIG
2088 class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
2091 #ifdef HAVE_TARGET_64_LITTLE
2093 class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
2096 #ifdef HAVE_TARGET_64_BIG
2098 class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
2101 #ifdef HAVE_TARGET_32_LITTLE
2103 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
2106 #ifdef HAVE_TARGET_32_BIG
2108 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
2111 #ifdef HAVE_TARGET_64_LITTLE
2113 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
2116 #ifdef HAVE_TARGET_64_BIG
2118 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
2121 #ifdef HAVE_TARGET_32_LITTLE
2123 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
2126 #ifdef HAVE_TARGET_32_BIG
2128 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
2131 #ifdef HAVE_TARGET_64_LITTLE
2133 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
2136 #ifdef HAVE_TARGET_64_BIG
2138 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
2141 #ifdef HAVE_TARGET_32_LITTLE
2143 class Output_data_got<32, false>;
2146 #ifdef HAVE_TARGET_32_BIG
2148 class Output_data_got<32, true>;
2151 #ifdef HAVE_TARGET_64_LITTLE
2153 class Output_data_got<64, false>;
2156 #ifdef HAVE_TARGET_64_BIG
2158 class Output_data_got<64, true>;
2161 } // End namespace gold.