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::sizes_are_fixed
;
48 // Output_data methods.
50 Output_data::~Output_data()
54 // Set the address and offset.
57 Output_data::set_address(uint64_t addr
, off_t off
)
59 this->address_
= addr
;
62 // Let the child class know.
63 this->do_set_address(addr
, off
);
66 // Return the default alignment for a size--32 or 64.
69 Output_data::default_alignment(int size
)
79 // Output_section_header methods. This currently assumes that the
80 // segment and section lists are complete at construction time.
82 Output_section_headers::Output_section_headers(
84 const Layout::Segment_list
* segment_list
,
85 const Layout::Section_list
* unattached_section_list
,
86 const Stringpool
* secnamepool
)
88 segment_list_(segment_list
),
89 unattached_section_list_(unattached_section_list
),
90 secnamepool_(secnamepool
)
92 // Count all the sections. Start with 1 for the null section.
94 for (Layout::Segment_list::const_iterator p
= segment_list
->begin();
95 p
!= segment_list
->end();
97 if ((*p
)->type() == elfcpp::PT_LOAD
)
98 count
+= (*p
)->output_section_count();
99 count
+= unattached_section_list
->size();
101 const int size
= parameters
->get_size();
104 shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
106 shdr_size
= elfcpp::Elf_sizes
<64>::shdr_size
;
110 this->set_data_size(count
* shdr_size
);
113 // Write out the section headers.
116 Output_section_headers::do_write(Output_file
* of
)
118 if (parameters
->get_size() == 32)
120 if (parameters
->is_big_endian())
122 #ifdef HAVE_TARGET_32_BIG
123 this->do_sized_write
<32, true>(of
);
130 #ifdef HAVE_TARGET_32_LITTLE
131 this->do_sized_write
<32, false>(of
);
137 else if (parameters
->get_size() == 64)
139 if (parameters
->is_big_endian())
141 #ifdef HAVE_TARGET_64_BIG
142 this->do_sized_write
<64, true>(of
);
149 #ifdef HAVE_TARGET_64_LITTLE
150 this->do_sized_write
<64, false>(of
);
160 template<int size
, bool big_endian
>
162 Output_section_headers::do_sized_write(Output_file
* of
)
164 off_t all_shdrs_size
= this->data_size();
165 unsigned char* view
= of
->get_output_view(this->offset(), all_shdrs_size
);
167 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
168 unsigned char* v
= view
;
171 typename
elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
172 oshdr
.put_sh_name(0);
173 oshdr
.put_sh_type(elfcpp::SHT_NULL
);
174 oshdr
.put_sh_flags(0);
175 oshdr
.put_sh_addr(0);
176 oshdr
.put_sh_offset(0);
177 oshdr
.put_sh_size(0);
178 oshdr
.put_sh_link(0);
179 oshdr
.put_sh_info(0);
180 oshdr
.put_sh_addralign(0);
181 oshdr
.put_sh_entsize(0);
187 for (Layout::Segment_list::const_iterator p
= this->segment_list_
->begin();
188 p
!= this->segment_list_
->end();
190 v
= (*p
)->write_section_headers
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
191 this->layout_
, this->secnamepool_
, v
, &shndx
192 SELECT_SIZE_ENDIAN(size
, big_endian
));
193 for (Layout::Section_list::const_iterator p
=
194 this->unattached_section_list_
->begin();
195 p
!= this->unattached_section_list_
->end();
198 gold_assert(shndx
== (*p
)->out_shndx());
199 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
200 (*p
)->write_header(this->layout_
, this->secnamepool_
, &oshdr
);
205 of
->write_output_view(this->offset(), all_shdrs_size
, view
);
208 // Output_segment_header methods.
210 Output_segment_headers::Output_segment_headers(
211 const Layout::Segment_list
& segment_list
)
212 : segment_list_(segment_list
)
214 const int size
= parameters
->get_size();
217 phdr_size
= elfcpp::Elf_sizes
<32>::phdr_size
;
219 phdr_size
= elfcpp::Elf_sizes
<64>::phdr_size
;
223 this->set_data_size(segment_list
.size() * phdr_size
);
227 Output_segment_headers::do_write(Output_file
* of
)
229 if (parameters
->get_size() == 32)
231 if (parameters
->is_big_endian())
233 #ifdef HAVE_TARGET_32_BIG
234 this->do_sized_write
<32, true>(of
);
241 #ifdef HAVE_TARGET_32_LITTLE
242 this->do_sized_write
<32, false>(of
);
248 else if (parameters
->get_size() == 64)
250 if (parameters
->is_big_endian())
252 #ifdef HAVE_TARGET_64_BIG
253 this->do_sized_write
<64, true>(of
);
260 #ifdef HAVE_TARGET_64_LITTLE
261 this->do_sized_write
<64, false>(of
);
271 template<int size
, bool big_endian
>
273 Output_segment_headers::do_sized_write(Output_file
* of
)
275 const int phdr_size
= elfcpp::Elf_sizes
<size
>::phdr_size
;
276 off_t all_phdrs_size
= this->segment_list_
.size() * phdr_size
;
277 unsigned char* view
= of
->get_output_view(this->offset(),
279 unsigned char* v
= view
;
280 for (Layout::Segment_list::const_iterator p
= this->segment_list_
.begin();
281 p
!= this->segment_list_
.end();
284 elfcpp::Phdr_write
<size
, big_endian
> ophdr(v
);
285 (*p
)->write_header(&ophdr
);
289 of
->write_output_view(this->offset(), all_phdrs_size
, view
);
292 // Output_file_header methods.
294 Output_file_header::Output_file_header(const Target
* target
,
295 const Symbol_table
* symtab
,
296 const Output_segment_headers
* osh
)
299 segment_header_(osh
),
300 section_header_(NULL
),
303 const int size
= parameters
->get_size();
306 ehdr_size
= elfcpp::Elf_sizes
<32>::ehdr_size
;
308 ehdr_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
312 this->set_data_size(ehdr_size
);
315 // Set the section table information for a file header.
318 Output_file_header::set_section_info(const Output_section_headers
* shdrs
,
319 const Output_section
* shstrtab
)
321 this->section_header_
= shdrs
;
322 this->shstrtab_
= shstrtab
;
325 // Write out the file header.
328 Output_file_header::do_write(Output_file
* of
)
330 if (parameters
->get_size() == 32)
332 if (parameters
->is_big_endian())
334 #ifdef HAVE_TARGET_32_BIG
335 this->do_sized_write
<32, true>(of
);
342 #ifdef HAVE_TARGET_32_LITTLE
343 this->do_sized_write
<32, false>(of
);
349 else if (parameters
->get_size() == 64)
351 if (parameters
->is_big_endian())
353 #ifdef HAVE_TARGET_64_BIG
354 this->do_sized_write
<64, true>(of
);
361 #ifdef HAVE_TARGET_64_LITTLE
362 this->do_sized_write
<64, false>(of
);
372 // Write out the file header with appropriate size and endianess.
374 template<int size
, bool big_endian
>
376 Output_file_header::do_sized_write(Output_file
* of
)
378 gold_assert(this->offset() == 0);
380 int ehdr_size
= elfcpp::Elf_sizes
<size
>::ehdr_size
;
381 unsigned char* view
= of
->get_output_view(0, ehdr_size
);
382 elfcpp::Ehdr_write
<size
, big_endian
> oehdr(view
);
384 unsigned char e_ident
[elfcpp::EI_NIDENT
];
385 memset(e_ident
, 0, elfcpp::EI_NIDENT
);
386 e_ident
[elfcpp::EI_MAG0
] = elfcpp::ELFMAG0
;
387 e_ident
[elfcpp::EI_MAG1
] = elfcpp::ELFMAG1
;
388 e_ident
[elfcpp::EI_MAG2
] = elfcpp::ELFMAG2
;
389 e_ident
[elfcpp::EI_MAG3
] = elfcpp::ELFMAG3
;
391 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS32
;
393 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS64
;
396 e_ident
[elfcpp::EI_DATA
] = (big_endian
397 ? elfcpp::ELFDATA2MSB
398 : elfcpp::ELFDATA2LSB
);
399 e_ident
[elfcpp::EI_VERSION
] = elfcpp::EV_CURRENT
;
400 // FIXME: Some targets may need to set EI_OSABI and EI_ABIVERSION.
401 oehdr
.put_e_ident(e_ident
);
404 if (parameters
->output_is_object())
405 e_type
= elfcpp::ET_REL
;
406 else if (parameters
->output_is_shared())
407 e_type
= elfcpp::ET_DYN
;
409 e_type
= elfcpp::ET_EXEC
;
410 oehdr
.put_e_type(e_type
);
412 oehdr
.put_e_machine(this->target_
->machine_code());
413 oehdr
.put_e_version(elfcpp::EV_CURRENT
);
415 // FIXME: Need to support -e, and target specific entry symbol.
416 Symbol
* sym
= this->symtab_
->lookup("_start");
417 typename Sized_symbol
<size
>::Value_type v
;
422 Sized_symbol
<size
>* ssym
;
423 ssym
= this->symtab_
->get_sized_symbol
SELECT_SIZE_NAME(size
) (
424 sym
SELECT_SIZE(size
));
427 oehdr
.put_e_entry(v
);
429 oehdr
.put_e_phoff(this->segment_header_
->offset());
430 oehdr
.put_e_shoff(this->section_header_
->offset());
432 // FIXME: The target needs to set the flags.
433 oehdr
.put_e_flags(0);
435 oehdr
.put_e_ehsize(elfcpp::Elf_sizes
<size
>::ehdr_size
);
436 oehdr
.put_e_phentsize(elfcpp::Elf_sizes
<size
>::phdr_size
);
437 oehdr
.put_e_phnum(this->segment_header_
->data_size()
438 / elfcpp::Elf_sizes
<size
>::phdr_size
);
439 oehdr
.put_e_shentsize(elfcpp::Elf_sizes
<size
>::shdr_size
);
440 oehdr
.put_e_shnum(this->section_header_
->data_size()
441 / elfcpp::Elf_sizes
<size
>::shdr_size
);
442 oehdr
.put_e_shstrndx(this->shstrtab_
->out_shndx());
444 of
->write_output_view(0, ehdr_size
, view
);
447 // Output_data_const methods.
450 Output_data_const::do_write(Output_file
* of
)
452 of
->write(this->offset(), this->data_
.data(), this->data_
.size());
455 // Output_data_const_buffer methods.
458 Output_data_const_buffer::do_write(Output_file
* of
)
460 of
->write(this->offset(), this->p_
, this->data_size());
463 // Output_section_data methods.
465 // Record the output section, and set the entry size and such.
468 Output_section_data::set_output_section(Output_section
* os
)
470 gold_assert(this->output_section_
== NULL
);
471 this->output_section_
= os
;
472 this->do_adjust_output_section(os
);
475 // Return the section index of the output section.
478 Output_section_data::do_out_shndx() const
480 gold_assert(this->output_section_
!= NULL
);
481 return this->output_section_
->out_shndx();
484 // Output_data_strtab methods.
486 // Set the address. We don't actually care about the address, but we
487 // do set our final size.
490 Output_data_strtab::do_set_address(uint64_t, off_t
)
492 this->strtab_
->set_string_offsets();
493 this->set_data_size(this->strtab_
->get_strtab_size());
496 // Write out a string table.
499 Output_data_strtab::do_write(Output_file
* of
)
501 this->strtab_
->write(of
, this->offset());
504 // Output_reloc methods.
506 // Get the symbol index of a relocation.
508 template<bool dynamic
, int size
, bool big_endian
>
510 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::get_symbol_index()
514 switch (this->local_sym_index_
)
520 if (this->u1_
.gsym
== NULL
)
523 index
= this->u1_
.gsym
->dynsym_index();
525 index
= this->u1_
.gsym
->symtab_index();
530 index
= this->u1_
.os
->dynsym_index();
532 index
= this->u1_
.os
->symtab_index();
536 // Relocations without symbols use a symbol index of 0.
543 // FIXME: It seems that some targets may need to generate
544 // dynamic relocations against local symbols for some
545 // reasons. This will have to be addressed at some point.
549 index
= this->u1_
.relobj
->symtab_index(this->local_sym_index_
);
552 gold_assert(index
!= -1U);
556 // Write out the offset and info fields of a Rel or Rela relocation
559 template<bool dynamic
, int size
, bool big_endian
>
560 template<typename Write_rel
>
562 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::write_rel(
565 Address address
= this->address_
;
566 if (this->shndx_
!= INVALID_CODE
)
569 Output_section
* os
= this->u2_
.relobj
->output_section(this->shndx_
,
571 gold_assert(os
!= NULL
);
572 address
+= os
->address() + off
;
574 else if (this->u2_
.od
!= NULL
)
575 address
+= this->u2_
.od
->address();
576 wr
->put_r_offset(address
);
577 wr
->put_r_info(elfcpp::elf_r_info
<size
>(this->get_symbol_index(),
581 // Write out a Rel relocation.
583 template<bool dynamic
, int size
, bool big_endian
>
585 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::write(
586 unsigned char* pov
) const
588 elfcpp::Rel_write
<size
, big_endian
> orel(pov
);
589 this->write_rel(&orel
);
592 // Write out a Rela relocation.
594 template<bool dynamic
, int size
, bool big_endian
>
596 Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>::write(
597 unsigned char* pov
) const
599 elfcpp::Rela_write
<size
, big_endian
> orel(pov
);
600 this->rel_
.write_rel(&orel
);
601 orel
.put_r_addend(this->addend_
);
604 // Output_data_reloc_base methods.
606 // Adjust the output section.
608 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
610 Output_data_reloc_base
<sh_type
, dynamic
, size
, big_endian
>
611 ::do_adjust_output_section(Output_section
* os
)
613 if (sh_type
== elfcpp::SHT_REL
)
614 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rel_size
);
615 else if (sh_type
== elfcpp::SHT_RELA
)
616 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rela_size
);
620 os
->set_should_link_to_dynsym();
622 os
->set_should_link_to_symtab();
625 // Write out relocation data.
627 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
629 Output_data_reloc_base
<sh_type
, dynamic
, size
, big_endian
>::do_write(
632 const off_t off
= this->offset();
633 const off_t oview_size
= this->data_size();
634 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
636 unsigned char* pov
= oview
;
637 for (typename
Relocs::const_iterator p
= this->relocs_
.begin();
638 p
!= this->relocs_
.end();
645 gold_assert(pov
- oview
== oview_size
);
647 of
->write_output_view(off
, oview_size
, oview
);
649 // We no longer need the relocation entries.
650 this->relocs_
.clear();
653 // Output_data_got::Got_entry methods.
655 // Write out the entry.
657 template<int size
, bool big_endian
>
659 Output_data_got
<size
, big_endian
>::Got_entry::write(unsigned char* pov
) const
663 switch (this->local_sym_index_
)
667 Symbol
* gsym
= this->u_
.gsym
;
669 // If the symbol is resolved locally, we need to write out its
670 // value. Otherwise we just write zero. The target code is
671 // responsible for creating a relocation entry to fill in the
673 if (gsym
->final_value_is_known())
675 Sized_symbol
<size
>* sgsym
;
676 // This cast is a bit ugly. We don't want to put a
677 // virtual method in Symbol, because we want Symbol to be
678 // as small as possible.
679 sgsym
= static_cast<Sized_symbol
<size
>*>(gsym
);
680 val
= sgsym
->value();
686 val
= this->u_
.constant
;
690 val
= this->u_
.object
->local_symbol_value(this->local_sym_index_
);
694 elfcpp::Swap
<size
, big_endian
>::writeval(pov
, val
);
697 // Output_data_got methods.
699 // Add an entry for a global symbol to the GOT. This returns true if
700 // this is a new GOT entry, false if the symbol already had a GOT
703 template<int size
, bool big_endian
>
705 Output_data_got
<size
, big_endian
>::add_global(Symbol
* gsym
)
707 if (gsym
->has_got_offset())
710 this->entries_
.push_back(Got_entry(gsym
));
711 this->set_got_size();
712 gsym
->set_got_offset(this->last_got_offset());
716 // Add an entry for a local symbol to the GOT. This returns true if
717 // this is a new GOT entry, false if the symbol already has a GOT
720 template<int size
, bool big_endian
>
722 Output_data_got
<size
, big_endian
>::add_local(
723 Sized_relobj
<size
, big_endian
>* object
,
726 if (object
->local_has_got_offset(symndx
))
728 this->entries_
.push_back(Got_entry(object
, symndx
));
729 this->set_got_size();
730 object
->set_local_got_offset(symndx
, this->last_got_offset());
734 // Write out the GOT.
736 template<int size
, bool big_endian
>
738 Output_data_got
<size
, big_endian
>::do_write(Output_file
* of
)
740 const int add
= size
/ 8;
742 const off_t off
= this->offset();
743 const off_t oview_size
= this->data_size();
744 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
746 unsigned char* pov
= oview
;
747 for (typename
Got_entries::const_iterator p
= this->entries_
.begin();
748 p
!= this->entries_
.end();
755 gold_assert(pov
- oview
== oview_size
);
757 of
->write_output_view(off
, oview_size
, oview
);
759 // We no longer need the GOT entries.
760 this->entries_
.clear();
763 // Output_data_dynamic::Dynamic_entry methods.
765 // Write out the entry.
767 template<int size
, bool big_endian
>
769 Output_data_dynamic::Dynamic_entry::write(
771 const Stringpool
* pool
772 ACCEPT_SIZE_ENDIAN
) const
774 typename
elfcpp::Elf_types
<size
>::Elf_WXword val
;
775 switch (this->classification_
)
781 case DYNAMIC_SECTION_ADDRESS
:
782 val
= this->u_
.od
->address();
785 case DYNAMIC_SECTION_SIZE
:
786 val
= this->u_
.od
->data_size();
791 const Sized_symbol
<size
>* s
=
792 static_cast<const Sized_symbol
<size
>*>(this->u_
.sym
);
798 val
= pool
->get_offset(this->u_
.str
);
805 elfcpp::Dyn_write
<size
, big_endian
> dw(pov
);
806 dw
.put_d_tag(this->tag_
);
810 // Output_data_dynamic methods.
812 // Adjust the output section to set the entry size.
815 Output_data_dynamic::do_adjust_output_section(Output_section
* os
)
817 if (parameters
->get_size() == 32)
818 os
->set_entsize(elfcpp::Elf_sizes
<32>::dyn_size
);
819 else if (parameters
->get_size() == 64)
820 os
->set_entsize(elfcpp::Elf_sizes
<64>::dyn_size
);
825 // Set the final data size.
828 Output_data_dynamic::do_set_address(uint64_t, off_t
)
830 // Add the terminating entry.
831 this->add_constant(elfcpp::DT_NULL
, 0);
834 if (parameters
->get_size() == 32)
835 dyn_size
= elfcpp::Elf_sizes
<32>::dyn_size
;
836 else if (parameters
->get_size() == 64)
837 dyn_size
= elfcpp::Elf_sizes
<64>::dyn_size
;
840 this->set_data_size(this->entries_
.size() * dyn_size
);
843 // Write out the dynamic entries.
846 Output_data_dynamic::do_write(Output_file
* of
)
848 if (parameters
->get_size() == 32)
850 if (parameters
->is_big_endian())
852 #ifdef HAVE_TARGET_32_BIG
853 this->sized_write
<32, true>(of
);
860 #ifdef HAVE_TARGET_32_LITTLE
861 this->sized_write
<32, false>(of
);
867 else if (parameters
->get_size() == 64)
869 if (parameters
->is_big_endian())
871 #ifdef HAVE_TARGET_64_BIG
872 this->sized_write
<64, true>(of
);
879 #ifdef HAVE_TARGET_64_LITTLE
880 this->sized_write
<64, false>(of
);
890 template<int size
, bool big_endian
>
892 Output_data_dynamic::sized_write(Output_file
* of
)
894 const int dyn_size
= elfcpp::Elf_sizes
<size
>::dyn_size
;
896 const off_t offset
= this->offset();
897 const off_t oview_size
= this->data_size();
898 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
900 unsigned char* pov
= oview
;
901 for (typename
Dynamic_entries::const_iterator p
= this->entries_
.begin();
902 p
!= this->entries_
.end();
905 p
->write
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
)(
906 pov
, this->pool_
SELECT_SIZE_ENDIAN(size
, big_endian
));
910 gold_assert(pov
- oview
== oview_size
);
912 of
->write_output_view(offset
, oview_size
, oview
);
914 // We no longer need the dynamic entries.
915 this->entries_
.clear();
918 // Output_section::Input_section methods.
920 // Return the data size. For an input section we store the size here.
921 // For an Output_section_data, we have to ask it for the size.
924 Output_section::Input_section::data_size() const
926 if (this->is_input_section())
927 return this->u1_
.data_size
;
929 return this->u2_
.posd
->data_size();
932 // Set the address and file offset.
935 Output_section::Input_section::set_address(uint64_t addr
, off_t off
,
938 if (this->is_input_section())
939 this->u2_
.object
->set_section_offset(this->shndx_
, off
- secoff
);
941 this->u2_
.posd
->set_address(addr
, off
);
944 // Try to turn an input address into an output address.
947 Output_section::Input_section::output_address(const Relobj
* object
,
950 uint64_t output_section_address
,
951 uint64_t *poutput
) const
953 if (!this->is_input_section())
954 return this->u2_
.posd
->output_address(object
, shndx
, offset
,
955 output_section_address
, poutput
);
958 if (this->shndx_
!= shndx
959 || this->u2_
.object
!= object
)
962 Output_section
* os
= object
->output_section(shndx
, &output_offset
);
963 gold_assert(os
!= NULL
);
964 *poutput
= output_section_address
+ output_offset
+ offset
;
969 // Write out the data. We don't have to do anything for an input
970 // section--they are handled via Object::relocate--but this is where
971 // we write out the data for an Output_section_data.
974 Output_section::Input_section::write(Output_file
* of
)
976 if (!this->is_input_section())
977 this->u2_
.posd
->write(of
);
980 // Output_section methods.
982 // Construct an Output_section. NAME will point into a Stringpool.
984 Output_section::Output_section(const char* name
, elfcpp::Elf_Word type
,
985 elfcpp::Elf_Xword flags
)
999 first_input_offset_(0),
1001 needs_symtab_index_(false),
1002 needs_dynsym_index_(false),
1003 should_link_to_symtab_(false),
1004 should_link_to_dynsym_(false)
1008 Output_section::~Output_section()
1012 // Set the entry size.
1015 Output_section::set_entsize(uint64_t v
)
1017 if (this->entsize_
== 0)
1020 gold_assert(this->entsize_
== v
);
1023 // Add the input section SHNDX, with header SHDR, named SECNAME, in
1024 // OBJECT, to the Output_section. Return the offset of the input
1025 // section within the output section. We don't always keep track of
1026 // input sections for an Output_section. Instead, each Object keeps
1027 // track of the Output_section for each of its input sections.
1029 template<int size
, bool big_endian
>
1031 Output_section::add_input_section(Relobj
* object
, unsigned int shndx
,
1032 const char* secname
,
1033 const elfcpp::Shdr
<size
, big_endian
>& shdr
)
1035 elfcpp::Elf_Xword addralign
= shdr
.get_sh_addralign();
1036 if ((addralign
& (addralign
- 1)) != 0)
1038 object
->error(_("invalid alignment %lu for section \"%s\""),
1039 static_cast<unsigned long>(addralign
), secname
);
1043 if (addralign
> this->addralign_
)
1044 this->addralign_
= addralign
;
1046 // If this is a SHF_MERGE section, we pass all the input sections to
1047 // a Output_data_merge.
1048 if ((shdr
.get_sh_flags() & elfcpp::SHF_MERGE
) != 0)
1050 if (this->add_merge_input_section(object
, shndx
, shdr
.get_sh_flags(),
1051 shdr
.get_sh_entsize(),
1054 // Tell the relocation routines that they need to call the
1055 // output_address method to determine the final address.
1060 off_t offset_in_section
= this->data_size();
1061 off_t aligned_offset_in_section
= align_address(offset_in_section
,
1064 if (aligned_offset_in_section
> offset_in_section
1065 && (shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0
1066 && object
->target()->has_code_fill())
1068 // We need to add some fill data. Using fill_list_ when
1069 // possible is an optimization, since we will often have fill
1070 // sections without input sections.
1071 off_t fill_len
= aligned_offset_in_section
- offset_in_section
;
1072 if (this->input_sections_
.empty())
1073 this->fills_
.push_back(Fill(offset_in_section
, fill_len
));
1076 // FIXME: When relaxing, the size needs to adjust to
1077 // maintain a constant alignment.
1078 std::string
fill_data(object
->target()->code_fill(fill_len
));
1079 Output_data_const
* odc
= new Output_data_const(fill_data
, 1);
1080 this->input_sections_
.push_back(Input_section(odc
));
1084 this->set_data_size(aligned_offset_in_section
+ shdr
.get_sh_size());
1086 // We need to keep track of this section if we are already keeping
1087 // track of sections, or if we are relaxing. FIXME: Add test for
1089 if (!this->input_sections_
.empty())
1090 this->input_sections_
.push_back(Input_section(object
, shndx
,
1094 return aligned_offset_in_section
;
1097 // Add arbitrary data to an output section.
1100 Output_section::add_output_section_data(Output_section_data
* posd
)
1102 Input_section
inp(posd
);
1103 this->add_output_section_data(&inp
);
1106 // Add arbitrary data to an output section by Input_section.
1109 Output_section::add_output_section_data(Input_section
* inp
)
1111 if (this->input_sections_
.empty())
1112 this->first_input_offset_
= this->data_size();
1114 this->input_sections_
.push_back(*inp
);
1116 uint64_t addralign
= inp
->addralign();
1117 if (addralign
> this->addralign_
)
1118 this->addralign_
= addralign
;
1120 inp
->set_output_section(this);
1123 // Add a merge section to an output section.
1126 Output_section::add_output_merge_section(Output_section_data
* posd
,
1127 bool is_string
, uint64_t entsize
)
1129 Input_section
inp(posd
, is_string
, entsize
);
1130 this->add_output_section_data(&inp
);
1133 // Add an input section to a SHF_MERGE section.
1136 Output_section::add_merge_input_section(Relobj
* object
, unsigned int shndx
,
1137 uint64_t flags
, uint64_t entsize
,
1140 // We only merge constants if the alignment is not more than the
1141 // entry size. This could be handled, but it's unusual.
1142 if (addralign
> entsize
)
1145 bool is_string
= (flags
& elfcpp::SHF_STRINGS
) != 0;
1146 Input_section_list::iterator p
;
1147 for (p
= this->input_sections_
.begin();
1148 p
!= this->input_sections_
.end();
1150 if (p
->is_merge_section(is_string
, entsize
))
1153 // We handle the actual constant merging in Output_merge_data or
1154 // Output_merge_string_data.
1155 if (p
!= this->input_sections_
.end())
1156 p
->add_input_section(object
, shndx
);
1159 Output_section_data
* posd
;
1161 posd
= new Output_merge_data(entsize
);
1162 else if (entsize
== 1)
1163 posd
= new Output_merge_string
<char>();
1164 else if (entsize
== 2)
1165 posd
= new Output_merge_string
<uint16_t>();
1166 else if (entsize
== 4)
1167 posd
= new Output_merge_string
<uint32_t>();
1171 this->add_output_merge_section(posd
, is_string
, entsize
);
1172 posd
->add_input_section(object
, shndx
);
1178 // Return the output virtual address of OFFSET relative to the start
1179 // of input section SHNDX in object OBJECT.
1182 Output_section::output_address(const Relobj
* object
, unsigned int shndx
,
1185 uint64_t addr
= this->address() + this->first_input_offset_
;
1186 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
1187 p
!= this->input_sections_
.end();
1190 addr
= align_address(addr
, p
->addralign());
1192 if (p
->output_address(object
, shndx
, offset
, addr
, &output
))
1194 addr
+= p
->data_size();
1197 // If we get here, it means that we don't know the mapping for this
1198 // input section. This might happen in principle if
1199 // add_input_section were called before add_output_section_data.
1200 // But it should never actually happen.
1205 // Set the address of an Output_section. This is where we handle
1206 // setting the addresses of any Output_section_data objects.
1209 Output_section::do_set_address(uint64_t address
, off_t startoff
)
1211 if (this->input_sections_
.empty())
1214 off_t off
= startoff
+ this->first_input_offset_
;
1215 for (Input_section_list::iterator p
= this->input_sections_
.begin();
1216 p
!= this->input_sections_
.end();
1219 off
= align_address(off
, p
->addralign());
1220 p
->set_address(address
+ (off
- startoff
), off
, startoff
);
1221 off
+= p
->data_size();
1224 this->set_data_size(off
- startoff
);
1227 // Write the section header to *OSHDR.
1229 template<int size
, bool big_endian
>
1231 Output_section::write_header(const Layout
* layout
,
1232 const Stringpool
* secnamepool
,
1233 elfcpp::Shdr_write
<size
, big_endian
>* oshdr
) const
1235 oshdr
->put_sh_name(secnamepool
->get_offset(this->name_
));
1236 oshdr
->put_sh_type(this->type_
);
1237 oshdr
->put_sh_flags(this->flags_
);
1238 oshdr
->put_sh_addr(this->address());
1239 oshdr
->put_sh_offset(this->offset());
1240 oshdr
->put_sh_size(this->data_size());
1241 if (this->link_section_
!= NULL
)
1242 oshdr
->put_sh_link(this->link_section_
->out_shndx());
1243 else if (this->should_link_to_symtab_
)
1244 oshdr
->put_sh_link(layout
->symtab_section()->out_shndx());
1245 else if (this->should_link_to_dynsym_
)
1246 oshdr
->put_sh_link(layout
->dynsym_section()->out_shndx());
1248 oshdr
->put_sh_link(this->link_
);
1249 if (this->info_section_
!= NULL
)
1250 oshdr
->put_sh_info(this->info_section_
->out_shndx());
1252 oshdr
->put_sh_info(this->info_
);
1253 oshdr
->put_sh_addralign(this->addralign_
);
1254 oshdr
->put_sh_entsize(this->entsize_
);
1257 // Write out the data. For input sections the data is written out by
1258 // Object::relocate, but we have to handle Output_section_data objects
1262 Output_section::do_write(Output_file
* of
)
1264 off_t output_section_file_offset
= this->offset();
1265 for (Fill_list::iterator p
= this->fills_
.begin();
1266 p
!= this->fills_
.end();
1269 std::string
fill_data(of
->target()->code_fill(p
->length()));
1270 of
->write(output_section_file_offset
+ p
->section_offset(),
1271 fill_data
.data(), fill_data
.size());
1274 for (Input_section_list::iterator p
= this->input_sections_
.begin();
1275 p
!= this->input_sections_
.end();
1280 // Output segment methods.
1282 Output_segment::Output_segment(elfcpp::Elf_Word type
, elfcpp::Elf_Word flags
)
1293 is_align_known_(false)
1297 // Add an Output_section to an Output_segment.
1300 Output_segment::add_output_section(Output_section
* os
,
1301 elfcpp::Elf_Word seg_flags
,
1304 gold_assert((os
->flags() & elfcpp::SHF_ALLOC
) != 0);
1305 gold_assert(!this->is_align_known_
);
1307 // Update the segment flags.
1308 this->flags_
|= seg_flags
;
1310 Output_segment::Output_data_list
* pdl
;
1311 if (os
->type() == elfcpp::SHT_NOBITS
)
1312 pdl
= &this->output_bss_
;
1314 pdl
= &this->output_data_
;
1316 // So that PT_NOTE segments will work correctly, we need to ensure
1317 // that all SHT_NOTE sections are adjacent. This will normally
1318 // happen automatically, because all the SHT_NOTE input sections
1319 // will wind up in the same output section. However, it is possible
1320 // for multiple SHT_NOTE input sections to have different section
1321 // flags, and thus be in different output sections, but for the
1322 // different section flags to map into the same segment flags and
1323 // thus the same output segment.
1325 // Note that while there may be many input sections in an output
1326 // section, there are normally only a few output sections in an
1327 // output segment. This loop is expected to be fast.
1329 if (os
->type() == elfcpp::SHT_NOTE
&& !pdl
->empty())
1331 Output_segment::Output_data_list::iterator p
= pdl
->end();
1335 if ((*p
)->is_section_type(elfcpp::SHT_NOTE
))
1337 // We don't worry about the FRONT parameter.
1343 while (p
!= pdl
->begin());
1346 // Similarly, so that PT_TLS segments will work, we need to group
1347 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
1348 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
1349 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
1351 if ((os
->flags() & elfcpp::SHF_TLS
) != 0 && !this->output_data_
.empty())
1353 pdl
= &this->output_data_
;
1354 bool nobits
= os
->type() == elfcpp::SHT_NOBITS
;
1355 bool sawtls
= false;
1356 Output_segment::Output_data_list::iterator p
= pdl
->end();
1361 if ((*p
)->is_section_flag_set(elfcpp::SHF_TLS
))
1364 // Put a NOBITS section after the first TLS section.
1365 // But a PROGBITS section after the first TLS/PROGBITS
1367 insert
= nobits
|| !(*p
)->is_section_type(elfcpp::SHT_NOBITS
);
1371 // If we've gone past the TLS sections, but we've seen a
1372 // TLS section, then we need to insert this section now.
1378 // We don't worry about the FRONT parameter.
1384 while (p
!= pdl
->begin());
1386 // There are no TLS sections yet; put this one at the requested
1387 // location in the section list.
1391 pdl
->push_front(os
);
1396 // Add an Output_data (which is not an Output_section) to the start of
1400 Output_segment::add_initial_output_data(Output_data
* od
)
1402 gold_assert(!this->is_align_known_
);
1403 this->output_data_
.push_front(od
);
1406 // Return the maximum alignment of the Output_data in Output_segment.
1407 // Once we compute this, we prohibit new sections from being added.
1410 Output_segment::addralign()
1412 if (!this->is_align_known_
)
1416 addralign
= Output_segment::maximum_alignment(&this->output_data_
);
1417 if (addralign
> this->align_
)
1418 this->align_
= addralign
;
1420 addralign
= Output_segment::maximum_alignment(&this->output_bss_
);
1421 if (addralign
> this->align_
)
1422 this->align_
= addralign
;
1424 this->is_align_known_
= true;
1427 return this->align_
;
1430 // Return the maximum alignment of a list of Output_data.
1433 Output_segment::maximum_alignment(const Output_data_list
* pdl
)
1436 for (Output_data_list::const_iterator p
= pdl
->begin();
1440 uint64_t addralign
= (*p
)->addralign();
1441 if (addralign
> ret
)
1447 // Set the section addresses for an Output_segment. ADDR is the
1448 // address and *POFF is the file offset. Set the section indexes
1449 // starting with *PSHNDX. Return the address of the immediately
1450 // following segment. Update *POFF and *PSHNDX.
1453 Output_segment::set_section_addresses(uint64_t addr
, off_t
* poff
,
1454 unsigned int* pshndx
)
1456 gold_assert(this->type_
== elfcpp::PT_LOAD
);
1458 this->vaddr_
= addr
;
1459 this->paddr_
= addr
;
1461 off_t orig_off
= *poff
;
1462 this->offset_
= orig_off
;
1464 *poff
= align_address(*poff
, this->addralign());
1466 addr
= this->set_section_list_addresses(&this->output_data_
, addr
, poff
,
1468 this->filesz_
= *poff
- orig_off
;
1472 uint64_t ret
= this->set_section_list_addresses(&this->output_bss_
, addr
,
1474 this->memsz_
= *poff
- orig_off
;
1476 // Ignore the file offset adjustments made by the BSS Output_data
1483 // Set the addresses and file offsets in a list of Output_data
1487 Output_segment::set_section_list_addresses(Output_data_list
* pdl
,
1488 uint64_t addr
, off_t
* poff
,
1489 unsigned int* pshndx
)
1491 off_t startoff
= *poff
;
1493 off_t off
= startoff
;
1494 for (Output_data_list::iterator p
= pdl
->begin();
1498 off
= align_address(off
, (*p
)->addralign());
1499 (*p
)->set_address(addr
+ (off
- startoff
), off
);
1501 // Unless this is a PT_TLS segment, we want to ignore the size
1502 // of a SHF_TLS/SHT_NOBITS section. Such a section does not
1503 // affect the size of a PT_LOAD segment.
1504 if (this->type_
== elfcpp::PT_TLS
1505 || !(*p
)->is_section_flag_set(elfcpp::SHF_TLS
)
1506 || !(*p
)->is_section_type(elfcpp::SHT_NOBITS
))
1507 off
+= (*p
)->data_size();
1509 if ((*p
)->is_section())
1511 (*p
)->set_out_shndx(*pshndx
);
1517 return addr
+ (off
- startoff
);
1520 // For a non-PT_LOAD segment, set the offset from the sections, if
1524 Output_segment::set_offset()
1526 gold_assert(this->type_
!= elfcpp::PT_LOAD
);
1528 if (this->output_data_
.empty() && this->output_bss_
.empty())
1539 const Output_data
* first
;
1540 if (this->output_data_
.empty())
1541 first
= this->output_bss_
.front();
1543 first
= this->output_data_
.front();
1544 this->vaddr_
= first
->address();
1545 this->paddr_
= this->vaddr_
;
1546 this->offset_
= first
->offset();
1548 if (this->output_data_
.empty())
1552 const Output_data
* last_data
= this->output_data_
.back();
1553 this->filesz_
= (last_data
->address()
1554 + last_data
->data_size()
1558 const Output_data
* last
;
1559 if (this->output_bss_
.empty())
1560 last
= this->output_data_
.back();
1562 last
= this->output_bss_
.back();
1563 this->memsz_
= (last
->address()
1568 // Return the number of Output_sections in an Output_segment.
1571 Output_segment::output_section_count() const
1573 return (this->output_section_count_list(&this->output_data_
)
1574 + this->output_section_count_list(&this->output_bss_
));
1577 // Return the number of Output_sections in an Output_data_list.
1580 Output_segment::output_section_count_list(const Output_data_list
* pdl
) const
1582 unsigned int count
= 0;
1583 for (Output_data_list::const_iterator p
= pdl
->begin();
1587 if ((*p
)->is_section())
1593 // Write the segment data into *OPHDR.
1595 template<int size
, bool big_endian
>
1597 Output_segment::write_header(elfcpp::Phdr_write
<size
, big_endian
>* ophdr
)
1599 ophdr
->put_p_type(this->type_
);
1600 ophdr
->put_p_offset(this->offset_
);
1601 ophdr
->put_p_vaddr(this->vaddr_
);
1602 ophdr
->put_p_paddr(this->paddr_
);
1603 ophdr
->put_p_filesz(this->filesz_
);
1604 ophdr
->put_p_memsz(this->memsz_
);
1605 ophdr
->put_p_flags(this->flags_
);
1606 ophdr
->put_p_align(this->addralign());
1609 // Write the section headers into V.
1611 template<int size
, bool big_endian
>
1613 Output_segment::write_section_headers(const Layout
* layout
,
1614 const Stringpool
* secnamepool
,
1616 unsigned int *pshndx
1617 ACCEPT_SIZE_ENDIAN
) const
1619 // Every section that is attached to a segment must be attached to a
1620 // PT_LOAD segment, so we only write out section headers for PT_LOAD
1622 if (this->type_
!= elfcpp::PT_LOAD
)
1625 v
= this->write_section_headers_list
1626 SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
1627 layout
, secnamepool
, &this->output_data_
, v
, pshndx
1628 SELECT_SIZE_ENDIAN(size
, big_endian
));
1629 v
= this->write_section_headers_list
1630 SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
1631 layout
, secnamepool
, &this->output_bss_
, v
, pshndx
1632 SELECT_SIZE_ENDIAN(size
, big_endian
));
1636 template<int size
, bool big_endian
>
1638 Output_segment::write_section_headers_list(const Layout
* layout
,
1639 const Stringpool
* secnamepool
,
1640 const Output_data_list
* pdl
,
1642 unsigned int* pshndx
1643 ACCEPT_SIZE_ENDIAN
) const
1645 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
1646 for (Output_data_list::const_iterator p
= pdl
->begin();
1650 if ((*p
)->is_section())
1652 const Output_section
* ps
= static_cast<const Output_section
*>(*p
);
1653 gold_assert(*pshndx
== ps
->out_shndx());
1654 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
1655 ps
->write_header(layout
, secnamepool
, &oshdr
);
1663 // Output_file methods.
1665 Output_file::Output_file(const General_options
& options
, Target
* target
)
1666 : options_(options
),
1668 name_(options
.output_file_name()),
1675 // Open the output file.
1678 Output_file::open(off_t file_size
)
1680 this->file_size_
= file_size
;
1682 // Unlink the file first; otherwise the open() may fail if the file
1683 // is busy (e.g. it's an executable that's currently being executed).
1685 // However, the linker may be part of a system where a zero-length
1686 // file is created for it to write to, with tight permissions (gcc
1687 // 2.95 did something like this). Unlinking the file would work
1688 // around those permission controls, so we only unlink if the file
1689 // has a non-zero size. We also unlink only regular files to avoid
1690 // trouble with directories/etc.
1692 // If we fail, continue; this command is merely a best-effort attempt
1693 // to improve the odds for open().
1696 if (::stat(this->name_
, &s
) == 0 && s
.st_size
!= 0)
1697 unlink_if_ordinary(this->name_
);
1699 int mode
= parameters
->output_is_object() ? 0666 : 0777;
1700 int o
= ::open(this->name_
, O_RDWR
| O_CREAT
| O_TRUNC
, mode
);
1702 gold_fatal(_("%s: open: %s"), this->name_
, strerror(errno
));
1705 // Write out one byte to make the file the right size.
1706 if (::lseek(o
, file_size
- 1, SEEK_SET
) < 0)
1707 gold_fatal(_("%s: lseek: %s"), this->name_
, strerror(errno
));
1709 if (::write(o
, &b
, 1) != 1)
1710 gold_fatal(_("%s: write: %s"), this->name_
, strerror(errno
));
1712 // Map the file into memory.
1713 void* base
= ::mmap(NULL
, file_size
, PROT_READ
| PROT_WRITE
,
1715 if (base
== MAP_FAILED
)
1716 gold_fatal(_("%s: mmap: %s"), this->name_
, strerror(errno
));
1717 this->base_
= static_cast<unsigned char*>(base
);
1720 // Close the output file.
1723 Output_file::close()
1725 if (::munmap(this->base_
, this->file_size_
) < 0)
1726 gold_error(_("%s: munmap: %s"), this->name_
, strerror(errno
));
1729 if (::close(this->o_
) < 0)
1730 gold_error(_("%s: close: %s"), this->name_
, strerror(errno
));
1734 // Instantiate the templates we need. We could use the configure
1735 // script to restrict this to only the ones for implemented targets.
1737 #ifdef HAVE_TARGET_32_LITTLE
1740 Output_section::add_input_section
<32, false>(
1743 const char* secname
,
1744 const elfcpp::Shdr
<32, false>& shdr
);
1747 #ifdef HAVE_TARGET_32_BIG
1750 Output_section::add_input_section
<32, true>(
1753 const char* secname
,
1754 const elfcpp::Shdr
<32, true>& shdr
);
1757 #ifdef HAVE_TARGET_64_LITTLE
1760 Output_section::add_input_section
<64, false>(
1763 const char* secname
,
1764 const elfcpp::Shdr
<64, false>& shdr
);
1767 #ifdef HAVE_TARGET_64_BIG
1770 Output_section::add_input_section
<64, true>(
1773 const char* secname
,
1774 const elfcpp::Shdr
<64, true>& shdr
);
1777 #ifdef HAVE_TARGET_32_LITTLE
1779 class Output_data_reloc
<elfcpp::SHT_REL
, false, 32, false>;
1782 #ifdef HAVE_TARGET_32_BIG
1784 class Output_data_reloc
<elfcpp::SHT_REL
, false, 32, true>;
1787 #ifdef HAVE_TARGET_64_LITTLE
1789 class Output_data_reloc
<elfcpp::SHT_REL
, false, 64, false>;
1792 #ifdef HAVE_TARGET_64_BIG
1794 class Output_data_reloc
<elfcpp::SHT_REL
, false, 64, true>;
1797 #ifdef HAVE_TARGET_32_LITTLE
1799 class Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false>;
1802 #ifdef HAVE_TARGET_32_BIG
1804 class Output_data_reloc
<elfcpp::SHT_REL
, true, 32, true>;
1807 #ifdef HAVE_TARGET_64_LITTLE
1809 class Output_data_reloc
<elfcpp::SHT_REL
, true, 64, false>;
1812 #ifdef HAVE_TARGET_64_BIG
1814 class Output_data_reloc
<elfcpp::SHT_REL
, true, 64, true>;
1817 #ifdef HAVE_TARGET_32_LITTLE
1819 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 32, false>;
1822 #ifdef HAVE_TARGET_32_BIG
1824 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 32, true>;
1827 #ifdef HAVE_TARGET_64_LITTLE
1829 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 64, false>;
1832 #ifdef HAVE_TARGET_64_BIG
1834 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 64, true>;
1837 #ifdef HAVE_TARGET_32_LITTLE
1839 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, false>;
1842 #ifdef HAVE_TARGET_32_BIG
1844 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, true>;
1847 #ifdef HAVE_TARGET_64_LITTLE
1849 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false>;
1852 #ifdef HAVE_TARGET_64_BIG
1854 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, true>;
1857 #ifdef HAVE_TARGET_32_LITTLE
1859 class Output_data_got
<32, false>;
1862 #ifdef HAVE_TARGET_32_BIG
1864 class Output_data_got
<32, true>;
1867 #ifdef HAVE_TARGET_64_LITTLE
1869 class Output_data_got
<64, false>;
1872 #ifdef HAVE_TARGET_64_BIG
1874 class Output_data_got
<64, true>;
1877 } // End namespace gold.