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