1 // layout.cc -- lay out output file sections for gold
3 // Copyright 2006, 2007, 2008, 2009, 2010 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
))
355 // The GNU linker strips .gnu_debuglink sections, so we do too.
356 // This is a feature used to keep debugging information in
358 if (strcmp(name
, ".gnu_debuglink") == 0)
367 // Return an output section named NAME, or NULL if there is none.
370 Layout::find_output_section(const char* name
) const
372 for (Section_list::const_iterator p
= this->section_list_
.begin();
373 p
!= this->section_list_
.end();
375 if (strcmp((*p
)->name(), name
) == 0)
380 // Return an output segment of type TYPE, with segment flags SET set
381 // and segment flags CLEAR clear. Return NULL if there is none.
384 Layout::find_output_segment(elfcpp::PT type
, elfcpp::Elf_Word set
,
385 elfcpp::Elf_Word clear
) const
387 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
388 p
!= this->segment_list_
.end();
390 if (static_cast<elfcpp::PT
>((*p
)->type()) == type
391 && ((*p
)->flags() & set
) == set
392 && ((*p
)->flags() & clear
) == 0)
397 // Return the output section to use for section NAME with type TYPE
398 // and section flags FLAGS. NAME must be canonicalized in the string
399 // pool, and NAME_KEY is the key. IS_INTERP is true if this is the
400 // .interp section. IS_DYNAMIC_LINKER_SECTION is true if this section
401 // is used by the dynamic linker. IS_RELRO is true for a relro
402 // section. IS_LAST_RELRO is true for the last relro section.
403 // IS_FIRST_NON_RELRO is true for the first non-relro section.
406 Layout::get_output_section(const char* name
, Stringpool::Key name_key
,
407 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
,
408 bool is_interp
, bool is_dynamic_linker_section
,
409 bool is_relro
, bool is_last_relro
,
410 bool is_first_non_relro
)
412 elfcpp::Elf_Xword lookup_flags
= flags
;
414 // Ignoring SHF_WRITE and SHF_EXECINSTR here means that we combine
415 // read-write with read-only sections. Some other ELF linkers do
416 // not do this. FIXME: Perhaps there should be an option
418 lookup_flags
&= ~(elfcpp::SHF_WRITE
| elfcpp::SHF_EXECINSTR
);
420 const Key
key(name_key
, std::make_pair(type
, lookup_flags
));
421 const std::pair
<Key
, Output_section
*> v(key
, NULL
);
422 std::pair
<Section_name_map::iterator
, bool> ins(
423 this->section_name_map_
.insert(v
));
426 return ins
.first
->second
;
429 // This is the first time we've seen this name/type/flags
430 // combination. For compatibility with the GNU linker, we
431 // combine sections with contents and zero flags with sections
432 // with non-zero flags. This is a workaround for cases where
433 // assembler code forgets to set section flags. FIXME: Perhaps
434 // there should be an option to control this.
435 Output_section
* os
= NULL
;
437 if (type
== elfcpp::SHT_PROGBITS
)
441 Output_section
* same_name
= this->find_output_section(name
);
442 if (same_name
!= NULL
443 && same_name
->type() == elfcpp::SHT_PROGBITS
444 && (same_name
->flags() & elfcpp::SHF_TLS
) == 0)
447 else if ((flags
& elfcpp::SHF_TLS
) == 0)
449 elfcpp::Elf_Xword zero_flags
= 0;
450 const Key
zero_key(name_key
, std::make_pair(type
, zero_flags
));
451 Section_name_map::iterator p
=
452 this->section_name_map_
.find(zero_key
);
453 if (p
!= this->section_name_map_
.end())
459 os
= this->make_output_section(name
, type
, flags
, is_interp
,
460 is_dynamic_linker_section
, is_relro
,
461 is_last_relro
, is_first_non_relro
);
462 ins
.first
->second
= os
;
467 // Pick the output section to use for section NAME, in input file
468 // RELOBJ, with type TYPE and flags FLAGS. RELOBJ may be NULL for a
469 // linker created section. IS_INPUT_SECTION is true if we are
470 // choosing an output section for an input section found in a input
471 // file. IS_INTERP is true if this is the .interp section.
472 // IS_DYNAMIC_LINKER_SECTION is true if this section is used by the
473 // dynamic linker. IS_RELRO is true for a relro section.
474 // IS_LAST_RELRO is true for the last relro section.
475 // IS_FIRST_NON_RELRO is true for the first non-relro section. This
476 // will return NULL if the input section should be discarded.
479 Layout::choose_output_section(const Relobj
* relobj
, const char* name
,
480 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
,
481 bool is_input_section
, bool is_interp
,
482 bool is_dynamic_linker_section
, bool is_relro
,
483 bool is_last_relro
, bool is_first_non_relro
)
485 // We should not see any input sections after we have attached
486 // sections to segments.
487 gold_assert(!is_input_section
|| !this->sections_are_attached_
);
489 // Some flags in the input section should not be automatically
490 // copied to the output section.
491 flags
&= ~ (elfcpp::SHF_INFO_LINK
492 | elfcpp::SHF_LINK_ORDER
495 | elfcpp::SHF_STRINGS
);
497 if (this->script_options_
->saw_sections_clause())
499 // We are using a SECTIONS clause, so the output section is
500 // chosen based only on the name.
502 Script_sections
* ss
= this->script_options_
->script_sections();
503 const char* file_name
= relobj
== NULL
? NULL
: relobj
->name().c_str();
504 Output_section
** output_section_slot
;
505 Script_sections::Section_type script_section_type
;
506 name
= ss
->output_section_name(file_name
, name
, &output_section_slot
,
507 &script_section_type
);
510 // The SECTIONS clause says to discard this input section.
514 // We can only handle script section types ST_NONE and ST_NOLOAD.
515 switch (script_section_type
)
517 case Script_sections::ST_NONE
:
519 case Script_sections::ST_NOLOAD
:
520 flags
&= elfcpp::SHF_ALLOC
;
526 // If this is an orphan section--one not mentioned in the linker
527 // script--then OUTPUT_SECTION_SLOT will be NULL, and we do the
528 // default processing below.
530 if (output_section_slot
!= NULL
)
532 if (*output_section_slot
!= NULL
)
534 (*output_section_slot
)->update_flags_for_input_section(flags
);
535 return *output_section_slot
;
538 // We don't put sections found in the linker script into
539 // SECTION_NAME_MAP_. That keeps us from getting confused
540 // if an orphan section is mapped to a section with the same
541 // name as one in the linker script.
543 name
= this->namepool_
.add(name
, false, NULL
);
546 this->make_output_section(name
, type
, flags
, is_interp
,
547 is_dynamic_linker_section
, is_relro
,
548 is_last_relro
, is_first_non_relro
);
549 os
->set_found_in_sections_clause();
551 // Special handling for NOLOAD sections.
552 if (script_section_type
== Script_sections::ST_NOLOAD
)
556 // The constructor of Output_section sets addresses of non-ALLOC
557 // sections to 0 by default. We don't want that for NOLOAD
558 // sections even if they have no SHF_ALLOC flag.
559 if ((os
->flags() & elfcpp::SHF_ALLOC
) == 0
560 && os
->is_address_valid())
562 gold_assert(os
->address() == 0
563 && !os
->is_offset_valid()
564 && !os
->is_data_size_valid());
565 os
->reset_address_and_file_offset();
569 *output_section_slot
= os
;
574 // FIXME: Handle SHF_OS_NONCONFORMING somewhere.
576 // Turn NAME from the name of the input section into the name of the
579 size_t len
= strlen(name
);
581 && !this->script_options_
->saw_sections_clause()
582 && !parameters
->options().relocatable())
583 name
= Layout::output_section_name(name
, &len
);
585 Stringpool::Key name_key
;
586 name
= this->namepool_
.add_with_length(name
, len
, true, &name_key
);
588 // Find or make the output section. The output section is selected
589 // based on the section name, type, and flags.
590 return this->get_output_section(name
, name_key
, type
, flags
, is_interp
,
591 is_dynamic_linker_section
, is_relro
,
592 is_last_relro
, is_first_non_relro
);
595 // Return the output section to use for input section SHNDX, with name
596 // NAME, with header HEADER, from object OBJECT. RELOC_SHNDX is the
597 // index of a relocation section which applies to this section, or 0
598 // if none, or -1U if more than one. RELOC_TYPE is the type of the
599 // relocation section if there is one. Set *OFF to the offset of this
600 // input section without the output section. Return NULL if the
601 // section should be discarded. Set *OFF to -1 if the section
602 // contents should not be written directly to the output file, but
603 // will instead receive special handling.
605 template<int size
, bool big_endian
>
607 Layout::layout(Sized_relobj
<size
, big_endian
>* object
, unsigned int shndx
,
608 const char* name
, const elfcpp::Shdr
<size
, big_endian
>& shdr
,
609 unsigned int reloc_shndx
, unsigned int, off_t
* off
)
613 if (!this->include_section(object
, name
, shdr
))
618 // Sometimes .init_array*, .preinit_array* and .fini_array* do not have
619 // correct section types. Force them here.
620 elfcpp::Elf_Word sh_type
= shdr
.get_sh_type();
621 if (sh_type
== elfcpp::SHT_PROGBITS
)
623 static const char init_array_prefix
[] = ".init_array";
624 static const char preinit_array_prefix
[] = ".preinit_array";
625 static const char fini_array_prefix
[] = ".fini_array";
626 static size_t init_array_prefix_size
= sizeof(init_array_prefix
) - 1;
627 static size_t preinit_array_prefix_size
=
628 sizeof(preinit_array_prefix
) - 1;
629 static size_t fini_array_prefix_size
= sizeof(fini_array_prefix
) - 1;
631 if (strncmp(name
, init_array_prefix
, init_array_prefix_size
) == 0)
632 sh_type
= elfcpp::SHT_INIT_ARRAY
;
633 else if (strncmp(name
, preinit_array_prefix
, preinit_array_prefix_size
)
635 sh_type
= elfcpp::SHT_PREINIT_ARRAY
;
636 else if (strncmp(name
, fini_array_prefix
, fini_array_prefix_size
) == 0)
637 sh_type
= elfcpp::SHT_FINI_ARRAY
;
640 // In a relocatable link a grouped section must not be combined with
641 // any other sections.
642 if (parameters
->options().relocatable()
643 && (shdr
.get_sh_flags() & elfcpp::SHF_GROUP
) != 0)
645 name
= this->namepool_
.add(name
, true, NULL
);
646 os
= this->make_output_section(name
, sh_type
, shdr
.get_sh_flags(), false,
647 false, false, false, false);
651 os
= this->choose_output_section(object
, name
, sh_type
,
652 shdr
.get_sh_flags(), true, false,
653 false, false, false, false);
658 // By default the GNU linker sorts input sections whose names match
659 // .ctor.*, .dtor.*, .init_array.*, or .fini_array.*. The sections
660 // are sorted by name. This is used to implement constructor
661 // priority ordering. We are compatible.
662 if (!this->script_options_
->saw_sections_clause()
663 && (is_prefix_of(".ctors.", name
)
664 || is_prefix_of(".dtors.", name
)
665 || is_prefix_of(".init_array.", name
)
666 || is_prefix_of(".fini_array.", name
)))
667 os
->set_must_sort_attached_input_sections();
669 // FIXME: Handle SHF_LINK_ORDER somewhere.
671 *off
= os
->add_input_section(object
, shndx
, name
, shdr
, reloc_shndx
,
672 this->script_options_
->saw_sections_clause());
673 this->have_added_input_section_
= true;
678 // Handle a relocation section when doing a relocatable link.
680 template<int size
, bool big_endian
>
682 Layout::layout_reloc(Sized_relobj
<size
, big_endian
>* object
,
684 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
685 Output_section
* data_section
,
686 Relocatable_relocs
* rr
)
688 gold_assert(parameters
->options().relocatable()
689 || parameters
->options().emit_relocs());
691 int sh_type
= shdr
.get_sh_type();
694 if (sh_type
== elfcpp::SHT_REL
)
696 else if (sh_type
== elfcpp::SHT_RELA
)
700 name
+= data_section
->name();
702 Output_section
* os
= this->choose_output_section(object
, name
.c_str(),
706 false, false, false);
708 os
->set_should_link_to_symtab();
709 os
->set_info_section(data_section
);
711 Output_section_data
* posd
;
712 if (sh_type
== elfcpp::SHT_REL
)
714 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rel_size
);
715 posd
= new Output_relocatable_relocs
<elfcpp::SHT_REL
,
719 else if (sh_type
== elfcpp::SHT_RELA
)
721 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rela_size
);
722 posd
= new Output_relocatable_relocs
<elfcpp::SHT_RELA
,
729 os
->add_output_section_data(posd
);
730 rr
->set_output_data(posd
);
735 // Handle a group section when doing a relocatable link.
737 template<int size
, bool big_endian
>
739 Layout::layout_group(Symbol_table
* symtab
,
740 Sized_relobj
<size
, big_endian
>* object
,
742 const char* group_section_name
,
743 const char* signature
,
744 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
745 elfcpp::Elf_Word flags
,
746 std::vector
<unsigned int>* shndxes
)
748 gold_assert(parameters
->options().relocatable());
749 gold_assert(shdr
.get_sh_type() == elfcpp::SHT_GROUP
);
750 group_section_name
= this->namepool_
.add(group_section_name
, true, NULL
);
751 Output_section
* os
= this->make_output_section(group_section_name
,
757 // We need to find a symbol with the signature in the symbol table.
758 // If we don't find one now, we need to look again later.
759 Symbol
* sym
= symtab
->lookup(signature
, NULL
);
761 os
->set_info_symndx(sym
);
764 // Reserve some space to minimize reallocations.
765 if (this->group_signatures_
.empty())
766 this->group_signatures_
.reserve(this->number_of_input_files_
* 16);
768 // We will wind up using a symbol whose name is the signature.
769 // So just put the signature in the symbol name pool to save it.
770 signature
= symtab
->canonicalize_name(signature
);
771 this->group_signatures_
.push_back(Group_signature(os
, signature
));
774 os
->set_should_link_to_symtab();
777 section_size_type entry_count
=
778 convert_to_section_size_type(shdr
.get_sh_size() / 4);
779 Output_section_data
* posd
=
780 new Output_data_group
<size
, big_endian
>(object
, entry_count
, flags
,
782 os
->add_output_section_data(posd
);
785 // Special GNU handling of sections name .eh_frame. They will
786 // normally hold exception frame data as defined by the C++ ABI
787 // (http://codesourcery.com/cxx-abi/).
789 template<int size
, bool big_endian
>
791 Layout::layout_eh_frame(Sized_relobj
<size
, big_endian
>* object
,
792 const unsigned char* symbols
,
794 const unsigned char* symbol_names
,
795 off_t symbol_names_size
,
797 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
798 unsigned int reloc_shndx
, unsigned int reloc_type
,
801 gold_assert(shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
);
802 gold_assert((shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) != 0);
804 const char* const name
= ".eh_frame";
805 Output_section
* os
= this->choose_output_section(object
,
807 elfcpp::SHT_PROGBITS
,
810 false, false, false);
814 if (this->eh_frame_section_
== NULL
)
816 this->eh_frame_section_
= os
;
817 this->eh_frame_data_
= new Eh_frame();
819 if (parameters
->options().eh_frame_hdr())
821 Output_section
* hdr_os
=
822 this->choose_output_section(NULL
,
824 elfcpp::SHT_PROGBITS
,
827 false, false, false);
831 Eh_frame_hdr
* hdr_posd
= new Eh_frame_hdr(os
,
832 this->eh_frame_data_
);
833 hdr_os
->add_output_section_data(hdr_posd
);
835 hdr_os
->set_after_input_sections();
837 if (!this->script_options_
->saw_phdrs_clause())
839 Output_segment
* hdr_oseg
;
840 hdr_oseg
= this->make_output_segment(elfcpp::PT_GNU_EH_FRAME
,
842 hdr_oseg
->add_output_section(hdr_os
, elfcpp::PF_R
, false);
845 this->eh_frame_data_
->set_eh_frame_hdr(hdr_posd
);
850 gold_assert(this->eh_frame_section_
== os
);
852 if (this->eh_frame_data_
->add_ehframe_input_section(object
,
861 os
->update_flags_for_input_section(shdr
.get_sh_flags());
863 // We found a .eh_frame section we are going to optimize, so now
864 // we can add the set of optimized sections to the output
865 // section. We need to postpone adding this until we've found a
866 // section we can optimize so that the .eh_frame section in
867 // crtbegin.o winds up at the start of the output section.
868 if (!this->added_eh_frame_data_
)
870 os
->add_output_section_data(this->eh_frame_data_
);
871 this->added_eh_frame_data_
= true;
877 // We couldn't handle this .eh_frame section for some reason.
878 // Add it as a normal section.
879 bool saw_sections_clause
= this->script_options_
->saw_sections_clause();
880 *off
= os
->add_input_section(object
, shndx
, name
, shdr
, reloc_shndx
,
881 saw_sections_clause
);
882 this->have_added_input_section_
= true;
888 // Add POSD to an output section using NAME, TYPE, and FLAGS. Return
889 // the output section.
892 Layout::add_output_section_data(const char* name
, elfcpp::Elf_Word type
,
893 elfcpp::Elf_Xword flags
,
894 Output_section_data
* posd
,
895 bool is_dynamic_linker_section
,
896 bool is_relro
, bool is_last_relro
,
897 bool is_first_non_relro
)
899 Output_section
* os
= this->choose_output_section(NULL
, name
, type
, flags
,
901 is_dynamic_linker_section
,
902 is_relro
, is_last_relro
,
905 os
->add_output_section_data(posd
);
909 // Map section flags to segment flags.
912 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags
)
914 elfcpp::Elf_Word ret
= elfcpp::PF_R
;
915 if ((flags
& elfcpp::SHF_WRITE
) != 0)
917 if ((flags
& elfcpp::SHF_EXECINSTR
) != 0)
922 // Sometimes we compress sections. This is typically done for
923 // sections that are not part of normal program execution (such as
924 // .debug_* sections), and where the readers of these sections know
925 // how to deal with compressed sections. This routine doesn't say for
926 // certain whether we'll compress -- it depends on commandline options
927 // as well -- just whether this section is a candidate for compression.
928 // (The Output_compressed_section class decides whether to compress
929 // a given section, and picks the name of the compressed section.)
932 is_compressible_debug_section(const char* secname
)
934 return (strncmp(secname
, ".debug", sizeof(".debug") - 1) == 0);
937 // Make a new Output_section, and attach it to segments as
938 // appropriate. IS_INTERP is true if this is the .interp section.
939 // IS_DYNAMIC_LINKER_SECTION is true if this section is used by the
940 // dynamic linker. IS_RELRO is true if this is a relro section.
941 // IS_LAST_RELRO is true if this is the last relro section.
942 // IS_FIRST_NON_RELRO is true if this is the first non relro section.
945 Layout::make_output_section(const char* name
, elfcpp::Elf_Word type
,
946 elfcpp::Elf_Xword flags
, bool is_interp
,
947 bool is_dynamic_linker_section
, bool is_relro
,
948 bool is_last_relro
, bool is_first_non_relro
)
951 if ((flags
& elfcpp::SHF_ALLOC
) == 0
952 && strcmp(parameters
->options().compress_debug_sections(), "none") != 0
953 && is_compressible_debug_section(name
))
954 os
= new Output_compressed_section(¶meters
->options(), name
, type
,
956 else if ((flags
& elfcpp::SHF_ALLOC
) == 0
957 && parameters
->options().strip_debug_non_line()
958 && strcmp(".debug_abbrev", name
) == 0)
960 os
= this->debug_abbrev_
= new Output_reduced_debug_abbrev_section(
962 if (this->debug_info_
)
963 this->debug_info_
->set_abbreviations(this->debug_abbrev_
);
965 else if ((flags
& elfcpp::SHF_ALLOC
) == 0
966 && parameters
->options().strip_debug_non_line()
967 && strcmp(".debug_info", name
) == 0)
969 os
= this->debug_info_
= new Output_reduced_debug_info_section(
971 if (this->debug_abbrev_
)
972 this->debug_info_
->set_abbreviations(this->debug_abbrev_
);
976 // FIXME: const_cast is ugly.
977 Target
* target
= const_cast<Target
*>(¶meters
->target());
978 os
= target
->make_output_section(name
, type
, flags
);
983 if (is_dynamic_linker_section
)
984 os
->set_is_dynamic_linker_section();
988 os
->set_is_last_relro();
989 if (is_first_non_relro
)
990 os
->set_is_first_non_relro();
992 parameters
->target().new_output_section(os
);
994 this->section_list_
.push_back(os
);
996 // The GNU linker by default sorts some sections by priority, so we
997 // do the same. We need to know that this might happen before we
998 // attach any input sections.
999 if (!this->script_options_
->saw_sections_clause()
1000 && (strcmp(name
, ".ctors") == 0
1001 || strcmp(name
, ".dtors") == 0
1002 || strcmp(name
, ".init_array") == 0
1003 || strcmp(name
, ".fini_array") == 0))
1004 os
->set_may_sort_attached_input_sections();
1006 // With -z relro, we have to recognize the special sections by name.
1007 // There is no other way.
1008 if (!this->script_options_
->saw_sections_clause()
1009 && parameters
->options().relro()
1010 && type
== elfcpp::SHT_PROGBITS
1011 && (flags
& elfcpp::SHF_ALLOC
) != 0
1012 && (flags
& elfcpp::SHF_WRITE
) != 0)
1014 if (strcmp(name
, ".data.rel.ro") == 0)
1016 else if (strcmp(name
, ".data.rel.ro.local") == 0)
1019 os
->set_is_relro_local();
1023 // Check for .stab*str sections, as .stab* sections need to link to
1025 if (type
== elfcpp::SHT_STRTAB
1026 && !this->have_stabstr_section_
1027 && strncmp(name
, ".stab", 5) == 0
1028 && strcmp(name
+ strlen(name
) - 3, "str") == 0)
1029 this->have_stabstr_section_
= true;
1031 // If we have already attached the sections to segments, then we
1032 // need to attach this one now. This happens for sections created
1033 // directly by the linker.
1034 if (this->sections_are_attached_
)
1035 this->attach_section_to_segment(os
);
1040 // Attach output sections to segments. This is called after we have
1041 // seen all the input sections.
1044 Layout::attach_sections_to_segments()
1046 for (Section_list::iterator p
= this->section_list_
.begin();
1047 p
!= this->section_list_
.end();
1049 this->attach_section_to_segment(*p
);
1051 this->sections_are_attached_
= true;
1054 // Attach an output section to a segment.
1057 Layout::attach_section_to_segment(Output_section
* os
)
1059 if ((os
->flags() & elfcpp::SHF_ALLOC
) == 0)
1060 this->unattached_section_list_
.push_back(os
);
1062 this->attach_allocated_section_to_segment(os
);
1065 // Attach an allocated output section to a segment.
1068 Layout::attach_allocated_section_to_segment(Output_section
* os
)
1070 elfcpp::Elf_Xword flags
= os
->flags();
1071 gold_assert((flags
& elfcpp::SHF_ALLOC
) != 0);
1073 if (parameters
->options().relocatable())
1076 // If we have a SECTIONS clause, we can't handle the attachment to
1077 // segments until after we've seen all the sections.
1078 if (this->script_options_
->saw_sections_clause())
1081 gold_assert(!this->script_options_
->saw_phdrs_clause());
1083 // This output section goes into a PT_LOAD segment.
1085 elfcpp::Elf_Word seg_flags
= Layout::section_flags_to_segment(flags
);
1087 // Check for --section-start.
1089 bool is_address_set
= parameters
->options().section_start(os
->name(), &addr
);
1091 // In general the only thing we really care about for PT_LOAD
1092 // segments is whether or not they are writable, so that is how we
1093 // search for them. Large data sections also go into their own
1094 // PT_LOAD segment. People who need segments sorted on some other
1095 // basis will have to use a linker script.
1097 Segment_list::const_iterator p
;
1098 for (p
= this->segment_list_
.begin();
1099 p
!= this->segment_list_
.end();
1102 if ((*p
)->type() != elfcpp::PT_LOAD
)
1104 if (!parameters
->options().omagic()
1105 && ((*p
)->flags() & elfcpp::PF_W
) != (seg_flags
& elfcpp::PF_W
))
1107 // If -Tbss was specified, we need to separate the data and BSS
1109 if (parameters
->options().user_set_Tbss())
1111 if ((os
->type() == elfcpp::SHT_NOBITS
)
1112 == (*p
)->has_any_data_sections())
1115 if (os
->is_large_data_section() && !(*p
)->is_large_data_segment())
1120 if ((*p
)->are_addresses_set())
1123 (*p
)->add_initial_output_data(os
);
1124 (*p
)->update_flags_for_output_section(seg_flags
);
1125 (*p
)->set_addresses(addr
, addr
);
1129 (*p
)->add_output_section(os
, seg_flags
, true);
1133 if (p
== this->segment_list_
.end())
1135 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_LOAD
,
1137 if (os
->is_large_data_section())
1138 oseg
->set_is_large_data_segment();
1139 oseg
->add_output_section(os
, seg_flags
, true);
1141 oseg
->set_addresses(addr
, addr
);
1144 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
1146 if (os
->type() == elfcpp::SHT_NOTE
)
1148 // See if we already have an equivalent PT_NOTE segment.
1149 for (p
= this->segment_list_
.begin();
1150 p
!= segment_list_
.end();
1153 if ((*p
)->type() == elfcpp::PT_NOTE
1154 && (((*p
)->flags() & elfcpp::PF_W
)
1155 == (seg_flags
& elfcpp::PF_W
)))
1157 (*p
)->add_output_section(os
, seg_flags
, false);
1162 if (p
== this->segment_list_
.end())
1164 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_NOTE
,
1166 oseg
->add_output_section(os
, seg_flags
, false);
1170 // If we see a loadable SHF_TLS section, we create a PT_TLS
1171 // segment. There can only be one such segment.
1172 if ((flags
& elfcpp::SHF_TLS
) != 0)
1174 if (this->tls_segment_
== NULL
)
1175 this->make_output_segment(elfcpp::PT_TLS
, seg_flags
);
1176 this->tls_segment_
->add_output_section(os
, seg_flags
, false);
1179 // If -z relro is in effect, and we see a relro section, we create a
1180 // PT_GNU_RELRO segment. There can only be one such segment.
1181 if (os
->is_relro() && parameters
->options().relro())
1183 gold_assert(seg_flags
== (elfcpp::PF_R
| elfcpp::PF_W
));
1184 if (this->relro_segment_
== NULL
)
1185 this->make_output_segment(elfcpp::PT_GNU_RELRO
, seg_flags
);
1186 this->relro_segment_
->add_output_section(os
, seg_flags
, false);
1190 // Make an output section for a script.
1193 Layout::make_output_section_for_script(
1195 Script_sections::Section_type section_type
)
1197 name
= this->namepool_
.add(name
, false, NULL
);
1198 elfcpp::Elf_Xword sh_flags
= elfcpp::SHF_ALLOC
;
1199 if (section_type
== Script_sections::ST_NOLOAD
)
1201 Output_section
* os
= this->make_output_section(name
, elfcpp::SHT_PROGBITS
,
1203 false, false, false, false);
1204 os
->set_found_in_sections_clause();
1205 if (section_type
== Script_sections::ST_NOLOAD
)
1206 os
->set_is_noload();
1210 // Return the number of segments we expect to see.
1213 Layout::expected_segment_count() const
1215 size_t ret
= this->segment_list_
.size();
1217 // If we didn't see a SECTIONS clause in a linker script, we should
1218 // already have the complete list of segments. Otherwise we ask the
1219 // SECTIONS clause how many segments it expects, and add in the ones
1220 // we already have (PT_GNU_STACK, PT_GNU_EH_FRAME, etc.)
1222 if (!this->script_options_
->saw_sections_clause())
1226 const Script_sections
* ss
= this->script_options_
->script_sections();
1227 return ret
+ ss
->expected_segment_count(this);
1231 // Handle the .note.GNU-stack section at layout time. SEEN_GNU_STACK
1232 // is whether we saw a .note.GNU-stack section in the object file.
1233 // GNU_STACK_FLAGS is the section flags. The flags give the
1234 // protection required for stack memory. We record this in an
1235 // executable as a PT_GNU_STACK segment. If an object file does not
1236 // have a .note.GNU-stack segment, we must assume that it is an old
1237 // object. On some targets that will force an executable stack.
1240 Layout::layout_gnu_stack(bool seen_gnu_stack
, uint64_t gnu_stack_flags
)
1242 if (!seen_gnu_stack
)
1243 this->input_without_gnu_stack_note_
= true;
1246 this->input_with_gnu_stack_note_
= true;
1247 if ((gnu_stack_flags
& elfcpp::SHF_EXECINSTR
) != 0)
1248 this->input_requires_executable_stack_
= true;
1252 // Create automatic note sections.
1255 Layout::create_notes()
1257 this->create_gold_note();
1258 this->create_executable_stack_info();
1259 this->create_build_id();
1262 // Create the dynamic sections which are needed before we read the
1266 Layout::create_initial_dynamic_sections(Symbol_table
* symtab
)
1268 if (parameters
->doing_static_link())
1271 this->dynamic_section_
= this->choose_output_section(NULL
, ".dynamic",
1272 elfcpp::SHT_DYNAMIC
,
1274 | elfcpp::SHF_WRITE
),
1276 true, false, false);
1278 this->dynamic_symbol_
=
1279 symtab
->define_in_output_data("_DYNAMIC", NULL
, Symbol_table::PREDEFINED
,
1280 this->dynamic_section_
, 0, 0,
1281 elfcpp::STT_OBJECT
, elfcpp::STB_LOCAL
,
1282 elfcpp::STV_HIDDEN
, 0, false, false);
1284 this->dynamic_data_
= new Output_data_dynamic(&this->dynpool_
);
1286 this->dynamic_section_
->add_output_section_data(this->dynamic_data_
);
1289 // For each output section whose name can be represented as C symbol,
1290 // define __start and __stop symbols for the section. This is a GNU
1294 Layout::define_section_symbols(Symbol_table
* symtab
)
1296 for (Section_list::const_iterator p
= this->section_list_
.begin();
1297 p
!= this->section_list_
.end();
1300 const char* const name
= (*p
)->name();
1301 if (is_cident(name
))
1303 const std::string
name_string(name
);
1304 const std::string
start_name(cident_section_start_prefix
1306 const std::string
stop_name(cident_section_stop_prefix
1309 symtab
->define_in_output_data(start_name
.c_str(),
1311 Symbol_table::PREDEFINED
,
1317 elfcpp::STV_DEFAULT
,
1319 false, // offset_is_from_end
1320 true); // only_if_ref
1322 symtab
->define_in_output_data(stop_name
.c_str(),
1324 Symbol_table::PREDEFINED
,
1330 elfcpp::STV_DEFAULT
,
1332 true, // offset_is_from_end
1333 true); // only_if_ref
1338 // Define symbols for group signatures.
1341 Layout::define_group_signatures(Symbol_table
* symtab
)
1343 for (Group_signatures::iterator p
= this->group_signatures_
.begin();
1344 p
!= this->group_signatures_
.end();
1347 Symbol
* sym
= symtab
->lookup(p
->signature
, NULL
);
1349 p
->section
->set_info_symndx(sym
);
1352 // Force the name of the group section to the group
1353 // signature, and use the group's section symbol as the
1354 // signature symbol.
1355 if (strcmp(p
->section
->name(), p
->signature
) != 0)
1357 const char* name
= this->namepool_
.add(p
->signature
,
1359 p
->section
->set_name(name
);
1361 p
->section
->set_needs_symtab_index();
1362 p
->section
->set_info_section_symndx(p
->section
);
1366 this->group_signatures_
.clear();
1369 // Find the first read-only PT_LOAD segment, creating one if
1373 Layout::find_first_load_seg()
1375 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
1376 p
!= this->segment_list_
.end();
1379 if ((*p
)->type() == elfcpp::PT_LOAD
1380 && ((*p
)->flags() & elfcpp::PF_R
) != 0
1381 && (parameters
->options().omagic()
1382 || ((*p
)->flags() & elfcpp::PF_W
) == 0))
1386 gold_assert(!this->script_options_
->saw_phdrs_clause());
1388 Output_segment
* load_seg
= this->make_output_segment(elfcpp::PT_LOAD
,
1393 // Save states of all current output segments. Store saved states
1394 // in SEGMENT_STATES.
1397 Layout::save_segments(Segment_states
* segment_states
)
1399 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
1400 p
!= this->segment_list_
.end();
1403 Output_segment
* segment
= *p
;
1405 Output_segment
* copy
= new Output_segment(*segment
);
1406 (*segment_states
)[segment
] = copy
;
1410 // Restore states of output segments and delete any segment not found in
1414 Layout::restore_segments(const Segment_states
* segment_states
)
1416 // Go through the segment list and remove any segment added in the
1418 this->tls_segment_
= NULL
;
1419 this->relro_segment_
= NULL
;
1420 Segment_list::iterator list_iter
= this->segment_list_
.begin();
1421 while (list_iter
!= this->segment_list_
.end())
1423 Output_segment
* segment
= *list_iter
;
1424 Segment_states::const_iterator states_iter
=
1425 segment_states
->find(segment
);
1426 if (states_iter
!= segment_states
->end())
1428 const Output_segment
* copy
= states_iter
->second
;
1429 // Shallow copy to restore states.
1432 // Also fix up TLS and RELRO segment pointers as appropriate.
1433 if (segment
->type() == elfcpp::PT_TLS
)
1434 this->tls_segment_
= segment
;
1435 else if (segment
->type() == elfcpp::PT_GNU_RELRO
)
1436 this->relro_segment_
= segment
;
1442 list_iter
= this->segment_list_
.erase(list_iter
);
1443 // This is a segment created during section layout. It should be
1444 // safe to remove it since we should have removed all pointers to it.
1450 // Clean up after relaxation so that sections can be laid out again.
1453 Layout::clean_up_after_relaxation()
1455 // Restore the segments to point state just prior to the relaxation loop.
1456 Script_sections
* script_section
= this->script_options_
->script_sections();
1457 script_section
->release_segments();
1458 this->restore_segments(this->segment_states_
);
1460 // Reset section addresses and file offsets
1461 for (Section_list::iterator p
= this->section_list_
.begin();
1462 p
!= this->section_list_
.end();
1465 (*p
)->restore_states();
1467 // If an input section changes size because of relaxation,
1468 // we need to adjust the section offsets of all input sections.
1469 // after such a section.
1470 if ((*p
)->section_offsets_need_adjustment())
1471 (*p
)->adjust_section_offsets();
1473 (*p
)->reset_address_and_file_offset();
1476 // Reset special output object address and file offsets.
1477 for (Data_list::iterator p
= this->special_output_list_
.begin();
1478 p
!= this->special_output_list_
.end();
1480 (*p
)->reset_address_and_file_offset();
1482 // A linker script may have created some output section data objects.
1483 // They are useless now.
1484 for (Output_section_data_list::const_iterator p
=
1485 this->script_output_section_data_list_
.begin();
1486 p
!= this->script_output_section_data_list_
.end();
1489 this->script_output_section_data_list_
.clear();
1492 // Prepare for relaxation.
1495 Layout::prepare_for_relaxation()
1497 // Create an relaxation debug check if in debugging mode.
1498 if (is_debugging_enabled(DEBUG_RELAXATION
))
1499 this->relaxation_debug_check_
= new Relaxation_debug_check();
1501 // Save segment states.
1502 this->segment_states_
= new Segment_states();
1503 this->save_segments(this->segment_states_
);
1505 for(Section_list::const_iterator p
= this->section_list_
.begin();
1506 p
!= this->section_list_
.end();
1508 (*p
)->save_states();
1510 if (is_debugging_enabled(DEBUG_RELAXATION
))
1511 this->relaxation_debug_check_
->check_output_data_for_reset_values(
1512 this->section_list_
, this->special_output_list_
);
1514 // Also enable recording of output section data from scripts.
1515 this->record_output_section_data_from_script_
= true;
1518 // Relaxation loop body: If target has no relaxation, this runs only once
1519 // Otherwise, the target relaxation hook is called at the end of
1520 // each iteration. If the hook returns true, it means re-layout of
1521 // section is required.
1523 // The number of segments created by a linking script without a PHDRS
1524 // clause may be affected by section sizes and alignments. There is
1525 // a remote chance that relaxation causes different number of PT_LOAD
1526 // segments are created and sections are attached to different segments.
1527 // Therefore, we always throw away all segments created during section
1528 // layout. In order to be able to restart the section layout, we keep
1529 // a copy of the segment list right before the relaxation loop and use
1530 // that to restore the segments.
1532 // PASS is the current relaxation pass number.
1533 // SYMTAB is a symbol table.
1534 // PLOAD_SEG is the address of a pointer for the load segment.
1535 // PHDR_SEG is a pointer to the PHDR segment.
1536 // SEGMENT_HEADERS points to the output segment header.
1537 // FILE_HEADER points to the output file header.
1538 // PSHNDX is the address to store the output section index.
1541 Layout::relaxation_loop_body(
1544 Symbol_table
* symtab
,
1545 Output_segment
** pload_seg
,
1546 Output_segment
* phdr_seg
,
1547 Output_segment_headers
* segment_headers
,
1548 Output_file_header
* file_header
,
1549 unsigned int* pshndx
)
1551 // If this is not the first iteration, we need to clean up after
1552 // relaxation so that we can lay out the sections again.
1554 this->clean_up_after_relaxation();
1556 // If there is a SECTIONS clause, put all the input sections into
1557 // the required order.
1558 Output_segment
* load_seg
;
1559 if (this->script_options_
->saw_sections_clause())
1560 load_seg
= this->set_section_addresses_from_script(symtab
);
1561 else if (parameters
->options().relocatable())
1564 load_seg
= this->find_first_load_seg();
1566 if (parameters
->options().oformat_enum()
1567 != General_options::OBJECT_FORMAT_ELF
)
1570 // If the user set the address of the text segment, that may not be
1571 // compatible with putting the segment headers and file headers into
1573 if (parameters
->options().user_set_Ttext())
1576 gold_assert(phdr_seg
== NULL
1578 || this->script_options_
->saw_sections_clause());
1580 // If the address of the load segment we found has been set by
1581 // --section-start rather than by a script, then we don't want to
1582 // use it for the file and segment headers.
1583 if (load_seg
!= NULL
1584 && load_seg
->are_addresses_set()
1585 && !this->script_options_
->saw_sections_clause())
1588 // Lay out the segment headers.
1589 if (!parameters
->options().relocatable())
1591 gold_assert(segment_headers
!= NULL
);
1592 if (load_seg
!= NULL
)
1593 load_seg
->add_initial_output_data(segment_headers
);
1594 if (phdr_seg
!= NULL
)
1595 phdr_seg
->add_initial_output_data(segment_headers
);
1598 // Lay out the file header.
1599 if (load_seg
!= NULL
)
1600 load_seg
->add_initial_output_data(file_header
);
1602 if (this->script_options_
->saw_phdrs_clause()
1603 && !parameters
->options().relocatable())
1605 // Support use of FILEHDRS and PHDRS attachments in a PHDRS
1606 // clause in a linker script.
1607 Script_sections
* ss
= this->script_options_
->script_sections();
1608 ss
->put_headers_in_phdrs(file_header
, segment_headers
);
1611 // We set the output section indexes in set_segment_offsets and
1612 // set_section_indexes.
1615 // Set the file offsets of all the segments, and all the sections
1618 if (!parameters
->options().relocatable())
1619 off
= this->set_segment_offsets(target
, load_seg
, pshndx
);
1621 off
= this->set_relocatable_section_offsets(file_header
, pshndx
);
1623 // Verify that the dummy relaxation does not change anything.
1624 if (is_debugging_enabled(DEBUG_RELAXATION
))
1627 this->relaxation_debug_check_
->read_sections(this->section_list_
);
1629 this->relaxation_debug_check_
->verify_sections(this->section_list_
);
1632 *pload_seg
= load_seg
;
1636 // Finalize the layout. When this is called, we have created all the
1637 // output sections and all the output segments which are based on
1638 // input sections. We have several things to do, and we have to do
1639 // them in the right order, so that we get the right results correctly
1642 // 1) Finalize the list of output segments and create the segment
1645 // 2) Finalize the dynamic symbol table and associated sections.
1647 // 3) Determine the final file offset of all the output segments.
1649 // 4) Determine the final file offset of all the SHF_ALLOC output
1652 // 5) Create the symbol table sections and the section name table
1655 // 6) Finalize the symbol table: set symbol values to their final
1656 // value and make a final determination of which symbols are going
1657 // into the output symbol table.
1659 // 7) Create the section table header.
1661 // 8) Determine the final file offset of all the output sections which
1662 // are not SHF_ALLOC, including the section table header.
1664 // 9) Finalize the ELF file header.
1666 // This function returns the size of the output file.
1669 Layout::finalize(const Input_objects
* input_objects
, Symbol_table
* symtab
,
1670 Target
* target
, const Task
* task
)
1672 target
->finalize_sections(this, input_objects
, symtab
);
1674 this->count_local_symbols(task
, input_objects
);
1676 this->link_stabs_sections();
1678 Output_segment
* phdr_seg
= NULL
;
1679 if (!parameters
->options().relocatable() && !parameters
->doing_static_link())
1681 // There was a dynamic object in the link. We need to create
1682 // some information for the dynamic linker.
1684 // Create the PT_PHDR segment which will hold the program
1686 if (!this->script_options_
->saw_phdrs_clause())
1687 phdr_seg
= this->make_output_segment(elfcpp::PT_PHDR
, elfcpp::PF_R
);
1689 // Create the dynamic symbol table, including the hash table.
1690 Output_section
* dynstr
;
1691 std::vector
<Symbol
*> dynamic_symbols
;
1692 unsigned int local_dynamic_count
;
1693 Versions
versions(*this->script_options()->version_script_info(),
1695 this->create_dynamic_symtab(input_objects
, symtab
, &dynstr
,
1696 &local_dynamic_count
, &dynamic_symbols
,
1699 // Create the .interp section to hold the name of the
1700 // interpreter, and put it in a PT_INTERP segment.
1701 if (!parameters
->options().shared())
1702 this->create_interp(target
);
1704 // Finish the .dynamic section to hold the dynamic data, and put
1705 // it in a PT_DYNAMIC segment.
1706 this->finish_dynamic_section(input_objects
, symtab
);
1708 // We should have added everything we need to the dynamic string
1710 this->dynpool_
.set_string_offsets();
1712 // Create the version sections. We can't do this until the
1713 // dynamic string table is complete.
1714 this->create_version_sections(&versions
, symtab
, local_dynamic_count
,
1715 dynamic_symbols
, dynstr
);
1717 // Set the size of the _DYNAMIC symbol. We can't do this until
1718 // after we call create_version_sections.
1719 this->set_dynamic_symbol_size(symtab
);
1722 if (this->incremental_inputs_
)
1724 this->incremental_inputs_
->finalize();
1725 this->create_incremental_info_sections();
1728 // Create segment headers.
1729 Output_segment_headers
* segment_headers
=
1730 (parameters
->options().relocatable()
1732 : new Output_segment_headers(this->segment_list_
));
1734 // Lay out the file header.
1735 Output_file_header
* file_header
1736 = new Output_file_header(target
, symtab
, segment_headers
,
1737 parameters
->options().entry());
1739 this->special_output_list_
.push_back(file_header
);
1740 if (segment_headers
!= NULL
)
1741 this->special_output_list_
.push_back(segment_headers
);
1743 // Find approriate places for orphan output sections if we are using
1745 if (this->script_options_
->saw_sections_clause())
1746 this->place_orphan_sections_in_script();
1748 Output_segment
* load_seg
;
1753 // Take a snapshot of the section layout as needed.
1754 if (target
->may_relax())
1755 this->prepare_for_relaxation();
1757 // Run the relaxation loop to lay out sections.
1760 off
= this->relaxation_loop_body(pass
, target
, symtab
, &load_seg
,
1761 phdr_seg
, segment_headers
, file_header
,
1765 while (target
->may_relax()
1766 && target
->relax(pass
, input_objects
, symtab
, this));
1768 // Set the file offsets of all the non-data sections we've seen so
1769 // far which don't have to wait for the input sections. We need
1770 // this in order to finalize local symbols in non-allocated
1772 off
= this->set_section_offsets(off
, BEFORE_INPUT_SECTIONS_PASS
);
1774 // Set the section indexes of all unallocated sections seen so far,
1775 // in case any of them are somehow referenced by a symbol.
1776 shndx
= this->set_section_indexes(shndx
);
1778 // Create the symbol table sections.
1779 this->create_symtab_sections(input_objects
, symtab
, shndx
, &off
);
1780 if (!parameters
->doing_static_link())
1781 this->assign_local_dynsym_offsets(input_objects
);
1783 // Process any symbol assignments from a linker script. This must
1784 // be called after the symbol table has been finalized.
1785 this->script_options_
->finalize_symbols(symtab
, this);
1787 // Create the .shstrtab section.
1788 Output_section
* shstrtab_section
= this->create_shstrtab();
1790 // Set the file offsets of the rest of the non-data sections which
1791 // don't have to wait for the input sections.
1792 off
= this->set_section_offsets(off
, BEFORE_INPUT_SECTIONS_PASS
);
1794 // Now that all sections have been created, set the section indexes
1795 // for any sections which haven't been done yet.
1796 shndx
= this->set_section_indexes(shndx
);
1798 // Create the section table header.
1799 this->create_shdrs(shstrtab_section
, &off
);
1801 // If there are no sections which require postprocessing, we can
1802 // handle the section names now, and avoid a resize later.
1803 if (!this->any_postprocessing_sections_
)
1804 off
= this->set_section_offsets(off
,
1805 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
);
1807 file_header
->set_section_info(this->section_headers_
, shstrtab_section
);
1809 // Now we know exactly where everything goes in the output file
1810 // (except for non-allocated sections which require postprocessing).
1811 Output_data::layout_complete();
1813 this->output_file_size_
= off
;
1818 // Create a note header following the format defined in the ELF ABI.
1819 // NAME is the name, NOTE_TYPE is the type, SECTION_NAME is the name
1820 // of the section to create, DESCSZ is the size of the descriptor.
1821 // ALLOCATE is true if the section should be allocated in memory.
1822 // This returns the new note section. It sets *TRAILING_PADDING to
1823 // the number of trailing zero bytes required.
1826 Layout::create_note(const char* name
, int note_type
,
1827 const char* section_name
, size_t descsz
,
1828 bool allocate
, size_t* trailing_padding
)
1830 // Authorities all agree that the values in a .note field should
1831 // be aligned on 4-byte boundaries for 32-bit binaries. However,
1832 // they differ on what the alignment is for 64-bit binaries.
1833 // The GABI says unambiguously they take 8-byte alignment:
1834 // http://sco.com/developers/gabi/latest/ch5.pheader.html#note_section
1835 // Other documentation says alignment should always be 4 bytes:
1836 // http://www.netbsd.org/docs/kernel/elf-notes.html#note-format
1837 // GNU ld and GNU readelf both support the latter (at least as of
1838 // version 2.16.91), and glibc always generates the latter for
1839 // .note.ABI-tag (as of version 1.6), so that's the one we go with
1841 #ifdef GABI_FORMAT_FOR_DOTNOTE_SECTION // This is not defined by default.
1842 const int size
= parameters
->target().get_size();
1844 const int size
= 32;
1847 // The contents of the .note section.
1848 size_t namesz
= strlen(name
) + 1;
1849 size_t aligned_namesz
= align_address(namesz
, size
/ 8);
1850 size_t aligned_descsz
= align_address(descsz
, size
/ 8);
1852 size_t notehdrsz
= 3 * (size
/ 8) + aligned_namesz
;
1854 unsigned char* buffer
= new unsigned char[notehdrsz
];
1855 memset(buffer
, 0, notehdrsz
);
1857 bool is_big_endian
= parameters
->target().is_big_endian();
1863 elfcpp::Swap
<32, false>::writeval(buffer
, namesz
);
1864 elfcpp::Swap
<32, false>::writeval(buffer
+ 4, descsz
);
1865 elfcpp::Swap
<32, false>::writeval(buffer
+ 8, note_type
);
1869 elfcpp::Swap
<32, true>::writeval(buffer
, namesz
);
1870 elfcpp::Swap
<32, true>::writeval(buffer
+ 4, descsz
);
1871 elfcpp::Swap
<32, true>::writeval(buffer
+ 8, note_type
);
1874 else if (size
== 64)
1878 elfcpp::Swap
<64, false>::writeval(buffer
, namesz
);
1879 elfcpp::Swap
<64, false>::writeval(buffer
+ 8, descsz
);
1880 elfcpp::Swap
<64, false>::writeval(buffer
+ 16, note_type
);
1884 elfcpp::Swap
<64, true>::writeval(buffer
, namesz
);
1885 elfcpp::Swap
<64, true>::writeval(buffer
+ 8, descsz
);
1886 elfcpp::Swap
<64, true>::writeval(buffer
+ 16, note_type
);
1892 memcpy(buffer
+ 3 * (size
/ 8), name
, namesz
);
1894 elfcpp::Elf_Xword flags
= 0;
1896 flags
= elfcpp::SHF_ALLOC
;
1897 Output_section
* os
= this->choose_output_section(NULL
, section_name
,
1899 flags
, false, false,
1900 false, false, false, false);
1904 Output_section_data
* posd
= new Output_data_const_buffer(buffer
, notehdrsz
,
1907 os
->add_output_section_data(posd
);
1909 *trailing_padding
= aligned_descsz
- descsz
;
1914 // For an executable or shared library, create a note to record the
1915 // version of gold used to create the binary.
1918 Layout::create_gold_note()
1920 if (parameters
->options().relocatable())
1923 std::string desc
= std::string("gold ") + gold::get_version_string();
1925 size_t trailing_padding
;
1926 Output_section
*os
= this->create_note("GNU", elfcpp::NT_GNU_GOLD_VERSION
,
1927 ".note.gnu.gold-version", desc
.size(),
1928 false, &trailing_padding
);
1932 Output_section_data
* posd
= new Output_data_const(desc
, 4);
1933 os
->add_output_section_data(posd
);
1935 if (trailing_padding
> 0)
1937 posd
= new Output_data_zero_fill(trailing_padding
, 0);
1938 os
->add_output_section_data(posd
);
1942 // Record whether the stack should be executable. This can be set
1943 // from the command line using the -z execstack or -z noexecstack
1944 // options. Otherwise, if any input file has a .note.GNU-stack
1945 // section with the SHF_EXECINSTR flag set, the stack should be
1946 // executable. Otherwise, if at least one input file a
1947 // .note.GNU-stack section, and some input file has no .note.GNU-stack
1948 // section, we use the target default for whether the stack should be
1949 // executable. Otherwise, we don't generate a stack note. When
1950 // generating a object file, we create a .note.GNU-stack section with
1951 // the appropriate marking. When generating an executable or shared
1952 // library, we create a PT_GNU_STACK segment.
1955 Layout::create_executable_stack_info()
1957 bool is_stack_executable
;
1958 if (parameters
->options().is_execstack_set())
1959 is_stack_executable
= parameters
->options().is_stack_executable();
1960 else if (!this->input_with_gnu_stack_note_
)
1964 if (this->input_requires_executable_stack_
)
1965 is_stack_executable
= true;
1966 else if (this->input_without_gnu_stack_note_
)
1967 is_stack_executable
=
1968 parameters
->target().is_default_stack_executable();
1970 is_stack_executable
= false;
1973 if (parameters
->options().relocatable())
1975 const char* name
= this->namepool_
.add(".note.GNU-stack", false, NULL
);
1976 elfcpp::Elf_Xword flags
= 0;
1977 if (is_stack_executable
)
1978 flags
|= elfcpp::SHF_EXECINSTR
;
1979 this->make_output_section(name
, elfcpp::SHT_PROGBITS
, flags
, false,
1980 false, false, false, false);
1984 if (this->script_options_
->saw_phdrs_clause())
1986 int flags
= elfcpp::PF_R
| elfcpp::PF_W
;
1987 if (is_stack_executable
)
1988 flags
|= elfcpp::PF_X
;
1989 this->make_output_segment(elfcpp::PT_GNU_STACK
, flags
);
1993 // If --build-id was used, set up the build ID note.
1996 Layout::create_build_id()
1998 if (!parameters
->options().user_set_build_id())
2001 const char* style
= parameters
->options().build_id();
2002 if (strcmp(style
, "none") == 0)
2005 // Set DESCSZ to the size of the note descriptor. When possible,
2006 // set DESC to the note descriptor contents.
2009 if (strcmp(style
, "md5") == 0)
2011 else if (strcmp(style
, "sha1") == 0)
2013 else if (strcmp(style
, "uuid") == 0)
2015 const size_t uuidsz
= 128 / 8;
2017 char buffer
[uuidsz
];
2018 memset(buffer
, 0, uuidsz
);
2020 int descriptor
= open_descriptor(-1, "/dev/urandom", O_RDONLY
);
2022 gold_error(_("--build-id=uuid failed: could not open /dev/urandom: %s"),
2026 ssize_t got
= ::read(descriptor
, buffer
, uuidsz
);
2027 release_descriptor(descriptor
, true);
2029 gold_error(_("/dev/urandom: read failed: %s"), strerror(errno
));
2030 else if (static_cast<size_t>(got
) != uuidsz
)
2031 gold_error(_("/dev/urandom: expected %zu bytes, got %zd bytes"),
2035 desc
.assign(buffer
, uuidsz
);
2038 else if (strncmp(style
, "0x", 2) == 0)
2041 const char* p
= style
+ 2;
2044 if (hex_p(p
[0]) && hex_p(p
[1]))
2046 char c
= (hex_value(p
[0]) << 4) | hex_value(p
[1]);
2050 else if (*p
== '-' || *p
== ':')
2053 gold_fatal(_("--build-id argument '%s' not a valid hex number"),
2056 descsz
= desc
.size();
2059 gold_fatal(_("unrecognized --build-id argument '%s'"), style
);
2062 size_t trailing_padding
;
2063 Output_section
* os
= this->create_note("GNU", elfcpp::NT_GNU_BUILD_ID
,
2064 ".note.gnu.build-id", descsz
, true,
2071 // We know the value already, so we fill it in now.
2072 gold_assert(desc
.size() == descsz
);
2074 Output_section_data
* posd
= new Output_data_const(desc
, 4);
2075 os
->add_output_section_data(posd
);
2077 if (trailing_padding
!= 0)
2079 posd
= new Output_data_zero_fill(trailing_padding
, 0);
2080 os
->add_output_section_data(posd
);
2085 // We need to compute a checksum after we have completed the
2087 gold_assert(trailing_padding
== 0);
2088 this->build_id_note_
= new Output_data_zero_fill(descsz
, 4);
2089 os
->add_output_section_data(this->build_id_note_
);
2093 // If we have both .stabXX and .stabXXstr sections, then the sh_link
2094 // field of the former should point to the latter. I'm not sure who
2095 // started this, but the GNU linker does it, and some tools depend
2099 Layout::link_stabs_sections()
2101 if (!this->have_stabstr_section_
)
2104 for (Section_list::iterator p
= this->section_list_
.begin();
2105 p
!= this->section_list_
.end();
2108 if ((*p
)->type() != elfcpp::SHT_STRTAB
)
2111 const char* name
= (*p
)->name();
2112 if (strncmp(name
, ".stab", 5) != 0)
2115 size_t len
= strlen(name
);
2116 if (strcmp(name
+ len
- 3, "str") != 0)
2119 std::string
stab_name(name
, len
- 3);
2120 Output_section
* stab_sec
;
2121 stab_sec
= this->find_output_section(stab_name
.c_str());
2122 if (stab_sec
!= NULL
)
2123 stab_sec
->set_link_section(*p
);
2127 // Create .gnu_incremental_inputs and .gnu_incremental_strtab sections needed
2128 // for the next run of incremental linking to check what has changed.
2131 Layout::create_incremental_info_sections()
2133 gold_assert(this->incremental_inputs_
!= NULL
);
2135 // Add the .gnu_incremental_inputs section.
2136 const char *incremental_inputs_name
=
2137 this->namepool_
.add(".gnu_incremental_inputs", false, NULL
);
2138 Output_section
* inputs_os
=
2139 this->make_output_section(incremental_inputs_name
,
2140 elfcpp::SHT_GNU_INCREMENTAL_INPUTS
, 0,
2141 false, false, false, false, false);
2142 Output_section_data
* posd
=
2143 this->incremental_inputs_
->create_incremental_inputs_section_data();
2144 inputs_os
->add_output_section_data(posd
);
2146 // Add the .gnu_incremental_strtab section.
2147 const char *incremental_strtab_name
=
2148 this->namepool_
.add(".gnu_incremental_strtab", false, NULL
);
2149 Output_section
* strtab_os
= this->make_output_section(incremental_strtab_name
,
2152 false, false, false);
2153 Output_data_strtab
* strtab_data
=
2154 new Output_data_strtab(this->incremental_inputs_
->get_stringpool());
2155 strtab_os
->add_output_section_data(strtab_data
);
2157 inputs_os
->set_link_section(strtab_data
);
2160 // Return whether SEG1 should be before SEG2 in the output file. This
2161 // is based entirely on the segment type and flags. When this is
2162 // called the segment addresses has normally not yet been set.
2165 Layout::segment_precedes(const Output_segment
* seg1
,
2166 const Output_segment
* seg2
)
2168 elfcpp::Elf_Word type1
= seg1
->type();
2169 elfcpp::Elf_Word type2
= seg2
->type();
2171 // The single PT_PHDR segment is required to precede any loadable
2172 // segment. We simply make it always first.
2173 if (type1
== elfcpp::PT_PHDR
)
2175 gold_assert(type2
!= elfcpp::PT_PHDR
);
2178 if (type2
== elfcpp::PT_PHDR
)
2181 // The single PT_INTERP segment is required to precede any loadable
2182 // segment. We simply make it always second.
2183 if (type1
== elfcpp::PT_INTERP
)
2185 gold_assert(type2
!= elfcpp::PT_INTERP
);
2188 if (type2
== elfcpp::PT_INTERP
)
2191 // We then put PT_LOAD segments before any other segments.
2192 if (type1
== elfcpp::PT_LOAD
&& type2
!= elfcpp::PT_LOAD
)
2194 if (type2
== elfcpp::PT_LOAD
&& type1
!= elfcpp::PT_LOAD
)
2197 // We put the PT_TLS segment last except for the PT_GNU_RELRO
2198 // segment, because that is where the dynamic linker expects to find
2199 // it (this is just for efficiency; other positions would also work
2201 if (type1
== elfcpp::PT_TLS
2202 && type2
!= elfcpp::PT_TLS
2203 && type2
!= elfcpp::PT_GNU_RELRO
)
2205 if (type2
== elfcpp::PT_TLS
2206 && type1
!= elfcpp::PT_TLS
2207 && type1
!= elfcpp::PT_GNU_RELRO
)
2210 // We put the PT_GNU_RELRO segment last, because that is where the
2211 // dynamic linker expects to find it (as with PT_TLS, this is just
2213 if (type1
== elfcpp::PT_GNU_RELRO
&& type2
!= elfcpp::PT_GNU_RELRO
)
2215 if (type2
== elfcpp::PT_GNU_RELRO
&& type1
!= elfcpp::PT_GNU_RELRO
)
2218 const elfcpp::Elf_Word flags1
= seg1
->flags();
2219 const elfcpp::Elf_Word flags2
= seg2
->flags();
2221 // The order of non-PT_LOAD segments is unimportant. We simply sort
2222 // by the numeric segment type and flags values. There should not
2223 // be more than one segment with the same type and flags.
2224 if (type1
!= elfcpp::PT_LOAD
)
2227 return type1
< type2
;
2228 gold_assert(flags1
!= flags2
);
2229 return flags1
< flags2
;
2232 // If the addresses are set already, sort by load address.
2233 if (seg1
->are_addresses_set())
2235 if (!seg2
->are_addresses_set())
2238 unsigned int section_count1
= seg1
->output_section_count();
2239 unsigned int section_count2
= seg2
->output_section_count();
2240 if (section_count1
== 0 && section_count2
> 0)
2242 if (section_count1
> 0 && section_count2
== 0)
2245 uint64_t paddr1
= seg1
->first_section_load_address();
2246 uint64_t paddr2
= seg2
->first_section_load_address();
2247 if (paddr1
!= paddr2
)
2248 return paddr1
< paddr2
;
2250 else if (seg2
->are_addresses_set())
2253 // A segment which holds large data comes after a segment which does
2254 // not hold large data.
2255 if (seg1
->is_large_data_segment())
2257 if (!seg2
->is_large_data_segment())
2260 else if (seg2
->is_large_data_segment())
2263 // Otherwise, we sort PT_LOAD segments based on the flags. Readonly
2264 // segments come before writable segments. Then writable segments
2265 // with data come before writable segments without data. Then
2266 // executable segments come before non-executable segments. Then
2267 // the unlikely case of a non-readable segment comes before the
2268 // normal case of a readable segment. If there are multiple
2269 // segments with the same type and flags, we require that the
2270 // address be set, and we sort by virtual address and then physical
2272 if ((flags1
& elfcpp::PF_W
) != (flags2
& elfcpp::PF_W
))
2273 return (flags1
& elfcpp::PF_W
) == 0;
2274 if ((flags1
& elfcpp::PF_W
) != 0
2275 && seg1
->has_any_data_sections() != seg2
->has_any_data_sections())
2276 return seg1
->has_any_data_sections();
2277 if ((flags1
& elfcpp::PF_X
) != (flags2
& elfcpp::PF_X
))
2278 return (flags1
& elfcpp::PF_X
) != 0;
2279 if ((flags1
& elfcpp::PF_R
) != (flags2
& elfcpp::PF_R
))
2280 return (flags1
& elfcpp::PF_R
) == 0;
2282 // We shouldn't get here--we shouldn't create segments which we
2283 // can't distinguish.
2287 // Increase OFF so that it is congruent to ADDR modulo ABI_PAGESIZE.
2290 align_file_offset(off_t off
, uint64_t addr
, uint64_t abi_pagesize
)
2292 uint64_t unsigned_off
= off
;
2293 uint64_t aligned_off
= ((unsigned_off
& ~(abi_pagesize
- 1))
2294 | (addr
& (abi_pagesize
- 1)));
2295 if (aligned_off
< unsigned_off
)
2296 aligned_off
+= abi_pagesize
;
2300 // Set the file offsets of all the segments, and all the sections they
2301 // contain. They have all been created. LOAD_SEG must be be laid out
2302 // first. Return the offset of the data to follow.
2305 Layout::set_segment_offsets(const Target
* target
, Output_segment
* load_seg
,
2306 unsigned int *pshndx
)
2308 // Sort them into the final order.
2309 std::sort(this->segment_list_
.begin(), this->segment_list_
.end(),
2310 Layout::Compare_segments());
2312 // Find the PT_LOAD segments, and set their addresses and offsets
2313 // and their section's addresses and offsets.
2315 if (parameters
->options().user_set_Ttext())
2316 addr
= parameters
->options().Ttext();
2317 else if (parameters
->options().output_is_position_independent())
2320 addr
= target
->default_text_segment_address();
2323 // If LOAD_SEG is NULL, then the file header and segment headers
2324 // will not be loadable. But they still need to be at offset 0 in
2325 // the file. Set their offsets now.
2326 if (load_seg
== NULL
)
2328 for (Data_list::iterator p
= this->special_output_list_
.begin();
2329 p
!= this->special_output_list_
.end();
2332 off
= align_address(off
, (*p
)->addralign());
2333 (*p
)->set_address_and_file_offset(0, off
);
2334 off
+= (*p
)->data_size();
2338 unsigned int increase_relro
= this->increase_relro_
;
2339 if (this->script_options_
->saw_sections_clause())
2342 const bool check_sections
= parameters
->options().check_sections();
2343 Output_segment
* last_load_segment
= NULL
;
2345 bool was_readonly
= false;
2346 for (Segment_list::iterator p
= this->segment_list_
.begin();
2347 p
!= this->segment_list_
.end();
2350 if ((*p
)->type() == elfcpp::PT_LOAD
)
2352 if (load_seg
!= NULL
&& load_seg
!= *p
)
2356 bool are_addresses_set
= (*p
)->are_addresses_set();
2357 if (are_addresses_set
)
2359 // When it comes to setting file offsets, we care about
2360 // the physical address.
2361 addr
= (*p
)->paddr();
2363 else if (parameters
->options().user_set_Tdata()
2364 && ((*p
)->flags() & elfcpp::PF_W
) != 0
2365 && (!parameters
->options().user_set_Tbss()
2366 || (*p
)->has_any_data_sections()))
2368 addr
= parameters
->options().Tdata();
2369 are_addresses_set
= true;
2371 else if (parameters
->options().user_set_Tbss()
2372 && ((*p
)->flags() & elfcpp::PF_W
) != 0
2373 && !(*p
)->has_any_data_sections())
2375 addr
= parameters
->options().Tbss();
2376 are_addresses_set
= true;
2379 uint64_t orig_addr
= addr
;
2380 uint64_t orig_off
= off
;
2382 uint64_t aligned_addr
= 0;
2383 uint64_t abi_pagesize
= target
->abi_pagesize();
2384 uint64_t common_pagesize
= target
->common_pagesize();
2386 if (!parameters
->options().nmagic()
2387 && !parameters
->options().omagic())
2388 (*p
)->set_minimum_p_align(common_pagesize
);
2390 if (!are_addresses_set
)
2392 // If the last segment was readonly, and this one is
2393 // not, then skip the address forward one page,
2394 // maintaining the same position within the page. This
2395 // lets us store both segments overlapping on a single
2396 // page in the file, but the loader will put them on
2397 // different pages in memory.
2399 addr
= align_address(addr
, (*p
)->maximum_alignment());
2400 aligned_addr
= addr
;
2402 if (was_readonly
&& ((*p
)->flags() & elfcpp::PF_W
) != 0)
2404 if ((addr
& (abi_pagesize
- 1)) != 0)
2405 addr
= addr
+ abi_pagesize
;
2408 off
= orig_off
+ ((addr
- orig_addr
) & (abi_pagesize
- 1));
2411 if (!parameters
->options().nmagic()
2412 && !parameters
->options().omagic())
2413 off
= align_file_offset(off
, addr
, abi_pagesize
);
2414 else if (load_seg
== NULL
)
2416 // This is -N or -n with a section script which prevents
2417 // us from using a load segment. We need to ensure that
2418 // the file offset is aligned to the alignment of the
2419 // segment. This is because the linker script
2420 // implicitly assumed a zero offset. If we don't align
2421 // here, then the alignment of the sections in the
2422 // linker script may not match the alignment of the
2423 // sections in the set_section_addresses call below,
2424 // causing an error about dot moving backward.
2425 off
= align_address(off
, (*p
)->maximum_alignment());
2428 unsigned int shndx_hold
= *pshndx
;
2429 uint64_t new_addr
= (*p
)->set_section_addresses(this, false, addr
,
2433 // Now that we know the size of this segment, we may be able
2434 // to save a page in memory, at the cost of wasting some
2435 // file space, by instead aligning to the start of a new
2436 // page. Here we use the real machine page size rather than
2437 // the ABI mandated page size.
2439 if (!are_addresses_set
&& aligned_addr
!= addr
)
2441 uint64_t first_off
= (common_pagesize
2443 & (common_pagesize
- 1)));
2444 uint64_t last_off
= new_addr
& (common_pagesize
- 1);
2447 && ((aligned_addr
& ~ (common_pagesize
- 1))
2448 != (new_addr
& ~ (common_pagesize
- 1)))
2449 && first_off
+ last_off
<= common_pagesize
)
2451 *pshndx
= shndx_hold
;
2452 addr
= align_address(aligned_addr
, common_pagesize
);
2453 addr
= align_address(addr
, (*p
)->maximum_alignment());
2454 off
= orig_off
+ ((addr
- orig_addr
) & (abi_pagesize
- 1));
2455 off
= align_file_offset(off
, addr
, abi_pagesize
);
2456 new_addr
= (*p
)->set_section_addresses(this, true, addr
,
2464 if (((*p
)->flags() & elfcpp::PF_W
) == 0)
2465 was_readonly
= true;
2467 // Implement --check-sections. We know that the segments
2468 // are sorted by LMA.
2469 if (check_sections
&& last_load_segment
!= NULL
)
2471 gold_assert(last_load_segment
->paddr() <= (*p
)->paddr());
2472 if (last_load_segment
->paddr() + last_load_segment
->memsz()
2475 unsigned long long lb1
= last_load_segment
->paddr();
2476 unsigned long long le1
= lb1
+ last_load_segment
->memsz();
2477 unsigned long long lb2
= (*p
)->paddr();
2478 unsigned long long le2
= lb2
+ (*p
)->memsz();
2479 gold_error(_("load segment overlap [0x%llx -> 0x%llx] and "
2480 "[0x%llx -> 0x%llx]"),
2481 lb1
, le1
, lb2
, le2
);
2484 last_load_segment
= *p
;
2488 // Handle the non-PT_LOAD segments, setting their offsets from their
2489 // section's offsets.
2490 for (Segment_list::iterator p
= this->segment_list_
.begin();
2491 p
!= this->segment_list_
.end();
2494 if ((*p
)->type() != elfcpp::PT_LOAD
)
2495 (*p
)->set_offset((*p
)->type() == elfcpp::PT_GNU_RELRO
2500 // Set the TLS offsets for each section in the PT_TLS segment.
2501 if (this->tls_segment_
!= NULL
)
2502 this->tls_segment_
->set_tls_offsets();
2507 // Set the offsets of all the allocated sections when doing a
2508 // relocatable link. This does the same jobs as set_segment_offsets,
2509 // only for a relocatable link.
2512 Layout::set_relocatable_section_offsets(Output_data
* file_header
,
2513 unsigned int *pshndx
)
2517 file_header
->set_address_and_file_offset(0, 0);
2518 off
+= file_header
->data_size();
2520 for (Section_list::iterator p
= this->section_list_
.begin();
2521 p
!= this->section_list_
.end();
2524 // We skip unallocated sections here, except that group sections
2525 // have to come first.
2526 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) == 0
2527 && (*p
)->type() != elfcpp::SHT_GROUP
)
2530 off
= align_address(off
, (*p
)->addralign());
2532 // The linker script might have set the address.
2533 if (!(*p
)->is_address_valid())
2534 (*p
)->set_address(0);
2535 (*p
)->set_file_offset(off
);
2536 (*p
)->finalize_data_size();
2537 off
+= (*p
)->data_size();
2539 (*p
)->set_out_shndx(*pshndx
);
2546 // Set the file offset of all the sections not associated with a
2550 Layout::set_section_offsets(off_t off
, Layout::Section_offset_pass pass
)
2552 for (Section_list::iterator p
= this->unattached_section_list_
.begin();
2553 p
!= this->unattached_section_list_
.end();
2556 // The symtab section is handled in create_symtab_sections.
2557 if (*p
== this->symtab_section_
)
2560 // If we've already set the data size, don't set it again.
2561 if ((*p
)->is_offset_valid() && (*p
)->is_data_size_valid())
2564 if (pass
== BEFORE_INPUT_SECTIONS_PASS
2565 && (*p
)->requires_postprocessing())
2567 (*p
)->create_postprocessing_buffer();
2568 this->any_postprocessing_sections_
= true;
2571 if (pass
== BEFORE_INPUT_SECTIONS_PASS
2572 && (*p
)->after_input_sections())
2574 else if (pass
== POSTPROCESSING_SECTIONS_PASS
2575 && (!(*p
)->after_input_sections()
2576 || (*p
)->type() == elfcpp::SHT_STRTAB
))
2578 else if (pass
== STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
2579 && (!(*p
)->after_input_sections()
2580 || (*p
)->type() != elfcpp::SHT_STRTAB
))
2583 off
= align_address(off
, (*p
)->addralign());
2584 (*p
)->set_file_offset(off
);
2585 (*p
)->finalize_data_size();
2586 off
+= (*p
)->data_size();
2588 // At this point the name must be set.
2589 if (pass
!= STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
)
2590 this->namepool_
.add((*p
)->name(), false, NULL
);
2595 // Set the section indexes of all the sections not associated with a
2599 Layout::set_section_indexes(unsigned int shndx
)
2601 for (Section_list::iterator p
= this->unattached_section_list_
.begin();
2602 p
!= this->unattached_section_list_
.end();
2605 if (!(*p
)->has_out_shndx())
2607 (*p
)->set_out_shndx(shndx
);
2614 // Set the section addresses according to the linker script. This is
2615 // only called when we see a SECTIONS clause. This returns the
2616 // program segment which should hold the file header and segment
2617 // headers, if any. It will return NULL if they should not be in a
2621 Layout::set_section_addresses_from_script(Symbol_table
* symtab
)
2623 Script_sections
* ss
= this->script_options_
->script_sections();
2624 gold_assert(ss
->saw_sections_clause());
2625 return this->script_options_
->set_section_addresses(symtab
, this);
2628 // Place the orphan sections in the linker script.
2631 Layout::place_orphan_sections_in_script()
2633 Script_sections
* ss
= this->script_options_
->script_sections();
2634 gold_assert(ss
->saw_sections_clause());
2636 // Place each orphaned output section in the script.
2637 for (Section_list::iterator p
= this->section_list_
.begin();
2638 p
!= this->section_list_
.end();
2641 if (!(*p
)->found_in_sections_clause())
2642 ss
->place_orphan(*p
);
2646 // Count the local symbols in the regular symbol table and the dynamic
2647 // symbol table, and build the respective string pools.
2650 Layout::count_local_symbols(const Task
* task
,
2651 const Input_objects
* input_objects
)
2653 // First, figure out an upper bound on the number of symbols we'll
2654 // be inserting into each pool. This helps us create the pools with
2655 // the right size, to avoid unnecessary hashtable resizing.
2656 unsigned int symbol_count
= 0;
2657 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2658 p
!= input_objects
->relobj_end();
2660 symbol_count
+= (*p
)->local_symbol_count();
2662 // Go from "upper bound" to "estimate." We overcount for two
2663 // reasons: we double-count symbols that occur in more than one
2664 // object file, and we count symbols that are dropped from the
2665 // output. Add it all together and assume we overcount by 100%.
2668 // We assume all symbols will go into both the sympool and dynpool.
2669 this->sympool_
.reserve(symbol_count
);
2670 this->dynpool_
.reserve(symbol_count
);
2672 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2673 p
!= input_objects
->relobj_end();
2676 Task_lock_obj
<Object
> tlo(task
, *p
);
2677 (*p
)->count_local_symbols(&this->sympool_
, &this->dynpool_
);
2681 // Create the symbol table sections. Here we also set the final
2682 // values of the symbols. At this point all the loadable sections are
2683 // fully laid out. SHNUM is the number of sections so far.
2686 Layout::create_symtab_sections(const Input_objects
* input_objects
,
2687 Symbol_table
* symtab
,
2693 if (parameters
->target().get_size() == 32)
2695 symsize
= elfcpp::Elf_sizes
<32>::sym_size
;
2698 else if (parameters
->target().get_size() == 64)
2700 symsize
= elfcpp::Elf_sizes
<64>::sym_size
;
2707 off
= align_address(off
, align
);
2708 off_t startoff
= off
;
2710 // Save space for the dummy symbol at the start of the section. We
2711 // never bother to write this out--it will just be left as zero.
2713 unsigned int local_symbol_index
= 1;
2715 // Add STT_SECTION symbols for each Output section which needs one.
2716 for (Section_list::iterator p
= this->section_list_
.begin();
2717 p
!= this->section_list_
.end();
2720 if (!(*p
)->needs_symtab_index())
2721 (*p
)->set_symtab_index(-1U);
2724 (*p
)->set_symtab_index(local_symbol_index
);
2725 ++local_symbol_index
;
2730 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2731 p
!= input_objects
->relobj_end();
2734 unsigned int index
= (*p
)->finalize_local_symbols(local_symbol_index
,
2736 off
+= (index
- local_symbol_index
) * symsize
;
2737 local_symbol_index
= index
;
2740 unsigned int local_symcount
= local_symbol_index
;
2741 gold_assert(static_cast<off_t
>(local_symcount
* symsize
) == off
- startoff
);
2744 size_t dyn_global_index
;
2746 if (this->dynsym_section_
== NULL
)
2749 dyn_global_index
= 0;
2754 dyn_global_index
= this->dynsym_section_
->info();
2755 off_t locsize
= dyn_global_index
* this->dynsym_section_
->entsize();
2756 dynoff
= this->dynsym_section_
->offset() + locsize
;
2757 dyncount
= (this->dynsym_section_
->data_size() - locsize
) / symsize
;
2758 gold_assert(static_cast<off_t
>(dyncount
* symsize
)
2759 == this->dynsym_section_
->data_size() - locsize
);
2762 off
= symtab
->finalize(off
, dynoff
, dyn_global_index
, dyncount
,
2763 &this->sympool_
, &local_symcount
);
2765 if (!parameters
->options().strip_all())
2767 this->sympool_
.set_string_offsets();
2769 const char* symtab_name
= this->namepool_
.add(".symtab", false, NULL
);
2770 Output_section
* osymtab
= this->make_output_section(symtab_name
,
2773 false, false, false);
2774 this->symtab_section_
= osymtab
;
2776 Output_section_data
* pos
= new Output_data_fixed_space(off
- startoff
,
2779 osymtab
->add_output_section_data(pos
);
2781 // We generate a .symtab_shndx section if we have more than
2782 // SHN_LORESERVE sections. Technically it is possible that we
2783 // don't need one, because it is possible that there are no
2784 // symbols in any of sections with indexes larger than
2785 // SHN_LORESERVE. That is probably unusual, though, and it is
2786 // easier to always create one than to compute section indexes
2787 // twice (once here, once when writing out the symbols).
2788 if (shnum
>= elfcpp::SHN_LORESERVE
)
2790 const char* symtab_xindex_name
= this->namepool_
.add(".symtab_shndx",
2792 Output_section
* osymtab_xindex
=
2793 this->make_output_section(symtab_xindex_name
,
2794 elfcpp::SHT_SYMTAB_SHNDX
, 0, false,
2795 false, false, false, false);
2797 size_t symcount
= (off
- startoff
) / symsize
;
2798 this->symtab_xindex_
= new Output_symtab_xindex(symcount
);
2800 osymtab_xindex
->add_output_section_data(this->symtab_xindex_
);
2802 osymtab_xindex
->set_link_section(osymtab
);
2803 osymtab_xindex
->set_addralign(4);
2804 osymtab_xindex
->set_entsize(4);
2806 osymtab_xindex
->set_after_input_sections();
2808 // This tells the driver code to wait until the symbol table
2809 // has written out before writing out the postprocessing
2810 // sections, including the .symtab_shndx section.
2811 this->any_postprocessing_sections_
= true;
2814 const char* strtab_name
= this->namepool_
.add(".strtab", false, NULL
);
2815 Output_section
* ostrtab
= this->make_output_section(strtab_name
,
2818 false, false, false);
2820 Output_section_data
* pstr
= new Output_data_strtab(&this->sympool_
);
2821 ostrtab
->add_output_section_data(pstr
);
2823 osymtab
->set_file_offset(startoff
);
2824 osymtab
->finalize_data_size();
2825 osymtab
->set_link_section(ostrtab
);
2826 osymtab
->set_info(local_symcount
);
2827 osymtab
->set_entsize(symsize
);
2833 // Create the .shstrtab section, which holds the names of the
2834 // sections. At the time this is called, we have created all the
2835 // output sections except .shstrtab itself.
2838 Layout::create_shstrtab()
2840 // FIXME: We don't need to create a .shstrtab section if we are
2841 // stripping everything.
2843 const char* name
= this->namepool_
.add(".shstrtab", false, NULL
);
2845 Output_section
* os
= this->make_output_section(name
, elfcpp::SHT_STRTAB
, 0,
2846 false, false, false, false,
2849 if (strcmp(parameters
->options().compress_debug_sections(), "none") != 0)
2851 // We can't write out this section until we've set all the
2852 // section names, and we don't set the names of compressed
2853 // output sections until relocations are complete. FIXME: With
2854 // the current names we use, this is unnecessary.
2855 os
->set_after_input_sections();
2858 Output_section_data
* posd
= new Output_data_strtab(&this->namepool_
);
2859 os
->add_output_section_data(posd
);
2864 // Create the section headers. SIZE is 32 or 64. OFF is the file
2868 Layout::create_shdrs(const Output_section
* shstrtab_section
, off_t
* poff
)
2870 Output_section_headers
* oshdrs
;
2871 oshdrs
= new Output_section_headers(this,
2872 &this->segment_list_
,
2873 &this->section_list_
,
2874 &this->unattached_section_list_
,
2877 off_t off
= align_address(*poff
, oshdrs
->addralign());
2878 oshdrs
->set_address_and_file_offset(0, off
);
2879 off
+= oshdrs
->data_size();
2881 this->section_headers_
= oshdrs
;
2884 // Count the allocated sections.
2887 Layout::allocated_output_section_count() const
2889 size_t section_count
= 0;
2890 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
2891 p
!= this->segment_list_
.end();
2893 section_count
+= (*p
)->output_section_count();
2894 return section_count
;
2897 // Create the dynamic symbol table.
2900 Layout::create_dynamic_symtab(const Input_objects
* input_objects
,
2901 Symbol_table
* symtab
,
2902 Output_section
**pdynstr
,
2903 unsigned int* plocal_dynamic_count
,
2904 std::vector
<Symbol
*>* pdynamic_symbols
,
2905 Versions
* pversions
)
2907 // Count all the symbols in the dynamic symbol table, and set the
2908 // dynamic symbol indexes.
2910 // Skip symbol 0, which is always all zeroes.
2911 unsigned int index
= 1;
2913 // Add STT_SECTION symbols for each Output section which needs one.
2914 for (Section_list::iterator p
= this->section_list_
.begin();
2915 p
!= this->section_list_
.end();
2918 if (!(*p
)->needs_dynsym_index())
2919 (*p
)->set_dynsym_index(-1U);
2922 (*p
)->set_dynsym_index(index
);
2927 // Count the local symbols that need to go in the dynamic symbol table,
2928 // and set the dynamic symbol indexes.
2929 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2930 p
!= input_objects
->relobj_end();
2933 unsigned int new_index
= (*p
)->set_local_dynsym_indexes(index
);
2937 unsigned int local_symcount
= index
;
2938 *plocal_dynamic_count
= local_symcount
;
2940 index
= symtab
->set_dynsym_indexes(index
, pdynamic_symbols
,
2941 &this->dynpool_
, pversions
);
2945 const int size
= parameters
->target().get_size();
2948 symsize
= elfcpp::Elf_sizes
<32>::sym_size
;
2951 else if (size
== 64)
2953 symsize
= elfcpp::Elf_sizes
<64>::sym_size
;
2959 // Create the dynamic symbol table section.
2961 Output_section
* dynsym
= this->choose_output_section(NULL
, ".dynsym",
2965 false, false, false);
2967 Output_section_data
* odata
= new Output_data_fixed_space(index
* symsize
,
2970 dynsym
->add_output_section_data(odata
);
2972 dynsym
->set_info(local_symcount
);
2973 dynsym
->set_entsize(symsize
);
2974 dynsym
->set_addralign(align
);
2976 this->dynsym_section_
= dynsym
;
2978 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
2979 odyn
->add_section_address(elfcpp::DT_SYMTAB
, dynsym
);
2980 odyn
->add_constant(elfcpp::DT_SYMENT
, symsize
);
2982 // If there are more than SHN_LORESERVE allocated sections, we
2983 // create a .dynsym_shndx section. It is possible that we don't
2984 // need one, because it is possible that there are no dynamic
2985 // symbols in any of the sections with indexes larger than
2986 // SHN_LORESERVE. This is probably unusual, though, and at this
2987 // time we don't know the actual section indexes so it is
2988 // inconvenient to check.
2989 if (this->allocated_output_section_count() >= elfcpp::SHN_LORESERVE
)
2991 Output_section
* dynsym_xindex
=
2992 this->choose_output_section(NULL
, ".dynsym_shndx",
2993 elfcpp::SHT_SYMTAB_SHNDX
,
2995 false, false, true, false, false, false);
2997 this->dynsym_xindex_
= new Output_symtab_xindex(index
);
2999 dynsym_xindex
->add_output_section_data(this->dynsym_xindex_
);
3001 dynsym_xindex
->set_link_section(dynsym
);
3002 dynsym_xindex
->set_addralign(4);
3003 dynsym_xindex
->set_entsize(4);
3005 dynsym_xindex
->set_after_input_sections();
3007 // This tells the driver code to wait until the symbol table has
3008 // written out before writing out the postprocessing sections,
3009 // including the .dynsym_shndx section.
3010 this->any_postprocessing_sections_
= true;
3013 // Create the dynamic string table section.
3015 Output_section
* dynstr
= this->choose_output_section(NULL
, ".dynstr",
3019 false, false, false);
3021 Output_section_data
* strdata
= new Output_data_strtab(&this->dynpool_
);
3022 dynstr
->add_output_section_data(strdata
);
3024 dynsym
->set_link_section(dynstr
);
3025 this->dynamic_section_
->set_link_section(dynstr
);
3027 odyn
->add_section_address(elfcpp::DT_STRTAB
, dynstr
);
3028 odyn
->add_section_size(elfcpp::DT_STRSZ
, dynstr
);
3032 // Create the hash tables.
3034 if (strcmp(parameters
->options().hash_style(), "sysv") == 0
3035 || strcmp(parameters
->options().hash_style(), "both") == 0)
3037 unsigned char* phash
;
3038 unsigned int hashlen
;
3039 Dynobj::create_elf_hash_table(*pdynamic_symbols
, local_symcount
,
3042 Output_section
* hashsec
= this->choose_output_section(NULL
, ".hash",
3049 Output_section_data
* hashdata
= new Output_data_const_buffer(phash
,
3053 hashsec
->add_output_section_data(hashdata
);
3055 hashsec
->set_link_section(dynsym
);
3056 hashsec
->set_entsize(4);
3058 odyn
->add_section_address(elfcpp::DT_HASH
, hashsec
);
3061 if (strcmp(parameters
->options().hash_style(), "gnu") == 0
3062 || strcmp(parameters
->options().hash_style(), "both") == 0)
3064 unsigned char* phash
;
3065 unsigned int hashlen
;
3066 Dynobj::create_gnu_hash_table(*pdynamic_symbols
, local_symcount
,
3069 Output_section
* hashsec
= this->choose_output_section(NULL
, ".gnu.hash",
3070 elfcpp::SHT_GNU_HASH
,
3076 Output_section_data
* hashdata
= new Output_data_const_buffer(phash
,
3080 hashsec
->add_output_section_data(hashdata
);
3082 hashsec
->set_link_section(dynsym
);
3084 // For a 64-bit target, the entries in .gnu.hash do not have a
3085 // uniform size, so we only set the entry size for a 32-bit
3087 if (parameters
->target().get_size() == 32)
3088 hashsec
->set_entsize(4);
3090 odyn
->add_section_address(elfcpp::DT_GNU_HASH
, hashsec
);
3094 // Assign offsets to each local portion of the dynamic symbol table.
3097 Layout::assign_local_dynsym_offsets(const Input_objects
* input_objects
)
3099 Output_section
* dynsym
= this->dynsym_section_
;
3100 gold_assert(dynsym
!= NULL
);
3102 off_t off
= dynsym
->offset();
3104 // Skip the dummy symbol at the start of the section.
3105 off
+= dynsym
->entsize();
3107 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
3108 p
!= input_objects
->relobj_end();
3111 unsigned int count
= (*p
)->set_local_dynsym_offset(off
);
3112 off
+= count
* dynsym
->entsize();
3116 // Create the version sections.
3119 Layout::create_version_sections(const Versions
* versions
,
3120 const Symbol_table
* symtab
,
3121 unsigned int local_symcount
,
3122 const std::vector
<Symbol
*>& dynamic_symbols
,
3123 const Output_section
* dynstr
)
3125 if (!versions
->any_defs() && !versions
->any_needs())
3128 switch (parameters
->size_and_endianness())
3130 #ifdef HAVE_TARGET_32_LITTLE
3131 case Parameters::TARGET_32_LITTLE
:
3132 this->sized_create_version_sections
<32, false>(versions
, symtab
,
3134 dynamic_symbols
, dynstr
);
3137 #ifdef HAVE_TARGET_32_BIG
3138 case Parameters::TARGET_32_BIG
:
3139 this->sized_create_version_sections
<32, true>(versions
, symtab
,
3141 dynamic_symbols
, dynstr
);
3144 #ifdef HAVE_TARGET_64_LITTLE
3145 case Parameters::TARGET_64_LITTLE
:
3146 this->sized_create_version_sections
<64, false>(versions
, symtab
,
3148 dynamic_symbols
, dynstr
);
3151 #ifdef HAVE_TARGET_64_BIG
3152 case Parameters::TARGET_64_BIG
:
3153 this->sized_create_version_sections
<64, true>(versions
, symtab
,
3155 dynamic_symbols
, dynstr
);
3163 // Create the version sections, sized version.
3165 template<int size
, bool big_endian
>
3167 Layout::sized_create_version_sections(
3168 const Versions
* versions
,
3169 const Symbol_table
* symtab
,
3170 unsigned int local_symcount
,
3171 const std::vector
<Symbol
*>& dynamic_symbols
,
3172 const Output_section
* dynstr
)
3174 Output_section
* vsec
= this->choose_output_section(NULL
, ".gnu.version",
3175 elfcpp::SHT_GNU_versym
,
3178 false, false, false);
3180 unsigned char* vbuf
;
3182 versions
->symbol_section_contents
<size
, big_endian
>(symtab
, &this->dynpool_
,
3187 Output_section_data
* vdata
= new Output_data_const_buffer(vbuf
, vsize
, 2,
3190 vsec
->add_output_section_data(vdata
);
3191 vsec
->set_entsize(2);
3192 vsec
->set_link_section(this->dynsym_section_
);
3194 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
3195 odyn
->add_section_address(elfcpp::DT_VERSYM
, vsec
);
3197 if (versions
->any_defs())
3199 Output_section
* vdsec
;
3200 vdsec
= this->choose_output_section(NULL
, ".gnu.version_d",
3201 elfcpp::SHT_GNU_verdef
,
3203 false, false, true, false, false,
3206 unsigned char* vdbuf
;
3207 unsigned int vdsize
;
3208 unsigned int vdentries
;
3209 versions
->def_section_contents
<size
, big_endian
>(&this->dynpool_
, &vdbuf
,
3210 &vdsize
, &vdentries
);
3212 Output_section_data
* vddata
=
3213 new Output_data_const_buffer(vdbuf
, vdsize
, 4, "** version defs");
3215 vdsec
->add_output_section_data(vddata
);
3216 vdsec
->set_link_section(dynstr
);
3217 vdsec
->set_info(vdentries
);
3219 odyn
->add_section_address(elfcpp::DT_VERDEF
, vdsec
);
3220 odyn
->add_constant(elfcpp::DT_VERDEFNUM
, vdentries
);
3223 if (versions
->any_needs())
3225 Output_section
* vnsec
;
3226 vnsec
= this->choose_output_section(NULL
, ".gnu.version_r",
3227 elfcpp::SHT_GNU_verneed
,
3229 false, false, true, false, false,
3232 unsigned char* vnbuf
;
3233 unsigned int vnsize
;
3234 unsigned int vnentries
;
3235 versions
->need_section_contents
<size
, big_endian
>(&this->dynpool_
,
3239 Output_section_data
* vndata
=
3240 new Output_data_const_buffer(vnbuf
, vnsize
, 4, "** version refs");
3242 vnsec
->add_output_section_data(vndata
);
3243 vnsec
->set_link_section(dynstr
);
3244 vnsec
->set_info(vnentries
);
3246 odyn
->add_section_address(elfcpp::DT_VERNEED
, vnsec
);
3247 odyn
->add_constant(elfcpp::DT_VERNEEDNUM
, vnentries
);
3251 // Create the .interp section and PT_INTERP segment.
3254 Layout::create_interp(const Target
* target
)
3256 const char* interp
= parameters
->options().dynamic_linker();
3259 interp
= target
->dynamic_linker();
3260 gold_assert(interp
!= NULL
);
3263 size_t len
= strlen(interp
) + 1;
3265 Output_section_data
* odata
= new Output_data_const(interp
, len
, 1);
3267 Output_section
* osec
= this->choose_output_section(NULL
, ".interp",
3268 elfcpp::SHT_PROGBITS
,
3271 false, false, false);
3272 osec
->add_output_section_data(odata
);
3274 if (!this->script_options_
->saw_phdrs_clause())
3276 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_INTERP
,
3278 oseg
->add_output_section(osec
, elfcpp::PF_R
, false);
3282 // Add dynamic tags for the PLT and the dynamic relocs. This is
3283 // called by the target-specific code. This does nothing if not doing
3286 // USE_REL is true for REL relocs rather than RELA relocs.
3288 // If PLT_GOT is not NULL, then DT_PLTGOT points to it.
3290 // If PLT_REL is not NULL, it is used for DT_PLTRELSZ, and DT_JMPREL,
3291 // and we also set DT_PLTREL. We use PLT_REL's output section, since
3292 // some targets have multiple reloc sections in PLT_REL.
3294 // If DYN_REL is not NULL, it is used for DT_REL/DT_RELA,
3295 // DT_RELSZ/DT_RELASZ, DT_RELENT/DT_RELAENT.
3297 // If ADD_DEBUG is true, we add a DT_DEBUG entry when generating an
3301 Layout::add_target_dynamic_tags(bool use_rel
, const Output_data
* plt_got
,
3302 const Output_data
* plt_rel
,
3303 const Output_data_reloc_generic
* dyn_rel
,
3304 bool add_debug
, bool dynrel_includes_plt
)
3306 Output_data_dynamic
* odyn
= this->dynamic_data_
;
3310 if (plt_got
!= NULL
&& plt_got
->output_section() != NULL
)
3311 odyn
->add_section_address(elfcpp::DT_PLTGOT
, plt_got
);
3313 if (plt_rel
!= NULL
&& plt_rel
->output_section() != NULL
)
3315 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, plt_rel
->output_section());
3316 odyn
->add_section_address(elfcpp::DT_JMPREL
, plt_rel
->output_section());
3317 odyn
->add_constant(elfcpp::DT_PLTREL
,
3318 use_rel
? elfcpp::DT_REL
: elfcpp::DT_RELA
);
3321 if (dyn_rel
!= NULL
&& dyn_rel
->output_section() != NULL
)
3323 odyn
->add_section_address(use_rel
? elfcpp::DT_REL
: elfcpp::DT_RELA
,
3325 if (plt_rel
!= NULL
&& dynrel_includes_plt
)
3326 odyn
->add_section_size(use_rel
? elfcpp::DT_RELSZ
: elfcpp::DT_RELASZ
,
3329 odyn
->add_section_size(use_rel
? elfcpp::DT_RELSZ
: elfcpp::DT_RELASZ
,
3331 const int size
= parameters
->target().get_size();
3336 rel_tag
= elfcpp::DT_RELENT
;
3338 rel_size
= Reloc_types
<elfcpp::SHT_REL
, 32, false>::reloc_size
;
3339 else if (size
== 64)
3340 rel_size
= Reloc_types
<elfcpp::SHT_REL
, 64, false>::reloc_size
;
3346 rel_tag
= elfcpp::DT_RELAENT
;
3348 rel_size
= Reloc_types
<elfcpp::SHT_RELA
, 32, false>::reloc_size
;
3349 else if (size
== 64)
3350 rel_size
= Reloc_types
<elfcpp::SHT_RELA
, 64, false>::reloc_size
;
3354 odyn
->add_constant(rel_tag
, rel_size
);
3356 if (parameters
->options().combreloc())
3358 size_t c
= dyn_rel
->relative_reloc_count();
3360 odyn
->add_constant((use_rel
3361 ? elfcpp::DT_RELCOUNT
3362 : elfcpp::DT_RELACOUNT
),
3367 if (add_debug
&& !parameters
->options().shared())
3369 // The value of the DT_DEBUG tag is filled in by the dynamic
3370 // linker at run time, and used by the debugger.
3371 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
3375 // Finish the .dynamic section and PT_DYNAMIC segment.
3378 Layout::finish_dynamic_section(const Input_objects
* input_objects
,
3379 const Symbol_table
* symtab
)
3381 if (!this->script_options_
->saw_phdrs_clause())
3383 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_DYNAMIC
,
3386 oseg
->add_output_section(this->dynamic_section_
,
3387 elfcpp::PF_R
| elfcpp::PF_W
,
3391 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
3393 for (Input_objects::Dynobj_iterator p
= input_objects
->dynobj_begin();
3394 p
!= input_objects
->dynobj_end();
3397 if (!(*p
)->is_needed()
3398 && (*p
)->input_file()->options().as_needed())
3400 // This dynamic object was linked with --as-needed, but it
3405 odyn
->add_string(elfcpp::DT_NEEDED
, (*p
)->soname());
3408 if (parameters
->options().shared())
3410 const char* soname
= parameters
->options().soname();
3412 odyn
->add_string(elfcpp::DT_SONAME
, soname
);
3415 Symbol
* sym
= symtab
->lookup(parameters
->options().init());
3416 if (sym
!= NULL
&& sym
->is_defined() && !sym
->is_from_dynobj())
3417 odyn
->add_symbol(elfcpp::DT_INIT
, sym
);
3419 sym
= symtab
->lookup(parameters
->options().fini());
3420 if (sym
!= NULL
&& sym
->is_defined() && !sym
->is_from_dynobj())
3421 odyn
->add_symbol(elfcpp::DT_FINI
, sym
);
3423 // Look for .init_array, .preinit_array and .fini_array by checking
3425 for(Layout::Section_list::const_iterator p
= this->section_list_
.begin();
3426 p
!= this->section_list_
.end();
3428 switch((*p
)->type())
3430 case elfcpp::SHT_FINI_ARRAY
:
3431 odyn
->add_section_address(elfcpp::DT_FINI_ARRAY
, *p
);
3432 odyn
->add_section_size(elfcpp::DT_FINI_ARRAYSZ
, *p
);
3434 case elfcpp::SHT_INIT_ARRAY
:
3435 odyn
->add_section_address(elfcpp::DT_INIT_ARRAY
, *p
);
3436 odyn
->add_section_size(elfcpp::DT_INIT_ARRAYSZ
, *p
);
3438 case elfcpp::SHT_PREINIT_ARRAY
:
3439 odyn
->add_section_address(elfcpp::DT_PREINIT_ARRAY
, *p
);
3440 odyn
->add_section_size(elfcpp::DT_PREINIT_ARRAYSZ
, *p
);
3446 // Add a DT_RPATH entry if needed.
3447 const General_options::Dir_list
& rpath(parameters
->options().rpath());
3450 std::string rpath_val
;
3451 for (General_options::Dir_list::const_iterator p
= rpath
.begin();
3455 if (rpath_val
.empty())
3456 rpath_val
= p
->name();
3459 // Eliminate duplicates.
3460 General_options::Dir_list::const_iterator q
;
3461 for (q
= rpath
.begin(); q
!= p
; ++q
)
3462 if (q
->name() == p
->name())
3467 rpath_val
+= p
->name();
3472 odyn
->add_string(elfcpp::DT_RPATH
, rpath_val
);
3473 if (parameters
->options().enable_new_dtags())
3474 odyn
->add_string(elfcpp::DT_RUNPATH
, rpath_val
);
3477 // Look for text segments that have dynamic relocations.
3478 bool have_textrel
= false;
3479 if (!this->script_options_
->saw_sections_clause())
3481 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
3482 p
!= this->segment_list_
.end();
3485 if (((*p
)->flags() & elfcpp::PF_W
) == 0
3486 && (*p
)->dynamic_reloc_count() > 0)
3488 have_textrel
= true;
3495 // We don't know the section -> segment mapping, so we are
3496 // conservative and just look for readonly sections with
3497 // relocations. If those sections wind up in writable segments,
3498 // then we have created an unnecessary DT_TEXTREL entry.
3499 for (Section_list::const_iterator p
= this->section_list_
.begin();
3500 p
!= this->section_list_
.end();
3503 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) != 0
3504 && ((*p
)->flags() & elfcpp::SHF_WRITE
) == 0
3505 && ((*p
)->dynamic_reloc_count() > 0))
3507 have_textrel
= true;
3513 // Add a DT_FLAGS entry. We add it even if no flags are set so that
3514 // post-link tools can easily modify these flags if desired.
3515 unsigned int flags
= 0;
3518 // Add a DT_TEXTREL for compatibility with older loaders.
3519 odyn
->add_constant(elfcpp::DT_TEXTREL
, 0);
3520 flags
|= elfcpp::DF_TEXTREL
;
3522 if (parameters
->options().text())
3523 gold_error(_("read-only segment has dynamic relocations"));
3524 else if (parameters
->options().warn_shared_textrel()
3525 && parameters
->options().shared())
3526 gold_warning(_("shared library text segment is not shareable"));
3528 if (parameters
->options().shared() && this->has_static_tls())
3529 flags
|= elfcpp::DF_STATIC_TLS
;
3530 if (parameters
->options().origin())
3531 flags
|= elfcpp::DF_ORIGIN
;
3532 if (parameters
->options().Bsymbolic())
3534 flags
|= elfcpp::DF_SYMBOLIC
;
3535 // Add DT_SYMBOLIC for compatibility with older loaders.
3536 odyn
->add_constant(elfcpp::DT_SYMBOLIC
, 0);
3538 if (parameters
->options().now())
3539 flags
|= elfcpp::DF_BIND_NOW
;
3540 odyn
->add_constant(elfcpp::DT_FLAGS
, flags
);
3543 if (parameters
->options().initfirst())
3544 flags
|= elfcpp::DF_1_INITFIRST
;
3545 if (parameters
->options().interpose())
3546 flags
|= elfcpp::DF_1_INTERPOSE
;
3547 if (parameters
->options().loadfltr())
3548 flags
|= elfcpp::DF_1_LOADFLTR
;
3549 if (parameters
->options().nodefaultlib())
3550 flags
|= elfcpp::DF_1_NODEFLIB
;
3551 if (parameters
->options().nodelete())
3552 flags
|= elfcpp::DF_1_NODELETE
;
3553 if (parameters
->options().nodlopen())
3554 flags
|= elfcpp::DF_1_NOOPEN
;
3555 if (parameters
->options().nodump())
3556 flags
|= elfcpp::DF_1_NODUMP
;
3557 if (!parameters
->options().shared())
3558 flags
&= ~(elfcpp::DF_1_INITFIRST
3559 | elfcpp::DF_1_NODELETE
3560 | elfcpp::DF_1_NOOPEN
);
3561 if (parameters
->options().origin())
3562 flags
|= elfcpp::DF_1_ORIGIN
;
3563 if (parameters
->options().now())
3564 flags
|= elfcpp::DF_1_NOW
;
3566 odyn
->add_constant(elfcpp::DT_FLAGS_1
, flags
);
3569 // Set the size of the _DYNAMIC symbol table to be the size of the
3573 Layout::set_dynamic_symbol_size(const Symbol_table
* symtab
)
3575 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
3576 odyn
->finalize_data_size();
3577 off_t data_size
= odyn
->data_size();
3578 const int size
= parameters
->target().get_size();
3580 symtab
->get_sized_symbol
<32>(this->dynamic_symbol_
)->set_symsize(data_size
);
3581 else if (size
== 64)
3582 symtab
->get_sized_symbol
<64>(this->dynamic_symbol_
)->set_symsize(data_size
);
3587 // The mapping of input section name prefixes to output section names.
3588 // In some cases one prefix is itself a prefix of another prefix; in
3589 // such a case the longer prefix must come first. These prefixes are
3590 // based on the GNU linker default ELF linker script.
3592 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
3593 const Layout::Section_name_mapping
Layout::section_name_mapping
[] =
3595 MAPPING_INIT(".text.", ".text"),
3596 MAPPING_INIT(".ctors.", ".ctors"),
3597 MAPPING_INIT(".dtors.", ".dtors"),
3598 MAPPING_INIT(".rodata.", ".rodata"),
3599 MAPPING_INIT(".data.rel.ro.local", ".data.rel.ro.local"),
3600 MAPPING_INIT(".data.rel.ro", ".data.rel.ro"),
3601 MAPPING_INIT(".data.", ".data"),
3602 MAPPING_INIT(".bss.", ".bss"),
3603 MAPPING_INIT(".tdata.", ".tdata"),
3604 MAPPING_INIT(".tbss.", ".tbss"),
3605 MAPPING_INIT(".init_array.", ".init_array"),
3606 MAPPING_INIT(".fini_array.", ".fini_array"),
3607 MAPPING_INIT(".sdata.", ".sdata"),
3608 MAPPING_INIT(".sbss.", ".sbss"),
3609 // FIXME: In the GNU linker, .sbss2 and .sdata2 are handled
3610 // differently depending on whether it is creating a shared library.
3611 MAPPING_INIT(".sdata2.", ".sdata"),
3612 MAPPING_INIT(".sbss2.", ".sbss"),
3613 MAPPING_INIT(".lrodata.", ".lrodata"),
3614 MAPPING_INIT(".ldata.", ".ldata"),
3615 MAPPING_INIT(".lbss.", ".lbss"),
3616 MAPPING_INIT(".gcc_except_table.", ".gcc_except_table"),
3617 MAPPING_INIT(".gnu.linkonce.d.rel.ro.local.", ".data.rel.ro.local"),
3618 MAPPING_INIT(".gnu.linkonce.d.rel.ro.", ".data.rel.ro"),
3619 MAPPING_INIT(".gnu.linkonce.t.", ".text"),
3620 MAPPING_INIT(".gnu.linkonce.r.", ".rodata"),
3621 MAPPING_INIT(".gnu.linkonce.d.", ".data"),
3622 MAPPING_INIT(".gnu.linkonce.b.", ".bss"),
3623 MAPPING_INIT(".gnu.linkonce.s.", ".sdata"),
3624 MAPPING_INIT(".gnu.linkonce.sb.", ".sbss"),
3625 MAPPING_INIT(".gnu.linkonce.s2.", ".sdata"),
3626 MAPPING_INIT(".gnu.linkonce.sb2.", ".sbss"),
3627 MAPPING_INIT(".gnu.linkonce.wi.", ".debug_info"),
3628 MAPPING_INIT(".gnu.linkonce.td.", ".tdata"),
3629 MAPPING_INIT(".gnu.linkonce.tb.", ".tbss"),
3630 MAPPING_INIT(".gnu.linkonce.lr.", ".lrodata"),
3631 MAPPING_INIT(".gnu.linkonce.l.", ".ldata"),
3632 MAPPING_INIT(".gnu.linkonce.lb.", ".lbss"),
3633 MAPPING_INIT(".ARM.extab", ".ARM.extab"),
3634 MAPPING_INIT(".gnu.linkonce.armextab.", ".ARM.extab"),
3635 MAPPING_INIT(".ARM.exidx", ".ARM.exidx"),
3636 MAPPING_INIT(".gnu.linkonce.armexidx.", ".ARM.exidx"),
3640 const int Layout::section_name_mapping_count
=
3641 (sizeof(Layout::section_name_mapping
)
3642 / sizeof(Layout::section_name_mapping
[0]));
3644 // Choose the output section name to use given an input section name.
3645 // Set *PLEN to the length of the name. *PLEN is initialized to the
3649 Layout::output_section_name(const char* name
, size_t* plen
)
3651 // gcc 4.3 generates the following sorts of section names when it
3652 // needs a section name specific to a function:
3658 // .data.rel.local.FN
3660 // .data.rel.ro.local.FN
3667 // The GNU linker maps all of those to the part before the .FN,
3668 // except that .data.rel.local.FN is mapped to .data, and
3669 // .data.rel.ro.local.FN is mapped to .data.rel.ro. The sections
3670 // beginning with .data.rel.ro.local are grouped together.
3672 // For an anonymous namespace, the string FN can contain a '.'.
3674 // Also of interest: .rodata.strN.N, .rodata.cstN, both of which the
3675 // GNU linker maps to .rodata.
3677 // The .data.rel.ro sections are used with -z relro. The sections
3678 // are recognized by name. We use the same names that the GNU
3679 // linker does for these sections.
3681 // It is hard to handle this in a principled way, so we don't even
3682 // try. We use a table of mappings. If the input section name is
3683 // not found in the table, we simply use it as the output section
3686 const Section_name_mapping
* psnm
= section_name_mapping
;
3687 for (int i
= 0; i
< section_name_mapping_count
; ++i
, ++psnm
)
3689 if (strncmp(name
, psnm
->from
, psnm
->fromlen
) == 0)
3691 *plen
= psnm
->tolen
;
3699 // Check if a comdat group or .gnu.linkonce section with the given
3700 // NAME is selected for the link. If there is already a section,
3701 // *KEPT_SECTION is set to point to the existing section and the
3702 // function returns false. Otherwise, OBJECT, SHNDX, IS_COMDAT, and
3703 // IS_GROUP_NAME are recorded for this NAME in the layout object,
3704 // *KEPT_SECTION is set to the internal copy and the function returns
3708 Layout::find_or_add_kept_section(const std::string
& name
,
3713 Kept_section
** kept_section
)
3715 // It's normal to see a couple of entries here, for the x86 thunk
3716 // sections. If we see more than a few, we're linking a C++
3717 // program, and we resize to get more space to minimize rehashing.
3718 if (this->signatures_
.size() > 4
3719 && !this->resized_signatures_
)
3721 reserve_unordered_map(&this->signatures_
,
3722 this->number_of_input_files_
* 64);
3723 this->resized_signatures_
= true;
3726 Kept_section candidate
;
3727 std::pair
<Signatures::iterator
, bool> ins
=
3728 this->signatures_
.insert(std::make_pair(name
, candidate
));
3730 if (kept_section
!= NULL
)
3731 *kept_section
= &ins
.first
->second
;
3734 // This is the first time we've seen this signature.
3735 ins
.first
->second
.set_object(object
);
3736 ins
.first
->second
.set_shndx(shndx
);
3738 ins
.first
->second
.set_is_comdat();
3740 ins
.first
->second
.set_is_group_name();
3744 // We have already seen this signature.
3746 if (ins
.first
->second
.is_group_name())
3748 // We've already seen a real section group with this signature.
3749 // If the kept group is from a plugin object, and we're in the
3750 // replacement phase, accept the new one as a replacement.
3751 if (ins
.first
->second
.object() == NULL
3752 && parameters
->options().plugins()->in_replacement_phase())
3754 ins
.first
->second
.set_object(object
);
3755 ins
.first
->second
.set_shndx(shndx
);
3760 else if (is_group_name
)
3762 // This is a real section group, and we've already seen a
3763 // linkonce section with this signature. Record that we've seen
3764 // a section group, and don't include this section group.
3765 ins
.first
->second
.set_is_group_name();
3770 // We've already seen a linkonce section and this is a linkonce
3771 // section. These don't block each other--this may be the same
3772 // symbol name with different section types.
3777 // Store the allocated sections into the section list.
3780 Layout::get_allocated_sections(Section_list
* section_list
) const
3782 for (Section_list::const_iterator p
= this->section_list_
.begin();
3783 p
!= this->section_list_
.end();
3785 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) != 0)
3786 section_list
->push_back(*p
);
3789 // Create an output segment.
3792 Layout::make_output_segment(elfcpp::Elf_Word type
, elfcpp::Elf_Word flags
)
3794 gold_assert(!parameters
->options().relocatable());
3795 Output_segment
* oseg
= new Output_segment(type
, flags
);
3796 this->segment_list_
.push_back(oseg
);
3798 if (type
== elfcpp::PT_TLS
)
3799 this->tls_segment_
= oseg
;
3800 else if (type
== elfcpp::PT_GNU_RELRO
)
3801 this->relro_segment_
= oseg
;
3806 // Write out the Output_sections. Most won't have anything to write,
3807 // since most of the data will come from input sections which are
3808 // handled elsewhere. But some Output_sections do have Output_data.
3811 Layout::write_output_sections(Output_file
* of
) const
3813 for (Section_list::const_iterator p
= this->section_list_
.begin();
3814 p
!= this->section_list_
.end();
3817 if (!(*p
)->after_input_sections())
3822 // Write out data not associated with a section or the symbol table.
3825 Layout::write_data(const Symbol_table
* symtab
, Output_file
* of
) const
3827 if (!parameters
->options().strip_all())
3829 const Output_section
* symtab_section
= this->symtab_section_
;
3830 for (Section_list::const_iterator p
= this->section_list_
.begin();
3831 p
!= this->section_list_
.end();
3834 if ((*p
)->needs_symtab_index())
3836 gold_assert(symtab_section
!= NULL
);
3837 unsigned int index
= (*p
)->symtab_index();
3838 gold_assert(index
> 0 && index
!= -1U);
3839 off_t off
= (symtab_section
->offset()
3840 + index
* symtab_section
->entsize());
3841 symtab
->write_section_symbol(*p
, this->symtab_xindex_
, of
, off
);
3846 const Output_section
* dynsym_section
= this->dynsym_section_
;
3847 for (Section_list::const_iterator p
= this->section_list_
.begin();
3848 p
!= this->section_list_
.end();
3851 if ((*p
)->needs_dynsym_index())
3853 gold_assert(dynsym_section
!= NULL
);
3854 unsigned int index
= (*p
)->dynsym_index();
3855 gold_assert(index
> 0 && index
!= -1U);
3856 off_t off
= (dynsym_section
->offset()
3857 + index
* dynsym_section
->entsize());
3858 symtab
->write_section_symbol(*p
, this->dynsym_xindex_
, of
, off
);
3862 // Write out the Output_data which are not in an Output_section.
3863 for (Data_list::const_iterator p
= this->special_output_list_
.begin();
3864 p
!= this->special_output_list_
.end();
3869 // Write out the Output_sections which can only be written after the
3870 // input sections are complete.
3873 Layout::write_sections_after_input_sections(Output_file
* of
)
3875 // Determine the final section offsets, and thus the final output
3876 // file size. Note we finalize the .shstrab last, to allow the
3877 // after_input_section sections to modify their section-names before
3879 if (this->any_postprocessing_sections_
)
3881 off_t off
= this->output_file_size_
;
3882 off
= this->set_section_offsets(off
, POSTPROCESSING_SECTIONS_PASS
);
3884 // Now that we've finalized the names, we can finalize the shstrab.
3886 this->set_section_offsets(off
,
3887 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
);
3889 if (off
> this->output_file_size_
)
3892 this->output_file_size_
= off
;
3896 for (Section_list::const_iterator p
= this->section_list_
.begin();
3897 p
!= this->section_list_
.end();
3900 if ((*p
)->after_input_sections())
3904 this->section_headers_
->write(of
);
3907 // If the build ID requires computing a checksum, do so here, and
3908 // write it out. We compute a checksum over the entire file because
3909 // that is simplest.
3912 Layout::write_build_id(Output_file
* of
) const
3914 if (this->build_id_note_
== NULL
)
3917 const unsigned char* iv
= of
->get_input_view(0, this->output_file_size_
);
3919 unsigned char* ov
= of
->get_output_view(this->build_id_note_
->offset(),
3920 this->build_id_note_
->data_size());
3922 const char* style
= parameters
->options().build_id();
3923 if (strcmp(style
, "sha1") == 0)
3926 sha1_init_ctx(&ctx
);
3927 sha1_process_bytes(iv
, this->output_file_size_
, &ctx
);
3928 sha1_finish_ctx(&ctx
, ov
);
3930 else if (strcmp(style
, "md5") == 0)
3934 md5_process_bytes(iv
, this->output_file_size_
, &ctx
);
3935 md5_finish_ctx(&ctx
, ov
);
3940 of
->write_output_view(this->build_id_note_
->offset(),
3941 this->build_id_note_
->data_size(),
3944 of
->free_input_view(0, this->output_file_size_
, iv
);
3947 // Write out a binary file. This is called after the link is
3948 // complete. IN is the temporary output file we used to generate the
3949 // ELF code. We simply walk through the segments, read them from
3950 // their file offset in IN, and write them to their load address in
3951 // the output file. FIXME: with a bit more work, we could support
3952 // S-records and/or Intel hex format here.
3955 Layout::write_binary(Output_file
* in
) const
3957 gold_assert(parameters
->options().oformat_enum()
3958 == General_options::OBJECT_FORMAT_BINARY
);
3960 // Get the size of the binary file.
3961 uint64_t max_load_address
= 0;
3962 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
3963 p
!= this->segment_list_
.end();
3966 if ((*p
)->type() == elfcpp::PT_LOAD
&& (*p
)->filesz() > 0)
3968 uint64_t max_paddr
= (*p
)->paddr() + (*p
)->filesz();
3969 if (max_paddr
> max_load_address
)
3970 max_load_address
= max_paddr
;
3974 Output_file
out(parameters
->options().output_file_name());
3975 out
.open(max_load_address
);
3977 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
3978 p
!= this->segment_list_
.end();
3981 if ((*p
)->type() == elfcpp::PT_LOAD
&& (*p
)->filesz() > 0)
3983 const unsigned char* vin
= in
->get_input_view((*p
)->offset(),
3985 unsigned char* vout
= out
.get_output_view((*p
)->paddr(),
3987 memcpy(vout
, vin
, (*p
)->filesz());
3988 out
.write_output_view((*p
)->paddr(), (*p
)->filesz(), vout
);
3989 in
->free_input_view((*p
)->offset(), (*p
)->filesz(), vin
);
3996 // Print the output sections to the map file.
3999 Layout::print_to_mapfile(Mapfile
* mapfile
) const
4001 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
4002 p
!= this->segment_list_
.end();
4004 (*p
)->print_sections_to_mapfile(mapfile
);
4007 // Print statistical information to stderr. This is used for --stats.
4010 Layout::print_stats() const
4012 this->namepool_
.print_stats("section name pool");
4013 this->sympool_
.print_stats("output symbol name pool");
4014 this->dynpool_
.print_stats("dynamic name pool");
4016 for (Section_list::const_iterator p
= this->section_list_
.begin();
4017 p
!= this->section_list_
.end();
4019 (*p
)->print_merge_stats();
4022 // Write_sections_task methods.
4024 // We can always run this task.
4027 Write_sections_task::is_runnable()
4032 // We need to unlock both OUTPUT_SECTIONS_BLOCKER and FINAL_BLOCKER
4036 Write_sections_task::locks(Task_locker
* tl
)
4038 tl
->add(this, this->output_sections_blocker_
);
4039 tl
->add(this, this->final_blocker_
);
4042 // Run the task--write out the data.
4045 Write_sections_task::run(Workqueue
*)
4047 this->layout_
->write_output_sections(this->of_
);
4050 // Write_data_task methods.
4052 // We can always run this task.
4055 Write_data_task::is_runnable()
4060 // We need to unlock FINAL_BLOCKER when finished.
4063 Write_data_task::locks(Task_locker
* tl
)
4065 tl
->add(this, this->final_blocker_
);
4068 // Run the task--write out the data.
4071 Write_data_task::run(Workqueue
*)
4073 this->layout_
->write_data(this->symtab_
, this->of_
);
4076 // Write_symbols_task methods.
4078 // We can always run this task.
4081 Write_symbols_task::is_runnable()
4086 // We need to unlock FINAL_BLOCKER when finished.
4089 Write_symbols_task::locks(Task_locker
* tl
)
4091 tl
->add(this, this->final_blocker_
);
4094 // Run the task--write out the symbols.
4097 Write_symbols_task::run(Workqueue
*)
4099 this->symtab_
->write_globals(this->sympool_
, this->dynpool_
,
4100 this->layout_
->symtab_xindex(),
4101 this->layout_
->dynsym_xindex(), this->of_
);
4104 // Write_after_input_sections_task methods.
4106 // We can only run this task after the input sections have completed.
4109 Write_after_input_sections_task::is_runnable()
4111 if (this->input_sections_blocker_
->is_blocked())
4112 return this->input_sections_blocker_
;
4116 // We need to unlock FINAL_BLOCKER when finished.
4119 Write_after_input_sections_task::locks(Task_locker
* tl
)
4121 tl
->add(this, this->final_blocker_
);
4127 Write_after_input_sections_task::run(Workqueue
*)
4129 this->layout_
->write_sections_after_input_sections(this->of_
);
4132 // Close_task_runner methods.
4134 // Run the task--close the file.
4137 Close_task_runner::run(Workqueue
*, const Task
*)
4139 // If we need to compute a checksum for the BUILD if, we do so here.
4140 this->layout_
->write_build_id(this->of_
);
4142 // If we've been asked to create a binary file, we do so here.
4143 if (this->options_
->oformat_enum() != General_options::OBJECT_FORMAT_ELF
)
4144 this->layout_
->write_binary(this->of_
);
4149 // Instantiate the templates we need. We could use the configure
4150 // script to restrict this to only the ones for implemented targets.
4152 #ifdef HAVE_TARGET_32_LITTLE
4155 Layout::layout
<32, false>(Sized_relobj
<32, false>* object
, unsigned int shndx
,
4157 const elfcpp::Shdr
<32, false>& shdr
,
4158 unsigned int, unsigned int, off_t
*);
4161 #ifdef HAVE_TARGET_32_BIG
4164 Layout::layout
<32, true>(Sized_relobj
<32, true>* object
, unsigned int shndx
,
4166 const elfcpp::Shdr
<32, true>& shdr
,
4167 unsigned int, unsigned int, off_t
*);
4170 #ifdef HAVE_TARGET_64_LITTLE
4173 Layout::layout
<64, false>(Sized_relobj
<64, false>* object
, unsigned int shndx
,
4175 const elfcpp::Shdr
<64, false>& shdr
,
4176 unsigned int, unsigned int, off_t
*);
4179 #ifdef HAVE_TARGET_64_BIG
4182 Layout::layout
<64, true>(Sized_relobj
<64, true>* object
, unsigned int shndx
,
4184 const elfcpp::Shdr
<64, true>& shdr
,
4185 unsigned int, unsigned int, off_t
*);
4188 #ifdef HAVE_TARGET_32_LITTLE
4191 Layout::layout_reloc
<32, false>(Sized_relobj
<32, false>* object
,
4192 unsigned int reloc_shndx
,
4193 const elfcpp::Shdr
<32, false>& shdr
,
4194 Output_section
* data_section
,
4195 Relocatable_relocs
* rr
);
4198 #ifdef HAVE_TARGET_32_BIG
4201 Layout::layout_reloc
<32, true>(Sized_relobj
<32, true>* object
,
4202 unsigned int reloc_shndx
,
4203 const elfcpp::Shdr
<32, true>& shdr
,
4204 Output_section
* data_section
,
4205 Relocatable_relocs
* rr
);
4208 #ifdef HAVE_TARGET_64_LITTLE
4211 Layout::layout_reloc
<64, false>(Sized_relobj
<64, false>* object
,
4212 unsigned int reloc_shndx
,
4213 const elfcpp::Shdr
<64, false>& shdr
,
4214 Output_section
* data_section
,
4215 Relocatable_relocs
* rr
);
4218 #ifdef HAVE_TARGET_64_BIG
4221 Layout::layout_reloc
<64, true>(Sized_relobj
<64, true>* object
,
4222 unsigned int reloc_shndx
,
4223 const elfcpp::Shdr
<64, true>& shdr
,
4224 Output_section
* data_section
,
4225 Relocatable_relocs
* rr
);
4228 #ifdef HAVE_TARGET_32_LITTLE
4231 Layout::layout_group
<32, false>(Symbol_table
* symtab
,
4232 Sized_relobj
<32, false>* object
,
4234 const char* group_section_name
,
4235 const char* signature
,
4236 const elfcpp::Shdr
<32, false>& shdr
,
4237 elfcpp::Elf_Word flags
,
4238 std::vector
<unsigned int>* shndxes
);
4241 #ifdef HAVE_TARGET_32_BIG
4244 Layout::layout_group
<32, true>(Symbol_table
* symtab
,
4245 Sized_relobj
<32, true>* object
,
4247 const char* group_section_name
,
4248 const char* signature
,
4249 const elfcpp::Shdr
<32, true>& shdr
,
4250 elfcpp::Elf_Word flags
,
4251 std::vector
<unsigned int>* shndxes
);
4254 #ifdef HAVE_TARGET_64_LITTLE
4257 Layout::layout_group
<64, false>(Symbol_table
* symtab
,
4258 Sized_relobj
<64, false>* object
,
4260 const char* group_section_name
,
4261 const char* signature
,
4262 const elfcpp::Shdr
<64, false>& shdr
,
4263 elfcpp::Elf_Word flags
,
4264 std::vector
<unsigned int>* shndxes
);
4267 #ifdef HAVE_TARGET_64_BIG
4270 Layout::layout_group
<64, true>(Symbol_table
* symtab
,
4271 Sized_relobj
<64, true>* object
,
4273 const char* group_section_name
,
4274 const char* signature
,
4275 const elfcpp::Shdr
<64, true>& shdr
,
4276 elfcpp::Elf_Word flags
,
4277 std::vector
<unsigned int>* shndxes
);
4280 #ifdef HAVE_TARGET_32_LITTLE
4283 Layout::layout_eh_frame
<32, false>(Sized_relobj
<32, false>* object
,
4284 const unsigned char* symbols
,
4286 const unsigned char* symbol_names
,
4287 off_t symbol_names_size
,
4289 const elfcpp::Shdr
<32, false>& shdr
,
4290 unsigned int reloc_shndx
,
4291 unsigned int reloc_type
,
4295 #ifdef HAVE_TARGET_32_BIG
4298 Layout::layout_eh_frame
<32, true>(Sized_relobj
<32, true>* object
,
4299 const unsigned char* symbols
,
4301 const unsigned char* symbol_names
,
4302 off_t symbol_names_size
,
4304 const elfcpp::Shdr
<32, true>& shdr
,
4305 unsigned int reloc_shndx
,
4306 unsigned int reloc_type
,
4310 #ifdef HAVE_TARGET_64_LITTLE
4313 Layout::layout_eh_frame
<64, false>(Sized_relobj
<64, false>* object
,
4314 const unsigned char* symbols
,
4316 const unsigned char* symbol_names
,
4317 off_t symbol_names_size
,
4319 const elfcpp::Shdr
<64, false>& shdr
,
4320 unsigned int reloc_shndx
,
4321 unsigned int reloc_type
,
4325 #ifdef HAVE_TARGET_64_BIG
4328 Layout::layout_eh_frame
<64, true>(Sized_relobj
<64, true>* object
,
4329 const unsigned char* symbols
,
4331 const unsigned char* symbol_names
,
4332 off_t symbol_names_size
,
4334 const elfcpp::Shdr
<64, true>& shdr
,
4335 unsigned int reloc_shndx
,
4336 unsigned int reloc_type
,
4340 } // End namespace gold.