1 // output.cc -- manage the output file for gold
3 // Copyright 2006, 2007, 2008, 2009 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.
33 #include "libiberty.h" // for unlink_if_ordinary()
35 #include "parameters.h"
40 #include "descriptors.h"
43 // Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS
45 # define MAP_ANONYMOUS MAP_ANON
51 // Output_data variables.
53 bool Output_data::allocated_sizes_are_fixed
;
55 // Output_data methods.
57 Output_data::~Output_data()
61 // Return the default alignment for the target size.
64 Output_data::default_alignment()
66 return Output_data::default_alignment_for_size(
67 parameters
->target().get_size());
70 // Return the default alignment for a size--32 or 64.
73 Output_data::default_alignment_for_size(int size
)
83 // Output_section_header methods. This currently assumes that the
84 // segment and section lists are complete at construction time.
86 Output_section_headers::Output_section_headers(
88 const Layout::Segment_list
* segment_list
,
89 const Layout::Section_list
* section_list
,
90 const Layout::Section_list
* unattached_section_list
,
91 const Stringpool
* secnamepool
,
92 const Output_section
* shstrtab_section
)
94 segment_list_(segment_list
),
95 section_list_(section_list
),
96 unattached_section_list_(unattached_section_list
),
97 secnamepool_(secnamepool
),
98 shstrtab_section_(shstrtab_section
)
100 // Count all the sections. Start with 1 for the null section.
102 if (!parameters
->options().relocatable())
104 for (Layout::Segment_list::const_iterator p
= segment_list
->begin();
105 p
!= segment_list
->end();
107 if ((*p
)->type() == elfcpp::PT_LOAD
)
108 count
+= (*p
)->output_section_count();
112 for (Layout::Section_list::const_iterator p
= section_list
->begin();
113 p
!= section_list
->end();
115 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) != 0)
118 count
+= unattached_section_list
->size();
120 const int size
= parameters
->target().get_size();
123 shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
125 shdr_size
= elfcpp::Elf_sizes
<64>::shdr_size
;
129 this->set_data_size(count
* shdr_size
);
132 // Write out the section headers.
135 Output_section_headers::do_write(Output_file
* of
)
137 switch (parameters
->size_and_endianness())
139 #ifdef HAVE_TARGET_32_LITTLE
140 case Parameters::TARGET_32_LITTLE
:
141 this->do_sized_write
<32, false>(of
);
144 #ifdef HAVE_TARGET_32_BIG
145 case Parameters::TARGET_32_BIG
:
146 this->do_sized_write
<32, true>(of
);
149 #ifdef HAVE_TARGET_64_LITTLE
150 case Parameters::TARGET_64_LITTLE
:
151 this->do_sized_write
<64, false>(of
);
154 #ifdef HAVE_TARGET_64_BIG
155 case Parameters::TARGET_64_BIG
:
156 this->do_sized_write
<64, true>(of
);
164 template<int size
, bool big_endian
>
166 Output_section_headers::do_sized_write(Output_file
* of
)
168 off_t all_shdrs_size
= this->data_size();
169 unsigned char* view
= of
->get_output_view(this->offset(), all_shdrs_size
);
171 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
172 unsigned char* v
= view
;
175 typename
elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
176 oshdr
.put_sh_name(0);
177 oshdr
.put_sh_type(elfcpp::SHT_NULL
);
178 oshdr
.put_sh_flags(0);
179 oshdr
.put_sh_addr(0);
180 oshdr
.put_sh_offset(0);
182 size_t section_count
= (this->data_size()
183 / elfcpp::Elf_sizes
<size
>::shdr_size
);
184 if (section_count
< elfcpp::SHN_LORESERVE
)
185 oshdr
.put_sh_size(0);
187 oshdr
.put_sh_size(section_count
);
189 unsigned int shstrndx
= this->shstrtab_section_
->out_shndx();
190 if (shstrndx
< elfcpp::SHN_LORESERVE
)
191 oshdr
.put_sh_link(0);
193 oshdr
.put_sh_link(shstrndx
);
195 oshdr
.put_sh_info(0);
196 oshdr
.put_sh_addralign(0);
197 oshdr
.put_sh_entsize(0);
202 unsigned int shndx
= 1;
203 if (!parameters
->options().relocatable())
205 for (Layout::Segment_list::const_iterator p
=
206 this->segment_list_
->begin();
207 p
!= this->segment_list_
->end();
209 v
= (*p
)->write_section_headers
<size
, big_endian
>(this->layout_
,
216 for (Layout::Section_list::const_iterator p
=
217 this->section_list_
->begin();
218 p
!= this->section_list_
->end();
221 // We do unallocated sections below, except that group
222 // sections have to come first.
223 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) == 0
224 && (*p
)->type() != elfcpp::SHT_GROUP
)
226 gold_assert(shndx
== (*p
)->out_shndx());
227 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
228 (*p
)->write_header(this->layout_
, this->secnamepool_
, &oshdr
);
234 for (Layout::Section_list::const_iterator p
=
235 this->unattached_section_list_
->begin();
236 p
!= this->unattached_section_list_
->end();
239 // For a relocatable link, we did unallocated group sections
240 // above, since they have to come first.
241 if ((*p
)->type() == elfcpp::SHT_GROUP
242 && parameters
->options().relocatable())
244 gold_assert(shndx
== (*p
)->out_shndx());
245 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
246 (*p
)->write_header(this->layout_
, this->secnamepool_
, &oshdr
);
251 of
->write_output_view(this->offset(), all_shdrs_size
, view
);
254 // Output_segment_header methods.
256 Output_segment_headers::Output_segment_headers(
257 const Layout::Segment_list
& segment_list
)
258 : segment_list_(segment_list
)
260 const int size
= parameters
->target().get_size();
263 phdr_size
= elfcpp::Elf_sizes
<32>::phdr_size
;
265 phdr_size
= elfcpp::Elf_sizes
<64>::phdr_size
;
269 this->set_data_size(segment_list
.size() * phdr_size
);
273 Output_segment_headers::do_write(Output_file
* of
)
275 switch (parameters
->size_and_endianness())
277 #ifdef HAVE_TARGET_32_LITTLE
278 case Parameters::TARGET_32_LITTLE
:
279 this->do_sized_write
<32, false>(of
);
282 #ifdef HAVE_TARGET_32_BIG
283 case Parameters::TARGET_32_BIG
:
284 this->do_sized_write
<32, true>(of
);
287 #ifdef HAVE_TARGET_64_LITTLE
288 case Parameters::TARGET_64_LITTLE
:
289 this->do_sized_write
<64, false>(of
);
292 #ifdef HAVE_TARGET_64_BIG
293 case Parameters::TARGET_64_BIG
:
294 this->do_sized_write
<64, true>(of
);
302 template<int size
, bool big_endian
>
304 Output_segment_headers::do_sized_write(Output_file
* of
)
306 const int phdr_size
= elfcpp::Elf_sizes
<size
>::phdr_size
;
307 off_t all_phdrs_size
= this->segment_list_
.size() * phdr_size
;
308 gold_assert(all_phdrs_size
== this->data_size());
309 unsigned char* view
= of
->get_output_view(this->offset(),
311 unsigned char* v
= view
;
312 for (Layout::Segment_list::const_iterator p
= this->segment_list_
.begin();
313 p
!= this->segment_list_
.end();
316 elfcpp::Phdr_write
<size
, big_endian
> ophdr(v
);
317 (*p
)->write_header(&ophdr
);
321 gold_assert(v
- view
== all_phdrs_size
);
323 of
->write_output_view(this->offset(), all_phdrs_size
, view
);
326 // Output_file_header methods.
328 Output_file_header::Output_file_header(const Target
* target
,
329 const Symbol_table
* symtab
,
330 const Output_segment_headers
* osh
,
334 segment_header_(osh
),
335 section_header_(NULL
),
339 const int size
= parameters
->target().get_size();
342 ehdr_size
= elfcpp::Elf_sizes
<32>::ehdr_size
;
344 ehdr_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
348 this->set_data_size(ehdr_size
);
351 // Set the section table information for a file header.
354 Output_file_header::set_section_info(const Output_section_headers
* shdrs
,
355 const Output_section
* shstrtab
)
357 this->section_header_
= shdrs
;
358 this->shstrtab_
= shstrtab
;
361 // Write out the file header.
364 Output_file_header::do_write(Output_file
* of
)
366 gold_assert(this->offset() == 0);
368 switch (parameters
->size_and_endianness())
370 #ifdef HAVE_TARGET_32_LITTLE
371 case Parameters::TARGET_32_LITTLE
:
372 this->do_sized_write
<32, false>(of
);
375 #ifdef HAVE_TARGET_32_BIG
376 case Parameters::TARGET_32_BIG
:
377 this->do_sized_write
<32, true>(of
);
380 #ifdef HAVE_TARGET_64_LITTLE
381 case Parameters::TARGET_64_LITTLE
:
382 this->do_sized_write
<64, false>(of
);
385 #ifdef HAVE_TARGET_64_BIG
386 case Parameters::TARGET_64_BIG
:
387 this->do_sized_write
<64, true>(of
);
395 // Write out the file header with appropriate size and endianess.
397 template<int size
, bool big_endian
>
399 Output_file_header::do_sized_write(Output_file
* of
)
401 gold_assert(this->offset() == 0);
403 int ehdr_size
= elfcpp::Elf_sizes
<size
>::ehdr_size
;
404 unsigned char* view
= of
->get_output_view(0, ehdr_size
);
405 elfcpp::Ehdr_write
<size
, big_endian
> oehdr(view
);
407 unsigned char e_ident
[elfcpp::EI_NIDENT
];
408 memset(e_ident
, 0, elfcpp::EI_NIDENT
);
409 e_ident
[elfcpp::EI_MAG0
] = elfcpp::ELFMAG0
;
410 e_ident
[elfcpp::EI_MAG1
] = elfcpp::ELFMAG1
;
411 e_ident
[elfcpp::EI_MAG2
] = elfcpp::ELFMAG2
;
412 e_ident
[elfcpp::EI_MAG3
] = elfcpp::ELFMAG3
;
414 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS32
;
416 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS64
;
419 e_ident
[elfcpp::EI_DATA
] = (big_endian
420 ? elfcpp::ELFDATA2MSB
421 : elfcpp::ELFDATA2LSB
);
422 e_ident
[elfcpp::EI_VERSION
] = elfcpp::EV_CURRENT
;
423 // FIXME: Some targets may need to set EI_OSABI and EI_ABIVERSION.
424 oehdr
.put_e_ident(e_ident
);
427 if (parameters
->options().relocatable())
428 e_type
= elfcpp::ET_REL
;
429 else if (parameters
->options().shared())
430 e_type
= elfcpp::ET_DYN
;
432 e_type
= elfcpp::ET_EXEC
;
433 oehdr
.put_e_type(e_type
);
435 oehdr
.put_e_machine(this->target_
->machine_code());
436 oehdr
.put_e_version(elfcpp::EV_CURRENT
);
438 oehdr
.put_e_entry(this->entry
<size
>());
440 if (this->segment_header_
== NULL
)
441 oehdr
.put_e_phoff(0);
443 oehdr
.put_e_phoff(this->segment_header_
->offset());
445 oehdr
.put_e_shoff(this->section_header_
->offset());
447 // FIXME: The target needs to set the flags.
448 oehdr
.put_e_flags(0);
450 oehdr
.put_e_ehsize(elfcpp::Elf_sizes
<size
>::ehdr_size
);
452 if (this->segment_header_
== NULL
)
454 oehdr
.put_e_phentsize(0);
455 oehdr
.put_e_phnum(0);
459 oehdr
.put_e_phentsize(elfcpp::Elf_sizes
<size
>::phdr_size
);
460 oehdr
.put_e_phnum(this->segment_header_
->data_size()
461 / elfcpp::Elf_sizes
<size
>::phdr_size
);
464 oehdr
.put_e_shentsize(elfcpp::Elf_sizes
<size
>::shdr_size
);
465 size_t section_count
= (this->section_header_
->data_size()
466 / elfcpp::Elf_sizes
<size
>::shdr_size
);
468 if (section_count
< elfcpp::SHN_LORESERVE
)
469 oehdr
.put_e_shnum(this->section_header_
->data_size()
470 / elfcpp::Elf_sizes
<size
>::shdr_size
);
472 oehdr
.put_e_shnum(0);
474 unsigned int shstrndx
= this->shstrtab_
->out_shndx();
475 if (shstrndx
< elfcpp::SHN_LORESERVE
)
476 oehdr
.put_e_shstrndx(this->shstrtab_
->out_shndx());
478 oehdr
.put_e_shstrndx(elfcpp::SHN_XINDEX
);
480 of
->write_output_view(0, ehdr_size
, view
);
483 // Return the value to use for the entry address. THIS->ENTRY_ is the
484 // symbol specified on the command line, if any.
487 typename
elfcpp::Elf_types
<size
>::Elf_Addr
488 Output_file_header::entry()
490 const bool should_issue_warning
= (this->entry_
!= NULL
491 && !parameters
->options().relocatable()
492 && !parameters
->options().shared());
494 // FIXME: Need to support target specific entry symbol.
495 const char* entry
= this->entry_
;
499 Symbol
* sym
= this->symtab_
->lookup(entry
);
501 typename Sized_symbol
<size
>::Value_type v
;
504 Sized_symbol
<size
>* ssym
;
505 ssym
= this->symtab_
->get_sized_symbol
<size
>(sym
);
506 if (!ssym
->is_defined() && should_issue_warning
)
507 gold_warning("entry symbol '%s' exists but is not defined", entry
);
512 // We couldn't find the entry symbol. See if we can parse it as
513 // a number. This supports, e.g., -e 0x1000.
515 v
= strtoull(entry
, &endptr
, 0);
518 if (should_issue_warning
)
519 gold_warning("cannot find entry symbol '%s'", entry
);
527 // Output_data_const methods.
530 Output_data_const::do_write(Output_file
* of
)
532 of
->write(this->offset(), this->data_
.data(), this->data_
.size());
535 // Output_data_const_buffer methods.
538 Output_data_const_buffer::do_write(Output_file
* of
)
540 of
->write(this->offset(), this->p_
, this->data_size());
543 // Output_section_data methods.
545 // Record the output section, and set the entry size and such.
548 Output_section_data::set_output_section(Output_section
* os
)
550 gold_assert(this->output_section_
== NULL
);
551 this->output_section_
= os
;
552 this->do_adjust_output_section(os
);
555 // Return the section index of the output section.
558 Output_section_data::do_out_shndx() const
560 gold_assert(this->output_section_
!= NULL
);
561 return this->output_section_
->out_shndx();
564 // Set the alignment, which means we may need to update the alignment
565 // of the output section.
568 Output_section_data::set_addralign(uint64_t addralign
)
570 this->addralign_
= addralign
;
571 if (this->output_section_
!= NULL
572 && this->output_section_
->addralign() < addralign
)
573 this->output_section_
->set_addralign(addralign
);
576 // Output_data_strtab methods.
578 // Set the final data size.
581 Output_data_strtab::set_final_data_size()
583 this->strtab_
->set_string_offsets();
584 this->set_data_size(this->strtab_
->get_strtab_size());
587 // Write out a string table.
590 Output_data_strtab::do_write(Output_file
* of
)
592 this->strtab_
->write(of
, this->offset());
595 // Output_reloc methods.
597 // A reloc against a global symbol.
599 template<bool dynamic
, int size
, bool big_endian
>
600 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
606 : address_(address
), local_sym_index_(GSYM_CODE
), type_(type
),
607 is_relative_(is_relative
), is_section_symbol_(false), shndx_(INVALID_CODE
)
609 // this->type_ is a bitfield; make sure TYPE fits.
610 gold_assert(this->type_
== type
);
611 this->u1_
.gsym
= gsym
;
614 this->set_needs_dynsym_index();
617 template<bool dynamic
, int size
, bool big_endian
>
618 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
621 Sized_relobj
<size
, big_endian
>* relobj
,
625 : address_(address
), local_sym_index_(GSYM_CODE
), type_(type
),
626 is_relative_(is_relative
), is_section_symbol_(false), shndx_(shndx
)
628 gold_assert(shndx
!= INVALID_CODE
);
629 // this->type_ is a bitfield; make sure TYPE fits.
630 gold_assert(this->type_
== type
);
631 this->u1_
.gsym
= gsym
;
632 this->u2_
.relobj
= relobj
;
634 this->set_needs_dynsym_index();
637 // A reloc against a local symbol.
639 template<bool dynamic
, int size
, bool big_endian
>
640 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
641 Sized_relobj
<size
, big_endian
>* relobj
,
642 unsigned int local_sym_index
,
647 bool is_section_symbol
)
648 : address_(address
), local_sym_index_(local_sym_index
), type_(type
),
649 is_relative_(is_relative
), is_section_symbol_(is_section_symbol
),
652 gold_assert(local_sym_index
!= GSYM_CODE
653 && local_sym_index
!= INVALID_CODE
);
654 // this->type_ is a bitfield; make sure TYPE fits.
655 gold_assert(this->type_
== type
);
656 this->u1_
.relobj
= relobj
;
659 this->set_needs_dynsym_index();
662 template<bool dynamic
, int size
, bool big_endian
>
663 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
664 Sized_relobj
<size
, big_endian
>* relobj
,
665 unsigned int local_sym_index
,
670 bool is_section_symbol
)
671 : address_(address
), local_sym_index_(local_sym_index
), type_(type
),
672 is_relative_(is_relative
), is_section_symbol_(is_section_symbol
),
675 gold_assert(local_sym_index
!= GSYM_CODE
676 && local_sym_index
!= INVALID_CODE
);
677 gold_assert(shndx
!= INVALID_CODE
);
678 // this->type_ is a bitfield; make sure TYPE fits.
679 gold_assert(this->type_
== type
);
680 this->u1_
.relobj
= relobj
;
681 this->u2_
.relobj
= relobj
;
683 this->set_needs_dynsym_index();
686 // A reloc against the STT_SECTION symbol of an output section.
688 template<bool dynamic
, int size
, bool big_endian
>
689 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
694 : address_(address
), local_sym_index_(SECTION_CODE
), type_(type
),
695 is_relative_(false), is_section_symbol_(true), shndx_(INVALID_CODE
)
697 // this->type_ is a bitfield; make sure TYPE fits.
698 gold_assert(this->type_
== type
);
702 this->set_needs_dynsym_index();
704 os
->set_needs_symtab_index();
707 template<bool dynamic
, int size
, bool big_endian
>
708 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::Output_reloc(
711 Sized_relobj
<size
, big_endian
>* relobj
,
714 : address_(address
), local_sym_index_(SECTION_CODE
), type_(type
),
715 is_relative_(false), is_section_symbol_(true), shndx_(shndx
)
717 gold_assert(shndx
!= INVALID_CODE
);
718 // this->type_ is a bitfield; make sure TYPE fits.
719 gold_assert(this->type_
== type
);
721 this->u2_
.relobj
= relobj
;
723 this->set_needs_dynsym_index();
725 os
->set_needs_symtab_index();
728 // Record that we need a dynamic symbol index for this relocation.
730 template<bool dynamic
, int size
, bool big_endian
>
732 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::
733 set_needs_dynsym_index()
735 if (this->is_relative_
)
737 switch (this->local_sym_index_
)
743 this->u1_
.gsym
->set_needs_dynsym_entry();
747 this->u1_
.os
->set_needs_dynsym_index();
755 const unsigned int lsi
= this->local_sym_index_
;
756 if (!this->is_section_symbol_
)
757 this->u1_
.relobj
->set_needs_output_dynsym_entry(lsi
);
759 this->u1_
.relobj
->output_section(lsi
)->set_needs_dynsym_index();
765 // Get the symbol index of a relocation.
767 template<bool dynamic
, int size
, bool big_endian
>
769 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::get_symbol_index()
773 switch (this->local_sym_index_
)
779 if (this->u1_
.gsym
== NULL
)
782 index
= this->u1_
.gsym
->dynsym_index();
784 index
= this->u1_
.gsym
->symtab_index();
789 index
= this->u1_
.os
->dynsym_index();
791 index
= this->u1_
.os
->symtab_index();
795 // Relocations without symbols use a symbol index of 0.
801 const unsigned int lsi
= this->local_sym_index_
;
802 if (!this->is_section_symbol_
)
805 index
= this->u1_
.relobj
->dynsym_index(lsi
);
807 index
= this->u1_
.relobj
->symtab_index(lsi
);
811 Output_section
* os
= this->u1_
.relobj
->output_section(lsi
);
812 gold_assert(os
!= NULL
);
814 index
= os
->dynsym_index();
816 index
= os
->symtab_index();
821 gold_assert(index
!= -1U);
825 // For a local section symbol, get the address of the offset ADDEND
826 // within the input section.
828 template<bool dynamic
, int size
, bool big_endian
>
829 typename
elfcpp::Elf_types
<size
>::Elf_Addr
830 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::
831 local_section_offset(Addend addend
) const
833 gold_assert(this->local_sym_index_
!= GSYM_CODE
834 && this->local_sym_index_
!= SECTION_CODE
835 && this->local_sym_index_
!= INVALID_CODE
836 && this->is_section_symbol_
);
837 const unsigned int lsi
= this->local_sym_index_
;
838 Output_section
* os
= this->u1_
.relobj
->output_section(lsi
);
839 gold_assert(os
!= NULL
);
840 Address offset
= this->u1_
.relobj
->get_output_section_offset(lsi
);
841 if (offset
!= invalid_address
)
842 return offset
+ addend
;
843 // This is a merge section.
844 offset
= os
->output_address(this->u1_
.relobj
, lsi
, addend
);
845 gold_assert(offset
!= invalid_address
);
849 // Get the output address of a relocation.
851 template<bool dynamic
, int size
, bool big_endian
>
852 typename
elfcpp::Elf_types
<size
>::Elf_Addr
853 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::get_address() const
855 Address address
= this->address_
;
856 if (this->shndx_
!= INVALID_CODE
)
858 Output_section
* os
= this->u2_
.relobj
->output_section(this->shndx_
);
859 gold_assert(os
!= NULL
);
860 Address off
= this->u2_
.relobj
->get_output_section_offset(this->shndx_
);
861 if (off
!= invalid_address
)
862 address
+= os
->address() + off
;
865 address
= os
->output_address(this->u2_
.relobj
, this->shndx_
,
867 gold_assert(address
!= invalid_address
);
870 else if (this->u2_
.od
!= NULL
)
871 address
+= this->u2_
.od
->address();
875 // Write out the offset and info fields of a Rel or Rela relocation
878 template<bool dynamic
, int size
, bool big_endian
>
879 template<typename Write_rel
>
881 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::write_rel(
884 wr
->put_r_offset(this->get_address());
885 unsigned int sym_index
= this->is_relative_
? 0 : this->get_symbol_index();
886 wr
->put_r_info(elfcpp::elf_r_info
<size
>(sym_index
, this->type_
));
889 // Write out a Rel relocation.
891 template<bool dynamic
, int size
, bool big_endian
>
893 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::write(
894 unsigned char* pov
) const
896 elfcpp::Rel_write
<size
, big_endian
> orel(pov
);
897 this->write_rel(&orel
);
900 // Get the value of the symbol referred to by a Rel relocation.
902 template<bool dynamic
, int size
, bool big_endian
>
903 typename
elfcpp::Elf_types
<size
>::Elf_Addr
904 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::symbol_value(
907 if (this->local_sym_index_
== GSYM_CODE
)
909 const Sized_symbol
<size
>* sym
;
910 sym
= static_cast<const Sized_symbol
<size
>*>(this->u1_
.gsym
);
911 return sym
->value() + addend
;
913 gold_assert(this->local_sym_index_
!= SECTION_CODE
914 && this->local_sym_index_
!= INVALID_CODE
915 && !this->is_section_symbol_
);
916 const unsigned int lsi
= this->local_sym_index_
;
917 const Symbol_value
<size
>* symval
= this->u1_
.relobj
->local_symbol(lsi
);
918 return symval
->value(this->u1_
.relobj
, addend
);
921 // Reloc comparison. This function sorts the dynamic relocs for the
922 // benefit of the dynamic linker. First we sort all relative relocs
923 // to the front. Among relative relocs, we sort by output address.
924 // Among non-relative relocs, we sort by symbol index, then by output
927 template<bool dynamic
, int size
, bool big_endian
>
929 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>::
930 compare(const Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>& r2
)
933 if (this->is_relative_
)
935 if (!r2
.is_relative_
)
937 // Otherwise sort by reloc address below.
939 else if (r2
.is_relative_
)
943 unsigned int sym1
= this->get_symbol_index();
944 unsigned int sym2
= r2
.get_symbol_index();
947 else if (sym1
> sym2
)
949 // Otherwise sort by reloc address.
952 section_offset_type addr1
= this->get_address();
953 section_offset_type addr2
= r2
.get_address();
956 else if (addr1
> addr2
)
959 // Final tie breaker, in order to generate the same output on any
961 unsigned int type1
= this->type_
;
962 unsigned int type2
= r2
.type_
;
965 else if (type1
> type2
)
968 // These relocs appear to be exactly the same.
972 // Write out a Rela relocation.
974 template<bool dynamic
, int size
, bool big_endian
>
976 Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>::write(
977 unsigned char* pov
) const
979 elfcpp::Rela_write
<size
, big_endian
> orel(pov
);
980 this->rel_
.write_rel(&orel
);
981 Addend addend
= this->addend_
;
982 if (this->rel_
.is_relative())
983 addend
= this->rel_
.symbol_value(addend
);
984 else if (this->rel_
.is_local_section_symbol())
985 addend
= this->rel_
.local_section_offset(addend
);
986 orel
.put_r_addend(addend
);
989 // Output_data_reloc_base methods.
991 // Adjust the output section.
993 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
995 Output_data_reloc_base
<sh_type
, dynamic
, size
, big_endian
>
996 ::do_adjust_output_section(Output_section
* os
)
998 if (sh_type
== elfcpp::SHT_REL
)
999 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rel_size
);
1000 else if (sh_type
== elfcpp::SHT_RELA
)
1001 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rela_size
);
1005 os
->set_should_link_to_dynsym();
1007 os
->set_should_link_to_symtab();
1010 // Write out relocation data.
1012 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1014 Output_data_reloc_base
<sh_type
, dynamic
, size
, big_endian
>::do_write(
1017 const off_t off
= this->offset();
1018 const off_t oview_size
= this->data_size();
1019 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
1021 if (this->sort_relocs_
)
1023 gold_assert(dynamic
);
1024 std::sort(this->relocs_
.begin(), this->relocs_
.end(),
1025 Sort_relocs_comparison());
1028 unsigned char* pov
= oview
;
1029 for (typename
Relocs::const_iterator p
= this->relocs_
.begin();
1030 p
!= this->relocs_
.end();
1037 gold_assert(pov
- oview
== oview_size
);
1039 of
->write_output_view(off
, oview_size
, oview
);
1041 // We no longer need the relocation entries.
1042 this->relocs_
.clear();
1045 // Class Output_relocatable_relocs.
1047 template<int sh_type
, int size
, bool big_endian
>
1049 Output_relocatable_relocs
<sh_type
, size
, big_endian
>::set_final_data_size()
1051 this->set_data_size(this->rr_
->output_reloc_count()
1052 * Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
);
1055 // class Output_data_group.
1057 template<int size
, bool big_endian
>
1058 Output_data_group
<size
, big_endian
>::Output_data_group(
1059 Sized_relobj
<size
, big_endian
>* relobj
,
1060 section_size_type entry_count
,
1061 elfcpp::Elf_Word flags
,
1062 std::vector
<unsigned int>* input_shndxes
)
1063 : Output_section_data(entry_count
* 4, 4),
1067 this->input_shndxes_
.swap(*input_shndxes
);
1070 // Write out the section group, which means translating the section
1071 // indexes to apply to the output file.
1073 template<int size
, bool big_endian
>
1075 Output_data_group
<size
, big_endian
>::do_write(Output_file
* of
)
1077 const off_t off
= this->offset();
1078 const section_size_type oview_size
=
1079 convert_to_section_size_type(this->data_size());
1080 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
1082 elfcpp::Elf_Word
* contents
= reinterpret_cast<elfcpp::Elf_Word
*>(oview
);
1083 elfcpp::Swap
<32, big_endian
>::writeval(contents
, this->flags_
);
1086 for (std::vector
<unsigned int>::const_iterator p
=
1087 this->input_shndxes_
.begin();
1088 p
!= this->input_shndxes_
.end();
1091 Output_section
* os
= this->relobj_
->output_section(*p
);
1093 unsigned int output_shndx
;
1095 output_shndx
= os
->out_shndx();
1098 this->relobj_
->error(_("section group retained but "
1099 "group element discarded"));
1103 elfcpp::Swap
<32, big_endian
>::writeval(contents
, output_shndx
);
1106 size_t wrote
= reinterpret_cast<unsigned char*>(contents
) - oview
;
1107 gold_assert(wrote
== oview_size
);
1109 of
->write_output_view(off
, oview_size
, oview
);
1111 // We no longer need this information.
1112 this->input_shndxes_
.clear();
1115 // Output_data_got::Got_entry methods.
1117 // Write out the entry.
1119 template<int size
, bool big_endian
>
1121 Output_data_got
<size
, big_endian
>::Got_entry::write(unsigned char* pov
) const
1125 switch (this->local_sym_index_
)
1129 // If the symbol is resolved locally, we need to write out the
1130 // link-time value, which will be relocated dynamically by a
1131 // RELATIVE relocation.
1132 Symbol
* gsym
= this->u_
.gsym
;
1133 Sized_symbol
<size
>* sgsym
;
1134 // This cast is a bit ugly. We don't want to put a
1135 // virtual method in Symbol, because we want Symbol to be
1136 // as small as possible.
1137 sgsym
= static_cast<Sized_symbol
<size
>*>(gsym
);
1138 val
= sgsym
->value();
1143 val
= this->u_
.constant
;
1148 const unsigned int lsi
= this->local_sym_index_
;
1149 const Symbol_value
<size
>* symval
= this->u_
.object
->local_symbol(lsi
);
1150 val
= symval
->value(this->u_
.object
, 0);
1155 elfcpp::Swap
<size
, big_endian
>::writeval(pov
, val
);
1158 // Output_data_got methods.
1160 // Add an entry for a global symbol to the GOT. This returns true if
1161 // this is a new GOT entry, false if the symbol already had a GOT
1164 template<int size
, bool big_endian
>
1166 Output_data_got
<size
, big_endian
>::add_global(
1168 unsigned int got_type
)
1170 if (gsym
->has_got_offset(got_type
))
1173 this->entries_
.push_back(Got_entry(gsym
));
1174 this->set_got_size();
1175 gsym
->set_got_offset(got_type
, this->last_got_offset());
1179 // Add an entry for a global symbol to the GOT, and add a dynamic
1180 // relocation of type R_TYPE for the GOT entry.
1181 template<int size
, bool big_endian
>
1183 Output_data_got
<size
, big_endian
>::add_global_with_rel(
1185 unsigned int got_type
,
1187 unsigned int r_type
)
1189 if (gsym
->has_got_offset(got_type
))
1192 this->entries_
.push_back(Got_entry());
1193 this->set_got_size();
1194 unsigned int got_offset
= this->last_got_offset();
1195 gsym
->set_got_offset(got_type
, got_offset
);
1196 rel_dyn
->add_global(gsym
, r_type
, this, got_offset
);
1199 template<int size
, bool big_endian
>
1201 Output_data_got
<size
, big_endian
>::add_global_with_rela(
1203 unsigned int got_type
,
1205 unsigned int r_type
)
1207 if (gsym
->has_got_offset(got_type
))
1210 this->entries_
.push_back(Got_entry());
1211 this->set_got_size();
1212 unsigned int got_offset
= this->last_got_offset();
1213 gsym
->set_got_offset(got_type
, got_offset
);
1214 rela_dyn
->add_global(gsym
, r_type
, this, got_offset
, 0);
1217 // Add a pair of entries for a global symbol to the GOT, and add
1218 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1219 // If R_TYPE_2 == 0, add the second entry with no relocation.
1220 template<int size
, bool big_endian
>
1222 Output_data_got
<size
, big_endian
>::add_global_pair_with_rel(
1224 unsigned int got_type
,
1226 unsigned int r_type_1
,
1227 unsigned int r_type_2
)
1229 if (gsym
->has_got_offset(got_type
))
1232 this->entries_
.push_back(Got_entry());
1233 unsigned int got_offset
= this->last_got_offset();
1234 gsym
->set_got_offset(got_type
, got_offset
);
1235 rel_dyn
->add_global(gsym
, r_type_1
, this, got_offset
);
1237 this->entries_
.push_back(Got_entry());
1240 got_offset
= this->last_got_offset();
1241 rel_dyn
->add_global(gsym
, r_type_2
, this, got_offset
);
1244 this->set_got_size();
1247 template<int size
, bool big_endian
>
1249 Output_data_got
<size
, big_endian
>::add_global_pair_with_rela(
1251 unsigned int got_type
,
1253 unsigned int r_type_1
,
1254 unsigned int r_type_2
)
1256 if (gsym
->has_got_offset(got_type
))
1259 this->entries_
.push_back(Got_entry());
1260 unsigned int got_offset
= this->last_got_offset();
1261 gsym
->set_got_offset(got_type
, got_offset
);
1262 rela_dyn
->add_global(gsym
, r_type_1
, this, got_offset
, 0);
1264 this->entries_
.push_back(Got_entry());
1267 got_offset
= this->last_got_offset();
1268 rela_dyn
->add_global(gsym
, r_type_2
, this, got_offset
, 0);
1271 this->set_got_size();
1274 // Add an entry for a local symbol to the GOT. This returns true if
1275 // this is a new GOT entry, false if the symbol already has a GOT
1278 template<int size
, bool big_endian
>
1280 Output_data_got
<size
, big_endian
>::add_local(
1281 Sized_relobj
<size
, big_endian
>* object
,
1282 unsigned int symndx
,
1283 unsigned int got_type
)
1285 if (object
->local_has_got_offset(symndx
, got_type
))
1288 this->entries_
.push_back(Got_entry(object
, symndx
));
1289 this->set_got_size();
1290 object
->set_local_got_offset(symndx
, got_type
, this->last_got_offset());
1294 // Add an entry for a local symbol to the GOT, and add a dynamic
1295 // relocation of type R_TYPE for the GOT entry.
1296 template<int size
, bool big_endian
>
1298 Output_data_got
<size
, big_endian
>::add_local_with_rel(
1299 Sized_relobj
<size
, big_endian
>* object
,
1300 unsigned int symndx
,
1301 unsigned int got_type
,
1303 unsigned int r_type
)
1305 if (object
->local_has_got_offset(symndx
, got_type
))
1308 this->entries_
.push_back(Got_entry());
1309 this->set_got_size();
1310 unsigned int got_offset
= this->last_got_offset();
1311 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1312 rel_dyn
->add_local(object
, symndx
, r_type
, this, got_offset
);
1315 template<int size
, bool big_endian
>
1317 Output_data_got
<size
, big_endian
>::add_local_with_rela(
1318 Sized_relobj
<size
, big_endian
>* object
,
1319 unsigned int symndx
,
1320 unsigned int got_type
,
1322 unsigned int r_type
)
1324 if (object
->local_has_got_offset(symndx
, got_type
))
1327 this->entries_
.push_back(Got_entry());
1328 this->set_got_size();
1329 unsigned int got_offset
= this->last_got_offset();
1330 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1331 rela_dyn
->add_local(object
, symndx
, r_type
, this, got_offset
, 0);
1334 // Add a pair of entries for a local symbol to the GOT, and add
1335 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1336 // If R_TYPE_2 == 0, add the second entry with no relocation.
1337 template<int size
, bool big_endian
>
1339 Output_data_got
<size
, big_endian
>::add_local_pair_with_rel(
1340 Sized_relobj
<size
, big_endian
>* object
,
1341 unsigned int symndx
,
1343 unsigned int got_type
,
1345 unsigned int r_type_1
,
1346 unsigned int r_type_2
)
1348 if (object
->local_has_got_offset(symndx
, got_type
))
1351 this->entries_
.push_back(Got_entry());
1352 unsigned int got_offset
= this->last_got_offset();
1353 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1354 Output_section
* os
= object
->output_section(shndx
);
1355 rel_dyn
->add_output_section(os
, r_type_1
, this, got_offset
);
1357 this->entries_
.push_back(Got_entry(object
, symndx
));
1360 got_offset
= this->last_got_offset();
1361 rel_dyn
->add_output_section(os
, r_type_2
, this, got_offset
);
1364 this->set_got_size();
1367 template<int size
, bool big_endian
>
1369 Output_data_got
<size
, big_endian
>::add_local_pair_with_rela(
1370 Sized_relobj
<size
, big_endian
>* object
,
1371 unsigned int symndx
,
1373 unsigned int got_type
,
1375 unsigned int r_type_1
,
1376 unsigned int r_type_2
)
1378 if (object
->local_has_got_offset(symndx
, got_type
))
1381 this->entries_
.push_back(Got_entry());
1382 unsigned int got_offset
= this->last_got_offset();
1383 object
->set_local_got_offset(symndx
, got_type
, got_offset
);
1384 Output_section
* os
= object
->output_section(shndx
);
1385 rela_dyn
->add_output_section(os
, r_type_1
, this, got_offset
, 0);
1387 this->entries_
.push_back(Got_entry(object
, symndx
));
1390 got_offset
= this->last_got_offset();
1391 rela_dyn
->add_output_section(os
, r_type_2
, this, got_offset
, 0);
1394 this->set_got_size();
1397 // Write out the GOT.
1399 template<int size
, bool big_endian
>
1401 Output_data_got
<size
, big_endian
>::do_write(Output_file
* of
)
1403 const int add
= size
/ 8;
1405 const off_t off
= this->offset();
1406 const off_t oview_size
= this->data_size();
1407 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
1409 unsigned char* pov
= oview
;
1410 for (typename
Got_entries::const_iterator p
= this->entries_
.begin();
1411 p
!= this->entries_
.end();
1418 gold_assert(pov
- oview
== oview_size
);
1420 of
->write_output_view(off
, oview_size
, oview
);
1422 // We no longer need the GOT entries.
1423 this->entries_
.clear();
1426 // Output_data_dynamic::Dynamic_entry methods.
1428 // Write out the entry.
1430 template<int size
, bool big_endian
>
1432 Output_data_dynamic::Dynamic_entry::write(
1434 const Stringpool
* pool
) const
1436 typename
elfcpp::Elf_types
<size
>::Elf_WXword val
;
1437 switch (this->offset_
)
1439 case DYNAMIC_NUMBER
:
1443 case DYNAMIC_SECTION_SIZE
:
1444 val
= this->u_
.od
->data_size();
1447 case DYNAMIC_SYMBOL
:
1449 const Sized_symbol
<size
>* s
=
1450 static_cast<const Sized_symbol
<size
>*>(this->u_
.sym
);
1455 case DYNAMIC_STRING
:
1456 val
= pool
->get_offset(this->u_
.str
);
1460 val
= this->u_
.od
->address() + this->offset_
;
1464 elfcpp::Dyn_write
<size
, big_endian
> dw(pov
);
1465 dw
.put_d_tag(this->tag_
);
1469 // Output_data_dynamic methods.
1471 // Adjust the output section to set the entry size.
1474 Output_data_dynamic::do_adjust_output_section(Output_section
* os
)
1476 if (parameters
->target().get_size() == 32)
1477 os
->set_entsize(elfcpp::Elf_sizes
<32>::dyn_size
);
1478 else if (parameters
->target().get_size() == 64)
1479 os
->set_entsize(elfcpp::Elf_sizes
<64>::dyn_size
);
1484 // Set the final data size.
1487 Output_data_dynamic::set_final_data_size()
1489 // Add the terminating entry.
1490 this->add_constant(elfcpp::DT_NULL
, 0);
1493 if (parameters
->target().get_size() == 32)
1494 dyn_size
= elfcpp::Elf_sizes
<32>::dyn_size
;
1495 else if (parameters
->target().get_size() == 64)
1496 dyn_size
= elfcpp::Elf_sizes
<64>::dyn_size
;
1499 this->set_data_size(this->entries_
.size() * dyn_size
);
1502 // Write out the dynamic entries.
1505 Output_data_dynamic::do_write(Output_file
* of
)
1507 switch (parameters
->size_and_endianness())
1509 #ifdef HAVE_TARGET_32_LITTLE
1510 case Parameters::TARGET_32_LITTLE
:
1511 this->sized_write
<32, false>(of
);
1514 #ifdef HAVE_TARGET_32_BIG
1515 case Parameters::TARGET_32_BIG
:
1516 this->sized_write
<32, true>(of
);
1519 #ifdef HAVE_TARGET_64_LITTLE
1520 case Parameters::TARGET_64_LITTLE
:
1521 this->sized_write
<64, false>(of
);
1524 #ifdef HAVE_TARGET_64_BIG
1525 case Parameters::TARGET_64_BIG
:
1526 this->sized_write
<64, true>(of
);
1534 template<int size
, bool big_endian
>
1536 Output_data_dynamic::sized_write(Output_file
* of
)
1538 const int dyn_size
= elfcpp::Elf_sizes
<size
>::dyn_size
;
1540 const off_t offset
= this->offset();
1541 const off_t oview_size
= this->data_size();
1542 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1544 unsigned char* pov
= oview
;
1545 for (typename
Dynamic_entries::const_iterator p
= this->entries_
.begin();
1546 p
!= this->entries_
.end();
1549 p
->write
<size
, big_endian
>(pov
, this->pool_
);
1553 gold_assert(pov
- oview
== oview_size
);
1555 of
->write_output_view(offset
, oview_size
, oview
);
1557 // We no longer need the dynamic entries.
1558 this->entries_
.clear();
1561 // Class Output_symtab_xindex.
1564 Output_symtab_xindex::do_write(Output_file
* of
)
1566 const off_t offset
= this->offset();
1567 const off_t oview_size
= this->data_size();
1568 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1570 memset(oview
, 0, oview_size
);
1572 if (parameters
->target().is_big_endian())
1573 this->endian_do_write
<true>(oview
);
1575 this->endian_do_write
<false>(oview
);
1577 of
->write_output_view(offset
, oview_size
, oview
);
1579 // We no longer need the data.
1580 this->entries_
.clear();
1583 template<bool big_endian
>
1585 Output_symtab_xindex::endian_do_write(unsigned char* const oview
)
1587 for (Xindex_entries::const_iterator p
= this->entries_
.begin();
1588 p
!= this->entries_
.end();
1590 elfcpp::Swap
<32, big_endian
>::writeval(oview
+ p
->first
* 4, p
->second
);
1593 // Output_section::Input_section methods.
1595 // Return the data size. For an input section we store the size here.
1596 // For an Output_section_data, we have to ask it for the size.
1599 Output_section::Input_section::data_size() const
1601 if (this->is_input_section())
1602 return this->u1_
.data_size
;
1604 return this->u2_
.posd
->data_size();
1607 // Set the address and file offset.
1610 Output_section::Input_section::set_address_and_file_offset(
1613 off_t section_file_offset
)
1615 if (this->is_input_section())
1616 this->u2_
.object
->set_section_offset(this->shndx_
,
1617 file_offset
- section_file_offset
);
1619 this->u2_
.posd
->set_address_and_file_offset(address
, file_offset
);
1622 // Reset the address and file offset.
1625 Output_section::Input_section::reset_address_and_file_offset()
1627 if (!this->is_input_section())
1628 this->u2_
.posd
->reset_address_and_file_offset();
1631 // Finalize the data size.
1634 Output_section::Input_section::finalize_data_size()
1636 if (!this->is_input_section())
1637 this->u2_
.posd
->finalize_data_size();
1640 // Try to turn an input offset into an output offset. We want to
1641 // return the output offset relative to the start of this
1642 // Input_section in the output section.
1645 Output_section::Input_section::output_offset(
1646 const Relobj
* object
,
1648 section_offset_type offset
,
1649 section_offset_type
*poutput
) const
1651 if (!this->is_input_section())
1652 return this->u2_
.posd
->output_offset(object
, shndx
, offset
, poutput
);
1655 if (this->shndx_
!= shndx
|| this->u2_
.object
!= object
)
1662 // Return whether this is the merge section for the input section
1666 Output_section::Input_section::is_merge_section_for(const Relobj
* object
,
1667 unsigned int shndx
) const
1669 if (this->is_input_section())
1671 return this->u2_
.posd
->is_merge_section_for(object
, shndx
);
1674 // Write out the data. We don't have to do anything for an input
1675 // section--they are handled via Object::relocate--but this is where
1676 // we write out the data for an Output_section_data.
1679 Output_section::Input_section::write(Output_file
* of
)
1681 if (!this->is_input_section())
1682 this->u2_
.posd
->write(of
);
1685 // Write the data to a buffer. As for write(), we don't have to do
1686 // anything for an input section.
1689 Output_section::Input_section::write_to_buffer(unsigned char* buffer
)
1691 if (!this->is_input_section())
1692 this->u2_
.posd
->write_to_buffer(buffer
);
1695 // Print to a map file.
1698 Output_section::Input_section::print_to_mapfile(Mapfile
* mapfile
) const
1700 switch (this->shndx_
)
1702 case OUTPUT_SECTION_CODE
:
1703 case MERGE_DATA_SECTION_CODE
:
1704 case MERGE_STRING_SECTION_CODE
:
1705 this->u2_
.posd
->print_to_mapfile(mapfile
);
1709 mapfile
->print_input_section(this->u2_
.object
, this->shndx_
);
1714 // Output_section methods.
1716 // Construct an Output_section. NAME will point into a Stringpool.
1718 Output_section::Output_section(const char* name
, elfcpp::Elf_Word type
,
1719 elfcpp::Elf_Xword flags
)
1724 link_section_(NULL
),
1726 info_section_(NULL
),
1735 first_input_offset_(0),
1737 postprocessing_buffer_(NULL
),
1738 needs_symtab_index_(false),
1739 needs_dynsym_index_(false),
1740 should_link_to_symtab_(false),
1741 should_link_to_dynsym_(false),
1742 after_input_sections_(false),
1743 requires_postprocessing_(false),
1744 found_in_sections_clause_(false),
1745 has_load_address_(false),
1746 info_uses_section_index_(false),
1747 may_sort_attached_input_sections_(false),
1748 must_sort_attached_input_sections_(false),
1749 attached_input_sections_are_sorted_(false),
1751 is_relro_local_(false),
1754 // An unallocated section has no address. Forcing this means that
1755 // we don't need special treatment for symbols defined in debug
1757 if ((flags
& elfcpp::SHF_ALLOC
) == 0)
1758 this->set_address(0);
1761 Output_section::~Output_section()
1765 // Set the entry size.
1768 Output_section::set_entsize(uint64_t v
)
1770 if (this->entsize_
== 0)
1773 gold_assert(this->entsize_
== v
);
1776 // Add the input section SHNDX, with header SHDR, named SECNAME, in
1777 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
1778 // relocation section which applies to this section, or 0 if none, or
1779 // -1U if more than one. Return the offset of the input section
1780 // within the output section. Return -1 if the input section will
1781 // receive special handling. In the normal case we don't always keep
1782 // track of input sections for an Output_section. Instead, each
1783 // Object keeps track of the Output_section for each of its input
1784 // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
1785 // track of input sections here; this is used when SECTIONS appears in
1788 template<int size
, bool big_endian
>
1790 Output_section::add_input_section(Sized_relobj
<size
, big_endian
>* object
,
1792 const char* secname
,
1793 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
1794 unsigned int reloc_shndx
,
1795 bool have_sections_script
)
1797 elfcpp::Elf_Xword addralign
= shdr
.get_sh_addralign();
1798 if ((addralign
& (addralign
- 1)) != 0)
1800 object
->error(_("invalid alignment %lu for section \"%s\""),
1801 static_cast<unsigned long>(addralign
), secname
);
1805 if (addralign
> this->addralign_
)
1806 this->addralign_
= addralign
;
1808 typename
elfcpp::Elf_types
<size
>::Elf_WXword sh_flags
= shdr
.get_sh_flags();
1809 this->update_flags_for_input_section(sh_flags
);
1811 uint64_t entsize
= shdr
.get_sh_entsize();
1813 // .debug_str is a mergeable string section, but is not always so
1814 // marked by compilers. Mark manually here so we can optimize.
1815 if (strcmp(secname
, ".debug_str") == 0)
1817 sh_flags
|= (elfcpp::SHF_MERGE
| elfcpp::SHF_STRINGS
);
1821 // If this is a SHF_MERGE section, we pass all the input sections to
1822 // a Output_data_merge. We don't try to handle relocations for such
1823 // a section. We don't try to handle empty merge sections--they
1824 // mess up the mappings, and are useless anyhow.
1825 if ((sh_flags
& elfcpp::SHF_MERGE
) != 0
1827 && shdr
.get_sh_size() > 0)
1829 if (this->add_merge_input_section(object
, shndx
, sh_flags
,
1830 entsize
, addralign
))
1832 // Tell the relocation routines that they need to call the
1833 // output_offset method to determine the final address.
1838 off_t offset_in_section
= this->current_data_size_for_child();
1839 off_t aligned_offset_in_section
= align_address(offset_in_section
,
1842 if (aligned_offset_in_section
> offset_in_section
1843 && !have_sections_script
1844 && (sh_flags
& elfcpp::SHF_EXECINSTR
) != 0
1845 && object
->target()->has_code_fill())
1847 // We need to add some fill data. Using fill_list_ when
1848 // possible is an optimization, since we will often have fill
1849 // sections without input sections.
1850 off_t fill_len
= aligned_offset_in_section
- offset_in_section
;
1851 if (this->input_sections_
.empty())
1852 this->fills_
.push_back(Fill(offset_in_section
, fill_len
));
1855 // FIXME: When relaxing, the size needs to adjust to
1856 // maintain a constant alignment.
1857 std::string
fill_data(object
->target()->code_fill(fill_len
));
1858 Output_data_const
* odc
= new Output_data_const(fill_data
, 1);
1859 this->input_sections_
.push_back(Input_section(odc
));
1863 this->set_current_data_size_for_child(aligned_offset_in_section
1864 + shdr
.get_sh_size());
1866 // We need to keep track of this section if we are already keeping
1867 // track of sections, or if we are relaxing. Also, if this is a
1868 // section which requires sorting, or which may require sorting in
1869 // the future, we keep track of the sections. FIXME: Add test for
1871 if (have_sections_script
1872 || !this->input_sections_
.empty()
1873 || this->may_sort_attached_input_sections()
1874 || this->must_sort_attached_input_sections()
1875 || parameters
->options().user_set_Map())
1876 this->input_sections_
.push_back(Input_section(object
, shndx
,
1880 return aligned_offset_in_section
;
1883 // Add arbitrary data to an output section.
1886 Output_section::add_output_section_data(Output_section_data
* posd
)
1888 Input_section
inp(posd
);
1889 this->add_output_section_data(&inp
);
1891 if (posd
->is_data_size_valid())
1893 off_t offset_in_section
= this->current_data_size_for_child();
1894 off_t aligned_offset_in_section
= align_address(offset_in_section
,
1896 this->set_current_data_size_for_child(aligned_offset_in_section
1897 + posd
->data_size());
1901 // Add arbitrary data to an output section by Input_section.
1904 Output_section::add_output_section_data(Input_section
* inp
)
1906 if (this->input_sections_
.empty())
1907 this->first_input_offset_
= this->current_data_size_for_child();
1909 this->input_sections_
.push_back(*inp
);
1911 uint64_t addralign
= inp
->addralign();
1912 if (addralign
> this->addralign_
)
1913 this->addralign_
= addralign
;
1915 inp
->set_output_section(this);
1918 // Add a merge section to an output section.
1921 Output_section::add_output_merge_section(Output_section_data
* posd
,
1922 bool is_string
, uint64_t entsize
)
1924 Input_section
inp(posd
, is_string
, entsize
);
1925 this->add_output_section_data(&inp
);
1928 // Add an input section to a SHF_MERGE section.
1931 Output_section::add_merge_input_section(Relobj
* object
, unsigned int shndx
,
1932 uint64_t flags
, uint64_t entsize
,
1935 bool is_string
= (flags
& elfcpp::SHF_STRINGS
) != 0;
1937 // We only merge strings if the alignment is not more than the
1938 // character size. This could be handled, but it's unusual.
1939 if (is_string
&& addralign
> entsize
)
1942 Input_section_list::iterator p
;
1943 for (p
= this->input_sections_
.begin();
1944 p
!= this->input_sections_
.end();
1946 if (p
->is_merge_section(is_string
, entsize
, addralign
))
1948 p
->add_input_section(object
, shndx
);
1952 // We handle the actual constant merging in Output_merge_data or
1953 // Output_merge_string_data.
1954 Output_section_data
* posd
;
1956 posd
= new Output_merge_data(entsize
, addralign
);
1962 posd
= new Output_merge_string
<char>(addralign
);
1965 posd
= new Output_merge_string
<uint16_t>(addralign
);
1968 posd
= new Output_merge_string
<uint32_t>(addralign
);
1975 this->add_output_merge_section(posd
, is_string
, entsize
);
1976 posd
->add_input_section(object
, shndx
);
1981 // Given an address OFFSET relative to the start of input section
1982 // SHNDX in OBJECT, return whether this address is being included in
1983 // the final link. This should only be called if SHNDX in OBJECT has
1984 // a special mapping.
1987 Output_section::is_input_address_mapped(const Relobj
* object
,
1991 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
1992 p
!= this->input_sections_
.end();
1995 section_offset_type output_offset
;
1996 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
1997 return output_offset
!= -1;
2000 // By default we assume that the address is mapped. This should
2001 // only be called after we have passed all sections to Layout. At
2002 // that point we should know what we are discarding.
2006 // Given an address OFFSET relative to the start of input section
2007 // SHNDX in object OBJECT, return the output offset relative to the
2008 // start of the input section in the output section. This should only
2009 // be called if SHNDX in OBJECT has a special mapping.
2012 Output_section::output_offset(const Relobj
* object
, unsigned int shndx
,
2013 section_offset_type offset
) const
2015 // This can only be called meaningfully when layout is complete.
2016 gold_assert(Output_data::is_layout_complete());
2018 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
2019 p
!= this->input_sections_
.end();
2022 section_offset_type output_offset
;
2023 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
2024 return output_offset
;
2029 // Return the output virtual address of OFFSET relative to the start
2030 // of input section SHNDX in object OBJECT.
2033 Output_section::output_address(const Relobj
* object
, unsigned int shndx
,
2036 uint64_t addr
= this->address() + this->first_input_offset_
;
2037 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
2038 p
!= this->input_sections_
.end();
2041 addr
= align_address(addr
, p
->addralign());
2042 section_offset_type output_offset
;
2043 if (p
->output_offset(object
, shndx
, offset
, &output_offset
))
2045 if (output_offset
== -1)
2047 return addr
+ output_offset
;
2049 addr
+= p
->data_size();
2052 // If we get here, it means that we don't know the mapping for this
2053 // input section. This might happen in principle if
2054 // add_input_section were called before add_output_section_data.
2055 // But it should never actually happen.
2060 // Find the output address of the start of the merged section for
2061 // input section SHNDX in object OBJECT.
2064 Output_section::find_starting_output_address(const Relobj
* object
,
2066 uint64_t* paddr
) const
2068 uint64_t addr
= this->address() + this->first_input_offset_
;
2069 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
2070 p
!= this->input_sections_
.end();
2073 addr
= align_address(addr
, p
->addralign());
2075 // It would be nice if we could use the existing output_offset
2076 // method to get the output offset of input offset 0.
2077 // Unfortunately we don't know for sure that input offset 0 is
2079 if (p
->is_merge_section_for(object
, shndx
))
2085 addr
+= p
->data_size();
2088 // We couldn't find a merge output section for this input section.
2092 // Set the data size of an Output_section. This is where we handle
2093 // setting the addresses of any Output_section_data objects.
2096 Output_section::set_final_data_size()
2098 if (this->input_sections_
.empty())
2100 this->set_data_size(this->current_data_size_for_child());
2104 if (this->must_sort_attached_input_sections())
2105 this->sort_attached_input_sections();
2107 uint64_t address
= this->address();
2108 off_t startoff
= this->offset();
2109 off_t off
= startoff
+ this->first_input_offset_
;
2110 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2111 p
!= this->input_sections_
.end();
2114 off
= align_address(off
, p
->addralign());
2115 p
->set_address_and_file_offset(address
+ (off
- startoff
), off
,
2117 off
+= p
->data_size();
2120 this->set_data_size(off
- startoff
);
2123 // Reset the address and file offset.
2126 Output_section::do_reset_address_and_file_offset()
2128 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2129 p
!= this->input_sections_
.end();
2131 p
->reset_address_and_file_offset();
2134 // Set the TLS offset. Called only for SHT_TLS sections.
2137 Output_section::do_set_tls_offset(uint64_t tls_base
)
2139 this->tls_offset_
= this->address() - tls_base
;
2142 // In a few cases we need to sort the input sections attached to an
2143 // output section. This is used to implement the type of constructor
2144 // priority ordering implemented by the GNU linker, in which the
2145 // priority becomes part of the section name and the sections are
2146 // sorted by name. We only do this for an output section if we see an
2147 // attached input section matching ".ctor.*", ".dtor.*",
2148 // ".init_array.*" or ".fini_array.*".
2150 class Output_section::Input_section_sort_entry
2153 Input_section_sort_entry()
2154 : input_section_(), index_(-1U), section_has_name_(false),
2158 Input_section_sort_entry(const Input_section
& input_section
,
2160 : input_section_(input_section
), index_(index
),
2161 section_has_name_(input_section
.is_input_section())
2163 if (this->section_has_name_
)
2165 // This is only called single-threaded from Layout::finalize,
2166 // so it is OK to lock. Unfortunately we have no way to pass
2168 const Task
* dummy_task
= reinterpret_cast<const Task
*>(-1);
2169 Object
* obj
= input_section
.relobj();
2170 Task_lock_obj
<Object
> tl(dummy_task
, obj
);
2172 // This is a slow operation, which should be cached in
2173 // Layout::layout if this becomes a speed problem.
2174 this->section_name_
= obj
->section_name(input_section
.shndx());
2178 // Return the Input_section.
2179 const Input_section
&
2180 input_section() const
2182 gold_assert(this->index_
!= -1U);
2183 return this->input_section_
;
2186 // The index of this entry in the original list. This is used to
2187 // make the sort stable.
2191 gold_assert(this->index_
!= -1U);
2192 return this->index_
;
2195 // Whether there is a section name.
2197 section_has_name() const
2198 { return this->section_has_name_
; }
2200 // The section name.
2202 section_name() const
2204 gold_assert(this->section_has_name_
);
2205 return this->section_name_
;
2208 // Return true if the section name has a priority. This is assumed
2209 // to be true if it has a dot after the initial dot.
2211 has_priority() const
2213 gold_assert(this->section_has_name_
);
2214 return this->section_name_
.find('.', 1);
2217 // Return true if this an input file whose base name matches
2218 // FILE_NAME. The base name must have an extension of ".o", and
2219 // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
2220 // This is to match crtbegin.o as well as crtbeginS.o without
2221 // getting confused by other possibilities. Overall matching the
2222 // file name this way is a dreadful hack, but the GNU linker does it
2223 // in order to better support gcc, and we need to be compatible.
2225 match_file_name(const char* match_file_name
) const
2227 const std::string
& file_name(this->input_section_
.relobj()->name());
2228 const char* base_name
= lbasename(file_name
.c_str());
2229 size_t match_len
= strlen(match_file_name
);
2230 if (strncmp(base_name
, match_file_name
, match_len
) != 0)
2232 size_t base_len
= strlen(base_name
);
2233 if (base_len
!= match_len
+ 2 && base_len
!= match_len
+ 3)
2235 return memcmp(base_name
+ base_len
- 2, ".o", 2) == 0;
2239 // The Input_section we are sorting.
2240 Input_section input_section_
;
2241 // The index of this Input_section in the original list.
2242 unsigned int index_
;
2243 // Whether this Input_section has a section name--it won't if this
2244 // is some random Output_section_data.
2245 bool section_has_name_
;
2246 // The section name if there is one.
2247 std::string section_name_
;
2250 // Return true if S1 should come before S2 in the output section.
2253 Output_section::Input_section_sort_compare::operator()(
2254 const Output_section::Input_section_sort_entry
& s1
,
2255 const Output_section::Input_section_sort_entry
& s2
) const
2257 // crtbegin.o must come first.
2258 bool s1_begin
= s1
.match_file_name("crtbegin");
2259 bool s2_begin
= s2
.match_file_name("crtbegin");
2260 if (s1_begin
|| s2_begin
)
2266 return s1
.index() < s2
.index();
2269 // crtend.o must come last.
2270 bool s1_end
= s1
.match_file_name("crtend");
2271 bool s2_end
= s2
.match_file_name("crtend");
2272 if (s1_end
|| s2_end
)
2278 return s1
.index() < s2
.index();
2281 // We sort all the sections with no names to the end.
2282 if (!s1
.section_has_name() || !s2
.section_has_name())
2284 if (s1
.section_has_name())
2286 if (s2
.section_has_name())
2288 return s1
.index() < s2
.index();
2291 // A section with a priority follows a section without a priority.
2292 // The GNU linker does this for all but .init_array sections; until
2293 // further notice we'll assume that that is an mistake.
2294 bool s1_has_priority
= s1
.has_priority();
2295 bool s2_has_priority
= s2
.has_priority();
2296 if (s1_has_priority
&& !s2_has_priority
)
2298 if (!s1_has_priority
&& s2_has_priority
)
2301 // Otherwise we sort by name.
2302 int compare
= s1
.section_name().compare(s2
.section_name());
2306 // Otherwise we keep the input order.
2307 return s1
.index() < s2
.index();
2310 // Sort the input sections attached to an output section.
2313 Output_section::sort_attached_input_sections()
2315 if (this->attached_input_sections_are_sorted_
)
2318 // The only thing we know about an input section is the object and
2319 // the section index. We need the section name. Recomputing this
2320 // is slow but this is an unusual case. If this becomes a speed
2321 // problem we can cache the names as required in Layout::layout.
2323 // We start by building a larger vector holding a copy of each
2324 // Input_section, plus its current index in the list and its name.
2325 std::vector
<Input_section_sort_entry
> sort_list
;
2328 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2329 p
!= this->input_sections_
.end();
2331 sort_list
.push_back(Input_section_sort_entry(*p
, i
));
2333 // Sort the input sections.
2334 std::sort(sort_list
.begin(), sort_list
.end(), Input_section_sort_compare());
2336 // Copy the sorted input sections back to our list.
2337 this->input_sections_
.clear();
2338 for (std::vector
<Input_section_sort_entry
>::iterator p
= sort_list
.begin();
2339 p
!= sort_list
.end();
2341 this->input_sections_
.push_back(p
->input_section());
2343 // Remember that we sorted the input sections, since we might get
2345 this->attached_input_sections_are_sorted_
= true;
2348 // Write the section header to *OSHDR.
2350 template<int size
, bool big_endian
>
2352 Output_section::write_header(const Layout
* layout
,
2353 const Stringpool
* secnamepool
,
2354 elfcpp::Shdr_write
<size
, big_endian
>* oshdr
) const
2356 oshdr
->put_sh_name(secnamepool
->get_offset(this->name_
));
2357 oshdr
->put_sh_type(this->type_
);
2359 elfcpp::Elf_Xword flags
= this->flags_
;
2360 if (this->info_section_
!= NULL
&& this->info_uses_section_index_
)
2361 flags
|= elfcpp::SHF_INFO_LINK
;
2362 oshdr
->put_sh_flags(flags
);
2364 oshdr
->put_sh_addr(this->address());
2365 oshdr
->put_sh_offset(this->offset());
2366 oshdr
->put_sh_size(this->data_size());
2367 if (this->link_section_
!= NULL
)
2368 oshdr
->put_sh_link(this->link_section_
->out_shndx());
2369 else if (this->should_link_to_symtab_
)
2370 oshdr
->put_sh_link(layout
->symtab_section()->out_shndx());
2371 else if (this->should_link_to_dynsym_
)
2372 oshdr
->put_sh_link(layout
->dynsym_section()->out_shndx());
2374 oshdr
->put_sh_link(this->link_
);
2376 elfcpp::Elf_Word info
;
2377 if (this->info_section_
!= NULL
)
2379 if (this->info_uses_section_index_
)
2380 info
= this->info_section_
->out_shndx();
2382 info
= this->info_section_
->symtab_index();
2384 else if (this->info_symndx_
!= NULL
)
2385 info
= this->info_symndx_
->symtab_index();
2388 oshdr
->put_sh_info(info
);
2390 oshdr
->put_sh_addralign(this->addralign_
);
2391 oshdr
->put_sh_entsize(this->entsize_
);
2394 // Write out the data. For input sections the data is written out by
2395 // Object::relocate, but we have to handle Output_section_data objects
2399 Output_section::do_write(Output_file
* of
)
2401 gold_assert(!this->requires_postprocessing());
2403 off_t output_section_file_offset
= this->offset();
2404 for (Fill_list::iterator p
= this->fills_
.begin();
2405 p
!= this->fills_
.end();
2408 std::string
fill_data(parameters
->target().code_fill(p
->length()));
2409 of
->write(output_section_file_offset
+ p
->section_offset(),
2410 fill_data
.data(), fill_data
.size());
2413 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2414 p
!= this->input_sections_
.end();
2419 // If a section requires postprocessing, create the buffer to use.
2422 Output_section::create_postprocessing_buffer()
2424 gold_assert(this->requires_postprocessing());
2426 if (this->postprocessing_buffer_
!= NULL
)
2429 if (!this->input_sections_
.empty())
2431 off_t off
= this->first_input_offset_
;
2432 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2433 p
!= this->input_sections_
.end();
2436 off
= align_address(off
, p
->addralign());
2437 p
->finalize_data_size();
2438 off
+= p
->data_size();
2440 this->set_current_data_size_for_child(off
);
2443 off_t buffer_size
= this->current_data_size_for_child();
2444 this->postprocessing_buffer_
= new unsigned char[buffer_size
];
2447 // Write all the data of an Output_section into the postprocessing
2448 // buffer. This is used for sections which require postprocessing,
2449 // such as compression. Input sections are handled by
2450 // Object::Relocate.
2453 Output_section::write_to_postprocessing_buffer()
2455 gold_assert(this->requires_postprocessing());
2457 unsigned char* buffer
= this->postprocessing_buffer();
2458 for (Fill_list::iterator p
= this->fills_
.begin();
2459 p
!= this->fills_
.end();
2462 std::string
fill_data(parameters
->target().code_fill(p
->length()));
2463 memcpy(buffer
+ p
->section_offset(), fill_data
.data(),
2467 off_t off
= this->first_input_offset_
;
2468 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2469 p
!= this->input_sections_
.end();
2472 off
= align_address(off
, p
->addralign());
2473 p
->write_to_buffer(buffer
+ off
);
2474 off
+= p
->data_size();
2478 // Get the input sections for linker script processing. We leave
2479 // behind the Output_section_data entries. Note that this may be
2480 // slightly incorrect for merge sections. We will leave them behind,
2481 // but it is possible that the script says that they should follow
2482 // some other input sections, as in:
2483 // .rodata { *(.rodata) *(.rodata.cst*) }
2484 // For that matter, we don't handle this correctly:
2485 // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
2486 // With luck this will never matter.
2489 Output_section::get_input_sections(
2491 const std::string
& fill
,
2492 std::list
<std::pair
<Relobj
*, unsigned int> >* input_sections
)
2494 uint64_t orig_address
= address
;
2496 address
= align_address(address
, this->addralign());
2498 Input_section_list remaining
;
2499 for (Input_section_list::iterator p
= this->input_sections_
.begin();
2500 p
!= this->input_sections_
.end();
2503 if (p
->is_input_section())
2504 input_sections
->push_back(std::make_pair(p
->relobj(), p
->shndx()));
2507 uint64_t aligned_address
= align_address(address
, p
->addralign());
2508 if (aligned_address
!= address
&& !fill
.empty())
2510 section_size_type length
=
2511 convert_to_section_size_type(aligned_address
- address
);
2512 std::string this_fill
;
2513 this_fill
.reserve(length
);
2514 while (this_fill
.length() + fill
.length() <= length
)
2516 if (this_fill
.length() < length
)
2517 this_fill
.append(fill
, 0, length
- this_fill
.length());
2519 Output_section_data
* posd
= new Output_data_const(this_fill
, 0);
2520 remaining
.push_back(Input_section(posd
));
2522 address
= aligned_address
;
2524 remaining
.push_back(*p
);
2526 p
->finalize_data_size();
2527 address
+= p
->data_size();
2531 this->input_sections_
.swap(remaining
);
2532 this->first_input_offset_
= 0;
2534 uint64_t data_size
= address
- orig_address
;
2535 this->set_current_data_size_for_child(data_size
);
2539 // Add an input section from a script.
2542 Output_section::add_input_section_for_script(Relobj
* object
,
2547 if (addralign
> this->addralign_
)
2548 this->addralign_
= addralign
;
2550 off_t offset_in_section
= this->current_data_size_for_child();
2551 off_t aligned_offset_in_section
= align_address(offset_in_section
,
2554 this->set_current_data_size_for_child(aligned_offset_in_section
2557 this->input_sections_
.push_back(Input_section(object
, shndx
,
2558 data_size
, addralign
));
2561 // Print to the map file.
2564 Output_section::do_print_to_mapfile(Mapfile
* mapfile
) const
2566 mapfile
->print_output_section(this);
2568 for (Input_section_list::const_iterator p
= this->input_sections_
.begin();
2569 p
!= this->input_sections_
.end();
2571 p
->print_to_mapfile(mapfile
);
2574 // Print stats for merge sections to stderr.
2577 Output_section::print_merge_stats()
2579 Input_section_list::iterator p
;
2580 for (p
= this->input_sections_
.begin();
2581 p
!= this->input_sections_
.end();
2583 p
->print_merge_stats(this->name_
);
2586 // Output segment methods.
2588 Output_segment::Output_segment(elfcpp::Elf_Word type
, elfcpp::Elf_Word flags
)
2600 is_max_align_known_(false),
2601 are_addresses_set_(false)
2605 // Add an Output_section to an Output_segment.
2608 Output_segment::add_output_section(Output_section
* os
,
2609 elfcpp::Elf_Word seg_flags
)
2611 gold_assert((os
->flags() & elfcpp::SHF_ALLOC
) != 0);
2612 gold_assert(!this->is_max_align_known_
);
2614 // Update the segment flags.
2615 this->flags_
|= seg_flags
;
2617 Output_segment::Output_data_list
* pdl
;
2618 if (os
->type() == elfcpp::SHT_NOBITS
)
2619 pdl
= &this->output_bss_
;
2621 pdl
= &this->output_data_
;
2623 // So that PT_NOTE segments will work correctly, we need to ensure
2624 // that all SHT_NOTE sections are adjacent. This will normally
2625 // happen automatically, because all the SHT_NOTE input sections
2626 // will wind up in the same output section. However, it is possible
2627 // for multiple SHT_NOTE input sections to have different section
2628 // flags, and thus be in different output sections, but for the
2629 // different section flags to map into the same segment flags and
2630 // thus the same output segment.
2632 // Note that while there may be many input sections in an output
2633 // section, there are normally only a few output sections in an
2634 // output segment. This loop is expected to be fast.
2636 if (os
->type() == elfcpp::SHT_NOTE
&& !pdl
->empty())
2638 Output_segment::Output_data_list::iterator p
= pdl
->end();
2642 if ((*p
)->is_section_type(elfcpp::SHT_NOTE
))
2649 while (p
!= pdl
->begin());
2652 // Similarly, so that PT_TLS segments will work, we need to group
2653 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
2654 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
2655 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
2656 // correctly. SHF_TLS sections get added to both a PT_LOAD segment
2657 // and the PT_TLS segment -- we do this grouping only for the
2659 if (this->type_
!= elfcpp::PT_TLS
2660 && (os
->flags() & elfcpp::SHF_TLS
) != 0)
2662 pdl
= &this->output_data_
;
2663 bool nobits
= os
->type() == elfcpp::SHT_NOBITS
;
2664 bool sawtls
= false;
2665 Output_segment::Output_data_list::iterator p
= pdl
->end();
2670 if ((*p
)->is_section_flag_set(elfcpp::SHF_TLS
))
2673 // Put a NOBITS section after the first TLS section.
2674 // Put a PROGBITS section after the first TLS/PROGBITS
2676 insert
= nobits
|| !(*p
)->is_section_type(elfcpp::SHT_NOBITS
);
2680 // If we've gone past the TLS sections, but we've seen a
2681 // TLS section, then we need to insert this section now.
2692 while (p
!= pdl
->begin());
2694 // There are no TLS sections yet; put this one at the requested
2695 // location in the section list.
2698 // For the PT_GNU_RELRO segment, we need to group relro sections,
2699 // and we need to put them before any non-relro sections. Also,
2700 // relro local sections go before relro non-local sections.
2701 if (parameters
->options().relro() && os
->is_relro())
2703 gold_assert(pdl
== &this->output_data_
);
2704 Output_segment::Output_data_list::iterator p
;
2705 for (p
= pdl
->begin(); p
!= pdl
->end(); ++p
)
2707 if (!(*p
)->is_section())
2710 Output_section
* pos
= (*p
)->output_section();
2711 if (!pos
->is_relro()
2712 || (os
->is_relro_local() && !pos
->is_relro_local()))
2723 // Remove an Output_section from this segment. It is an error if it
2727 Output_segment::remove_output_section(Output_section
* os
)
2729 // We only need this for SHT_PROGBITS.
2730 gold_assert(os
->type() == elfcpp::SHT_PROGBITS
);
2731 for (Output_data_list::iterator p
= this->output_data_
.begin();
2732 p
!= this->output_data_
.end();
2737 this->output_data_
.erase(p
);
2744 // Add an Output_data (which is not an Output_section) to the start of
2748 Output_segment::add_initial_output_data(Output_data
* od
)
2750 gold_assert(!this->is_max_align_known_
);
2751 this->output_data_
.push_front(od
);
2754 // Return whether the first data section is a relro section.
2757 Output_segment::is_first_section_relro() const
2759 return (!this->output_data_
.empty()
2760 && this->output_data_
.front()->is_section()
2761 && this->output_data_
.front()->output_section()->is_relro());
2764 // Return the maximum alignment of the Output_data in Output_segment.
2767 Output_segment::maximum_alignment()
2769 if (!this->is_max_align_known_
)
2773 addralign
= Output_segment::maximum_alignment_list(&this->output_data_
);
2774 if (addralign
> this->max_align_
)
2775 this->max_align_
= addralign
;
2777 addralign
= Output_segment::maximum_alignment_list(&this->output_bss_
);
2778 if (addralign
> this->max_align_
)
2779 this->max_align_
= addralign
;
2781 // If -z relro is in effect, and the first section in this
2782 // segment is a relro section, then the segment must be aligned
2783 // to at least the common page size. This ensures that the
2784 // PT_GNU_RELRO segment will start at a page boundary.
2785 if (this->type_
== elfcpp::PT_LOAD
2786 && parameters
->options().relro()
2787 && this->is_first_section_relro())
2789 addralign
= parameters
->target().common_pagesize();
2790 if (addralign
> this->max_align_
)
2791 this->max_align_
= addralign
;
2794 this->is_max_align_known_
= true;
2797 return this->max_align_
;
2800 // Return the maximum alignment of a list of Output_data.
2803 Output_segment::maximum_alignment_list(const Output_data_list
* pdl
)
2806 for (Output_data_list::const_iterator p
= pdl
->begin();
2810 uint64_t addralign
= (*p
)->addralign();
2811 if (addralign
> ret
)
2817 // Return the number of dynamic relocs applied to this segment.
2820 Output_segment::dynamic_reloc_count() const
2822 return (this->dynamic_reloc_count_list(&this->output_data_
)
2823 + this->dynamic_reloc_count_list(&this->output_bss_
));
2826 // Return the number of dynamic relocs applied to an Output_data_list.
2829 Output_segment::dynamic_reloc_count_list(const Output_data_list
* pdl
) const
2831 unsigned int count
= 0;
2832 for (Output_data_list::const_iterator p
= pdl
->begin();
2835 count
+= (*p
)->dynamic_reloc_count();
2839 // Set the section addresses for an Output_segment. If RESET is true,
2840 // reset the addresses first. ADDR is the address and *POFF is the
2841 // file offset. Set the section indexes starting with *PSHNDX.
2842 // Return the address of the immediately following segment. Update
2843 // *POFF and *PSHNDX.
2846 Output_segment::set_section_addresses(const Layout
* layout
, bool reset
,
2847 uint64_t addr
, off_t
* poff
,
2848 unsigned int* pshndx
)
2850 gold_assert(this->type_
== elfcpp::PT_LOAD
);
2852 if (!reset
&& this->are_addresses_set_
)
2854 gold_assert(this->paddr_
== addr
);
2855 addr
= this->vaddr_
;
2859 this->vaddr_
= addr
;
2860 this->paddr_
= addr
;
2861 this->are_addresses_set_
= true;
2864 bool in_tls
= false;
2866 bool in_relro
= (parameters
->options().relro()
2867 && this->is_first_section_relro());
2869 off_t orig_off
= *poff
;
2870 this->offset_
= orig_off
;
2872 addr
= this->set_section_list_addresses(layout
, reset
, &this->output_data_
,
2873 addr
, poff
, pshndx
, &in_tls
,
2875 this->filesz_
= *poff
- orig_off
;
2879 uint64_t ret
= this->set_section_list_addresses(layout
, reset
,
2882 &in_tls
, &in_relro
);
2884 // If the last section was a TLS section, align upward to the
2885 // alignment of the TLS segment, so that the overall size of the TLS
2886 // segment is aligned.
2889 uint64_t segment_align
= layout
->tls_segment()->maximum_alignment();
2890 *poff
= align_address(*poff
, segment_align
);
2893 // If all the sections were relro sections, align upward to the
2894 // common page size.
2897 uint64_t page_align
= parameters
->target().common_pagesize();
2898 *poff
= align_address(*poff
, page_align
);
2901 this->memsz_
= *poff
- orig_off
;
2903 // Ignore the file offset adjustments made by the BSS Output_data
2910 // Set the addresses and file offsets in a list of Output_data
2914 Output_segment::set_section_list_addresses(const Layout
* layout
, bool reset
,
2915 Output_data_list
* pdl
,
2916 uint64_t addr
, off_t
* poff
,
2917 unsigned int* pshndx
,
2918 bool* in_tls
, bool* in_relro
)
2920 off_t startoff
= *poff
;
2922 off_t off
= startoff
;
2923 for (Output_data_list::iterator p
= pdl
->begin();
2928 (*p
)->reset_address_and_file_offset();
2930 // When using a linker script the section will most likely
2931 // already have an address.
2932 if (!(*p
)->is_address_valid())
2934 uint64_t align
= (*p
)->addralign();
2936 if ((*p
)->is_section_flag_set(elfcpp::SHF_TLS
))
2938 // Give the first TLS section the alignment of the
2939 // entire TLS segment. Otherwise the TLS segment as a
2940 // whole may be misaligned.
2943 Output_segment
* tls_segment
= layout
->tls_segment();
2944 gold_assert(tls_segment
!= NULL
);
2945 uint64_t segment_align
= tls_segment
->maximum_alignment();
2946 gold_assert(segment_align
>= align
);
2947 align
= segment_align
;
2954 // If this is the first section after the TLS segment,
2955 // align it to at least the alignment of the TLS
2956 // segment, so that the size of the overall TLS segment
2960 uint64_t segment_align
=
2961 layout
->tls_segment()->maximum_alignment();
2962 if (segment_align
> align
)
2963 align
= segment_align
;
2969 // If this is a non-relro section after a relro section,
2970 // align it to a common page boundary so that the dynamic
2971 // linker has a page to mark as read-only.
2973 && (!(*p
)->is_section()
2974 || !(*p
)->output_section()->is_relro()))
2976 uint64_t page_align
= parameters
->target().common_pagesize();
2977 if (page_align
> align
)
2982 off
= align_address(off
, align
);
2983 (*p
)->set_address_and_file_offset(addr
+ (off
- startoff
), off
);
2987 // The script may have inserted a skip forward, but it
2988 // better not have moved backward.
2989 gold_assert((*p
)->address() >= addr
+ (off
- startoff
));
2990 off
+= (*p
)->address() - (addr
+ (off
- startoff
));
2991 (*p
)->set_file_offset(off
);
2992 (*p
)->finalize_data_size();
2995 // We want to ignore the size of a SHF_TLS or SHT_NOBITS
2996 // section. Such a section does not affect the size of a
2998 if (!(*p
)->is_section_flag_set(elfcpp::SHF_TLS
)
2999 || !(*p
)->is_section_type(elfcpp::SHT_NOBITS
))
3000 off
+= (*p
)->data_size();
3002 if ((*p
)->is_section())
3004 (*p
)->set_out_shndx(*pshndx
);
3010 return addr
+ (off
- startoff
);
3013 // For a non-PT_LOAD segment, set the offset from the sections, if
3017 Output_segment::set_offset()
3019 gold_assert(this->type_
!= elfcpp::PT_LOAD
);
3021 gold_assert(!this->are_addresses_set_
);
3023 if (this->output_data_
.empty() && this->output_bss_
.empty())
3027 this->are_addresses_set_
= true;
3029 this->min_p_align_
= 0;
3035 const Output_data
* first
;
3036 if (this->output_data_
.empty())
3037 first
= this->output_bss_
.front();
3039 first
= this->output_data_
.front();
3040 this->vaddr_
= first
->address();
3041 this->paddr_
= (first
->has_load_address()
3042 ? first
->load_address()
3044 this->are_addresses_set_
= true;
3045 this->offset_
= first
->offset();
3047 if (this->output_data_
.empty())
3051 const Output_data
* last_data
= this->output_data_
.back();
3052 this->filesz_
= (last_data
->address()
3053 + last_data
->data_size()
3057 const Output_data
* last
;
3058 if (this->output_bss_
.empty())
3059 last
= this->output_data_
.back();
3061 last
= this->output_bss_
.back();
3062 this->memsz_
= (last
->address()
3066 // If this is a TLS segment, align the memory size. The code in
3067 // set_section_list ensures that the section after the TLS segment
3068 // is aligned to give us room.
3069 if (this->type_
== elfcpp::PT_TLS
)
3071 uint64_t segment_align
= this->maximum_alignment();
3072 gold_assert(this->vaddr_
== align_address(this->vaddr_
, segment_align
));
3073 this->memsz_
= align_address(this->memsz_
, segment_align
);
3076 // If this is a RELRO segment, align the memory size. The code in
3077 // set_section_list ensures that the section after the RELRO segment
3078 // is aligned to give us room.
3079 if (this->type_
== elfcpp::PT_GNU_RELRO
)
3081 uint64_t page_align
= parameters
->target().common_pagesize();
3082 gold_assert(this->vaddr_
== align_address(this->vaddr_
, page_align
));
3083 this->memsz_
= align_address(this->memsz_
, page_align
);
3087 // Set the TLS offsets of the sections in the PT_TLS segment.
3090 Output_segment::set_tls_offsets()
3092 gold_assert(this->type_
== elfcpp::PT_TLS
);
3094 for (Output_data_list::iterator p
= this->output_data_
.begin();
3095 p
!= this->output_data_
.end();
3097 (*p
)->set_tls_offset(this->vaddr_
);
3099 for (Output_data_list::iterator p
= this->output_bss_
.begin();
3100 p
!= this->output_bss_
.end();
3102 (*p
)->set_tls_offset(this->vaddr_
);
3105 // Return the address of the first section.
3108 Output_segment::first_section_load_address() const
3110 for (Output_data_list::const_iterator p
= this->output_data_
.begin();
3111 p
!= this->output_data_
.end();
3113 if ((*p
)->is_section())
3114 return (*p
)->has_load_address() ? (*p
)->load_address() : (*p
)->address();
3116 for (Output_data_list::const_iterator p
= this->output_bss_
.begin();
3117 p
!= this->output_bss_
.end();
3119 if ((*p
)->is_section())
3120 return (*p
)->has_load_address() ? (*p
)->load_address() : (*p
)->address();
3125 // Return the number of Output_sections in an Output_segment.
3128 Output_segment::output_section_count() const
3130 return (this->output_section_count_list(&this->output_data_
)
3131 + this->output_section_count_list(&this->output_bss_
));
3134 // Return the number of Output_sections in an Output_data_list.
3137 Output_segment::output_section_count_list(const Output_data_list
* pdl
) const
3139 unsigned int count
= 0;
3140 for (Output_data_list::const_iterator p
= pdl
->begin();
3144 if ((*p
)->is_section())
3150 // Return the section attached to the list segment with the lowest
3151 // load address. This is used when handling a PHDRS clause in a
3155 Output_segment::section_with_lowest_load_address() const
3157 Output_section
* found
= NULL
;
3158 uint64_t found_lma
= 0;
3159 this->lowest_load_address_in_list(&this->output_data_
, &found
, &found_lma
);
3161 Output_section
* found_data
= found
;
3162 this->lowest_load_address_in_list(&this->output_bss_
, &found
, &found_lma
);
3163 if (found
!= found_data
&& found_data
!= NULL
)
3165 gold_error(_("nobits section %s may not precede progbits section %s "
3167 found
->name(), found_data
->name());
3174 // Look through a list for a section with a lower load address.
3177 Output_segment::lowest_load_address_in_list(const Output_data_list
* pdl
,
3178 Output_section
** found
,
3179 uint64_t* found_lma
) const
3181 for (Output_data_list::const_iterator p
= pdl
->begin();
3185 if (!(*p
)->is_section())
3187 Output_section
* os
= static_cast<Output_section
*>(*p
);
3188 uint64_t lma
= (os
->has_load_address()
3189 ? os
->load_address()
3191 if (*found
== NULL
|| lma
< *found_lma
)
3199 // Write the segment data into *OPHDR.
3201 template<int size
, bool big_endian
>
3203 Output_segment::write_header(elfcpp::Phdr_write
<size
, big_endian
>* ophdr
)
3205 ophdr
->put_p_type(this->type_
);
3206 ophdr
->put_p_offset(this->offset_
);
3207 ophdr
->put_p_vaddr(this->vaddr_
);
3208 ophdr
->put_p_paddr(this->paddr_
);
3209 ophdr
->put_p_filesz(this->filesz_
);
3210 ophdr
->put_p_memsz(this->memsz_
);
3211 ophdr
->put_p_flags(this->flags_
);
3212 ophdr
->put_p_align(std::max(this->min_p_align_
, this->maximum_alignment()));
3215 // Write the section headers into V.
3217 template<int size
, bool big_endian
>
3219 Output_segment::write_section_headers(const Layout
* layout
,
3220 const Stringpool
* secnamepool
,
3222 unsigned int *pshndx
) const
3224 // Every section that is attached to a segment must be attached to a
3225 // PT_LOAD segment, so we only write out section headers for PT_LOAD
3227 if (this->type_
!= elfcpp::PT_LOAD
)
3230 v
= this->write_section_headers_list
<size
, big_endian
>(layout
, secnamepool
,
3231 &this->output_data_
,
3233 v
= this->write_section_headers_list
<size
, big_endian
>(layout
, secnamepool
,
3239 template<int size
, bool big_endian
>
3241 Output_segment::write_section_headers_list(const Layout
* layout
,
3242 const Stringpool
* secnamepool
,
3243 const Output_data_list
* pdl
,
3245 unsigned int* pshndx
) const
3247 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
3248 for (Output_data_list::const_iterator p
= pdl
->begin();
3252 if ((*p
)->is_section())
3254 const Output_section
* ps
= static_cast<const Output_section
*>(*p
);
3255 gold_assert(*pshndx
== ps
->out_shndx());
3256 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
3257 ps
->write_header(layout
, secnamepool
, &oshdr
);
3265 // Print the output sections to the map file.
3268 Output_segment::print_sections_to_mapfile(Mapfile
* mapfile
) const
3270 if (this->type() != elfcpp::PT_LOAD
)
3272 this->print_section_list_to_mapfile(mapfile
, &this->output_data_
);
3273 this->print_section_list_to_mapfile(mapfile
, &this->output_bss_
);
3276 // Print an output section list to the map file.
3279 Output_segment::print_section_list_to_mapfile(Mapfile
* mapfile
,
3280 const Output_data_list
* pdl
) const
3282 for (Output_data_list::const_iterator p
= pdl
->begin();
3285 (*p
)->print_to_mapfile(mapfile
);
3288 // Output_file methods.
3290 Output_file::Output_file(const char* name
)
3295 map_is_anonymous_(false),
3296 is_temporary_(false)
3300 // Open the output file.
3303 Output_file::open(off_t file_size
)
3305 this->file_size_
= file_size
;
3307 // Unlink the file first; otherwise the open() may fail if the file
3308 // is busy (e.g. it's an executable that's currently being executed).
3310 // However, the linker may be part of a system where a zero-length
3311 // file is created for it to write to, with tight permissions (gcc
3312 // 2.95 did something like this). Unlinking the file would work
3313 // around those permission controls, so we only unlink if the file
3314 // has a non-zero size. We also unlink only regular files to avoid
3315 // trouble with directories/etc.
3317 // If we fail, continue; this command is merely a best-effort attempt
3318 // to improve the odds for open().
3320 // We let the name "-" mean "stdout"
3321 if (!this->is_temporary_
)
3323 if (strcmp(this->name_
, "-") == 0)
3324 this->o_
= STDOUT_FILENO
;
3328 if (::stat(this->name_
, &s
) == 0 && s
.st_size
!= 0)
3329 unlink_if_ordinary(this->name_
);
3331 int mode
= parameters
->options().relocatable() ? 0666 : 0777;
3332 int o
= open_descriptor(-1, this->name_
, O_RDWR
| O_CREAT
| O_TRUNC
,
3335 gold_fatal(_("%s: open: %s"), this->name_
, strerror(errno
));
3343 // Resize the output file.
3346 Output_file::resize(off_t file_size
)
3348 // If the mmap is mapping an anonymous memory buffer, this is easy:
3349 // just mremap to the new size. If it's mapping to a file, we want
3350 // to unmap to flush to the file, then remap after growing the file.
3351 if (this->map_is_anonymous_
)
3353 void* base
= ::mremap(this->base_
, this->file_size_
, file_size
,
3355 if (base
== MAP_FAILED
)
3356 gold_fatal(_("%s: mremap: %s"), this->name_
, strerror(errno
));
3357 this->base_
= static_cast<unsigned char*>(base
);
3358 this->file_size_
= file_size
;
3363 this->file_size_
= file_size
;
3368 // Map the file into memory.
3373 const int o
= this->o_
;
3375 // If the output file is not a regular file, don't try to mmap it;
3376 // instead, we'll mmap a block of memory (an anonymous buffer), and
3377 // then later write the buffer to the file.
3379 struct stat statbuf
;
3380 if (o
== STDOUT_FILENO
|| o
== STDERR_FILENO
3381 || ::fstat(o
, &statbuf
) != 0
3382 || !S_ISREG(statbuf
.st_mode
)
3383 || this->is_temporary_
)
3385 this->map_is_anonymous_
= true;
3386 base
= ::mmap(NULL
, this->file_size_
, PROT_READ
| PROT_WRITE
,
3387 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
3391 // Write out one byte to make the file the right size.
3392 if (::lseek(o
, this->file_size_
- 1, SEEK_SET
) < 0)
3393 gold_fatal(_("%s: lseek: %s"), this->name_
, strerror(errno
));
3395 if (::write(o
, &b
, 1) != 1)
3396 gold_fatal(_("%s: write: %s"), this->name_
, strerror(errno
));
3398 // Map the file into memory.
3399 this->map_is_anonymous_
= false;
3400 base
= ::mmap(NULL
, this->file_size_
, PROT_READ
| PROT_WRITE
,
3403 if (base
== MAP_FAILED
)
3404 gold_fatal(_("%s: mmap: %s"), this->name_
, strerror(errno
));
3405 this->base_
= static_cast<unsigned char*>(base
);
3408 // Unmap the file from memory.
3411 Output_file::unmap()
3413 if (::munmap(this->base_
, this->file_size_
) < 0)
3414 gold_error(_("%s: munmap: %s"), this->name_
, strerror(errno
));
3418 // Close the output file.
3421 Output_file::close()
3423 // If the map isn't file-backed, we need to write it now.
3424 if (this->map_is_anonymous_
&& !this->is_temporary_
)
3426 size_t bytes_to_write
= this->file_size_
;
3427 while (bytes_to_write
> 0)
3429 ssize_t bytes_written
= ::write(this->o_
, this->base_
, bytes_to_write
);
3430 if (bytes_written
== 0)
3431 gold_error(_("%s: write: unexpected 0 return-value"), this->name_
);
3432 else if (bytes_written
< 0)
3433 gold_error(_("%s: write: %s"), this->name_
, strerror(errno
));
3435 bytes_to_write
-= bytes_written
;
3440 // We don't close stdout or stderr
3441 if (this->o_
!= STDOUT_FILENO
3442 && this->o_
!= STDERR_FILENO
3443 && !this->is_temporary_
)
3444 if (::close(this->o_
) < 0)
3445 gold_error(_("%s: close: %s"), this->name_
, strerror(errno
));
3449 // Instantiate the templates we need. We could use the configure
3450 // script to restrict this to only the ones for implemented targets.
3452 #ifdef HAVE_TARGET_32_LITTLE
3455 Output_section::add_input_section
<32, false>(
3456 Sized_relobj
<32, false>* object
,
3458 const char* secname
,
3459 const elfcpp::Shdr
<32, false>& shdr
,
3460 unsigned int reloc_shndx
,
3461 bool have_sections_script
);
3464 #ifdef HAVE_TARGET_32_BIG
3467 Output_section::add_input_section
<32, true>(
3468 Sized_relobj
<32, true>* object
,
3470 const char* secname
,
3471 const elfcpp::Shdr
<32, true>& shdr
,
3472 unsigned int reloc_shndx
,
3473 bool have_sections_script
);
3476 #ifdef HAVE_TARGET_64_LITTLE
3479 Output_section::add_input_section
<64, false>(
3480 Sized_relobj
<64, false>* object
,
3482 const char* secname
,
3483 const elfcpp::Shdr
<64, false>& shdr
,
3484 unsigned int reloc_shndx
,
3485 bool have_sections_script
);
3488 #ifdef HAVE_TARGET_64_BIG
3491 Output_section::add_input_section
<64, true>(
3492 Sized_relobj
<64, true>* object
,
3494 const char* secname
,
3495 const elfcpp::Shdr
<64, true>& shdr
,
3496 unsigned int reloc_shndx
,
3497 bool have_sections_script
);
3500 #ifdef HAVE_TARGET_32_LITTLE
3502 class Output_reloc
<elfcpp::SHT_REL
, false, 32, false>;
3505 #ifdef HAVE_TARGET_32_BIG
3507 class Output_reloc
<elfcpp::SHT_REL
, false, 32, true>;
3510 #ifdef HAVE_TARGET_64_LITTLE
3512 class Output_reloc
<elfcpp::SHT_REL
, false, 64, false>;
3515 #ifdef HAVE_TARGET_64_BIG
3517 class Output_reloc
<elfcpp::SHT_REL
, false, 64, true>;
3520 #ifdef HAVE_TARGET_32_LITTLE
3522 class Output_reloc
<elfcpp::SHT_REL
, true, 32, false>;
3525 #ifdef HAVE_TARGET_32_BIG
3527 class Output_reloc
<elfcpp::SHT_REL
, true, 32, true>;
3530 #ifdef HAVE_TARGET_64_LITTLE
3532 class Output_reloc
<elfcpp::SHT_REL
, true, 64, false>;
3535 #ifdef HAVE_TARGET_64_BIG
3537 class Output_reloc
<elfcpp::SHT_REL
, true, 64, true>;
3540 #ifdef HAVE_TARGET_32_LITTLE
3542 class Output_reloc
<elfcpp::SHT_RELA
, false, 32, false>;
3545 #ifdef HAVE_TARGET_32_BIG
3547 class Output_reloc
<elfcpp::SHT_RELA
, false, 32, true>;
3550 #ifdef HAVE_TARGET_64_LITTLE
3552 class Output_reloc
<elfcpp::SHT_RELA
, false, 64, false>;
3555 #ifdef HAVE_TARGET_64_BIG
3557 class Output_reloc
<elfcpp::SHT_RELA
, false, 64, true>;
3560 #ifdef HAVE_TARGET_32_LITTLE
3562 class Output_reloc
<elfcpp::SHT_RELA
, true, 32, false>;
3565 #ifdef HAVE_TARGET_32_BIG
3567 class Output_reloc
<elfcpp::SHT_RELA
, true, 32, true>;
3570 #ifdef HAVE_TARGET_64_LITTLE
3572 class Output_reloc
<elfcpp::SHT_RELA
, true, 64, false>;
3575 #ifdef HAVE_TARGET_64_BIG
3577 class Output_reloc
<elfcpp::SHT_RELA
, true, 64, true>;
3580 #ifdef HAVE_TARGET_32_LITTLE
3582 class Output_data_reloc
<elfcpp::SHT_REL
, false, 32, false>;
3585 #ifdef HAVE_TARGET_32_BIG
3587 class Output_data_reloc
<elfcpp::SHT_REL
, false, 32, true>;
3590 #ifdef HAVE_TARGET_64_LITTLE
3592 class Output_data_reloc
<elfcpp::SHT_REL
, false, 64, false>;
3595 #ifdef HAVE_TARGET_64_BIG
3597 class Output_data_reloc
<elfcpp::SHT_REL
, false, 64, true>;
3600 #ifdef HAVE_TARGET_32_LITTLE
3602 class Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false>;
3605 #ifdef HAVE_TARGET_32_BIG
3607 class Output_data_reloc
<elfcpp::SHT_REL
, true, 32, true>;
3610 #ifdef HAVE_TARGET_64_LITTLE
3612 class Output_data_reloc
<elfcpp::SHT_REL
, true, 64, false>;
3615 #ifdef HAVE_TARGET_64_BIG
3617 class Output_data_reloc
<elfcpp::SHT_REL
, true, 64, true>;
3620 #ifdef HAVE_TARGET_32_LITTLE
3622 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 32, false>;
3625 #ifdef HAVE_TARGET_32_BIG
3627 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 32, true>;
3630 #ifdef HAVE_TARGET_64_LITTLE
3632 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 64, false>;
3635 #ifdef HAVE_TARGET_64_BIG
3637 class Output_data_reloc
<elfcpp::SHT_RELA
, false, 64, true>;
3640 #ifdef HAVE_TARGET_32_LITTLE
3642 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, false>;
3645 #ifdef HAVE_TARGET_32_BIG
3647 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, true>;
3650 #ifdef HAVE_TARGET_64_LITTLE
3652 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false>;
3655 #ifdef HAVE_TARGET_64_BIG
3657 class Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, true>;
3660 #ifdef HAVE_TARGET_32_LITTLE
3662 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 32, false>;
3665 #ifdef HAVE_TARGET_32_BIG
3667 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 32, true>;
3670 #ifdef HAVE_TARGET_64_LITTLE
3672 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 64, false>;
3675 #ifdef HAVE_TARGET_64_BIG
3677 class Output_relocatable_relocs
<elfcpp::SHT_REL
, 64, true>;
3680 #ifdef HAVE_TARGET_32_LITTLE
3682 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 32, false>;
3685 #ifdef HAVE_TARGET_32_BIG
3687 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 32, true>;
3690 #ifdef HAVE_TARGET_64_LITTLE
3692 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 64, false>;
3695 #ifdef HAVE_TARGET_64_BIG
3697 class Output_relocatable_relocs
<elfcpp::SHT_RELA
, 64, true>;
3700 #ifdef HAVE_TARGET_32_LITTLE
3702 class Output_data_group
<32, false>;
3705 #ifdef HAVE_TARGET_32_BIG
3707 class Output_data_group
<32, true>;
3710 #ifdef HAVE_TARGET_64_LITTLE
3712 class Output_data_group
<64, false>;
3715 #ifdef HAVE_TARGET_64_BIG
3717 class Output_data_group
<64, true>;
3720 #ifdef HAVE_TARGET_32_LITTLE
3722 class Output_data_got
<32, false>;
3725 #ifdef HAVE_TARGET_32_BIG
3727 class Output_data_got
<32, true>;
3730 #ifdef HAVE_TARGET_64_LITTLE
3732 class Output_data_got
<64, false>;
3735 #ifdef HAVE_TARGET_64_BIG
3737 class Output_data_got
<64, true>;
3740 } // End namespace gold.