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,
65 : options_(options), input_objects_(input_objects), symtab_(symtab),
71 run(Workqueue*, const Task*);
74 Layout_task_runner(const Layout_task_runner&);
75 Layout_task_runner& operator=(const Layout_task_runner&);
77 const General_options& options_;
78 const Input_objects* input_objects_;
79 Symbol_table* symtab_;
83 // This class handles the details of laying out input sections.
88 Layout(const General_options& options, Script_options*);
90 // Given an input section SHNDX, named NAME, with data in SHDR, from
91 // the object file OBJECT, return the output section where this
92 // input section should go. RELOC_SHNDX is the index of a
93 // relocation section which applies to this section, or 0 if none,
94 // or -1U if more than one. RELOC_TYPE is the type of the
95 // relocation section if there is one. Set *OFFSET to the offset
96 // within the output section.
97 template<int size, bool big_endian>
99 layout(Sized_relobj<size, big_endian> *object, unsigned int shndx,
100 const char* name, const elfcpp::Shdr<size, big_endian>& shdr,
101 unsigned int reloc_shndx, unsigned int reloc_type, off_t* offset);
103 // Like layout, only for exception frame sections. OBJECT is an
104 // object file. SYMBOLS is the contents of the symbol table
105 // section, with size SYMBOLS_SIZE. SYMBOL_NAMES is the contents of
106 // the symbol name section, with size SYMBOL_NAMES_SIZE. SHNDX is a
107 // .eh_frame section in OBJECT. SHDR is the section header.
108 // RELOC_SHNDX is the index of a relocation section which applies to
109 // this section, or 0 if none, or -1U if more than one. RELOC_TYPE
110 // is the type of the relocation section if there is one. This
111 // returns the output section, and sets *OFFSET to the offset.
112 template<int size, bool big_endian>
114 layout_eh_frame(Sized_relobj<size, big_endian>* object,
115 const unsigned char* symbols,
117 const unsigned char* symbol_names,
118 off_t symbol_names_size,
120 const elfcpp::Shdr<size, big_endian>& shdr,
121 unsigned int reloc_shndx, unsigned int reloc_type,
124 // Handle a GNU stack note. This is called once per input object
125 // file. SEEN_GNU_STACK is true if the object file has a
126 // .note.GNU-stack section. GNU_STACK_FLAGS is the section flags
127 // from that section if there was one.
129 layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags);
131 // Add an Output_section_data to the layout. This is used for
132 // special sections like the GOT section.
134 add_output_section_data(const char* name, elfcpp::Elf_Word type,
135 elfcpp::Elf_Xword flags,
136 Output_section_data*);
138 // Create dynamic sections if necessary.
140 create_initial_dynamic_sections(const Input_objects*, Symbol_table*);
142 // Define __start and __stop symbols for output sections.
144 define_section_symbols(Symbol_table*, const Target*);
146 // Define symbols from any linker script.
148 define_script_symbols(Symbol_table* symtab, const Target* target)
149 { this->script_options_->add_symbols_to_table(symtab, target); }
151 // Return the Stringpool used for symbol names.
154 { return &this->sympool_; }
156 // Return the Stringpool used for dynamic symbol names and dynamic
160 { return &this->dynpool_; }
162 // Return whether a section is a .gnu.linkonce section, given the
165 is_linkonce(const char* name)
166 { return strncmp(name, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
168 // Record the signature of a comdat section, and return whether to
169 // include it in the link. The GROUP parameter is true for a
170 // section group signature, false for a signature derived from a
171 // .gnu.linkonce section.
173 add_comdat(const char*, bool group);
175 // Finalize the layout after all the input sections have been added.
177 finalize(const Input_objects*, Symbol_table*, const Task*);
179 // Return whether any sections require postprocessing.
181 any_postprocessing_sections() const
182 { return this->any_postprocessing_sections_; }
184 // Return the size of the output file.
186 output_file_size() const
187 { return this->output_file_size_; }
189 // Return the TLS segment. This will return NULL if there isn't
193 { return this->tls_segment_; }
195 // Return the normal symbol table.
197 symtab_section() const
199 gold_assert(this->symtab_section_ != NULL);
200 return this->symtab_section_;
203 // Return the dynamic symbol table.
205 dynsym_section() const
207 gold_assert(this->dynsym_section_ != NULL);
208 return this->dynsym_section_;
211 // Return the dynamic tags.
214 { return this->dynamic_data_; }
216 // Write out the output sections.
218 write_output_sections(Output_file* of) const;
220 // Write out data not associated with an input file or the symbol
223 write_data(const Symbol_table*, Output_file*) const;
225 // Write out output sections which can not be written until all the
226 // input sections are complete.
228 write_sections_after_input_sections(Output_file* of);
230 // Return an output section named NAME, or NULL if there is none.
232 find_output_section(const char* name) const;
234 // Return an output segment of type TYPE, with segment flags SET set
235 // and segment flags CLEAR clear. Return NULL if there is none.
237 find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
238 elfcpp::Elf_Word clear) const;
240 // Set a flag to indicate that an object file uses the static TLS model.
243 { this->has_static_tls_ = true; }
245 // Return true if any object file uses the static TLS model.
247 has_static_tls() const
248 { return this->has_static_tls_; }
250 // Return the options which may be set by a linker script.
253 { return this->script_options_; }
255 const Script_options*
256 script_options() const
257 { return this->script_options_; }
259 // Dump statistical information to stderr.
263 // The list of segments.
265 typedef std::vector<Output_segment*> Segment_list;
267 // The list of sections not attached to a segment.
269 typedef std::vector<Output_section*> Section_list;
271 // The list of information to write out which is not attached to
272 // either a section or a segment.
273 typedef std::vector<Output_data*> Data_list;
276 Layout(const Layout&);
277 Layout& operator=(const Layout&);
279 // Mapping from .gnu.linkonce section names to output section names.
280 struct Linkonce_mapping
287 static const Linkonce_mapping linkonce_mapping[];
288 static const int linkonce_mapping_count;
290 // Create a .note section for gold.
294 // Record whether the stack must be executable.
296 create_executable_stack_info(const Target*);
298 // Find the first read-only PT_LOAD segment, creating one if
301 find_first_load_seg();
303 // Count the local symbols in the regular symbol table and the dynamic
304 // symbol table, and build the respective string pools.
306 count_local_symbols(const Task*, const Input_objects*);
308 // Create the output sections for the symbol table.
310 create_symtab_sections(const Input_objects*, Symbol_table*, off_t*);
312 // Create the .shstrtab section.
316 // Create the section header table.
318 create_shdrs(off_t*);
320 // Create the dynamic symbol table.
322 create_dynamic_symtab(const Input_objects*, const Target*,
323 Symbol_table*, Output_section** pdynstr,
324 unsigned int* plocal_dynamic_count,
325 std::vector<Symbol*>* pdynamic_symbols,
328 // Assign offsets to each local portion of the dynamic symbol table.
330 assign_local_dynsym_offsets(const Input_objects*);
332 // Finish the .dynamic section and PT_DYNAMIC segment.
334 finish_dynamic_section(const Input_objects*, const Symbol_table*);
336 // Create the .interp section and PT_INTERP segment.
338 create_interp(const Target* target);
340 // Create the version sections.
342 create_version_sections(const Versions*,
344 unsigned int local_symcount,
345 const std::vector<Symbol*>& dynamic_symbols,
346 const Output_section* dynstr);
348 template<int size, bool big_endian>
350 sized_create_version_sections(const Versions* versions,
352 unsigned int local_symcount,
353 const std::vector<Symbol*>& dynamic_symbols,
354 const Output_section* dynstr
357 // Return whether to include this section in the link.
358 template<int size, bool big_endian>
360 include_section(Sized_relobj<size, big_endian>* object, const char* name,
361 const elfcpp::Shdr<size, big_endian>&);
363 // Return the output section name to use given an input section
364 // name. Set *PLEN to the length of the name. *PLEN must be
365 // initialized to the length of NAME.
367 output_section_name(const char* name, size_t* plen);
369 // Return the output section name to use for a linkonce section
370 // name. PLEN is as for output_section_name.
372 linkonce_output_name(const char* name, size_t* plen);
374 // Return the output section for NAME, TYPE and FLAGS.
376 get_output_section(const char* name, Stringpool::Key name_key,
377 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags);
379 // Create a new Output_section.
381 make_output_section(const char* name, elfcpp::Elf_Word type,
382 elfcpp::Elf_Xword flags);
384 // Set the final file offsets of all the segments.
386 set_segment_offsets(const Target*, Output_segment*, unsigned int* pshndx);
388 // Set the final file offsets of all the sections not associated
389 // with a segment. We set section offsets in three passes: the
390 // first handles all allocated sections, the second sections that
391 // require postprocessing, and the last the late-bound STRTAB
392 // sections (probably only shstrtab, which is the one we care about
393 // because it holds section names).
394 enum Section_offset_pass
396 BEFORE_INPUT_SECTIONS_PASS,
397 POSTPROCESSING_SECTIONS_PASS,
398 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
401 set_section_offsets(off_t, Section_offset_pass pass);
403 // Set the final section indexes of all the sections not associated
404 // with a segment. Returns the next unused index.
406 set_section_indexes(unsigned int pshndx);
408 // Return whether SEG1 comes before SEG2 in the output file.
410 segment_precedes(const Output_segment* seg1, const Output_segment* seg2);
412 // Map from section flags to segment flags.
413 static elfcpp::Elf_Word
414 section_flags_to_segment(elfcpp::Elf_Xword flags);
416 // A mapping used for group signatures.
417 typedef Unordered_map<std::string, bool> Signatures;
419 // Mapping from input section name/type/flags to output section. We
420 // use canonicalized strings here.
422 typedef std::pair<Stringpool::Key,
423 std::pair<elfcpp::Elf_Word, elfcpp::Elf_Xword> > Key;
428 operator()(const Key& k) const;
431 typedef Unordered_map<Key, Output_section*, Hash_key> Section_name_map;
433 // A comparison class for segments.
435 struct Compare_segments
438 operator()(const Output_segment* seg1, const Output_segment* seg2)
439 { return Layout::segment_precedes(seg1, seg2); }
442 // A reference to the options on the command line.
443 const General_options& options_;
444 // Information set by scripts or by command line options.
445 Script_options* script_options_;
446 // The output section names.
447 Stringpool namepool_;
448 // The output symbol names.
450 // The dynamic strings, if needed.
452 // The list of group sections and linkonce sections which we have seen.
453 Signatures signatures_;
454 // The mapping from input section name/type/flags to output sections.
455 Section_name_map section_name_map_;
456 // The list of output segments.
457 Segment_list segment_list_;
458 // The list of output sections.
459 Section_list section_list_;
460 // The list of output sections which are not attached to any output
462 Section_list unattached_section_list_;
463 // The list of unattached Output_data objects which require special
464 // handling because they are not Output_sections.
465 Data_list special_output_list_;
466 // The section headers.
467 Output_section_headers* section_headers_;
468 // A pointer to the PT_TLS segment if there is one.
469 Output_segment* tls_segment_;
470 // The SHT_SYMTAB output section.
471 Output_section* symtab_section_;
472 // The SHT_DYNSYM output section if there is one.
473 Output_section* dynsym_section_;
474 // The SHT_DYNAMIC output section if there is one.
475 Output_section* dynamic_section_;
476 // The dynamic data which goes into dynamic_section_.
477 Output_data_dynamic* dynamic_data_;
478 // The exception frame output section if there is one.
479 Output_section* eh_frame_section_;
480 // The exception frame data for eh_frame_section_.
481 Eh_frame* eh_frame_data_;
482 // The exception frame header output section if there is one.
483 Output_section* eh_frame_hdr_section_;
484 // The size of the output file.
485 off_t output_file_size_;
486 // Whether we have seen an object file marked to require an
488 bool input_requires_executable_stack_;
489 // Whether we have seen at least one object file with an executable
491 bool input_with_gnu_stack_note_;
492 // Whether we have seen at least one object file without an
493 // executable stack marker.
494 bool input_without_gnu_stack_note_;
495 // Whether we have seen an object file that uses the static TLS model.
496 bool has_static_tls_;
497 // Whether any sections require postprocessing.
498 bool any_postprocessing_sections_;
501 // This task handles writing out data in output sections which is not
502 // part of an input section, or which requires special handling. When
503 // this is done, it unblocks both output_sections_blocker and
506 class Write_sections_task : public Task
509 Write_sections_task(const Layout* layout, Output_file* of,
510 Task_token* output_sections_blocker,
511 Task_token* final_blocker)
512 : layout_(layout), of_(of),
513 output_sections_blocker_(output_sections_blocker),
514 final_blocker_(final_blocker)
517 // The standard Task methods.
530 { return "Write_sections_task"; }
533 class Write_sections_locker;
535 const Layout* layout_;
537 Task_token* output_sections_blocker_;
538 Task_token* final_blocker_;
541 // This task handles writing out data which is not part of a section
544 class Write_data_task : public Task
547 Write_data_task(const Layout* layout, const Symbol_table* symtab,
548 Output_file* of, Task_token* final_blocker)
549 : layout_(layout), symtab_(symtab), of_(of), final_blocker_(final_blocker)
552 // The standard Task methods.
565 { return "Write_data_task"; }
568 const Layout* layout_;
569 const Symbol_table* symtab_;
571 Task_token* final_blocker_;
574 // This task handles writing out the global symbols.
576 class Write_symbols_task : public Task
579 Write_symbols_task(const Symbol_table* symtab,
580 const Input_objects* input_objects,
581 const Stringpool* sympool, const Stringpool* dynpool,
582 Output_file* of, Task_token* final_blocker)
583 : symtab_(symtab), input_objects_(input_objects), sympool_(sympool),
584 dynpool_(dynpool), of_(of), final_blocker_(final_blocker)
587 // The standard Task methods.
600 { return "Write_symbols_task"; }
603 const Symbol_table* symtab_;
604 const Input_objects* input_objects_;
605 const Stringpool* sympool_;
606 const Stringpool* dynpool_;
608 Task_token* final_blocker_;
611 // This task handles writing out data in output sections which can't
612 // be written out until all the input sections have been handled.
613 // This is for sections whose contents is based on the contents of
614 // other output sections.
616 class Write_after_input_sections_task : public Task
619 Write_after_input_sections_task(Layout* layout, Output_file* of,
620 Task_token* input_sections_blocker,
621 Task_token* final_blocker)
622 : layout_(layout), of_(of),
623 input_sections_blocker_(input_sections_blocker),
624 final_blocker_(final_blocker)
627 // The standard Task methods.
640 { return "Write_after_input_sections_task"; }
645 Task_token* input_sections_blocker_;
646 Task_token* final_blocker_;
649 // This task function handles closing the file.
651 class Close_task_runner : public Task_function_runner
654 Close_task_runner(Output_file* of)
658 // Run the operation.
660 run(Workqueue*, const Task*);
666 // A small helper function to align an address.
669 align_address(uint64_t address, uint64_t addralign)
672 address = (address + addralign - 1) &~ (addralign - 1);
676 } // End namespace gold.
678 #endif // !defined(GOLD_LAYOUT_H)