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
;
45 class Output_section_data
;
47 class Output_section_headers
;
50 class Output_data_dynamic
;
51 class Output_symtab_xindex
;
52 class Output_reduced_debug_abbrev_section
;
53 class Output_reduced_debug_info_section
;
57 // This task function handles mapping the input sections to output
58 // sections and laying them out in memory.
60 class Layout_task_runner
: public Task_function_runner
63 // OPTIONS is the command line options, INPUT_OBJECTS is the list of
64 // input objects, SYMTAB is the symbol table, LAYOUT is the layout
66 Layout_task_runner(const General_options
& options
,
67 const Input_objects
* input_objects
,
72 : options_(options
), input_objects_(input_objects
), symtab_(symtab
),
73 target_(target
), layout_(layout
), mapfile_(mapfile
)
78 run(Workqueue
*, const Task
*);
81 Layout_task_runner(const Layout_task_runner
&);
82 Layout_task_runner
& operator=(const Layout_task_runner
&);
84 const General_options
& options_
;
85 const Input_objects
* input_objects_
;
86 Symbol_table
* symtab_
;
92 // This struct holds information about the comdat or .gnu.linkonce
98 : object(NULL
), shndx(0), is_group(false), group_sections(NULL
)
100 Kept_section(Relobj
* a_object
, unsigned int a_shndx
, bool a_is_group
)
101 : object(a_object
), shndx(a_shndx
), is_group(a_is_group
),
105 typedef Unordered_map
<std::string
, unsigned int> Comdat_group
;
107 // The object containing the comdat or .gnu.linkonce.
109 // Index to the group section for comdats and the section itself for
112 // The Kept_sections are values of a mapping, that maps names to
113 // them. This field is true if this struct is associated with the
114 // name of a comdat or .gnu.linkonce, false if it is associated with
115 // the name of a symbol obtained from the .gnu.linkonce.* name
116 // through some heuristics.
118 // For comdats, a map from names of the sections in the group to
119 // indexes in OBJECT_. NULL for .gnu.linkonce.
120 Comdat_group
* group_sections
;
123 // This class handles the details of laying out input sections.
128 Layout(const General_options
& options
, Script_options
*);
130 // Given an input section SHNDX, named NAME, with data in SHDR, from
131 // the object file OBJECT, return the output section where this
132 // input section should go. RELOC_SHNDX is the index of a
133 // relocation section which applies to this section, or 0 if none,
134 // or -1U if more than one. RELOC_TYPE is the type of the
135 // relocation section if there is one. Set *OFFSET to the offset
136 // within the output section.
137 template<int size
, bool big_endian
>
139 layout(Sized_relobj
<size
, big_endian
> *object
, unsigned int shndx
,
140 const char* name
, const elfcpp::Shdr
<size
, big_endian
>& shdr
,
141 unsigned int reloc_shndx
, unsigned int reloc_type
, off_t
* offset
);
143 // Layout an input reloc section when doing a relocatable link. The
144 // section is RELOC_SHNDX in OBJECT, with data in SHDR.
145 // DATA_SECTION is the reloc section to which it refers. RR is the
146 // relocatable information.
147 template<int size
, bool big_endian
>
149 layout_reloc(Sized_relobj
<size
, big_endian
>* object
,
150 unsigned int reloc_shndx
,
151 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
152 Output_section
* data_section
,
153 Relocatable_relocs
* rr
);
155 // Layout a group section when doing a relocatable link.
156 template<int size
, bool big_endian
>
158 layout_group(Symbol_table
* symtab
,
159 Sized_relobj
<size
, big_endian
>* object
,
160 unsigned int group_shndx
,
161 const char* group_section_name
,
162 const char* signature
,
163 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
164 elfcpp::Elf_Word flags
,
165 std::vector
<unsigned int>* shndxes
);
167 // Like layout, only for exception frame sections. OBJECT is an
168 // object file. SYMBOLS is the contents of the symbol table
169 // section, with size SYMBOLS_SIZE. SYMBOL_NAMES is the contents of
170 // the symbol name section, with size SYMBOL_NAMES_SIZE. SHNDX is a
171 // .eh_frame section in OBJECT. SHDR is the section header.
172 // RELOC_SHNDX is the index of a relocation section which applies to
173 // this section, or 0 if none, or -1U if more than one. RELOC_TYPE
174 // is the type of the relocation section if there is one. This
175 // returns the output section, and sets *OFFSET to the offset.
176 template<int size
, bool big_endian
>
178 layout_eh_frame(Sized_relobj
<size
, big_endian
>* object
,
179 const unsigned char* symbols
,
181 const unsigned char* symbol_names
,
182 off_t symbol_names_size
,
184 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
185 unsigned int reloc_shndx
, unsigned int reloc_type
,
188 // Handle a GNU stack note. This is called once per input object
189 // file. SEEN_GNU_STACK is true if the object file has a
190 // .note.GNU-stack section. GNU_STACK_FLAGS is the section flags
191 // from that section if there was one.
193 layout_gnu_stack(bool seen_gnu_stack
, uint64_t gnu_stack_flags
);
195 // Add an Output_section_data to the layout. This is used for
196 // special sections like the GOT section.
198 add_output_section_data(const char* name
, elfcpp::Elf_Word type
,
199 elfcpp::Elf_Xword flags
,
200 Output_section_data
*);
202 // Create dynamic sections if necessary.
204 create_initial_dynamic_sections(Symbol_table
*);
206 // Define __start and __stop symbols for output sections.
208 define_section_symbols(Symbol_table
*);
210 // Create sections for linker scripts.
212 create_script_sections()
213 { this->script_options_
->create_script_sections(this); }
215 // Define symbols from any linker script.
217 define_script_symbols(Symbol_table
* symtab
)
218 { this->script_options_
->add_symbols_to_table(symtab
); }
220 // Define symbols for group signatures.
222 define_group_signatures(Symbol_table
*);
224 // Return the Stringpool used for symbol names.
227 { return &this->sympool_
; }
229 // Return the Stringpool used for dynamic symbol names and dynamic
233 { return &this->dynpool_
; }
235 // Return the symtab_xindex section used to hold large section
236 // indexes for the normal symbol table.
237 Output_symtab_xindex
*
238 symtab_xindex() const
239 { return this->symtab_xindex_
; }
241 // Return the dynsym_xindex section used to hold large section
242 // indexes for the dynamic symbol table.
243 Output_symtab_xindex
*
244 dynsym_xindex() const
245 { return this->dynsym_xindex_
; }
247 // Return whether a section is a .gnu.linkonce section, given the
250 is_linkonce(const char* name
)
251 { return strncmp(name
, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
253 // Return true if a section is a debugging section.
255 is_debug_info_section(const char* name
)
257 // Debugging sections can only be recognized by name.
258 return (strncmp(name
, ".debug", sizeof(".debug") - 1) == 0
259 || strncmp(name
, ".gnu.linkonce.wi.",
260 sizeof(".gnu.linkonce.wi.") - 1) == 0
261 || strncmp(name
, ".line", sizeof(".line") - 1) == 0
262 || strncmp(name
, ".stab", sizeof(".stab") - 1) == 0);
265 // Check if a comdat group or .gnu.linkonce section with the given
266 // NAME is selected for the link. If there is already a section,
267 // *KEPT_SECTION is set to point to the signature and the function
268 // returns false. Otherwise, the CANDIDATE signature is recorded
269 // for this NAME in the layout object, *KEPT_SECTION is set to the
270 // internal copy and the function return false. In some cases, with
271 // CANDIDATE->GROUP_ being false, KEPT_SECTION can point back to
274 find_or_add_kept_section(const std::string name
,
275 Kept_section
* candidate
,
276 Kept_section
** kept_section
);
278 // Find the given comdat signature, and return the object and section
279 // index of the kept group.
281 find_kept_object(const std::string
&, unsigned int*) const;
283 // Finalize the layout after all the input sections have been added.
285 finalize(const Input_objects
*, Symbol_table
*, Target
*, const Task
*);
287 // Return whether any sections require postprocessing.
289 any_postprocessing_sections() const
290 { return this->any_postprocessing_sections_
; }
292 // Return the size of the output file.
294 output_file_size() const
295 { return this->output_file_size_
; }
297 // Return the TLS segment. This will return NULL if there isn't
301 { return this->tls_segment_
; }
303 // Return the normal symbol table.
305 symtab_section() const
307 gold_assert(this->symtab_section_
!= NULL
);
308 return this->symtab_section_
;
311 // Return the dynamic symbol table.
313 dynsym_section() const
315 gold_assert(this->dynsym_section_
!= NULL
);
316 return this->dynsym_section_
;
319 // Return the dynamic tags.
322 { return this->dynamic_data_
; }
324 // Write out the output sections.
326 write_output_sections(Output_file
* of
) const;
328 // Write out data not associated with an input file or the symbol
331 write_data(const Symbol_table
*, Output_file
*) const;
333 // Write out output sections which can not be written until all the
334 // input sections are complete.
336 write_sections_after_input_sections(Output_file
* of
);
338 // Return an output section named NAME, or NULL if there is none.
340 find_output_section(const char* name
) const;
342 // Return an output segment of type TYPE, with segment flags SET set
343 // and segment flags CLEAR clear. Return NULL if there is none.
345 find_output_segment(elfcpp::PT type
, elfcpp::Elf_Word set
,
346 elfcpp::Elf_Word clear
) const;
348 // Return the number of segments we expect to produce.
350 expected_segment_count() const;
352 // Set a flag to indicate that an object file uses the static TLS model.
355 { this->has_static_tls_
= true; }
357 // Return true if any object file uses the static TLS model.
359 has_static_tls() const
360 { return this->has_static_tls_
; }
362 // Return the options which may be set by a linker script.
365 { return this->script_options_
; }
367 const Script_options
*
368 script_options() const
369 { return this->script_options_
; }
371 // Compute and write out the build ID if needed.
373 write_build_id(Output_file
*) const;
375 // Rewrite output file in binary format.
377 write_binary(Output_file
* in
) const;
379 // Print output sections to the map file.
381 print_to_mapfile(Mapfile
*) const;
383 // Dump statistical information to stderr.
387 // A list of segments.
389 typedef std::vector
<Output_segment
*> Segment_list
;
391 // A list of sections.
393 typedef std::vector
<Output_section
*> Section_list
;
395 // The list of information to write out which is not attached to
396 // either a section or a segment.
397 typedef std::vector
<Output_data
*> Data_list
;
399 // Store the allocated sections into the section list. This is used
400 // by the linker script code.
402 get_allocated_sections(Section_list
*) const;
404 // Make a section for a linker script to hold data.
406 make_output_section_for_script(const char* name
);
408 // Make a segment. This is used by the linker script code.
410 make_output_segment(elfcpp::Elf_Word type
, elfcpp::Elf_Word flags
);
412 // Return the number of segments.
414 segment_count() const
415 { return this->segment_list_
.size(); }
417 // Map from section flags to segment flags.
418 static elfcpp::Elf_Word
419 section_flags_to_segment(elfcpp::Elf_Xword flags
);
421 // Attach sections to segments.
423 attach_sections_to_segments();
426 Layout(const Layout
&);
427 Layout
& operator=(const Layout
&);
429 // Mapping from .gnu.linkonce section names to output section names.
430 struct Linkonce_mapping
437 static const Linkonce_mapping linkonce_mapping
[];
438 static const int linkonce_mapping_count
;
440 // During a relocatable link, a list of group sections and
442 struct Group_signature
444 // The group section.
445 Output_section
* section
;
447 const char* signature
;
450 : section(NULL
), signature(NULL
)
453 Group_signature(Output_section
* sectiona
, const char* signaturea
)
454 : section(sectiona
), signature(signaturea
)
457 typedef std::vector
<Group_signature
> Group_signatures
;
459 // Create a note section, filling in the header.
461 create_note(const char* name
, int note_type
, const char *section_name
,
462 size_t descsz
, bool allocate
, size_t* trailing_padding
);
464 // Create a note section for gold version.
468 // Record whether the stack must be executable.
470 create_executable_stack_info(const Target
*);
472 // Create a build ID note if needed.
476 // Find the first read-only PT_LOAD segment, creating one if
479 find_first_load_seg();
481 // Count the local symbols in the regular symbol table and the dynamic
482 // symbol table, and build the respective string pools.
484 count_local_symbols(const Task
*, const Input_objects
*);
486 // Create the output sections for the symbol table.
488 create_symtab_sections(const Input_objects
*, Symbol_table
*,
489 unsigned int, off_t
*);
491 // Create the .shstrtab section.
495 // Create the section header table.
497 create_shdrs(const Output_section
* shstrtab_section
, off_t
*);
499 // Create the dynamic symbol table.
501 create_dynamic_symtab(const Input_objects
*, Symbol_table
*,
502 Output_section
** pdynstr
,
503 unsigned int* plocal_dynamic_count
,
504 std::vector
<Symbol
*>* pdynamic_symbols
,
507 // Assign offsets to each local portion of the dynamic symbol table.
509 assign_local_dynsym_offsets(const Input_objects
*);
511 // Finish the .dynamic section and PT_DYNAMIC segment.
513 finish_dynamic_section(const Input_objects
*, const Symbol_table
*);
515 // Create the .interp section and PT_INTERP segment.
517 create_interp(const Target
* target
);
519 // Create the version sections.
521 create_version_sections(const Versions
*,
523 unsigned int local_symcount
,
524 const std::vector
<Symbol
*>& dynamic_symbols
,
525 const Output_section
* dynstr
);
527 template<int size
, bool big_endian
>
529 sized_create_version_sections(const Versions
* versions
,
531 unsigned int local_symcount
,
532 const std::vector
<Symbol
*>& dynamic_symbols
,
533 const Output_section
* dynstr
);
535 // Return whether to include this section in the link.
536 template<int size
, bool big_endian
>
538 include_section(Sized_relobj
<size
, big_endian
>* object
, const char* name
,
539 const elfcpp::Shdr
<size
, big_endian
>&);
541 // Return the output section name to use given an input section
542 // name. Set *PLEN to the length of the name. *PLEN must be
543 // initialized to the length of NAME.
545 output_section_name(const char* name
, size_t* plen
);
547 // Return the output section name to use for a linkonce section
548 // name. PLEN is as for output_section_name.
550 linkonce_output_name(const char* name
, size_t* plen
);
552 // Return the number of allocated output sections.
554 allocated_output_section_count() const;
556 // Return the output section for NAME, TYPE and FLAGS.
558 get_output_section(const char* name
, Stringpool::Key name_key
,
559 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
);
561 // Choose the output section for NAME in RELOBJ.
563 choose_output_section(const Relobj
* relobj
, const char* name
,
564 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
,
565 bool is_input_section
);
567 // Create a new Output_section.
569 make_output_section(const char* name
, elfcpp::Elf_Word type
,
570 elfcpp::Elf_Xword flags
);
572 // Attach a section to a segment.
574 attach_section_to_segment(Output_section
*);
576 // Attach an allocated section to a segment.
578 attach_allocated_section_to_segment(Output_section
*);
580 // Set the final file offsets of all the segments.
582 set_segment_offsets(const Target
*, Output_segment
*, unsigned int* pshndx
);
584 // Set the file offsets of the sections when doing a relocatable
587 set_relocatable_section_offsets(Output_data
*, unsigned int* pshndx
);
589 // Set the final file offsets of all the sections not associated
590 // with a segment. We set section offsets in three passes: the
591 // first handles all allocated sections, the second sections that
592 // require postprocessing, and the last the late-bound STRTAB
593 // sections (probably only shstrtab, which is the one we care about
594 // because it holds section names).
595 enum Section_offset_pass
597 BEFORE_INPUT_SECTIONS_PASS
,
598 POSTPROCESSING_SECTIONS_PASS
,
599 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
602 set_section_offsets(off_t
, Section_offset_pass pass
);
604 // Set the final section indexes of all the sections not associated
605 // with a segment. Returns the next unused index.
607 set_section_indexes(unsigned int pshndx
);
609 // Set the section addresses when using a script.
611 set_section_addresses_from_script(Symbol_table
*);
613 // Return whether SEG1 comes before SEG2 in the output file.
615 segment_precedes(const Output_segment
* seg1
, const Output_segment
* seg2
);
617 // A mapping used for kept comdats/.gnu.linkonce group signatures.
618 typedef Unordered_map
<std::string
, Kept_section
> Signatures
;
620 // Mapping from input section name/type/flags to output section. We
621 // use canonicalized strings here.
623 typedef std::pair
<Stringpool::Key
,
624 std::pair
<elfcpp::Elf_Word
, elfcpp::Elf_Xword
> > Key
;
629 operator()(const Key
& k
) const;
632 typedef Unordered_map
<Key
, Output_section
*, Hash_key
> Section_name_map
;
634 // A comparison class for segments.
636 struct Compare_segments
639 operator()(const Output_segment
* seg1
, const Output_segment
* seg2
)
640 { return Layout::segment_precedes(seg1
, seg2
); }
643 // A reference to the options on the command line.
644 const General_options
& options_
;
645 // Information set by scripts or by command line options.
646 Script_options
* script_options_
;
647 // The output section names.
648 Stringpool namepool_
;
649 // The output symbol names.
651 // The dynamic strings, if needed.
653 // The list of group sections and linkonce sections which we have seen.
654 Signatures signatures_
;
655 // The mapping from input section name/type/flags to output sections.
656 Section_name_map section_name_map_
;
657 // The list of output segments.
658 Segment_list segment_list_
;
659 // The list of output sections.
660 Section_list section_list_
;
661 // The list of output sections which are not attached to any output
663 Section_list unattached_section_list_
;
664 // Whether we have attached the sections to the segments.
665 bool sections_are_attached_
;
666 // The list of unattached Output_data objects which require special
667 // handling because they are not Output_sections.
668 Data_list special_output_list_
;
669 // The section headers.
670 Output_section_headers
* section_headers_
;
671 // A pointer to the PT_TLS segment if there is one.
672 Output_segment
* tls_segment_
;
673 // A pointer to the PT_GNU_RELRO segment if there is one.
674 Output_segment
* relro_segment_
;
675 // The SHT_SYMTAB output section.
676 Output_section
* symtab_section_
;
677 // The SHT_SYMTAB_SHNDX for the regular symbol table if there is one.
678 Output_symtab_xindex
* symtab_xindex_
;
679 // The SHT_DYNSYM output section if there is one.
680 Output_section
* dynsym_section_
;
681 // The SHT_SYMTAB_SHNDX for the dynamic symbol table if there is one.
682 Output_symtab_xindex
* dynsym_xindex_
;
683 // The SHT_DYNAMIC output section if there is one.
684 Output_section
* dynamic_section_
;
685 // The dynamic data which goes into dynamic_section_.
686 Output_data_dynamic
* dynamic_data_
;
687 // The exception frame output section if there is one.
688 Output_section
* eh_frame_section_
;
689 // The exception frame data for eh_frame_section_.
690 Eh_frame
* eh_frame_data_
;
691 // Whether we have added eh_frame_data_ to the .eh_frame section.
692 bool added_eh_frame_data_
;
693 // The exception frame header output section if there is one.
694 Output_section
* eh_frame_hdr_section_
;
695 // The space for the build ID checksum if there is one.
696 Output_section_data
* build_id_note_
;
697 // The output section containing dwarf abbreviations
698 Output_reduced_debug_abbrev_section
* debug_abbrev_
;
699 // The output section containing the dwarf debug info tree
700 Output_reduced_debug_info_section
* debug_info_
;
701 // A list of group sections and their signatures.
702 Group_signatures group_signatures_
;
703 // The size of the output file.
704 off_t output_file_size_
;
705 // Whether we have seen an object file marked to require an
707 bool input_requires_executable_stack_
;
708 // Whether we have seen at least one object file with an executable
710 bool input_with_gnu_stack_note_
;
711 // Whether we have seen at least one object file without an
712 // executable stack marker.
713 bool input_without_gnu_stack_note_
;
714 // Whether we have seen an object file that uses the static TLS model.
715 bool has_static_tls_
;
716 // Whether any sections require postprocessing.
717 bool any_postprocessing_sections_
;
720 // This task handles writing out data in output sections which is not
721 // part of an input section, or which requires special handling. When
722 // this is done, it unblocks both output_sections_blocker and
725 class Write_sections_task
: public Task
728 Write_sections_task(const Layout
* layout
, Output_file
* of
,
729 Task_token
* output_sections_blocker
,
730 Task_token
* final_blocker
)
731 : layout_(layout
), of_(of
),
732 output_sections_blocker_(output_sections_blocker
),
733 final_blocker_(final_blocker
)
736 // The standard Task methods.
749 { return "Write_sections_task"; }
752 class Write_sections_locker
;
754 const Layout
* layout_
;
756 Task_token
* output_sections_blocker_
;
757 Task_token
* final_blocker_
;
760 // This task handles writing out data which is not part of a section
763 class Write_data_task
: public Task
766 Write_data_task(const Layout
* layout
, const Symbol_table
* symtab
,
767 Output_file
* of
, Task_token
* final_blocker
)
768 : layout_(layout
), symtab_(symtab
), of_(of
), final_blocker_(final_blocker
)
771 // The standard Task methods.
784 { return "Write_data_task"; }
787 const Layout
* layout_
;
788 const Symbol_table
* symtab_
;
790 Task_token
* final_blocker_
;
793 // This task handles writing out the global symbols.
795 class Write_symbols_task
: public Task
798 Write_symbols_task(const Layout
* layout
, const Symbol_table
* symtab
,
799 const Input_objects
* input_objects
,
800 const Stringpool
* sympool
, const Stringpool
* dynpool
,
801 Output_file
* of
, Task_token
* final_blocker
)
802 : layout_(layout
), symtab_(symtab
), input_objects_(input_objects
),
803 sympool_(sympool
), dynpool_(dynpool
), of_(of
),
804 final_blocker_(final_blocker
)
807 // The standard Task methods.
820 { return "Write_symbols_task"; }
823 const Layout
* layout_
;
824 const Symbol_table
* symtab_
;
825 const Input_objects
* input_objects_
;
826 const Stringpool
* sympool_
;
827 const Stringpool
* dynpool_
;
829 Task_token
* final_blocker_
;
832 // This task handles writing out data in output sections which can't
833 // be written out until all the input sections have been handled.
834 // This is for sections whose contents is based on the contents of
835 // other output sections.
837 class Write_after_input_sections_task
: public Task
840 Write_after_input_sections_task(Layout
* layout
, Output_file
* of
,
841 Task_token
* input_sections_blocker
,
842 Task_token
* final_blocker
)
843 : layout_(layout
), of_(of
),
844 input_sections_blocker_(input_sections_blocker
),
845 final_blocker_(final_blocker
)
848 // The standard Task methods.
861 { return "Write_after_input_sections_task"; }
866 Task_token
* input_sections_blocker_
;
867 Task_token
* final_blocker_
;
870 // This task function handles closing the file.
872 class Close_task_runner
: public Task_function_runner
875 Close_task_runner(const General_options
* options
, const Layout
* layout
,
877 : options_(options
), layout_(layout
), of_(of
)
880 // Run the operation.
882 run(Workqueue
*, const Task
*);
885 const General_options
* options_
;
886 const Layout
* layout_
;
890 // A small helper function to align an address.
893 align_address(uint64_t address
, uint64_t addralign
)
896 address
= (address
+ addralign
- 1) &~ (addralign
- 1);
900 } // End namespace gold.
902 #endif // !defined(GOLD_LAYOUT_H)