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.
32 #include "libiberty.h"
36 #include "parameters.h"
39 #include "script-sections.h"
44 #include "compressed_output.h"
51 // Layout_task_runner methods.
53 // Lay out the sections. This is called after all the input objects
57 Layout_task_runner::run(Workqueue
* workqueue
, const Task
* task
)
59 off_t file_size
= this->layout_
->finalize(this->input_objects_
,
64 // Now we know the final size of the output file and we know where
65 // each piece of information goes.
66 Output_file
* of
= new Output_file(parameters
->options().output_file_name());
67 if (this->options_
.oformat_enum() != General_options::OBJECT_FORMAT_ELF
)
68 of
->set_is_temporary();
71 // Queue up the final set of tasks.
72 gold::queue_final_tasks(this->options_
, this->input_objects_
,
73 this->symtab_
, this->layout_
, workqueue
, of
);
78 Layout::Layout(const General_options
& options
, Script_options
* script_options
)
80 script_options_(script_options
),
88 unattached_section_list_(),
89 sections_are_attached_(false),
90 special_output_list_(),
91 section_headers_(NULL
),
93 symtab_section_(NULL
),
95 dynsym_section_(NULL
),
97 dynamic_section_(NULL
),
99 eh_frame_section_(NULL
),
100 eh_frame_data_(NULL
),
101 added_eh_frame_data_(false),
102 eh_frame_hdr_section_(NULL
),
103 build_id_note_(NULL
),
105 output_file_size_(-1),
106 input_requires_executable_stack_(false),
107 input_with_gnu_stack_note_(false),
108 input_without_gnu_stack_note_(false),
109 has_static_tls_(false),
110 any_postprocessing_sections_(false)
112 // Make space for more than enough segments for a typical file.
113 // This is just for efficiency--it's OK if we wind up needing more.
114 this->segment_list_
.reserve(12);
116 // We expect two unattached Output_data objects: the file header and
117 // the segment headers.
118 this->special_output_list_
.reserve(2);
121 // Hash a key we use to look up an output section mapping.
124 Layout::Hash_key::operator()(const Layout::Key
& k
) const
126 return k
.first
+ k
.second
.first
+ k
.second
.second
;
129 // Return whether PREFIX is a prefix of STR.
132 is_prefix_of(const char* prefix
, const char* str
)
134 return strncmp(prefix
, str
, strlen(prefix
)) == 0;
137 // Returns whether the given section is in the list of
138 // debug-sections-used-by-some-version-of-gdb. Currently,
139 // we've checked versions of gdb up to and including 6.7.1.
141 static const char* gdb_sections
[] =
143 // ".debug_aranges", // not used by gdb as of 6.7.1
149 // ".debug_pubnames", // not used by gdb as of 6.7.1
155 is_gdb_debug_section(const char* str
)
157 // We can do this faster: binary search or a hashtable. But why bother?
158 for (size_t i
= 0; i
< sizeof(gdb_sections
)/sizeof(*gdb_sections
); ++i
)
159 if (strcmp(str
, gdb_sections
[i
]) == 0)
164 // Whether to include this section in the link.
166 template<int size
, bool big_endian
>
168 Layout::include_section(Sized_relobj
<size
, big_endian
>*, const char* name
,
169 const elfcpp::Shdr
<size
, big_endian
>& shdr
)
171 switch (shdr
.get_sh_type())
173 case elfcpp::SHT_NULL
:
174 case elfcpp::SHT_SYMTAB
:
175 case elfcpp::SHT_DYNSYM
:
176 case elfcpp::SHT_STRTAB
:
177 case elfcpp::SHT_HASH
:
178 case elfcpp::SHT_DYNAMIC
:
179 case elfcpp::SHT_SYMTAB_SHNDX
:
182 case elfcpp::SHT_RELA
:
183 case elfcpp::SHT_REL
:
184 case elfcpp::SHT_GROUP
:
185 // If we are emitting relocations these should be handled
187 gold_assert(!parameters
->options().relocatable()
188 && !parameters
->options().emit_relocs());
191 case elfcpp::SHT_PROGBITS
:
192 if (parameters
->options().strip_debug()
193 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
195 // Debugging sections can only be recognized by name.
196 if (is_prefix_of(".debug", name
)
197 || is_prefix_of(".gnu.linkonce.wi.", name
)
198 || is_prefix_of(".line", name
)
199 || is_prefix_of(".stab", name
))
202 if (parameters
->options().strip_debug_gdb()
203 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
205 // Debugging sections can only be recognized by name.
206 if (is_prefix_of(".debug", name
)
207 && !is_gdb_debug_section(name
))
217 // Return an output section named NAME, or NULL if there is none.
220 Layout::find_output_section(const char* name
) const
222 for (Section_list::const_iterator p
= this->section_list_
.begin();
223 p
!= this->section_list_
.end();
225 if (strcmp((*p
)->name(), name
) == 0)
230 // Return an output segment of type TYPE, with segment flags SET set
231 // and segment flags CLEAR clear. Return NULL if there is none.
234 Layout::find_output_segment(elfcpp::PT type
, elfcpp::Elf_Word set
,
235 elfcpp::Elf_Word clear
) const
237 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
238 p
!= this->segment_list_
.end();
240 if (static_cast<elfcpp::PT
>((*p
)->type()) == type
241 && ((*p
)->flags() & set
) == set
242 && ((*p
)->flags() & clear
) == 0)
247 // Return the output section to use for section NAME with type TYPE
248 // and section flags FLAGS. NAME must be canonicalized in the string
249 // pool, and NAME_KEY is the key.
252 Layout::get_output_section(const char* name
, Stringpool::Key name_key
,
253 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
)
255 elfcpp::Elf_Xword lookup_flags
= flags
;
257 // Ignoring SHF_WRITE and SHF_EXECINSTR here means that we combine
258 // read-write with read-only sections. Some other ELF linkers do
259 // not do this. FIXME: Perhaps there should be an option
261 lookup_flags
&= ~(elfcpp::SHF_WRITE
| elfcpp::SHF_EXECINSTR
);
263 const Key
key(name_key
, std::make_pair(type
, lookup_flags
));
264 const std::pair
<Key
, Output_section
*> v(key
, NULL
);
265 std::pair
<Section_name_map::iterator
, bool> ins(
266 this->section_name_map_
.insert(v
));
269 return ins
.first
->second
;
272 // This is the first time we've seen this name/type/flags
273 // combination. For compatibility with the GNU linker, we
274 // combine sections with contents and zero flags with sections
275 // with non-zero flags. This is a workaround for cases where
276 // assembler code forgets to set section flags. FIXME: Perhaps
277 // there should be an option to control this.
278 Output_section
* os
= NULL
;
280 if (type
== elfcpp::SHT_PROGBITS
)
284 Output_section
* same_name
= this->find_output_section(name
);
285 if (same_name
!= NULL
286 && same_name
->type() == elfcpp::SHT_PROGBITS
287 && (same_name
->flags() & elfcpp::SHF_TLS
) == 0)
290 else if ((flags
& elfcpp::SHF_TLS
) == 0)
292 elfcpp::Elf_Xword zero_flags
= 0;
293 const Key
zero_key(name_key
, std::make_pair(type
, zero_flags
));
294 Section_name_map::iterator p
=
295 this->section_name_map_
.find(zero_key
);
296 if (p
!= this->section_name_map_
.end())
302 os
= this->make_output_section(name
, type
, flags
);
303 ins
.first
->second
= os
;
308 // Pick the output section to use for section NAME, in input file
309 // RELOBJ, with type TYPE and flags FLAGS. RELOBJ may be NULL for a
310 // linker created section. IS_INPUT_SECTION is true if we are
311 // choosing an output section for an input section found in a input
312 // file. This will return NULL if the input section should be
316 Layout::choose_output_section(const Relobj
* relobj
, const char* name
,
317 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
,
318 bool is_input_section
)
320 // We should not see any input sections after we have attached
321 // sections to segments.
322 gold_assert(!is_input_section
|| !this->sections_are_attached_
);
324 // Some flags in the input section should not be automatically
325 // copied to the output section.
326 flags
&= ~ (elfcpp::SHF_INFO_LINK
327 | elfcpp::SHF_LINK_ORDER
330 | elfcpp::SHF_STRINGS
);
332 if (this->script_options_
->saw_sections_clause())
334 // We are using a SECTIONS clause, so the output section is
335 // chosen based only on the name.
337 Script_sections
* ss
= this->script_options_
->script_sections();
338 const char* file_name
= relobj
== NULL
? NULL
: relobj
->name().c_str();
339 Output_section
** output_section_slot
;
340 name
= ss
->output_section_name(file_name
, name
, &output_section_slot
);
343 // The SECTIONS clause says to discard this input section.
347 // If this is an orphan section--one not mentioned in the linker
348 // script--then OUTPUT_SECTION_SLOT will be NULL, and we do the
349 // default processing below.
351 if (output_section_slot
!= NULL
)
353 if (*output_section_slot
!= NULL
)
354 return *output_section_slot
;
356 // We don't put sections found in the linker script into
357 // SECTION_NAME_MAP_. That keeps us from getting confused
358 // if an orphan section is mapped to a section with the same
359 // name as one in the linker script.
361 name
= this->namepool_
.add(name
, false, NULL
);
363 Output_section
* os
= this->make_output_section(name
, type
, flags
);
364 os
->set_found_in_sections_clause();
365 *output_section_slot
= os
;
370 // FIXME: Handle SHF_OS_NONCONFORMING somewhere.
372 // Turn NAME from the name of the input section into the name of the
375 size_t len
= strlen(name
);
376 if (is_input_section
&& !parameters
->options().relocatable())
377 name
= Layout::output_section_name(name
, &len
);
379 Stringpool::Key name_key
;
380 name
= this->namepool_
.add_with_length(name
, len
, true, &name_key
);
382 // Find or make the output section. The output section is selected
383 // based on the section name, type, and flags.
384 return this->get_output_section(name
, name_key
, type
, flags
);
387 // Return the output section to use for input section SHNDX, with name
388 // NAME, with header HEADER, from object OBJECT. RELOC_SHNDX is the
389 // index of a relocation section which applies to this section, or 0
390 // if none, or -1U if more than one. RELOC_TYPE is the type of the
391 // relocation section if there is one. Set *OFF to the offset of this
392 // input section without the output section. Return NULL if the
393 // section should be discarded. Set *OFF to -1 if the section
394 // contents should not be written directly to the output file, but
395 // will instead receive special handling.
397 template<int size
, bool big_endian
>
399 Layout::layout(Sized_relobj
<size
, big_endian
>* object
, unsigned int shndx
,
400 const char* name
, const elfcpp::Shdr
<size
, big_endian
>& shdr
,
401 unsigned int reloc_shndx
, unsigned int, off_t
* off
)
403 if (!this->include_section(object
, name
, shdr
))
408 // In a relocatable link a grouped section must not be combined with
409 // any other sections.
410 if (parameters
->options().relocatable()
411 && (shdr
.get_sh_flags() & elfcpp::SHF_GROUP
) != 0)
413 name
= this->namepool_
.add(name
, true, NULL
);
414 os
= this->make_output_section(name
, shdr
.get_sh_type(),
415 shdr
.get_sh_flags());
419 os
= this->choose_output_section(object
, name
, shdr
.get_sh_type(),
420 shdr
.get_sh_flags(), true);
425 // By default the GNU linker sorts input sections whose names match
426 // .ctor.*, .dtor.*, .init_array.*, or .fini_array.*. The sections
427 // are sorted by name. This is used to implement constructor
428 // priority ordering. We are compatible.
429 if (!this->script_options_
->saw_sections_clause()
430 && (is_prefix_of(".ctors.", name
)
431 || is_prefix_of(".dtors.", name
)
432 || is_prefix_of(".init_array.", name
)
433 || is_prefix_of(".fini_array.", name
)))
434 os
->set_must_sort_attached_input_sections();
436 // FIXME: Handle SHF_LINK_ORDER somewhere.
438 *off
= os
->add_input_section(object
, shndx
, name
, shdr
, reloc_shndx
,
439 this->script_options_
->saw_sections_clause());
444 // Handle a relocation section when doing a relocatable link.
446 template<int size
, bool big_endian
>
448 Layout::layout_reloc(Sized_relobj
<size
, big_endian
>* object
,
450 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
451 Output_section
* data_section
,
452 Relocatable_relocs
* rr
)
454 gold_assert(parameters
->options().relocatable()
455 || parameters
->options().emit_relocs());
457 int sh_type
= shdr
.get_sh_type();
460 if (sh_type
== elfcpp::SHT_REL
)
462 else if (sh_type
== elfcpp::SHT_RELA
)
466 name
+= data_section
->name();
468 Output_section
* os
= this->choose_output_section(object
, name
.c_str(),
473 os
->set_should_link_to_symtab();
474 os
->set_info_section(data_section
);
476 Output_section_data
* posd
;
477 if (sh_type
== elfcpp::SHT_REL
)
479 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rel_size
);
480 posd
= new Output_relocatable_relocs
<elfcpp::SHT_REL
,
484 else if (sh_type
== elfcpp::SHT_RELA
)
486 os
->set_entsize(elfcpp::Elf_sizes
<size
>::rela_size
);
487 posd
= new Output_relocatable_relocs
<elfcpp::SHT_RELA
,
494 os
->add_output_section_data(posd
);
495 rr
->set_output_data(posd
);
500 // Handle a group section when doing a relocatable link.
502 template<int size
, bool big_endian
>
504 Layout::layout_group(Symbol_table
* symtab
,
505 Sized_relobj
<size
, big_endian
>* object
,
507 const char* group_section_name
,
508 const char* signature
,
509 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
510 const elfcpp::Elf_Word
* contents
)
512 gold_assert(parameters
->options().relocatable());
513 gold_assert(shdr
.get_sh_type() == elfcpp::SHT_GROUP
);
514 group_section_name
= this->namepool_
.add(group_section_name
, true, NULL
);
515 Output_section
* os
= this->make_output_section(group_section_name
,
517 shdr
.get_sh_flags());
519 // We need to find a symbol with the signature in the symbol table.
520 // If we don't find one now, we need to look again later.
521 Symbol
* sym
= symtab
->lookup(signature
, NULL
);
523 os
->set_info_symndx(sym
);
526 // We will wind up using a symbol whose name is the signature.
527 // So just put the signature in the symbol name pool to save it.
528 signature
= symtab
->canonicalize_name(signature
);
529 this->group_signatures_
.push_back(Group_signature(os
, signature
));
532 os
->set_should_link_to_symtab();
535 section_size_type entry_count
=
536 convert_to_section_size_type(shdr
.get_sh_size() / 4);
537 Output_section_data
* posd
=
538 new Output_data_group
<size
, big_endian
>(object
, entry_count
, contents
);
539 os
->add_output_section_data(posd
);
542 // Special GNU handling of sections name .eh_frame. They will
543 // normally hold exception frame data as defined by the C++ ABI
544 // (http://codesourcery.com/cxx-abi/).
546 template<int size
, bool big_endian
>
548 Layout::layout_eh_frame(Sized_relobj
<size
, big_endian
>* object
,
549 const unsigned char* symbols
,
551 const unsigned char* symbol_names
,
552 off_t symbol_names_size
,
554 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
555 unsigned int reloc_shndx
, unsigned int reloc_type
,
558 gold_assert(shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
);
559 gold_assert((shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) != 0);
561 const char* const name
= ".eh_frame";
562 Output_section
* os
= this->choose_output_section(object
,
564 elfcpp::SHT_PROGBITS
,
570 if (this->eh_frame_section_
== NULL
)
572 this->eh_frame_section_
= os
;
573 this->eh_frame_data_
= new Eh_frame();
575 if (this->options_
.eh_frame_hdr())
577 Output_section
* hdr_os
=
578 this->choose_output_section(NULL
,
580 elfcpp::SHT_PROGBITS
,
586 Eh_frame_hdr
* hdr_posd
= new Eh_frame_hdr(os
,
587 this->eh_frame_data_
);
588 hdr_os
->add_output_section_data(hdr_posd
);
590 hdr_os
->set_after_input_sections();
592 if (!this->script_options_
->saw_phdrs_clause())
594 Output_segment
* hdr_oseg
;
595 hdr_oseg
= this->make_output_segment(elfcpp::PT_GNU_EH_FRAME
,
597 hdr_oseg
->add_output_section(hdr_os
, elfcpp::PF_R
);
600 this->eh_frame_data_
->set_eh_frame_hdr(hdr_posd
);
605 gold_assert(this->eh_frame_section_
== os
);
607 if (this->eh_frame_data_
->add_ehframe_input_section(object
,
616 os
->update_flags_for_input_section(shdr
.get_sh_flags());
618 // We found a .eh_frame section we are going to optimize, so now
619 // we can add the set of optimized sections to the output
620 // section. We need to postpone adding this until we've found a
621 // section we can optimize so that the .eh_frame section in
622 // crtbegin.o winds up at the start of the output section.
623 if (!this->added_eh_frame_data_
)
625 os
->add_output_section_data(this->eh_frame_data_
);
626 this->added_eh_frame_data_
= true;
632 // We couldn't handle this .eh_frame section for some reason.
633 // Add it as a normal section.
634 bool saw_sections_clause
= this->script_options_
->saw_sections_clause();
635 *off
= os
->add_input_section(object
, shndx
, name
, shdr
, reloc_shndx
,
636 saw_sections_clause
);
642 // Add POSD to an output section using NAME, TYPE, and FLAGS.
645 Layout::add_output_section_data(const char* name
, elfcpp::Elf_Word type
,
646 elfcpp::Elf_Xword flags
,
647 Output_section_data
* posd
)
649 Output_section
* os
= this->choose_output_section(NULL
, name
, type
, flags
,
652 os
->add_output_section_data(posd
);
655 // Map section flags to segment flags.
658 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags
)
660 elfcpp::Elf_Word ret
= elfcpp::PF_R
;
661 if ((flags
& elfcpp::SHF_WRITE
) != 0)
663 if ((flags
& elfcpp::SHF_EXECINSTR
) != 0)
668 // Sometimes we compress sections. This is typically done for
669 // sections that are not part of normal program execution (such as
670 // .debug_* sections), and where the readers of these sections know
671 // how to deal with compressed sections. (To make it easier for them,
672 // we will rename the ouput section in such cases from .foo to
673 // .foo.zlib.nnnn, where nnnn is the uncompressed size.) This routine
674 // doesn't say for certain whether we'll compress -- it depends on
675 // commandline options as well -- just whether this section is a
676 // candidate for compression.
679 is_compressible_debug_section(const char* secname
)
681 return (strncmp(secname
, ".debug", sizeof(".debug") - 1) == 0);
684 // Make a new Output_section, and attach it to segments as
688 Layout::make_output_section(const char* name
, elfcpp::Elf_Word type
,
689 elfcpp::Elf_Xword flags
)
692 if ((flags
& elfcpp::SHF_ALLOC
) == 0
693 && strcmp(this->options_
.compress_debug_sections(), "none") != 0
694 && is_compressible_debug_section(name
))
695 os
= new Output_compressed_section(&this->options_
, name
, type
, flags
);
697 os
= new Output_section(name
, type
, flags
);
699 this->section_list_
.push_back(os
);
701 // The GNU linker by default sorts some sections by priority, so we
702 // do the same. We need to know that this might happen before we
703 // attach any input sections.
704 if (!this->script_options_
->saw_sections_clause()
705 && (strcmp(name
, ".ctors") == 0
706 || strcmp(name
, ".dtors") == 0
707 || strcmp(name
, ".init_array") == 0
708 || strcmp(name
, ".fini_array") == 0))
709 os
->set_may_sort_attached_input_sections();
711 // If we have already attached the sections to segments, then we
712 // need to attach this one now. This happens for sections created
713 // directly by the linker.
714 if (this->sections_are_attached_
)
715 this->attach_section_to_segment(os
);
720 // Attach output sections to segments. This is called after we have
721 // seen all the input sections.
724 Layout::attach_sections_to_segments()
726 for (Section_list::iterator p
= this->section_list_
.begin();
727 p
!= this->section_list_
.end();
729 this->attach_section_to_segment(*p
);
731 this->sections_are_attached_
= true;
734 // Attach an output section to a segment.
737 Layout::attach_section_to_segment(Output_section
* os
)
739 if ((os
->flags() & elfcpp::SHF_ALLOC
) == 0)
740 this->unattached_section_list_
.push_back(os
);
742 this->attach_allocated_section_to_segment(os
);
745 // Attach an allocated output section to a segment.
748 Layout::attach_allocated_section_to_segment(Output_section
* os
)
750 elfcpp::Elf_Xword flags
= os
->flags();
751 gold_assert((flags
& elfcpp::SHF_ALLOC
) != 0);
753 if (parameters
->options().relocatable())
756 // If we have a SECTIONS clause, we can't handle the attachment to
757 // segments until after we've seen all the sections.
758 if (this->script_options_
->saw_sections_clause())
761 gold_assert(!this->script_options_
->saw_phdrs_clause());
763 // This output section goes into a PT_LOAD segment.
765 elfcpp::Elf_Word seg_flags
= Layout::section_flags_to_segment(flags
);
767 // In general the only thing we really care about for PT_LOAD
768 // segments is whether or not they are writable, so that is how we
769 // search for them. People who need segments sorted on some other
770 // basis will have to use a linker script.
772 Segment_list::const_iterator p
;
773 for (p
= this->segment_list_
.begin();
774 p
!= this->segment_list_
.end();
777 if ((*p
)->type() == elfcpp::PT_LOAD
778 && ((*p
)->flags() & elfcpp::PF_W
) == (seg_flags
& elfcpp::PF_W
))
780 // If -Tbss was specified, we need to separate the data
782 if (this->options_
.user_set_Tbss())
784 if ((os
->type() == elfcpp::SHT_NOBITS
)
785 == (*p
)->has_any_data_sections())
789 (*p
)->add_output_section(os
, seg_flags
);
794 if (p
== this->segment_list_
.end())
796 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_LOAD
,
798 oseg
->add_output_section(os
, seg_flags
);
801 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
803 if (os
->type() == elfcpp::SHT_NOTE
)
805 // See if we already have an equivalent PT_NOTE segment.
806 for (p
= this->segment_list_
.begin();
807 p
!= segment_list_
.end();
810 if ((*p
)->type() == elfcpp::PT_NOTE
811 && (((*p
)->flags() & elfcpp::PF_W
)
812 == (seg_flags
& elfcpp::PF_W
)))
814 (*p
)->add_output_section(os
, seg_flags
);
819 if (p
== this->segment_list_
.end())
821 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_NOTE
,
823 oseg
->add_output_section(os
, seg_flags
);
827 // If we see a loadable SHF_TLS section, we create a PT_TLS
828 // segment. There can only be one such segment.
829 if ((flags
& elfcpp::SHF_TLS
) != 0)
831 if (this->tls_segment_
== NULL
)
832 this->tls_segment_
= this->make_output_segment(elfcpp::PT_TLS
,
834 this->tls_segment_
->add_output_section(os
, seg_flags
);
838 // Make an output section for a script.
841 Layout::make_output_section_for_script(const char* name
)
843 name
= this->namepool_
.add(name
, false, NULL
);
844 Output_section
* os
= this->make_output_section(name
, elfcpp::SHT_PROGBITS
,
846 os
->set_found_in_sections_clause();
850 // Return the number of segments we expect to see.
853 Layout::expected_segment_count() const
855 size_t ret
= this->segment_list_
.size();
857 // If we didn't see a SECTIONS clause in a linker script, we should
858 // already have the complete list of segments. Otherwise we ask the
859 // SECTIONS clause how many segments it expects, and add in the ones
860 // we already have (PT_GNU_STACK, PT_GNU_EH_FRAME, etc.)
862 if (!this->script_options_
->saw_sections_clause())
866 const Script_sections
* ss
= this->script_options_
->script_sections();
867 return ret
+ ss
->expected_segment_count(this);
871 // Handle the .note.GNU-stack section at layout time. SEEN_GNU_STACK
872 // is whether we saw a .note.GNU-stack section in the object file.
873 // GNU_STACK_FLAGS is the section flags. The flags give the
874 // protection required for stack memory. We record this in an
875 // executable as a PT_GNU_STACK segment. If an object file does not
876 // have a .note.GNU-stack segment, we must assume that it is an old
877 // object. On some targets that will force an executable stack.
880 Layout::layout_gnu_stack(bool seen_gnu_stack
, uint64_t gnu_stack_flags
)
883 this->input_without_gnu_stack_note_
= true;
886 this->input_with_gnu_stack_note_
= true;
887 if ((gnu_stack_flags
& elfcpp::SHF_EXECINSTR
) != 0)
888 this->input_requires_executable_stack_
= true;
892 // Create the dynamic sections which are needed before we read the
896 Layout::create_initial_dynamic_sections(Symbol_table
* symtab
)
898 if (parameters
->doing_static_link())
901 this->dynamic_section_
= this->choose_output_section(NULL
, ".dynamic",
904 | elfcpp::SHF_WRITE
),
907 symtab
->define_in_output_data("_DYNAMIC", NULL
, this->dynamic_section_
, 0, 0,
908 elfcpp::STT_OBJECT
, elfcpp::STB_LOCAL
,
909 elfcpp::STV_HIDDEN
, 0, false, false);
911 this->dynamic_data_
= new Output_data_dynamic(&this->dynpool_
);
913 this->dynamic_section_
->add_output_section_data(this->dynamic_data_
);
916 // For each output section whose name can be represented as C symbol,
917 // define __start and __stop symbols for the section. This is a GNU
921 Layout::define_section_symbols(Symbol_table
* symtab
)
923 for (Section_list::const_iterator p
= this->section_list_
.begin();
924 p
!= this->section_list_
.end();
927 const char* const name
= (*p
)->name();
928 if (name
[strspn(name
,
930 "ABCDEFGHIJKLMNOPWRSTUVWXYZ"
931 "abcdefghijklmnopqrstuvwxyz"
935 const std::string
name_string(name
);
936 const std::string
start_name("__start_" + name_string
);
937 const std::string
stop_name("__stop_" + name_string
);
939 symtab
->define_in_output_data(start_name
.c_str(),
948 false, // offset_is_from_end
949 true); // only_if_ref
951 symtab
->define_in_output_data(stop_name
.c_str(),
960 true, // offset_is_from_end
961 true); // only_if_ref
966 // Define symbols for group signatures.
969 Layout::define_group_signatures(Symbol_table
* symtab
)
971 for (Group_signatures::iterator p
= this->group_signatures_
.begin();
972 p
!= this->group_signatures_
.end();
975 Symbol
* sym
= symtab
->lookup(p
->signature
, NULL
);
977 p
->section
->set_info_symndx(sym
);
980 // Force the name of the group section to the group
981 // signature, and use the group's section symbol as the
983 if (strcmp(p
->section
->name(), p
->signature
) != 0)
985 const char* name
= this->namepool_
.add(p
->signature
,
987 p
->section
->set_name(name
);
989 p
->section
->set_needs_symtab_index();
990 p
->section
->set_info_section_symndx(p
->section
);
994 this->group_signatures_
.clear();
997 // Find the first read-only PT_LOAD segment, creating one if
1001 Layout::find_first_load_seg()
1003 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
1004 p
!= this->segment_list_
.end();
1007 if ((*p
)->type() == elfcpp::PT_LOAD
1008 && ((*p
)->flags() & elfcpp::PF_R
) != 0
1009 && ((*p
)->flags() & elfcpp::PF_W
) == 0)
1013 gold_assert(!this->script_options_
->saw_phdrs_clause());
1015 Output_segment
* load_seg
= this->make_output_segment(elfcpp::PT_LOAD
,
1020 // Finalize the layout. When this is called, we have created all the
1021 // output sections and all the output segments which are based on
1022 // input sections. We have several things to do, and we have to do
1023 // them in the right order, so that we get the right results correctly
1026 // 1) Finalize the list of output segments and create the segment
1029 // 2) Finalize the dynamic symbol table and associated sections.
1031 // 3) Determine the final file offset of all the output segments.
1033 // 4) Determine the final file offset of all the SHF_ALLOC output
1036 // 5) Create the symbol table sections and the section name table
1039 // 6) Finalize the symbol table: set symbol values to their final
1040 // value and make a final determination of which symbols are going
1041 // into the output symbol table.
1043 // 7) Create the section table header.
1045 // 8) Determine the final file offset of all the output sections which
1046 // are not SHF_ALLOC, including the section table header.
1048 // 9) Finalize the ELF file header.
1050 // This function returns the size of the output file.
1053 Layout::finalize(const Input_objects
* input_objects
, Symbol_table
* symtab
,
1054 Target
* target
, const Task
* task
)
1056 target
->finalize_sections(this);
1058 this->count_local_symbols(task
, input_objects
);
1060 this->create_gold_note();
1061 this->create_executable_stack_info(target
);
1062 this->create_build_id();
1064 Output_segment
* phdr_seg
= NULL
;
1065 if (!parameters
->options().relocatable() && !parameters
->doing_static_link())
1067 // There was a dynamic object in the link. We need to create
1068 // some information for the dynamic linker.
1070 // Create the PT_PHDR segment which will hold the program
1072 if (!this->script_options_
->saw_phdrs_clause())
1073 phdr_seg
= this->make_output_segment(elfcpp::PT_PHDR
, elfcpp::PF_R
);
1075 // Create the dynamic symbol table, including the hash table.
1076 Output_section
* dynstr
;
1077 std::vector
<Symbol
*> dynamic_symbols
;
1078 unsigned int local_dynamic_count
;
1079 Versions
versions(*this->script_options()->version_script_info(),
1081 this->create_dynamic_symtab(input_objects
, symtab
, &dynstr
,
1082 &local_dynamic_count
, &dynamic_symbols
,
1085 // Create the .interp section to hold the name of the
1086 // interpreter, and put it in a PT_INTERP segment.
1087 if (!parameters
->options().shared())
1088 this->create_interp(target
);
1090 // Finish the .dynamic section to hold the dynamic data, and put
1091 // it in a PT_DYNAMIC segment.
1092 this->finish_dynamic_section(input_objects
, symtab
);
1094 // We should have added everything we need to the dynamic string
1096 this->dynpool_
.set_string_offsets();
1098 // Create the version sections. We can't do this until the
1099 // dynamic string table is complete.
1100 this->create_version_sections(&versions
, symtab
, local_dynamic_count
,
1101 dynamic_symbols
, dynstr
);
1104 // If there is a SECTIONS clause, put all the input sections into
1105 // the required order.
1106 Output_segment
* load_seg
;
1107 if (this->script_options_
->saw_sections_clause())
1108 load_seg
= this->set_section_addresses_from_script(symtab
);
1109 else if (parameters
->options().relocatable())
1112 load_seg
= this->find_first_load_seg();
1114 if (this->options_
.oformat_enum() != General_options::OBJECT_FORMAT_ELF
)
1117 gold_assert(phdr_seg
== NULL
|| load_seg
!= NULL
);
1119 // Lay out the segment headers.
1120 Output_segment_headers
* segment_headers
;
1121 if (parameters
->options().relocatable())
1122 segment_headers
= NULL
;
1125 segment_headers
= new Output_segment_headers(this->segment_list_
);
1126 if (load_seg
!= NULL
)
1127 load_seg
->add_initial_output_data(segment_headers
);
1128 if (phdr_seg
!= NULL
)
1129 phdr_seg
->add_initial_output_data(segment_headers
);
1132 // Lay out the file header.
1133 Output_file_header
* file_header
;
1134 file_header
= new Output_file_header(target
, symtab
, segment_headers
,
1135 this->options_
.entry());
1136 if (load_seg
!= NULL
)
1137 load_seg
->add_initial_output_data(file_header
);
1139 this->special_output_list_
.push_back(file_header
);
1140 if (segment_headers
!= NULL
)
1141 this->special_output_list_
.push_back(segment_headers
);
1143 if (this->script_options_
->saw_phdrs_clause()
1144 && !parameters
->options().relocatable())
1146 // Support use of FILEHDRS and PHDRS attachments in a PHDRS
1147 // clause in a linker script.
1148 Script_sections
* ss
= this->script_options_
->script_sections();
1149 ss
->put_headers_in_phdrs(file_header
, segment_headers
);
1152 // We set the output section indexes in set_segment_offsets and
1153 // set_section_indexes.
1154 unsigned int shndx
= 1;
1156 // Set the file offsets of all the segments, and all the sections
1159 if (!parameters
->options().relocatable())
1160 off
= this->set_segment_offsets(target
, load_seg
, &shndx
);
1162 off
= this->set_relocatable_section_offsets(file_header
, &shndx
);
1164 // Set the file offsets of all the non-data sections we've seen so
1165 // far which don't have to wait for the input sections. We need
1166 // this in order to finalize local symbols in non-allocated
1168 off
= this->set_section_offsets(off
, BEFORE_INPUT_SECTIONS_PASS
);
1170 // Set the section indexes of all unallocated sections seen so far,
1171 // in case any of them are somehow referenced by a symbol.
1172 shndx
= this->set_section_indexes(shndx
);
1174 // Create the symbol table sections.
1175 this->create_symtab_sections(input_objects
, symtab
, shndx
, &off
);
1176 if (!parameters
->doing_static_link())
1177 this->assign_local_dynsym_offsets(input_objects
);
1179 // Process any symbol assignments from a linker script. This must
1180 // be called after the symbol table has been finalized.
1181 this->script_options_
->finalize_symbols(symtab
, this);
1183 // Create the .shstrtab section.
1184 Output_section
* shstrtab_section
= this->create_shstrtab();
1186 // Set the file offsets of the rest of the non-data sections which
1187 // don't have to wait for the input sections.
1188 off
= this->set_section_offsets(off
, BEFORE_INPUT_SECTIONS_PASS
);
1190 // Now that all sections have been created, set the section indexes
1191 // for any sections which haven't been done yet.
1192 shndx
= this->set_section_indexes(shndx
);
1194 // Create the section table header.
1195 this->create_shdrs(shstrtab_section
, &off
);
1197 // If there are no sections which require postprocessing, we can
1198 // handle the section names now, and avoid a resize later.
1199 if (!this->any_postprocessing_sections_
)
1200 off
= this->set_section_offsets(off
,
1201 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
);
1203 file_header
->set_section_info(this->section_headers_
, shstrtab_section
);
1205 // Now we know exactly where everything goes in the output file
1206 // (except for non-allocated sections which require postprocessing).
1207 Output_data::layout_complete();
1209 this->output_file_size_
= off
;
1214 // Create a note header following the format defined in the ELF ABI.
1215 // NAME is the name, NOTE_TYPE is the type, DESCSZ is the size of the
1216 // descriptor. ALLOCATE is true if the section should be allocated in
1217 // memory. This returns the new note section. It sets
1218 // *TRAILING_PADDING to the number of trailing zero bytes required.
1221 Layout::create_note(const char* name
, int note_type
, size_t descsz
,
1222 bool allocate
, size_t* trailing_padding
)
1224 // Authorities all agree that the values in a .note field should
1225 // be aligned on 4-byte boundaries for 32-bit binaries. However,
1226 // they differ on what the alignment is for 64-bit binaries.
1227 // The GABI says unambiguously they take 8-byte alignment:
1228 // http://sco.com/developers/gabi/latest/ch5.pheader.html#note_section
1229 // Other documentation says alignment should always be 4 bytes:
1230 // http://www.netbsd.org/docs/kernel/elf-notes.html#note-format
1231 // GNU ld and GNU readelf both support the latter (at least as of
1232 // version 2.16.91), and glibc always generates the latter for
1233 // .note.ABI-tag (as of version 1.6), so that's the one we go with
1235 #ifdef GABI_FORMAT_FOR_DOTNOTE_SECTION // This is not defined by default.
1236 const int size
= parameters
->target().get_size();
1238 const int size
= 32;
1241 // The contents of the .note section.
1242 size_t namesz
= strlen(name
) + 1;
1243 size_t aligned_namesz
= align_address(namesz
, size
/ 8);
1244 size_t aligned_descsz
= align_address(descsz
, size
/ 8);
1246 size_t notehdrsz
= 3 * (size
/ 8) + aligned_namesz
;
1248 unsigned char* buffer
= new unsigned char[notehdrsz
];
1249 memset(buffer
, 0, notehdrsz
);
1251 bool is_big_endian
= parameters
->target().is_big_endian();
1257 elfcpp::Swap
<32, false>::writeval(buffer
, namesz
);
1258 elfcpp::Swap
<32, false>::writeval(buffer
+ 4, descsz
);
1259 elfcpp::Swap
<32, false>::writeval(buffer
+ 8, note_type
);
1263 elfcpp::Swap
<32, true>::writeval(buffer
, namesz
);
1264 elfcpp::Swap
<32, true>::writeval(buffer
+ 4, descsz
);
1265 elfcpp::Swap
<32, true>::writeval(buffer
+ 8, note_type
);
1268 else if (size
== 64)
1272 elfcpp::Swap
<64, false>::writeval(buffer
, namesz
);
1273 elfcpp::Swap
<64, false>::writeval(buffer
+ 8, descsz
);
1274 elfcpp::Swap
<64, false>::writeval(buffer
+ 16, note_type
);
1278 elfcpp::Swap
<64, true>::writeval(buffer
, namesz
);
1279 elfcpp::Swap
<64, true>::writeval(buffer
+ 8, descsz
);
1280 elfcpp::Swap
<64, true>::writeval(buffer
+ 16, note_type
);
1286 memcpy(buffer
+ 3 * (size
/ 8), name
, namesz
);
1288 const char* note_name
= this->namepool_
.add(".note", false, NULL
);
1289 elfcpp::Elf_Xword flags
= 0;
1291 flags
= elfcpp::SHF_ALLOC
;
1292 Output_section
* os
= this->make_output_section(note_name
,
1295 Output_section_data
* posd
= new Output_data_const_buffer(buffer
, notehdrsz
,
1297 os
->add_output_section_data(posd
);
1299 *trailing_padding
= aligned_descsz
- descsz
;
1304 // For an executable or shared library, create a note to record the
1305 // version of gold used to create the binary.
1308 Layout::create_gold_note()
1310 if (parameters
->options().relocatable())
1313 std::string desc
= std::string("gold ") + gold::get_version_string();
1315 size_t trailing_padding
;
1316 Output_section
*os
= this->create_note("GNU", elfcpp::NT_GNU_GOLD_VERSION
,
1317 desc
.size(), false, &trailing_padding
);
1319 Output_section_data
* posd
= new Output_data_const(desc
, 4);
1320 os
->add_output_section_data(posd
);
1322 if (trailing_padding
> 0)
1324 posd
= new Output_data_fixed_space(trailing_padding
, 0);
1325 os
->add_output_section_data(posd
);
1329 // Record whether the stack should be executable. This can be set
1330 // from the command line using the -z execstack or -z noexecstack
1331 // options. Otherwise, if any input file has a .note.GNU-stack
1332 // section with the SHF_EXECINSTR flag set, the stack should be
1333 // executable. Otherwise, if at least one input file a
1334 // .note.GNU-stack section, and some input file has no .note.GNU-stack
1335 // section, we use the target default for whether the stack should be
1336 // executable. Otherwise, we don't generate a stack note. When
1337 // generating a object file, we create a .note.GNU-stack section with
1338 // the appropriate marking. When generating an executable or shared
1339 // library, we create a PT_GNU_STACK segment.
1342 Layout::create_executable_stack_info(const Target
* target
)
1344 bool is_stack_executable
;
1345 if (this->options_
.is_execstack_set())
1346 is_stack_executable
= this->options_
.is_stack_executable();
1347 else if (!this->input_with_gnu_stack_note_
)
1351 if (this->input_requires_executable_stack_
)
1352 is_stack_executable
= true;
1353 else if (this->input_without_gnu_stack_note_
)
1354 is_stack_executable
= target
->is_default_stack_executable();
1356 is_stack_executable
= false;
1359 if (parameters
->options().relocatable())
1361 const char* name
= this->namepool_
.add(".note.GNU-stack", false, NULL
);
1362 elfcpp::Elf_Xword flags
= 0;
1363 if (is_stack_executable
)
1364 flags
|= elfcpp::SHF_EXECINSTR
;
1365 this->make_output_section(name
, elfcpp::SHT_PROGBITS
, flags
);
1369 if (this->script_options_
->saw_phdrs_clause())
1371 int flags
= elfcpp::PF_R
| elfcpp::PF_W
;
1372 if (is_stack_executable
)
1373 flags
|= elfcpp::PF_X
;
1374 this->make_output_segment(elfcpp::PT_GNU_STACK
, flags
);
1378 // If --build-id was used, set up the build ID note.
1381 Layout::create_build_id()
1383 if (!parameters
->options().user_set_build_id())
1386 const char* style
= parameters
->options().build_id();
1387 if (strcmp(style
, "none") == 0)
1390 // Set DESCSZ to the size of the note descriptor. When possible,
1391 // set DESC to the note descriptor contents.
1394 if (strcmp(style
, "md5") == 0)
1396 else if (strcmp(style
, "sha1") == 0)
1398 else if (strcmp(style
, "uuid") == 0)
1400 const size_t uuidsz
= 128 / 8;
1402 char buffer
[uuidsz
];
1403 memset(buffer
, 0, uuidsz
);
1405 int descriptor
= ::open("/dev/urandom", O_RDONLY
);
1407 gold_error(_("--build-id=uuid failed: could not open /dev/urandom: %s"),
1411 ssize_t got
= ::read(descriptor
, buffer
, uuidsz
);
1412 ::close(descriptor
);
1414 gold_error(_("/dev/urandom: read failed: %s"), strerror(errno
));
1415 else if (static_cast<size_t>(got
) != uuidsz
)
1416 gold_error(_("/dev/urandom: expected %zu bytes, got %zd bytes"),
1420 desc
.assign(buffer
, uuidsz
);
1423 else if (strncmp(style
, "0x", 2) == 0)
1426 const char* p
= style
+ 2;
1429 if (hex_p(p
[0]) && hex_p(p
[1]))
1431 char c
= (hex_value(p
[0]) << 4) | hex_value(p
[1]);
1435 else if (*p
== '-' || *p
== ':')
1438 gold_fatal(_("--build-id argument '%s' not a valid hex number"),
1441 descsz
= desc
.size();
1444 gold_fatal(_("unrecognized --build-id argument '%s'"), style
);
1447 size_t trailing_padding
;
1448 Output_section
* os
= this->create_note("GNU", elfcpp::NT_GNU_BUILD_ID
,
1449 descsz
, true, &trailing_padding
);
1453 // We know the value already, so we fill it in now.
1454 gold_assert(desc
.size() == descsz
);
1456 Output_section_data
* posd
= new Output_data_const(desc
, 4);
1457 os
->add_output_section_data(posd
);
1459 if (trailing_padding
!= 0)
1461 posd
= new Output_data_fixed_space(trailing_padding
, 0);
1462 os
->add_output_section_data(posd
);
1467 // We need to compute a checksum after we have completed the
1469 gold_assert(trailing_padding
== 0);
1470 this->build_id_note_
= new Output_data_fixed_space(descsz
, 4);
1471 os
->add_output_section_data(this->build_id_note_
);
1472 os
->set_after_input_sections();
1476 // Return whether SEG1 should be before SEG2 in the output file. This
1477 // is based entirely on the segment type and flags. When this is
1478 // called the segment addresses has normally not yet been set.
1481 Layout::segment_precedes(const Output_segment
* seg1
,
1482 const Output_segment
* seg2
)
1484 elfcpp::Elf_Word type1
= seg1
->type();
1485 elfcpp::Elf_Word type2
= seg2
->type();
1487 // The single PT_PHDR segment is required to precede any loadable
1488 // segment. We simply make it always first.
1489 if (type1
== elfcpp::PT_PHDR
)
1491 gold_assert(type2
!= elfcpp::PT_PHDR
);
1494 if (type2
== elfcpp::PT_PHDR
)
1497 // The single PT_INTERP segment is required to precede any loadable
1498 // segment. We simply make it always second.
1499 if (type1
== elfcpp::PT_INTERP
)
1501 gold_assert(type2
!= elfcpp::PT_INTERP
);
1504 if (type2
== elfcpp::PT_INTERP
)
1507 // We then put PT_LOAD segments before any other segments.
1508 if (type1
== elfcpp::PT_LOAD
&& type2
!= elfcpp::PT_LOAD
)
1510 if (type2
== elfcpp::PT_LOAD
&& type1
!= elfcpp::PT_LOAD
)
1513 // We put the PT_TLS segment last, because that is where the dynamic
1514 // linker expects to find it (this is just for efficiency; other
1515 // positions would also work correctly).
1516 if (type1
== elfcpp::PT_TLS
&& type2
!= elfcpp::PT_TLS
)
1518 if (type2
== elfcpp::PT_TLS
&& type1
!= elfcpp::PT_TLS
)
1521 const elfcpp::Elf_Word flags1
= seg1
->flags();
1522 const elfcpp::Elf_Word flags2
= seg2
->flags();
1524 // The order of non-PT_LOAD segments is unimportant. We simply sort
1525 // by the numeric segment type and flags values. There should not
1526 // be more than one segment with the same type and flags.
1527 if (type1
!= elfcpp::PT_LOAD
)
1530 return type1
< type2
;
1531 gold_assert(flags1
!= flags2
);
1532 return flags1
< flags2
;
1535 // If the addresses are set already, sort by load address.
1536 if (seg1
->are_addresses_set())
1538 if (!seg2
->are_addresses_set())
1541 unsigned int section_count1
= seg1
->output_section_count();
1542 unsigned int section_count2
= seg2
->output_section_count();
1543 if (section_count1
== 0 && section_count2
> 0)
1545 if (section_count1
> 0 && section_count2
== 0)
1548 uint64_t paddr1
= seg1
->first_section_load_address();
1549 uint64_t paddr2
= seg2
->first_section_load_address();
1550 if (paddr1
!= paddr2
)
1551 return paddr1
< paddr2
;
1553 else if (seg2
->are_addresses_set())
1556 // We sort PT_LOAD segments based on the flags. Readonly segments
1557 // come before writable segments. Then writable segments with data
1558 // come before writable segments without data. Then executable
1559 // segments come before non-executable segments. Then the unlikely
1560 // case of a non-readable segment comes before the normal case of a
1561 // readable segment. If there are multiple segments with the same
1562 // type and flags, we require that the address be set, and we sort
1563 // by virtual address and then physical address.
1564 if ((flags1
& elfcpp::PF_W
) != (flags2
& elfcpp::PF_W
))
1565 return (flags1
& elfcpp::PF_W
) == 0;
1566 if ((flags1
& elfcpp::PF_W
) != 0
1567 && seg1
->has_any_data_sections() != seg2
->has_any_data_sections())
1568 return seg1
->has_any_data_sections();
1569 if ((flags1
& elfcpp::PF_X
) != (flags2
& elfcpp::PF_X
))
1570 return (flags1
& elfcpp::PF_X
) != 0;
1571 if ((flags1
& elfcpp::PF_R
) != (flags2
& elfcpp::PF_R
))
1572 return (flags1
& elfcpp::PF_R
) == 0;
1574 // We shouldn't get here--we shouldn't create segments which we
1575 // can't distinguish.
1579 // Set the file offsets of all the segments, and all the sections they
1580 // contain. They have all been created. LOAD_SEG must be be laid out
1581 // first. Return the offset of the data to follow.
1584 Layout::set_segment_offsets(const Target
* target
, Output_segment
* load_seg
,
1585 unsigned int *pshndx
)
1587 // Sort them into the final order.
1588 std::sort(this->segment_list_
.begin(), this->segment_list_
.end(),
1589 Layout::Compare_segments());
1591 // Find the PT_LOAD segments, and set their addresses and offsets
1592 // and their section's addresses and offsets.
1594 if (this->options_
.user_set_Ttext())
1595 addr
= this->options_
.Ttext();
1596 else if (parameters
->options().shared())
1599 addr
= target
->default_text_segment_address();
1602 // If LOAD_SEG is NULL, then the file header and segment headers
1603 // will not be loadable. But they still need to be at offset 0 in
1604 // the file. Set their offsets now.
1605 if (load_seg
== NULL
)
1607 for (Data_list::iterator p
= this->special_output_list_
.begin();
1608 p
!= this->special_output_list_
.end();
1611 off
= align_address(off
, (*p
)->addralign());
1612 (*p
)->set_address_and_file_offset(0, off
);
1613 off
+= (*p
)->data_size();
1617 bool was_readonly
= false;
1618 for (Segment_list::iterator p
= this->segment_list_
.begin();
1619 p
!= this->segment_list_
.end();
1622 if ((*p
)->type() == elfcpp::PT_LOAD
)
1624 if (load_seg
!= NULL
&& load_seg
!= *p
)
1628 bool are_addresses_set
= (*p
)->are_addresses_set();
1629 if (are_addresses_set
)
1631 // When it comes to setting file offsets, we care about
1632 // the physical address.
1633 addr
= (*p
)->paddr();
1635 else if (this->options_
.user_set_Tdata()
1636 && ((*p
)->flags() & elfcpp::PF_W
) != 0
1637 && (!this->options_
.user_set_Tbss()
1638 || (*p
)->has_any_data_sections()))
1640 addr
= this->options_
.Tdata();
1641 are_addresses_set
= true;
1643 else if (this->options_
.user_set_Tbss()
1644 && ((*p
)->flags() & elfcpp::PF_W
) != 0
1645 && !(*p
)->has_any_data_sections())
1647 addr
= this->options_
.Tbss();
1648 are_addresses_set
= true;
1651 uint64_t orig_addr
= addr
;
1652 uint64_t orig_off
= off
;
1654 uint64_t aligned_addr
= 0;
1655 uint64_t abi_pagesize
= target
->abi_pagesize();
1657 // FIXME: This should depend on the -n and -N options.
1658 (*p
)->set_minimum_p_align(target
->common_pagesize());
1660 if (are_addresses_set
)
1662 // Adjust the file offset to the same address modulo the
1664 uint64_t unsigned_off
= off
;
1665 uint64_t aligned_off
= ((unsigned_off
& ~(abi_pagesize
- 1))
1666 | (addr
& (abi_pagesize
- 1)));
1667 if (aligned_off
< unsigned_off
)
1668 aligned_off
+= abi_pagesize
;
1673 // If the last segment was readonly, and this one is
1674 // not, then skip the address forward one page,
1675 // maintaining the same position within the page. This
1676 // lets us store both segments overlapping on a single
1677 // page in the file, but the loader will put them on
1678 // different pages in memory.
1680 addr
= align_address(addr
, (*p
)->maximum_alignment());
1681 aligned_addr
= addr
;
1683 if (was_readonly
&& ((*p
)->flags() & elfcpp::PF_W
) != 0)
1685 if ((addr
& (abi_pagesize
- 1)) != 0)
1686 addr
= addr
+ abi_pagesize
;
1689 off
= orig_off
+ ((addr
- orig_addr
) & (abi_pagesize
- 1));
1692 unsigned int shndx_hold
= *pshndx
;
1693 uint64_t new_addr
= (*p
)->set_section_addresses(this, false, addr
,
1696 // Now that we know the size of this segment, we may be able
1697 // to save a page in memory, at the cost of wasting some
1698 // file space, by instead aligning to the start of a new
1699 // page. Here we use the real machine page size rather than
1700 // the ABI mandated page size.
1702 if (!are_addresses_set
&& aligned_addr
!= addr
)
1704 uint64_t common_pagesize
= target
->common_pagesize();
1705 uint64_t first_off
= (common_pagesize
1707 & (common_pagesize
- 1)));
1708 uint64_t last_off
= new_addr
& (common_pagesize
- 1);
1711 && ((aligned_addr
& ~ (common_pagesize
- 1))
1712 != (new_addr
& ~ (common_pagesize
- 1)))
1713 && first_off
+ last_off
<= common_pagesize
)
1715 *pshndx
= shndx_hold
;
1716 addr
= align_address(aligned_addr
, common_pagesize
);
1717 addr
= align_address(addr
, (*p
)->maximum_alignment());
1718 off
= orig_off
+ ((addr
- orig_addr
) & (abi_pagesize
- 1));
1719 new_addr
= (*p
)->set_section_addresses(this, true, addr
,
1726 if (((*p
)->flags() & elfcpp::PF_W
) == 0)
1727 was_readonly
= true;
1731 // Handle the non-PT_LOAD segments, setting their offsets from their
1732 // section's offsets.
1733 for (Segment_list::iterator p
= this->segment_list_
.begin();
1734 p
!= this->segment_list_
.end();
1737 if ((*p
)->type() != elfcpp::PT_LOAD
)
1741 // Set the TLS offsets for each section in the PT_TLS segment.
1742 if (this->tls_segment_
!= NULL
)
1743 this->tls_segment_
->set_tls_offsets();
1748 // Set the offsets of all the allocated sections when doing a
1749 // relocatable link. This does the same jobs as set_segment_offsets,
1750 // only for a relocatable link.
1753 Layout::set_relocatable_section_offsets(Output_data
* file_header
,
1754 unsigned int *pshndx
)
1758 file_header
->set_address_and_file_offset(0, 0);
1759 off
+= file_header
->data_size();
1761 for (Section_list::iterator p
= this->section_list_
.begin();
1762 p
!= this->section_list_
.end();
1765 // We skip unallocated sections here, except that group sections
1766 // have to come first.
1767 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) == 0
1768 && (*p
)->type() != elfcpp::SHT_GROUP
)
1771 off
= align_address(off
, (*p
)->addralign());
1773 // The linker script might have set the address.
1774 if (!(*p
)->is_address_valid())
1775 (*p
)->set_address(0);
1776 (*p
)->set_file_offset(off
);
1777 (*p
)->finalize_data_size();
1778 off
+= (*p
)->data_size();
1780 (*p
)->set_out_shndx(*pshndx
);
1787 // Set the file offset of all the sections not associated with a
1791 Layout::set_section_offsets(off_t off
, Layout::Section_offset_pass pass
)
1793 for (Section_list::iterator p
= this->unattached_section_list_
.begin();
1794 p
!= this->unattached_section_list_
.end();
1797 // The symtab section is handled in create_symtab_sections.
1798 if (*p
== this->symtab_section_
)
1801 // If we've already set the data size, don't set it again.
1802 if ((*p
)->is_offset_valid() && (*p
)->is_data_size_valid())
1805 if (pass
== BEFORE_INPUT_SECTIONS_PASS
1806 && (*p
)->requires_postprocessing())
1808 (*p
)->create_postprocessing_buffer();
1809 this->any_postprocessing_sections_
= true;
1812 if (pass
== BEFORE_INPUT_SECTIONS_PASS
1813 && (*p
)->after_input_sections())
1815 else if (pass
== POSTPROCESSING_SECTIONS_PASS
1816 && (!(*p
)->after_input_sections()
1817 || (*p
)->type() == elfcpp::SHT_STRTAB
))
1819 else if (pass
== STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
1820 && (!(*p
)->after_input_sections()
1821 || (*p
)->type() != elfcpp::SHT_STRTAB
))
1824 off
= align_address(off
, (*p
)->addralign());
1825 (*p
)->set_file_offset(off
);
1826 (*p
)->finalize_data_size();
1827 off
+= (*p
)->data_size();
1829 // At this point the name must be set.
1830 if (pass
!= STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
)
1831 this->namepool_
.add((*p
)->name(), false, NULL
);
1836 // Set the section indexes of all the sections not associated with a
1840 Layout::set_section_indexes(unsigned int shndx
)
1842 for (Section_list::iterator p
= this->unattached_section_list_
.begin();
1843 p
!= this->unattached_section_list_
.end();
1846 if (!(*p
)->has_out_shndx())
1848 (*p
)->set_out_shndx(shndx
);
1855 // Set the section addresses according to the linker script. This is
1856 // only called when we see a SECTIONS clause. This returns the
1857 // program segment which should hold the file header and segment
1858 // headers, if any. It will return NULL if they should not be in a
1862 Layout::set_section_addresses_from_script(Symbol_table
* symtab
)
1864 Script_sections
* ss
= this->script_options_
->script_sections();
1865 gold_assert(ss
->saw_sections_clause());
1867 // Place each orphaned output section in the script.
1868 for (Section_list::iterator p
= this->section_list_
.begin();
1869 p
!= this->section_list_
.end();
1872 if (!(*p
)->found_in_sections_clause())
1873 ss
->place_orphan(*p
);
1876 return this->script_options_
->set_section_addresses(symtab
, this);
1879 // Count the local symbols in the regular symbol table and the dynamic
1880 // symbol table, and build the respective string pools.
1883 Layout::count_local_symbols(const Task
* task
,
1884 const Input_objects
* input_objects
)
1886 // First, figure out an upper bound on the number of symbols we'll
1887 // be inserting into each pool. This helps us create the pools with
1888 // the right size, to avoid unnecessary hashtable resizing.
1889 unsigned int symbol_count
= 0;
1890 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
1891 p
!= input_objects
->relobj_end();
1893 symbol_count
+= (*p
)->local_symbol_count();
1895 // Go from "upper bound" to "estimate." We overcount for two
1896 // reasons: we double-count symbols that occur in more than one
1897 // object file, and we count symbols that are dropped from the
1898 // output. Add it all together and assume we overcount by 100%.
1901 // We assume all symbols will go into both the sympool and dynpool.
1902 this->sympool_
.reserve(symbol_count
);
1903 this->dynpool_
.reserve(symbol_count
);
1905 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
1906 p
!= input_objects
->relobj_end();
1909 Task_lock_obj
<Object
> tlo(task
, *p
);
1910 (*p
)->count_local_symbols(&this->sympool_
, &this->dynpool_
);
1914 // Create the symbol table sections. Here we also set the final
1915 // values of the symbols. At this point all the loadable sections are
1916 // fully laid out. SHNUM is the number of sections so far.
1919 Layout::create_symtab_sections(const Input_objects
* input_objects
,
1920 Symbol_table
* symtab
,
1926 if (parameters
->target().get_size() == 32)
1928 symsize
= elfcpp::Elf_sizes
<32>::sym_size
;
1931 else if (parameters
->target().get_size() == 64)
1933 symsize
= elfcpp::Elf_sizes
<64>::sym_size
;
1940 off
= align_address(off
, align
);
1941 off_t startoff
= off
;
1943 // Save space for the dummy symbol at the start of the section. We
1944 // never bother to write this out--it will just be left as zero.
1946 unsigned int local_symbol_index
= 1;
1948 // Add STT_SECTION symbols for each Output section which needs one.
1949 for (Section_list::iterator p
= this->section_list_
.begin();
1950 p
!= this->section_list_
.end();
1953 if (!(*p
)->needs_symtab_index())
1954 (*p
)->set_symtab_index(-1U);
1957 (*p
)->set_symtab_index(local_symbol_index
);
1958 ++local_symbol_index
;
1963 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
1964 p
!= input_objects
->relobj_end();
1967 unsigned int index
= (*p
)->finalize_local_symbols(local_symbol_index
,
1969 off
+= (index
- local_symbol_index
) * symsize
;
1970 local_symbol_index
= index
;
1973 unsigned int local_symcount
= local_symbol_index
;
1974 gold_assert(local_symcount
* symsize
== off
- startoff
);
1977 size_t dyn_global_index
;
1979 if (this->dynsym_section_
== NULL
)
1982 dyn_global_index
= 0;
1987 dyn_global_index
= this->dynsym_section_
->info();
1988 off_t locsize
= dyn_global_index
* this->dynsym_section_
->entsize();
1989 dynoff
= this->dynsym_section_
->offset() + locsize
;
1990 dyncount
= (this->dynsym_section_
->data_size() - locsize
) / symsize
;
1991 gold_assert(static_cast<off_t
>(dyncount
* symsize
)
1992 == this->dynsym_section_
->data_size() - locsize
);
1995 off
= symtab
->finalize(off
, dynoff
, dyn_global_index
, dyncount
,
1996 &this->sympool_
, &local_symcount
);
1998 if (!parameters
->options().strip_all())
2000 this->sympool_
.set_string_offsets();
2002 const char* symtab_name
= this->namepool_
.add(".symtab", false, NULL
);
2003 Output_section
* osymtab
= this->make_output_section(symtab_name
,
2006 this->symtab_section_
= osymtab
;
2008 Output_section_data
* pos
= new Output_data_fixed_space(off
- startoff
,
2010 osymtab
->add_output_section_data(pos
);
2012 // We generate a .symtab_shndx section if we have more than
2013 // SHN_LORESERVE sections. Technically it is possible that we
2014 // don't need one, because it is possible that there are no
2015 // symbols in any of sections with indexes larger than
2016 // SHN_LORESERVE. That is probably unusual, though, and it is
2017 // easier to always create one than to compute section indexes
2018 // twice (once here, once when writing out the symbols).
2019 if (shnum
>= elfcpp::SHN_LORESERVE
)
2021 const char* symtab_xindex_name
= this->namepool_
.add(".symtab_shndx",
2023 Output_section
* osymtab_xindex
=
2024 this->make_output_section(symtab_xindex_name
,
2025 elfcpp::SHT_SYMTAB_SHNDX
, 0);
2027 size_t symcount
= (off
- startoff
) / symsize
;
2028 this->symtab_xindex_
= new Output_symtab_xindex(symcount
);
2030 osymtab_xindex
->add_output_section_data(this->symtab_xindex_
);
2032 osymtab_xindex
->set_link_section(osymtab
);
2033 osymtab_xindex
->set_addralign(4);
2034 osymtab_xindex
->set_entsize(4);
2036 osymtab_xindex
->set_after_input_sections();
2038 // This tells the driver code to wait until the symbol table
2039 // has written out before writing out the postprocessing
2040 // sections, including the .symtab_shndx section.
2041 this->any_postprocessing_sections_
= true;
2044 const char* strtab_name
= this->namepool_
.add(".strtab", false, NULL
);
2045 Output_section
* ostrtab
= this->make_output_section(strtab_name
,
2049 Output_section_data
* pstr
= new Output_data_strtab(&this->sympool_
);
2050 ostrtab
->add_output_section_data(pstr
);
2052 osymtab
->set_file_offset(startoff
);
2053 osymtab
->finalize_data_size();
2054 osymtab
->set_link_section(ostrtab
);
2055 osymtab
->set_info(local_symcount
);
2056 osymtab
->set_entsize(symsize
);
2062 // Create the .shstrtab section, which holds the names of the
2063 // sections. At the time this is called, we have created all the
2064 // output sections except .shstrtab itself.
2067 Layout::create_shstrtab()
2069 // FIXME: We don't need to create a .shstrtab section if we are
2070 // stripping everything.
2072 const char* name
= this->namepool_
.add(".shstrtab", false, NULL
);
2074 Output_section
* os
= this->make_output_section(name
, elfcpp::SHT_STRTAB
, 0);
2076 // We can't write out this section until we've set all the section
2077 // names, and we don't set the names of compressed output sections
2078 // until relocations are complete.
2079 os
->set_after_input_sections();
2081 Output_section_data
* posd
= new Output_data_strtab(&this->namepool_
);
2082 os
->add_output_section_data(posd
);
2087 // Create the section headers. SIZE is 32 or 64. OFF is the file
2091 Layout::create_shdrs(const Output_section
* shstrtab_section
, off_t
* poff
)
2093 Output_section_headers
* oshdrs
;
2094 oshdrs
= new Output_section_headers(this,
2095 &this->segment_list_
,
2096 &this->section_list_
,
2097 &this->unattached_section_list_
,
2100 off_t off
= align_address(*poff
, oshdrs
->addralign());
2101 oshdrs
->set_address_and_file_offset(0, off
);
2102 off
+= oshdrs
->data_size();
2104 this->section_headers_
= oshdrs
;
2107 // Count the allocated sections.
2110 Layout::allocated_output_section_count() const
2112 size_t section_count
= 0;
2113 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
2114 p
!= this->segment_list_
.end();
2116 section_count
+= (*p
)->output_section_count();
2117 return section_count
;
2120 // Create the dynamic symbol table.
2123 Layout::create_dynamic_symtab(const Input_objects
* input_objects
,
2124 Symbol_table
* symtab
,
2125 Output_section
**pdynstr
,
2126 unsigned int* plocal_dynamic_count
,
2127 std::vector
<Symbol
*>* pdynamic_symbols
,
2128 Versions
* pversions
)
2130 // Count all the symbols in the dynamic symbol table, and set the
2131 // dynamic symbol indexes.
2133 // Skip symbol 0, which is always all zeroes.
2134 unsigned int index
= 1;
2136 // Add STT_SECTION symbols for each Output section which needs one.
2137 for (Section_list::iterator p
= this->section_list_
.begin();
2138 p
!= this->section_list_
.end();
2141 if (!(*p
)->needs_dynsym_index())
2142 (*p
)->set_dynsym_index(-1U);
2145 (*p
)->set_dynsym_index(index
);
2150 // Count the local symbols that need to go in the dynamic symbol table,
2151 // and set the dynamic symbol indexes.
2152 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2153 p
!= input_objects
->relobj_end();
2156 unsigned int new_index
= (*p
)->set_local_dynsym_indexes(index
);
2160 unsigned int local_symcount
= index
;
2161 *plocal_dynamic_count
= local_symcount
;
2163 index
= symtab
->set_dynsym_indexes(index
, pdynamic_symbols
,
2164 &this->dynpool_
, pversions
);
2168 const int size
= parameters
->target().get_size();
2171 symsize
= elfcpp::Elf_sizes
<32>::sym_size
;
2174 else if (size
== 64)
2176 symsize
= elfcpp::Elf_sizes
<64>::sym_size
;
2182 // Create the dynamic symbol table section.
2184 Output_section
* dynsym
= this->choose_output_section(NULL
, ".dynsym",
2189 Output_section_data
* odata
= new Output_data_fixed_space(index
* symsize
,
2191 dynsym
->add_output_section_data(odata
);
2193 dynsym
->set_info(local_symcount
);
2194 dynsym
->set_entsize(symsize
);
2195 dynsym
->set_addralign(align
);
2197 this->dynsym_section_
= dynsym
;
2199 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
2200 odyn
->add_section_address(elfcpp::DT_SYMTAB
, dynsym
);
2201 odyn
->add_constant(elfcpp::DT_SYMENT
, symsize
);
2203 // If there are more than SHN_LORESERVE allocated sections, we
2204 // create a .dynsym_shndx section. It is possible that we don't
2205 // need one, because it is possible that there are no dynamic
2206 // symbols in any of the sections with indexes larger than
2207 // SHN_LORESERVE. This is probably unusual, though, and at this
2208 // time we don't know the actual section indexes so it is
2209 // inconvenient to check.
2210 if (this->allocated_output_section_count() >= elfcpp::SHN_LORESERVE
)
2212 Output_section
* dynsym_xindex
=
2213 this->choose_output_section(NULL
, ".dynsym_shndx",
2214 elfcpp::SHT_SYMTAB_SHNDX
,
2218 this->dynsym_xindex_
= new Output_symtab_xindex(index
);
2220 dynsym_xindex
->add_output_section_data(this->dynsym_xindex_
);
2222 dynsym_xindex
->set_link_section(dynsym
);
2223 dynsym_xindex
->set_addralign(4);
2224 dynsym_xindex
->set_entsize(4);
2226 dynsym_xindex
->set_after_input_sections();
2228 // This tells the driver code to wait until the symbol table has
2229 // written out before writing out the postprocessing sections,
2230 // including the .dynsym_shndx section.
2231 this->any_postprocessing_sections_
= true;
2234 // Create the dynamic string table section.
2236 Output_section
* dynstr
= this->choose_output_section(NULL
, ".dynstr",
2241 Output_section_data
* strdata
= new Output_data_strtab(&this->dynpool_
);
2242 dynstr
->add_output_section_data(strdata
);
2244 dynsym
->set_link_section(dynstr
);
2245 this->dynamic_section_
->set_link_section(dynstr
);
2247 odyn
->add_section_address(elfcpp::DT_STRTAB
, dynstr
);
2248 odyn
->add_section_size(elfcpp::DT_STRSZ
, dynstr
);
2252 // Create the hash tables.
2254 if (strcmp(parameters
->options().hash_style(), "sysv") == 0
2255 || strcmp(parameters
->options().hash_style(), "both") == 0)
2257 unsigned char* phash
;
2258 unsigned int hashlen
;
2259 Dynobj::create_elf_hash_table(*pdynamic_symbols
, local_symcount
,
2262 Output_section
* hashsec
= this->choose_output_section(NULL
, ".hash",
2267 Output_section_data
* hashdata
= new Output_data_const_buffer(phash
,
2270 hashsec
->add_output_section_data(hashdata
);
2272 hashsec
->set_link_section(dynsym
);
2273 hashsec
->set_entsize(4);
2275 odyn
->add_section_address(elfcpp::DT_HASH
, hashsec
);
2278 if (strcmp(parameters
->options().hash_style(), "gnu") == 0
2279 || strcmp(parameters
->options().hash_style(), "both") == 0)
2281 unsigned char* phash
;
2282 unsigned int hashlen
;
2283 Dynobj::create_gnu_hash_table(*pdynamic_symbols
, local_symcount
,
2286 Output_section
* hashsec
= this->choose_output_section(NULL
, ".gnu.hash",
2287 elfcpp::SHT_GNU_HASH
,
2291 Output_section_data
* hashdata
= new Output_data_const_buffer(phash
,
2294 hashsec
->add_output_section_data(hashdata
);
2296 hashsec
->set_link_section(dynsym
);
2297 hashsec
->set_entsize(4);
2299 odyn
->add_section_address(elfcpp::DT_GNU_HASH
, hashsec
);
2303 // Assign offsets to each local portion of the dynamic symbol table.
2306 Layout::assign_local_dynsym_offsets(const Input_objects
* input_objects
)
2308 Output_section
* dynsym
= this->dynsym_section_
;
2309 gold_assert(dynsym
!= NULL
);
2311 off_t off
= dynsym
->offset();
2313 // Skip the dummy symbol at the start of the section.
2314 off
+= dynsym
->entsize();
2316 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
2317 p
!= input_objects
->relobj_end();
2320 unsigned int count
= (*p
)->set_local_dynsym_offset(off
);
2321 off
+= count
* dynsym
->entsize();
2325 // Create the version sections.
2328 Layout::create_version_sections(const Versions
* versions
,
2329 const Symbol_table
* symtab
,
2330 unsigned int local_symcount
,
2331 const std::vector
<Symbol
*>& dynamic_symbols
,
2332 const Output_section
* dynstr
)
2334 if (!versions
->any_defs() && !versions
->any_needs())
2337 switch (parameters
->size_and_endianness())
2339 #ifdef HAVE_TARGET_32_LITTLE
2340 case Parameters::TARGET_32_LITTLE
:
2341 this->sized_create_version_sections
<32, false>(versions
, symtab
,
2343 dynamic_symbols
, dynstr
);
2346 #ifdef HAVE_TARGET_32_BIG
2347 case Parameters::TARGET_32_BIG
:
2348 this->sized_create_version_sections
<32, true>(versions
, symtab
,
2350 dynamic_symbols
, dynstr
);
2353 #ifdef HAVE_TARGET_64_LITTLE
2354 case Parameters::TARGET_64_LITTLE
:
2355 this->sized_create_version_sections
<64, false>(versions
, symtab
,
2357 dynamic_symbols
, dynstr
);
2360 #ifdef HAVE_TARGET_64_BIG
2361 case Parameters::TARGET_64_BIG
:
2362 this->sized_create_version_sections
<64, true>(versions
, symtab
,
2364 dynamic_symbols
, dynstr
);
2372 // Create the version sections, sized version.
2374 template<int size
, bool big_endian
>
2376 Layout::sized_create_version_sections(
2377 const Versions
* versions
,
2378 const Symbol_table
* symtab
,
2379 unsigned int local_symcount
,
2380 const std::vector
<Symbol
*>& dynamic_symbols
,
2381 const Output_section
* dynstr
)
2383 Output_section
* vsec
= this->choose_output_section(NULL
, ".gnu.version",
2384 elfcpp::SHT_GNU_versym
,
2388 unsigned char* vbuf
;
2390 versions
->symbol_section_contents
<size
, big_endian
>(symtab
, &this->dynpool_
,
2395 Output_section_data
* vdata
= new Output_data_const_buffer(vbuf
, vsize
, 2);
2397 vsec
->add_output_section_data(vdata
);
2398 vsec
->set_entsize(2);
2399 vsec
->set_link_section(this->dynsym_section_
);
2401 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
2402 odyn
->add_section_address(elfcpp::DT_VERSYM
, vsec
);
2404 if (versions
->any_defs())
2406 Output_section
* vdsec
;
2407 vdsec
= this->choose_output_section(NULL
, ".gnu.version_d",
2408 elfcpp::SHT_GNU_verdef
,
2412 unsigned char* vdbuf
;
2413 unsigned int vdsize
;
2414 unsigned int vdentries
;
2415 versions
->def_section_contents
<size
, big_endian
>(&this->dynpool_
, &vdbuf
,
2416 &vdsize
, &vdentries
);
2418 Output_section_data
* vddata
= new Output_data_const_buffer(vdbuf
,
2422 vdsec
->add_output_section_data(vddata
);
2423 vdsec
->set_link_section(dynstr
);
2424 vdsec
->set_info(vdentries
);
2426 odyn
->add_section_address(elfcpp::DT_VERDEF
, vdsec
);
2427 odyn
->add_constant(elfcpp::DT_VERDEFNUM
, vdentries
);
2430 if (versions
->any_needs())
2432 Output_section
* vnsec
;
2433 vnsec
= this->choose_output_section(NULL
, ".gnu.version_r",
2434 elfcpp::SHT_GNU_verneed
,
2438 unsigned char* vnbuf
;
2439 unsigned int vnsize
;
2440 unsigned int vnentries
;
2441 versions
->need_section_contents
<size
, big_endian
>(&this->dynpool_
,
2445 Output_section_data
* vndata
= new Output_data_const_buffer(vnbuf
,
2449 vnsec
->add_output_section_data(vndata
);
2450 vnsec
->set_link_section(dynstr
);
2451 vnsec
->set_info(vnentries
);
2453 odyn
->add_section_address(elfcpp::DT_VERNEED
, vnsec
);
2454 odyn
->add_constant(elfcpp::DT_VERNEEDNUM
, vnentries
);
2458 // Create the .interp section and PT_INTERP segment.
2461 Layout::create_interp(const Target
* target
)
2463 const char* interp
= this->options_
.dynamic_linker();
2466 interp
= target
->dynamic_linker();
2467 gold_assert(interp
!= NULL
);
2470 size_t len
= strlen(interp
) + 1;
2472 Output_section_data
* odata
= new Output_data_const(interp
, len
, 1);
2474 Output_section
* osec
= this->choose_output_section(NULL
, ".interp",
2475 elfcpp::SHT_PROGBITS
,
2478 osec
->add_output_section_data(odata
);
2480 if (!this->script_options_
->saw_phdrs_clause())
2482 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_INTERP
,
2484 oseg
->add_initial_output_section(osec
, elfcpp::PF_R
);
2488 // Finish the .dynamic section and PT_DYNAMIC segment.
2491 Layout::finish_dynamic_section(const Input_objects
* input_objects
,
2492 const Symbol_table
* symtab
)
2494 if (!this->script_options_
->saw_phdrs_clause())
2496 Output_segment
* oseg
= this->make_output_segment(elfcpp::PT_DYNAMIC
,
2499 oseg
->add_initial_output_section(this->dynamic_section_
,
2500 elfcpp::PF_R
| elfcpp::PF_W
);
2503 Output_data_dynamic
* const odyn
= this->dynamic_data_
;
2505 for (Input_objects::Dynobj_iterator p
= input_objects
->dynobj_begin();
2506 p
!= input_objects
->dynobj_end();
2509 // FIXME: Handle --as-needed.
2510 odyn
->add_string(elfcpp::DT_NEEDED
, (*p
)->soname());
2513 if (parameters
->options().shared())
2515 const char* soname
= this->options_
.soname();
2517 odyn
->add_string(elfcpp::DT_SONAME
, soname
);
2520 // FIXME: Support --init and --fini.
2521 Symbol
* sym
= symtab
->lookup("_init");
2522 if (sym
!= NULL
&& sym
->is_defined() && !sym
->is_from_dynobj())
2523 odyn
->add_symbol(elfcpp::DT_INIT
, sym
);
2525 sym
= symtab
->lookup("_fini");
2526 if (sym
!= NULL
&& sym
->is_defined() && !sym
->is_from_dynobj())
2527 odyn
->add_symbol(elfcpp::DT_FINI
, sym
);
2529 // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY.
2531 // Add a DT_RPATH entry if needed.
2532 const General_options::Dir_list
& rpath(this->options_
.rpath());
2535 std::string rpath_val
;
2536 for (General_options::Dir_list::const_iterator p
= rpath
.begin();
2540 if (rpath_val
.empty())
2541 rpath_val
= p
->name();
2544 // Eliminate duplicates.
2545 General_options::Dir_list::const_iterator q
;
2546 for (q
= rpath
.begin(); q
!= p
; ++q
)
2547 if (q
->name() == p
->name())
2552 rpath_val
+= p
->name();
2557 odyn
->add_string(elfcpp::DT_RPATH
, rpath_val
);
2558 if (parameters
->options().enable_new_dtags())
2559 odyn
->add_string(elfcpp::DT_RUNPATH
, rpath_val
);
2562 // Look for text segments that have dynamic relocations.
2563 bool have_textrel
= false;
2564 if (!this->script_options_
->saw_sections_clause())
2566 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
2567 p
!= this->segment_list_
.end();
2570 if (((*p
)->flags() & elfcpp::PF_W
) == 0
2571 && (*p
)->dynamic_reloc_count() > 0)
2573 have_textrel
= true;
2580 // We don't know the section -> segment mapping, so we are
2581 // conservative and just look for readonly sections with
2582 // relocations. If those sections wind up in writable segments,
2583 // then we have created an unnecessary DT_TEXTREL entry.
2584 for (Section_list::const_iterator p
= this->section_list_
.begin();
2585 p
!= this->section_list_
.end();
2588 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) != 0
2589 && ((*p
)->flags() & elfcpp::SHF_WRITE
) == 0
2590 && ((*p
)->dynamic_reloc_count() > 0))
2592 have_textrel
= true;
2598 // Add a DT_FLAGS entry. We add it even if no flags are set so that
2599 // post-link tools can easily modify these flags if desired.
2600 unsigned int flags
= 0;
2603 // Add a DT_TEXTREL for compatibility with older loaders.
2604 odyn
->add_constant(elfcpp::DT_TEXTREL
, 0);
2605 flags
|= elfcpp::DF_TEXTREL
;
2607 if (parameters
->options().shared() && this->has_static_tls())
2608 flags
|= elfcpp::DF_STATIC_TLS
;
2609 odyn
->add_constant(elfcpp::DT_FLAGS
, flags
);
2612 if (parameters
->options().initfirst())
2613 flags
|= elfcpp::DF_1_INITFIRST
;
2614 if (parameters
->options().interpose())
2615 flags
|= elfcpp::DF_1_INTERPOSE
;
2616 if (parameters
->options().loadfltr())
2617 flags
|= elfcpp::DF_1_LOADFLTR
;
2618 if (parameters
->options().nodefaultlib())
2619 flags
|= elfcpp::DF_1_NODEFLIB
;
2620 if (parameters
->options().nodelete())
2621 flags
|= elfcpp::DF_1_NODELETE
;
2622 if (parameters
->options().nodlopen())
2623 flags
|= elfcpp::DF_1_NOOPEN
;
2624 if (parameters
->options().nodump())
2625 flags
|= elfcpp::DF_1_NODUMP
;
2626 if (!parameters
->options().shared())
2627 flags
&= ~(elfcpp::DF_1_INITFIRST
2628 | elfcpp::DF_1_NODELETE
2629 | elfcpp::DF_1_NOOPEN
);
2631 odyn
->add_constant(elfcpp::DT_FLAGS_1
, flags
);
2634 // The mapping of .gnu.linkonce section names to real section names.
2636 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
2637 const Layout::Linkonce_mapping
Layout::linkonce_mapping
[] =
2639 MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d".
2640 MAPPING_INIT("t", ".text"),
2641 MAPPING_INIT("r", ".rodata"),
2642 MAPPING_INIT("d", ".data"),
2643 MAPPING_INIT("b", ".bss"),
2644 MAPPING_INIT("s", ".sdata"),
2645 MAPPING_INIT("sb", ".sbss"),
2646 MAPPING_INIT("s2", ".sdata2"),
2647 MAPPING_INIT("sb2", ".sbss2"),
2648 MAPPING_INIT("wi", ".debug_info"),
2649 MAPPING_INIT("td", ".tdata"),
2650 MAPPING_INIT("tb", ".tbss"),
2651 MAPPING_INIT("lr", ".lrodata"),
2652 MAPPING_INIT("l", ".ldata"),
2653 MAPPING_INIT("lb", ".lbss"),
2657 const int Layout::linkonce_mapping_count
=
2658 sizeof(Layout::linkonce_mapping
) / sizeof(Layout::linkonce_mapping
[0]);
2660 // Return the name of the output section to use for a .gnu.linkonce
2661 // section. This is based on the default ELF linker script of the old
2662 // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
2663 // to ".text". Set *PLEN to the length of the name. *PLEN is
2664 // initialized to the length of NAME.
2667 Layout::linkonce_output_name(const char* name
, size_t *plen
)
2669 const char* s
= name
+ sizeof(".gnu.linkonce") - 1;
2673 const Linkonce_mapping
* plm
= linkonce_mapping
;
2674 for (int i
= 0; i
< linkonce_mapping_count
; ++i
, ++plm
)
2676 if (strncmp(s
, plm
->from
, plm
->fromlen
) == 0 && s
[plm
->fromlen
] == '.')
2685 // Choose the output section name to use given an input section name.
2686 // Set *PLEN to the length of the name. *PLEN is initialized to the
2690 Layout::output_section_name(const char* name
, size_t* plen
)
2692 if (Layout::is_linkonce(name
))
2694 // .gnu.linkonce sections are laid out as though they were named
2695 // for the sections are placed into.
2696 return Layout::linkonce_output_name(name
, plen
);
2699 // gcc 4.3 generates the following sorts of section names when it
2700 // needs a section name specific to a function:
2706 // .data.rel.local.FN
2708 // .data.rel.ro.local.FN
2715 // The GNU linker maps all of those to the part before the .FN,
2716 // except that .data.rel.local.FN is mapped to .data, and
2717 // .data.rel.ro.local.FN is mapped to .data.rel.ro. The sections
2718 // beginning with .data.rel.ro.local are grouped together.
2720 // For an anonymous namespace, the string FN can contain a '.'.
2722 // Also of interest: .rodata.strN.N, .rodata.cstN, both of which the
2723 // GNU linker maps to .rodata.
2725 // The .data.rel.ro sections enable a security feature triggered by
2726 // the -z relro option. Section which need to be relocated at
2727 // program startup time but which may be readonly after startup are
2728 // grouped into .data.rel.ro. They are then put into a PT_GNU_RELRO
2729 // segment. The dynamic linker will make that segment writable,
2730 // perform relocations, and then make it read-only. FIXME: We do
2731 // not yet implement this optimization.
2733 // It is hard to handle this in a principled way.
2735 // These are the rules we follow:
2737 // If the section name has no initial '.', or no dot other than an
2738 // initial '.', we use the name unchanged (i.e., "mysection" and
2739 // ".text" are unchanged).
2741 // If the name starts with ".data.rel.ro" we use ".data.rel.ro".
2743 // Otherwise, we drop the second '.' and everything that comes after
2744 // it (i.e., ".text.XXX" becomes ".text").
2746 const char* s
= name
;
2750 const char* sdot
= strchr(s
, '.');
2754 const char* const data_rel_ro
= ".data.rel.ro";
2755 if (strncmp(name
, data_rel_ro
, strlen(data_rel_ro
)) == 0)
2757 *plen
= strlen(data_rel_ro
);
2761 *plen
= sdot
- name
;
2765 // Record the signature of a comdat section, and return whether to
2766 // include it in the link. If GROUP is true, this is a regular
2767 // section group. If GROUP is false, this is a group signature
2768 // derived from the name of a linkonce section. We want linkonce
2769 // signatures and group signatures to block each other, but we don't
2770 // want a linkonce signature to block another linkonce signature.
2773 Layout::add_comdat(const char* signature
, bool group
)
2775 std::string
sig(signature
);
2776 std::pair
<Signatures::iterator
, bool> ins(
2777 this->signatures_
.insert(std::make_pair(sig
, group
)));
2781 // This is the first time we've seen this signature.
2785 if (ins
.first
->second
)
2787 // We've already seen a real section group with this signature.
2792 // This is a real section group, and we've already seen a
2793 // linkonce section with this signature. Record that we've seen
2794 // a section group, and don't include this section group.
2795 ins
.first
->second
= true;
2800 // We've already seen a linkonce section and this is a linkonce
2801 // section. These don't block each other--this may be the same
2802 // symbol name with different section types.
2807 // Store the allocated sections into the section list.
2810 Layout::get_allocated_sections(Section_list
* section_list
) const
2812 for (Section_list::const_iterator p
= this->section_list_
.begin();
2813 p
!= this->section_list_
.end();
2815 if (((*p
)->flags() & elfcpp::SHF_ALLOC
) != 0)
2816 section_list
->push_back(*p
);
2819 // Create an output segment.
2822 Layout::make_output_segment(elfcpp::Elf_Word type
, elfcpp::Elf_Word flags
)
2824 gold_assert(!parameters
->options().relocatable());
2825 Output_segment
* oseg
= new Output_segment(type
, flags
);
2826 this->segment_list_
.push_back(oseg
);
2830 // Write out the Output_sections. Most won't have anything to write,
2831 // since most of the data will come from input sections which are
2832 // handled elsewhere. But some Output_sections do have Output_data.
2835 Layout::write_output_sections(Output_file
* of
) const
2837 for (Section_list::const_iterator p
= this->section_list_
.begin();
2838 p
!= this->section_list_
.end();
2841 if (!(*p
)->after_input_sections())
2846 // Write out data not associated with a section or the symbol table.
2849 Layout::write_data(const Symbol_table
* symtab
, Output_file
* of
) const
2851 if (!parameters
->options().strip_all())
2853 const Output_section
* symtab_section
= this->symtab_section_
;
2854 for (Section_list::const_iterator p
= this->section_list_
.begin();
2855 p
!= this->section_list_
.end();
2858 if ((*p
)->needs_symtab_index())
2860 gold_assert(symtab_section
!= NULL
);
2861 unsigned int index
= (*p
)->symtab_index();
2862 gold_assert(index
> 0 && index
!= -1U);
2863 off_t off
= (symtab_section
->offset()
2864 + index
* symtab_section
->entsize());
2865 symtab
->write_section_symbol(*p
, this->symtab_xindex_
, of
, off
);
2870 const Output_section
* dynsym_section
= this->dynsym_section_
;
2871 for (Section_list::const_iterator p
= this->section_list_
.begin();
2872 p
!= this->section_list_
.end();
2875 if ((*p
)->needs_dynsym_index())
2877 gold_assert(dynsym_section
!= NULL
);
2878 unsigned int index
= (*p
)->dynsym_index();
2879 gold_assert(index
> 0 && index
!= -1U);
2880 off_t off
= (dynsym_section
->offset()
2881 + index
* dynsym_section
->entsize());
2882 symtab
->write_section_symbol(*p
, this->dynsym_xindex_
, of
, off
);
2886 // Write out the Output_data which are not in an Output_section.
2887 for (Data_list::const_iterator p
= this->special_output_list_
.begin();
2888 p
!= this->special_output_list_
.end();
2893 // Write out the Output_sections which can only be written after the
2894 // input sections are complete.
2897 Layout::write_sections_after_input_sections(Output_file
* of
)
2899 // Determine the final section offsets, and thus the final output
2900 // file size. Note we finalize the .shstrab last, to allow the
2901 // after_input_section sections to modify their section-names before
2903 if (this->any_postprocessing_sections_
)
2905 off_t off
= this->output_file_size_
;
2906 off
= this->set_section_offsets(off
, POSTPROCESSING_SECTIONS_PASS
);
2908 // Now that we've finalized the names, we can finalize the shstrab.
2910 this->set_section_offsets(off
,
2911 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
);
2913 if (off
> this->output_file_size_
)
2916 this->output_file_size_
= off
;
2920 for (Section_list::const_iterator p
= this->section_list_
.begin();
2921 p
!= this->section_list_
.end();
2924 if ((*p
)->after_input_sections())
2928 this->section_headers_
->write(of
);
2931 // If the build ID requires computing a checksum, do so here, and
2932 // write it out. We compute a checksum over the entire file because
2933 // that is simplest.
2936 Layout::write_build_id(Output_file
* of
) const
2938 if (this->build_id_note_
== NULL
)
2941 const unsigned char* iv
= of
->get_input_view(0, this->output_file_size_
);
2943 unsigned char* ov
= of
->get_output_view(this->build_id_note_
->offset(),
2944 this->build_id_note_
->data_size());
2946 const char* style
= parameters
->options().build_id();
2947 if (strcmp(style
, "sha1") == 0)
2950 sha1_init_ctx(&ctx
);
2951 sha1_process_bytes(iv
, this->output_file_size_
, &ctx
);
2952 sha1_finish_ctx(&ctx
, ov
);
2954 else if (strcmp(style
, "md5") == 0)
2958 md5_process_bytes(iv
, this->output_file_size_
, &ctx
);
2959 md5_finish_ctx(&ctx
, ov
);
2964 of
->write_output_view(this->build_id_note_
->offset(),
2965 this->build_id_note_
->data_size(),
2968 of
->free_input_view(0, this->output_file_size_
, iv
);
2971 // Write out a binary file. This is called after the link is
2972 // complete. IN is the temporary output file we used to generate the
2973 // ELF code. We simply walk through the segments, read them from
2974 // their file offset in IN, and write them to their load address in
2975 // the output file. FIXME: with a bit more work, we could support
2976 // S-records and/or Intel hex format here.
2979 Layout::write_binary(Output_file
* in
) const
2981 gold_assert(this->options_
.oformat_enum()
2982 == General_options::OBJECT_FORMAT_BINARY
);
2984 // Get the size of the binary file.
2985 uint64_t max_load_address
= 0;
2986 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
2987 p
!= this->segment_list_
.end();
2990 if ((*p
)->type() == elfcpp::PT_LOAD
&& (*p
)->filesz() > 0)
2992 uint64_t max_paddr
= (*p
)->paddr() + (*p
)->filesz();
2993 if (max_paddr
> max_load_address
)
2994 max_load_address
= max_paddr
;
2998 Output_file
out(parameters
->options().output_file_name());
2999 out
.open(max_load_address
);
3001 for (Segment_list::const_iterator p
= this->segment_list_
.begin();
3002 p
!= this->segment_list_
.end();
3005 if ((*p
)->type() == elfcpp::PT_LOAD
&& (*p
)->filesz() > 0)
3007 const unsigned char* vin
= in
->get_input_view((*p
)->offset(),
3009 unsigned char* vout
= out
.get_output_view((*p
)->paddr(),
3011 memcpy(vout
, vin
, (*p
)->filesz());
3012 out
.write_output_view((*p
)->paddr(), (*p
)->filesz(), vout
);
3013 in
->free_input_view((*p
)->offset(), (*p
)->filesz(), vin
);
3020 // Print statistical information to stderr. This is used for --stats.
3023 Layout::print_stats() const
3025 this->namepool_
.print_stats("section name pool");
3026 this->sympool_
.print_stats("output symbol name pool");
3027 this->dynpool_
.print_stats("dynamic name pool");
3029 for (Section_list::const_iterator p
= this->section_list_
.begin();
3030 p
!= this->section_list_
.end();
3032 (*p
)->print_merge_stats();
3035 // Write_sections_task methods.
3037 // We can always run this task.
3040 Write_sections_task::is_runnable()
3045 // We need to unlock both OUTPUT_SECTIONS_BLOCKER and FINAL_BLOCKER
3049 Write_sections_task::locks(Task_locker
* tl
)
3051 tl
->add(this, this->output_sections_blocker_
);
3052 tl
->add(this, this->final_blocker_
);
3055 // Run the task--write out the data.
3058 Write_sections_task::run(Workqueue
*)
3060 this->layout_
->write_output_sections(this->of_
);
3063 // Write_data_task methods.
3065 // We can always run this task.
3068 Write_data_task::is_runnable()
3073 // We need to unlock FINAL_BLOCKER when finished.
3076 Write_data_task::locks(Task_locker
* tl
)
3078 tl
->add(this, this->final_blocker_
);
3081 // Run the task--write out the data.
3084 Write_data_task::run(Workqueue
*)
3086 this->layout_
->write_data(this->symtab_
, this->of_
);
3089 // Write_symbols_task methods.
3091 // We can always run this task.
3094 Write_symbols_task::is_runnable()
3099 // We need to unlock FINAL_BLOCKER when finished.
3102 Write_symbols_task::locks(Task_locker
* tl
)
3104 tl
->add(this, this->final_blocker_
);
3107 // Run the task--write out the symbols.
3110 Write_symbols_task::run(Workqueue
*)
3112 this->symtab_
->write_globals(this->input_objects_
, this->sympool_
,
3113 this->dynpool_
, this->layout_
->symtab_xindex(),
3114 this->layout_
->dynsym_xindex(), this->of_
);
3117 // Write_after_input_sections_task methods.
3119 // We can only run this task after the input sections have completed.
3122 Write_after_input_sections_task::is_runnable()
3124 if (this->input_sections_blocker_
->is_blocked())
3125 return this->input_sections_blocker_
;
3129 // We need to unlock FINAL_BLOCKER when finished.
3132 Write_after_input_sections_task::locks(Task_locker
* tl
)
3134 tl
->add(this, this->final_blocker_
);
3140 Write_after_input_sections_task::run(Workqueue
*)
3142 this->layout_
->write_sections_after_input_sections(this->of_
);
3145 // Close_task_runner methods.
3147 // Run the task--close the file.
3150 Close_task_runner::run(Workqueue
*, const Task
*)
3152 // If we need to compute a checksum for the BUILD if, we do so here.
3153 this->layout_
->write_build_id(this->of_
);
3155 // If we've been asked to create a binary file, we do so here.
3156 if (this->options_
->oformat_enum() != General_options::OBJECT_FORMAT_ELF
)
3157 this->layout_
->write_binary(this->of_
);
3162 // Instantiate the templates we need. We could use the configure
3163 // script to restrict this to only the ones for implemented targets.
3165 #ifdef HAVE_TARGET_32_LITTLE
3168 Layout::layout
<32, false>(Sized_relobj
<32, false>* object
, unsigned int shndx
,
3170 const elfcpp::Shdr
<32, false>& shdr
,
3171 unsigned int, unsigned int, off_t
*);
3174 #ifdef HAVE_TARGET_32_BIG
3177 Layout::layout
<32, true>(Sized_relobj
<32, true>* object
, unsigned int shndx
,
3179 const elfcpp::Shdr
<32, true>& shdr
,
3180 unsigned int, unsigned int, off_t
*);
3183 #ifdef HAVE_TARGET_64_LITTLE
3186 Layout::layout
<64, false>(Sized_relobj
<64, false>* object
, unsigned int shndx
,
3188 const elfcpp::Shdr
<64, false>& shdr
,
3189 unsigned int, unsigned int, off_t
*);
3192 #ifdef HAVE_TARGET_64_BIG
3195 Layout::layout
<64, true>(Sized_relobj
<64, true>* object
, unsigned int shndx
,
3197 const elfcpp::Shdr
<64, true>& shdr
,
3198 unsigned int, unsigned int, off_t
*);
3201 #ifdef HAVE_TARGET_32_LITTLE
3204 Layout::layout_reloc
<32, false>(Sized_relobj
<32, false>* object
,
3205 unsigned int reloc_shndx
,
3206 const elfcpp::Shdr
<32, false>& shdr
,
3207 Output_section
* data_section
,
3208 Relocatable_relocs
* rr
);
3211 #ifdef HAVE_TARGET_32_BIG
3214 Layout::layout_reloc
<32, true>(Sized_relobj
<32, true>* object
,
3215 unsigned int reloc_shndx
,
3216 const elfcpp::Shdr
<32, true>& shdr
,
3217 Output_section
* data_section
,
3218 Relocatable_relocs
* rr
);
3221 #ifdef HAVE_TARGET_64_LITTLE
3224 Layout::layout_reloc
<64, false>(Sized_relobj
<64, false>* object
,
3225 unsigned int reloc_shndx
,
3226 const elfcpp::Shdr
<64, false>& shdr
,
3227 Output_section
* data_section
,
3228 Relocatable_relocs
* rr
);
3231 #ifdef HAVE_TARGET_64_BIG
3234 Layout::layout_reloc
<64, true>(Sized_relobj
<64, true>* object
,
3235 unsigned int reloc_shndx
,
3236 const elfcpp::Shdr
<64, true>& shdr
,
3237 Output_section
* data_section
,
3238 Relocatable_relocs
* rr
);
3241 #ifdef HAVE_TARGET_32_LITTLE
3244 Layout::layout_group
<32, false>(Symbol_table
* symtab
,
3245 Sized_relobj
<32, false>* object
,
3247 const char* group_section_name
,
3248 const char* signature
,
3249 const elfcpp::Shdr
<32, false>& shdr
,
3250 const elfcpp::Elf_Word
* contents
);
3253 #ifdef HAVE_TARGET_32_BIG
3256 Layout::layout_group
<32, true>(Symbol_table
* symtab
,
3257 Sized_relobj
<32, true>* object
,
3259 const char* group_section_name
,
3260 const char* signature
,
3261 const elfcpp::Shdr
<32, true>& shdr
,
3262 const elfcpp::Elf_Word
* contents
);
3265 #ifdef HAVE_TARGET_64_LITTLE
3268 Layout::layout_group
<64, false>(Symbol_table
* symtab
,
3269 Sized_relobj
<64, false>* object
,
3271 const char* group_section_name
,
3272 const char* signature
,
3273 const elfcpp::Shdr
<64, false>& shdr
,
3274 const elfcpp::Elf_Word
* contents
);
3277 #ifdef HAVE_TARGET_64_BIG
3280 Layout::layout_group
<64, true>(Symbol_table
* symtab
,
3281 Sized_relobj
<64, true>* object
,
3283 const char* group_section_name
,
3284 const char* signature
,
3285 const elfcpp::Shdr
<64, true>& shdr
,
3286 const elfcpp::Elf_Word
* contents
);
3289 #ifdef HAVE_TARGET_32_LITTLE
3292 Layout::layout_eh_frame
<32, false>(Sized_relobj
<32, false>* object
,
3293 const unsigned char* symbols
,
3295 const unsigned char* symbol_names
,
3296 off_t symbol_names_size
,
3298 const elfcpp::Shdr
<32, false>& shdr
,
3299 unsigned int reloc_shndx
,
3300 unsigned int reloc_type
,
3304 #ifdef HAVE_TARGET_32_BIG
3307 Layout::layout_eh_frame
<32, true>(Sized_relobj
<32, true>* object
,
3308 const unsigned char* symbols
,
3310 const unsigned char* symbol_names
,
3311 off_t symbol_names_size
,
3313 const elfcpp::Shdr
<32, true>& shdr
,
3314 unsigned int reloc_shndx
,
3315 unsigned int reloc_type
,
3319 #ifdef HAVE_TARGET_64_LITTLE
3322 Layout::layout_eh_frame
<64, false>(Sized_relobj
<64, false>* object
,
3323 const unsigned char* symbols
,
3325 const unsigned char* symbol_names
,
3326 off_t symbol_names_size
,
3328 const elfcpp::Shdr
<64, false>& shdr
,
3329 unsigned int reloc_shndx
,
3330 unsigned int reloc_type
,
3334 #ifdef HAVE_TARGET_64_BIG
3337 Layout::layout_eh_frame
<64, true>(Sized_relobj
<64, true>* object
,
3338 const unsigned char* symbols
,
3340 const unsigned char* symbol_names
,
3341 off_t symbol_names_size
,
3343 const elfcpp::Shdr
<64, true>& shdr
,
3344 unsigned int reloc_shndx
,
3345 unsigned int reloc_type
,
3349 } // End namespace gold.