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