* output.h (Output_section_lookup_maps::add_merge_section):
[binutils.git] / gold / layout.cc
blobb22a3bd7d6e680d92a2d9d57d58fa419401ac2dc
1 // layout.cc -- lay out output file sections for gold
3 // Copyright 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
23 #include "gold.h"
25 #include <cerrno>
26 #include <cstring>
27 #include <algorithm>
28 #include <iostream>
29 #include <fstream>
30 #include <utility>
31 #include <fcntl.h>
32 #include <fnmatch.h>
33 #include <unistd.h>
34 #include "libiberty.h"
35 #include "md5.h"
36 #include "sha1.h"
38 #include "parameters.h"
39 #include "options.h"
40 #include "mapfile.h"
41 #include "script.h"
42 #include "script-sections.h"
43 #include "output.h"
44 #include "symtab.h"
45 #include "dynobj.h"
46 #include "ehframe.h"
47 #include "compressed_output.h"
48 #include "reduced_debug_output.h"
49 #include "reloc.h"
50 #include "descriptors.h"
51 #include "plugin.h"
52 #include "incremental.h"
53 #include "layout.h"
55 namespace gold
58 // Layout::Relaxation_debug_check methods.
60 // Check that sections and special data are in reset states.
61 // We do not save states for Output_sections and special Output_data.
62 // So we check that they have not assigned any addresses or offsets.
63 // clean_up_after_relaxation simply resets their addresses and offsets.
64 void
65 Layout::Relaxation_debug_check::check_output_data_for_reset_values(
66 const Layout::Section_list& sections,
67 const Layout::Data_list& special_outputs)
69 for(Layout::Section_list::const_iterator p = sections.begin();
70 p != sections.end();
71 ++p)
72 gold_assert((*p)->address_and_file_offset_have_reset_values());
74 for(Layout::Data_list::const_iterator p = special_outputs.begin();
75 p != special_outputs.end();
76 ++p)
77 gold_assert((*p)->address_and_file_offset_have_reset_values());
80 // Save information of SECTIONS for checking later.
82 void
83 Layout::Relaxation_debug_check::read_sections(
84 const Layout::Section_list& sections)
86 for(Layout::Section_list::const_iterator p = sections.begin();
87 p != sections.end();
88 ++p)
90 Output_section* os = *p;
91 Section_info info;
92 info.output_section = os;
93 info.address = os->is_address_valid() ? os->address() : 0;
94 info.data_size = os->is_data_size_valid() ? os->data_size() : -1;
95 info.offset = os->is_offset_valid()? os->offset() : -1 ;
96 this->section_infos_.push_back(info);
100 // Verify SECTIONS using previously recorded information.
102 void
103 Layout::Relaxation_debug_check::verify_sections(
104 const Layout::Section_list& sections)
106 size_t i = 0;
107 for(Layout::Section_list::const_iterator p = sections.begin();
108 p != sections.end();
109 ++p, ++i)
111 Output_section* os = *p;
112 uint64_t address = os->is_address_valid() ? os->address() : 0;
113 off_t data_size = os->is_data_size_valid() ? os->data_size() : -1;
114 off_t offset = os->is_offset_valid()? os->offset() : -1 ;
116 if (i >= this->section_infos_.size())
118 gold_fatal("Section_info of %s missing.\n", os->name());
120 const Section_info& info = this->section_infos_[i];
121 if (os != info.output_section)
122 gold_fatal("Section order changed. Expecting %s but see %s\n",
123 info.output_section->name(), os->name());
124 if (address != info.address
125 || data_size != info.data_size
126 || offset != info.offset)
127 gold_fatal("Section %s changed.\n", os->name());
131 // Layout_task_runner methods.
133 // Lay out the sections. This is called after all the input objects
134 // have been read.
136 void
137 Layout_task_runner::run(Workqueue* workqueue, const Task* task)
139 off_t file_size = this->layout_->finalize(this->input_objects_,
140 this->symtab_,
141 this->target_,
142 task);
144 // Now we know the final size of the output file and we know where
145 // each piece of information goes.
147 if (this->mapfile_ != NULL)
149 this->mapfile_->print_discarded_sections(this->input_objects_);
150 this->layout_->print_to_mapfile(this->mapfile_);
153 Output_file* of = new Output_file(parameters->options().output_file_name());
154 if (this->options_.oformat_enum() != General_options::OBJECT_FORMAT_ELF)
155 of->set_is_temporary();
156 of->open(file_size);
158 // Queue up the final set of tasks.
159 gold::queue_final_tasks(this->options_, this->input_objects_,
160 this->symtab_, this->layout_, workqueue, of);
163 // Layout methods.
165 Layout::Layout(int number_of_input_files, Script_options* script_options)
166 : number_of_input_files_(number_of_input_files),
167 script_options_(script_options),
168 namepool_(),
169 sympool_(),
170 dynpool_(),
171 signatures_(),
172 section_name_map_(),
173 segment_list_(),
174 section_list_(),
175 unattached_section_list_(),
176 special_output_list_(),
177 section_headers_(NULL),
178 tls_segment_(NULL),
179 relro_segment_(NULL),
180 increase_relro_(0),
181 symtab_section_(NULL),
182 symtab_xindex_(NULL),
183 dynsym_section_(NULL),
184 dynsym_xindex_(NULL),
185 dynamic_section_(NULL),
186 dynamic_symbol_(NULL),
187 dynamic_data_(NULL),
188 eh_frame_section_(NULL),
189 eh_frame_data_(NULL),
190 added_eh_frame_data_(false),
191 eh_frame_hdr_section_(NULL),
192 build_id_note_(NULL),
193 debug_abbrev_(NULL),
194 debug_info_(NULL),
195 group_signatures_(),
196 output_file_size_(-1),
197 have_added_input_section_(false),
198 sections_are_attached_(false),
199 input_requires_executable_stack_(false),
200 input_with_gnu_stack_note_(false),
201 input_without_gnu_stack_note_(false),
202 has_static_tls_(false),
203 any_postprocessing_sections_(false),
204 resized_signatures_(false),
205 have_stabstr_section_(false),
206 incremental_inputs_(NULL),
207 record_output_section_data_from_script_(false),
208 script_output_section_data_list_(),
209 segment_states_(NULL),
210 relaxation_debug_check_(NULL)
212 // Make space for more than enough segments for a typical file.
213 // This is just for efficiency--it's OK if we wind up needing more.
214 this->segment_list_.reserve(12);
216 // We expect two unattached Output_data objects: the file header and
217 // the segment headers.
218 this->special_output_list_.reserve(2);
220 // Initialize structure needed for an incremental build.
221 if (parameters->options().incremental())
222 this->incremental_inputs_ = new Incremental_inputs;
224 // The section name pool is worth optimizing in all cases, because
225 // it is small, but there are often overlaps due to .rel sections.
226 this->namepool_.set_optimize();
229 // Hash a key we use to look up an output section mapping.
231 size_t
232 Layout::Hash_key::operator()(const Layout::Key& k) const
234 return k.first + k.second.first + k.second.second;
237 // Returns whether the given section is in the list of
238 // debug-sections-used-by-some-version-of-gdb. Currently,
239 // we've checked versions of gdb up to and including 6.7.1.
241 static const char* gdb_sections[] =
242 { ".debug_abbrev",
243 // ".debug_aranges", // not used by gdb as of 6.7.1
244 ".debug_frame",
245 ".debug_info",
246 ".debug_types",
247 ".debug_line",
248 ".debug_loc",
249 ".debug_macinfo",
250 // ".debug_pubnames", // not used by gdb as of 6.7.1
251 ".debug_ranges",
252 ".debug_str",
255 static const char* lines_only_debug_sections[] =
256 { ".debug_abbrev",
257 // ".debug_aranges", // not used by gdb as of 6.7.1
258 // ".debug_frame",
259 ".debug_info",
260 // ".debug_types",
261 ".debug_line",
262 // ".debug_loc",
263 // ".debug_macinfo",
264 // ".debug_pubnames", // not used by gdb as of 6.7.1
265 // ".debug_ranges",
266 ".debug_str",
269 static inline bool
270 is_gdb_debug_section(const char* str)
272 // We can do this faster: binary search or a hashtable. But why bother?
273 for (size_t i = 0; i < sizeof(gdb_sections)/sizeof(*gdb_sections); ++i)
274 if (strcmp(str, gdb_sections[i]) == 0)
275 return true;
276 return false;
279 static inline bool
280 is_lines_only_debug_section(const char* str)
282 // We can do this faster: binary search or a hashtable. But why bother?
283 for (size_t i = 0;
284 i < sizeof(lines_only_debug_sections)/sizeof(*lines_only_debug_sections);
285 ++i)
286 if (strcmp(str, lines_only_debug_sections[i]) == 0)
287 return true;
288 return false;
291 // Whether to include this section in the link.
293 template<int size, bool big_endian>
294 bool
295 Layout::include_section(Sized_relobj<size, big_endian>*, const char* name,
296 const elfcpp::Shdr<size, big_endian>& shdr)
298 if (shdr.get_sh_flags() & elfcpp::SHF_EXCLUDE)
299 return false;
301 switch (shdr.get_sh_type())
303 case elfcpp::SHT_NULL:
304 case elfcpp::SHT_SYMTAB:
305 case elfcpp::SHT_DYNSYM:
306 case elfcpp::SHT_HASH:
307 case elfcpp::SHT_DYNAMIC:
308 case elfcpp::SHT_SYMTAB_SHNDX:
309 return false;
311 case elfcpp::SHT_STRTAB:
312 // Discard the sections which have special meanings in the ELF
313 // ABI. Keep others (e.g., .stabstr). We could also do this by
314 // checking the sh_link fields of the appropriate sections.
315 return (strcmp(name, ".dynstr") != 0
316 && strcmp(name, ".strtab") != 0
317 && strcmp(name, ".shstrtab") != 0);
319 case elfcpp::SHT_RELA:
320 case elfcpp::SHT_REL:
321 case elfcpp::SHT_GROUP:
322 // If we are emitting relocations these should be handled
323 // elsewhere.
324 gold_assert(!parameters->options().relocatable()
325 && !parameters->options().emit_relocs());
326 return false;
328 case elfcpp::SHT_PROGBITS:
329 if (parameters->options().strip_debug()
330 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
332 if (is_debug_info_section(name))
333 return false;
335 if (parameters->options().strip_debug_non_line()
336 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
338 // Debugging sections can only be recognized by name.
339 if (is_prefix_of(".debug", name)
340 && !is_lines_only_debug_section(name))
341 return false;
343 if (parameters->options().strip_debug_gdb()
344 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
346 // Debugging sections can only be recognized by name.
347 if (is_prefix_of(".debug", name)
348 && !is_gdb_debug_section(name))
349 return false;
351 if (parameters->options().strip_lto_sections()
352 && !parameters->options().relocatable()
353 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
355 // Ignore LTO sections containing intermediate code.
356 if (is_prefix_of(".gnu.lto_", name))
357 return false;
359 // The GNU linker strips .gnu_debuglink sections, so we do too.
360 // This is a feature used to keep debugging information in
361 // separate files.
362 if (strcmp(name, ".gnu_debuglink") == 0)
363 return false;
364 return true;
366 default:
367 return true;
371 // Return an output section named NAME, or NULL if there is none.
373 Output_section*
374 Layout::find_output_section(const char* name) const
376 for (Section_list::const_iterator p = this->section_list_.begin();
377 p != this->section_list_.end();
378 ++p)
379 if (strcmp((*p)->name(), name) == 0)
380 return *p;
381 return NULL;
384 // Return an output segment of type TYPE, with segment flags SET set
385 // and segment flags CLEAR clear. Return NULL if there is none.
387 Output_segment*
388 Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
389 elfcpp::Elf_Word clear) const
391 for (Segment_list::const_iterator p = this->segment_list_.begin();
392 p != this->segment_list_.end();
393 ++p)
394 if (static_cast<elfcpp::PT>((*p)->type()) == type
395 && ((*p)->flags() & set) == set
396 && ((*p)->flags() & clear) == 0)
397 return *p;
398 return NULL;
401 // Return the output section to use for section NAME with type TYPE
402 // and section flags FLAGS. NAME must be canonicalized in the string
403 // pool, and NAME_KEY is the key. IS_INTERP is true if this is the
404 // .interp section. IS_DYNAMIC_LINKER_SECTION is true if this section
405 // is used by the dynamic linker. IS_RELRO is true for a relro
406 // section. IS_LAST_RELRO is true for the last relro section.
407 // IS_FIRST_NON_RELRO is true for the first non-relro section.
409 Output_section*
410 Layout::get_output_section(const char* name, Stringpool::Key name_key,
411 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
412 bool is_interp, bool is_dynamic_linker_section,
413 bool is_relro, bool is_last_relro,
414 bool is_first_non_relro)
416 elfcpp::Elf_Xword lookup_flags = flags;
418 // Ignoring SHF_WRITE and SHF_EXECINSTR here means that we combine
419 // read-write with read-only sections. Some other ELF linkers do
420 // not do this. FIXME: Perhaps there should be an option
421 // controlling this.
422 lookup_flags &= ~(elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR);
424 const Key key(name_key, std::make_pair(type, lookup_flags));
425 const std::pair<Key, Output_section*> v(key, NULL);
426 std::pair<Section_name_map::iterator, bool> ins(
427 this->section_name_map_.insert(v));
429 if (!ins.second)
430 return ins.first->second;
431 else
433 // This is the first time we've seen this name/type/flags
434 // combination. For compatibility with the GNU linker, we
435 // combine sections with contents and zero flags with sections
436 // with non-zero flags. This is a workaround for cases where
437 // assembler code forgets to set section flags. FIXME: Perhaps
438 // there should be an option to control this.
439 Output_section* os = NULL;
441 if (type == elfcpp::SHT_PROGBITS)
443 if (flags == 0)
445 Output_section* same_name = this->find_output_section(name);
446 if (same_name != NULL
447 && same_name->type() == elfcpp::SHT_PROGBITS
448 && (same_name->flags() & elfcpp::SHF_TLS) == 0)
449 os = same_name;
451 else if ((flags & elfcpp::SHF_TLS) == 0)
453 elfcpp::Elf_Xword zero_flags = 0;
454 const Key zero_key(name_key, std::make_pair(type, zero_flags));
455 Section_name_map::iterator p =
456 this->section_name_map_.find(zero_key);
457 if (p != this->section_name_map_.end())
458 os = p->second;
462 if (os == NULL)
463 os = this->make_output_section(name, type, flags, is_interp,
464 is_dynamic_linker_section, is_relro,
465 is_last_relro, is_first_non_relro);
466 ins.first->second = os;
467 return os;
471 // Pick the output section to use for section NAME, in input file
472 // RELOBJ, with type TYPE and flags FLAGS. RELOBJ may be NULL for a
473 // linker created section. IS_INPUT_SECTION is true if we are
474 // choosing an output section for an input section found in a input
475 // file. IS_INTERP is true if this is the .interp section.
476 // IS_DYNAMIC_LINKER_SECTION is true if this section is used by the
477 // dynamic linker. IS_RELRO is true for a relro section.
478 // IS_LAST_RELRO is true for the last relro section.
479 // IS_FIRST_NON_RELRO is true for the first non-relro section. This
480 // will return NULL if the input section should be discarded.
482 Output_section*
483 Layout::choose_output_section(const Relobj* relobj, const char* name,
484 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
485 bool is_input_section, bool is_interp,
486 bool is_dynamic_linker_section, bool is_relro,
487 bool is_last_relro, bool is_first_non_relro)
489 // We should not see any input sections after we have attached
490 // sections to segments.
491 gold_assert(!is_input_section || !this->sections_are_attached_);
493 // Some flags in the input section should not be automatically
494 // copied to the output section.
495 flags &= ~ (elfcpp::SHF_INFO_LINK
496 | elfcpp::SHF_LINK_ORDER
497 | elfcpp::SHF_GROUP
498 | elfcpp::SHF_MERGE
499 | elfcpp::SHF_STRINGS);
501 if (this->script_options_->saw_sections_clause())
503 // We are using a SECTIONS clause, so the output section is
504 // chosen based only on the name.
506 Script_sections* ss = this->script_options_->script_sections();
507 const char* file_name = relobj == NULL ? NULL : relobj->name().c_str();
508 Output_section** output_section_slot;
509 Script_sections::Section_type script_section_type;
510 name = ss->output_section_name(file_name, name, &output_section_slot,
511 &script_section_type);
512 if (name == NULL)
514 // The SECTIONS clause says to discard this input section.
515 return NULL;
518 // We can only handle script section types ST_NONE and ST_NOLOAD.
519 switch (script_section_type)
521 case Script_sections::ST_NONE:
522 break;
523 case Script_sections::ST_NOLOAD:
524 flags &= elfcpp::SHF_ALLOC;
525 break;
526 default:
527 gold_unreachable();
530 // If this is an orphan section--one not mentioned in the linker
531 // script--then OUTPUT_SECTION_SLOT will be NULL, and we do the
532 // default processing below.
534 if (output_section_slot != NULL)
536 if (*output_section_slot != NULL)
538 (*output_section_slot)->update_flags_for_input_section(flags);
539 return *output_section_slot;
542 // We don't put sections found in the linker script into
543 // SECTION_NAME_MAP_. That keeps us from getting confused
544 // if an orphan section is mapped to a section with the same
545 // name as one in the linker script.
547 name = this->namepool_.add(name, false, NULL);
549 Output_section* os =
550 this->make_output_section(name, type, flags, is_interp,
551 is_dynamic_linker_section, is_relro,
552 is_last_relro, is_first_non_relro);
553 os->set_found_in_sections_clause();
555 // Special handling for NOLOAD sections.
556 if (script_section_type == Script_sections::ST_NOLOAD)
558 os->set_is_noload();
560 // The constructor of Output_section sets addresses of non-ALLOC
561 // sections to 0 by default. We don't want that for NOLOAD
562 // sections even if they have no SHF_ALLOC flag.
563 if ((os->flags() & elfcpp::SHF_ALLOC) == 0
564 && os->is_address_valid())
566 gold_assert(os->address() == 0
567 && !os->is_offset_valid()
568 && !os->is_data_size_valid());
569 os->reset_address_and_file_offset();
573 *output_section_slot = os;
574 return os;
578 // FIXME: Handle SHF_OS_NONCONFORMING somewhere.
580 // Turn NAME from the name of the input section into the name of the
581 // output section.
583 size_t len = strlen(name);
584 if (is_input_section
585 && !this->script_options_->saw_sections_clause()
586 && !parameters->options().relocatable())
587 name = Layout::output_section_name(name, &len);
589 Stringpool::Key name_key;
590 name = this->namepool_.add_with_length(name, len, true, &name_key);
592 // Find or make the output section. The output section is selected
593 // based on the section name, type, and flags.
594 return this->get_output_section(name, name_key, type, flags, is_interp,
595 is_dynamic_linker_section, is_relro,
596 is_last_relro, is_first_non_relro);
599 // Return the output section to use for input section SHNDX, with name
600 // NAME, with header HEADER, from object OBJECT. RELOC_SHNDX is the
601 // index of a relocation section which applies to this section, or 0
602 // if none, or -1U if more than one. RELOC_TYPE is the type of the
603 // relocation section if there is one. Set *OFF to the offset of this
604 // input section without the output section. Return NULL if the
605 // section should be discarded. Set *OFF to -1 if the section
606 // contents should not be written directly to the output file, but
607 // will instead receive special handling.
609 template<int size, bool big_endian>
610 Output_section*
611 Layout::layout(Sized_relobj<size, big_endian>* object, unsigned int shndx,
612 const char* name, const elfcpp::Shdr<size, big_endian>& shdr,
613 unsigned int reloc_shndx, unsigned int, off_t* off)
615 *off = 0;
617 if (!this->include_section(object, name, shdr))
618 return NULL;
620 Output_section* os;
622 // Sometimes .init_array*, .preinit_array* and .fini_array* do not have
623 // correct section types. Force them here.
624 elfcpp::Elf_Word sh_type = shdr.get_sh_type();
625 if (sh_type == elfcpp::SHT_PROGBITS)
627 static const char init_array_prefix[] = ".init_array";
628 static const char preinit_array_prefix[] = ".preinit_array";
629 static const char fini_array_prefix[] = ".fini_array";
630 static size_t init_array_prefix_size = sizeof(init_array_prefix) - 1;
631 static size_t preinit_array_prefix_size =
632 sizeof(preinit_array_prefix) - 1;
633 static size_t fini_array_prefix_size = sizeof(fini_array_prefix) - 1;
635 if (strncmp(name, init_array_prefix, init_array_prefix_size) == 0)
636 sh_type = elfcpp::SHT_INIT_ARRAY;
637 else if (strncmp(name, preinit_array_prefix, preinit_array_prefix_size)
638 == 0)
639 sh_type = elfcpp::SHT_PREINIT_ARRAY;
640 else if (strncmp(name, fini_array_prefix, fini_array_prefix_size) == 0)
641 sh_type = elfcpp::SHT_FINI_ARRAY;
644 // In a relocatable link a grouped section must not be combined with
645 // any other sections.
646 if (parameters->options().relocatable()
647 && (shdr.get_sh_flags() & elfcpp::SHF_GROUP) != 0)
649 name = this->namepool_.add(name, true, NULL);
650 os = this->make_output_section(name, sh_type, shdr.get_sh_flags(), false,
651 false, false, false, false);
653 else
655 os = this->choose_output_section(object, name, sh_type,
656 shdr.get_sh_flags(), true, false,
657 false, false, false, false);
658 if (os == NULL)
659 return NULL;
662 // By default the GNU linker sorts input sections whose names match
663 // .ctor.*, .dtor.*, .init_array.*, or .fini_array.*. The sections
664 // are sorted by name. This is used to implement constructor
665 // priority ordering. We are compatible.
666 if (!this->script_options_->saw_sections_clause()
667 && (is_prefix_of(".ctors.", name)
668 || is_prefix_of(".dtors.", name)
669 || is_prefix_of(".init_array.", name)
670 || is_prefix_of(".fini_array.", name)))
671 os->set_must_sort_attached_input_sections();
673 // FIXME: Handle SHF_LINK_ORDER somewhere.
675 *off = os->add_input_section(this, object, shndx, name, shdr, reloc_shndx,
676 this->script_options_->saw_sections_clause());
677 this->have_added_input_section_ = true;
679 return os;
682 // Handle a relocation section when doing a relocatable link.
684 template<int size, bool big_endian>
685 Output_section*
686 Layout::layout_reloc(Sized_relobj<size, big_endian>* object,
687 unsigned int,
688 const elfcpp::Shdr<size, big_endian>& shdr,
689 Output_section* data_section,
690 Relocatable_relocs* rr)
692 gold_assert(parameters->options().relocatable()
693 || parameters->options().emit_relocs());
695 int sh_type = shdr.get_sh_type();
697 std::string name;
698 if (sh_type == elfcpp::SHT_REL)
699 name = ".rel";
700 else if (sh_type == elfcpp::SHT_RELA)
701 name = ".rela";
702 else
703 gold_unreachable();
704 name += data_section->name();
706 // In a relocatable link relocs for a grouped section must not be
707 // combined with other reloc sections.
708 Output_section* os;
709 if (!parameters->options().relocatable()
710 || (data_section->flags() & elfcpp::SHF_GROUP) == 0)
711 os = this->choose_output_section(object, name.c_str(), sh_type,
712 shdr.get_sh_flags(), false, false,
713 false, false, false, false);
714 else
716 const char* n = this->namepool_.add(name.c_str(), true, NULL);
717 os = this->make_output_section(n, sh_type, shdr.get_sh_flags(),
718 false, false, false, false, false);
721 os->set_should_link_to_symtab();
722 os->set_info_section(data_section);
724 Output_section_data* posd;
725 if (sh_type == elfcpp::SHT_REL)
727 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
728 posd = new Output_relocatable_relocs<elfcpp::SHT_REL,
729 size,
730 big_endian>(rr);
732 else if (sh_type == elfcpp::SHT_RELA)
734 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
735 posd = new Output_relocatable_relocs<elfcpp::SHT_RELA,
736 size,
737 big_endian>(rr);
739 else
740 gold_unreachable();
742 os->add_output_section_data(posd);
743 rr->set_output_data(posd);
745 return os;
748 // Handle a group section when doing a relocatable link.
750 template<int size, bool big_endian>
751 void
752 Layout::layout_group(Symbol_table* symtab,
753 Sized_relobj<size, big_endian>* object,
754 unsigned int,
755 const char* group_section_name,
756 const char* signature,
757 const elfcpp::Shdr<size, big_endian>& shdr,
758 elfcpp::Elf_Word flags,
759 std::vector<unsigned int>* shndxes)
761 gold_assert(parameters->options().relocatable());
762 gold_assert(shdr.get_sh_type() == elfcpp::SHT_GROUP);
763 group_section_name = this->namepool_.add(group_section_name, true, NULL);
764 Output_section* os = this->make_output_section(group_section_name,
765 elfcpp::SHT_GROUP,
766 shdr.get_sh_flags(),
767 false, false, false,
768 false, false);
770 // We need to find a symbol with the signature in the symbol table.
771 // If we don't find one now, we need to look again later.
772 Symbol* sym = symtab->lookup(signature, NULL);
773 if (sym != NULL)
774 os->set_info_symndx(sym);
775 else
777 // Reserve some space to minimize reallocations.
778 if (this->group_signatures_.empty())
779 this->group_signatures_.reserve(this->number_of_input_files_ * 16);
781 // We will wind up using a symbol whose name is the signature.
782 // So just put the signature in the symbol name pool to save it.
783 signature = symtab->canonicalize_name(signature);
784 this->group_signatures_.push_back(Group_signature(os, signature));
787 os->set_should_link_to_symtab();
788 os->set_entsize(4);
790 section_size_type entry_count =
791 convert_to_section_size_type(shdr.get_sh_size() / 4);
792 Output_section_data* posd =
793 new Output_data_group<size, big_endian>(object, entry_count, flags,
794 shndxes);
795 os->add_output_section_data(posd);
798 // Special GNU handling of sections name .eh_frame. They will
799 // normally hold exception frame data as defined by the C++ ABI
800 // (http://codesourcery.com/cxx-abi/).
802 template<int size, bool big_endian>
803 Output_section*
804 Layout::layout_eh_frame(Sized_relobj<size, big_endian>* object,
805 const unsigned char* symbols,
806 off_t symbols_size,
807 const unsigned char* symbol_names,
808 off_t symbol_names_size,
809 unsigned int shndx,
810 const elfcpp::Shdr<size, big_endian>& shdr,
811 unsigned int reloc_shndx, unsigned int reloc_type,
812 off_t* off)
814 gold_assert(shdr.get_sh_type() == elfcpp::SHT_PROGBITS);
815 gold_assert((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0);
817 const char* const name = ".eh_frame";
818 Output_section* os = this->choose_output_section(object,
819 name,
820 elfcpp::SHT_PROGBITS,
821 elfcpp::SHF_ALLOC,
822 false, false, false,
823 false, false, false);
824 if (os == NULL)
825 return NULL;
827 if (this->eh_frame_section_ == NULL)
829 this->eh_frame_section_ = os;
830 this->eh_frame_data_ = new Eh_frame();
832 if (parameters->options().eh_frame_hdr())
834 Output_section* hdr_os =
835 this->choose_output_section(NULL,
836 ".eh_frame_hdr",
837 elfcpp::SHT_PROGBITS,
838 elfcpp::SHF_ALLOC,
839 false, false, false,
840 false, false, false);
842 if (hdr_os != NULL)
844 Eh_frame_hdr* hdr_posd = new Eh_frame_hdr(os,
845 this->eh_frame_data_);
846 hdr_os->add_output_section_data(hdr_posd);
848 hdr_os->set_after_input_sections();
850 if (!this->script_options_->saw_phdrs_clause())
852 Output_segment* hdr_oseg;
853 hdr_oseg = this->make_output_segment(elfcpp::PT_GNU_EH_FRAME,
854 elfcpp::PF_R);
855 hdr_oseg->add_output_section(hdr_os, elfcpp::PF_R, false);
858 this->eh_frame_data_->set_eh_frame_hdr(hdr_posd);
863 gold_assert(this->eh_frame_section_ == os);
865 if (this->eh_frame_data_->add_ehframe_input_section(object,
866 symbols,
867 symbols_size,
868 symbol_names,
869 symbol_names_size,
870 shndx,
871 reloc_shndx,
872 reloc_type))
874 os->update_flags_for_input_section(shdr.get_sh_flags());
876 // We found a .eh_frame section we are going to optimize, so now
877 // we can add the set of optimized sections to the output
878 // section. We need to postpone adding this until we've found a
879 // section we can optimize so that the .eh_frame section in
880 // crtbegin.o winds up at the start of the output section.
881 if (!this->added_eh_frame_data_)
883 os->add_output_section_data(this->eh_frame_data_);
884 this->added_eh_frame_data_ = true;
886 *off = -1;
888 else
890 // We couldn't handle this .eh_frame section for some reason.
891 // Add it as a normal section.
892 bool saw_sections_clause = this->script_options_->saw_sections_clause();
893 *off = os->add_input_section(this, object, shndx, name, shdr, reloc_shndx,
894 saw_sections_clause);
895 this->have_added_input_section_ = true;
898 return os;
901 // Add POSD to an output section using NAME, TYPE, and FLAGS. Return
902 // the output section.
904 Output_section*
905 Layout::add_output_section_data(const char* name, elfcpp::Elf_Word type,
906 elfcpp::Elf_Xword flags,
907 Output_section_data* posd,
908 bool is_dynamic_linker_section,
909 bool is_relro, bool is_last_relro,
910 bool is_first_non_relro)
912 Output_section* os = this->choose_output_section(NULL, name, type, flags,
913 false, false,
914 is_dynamic_linker_section,
915 is_relro, is_last_relro,
916 is_first_non_relro);
917 if (os != NULL)
918 os->add_output_section_data(posd);
919 return os;
922 // Map section flags to segment flags.
924 elfcpp::Elf_Word
925 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags)
927 elfcpp::Elf_Word ret = elfcpp::PF_R;
928 if ((flags & elfcpp::SHF_WRITE) != 0)
929 ret |= elfcpp::PF_W;
930 if ((flags & elfcpp::SHF_EXECINSTR) != 0)
931 ret |= elfcpp::PF_X;
932 return ret;
935 // Sometimes we compress sections. This is typically done for
936 // sections that are not part of normal program execution (such as
937 // .debug_* sections), and where the readers of these sections know
938 // how to deal with compressed sections. This routine doesn't say for
939 // certain whether we'll compress -- it depends on commandline options
940 // as well -- just whether this section is a candidate for compression.
941 // (The Output_compressed_section class decides whether to compress
942 // a given section, and picks the name of the compressed section.)
944 static bool
945 is_compressible_debug_section(const char* secname)
947 return (is_prefix_of(".debug", secname));
950 // We may see compressed debug sections in input files. Return TRUE
951 // if this is the name of a compressed debug section.
953 bool
954 is_compressed_debug_section(const char* secname)
956 return (is_prefix_of(".zdebug", secname));
959 // Make a new Output_section, and attach it to segments as
960 // appropriate. IS_INTERP is true if this is the .interp section.
961 // IS_DYNAMIC_LINKER_SECTION is true if this section is used by the
962 // dynamic linker. IS_RELRO is true if this is a relro section.
963 // IS_LAST_RELRO is true if this is the last relro section.
964 // IS_FIRST_NON_RELRO is true if this is the first non relro section.
966 Output_section*
967 Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
968 elfcpp::Elf_Xword flags, bool is_interp,
969 bool is_dynamic_linker_section, bool is_relro,
970 bool is_last_relro, bool is_first_non_relro)
972 Output_section* os;
973 if ((flags & elfcpp::SHF_ALLOC) == 0
974 && strcmp(parameters->options().compress_debug_sections(), "none") != 0
975 && is_compressible_debug_section(name))
976 os = new Output_compressed_section(&parameters->options(), name, type,
977 flags);
978 else if ((flags & elfcpp::SHF_ALLOC) == 0
979 && parameters->options().strip_debug_non_line()
980 && strcmp(".debug_abbrev", name) == 0)
982 os = this->debug_abbrev_ = new Output_reduced_debug_abbrev_section(
983 name, type, flags);
984 if (this->debug_info_)
985 this->debug_info_->set_abbreviations(this->debug_abbrev_);
987 else if ((flags & elfcpp::SHF_ALLOC) == 0
988 && parameters->options().strip_debug_non_line()
989 && strcmp(".debug_info", name) == 0)
991 os = this->debug_info_ = new Output_reduced_debug_info_section(
992 name, type, flags);
993 if (this->debug_abbrev_)
994 this->debug_info_->set_abbreviations(this->debug_abbrev_);
996 else
998 // FIXME: const_cast is ugly.
999 Target* target = const_cast<Target*>(&parameters->target());
1000 os = target->make_output_section(name, type, flags);
1003 if (is_interp)
1004 os->set_is_interp();
1005 if (is_dynamic_linker_section)
1006 os->set_is_dynamic_linker_section();
1007 if (is_relro)
1008 os->set_is_relro();
1009 if (is_last_relro)
1010 os->set_is_last_relro();
1011 if (is_first_non_relro)
1012 os->set_is_first_non_relro();
1014 parameters->target().new_output_section(os);
1016 this->section_list_.push_back(os);
1018 // The GNU linker by default sorts some sections by priority, so we
1019 // do the same. We need to know that this might happen before we
1020 // attach any input sections.
1021 if (!this->script_options_->saw_sections_clause()
1022 && (strcmp(name, ".ctors") == 0
1023 || strcmp(name, ".dtors") == 0
1024 || strcmp(name, ".init_array") == 0
1025 || strcmp(name, ".fini_array") == 0))
1026 os->set_may_sort_attached_input_sections();
1028 // With -z relro, we have to recognize the special sections by name.
1029 // There is no other way.
1030 if (!this->script_options_->saw_sections_clause()
1031 && parameters->options().relro()
1032 && type == elfcpp::SHT_PROGBITS
1033 && (flags & elfcpp::SHF_ALLOC) != 0
1034 && (flags & elfcpp::SHF_WRITE) != 0)
1036 if (strcmp(name, ".data.rel.ro") == 0)
1037 os->set_is_relro();
1038 else if (strcmp(name, ".data.rel.ro.local") == 0)
1040 os->set_is_relro();
1041 os->set_is_relro_local();
1045 // Check for .stab*str sections, as .stab* sections need to link to
1046 // them.
1047 if (type == elfcpp::SHT_STRTAB
1048 && !this->have_stabstr_section_
1049 && strncmp(name, ".stab", 5) == 0
1050 && strcmp(name + strlen(name) - 3, "str") == 0)
1051 this->have_stabstr_section_ = true;
1053 // If we have already attached the sections to segments, then we
1054 // need to attach this one now. This happens for sections created
1055 // directly by the linker.
1056 if (this->sections_are_attached_)
1057 this->attach_section_to_segment(os);
1059 return os;
1062 // Attach output sections to segments. This is called after we have
1063 // seen all the input sections.
1065 void
1066 Layout::attach_sections_to_segments()
1068 for (Section_list::iterator p = this->section_list_.begin();
1069 p != this->section_list_.end();
1070 ++p)
1071 this->attach_section_to_segment(*p);
1073 this->sections_are_attached_ = true;
1076 // Attach an output section to a segment.
1078 void
1079 Layout::attach_section_to_segment(Output_section* os)
1081 if ((os->flags() & elfcpp::SHF_ALLOC) == 0)
1082 this->unattached_section_list_.push_back(os);
1083 else
1084 this->attach_allocated_section_to_segment(os);
1087 // Attach an allocated output section to a segment.
1089 void
1090 Layout::attach_allocated_section_to_segment(Output_section* os)
1092 elfcpp::Elf_Xword flags = os->flags();
1093 gold_assert((flags & elfcpp::SHF_ALLOC) != 0);
1095 if (parameters->options().relocatable())
1096 return;
1098 // If we have a SECTIONS clause, we can't handle the attachment to
1099 // segments until after we've seen all the sections.
1100 if (this->script_options_->saw_sections_clause())
1101 return;
1103 gold_assert(!this->script_options_->saw_phdrs_clause());
1105 // This output section goes into a PT_LOAD segment.
1107 elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags);
1109 // Check for --section-start.
1110 uint64_t addr;
1111 bool is_address_set = parameters->options().section_start(os->name(), &addr);
1113 // In general the only thing we really care about for PT_LOAD
1114 // segments is whether or not they are writable, so that is how we
1115 // search for them. Large data sections also go into their own
1116 // PT_LOAD segment. People who need segments sorted on some other
1117 // basis will have to use a linker script.
1119 Segment_list::const_iterator p;
1120 for (p = this->segment_list_.begin();
1121 p != this->segment_list_.end();
1122 ++p)
1124 if ((*p)->type() != elfcpp::PT_LOAD)
1125 continue;
1126 if (!parameters->options().omagic()
1127 && ((*p)->flags() & elfcpp::PF_W) != (seg_flags & elfcpp::PF_W))
1128 continue;
1129 // If -Tbss was specified, we need to separate the data and BSS
1130 // segments.
1131 if (parameters->options().user_set_Tbss())
1133 if ((os->type() == elfcpp::SHT_NOBITS)
1134 == (*p)->has_any_data_sections())
1135 continue;
1137 if (os->is_large_data_section() && !(*p)->is_large_data_segment())
1138 continue;
1140 if (is_address_set)
1142 if ((*p)->are_addresses_set())
1143 continue;
1145 (*p)->add_initial_output_data(os);
1146 (*p)->update_flags_for_output_section(seg_flags);
1147 (*p)->set_addresses(addr, addr);
1148 break;
1151 (*p)->add_output_section(os, seg_flags, true);
1152 break;
1155 if (p == this->segment_list_.end())
1157 Output_segment* oseg = this->make_output_segment(elfcpp::PT_LOAD,
1158 seg_flags);
1159 if (os->is_large_data_section())
1160 oseg->set_is_large_data_segment();
1161 oseg->add_output_section(os, seg_flags, true);
1162 if (is_address_set)
1163 oseg->set_addresses(addr, addr);
1166 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
1167 // segment.
1168 if (os->type() == elfcpp::SHT_NOTE)
1170 // See if we already have an equivalent PT_NOTE segment.
1171 for (p = this->segment_list_.begin();
1172 p != segment_list_.end();
1173 ++p)
1175 if ((*p)->type() == elfcpp::PT_NOTE
1176 && (((*p)->flags() & elfcpp::PF_W)
1177 == (seg_flags & elfcpp::PF_W)))
1179 (*p)->add_output_section(os, seg_flags, false);
1180 break;
1184 if (p == this->segment_list_.end())
1186 Output_segment* oseg = this->make_output_segment(elfcpp::PT_NOTE,
1187 seg_flags);
1188 oseg->add_output_section(os, seg_flags, false);
1192 // If we see a loadable SHF_TLS section, we create a PT_TLS
1193 // segment. There can only be one such segment.
1194 if ((flags & elfcpp::SHF_TLS) != 0)
1196 if (this->tls_segment_ == NULL)
1197 this->make_output_segment(elfcpp::PT_TLS, seg_flags);
1198 this->tls_segment_->add_output_section(os, seg_flags, false);
1201 // If -z relro is in effect, and we see a relro section, we create a
1202 // PT_GNU_RELRO segment. There can only be one such segment.
1203 if (os->is_relro() && parameters->options().relro())
1205 gold_assert(seg_flags == (elfcpp::PF_R | elfcpp::PF_W));
1206 if (this->relro_segment_ == NULL)
1207 this->make_output_segment(elfcpp::PT_GNU_RELRO, seg_flags);
1208 this->relro_segment_->add_output_section(os, seg_flags, false);
1212 // Make an output section for a script.
1214 Output_section*
1215 Layout::make_output_section_for_script(
1216 const char* name,
1217 Script_sections::Section_type section_type)
1219 name = this->namepool_.add(name, false, NULL);
1220 elfcpp::Elf_Xword sh_flags = elfcpp::SHF_ALLOC;
1221 if (section_type == Script_sections::ST_NOLOAD)
1222 sh_flags = 0;
1223 Output_section* os = this->make_output_section(name, elfcpp::SHT_PROGBITS,
1224 sh_flags, false,
1225 false, false, false, false);
1226 os->set_found_in_sections_clause();
1227 if (section_type == Script_sections::ST_NOLOAD)
1228 os->set_is_noload();
1229 return os;
1232 // Return the number of segments we expect to see.
1234 size_t
1235 Layout::expected_segment_count() const
1237 size_t ret = this->segment_list_.size();
1239 // If we didn't see a SECTIONS clause in a linker script, we should
1240 // already have the complete list of segments. Otherwise we ask the
1241 // SECTIONS clause how many segments it expects, and add in the ones
1242 // we already have (PT_GNU_STACK, PT_GNU_EH_FRAME, etc.)
1244 if (!this->script_options_->saw_sections_clause())
1245 return ret;
1246 else
1248 const Script_sections* ss = this->script_options_->script_sections();
1249 return ret + ss->expected_segment_count(this);
1253 // Handle the .note.GNU-stack section at layout time. SEEN_GNU_STACK
1254 // is whether we saw a .note.GNU-stack section in the object file.
1255 // GNU_STACK_FLAGS is the section flags. The flags give the
1256 // protection required for stack memory. We record this in an
1257 // executable as a PT_GNU_STACK segment. If an object file does not
1258 // have a .note.GNU-stack segment, we must assume that it is an old
1259 // object. On some targets that will force an executable stack.
1261 void
1262 Layout::layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags)
1264 if (!seen_gnu_stack)
1265 this->input_without_gnu_stack_note_ = true;
1266 else
1268 this->input_with_gnu_stack_note_ = true;
1269 if ((gnu_stack_flags & elfcpp::SHF_EXECINSTR) != 0)
1270 this->input_requires_executable_stack_ = true;
1274 // Create automatic note sections.
1276 void
1277 Layout::create_notes()
1279 this->create_gold_note();
1280 this->create_executable_stack_info();
1281 this->create_build_id();
1284 // Create the dynamic sections which are needed before we read the
1285 // relocs.
1287 void
1288 Layout::create_initial_dynamic_sections(Symbol_table* symtab)
1290 if (parameters->doing_static_link())
1291 return;
1293 this->dynamic_section_ = this->choose_output_section(NULL, ".dynamic",
1294 elfcpp::SHT_DYNAMIC,
1295 (elfcpp::SHF_ALLOC
1296 | elfcpp::SHF_WRITE),
1297 false, false, true,
1298 true, false, false);
1300 this->dynamic_symbol_ =
1301 symtab->define_in_output_data("_DYNAMIC", NULL, Symbol_table::PREDEFINED,
1302 this->dynamic_section_, 0, 0,
1303 elfcpp::STT_OBJECT, elfcpp::STB_LOCAL,
1304 elfcpp::STV_HIDDEN, 0, false, false);
1306 this->dynamic_data_ = new Output_data_dynamic(&this->dynpool_);
1308 this->dynamic_section_->add_output_section_data(this->dynamic_data_);
1311 // For each output section whose name can be represented as C symbol,
1312 // define __start and __stop symbols for the section. This is a GNU
1313 // extension.
1315 void
1316 Layout::define_section_symbols(Symbol_table* symtab)
1318 for (Section_list::const_iterator p = this->section_list_.begin();
1319 p != this->section_list_.end();
1320 ++p)
1322 const char* const name = (*p)->name();
1323 if (is_cident(name))
1325 const std::string name_string(name);
1326 const std::string start_name(cident_section_start_prefix
1327 + name_string);
1328 const std::string stop_name(cident_section_stop_prefix
1329 + name_string);
1331 symtab->define_in_output_data(start_name.c_str(),
1332 NULL, // version
1333 Symbol_table::PREDEFINED,
1335 0, // value
1336 0, // symsize
1337 elfcpp::STT_NOTYPE,
1338 elfcpp::STB_GLOBAL,
1339 elfcpp::STV_DEFAULT,
1340 0, // nonvis
1341 false, // offset_is_from_end
1342 true); // only_if_ref
1344 symtab->define_in_output_data(stop_name.c_str(),
1345 NULL, // version
1346 Symbol_table::PREDEFINED,
1348 0, // value
1349 0, // symsize
1350 elfcpp::STT_NOTYPE,
1351 elfcpp::STB_GLOBAL,
1352 elfcpp::STV_DEFAULT,
1353 0, // nonvis
1354 true, // offset_is_from_end
1355 true); // only_if_ref
1360 // Define symbols for group signatures.
1362 void
1363 Layout::define_group_signatures(Symbol_table* symtab)
1365 for (Group_signatures::iterator p = this->group_signatures_.begin();
1366 p != this->group_signatures_.end();
1367 ++p)
1369 Symbol* sym = symtab->lookup(p->signature, NULL);
1370 if (sym != NULL)
1371 p->section->set_info_symndx(sym);
1372 else
1374 // Force the name of the group section to the group
1375 // signature, and use the group's section symbol as the
1376 // signature symbol.
1377 if (strcmp(p->section->name(), p->signature) != 0)
1379 const char* name = this->namepool_.add(p->signature,
1380 true, NULL);
1381 p->section->set_name(name);
1383 p->section->set_needs_symtab_index();
1384 p->section->set_info_section_symndx(p->section);
1388 this->group_signatures_.clear();
1391 // Find the first read-only PT_LOAD segment, creating one if
1392 // necessary.
1394 Output_segment*
1395 Layout::find_first_load_seg()
1397 for (Segment_list::const_iterator p = this->segment_list_.begin();
1398 p != this->segment_list_.end();
1399 ++p)
1401 if ((*p)->type() == elfcpp::PT_LOAD
1402 && ((*p)->flags() & elfcpp::PF_R) != 0
1403 && (parameters->options().omagic()
1404 || ((*p)->flags() & elfcpp::PF_W) == 0))
1405 return *p;
1408 gold_assert(!this->script_options_->saw_phdrs_clause());
1410 Output_segment* load_seg = this->make_output_segment(elfcpp::PT_LOAD,
1411 elfcpp::PF_R);
1412 return load_seg;
1415 // Save states of all current output segments. Store saved states
1416 // in SEGMENT_STATES.
1418 void
1419 Layout::save_segments(Segment_states* segment_states)
1421 for (Segment_list::const_iterator p = this->segment_list_.begin();
1422 p != this->segment_list_.end();
1423 ++p)
1425 Output_segment* segment = *p;
1426 // Shallow copy.
1427 Output_segment* copy = new Output_segment(*segment);
1428 (*segment_states)[segment] = copy;
1432 // Restore states of output segments and delete any segment not found in
1433 // SEGMENT_STATES.
1435 void
1436 Layout::restore_segments(const Segment_states* segment_states)
1438 // Go through the segment list and remove any segment added in the
1439 // relaxation loop.
1440 this->tls_segment_ = NULL;
1441 this->relro_segment_ = NULL;
1442 Segment_list::iterator list_iter = this->segment_list_.begin();
1443 while (list_iter != this->segment_list_.end())
1445 Output_segment* segment = *list_iter;
1446 Segment_states::const_iterator states_iter =
1447 segment_states->find(segment);
1448 if (states_iter != segment_states->end())
1450 const Output_segment* copy = states_iter->second;
1451 // Shallow copy to restore states.
1452 *segment = *copy;
1454 // Also fix up TLS and RELRO segment pointers as appropriate.
1455 if (segment->type() == elfcpp::PT_TLS)
1456 this->tls_segment_ = segment;
1457 else if (segment->type() == elfcpp::PT_GNU_RELRO)
1458 this->relro_segment_ = segment;
1460 ++list_iter;
1462 else
1464 list_iter = this->segment_list_.erase(list_iter);
1465 // This is a segment created during section layout. It should be
1466 // safe to remove it since we should have removed all pointers to it.
1467 delete segment;
1472 // Clean up after relaxation so that sections can be laid out again.
1474 void
1475 Layout::clean_up_after_relaxation()
1477 // Restore the segments to point state just prior to the relaxation loop.
1478 Script_sections* script_section = this->script_options_->script_sections();
1479 script_section->release_segments();
1480 this->restore_segments(this->segment_states_);
1482 // Reset section addresses and file offsets
1483 for (Section_list::iterator p = this->section_list_.begin();
1484 p != this->section_list_.end();
1485 ++p)
1487 (*p)->restore_states();
1489 // If an input section changes size because of relaxation,
1490 // we need to adjust the section offsets of all input sections.
1491 // after such a section.
1492 if ((*p)->section_offsets_need_adjustment())
1493 (*p)->adjust_section_offsets();
1495 (*p)->reset_address_and_file_offset();
1498 // Reset special output object address and file offsets.
1499 for (Data_list::iterator p = this->special_output_list_.begin();
1500 p != this->special_output_list_.end();
1501 ++p)
1502 (*p)->reset_address_and_file_offset();
1504 // A linker script may have created some output section data objects.
1505 // They are useless now.
1506 for (Output_section_data_list::const_iterator p =
1507 this->script_output_section_data_list_.begin();
1508 p != this->script_output_section_data_list_.end();
1509 ++p)
1510 delete *p;
1511 this->script_output_section_data_list_.clear();
1514 // Prepare for relaxation.
1516 void
1517 Layout::prepare_for_relaxation()
1519 // Create an relaxation debug check if in debugging mode.
1520 if (is_debugging_enabled(DEBUG_RELAXATION))
1521 this->relaxation_debug_check_ = new Relaxation_debug_check();
1523 // Save segment states.
1524 this->segment_states_ = new Segment_states();
1525 this->save_segments(this->segment_states_);
1527 for(Section_list::const_iterator p = this->section_list_.begin();
1528 p != this->section_list_.end();
1529 ++p)
1530 (*p)->save_states();
1532 if (is_debugging_enabled(DEBUG_RELAXATION))
1533 this->relaxation_debug_check_->check_output_data_for_reset_values(
1534 this->section_list_, this->special_output_list_);
1536 // Also enable recording of output section data from scripts.
1537 this->record_output_section_data_from_script_ = true;
1540 // Relaxation loop body: If target has no relaxation, this runs only once
1541 // Otherwise, the target relaxation hook is called at the end of
1542 // each iteration. If the hook returns true, it means re-layout of
1543 // section is required.
1545 // The number of segments created by a linking script without a PHDRS
1546 // clause may be affected by section sizes and alignments. There is
1547 // a remote chance that relaxation causes different number of PT_LOAD
1548 // segments are created and sections are attached to different segments.
1549 // Therefore, we always throw away all segments created during section
1550 // layout. In order to be able to restart the section layout, we keep
1551 // a copy of the segment list right before the relaxation loop and use
1552 // that to restore the segments.
1554 // PASS is the current relaxation pass number.
1555 // SYMTAB is a symbol table.
1556 // PLOAD_SEG is the address of a pointer for the load segment.
1557 // PHDR_SEG is a pointer to the PHDR segment.
1558 // SEGMENT_HEADERS points to the output segment header.
1559 // FILE_HEADER points to the output file header.
1560 // PSHNDX is the address to store the output section index.
1562 off_t inline
1563 Layout::relaxation_loop_body(
1564 int pass,
1565 Target* target,
1566 Symbol_table* symtab,
1567 Output_segment** pload_seg,
1568 Output_segment* phdr_seg,
1569 Output_segment_headers* segment_headers,
1570 Output_file_header* file_header,
1571 unsigned int* pshndx)
1573 // If this is not the first iteration, we need to clean up after
1574 // relaxation so that we can lay out the sections again.
1575 if (pass != 0)
1576 this->clean_up_after_relaxation();
1578 // If there is a SECTIONS clause, put all the input sections into
1579 // the required order.
1580 Output_segment* load_seg;
1581 if (this->script_options_->saw_sections_clause())
1582 load_seg = this->set_section_addresses_from_script(symtab);
1583 else if (parameters->options().relocatable())
1584 load_seg = NULL;
1585 else
1586 load_seg = this->find_first_load_seg();
1588 if (parameters->options().oformat_enum()
1589 != General_options::OBJECT_FORMAT_ELF)
1590 load_seg = NULL;
1592 // If the user set the address of the text segment, that may not be
1593 // compatible with putting the segment headers and file headers into
1594 // that segment.
1595 if (parameters->options().user_set_Ttext())
1596 load_seg = NULL;
1598 gold_assert(phdr_seg == NULL
1599 || load_seg != NULL
1600 || this->script_options_->saw_sections_clause());
1602 // If the address of the load segment we found has been set by
1603 // --section-start rather than by a script, then we don't want to
1604 // use it for the file and segment headers.
1605 if (load_seg != NULL
1606 && load_seg->are_addresses_set()
1607 && !this->script_options_->saw_sections_clause())
1608 load_seg = NULL;
1610 // Lay out the segment headers.
1611 if (!parameters->options().relocatable())
1613 gold_assert(segment_headers != NULL);
1614 if (load_seg != NULL)
1615 load_seg->add_initial_output_data(segment_headers);
1616 if (phdr_seg != NULL)
1617 phdr_seg->add_initial_output_data(segment_headers);
1620 // Lay out the file header.
1621 if (load_seg != NULL)
1622 load_seg->add_initial_output_data(file_header);
1624 if (this->script_options_->saw_phdrs_clause()
1625 && !parameters->options().relocatable())
1627 // Support use of FILEHDRS and PHDRS attachments in a PHDRS
1628 // clause in a linker script.
1629 Script_sections* ss = this->script_options_->script_sections();
1630 ss->put_headers_in_phdrs(file_header, segment_headers);
1633 // We set the output section indexes in set_segment_offsets and
1634 // set_section_indexes.
1635 *pshndx = 1;
1637 // Set the file offsets of all the segments, and all the sections
1638 // they contain.
1639 off_t off;
1640 if (!parameters->options().relocatable())
1641 off = this->set_segment_offsets(target, load_seg, pshndx);
1642 else
1643 off = this->set_relocatable_section_offsets(file_header, pshndx);
1645 // Verify that the dummy relaxation does not change anything.
1646 if (is_debugging_enabled(DEBUG_RELAXATION))
1648 if (pass == 0)
1649 this->relaxation_debug_check_->read_sections(this->section_list_);
1650 else
1651 this->relaxation_debug_check_->verify_sections(this->section_list_);
1654 *pload_seg = load_seg;
1655 return off;
1658 // Search the list of patterns and find the postion of the given section
1659 // name in the output section. If the section name matches a glob
1660 // pattern and a non-glob name, then the non-glob position takes
1661 // precedence. Return 0 if no match is found.
1663 unsigned int
1664 Layout::find_section_order_index(const std::string& section_name)
1666 Unordered_map<std::string, unsigned int>::iterator map_it;
1667 map_it = this->input_section_position_.find(section_name);
1668 if (map_it != this->input_section_position_.end())
1669 return map_it->second;
1671 // Absolute match failed. Linear search the glob patterns.
1672 std::vector<std::string>::iterator it;
1673 for (it = this->input_section_glob_.begin();
1674 it != this->input_section_glob_.end();
1675 ++it)
1677 if (fnmatch((*it).c_str(), section_name.c_str(), FNM_NOESCAPE) == 0)
1679 map_it = this->input_section_position_.find(*it);
1680 gold_assert(map_it != this->input_section_position_.end());
1681 return map_it->second;
1684 return 0;
1687 // Read the sequence of input sections from the file specified with
1688 // --section-ordering-file.
1690 void
1691 Layout::read_layout_from_file()
1693 const char* filename = parameters->options().section_ordering_file();
1694 std::ifstream in;
1695 std::string line;
1697 in.open(filename);
1698 if (!in)
1699 gold_fatal(_("unable to open --section-ordering-file file %s: %s"),
1700 filename, strerror(errno));
1702 std::getline(in, line); // this chops off the trailing \n, if any
1703 unsigned int position = 1;
1705 while (in)
1707 if (!line.empty() && line[line.length() - 1] == '\r') // Windows
1708 line.resize(line.length() - 1);
1709 // Ignore comments, beginning with '#'
1710 if (line[0] == '#')
1712 std::getline(in, line);
1713 continue;
1715 this->input_section_position_[line] = position;
1716 // Store all glob patterns in a vector.
1717 if (is_wildcard_string(line.c_str()))
1718 this->input_section_glob_.push_back(line);
1719 position++;
1720 std::getline(in, line);
1724 // Finalize the layout. When this is called, we have created all the
1725 // output sections and all the output segments which are based on
1726 // input sections. We have several things to do, and we have to do
1727 // them in the right order, so that we get the right results correctly
1728 // and efficiently.
1730 // 1) Finalize the list of output segments and create the segment
1731 // table header.
1733 // 2) Finalize the dynamic symbol table and associated sections.
1735 // 3) Determine the final file offset of all the output segments.
1737 // 4) Determine the final file offset of all the SHF_ALLOC output
1738 // sections.
1740 // 5) Create the symbol table sections and the section name table
1741 // section.
1743 // 6) Finalize the symbol table: set symbol values to their final
1744 // value and make a final determination of which symbols are going
1745 // into the output symbol table.
1747 // 7) Create the section table header.
1749 // 8) Determine the final file offset of all the output sections which
1750 // are not SHF_ALLOC, including the section table header.
1752 // 9) Finalize the ELF file header.
1754 // This function returns the size of the output file.
1756 off_t
1757 Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab,
1758 Target* target, const Task* task)
1760 target->finalize_sections(this, input_objects, symtab);
1762 this->count_local_symbols(task, input_objects);
1764 this->link_stabs_sections();
1766 Output_segment* phdr_seg = NULL;
1767 if (!parameters->options().relocatable() && !parameters->doing_static_link())
1769 // There was a dynamic object in the link. We need to create
1770 // some information for the dynamic linker.
1772 // Create the PT_PHDR segment which will hold the program
1773 // headers.
1774 if (!this->script_options_->saw_phdrs_clause())
1775 phdr_seg = this->make_output_segment(elfcpp::PT_PHDR, elfcpp::PF_R);
1777 // Create the dynamic symbol table, including the hash table.
1778 Output_section* dynstr;
1779 std::vector<Symbol*> dynamic_symbols;
1780 unsigned int local_dynamic_count;
1781 Versions versions(*this->script_options()->version_script_info(),
1782 &this->dynpool_);
1783 this->create_dynamic_symtab(input_objects, symtab, &dynstr,
1784 &local_dynamic_count, &dynamic_symbols,
1785 &versions);
1787 // Create the .interp section to hold the name of the
1788 // interpreter, and put it in a PT_INTERP segment.
1789 if (!parameters->options().shared())
1790 this->create_interp(target);
1792 // Finish the .dynamic section to hold the dynamic data, and put
1793 // it in a PT_DYNAMIC segment.
1794 this->finish_dynamic_section(input_objects, symtab);
1796 // We should have added everything we need to the dynamic string
1797 // table.
1798 this->dynpool_.set_string_offsets();
1800 // Create the version sections. We can't do this until the
1801 // dynamic string table is complete.
1802 this->create_version_sections(&versions, symtab, local_dynamic_count,
1803 dynamic_symbols, dynstr);
1805 // Set the size of the _DYNAMIC symbol. We can't do this until
1806 // after we call create_version_sections.
1807 this->set_dynamic_symbol_size(symtab);
1810 if (this->incremental_inputs_)
1812 this->incremental_inputs_->finalize();
1813 this->create_incremental_info_sections();
1816 // Create segment headers.
1817 Output_segment_headers* segment_headers =
1818 (parameters->options().relocatable()
1819 ? NULL
1820 : new Output_segment_headers(this->segment_list_));
1822 // Lay out the file header.
1823 Output_file_header* file_header
1824 = new Output_file_header(target, symtab, segment_headers,
1825 parameters->options().entry());
1827 this->special_output_list_.push_back(file_header);
1828 if (segment_headers != NULL)
1829 this->special_output_list_.push_back(segment_headers);
1831 // Find approriate places for orphan output sections if we are using
1832 // a linker script.
1833 if (this->script_options_->saw_sections_clause())
1834 this->place_orphan_sections_in_script();
1836 Output_segment* load_seg;
1837 off_t off;
1838 unsigned int shndx;
1839 int pass = 0;
1841 // Take a snapshot of the section layout as needed.
1842 if (target->may_relax())
1843 this->prepare_for_relaxation();
1845 // Run the relaxation loop to lay out sections.
1848 off = this->relaxation_loop_body(pass, target, symtab, &load_seg,
1849 phdr_seg, segment_headers, file_header,
1850 &shndx);
1851 pass++;
1853 while (target->may_relax()
1854 && target->relax(pass, input_objects, symtab, this));
1856 // Set the file offsets of all the non-data sections we've seen so
1857 // far which don't have to wait for the input sections. We need
1858 // this in order to finalize local symbols in non-allocated
1859 // sections.
1860 off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS);
1862 // Set the section indexes of all unallocated sections seen so far,
1863 // in case any of them are somehow referenced by a symbol.
1864 shndx = this->set_section_indexes(shndx);
1866 // Create the symbol table sections.
1867 this->create_symtab_sections(input_objects, symtab, shndx, &off);
1868 if (!parameters->doing_static_link())
1869 this->assign_local_dynsym_offsets(input_objects);
1871 // Process any symbol assignments from a linker script. This must
1872 // be called after the symbol table has been finalized.
1873 this->script_options_->finalize_symbols(symtab, this);
1875 // Create the .shstrtab section.
1876 Output_section* shstrtab_section = this->create_shstrtab();
1878 // Set the file offsets of the rest of the non-data sections which
1879 // don't have to wait for the input sections.
1880 off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS);
1882 // Now that all sections have been created, set the section indexes
1883 // for any sections which haven't been done yet.
1884 shndx = this->set_section_indexes(shndx);
1886 // Create the section table header.
1887 this->create_shdrs(shstrtab_section, &off);
1889 // If there are no sections which require postprocessing, we can
1890 // handle the section names now, and avoid a resize later.
1891 if (!this->any_postprocessing_sections_)
1892 off = this->set_section_offsets(off,
1893 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS);
1895 file_header->set_section_info(this->section_headers_, shstrtab_section);
1897 // Now we know exactly where everything goes in the output file
1898 // (except for non-allocated sections which require postprocessing).
1899 Output_data::layout_complete();
1901 this->output_file_size_ = off;
1903 return off;
1906 // Create a note header following the format defined in the ELF ABI.
1907 // NAME is the name, NOTE_TYPE is the type, SECTION_NAME is the name
1908 // of the section to create, DESCSZ is the size of the descriptor.
1909 // ALLOCATE is true if the section should be allocated in memory.
1910 // This returns the new note section. It sets *TRAILING_PADDING to
1911 // the number of trailing zero bytes required.
1913 Output_section*
1914 Layout::create_note(const char* name, int note_type,
1915 const char* section_name, size_t descsz,
1916 bool allocate, size_t* trailing_padding)
1918 // Authorities all agree that the values in a .note field should
1919 // be aligned on 4-byte boundaries for 32-bit binaries. However,
1920 // they differ on what the alignment is for 64-bit binaries.
1921 // The GABI says unambiguously they take 8-byte alignment:
1922 // http://sco.com/developers/gabi/latest/ch5.pheader.html#note_section
1923 // Other documentation says alignment should always be 4 bytes:
1924 // http://www.netbsd.org/docs/kernel/elf-notes.html#note-format
1925 // GNU ld and GNU readelf both support the latter (at least as of
1926 // version 2.16.91), and glibc always generates the latter for
1927 // .note.ABI-tag (as of version 1.6), so that's the one we go with
1928 // here.
1929 #ifdef GABI_FORMAT_FOR_DOTNOTE_SECTION // This is not defined by default.
1930 const int size = parameters->target().get_size();
1931 #else
1932 const int size = 32;
1933 #endif
1935 // The contents of the .note section.
1936 size_t namesz = strlen(name) + 1;
1937 size_t aligned_namesz = align_address(namesz, size / 8);
1938 size_t aligned_descsz = align_address(descsz, size / 8);
1940 size_t notehdrsz = 3 * (size / 8) + aligned_namesz;
1942 unsigned char* buffer = new unsigned char[notehdrsz];
1943 memset(buffer, 0, notehdrsz);
1945 bool is_big_endian = parameters->target().is_big_endian();
1947 if (size == 32)
1949 if (!is_big_endian)
1951 elfcpp::Swap<32, false>::writeval(buffer, namesz);
1952 elfcpp::Swap<32, false>::writeval(buffer + 4, descsz);
1953 elfcpp::Swap<32, false>::writeval(buffer + 8, note_type);
1955 else
1957 elfcpp::Swap<32, true>::writeval(buffer, namesz);
1958 elfcpp::Swap<32, true>::writeval(buffer + 4, descsz);
1959 elfcpp::Swap<32, true>::writeval(buffer + 8, note_type);
1962 else if (size == 64)
1964 if (!is_big_endian)
1966 elfcpp::Swap<64, false>::writeval(buffer, namesz);
1967 elfcpp::Swap<64, false>::writeval(buffer + 8, descsz);
1968 elfcpp::Swap<64, false>::writeval(buffer + 16, note_type);
1970 else
1972 elfcpp::Swap<64, true>::writeval(buffer, namesz);
1973 elfcpp::Swap<64, true>::writeval(buffer + 8, descsz);
1974 elfcpp::Swap<64, true>::writeval(buffer + 16, note_type);
1977 else
1978 gold_unreachable();
1980 memcpy(buffer + 3 * (size / 8), name, namesz);
1982 elfcpp::Elf_Xword flags = 0;
1983 if (allocate)
1984 flags = elfcpp::SHF_ALLOC;
1985 Output_section* os = this->choose_output_section(NULL, section_name,
1986 elfcpp::SHT_NOTE,
1987 flags, false, false,
1988 false, false, false, false);
1989 if (os == NULL)
1990 return NULL;
1992 Output_section_data* posd = new Output_data_const_buffer(buffer, notehdrsz,
1993 size / 8,
1994 "** note header");
1995 os->add_output_section_data(posd);
1997 *trailing_padding = aligned_descsz - descsz;
1999 return os;
2002 // For an executable or shared library, create a note to record the
2003 // version of gold used to create the binary.
2005 void
2006 Layout::create_gold_note()
2008 if (parameters->options().relocatable())
2009 return;
2011 std::string desc = std::string("gold ") + gold::get_version_string();
2013 size_t trailing_padding;
2014 Output_section *os = this->create_note("GNU", elfcpp::NT_GNU_GOLD_VERSION,
2015 ".note.gnu.gold-version", desc.size(),
2016 false, &trailing_padding);
2017 if (os == NULL)
2018 return;
2020 Output_section_data* posd = new Output_data_const(desc, 4);
2021 os->add_output_section_data(posd);
2023 if (trailing_padding > 0)
2025 posd = new Output_data_zero_fill(trailing_padding, 0);
2026 os->add_output_section_data(posd);
2030 // Record whether the stack should be executable. This can be set
2031 // from the command line using the -z execstack or -z noexecstack
2032 // options. Otherwise, if any input file has a .note.GNU-stack
2033 // section with the SHF_EXECINSTR flag set, the stack should be
2034 // executable. Otherwise, if at least one input file a
2035 // .note.GNU-stack section, and some input file has no .note.GNU-stack
2036 // section, we use the target default for whether the stack should be
2037 // executable. Otherwise, we don't generate a stack note. When
2038 // generating a object file, we create a .note.GNU-stack section with
2039 // the appropriate marking. When generating an executable or shared
2040 // library, we create a PT_GNU_STACK segment.
2042 void
2043 Layout::create_executable_stack_info()
2045 bool is_stack_executable;
2046 if (parameters->options().is_execstack_set())
2047 is_stack_executable = parameters->options().is_stack_executable();
2048 else if (!this->input_with_gnu_stack_note_)
2049 return;
2050 else
2052 if (this->input_requires_executable_stack_)
2053 is_stack_executable = true;
2054 else if (this->input_without_gnu_stack_note_)
2055 is_stack_executable =
2056 parameters->target().is_default_stack_executable();
2057 else
2058 is_stack_executable = false;
2061 if (parameters->options().relocatable())
2063 const char* name = this->namepool_.add(".note.GNU-stack", false, NULL);
2064 elfcpp::Elf_Xword flags = 0;
2065 if (is_stack_executable)
2066 flags |= elfcpp::SHF_EXECINSTR;
2067 this->make_output_section(name, elfcpp::SHT_PROGBITS, flags, false,
2068 false, false, false, false);
2070 else
2072 if (this->script_options_->saw_phdrs_clause())
2073 return;
2074 int flags = elfcpp::PF_R | elfcpp::PF_W;
2075 if (is_stack_executable)
2076 flags |= elfcpp::PF_X;
2077 this->make_output_segment(elfcpp::PT_GNU_STACK, flags);
2081 // If --build-id was used, set up the build ID note.
2083 void
2084 Layout::create_build_id()
2086 if (!parameters->options().user_set_build_id())
2087 return;
2089 const char* style = parameters->options().build_id();
2090 if (strcmp(style, "none") == 0)
2091 return;
2093 // Set DESCSZ to the size of the note descriptor. When possible,
2094 // set DESC to the note descriptor contents.
2095 size_t descsz;
2096 std::string desc;
2097 if (strcmp(style, "md5") == 0)
2098 descsz = 128 / 8;
2099 else if (strcmp(style, "sha1") == 0)
2100 descsz = 160 / 8;
2101 else if (strcmp(style, "uuid") == 0)
2103 const size_t uuidsz = 128 / 8;
2105 char buffer[uuidsz];
2106 memset(buffer, 0, uuidsz);
2108 int descriptor = open_descriptor(-1, "/dev/urandom", O_RDONLY);
2109 if (descriptor < 0)
2110 gold_error(_("--build-id=uuid failed: could not open /dev/urandom: %s"),
2111 strerror(errno));
2112 else
2114 ssize_t got = ::read(descriptor, buffer, uuidsz);
2115 release_descriptor(descriptor, true);
2116 if (got < 0)
2117 gold_error(_("/dev/urandom: read failed: %s"), strerror(errno));
2118 else if (static_cast<size_t>(got) != uuidsz)
2119 gold_error(_("/dev/urandom: expected %zu bytes, got %zd bytes"),
2120 uuidsz, got);
2123 desc.assign(buffer, uuidsz);
2124 descsz = uuidsz;
2126 else if (strncmp(style, "0x", 2) == 0)
2128 hex_init();
2129 const char* p = style + 2;
2130 while (*p != '\0')
2132 if (hex_p(p[0]) && hex_p(p[1]))
2134 char c = (hex_value(p[0]) << 4) | hex_value(p[1]);
2135 desc += c;
2136 p += 2;
2138 else if (*p == '-' || *p == ':')
2139 ++p;
2140 else
2141 gold_fatal(_("--build-id argument '%s' not a valid hex number"),
2142 style);
2144 descsz = desc.size();
2146 else
2147 gold_fatal(_("unrecognized --build-id argument '%s'"), style);
2149 // Create the note.
2150 size_t trailing_padding;
2151 Output_section* os = this->create_note("GNU", elfcpp::NT_GNU_BUILD_ID,
2152 ".note.gnu.build-id", descsz, true,
2153 &trailing_padding);
2154 if (os == NULL)
2155 return;
2157 if (!desc.empty())
2159 // We know the value already, so we fill it in now.
2160 gold_assert(desc.size() == descsz);
2162 Output_section_data* posd = new Output_data_const(desc, 4);
2163 os->add_output_section_data(posd);
2165 if (trailing_padding != 0)
2167 posd = new Output_data_zero_fill(trailing_padding, 0);
2168 os->add_output_section_data(posd);
2171 else
2173 // We need to compute a checksum after we have completed the
2174 // link.
2175 gold_assert(trailing_padding == 0);
2176 this->build_id_note_ = new Output_data_zero_fill(descsz, 4);
2177 os->add_output_section_data(this->build_id_note_);
2181 // If we have both .stabXX and .stabXXstr sections, then the sh_link
2182 // field of the former should point to the latter. I'm not sure who
2183 // started this, but the GNU linker does it, and some tools depend
2184 // upon it.
2186 void
2187 Layout::link_stabs_sections()
2189 if (!this->have_stabstr_section_)
2190 return;
2192 for (Section_list::iterator p = this->section_list_.begin();
2193 p != this->section_list_.end();
2194 ++p)
2196 if ((*p)->type() != elfcpp::SHT_STRTAB)
2197 continue;
2199 const char* name = (*p)->name();
2200 if (strncmp(name, ".stab", 5) != 0)
2201 continue;
2203 size_t len = strlen(name);
2204 if (strcmp(name + len - 3, "str") != 0)
2205 continue;
2207 std::string stab_name(name, len - 3);
2208 Output_section* stab_sec;
2209 stab_sec = this->find_output_section(stab_name.c_str());
2210 if (stab_sec != NULL)
2211 stab_sec->set_link_section(*p);
2215 // Create .gnu_incremental_inputs and .gnu_incremental_strtab sections needed
2216 // for the next run of incremental linking to check what has changed.
2218 void
2219 Layout::create_incremental_info_sections()
2221 gold_assert(this->incremental_inputs_ != NULL);
2223 // Add the .gnu_incremental_inputs section.
2224 const char *incremental_inputs_name =
2225 this->namepool_.add(".gnu_incremental_inputs", false, NULL);
2226 Output_section* inputs_os =
2227 this->make_output_section(incremental_inputs_name,
2228 elfcpp::SHT_GNU_INCREMENTAL_INPUTS, 0,
2229 false, false, false, false, false);
2230 Output_section_data* posd =
2231 this->incremental_inputs_->create_incremental_inputs_section_data();
2232 inputs_os->add_output_section_data(posd);
2234 // Add the .gnu_incremental_strtab section.
2235 const char *incremental_strtab_name =
2236 this->namepool_.add(".gnu_incremental_strtab", false, NULL);
2237 Output_section* strtab_os = this->make_output_section(incremental_strtab_name,
2238 elfcpp::SHT_STRTAB,
2239 0, false, false,
2240 false, false, false);
2241 Output_data_strtab* strtab_data =
2242 new Output_data_strtab(this->incremental_inputs_->get_stringpool());
2243 strtab_os->add_output_section_data(strtab_data);
2245 inputs_os->set_link_section(strtab_data);
2248 // Return whether SEG1 should be before SEG2 in the output file. This
2249 // is based entirely on the segment type and flags. When this is
2250 // called the segment addresses has normally not yet been set.
2252 bool
2253 Layout::segment_precedes(const Output_segment* seg1,
2254 const Output_segment* seg2)
2256 elfcpp::Elf_Word type1 = seg1->type();
2257 elfcpp::Elf_Word type2 = seg2->type();
2259 // The single PT_PHDR segment is required to precede any loadable
2260 // segment. We simply make it always first.
2261 if (type1 == elfcpp::PT_PHDR)
2263 gold_assert(type2 != elfcpp::PT_PHDR);
2264 return true;
2266 if (type2 == elfcpp::PT_PHDR)
2267 return false;
2269 // The single PT_INTERP segment is required to precede any loadable
2270 // segment. We simply make it always second.
2271 if (type1 == elfcpp::PT_INTERP)
2273 gold_assert(type2 != elfcpp::PT_INTERP);
2274 return true;
2276 if (type2 == elfcpp::PT_INTERP)
2277 return false;
2279 // We then put PT_LOAD segments before any other segments.
2280 if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD)
2281 return true;
2282 if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD)
2283 return false;
2285 // We put the PT_TLS segment last except for the PT_GNU_RELRO
2286 // segment, because that is where the dynamic linker expects to find
2287 // it (this is just for efficiency; other positions would also work
2288 // correctly).
2289 if (type1 == elfcpp::PT_TLS
2290 && type2 != elfcpp::PT_TLS
2291 && type2 != elfcpp::PT_GNU_RELRO)
2292 return false;
2293 if (type2 == elfcpp::PT_TLS
2294 && type1 != elfcpp::PT_TLS
2295 && type1 != elfcpp::PT_GNU_RELRO)
2296 return true;
2298 // We put the PT_GNU_RELRO segment last, because that is where the
2299 // dynamic linker expects to find it (as with PT_TLS, this is just
2300 // for efficiency).
2301 if (type1 == elfcpp::PT_GNU_RELRO && type2 != elfcpp::PT_GNU_RELRO)
2302 return false;
2303 if (type2 == elfcpp::PT_GNU_RELRO && type1 != elfcpp::PT_GNU_RELRO)
2304 return true;
2306 const elfcpp::Elf_Word flags1 = seg1->flags();
2307 const elfcpp::Elf_Word flags2 = seg2->flags();
2309 // The order of non-PT_LOAD segments is unimportant. We simply sort
2310 // by the numeric segment type and flags values. There should not
2311 // be more than one segment with the same type and flags.
2312 if (type1 != elfcpp::PT_LOAD)
2314 if (type1 != type2)
2315 return type1 < type2;
2316 gold_assert(flags1 != flags2);
2317 return flags1 < flags2;
2320 // If the addresses are set already, sort by load address.
2321 if (seg1->are_addresses_set())
2323 if (!seg2->are_addresses_set())
2324 return true;
2326 unsigned int section_count1 = seg1->output_section_count();
2327 unsigned int section_count2 = seg2->output_section_count();
2328 if (section_count1 == 0 && section_count2 > 0)
2329 return true;
2330 if (section_count1 > 0 && section_count2 == 0)
2331 return false;
2333 uint64_t paddr1 = seg1->first_section_load_address();
2334 uint64_t paddr2 = seg2->first_section_load_address();
2335 if (paddr1 != paddr2)
2336 return paddr1 < paddr2;
2338 else if (seg2->are_addresses_set())
2339 return false;
2341 // A segment which holds large data comes after a segment which does
2342 // not hold large data.
2343 if (seg1->is_large_data_segment())
2345 if (!seg2->is_large_data_segment())
2346 return false;
2348 else if (seg2->is_large_data_segment())
2349 return true;
2351 // Otherwise, we sort PT_LOAD segments based on the flags. Readonly
2352 // segments come before writable segments. Then writable segments
2353 // with data come before writable segments without data. Then
2354 // executable segments come before non-executable segments. Then
2355 // the unlikely case of a non-readable segment comes before the
2356 // normal case of a readable segment. If there are multiple
2357 // segments with the same type and flags, we require that the
2358 // address be set, and we sort by virtual address and then physical
2359 // address.
2360 if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W))
2361 return (flags1 & elfcpp::PF_W) == 0;
2362 if ((flags1 & elfcpp::PF_W) != 0
2363 && seg1->has_any_data_sections() != seg2->has_any_data_sections())
2364 return seg1->has_any_data_sections();
2365 if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X))
2366 return (flags1 & elfcpp::PF_X) != 0;
2367 if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R))
2368 return (flags1 & elfcpp::PF_R) == 0;
2370 // We shouldn't get here--we shouldn't create segments which we
2371 // can't distinguish.
2372 gold_unreachable();
2375 // Increase OFF so that it is congruent to ADDR modulo ABI_PAGESIZE.
2377 static off_t
2378 align_file_offset(off_t off, uint64_t addr, uint64_t abi_pagesize)
2380 uint64_t unsigned_off = off;
2381 uint64_t aligned_off = ((unsigned_off & ~(abi_pagesize - 1))
2382 | (addr & (abi_pagesize - 1)));
2383 if (aligned_off < unsigned_off)
2384 aligned_off += abi_pagesize;
2385 return aligned_off;
2388 // Set the file offsets of all the segments, and all the sections they
2389 // contain. They have all been created. LOAD_SEG must be be laid out
2390 // first. Return the offset of the data to follow.
2392 off_t
2393 Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
2394 unsigned int *pshndx)
2396 // Sort them into the final order.
2397 std::sort(this->segment_list_.begin(), this->segment_list_.end(),
2398 Layout::Compare_segments());
2400 // Find the PT_LOAD segments, and set their addresses and offsets
2401 // and their section's addresses and offsets.
2402 uint64_t addr;
2403 if (parameters->options().user_set_Ttext())
2404 addr = parameters->options().Ttext();
2405 else if (parameters->options().output_is_position_independent())
2406 addr = 0;
2407 else
2408 addr = target->default_text_segment_address();
2409 off_t off = 0;
2411 // If LOAD_SEG is NULL, then the file header and segment headers
2412 // will not be loadable. But they still need to be at offset 0 in
2413 // the file. Set their offsets now.
2414 if (load_seg == NULL)
2416 for (Data_list::iterator p = this->special_output_list_.begin();
2417 p != this->special_output_list_.end();
2418 ++p)
2420 off = align_address(off, (*p)->addralign());
2421 (*p)->set_address_and_file_offset(0, off);
2422 off += (*p)->data_size();
2426 unsigned int increase_relro = this->increase_relro_;
2427 if (this->script_options_->saw_sections_clause())
2428 increase_relro = 0;
2430 const bool check_sections = parameters->options().check_sections();
2431 Output_segment* last_load_segment = NULL;
2433 bool was_readonly = false;
2434 for (Segment_list::iterator p = this->segment_list_.begin();
2435 p != this->segment_list_.end();
2436 ++p)
2438 if ((*p)->type() == elfcpp::PT_LOAD)
2440 if (load_seg != NULL && load_seg != *p)
2441 gold_unreachable();
2442 load_seg = NULL;
2444 bool are_addresses_set = (*p)->are_addresses_set();
2445 if (are_addresses_set)
2447 // When it comes to setting file offsets, we care about
2448 // the physical address.
2449 addr = (*p)->paddr();
2451 else if (parameters->options().user_set_Tdata()
2452 && ((*p)->flags() & elfcpp::PF_W) != 0
2453 && (!parameters->options().user_set_Tbss()
2454 || (*p)->has_any_data_sections()))
2456 addr = parameters->options().Tdata();
2457 are_addresses_set = true;
2459 else if (parameters->options().user_set_Tbss()
2460 && ((*p)->flags() & elfcpp::PF_W) != 0
2461 && !(*p)->has_any_data_sections())
2463 addr = parameters->options().Tbss();
2464 are_addresses_set = true;
2467 uint64_t orig_addr = addr;
2468 uint64_t orig_off = off;
2470 uint64_t aligned_addr = 0;
2471 uint64_t abi_pagesize = target->abi_pagesize();
2472 uint64_t common_pagesize = target->common_pagesize();
2474 if (!parameters->options().nmagic()
2475 && !parameters->options().omagic())
2476 (*p)->set_minimum_p_align(common_pagesize);
2478 if (!are_addresses_set)
2480 // If the last segment was readonly, and this one is
2481 // not, then skip the address forward one page,
2482 // maintaining the same position within the page. This
2483 // lets us store both segments overlapping on a single
2484 // page in the file, but the loader will put them on
2485 // different pages in memory.
2487 addr = align_address(addr, (*p)->maximum_alignment());
2488 aligned_addr = addr;
2490 if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0)
2492 if ((addr & (abi_pagesize - 1)) != 0)
2493 addr = addr + abi_pagesize;
2496 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
2499 if (!parameters->options().nmagic()
2500 && !parameters->options().omagic())
2501 off = align_file_offset(off, addr, abi_pagesize);
2502 else if (load_seg == NULL)
2504 // This is -N or -n with a section script which prevents
2505 // us from using a load segment. We need to ensure that
2506 // the file offset is aligned to the alignment of the
2507 // segment. This is because the linker script
2508 // implicitly assumed a zero offset. If we don't align
2509 // here, then the alignment of the sections in the
2510 // linker script may not match the alignment of the
2511 // sections in the set_section_addresses call below,
2512 // causing an error about dot moving backward.
2513 off = align_address(off, (*p)->maximum_alignment());
2516 unsigned int shndx_hold = *pshndx;
2517 uint64_t new_addr = (*p)->set_section_addresses(this, false, addr,
2518 increase_relro,
2519 &off, pshndx);
2521 // Now that we know the size of this segment, we may be able
2522 // to save a page in memory, at the cost of wasting some
2523 // file space, by instead aligning to the start of a new
2524 // page. Here we use the real machine page size rather than
2525 // the ABI mandated page size.
2527 if (!are_addresses_set && aligned_addr != addr)
2529 uint64_t first_off = (common_pagesize
2530 - (aligned_addr
2531 & (common_pagesize - 1)));
2532 uint64_t last_off = new_addr & (common_pagesize - 1);
2533 if (first_off > 0
2534 && last_off > 0
2535 && ((aligned_addr & ~ (common_pagesize - 1))
2536 != (new_addr & ~ (common_pagesize - 1)))
2537 && first_off + last_off <= common_pagesize)
2539 *pshndx = shndx_hold;
2540 addr = align_address(aligned_addr, common_pagesize);
2541 addr = align_address(addr, (*p)->maximum_alignment());
2542 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
2543 off = align_file_offset(off, addr, abi_pagesize);
2544 new_addr = (*p)->set_section_addresses(this, true, addr,
2545 increase_relro,
2546 &off, pshndx);
2550 addr = new_addr;
2552 if (((*p)->flags() & elfcpp::PF_W) == 0)
2553 was_readonly = true;
2555 // Implement --check-sections. We know that the segments
2556 // are sorted by LMA.
2557 if (check_sections && last_load_segment != NULL)
2559 gold_assert(last_load_segment->paddr() <= (*p)->paddr());
2560 if (last_load_segment->paddr() + last_load_segment->memsz()
2561 > (*p)->paddr())
2563 unsigned long long lb1 = last_load_segment->paddr();
2564 unsigned long long le1 = lb1 + last_load_segment->memsz();
2565 unsigned long long lb2 = (*p)->paddr();
2566 unsigned long long le2 = lb2 + (*p)->memsz();
2567 gold_error(_("load segment overlap [0x%llx -> 0x%llx] and "
2568 "[0x%llx -> 0x%llx]"),
2569 lb1, le1, lb2, le2);
2572 last_load_segment = *p;
2576 // Handle the non-PT_LOAD segments, setting their offsets from their
2577 // section's offsets.
2578 for (Segment_list::iterator p = this->segment_list_.begin();
2579 p != this->segment_list_.end();
2580 ++p)
2582 if ((*p)->type() != elfcpp::PT_LOAD)
2583 (*p)->set_offset((*p)->type() == elfcpp::PT_GNU_RELRO
2584 ? increase_relro
2585 : 0);
2588 // Set the TLS offsets for each section in the PT_TLS segment.
2589 if (this->tls_segment_ != NULL)
2590 this->tls_segment_->set_tls_offsets();
2592 return off;
2595 // Set the offsets of all the allocated sections when doing a
2596 // relocatable link. This does the same jobs as set_segment_offsets,
2597 // only for a relocatable link.
2599 off_t
2600 Layout::set_relocatable_section_offsets(Output_data* file_header,
2601 unsigned int *pshndx)
2603 off_t off = 0;
2605 file_header->set_address_and_file_offset(0, 0);
2606 off += file_header->data_size();
2608 for (Section_list::iterator p = this->section_list_.begin();
2609 p != this->section_list_.end();
2610 ++p)
2612 // We skip unallocated sections here, except that group sections
2613 // have to come first.
2614 if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
2615 && (*p)->type() != elfcpp::SHT_GROUP)
2616 continue;
2618 off = align_address(off, (*p)->addralign());
2620 // The linker script might have set the address.
2621 if (!(*p)->is_address_valid())
2622 (*p)->set_address(0);
2623 (*p)->set_file_offset(off);
2624 (*p)->finalize_data_size();
2625 off += (*p)->data_size();
2627 (*p)->set_out_shndx(*pshndx);
2628 ++*pshndx;
2631 return off;
2634 // Set the file offset of all the sections not associated with a
2635 // segment.
2637 off_t
2638 Layout::set_section_offsets(off_t off, Layout::Section_offset_pass pass)
2640 for (Section_list::iterator p = this->unattached_section_list_.begin();
2641 p != this->unattached_section_list_.end();
2642 ++p)
2644 // The symtab section is handled in create_symtab_sections.
2645 if (*p == this->symtab_section_)
2646 continue;
2648 // If we've already set the data size, don't set it again.
2649 if ((*p)->is_offset_valid() && (*p)->is_data_size_valid())
2650 continue;
2652 if (pass == BEFORE_INPUT_SECTIONS_PASS
2653 && (*p)->requires_postprocessing())
2655 (*p)->create_postprocessing_buffer();
2656 this->any_postprocessing_sections_ = true;
2659 if (pass == BEFORE_INPUT_SECTIONS_PASS
2660 && (*p)->after_input_sections())
2661 continue;
2662 else if (pass == POSTPROCESSING_SECTIONS_PASS
2663 && (!(*p)->after_input_sections()
2664 || (*p)->type() == elfcpp::SHT_STRTAB))
2665 continue;
2666 else if (pass == STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
2667 && (!(*p)->after_input_sections()
2668 || (*p)->type() != elfcpp::SHT_STRTAB))
2669 continue;
2671 off = align_address(off, (*p)->addralign());
2672 (*p)->set_file_offset(off);
2673 (*p)->finalize_data_size();
2674 off += (*p)->data_size();
2676 // At this point the name must be set.
2677 if (pass != STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS)
2678 this->namepool_.add((*p)->name(), false, NULL);
2680 return off;
2683 // Set the section indexes of all the sections not associated with a
2684 // segment.
2686 unsigned int
2687 Layout::set_section_indexes(unsigned int shndx)
2689 for (Section_list::iterator p = this->unattached_section_list_.begin();
2690 p != this->unattached_section_list_.end();
2691 ++p)
2693 if (!(*p)->has_out_shndx())
2695 (*p)->set_out_shndx(shndx);
2696 ++shndx;
2699 return shndx;
2702 // Set the section addresses according to the linker script. This is
2703 // only called when we see a SECTIONS clause. This returns the
2704 // program segment which should hold the file header and segment
2705 // headers, if any. It will return NULL if they should not be in a
2706 // segment.
2708 Output_segment*
2709 Layout::set_section_addresses_from_script(Symbol_table* symtab)
2711 Script_sections* ss = this->script_options_->script_sections();
2712 gold_assert(ss->saw_sections_clause());
2713 return this->script_options_->set_section_addresses(symtab, this);
2716 // Place the orphan sections in the linker script.
2718 void
2719 Layout::place_orphan_sections_in_script()
2721 Script_sections* ss = this->script_options_->script_sections();
2722 gold_assert(ss->saw_sections_clause());
2724 // Place each orphaned output section in the script.
2725 for (Section_list::iterator p = this->section_list_.begin();
2726 p != this->section_list_.end();
2727 ++p)
2729 if (!(*p)->found_in_sections_clause())
2730 ss->place_orphan(*p);
2734 // Count the local symbols in the regular symbol table and the dynamic
2735 // symbol table, and build the respective string pools.
2737 void
2738 Layout::count_local_symbols(const Task* task,
2739 const Input_objects* input_objects)
2741 // First, figure out an upper bound on the number of symbols we'll
2742 // be inserting into each pool. This helps us create the pools with
2743 // the right size, to avoid unnecessary hashtable resizing.
2744 unsigned int symbol_count = 0;
2745 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
2746 p != input_objects->relobj_end();
2747 ++p)
2748 symbol_count += (*p)->local_symbol_count();
2750 // Go from "upper bound" to "estimate." We overcount for two
2751 // reasons: we double-count symbols that occur in more than one
2752 // object file, and we count symbols that are dropped from the
2753 // output. Add it all together and assume we overcount by 100%.
2754 symbol_count /= 2;
2756 // We assume all symbols will go into both the sympool and dynpool.
2757 this->sympool_.reserve(symbol_count);
2758 this->dynpool_.reserve(symbol_count);
2760 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
2761 p != input_objects->relobj_end();
2762 ++p)
2764 Task_lock_obj<Object> tlo(task, *p);
2765 (*p)->count_local_symbols(&this->sympool_, &this->dynpool_);
2769 // Create the symbol table sections. Here we also set the final
2770 // values of the symbols. At this point all the loadable sections are
2771 // fully laid out. SHNUM is the number of sections so far.
2773 void
2774 Layout::create_symtab_sections(const Input_objects* input_objects,
2775 Symbol_table* symtab,
2776 unsigned int shnum,
2777 off_t* poff)
2779 int symsize;
2780 unsigned int align;
2781 if (parameters->target().get_size() == 32)
2783 symsize = elfcpp::Elf_sizes<32>::sym_size;
2784 align = 4;
2786 else if (parameters->target().get_size() == 64)
2788 symsize = elfcpp::Elf_sizes<64>::sym_size;
2789 align = 8;
2791 else
2792 gold_unreachable();
2794 off_t off = *poff;
2795 off = align_address(off, align);
2796 off_t startoff = off;
2798 // Save space for the dummy symbol at the start of the section. We
2799 // never bother to write this out--it will just be left as zero.
2800 off += symsize;
2801 unsigned int local_symbol_index = 1;
2803 // Add STT_SECTION symbols for each Output section which needs one.
2804 for (Section_list::iterator p = this->section_list_.begin();
2805 p != this->section_list_.end();
2806 ++p)
2808 if (!(*p)->needs_symtab_index())
2809 (*p)->set_symtab_index(-1U);
2810 else
2812 (*p)->set_symtab_index(local_symbol_index);
2813 ++local_symbol_index;
2814 off += symsize;
2818 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
2819 p != input_objects->relobj_end();
2820 ++p)
2822 unsigned int index = (*p)->finalize_local_symbols(local_symbol_index,
2823 off, symtab);
2824 off += (index - local_symbol_index) * symsize;
2825 local_symbol_index = index;
2828 unsigned int local_symcount = local_symbol_index;
2829 gold_assert(static_cast<off_t>(local_symcount * symsize) == off - startoff);
2831 off_t dynoff;
2832 size_t dyn_global_index;
2833 size_t dyncount;
2834 if (this->dynsym_section_ == NULL)
2836 dynoff = 0;
2837 dyn_global_index = 0;
2838 dyncount = 0;
2840 else
2842 dyn_global_index = this->dynsym_section_->info();
2843 off_t locsize = dyn_global_index * this->dynsym_section_->entsize();
2844 dynoff = this->dynsym_section_->offset() + locsize;
2845 dyncount = (this->dynsym_section_->data_size() - locsize) / symsize;
2846 gold_assert(static_cast<off_t>(dyncount * symsize)
2847 == this->dynsym_section_->data_size() - locsize);
2850 off = symtab->finalize(off, dynoff, dyn_global_index, dyncount,
2851 &this->sympool_, &local_symcount);
2853 if (!parameters->options().strip_all())
2855 this->sympool_.set_string_offsets();
2857 const char* symtab_name = this->namepool_.add(".symtab", false, NULL);
2858 Output_section* osymtab = this->make_output_section(symtab_name,
2859 elfcpp::SHT_SYMTAB,
2860 0, false, false,
2861 false, false, false);
2862 this->symtab_section_ = osymtab;
2864 Output_section_data* pos = new Output_data_fixed_space(off - startoff,
2865 align,
2866 "** symtab");
2867 osymtab->add_output_section_data(pos);
2869 // We generate a .symtab_shndx section if we have more than
2870 // SHN_LORESERVE sections. Technically it is possible that we
2871 // don't need one, because it is possible that there are no
2872 // symbols in any of sections with indexes larger than
2873 // SHN_LORESERVE. That is probably unusual, though, and it is
2874 // easier to always create one than to compute section indexes
2875 // twice (once here, once when writing out the symbols).
2876 if (shnum >= elfcpp::SHN_LORESERVE)
2878 const char* symtab_xindex_name = this->namepool_.add(".symtab_shndx",
2879 false, NULL);
2880 Output_section* osymtab_xindex =
2881 this->make_output_section(symtab_xindex_name,
2882 elfcpp::SHT_SYMTAB_SHNDX, 0, false,
2883 false, false, false, false);
2885 size_t symcount = (off - startoff) / symsize;
2886 this->symtab_xindex_ = new Output_symtab_xindex(symcount);
2888 osymtab_xindex->add_output_section_data(this->symtab_xindex_);
2890 osymtab_xindex->set_link_section(osymtab);
2891 osymtab_xindex->set_addralign(4);
2892 osymtab_xindex->set_entsize(4);
2894 osymtab_xindex->set_after_input_sections();
2896 // This tells the driver code to wait until the symbol table
2897 // has written out before writing out the postprocessing
2898 // sections, including the .symtab_shndx section.
2899 this->any_postprocessing_sections_ = true;
2902 const char* strtab_name = this->namepool_.add(".strtab", false, NULL);
2903 Output_section* ostrtab = this->make_output_section(strtab_name,
2904 elfcpp::SHT_STRTAB,
2905 0, false, false,
2906 false, false, false);
2908 Output_section_data* pstr = new Output_data_strtab(&this->sympool_);
2909 ostrtab->add_output_section_data(pstr);
2911 osymtab->set_file_offset(startoff);
2912 osymtab->finalize_data_size();
2913 osymtab->set_link_section(ostrtab);
2914 osymtab->set_info(local_symcount);
2915 osymtab->set_entsize(symsize);
2917 *poff = off;
2921 // Create the .shstrtab section, which holds the names of the
2922 // sections. At the time this is called, we have created all the
2923 // output sections except .shstrtab itself.
2925 Output_section*
2926 Layout::create_shstrtab()
2928 // FIXME: We don't need to create a .shstrtab section if we are
2929 // stripping everything.
2931 const char* name = this->namepool_.add(".shstrtab", false, NULL);
2933 Output_section* os = this->make_output_section(name, elfcpp::SHT_STRTAB, 0,
2934 false, false, false, false,
2935 false);
2937 if (strcmp(parameters->options().compress_debug_sections(), "none") != 0)
2939 // We can't write out this section until we've set all the
2940 // section names, and we don't set the names of compressed
2941 // output sections until relocations are complete. FIXME: With
2942 // the current names we use, this is unnecessary.
2943 os->set_after_input_sections();
2946 Output_section_data* posd = new Output_data_strtab(&this->namepool_);
2947 os->add_output_section_data(posd);
2949 return os;
2952 // Create the section headers. SIZE is 32 or 64. OFF is the file
2953 // offset.
2955 void
2956 Layout::create_shdrs(const Output_section* shstrtab_section, off_t* poff)
2958 Output_section_headers* oshdrs;
2959 oshdrs = new Output_section_headers(this,
2960 &this->segment_list_,
2961 &this->section_list_,
2962 &this->unattached_section_list_,
2963 &this->namepool_,
2964 shstrtab_section);
2965 off_t off = align_address(*poff, oshdrs->addralign());
2966 oshdrs->set_address_and_file_offset(0, off);
2967 off += oshdrs->data_size();
2968 *poff = off;
2969 this->section_headers_ = oshdrs;
2972 // Count the allocated sections.
2974 size_t
2975 Layout::allocated_output_section_count() const
2977 size_t section_count = 0;
2978 for (Segment_list::const_iterator p = this->segment_list_.begin();
2979 p != this->segment_list_.end();
2980 ++p)
2981 section_count += (*p)->output_section_count();
2982 return section_count;
2985 // Create the dynamic symbol table.
2987 void
2988 Layout::create_dynamic_symtab(const Input_objects* input_objects,
2989 Symbol_table* symtab,
2990 Output_section **pdynstr,
2991 unsigned int* plocal_dynamic_count,
2992 std::vector<Symbol*>* pdynamic_symbols,
2993 Versions* pversions)
2995 // Count all the symbols in the dynamic symbol table, and set the
2996 // dynamic symbol indexes.
2998 // Skip symbol 0, which is always all zeroes.
2999 unsigned int index = 1;
3001 // Add STT_SECTION symbols for each Output section which needs one.
3002 for (Section_list::iterator p = this->section_list_.begin();
3003 p != this->section_list_.end();
3004 ++p)
3006 if (!(*p)->needs_dynsym_index())
3007 (*p)->set_dynsym_index(-1U);
3008 else
3010 (*p)->set_dynsym_index(index);
3011 ++index;
3015 // Count the local symbols that need to go in the dynamic symbol table,
3016 // and set the dynamic symbol indexes.
3017 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
3018 p != input_objects->relobj_end();
3019 ++p)
3021 unsigned int new_index = (*p)->set_local_dynsym_indexes(index);
3022 index = new_index;
3025 unsigned int local_symcount = index;
3026 *plocal_dynamic_count = local_symcount;
3028 index = symtab->set_dynsym_indexes(index, pdynamic_symbols,
3029 &this->dynpool_, pversions);
3031 int symsize;
3032 unsigned int align;
3033 const int size = parameters->target().get_size();
3034 if (size == 32)
3036 symsize = elfcpp::Elf_sizes<32>::sym_size;
3037 align = 4;
3039 else if (size == 64)
3041 symsize = elfcpp::Elf_sizes<64>::sym_size;
3042 align = 8;
3044 else
3045 gold_unreachable();
3047 // Create the dynamic symbol table section.
3049 Output_section* dynsym = this->choose_output_section(NULL, ".dynsym",
3050 elfcpp::SHT_DYNSYM,
3051 elfcpp::SHF_ALLOC,
3052 false, false, true,
3053 false, false, false);
3055 Output_section_data* odata = new Output_data_fixed_space(index * symsize,
3056 align,
3057 "** dynsym");
3058 dynsym->add_output_section_data(odata);
3060 dynsym->set_info(local_symcount);
3061 dynsym->set_entsize(symsize);
3062 dynsym->set_addralign(align);
3064 this->dynsym_section_ = dynsym;
3066 Output_data_dynamic* const odyn = this->dynamic_data_;
3067 odyn->add_section_address(elfcpp::DT_SYMTAB, dynsym);
3068 odyn->add_constant(elfcpp::DT_SYMENT, symsize);
3070 // If there are more than SHN_LORESERVE allocated sections, we
3071 // create a .dynsym_shndx section. It is possible that we don't
3072 // need one, because it is possible that there are no dynamic
3073 // symbols in any of the sections with indexes larger than
3074 // SHN_LORESERVE. This is probably unusual, though, and at this
3075 // time we don't know the actual section indexes so it is
3076 // inconvenient to check.
3077 if (this->allocated_output_section_count() >= elfcpp::SHN_LORESERVE)
3079 Output_section* dynsym_xindex =
3080 this->choose_output_section(NULL, ".dynsym_shndx",
3081 elfcpp::SHT_SYMTAB_SHNDX,
3082 elfcpp::SHF_ALLOC,
3083 false, false, true, false, false, false);
3085 this->dynsym_xindex_ = new Output_symtab_xindex(index);
3087 dynsym_xindex->add_output_section_data(this->dynsym_xindex_);
3089 dynsym_xindex->set_link_section(dynsym);
3090 dynsym_xindex->set_addralign(4);
3091 dynsym_xindex->set_entsize(4);
3093 dynsym_xindex->set_after_input_sections();
3095 // This tells the driver code to wait until the symbol table has
3096 // written out before writing out the postprocessing sections,
3097 // including the .dynsym_shndx section.
3098 this->any_postprocessing_sections_ = true;
3101 // Create the dynamic string table section.
3103 Output_section* dynstr = this->choose_output_section(NULL, ".dynstr",
3104 elfcpp::SHT_STRTAB,
3105 elfcpp::SHF_ALLOC,
3106 false, false, true,
3107 false, false, false);
3109 Output_section_data* strdata = new Output_data_strtab(&this->dynpool_);
3110 dynstr->add_output_section_data(strdata);
3112 dynsym->set_link_section(dynstr);
3113 this->dynamic_section_->set_link_section(dynstr);
3115 odyn->add_section_address(elfcpp::DT_STRTAB, dynstr);
3116 odyn->add_section_size(elfcpp::DT_STRSZ, dynstr);
3118 *pdynstr = dynstr;
3120 // Create the hash tables.
3122 if (strcmp(parameters->options().hash_style(), "sysv") == 0
3123 || strcmp(parameters->options().hash_style(), "both") == 0)
3125 unsigned char* phash;
3126 unsigned int hashlen;
3127 Dynobj::create_elf_hash_table(*pdynamic_symbols, local_symcount,
3128 &phash, &hashlen);
3130 Output_section* hashsec = this->choose_output_section(NULL, ".hash",
3131 elfcpp::SHT_HASH,
3132 elfcpp::SHF_ALLOC,
3133 false, false, true,
3134 false, false,
3135 false);
3137 Output_section_data* hashdata = new Output_data_const_buffer(phash,
3138 hashlen,
3139 align,
3140 "** hash");
3141 hashsec->add_output_section_data(hashdata);
3143 hashsec->set_link_section(dynsym);
3144 hashsec->set_entsize(4);
3146 odyn->add_section_address(elfcpp::DT_HASH, hashsec);
3149 if (strcmp(parameters->options().hash_style(), "gnu") == 0
3150 || strcmp(parameters->options().hash_style(), "both") == 0)
3152 unsigned char* phash;
3153 unsigned int hashlen;
3154 Dynobj::create_gnu_hash_table(*pdynamic_symbols, local_symcount,
3155 &phash, &hashlen);
3157 Output_section* hashsec = this->choose_output_section(NULL, ".gnu.hash",
3158 elfcpp::SHT_GNU_HASH,
3159 elfcpp::SHF_ALLOC,
3160 false, false, true,
3161 false, false,
3162 false);
3164 Output_section_data* hashdata = new Output_data_const_buffer(phash,
3165 hashlen,
3166 align,
3167 "** hash");
3168 hashsec->add_output_section_data(hashdata);
3170 hashsec->set_link_section(dynsym);
3172 // For a 64-bit target, the entries in .gnu.hash do not have a
3173 // uniform size, so we only set the entry size for a 32-bit
3174 // target.
3175 if (parameters->target().get_size() == 32)
3176 hashsec->set_entsize(4);
3178 odyn->add_section_address(elfcpp::DT_GNU_HASH, hashsec);
3182 // Assign offsets to each local portion of the dynamic symbol table.
3184 void
3185 Layout::assign_local_dynsym_offsets(const Input_objects* input_objects)
3187 Output_section* dynsym = this->dynsym_section_;
3188 gold_assert(dynsym != NULL);
3190 off_t off = dynsym->offset();
3192 // Skip the dummy symbol at the start of the section.
3193 off += dynsym->entsize();
3195 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
3196 p != input_objects->relobj_end();
3197 ++p)
3199 unsigned int count = (*p)->set_local_dynsym_offset(off);
3200 off += count * dynsym->entsize();
3204 // Create the version sections.
3206 void
3207 Layout::create_version_sections(const Versions* versions,
3208 const Symbol_table* symtab,
3209 unsigned int local_symcount,
3210 const std::vector<Symbol*>& dynamic_symbols,
3211 const Output_section* dynstr)
3213 if (!versions->any_defs() && !versions->any_needs())
3214 return;
3216 switch (parameters->size_and_endianness())
3218 #ifdef HAVE_TARGET_32_LITTLE
3219 case Parameters::TARGET_32_LITTLE:
3220 this->sized_create_version_sections<32, false>(versions, symtab,
3221 local_symcount,
3222 dynamic_symbols, dynstr);
3223 break;
3224 #endif
3225 #ifdef HAVE_TARGET_32_BIG
3226 case Parameters::TARGET_32_BIG:
3227 this->sized_create_version_sections<32, true>(versions, symtab,
3228 local_symcount,
3229 dynamic_symbols, dynstr);
3230 break;
3231 #endif
3232 #ifdef HAVE_TARGET_64_LITTLE
3233 case Parameters::TARGET_64_LITTLE:
3234 this->sized_create_version_sections<64, false>(versions, symtab,
3235 local_symcount,
3236 dynamic_symbols, dynstr);
3237 break;
3238 #endif
3239 #ifdef HAVE_TARGET_64_BIG
3240 case Parameters::TARGET_64_BIG:
3241 this->sized_create_version_sections<64, true>(versions, symtab,
3242 local_symcount,
3243 dynamic_symbols, dynstr);
3244 break;
3245 #endif
3246 default:
3247 gold_unreachable();
3251 // Create the version sections, sized version.
3253 template<int size, bool big_endian>
3254 void
3255 Layout::sized_create_version_sections(
3256 const Versions* versions,
3257 const Symbol_table* symtab,
3258 unsigned int local_symcount,
3259 const std::vector<Symbol*>& dynamic_symbols,
3260 const Output_section* dynstr)
3262 Output_section* vsec = this->choose_output_section(NULL, ".gnu.version",
3263 elfcpp::SHT_GNU_versym,
3264 elfcpp::SHF_ALLOC,
3265 false, false, true,
3266 false, false, false);
3268 unsigned char* vbuf;
3269 unsigned int vsize;
3270 versions->symbol_section_contents<size, big_endian>(symtab, &this->dynpool_,
3271 local_symcount,
3272 dynamic_symbols,
3273 &vbuf, &vsize);
3275 Output_section_data* vdata = new Output_data_const_buffer(vbuf, vsize, 2,
3276 "** versions");
3278 vsec->add_output_section_data(vdata);
3279 vsec->set_entsize(2);
3280 vsec->set_link_section(this->dynsym_section_);
3282 Output_data_dynamic* const odyn = this->dynamic_data_;
3283 odyn->add_section_address(elfcpp::DT_VERSYM, vsec);
3285 if (versions->any_defs())
3287 Output_section* vdsec;
3288 vdsec= this->choose_output_section(NULL, ".gnu.version_d",
3289 elfcpp::SHT_GNU_verdef,
3290 elfcpp::SHF_ALLOC,
3291 false, false, true, false, false,
3292 false);
3294 unsigned char* vdbuf;
3295 unsigned int vdsize;
3296 unsigned int vdentries;
3297 versions->def_section_contents<size, big_endian>(&this->dynpool_, &vdbuf,
3298 &vdsize, &vdentries);
3300 Output_section_data* vddata =
3301 new Output_data_const_buffer(vdbuf, vdsize, 4, "** version defs");
3303 vdsec->add_output_section_data(vddata);
3304 vdsec->set_link_section(dynstr);
3305 vdsec->set_info(vdentries);
3307 odyn->add_section_address(elfcpp::DT_VERDEF, vdsec);
3308 odyn->add_constant(elfcpp::DT_VERDEFNUM, vdentries);
3311 if (versions->any_needs())
3313 Output_section* vnsec;
3314 vnsec = this->choose_output_section(NULL, ".gnu.version_r",
3315 elfcpp::SHT_GNU_verneed,
3316 elfcpp::SHF_ALLOC,
3317 false, false, true, false, false,
3318 false);
3320 unsigned char* vnbuf;
3321 unsigned int vnsize;
3322 unsigned int vnentries;
3323 versions->need_section_contents<size, big_endian>(&this->dynpool_,
3324 &vnbuf, &vnsize,
3325 &vnentries);
3327 Output_section_data* vndata =
3328 new Output_data_const_buffer(vnbuf, vnsize, 4, "** version refs");
3330 vnsec->add_output_section_data(vndata);
3331 vnsec->set_link_section(dynstr);
3332 vnsec->set_info(vnentries);
3334 odyn->add_section_address(elfcpp::DT_VERNEED, vnsec);
3335 odyn->add_constant(elfcpp::DT_VERNEEDNUM, vnentries);
3339 // Create the .interp section and PT_INTERP segment.
3341 void
3342 Layout::create_interp(const Target* target)
3344 const char* interp = parameters->options().dynamic_linker();
3345 if (interp == NULL)
3347 interp = target->dynamic_linker();
3348 gold_assert(interp != NULL);
3351 size_t len = strlen(interp) + 1;
3353 Output_section_data* odata = new Output_data_const(interp, len, 1);
3355 Output_section* osec = this->choose_output_section(NULL, ".interp",
3356 elfcpp::SHT_PROGBITS,
3357 elfcpp::SHF_ALLOC,
3358 false, true, true,
3359 false, false, false);
3360 osec->add_output_section_data(odata);
3362 if (!this->script_options_->saw_phdrs_clause())
3364 Output_segment* oseg = this->make_output_segment(elfcpp::PT_INTERP,
3365 elfcpp::PF_R);
3366 oseg->add_output_section(osec, elfcpp::PF_R, false);
3370 // Add dynamic tags for the PLT and the dynamic relocs. This is
3371 // called by the target-specific code. This does nothing if not doing
3372 // a dynamic link.
3374 // USE_REL is true for REL relocs rather than RELA relocs.
3376 // If PLT_GOT is not NULL, then DT_PLTGOT points to it.
3378 // If PLT_REL is not NULL, it is used for DT_PLTRELSZ, and DT_JMPREL,
3379 // and we also set DT_PLTREL. We use PLT_REL's output section, since
3380 // some targets have multiple reloc sections in PLT_REL.
3382 // If DYN_REL is not NULL, it is used for DT_REL/DT_RELA,
3383 // DT_RELSZ/DT_RELASZ, DT_RELENT/DT_RELAENT.
3385 // If ADD_DEBUG is true, we add a DT_DEBUG entry when generating an
3386 // executable.
3388 void
3389 Layout::add_target_dynamic_tags(bool use_rel, const Output_data* plt_got,
3390 const Output_data* plt_rel,
3391 const Output_data_reloc_generic* dyn_rel,
3392 bool add_debug, bool dynrel_includes_plt)
3394 Output_data_dynamic* odyn = this->dynamic_data_;
3395 if (odyn == NULL)
3396 return;
3398 if (plt_got != NULL && plt_got->output_section() != NULL)
3399 odyn->add_section_address(elfcpp::DT_PLTGOT, plt_got);
3401 if (plt_rel != NULL && plt_rel->output_section() != NULL)
3403 odyn->add_section_size(elfcpp::DT_PLTRELSZ, plt_rel->output_section());
3404 odyn->add_section_address(elfcpp::DT_JMPREL, plt_rel->output_section());
3405 odyn->add_constant(elfcpp::DT_PLTREL,
3406 use_rel ? elfcpp::DT_REL : elfcpp::DT_RELA);
3409 if (dyn_rel != NULL && dyn_rel->output_section() != NULL)
3411 odyn->add_section_address(use_rel ? elfcpp::DT_REL : elfcpp::DT_RELA,
3412 dyn_rel);
3413 if (plt_rel != NULL && dynrel_includes_plt)
3414 odyn->add_section_size(use_rel ? elfcpp::DT_RELSZ : elfcpp::DT_RELASZ,
3415 dyn_rel, plt_rel);
3416 else
3417 odyn->add_section_size(use_rel ? elfcpp::DT_RELSZ : elfcpp::DT_RELASZ,
3418 dyn_rel);
3419 const int size = parameters->target().get_size();
3420 elfcpp::DT rel_tag;
3421 int rel_size;
3422 if (use_rel)
3424 rel_tag = elfcpp::DT_RELENT;
3425 if (size == 32)
3426 rel_size = Reloc_types<elfcpp::SHT_REL, 32, false>::reloc_size;
3427 else if (size == 64)
3428 rel_size = Reloc_types<elfcpp::SHT_REL, 64, false>::reloc_size;
3429 else
3430 gold_unreachable();
3432 else
3434 rel_tag = elfcpp::DT_RELAENT;
3435 if (size == 32)
3436 rel_size = Reloc_types<elfcpp::SHT_RELA, 32, false>::reloc_size;
3437 else if (size == 64)
3438 rel_size = Reloc_types<elfcpp::SHT_RELA, 64, false>::reloc_size;
3439 else
3440 gold_unreachable();
3442 odyn->add_constant(rel_tag, rel_size);
3444 if (parameters->options().combreloc())
3446 size_t c = dyn_rel->relative_reloc_count();
3447 if (c > 0)
3448 odyn->add_constant((use_rel
3449 ? elfcpp::DT_RELCOUNT
3450 : elfcpp::DT_RELACOUNT),
3455 if (add_debug && !parameters->options().shared())
3457 // The value of the DT_DEBUG tag is filled in by the dynamic
3458 // linker at run time, and used by the debugger.
3459 odyn->add_constant(elfcpp::DT_DEBUG, 0);
3463 // Finish the .dynamic section and PT_DYNAMIC segment.
3465 void
3466 Layout::finish_dynamic_section(const Input_objects* input_objects,
3467 const Symbol_table* symtab)
3469 if (!this->script_options_->saw_phdrs_clause())
3471 Output_segment* oseg = this->make_output_segment(elfcpp::PT_DYNAMIC,
3472 (elfcpp::PF_R
3473 | elfcpp::PF_W));
3474 oseg->add_output_section(this->dynamic_section_,
3475 elfcpp::PF_R | elfcpp::PF_W,
3476 false);
3479 Output_data_dynamic* const odyn = this->dynamic_data_;
3481 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
3482 p != input_objects->dynobj_end();
3483 ++p)
3485 if (!(*p)->is_needed()
3486 && (*p)->input_file()->options().as_needed())
3488 // This dynamic object was linked with --as-needed, but it
3489 // is not needed.
3490 continue;
3493 odyn->add_string(elfcpp::DT_NEEDED, (*p)->soname());
3496 if (parameters->options().shared())
3498 const char* soname = parameters->options().soname();
3499 if (soname != NULL)
3500 odyn->add_string(elfcpp::DT_SONAME, soname);
3503 Symbol* sym = symtab->lookup(parameters->options().init());
3504 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
3505 odyn->add_symbol(elfcpp::DT_INIT, sym);
3507 sym = symtab->lookup(parameters->options().fini());
3508 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
3509 odyn->add_symbol(elfcpp::DT_FINI, sym);
3511 // Look for .init_array, .preinit_array and .fini_array by checking
3512 // section types.
3513 for(Layout::Section_list::const_iterator p = this->section_list_.begin();
3514 p != this->section_list_.end();
3515 ++p)
3516 switch((*p)->type())
3518 case elfcpp::SHT_FINI_ARRAY:
3519 odyn->add_section_address(elfcpp::DT_FINI_ARRAY, *p);
3520 odyn->add_section_size(elfcpp::DT_FINI_ARRAYSZ, *p);
3521 break;
3522 case elfcpp::SHT_INIT_ARRAY:
3523 odyn->add_section_address(elfcpp::DT_INIT_ARRAY, *p);
3524 odyn->add_section_size(elfcpp::DT_INIT_ARRAYSZ, *p);
3525 break;
3526 case elfcpp::SHT_PREINIT_ARRAY:
3527 odyn->add_section_address(elfcpp::DT_PREINIT_ARRAY, *p);
3528 odyn->add_section_size(elfcpp::DT_PREINIT_ARRAYSZ, *p);
3529 break;
3530 default:
3531 break;
3534 // Add a DT_RPATH entry if needed.
3535 const General_options::Dir_list& rpath(parameters->options().rpath());
3536 if (!rpath.empty())
3538 std::string rpath_val;
3539 for (General_options::Dir_list::const_iterator p = rpath.begin();
3540 p != rpath.end();
3541 ++p)
3543 if (rpath_val.empty())
3544 rpath_val = p->name();
3545 else
3547 // Eliminate duplicates.
3548 General_options::Dir_list::const_iterator q;
3549 for (q = rpath.begin(); q != p; ++q)
3550 if (q->name() == p->name())
3551 break;
3552 if (q == p)
3554 rpath_val += ':';
3555 rpath_val += p->name();
3560 odyn->add_string(elfcpp::DT_RPATH, rpath_val);
3561 if (parameters->options().enable_new_dtags())
3562 odyn->add_string(elfcpp::DT_RUNPATH, rpath_val);
3565 // Look for text segments that have dynamic relocations.
3566 bool have_textrel = false;
3567 if (!this->script_options_->saw_sections_clause())
3569 for (Segment_list::const_iterator p = this->segment_list_.begin();
3570 p != this->segment_list_.end();
3571 ++p)
3573 if (((*p)->flags() & elfcpp::PF_W) == 0
3574 && (*p)->dynamic_reloc_count() > 0)
3576 have_textrel = true;
3577 break;
3581 else
3583 // We don't know the section -> segment mapping, so we are
3584 // conservative and just look for readonly sections with
3585 // relocations. If those sections wind up in writable segments,
3586 // then we have created an unnecessary DT_TEXTREL entry.
3587 for (Section_list::const_iterator p = this->section_list_.begin();
3588 p != this->section_list_.end();
3589 ++p)
3591 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0
3592 && ((*p)->flags() & elfcpp::SHF_WRITE) == 0
3593 && ((*p)->dynamic_reloc_count() > 0))
3595 have_textrel = true;
3596 break;
3601 // Add a DT_FLAGS entry. We add it even if no flags are set so that
3602 // post-link tools can easily modify these flags if desired.
3603 unsigned int flags = 0;
3604 if (have_textrel)
3606 // Add a DT_TEXTREL for compatibility with older loaders.
3607 odyn->add_constant(elfcpp::DT_TEXTREL, 0);
3608 flags |= elfcpp::DF_TEXTREL;
3610 if (parameters->options().text())
3611 gold_error(_("read-only segment has dynamic relocations"));
3612 else if (parameters->options().warn_shared_textrel()
3613 && parameters->options().shared())
3614 gold_warning(_("shared library text segment is not shareable"));
3616 if (parameters->options().shared() && this->has_static_tls())
3617 flags |= elfcpp::DF_STATIC_TLS;
3618 if (parameters->options().origin())
3619 flags |= elfcpp::DF_ORIGIN;
3620 if (parameters->options().Bsymbolic())
3622 flags |= elfcpp::DF_SYMBOLIC;
3623 // Add DT_SYMBOLIC for compatibility with older loaders.
3624 odyn->add_constant(elfcpp::DT_SYMBOLIC, 0);
3626 if (parameters->options().now())
3627 flags |= elfcpp::DF_BIND_NOW;
3628 odyn->add_constant(elfcpp::DT_FLAGS, flags);
3630 flags = 0;
3631 if (parameters->options().initfirst())
3632 flags |= elfcpp::DF_1_INITFIRST;
3633 if (parameters->options().interpose())
3634 flags |= elfcpp::DF_1_INTERPOSE;
3635 if (parameters->options().loadfltr())
3636 flags |= elfcpp::DF_1_LOADFLTR;
3637 if (parameters->options().nodefaultlib())
3638 flags |= elfcpp::DF_1_NODEFLIB;
3639 if (parameters->options().nodelete())
3640 flags |= elfcpp::DF_1_NODELETE;
3641 if (parameters->options().nodlopen())
3642 flags |= elfcpp::DF_1_NOOPEN;
3643 if (parameters->options().nodump())
3644 flags |= elfcpp::DF_1_NODUMP;
3645 if (!parameters->options().shared())
3646 flags &= ~(elfcpp::DF_1_INITFIRST
3647 | elfcpp::DF_1_NODELETE
3648 | elfcpp::DF_1_NOOPEN);
3649 if (parameters->options().origin())
3650 flags |= elfcpp::DF_1_ORIGIN;
3651 if (parameters->options().now())
3652 flags |= elfcpp::DF_1_NOW;
3653 if (flags)
3654 odyn->add_constant(elfcpp::DT_FLAGS_1, flags);
3657 // Set the size of the _DYNAMIC symbol table to be the size of the
3658 // dynamic data.
3660 void
3661 Layout::set_dynamic_symbol_size(const Symbol_table* symtab)
3663 Output_data_dynamic* const odyn = this->dynamic_data_;
3664 odyn->finalize_data_size();
3665 off_t data_size = odyn->data_size();
3666 const int size = parameters->target().get_size();
3667 if (size == 32)
3668 symtab->get_sized_symbol<32>(this->dynamic_symbol_)->set_symsize(data_size);
3669 else if (size == 64)
3670 symtab->get_sized_symbol<64>(this->dynamic_symbol_)->set_symsize(data_size);
3671 else
3672 gold_unreachable();
3675 // The mapping of input section name prefixes to output section names.
3676 // In some cases one prefix is itself a prefix of another prefix; in
3677 // such a case the longer prefix must come first. These prefixes are
3678 // based on the GNU linker default ELF linker script.
3680 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
3681 const Layout::Section_name_mapping Layout::section_name_mapping[] =
3683 MAPPING_INIT(".text.", ".text"),
3684 MAPPING_INIT(".ctors.", ".ctors"),
3685 MAPPING_INIT(".dtors.", ".dtors"),
3686 MAPPING_INIT(".rodata.", ".rodata"),
3687 MAPPING_INIT(".data.rel.ro.local", ".data.rel.ro.local"),
3688 MAPPING_INIT(".data.rel.ro", ".data.rel.ro"),
3689 MAPPING_INIT(".data.", ".data"),
3690 MAPPING_INIT(".bss.", ".bss"),
3691 MAPPING_INIT(".tdata.", ".tdata"),
3692 MAPPING_INIT(".tbss.", ".tbss"),
3693 MAPPING_INIT(".init_array.", ".init_array"),
3694 MAPPING_INIT(".fini_array.", ".fini_array"),
3695 MAPPING_INIT(".sdata.", ".sdata"),
3696 MAPPING_INIT(".sbss.", ".sbss"),
3697 // FIXME: In the GNU linker, .sbss2 and .sdata2 are handled
3698 // differently depending on whether it is creating a shared library.
3699 MAPPING_INIT(".sdata2.", ".sdata"),
3700 MAPPING_INIT(".sbss2.", ".sbss"),
3701 MAPPING_INIT(".lrodata.", ".lrodata"),
3702 MAPPING_INIT(".ldata.", ".ldata"),
3703 MAPPING_INIT(".lbss.", ".lbss"),
3704 MAPPING_INIT(".gcc_except_table.", ".gcc_except_table"),
3705 MAPPING_INIT(".gnu.linkonce.d.rel.ro.local.", ".data.rel.ro.local"),
3706 MAPPING_INIT(".gnu.linkonce.d.rel.ro.", ".data.rel.ro"),
3707 MAPPING_INIT(".gnu.linkonce.t.", ".text"),
3708 MAPPING_INIT(".gnu.linkonce.r.", ".rodata"),
3709 MAPPING_INIT(".gnu.linkonce.d.", ".data"),
3710 MAPPING_INIT(".gnu.linkonce.b.", ".bss"),
3711 MAPPING_INIT(".gnu.linkonce.s.", ".sdata"),
3712 MAPPING_INIT(".gnu.linkonce.sb.", ".sbss"),
3713 MAPPING_INIT(".gnu.linkonce.s2.", ".sdata"),
3714 MAPPING_INIT(".gnu.linkonce.sb2.", ".sbss"),
3715 MAPPING_INIT(".gnu.linkonce.wi.", ".debug_info"),
3716 MAPPING_INIT(".gnu.linkonce.td.", ".tdata"),
3717 MAPPING_INIT(".gnu.linkonce.tb.", ".tbss"),
3718 MAPPING_INIT(".gnu.linkonce.lr.", ".lrodata"),
3719 MAPPING_INIT(".gnu.linkonce.l.", ".ldata"),
3720 MAPPING_INIT(".gnu.linkonce.lb.", ".lbss"),
3721 MAPPING_INIT(".ARM.extab", ".ARM.extab"),
3722 MAPPING_INIT(".gnu.linkonce.armextab.", ".ARM.extab"),
3723 MAPPING_INIT(".ARM.exidx", ".ARM.exidx"),
3724 MAPPING_INIT(".gnu.linkonce.armexidx.", ".ARM.exidx"),
3726 #undef MAPPING_INIT
3728 const int Layout::section_name_mapping_count =
3729 (sizeof(Layout::section_name_mapping)
3730 / sizeof(Layout::section_name_mapping[0]));
3732 // Choose the output section name to use given an input section name.
3733 // Set *PLEN to the length of the name. *PLEN is initialized to the
3734 // length of NAME.
3736 const char*
3737 Layout::output_section_name(const char* name, size_t* plen)
3739 // gcc 4.3 generates the following sorts of section names when it
3740 // needs a section name specific to a function:
3741 // .text.FN
3742 // .rodata.FN
3743 // .sdata2.FN
3744 // .data.FN
3745 // .data.rel.FN
3746 // .data.rel.local.FN
3747 // .data.rel.ro.FN
3748 // .data.rel.ro.local.FN
3749 // .sdata.FN
3750 // .bss.FN
3751 // .sbss.FN
3752 // .tdata.FN
3753 // .tbss.FN
3755 // The GNU linker maps all of those to the part before the .FN,
3756 // except that .data.rel.local.FN is mapped to .data, and
3757 // .data.rel.ro.local.FN is mapped to .data.rel.ro. The sections
3758 // beginning with .data.rel.ro.local are grouped together.
3760 // For an anonymous namespace, the string FN can contain a '.'.
3762 // Also of interest: .rodata.strN.N, .rodata.cstN, both of which the
3763 // GNU linker maps to .rodata.
3765 // The .data.rel.ro sections are used with -z relro. The sections
3766 // are recognized by name. We use the same names that the GNU
3767 // linker does for these sections.
3769 // It is hard to handle this in a principled way, so we don't even
3770 // try. We use a table of mappings. If the input section name is
3771 // not found in the table, we simply use it as the output section
3772 // name.
3774 const Section_name_mapping* psnm = section_name_mapping;
3775 for (int i = 0; i < section_name_mapping_count; ++i, ++psnm)
3777 if (strncmp(name, psnm->from, psnm->fromlen) == 0)
3779 *plen = psnm->tolen;
3780 return psnm->to;
3784 // Compressed debug sections should be mapped to the corresponding
3785 // uncompressed section.
3786 if (is_compressed_debug_section(name))
3788 size_t len = strlen(name);
3789 char *uncompressed_name = new char[len];
3790 uncompressed_name[0] = '.';
3791 gold_assert(name[0] == '.' && name[1] == 'z');
3792 strncpy(&uncompressed_name[1], &name[2], len - 2);
3793 uncompressed_name[len - 1] = '\0';
3794 *plen = len - 1;
3795 return uncompressed_name;
3798 return name;
3801 // Check if a comdat group or .gnu.linkonce section with the given
3802 // NAME is selected for the link. If there is already a section,
3803 // *KEPT_SECTION is set to point to the existing section and the
3804 // function returns false. Otherwise, OBJECT, SHNDX, IS_COMDAT, and
3805 // IS_GROUP_NAME are recorded for this NAME in the layout object,
3806 // *KEPT_SECTION is set to the internal copy and the function returns
3807 // true.
3809 bool
3810 Layout::find_or_add_kept_section(const std::string& name,
3811 Relobj* object,
3812 unsigned int shndx,
3813 bool is_comdat,
3814 bool is_group_name,
3815 Kept_section** kept_section)
3817 // It's normal to see a couple of entries here, for the x86 thunk
3818 // sections. If we see more than a few, we're linking a C++
3819 // program, and we resize to get more space to minimize rehashing.
3820 if (this->signatures_.size() > 4
3821 && !this->resized_signatures_)
3823 reserve_unordered_map(&this->signatures_,
3824 this->number_of_input_files_ * 64);
3825 this->resized_signatures_ = true;
3828 Kept_section candidate;
3829 std::pair<Signatures::iterator, bool> ins =
3830 this->signatures_.insert(std::make_pair(name, candidate));
3832 if (kept_section != NULL)
3833 *kept_section = &ins.first->second;
3834 if (ins.second)
3836 // This is the first time we've seen this signature.
3837 ins.first->second.set_object(object);
3838 ins.first->second.set_shndx(shndx);
3839 if (is_comdat)
3840 ins.first->second.set_is_comdat();
3841 if (is_group_name)
3842 ins.first->second.set_is_group_name();
3843 return true;
3846 // We have already seen this signature.
3848 if (ins.first->second.is_group_name())
3850 // We've already seen a real section group with this signature.
3851 // If the kept group is from a plugin object, and we're in the
3852 // replacement phase, accept the new one as a replacement.
3853 if (ins.first->second.object() == NULL
3854 && parameters->options().plugins()->in_replacement_phase())
3856 ins.first->second.set_object(object);
3857 ins.first->second.set_shndx(shndx);
3858 return true;
3860 return false;
3862 else if (is_group_name)
3864 // This is a real section group, and we've already seen a
3865 // linkonce section with this signature. Record that we've seen
3866 // a section group, and don't include this section group.
3867 ins.first->second.set_is_group_name();
3868 return false;
3870 else
3872 // We've already seen a linkonce section and this is a linkonce
3873 // section. These don't block each other--this may be the same
3874 // symbol name with different section types.
3875 return true;
3879 // Store the allocated sections into the section list.
3881 void
3882 Layout::get_allocated_sections(Section_list* section_list) const
3884 for (Section_list::const_iterator p = this->section_list_.begin();
3885 p != this->section_list_.end();
3886 ++p)
3887 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
3888 section_list->push_back(*p);
3891 // Create an output segment.
3893 Output_segment*
3894 Layout::make_output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
3896 gold_assert(!parameters->options().relocatable());
3897 Output_segment* oseg = new Output_segment(type, flags);
3898 this->segment_list_.push_back(oseg);
3900 if (type == elfcpp::PT_TLS)
3901 this->tls_segment_ = oseg;
3902 else if (type == elfcpp::PT_GNU_RELRO)
3903 this->relro_segment_ = oseg;
3905 return oseg;
3908 // Write out the Output_sections. Most won't have anything to write,
3909 // since most of the data will come from input sections which are
3910 // handled elsewhere. But some Output_sections do have Output_data.
3912 void
3913 Layout::write_output_sections(Output_file* of) const
3915 for (Section_list::const_iterator p = this->section_list_.begin();
3916 p != this->section_list_.end();
3917 ++p)
3919 if (!(*p)->after_input_sections())
3920 (*p)->write(of);
3924 // Write out data not associated with a section or the symbol table.
3926 void
3927 Layout::write_data(const Symbol_table* symtab, Output_file* of) const
3929 if (!parameters->options().strip_all())
3931 const Output_section* symtab_section = this->symtab_section_;
3932 for (Section_list::const_iterator p = this->section_list_.begin();
3933 p != this->section_list_.end();
3934 ++p)
3936 if ((*p)->needs_symtab_index())
3938 gold_assert(symtab_section != NULL);
3939 unsigned int index = (*p)->symtab_index();
3940 gold_assert(index > 0 && index != -1U);
3941 off_t off = (symtab_section->offset()
3942 + index * symtab_section->entsize());
3943 symtab->write_section_symbol(*p, this->symtab_xindex_, of, off);
3948 const Output_section* dynsym_section = this->dynsym_section_;
3949 for (Section_list::const_iterator p = this->section_list_.begin();
3950 p != this->section_list_.end();
3951 ++p)
3953 if ((*p)->needs_dynsym_index())
3955 gold_assert(dynsym_section != NULL);
3956 unsigned int index = (*p)->dynsym_index();
3957 gold_assert(index > 0 && index != -1U);
3958 off_t off = (dynsym_section->offset()
3959 + index * dynsym_section->entsize());
3960 symtab->write_section_symbol(*p, this->dynsym_xindex_, of, off);
3964 // Write out the Output_data which are not in an Output_section.
3965 for (Data_list::const_iterator p = this->special_output_list_.begin();
3966 p != this->special_output_list_.end();
3967 ++p)
3968 (*p)->write(of);
3971 // Write out the Output_sections which can only be written after the
3972 // input sections are complete.
3974 void
3975 Layout::write_sections_after_input_sections(Output_file* of)
3977 // Determine the final section offsets, and thus the final output
3978 // file size. Note we finalize the .shstrab last, to allow the
3979 // after_input_section sections to modify their section-names before
3980 // writing.
3981 if (this->any_postprocessing_sections_)
3983 off_t off = this->output_file_size_;
3984 off = this->set_section_offsets(off, POSTPROCESSING_SECTIONS_PASS);
3986 // Now that we've finalized the names, we can finalize the shstrab.
3987 off =
3988 this->set_section_offsets(off,
3989 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS);
3991 if (off > this->output_file_size_)
3993 of->resize(off);
3994 this->output_file_size_ = off;
3998 for (Section_list::const_iterator p = this->section_list_.begin();
3999 p != this->section_list_.end();
4000 ++p)
4002 if ((*p)->after_input_sections())
4003 (*p)->write(of);
4006 this->section_headers_->write(of);
4009 // If the build ID requires computing a checksum, do so here, and
4010 // write it out. We compute a checksum over the entire file because
4011 // that is simplest.
4013 void
4014 Layout::write_build_id(Output_file* of) const
4016 if (this->build_id_note_ == NULL)
4017 return;
4019 const unsigned char* iv = of->get_input_view(0, this->output_file_size_);
4021 unsigned char* ov = of->get_output_view(this->build_id_note_->offset(),
4022 this->build_id_note_->data_size());
4024 const char* style = parameters->options().build_id();
4025 if (strcmp(style, "sha1") == 0)
4027 sha1_ctx ctx;
4028 sha1_init_ctx(&ctx);
4029 sha1_process_bytes(iv, this->output_file_size_, &ctx);
4030 sha1_finish_ctx(&ctx, ov);
4032 else if (strcmp(style, "md5") == 0)
4034 md5_ctx ctx;
4035 md5_init_ctx(&ctx);
4036 md5_process_bytes(iv, this->output_file_size_, &ctx);
4037 md5_finish_ctx(&ctx, ov);
4039 else
4040 gold_unreachable();
4042 of->write_output_view(this->build_id_note_->offset(),
4043 this->build_id_note_->data_size(),
4044 ov);
4046 of->free_input_view(0, this->output_file_size_, iv);
4049 // Write out a binary file. This is called after the link is
4050 // complete. IN is the temporary output file we used to generate the
4051 // ELF code. We simply walk through the segments, read them from
4052 // their file offset in IN, and write them to their load address in
4053 // the output file. FIXME: with a bit more work, we could support
4054 // S-records and/or Intel hex format here.
4056 void
4057 Layout::write_binary(Output_file* in) const
4059 gold_assert(parameters->options().oformat_enum()
4060 == General_options::OBJECT_FORMAT_BINARY);
4062 // Get the size of the binary file.
4063 uint64_t max_load_address = 0;
4064 for (Segment_list::const_iterator p = this->segment_list_.begin();
4065 p != this->segment_list_.end();
4066 ++p)
4068 if ((*p)->type() == elfcpp::PT_LOAD && (*p)->filesz() > 0)
4070 uint64_t max_paddr = (*p)->paddr() + (*p)->filesz();
4071 if (max_paddr > max_load_address)
4072 max_load_address = max_paddr;
4076 Output_file out(parameters->options().output_file_name());
4077 out.open(max_load_address);
4079 for (Segment_list::const_iterator p = this->segment_list_.begin();
4080 p != this->segment_list_.end();
4081 ++p)
4083 if ((*p)->type() == elfcpp::PT_LOAD && (*p)->filesz() > 0)
4085 const unsigned char* vin = in->get_input_view((*p)->offset(),
4086 (*p)->filesz());
4087 unsigned char* vout = out.get_output_view((*p)->paddr(),
4088 (*p)->filesz());
4089 memcpy(vout, vin, (*p)->filesz());
4090 out.write_output_view((*p)->paddr(), (*p)->filesz(), vout);
4091 in->free_input_view((*p)->offset(), (*p)->filesz(), vin);
4095 out.close();
4098 // Print the output sections to the map file.
4100 void
4101 Layout::print_to_mapfile(Mapfile* mapfile) const
4103 for (Segment_list::const_iterator p = this->segment_list_.begin();
4104 p != this->segment_list_.end();
4105 ++p)
4106 (*p)->print_sections_to_mapfile(mapfile);
4109 // Print statistical information to stderr. This is used for --stats.
4111 void
4112 Layout::print_stats() const
4114 this->namepool_.print_stats("section name pool");
4115 this->sympool_.print_stats("output symbol name pool");
4116 this->dynpool_.print_stats("dynamic name pool");
4118 for (Section_list::const_iterator p = this->section_list_.begin();
4119 p != this->section_list_.end();
4120 ++p)
4121 (*p)->print_merge_stats();
4124 // Write_sections_task methods.
4126 // We can always run this task.
4128 Task_token*
4129 Write_sections_task::is_runnable()
4131 return NULL;
4134 // We need to unlock both OUTPUT_SECTIONS_BLOCKER and FINAL_BLOCKER
4135 // when finished.
4137 void
4138 Write_sections_task::locks(Task_locker* tl)
4140 tl->add(this, this->output_sections_blocker_);
4141 tl->add(this, this->final_blocker_);
4144 // Run the task--write out the data.
4146 void
4147 Write_sections_task::run(Workqueue*)
4149 this->layout_->write_output_sections(this->of_);
4152 // Write_data_task methods.
4154 // We can always run this task.
4156 Task_token*
4157 Write_data_task::is_runnable()
4159 return NULL;
4162 // We need to unlock FINAL_BLOCKER when finished.
4164 void
4165 Write_data_task::locks(Task_locker* tl)
4167 tl->add(this, this->final_blocker_);
4170 // Run the task--write out the data.
4172 void
4173 Write_data_task::run(Workqueue*)
4175 this->layout_->write_data(this->symtab_, this->of_);
4178 // Write_symbols_task methods.
4180 // We can always run this task.
4182 Task_token*
4183 Write_symbols_task::is_runnable()
4185 return NULL;
4188 // We need to unlock FINAL_BLOCKER when finished.
4190 void
4191 Write_symbols_task::locks(Task_locker* tl)
4193 tl->add(this, this->final_blocker_);
4196 // Run the task--write out the symbols.
4198 void
4199 Write_symbols_task::run(Workqueue*)
4201 this->symtab_->write_globals(this->sympool_, this->dynpool_,
4202 this->layout_->symtab_xindex(),
4203 this->layout_->dynsym_xindex(), this->of_);
4206 // Write_after_input_sections_task methods.
4208 // We can only run this task after the input sections have completed.
4210 Task_token*
4211 Write_after_input_sections_task::is_runnable()
4213 if (this->input_sections_blocker_->is_blocked())
4214 return this->input_sections_blocker_;
4215 return NULL;
4218 // We need to unlock FINAL_BLOCKER when finished.
4220 void
4221 Write_after_input_sections_task::locks(Task_locker* tl)
4223 tl->add(this, this->final_blocker_);
4226 // Run the task.
4228 void
4229 Write_after_input_sections_task::run(Workqueue*)
4231 this->layout_->write_sections_after_input_sections(this->of_);
4234 // Close_task_runner methods.
4236 // Run the task--close the file.
4238 void
4239 Close_task_runner::run(Workqueue*, const Task*)
4241 // If we need to compute a checksum for the BUILD if, we do so here.
4242 this->layout_->write_build_id(this->of_);
4244 // If we've been asked to create a binary file, we do so here.
4245 if (this->options_->oformat_enum() != General_options::OBJECT_FORMAT_ELF)
4246 this->layout_->write_binary(this->of_);
4248 this->of_->close();
4251 // Instantiate the templates we need. We could use the configure
4252 // script to restrict this to only the ones for implemented targets.
4254 #ifdef HAVE_TARGET_32_LITTLE
4255 template
4256 Output_section*
4257 Layout::layout<32, false>(Sized_relobj<32, false>* object, unsigned int shndx,
4258 const char* name,
4259 const elfcpp::Shdr<32, false>& shdr,
4260 unsigned int, unsigned int, off_t*);
4261 #endif
4263 #ifdef HAVE_TARGET_32_BIG
4264 template
4265 Output_section*
4266 Layout::layout<32, true>(Sized_relobj<32, true>* object, unsigned int shndx,
4267 const char* name,
4268 const elfcpp::Shdr<32, true>& shdr,
4269 unsigned int, unsigned int, off_t*);
4270 #endif
4272 #ifdef HAVE_TARGET_64_LITTLE
4273 template
4274 Output_section*
4275 Layout::layout<64, false>(Sized_relobj<64, false>* object, unsigned int shndx,
4276 const char* name,
4277 const elfcpp::Shdr<64, false>& shdr,
4278 unsigned int, unsigned int, off_t*);
4279 #endif
4281 #ifdef HAVE_TARGET_64_BIG
4282 template
4283 Output_section*
4284 Layout::layout<64, true>(Sized_relobj<64, true>* object, unsigned int shndx,
4285 const char* name,
4286 const elfcpp::Shdr<64, true>& shdr,
4287 unsigned int, unsigned int, off_t*);
4288 #endif
4290 #ifdef HAVE_TARGET_32_LITTLE
4291 template
4292 Output_section*
4293 Layout::layout_reloc<32, false>(Sized_relobj<32, false>* object,
4294 unsigned int reloc_shndx,
4295 const elfcpp::Shdr<32, false>& shdr,
4296 Output_section* data_section,
4297 Relocatable_relocs* rr);
4298 #endif
4300 #ifdef HAVE_TARGET_32_BIG
4301 template
4302 Output_section*
4303 Layout::layout_reloc<32, true>(Sized_relobj<32, true>* object,
4304 unsigned int reloc_shndx,
4305 const elfcpp::Shdr<32, true>& shdr,
4306 Output_section* data_section,
4307 Relocatable_relocs* rr);
4308 #endif
4310 #ifdef HAVE_TARGET_64_LITTLE
4311 template
4312 Output_section*
4313 Layout::layout_reloc<64, false>(Sized_relobj<64, false>* object,
4314 unsigned int reloc_shndx,
4315 const elfcpp::Shdr<64, false>& shdr,
4316 Output_section* data_section,
4317 Relocatable_relocs* rr);
4318 #endif
4320 #ifdef HAVE_TARGET_64_BIG
4321 template
4322 Output_section*
4323 Layout::layout_reloc<64, true>(Sized_relobj<64, true>* object,
4324 unsigned int reloc_shndx,
4325 const elfcpp::Shdr<64, true>& shdr,
4326 Output_section* data_section,
4327 Relocatable_relocs* rr);
4328 #endif
4330 #ifdef HAVE_TARGET_32_LITTLE
4331 template
4332 void
4333 Layout::layout_group<32, false>(Symbol_table* symtab,
4334 Sized_relobj<32, false>* object,
4335 unsigned int,
4336 const char* group_section_name,
4337 const char* signature,
4338 const elfcpp::Shdr<32, false>& shdr,
4339 elfcpp::Elf_Word flags,
4340 std::vector<unsigned int>* shndxes);
4341 #endif
4343 #ifdef HAVE_TARGET_32_BIG
4344 template
4345 void
4346 Layout::layout_group<32, true>(Symbol_table* symtab,
4347 Sized_relobj<32, true>* object,
4348 unsigned int,
4349 const char* group_section_name,
4350 const char* signature,
4351 const elfcpp::Shdr<32, true>& shdr,
4352 elfcpp::Elf_Word flags,
4353 std::vector<unsigned int>* shndxes);
4354 #endif
4356 #ifdef HAVE_TARGET_64_LITTLE
4357 template
4358 void
4359 Layout::layout_group<64, false>(Symbol_table* symtab,
4360 Sized_relobj<64, false>* object,
4361 unsigned int,
4362 const char* group_section_name,
4363 const char* signature,
4364 const elfcpp::Shdr<64, false>& shdr,
4365 elfcpp::Elf_Word flags,
4366 std::vector<unsigned int>* shndxes);
4367 #endif
4369 #ifdef HAVE_TARGET_64_BIG
4370 template
4371 void
4372 Layout::layout_group<64, true>(Symbol_table* symtab,
4373 Sized_relobj<64, true>* object,
4374 unsigned int,
4375 const char* group_section_name,
4376 const char* signature,
4377 const elfcpp::Shdr<64, true>& shdr,
4378 elfcpp::Elf_Word flags,
4379 std::vector<unsigned int>* shndxes);
4380 #endif
4382 #ifdef HAVE_TARGET_32_LITTLE
4383 template
4384 Output_section*
4385 Layout::layout_eh_frame<32, false>(Sized_relobj<32, false>* object,
4386 const unsigned char* symbols,
4387 off_t symbols_size,
4388 const unsigned char* symbol_names,
4389 off_t symbol_names_size,
4390 unsigned int shndx,
4391 const elfcpp::Shdr<32, false>& shdr,
4392 unsigned int reloc_shndx,
4393 unsigned int reloc_type,
4394 off_t* off);
4395 #endif
4397 #ifdef HAVE_TARGET_32_BIG
4398 template
4399 Output_section*
4400 Layout::layout_eh_frame<32, true>(Sized_relobj<32, true>* object,
4401 const unsigned char* symbols,
4402 off_t symbols_size,
4403 const unsigned char* symbol_names,
4404 off_t symbol_names_size,
4405 unsigned int shndx,
4406 const elfcpp::Shdr<32, true>& shdr,
4407 unsigned int reloc_shndx,
4408 unsigned int reloc_type,
4409 off_t* off);
4410 #endif
4412 #ifdef HAVE_TARGET_64_LITTLE
4413 template
4414 Output_section*
4415 Layout::layout_eh_frame<64, false>(Sized_relobj<64, false>* object,
4416 const unsigned char* symbols,
4417 off_t symbols_size,
4418 const unsigned char* symbol_names,
4419 off_t symbol_names_size,
4420 unsigned int shndx,
4421 const elfcpp::Shdr<64, false>& shdr,
4422 unsigned int reloc_shndx,
4423 unsigned int reloc_type,
4424 off_t* off);
4425 #endif
4427 #ifdef HAVE_TARGET_64_BIG
4428 template
4429 Output_section*
4430 Layout::layout_eh_frame<64, true>(Sized_relobj<64, true>* object,
4431 const unsigned char* symbols,
4432 off_t symbols_size,
4433 const unsigned char* symbol_names,
4434 off_t symbol_names_size,
4435 unsigned int shndx,
4436 const elfcpp::Shdr<64, true>& shdr,
4437 unsigned int reloc_shndx,
4438 unsigned int reloc_type,
4439 off_t* off);
4440 #endif
4442 } // End namespace gold.