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 gold_assert(all_phdrs_size
== this->data_size());
279 unsigned char* view
= of
->get_output_view(this->offset(),
281 unsigned char* v
= view
;
282 for (Layout::Segment_list::const_iterator p
= this->segment_list_
.begin();
283 p
!= this->segment_list_
.end();
286 elfcpp::Phdr_write
<size
, big_endian
> ophdr(v
);
287 (*p
)->write_header(&ophdr
);
291 gold_assert(v
- view
== all_phdrs_size
);
293 of
->write_output_view(this->offset(), all_phdrs_size
, view
);
296 // Output_file_header methods.
298 Output_file_header::Output_file_header(const Target
* target
,
299 const Symbol_table
* symtab
,
300 const Output_segment_headers
* osh
,
304 segment_header_(osh
),
305 section_header_(NULL
),
309 const int size
= parameters
->get_size();
312 ehdr_size
= elfcpp::Elf_sizes
<32>::ehdr_size
;
314 ehdr_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
318 this->set_data_size(ehdr_size
);
321 // Set the section table information for a file header.
324 Output_file_header::set_section_info(const Output_section_headers
* shdrs
,
325 const Output_section
* shstrtab
)
327 this->section_header_
= shdrs
;
328 this->shstrtab_
= shstrtab
;
331 // Write out the file header.
334 Output_file_header::do_write(Output_file
* of
)
336 gold_assert(this->offset() == 0);
338 if (parameters
->get_size() == 32)
340 if (parameters
->is_big_endian())
342 #ifdef HAVE_TARGET_32_BIG
343 this->do_sized_write
<32, true>(of
);
350 #ifdef HAVE_TARGET_32_LITTLE
351 this->do_sized_write
<32, false>(of
);
357 else if (parameters
->get_size() == 64)
359 if (parameters
->is_big_endian())
361 #ifdef HAVE_TARGET_64_BIG
362 this->do_sized_write
<64, true>(of
);
369 #ifdef HAVE_TARGET_64_LITTLE
370 this->do_sized_write
<64, false>(of
);
380 // Write out the file header with appropriate size and endianess.
382 template<int size
, bool big_endian
>
384 Output_file_header::do_sized_write(Output_file
* of
)
386 gold_assert(this->offset() == 0);
388 int ehdr_size
= elfcpp::Elf_sizes
<size
>::ehdr_size
;
389 unsigned char* view
= of
->get_output_view(0, ehdr_size
);
390 elfcpp::Ehdr_write
<size
, big_endian
> oehdr(view
);
392 unsigned char e_ident
[elfcpp::EI_NIDENT
];
393 memset(e_ident
, 0, elfcpp::EI_NIDENT
);
394 e_ident
[elfcpp::EI_MAG0
] = elfcpp::ELFMAG0
;
395 e_ident
[elfcpp::EI_MAG1
] = elfcpp::ELFMAG1
;
396 e_ident
[elfcpp::EI_MAG2
] = elfcpp::ELFMAG2
;
397 e_ident
[elfcpp::EI_MAG3
] = elfcpp::ELFMAG3
;
399 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS32
;
401 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS64
;
404 e_ident
[elfcpp::EI_DATA
] = (big_endian
405 ? elfcpp::ELFDATA2MSB
406 : elfcpp::ELFDATA2LSB
);
407 e_ident
[elfcpp::EI_VERSION
] = elfcpp::EV_CURRENT
;
408 // FIXME: Some targets may need to set EI_OSABI and EI_ABIVERSION.
409 oehdr
.put_e_ident(e_ident
);
412 if (parameters
->output_is_object())
413 e_type
= elfcpp::ET_REL
;
414 else if (parameters
->output_is_shared())
415 e_type
= elfcpp::ET_DYN
;
417 e_type
= elfcpp::ET_EXEC
;
418 oehdr
.put_e_type(e_type
);
420 oehdr
.put_e_machine(this->target_
->machine_code());
421 oehdr
.put_e_version(elfcpp::EV_CURRENT
);
423 oehdr
.put_e_entry(this->entry
<size
>());
425 oehdr
.put_e_phoff(this->segment_header_
->offset());
426 oehdr
.put_e_shoff(this->section_header_
->offset());
428 // FIXME: The target needs to set the flags.
429 oehdr
.put_e_flags(0);
431 oehdr
.put_e_ehsize(elfcpp::Elf_sizes
<size
>::ehdr_size
);
432 oehdr
.put_e_phentsize(elfcpp::Elf_sizes
<size
>::phdr_size
);
433 oehdr
.put_e_phnum(this->segment_header_
->data_size()
434 / elfcpp::Elf_sizes
<size
>::phdr_size
);
435 oehdr
.put_e_shentsize(elfcpp::Elf_sizes
<size
>::shdr_size
);
436 oehdr
.put_e_shnum(this->section_header_
->data_size()
437 / elfcpp::Elf_sizes
<size
>::shdr_size
);
438 oehdr
.put_e_shstrndx(this->shstrtab_
->out_shndx());
440 of
->write_output_view(0, ehdr_size
, view
);
443 // Return the value to use for the entry address. THIS->ENTRY_ is the
444 // symbol specified on the command line, if any.
447 typename
elfcpp::Elf_types
<size
>::Elf_Addr
448 Output_file_header::entry()
450 const bool should_issue_warning
= (this->entry_
!= NULL
451 && parameters
->output_is_executable());
453 // FIXME: Need to support target specific entry symbol.
454 const char* entry
= this->entry_
;
458 Symbol
* sym
= this->symtab_
->lookup(entry
);
460 typename Sized_symbol
<size
>::Value_type v
;
463 Sized_symbol
<size
>* ssym
;
464 ssym
= this->symtab_
->get_sized_symbol
<size
>(sym
);
465 if (!ssym
->is_defined() && should_issue_warning
)
466 gold_warning("entry symbol '%s' exists but is not defined", entry
);
471 // We couldn't find the entry symbol. See if we can parse it as
472 // a number. This supports, e.g., -e 0x1000.
474 v
= strtoull(entry
, &endptr
, 0);
477 if (should_issue_warning
)
478 gold_warning("cannot find entry symbol '%s'", entry
);
486 // Output_data_const methods.
489 Output_data_const::do_write(Output_file
* of
)
491 of
->write(this->offset(), this->data_
.data(), this->data_
.size());
494 // Output_data_const_buffer methods.
497 Output_data_const_buffer::do_write(Output_file
* of
)
499 of
->write(this->offset(), this->p_
, this->data_size());
502 // Output_section_data methods.
504 // Record the output section, and set the entry size and such.
507 Output_section_data::set_output_section(Output_section
* os
)
509 gold_assert(this->output_section_
== NULL
);
510 this->output_section_
= os
;
511 this->do_adjust_output_section(os
);
514 // Return the section index of the output section.
517 Output_section_data::do_out_shndx() const
519 gold_assert(this->output_section_
!= NULL
);
520 return this->output_section_
->out_shndx();
523 // Output_data_strtab methods.
525 // Set the final data size.
528 Output_data_strtab::set_final_data_size()
530 this->strtab_
->set_string_offsets();
531 this->set_data_size(this->strtab_
->get_strtab_size());
534 // Write out a string table.
537 Output_data_strtab::do_write(Output_file
* of
)
539 this->strtab_
->write(of
, this->offset());
542 // Output_reloc methods.
544 // A reloc against a global symbol.
546 template<bool dynamic
, int size
, bool big_endian
>
547 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
553 : address_(address
), local_sym_index_(GSYM_CODE
), type_(type
),
554 is_relative_(is_relative
), shndx_(INVALID_CODE
)
556 this->u1_
.gsym
= gsym
;
558 if (dynamic
&& !is_relative
)
559 gsym
->set_needs_dynsym_entry();
562 template<bool dynamic
, int size
, bool big_endian
>
563 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
570 : address_(address
), local_sym_index_(GSYM_CODE
), type_(type
),
571 is_relative_(is_relative
), shndx_(shndx
)
573 gold_assert(shndx
!= INVALID_CODE
);
574 this->u1_
.gsym
= gsym
;
575 this->u2_
.relobj
= relobj
;
576 if (dynamic
&& !is_relative
)
577 gsym
->set_needs_dynsym_entry();
580 // A reloc against a local symbol.
582 template<bool dynamic
, int size
, bool big_endian
>
583 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
584 Sized_relobj
<size
, big_endian
>* relobj
,
585 unsigned int local_sym_index
,
590 : address_(address
), local_sym_index_(local_sym_index
), type_(type
),
591 is_relative_(is_relative
), shndx_(INVALID_CODE
)
593 gold_assert(local_sym_index
!= GSYM_CODE
594 && local_sym_index
!= INVALID_CODE
);
595 this->u1_
.relobj
= relobj
;
597 if (dynamic
&& !is_relative
)
598 relobj
->set_needs_output_dynsym_entry(local_sym_index
);
601 template<bool dynamic
, int size
, bool big_endian
>
602 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
603 Sized_relobj
<size
, big_endian
>* relobj
,
604 unsigned int local_sym_index
,
609 : address_(address
), local_sym_index_(local_sym_index
), type_(type
),
610 is_relative_(is_relative
), shndx_(shndx
)
612 gold_assert(local_sym_index
!= GSYM_CODE
613 && local_sym_index
!= INVALID_CODE
);
614 gold_assert(shndx
!= INVALID_CODE
);
615 this->u1_
.relobj
= relobj
;
616 this->u2_
.relobj
= relobj
;
617 if (dynamic
&& !is_relative
)
618 relobj
->set_needs_output_dynsym_entry(local_sym_index
);
621 // A reloc against the STT_SECTION symbol of an output section.
623 template<bool dynamic
, int size
, bool big_endian
>
624 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
629 : address_(address
), local_sym_index_(SECTION_CODE
), type_(type
),
630 is_relative_(false), shndx_(INVALID_CODE
)
635 os
->set_needs_dynsym_index();
638 template<bool dynamic
, int size
, bool big_endian
>
639 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
645 : address_(address
), local_sym_index_(SECTION_CODE
), type_(type
),
646 is_relative_(false), shndx_(shndx
)
648 gold_assert(shndx
!= INVALID_CODE
);
650 this->u2_
.relobj
= relobj
;
652 os
->set_needs_dynsym_index();
655 // Get the symbol index of a relocation.
657 template<bool dynamic
, int size
, bool big_endian
>
659 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::get_symbol_index()
663 switch (this->local_sym_index_
)
669 if (this->u1_
.gsym
== NULL
)
672 index
= this->u1_
.gsym
->dynsym_index();
674 index
= this->u1_
.gsym
->symtab_index();
679 index
= this->u1_
.os
->dynsym_index();
681 index
= this->u1_
.os
->symtab_index();
685 // Relocations without symbols use a symbol index of 0.
691 index
= this->u1_
.relobj
->dynsym_index(this->local_sym_index_
);
693 index
= this->u1_
.relobj
->symtab_index(this->local_sym_index_
);
696 gold_assert(index
!= -1U);
700 // Write out the offset and info fields of a Rel or Rela relocation
703 template<bool dynamic
, int size
, bool big_endian
>
704 template<typename Write_rel
>
706 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::write_rel(
709 Address address
= this->address_
;
710 if (this->shndx_
!= INVALID_CODE
)
712 section_offset_type off
;
713 Output_section
* os
= this->u2_
.relobj
->output_section(this->shndx_
,
715 gold_assert(os
!= NULL
);
717 address
+= os
->address() + off
;
720 address
= os
->output_address(this->u2_
.relobj
, this->shndx_
,
722 gold_assert(address
!= -1U);
725 else if (this->u2_
.od
!= NULL
)
726 address
+= this->u2_
.od
->address();
727 wr
->put_r_offset(address
);
728 unsigned int sym_index
= this->is_relative_
? 0 : this->get_symbol_index();
729 wr
->put_r_info(elfcpp::elf_r_info
<size
>(sym_index
, this->type_
));
732 // Write out a Rel relocation.
734 template<bool dynamic
, int size
, bool big_endian
>
736 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::write(
737 unsigned char* pov
) const
739 elfcpp::Rel_write
<size
, big_endian
> orel(pov
);
740 this->write_rel(&orel
);
743 // Get the value of the symbol referred to by a Rel relocation.
745 template<bool dynamic
, int size
, bool big_endian
>
746 typename
elfcpp::Elf_types
<size
>::Elf_Addr
747 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::symbol_value() const
749 if (this->local_sym_index_
== GSYM_CODE
)
751 const Sized_symbol
<size
>* sym
;
752 sym
= static_cast<const Sized_symbol
<size
>*>(this->u1_
.gsym
);
755 gold_assert(this->local_sym_index_
!= SECTION_CODE
756 && this->local_sym_index_
!= INVALID_CODE
);
757 const Sized_relobj
<size
, big_endian
>* relobj
= this->u1_
.relobj
;
758 return relobj
->local_symbol_value(this->local_sym_index_
);
761 // Write out a Rela relocation.
763 template<bool dynamic
, int size
, bool big_endian
>
765 Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>::write(
766 unsigned char* pov
) const
768 elfcpp::Rela_write
<size
, big_endian
> orel(pov
);
769 this->rel_
.write_rel(&orel
);
770 Addend addend
= this->addend_
;
771 if (rel_
.is_relative())
772 addend
+= rel_
.symbol_value();
773 orel
.put_r_addend(addend
);
776 // Output_data_reloc_base methods.
778 // Adjust the output section.
780 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
782 Output_data_reloc_base
<sh_type
, dynamic
, size
, big_endian
>
783 ::do_adjust_output_section(Output_section
* os
)
785 if (sh_type
== elfcpp::SHT_REL
)
786 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rel_size
);
787 else if (sh_type
== elfcpp::SHT_RELA
)
788 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rela_size
);
792 os
->set_should_link_to_dynsym();
794 os
->set_should_link_to_symtab();
797 // Write out relocation data.
799 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
801 Output_data_reloc_base
<sh_type
, dynamic
, size
, big_endian
>::do_write(
804 const off_t off
= this->offset();
805 const off_t oview_size
= this->data_size();
806 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
808 unsigned char* pov
= oview
;
809 for (typename
Relocs::const_iterator p
= this->relocs_
.begin();
810 p
!= this->relocs_
.end();
817 gold_assert(pov
- oview
== oview_size
);
819 of
->write_output_view(off
, oview_size
, oview
);
821 // We no longer need the relocation entries.
822 this->relocs_
.clear();
825 // Output_data_got::Got_entry methods.
827 // Write out the entry.
829 template<int size
, bool big_endian
>
831 Output_data_got
<size
, big_endian
>::Got_entry::write(unsigned char* pov
) const
835 switch (this->local_sym_index_
)
839 // If the symbol is resolved locally, we need to write out the
840 // link-time value, which will be relocated dynamically by a
841 // RELATIVE relocation.
842 Symbol
* gsym
= this->u_
.gsym
;
843 Sized_symbol
<size
>* sgsym
;
844 // This cast is a bit ugly. We don't want to put a
845 // virtual method in Symbol, because we want Symbol to be
846 // as small as possible.
847 sgsym
= static_cast<Sized_symbol
<size
>*>(gsym
);
848 val
= sgsym
->value();
853 val
= this->u_
.constant
;
857 val
= this->u_
.object
->local_symbol_value(this->local_sym_index_
);
861 elfcpp::Swap
<size
, big_endian
>::writeval(pov
, val
);
864 // Output_data_got methods.
866 // Add an entry for a global symbol to the GOT. This returns true if
867 // this is a new GOT entry, false if the symbol already had a GOT
870 template<int size
, bool big_endian
>
872 Output_data_got
<size
, big_endian
>::add_global(Symbol
* gsym
)
874 if (gsym
->has_got_offset())
877 this->entries_
.push_back(Got_entry(gsym
));
878 this->set_got_size();
879 gsym
->set_got_offset(this->last_got_offset());
883 // Add an entry for a global symbol to the GOT, and add a dynamic
884 // relocation of type R_TYPE for the GOT entry.
885 template<int size
, bool big_endian
>
887 Output_data_got
<size
, big_endian
>::add_global_with_rel(
892 if (gsym
->has_got_offset())
895 this->entries_
.push_back(Got_entry());
896 this->set_got_size();
897 unsigned int got_offset
= this->last_got_offset();
898 gsym
->set_got_offset(got_offset
);
899 rel_dyn
->add_global(gsym
, r_type
, this, got_offset
);
902 template<int size
, bool big_endian
>
904 Output_data_got
<size
, big_endian
>::add_global_with_rela(
909 if (gsym
->has_got_offset())
912 this->entries_
.push_back(Got_entry());
913 this->set_got_size();
914 unsigned int got_offset
= this->last_got_offset();
915 gsym
->set_got_offset(got_offset
);
916 rela_dyn
->add_global(gsym
, r_type
, this, got_offset
, 0);
919 // Add an entry for a local symbol to the GOT. This returns true if
920 // this is a new GOT entry, false if the symbol already has a GOT
923 template<int size
, bool big_endian
>
925 Output_data_got
<size
, big_endian
>::add_local(
926 Sized_relobj
<size
, big_endian
>* object
,
929 if (object
->local_has_got_offset(symndx
))
932 this->entries_
.push_back(Got_entry(object
, symndx
));
933 this->set_got_size();
934 object
->set_local_got_offset(symndx
, this->last_got_offset());
938 // Add an entry for a local symbol to the GOT, and add a dynamic
939 // relocation of type R_TYPE for the GOT entry.
940 template<int size
, bool big_endian
>
942 Output_data_got
<size
, big_endian
>::add_local_with_rel(
943 Sized_relobj
<size
, big_endian
>* object
,
948 if (object
->local_has_got_offset(symndx
))
951 this->entries_
.push_back(Got_entry());
952 this->set_got_size();
953 unsigned int got_offset
= this->last_got_offset();
954 object
->set_local_got_offset(symndx
, got_offset
);
955 rel_dyn
->add_local(object
, symndx
, r_type
, this, got_offset
);
958 template<int size
, bool big_endian
>
960 Output_data_got
<size
, big_endian
>::add_local_with_rela(
961 Sized_relobj
<size
, big_endian
>* object
,
966 if (object
->local_has_got_offset(symndx
))
969 this->entries_
.push_back(Got_entry());
970 this->set_got_size();
971 unsigned int got_offset
= this->last_got_offset();
972 object
->set_local_got_offset(symndx
, got_offset
);
973 rela_dyn
->add_local(object
, symndx
, r_type
, this, got_offset
, 0);
976 // Add an entry (or a pair of entries) for a global TLS symbol to the GOT.
977 // In a pair of entries, the first value in the pair will be used for the
978 // module index, and the second value will be used for the dtv-relative
979 // offset. This returns true if this is a new GOT entry, false if the symbol
980 // already has a GOT entry.
982 template<int size
, bool big_endian
>
984 Output_data_got
<size
, big_endian
>::add_global_tls(Symbol
* gsym
, bool need_pair
)
986 if (gsym
->has_tls_got_offset(need_pair
))
989 this->entries_
.push_back(Got_entry(gsym
));
990 gsym
->set_tls_got_offset(this->last_got_offset(), need_pair
);
992 this->entries_
.push_back(Got_entry(gsym
));
993 this->set_got_size();
997 // Add an entry for a global TLS symbol to the GOT, and add a dynamic
998 // relocation of type R_TYPE.
999 template<int size
, bool big_endian
>
1001 Output_data_got
<size
, big_endian
>::add_global_tls_with_rel(
1004 unsigned int r_type
)
1006 if (gsym
->has_tls_got_offset(false))
1009 this->entries_
.push_back(Got_entry());
1010 this->set_got_size();
1011 unsigned int got_offset
= this->last_got_offset();
1012 gsym
->set_tls_got_offset(got_offset
, false);
1013 rel_dyn
->add_global(gsym
, r_type
, this, got_offset
);
1016 template<int size
, bool big_endian
>
1018 Output_data_got
<size
, big_endian
>::add_global_tls_with_rela(
1021 unsigned int r_type
)
1023 if (gsym
->has_tls_got_offset(false))
1026 this->entries_
.push_back(Got_entry());
1027 this->set_got_size();
1028 unsigned int got_offset
= this->last_got_offset();
1029 gsym
->set_tls_got_offset(got_offset
, false);
1030 rela_dyn
->add_global(gsym
, r_type
, this, got_offset
, 0);
1033 // Add a pair of entries for a global TLS symbol to the GOT, and add
1034 // dynamic relocations of type MOD_R_TYPE and DTV_R_TYPE, respectively.
1035 template<int size
, bool big_endian
>
1037 Output_data_got
<size
, big_endian
>::add_global_tls_with_rel(
1040 unsigned int mod_r_type
,
1041 unsigned int dtv_r_type
)
1043 if (gsym
->has_tls_got_offset(true))
1046 this->entries_
.push_back(Got_entry());
1047 unsigned int got_offset
= this->last_got_offset();
1048 gsym
->set_tls_got_offset(got_offset
, true);
1049 rel_dyn
->add_global(gsym
, mod_r_type
, this, got_offset
);
1051 this->entries_
.push_back(Got_entry());
1052 this->set_got_size();
1053 got_offset
= this->last_got_offset();
1054 rel_dyn
->add_global(gsym
, dtv_r_type
, this, got_offset
);
1057 template<int size
, bool big_endian
>
1059 Output_data_got
<size
, big_endian
>::add_global_tls_with_rela(
1062 unsigned int mod_r_type
,
1063 unsigned int dtv_r_type
)
1065 if (gsym
->has_tls_got_offset(true))
1068 this->entries_
.push_back(Got_entry());
1069 unsigned int got_offset
= this->last_got_offset();
1070 gsym
->set_tls_got_offset(got_offset
, true);
1071 rela_dyn
->add_global(gsym
, mod_r_type
, this, got_offset
, 0);
1073 this->entries_
.push_back(Got_entry());
1074 this->set_got_size();
1075 got_offset
= this->last_got_offset();
1076 rela_dyn
->add_global(gsym
, dtv_r_type
, this, got_offset
, 0);
1079 // Add an entry (or a pair of entries) for a local TLS symbol to the GOT.
1080 // In a pair of entries, the first value in the pair will be used for the
1081 // module index, and the second value will be used for the dtv-relative
1082 // offset. This returns true if this is a new GOT entry, false if the symbol
1083 // already has a GOT entry.
1085 template<int size
, bool big_endian
>
1087 Output_data_got
<size
, big_endian
>::add_local_tls(
1088 Sized_relobj
<size
, big_endian
>* object
,
1089 unsigned int symndx
,
1092 if (object
->local_has_tls_got_offset(symndx
, need_pair
))
1095 this->entries_
.push_back(Got_entry(object
, symndx
));
1096 object
->set_local_tls_got_offset(symndx
, this->last_got_offset(), need_pair
);
1098 this->entries_
.push_back(Got_entry(object
, symndx
));
1099 this->set_got_size();
1103 // Add an entry (or pair of entries) for a local TLS symbol to the GOT,
1104 // and add a dynamic relocation of type R_TYPE for the first GOT entry.
1105 // Because this is a local symbol, the first GOT entry can be relocated
1106 // relative to a section symbol, and the second GOT entry will have an
1107 // dtv-relative value that can be computed at link time.
1108 template<int size
, bool big_endian
>
1110 Output_data_got
<size
, big_endian
>::add_local_tls_with_rel(
1111 Sized_relobj
<size
, big_endian
>* object
,
1112 unsigned int symndx
,
1116 unsigned int r_type
)
1118 if (object
->local_has_tls_got_offset(symndx
, need_pair
))
1121 this->entries_
.push_back(Got_entry());
1122 unsigned int got_offset
= this->last_got_offset();
1123 object
->set_local_tls_got_offset(symndx
, got_offset
, need_pair
);
1124 section_offset_type off
;
1125 Output_section
* os
= object
->output_section(shndx
, &off
);
1126 rel_dyn
->add_output_section(os
, r_type
, this, got_offset
);
1128 // The second entry of the pair will be statically initialized
1129 // with the TLS offset of the symbol.
1131 this->entries_
.push_back(Got_entry(object
, symndx
));
1133 this->set_got_size();
1136 template<int size
, bool big_endian
>
1138 Output_data_got
<size
, big_endian
>::add_local_tls_with_rela(
1139 Sized_relobj
<size
, big_endian
>* object
,
1140 unsigned int symndx
,
1144 unsigned int r_type
)
1146 if (object
->local_has_tls_got_offset(symndx
, need_pair
))
1149 this->entries_
.push_back(Got_entry());
1150 unsigned int got_offset
= this->last_got_offset();
1151 object
->set_local_tls_got_offset(symndx
, got_offset
, need_pair
);
1152 section_offset_type off
;
1153 Output_section
* os
= object
->output_section(shndx
, &off
);
1154 rela_dyn
->add_output_section(os
, r_type
, this, got_offset
, 0);
1156 // The second entry of the pair will be statically initialized
1157 // with the TLS offset of the symbol.
1159 this->entries_
.push_back(Got_entry(object
, symndx
));
1161 this->set_got_size();
1164 // Write out the GOT.
1166 template<int size
, bool big_endian
>
1168 Output_data_got
<size
, big_endian
>::do_write(Output_file
* of
)
1170 const int add
= size
/ 8;
1172 const off_t off
= this->offset();
1173 const off_t oview_size
= this->data_size();
1174 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
1176 unsigned char* pov
= oview
;
1177 for (typename
Got_entries::const_iterator p
= this->entries_
.begin();
1178 p
!= this->entries_
.end();
1185 gold_assert(pov
- oview
== oview_size
);
1187 of
->write_output_view(off
, oview_size
, oview
);
1189 // We no longer need the GOT entries.
1190 this->entries_
.clear();
1193 // Output_data_dynamic::Dynamic_entry methods.
1195 // Write out the entry.
1197 template<int size
, bool big_endian
>
1199 Output_data_dynamic::Dynamic_entry::write(
1201 const Stringpool
* pool
1202 ACCEPT_SIZE_ENDIAN
) const
1204 typename
elfcpp::Elf_types
<size
>::Elf_WXword val
;
1205 switch (this->classification_
)
1207 case DYNAMIC_NUMBER
:
1211 case DYNAMIC_SECTION_ADDRESS
:
1212 val
= this->u_
.od
->address();
1215 case DYNAMIC_SECTION_SIZE
:
1216 val
= this->u_
.od
->data_size();
1219 case DYNAMIC_SYMBOL
:
1221 const Sized_symbol
<size
>* s
=
1222 static_cast<const Sized_symbol
<size
>*>(this->u_
.sym
);
1227 case DYNAMIC_STRING
:
1228 val
= pool
->get_offset(this->u_
.str
);
1235 elfcpp::Dyn_write
<size
, big_endian
> dw(pov
);
1236 dw
.put_d_tag(this->tag_
);
1240 // Output_data_dynamic methods.
1242 // Adjust the output section to set the entry size.
1245 Output_data_dynamic::do_adjust_output_section(Output_section
* os
)
1247 if (parameters
->get_size() == 32)
1248 os
->set_entsize(elfcpp::Elf_sizes
<32>::dyn_size
);
1249 else if (parameters
->get_size() == 64)
1250 os
->set_entsize(elfcpp::Elf_sizes
<64>::dyn_size
);
1255 // Set the final data size.
1258 Output_data_dynamic::set_final_data_size()
1260 // Add the terminating entry.
1261 this->add_constant(elfcpp::DT_NULL
, 0);
1264 if (parameters
->get_size() == 32)
1265 dyn_size
= elfcpp::Elf_sizes
<32>::dyn_size
;
1266 else if (parameters
->get_size() == 64)
1267 dyn_size
= elfcpp::Elf_sizes
<64>::dyn_size
;
1270 this->set_data_size(this->entries_
.size() * dyn_size
);
1273 // Write out the dynamic entries.
1276 Output_data_dynamic::do_write(Output_file
* of
)
1278 if (parameters
->get_size() == 32)
1280 if (parameters
->is_big_endian())
1282 #ifdef HAVE_TARGET_32_BIG
1283 this->sized_write
<32, true>(of
);
1290 #ifdef HAVE_TARGET_32_LITTLE
1291 this->sized_write
<32, false>(of
);
1297 else if (parameters
->get_size() == 64)
1299 if (parameters
->is_big_endian())
1301 #ifdef HAVE_TARGET_64_BIG
1302 this->sized_write
<64, true>(of
);
1309 #ifdef HAVE_TARGET_64_LITTLE
1310 this->sized_write
<64, false>(of
);
1320 template<int size
, bool big_endian
>
1322 Output_data_dynamic::sized_write(Output_file
* of
)
1324 const int dyn_size
= elfcpp::Elf_sizes
<size
>::dyn_size
;
1326 const off_t offset
= this->offset();
1327 const off_t oview_size
= this->data_size();
1328 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1330 unsigned char* pov
= oview
;
1331 for (typename
Dynamic_entries::const_iterator p
= this->entries_
.begin();
1332 p
!= this->entries_
.end();
1335 p
->write
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
)(
1336 pov
, this->pool_
SELECT_SIZE_ENDIAN(size
, big_endian
));
1340 gold_assert(pov
- oview
== oview_size
);
1342 of
->write_output_view(offset
, oview_size
, oview
);
1344 // We no longer need the dynamic entries.
1345 this->entries_
.clear();
1348 // Output_section::Input_section methods.
1350 // Return the data size. For an input section we store the size here.
1351 // For an Output_section_data, we have to ask it for the size.
1354 Output_section::Input_section::data_size() const
1356 if (this->is_input_section())
1357 return this->u1_
.data_size
;
1359 return this->u2_
.posd
->data_size();
1362 // Set the address and file offset.
1365 Output_section::Input_section::set_address_and_file_offset(
1368 off_t section_file_offset
)
1370 if (this->is_input_section())
1371 this->u2_
.object
->set_section_offset(this->shndx_
,
1372 file_offset
- section_file_offset
);
1374 this->u2_
.posd
->set_address_and_file_offset(address
, file_offset
);
1377 // Reset the address and file offset.
1380 Output_section::Input_section::reset_address_and_file_offset()
1382 if (!this->is_input_section())
1383 this->u2_
.posd
->reset_address_and_file_offset();
1386 // Finalize the data size.
1389 Output_section::Input_section::finalize_data_size()
1391 if (!this->is_input_section())
1392 this->u2_
.posd
->finalize_data_size();
1395 // Try to turn an input offset into an output offset. We want to
1396 // return the output offset relative to the start of this
1397 // Input_section in the output section.
1400 Output_section::Input_section::output_offset(
1401 const Relobj
* object
,
1403 section_offset_type offset
,
1404 section_offset_type
*poutput
) const
1406 if (!this->is_input_section())
1407 return this->u2_
.posd
->output_offset(object
, shndx
, offset
, poutput
);
1410 if (this->shndx_
!= shndx
|| this->u2_
.object
!= object
)
1417 // Return whether this is the merge section for the input section
1421 Output_section::Input_section::is_merge_section_for(const Relobj
* object
,
1422 unsigned int shndx
) const
1424 if (this->is_input_section())
1426 return this->u2_
.posd
->is_merge_section_for(object
, shndx
);
1429 // Write out the data. We don't have to do anything for an input
1430 // section--they are handled via Object::relocate--but this is where
1431 // we write out the data for an Output_section_data.
1434 Output_section::Input_section::write(Output_file
* of
)
1436 if (!this->is_input_section())
1437 this->u2_
.posd
->write(of
);
1440 // Write the data to a buffer. As for write(), we don't have to do
1441 // anything for an input section.
1444 Output_section::Input_section::write_to_buffer(unsigned char* buffer
)
1446 if (!this->is_input_section())
1447 this->u2_
.posd
->write_to_buffer(buffer
);
1450 // Output_section methods.
1452 // Construct an Output_section. NAME will point into a Stringpool.
1454 Output_section::Output_section(const char* name
, elfcpp::Elf_Word type
,
1455 elfcpp::Elf_Xword flags
)
1460 link_section_(NULL
),
1462 info_section_(NULL
),
1470 first_input_offset_(0),
1472 postprocessing_buffer_(NULL
),
1473 needs_symtab_index_(false),
1474 needs_dynsym_index_(false),
1475 should_link_to_symtab_(false),
1476 should_link_to_dynsym_(false),
1477 after_input_sections_(false),
1478 requires_postprocessing_(false),
1479 found_in_sections_clause_(false),
1480 has_load_address_(false),
1483 // An unallocated section has no address. Forcing this means that
1484 // we don't need special treatment for symbols defined in debug
1486 if ((flags
& elfcpp::SHF_ALLOC
) == 0)
1487 this->set_address(0);
1490 Output_section::~Output_section()
1494 // Set the entry size.
1497 Output_section::set_entsize(uint64_t v
)
1499 if (this->entsize_
== 0)
1502 gold_assert(this->entsize_
== v
);
1505 // Add the input section SHNDX, with header SHDR, named SECNAME, in
1506 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
1507 // relocation section which applies to this section, or 0 if none, or
1508 // -1U if more than one. Return the offset of the input section
1509 // within the output section. Return -1 if the input section will
1510 // receive special handling. In the normal case we don't always keep
1511 // track of input sections for an Output_section. Instead, each
1512 // Object keeps track of the Output_section for each of its input
1513 // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
1514 // track of input sections here; this is used when SECTIONS appears in
1517 template<int size
, bool big_endian
>
1519 Output_section::add_input_section(Sized_relobj
<size
, big_endian
>* object
,
1521 const char* secname
,
1522 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
1523 unsigned int reloc_shndx
,
1524 bool have_sections_script
)
1526 elfcpp::Elf_Xword addralign
= shdr
.get_sh_addralign();
1527 if ((addralign
& (addralign
- 1)) != 0)
1529 object
->error(_("invalid alignment %lu for section \"%s\""),
1530 static_cast<unsigned long>(addralign
), secname
);
1534 if (addralign
> this->addralign_
)
1535 this->addralign_
= addralign
;
1537 typename
elfcpp::Elf_types
<size
>::Elf_WXword sh_flags
= shdr
.get_sh_flags();
1538 this->flags_
|= (sh_flags
1539 & (elfcpp::SHF_WRITE
1541 | elfcpp::SHF_EXECINSTR
));
1543 uint64_t entsize
= shdr
.get_sh_entsize();
1545 // .debug_str is a mergeable string section, but is not always so
1546 // marked by compilers. Mark manually here so we can optimize.
1547 if (strcmp(secname
, ".debug_str") == 0)
1549 sh_flags
|= (elfcpp::SHF_MERGE
| elfcpp::SHF_STRINGS
);
1553 // If this is a SHF_MERGE section, we pass all the input sections to
1554 // a Output_data_merge. We don't try to handle relocations for such
1556 if ((sh_flags
& elfcpp::SHF_MERGE
) != 0
1557 && reloc_shndx
== 0)
1559 if (this->add_merge_input_section(object
, shndx
, sh_flags
,
1560 entsize
, addralign
))
1562 // Tell the relocation routines that they need to call the
1563 // output_offset method to determine the final address.
1568 off_t offset_in_section
= this->current_data_size_for_child();
1569 off_t aligned_offset_in_section
= align_address(offset_in_section
,
1572 if (aligned_offset_in_section
> offset_in_section
1573 && !have_sections_script
1574 && (sh_flags
& elfcpp::SHF_EXECINSTR
) != 0
1575 && object
->target()->has_code_fill())
1577 // We need to add some fill data. Using fill_list_ when
1578 // possible is an optimization, since we will often have fill
1579 // sections without input sections.
1580 off_t fill_len
= aligned_offset_in_section
- offset_in_section
;
1581 if (this->input_sections_
.empty())
1582 this->fills_
.push_back(Fill(offset_in_section
, fill_len
));
1585 // FIXME: When relaxing, the size needs to adjust to
1586 // maintain a constant alignment.
1587 std::string
fill_data(object
->target()->code_fill(fill_len
));
1588 Output_data_const
* odc
= new Output_data_const(fill_data
, 1);
1589 this->input_sections_
.push_back(Input_section(odc
));
1593 this->set_current_data_size_for_child(aligned_offset_in_section
1594 + shdr
.get_sh_size());
1596 // We need to keep track of this section if we are already keeping
1597 // track of sections, or if we are relaxing. FIXME: Add test for
1599 if (have_sections_script
|| !this->input_sections_
.empty())
1600 this->input_sections_
.push_back(Input_section(object
, shndx
,
1604 return aligned_offset_in_section
;
1607 // Add arbitrary data to an output section.
1610 Output_section::add_output_section_data(Output_section_data
* posd
)
1612 Input_section
inp(posd
);
1613 this->add_output_section_data(&inp
);
1615 if (posd
->is_data_size_valid())
1617 off_t offset_in_section
= this->current_data_size_for_child();
1618 off_t aligned_offset_in_section
= align_address(offset_in_section
,
1620 this->set_current_data_size_for_child(aligned_offset_in_section
1621 + posd
->data_size());
1625 // Add arbitrary data to an output section by Input_section.
1628 Output_section::add_output_section_data(Input_section
* inp
)
1630 if (this->input_sections_
.empty())
1631 this->first_input_offset_
= this->current_data_size_for_child();
1633 this->input_sections_
.push_back(*inp
);
1635 uint64_t addralign
= inp
->addralign();
1636 if (addralign
> this->addralign_
)
1637 this->addralign_
= addralign
;
1639 inp
->set_output_section(this);
1642 // Add a merge section to an output section.
1645 Output_section::add_output_merge_section(Output_section_data
* posd
,
1646 bool is_string
, uint64_t entsize
)
1648 Input_section
inp(posd
, is_string
, entsize
);
1649 this->add_output_section_data(&inp
);
1652 // Add an input section to a SHF_MERGE section.
1655 Output_section::add_merge_input_section(Relobj
* object
, unsigned int shndx
,
1656 uint64_t flags
, uint64_t entsize
,
1659 bool is_string
= (flags
& elfcpp::SHF_STRINGS
) != 0;
1661 // We only merge strings if the alignment is not more than the
1662 // character size. This could be handled, but it's unusual.
1663 if (is_string
&& addralign
> entsize
)
1666 Input_section_list::iterator p
;
1667 for (p
= this->input_sections_
.begin();
1668 p
!= this->input_sections_
.end();
1670 if (p
->is_merge_section(is_string
, entsize
, addralign
))
1672 p
->add_input_section(object
, shndx
);
1676 // We handle the actual constant merging in Output_merge_data or
1677 // Output_merge_string_data.
1678 Output_section_data
* posd
;
1680 posd
= new Output_merge_data(entsize
, addralign
);
1686 posd
= new Output_merge_string
<char>(addralign
);
1689 posd
= new Output_merge_string
<uint16_t>(addralign
);
1692 posd
= new Output_merge_string
<uint32_t>(addralign
);
1699 this->add_output_merge_section(posd
, is_string
, entsize
);
1700 posd
->add_input_section(object
, shndx
);
1705 // Given an address OFFSET relative to the start of input section
1706 // SHNDX in OBJECT, return whether this address is being included in
1707 // the final link. This should only be called if SHNDX in OBJECT has
1708 // a special mapping.
1711 Output_section::is_input_address_mapped(const Relobj
* object
,
1715 gold_assert(object
->is_section_specially_mapped(shndx
));
1717 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
1718 p
!= this->input_sections_
.end();
1721 section_offset_type output_offset
;
1722 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
1723 return output_offset
!= -1;
1726 // By default we assume that the address is mapped. This should
1727 // only be called after we have passed all sections to Layout. At
1728 // that point we should know what we are discarding.
1732 // Given an address OFFSET relative to the start of input section
1733 // SHNDX in object OBJECT, return the output offset relative to the
1734 // start of the input section in the output section. This should only
1735 // be called if SHNDX in OBJECT has a special mapping.
1738 Output_section::output_offset(const Relobj
* object
, unsigned int shndx
,
1739 section_offset_type offset
) const
1741 gold_assert(object
->is_section_specially_mapped(shndx
));
1742 // This can only be called meaningfully when layout is complete.
1743 gold_assert(Output_data::is_layout_complete());
1745 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
1746 p
!= this->input_sections_
.end();
1749 section_offset_type output_offset
;
1750 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
1751 return output_offset
;
1756 // Return the output virtual address of OFFSET relative to the start
1757 // of input section SHNDX in object OBJECT.
1760 Output_section::output_address(const Relobj
* object
, unsigned int shndx
,
1763 gold_assert(object
->is_section_specially_mapped(shndx
));
1765 uint64_t addr
= this->address() + this->first_input_offset_
;
1766 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
1767 p
!= this->input_sections_
.end();
1770 addr
= align_address(addr
, p
->addralign());
1771 section_offset_type output_offset
;
1772 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
1774 if (output_offset
== -1)
1776 return addr
+ output_offset
;
1778 addr
+= p
->data_size();
1781 // If we get here, it means that we don't know the mapping for this
1782 // input section. This might happen in principle if
1783 // add_input_section were called before add_output_section_data.
1784 // But it should never actually happen.
1789 // Return the output address of the start of the merged section for
1790 // input section SHNDX in object OBJECT.
1793 Output_section::starting_output_address(const Relobj
* object
,
1794 unsigned int shndx
) const
1796 gold_assert(object
->is_section_specially_mapped(shndx
));
1798 uint64_t addr
= this->address() + this->first_input_offset_
;
1799 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
1800 p
!= this->input_sections_
.end();
1803 addr
= align_address(addr
, p
->addralign());
1805 // It would be nice if we could use the existing output_offset
1806 // method to get the output offset of input offset 0.
1807 // Unfortunately we don't know for sure that input offset 0 is
1809 if (p
->is_merge_section_for(object
, shndx
))
1812 addr
+= p
->data_size();
1817 // Set the data size of an Output_section. This is where we handle
1818 // setting the addresses of any Output_section_data objects.
1821 Output_section::set_final_data_size()
1823 if (this->input_sections_
.empty())
1825 this->set_data_size(this->current_data_size_for_child());
1829 uint64_t address
= this->address();
1830 off_t startoff
= this->offset();
1831 off_t off
= startoff
+ this->first_input_offset_
;
1832 for (Input_section_list::iterator p
= this->input_sections_
.begin();
1833 p
!= this->input_sections_
.end();
1836 off
= align_address(off
, p
->addralign());
1837 p
->set_address_and_file_offset(address
+ (off
- startoff
), off
,
1839 off
+= p
->data_size();
1842 this->set_data_size(off
- startoff
);
1845 // Reset the address and file offset.
1848 Output_section::do_reset_address_and_file_offset()
1850 for (Input_section_list::iterator p
= this->input_sections_
.begin();
1851 p
!= this->input_sections_
.end();
1853 p
->reset_address_and_file_offset();
1856 // Set the TLS offset. Called only for SHT_TLS sections.
1859 Output_section::do_set_tls_offset(uint64_t tls_base
)
1861 this->tls_offset_
= this->address() - tls_base
;
1864 // Write the section header to *OSHDR.
1866 template<int size
, bool big_endian
>
1868 Output_section::write_header(const Layout
* layout
,
1869 const Stringpool
* secnamepool
,
1870 elfcpp::Shdr_write
<size
, big_endian
>* oshdr
) const
1872 oshdr
->put_sh_name(secnamepool
->get_offset(this->name_
));
1873 oshdr
->put_sh_type(this->type_
);
1874 oshdr
->put_sh_flags(this->flags_
);
1875 oshdr
->put_sh_addr(this->address());
1876 oshdr
->put_sh_offset(this->offset());
1877 oshdr
->put_sh_size(this->data_size());
1878 if (this->link_section_
!= NULL
)
1879 oshdr
->put_sh_link(this->link_section_
->out_shndx());
1880 else if (this->should_link_to_symtab_
)
1881 oshdr
->put_sh_link(layout
->symtab_section()->out_shndx());
1882 else if (this->should_link_to_dynsym_
)
1883 oshdr
->put_sh_link(layout
->dynsym_section()->out_shndx());
1885 oshdr
->put_sh_link(this->link_
);
1886 if (this->info_section_
!= NULL
)
1887 oshdr
->put_sh_info(this->info_section_
->out_shndx());
1889 oshdr
->put_sh_info(this->info_
);
1890 oshdr
->put_sh_addralign(this->addralign_
);
1891 oshdr
->put_sh_entsize(this->entsize_
);
1894 // Write out the data. For input sections the data is written out by
1895 // Object::relocate, but we have to handle Output_section_data objects
1899 Output_section::do_write(Output_file
* of
)
1901 gold_assert(!this->requires_postprocessing());
1903 off_t output_section_file_offset
= this->offset();
1904 for (Fill_list::iterator p
= this->fills_
.begin();
1905 p
!= this->fills_
.end();
1908 std::string
fill_data(parameters
->target()->code_fill(p
->length()));
1909 of
->write(output_section_file_offset
+ p
->section_offset(),
1910 fill_data
.data(), fill_data
.size());
1913 for (Input_section_list::iterator p
= this->input_sections_
.begin();
1914 p
!= this->input_sections_
.end();
1919 // If a section requires postprocessing, create the buffer to use.
1922 Output_section::create_postprocessing_buffer()
1924 gold_assert(this->requires_postprocessing());
1926 if (this->postprocessing_buffer_
!= NULL
)
1929 if (!this->input_sections_
.empty())
1931 off_t off
= this->first_input_offset_
;
1932 for (Input_section_list::iterator p
= this->input_sections_
.begin();
1933 p
!= this->input_sections_
.end();
1936 off
= align_address(off
, p
->addralign());
1937 p
->finalize_data_size();
1938 off
+= p
->data_size();
1940 this->set_current_data_size_for_child(off
);
1943 off_t buffer_size
= this->current_data_size_for_child();
1944 this->postprocessing_buffer_
= new unsigned char[buffer_size
];
1947 // Write all the data of an Output_section into the postprocessing
1948 // buffer. This is used for sections which require postprocessing,
1949 // such as compression. Input sections are handled by
1950 // Object::Relocate.
1953 Output_section::write_to_postprocessing_buffer()
1955 gold_assert(this->requires_postprocessing());
1957 Target
* target
= parameters
->target();
1958 unsigned char* buffer
= this->postprocessing_buffer();
1959 for (Fill_list::iterator p
= this->fills_
.begin();
1960 p
!= this->fills_
.end();
1963 std::string
fill_data(target
->code_fill(p
->length()));
1964 memcpy(buffer
+ p
->section_offset(), fill_data
.data(),
1968 off_t off
= this->first_input_offset_
;
1969 for (Input_section_list::iterator p
= this->input_sections_
.begin();
1970 p
!= this->input_sections_
.end();
1973 off
= align_address(off
, p
->addralign());
1974 p
->write_to_buffer(buffer
+ off
);
1975 off
+= p
->data_size();
1979 // Get the input sections for linker script processing. We leave
1980 // behind the Output_section_data entries. Note that this may be
1981 // slightly incorrect for merge sections. We will leave them behind,
1982 // but it is possible that the script says that they should follow
1983 // some other input sections, as in:
1984 // .rodata { *(.rodata) *(.rodata.cst*) }
1985 // For that matter, we don't handle this correctly:
1986 // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
1987 // With luck this will never matter.
1990 Output_section::get_input_sections(
1992 const std::string
& fill
,
1993 std::list
<std::pair
<Relobj
*, unsigned int> >* input_sections
)
1995 uint64_t orig_address
= address
;
1997 address
= align_address(address
, this->addralign());
1999 Input_section_list remaining
;
2000 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2001 p
!= this->input_sections_
.end();
2004 if (p
->is_input_section())
2005 input_sections
->push_back(std::make_pair(p
->relobj(), p
->shndx()));
2008 uint64_t aligned_address
= align_address(address
, p
->addralign());
2009 if (aligned_address
!= address
&& !fill
.empty())
2011 section_size_type length
=
2012 convert_to_section_size_type(aligned_address
- address
);
2013 std::string this_fill
;
2014 this_fill
.reserve(length
);
2015 while (this_fill
.length() + fill
.length() <= length
)
2017 if (this_fill
.length() < length
)
2018 this_fill
.append(fill
, 0, length
- this_fill
.length());
2020 Output_section_data
* posd
= new Output_data_const(this_fill
, 0);
2021 remaining
.push_back(Input_section(posd
));
2023 address
= aligned_address
;
2025 remaining
.push_back(*p
);
2027 p
->finalize_data_size();
2028 address
+= p
->data_size();
2032 this->input_sections_
.swap(remaining
);
2033 this->first_input_offset_
= 0;
2035 uint64_t data_size
= address
- orig_address
;
2036 this->set_current_data_size_for_child(data_size
);
2040 // Add an input section from a script.
2043 Output_section::add_input_section_for_script(Relobj
* object
,
2048 if (addralign
> this->addralign_
)
2049 this->addralign_
= addralign
;
2051 off_t offset_in_section
= this->current_data_size_for_child();
2052 off_t aligned_offset_in_section
= align_address(offset_in_section
,
2055 this->set_current_data_size_for_child(aligned_offset_in_section
2058 this->input_sections_
.push_back(Input_section(object
, shndx
,
2059 data_size
, addralign
));
2062 // Print stats for merge sections to stderr.
2065 Output_section::print_merge_stats()
2067 Input_section_list::iterator p
;
2068 for (p
= this->input_sections_
.begin();
2069 p
!= this->input_sections_
.end();
2071 p
->print_merge_stats(this->name_
);
2074 // Output segment methods.
2076 Output_segment::Output_segment(elfcpp::Elf_Word type
, elfcpp::Elf_Word flags
)
2088 is_max_align_known_(false),
2089 are_addresses_set_(false)
2093 // Add an Output_section to an Output_segment.
2096 Output_segment::add_output_section(Output_section
* os
,
2097 elfcpp::Elf_Word seg_flags
,
2100 gold_assert((os
->flags() & elfcpp::SHF_ALLOC
) != 0);
2101 gold_assert(!this->is_max_align_known_
);
2103 // Update the segment flags.
2104 this->flags_
|= seg_flags
;
2106 Output_segment::Output_data_list
* pdl
;
2107 if (os
->type() == elfcpp::SHT_NOBITS
)
2108 pdl
= &this->output_bss_
;
2110 pdl
= &this->output_data_
;
2112 // So that PT_NOTE segments will work correctly, we need to ensure
2113 // that all SHT_NOTE sections are adjacent. This will normally
2114 // happen automatically, because all the SHT_NOTE input sections
2115 // will wind up in the same output section. However, it is possible
2116 // for multiple SHT_NOTE input sections to have different section
2117 // flags, and thus be in different output sections, but for the
2118 // different section flags to map into the same segment flags and
2119 // thus the same output segment.
2121 // Note that while there may be many input sections in an output
2122 // section, there are normally only a few output sections in an
2123 // output segment. This loop is expected to be fast.
2125 if (os
->type() == elfcpp::SHT_NOTE
&& !pdl
->empty())
2127 Output_segment::Output_data_list::iterator p
= pdl
->end();
2131 if ((*p
)->is_section_type(elfcpp::SHT_NOTE
))
2133 // We don't worry about the FRONT parameter.
2139 while (p
!= pdl
->begin());
2142 // Similarly, so that PT_TLS segments will work, we need to group
2143 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
2144 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
2145 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
2146 // correctly. SHF_TLS sections get added to both a PT_LOAD segment
2147 // and the PT_TLS segment -- we do this grouping only for the
2149 if (this->type_
!= elfcpp::PT_TLS
2150 && (os
->flags() & elfcpp::SHF_TLS
) != 0
2151 && !this->output_data_
.empty())
2153 pdl
= &this->output_data_
;
2154 bool nobits
= os
->type() == elfcpp::SHT_NOBITS
;
2155 bool sawtls
= false;
2156 Output_segment::Output_data_list::iterator p
= pdl
->end();
2161 if ((*p
)->is_section_flag_set(elfcpp::SHF_TLS
))
2164 // Put a NOBITS section after the first TLS section.
2165 // But a PROGBITS section after the first TLS/PROGBITS
2167 insert
= nobits
|| !(*p
)->is_section_type(elfcpp::SHT_NOBITS
);
2171 // If we've gone past the TLS sections, but we've seen a
2172 // TLS section, then we need to insert this section now.
2178 // We don't worry about the FRONT parameter.
2184 while (p
!= pdl
->begin());
2186 // There are no TLS sections yet; put this one at the requested
2187 // location in the section list.
2191 pdl
->push_front(os
);
2196 // Add an Output_data (which is not an Output_section) to the start of
2200 Output_segment::add_initial_output_data(Output_data
* od
)
2202 gold_assert(!this->is_max_align_known_
);
2203 this->output_data_
.push_front(od
);
2206 // Return the maximum alignment of the Output_data in Output_segment.
2209 Output_segment::maximum_alignment()
2211 if (!this->is_max_align_known_
)
2215 addralign
= Output_segment::maximum_alignment_list(&this->output_data_
);
2216 if (addralign
> this->max_align_
)
2217 this->max_align_
= addralign
;
2219 addralign
= Output_segment::maximum_alignment_list(&this->output_bss_
);
2220 if (addralign
> this->max_align_
)
2221 this->max_align_
= addralign
;
2223 this->is_max_align_known_
= true;
2226 return this->max_align_
;
2229 // Return the maximum alignment of a list of Output_data.
2232 Output_segment::maximum_alignment_list(const Output_data_list
* pdl
)
2235 for (Output_data_list::const_iterator p
= pdl
->begin();
2239 uint64_t addralign
= (*p
)->addralign();
2240 if (addralign
> ret
)
2246 // Return the number of dynamic relocs applied to this segment.
2249 Output_segment::dynamic_reloc_count() const
2251 return (this->dynamic_reloc_count_list(&this->output_data_
)
2252 + this->dynamic_reloc_count_list(&this->output_bss_
));
2255 // Return the number of dynamic relocs applied to an Output_data_list.
2258 Output_segment::dynamic_reloc_count_list(const Output_data_list
* pdl
) const
2260 unsigned int count
= 0;
2261 for (Output_data_list::const_iterator p
= pdl
->begin();
2264 count
+= (*p
)->dynamic_reloc_count();
2268 // Set the section addresses for an Output_segment. If RESET is true,
2269 // reset the addresses first. ADDR is the address and *POFF is the
2270 // file offset. Set the section indexes starting with *PSHNDX.
2271 // Return the address of the immediately following segment. Update
2272 // *POFF and *PSHNDX.
2275 Output_segment::set_section_addresses(bool reset
, uint64_t addr
, off_t
* poff
,
2276 unsigned int* pshndx
)
2278 gold_assert(this->type_
== elfcpp::PT_LOAD
);
2280 if (!reset
&& this->are_addresses_set_
)
2282 gold_assert(this->paddr_
== addr
);
2283 addr
= this->vaddr_
;
2287 this->vaddr_
= addr
;
2288 this->paddr_
= addr
;
2289 this->are_addresses_set_
= true;
2292 off_t orig_off
= *poff
;
2293 this->offset_
= orig_off
;
2295 addr
= this->set_section_list_addresses(reset
, &this->output_data_
,
2296 addr
, poff
, pshndx
);
2297 this->filesz_
= *poff
- orig_off
;
2301 uint64_t ret
= this->set_section_list_addresses(reset
, &this->output_bss_
,
2302 addr
, poff
, pshndx
);
2303 this->memsz_
= *poff
- orig_off
;
2305 // Ignore the file offset adjustments made by the BSS Output_data
2312 // Set the addresses and file offsets in a list of Output_data
2316 Output_segment::set_section_list_addresses(bool reset
, Output_data_list
* pdl
,
2317 uint64_t addr
, off_t
* poff
,
2318 unsigned int* pshndx
)
2320 off_t startoff
= *poff
;
2322 off_t off
= startoff
;
2323 for (Output_data_list::iterator p
= pdl
->begin();
2327 off
= align_address(off
, (*p
)->addralign());
2330 (*p
)->reset_address_and_file_offset();
2332 // When using a linker script the section will most likely
2333 // already have an address.
2334 if (!(*p
)->is_address_valid())
2335 (*p
)->set_address_and_file_offset(addr
+ (off
- startoff
), off
);
2338 // The script may have inserted a skip forward, but it
2339 // better not have moved backward.
2340 gold_assert((*p
)->address() >= addr
);
2341 off
= startoff
+ ((*p
)->address() - addr
);
2342 (*p
)->set_file_offset(off
);
2343 (*p
)->finalize_data_size();
2346 // Unless this is a PT_TLS segment, we want to ignore the size
2347 // of a SHF_TLS/SHT_NOBITS section. Such a section does not
2348 // affect the size of a PT_LOAD segment.
2349 if (this->type_
== elfcpp::PT_TLS
2350 || !(*p
)->is_section_flag_set(elfcpp::SHF_TLS
)
2351 || !(*p
)->is_section_type(elfcpp::SHT_NOBITS
))
2352 off
+= (*p
)->data_size();
2354 if ((*p
)->is_section())
2356 (*p
)->set_out_shndx(*pshndx
);
2362 return addr
+ (off
- startoff
);
2365 // For a non-PT_LOAD segment, set the offset from the sections, if
2369 Output_segment::set_offset()
2371 gold_assert(this->type_
!= elfcpp::PT_LOAD
);
2373 gold_assert(!this->are_addresses_set_
);
2375 if (this->output_data_
.empty() && this->output_bss_
.empty())
2379 this->are_addresses_set_
= true;
2381 this->min_p_align_
= 0;
2387 const Output_data
* first
;
2388 if (this->output_data_
.empty())
2389 first
= this->output_bss_
.front();
2391 first
= this->output_data_
.front();
2392 this->vaddr_
= first
->address();
2393 this->paddr_
= (first
->has_load_address()
2394 ? first
->load_address()
2396 this->are_addresses_set_
= true;
2397 this->offset_
= first
->offset();
2399 if (this->output_data_
.empty())
2403 const Output_data
* last_data
= this->output_data_
.back();
2404 this->filesz_
= (last_data
->address()
2405 + last_data
->data_size()
2409 const Output_data
* last
;
2410 if (this->output_bss_
.empty())
2411 last
= this->output_data_
.back();
2413 last
= this->output_bss_
.back();
2414 this->memsz_
= (last
->address()
2419 // Set the TLS offsets of the sections in the PT_TLS segment.
2422 Output_segment::set_tls_offsets()
2424 gold_assert(this->type_
== elfcpp::PT_TLS
);
2426 for (Output_data_list::iterator p
= this->output_data_
.begin();
2427 p
!= this->output_data_
.end();
2429 (*p
)->set_tls_offset(this->vaddr_
);
2431 for (Output_data_list::iterator p
= this->output_bss_
.begin();
2432 p
!= this->output_bss_
.end();
2434 (*p
)->set_tls_offset(this->vaddr_
);
2437 // Return the address of the first section.
2440 Output_segment::first_section_load_address() const
2442 for (Output_data_list::const_iterator p
= this->output_data_
.begin();
2443 p
!= this->output_data_
.end();
2445 if ((*p
)->is_section())
2446 return (*p
)->has_load_address() ? (*p
)->load_address() : (*p
)->address();
2448 for (Output_data_list::const_iterator p
= this->output_bss_
.begin();
2449 p
!= this->output_bss_
.end();
2451 if ((*p
)->is_section())
2452 return (*p
)->has_load_address() ? (*p
)->load_address() : (*p
)->address();
2457 // Return the number of Output_sections in an Output_segment.
2460 Output_segment::output_section_count() const
2462 return (this->output_section_count_list(&this->output_data_
)
2463 + this->output_section_count_list(&this->output_bss_
));
2466 // Return the number of Output_sections in an Output_data_list.
2469 Output_segment::output_section_count_list(const Output_data_list
* pdl
) const
2471 unsigned int count
= 0;
2472 for (Output_data_list::const_iterator p
= pdl
->begin();
2476 if ((*p
)->is_section())
2482 // Write the segment data into *OPHDR.
2484 template<int size
, bool big_endian
>
2486 Output_segment::write_header(elfcpp::Phdr_write
<size
, big_endian
>* ophdr
)
2488 ophdr
->put_p_type(this->type_
);
2489 ophdr
->put_p_offset(this->offset_
);
2490 ophdr
->put_p_vaddr(this->vaddr_
);
2491 ophdr
->put_p_paddr(this->paddr_
);
2492 ophdr
->put_p_filesz(this->filesz_
);
2493 ophdr
->put_p_memsz(this->memsz_
);
2494 ophdr
->put_p_flags(this->flags_
);
2495 ophdr
->put_p_align(std::max(this->min_p_align_
, this->maximum_alignment()));
2498 // Write the section headers into V.
2500 template<int size
, bool big_endian
>
2502 Output_segment::write_section_headers(const Layout
* layout
,
2503 const Stringpool
* secnamepool
,
2505 unsigned int *pshndx
2506 ACCEPT_SIZE_ENDIAN
) const
2508 // Every section that is attached to a segment must be attached to a
2509 // PT_LOAD segment, so we only write out section headers for PT_LOAD
2511 if (this->type_
!= elfcpp::PT_LOAD
)
2514 v
= this->write_section_headers_list
2515 SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
2516 layout
, secnamepool
, &this->output_data_
, v
, pshndx
2517 SELECT_SIZE_ENDIAN(size
, big_endian
));
2518 v
= this->write_section_headers_list
2519 SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
2520 layout
, secnamepool
, &this->output_bss_
, v
, pshndx
2521 SELECT_SIZE_ENDIAN(size
, big_endian
));
2525 template<int size
, bool big_endian
>
2527 Output_segment::write_section_headers_list(const Layout
* layout
,
2528 const Stringpool
* secnamepool
,
2529 const Output_data_list
* pdl
,
2531 unsigned int* pshndx
2532 ACCEPT_SIZE_ENDIAN
) const
2534 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
2535 for (Output_data_list::const_iterator p
= pdl
->begin();
2539 if ((*p
)->is_section())
2541 const Output_section
* ps
= static_cast<const Output_section
*>(*p
);
2542 gold_assert(*pshndx
== ps
->out_shndx());
2543 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
2544 ps
->write_header(layout
, secnamepool
, &oshdr
);
2552 // Output_file methods.
2554 Output_file::Output_file(const char* name
)
2559 map_is_anonymous_(false)
2563 // Open the output file.
2566 Output_file::open(off_t file_size
)
2568 this->file_size_
= file_size
;
2570 // Unlink the file first; otherwise the open() may fail if the file
2571 // is busy (e.g. it's an executable that's currently being executed).
2573 // However, the linker may be part of a system where a zero-length
2574 // file is created for it to write to, with tight permissions (gcc
2575 // 2.95 did something like this). Unlinking the file would work
2576 // around those permission controls, so we only unlink if the file
2577 // has a non-zero size. We also unlink only regular files to avoid
2578 // trouble with directories/etc.
2580 // If we fail, continue; this command is merely a best-effort attempt
2581 // to improve the odds for open().
2583 // We let the name "-" mean "stdout"
2584 if (strcmp(this->name_
, "-") == 0)
2585 this->o_
= STDOUT_FILENO
;
2589 if (::stat(this->name_
, &s
) == 0 && s
.st_size
!= 0)
2590 unlink_if_ordinary(this->name_
);
2592 int mode
= parameters
->output_is_object() ? 0666 : 0777;
2593 int o
= ::open(this->name_
, O_RDWR
| O_CREAT
| O_TRUNC
, mode
);
2595 gold_fatal(_("%s: open: %s"), this->name_
, strerror(errno
));
2602 // Resize the output file.
2605 Output_file::resize(off_t file_size
)
2607 // If the mmap is mapping an anonymous memory buffer, this is easy:
2608 // just mremap to the new size. If it's mapping to a file, we want
2609 // to unmap to flush to the file, then remap after growing the file.
2610 if (this->map_is_anonymous_
)
2612 void* base
= ::mremap(this->base_
, this->file_size_
, file_size
,
2614 if (base
== MAP_FAILED
)
2615 gold_fatal(_("%s: mremap: %s"), this->name_
, strerror(errno
));
2616 this->base_
= static_cast<unsigned char*>(base
);
2617 this->file_size_
= file_size
;
2622 this->file_size_
= file_size
;
2627 // Map the file into memory.
2632 const int o
= this->o_
;
2634 // If the output file is not a regular file, don't try to mmap it;
2635 // instead, we'll mmap a block of memory (an anonymous buffer), and
2636 // then later write the buffer to the file.
2638 struct stat statbuf
;
2639 if (o
== STDOUT_FILENO
|| o
== STDERR_FILENO
2640 || ::fstat(o
, &statbuf
) != 0
2641 || !S_ISREG(statbuf
.st_mode
))
2643 this->map_is_anonymous_
= true;
2644 base
= ::mmap(NULL
, this->file_size_
, PROT_READ
| PROT_WRITE
,
2645 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
2649 // Write out one byte to make the file the right size.
2650 if (::lseek(o
, this->file_size_
- 1, SEEK_SET
) < 0)
2651 gold_fatal(_("%s: lseek: %s"), this->name_
, strerror(errno
));
2653 if (::write(o
, &b
, 1) != 1)
2654 gold_fatal(_("%s: write: %s"), this->name_
, strerror(errno
));
2656 // Map the file into memory.
2657 this->map_is_anonymous_
= false;
2658 base
= ::mmap(NULL
, this->file_size_
, PROT_READ
| PROT_WRITE
,
2661 if (base
== MAP_FAILED
)
2662 gold_fatal(_("%s: mmap: %s"), this->name_
, strerror(errno
));
2663 this->base_
= static_cast<unsigned char*>(base
);
2666 // Unmap the file from memory.
2669 Output_file::unmap()
2671 if (::munmap(this->base_
, this->file_size_
) < 0)
2672 gold_error(_("%s: munmap: %s"), this->name_
, strerror(errno
));
2676 // Close the output file.
2679 Output_file::close()
2681 // If the map isn't file-backed, we need to write it now.
2682 if (this->map_is_anonymous_
)
2684 size_t bytes_to_write
= this->file_size_
;
2685 while (bytes_to_write
> 0)
2687 ssize_t bytes_written
= ::write(this->o_
, this->base_
, bytes_to_write
);
2688 if (bytes_written
== 0)
2689 gold_error(_("%s: write: unexpected 0 return-value"), this->name_
);
2690 else if (bytes_written
< 0)
2691 gold_error(_("%s: write: %s"), this->name_
, strerror(errno
));
2693 bytes_to_write
-= bytes_written
;
2698 // We don't close stdout or stderr
2699 if (this->o_
!= STDOUT_FILENO
&& this->o_
!= STDERR_FILENO
)
2700 if (::close(this->o_
) < 0)
2701 gold_error(_("%s: close: %s"), this->name_
, strerror(errno
));
2705 // Instantiate the templates we need. We could use the configure
2706 // script to restrict this to only the ones for implemented targets.
2708 #ifdef HAVE_TARGET_32_LITTLE
2711 Output_section::add_input_section
<32, false>(
2712 Sized_relobj
<32, false>* object
,
2714 const char* secname
,
2715 const elfcpp::Shdr
<32, false>& shdr
,
2716 unsigned int reloc_shndx
,
2717 bool have_sections_script
);
2720 #ifdef HAVE_TARGET_32_BIG
2723 Output_section::add_input_section
<32, true>(
2724 Sized_relobj
<32, true>* object
,
2726 const char* secname
,
2727 const elfcpp::Shdr
<32, true>& shdr
,
2728 unsigned int reloc_shndx
,
2729 bool have_sections_script
);
2732 #ifdef HAVE_TARGET_64_LITTLE
2735 Output_section::add_input_section
<64, false>(
2736 Sized_relobj
<64, false>* object
,
2738 const char* secname
,
2739 const elfcpp::Shdr
<64, false>& shdr
,
2740 unsigned int reloc_shndx
,
2741 bool have_sections_script
);
2744 #ifdef HAVE_TARGET_64_BIG
2747 Output_section::add_input_section
<64, true>(
2748 Sized_relobj
<64, true>* object
,
2750 const char* secname
,
2751 const elfcpp::Shdr
<64, true>& shdr
,
2752 unsigned int reloc_shndx
,
2753 bool have_sections_script
);
2756 #ifdef HAVE_TARGET_32_LITTLE
2758 class Output_data_reloc
<elfcpp::SHT_REL
, false, 32, false>;
2761 #ifdef HAVE_TARGET_32_BIG
2763 class Output_data_reloc
<elfcpp::SHT_REL
, false, 32, true>;
2766 #ifdef HAVE_TARGET_64_LITTLE
2768 class Output_data_reloc
<elfcpp::SHT_REL
, false, 64, false>;
2771 #ifdef HAVE_TARGET_64_BIG
2773 class Output_data_reloc
<elfcpp::SHT_REL
, false, 64, true>;
2776 #ifdef HAVE_TARGET_32_LITTLE
2778 class Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false>;
2781 #ifdef HAVE_TARGET_32_BIG
2783 class Output_data_reloc
<elfcpp::SHT_REL
, true, 32, true>;
2786 #ifdef HAVE_TARGET_64_LITTLE
2788 class Output_data_reloc
<elfcpp::SHT_REL
, true, 64, false>;
2791 #ifdef HAVE_TARGET_64_BIG
2793 class Output_data_reloc
<elfcpp::SHT_REL
, true, 64, true>;
2796 #ifdef HAVE_TARGET_32_LITTLE
2798 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 32, false>;
2801 #ifdef HAVE_TARGET_32_BIG
2803 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 32, true>;
2806 #ifdef HAVE_TARGET_64_LITTLE
2808 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 64, false>;
2811 #ifdef HAVE_TARGET_64_BIG
2813 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 64, true>;
2816 #ifdef HAVE_TARGET_32_LITTLE
2818 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, false>;
2821 #ifdef HAVE_TARGET_32_BIG
2823 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, true>;
2826 #ifdef HAVE_TARGET_64_LITTLE
2828 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false>;
2831 #ifdef HAVE_TARGET_64_BIG
2833 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, true>;
2836 #ifdef HAVE_TARGET_32_LITTLE
2838 class Output_data_got
<32, false>;
2841 #ifdef HAVE_TARGET_32_BIG
2843 class Output_data_got
<32, true>;
2846 #ifdef HAVE_TARGET_64_LITTLE
2848 class Output_data_got
<64, false>;
2851 #ifdef HAVE_TARGET_64_BIG
2853 class Output_data_got
<64, true>;
2856 } // End namespace gold.