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