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.
32 #include "workqueue.h"
35 #include "stringpool.h"
40 class General_options
;
43 class Output_section_data
;
45 class Output_section_headers
;
48 class Output_data_dynamic
;
52 // This task function handles mapping the input sections to output
53 // sections and laying them out in memory.
55 class Layout_task_runner
: public Task_function_runner
58 // OPTIONS is the command line options, INPUT_OBJECTS is the list of
59 // input objects, SYMTAB is the symbol table, LAYOUT is the layout
61 Layout_task_runner(const General_options
& options
,
62 const Input_objects
* input_objects
,
66 : options_(options
), input_objects_(input_objects
), symtab_(symtab
),
67 target_(target
), layout_(layout
)
72 run(Workqueue
*, const Task
*);
75 Layout_task_runner(const Layout_task_runner
&);
76 Layout_task_runner
& operator=(const Layout_task_runner
&);
78 const General_options
& options_
;
79 const Input_objects
* input_objects_
;
80 Symbol_table
* symtab_
;
85 // This class handles the details of laying out input sections.
90 Layout(const General_options
& options
, Script_options
*);
92 // Given an input section SHNDX, named NAME, with data in SHDR, from
93 // the object file OBJECT, return the output section where this
94 // input section should go. RELOC_SHNDX is the index of a
95 // relocation section which applies to this section, or 0 if none,
96 // or -1U if more than one. RELOC_TYPE is the type of the
97 // relocation section if there is one. Set *OFFSET to the offset
98 // within the output section.
99 template<int size
, bool big_endian
>
101 layout(Sized_relobj
<size
, big_endian
> *object
, unsigned int shndx
,
102 const char* name
, const elfcpp::Shdr
<size
, big_endian
>& shdr
,
103 unsigned int reloc_shndx
, unsigned int reloc_type
, off_t
* offset
);
105 // Layout an input reloc section when doing a relocatable link. The
106 // section is RELOC_SHNDX in OBJECT, with data in SHDR.
107 // DATA_SECTION is the reloc section to which it refers. RR is the
108 // relocatable information.
109 template<int size
, bool big_endian
>
111 layout_reloc(Sized_relobj
<size
, big_endian
>* object
,
112 unsigned int reloc_shndx
,
113 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
114 Output_section
* data_section
,
115 Relocatable_relocs
* rr
);
117 // Layout a group section when doing a relocatable link.
118 template<int size
, bool big_endian
>
120 layout_group(Symbol_table
* symtab
,
121 Sized_relobj
<size
, big_endian
>* object
,
122 unsigned int group_shndx
,
123 const char* group_section_name
,
124 const char* signature
,
125 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
126 const elfcpp::Elf_Word
* contents
);
128 // Like layout, only for exception frame sections. OBJECT is an
129 // object file. SYMBOLS is the contents of the symbol table
130 // section, with size SYMBOLS_SIZE. SYMBOL_NAMES is the contents of
131 // the symbol name section, with size SYMBOL_NAMES_SIZE. SHNDX is a
132 // .eh_frame section in OBJECT. SHDR is the section header.
133 // RELOC_SHNDX is the index of a relocation section which applies to
134 // this section, or 0 if none, or -1U if more than one. RELOC_TYPE
135 // is the type of the relocation section if there is one. This
136 // returns the output section, and sets *OFFSET to the offset.
137 template<int size
, bool big_endian
>
139 layout_eh_frame(Sized_relobj
<size
, big_endian
>* object
,
140 const unsigned char* symbols
,
142 const unsigned char* symbol_names
,
143 off_t symbol_names_size
,
145 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
146 unsigned int reloc_shndx
, unsigned int reloc_type
,
149 // Handle a GNU stack note. This is called once per input object
150 // file. SEEN_GNU_STACK is true if the object file has a
151 // .note.GNU-stack section. GNU_STACK_FLAGS is the section flags
152 // from that section if there was one.
154 layout_gnu_stack(bool seen_gnu_stack
, uint64_t gnu_stack_flags
);
156 // Add an Output_section_data to the layout. This is used for
157 // special sections like the GOT section.
159 add_output_section_data(const char* name
, elfcpp::Elf_Word type
,
160 elfcpp::Elf_Xword flags
,
161 Output_section_data
*);
163 // Create dynamic sections if necessary.
165 create_initial_dynamic_sections(Symbol_table
*);
167 // Define __start and __stop symbols for output sections.
169 define_section_symbols(Symbol_table
*);
171 // Create sections for linker scripts.
173 create_script_sections()
174 { this->script_options_
->create_script_sections(this); }
176 // Define symbols from any linker script.
178 define_script_symbols(Symbol_table
* symtab
)
179 { this->script_options_
->add_symbols_to_table(symtab
); }
181 // Define symbols for group signatures.
183 define_group_signatures(Symbol_table
*);
185 // Return the Stringpool used for symbol names.
188 { return &this->sympool_
; }
190 // Return the Stringpool used for dynamic symbol names and dynamic
194 { return &this->dynpool_
; }
196 // Return whether a section is a .gnu.linkonce section, given the
199 is_linkonce(const char* name
)
200 { return strncmp(name
, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
202 // Record the signature of a comdat section, and return whether to
203 // include it in the link. The GROUP parameter is true for a
204 // section group signature, false for a signature derived from a
205 // .gnu.linkonce section.
207 add_comdat(const char*, bool group
);
209 // Finalize the layout after all the input sections have been added.
211 finalize(const Input_objects
*, Symbol_table
*, Target
*, const Task
*);
213 // Return whether any sections require postprocessing.
215 any_postprocessing_sections() const
216 { return this->any_postprocessing_sections_
; }
218 // Return the size of the output file.
220 output_file_size() const
221 { return this->output_file_size_
; }
223 // Return the TLS segment. This will return NULL if there isn't
227 { return this->tls_segment_
; }
229 // Return the normal symbol table.
231 symtab_section() const
233 gold_assert(this->symtab_section_
!= NULL
);
234 return this->symtab_section_
;
237 // Return the dynamic symbol table.
239 dynsym_section() const
241 gold_assert(this->dynsym_section_
!= NULL
);
242 return this->dynsym_section_
;
245 // Return the dynamic tags.
248 { return this->dynamic_data_
; }
250 // Write out the output sections.
252 write_output_sections(Output_file
* of
) const;
254 // Write out data not associated with an input file or the symbol
257 write_data(const Symbol_table
*, Output_file
*) const;
259 // Write out output sections which can not be written until all the
260 // input sections are complete.
262 write_sections_after_input_sections(Output_file
* of
);
264 // Return an output section named NAME, or NULL if there is none.
266 find_output_section(const char* name
) const;
268 // Return an output segment of type TYPE, with segment flags SET set
269 // and segment flags CLEAR clear. Return NULL if there is none.
271 find_output_segment(elfcpp::PT type
, elfcpp::Elf_Word set
,
272 elfcpp::Elf_Word clear
) const;
274 // Return the number of segments we expect to produce.
276 expected_segment_count() const;
278 // Set a flag to indicate that an object file uses the static TLS model.
281 { this->has_static_tls_
= true; }
283 // Return true if any object file uses the static TLS model.
285 has_static_tls() const
286 { return this->has_static_tls_
; }
288 // Return the options which may be set by a linker script.
291 { return this->script_options_
; }
293 const Script_options
*
294 script_options() const
295 { return this->script_options_
; }
297 // Rewrite output file in binary format.
299 write_binary(Output_file
* in
) const;
301 // Dump statistical information to stderr.
305 // A list of segments.
307 typedef std::vector
<Output_segment
*> Segment_list
;
309 // A list of sections.
311 typedef std::vector
<Output_section
*> Section_list
;
313 // The list of information to write out which is not attached to
314 // either a section or a segment.
315 typedef std::vector
<Output_data
*> Data_list
;
317 // Store the allocated sections into the section list. This is used
318 // by the linker script code.
320 get_allocated_sections(Section_list
*) const;
322 // Make a section for a linker script to hold data.
324 make_output_section_for_script(const char* name
);
326 // Make a segment. This is used by the linker script code.
328 make_output_segment(elfcpp::Elf_Word type
, elfcpp::Elf_Word flags
);
330 // Return the number of segments.
332 segment_count() const
333 { return this->segment_list_
.size(); }
335 // Map from section flags to segment flags.
336 static elfcpp::Elf_Word
337 section_flags_to_segment(elfcpp::Elf_Xword flags
);
340 Layout(const Layout
&);
341 Layout
& operator=(const Layout
&);
343 // Mapping from .gnu.linkonce section names to output section names.
344 struct Linkonce_mapping
351 static const Linkonce_mapping linkonce_mapping
[];
352 static const int linkonce_mapping_count
;
354 // During a relocatable link, a list of group sections and
356 struct Group_signature
358 // The group section.
359 Output_section
* section
;
361 const char* signature
;
364 : section(NULL
), signature(NULL
)
367 Group_signature(Output_section
* sectiona
, const char* signaturea
)
368 : section(sectiona
), signature(signaturea
)
371 typedef std::vector
<Group_signature
> Group_signatures
;
373 // Create a .note section for gold.
377 // Record whether the stack must be executable.
379 create_executable_stack_info(const Target
*);
381 // Find the first read-only PT_LOAD segment, creating one if
384 find_first_load_seg();
386 // Count the local symbols in the regular symbol table and the dynamic
387 // symbol table, and build the respective string pools.
389 count_local_symbols(const Task
*, const Input_objects
*);
391 // Create the output sections for the symbol table.
393 create_symtab_sections(const Input_objects
*, Symbol_table
*, off_t
*);
395 // Create the .shstrtab section.
399 // Create the section header table.
401 create_shdrs(off_t
*);
403 // Create the dynamic symbol table.
405 create_dynamic_symtab(const Input_objects
*, Symbol_table
*,
406 Output_section
** pdynstr
,
407 unsigned int* plocal_dynamic_count
,
408 std::vector
<Symbol
*>* pdynamic_symbols
,
411 // Assign offsets to each local portion of the dynamic symbol table.
413 assign_local_dynsym_offsets(const Input_objects
*);
415 // Finish the .dynamic section and PT_DYNAMIC segment.
417 finish_dynamic_section(const Input_objects
*, const Symbol_table
*);
419 // Create the .interp section and PT_INTERP segment.
421 create_interp(const Target
* target
);
423 // Create the version sections.
425 create_version_sections(const Versions
*,
427 unsigned int local_symcount
,
428 const std::vector
<Symbol
*>& dynamic_symbols
,
429 const Output_section
* dynstr
);
431 template<int size
, bool big_endian
>
433 sized_create_version_sections(const Versions
* versions
,
435 unsigned int local_symcount
,
436 const std::vector
<Symbol
*>& dynamic_symbols
,
437 const Output_section
* dynstr
);
439 // Return whether to include this section in the link.
440 template<int size
, bool big_endian
>
442 include_section(Sized_relobj
<size
, big_endian
>* object
, const char* name
,
443 const elfcpp::Shdr
<size
, big_endian
>&);
445 // Return the output section name to use given an input section
446 // name. Set *PLEN to the length of the name. *PLEN must be
447 // initialized to the length of NAME.
449 output_section_name(const char* name
, size_t* plen
);
451 // Return the output section name to use for a linkonce section
452 // name. PLEN is as for output_section_name.
454 linkonce_output_name(const char* name
, size_t* plen
);
456 // Return the output section for NAME, TYPE and FLAGS.
458 get_output_section(const char* name
, Stringpool::Key name_key
,
459 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
);
461 // Choose the output section for NAME in RELOBJ.
463 choose_output_section(const Relobj
* relobj
, const char* name
,
464 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
,
467 // Create a new Output_section.
469 make_output_section(const char* name
, elfcpp::Elf_Word type
,
470 elfcpp::Elf_Xword flags
);
472 // Set the final file offsets of all the segments.
474 set_segment_offsets(const Target
*, Output_segment
*, unsigned int* pshndx
);
476 // Set the file offsets of the sections when doing a relocatable
479 set_relocatable_section_offsets(Output_data
*, unsigned int* pshndx
);
481 // Set the final file offsets of all the sections not associated
482 // with a segment. We set section offsets in three passes: the
483 // first handles all allocated sections, the second sections that
484 // require postprocessing, and the last the late-bound STRTAB
485 // sections (probably only shstrtab, which is the one we care about
486 // because it holds section names).
487 enum Section_offset_pass
489 BEFORE_INPUT_SECTIONS_PASS
,
490 POSTPROCESSING_SECTIONS_PASS
,
491 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
494 set_section_offsets(off_t
, Section_offset_pass pass
);
496 // Set the final section indexes of all the sections not associated
497 // with a segment. Returns the next unused index.
499 set_section_indexes(unsigned int pshndx
);
501 // Set the section addresses when using a script.
503 set_section_addresses_from_script(Symbol_table
*);
505 // Return whether SEG1 comes before SEG2 in the output file.
507 segment_precedes(const Output_segment
* seg1
, const Output_segment
* seg2
);
509 // A mapping used for group signatures.
510 typedef Unordered_map
<std::string
, bool> Signatures
;
512 // Mapping from input section name/type/flags to output section. We
513 // use canonicalized strings here.
515 typedef std::pair
<Stringpool::Key
,
516 std::pair
<elfcpp::Elf_Word
, elfcpp::Elf_Xword
> > Key
;
521 operator()(const Key
& k
) const;
524 typedef Unordered_map
<Key
, Output_section
*, Hash_key
> Section_name_map
;
526 // A comparison class for segments.
528 struct Compare_segments
531 operator()(const Output_segment
* seg1
, const Output_segment
* seg2
)
532 { return Layout::segment_precedes(seg1
, seg2
); }
535 // A reference to the options on the command line.
536 const General_options
& options_
;
537 // Information set by scripts or by command line options.
538 Script_options
* script_options_
;
539 // The output section names.
540 Stringpool namepool_
;
541 // The output symbol names.
543 // The dynamic strings, if needed.
545 // The list of group sections and linkonce sections which we have seen.
546 Signatures signatures_
;
547 // The mapping from input section name/type/flags to output sections.
548 Section_name_map section_name_map_
;
549 // The list of output segments.
550 Segment_list segment_list_
;
551 // The list of output sections.
552 Section_list section_list_
;
553 // The list of output sections which are not attached to any output
555 Section_list unattached_section_list_
;
556 // The list of unattached Output_data objects which require special
557 // handling because they are not Output_sections.
558 Data_list special_output_list_
;
559 // The section headers.
560 Output_section_headers
* section_headers_
;
561 // A pointer to the PT_TLS segment if there is one.
562 Output_segment
* tls_segment_
;
563 // The SHT_SYMTAB output section.
564 Output_section
* symtab_section_
;
565 // The SHT_DYNSYM output section if there is one.
566 Output_section
* dynsym_section_
;
567 // The SHT_DYNAMIC output section if there is one.
568 Output_section
* dynamic_section_
;
569 // The dynamic data which goes into dynamic_section_.
570 Output_data_dynamic
* dynamic_data_
;
571 // The exception frame output section if there is one.
572 Output_section
* eh_frame_section_
;
573 // The exception frame data for eh_frame_section_.
574 Eh_frame
* eh_frame_data_
;
575 // The exception frame header output section if there is one.
576 Output_section
* eh_frame_hdr_section_
;
577 // A list of group sections and their signatures.
578 Group_signatures group_signatures_
;
579 // The size of the output file.
580 off_t output_file_size_
;
581 // Whether we have seen an object file marked to require an
583 bool input_requires_executable_stack_
;
584 // Whether we have seen at least one object file with an executable
586 bool input_with_gnu_stack_note_
;
587 // Whether we have seen at least one object file without an
588 // executable stack marker.
589 bool input_without_gnu_stack_note_
;
590 // Whether we have seen an object file that uses the static TLS model.
591 bool has_static_tls_
;
592 // Whether any sections require postprocessing.
593 bool any_postprocessing_sections_
;
596 // This task handles writing out data in output sections which is not
597 // part of an input section, or which requires special handling. When
598 // this is done, it unblocks both output_sections_blocker and
601 class Write_sections_task
: public Task
604 Write_sections_task(const Layout
* layout
, Output_file
* of
,
605 Task_token
* output_sections_blocker
,
606 Task_token
* final_blocker
)
607 : layout_(layout
), of_(of
),
608 output_sections_blocker_(output_sections_blocker
),
609 final_blocker_(final_blocker
)
612 // The standard Task methods.
625 { return "Write_sections_task"; }
628 class Write_sections_locker
;
630 const Layout
* layout_
;
632 Task_token
* output_sections_blocker_
;
633 Task_token
* final_blocker_
;
636 // This task handles writing out data which is not part of a section
639 class Write_data_task
: public Task
642 Write_data_task(const Layout
* layout
, const Symbol_table
* symtab
,
643 Output_file
* of
, Task_token
* final_blocker
)
644 : layout_(layout
), symtab_(symtab
), of_(of
), final_blocker_(final_blocker
)
647 // The standard Task methods.
660 { return "Write_data_task"; }
663 const Layout
* layout_
;
664 const Symbol_table
* symtab_
;
666 Task_token
* final_blocker_
;
669 // This task handles writing out the global symbols.
671 class Write_symbols_task
: public Task
674 Write_symbols_task(const Symbol_table
* symtab
,
675 const Input_objects
* input_objects
,
676 const Stringpool
* sympool
, const Stringpool
* dynpool
,
677 Output_file
* of
, Task_token
* final_blocker
)
678 : symtab_(symtab
), input_objects_(input_objects
), sympool_(sympool
),
679 dynpool_(dynpool
), of_(of
), final_blocker_(final_blocker
)
682 // The standard Task methods.
695 { return "Write_symbols_task"; }
698 const Symbol_table
* symtab_
;
699 const Input_objects
* input_objects_
;
700 const Stringpool
* sympool_
;
701 const Stringpool
* dynpool_
;
703 Task_token
* final_blocker_
;
706 // This task handles writing out data in output sections which can't
707 // be written out until all the input sections have been handled.
708 // This is for sections whose contents is based on the contents of
709 // other output sections.
711 class Write_after_input_sections_task
: public Task
714 Write_after_input_sections_task(Layout
* layout
, Output_file
* of
,
715 Task_token
* input_sections_blocker
,
716 Task_token
* final_blocker
)
717 : layout_(layout
), of_(of
),
718 input_sections_blocker_(input_sections_blocker
),
719 final_blocker_(final_blocker
)
722 // The standard Task methods.
735 { return "Write_after_input_sections_task"; }
740 Task_token
* input_sections_blocker_
;
741 Task_token
* final_blocker_
;
744 // This task function handles closing the file.
746 class Close_task_runner
: public Task_function_runner
749 Close_task_runner(const General_options
* options
, const Layout
* layout
,
751 : options_(options
), layout_(layout
), of_(of
)
754 // Run the operation.
756 run(Workqueue
*, const Task
*);
759 const General_options
* options_
;
760 const Layout
* layout_
;
764 // A small helper function to align an address.
767 align_address(uint64_t address
, uint64_t addralign
)
770 address
= (address
+ addralign
- 1) &~ (addralign
- 1);
774 } // End namespace gold.
776 #endif // !defined(GOLD_LAYOUT_H)