bfd/
[binutils.git] / gold / layout.h
blob88561258c03535ecb33dcf1e082f88ecbfda6042
1 // layout.h -- lay out output file sections for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009 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.
23 #ifndef GOLD_LAYOUT_H
24 #define GOLD_LAYOUT_H
26 #include <cstring>
27 #include <list>
28 #include <map>
29 #include <string>
30 #include <utility>
31 #include <vector>
33 #include "script.h"
34 #include "workqueue.h"
35 #include "object.h"
36 #include "dynobj.h"
37 #include "stringpool.h"
39 namespace gold
42 class General_options;
43 class Incremental_inputs;
44 class Input_objects;
45 class Mapfile;
46 class Symbol_table;
47 class Output_section_data;
48 class Output_section;
49 class Output_section_headers;
50 class Output_segment_headers;
51 class Output_file_header;
52 class Output_segment;
53 class Output_data;
54 class Output_data_dynamic;
55 class Output_symtab_xindex;
56 class Output_reduced_debug_abbrev_section;
57 class Output_reduced_debug_info_section;
58 class Eh_frame;
59 class Target;
61 // This task function handles mapping the input sections to output
62 // sections and laying them out in memory.
64 class Layout_task_runner : public Task_function_runner
66 public:
67 // OPTIONS is the command line options, INPUT_OBJECTS is the list of
68 // input objects, SYMTAB is the symbol table, LAYOUT is the layout
69 // object.
70 Layout_task_runner(const General_options& options,
71 const Input_objects* input_objects,
72 Symbol_table* symtab,
73 Target* target,
74 Layout* layout,
75 Mapfile* mapfile)
76 : options_(options), input_objects_(input_objects), symtab_(symtab),
77 target_(target), layout_(layout), mapfile_(mapfile)
78 { }
80 // Run the operation.
81 void
82 run(Workqueue*, const Task*);
84 private:
85 Layout_task_runner(const Layout_task_runner&);
86 Layout_task_runner& operator=(const Layout_task_runner&);
88 const General_options& options_;
89 const Input_objects* input_objects_;
90 Symbol_table* symtab_;
91 Target* target_;
92 Layout* layout_;
93 Mapfile* mapfile_;
96 // This class holds information about the comdat group or
97 // .gnu.linkonce section that will be kept for a given signature.
99 class Kept_section
101 private:
102 // For a comdat group, we build a mapping from the name of each
103 // section in the group to the section index and the size in object.
104 // When we discard a group in some other object file, we use this
105 // map to figure out which kept section the discarded section is
106 // associated with. We then use that mapping when processing relocs
107 // against discarded sections.
108 struct Comdat_section_info
110 // The section index.
111 unsigned int shndx;
112 // The section size.
113 uint64_t size;
115 Comdat_section_info(unsigned int a_shndx, uint64_t a_size)
116 : shndx(a_shndx), size(a_size)
120 // Most comdat groups have only one or two sections, so we use a
121 // std::map rather than an Unordered_map to optimize for that case
122 // without paying too heavily for groups with more sections.
123 typedef std::map<std::string, Comdat_section_info> Comdat_group;
125 public:
126 Kept_section()
127 : object_(NULL), shndx_(0), is_comdat_(false), is_group_name_(false)
128 { this->u_.linkonce_size = 0; }
130 // We need to support copies for the signature map in the Layout
131 // object, but we should never copy an object after it has been
132 // marked as a comdat section.
133 Kept_section(const Kept_section& k)
134 : object_(k.object_), shndx_(k.shndx_), is_comdat_(false),
135 is_group_name_(k.is_group_name_)
137 gold_assert(!k.is_comdat_);
138 this->u_.linkonce_size = 0;
141 ~Kept_section()
143 if (this->is_comdat_)
144 delete this->u_.group_sections;
147 // The object where this section lives.
148 Relobj*
149 object() const
150 { return this->object_; }
152 // Set the object.
153 void
154 set_object(Relobj* object)
156 gold_assert(this->object_ == NULL);
157 this->object_ = object;
160 // The section index.
161 unsigned int
162 shndx() const
163 { return this->shndx_; }
165 // Set the section index.
166 void
167 set_shndx(unsigned int shndx)
169 gold_assert(this->shndx_ == 0);
170 this->shndx_ = shndx;
173 // Whether this is a comdat group.
174 bool
175 is_comdat() const
176 { return this->is_comdat_; }
178 // Set that this is a comdat group.
179 void
180 set_is_comdat()
182 gold_assert(!this->is_comdat_);
183 this->is_comdat_ = true;
184 this->u_.group_sections = new Comdat_group();
187 // Whether this is associated with the name of a group or section
188 // rather than the symbol name derived from a linkonce section.
189 bool
190 is_group_name() const
191 { return this->is_group_name_; }
193 // Note that this represents a comdat group rather than a single
194 // linkonce section.
195 void
196 set_is_group_name()
197 { this->is_group_name_ = true; }
199 // Add a section to the group list.
200 void
201 add_comdat_section(const std::string& name, unsigned int shndx,
202 uint64_t size)
204 gold_assert(this->is_comdat_);
205 Comdat_section_info sinfo(shndx, size);
206 this->u_.group_sections->insert(std::make_pair(name, sinfo));
209 // Look for a section name in the group list, and return whether it
210 // was found. If found, returns the section index and size.
211 bool
212 find_comdat_section(const std::string& name, unsigned int *pshndx,
213 uint64_t *psize) const
215 gold_assert(this->is_comdat_);
216 Comdat_group::const_iterator p = this->u_.group_sections->find(name);
217 if (p == this->u_.group_sections->end())
218 return false;
219 *pshndx = p->second.shndx;
220 *psize = p->second.size;
221 return true;
224 // If there is only one section in the group list, return true, and
225 // return the section index and size.
226 bool
227 find_single_comdat_section(unsigned int *pshndx, uint64_t *psize) const
229 gold_assert(this->is_comdat_);
230 if (this->u_.group_sections->size() != 1)
231 return false;
232 Comdat_group::const_iterator p = this->u_.group_sections->begin();
233 *pshndx = p->second.shndx;
234 *psize = p->second.size;
235 return true;
238 // Return the size of a linkonce section.
239 uint64_t
240 linkonce_size() const
242 gold_assert(!this->is_comdat_);
243 return this->u_.linkonce_size;
246 // Set the size of a linkonce section.
247 void
248 set_linkonce_size(uint64_t size)
250 gold_assert(!this->is_comdat_);
251 this->u_.linkonce_size = size;
254 private:
255 // No assignment.
256 Kept_section& operator=(const Kept_section&);
258 // The object containing the comdat group or .gnu.linkonce section.
259 Relobj* object_;
260 // Index of the group section for comdats and the section itself for
261 // .gnu.linkonce.
262 unsigned int shndx_;
263 // True if this is for a comdat group rather than a .gnu.linkonce
264 // section.
265 bool is_comdat_;
266 // The Kept_sections are values of a mapping, that maps names to
267 // them. This field is true if this struct is associated with the
268 // name of a comdat or .gnu.linkonce, false if it is associated with
269 // the name of a symbol obtained from the .gnu.linkonce.* name
270 // through some heuristics.
271 bool is_group_name_;
272 union
274 // If the is_comdat_ field is true, this holds a map from names of
275 // the sections in the group to section indexes in object_ and to
276 // section sizes.
277 Comdat_group* group_sections;
278 // If the is_comdat_ field is false, this holds the size of the
279 // single section.
280 uint64_t linkonce_size;
281 } u_;
284 // This class handles the details of laying out input sections.
286 class Layout
288 public:
289 Layout(int number_of_input_files, Script_options*);
291 ~Layout()
293 delete this->relaxation_debug_check_;
294 delete this->segment_states_;
297 // Given an input section SHNDX, named NAME, with data in SHDR, from
298 // the object file OBJECT, return the output section where this
299 // input section should go. RELOC_SHNDX is the index of a
300 // relocation section which applies to this section, or 0 if none,
301 // or -1U if more than one. RELOC_TYPE is the type of the
302 // relocation section if there is one. Set *OFFSET to the offset
303 // within the output section.
304 template<int size, bool big_endian>
305 Output_section*
306 layout(Sized_relobj<size, big_endian> *object, unsigned int shndx,
307 const char* name, const elfcpp::Shdr<size, big_endian>& shdr,
308 unsigned int reloc_shndx, unsigned int reloc_type, off_t* offset);
310 // Layout an input reloc section when doing a relocatable link. The
311 // section is RELOC_SHNDX in OBJECT, with data in SHDR.
312 // DATA_SECTION is the reloc section to which it refers. RR is the
313 // relocatable information.
314 template<int size, bool big_endian>
315 Output_section*
316 layout_reloc(Sized_relobj<size, big_endian>* object,
317 unsigned int reloc_shndx,
318 const elfcpp::Shdr<size, big_endian>& shdr,
319 Output_section* data_section,
320 Relocatable_relocs* rr);
322 // Layout a group section when doing a relocatable link.
323 template<int size, bool big_endian>
324 void
325 layout_group(Symbol_table* symtab,
326 Sized_relobj<size, big_endian>* object,
327 unsigned int group_shndx,
328 const char* group_section_name,
329 const char* signature,
330 const elfcpp::Shdr<size, big_endian>& shdr,
331 elfcpp::Elf_Word flags,
332 std::vector<unsigned int>* shndxes);
334 // Like layout, only for exception frame sections. OBJECT is an
335 // object file. SYMBOLS is the contents of the symbol table
336 // section, with size SYMBOLS_SIZE. SYMBOL_NAMES is the contents of
337 // the symbol name section, with size SYMBOL_NAMES_SIZE. SHNDX is a
338 // .eh_frame section in OBJECT. SHDR is the section header.
339 // RELOC_SHNDX is the index of a relocation section which applies to
340 // this section, or 0 if none, or -1U if more than one. RELOC_TYPE
341 // is the type of the relocation section if there is one. This
342 // returns the output section, and sets *OFFSET to the offset.
343 template<int size, bool big_endian>
344 Output_section*
345 layout_eh_frame(Sized_relobj<size, big_endian>* object,
346 const unsigned char* symbols,
347 off_t symbols_size,
348 const unsigned char* symbol_names,
349 off_t symbol_names_size,
350 unsigned int shndx,
351 const elfcpp::Shdr<size, big_endian>& shdr,
352 unsigned int reloc_shndx, unsigned int reloc_type,
353 off_t* offset);
355 // Handle a GNU stack note. This is called once per input object
356 // file. SEEN_GNU_STACK is true if the object file has a
357 // .note.GNU-stack section. GNU_STACK_FLAGS is the section flags
358 // from that section if there was one.
359 void
360 layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags);
362 // Add an Output_section_data to the layout. This is used for
363 // special sections like the GOT section.
364 Output_section*
365 add_output_section_data(const char* name, elfcpp::Elf_Word type,
366 elfcpp::Elf_Xword flags,
367 Output_section_data*);
369 // Create dynamic sections if necessary.
370 void
371 create_initial_dynamic_sections(Symbol_table*);
373 // Define __start and __stop symbols for output sections.
374 void
375 define_section_symbols(Symbol_table*);
377 // Create automatic note sections.
378 void
379 create_notes();
381 // Create sections for linker scripts.
382 void
383 create_script_sections()
384 { this->script_options_->create_script_sections(this); }
386 // Define symbols from any linker script.
387 void
388 define_script_symbols(Symbol_table* symtab)
389 { this->script_options_->add_symbols_to_table(symtab); }
391 // Define symbols for group signatures.
392 void
393 define_group_signatures(Symbol_table*);
395 // Return the Stringpool used for symbol names.
396 const Stringpool*
397 sympool() const
398 { return &this->sympool_; }
400 // Return the Stringpool used for dynamic symbol names and dynamic
401 // tags.
402 const Stringpool*
403 dynpool() const
404 { return &this->dynpool_; }
406 // Return the symtab_xindex section used to hold large section
407 // indexes for the normal symbol table.
408 Output_symtab_xindex*
409 symtab_xindex() const
410 { return this->symtab_xindex_; }
412 // Return the dynsym_xindex section used to hold large section
413 // indexes for the dynamic symbol table.
414 Output_symtab_xindex*
415 dynsym_xindex() const
416 { return this->dynsym_xindex_; }
418 // Return whether a section is a .gnu.linkonce section, given the
419 // section name.
420 static inline bool
421 is_linkonce(const char* name)
422 { return strncmp(name, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
424 // Return true if a section is a debugging section.
425 static inline bool
426 is_debug_info_section(const char* name)
428 // Debugging sections can only be recognized by name.
429 return (strncmp(name, ".debug", sizeof(".debug") - 1) == 0
430 || strncmp(name, ".gnu.linkonce.wi.",
431 sizeof(".gnu.linkonce.wi.") - 1) == 0
432 || strncmp(name, ".line", sizeof(".line") - 1) == 0
433 || strncmp(name, ".stab", sizeof(".stab") - 1) == 0);
436 // Check if a comdat group or .gnu.linkonce section with the given
437 // NAME is selected for the link. If there is already a section,
438 // *KEPT_SECTION is set to point to the signature and the function
439 // returns false. Otherwise, OBJECT, SHNDX,IS_COMDAT, and
440 // IS_GROUP_NAME are recorded for this NAME in the layout object,
441 // *KEPT_SECTION is set to the internal copy and the function return
442 // false.
443 bool
444 find_or_add_kept_section(const std::string& name, Relobj* object,
445 unsigned int shndx, bool is_comdat,
446 bool is_group_name, Kept_section** kept_section);
448 // Finalize the layout after all the input sections have been added.
449 off_t
450 finalize(const Input_objects*, Symbol_table*, Target*, const Task*);
452 // Return whether any sections require postprocessing.
453 bool
454 any_postprocessing_sections() const
455 { return this->any_postprocessing_sections_; }
457 // Return the size of the output file.
458 off_t
459 output_file_size() const
460 { return this->output_file_size_; }
462 // Return the TLS segment. This will return NULL if there isn't
463 // one.
464 Output_segment*
465 tls_segment() const
466 { return this->tls_segment_; }
468 // Return the normal symbol table.
469 Output_section*
470 symtab_section() const
472 gold_assert(this->symtab_section_ != NULL);
473 return this->symtab_section_;
476 // Return the dynamic symbol table.
477 Output_section*
478 dynsym_section() const
480 gold_assert(this->dynsym_section_ != NULL);
481 return this->dynsym_section_;
484 // Return the dynamic tags.
485 Output_data_dynamic*
486 dynamic_data() const
487 { return this->dynamic_data_; }
489 // Write out the output sections.
490 void
491 write_output_sections(Output_file* of) const;
493 // Write out data not associated with an input file or the symbol
494 // table.
495 void
496 write_data(const Symbol_table*, Output_file*) const;
498 // Write out output sections which can not be written until all the
499 // input sections are complete.
500 void
501 write_sections_after_input_sections(Output_file* of);
503 // Return an output section named NAME, or NULL if there is none.
504 Output_section*
505 find_output_section(const char* name) const;
507 // Return an output segment of type TYPE, with segment flags SET set
508 // and segment flags CLEAR clear. Return NULL if there is none.
509 Output_segment*
510 find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
511 elfcpp::Elf_Word clear) const;
513 // Return the number of segments we expect to produce.
514 size_t
515 expected_segment_count() const;
517 // Set a flag to indicate that an object file uses the static TLS model.
518 void
519 set_has_static_tls()
520 { this->has_static_tls_ = true; }
522 // Return true if any object file uses the static TLS model.
523 bool
524 has_static_tls() const
525 { return this->has_static_tls_; }
527 // Return the options which may be set by a linker script.
528 Script_options*
529 script_options()
530 { return this->script_options_; }
532 const Script_options*
533 script_options() const
534 { return this->script_options_; }
536 // Return the object managing inputs in incremental build. NULL in
537 // non-incremental builds.
538 Incremental_inputs*
539 incremental_inputs()
540 { return this->incremental_inputs_; }
542 // Compute and write out the build ID if needed.
543 void
544 write_build_id(Output_file*) const;
546 // Rewrite output file in binary format.
547 void
548 write_binary(Output_file* in) const;
550 // Print output sections to the map file.
551 void
552 print_to_mapfile(Mapfile*) const;
554 // Dump statistical information to stderr.
555 void
556 print_stats() const;
558 // A list of segments.
560 typedef std::vector<Output_segment*> Segment_list;
562 // A list of sections.
564 typedef std::vector<Output_section*> Section_list;
566 // The list of information to write out which is not attached to
567 // either a section or a segment.
568 typedef std::vector<Output_data*> Data_list;
570 // Store the allocated sections into the section list. This is used
571 // by the linker script code.
572 void
573 get_allocated_sections(Section_list*) const;
575 // Make a section for a linker script to hold data.
576 Output_section*
577 make_output_section_for_script(const char* name);
579 // Make a segment. This is used by the linker script code.
580 Output_segment*
581 make_output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags);
583 // Return the number of segments.
584 size_t
585 segment_count() const
586 { return this->segment_list_.size(); }
588 // Map from section flags to segment flags.
589 static elfcpp::Elf_Word
590 section_flags_to_segment(elfcpp::Elf_Xword flags);
592 // Attach sections to segments.
593 void
594 attach_sections_to_segments();
596 // For relaxation clean up, we need to know output section data created
597 // from a linker script.
598 void
599 new_output_section_data_from_script(Output_section_data* posd)
601 if (this->record_output_section_data_from_script_)
602 this->script_output_section_data_list_.push_back(posd);
605 private:
606 Layout(const Layout&);
607 Layout& operator=(const Layout&);
609 // Mapping from input section names to output section names.
610 struct Section_name_mapping
612 const char* from;
613 int fromlen;
614 const char* to;
615 int tolen;
617 static const Section_name_mapping section_name_mapping[];
618 static const int section_name_mapping_count;
620 // During a relocatable link, a list of group sections and
621 // signatures.
622 struct Group_signature
624 // The group section.
625 Output_section* section;
626 // The signature.
627 const char* signature;
629 Group_signature()
630 : section(NULL), signature(NULL)
633 Group_signature(Output_section* sectiona, const char* signaturea)
634 : section(sectiona), signature(signaturea)
637 typedef std::vector<Group_signature> Group_signatures;
639 // Create a note section, filling in the header.
640 Output_section*
641 create_note(const char* name, int note_type, const char *section_name,
642 size_t descsz, bool allocate, size_t* trailing_padding);
644 // Create a note section for gold version.
645 void
646 create_gold_note();
648 // Record whether the stack must be executable.
649 void
650 create_executable_stack_info();
652 // Create a build ID note if needed.
653 void
654 create_build_id();
656 // Link .stab and .stabstr sections.
657 void
658 link_stabs_sections();
660 // Create .gnu_incremental_inputs and .gnu_incremental_strtab sections needed
661 // for the next run of incremental linking to check what has changed.
662 void
663 create_incremental_info_sections();
665 // Find the first read-only PT_LOAD segment, creating one if
666 // necessary.
667 Output_segment*
668 find_first_load_seg();
670 // Count the local symbols in the regular symbol table and the dynamic
671 // symbol table, and build the respective string pools.
672 void
673 count_local_symbols(const Task*, const Input_objects*);
675 // Create the output sections for the symbol table.
676 void
677 create_symtab_sections(const Input_objects*, Symbol_table*,
678 unsigned int, off_t*);
680 // Create the .shstrtab section.
681 Output_section*
682 create_shstrtab();
684 // Create the section header table.
685 void
686 create_shdrs(const Output_section* shstrtab_section, off_t*);
688 // Create the dynamic symbol table.
689 void
690 create_dynamic_symtab(const Input_objects*, Symbol_table*,
691 Output_section** pdynstr,
692 unsigned int* plocal_dynamic_count,
693 std::vector<Symbol*>* pdynamic_symbols,
694 Versions* versions);
696 // Assign offsets to each local portion of the dynamic symbol table.
697 void
698 assign_local_dynsym_offsets(const Input_objects*);
700 // Finish the .dynamic section and PT_DYNAMIC segment.
701 void
702 finish_dynamic_section(const Input_objects*, const Symbol_table*);
704 // Create the .interp section and PT_INTERP segment.
705 void
706 create_interp(const Target* target);
708 // Create the version sections.
709 void
710 create_version_sections(const Versions*,
711 const Symbol_table*,
712 unsigned int local_symcount,
713 const std::vector<Symbol*>& dynamic_symbols,
714 const Output_section* dynstr);
716 template<int size, bool big_endian>
717 void
718 sized_create_version_sections(const Versions* versions,
719 const Symbol_table*,
720 unsigned int local_symcount,
721 const std::vector<Symbol*>& dynamic_symbols,
722 const Output_section* dynstr);
724 // Return whether to include this section in the link.
725 template<int size, bool big_endian>
726 bool
727 include_section(Sized_relobj<size, big_endian>* object, const char* name,
728 const elfcpp::Shdr<size, big_endian>&);
730 // Return the output section name to use given an input section
731 // name. Set *PLEN to the length of the name. *PLEN must be
732 // initialized to the length of NAME.
733 static const char*
734 output_section_name(const char* name, size_t* plen);
736 // Return the number of allocated output sections.
737 size_t
738 allocated_output_section_count() const;
740 // Return the output section for NAME, TYPE and FLAGS.
741 Output_section*
742 get_output_section(const char* name, Stringpool::Key name_key,
743 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags);
745 // Choose the output section for NAME in RELOBJ.
746 Output_section*
747 choose_output_section(const Relobj* relobj, const char* name,
748 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
749 bool is_input_section);
751 // Create a new Output_section.
752 Output_section*
753 make_output_section(const char* name, elfcpp::Elf_Word type,
754 elfcpp::Elf_Xword flags);
756 // Attach a section to a segment.
757 void
758 attach_section_to_segment(Output_section*);
760 // Attach an allocated section to a segment.
761 void
762 attach_allocated_section_to_segment(Output_section*);
764 // Set the final file offsets of all the segments.
765 off_t
766 set_segment_offsets(const Target*, Output_segment*, unsigned int* pshndx);
768 // Set the file offsets of the sections when doing a relocatable
769 // link.
770 off_t
771 set_relocatable_section_offsets(Output_data*, unsigned int* pshndx);
773 // Set the final file offsets of all the sections not associated
774 // with a segment. We set section offsets in three passes: the
775 // first handles all allocated sections, the second sections that
776 // require postprocessing, and the last the late-bound STRTAB
777 // sections (probably only shstrtab, which is the one we care about
778 // because it holds section names).
779 enum Section_offset_pass
781 BEFORE_INPUT_SECTIONS_PASS,
782 POSTPROCESSING_SECTIONS_PASS,
783 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
785 off_t
786 set_section_offsets(off_t, Section_offset_pass pass);
788 // Set the final section indexes of all the sections not associated
789 // with a segment. Returns the next unused index.
790 unsigned int
791 set_section_indexes(unsigned int pshndx);
793 // Set the section addresses when using a script.
794 Output_segment*
795 set_section_addresses_from_script(Symbol_table*);
797 // Find appropriate places or orphan sections in a script.
798 void
799 place_orphan_sections_in_script();
801 // Return whether SEG1 comes before SEG2 in the output file.
802 static bool
803 segment_precedes(const Output_segment* seg1, const Output_segment* seg2);
805 // Use to save and restore segments during relaxation.
806 typedef Unordered_map<const Output_segment*, const Output_segment*>
807 Segment_states;
809 // Save states of current output segments.
810 void
811 save_segments(Segment_states*);
813 // Restore output segment states.
814 void
815 restore_segments(const Segment_states*);
817 // Clean up after relaxation so that it is possible to lay out the
818 // sections and segments again.
819 void
820 clean_up_after_relaxation();
822 // Doing preparation work for relaxation. This is factored out to make
823 // Layout::finalized a bit smaller and easier to read.
824 void
825 prepare_for_relaxation();
827 // Main body of the relaxation loop, which lays out the section.
828 off_t
829 relaxation_loop_body(int, Target*, Symbol_table*, Output_segment**,
830 Output_segment*, Output_segment_headers*,
831 Output_file_header*, unsigned int*);
833 // A mapping used for kept comdats/.gnu.linkonce group signatures.
834 typedef Unordered_map<std::string, Kept_section> Signatures;
836 // Mapping from input section name/type/flags to output section. We
837 // use canonicalized strings here.
839 typedef std::pair<Stringpool::Key,
840 std::pair<elfcpp::Elf_Word, elfcpp::Elf_Xword> > Key;
842 struct Hash_key
844 size_t
845 operator()(const Key& k) const;
848 typedef Unordered_map<Key, Output_section*, Hash_key> Section_name_map;
850 // A comparison class for segments.
852 struct Compare_segments
854 bool
855 operator()(const Output_segment* seg1, const Output_segment* seg2)
856 { return Layout::segment_precedes(seg1, seg2); }
859 typedef std::vector<Output_section_data*> Output_section_data_list;
861 // Debug checker class.
862 class Relaxation_debug_check
864 public:
865 Relaxation_debug_check()
866 : section_infos_()
869 // Check that sections and special data are in reset states.
870 void
871 check_output_data_for_reset_values(const Layout::Section_list&,
872 const Layout::Data_list&);
874 // Record information of a section list.
875 void
876 read_sections(const Layout::Section_list&);
878 // Verify a section list with recorded information.
879 void
880 verify_sections(const Layout::Section_list&);
882 private:
883 // Information we care about a section.
884 struct Section_info
886 // Output section described by this.
887 Output_section* output_section;
888 // Load address.
889 uint64_t address;
890 // Data size.
891 off_t data_size;
892 // File offset.
893 off_t offset;
896 // Section information.
897 std::vector<Section_info> section_infos_;
900 // The number of input files, for sizing tables.
901 int number_of_input_files_;
902 // Information set by scripts or by command line options.
903 Script_options* script_options_;
904 // The output section names.
905 Stringpool namepool_;
906 // The output symbol names.
907 Stringpool sympool_;
908 // The dynamic strings, if needed.
909 Stringpool dynpool_;
910 // The list of group sections and linkonce sections which we have seen.
911 Signatures signatures_;
912 // The mapping from input section name/type/flags to output sections.
913 Section_name_map section_name_map_;
914 // The list of output segments.
915 Segment_list segment_list_;
916 // The list of output sections.
917 Section_list section_list_;
918 // The list of output sections which are not attached to any output
919 // segment.
920 Section_list unattached_section_list_;
921 // The list of unattached Output_data objects which require special
922 // handling because they are not Output_sections.
923 Data_list special_output_list_;
924 // The section headers.
925 Output_section_headers* section_headers_;
926 // A pointer to the PT_TLS segment if there is one.
927 Output_segment* tls_segment_;
928 // A pointer to the PT_GNU_RELRO segment if there is one.
929 Output_segment* relro_segment_;
930 // The SHT_SYMTAB output section.
931 Output_section* symtab_section_;
932 // The SHT_SYMTAB_SHNDX for the regular symbol table if there is one.
933 Output_symtab_xindex* symtab_xindex_;
934 // The SHT_DYNSYM output section if there is one.
935 Output_section* dynsym_section_;
936 // The SHT_SYMTAB_SHNDX for the dynamic symbol table if there is one.
937 Output_symtab_xindex* dynsym_xindex_;
938 // The SHT_DYNAMIC output section if there is one.
939 Output_section* dynamic_section_;
940 // The dynamic data which goes into dynamic_section_.
941 Output_data_dynamic* dynamic_data_;
942 // The exception frame output section if there is one.
943 Output_section* eh_frame_section_;
944 // The exception frame data for eh_frame_section_.
945 Eh_frame* eh_frame_data_;
946 // Whether we have added eh_frame_data_ to the .eh_frame section.
947 bool added_eh_frame_data_;
948 // The exception frame header output section if there is one.
949 Output_section* eh_frame_hdr_section_;
950 // The space for the build ID checksum if there is one.
951 Output_section_data* build_id_note_;
952 // The output section containing dwarf abbreviations
953 Output_reduced_debug_abbrev_section* debug_abbrev_;
954 // The output section containing the dwarf debug info tree
955 Output_reduced_debug_info_section* debug_info_;
956 // A list of group sections and their signatures.
957 Group_signatures group_signatures_;
958 // The size of the output file.
959 off_t output_file_size_;
960 // Whether we have attached the sections to the segments.
961 bool sections_are_attached_;
962 // Whether we have seen an object file marked to require an
963 // executable stack.
964 bool input_requires_executable_stack_;
965 // Whether we have seen at least one object file with an executable
966 // stack marker.
967 bool input_with_gnu_stack_note_;
968 // Whether we have seen at least one object file without an
969 // executable stack marker.
970 bool input_without_gnu_stack_note_;
971 // Whether we have seen an object file that uses the static TLS model.
972 bool has_static_tls_;
973 // Whether any sections require postprocessing.
974 bool any_postprocessing_sections_;
975 // Whether we have resized the signatures_ hash table.
976 bool resized_signatures_;
977 // Whether we have created a .stab*str output section.
978 bool have_stabstr_section_;
979 // In incremental build, holds information check the inputs and build the
980 // .gnu_incremental_inputs section.
981 Incremental_inputs* incremental_inputs_;
982 // Whether we record output section data created in script
983 bool record_output_section_data_from_script_;
984 // List of output data that needs to be removed at relexation clean up.
985 Output_section_data_list script_output_section_data_list_;
986 // Structure to save segment states before entering the relaxation loop.
987 Segment_states* segment_states_;
988 // A relaxation debug checker. We only create one when in debugging mode.
989 Relaxation_debug_check* relaxation_debug_check_;
992 // This task handles writing out data in output sections which is not
993 // part of an input section, or which requires special handling. When
994 // this is done, it unblocks both output_sections_blocker and
995 // final_blocker.
997 class Write_sections_task : public Task
999 public:
1000 Write_sections_task(const Layout* layout, Output_file* of,
1001 Task_token* output_sections_blocker,
1002 Task_token* final_blocker)
1003 : layout_(layout), of_(of),
1004 output_sections_blocker_(output_sections_blocker),
1005 final_blocker_(final_blocker)
1008 // The standard Task methods.
1010 Task_token*
1011 is_runnable();
1013 void
1014 locks(Task_locker*);
1016 void
1017 run(Workqueue*);
1019 std::string
1020 get_name() const
1021 { return "Write_sections_task"; }
1023 private:
1024 class Write_sections_locker;
1026 const Layout* layout_;
1027 Output_file* of_;
1028 Task_token* output_sections_blocker_;
1029 Task_token* final_blocker_;
1032 // This task handles writing out data which is not part of a section
1033 // or segment.
1035 class Write_data_task : public Task
1037 public:
1038 Write_data_task(const Layout* layout, const Symbol_table* symtab,
1039 Output_file* of, Task_token* final_blocker)
1040 : layout_(layout), symtab_(symtab), of_(of), final_blocker_(final_blocker)
1043 // The standard Task methods.
1045 Task_token*
1046 is_runnable();
1048 void
1049 locks(Task_locker*);
1051 void
1052 run(Workqueue*);
1054 std::string
1055 get_name() const
1056 { return "Write_data_task"; }
1058 private:
1059 const Layout* layout_;
1060 const Symbol_table* symtab_;
1061 Output_file* of_;
1062 Task_token* final_blocker_;
1065 // This task handles writing out the global symbols.
1067 class Write_symbols_task : public Task
1069 public:
1070 Write_symbols_task(const Layout* layout, const Symbol_table* symtab,
1071 const Input_objects* input_objects,
1072 const Stringpool* sympool, const Stringpool* dynpool,
1073 Output_file* of, Task_token* final_blocker)
1074 : layout_(layout), symtab_(symtab), input_objects_(input_objects),
1075 sympool_(sympool), dynpool_(dynpool), of_(of),
1076 final_blocker_(final_blocker)
1079 // The standard Task methods.
1081 Task_token*
1082 is_runnable();
1084 void
1085 locks(Task_locker*);
1087 void
1088 run(Workqueue*);
1090 std::string
1091 get_name() const
1092 { return "Write_symbols_task"; }
1094 private:
1095 const Layout* layout_;
1096 const Symbol_table* symtab_;
1097 const Input_objects* input_objects_;
1098 const Stringpool* sympool_;
1099 const Stringpool* dynpool_;
1100 Output_file* of_;
1101 Task_token* final_blocker_;
1104 // This task handles writing out data in output sections which can't
1105 // be written out until all the input sections have been handled.
1106 // This is for sections whose contents is based on the contents of
1107 // other output sections.
1109 class Write_after_input_sections_task : public Task
1111 public:
1112 Write_after_input_sections_task(Layout* layout, Output_file* of,
1113 Task_token* input_sections_blocker,
1114 Task_token* final_blocker)
1115 : layout_(layout), of_(of),
1116 input_sections_blocker_(input_sections_blocker),
1117 final_blocker_(final_blocker)
1120 // The standard Task methods.
1122 Task_token*
1123 is_runnable();
1125 void
1126 locks(Task_locker*);
1128 void
1129 run(Workqueue*);
1131 std::string
1132 get_name() const
1133 { return "Write_after_input_sections_task"; }
1135 private:
1136 Layout* layout_;
1137 Output_file* of_;
1138 Task_token* input_sections_blocker_;
1139 Task_token* final_blocker_;
1142 // This task function handles closing the file.
1144 class Close_task_runner : public Task_function_runner
1146 public:
1147 Close_task_runner(const General_options* options, const Layout* layout,
1148 Output_file* of)
1149 : options_(options), layout_(layout), of_(of)
1152 // Run the operation.
1153 void
1154 run(Workqueue*, const Task*);
1156 private:
1157 const General_options* options_;
1158 const Layout* layout_;
1159 Output_file* of_;
1162 // A small helper function to align an address.
1164 inline uint64_t
1165 align_address(uint64_t address, uint64_t addralign)
1167 if (addralign != 0)
1168 address = (address + addralign - 1) &~ (addralign - 1);
1169 return address;
1172 } // End namespace gold.
1174 #endif // !defined(GOLD_LAYOUT_H)