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