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