1 // layout.cc -- lay out output file sections for gold
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
30 #include "parameters.h"
35 #include "compressed_output.h"
41 // Layout_task_runner methods.
43 // Lay out the sections. This is called after all the input objects
47 Layout_task_runner::run(Workqueue* workqueue)
49 off_t file_size = this->layout_->finalize(this->input_objects_,
52 // Now we know the final size of the output file and we know where
53 // each piece of information goes.
54 Output_file* of = new Output_file(this->options_,
55 this->input_objects_->target());
58 // Queue up the final set of tasks.
59 gold::queue_final_tasks(this->options_, this->input_objects_,
60 this->symtab_, this->layout_, workqueue, of);
65 Layout::Layout(const General_options& options)
66 : options_(options), namepool_(), sympool_(), dynpool_(), signatures_(),
67 section_name_map_(), segment_list_(), section_list_(),
68 unattached_section_list_(), special_output_list_(),
69 section_headers_(NULL), tls_segment_(NULL), symtab_section_(NULL),
70 dynsym_section_(NULL), dynamic_section_(NULL), dynamic_data_(NULL),
71 eh_frame_section_(NULL), output_file_size_(-1),
72 input_requires_executable_stack_(false),
73 input_with_gnu_stack_note_(false),
74 input_without_gnu_stack_note_(false),
75 has_static_tls_(false)
77 // Make space for more than enough segments for a typical file.
78 // This is just for efficiency--it's OK if we wind up needing more.
79 this->segment_list_.reserve(12);
81 // We expect two unattached Output_data objects: the file header and
82 // the segment headers.
83 this->special_output_list_.reserve(2);
86 // Hash a key we use to look up an output section mapping.
89 Layout::Hash_key::operator()(const Layout::Key& k) const
91 return k.first + k.second.first + k.second.second;
94 // Return whether PREFIX is a prefix of STR.
97 is_prefix_of(const char* prefix, const char* str)
99 return strncmp(prefix, str, strlen(prefix)) == 0;
102 // Returns whether the given section is in the list of
103 // debug-sections-used-by-some-version-of-gdb. Currently,
104 // we've checked versions of gdb up to and including 6.7.1.
106 static const char* gdb_sections[] =
108 // ".debug_aranges", // not used by gdb as of 6.7.1
114 // ".debug_pubnames", // not used by gdb as of 6.7.1
120 is_gdb_debug_section(const char* str)
122 // We can do this faster: binary search or a hashtable. But why bother?
123 for (size_t i = 0; i < sizeof(gdb_sections)/sizeof(*gdb_sections); ++i)
124 if (strcmp(str, gdb_sections[i]) == 0)
129 // Whether to include this section in the link.
131 template<int size, bool big_endian>
133 Layout::include_section(Sized_relobj<size, big_endian>*, const char* name,
134 const elfcpp::Shdr<size, big_endian>& shdr)
136 // Some section types are never linked. Some are only linked when
137 // doing a relocateable link.
138 switch (shdr.get_sh_type())
140 case elfcpp::SHT_NULL:
141 case elfcpp::SHT_SYMTAB:
142 case elfcpp::SHT_DYNSYM:
143 case elfcpp::SHT_STRTAB:
144 case elfcpp::SHT_HASH:
145 case elfcpp::SHT_DYNAMIC:
146 case elfcpp::SHT_SYMTAB_SHNDX:
149 case elfcpp::SHT_RELA:
150 case elfcpp::SHT_REL:
151 case elfcpp::SHT_GROUP:
152 return parameters->output_is_object();
154 case elfcpp::SHT_PROGBITS:
155 if (parameters->strip_debug()
156 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
158 // Debugging sections can only be recognized by name.
159 if (is_prefix_of(".debug", name)
160 || is_prefix_of(".gnu.linkonce.wi.", name)
161 || is_prefix_of(".line", name)
162 || is_prefix_of(".stab", name))
165 if (parameters->strip_debug_gdb()
166 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
168 // Debugging sections can only be recognized by name.
169 if (is_prefix_of(".debug", name)
170 && !is_gdb_debug_section(name))
180 // Return an output section named NAME, or NULL if there is none.
183 Layout::find_output_section(const char* name) const
185 for (Section_name_map::const_iterator p = this->section_name_map_.begin();
186 p != this->section_name_map_.end();
188 if (strcmp(p->second->name(), name) == 0)
193 // Return an output segment of type TYPE, with segment flags SET set
194 // and segment flags CLEAR clear. Return NULL if there is none.
197 Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
198 elfcpp::Elf_Word clear) const
200 for (Segment_list::const_iterator p = this->segment_list_.begin();
201 p != this->segment_list_.end();
203 if (static_cast<elfcpp::PT>((*p)->type()) == type
204 && ((*p)->flags() & set) == set
205 && ((*p)->flags() & clear) == 0)
210 // Return the output section to use for section NAME with type TYPE
211 // and section flags FLAGS.
214 Layout::get_output_section(const char* name, Stringpool::Key name_key,
215 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags)
217 // We should ignore some flags.
218 flags &= ~ (elfcpp::SHF_INFO_LINK
219 | elfcpp::SHF_LINK_ORDER
222 | elfcpp::SHF_STRINGS);
224 const Key key(name_key, std::make_pair(type, flags));
225 const std::pair<Key, Output_section*> v(key, NULL);
226 std::pair<Section_name_map::iterator, bool> ins(
227 this->section_name_map_.insert(v));
230 return ins.first->second;
233 // This is the first time we've seen this name/type/flags
235 Output_section* os = this->make_output_section(name, type, flags);
236 ins.first->second = os;
241 // Return the output section to use for input section SHNDX, with name
242 // NAME, with header HEADER, from object OBJECT. RELOC_SHNDX is the
243 // index of a relocation section which applies to this section, or 0
244 // if none, or -1U if more than one. RELOC_TYPE is the type of the
245 // relocation section if there is one. Set *OFF to the offset of this
246 // input section without the output section. Return NULL if the
247 // section should be discarded. Set *OFF to -1 if the section
248 // contents should not be written directly to the output file, but
249 // will instead receive special handling.
251 template<int size, bool big_endian>
253 Layout::layout(Sized_relobj<size, big_endian>* object, unsigned int shndx,
254 const char* name, const elfcpp::Shdr<size, big_endian>& shdr,
255 unsigned int reloc_shndx, unsigned int, off_t* off)
257 if (!this->include_section(object, name, shdr))
260 // If we are not doing a relocateable link, choose the name to use
261 // for the output section.
262 size_t len = strlen(name);
263 if (!parameters->output_is_object())
264 name = Layout::output_section_name(name, &len);
266 // FIXME: Handle SHF_OS_NONCONFORMING here.
268 // Canonicalize the section name.
269 Stringpool::Key name_key;
270 name = this->namepool_.add_prefix(name, len, &name_key);
272 // Find the output section. The output section is selected based on
273 // the section name, type, and flags.
274 Output_section* os = this->get_output_section(name, name_key,
276 shdr.get_sh_flags());
278 // FIXME: Handle SHF_LINK_ORDER somewhere.
280 *off = os->add_input_section(object, shndx, name, shdr, reloc_shndx);
285 // Special GNU handling of sections name .eh_frame. They will
286 // normally hold exception frame data as defined by the C++ ABI
287 // (http://codesourcery.com/cxx-abi/).
289 template<int size, bool big_endian>
291 Layout::layout_eh_frame(Sized_relobj<size, big_endian>* object,
292 const unsigned char* symbols,
294 const unsigned char* symbol_names,
295 off_t symbol_names_size,
297 const elfcpp::Shdr<size, big_endian>& shdr,
298 unsigned int reloc_shndx, unsigned int reloc_type,
301 gold_assert(shdr.get_sh_type() == elfcpp::SHT_PROGBITS);
302 gold_assert(shdr.get_sh_flags() == elfcpp::SHF_ALLOC);
304 Stringpool::Key name_key;
305 const char* name = this->namepool_.add(".eh_frame", false, &name_key);
307 Output_section* os = this->get_output_section(name, name_key,
308 elfcpp::SHT_PROGBITS,
311 if (this->eh_frame_section_ == NULL)
313 this->eh_frame_section_ = os;
314 this->eh_frame_data_ = new Eh_frame();
315 os->add_output_section_data(this->eh_frame_data_);
317 if (this->options_.create_eh_frame_hdr())
319 Stringpool::Key hdr_name_key;
320 const char* hdr_name = this->namepool_.add(".eh_frame_hdr",
323 Output_section* hdr_os =
324 this->get_output_section(hdr_name, hdr_name_key,
325 elfcpp::SHT_PROGBITS,
328 Eh_frame_hdr* hdr_posd = new Eh_frame_hdr(os, this->eh_frame_data_);
329 hdr_os->add_output_section_data(hdr_posd);
331 hdr_os->set_after_input_sections();
333 Output_segment* hdr_oseg =
334 new Output_segment(elfcpp::PT_GNU_EH_FRAME, elfcpp::PF_R);
335 this->segment_list_.push_back(hdr_oseg);
336 hdr_oseg->add_output_section(hdr_os, elfcpp::PF_R);
338 this->eh_frame_data_->set_eh_frame_hdr(hdr_posd);
342 gold_assert(this->eh_frame_section_ == os);
344 if (this->eh_frame_data_->add_ehframe_input_section(object,
355 // We couldn't handle this .eh_frame section for some reason.
356 // Add it as a normal section.
357 *off = os->add_input_section(object, shndx, name, shdr, reloc_shndx);
363 // Add POSD to an output section using NAME, TYPE, and FLAGS.
366 Layout::add_output_section_data(const char* name, elfcpp::Elf_Word type,
367 elfcpp::Elf_Xword flags,
368 Output_section_data* posd)
370 // Canonicalize the name.
371 Stringpool::Key name_key;
372 name = this->namepool_.add(name, true, &name_key);
374 Output_section* os = this->get_output_section(name, name_key, type, flags);
375 os->add_output_section_data(posd);
378 // Map section flags to segment flags.
381 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags)
383 elfcpp::Elf_Word ret = elfcpp::PF_R;
384 if ((flags & elfcpp::SHF_WRITE) != 0)
386 if ((flags & elfcpp::SHF_EXECINSTR) != 0)
391 // Sometimes we compress sections. This is typically done for
392 // sections that are not part of normal program execution (such as
393 // .debug_* sections), and where the readers of these sections know
394 // how to deal with compressed sections. (To make it easier for them,
395 // we will rename the ouput section in such cases from .foo to
396 // .foo.zlib.nnnn, where nnnn is the uncompressed size.) This routine
397 // doesn't say for certain whether we'll compress -- it depends on
398 // commandline options as well -- just whether this section is a
399 // candidate for compression.
402 is_compressible_debug_section(const char* secname)
404 return (strncmp(secname, ".debug", sizeof(".debug") - 1) == 0);
407 // Make a new Output_section, and attach it to segments as
411 Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
412 elfcpp::Elf_Xword flags)
415 if ((flags & elfcpp::SHF_ALLOC) == 0
416 && this->options_.compress_debug_sections()
417 && is_compressible_debug_section(name))
418 os = new Output_compressed_section(&this->options_, name, type, flags);
420 os = new Output_section(name, type, flags);
422 this->section_list_.push_back(os);
424 if ((flags & elfcpp::SHF_ALLOC) == 0)
425 this->unattached_section_list_.push_back(os);
428 // This output section goes into a PT_LOAD segment.
430 elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags);
432 // The only thing we really care about for PT_LOAD segments is
433 // whether or not they are writable, so that is how we search
434 // for them. People who need segments sorted on some other
435 // basis will have to wait until we implement a mechanism for
436 // them to describe the segments they want.
438 Segment_list::const_iterator p;
439 for (p = this->segment_list_.begin();
440 p != this->segment_list_.end();
443 if ((*p)->type() == elfcpp::PT_LOAD
444 && ((*p)->flags() & elfcpp::PF_W) == (seg_flags & elfcpp::PF_W))
446 (*p)->add_output_section(os, seg_flags);
451 if (p == this->segment_list_.end())
453 Output_segment* oseg = new Output_segment(elfcpp::PT_LOAD,
455 this->segment_list_.push_back(oseg);
456 oseg->add_output_section(os, seg_flags);
459 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
461 if (type == elfcpp::SHT_NOTE)
463 // See if we already have an equivalent PT_NOTE segment.
464 for (p = this->segment_list_.begin();
465 p != segment_list_.end();
468 if ((*p)->type() == elfcpp::PT_NOTE
469 && (((*p)->flags() & elfcpp::PF_W)
470 == (seg_flags & elfcpp::PF_W)))
472 (*p)->add_output_section(os, seg_flags);
477 if (p == this->segment_list_.end())
479 Output_segment* oseg = new Output_segment(elfcpp::PT_NOTE,
481 this->segment_list_.push_back(oseg);
482 oseg->add_output_section(os, seg_flags);
486 // If we see a loadable SHF_TLS section, we create a PT_TLS
487 // segment. There can only be one such segment.
488 if ((flags & elfcpp::SHF_TLS) != 0)
490 if (this->tls_segment_ == NULL)
492 this->tls_segment_ = new Output_segment(elfcpp::PT_TLS,
494 this->segment_list_.push_back(this->tls_segment_);
496 this->tls_segment_->add_output_section(os, seg_flags);
503 // Handle the .note.GNU-stack section at layout time. SEEN_GNU_STACK
504 // is whether we saw a .note.GNU-stack section in the object file.
505 // GNU_STACK_FLAGS is the section flags. The flags give the
506 // protection required for stack memory. We record this in an
507 // executable as a PT_GNU_STACK segment. If an object file does not
508 // have a .note.GNU-stack segment, we must assume that it is an old
509 // object. On some targets that will force an executable stack.
512 Layout::layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags)
515 this->input_without_gnu_stack_note_ = true;
518 this->input_with_gnu_stack_note_ = true;
519 if ((gnu_stack_flags & elfcpp::SHF_EXECINSTR) != 0)
520 this->input_requires_executable_stack_ = true;
524 // Create the dynamic sections which are needed before we read the
528 Layout::create_initial_dynamic_sections(const Input_objects* input_objects,
529 Symbol_table* symtab)
531 if (parameters->doing_static_link())
534 const char* dynamic_name = this->namepool_.add(".dynamic", false, NULL);
535 this->dynamic_section_ = this->make_output_section(dynamic_name,
538 | elfcpp::SHF_WRITE));
540 symtab->define_in_output_data(input_objects->target(), "_DYNAMIC", NULL,
541 this->dynamic_section_, 0, 0,
542 elfcpp::STT_OBJECT, elfcpp::STB_LOCAL,
543 elfcpp::STV_HIDDEN, 0, false, false);
545 this->dynamic_data_ = new Output_data_dynamic(&this->dynpool_);
547 this->dynamic_section_->add_output_section_data(this->dynamic_data_);
550 // For each output section whose name can be represented as C symbol,
551 // define __start and __stop symbols for the section. This is a GNU
555 Layout::define_section_symbols(Symbol_table* symtab, const Target* target)
557 for (Section_list::const_iterator p = this->section_list_.begin();
558 p != this->section_list_.end();
561 const char* const name = (*p)->name();
562 if (name[strspn(name,
564 "ABCDEFGHIJKLMNOPWRSTUVWXYZ"
565 "abcdefghijklmnopqrstuvwxyz"
569 const std::string name_string(name);
570 const std::string start_name("__start_" + name_string);
571 const std::string stop_name("__stop_" + name_string);
573 symtab->define_in_output_data(target,
583 false, // offset_is_from_end
584 false); // only_if_ref
586 symtab->define_in_output_data(target,
596 true, // offset_is_from_end
597 false); // only_if_ref
602 // Find the first read-only PT_LOAD segment, creating one if
606 Layout::find_first_load_seg()
608 for (Segment_list::const_iterator p = this->segment_list_.begin();
609 p != this->segment_list_.end();
612 if ((*p)->type() == elfcpp::PT_LOAD
613 && ((*p)->flags() & elfcpp::PF_R) != 0
614 && ((*p)->flags() & elfcpp::PF_W) == 0)
618 Output_segment* load_seg = new Output_segment(elfcpp::PT_LOAD, elfcpp::PF_R);
619 this->segment_list_.push_back(load_seg);
623 // Finalize the layout. When this is called, we have created all the
624 // output sections and all the output segments which are based on
625 // input sections. We have several things to do, and we have to do
626 // them in the right order, so that we get the right results correctly
629 // 1) Finalize the list of output segments and create the segment
632 // 2) Finalize the dynamic symbol table and associated sections.
634 // 3) Determine the final file offset of all the output segments.
636 // 4) Determine the final file offset of all the SHF_ALLOC output
639 // 5) Create the symbol table sections and the section name table
642 // 6) Finalize the symbol table: set symbol values to their final
643 // value and make a final determination of which symbols are going
644 // into the output symbol table.
646 // 7) Create the section table header.
648 // 8) Determine the final file offset of all the output sections which
649 // are not SHF_ALLOC, including the section table header.
651 // 9) Finalize the ELF file header.
653 // This function returns the size of the output file.
656 Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
658 Target* const target = input_objects->target();
660 target->finalize_sections(this);
662 this->count_local_symbols(input_objects);
664 this->create_gold_note();
665 this->create_executable_stack_info(target);
667 Output_segment* phdr_seg = NULL;
668 if (!parameters->doing_static_link())
670 // There was a dynamic object in the link. We need to create
671 // some information for the dynamic linker.
673 // Create the PT_PHDR segment which will hold the program
675 phdr_seg = new Output_segment(elfcpp::PT_PHDR, elfcpp::PF_R);
676 this->segment_list_.push_back(phdr_seg);
678 // Create the dynamic symbol table, including the hash table.
679 Output_section* dynstr;
680 std::vector<Symbol*> dynamic_symbols;
681 unsigned int local_dynamic_count;
683 this->create_dynamic_symtab(input_objects, target, symtab, &dynstr,
684 &local_dynamic_count, &dynamic_symbols,
687 // Create the .interp section to hold the name of the
688 // interpreter, and put it in a PT_INTERP segment.
689 if (!parameters->output_is_shared())
690 this->create_interp(target);
692 // Finish the .dynamic section to hold the dynamic data, and put
693 // it in a PT_DYNAMIC segment.
694 this->finish_dynamic_section(input_objects, symtab);
696 // We should have added everything we need to the dynamic string
698 this->dynpool_.set_string_offsets();
700 // Create the version sections. We can't do this until the
701 // dynamic string table is complete.
702 this->create_version_sections(&versions, symtab, local_dynamic_count,
703 dynamic_symbols, dynstr);
706 // FIXME: Handle PT_GNU_STACK.
708 Output_segment* load_seg = this->find_first_load_seg();
710 // Lay out the segment headers.
711 Output_segment_headers* segment_headers;
712 segment_headers = new Output_segment_headers(this->segment_list_);
713 load_seg->add_initial_output_data(segment_headers);
714 this->special_output_list_.push_back(segment_headers);
715 if (phdr_seg != NULL)
716 phdr_seg->add_initial_output_data(segment_headers);
718 // Lay out the file header.
719 Output_file_header* file_header;
720 file_header = new Output_file_header(target, symtab, segment_headers);
721 load_seg->add_initial_output_data(file_header);
722 this->special_output_list_.push_back(file_header);
724 // We set the output section indexes in set_segment_offsets and
725 // set_section_indexes.
726 unsigned int shndx = 1;
728 // Set the file offsets of all the segments, and all the sections
730 off_t off = this->set_segment_offsets(target, load_seg, &shndx);
732 // Create the symbol table sections.
733 this->create_symtab_sections(input_objects, symtab, &off);
734 if (!parameters->doing_static_link())
735 this->assign_local_dynsym_offsets(input_objects);
737 // Create the .shstrtab section.
738 Output_section* shstrtab_section = this->create_shstrtab();
740 // Set the file offsets of all the non-data sections which don't
741 // have to wait for the input sections.
742 off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS);
744 // Now that all sections have been created, set the section indexes.
745 shndx = this->set_section_indexes(shndx);
747 // Create the section table header.
748 this->create_shdrs(&off);
750 file_header->set_section_info(this->section_headers_, shstrtab_section);
752 // Now we know exactly where everything goes in the output file
753 // (except for non-allocated sections which require postprocessing).
754 Output_data::layout_complete();
756 this->output_file_size_ = off;
761 // Create a .note section for an executable or shared library. This
762 // records the version of gold used to create the binary.
765 Layout::create_gold_note()
767 if (parameters->output_is_object())
770 // Authorities all agree that the values in a .note field should
771 // be aligned on 4-byte boundaries for 32-bit binaries. However,
772 // they differ on what the alignment is for 64-bit binaries.
773 // The GABI says unambiguously they take 8-byte alignment:
774 // http://sco.com/developers/gabi/latest/ch5.pheader.html#note_section
775 // Other documentation says alignment should always be 4 bytes:
776 // http://www.netbsd.org/docs/kernel/elf-notes.html#note-format
777 // GNU ld and GNU readelf both support the latter (at least as of
778 // version 2.16.91), and glibc always generates the latter for
779 // .note.ABI-tag (as of version 1.6), so that's the one we go with
781 #ifdef GABI_FORMAT_FOR_DOTNOTE_SECTION // This is not defined by default.
782 const int size = parameters->get_size();
787 // The contents of the .note section.
788 const char* name = "GNU";
789 std::string desc(std::string("gold ") + gold::get_version_string());
790 size_t namesz = strlen(name) + 1;
791 size_t aligned_namesz = align_address(namesz, size / 8);
792 size_t descsz = desc.length() + 1;
793 size_t aligned_descsz = align_address(descsz, size / 8);
794 const int note_type = 4;
796 size_t notesz = 3 * (size / 8) + aligned_namesz + aligned_descsz;
798 unsigned char buffer[128];
799 gold_assert(sizeof buffer >= notesz);
800 memset(buffer, 0, notesz);
802 bool is_big_endian = parameters->is_big_endian();
808 elfcpp::Swap<32, false>::writeval(buffer, namesz);
809 elfcpp::Swap<32, false>::writeval(buffer + 4, descsz);
810 elfcpp::Swap<32, false>::writeval(buffer + 8, note_type);
814 elfcpp::Swap<32, true>::writeval(buffer, namesz);
815 elfcpp::Swap<32, true>::writeval(buffer + 4, descsz);
816 elfcpp::Swap<32, true>::writeval(buffer + 8, note_type);
823 elfcpp::Swap<64, false>::writeval(buffer, namesz);
824 elfcpp::Swap<64, false>::writeval(buffer + 8, descsz);
825 elfcpp::Swap<64, false>::writeval(buffer + 16, note_type);
829 elfcpp::Swap<64, true>::writeval(buffer, namesz);
830 elfcpp::Swap<64, true>::writeval(buffer + 8, descsz);
831 elfcpp::Swap<64, true>::writeval(buffer + 16, note_type);
837 memcpy(buffer + 3 * (size / 8), name, namesz);
838 memcpy(buffer + 3 * (size / 8) + aligned_namesz, desc.data(), descsz);
840 const char* note_name = this->namepool_.add(".note", false, NULL);
841 Output_section* os = this->make_output_section(note_name,
844 Output_section_data* posd = new Output_data_const(buffer, notesz,
846 os->add_output_section_data(posd);
849 // Record whether the stack should be executable. This can be set
850 // from the command line using the -z execstack or -z noexecstack
851 // options. Otherwise, if any input file has a .note.GNU-stack
852 // section with the SHF_EXECINSTR flag set, the stack should be
853 // executable. Otherwise, if at least one input file a
854 // .note.GNU-stack section, and some input file has no .note.GNU-stack
855 // section, we use the target default for whether the stack should be
856 // executable. Otherwise, we don't generate a stack note. When
857 // generating a object file, we create a .note.GNU-stack section with
858 // the appropriate marking. When generating an executable or shared
859 // library, we create a PT_GNU_STACK segment.
862 Layout::create_executable_stack_info(const Target* target)
864 bool is_stack_executable;
865 if (this->options_.is_execstack_set())
866 is_stack_executable = this->options_.is_stack_executable();
867 else if (!this->input_with_gnu_stack_note_)
871 if (this->input_requires_executable_stack_)
872 is_stack_executable = true;
873 else if (this->input_without_gnu_stack_note_)
874 is_stack_executable = target->is_default_stack_executable();
876 is_stack_executable = false;
879 if (parameters->output_is_object())
881 const char* name = this->namepool_.add(".note.GNU-stack", false, NULL);
882 elfcpp::Elf_Xword flags = 0;
883 if (is_stack_executable)
884 flags |= elfcpp::SHF_EXECINSTR;
885 this->make_output_section(name, elfcpp::SHT_PROGBITS, flags);
889 int flags = elfcpp::PF_R | elfcpp::PF_W;
890 if (is_stack_executable)
891 flags |= elfcpp::PF_X;
892 Output_segment* oseg = new Output_segment(elfcpp::PT_GNU_STACK, flags);
893 this->segment_list_.push_back(oseg);
897 // Return whether SEG1 should be before SEG2 in the output file. This
898 // is based entirely on the segment type and flags. When this is
899 // called the segment addresses has normally not yet been set.
902 Layout::segment_precedes(const Output_segment* seg1,
903 const Output_segment* seg2)
905 elfcpp::Elf_Word type1 = seg1->type();
906 elfcpp::Elf_Word type2 = seg2->type();
908 // The single PT_PHDR segment is required to precede any loadable
909 // segment. We simply make it always first.
910 if (type1 == elfcpp::PT_PHDR)
912 gold_assert(type2 != elfcpp::PT_PHDR);
915 if (type2 == elfcpp::PT_PHDR)
918 // The single PT_INTERP segment is required to precede any loadable
919 // segment. We simply make it always second.
920 if (type1 == elfcpp::PT_INTERP)
922 gold_assert(type2 != elfcpp::PT_INTERP);
925 if (type2 == elfcpp::PT_INTERP)
928 // We then put PT_LOAD segments before any other segments.
929 if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD)
931 if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD)
934 // We put the PT_TLS segment last, because that is where the dynamic
935 // linker expects to find it (this is just for efficiency; other
936 // positions would also work correctly).
937 if (type1 == elfcpp::PT_TLS && type2 != elfcpp::PT_TLS)
939 if (type2 == elfcpp::PT_TLS && type1 != elfcpp::PT_TLS)
942 const elfcpp::Elf_Word flags1 = seg1->flags();
943 const elfcpp::Elf_Word flags2 = seg2->flags();
945 // The order of non-PT_LOAD segments is unimportant. We simply sort
946 // by the numeric segment type and flags values. There should not
947 // be more than one segment with the same type and flags.
948 if (type1 != elfcpp::PT_LOAD)
951 return type1 < type2;
952 gold_assert(flags1 != flags2);
953 return flags1 < flags2;
956 // We sort PT_LOAD segments based on the flags. Readonly segments
957 // come before writable segments. Then executable segments come
958 // before non-executable segments. Then the unlikely case of a
959 // non-readable segment comes before the normal case of a readable
960 // segment. If there are multiple segments with the same type and
961 // flags, we require that the address be set, and we sort by
962 // virtual address and then physical address.
963 if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W))
964 return (flags1 & elfcpp::PF_W) == 0;
965 if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X))
966 return (flags1 & elfcpp::PF_X) != 0;
967 if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R))
968 return (flags1 & elfcpp::PF_R) == 0;
970 uint64_t vaddr1 = seg1->vaddr();
971 uint64_t vaddr2 = seg2->vaddr();
972 if (vaddr1 != vaddr2)
973 return vaddr1 < vaddr2;
975 uint64_t paddr1 = seg1->paddr();
976 uint64_t paddr2 = seg2->paddr();
977 gold_assert(paddr1 != paddr2);
978 return paddr1 < paddr2;
981 // Set the file offsets of all the segments, and all the sections they
982 // contain. They have all been created. LOAD_SEG must be be laid out
983 // first. Return the offset of the data to follow.
986 Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
987 unsigned int *pshndx)
989 // Sort them into the final order.
990 std::sort(this->segment_list_.begin(), this->segment_list_.end(),
991 Layout::Compare_segments());
993 // Find the PT_LOAD segments, and set their addresses and offsets
994 // and their section's addresses and offsets.
996 if (parameters->output_is_shared())
998 else if (options_.user_set_text_segment_address())
999 addr = options_.text_segment_address();
1001 addr = target->default_text_segment_address();
1003 bool was_readonly = false;
1004 for (Segment_list::iterator p = this->segment_list_.begin();
1005 p != this->segment_list_.end();
1008 if ((*p)->type() == elfcpp::PT_LOAD)
1010 if (load_seg != NULL && load_seg != *p)
1014 // If the last segment was readonly, and this one is not,
1015 // then skip the address forward one page, maintaining the
1016 // same position within the page. This lets us store both
1017 // segments overlapping on a single page in the file, but
1018 // the loader will put them on different pages in memory.
1020 uint64_t orig_addr = addr;
1021 uint64_t orig_off = off;
1023 uint64_t aligned_addr = addr;
1024 uint64_t abi_pagesize = target->abi_pagesize();
1026 // FIXME: This should depend on the -n and -N options.
1027 (*p)->set_minimum_addralign(target->common_pagesize());
1029 if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0)
1031 uint64_t align = (*p)->addralign();
1033 addr = align_address(addr, align);
1034 aligned_addr = addr;
1035 if ((addr & (abi_pagesize - 1)) != 0)
1036 addr = addr + abi_pagesize;
1039 unsigned int shndx_hold = *pshndx;
1040 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
1041 uint64_t new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
1043 // Now that we know the size of this segment, we may be able
1044 // to save a page in memory, at the cost of wasting some
1045 // file space, by instead aligning to the start of a new
1046 // page. Here we use the real machine page size rather than
1047 // the ABI mandated page size.
1049 if (aligned_addr != addr)
1051 uint64_t common_pagesize = target->common_pagesize();
1052 uint64_t first_off = (common_pagesize
1054 & (common_pagesize - 1)));
1055 uint64_t last_off = new_addr & (common_pagesize - 1);
1058 && ((aligned_addr & ~ (common_pagesize - 1))
1059 != (new_addr & ~ (common_pagesize - 1)))
1060 && first_off + last_off <= common_pagesize)
1062 *pshndx = shndx_hold;
1063 addr = align_address(aligned_addr, common_pagesize);
1064 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
1065 new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
1071 if (((*p)->flags() & elfcpp::PF_W) == 0)
1072 was_readonly = true;
1076 // Handle the non-PT_LOAD segments, setting their offsets from their
1077 // section's offsets.
1078 for (Segment_list::iterator p = this->segment_list_.begin();
1079 p != this->segment_list_.end();
1082 if ((*p)->type() != elfcpp::PT_LOAD)
1086 // Set the TLS offsets for each section in the PT_TLS segment.
1087 if (this->tls_segment_ != NULL)
1088 this->tls_segment_->set_tls_offsets();
1093 // Set the file offset of all the sections not associated with a
1097 Layout::set_section_offsets(off_t off, Layout::Section_offset_pass pass)
1099 for (Section_list::iterator p = this->unattached_section_list_.begin();
1100 p != this->unattached_section_list_.end();
1103 // The symtab section is handled in create_symtab_sections.
1104 if (*p == this->symtab_section_)
1107 if (pass == BEFORE_INPUT_SECTIONS_PASS
1108 && (*p)->requires_postprocessing())
1109 (*p)->create_postprocessing_buffer();
1111 if (pass == BEFORE_INPUT_SECTIONS_PASS
1112 && (*p)->after_input_sections())
1114 else if (pass == AFTER_INPUT_SECTIONS_PASS
1115 && (!(*p)->after_input_sections()
1116 || (*p)->type() == elfcpp::SHT_STRTAB))
1118 else if (pass == STRTAB_AFTER_INPUT_SECTIONS_PASS
1119 && (!(*p)->after_input_sections()
1120 || (*p)->type() != elfcpp::SHT_STRTAB))
1123 off = align_address(off, (*p)->addralign());
1124 (*p)->set_file_offset(off);
1125 (*p)->finalize_data_size();
1126 off += (*p)->data_size();
1128 // At this point the name must be set.
1129 if (pass != STRTAB_AFTER_INPUT_SECTIONS_PASS)
1130 this->namepool_.add((*p)->name(), false, NULL);
1135 // Set the section indexes of all the sections not associated with a
1139 Layout::set_section_indexes(unsigned int shndx)
1141 for (Section_list::iterator p = this->unattached_section_list_.begin();
1142 p != this->unattached_section_list_.end();
1145 (*p)->set_out_shndx(shndx);
1151 // Count the local symbols in the regular symbol table and the dynamic
1152 // symbol table, and build the respective string pools.
1155 Layout::count_local_symbols(const Input_objects* input_objects)
1157 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
1158 p != input_objects->relobj_end();
1161 Task_lock_obj<Object> tlo(**p);
1162 (*p)->count_local_symbols(&this->sympool_, &this->dynpool_);
1166 // Create the symbol table sections. Here we also set the final
1167 // values of the symbols. At this point all the loadable sections are
1171 Layout::create_symtab_sections(const Input_objects* input_objects,
1172 Symbol_table* symtab,
1177 if (parameters->get_size() == 32)
1179 symsize = elfcpp::Elf_sizes<32>::sym_size;
1182 else if (parameters->get_size() == 64)
1184 symsize = elfcpp::Elf_sizes<64>::sym_size;
1191 off = align_address(off, align);
1192 off_t startoff = off;
1194 // Save space for the dummy symbol at the start of the section. We
1195 // never bother to write this out--it will just be left as zero.
1197 unsigned int local_symbol_index = 1;
1199 // Add STT_SECTION symbols for each Output section which needs one.
1200 for (Section_list::iterator p = this->section_list_.begin();
1201 p != this->section_list_.end();
1204 if (!(*p)->needs_symtab_index())
1205 (*p)->set_symtab_index(-1U);
1208 (*p)->set_symtab_index(local_symbol_index);
1209 ++local_symbol_index;
1214 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
1215 p != input_objects->relobj_end();
1218 unsigned int index = (*p)->finalize_local_symbols(local_symbol_index,
1220 off += (index - local_symbol_index) * symsize;
1221 local_symbol_index = index;
1224 unsigned int local_symcount = local_symbol_index;
1225 gold_assert(local_symcount * symsize == off - startoff);
1228 size_t dyn_global_index;
1230 if (this->dynsym_section_ == NULL)
1233 dyn_global_index = 0;
1238 dyn_global_index = this->dynsym_section_->info();
1239 off_t locsize = dyn_global_index * this->dynsym_section_->entsize();
1240 dynoff = this->dynsym_section_->offset() + locsize;
1241 dyncount = (this->dynsym_section_->data_size() - locsize) / symsize;
1242 gold_assert(static_cast<off_t>(dyncount * symsize)
1243 == this->dynsym_section_->data_size() - locsize);
1246 off = symtab->finalize(local_symcount, off, dynoff, dyn_global_index,
1247 dyncount, &this->sympool_);
1249 if (!parameters->strip_all())
1251 this->sympool_.set_string_offsets();
1253 const char* symtab_name = this->namepool_.add(".symtab", false, NULL);
1254 Output_section* osymtab = this->make_output_section(symtab_name,
1257 this->symtab_section_ = osymtab;
1259 Output_section_data* pos = new Output_data_fixed_space(off - startoff,
1261 osymtab->add_output_section_data(pos);
1263 const char* strtab_name = this->namepool_.add(".strtab", false, NULL);
1264 Output_section* ostrtab = this->make_output_section(strtab_name,
1268 Output_section_data* pstr = new Output_data_strtab(&this->sympool_);
1269 ostrtab->add_output_section_data(pstr);
1271 osymtab->set_file_offset(startoff);
1272 osymtab->finalize_data_size();
1273 osymtab->set_link_section(ostrtab);
1274 osymtab->set_info(local_symcount);
1275 osymtab->set_entsize(symsize);
1281 // Create the .shstrtab section, which holds the names of the
1282 // sections. At the time this is called, we have created all the
1283 // output sections except .shstrtab itself.
1286 Layout::create_shstrtab()
1288 // FIXME: We don't need to create a .shstrtab section if we are
1289 // stripping everything.
1291 const char* name = this->namepool_.add(".shstrtab", false, NULL);
1293 Output_section* os = this->make_output_section(name, elfcpp::SHT_STRTAB, 0);
1295 // We can't write out this section until we've set all the section
1296 // names, and we don't set the names of compressed output sections
1297 // until relocations are complete.
1298 os->set_after_input_sections();
1300 Output_section_data* posd = new Output_data_strtab(&this->namepool_);
1301 os->add_output_section_data(posd);
1306 // Create the section headers. SIZE is 32 or 64. OFF is the file
1310 Layout::create_shdrs(off_t* poff)
1312 Output_section_headers* oshdrs;
1313 oshdrs = new Output_section_headers(this,
1314 &this->segment_list_,
1315 &this->unattached_section_list_,
1317 off_t off = align_address(*poff, oshdrs->addralign());
1318 oshdrs->set_address_and_file_offset(0, off);
1319 off += oshdrs->data_size();
1321 this->section_headers_ = oshdrs;
1324 // Create the dynamic symbol table.
1327 Layout::create_dynamic_symtab(const Input_objects* input_objects,
1328 const Target* target, Symbol_table* symtab,
1329 Output_section **pdynstr,
1330 unsigned int* plocal_dynamic_count,
1331 std::vector<Symbol*>* pdynamic_symbols,
1332 Versions* pversions)
1334 // Count all the symbols in the dynamic symbol table, and set the
1335 // dynamic symbol indexes.
1337 // Skip symbol 0, which is always all zeroes.
1338 unsigned int index = 1;
1340 // Add STT_SECTION symbols for each Output section which needs one.
1341 for (Section_list::iterator p = this->section_list_.begin();
1342 p != this->section_list_.end();
1345 if (!(*p)->needs_dynsym_index())
1346 (*p)->set_dynsym_index(-1U);
1349 (*p)->set_dynsym_index(index);
1354 // Count the local symbols that need to go in the dynamic symbol table,
1355 // and set the dynamic symbol indexes.
1356 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
1357 p != input_objects->relobj_end();
1360 unsigned int new_index = (*p)->set_local_dynsym_indexes(index);
1364 unsigned int local_symcount = index;
1365 *plocal_dynamic_count = local_symcount;
1367 // FIXME: We have to tell set_dynsym_indexes whether the
1368 // -E/--export-dynamic option was used.
1369 index = symtab->set_dynsym_indexes(target, index, pdynamic_symbols,
1370 &this->dynpool_, pversions);
1374 const int size = parameters->get_size();
1377 symsize = elfcpp::Elf_sizes<32>::sym_size;
1380 else if (size == 64)
1382 symsize = elfcpp::Elf_sizes<64>::sym_size;
1388 // Create the dynamic symbol table section.
1390 const char* dynsym_name = this->namepool_.add(".dynsym", false, NULL);
1391 Output_section* dynsym = this->make_output_section(dynsym_name,
1395 Output_section_data* odata = new Output_data_fixed_space(index * symsize,
1397 dynsym->add_output_section_data(odata);
1399 dynsym->set_info(local_symcount);
1400 dynsym->set_entsize(symsize);
1401 dynsym->set_addralign(align);
1403 this->dynsym_section_ = dynsym;
1405 Output_data_dynamic* const odyn = this->dynamic_data_;
1406 odyn->add_section_address(elfcpp::DT_SYMTAB, dynsym);
1407 odyn->add_constant(elfcpp::DT_SYMENT, symsize);
1409 // Create the dynamic string table section.
1411 const char* dynstr_name = this->namepool_.add(".dynstr", false, NULL);
1412 Output_section* dynstr = this->make_output_section(dynstr_name,
1416 Output_section_data* strdata = new Output_data_strtab(&this->dynpool_);
1417 dynstr->add_output_section_data(strdata);
1419 dynsym->set_link_section(dynstr);
1420 this->dynamic_section_->set_link_section(dynstr);
1422 odyn->add_section_address(elfcpp::DT_STRTAB, dynstr);
1423 odyn->add_section_size(elfcpp::DT_STRSZ, dynstr);
1427 // Create the hash tables.
1429 // FIXME: We need an option to create a GNU hash table.
1431 unsigned char* phash;
1432 unsigned int hashlen;
1433 Dynobj::create_elf_hash_table(*pdynamic_symbols, local_symcount,
1436 const char* hash_name = this->namepool_.add(".hash", false, NULL);
1437 Output_section* hashsec = this->make_output_section(hash_name,
1441 Output_section_data* hashdata = new Output_data_const_buffer(phash,
1444 hashsec->add_output_section_data(hashdata);
1446 hashsec->set_link_section(dynsym);
1447 hashsec->set_entsize(4);
1449 odyn->add_section_address(elfcpp::DT_HASH, hashsec);
1452 // Assign offsets to each local portion of the dynamic symbol table.
1455 Layout::assign_local_dynsym_offsets(const Input_objects* input_objects)
1457 Output_section* dynsym = this->dynsym_section_;
1458 gold_assert(dynsym != NULL);
1460 off_t off = dynsym->offset();
1462 // Skip the dummy symbol at the start of the section.
1463 off += dynsym->entsize();
1465 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
1466 p != input_objects->relobj_end();
1469 unsigned int count = (*p)->set_local_dynsym_offset(off);
1470 off += count * dynsym->entsize();
1474 // Create the version sections.
1477 Layout::create_version_sections(const Versions* versions,
1478 const Symbol_table* symtab,
1479 unsigned int local_symcount,
1480 const std::vector<Symbol*>& dynamic_symbols,
1481 const Output_section* dynstr)
1483 if (!versions->any_defs() && !versions->any_needs())
1486 if (parameters->get_size() == 32)
1488 if (parameters->is_big_endian())
1490 #ifdef HAVE_TARGET_32_BIG
1491 this->sized_create_version_sections
1492 SELECT_SIZE_ENDIAN_NAME(32, true)(
1493 versions, symtab, local_symcount, dynamic_symbols, dynstr
1494 SELECT_SIZE_ENDIAN(32, true));
1501 #ifdef HAVE_TARGET_32_LITTLE
1502 this->sized_create_version_sections
1503 SELECT_SIZE_ENDIAN_NAME(32, false)(
1504 versions, symtab, local_symcount, dynamic_symbols, dynstr
1505 SELECT_SIZE_ENDIAN(32, false));
1511 else if (parameters->get_size() == 64)
1513 if (parameters->is_big_endian())
1515 #ifdef HAVE_TARGET_64_BIG
1516 this->sized_create_version_sections
1517 SELECT_SIZE_ENDIAN_NAME(64, true)(
1518 versions, symtab, local_symcount, dynamic_symbols, dynstr
1519 SELECT_SIZE_ENDIAN(64, true));
1526 #ifdef HAVE_TARGET_64_LITTLE
1527 this->sized_create_version_sections
1528 SELECT_SIZE_ENDIAN_NAME(64, false)(
1529 versions, symtab, local_symcount, dynamic_symbols, dynstr
1530 SELECT_SIZE_ENDIAN(64, false));
1540 // Create the version sections, sized version.
1542 template<int size, bool big_endian>
1544 Layout::sized_create_version_sections(
1545 const Versions* versions,
1546 const Symbol_table* symtab,
1547 unsigned int local_symcount,
1548 const std::vector<Symbol*>& dynamic_symbols,
1549 const Output_section* dynstr
1552 const char* vname = this->namepool_.add(".gnu.version", false, NULL);
1553 Output_section* vsec = this->make_output_section(vname,
1554 elfcpp::SHT_GNU_versym,
1557 unsigned char* vbuf;
1559 versions->symbol_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1560 symtab, &this->dynpool_, local_symcount, dynamic_symbols, &vbuf, &vsize
1561 SELECT_SIZE_ENDIAN(size, big_endian));
1563 Output_section_data* vdata = new Output_data_const_buffer(vbuf, vsize, 2);
1565 vsec->add_output_section_data(vdata);
1566 vsec->set_entsize(2);
1567 vsec->set_link_section(this->dynsym_section_);
1569 Output_data_dynamic* const odyn = this->dynamic_data_;
1570 odyn->add_section_address(elfcpp::DT_VERSYM, vsec);
1572 if (versions->any_defs())
1574 const char* vdname = this->namepool_.add(".gnu.version_d", false, NULL);
1575 Output_section *vdsec;
1576 vdsec = this->make_output_section(vdname, elfcpp::SHT_GNU_verdef,
1579 unsigned char* vdbuf;
1580 unsigned int vdsize;
1581 unsigned int vdentries;
1582 versions->def_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1583 &this->dynpool_, &vdbuf, &vdsize, &vdentries
1584 SELECT_SIZE_ENDIAN(size, big_endian));
1586 Output_section_data* vddata = new Output_data_const_buffer(vdbuf,
1590 vdsec->add_output_section_data(vddata);
1591 vdsec->set_link_section(dynstr);
1592 vdsec->set_info(vdentries);
1594 odyn->add_section_address(elfcpp::DT_VERDEF, vdsec);
1595 odyn->add_constant(elfcpp::DT_VERDEFNUM, vdentries);
1598 if (versions->any_needs())
1600 const char* vnname = this->namepool_.add(".gnu.version_r", false, NULL);
1601 Output_section* vnsec;
1602 vnsec = this->make_output_section(vnname, elfcpp::SHT_GNU_verneed,
1605 unsigned char* vnbuf;
1606 unsigned int vnsize;
1607 unsigned int vnentries;
1608 versions->need_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)
1609 (&this->dynpool_, &vnbuf, &vnsize, &vnentries
1610 SELECT_SIZE_ENDIAN(size, big_endian));
1612 Output_section_data* vndata = new Output_data_const_buffer(vnbuf,
1616 vnsec->add_output_section_data(vndata);
1617 vnsec->set_link_section(dynstr);
1618 vnsec->set_info(vnentries);
1620 odyn->add_section_address(elfcpp::DT_VERNEED, vnsec);
1621 odyn->add_constant(elfcpp::DT_VERNEEDNUM, vnentries);
1625 // Create the .interp section and PT_INTERP segment.
1628 Layout::create_interp(const Target* target)
1630 const char* interp = this->options_.dynamic_linker();
1633 interp = target->dynamic_linker();
1634 gold_assert(interp != NULL);
1637 size_t len = strlen(interp) + 1;
1639 Output_section_data* odata = new Output_data_const(interp, len, 1);
1641 const char* interp_name = this->namepool_.add(".interp", false, NULL);
1642 Output_section* osec = this->make_output_section(interp_name,
1643 elfcpp::SHT_PROGBITS,
1645 osec->add_output_section_data(odata);
1647 Output_segment* oseg = new Output_segment(elfcpp::PT_INTERP, elfcpp::PF_R);
1648 this->segment_list_.push_back(oseg);
1649 oseg->add_initial_output_section(osec, elfcpp::PF_R);
1652 // Finish the .dynamic section and PT_DYNAMIC segment.
1655 Layout::finish_dynamic_section(const Input_objects* input_objects,
1656 const Symbol_table* symtab)
1658 Output_segment* oseg = new Output_segment(elfcpp::PT_DYNAMIC,
1659 elfcpp::PF_R | elfcpp::PF_W);
1660 this->segment_list_.push_back(oseg);
1661 oseg->add_initial_output_section(this->dynamic_section_,
1662 elfcpp::PF_R | elfcpp::PF_W);
1664 Output_data_dynamic* const odyn = this->dynamic_data_;
1666 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
1667 p != input_objects->dynobj_end();
1670 // FIXME: Handle --as-needed.
1671 odyn->add_string(elfcpp::DT_NEEDED, (*p)->soname());
1674 // FIXME: Support --init and --fini.
1675 Symbol* sym = symtab->lookup("_init");
1676 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
1677 odyn->add_symbol(elfcpp::DT_INIT, sym);
1679 sym = symtab->lookup("_fini");
1680 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
1681 odyn->add_symbol(elfcpp::DT_FINI, sym);
1683 // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY.
1685 // Add a DT_RPATH entry if needed.
1686 const General_options::Dir_list& rpath(this->options_.rpath());
1689 std::string rpath_val;
1690 for (General_options::Dir_list::const_iterator p = rpath.begin();
1694 if (rpath_val.empty())
1695 rpath_val = p->name();
1698 // Eliminate duplicates.
1699 General_options::Dir_list::const_iterator q;
1700 for (q = rpath.begin(); q != p; ++q)
1701 if (q->name() == p->name())
1706 rpath_val += p->name();
1711 odyn->add_string(elfcpp::DT_RPATH, rpath_val);
1714 // Look for text segments that have dynamic relocations.
1715 bool have_textrel = false;
1716 for (Segment_list::const_iterator p = this->segment_list_.begin();
1717 p != this->segment_list_.end();
1720 if (((*p)->flags() & elfcpp::PF_W) == 0
1721 && (*p)->dynamic_reloc_count() > 0)
1723 have_textrel = true;
1728 // Add a DT_FLAGS entry. We add it even if no flags are set so that
1729 // post-link tools can easily modify these flags if desired.
1730 unsigned int flags = 0;
1733 // Add a DT_TEXTREL for compatibility with older loaders.
1734 odyn->add_constant(elfcpp::DT_TEXTREL, 0);
1735 flags |= elfcpp::DF_TEXTREL;
1737 if (parameters->output_is_shared() && this->has_static_tls())
1738 flags |= elfcpp::DF_STATIC_TLS;
1739 odyn->add_constant(elfcpp::DT_FLAGS, flags);
1742 // The mapping of .gnu.linkonce section names to real section names.
1744 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
1745 const Layout::Linkonce_mapping Layout::linkonce_mapping[] =
1747 MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d".
1748 MAPPING_INIT("t", ".text"),
1749 MAPPING_INIT("r", ".rodata"),
1750 MAPPING_INIT("d", ".data"),
1751 MAPPING_INIT("b", ".bss"),
1752 MAPPING_INIT("s", ".sdata"),
1753 MAPPING_INIT("sb", ".sbss"),
1754 MAPPING_INIT("s2", ".sdata2"),
1755 MAPPING_INIT("sb2", ".sbss2"),
1756 MAPPING_INIT("wi", ".debug_info"),
1757 MAPPING_INIT("td", ".tdata"),
1758 MAPPING_INIT("tb", ".tbss"),
1759 MAPPING_INIT("lr", ".lrodata"),
1760 MAPPING_INIT("l", ".ldata"),
1761 MAPPING_INIT("lb", ".lbss"),
1765 const int Layout::linkonce_mapping_count =
1766 sizeof(Layout::linkonce_mapping) / sizeof(Layout::linkonce_mapping[0]);
1768 // Return the name of the output section to use for a .gnu.linkonce
1769 // section. This is based on the default ELF linker script of the old
1770 // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
1771 // to ".text". Set *PLEN to the length of the name. *PLEN is
1772 // initialized to the length of NAME.
1775 Layout::linkonce_output_name(const char* name, size_t *plen)
1777 const char* s = name + sizeof(".gnu.linkonce") - 1;
1781 const Linkonce_mapping* plm = linkonce_mapping;
1782 for (int i = 0; i < linkonce_mapping_count; ++i, ++plm)
1784 if (strncmp(s, plm->from, plm->fromlen) == 0 && s[plm->fromlen] == '.')
1793 // Choose the output section name to use given an input section name.
1794 // Set *PLEN to the length of the name. *PLEN is initialized to the
1798 Layout::output_section_name(const char* name, size_t* plen)
1800 if (Layout::is_linkonce(name))
1802 // .gnu.linkonce sections are laid out as though they were named
1803 // for the sections are placed into.
1804 return Layout::linkonce_output_name(name, plen);
1807 // gcc 4.3 generates the following sorts of section names when it
1808 // needs a section name specific to a function:
1814 // .data.rel.local.FN
1816 // .data.rel.ro.local.FN
1823 // The GNU linker maps all of those to the part before the .FN,
1824 // except that .data.rel.local.FN is mapped to .data, and
1825 // .data.rel.ro.local.FN is mapped to .data.rel.ro. The sections
1826 // beginning with .data.rel.ro.local are grouped together.
1828 // For an anonymous namespace, the string FN can contain a '.'.
1830 // Also of interest: .rodata.strN.N, .rodata.cstN, both of which the
1831 // GNU linker maps to .rodata.
1833 // The .data.rel.ro sections enable a security feature triggered by
1834 // the -z relro option. Section which need to be relocated at
1835 // program startup time but which may be readonly after startup are
1836 // grouped into .data.rel.ro. They are then put into a PT_GNU_RELRO
1837 // segment. The dynamic linker will make that segment writable,
1838 // perform relocations, and then make it read-only. FIXME: We do
1839 // not yet implement this optimization.
1841 // It is hard to handle this in a principled way.
1843 // These are the rules we follow:
1845 // If the section name has no initial '.', or no dot other than an
1846 // initial '.', we use the name unchanged (i.e., "mysection" and
1847 // ".text" are unchanged).
1849 // If the name starts with ".data.rel.ro" we use ".data.rel.ro".
1851 // Otherwise, we drop the second '.' and everything that comes after
1852 // it (i.e., ".text.XXX" becomes ".text").
1854 const char* s = name;
1858 const char* sdot = strchr(s, '.');
1862 const char* const data_rel_ro = ".data.rel.ro";
1863 if (strncmp(name, data_rel_ro, strlen(data_rel_ro)) == 0)
1865 *plen = strlen(data_rel_ro);
1869 *plen = sdot - name;
1873 // Record the signature of a comdat section, and return whether to
1874 // include it in the link. If GROUP is true, this is a regular
1875 // section group. If GROUP is false, this is a group signature
1876 // derived from the name of a linkonce section. We want linkonce
1877 // signatures and group signatures to block each other, but we don't
1878 // want a linkonce signature to block another linkonce signature.
1881 Layout::add_comdat(const char* signature, bool group)
1883 std::string sig(signature);
1884 std::pair<Signatures::iterator, bool> ins(
1885 this->signatures_.insert(std::make_pair(sig, group)));
1889 // This is the first time we've seen this signature.
1893 if (ins.first->second)
1895 // We've already seen a real section group with this signature.
1900 // This is a real section group, and we've already seen a
1901 // linkonce section with this signature. Record that we've seen
1902 // a section group, and don't include this section group.
1903 ins.first->second = true;
1908 // We've already seen a linkonce section and this is a linkonce
1909 // section. These don't block each other--this may be the same
1910 // symbol name with different section types.
1915 // Write out the Output_sections. Most won't have anything to write,
1916 // since most of the data will come from input sections which are
1917 // handled elsewhere. But some Output_sections do have Output_data.
1920 Layout::write_output_sections(Output_file* of) const
1922 for (Section_list::const_iterator p = this->section_list_.begin();
1923 p != this->section_list_.end();
1926 if (!(*p)->after_input_sections())
1931 // Write out data not associated with a section or the symbol table.
1934 Layout::write_data(const Symbol_table* symtab, Output_file* of) const
1936 if (!parameters->strip_all())
1938 const Output_section* symtab_section = this->symtab_section_;
1939 for (Section_list::const_iterator p = this->section_list_.begin();
1940 p != this->section_list_.end();
1943 if ((*p)->needs_symtab_index())
1945 gold_assert(symtab_section != NULL);
1946 unsigned int index = (*p)->symtab_index();
1947 gold_assert(index > 0 && index != -1U);
1948 off_t off = (symtab_section->offset()
1949 + index * symtab_section->entsize());
1950 symtab->write_section_symbol(*p, of, off);
1955 const Output_section* dynsym_section = this->dynsym_section_;
1956 for (Section_list::const_iterator p = this->section_list_.begin();
1957 p != this->section_list_.end();
1960 if ((*p)->needs_dynsym_index())
1962 gold_assert(dynsym_section != NULL);
1963 unsigned int index = (*p)->dynsym_index();
1964 gold_assert(index > 0 && index != -1U);
1965 off_t off = (dynsym_section->offset()
1966 + index * dynsym_section->entsize());
1967 symtab->write_section_symbol(*p, of, off);
1971 // Write out the Output_data which are not in an Output_section.
1972 for (Data_list::const_iterator p = this->special_output_list_.begin();
1973 p != this->special_output_list_.end();
1978 // Write out the Output_sections which can only be written after the
1979 // input sections are complete.
1982 Layout::write_sections_after_input_sections(Output_file* of)
1984 // Determine the final section offsets, and thus the final output
1985 // file size. Note we finalize the .shstrab last, to allow the
1986 // after_input_section sections to modify their section-names before
1988 off_t off = this->output_file_size_;
1989 off = this->set_section_offsets(off, AFTER_INPUT_SECTIONS_PASS);
1991 // Now that we've finalized the names, we can finalize the shstrab.
1992 off = this->set_section_offsets(off, STRTAB_AFTER_INPUT_SECTIONS_PASS);
1994 if (off > this->output_file_size_)
1997 this->output_file_size_ = off;
2000 for (Section_list::const_iterator p = this->section_list_.begin();
2001 p != this->section_list_.end();
2004 if ((*p)->after_input_sections())
2008 for (Section_list::const_iterator p = this->unattached_section_list_.begin();
2009 p != this->unattached_section_list_.end();
2012 if ((*p)->after_input_sections())
2016 this->section_headers_->write(of);
2019 // Print statistical information to stderr. This is used for --stats.
2022 Layout::print_stats() const
2024 this->namepool_.print_stats("section name pool");
2025 this->sympool_.print_stats("output symbol name pool");
2026 this->dynpool_.print_stats("dynamic name pool");
2029 // Write_sections_task methods.
2031 // We can always run this task.
2033 Task::Is_runnable_type
2034 Write_sections_task::is_runnable(Workqueue*)
2039 // We need to unlock both OUTPUT_SECTIONS_BLOCKER and FINAL_BLOCKER
2042 class Write_sections_task::Write_sections_locker : public Task_locker
2045 Write_sections_locker(Task_token& output_sections_blocker,
2046 Task_token& final_blocker,
2047 Workqueue* workqueue)
2048 : output_sections_block_(output_sections_blocker, workqueue),
2049 final_block_(final_blocker, workqueue)
2053 Task_block_token output_sections_block_;
2054 Task_block_token final_block_;
2058 Write_sections_task::locks(Workqueue* workqueue)
2060 return new Write_sections_locker(*this->output_sections_blocker_,
2061 *this->final_blocker_,
2065 // Run the task--write out the data.
2068 Write_sections_task::run(Workqueue*)
2070 this->layout_->write_output_sections(this->of_);
2073 // Write_data_task methods.
2075 // We can always run this task.
2077 Task::Is_runnable_type
2078 Write_data_task::is_runnable(Workqueue*)
2083 // We need to unlock FINAL_BLOCKER when finished.
2086 Write_data_task::locks(Workqueue* workqueue)
2088 return new Task_locker_block(*this->final_blocker_, workqueue);
2091 // Run the task--write out the data.
2094 Write_data_task::run(Workqueue*)
2096 this->layout_->write_data(this->symtab_, this->of_);
2099 // Write_symbols_task methods.
2101 // We can always run this task.
2103 Task::Is_runnable_type
2104 Write_symbols_task::is_runnable(Workqueue*)
2109 // We need to unlock FINAL_BLOCKER when finished.
2112 Write_symbols_task::locks(Workqueue* workqueue)
2114 return new Task_locker_block(*this->final_blocker_, workqueue);
2117 // Run the task--write out the symbols.
2120 Write_symbols_task::run(Workqueue*)
2122 this->symtab_->write_globals(this->input_objects_, this->sympool_,
2123 this->dynpool_, this->of_);
2126 // Write_after_input_sections_task methods.
2128 // We can only run this task after the input sections have completed.
2130 Task::Is_runnable_type
2131 Write_after_input_sections_task::is_runnable(Workqueue*)
2133 if (this->input_sections_blocker_->is_blocked())
2138 // We need to unlock FINAL_BLOCKER when finished.
2141 Write_after_input_sections_task::locks(Workqueue* workqueue)
2143 return new Task_locker_block(*this->final_blocker_, workqueue);
2149 Write_after_input_sections_task::run(Workqueue*)
2151 this->layout_->write_sections_after_input_sections(this->of_);
2154 // Close_task_runner methods.
2156 // Run the task--close the file.
2159 Close_task_runner::run(Workqueue*)
2164 // Instantiate the templates we need. We could use the configure
2165 // script to restrict this to only the ones for implemented targets.
2167 #ifdef HAVE_TARGET_32_LITTLE
2170 Layout::layout<32, false>(Sized_relobj<32, false>* object, unsigned int shndx,
2172 const elfcpp::Shdr<32, false>& shdr,
2173 unsigned int, unsigned int, off_t*);
2176 #ifdef HAVE_TARGET_32_BIG
2179 Layout::layout<32, true>(Sized_relobj<32, true>* object, unsigned int shndx,
2181 const elfcpp::Shdr<32, true>& shdr,
2182 unsigned int, unsigned int, off_t*);
2185 #ifdef HAVE_TARGET_64_LITTLE
2188 Layout::layout<64, false>(Sized_relobj<64, false>* object, unsigned int shndx,
2190 const elfcpp::Shdr<64, false>& shdr,
2191 unsigned int, unsigned int, off_t*);
2194 #ifdef HAVE_TARGET_64_BIG
2197 Layout::layout<64, true>(Sized_relobj<64, true>* object, unsigned int shndx,
2199 const elfcpp::Shdr<64, true>& shdr,
2200 unsigned int, unsigned int, off_t*);
2203 #ifdef HAVE_TARGET_32_LITTLE
2206 Layout::layout_eh_frame<32, false>(Sized_relobj<32, false>* object,
2207 const unsigned char* symbols,
2209 const unsigned char* symbol_names,
2210 off_t symbol_names_size,
2212 const elfcpp::Shdr<32, false>& shdr,
2213 unsigned int reloc_shndx,
2214 unsigned int reloc_type,
2218 #ifdef HAVE_TARGET_32_BIG
2221 Layout::layout_eh_frame<32, true>(Sized_relobj<32, true>* object,
2222 const unsigned char* symbols,
2224 const unsigned char* symbol_names,
2225 off_t symbol_names_size,
2227 const elfcpp::Shdr<32, true>& shdr,
2228 unsigned int reloc_shndx,
2229 unsigned int reloc_type,
2233 #ifdef HAVE_TARGET_64_LITTLE
2236 Layout::layout_eh_frame<64, false>(Sized_relobj<64, false>* object,
2237 const unsigned char* symbols,
2239 const unsigned char* symbol_names,
2240 off_t symbol_names_size,
2242 const elfcpp::Shdr<64, false>& shdr,
2243 unsigned int reloc_shndx,
2244 unsigned int reloc_type,
2248 #ifdef HAVE_TARGET_64_BIG
2251 Layout::layout_eh_frame<64, true>(Sized_relobj<64, true>* object,
2252 const unsigned char* symbols,
2254 const unsigned char* symbol_names,
2255 off_t symbol_names_size,
2257 const elfcpp::Shdr<64, true>& shdr,
2258 unsigned int reloc_shndx,
2259 unsigned int reloc_type,
2263 } // End namespace gold.