bfd/
[binutils.git] / gold / layout.cc
blob1bbfcb6eb5e32dd513838720eec7d9a26bee7154
1 // layout.cc -- lay out output file sections for gold
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
23 #include "gold.h"
25 #include <cerrno>
26 #include <cstring>
27 #include <algorithm>
28 #include <iostream>
29 #include <utility>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include "libiberty.h"
33 #include "md5.h"
34 #include "sha1.h"
36 #include "parameters.h"
37 #include "options.h"
38 #include "script.h"
39 #include "script-sections.h"
40 #include "output.h"
41 #include "symtab.h"
42 #include "dynobj.h"
43 #include "ehframe.h"
44 #include "compressed_output.h"
45 #include "reloc.h"
46 #include "layout.h"
48 namespace gold
51 // Layout_task_runner methods.
53 // Lay out the sections. This is called after all the input objects
54 // have been read.
56 void
57 Layout_task_runner::run(Workqueue* workqueue, const Task* task)
59 off_t file_size = this->layout_->finalize(this->input_objects_,
60 this->symtab_,
61 this->target_,
62 task);
64 // Now we know the final size of the output file and we know where
65 // each piece of information goes.
66 Output_file* of = new Output_file(parameters->options().output_file_name());
67 if (this->options_.oformat_enum() != General_options::OBJECT_FORMAT_ELF)
68 of->set_is_temporary();
69 of->open(file_size);
71 // Queue up the final set of tasks.
72 gold::queue_final_tasks(this->options_, this->input_objects_,
73 this->symtab_, this->layout_, workqueue, of);
76 // Layout methods.
78 Layout::Layout(const General_options& options, Script_options* script_options)
79 : options_(options), script_options_(script_options), namepool_(),
80 sympool_(), dynpool_(), signatures_(),
81 section_name_map_(), segment_list_(), section_list_(),
82 unattached_section_list_(), special_output_list_(),
83 section_headers_(NULL), tls_segment_(NULL), symtab_section_(NULL),
84 dynsym_section_(NULL), dynamic_section_(NULL), dynamic_data_(NULL),
85 eh_frame_section_(NULL), eh_frame_data_(NULL), eh_frame_hdr_section_(NULL),
86 build_id_note_(NULL), group_signatures_(), output_file_size_(-1),
87 input_requires_executable_stack_(false),
88 input_with_gnu_stack_note_(false),
89 input_without_gnu_stack_note_(false),
90 has_static_tls_(false),
91 any_postprocessing_sections_(false)
93 // Make space for more than enough segments for a typical file.
94 // This is just for efficiency--it's OK if we wind up needing more.
95 this->segment_list_.reserve(12);
97 // We expect two unattached Output_data objects: the file header and
98 // the segment headers.
99 this->special_output_list_.reserve(2);
102 // Hash a key we use to look up an output section mapping.
104 size_t
105 Layout::Hash_key::operator()(const Layout::Key& k) const
107 return k.first + k.second.first + k.second.second;
110 // Return whether PREFIX is a prefix of STR.
112 static inline bool
113 is_prefix_of(const char* prefix, const char* str)
115 return strncmp(prefix, str, strlen(prefix)) == 0;
118 // Returns whether the given section is in the list of
119 // debug-sections-used-by-some-version-of-gdb. Currently,
120 // we've checked versions of gdb up to and including 6.7.1.
122 static const char* gdb_sections[] =
123 { ".debug_abbrev",
124 // ".debug_aranges", // not used by gdb as of 6.7.1
125 ".debug_frame",
126 ".debug_info",
127 ".debug_line",
128 ".debug_loc",
129 ".debug_macinfo",
130 // ".debug_pubnames", // not used by gdb as of 6.7.1
131 ".debug_ranges",
132 ".debug_str",
135 static inline bool
136 is_gdb_debug_section(const char* str)
138 // We can do this faster: binary search or a hashtable. But why bother?
139 for (size_t i = 0; i < sizeof(gdb_sections)/sizeof(*gdb_sections); ++i)
140 if (strcmp(str, gdb_sections[i]) == 0)
141 return true;
142 return false;
145 // Whether to include this section in the link.
147 template<int size, bool big_endian>
148 bool
149 Layout::include_section(Sized_relobj<size, big_endian>*, const char* name,
150 const elfcpp::Shdr<size, big_endian>& shdr)
152 switch (shdr.get_sh_type())
154 case elfcpp::SHT_NULL:
155 case elfcpp::SHT_SYMTAB:
156 case elfcpp::SHT_DYNSYM:
157 case elfcpp::SHT_STRTAB:
158 case elfcpp::SHT_HASH:
159 case elfcpp::SHT_DYNAMIC:
160 case elfcpp::SHT_SYMTAB_SHNDX:
161 return false;
163 case elfcpp::SHT_RELA:
164 case elfcpp::SHT_REL:
165 case elfcpp::SHT_GROUP:
166 // If we are emitting relocations these should be handled
167 // elsewhere.
168 gold_assert(!parameters->options().relocatable()
169 && !parameters->options().emit_relocs());
170 return false;
172 case elfcpp::SHT_PROGBITS:
173 if (parameters->options().strip_debug()
174 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
176 // Debugging sections can only be recognized by name.
177 if (is_prefix_of(".debug", name)
178 || is_prefix_of(".gnu.linkonce.wi.", name)
179 || is_prefix_of(".line", name)
180 || is_prefix_of(".stab", name))
181 return false;
183 if (parameters->options().strip_debug_gdb()
184 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
186 // Debugging sections can only be recognized by name.
187 if (is_prefix_of(".debug", name)
188 && !is_gdb_debug_section(name))
189 return false;
191 return true;
193 default:
194 return true;
198 // Return an output section named NAME, or NULL if there is none.
200 Output_section*
201 Layout::find_output_section(const char* name) const
203 for (Section_list::const_iterator p = this->section_list_.begin();
204 p != this->section_list_.end();
205 ++p)
206 if (strcmp((*p)->name(), name) == 0)
207 return *p;
208 return NULL;
211 // Return an output segment of type TYPE, with segment flags SET set
212 // and segment flags CLEAR clear. Return NULL if there is none.
214 Output_segment*
215 Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
216 elfcpp::Elf_Word clear) const
218 for (Segment_list::const_iterator p = this->segment_list_.begin();
219 p != this->segment_list_.end();
220 ++p)
221 if (static_cast<elfcpp::PT>((*p)->type()) == type
222 && ((*p)->flags() & set) == set
223 && ((*p)->flags() & clear) == 0)
224 return *p;
225 return NULL;
228 // Return the output section to use for section NAME with type TYPE
229 // and section flags FLAGS. NAME must be canonicalized in the string
230 // pool, and NAME_KEY is the key.
232 Output_section*
233 Layout::get_output_section(const char* name, Stringpool::Key name_key,
234 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags)
236 const Key key(name_key, std::make_pair(type, flags));
237 const std::pair<Key, Output_section*> v(key, NULL);
238 std::pair<Section_name_map::iterator, bool> ins(
239 this->section_name_map_.insert(v));
241 if (!ins.second)
242 return ins.first->second;
243 else
245 // This is the first time we've seen this name/type/flags
246 // combination. For compatibility with the GNU linker, we
247 // combine sections with contents and zero flags with sections
248 // with non-zero flags. This is a workaround for cases where
249 // assembler code forgets to set section flags. FIXME: Perhaps
250 // there should be an option to control this.
251 Output_section* os = NULL;
253 if (type == elfcpp::SHT_PROGBITS)
255 if (flags == 0)
257 Output_section* same_name = this->find_output_section(name);
258 if (same_name != NULL
259 && same_name->type() == elfcpp::SHT_PROGBITS
260 && (same_name->flags() & elfcpp::SHF_TLS) == 0)
261 os = same_name;
263 else if ((flags & elfcpp::SHF_TLS) == 0)
265 elfcpp::Elf_Xword zero_flags = 0;
266 const Key zero_key(name_key, std::make_pair(type, zero_flags));
267 Section_name_map::iterator p =
268 this->section_name_map_.find(zero_key);
269 if (p != this->section_name_map_.end())
271 os = p->second;
272 if ((flags & elfcpp::SHF_ALLOC) != 0)
273 this->allocate_output_section(os, flags);
278 if (os == NULL)
279 os = this->make_output_section(name, type, flags);
280 ins.first->second = os;
281 return os;
285 // Pick the output section to use for section NAME, in input file
286 // RELOBJ, with type TYPE and flags FLAGS. RELOBJ may be NULL for a
287 // linker created section. ADJUST_NAME is true if we should apply the
288 // standard name mappings in Layout::output_section_name. This will
289 // return NULL if the input section should be discarded.
291 Output_section*
292 Layout::choose_output_section(const Relobj* relobj, const char* name,
293 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
294 bool adjust_name)
296 // We should ignore some flags. FIXME: This will need some
297 // adjustment for ld -r.
298 flags &= ~ (elfcpp::SHF_INFO_LINK
299 | elfcpp::SHF_LINK_ORDER
300 | elfcpp::SHF_GROUP
301 | elfcpp::SHF_MERGE
302 | elfcpp::SHF_STRINGS);
304 if (this->script_options_->saw_sections_clause())
306 // We are using a SECTIONS clause, so the output section is
307 // chosen based only on the name.
309 Script_sections* ss = this->script_options_->script_sections();
310 const char* file_name = relobj == NULL ? NULL : relobj->name().c_str();
311 Output_section** output_section_slot;
312 name = ss->output_section_name(file_name, name, &output_section_slot);
313 if (name == NULL)
315 // The SECTIONS clause says to discard this input section.
316 return NULL;
319 // If this is an orphan section--one not mentioned in the linker
320 // script--then OUTPUT_SECTION_SLOT will be NULL, and we do the
321 // default processing below.
323 if (output_section_slot != NULL)
325 if (*output_section_slot != NULL)
327 // If the output section was created unallocated, and we
328 // are now allocating it, then we need to clear the
329 // address set in the constructor and remove it from the
330 // unattached section list.
331 if (((*output_section_slot)->flags() & elfcpp::SHF_ALLOC) == 0
332 && (flags & elfcpp::SHF_ALLOC) != 0)
333 this->allocate_output_section(*output_section_slot, flags);
335 return *output_section_slot;
338 // We don't put sections found in the linker script into
339 // SECTION_NAME_MAP_. That keeps us from getting confused
340 // if an orphan section is mapped to a section with the same
341 // name as one in the linker script.
343 name = this->namepool_.add(name, false, NULL);
345 Output_section* os = this->make_output_section(name, type, flags);
346 os->set_found_in_sections_clause();
347 *output_section_slot = os;
348 return os;
352 // FIXME: Handle SHF_OS_NONCONFORMING somewhere.
354 // Turn NAME from the name of the input section into the name of the
355 // output section.
357 size_t len = strlen(name);
358 if (adjust_name && !parameters->options().relocatable())
359 name = Layout::output_section_name(name, &len);
361 Stringpool::Key name_key;
362 name = this->namepool_.add_with_length(name, len, true, &name_key);
364 // Find or make the output section. The output section is selected
365 // based on the section name, type, and flags.
366 return this->get_output_section(name, name_key, type, flags);
369 // Return the output section to use for input section SHNDX, with name
370 // NAME, with header HEADER, from object OBJECT. RELOC_SHNDX is the
371 // index of a relocation section which applies to this section, or 0
372 // if none, or -1U if more than one. RELOC_TYPE is the type of the
373 // relocation section if there is one. Set *OFF to the offset of this
374 // input section without the output section. Return NULL if the
375 // section should be discarded. Set *OFF to -1 if the section
376 // contents should not be written directly to the output file, but
377 // will instead receive special handling.
379 template<int size, bool big_endian>
380 Output_section*
381 Layout::layout(Sized_relobj<size, big_endian>* object, unsigned int shndx,
382 const char* name, const elfcpp::Shdr<size, big_endian>& shdr,
383 unsigned int reloc_shndx, unsigned int, off_t* off)
385 if (!this->include_section(object, name, shdr))
386 return NULL;
388 Output_section* os;
390 // In a relocatable link a grouped section must not be combined with
391 // any other sections.
392 if (parameters->options().relocatable()
393 && (shdr.get_sh_flags() & elfcpp::SHF_GROUP) != 0)
395 name = this->namepool_.add(name, true, NULL);
396 os = this->make_output_section(name, shdr.get_sh_type(),
397 shdr.get_sh_flags());
399 else
401 os = this->choose_output_section(object, name, shdr.get_sh_type(),
402 shdr.get_sh_flags(), true);
403 if (os == NULL)
404 return NULL;
407 // FIXME: Handle SHF_LINK_ORDER somewhere.
409 *off = os->add_input_section(object, shndx, name, shdr, reloc_shndx,
410 this->script_options_->saw_sections_clause());
412 return os;
415 // Handle a relocation section when doing a relocatable link.
417 template<int size, bool big_endian>
418 Output_section*
419 Layout::layout_reloc(Sized_relobj<size, big_endian>* object,
420 unsigned int,
421 const elfcpp::Shdr<size, big_endian>& shdr,
422 Output_section* data_section,
423 Relocatable_relocs* rr)
425 gold_assert(parameters->options().relocatable()
426 || parameters->options().emit_relocs());
428 int sh_type = shdr.get_sh_type();
430 std::string name;
431 if (sh_type == elfcpp::SHT_REL)
432 name = ".rel";
433 else if (sh_type == elfcpp::SHT_RELA)
434 name = ".rela";
435 else
436 gold_unreachable();
437 name += data_section->name();
439 Output_section* os = this->choose_output_section(object, name.c_str(),
440 sh_type,
441 shdr.get_sh_flags(),
442 false);
444 os->set_should_link_to_symtab();
445 os->set_info_section(data_section);
447 Output_section_data* posd;
448 if (sh_type == elfcpp::SHT_REL)
450 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
451 posd = new Output_relocatable_relocs<elfcpp::SHT_REL,
452 size,
453 big_endian>(rr);
455 else if (sh_type == elfcpp::SHT_RELA)
457 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
458 posd = new Output_relocatable_relocs<elfcpp::SHT_RELA,
459 size,
460 big_endian>(rr);
462 else
463 gold_unreachable();
465 os->add_output_section_data(posd);
466 rr->set_output_data(posd);
468 return os;
471 // Handle a group section when doing a relocatable link.
473 template<int size, bool big_endian>
474 void
475 Layout::layout_group(Symbol_table* symtab,
476 Sized_relobj<size, big_endian>* object,
477 unsigned int,
478 const char* group_section_name,
479 const char* signature,
480 const elfcpp::Shdr<size, big_endian>& shdr,
481 const elfcpp::Elf_Word* contents)
483 gold_assert(parameters->options().relocatable());
484 gold_assert(shdr.get_sh_type() == elfcpp::SHT_GROUP);
485 group_section_name = this->namepool_.add(group_section_name, true, NULL);
486 Output_section* os = this->make_output_section(group_section_name,
487 elfcpp::SHT_GROUP,
488 shdr.get_sh_flags());
490 // We need to find a symbol with the signature in the symbol table.
491 // If we don't find one now, we need to look again later.
492 Symbol* sym = symtab->lookup(signature, NULL);
493 if (sym != NULL)
494 os->set_info_symndx(sym);
495 else
497 // We will wind up using a symbol whose name is the signature.
498 // So just put the signature in the symbol name pool to save it.
499 signature = symtab->canonicalize_name(signature);
500 this->group_signatures_.push_back(Group_signature(os, signature));
503 os->set_should_link_to_symtab();
504 os->set_entsize(4);
506 section_size_type entry_count =
507 convert_to_section_size_type(shdr.get_sh_size() / 4);
508 Output_section_data* posd =
509 new Output_data_group<size, big_endian>(object, entry_count, contents);
510 os->add_output_section_data(posd);
513 // Special GNU handling of sections name .eh_frame. They will
514 // normally hold exception frame data as defined by the C++ ABI
515 // (http://codesourcery.com/cxx-abi/).
517 template<int size, bool big_endian>
518 Output_section*
519 Layout::layout_eh_frame(Sized_relobj<size, big_endian>* object,
520 const unsigned char* symbols,
521 off_t symbols_size,
522 const unsigned char* symbol_names,
523 off_t symbol_names_size,
524 unsigned int shndx,
525 const elfcpp::Shdr<size, big_endian>& shdr,
526 unsigned int reloc_shndx, unsigned int reloc_type,
527 off_t* off)
529 gold_assert(shdr.get_sh_type() == elfcpp::SHT_PROGBITS);
530 gold_assert((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0);
532 const char* const name = ".eh_frame";
533 Output_section* os = this->choose_output_section(object,
534 name,
535 elfcpp::SHT_PROGBITS,
536 elfcpp::SHF_ALLOC,
537 false);
538 if (os == NULL)
539 return NULL;
541 // On some targets gcc assumes that a read-only .eh_frame section
542 // will be merged with a read-write .eh_frame section.
543 if ((shdr.get_sh_flags() & elfcpp::SHF_WRITE) != 0
544 && (os->flags() & elfcpp::SHF_WRITE) == 0)
546 elfcpp::Elf_Xword new_flags = os->flags() | elfcpp::SHF_WRITE;
547 this->write_enable_output_section(os, new_flags);
548 os->set_flags(new_flags);
551 if (this->eh_frame_section_ == NULL)
553 this->eh_frame_section_ = os;
554 this->eh_frame_data_ = new Eh_frame();
555 os->add_output_section_data(this->eh_frame_data_);
557 if (this->options_.eh_frame_hdr())
559 Output_section* hdr_os =
560 this->choose_output_section(NULL,
561 ".eh_frame_hdr",
562 elfcpp::SHT_PROGBITS,
563 elfcpp::SHF_ALLOC,
564 false);
566 if (hdr_os != NULL)
568 Eh_frame_hdr* hdr_posd = new Eh_frame_hdr(os,
569 this->eh_frame_data_);
570 hdr_os->add_output_section_data(hdr_posd);
572 hdr_os->set_after_input_sections();
574 if (!this->script_options_->saw_phdrs_clause())
576 Output_segment* hdr_oseg;
577 hdr_oseg = this->make_output_segment(elfcpp::PT_GNU_EH_FRAME,
578 elfcpp::PF_R);
579 hdr_oseg->add_output_section(hdr_os, elfcpp::PF_R);
582 this->eh_frame_data_->set_eh_frame_hdr(hdr_posd);
587 gold_assert(this->eh_frame_section_ == os);
589 if (this->eh_frame_data_->add_ehframe_input_section(object,
590 symbols,
591 symbols_size,
592 symbol_names,
593 symbol_names_size,
594 shndx,
595 reloc_shndx,
596 reloc_type))
597 *off = -1;
598 else
600 // We couldn't handle this .eh_frame section for some reason.
601 // Add it as a normal section.
602 bool saw_sections_clause = this->script_options_->saw_sections_clause();
603 *off = os->add_input_section(object, shndx, name, shdr, reloc_shndx,
604 saw_sections_clause);
607 return os;
610 // Add POSD to an output section using NAME, TYPE, and FLAGS.
612 void
613 Layout::add_output_section_data(const char* name, elfcpp::Elf_Word type,
614 elfcpp::Elf_Xword flags,
615 Output_section_data* posd)
617 Output_section* os = this->choose_output_section(NULL, name, type, flags,
618 false);
619 if (os != NULL)
620 os->add_output_section_data(posd);
623 // Map section flags to segment flags.
625 elfcpp::Elf_Word
626 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags)
628 elfcpp::Elf_Word ret = elfcpp::PF_R;
629 if ((flags & elfcpp::SHF_WRITE) != 0)
630 ret |= elfcpp::PF_W;
631 if ((flags & elfcpp::SHF_EXECINSTR) != 0)
632 ret |= elfcpp::PF_X;
633 return ret;
636 // Sometimes we compress sections. This is typically done for
637 // sections that are not part of normal program execution (such as
638 // .debug_* sections), and where the readers of these sections know
639 // how to deal with compressed sections. (To make it easier for them,
640 // we will rename the ouput section in such cases from .foo to
641 // .foo.zlib.nnnn, where nnnn is the uncompressed size.) This routine
642 // doesn't say for certain whether we'll compress -- it depends on
643 // commandline options as well -- just whether this section is a
644 // candidate for compression.
646 static bool
647 is_compressible_debug_section(const char* secname)
649 return (strncmp(secname, ".debug", sizeof(".debug") - 1) == 0);
652 // Make a new Output_section, and attach it to segments as
653 // appropriate.
655 Output_section*
656 Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
657 elfcpp::Elf_Xword flags)
659 Output_section* os;
660 if ((flags & elfcpp::SHF_ALLOC) == 0
661 && strcmp(this->options_.compress_debug_sections(), "none") != 0
662 && is_compressible_debug_section(name))
663 os = new Output_compressed_section(&this->options_, name, type, flags);
664 else
665 os = new Output_section(name, type, flags);
667 this->section_list_.push_back(os);
669 if ((flags & elfcpp::SHF_ALLOC) == 0)
670 this->unattached_section_list_.push_back(os);
671 else
672 this->attach_to_segment(os, flags);
674 return os;
677 // Attach an allocated output section to a segment.
679 void
680 Layout::attach_to_segment(Output_section* os, elfcpp::Elf_Xword flags)
682 gold_assert((flags & elfcpp::SHF_ALLOC) != 0);
684 if (parameters->options().relocatable())
685 return;
687 // If we have a SECTIONS clause, we can't handle the attachment to
688 // segments until after we've seen all the sections.
689 if (this->script_options_->saw_sections_clause())
690 return;
692 gold_assert(!this->script_options_->saw_phdrs_clause());
694 // This output section goes into a PT_LOAD segment.
696 elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags);
698 // In general the only thing we really care about for PT_LOAD
699 // segments is whether or not they are writable, so that is how we
700 // search for them. People who need segments sorted on some other
701 // basis will have to use a linker script.
703 Segment_list::const_iterator p;
704 for (p = this->segment_list_.begin();
705 p != this->segment_list_.end();
706 ++p)
708 if ((*p)->type() == elfcpp::PT_LOAD
709 && ((*p)->flags() & elfcpp::PF_W) == (seg_flags & elfcpp::PF_W))
711 // If -Tbss was specified, we need to separate the data
712 // and BSS segments.
713 if (this->options_.user_set_Tbss())
715 if ((os->type() == elfcpp::SHT_NOBITS)
716 == (*p)->has_any_data_sections())
717 continue;
720 (*p)->add_output_section(os, seg_flags);
721 break;
725 if (p == this->segment_list_.end())
727 Output_segment* oseg = this->make_output_segment(elfcpp::PT_LOAD,
728 seg_flags);
729 oseg->add_output_section(os, seg_flags);
732 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
733 // segment.
734 if (os->type() == elfcpp::SHT_NOTE)
736 // See if we already have an equivalent PT_NOTE segment.
737 for (p = this->segment_list_.begin();
738 p != segment_list_.end();
739 ++p)
741 if ((*p)->type() == elfcpp::PT_NOTE
742 && (((*p)->flags() & elfcpp::PF_W)
743 == (seg_flags & elfcpp::PF_W)))
745 (*p)->add_output_section(os, seg_flags);
746 break;
750 if (p == this->segment_list_.end())
752 Output_segment* oseg = this->make_output_segment(elfcpp::PT_NOTE,
753 seg_flags);
754 oseg->add_output_section(os, seg_flags);
758 // If we see a loadable SHF_TLS section, we create a PT_TLS
759 // segment. There can only be one such segment.
760 if ((flags & elfcpp::SHF_TLS) != 0)
762 if (this->tls_segment_ == NULL)
763 this->tls_segment_ = this->make_output_segment(elfcpp::PT_TLS,
764 seg_flags);
765 this->tls_segment_->add_output_section(os, seg_flags);
769 // Make an output section for a script.
771 Output_section*
772 Layout::make_output_section_for_script(const char* name)
774 name = this->namepool_.add(name, false, NULL);
775 Output_section* os = this->make_output_section(name, elfcpp::SHT_PROGBITS,
776 elfcpp::SHF_ALLOC);
777 os->set_found_in_sections_clause();
778 return os;
781 // We have to move an existing output section from the unallocated
782 // list to the allocated list.
784 void
785 Layout::allocate_output_section(Output_section* os, elfcpp::Elf_Xword flags)
787 os->reset_address_and_file_offset();
789 Section_list::iterator p = std::find(this->unattached_section_list_.begin(),
790 this->unattached_section_list_.end(),
791 os);
792 gold_assert(p != this->unattached_section_list_.end());
793 this->unattached_section_list_.erase(p);
795 this->attach_to_segment(os, flags);
798 // We have to move an existing output section from the read-only
799 // segment to the writable segment.
801 void
802 Layout::write_enable_output_section(Output_section* os,
803 elfcpp::Elf_Xword flags)
805 gold_assert((os->flags() & elfcpp::SHF_WRITE) == 0);
806 gold_assert(os->type() == elfcpp::SHT_PROGBITS);
807 gold_assert((flags & elfcpp::SHF_WRITE) != 0);
808 gold_assert((flags & elfcpp::SHF_ALLOC) != 0);
810 if (parameters->options().relocatable())
811 return;
813 if (this->script_options_->saw_sections_clause())
814 return;
816 Segment_list::iterator p;
817 for (p = this->segment_list_.begin();
818 p != this->segment_list_.end();
819 ++p)
821 if ((*p)->type() == elfcpp::PT_LOAD
822 && ((*p)->flags() & elfcpp::PF_W) == 0)
824 (*p)->remove_output_section(os);
825 break;
828 gold_assert(p != this->segment_list_.end());
830 this->attach_to_segment(os, flags);
833 // Return the number of segments we expect to see.
835 size_t
836 Layout::expected_segment_count() const
838 size_t ret = this->segment_list_.size();
840 // If we didn't see a SECTIONS clause in a linker script, we should
841 // already have the complete list of segments. Otherwise we ask the
842 // SECTIONS clause how many segments it expects, and add in the ones
843 // we already have (PT_GNU_STACK, PT_GNU_EH_FRAME, etc.)
845 if (!this->script_options_->saw_sections_clause())
846 return ret;
847 else
849 const Script_sections* ss = this->script_options_->script_sections();
850 return ret + ss->expected_segment_count(this);
854 // Handle the .note.GNU-stack section at layout time. SEEN_GNU_STACK
855 // is whether we saw a .note.GNU-stack section in the object file.
856 // GNU_STACK_FLAGS is the section flags. The flags give the
857 // protection required for stack memory. We record this in an
858 // executable as a PT_GNU_STACK segment. If an object file does not
859 // have a .note.GNU-stack segment, we must assume that it is an old
860 // object. On some targets that will force an executable stack.
862 void
863 Layout::layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags)
865 if (!seen_gnu_stack)
866 this->input_without_gnu_stack_note_ = true;
867 else
869 this->input_with_gnu_stack_note_ = true;
870 if ((gnu_stack_flags & elfcpp::SHF_EXECINSTR) != 0)
871 this->input_requires_executable_stack_ = true;
875 // Create the dynamic sections which are needed before we read the
876 // relocs.
878 void
879 Layout::create_initial_dynamic_sections(Symbol_table* symtab)
881 if (parameters->doing_static_link())
882 return;
884 this->dynamic_section_ = this->choose_output_section(NULL, ".dynamic",
885 elfcpp::SHT_DYNAMIC,
886 (elfcpp::SHF_ALLOC
887 | elfcpp::SHF_WRITE),
888 false);
890 symtab->define_in_output_data("_DYNAMIC", NULL, this->dynamic_section_, 0, 0,
891 elfcpp::STT_OBJECT, elfcpp::STB_LOCAL,
892 elfcpp::STV_HIDDEN, 0, false, false);
894 this->dynamic_data_ = new Output_data_dynamic(&this->dynpool_);
896 this->dynamic_section_->add_output_section_data(this->dynamic_data_);
899 // For each output section whose name can be represented as C symbol,
900 // define __start and __stop symbols for the section. This is a GNU
901 // extension.
903 void
904 Layout::define_section_symbols(Symbol_table* symtab)
906 for (Section_list::const_iterator p = this->section_list_.begin();
907 p != this->section_list_.end();
908 ++p)
910 const char* const name = (*p)->name();
911 if (name[strspn(name,
912 ("0123456789"
913 "ABCDEFGHIJKLMNOPWRSTUVWXYZ"
914 "abcdefghijklmnopqrstuvwxyz"
915 "_"))]
916 == '\0')
918 const std::string name_string(name);
919 const std::string start_name("__start_" + name_string);
920 const std::string stop_name("__stop_" + name_string);
922 symtab->define_in_output_data(start_name.c_str(),
923 NULL, // version
925 0, // value
926 0, // symsize
927 elfcpp::STT_NOTYPE,
928 elfcpp::STB_GLOBAL,
929 elfcpp::STV_DEFAULT,
930 0, // nonvis
931 false, // offset_is_from_end
932 true); // only_if_ref
934 symtab->define_in_output_data(stop_name.c_str(),
935 NULL, // version
937 0, // value
938 0, // symsize
939 elfcpp::STT_NOTYPE,
940 elfcpp::STB_GLOBAL,
941 elfcpp::STV_DEFAULT,
942 0, // nonvis
943 true, // offset_is_from_end
944 true); // only_if_ref
949 // Define symbols for group signatures.
951 void
952 Layout::define_group_signatures(Symbol_table* symtab)
954 for (Group_signatures::iterator p = this->group_signatures_.begin();
955 p != this->group_signatures_.end();
956 ++p)
958 Symbol* sym = symtab->lookup(p->signature, NULL);
959 if (sym != NULL)
960 p->section->set_info_symndx(sym);
961 else
963 // Force the name of the group section to the group
964 // signature, and use the group's section symbol as the
965 // signature symbol.
966 if (strcmp(p->section->name(), p->signature) != 0)
968 const char* name = this->namepool_.add(p->signature,
969 true, NULL);
970 p->section->set_name(name);
972 p->section->set_needs_symtab_index();
973 p->section->set_info_section_symndx(p->section);
977 this->group_signatures_.clear();
980 // Find the first read-only PT_LOAD segment, creating one if
981 // necessary.
983 Output_segment*
984 Layout::find_first_load_seg()
986 for (Segment_list::const_iterator p = this->segment_list_.begin();
987 p != this->segment_list_.end();
988 ++p)
990 if ((*p)->type() == elfcpp::PT_LOAD
991 && ((*p)->flags() & elfcpp::PF_R) != 0
992 && ((*p)->flags() & elfcpp::PF_W) == 0)
993 return *p;
996 gold_assert(!this->script_options_->saw_phdrs_clause());
998 Output_segment* load_seg = this->make_output_segment(elfcpp::PT_LOAD,
999 elfcpp::PF_R);
1000 return load_seg;
1003 // Finalize the layout. When this is called, we have created all the
1004 // output sections and all the output segments which are based on
1005 // input sections. We have several things to do, and we have to do
1006 // them in the right order, so that we get the right results correctly
1007 // and efficiently.
1009 // 1) Finalize the list of output segments and create the segment
1010 // table header.
1012 // 2) Finalize the dynamic symbol table and associated sections.
1014 // 3) Determine the final file offset of all the output segments.
1016 // 4) Determine the final file offset of all the SHF_ALLOC output
1017 // sections.
1019 // 5) Create the symbol table sections and the section name table
1020 // section.
1022 // 6) Finalize the symbol table: set symbol values to their final
1023 // value and make a final determination of which symbols are going
1024 // into the output symbol table.
1026 // 7) Create the section table header.
1028 // 8) Determine the final file offset of all the output sections which
1029 // are not SHF_ALLOC, including the section table header.
1031 // 9) Finalize the ELF file header.
1033 // This function returns the size of the output file.
1035 off_t
1036 Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab,
1037 Target* target, const Task* task)
1039 target->finalize_sections(this);
1041 this->count_local_symbols(task, input_objects);
1043 this->create_gold_note();
1044 this->create_executable_stack_info(target);
1045 this->create_build_id();
1047 Output_segment* phdr_seg = NULL;
1048 if (!parameters->options().relocatable() && !parameters->doing_static_link())
1050 // There was a dynamic object in the link. We need to create
1051 // some information for the dynamic linker.
1053 // Create the PT_PHDR segment which will hold the program
1054 // headers.
1055 if (!this->script_options_->saw_phdrs_clause())
1056 phdr_seg = this->make_output_segment(elfcpp::PT_PHDR, elfcpp::PF_R);
1058 // Create the dynamic symbol table, including the hash table.
1059 Output_section* dynstr;
1060 std::vector<Symbol*> dynamic_symbols;
1061 unsigned int local_dynamic_count;
1062 Versions versions(*this->script_options()->version_script_info(),
1063 &this->dynpool_);
1064 this->create_dynamic_symtab(input_objects, symtab, &dynstr,
1065 &local_dynamic_count, &dynamic_symbols,
1066 &versions);
1068 // Create the .interp section to hold the name of the
1069 // interpreter, and put it in a PT_INTERP segment.
1070 if (!parameters->options().shared())
1071 this->create_interp(target);
1073 // Finish the .dynamic section to hold the dynamic data, and put
1074 // it in a PT_DYNAMIC segment.
1075 this->finish_dynamic_section(input_objects, symtab);
1077 // We should have added everything we need to the dynamic string
1078 // table.
1079 this->dynpool_.set_string_offsets();
1081 // Create the version sections. We can't do this until the
1082 // dynamic string table is complete.
1083 this->create_version_sections(&versions, symtab, local_dynamic_count,
1084 dynamic_symbols, dynstr);
1087 // If there is a SECTIONS clause, put all the input sections into
1088 // the required order.
1089 Output_segment* load_seg;
1090 if (this->script_options_->saw_sections_clause())
1091 load_seg = this->set_section_addresses_from_script(symtab);
1092 else if (parameters->options().relocatable())
1093 load_seg = NULL;
1094 else
1095 load_seg = this->find_first_load_seg();
1097 if (this->options_.oformat_enum() != General_options::OBJECT_FORMAT_ELF)
1098 load_seg = NULL;
1100 gold_assert(phdr_seg == NULL || load_seg != NULL);
1102 // Lay out the segment headers.
1103 Output_segment_headers* segment_headers;
1104 if (parameters->options().relocatable())
1105 segment_headers = NULL;
1106 else
1108 segment_headers = new Output_segment_headers(this->segment_list_);
1109 if (load_seg != NULL)
1110 load_seg->add_initial_output_data(segment_headers);
1111 if (phdr_seg != NULL)
1112 phdr_seg->add_initial_output_data(segment_headers);
1115 // Lay out the file header.
1116 Output_file_header* file_header;
1117 file_header = new Output_file_header(target, symtab, segment_headers,
1118 this->options_.entry());
1119 if (load_seg != NULL)
1120 load_seg->add_initial_output_data(file_header);
1122 this->special_output_list_.push_back(file_header);
1123 if (segment_headers != NULL)
1124 this->special_output_list_.push_back(segment_headers);
1126 if (this->script_options_->saw_phdrs_clause()
1127 && !parameters->options().relocatable())
1129 // Support use of FILEHDRS and PHDRS attachments in a PHDRS
1130 // clause in a linker script.
1131 Script_sections* ss = this->script_options_->script_sections();
1132 ss->put_headers_in_phdrs(file_header, segment_headers);
1135 // We set the output section indexes in set_segment_offsets and
1136 // set_section_indexes.
1137 unsigned int shndx = 1;
1139 // Set the file offsets of all the segments, and all the sections
1140 // they contain.
1141 off_t off;
1142 if (!parameters->options().relocatable())
1143 off = this->set_segment_offsets(target, load_seg, &shndx);
1144 else
1145 off = this->set_relocatable_section_offsets(file_header, &shndx);
1147 // Set the file offsets of all the non-data sections we've seen so
1148 // far which don't have to wait for the input sections. We need
1149 // this in order to finalize local symbols in non-allocated
1150 // sections.
1151 off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS);
1153 // Create the symbol table sections.
1154 this->create_symtab_sections(input_objects, symtab, &off);
1155 if (!parameters->doing_static_link())
1156 this->assign_local_dynsym_offsets(input_objects);
1158 // Process any symbol assignments from a linker script. This must
1159 // be called after the symbol table has been finalized.
1160 this->script_options_->finalize_symbols(symtab, this);
1162 // Create the .shstrtab section.
1163 Output_section* shstrtab_section = this->create_shstrtab();
1165 // Set the file offsets of the rest of the non-data sections which
1166 // don't have to wait for the input sections.
1167 off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS);
1169 // Now that all sections have been created, set the section indexes.
1170 shndx = this->set_section_indexes(shndx);
1172 // Create the section table header.
1173 this->create_shdrs(&off);
1175 // If there are no sections which require postprocessing, we can
1176 // handle the section names now, and avoid a resize later.
1177 if (!this->any_postprocessing_sections_)
1178 off = this->set_section_offsets(off,
1179 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS);
1181 file_header->set_section_info(this->section_headers_, shstrtab_section);
1183 // Now we know exactly where everything goes in the output file
1184 // (except for non-allocated sections which require postprocessing).
1185 Output_data::layout_complete();
1187 this->output_file_size_ = off;
1189 return off;
1192 // Create a note header following the format defined in the ELF ABI.
1193 // NAME is the name, NOTE_TYPE is the type, DESCSZ is the size of the
1194 // descriptor. ALLOCATE is true if the section should be allocated in
1195 // memory. This returns the new note section. It sets
1196 // *TRAILING_PADDING to the number of trailing zero bytes required.
1198 Output_section*
1199 Layout::create_note(const char* name, int note_type, size_t descsz,
1200 bool allocate, size_t* trailing_padding)
1202 // Authorities all agree that the values in a .note field should
1203 // be aligned on 4-byte boundaries for 32-bit binaries. However,
1204 // they differ on what the alignment is for 64-bit binaries.
1205 // The GABI says unambiguously they take 8-byte alignment:
1206 // http://sco.com/developers/gabi/latest/ch5.pheader.html#note_section
1207 // Other documentation says alignment should always be 4 bytes:
1208 // http://www.netbsd.org/docs/kernel/elf-notes.html#note-format
1209 // GNU ld and GNU readelf both support the latter (at least as of
1210 // version 2.16.91), and glibc always generates the latter for
1211 // .note.ABI-tag (as of version 1.6), so that's the one we go with
1212 // here.
1213 #ifdef GABI_FORMAT_FOR_DOTNOTE_SECTION // This is not defined by default.
1214 const int size = parameters->target().get_size();
1215 #else
1216 const int size = 32;
1217 #endif
1219 // The contents of the .note section.
1220 size_t namesz = strlen(name) + 1;
1221 size_t aligned_namesz = align_address(namesz, size / 8);
1222 size_t aligned_descsz = align_address(descsz, size / 8);
1224 size_t notehdrsz = 3 * (size / 8) + aligned_namesz;
1226 unsigned char* buffer = new unsigned char[notehdrsz];
1227 memset(buffer, 0, notehdrsz);
1229 bool is_big_endian = parameters->target().is_big_endian();
1231 if (size == 32)
1233 if (!is_big_endian)
1235 elfcpp::Swap<32, false>::writeval(buffer, namesz);
1236 elfcpp::Swap<32, false>::writeval(buffer + 4, descsz);
1237 elfcpp::Swap<32, false>::writeval(buffer + 8, note_type);
1239 else
1241 elfcpp::Swap<32, true>::writeval(buffer, namesz);
1242 elfcpp::Swap<32, true>::writeval(buffer + 4, descsz);
1243 elfcpp::Swap<32, true>::writeval(buffer + 8, note_type);
1246 else if (size == 64)
1248 if (!is_big_endian)
1250 elfcpp::Swap<64, false>::writeval(buffer, namesz);
1251 elfcpp::Swap<64, false>::writeval(buffer + 8, descsz);
1252 elfcpp::Swap<64, false>::writeval(buffer + 16, note_type);
1254 else
1256 elfcpp::Swap<64, true>::writeval(buffer, namesz);
1257 elfcpp::Swap<64, true>::writeval(buffer + 8, descsz);
1258 elfcpp::Swap<64, true>::writeval(buffer + 16, note_type);
1261 else
1262 gold_unreachable();
1264 memcpy(buffer + 3 * (size / 8), name, namesz);
1266 const char* note_name = this->namepool_.add(".note", false, NULL);
1267 elfcpp::Elf_Xword flags = 0;
1268 if (allocate)
1269 flags = elfcpp::SHF_ALLOC;
1270 Output_section* os = this->make_output_section(note_name,
1271 elfcpp::SHT_NOTE,
1272 flags);
1273 Output_section_data* posd = new Output_data_const_buffer(buffer, notehdrsz,
1274 size / 8);
1275 os->add_output_section_data(posd);
1277 *trailing_padding = aligned_descsz - descsz;
1279 return os;
1282 // For an executable or shared library, create a note to record the
1283 // version of gold used to create the binary.
1285 void
1286 Layout::create_gold_note()
1288 if (parameters->options().relocatable())
1289 return;
1291 std::string desc = std::string("gold ") + gold::get_version_string();
1293 size_t trailing_padding;
1294 Output_section *os = this->create_note("GNU", elfcpp::NT_GNU_GOLD_VERSION,
1295 desc.size(), false, &trailing_padding);
1297 Output_section_data* posd = new Output_data_const(desc, 4);
1298 os->add_output_section_data(posd);
1300 if (trailing_padding > 0)
1302 posd = new Output_data_fixed_space(trailing_padding, 0);
1303 os->add_output_section_data(posd);
1307 // Record whether the stack should be executable. This can be set
1308 // from the command line using the -z execstack or -z noexecstack
1309 // options. Otherwise, if any input file has a .note.GNU-stack
1310 // section with the SHF_EXECINSTR flag set, the stack should be
1311 // executable. Otherwise, if at least one input file a
1312 // .note.GNU-stack section, and some input file has no .note.GNU-stack
1313 // section, we use the target default for whether the stack should be
1314 // executable. Otherwise, we don't generate a stack note. When
1315 // generating a object file, we create a .note.GNU-stack section with
1316 // the appropriate marking. When generating an executable or shared
1317 // library, we create a PT_GNU_STACK segment.
1319 void
1320 Layout::create_executable_stack_info(const Target* target)
1322 bool is_stack_executable;
1323 if (this->options_.is_execstack_set())
1324 is_stack_executable = this->options_.is_stack_executable();
1325 else if (!this->input_with_gnu_stack_note_)
1326 return;
1327 else
1329 if (this->input_requires_executable_stack_)
1330 is_stack_executable = true;
1331 else if (this->input_without_gnu_stack_note_)
1332 is_stack_executable = target->is_default_stack_executable();
1333 else
1334 is_stack_executable = false;
1337 if (parameters->options().relocatable())
1339 const char* name = this->namepool_.add(".note.GNU-stack", false, NULL);
1340 elfcpp::Elf_Xword flags = 0;
1341 if (is_stack_executable)
1342 flags |= elfcpp::SHF_EXECINSTR;
1343 this->make_output_section(name, elfcpp::SHT_PROGBITS, flags);
1345 else
1347 if (this->script_options_->saw_phdrs_clause())
1348 return;
1349 int flags = elfcpp::PF_R | elfcpp::PF_W;
1350 if (is_stack_executable)
1351 flags |= elfcpp::PF_X;
1352 this->make_output_segment(elfcpp::PT_GNU_STACK, flags);
1356 // If --build-id was used, set up the build ID note.
1358 void
1359 Layout::create_build_id()
1361 if (!parameters->options().user_set_build_id())
1362 return;
1364 const char* style = parameters->options().build_id();
1365 if (strcmp(style, "none") == 0)
1366 return;
1368 // Set DESCSZ to the size of the note descriptor. When possible,
1369 // set DESC to the note descriptor contents.
1370 size_t descsz;
1371 std::string desc;
1372 if (strcmp(style, "md5") == 0)
1373 descsz = 128 / 8;
1374 else if (strcmp(style, "sha1") == 0)
1375 descsz = 160 / 8;
1376 else if (strcmp(style, "uuid") == 0)
1378 const size_t uuidsz = 128 / 8;
1380 char buffer[uuidsz];
1381 memset(buffer, 0, uuidsz);
1383 int descriptor = ::open("/dev/urandom", O_RDONLY);
1384 if (descriptor < 0)
1385 gold_error(_("--build-id=uuid failed: could not open /dev/urandom: %s"),
1386 strerror(errno));
1387 else
1389 ssize_t got = ::read(descriptor, buffer, uuidsz);
1390 ::close(descriptor);
1391 if (got < 0)
1392 gold_error(_("/dev/urandom: read failed: %s"), strerror(errno));
1393 else if (static_cast<size_t>(got) != uuidsz)
1394 gold_error(_("/dev/urandom: expected %zu bytes, got %zd bytes"),
1395 uuidsz, got);
1398 desc.assign(buffer, uuidsz);
1399 descsz = uuidsz;
1401 else if (strncmp(style, "0x", 2) == 0)
1403 hex_init();
1404 const char* p = style + 2;
1405 while (*p != '\0')
1407 if (hex_p(p[0]) && hex_p(p[1]))
1409 char c = (hex_value(p[0]) << 4) | hex_value(p[1]);
1410 desc += c;
1411 p += 2;
1413 else if (*p == '-' || *p == ':')
1414 ++p;
1415 else
1416 gold_fatal(_("--build-id argument '%s' not a valid hex number"),
1417 style);
1419 descsz = desc.size();
1421 else
1422 gold_fatal(_("unrecognized --build-id argument '%s'"), style);
1424 // Create the note.
1425 size_t trailing_padding;
1426 Output_section* os = this->create_note("GNU", elfcpp::NT_GNU_BUILD_ID,
1427 descsz, true, &trailing_padding);
1429 if (!desc.empty())
1431 // We know the value already, so we fill it in now.
1432 gold_assert(desc.size() == descsz);
1434 Output_section_data* posd = new Output_data_const(desc, 4);
1435 os->add_output_section_data(posd);
1437 if (trailing_padding != 0)
1439 posd = new Output_data_fixed_space(trailing_padding, 0);
1440 os->add_output_section_data(posd);
1443 else
1445 // We need to compute a checksum after we have completed the
1446 // link.
1447 gold_assert(trailing_padding == 0);
1448 this->build_id_note_ = new Output_data_fixed_space(descsz, 4);
1449 os->add_output_section_data(this->build_id_note_);
1450 os->set_after_input_sections();
1454 // Return whether SEG1 should be before SEG2 in the output file. This
1455 // is based entirely on the segment type and flags. When this is
1456 // called the segment addresses has normally not yet been set.
1458 bool
1459 Layout::segment_precedes(const Output_segment* seg1,
1460 const Output_segment* seg2)
1462 elfcpp::Elf_Word type1 = seg1->type();
1463 elfcpp::Elf_Word type2 = seg2->type();
1465 // The single PT_PHDR segment is required to precede any loadable
1466 // segment. We simply make it always first.
1467 if (type1 == elfcpp::PT_PHDR)
1469 gold_assert(type2 != elfcpp::PT_PHDR);
1470 return true;
1472 if (type2 == elfcpp::PT_PHDR)
1473 return false;
1475 // The single PT_INTERP segment is required to precede any loadable
1476 // segment. We simply make it always second.
1477 if (type1 == elfcpp::PT_INTERP)
1479 gold_assert(type2 != elfcpp::PT_INTERP);
1480 return true;
1482 if (type2 == elfcpp::PT_INTERP)
1483 return false;
1485 // We then put PT_LOAD segments before any other segments.
1486 if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD)
1487 return true;
1488 if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD)
1489 return false;
1491 // We put the PT_TLS segment last, because that is where the dynamic
1492 // linker expects to find it (this is just for efficiency; other
1493 // positions would also work correctly).
1494 if (type1 == elfcpp::PT_TLS && type2 != elfcpp::PT_TLS)
1495 return false;
1496 if (type2 == elfcpp::PT_TLS && type1 != elfcpp::PT_TLS)
1497 return true;
1499 const elfcpp::Elf_Word flags1 = seg1->flags();
1500 const elfcpp::Elf_Word flags2 = seg2->flags();
1502 // The order of non-PT_LOAD segments is unimportant. We simply sort
1503 // by the numeric segment type and flags values. There should not
1504 // be more than one segment with the same type and flags.
1505 if (type1 != elfcpp::PT_LOAD)
1507 if (type1 != type2)
1508 return type1 < type2;
1509 gold_assert(flags1 != flags2);
1510 return flags1 < flags2;
1513 // If the addresses are set already, sort by load address.
1514 if (seg1->are_addresses_set())
1516 if (!seg2->are_addresses_set())
1517 return true;
1519 unsigned int section_count1 = seg1->output_section_count();
1520 unsigned int section_count2 = seg2->output_section_count();
1521 if (section_count1 == 0 && section_count2 > 0)
1522 return true;
1523 if (section_count1 > 0 && section_count2 == 0)
1524 return false;
1526 uint64_t paddr1 = seg1->first_section_load_address();
1527 uint64_t paddr2 = seg2->first_section_load_address();
1528 if (paddr1 != paddr2)
1529 return paddr1 < paddr2;
1531 else if (seg2->are_addresses_set())
1532 return false;
1534 // We sort PT_LOAD segments based on the flags. Readonly segments
1535 // come before writable segments. Then writable segments with data
1536 // come before writable segments without data. Then executable
1537 // segments come before non-executable segments. Then the unlikely
1538 // case of a non-readable segment comes before the normal case of a
1539 // readable segment. If there are multiple segments with the same
1540 // type and flags, we require that the address be set, and we sort
1541 // by virtual address and then physical address.
1542 if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W))
1543 return (flags1 & elfcpp::PF_W) == 0;
1544 if ((flags1 & elfcpp::PF_W) != 0
1545 && seg1->has_any_data_sections() != seg2->has_any_data_sections())
1546 return seg1->has_any_data_sections();
1547 if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X))
1548 return (flags1 & elfcpp::PF_X) != 0;
1549 if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R))
1550 return (flags1 & elfcpp::PF_R) == 0;
1552 // We shouldn't get here--we shouldn't create segments which we
1553 // can't distinguish.
1554 gold_unreachable();
1557 // Set the file offsets of all the segments, and all the sections they
1558 // contain. They have all been created. LOAD_SEG must be be laid out
1559 // first. Return the offset of the data to follow.
1561 off_t
1562 Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
1563 unsigned int *pshndx)
1565 // Sort them into the final order.
1566 std::sort(this->segment_list_.begin(), this->segment_list_.end(),
1567 Layout::Compare_segments());
1569 // Find the PT_LOAD segments, and set their addresses and offsets
1570 // and their section's addresses and offsets.
1571 uint64_t addr;
1572 if (this->options_.user_set_Ttext())
1573 addr = this->options_.Ttext();
1574 else if (parameters->options().shared())
1575 addr = 0;
1576 else
1577 addr = target->default_text_segment_address();
1578 off_t off = 0;
1580 // If LOAD_SEG is NULL, then the file header and segment headers
1581 // will not be loadable. But they still need to be at offset 0 in
1582 // the file. Set their offsets now.
1583 if (load_seg == NULL)
1585 for (Data_list::iterator p = this->special_output_list_.begin();
1586 p != this->special_output_list_.end();
1587 ++p)
1589 off = align_address(off, (*p)->addralign());
1590 (*p)->set_address_and_file_offset(0, off);
1591 off += (*p)->data_size();
1595 bool was_readonly = false;
1596 for (Segment_list::iterator p = this->segment_list_.begin();
1597 p != this->segment_list_.end();
1598 ++p)
1600 if ((*p)->type() == elfcpp::PT_LOAD)
1602 if (load_seg != NULL && load_seg != *p)
1603 gold_unreachable();
1604 load_seg = NULL;
1606 bool are_addresses_set = (*p)->are_addresses_set();
1607 if (are_addresses_set)
1609 // When it comes to setting file offsets, we care about
1610 // the physical address.
1611 addr = (*p)->paddr();
1613 else if (this->options_.user_set_Tdata()
1614 && ((*p)->flags() & elfcpp::PF_W) != 0
1615 && (!this->options_.user_set_Tbss()
1616 || (*p)->has_any_data_sections()))
1618 addr = this->options_.Tdata();
1619 are_addresses_set = true;
1621 else if (this->options_.user_set_Tbss()
1622 && ((*p)->flags() & elfcpp::PF_W) != 0
1623 && !(*p)->has_any_data_sections())
1625 addr = this->options_.Tbss();
1626 are_addresses_set = true;
1629 uint64_t orig_addr = addr;
1630 uint64_t orig_off = off;
1632 uint64_t aligned_addr = 0;
1633 uint64_t abi_pagesize = target->abi_pagesize();
1635 // FIXME: This should depend on the -n and -N options.
1636 (*p)->set_minimum_p_align(target->common_pagesize());
1638 if (are_addresses_set)
1640 // Adjust the file offset to the same address modulo the
1641 // page size.
1642 uint64_t unsigned_off = off;
1643 uint64_t aligned_off = ((unsigned_off & ~(abi_pagesize - 1))
1644 | (addr & (abi_pagesize - 1)));
1645 if (aligned_off < unsigned_off)
1646 aligned_off += abi_pagesize;
1647 off = aligned_off;
1649 else
1651 // If the last segment was readonly, and this one is
1652 // not, then skip the address forward one page,
1653 // maintaining the same position within the page. This
1654 // lets us store both segments overlapping on a single
1655 // page in the file, but the loader will put them on
1656 // different pages in memory.
1658 addr = align_address(addr, (*p)->maximum_alignment());
1659 aligned_addr = addr;
1661 if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0)
1663 if ((addr & (abi_pagesize - 1)) != 0)
1664 addr = addr + abi_pagesize;
1667 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
1670 unsigned int shndx_hold = *pshndx;
1671 uint64_t new_addr = (*p)->set_section_addresses(this, false, addr,
1672 &off, pshndx);
1674 // Now that we know the size of this segment, we may be able
1675 // to save a page in memory, at the cost of wasting some
1676 // file space, by instead aligning to the start of a new
1677 // page. Here we use the real machine page size rather than
1678 // the ABI mandated page size.
1680 if (!are_addresses_set && aligned_addr != addr)
1682 uint64_t common_pagesize = target->common_pagesize();
1683 uint64_t first_off = (common_pagesize
1684 - (aligned_addr
1685 & (common_pagesize - 1)));
1686 uint64_t last_off = new_addr & (common_pagesize - 1);
1687 if (first_off > 0
1688 && last_off > 0
1689 && ((aligned_addr & ~ (common_pagesize - 1))
1690 != (new_addr & ~ (common_pagesize - 1)))
1691 && first_off + last_off <= common_pagesize)
1693 *pshndx = shndx_hold;
1694 addr = align_address(aligned_addr, common_pagesize);
1695 addr = align_address(addr, (*p)->maximum_alignment());
1696 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
1697 new_addr = (*p)->set_section_addresses(this, true, addr,
1698 &off, pshndx);
1702 addr = new_addr;
1704 if (((*p)->flags() & elfcpp::PF_W) == 0)
1705 was_readonly = true;
1709 // Handle the non-PT_LOAD segments, setting their offsets from their
1710 // section's offsets.
1711 for (Segment_list::iterator p = this->segment_list_.begin();
1712 p != this->segment_list_.end();
1713 ++p)
1715 if ((*p)->type() != elfcpp::PT_LOAD)
1716 (*p)->set_offset();
1719 // Set the TLS offsets for each section in the PT_TLS segment.
1720 if (this->tls_segment_ != NULL)
1721 this->tls_segment_->set_tls_offsets();
1723 return off;
1726 // Set the offsets of all the allocated sections when doing a
1727 // relocatable link. This does the same jobs as set_segment_offsets,
1728 // only for a relocatable link.
1730 off_t
1731 Layout::set_relocatable_section_offsets(Output_data* file_header,
1732 unsigned int *pshndx)
1734 off_t off = 0;
1736 file_header->set_address_and_file_offset(0, 0);
1737 off += file_header->data_size();
1739 for (Section_list::iterator p = this->section_list_.begin();
1740 p != this->section_list_.end();
1741 ++p)
1743 // We skip unallocated sections here, except that group sections
1744 // have to come first.
1745 if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
1746 && (*p)->type() != elfcpp::SHT_GROUP)
1747 continue;
1749 off = align_address(off, (*p)->addralign());
1751 // The linker script might have set the address.
1752 if (!(*p)->is_address_valid())
1753 (*p)->set_address(0);
1754 (*p)->set_file_offset(off);
1755 (*p)->finalize_data_size();
1756 off += (*p)->data_size();
1758 (*p)->set_out_shndx(*pshndx);
1759 ++*pshndx;
1762 return off;
1765 // Set the file offset of all the sections not associated with a
1766 // segment.
1768 off_t
1769 Layout::set_section_offsets(off_t off, Layout::Section_offset_pass pass)
1771 for (Section_list::iterator p = this->unattached_section_list_.begin();
1772 p != this->unattached_section_list_.end();
1773 ++p)
1775 // The symtab section is handled in create_symtab_sections.
1776 if (*p == this->symtab_section_)
1777 continue;
1779 // If we've already set the data size, don't set it again.
1780 if ((*p)->is_offset_valid() && (*p)->is_data_size_valid())
1781 continue;
1783 if (pass == BEFORE_INPUT_SECTIONS_PASS
1784 && (*p)->requires_postprocessing())
1786 (*p)->create_postprocessing_buffer();
1787 this->any_postprocessing_sections_ = true;
1790 if (pass == BEFORE_INPUT_SECTIONS_PASS
1791 && (*p)->after_input_sections())
1792 continue;
1793 else if (pass == POSTPROCESSING_SECTIONS_PASS
1794 && (!(*p)->after_input_sections()
1795 || (*p)->type() == elfcpp::SHT_STRTAB))
1796 continue;
1797 else if (pass == STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
1798 && (!(*p)->after_input_sections()
1799 || (*p)->type() != elfcpp::SHT_STRTAB))
1800 continue;
1802 off = align_address(off, (*p)->addralign());
1803 (*p)->set_file_offset(off);
1804 (*p)->finalize_data_size();
1805 off += (*p)->data_size();
1807 // At this point the name must be set.
1808 if (pass != STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS)
1809 this->namepool_.add((*p)->name(), false, NULL);
1811 return off;
1814 // Set the section indexes of all the sections not associated with a
1815 // segment.
1817 unsigned int
1818 Layout::set_section_indexes(unsigned int shndx)
1820 const bool output_is_object = parameters->options().relocatable();
1821 for (Section_list::iterator p = this->unattached_section_list_.begin();
1822 p != this->unattached_section_list_.end();
1823 ++p)
1825 // In a relocatable link, we already did group sections.
1826 if (output_is_object
1827 && (*p)->type() == elfcpp::SHT_GROUP)
1828 continue;
1830 (*p)->set_out_shndx(shndx);
1831 ++shndx;
1833 return shndx;
1836 // Set the section addresses according to the linker script. This is
1837 // only called when we see a SECTIONS clause. This returns the
1838 // program segment which should hold the file header and segment
1839 // headers, if any. It will return NULL if they should not be in a
1840 // segment.
1842 Output_segment*
1843 Layout::set_section_addresses_from_script(Symbol_table* symtab)
1845 Script_sections* ss = this->script_options_->script_sections();
1846 gold_assert(ss->saw_sections_clause());
1848 // Place each orphaned output section in the script.
1849 for (Section_list::iterator p = this->section_list_.begin();
1850 p != this->section_list_.end();
1851 ++p)
1853 if (!(*p)->found_in_sections_clause())
1854 ss->place_orphan(*p);
1857 return this->script_options_->set_section_addresses(symtab, this);
1860 // Count the local symbols in the regular symbol table and the dynamic
1861 // symbol table, and build the respective string pools.
1863 void
1864 Layout::count_local_symbols(const Task* task,
1865 const Input_objects* input_objects)
1867 // First, figure out an upper bound on the number of symbols we'll
1868 // be inserting into each pool. This helps us create the pools with
1869 // the right size, to avoid unnecessary hashtable resizing.
1870 unsigned int symbol_count = 0;
1871 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
1872 p != input_objects->relobj_end();
1873 ++p)
1874 symbol_count += (*p)->local_symbol_count();
1876 // Go from "upper bound" to "estimate." We overcount for two
1877 // reasons: we double-count symbols that occur in more than one
1878 // object file, and we count symbols that are dropped from the
1879 // output. Add it all together and assume we overcount by 100%.
1880 symbol_count /= 2;
1882 // We assume all symbols will go into both the sympool and dynpool.
1883 this->sympool_.reserve(symbol_count);
1884 this->dynpool_.reserve(symbol_count);
1886 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
1887 p != input_objects->relobj_end();
1888 ++p)
1890 Task_lock_obj<Object> tlo(task, *p);
1891 (*p)->count_local_symbols(&this->sympool_, &this->dynpool_);
1895 // Create the symbol table sections. Here we also set the final
1896 // values of the symbols. At this point all the loadable sections are
1897 // fully laid out.
1899 void
1900 Layout::create_symtab_sections(const Input_objects* input_objects,
1901 Symbol_table* symtab,
1902 off_t* poff)
1904 int symsize;
1905 unsigned int align;
1906 if (parameters->target().get_size() == 32)
1908 symsize = elfcpp::Elf_sizes<32>::sym_size;
1909 align = 4;
1911 else if (parameters->target().get_size() == 64)
1913 symsize = elfcpp::Elf_sizes<64>::sym_size;
1914 align = 8;
1916 else
1917 gold_unreachable();
1919 off_t off = *poff;
1920 off = align_address(off, align);
1921 off_t startoff = off;
1923 // Save space for the dummy symbol at the start of the section. We
1924 // never bother to write this out--it will just be left as zero.
1925 off += symsize;
1926 unsigned int local_symbol_index = 1;
1928 // Add STT_SECTION symbols for each Output section which needs one.
1929 for (Section_list::iterator p = this->section_list_.begin();
1930 p != this->section_list_.end();
1931 ++p)
1933 if (!(*p)->needs_symtab_index())
1934 (*p)->set_symtab_index(-1U);
1935 else
1937 (*p)->set_symtab_index(local_symbol_index);
1938 ++local_symbol_index;
1939 off += symsize;
1943 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
1944 p != input_objects->relobj_end();
1945 ++p)
1947 unsigned int index = (*p)->finalize_local_symbols(local_symbol_index,
1948 off);
1949 off += (index - local_symbol_index) * symsize;
1950 local_symbol_index = index;
1953 unsigned int local_symcount = local_symbol_index;
1954 gold_assert(local_symcount * symsize == off - startoff);
1956 off_t dynoff;
1957 size_t dyn_global_index;
1958 size_t dyncount;
1959 if (this->dynsym_section_ == NULL)
1961 dynoff = 0;
1962 dyn_global_index = 0;
1963 dyncount = 0;
1965 else
1967 dyn_global_index = this->dynsym_section_->info();
1968 off_t locsize = dyn_global_index * this->dynsym_section_->entsize();
1969 dynoff = this->dynsym_section_->offset() + locsize;
1970 dyncount = (this->dynsym_section_->data_size() - locsize) / symsize;
1971 gold_assert(static_cast<off_t>(dyncount * symsize)
1972 == this->dynsym_section_->data_size() - locsize);
1975 off = symtab->finalize(off, dynoff, dyn_global_index, dyncount,
1976 &this->sympool_, &local_symcount);
1978 if (!parameters->options().strip_all())
1980 this->sympool_.set_string_offsets();
1982 const char* symtab_name = this->namepool_.add(".symtab", false, NULL);
1983 Output_section* osymtab = this->make_output_section(symtab_name,
1984 elfcpp::SHT_SYMTAB,
1986 this->symtab_section_ = osymtab;
1988 Output_section_data* pos = new Output_data_fixed_space(off - startoff,
1989 align);
1990 osymtab->add_output_section_data(pos);
1992 const char* strtab_name = this->namepool_.add(".strtab", false, NULL);
1993 Output_section* ostrtab = this->make_output_section(strtab_name,
1994 elfcpp::SHT_STRTAB,
1997 Output_section_data* pstr = new Output_data_strtab(&this->sympool_);
1998 ostrtab->add_output_section_data(pstr);
2000 osymtab->set_file_offset(startoff);
2001 osymtab->finalize_data_size();
2002 osymtab->set_link_section(ostrtab);
2003 osymtab->set_info(local_symcount);
2004 osymtab->set_entsize(symsize);
2006 *poff = off;
2010 // Create the .shstrtab section, which holds the names of the
2011 // sections. At the time this is called, we have created all the
2012 // output sections except .shstrtab itself.
2014 Output_section*
2015 Layout::create_shstrtab()
2017 // FIXME: We don't need to create a .shstrtab section if we are
2018 // stripping everything.
2020 const char* name = this->namepool_.add(".shstrtab", false, NULL);
2022 Output_section* os = this->make_output_section(name, elfcpp::SHT_STRTAB, 0);
2024 // We can't write out this section until we've set all the section
2025 // names, and we don't set the names of compressed output sections
2026 // until relocations are complete.
2027 os->set_after_input_sections();
2029 Output_section_data* posd = new Output_data_strtab(&this->namepool_);
2030 os->add_output_section_data(posd);
2032 return os;
2035 // Create the section headers. SIZE is 32 or 64. OFF is the file
2036 // offset.
2038 void
2039 Layout::create_shdrs(off_t* poff)
2041 Output_section_headers* oshdrs;
2042 oshdrs = new Output_section_headers(this,
2043 &this->segment_list_,
2044 &this->section_list_,
2045 &this->unattached_section_list_,
2046 &this->namepool_);
2047 off_t off = align_address(*poff, oshdrs->addralign());
2048 oshdrs->set_address_and_file_offset(0, off);
2049 off += oshdrs->data_size();
2050 *poff = off;
2051 this->section_headers_ = oshdrs;
2054 // Create the dynamic symbol table.
2056 void
2057 Layout::create_dynamic_symtab(const Input_objects* input_objects,
2058 Symbol_table* symtab,
2059 Output_section **pdynstr,
2060 unsigned int* plocal_dynamic_count,
2061 std::vector<Symbol*>* pdynamic_symbols,
2062 Versions* pversions)
2064 // Count all the symbols in the dynamic symbol table, and set the
2065 // dynamic symbol indexes.
2067 // Skip symbol 0, which is always all zeroes.
2068 unsigned int index = 1;
2070 // Add STT_SECTION symbols for each Output section which needs one.
2071 for (Section_list::iterator p = this->section_list_.begin();
2072 p != this->section_list_.end();
2073 ++p)
2075 if (!(*p)->needs_dynsym_index())
2076 (*p)->set_dynsym_index(-1U);
2077 else
2079 (*p)->set_dynsym_index(index);
2080 ++index;
2084 // Count the local symbols that need to go in the dynamic symbol table,
2085 // and set the dynamic symbol indexes.
2086 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
2087 p != input_objects->relobj_end();
2088 ++p)
2090 unsigned int new_index = (*p)->set_local_dynsym_indexes(index);
2091 index = new_index;
2094 unsigned int local_symcount = index;
2095 *plocal_dynamic_count = local_symcount;
2097 // FIXME: We have to tell set_dynsym_indexes whether the
2098 // -E/--export-dynamic option was used.
2099 index = symtab->set_dynsym_indexes(index, pdynamic_symbols,
2100 &this->dynpool_, pversions);
2102 int symsize;
2103 unsigned int align;
2104 const int size = parameters->target().get_size();
2105 if (size == 32)
2107 symsize = elfcpp::Elf_sizes<32>::sym_size;
2108 align = 4;
2110 else if (size == 64)
2112 symsize = elfcpp::Elf_sizes<64>::sym_size;
2113 align = 8;
2115 else
2116 gold_unreachable();
2118 // Create the dynamic symbol table section.
2120 Output_section* dynsym = this->choose_output_section(NULL, ".dynsym",
2121 elfcpp::SHT_DYNSYM,
2122 elfcpp::SHF_ALLOC,
2123 false);
2125 Output_section_data* odata = new Output_data_fixed_space(index * symsize,
2126 align);
2127 dynsym->add_output_section_data(odata);
2129 dynsym->set_info(local_symcount);
2130 dynsym->set_entsize(symsize);
2131 dynsym->set_addralign(align);
2133 this->dynsym_section_ = dynsym;
2135 Output_data_dynamic* const odyn = this->dynamic_data_;
2136 odyn->add_section_address(elfcpp::DT_SYMTAB, dynsym);
2137 odyn->add_constant(elfcpp::DT_SYMENT, symsize);
2139 // Create the dynamic string table section.
2141 Output_section* dynstr = this->choose_output_section(NULL, ".dynstr",
2142 elfcpp::SHT_STRTAB,
2143 elfcpp::SHF_ALLOC,
2144 false);
2146 Output_section_data* strdata = new Output_data_strtab(&this->dynpool_);
2147 dynstr->add_output_section_data(strdata);
2149 dynsym->set_link_section(dynstr);
2150 this->dynamic_section_->set_link_section(dynstr);
2152 odyn->add_section_address(elfcpp::DT_STRTAB, dynstr);
2153 odyn->add_section_size(elfcpp::DT_STRSZ, dynstr);
2155 *pdynstr = dynstr;
2157 // Create the hash tables.
2159 if (strcmp(parameters->options().hash_style(), "sysv") == 0
2160 || strcmp(parameters->options().hash_style(), "both") == 0)
2162 unsigned char* phash;
2163 unsigned int hashlen;
2164 Dynobj::create_elf_hash_table(*pdynamic_symbols, local_symcount,
2165 &phash, &hashlen);
2167 Output_section* hashsec = this->choose_output_section(NULL, ".hash",
2168 elfcpp::SHT_HASH,
2169 elfcpp::SHF_ALLOC,
2170 false);
2172 Output_section_data* hashdata = new Output_data_const_buffer(phash,
2173 hashlen,
2174 align);
2175 hashsec->add_output_section_data(hashdata);
2177 hashsec->set_link_section(dynsym);
2178 hashsec->set_entsize(4);
2180 odyn->add_section_address(elfcpp::DT_HASH, hashsec);
2183 if (strcmp(parameters->options().hash_style(), "gnu") == 0
2184 || strcmp(parameters->options().hash_style(), "both") == 0)
2186 unsigned char* phash;
2187 unsigned int hashlen;
2188 Dynobj::create_gnu_hash_table(*pdynamic_symbols, local_symcount,
2189 &phash, &hashlen);
2191 Output_section* hashsec = this->choose_output_section(NULL, ".gnu.hash",
2192 elfcpp::SHT_GNU_HASH,
2193 elfcpp::SHF_ALLOC,
2194 false);
2196 Output_section_data* hashdata = new Output_data_const_buffer(phash,
2197 hashlen,
2198 align);
2199 hashsec->add_output_section_data(hashdata);
2201 hashsec->set_link_section(dynsym);
2202 hashsec->set_entsize(4);
2204 odyn->add_section_address(elfcpp::DT_GNU_HASH, hashsec);
2208 // Assign offsets to each local portion of the dynamic symbol table.
2210 void
2211 Layout::assign_local_dynsym_offsets(const Input_objects* input_objects)
2213 Output_section* dynsym = this->dynsym_section_;
2214 gold_assert(dynsym != NULL);
2216 off_t off = dynsym->offset();
2218 // Skip the dummy symbol at the start of the section.
2219 off += dynsym->entsize();
2221 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
2222 p != input_objects->relobj_end();
2223 ++p)
2225 unsigned int count = (*p)->set_local_dynsym_offset(off);
2226 off += count * dynsym->entsize();
2230 // Create the version sections.
2232 void
2233 Layout::create_version_sections(const Versions* versions,
2234 const Symbol_table* symtab,
2235 unsigned int local_symcount,
2236 const std::vector<Symbol*>& dynamic_symbols,
2237 const Output_section* dynstr)
2239 if (!versions->any_defs() && !versions->any_needs())
2240 return;
2242 switch (parameters->size_and_endianness())
2244 #ifdef HAVE_TARGET_32_LITTLE
2245 case Parameters::TARGET_32_LITTLE:
2246 this->sized_create_version_sections<32, false>(versions, symtab,
2247 local_symcount,
2248 dynamic_symbols, dynstr);
2249 break;
2250 #endif
2251 #ifdef HAVE_TARGET_32_BIG
2252 case Parameters::TARGET_32_BIG:
2253 this->sized_create_version_sections<32, true>(versions, symtab,
2254 local_symcount,
2255 dynamic_symbols, dynstr);
2256 break;
2257 #endif
2258 #ifdef HAVE_TARGET_64_LITTLE
2259 case Parameters::TARGET_64_LITTLE:
2260 this->sized_create_version_sections<64, false>(versions, symtab,
2261 local_symcount,
2262 dynamic_symbols, dynstr);
2263 break;
2264 #endif
2265 #ifdef HAVE_TARGET_64_BIG
2266 case Parameters::TARGET_64_BIG:
2267 this->sized_create_version_sections<64, true>(versions, symtab,
2268 local_symcount,
2269 dynamic_symbols, dynstr);
2270 break;
2271 #endif
2272 default:
2273 gold_unreachable();
2277 // Create the version sections, sized version.
2279 template<int size, bool big_endian>
2280 void
2281 Layout::sized_create_version_sections(
2282 const Versions* versions,
2283 const Symbol_table* symtab,
2284 unsigned int local_symcount,
2285 const std::vector<Symbol*>& dynamic_symbols,
2286 const Output_section* dynstr)
2288 Output_section* vsec = this->choose_output_section(NULL, ".gnu.version",
2289 elfcpp::SHT_GNU_versym,
2290 elfcpp::SHF_ALLOC,
2291 false);
2293 unsigned char* vbuf;
2294 unsigned int vsize;
2295 versions->symbol_section_contents<size, big_endian>(symtab, &this->dynpool_,
2296 local_symcount,
2297 dynamic_symbols,
2298 &vbuf, &vsize);
2300 Output_section_data* vdata = new Output_data_const_buffer(vbuf, vsize, 2);
2302 vsec->add_output_section_data(vdata);
2303 vsec->set_entsize(2);
2304 vsec->set_link_section(this->dynsym_section_);
2306 Output_data_dynamic* const odyn = this->dynamic_data_;
2307 odyn->add_section_address(elfcpp::DT_VERSYM, vsec);
2309 if (versions->any_defs())
2311 Output_section* vdsec;
2312 vdsec= this->choose_output_section(NULL, ".gnu.version_d",
2313 elfcpp::SHT_GNU_verdef,
2314 elfcpp::SHF_ALLOC,
2315 false);
2317 unsigned char* vdbuf;
2318 unsigned int vdsize;
2319 unsigned int vdentries;
2320 versions->def_section_contents<size, big_endian>(&this->dynpool_, &vdbuf,
2321 &vdsize, &vdentries);
2323 Output_section_data* vddata = new Output_data_const_buffer(vdbuf,
2324 vdsize,
2327 vdsec->add_output_section_data(vddata);
2328 vdsec->set_link_section(dynstr);
2329 vdsec->set_info(vdentries);
2331 odyn->add_section_address(elfcpp::DT_VERDEF, vdsec);
2332 odyn->add_constant(elfcpp::DT_VERDEFNUM, vdentries);
2335 if (versions->any_needs())
2337 Output_section* vnsec;
2338 vnsec = this->choose_output_section(NULL, ".gnu.version_r",
2339 elfcpp::SHT_GNU_verneed,
2340 elfcpp::SHF_ALLOC,
2341 false);
2343 unsigned char* vnbuf;
2344 unsigned int vnsize;
2345 unsigned int vnentries;
2346 versions->need_section_contents<size, big_endian>(&this->dynpool_,
2347 &vnbuf, &vnsize,
2348 &vnentries);
2350 Output_section_data* vndata = new Output_data_const_buffer(vnbuf,
2351 vnsize,
2354 vnsec->add_output_section_data(vndata);
2355 vnsec->set_link_section(dynstr);
2356 vnsec->set_info(vnentries);
2358 odyn->add_section_address(elfcpp::DT_VERNEED, vnsec);
2359 odyn->add_constant(elfcpp::DT_VERNEEDNUM, vnentries);
2363 // Create the .interp section and PT_INTERP segment.
2365 void
2366 Layout::create_interp(const Target* target)
2368 const char* interp = this->options_.dynamic_linker();
2369 if (interp == NULL)
2371 interp = target->dynamic_linker();
2372 gold_assert(interp != NULL);
2375 size_t len = strlen(interp) + 1;
2377 Output_section_data* odata = new Output_data_const(interp, len, 1);
2379 Output_section* osec = this->choose_output_section(NULL, ".interp",
2380 elfcpp::SHT_PROGBITS,
2381 elfcpp::SHF_ALLOC,
2382 false);
2383 osec->add_output_section_data(odata);
2385 if (!this->script_options_->saw_phdrs_clause())
2387 Output_segment* oseg = this->make_output_segment(elfcpp::PT_INTERP,
2388 elfcpp::PF_R);
2389 oseg->add_initial_output_section(osec, elfcpp::PF_R);
2393 // Finish the .dynamic section and PT_DYNAMIC segment.
2395 void
2396 Layout::finish_dynamic_section(const Input_objects* input_objects,
2397 const Symbol_table* symtab)
2399 if (!this->script_options_->saw_phdrs_clause())
2401 Output_segment* oseg = this->make_output_segment(elfcpp::PT_DYNAMIC,
2402 (elfcpp::PF_R
2403 | elfcpp::PF_W));
2404 oseg->add_initial_output_section(this->dynamic_section_,
2405 elfcpp::PF_R | elfcpp::PF_W);
2408 Output_data_dynamic* const odyn = this->dynamic_data_;
2410 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
2411 p != input_objects->dynobj_end();
2412 ++p)
2414 // FIXME: Handle --as-needed.
2415 odyn->add_string(elfcpp::DT_NEEDED, (*p)->soname());
2418 if (parameters->options().shared())
2420 const char* soname = this->options_.soname();
2421 if (soname != NULL)
2422 odyn->add_string(elfcpp::DT_SONAME, soname);
2425 // FIXME: Support --init and --fini.
2426 Symbol* sym = symtab->lookup("_init");
2427 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
2428 odyn->add_symbol(elfcpp::DT_INIT, sym);
2430 sym = symtab->lookup("_fini");
2431 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
2432 odyn->add_symbol(elfcpp::DT_FINI, sym);
2434 // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY.
2436 // Add a DT_RPATH entry if needed.
2437 const General_options::Dir_list& rpath(this->options_.rpath());
2438 if (!rpath.empty())
2440 std::string rpath_val;
2441 for (General_options::Dir_list::const_iterator p = rpath.begin();
2442 p != rpath.end();
2443 ++p)
2445 if (rpath_val.empty())
2446 rpath_val = p->name();
2447 else
2449 // Eliminate duplicates.
2450 General_options::Dir_list::const_iterator q;
2451 for (q = rpath.begin(); q != p; ++q)
2452 if (q->name() == p->name())
2453 break;
2454 if (q == p)
2456 rpath_val += ':';
2457 rpath_val += p->name();
2462 odyn->add_string(elfcpp::DT_RPATH, rpath_val);
2465 // Look for text segments that have dynamic relocations.
2466 bool have_textrel = false;
2467 if (!this->script_options_->saw_sections_clause())
2469 for (Segment_list::const_iterator p = this->segment_list_.begin();
2470 p != this->segment_list_.end();
2471 ++p)
2473 if (((*p)->flags() & elfcpp::PF_W) == 0
2474 && (*p)->dynamic_reloc_count() > 0)
2476 have_textrel = true;
2477 break;
2481 else
2483 // We don't know the section -> segment mapping, so we are
2484 // conservative and just look for readonly sections with
2485 // relocations. If those sections wind up in writable segments,
2486 // then we have created an unnecessary DT_TEXTREL entry.
2487 for (Section_list::const_iterator p = this->section_list_.begin();
2488 p != this->section_list_.end();
2489 ++p)
2491 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0
2492 && ((*p)->flags() & elfcpp::SHF_WRITE) == 0
2493 && ((*p)->dynamic_reloc_count() > 0))
2495 have_textrel = true;
2496 break;
2501 // Add a DT_FLAGS entry. We add it even if no flags are set so that
2502 // post-link tools can easily modify these flags if desired.
2503 unsigned int flags = 0;
2504 if (have_textrel)
2506 // Add a DT_TEXTREL for compatibility with older loaders.
2507 odyn->add_constant(elfcpp::DT_TEXTREL, 0);
2508 flags |= elfcpp::DF_TEXTREL;
2510 if (parameters->options().shared() && this->has_static_tls())
2511 flags |= elfcpp::DF_STATIC_TLS;
2512 odyn->add_constant(elfcpp::DT_FLAGS, flags);
2515 // The mapping of .gnu.linkonce section names to real section names.
2517 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
2518 const Layout::Linkonce_mapping Layout::linkonce_mapping[] =
2520 MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d".
2521 MAPPING_INIT("t", ".text"),
2522 MAPPING_INIT("r", ".rodata"),
2523 MAPPING_INIT("d", ".data"),
2524 MAPPING_INIT("b", ".bss"),
2525 MAPPING_INIT("s", ".sdata"),
2526 MAPPING_INIT("sb", ".sbss"),
2527 MAPPING_INIT("s2", ".sdata2"),
2528 MAPPING_INIT("sb2", ".sbss2"),
2529 MAPPING_INIT("wi", ".debug_info"),
2530 MAPPING_INIT("td", ".tdata"),
2531 MAPPING_INIT("tb", ".tbss"),
2532 MAPPING_INIT("lr", ".lrodata"),
2533 MAPPING_INIT("l", ".ldata"),
2534 MAPPING_INIT("lb", ".lbss"),
2536 #undef MAPPING_INIT
2538 const int Layout::linkonce_mapping_count =
2539 sizeof(Layout::linkonce_mapping) / sizeof(Layout::linkonce_mapping[0]);
2541 // Return the name of the output section to use for a .gnu.linkonce
2542 // section. This is based on the default ELF linker script of the old
2543 // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
2544 // to ".text". Set *PLEN to the length of the name. *PLEN is
2545 // initialized to the length of NAME.
2547 const char*
2548 Layout::linkonce_output_name(const char* name, size_t *plen)
2550 const char* s = name + sizeof(".gnu.linkonce") - 1;
2551 if (*s != '.')
2552 return name;
2553 ++s;
2554 const Linkonce_mapping* plm = linkonce_mapping;
2555 for (int i = 0; i < linkonce_mapping_count; ++i, ++plm)
2557 if (strncmp(s, plm->from, plm->fromlen) == 0 && s[plm->fromlen] == '.')
2559 *plen = plm->tolen;
2560 return plm->to;
2563 return name;
2566 // Choose the output section name to use given an input section name.
2567 // Set *PLEN to the length of the name. *PLEN is initialized to the
2568 // length of NAME.
2570 const char*
2571 Layout::output_section_name(const char* name, size_t* plen)
2573 if (Layout::is_linkonce(name))
2575 // .gnu.linkonce sections are laid out as though they were named
2576 // for the sections are placed into.
2577 return Layout::linkonce_output_name(name, plen);
2580 // gcc 4.3 generates the following sorts of section names when it
2581 // needs a section name specific to a function:
2582 // .text.FN
2583 // .rodata.FN
2584 // .sdata2.FN
2585 // .data.FN
2586 // .data.rel.FN
2587 // .data.rel.local.FN
2588 // .data.rel.ro.FN
2589 // .data.rel.ro.local.FN
2590 // .sdata.FN
2591 // .bss.FN
2592 // .sbss.FN
2593 // .tdata.FN
2594 // .tbss.FN
2596 // The GNU linker maps all of those to the part before the .FN,
2597 // except that .data.rel.local.FN is mapped to .data, and
2598 // .data.rel.ro.local.FN is mapped to .data.rel.ro. The sections
2599 // beginning with .data.rel.ro.local are grouped together.
2601 // For an anonymous namespace, the string FN can contain a '.'.
2603 // Also of interest: .rodata.strN.N, .rodata.cstN, both of which the
2604 // GNU linker maps to .rodata.
2606 // The .data.rel.ro sections enable a security feature triggered by
2607 // the -z relro option. Section which need to be relocated at
2608 // program startup time but which may be readonly after startup are
2609 // grouped into .data.rel.ro. They are then put into a PT_GNU_RELRO
2610 // segment. The dynamic linker will make that segment writable,
2611 // perform relocations, and then make it read-only. FIXME: We do
2612 // not yet implement this optimization.
2614 // It is hard to handle this in a principled way.
2616 // These are the rules we follow:
2618 // If the section name has no initial '.', or no dot other than an
2619 // initial '.', we use the name unchanged (i.e., "mysection" and
2620 // ".text" are unchanged).
2622 // If the name starts with ".data.rel.ro" we use ".data.rel.ro".
2624 // Otherwise, we drop the second '.' and everything that comes after
2625 // it (i.e., ".text.XXX" becomes ".text").
2627 const char* s = name;
2628 if (*s != '.')
2629 return name;
2630 ++s;
2631 const char* sdot = strchr(s, '.');
2632 if (sdot == NULL)
2633 return name;
2635 const char* const data_rel_ro = ".data.rel.ro";
2636 if (strncmp(name, data_rel_ro, strlen(data_rel_ro)) == 0)
2638 *plen = strlen(data_rel_ro);
2639 return data_rel_ro;
2642 *plen = sdot - name;
2643 return name;
2646 // Record the signature of a comdat section, and return whether to
2647 // include it in the link. If GROUP is true, this is a regular
2648 // section group. If GROUP is false, this is a group signature
2649 // derived from the name of a linkonce section. We want linkonce
2650 // signatures and group signatures to block each other, but we don't
2651 // want a linkonce signature to block another linkonce signature.
2653 bool
2654 Layout::add_comdat(const char* signature, bool group)
2656 std::string sig(signature);
2657 std::pair<Signatures::iterator, bool> ins(
2658 this->signatures_.insert(std::make_pair(sig, group)));
2660 if (ins.second)
2662 // This is the first time we've seen this signature.
2663 return true;
2666 if (ins.first->second)
2668 // We've already seen a real section group with this signature.
2669 return false;
2671 else if (group)
2673 // This is a real section group, and we've already seen a
2674 // linkonce section with this signature. Record that we've seen
2675 // a section group, and don't include this section group.
2676 ins.first->second = true;
2677 return false;
2679 else
2681 // We've already seen a linkonce section and this is a linkonce
2682 // section. These don't block each other--this may be the same
2683 // symbol name with different section types.
2684 return true;
2688 // Store the allocated sections into the section list.
2690 void
2691 Layout::get_allocated_sections(Section_list* section_list) const
2693 for (Section_list::const_iterator p = this->section_list_.begin();
2694 p != this->section_list_.end();
2695 ++p)
2696 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
2697 section_list->push_back(*p);
2700 // Create an output segment.
2702 Output_segment*
2703 Layout::make_output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
2705 gold_assert(!parameters->options().relocatable());
2706 Output_segment* oseg = new Output_segment(type, flags);
2707 this->segment_list_.push_back(oseg);
2708 return oseg;
2711 // Write out the Output_sections. Most won't have anything to write,
2712 // since most of the data will come from input sections which are
2713 // handled elsewhere. But some Output_sections do have Output_data.
2715 void
2716 Layout::write_output_sections(Output_file* of) const
2718 for (Section_list::const_iterator p = this->section_list_.begin();
2719 p != this->section_list_.end();
2720 ++p)
2722 if (!(*p)->after_input_sections())
2723 (*p)->write(of);
2727 // Write out data not associated with a section or the symbol table.
2729 void
2730 Layout::write_data(const Symbol_table* symtab, Output_file* of) const
2732 if (!parameters->options().strip_all())
2734 const Output_section* symtab_section = this->symtab_section_;
2735 for (Section_list::const_iterator p = this->section_list_.begin();
2736 p != this->section_list_.end();
2737 ++p)
2739 if ((*p)->needs_symtab_index())
2741 gold_assert(symtab_section != NULL);
2742 unsigned int index = (*p)->symtab_index();
2743 gold_assert(index > 0 && index != -1U);
2744 off_t off = (symtab_section->offset()
2745 + index * symtab_section->entsize());
2746 symtab->write_section_symbol(*p, of, off);
2751 const Output_section* dynsym_section = this->dynsym_section_;
2752 for (Section_list::const_iterator p = this->section_list_.begin();
2753 p != this->section_list_.end();
2754 ++p)
2756 if ((*p)->needs_dynsym_index())
2758 gold_assert(dynsym_section != NULL);
2759 unsigned int index = (*p)->dynsym_index();
2760 gold_assert(index > 0 && index != -1U);
2761 off_t off = (dynsym_section->offset()
2762 + index * dynsym_section->entsize());
2763 symtab->write_section_symbol(*p, of, off);
2767 // Write out the Output_data which are not in an Output_section.
2768 for (Data_list::const_iterator p = this->special_output_list_.begin();
2769 p != this->special_output_list_.end();
2770 ++p)
2771 (*p)->write(of);
2774 // Write out the Output_sections which can only be written after the
2775 // input sections are complete.
2777 void
2778 Layout::write_sections_after_input_sections(Output_file* of)
2780 // Determine the final section offsets, and thus the final output
2781 // file size. Note we finalize the .shstrab last, to allow the
2782 // after_input_section sections to modify their section-names before
2783 // writing.
2784 if (this->any_postprocessing_sections_)
2786 off_t off = this->output_file_size_;
2787 off = this->set_section_offsets(off, POSTPROCESSING_SECTIONS_PASS);
2789 // Now that we've finalized the names, we can finalize the shstrab.
2790 off =
2791 this->set_section_offsets(off,
2792 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS);
2794 if (off > this->output_file_size_)
2796 of->resize(off);
2797 this->output_file_size_ = off;
2801 for (Section_list::const_iterator p = this->section_list_.begin();
2802 p != this->section_list_.end();
2803 ++p)
2805 if ((*p)->after_input_sections())
2806 (*p)->write(of);
2809 this->section_headers_->write(of);
2812 // If the build ID requires computing a checksum, do so here, and
2813 // write it out. We compute a checksum over the entire file because
2814 // that is simplest.
2816 void
2817 Layout::write_build_id(Output_file* of) const
2819 if (this->build_id_note_ == NULL)
2820 return;
2822 const unsigned char* iv = of->get_input_view(0, this->output_file_size_);
2824 unsigned char* ov = of->get_output_view(this->build_id_note_->offset(),
2825 this->build_id_note_->data_size());
2827 const char* style = parameters->options().build_id();
2828 if (strcmp(style, "sha1") == 0)
2830 sha1_ctx ctx;
2831 sha1_init_ctx(&ctx);
2832 sha1_process_bytes(iv, this->output_file_size_, &ctx);
2833 sha1_finish_ctx(&ctx, ov);
2835 else if (strcmp(style, "md5") == 0)
2837 md5_ctx ctx;
2838 md5_init_ctx(&ctx);
2839 md5_process_bytes(iv, this->output_file_size_, &ctx);
2840 md5_finish_ctx(&ctx, ov);
2842 else
2843 gold_unreachable();
2845 of->write_output_view(this->build_id_note_->offset(),
2846 this->build_id_note_->data_size(),
2847 ov);
2849 of->free_input_view(0, this->output_file_size_, iv);
2852 // Write out a binary file. This is called after the link is
2853 // complete. IN is the temporary output file we used to generate the
2854 // ELF code. We simply walk through the segments, read them from
2855 // their file offset in IN, and write them to their load address in
2856 // the output file. FIXME: with a bit more work, we could support
2857 // S-records and/or Intel hex format here.
2859 void
2860 Layout::write_binary(Output_file* in) const
2862 gold_assert(this->options_.oformat_enum()
2863 == General_options::OBJECT_FORMAT_BINARY);
2865 // Get the size of the binary file.
2866 uint64_t max_load_address = 0;
2867 for (Segment_list::const_iterator p = this->segment_list_.begin();
2868 p != this->segment_list_.end();
2869 ++p)
2871 if ((*p)->type() == elfcpp::PT_LOAD && (*p)->filesz() > 0)
2873 uint64_t max_paddr = (*p)->paddr() + (*p)->filesz();
2874 if (max_paddr > max_load_address)
2875 max_load_address = max_paddr;
2879 Output_file out(parameters->options().output_file_name());
2880 out.open(max_load_address);
2882 for (Segment_list::const_iterator p = this->segment_list_.begin();
2883 p != this->segment_list_.end();
2884 ++p)
2886 if ((*p)->type() == elfcpp::PT_LOAD && (*p)->filesz() > 0)
2888 const unsigned char* vin = in->get_input_view((*p)->offset(),
2889 (*p)->filesz());
2890 unsigned char* vout = out.get_output_view((*p)->paddr(),
2891 (*p)->filesz());
2892 memcpy(vout, vin, (*p)->filesz());
2893 out.write_output_view((*p)->paddr(), (*p)->filesz(), vout);
2894 in->free_input_view((*p)->offset(), (*p)->filesz(), vin);
2898 out.close();
2901 // Print statistical information to stderr. This is used for --stats.
2903 void
2904 Layout::print_stats() const
2906 this->namepool_.print_stats("section name pool");
2907 this->sympool_.print_stats("output symbol name pool");
2908 this->dynpool_.print_stats("dynamic name pool");
2910 for (Section_list::const_iterator p = this->section_list_.begin();
2911 p != this->section_list_.end();
2912 ++p)
2913 (*p)->print_merge_stats();
2916 // Write_sections_task methods.
2918 // We can always run this task.
2920 Task_token*
2921 Write_sections_task::is_runnable()
2923 return NULL;
2926 // We need to unlock both OUTPUT_SECTIONS_BLOCKER and FINAL_BLOCKER
2927 // when finished.
2929 void
2930 Write_sections_task::locks(Task_locker* tl)
2932 tl->add(this, this->output_sections_blocker_);
2933 tl->add(this, this->final_blocker_);
2936 // Run the task--write out the data.
2938 void
2939 Write_sections_task::run(Workqueue*)
2941 this->layout_->write_output_sections(this->of_);
2944 // Write_data_task methods.
2946 // We can always run this task.
2948 Task_token*
2949 Write_data_task::is_runnable()
2951 return NULL;
2954 // We need to unlock FINAL_BLOCKER when finished.
2956 void
2957 Write_data_task::locks(Task_locker* tl)
2959 tl->add(this, this->final_blocker_);
2962 // Run the task--write out the data.
2964 void
2965 Write_data_task::run(Workqueue*)
2967 this->layout_->write_data(this->symtab_, this->of_);
2970 // Write_symbols_task methods.
2972 // We can always run this task.
2974 Task_token*
2975 Write_symbols_task::is_runnable()
2977 return NULL;
2980 // We need to unlock FINAL_BLOCKER when finished.
2982 void
2983 Write_symbols_task::locks(Task_locker* tl)
2985 tl->add(this, this->final_blocker_);
2988 // Run the task--write out the symbols.
2990 void
2991 Write_symbols_task::run(Workqueue*)
2993 this->symtab_->write_globals(this->input_objects_, this->sympool_,
2994 this->dynpool_, this->of_);
2997 // Write_after_input_sections_task methods.
2999 // We can only run this task after the input sections have completed.
3001 Task_token*
3002 Write_after_input_sections_task::is_runnable()
3004 if (this->input_sections_blocker_->is_blocked())
3005 return this->input_sections_blocker_;
3006 return NULL;
3009 // We need to unlock FINAL_BLOCKER when finished.
3011 void
3012 Write_after_input_sections_task::locks(Task_locker* tl)
3014 tl->add(this, this->final_blocker_);
3017 // Run the task.
3019 void
3020 Write_after_input_sections_task::run(Workqueue*)
3022 this->layout_->write_sections_after_input_sections(this->of_);
3025 // Close_task_runner methods.
3027 // Run the task--close the file.
3029 void
3030 Close_task_runner::run(Workqueue*, const Task*)
3032 // If we need to compute a checksum for the BUILD if, we do so here.
3033 this->layout_->write_build_id(this->of_);
3035 // If we've been asked to create a binary file, we do so here.
3036 if (this->options_->oformat_enum() != General_options::OBJECT_FORMAT_ELF)
3037 this->layout_->write_binary(this->of_);
3039 this->of_->close();
3042 // Instantiate the templates we need. We could use the configure
3043 // script to restrict this to only the ones for implemented targets.
3045 #ifdef HAVE_TARGET_32_LITTLE
3046 template
3047 Output_section*
3048 Layout::layout<32, false>(Sized_relobj<32, false>* object, unsigned int shndx,
3049 const char* name,
3050 const elfcpp::Shdr<32, false>& shdr,
3051 unsigned int, unsigned int, off_t*);
3052 #endif
3054 #ifdef HAVE_TARGET_32_BIG
3055 template
3056 Output_section*
3057 Layout::layout<32, true>(Sized_relobj<32, true>* object, unsigned int shndx,
3058 const char* name,
3059 const elfcpp::Shdr<32, true>& shdr,
3060 unsigned int, unsigned int, off_t*);
3061 #endif
3063 #ifdef HAVE_TARGET_64_LITTLE
3064 template
3065 Output_section*
3066 Layout::layout<64, false>(Sized_relobj<64, false>* object, unsigned int shndx,
3067 const char* name,
3068 const elfcpp::Shdr<64, false>& shdr,
3069 unsigned int, unsigned int, off_t*);
3070 #endif
3072 #ifdef HAVE_TARGET_64_BIG
3073 template
3074 Output_section*
3075 Layout::layout<64, true>(Sized_relobj<64, true>* object, unsigned int shndx,
3076 const char* name,
3077 const elfcpp::Shdr<64, true>& shdr,
3078 unsigned int, unsigned int, off_t*);
3079 #endif
3081 #ifdef HAVE_TARGET_32_LITTLE
3082 template
3083 Output_section*
3084 Layout::layout_reloc<32, false>(Sized_relobj<32, false>* object,
3085 unsigned int reloc_shndx,
3086 const elfcpp::Shdr<32, false>& shdr,
3087 Output_section* data_section,
3088 Relocatable_relocs* rr);
3089 #endif
3091 #ifdef HAVE_TARGET_32_BIG
3092 template
3093 Output_section*
3094 Layout::layout_reloc<32, true>(Sized_relobj<32, true>* object,
3095 unsigned int reloc_shndx,
3096 const elfcpp::Shdr<32, true>& shdr,
3097 Output_section* data_section,
3098 Relocatable_relocs* rr);
3099 #endif
3101 #ifdef HAVE_TARGET_64_LITTLE
3102 template
3103 Output_section*
3104 Layout::layout_reloc<64, false>(Sized_relobj<64, false>* object,
3105 unsigned int reloc_shndx,
3106 const elfcpp::Shdr<64, false>& shdr,
3107 Output_section* data_section,
3108 Relocatable_relocs* rr);
3109 #endif
3111 #ifdef HAVE_TARGET_64_BIG
3112 template
3113 Output_section*
3114 Layout::layout_reloc<64, true>(Sized_relobj<64, true>* object,
3115 unsigned int reloc_shndx,
3116 const elfcpp::Shdr<64, true>& shdr,
3117 Output_section* data_section,
3118 Relocatable_relocs* rr);
3119 #endif
3121 #ifdef HAVE_TARGET_32_LITTLE
3122 template
3123 void
3124 Layout::layout_group<32, false>(Symbol_table* symtab,
3125 Sized_relobj<32, false>* object,
3126 unsigned int,
3127 const char* group_section_name,
3128 const char* signature,
3129 const elfcpp::Shdr<32, false>& shdr,
3130 const elfcpp::Elf_Word* contents);
3131 #endif
3133 #ifdef HAVE_TARGET_32_BIG
3134 template
3135 void
3136 Layout::layout_group<32, true>(Symbol_table* symtab,
3137 Sized_relobj<32, true>* object,
3138 unsigned int,
3139 const char* group_section_name,
3140 const char* signature,
3141 const elfcpp::Shdr<32, true>& shdr,
3142 const elfcpp::Elf_Word* contents);
3143 #endif
3145 #ifdef HAVE_TARGET_64_LITTLE
3146 template
3147 void
3148 Layout::layout_group<64, false>(Symbol_table* symtab,
3149 Sized_relobj<64, false>* object,
3150 unsigned int,
3151 const char* group_section_name,
3152 const char* signature,
3153 const elfcpp::Shdr<64, false>& shdr,
3154 const elfcpp::Elf_Word* contents);
3155 #endif
3157 #ifdef HAVE_TARGET_64_BIG
3158 template
3159 void
3160 Layout::layout_group<64, true>(Symbol_table* symtab,
3161 Sized_relobj<64, true>* object,
3162 unsigned int,
3163 const char* group_section_name,
3164 const char* signature,
3165 const elfcpp::Shdr<64, true>& shdr,
3166 const elfcpp::Elf_Word* contents);
3167 #endif
3169 #ifdef HAVE_TARGET_32_LITTLE
3170 template
3171 Output_section*
3172 Layout::layout_eh_frame<32, false>(Sized_relobj<32, false>* object,
3173 const unsigned char* symbols,
3174 off_t symbols_size,
3175 const unsigned char* symbol_names,
3176 off_t symbol_names_size,
3177 unsigned int shndx,
3178 const elfcpp::Shdr<32, false>& shdr,
3179 unsigned int reloc_shndx,
3180 unsigned int reloc_type,
3181 off_t* off);
3182 #endif
3184 #ifdef HAVE_TARGET_32_BIG
3185 template
3186 Output_section*
3187 Layout::layout_eh_frame<32, true>(Sized_relobj<32, true>* object,
3188 const unsigned char* symbols,
3189 off_t symbols_size,
3190 const unsigned char* symbol_names,
3191 off_t symbol_names_size,
3192 unsigned int shndx,
3193 const elfcpp::Shdr<32, true>& shdr,
3194 unsigned int reloc_shndx,
3195 unsigned int reloc_type,
3196 off_t* off);
3197 #endif
3199 #ifdef HAVE_TARGET_64_LITTLE
3200 template
3201 Output_section*
3202 Layout::layout_eh_frame<64, false>(Sized_relobj<64, false>* object,
3203 const unsigned char* symbols,
3204 off_t symbols_size,
3205 const unsigned char* symbol_names,
3206 off_t symbol_names_size,
3207 unsigned int shndx,
3208 const elfcpp::Shdr<64, false>& shdr,
3209 unsigned int reloc_shndx,
3210 unsigned int reloc_type,
3211 off_t* off);
3212 #endif
3214 #ifdef HAVE_TARGET_64_BIG
3215 template
3216 Output_section*
3217 Layout::layout_eh_frame<64, true>(Sized_relobj<64, true>* object,
3218 const unsigned char* symbols,
3219 off_t symbols_size,
3220 const unsigned char* symbol_names,
3221 off_t symbol_names_size,
3222 unsigned int shndx,
3223 const elfcpp::Shdr<64, true>& shdr,
3224 unsigned int reloc_shndx,
3225 unsigned int reloc_type,
3226 off_t* off);
3227 #endif
3229 } // End namespace gold.