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