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
;
55 // This task function handles mapping the input sections to output
56 // sections and laying them out in memory.
58 class Layout_task_runner
: public Task_function_runner
61 // OPTIONS is the command line options, INPUT_OBJECTS is the list of
62 // input objects, SYMTAB is the symbol table, LAYOUT is the layout
64 Layout_task_runner(const General_options
& options
,
65 const Input_objects
* input_objects
,
70 : options_(options
), input_objects_(input_objects
), symtab_(symtab
),
71 target_(target
), layout_(layout
), mapfile_(mapfile
)
76 run(Workqueue
*, const Task
*);
79 Layout_task_runner(const Layout_task_runner
&);
80 Layout_task_runner
& operator=(const Layout_task_runner
&);
82 const General_options
& options_
;
83 const Input_objects
* input_objects_
;
84 Symbol_table
* symtab_
;
90 // This class handles the details of laying out input sections.
95 Layout(const General_options
& options
, Script_options
*);
97 // Given an input section SHNDX, named NAME, with data in SHDR, from
98 // the object file OBJECT, return the output section where this
99 // input section should go. RELOC_SHNDX is the index of a
100 // relocation section which applies to this section, or 0 if none,
101 // or -1U if more than one. RELOC_TYPE is the type of the
102 // relocation section if there is one. Set *OFFSET to the offset
103 // within the output section.
104 template<int size
, bool big_endian
>
106 layout(Sized_relobj
<size
, big_endian
> *object
, unsigned int shndx
,
107 const char* name
, const elfcpp::Shdr
<size
, big_endian
>& shdr
,
108 unsigned int reloc_shndx
, unsigned int reloc_type
, off_t
* offset
);
110 // Layout an input reloc section when doing a relocatable link. The
111 // section is RELOC_SHNDX in OBJECT, with data in SHDR.
112 // DATA_SECTION is the reloc section to which it refers. RR is the
113 // relocatable information.
114 template<int size
, bool big_endian
>
116 layout_reloc(Sized_relobj
<size
, big_endian
>* object
,
117 unsigned int reloc_shndx
,
118 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
119 Output_section
* data_section
,
120 Relocatable_relocs
* rr
);
122 // Layout a group section when doing a relocatable link.
123 template<int size
, bool big_endian
>
125 layout_group(Symbol_table
* symtab
,
126 Sized_relobj
<size
, big_endian
>* object
,
127 unsigned int group_shndx
,
128 const char* group_section_name
,
129 const char* signature
,
130 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
131 elfcpp::Elf_Word flags
,
132 std::vector
<unsigned int>* shndxes
);
134 // Like layout, only for exception frame sections. OBJECT is an
135 // object file. SYMBOLS is the contents of the symbol table
136 // section, with size SYMBOLS_SIZE. SYMBOL_NAMES is the contents of
137 // the symbol name section, with size SYMBOL_NAMES_SIZE. SHNDX is a
138 // .eh_frame section in OBJECT. SHDR is the section header.
139 // RELOC_SHNDX is the index of a relocation section which applies to
140 // this section, or 0 if none, or -1U if more than one. RELOC_TYPE
141 // is the type of the relocation section if there is one. This
142 // returns the output section, and sets *OFFSET to the offset.
143 template<int size
, bool big_endian
>
145 layout_eh_frame(Sized_relobj
<size
, big_endian
>* object
,
146 const unsigned char* symbols
,
148 const unsigned char* symbol_names
,
149 off_t symbol_names_size
,
151 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
152 unsigned int reloc_shndx
, unsigned int reloc_type
,
155 // Handle a GNU stack note. This is called once per input object
156 // file. SEEN_GNU_STACK is true if the object file has a
157 // .note.GNU-stack section. GNU_STACK_FLAGS is the section flags
158 // from that section if there was one.
160 layout_gnu_stack(bool seen_gnu_stack
, uint64_t gnu_stack_flags
);
162 // Add an Output_section_data to the layout. This is used for
163 // special sections like the GOT section.
165 add_output_section_data(const char* name
, elfcpp::Elf_Word type
,
166 elfcpp::Elf_Xword flags
,
167 Output_section_data
*);
169 // Create dynamic sections if necessary.
171 create_initial_dynamic_sections(Symbol_table
*);
173 // Define __start and __stop symbols for output sections.
175 define_section_symbols(Symbol_table
*);
177 // Create sections for linker scripts.
179 create_script_sections()
180 { this->script_options_
->create_script_sections(this); }
182 // Define symbols from any linker script.
184 define_script_symbols(Symbol_table
* symtab
)
185 { this->script_options_
->add_symbols_to_table(symtab
); }
187 // Define symbols for group signatures.
189 define_group_signatures(Symbol_table
*);
191 // Return the Stringpool used for symbol names.
194 { return &this->sympool_
; }
196 // Return the Stringpool used for dynamic symbol names and dynamic
200 { return &this->dynpool_
; }
202 // Return the symtab_xindex section used to hold large section
203 // indexes for the normal symbol table.
204 Output_symtab_xindex
*
205 symtab_xindex() const
206 { return this->symtab_xindex_
; }
208 // Return the dynsym_xindex section used to hold large section
209 // indexes for the dynamic symbol table.
210 Output_symtab_xindex
*
211 dynsym_xindex() const
212 { return this->dynsym_xindex_
; }
214 // Return whether a section is a .gnu.linkonce section, given the
217 is_linkonce(const char* name
)
218 { return strncmp(name
, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
220 // Return true if a section is a debugging section.
222 is_debug_info_section(const char* name
)
224 // Debugging sections can only be recognized by name.
225 return (strncmp(name
, ".debug", sizeof(".debug") - 1) == 0
226 || strncmp(name
, ".gnu.linkonce.wi.",
227 sizeof(".gnu.linkonce.wi.") - 1) == 0
228 || strncmp(name
, ".line", sizeof(".line") - 1) == 0
229 || strncmp(name
, ".stab", sizeof(".stab") - 1) == 0);
232 // Record the signature of a comdat section, and return whether to
233 // include it in the link. The GROUP parameter is true for a
234 // section group signature, false for a signature derived from a
235 // .gnu.linkonce section.
237 add_comdat(Relobj
*, unsigned int, const std::string
&, bool group
);
239 // Find the given comdat signature, and return the object and section
240 // index of the kept group.
242 find_kept_object(const std::string
&, unsigned int*) const;
244 // Finalize the layout after all the input sections have been added.
246 finalize(const Input_objects
*, Symbol_table
*, Target
*, const Task
*);
248 // Return whether any sections require postprocessing.
250 any_postprocessing_sections() const
251 { return this->any_postprocessing_sections_
; }
253 // Return the size of the output file.
255 output_file_size() const
256 { return this->output_file_size_
; }
258 // Return the TLS segment. This will return NULL if there isn't
262 { return this->tls_segment_
; }
264 // Return the normal symbol table.
266 symtab_section() const
268 gold_assert(this->symtab_section_
!= NULL
);
269 return this->symtab_section_
;
272 // Return the dynamic symbol table.
274 dynsym_section() const
276 gold_assert(this->dynsym_section_
!= NULL
);
277 return this->dynsym_section_
;
280 // Return the dynamic tags.
283 { return this->dynamic_data_
; }
285 // Write out the output sections.
287 write_output_sections(Output_file
* of
) const;
289 // Write out data not associated with an input file or the symbol
292 write_data(const Symbol_table
*, Output_file
*) const;
294 // Write out output sections which can not be written until all the
295 // input sections are complete.
297 write_sections_after_input_sections(Output_file
* of
);
299 // Return an output section named NAME, or NULL if there is none.
301 find_output_section(const char* name
) const;
303 // Return an output segment of type TYPE, with segment flags SET set
304 // and segment flags CLEAR clear. Return NULL if there is none.
306 find_output_segment(elfcpp::PT type
, elfcpp::Elf_Word set
,
307 elfcpp::Elf_Word clear
) const;
309 // Return the number of segments we expect to produce.
311 expected_segment_count() const;
313 // Set a flag to indicate that an object file uses the static TLS model.
316 { this->has_static_tls_
= true; }
318 // Return true if any object file uses the static TLS model.
320 has_static_tls() const
321 { return this->has_static_tls_
; }
323 // Return the options which may be set by a linker script.
326 { return this->script_options_
; }
328 const Script_options
*
329 script_options() const
330 { return this->script_options_
; }
332 // Compute and write out the build ID if needed.
334 write_build_id(Output_file
*) const;
336 // Rewrite output file in binary format.
338 write_binary(Output_file
* in
) const;
340 // Print output sections to the map file.
342 print_to_mapfile(Mapfile
*) const;
344 // Dump statistical information to stderr.
348 // A list of segments.
350 typedef std::vector
<Output_segment
*> Segment_list
;
352 // A list of sections.
354 typedef std::vector
<Output_section
*> Section_list
;
356 // The list of information to write out which is not attached to
357 // either a section or a segment.
358 typedef std::vector
<Output_data
*> Data_list
;
360 // Store the allocated sections into the section list. This is used
361 // by the linker script code.
363 get_allocated_sections(Section_list
*) const;
365 // Make a section for a linker script to hold data.
367 make_output_section_for_script(const char* name
);
369 // Make a segment. This is used by the linker script code.
371 make_output_segment(elfcpp::Elf_Word type
, elfcpp::Elf_Word flags
);
373 // Return the number of segments.
375 segment_count() const
376 { return this->segment_list_
.size(); }
378 // Map from section flags to segment flags.
379 static elfcpp::Elf_Word
380 section_flags_to_segment(elfcpp::Elf_Xword flags
);
382 // Attach sections to segments.
384 attach_sections_to_segments();
387 Layout(const Layout
&);
388 Layout
& operator=(const Layout
&);
390 // Mapping from .gnu.linkonce section names to output section names.
391 struct Linkonce_mapping
398 static const Linkonce_mapping linkonce_mapping
[];
399 static const int linkonce_mapping_count
;
401 // During a relocatable link, a list of group sections and
403 struct Group_signature
405 // The group section.
406 Output_section
* section
;
408 const char* signature
;
411 : section(NULL
), signature(NULL
)
414 Group_signature(Output_section
* sectiona
, const char* signaturea
)
415 : section(sectiona
), signature(signaturea
)
418 typedef std::vector
<Group_signature
> Group_signatures
;
420 // Create a .note section, filling in the header.
422 create_note(const char* name
, int note_type
, size_t descsz
,
423 bool allocate
, size_t* trailing_padding
);
425 // Create a .note section for gold.
429 // Record whether the stack must be executable.
431 create_executable_stack_info(const Target
*);
433 // Create a build ID note if needed.
437 // Find the first read-only PT_LOAD segment, creating one if
440 find_first_load_seg();
442 // Count the local symbols in the regular symbol table and the dynamic
443 // symbol table, and build the respective string pools.
445 count_local_symbols(const Task
*, const Input_objects
*);
447 // Create the output sections for the symbol table.
449 create_symtab_sections(const Input_objects
*, Symbol_table
*,
450 unsigned int, off_t
*);
452 // Create the .shstrtab section.
456 // Create the section header table.
458 create_shdrs(const Output_section
* shstrtab_section
, off_t
*);
460 // Create the dynamic symbol table.
462 create_dynamic_symtab(const Input_objects
*, Symbol_table
*,
463 Output_section
** pdynstr
,
464 unsigned int* plocal_dynamic_count
,
465 std::vector
<Symbol
*>* pdynamic_symbols
,
468 // Assign offsets to each local portion of the dynamic symbol table.
470 assign_local_dynsym_offsets(const Input_objects
*);
472 // Finish the .dynamic section and PT_DYNAMIC segment.
474 finish_dynamic_section(const Input_objects
*, const Symbol_table
*);
476 // Create the .interp section and PT_INTERP segment.
478 create_interp(const Target
* target
);
480 // Create the version sections.
482 create_version_sections(const Versions
*,
484 unsigned int local_symcount
,
485 const std::vector
<Symbol
*>& dynamic_symbols
,
486 const Output_section
* dynstr
);
488 template<int size
, bool big_endian
>
490 sized_create_version_sections(const Versions
* versions
,
492 unsigned int local_symcount
,
493 const std::vector
<Symbol
*>& dynamic_symbols
,
494 const Output_section
* dynstr
);
496 // Return whether to include this section in the link.
497 template<int size
, bool big_endian
>
499 include_section(Sized_relobj
<size
, big_endian
>* object
, const char* name
,
500 const elfcpp::Shdr
<size
, big_endian
>&);
502 // Return the output section name to use given an input section
503 // name. Set *PLEN to the length of the name. *PLEN must be
504 // initialized to the length of NAME.
506 output_section_name(const char* name
, size_t* plen
);
508 // Return the output section name to use for a linkonce section
509 // name. PLEN is as for output_section_name.
511 linkonce_output_name(const char* name
, size_t* plen
);
513 // Return the number of allocated output sections.
515 allocated_output_section_count() const;
517 // Return the output section for NAME, TYPE and FLAGS.
519 get_output_section(const char* name
, Stringpool::Key name_key
,
520 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
);
522 // Choose the output section for NAME in RELOBJ.
524 choose_output_section(const Relobj
* relobj
, const char* name
,
525 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
,
526 bool is_input_section
);
528 // Create a new Output_section.
530 make_output_section(const char* name
, elfcpp::Elf_Word type
,
531 elfcpp::Elf_Xword flags
);
533 // Attach a section to a segment.
535 attach_section_to_segment(Output_section
*);
537 // Attach an allocated section to a segment.
539 attach_allocated_section_to_segment(Output_section
*);
541 // Set the final file offsets of all the segments.
543 set_segment_offsets(const Target
*, Output_segment
*, unsigned int* pshndx
);
545 // Set the file offsets of the sections when doing a relocatable
548 set_relocatable_section_offsets(Output_data
*, unsigned int* pshndx
);
550 // Set the final file offsets of all the sections not associated
551 // with a segment. We set section offsets in three passes: the
552 // first handles all allocated sections, the second sections that
553 // require postprocessing, and the last the late-bound STRTAB
554 // sections (probably only shstrtab, which is the one we care about
555 // because it holds section names).
556 enum Section_offset_pass
558 BEFORE_INPUT_SECTIONS_PASS
,
559 POSTPROCESSING_SECTIONS_PASS
,
560 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
563 set_section_offsets(off_t
, Section_offset_pass pass
);
565 // Set the final section indexes of all the sections not associated
566 // with a segment. Returns the next unused index.
568 set_section_indexes(unsigned int pshndx
);
570 // Set the section addresses when using a script.
572 set_section_addresses_from_script(Symbol_table
*);
574 // Return whether SEG1 comes before SEG2 in the output file.
576 segment_precedes(const Output_segment
* seg1
, const Output_segment
* seg2
);
578 // A mapping used for group signatures.
582 : object_(NULL
), shndx_(0), group_(false)
584 Kept_section(Relobj
* object
, unsigned int shndx
, bool group
)
585 : object_(object
), shndx_(shndx
), group_(group
)
591 typedef Unordered_map
<std::string
, Kept_section
> Signatures
;
593 // Mapping from input section name/type/flags to output section. We
594 // use canonicalized strings here.
596 typedef std::pair
<Stringpool::Key
,
597 std::pair
<elfcpp::Elf_Word
, elfcpp::Elf_Xword
> > Key
;
602 operator()(const Key
& k
) const;
605 typedef Unordered_map
<Key
, Output_section
*, Hash_key
> Section_name_map
;
607 // A comparison class for segments.
609 struct Compare_segments
612 operator()(const Output_segment
* seg1
, const Output_segment
* seg2
)
613 { return Layout::segment_precedes(seg1
, seg2
); }
616 // A reference to the options on the command line.
617 const General_options
& options_
;
618 // Information set by scripts or by command line options.
619 Script_options
* script_options_
;
620 // The output section names.
621 Stringpool namepool_
;
622 // The output symbol names.
624 // The dynamic strings, if needed.
626 // The list of group sections and linkonce sections which we have seen.
627 Signatures signatures_
;
628 // The mapping from input section name/type/flags to output sections.
629 Section_name_map section_name_map_
;
630 // The list of output segments.
631 Segment_list segment_list_
;
632 // The list of output sections.
633 Section_list section_list_
;
634 // The list of output sections which are not attached to any output
636 Section_list unattached_section_list_
;
637 // Whether we have attached the sections to the segments.
638 bool sections_are_attached_
;
639 // The list of unattached Output_data objects which require special
640 // handling because they are not Output_sections.
641 Data_list special_output_list_
;
642 // The section headers.
643 Output_section_headers
* section_headers_
;
644 // A pointer to the PT_TLS segment if there is one.
645 Output_segment
* tls_segment_
;
646 // A pointer to the PT_GNU_RELRO segment if there is one.
647 Output_segment
* relro_segment_
;
648 // The SHT_SYMTAB output section.
649 Output_section
* symtab_section_
;
650 // The SHT_SYMTAB_SHNDX for the regular symbol table if there is one.
651 Output_symtab_xindex
* symtab_xindex_
;
652 // The SHT_DYNSYM output section if there is one.
653 Output_section
* dynsym_section_
;
654 // The SHT_SYMTAB_SHNDX for the dynamic symbol table if there is one.
655 Output_symtab_xindex
* dynsym_xindex_
;
656 // The SHT_DYNAMIC output section if there is one.
657 Output_section
* dynamic_section_
;
658 // The dynamic data which goes into dynamic_section_.
659 Output_data_dynamic
* dynamic_data_
;
660 // The exception frame output section if there is one.
661 Output_section
* eh_frame_section_
;
662 // The exception frame data for eh_frame_section_.
663 Eh_frame
* eh_frame_data_
;
664 // Whether we have added eh_frame_data_ to the .eh_frame section.
665 bool added_eh_frame_data_
;
666 // The exception frame header output section if there is one.
667 Output_section
* eh_frame_hdr_section_
;
668 // The space for the build ID checksum if there is one.
669 Output_section_data
* build_id_note_
;
670 // A list of group sections and their signatures.
671 Group_signatures group_signatures_
;
672 // The size of the output file.
673 off_t output_file_size_
;
674 // Whether we have seen an object file marked to require an
676 bool input_requires_executable_stack_
;
677 // Whether we have seen at least one object file with an executable
679 bool input_with_gnu_stack_note_
;
680 // Whether we have seen at least one object file without an
681 // executable stack marker.
682 bool input_without_gnu_stack_note_
;
683 // Whether we have seen an object file that uses the static TLS model.
684 bool has_static_tls_
;
685 // Whether any sections require postprocessing.
686 bool any_postprocessing_sections_
;
689 // This task handles writing out data in output sections which is not
690 // part of an input section, or which requires special handling. When
691 // this is done, it unblocks both output_sections_blocker and
694 class Write_sections_task
: public Task
697 Write_sections_task(const Layout
* layout
, Output_file
* of
,
698 Task_token
* output_sections_blocker
,
699 Task_token
* final_blocker
)
700 : layout_(layout
), of_(of
),
701 output_sections_blocker_(output_sections_blocker
),
702 final_blocker_(final_blocker
)
705 // The standard Task methods.
718 { return "Write_sections_task"; }
721 class Write_sections_locker
;
723 const Layout
* layout_
;
725 Task_token
* output_sections_blocker_
;
726 Task_token
* final_blocker_
;
729 // This task handles writing out data which is not part of a section
732 class Write_data_task
: public Task
735 Write_data_task(const Layout
* layout
, const Symbol_table
* symtab
,
736 Output_file
* of
, Task_token
* final_blocker
)
737 : layout_(layout
), symtab_(symtab
), of_(of
), final_blocker_(final_blocker
)
740 // The standard Task methods.
753 { return "Write_data_task"; }
756 const Layout
* layout_
;
757 const Symbol_table
* symtab_
;
759 Task_token
* final_blocker_
;
762 // This task handles writing out the global symbols.
764 class Write_symbols_task
: public Task
767 Write_symbols_task(const Layout
* layout
, const Symbol_table
* symtab
,
768 const Input_objects
* input_objects
,
769 const Stringpool
* sympool
, const Stringpool
* dynpool
,
770 Output_file
* of
, Task_token
* final_blocker
)
771 : layout_(layout
), symtab_(symtab
), input_objects_(input_objects
),
772 sympool_(sympool
), dynpool_(dynpool
), of_(of
),
773 final_blocker_(final_blocker
)
776 // The standard Task methods.
789 { return "Write_symbols_task"; }
792 const Layout
* layout_
;
793 const Symbol_table
* symtab_
;
794 const Input_objects
* input_objects_
;
795 const Stringpool
* sympool_
;
796 const Stringpool
* dynpool_
;
798 Task_token
* final_blocker_
;
801 // This task handles writing out data in output sections which can't
802 // be written out until all the input sections have been handled.
803 // This is for sections whose contents is based on the contents of
804 // other output sections.
806 class Write_after_input_sections_task
: public Task
809 Write_after_input_sections_task(Layout
* layout
, Output_file
* of
,
810 Task_token
* input_sections_blocker
,
811 Task_token
* final_blocker
)
812 : layout_(layout
), of_(of
),
813 input_sections_blocker_(input_sections_blocker
),
814 final_blocker_(final_blocker
)
817 // The standard Task methods.
830 { return "Write_after_input_sections_task"; }
835 Task_token
* input_sections_blocker_
;
836 Task_token
* final_blocker_
;
839 // This task function handles closing the file.
841 class Close_task_runner
: public Task_function_runner
844 Close_task_runner(const General_options
* options
, const Layout
* layout
,
846 : options_(options
), layout_(layout
), of_(of
)
849 // Run the operation.
851 run(Workqueue
*, const Task
*);
854 const General_options
* options_
;
855 const Layout
* layout_
;
859 // A small helper function to align an address.
862 align_address(uint64_t address
, uint64_t addralign
)
865 address
= (address
+ addralign
- 1) &~ (addralign
- 1);
869 } // End namespace gold.
871 #endif // !defined(GOLD_LAYOUT_H)