2008-04-11 Cary Coutant <ccoutant@google.com>
[binutils.git] / gold / layout.cc
blobefeaf659a4d7a7026c258fcc1793585b7234530c
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),
86 added_eh_frame_data_(false), eh_frame_hdr_section_(NULL),
87 build_id_note_(NULL), group_signatures_(), output_file_size_(-1),
88 input_requires_executable_stack_(false),
89 input_with_gnu_stack_note_(false),
90 input_without_gnu_stack_note_(false),
91 has_static_tls_(false),
92 any_postprocessing_sections_(false)
94 // Make space for more than enough segments for a typical file.
95 // This is just for efficiency--it's OK if we wind up needing more.
96 this->segment_list_.reserve(12);
98 // We expect two unattached Output_data objects: the file header and
99 // the segment headers.
100 this->special_output_list_.reserve(2);
103 // Hash a key we use to look up an output section mapping.
105 size_t
106 Layout::Hash_key::operator()(const Layout::Key& k) const
108 return k.first + k.second.first + k.second.second;
111 // Return whether PREFIX is a prefix of STR.
113 static inline bool
114 is_prefix_of(const char* prefix, const char* str)
116 return strncmp(prefix, str, strlen(prefix)) == 0;
119 // Returns whether the given section is in the list of
120 // debug-sections-used-by-some-version-of-gdb. Currently,
121 // we've checked versions of gdb up to and including 6.7.1.
123 static const char* gdb_sections[] =
124 { ".debug_abbrev",
125 // ".debug_aranges", // not used by gdb as of 6.7.1
126 ".debug_frame",
127 ".debug_info",
128 ".debug_line",
129 ".debug_loc",
130 ".debug_macinfo",
131 // ".debug_pubnames", // not used by gdb as of 6.7.1
132 ".debug_ranges",
133 ".debug_str",
136 static inline bool
137 is_gdb_debug_section(const char* str)
139 // We can do this faster: binary search or a hashtable. But why bother?
140 for (size_t i = 0; i < sizeof(gdb_sections)/sizeof(*gdb_sections); ++i)
141 if (strcmp(str, gdb_sections[i]) == 0)
142 return true;
143 return false;
146 // Whether to include this section in the link.
148 template<int size, bool big_endian>
149 bool
150 Layout::include_section(Sized_relobj<size, big_endian>*, const char* name,
151 const elfcpp::Shdr<size, big_endian>& shdr)
153 switch (shdr.get_sh_type())
155 case elfcpp::SHT_NULL:
156 case elfcpp::SHT_SYMTAB:
157 case elfcpp::SHT_DYNSYM:
158 case elfcpp::SHT_STRTAB:
159 case elfcpp::SHT_HASH:
160 case elfcpp::SHT_DYNAMIC:
161 case elfcpp::SHT_SYMTAB_SHNDX:
162 return false;
164 case elfcpp::SHT_RELA:
165 case elfcpp::SHT_REL:
166 case elfcpp::SHT_GROUP:
167 // If we are emitting relocations these should be handled
168 // elsewhere.
169 gold_assert(!parameters->options().relocatable()
170 && !parameters->options().emit_relocs());
171 return false;
173 case elfcpp::SHT_PROGBITS:
174 if (parameters->options().strip_debug()
175 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
177 // Debugging sections can only be recognized by name.
178 if (is_prefix_of(".debug", name)
179 || is_prefix_of(".gnu.linkonce.wi.", name)
180 || is_prefix_of(".line", name)
181 || is_prefix_of(".stab", name))
182 return false;
184 if (parameters->options().strip_debug_gdb()
185 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
187 // Debugging sections can only be recognized by name.
188 if (is_prefix_of(".debug", name)
189 && !is_gdb_debug_section(name))
190 return false;
192 return true;
194 default:
195 return true;
199 // Return an output section named NAME, or NULL if there is none.
201 Output_section*
202 Layout::find_output_section(const char* name) const
204 for (Section_list::const_iterator p = this->section_list_.begin();
205 p != this->section_list_.end();
206 ++p)
207 if (strcmp((*p)->name(), name) == 0)
208 return *p;
209 return NULL;
212 // Return an output segment of type TYPE, with segment flags SET set
213 // and segment flags CLEAR clear. Return NULL if there is none.
215 Output_segment*
216 Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
217 elfcpp::Elf_Word clear) const
219 for (Segment_list::const_iterator p = this->segment_list_.begin();
220 p != this->segment_list_.end();
221 ++p)
222 if (static_cast<elfcpp::PT>((*p)->type()) == type
223 && ((*p)->flags() & set) == set
224 && ((*p)->flags() & clear) == 0)
225 return *p;
226 return NULL;
229 // Return the output section to use for section NAME with type TYPE
230 // and section flags FLAGS. NAME must be canonicalized in the string
231 // pool, and NAME_KEY is the key.
233 Output_section*
234 Layout::get_output_section(const char* name, Stringpool::Key name_key,
235 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags)
237 const Key key(name_key, std::make_pair(type, flags));
238 const std::pair<Key, Output_section*> v(key, NULL);
239 std::pair<Section_name_map::iterator, bool> ins(
240 this->section_name_map_.insert(v));
242 if (!ins.second)
243 return ins.first->second;
244 else
246 // This is the first time we've seen this name/type/flags
247 // combination. For compatibility with the GNU linker, we
248 // combine sections with contents and zero flags with sections
249 // with non-zero flags. This is a workaround for cases where
250 // assembler code forgets to set section flags. FIXME: Perhaps
251 // there should be an option to control this.
252 Output_section* os = NULL;
254 if (type == elfcpp::SHT_PROGBITS)
256 if (flags == 0)
258 Output_section* same_name = this->find_output_section(name);
259 if (same_name != NULL
260 && same_name->type() == elfcpp::SHT_PROGBITS
261 && (same_name->flags() & elfcpp::SHF_TLS) == 0)
262 os = same_name;
264 else if ((flags & elfcpp::SHF_TLS) == 0)
266 elfcpp::Elf_Xword zero_flags = 0;
267 const Key zero_key(name_key, std::make_pair(type, zero_flags));
268 Section_name_map::iterator p =
269 this->section_name_map_.find(zero_key);
270 if (p != this->section_name_map_.end())
272 os = p->second;
273 if ((flags & elfcpp::SHF_ALLOC) != 0)
274 this->allocate_output_section(os, flags);
279 if (os == NULL)
280 os = this->make_output_section(name, type, flags);
281 ins.first->second = os;
282 return os;
286 // Pick the output section to use for section NAME, in input file
287 // RELOBJ, with type TYPE and flags FLAGS. RELOBJ may be NULL for a
288 // linker created section. ADJUST_NAME is true if we should apply the
289 // standard name mappings in Layout::output_section_name. This will
290 // return NULL if the input section should be discarded.
292 Output_section*
293 Layout::choose_output_section(const Relobj* relobj, const char* name,
294 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
295 bool adjust_name)
297 // We should ignore some flags. FIXME: This will need some
298 // adjustment for ld -r.
299 flags &= ~ (elfcpp::SHF_INFO_LINK
300 | elfcpp::SHF_LINK_ORDER
301 | elfcpp::SHF_GROUP
302 | elfcpp::SHF_MERGE
303 | elfcpp::SHF_STRINGS);
305 if (this->script_options_->saw_sections_clause())
307 // We are using a SECTIONS clause, so the output section is
308 // chosen based only on the name.
310 Script_sections* ss = this->script_options_->script_sections();
311 const char* file_name = relobj == NULL ? NULL : relobj->name().c_str();
312 Output_section** output_section_slot;
313 name = ss->output_section_name(file_name, name, &output_section_slot);
314 if (name == NULL)
316 // The SECTIONS clause says to discard this input section.
317 return NULL;
320 // If this is an orphan section--one not mentioned in the linker
321 // script--then OUTPUT_SECTION_SLOT will be NULL, and we do the
322 // default processing below.
324 if (output_section_slot != NULL)
326 if (*output_section_slot != NULL)
328 // If the output section was created unallocated, and we
329 // are now allocating it, then we need to clear the
330 // address set in the constructor and remove it from the
331 // unattached section list.
332 if (((*output_section_slot)->flags() & elfcpp::SHF_ALLOC) == 0
333 && (flags & elfcpp::SHF_ALLOC) != 0)
334 this->allocate_output_section(*output_section_slot, flags);
336 return *output_section_slot;
339 // We don't put sections found in the linker script into
340 // SECTION_NAME_MAP_. That keeps us from getting confused
341 // if an orphan section is mapped to a section with the same
342 // name as one in the linker script.
344 name = this->namepool_.add(name, false, NULL);
346 Output_section* os = this->make_output_section(name, type, flags);
347 os->set_found_in_sections_clause();
348 *output_section_slot = os;
349 return os;
353 // FIXME: Handle SHF_OS_NONCONFORMING somewhere.
355 // Turn NAME from the name of the input section into the name of the
356 // output section.
358 size_t len = strlen(name);
359 if (adjust_name && !parameters->options().relocatable())
360 name = Layout::output_section_name(name, &len);
362 Stringpool::Key name_key;
363 name = this->namepool_.add_with_length(name, len, true, &name_key);
365 // Find or make the output section. The output section is selected
366 // based on the section name, type, and flags.
367 return this->get_output_section(name, name_key, type, flags);
370 // Return the output section to use for input section SHNDX, with name
371 // NAME, with header HEADER, from object OBJECT. RELOC_SHNDX is the
372 // index of a relocation section which applies to this section, or 0
373 // if none, or -1U if more than one. RELOC_TYPE is the type of the
374 // relocation section if there is one. Set *OFF to the offset of this
375 // input section without the output section. Return NULL if the
376 // section should be discarded. Set *OFF to -1 if the section
377 // contents should not be written directly to the output file, but
378 // will instead receive special handling.
380 template<int size, bool big_endian>
381 Output_section*
382 Layout::layout(Sized_relobj<size, big_endian>* object, unsigned int shndx,
383 const char* name, const elfcpp::Shdr<size, big_endian>& shdr,
384 unsigned int reloc_shndx, unsigned int, off_t* off)
386 if (!this->include_section(object, name, shdr))
387 return NULL;
389 Output_section* os;
391 // In a relocatable link a grouped section must not be combined with
392 // any other sections.
393 if (parameters->options().relocatable()
394 && (shdr.get_sh_flags() & elfcpp::SHF_GROUP) != 0)
396 name = this->namepool_.add(name, true, NULL);
397 os = this->make_output_section(name, shdr.get_sh_type(),
398 shdr.get_sh_flags());
400 else
402 os = this->choose_output_section(object, name, shdr.get_sh_type(),
403 shdr.get_sh_flags(), true);
404 if (os == NULL)
405 return NULL;
408 // By default the GNU linker sorts input sections whose names match
409 // .ctor.*, .dtor.*, .init_array.*, or .fini_array.*. The sections
410 // are sorted by name. This is used to implement constructor
411 // priority ordering. We are compatible.
412 if (!this->script_options_->saw_sections_clause()
413 && (is_prefix_of(".ctors.", name)
414 || is_prefix_of(".dtors.", name)
415 || is_prefix_of(".init_array.", name)
416 || is_prefix_of(".fini_array.", name)))
417 os->set_must_sort_attached_input_sections();
419 // FIXME: Handle SHF_LINK_ORDER somewhere.
421 *off = os->add_input_section(object, shndx, name, shdr, reloc_shndx,
422 this->script_options_->saw_sections_clause());
424 return os;
427 // Handle a relocation section when doing a relocatable link.
429 template<int size, bool big_endian>
430 Output_section*
431 Layout::layout_reloc(Sized_relobj<size, big_endian>* object,
432 unsigned int,
433 const elfcpp::Shdr<size, big_endian>& shdr,
434 Output_section* data_section,
435 Relocatable_relocs* rr)
437 gold_assert(parameters->options().relocatable()
438 || parameters->options().emit_relocs());
440 int sh_type = shdr.get_sh_type();
442 std::string name;
443 if (sh_type == elfcpp::SHT_REL)
444 name = ".rel";
445 else if (sh_type == elfcpp::SHT_RELA)
446 name = ".rela";
447 else
448 gold_unreachable();
449 name += data_section->name();
451 Output_section* os = this->choose_output_section(object, name.c_str(),
452 sh_type,
453 shdr.get_sh_flags(),
454 false);
456 os->set_should_link_to_symtab();
457 os->set_info_section(data_section);
459 Output_section_data* posd;
460 if (sh_type == elfcpp::SHT_REL)
462 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
463 posd = new Output_relocatable_relocs<elfcpp::SHT_REL,
464 size,
465 big_endian>(rr);
467 else if (sh_type == elfcpp::SHT_RELA)
469 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
470 posd = new Output_relocatable_relocs<elfcpp::SHT_RELA,
471 size,
472 big_endian>(rr);
474 else
475 gold_unreachable();
477 os->add_output_section_data(posd);
478 rr->set_output_data(posd);
480 return os;
483 // Handle a group section when doing a relocatable link.
485 template<int size, bool big_endian>
486 void
487 Layout::layout_group(Symbol_table* symtab,
488 Sized_relobj<size, big_endian>* object,
489 unsigned int,
490 const char* group_section_name,
491 const char* signature,
492 const elfcpp::Shdr<size, big_endian>& shdr,
493 const elfcpp::Elf_Word* contents)
495 gold_assert(parameters->options().relocatable());
496 gold_assert(shdr.get_sh_type() == elfcpp::SHT_GROUP);
497 group_section_name = this->namepool_.add(group_section_name, true, NULL);
498 Output_section* os = this->make_output_section(group_section_name,
499 elfcpp::SHT_GROUP,
500 shdr.get_sh_flags());
502 // We need to find a symbol with the signature in the symbol table.
503 // If we don't find one now, we need to look again later.
504 Symbol* sym = symtab->lookup(signature, NULL);
505 if (sym != NULL)
506 os->set_info_symndx(sym);
507 else
509 // We will wind up using a symbol whose name is the signature.
510 // So just put the signature in the symbol name pool to save it.
511 signature = symtab->canonicalize_name(signature);
512 this->group_signatures_.push_back(Group_signature(os, signature));
515 os->set_should_link_to_symtab();
516 os->set_entsize(4);
518 section_size_type entry_count =
519 convert_to_section_size_type(shdr.get_sh_size() / 4);
520 Output_section_data* posd =
521 new Output_data_group<size, big_endian>(object, entry_count, contents);
522 os->add_output_section_data(posd);
525 // Special GNU handling of sections name .eh_frame. They will
526 // normally hold exception frame data as defined by the C++ ABI
527 // (http://codesourcery.com/cxx-abi/).
529 template<int size, bool big_endian>
530 Output_section*
531 Layout::layout_eh_frame(Sized_relobj<size, big_endian>* object,
532 const unsigned char* symbols,
533 off_t symbols_size,
534 const unsigned char* symbol_names,
535 off_t symbol_names_size,
536 unsigned int shndx,
537 const elfcpp::Shdr<size, big_endian>& shdr,
538 unsigned int reloc_shndx, unsigned int reloc_type,
539 off_t* off)
541 gold_assert(shdr.get_sh_type() == elfcpp::SHT_PROGBITS);
542 gold_assert((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0);
544 const char* const name = ".eh_frame";
545 Output_section* os = this->choose_output_section(object,
546 name,
547 elfcpp::SHT_PROGBITS,
548 elfcpp::SHF_ALLOC,
549 false);
550 if (os == NULL)
551 return NULL;
553 // On some targets gcc assumes that a read-only .eh_frame section
554 // will be merged with a read-write .eh_frame section.
555 if ((shdr.get_sh_flags() & elfcpp::SHF_WRITE) != 0
556 && (os->flags() & elfcpp::SHF_WRITE) == 0)
558 elfcpp::Elf_Xword new_flags = os->flags() | elfcpp::SHF_WRITE;
559 this->write_enable_output_section(os, new_flags);
560 os->set_flags(new_flags);
563 if (this->eh_frame_section_ == NULL)
565 this->eh_frame_section_ = os;
566 this->eh_frame_data_ = new Eh_frame();
568 if (this->options_.eh_frame_hdr())
570 Output_section* hdr_os =
571 this->choose_output_section(NULL,
572 ".eh_frame_hdr",
573 elfcpp::SHT_PROGBITS,
574 elfcpp::SHF_ALLOC,
575 false);
577 if (hdr_os != NULL)
579 Eh_frame_hdr* hdr_posd = new Eh_frame_hdr(os,
580 this->eh_frame_data_);
581 hdr_os->add_output_section_data(hdr_posd);
583 hdr_os->set_after_input_sections();
585 if (!this->script_options_->saw_phdrs_clause())
587 Output_segment* hdr_oseg;
588 hdr_oseg = this->make_output_segment(elfcpp::PT_GNU_EH_FRAME,
589 elfcpp::PF_R);
590 hdr_oseg->add_output_section(hdr_os, elfcpp::PF_R);
593 this->eh_frame_data_->set_eh_frame_hdr(hdr_posd);
598 gold_assert(this->eh_frame_section_ == os);
600 if (this->eh_frame_data_->add_ehframe_input_section(object,
601 symbols,
602 symbols_size,
603 symbol_names,
604 symbol_names_size,
605 shndx,
606 reloc_shndx,
607 reloc_type))
609 // We found a .eh_frame section we are going to optimize, so now
610 // we can add the set of optimized sections to the output
611 // section. We need to postpone adding this until we've found a
612 // section we can optimize so that the .eh_frame section in
613 // crtbegin.o winds up at the start of the output section.
614 if (!this->added_eh_frame_data_)
616 os->add_output_section_data(this->eh_frame_data_);
617 this->added_eh_frame_data_ = true;
619 *off = -1;
621 else
623 // We couldn't handle this .eh_frame section for some reason.
624 // Add it as a normal section.
625 bool saw_sections_clause = this->script_options_->saw_sections_clause();
626 *off = os->add_input_section(object, shndx, name, shdr, reloc_shndx,
627 saw_sections_clause);
630 return os;
633 // Add POSD to an output section using NAME, TYPE, and FLAGS.
635 void
636 Layout::add_output_section_data(const char* name, elfcpp::Elf_Word type,
637 elfcpp::Elf_Xword flags,
638 Output_section_data* posd)
640 Output_section* os = this->choose_output_section(NULL, name, type, flags,
641 false);
642 if (os != NULL)
643 os->add_output_section_data(posd);
646 // Map section flags to segment flags.
648 elfcpp::Elf_Word
649 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags)
651 elfcpp::Elf_Word ret = elfcpp::PF_R;
652 if ((flags & elfcpp::SHF_WRITE) != 0)
653 ret |= elfcpp::PF_W;
654 if ((flags & elfcpp::SHF_EXECINSTR) != 0)
655 ret |= elfcpp::PF_X;
656 return ret;
659 // Sometimes we compress sections. This is typically done for
660 // sections that are not part of normal program execution (such as
661 // .debug_* sections), and where the readers of these sections know
662 // how to deal with compressed sections. (To make it easier for them,
663 // we will rename the ouput section in such cases from .foo to
664 // .foo.zlib.nnnn, where nnnn is the uncompressed size.) This routine
665 // doesn't say for certain whether we'll compress -- it depends on
666 // commandline options as well -- just whether this section is a
667 // candidate for compression.
669 static bool
670 is_compressible_debug_section(const char* secname)
672 return (strncmp(secname, ".debug", sizeof(".debug") - 1) == 0);
675 // Make a new Output_section, and attach it to segments as
676 // appropriate.
678 Output_section*
679 Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
680 elfcpp::Elf_Xword flags)
682 Output_section* os;
683 if ((flags & elfcpp::SHF_ALLOC) == 0
684 && strcmp(this->options_.compress_debug_sections(), "none") != 0
685 && is_compressible_debug_section(name))
686 os = new Output_compressed_section(&this->options_, name, type, flags);
687 else
688 os = new Output_section(name, type, flags);
690 this->section_list_.push_back(os);
692 if ((flags & elfcpp::SHF_ALLOC) == 0)
693 this->unattached_section_list_.push_back(os);
694 else
695 this->attach_to_segment(os, flags);
697 // The GNU linker by default sorts some sections by priority, so we
698 // do the same. We need to know that this might happen before we
699 // attach any input sections.
700 if (!this->script_options_->saw_sections_clause()
701 && (strcmp(name, ".ctors") == 0
702 || strcmp(name, ".dtors") == 0
703 || strcmp(name, ".init_array") == 0
704 || strcmp(name, ".fini_array") == 0))
705 os->set_may_sort_attached_input_sections();
707 return os;
710 // Attach an allocated output section to a segment.
712 void
713 Layout::attach_to_segment(Output_section* os, elfcpp::Elf_Xword flags)
715 gold_assert((flags & elfcpp::SHF_ALLOC) != 0);
717 if (parameters->options().relocatable())
718 return;
720 // If we have a SECTIONS clause, we can't handle the attachment to
721 // segments until after we've seen all the sections.
722 if (this->script_options_->saw_sections_clause())
723 return;
725 gold_assert(!this->script_options_->saw_phdrs_clause());
727 // This output section goes into a PT_LOAD segment.
729 elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags);
731 // In general the only thing we really care about for PT_LOAD
732 // segments is whether or not they are writable, so that is how we
733 // search for them. People who need segments sorted on some other
734 // basis will have to use a linker script.
736 Segment_list::const_iterator p;
737 for (p = this->segment_list_.begin();
738 p != this->segment_list_.end();
739 ++p)
741 if ((*p)->type() == elfcpp::PT_LOAD
742 && ((*p)->flags() & elfcpp::PF_W) == (seg_flags & elfcpp::PF_W))
744 // If -Tbss was specified, we need to separate the data
745 // and BSS segments.
746 if (this->options_.user_set_Tbss())
748 if ((os->type() == elfcpp::SHT_NOBITS)
749 == (*p)->has_any_data_sections())
750 continue;
753 (*p)->add_output_section(os, seg_flags);
754 break;
758 if (p == this->segment_list_.end())
760 Output_segment* oseg = this->make_output_segment(elfcpp::PT_LOAD,
761 seg_flags);
762 oseg->add_output_section(os, seg_flags);
765 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
766 // segment.
767 if (os->type() == elfcpp::SHT_NOTE)
769 // See if we already have an equivalent PT_NOTE segment.
770 for (p = this->segment_list_.begin();
771 p != segment_list_.end();
772 ++p)
774 if ((*p)->type() == elfcpp::PT_NOTE
775 && (((*p)->flags() & elfcpp::PF_W)
776 == (seg_flags & elfcpp::PF_W)))
778 (*p)->add_output_section(os, seg_flags);
779 break;
783 if (p == this->segment_list_.end())
785 Output_segment* oseg = this->make_output_segment(elfcpp::PT_NOTE,
786 seg_flags);
787 oseg->add_output_section(os, seg_flags);
791 // If we see a loadable SHF_TLS section, we create a PT_TLS
792 // segment. There can only be one such segment.
793 if ((flags & elfcpp::SHF_TLS) != 0)
795 if (this->tls_segment_ == NULL)
796 this->tls_segment_ = this->make_output_segment(elfcpp::PT_TLS,
797 seg_flags);
798 this->tls_segment_->add_output_section(os, seg_flags);
802 // Make an output section for a script.
804 Output_section*
805 Layout::make_output_section_for_script(const char* name)
807 name = this->namepool_.add(name, false, NULL);
808 Output_section* os = this->make_output_section(name, elfcpp::SHT_PROGBITS,
809 elfcpp::SHF_ALLOC);
810 os->set_found_in_sections_clause();
811 return os;
814 // We have to move an existing output section from the unallocated
815 // list to the allocated list.
817 void
818 Layout::allocate_output_section(Output_section* os, elfcpp::Elf_Xword flags)
820 os->reset_address_and_file_offset();
822 Section_list::iterator p = std::find(this->unattached_section_list_.begin(),
823 this->unattached_section_list_.end(),
824 os);
825 gold_assert(p != this->unattached_section_list_.end());
826 this->unattached_section_list_.erase(p);
828 this->attach_to_segment(os, flags);
831 // We have to move an existing output section from the read-only
832 // segment to the writable segment.
834 void
835 Layout::write_enable_output_section(Output_section* os,
836 elfcpp::Elf_Xword flags)
838 gold_assert((os->flags() & elfcpp::SHF_WRITE) == 0);
839 gold_assert(os->type() == elfcpp::SHT_PROGBITS);
840 gold_assert((flags & elfcpp::SHF_WRITE) != 0);
841 gold_assert((flags & elfcpp::SHF_ALLOC) != 0);
843 if (parameters->options().relocatable())
844 return;
846 if (this->script_options_->saw_sections_clause())
847 return;
849 Segment_list::iterator p;
850 for (p = this->segment_list_.begin();
851 p != this->segment_list_.end();
852 ++p)
854 if ((*p)->type() == elfcpp::PT_LOAD
855 && ((*p)->flags() & elfcpp::PF_W) == 0)
857 (*p)->remove_output_section(os);
858 break;
861 gold_assert(p != this->segment_list_.end());
863 this->attach_to_segment(os, flags);
866 // Return the number of segments we expect to see.
868 size_t
869 Layout::expected_segment_count() const
871 size_t ret = this->segment_list_.size();
873 // If we didn't see a SECTIONS clause in a linker script, we should
874 // already have the complete list of segments. Otherwise we ask the
875 // SECTIONS clause how many segments it expects, and add in the ones
876 // we already have (PT_GNU_STACK, PT_GNU_EH_FRAME, etc.)
878 if (!this->script_options_->saw_sections_clause())
879 return ret;
880 else
882 const Script_sections* ss = this->script_options_->script_sections();
883 return ret + ss->expected_segment_count(this);
887 // Handle the .note.GNU-stack section at layout time. SEEN_GNU_STACK
888 // is whether we saw a .note.GNU-stack section in the object file.
889 // GNU_STACK_FLAGS is the section flags. The flags give the
890 // protection required for stack memory. We record this in an
891 // executable as a PT_GNU_STACK segment. If an object file does not
892 // have a .note.GNU-stack segment, we must assume that it is an old
893 // object. On some targets that will force an executable stack.
895 void
896 Layout::layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags)
898 if (!seen_gnu_stack)
899 this->input_without_gnu_stack_note_ = true;
900 else
902 this->input_with_gnu_stack_note_ = true;
903 if ((gnu_stack_flags & elfcpp::SHF_EXECINSTR) != 0)
904 this->input_requires_executable_stack_ = true;
908 // Create the dynamic sections which are needed before we read the
909 // relocs.
911 void
912 Layout::create_initial_dynamic_sections(Symbol_table* symtab)
914 if (parameters->doing_static_link())
915 return;
917 this->dynamic_section_ = this->choose_output_section(NULL, ".dynamic",
918 elfcpp::SHT_DYNAMIC,
919 (elfcpp::SHF_ALLOC
920 | elfcpp::SHF_WRITE),
921 false);
923 symtab->define_in_output_data("_DYNAMIC", NULL, this->dynamic_section_, 0, 0,
924 elfcpp::STT_OBJECT, elfcpp::STB_LOCAL,
925 elfcpp::STV_HIDDEN, 0, false, false);
927 this->dynamic_data_ = new Output_data_dynamic(&this->dynpool_);
929 this->dynamic_section_->add_output_section_data(this->dynamic_data_);
932 // For each output section whose name can be represented as C symbol,
933 // define __start and __stop symbols for the section. This is a GNU
934 // extension.
936 void
937 Layout::define_section_symbols(Symbol_table* symtab)
939 for (Section_list::const_iterator p = this->section_list_.begin();
940 p != this->section_list_.end();
941 ++p)
943 const char* const name = (*p)->name();
944 if (name[strspn(name,
945 ("0123456789"
946 "ABCDEFGHIJKLMNOPWRSTUVWXYZ"
947 "abcdefghijklmnopqrstuvwxyz"
948 "_"))]
949 == '\0')
951 const std::string name_string(name);
952 const std::string start_name("__start_" + name_string);
953 const std::string stop_name("__stop_" + name_string);
955 symtab->define_in_output_data(start_name.c_str(),
956 NULL, // version
958 0, // value
959 0, // symsize
960 elfcpp::STT_NOTYPE,
961 elfcpp::STB_GLOBAL,
962 elfcpp::STV_DEFAULT,
963 0, // nonvis
964 false, // offset_is_from_end
965 true); // only_if_ref
967 symtab->define_in_output_data(stop_name.c_str(),
968 NULL, // version
970 0, // value
971 0, // symsize
972 elfcpp::STT_NOTYPE,
973 elfcpp::STB_GLOBAL,
974 elfcpp::STV_DEFAULT,
975 0, // nonvis
976 true, // offset_is_from_end
977 true); // only_if_ref
982 // Define symbols for group signatures.
984 void
985 Layout::define_group_signatures(Symbol_table* symtab)
987 for (Group_signatures::iterator p = this->group_signatures_.begin();
988 p != this->group_signatures_.end();
989 ++p)
991 Symbol* sym = symtab->lookup(p->signature, NULL);
992 if (sym != NULL)
993 p->section->set_info_symndx(sym);
994 else
996 // Force the name of the group section to the group
997 // signature, and use the group's section symbol as the
998 // signature symbol.
999 if (strcmp(p->section->name(), p->signature) != 0)
1001 const char* name = this->namepool_.add(p->signature,
1002 true, NULL);
1003 p->section->set_name(name);
1005 p->section->set_needs_symtab_index();
1006 p->section->set_info_section_symndx(p->section);
1010 this->group_signatures_.clear();
1013 // Find the first read-only PT_LOAD segment, creating one if
1014 // necessary.
1016 Output_segment*
1017 Layout::find_first_load_seg()
1019 for (Segment_list::const_iterator p = this->segment_list_.begin();
1020 p != this->segment_list_.end();
1021 ++p)
1023 if ((*p)->type() == elfcpp::PT_LOAD
1024 && ((*p)->flags() & elfcpp::PF_R) != 0
1025 && ((*p)->flags() & elfcpp::PF_W) == 0)
1026 return *p;
1029 gold_assert(!this->script_options_->saw_phdrs_clause());
1031 Output_segment* load_seg = this->make_output_segment(elfcpp::PT_LOAD,
1032 elfcpp::PF_R);
1033 return load_seg;
1036 // Finalize the layout. When this is called, we have created all the
1037 // output sections and all the output segments which are based on
1038 // input sections. We have several things to do, and we have to do
1039 // them in the right order, so that we get the right results correctly
1040 // and efficiently.
1042 // 1) Finalize the list of output segments and create the segment
1043 // table header.
1045 // 2) Finalize the dynamic symbol table and associated sections.
1047 // 3) Determine the final file offset of all the output segments.
1049 // 4) Determine the final file offset of all the SHF_ALLOC output
1050 // sections.
1052 // 5) Create the symbol table sections and the section name table
1053 // section.
1055 // 6) Finalize the symbol table: set symbol values to their final
1056 // value and make a final determination of which symbols are going
1057 // into the output symbol table.
1059 // 7) Create the section table header.
1061 // 8) Determine the final file offset of all the output sections which
1062 // are not SHF_ALLOC, including the section table header.
1064 // 9) Finalize the ELF file header.
1066 // This function returns the size of the output file.
1068 off_t
1069 Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab,
1070 Target* target, const Task* task)
1072 target->finalize_sections(this);
1074 this->count_local_symbols(task, input_objects);
1076 this->create_gold_note();
1077 this->create_executable_stack_info(target);
1078 this->create_build_id();
1080 Output_segment* phdr_seg = NULL;
1081 if (!parameters->options().relocatable() && !parameters->doing_static_link())
1083 // There was a dynamic object in the link. We need to create
1084 // some information for the dynamic linker.
1086 // Create the PT_PHDR segment which will hold the program
1087 // headers.
1088 if (!this->script_options_->saw_phdrs_clause())
1089 phdr_seg = this->make_output_segment(elfcpp::PT_PHDR, elfcpp::PF_R);
1091 // Create the dynamic symbol table, including the hash table.
1092 Output_section* dynstr;
1093 std::vector<Symbol*> dynamic_symbols;
1094 unsigned int local_dynamic_count;
1095 Versions versions(*this->script_options()->version_script_info(),
1096 &this->dynpool_);
1097 this->create_dynamic_symtab(input_objects, symtab, &dynstr,
1098 &local_dynamic_count, &dynamic_symbols,
1099 &versions);
1101 // Create the .interp section to hold the name of the
1102 // interpreter, and put it in a PT_INTERP segment.
1103 if (!parameters->options().shared())
1104 this->create_interp(target);
1106 // Finish the .dynamic section to hold the dynamic data, and put
1107 // it in a PT_DYNAMIC segment.
1108 this->finish_dynamic_section(input_objects, symtab);
1110 // We should have added everything we need to the dynamic string
1111 // table.
1112 this->dynpool_.set_string_offsets();
1114 // Create the version sections. We can't do this until the
1115 // dynamic string table is complete.
1116 this->create_version_sections(&versions, symtab, local_dynamic_count,
1117 dynamic_symbols, dynstr);
1120 // If there is a SECTIONS clause, put all the input sections into
1121 // the required order.
1122 Output_segment* load_seg;
1123 if (this->script_options_->saw_sections_clause())
1124 load_seg = this->set_section_addresses_from_script(symtab);
1125 else if (parameters->options().relocatable())
1126 load_seg = NULL;
1127 else
1128 load_seg = this->find_first_load_seg();
1130 if (this->options_.oformat_enum() != General_options::OBJECT_FORMAT_ELF)
1131 load_seg = NULL;
1133 gold_assert(phdr_seg == NULL || load_seg != NULL);
1135 // Lay out the segment headers.
1136 Output_segment_headers* segment_headers;
1137 if (parameters->options().relocatable())
1138 segment_headers = NULL;
1139 else
1141 segment_headers = new Output_segment_headers(this->segment_list_);
1142 if (load_seg != NULL)
1143 load_seg->add_initial_output_data(segment_headers);
1144 if (phdr_seg != NULL)
1145 phdr_seg->add_initial_output_data(segment_headers);
1148 // Lay out the file header.
1149 Output_file_header* file_header;
1150 file_header = new Output_file_header(target, symtab, segment_headers,
1151 this->options_.entry());
1152 if (load_seg != NULL)
1153 load_seg->add_initial_output_data(file_header);
1155 this->special_output_list_.push_back(file_header);
1156 if (segment_headers != NULL)
1157 this->special_output_list_.push_back(segment_headers);
1159 if (this->script_options_->saw_phdrs_clause()
1160 && !parameters->options().relocatable())
1162 // Support use of FILEHDRS and PHDRS attachments in a PHDRS
1163 // clause in a linker script.
1164 Script_sections* ss = this->script_options_->script_sections();
1165 ss->put_headers_in_phdrs(file_header, segment_headers);
1168 // We set the output section indexes in set_segment_offsets and
1169 // set_section_indexes.
1170 unsigned int shndx = 1;
1172 // Set the file offsets of all the segments, and all the sections
1173 // they contain.
1174 off_t off;
1175 if (!parameters->options().relocatable())
1176 off = this->set_segment_offsets(target, load_seg, &shndx);
1177 else
1178 off = this->set_relocatable_section_offsets(file_header, &shndx);
1180 // Set the file offsets of all the non-data sections we've seen so
1181 // far which don't have to wait for the input sections. We need
1182 // this in order to finalize local symbols in non-allocated
1183 // sections.
1184 off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS);
1186 // Create the symbol table sections.
1187 this->create_symtab_sections(input_objects, symtab, &off);
1188 if (!parameters->doing_static_link())
1189 this->assign_local_dynsym_offsets(input_objects);
1191 // Process any symbol assignments from a linker script. This must
1192 // be called after the symbol table has been finalized.
1193 this->script_options_->finalize_symbols(symtab, this);
1195 // Create the .shstrtab section.
1196 Output_section* shstrtab_section = this->create_shstrtab();
1198 // Set the file offsets of the rest of the non-data sections which
1199 // don't have to wait for the input sections.
1200 off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS);
1202 // Now that all sections have been created, set the section indexes.
1203 shndx = this->set_section_indexes(shndx);
1205 // Create the section table header.
1206 this->create_shdrs(&off);
1208 // If there are no sections which require postprocessing, we can
1209 // handle the section names now, and avoid a resize later.
1210 if (!this->any_postprocessing_sections_)
1211 off = this->set_section_offsets(off,
1212 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS);
1214 file_header->set_section_info(this->section_headers_, shstrtab_section);
1216 // Now we know exactly where everything goes in the output file
1217 // (except for non-allocated sections which require postprocessing).
1218 Output_data::layout_complete();
1220 this->output_file_size_ = off;
1222 return off;
1225 // Create a note header following the format defined in the ELF ABI.
1226 // NAME is the name, NOTE_TYPE is the type, DESCSZ is the size of the
1227 // descriptor. ALLOCATE is true if the section should be allocated in
1228 // memory. This returns the new note section. It sets
1229 // *TRAILING_PADDING to the number of trailing zero bytes required.
1231 Output_section*
1232 Layout::create_note(const char* name, int note_type, size_t descsz,
1233 bool allocate, size_t* trailing_padding)
1235 // Authorities all agree that the values in a .note field should
1236 // be aligned on 4-byte boundaries for 32-bit binaries. However,
1237 // they differ on what the alignment is for 64-bit binaries.
1238 // The GABI says unambiguously they take 8-byte alignment:
1239 // http://sco.com/developers/gabi/latest/ch5.pheader.html#note_section
1240 // Other documentation says alignment should always be 4 bytes:
1241 // http://www.netbsd.org/docs/kernel/elf-notes.html#note-format
1242 // GNU ld and GNU readelf both support the latter (at least as of
1243 // version 2.16.91), and glibc always generates the latter for
1244 // .note.ABI-tag (as of version 1.6), so that's the one we go with
1245 // here.
1246 #ifdef GABI_FORMAT_FOR_DOTNOTE_SECTION // This is not defined by default.
1247 const int size = parameters->target().get_size();
1248 #else
1249 const int size = 32;
1250 #endif
1252 // The contents of the .note section.
1253 size_t namesz = strlen(name) + 1;
1254 size_t aligned_namesz = align_address(namesz, size / 8);
1255 size_t aligned_descsz = align_address(descsz, size / 8);
1257 size_t notehdrsz = 3 * (size / 8) + aligned_namesz;
1259 unsigned char* buffer = new unsigned char[notehdrsz];
1260 memset(buffer, 0, notehdrsz);
1262 bool is_big_endian = parameters->target().is_big_endian();
1264 if (size == 32)
1266 if (!is_big_endian)
1268 elfcpp::Swap<32, false>::writeval(buffer, namesz);
1269 elfcpp::Swap<32, false>::writeval(buffer + 4, descsz);
1270 elfcpp::Swap<32, false>::writeval(buffer + 8, note_type);
1272 else
1274 elfcpp::Swap<32, true>::writeval(buffer, namesz);
1275 elfcpp::Swap<32, true>::writeval(buffer + 4, descsz);
1276 elfcpp::Swap<32, true>::writeval(buffer + 8, note_type);
1279 else if (size == 64)
1281 if (!is_big_endian)
1283 elfcpp::Swap<64, false>::writeval(buffer, namesz);
1284 elfcpp::Swap<64, false>::writeval(buffer + 8, descsz);
1285 elfcpp::Swap<64, false>::writeval(buffer + 16, note_type);
1287 else
1289 elfcpp::Swap<64, true>::writeval(buffer, namesz);
1290 elfcpp::Swap<64, true>::writeval(buffer + 8, descsz);
1291 elfcpp::Swap<64, true>::writeval(buffer + 16, note_type);
1294 else
1295 gold_unreachable();
1297 memcpy(buffer + 3 * (size / 8), name, namesz);
1299 const char* note_name = this->namepool_.add(".note", false, NULL);
1300 elfcpp::Elf_Xword flags = 0;
1301 if (allocate)
1302 flags = elfcpp::SHF_ALLOC;
1303 Output_section* os = this->make_output_section(note_name,
1304 elfcpp::SHT_NOTE,
1305 flags);
1306 Output_section_data* posd = new Output_data_const_buffer(buffer, notehdrsz,
1307 size / 8);
1308 os->add_output_section_data(posd);
1310 *trailing_padding = aligned_descsz - descsz;
1312 return os;
1315 // For an executable or shared library, create a note to record the
1316 // version of gold used to create the binary.
1318 void
1319 Layout::create_gold_note()
1321 if (parameters->options().relocatable())
1322 return;
1324 std::string desc = std::string("gold ") + gold::get_version_string();
1326 size_t trailing_padding;
1327 Output_section *os = this->create_note("GNU", elfcpp::NT_GNU_GOLD_VERSION,
1328 desc.size(), false, &trailing_padding);
1330 Output_section_data* posd = new Output_data_const(desc, 4);
1331 os->add_output_section_data(posd);
1333 if (trailing_padding > 0)
1335 posd = new Output_data_fixed_space(trailing_padding, 0);
1336 os->add_output_section_data(posd);
1340 // Record whether the stack should be executable. This can be set
1341 // from the command line using the -z execstack or -z noexecstack
1342 // options. Otherwise, if any input file has a .note.GNU-stack
1343 // section with the SHF_EXECINSTR flag set, the stack should be
1344 // executable. Otherwise, if at least one input file a
1345 // .note.GNU-stack section, and some input file has no .note.GNU-stack
1346 // section, we use the target default for whether the stack should be
1347 // executable. Otherwise, we don't generate a stack note. When
1348 // generating a object file, we create a .note.GNU-stack section with
1349 // the appropriate marking. When generating an executable or shared
1350 // library, we create a PT_GNU_STACK segment.
1352 void
1353 Layout::create_executable_stack_info(const Target* target)
1355 bool is_stack_executable;
1356 if (this->options_.is_execstack_set())
1357 is_stack_executable = this->options_.is_stack_executable();
1358 else if (!this->input_with_gnu_stack_note_)
1359 return;
1360 else
1362 if (this->input_requires_executable_stack_)
1363 is_stack_executable = true;
1364 else if (this->input_without_gnu_stack_note_)
1365 is_stack_executable = target->is_default_stack_executable();
1366 else
1367 is_stack_executable = false;
1370 if (parameters->options().relocatable())
1372 const char* name = this->namepool_.add(".note.GNU-stack", false, NULL);
1373 elfcpp::Elf_Xword flags = 0;
1374 if (is_stack_executable)
1375 flags |= elfcpp::SHF_EXECINSTR;
1376 this->make_output_section(name, elfcpp::SHT_PROGBITS, flags);
1378 else
1380 if (this->script_options_->saw_phdrs_clause())
1381 return;
1382 int flags = elfcpp::PF_R | elfcpp::PF_W;
1383 if (is_stack_executable)
1384 flags |= elfcpp::PF_X;
1385 this->make_output_segment(elfcpp::PT_GNU_STACK, flags);
1389 // If --build-id was used, set up the build ID note.
1391 void
1392 Layout::create_build_id()
1394 if (!parameters->options().user_set_build_id())
1395 return;
1397 const char* style = parameters->options().build_id();
1398 if (strcmp(style, "none") == 0)
1399 return;
1401 // Set DESCSZ to the size of the note descriptor. When possible,
1402 // set DESC to the note descriptor contents.
1403 size_t descsz;
1404 std::string desc;
1405 if (strcmp(style, "md5") == 0)
1406 descsz = 128 / 8;
1407 else if (strcmp(style, "sha1") == 0)
1408 descsz = 160 / 8;
1409 else if (strcmp(style, "uuid") == 0)
1411 const size_t uuidsz = 128 / 8;
1413 char buffer[uuidsz];
1414 memset(buffer, 0, uuidsz);
1416 int descriptor = ::open("/dev/urandom", O_RDONLY);
1417 if (descriptor < 0)
1418 gold_error(_("--build-id=uuid failed: could not open /dev/urandom: %s"),
1419 strerror(errno));
1420 else
1422 ssize_t got = ::read(descriptor, buffer, uuidsz);
1423 ::close(descriptor);
1424 if (got < 0)
1425 gold_error(_("/dev/urandom: read failed: %s"), strerror(errno));
1426 else if (static_cast<size_t>(got) != uuidsz)
1427 gold_error(_("/dev/urandom: expected %zu bytes, got %zd bytes"),
1428 uuidsz, got);
1431 desc.assign(buffer, uuidsz);
1432 descsz = uuidsz;
1434 else if (strncmp(style, "0x", 2) == 0)
1436 hex_init();
1437 const char* p = style + 2;
1438 while (*p != '\0')
1440 if (hex_p(p[0]) && hex_p(p[1]))
1442 char c = (hex_value(p[0]) << 4) | hex_value(p[1]);
1443 desc += c;
1444 p += 2;
1446 else if (*p == '-' || *p == ':')
1447 ++p;
1448 else
1449 gold_fatal(_("--build-id argument '%s' not a valid hex number"),
1450 style);
1452 descsz = desc.size();
1454 else
1455 gold_fatal(_("unrecognized --build-id argument '%s'"), style);
1457 // Create the note.
1458 size_t trailing_padding;
1459 Output_section* os = this->create_note("GNU", elfcpp::NT_GNU_BUILD_ID,
1460 descsz, true, &trailing_padding);
1462 if (!desc.empty())
1464 // We know the value already, so we fill it in now.
1465 gold_assert(desc.size() == descsz);
1467 Output_section_data* posd = new Output_data_const(desc, 4);
1468 os->add_output_section_data(posd);
1470 if (trailing_padding != 0)
1472 posd = new Output_data_fixed_space(trailing_padding, 0);
1473 os->add_output_section_data(posd);
1476 else
1478 // We need to compute a checksum after we have completed the
1479 // link.
1480 gold_assert(trailing_padding == 0);
1481 this->build_id_note_ = new Output_data_fixed_space(descsz, 4);
1482 os->add_output_section_data(this->build_id_note_);
1483 os->set_after_input_sections();
1487 // Return whether SEG1 should be before SEG2 in the output file. This
1488 // is based entirely on the segment type and flags. When this is
1489 // called the segment addresses has normally not yet been set.
1491 bool
1492 Layout::segment_precedes(const Output_segment* seg1,
1493 const Output_segment* seg2)
1495 elfcpp::Elf_Word type1 = seg1->type();
1496 elfcpp::Elf_Word type2 = seg2->type();
1498 // The single PT_PHDR segment is required to precede any loadable
1499 // segment. We simply make it always first.
1500 if (type1 == elfcpp::PT_PHDR)
1502 gold_assert(type2 != elfcpp::PT_PHDR);
1503 return true;
1505 if (type2 == elfcpp::PT_PHDR)
1506 return false;
1508 // The single PT_INTERP segment is required to precede any loadable
1509 // segment. We simply make it always second.
1510 if (type1 == elfcpp::PT_INTERP)
1512 gold_assert(type2 != elfcpp::PT_INTERP);
1513 return true;
1515 if (type2 == elfcpp::PT_INTERP)
1516 return false;
1518 // We then put PT_LOAD segments before any other segments.
1519 if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD)
1520 return true;
1521 if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD)
1522 return false;
1524 // We put the PT_TLS segment last, because that is where the dynamic
1525 // linker expects to find it (this is just for efficiency; other
1526 // positions would also work correctly).
1527 if (type1 == elfcpp::PT_TLS && type2 != elfcpp::PT_TLS)
1528 return false;
1529 if (type2 == elfcpp::PT_TLS && type1 != elfcpp::PT_TLS)
1530 return true;
1532 const elfcpp::Elf_Word flags1 = seg1->flags();
1533 const elfcpp::Elf_Word flags2 = seg2->flags();
1535 // The order of non-PT_LOAD segments is unimportant. We simply sort
1536 // by the numeric segment type and flags values. There should not
1537 // be more than one segment with the same type and flags.
1538 if (type1 != elfcpp::PT_LOAD)
1540 if (type1 != type2)
1541 return type1 < type2;
1542 gold_assert(flags1 != flags2);
1543 return flags1 < flags2;
1546 // If the addresses are set already, sort by load address.
1547 if (seg1->are_addresses_set())
1549 if (!seg2->are_addresses_set())
1550 return true;
1552 unsigned int section_count1 = seg1->output_section_count();
1553 unsigned int section_count2 = seg2->output_section_count();
1554 if (section_count1 == 0 && section_count2 > 0)
1555 return true;
1556 if (section_count1 > 0 && section_count2 == 0)
1557 return false;
1559 uint64_t paddr1 = seg1->first_section_load_address();
1560 uint64_t paddr2 = seg2->first_section_load_address();
1561 if (paddr1 != paddr2)
1562 return paddr1 < paddr2;
1564 else if (seg2->are_addresses_set())
1565 return false;
1567 // We sort PT_LOAD segments based on the flags. Readonly segments
1568 // come before writable segments. Then writable segments with data
1569 // come before writable segments without data. Then executable
1570 // segments come before non-executable segments. Then the unlikely
1571 // case of a non-readable segment comes before the normal case of a
1572 // readable segment. If there are multiple segments with the same
1573 // type and flags, we require that the address be set, and we sort
1574 // by virtual address and then physical address.
1575 if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W))
1576 return (flags1 & elfcpp::PF_W) == 0;
1577 if ((flags1 & elfcpp::PF_W) != 0
1578 && seg1->has_any_data_sections() != seg2->has_any_data_sections())
1579 return seg1->has_any_data_sections();
1580 if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X))
1581 return (flags1 & elfcpp::PF_X) != 0;
1582 if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R))
1583 return (flags1 & elfcpp::PF_R) == 0;
1585 // We shouldn't get here--we shouldn't create segments which we
1586 // can't distinguish.
1587 gold_unreachable();
1590 // Set the file offsets of all the segments, and all the sections they
1591 // contain. They have all been created. LOAD_SEG must be be laid out
1592 // first. Return the offset of the data to follow.
1594 off_t
1595 Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
1596 unsigned int *pshndx)
1598 // Sort them into the final order.
1599 std::sort(this->segment_list_.begin(), this->segment_list_.end(),
1600 Layout::Compare_segments());
1602 // Find the PT_LOAD segments, and set their addresses and offsets
1603 // and their section's addresses and offsets.
1604 uint64_t addr;
1605 if (this->options_.user_set_Ttext())
1606 addr = this->options_.Ttext();
1607 else if (parameters->options().shared())
1608 addr = 0;
1609 else
1610 addr = target->default_text_segment_address();
1611 off_t off = 0;
1613 // If LOAD_SEG is NULL, then the file header and segment headers
1614 // will not be loadable. But they still need to be at offset 0 in
1615 // the file. Set their offsets now.
1616 if (load_seg == NULL)
1618 for (Data_list::iterator p = this->special_output_list_.begin();
1619 p != this->special_output_list_.end();
1620 ++p)
1622 off = align_address(off, (*p)->addralign());
1623 (*p)->set_address_and_file_offset(0, off);
1624 off += (*p)->data_size();
1628 bool was_readonly = false;
1629 for (Segment_list::iterator p = this->segment_list_.begin();
1630 p != this->segment_list_.end();
1631 ++p)
1633 if ((*p)->type() == elfcpp::PT_LOAD)
1635 if (load_seg != NULL && load_seg != *p)
1636 gold_unreachable();
1637 load_seg = NULL;
1639 bool are_addresses_set = (*p)->are_addresses_set();
1640 if (are_addresses_set)
1642 // When it comes to setting file offsets, we care about
1643 // the physical address.
1644 addr = (*p)->paddr();
1646 else if (this->options_.user_set_Tdata()
1647 && ((*p)->flags() & elfcpp::PF_W) != 0
1648 && (!this->options_.user_set_Tbss()
1649 || (*p)->has_any_data_sections()))
1651 addr = this->options_.Tdata();
1652 are_addresses_set = true;
1654 else if (this->options_.user_set_Tbss()
1655 && ((*p)->flags() & elfcpp::PF_W) != 0
1656 && !(*p)->has_any_data_sections())
1658 addr = this->options_.Tbss();
1659 are_addresses_set = true;
1662 uint64_t orig_addr = addr;
1663 uint64_t orig_off = off;
1665 uint64_t aligned_addr = 0;
1666 uint64_t abi_pagesize = target->abi_pagesize();
1668 // FIXME: This should depend on the -n and -N options.
1669 (*p)->set_minimum_p_align(target->common_pagesize());
1671 if (are_addresses_set)
1673 // Adjust the file offset to the same address modulo the
1674 // page size.
1675 uint64_t unsigned_off = off;
1676 uint64_t aligned_off = ((unsigned_off & ~(abi_pagesize - 1))
1677 | (addr & (abi_pagesize - 1)));
1678 if (aligned_off < unsigned_off)
1679 aligned_off += abi_pagesize;
1680 off = aligned_off;
1682 else
1684 // If the last segment was readonly, and this one is
1685 // not, then skip the address forward one page,
1686 // maintaining the same position within the page. This
1687 // lets us store both segments overlapping on a single
1688 // page in the file, but the loader will put them on
1689 // different pages in memory.
1691 addr = align_address(addr, (*p)->maximum_alignment());
1692 aligned_addr = addr;
1694 if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0)
1696 if ((addr & (abi_pagesize - 1)) != 0)
1697 addr = addr + abi_pagesize;
1700 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
1703 unsigned int shndx_hold = *pshndx;
1704 uint64_t new_addr = (*p)->set_section_addresses(this, false, addr,
1705 &off, pshndx);
1707 // Now that we know the size of this segment, we may be able
1708 // to save a page in memory, at the cost of wasting some
1709 // file space, by instead aligning to the start of a new
1710 // page. Here we use the real machine page size rather than
1711 // the ABI mandated page size.
1713 if (!are_addresses_set && aligned_addr != addr)
1715 uint64_t common_pagesize = target->common_pagesize();
1716 uint64_t first_off = (common_pagesize
1717 - (aligned_addr
1718 & (common_pagesize - 1)));
1719 uint64_t last_off = new_addr & (common_pagesize - 1);
1720 if (first_off > 0
1721 && last_off > 0
1722 && ((aligned_addr & ~ (common_pagesize - 1))
1723 != (new_addr & ~ (common_pagesize - 1)))
1724 && first_off + last_off <= common_pagesize)
1726 *pshndx = shndx_hold;
1727 addr = align_address(aligned_addr, common_pagesize);
1728 addr = align_address(addr, (*p)->maximum_alignment());
1729 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
1730 new_addr = (*p)->set_section_addresses(this, true, addr,
1731 &off, pshndx);
1735 addr = new_addr;
1737 if (((*p)->flags() & elfcpp::PF_W) == 0)
1738 was_readonly = true;
1742 // Handle the non-PT_LOAD segments, setting their offsets from their
1743 // section's offsets.
1744 for (Segment_list::iterator p = this->segment_list_.begin();
1745 p != this->segment_list_.end();
1746 ++p)
1748 if ((*p)->type() != elfcpp::PT_LOAD)
1749 (*p)->set_offset();
1752 // Set the TLS offsets for each section in the PT_TLS segment.
1753 if (this->tls_segment_ != NULL)
1754 this->tls_segment_->set_tls_offsets();
1756 return off;
1759 // Set the offsets of all the allocated sections when doing a
1760 // relocatable link. This does the same jobs as set_segment_offsets,
1761 // only for a relocatable link.
1763 off_t
1764 Layout::set_relocatable_section_offsets(Output_data* file_header,
1765 unsigned int *pshndx)
1767 off_t off = 0;
1769 file_header->set_address_and_file_offset(0, 0);
1770 off += file_header->data_size();
1772 for (Section_list::iterator p = this->section_list_.begin();
1773 p != this->section_list_.end();
1774 ++p)
1776 // We skip unallocated sections here, except that group sections
1777 // have to come first.
1778 if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
1779 && (*p)->type() != elfcpp::SHT_GROUP)
1780 continue;
1782 off = align_address(off, (*p)->addralign());
1784 // The linker script might have set the address.
1785 if (!(*p)->is_address_valid())
1786 (*p)->set_address(0);
1787 (*p)->set_file_offset(off);
1788 (*p)->finalize_data_size();
1789 off += (*p)->data_size();
1791 (*p)->set_out_shndx(*pshndx);
1792 ++*pshndx;
1795 return off;
1798 // Set the file offset of all the sections not associated with a
1799 // segment.
1801 off_t
1802 Layout::set_section_offsets(off_t off, Layout::Section_offset_pass pass)
1804 for (Section_list::iterator p = this->unattached_section_list_.begin();
1805 p != this->unattached_section_list_.end();
1806 ++p)
1808 // The symtab section is handled in create_symtab_sections.
1809 if (*p == this->symtab_section_)
1810 continue;
1812 // If we've already set the data size, don't set it again.
1813 if ((*p)->is_offset_valid() && (*p)->is_data_size_valid())
1814 continue;
1816 if (pass == BEFORE_INPUT_SECTIONS_PASS
1817 && (*p)->requires_postprocessing())
1819 (*p)->create_postprocessing_buffer();
1820 this->any_postprocessing_sections_ = true;
1823 if (pass == BEFORE_INPUT_SECTIONS_PASS
1824 && (*p)->after_input_sections())
1825 continue;
1826 else if (pass == POSTPROCESSING_SECTIONS_PASS
1827 && (!(*p)->after_input_sections()
1828 || (*p)->type() == elfcpp::SHT_STRTAB))
1829 continue;
1830 else if (pass == STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
1831 && (!(*p)->after_input_sections()
1832 || (*p)->type() != elfcpp::SHT_STRTAB))
1833 continue;
1835 off = align_address(off, (*p)->addralign());
1836 (*p)->set_file_offset(off);
1837 (*p)->finalize_data_size();
1838 off += (*p)->data_size();
1840 // At this point the name must be set.
1841 if (pass != STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS)
1842 this->namepool_.add((*p)->name(), false, NULL);
1844 return off;
1847 // Set the section indexes of all the sections not associated with a
1848 // segment.
1850 unsigned int
1851 Layout::set_section_indexes(unsigned int shndx)
1853 const bool output_is_object = parameters->options().relocatable();
1854 for (Section_list::iterator p = this->unattached_section_list_.begin();
1855 p != this->unattached_section_list_.end();
1856 ++p)
1858 // In a relocatable link, we already did group sections.
1859 if (output_is_object
1860 && (*p)->type() == elfcpp::SHT_GROUP)
1861 continue;
1863 (*p)->set_out_shndx(shndx);
1864 ++shndx;
1866 return shndx;
1869 // Set the section addresses according to the linker script. This is
1870 // only called when we see a SECTIONS clause. This returns the
1871 // program segment which should hold the file header and segment
1872 // headers, if any. It will return NULL if they should not be in a
1873 // segment.
1875 Output_segment*
1876 Layout::set_section_addresses_from_script(Symbol_table* symtab)
1878 Script_sections* ss = this->script_options_->script_sections();
1879 gold_assert(ss->saw_sections_clause());
1881 // Place each orphaned output section in the script.
1882 for (Section_list::iterator p = this->section_list_.begin();
1883 p != this->section_list_.end();
1884 ++p)
1886 if (!(*p)->found_in_sections_clause())
1887 ss->place_orphan(*p);
1890 return this->script_options_->set_section_addresses(symtab, this);
1893 // Count the local symbols in the regular symbol table and the dynamic
1894 // symbol table, and build the respective string pools.
1896 void
1897 Layout::count_local_symbols(const Task* task,
1898 const Input_objects* input_objects)
1900 // First, figure out an upper bound on the number of symbols we'll
1901 // be inserting into each pool. This helps us create the pools with
1902 // the right size, to avoid unnecessary hashtable resizing.
1903 unsigned int symbol_count = 0;
1904 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
1905 p != input_objects->relobj_end();
1906 ++p)
1907 symbol_count += (*p)->local_symbol_count();
1909 // Go from "upper bound" to "estimate." We overcount for two
1910 // reasons: we double-count symbols that occur in more than one
1911 // object file, and we count symbols that are dropped from the
1912 // output. Add it all together and assume we overcount by 100%.
1913 symbol_count /= 2;
1915 // We assume all symbols will go into both the sympool and dynpool.
1916 this->sympool_.reserve(symbol_count);
1917 this->dynpool_.reserve(symbol_count);
1919 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
1920 p != input_objects->relobj_end();
1921 ++p)
1923 Task_lock_obj<Object> tlo(task, *p);
1924 (*p)->count_local_symbols(&this->sympool_, &this->dynpool_);
1928 // Create the symbol table sections. Here we also set the final
1929 // values of the symbols. At this point all the loadable sections are
1930 // fully laid out.
1932 void
1933 Layout::create_symtab_sections(const Input_objects* input_objects,
1934 Symbol_table* symtab,
1935 off_t* poff)
1937 int symsize;
1938 unsigned int align;
1939 if (parameters->target().get_size() == 32)
1941 symsize = elfcpp::Elf_sizes<32>::sym_size;
1942 align = 4;
1944 else if (parameters->target().get_size() == 64)
1946 symsize = elfcpp::Elf_sizes<64>::sym_size;
1947 align = 8;
1949 else
1950 gold_unreachable();
1952 off_t off = *poff;
1953 off = align_address(off, align);
1954 off_t startoff = off;
1956 // Save space for the dummy symbol at the start of the section. We
1957 // never bother to write this out--it will just be left as zero.
1958 off += symsize;
1959 unsigned int local_symbol_index = 1;
1961 // Add STT_SECTION symbols for each Output section which needs one.
1962 for (Section_list::iterator p = this->section_list_.begin();
1963 p != this->section_list_.end();
1964 ++p)
1966 if (!(*p)->needs_symtab_index())
1967 (*p)->set_symtab_index(-1U);
1968 else
1970 (*p)->set_symtab_index(local_symbol_index);
1971 ++local_symbol_index;
1972 off += symsize;
1976 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
1977 p != input_objects->relobj_end();
1978 ++p)
1980 unsigned int index = (*p)->finalize_local_symbols(local_symbol_index,
1981 off);
1982 off += (index - local_symbol_index) * symsize;
1983 local_symbol_index = index;
1986 unsigned int local_symcount = local_symbol_index;
1987 gold_assert(local_symcount * symsize == off - startoff);
1989 off_t dynoff;
1990 size_t dyn_global_index;
1991 size_t dyncount;
1992 if (this->dynsym_section_ == NULL)
1994 dynoff = 0;
1995 dyn_global_index = 0;
1996 dyncount = 0;
1998 else
2000 dyn_global_index = this->dynsym_section_->info();
2001 off_t locsize = dyn_global_index * this->dynsym_section_->entsize();
2002 dynoff = this->dynsym_section_->offset() + locsize;
2003 dyncount = (this->dynsym_section_->data_size() - locsize) / symsize;
2004 gold_assert(static_cast<off_t>(dyncount * symsize)
2005 == this->dynsym_section_->data_size() - locsize);
2008 off = symtab->finalize(off, dynoff, dyn_global_index, dyncount,
2009 &this->sympool_, &local_symcount);
2011 if (!parameters->options().strip_all())
2013 this->sympool_.set_string_offsets();
2015 const char* symtab_name = this->namepool_.add(".symtab", false, NULL);
2016 Output_section* osymtab = this->make_output_section(symtab_name,
2017 elfcpp::SHT_SYMTAB,
2019 this->symtab_section_ = osymtab;
2021 Output_section_data* pos = new Output_data_fixed_space(off - startoff,
2022 align);
2023 osymtab->add_output_section_data(pos);
2025 const char* strtab_name = this->namepool_.add(".strtab", false, NULL);
2026 Output_section* ostrtab = this->make_output_section(strtab_name,
2027 elfcpp::SHT_STRTAB,
2030 Output_section_data* pstr = new Output_data_strtab(&this->sympool_);
2031 ostrtab->add_output_section_data(pstr);
2033 osymtab->set_file_offset(startoff);
2034 osymtab->finalize_data_size();
2035 osymtab->set_link_section(ostrtab);
2036 osymtab->set_info(local_symcount);
2037 osymtab->set_entsize(symsize);
2039 *poff = off;
2043 // Create the .shstrtab section, which holds the names of the
2044 // sections. At the time this is called, we have created all the
2045 // output sections except .shstrtab itself.
2047 Output_section*
2048 Layout::create_shstrtab()
2050 // FIXME: We don't need to create a .shstrtab section if we are
2051 // stripping everything.
2053 const char* name = this->namepool_.add(".shstrtab", false, NULL);
2055 Output_section* os = this->make_output_section(name, elfcpp::SHT_STRTAB, 0);
2057 // We can't write out this section until we've set all the section
2058 // names, and we don't set the names of compressed output sections
2059 // until relocations are complete.
2060 os->set_after_input_sections();
2062 Output_section_data* posd = new Output_data_strtab(&this->namepool_);
2063 os->add_output_section_data(posd);
2065 return os;
2068 // Create the section headers. SIZE is 32 or 64. OFF is the file
2069 // offset.
2071 void
2072 Layout::create_shdrs(off_t* poff)
2074 Output_section_headers* oshdrs;
2075 oshdrs = new Output_section_headers(this,
2076 &this->segment_list_,
2077 &this->section_list_,
2078 &this->unattached_section_list_,
2079 &this->namepool_);
2080 off_t off = align_address(*poff, oshdrs->addralign());
2081 oshdrs->set_address_and_file_offset(0, off);
2082 off += oshdrs->data_size();
2083 *poff = off;
2084 this->section_headers_ = oshdrs;
2087 // Create the dynamic symbol table.
2089 void
2090 Layout::create_dynamic_symtab(const Input_objects* input_objects,
2091 Symbol_table* symtab,
2092 Output_section **pdynstr,
2093 unsigned int* plocal_dynamic_count,
2094 std::vector<Symbol*>* pdynamic_symbols,
2095 Versions* pversions)
2097 // Count all the symbols in the dynamic symbol table, and set the
2098 // dynamic symbol indexes.
2100 // Skip symbol 0, which is always all zeroes.
2101 unsigned int index = 1;
2103 // Add STT_SECTION symbols for each Output section which needs one.
2104 for (Section_list::iterator p = this->section_list_.begin();
2105 p != this->section_list_.end();
2106 ++p)
2108 if (!(*p)->needs_dynsym_index())
2109 (*p)->set_dynsym_index(-1U);
2110 else
2112 (*p)->set_dynsym_index(index);
2113 ++index;
2117 // Count the local symbols that need to go in the dynamic symbol table,
2118 // and set the dynamic symbol indexes.
2119 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
2120 p != input_objects->relobj_end();
2121 ++p)
2123 unsigned int new_index = (*p)->set_local_dynsym_indexes(index);
2124 index = new_index;
2127 unsigned int local_symcount = index;
2128 *plocal_dynamic_count = local_symcount;
2130 // FIXME: We have to tell set_dynsym_indexes whether the
2131 // -E/--export-dynamic option was used.
2132 index = symtab->set_dynsym_indexes(index, pdynamic_symbols,
2133 &this->dynpool_, pversions);
2135 int symsize;
2136 unsigned int align;
2137 const int size = parameters->target().get_size();
2138 if (size == 32)
2140 symsize = elfcpp::Elf_sizes<32>::sym_size;
2141 align = 4;
2143 else if (size == 64)
2145 symsize = elfcpp::Elf_sizes<64>::sym_size;
2146 align = 8;
2148 else
2149 gold_unreachable();
2151 // Create the dynamic symbol table section.
2153 Output_section* dynsym = this->choose_output_section(NULL, ".dynsym",
2154 elfcpp::SHT_DYNSYM,
2155 elfcpp::SHF_ALLOC,
2156 false);
2158 Output_section_data* odata = new Output_data_fixed_space(index * symsize,
2159 align);
2160 dynsym->add_output_section_data(odata);
2162 dynsym->set_info(local_symcount);
2163 dynsym->set_entsize(symsize);
2164 dynsym->set_addralign(align);
2166 this->dynsym_section_ = dynsym;
2168 Output_data_dynamic* const odyn = this->dynamic_data_;
2169 odyn->add_section_address(elfcpp::DT_SYMTAB, dynsym);
2170 odyn->add_constant(elfcpp::DT_SYMENT, symsize);
2172 // Create the dynamic string table section.
2174 Output_section* dynstr = this->choose_output_section(NULL, ".dynstr",
2175 elfcpp::SHT_STRTAB,
2176 elfcpp::SHF_ALLOC,
2177 false);
2179 Output_section_data* strdata = new Output_data_strtab(&this->dynpool_);
2180 dynstr->add_output_section_data(strdata);
2182 dynsym->set_link_section(dynstr);
2183 this->dynamic_section_->set_link_section(dynstr);
2185 odyn->add_section_address(elfcpp::DT_STRTAB, dynstr);
2186 odyn->add_section_size(elfcpp::DT_STRSZ, dynstr);
2188 *pdynstr = dynstr;
2190 // Create the hash tables.
2192 if (strcmp(parameters->options().hash_style(), "sysv") == 0
2193 || strcmp(parameters->options().hash_style(), "both") == 0)
2195 unsigned char* phash;
2196 unsigned int hashlen;
2197 Dynobj::create_elf_hash_table(*pdynamic_symbols, local_symcount,
2198 &phash, &hashlen);
2200 Output_section* hashsec = this->choose_output_section(NULL, ".hash",
2201 elfcpp::SHT_HASH,
2202 elfcpp::SHF_ALLOC,
2203 false);
2205 Output_section_data* hashdata = new Output_data_const_buffer(phash,
2206 hashlen,
2207 align);
2208 hashsec->add_output_section_data(hashdata);
2210 hashsec->set_link_section(dynsym);
2211 hashsec->set_entsize(4);
2213 odyn->add_section_address(elfcpp::DT_HASH, hashsec);
2216 if (strcmp(parameters->options().hash_style(), "gnu") == 0
2217 || strcmp(parameters->options().hash_style(), "both") == 0)
2219 unsigned char* phash;
2220 unsigned int hashlen;
2221 Dynobj::create_gnu_hash_table(*pdynamic_symbols, local_symcount,
2222 &phash, &hashlen);
2224 Output_section* hashsec = this->choose_output_section(NULL, ".gnu.hash",
2225 elfcpp::SHT_GNU_HASH,
2226 elfcpp::SHF_ALLOC,
2227 false);
2229 Output_section_data* hashdata = new Output_data_const_buffer(phash,
2230 hashlen,
2231 align);
2232 hashsec->add_output_section_data(hashdata);
2234 hashsec->set_link_section(dynsym);
2235 hashsec->set_entsize(4);
2237 odyn->add_section_address(elfcpp::DT_GNU_HASH, hashsec);
2241 // Assign offsets to each local portion of the dynamic symbol table.
2243 void
2244 Layout::assign_local_dynsym_offsets(const Input_objects* input_objects)
2246 Output_section* dynsym = this->dynsym_section_;
2247 gold_assert(dynsym != NULL);
2249 off_t off = dynsym->offset();
2251 // Skip the dummy symbol at the start of the section.
2252 off += dynsym->entsize();
2254 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
2255 p != input_objects->relobj_end();
2256 ++p)
2258 unsigned int count = (*p)->set_local_dynsym_offset(off);
2259 off += count * dynsym->entsize();
2263 // Create the version sections.
2265 void
2266 Layout::create_version_sections(const Versions* versions,
2267 const Symbol_table* symtab,
2268 unsigned int local_symcount,
2269 const std::vector<Symbol*>& dynamic_symbols,
2270 const Output_section* dynstr)
2272 if (!versions->any_defs() && !versions->any_needs())
2273 return;
2275 switch (parameters->size_and_endianness())
2277 #ifdef HAVE_TARGET_32_LITTLE
2278 case Parameters::TARGET_32_LITTLE:
2279 this->sized_create_version_sections<32, false>(versions, symtab,
2280 local_symcount,
2281 dynamic_symbols, dynstr);
2282 break;
2283 #endif
2284 #ifdef HAVE_TARGET_32_BIG
2285 case Parameters::TARGET_32_BIG:
2286 this->sized_create_version_sections<32, true>(versions, symtab,
2287 local_symcount,
2288 dynamic_symbols, dynstr);
2289 break;
2290 #endif
2291 #ifdef HAVE_TARGET_64_LITTLE
2292 case Parameters::TARGET_64_LITTLE:
2293 this->sized_create_version_sections<64, false>(versions, symtab,
2294 local_symcount,
2295 dynamic_symbols, dynstr);
2296 break;
2297 #endif
2298 #ifdef HAVE_TARGET_64_BIG
2299 case Parameters::TARGET_64_BIG:
2300 this->sized_create_version_sections<64, true>(versions, symtab,
2301 local_symcount,
2302 dynamic_symbols, dynstr);
2303 break;
2304 #endif
2305 default:
2306 gold_unreachable();
2310 // Create the version sections, sized version.
2312 template<int size, bool big_endian>
2313 void
2314 Layout::sized_create_version_sections(
2315 const Versions* versions,
2316 const Symbol_table* symtab,
2317 unsigned int local_symcount,
2318 const std::vector<Symbol*>& dynamic_symbols,
2319 const Output_section* dynstr)
2321 Output_section* vsec = this->choose_output_section(NULL, ".gnu.version",
2322 elfcpp::SHT_GNU_versym,
2323 elfcpp::SHF_ALLOC,
2324 false);
2326 unsigned char* vbuf;
2327 unsigned int vsize;
2328 versions->symbol_section_contents<size, big_endian>(symtab, &this->dynpool_,
2329 local_symcount,
2330 dynamic_symbols,
2331 &vbuf, &vsize);
2333 Output_section_data* vdata = new Output_data_const_buffer(vbuf, vsize, 2);
2335 vsec->add_output_section_data(vdata);
2336 vsec->set_entsize(2);
2337 vsec->set_link_section(this->dynsym_section_);
2339 Output_data_dynamic* const odyn = this->dynamic_data_;
2340 odyn->add_section_address(elfcpp::DT_VERSYM, vsec);
2342 if (versions->any_defs())
2344 Output_section* vdsec;
2345 vdsec= this->choose_output_section(NULL, ".gnu.version_d",
2346 elfcpp::SHT_GNU_verdef,
2347 elfcpp::SHF_ALLOC,
2348 false);
2350 unsigned char* vdbuf;
2351 unsigned int vdsize;
2352 unsigned int vdentries;
2353 versions->def_section_contents<size, big_endian>(&this->dynpool_, &vdbuf,
2354 &vdsize, &vdentries);
2356 Output_section_data* vddata = new Output_data_const_buffer(vdbuf,
2357 vdsize,
2360 vdsec->add_output_section_data(vddata);
2361 vdsec->set_link_section(dynstr);
2362 vdsec->set_info(vdentries);
2364 odyn->add_section_address(elfcpp::DT_VERDEF, vdsec);
2365 odyn->add_constant(elfcpp::DT_VERDEFNUM, vdentries);
2368 if (versions->any_needs())
2370 Output_section* vnsec;
2371 vnsec = this->choose_output_section(NULL, ".gnu.version_r",
2372 elfcpp::SHT_GNU_verneed,
2373 elfcpp::SHF_ALLOC,
2374 false);
2376 unsigned char* vnbuf;
2377 unsigned int vnsize;
2378 unsigned int vnentries;
2379 versions->need_section_contents<size, big_endian>(&this->dynpool_,
2380 &vnbuf, &vnsize,
2381 &vnentries);
2383 Output_section_data* vndata = new Output_data_const_buffer(vnbuf,
2384 vnsize,
2387 vnsec->add_output_section_data(vndata);
2388 vnsec->set_link_section(dynstr);
2389 vnsec->set_info(vnentries);
2391 odyn->add_section_address(elfcpp::DT_VERNEED, vnsec);
2392 odyn->add_constant(elfcpp::DT_VERNEEDNUM, vnentries);
2396 // Create the .interp section and PT_INTERP segment.
2398 void
2399 Layout::create_interp(const Target* target)
2401 const char* interp = this->options_.dynamic_linker();
2402 if (interp == NULL)
2404 interp = target->dynamic_linker();
2405 gold_assert(interp != NULL);
2408 size_t len = strlen(interp) + 1;
2410 Output_section_data* odata = new Output_data_const(interp, len, 1);
2412 Output_section* osec = this->choose_output_section(NULL, ".interp",
2413 elfcpp::SHT_PROGBITS,
2414 elfcpp::SHF_ALLOC,
2415 false);
2416 osec->add_output_section_data(odata);
2418 if (!this->script_options_->saw_phdrs_clause())
2420 Output_segment* oseg = this->make_output_segment(elfcpp::PT_INTERP,
2421 elfcpp::PF_R);
2422 oseg->add_initial_output_section(osec, elfcpp::PF_R);
2426 // Finish the .dynamic section and PT_DYNAMIC segment.
2428 void
2429 Layout::finish_dynamic_section(const Input_objects* input_objects,
2430 const Symbol_table* symtab)
2432 if (!this->script_options_->saw_phdrs_clause())
2434 Output_segment* oseg = this->make_output_segment(elfcpp::PT_DYNAMIC,
2435 (elfcpp::PF_R
2436 | elfcpp::PF_W));
2437 oseg->add_initial_output_section(this->dynamic_section_,
2438 elfcpp::PF_R | elfcpp::PF_W);
2441 Output_data_dynamic* const odyn = this->dynamic_data_;
2443 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
2444 p != input_objects->dynobj_end();
2445 ++p)
2447 // FIXME: Handle --as-needed.
2448 odyn->add_string(elfcpp::DT_NEEDED, (*p)->soname());
2451 if (parameters->options().shared())
2453 const char* soname = this->options_.soname();
2454 if (soname != NULL)
2455 odyn->add_string(elfcpp::DT_SONAME, soname);
2458 // FIXME: Support --init and --fini.
2459 Symbol* sym = symtab->lookup("_init");
2460 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
2461 odyn->add_symbol(elfcpp::DT_INIT, sym);
2463 sym = symtab->lookup("_fini");
2464 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
2465 odyn->add_symbol(elfcpp::DT_FINI, sym);
2467 // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY.
2469 // Add a DT_RPATH entry if needed.
2470 const General_options::Dir_list& rpath(this->options_.rpath());
2471 if (!rpath.empty())
2473 std::string rpath_val;
2474 for (General_options::Dir_list::const_iterator p = rpath.begin();
2475 p != rpath.end();
2476 ++p)
2478 if (rpath_val.empty())
2479 rpath_val = p->name();
2480 else
2482 // Eliminate duplicates.
2483 General_options::Dir_list::const_iterator q;
2484 for (q = rpath.begin(); q != p; ++q)
2485 if (q->name() == p->name())
2486 break;
2487 if (q == p)
2489 rpath_val += ':';
2490 rpath_val += p->name();
2495 odyn->add_string(elfcpp::DT_RPATH, rpath_val);
2498 // Look for text segments that have dynamic relocations.
2499 bool have_textrel = false;
2500 if (!this->script_options_->saw_sections_clause())
2502 for (Segment_list::const_iterator p = this->segment_list_.begin();
2503 p != this->segment_list_.end();
2504 ++p)
2506 if (((*p)->flags() & elfcpp::PF_W) == 0
2507 && (*p)->dynamic_reloc_count() > 0)
2509 have_textrel = true;
2510 break;
2514 else
2516 // We don't know the section -> segment mapping, so we are
2517 // conservative and just look for readonly sections with
2518 // relocations. If those sections wind up in writable segments,
2519 // then we have created an unnecessary DT_TEXTREL entry.
2520 for (Section_list::const_iterator p = this->section_list_.begin();
2521 p != this->section_list_.end();
2522 ++p)
2524 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0
2525 && ((*p)->flags() & elfcpp::SHF_WRITE) == 0
2526 && ((*p)->dynamic_reloc_count() > 0))
2528 have_textrel = true;
2529 break;
2534 // Add a DT_FLAGS entry. We add it even if no flags are set so that
2535 // post-link tools can easily modify these flags if desired.
2536 unsigned int flags = 0;
2537 if (have_textrel)
2539 // Add a DT_TEXTREL for compatibility with older loaders.
2540 odyn->add_constant(elfcpp::DT_TEXTREL, 0);
2541 flags |= elfcpp::DF_TEXTREL;
2543 if (parameters->options().shared() && this->has_static_tls())
2544 flags |= elfcpp::DF_STATIC_TLS;
2545 odyn->add_constant(elfcpp::DT_FLAGS, flags);
2548 // The mapping of .gnu.linkonce section names to real section names.
2550 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
2551 const Layout::Linkonce_mapping Layout::linkonce_mapping[] =
2553 MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d".
2554 MAPPING_INIT("t", ".text"),
2555 MAPPING_INIT("r", ".rodata"),
2556 MAPPING_INIT("d", ".data"),
2557 MAPPING_INIT("b", ".bss"),
2558 MAPPING_INIT("s", ".sdata"),
2559 MAPPING_INIT("sb", ".sbss"),
2560 MAPPING_INIT("s2", ".sdata2"),
2561 MAPPING_INIT("sb2", ".sbss2"),
2562 MAPPING_INIT("wi", ".debug_info"),
2563 MAPPING_INIT("td", ".tdata"),
2564 MAPPING_INIT("tb", ".tbss"),
2565 MAPPING_INIT("lr", ".lrodata"),
2566 MAPPING_INIT("l", ".ldata"),
2567 MAPPING_INIT("lb", ".lbss"),
2569 #undef MAPPING_INIT
2571 const int Layout::linkonce_mapping_count =
2572 sizeof(Layout::linkonce_mapping) / sizeof(Layout::linkonce_mapping[0]);
2574 // Return the name of the output section to use for a .gnu.linkonce
2575 // section. This is based on the default ELF linker script of the old
2576 // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
2577 // to ".text". Set *PLEN to the length of the name. *PLEN is
2578 // initialized to the length of NAME.
2580 const char*
2581 Layout::linkonce_output_name(const char* name, size_t *plen)
2583 const char* s = name + sizeof(".gnu.linkonce") - 1;
2584 if (*s != '.')
2585 return name;
2586 ++s;
2587 const Linkonce_mapping* plm = linkonce_mapping;
2588 for (int i = 0; i < linkonce_mapping_count; ++i, ++plm)
2590 if (strncmp(s, plm->from, plm->fromlen) == 0 && s[plm->fromlen] == '.')
2592 *plen = plm->tolen;
2593 return plm->to;
2596 return name;
2599 // Choose the output section name to use given an input section name.
2600 // Set *PLEN to the length of the name. *PLEN is initialized to the
2601 // length of NAME.
2603 const char*
2604 Layout::output_section_name(const char* name, size_t* plen)
2606 if (Layout::is_linkonce(name))
2608 // .gnu.linkonce sections are laid out as though they were named
2609 // for the sections are placed into.
2610 return Layout::linkonce_output_name(name, plen);
2613 // gcc 4.3 generates the following sorts of section names when it
2614 // needs a section name specific to a function:
2615 // .text.FN
2616 // .rodata.FN
2617 // .sdata2.FN
2618 // .data.FN
2619 // .data.rel.FN
2620 // .data.rel.local.FN
2621 // .data.rel.ro.FN
2622 // .data.rel.ro.local.FN
2623 // .sdata.FN
2624 // .bss.FN
2625 // .sbss.FN
2626 // .tdata.FN
2627 // .tbss.FN
2629 // The GNU linker maps all of those to the part before the .FN,
2630 // except that .data.rel.local.FN is mapped to .data, and
2631 // .data.rel.ro.local.FN is mapped to .data.rel.ro. The sections
2632 // beginning with .data.rel.ro.local are grouped together.
2634 // For an anonymous namespace, the string FN can contain a '.'.
2636 // Also of interest: .rodata.strN.N, .rodata.cstN, both of which the
2637 // GNU linker maps to .rodata.
2639 // The .data.rel.ro sections enable a security feature triggered by
2640 // the -z relro option. Section which need to be relocated at
2641 // program startup time but which may be readonly after startup are
2642 // grouped into .data.rel.ro. They are then put into a PT_GNU_RELRO
2643 // segment. The dynamic linker will make that segment writable,
2644 // perform relocations, and then make it read-only. FIXME: We do
2645 // not yet implement this optimization.
2647 // It is hard to handle this in a principled way.
2649 // These are the rules we follow:
2651 // If the section name has no initial '.', or no dot other than an
2652 // initial '.', we use the name unchanged (i.e., "mysection" and
2653 // ".text" are unchanged).
2655 // If the name starts with ".data.rel.ro" we use ".data.rel.ro".
2657 // Otherwise, we drop the second '.' and everything that comes after
2658 // it (i.e., ".text.XXX" becomes ".text").
2660 const char* s = name;
2661 if (*s != '.')
2662 return name;
2663 ++s;
2664 const char* sdot = strchr(s, '.');
2665 if (sdot == NULL)
2666 return name;
2668 const char* const data_rel_ro = ".data.rel.ro";
2669 if (strncmp(name, data_rel_ro, strlen(data_rel_ro)) == 0)
2671 *plen = strlen(data_rel_ro);
2672 return data_rel_ro;
2675 *plen = sdot - name;
2676 return name;
2679 // Record the signature of a comdat section, and return whether to
2680 // include it in the link. If GROUP is true, this is a regular
2681 // section group. If GROUP is false, this is a group signature
2682 // derived from the name of a linkonce section. We want linkonce
2683 // signatures and group signatures to block each other, but we don't
2684 // want a linkonce signature to block another linkonce signature.
2686 bool
2687 Layout::add_comdat(const char* signature, bool group)
2689 std::string sig(signature);
2690 std::pair<Signatures::iterator, bool> ins(
2691 this->signatures_.insert(std::make_pair(sig, group)));
2693 if (ins.second)
2695 // This is the first time we've seen this signature.
2696 return true;
2699 if (ins.first->second)
2701 // We've already seen a real section group with this signature.
2702 return false;
2704 else if (group)
2706 // This is a real section group, and we've already seen a
2707 // linkonce section with this signature. Record that we've seen
2708 // a section group, and don't include this section group.
2709 ins.first->second = true;
2710 return false;
2712 else
2714 // We've already seen a linkonce section and this is a linkonce
2715 // section. These don't block each other--this may be the same
2716 // symbol name with different section types.
2717 return true;
2721 // Store the allocated sections into the section list.
2723 void
2724 Layout::get_allocated_sections(Section_list* section_list) const
2726 for (Section_list::const_iterator p = this->section_list_.begin();
2727 p != this->section_list_.end();
2728 ++p)
2729 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
2730 section_list->push_back(*p);
2733 // Create an output segment.
2735 Output_segment*
2736 Layout::make_output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
2738 gold_assert(!parameters->options().relocatable());
2739 Output_segment* oseg = new Output_segment(type, flags);
2740 this->segment_list_.push_back(oseg);
2741 return oseg;
2744 // Write out the Output_sections. Most won't have anything to write,
2745 // since most of the data will come from input sections which are
2746 // handled elsewhere. But some Output_sections do have Output_data.
2748 void
2749 Layout::write_output_sections(Output_file* of) const
2751 for (Section_list::const_iterator p = this->section_list_.begin();
2752 p != this->section_list_.end();
2753 ++p)
2755 if (!(*p)->after_input_sections())
2756 (*p)->write(of);
2760 // Write out data not associated with a section or the symbol table.
2762 void
2763 Layout::write_data(const Symbol_table* symtab, Output_file* of) const
2765 if (!parameters->options().strip_all())
2767 const Output_section* symtab_section = this->symtab_section_;
2768 for (Section_list::const_iterator p = this->section_list_.begin();
2769 p != this->section_list_.end();
2770 ++p)
2772 if ((*p)->needs_symtab_index())
2774 gold_assert(symtab_section != NULL);
2775 unsigned int index = (*p)->symtab_index();
2776 gold_assert(index > 0 && index != -1U);
2777 off_t off = (symtab_section->offset()
2778 + index * symtab_section->entsize());
2779 symtab->write_section_symbol(*p, of, off);
2784 const Output_section* dynsym_section = this->dynsym_section_;
2785 for (Section_list::const_iterator p = this->section_list_.begin();
2786 p != this->section_list_.end();
2787 ++p)
2789 if ((*p)->needs_dynsym_index())
2791 gold_assert(dynsym_section != NULL);
2792 unsigned int index = (*p)->dynsym_index();
2793 gold_assert(index > 0 && index != -1U);
2794 off_t off = (dynsym_section->offset()
2795 + index * dynsym_section->entsize());
2796 symtab->write_section_symbol(*p, of, off);
2800 // Write out the Output_data which are not in an Output_section.
2801 for (Data_list::const_iterator p = this->special_output_list_.begin();
2802 p != this->special_output_list_.end();
2803 ++p)
2804 (*p)->write(of);
2807 // Write out the Output_sections which can only be written after the
2808 // input sections are complete.
2810 void
2811 Layout::write_sections_after_input_sections(Output_file* of)
2813 // Determine the final section offsets, and thus the final output
2814 // file size. Note we finalize the .shstrab last, to allow the
2815 // after_input_section sections to modify their section-names before
2816 // writing.
2817 if (this->any_postprocessing_sections_)
2819 off_t off = this->output_file_size_;
2820 off = this->set_section_offsets(off, POSTPROCESSING_SECTIONS_PASS);
2822 // Now that we've finalized the names, we can finalize the shstrab.
2823 off =
2824 this->set_section_offsets(off,
2825 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS);
2827 if (off > this->output_file_size_)
2829 of->resize(off);
2830 this->output_file_size_ = off;
2834 for (Section_list::const_iterator p = this->section_list_.begin();
2835 p != this->section_list_.end();
2836 ++p)
2838 if ((*p)->after_input_sections())
2839 (*p)->write(of);
2842 this->section_headers_->write(of);
2845 // If the build ID requires computing a checksum, do so here, and
2846 // write it out. We compute a checksum over the entire file because
2847 // that is simplest.
2849 void
2850 Layout::write_build_id(Output_file* of) const
2852 if (this->build_id_note_ == NULL)
2853 return;
2855 const unsigned char* iv = of->get_input_view(0, this->output_file_size_);
2857 unsigned char* ov = of->get_output_view(this->build_id_note_->offset(),
2858 this->build_id_note_->data_size());
2860 const char* style = parameters->options().build_id();
2861 if (strcmp(style, "sha1") == 0)
2863 sha1_ctx ctx;
2864 sha1_init_ctx(&ctx);
2865 sha1_process_bytes(iv, this->output_file_size_, &ctx);
2866 sha1_finish_ctx(&ctx, ov);
2868 else if (strcmp(style, "md5") == 0)
2870 md5_ctx ctx;
2871 md5_init_ctx(&ctx);
2872 md5_process_bytes(iv, this->output_file_size_, &ctx);
2873 md5_finish_ctx(&ctx, ov);
2875 else
2876 gold_unreachable();
2878 of->write_output_view(this->build_id_note_->offset(),
2879 this->build_id_note_->data_size(),
2880 ov);
2882 of->free_input_view(0, this->output_file_size_, iv);
2885 // Write out a binary file. This is called after the link is
2886 // complete. IN is the temporary output file we used to generate the
2887 // ELF code. We simply walk through the segments, read them from
2888 // their file offset in IN, and write them to their load address in
2889 // the output file. FIXME: with a bit more work, we could support
2890 // S-records and/or Intel hex format here.
2892 void
2893 Layout::write_binary(Output_file* in) const
2895 gold_assert(this->options_.oformat_enum()
2896 == General_options::OBJECT_FORMAT_BINARY);
2898 // Get the size of the binary file.
2899 uint64_t max_load_address = 0;
2900 for (Segment_list::const_iterator p = this->segment_list_.begin();
2901 p != this->segment_list_.end();
2902 ++p)
2904 if ((*p)->type() == elfcpp::PT_LOAD && (*p)->filesz() > 0)
2906 uint64_t max_paddr = (*p)->paddr() + (*p)->filesz();
2907 if (max_paddr > max_load_address)
2908 max_load_address = max_paddr;
2912 Output_file out(parameters->options().output_file_name());
2913 out.open(max_load_address);
2915 for (Segment_list::const_iterator p = this->segment_list_.begin();
2916 p != this->segment_list_.end();
2917 ++p)
2919 if ((*p)->type() == elfcpp::PT_LOAD && (*p)->filesz() > 0)
2921 const unsigned char* vin = in->get_input_view((*p)->offset(),
2922 (*p)->filesz());
2923 unsigned char* vout = out.get_output_view((*p)->paddr(),
2924 (*p)->filesz());
2925 memcpy(vout, vin, (*p)->filesz());
2926 out.write_output_view((*p)->paddr(), (*p)->filesz(), vout);
2927 in->free_input_view((*p)->offset(), (*p)->filesz(), vin);
2931 out.close();
2934 // Print statistical information to stderr. This is used for --stats.
2936 void
2937 Layout::print_stats() const
2939 this->namepool_.print_stats("section name pool");
2940 this->sympool_.print_stats("output symbol name pool");
2941 this->dynpool_.print_stats("dynamic name pool");
2943 for (Section_list::const_iterator p = this->section_list_.begin();
2944 p != this->section_list_.end();
2945 ++p)
2946 (*p)->print_merge_stats();
2949 // Write_sections_task methods.
2951 // We can always run this task.
2953 Task_token*
2954 Write_sections_task::is_runnable()
2956 return NULL;
2959 // We need to unlock both OUTPUT_SECTIONS_BLOCKER and FINAL_BLOCKER
2960 // when finished.
2962 void
2963 Write_sections_task::locks(Task_locker* tl)
2965 tl->add(this, this->output_sections_blocker_);
2966 tl->add(this, this->final_blocker_);
2969 // Run the task--write out the data.
2971 void
2972 Write_sections_task::run(Workqueue*)
2974 this->layout_->write_output_sections(this->of_);
2977 // Write_data_task methods.
2979 // We can always run this task.
2981 Task_token*
2982 Write_data_task::is_runnable()
2984 return NULL;
2987 // We need to unlock FINAL_BLOCKER when finished.
2989 void
2990 Write_data_task::locks(Task_locker* tl)
2992 tl->add(this, this->final_blocker_);
2995 // Run the task--write out the data.
2997 void
2998 Write_data_task::run(Workqueue*)
3000 this->layout_->write_data(this->symtab_, this->of_);
3003 // Write_symbols_task methods.
3005 // We can always run this task.
3007 Task_token*
3008 Write_symbols_task::is_runnable()
3010 return NULL;
3013 // We need to unlock FINAL_BLOCKER when finished.
3015 void
3016 Write_symbols_task::locks(Task_locker* tl)
3018 tl->add(this, this->final_blocker_);
3021 // Run the task--write out the symbols.
3023 void
3024 Write_symbols_task::run(Workqueue*)
3026 this->symtab_->write_globals(this->input_objects_, this->sympool_,
3027 this->dynpool_, this->of_);
3030 // Write_after_input_sections_task methods.
3032 // We can only run this task after the input sections have completed.
3034 Task_token*
3035 Write_after_input_sections_task::is_runnable()
3037 if (this->input_sections_blocker_->is_blocked())
3038 return this->input_sections_blocker_;
3039 return NULL;
3042 // We need to unlock FINAL_BLOCKER when finished.
3044 void
3045 Write_after_input_sections_task::locks(Task_locker* tl)
3047 tl->add(this, this->final_blocker_);
3050 // Run the task.
3052 void
3053 Write_after_input_sections_task::run(Workqueue*)
3055 this->layout_->write_sections_after_input_sections(this->of_);
3058 // Close_task_runner methods.
3060 // Run the task--close the file.
3062 void
3063 Close_task_runner::run(Workqueue*, const Task*)
3065 // If we need to compute a checksum for the BUILD if, we do so here.
3066 this->layout_->write_build_id(this->of_);
3068 // If we've been asked to create a binary file, we do so here.
3069 if (this->options_->oformat_enum() != General_options::OBJECT_FORMAT_ELF)
3070 this->layout_->write_binary(this->of_);
3072 this->of_->close();
3075 // Instantiate the templates we need. We could use the configure
3076 // script to restrict this to only the ones for implemented targets.
3078 #ifdef HAVE_TARGET_32_LITTLE
3079 template
3080 Output_section*
3081 Layout::layout<32, false>(Sized_relobj<32, false>* object, unsigned int shndx,
3082 const char* name,
3083 const elfcpp::Shdr<32, false>& shdr,
3084 unsigned int, unsigned int, off_t*);
3085 #endif
3087 #ifdef HAVE_TARGET_32_BIG
3088 template
3089 Output_section*
3090 Layout::layout<32, true>(Sized_relobj<32, true>* object, unsigned int shndx,
3091 const char* name,
3092 const elfcpp::Shdr<32, true>& shdr,
3093 unsigned int, unsigned int, off_t*);
3094 #endif
3096 #ifdef HAVE_TARGET_64_LITTLE
3097 template
3098 Output_section*
3099 Layout::layout<64, false>(Sized_relobj<64, false>* object, unsigned int shndx,
3100 const char* name,
3101 const elfcpp::Shdr<64, false>& shdr,
3102 unsigned int, unsigned int, off_t*);
3103 #endif
3105 #ifdef HAVE_TARGET_64_BIG
3106 template
3107 Output_section*
3108 Layout::layout<64, true>(Sized_relobj<64, true>* object, unsigned int shndx,
3109 const char* name,
3110 const elfcpp::Shdr<64, true>& shdr,
3111 unsigned int, unsigned int, off_t*);
3112 #endif
3114 #ifdef HAVE_TARGET_32_LITTLE
3115 template
3116 Output_section*
3117 Layout::layout_reloc<32, false>(Sized_relobj<32, false>* object,
3118 unsigned int reloc_shndx,
3119 const elfcpp::Shdr<32, false>& shdr,
3120 Output_section* data_section,
3121 Relocatable_relocs* rr);
3122 #endif
3124 #ifdef HAVE_TARGET_32_BIG
3125 template
3126 Output_section*
3127 Layout::layout_reloc<32, true>(Sized_relobj<32, true>* object,
3128 unsigned int reloc_shndx,
3129 const elfcpp::Shdr<32, true>& shdr,
3130 Output_section* data_section,
3131 Relocatable_relocs* rr);
3132 #endif
3134 #ifdef HAVE_TARGET_64_LITTLE
3135 template
3136 Output_section*
3137 Layout::layout_reloc<64, false>(Sized_relobj<64, false>* object,
3138 unsigned int reloc_shndx,
3139 const elfcpp::Shdr<64, false>& shdr,
3140 Output_section* data_section,
3141 Relocatable_relocs* rr);
3142 #endif
3144 #ifdef HAVE_TARGET_64_BIG
3145 template
3146 Output_section*
3147 Layout::layout_reloc<64, true>(Sized_relobj<64, true>* object,
3148 unsigned int reloc_shndx,
3149 const elfcpp::Shdr<64, true>& shdr,
3150 Output_section* data_section,
3151 Relocatable_relocs* rr);
3152 #endif
3154 #ifdef HAVE_TARGET_32_LITTLE
3155 template
3156 void
3157 Layout::layout_group<32, false>(Symbol_table* symtab,
3158 Sized_relobj<32, false>* object,
3159 unsigned int,
3160 const char* group_section_name,
3161 const char* signature,
3162 const elfcpp::Shdr<32, false>& shdr,
3163 const elfcpp::Elf_Word* contents);
3164 #endif
3166 #ifdef HAVE_TARGET_32_BIG
3167 template
3168 void
3169 Layout::layout_group<32, true>(Symbol_table* symtab,
3170 Sized_relobj<32, true>* object,
3171 unsigned int,
3172 const char* group_section_name,
3173 const char* signature,
3174 const elfcpp::Shdr<32, true>& shdr,
3175 const elfcpp::Elf_Word* contents);
3176 #endif
3178 #ifdef HAVE_TARGET_64_LITTLE
3179 template
3180 void
3181 Layout::layout_group<64, false>(Symbol_table* symtab,
3182 Sized_relobj<64, false>* object,
3183 unsigned int,
3184 const char* group_section_name,
3185 const char* signature,
3186 const elfcpp::Shdr<64, false>& shdr,
3187 const elfcpp::Elf_Word* contents);
3188 #endif
3190 #ifdef HAVE_TARGET_64_BIG
3191 template
3192 void
3193 Layout::layout_group<64, true>(Symbol_table* symtab,
3194 Sized_relobj<64, true>* object,
3195 unsigned int,
3196 const char* group_section_name,
3197 const char* signature,
3198 const elfcpp::Shdr<64, true>& shdr,
3199 const elfcpp::Elf_Word* contents);
3200 #endif
3202 #ifdef HAVE_TARGET_32_LITTLE
3203 template
3204 Output_section*
3205 Layout::layout_eh_frame<32, false>(Sized_relobj<32, false>* object,
3206 const unsigned char* symbols,
3207 off_t symbols_size,
3208 const unsigned char* symbol_names,
3209 off_t symbol_names_size,
3210 unsigned int shndx,
3211 const elfcpp::Shdr<32, false>& shdr,
3212 unsigned int reloc_shndx,
3213 unsigned int reloc_type,
3214 off_t* off);
3215 #endif
3217 #ifdef HAVE_TARGET_32_BIG
3218 template
3219 Output_section*
3220 Layout::layout_eh_frame<32, true>(Sized_relobj<32, true>* object,
3221 const unsigned char* symbols,
3222 off_t symbols_size,
3223 const unsigned char* symbol_names,
3224 off_t symbol_names_size,
3225 unsigned int shndx,
3226 const elfcpp::Shdr<32, true>& shdr,
3227 unsigned int reloc_shndx,
3228 unsigned int reloc_type,
3229 off_t* off);
3230 #endif
3232 #ifdef HAVE_TARGET_64_LITTLE
3233 template
3234 Output_section*
3235 Layout::layout_eh_frame<64, false>(Sized_relobj<64, false>* object,
3236 const unsigned char* symbols,
3237 off_t symbols_size,
3238 const unsigned char* symbol_names,
3239 off_t symbol_names_size,
3240 unsigned int shndx,
3241 const elfcpp::Shdr<64, false>& shdr,
3242 unsigned int reloc_shndx,
3243 unsigned int reloc_type,
3244 off_t* off);
3245 #endif
3247 #ifdef HAVE_TARGET_64_BIG
3248 template
3249 Output_section*
3250 Layout::layout_eh_frame<64, true>(Sized_relobj<64, true>* object,
3251 const unsigned char* symbols,
3252 off_t symbols_size,
3253 const unsigned char* symbol_names,
3254 off_t symbol_names_size,
3255 unsigned int shndx,
3256 const elfcpp::Shdr<64, true>& shdr,
3257 unsigned int reloc_shndx,
3258 unsigned int reloc_type,
3259 off_t* off);
3260 #endif
3262 } // End namespace gold.