1 // layout.cc -- lay out output file sections 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.
32 #include "libiberty.h"
36 #include "parameters.h"
40 #include "script-sections.h"
45 #include "compressed_output.h"
46 #include "reduced_debug_output.h"
48 #include "descriptors.h"
50 #include "incremental.h"
56 // Layout::Relaxation_debug_check methods.
58 // Check that sections and special data are in reset states.
59 // We do not save states for Output_sections and special Output_data.
60 // So we check that they have not assigned any addresses or offsets.
61 // clean_up_after_relaxation simply resets their addresses and offsets.
63 Layout::Relaxation_debug_check::check_output_data_for_reset_values(
64 const Layout::Section_list
& sections
,
65 const Layout::Data_list
& special_outputs
)
67 for(Layout::Section_list::const_iterator p
= sections
.begin();
70 gold_assert((*p
)->address_and_file_offset_have_reset_values());
72 for(Layout::Data_list::const_iterator p
= special_outputs
.begin();
73 p
!= special_outputs
.end();
75 gold_assert((*p
)->address_and_file_offset_have_reset_values());
78 // Save information of SECTIONS for checking later.
81 Layout::Relaxation_debug_check::read_sections(
82 const Layout::Section_list
& sections
)
84 for(Layout::Section_list::const_iterator p
= sections
.begin();
88 Output_section
* os
= *p
;
90 info
.output_section
= os
;
91 info
.address
= os
->is_address_valid() ? os
->address() : 0;
92 info
.data_size
= os
->is_data_size_valid() ? os
->data_size() : -1;
93 info
.offset
= os
->is_offset_valid()? os
->offset() : -1 ;
94 this->section_infos_
.push_back(info
);
98 // Verify SECTIONS using previously recorded information.
101 Layout::Relaxation_debug_check::verify_sections(
102 const Layout::Section_list
& sections
)
105 for(Layout::Section_list::const_iterator p
= sections
.begin();
109 Output_section
* os
= *p
;
110 uint64_t address
= os
->is_address_valid() ? os
->address() : 0;
111 off_t data_size
= os
->is_data_size_valid() ? os
->data_size() : -1;
112 off_t offset
= os
->is_offset_valid()? os
->offset() : -1 ;
114 if (i
>= this->section_infos_
.size())
116 gold_fatal("Section_info of %s missing.\n", os
->name());
118 const Section_info
& info
= this->section_infos_
[i
];
119 if (os
!= info
.output_section
)
120 gold_fatal("Section order changed. Expecting %s but see %s\n",
121 info
.output_section
->name(), os
->name());
122 if (address
!= info
.address
123 || data_size
!= info
.data_size
124 || offset
!= info
.offset
)
125 gold_fatal("Section %s changed.\n", os
->name());
129 // Layout_task_runner methods.
131 // Lay out the sections. This is called after all the input objects
135 Layout_task_runner::run(Workqueue
* workqueue
, const Task
* task
)
137 off_t file_size
= this->layout_
->finalize(this->input_objects_
,
142 // Now we know the final size of the output file and we know where
143 // each piece of information goes.
145 if (this->mapfile_
!= NULL
)
147 this->mapfile_
->print_discarded_sections(this->input_objects_
);
148 this->layout_
->print_to_mapfile(this->mapfile_
);
151 Output_file
* of
= new Output_file(parameters
->options().output_file_name());
152 if (this->options_
.oformat_enum() != General_options::OBJECT_FORMAT_ELF
)
153 of
->set_is_temporary();
156 // Queue up the final set of tasks.
157 gold::queue_final_tasks(this->options_
, this->input_objects_
,
158 this->symtab_
, this->layout_
, workqueue
, of
);
163 Layout::Layout(int number_of_input_files
, Script_options
* script_options
)
164 : number_of_input_files_(number_of_input_files
),
165 script_options_(script_options
),
173 unattached_section_list_(),
174 special_output_list_(),
175 section_headers_(NULL
),
177 relro_segment_(NULL
),
179 symtab_section_(NULL
),
180 symtab_xindex_(NULL
),
181 dynsym_section_(NULL
),
182 dynsym_xindex_(NULL
),
183 dynamic_section_(NULL
),
184 dynamic_symbol_(NULL
),
186 eh_frame_section_(NULL
),
187 eh_frame_data_(NULL
),
188 added_eh_frame_data_(false),
189 eh_frame_hdr_section_(NULL
),
190 build_id_note_(NULL
),
194 output_file_size_(-1),
195 have_added_input_section_(false),
196 sections_are_attached_(false),
197 input_requires_executable_stack_(false),
198 input_with_gnu_stack_note_(false),
199 input_without_gnu_stack_note_(false),
200 has_static_tls_(false),
201 any_postprocessing_sections_(false),
202 resized_signatures_(false),
203 have_stabstr_section_(false),
204 incremental_inputs_(NULL
),
205 record_output_section_data_from_script_(false),
206 script_output_section_data_list_(),
207 segment_states_(NULL
),
208 relaxation_debug_check_(NULL
)
210 // Make space for more than enough segments for a typical file.
211 // This is just for efficiency--it's OK if we wind up needing more.
212 this->segment_list_
.reserve(12);
214 // We expect two unattached Output_data objects: the file header and
215 // the segment headers.
216 this->special_output_list_
.reserve(2);
218 // Initialize structure needed for an incremental build.
219 if (parameters
->options().incremental())
220 this->incremental_inputs_
= new Incremental_inputs
;
222 // The section name pool is worth optimizing in all cases, because
223 // it is small, but there are often overlaps due to .rel sections.
224 this->namepool_
.set_optimize();
227 // Hash a key we use to look up an output section mapping.
230 Layout::Hash_key::operator()(const Layout::Key
& k
) const
232 return k
.first
+ k
.second
.first
+ k
.second
.second
;
235 // Returns whether the given section is in the list of
236 // debug-sections-used-by-some-version-of-gdb. Currently,
237 // we've checked versions of gdb up to and including 6.7.1.
239 static const char* gdb_sections
[] =
241 // ".debug_aranges", // not used by gdb as of 6.7.1
247 // ".debug_pubnames", // not used by gdb as of 6.7.1
252 static const char* lines_only_debug_sections
[] =
254 // ".debug_aranges", // not used by gdb as of 6.7.1
260 // ".debug_pubnames", // not used by gdb as of 6.7.1
266 is_gdb_debug_section(const char* str
)
268 // We can do this faster: binary search or a hashtable. But why bother?
269 for (size_t i
= 0; i
< sizeof(gdb_sections
)/sizeof(*gdb_sections
); ++i
)
270 if (strcmp(str
, gdb_sections
[i
]) == 0)
276 is_lines_only_debug_section(const char* str
)
278 // We can do this faster: binary search or a hashtable. But why bother?
280 i
< sizeof(lines_only_debug_sections
)/sizeof(*lines_only_debug_sections
);
282 if (strcmp(str
, lines_only_debug_sections
[i
]) == 0)
287 // Whether to include this section in the link.
289 template<int size
, bool big_endian
>
291 Layout::include_section(Sized_relobj
<size
, big_endian
>*, const char* name
,
292 const elfcpp::Shdr
<size
, big_endian
>& shdr
)
294 if (shdr
.get_sh_flags() & elfcpp::SHF_EXCLUDE
)
297 switch (shdr
.get_sh_type())
299 case elfcpp::SHT_NULL
:
300 case elfcpp::SHT_SYMTAB
:
301 case elfcpp::SHT_DYNSYM
:
302 case elfcpp::SHT_HASH
:
303 case elfcpp::SHT_DYNAMIC
:
304 case elfcpp::SHT_SYMTAB_SHNDX
:
307 case elfcpp::SHT_STRTAB
:
308 // Discard the sections which have special meanings in the ELF
309 // ABI. Keep others (e.g., .stabstr). We could also do this by
310 // checking the sh_link fields of the appropriate sections.
311 return (strcmp(name
, ".dynstr") != 0
312 && strcmp(name
, ".strtab") != 0
313 && strcmp(name
, ".shstrtab") != 0);
315 case elfcpp::SHT_RELA
:
316 case elfcpp::SHT_REL
:
317 case elfcpp::SHT_GROUP
:
318 // If we are emitting relocations these should be handled
320 gold_assert(!parameters
->options().relocatable()
321 && !parameters
->options().emit_relocs());
324 case elfcpp::SHT_PROGBITS
:
325 if (parameters
->options().strip_debug()
326 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
328 if (is_debug_info_section(name
))
331 if (parameters
->options().strip_debug_non_line()
332 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
334 // Debugging sections can only be recognized by name.
335 if (is_prefix_of(".debug", name
)
336 && !is_lines_only_debug_section(name
))
339 if (parameters
->options().strip_debug_gdb()
340 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
342 // Debugging sections can only be recognized by name.
343 if (is_prefix_of(".debug", name
)
344 && !is_gdb_debug_section(name
))
347 if (parameters
->options().strip_lto_sections()
348 && !parameters
->options().relocatable()
349 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
351 // Ignore LTO sections containing intermediate code.
352 if (is_prefix_of(".gnu.lto_", name
))
362 // Return an output section named NAME, or NULL if there is none.
365 Layout::find_output_section(const char* name
) const
367 for (Section_list::const_iterator p
= this->section_list_
.begin();
368 p
!= this->section_list_
.end();
370 if (strcmp((*p
)->name(), name
) == 0)
375 // Return an output segment of type TYPE, with segment flags SET set
376 // and segment flags CLEAR clear. Return NULL if there is none.
379 Layout::find_output_segment(elfcpp::PT type
, elfcpp::Elf_Word set
,
380 elfcpp::Elf_Word clear
) const
382 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
383 p
!= this->segment_list_
.end();
385 if (static_cast<elfcpp::PT
>((*p
)->type()) == type
386 && ((*p
)->flags() & set
) == set
387 && ((*p
)->flags() & clear
) == 0)
392 // Return the output section to use for section NAME with type TYPE
393 // and section flags FLAGS. NAME must be canonicalized in the string
394 // pool, and NAME_KEY is the key. IS_INTERP is true if this is the
395 // .interp section. IS_DYNAMIC_LINKER_SECTION is true if this section
396 // is used by the dynamic linker. IS_RELRO is true for a relro
397 // section. IS_LAST_RELRO is true for the last relro section.
398 // IS_FIRST_NON_RELRO is true for the first non-relro section.
401 Layout::get_output_section(const char* name
, Stringpool::Key name_key
,
402 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
,
403 bool is_interp
, bool is_dynamic_linker_section
,
404 bool is_relro
, bool is_last_relro
,
405 bool is_first_non_relro
)
407 elfcpp::Elf_Xword lookup_flags
= flags
;
409 // Ignoring SHF_WRITE and SHF_EXECINSTR here means that we combine
410 // read-write with read-only sections. Some other ELF linkers do
411 // not do this. FIXME: Perhaps there should be an option
413 lookup_flags
&= ~(elfcpp::SHF_WRITE
| elfcpp::SHF_EXECINSTR
);
415 const Key
key(name_key
, std::make_pair(type
, lookup_flags
));
416 const std::pair
<Key
, Output_section
*> v(key
, NULL
);
417 std::pair
<Section_name_map::iterator
, bool> ins(
418 this->section_name_map_
.insert(v
));
421 return ins
.first
->second
;
424 // This is the first time we've seen this name/type/flags
425 // combination. For compatibility with the GNU linker, we
426 // combine sections with contents and zero flags with sections
427 // with non-zero flags. This is a workaround for cases where
428 // assembler code forgets to set section flags. FIXME: Perhaps
429 // there should be an option to control this.
430 Output_section
* os
= NULL
;
432 if (type
== elfcpp::SHT_PROGBITS
)
436 Output_section
* same_name
= this->find_output_section(name
);
437 if (same_name
!= NULL
438 && same_name
->type() == elfcpp::SHT_PROGBITS
439 && (same_name
->flags() & elfcpp::SHF_TLS
) == 0)
442 else if ((flags
& elfcpp::SHF_TLS
) == 0)
444 elfcpp::Elf_Xword zero_flags
= 0;
445 const Key
zero_key(name_key
, std::make_pair(type
, zero_flags
));
446 Section_name_map::iterator p
=
447 this->section_name_map_
.find(zero_key
);
448 if (p
!= this->section_name_map_
.end())
454 os
= this->make_output_section(name
, type
, flags
, is_interp
,
455 is_dynamic_linker_section
, is_relro
,
456 is_last_relro
, is_first_non_relro
);
457 ins
.first
->second
= os
;
462 // Pick the output section to use for section NAME, in input file
463 // RELOBJ, with type TYPE and flags FLAGS. RELOBJ may be NULL for a
464 // linker created section. IS_INPUT_SECTION is true if we are
465 // choosing an output section for an input section found in a input
466 // file. IS_INTERP is true if this is the .interp section.
467 // IS_DYNAMIC_LINKER_SECTION is true if this section is used by the
468 // dynamic linker. IS_RELRO is true for a relro section.
469 // IS_LAST_RELRO is true for the last relro section.
470 // IS_FIRST_NON_RELRO is true for the first non-relro section. This
471 // will return NULL if the input section should be discarded.
474 Layout::choose_output_section(const Relobj
* relobj
, const char* name
,
475 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
,
476 bool is_input_section
, bool is_interp
,
477 bool is_dynamic_linker_section
, bool is_relro
,
478 bool is_last_relro
, bool is_first_non_relro
)
480 // We should not see any input sections after we have attached
481 // sections to segments.
482 gold_assert(!is_input_section
|| !this->sections_are_attached_
);
484 // Some flags in the input section should not be automatically
485 // copied to the output section.
486 flags
&= ~ (elfcpp::SHF_INFO_LINK
487 | elfcpp::SHF_LINK_ORDER
490 | elfcpp::SHF_STRINGS
);
492 if (this->script_options_
->saw_sections_clause())
494 // We are using a SECTIONS clause, so the output section is
495 // chosen based only on the name.
497 Script_sections
* ss
= this->script_options_
->script_sections();
498 const char* file_name
= relobj
== NULL
? NULL
: relobj
->name().c_str();
499 Output_section
** output_section_slot
;
500 name
= ss
->output_section_name(file_name
, name
, &output_section_slot
);
503 // The SECTIONS clause says to discard this input section.
507 // If this is an orphan section--one not mentioned in the linker
508 // script--then OUTPUT_SECTION_SLOT will be NULL, and we do the
509 // default processing below.
511 if (output_section_slot
!= NULL
)
513 if (*output_section_slot
!= NULL
)
515 (*output_section_slot
)->update_flags_for_input_section(flags
);
516 return *output_section_slot
;
519 // We don't put sections found in the linker script into
520 // SECTION_NAME_MAP_. That keeps us from getting confused
521 // if an orphan section is mapped to a section with the same
522 // name as one in the linker script.
524 name
= this->namepool_
.add(name
, false, NULL
);
527 this->make_output_section(name
, type
, flags
, is_interp
,
528 is_dynamic_linker_section
, is_relro
,
529 is_last_relro
, is_first_non_relro
);
530 os
->set_found_in_sections_clause();
531 *output_section_slot
= os
;
536 // FIXME: Handle SHF_OS_NONCONFORMING somewhere.
538 // Turn NAME from the name of the input section into the name of the
541 size_t len
= strlen(name
);
543 && !this->script_options_
->saw_sections_clause()
544 && !parameters
->options().relocatable())
545 name
= Layout::output_section_name(name
, &len
);
547 Stringpool::Key name_key
;
548 name
= this->namepool_
.add_with_length(name
, len
, true, &name_key
);
550 // Find or make the output section. The output section is selected
551 // based on the section name, type, and flags.
552 return this->get_output_section(name
, name_key
, type
, flags
, is_interp
,
553 is_dynamic_linker_section
, is_relro
,
554 is_last_relro
, is_first_non_relro
);
557 // Return the output section to use for input section SHNDX, with name
558 // NAME, with header HEADER, from object OBJECT. RELOC_SHNDX is the
559 // index of a relocation section which applies to this section, or 0
560 // if none, or -1U if more than one. RELOC_TYPE is the type of the
561 // relocation section if there is one. Set *OFF to the offset of this
562 // input section without the output section. Return NULL if the
563 // section should be discarded. Set *OFF to -1 if the section
564 // contents should not be written directly to the output file, but
565 // will instead receive special handling.
567 template<int size
, bool big_endian
>
569 Layout::layout(Sized_relobj
<size
, big_endian
>* object
, unsigned int shndx
,
570 const char* name
, const elfcpp::Shdr
<size
, big_endian
>& shdr
,
571 unsigned int reloc_shndx
, unsigned int, off_t
* off
)
575 if (!this->include_section(object
, name
, shdr
))
580 // In a relocatable link a grouped section must not be combined with
581 // any other sections.
582 if (parameters
->options().relocatable()
583 && (shdr
.get_sh_flags() & elfcpp::SHF_GROUP
) != 0)
585 name
= this->namepool_
.add(name
, true, NULL
);
586 os
= this->make_output_section(name
, shdr
.get_sh_type(),
587 shdr
.get_sh_flags(), false, false,
588 false, false, false);
592 os
= this->choose_output_section(object
, name
, shdr
.get_sh_type(),
593 shdr
.get_sh_flags(), true, false,
594 false, false, false, false);
599 // By default the GNU linker sorts input sections whose names match
600 // .ctor.*, .dtor.*, .init_array.*, or .fini_array.*. The sections
601 // are sorted by name. This is used to implement constructor
602 // priority ordering. We are compatible.
603 if (!this->script_options_
->saw_sections_clause()
604 && (is_prefix_of(".ctors.", name
)
605 || is_prefix_of(".dtors.", name
)
606 || is_prefix_of(".init_array.", name
)
607 || is_prefix_of(".fini_array.", name
)))
608 os
->set_must_sort_attached_input_sections();
610 // FIXME: Handle SHF_LINK_ORDER somewhere.
612 *off
= os
->add_input_section(object
, shndx
, name
, shdr
, reloc_shndx
,
613 this->script_options_
->saw_sections_clause());
614 this->have_added_input_section_
= true;
619 // Handle a relocation section when doing a relocatable link.
621 template<int size
, bool big_endian
>
623 Layout::layout_reloc(Sized_relobj
<size
, big_endian
>* object
,
625 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
626 Output_section
* data_section
,
627 Relocatable_relocs
* rr
)
629 gold_assert(parameters
->options().relocatable()
630 || parameters
->options().emit_relocs());
632 int sh_type
= shdr
.get_sh_type();
635 if (sh_type
== elfcpp::SHT_REL
)
637 else if (sh_type
== elfcpp::SHT_RELA
)
641 name
+= data_section
->name();
643 Output_section
* os
= this->choose_output_section(object
, name
.c_str(),
647 false, false, false);
649 os
->set_should_link_to_symtab();
650 os
->set_info_section(data_section
);
652 Output_section_data
* posd
;
653 if (sh_type
== elfcpp::SHT_REL
)
655 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rel_size
);
656 posd
= new Output_relocatable_relocs
<elfcpp::SHT_REL
,
660 else if (sh_type
== elfcpp::SHT_RELA
)
662 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rela_size
);
663 posd
= new Output_relocatable_relocs
<elfcpp::SHT_RELA
,
670 os
->add_output_section_data(posd
);
671 rr
->set_output_data(posd
);
676 // Handle a group section when doing a relocatable link.
678 template<int size
, bool big_endian
>
680 Layout::layout_group(Symbol_table
* symtab
,
681 Sized_relobj
<size
, big_endian
>* object
,
683 const char* group_section_name
,
684 const char* signature
,
685 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
686 elfcpp::Elf_Word flags
,
687 std::vector
<unsigned int>* shndxes
)
689 gold_assert(parameters
->options().relocatable());
690 gold_assert(shdr
.get_sh_type() == elfcpp::SHT_GROUP
);
691 group_section_name
= this->namepool_
.add(group_section_name
, true, NULL
);
692 Output_section
* os
= this->make_output_section(group_section_name
,
698 // We need to find a symbol with the signature in the symbol table.
699 // If we don't find one now, we need to look again later.
700 Symbol
* sym
= symtab
->lookup(signature
, NULL
);
702 os
->set_info_symndx(sym
);
705 // Reserve some space to minimize reallocations.
706 if (this->group_signatures_
.empty())
707 this->group_signatures_
.reserve(this->number_of_input_files_
* 16);
709 // We will wind up using a symbol whose name is the signature.
710 // So just put the signature in the symbol name pool to save it.
711 signature
= symtab
->canonicalize_name(signature
);
712 this->group_signatures_
.push_back(Group_signature(os
, signature
));
715 os
->set_should_link_to_symtab();
718 section_size_type entry_count
=
719 convert_to_section_size_type(shdr
.get_sh_size() / 4);
720 Output_section_data
* posd
=
721 new Output_data_group
<size
, big_endian
>(object
, entry_count
, flags
,
723 os
->add_output_section_data(posd
);
726 // Special GNU handling of sections name .eh_frame. They will
727 // normally hold exception frame data as defined by the C++ ABI
728 // (http://codesourcery.com/cxx-abi/).
730 template<int size
, bool big_endian
>
732 Layout::layout_eh_frame(Sized_relobj
<size
, big_endian
>* object
,
733 const unsigned char* symbols
,
735 const unsigned char* symbol_names
,
736 off_t symbol_names_size
,
738 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
739 unsigned int reloc_shndx
, unsigned int reloc_type
,
742 gold_assert(shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
);
743 gold_assert((shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) != 0);
745 const char* const name
= ".eh_frame";
746 Output_section
* os
= this->choose_output_section(object
,
748 elfcpp::SHT_PROGBITS
,
751 false, false, false);
755 if (this->eh_frame_section_
== NULL
)
757 this->eh_frame_section_
= os
;
758 this->eh_frame_data_
= new Eh_frame();
760 if (parameters
->options().eh_frame_hdr())
762 Output_section
* hdr_os
=
763 this->choose_output_section(NULL
,
765 elfcpp::SHT_PROGBITS
,
768 false, false, false);
772 Eh_frame_hdr
* hdr_posd
= new Eh_frame_hdr(os
,
773 this->eh_frame_data_
);
774 hdr_os
->add_output_section_data(hdr_posd
);
776 hdr_os
->set_after_input_sections();
778 if (!this->script_options_
->saw_phdrs_clause())
780 Output_segment
* hdr_oseg
;
781 hdr_oseg
= this->make_output_segment(elfcpp::PT_GNU_EH_FRAME
,
783 hdr_oseg
->add_output_section(hdr_os
, elfcpp::PF_R
, false);
786 this->eh_frame_data_
->set_eh_frame_hdr(hdr_posd
);
791 gold_assert(this->eh_frame_section_
== os
);
793 if (this->eh_frame_data_
->add_ehframe_input_section(object
,
802 os
->update_flags_for_input_section(shdr
.get_sh_flags());
804 // We found a .eh_frame section we are going to optimize, so now
805 // we can add the set of optimized sections to the output
806 // section. We need to postpone adding this until we've found a
807 // section we can optimize so that the .eh_frame section in
808 // crtbegin.o winds up at the start of the output section.
809 if (!this->added_eh_frame_data_
)
811 os
->add_output_section_data(this->eh_frame_data_
);
812 this->added_eh_frame_data_
= true;
818 // We couldn't handle this .eh_frame section for some reason.
819 // Add it as a normal section.
820 bool saw_sections_clause
= this->script_options_
->saw_sections_clause();
821 *off
= os
->add_input_section(object
, shndx
, name
, shdr
, reloc_shndx
,
822 saw_sections_clause
);
823 this->have_added_input_section_
= true;
829 // Add POSD to an output section using NAME, TYPE, and FLAGS. Return
830 // the output section.
833 Layout::add_output_section_data(const char* name
, elfcpp::Elf_Word type
,
834 elfcpp::Elf_Xword flags
,
835 Output_section_data
* posd
,
836 bool is_dynamic_linker_section
,
837 bool is_relro
, bool is_last_relro
,
838 bool is_first_non_relro
)
840 Output_section
* os
= this->choose_output_section(NULL
, name
, type
, flags
,
842 is_dynamic_linker_section
,
843 is_relro
, is_last_relro
,
846 os
->add_output_section_data(posd
);
850 // Map section flags to segment flags.
853 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags
)
855 elfcpp::Elf_Word ret
= elfcpp::PF_R
;
856 if ((flags
& elfcpp::SHF_WRITE
) != 0)
858 if ((flags
& elfcpp::SHF_EXECINSTR
) != 0)
863 // Sometimes we compress sections. This is typically done for
864 // sections that are not part of normal program execution (such as
865 // .debug_* sections), and where the readers of these sections know
866 // how to deal with compressed sections. This routine doesn't say for
867 // certain whether we'll compress -- it depends on commandline options
868 // as well -- just whether this section is a candidate for compression.
869 // (The Output_compressed_section class decides whether to compress
870 // a given section, and picks the name of the compressed section.)
873 is_compressible_debug_section(const char* secname
)
875 return (strncmp(secname
, ".debug", sizeof(".debug") - 1) == 0);
878 // Make a new Output_section, and attach it to segments as
879 // appropriate. IS_INTERP is true if this is the .interp section.
880 // IS_DYNAMIC_LINKER_SECTION is true if this section is used by the
881 // dynamic linker. IS_RELRO is true if this is a relro section.
882 // IS_LAST_RELRO is true if this is the last relro section.
883 // IS_FIRST_NON_RELRO is true if this is the first non relro section.
886 Layout::make_output_section(const char* name
, elfcpp::Elf_Word type
,
887 elfcpp::Elf_Xword flags
, bool is_interp
,
888 bool is_dynamic_linker_section
, bool is_relro
,
889 bool is_last_relro
, bool is_first_non_relro
)
892 if ((flags
& elfcpp::SHF_ALLOC
) == 0
893 && strcmp(parameters
->options().compress_debug_sections(), "none") != 0
894 && is_compressible_debug_section(name
))
895 os
= new Output_compressed_section(¶meters
->options(), name
, type
,
897 else if ((flags
& elfcpp::SHF_ALLOC
) == 0
898 && parameters
->options().strip_debug_non_line()
899 && strcmp(".debug_abbrev", name
) == 0)
901 os
= this->debug_abbrev_
= new Output_reduced_debug_abbrev_section(
903 if (this->debug_info_
)
904 this->debug_info_
->set_abbreviations(this->debug_abbrev_
);
906 else if ((flags
& elfcpp::SHF_ALLOC
) == 0
907 && parameters
->options().strip_debug_non_line()
908 && strcmp(".debug_info", name
) == 0)
910 os
= this->debug_info_
= new Output_reduced_debug_info_section(
912 if (this->debug_abbrev_
)
913 this->debug_info_
->set_abbreviations(this->debug_abbrev_
);
917 // FIXME: const_cast is ugly.
918 Target
* target
= const_cast<Target
*>(¶meters
->target());
919 os
= target
->make_output_section(name
, type
, flags
);
924 if (is_dynamic_linker_section
)
925 os
->set_is_dynamic_linker_section();
929 os
->set_is_last_relro();
930 if (is_first_non_relro
)
931 os
->set_is_first_non_relro();
933 parameters
->target().new_output_section(os
);
935 this->section_list_
.push_back(os
);
937 // The GNU linker by default sorts some sections by priority, so we
938 // do the same. We need to know that this might happen before we
939 // attach any input sections.
940 if (!this->script_options_
->saw_sections_clause()
941 && (strcmp(name
, ".ctors") == 0
942 || strcmp(name
, ".dtors") == 0
943 || strcmp(name
, ".init_array") == 0
944 || strcmp(name
, ".fini_array") == 0))
945 os
->set_may_sort_attached_input_sections();
947 // With -z relro, we have to recognize the special sections by name.
948 // There is no other way.
949 if (!this->script_options_
->saw_sections_clause()
950 && parameters
->options().relro()
951 && type
== elfcpp::SHT_PROGBITS
952 && (flags
& elfcpp::SHF_ALLOC
) != 0
953 && (flags
& elfcpp::SHF_WRITE
) != 0)
955 if (strcmp(name
, ".data.rel.ro") == 0)
957 else if (strcmp(name
, ".data.rel.ro.local") == 0)
960 os
->set_is_relro_local();
964 // Check for .stab*str sections, as .stab* sections need to link to
966 if (type
== elfcpp::SHT_STRTAB
967 && !this->have_stabstr_section_
968 && strncmp(name
, ".stab", 5) == 0
969 && strcmp(name
+ strlen(name
) - 3, "str") == 0)
970 this->have_stabstr_section_
= true;
972 // If we have already attached the sections to segments, then we
973 // need to attach this one now. This happens for sections created
974 // directly by the linker.
975 if (this->sections_are_attached_
)
976 this->attach_section_to_segment(os
);
981 // Attach output sections to segments. This is called after we have
982 // seen all the input sections.
985 Layout::attach_sections_to_segments()
987 for (Section_list::iterator p
= this->section_list_
.begin();
988 p
!= this->section_list_
.end();
990 this->attach_section_to_segment(*p
);
992 this->sections_are_attached_
= true;
995 // Attach an output section to a segment.
998 Layout::attach_section_to_segment(Output_section
* os
)
1000 if ((os
->flags() & elfcpp::SHF_ALLOC
) == 0)
1001 this->unattached_section_list_
.push_back(os
);
1003 this->attach_allocated_section_to_segment(os
);
1006 // Attach an allocated output section to a segment.
1009 Layout::attach_allocated_section_to_segment(Output_section
* os
)
1011 elfcpp::Elf_Xword flags
= os
->flags();
1012 gold_assert((flags
& elfcpp::SHF_ALLOC
) != 0);
1014 if (parameters
->options().relocatable())
1017 // If we have a SECTIONS clause, we can't handle the attachment to
1018 // segments until after we've seen all the sections.
1019 if (this->script_options_
->saw_sections_clause())
1022 gold_assert(!this->script_options_
->saw_phdrs_clause());
1024 // This output section goes into a PT_LOAD segment.
1026 elfcpp::Elf_Word seg_flags
= Layout::section_flags_to_segment(flags
);
1028 bool sort_sections
= !this->script_options_
->saw_sections_clause();
1030 // In general the only thing we really care about for PT_LOAD
1031 // segments is whether or not they are writable, so that is how we
1032 // search for them. Large data sections also go into their own
1033 // PT_LOAD segment. People who need segments sorted on some other
1034 // basis will have to use a linker script.
1036 Segment_list::const_iterator p
;
1037 for (p
= this->segment_list_
.begin();
1038 p
!= this->segment_list_
.end();
1041 if ((*p
)->type() != elfcpp::PT_LOAD
)
1043 if (!parameters
->options().omagic()
1044 && ((*p
)->flags() & elfcpp::PF_W
) != (seg_flags
& elfcpp::PF_W
))
1046 // If -Tbss was specified, we need to separate the data and BSS
1048 if (parameters
->options().user_set_Tbss())
1050 if ((os
->type() == elfcpp::SHT_NOBITS
)
1051 == (*p
)->has_any_data_sections())
1054 if (os
->is_large_data_section() && !(*p
)->is_large_data_segment())
1057 (*p
)->add_output_section(os
, seg_flags
, sort_sections
);
1061 if (p
== this->segment_list_
.end())
1063 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_LOAD
,
1065 if (os
->is_large_data_section())
1066 oseg
->set_is_large_data_segment();
1067 oseg
->add_output_section(os
, seg_flags
, sort_sections
);
1070 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
1072 if (os
->type() == elfcpp::SHT_NOTE
)
1074 // See if we already have an equivalent PT_NOTE segment.
1075 for (p
= this->segment_list_
.begin();
1076 p
!= segment_list_
.end();
1079 if ((*p
)->type() == elfcpp::PT_NOTE
1080 && (((*p
)->flags() & elfcpp::PF_W
)
1081 == (seg_flags
& elfcpp::PF_W
)))
1083 (*p
)->add_output_section(os
, seg_flags
, false);
1088 if (p
== this->segment_list_
.end())
1090 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_NOTE
,
1092 oseg
->add_output_section(os
, seg_flags
, false);
1096 // If we see a loadable SHF_TLS section, we create a PT_TLS
1097 // segment. There can only be one such segment.
1098 if ((flags
& elfcpp::SHF_TLS
) != 0)
1100 if (this->tls_segment_
== NULL
)
1101 this->make_output_segment(elfcpp::PT_TLS
, seg_flags
);
1102 this->tls_segment_
->add_output_section(os
, seg_flags
, false);
1105 // If -z relro is in effect, and we see a relro section, we create a
1106 // PT_GNU_RELRO segment. There can only be one such segment.
1107 if (os
->is_relro() && parameters
->options().relro())
1109 gold_assert(seg_flags
== (elfcpp::PF_R
| elfcpp::PF_W
));
1110 if (this->relro_segment_
== NULL
)
1111 this->make_output_segment(elfcpp::PT_GNU_RELRO
, seg_flags
);
1112 this->relro_segment_
->add_output_section(os
, seg_flags
, false);
1116 // Make an output section for a script.
1119 Layout::make_output_section_for_script(const char* name
)
1121 name
= this->namepool_
.add(name
, false, NULL
);
1122 Output_section
* os
= this->make_output_section(name
, elfcpp::SHT_PROGBITS
,
1123 elfcpp::SHF_ALLOC
, false,
1124 false, false, false, false);
1125 os
->set_found_in_sections_clause();
1129 // Return the number of segments we expect to see.
1132 Layout::expected_segment_count() const
1134 size_t ret
= this->segment_list_
.size();
1136 // If we didn't see a SECTIONS clause in a linker script, we should
1137 // already have the complete list of segments. Otherwise we ask the
1138 // SECTIONS clause how many segments it expects, and add in the ones
1139 // we already have (PT_GNU_STACK, PT_GNU_EH_FRAME, etc.)
1141 if (!this->script_options_
->saw_sections_clause())
1145 const Script_sections
* ss
= this->script_options_
->script_sections();
1146 return ret
+ ss
->expected_segment_count(this);
1150 // Handle the .note.GNU-stack section at layout time. SEEN_GNU_STACK
1151 // is whether we saw a .note.GNU-stack section in the object file.
1152 // GNU_STACK_FLAGS is the section flags. The flags give the
1153 // protection required for stack memory. We record this in an
1154 // executable as a PT_GNU_STACK segment. If an object file does not
1155 // have a .note.GNU-stack segment, we must assume that it is an old
1156 // object. On some targets that will force an executable stack.
1159 Layout::layout_gnu_stack(bool seen_gnu_stack
, uint64_t gnu_stack_flags
)
1161 if (!seen_gnu_stack
)
1162 this->input_without_gnu_stack_note_
= true;
1165 this->input_with_gnu_stack_note_
= true;
1166 if ((gnu_stack_flags
& elfcpp::SHF_EXECINSTR
) != 0)
1167 this->input_requires_executable_stack_
= true;
1171 // Create automatic note sections.
1174 Layout::create_notes()
1176 this->create_gold_note();
1177 this->create_executable_stack_info();
1178 this->create_build_id();
1181 // Create the dynamic sections which are needed before we read the
1185 Layout::create_initial_dynamic_sections(Symbol_table
* symtab
)
1187 if (parameters
->doing_static_link())
1190 this->dynamic_section_
= this->choose_output_section(NULL
, ".dynamic",
1191 elfcpp::SHT_DYNAMIC
,
1193 | elfcpp::SHF_WRITE
),
1195 true, false, false);
1197 this->dynamic_symbol_
=
1198 symtab
->define_in_output_data("_DYNAMIC", NULL
, Symbol_table::PREDEFINED
,
1199 this->dynamic_section_
, 0, 0,
1200 elfcpp::STT_OBJECT
, elfcpp::STB_LOCAL
,
1201 elfcpp::STV_HIDDEN
, 0, false, false);
1203 this->dynamic_data_
= new Output_data_dynamic(&this->dynpool_
);
1205 this->dynamic_section_
->add_output_section_data(this->dynamic_data_
);
1208 // For each output section whose name can be represented as C symbol,
1209 // define __start and __stop symbols for the section. This is a GNU
1213 Layout::define_section_symbols(Symbol_table
* symtab
)
1215 for (Section_list::const_iterator p
= this->section_list_
.begin();
1216 p
!= this->section_list_
.end();
1219 const char* const name
= (*p
)->name();
1220 if (name
[strspn(name
,
1222 "ABCDEFGHIJKLMNOPWRSTUVWXYZ"
1223 "abcdefghijklmnopqrstuvwxyz"
1227 const std::string
name_string(name
);
1228 const std::string
start_name("__start_" + name_string
);
1229 const std::string
stop_name("__stop_" + name_string
);
1231 symtab
->define_in_output_data(start_name
.c_str(),
1233 Symbol_table::PREDEFINED
,
1239 elfcpp::STV_DEFAULT
,
1241 false, // offset_is_from_end
1242 true); // only_if_ref
1244 symtab
->define_in_output_data(stop_name
.c_str(),
1246 Symbol_table::PREDEFINED
,
1252 elfcpp::STV_DEFAULT
,
1254 true, // offset_is_from_end
1255 true); // only_if_ref
1260 // Define symbols for group signatures.
1263 Layout::define_group_signatures(Symbol_table
* symtab
)
1265 for (Group_signatures::iterator p
= this->group_signatures_
.begin();
1266 p
!= this->group_signatures_
.end();
1269 Symbol
* sym
= symtab
->lookup(p
->signature
, NULL
);
1271 p
->section
->set_info_symndx(sym
);
1274 // Force the name of the group section to the group
1275 // signature, and use the group's section symbol as the
1276 // signature symbol.
1277 if (strcmp(p
->section
->name(), p
->signature
) != 0)
1279 const char* name
= this->namepool_
.add(p
->signature
,
1281 p
->section
->set_name(name
);
1283 p
->section
->set_needs_symtab_index();
1284 p
->section
->set_info_section_symndx(p
->section
);
1288 this->group_signatures_
.clear();
1291 // Find the first read-only PT_LOAD segment, creating one if
1295 Layout::find_first_load_seg()
1297 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
1298 p
!= this->segment_list_
.end();
1301 if ((*p
)->type() == elfcpp::PT_LOAD
1302 && ((*p
)->flags() & elfcpp::PF_R
) != 0
1303 && (parameters
->options().omagic()
1304 || ((*p
)->flags() & elfcpp::PF_W
) == 0))
1308 gold_assert(!this->script_options_
->saw_phdrs_clause());
1310 Output_segment
* load_seg
= this->make_output_segment(elfcpp::PT_LOAD
,
1315 // Save states of all current output segments. Store saved states
1316 // in SEGMENT_STATES.
1319 Layout::save_segments(Segment_states
* segment_states
)
1321 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
1322 p
!= this->segment_list_
.end();
1325 Output_segment
* segment
= *p
;
1327 Output_segment
* copy
= new Output_segment(*segment
);
1328 (*segment_states
)[segment
] = copy
;
1332 // Restore states of output segments and delete any segment not found in
1336 Layout::restore_segments(const Segment_states
* segment_states
)
1338 // Go through the segment list and remove any segment added in the
1340 this->tls_segment_
= NULL
;
1341 this->relro_segment_
= NULL
;
1342 Segment_list::iterator list_iter
= this->segment_list_
.begin();
1343 while (list_iter
!= this->segment_list_
.end())
1345 Output_segment
* segment
= *list_iter
;
1346 Segment_states::const_iterator states_iter
=
1347 segment_states
->find(segment
);
1348 if (states_iter
!= segment_states
->end())
1350 const Output_segment
* copy
= states_iter
->second
;
1351 // Shallow copy to restore states.
1354 // Also fix up TLS and RELRO segment pointers as appropriate.
1355 if (segment
->type() == elfcpp::PT_TLS
)
1356 this->tls_segment_
= segment
;
1357 else if (segment
->type() == elfcpp::PT_GNU_RELRO
)
1358 this->relro_segment_
= segment
;
1364 list_iter
= this->segment_list_
.erase(list_iter
);
1365 // This is a segment created during section layout. It should be
1366 // safe to remove it since we should have removed all pointers to it.
1372 // Clean up after relaxation so that sections can be laid out again.
1375 Layout::clean_up_after_relaxation()
1377 // Restore the segments to point state just prior to the relaxation loop.
1378 Script_sections
* script_section
= this->script_options_
->script_sections();
1379 script_section
->release_segments();
1380 this->restore_segments(this->segment_states_
);
1382 // Reset section addresses and file offsets
1383 for (Section_list::iterator p
= this->section_list_
.begin();
1384 p
!= this->section_list_
.end();
1387 (*p
)->reset_address_and_file_offset();
1388 (*p
)->restore_states();
1391 // Reset special output object address and file offsets.
1392 for (Data_list::iterator p
= this->special_output_list_
.begin();
1393 p
!= this->special_output_list_
.end();
1395 (*p
)->reset_address_and_file_offset();
1397 // A linker script may have created some output section data objects.
1398 // They are useless now.
1399 for (Output_section_data_list::const_iterator p
=
1400 this->script_output_section_data_list_
.begin();
1401 p
!= this->script_output_section_data_list_
.end();
1404 this->script_output_section_data_list_
.clear();
1407 // Prepare for relaxation.
1410 Layout::prepare_for_relaxation()
1412 // Create an relaxation debug check if in debugging mode.
1413 if (is_debugging_enabled(DEBUG_RELAXATION
))
1414 this->relaxation_debug_check_
= new Relaxation_debug_check();
1416 // Save segment states.
1417 this->segment_states_
= new Segment_states();
1418 this->save_segments(this->segment_states_
);
1420 for(Section_list::const_iterator p
= this->section_list_
.begin();
1421 p
!= this->section_list_
.end();
1423 (*p
)->save_states();
1425 if (is_debugging_enabled(DEBUG_RELAXATION
))
1426 this->relaxation_debug_check_
->check_output_data_for_reset_values(
1427 this->section_list_
, this->special_output_list_
);
1429 // Also enable recording of output section data from scripts.
1430 this->record_output_section_data_from_script_
= true;
1433 // Relaxation loop body: If target has no relaxation, this runs only once
1434 // Otherwise, the target relaxation hook is called at the end of
1435 // each iteration. If the hook returns true, it means re-layout of
1436 // section is required.
1438 // The number of segments created by a linking script without a PHDRS
1439 // clause may be affected by section sizes and alignments. There is
1440 // a remote chance that relaxation causes different number of PT_LOAD
1441 // segments are created and sections are attached to different segments.
1442 // Therefore, we always throw away all segments created during section
1443 // layout. In order to be able to restart the section layout, we keep
1444 // a copy of the segment list right before the relaxation loop and use
1445 // that to restore the segments.
1447 // PASS is the current relaxation pass number.
1448 // SYMTAB is a symbol table.
1449 // PLOAD_SEG is the address of a pointer for the load segment.
1450 // PHDR_SEG is a pointer to the PHDR segment.
1451 // SEGMENT_HEADERS points to the output segment header.
1452 // FILE_HEADER points to the output file header.
1453 // PSHNDX is the address to store the output section index.
1456 Layout::relaxation_loop_body(
1459 Symbol_table
* symtab
,
1460 Output_segment
** pload_seg
,
1461 Output_segment
* phdr_seg
,
1462 Output_segment_headers
* segment_headers
,
1463 Output_file_header
* file_header
,
1464 unsigned int* pshndx
)
1466 // If this is not the first iteration, we need to clean up after
1467 // relaxation so that we can lay out the sections again.
1469 this->clean_up_after_relaxation();
1471 // If there is a SECTIONS clause, put all the input sections into
1472 // the required order.
1473 Output_segment
* load_seg
;
1474 if (this->script_options_
->saw_sections_clause())
1475 load_seg
= this->set_section_addresses_from_script(symtab
);
1476 else if (parameters
->options().relocatable())
1479 load_seg
= this->find_first_load_seg();
1481 if (parameters
->options().oformat_enum()
1482 != General_options::OBJECT_FORMAT_ELF
)
1485 // If the user set the address of the text segment, that may not be
1486 // compatible with putting the segment headers and file headers into
1488 if (parameters
->options().user_set_Ttext())
1491 gold_assert(phdr_seg
== NULL
1493 || this->script_options_
->saw_sections_clause());
1495 // Lay out the segment headers.
1496 if (!parameters
->options().relocatable())
1498 gold_assert(segment_headers
!= NULL
);
1499 if (load_seg
!= NULL
)
1500 load_seg
->add_initial_output_data(segment_headers
);
1501 if (phdr_seg
!= NULL
)
1502 phdr_seg
->add_initial_output_data(segment_headers
);
1505 // Lay out the file header.
1506 if (load_seg
!= NULL
)
1507 load_seg
->add_initial_output_data(file_header
);
1509 if (this->script_options_
->saw_phdrs_clause()
1510 && !parameters
->options().relocatable())
1512 // Support use of FILEHDRS and PHDRS attachments in a PHDRS
1513 // clause in a linker script.
1514 Script_sections
* ss
= this->script_options_
->script_sections();
1515 ss
->put_headers_in_phdrs(file_header
, segment_headers
);
1518 // We set the output section indexes in set_segment_offsets and
1519 // set_section_indexes.
1522 // Set the file offsets of all the segments, and all the sections
1525 if (!parameters
->options().relocatable())
1526 off
= this->set_segment_offsets(target
, load_seg
, pshndx
);
1528 off
= this->set_relocatable_section_offsets(file_header
, pshndx
);
1530 // Verify that the dummy relaxation does not change anything.
1531 if (is_debugging_enabled(DEBUG_RELAXATION
))
1534 this->relaxation_debug_check_
->read_sections(this->section_list_
);
1536 this->relaxation_debug_check_
->verify_sections(this->section_list_
);
1539 *pload_seg
= load_seg
;
1543 // Finalize the layout. When this is called, we have created all the
1544 // output sections and all the output segments which are based on
1545 // input sections. We have several things to do, and we have to do
1546 // them in the right order, so that we get the right results correctly
1549 // 1) Finalize the list of output segments and create the segment
1552 // 2) Finalize the dynamic symbol table and associated sections.
1554 // 3) Determine the final file offset of all the output segments.
1556 // 4) Determine the final file offset of all the SHF_ALLOC output
1559 // 5) Create the symbol table sections and the section name table
1562 // 6) Finalize the symbol table: set symbol values to their final
1563 // value and make a final determination of which symbols are going
1564 // into the output symbol table.
1566 // 7) Create the section table header.
1568 // 8) Determine the final file offset of all the output sections which
1569 // are not SHF_ALLOC, including the section table header.
1571 // 9) Finalize the ELF file header.
1573 // This function returns the size of the output file.
1576 Layout::finalize(const Input_objects
* input_objects
, Symbol_table
* symtab
,
1577 Target
* target
, const Task
* task
)
1579 target
->finalize_sections(this, input_objects
, symtab
);
1581 this->count_local_symbols(task
, input_objects
);
1583 this->link_stabs_sections();
1585 Output_segment
* phdr_seg
= NULL
;
1586 if (!parameters
->options().relocatable() && !parameters
->doing_static_link())
1588 // There was a dynamic object in the link. We need to create
1589 // some information for the dynamic linker.
1591 // Create the PT_PHDR segment which will hold the program
1593 if (!this->script_options_
->saw_phdrs_clause())
1594 phdr_seg
= this->make_output_segment(elfcpp::PT_PHDR
, elfcpp::PF_R
);
1596 // Create the dynamic symbol table, including the hash table.
1597 Output_section
* dynstr
;
1598 std::vector
<Symbol
*> dynamic_symbols
;
1599 unsigned int local_dynamic_count
;
1600 Versions
versions(*this->script_options()->version_script_info(),
1602 this->create_dynamic_symtab(input_objects
, symtab
, &dynstr
,
1603 &local_dynamic_count
, &dynamic_symbols
,
1606 // Create the .interp section to hold the name of the
1607 // interpreter, and put it in a PT_INTERP segment.
1608 if (!parameters
->options().shared())
1609 this->create_interp(target
);
1611 // Finish the .dynamic section to hold the dynamic data, and put
1612 // it in a PT_DYNAMIC segment.
1613 this->finish_dynamic_section(input_objects
, symtab
);
1615 // We should have added everything we need to the dynamic string
1617 this->dynpool_
.set_string_offsets();
1619 // Create the version sections. We can't do this until the
1620 // dynamic string table is complete.
1621 this->create_version_sections(&versions
, symtab
, local_dynamic_count
,
1622 dynamic_symbols
, dynstr
);
1624 // Set the size of the _DYNAMIC symbol. We can't do this until
1625 // after we call create_version_sections.
1626 this->set_dynamic_symbol_size(symtab
);
1629 if (this->incremental_inputs_
)
1631 this->incremental_inputs_
->finalize();
1632 this->create_incremental_info_sections();
1635 // Create segment headers.
1636 Output_segment_headers
* segment_headers
=
1637 (parameters
->options().relocatable()
1639 : new Output_segment_headers(this->segment_list_
));
1641 // Lay out the file header.
1642 Output_file_header
* file_header
1643 = new Output_file_header(target
, symtab
, segment_headers
,
1644 parameters
->options().entry());
1646 this->special_output_list_
.push_back(file_header
);
1647 if (segment_headers
!= NULL
)
1648 this->special_output_list_
.push_back(segment_headers
);
1650 // Find approriate places for orphan output sections if we are using
1652 if (this->script_options_
->saw_sections_clause())
1653 this->place_orphan_sections_in_script();
1655 Output_segment
* load_seg
;
1660 // Take a snapshot of the section layout as needed.
1661 if (target
->may_relax())
1662 this->prepare_for_relaxation();
1664 // Run the relaxation loop to lay out sections.
1667 off
= this->relaxation_loop_body(pass
, target
, symtab
, &load_seg
,
1668 phdr_seg
, segment_headers
, file_header
,
1672 while (target
->may_relax()
1673 && target
->relax(pass
, input_objects
, symtab
, this));
1675 // Set the file offsets of all the non-data sections we've seen so
1676 // far which don't have to wait for the input sections. We need
1677 // this in order to finalize local symbols in non-allocated
1679 off
= this->set_section_offsets(off
, BEFORE_INPUT_SECTIONS_PASS
);
1681 // Set the section indexes of all unallocated sections seen so far,
1682 // in case any of them are somehow referenced by a symbol.
1683 shndx
= this->set_section_indexes(shndx
);
1685 // Create the symbol table sections.
1686 this->create_symtab_sections(input_objects
, symtab
, shndx
, &off
);
1687 if (!parameters
->doing_static_link())
1688 this->assign_local_dynsym_offsets(input_objects
);
1690 // Process any symbol assignments from a linker script. This must
1691 // be called after the symbol table has been finalized.
1692 this->script_options_
->finalize_symbols(symtab
, this);
1694 // Create the .shstrtab section.
1695 Output_section
* shstrtab_section
= this->create_shstrtab();
1697 // Set the file offsets of the rest of the non-data sections which
1698 // don't have to wait for the input sections.
1699 off
= this->set_section_offsets(off
, BEFORE_INPUT_SECTIONS_PASS
);
1701 // Now that all sections have been created, set the section indexes
1702 // for any sections which haven't been done yet.
1703 shndx
= this->set_section_indexes(shndx
);
1705 // Create the section table header.
1706 this->create_shdrs(shstrtab_section
, &off
);
1708 // If there are no sections which require postprocessing, we can
1709 // handle the section names now, and avoid a resize later.
1710 if (!this->any_postprocessing_sections_
)
1711 off
= this->set_section_offsets(off
,
1712 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
);
1714 file_header
->set_section_info(this->section_headers_
, shstrtab_section
);
1716 // Now we know exactly where everything goes in the output file
1717 // (except for non-allocated sections which require postprocessing).
1718 Output_data::layout_complete();
1720 this->output_file_size_
= off
;
1725 // Create a note header following the format defined in the ELF ABI.
1726 // NAME is the name, NOTE_TYPE is the type, SECTION_NAME is the name
1727 // of the section to create, DESCSZ is the size of the descriptor.
1728 // ALLOCATE is true if the section should be allocated in memory.
1729 // This returns the new note section. It sets *TRAILING_PADDING to
1730 // the number of trailing zero bytes required.
1733 Layout::create_note(const char* name
, int note_type
,
1734 const char* section_name
, size_t descsz
,
1735 bool allocate
, size_t* trailing_padding
)
1737 // Authorities all agree that the values in a .note field should
1738 // be aligned on 4-byte boundaries for 32-bit binaries. However,
1739 // they differ on what the alignment is for 64-bit binaries.
1740 // The GABI says unambiguously they take 8-byte alignment:
1741 // http://sco.com/developers/gabi/latest/ch5.pheader.html#note_section
1742 // Other documentation says alignment should always be 4 bytes:
1743 // http://www.netbsd.org/docs/kernel/elf-notes.html#note-format
1744 // GNU ld and GNU readelf both support the latter (at least as of
1745 // version 2.16.91), and glibc always generates the latter for
1746 // .note.ABI-tag (as of version 1.6), so that's the one we go with
1748 #ifdef GABI_FORMAT_FOR_DOTNOTE_SECTION // This is not defined by default.
1749 const int size
= parameters
->target().get_size();
1751 const int size
= 32;
1754 // The contents of the .note section.
1755 size_t namesz
= strlen(name
) + 1;
1756 size_t aligned_namesz
= align_address(namesz
, size
/ 8);
1757 size_t aligned_descsz
= align_address(descsz
, size
/ 8);
1759 size_t notehdrsz
= 3 * (size
/ 8) + aligned_namesz
;
1761 unsigned char* buffer
= new unsigned char[notehdrsz
];
1762 memset(buffer
, 0, notehdrsz
);
1764 bool is_big_endian
= parameters
->target().is_big_endian();
1770 elfcpp::Swap
<32, false>::writeval(buffer
, namesz
);
1771 elfcpp::Swap
<32, false>::writeval(buffer
+ 4, descsz
);
1772 elfcpp::Swap
<32, false>::writeval(buffer
+ 8, note_type
);
1776 elfcpp::Swap
<32, true>::writeval(buffer
, namesz
);
1777 elfcpp::Swap
<32, true>::writeval(buffer
+ 4, descsz
);
1778 elfcpp::Swap
<32, true>::writeval(buffer
+ 8, note_type
);
1781 else if (size
== 64)
1785 elfcpp::Swap
<64, false>::writeval(buffer
, namesz
);
1786 elfcpp::Swap
<64, false>::writeval(buffer
+ 8, descsz
);
1787 elfcpp::Swap
<64, false>::writeval(buffer
+ 16, note_type
);
1791 elfcpp::Swap
<64, true>::writeval(buffer
, namesz
);
1792 elfcpp::Swap
<64, true>::writeval(buffer
+ 8, descsz
);
1793 elfcpp::Swap
<64, true>::writeval(buffer
+ 16, note_type
);
1799 memcpy(buffer
+ 3 * (size
/ 8), name
, namesz
);
1801 elfcpp::Elf_Xword flags
= 0;
1803 flags
= elfcpp::SHF_ALLOC
;
1804 Output_section
* os
= this->choose_output_section(NULL
, section_name
,
1806 flags
, false, false,
1807 false, false, false, false);
1811 Output_section_data
* posd
= new Output_data_const_buffer(buffer
, notehdrsz
,
1814 os
->add_output_section_data(posd
);
1816 *trailing_padding
= aligned_descsz
- descsz
;
1821 // For an executable or shared library, create a note to record the
1822 // version of gold used to create the binary.
1825 Layout::create_gold_note()
1827 if (parameters
->options().relocatable())
1830 std::string desc
= std::string("gold ") + gold::get_version_string();
1832 size_t trailing_padding
;
1833 Output_section
*os
= this->create_note("GNU", elfcpp::NT_GNU_GOLD_VERSION
,
1834 ".note.gnu.gold-version", desc
.size(),
1835 false, &trailing_padding
);
1839 Output_section_data
* posd
= new Output_data_const(desc
, 4);
1840 os
->add_output_section_data(posd
);
1842 if (trailing_padding
> 0)
1844 posd
= new Output_data_zero_fill(trailing_padding
, 0);
1845 os
->add_output_section_data(posd
);
1849 // Record whether the stack should be executable. This can be set
1850 // from the command line using the -z execstack or -z noexecstack
1851 // options. Otherwise, if any input file has a .note.GNU-stack
1852 // section with the SHF_EXECINSTR flag set, the stack should be
1853 // executable. Otherwise, if at least one input file a
1854 // .note.GNU-stack section, and some input file has no .note.GNU-stack
1855 // section, we use the target default for whether the stack should be
1856 // executable. Otherwise, we don't generate a stack note. When
1857 // generating a object file, we create a .note.GNU-stack section with
1858 // the appropriate marking. When generating an executable or shared
1859 // library, we create a PT_GNU_STACK segment.
1862 Layout::create_executable_stack_info()
1864 bool is_stack_executable
;
1865 if (parameters
->options().is_execstack_set())
1866 is_stack_executable
= parameters
->options().is_stack_executable();
1867 else if (!this->input_with_gnu_stack_note_
)
1871 if (this->input_requires_executable_stack_
)
1872 is_stack_executable
= true;
1873 else if (this->input_without_gnu_stack_note_
)
1874 is_stack_executable
=
1875 parameters
->target().is_default_stack_executable();
1877 is_stack_executable
= false;
1880 if (parameters
->options().relocatable())
1882 const char* name
= this->namepool_
.add(".note.GNU-stack", false, NULL
);
1883 elfcpp::Elf_Xword flags
= 0;
1884 if (is_stack_executable
)
1885 flags
|= elfcpp::SHF_EXECINSTR
;
1886 this->make_output_section(name
, elfcpp::SHT_PROGBITS
, flags
, false,
1887 false, false, false, false);
1891 if (this->script_options_
->saw_phdrs_clause())
1893 int flags
= elfcpp::PF_R
| elfcpp::PF_W
;
1894 if (is_stack_executable
)
1895 flags
|= elfcpp::PF_X
;
1896 this->make_output_segment(elfcpp::PT_GNU_STACK
, flags
);
1900 // If --build-id was used, set up the build ID note.
1903 Layout::create_build_id()
1905 if (!parameters
->options().user_set_build_id())
1908 const char* style
= parameters
->options().build_id();
1909 if (strcmp(style
, "none") == 0)
1912 // Set DESCSZ to the size of the note descriptor. When possible,
1913 // set DESC to the note descriptor contents.
1916 if (strcmp(style
, "md5") == 0)
1918 else if (strcmp(style
, "sha1") == 0)
1920 else if (strcmp(style
, "uuid") == 0)
1922 const size_t uuidsz
= 128 / 8;
1924 char buffer
[uuidsz
];
1925 memset(buffer
, 0, uuidsz
);
1927 int descriptor
= open_descriptor(-1, "/dev/urandom", O_RDONLY
);
1929 gold_error(_("--build-id=uuid failed: could not open /dev/urandom: %s"),
1933 ssize_t got
= ::read(descriptor
, buffer
, uuidsz
);
1934 release_descriptor(descriptor
, true);
1936 gold_error(_("/dev/urandom: read failed: %s"), strerror(errno
));
1937 else if (static_cast<size_t>(got
) != uuidsz
)
1938 gold_error(_("/dev/urandom: expected %zu bytes, got %zd bytes"),
1942 desc
.assign(buffer
, uuidsz
);
1945 else if (strncmp(style
, "0x", 2) == 0)
1948 const char* p
= style
+ 2;
1951 if (hex_p(p
[0]) && hex_p(p
[1]))
1953 char c
= (hex_value(p
[0]) << 4) | hex_value(p
[1]);
1957 else if (*p
== '-' || *p
== ':')
1960 gold_fatal(_("--build-id argument '%s' not a valid hex number"),
1963 descsz
= desc
.size();
1966 gold_fatal(_("unrecognized --build-id argument '%s'"), style
);
1969 size_t trailing_padding
;
1970 Output_section
* os
= this->create_note("GNU", elfcpp::NT_GNU_BUILD_ID
,
1971 ".note.gnu.build-id", descsz
, true,
1978 // We know the value already, so we fill it in now.
1979 gold_assert(desc
.size() == descsz
);
1981 Output_section_data
* posd
= new Output_data_const(desc
, 4);
1982 os
->add_output_section_data(posd
);
1984 if (trailing_padding
!= 0)
1986 posd
= new Output_data_zero_fill(trailing_padding
, 0);
1987 os
->add_output_section_data(posd
);
1992 // We need to compute a checksum after we have completed the
1994 gold_assert(trailing_padding
== 0);
1995 this->build_id_note_
= new Output_data_zero_fill(descsz
, 4);
1996 os
->add_output_section_data(this->build_id_note_
);
2000 // If we have both .stabXX and .stabXXstr sections, then the sh_link
2001 // field of the former should point to the latter. I'm not sure who
2002 // started this, but the GNU linker does it, and some tools depend
2006 Layout::link_stabs_sections()
2008 if (!this->have_stabstr_section_
)
2011 for (Section_list::iterator p
= this->section_list_
.begin();
2012 p
!= this->section_list_
.end();
2015 if ((*p
)->type() != elfcpp::SHT_STRTAB
)
2018 const char* name
= (*p
)->name();
2019 if (strncmp(name
, ".stab", 5) != 0)
2022 size_t len
= strlen(name
);
2023 if (strcmp(name
+ len
- 3, "str") != 0)
2026 std::string
stab_name(name
, len
- 3);
2027 Output_section
* stab_sec
;
2028 stab_sec
= this->find_output_section(stab_name
.c_str());
2029 if (stab_sec
!= NULL
)
2030 stab_sec
->set_link_section(*p
);
2034 // Create .gnu_incremental_inputs and .gnu_incremental_strtab sections needed
2035 // for the next run of incremental linking to check what has changed.
2038 Layout::create_incremental_info_sections()
2040 gold_assert(this->incremental_inputs_
!= NULL
);
2042 // Add the .gnu_incremental_inputs section.
2043 const char *incremental_inputs_name
=
2044 this->namepool_
.add(".gnu_incremental_inputs", false, NULL
);
2045 Output_section
* inputs_os
=
2046 this->make_output_section(incremental_inputs_name
,
2047 elfcpp::SHT_GNU_INCREMENTAL_INPUTS
, 0,
2048 false, false, false, false, false);
2049 Output_section_data
* posd
=
2050 this->incremental_inputs_
->create_incremental_inputs_section_data();
2051 inputs_os
->add_output_section_data(posd
);
2053 // Add the .gnu_incremental_strtab section.
2054 const char *incremental_strtab_name
=
2055 this->namepool_
.add(".gnu_incremental_strtab", false, NULL
);
2056 Output_section
* strtab_os
= this->make_output_section(incremental_strtab_name
,
2059 false, false, false);
2060 Output_data_strtab
* strtab_data
=
2061 new Output_data_strtab(this->incremental_inputs_
->get_stringpool());
2062 strtab_os
->add_output_section_data(strtab_data
);
2064 inputs_os
->set_link_section(strtab_data
);
2067 // Return whether SEG1 should be before SEG2 in the output file. This
2068 // is based entirely on the segment type and flags. When this is
2069 // called the segment addresses has normally not yet been set.
2072 Layout::segment_precedes(const Output_segment
* seg1
,
2073 const Output_segment
* seg2
)
2075 elfcpp::Elf_Word type1
= seg1
->type();
2076 elfcpp::Elf_Word type2
= seg2
->type();
2078 // The single PT_PHDR segment is required to precede any loadable
2079 // segment. We simply make it always first.
2080 if (type1
== elfcpp::PT_PHDR
)
2082 gold_assert(type2
!= elfcpp::PT_PHDR
);
2085 if (type2
== elfcpp::PT_PHDR
)
2088 // The single PT_INTERP segment is required to precede any loadable
2089 // segment. We simply make it always second.
2090 if (type1
== elfcpp::PT_INTERP
)
2092 gold_assert(type2
!= elfcpp::PT_INTERP
);
2095 if (type2
== elfcpp::PT_INTERP
)
2098 // We then put PT_LOAD segments before any other segments.
2099 if (type1
== elfcpp::PT_LOAD
&& type2
!= elfcpp::PT_LOAD
)
2101 if (type2
== elfcpp::PT_LOAD
&& type1
!= elfcpp::PT_LOAD
)
2104 // We put the PT_TLS segment last except for the PT_GNU_RELRO
2105 // segment, because that is where the dynamic linker expects to find
2106 // it (this is just for efficiency; other positions would also work
2108 if (type1
== elfcpp::PT_TLS
2109 && type2
!= elfcpp::PT_TLS
2110 && type2
!= elfcpp::PT_GNU_RELRO
)
2112 if (type2
== elfcpp::PT_TLS
2113 && type1
!= elfcpp::PT_TLS
2114 && type1
!= elfcpp::PT_GNU_RELRO
)
2117 // We put the PT_GNU_RELRO segment last, because that is where the
2118 // dynamic linker expects to find it (as with PT_TLS, this is just
2120 if (type1
== elfcpp::PT_GNU_RELRO
&& type2
!= elfcpp::PT_GNU_RELRO
)
2122 if (type2
== elfcpp::PT_GNU_RELRO
&& type1
!= elfcpp::PT_GNU_RELRO
)
2125 const elfcpp::Elf_Word flags1
= seg1
->flags();
2126 const elfcpp::Elf_Word flags2
= seg2
->flags();
2128 // The order of non-PT_LOAD segments is unimportant. We simply sort
2129 // by the numeric segment type and flags values. There should not
2130 // be more than one segment with the same type and flags.
2131 if (type1
!= elfcpp::PT_LOAD
)
2134 return type1
< type2
;
2135 gold_assert(flags1
!= flags2
);
2136 return flags1
< flags2
;
2139 // If the addresses are set already, sort by load address.
2140 if (seg1
->are_addresses_set())
2142 if (!seg2
->are_addresses_set())
2145 unsigned int section_count1
= seg1
->output_section_count();
2146 unsigned int section_count2
= seg2
->output_section_count();
2147 if (section_count1
== 0 && section_count2
> 0)
2149 if (section_count1
> 0 && section_count2
== 0)
2152 uint64_t paddr1
= seg1
->first_section_load_address();
2153 uint64_t paddr2
= seg2
->first_section_load_address();
2154 if (paddr1
!= paddr2
)
2155 return paddr1
< paddr2
;
2157 else if (seg2
->are_addresses_set())
2160 // A segment which holds large data comes after a segment which does
2161 // not hold large data.
2162 if (seg1
->is_large_data_segment())
2164 if (!seg2
->is_large_data_segment())
2167 else if (seg2
->is_large_data_segment())
2170 // Otherwise, we sort PT_LOAD segments based on the flags. Readonly
2171 // segments come before writable segments. Then writable segments
2172 // with data come before writable segments without data. Then
2173 // executable segments come before non-executable segments. Then
2174 // the unlikely case of a non-readable segment comes before the
2175 // normal case of a readable segment. If there are multiple
2176 // segments with the same type and flags, we require that the
2177 // address be set, and we sort by virtual address and then physical
2179 if ((flags1
& elfcpp::PF_W
) != (flags2
& elfcpp::PF_W
))
2180 return (flags1
& elfcpp::PF_W
) == 0;
2181 if ((flags1
& elfcpp::PF_W
) != 0
2182 && seg1
->has_any_data_sections() != seg2
->has_any_data_sections())
2183 return seg1
->has_any_data_sections();
2184 if ((flags1
& elfcpp::PF_X
) != (flags2
& elfcpp::PF_X
))
2185 return (flags1
& elfcpp::PF_X
) != 0;
2186 if ((flags1
& elfcpp::PF_R
) != (flags2
& elfcpp::PF_R
))
2187 return (flags1
& elfcpp::PF_R
) == 0;
2189 // We shouldn't get here--we shouldn't create segments which we
2190 // can't distinguish.
2194 // Increase OFF so that it is congruent to ADDR modulo ABI_PAGESIZE.
2197 align_file_offset(off_t off
, uint64_t addr
, uint64_t abi_pagesize
)
2199 uint64_t unsigned_off
= off
;
2200 uint64_t aligned_off
= ((unsigned_off
& ~(abi_pagesize
- 1))
2201 | (addr
& (abi_pagesize
- 1)));
2202 if (aligned_off
< unsigned_off
)
2203 aligned_off
+= abi_pagesize
;
2207 // Set the file offsets of all the segments, and all the sections they
2208 // contain. They have all been created. LOAD_SEG must be be laid out
2209 // first. Return the offset of the data to follow.
2212 Layout::set_segment_offsets(const Target
* target
, Output_segment
* load_seg
,
2213 unsigned int *pshndx
)
2215 // Sort them into the final order.
2216 std::sort(this->segment_list_
.begin(), this->segment_list_
.end(),
2217 Layout::Compare_segments());
2219 // Find the PT_LOAD segments, and set their addresses and offsets
2220 // and their section's addresses and offsets.
2222 if (parameters
->options().user_set_Ttext())
2223 addr
= parameters
->options().Ttext();
2224 else if (parameters
->options().output_is_position_independent())
2227 addr
= target
->default_text_segment_address();
2230 // If LOAD_SEG is NULL, then the file header and segment headers
2231 // will not be loadable. But they still need to be at offset 0 in
2232 // the file. Set their offsets now.
2233 if (load_seg
== NULL
)
2235 for (Data_list::iterator p
= this->special_output_list_
.begin();
2236 p
!= this->special_output_list_
.end();
2239 off
= align_address(off
, (*p
)->addralign());
2240 (*p
)->set_address_and_file_offset(0, off
);
2241 off
+= (*p
)->data_size();
2245 unsigned int increase_relro
= this->increase_relro_
;
2246 if (this->script_options_
->saw_sections_clause())
2249 const bool check_sections
= parameters
->options().check_sections();
2250 Output_segment
* last_load_segment
= NULL
;
2252 bool was_readonly
= false;
2253 for (Segment_list::iterator p
= this->segment_list_
.begin();
2254 p
!= this->segment_list_
.end();
2257 if ((*p
)->type() == elfcpp::PT_LOAD
)
2259 if (load_seg
!= NULL
&& load_seg
!= *p
)
2263 bool are_addresses_set
= (*p
)->are_addresses_set();
2264 if (are_addresses_set
)
2266 // When it comes to setting file offsets, we care about
2267 // the physical address.
2268 addr
= (*p
)->paddr();
2270 else if (parameters
->options().user_set_Tdata()
2271 && ((*p
)->flags() & elfcpp::PF_W
) != 0
2272 && (!parameters
->options().user_set_Tbss()
2273 || (*p
)->has_any_data_sections()))
2275 addr
= parameters
->options().Tdata();
2276 are_addresses_set
= true;
2278 else if (parameters
->options().user_set_Tbss()
2279 && ((*p
)->flags() & elfcpp::PF_W
) != 0
2280 && !(*p
)->has_any_data_sections())
2282 addr
= parameters
->options().Tbss();
2283 are_addresses_set
= true;
2286 uint64_t orig_addr
= addr
;
2287 uint64_t orig_off
= off
;
2289 uint64_t aligned_addr
= 0;
2290 uint64_t abi_pagesize
= target
->abi_pagesize();
2291 uint64_t common_pagesize
= target
->common_pagesize();
2293 if (!parameters
->options().nmagic()
2294 && !parameters
->options().omagic())
2295 (*p
)->set_minimum_p_align(common_pagesize
);
2297 if (!are_addresses_set
)
2299 // If the last segment was readonly, and this one is
2300 // not, then skip the address forward one page,
2301 // maintaining the same position within the page. This
2302 // lets us store both segments overlapping on a single
2303 // page in the file, but the loader will put them on
2304 // different pages in memory.
2306 addr
= align_address(addr
, (*p
)->maximum_alignment());
2307 aligned_addr
= addr
;
2309 if (was_readonly
&& ((*p
)->flags() & elfcpp::PF_W
) != 0)
2311 if ((addr
& (abi_pagesize
- 1)) != 0)
2312 addr
= addr
+ abi_pagesize
;
2315 off
= orig_off
+ ((addr
- orig_addr
) & (abi_pagesize
- 1));
2318 if (!parameters
->options().nmagic()
2319 && !parameters
->options().omagic())
2320 off
= align_file_offset(off
, addr
, abi_pagesize
);
2321 else if (load_seg
== NULL
)
2323 // This is -N or -n with a section script which prevents
2324 // us from using a load segment. We need to ensure that
2325 // the file offset is aligned to the alignment of the
2326 // segment. This is because the linker script
2327 // implicitly assumed a zero offset. If we don't align
2328 // here, then the alignment of the sections in the
2329 // linker script may not match the alignment of the
2330 // sections in the set_section_addresses call below,
2331 // causing an error about dot moving backward.
2332 off
= align_address(off
, (*p
)->maximum_alignment());
2335 unsigned int shndx_hold
= *pshndx
;
2336 uint64_t new_addr
= (*p
)->set_section_addresses(this, false, addr
,
2340 // Now that we know the size of this segment, we may be able
2341 // to save a page in memory, at the cost of wasting some
2342 // file space, by instead aligning to the start of a new
2343 // page. Here we use the real machine page size rather than
2344 // the ABI mandated page size.
2346 if (!are_addresses_set
&& aligned_addr
!= addr
)
2348 uint64_t first_off
= (common_pagesize
2350 & (common_pagesize
- 1)));
2351 uint64_t last_off
= new_addr
& (common_pagesize
- 1);
2354 && ((aligned_addr
& ~ (common_pagesize
- 1))
2355 != (new_addr
& ~ (common_pagesize
- 1)))
2356 && first_off
+ last_off
<= common_pagesize
)
2358 *pshndx
= shndx_hold
;
2359 addr
= align_address(aligned_addr
, common_pagesize
);
2360 addr
= align_address(addr
, (*p
)->maximum_alignment());
2361 off
= orig_off
+ ((addr
- orig_addr
) & (abi_pagesize
- 1));
2362 off
= align_file_offset(off
, addr
, abi_pagesize
);
2363 new_addr
= (*p
)->set_section_addresses(this, true, addr
,
2371 if (((*p
)->flags() & elfcpp::PF_W
) == 0)
2372 was_readonly
= true;
2374 // Implement --check-sections. We know that the segments
2375 // are sorted by LMA.
2376 if (check_sections
&& last_load_segment
!= NULL
)
2378 gold_assert(last_load_segment
->paddr() <= (*p
)->paddr());
2379 if (last_load_segment
->paddr() + last_load_segment
->memsz()
2382 unsigned long long lb1
= last_load_segment
->paddr();
2383 unsigned long long le1
= lb1
+ last_load_segment
->memsz();
2384 unsigned long long lb2
= (*p
)->paddr();
2385 unsigned long long le2
= lb2
+ (*p
)->memsz();
2386 gold_error(_("load segment overlap [0x%llx -> 0x%llx] and "
2387 "[0x%llx -> 0x%llx]"),
2388 lb1
, le1
, lb2
, le2
);
2391 last_load_segment
= *p
;
2395 // Handle the non-PT_LOAD segments, setting their offsets from their
2396 // section's offsets.
2397 for (Segment_list::iterator p
= this->segment_list_
.begin();
2398 p
!= this->segment_list_
.end();
2401 if ((*p
)->type() != elfcpp::PT_LOAD
)
2402 (*p
)->set_offset((*p
)->type() == elfcpp::PT_GNU_RELRO
2407 // Set the TLS offsets for each section in the PT_TLS segment.
2408 if (this->tls_segment_
!= NULL
)
2409 this->tls_segment_
->set_tls_offsets();
2414 // Set the offsets of all the allocated sections when doing a
2415 // relocatable link. This does the same jobs as set_segment_offsets,
2416 // only for a relocatable link.
2419 Layout::set_relocatable_section_offsets(Output_data
* file_header
,
2420 unsigned int *pshndx
)
2424 file_header
->set_address_and_file_offset(0, 0);
2425 off
+= file_header
->data_size();
2427 for (Section_list::iterator p
= this->section_list_
.begin();
2428 p
!= this->section_list_
.end();
2431 // We skip unallocated sections here, except that group sections
2432 // have to come first.
2433 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) == 0
2434 && (*p
)->type() != elfcpp::SHT_GROUP
)
2437 off
= align_address(off
, (*p
)->addralign());
2439 // The linker script might have set the address.
2440 if (!(*p
)->is_address_valid())
2441 (*p
)->set_address(0);
2442 (*p
)->set_file_offset(off
);
2443 (*p
)->finalize_data_size();
2444 off
+= (*p
)->data_size();
2446 (*p
)->set_out_shndx(*pshndx
);
2453 // Set the file offset of all the sections not associated with a
2457 Layout::set_section_offsets(off_t off
, Layout::Section_offset_pass pass
)
2459 for (Section_list::iterator p
= this->unattached_section_list_
.begin();
2460 p
!= this->unattached_section_list_
.end();
2463 // The symtab section is handled in create_symtab_sections.
2464 if (*p
== this->symtab_section_
)
2467 // If we've already set the data size, don't set it again.
2468 if ((*p
)->is_offset_valid() && (*p
)->is_data_size_valid())
2471 if (pass
== BEFORE_INPUT_SECTIONS_PASS
2472 && (*p
)->requires_postprocessing())
2474 (*p
)->create_postprocessing_buffer();
2475 this->any_postprocessing_sections_
= true;
2478 if (pass
== BEFORE_INPUT_SECTIONS_PASS
2479 && (*p
)->after_input_sections())
2481 else if (pass
== POSTPROCESSING_SECTIONS_PASS
2482 && (!(*p
)->after_input_sections()
2483 || (*p
)->type() == elfcpp::SHT_STRTAB
))
2485 else if (pass
== STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
2486 && (!(*p
)->after_input_sections()
2487 || (*p
)->type() != elfcpp::SHT_STRTAB
))
2490 off
= align_address(off
, (*p
)->addralign());
2491 (*p
)->set_file_offset(off
);
2492 (*p
)->finalize_data_size();
2493 off
+= (*p
)->data_size();
2495 // At this point the name must be set.
2496 if (pass
!= STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
)
2497 this->namepool_
.add((*p
)->name(), false, NULL
);
2502 // Set the section indexes of all the sections not associated with a
2506 Layout::set_section_indexes(unsigned int shndx
)
2508 for (Section_list::iterator p
= this->unattached_section_list_
.begin();
2509 p
!= this->unattached_section_list_
.end();
2512 if (!(*p
)->has_out_shndx())
2514 (*p
)->set_out_shndx(shndx
);
2521 // Set the section addresses according to the linker script. This is
2522 // only called when we see a SECTIONS clause. This returns the
2523 // program segment which should hold the file header and segment
2524 // headers, if any. It will return NULL if they should not be in a
2528 Layout::set_section_addresses_from_script(Symbol_table
* symtab
)
2530 Script_sections
* ss
= this->script_options_
->script_sections();
2531 gold_assert(ss
->saw_sections_clause());
2532 return this->script_options_
->set_section_addresses(symtab
, this);
2535 // Place the orphan sections in the linker script.
2538 Layout::place_orphan_sections_in_script()
2540 Script_sections
* ss
= this->script_options_
->script_sections();
2541 gold_assert(ss
->saw_sections_clause());
2543 // Place each orphaned output section in the script.
2544 for (Section_list::iterator p
= this->section_list_
.begin();
2545 p
!= this->section_list_
.end();
2548 if (!(*p
)->found_in_sections_clause())
2549 ss
->place_orphan(*p
);
2553 // Count the local symbols in the regular symbol table and the dynamic
2554 // symbol table, and build the respective string pools.
2557 Layout::count_local_symbols(const Task
* task
,
2558 const Input_objects
* input_objects
)
2560 // First, figure out an upper bound on the number of symbols we'll
2561 // be inserting into each pool. This helps us create the pools with
2562 // the right size, to avoid unnecessary hashtable resizing.
2563 unsigned int symbol_count
= 0;
2564 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2565 p
!= input_objects
->relobj_end();
2567 symbol_count
+= (*p
)->local_symbol_count();
2569 // Go from "upper bound" to "estimate." We overcount for two
2570 // reasons: we double-count symbols that occur in more than one
2571 // object file, and we count symbols that are dropped from the
2572 // output. Add it all together and assume we overcount by 100%.
2575 // We assume all symbols will go into both the sympool and dynpool.
2576 this->sympool_
.reserve(symbol_count
);
2577 this->dynpool_
.reserve(symbol_count
);
2579 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2580 p
!= input_objects
->relobj_end();
2583 Task_lock_obj
<Object
> tlo(task
, *p
);
2584 (*p
)->count_local_symbols(&this->sympool_
, &this->dynpool_
);
2588 // Create the symbol table sections. Here we also set the final
2589 // values of the symbols. At this point all the loadable sections are
2590 // fully laid out. SHNUM is the number of sections so far.
2593 Layout::create_symtab_sections(const Input_objects
* input_objects
,
2594 Symbol_table
* symtab
,
2600 if (parameters
->target().get_size() == 32)
2602 symsize
= elfcpp::Elf_sizes
<32>::sym_size
;
2605 else if (parameters
->target().get_size() == 64)
2607 symsize
= elfcpp::Elf_sizes
<64>::sym_size
;
2614 off
= align_address(off
, align
);
2615 off_t startoff
= off
;
2617 // Save space for the dummy symbol at the start of the section. We
2618 // never bother to write this out--it will just be left as zero.
2620 unsigned int local_symbol_index
= 1;
2622 // Add STT_SECTION symbols for each Output section which needs one.
2623 for (Section_list::iterator p
= this->section_list_
.begin();
2624 p
!= this->section_list_
.end();
2627 if (!(*p
)->needs_symtab_index())
2628 (*p
)->set_symtab_index(-1U);
2631 (*p
)->set_symtab_index(local_symbol_index
);
2632 ++local_symbol_index
;
2637 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2638 p
!= input_objects
->relobj_end();
2641 unsigned int index
= (*p
)->finalize_local_symbols(local_symbol_index
,
2643 off
+= (index
- local_symbol_index
) * symsize
;
2644 local_symbol_index
= index
;
2647 unsigned int local_symcount
= local_symbol_index
;
2648 gold_assert(static_cast<off_t
>(local_symcount
* symsize
) == off
- startoff
);
2651 size_t dyn_global_index
;
2653 if (this->dynsym_section_
== NULL
)
2656 dyn_global_index
= 0;
2661 dyn_global_index
= this->dynsym_section_
->info();
2662 off_t locsize
= dyn_global_index
* this->dynsym_section_
->entsize();
2663 dynoff
= this->dynsym_section_
->offset() + locsize
;
2664 dyncount
= (this->dynsym_section_
->data_size() - locsize
) / symsize
;
2665 gold_assert(static_cast<off_t
>(dyncount
* symsize
)
2666 == this->dynsym_section_
->data_size() - locsize
);
2669 off
= symtab
->finalize(off
, dynoff
, dyn_global_index
, dyncount
,
2670 &this->sympool_
, &local_symcount
);
2672 if (!parameters
->options().strip_all())
2674 this->sympool_
.set_string_offsets();
2676 const char* symtab_name
= this->namepool_
.add(".symtab", false, NULL
);
2677 Output_section
* osymtab
= this->make_output_section(symtab_name
,
2680 false, false, false);
2681 this->symtab_section_
= osymtab
;
2683 Output_section_data
* pos
= new Output_data_fixed_space(off
- startoff
,
2686 osymtab
->add_output_section_data(pos
);
2688 // We generate a .symtab_shndx section if we have more than
2689 // SHN_LORESERVE sections. Technically it is possible that we
2690 // don't need one, because it is possible that there are no
2691 // symbols in any of sections with indexes larger than
2692 // SHN_LORESERVE. That is probably unusual, though, and it is
2693 // easier to always create one than to compute section indexes
2694 // twice (once here, once when writing out the symbols).
2695 if (shnum
>= elfcpp::SHN_LORESERVE
)
2697 const char* symtab_xindex_name
= this->namepool_
.add(".symtab_shndx",
2699 Output_section
* osymtab_xindex
=
2700 this->make_output_section(symtab_xindex_name
,
2701 elfcpp::SHT_SYMTAB_SHNDX
, 0, false,
2702 false, false, false, false);
2704 size_t symcount
= (off
- startoff
) / symsize
;
2705 this->symtab_xindex_
= new Output_symtab_xindex(symcount
);
2707 osymtab_xindex
->add_output_section_data(this->symtab_xindex_
);
2709 osymtab_xindex
->set_link_section(osymtab
);
2710 osymtab_xindex
->set_addralign(4);
2711 osymtab_xindex
->set_entsize(4);
2713 osymtab_xindex
->set_after_input_sections();
2715 // This tells the driver code to wait until the symbol table
2716 // has written out before writing out the postprocessing
2717 // sections, including the .symtab_shndx section.
2718 this->any_postprocessing_sections_
= true;
2721 const char* strtab_name
= this->namepool_
.add(".strtab", false, NULL
);
2722 Output_section
* ostrtab
= this->make_output_section(strtab_name
,
2725 false, false, false);
2727 Output_section_data
* pstr
= new Output_data_strtab(&this->sympool_
);
2728 ostrtab
->add_output_section_data(pstr
);
2730 osymtab
->set_file_offset(startoff
);
2731 osymtab
->finalize_data_size();
2732 osymtab
->set_link_section(ostrtab
);
2733 osymtab
->set_info(local_symcount
);
2734 osymtab
->set_entsize(symsize
);
2740 // Create the .shstrtab section, which holds the names of the
2741 // sections. At the time this is called, we have created all the
2742 // output sections except .shstrtab itself.
2745 Layout::create_shstrtab()
2747 // FIXME: We don't need to create a .shstrtab section if we are
2748 // stripping everything.
2750 const char* name
= this->namepool_
.add(".shstrtab", false, NULL
);
2752 Output_section
* os
= this->make_output_section(name
, elfcpp::SHT_STRTAB
, 0,
2753 false, false, false, false,
2756 if (strcmp(parameters
->options().compress_debug_sections(), "none") != 0)
2758 // We can't write out this section until we've set all the
2759 // section names, and we don't set the names of compressed
2760 // output sections until relocations are complete. FIXME: With
2761 // the current names we use, this is unnecessary.
2762 os
->set_after_input_sections();
2765 Output_section_data
* posd
= new Output_data_strtab(&this->namepool_
);
2766 os
->add_output_section_data(posd
);
2771 // Create the section headers. SIZE is 32 or 64. OFF is the file
2775 Layout::create_shdrs(const Output_section
* shstrtab_section
, off_t
* poff
)
2777 Output_section_headers
* oshdrs
;
2778 oshdrs
= new Output_section_headers(this,
2779 &this->segment_list_
,
2780 &this->section_list_
,
2781 &this->unattached_section_list_
,
2784 off_t off
= align_address(*poff
, oshdrs
->addralign());
2785 oshdrs
->set_address_and_file_offset(0, off
);
2786 off
+= oshdrs
->data_size();
2788 this->section_headers_
= oshdrs
;
2791 // Count the allocated sections.
2794 Layout::allocated_output_section_count() const
2796 size_t section_count
= 0;
2797 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
2798 p
!= this->segment_list_
.end();
2800 section_count
+= (*p
)->output_section_count();
2801 return section_count
;
2804 // Create the dynamic symbol table.
2807 Layout::create_dynamic_symtab(const Input_objects
* input_objects
,
2808 Symbol_table
* symtab
,
2809 Output_section
**pdynstr
,
2810 unsigned int* plocal_dynamic_count
,
2811 std::vector
<Symbol
*>* pdynamic_symbols
,
2812 Versions
* pversions
)
2814 // Count all the symbols in the dynamic symbol table, and set the
2815 // dynamic symbol indexes.
2817 // Skip symbol 0, which is always all zeroes.
2818 unsigned int index
= 1;
2820 // Add STT_SECTION symbols for each Output section which needs one.
2821 for (Section_list::iterator p
= this->section_list_
.begin();
2822 p
!= this->section_list_
.end();
2825 if (!(*p
)->needs_dynsym_index())
2826 (*p
)->set_dynsym_index(-1U);
2829 (*p
)->set_dynsym_index(index
);
2834 // Count the local symbols that need to go in the dynamic symbol table,
2835 // and set the dynamic symbol indexes.
2836 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2837 p
!= input_objects
->relobj_end();
2840 unsigned int new_index
= (*p
)->set_local_dynsym_indexes(index
);
2844 unsigned int local_symcount
= index
;
2845 *plocal_dynamic_count
= local_symcount
;
2847 index
= symtab
->set_dynsym_indexes(index
, pdynamic_symbols
,
2848 &this->dynpool_
, pversions
);
2852 const int size
= parameters
->target().get_size();
2855 symsize
= elfcpp::Elf_sizes
<32>::sym_size
;
2858 else if (size
== 64)
2860 symsize
= elfcpp::Elf_sizes
<64>::sym_size
;
2866 // Create the dynamic symbol table section.
2868 Output_section
* dynsym
= this->choose_output_section(NULL
, ".dynsym",
2872 false, false, false);
2874 Output_section_data
* odata
= new Output_data_fixed_space(index
* symsize
,
2877 dynsym
->add_output_section_data(odata
);
2879 dynsym
->set_info(local_symcount
);
2880 dynsym
->set_entsize(symsize
);
2881 dynsym
->set_addralign(align
);
2883 this->dynsym_section_
= dynsym
;
2885 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
2886 odyn
->add_section_address(elfcpp::DT_SYMTAB
, dynsym
);
2887 odyn
->add_constant(elfcpp::DT_SYMENT
, symsize
);
2889 // If there are more than SHN_LORESERVE allocated sections, we
2890 // create a .dynsym_shndx section. It is possible that we don't
2891 // need one, because it is possible that there are no dynamic
2892 // symbols in any of the sections with indexes larger than
2893 // SHN_LORESERVE. This is probably unusual, though, and at this
2894 // time we don't know the actual section indexes so it is
2895 // inconvenient to check.
2896 if (this->allocated_output_section_count() >= elfcpp::SHN_LORESERVE
)
2898 Output_section
* dynsym_xindex
=
2899 this->choose_output_section(NULL
, ".dynsym_shndx",
2900 elfcpp::SHT_SYMTAB_SHNDX
,
2902 false, false, true, false, false, false);
2904 this->dynsym_xindex_
= new Output_symtab_xindex(index
);
2906 dynsym_xindex
->add_output_section_data(this->dynsym_xindex_
);
2908 dynsym_xindex
->set_link_section(dynsym
);
2909 dynsym_xindex
->set_addralign(4);
2910 dynsym_xindex
->set_entsize(4);
2912 dynsym_xindex
->set_after_input_sections();
2914 // This tells the driver code to wait until the symbol table has
2915 // written out before writing out the postprocessing sections,
2916 // including the .dynsym_shndx section.
2917 this->any_postprocessing_sections_
= true;
2920 // Create the dynamic string table section.
2922 Output_section
* dynstr
= this->choose_output_section(NULL
, ".dynstr",
2926 false, false, false);
2928 Output_section_data
* strdata
= new Output_data_strtab(&this->dynpool_
);
2929 dynstr
->add_output_section_data(strdata
);
2931 dynsym
->set_link_section(dynstr
);
2932 this->dynamic_section_
->set_link_section(dynstr
);
2934 odyn
->add_section_address(elfcpp::DT_STRTAB
, dynstr
);
2935 odyn
->add_section_size(elfcpp::DT_STRSZ
, dynstr
);
2939 // Create the hash tables.
2941 if (strcmp(parameters
->options().hash_style(), "sysv") == 0
2942 || strcmp(parameters
->options().hash_style(), "both") == 0)
2944 unsigned char* phash
;
2945 unsigned int hashlen
;
2946 Dynobj::create_elf_hash_table(*pdynamic_symbols
, local_symcount
,
2949 Output_section
* hashsec
= this->choose_output_section(NULL
, ".hash",
2956 Output_section_data
* hashdata
= new Output_data_const_buffer(phash
,
2960 hashsec
->add_output_section_data(hashdata
);
2962 hashsec
->set_link_section(dynsym
);
2963 hashsec
->set_entsize(4);
2965 odyn
->add_section_address(elfcpp::DT_HASH
, hashsec
);
2968 if (strcmp(parameters
->options().hash_style(), "gnu") == 0
2969 || strcmp(parameters
->options().hash_style(), "both") == 0)
2971 unsigned char* phash
;
2972 unsigned int hashlen
;
2973 Dynobj::create_gnu_hash_table(*pdynamic_symbols
, local_symcount
,
2976 Output_section
* hashsec
= this->choose_output_section(NULL
, ".gnu.hash",
2977 elfcpp::SHT_GNU_HASH
,
2983 Output_section_data
* hashdata
= new Output_data_const_buffer(phash
,
2987 hashsec
->add_output_section_data(hashdata
);
2989 hashsec
->set_link_section(dynsym
);
2991 // For a 64-bit target, the entries in .gnu.hash do not have a
2992 // uniform size, so we only set the entry size for a 32-bit
2994 if (parameters
->target().get_size() == 32)
2995 hashsec
->set_entsize(4);
2997 odyn
->add_section_address(elfcpp::DT_GNU_HASH
, hashsec
);
3001 // Assign offsets to each local portion of the dynamic symbol table.
3004 Layout::assign_local_dynsym_offsets(const Input_objects
* input_objects
)
3006 Output_section
* dynsym
= this->dynsym_section_
;
3007 gold_assert(dynsym
!= NULL
);
3009 off_t off
= dynsym
->offset();
3011 // Skip the dummy symbol at the start of the section.
3012 off
+= dynsym
->entsize();
3014 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
3015 p
!= input_objects
->relobj_end();
3018 unsigned int count
= (*p
)->set_local_dynsym_offset(off
);
3019 off
+= count
* dynsym
->entsize();
3023 // Create the version sections.
3026 Layout::create_version_sections(const Versions
* versions
,
3027 const Symbol_table
* symtab
,
3028 unsigned int local_symcount
,
3029 const std::vector
<Symbol
*>& dynamic_symbols
,
3030 const Output_section
* dynstr
)
3032 if (!versions
->any_defs() && !versions
->any_needs())
3035 switch (parameters
->size_and_endianness())
3037 #ifdef HAVE_TARGET_32_LITTLE
3038 case Parameters::TARGET_32_LITTLE
:
3039 this->sized_create_version_sections
<32, false>(versions
, symtab
,
3041 dynamic_symbols
, dynstr
);
3044 #ifdef HAVE_TARGET_32_BIG
3045 case Parameters::TARGET_32_BIG
:
3046 this->sized_create_version_sections
<32, true>(versions
, symtab
,
3048 dynamic_symbols
, dynstr
);
3051 #ifdef HAVE_TARGET_64_LITTLE
3052 case Parameters::TARGET_64_LITTLE
:
3053 this->sized_create_version_sections
<64, false>(versions
, symtab
,
3055 dynamic_symbols
, dynstr
);
3058 #ifdef HAVE_TARGET_64_BIG
3059 case Parameters::TARGET_64_BIG
:
3060 this->sized_create_version_sections
<64, true>(versions
, symtab
,
3062 dynamic_symbols
, dynstr
);
3070 // Create the version sections, sized version.
3072 template<int size
, bool big_endian
>
3074 Layout::sized_create_version_sections(
3075 const Versions
* versions
,
3076 const Symbol_table
* symtab
,
3077 unsigned int local_symcount
,
3078 const std::vector
<Symbol
*>& dynamic_symbols
,
3079 const Output_section
* dynstr
)
3081 Output_section
* vsec
= this->choose_output_section(NULL
, ".gnu.version",
3082 elfcpp::SHT_GNU_versym
,
3085 false, false, false);
3087 unsigned char* vbuf
;
3089 versions
->symbol_section_contents
<size
, big_endian
>(symtab
, &this->dynpool_
,
3094 Output_section_data
* vdata
= new Output_data_const_buffer(vbuf
, vsize
, 2,
3097 vsec
->add_output_section_data(vdata
);
3098 vsec
->set_entsize(2);
3099 vsec
->set_link_section(this->dynsym_section_
);
3101 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
3102 odyn
->add_section_address(elfcpp::DT_VERSYM
, vsec
);
3104 if (versions
->any_defs())
3106 Output_section
* vdsec
;
3107 vdsec
= this->choose_output_section(NULL
, ".gnu.version_d",
3108 elfcpp::SHT_GNU_verdef
,
3110 false, false, true, false, false,
3113 unsigned char* vdbuf
;
3114 unsigned int vdsize
;
3115 unsigned int vdentries
;
3116 versions
->def_section_contents
<size
, big_endian
>(&this->dynpool_
, &vdbuf
,
3117 &vdsize
, &vdentries
);
3119 Output_section_data
* vddata
=
3120 new Output_data_const_buffer(vdbuf
, vdsize
, 4, "** version defs");
3122 vdsec
->add_output_section_data(vddata
);
3123 vdsec
->set_link_section(dynstr
);
3124 vdsec
->set_info(vdentries
);
3126 odyn
->add_section_address(elfcpp::DT_VERDEF
, vdsec
);
3127 odyn
->add_constant(elfcpp::DT_VERDEFNUM
, vdentries
);
3130 if (versions
->any_needs())
3132 Output_section
* vnsec
;
3133 vnsec
= this->choose_output_section(NULL
, ".gnu.version_r",
3134 elfcpp::SHT_GNU_verneed
,
3136 false, false, true, false, false,
3139 unsigned char* vnbuf
;
3140 unsigned int vnsize
;
3141 unsigned int vnentries
;
3142 versions
->need_section_contents
<size
, big_endian
>(&this->dynpool_
,
3146 Output_section_data
* vndata
=
3147 new Output_data_const_buffer(vnbuf
, vnsize
, 4, "** version refs");
3149 vnsec
->add_output_section_data(vndata
);
3150 vnsec
->set_link_section(dynstr
);
3151 vnsec
->set_info(vnentries
);
3153 odyn
->add_section_address(elfcpp::DT_VERNEED
, vnsec
);
3154 odyn
->add_constant(elfcpp::DT_VERNEEDNUM
, vnentries
);
3158 // Create the .interp section and PT_INTERP segment.
3161 Layout::create_interp(const Target
* target
)
3163 const char* interp
= parameters
->options().dynamic_linker();
3166 interp
= target
->dynamic_linker();
3167 gold_assert(interp
!= NULL
);
3170 size_t len
= strlen(interp
) + 1;
3172 Output_section_data
* odata
= new Output_data_const(interp
, len
, 1);
3174 Output_section
* osec
= this->choose_output_section(NULL
, ".interp",
3175 elfcpp::SHT_PROGBITS
,
3178 false, false, false);
3179 osec
->add_output_section_data(odata
);
3181 if (!this->script_options_
->saw_phdrs_clause())
3183 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_INTERP
,
3185 oseg
->add_output_section(osec
, elfcpp::PF_R
, false);
3189 // Finish the .dynamic section and PT_DYNAMIC segment.
3192 Layout::finish_dynamic_section(const Input_objects
* input_objects
,
3193 const Symbol_table
* symtab
)
3195 if (!this->script_options_
->saw_phdrs_clause())
3197 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_DYNAMIC
,
3200 oseg
->add_output_section(this->dynamic_section_
,
3201 elfcpp::PF_R
| elfcpp::PF_W
,
3205 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
3207 for (Input_objects::Dynobj_iterator p
= input_objects
->dynobj_begin();
3208 p
!= input_objects
->dynobj_end();
3211 if (!(*p
)->is_needed()
3212 && (*p
)->input_file()->options().as_needed())
3214 // This dynamic object was linked with --as-needed, but it
3219 odyn
->add_string(elfcpp::DT_NEEDED
, (*p
)->soname());
3222 if (parameters
->options().shared())
3224 const char* soname
= parameters
->options().soname();
3226 odyn
->add_string(elfcpp::DT_SONAME
, soname
);
3229 Symbol
* sym
= symtab
->lookup(parameters
->options().init());
3230 if (sym
!= NULL
&& sym
->is_defined() && !sym
->is_from_dynobj())
3231 odyn
->add_symbol(elfcpp::DT_INIT
, sym
);
3233 sym
= symtab
->lookup(parameters
->options().fini());
3234 if (sym
!= NULL
&& sym
->is_defined() && !sym
->is_from_dynobj())
3235 odyn
->add_symbol(elfcpp::DT_FINI
, sym
);
3237 // Look for .init_array, .preinit_array and .fini_array by checking
3239 for(Layout::Section_list::const_iterator p
= this->section_list_
.begin();
3240 p
!= this->section_list_
.end();
3242 switch((*p
)->type())
3244 case elfcpp::SHT_FINI_ARRAY
:
3245 odyn
->add_section_address(elfcpp::DT_FINI_ARRAY
, *p
);
3246 odyn
->add_section_size(elfcpp::DT_FINI_ARRAYSZ
, *p
);
3248 case elfcpp::SHT_INIT_ARRAY
:
3249 odyn
->add_section_address(elfcpp::DT_INIT_ARRAY
, *p
);
3250 odyn
->add_section_size(elfcpp::DT_INIT_ARRAYSZ
, *p
);
3252 case elfcpp::SHT_PREINIT_ARRAY
:
3253 odyn
->add_section_address(elfcpp::DT_PREINIT_ARRAY
, *p
);
3254 odyn
->add_section_size(elfcpp::DT_PREINIT_ARRAYSZ
, *p
);
3260 // Add a DT_RPATH entry if needed.
3261 const General_options::Dir_list
& rpath(parameters
->options().rpath());
3264 std::string rpath_val
;
3265 for (General_options::Dir_list::const_iterator p
= rpath
.begin();
3269 if (rpath_val
.empty())
3270 rpath_val
= p
->name();
3273 // Eliminate duplicates.
3274 General_options::Dir_list::const_iterator q
;
3275 for (q
= rpath
.begin(); q
!= p
; ++q
)
3276 if (q
->name() == p
->name())
3281 rpath_val
+= p
->name();
3286 odyn
->add_string(elfcpp::DT_RPATH
, rpath_val
);
3287 if (parameters
->options().enable_new_dtags())
3288 odyn
->add_string(elfcpp::DT_RUNPATH
, rpath_val
);
3291 // Look for text segments that have dynamic relocations.
3292 bool have_textrel
= false;
3293 if (!this->script_options_
->saw_sections_clause())
3295 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
3296 p
!= this->segment_list_
.end();
3299 if (((*p
)->flags() & elfcpp::PF_W
) == 0
3300 && (*p
)->dynamic_reloc_count() > 0)
3302 have_textrel
= true;
3309 // We don't know the section -> segment mapping, so we are
3310 // conservative and just look for readonly sections with
3311 // relocations. If those sections wind up in writable segments,
3312 // then we have created an unnecessary DT_TEXTREL entry.
3313 for (Section_list::const_iterator p
= this->section_list_
.begin();
3314 p
!= this->section_list_
.end();
3317 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) != 0
3318 && ((*p
)->flags() & elfcpp::SHF_WRITE
) == 0
3319 && ((*p
)->dynamic_reloc_count() > 0))
3321 have_textrel
= true;
3327 // Add a DT_FLAGS entry. We add it even if no flags are set so that
3328 // post-link tools can easily modify these flags if desired.
3329 unsigned int flags
= 0;
3332 // Add a DT_TEXTREL for compatibility with older loaders.
3333 odyn
->add_constant(elfcpp::DT_TEXTREL
, 0);
3334 flags
|= elfcpp::DF_TEXTREL
;
3336 if (parameters
->options().shared() && this->has_static_tls())
3337 flags
|= elfcpp::DF_STATIC_TLS
;
3338 if (parameters
->options().origin())
3339 flags
|= elfcpp::DF_ORIGIN
;
3340 if (parameters
->options().Bsymbolic())
3342 flags
|= elfcpp::DF_SYMBOLIC
;
3343 // Add DT_SYMBOLIC for compatibility with older loaders.
3344 odyn
->add_constant(elfcpp::DT_SYMBOLIC
, 0);
3346 if (parameters
->options().now())
3347 flags
|= elfcpp::DF_BIND_NOW
;
3348 odyn
->add_constant(elfcpp::DT_FLAGS
, flags
);
3351 if (parameters
->options().initfirst())
3352 flags
|= elfcpp::DF_1_INITFIRST
;
3353 if (parameters
->options().interpose())
3354 flags
|= elfcpp::DF_1_INTERPOSE
;
3355 if (parameters
->options().loadfltr())
3356 flags
|= elfcpp::DF_1_LOADFLTR
;
3357 if (parameters
->options().nodefaultlib())
3358 flags
|= elfcpp::DF_1_NODEFLIB
;
3359 if (parameters
->options().nodelete())
3360 flags
|= elfcpp::DF_1_NODELETE
;
3361 if (parameters
->options().nodlopen())
3362 flags
|= elfcpp::DF_1_NOOPEN
;
3363 if (parameters
->options().nodump())
3364 flags
|= elfcpp::DF_1_NODUMP
;
3365 if (!parameters
->options().shared())
3366 flags
&= ~(elfcpp::DF_1_INITFIRST
3367 | elfcpp::DF_1_NODELETE
3368 | elfcpp::DF_1_NOOPEN
);
3369 if (parameters
->options().origin())
3370 flags
|= elfcpp::DF_1_ORIGIN
;
3371 if (parameters
->options().now())
3372 flags
|= elfcpp::DF_1_NOW
;
3374 odyn
->add_constant(elfcpp::DT_FLAGS_1
, flags
);
3377 // Set the size of the _DYNAMIC symbol table to be the size of the
3381 Layout::set_dynamic_symbol_size(const Symbol_table
* symtab
)
3383 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
3384 odyn
->finalize_data_size();
3385 off_t data_size
= odyn
->data_size();
3386 const int size
= parameters
->target().get_size();
3388 symtab
->get_sized_symbol
<32>(this->dynamic_symbol_
)->set_symsize(data_size
);
3389 else if (size
== 64)
3390 symtab
->get_sized_symbol
<64>(this->dynamic_symbol_
)->set_symsize(data_size
);
3395 // The mapping of input section name prefixes to output section names.
3396 // In some cases one prefix is itself a prefix of another prefix; in
3397 // such a case the longer prefix must come first. These prefixes are
3398 // based on the GNU linker default ELF linker script.
3400 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
3401 const Layout::Section_name_mapping
Layout::section_name_mapping
[] =
3403 MAPPING_INIT(".text.", ".text"),
3404 MAPPING_INIT(".ctors.", ".ctors"),
3405 MAPPING_INIT(".dtors.", ".dtors"),
3406 MAPPING_INIT(".rodata.", ".rodata"),
3407 MAPPING_INIT(".data.rel.ro.local", ".data.rel.ro.local"),
3408 MAPPING_INIT(".data.rel.ro", ".data.rel.ro"),
3409 MAPPING_INIT(".data.", ".data"),
3410 MAPPING_INIT(".bss.", ".bss"),
3411 MAPPING_INIT(".tdata.", ".tdata"),
3412 MAPPING_INIT(".tbss.", ".tbss"),
3413 MAPPING_INIT(".init_array.", ".init_array"),
3414 MAPPING_INIT(".fini_array.", ".fini_array"),
3415 MAPPING_INIT(".sdata.", ".sdata"),
3416 MAPPING_INIT(".sbss.", ".sbss"),
3417 // FIXME: In the GNU linker, .sbss2 and .sdata2 are handled
3418 // differently depending on whether it is creating a shared library.
3419 MAPPING_INIT(".sdata2.", ".sdata"),
3420 MAPPING_INIT(".sbss2.", ".sbss"),
3421 MAPPING_INIT(".lrodata.", ".lrodata"),
3422 MAPPING_INIT(".ldata.", ".ldata"),
3423 MAPPING_INIT(".lbss.", ".lbss"),
3424 MAPPING_INIT(".gcc_except_table.", ".gcc_except_table"),
3425 MAPPING_INIT(".gnu.linkonce.d.rel.ro.local.", ".data.rel.ro.local"),
3426 MAPPING_INIT(".gnu.linkonce.d.rel.ro.", ".data.rel.ro"),
3427 MAPPING_INIT(".gnu.linkonce.t.", ".text"),
3428 MAPPING_INIT(".gnu.linkonce.r.", ".rodata"),
3429 MAPPING_INIT(".gnu.linkonce.d.", ".data"),
3430 MAPPING_INIT(".gnu.linkonce.b.", ".bss"),
3431 MAPPING_INIT(".gnu.linkonce.s.", ".sdata"),
3432 MAPPING_INIT(".gnu.linkonce.sb.", ".sbss"),
3433 MAPPING_INIT(".gnu.linkonce.s2.", ".sdata"),
3434 MAPPING_INIT(".gnu.linkonce.sb2.", ".sbss"),
3435 MAPPING_INIT(".gnu.linkonce.wi.", ".debug_info"),
3436 MAPPING_INIT(".gnu.linkonce.td.", ".tdata"),
3437 MAPPING_INIT(".gnu.linkonce.tb.", ".tbss"),
3438 MAPPING_INIT(".gnu.linkonce.lr.", ".lrodata"),
3439 MAPPING_INIT(".gnu.linkonce.l.", ".ldata"),
3440 MAPPING_INIT(".gnu.linkonce.lb.", ".lbss"),
3441 MAPPING_INIT(".ARM.extab.", ".ARM.extab"),
3442 MAPPING_INIT(".gnu.linkonce.armextab.", ".ARM.extab"),
3443 MAPPING_INIT(".ARM.exidx.", ".ARM.exidx"),
3444 MAPPING_INIT(".gnu.linkonce.armexidx.", ".ARM.exidx"),
3448 const int Layout::section_name_mapping_count
=
3449 (sizeof(Layout::section_name_mapping
)
3450 / sizeof(Layout::section_name_mapping
[0]));
3452 // Choose the output section name to use given an input section name.
3453 // Set *PLEN to the length of the name. *PLEN is initialized to the
3457 Layout::output_section_name(const char* name
, size_t* plen
)
3459 // gcc 4.3 generates the following sorts of section names when it
3460 // needs a section name specific to a function:
3466 // .data.rel.local.FN
3468 // .data.rel.ro.local.FN
3475 // The GNU linker maps all of those to the part before the .FN,
3476 // except that .data.rel.local.FN is mapped to .data, and
3477 // .data.rel.ro.local.FN is mapped to .data.rel.ro. The sections
3478 // beginning with .data.rel.ro.local are grouped together.
3480 // For an anonymous namespace, the string FN can contain a '.'.
3482 // Also of interest: .rodata.strN.N, .rodata.cstN, both of which the
3483 // GNU linker maps to .rodata.
3485 // The .data.rel.ro sections are used with -z relro. The sections
3486 // are recognized by name. We use the same names that the GNU
3487 // linker does for these sections.
3489 // It is hard to handle this in a principled way, so we don't even
3490 // try. We use a table of mappings. If the input section name is
3491 // not found in the table, we simply use it as the output section
3494 const Section_name_mapping
* psnm
= section_name_mapping
;
3495 for (int i
= 0; i
< section_name_mapping_count
; ++i
, ++psnm
)
3497 if (strncmp(name
, psnm
->from
, psnm
->fromlen
) == 0)
3499 *plen
= psnm
->tolen
;
3507 // Check if a comdat group or .gnu.linkonce section with the given
3508 // NAME is selected for the link. If there is already a section,
3509 // *KEPT_SECTION is set to point to the existing section and the
3510 // function returns false. Otherwise, OBJECT, SHNDX, IS_COMDAT, and
3511 // IS_GROUP_NAME are recorded for this NAME in the layout object,
3512 // *KEPT_SECTION is set to the internal copy and the function returns
3516 Layout::find_or_add_kept_section(const std::string
& name
,
3521 Kept_section
** kept_section
)
3523 // It's normal to see a couple of entries here, for the x86 thunk
3524 // sections. If we see more than a few, we're linking a C++
3525 // program, and we resize to get more space to minimize rehashing.
3526 if (this->signatures_
.size() > 4
3527 && !this->resized_signatures_
)
3529 reserve_unordered_map(&this->signatures_
,
3530 this->number_of_input_files_
* 64);
3531 this->resized_signatures_
= true;
3534 Kept_section candidate
;
3535 std::pair
<Signatures::iterator
, bool> ins
=
3536 this->signatures_
.insert(std::make_pair(name
, candidate
));
3538 if (kept_section
!= NULL
)
3539 *kept_section
= &ins
.first
->second
;
3542 // This is the first time we've seen this signature.
3543 ins
.first
->second
.set_object(object
);
3544 ins
.first
->second
.set_shndx(shndx
);
3546 ins
.first
->second
.set_is_comdat();
3548 ins
.first
->second
.set_is_group_name();
3552 // We have already seen this signature.
3554 if (ins
.first
->second
.is_group_name())
3556 // We've already seen a real section group with this signature.
3557 // If the kept group is from a plugin object, and we're in the
3558 // replacement phase, accept the new one as a replacement.
3559 if (ins
.first
->second
.object() == NULL
3560 && parameters
->options().plugins()->in_replacement_phase())
3562 ins
.first
->second
.set_object(object
);
3563 ins
.first
->second
.set_shndx(shndx
);
3568 else if (is_group_name
)
3570 // This is a real section group, and we've already seen a
3571 // linkonce section with this signature. Record that we've seen
3572 // a section group, and don't include this section group.
3573 ins
.first
->second
.set_is_group_name();
3578 // We've already seen a linkonce section and this is a linkonce
3579 // section. These don't block each other--this may be the same
3580 // symbol name with different section types.
3585 // Store the allocated sections into the section list.
3588 Layout::get_allocated_sections(Section_list
* section_list
) const
3590 for (Section_list::const_iterator p
= this->section_list_
.begin();
3591 p
!= this->section_list_
.end();
3593 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) != 0)
3594 section_list
->push_back(*p
);
3597 // Create an output segment.
3600 Layout::make_output_segment(elfcpp::Elf_Word type
, elfcpp::Elf_Word flags
)
3602 gold_assert(!parameters
->options().relocatable());
3603 Output_segment
* oseg
= new Output_segment(type
, flags
);
3604 this->segment_list_
.push_back(oseg
);
3606 if (type
== elfcpp::PT_TLS
)
3607 this->tls_segment_
= oseg
;
3608 else if (type
== elfcpp::PT_GNU_RELRO
)
3609 this->relro_segment_
= oseg
;
3614 // Write out the Output_sections. Most won't have anything to write,
3615 // since most of the data will come from input sections which are
3616 // handled elsewhere. But some Output_sections do have Output_data.
3619 Layout::write_output_sections(Output_file
* of
) const
3621 for (Section_list::const_iterator p
= this->section_list_
.begin();
3622 p
!= this->section_list_
.end();
3625 if (!(*p
)->after_input_sections())
3630 // Write out data not associated with a section or the symbol table.
3633 Layout::write_data(const Symbol_table
* symtab
, Output_file
* of
) const
3635 if (!parameters
->options().strip_all())
3637 const Output_section
* symtab_section
= this->symtab_section_
;
3638 for (Section_list::const_iterator p
= this->section_list_
.begin();
3639 p
!= this->section_list_
.end();
3642 if ((*p
)->needs_symtab_index())
3644 gold_assert(symtab_section
!= NULL
);
3645 unsigned int index
= (*p
)->symtab_index();
3646 gold_assert(index
> 0 && index
!= -1U);
3647 off_t off
= (symtab_section
->offset()
3648 + index
* symtab_section
->entsize());
3649 symtab
->write_section_symbol(*p
, this->symtab_xindex_
, of
, off
);
3654 const Output_section
* dynsym_section
= this->dynsym_section_
;
3655 for (Section_list::const_iterator p
= this->section_list_
.begin();
3656 p
!= this->section_list_
.end();
3659 if ((*p
)->needs_dynsym_index())
3661 gold_assert(dynsym_section
!= NULL
);
3662 unsigned int index
= (*p
)->dynsym_index();
3663 gold_assert(index
> 0 && index
!= -1U);
3664 off_t off
= (dynsym_section
->offset()
3665 + index
* dynsym_section
->entsize());
3666 symtab
->write_section_symbol(*p
, this->dynsym_xindex_
, of
, off
);
3670 // Write out the Output_data which are not in an Output_section.
3671 for (Data_list::const_iterator p
= this->special_output_list_
.begin();
3672 p
!= this->special_output_list_
.end();
3677 // Write out the Output_sections which can only be written after the
3678 // input sections are complete.
3681 Layout::write_sections_after_input_sections(Output_file
* of
)
3683 // Determine the final section offsets, and thus the final output
3684 // file size. Note we finalize the .shstrab last, to allow the
3685 // after_input_section sections to modify their section-names before
3687 if (this->any_postprocessing_sections_
)
3689 off_t off
= this->output_file_size_
;
3690 off
= this->set_section_offsets(off
, POSTPROCESSING_SECTIONS_PASS
);
3692 // Now that we've finalized the names, we can finalize the shstrab.
3694 this->set_section_offsets(off
,
3695 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
);
3697 if (off
> this->output_file_size_
)
3700 this->output_file_size_
= off
;
3704 for (Section_list::const_iterator p
= this->section_list_
.begin();
3705 p
!= this->section_list_
.end();
3708 if ((*p
)->after_input_sections())
3712 this->section_headers_
->write(of
);
3715 // If the build ID requires computing a checksum, do so here, and
3716 // write it out. We compute a checksum over the entire file because
3717 // that is simplest.
3720 Layout::write_build_id(Output_file
* of
) const
3722 if (this->build_id_note_
== NULL
)
3725 const unsigned char* iv
= of
->get_input_view(0, this->output_file_size_
);
3727 unsigned char* ov
= of
->get_output_view(this->build_id_note_
->offset(),
3728 this->build_id_note_
->data_size());
3730 const char* style
= parameters
->options().build_id();
3731 if (strcmp(style
, "sha1") == 0)
3734 sha1_init_ctx(&ctx
);
3735 sha1_process_bytes(iv
, this->output_file_size_
, &ctx
);
3736 sha1_finish_ctx(&ctx
, ov
);
3738 else if (strcmp(style
, "md5") == 0)
3742 md5_process_bytes(iv
, this->output_file_size_
, &ctx
);
3743 md5_finish_ctx(&ctx
, ov
);
3748 of
->write_output_view(this->build_id_note_
->offset(),
3749 this->build_id_note_
->data_size(),
3752 of
->free_input_view(0, this->output_file_size_
, iv
);
3755 // Write out a binary file. This is called after the link is
3756 // complete. IN is the temporary output file we used to generate the
3757 // ELF code. We simply walk through the segments, read them from
3758 // their file offset in IN, and write them to their load address in
3759 // the output file. FIXME: with a bit more work, we could support
3760 // S-records and/or Intel hex format here.
3763 Layout::write_binary(Output_file
* in
) const
3765 gold_assert(parameters
->options().oformat_enum()
3766 == General_options::OBJECT_FORMAT_BINARY
);
3768 // Get the size of the binary file.
3769 uint64_t max_load_address
= 0;
3770 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
3771 p
!= this->segment_list_
.end();
3774 if ((*p
)->type() == elfcpp::PT_LOAD
&& (*p
)->filesz() > 0)
3776 uint64_t max_paddr
= (*p
)->paddr() + (*p
)->filesz();
3777 if (max_paddr
> max_load_address
)
3778 max_load_address
= max_paddr
;
3782 Output_file
out(parameters
->options().output_file_name());
3783 out
.open(max_load_address
);
3785 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
3786 p
!= this->segment_list_
.end();
3789 if ((*p
)->type() == elfcpp::PT_LOAD
&& (*p
)->filesz() > 0)
3791 const unsigned char* vin
= in
->get_input_view((*p
)->offset(),
3793 unsigned char* vout
= out
.get_output_view((*p
)->paddr(),
3795 memcpy(vout
, vin
, (*p
)->filesz());
3796 out
.write_output_view((*p
)->paddr(), (*p
)->filesz(), vout
);
3797 in
->free_input_view((*p
)->offset(), (*p
)->filesz(), vin
);
3804 // Print the output sections to the map file.
3807 Layout::print_to_mapfile(Mapfile
* mapfile
) const
3809 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
3810 p
!= this->segment_list_
.end();
3812 (*p
)->print_sections_to_mapfile(mapfile
);
3815 // Print statistical information to stderr. This is used for --stats.
3818 Layout::print_stats() const
3820 this->namepool_
.print_stats("section name pool");
3821 this->sympool_
.print_stats("output symbol name pool");
3822 this->dynpool_
.print_stats("dynamic name pool");
3824 for (Section_list::const_iterator p
= this->section_list_
.begin();
3825 p
!= this->section_list_
.end();
3827 (*p
)->print_merge_stats();
3830 // Write_sections_task methods.
3832 // We can always run this task.
3835 Write_sections_task::is_runnable()
3840 // We need to unlock both OUTPUT_SECTIONS_BLOCKER and FINAL_BLOCKER
3844 Write_sections_task::locks(Task_locker
* tl
)
3846 tl
->add(this, this->output_sections_blocker_
);
3847 tl
->add(this, this->final_blocker_
);
3850 // Run the task--write out the data.
3853 Write_sections_task::run(Workqueue
*)
3855 this->layout_
->write_output_sections(this->of_
);
3858 // Write_data_task methods.
3860 // We can always run this task.
3863 Write_data_task::is_runnable()
3868 // We need to unlock FINAL_BLOCKER when finished.
3871 Write_data_task::locks(Task_locker
* tl
)
3873 tl
->add(this, this->final_blocker_
);
3876 // Run the task--write out the data.
3879 Write_data_task::run(Workqueue
*)
3881 this->layout_
->write_data(this->symtab_
, this->of_
);
3884 // Write_symbols_task methods.
3886 // We can always run this task.
3889 Write_symbols_task::is_runnable()
3894 // We need to unlock FINAL_BLOCKER when finished.
3897 Write_symbols_task::locks(Task_locker
* tl
)
3899 tl
->add(this, this->final_blocker_
);
3902 // Run the task--write out the symbols.
3905 Write_symbols_task::run(Workqueue
*)
3907 this->symtab_
->write_globals(this->sympool_
, this->dynpool_
,
3908 this->layout_
->symtab_xindex(),
3909 this->layout_
->dynsym_xindex(), this->of_
);
3912 // Write_after_input_sections_task methods.
3914 // We can only run this task after the input sections have completed.
3917 Write_after_input_sections_task::is_runnable()
3919 if (this->input_sections_blocker_
->is_blocked())
3920 return this->input_sections_blocker_
;
3924 // We need to unlock FINAL_BLOCKER when finished.
3927 Write_after_input_sections_task::locks(Task_locker
* tl
)
3929 tl
->add(this, this->final_blocker_
);
3935 Write_after_input_sections_task::run(Workqueue
*)
3937 this->layout_
->write_sections_after_input_sections(this->of_
);
3940 // Close_task_runner methods.
3942 // Run the task--close the file.
3945 Close_task_runner::run(Workqueue
*, const Task
*)
3947 // If we need to compute a checksum for the BUILD if, we do so here.
3948 this->layout_
->write_build_id(this->of_
);
3950 // If we've been asked to create a binary file, we do so here.
3951 if (this->options_
->oformat_enum() != General_options::OBJECT_FORMAT_ELF
)
3952 this->layout_
->write_binary(this->of_
);
3957 // Instantiate the templates we need. We could use the configure
3958 // script to restrict this to only the ones for implemented targets.
3960 #ifdef HAVE_TARGET_32_LITTLE
3963 Layout::layout
<32, false>(Sized_relobj
<32, false>* object
, unsigned int shndx
,
3965 const elfcpp::Shdr
<32, false>& shdr
,
3966 unsigned int, unsigned int, off_t
*);
3969 #ifdef HAVE_TARGET_32_BIG
3972 Layout::layout
<32, true>(Sized_relobj
<32, true>* object
, unsigned int shndx
,
3974 const elfcpp::Shdr
<32, true>& shdr
,
3975 unsigned int, unsigned int, off_t
*);
3978 #ifdef HAVE_TARGET_64_LITTLE
3981 Layout::layout
<64, false>(Sized_relobj
<64, false>* object
, unsigned int shndx
,
3983 const elfcpp::Shdr
<64, false>& shdr
,
3984 unsigned int, unsigned int, off_t
*);
3987 #ifdef HAVE_TARGET_64_BIG
3990 Layout::layout
<64, true>(Sized_relobj
<64, true>* object
, unsigned int shndx
,
3992 const elfcpp::Shdr
<64, true>& shdr
,
3993 unsigned int, unsigned int, off_t
*);
3996 #ifdef HAVE_TARGET_32_LITTLE
3999 Layout::layout_reloc
<32, false>(Sized_relobj
<32, false>* object
,
4000 unsigned int reloc_shndx
,
4001 const elfcpp::Shdr
<32, false>& shdr
,
4002 Output_section
* data_section
,
4003 Relocatable_relocs
* rr
);
4006 #ifdef HAVE_TARGET_32_BIG
4009 Layout::layout_reloc
<32, true>(Sized_relobj
<32, true>* object
,
4010 unsigned int reloc_shndx
,
4011 const elfcpp::Shdr
<32, true>& shdr
,
4012 Output_section
* data_section
,
4013 Relocatable_relocs
* rr
);
4016 #ifdef HAVE_TARGET_64_LITTLE
4019 Layout::layout_reloc
<64, false>(Sized_relobj
<64, false>* object
,
4020 unsigned int reloc_shndx
,
4021 const elfcpp::Shdr
<64, false>& shdr
,
4022 Output_section
* data_section
,
4023 Relocatable_relocs
* rr
);
4026 #ifdef HAVE_TARGET_64_BIG
4029 Layout::layout_reloc
<64, true>(Sized_relobj
<64, true>* object
,
4030 unsigned int reloc_shndx
,
4031 const elfcpp::Shdr
<64, true>& shdr
,
4032 Output_section
* data_section
,
4033 Relocatable_relocs
* rr
);
4036 #ifdef HAVE_TARGET_32_LITTLE
4039 Layout::layout_group
<32, false>(Symbol_table
* symtab
,
4040 Sized_relobj
<32, false>* object
,
4042 const char* group_section_name
,
4043 const char* signature
,
4044 const elfcpp::Shdr
<32, false>& shdr
,
4045 elfcpp::Elf_Word flags
,
4046 std::vector
<unsigned int>* shndxes
);
4049 #ifdef HAVE_TARGET_32_BIG
4052 Layout::layout_group
<32, true>(Symbol_table
* symtab
,
4053 Sized_relobj
<32, true>* object
,
4055 const char* group_section_name
,
4056 const char* signature
,
4057 const elfcpp::Shdr
<32, true>& shdr
,
4058 elfcpp::Elf_Word flags
,
4059 std::vector
<unsigned int>* shndxes
);
4062 #ifdef HAVE_TARGET_64_LITTLE
4065 Layout::layout_group
<64, false>(Symbol_table
* symtab
,
4066 Sized_relobj
<64, false>* object
,
4068 const char* group_section_name
,
4069 const char* signature
,
4070 const elfcpp::Shdr
<64, false>& shdr
,
4071 elfcpp::Elf_Word flags
,
4072 std::vector
<unsigned int>* shndxes
);
4075 #ifdef HAVE_TARGET_64_BIG
4078 Layout::layout_group
<64, true>(Symbol_table
* symtab
,
4079 Sized_relobj
<64, true>* object
,
4081 const char* group_section_name
,
4082 const char* signature
,
4083 const elfcpp::Shdr
<64, true>& shdr
,
4084 elfcpp::Elf_Word flags
,
4085 std::vector
<unsigned int>* shndxes
);
4088 #ifdef HAVE_TARGET_32_LITTLE
4091 Layout::layout_eh_frame
<32, false>(Sized_relobj
<32, false>* object
,
4092 const unsigned char* symbols
,
4094 const unsigned char* symbol_names
,
4095 off_t symbol_names_size
,
4097 const elfcpp::Shdr
<32, false>& shdr
,
4098 unsigned int reloc_shndx
,
4099 unsigned int reloc_type
,
4103 #ifdef HAVE_TARGET_32_BIG
4106 Layout::layout_eh_frame
<32, true>(Sized_relobj
<32, true>* object
,
4107 const unsigned char* symbols
,
4109 const unsigned char* symbol_names
,
4110 off_t symbol_names_size
,
4112 const elfcpp::Shdr
<32, true>& shdr
,
4113 unsigned int reloc_shndx
,
4114 unsigned int reloc_type
,
4118 #ifdef HAVE_TARGET_64_LITTLE
4121 Layout::layout_eh_frame
<64, false>(Sized_relobj
<64, false>* object
,
4122 const unsigned char* symbols
,
4124 const unsigned char* symbol_names
,
4125 off_t symbol_names_size
,
4127 const elfcpp::Shdr
<64, false>& shdr
,
4128 unsigned int reloc_shndx
,
4129 unsigned int reloc_type
,
4133 #ifdef HAVE_TARGET_64_BIG
4136 Layout::layout_eh_frame
<64, true>(Sized_relobj
<64, true>* object
,
4137 const unsigned char* symbols
,
4139 const unsigned char* symbol_names
,
4140 off_t symbol_names_size
,
4142 const elfcpp::Shdr
<64, true>& shdr
,
4143 unsigned int reloc_shndx
,
4144 unsigned int reloc_type
,
4148 } // End namespace gold.