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