1 // layout.h -- lay out output file sections for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
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 class handles the details of laying out input sections.
97 Layout(const General_options
& options
, Script_options
*);
99 // Given an input section SHNDX, named NAME, with data in SHDR, from
100 // the object file OBJECT, return the output section where this
101 // input section should go. RELOC_SHNDX is the index of a
102 // relocation section which applies to this section, or 0 if none,
103 // or -1U if more than one. RELOC_TYPE is the type of the
104 // relocation section if there is one. Set *OFFSET to the offset
105 // within the output section.
106 template<int size
, bool big_endian
>
108 layout(Sized_relobj
<size
, big_endian
> *object
, unsigned int shndx
,
109 const char* name
, const elfcpp::Shdr
<size
, big_endian
>& shdr
,
110 unsigned int reloc_shndx
, unsigned int reloc_type
, off_t
* offset
);
112 // Layout an input reloc section when doing a relocatable link. The
113 // section is RELOC_SHNDX in OBJECT, with data in SHDR.
114 // DATA_SECTION is the reloc section to which it refers. RR is the
115 // relocatable information.
116 template<int size
, bool big_endian
>
118 layout_reloc(Sized_relobj
<size
, big_endian
>* object
,
119 unsigned int reloc_shndx
,
120 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
121 Output_section
* data_section
,
122 Relocatable_relocs
* rr
);
124 // Layout a group section when doing a relocatable link.
125 template<int size
, bool big_endian
>
127 layout_group(Symbol_table
* symtab
,
128 Sized_relobj
<size
, big_endian
>* object
,
129 unsigned int group_shndx
,
130 const char* group_section_name
,
131 const char* signature
,
132 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
133 elfcpp::Elf_Word flags
,
134 std::vector
<unsigned int>* shndxes
);
136 // Like layout, only for exception frame sections. OBJECT is an
137 // object file. SYMBOLS is the contents of the symbol table
138 // section, with size SYMBOLS_SIZE. SYMBOL_NAMES is the contents of
139 // the symbol name section, with size SYMBOL_NAMES_SIZE. SHNDX is a
140 // .eh_frame section in OBJECT. SHDR is the section header.
141 // RELOC_SHNDX is the index of a relocation section which applies to
142 // this section, or 0 if none, or -1U if more than one. RELOC_TYPE
143 // is the type of the relocation section if there is one. This
144 // returns the output section, and sets *OFFSET to the offset.
145 template<int size
, bool big_endian
>
147 layout_eh_frame(Sized_relobj
<size
, big_endian
>* object
,
148 const unsigned char* symbols
,
150 const unsigned char* symbol_names
,
151 off_t symbol_names_size
,
153 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
154 unsigned int reloc_shndx
, unsigned int reloc_type
,
157 // Handle a GNU stack note. This is called once per input object
158 // file. SEEN_GNU_STACK is true if the object file has a
159 // .note.GNU-stack section. GNU_STACK_FLAGS is the section flags
160 // from that section if there was one.
162 layout_gnu_stack(bool seen_gnu_stack
, uint64_t gnu_stack_flags
);
164 // Add an Output_section_data to the layout. This is used for
165 // special sections like the GOT section.
167 add_output_section_data(const char* name
, elfcpp::Elf_Word type
,
168 elfcpp::Elf_Xword flags
,
169 Output_section_data
*);
171 // Create dynamic sections if necessary.
173 create_initial_dynamic_sections(Symbol_table
*);
175 // Define __start and __stop symbols for output sections.
177 define_section_symbols(Symbol_table
*);
179 // Create sections for linker scripts.
181 create_script_sections()
182 { this->script_options_
->create_script_sections(this); }
184 // Define symbols from any linker script.
186 define_script_symbols(Symbol_table
* symtab
)
187 { this->script_options_
->add_symbols_to_table(symtab
); }
189 // Define symbols for group signatures.
191 define_group_signatures(Symbol_table
*);
193 // Return the Stringpool used for symbol names.
196 { return &this->sympool_
; }
198 // Return the Stringpool used for dynamic symbol names and dynamic
202 { return &this->dynpool_
; }
204 // Return the symtab_xindex section used to hold large section
205 // indexes for the normal symbol table.
206 Output_symtab_xindex
*
207 symtab_xindex() const
208 { return this->symtab_xindex_
; }
210 // Return the dynsym_xindex section used to hold large section
211 // indexes for the dynamic symbol table.
212 Output_symtab_xindex
*
213 dynsym_xindex() const
214 { return this->dynsym_xindex_
; }
216 // Return whether a section is a .gnu.linkonce section, given the
219 is_linkonce(const char* name
)
220 { return strncmp(name
, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
222 // Return true if a section is a debugging section.
224 is_debug_info_section(const char* name
)
226 // Debugging sections can only be recognized by name.
227 return (strncmp(name
, ".debug", sizeof(".debug") - 1) == 0
228 || strncmp(name
, ".gnu.linkonce.wi.",
229 sizeof(".gnu.linkonce.wi.") - 1) == 0
230 || strncmp(name
, ".line", sizeof(".line") - 1) == 0
231 || strncmp(name
, ".stab", sizeof(".stab") - 1) == 0);
234 // Record the signature of a comdat section, and return whether to
235 // include it in the link. The GROUP parameter is true for a
236 // section group signature, false for a signature derived from a
237 // .gnu.linkonce section.
239 add_comdat(Relobj
*, unsigned int, const std::string
&, bool group
);
241 // Find the given comdat signature, and return the object and section
242 // index of the kept group.
244 find_kept_object(const std::string
&, unsigned int*) const;
246 // Finalize the layout after all the input sections have been added.
248 finalize(const Input_objects
*, Symbol_table
*, Target
*, const Task
*);
250 // Return whether any sections require postprocessing.
252 any_postprocessing_sections() const
253 { return this->any_postprocessing_sections_
; }
255 // Return the size of the output file.
257 output_file_size() const
258 { return this->output_file_size_
; }
260 // Return the TLS segment. This will return NULL if there isn't
264 { return this->tls_segment_
; }
266 // Return the normal symbol table.
268 symtab_section() const
270 gold_assert(this->symtab_section_
!= NULL
);
271 return this->symtab_section_
;
274 // Return the dynamic symbol table.
276 dynsym_section() const
278 gold_assert(this->dynsym_section_
!= NULL
);
279 return this->dynsym_section_
;
282 // Return the dynamic tags.
285 { return this->dynamic_data_
; }
287 // Write out the output sections.
289 write_output_sections(Output_file
* of
) const;
291 // Write out data not associated with an input file or the symbol
294 write_data(const Symbol_table
*, Output_file
*) const;
296 // Write out output sections which can not be written until all the
297 // input sections are complete.
299 write_sections_after_input_sections(Output_file
* of
);
301 // Return an output section named NAME, or NULL if there is none.
303 find_output_section(const char* name
) const;
305 // Return an output segment of type TYPE, with segment flags SET set
306 // and segment flags CLEAR clear. Return NULL if there is none.
308 find_output_segment(elfcpp::PT type
, elfcpp::Elf_Word set
,
309 elfcpp::Elf_Word clear
) const;
311 // Return the number of segments we expect to produce.
313 expected_segment_count() const;
315 // Set a flag to indicate that an object file uses the static TLS model.
318 { this->has_static_tls_
= true; }
320 // Return true if any object file uses the static TLS model.
322 has_static_tls() const
323 { return this->has_static_tls_
; }
325 // Return the options which may be set by a linker script.
328 { return this->script_options_
; }
330 const Script_options
*
331 script_options() const
332 { return this->script_options_
; }
334 // Compute and write out the build ID if needed.
336 write_build_id(Output_file
*) const;
338 // Rewrite output file in binary format.
340 write_binary(Output_file
* in
) const;
342 // Print output sections to the map file.
344 print_to_mapfile(Mapfile
*) const;
346 // Dump statistical information to stderr.
350 // A list of segments.
352 typedef std::vector
<Output_segment
*> Segment_list
;
354 // A list of sections.
356 typedef std::vector
<Output_section
*> Section_list
;
358 // The list of information to write out which is not attached to
359 // either a section or a segment.
360 typedef std::vector
<Output_data
*> Data_list
;
362 // Store the allocated sections into the section list. This is used
363 // by the linker script code.
365 get_allocated_sections(Section_list
*) const;
367 // Make a section for a linker script to hold data.
369 make_output_section_for_script(const char* name
);
371 // Make a segment. This is used by the linker script code.
373 make_output_segment(elfcpp::Elf_Word type
, elfcpp::Elf_Word flags
);
375 // Return the number of segments.
377 segment_count() const
378 { return this->segment_list_
.size(); }
380 // Map from section flags to segment flags.
381 static elfcpp::Elf_Word
382 section_flags_to_segment(elfcpp::Elf_Xword flags
);
384 // Attach sections to segments.
386 attach_sections_to_segments();
389 Layout(const Layout
&);
390 Layout
& operator=(const Layout
&);
392 // Mapping from .gnu.linkonce section names to output section names.
393 struct Linkonce_mapping
400 static const Linkonce_mapping linkonce_mapping
[];
401 static const int linkonce_mapping_count
;
403 // During a relocatable link, a list of group sections and
405 struct Group_signature
407 // The group section.
408 Output_section
* section
;
410 const char* signature
;
413 : section(NULL
), signature(NULL
)
416 Group_signature(Output_section
* sectiona
, const char* signaturea
)
417 : section(sectiona
), signature(signaturea
)
420 typedef std::vector
<Group_signature
> Group_signatures
;
422 // Create a .note section, filling in the header.
424 create_note(const char* name
, int note_type
, size_t descsz
,
425 bool allocate
, size_t* trailing_padding
);
427 // Create a .note section for gold.
431 // Record whether the stack must be executable.
433 create_executable_stack_info(const Target
*);
435 // Create a build ID note if needed.
439 // Find the first read-only PT_LOAD segment, creating one if
442 find_first_load_seg();
444 // Count the local symbols in the regular symbol table and the dynamic
445 // symbol table, and build the respective string pools.
447 count_local_symbols(const Task
*, const Input_objects
*);
449 // Create the output sections for the symbol table.
451 create_symtab_sections(const Input_objects
*, Symbol_table
*,
452 unsigned int, off_t
*);
454 // Create the .shstrtab section.
458 // Create the section header table.
460 create_shdrs(const Output_section
* shstrtab_section
, off_t
*);
462 // Create the dynamic symbol table.
464 create_dynamic_symtab(const Input_objects
*, Symbol_table
*,
465 Output_section
** pdynstr
,
466 unsigned int* plocal_dynamic_count
,
467 std::vector
<Symbol
*>* pdynamic_symbols
,
470 // Assign offsets to each local portion of the dynamic symbol table.
472 assign_local_dynsym_offsets(const Input_objects
*);
474 // Finish the .dynamic section and PT_DYNAMIC segment.
476 finish_dynamic_section(const Input_objects
*, const Symbol_table
*);
478 // Create the .interp section and PT_INTERP segment.
480 create_interp(const Target
* target
);
482 // Create the version sections.
484 create_version_sections(const Versions
*,
486 unsigned int local_symcount
,
487 const std::vector
<Symbol
*>& dynamic_symbols
,
488 const Output_section
* dynstr
);
490 template<int size
, bool big_endian
>
492 sized_create_version_sections(const Versions
* versions
,
494 unsigned int local_symcount
,
495 const std::vector
<Symbol
*>& dynamic_symbols
,
496 const Output_section
* dynstr
);
498 // Return whether to include this section in the link.
499 template<int size
, bool big_endian
>
501 include_section(Sized_relobj
<size
, big_endian
>* object
, const char* name
,
502 const elfcpp::Shdr
<size
, big_endian
>&);
504 // Return the output section name to use given an input section
505 // name. Set *PLEN to the length of the name. *PLEN must be
506 // initialized to the length of NAME.
508 output_section_name(const char* name
, size_t* plen
);
510 // Return the output section name to use for a linkonce section
511 // name. PLEN is as for output_section_name.
513 linkonce_output_name(const char* name
, size_t* plen
);
515 // Return the number of allocated output sections.
517 allocated_output_section_count() const;
519 // Return the output section for NAME, TYPE and FLAGS.
521 get_output_section(const char* name
, Stringpool::Key name_key
,
522 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
);
524 // Choose the output section for NAME in RELOBJ.
526 choose_output_section(const Relobj
* relobj
, const char* name
,
527 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
,
528 bool is_input_section
);
530 // Create a new Output_section.
532 make_output_section(const char* name
, elfcpp::Elf_Word type
,
533 elfcpp::Elf_Xword flags
);
535 // Attach a section to a segment.
537 attach_section_to_segment(Output_section
*);
539 // Attach an allocated section to a segment.
541 attach_allocated_section_to_segment(Output_section
*);
543 // Set the final file offsets of all the segments.
545 set_segment_offsets(const Target
*, Output_segment
*, unsigned int* pshndx
);
547 // Set the file offsets of the sections when doing a relocatable
550 set_relocatable_section_offsets(Output_data
*, unsigned int* pshndx
);
552 // Set the final file offsets of all the sections not associated
553 // with a segment. We set section offsets in three passes: the
554 // first handles all allocated sections, the second sections that
555 // require postprocessing, and the last the late-bound STRTAB
556 // sections (probably only shstrtab, which is the one we care about
557 // because it holds section names).
558 enum Section_offset_pass
560 BEFORE_INPUT_SECTIONS_PASS
,
561 POSTPROCESSING_SECTIONS_PASS
,
562 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
565 set_section_offsets(off_t
, Section_offset_pass pass
);
567 // Set the final section indexes of all the sections not associated
568 // with a segment. Returns the next unused index.
570 set_section_indexes(unsigned int pshndx
);
572 // Set the section addresses when using a script.
574 set_section_addresses_from_script(Symbol_table
*);
576 // Return whether SEG1 comes before SEG2 in the output file.
578 segment_precedes(const Output_segment
* seg1
, const Output_segment
* seg2
);
580 // A mapping used for group signatures.
584 : object_(NULL
), shndx_(0), group_(false)
586 Kept_section(Relobj
* object
, unsigned int shndx
, bool group
)
587 : object_(object
), shndx_(shndx
), group_(group
)
593 typedef Unordered_map
<std::string
, Kept_section
> Signatures
;
595 // Mapping from input section name/type/flags to output section. We
596 // use canonicalized strings here.
598 typedef std::pair
<Stringpool::Key
,
599 std::pair
<elfcpp::Elf_Word
, elfcpp::Elf_Xword
> > Key
;
604 operator()(const Key
& k
) const;
607 typedef Unordered_map
<Key
, Output_section
*, Hash_key
> Section_name_map
;
609 // A comparison class for segments.
611 struct Compare_segments
614 operator()(const Output_segment
* seg1
, const Output_segment
* seg2
)
615 { return Layout::segment_precedes(seg1
, seg2
); }
618 // A reference to the options on the command line.
619 const General_options
& options_
;
620 // Information set by scripts or by command line options.
621 Script_options
* script_options_
;
622 // The output section names.
623 Stringpool namepool_
;
624 // The output symbol names.
626 // The dynamic strings, if needed.
628 // The list of group sections and linkonce sections which we have seen.
629 Signatures signatures_
;
630 // The mapping from input section name/type/flags to output sections.
631 Section_name_map section_name_map_
;
632 // The list of output segments.
633 Segment_list segment_list_
;
634 // The list of output sections.
635 Section_list section_list_
;
636 // The list of output sections which are not attached to any output
638 Section_list unattached_section_list_
;
639 // Whether we have attached the sections to the segments.
640 bool sections_are_attached_
;
641 // The list of unattached Output_data objects which require special
642 // handling because they are not Output_sections.
643 Data_list special_output_list_
;
644 // The section headers.
645 Output_section_headers
* section_headers_
;
646 // A pointer to the PT_TLS segment if there is one.
647 Output_segment
* tls_segment_
;
648 // A pointer to the PT_GNU_RELRO segment if there is one.
649 Output_segment
* relro_segment_
;
650 // The SHT_SYMTAB output section.
651 Output_section
* symtab_section_
;
652 // The SHT_SYMTAB_SHNDX for the regular symbol table if there is one.
653 Output_symtab_xindex
* symtab_xindex_
;
654 // The SHT_DYNSYM output section if there is one.
655 Output_section
* dynsym_section_
;
656 // The SHT_SYMTAB_SHNDX for the dynamic symbol table if there is one.
657 Output_symtab_xindex
* dynsym_xindex_
;
658 // The SHT_DYNAMIC output section if there is one.
659 Output_section
* dynamic_section_
;
660 // The dynamic data which goes into dynamic_section_.
661 Output_data_dynamic
* dynamic_data_
;
662 // The exception frame output section if there is one.
663 Output_section
* eh_frame_section_
;
664 // The exception frame data for eh_frame_section_.
665 Eh_frame
* eh_frame_data_
;
666 // Whether we have added eh_frame_data_ to the .eh_frame section.
667 bool added_eh_frame_data_
;
668 // The exception frame header output section if there is one.
669 Output_section
* eh_frame_hdr_section_
;
670 // The space for the build ID checksum if there is one.
671 Output_section_data
* build_id_note_
;
672 // The output section containing dwarf abbreviations
673 Output_reduced_debug_abbrev_section
* debug_abbrev_
;
674 // The output section containing the dwarf debug info tree
675 Output_reduced_debug_info_section
* debug_info_
;
676 // A list of group sections and their signatures.
677 Group_signatures group_signatures_
;
678 // The size of the output file.
679 off_t output_file_size_
;
680 // Whether we have seen an object file marked to require an
682 bool input_requires_executable_stack_
;
683 // Whether we have seen at least one object file with an executable
685 bool input_with_gnu_stack_note_
;
686 // Whether we have seen at least one object file without an
687 // executable stack marker.
688 bool input_without_gnu_stack_note_
;
689 // Whether we have seen an object file that uses the static TLS model.
690 bool has_static_tls_
;
691 // Whether any sections require postprocessing.
692 bool any_postprocessing_sections_
;
695 // This task handles writing out data in output sections which is not
696 // part of an input section, or which requires special handling. When
697 // this is done, it unblocks both output_sections_blocker and
700 class Write_sections_task
: public Task
703 Write_sections_task(const Layout
* layout
, Output_file
* of
,
704 Task_token
* output_sections_blocker
,
705 Task_token
* final_blocker
)
706 : layout_(layout
), of_(of
),
707 output_sections_blocker_(output_sections_blocker
),
708 final_blocker_(final_blocker
)
711 // The standard Task methods.
724 { return "Write_sections_task"; }
727 class Write_sections_locker
;
729 const Layout
* layout_
;
731 Task_token
* output_sections_blocker_
;
732 Task_token
* final_blocker_
;
735 // This task handles writing out data which is not part of a section
738 class Write_data_task
: public Task
741 Write_data_task(const Layout
* layout
, const Symbol_table
* symtab
,
742 Output_file
* of
, Task_token
* final_blocker
)
743 : layout_(layout
), symtab_(symtab
), of_(of
), final_blocker_(final_blocker
)
746 // The standard Task methods.
759 { return "Write_data_task"; }
762 const Layout
* layout_
;
763 const Symbol_table
* symtab_
;
765 Task_token
* final_blocker_
;
768 // This task handles writing out the global symbols.
770 class Write_symbols_task
: public Task
773 Write_symbols_task(const Layout
* layout
, const Symbol_table
* symtab
,
774 const Input_objects
* input_objects
,
775 const Stringpool
* sympool
, const Stringpool
* dynpool
,
776 Output_file
* of
, Task_token
* final_blocker
)
777 : layout_(layout
), symtab_(symtab
), input_objects_(input_objects
),
778 sympool_(sympool
), dynpool_(dynpool
), of_(of
),
779 final_blocker_(final_blocker
)
782 // The standard Task methods.
795 { return "Write_symbols_task"; }
798 const Layout
* layout_
;
799 const Symbol_table
* symtab_
;
800 const Input_objects
* input_objects_
;
801 const Stringpool
* sympool_
;
802 const Stringpool
* dynpool_
;
804 Task_token
* final_blocker_
;
807 // This task handles writing out data in output sections which can't
808 // be written out until all the input sections have been handled.
809 // This is for sections whose contents is based on the contents of
810 // other output sections.
812 class Write_after_input_sections_task
: public Task
815 Write_after_input_sections_task(Layout
* layout
, Output_file
* of
,
816 Task_token
* input_sections_blocker
,
817 Task_token
* final_blocker
)
818 : layout_(layout
), of_(of
),
819 input_sections_blocker_(input_sections_blocker
),
820 final_blocker_(final_blocker
)
823 // The standard Task methods.
836 { return "Write_after_input_sections_task"; }
841 Task_token
* input_sections_blocker_
;
842 Task_token
* final_blocker_
;
845 // This task function handles closing the file.
847 class Close_task_runner
: public Task_function_runner
850 Close_task_runner(const General_options
* options
, const Layout
* layout
,
852 : options_(options
), layout_(layout
), of_(of
)
855 // Run the operation.
857 run(Workqueue
*, const Task
*);
860 const General_options
* options_
;
861 const Layout
* layout_
;
865 // A small helper function to align an address.
868 align_address(uint64_t address
, uint64_t addralign
)
871 address
= (address
+ addralign
- 1) &~ (addralign
- 1);
875 } // End namespace gold.
877 #endif // !defined(GOLD_LAYOUT_H)