1 // layout.h -- lay out output file sections for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009 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.
33 #include "workqueue.h"
36 #include "stringpool.h"
41 class General_options;
42 class Incremental_inputs;
46 class Output_section_data;
48 class Output_section_headers;
51 class Output_data_dynamic;
52 class Output_symtab_xindex;
53 class Output_reduced_debug_abbrev_section;
54 class Output_reduced_debug_info_section;
58 // This task function handles mapping the input sections to output
59 // sections and laying them out in memory.
61 class Layout_task_runner : public Task_function_runner
64 // OPTIONS is the command line options, INPUT_OBJECTS is the list of
65 // input objects, SYMTAB is the symbol table, LAYOUT is the layout
67 Layout_task_runner(const General_options& options,
68 const Input_objects* input_objects,
73 : options_(options), input_objects_(input_objects), symtab_(symtab),
74 target_(target), layout_(layout), mapfile_(mapfile)
79 run(Workqueue*, const Task*);
82 Layout_task_runner(const Layout_task_runner&);
83 Layout_task_runner& operator=(const Layout_task_runner&);
85 const General_options& options_;
86 const Input_objects* input_objects_;
87 Symbol_table* symtab_;
93 // This struct holds information about the comdat or .gnu.linkonce
99 : object(NULL), shndx(0), is_group(false), group_sections(NULL)
101 Kept_section(Relobj* a_object, unsigned int a_shndx, bool a_is_group)
102 : object(a_object), shndx(a_shndx), is_group(a_is_group),
106 typedef Unordered_map<std::string, unsigned int> Comdat_group;
108 // The object containing the comdat or .gnu.linkonce.
110 // Index to the group section for comdats and the section itself for
113 // The Kept_sections are values of a mapping, that maps names to
114 // them. This field is true if this struct is associated with the
115 // name of a comdat or .gnu.linkonce, false if it is associated with
116 // the name of a symbol obtained from the .gnu.linkonce.* name
117 // through some heuristics.
119 // For comdats, a map from names of the sections in the group to
120 // indexes in OBJECT_. NULL for .gnu.linkonce.
121 Comdat_group* group_sections;
124 // This class handles the details of laying out input sections.
129 Layout(int number_of_input_files, Script_options*);
131 // Given an input section SHNDX, named NAME, with data in SHDR, from
132 // the object file OBJECT, return the output section where this
133 // input section should go. RELOC_SHNDX is the index of a
134 // relocation section which applies to this section, or 0 if none,
135 // or -1U if more than one. RELOC_TYPE is the type of the
136 // relocation section if there is one. Set *OFFSET to the offset
137 // within the output section.
138 template<int size, bool big_endian>
140 layout(Sized_relobj<size, big_endian> *object, unsigned int shndx,
141 const char* name, const elfcpp::Shdr<size, big_endian>& shdr,
142 unsigned int reloc_shndx, unsigned int reloc_type, off_t* offset);
144 // Layout an input reloc section when doing a relocatable link. The
145 // section is RELOC_SHNDX in OBJECT, with data in SHDR.
146 // DATA_SECTION is the reloc section to which it refers. RR is the
147 // relocatable information.
148 template<int size, bool big_endian>
150 layout_reloc(Sized_relobj<size, big_endian>* object,
151 unsigned int reloc_shndx,
152 const elfcpp::Shdr<size, big_endian>& shdr,
153 Output_section* data_section,
154 Relocatable_relocs* rr);
156 // Layout a group section when doing a relocatable link.
157 template<int size, bool big_endian>
159 layout_group(Symbol_table* symtab,
160 Sized_relobj<size, big_endian>* object,
161 unsigned int group_shndx,
162 const char* group_section_name,
163 const char* signature,
164 const elfcpp::Shdr<size, big_endian>& shdr,
165 elfcpp::Elf_Word flags,
166 std::vector<unsigned int>* shndxes);
168 // Like layout, only for exception frame sections. OBJECT is an
169 // object file. SYMBOLS is the contents of the symbol table
170 // section, with size SYMBOLS_SIZE. SYMBOL_NAMES is the contents of
171 // the symbol name section, with size SYMBOL_NAMES_SIZE. SHNDX is a
172 // .eh_frame section in OBJECT. SHDR is the section header.
173 // RELOC_SHNDX is the index of a relocation section which applies to
174 // this section, or 0 if none, or -1U if more than one. RELOC_TYPE
175 // is the type of the relocation section if there is one. This
176 // returns the output section, and sets *OFFSET to the offset.
177 template<int size, bool big_endian>
179 layout_eh_frame(Sized_relobj<size, big_endian>* object,
180 const unsigned char* symbols,
182 const unsigned char* symbol_names,
183 off_t symbol_names_size,
185 const elfcpp::Shdr<size, big_endian>& shdr,
186 unsigned int reloc_shndx, unsigned int reloc_type,
189 // Handle a GNU stack note. This is called once per input object
190 // file. SEEN_GNU_STACK is true if the object file has a
191 // .note.GNU-stack section. GNU_STACK_FLAGS is the section flags
192 // from that section if there was one.
194 layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags);
196 // Add an Output_section_data to the layout. This is used for
197 // special sections like the GOT section.
199 add_output_section_data(const char* name, elfcpp::Elf_Word type,
200 elfcpp::Elf_Xword flags,
201 Output_section_data*);
203 // Create dynamic sections if necessary.
205 create_initial_dynamic_sections(Symbol_table*);
207 // Define __start and __stop symbols for output sections.
209 define_section_symbols(Symbol_table*);
211 // Create sections for linker scripts.
213 create_script_sections()
214 { this->script_options_->create_script_sections(this); }
216 // Define symbols from any linker script.
218 define_script_symbols(Symbol_table* symtab)
219 { this->script_options_->add_symbols_to_table(symtab); }
221 // Define symbols for group signatures.
223 define_group_signatures(Symbol_table*);
225 // Return the Stringpool used for symbol names.
228 { return &this->sympool_; }
230 // Return the Stringpool used for dynamic symbol names and dynamic
234 { return &this->dynpool_; }
236 // Return the symtab_xindex section used to hold large section
237 // indexes for the normal symbol table.
238 Output_symtab_xindex*
239 symtab_xindex() const
240 { return this->symtab_xindex_; }
242 // Return the dynsym_xindex section used to hold large section
243 // indexes for the dynamic symbol table.
244 Output_symtab_xindex*
245 dynsym_xindex() const
246 { return this->dynsym_xindex_; }
248 // Return whether a section is a .gnu.linkonce section, given the
251 is_linkonce(const char* name)
252 { return strncmp(name, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
254 // Return true if a section is a debugging section.
256 is_debug_info_section(const char* name)
258 // Debugging sections can only be recognized by name.
259 return (strncmp(name, ".debug", sizeof(".debug") - 1) == 0
260 || strncmp(name, ".gnu.linkonce.wi.",
261 sizeof(".gnu.linkonce.wi.") - 1) == 0
262 || strncmp(name, ".line", sizeof(".line") - 1) == 0
263 || strncmp(name, ".stab", sizeof(".stab") - 1) == 0);
266 // Check if a comdat group or .gnu.linkonce section with the given
267 // NAME is selected for the link. If there is already a section,
268 // *KEPT_SECTION is set to point to the signature and the function
269 // returns false. Otherwise, the CANDIDATE signature is recorded
270 // for this NAME in the layout object, *KEPT_SECTION is set to the
271 // internal copy and the function return false. In some cases, with
272 // CANDIDATE->GROUP_ being false, KEPT_SECTION can point back to
275 find_or_add_kept_section(const std::string& name,
276 Kept_section* candidate,
277 Kept_section** kept_section);
279 // Find the given comdat signature, and return the object and section
280 // index of the kept group.
282 find_kept_object(const std::string&, unsigned int*) const;
284 // Finalize the layout after all the input sections have been added.
286 finalize(const Input_objects*, Symbol_table*, Target*, const Task*);
288 // Return whether any sections require postprocessing.
290 any_postprocessing_sections() const
291 { return this->any_postprocessing_sections_; }
293 // Return the size of the output file.
295 output_file_size() const
296 { return this->output_file_size_; }
298 // Return the TLS segment. This will return NULL if there isn't
302 { return this->tls_segment_; }
304 // Return the normal symbol table.
306 symtab_section() const
308 gold_assert(this->symtab_section_ != NULL);
309 return this->symtab_section_;
312 // Return the dynamic symbol table.
314 dynsym_section() const
316 gold_assert(this->dynsym_section_ != NULL);
317 return this->dynsym_section_;
320 // Return the dynamic tags.
323 { return this->dynamic_data_; }
325 // Write out the output sections.
327 write_output_sections(Output_file* of) const;
329 // Write out data not associated with an input file or the symbol
332 write_data(const Symbol_table*, Output_file*) const;
334 // Write out output sections which can not be written until all the
335 // input sections are complete.
337 write_sections_after_input_sections(Output_file* of);
339 // Return an output section named NAME, or NULL if there is none.
341 find_output_section(const char* name) const;
343 // Return an output segment of type TYPE, with segment flags SET set
344 // and segment flags CLEAR clear. Return NULL if there is none.
346 find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
347 elfcpp::Elf_Word clear) const;
349 // Return the number of segments we expect to produce.
351 expected_segment_count() const;
353 // Set a flag to indicate that an object file uses the static TLS model.
356 { this->has_static_tls_ = true; }
358 // Return true if any object file uses the static TLS model.
360 has_static_tls() const
361 { return this->has_static_tls_; }
363 // Return the options which may be set by a linker script.
366 { return this->script_options_; }
368 const Script_options*
369 script_options() const
370 { return this->script_options_; }
372 // Return the object managing inputs in incremental build. NULL in
373 // non-incremental builds.
376 { return this->incremental_inputs_; }
378 // Compute and write out the build ID if needed.
380 write_build_id(Output_file*) const;
382 // Rewrite output file in binary format.
384 write_binary(Output_file* in) const;
386 // Print output sections to the map file.
388 print_to_mapfile(Mapfile*) const;
390 // Dump statistical information to stderr.
394 // A list of segments.
396 typedef std::vector<Output_segment*> Segment_list;
398 // A list of sections.
400 typedef std::vector<Output_section*> Section_list;
402 // The list of information to write out which is not attached to
403 // either a section or a segment.
404 typedef std::vector<Output_data*> Data_list;
406 // Store the allocated sections into the section list. This is used
407 // by the linker script code.
409 get_allocated_sections(Section_list*) const;
411 // Make a section for a linker script to hold data.
413 make_output_section_for_script(const char* name);
415 // Make a segment. This is used by the linker script code.
417 make_output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags);
419 // Return the number of segments.
421 segment_count() const
422 { return this->segment_list_.size(); }
424 // Map from section flags to segment flags.
425 static elfcpp::Elf_Word
426 section_flags_to_segment(elfcpp::Elf_Xword flags);
428 // Attach sections to segments.
430 attach_sections_to_segments();
433 Layout(const Layout&);
434 Layout& operator=(const Layout&);
436 // Mapping from .gnu.linkonce section names to output section names.
437 struct Linkonce_mapping
444 static const Linkonce_mapping linkonce_mapping[];
445 static const int linkonce_mapping_count;
447 // During a relocatable link, a list of group sections and
449 struct Group_signature
451 // The group section.
452 Output_section* section;
454 const char* signature;
457 : section(NULL), signature(NULL)
460 Group_signature(Output_section* sectiona, const char* signaturea)
461 : section(sectiona), signature(signaturea)
464 typedef std::vector<Group_signature> Group_signatures;
466 // Create a note section, filling in the header.
468 create_note(const char* name, int note_type, const char *section_name,
469 size_t descsz, bool allocate, size_t* trailing_padding);
471 // Create a note section for gold version.
475 // Record whether the stack must be executable.
477 create_executable_stack_info(const Target*);
479 // Create a build ID note if needed.
483 // Create .gnu_incremental_inputs and .gnu_incremental_strtab sections needed
484 // for the next run of incremental linking to check what has changed.
486 create_incremental_info_sections();
488 // Find the first read-only PT_LOAD segment, creating one if
491 find_first_load_seg();
493 // Count the local symbols in the regular symbol table and the dynamic
494 // symbol table, and build the respective string pools.
496 count_local_symbols(const Task*, const Input_objects*);
498 // Create the output sections for the symbol table.
500 create_symtab_sections(const Input_objects*, Symbol_table*,
501 unsigned int, off_t*);
503 // Create the .shstrtab section.
507 // Create the section header table.
509 create_shdrs(const Output_section* shstrtab_section, off_t*);
511 // Create the dynamic symbol table.
513 create_dynamic_symtab(const Input_objects*, Symbol_table*,
514 Output_section** pdynstr,
515 unsigned int* plocal_dynamic_count,
516 std::vector<Symbol*>* pdynamic_symbols,
519 // Assign offsets to each local portion of the dynamic symbol table.
521 assign_local_dynsym_offsets(const Input_objects*);
523 // Finish the .dynamic section and PT_DYNAMIC segment.
525 finish_dynamic_section(const Input_objects*, const Symbol_table*);
527 // Create the .interp section and PT_INTERP segment.
529 create_interp(const Target* target);
531 // Create the version sections.
533 create_version_sections(const Versions*,
535 unsigned int local_symcount,
536 const std::vector<Symbol*>& dynamic_symbols,
537 const Output_section* dynstr);
539 template<int size, bool big_endian>
541 sized_create_version_sections(const Versions* versions,
543 unsigned int local_symcount,
544 const std::vector<Symbol*>& dynamic_symbols,
545 const Output_section* dynstr);
547 // Return whether to include this section in the link.
548 template<int size, bool big_endian>
550 include_section(Sized_relobj<size, big_endian>* object, const char* name,
551 const elfcpp::Shdr<size, big_endian>&);
553 // Return the output section name to use given an input section
554 // name. Set *PLEN to the length of the name. *PLEN must be
555 // initialized to the length of NAME.
557 output_section_name(const char* name, size_t* plen);
559 // Return the output section name to use for a linkonce section
560 // name. PLEN is as for output_section_name.
562 linkonce_output_name(const char* name, size_t* plen);
564 // Return the number of allocated output sections.
566 allocated_output_section_count() const;
568 // Return the output section for NAME, TYPE and FLAGS.
570 get_output_section(const char* name, Stringpool::Key name_key,
571 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags);
573 // Choose the output section for NAME in RELOBJ.
575 choose_output_section(const Relobj* relobj, const char* name,
576 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
577 bool is_input_section);
579 // Create a new Output_section.
581 make_output_section(const char* name, elfcpp::Elf_Word type,
582 elfcpp::Elf_Xword flags);
584 // Attach a section to a segment.
586 attach_section_to_segment(Output_section*);
588 // Attach an allocated section to a segment.
590 attach_allocated_section_to_segment(Output_section*);
592 // Set the final file offsets of all the segments.
594 set_segment_offsets(const Target*, Output_segment*, unsigned int* pshndx);
596 // Set the file offsets of the sections when doing a relocatable
599 set_relocatable_section_offsets(Output_data*, unsigned int* pshndx);
601 // Set the final file offsets of all the sections not associated
602 // with a segment. We set section offsets in three passes: the
603 // first handles all allocated sections, the second sections that
604 // require postprocessing, and the last the late-bound STRTAB
605 // sections (probably only shstrtab, which is the one we care about
606 // because it holds section names).
607 enum Section_offset_pass
609 BEFORE_INPUT_SECTIONS_PASS,
610 POSTPROCESSING_SECTIONS_PASS,
611 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
614 set_section_offsets(off_t, Section_offset_pass pass);
616 // Set the final section indexes of all the sections not associated
617 // with a segment. Returns the next unused index.
619 set_section_indexes(unsigned int pshndx);
621 // Set the section addresses when using a script.
623 set_section_addresses_from_script(Symbol_table*);
625 // Return whether SEG1 comes before SEG2 in the output file.
627 segment_precedes(const Output_segment* seg1, const Output_segment* seg2);
629 // A mapping used for kept comdats/.gnu.linkonce group signatures.
630 typedef Unordered_map<std::string, Kept_section> Signatures;
632 // Mapping from input section name/type/flags to output section. We
633 // use canonicalized strings here.
635 typedef std::pair<Stringpool::Key,
636 std::pair<elfcpp::Elf_Word, elfcpp::Elf_Xword> > Key;
641 operator()(const Key& k) const;
644 typedef Unordered_map<Key, Output_section*, Hash_key> Section_name_map;
646 // A comparison class for segments.
648 struct Compare_segments
651 operator()(const Output_segment* seg1, const Output_segment* seg2)
652 { return Layout::segment_precedes(seg1, seg2); }
655 // The number of input files, for sizing tables.
656 int number_of_input_files_;
657 // Information set by scripts or by command line options.
658 Script_options* script_options_;
659 // The output section names.
660 Stringpool namepool_;
661 // The output symbol names.
663 // The dynamic strings, if needed.
665 // The list of group sections and linkonce sections which we have seen.
666 Signatures signatures_;
667 // The mapping from input section name/type/flags to output sections.
668 Section_name_map section_name_map_;
669 // The list of output segments.
670 Segment_list segment_list_;
671 // The list of output sections.
672 Section_list section_list_;
673 // The list of output sections which are not attached to any output
675 Section_list unattached_section_list_;
676 // The list of unattached Output_data objects which require special
677 // handling because they are not Output_sections.
678 Data_list special_output_list_;
679 // The section headers.
680 Output_section_headers* section_headers_;
681 // A pointer to the PT_TLS segment if there is one.
682 Output_segment* tls_segment_;
683 // A pointer to the PT_GNU_RELRO segment if there is one.
684 Output_segment* relro_segment_;
685 // The SHT_SYMTAB output section.
686 Output_section* symtab_section_;
687 // The SHT_SYMTAB_SHNDX for the regular symbol table if there is one.
688 Output_symtab_xindex* symtab_xindex_;
689 // The SHT_DYNSYM output section if there is one.
690 Output_section* dynsym_section_;
691 // The SHT_SYMTAB_SHNDX for the dynamic symbol table if there is one.
692 Output_symtab_xindex* dynsym_xindex_;
693 // The SHT_DYNAMIC output section if there is one.
694 Output_section* dynamic_section_;
695 // The dynamic data which goes into dynamic_section_.
696 Output_data_dynamic* dynamic_data_;
697 // The exception frame output section if there is one.
698 Output_section* eh_frame_section_;
699 // The exception frame data for eh_frame_section_.
700 Eh_frame* eh_frame_data_;
701 // Whether we have added eh_frame_data_ to the .eh_frame section.
702 bool added_eh_frame_data_;
703 // The exception frame header output section if there is one.
704 Output_section* eh_frame_hdr_section_;
705 // The space for the build ID checksum if there is one.
706 Output_section_data* build_id_note_;
707 // The output section containing dwarf abbreviations
708 Output_reduced_debug_abbrev_section* debug_abbrev_;
709 // The output section containing the dwarf debug info tree
710 Output_reduced_debug_info_section* debug_info_;
711 // A list of group sections and their signatures.
712 Group_signatures group_signatures_;
713 // The size of the output file.
714 off_t output_file_size_;
715 // Whether we have attached the sections to the segments.
716 bool sections_are_attached_;
717 // Whether we have seen an object file marked to require an
719 bool input_requires_executable_stack_;
720 // Whether we have seen at least one object file with an executable
722 bool input_with_gnu_stack_note_;
723 // Whether we have seen at least one object file without an
724 // executable stack marker.
725 bool input_without_gnu_stack_note_;
726 // Whether we have seen an object file that uses the static TLS model.
727 bool has_static_tls_;
728 // Whether any sections require postprocessing.
729 bool any_postprocessing_sections_;
730 // Whether we have resized the signatures_ hash table.
731 bool resized_signatures_;
732 // In incremental build, holds information check the inputs and build the
733 // .gnu_incremental_inputs section.
734 Incremental_inputs* incremental_inputs_;
737 // This task handles writing out data in output sections which is not
738 // part of an input section, or which requires special handling. When
739 // this is done, it unblocks both output_sections_blocker and
742 class Write_sections_task : public Task
745 Write_sections_task(const Layout* layout, Output_file* of,
746 Task_token* output_sections_blocker,
747 Task_token* final_blocker)
748 : layout_(layout), of_(of),
749 output_sections_blocker_(output_sections_blocker),
750 final_blocker_(final_blocker)
753 // The standard Task methods.
766 { return "Write_sections_task"; }
769 class Write_sections_locker;
771 const Layout* layout_;
773 Task_token* output_sections_blocker_;
774 Task_token* final_blocker_;
777 // This task handles writing out data which is not part of a section
780 class Write_data_task : public Task
783 Write_data_task(const Layout* layout, const Symbol_table* symtab,
784 Output_file* of, Task_token* final_blocker)
785 : layout_(layout), symtab_(symtab), of_(of), final_blocker_(final_blocker)
788 // The standard Task methods.
801 { return "Write_data_task"; }
804 const Layout* layout_;
805 const Symbol_table* symtab_;
807 Task_token* final_blocker_;
810 // This task handles writing out the global symbols.
812 class Write_symbols_task : public Task
815 Write_symbols_task(const Layout* layout, const Symbol_table* symtab,
816 const Input_objects* input_objects,
817 const Stringpool* sympool, const Stringpool* dynpool,
818 Output_file* of, Task_token* final_blocker)
819 : layout_(layout), symtab_(symtab), input_objects_(input_objects),
820 sympool_(sympool), dynpool_(dynpool), of_(of),
821 final_blocker_(final_blocker)
824 // The standard Task methods.
837 { return "Write_symbols_task"; }
840 const Layout* layout_;
841 const Symbol_table* symtab_;
842 const Input_objects* input_objects_;
843 const Stringpool* sympool_;
844 const Stringpool* dynpool_;
846 Task_token* final_blocker_;
849 // This task handles writing out data in output sections which can't
850 // be written out until all the input sections have been handled.
851 // This is for sections whose contents is based on the contents of
852 // other output sections.
854 class Write_after_input_sections_task : public Task
857 Write_after_input_sections_task(Layout* layout, Output_file* of,
858 Task_token* input_sections_blocker,
859 Task_token* final_blocker)
860 : layout_(layout), of_(of),
861 input_sections_blocker_(input_sections_blocker),
862 final_blocker_(final_blocker)
865 // The standard Task methods.
878 { return "Write_after_input_sections_task"; }
883 Task_token* input_sections_blocker_;
884 Task_token* final_blocker_;
887 // This task function handles closing the file.
889 class Close_task_runner : public Task_function_runner
892 Close_task_runner(const General_options* options, const Layout* layout,
894 : options_(options), layout_(layout), of_(of)
897 // Run the operation.
899 run(Workqueue*, const Task*);
902 const General_options* options_;
903 const Layout* layout_;
907 // A small helper function to align an address.
910 align_address(uint64_t address, uint64_t addralign)
913 address = (address + addralign - 1) &~ (addralign - 1);
917 } // End namespace gold.
919 #endif // !defined(GOLD_LAYOUT_H)