1 // layout.h -- lay out output file sections for gold -*- C++ -*-
3 // Copyright 2006, 2007 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.
31 #include "workqueue.h"
34 #include "stringpool.h"
39 class General_options
;
42 class Output_section_data
;
44 class Output_section_headers
;
47 class Output_data_dynamic
;
51 // This task function handles mapping the input sections to output
52 // sections and laying them out in memory.
54 class Layout_task_runner
: public Task_function_runner
57 // OPTIONS is the command line options, INPUT_OBJECTS is the list of
58 // input objects, SYMTAB is the symbol table, LAYOUT is the layout
60 Layout_task_runner(const General_options
& options
,
61 const Input_objects
* input_objects
,
64 : options_(options
), input_objects_(input_objects
), symtab_(symtab
),
73 Layout_task_runner(const Layout_task_runner
&);
74 Layout_task_runner
& operator=(const Layout_task_runner
&);
76 const General_options
& options_
;
77 const Input_objects
* input_objects_
;
78 Symbol_table
* symtab_
;
82 // This class handles the details of laying out input sections.
87 Layout(const General_options
& options
);
89 // Given an input section SHNDX, named NAME, with data in SHDR, from
90 // the object file OBJECT, return the output section where this
91 // input section should go. RELOC_SHNDX is the index of a
92 // relocation section which applies to this section, or 0 if none,
93 // or -1U if more than one. RELOC_TYPE is the type of the
94 // relocation section if there is one. Set *OFFSET to the offset
95 // within the output section.
96 template<int size
, bool big_endian
>
98 layout(Sized_relobj
<size
, big_endian
> *object
, unsigned int shndx
,
99 const char* name
, const elfcpp::Shdr
<size
, big_endian
>& shdr
,
100 unsigned int reloc_shndx
, unsigned int reloc_type
, off_t
* offset
);
102 // Like layout, only for exception frame sections. OBJECT is an
103 // object file. SYMBOLS is the contents of the symbol table
104 // section, with size SYMBOLS_SIZE. SYMBOL_NAMES is the contents of
105 // the symbol name section, with size SYMBOL_NAMES_SIZE. SHNDX is a
106 // .eh_frame section in OBJECT. SHDR is the section header.
107 // RELOC_SHNDX is the index of a relocation section which applies to
108 // this section, or 0 if none, or -1U if more than one. RELOC_TYPE
109 // is the type of the relocation section if there is one. This
110 // returns the output section, and sets *OFFSET to the offset.
111 template<int size
, bool big_endian
>
113 layout_eh_frame(Sized_relobj
<size
, big_endian
>* object
,
114 const unsigned char* symbols
,
116 const unsigned char* symbol_names
,
117 off_t symbol_names_size
,
119 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
120 unsigned int reloc_shndx
, unsigned int reloc_type
,
123 // Handle a GNU stack note. This is called once per input object
124 // file. SEEN_GNU_STACK is true if the object file has a
125 // .note.GNU-stack section. GNU_STACK_FLAGS is the section flags
126 // from that section if there was one.
128 layout_gnu_stack(bool seen_gnu_stack
, uint64_t gnu_stack_flags
);
130 // Add an Output_section_data to the layout. This is used for
131 // special sections like the GOT section.
133 add_output_section_data(const char* name
, elfcpp::Elf_Word type
,
134 elfcpp::Elf_Xword flags
,
135 Output_section_data
*);
137 // Create dynamic sections if necessary.
139 create_initial_dynamic_sections(const Input_objects
*, Symbol_table
*);
141 // Define __start and __stop symbols for output sections.
143 define_section_symbols(Symbol_table
*, const Target
*);
145 // Return the Stringpool used for symbol names.
148 { return &this->sympool_
; }
150 // Return the Stringpool used for dynamic symbol names and dynamic
154 { return &this->dynpool_
; }
156 // Return whether a section is a .gnu.linkonce section, given the
159 is_linkonce(const char* name
)
160 { return strncmp(name
, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
162 // Record the signature of a comdat section, and return whether to
163 // include it in the link. The GROUP parameter is true for a
164 // section group signature, false for a signature derived from a
165 // .gnu.linkonce section.
167 add_comdat(const char*, bool group
);
169 // Finalize the layout after all the input sections have been added.
171 finalize(const Input_objects
*, Symbol_table
*);
173 // Record that we have seen a relocation in the text section.
176 { this->have_textrel_
= true; }
178 // Return the size of the output file.
180 output_file_size() const
181 { return this->output_file_size_
; }
183 // Return the TLS segment. This will return NULL if there isn't
187 { return this->tls_segment_
; }
189 // Return the normal symbol table.
191 symtab_section() const
193 gold_assert(this->symtab_section_
!= NULL
);
194 return this->symtab_section_
;
197 // Return the dynamic symbol table.
199 dynsym_section() const
201 gold_assert(this->dynsym_section_
!= NULL
);
202 return this->dynsym_section_
;
205 // Return the dynamic tags.
208 { return this->dynamic_data_
; }
210 // Write out the output sections.
212 write_output_sections(Output_file
* of
) const;
214 // Write out data not associated with an input file or the symbol
217 write_data(const Symbol_table
*, Output_file
*) const;
219 // Write out output sections which can not be written until all the
220 // input sections are complete.
222 write_sections_after_input_sections(Output_file
* of
) const;
224 // Return an output section named NAME, or NULL if there is none.
226 find_output_section(const char* name
) const;
228 // Return an output segment of type TYPE, with segment flags SET set
229 // and segment flags CLEAR clear. Return NULL if there is none.
231 find_output_segment(elfcpp::PT type
, elfcpp::Elf_Word set
,
232 elfcpp::Elf_Word clear
) const;
234 // The list of segments.
236 typedef std::vector
<Output_segment
*> Segment_list
;
238 // The list of sections not attached to a segment.
240 typedef std::vector
<Output_section
*> Section_list
;
242 // The list of information to write out which is not attached to
243 // either a section or a segment.
244 typedef std::vector
<Output_data
*> Data_list
;
247 Layout(const Layout
&);
248 Layout
& operator=(const Layout
&);
250 // Mapping from .gnu.linkonce section names to output section names.
251 struct Linkonce_mapping
258 static const Linkonce_mapping linkonce_mapping
[];
259 static const int linkonce_mapping_count
;
261 // Create a .note section for gold.
265 // Record whether the stack must be executable.
267 create_executable_stack_info(const Target
*);
269 // Find the first read-only PT_LOAD segment, creating one if
272 find_first_load_seg();
274 // Create the output sections for the symbol table.
276 create_symtab_sections(const Input_objects
*, Symbol_table
*, off_t
*);
278 // Create the .shstrtab section.
282 // Create the section header table.
283 Output_section_headers
*
284 create_shdrs(off_t
*);
286 // Create the dynamic symbol table.
288 create_dynamic_symtab(const Target
*, Symbol_table
*, Output_section
** pdynstr
,
289 unsigned int* plocal_dynamic_count
,
290 std::vector
<Symbol
*>* pdynamic_symbols
,
293 // Finish the .dynamic section and PT_DYNAMIC segment.
295 finish_dynamic_section(const Input_objects
*, const Symbol_table
*);
297 // Create the .interp section and PT_INTERP segment.
299 create_interp(const Target
* target
);
301 // Create the version sections.
303 create_version_sections(const Versions
*,
305 unsigned int local_symcount
,
306 const std::vector
<Symbol
*>& dynamic_symbols
,
307 const Output_section
* dynstr
);
309 template<int size
, bool big_endian
>
311 sized_create_version_sections(const Versions
* versions
,
313 unsigned int local_symcount
,
314 const std::vector
<Symbol
*>& dynamic_symbols
,
315 const Output_section
* dynstr
318 // Return whether to include this section in the link.
319 template<int size
, bool big_endian
>
321 include_section(Sized_relobj
<size
, big_endian
>* object
, const char* name
,
322 const elfcpp::Shdr
<size
, big_endian
>&);
324 // Return the output section name to use given an input section
325 // name. Set *PLEN to the length of the name. *PLEN must be
326 // initialized to the length of NAME.
328 output_section_name(const char* name
, size_t* plen
);
330 // Return the output section name to use for a linkonce section
331 // name. PLEN is as for output_section_name.
333 linkonce_output_name(const char* name
, size_t* plen
);
335 // Return the output section for NAME, TYPE and FLAGS.
337 get_output_section(const char* name
, Stringpool::Key name_key
,
338 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
);
340 // Create a new Output_section.
342 make_output_section(const char* name
, elfcpp::Elf_Word type
,
343 elfcpp::Elf_Xword flags
);
345 // Set the final file offsets of all the segments.
347 set_segment_offsets(const Target
*, Output_segment
*, unsigned int* pshndx
);
349 // Set the final file offsets of all the sections not associated
352 set_section_offsets(off_t
, bool do_bits_sections
);
354 // Set the final section indexes of all the sections not associated
355 // with a segment. Returns the next unused index.
357 set_section_indexes(unsigned int pshndx
);
359 // Return whether SEG1 comes before SEG2 in the output file.
361 segment_precedes(const Output_segment
* seg1
, const Output_segment
* seg2
);
363 // Map from section flags to segment flags.
364 static elfcpp::Elf_Word
365 section_flags_to_segment(elfcpp::Elf_Xword flags
);
367 // A mapping used for group signatures.
368 typedef Unordered_map
<std::string
, bool> Signatures
;
370 // Mapping from input section name/type/flags to output section. We
371 // use canonicalized strings here.
373 typedef std::pair
<Stringpool::Key
,
374 std::pair
<elfcpp::Elf_Word
, elfcpp::Elf_Xword
> > Key
;
379 operator()(const Key
& k
) const;
382 typedef Unordered_map
<Key
, Output_section
*, Hash_key
> Section_name_map
;
384 // A comparison class for segments.
386 struct Compare_segments
389 operator()(const Output_segment
* seg1
, const Output_segment
* seg2
)
390 { return Layout::segment_precedes(seg1
, seg2
); }
393 // A reference to the options on the command line.
394 const General_options
& options_
;
395 // The output section names.
396 Stringpool namepool_
;
397 // The output symbol names.
399 // The dynamic strings, if needed.
401 // The list of group sections and linkonce sections which we have seen.
402 Signatures signatures_
;
403 // The mapping from input section name/type/flags to output sections.
404 Section_name_map section_name_map_
;
405 // The list of output segments.
406 Segment_list segment_list_
;
407 // The list of output sections.
408 Section_list section_list_
;
409 // The list of output sections which are not attached to any output
411 Section_list unattached_section_list_
;
412 // The list of unattached Output_data objects which require special
413 // handling because they are not Output_sections.
414 Data_list special_output_list_
;
415 // A pointer to the PT_TLS segment if there is one.
416 Output_segment
* tls_segment_
;
417 // The SHT_SYMTAB output section.
418 Output_section
* symtab_section_
;
419 // The SHT_DYNSYM output section if there is one.
420 Output_section
* dynsym_section_
;
421 // The SHT_DYNAMIC output section if there is one.
422 Output_section
* dynamic_section_
;
423 // The dynamic data which goes into dynamic_section_.
424 Output_data_dynamic
* dynamic_data_
;
425 // The exception frame output section if there is one.
426 Output_section
* eh_frame_section_
;
427 // The exception frame data for eh_frame_section_.
428 Eh_frame
* eh_frame_data_
;
429 // The exception frame header output section if there is one.
430 Output_section
* eh_frame_hdr_section_
;
431 // The size of the output file.
432 off_t output_file_size_
;
433 // Whether we have seen an object file marked to require an
435 bool input_requires_executable_stack_
;
436 // Whether we have seen at least one object file with an executable
438 bool input_with_gnu_stack_note_
;
439 // Whether we have seen at least one object file without an
440 // executable stack marker.
441 bool input_without_gnu_stack_note_
;
442 // Whether we have seen a relocation in the text section.
446 // This task handles writing out data in output sections which is not
447 // part of an input section, or which requires special handling. When
448 // this is done, it unblocks both output_sections_blocker and
451 class Write_sections_task
: public Task
454 Write_sections_task(const Layout
* layout
, Output_file
* of
,
455 Task_token
* output_sections_blocker
,
456 Task_token
* final_blocker
)
457 : layout_(layout
), of_(of
),
458 output_sections_blocker_(output_sections_blocker
),
459 final_blocker_(final_blocker
)
462 // The standard Task methods.
465 is_runnable(Workqueue
*);
474 class Write_sections_locker
;
476 const Layout
* layout_
;
478 Task_token
* output_sections_blocker_
;
479 Task_token
* final_blocker_
;
482 // This task handles writing out data which is not part of a section
485 class Write_data_task
: public Task
488 Write_data_task(const Layout
* layout
, const Symbol_table
* symtab
,
489 Output_file
* of
, Task_token
* final_blocker
)
490 : layout_(layout
), symtab_(symtab
), of_(of
), final_blocker_(final_blocker
)
493 // The standard Task methods.
496 is_runnable(Workqueue
*);
505 const Layout
* layout_
;
506 const Symbol_table
* symtab_
;
508 Task_token
* final_blocker_
;
511 // This task handles writing out the global symbols.
513 class Write_symbols_task
: public Task
516 Write_symbols_task(const Symbol_table
* symtab
,
517 const Input_objects
* input_objects
,
518 const Stringpool
* sympool
, const Stringpool
* dynpool
,
519 Output_file
* of
, Task_token
* final_blocker
)
520 : symtab_(symtab
), input_objects_(input_objects
), sympool_(sympool
),
521 dynpool_(dynpool
), of_(of
), final_blocker_(final_blocker
)
524 // The standard Task methods.
527 is_runnable(Workqueue
*);
536 const Symbol_table
* symtab_
;
537 const Input_objects
* input_objects_
;
538 const Stringpool
* sympool_
;
539 const Stringpool
* dynpool_
;
541 Task_token
* final_blocker_
;
544 // This task handles writing out data in output sections which can't
545 // be written out until all the input sections have been handled.
546 // This is for sections whose contents is based on the contents of
547 // other output sections.
549 class Write_after_input_sections_task
: public Task
552 Write_after_input_sections_task(const Layout
* layout
, Output_file
* of
,
553 Task_token
* input_sections_blocker
,
554 Task_token
* final_blocker
)
555 : layout_(layout
), of_(of
),
556 input_sections_blocker_(input_sections_blocker
),
557 final_blocker_(final_blocker
)
560 // The standard Task methods.
563 is_runnable(Workqueue
*);
572 class Write_sections_locker
;
574 const Layout
* layout_
;
576 Task_token
* input_sections_blocker_
;
577 Task_token
* final_blocker_
;
580 // This task function handles closing the file.
582 class Close_task_runner
: public Task_function_runner
585 Close_task_runner(Output_file
* of
)
589 // Run the operation.
597 // A small helper function to align an address.
600 align_address(uint64_t address
, uint64_t addralign
)
603 address
= (address
+ addralign
- 1) &~ (addralign
- 1);
607 } // End namespace gold.
609 #endif // !defined(GOLD_LAYOUT_H)