1 // layout.cc -- lay out output file sections for gold
3 // Copyright 2006, 2007, 2008 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.
30 #include "parameters.h"
33 #include "script-sections.h"
38 #include "compressed_output.h"
45 // Layout_task_runner methods.
47 // Lay out the sections. This is called after all the input objects
51 Layout_task_runner::run(Workqueue
* workqueue
, const Task
* task
)
53 off_t file_size
= this->layout_
->finalize(this->input_objects_
,
57 // Now we know the final size of the output file and we know where
58 // each piece of information goes.
59 Output_file
* of
= new Output_file(parameters
->output_file_name());
62 // Queue up the final set of tasks.
63 gold::queue_final_tasks(this->options_
, this->input_objects_
,
64 this->symtab_
, this->layout_
, workqueue
, of
);
69 Layout::Layout(const General_options
& options
, Script_options
* script_options
)
70 : options_(options
), script_options_(script_options
), namepool_(),
71 sympool_(), dynpool_(), signatures_(),
72 section_name_map_(), segment_list_(), section_list_(),
73 unattached_section_list_(), special_output_list_(),
74 section_headers_(NULL
), tls_segment_(NULL
), symtab_section_(NULL
),
75 dynsym_section_(NULL
), dynamic_section_(NULL
), dynamic_data_(NULL
),
76 eh_frame_section_(NULL
), group_signatures_(), output_file_size_(-1),
77 input_requires_executable_stack_(false),
78 input_with_gnu_stack_note_(false),
79 input_without_gnu_stack_note_(false),
80 has_static_tls_(false),
81 any_postprocessing_sections_(false)
83 // Make space for more than enough segments for a typical file.
84 // This is just for efficiency--it's OK if we wind up needing more.
85 this->segment_list_
.reserve(12);
87 // We expect two unattached Output_data objects: the file header and
88 // the segment headers.
89 this->special_output_list_
.reserve(2);
92 // Hash a key we use to look up an output section mapping.
95 Layout::Hash_key::operator()(const Layout::Key
& k
) const
97 return k
.first
+ k
.second
.first
+ k
.second
.second
;
100 // Return whether PREFIX is a prefix of STR.
103 is_prefix_of(const char* prefix
, const char* str
)
105 return strncmp(prefix
, str
, strlen(prefix
)) == 0;
108 // Returns whether the given section is in the list of
109 // debug-sections-used-by-some-version-of-gdb. Currently,
110 // we've checked versions of gdb up to and including 6.7.1.
112 static const char* gdb_sections
[] =
114 // ".debug_aranges", // not used by gdb as of 6.7.1
120 // ".debug_pubnames", // not used by gdb as of 6.7.1
126 is_gdb_debug_section(const char* str
)
128 // We can do this faster: binary search or a hashtable. But why bother?
129 for (size_t i
= 0; i
< sizeof(gdb_sections
)/sizeof(*gdb_sections
); ++i
)
130 if (strcmp(str
, gdb_sections
[i
]) == 0)
135 // Whether to include this section in the link.
137 template<int size
, bool big_endian
>
139 Layout::include_section(Sized_relobj
<size
, big_endian
>*, const char* name
,
140 const elfcpp::Shdr
<size
, big_endian
>& shdr
)
142 switch (shdr
.get_sh_type())
144 case elfcpp::SHT_NULL
:
145 case elfcpp::SHT_SYMTAB
:
146 case elfcpp::SHT_DYNSYM
:
147 case elfcpp::SHT_STRTAB
:
148 case elfcpp::SHT_HASH
:
149 case elfcpp::SHT_DYNAMIC
:
150 case elfcpp::SHT_SYMTAB_SHNDX
:
153 case elfcpp::SHT_RELA
:
154 case elfcpp::SHT_REL
:
155 case elfcpp::SHT_GROUP
:
156 // For a relocatable link these should be handled elsewhere.
157 gold_assert(!parameters
->output_is_object());
160 case elfcpp::SHT_PROGBITS
:
161 if (parameters
->strip_debug()
162 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
164 // Debugging sections can only be recognized by name.
165 if (is_prefix_of(".debug", name
)
166 || is_prefix_of(".gnu.linkonce.wi.", name
)
167 || is_prefix_of(".line", name
)
168 || is_prefix_of(".stab", name
))
171 if (parameters
->strip_debug_gdb()
172 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
174 // Debugging sections can only be recognized by name.
175 if (is_prefix_of(".debug", name
)
176 && !is_gdb_debug_section(name
))
186 // Return an output section named NAME, or NULL if there is none.
189 Layout::find_output_section(const char* name
) const
191 for (Section_list::const_iterator p
= this->section_list_
.begin();
192 p
!= this->section_list_
.end();
194 if (strcmp((*p
)->name(), name
) == 0)
199 // Return an output segment of type TYPE, with segment flags SET set
200 // and segment flags CLEAR clear. Return NULL if there is none.
203 Layout::find_output_segment(elfcpp::PT type
, elfcpp::Elf_Word set
,
204 elfcpp::Elf_Word clear
) const
206 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
207 p
!= this->segment_list_
.end();
209 if (static_cast<elfcpp::PT
>((*p
)->type()) == type
210 && ((*p
)->flags() & set
) == set
211 && ((*p
)->flags() & clear
) == 0)
216 // Return the output section to use for section NAME with type TYPE
217 // and section flags FLAGS. NAME must be canonicalized in the string
218 // pool, and NAME_KEY is the key.
221 Layout::get_output_section(const char* name
, Stringpool::Key name_key
,
222 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
)
224 const Key
key(name_key
, std::make_pair(type
, flags
));
225 const std::pair
<Key
, Output_section
*> v(key
, NULL
);
226 std::pair
<Section_name_map::iterator
, bool> ins(
227 this->section_name_map_
.insert(v
));
230 return ins
.first
->second
;
233 // This is the first time we've seen this name/type/flags
235 Output_section
* os
= this->make_output_section(name
, type
, flags
);
236 ins
.first
->second
= os
;
241 // Pick the output section to use for section NAME, in input file
242 // RELOBJ, with type TYPE and flags FLAGS. RELOBJ may be NULL for a
243 // linker created section. ADJUST_NAME is true if we should apply the
244 // standard name mappings in Layout::output_section_name. This will
245 // return NULL if the input section should be discarded.
248 Layout::choose_output_section(const Relobj
* relobj
, const char* name
,
249 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
,
252 // We should ignore some flags. FIXME: This will need some
253 // adjustment for ld -r.
254 flags
&= ~ (elfcpp::SHF_INFO_LINK
255 | elfcpp::SHF_LINK_ORDER
258 | elfcpp::SHF_STRINGS
);
260 if (this->script_options_
->saw_sections_clause())
262 // We are using a SECTIONS clause, so the output section is
263 // chosen based only on the name.
265 Script_sections
* ss
= this->script_options_
->script_sections();
266 const char* file_name
= relobj
== NULL
? NULL
: relobj
->name().c_str();
267 Output_section
** output_section_slot
;
268 name
= ss
->output_section_name(file_name
, name
, &output_section_slot
);
271 // The SECTIONS clause says to discard this input section.
275 // If this is an orphan section--one not mentioned in the linker
276 // script--then OUTPUT_SECTION_SLOT will be NULL, and we do the
277 // default processing below.
279 if (output_section_slot
!= NULL
)
281 if (*output_section_slot
!= NULL
)
282 return *output_section_slot
;
284 // We don't put sections found in the linker script into
285 // SECTION_NAME_MAP_. That keeps us from getting confused
286 // if an orphan section is mapped to a section with the same
287 // name as one in the linker script.
289 name
= this->namepool_
.add(name
, false, NULL
);
291 Output_section
* os
= this->make_output_section(name
, type
, flags
);
292 os
->set_found_in_sections_clause();
293 *output_section_slot
= os
;
298 // FIXME: Handle SHF_OS_NONCONFORMING somewhere.
300 // Turn NAME from the name of the input section into the name of the
303 size_t len
= strlen(name
);
304 if (adjust_name
&& !parameters
->output_is_object())
305 name
= Layout::output_section_name(name
, &len
);
307 Stringpool::Key name_key
;
308 name
= this->namepool_
.add_with_length(name
, len
, true, &name_key
);
310 // Find or make the output section. The output section is selected
311 // based on the section name, type, and flags.
312 return this->get_output_section(name
, name_key
, type
, flags
);
315 // Return the output section to use for input section SHNDX, with name
316 // NAME, with header HEADER, from object OBJECT. RELOC_SHNDX is the
317 // index of a relocation section which applies to this section, or 0
318 // if none, or -1U if more than one. RELOC_TYPE is the type of the
319 // relocation section if there is one. Set *OFF to the offset of this
320 // input section without the output section. Return NULL if the
321 // section should be discarded. Set *OFF to -1 if the section
322 // contents should not be written directly to the output file, but
323 // will instead receive special handling.
325 template<int size
, bool big_endian
>
327 Layout::layout(Sized_relobj
<size
, big_endian
>* object
, unsigned int shndx
,
328 const char* name
, const elfcpp::Shdr
<size
, big_endian
>& shdr
,
329 unsigned int reloc_shndx
, unsigned int, off_t
* off
)
331 if (!this->include_section(object
, name
, shdr
))
336 // In a relocatable link a grouped section must not be combined with
337 // any other sections.
338 if (parameters
->output_is_object()
339 && (shdr
.get_sh_flags() & elfcpp::SHF_GROUP
) != 0)
341 name
= this->namepool_
.add(name
, true, NULL
);
342 os
= this->make_output_section(name
, shdr
.get_sh_type(),
343 shdr
.get_sh_flags());
347 os
= this->choose_output_section(object
, name
, shdr
.get_sh_type(),
348 shdr
.get_sh_flags(), true);
353 // FIXME: Handle SHF_LINK_ORDER somewhere.
355 *off
= os
->add_input_section(object
, shndx
, name
, shdr
, reloc_shndx
,
356 this->script_options_
->saw_sections_clause());
361 // Handle a relocation section when doing a relocatable link.
363 template<int size
, bool big_endian
>
365 Layout::layout_reloc(Sized_relobj
<size
, big_endian
>* object
,
367 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
368 Output_section
* data_section
,
369 Relocatable_relocs
* rr
)
371 gold_assert(parameters
->output_is_object());
373 int sh_type
= shdr
.get_sh_type();
376 if (sh_type
== elfcpp::SHT_REL
)
378 else if (sh_type
== elfcpp::SHT_RELA
)
382 name
+= data_section
->name();
384 Output_section
* os
= this->choose_output_section(object
, name
.c_str(),
389 os
->set_should_link_to_symtab();
390 os
->set_info_section(data_section
);
392 Output_section_data
* posd
;
393 if (sh_type
== elfcpp::SHT_REL
)
395 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rel_size
);
396 posd
= new Output_relocatable_relocs
<elfcpp::SHT_REL
,
400 else if (sh_type
== elfcpp::SHT_RELA
)
402 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rela_size
);
403 posd
= new Output_relocatable_relocs
<elfcpp::SHT_RELA
,
410 os
->add_output_section_data(posd
);
411 rr
->set_output_data(posd
);
416 // Handle a group section when doing a relocatable link.
418 template<int size
, bool big_endian
>
420 Layout::layout_group(Symbol_table
* symtab
,
421 Sized_relobj
<size
, big_endian
>* object
,
423 const char* group_section_name
,
424 const char* signature
,
425 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
426 const elfcpp::Elf_Word
* contents
)
428 gold_assert(parameters
->output_is_object());
429 gold_assert(shdr
.get_sh_type() == elfcpp::SHT_GROUP
);
430 group_section_name
= this->namepool_
.add(group_section_name
, true, NULL
);
431 Output_section
* os
= this->make_output_section(group_section_name
,
433 shdr
.get_sh_flags());
435 // We need to find a symbol with the signature in the symbol table.
436 // If we don't find one now, we need to look again later.
437 Symbol
* sym
= symtab
->lookup(signature
, NULL
);
439 os
->set_info_symndx(sym
);
442 // We will wind up using a symbol whose name is the signature.
443 // So just put the signature in the symbol name pool to save it.
444 signature
= symtab
->canonicalize_name(signature
);
445 this->group_signatures_
.push_back(Group_signature(os
, signature
));
448 os
->set_should_link_to_symtab();
451 section_size_type entry_count
=
452 convert_to_section_size_type(shdr
.get_sh_size() / 4);
453 Output_section_data
* posd
=
454 new Output_data_group
<size
, big_endian
>(object
, entry_count
, contents
);
455 os
->add_output_section_data(posd
);
458 // Special GNU handling of sections name .eh_frame. They will
459 // normally hold exception frame data as defined by the C++ ABI
460 // (http://codesourcery.com/cxx-abi/).
462 template<int size
, bool big_endian
>
464 Layout::layout_eh_frame(Sized_relobj
<size
, big_endian
>* object
,
465 const unsigned char* symbols
,
467 const unsigned char* symbol_names
,
468 off_t symbol_names_size
,
470 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
471 unsigned int reloc_shndx
, unsigned int reloc_type
,
474 gold_assert(shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
);
475 gold_assert(shdr
.get_sh_flags() == elfcpp::SHF_ALLOC
);
477 const char* const name
= ".eh_frame";
478 Output_section
* os
= this->choose_output_section(object
,
480 elfcpp::SHT_PROGBITS
,
486 if (this->eh_frame_section_
== NULL
)
488 this->eh_frame_section_
= os
;
489 this->eh_frame_data_
= new Eh_frame();
490 os
->add_output_section_data(this->eh_frame_data_
);
492 if (this->options_
.create_eh_frame_hdr())
494 Output_section
* hdr_os
=
495 this->choose_output_section(NULL
,
497 elfcpp::SHT_PROGBITS
,
503 Eh_frame_hdr
* hdr_posd
= new Eh_frame_hdr(os
,
504 this->eh_frame_data_
);
505 hdr_os
->add_output_section_data(hdr_posd
);
507 hdr_os
->set_after_input_sections();
509 if (!this->script_options_
->saw_phdrs_clause())
511 Output_segment
* hdr_oseg
;
512 hdr_oseg
= this->make_output_segment(elfcpp::PT_GNU_EH_FRAME
,
514 hdr_oseg
->add_output_section(hdr_os
, elfcpp::PF_R
);
517 this->eh_frame_data_
->set_eh_frame_hdr(hdr_posd
);
522 gold_assert(this->eh_frame_section_
== os
);
524 if (this->eh_frame_data_
->add_ehframe_input_section(object
,
535 // We couldn't handle this .eh_frame section for some reason.
536 // Add it as a normal section.
537 bool saw_sections_clause
= this->script_options_
->saw_sections_clause();
538 *off
= os
->add_input_section(object
, shndx
, name
, shdr
, reloc_shndx
,
539 saw_sections_clause
);
545 // Add POSD to an output section using NAME, TYPE, and FLAGS.
548 Layout::add_output_section_data(const char* name
, elfcpp::Elf_Word type
,
549 elfcpp::Elf_Xword flags
,
550 Output_section_data
* posd
)
552 Output_section
* os
= this->choose_output_section(NULL
, name
, type
, flags
,
555 os
->add_output_section_data(posd
);
558 // Map section flags to segment flags.
561 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags
)
563 elfcpp::Elf_Word ret
= elfcpp::PF_R
;
564 if ((flags
& elfcpp::SHF_WRITE
) != 0)
566 if ((flags
& elfcpp::SHF_EXECINSTR
) != 0)
571 // Sometimes we compress sections. This is typically done for
572 // sections that are not part of normal program execution (such as
573 // .debug_* sections), and where the readers of these sections know
574 // how to deal with compressed sections. (To make it easier for them,
575 // we will rename the ouput section in such cases from .foo to
576 // .foo.zlib.nnnn, where nnnn is the uncompressed size.) This routine
577 // doesn't say for certain whether we'll compress -- it depends on
578 // commandline options as well -- just whether this section is a
579 // candidate for compression.
582 is_compressible_debug_section(const char* secname
)
584 return (strncmp(secname
, ".debug", sizeof(".debug") - 1) == 0);
587 // Make a new Output_section, and attach it to segments as
591 Layout::make_output_section(const char* name
, elfcpp::Elf_Word type
,
592 elfcpp::Elf_Xword flags
)
595 if ((flags
& elfcpp::SHF_ALLOC
) == 0
596 && this->options_
.compress_debug_sections()
597 && is_compressible_debug_section(name
))
598 os
= new Output_compressed_section(&this->options_
, name
, type
, flags
);
600 os
= new Output_section(name
, type
, flags
);
602 this->section_list_
.push_back(os
);
604 if ((flags
& elfcpp::SHF_ALLOC
) == 0)
605 this->unattached_section_list_
.push_back(os
);
608 if (parameters
->output_is_object())
611 // If we have a SECTIONS clause, we can't handle the attachment
612 // to segments until after we've seen all the sections.
613 if (this->script_options_
->saw_sections_clause())
616 gold_assert(!this->script_options_
->saw_phdrs_clause());
618 // This output section goes into a PT_LOAD segment.
620 elfcpp::Elf_Word seg_flags
= Layout::section_flags_to_segment(flags
);
622 // The only thing we really care about for PT_LOAD segments is
623 // whether or not they are writable, so that is how we search
624 // for them. People who need segments sorted on some other
625 // basis will have to wait until we implement a mechanism for
626 // them to describe the segments they want.
628 Segment_list::const_iterator p
;
629 for (p
= this->segment_list_
.begin();
630 p
!= this->segment_list_
.end();
633 if ((*p
)->type() == elfcpp::PT_LOAD
634 && ((*p
)->flags() & elfcpp::PF_W
) == (seg_flags
& elfcpp::PF_W
))
636 (*p
)->add_output_section(os
, seg_flags
);
641 if (p
== this->segment_list_
.end())
643 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_LOAD
,
645 oseg
->add_output_section(os
, seg_flags
);
648 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
650 if (type
== elfcpp::SHT_NOTE
)
652 // See if we already have an equivalent PT_NOTE segment.
653 for (p
= this->segment_list_
.begin();
654 p
!= segment_list_
.end();
657 if ((*p
)->type() == elfcpp::PT_NOTE
658 && (((*p
)->flags() & elfcpp::PF_W
)
659 == (seg_flags
& elfcpp::PF_W
)))
661 (*p
)->add_output_section(os
, seg_flags
);
666 if (p
== this->segment_list_
.end())
668 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_NOTE
,
670 oseg
->add_output_section(os
, seg_flags
);
674 // If we see a loadable SHF_TLS section, we create a PT_TLS
675 // segment. There can only be one such segment.
676 if ((flags
& elfcpp::SHF_TLS
) != 0)
678 if (this->tls_segment_
== NULL
)
679 this->tls_segment_
= this->make_output_segment(elfcpp::PT_TLS
,
681 this->tls_segment_
->add_output_section(os
, seg_flags
);
688 // Return the number of segments we expect to see.
691 Layout::expected_segment_count() const
693 size_t ret
= this->segment_list_
.size();
695 // If we didn't see a SECTIONS clause in a linker script, we should
696 // already have the complete list of segments. Otherwise we ask the
697 // SECTIONS clause how many segments it expects, and add in the ones
698 // we already have (PT_GNU_STACK, PT_GNU_EH_FRAME, etc.)
700 if (!this->script_options_
->saw_sections_clause())
704 const Script_sections
* ss
= this->script_options_
->script_sections();
705 return ret
+ ss
->expected_segment_count(this);
709 // Handle the .note.GNU-stack section at layout time. SEEN_GNU_STACK
710 // is whether we saw a .note.GNU-stack section in the object file.
711 // GNU_STACK_FLAGS is the section flags. The flags give the
712 // protection required for stack memory. We record this in an
713 // executable as a PT_GNU_STACK segment. If an object file does not
714 // have a .note.GNU-stack segment, we must assume that it is an old
715 // object. On some targets that will force an executable stack.
718 Layout::layout_gnu_stack(bool seen_gnu_stack
, uint64_t gnu_stack_flags
)
721 this->input_without_gnu_stack_note_
= true;
724 this->input_with_gnu_stack_note_
= true;
725 if ((gnu_stack_flags
& elfcpp::SHF_EXECINSTR
) != 0)
726 this->input_requires_executable_stack_
= true;
730 // Create the dynamic sections which are needed before we read the
734 Layout::create_initial_dynamic_sections(Symbol_table
* symtab
)
736 if (parameters
->doing_static_link())
739 this->dynamic_section_
= this->choose_output_section(NULL
, ".dynamic",
742 | elfcpp::SHF_WRITE
),
745 symtab
->define_in_output_data("_DYNAMIC", NULL
, this->dynamic_section_
, 0, 0,
746 elfcpp::STT_OBJECT
, elfcpp::STB_LOCAL
,
747 elfcpp::STV_HIDDEN
, 0, false, false);
749 this->dynamic_data_
= new Output_data_dynamic(&this->dynpool_
);
751 this->dynamic_section_
->add_output_section_data(this->dynamic_data_
);
754 // For each output section whose name can be represented as C symbol,
755 // define __start and __stop symbols for the section. This is a GNU
759 Layout::define_section_symbols(Symbol_table
* symtab
)
761 for (Section_list::const_iterator p
= this->section_list_
.begin();
762 p
!= this->section_list_
.end();
765 const char* const name
= (*p
)->name();
766 if (name
[strspn(name
,
768 "ABCDEFGHIJKLMNOPWRSTUVWXYZ"
769 "abcdefghijklmnopqrstuvwxyz"
773 const std::string
name_string(name
);
774 const std::string
start_name("__start_" + name_string
);
775 const std::string
stop_name("__stop_" + name_string
);
777 symtab
->define_in_output_data(start_name
.c_str(),
786 false, // offset_is_from_end
787 true); // only_if_ref
789 symtab
->define_in_output_data(stop_name
.c_str(),
798 true, // offset_is_from_end
799 true); // only_if_ref
804 // Define symbols for group signatures.
807 Layout::define_group_signatures(Symbol_table
* symtab
)
809 for (Group_signatures::iterator p
= this->group_signatures_
.begin();
810 p
!= this->group_signatures_
.end();
813 Symbol
* sym
= symtab
->lookup(p
->signature
, NULL
);
815 p
->section
->set_info_symndx(sym
);
818 // Force the name of the group section to the group
819 // signature, and use the group's section symbol as the
821 if (strcmp(p
->section
->name(), p
->signature
) != 0)
823 const char* name
= this->namepool_
.add(p
->signature
,
825 p
->section
->set_name(name
);
827 p
->section
->set_needs_symtab_index();
828 p
->section
->set_info_section_symndx(p
->section
);
832 this->group_signatures_
.clear();
835 // Find the first read-only PT_LOAD segment, creating one if
839 Layout::find_first_load_seg()
841 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
842 p
!= this->segment_list_
.end();
845 if ((*p
)->type() == elfcpp::PT_LOAD
846 && ((*p
)->flags() & elfcpp::PF_R
) != 0
847 && ((*p
)->flags() & elfcpp::PF_W
) == 0)
851 gold_assert(!this->script_options_
->saw_phdrs_clause());
853 Output_segment
* load_seg
= this->make_output_segment(elfcpp::PT_LOAD
,
858 // Finalize the layout. When this is called, we have created all the
859 // output sections and all the output segments which are based on
860 // input sections. We have several things to do, and we have to do
861 // them in the right order, so that we get the right results correctly
864 // 1) Finalize the list of output segments and create the segment
867 // 2) Finalize the dynamic symbol table and associated sections.
869 // 3) Determine the final file offset of all the output segments.
871 // 4) Determine the final file offset of all the SHF_ALLOC output
874 // 5) Create the symbol table sections and the section name table
877 // 6) Finalize the symbol table: set symbol values to their final
878 // value and make a final determination of which symbols are going
879 // into the output symbol table.
881 // 7) Create the section table header.
883 // 8) Determine the final file offset of all the output sections which
884 // are not SHF_ALLOC, including the section table header.
886 // 9) Finalize the ELF file header.
888 // This function returns the size of the output file.
891 Layout::finalize(const Input_objects
* input_objects
, Symbol_table
* symtab
,
894 Target
* const target
= parameters
->target();
896 target
->finalize_sections(this);
898 this->count_local_symbols(task
, input_objects
);
900 this->create_gold_note();
901 this->create_executable_stack_info(target
);
903 Output_segment
* phdr_seg
= NULL
;
904 if (!parameters
->output_is_object() && !parameters
->doing_static_link())
906 // There was a dynamic object in the link. We need to create
907 // some information for the dynamic linker.
909 // Create the PT_PHDR segment which will hold the program
911 if (!this->script_options_
->saw_phdrs_clause())
912 phdr_seg
= this->make_output_segment(elfcpp::PT_PHDR
, elfcpp::PF_R
);
914 // Create the dynamic symbol table, including the hash table.
915 Output_section
* dynstr
;
916 std::vector
<Symbol
*> dynamic_symbols
;
917 unsigned int local_dynamic_count
;
918 Versions
versions(this->options_
, &this->dynpool_
);
919 this->create_dynamic_symtab(input_objects
, symtab
, &dynstr
,
920 &local_dynamic_count
, &dynamic_symbols
,
923 // Create the .interp section to hold the name of the
924 // interpreter, and put it in a PT_INTERP segment.
925 if (!parameters
->output_is_shared())
926 this->create_interp(target
);
928 // Finish the .dynamic section to hold the dynamic data, and put
929 // it in a PT_DYNAMIC segment.
930 this->finish_dynamic_section(input_objects
, symtab
);
932 // We should have added everything we need to the dynamic string
934 this->dynpool_
.set_string_offsets();
936 // Create the version sections. We can't do this until the
937 // dynamic string table is complete.
938 this->create_version_sections(&versions
, symtab
, local_dynamic_count
,
939 dynamic_symbols
, dynstr
);
942 // If there is a SECTIONS clause, put all the input sections into
943 // the required order.
944 Output_segment
* load_seg
;
945 if (this->script_options_
->saw_sections_clause())
946 load_seg
= this->set_section_addresses_from_script(symtab
);
947 else if (parameters
->output_is_object())
950 load_seg
= this->find_first_load_seg();
952 gold_assert(phdr_seg
== NULL
|| load_seg
!= NULL
);
954 // Lay out the segment headers.
955 Output_segment_headers
* segment_headers
;
956 if (parameters
->output_is_object())
957 segment_headers
= NULL
;
960 segment_headers
= new Output_segment_headers(this->segment_list_
);
961 if (load_seg
!= NULL
)
962 load_seg
->add_initial_output_data(segment_headers
);
963 if (phdr_seg
!= NULL
)
964 phdr_seg
->add_initial_output_data(segment_headers
);
967 // Lay out the file header.
968 Output_file_header
* file_header
;
969 file_header
= new Output_file_header(target
, symtab
, segment_headers
,
970 this->script_options_
->entry());
971 if (load_seg
!= NULL
)
972 load_seg
->add_initial_output_data(file_header
);
974 this->special_output_list_
.push_back(file_header
);
975 if (segment_headers
!= NULL
)
976 this->special_output_list_
.push_back(segment_headers
);
978 if (this->script_options_
->saw_phdrs_clause()
979 && !parameters
->output_is_object())
981 // Support use of FILEHDRS and PHDRS attachments in a PHDRS
982 // clause in a linker script.
983 Script_sections
* ss
= this->script_options_
->script_sections();
984 ss
->put_headers_in_phdrs(file_header
, segment_headers
);
987 // We set the output section indexes in set_segment_offsets and
988 // set_section_indexes.
989 unsigned int shndx
= 1;
991 // Set the file offsets of all the segments, and all the sections
994 if (!parameters
->output_is_object())
995 off
= this->set_segment_offsets(target
, load_seg
, &shndx
);
997 off
= this->set_relocatable_section_offsets(file_header
, &shndx
);
999 // Set the file offsets of all the non-data sections we've seen so
1000 // far which don't have to wait for the input sections. We need
1001 // this in order to finalize local symbols in non-allocated
1003 off
= this->set_section_offsets(off
, BEFORE_INPUT_SECTIONS_PASS
);
1005 // Create the symbol table sections.
1006 this->create_symtab_sections(input_objects
, symtab
, &off
);
1007 if (!parameters
->doing_static_link())
1008 this->assign_local_dynsym_offsets(input_objects
);
1010 // Process any symbol assignments from a linker script. This must
1011 // be called after the symbol table has been finalized.
1012 this->script_options_
->finalize_symbols(symtab
, this);
1014 // Create the .shstrtab section.
1015 Output_section
* shstrtab_section
= this->create_shstrtab();
1017 // Set the file offsets of the rest of the non-data sections which
1018 // don't have to wait for the input sections.
1019 off
= this->set_section_offsets(off
, BEFORE_INPUT_SECTIONS_PASS
);
1021 // Now that all sections have been created, set the section indexes.
1022 shndx
= this->set_section_indexes(shndx
);
1024 // Create the section table header.
1025 this->create_shdrs(&off
);
1027 // If there are no sections which require postprocessing, we can
1028 // handle the section names now, and avoid a resize later.
1029 if (!this->any_postprocessing_sections_
)
1030 off
= this->set_section_offsets(off
,
1031 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
);
1033 file_header
->set_section_info(this->section_headers_
, shstrtab_section
);
1035 // Now we know exactly where everything goes in the output file
1036 // (except for non-allocated sections which require postprocessing).
1037 Output_data::layout_complete();
1039 this->output_file_size_
= off
;
1044 // Create a .note section for an executable or shared library. This
1045 // records the version of gold used to create the binary.
1048 Layout::create_gold_note()
1050 if (parameters
->output_is_object())
1053 // Authorities all agree that the values in a .note field should
1054 // be aligned on 4-byte boundaries for 32-bit binaries. However,
1055 // they differ on what the alignment is for 64-bit binaries.
1056 // The GABI says unambiguously they take 8-byte alignment:
1057 // http://sco.com/developers/gabi/latest/ch5.pheader.html#note_section
1058 // Other documentation says alignment should always be 4 bytes:
1059 // http://www.netbsd.org/docs/kernel/elf-notes.html#note-format
1060 // GNU ld and GNU readelf both support the latter (at least as of
1061 // version 2.16.91), and glibc always generates the latter for
1062 // .note.ABI-tag (as of version 1.6), so that's the one we go with
1064 #ifdef GABI_FORMAT_FOR_DOTNOTE_SECTION // This is not defined by default.
1065 const int size
= parameters
->get_size();
1067 const int size
= 32;
1070 // The contents of the .note section.
1071 const char* name
= "GNU";
1072 std::string
desc(std::string("gold ") + gold::get_version_string());
1073 size_t namesz
= strlen(name
) + 1;
1074 size_t aligned_namesz
= align_address(namesz
, size
/ 8);
1075 size_t descsz
= desc
.length() + 1;
1076 size_t aligned_descsz
= align_address(descsz
, size
/ 8);
1077 const int note_type
= 4;
1079 size_t notesz
= 3 * (size
/ 8) + aligned_namesz
+ aligned_descsz
;
1081 unsigned char buffer
[128];
1082 gold_assert(sizeof buffer
>= notesz
);
1083 memset(buffer
, 0, notesz
);
1085 bool is_big_endian
= parameters
->is_big_endian();
1091 elfcpp::Swap
<32, false>::writeval(buffer
, namesz
);
1092 elfcpp::Swap
<32, false>::writeval(buffer
+ 4, descsz
);
1093 elfcpp::Swap
<32, false>::writeval(buffer
+ 8, note_type
);
1097 elfcpp::Swap
<32, true>::writeval(buffer
, namesz
);
1098 elfcpp::Swap
<32, true>::writeval(buffer
+ 4, descsz
);
1099 elfcpp::Swap
<32, true>::writeval(buffer
+ 8, note_type
);
1102 else if (size
== 64)
1106 elfcpp::Swap
<64, false>::writeval(buffer
, namesz
);
1107 elfcpp::Swap
<64, false>::writeval(buffer
+ 8, descsz
);
1108 elfcpp::Swap
<64, false>::writeval(buffer
+ 16, note_type
);
1112 elfcpp::Swap
<64, true>::writeval(buffer
, namesz
);
1113 elfcpp::Swap
<64, true>::writeval(buffer
+ 8, descsz
);
1114 elfcpp::Swap
<64, true>::writeval(buffer
+ 16, note_type
);
1120 memcpy(buffer
+ 3 * (size
/ 8), name
, namesz
);
1121 memcpy(buffer
+ 3 * (size
/ 8) + aligned_namesz
, desc
.data(), descsz
);
1123 const char* note_name
= this->namepool_
.add(".note", false, NULL
);
1124 Output_section
* os
= this->make_output_section(note_name
,
1127 Output_section_data
* posd
= new Output_data_const(buffer
, notesz
,
1129 os
->add_output_section_data(posd
);
1132 // Record whether the stack should be executable. This can be set
1133 // from the command line using the -z execstack or -z noexecstack
1134 // options. Otherwise, if any input file has a .note.GNU-stack
1135 // section with the SHF_EXECINSTR flag set, the stack should be
1136 // executable. Otherwise, if at least one input file a
1137 // .note.GNU-stack section, and some input file has no .note.GNU-stack
1138 // section, we use the target default for whether the stack should be
1139 // executable. Otherwise, we don't generate a stack note. When
1140 // generating a object file, we create a .note.GNU-stack section with
1141 // the appropriate marking. When generating an executable or shared
1142 // library, we create a PT_GNU_STACK segment.
1145 Layout::create_executable_stack_info(const Target
* target
)
1147 bool is_stack_executable
;
1148 if (this->options_
.is_execstack_set())
1149 is_stack_executable
= this->options_
.is_stack_executable();
1150 else if (!this->input_with_gnu_stack_note_
)
1154 if (this->input_requires_executable_stack_
)
1155 is_stack_executable
= true;
1156 else if (this->input_without_gnu_stack_note_
)
1157 is_stack_executable
= target
->is_default_stack_executable();
1159 is_stack_executable
= false;
1162 if (parameters
->output_is_object())
1164 const char* name
= this->namepool_
.add(".note.GNU-stack", false, NULL
);
1165 elfcpp::Elf_Xword flags
= 0;
1166 if (is_stack_executable
)
1167 flags
|= elfcpp::SHF_EXECINSTR
;
1168 this->make_output_section(name
, elfcpp::SHT_PROGBITS
, flags
);
1172 if (this->script_options_
->saw_phdrs_clause())
1174 int flags
= elfcpp::PF_R
| elfcpp::PF_W
;
1175 if (is_stack_executable
)
1176 flags
|= elfcpp::PF_X
;
1177 this->make_output_segment(elfcpp::PT_GNU_STACK
, flags
);
1181 // Return whether SEG1 should be before SEG2 in the output file. This
1182 // is based entirely on the segment type and flags. When this is
1183 // called the segment addresses has normally not yet been set.
1186 Layout::segment_precedes(const Output_segment
* seg1
,
1187 const Output_segment
* seg2
)
1189 elfcpp::Elf_Word type1
= seg1
->type();
1190 elfcpp::Elf_Word type2
= seg2
->type();
1192 // The single PT_PHDR segment is required to precede any loadable
1193 // segment. We simply make it always first.
1194 if (type1
== elfcpp::PT_PHDR
)
1196 gold_assert(type2
!= elfcpp::PT_PHDR
);
1199 if (type2
== elfcpp::PT_PHDR
)
1202 // The single PT_INTERP segment is required to precede any loadable
1203 // segment. We simply make it always second.
1204 if (type1
== elfcpp::PT_INTERP
)
1206 gold_assert(type2
!= elfcpp::PT_INTERP
);
1209 if (type2
== elfcpp::PT_INTERP
)
1212 // We then put PT_LOAD segments before any other segments.
1213 if (type1
== elfcpp::PT_LOAD
&& type2
!= elfcpp::PT_LOAD
)
1215 if (type2
== elfcpp::PT_LOAD
&& type1
!= elfcpp::PT_LOAD
)
1218 // We put the PT_TLS segment last, because that is where the dynamic
1219 // linker expects to find it (this is just for efficiency; other
1220 // positions would also work correctly).
1221 if (type1
== elfcpp::PT_TLS
&& type2
!= elfcpp::PT_TLS
)
1223 if (type2
== elfcpp::PT_TLS
&& type1
!= elfcpp::PT_TLS
)
1226 const elfcpp::Elf_Word flags1
= seg1
->flags();
1227 const elfcpp::Elf_Word flags2
= seg2
->flags();
1229 // The order of non-PT_LOAD segments is unimportant. We simply sort
1230 // by the numeric segment type and flags values. There should not
1231 // be more than one segment with the same type and flags.
1232 if (type1
!= elfcpp::PT_LOAD
)
1235 return type1
< type2
;
1236 gold_assert(flags1
!= flags2
);
1237 return flags1
< flags2
;
1240 // If the addresses are set already, sort by load address.
1241 if (seg1
->are_addresses_set())
1243 if (!seg2
->are_addresses_set())
1246 unsigned int section_count1
= seg1
->output_section_count();
1247 unsigned int section_count2
= seg2
->output_section_count();
1248 if (section_count1
== 0 && section_count2
> 0)
1250 if (section_count1
> 0 && section_count2
== 0)
1253 uint64_t paddr1
= seg1
->first_section_load_address();
1254 uint64_t paddr2
= seg2
->first_section_load_address();
1255 if (paddr1
!= paddr2
)
1256 return paddr1
< paddr2
;
1258 else if (seg2
->are_addresses_set())
1261 // We sort PT_LOAD segments based on the flags. Readonly segments
1262 // come before writable segments. Then executable segments come
1263 // before non-executable segments. Then the unlikely case of a
1264 // non-readable segment comes before the normal case of a readable
1265 // segment. If there are multiple segments with the same type and
1266 // flags, we require that the address be set, and we sort by
1267 // virtual address and then physical address.
1268 if ((flags1
& elfcpp::PF_W
) != (flags2
& elfcpp::PF_W
))
1269 return (flags1
& elfcpp::PF_W
) == 0;
1270 if ((flags1
& elfcpp::PF_X
) != (flags2
& elfcpp::PF_X
))
1271 return (flags1
& elfcpp::PF_X
) != 0;
1272 if ((flags1
& elfcpp::PF_R
) != (flags2
& elfcpp::PF_R
))
1273 return (flags1
& elfcpp::PF_R
) == 0;
1275 // We shouldn't get here--we shouldn't create segments which we
1276 // can't distinguish.
1280 // Set the file offsets of all the segments, and all the sections they
1281 // contain. They have all been created. LOAD_SEG must be be laid out
1282 // first. Return the offset of the data to follow.
1285 Layout::set_segment_offsets(const Target
* target
, Output_segment
* load_seg
,
1286 unsigned int *pshndx
)
1288 // Sort them into the final order.
1289 std::sort(this->segment_list_
.begin(), this->segment_list_
.end(),
1290 Layout::Compare_segments());
1292 // Find the PT_LOAD segments, and set their addresses and offsets
1293 // and their section's addresses and offsets.
1295 if (this->options_
.user_set_text_segment_address())
1296 addr
= options_
.text_segment_address();
1297 else if (parameters
->output_is_shared())
1300 addr
= target
->default_text_segment_address();
1303 // If LOAD_SEG is NULL, then the file header and segment headers
1304 // will not be loadable. But they still need to be at offset 0 in
1305 // the file. Set their offsets now.
1306 if (load_seg
== NULL
)
1308 for (Data_list::iterator p
= this->special_output_list_
.begin();
1309 p
!= this->special_output_list_
.end();
1312 off
= align_address(off
, (*p
)->addralign());
1313 (*p
)->set_address_and_file_offset(0, off
);
1314 off
+= (*p
)->data_size();
1318 bool was_readonly
= false;
1319 for (Segment_list::iterator p
= this->segment_list_
.begin();
1320 p
!= this->segment_list_
.end();
1323 if ((*p
)->type() == elfcpp::PT_LOAD
)
1325 if (load_seg
!= NULL
&& load_seg
!= *p
)
1329 uint64_t orig_addr
= addr
;
1330 uint64_t orig_off
= off
;
1332 uint64_t aligned_addr
= 0;
1333 uint64_t abi_pagesize
= target
->abi_pagesize();
1335 // FIXME: This should depend on the -n and -N options.
1336 (*p
)->set_minimum_p_align(target
->common_pagesize());
1338 bool are_addresses_set
= (*p
)->are_addresses_set();
1339 if (are_addresses_set
)
1341 // When it comes to setting file offsets, we care about
1342 // the physical address.
1343 addr
= (*p
)->paddr();
1345 // Adjust the file offset to the same address modulo the
1347 uint64_t unsigned_off
= off
;
1348 uint64_t aligned_off
= ((unsigned_off
& ~(abi_pagesize
- 1))
1349 | (addr
& (abi_pagesize
- 1)));
1350 if (aligned_off
< unsigned_off
)
1351 aligned_off
+= abi_pagesize
;
1356 // If the last segment was readonly, and this one is
1357 // not, then skip the address forward one page,
1358 // maintaining the same position within the page. This
1359 // lets us store both segments overlapping on a single
1360 // page in the file, but the loader will put them on
1361 // different pages in memory.
1363 addr
= align_address(addr
, (*p
)->maximum_alignment());
1364 aligned_addr
= addr
;
1366 if (was_readonly
&& ((*p
)->flags() & elfcpp::PF_W
) != 0)
1368 if ((addr
& (abi_pagesize
- 1)) != 0)
1369 addr
= addr
+ abi_pagesize
;
1372 off
= orig_off
+ ((addr
- orig_addr
) & (abi_pagesize
- 1));
1375 unsigned int shndx_hold
= *pshndx
;
1376 uint64_t new_addr
= (*p
)->set_section_addresses(false, addr
, &off
,
1379 // Now that we know the size of this segment, we may be able
1380 // to save a page in memory, at the cost of wasting some
1381 // file space, by instead aligning to the start of a new
1382 // page. Here we use the real machine page size rather than
1383 // the ABI mandated page size.
1385 if (!are_addresses_set
&& aligned_addr
!= addr
)
1387 uint64_t common_pagesize
= target
->common_pagesize();
1388 uint64_t first_off
= (common_pagesize
1390 & (common_pagesize
- 1)));
1391 uint64_t last_off
= new_addr
& (common_pagesize
- 1);
1394 && ((aligned_addr
& ~ (common_pagesize
- 1))
1395 != (new_addr
& ~ (common_pagesize
- 1)))
1396 && first_off
+ last_off
<= common_pagesize
)
1398 *pshndx
= shndx_hold
;
1399 addr
= align_address(aligned_addr
, common_pagesize
);
1400 addr
= align_address(addr
, (*p
)->maximum_alignment());
1401 off
= orig_off
+ ((addr
- orig_addr
) & (abi_pagesize
- 1));
1402 new_addr
= (*p
)->set_section_addresses(true, addr
, &off
,
1409 if (((*p
)->flags() & elfcpp::PF_W
) == 0)
1410 was_readonly
= true;
1414 // Handle the non-PT_LOAD segments, setting their offsets from their
1415 // section's offsets.
1416 for (Segment_list::iterator p
= this->segment_list_
.begin();
1417 p
!= this->segment_list_
.end();
1420 if ((*p
)->type() != elfcpp::PT_LOAD
)
1424 // Set the TLS offsets for each section in the PT_TLS segment.
1425 if (this->tls_segment_
!= NULL
)
1426 this->tls_segment_
->set_tls_offsets();
1431 // Set the offsets of all the allocated sections when doing a
1432 // relocatable link. This does the same jobs as set_segment_offsets,
1433 // only for a relocatable link.
1436 Layout::set_relocatable_section_offsets(Output_data
* file_header
,
1437 unsigned int *pshndx
)
1441 file_header
->set_address_and_file_offset(0, 0);
1442 off
+= file_header
->data_size();
1444 for (Section_list::iterator p
= this->section_list_
.begin();
1445 p
!= this->section_list_
.end();
1448 // We skip unallocated sections here, except that group sections
1449 // have to come first.
1450 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) == 0
1451 && (*p
)->type() != elfcpp::SHT_GROUP
)
1454 off
= align_address(off
, (*p
)->addralign());
1456 // The linker script might have set the address.
1457 if (!(*p
)->is_address_valid())
1458 (*p
)->set_address(0);
1459 (*p
)->set_file_offset(off
);
1460 (*p
)->finalize_data_size();
1461 off
+= (*p
)->data_size();
1463 (*p
)->set_out_shndx(*pshndx
);
1470 // Set the file offset of all the sections not associated with a
1474 Layout::set_section_offsets(off_t off
, Layout::Section_offset_pass pass
)
1476 for (Section_list::iterator p
= this->unattached_section_list_
.begin();
1477 p
!= this->unattached_section_list_
.end();
1480 // The symtab section is handled in create_symtab_sections.
1481 if (*p
== this->symtab_section_
)
1484 // If we've already set the data size, don't set it again.
1485 if ((*p
)->is_offset_valid() && (*p
)->is_data_size_valid())
1488 if (pass
== BEFORE_INPUT_SECTIONS_PASS
1489 && (*p
)->requires_postprocessing())
1491 (*p
)->create_postprocessing_buffer();
1492 this->any_postprocessing_sections_
= true;
1495 if (pass
== BEFORE_INPUT_SECTIONS_PASS
1496 && (*p
)->after_input_sections())
1498 else if (pass
== POSTPROCESSING_SECTIONS_PASS
1499 && (!(*p
)->after_input_sections()
1500 || (*p
)->type() == elfcpp::SHT_STRTAB
))
1502 else if (pass
== STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
1503 && (!(*p
)->after_input_sections()
1504 || (*p
)->type() != elfcpp::SHT_STRTAB
))
1507 off
= align_address(off
, (*p
)->addralign());
1508 (*p
)->set_file_offset(off
);
1509 (*p
)->finalize_data_size();
1510 off
+= (*p
)->data_size();
1512 // At this point the name must be set.
1513 if (pass
!= STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
)
1514 this->namepool_
.add((*p
)->name(), false, NULL
);
1519 // Set the section indexes of all the sections not associated with a
1523 Layout::set_section_indexes(unsigned int shndx
)
1525 const bool output_is_object
= parameters
->output_is_object();
1526 for (Section_list::iterator p
= this->unattached_section_list_
.begin();
1527 p
!= this->unattached_section_list_
.end();
1530 // In a relocatable link, we already did group sections.
1531 if (output_is_object
1532 && (*p
)->type() == elfcpp::SHT_GROUP
)
1535 (*p
)->set_out_shndx(shndx
);
1541 // Set the section addresses according to the linker script. This is
1542 // only called when we see a SECTIONS clause. This returns the
1543 // program segment which should hold the file header and segment
1544 // headers, if any. It will return NULL if they should not be in a
1548 Layout::set_section_addresses_from_script(Symbol_table
* symtab
)
1550 Script_sections
* ss
= this->script_options_
->script_sections();
1551 gold_assert(ss
->saw_sections_clause());
1553 // Place each orphaned output section in the script.
1554 for (Section_list::iterator p
= this->section_list_
.begin();
1555 p
!= this->section_list_
.end();
1558 if (!(*p
)->found_in_sections_clause())
1559 ss
->place_orphan(*p
);
1562 return this->script_options_
->set_section_addresses(symtab
, this);
1565 // Count the local symbols in the regular symbol table and the dynamic
1566 // symbol table, and build the respective string pools.
1569 Layout::count_local_symbols(const Task
* task
,
1570 const Input_objects
* input_objects
)
1572 // First, figure out an upper bound on the number of symbols we'll
1573 // be inserting into each pool. This helps us create the pools with
1574 // the right size, to avoid unnecessary hashtable resizing.
1575 unsigned int symbol_count
= 0;
1576 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
1577 p
!= input_objects
->relobj_end();
1579 symbol_count
+= (*p
)->local_symbol_count();
1581 // Go from "upper bound" to "estimate." We overcount for two
1582 // reasons: we double-count symbols that occur in more than one
1583 // object file, and we count symbols that are dropped from the
1584 // output. Add it all together and assume we overcount by 100%.
1587 // We assume all symbols will go into both the sympool and dynpool.
1588 this->sympool_
.reserve(symbol_count
);
1589 this->dynpool_
.reserve(symbol_count
);
1591 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
1592 p
!= input_objects
->relobj_end();
1595 Task_lock_obj
<Object
> tlo(task
, *p
);
1596 (*p
)->count_local_symbols(&this->sympool_
, &this->dynpool_
);
1600 // Create the symbol table sections. Here we also set the final
1601 // values of the symbols. At this point all the loadable sections are
1605 Layout::create_symtab_sections(const Input_objects
* input_objects
,
1606 Symbol_table
* symtab
,
1611 if (parameters
->get_size() == 32)
1613 symsize
= elfcpp::Elf_sizes
<32>::sym_size
;
1616 else if (parameters
->get_size() == 64)
1618 symsize
= elfcpp::Elf_sizes
<64>::sym_size
;
1625 off
= align_address(off
, align
);
1626 off_t startoff
= off
;
1628 // Save space for the dummy symbol at the start of the section. We
1629 // never bother to write this out--it will just be left as zero.
1631 unsigned int local_symbol_index
= 1;
1633 // Add STT_SECTION symbols for each Output section which needs one.
1634 for (Section_list::iterator p
= this->section_list_
.begin();
1635 p
!= this->section_list_
.end();
1638 if (!(*p
)->needs_symtab_index())
1639 (*p
)->set_symtab_index(-1U);
1642 (*p
)->set_symtab_index(local_symbol_index
);
1643 ++local_symbol_index
;
1648 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
1649 p
!= input_objects
->relobj_end();
1652 unsigned int index
= (*p
)->finalize_local_symbols(local_symbol_index
,
1654 off
+= (index
- local_symbol_index
) * symsize
;
1655 local_symbol_index
= index
;
1658 unsigned int local_symcount
= local_symbol_index
;
1659 gold_assert(local_symcount
* symsize
== off
- startoff
);
1662 size_t dyn_global_index
;
1664 if (this->dynsym_section_
== NULL
)
1667 dyn_global_index
= 0;
1672 dyn_global_index
= this->dynsym_section_
->info();
1673 off_t locsize
= dyn_global_index
* this->dynsym_section_
->entsize();
1674 dynoff
= this->dynsym_section_
->offset() + locsize
;
1675 dyncount
= (this->dynsym_section_
->data_size() - locsize
) / symsize
;
1676 gold_assert(static_cast<off_t
>(dyncount
* symsize
)
1677 == this->dynsym_section_
->data_size() - locsize
);
1680 off
= symtab
->finalize(off
, dynoff
, dyn_global_index
, dyncount
,
1681 &this->sympool_
, &local_symcount
);
1683 if (!parameters
->strip_all())
1685 this->sympool_
.set_string_offsets();
1687 const char* symtab_name
= this->namepool_
.add(".symtab", false, NULL
);
1688 Output_section
* osymtab
= this->make_output_section(symtab_name
,
1691 this->symtab_section_
= osymtab
;
1693 Output_section_data
* pos
= new Output_data_fixed_space(off
- startoff
,
1695 osymtab
->add_output_section_data(pos
);
1697 const char* strtab_name
= this->namepool_
.add(".strtab", false, NULL
);
1698 Output_section
* ostrtab
= this->make_output_section(strtab_name
,
1702 Output_section_data
* pstr
= new Output_data_strtab(&this->sympool_
);
1703 ostrtab
->add_output_section_data(pstr
);
1705 osymtab
->set_file_offset(startoff
);
1706 osymtab
->finalize_data_size();
1707 osymtab
->set_link_section(ostrtab
);
1708 osymtab
->set_info(local_symcount
);
1709 osymtab
->set_entsize(symsize
);
1715 // Create the .shstrtab section, which holds the names of the
1716 // sections. At the time this is called, we have created all the
1717 // output sections except .shstrtab itself.
1720 Layout::create_shstrtab()
1722 // FIXME: We don't need to create a .shstrtab section if we are
1723 // stripping everything.
1725 const char* name
= this->namepool_
.add(".shstrtab", false, NULL
);
1727 Output_section
* os
= this->make_output_section(name
, elfcpp::SHT_STRTAB
, 0);
1729 // We can't write out this section until we've set all the section
1730 // names, and we don't set the names of compressed output sections
1731 // until relocations are complete.
1732 os
->set_after_input_sections();
1734 Output_section_data
* posd
= new Output_data_strtab(&this->namepool_
);
1735 os
->add_output_section_data(posd
);
1740 // Create the section headers. SIZE is 32 or 64. OFF is the file
1744 Layout::create_shdrs(off_t
* poff
)
1746 Output_section_headers
* oshdrs
;
1747 oshdrs
= new Output_section_headers(this,
1748 &this->segment_list_
,
1749 &this->section_list_
,
1750 &this->unattached_section_list_
,
1752 off_t off
= align_address(*poff
, oshdrs
->addralign());
1753 oshdrs
->set_address_and_file_offset(0, off
);
1754 off
+= oshdrs
->data_size();
1756 this->section_headers_
= oshdrs
;
1759 // Create the dynamic symbol table.
1762 Layout::create_dynamic_symtab(const Input_objects
* input_objects
,
1763 Symbol_table
* symtab
,
1764 Output_section
**pdynstr
,
1765 unsigned int* plocal_dynamic_count
,
1766 std::vector
<Symbol
*>* pdynamic_symbols
,
1767 Versions
* pversions
)
1769 // Count all the symbols in the dynamic symbol table, and set the
1770 // dynamic symbol indexes.
1772 // Skip symbol 0, which is always all zeroes.
1773 unsigned int index
= 1;
1775 // Add STT_SECTION symbols for each Output section which needs one.
1776 for (Section_list::iterator p
= this->section_list_
.begin();
1777 p
!= this->section_list_
.end();
1780 if (!(*p
)->needs_dynsym_index())
1781 (*p
)->set_dynsym_index(-1U);
1784 (*p
)->set_dynsym_index(index
);
1789 // Count the local symbols that need to go in the dynamic symbol table,
1790 // and set the dynamic symbol indexes.
1791 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
1792 p
!= input_objects
->relobj_end();
1795 unsigned int new_index
= (*p
)->set_local_dynsym_indexes(index
);
1799 unsigned int local_symcount
= index
;
1800 *plocal_dynamic_count
= local_symcount
;
1802 // FIXME: We have to tell set_dynsym_indexes whether the
1803 // -E/--export-dynamic option was used.
1804 index
= symtab
->set_dynsym_indexes(index
, pdynamic_symbols
,
1805 &this->dynpool_
, pversions
);
1809 const int size
= parameters
->get_size();
1812 symsize
= elfcpp::Elf_sizes
<32>::sym_size
;
1815 else if (size
== 64)
1817 symsize
= elfcpp::Elf_sizes
<64>::sym_size
;
1823 // Create the dynamic symbol table section.
1825 Output_section
* dynsym
= this->choose_output_section(NULL
, ".dynsym",
1830 Output_section_data
* odata
= new Output_data_fixed_space(index
* symsize
,
1832 dynsym
->add_output_section_data(odata
);
1834 dynsym
->set_info(local_symcount
);
1835 dynsym
->set_entsize(symsize
);
1836 dynsym
->set_addralign(align
);
1838 this->dynsym_section_
= dynsym
;
1840 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
1841 odyn
->add_section_address(elfcpp::DT_SYMTAB
, dynsym
);
1842 odyn
->add_constant(elfcpp::DT_SYMENT
, symsize
);
1844 // Create the dynamic string table section.
1846 Output_section
* dynstr
= this->choose_output_section(NULL
, ".dynstr",
1851 Output_section_data
* strdata
= new Output_data_strtab(&this->dynpool_
);
1852 dynstr
->add_output_section_data(strdata
);
1854 dynsym
->set_link_section(dynstr
);
1855 this->dynamic_section_
->set_link_section(dynstr
);
1857 odyn
->add_section_address(elfcpp::DT_STRTAB
, dynstr
);
1858 odyn
->add_section_size(elfcpp::DT_STRSZ
, dynstr
);
1862 // Create the hash tables.
1864 // FIXME: We need an option to create a GNU hash table.
1866 unsigned char* phash
;
1867 unsigned int hashlen
;
1868 Dynobj::create_elf_hash_table(*pdynamic_symbols
, local_symcount
,
1871 Output_section
* hashsec
= this->choose_output_section(NULL
, ".hash",
1876 Output_section_data
* hashdata
= new Output_data_const_buffer(phash
,
1879 hashsec
->add_output_section_data(hashdata
);
1881 hashsec
->set_link_section(dynsym
);
1882 hashsec
->set_entsize(4);
1884 odyn
->add_section_address(elfcpp::DT_HASH
, hashsec
);
1887 // Assign offsets to each local portion of the dynamic symbol table.
1890 Layout::assign_local_dynsym_offsets(const Input_objects
* input_objects
)
1892 Output_section
* dynsym
= this->dynsym_section_
;
1893 gold_assert(dynsym
!= NULL
);
1895 off_t off
= dynsym
->offset();
1897 // Skip the dummy symbol at the start of the section.
1898 off
+= dynsym
->entsize();
1900 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
1901 p
!= input_objects
->relobj_end();
1904 unsigned int count
= (*p
)->set_local_dynsym_offset(off
);
1905 off
+= count
* dynsym
->entsize();
1909 // Create the version sections.
1912 Layout::create_version_sections(const Versions
* versions
,
1913 const Symbol_table
* symtab
,
1914 unsigned int local_symcount
,
1915 const std::vector
<Symbol
*>& dynamic_symbols
,
1916 const Output_section
* dynstr
)
1918 if (!versions
->any_defs() && !versions
->any_needs())
1921 if (parameters
->get_size() == 32)
1923 if (parameters
->is_big_endian())
1925 #ifdef HAVE_TARGET_32_BIG
1926 this->sized_create_version_sections
1927 SELECT_SIZE_ENDIAN_NAME(32, true)(
1928 versions
, symtab
, local_symcount
, dynamic_symbols
, dynstr
1929 SELECT_SIZE_ENDIAN(32, true));
1936 #ifdef HAVE_TARGET_32_LITTLE
1937 this->sized_create_version_sections
1938 SELECT_SIZE_ENDIAN_NAME(32, false)(
1939 versions
, symtab
, local_symcount
, dynamic_symbols
, dynstr
1940 SELECT_SIZE_ENDIAN(32, false));
1946 else if (parameters
->get_size() == 64)
1948 if (parameters
->is_big_endian())
1950 #ifdef HAVE_TARGET_64_BIG
1951 this->sized_create_version_sections
1952 SELECT_SIZE_ENDIAN_NAME(64, true)(
1953 versions
, symtab
, local_symcount
, dynamic_symbols
, dynstr
1954 SELECT_SIZE_ENDIAN(64, true));
1961 #ifdef HAVE_TARGET_64_LITTLE
1962 this->sized_create_version_sections
1963 SELECT_SIZE_ENDIAN_NAME(64, false)(
1964 versions
, symtab
, local_symcount
, dynamic_symbols
, dynstr
1965 SELECT_SIZE_ENDIAN(64, false));
1975 // Create the version sections, sized version.
1977 template<int size
, bool big_endian
>
1979 Layout::sized_create_version_sections(
1980 const Versions
* versions
,
1981 const Symbol_table
* symtab
,
1982 unsigned int local_symcount
,
1983 const std::vector
<Symbol
*>& dynamic_symbols
,
1984 const Output_section
* dynstr
1987 Output_section
* vsec
= this->choose_output_section(NULL
, ".gnu.version",
1988 elfcpp::SHT_GNU_versym
,
1992 unsigned char* vbuf
;
1994 versions
->symbol_section_contents
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
)(
1995 symtab
, &this->dynpool_
, local_symcount
, dynamic_symbols
, &vbuf
, &vsize
1996 SELECT_SIZE_ENDIAN(size
, big_endian
));
1998 Output_section_data
* vdata
= new Output_data_const_buffer(vbuf
, vsize
, 2);
2000 vsec
->add_output_section_data(vdata
);
2001 vsec
->set_entsize(2);
2002 vsec
->set_link_section(this->dynsym_section_
);
2004 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
2005 odyn
->add_section_address(elfcpp::DT_VERSYM
, vsec
);
2007 if (versions
->any_defs())
2009 Output_section
* vdsec
;
2010 vdsec
= this->choose_output_section(NULL
, ".gnu.version_d",
2011 elfcpp::SHT_GNU_verdef
,
2015 unsigned char* vdbuf
;
2016 unsigned int vdsize
;
2017 unsigned int vdentries
;
2018 versions
->def_section_contents
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
)(
2019 &this->dynpool_
, &vdbuf
, &vdsize
, &vdentries
2020 SELECT_SIZE_ENDIAN(size
, big_endian
));
2022 Output_section_data
* vddata
= new Output_data_const_buffer(vdbuf
,
2026 vdsec
->add_output_section_data(vddata
);
2027 vdsec
->set_link_section(dynstr
);
2028 vdsec
->set_info(vdentries
);
2030 odyn
->add_section_address(elfcpp::DT_VERDEF
, vdsec
);
2031 odyn
->add_constant(elfcpp::DT_VERDEFNUM
, vdentries
);
2034 if (versions
->any_needs())
2036 Output_section
* vnsec
;
2037 vnsec
= this->choose_output_section(NULL
, ".gnu.version_r",
2038 elfcpp::SHT_GNU_verneed
,
2042 unsigned char* vnbuf
;
2043 unsigned int vnsize
;
2044 unsigned int vnentries
;
2045 versions
->need_section_contents
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
)
2046 (&this->dynpool_
, &vnbuf
, &vnsize
, &vnentries
2047 SELECT_SIZE_ENDIAN(size
, big_endian
));
2049 Output_section_data
* vndata
= new Output_data_const_buffer(vnbuf
,
2053 vnsec
->add_output_section_data(vndata
);
2054 vnsec
->set_link_section(dynstr
);
2055 vnsec
->set_info(vnentries
);
2057 odyn
->add_section_address(elfcpp::DT_VERNEED
, vnsec
);
2058 odyn
->add_constant(elfcpp::DT_VERNEEDNUM
, vnentries
);
2062 // Create the .interp section and PT_INTERP segment.
2065 Layout::create_interp(const Target
* target
)
2067 const char* interp
= this->options_
.dynamic_linker();
2070 interp
= target
->dynamic_linker();
2071 gold_assert(interp
!= NULL
);
2074 size_t len
= strlen(interp
) + 1;
2076 Output_section_data
* odata
= new Output_data_const(interp
, len
, 1);
2078 Output_section
* osec
= this->choose_output_section(NULL
, ".interp",
2079 elfcpp::SHT_PROGBITS
,
2082 osec
->add_output_section_data(odata
);
2084 if (!this->script_options_
->saw_phdrs_clause())
2086 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_INTERP
,
2088 oseg
->add_initial_output_section(osec
, elfcpp::PF_R
);
2092 // Finish the .dynamic section and PT_DYNAMIC segment.
2095 Layout::finish_dynamic_section(const Input_objects
* input_objects
,
2096 const Symbol_table
* symtab
)
2098 if (!this->script_options_
->saw_phdrs_clause())
2100 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_DYNAMIC
,
2103 oseg
->add_initial_output_section(this->dynamic_section_
,
2104 elfcpp::PF_R
| elfcpp::PF_W
);
2107 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
2109 for (Input_objects::Dynobj_iterator p
= input_objects
->dynobj_begin();
2110 p
!= input_objects
->dynobj_end();
2113 // FIXME: Handle --as-needed.
2114 odyn
->add_string(elfcpp::DT_NEEDED
, (*p
)->soname());
2117 if (parameters
->output_is_shared())
2119 const char* soname
= this->options_
.soname();
2121 odyn
->add_string(elfcpp::DT_SONAME
, soname
);
2124 // FIXME: Support --init and --fini.
2125 Symbol
* sym
= symtab
->lookup("_init");
2126 if (sym
!= NULL
&& sym
->is_defined() && !sym
->is_from_dynobj())
2127 odyn
->add_symbol(elfcpp::DT_INIT
, sym
);
2129 sym
= symtab
->lookup("_fini");
2130 if (sym
!= NULL
&& sym
->is_defined() && !sym
->is_from_dynobj())
2131 odyn
->add_symbol(elfcpp::DT_FINI
, sym
);
2133 // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY.
2135 // Add a DT_RPATH entry if needed.
2136 const General_options::Dir_list
& rpath(this->options_
.rpath());
2139 std::string rpath_val
;
2140 for (General_options::Dir_list::const_iterator p
= rpath
.begin();
2144 if (rpath_val
.empty())
2145 rpath_val
= p
->name();
2148 // Eliminate duplicates.
2149 General_options::Dir_list::const_iterator q
;
2150 for (q
= rpath
.begin(); q
!= p
; ++q
)
2151 if (q
->name() == p
->name())
2156 rpath_val
+= p
->name();
2161 odyn
->add_string(elfcpp::DT_RPATH
, rpath_val
);
2164 // Look for text segments that have dynamic relocations.
2165 bool have_textrel
= false;
2166 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
2167 p
!= this->segment_list_
.end();
2170 if (((*p
)->flags() & elfcpp::PF_W
) == 0
2171 && (*p
)->dynamic_reloc_count() > 0)
2173 have_textrel
= true;
2178 // Add a DT_FLAGS entry. We add it even if no flags are set so that
2179 // post-link tools can easily modify these flags if desired.
2180 unsigned int flags
= 0;
2183 // Add a DT_TEXTREL for compatibility with older loaders.
2184 odyn
->add_constant(elfcpp::DT_TEXTREL
, 0);
2185 flags
|= elfcpp::DF_TEXTREL
;
2187 if (parameters
->output_is_shared() && this->has_static_tls())
2188 flags
|= elfcpp::DF_STATIC_TLS
;
2189 odyn
->add_constant(elfcpp::DT_FLAGS
, flags
);
2192 // The mapping of .gnu.linkonce section names to real section names.
2194 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
2195 const Layout::Linkonce_mapping
Layout::linkonce_mapping
[] =
2197 MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d".
2198 MAPPING_INIT("t", ".text"),
2199 MAPPING_INIT("r", ".rodata"),
2200 MAPPING_INIT("d", ".data"),
2201 MAPPING_INIT("b", ".bss"),
2202 MAPPING_INIT("s", ".sdata"),
2203 MAPPING_INIT("sb", ".sbss"),
2204 MAPPING_INIT("s2", ".sdata2"),
2205 MAPPING_INIT("sb2", ".sbss2"),
2206 MAPPING_INIT("wi", ".debug_info"),
2207 MAPPING_INIT("td", ".tdata"),
2208 MAPPING_INIT("tb", ".tbss"),
2209 MAPPING_INIT("lr", ".lrodata"),
2210 MAPPING_INIT("l", ".ldata"),
2211 MAPPING_INIT("lb", ".lbss"),
2215 const int Layout::linkonce_mapping_count
=
2216 sizeof(Layout::linkonce_mapping
) / sizeof(Layout::linkonce_mapping
[0]);
2218 // Return the name of the output section to use for a .gnu.linkonce
2219 // section. This is based on the default ELF linker script of the old
2220 // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
2221 // to ".text". Set *PLEN to the length of the name. *PLEN is
2222 // initialized to the length of NAME.
2225 Layout::linkonce_output_name(const char* name
, size_t *plen
)
2227 const char* s
= name
+ sizeof(".gnu.linkonce") - 1;
2231 const Linkonce_mapping
* plm
= linkonce_mapping
;
2232 for (int i
= 0; i
< linkonce_mapping_count
; ++i
, ++plm
)
2234 if (strncmp(s
, plm
->from
, plm
->fromlen
) == 0 && s
[plm
->fromlen
] == '.')
2243 // Choose the output section name to use given an input section name.
2244 // Set *PLEN to the length of the name. *PLEN is initialized to the
2248 Layout::output_section_name(const char* name
, size_t* plen
)
2250 if (Layout::is_linkonce(name
))
2252 // .gnu.linkonce sections are laid out as though they were named
2253 // for the sections are placed into.
2254 return Layout::linkonce_output_name(name
, plen
);
2257 // gcc 4.3 generates the following sorts of section names when it
2258 // needs a section name specific to a function:
2264 // .data.rel.local.FN
2266 // .data.rel.ro.local.FN
2273 // The GNU linker maps all of those to the part before the .FN,
2274 // except that .data.rel.local.FN is mapped to .data, and
2275 // .data.rel.ro.local.FN is mapped to .data.rel.ro. The sections
2276 // beginning with .data.rel.ro.local are grouped together.
2278 // For an anonymous namespace, the string FN can contain a '.'.
2280 // Also of interest: .rodata.strN.N, .rodata.cstN, both of which the
2281 // GNU linker maps to .rodata.
2283 // The .data.rel.ro sections enable a security feature triggered by
2284 // the -z relro option. Section which need to be relocated at
2285 // program startup time but which may be readonly after startup are
2286 // grouped into .data.rel.ro. They are then put into a PT_GNU_RELRO
2287 // segment. The dynamic linker will make that segment writable,
2288 // perform relocations, and then make it read-only. FIXME: We do
2289 // not yet implement this optimization.
2291 // It is hard to handle this in a principled way.
2293 // These are the rules we follow:
2295 // If the section name has no initial '.', or no dot other than an
2296 // initial '.', we use the name unchanged (i.e., "mysection" and
2297 // ".text" are unchanged).
2299 // If the name starts with ".data.rel.ro" we use ".data.rel.ro".
2301 // Otherwise, we drop the second '.' and everything that comes after
2302 // it (i.e., ".text.XXX" becomes ".text").
2304 const char* s
= name
;
2308 const char* sdot
= strchr(s
, '.');
2312 const char* const data_rel_ro
= ".data.rel.ro";
2313 if (strncmp(name
, data_rel_ro
, strlen(data_rel_ro
)) == 0)
2315 *plen
= strlen(data_rel_ro
);
2319 *plen
= sdot
- name
;
2323 // Record the signature of a comdat section, and return whether to
2324 // include it in the link. If GROUP is true, this is a regular
2325 // section group. If GROUP is false, this is a group signature
2326 // derived from the name of a linkonce section. We want linkonce
2327 // signatures and group signatures to block each other, but we don't
2328 // want a linkonce signature to block another linkonce signature.
2331 Layout::add_comdat(const char* signature
, bool group
)
2333 std::string
sig(signature
);
2334 std::pair
<Signatures::iterator
, bool> ins(
2335 this->signatures_
.insert(std::make_pair(sig
, group
)));
2339 // This is the first time we've seen this signature.
2343 if (ins
.first
->second
)
2345 // We've already seen a real section group with this signature.
2350 // This is a real section group, and we've already seen a
2351 // linkonce section with this signature. Record that we've seen
2352 // a section group, and don't include this section group.
2353 ins
.first
->second
= true;
2358 // We've already seen a linkonce section and this is a linkonce
2359 // section. These don't block each other--this may be the same
2360 // symbol name with different section types.
2365 // Store the allocated sections into the section list.
2368 Layout::get_allocated_sections(Section_list
* section_list
) const
2370 for (Section_list::const_iterator p
= this->section_list_
.begin();
2371 p
!= this->section_list_
.end();
2373 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) != 0)
2374 section_list
->push_back(*p
);
2377 // Create an output segment.
2380 Layout::make_output_segment(elfcpp::Elf_Word type
, elfcpp::Elf_Word flags
)
2382 gold_assert(!parameters
->output_is_object());
2383 Output_segment
* oseg
= new Output_segment(type
, flags
);
2384 this->segment_list_
.push_back(oseg
);
2388 // Write out the Output_sections. Most won't have anything to write,
2389 // since most of the data will come from input sections which are
2390 // handled elsewhere. But some Output_sections do have Output_data.
2393 Layout::write_output_sections(Output_file
* of
) const
2395 for (Section_list::const_iterator p
= this->section_list_
.begin();
2396 p
!= this->section_list_
.end();
2399 if (!(*p
)->after_input_sections())
2404 // Write out data not associated with a section or the symbol table.
2407 Layout::write_data(const Symbol_table
* symtab
, Output_file
* of
) const
2409 if (!parameters
->strip_all())
2411 const Output_section
* symtab_section
= this->symtab_section_
;
2412 for (Section_list::const_iterator p
= this->section_list_
.begin();
2413 p
!= this->section_list_
.end();
2416 if ((*p
)->needs_symtab_index())
2418 gold_assert(symtab_section
!= NULL
);
2419 unsigned int index
= (*p
)->symtab_index();
2420 gold_assert(index
> 0 && index
!= -1U);
2421 off_t off
= (symtab_section
->offset()
2422 + index
* symtab_section
->entsize());
2423 symtab
->write_section_symbol(*p
, of
, off
);
2428 const Output_section
* dynsym_section
= this->dynsym_section_
;
2429 for (Section_list::const_iterator p
= this->section_list_
.begin();
2430 p
!= this->section_list_
.end();
2433 if ((*p
)->needs_dynsym_index())
2435 gold_assert(dynsym_section
!= NULL
);
2436 unsigned int index
= (*p
)->dynsym_index();
2437 gold_assert(index
> 0 && index
!= -1U);
2438 off_t off
= (dynsym_section
->offset()
2439 + index
* dynsym_section
->entsize());
2440 symtab
->write_section_symbol(*p
, of
, off
);
2444 // Write out the Output_data which are not in an Output_section.
2445 for (Data_list::const_iterator p
= this->special_output_list_
.begin();
2446 p
!= this->special_output_list_
.end();
2451 // Write out the Output_sections which can only be written after the
2452 // input sections are complete.
2455 Layout::write_sections_after_input_sections(Output_file
* of
)
2457 // Determine the final section offsets, and thus the final output
2458 // file size. Note we finalize the .shstrab last, to allow the
2459 // after_input_section sections to modify their section-names before
2461 if (this->any_postprocessing_sections_
)
2463 off_t off
= this->output_file_size_
;
2464 off
= this->set_section_offsets(off
, POSTPROCESSING_SECTIONS_PASS
);
2466 // Now that we've finalized the names, we can finalize the shstrab.
2468 this->set_section_offsets(off
,
2469 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
);
2471 if (off
> this->output_file_size_
)
2474 this->output_file_size_
= off
;
2478 for (Section_list::const_iterator p
= this->section_list_
.begin();
2479 p
!= this->section_list_
.end();
2482 if ((*p
)->after_input_sections())
2486 this->section_headers_
->write(of
);
2489 // Print statistical information to stderr. This is used for --stats.
2492 Layout::print_stats() const
2494 this->namepool_
.print_stats("section name pool");
2495 this->sympool_
.print_stats("output symbol name pool");
2496 this->dynpool_
.print_stats("dynamic name pool");
2498 for (Section_list::const_iterator p
= this->section_list_
.begin();
2499 p
!= this->section_list_
.end();
2501 (*p
)->print_merge_stats();
2504 // Write_sections_task methods.
2506 // We can always run this task.
2509 Write_sections_task::is_runnable()
2514 // We need to unlock both OUTPUT_SECTIONS_BLOCKER and FINAL_BLOCKER
2518 Write_sections_task::locks(Task_locker
* tl
)
2520 tl
->add(this, this->output_sections_blocker_
);
2521 tl
->add(this, this->final_blocker_
);
2524 // Run the task--write out the data.
2527 Write_sections_task::run(Workqueue
*)
2529 this->layout_
->write_output_sections(this->of_
);
2532 // Write_data_task methods.
2534 // We can always run this task.
2537 Write_data_task::is_runnable()
2542 // We need to unlock FINAL_BLOCKER when finished.
2545 Write_data_task::locks(Task_locker
* tl
)
2547 tl
->add(this, this->final_blocker_
);
2550 // Run the task--write out the data.
2553 Write_data_task::run(Workqueue
*)
2555 this->layout_
->write_data(this->symtab_
, this->of_
);
2558 // Write_symbols_task methods.
2560 // We can always run this task.
2563 Write_symbols_task::is_runnable()
2568 // We need to unlock FINAL_BLOCKER when finished.
2571 Write_symbols_task::locks(Task_locker
* tl
)
2573 tl
->add(this, this->final_blocker_
);
2576 // Run the task--write out the symbols.
2579 Write_symbols_task::run(Workqueue
*)
2581 this->symtab_
->write_globals(this->input_objects_
, this->sympool_
,
2582 this->dynpool_
, this->of_
);
2585 // Write_after_input_sections_task methods.
2587 // We can only run this task after the input sections have completed.
2590 Write_after_input_sections_task::is_runnable()
2592 if (this->input_sections_blocker_
->is_blocked())
2593 return this->input_sections_blocker_
;
2597 // We need to unlock FINAL_BLOCKER when finished.
2600 Write_after_input_sections_task::locks(Task_locker
* tl
)
2602 tl
->add(this, this->final_blocker_
);
2608 Write_after_input_sections_task::run(Workqueue
*)
2610 this->layout_
->write_sections_after_input_sections(this->of_
);
2613 // Close_task_runner methods.
2615 // Run the task--close the file.
2618 Close_task_runner::run(Workqueue
*, const Task
*)
2623 // Instantiate the templates we need. We could use the configure
2624 // script to restrict this to only the ones for implemented targets.
2626 #ifdef HAVE_TARGET_32_LITTLE
2629 Layout::layout
<32, false>(Sized_relobj
<32, false>* object
, unsigned int shndx
,
2631 const elfcpp::Shdr
<32, false>& shdr
,
2632 unsigned int, unsigned int, off_t
*);
2635 #ifdef HAVE_TARGET_32_BIG
2638 Layout::layout
<32, true>(Sized_relobj
<32, true>* object
, unsigned int shndx
,
2640 const elfcpp::Shdr
<32, true>& shdr
,
2641 unsigned int, unsigned int, off_t
*);
2644 #ifdef HAVE_TARGET_64_LITTLE
2647 Layout::layout
<64, false>(Sized_relobj
<64, false>* object
, unsigned int shndx
,
2649 const elfcpp::Shdr
<64, false>& shdr
,
2650 unsigned int, unsigned int, off_t
*);
2653 #ifdef HAVE_TARGET_64_BIG
2656 Layout::layout
<64, true>(Sized_relobj
<64, true>* object
, unsigned int shndx
,
2658 const elfcpp::Shdr
<64, true>& shdr
,
2659 unsigned int, unsigned int, off_t
*);
2662 #ifdef HAVE_TARGET_32_LITTLE
2665 Layout::layout_reloc
<32, false>(Sized_relobj
<32, false>* object
,
2666 unsigned int reloc_shndx
,
2667 const elfcpp::Shdr
<32, false>& shdr
,
2668 Output_section
* data_section
,
2669 Relocatable_relocs
* rr
);
2672 #ifdef HAVE_TARGET_32_BIG
2675 Layout::layout_reloc
<32, true>(Sized_relobj
<32, true>* object
,
2676 unsigned int reloc_shndx
,
2677 const elfcpp::Shdr
<32, true>& shdr
,
2678 Output_section
* data_section
,
2679 Relocatable_relocs
* rr
);
2682 #ifdef HAVE_TARGET_64_LITTLE
2685 Layout::layout_reloc
<64, false>(Sized_relobj
<64, false>* object
,
2686 unsigned int reloc_shndx
,
2687 const elfcpp::Shdr
<64, false>& shdr
,
2688 Output_section
* data_section
,
2689 Relocatable_relocs
* rr
);
2692 #ifdef HAVE_TARGET_64_BIG
2695 Layout::layout_reloc
<64, true>(Sized_relobj
<64, true>* object
,
2696 unsigned int reloc_shndx
,
2697 const elfcpp::Shdr
<64, true>& shdr
,
2698 Output_section
* data_section
,
2699 Relocatable_relocs
* rr
);
2702 #ifdef HAVE_TARGET_32_LITTLE
2705 Layout::layout_group
<32, false>(Symbol_table
* symtab
,
2706 Sized_relobj
<32, false>* object
,
2708 const char* group_section_name
,
2709 const char* signature
,
2710 const elfcpp::Shdr
<32, false>& shdr
,
2711 const elfcpp::Elf_Word
* contents
);
2714 #ifdef HAVE_TARGET_32_BIG
2717 Layout::layout_group
<32, true>(Symbol_table
* symtab
,
2718 Sized_relobj
<32, true>* object
,
2720 const char* group_section_name
,
2721 const char* signature
,
2722 const elfcpp::Shdr
<32, true>& shdr
,
2723 const elfcpp::Elf_Word
* contents
);
2726 #ifdef HAVE_TARGET_64_LITTLE
2729 Layout::layout_group
<64, false>(Symbol_table
* symtab
,
2730 Sized_relobj
<64, false>* object
,
2732 const char* group_section_name
,
2733 const char* signature
,
2734 const elfcpp::Shdr
<64, false>& shdr
,
2735 const elfcpp::Elf_Word
* contents
);
2738 #ifdef HAVE_TARGET_64_BIG
2741 Layout::layout_group
<64, true>(Symbol_table
* symtab
,
2742 Sized_relobj
<64, true>* object
,
2744 const char* group_section_name
,
2745 const char* signature
,
2746 const elfcpp::Shdr
<64, true>& shdr
,
2747 const elfcpp::Elf_Word
* contents
);
2750 #ifdef HAVE_TARGET_32_LITTLE
2753 Layout::layout_eh_frame
<32, false>(Sized_relobj
<32, false>* object
,
2754 const unsigned char* symbols
,
2756 const unsigned char* symbol_names
,
2757 off_t symbol_names_size
,
2759 const elfcpp::Shdr
<32, false>& shdr
,
2760 unsigned int reloc_shndx
,
2761 unsigned int reloc_type
,
2765 #ifdef HAVE_TARGET_32_BIG
2768 Layout::layout_eh_frame
<32, true>(Sized_relobj
<32, true>* object
,
2769 const unsigned char* symbols
,
2771 const unsigned char* symbol_names
,
2772 off_t symbol_names_size
,
2774 const elfcpp::Shdr
<32, true>& shdr
,
2775 unsigned int reloc_shndx
,
2776 unsigned int reloc_type
,
2780 #ifdef HAVE_TARGET_64_LITTLE
2783 Layout::layout_eh_frame
<64, false>(Sized_relobj
<64, false>* object
,
2784 const unsigned char* symbols
,
2786 const unsigned char* symbol_names
,
2787 off_t symbol_names_size
,
2789 const elfcpp::Shdr
<64, false>& shdr
,
2790 unsigned int reloc_shndx
,
2791 unsigned int reloc_type
,
2795 #ifdef HAVE_TARGET_64_BIG
2798 Layout::layout_eh_frame
<64, true>(Sized_relobj
<64, true>* object
,
2799 const unsigned char* symbols
,
2801 const unsigned char* symbol_names
,
2802 off_t symbol_names_size
,
2804 const elfcpp::Shdr
<64, true>& shdr
,
2805 unsigned int reloc_shndx
,
2806 unsigned int reloc_type
,
2810 } // End namespace gold.