* layout.cc (Layout::Layout): Initialize increase_relro_.
[deliverable/binutils-gdb.git] / gold / layout.h
1 // layout.h -- lay out output file sections for gold -*- C++ -*-
2
3 // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
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.
12
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.
17
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.
22
23 #ifndef GOLD_LAYOUT_H
24 #define GOLD_LAYOUT_H
25
26 #include <cstring>
27 #include <list>
28 #include <map>
29 #include <string>
30 #include <utility>
31 #include <vector>
32
33 #include "script.h"
34 #include "workqueue.h"
35 #include "object.h"
36 #include "dynobj.h"
37 #include "stringpool.h"
38
39 namespace gold
40 {
41
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;
60
61 // This task function handles mapping the input sections to output
62 // sections and laying them out in memory.
63
64 class Layout_task_runner : public Task_function_runner
65 {
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 { }
79
80 // Run the operation.
81 void
82 run(Workqueue*, const Task*);
83
84 private:
85 Layout_task_runner(const Layout_task_runner&);
86 Layout_task_runner& operator=(const Layout_task_runner&);
87
88 const General_options& options_;
89 const Input_objects* input_objects_;
90 Symbol_table* symtab_;
91 Target* target_;
92 Layout* layout_;
93 Mapfile* mapfile_;
94 };
95
96 // This class holds information about the comdat group or
97 // .gnu.linkonce section that will be kept for a given signature.
98
99 class Kept_section
100 {
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
109 {
110 // The section index.
111 unsigned int shndx;
112 // The section size.
113 uint64_t size;
114
115 Comdat_section_info(unsigned int a_shndx, uint64_t a_size)
116 : shndx(a_shndx), size(a_size)
117 { }
118 };
119
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;
124
125 public:
126 Kept_section()
127 : object_(NULL), shndx_(0), is_comdat_(false), is_group_name_(false)
128 { this->u_.linkonce_size = 0; }
129
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_)
136 {
137 gold_assert(!k.is_comdat_);
138 this->u_.linkonce_size = 0;
139 }
140
141 ~Kept_section()
142 {
143 if (this->is_comdat_)
144 delete this->u_.group_sections;
145 }
146
147 // The object where this section lives.
148 Relobj*
149 object() const
150 { return this->object_; }
151
152 // Set the object.
153 void
154 set_object(Relobj* object)
155 {
156 gold_assert(this->object_ == NULL);
157 this->object_ = object;
158 }
159
160 // The section index.
161 unsigned int
162 shndx() const
163 { return this->shndx_; }
164
165 // Set the section index.
166 void
167 set_shndx(unsigned int shndx)
168 {
169 gold_assert(this->shndx_ == 0);
170 this->shndx_ = shndx;
171 }
172
173 // Whether this is a comdat group.
174 bool
175 is_comdat() const
176 { return this->is_comdat_; }
177
178 // Set that this is a comdat group.
179 void
180 set_is_comdat()
181 {
182 gold_assert(!this->is_comdat_);
183 this->is_comdat_ = true;
184 this->u_.group_sections = new Comdat_group();
185 }
186
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_; }
192
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; }
198
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)
203 {
204 gold_assert(this->is_comdat_);
205 Comdat_section_info sinfo(shndx, size);
206 this->u_.group_sections->insert(std::make_pair(name, sinfo));
207 }
208
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
214 {
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;
222 }
223
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
228 {
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;
236 }
237
238 // Return the size of a linkonce section.
239 uint64_t
240 linkonce_size() const
241 {
242 gold_assert(!this->is_comdat_);
243 return this->u_.linkonce_size;
244 }
245
246 // Set the size of a linkonce section.
247 void
248 set_linkonce_size(uint64_t size)
249 {
250 gold_assert(!this->is_comdat_);
251 this->u_.linkonce_size = size;
252 }
253
254 private:
255 // No assignment.
256 Kept_section& operator=(const Kept_section&);
257
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
273 {
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_;
282 };
283
284 // This class handles the details of laying out input sections.
285
286 class Layout
287 {
288 public:
289 Layout(int number_of_input_files, Script_options*);
290
291 ~Layout()
292 {
293 delete this->relaxation_debug_check_;
294 delete this->segment_states_;
295 }
296
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);
309
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);
321
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);
333
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);
354
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);
361
362 // Add an Output_section_data to the layout. This is used for
363 // special sections like the GOT section. IS_DYNAMIC_LINKER_SECTION
364 // is true for sections which are used by the dynamic linker, such
365 // as dynamic reloc sections. IS_RELRO is true for relro sections.
366 // IS_LAST_RELRO is true for the last relro section.
367 // IS_FIRST_NON_RELRO is true for the first section after the relro
368 // sections.
369 Output_section*
370 add_output_section_data(const char* name, elfcpp::Elf_Word type,
371 elfcpp::Elf_Xword flags,
372 Output_section_data*, bool is_dynamic_linker_section,
373 bool is_relro, bool is_last_relro,
374 bool is_first_non_relro);
375
376 // Increase the size of the relro segment by this much.
377 void
378 increase_relro(unsigned int s)
379 { this->increase_relro_ += s; }
380
381 // Create dynamic sections if necessary.
382 void
383 create_initial_dynamic_sections(Symbol_table*);
384
385 // Define __start and __stop symbols for output sections.
386 void
387 define_section_symbols(Symbol_table*);
388
389 // Create automatic note sections.
390 void
391 create_notes();
392
393 // Create sections for linker scripts.
394 void
395 create_script_sections()
396 { this->script_options_->create_script_sections(this); }
397
398 // Define symbols from any linker script.
399 void
400 define_script_symbols(Symbol_table* symtab)
401 { this->script_options_->add_symbols_to_table(symtab); }
402
403 // Define symbols for group signatures.
404 void
405 define_group_signatures(Symbol_table*);
406
407 // Return the Stringpool used for symbol names.
408 const Stringpool*
409 sympool() const
410 { return &this->sympool_; }
411
412 // Return the Stringpool used for dynamic symbol names and dynamic
413 // tags.
414 const Stringpool*
415 dynpool() const
416 { return &this->dynpool_; }
417
418 // Return the symtab_xindex section used to hold large section
419 // indexes for the normal symbol table.
420 Output_symtab_xindex*
421 symtab_xindex() const
422 { return this->symtab_xindex_; }
423
424 // Return the dynsym_xindex section used to hold large section
425 // indexes for the dynamic symbol table.
426 Output_symtab_xindex*
427 dynsym_xindex() const
428 { return this->dynsym_xindex_; }
429
430 // Return whether a section is a .gnu.linkonce section, given the
431 // section name.
432 static inline bool
433 is_linkonce(const char* name)
434 { return strncmp(name, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
435
436 // Return true if a section is a debugging section.
437 static inline bool
438 is_debug_info_section(const char* name)
439 {
440 // Debugging sections can only be recognized by name.
441 return (strncmp(name, ".debug", sizeof(".debug") - 1) == 0
442 || strncmp(name, ".gnu.linkonce.wi.",
443 sizeof(".gnu.linkonce.wi.") - 1) == 0
444 || strncmp(name, ".line", sizeof(".line") - 1) == 0
445 || strncmp(name, ".stab", sizeof(".stab") - 1) == 0);
446 }
447
448 // Check if a comdat group or .gnu.linkonce section with the given
449 // NAME is selected for the link. If there is already a section,
450 // *KEPT_SECTION is set to point to the signature and the function
451 // returns false. Otherwise, OBJECT, SHNDX,IS_COMDAT, and
452 // IS_GROUP_NAME are recorded for this NAME in the layout object,
453 // *KEPT_SECTION is set to the internal copy and the function return
454 // false.
455 bool
456 find_or_add_kept_section(const std::string& name, Relobj* object,
457 unsigned int shndx, bool is_comdat,
458 bool is_group_name, Kept_section** kept_section);
459
460 // Finalize the layout after all the input sections have been added.
461 off_t
462 finalize(const Input_objects*, Symbol_table*, Target*, const Task*);
463
464 // Return whether any sections require postprocessing.
465 bool
466 any_postprocessing_sections() const
467 { return this->any_postprocessing_sections_; }
468
469 // Return the size of the output file.
470 off_t
471 output_file_size() const
472 { return this->output_file_size_; }
473
474 // Return the TLS segment. This will return NULL if there isn't
475 // one.
476 Output_segment*
477 tls_segment() const
478 { return this->tls_segment_; }
479
480 // Return the normal symbol table.
481 Output_section*
482 symtab_section() const
483 {
484 gold_assert(this->symtab_section_ != NULL);
485 return this->symtab_section_;
486 }
487
488 // Return the dynamic symbol table.
489 Output_section*
490 dynsym_section() const
491 {
492 gold_assert(this->dynsym_section_ != NULL);
493 return this->dynsym_section_;
494 }
495
496 // Return the dynamic tags.
497 Output_data_dynamic*
498 dynamic_data() const
499 { return this->dynamic_data_; }
500
501 // Write out the output sections.
502 void
503 write_output_sections(Output_file* of) const;
504
505 // Write out data not associated with an input file or the symbol
506 // table.
507 void
508 write_data(const Symbol_table*, Output_file*) const;
509
510 // Write out output sections which can not be written until all the
511 // input sections are complete.
512 void
513 write_sections_after_input_sections(Output_file* of);
514
515 // Return an output section named NAME, or NULL if there is none.
516 Output_section*
517 find_output_section(const char* name) const;
518
519 // Return an output segment of type TYPE, with segment flags SET set
520 // and segment flags CLEAR clear. Return NULL if there is none.
521 Output_segment*
522 find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
523 elfcpp::Elf_Word clear) const;
524
525 // Return the number of segments we expect to produce.
526 size_t
527 expected_segment_count() const;
528
529 // Set a flag to indicate that an object file uses the static TLS model.
530 void
531 set_has_static_tls()
532 { this->has_static_tls_ = true; }
533
534 // Return true if any object file uses the static TLS model.
535 bool
536 has_static_tls() const
537 { return this->has_static_tls_; }
538
539 // Return the options which may be set by a linker script.
540 Script_options*
541 script_options()
542 { return this->script_options_; }
543
544 const Script_options*
545 script_options() const
546 { return this->script_options_; }
547
548 // Return the object managing inputs in incremental build. NULL in
549 // non-incremental builds.
550 Incremental_inputs*
551 incremental_inputs()
552 { return this->incremental_inputs_; }
553
554 // Compute and write out the build ID if needed.
555 void
556 write_build_id(Output_file*) const;
557
558 // Rewrite output file in binary format.
559 void
560 write_binary(Output_file* in) const;
561
562 // Print output sections to the map file.
563 void
564 print_to_mapfile(Mapfile*) const;
565
566 // Dump statistical information to stderr.
567 void
568 print_stats() const;
569
570 // A list of segments.
571
572 typedef std::vector<Output_segment*> Segment_list;
573
574 // A list of sections.
575
576 typedef std::vector<Output_section*> Section_list;
577
578 // The list of information to write out which is not attached to
579 // either a section or a segment.
580 typedef std::vector<Output_data*> Data_list;
581
582 // Store the allocated sections into the section list. This is used
583 // by the linker script code.
584 void
585 get_allocated_sections(Section_list*) const;
586
587 // Make a section for a linker script to hold data.
588 Output_section*
589 make_output_section_for_script(const char* name);
590
591 // Make a segment. This is used by the linker script code.
592 Output_segment*
593 make_output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags);
594
595 // Return the number of segments.
596 size_t
597 segment_count() const
598 { return this->segment_list_.size(); }
599
600 // Map from section flags to segment flags.
601 static elfcpp::Elf_Word
602 section_flags_to_segment(elfcpp::Elf_Xword flags);
603
604 // Attach sections to segments.
605 void
606 attach_sections_to_segments();
607
608 // For relaxation clean up, we need to know output section data created
609 // from a linker script.
610 void
611 new_output_section_data_from_script(Output_section_data* posd)
612 {
613 if (this->record_output_section_data_from_script_)
614 this->script_output_section_data_list_.push_back(posd);
615 }
616
617 // Return section list.
618 const Section_list&
619 section_list() const
620 { return this->section_list_; }
621
622 private:
623 Layout(const Layout&);
624 Layout& operator=(const Layout&);
625
626 // Mapping from input section names to output section names.
627 struct Section_name_mapping
628 {
629 const char* from;
630 int fromlen;
631 const char* to;
632 int tolen;
633 };
634 static const Section_name_mapping section_name_mapping[];
635 static const int section_name_mapping_count;
636
637 // During a relocatable link, a list of group sections and
638 // signatures.
639 struct Group_signature
640 {
641 // The group section.
642 Output_section* section;
643 // The signature.
644 const char* signature;
645
646 Group_signature()
647 : section(NULL), signature(NULL)
648 { }
649
650 Group_signature(Output_section* sectiona, const char* signaturea)
651 : section(sectiona), signature(signaturea)
652 { }
653 };
654 typedef std::vector<Group_signature> Group_signatures;
655
656 // Create a note section, filling in the header.
657 Output_section*
658 create_note(const char* name, int note_type, const char *section_name,
659 size_t descsz, bool allocate, size_t* trailing_padding);
660
661 // Create a note section for gold version.
662 void
663 create_gold_note();
664
665 // Record whether the stack must be executable.
666 void
667 create_executable_stack_info();
668
669 // Create a build ID note if needed.
670 void
671 create_build_id();
672
673 // Link .stab and .stabstr sections.
674 void
675 link_stabs_sections();
676
677 // Create .gnu_incremental_inputs and .gnu_incremental_strtab sections needed
678 // for the next run of incremental linking to check what has changed.
679 void
680 create_incremental_info_sections();
681
682 // Find the first read-only PT_LOAD segment, creating one if
683 // necessary.
684 Output_segment*
685 find_first_load_seg();
686
687 // Count the local symbols in the regular symbol table and the dynamic
688 // symbol table, and build the respective string pools.
689 void
690 count_local_symbols(const Task*, const Input_objects*);
691
692 // Create the output sections for the symbol table.
693 void
694 create_symtab_sections(const Input_objects*, Symbol_table*,
695 unsigned int, off_t*);
696
697 // Create the .shstrtab section.
698 Output_section*
699 create_shstrtab();
700
701 // Create the section header table.
702 void
703 create_shdrs(const Output_section* shstrtab_section, off_t*);
704
705 // Create the dynamic symbol table.
706 void
707 create_dynamic_symtab(const Input_objects*, Symbol_table*,
708 Output_section** pdynstr,
709 unsigned int* plocal_dynamic_count,
710 std::vector<Symbol*>* pdynamic_symbols,
711 Versions* versions);
712
713 // Assign offsets to each local portion of the dynamic symbol table.
714 void
715 assign_local_dynsym_offsets(const Input_objects*);
716
717 // Finish the .dynamic section and PT_DYNAMIC segment.
718 void
719 finish_dynamic_section(const Input_objects*, const Symbol_table*);
720
721 // Set the size of the _DYNAMIC symbol.
722 void
723 set_dynamic_symbol_size(const Symbol_table*);
724
725 // Create the .interp section and PT_INTERP segment.
726 void
727 create_interp(const Target* target);
728
729 // Create the version sections.
730 void
731 create_version_sections(const Versions*,
732 const Symbol_table*,
733 unsigned int local_symcount,
734 const std::vector<Symbol*>& dynamic_symbols,
735 const Output_section* dynstr);
736
737 template<int size, bool big_endian>
738 void
739 sized_create_version_sections(const Versions* versions,
740 const Symbol_table*,
741 unsigned int local_symcount,
742 const std::vector<Symbol*>& dynamic_symbols,
743 const Output_section* dynstr);
744
745 // Return whether to include this section in the link.
746 template<int size, bool big_endian>
747 bool
748 include_section(Sized_relobj<size, big_endian>* object, const char* name,
749 const elfcpp::Shdr<size, big_endian>&);
750
751 // Return the output section name to use given an input section
752 // name. Set *PLEN to the length of the name. *PLEN must be
753 // initialized to the length of NAME.
754 static const char*
755 output_section_name(const char* name, size_t* plen);
756
757 // Return the number of allocated output sections.
758 size_t
759 allocated_output_section_count() const;
760
761 // Return the output section for NAME, TYPE and FLAGS.
762 Output_section*
763 get_output_section(const char* name, Stringpool::Key name_key,
764 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
765 bool is_interp, bool is_dynamic_linker_section,
766 bool is_relro, bool is_last_relro,
767 bool is_first_non_relro);
768
769 // Choose the output section for NAME in RELOBJ.
770 Output_section*
771 choose_output_section(const Relobj* relobj, const char* name,
772 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
773 bool is_input_section, bool is_interp,
774 bool is_dynamic_linker_section, bool is_relro,
775 bool is_last_relro, bool is_first_non_relro);
776
777 // Create a new Output_section.
778 Output_section*
779 make_output_section(const char* name, elfcpp::Elf_Word type,
780 elfcpp::Elf_Xword flags, bool is_interp,
781 bool is_dynamic_linker_section, bool is_relro,
782 bool is_last_relro, bool is_first_non_relro);
783
784 // Attach a section to a segment.
785 void
786 attach_section_to_segment(Output_section*);
787
788 // Attach an allocated section to a segment.
789 void
790 attach_allocated_section_to_segment(Output_section*);
791
792 // Set the final file offsets of all the segments.
793 off_t
794 set_segment_offsets(const Target*, Output_segment*, unsigned int* pshndx);
795
796 // Set the file offsets of the sections when doing a relocatable
797 // link.
798 off_t
799 set_relocatable_section_offsets(Output_data*, unsigned int* pshndx);
800
801 // Set the final file offsets of all the sections not associated
802 // with a segment. We set section offsets in three passes: the
803 // first handles all allocated sections, the second sections that
804 // require postprocessing, and the last the late-bound STRTAB
805 // sections (probably only shstrtab, which is the one we care about
806 // because it holds section names).
807 enum Section_offset_pass
808 {
809 BEFORE_INPUT_SECTIONS_PASS,
810 POSTPROCESSING_SECTIONS_PASS,
811 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
812 };
813 off_t
814 set_section_offsets(off_t, Section_offset_pass pass);
815
816 // Set the final section indexes of all the sections not associated
817 // with a segment. Returns the next unused index.
818 unsigned int
819 set_section_indexes(unsigned int pshndx);
820
821 // Set the section addresses when using a script.
822 Output_segment*
823 set_section_addresses_from_script(Symbol_table*);
824
825 // Find appropriate places or orphan sections in a script.
826 void
827 place_orphan_sections_in_script();
828
829 // Return whether SEG1 comes before SEG2 in the output file.
830 static bool
831 segment_precedes(const Output_segment* seg1, const Output_segment* seg2);
832
833 // Use to save and restore segments during relaxation.
834 typedef Unordered_map<const Output_segment*, const Output_segment*>
835 Segment_states;
836
837 // Save states of current output segments.
838 void
839 save_segments(Segment_states*);
840
841 // Restore output segment states.
842 void
843 restore_segments(const Segment_states*);
844
845 // Clean up after relaxation so that it is possible to lay out the
846 // sections and segments again.
847 void
848 clean_up_after_relaxation();
849
850 // Doing preparation work for relaxation. This is factored out to make
851 // Layout::finalized a bit smaller and easier to read.
852 void
853 prepare_for_relaxation();
854
855 // Main body of the relaxation loop, which lays out the section.
856 off_t
857 relaxation_loop_body(int, Target*, Symbol_table*, Output_segment**,
858 Output_segment*, Output_segment_headers*,
859 Output_file_header*, unsigned int*);
860
861 // A mapping used for kept comdats/.gnu.linkonce group signatures.
862 typedef Unordered_map<std::string, Kept_section> Signatures;
863
864 // Mapping from input section name/type/flags to output section. We
865 // use canonicalized strings here.
866
867 typedef std::pair<Stringpool::Key,
868 std::pair<elfcpp::Elf_Word, elfcpp::Elf_Xword> > Key;
869
870 struct Hash_key
871 {
872 size_t
873 operator()(const Key& k) const;
874 };
875
876 typedef Unordered_map<Key, Output_section*, Hash_key> Section_name_map;
877
878 // A comparison class for segments.
879
880 struct Compare_segments
881 {
882 bool
883 operator()(const Output_segment* seg1, const Output_segment* seg2)
884 { return Layout::segment_precedes(seg1, seg2); }
885 };
886
887 typedef std::vector<Output_section_data*> Output_section_data_list;
888
889 // Debug checker class.
890 class Relaxation_debug_check
891 {
892 public:
893 Relaxation_debug_check()
894 : section_infos_()
895 { }
896
897 // Check that sections and special data are in reset states.
898 void
899 check_output_data_for_reset_values(const Layout::Section_list&,
900 const Layout::Data_list&);
901
902 // Record information of a section list.
903 void
904 read_sections(const Layout::Section_list&);
905
906 // Verify a section list with recorded information.
907 void
908 verify_sections(const Layout::Section_list&);
909
910 private:
911 // Information we care about a section.
912 struct Section_info
913 {
914 // Output section described by this.
915 Output_section* output_section;
916 // Load address.
917 uint64_t address;
918 // Data size.
919 off_t data_size;
920 // File offset.
921 off_t offset;
922 };
923
924 // Section information.
925 std::vector<Section_info> section_infos_;
926 };
927
928 // The number of input files, for sizing tables.
929 int number_of_input_files_;
930 // Information set by scripts or by command line options.
931 Script_options* script_options_;
932 // The output section names.
933 Stringpool namepool_;
934 // The output symbol names.
935 Stringpool sympool_;
936 // The dynamic strings, if needed.
937 Stringpool dynpool_;
938 // The list of group sections and linkonce sections which we have seen.
939 Signatures signatures_;
940 // The mapping from input section name/type/flags to output sections.
941 Section_name_map section_name_map_;
942 // The list of output segments.
943 Segment_list segment_list_;
944 // The list of output sections.
945 Section_list section_list_;
946 // The list of output sections which are not attached to any output
947 // segment.
948 Section_list unattached_section_list_;
949 // The list of unattached Output_data objects which require special
950 // handling because they are not Output_sections.
951 Data_list special_output_list_;
952 // The section headers.
953 Output_section_headers* section_headers_;
954 // A pointer to the PT_TLS segment if there is one.
955 Output_segment* tls_segment_;
956 // A pointer to the PT_GNU_RELRO segment if there is one.
957 Output_segment* relro_segment_;
958 // A backend may increase the size of the PT_GNU_RELRO segment if
959 // there is one. This is the amount to increase it by.
960 unsigned int increase_relro_;
961 // The SHT_SYMTAB output section.
962 Output_section* symtab_section_;
963 // The SHT_SYMTAB_SHNDX for the regular symbol table if there is one.
964 Output_symtab_xindex* symtab_xindex_;
965 // The SHT_DYNSYM output section if there is one.
966 Output_section* dynsym_section_;
967 // The SHT_SYMTAB_SHNDX for the dynamic symbol table if there is one.
968 Output_symtab_xindex* dynsym_xindex_;
969 // The SHT_DYNAMIC output section if there is one.
970 Output_section* dynamic_section_;
971 // The _DYNAMIC symbol if there is one.
972 Symbol* dynamic_symbol_;
973 // The dynamic data which goes into dynamic_section_.
974 Output_data_dynamic* dynamic_data_;
975 // The exception frame output section if there is one.
976 Output_section* eh_frame_section_;
977 // The exception frame data for eh_frame_section_.
978 Eh_frame* eh_frame_data_;
979 // Whether we have added eh_frame_data_ to the .eh_frame section.
980 bool added_eh_frame_data_;
981 // The exception frame header output section if there is one.
982 Output_section* eh_frame_hdr_section_;
983 // The space for the build ID checksum if there is one.
984 Output_section_data* build_id_note_;
985 // The output section containing dwarf abbreviations
986 Output_reduced_debug_abbrev_section* debug_abbrev_;
987 // The output section containing the dwarf debug info tree
988 Output_reduced_debug_info_section* debug_info_;
989 // A list of group sections and their signatures.
990 Group_signatures group_signatures_;
991 // The size of the output file.
992 off_t output_file_size_;
993 // Whether we have attached the sections to the segments.
994 bool sections_are_attached_;
995 // Whether we have seen an object file marked to require an
996 // executable stack.
997 bool input_requires_executable_stack_;
998 // Whether we have seen at least one object file with an executable
999 // stack marker.
1000 bool input_with_gnu_stack_note_;
1001 // Whether we have seen at least one object file without an
1002 // executable stack marker.
1003 bool input_without_gnu_stack_note_;
1004 // Whether we have seen an object file that uses the static TLS model.
1005 bool has_static_tls_;
1006 // Whether any sections require postprocessing.
1007 bool any_postprocessing_sections_;
1008 // Whether we have resized the signatures_ hash table.
1009 bool resized_signatures_;
1010 // Whether we have created a .stab*str output section.
1011 bool have_stabstr_section_;
1012 // In incremental build, holds information check the inputs and build the
1013 // .gnu_incremental_inputs section.
1014 Incremental_inputs* incremental_inputs_;
1015 // Whether we record output section data created in script
1016 bool record_output_section_data_from_script_;
1017 // List of output data that needs to be removed at relexation clean up.
1018 Output_section_data_list script_output_section_data_list_;
1019 // Structure to save segment states before entering the relaxation loop.
1020 Segment_states* segment_states_;
1021 // A relaxation debug checker. We only create one when in debugging mode.
1022 Relaxation_debug_check* relaxation_debug_check_;
1023 };
1024
1025 // This task handles writing out data in output sections which is not
1026 // part of an input section, or which requires special handling. When
1027 // this is done, it unblocks both output_sections_blocker and
1028 // final_blocker.
1029
1030 class Write_sections_task : public Task
1031 {
1032 public:
1033 Write_sections_task(const Layout* layout, Output_file* of,
1034 Task_token* output_sections_blocker,
1035 Task_token* final_blocker)
1036 : layout_(layout), of_(of),
1037 output_sections_blocker_(output_sections_blocker),
1038 final_blocker_(final_blocker)
1039 { }
1040
1041 // The standard Task methods.
1042
1043 Task_token*
1044 is_runnable();
1045
1046 void
1047 locks(Task_locker*);
1048
1049 void
1050 run(Workqueue*);
1051
1052 std::string
1053 get_name() const
1054 { return "Write_sections_task"; }
1055
1056 private:
1057 class Write_sections_locker;
1058
1059 const Layout* layout_;
1060 Output_file* of_;
1061 Task_token* output_sections_blocker_;
1062 Task_token* final_blocker_;
1063 };
1064
1065 // This task handles writing out data which is not part of a section
1066 // or segment.
1067
1068 class Write_data_task : public Task
1069 {
1070 public:
1071 Write_data_task(const Layout* layout, const Symbol_table* symtab,
1072 Output_file* of, Task_token* final_blocker)
1073 : layout_(layout), symtab_(symtab), of_(of), final_blocker_(final_blocker)
1074 { }
1075
1076 // The standard Task methods.
1077
1078 Task_token*
1079 is_runnable();
1080
1081 void
1082 locks(Task_locker*);
1083
1084 void
1085 run(Workqueue*);
1086
1087 std::string
1088 get_name() const
1089 { return "Write_data_task"; }
1090
1091 private:
1092 const Layout* layout_;
1093 const Symbol_table* symtab_;
1094 Output_file* of_;
1095 Task_token* final_blocker_;
1096 };
1097
1098 // This task handles writing out the global symbols.
1099
1100 class Write_symbols_task : public Task
1101 {
1102 public:
1103 Write_symbols_task(const Layout* layout, const Symbol_table* symtab,
1104 const Input_objects* input_objects,
1105 const Stringpool* sympool, const Stringpool* dynpool,
1106 Output_file* of, Task_token* final_blocker)
1107 : layout_(layout), symtab_(symtab), input_objects_(input_objects),
1108 sympool_(sympool), dynpool_(dynpool), of_(of),
1109 final_blocker_(final_blocker)
1110 { }
1111
1112 // The standard Task methods.
1113
1114 Task_token*
1115 is_runnable();
1116
1117 void
1118 locks(Task_locker*);
1119
1120 void
1121 run(Workqueue*);
1122
1123 std::string
1124 get_name() const
1125 { return "Write_symbols_task"; }
1126
1127 private:
1128 const Layout* layout_;
1129 const Symbol_table* symtab_;
1130 const Input_objects* input_objects_;
1131 const Stringpool* sympool_;
1132 const Stringpool* dynpool_;
1133 Output_file* of_;
1134 Task_token* final_blocker_;
1135 };
1136
1137 // This task handles writing out data in output sections which can't
1138 // be written out until all the input sections have been handled.
1139 // This is for sections whose contents is based on the contents of
1140 // other output sections.
1141
1142 class Write_after_input_sections_task : public Task
1143 {
1144 public:
1145 Write_after_input_sections_task(Layout* layout, Output_file* of,
1146 Task_token* input_sections_blocker,
1147 Task_token* final_blocker)
1148 : layout_(layout), of_(of),
1149 input_sections_blocker_(input_sections_blocker),
1150 final_blocker_(final_blocker)
1151 { }
1152
1153 // The standard Task methods.
1154
1155 Task_token*
1156 is_runnable();
1157
1158 void
1159 locks(Task_locker*);
1160
1161 void
1162 run(Workqueue*);
1163
1164 std::string
1165 get_name() const
1166 { return "Write_after_input_sections_task"; }
1167
1168 private:
1169 Layout* layout_;
1170 Output_file* of_;
1171 Task_token* input_sections_blocker_;
1172 Task_token* final_blocker_;
1173 };
1174
1175 // This task function handles closing the file.
1176
1177 class Close_task_runner : public Task_function_runner
1178 {
1179 public:
1180 Close_task_runner(const General_options* options, const Layout* layout,
1181 Output_file* of)
1182 : options_(options), layout_(layout), of_(of)
1183 { }
1184
1185 // Run the operation.
1186 void
1187 run(Workqueue*, const Task*);
1188
1189 private:
1190 const General_options* options_;
1191 const Layout* layout_;
1192 Output_file* of_;
1193 };
1194
1195 // A small helper function to align an address.
1196
1197 inline uint64_t
1198 align_address(uint64_t address, uint64_t addralign)
1199 {
1200 if (addralign != 0)
1201 address = (address + addralign - 1) &~ (addralign - 1);
1202 return address;
1203 }
1204
1205 } // End namespace gold.
1206
1207 #endif // !defined(GOLD_LAYOUT_H)
This page took 0.070771 seconds and 5 git commands to generate.