1 // layout.h -- lay out output file sections for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
34 #include "workqueue.h"
37 #include "stringpool.h"
42 class General_options;
43 class Incremental_inputs;
47 class Output_section_data;
49 class Output_section_headers;
50 class Output_segment_headers;
51 class Output_file_header;
54 class Output_data_reloc_generic;
55 class Output_data_dynamic;
56 class Output_symtab_xindex;
57 class Output_reduced_debug_abbrev_section;
58 class Output_reduced_debug_info_section;
63 // Return TRUE if SECNAME is the name of a compressed debug section.
65 is_compressed_debug_section(const char* secname);
67 // This task function handles mapping the input sections to output
68 // sections and laying them out in memory.
70 class Layout_task_runner : public Task_function_runner
73 // OPTIONS is the command line options, INPUT_OBJECTS is the list of
74 // input objects, SYMTAB is the symbol table, LAYOUT is the layout
76 Layout_task_runner(const General_options& options,
77 const Input_objects* input_objects,
82 : options_(options), input_objects_(input_objects), symtab_(symtab),
83 target_(target), layout_(layout), mapfile_(mapfile)
88 run(Workqueue*, const Task*);
91 Layout_task_runner(const Layout_task_runner&);
92 Layout_task_runner& operator=(const Layout_task_runner&);
94 const General_options& options_;
95 const Input_objects* input_objects_;
96 Symbol_table* symtab_;
102 // This class holds information about the comdat group or
103 // .gnu.linkonce section that will be kept for a given signature.
108 // For a comdat group, we build a mapping from the name of each
109 // section in the group to the section index and the size in object.
110 // When we discard a group in some other object file, we use this
111 // map to figure out which kept section the discarded section is
112 // associated with. We then use that mapping when processing relocs
113 // against discarded sections.
114 struct Comdat_section_info
116 // The section index.
121 Comdat_section_info(unsigned int a_shndx, uint64_t a_size)
122 : shndx(a_shndx), size(a_size)
126 // Most comdat groups have only one or two sections, so we use a
127 // std::map rather than an Unordered_map to optimize for that case
128 // without paying too heavily for groups with more sections.
129 typedef std::map<std::string, Comdat_section_info> Comdat_group;
133 : object_(NULL), shndx_(0), is_comdat_(false), is_group_name_(false)
134 { this->u_.linkonce_size = 0; }
136 // We need to support copies for the signature map in the Layout
137 // object, but we should never copy an object after it has been
138 // marked as a comdat section.
139 Kept_section(const Kept_section& k)
140 : object_(k.object_), shndx_(k.shndx_), is_comdat_(false),
141 is_group_name_(k.is_group_name_)
143 gold_assert(!k.is_comdat_);
144 this->u_.linkonce_size = 0;
149 if (this->is_comdat_)
150 delete this->u_.group_sections;
153 // The object where this section lives.
156 { return this->object_; }
160 set_object(Relobj* object)
162 gold_assert(this->object_ == NULL);
163 this->object_ = object;
166 // The section index.
169 { return this->shndx_; }
171 // Set the section index.
173 set_shndx(unsigned int shndx)
175 gold_assert(this->shndx_ == 0);
176 this->shndx_ = shndx;
179 // Whether this is a comdat group.
182 { return this->is_comdat_; }
184 // Set that this is a comdat group.
188 gold_assert(!this->is_comdat_);
189 this->is_comdat_ = true;
190 this->u_.group_sections = new Comdat_group();
193 // Whether this is associated with the name of a group or section
194 // rather than the symbol name derived from a linkonce section.
196 is_group_name() const
197 { return this->is_group_name_; }
199 // Note that this represents a comdat group rather than a single
203 { this->is_group_name_ = true; }
205 // Add a section to the group list.
207 add_comdat_section(const std::string& name, unsigned int shndx,
210 gold_assert(this->is_comdat_);
211 Comdat_section_info sinfo(shndx, size);
212 this->u_.group_sections->insert(std::make_pair(name, sinfo));
215 // Look for a section name in the group list, and return whether it
216 // was found. If found, returns the section index and size.
218 find_comdat_section(const std::string& name, unsigned int *pshndx,
219 uint64_t *psize) const
221 gold_assert(this->is_comdat_);
222 Comdat_group::const_iterator p = this->u_.group_sections->find(name);
223 if (p == this->u_.group_sections->end())
225 *pshndx = p->second.shndx;
226 *psize = p->second.size;
230 // If there is only one section in the group list, return true, and
231 // return the section index and size.
233 find_single_comdat_section(unsigned int *pshndx, uint64_t *psize) const
235 gold_assert(this->is_comdat_);
236 if (this->u_.group_sections->size() != 1)
238 Comdat_group::const_iterator p = this->u_.group_sections->begin();
239 *pshndx = p->second.shndx;
240 *psize = p->second.size;
244 // Return the size of a linkonce section.
246 linkonce_size() const
248 gold_assert(!this->is_comdat_);
249 return this->u_.linkonce_size;
252 // Set the size of a linkonce section.
254 set_linkonce_size(uint64_t size)
256 gold_assert(!this->is_comdat_);
257 this->u_.linkonce_size = size;
262 Kept_section& operator=(const Kept_section&);
264 // The object containing the comdat group or .gnu.linkonce section.
266 // Index of the group section for comdats and the section itself for
269 // True if this is for a comdat group rather than a .gnu.linkonce
272 // The Kept_sections are values of a mapping, that maps names to
273 // them. This field is true if this struct is associated with the
274 // name of a comdat or .gnu.linkonce, false if it is associated with
275 // the name of a symbol obtained from the .gnu.linkonce.* name
276 // through some heuristics.
280 // If the is_comdat_ field is true, this holds a map from names of
281 // the sections in the group to section indexes in object_ and to
283 Comdat_group* group_sections;
284 // If the is_comdat_ field is false, this holds the size of the
286 uint64_t linkonce_size;
290 // The ordering for output sections. This controls how output
291 // sections are ordered within a PT_LOAD output segment.
293 enum Output_section_order
295 // Unspecified. Used for non-load segments. Also used for the file
296 // and segment headers.
299 // The PT_INTERP section should come first, so that the dynamic
300 // linker can pick it up quickly.
303 // Loadable read-only note sections come next so that the PT_NOTE
304 // segment is on the first page of the executable.
307 // Put read-only sections used by the dynamic linker early in the
308 // executable to minimize paging.
309 ORDER_DYNAMIC_LINKER,
311 // Put reloc sections used by the dynamic linker after other
312 // sections used by the dynamic linker; otherwise, objcopy and strip
314 ORDER_DYNAMIC_RELOCS,
316 // Put the PLT reloc section after the other dynamic relocs;
317 // otherwise, prelink gets confused.
318 ORDER_DYNAMIC_PLT_RELOCS,
320 // The .init section.
326 // The regular text sections.
329 // The .fini section.
332 // The read-only sections.
335 // The exception frame sections.
338 // The TLS sections come first in the data section.
342 // Local RELRO (read-only after relocation) sections come before
343 // non-local RELRO sections. This data will be fully resolved by
347 // Non-local RELRO sections are grouped together after local RELRO
348 // sections. All RELRO sections must be adjacent so that they can
349 // all be put into a PT_GNU_RELRO segment.
352 // We permit marking exactly one output section as the last RELRO
353 // section. We do this so that the read-only GOT can be adjacent to
357 // Similarly, we permit marking exactly one output section as the
358 // first non-RELRO section.
359 ORDER_NON_RELRO_FIRST,
361 // The regular data sections come after the RELRO sections.
364 // Large data sections normally go in large data segments.
367 // Group writable notes so that we can have a single PT_NOTE
371 // The small data sections must be at the end of the data sections,
372 // so that they can be adjacent to the small BSS sections.
375 // The BSS sections start here.
377 // The small BSS sections must be at the start of the BSS sections,
378 // so that they can be adjacent to the small data sections.
381 // The regular BSS sections.
384 // The large BSS sections come after the other BSS sections.
391 // This class handles the details of laying out input sections.
396 Layout(int number_of_input_files, Script_options*);
400 delete this->relaxation_debug_check_;
401 delete this->segment_states_;
404 // Given an input section SHNDX, named NAME, with data in SHDR, from
405 // the object file OBJECT, return the output section where this
406 // input section should go. RELOC_SHNDX is the index of a
407 // relocation section which applies to this section, or 0 if none,
408 // or -1U if more than one. RELOC_TYPE is the type of the
409 // relocation section if there is one. Set *OFFSET to the offset
410 // within the output section.
411 template<int size, bool big_endian>
413 layout(Sized_relobj<size, big_endian> *object, unsigned int shndx,
414 const char* name, const elfcpp::Shdr<size, big_endian>& shdr,
415 unsigned int reloc_shndx, unsigned int reloc_type, off_t* offset);
418 find_section_order_index(const std::string&);
421 read_layout_from_file();
423 // Layout an input reloc section when doing a relocatable link. The
424 // section is RELOC_SHNDX in OBJECT, with data in SHDR.
425 // DATA_SECTION is the reloc section to which it refers. RR is the
426 // relocatable information.
427 template<int size, bool big_endian>
429 layout_reloc(Sized_relobj<size, big_endian>* object,
430 unsigned int reloc_shndx,
431 const elfcpp::Shdr<size, big_endian>& shdr,
432 Output_section* data_section,
433 Relocatable_relocs* rr);
435 // Layout a group section when doing a relocatable link.
436 template<int size, bool big_endian>
438 layout_group(Symbol_table* symtab,
439 Sized_relobj<size, big_endian>* object,
440 unsigned int group_shndx,
441 const char* group_section_name,
442 const char* signature,
443 const elfcpp::Shdr<size, big_endian>& shdr,
444 elfcpp::Elf_Word flags,
445 std::vector<unsigned int>* shndxes);
447 // Like layout, only for exception frame sections. OBJECT is an
448 // object file. SYMBOLS is the contents of the symbol table
449 // section, with size SYMBOLS_SIZE. SYMBOL_NAMES is the contents of
450 // the symbol name section, with size SYMBOL_NAMES_SIZE. SHNDX is a
451 // .eh_frame section in OBJECT. SHDR is the section header.
452 // RELOC_SHNDX is the index of a relocation section which applies to
453 // this section, or 0 if none, or -1U if more than one. RELOC_TYPE
454 // is the type of the relocation section if there is one. This
455 // returns the output section, and sets *OFFSET to the offset.
456 template<int size, bool big_endian>
458 layout_eh_frame(Sized_relobj<size, big_endian>* object,
459 const unsigned char* symbols,
461 const unsigned char* symbol_names,
462 off_t symbol_names_size,
464 const elfcpp::Shdr<size, big_endian>& shdr,
465 unsigned int reloc_shndx, unsigned int reloc_type,
468 // Handle a GNU stack note. This is called once per input object
469 // file. SEEN_GNU_STACK is true if the object file has a
470 // .note.GNU-stack section. GNU_STACK_FLAGS is the section flags
471 // from that section if there was one.
473 layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags);
475 // Add an Output_section_data to the layout. This is used for
476 // special sections like the GOT section. ORDER is where the
477 // section should wind up in the output segment. IS_RELRO is true
478 // for relro sections.
480 add_output_section_data(const char* name, elfcpp::Elf_Word type,
481 elfcpp::Elf_Xword flags,
482 Output_section_data*, Output_section_order order,
485 // Increase the size of the relro segment by this much.
487 increase_relro(unsigned int s)
488 { this->increase_relro_ += s; }
490 // Create dynamic sections if necessary.
492 create_initial_dynamic_sections(Symbol_table*);
494 // Define __start and __stop symbols for output sections.
496 define_section_symbols(Symbol_table*);
498 // Create automatic note sections.
502 // Create sections for linker scripts.
504 create_script_sections()
505 { this->script_options_->create_script_sections(this); }
507 // Define symbols from any linker script.
509 define_script_symbols(Symbol_table* symtab)
510 { this->script_options_->add_symbols_to_table(symtab); }
512 // Define symbols for group signatures.
514 define_group_signatures(Symbol_table*);
516 // Return the Stringpool used for symbol names.
519 { return &this->sympool_; }
521 // Return the Stringpool used for dynamic symbol names and dynamic
525 { return &this->dynpool_; }
527 // Return the symtab_xindex section used to hold large section
528 // indexes for the normal symbol table.
529 Output_symtab_xindex*
530 symtab_xindex() const
531 { return this->symtab_xindex_; }
533 // Return the dynsym_xindex section used to hold large section
534 // indexes for the dynamic symbol table.
535 Output_symtab_xindex*
536 dynsym_xindex() const
537 { return this->dynsym_xindex_; }
539 // Return whether a section is a .gnu.linkonce section, given the
542 is_linkonce(const char* name)
543 { return strncmp(name, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
545 // Whether we have added an input section.
547 have_added_input_section() const
548 { return this->have_added_input_section_; }
550 // Return true if a section is a debugging section.
552 is_debug_info_section(const char* name)
554 // Debugging sections can only be recognized by name.
555 return (strncmp(name, ".debug", sizeof(".debug") - 1) == 0
556 || strncmp(name, ".zdebug", sizeof(".zdebug") - 1) == 0
557 || strncmp(name, ".gnu.linkonce.wi.",
558 sizeof(".gnu.linkonce.wi.") - 1) == 0
559 || strncmp(name, ".line", sizeof(".line") - 1) == 0
560 || strncmp(name, ".stab", sizeof(".stab") - 1) == 0);
563 // Check if a comdat group or .gnu.linkonce section with the given
564 // NAME is selected for the link. If there is already a section,
565 // *KEPT_SECTION is set to point to the signature and the function
566 // returns false. Otherwise, OBJECT, SHNDX,IS_COMDAT, and
567 // IS_GROUP_NAME are recorded for this NAME in the layout object,
568 // *KEPT_SECTION is set to the internal copy and the function return
571 find_or_add_kept_section(const std::string& name, Relobj* object,
572 unsigned int shndx, bool is_comdat,
573 bool is_group_name, Kept_section** kept_section);
575 // Finalize the layout after all the input sections have been added.
577 finalize(const Input_objects*, Symbol_table*, Target*, const Task*);
579 // Return whether any sections require postprocessing.
581 any_postprocessing_sections() const
582 { return this->any_postprocessing_sections_; }
584 // Return the size of the output file.
586 output_file_size() const
587 { return this->output_file_size_; }
589 // Return the TLS segment. This will return NULL if there isn't
593 { return this->tls_segment_; }
595 // Return the normal symbol table.
597 symtab_section() const
599 gold_assert(this->symtab_section_ != NULL);
600 return this->symtab_section_;
603 // Return the dynamic symbol table.
605 dynsym_section() const
607 gold_assert(this->dynsym_section_ != NULL);
608 return this->dynsym_section_;
611 // Return the dynamic tags.
614 { return this->dynamic_data_; }
616 // Write out the output sections.
618 write_output_sections(Output_file* of) const;
620 // Write out data not associated with an input file or the symbol
623 write_data(const Symbol_table*, Output_file*) const;
625 // Write out output sections which can not be written until all the
626 // input sections are complete.
628 write_sections_after_input_sections(Output_file* of);
630 // Return an output section named NAME, or NULL if there is none.
632 find_output_section(const char* name) const;
634 // Return an output segment of type TYPE, with segment flags SET set
635 // and segment flags CLEAR clear. Return NULL if there is none.
637 find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
638 elfcpp::Elf_Word clear) const;
640 // Return the number of segments we expect to produce.
642 expected_segment_count() const;
644 // Set a flag to indicate that an object file uses the static TLS model.
647 { this->has_static_tls_ = true; }
649 // Return true if any object file uses the static TLS model.
651 has_static_tls() const
652 { return this->has_static_tls_; }
654 // Return the options which may be set by a linker script.
657 { return this->script_options_; }
659 const Script_options*
660 script_options() const
661 { return this->script_options_; }
663 // Return the object managing inputs in incremental build. NULL in
664 // non-incremental builds.
666 incremental_inputs() const
667 { return this->incremental_inputs_; }
669 // For the target-specific code to add dynamic tags which are common
672 add_target_dynamic_tags(bool use_rel, const Output_data* plt_got,
673 const Output_data* plt_rel,
674 const Output_data_reloc_generic* dyn_rel,
675 bool add_debug, bool dynrel_includes_plt);
677 // Compute and write out the build ID if needed.
679 write_build_id(Output_file*) const;
681 // Rewrite output file in binary format.
683 write_binary(Output_file* in) const;
685 // Print output sections to the map file.
687 print_to_mapfile(Mapfile*) const;
689 // Dump statistical information to stderr.
693 // A list of segments.
695 typedef std::vector<Output_segment*> Segment_list;
697 // A list of sections.
699 typedef std::vector<Output_section*> Section_list;
701 // The list of information to write out which is not attached to
702 // either a section or a segment.
703 typedef std::vector<Output_data*> Data_list;
705 // Store the allocated sections into the section list. This is used
706 // by the linker script code.
708 get_allocated_sections(Section_list*) const;
710 // Make a section for a linker script to hold data.
712 make_output_section_for_script(const char* name,
713 Script_sections::Section_type section_type);
715 // Make a segment. This is used by the linker script code.
717 make_output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags);
719 // Return the number of segments.
721 segment_count() const
722 { return this->segment_list_.size(); }
724 // Map from section flags to segment flags.
725 static elfcpp::Elf_Word
726 section_flags_to_segment(elfcpp::Elf_Xword flags);
728 // Attach sections to segments.
730 attach_sections_to_segments();
732 // For relaxation clean up, we need to know output section data created
733 // from a linker script.
735 new_output_section_data_from_script(Output_section_data* posd)
737 if (this->record_output_section_data_from_script_)
738 this->script_output_section_data_list_.push_back(posd);
741 // Return section list.
744 { return this->section_list_; }
747 Layout(const Layout&);
748 Layout& operator=(const Layout&);
750 // Mapping from input section names to output section names.
751 struct Section_name_mapping
758 static const Section_name_mapping section_name_mapping[];
759 static const int section_name_mapping_count;
761 // During a relocatable link, a list of group sections and
763 struct Group_signature
765 // The group section.
766 Output_section* section;
768 const char* signature;
771 : section(NULL), signature(NULL)
774 Group_signature(Output_section* sectiona, const char* signaturea)
775 : section(sectiona), signature(signaturea)
778 typedef std::vector<Group_signature> Group_signatures;
780 // Create a note section, filling in the header.
782 create_note(const char* name, int note_type, const char *section_name,
783 size_t descsz, bool allocate, size_t* trailing_padding);
785 // Create a note section for gold version.
789 // Record whether the stack must be executable.
791 create_executable_stack_info();
793 // Create a build ID note if needed.
797 // Link .stab and .stabstr sections.
799 link_stabs_sections();
801 // Create .gnu_incremental_inputs and .gnu_incremental_strtab sections needed
802 // for the next run of incremental linking to check what has changed.
804 create_incremental_info_sections(Symbol_table*);
806 // Find the first read-only PT_LOAD segment, creating one if
809 find_first_load_seg();
811 // Count the local symbols in the regular symbol table and the dynamic
812 // symbol table, and build the respective string pools.
814 count_local_symbols(const Task*, const Input_objects*);
816 // Create the output sections for the symbol table.
818 create_symtab_sections(const Input_objects*, Symbol_table*,
819 unsigned int, off_t*);
821 // Create the .shstrtab section.
825 // Create the section header table.
827 create_shdrs(const Output_section* shstrtab_section, off_t*);
829 // Create the dynamic symbol table.
831 create_dynamic_symtab(const Input_objects*, Symbol_table*,
832 Output_section** pdynstr,
833 unsigned int* plocal_dynamic_count,
834 std::vector<Symbol*>* pdynamic_symbols,
837 // Assign offsets to each local portion of the dynamic symbol table.
839 assign_local_dynsym_offsets(const Input_objects*);
841 // Finish the .dynamic section and PT_DYNAMIC segment.
843 finish_dynamic_section(const Input_objects*, const Symbol_table*);
845 // Set the size of the _DYNAMIC symbol.
847 set_dynamic_symbol_size(const Symbol_table*);
849 // Create the .interp section and PT_INTERP segment.
851 create_interp(const Target* target);
853 // Create the version sections.
855 create_version_sections(const Versions*,
857 unsigned int local_symcount,
858 const std::vector<Symbol*>& dynamic_symbols,
859 const Output_section* dynstr);
861 template<int size, bool big_endian>
863 sized_create_version_sections(const Versions* versions,
865 unsigned int local_symcount,
866 const std::vector<Symbol*>& dynamic_symbols,
867 const Output_section* dynstr);
869 // Return whether to include this section in the link.
870 template<int size, bool big_endian>
872 include_section(Sized_relobj<size, big_endian>* object, const char* name,
873 const elfcpp::Shdr<size, big_endian>&);
875 // Return the output section name to use given an input section
876 // name. Set *PLEN to the length of the name. *PLEN must be
877 // initialized to the length of NAME.
879 output_section_name(const char* name, size_t* plen);
881 // Return the number of allocated output sections.
883 allocated_output_section_count() const;
885 // Return the output section for NAME, TYPE and FLAGS.
887 get_output_section(const char* name, Stringpool::Key name_key,
888 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
889 Output_section_order order, bool is_relro);
891 // Choose the output section for NAME in RELOBJ.
893 choose_output_section(const Relobj* relobj, const char* name,
894 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
895 bool is_input_section, Output_section_order order,
898 // Create a new Output_section.
900 make_output_section(const char* name, elfcpp::Elf_Word type,
901 elfcpp::Elf_Xword flags, Output_section_order order,
904 // Attach a section to a segment.
906 attach_section_to_segment(Output_section*);
908 // Get section order.
910 default_section_order(Output_section*, bool is_relro_local);
912 // Attach an allocated section to a segment.
914 attach_allocated_section_to_segment(Output_section*);
916 // Set the final file offsets of all the segments.
918 set_segment_offsets(const Target*, Output_segment*, unsigned int* pshndx);
920 // Set the file offsets of the sections when doing a relocatable
923 set_relocatable_section_offsets(Output_data*, unsigned int* pshndx);
925 // Set the final file offsets of all the sections not associated
926 // with a segment. We set section offsets in three passes: the
927 // first handles all allocated sections, the second sections that
928 // require postprocessing, and the last the late-bound STRTAB
929 // sections (probably only shstrtab, which is the one we care about
930 // because it holds section names).
931 enum Section_offset_pass
933 BEFORE_INPUT_SECTIONS_PASS,
934 POSTPROCESSING_SECTIONS_PASS,
935 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
938 set_section_offsets(off_t, Section_offset_pass pass);
940 // Set the final section indexes of all the sections not associated
941 // with a segment. Returns the next unused index.
943 set_section_indexes(unsigned int pshndx);
945 // Set the section addresses when using a script.
947 set_section_addresses_from_script(Symbol_table*);
949 // Find appropriate places or orphan sections in a script.
951 place_orphan_sections_in_script();
953 // Return whether SEG1 comes before SEG2 in the output file.
955 segment_precedes(const Output_segment* seg1, const Output_segment* seg2);
957 // Use to save and restore segments during relaxation.
958 typedef Unordered_map<const Output_segment*, const Output_segment*>
961 // Save states of current output segments.
963 save_segments(Segment_states*);
965 // Restore output segment states.
967 restore_segments(const Segment_states*);
969 // Clean up after relaxation so that it is possible to lay out the
970 // sections and segments again.
972 clean_up_after_relaxation();
974 // Doing preparation work for relaxation. This is factored out to make
975 // Layout::finalized a bit smaller and easier to read.
977 prepare_for_relaxation();
979 // Main body of the relaxation loop, which lays out the section.
981 relaxation_loop_body(int, Target*, Symbol_table*, Output_segment**,
982 Output_segment*, Output_segment_headers*,
983 Output_file_header*, unsigned int*);
985 // A mapping used for kept comdats/.gnu.linkonce group signatures.
986 typedef Unordered_map<std::string, Kept_section> Signatures;
988 // Mapping from input section name/type/flags to output section. We
989 // use canonicalized strings here.
991 typedef std::pair<Stringpool::Key,
992 std::pair<elfcpp::Elf_Word, elfcpp::Elf_Xword> > Key;
997 operator()(const Key& k) const;
1000 typedef Unordered_map<Key, Output_section*, Hash_key> Section_name_map;
1002 // A comparison class for segments.
1004 struct Compare_segments
1007 operator()(const Output_segment* seg1, const Output_segment* seg2)
1008 { return Layout::segment_precedes(seg1, seg2); }
1011 typedef std::vector<Output_section_data*> Output_section_data_list;
1013 // Debug checker class.
1014 class Relaxation_debug_check
1017 Relaxation_debug_check()
1021 // Check that sections and special data are in reset states.
1023 check_output_data_for_reset_values(const Layout::Section_list&,
1024 const Layout::Data_list&);
1026 // Record information of a section list.
1028 read_sections(const Layout::Section_list&);
1030 // Verify a section list with recorded information.
1032 verify_sections(const Layout::Section_list&);
1035 // Information we care about a section.
1038 // Output section described by this.
1039 Output_section* output_section;
1048 // Section information.
1049 std::vector<Section_info> section_infos_;
1052 // The number of input files, for sizing tables.
1053 int number_of_input_files_;
1054 // Information set by scripts or by command line options.
1055 Script_options* script_options_;
1056 // The output section names.
1057 Stringpool namepool_;
1058 // The output symbol names.
1059 Stringpool sympool_;
1060 // The dynamic strings, if needed.
1061 Stringpool dynpool_;
1062 // The list of group sections and linkonce sections which we have seen.
1063 Signatures signatures_;
1064 // The mapping from input section name/type/flags to output sections.
1065 Section_name_map section_name_map_;
1066 // The list of output segments.
1067 Segment_list segment_list_;
1068 // The list of output sections.
1069 Section_list section_list_;
1070 // The list of output sections which are not attached to any output
1072 Section_list unattached_section_list_;
1073 // The list of unattached Output_data objects which require special
1074 // handling because they are not Output_sections.
1075 Data_list special_output_list_;
1076 // The section headers.
1077 Output_section_headers* section_headers_;
1078 // A pointer to the PT_TLS segment if there is one.
1079 Output_segment* tls_segment_;
1080 // A pointer to the PT_GNU_RELRO segment if there is one.
1081 Output_segment* relro_segment_;
1082 // A backend may increase the size of the PT_GNU_RELRO segment if
1083 // there is one. This is the amount to increase it by.
1084 unsigned int increase_relro_;
1085 // The SHT_SYMTAB output section.
1086 Output_section* symtab_section_;
1087 // The SHT_SYMTAB_SHNDX for the regular symbol table if there is one.
1088 Output_symtab_xindex* symtab_xindex_;
1089 // The SHT_DYNSYM output section if there is one.
1090 Output_section* dynsym_section_;
1091 // The SHT_SYMTAB_SHNDX for the dynamic symbol table if there is one.
1092 Output_symtab_xindex* dynsym_xindex_;
1093 // The SHT_DYNAMIC output section if there is one.
1094 Output_section* dynamic_section_;
1095 // The _DYNAMIC symbol if there is one.
1096 Symbol* dynamic_symbol_;
1097 // The dynamic data which goes into dynamic_section_.
1098 Output_data_dynamic* dynamic_data_;
1099 // The exception frame output section if there is one.
1100 Output_section* eh_frame_section_;
1101 // The exception frame data for eh_frame_section_.
1102 Eh_frame* eh_frame_data_;
1103 // Whether we have added eh_frame_data_ to the .eh_frame section.
1104 bool added_eh_frame_data_;
1105 // The exception frame header output section if there is one.
1106 Output_section* eh_frame_hdr_section_;
1107 // The space for the build ID checksum if there is one.
1108 Output_section_data* build_id_note_;
1109 // The output section containing dwarf abbreviations
1110 Output_reduced_debug_abbrev_section* debug_abbrev_;
1111 // The output section containing the dwarf debug info tree
1112 Output_reduced_debug_info_section* debug_info_;
1113 // A list of group sections and their signatures.
1114 Group_signatures group_signatures_;
1115 // The size of the output file.
1116 off_t output_file_size_;
1117 // Whether we have added an input section to an output section.
1118 bool have_added_input_section_;
1119 // Whether we have attached the sections to the segments.
1120 bool sections_are_attached_;
1121 // Whether we have seen an object file marked to require an
1122 // executable stack.
1123 bool input_requires_executable_stack_;
1124 // Whether we have seen at least one object file with an executable
1126 bool input_with_gnu_stack_note_;
1127 // Whether we have seen at least one object file without an
1128 // executable stack marker.
1129 bool input_without_gnu_stack_note_;
1130 // Whether we have seen an object file that uses the static TLS model.
1131 bool has_static_tls_;
1132 // Whether any sections require postprocessing.
1133 bool any_postprocessing_sections_;
1134 // Whether we have resized the signatures_ hash table.
1135 bool resized_signatures_;
1136 // Whether we have created a .stab*str output section.
1137 bool have_stabstr_section_;
1138 // In incremental build, holds information check the inputs and build the
1139 // .gnu_incremental_inputs section.
1140 Incremental_inputs* incremental_inputs_;
1141 // Whether we record output section data created in script
1142 bool record_output_section_data_from_script_;
1143 // List of output data that needs to be removed at relexation clean up.
1144 Output_section_data_list script_output_section_data_list_;
1145 // Structure to save segment states before entering the relaxation loop.
1146 Segment_states* segment_states_;
1147 // A relaxation debug checker. We only create one when in debugging mode.
1148 Relaxation_debug_check* relaxation_debug_check_;
1149 // Hash a pattern to its position in the section ordering file.
1150 Unordered_map<std::string, unsigned int> input_section_position_;
1151 // Vector of glob only patterns in the section_ordering file.
1152 std::vector<std::string> input_section_glob_;
1155 // This task handles writing out data in output sections which is not
1156 // part of an input section, or which requires special handling. When
1157 // this is done, it unblocks both output_sections_blocker and
1160 class Write_sections_task : public Task
1163 Write_sections_task(const Layout* layout, Output_file* of,
1164 Task_token* output_sections_blocker,
1165 Task_token* final_blocker)
1166 : layout_(layout), of_(of),
1167 output_sections_blocker_(output_sections_blocker),
1168 final_blocker_(final_blocker)
1171 // The standard Task methods.
1177 locks(Task_locker*);
1184 { return "Write_sections_task"; }
1187 class Write_sections_locker;
1189 const Layout* layout_;
1191 Task_token* output_sections_blocker_;
1192 Task_token* final_blocker_;
1195 // This task handles writing out data which is not part of a section
1198 class Write_data_task : public Task
1201 Write_data_task(const Layout* layout, const Symbol_table* symtab,
1202 Output_file* of, Task_token* final_blocker)
1203 : layout_(layout), symtab_(symtab), of_(of), final_blocker_(final_blocker)
1206 // The standard Task methods.
1212 locks(Task_locker*);
1219 { return "Write_data_task"; }
1222 const Layout* layout_;
1223 const Symbol_table* symtab_;
1225 Task_token* final_blocker_;
1228 // This task handles writing out the global symbols.
1230 class Write_symbols_task : public Task
1233 Write_symbols_task(const Layout* layout, const Symbol_table* symtab,
1234 const Input_objects* input_objects,
1235 const Stringpool* sympool, const Stringpool* dynpool,
1236 Output_file* of, Task_token* final_blocker)
1237 : layout_(layout), symtab_(symtab), input_objects_(input_objects),
1238 sympool_(sympool), dynpool_(dynpool), of_(of),
1239 final_blocker_(final_blocker)
1242 // The standard Task methods.
1248 locks(Task_locker*);
1255 { return "Write_symbols_task"; }
1258 const Layout* layout_;
1259 const Symbol_table* symtab_;
1260 const Input_objects* input_objects_;
1261 const Stringpool* sympool_;
1262 const Stringpool* dynpool_;
1264 Task_token* final_blocker_;
1267 // This task handles writing out data in output sections which can't
1268 // be written out until all the input sections have been handled.
1269 // This is for sections whose contents is based on the contents of
1270 // other output sections.
1272 class Write_after_input_sections_task : public Task
1275 Write_after_input_sections_task(Layout* layout, Output_file* of,
1276 Task_token* input_sections_blocker,
1277 Task_token* final_blocker)
1278 : layout_(layout), of_(of),
1279 input_sections_blocker_(input_sections_blocker),
1280 final_blocker_(final_blocker)
1283 // The standard Task methods.
1289 locks(Task_locker*);
1296 { return "Write_after_input_sections_task"; }
1301 Task_token* input_sections_blocker_;
1302 Task_token* final_blocker_;
1305 // This task function handles closing the file.
1307 class Close_task_runner : public Task_function_runner
1310 Close_task_runner(const General_options* options, const Layout* layout,
1312 : options_(options), layout_(layout), of_(of)
1315 // Run the operation.
1317 run(Workqueue*, const Task*);
1320 const General_options* options_;
1321 const Layout* layout_;
1325 // A small helper function to align an address.
1328 align_address(uint64_t address, uint64_t addralign)
1331 address = (address + addralign - 1) &~ (addralign - 1);
1335 } // End namespace gold.
1337 #endif // !defined(GOLD_LAYOUT_H)