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.
34 #include "libiberty.h"
38 #include "parameters.h"
42 #include "script-sections.h"
47 #include "compressed_output.h"
48 #include "reduced_debug_output.h"
50 #include "descriptors.h"
52 #include "incremental.h"
58 // Layout::Relaxation_debug_check methods.
60 // Check that sections and special data are in reset states.
61 // We do not save states for Output_sections and special Output_data.
62 // So we check that they have not assigned any addresses or offsets.
63 // clean_up_after_relaxation simply resets their addresses and offsets.
65 Layout::Relaxation_debug_check::check_output_data_for_reset_values(
66 const Layout::Section_list
& sections
,
67 const Layout::Data_list
& special_outputs
)
69 for(Layout::Section_list::const_iterator p
= sections
.begin();
72 gold_assert((*p
)->address_and_file_offset_have_reset_values());
74 for(Layout::Data_list::const_iterator p
= special_outputs
.begin();
75 p
!= special_outputs
.end();
77 gold_assert((*p
)->address_and_file_offset_have_reset_values());
80 // Save information of SECTIONS for checking later.
83 Layout::Relaxation_debug_check::read_sections(
84 const Layout::Section_list
& sections
)
86 for(Layout::Section_list::const_iterator p
= sections
.begin();
90 Output_section
* os
= *p
;
92 info
.output_section
= os
;
93 info
.address
= os
->is_address_valid() ? os
->address() : 0;
94 info
.data_size
= os
->is_data_size_valid() ? os
->data_size() : -1;
95 info
.offset
= os
->is_offset_valid()? os
->offset() : -1 ;
96 this->section_infos_
.push_back(info
);
100 // Verify SECTIONS using previously recorded information.
103 Layout::Relaxation_debug_check::verify_sections(
104 const Layout::Section_list
& sections
)
107 for(Layout::Section_list::const_iterator p
= sections
.begin();
111 Output_section
* os
= *p
;
112 uint64_t address
= os
->is_address_valid() ? os
->address() : 0;
113 off_t data_size
= os
->is_data_size_valid() ? os
->data_size() : -1;
114 off_t offset
= os
->is_offset_valid()? os
->offset() : -1 ;
116 if (i
>= this->section_infos_
.size())
118 gold_fatal("Section_info of %s missing.\n", os
->name());
120 const Section_info
& info
= this->section_infos_
[i
];
121 if (os
!= info
.output_section
)
122 gold_fatal("Section order changed. Expecting %s but see %s\n",
123 info
.output_section
->name(), os
->name());
124 if (address
!= info
.address
125 || data_size
!= info
.data_size
126 || offset
!= info
.offset
)
127 gold_fatal("Section %s changed.\n", os
->name());
131 // Layout_task_runner methods.
133 // Lay out the sections. This is called after all the input objects
137 Layout_task_runner::run(Workqueue
* workqueue
, const Task
* task
)
139 off_t file_size
= this->layout_
->finalize(this->input_objects_
,
144 // Now we know the final size of the output file and we know where
145 // each piece of information goes.
147 if (this->mapfile_
!= NULL
)
149 this->mapfile_
->print_discarded_sections(this->input_objects_
);
150 this->layout_
->print_to_mapfile(this->mapfile_
);
153 Output_file
* of
= new Output_file(parameters
->options().output_file_name());
154 if (this->options_
.oformat_enum() != General_options::OBJECT_FORMAT_ELF
)
155 of
->set_is_temporary();
158 // Queue up the final set of tasks.
159 gold::queue_final_tasks(this->options_
, this->input_objects_
,
160 this->symtab_
, this->layout_
, workqueue
, of
);
165 Layout::Layout(int number_of_input_files
, Script_options
* script_options
)
166 : number_of_input_files_(number_of_input_files
),
167 script_options_(script_options
),
175 unattached_section_list_(),
176 special_output_list_(),
177 section_headers_(NULL
),
179 relro_segment_(NULL
),
181 symtab_section_(NULL
),
182 symtab_xindex_(NULL
),
183 dynsym_section_(NULL
),
184 dynsym_xindex_(NULL
),
185 dynamic_section_(NULL
),
186 dynamic_symbol_(NULL
),
188 eh_frame_section_(NULL
),
189 eh_frame_data_(NULL
),
190 added_eh_frame_data_(false),
191 eh_frame_hdr_section_(NULL
),
192 build_id_note_(NULL
),
196 output_file_size_(-1),
197 have_added_input_section_(false),
198 sections_are_attached_(false),
199 input_requires_executable_stack_(false),
200 input_with_gnu_stack_note_(false),
201 input_without_gnu_stack_note_(false),
202 has_static_tls_(false),
203 any_postprocessing_sections_(false),
204 resized_signatures_(false),
205 have_stabstr_section_(false),
206 incremental_inputs_(NULL
),
207 record_output_section_data_from_script_(false),
208 script_output_section_data_list_(),
209 segment_states_(NULL
),
210 relaxation_debug_check_(NULL
)
212 // Make space for more than enough segments for a typical file.
213 // This is just for efficiency--it's OK if we wind up needing more.
214 this->segment_list_
.reserve(12);
216 // We expect two unattached Output_data objects: the file header and
217 // the segment headers.
218 this->special_output_list_
.reserve(2);
220 // Initialize structure needed for an incremental build.
221 if (parameters
->options().incremental())
222 this->incremental_inputs_
= new Incremental_inputs
;
224 // The section name pool is worth optimizing in all cases, because
225 // it is small, but there are often overlaps due to .rel sections.
226 this->namepool_
.set_optimize();
229 // Hash a key we use to look up an output section mapping.
232 Layout::Hash_key::operator()(const Layout::Key
& k
) const
234 return k
.first
+ k
.second
.first
+ k
.second
.second
;
237 // Returns whether the given section is in the list of
238 // debug-sections-used-by-some-version-of-gdb. Currently,
239 // we've checked versions of gdb up to and including 6.7.1.
241 static const char* gdb_sections
[] =
243 // ".debug_aranges", // not used by gdb as of 6.7.1
250 // ".debug_pubnames", // not used by gdb as of 6.7.1
255 static const char* lines_only_debug_sections
[] =
257 // ".debug_aranges", // not used by gdb as of 6.7.1
264 // ".debug_pubnames", // not used by gdb as of 6.7.1
270 is_gdb_debug_section(const char* str
)
272 // We can do this faster: binary search or a hashtable. But why bother?
273 for (size_t i
= 0; i
< sizeof(gdb_sections
)/sizeof(*gdb_sections
); ++i
)
274 if (strcmp(str
, gdb_sections
[i
]) == 0)
280 is_lines_only_debug_section(const char* str
)
282 // We can do this faster: binary search or a hashtable. But why bother?
284 i
< sizeof(lines_only_debug_sections
)/sizeof(*lines_only_debug_sections
);
286 if (strcmp(str
, lines_only_debug_sections
[i
]) == 0)
291 // Whether to include this section in the link.
293 template<int size
, bool big_endian
>
295 Layout::include_section(Sized_relobj
<size
, big_endian
>*, const char* name
,
296 const elfcpp::Shdr
<size
, big_endian
>& shdr
)
298 if (shdr
.get_sh_flags() & elfcpp::SHF_EXCLUDE
)
301 switch (shdr
.get_sh_type())
303 case elfcpp::SHT_NULL
:
304 case elfcpp::SHT_SYMTAB
:
305 case elfcpp::SHT_DYNSYM
:
306 case elfcpp::SHT_HASH
:
307 case elfcpp::SHT_DYNAMIC
:
308 case elfcpp::SHT_SYMTAB_SHNDX
:
311 case elfcpp::SHT_STRTAB
:
312 // Discard the sections which have special meanings in the ELF
313 // ABI. Keep others (e.g., .stabstr). We could also do this by
314 // checking the sh_link fields of the appropriate sections.
315 return (strcmp(name
, ".dynstr") != 0
316 && strcmp(name
, ".strtab") != 0
317 && strcmp(name
, ".shstrtab") != 0);
319 case elfcpp::SHT_RELA
:
320 case elfcpp::SHT_REL
:
321 case elfcpp::SHT_GROUP
:
322 // If we are emitting relocations these should be handled
324 gold_assert(!parameters
->options().relocatable()
325 && !parameters
->options().emit_relocs());
328 case elfcpp::SHT_PROGBITS
:
329 if (parameters
->options().strip_debug()
330 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
332 if (is_debug_info_section(name
))
335 if (parameters
->options().strip_debug_non_line()
336 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
338 // Debugging sections can only be recognized by name.
339 if (is_prefix_of(".debug", name
)
340 && !is_lines_only_debug_section(name
))
343 if (parameters
->options().strip_debug_gdb()
344 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
346 // Debugging sections can only be recognized by name.
347 if (is_prefix_of(".debug", name
)
348 && !is_gdb_debug_section(name
))
351 if (parameters
->options().strip_lto_sections()
352 && !parameters
->options().relocatable()
353 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
355 // Ignore LTO sections containing intermediate code.
356 if (is_prefix_of(".gnu.lto_", name
))
359 // The GNU linker strips .gnu_debuglink sections, so we do too.
360 // This is a feature used to keep debugging information in
362 if (strcmp(name
, ".gnu_debuglink") == 0)
371 // Return an output section named NAME, or NULL if there is none.
374 Layout::find_output_section(const char* name
) const
376 for (Section_list::const_iterator p
= this->section_list_
.begin();
377 p
!= this->section_list_
.end();
379 if (strcmp((*p
)->name(), name
) == 0)
384 // Return an output segment of type TYPE, with segment flags SET set
385 // and segment flags CLEAR clear. Return NULL if there is none.
388 Layout::find_output_segment(elfcpp::PT type
, elfcpp::Elf_Word set
,
389 elfcpp::Elf_Word clear
) const
391 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
392 p
!= this->segment_list_
.end();
394 if (static_cast<elfcpp::PT
>((*p
)->type()) == type
395 && ((*p
)->flags() & set
) == set
396 && ((*p
)->flags() & clear
) == 0)
401 // Return the output section to use for section NAME with type TYPE
402 // and section flags FLAGS. NAME must be canonicalized in the string
403 // pool, and NAME_KEY is the key. IS_INTERP is true if this is the
404 // .interp section. IS_DYNAMIC_LINKER_SECTION is true if this section
405 // is used by the dynamic linker. IS_RELRO is true for a relro
406 // section. IS_LAST_RELRO is true for the last relro section.
407 // IS_FIRST_NON_RELRO is true for the first non-relro section.
410 Layout::get_output_section(const char* name
, Stringpool::Key name_key
,
411 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
,
412 Output_section_order order
, bool is_relro
)
414 elfcpp::Elf_Xword lookup_flags
= flags
;
416 // Ignoring SHF_WRITE and SHF_EXECINSTR here means that we combine
417 // read-write with read-only sections. Some other ELF linkers do
418 // not do this. FIXME: Perhaps there should be an option
420 lookup_flags
&= ~(elfcpp::SHF_WRITE
| elfcpp::SHF_EXECINSTR
);
422 const Key
key(name_key
, std::make_pair(type
, lookup_flags
));
423 const std::pair
<Key
, Output_section
*> v(key
, NULL
);
424 std::pair
<Section_name_map::iterator
, bool> ins(
425 this->section_name_map_
.insert(v
));
428 return ins
.first
->second
;
431 // This is the first time we've seen this name/type/flags
432 // combination. For compatibility with the GNU linker, we
433 // combine sections with contents and zero flags with sections
434 // with non-zero flags. This is a workaround for cases where
435 // assembler code forgets to set section flags. FIXME: Perhaps
436 // there should be an option to control this.
437 Output_section
* os
= NULL
;
439 if (type
== elfcpp::SHT_PROGBITS
)
443 Output_section
* same_name
= this->find_output_section(name
);
444 if (same_name
!= NULL
445 && same_name
->type() == elfcpp::SHT_PROGBITS
446 && (same_name
->flags() & elfcpp::SHF_TLS
) == 0)
449 else if ((flags
& elfcpp::SHF_TLS
) == 0)
451 elfcpp::Elf_Xword zero_flags
= 0;
452 const Key
zero_key(name_key
, std::make_pair(type
, zero_flags
));
453 Section_name_map::iterator p
=
454 this->section_name_map_
.find(zero_key
);
455 if (p
!= this->section_name_map_
.end())
461 os
= this->make_output_section(name
, type
, flags
, order
, is_relro
);
463 ins
.first
->second
= os
;
468 // Pick the output section to use for section NAME, in input file
469 // RELOBJ, with type TYPE and flags FLAGS. RELOBJ may be NULL for a
470 // linker created section. IS_INPUT_SECTION is true if we are
471 // choosing an output section for an input section found in a input
472 // file. IS_INTERP is true if this is the .interp section.
473 // IS_DYNAMIC_LINKER_SECTION is true if this section is used by the
474 // dynamic linker. IS_RELRO is true for a relro section.
475 // IS_LAST_RELRO is true for the last relro section.
476 // IS_FIRST_NON_RELRO is true for the first non-relro section. This
477 // will return NULL if the input section should be discarded.
480 Layout::choose_output_section(const Relobj
* relobj
, const char* name
,
481 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
,
482 bool is_input_section
, Output_section_order order
,
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
);
545 Output_section
* os
= this->make_output_section(name
, type
, flags
,
548 os
->set_found_in_sections_clause();
550 // Special handling for NOLOAD sections.
551 if (script_section_type
== Script_sections::ST_NOLOAD
)
555 // The constructor of Output_section sets addresses of non-ALLOC
556 // sections to 0 by default. We don't want that for NOLOAD
557 // sections even if they have no SHF_ALLOC flag.
558 if ((os
->flags() & elfcpp::SHF_ALLOC
) == 0
559 && os
->is_address_valid())
561 gold_assert(os
->address() == 0
562 && !os
->is_offset_valid()
563 && !os
->is_data_size_valid());
564 os
->reset_address_and_file_offset();
568 *output_section_slot
= os
;
573 // FIXME: Handle SHF_OS_NONCONFORMING somewhere.
575 // Turn NAME from the name of the input section into the name of the
578 size_t len
= strlen(name
);
580 && !this->script_options_
->saw_sections_clause()
581 && !parameters
->options().relocatable())
582 name
= Layout::output_section_name(name
, &len
);
584 Stringpool::Key name_key
;
585 name
= this->namepool_
.add_with_length(name
, len
, true, &name_key
);
587 // Find or make the output section. The output section is selected
588 // based on the section name, type, and flags.
589 return this->get_output_section(name
, name_key
, type
, flags
, order
, is_relro
);
592 // Return the output section to use for input section SHNDX, with name
593 // NAME, with header HEADER, from object OBJECT. RELOC_SHNDX is the
594 // index of a relocation section which applies to this section, or 0
595 // if none, or -1U if more than one. RELOC_TYPE is the type of the
596 // relocation section if there is one. Set *OFF to the offset of this
597 // input section without the output section. Return NULL if the
598 // section should be discarded. Set *OFF to -1 if the section
599 // contents should not be written directly to the output file, but
600 // will instead receive special handling.
602 template<int size
, bool big_endian
>
604 Layout::layout(Sized_relobj
<size
, big_endian
>* object
, unsigned int shndx
,
605 const char* name
, const elfcpp::Shdr
<size
, big_endian
>& shdr
,
606 unsigned int reloc_shndx
, unsigned int, off_t
* off
)
610 if (!this->include_section(object
, name
, shdr
))
615 // Sometimes .init_array*, .preinit_array* and .fini_array* do not have
616 // correct section types. Force them here.
617 elfcpp::Elf_Word sh_type
= shdr
.get_sh_type();
618 if (sh_type
== elfcpp::SHT_PROGBITS
)
620 static const char init_array_prefix
[] = ".init_array";
621 static const char preinit_array_prefix
[] = ".preinit_array";
622 static const char fini_array_prefix
[] = ".fini_array";
623 static size_t init_array_prefix_size
= sizeof(init_array_prefix
) - 1;
624 static size_t preinit_array_prefix_size
=
625 sizeof(preinit_array_prefix
) - 1;
626 static size_t fini_array_prefix_size
= sizeof(fini_array_prefix
) - 1;
628 if (strncmp(name
, init_array_prefix
, init_array_prefix_size
) == 0)
629 sh_type
= elfcpp::SHT_INIT_ARRAY
;
630 else if (strncmp(name
, preinit_array_prefix
, preinit_array_prefix_size
)
632 sh_type
= elfcpp::SHT_PREINIT_ARRAY
;
633 else if (strncmp(name
, fini_array_prefix
, fini_array_prefix_size
) == 0)
634 sh_type
= elfcpp::SHT_FINI_ARRAY
;
637 // In a relocatable link a grouped section must not be combined with
638 // any other sections.
639 if (parameters
->options().relocatable()
640 && (shdr
.get_sh_flags() & elfcpp::SHF_GROUP
) != 0)
642 name
= this->namepool_
.add(name
, true, NULL
);
643 os
= this->make_output_section(name
, sh_type
, shdr
.get_sh_flags(),
644 ORDER_INVALID
, false);
648 os
= this->choose_output_section(object
, name
, sh_type
,
649 shdr
.get_sh_flags(), true,
650 ORDER_INVALID
, false);
655 // By default the GNU linker sorts input sections whose names match
656 // .ctor.*, .dtor.*, .init_array.*, or .fini_array.*. The sections
657 // are sorted by name. This is used to implement constructor
658 // priority ordering. We are compatible.
659 if (!this->script_options_
->saw_sections_clause()
660 && (is_prefix_of(".ctors.", name
)
661 || is_prefix_of(".dtors.", name
)
662 || is_prefix_of(".init_array.", name
)
663 || is_prefix_of(".fini_array.", name
)))
664 os
->set_must_sort_attached_input_sections();
666 // FIXME: Handle SHF_LINK_ORDER somewhere.
668 *off
= os
->add_input_section(this, object
, shndx
, name
, shdr
, reloc_shndx
,
669 this->script_options_
->saw_sections_clause());
670 this->have_added_input_section_
= true;
675 // Handle a relocation section when doing a relocatable link.
677 template<int size
, bool big_endian
>
679 Layout::layout_reloc(Sized_relobj
<size
, big_endian
>* object
,
681 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
682 Output_section
* data_section
,
683 Relocatable_relocs
* rr
)
685 gold_assert(parameters
->options().relocatable()
686 || parameters
->options().emit_relocs());
688 int sh_type
= shdr
.get_sh_type();
691 if (sh_type
== elfcpp::SHT_REL
)
693 else if (sh_type
== elfcpp::SHT_RELA
)
697 name
+= data_section
->name();
699 // In a relocatable link relocs for a grouped section must not be
700 // combined with other reloc sections.
702 if (!parameters
->options().relocatable()
703 || (data_section
->flags() & elfcpp::SHF_GROUP
) == 0)
704 os
= this->choose_output_section(object
, name
.c_str(), sh_type
,
705 shdr
.get_sh_flags(), false,
706 ORDER_INVALID
, false);
709 const char* n
= this->namepool_
.add(name
.c_str(), true, NULL
);
710 os
= this->make_output_section(n
, sh_type
, shdr
.get_sh_flags(),
711 ORDER_INVALID
, false);
714 os
->set_should_link_to_symtab();
715 os
->set_info_section(data_section
);
717 Output_section_data
* posd
;
718 if (sh_type
== elfcpp::SHT_REL
)
720 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rel_size
);
721 posd
= new Output_relocatable_relocs
<elfcpp::SHT_REL
,
725 else if (sh_type
== elfcpp::SHT_RELA
)
727 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rela_size
);
728 posd
= new Output_relocatable_relocs
<elfcpp::SHT_RELA
,
735 os
->add_output_section_data(posd
);
736 rr
->set_output_data(posd
);
741 // Handle a group section when doing a relocatable link.
743 template<int size
, bool big_endian
>
745 Layout::layout_group(Symbol_table
* symtab
,
746 Sized_relobj
<size
, big_endian
>* object
,
748 const char* group_section_name
,
749 const char* signature
,
750 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
751 elfcpp::Elf_Word flags
,
752 std::vector
<unsigned int>* shndxes
)
754 gold_assert(parameters
->options().relocatable());
755 gold_assert(shdr
.get_sh_type() == elfcpp::SHT_GROUP
);
756 group_section_name
= this->namepool_
.add(group_section_name
, true, NULL
);
757 Output_section
* os
= this->make_output_section(group_section_name
,
760 ORDER_INVALID
, false);
762 // We need to find a symbol with the signature in the symbol table.
763 // If we don't find one now, we need to look again later.
764 Symbol
* sym
= symtab
->lookup(signature
, NULL
);
766 os
->set_info_symndx(sym
);
769 // Reserve some space to minimize reallocations.
770 if (this->group_signatures_
.empty())
771 this->group_signatures_
.reserve(this->number_of_input_files_
* 16);
773 // We will wind up using a symbol whose name is the signature.
774 // So just put the signature in the symbol name pool to save it.
775 signature
= symtab
->canonicalize_name(signature
);
776 this->group_signatures_
.push_back(Group_signature(os
, signature
));
779 os
->set_should_link_to_symtab();
782 section_size_type entry_count
=
783 convert_to_section_size_type(shdr
.get_sh_size() / 4);
784 Output_section_data
* posd
=
785 new Output_data_group
<size
, big_endian
>(object
, entry_count
, flags
,
787 os
->add_output_section_data(posd
);
790 // Special GNU handling of sections name .eh_frame. They will
791 // normally hold exception frame data as defined by the C++ ABI
792 // (http://codesourcery.com/cxx-abi/).
794 template<int size
, bool big_endian
>
796 Layout::layout_eh_frame(Sized_relobj
<size
, big_endian
>* object
,
797 const unsigned char* symbols
,
799 const unsigned char* symbol_names
,
800 off_t symbol_names_size
,
802 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
803 unsigned int reloc_shndx
, unsigned int reloc_type
,
806 gold_assert(shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
);
807 gold_assert((shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) != 0);
809 const char* const name
= ".eh_frame";
810 Output_section
* os
= this->choose_output_section(object
, name
,
811 elfcpp::SHT_PROGBITS
,
812 elfcpp::SHF_ALLOC
, false,
813 ORDER_EHFRAME
, false);
817 if (this->eh_frame_section_
== NULL
)
819 this->eh_frame_section_
= os
;
820 this->eh_frame_data_
= new Eh_frame();
822 if (parameters
->options().eh_frame_hdr())
824 Output_section
* hdr_os
=
825 this->choose_output_section(NULL
, ".eh_frame_hdr",
826 elfcpp::SHT_PROGBITS
,
827 elfcpp::SHF_ALLOC
, false,
828 ORDER_EHFRAME
, false);
832 Eh_frame_hdr
* hdr_posd
= new Eh_frame_hdr(os
,
833 this->eh_frame_data_
);
834 hdr_os
->add_output_section_data(hdr_posd
);
836 hdr_os
->set_after_input_sections();
838 if (!this->script_options_
->saw_phdrs_clause())
840 Output_segment
* hdr_oseg
;
841 hdr_oseg
= this->make_output_segment(elfcpp::PT_GNU_EH_FRAME
,
843 hdr_oseg
->add_output_section_to_nonload(hdr_os
,
847 this->eh_frame_data_
->set_eh_frame_hdr(hdr_posd
);
852 gold_assert(this->eh_frame_section_
== os
);
854 if (this->eh_frame_data_
->add_ehframe_input_section(object
,
863 os
->update_flags_for_input_section(shdr
.get_sh_flags());
865 // We found a .eh_frame section we are going to optimize, so now
866 // we can add the set of optimized sections to the output
867 // section. We need to postpone adding this until we've found a
868 // section we can optimize so that the .eh_frame section in
869 // crtbegin.o winds up at the start of the output section.
870 if (!this->added_eh_frame_data_
)
872 os
->add_output_section_data(this->eh_frame_data_
);
873 this->added_eh_frame_data_
= true;
879 // We couldn't handle this .eh_frame section for some reason.
880 // Add it as a normal section.
881 bool saw_sections_clause
= this->script_options_
->saw_sections_clause();
882 *off
= os
->add_input_section(this, object
, shndx
, name
, shdr
, reloc_shndx
,
883 saw_sections_clause
);
884 this->have_added_input_section_
= true;
890 // Add POSD to an output section using NAME, TYPE, and FLAGS. Return
891 // the output section.
894 Layout::add_output_section_data(const char* name
, elfcpp::Elf_Word type
,
895 elfcpp::Elf_Xword flags
,
896 Output_section_data
* posd
,
897 Output_section_order order
, bool is_relro
)
899 Output_section
* os
= this->choose_output_section(NULL
, name
, type
, flags
,
900 false, order
, is_relro
);
902 os
->add_output_section_data(posd
);
906 // Map section flags to segment flags.
909 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags
)
911 elfcpp::Elf_Word ret
= elfcpp::PF_R
;
912 if ((flags
& elfcpp::SHF_WRITE
) != 0)
914 if ((flags
& elfcpp::SHF_EXECINSTR
) != 0)
919 // Sometimes we compress sections. This is typically done for
920 // sections that are not part of normal program execution (such as
921 // .debug_* sections), and where the readers of these sections know
922 // how to deal with compressed sections. This routine doesn't say for
923 // certain whether we'll compress -- it depends on commandline options
924 // as well -- just whether this section is a candidate for compression.
925 // (The Output_compressed_section class decides whether to compress
926 // a given section, and picks the name of the compressed section.)
929 is_compressible_debug_section(const char* secname
)
931 return (is_prefix_of(".debug", secname
));
934 // We may see compressed debug sections in input files. Return TRUE
935 // if this is the name of a compressed debug section.
938 is_compressed_debug_section(const char* secname
)
940 return (is_prefix_of(".zdebug", secname
));
943 // Make a new Output_section, and attach it to segments as
944 // appropriate. ORDER is the order in which this section should
945 // appear in the output segment. IS_RELRO is true if this is a relro
946 // (read-only after relocations) section.
949 Layout::make_output_section(const char* name
, elfcpp::Elf_Word type
,
950 elfcpp::Elf_Xword flags
,
951 Output_section_order order
, bool is_relro
)
954 if ((flags
& elfcpp::SHF_ALLOC
) == 0
955 && strcmp(parameters
->options().compress_debug_sections(), "none") != 0
956 && is_compressible_debug_section(name
))
957 os
= new Output_compressed_section(¶meters
->options(), name
, type
,
959 else if ((flags
& elfcpp::SHF_ALLOC
) == 0
960 && parameters
->options().strip_debug_non_line()
961 && strcmp(".debug_abbrev", name
) == 0)
963 os
= this->debug_abbrev_
= new Output_reduced_debug_abbrev_section(
965 if (this->debug_info_
)
966 this->debug_info_
->set_abbreviations(this->debug_abbrev_
);
968 else if ((flags
& elfcpp::SHF_ALLOC
) == 0
969 && parameters
->options().strip_debug_non_line()
970 && strcmp(".debug_info", name
) == 0)
972 os
= this->debug_info_
= new Output_reduced_debug_info_section(
974 if (this->debug_abbrev_
)
975 this->debug_info_
->set_abbreviations(this->debug_abbrev_
);
979 // FIXME: const_cast is ugly.
980 Target
* target
= const_cast<Target
*>(¶meters
->target());
981 os
= target
->make_output_section(name
, type
, flags
);
984 // With -z relro, we have to recognize the special sections by name.
985 // There is no other way.
986 bool is_relro_local
= false;
987 if (!this->script_options_
->saw_sections_clause()
988 && parameters
->options().relro()
989 && type
== elfcpp::SHT_PROGBITS
990 && (flags
& elfcpp::SHF_ALLOC
) != 0
991 && (flags
& elfcpp::SHF_WRITE
) != 0)
993 if (strcmp(name
, ".data.rel.ro") == 0)
995 else if (strcmp(name
, ".data.rel.ro.local") == 0)
998 is_relro_local
= true;
1000 else if (type
== elfcpp::SHT_INIT_ARRAY
1001 || type
== elfcpp::SHT_FINI_ARRAY
1002 || type
== elfcpp::SHT_PREINIT_ARRAY
)
1004 else if (strcmp(name
, ".ctors") == 0
1005 || strcmp(name
, ".dtors") == 0
1006 || strcmp(name
, ".jcr") == 0)
1013 if (order
== ORDER_INVALID
&& (flags
& elfcpp::SHF_ALLOC
) != 0)
1014 order
= this->default_section_order(os
, is_relro_local
);
1016 os
->set_order(order
);
1018 parameters
->target().new_output_section(os
);
1020 this->section_list_
.push_back(os
);
1022 // The GNU linker by default sorts some sections by priority, so we
1023 // do the same. We need to know that this might happen before we
1024 // attach any input sections.
1025 if (!this->script_options_
->saw_sections_clause()
1026 && (strcmp(name
, ".ctors") == 0
1027 || strcmp(name
, ".dtors") == 0
1028 || strcmp(name
, ".init_array") == 0
1029 || strcmp(name
, ".fini_array") == 0))
1030 os
->set_may_sort_attached_input_sections();
1032 // Check for .stab*str sections, as .stab* sections need to link to
1034 if (type
== elfcpp::SHT_STRTAB
1035 && !this->have_stabstr_section_
1036 && strncmp(name
, ".stab", 5) == 0
1037 && strcmp(name
+ strlen(name
) - 3, "str") == 0)
1038 this->have_stabstr_section_
= true;
1040 // If we have already attached the sections to segments, then we
1041 // need to attach this one now. This happens for sections created
1042 // directly by the linker.
1043 if (this->sections_are_attached_
)
1044 this->attach_section_to_segment(os
);
1049 // Return the default order in which a section should be placed in an
1050 // output segment. This function captures a lot of the ideas in
1051 // ld/scripttempl/elf.sc in the GNU linker. Note that the order of a
1052 // linker created section is normally set when the section is created;
1053 // this function is used for input sections.
1055 Output_section_order
1056 Layout::default_section_order(Output_section
* os
, bool is_relro_local
)
1058 gold_assert((os
->flags() & elfcpp::SHF_ALLOC
) != 0);
1059 bool is_write
= (os
->flags() & elfcpp::SHF_WRITE
) != 0;
1060 bool is_execinstr
= (os
->flags() & elfcpp::SHF_EXECINSTR
) != 0;
1061 bool is_bss
= false;
1066 case elfcpp::SHT_PROGBITS
:
1068 case elfcpp::SHT_NOBITS
:
1071 case elfcpp::SHT_RELA
:
1072 case elfcpp::SHT_REL
:
1074 return ORDER_DYNAMIC_RELOCS
;
1076 case elfcpp::SHT_HASH
:
1077 case elfcpp::SHT_DYNAMIC
:
1078 case elfcpp::SHT_SHLIB
:
1079 case elfcpp::SHT_DYNSYM
:
1080 case elfcpp::SHT_GNU_HASH
:
1081 case elfcpp::SHT_GNU_verdef
:
1082 case elfcpp::SHT_GNU_verneed
:
1083 case elfcpp::SHT_GNU_versym
:
1085 return ORDER_DYNAMIC_LINKER
;
1087 case elfcpp::SHT_NOTE
:
1088 return is_write
? ORDER_RW_NOTE
: ORDER_RO_NOTE
;
1091 if ((os
->flags() & elfcpp::SHF_TLS
) != 0)
1092 return is_bss
? ORDER_TLS_BSS
: ORDER_TLS_DATA
;
1094 if (!is_bss
&& !is_write
)
1098 if (strcmp(os
->name(), ".init") == 0)
1100 else if (strcmp(os
->name(), ".fini") == 0)
1103 return is_execinstr
? ORDER_TEXT
: ORDER_READONLY
;
1107 return is_relro_local
? ORDER_RELRO_LOCAL
: ORDER_RELRO
;
1109 if (os
->is_small_section())
1110 return is_bss
? ORDER_SMALL_BSS
: ORDER_SMALL_DATA
;
1111 if (os
->is_large_section())
1112 return is_bss
? ORDER_LARGE_BSS
: ORDER_LARGE_DATA
;
1114 return is_bss
? ORDER_BSS
: ORDER_DATA
;
1117 // Attach output sections to segments. This is called after we have
1118 // seen all the input sections.
1121 Layout::attach_sections_to_segments()
1123 for (Section_list::iterator p
= this->section_list_
.begin();
1124 p
!= this->section_list_
.end();
1126 this->attach_section_to_segment(*p
);
1128 this->sections_are_attached_
= true;
1131 // Attach an output section to a segment.
1134 Layout::attach_section_to_segment(Output_section
* os
)
1136 if ((os
->flags() & elfcpp::SHF_ALLOC
) == 0)
1137 this->unattached_section_list_
.push_back(os
);
1139 this->attach_allocated_section_to_segment(os
);
1142 // Attach an allocated output section to a segment.
1145 Layout::attach_allocated_section_to_segment(Output_section
* os
)
1147 elfcpp::Elf_Xword flags
= os
->flags();
1148 gold_assert((flags
& elfcpp::SHF_ALLOC
) != 0);
1150 if (parameters
->options().relocatable())
1153 // If we have a SECTIONS clause, we can't handle the attachment to
1154 // segments until after we've seen all the sections.
1155 if (this->script_options_
->saw_sections_clause())
1158 gold_assert(!this->script_options_
->saw_phdrs_clause());
1160 // This output section goes into a PT_LOAD segment.
1162 elfcpp::Elf_Word seg_flags
= Layout::section_flags_to_segment(flags
);
1164 // Check for --section-start.
1166 bool is_address_set
= parameters
->options().section_start(os
->name(), &addr
);
1168 // In general the only thing we really care about for PT_LOAD
1169 // segments is whether or not they are writable, so that is how we
1170 // search for them. Large data sections also go into their own
1171 // PT_LOAD segment. People who need segments sorted on some other
1172 // basis will have to use a linker script.
1174 Segment_list::const_iterator p
;
1175 for (p
= this->segment_list_
.begin();
1176 p
!= this->segment_list_
.end();
1179 if ((*p
)->type() != elfcpp::PT_LOAD
)
1181 if (!parameters
->options().omagic()
1182 && ((*p
)->flags() & elfcpp::PF_W
) != (seg_flags
& elfcpp::PF_W
))
1184 // If -Tbss was specified, we need to separate the data and BSS
1186 if (parameters
->options().user_set_Tbss())
1188 if ((os
->type() == elfcpp::SHT_NOBITS
)
1189 == (*p
)->has_any_data_sections())
1192 if (os
->is_large_data_section() && !(*p
)->is_large_data_segment())
1197 if ((*p
)->are_addresses_set())
1200 (*p
)->add_initial_output_data(os
);
1201 (*p
)->update_flags_for_output_section(seg_flags
);
1202 (*p
)->set_addresses(addr
, addr
);
1206 (*p
)->add_output_section_to_load(this, os
, seg_flags
);
1210 if (p
== this->segment_list_
.end())
1212 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_LOAD
,
1214 if (os
->is_large_data_section())
1215 oseg
->set_is_large_data_segment();
1216 oseg
->add_output_section_to_load(this, os
, seg_flags
);
1218 oseg
->set_addresses(addr
, addr
);
1221 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
1223 if (os
->type() == elfcpp::SHT_NOTE
)
1225 // See if we already have an equivalent PT_NOTE segment.
1226 for (p
= this->segment_list_
.begin();
1227 p
!= segment_list_
.end();
1230 if ((*p
)->type() == elfcpp::PT_NOTE
1231 && (((*p
)->flags() & elfcpp::PF_W
)
1232 == (seg_flags
& elfcpp::PF_W
)))
1234 (*p
)->add_output_section_to_nonload(os
, seg_flags
);
1239 if (p
== this->segment_list_
.end())
1241 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_NOTE
,
1243 oseg
->add_output_section_to_nonload(os
, seg_flags
);
1247 // If we see a loadable SHF_TLS section, we create a PT_TLS
1248 // segment. There can only be one such segment.
1249 if ((flags
& elfcpp::SHF_TLS
) != 0)
1251 if (this->tls_segment_
== NULL
)
1252 this->make_output_segment(elfcpp::PT_TLS
, seg_flags
);
1253 this->tls_segment_
->add_output_section_to_nonload(os
, seg_flags
);
1256 // If -z relro is in effect, and we see a relro section, we create a
1257 // PT_GNU_RELRO segment. There can only be one such segment.
1258 if (os
->is_relro() && parameters
->options().relro())
1260 gold_assert(seg_flags
== (elfcpp::PF_R
| elfcpp::PF_W
));
1261 if (this->relro_segment_
== NULL
)
1262 this->make_output_segment(elfcpp::PT_GNU_RELRO
, seg_flags
);
1263 this->relro_segment_
->add_output_section_to_nonload(os
, seg_flags
);
1267 // Make an output section for a script.
1270 Layout::make_output_section_for_script(
1272 Script_sections::Section_type section_type
)
1274 name
= this->namepool_
.add(name
, false, NULL
);
1275 elfcpp::Elf_Xword sh_flags
= elfcpp::SHF_ALLOC
;
1276 if (section_type
== Script_sections::ST_NOLOAD
)
1278 Output_section
* os
= this->make_output_section(name
, elfcpp::SHT_PROGBITS
,
1279 sh_flags
, ORDER_INVALID
,
1281 os
->set_found_in_sections_clause();
1282 if (section_type
== Script_sections::ST_NOLOAD
)
1283 os
->set_is_noload();
1287 // Return the number of segments we expect to see.
1290 Layout::expected_segment_count() const
1292 size_t ret
= this->segment_list_
.size();
1294 // If we didn't see a SECTIONS clause in a linker script, we should
1295 // already have the complete list of segments. Otherwise we ask the
1296 // SECTIONS clause how many segments it expects, and add in the ones
1297 // we already have (PT_GNU_STACK, PT_GNU_EH_FRAME, etc.)
1299 if (!this->script_options_
->saw_sections_clause())
1303 const Script_sections
* ss
= this->script_options_
->script_sections();
1304 return ret
+ ss
->expected_segment_count(this);
1308 // Handle the .note.GNU-stack section at layout time. SEEN_GNU_STACK
1309 // is whether we saw a .note.GNU-stack section in the object file.
1310 // GNU_STACK_FLAGS is the section flags. The flags give the
1311 // protection required for stack memory. We record this in an
1312 // executable as a PT_GNU_STACK segment. If an object file does not
1313 // have a .note.GNU-stack segment, we must assume that it is an old
1314 // object. On some targets that will force an executable stack.
1317 Layout::layout_gnu_stack(bool seen_gnu_stack
, uint64_t gnu_stack_flags
)
1319 if (!seen_gnu_stack
)
1320 this->input_without_gnu_stack_note_
= true;
1323 this->input_with_gnu_stack_note_
= true;
1324 if ((gnu_stack_flags
& elfcpp::SHF_EXECINSTR
) != 0)
1325 this->input_requires_executable_stack_
= true;
1329 // Create automatic note sections.
1332 Layout::create_notes()
1334 this->create_gold_note();
1335 this->create_executable_stack_info();
1336 this->create_build_id();
1339 // Create the dynamic sections which are needed before we read the
1343 Layout::create_initial_dynamic_sections(Symbol_table
* symtab
)
1345 if (parameters
->doing_static_link())
1348 this->dynamic_section_
= this->choose_output_section(NULL
, ".dynamic",
1349 elfcpp::SHT_DYNAMIC
,
1351 | elfcpp::SHF_WRITE
),
1355 this->dynamic_symbol_
=
1356 symtab
->define_in_output_data("_DYNAMIC", NULL
, Symbol_table::PREDEFINED
,
1357 this->dynamic_section_
, 0, 0,
1358 elfcpp::STT_OBJECT
, elfcpp::STB_LOCAL
,
1359 elfcpp::STV_HIDDEN
, 0, false, false);
1361 this->dynamic_data_
= new Output_data_dynamic(&this->dynpool_
);
1363 this->dynamic_section_
->add_output_section_data(this->dynamic_data_
);
1366 // For each output section whose name can be represented as C symbol,
1367 // define __start and __stop symbols for the section. This is a GNU
1371 Layout::define_section_symbols(Symbol_table
* symtab
)
1373 for (Section_list::const_iterator p
= this->section_list_
.begin();
1374 p
!= this->section_list_
.end();
1377 const char* const name
= (*p
)->name();
1378 if (is_cident(name
))
1380 const std::string
name_string(name
);
1381 const std::string
start_name(cident_section_start_prefix
1383 const std::string
stop_name(cident_section_stop_prefix
1386 symtab
->define_in_output_data(start_name
.c_str(),
1388 Symbol_table::PREDEFINED
,
1394 elfcpp::STV_DEFAULT
,
1396 false, // offset_is_from_end
1397 true); // only_if_ref
1399 symtab
->define_in_output_data(stop_name
.c_str(),
1401 Symbol_table::PREDEFINED
,
1407 elfcpp::STV_DEFAULT
,
1409 true, // offset_is_from_end
1410 true); // only_if_ref
1415 // Define symbols for group signatures.
1418 Layout::define_group_signatures(Symbol_table
* symtab
)
1420 for (Group_signatures::iterator p
= this->group_signatures_
.begin();
1421 p
!= this->group_signatures_
.end();
1424 Symbol
* sym
= symtab
->lookup(p
->signature
, NULL
);
1426 p
->section
->set_info_symndx(sym
);
1429 // Force the name of the group section to the group
1430 // signature, and use the group's section symbol as the
1431 // signature symbol.
1432 if (strcmp(p
->section
->name(), p
->signature
) != 0)
1434 const char* name
= this->namepool_
.add(p
->signature
,
1436 p
->section
->set_name(name
);
1438 p
->section
->set_needs_symtab_index();
1439 p
->section
->set_info_section_symndx(p
->section
);
1443 this->group_signatures_
.clear();
1446 // Find the first read-only PT_LOAD segment, creating one if
1450 Layout::find_first_load_seg()
1452 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
1453 p
!= this->segment_list_
.end();
1456 if ((*p
)->type() == elfcpp::PT_LOAD
1457 && ((*p
)->flags() & elfcpp::PF_R
) != 0
1458 && (parameters
->options().omagic()
1459 || ((*p
)->flags() & elfcpp::PF_W
) == 0))
1463 gold_assert(!this->script_options_
->saw_phdrs_clause());
1465 Output_segment
* load_seg
= this->make_output_segment(elfcpp::PT_LOAD
,
1470 // Save states of all current output segments. Store saved states
1471 // in SEGMENT_STATES.
1474 Layout::save_segments(Segment_states
* segment_states
)
1476 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
1477 p
!= this->segment_list_
.end();
1480 Output_segment
* segment
= *p
;
1482 Output_segment
* copy
= new Output_segment(*segment
);
1483 (*segment_states
)[segment
] = copy
;
1487 // Restore states of output segments and delete any segment not found in
1491 Layout::restore_segments(const Segment_states
* segment_states
)
1493 // Go through the segment list and remove any segment added in the
1495 this->tls_segment_
= NULL
;
1496 this->relro_segment_
= NULL
;
1497 Segment_list::iterator list_iter
= this->segment_list_
.begin();
1498 while (list_iter
!= this->segment_list_
.end())
1500 Output_segment
* segment
= *list_iter
;
1501 Segment_states::const_iterator states_iter
=
1502 segment_states
->find(segment
);
1503 if (states_iter
!= segment_states
->end())
1505 const Output_segment
* copy
= states_iter
->second
;
1506 // Shallow copy to restore states.
1509 // Also fix up TLS and RELRO segment pointers as appropriate.
1510 if (segment
->type() == elfcpp::PT_TLS
)
1511 this->tls_segment_
= segment
;
1512 else if (segment
->type() == elfcpp::PT_GNU_RELRO
)
1513 this->relro_segment_
= segment
;
1519 list_iter
= this->segment_list_
.erase(list_iter
);
1520 // This is a segment created during section layout. It should be
1521 // safe to remove it since we should have removed all pointers to it.
1527 // Clean up after relaxation so that sections can be laid out again.
1530 Layout::clean_up_after_relaxation()
1532 // Restore the segments to point state just prior to the relaxation loop.
1533 Script_sections
* script_section
= this->script_options_
->script_sections();
1534 script_section
->release_segments();
1535 this->restore_segments(this->segment_states_
);
1537 // Reset section addresses and file offsets
1538 for (Section_list::iterator p
= this->section_list_
.begin();
1539 p
!= this->section_list_
.end();
1542 (*p
)->restore_states();
1544 // If an input section changes size because of relaxation,
1545 // we need to adjust the section offsets of all input sections.
1546 // after such a section.
1547 if ((*p
)->section_offsets_need_adjustment())
1548 (*p
)->adjust_section_offsets();
1550 (*p
)->reset_address_and_file_offset();
1553 // Reset special output object address and file offsets.
1554 for (Data_list::iterator p
= this->special_output_list_
.begin();
1555 p
!= this->special_output_list_
.end();
1557 (*p
)->reset_address_and_file_offset();
1559 // A linker script may have created some output section data objects.
1560 // They are useless now.
1561 for (Output_section_data_list::const_iterator p
=
1562 this->script_output_section_data_list_
.begin();
1563 p
!= this->script_output_section_data_list_
.end();
1566 this->script_output_section_data_list_
.clear();
1569 // Prepare for relaxation.
1572 Layout::prepare_for_relaxation()
1574 // Create an relaxation debug check if in debugging mode.
1575 if (is_debugging_enabled(DEBUG_RELAXATION
))
1576 this->relaxation_debug_check_
= new Relaxation_debug_check();
1578 // Save segment states.
1579 this->segment_states_
= new Segment_states();
1580 this->save_segments(this->segment_states_
);
1582 for(Section_list::const_iterator p
= this->section_list_
.begin();
1583 p
!= this->section_list_
.end();
1585 (*p
)->save_states();
1587 if (is_debugging_enabled(DEBUG_RELAXATION
))
1588 this->relaxation_debug_check_
->check_output_data_for_reset_values(
1589 this->section_list_
, this->special_output_list_
);
1591 // Also enable recording of output section data from scripts.
1592 this->record_output_section_data_from_script_
= true;
1595 // Relaxation loop body: If target has no relaxation, this runs only once
1596 // Otherwise, the target relaxation hook is called at the end of
1597 // each iteration. If the hook returns true, it means re-layout of
1598 // section is required.
1600 // The number of segments created by a linking script without a PHDRS
1601 // clause may be affected by section sizes and alignments. There is
1602 // a remote chance that relaxation causes different number of PT_LOAD
1603 // segments are created and sections are attached to different segments.
1604 // Therefore, we always throw away all segments created during section
1605 // layout. In order to be able to restart the section layout, we keep
1606 // a copy of the segment list right before the relaxation loop and use
1607 // that to restore the segments.
1609 // PASS is the current relaxation pass number.
1610 // SYMTAB is a symbol table.
1611 // PLOAD_SEG is the address of a pointer for the load segment.
1612 // PHDR_SEG is a pointer to the PHDR segment.
1613 // SEGMENT_HEADERS points to the output segment header.
1614 // FILE_HEADER points to the output file header.
1615 // PSHNDX is the address to store the output section index.
1618 Layout::relaxation_loop_body(
1621 Symbol_table
* symtab
,
1622 Output_segment
** pload_seg
,
1623 Output_segment
* phdr_seg
,
1624 Output_segment_headers
* segment_headers
,
1625 Output_file_header
* file_header
,
1626 unsigned int* pshndx
)
1628 // If this is not the first iteration, we need to clean up after
1629 // relaxation so that we can lay out the sections again.
1631 this->clean_up_after_relaxation();
1633 // If there is a SECTIONS clause, put all the input sections into
1634 // the required order.
1635 Output_segment
* load_seg
;
1636 if (this->script_options_
->saw_sections_clause())
1637 load_seg
= this->set_section_addresses_from_script(symtab
);
1638 else if (parameters
->options().relocatable())
1641 load_seg
= this->find_first_load_seg();
1643 if (parameters
->options().oformat_enum()
1644 != General_options::OBJECT_FORMAT_ELF
)
1647 // If the user set the address of the text segment, that may not be
1648 // compatible with putting the segment headers and file headers into
1650 if (parameters
->options().user_set_Ttext())
1653 gold_assert(phdr_seg
== NULL
1655 || this->script_options_
->saw_sections_clause());
1657 // If the address of the load segment we found has been set by
1658 // --section-start rather than by a script, then we don't want to
1659 // use it for the file and segment headers.
1660 if (load_seg
!= NULL
1661 && load_seg
->are_addresses_set()
1662 && !this->script_options_
->saw_sections_clause())
1665 // Lay out the segment headers.
1666 if (!parameters
->options().relocatable())
1668 gold_assert(segment_headers
!= NULL
);
1669 if (load_seg
!= NULL
)
1670 load_seg
->add_initial_output_data(segment_headers
);
1671 if (phdr_seg
!= NULL
)
1672 phdr_seg
->add_initial_output_data(segment_headers
);
1675 // Lay out the file header.
1676 if (load_seg
!= NULL
)
1677 load_seg
->add_initial_output_data(file_header
);
1679 if (this->script_options_
->saw_phdrs_clause()
1680 && !parameters
->options().relocatable())
1682 // Support use of FILEHDRS and PHDRS attachments in a PHDRS
1683 // clause in a linker script.
1684 Script_sections
* ss
= this->script_options_
->script_sections();
1685 ss
->put_headers_in_phdrs(file_header
, segment_headers
);
1688 // We set the output section indexes in set_segment_offsets and
1689 // set_section_indexes.
1692 // Set the file offsets of all the segments, and all the sections
1695 if (!parameters
->options().relocatable())
1696 off
= this->set_segment_offsets(target
, load_seg
, pshndx
);
1698 off
= this->set_relocatable_section_offsets(file_header
, pshndx
);
1700 // Verify that the dummy relaxation does not change anything.
1701 if (is_debugging_enabled(DEBUG_RELAXATION
))
1704 this->relaxation_debug_check_
->read_sections(this->section_list_
);
1706 this->relaxation_debug_check_
->verify_sections(this->section_list_
);
1709 *pload_seg
= load_seg
;
1713 // Search the list of patterns and find the postion of the given section
1714 // name in the output section. If the section name matches a glob
1715 // pattern and a non-glob name, then the non-glob position takes
1716 // precedence. Return 0 if no match is found.
1719 Layout::find_section_order_index(const std::string
& section_name
)
1721 Unordered_map
<std::string
, unsigned int>::iterator map_it
;
1722 map_it
= this->input_section_position_
.find(section_name
);
1723 if (map_it
!= this->input_section_position_
.end())
1724 return map_it
->second
;
1726 // Absolute match failed. Linear search the glob patterns.
1727 std::vector
<std::string
>::iterator it
;
1728 for (it
= this->input_section_glob_
.begin();
1729 it
!= this->input_section_glob_
.end();
1732 if (fnmatch((*it
).c_str(), section_name
.c_str(), FNM_NOESCAPE
) == 0)
1734 map_it
= this->input_section_position_
.find(*it
);
1735 gold_assert(map_it
!= this->input_section_position_
.end());
1736 return map_it
->second
;
1742 // Read the sequence of input sections from the file specified with
1743 // --section-ordering-file.
1746 Layout::read_layout_from_file()
1748 const char* filename
= parameters
->options().section_ordering_file();
1754 gold_fatal(_("unable to open --section-ordering-file file %s: %s"),
1755 filename
, strerror(errno
));
1757 std::getline(in
, line
); // this chops off the trailing \n, if any
1758 unsigned int position
= 1;
1762 if (!line
.empty() && line
[line
.length() - 1] == '\r') // Windows
1763 line
.resize(line
.length() - 1);
1764 // Ignore comments, beginning with '#'
1767 std::getline(in
, line
);
1770 this->input_section_position_
[line
] = position
;
1771 // Store all glob patterns in a vector.
1772 if (is_wildcard_string(line
.c_str()))
1773 this->input_section_glob_
.push_back(line
);
1775 std::getline(in
, line
);
1779 // Finalize the layout. When this is called, we have created all the
1780 // output sections and all the output segments which are based on
1781 // input sections. We have several things to do, and we have to do
1782 // them in the right order, so that we get the right results correctly
1785 // 1) Finalize the list of output segments and create the segment
1788 // 2) Finalize the dynamic symbol table and associated sections.
1790 // 3) Determine the final file offset of all the output segments.
1792 // 4) Determine the final file offset of all the SHF_ALLOC output
1795 // 5) Create the symbol table sections and the section name table
1798 // 6) Finalize the symbol table: set symbol values to their final
1799 // value and make a final determination of which symbols are going
1800 // into the output symbol table.
1802 // 7) Create the section table header.
1804 // 8) Determine the final file offset of all the output sections which
1805 // are not SHF_ALLOC, including the section table header.
1807 // 9) Finalize the ELF file header.
1809 // This function returns the size of the output file.
1812 Layout::finalize(const Input_objects
* input_objects
, Symbol_table
* symtab
,
1813 Target
* target
, const Task
* task
)
1815 target
->finalize_sections(this, input_objects
, symtab
);
1817 this->count_local_symbols(task
, input_objects
);
1819 this->link_stabs_sections();
1821 Output_segment
* phdr_seg
= NULL
;
1822 if (!parameters
->options().relocatable() && !parameters
->doing_static_link())
1824 // There was a dynamic object in the link. We need to create
1825 // some information for the dynamic linker.
1827 // Create the PT_PHDR segment which will hold the program
1829 if (!this->script_options_
->saw_phdrs_clause())
1830 phdr_seg
= this->make_output_segment(elfcpp::PT_PHDR
, elfcpp::PF_R
);
1832 // Create the dynamic symbol table, including the hash table.
1833 Output_section
* dynstr
;
1834 std::vector
<Symbol
*> dynamic_symbols
;
1835 unsigned int local_dynamic_count
;
1836 Versions
versions(*this->script_options()->version_script_info(),
1838 this->create_dynamic_symtab(input_objects
, symtab
, &dynstr
,
1839 &local_dynamic_count
, &dynamic_symbols
,
1842 // Create the .interp section to hold the name of the
1843 // interpreter, and put it in a PT_INTERP segment.
1844 if (!parameters
->options().shared())
1845 this->create_interp(target
);
1847 // Finish the .dynamic section to hold the dynamic data, and put
1848 // it in a PT_DYNAMIC segment.
1849 this->finish_dynamic_section(input_objects
, symtab
);
1851 // We should have added everything we need to the dynamic string
1853 this->dynpool_
.set_string_offsets();
1855 // Create the version sections. We can't do this until the
1856 // dynamic string table is complete.
1857 this->create_version_sections(&versions
, symtab
, local_dynamic_count
,
1858 dynamic_symbols
, dynstr
);
1860 // Set the size of the _DYNAMIC symbol. We can't do this until
1861 // after we call create_version_sections.
1862 this->set_dynamic_symbol_size(symtab
);
1865 if (this->incremental_inputs_
)
1867 this->incremental_inputs_
->finalize();
1868 this->create_incremental_info_sections();
1871 // Create segment headers.
1872 Output_segment_headers
* segment_headers
=
1873 (parameters
->options().relocatable()
1875 : new Output_segment_headers(this->segment_list_
));
1877 // Lay out the file header.
1878 Output_file_header
* file_header
1879 = new Output_file_header(target
, symtab
, segment_headers
,
1880 parameters
->options().entry());
1882 this->special_output_list_
.push_back(file_header
);
1883 if (segment_headers
!= NULL
)
1884 this->special_output_list_
.push_back(segment_headers
);
1886 // Find approriate places for orphan output sections if we are using
1888 if (this->script_options_
->saw_sections_clause())
1889 this->place_orphan_sections_in_script();
1891 Output_segment
* load_seg
;
1896 // Take a snapshot of the section layout as needed.
1897 if (target
->may_relax())
1898 this->prepare_for_relaxation();
1900 // Run the relaxation loop to lay out sections.
1903 off
= this->relaxation_loop_body(pass
, target
, symtab
, &load_seg
,
1904 phdr_seg
, segment_headers
, file_header
,
1908 while (target
->may_relax()
1909 && target
->relax(pass
, input_objects
, symtab
, this));
1911 // Set the file offsets of all the non-data sections we've seen so
1912 // far which don't have to wait for the input sections. We need
1913 // this in order to finalize local symbols in non-allocated
1915 off
= this->set_section_offsets(off
, BEFORE_INPUT_SECTIONS_PASS
);
1917 // Set the section indexes of all unallocated sections seen so far,
1918 // in case any of them are somehow referenced by a symbol.
1919 shndx
= this->set_section_indexes(shndx
);
1921 // Create the symbol table sections.
1922 this->create_symtab_sections(input_objects
, symtab
, shndx
, &off
);
1923 if (!parameters
->doing_static_link())
1924 this->assign_local_dynsym_offsets(input_objects
);
1926 // Process any symbol assignments from a linker script. This must
1927 // be called after the symbol table has been finalized.
1928 this->script_options_
->finalize_symbols(symtab
, this);
1930 // Create the .shstrtab section.
1931 Output_section
* shstrtab_section
= this->create_shstrtab();
1933 // Set the file offsets of the rest of the non-data sections which
1934 // don't have to wait for the input sections.
1935 off
= this->set_section_offsets(off
, BEFORE_INPUT_SECTIONS_PASS
);
1937 // Now that all sections have been created, set the section indexes
1938 // for any sections which haven't been done yet.
1939 shndx
= this->set_section_indexes(shndx
);
1941 // Create the section table header.
1942 this->create_shdrs(shstrtab_section
, &off
);
1944 // If there are no sections which require postprocessing, we can
1945 // handle the section names now, and avoid a resize later.
1946 if (!this->any_postprocessing_sections_
)
1947 off
= this->set_section_offsets(off
,
1948 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
);
1950 file_header
->set_section_info(this->section_headers_
, shstrtab_section
);
1952 // Now we know exactly where everything goes in the output file
1953 // (except for non-allocated sections which require postprocessing).
1954 Output_data::layout_complete();
1956 this->output_file_size_
= off
;
1961 // Create a note header following the format defined in the ELF ABI.
1962 // NAME is the name, NOTE_TYPE is the type, SECTION_NAME is the name
1963 // of the section to create, DESCSZ is the size of the descriptor.
1964 // ALLOCATE is true if the section should be allocated in memory.
1965 // This returns the new note section. It sets *TRAILING_PADDING to
1966 // the number of trailing zero bytes required.
1969 Layout::create_note(const char* name
, int note_type
,
1970 const char* section_name
, size_t descsz
,
1971 bool allocate
, size_t* trailing_padding
)
1973 // Authorities all agree that the values in a .note field should
1974 // be aligned on 4-byte boundaries for 32-bit binaries. However,
1975 // they differ on what the alignment is for 64-bit binaries.
1976 // The GABI says unambiguously they take 8-byte alignment:
1977 // http://sco.com/developers/gabi/latest/ch5.pheader.html#note_section
1978 // Other documentation says alignment should always be 4 bytes:
1979 // http://www.netbsd.org/docs/kernel/elf-notes.html#note-format
1980 // GNU ld and GNU readelf both support the latter (at least as of
1981 // version 2.16.91), and glibc always generates the latter for
1982 // .note.ABI-tag (as of version 1.6), so that's the one we go with
1984 #ifdef GABI_FORMAT_FOR_DOTNOTE_SECTION // This is not defined by default.
1985 const int size
= parameters
->target().get_size();
1987 const int size
= 32;
1990 // The contents of the .note section.
1991 size_t namesz
= strlen(name
) + 1;
1992 size_t aligned_namesz
= align_address(namesz
, size
/ 8);
1993 size_t aligned_descsz
= align_address(descsz
, size
/ 8);
1995 size_t notehdrsz
= 3 * (size
/ 8) + aligned_namesz
;
1997 unsigned char* buffer
= new unsigned char[notehdrsz
];
1998 memset(buffer
, 0, notehdrsz
);
2000 bool is_big_endian
= parameters
->target().is_big_endian();
2006 elfcpp::Swap
<32, false>::writeval(buffer
, namesz
);
2007 elfcpp::Swap
<32, false>::writeval(buffer
+ 4, descsz
);
2008 elfcpp::Swap
<32, false>::writeval(buffer
+ 8, note_type
);
2012 elfcpp::Swap
<32, true>::writeval(buffer
, namesz
);
2013 elfcpp::Swap
<32, true>::writeval(buffer
+ 4, descsz
);
2014 elfcpp::Swap
<32, true>::writeval(buffer
+ 8, note_type
);
2017 else if (size
== 64)
2021 elfcpp::Swap
<64, false>::writeval(buffer
, namesz
);
2022 elfcpp::Swap
<64, false>::writeval(buffer
+ 8, descsz
);
2023 elfcpp::Swap
<64, false>::writeval(buffer
+ 16, note_type
);
2027 elfcpp::Swap
<64, true>::writeval(buffer
, namesz
);
2028 elfcpp::Swap
<64, true>::writeval(buffer
+ 8, descsz
);
2029 elfcpp::Swap
<64, true>::writeval(buffer
+ 16, note_type
);
2035 memcpy(buffer
+ 3 * (size
/ 8), name
, namesz
);
2037 elfcpp::Elf_Xword flags
= 0;
2038 Output_section_order order
= ORDER_INVALID
;
2041 flags
= elfcpp::SHF_ALLOC
;
2042 order
= ORDER_RO_NOTE
;
2044 Output_section
* os
= this->choose_output_section(NULL
, section_name
,
2046 flags
, false, order
, false);
2050 Output_section_data
* posd
= new Output_data_const_buffer(buffer
, notehdrsz
,
2053 os
->add_output_section_data(posd
);
2055 *trailing_padding
= aligned_descsz
- descsz
;
2060 // For an executable or shared library, create a note to record the
2061 // version of gold used to create the binary.
2064 Layout::create_gold_note()
2066 if (parameters
->options().relocatable())
2069 std::string desc
= std::string("gold ") + gold::get_version_string();
2071 size_t trailing_padding
;
2072 Output_section
*os
= this->create_note("GNU", elfcpp::NT_GNU_GOLD_VERSION
,
2073 ".note.gnu.gold-version", desc
.size(),
2074 false, &trailing_padding
);
2078 Output_section_data
* posd
= new Output_data_const(desc
, 4);
2079 os
->add_output_section_data(posd
);
2081 if (trailing_padding
> 0)
2083 posd
= new Output_data_zero_fill(trailing_padding
, 0);
2084 os
->add_output_section_data(posd
);
2088 // Record whether the stack should be executable. This can be set
2089 // from the command line using the -z execstack or -z noexecstack
2090 // options. Otherwise, if any input file has a .note.GNU-stack
2091 // section with the SHF_EXECINSTR flag set, the stack should be
2092 // executable. Otherwise, if at least one input file a
2093 // .note.GNU-stack section, and some input file has no .note.GNU-stack
2094 // section, we use the target default for whether the stack should be
2095 // executable. Otherwise, we don't generate a stack note. When
2096 // generating a object file, we create a .note.GNU-stack section with
2097 // the appropriate marking. When generating an executable or shared
2098 // library, we create a PT_GNU_STACK segment.
2101 Layout::create_executable_stack_info()
2103 bool is_stack_executable
;
2104 if (parameters
->options().is_execstack_set())
2105 is_stack_executable
= parameters
->options().is_stack_executable();
2106 else if (!this->input_with_gnu_stack_note_
)
2110 if (this->input_requires_executable_stack_
)
2111 is_stack_executable
= true;
2112 else if (this->input_without_gnu_stack_note_
)
2113 is_stack_executable
=
2114 parameters
->target().is_default_stack_executable();
2116 is_stack_executable
= false;
2119 if (parameters
->options().relocatable())
2121 const char* name
= this->namepool_
.add(".note.GNU-stack", false, NULL
);
2122 elfcpp::Elf_Xword flags
= 0;
2123 if (is_stack_executable
)
2124 flags
|= elfcpp::SHF_EXECINSTR
;
2125 this->make_output_section(name
, elfcpp::SHT_PROGBITS
, flags
,
2126 ORDER_INVALID
, false);
2130 if (this->script_options_
->saw_phdrs_clause())
2132 int flags
= elfcpp::PF_R
| elfcpp::PF_W
;
2133 if (is_stack_executable
)
2134 flags
|= elfcpp::PF_X
;
2135 this->make_output_segment(elfcpp::PT_GNU_STACK
, flags
);
2139 // If --build-id was used, set up the build ID note.
2142 Layout::create_build_id()
2144 if (!parameters
->options().user_set_build_id())
2147 const char* style
= parameters
->options().build_id();
2148 if (strcmp(style
, "none") == 0)
2151 // Set DESCSZ to the size of the note descriptor. When possible,
2152 // set DESC to the note descriptor contents.
2155 if (strcmp(style
, "md5") == 0)
2157 else if (strcmp(style
, "sha1") == 0)
2159 else if (strcmp(style
, "uuid") == 0)
2161 const size_t uuidsz
= 128 / 8;
2163 char buffer
[uuidsz
];
2164 memset(buffer
, 0, uuidsz
);
2166 int descriptor
= open_descriptor(-1, "/dev/urandom", O_RDONLY
);
2168 gold_error(_("--build-id=uuid failed: could not open /dev/urandom: %s"),
2172 ssize_t got
= ::read(descriptor
, buffer
, uuidsz
);
2173 release_descriptor(descriptor
, true);
2175 gold_error(_("/dev/urandom: read failed: %s"), strerror(errno
));
2176 else if (static_cast<size_t>(got
) != uuidsz
)
2177 gold_error(_("/dev/urandom: expected %zu bytes, got %zd bytes"),
2181 desc
.assign(buffer
, uuidsz
);
2184 else if (strncmp(style
, "0x", 2) == 0)
2187 const char* p
= style
+ 2;
2190 if (hex_p(p
[0]) && hex_p(p
[1]))
2192 char c
= (hex_value(p
[0]) << 4) | hex_value(p
[1]);
2196 else if (*p
== '-' || *p
== ':')
2199 gold_fatal(_("--build-id argument '%s' not a valid hex number"),
2202 descsz
= desc
.size();
2205 gold_fatal(_("unrecognized --build-id argument '%s'"), style
);
2208 size_t trailing_padding
;
2209 Output_section
* os
= this->create_note("GNU", elfcpp::NT_GNU_BUILD_ID
,
2210 ".note.gnu.build-id", descsz
, true,
2217 // We know the value already, so we fill it in now.
2218 gold_assert(desc
.size() == descsz
);
2220 Output_section_data
* posd
= new Output_data_const(desc
, 4);
2221 os
->add_output_section_data(posd
);
2223 if (trailing_padding
!= 0)
2225 posd
= new Output_data_zero_fill(trailing_padding
, 0);
2226 os
->add_output_section_data(posd
);
2231 // We need to compute a checksum after we have completed the
2233 gold_assert(trailing_padding
== 0);
2234 this->build_id_note_
= new Output_data_zero_fill(descsz
, 4);
2235 os
->add_output_section_data(this->build_id_note_
);
2239 // If we have both .stabXX and .stabXXstr sections, then the sh_link
2240 // field of the former should point to the latter. I'm not sure who
2241 // started this, but the GNU linker does it, and some tools depend
2245 Layout::link_stabs_sections()
2247 if (!this->have_stabstr_section_
)
2250 for (Section_list::iterator p
= this->section_list_
.begin();
2251 p
!= this->section_list_
.end();
2254 if ((*p
)->type() != elfcpp::SHT_STRTAB
)
2257 const char* name
= (*p
)->name();
2258 if (strncmp(name
, ".stab", 5) != 0)
2261 size_t len
= strlen(name
);
2262 if (strcmp(name
+ len
- 3, "str") != 0)
2265 std::string
stab_name(name
, len
- 3);
2266 Output_section
* stab_sec
;
2267 stab_sec
= this->find_output_section(stab_name
.c_str());
2268 if (stab_sec
!= NULL
)
2269 stab_sec
->set_link_section(*p
);
2273 // Create .gnu_incremental_inputs and .gnu_incremental_strtab sections needed
2274 // for the next run of incremental linking to check what has changed.
2277 Layout::create_incremental_info_sections()
2279 gold_assert(this->incremental_inputs_
!= NULL
);
2281 // Add the .gnu_incremental_inputs section.
2282 const char *incremental_inputs_name
=
2283 this->namepool_
.add(".gnu_incremental_inputs", false, NULL
);
2284 Output_section
* inputs_os
=
2285 this->make_output_section(incremental_inputs_name
,
2286 elfcpp::SHT_GNU_INCREMENTAL_INPUTS
, 0,
2287 ORDER_INVALID
, false);
2288 Output_section_data
* posd
=
2289 this->incremental_inputs_
->create_incremental_inputs_section_data();
2290 inputs_os
->add_output_section_data(posd
);
2292 // Add the .gnu_incremental_strtab section.
2293 const char *incremental_strtab_name
=
2294 this->namepool_
.add(".gnu_incremental_strtab", false, NULL
);
2295 Output_section
* strtab_os
= this->make_output_section(incremental_strtab_name
,
2299 Output_data_strtab
* strtab_data
=
2300 new Output_data_strtab(this->incremental_inputs_
->get_stringpool());
2301 strtab_os
->add_output_section_data(strtab_data
);
2303 inputs_os
->set_link_section(strtab_data
);
2306 // Return whether SEG1 should be before SEG2 in the output file. This
2307 // is based entirely on the segment type and flags. When this is
2308 // called the segment addresses has normally not yet been set.
2311 Layout::segment_precedes(const Output_segment
* seg1
,
2312 const Output_segment
* seg2
)
2314 elfcpp::Elf_Word type1
= seg1
->type();
2315 elfcpp::Elf_Word type2
= seg2
->type();
2317 // The single PT_PHDR segment is required to precede any loadable
2318 // segment. We simply make it always first.
2319 if (type1
== elfcpp::PT_PHDR
)
2321 gold_assert(type2
!= elfcpp::PT_PHDR
);
2324 if (type2
== elfcpp::PT_PHDR
)
2327 // The single PT_INTERP segment is required to precede any loadable
2328 // segment. We simply make it always second.
2329 if (type1
== elfcpp::PT_INTERP
)
2331 gold_assert(type2
!= elfcpp::PT_INTERP
);
2334 if (type2
== elfcpp::PT_INTERP
)
2337 // We then put PT_LOAD segments before any other segments.
2338 if (type1
== elfcpp::PT_LOAD
&& type2
!= elfcpp::PT_LOAD
)
2340 if (type2
== elfcpp::PT_LOAD
&& type1
!= elfcpp::PT_LOAD
)
2343 // We put the PT_TLS segment last except for the PT_GNU_RELRO
2344 // segment, because that is where the dynamic linker expects to find
2345 // it (this is just for efficiency; other positions would also work
2347 if (type1
== elfcpp::PT_TLS
2348 && type2
!= elfcpp::PT_TLS
2349 && type2
!= elfcpp::PT_GNU_RELRO
)
2351 if (type2
== elfcpp::PT_TLS
2352 && type1
!= elfcpp::PT_TLS
2353 && type1
!= elfcpp::PT_GNU_RELRO
)
2356 // We put the PT_GNU_RELRO segment last, because that is where the
2357 // dynamic linker expects to find it (as with PT_TLS, this is just
2359 if (type1
== elfcpp::PT_GNU_RELRO
&& type2
!= elfcpp::PT_GNU_RELRO
)
2361 if (type2
== elfcpp::PT_GNU_RELRO
&& type1
!= elfcpp::PT_GNU_RELRO
)
2364 const elfcpp::Elf_Word flags1
= seg1
->flags();
2365 const elfcpp::Elf_Word flags2
= seg2
->flags();
2367 // The order of non-PT_LOAD segments is unimportant. We simply sort
2368 // by the numeric segment type and flags values. There should not
2369 // be more than one segment with the same type and flags.
2370 if (type1
!= elfcpp::PT_LOAD
)
2373 return type1
< type2
;
2374 gold_assert(flags1
!= flags2
);
2375 return flags1
< flags2
;
2378 // If the addresses are set already, sort by load address.
2379 if (seg1
->are_addresses_set())
2381 if (!seg2
->are_addresses_set())
2384 unsigned int section_count1
= seg1
->output_section_count();
2385 unsigned int section_count2
= seg2
->output_section_count();
2386 if (section_count1
== 0 && section_count2
> 0)
2388 if (section_count1
> 0 && section_count2
== 0)
2391 uint64_t paddr1
= seg1
->first_section_load_address();
2392 uint64_t paddr2
= seg2
->first_section_load_address();
2393 if (paddr1
!= paddr2
)
2394 return paddr1
< paddr2
;
2396 else if (seg2
->are_addresses_set())
2399 // A segment which holds large data comes after a segment which does
2400 // not hold large data.
2401 if (seg1
->is_large_data_segment())
2403 if (!seg2
->is_large_data_segment())
2406 else if (seg2
->is_large_data_segment())
2409 // Otherwise, we sort PT_LOAD segments based on the flags. Readonly
2410 // segments come before writable segments. Then writable segments
2411 // with data come before writable segments without data. Then
2412 // executable segments come before non-executable segments. Then
2413 // the unlikely case of a non-readable segment comes before the
2414 // normal case of a readable segment. If there are multiple
2415 // segments with the same type and flags, we require that the
2416 // address be set, and we sort by virtual address and then physical
2418 if ((flags1
& elfcpp::PF_W
) != (flags2
& elfcpp::PF_W
))
2419 return (flags1
& elfcpp::PF_W
) == 0;
2420 if ((flags1
& elfcpp::PF_W
) != 0
2421 && seg1
->has_any_data_sections() != seg2
->has_any_data_sections())
2422 return seg1
->has_any_data_sections();
2423 if ((flags1
& elfcpp::PF_X
) != (flags2
& elfcpp::PF_X
))
2424 return (flags1
& elfcpp::PF_X
) != 0;
2425 if ((flags1
& elfcpp::PF_R
) != (flags2
& elfcpp::PF_R
))
2426 return (flags1
& elfcpp::PF_R
) == 0;
2428 // We shouldn't get here--we shouldn't create segments which we
2429 // can't distinguish.
2433 // Increase OFF so that it is congruent to ADDR modulo ABI_PAGESIZE.
2436 align_file_offset(off_t off
, uint64_t addr
, uint64_t abi_pagesize
)
2438 uint64_t unsigned_off
= off
;
2439 uint64_t aligned_off
= ((unsigned_off
& ~(abi_pagesize
- 1))
2440 | (addr
& (abi_pagesize
- 1)));
2441 if (aligned_off
< unsigned_off
)
2442 aligned_off
+= abi_pagesize
;
2446 // Set the file offsets of all the segments, and all the sections they
2447 // contain. They have all been created. LOAD_SEG must be be laid out
2448 // first. Return the offset of the data to follow.
2451 Layout::set_segment_offsets(const Target
* target
, Output_segment
* load_seg
,
2452 unsigned int *pshndx
)
2454 // Sort them into the final order.
2455 std::sort(this->segment_list_
.begin(), this->segment_list_
.end(),
2456 Layout::Compare_segments());
2458 // Find the PT_LOAD segments, and set their addresses and offsets
2459 // and their section's addresses and offsets.
2461 if (parameters
->options().user_set_Ttext())
2462 addr
= parameters
->options().Ttext();
2463 else if (parameters
->options().output_is_position_independent())
2466 addr
= target
->default_text_segment_address();
2469 // If LOAD_SEG is NULL, then the file header and segment headers
2470 // will not be loadable. But they still need to be at offset 0 in
2471 // the file. Set their offsets now.
2472 if (load_seg
== NULL
)
2474 for (Data_list::iterator p
= this->special_output_list_
.begin();
2475 p
!= this->special_output_list_
.end();
2478 off
= align_address(off
, (*p
)->addralign());
2479 (*p
)->set_address_and_file_offset(0, off
);
2480 off
+= (*p
)->data_size();
2484 unsigned int increase_relro
= this->increase_relro_
;
2485 if (this->script_options_
->saw_sections_clause())
2488 const bool check_sections
= parameters
->options().check_sections();
2489 Output_segment
* last_load_segment
= NULL
;
2491 bool was_readonly
= false;
2492 for (Segment_list::iterator p
= this->segment_list_
.begin();
2493 p
!= this->segment_list_
.end();
2496 if ((*p
)->type() == elfcpp::PT_LOAD
)
2498 if (load_seg
!= NULL
&& load_seg
!= *p
)
2502 bool are_addresses_set
= (*p
)->are_addresses_set();
2503 if (are_addresses_set
)
2505 // When it comes to setting file offsets, we care about
2506 // the physical address.
2507 addr
= (*p
)->paddr();
2509 else if (parameters
->options().user_set_Tdata()
2510 && ((*p
)->flags() & elfcpp::PF_W
) != 0
2511 && (!parameters
->options().user_set_Tbss()
2512 || (*p
)->has_any_data_sections()))
2514 addr
= parameters
->options().Tdata();
2515 are_addresses_set
= true;
2517 else if (parameters
->options().user_set_Tbss()
2518 && ((*p
)->flags() & elfcpp::PF_W
) != 0
2519 && !(*p
)->has_any_data_sections())
2521 addr
= parameters
->options().Tbss();
2522 are_addresses_set
= true;
2525 uint64_t orig_addr
= addr
;
2526 uint64_t orig_off
= off
;
2528 uint64_t aligned_addr
= 0;
2529 uint64_t abi_pagesize
= target
->abi_pagesize();
2530 uint64_t common_pagesize
= target
->common_pagesize();
2532 if (!parameters
->options().nmagic()
2533 && !parameters
->options().omagic())
2534 (*p
)->set_minimum_p_align(common_pagesize
);
2536 if (!are_addresses_set
)
2538 // If the last segment was readonly, and this one is
2539 // not, then skip the address forward one page,
2540 // maintaining the same position within the page. This
2541 // lets us store both segments overlapping on a single
2542 // page in the file, but the loader will put them on
2543 // different pages in memory.
2545 addr
= align_address(addr
, (*p
)->maximum_alignment());
2546 aligned_addr
= addr
;
2548 if (was_readonly
&& ((*p
)->flags() & elfcpp::PF_W
) != 0)
2550 if ((addr
& (abi_pagesize
- 1)) != 0)
2551 addr
= addr
+ abi_pagesize
;
2554 off
= orig_off
+ ((addr
- orig_addr
) & (abi_pagesize
- 1));
2557 if (!parameters
->options().nmagic()
2558 && !parameters
->options().omagic())
2559 off
= align_file_offset(off
, addr
, abi_pagesize
);
2560 else if (load_seg
== NULL
)
2562 // This is -N or -n with a section script which prevents
2563 // us from using a load segment. We need to ensure that
2564 // the file offset is aligned to the alignment of the
2565 // segment. This is because the linker script
2566 // implicitly assumed a zero offset. If we don't align
2567 // here, then the alignment of the sections in the
2568 // linker script may not match the alignment of the
2569 // sections in the set_section_addresses call below,
2570 // causing an error about dot moving backward.
2571 off
= align_address(off
, (*p
)->maximum_alignment());
2574 unsigned int shndx_hold
= *pshndx
;
2575 uint64_t new_addr
= (*p
)->set_section_addresses(this, false, addr
,
2579 // Now that we know the size of this segment, we may be able
2580 // to save a page in memory, at the cost of wasting some
2581 // file space, by instead aligning to the start of a new
2582 // page. Here we use the real machine page size rather than
2583 // the ABI mandated page size.
2585 if (!are_addresses_set
&& aligned_addr
!= addr
)
2587 uint64_t first_off
= (common_pagesize
2589 & (common_pagesize
- 1)));
2590 uint64_t last_off
= new_addr
& (common_pagesize
- 1);
2593 && ((aligned_addr
& ~ (common_pagesize
- 1))
2594 != (new_addr
& ~ (common_pagesize
- 1)))
2595 && first_off
+ last_off
<= common_pagesize
)
2597 *pshndx
= shndx_hold
;
2598 addr
= align_address(aligned_addr
, common_pagesize
);
2599 addr
= align_address(addr
, (*p
)->maximum_alignment());
2600 off
= orig_off
+ ((addr
- orig_addr
) & (abi_pagesize
- 1));
2601 off
= align_file_offset(off
, addr
, abi_pagesize
);
2602 new_addr
= (*p
)->set_section_addresses(this, true, addr
,
2610 if (((*p
)->flags() & elfcpp::PF_W
) == 0)
2611 was_readonly
= true;
2613 // Implement --check-sections. We know that the segments
2614 // are sorted by LMA.
2615 if (check_sections
&& last_load_segment
!= NULL
)
2617 gold_assert(last_load_segment
->paddr() <= (*p
)->paddr());
2618 if (last_load_segment
->paddr() + last_load_segment
->memsz()
2621 unsigned long long lb1
= last_load_segment
->paddr();
2622 unsigned long long le1
= lb1
+ last_load_segment
->memsz();
2623 unsigned long long lb2
= (*p
)->paddr();
2624 unsigned long long le2
= lb2
+ (*p
)->memsz();
2625 gold_error(_("load segment overlap [0x%llx -> 0x%llx] and "
2626 "[0x%llx -> 0x%llx]"),
2627 lb1
, le1
, lb2
, le2
);
2630 last_load_segment
= *p
;
2634 // Handle the non-PT_LOAD segments, setting their offsets from their
2635 // section's offsets.
2636 for (Segment_list::iterator p
= this->segment_list_
.begin();
2637 p
!= this->segment_list_
.end();
2640 if ((*p
)->type() != elfcpp::PT_LOAD
)
2641 (*p
)->set_offset((*p
)->type() == elfcpp::PT_GNU_RELRO
2646 // Set the TLS offsets for each section in the PT_TLS segment.
2647 if (this->tls_segment_
!= NULL
)
2648 this->tls_segment_
->set_tls_offsets();
2653 // Set the offsets of all the allocated sections when doing a
2654 // relocatable link. This does the same jobs as set_segment_offsets,
2655 // only for a relocatable link.
2658 Layout::set_relocatable_section_offsets(Output_data
* file_header
,
2659 unsigned int *pshndx
)
2663 file_header
->set_address_and_file_offset(0, 0);
2664 off
+= file_header
->data_size();
2666 for (Section_list::iterator p
= this->section_list_
.begin();
2667 p
!= this->section_list_
.end();
2670 // We skip unallocated sections here, except that group sections
2671 // have to come first.
2672 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) == 0
2673 && (*p
)->type() != elfcpp::SHT_GROUP
)
2676 off
= align_address(off
, (*p
)->addralign());
2678 // The linker script might have set the address.
2679 if (!(*p
)->is_address_valid())
2680 (*p
)->set_address(0);
2681 (*p
)->set_file_offset(off
);
2682 (*p
)->finalize_data_size();
2683 off
+= (*p
)->data_size();
2685 (*p
)->set_out_shndx(*pshndx
);
2692 // Set the file offset of all the sections not associated with a
2696 Layout::set_section_offsets(off_t off
, Layout::Section_offset_pass pass
)
2698 for (Section_list::iterator p
= this->unattached_section_list_
.begin();
2699 p
!= this->unattached_section_list_
.end();
2702 // The symtab section is handled in create_symtab_sections.
2703 if (*p
== this->symtab_section_
)
2706 // If we've already set the data size, don't set it again.
2707 if ((*p
)->is_offset_valid() && (*p
)->is_data_size_valid())
2710 if (pass
== BEFORE_INPUT_SECTIONS_PASS
2711 && (*p
)->requires_postprocessing())
2713 (*p
)->create_postprocessing_buffer();
2714 this->any_postprocessing_sections_
= true;
2717 if (pass
== BEFORE_INPUT_SECTIONS_PASS
2718 && (*p
)->after_input_sections())
2720 else if (pass
== POSTPROCESSING_SECTIONS_PASS
2721 && (!(*p
)->after_input_sections()
2722 || (*p
)->type() == elfcpp::SHT_STRTAB
))
2724 else if (pass
== STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
2725 && (!(*p
)->after_input_sections()
2726 || (*p
)->type() != elfcpp::SHT_STRTAB
))
2729 off
= align_address(off
, (*p
)->addralign());
2730 (*p
)->set_file_offset(off
);
2731 (*p
)->finalize_data_size();
2732 off
+= (*p
)->data_size();
2734 // At this point the name must be set.
2735 if (pass
!= STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
)
2736 this->namepool_
.add((*p
)->name(), false, NULL
);
2741 // Set the section indexes of all the sections not associated with a
2745 Layout::set_section_indexes(unsigned int shndx
)
2747 for (Section_list::iterator p
= this->unattached_section_list_
.begin();
2748 p
!= this->unattached_section_list_
.end();
2751 if (!(*p
)->has_out_shndx())
2753 (*p
)->set_out_shndx(shndx
);
2760 // Set the section addresses according to the linker script. This is
2761 // only called when we see a SECTIONS clause. This returns the
2762 // program segment which should hold the file header and segment
2763 // headers, if any. It will return NULL if they should not be in a
2767 Layout::set_section_addresses_from_script(Symbol_table
* symtab
)
2769 Script_sections
* ss
= this->script_options_
->script_sections();
2770 gold_assert(ss
->saw_sections_clause());
2771 return this->script_options_
->set_section_addresses(symtab
, this);
2774 // Place the orphan sections in the linker script.
2777 Layout::place_orphan_sections_in_script()
2779 Script_sections
* ss
= this->script_options_
->script_sections();
2780 gold_assert(ss
->saw_sections_clause());
2782 // Place each orphaned output section in the script.
2783 for (Section_list::iterator p
= this->section_list_
.begin();
2784 p
!= this->section_list_
.end();
2787 if (!(*p
)->found_in_sections_clause())
2788 ss
->place_orphan(*p
);
2792 // Count the local symbols in the regular symbol table and the dynamic
2793 // symbol table, and build the respective string pools.
2796 Layout::count_local_symbols(const Task
* task
,
2797 const Input_objects
* input_objects
)
2799 // First, figure out an upper bound on the number of symbols we'll
2800 // be inserting into each pool. This helps us create the pools with
2801 // the right size, to avoid unnecessary hashtable resizing.
2802 unsigned int symbol_count
= 0;
2803 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2804 p
!= input_objects
->relobj_end();
2806 symbol_count
+= (*p
)->local_symbol_count();
2808 // Go from "upper bound" to "estimate." We overcount for two
2809 // reasons: we double-count symbols that occur in more than one
2810 // object file, and we count symbols that are dropped from the
2811 // output. Add it all together and assume we overcount by 100%.
2814 // We assume all symbols will go into both the sympool and dynpool.
2815 this->sympool_
.reserve(symbol_count
);
2816 this->dynpool_
.reserve(symbol_count
);
2818 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2819 p
!= input_objects
->relobj_end();
2822 Task_lock_obj
<Object
> tlo(task
, *p
);
2823 (*p
)->count_local_symbols(&this->sympool_
, &this->dynpool_
);
2827 // Create the symbol table sections. Here we also set the final
2828 // values of the symbols. At this point all the loadable sections are
2829 // fully laid out. SHNUM is the number of sections so far.
2832 Layout::create_symtab_sections(const Input_objects
* input_objects
,
2833 Symbol_table
* symtab
,
2839 if (parameters
->target().get_size() == 32)
2841 symsize
= elfcpp::Elf_sizes
<32>::sym_size
;
2844 else if (parameters
->target().get_size() == 64)
2846 symsize
= elfcpp::Elf_sizes
<64>::sym_size
;
2853 off
= align_address(off
, align
);
2854 off_t startoff
= off
;
2856 // Save space for the dummy symbol at the start of the section. We
2857 // never bother to write this out--it will just be left as zero.
2859 unsigned int local_symbol_index
= 1;
2861 // Add STT_SECTION symbols for each Output section which needs one.
2862 for (Section_list::iterator p
= this->section_list_
.begin();
2863 p
!= this->section_list_
.end();
2866 if (!(*p
)->needs_symtab_index())
2867 (*p
)->set_symtab_index(-1U);
2870 (*p
)->set_symtab_index(local_symbol_index
);
2871 ++local_symbol_index
;
2876 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2877 p
!= input_objects
->relobj_end();
2880 unsigned int index
= (*p
)->finalize_local_symbols(local_symbol_index
,
2882 off
+= (index
- local_symbol_index
) * symsize
;
2883 local_symbol_index
= index
;
2886 unsigned int local_symcount
= local_symbol_index
;
2887 gold_assert(static_cast<off_t
>(local_symcount
* symsize
) == off
- startoff
);
2890 size_t dyn_global_index
;
2892 if (this->dynsym_section_
== NULL
)
2895 dyn_global_index
= 0;
2900 dyn_global_index
= this->dynsym_section_
->info();
2901 off_t locsize
= dyn_global_index
* this->dynsym_section_
->entsize();
2902 dynoff
= this->dynsym_section_
->offset() + locsize
;
2903 dyncount
= (this->dynsym_section_
->data_size() - locsize
) / symsize
;
2904 gold_assert(static_cast<off_t
>(dyncount
* symsize
)
2905 == this->dynsym_section_
->data_size() - locsize
);
2908 off
= symtab
->finalize(off
, dynoff
, dyn_global_index
, dyncount
,
2909 &this->sympool_
, &local_symcount
);
2911 if (!parameters
->options().strip_all())
2913 this->sympool_
.set_string_offsets();
2915 const char* symtab_name
= this->namepool_
.add(".symtab", false, NULL
);
2916 Output_section
* osymtab
= this->make_output_section(symtab_name
,
2920 this->symtab_section_
= osymtab
;
2922 Output_section_data
* pos
= new Output_data_fixed_space(off
- startoff
,
2925 osymtab
->add_output_section_data(pos
);
2927 // We generate a .symtab_shndx section if we have more than
2928 // SHN_LORESERVE sections. Technically it is possible that we
2929 // don't need one, because it is possible that there are no
2930 // symbols in any of sections with indexes larger than
2931 // SHN_LORESERVE. That is probably unusual, though, and it is
2932 // easier to always create one than to compute section indexes
2933 // twice (once here, once when writing out the symbols).
2934 if (shnum
>= elfcpp::SHN_LORESERVE
)
2936 const char* symtab_xindex_name
= this->namepool_
.add(".symtab_shndx",
2938 Output_section
* osymtab_xindex
=
2939 this->make_output_section(symtab_xindex_name
,
2940 elfcpp::SHT_SYMTAB_SHNDX
, 0,
2941 ORDER_INVALID
, false);
2943 size_t symcount
= (off
- startoff
) / symsize
;
2944 this->symtab_xindex_
= new Output_symtab_xindex(symcount
);
2946 osymtab_xindex
->add_output_section_data(this->symtab_xindex_
);
2948 osymtab_xindex
->set_link_section(osymtab
);
2949 osymtab_xindex
->set_addralign(4);
2950 osymtab_xindex
->set_entsize(4);
2952 osymtab_xindex
->set_after_input_sections();
2954 // This tells the driver code to wait until the symbol table
2955 // has written out before writing out the postprocessing
2956 // sections, including the .symtab_shndx section.
2957 this->any_postprocessing_sections_
= true;
2960 const char* strtab_name
= this->namepool_
.add(".strtab", false, NULL
);
2961 Output_section
* ostrtab
= this->make_output_section(strtab_name
,
2966 Output_section_data
* pstr
= new Output_data_strtab(&this->sympool_
);
2967 ostrtab
->add_output_section_data(pstr
);
2969 osymtab
->set_file_offset(startoff
);
2970 osymtab
->finalize_data_size();
2971 osymtab
->set_link_section(ostrtab
);
2972 osymtab
->set_info(local_symcount
);
2973 osymtab
->set_entsize(symsize
);
2979 // Create the .shstrtab section, which holds the names of the
2980 // sections. At the time this is called, we have created all the
2981 // output sections except .shstrtab itself.
2984 Layout::create_shstrtab()
2986 // FIXME: We don't need to create a .shstrtab section if we are
2987 // stripping everything.
2989 const char* name
= this->namepool_
.add(".shstrtab", false, NULL
);
2991 Output_section
* os
= this->make_output_section(name
, elfcpp::SHT_STRTAB
, 0,
2992 ORDER_INVALID
, false);
2994 if (strcmp(parameters
->options().compress_debug_sections(), "none") != 0)
2996 // We can't write out this section until we've set all the
2997 // section names, and we don't set the names of compressed
2998 // output sections until relocations are complete. FIXME: With
2999 // the current names we use, this is unnecessary.
3000 os
->set_after_input_sections();
3003 Output_section_data
* posd
= new Output_data_strtab(&this->namepool_
);
3004 os
->add_output_section_data(posd
);
3009 // Create the section headers. SIZE is 32 or 64. OFF is the file
3013 Layout::create_shdrs(const Output_section
* shstrtab_section
, off_t
* poff
)
3015 Output_section_headers
* oshdrs
;
3016 oshdrs
= new Output_section_headers(this,
3017 &this->segment_list_
,
3018 &this->section_list_
,
3019 &this->unattached_section_list_
,
3022 off_t off
= align_address(*poff
, oshdrs
->addralign());
3023 oshdrs
->set_address_and_file_offset(0, off
);
3024 off
+= oshdrs
->data_size();
3026 this->section_headers_
= oshdrs
;
3029 // Count the allocated sections.
3032 Layout::allocated_output_section_count() const
3034 size_t section_count
= 0;
3035 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
3036 p
!= this->segment_list_
.end();
3038 section_count
+= (*p
)->output_section_count();
3039 return section_count
;
3042 // Create the dynamic symbol table.
3045 Layout::create_dynamic_symtab(const Input_objects
* input_objects
,
3046 Symbol_table
* symtab
,
3047 Output_section
**pdynstr
,
3048 unsigned int* plocal_dynamic_count
,
3049 std::vector
<Symbol
*>* pdynamic_symbols
,
3050 Versions
* pversions
)
3052 // Count all the symbols in the dynamic symbol table, and set the
3053 // dynamic symbol indexes.
3055 // Skip symbol 0, which is always all zeroes.
3056 unsigned int index
= 1;
3058 // Add STT_SECTION symbols for each Output section which needs one.
3059 for (Section_list::iterator p
= this->section_list_
.begin();
3060 p
!= this->section_list_
.end();
3063 if (!(*p
)->needs_dynsym_index())
3064 (*p
)->set_dynsym_index(-1U);
3067 (*p
)->set_dynsym_index(index
);
3072 // Count the local symbols that need to go in the dynamic symbol table,
3073 // and set the dynamic symbol indexes.
3074 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
3075 p
!= input_objects
->relobj_end();
3078 unsigned int new_index
= (*p
)->set_local_dynsym_indexes(index
);
3082 unsigned int local_symcount
= index
;
3083 *plocal_dynamic_count
= local_symcount
;
3085 index
= symtab
->set_dynsym_indexes(index
, pdynamic_symbols
,
3086 &this->dynpool_
, pversions
);
3090 const int size
= parameters
->target().get_size();
3093 symsize
= elfcpp::Elf_sizes
<32>::sym_size
;
3096 else if (size
== 64)
3098 symsize
= elfcpp::Elf_sizes
<64>::sym_size
;
3104 // Create the dynamic symbol table section.
3106 Output_section
* dynsym
= this->choose_output_section(NULL
, ".dynsym",
3110 ORDER_DYNAMIC_LINKER
,
3113 Output_section_data
* odata
= new Output_data_fixed_space(index
* symsize
,
3116 dynsym
->add_output_section_data(odata
);
3118 dynsym
->set_info(local_symcount
);
3119 dynsym
->set_entsize(symsize
);
3120 dynsym
->set_addralign(align
);
3122 this->dynsym_section_
= dynsym
;
3124 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
3125 odyn
->add_section_address(elfcpp::DT_SYMTAB
, dynsym
);
3126 odyn
->add_constant(elfcpp::DT_SYMENT
, symsize
);
3128 // If there are more than SHN_LORESERVE allocated sections, we
3129 // create a .dynsym_shndx section. It is possible that we don't
3130 // need one, because it is possible that there are no dynamic
3131 // symbols in any of the sections with indexes larger than
3132 // SHN_LORESERVE. This is probably unusual, though, and at this
3133 // time we don't know the actual section indexes so it is
3134 // inconvenient to check.
3135 if (this->allocated_output_section_count() >= elfcpp::SHN_LORESERVE
)
3137 Output_section
* dynsym_xindex
=
3138 this->choose_output_section(NULL
, ".dynsym_shndx",
3139 elfcpp::SHT_SYMTAB_SHNDX
,
3141 false, ORDER_DYNAMIC_LINKER
, false);
3143 this->dynsym_xindex_
= new Output_symtab_xindex(index
);
3145 dynsym_xindex
->add_output_section_data(this->dynsym_xindex_
);
3147 dynsym_xindex
->set_link_section(dynsym
);
3148 dynsym_xindex
->set_addralign(4);
3149 dynsym_xindex
->set_entsize(4);
3151 dynsym_xindex
->set_after_input_sections();
3153 // This tells the driver code to wait until the symbol table has
3154 // written out before writing out the postprocessing sections,
3155 // including the .dynsym_shndx section.
3156 this->any_postprocessing_sections_
= true;
3159 // Create the dynamic string table section.
3161 Output_section
* dynstr
= this->choose_output_section(NULL
, ".dynstr",
3165 ORDER_DYNAMIC_LINKER
,
3168 Output_section_data
* strdata
= new Output_data_strtab(&this->dynpool_
);
3169 dynstr
->add_output_section_data(strdata
);
3171 dynsym
->set_link_section(dynstr
);
3172 this->dynamic_section_
->set_link_section(dynstr
);
3174 odyn
->add_section_address(elfcpp::DT_STRTAB
, dynstr
);
3175 odyn
->add_section_size(elfcpp::DT_STRSZ
, dynstr
);
3179 // Create the hash tables.
3181 if (strcmp(parameters
->options().hash_style(), "sysv") == 0
3182 || strcmp(parameters
->options().hash_style(), "both") == 0)
3184 unsigned char* phash
;
3185 unsigned int hashlen
;
3186 Dynobj::create_elf_hash_table(*pdynamic_symbols
, local_symcount
,
3189 Output_section
* hashsec
=
3190 this->choose_output_section(NULL
, ".hash", elfcpp::SHT_HASH
,
3191 elfcpp::SHF_ALLOC
, false,
3192 ORDER_DYNAMIC_LINKER
, false);
3194 Output_section_data
* hashdata
= new Output_data_const_buffer(phash
,
3198 hashsec
->add_output_section_data(hashdata
);
3200 hashsec
->set_link_section(dynsym
);
3201 hashsec
->set_entsize(4);
3203 odyn
->add_section_address(elfcpp::DT_HASH
, hashsec
);
3206 if (strcmp(parameters
->options().hash_style(), "gnu") == 0
3207 || strcmp(parameters
->options().hash_style(), "both") == 0)
3209 unsigned char* phash
;
3210 unsigned int hashlen
;
3211 Dynobj::create_gnu_hash_table(*pdynamic_symbols
, local_symcount
,
3214 Output_section
* hashsec
=
3215 this->choose_output_section(NULL
, ".gnu.hash", elfcpp::SHT_GNU_HASH
,
3216 elfcpp::SHF_ALLOC
, false,
3217 ORDER_DYNAMIC_LINKER
, false);
3219 Output_section_data
* hashdata
= new Output_data_const_buffer(phash
,
3223 hashsec
->add_output_section_data(hashdata
);
3225 hashsec
->set_link_section(dynsym
);
3227 // For a 64-bit target, the entries in .gnu.hash do not have a
3228 // uniform size, so we only set the entry size for a 32-bit
3230 if (parameters
->target().get_size() == 32)
3231 hashsec
->set_entsize(4);
3233 odyn
->add_section_address(elfcpp::DT_GNU_HASH
, hashsec
);
3237 // Assign offsets to each local portion of the dynamic symbol table.
3240 Layout::assign_local_dynsym_offsets(const Input_objects
* input_objects
)
3242 Output_section
* dynsym
= this->dynsym_section_
;
3243 gold_assert(dynsym
!= NULL
);
3245 off_t off
= dynsym
->offset();
3247 // Skip the dummy symbol at the start of the section.
3248 off
+= dynsym
->entsize();
3250 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
3251 p
!= input_objects
->relobj_end();
3254 unsigned int count
= (*p
)->set_local_dynsym_offset(off
);
3255 off
+= count
* dynsym
->entsize();
3259 // Create the version sections.
3262 Layout::create_version_sections(const Versions
* versions
,
3263 const Symbol_table
* symtab
,
3264 unsigned int local_symcount
,
3265 const std::vector
<Symbol
*>& dynamic_symbols
,
3266 const Output_section
* dynstr
)
3268 if (!versions
->any_defs() && !versions
->any_needs())
3271 switch (parameters
->size_and_endianness())
3273 #ifdef HAVE_TARGET_32_LITTLE
3274 case Parameters::TARGET_32_LITTLE
:
3275 this->sized_create_version_sections
<32, false>(versions
, symtab
,
3277 dynamic_symbols
, dynstr
);
3280 #ifdef HAVE_TARGET_32_BIG
3281 case Parameters::TARGET_32_BIG
:
3282 this->sized_create_version_sections
<32, true>(versions
, symtab
,
3284 dynamic_symbols
, dynstr
);
3287 #ifdef HAVE_TARGET_64_LITTLE
3288 case Parameters::TARGET_64_LITTLE
:
3289 this->sized_create_version_sections
<64, false>(versions
, symtab
,
3291 dynamic_symbols
, dynstr
);
3294 #ifdef HAVE_TARGET_64_BIG
3295 case Parameters::TARGET_64_BIG
:
3296 this->sized_create_version_sections
<64, true>(versions
, symtab
,
3298 dynamic_symbols
, dynstr
);
3306 // Create the version sections, sized version.
3308 template<int size
, bool big_endian
>
3310 Layout::sized_create_version_sections(
3311 const Versions
* versions
,
3312 const Symbol_table
* symtab
,
3313 unsigned int local_symcount
,
3314 const std::vector
<Symbol
*>& dynamic_symbols
,
3315 const Output_section
* dynstr
)
3317 Output_section
* vsec
= this->choose_output_section(NULL
, ".gnu.version",
3318 elfcpp::SHT_GNU_versym
,
3321 ORDER_DYNAMIC_LINKER
,
3324 unsigned char* vbuf
;
3326 versions
->symbol_section_contents
<size
, big_endian
>(symtab
, &this->dynpool_
,
3331 Output_section_data
* vdata
= new Output_data_const_buffer(vbuf
, vsize
, 2,
3334 vsec
->add_output_section_data(vdata
);
3335 vsec
->set_entsize(2);
3336 vsec
->set_link_section(this->dynsym_section_
);
3338 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
3339 odyn
->add_section_address(elfcpp::DT_VERSYM
, vsec
);
3341 if (versions
->any_defs())
3343 Output_section
* vdsec
;
3344 vdsec
= this->choose_output_section(NULL
, ".gnu.version_d",
3345 elfcpp::SHT_GNU_verdef
,
3347 false, ORDER_DYNAMIC_LINKER
, false);
3349 unsigned char* vdbuf
;
3350 unsigned int vdsize
;
3351 unsigned int vdentries
;
3352 versions
->def_section_contents
<size
, big_endian
>(&this->dynpool_
, &vdbuf
,
3353 &vdsize
, &vdentries
);
3355 Output_section_data
* vddata
=
3356 new Output_data_const_buffer(vdbuf
, vdsize
, 4, "** version defs");
3358 vdsec
->add_output_section_data(vddata
);
3359 vdsec
->set_link_section(dynstr
);
3360 vdsec
->set_info(vdentries
);
3362 odyn
->add_section_address(elfcpp::DT_VERDEF
, vdsec
);
3363 odyn
->add_constant(elfcpp::DT_VERDEFNUM
, vdentries
);
3366 if (versions
->any_needs())
3368 Output_section
* vnsec
;
3369 vnsec
= this->choose_output_section(NULL
, ".gnu.version_r",
3370 elfcpp::SHT_GNU_verneed
,
3372 false, ORDER_DYNAMIC_LINKER
, false);
3374 unsigned char* vnbuf
;
3375 unsigned int vnsize
;
3376 unsigned int vnentries
;
3377 versions
->need_section_contents
<size
, big_endian
>(&this->dynpool_
,
3381 Output_section_data
* vndata
=
3382 new Output_data_const_buffer(vnbuf
, vnsize
, 4, "** version refs");
3384 vnsec
->add_output_section_data(vndata
);
3385 vnsec
->set_link_section(dynstr
);
3386 vnsec
->set_info(vnentries
);
3388 odyn
->add_section_address(elfcpp::DT_VERNEED
, vnsec
);
3389 odyn
->add_constant(elfcpp::DT_VERNEEDNUM
, vnentries
);
3393 // Create the .interp section and PT_INTERP segment.
3396 Layout::create_interp(const Target
* target
)
3398 const char* interp
= parameters
->options().dynamic_linker();
3401 interp
= target
->dynamic_linker();
3402 gold_assert(interp
!= NULL
);
3405 size_t len
= strlen(interp
) + 1;
3407 Output_section_data
* odata
= new Output_data_const(interp
, len
, 1);
3409 Output_section
* osec
= this->choose_output_section(NULL
, ".interp",
3410 elfcpp::SHT_PROGBITS
,
3412 false, ORDER_INTERP
,
3414 osec
->add_output_section_data(odata
);
3416 if (!this->script_options_
->saw_phdrs_clause())
3418 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_INTERP
,
3420 oseg
->add_output_section_to_nonload(osec
, elfcpp::PF_R
);
3424 // Add dynamic tags for the PLT and the dynamic relocs. This is
3425 // called by the target-specific code. This does nothing if not doing
3428 // USE_REL is true for REL relocs rather than RELA relocs.
3430 // If PLT_GOT is not NULL, then DT_PLTGOT points to it.
3432 // If PLT_REL is not NULL, it is used for DT_PLTRELSZ, and DT_JMPREL,
3433 // and we also set DT_PLTREL. We use PLT_REL's output section, since
3434 // some targets have multiple reloc sections in PLT_REL.
3436 // If DYN_REL is not NULL, it is used for DT_REL/DT_RELA,
3437 // DT_RELSZ/DT_RELASZ, DT_RELENT/DT_RELAENT.
3439 // If ADD_DEBUG is true, we add a DT_DEBUG entry when generating an
3443 Layout::add_target_dynamic_tags(bool use_rel
, const Output_data
* plt_got
,
3444 const Output_data
* plt_rel
,
3445 const Output_data_reloc_generic
* dyn_rel
,
3446 bool add_debug
, bool dynrel_includes_plt
)
3448 Output_data_dynamic
* odyn
= this->dynamic_data_
;
3452 if (plt_got
!= NULL
&& plt_got
->output_section() != NULL
)
3453 odyn
->add_section_address(elfcpp::DT_PLTGOT
, plt_got
);
3455 if (plt_rel
!= NULL
&& plt_rel
->output_section() != NULL
)
3457 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, plt_rel
->output_section());
3458 odyn
->add_section_address(elfcpp::DT_JMPREL
, plt_rel
->output_section());
3459 odyn
->add_constant(elfcpp::DT_PLTREL
,
3460 use_rel
? elfcpp::DT_REL
: elfcpp::DT_RELA
);
3463 if (dyn_rel
!= NULL
&& dyn_rel
->output_section() != NULL
)
3465 odyn
->add_section_address(use_rel
? elfcpp::DT_REL
: elfcpp::DT_RELA
,
3467 if (plt_rel
!= NULL
&& dynrel_includes_plt
)
3468 odyn
->add_section_size(use_rel
? elfcpp::DT_RELSZ
: elfcpp::DT_RELASZ
,
3471 odyn
->add_section_size(use_rel
? elfcpp::DT_RELSZ
: elfcpp::DT_RELASZ
,
3473 const int size
= parameters
->target().get_size();
3478 rel_tag
= elfcpp::DT_RELENT
;
3480 rel_size
= Reloc_types
<elfcpp::SHT_REL
, 32, false>::reloc_size
;
3481 else if (size
== 64)
3482 rel_size
= Reloc_types
<elfcpp::SHT_REL
, 64, false>::reloc_size
;
3488 rel_tag
= elfcpp::DT_RELAENT
;
3490 rel_size
= Reloc_types
<elfcpp::SHT_RELA
, 32, false>::reloc_size
;
3491 else if (size
== 64)
3492 rel_size
= Reloc_types
<elfcpp::SHT_RELA
, 64, false>::reloc_size
;
3496 odyn
->add_constant(rel_tag
, rel_size
);
3498 if (parameters
->options().combreloc())
3500 size_t c
= dyn_rel
->relative_reloc_count();
3502 odyn
->add_constant((use_rel
3503 ? elfcpp::DT_RELCOUNT
3504 : elfcpp::DT_RELACOUNT
),
3509 if (add_debug
&& !parameters
->options().shared())
3511 // The value of the DT_DEBUG tag is filled in by the dynamic
3512 // linker at run time, and used by the debugger.
3513 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
3517 // Finish the .dynamic section and PT_DYNAMIC segment.
3520 Layout::finish_dynamic_section(const Input_objects
* input_objects
,
3521 const Symbol_table
* symtab
)
3523 if (!this->script_options_
->saw_phdrs_clause())
3525 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_DYNAMIC
,
3528 oseg
->add_output_section_to_nonload(this->dynamic_section_
,
3529 elfcpp::PF_R
| elfcpp::PF_W
);
3532 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
3534 for (Input_objects::Dynobj_iterator p
= input_objects
->dynobj_begin();
3535 p
!= input_objects
->dynobj_end();
3538 if (!(*p
)->is_needed()
3539 && (*p
)->input_file()->options().as_needed())
3541 // This dynamic object was linked with --as-needed, but it
3546 odyn
->add_string(elfcpp::DT_NEEDED
, (*p
)->soname());
3549 if (parameters
->options().shared())
3551 const char* soname
= parameters
->options().soname();
3553 odyn
->add_string(elfcpp::DT_SONAME
, soname
);
3556 Symbol
* sym
= symtab
->lookup(parameters
->options().init());
3557 if (sym
!= NULL
&& sym
->is_defined() && !sym
->is_from_dynobj())
3558 odyn
->add_symbol(elfcpp::DT_INIT
, sym
);
3560 sym
= symtab
->lookup(parameters
->options().fini());
3561 if (sym
!= NULL
&& sym
->is_defined() && !sym
->is_from_dynobj())
3562 odyn
->add_symbol(elfcpp::DT_FINI
, sym
);
3564 // Look for .init_array, .preinit_array and .fini_array by checking
3566 for(Layout::Section_list::const_iterator p
= this->section_list_
.begin();
3567 p
!= this->section_list_
.end();
3569 switch((*p
)->type())
3571 case elfcpp::SHT_FINI_ARRAY
:
3572 odyn
->add_section_address(elfcpp::DT_FINI_ARRAY
, *p
);
3573 odyn
->add_section_size(elfcpp::DT_FINI_ARRAYSZ
, *p
);
3575 case elfcpp::SHT_INIT_ARRAY
:
3576 odyn
->add_section_address(elfcpp::DT_INIT_ARRAY
, *p
);
3577 odyn
->add_section_size(elfcpp::DT_INIT_ARRAYSZ
, *p
);
3579 case elfcpp::SHT_PREINIT_ARRAY
:
3580 odyn
->add_section_address(elfcpp::DT_PREINIT_ARRAY
, *p
);
3581 odyn
->add_section_size(elfcpp::DT_PREINIT_ARRAYSZ
, *p
);
3587 // Add a DT_RPATH entry if needed.
3588 const General_options::Dir_list
& rpath(parameters
->options().rpath());
3591 std::string rpath_val
;
3592 for (General_options::Dir_list::const_iterator p
= rpath
.begin();
3596 if (rpath_val
.empty())
3597 rpath_val
= p
->name();
3600 // Eliminate duplicates.
3601 General_options::Dir_list::const_iterator q
;
3602 for (q
= rpath
.begin(); q
!= p
; ++q
)
3603 if (q
->name() == p
->name())
3608 rpath_val
+= p
->name();
3613 odyn
->add_string(elfcpp::DT_RPATH
, rpath_val
);
3614 if (parameters
->options().enable_new_dtags())
3615 odyn
->add_string(elfcpp::DT_RUNPATH
, rpath_val
);
3618 // Look for text segments that have dynamic relocations.
3619 bool have_textrel
= false;
3620 if (!this->script_options_
->saw_sections_clause())
3622 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
3623 p
!= this->segment_list_
.end();
3626 if (((*p
)->flags() & elfcpp::PF_W
) == 0
3627 && (*p
)->has_dynamic_reloc())
3629 have_textrel
= true;
3636 // We don't know the section -> segment mapping, so we are
3637 // conservative and just look for readonly sections with
3638 // relocations. If those sections wind up in writable segments,
3639 // then we have created an unnecessary DT_TEXTREL entry.
3640 for (Section_list::const_iterator p
= this->section_list_
.begin();
3641 p
!= this->section_list_
.end();
3644 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) != 0
3645 && ((*p
)->flags() & elfcpp::SHF_WRITE
) == 0
3646 && ((*p
)->has_dynamic_reloc()))
3648 have_textrel
= true;
3654 // Add a DT_FLAGS entry. We add it even if no flags are set so that
3655 // post-link tools can easily modify these flags if desired.
3656 unsigned int flags
= 0;
3659 // Add a DT_TEXTREL for compatibility with older loaders.
3660 odyn
->add_constant(elfcpp::DT_TEXTREL
, 0);
3661 flags
|= elfcpp::DF_TEXTREL
;
3663 if (parameters
->options().text())
3664 gold_error(_("read-only segment has dynamic relocations"));
3665 else if (parameters
->options().warn_shared_textrel()
3666 && parameters
->options().shared())
3667 gold_warning(_("shared library text segment is not shareable"));
3669 if (parameters
->options().shared() && this->has_static_tls())
3670 flags
|= elfcpp::DF_STATIC_TLS
;
3671 if (parameters
->options().origin())
3672 flags
|= elfcpp::DF_ORIGIN
;
3673 if (parameters
->options().Bsymbolic())
3675 flags
|= elfcpp::DF_SYMBOLIC
;
3676 // Add DT_SYMBOLIC for compatibility with older loaders.
3677 odyn
->add_constant(elfcpp::DT_SYMBOLIC
, 0);
3679 if (parameters
->options().now())
3680 flags
|= elfcpp::DF_BIND_NOW
;
3681 odyn
->add_constant(elfcpp::DT_FLAGS
, flags
);
3684 if (parameters
->options().initfirst())
3685 flags
|= elfcpp::DF_1_INITFIRST
;
3686 if (parameters
->options().interpose())
3687 flags
|= elfcpp::DF_1_INTERPOSE
;
3688 if (parameters
->options().loadfltr())
3689 flags
|= elfcpp::DF_1_LOADFLTR
;
3690 if (parameters
->options().nodefaultlib())
3691 flags
|= elfcpp::DF_1_NODEFLIB
;
3692 if (parameters
->options().nodelete())
3693 flags
|= elfcpp::DF_1_NODELETE
;
3694 if (parameters
->options().nodlopen())
3695 flags
|= elfcpp::DF_1_NOOPEN
;
3696 if (parameters
->options().nodump())
3697 flags
|= elfcpp::DF_1_NODUMP
;
3698 if (!parameters
->options().shared())
3699 flags
&= ~(elfcpp::DF_1_INITFIRST
3700 | elfcpp::DF_1_NODELETE
3701 | elfcpp::DF_1_NOOPEN
);
3702 if (parameters
->options().origin())
3703 flags
|= elfcpp::DF_1_ORIGIN
;
3704 if (parameters
->options().now())
3705 flags
|= elfcpp::DF_1_NOW
;
3707 odyn
->add_constant(elfcpp::DT_FLAGS_1
, flags
);
3710 // Set the size of the _DYNAMIC symbol table to be the size of the
3714 Layout::set_dynamic_symbol_size(const Symbol_table
* symtab
)
3716 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
3717 odyn
->finalize_data_size();
3718 off_t data_size
= odyn
->data_size();
3719 const int size
= parameters
->target().get_size();
3721 symtab
->get_sized_symbol
<32>(this->dynamic_symbol_
)->set_symsize(data_size
);
3722 else if (size
== 64)
3723 symtab
->get_sized_symbol
<64>(this->dynamic_symbol_
)->set_symsize(data_size
);
3728 // The mapping of input section name prefixes to output section names.
3729 // In some cases one prefix is itself a prefix of another prefix; in
3730 // such a case the longer prefix must come first. These prefixes are
3731 // based on the GNU linker default ELF linker script.
3733 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
3734 const Layout::Section_name_mapping
Layout::section_name_mapping
[] =
3736 MAPPING_INIT(".text.", ".text"),
3737 MAPPING_INIT(".ctors.", ".ctors"),
3738 MAPPING_INIT(".dtors.", ".dtors"),
3739 MAPPING_INIT(".rodata.", ".rodata"),
3740 MAPPING_INIT(".data.rel.ro.local", ".data.rel.ro.local"),
3741 MAPPING_INIT(".data.rel.ro", ".data.rel.ro"),
3742 MAPPING_INIT(".data.", ".data"),
3743 MAPPING_INIT(".bss.", ".bss"),
3744 MAPPING_INIT(".tdata.", ".tdata"),
3745 MAPPING_INIT(".tbss.", ".tbss"),
3746 MAPPING_INIT(".init_array.", ".init_array"),
3747 MAPPING_INIT(".fini_array.", ".fini_array"),
3748 MAPPING_INIT(".sdata.", ".sdata"),
3749 MAPPING_INIT(".sbss.", ".sbss"),
3750 // FIXME: In the GNU linker, .sbss2 and .sdata2 are handled
3751 // differently depending on whether it is creating a shared library.
3752 MAPPING_INIT(".sdata2.", ".sdata"),
3753 MAPPING_INIT(".sbss2.", ".sbss"),
3754 MAPPING_INIT(".lrodata.", ".lrodata"),
3755 MAPPING_INIT(".ldata.", ".ldata"),
3756 MAPPING_INIT(".lbss.", ".lbss"),
3757 MAPPING_INIT(".gcc_except_table.", ".gcc_except_table"),
3758 MAPPING_INIT(".gnu.linkonce.d.rel.ro.local.", ".data.rel.ro.local"),
3759 MAPPING_INIT(".gnu.linkonce.d.rel.ro.", ".data.rel.ro"),
3760 MAPPING_INIT(".gnu.linkonce.t.", ".text"),
3761 MAPPING_INIT(".gnu.linkonce.r.", ".rodata"),
3762 MAPPING_INIT(".gnu.linkonce.d.", ".data"),
3763 MAPPING_INIT(".gnu.linkonce.b.", ".bss"),
3764 MAPPING_INIT(".gnu.linkonce.s.", ".sdata"),
3765 MAPPING_INIT(".gnu.linkonce.sb.", ".sbss"),
3766 MAPPING_INIT(".gnu.linkonce.s2.", ".sdata"),
3767 MAPPING_INIT(".gnu.linkonce.sb2.", ".sbss"),
3768 MAPPING_INIT(".gnu.linkonce.wi.", ".debug_info"),
3769 MAPPING_INIT(".gnu.linkonce.td.", ".tdata"),
3770 MAPPING_INIT(".gnu.linkonce.tb.", ".tbss"),
3771 MAPPING_INIT(".gnu.linkonce.lr.", ".lrodata"),
3772 MAPPING_INIT(".gnu.linkonce.l.", ".ldata"),
3773 MAPPING_INIT(".gnu.linkonce.lb.", ".lbss"),
3774 MAPPING_INIT(".ARM.extab", ".ARM.extab"),
3775 MAPPING_INIT(".gnu.linkonce.armextab.", ".ARM.extab"),
3776 MAPPING_INIT(".ARM.exidx", ".ARM.exidx"),
3777 MAPPING_INIT(".gnu.linkonce.armexidx.", ".ARM.exidx"),
3781 const int Layout::section_name_mapping_count
=
3782 (sizeof(Layout::section_name_mapping
)
3783 / sizeof(Layout::section_name_mapping
[0]));
3785 // Choose the output section name to use given an input section name.
3786 // Set *PLEN to the length of the name. *PLEN is initialized to the
3790 Layout::output_section_name(const char* name
, size_t* plen
)
3792 // gcc 4.3 generates the following sorts of section names when it
3793 // needs a section name specific to a function:
3799 // .data.rel.local.FN
3801 // .data.rel.ro.local.FN
3808 // The GNU linker maps all of those to the part before the .FN,
3809 // except that .data.rel.local.FN is mapped to .data, and
3810 // .data.rel.ro.local.FN is mapped to .data.rel.ro. The sections
3811 // beginning with .data.rel.ro.local are grouped together.
3813 // For an anonymous namespace, the string FN can contain a '.'.
3815 // Also of interest: .rodata.strN.N, .rodata.cstN, both of which the
3816 // GNU linker maps to .rodata.
3818 // The .data.rel.ro sections are used with -z relro. The sections
3819 // are recognized by name. We use the same names that the GNU
3820 // linker does for these sections.
3822 // It is hard to handle this in a principled way, so we don't even
3823 // try. We use a table of mappings. If the input section name is
3824 // not found in the table, we simply use it as the output section
3827 const Section_name_mapping
* psnm
= section_name_mapping
;
3828 for (int i
= 0; i
< section_name_mapping_count
; ++i
, ++psnm
)
3830 if (strncmp(name
, psnm
->from
, psnm
->fromlen
) == 0)
3832 *plen
= psnm
->tolen
;
3837 // Compressed debug sections should be mapped to the corresponding
3838 // uncompressed section.
3839 if (is_compressed_debug_section(name
))
3841 size_t len
= strlen(name
);
3842 char *uncompressed_name
= new char[len
];
3843 uncompressed_name
[0] = '.';
3844 gold_assert(name
[0] == '.' && name
[1] == 'z');
3845 strncpy(&uncompressed_name
[1], &name
[2], len
- 2);
3846 uncompressed_name
[len
- 1] = '\0';
3848 return uncompressed_name
;
3854 // Check if a comdat group or .gnu.linkonce section with the given
3855 // NAME is selected for the link. If there is already a section,
3856 // *KEPT_SECTION is set to point to the existing section and the
3857 // function returns false. Otherwise, OBJECT, SHNDX, IS_COMDAT, and
3858 // IS_GROUP_NAME are recorded for this NAME in the layout object,
3859 // *KEPT_SECTION is set to the internal copy and the function returns
3863 Layout::find_or_add_kept_section(const std::string
& name
,
3868 Kept_section
** kept_section
)
3870 // It's normal to see a couple of entries here, for the x86 thunk
3871 // sections. If we see more than a few, we're linking a C++
3872 // program, and we resize to get more space to minimize rehashing.
3873 if (this->signatures_
.size() > 4
3874 && !this->resized_signatures_
)
3876 reserve_unordered_map(&this->signatures_
,
3877 this->number_of_input_files_
* 64);
3878 this->resized_signatures_
= true;
3881 Kept_section candidate
;
3882 std::pair
<Signatures::iterator
, bool> ins
=
3883 this->signatures_
.insert(std::make_pair(name
, candidate
));
3885 if (kept_section
!= NULL
)
3886 *kept_section
= &ins
.first
->second
;
3889 // This is the first time we've seen this signature.
3890 ins
.first
->second
.set_object(object
);
3891 ins
.first
->second
.set_shndx(shndx
);
3893 ins
.first
->second
.set_is_comdat();
3895 ins
.first
->second
.set_is_group_name();
3899 // We have already seen this signature.
3901 if (ins
.first
->second
.is_group_name())
3903 // We've already seen a real section group with this signature.
3904 // If the kept group is from a plugin object, and we're in the
3905 // replacement phase, accept the new one as a replacement.
3906 if (ins
.first
->second
.object() == NULL
3907 && parameters
->options().plugins()->in_replacement_phase())
3909 ins
.first
->second
.set_object(object
);
3910 ins
.first
->second
.set_shndx(shndx
);
3915 else if (is_group_name
)
3917 // This is a real section group, and we've already seen a
3918 // linkonce section with this signature. Record that we've seen
3919 // a section group, and don't include this section group.
3920 ins
.first
->second
.set_is_group_name();
3925 // We've already seen a linkonce section and this is a linkonce
3926 // section. These don't block each other--this may be the same
3927 // symbol name with different section types.
3932 // Store the allocated sections into the section list.
3935 Layout::get_allocated_sections(Section_list
* section_list
) const
3937 for (Section_list::const_iterator p
= this->section_list_
.begin();
3938 p
!= this->section_list_
.end();
3940 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) != 0)
3941 section_list
->push_back(*p
);
3944 // Create an output segment.
3947 Layout::make_output_segment(elfcpp::Elf_Word type
, elfcpp::Elf_Word flags
)
3949 gold_assert(!parameters
->options().relocatable());
3950 Output_segment
* oseg
= new Output_segment(type
, flags
);
3951 this->segment_list_
.push_back(oseg
);
3953 if (type
== elfcpp::PT_TLS
)
3954 this->tls_segment_
= oseg
;
3955 else if (type
== elfcpp::PT_GNU_RELRO
)
3956 this->relro_segment_
= oseg
;
3961 // Write out the Output_sections. Most won't have anything to write,
3962 // since most of the data will come from input sections which are
3963 // handled elsewhere. But some Output_sections do have Output_data.
3966 Layout::write_output_sections(Output_file
* of
) const
3968 for (Section_list::const_iterator p
= this->section_list_
.begin();
3969 p
!= this->section_list_
.end();
3972 if (!(*p
)->after_input_sections())
3977 // Write out data not associated with a section or the symbol table.
3980 Layout::write_data(const Symbol_table
* symtab
, Output_file
* of
) const
3982 if (!parameters
->options().strip_all())
3984 const Output_section
* symtab_section
= this->symtab_section_
;
3985 for (Section_list::const_iterator p
= this->section_list_
.begin();
3986 p
!= this->section_list_
.end();
3989 if ((*p
)->needs_symtab_index())
3991 gold_assert(symtab_section
!= NULL
);
3992 unsigned int index
= (*p
)->symtab_index();
3993 gold_assert(index
> 0 && index
!= -1U);
3994 off_t off
= (symtab_section
->offset()
3995 + index
* symtab_section
->entsize());
3996 symtab
->write_section_symbol(*p
, this->symtab_xindex_
, of
, off
);
4001 const Output_section
* dynsym_section
= this->dynsym_section_
;
4002 for (Section_list::const_iterator p
= this->section_list_
.begin();
4003 p
!= this->section_list_
.end();
4006 if ((*p
)->needs_dynsym_index())
4008 gold_assert(dynsym_section
!= NULL
);
4009 unsigned int index
= (*p
)->dynsym_index();
4010 gold_assert(index
> 0 && index
!= -1U);
4011 off_t off
= (dynsym_section
->offset()
4012 + index
* dynsym_section
->entsize());
4013 symtab
->write_section_symbol(*p
, this->dynsym_xindex_
, of
, off
);
4017 // Write out the Output_data which are not in an Output_section.
4018 for (Data_list::const_iterator p
= this->special_output_list_
.begin();
4019 p
!= this->special_output_list_
.end();
4024 // Write out the Output_sections which can only be written after the
4025 // input sections are complete.
4028 Layout::write_sections_after_input_sections(Output_file
* of
)
4030 // Determine the final section offsets, and thus the final output
4031 // file size. Note we finalize the .shstrab last, to allow the
4032 // after_input_section sections to modify their section-names before
4034 if (this->any_postprocessing_sections_
)
4036 off_t off
= this->output_file_size_
;
4037 off
= this->set_section_offsets(off
, POSTPROCESSING_SECTIONS_PASS
);
4039 // Now that we've finalized the names, we can finalize the shstrab.
4041 this->set_section_offsets(off
,
4042 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
);
4044 if (off
> this->output_file_size_
)
4047 this->output_file_size_
= off
;
4051 for (Section_list::const_iterator p
= this->section_list_
.begin();
4052 p
!= this->section_list_
.end();
4055 if ((*p
)->after_input_sections())
4059 this->section_headers_
->write(of
);
4062 // If the build ID requires computing a checksum, do so here, and
4063 // write it out. We compute a checksum over the entire file because
4064 // that is simplest.
4067 Layout::write_build_id(Output_file
* of
) const
4069 if (this->build_id_note_
== NULL
)
4072 const unsigned char* iv
= of
->get_input_view(0, this->output_file_size_
);
4074 unsigned char* ov
= of
->get_output_view(this->build_id_note_
->offset(),
4075 this->build_id_note_
->data_size());
4077 const char* style
= parameters
->options().build_id();
4078 if (strcmp(style
, "sha1") == 0)
4081 sha1_init_ctx(&ctx
);
4082 sha1_process_bytes(iv
, this->output_file_size_
, &ctx
);
4083 sha1_finish_ctx(&ctx
, ov
);
4085 else if (strcmp(style
, "md5") == 0)
4089 md5_process_bytes(iv
, this->output_file_size_
, &ctx
);
4090 md5_finish_ctx(&ctx
, ov
);
4095 of
->write_output_view(this->build_id_note_
->offset(),
4096 this->build_id_note_
->data_size(),
4099 of
->free_input_view(0, this->output_file_size_
, iv
);
4102 // Write out a binary file. This is called after the link is
4103 // complete. IN is the temporary output file we used to generate the
4104 // ELF code. We simply walk through the segments, read them from
4105 // their file offset in IN, and write them to their load address in
4106 // the output file. FIXME: with a bit more work, we could support
4107 // S-records and/or Intel hex format here.
4110 Layout::write_binary(Output_file
* in
) const
4112 gold_assert(parameters
->options().oformat_enum()
4113 == General_options::OBJECT_FORMAT_BINARY
);
4115 // Get the size of the binary file.
4116 uint64_t max_load_address
= 0;
4117 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
4118 p
!= this->segment_list_
.end();
4121 if ((*p
)->type() == elfcpp::PT_LOAD
&& (*p
)->filesz() > 0)
4123 uint64_t max_paddr
= (*p
)->paddr() + (*p
)->filesz();
4124 if (max_paddr
> max_load_address
)
4125 max_load_address
= max_paddr
;
4129 Output_file
out(parameters
->options().output_file_name());
4130 out
.open(max_load_address
);
4132 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
4133 p
!= this->segment_list_
.end();
4136 if ((*p
)->type() == elfcpp::PT_LOAD
&& (*p
)->filesz() > 0)
4138 const unsigned char* vin
= in
->get_input_view((*p
)->offset(),
4140 unsigned char* vout
= out
.get_output_view((*p
)->paddr(),
4142 memcpy(vout
, vin
, (*p
)->filesz());
4143 out
.write_output_view((*p
)->paddr(), (*p
)->filesz(), vout
);
4144 in
->free_input_view((*p
)->offset(), (*p
)->filesz(), vin
);
4151 // Print the output sections to the map file.
4154 Layout::print_to_mapfile(Mapfile
* mapfile
) const
4156 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
4157 p
!= this->segment_list_
.end();
4159 (*p
)->print_sections_to_mapfile(mapfile
);
4162 // Print statistical information to stderr. This is used for --stats.
4165 Layout::print_stats() const
4167 this->namepool_
.print_stats("section name pool");
4168 this->sympool_
.print_stats("output symbol name pool");
4169 this->dynpool_
.print_stats("dynamic name pool");
4171 for (Section_list::const_iterator p
= this->section_list_
.begin();
4172 p
!= this->section_list_
.end();
4174 (*p
)->print_merge_stats();
4177 // Write_sections_task methods.
4179 // We can always run this task.
4182 Write_sections_task::is_runnable()
4187 // We need to unlock both OUTPUT_SECTIONS_BLOCKER and FINAL_BLOCKER
4191 Write_sections_task::locks(Task_locker
* tl
)
4193 tl
->add(this, this->output_sections_blocker_
);
4194 tl
->add(this, this->final_blocker_
);
4197 // Run the task--write out the data.
4200 Write_sections_task::run(Workqueue
*)
4202 this->layout_
->write_output_sections(this->of_
);
4205 // Write_data_task methods.
4207 // We can always run this task.
4210 Write_data_task::is_runnable()
4215 // We need to unlock FINAL_BLOCKER when finished.
4218 Write_data_task::locks(Task_locker
* tl
)
4220 tl
->add(this, this->final_blocker_
);
4223 // Run the task--write out the data.
4226 Write_data_task::run(Workqueue
*)
4228 this->layout_
->write_data(this->symtab_
, this->of_
);
4231 // Write_symbols_task methods.
4233 // We can always run this task.
4236 Write_symbols_task::is_runnable()
4241 // We need to unlock FINAL_BLOCKER when finished.
4244 Write_symbols_task::locks(Task_locker
* tl
)
4246 tl
->add(this, this->final_blocker_
);
4249 // Run the task--write out the symbols.
4252 Write_symbols_task::run(Workqueue
*)
4254 this->symtab_
->write_globals(this->sympool_
, this->dynpool_
,
4255 this->layout_
->symtab_xindex(),
4256 this->layout_
->dynsym_xindex(), this->of_
);
4259 // Write_after_input_sections_task methods.
4261 // We can only run this task after the input sections have completed.
4264 Write_after_input_sections_task::is_runnable()
4266 if (this->input_sections_blocker_
->is_blocked())
4267 return this->input_sections_blocker_
;
4271 // We need to unlock FINAL_BLOCKER when finished.
4274 Write_after_input_sections_task::locks(Task_locker
* tl
)
4276 tl
->add(this, this->final_blocker_
);
4282 Write_after_input_sections_task::run(Workqueue
*)
4284 this->layout_
->write_sections_after_input_sections(this->of_
);
4287 // Close_task_runner methods.
4289 // Run the task--close the file.
4292 Close_task_runner::run(Workqueue
*, const Task
*)
4294 // If we need to compute a checksum for the BUILD if, we do so here.
4295 this->layout_
->write_build_id(this->of_
);
4297 // If we've been asked to create a binary file, we do so here.
4298 if (this->options_
->oformat_enum() != General_options::OBJECT_FORMAT_ELF
)
4299 this->layout_
->write_binary(this->of_
);
4304 // Instantiate the templates we need. We could use the configure
4305 // script to restrict this to only the ones for implemented targets.
4307 #ifdef HAVE_TARGET_32_LITTLE
4310 Layout::layout
<32, false>(Sized_relobj
<32, false>* object
, unsigned int shndx
,
4312 const elfcpp::Shdr
<32, false>& shdr
,
4313 unsigned int, unsigned int, off_t
*);
4316 #ifdef HAVE_TARGET_32_BIG
4319 Layout::layout
<32, true>(Sized_relobj
<32, true>* object
, unsigned int shndx
,
4321 const elfcpp::Shdr
<32, true>& shdr
,
4322 unsigned int, unsigned int, off_t
*);
4325 #ifdef HAVE_TARGET_64_LITTLE
4328 Layout::layout
<64, false>(Sized_relobj
<64, false>* object
, unsigned int shndx
,
4330 const elfcpp::Shdr
<64, false>& shdr
,
4331 unsigned int, unsigned int, off_t
*);
4334 #ifdef HAVE_TARGET_64_BIG
4337 Layout::layout
<64, true>(Sized_relobj
<64, true>* object
, unsigned int shndx
,
4339 const elfcpp::Shdr
<64, true>& shdr
,
4340 unsigned int, unsigned int, off_t
*);
4343 #ifdef HAVE_TARGET_32_LITTLE
4346 Layout::layout_reloc
<32, false>(Sized_relobj
<32, false>* object
,
4347 unsigned int reloc_shndx
,
4348 const elfcpp::Shdr
<32, false>& shdr
,
4349 Output_section
* data_section
,
4350 Relocatable_relocs
* rr
);
4353 #ifdef HAVE_TARGET_32_BIG
4356 Layout::layout_reloc
<32, true>(Sized_relobj
<32, true>* object
,
4357 unsigned int reloc_shndx
,
4358 const elfcpp::Shdr
<32, true>& shdr
,
4359 Output_section
* data_section
,
4360 Relocatable_relocs
* rr
);
4363 #ifdef HAVE_TARGET_64_LITTLE
4366 Layout::layout_reloc
<64, false>(Sized_relobj
<64, false>* object
,
4367 unsigned int reloc_shndx
,
4368 const elfcpp::Shdr
<64, false>& shdr
,
4369 Output_section
* data_section
,
4370 Relocatable_relocs
* rr
);
4373 #ifdef HAVE_TARGET_64_BIG
4376 Layout::layout_reloc
<64, true>(Sized_relobj
<64, true>* object
,
4377 unsigned int reloc_shndx
,
4378 const elfcpp::Shdr
<64, true>& shdr
,
4379 Output_section
* data_section
,
4380 Relocatable_relocs
* rr
);
4383 #ifdef HAVE_TARGET_32_LITTLE
4386 Layout::layout_group
<32, false>(Symbol_table
* symtab
,
4387 Sized_relobj
<32, false>* object
,
4389 const char* group_section_name
,
4390 const char* signature
,
4391 const elfcpp::Shdr
<32, false>& shdr
,
4392 elfcpp::Elf_Word flags
,
4393 std::vector
<unsigned int>* shndxes
);
4396 #ifdef HAVE_TARGET_32_BIG
4399 Layout::layout_group
<32, true>(Symbol_table
* symtab
,
4400 Sized_relobj
<32, true>* object
,
4402 const char* group_section_name
,
4403 const char* signature
,
4404 const elfcpp::Shdr
<32, true>& shdr
,
4405 elfcpp::Elf_Word flags
,
4406 std::vector
<unsigned int>* shndxes
);
4409 #ifdef HAVE_TARGET_64_LITTLE
4412 Layout::layout_group
<64, false>(Symbol_table
* symtab
,
4413 Sized_relobj
<64, false>* object
,
4415 const char* group_section_name
,
4416 const char* signature
,
4417 const elfcpp::Shdr
<64, false>& shdr
,
4418 elfcpp::Elf_Word flags
,
4419 std::vector
<unsigned int>* shndxes
);
4422 #ifdef HAVE_TARGET_64_BIG
4425 Layout::layout_group
<64, true>(Symbol_table
* symtab
,
4426 Sized_relobj
<64, true>* object
,
4428 const char* group_section_name
,
4429 const char* signature
,
4430 const elfcpp::Shdr
<64, true>& shdr
,
4431 elfcpp::Elf_Word flags
,
4432 std::vector
<unsigned int>* shndxes
);
4435 #ifdef HAVE_TARGET_32_LITTLE
4438 Layout::layout_eh_frame
<32, false>(Sized_relobj
<32, false>* object
,
4439 const unsigned char* symbols
,
4441 const unsigned char* symbol_names
,
4442 off_t symbol_names_size
,
4444 const elfcpp::Shdr
<32, false>& shdr
,
4445 unsigned int reloc_shndx
,
4446 unsigned int reloc_type
,
4450 #ifdef HAVE_TARGET_32_BIG
4453 Layout::layout_eh_frame
<32, true>(Sized_relobj
<32, true>* object
,
4454 const unsigned char* symbols
,
4456 const unsigned char* symbol_names
,
4457 off_t symbol_names_size
,
4459 const elfcpp::Shdr
<32, true>& shdr
,
4460 unsigned int reloc_shndx
,
4461 unsigned int reloc_type
,
4465 #ifdef HAVE_TARGET_64_LITTLE
4468 Layout::layout_eh_frame
<64, false>(Sized_relobj
<64, false>* object
,
4469 const unsigned char* symbols
,
4471 const unsigned char* symbol_names
,
4472 off_t symbol_names_size
,
4474 const elfcpp::Shdr
<64, false>& shdr
,
4475 unsigned int reloc_shndx
,
4476 unsigned int reloc_type
,
4480 #ifdef HAVE_TARGET_64_BIG
4483 Layout::layout_eh_frame
<64, true>(Sized_relobj
<64, true>* object
,
4484 const unsigned char* symbols
,
4486 const unsigned char* symbol_names
,
4487 off_t symbol_names_size
,
4489 const elfcpp::Shdr
<64, true>& shdr
,
4490 unsigned int reloc_shndx
,
4491 unsigned int reloc_type
,
4495 } // End namespace gold.