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