Add const to Object::read and Object::sized_target.
[deliverable/binutils-gdb.git] / gold / layout.h
CommitLineData
a2fb1b05
ILT
1// layout.h -- lay out output file sections for gold -*- C++ -*-
2
6cb15b7f
ILT
3// Copyright 2006, 2007 Free Software Foundation, Inc.
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
26#include <list>
27#include <string>
28#include <utility>
29#include <vector>
30
a2fb1b05
ILT
31#include "workqueue.h"
32#include "object.h"
14b31740 33#include "dynobj.h"
a2fb1b05
ILT
34#include "stringpool.h"
35
36namespace gold
37{
38
ead1e424 39class General_options;
54dc6425 40class Input_objects;
75f65a3e 41class Symbol_table;
ead1e424 42class Output_section_data;
a2fb1b05 43class Output_section;
75f65a3e 44class Output_section_headers;
a2fb1b05 45class Output_segment;
54dc6425 46class Output_data;
a3ad94ed 47class Output_data_dynamic;
730cdc88 48class Eh_frame;
61ba1cf9 49class Target;
a2fb1b05 50
92e059d8
ILT
51// This task function handles mapping the input sections to output
52// sections and laying them out in memory.
a2fb1b05 53
92e059d8 54class Layout_task_runner : public Task_function_runner
a2fb1b05
ILT
55{
56 public:
57 // OPTIONS is the command line options, INPUT_OBJECTS is the list of
92e059d8
ILT
58 // input objects, SYMTAB is the symbol table, LAYOUT is the layout
59 // object.
60 Layout_task_runner(const General_options& options,
61 const Input_objects* input_objects,
62 Symbol_table* symtab,
63 Layout* layout)
75f65a3e 64 : options_(options), input_objects_(input_objects), symtab_(symtab),
92e059d8 65 layout_(layout)
a2fb1b05
ILT
66 { }
67
92e059d8 68 // Run the operation.
a2fb1b05
ILT
69 void
70 run(Workqueue*);
71
72 private:
92e059d8
ILT
73 Layout_task_runner(const Layout_task_runner&);
74 Layout_task_runner& operator=(const Layout_task_runner&);
a2fb1b05
ILT
75
76 const General_options& options_;
54dc6425 77 const Input_objects* input_objects_;
75f65a3e 78 Symbol_table* symtab_;
12e14209 79 Layout* layout_;
a2fb1b05
ILT
80};
81
82// This class handles the details of laying out input sections.
83
84class Layout
85{
86 public:
54dc6425
ILT
87 Layout(const General_options& options);
88
ead1e424
ILT
89 // Given an input section SHNDX, named NAME, with data in SHDR, from
90 // the object file OBJECT, return the output section where this
730cdc88
ILT
91 // input section should go. RELOC_SHNDX is the index of a
92 // relocation section which applies to this section, or 0 if none,
93 // or -1U if more than one. RELOC_TYPE is the type of the
94 // relocation section if there is one. Set *OFFSET to the offset
95 // within the output section.
a2fb1b05
ILT
96 template<int size, bool big_endian>
97 Output_section*
730cdc88
ILT
98 layout(Sized_relobj<size, big_endian> *object, unsigned int shndx,
99 const char* name, const elfcpp::Shdr<size, big_endian>& shdr,
100 unsigned int reloc_shndx, unsigned int reloc_type, off_t* offset);
101
102 // Like layout, only for exception frame sections. OBJECT is an
103 // object file. SYMBOLS is the contents of the symbol table
104 // section, with size SYMBOLS_SIZE. SYMBOL_NAMES is the contents of
105 // the symbol name section, with size SYMBOL_NAMES_SIZE. SHNDX is a
106 // .eh_frame section in OBJECT. SHDR is the section header.
107 // RELOC_SHNDX is the index of a relocation section which applies to
108 // this section, or 0 if none, or -1U if more than one. RELOC_TYPE
109 // is the type of the relocation section if there is one. This
110 // returns the output section, and sets *OFFSET to the offset.
111 template<int size, bool big_endian>
112 Output_section*
113 layout_eh_frame(Sized_relobj<size, big_endian>* object,
114 const unsigned char* symbols,
115 off_t symbols_size,
116 const unsigned char* symbol_names,
117 off_t symbol_names_size,
118 unsigned int shndx,
119 const elfcpp::Shdr<size, big_endian>& shdr,
120 unsigned int reloc_shndx, unsigned int reloc_type,
121 off_t* offset);
a2fb1b05 122
35cdfc9a
ILT
123 // Handle a GNU stack note. This is called once per input object
124 // file. SEEN_GNU_STACK is true if the object file has a
125 // .note.GNU-stack section. GNU_STACK_FLAGS is the section flags
126 // from that section if there was one.
127 void
128 layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags);
129
ead1e424
ILT
130 // Add an Output_section_data to the layout. This is used for
131 // special sections like the GOT section.
132 void
133 add_output_section_data(const char* name, elfcpp::Elf_Word type,
134 elfcpp::Elf_Xword flags,
135 Output_section_data*);
136
a3ad94ed
ILT
137 // Create dynamic sections if necessary.
138 void
139 create_initial_dynamic_sections(const Input_objects*, Symbol_table*);
140
bfd58944
ILT
141 // Define __start and __stop symbols for output sections.
142 void
143 define_section_symbols(Symbol_table*, const Target*);
144
61ba1cf9
ILT
145 // Return the Stringpool used for symbol names.
146 const Stringpool*
147 sympool() const
148 { return &this->sympool_; }
149
16649710
ILT
150 // Return the Stringpool used for dynamic symbol names and dynamic
151 // tags.
152 const Stringpool*
153 dynpool() const
154 { return &this->dynpool_; }
155
a2fb1b05
ILT
156 // Return whether a section is a .gnu.linkonce section, given the
157 // section name.
158 static inline bool
159 is_linkonce(const char* name)
160 { return strncmp(name, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
161
162 // Record the signature of a comdat section, and return whether to
163 // include it in the link. The GROUP parameter is true for a
164 // section group signature, false for a signature derived from a
165 // .gnu.linkonce section.
166 bool
167 add_comdat(const char*, bool group);
168
54dc6425 169 // Finalize the layout after all the input sections have been added.
75f65a3e
ILT
170 off_t
171 finalize(const Input_objects*, Symbol_table*);
54dc6425 172
e44fcf3b
ILT
173 // Return the size of the output file.
174 off_t
175 output_file_size() const
176 { return this->output_file_size_; }
177
16649710
ILT
178 // Return the TLS segment. This will return NULL if there isn't
179 // one.
92e059d8
ILT
180 Output_segment*
181 tls_segment() const
182 { return this->tls_segment_; }
183
16649710
ILT
184 // Return the normal symbol table.
185 Output_section*
186 symtab_section() const
187 {
188 gold_assert(this->symtab_section_ != NULL);
189 return this->symtab_section_;
190 }
191
192 // Return the dynamic symbol table.
193 Output_section*
194 dynsym_section() const
195 {
196 gold_assert(this->dynsym_section_ != NULL);
197 return this->dynsym_section_;
198 }
199
200 // Return the dynamic tags.
201 Output_data_dynamic*
202 dynamic_data() const
203 { return this->dynamic_data_; }
204
730cdc88
ILT
205 // Write out the output sections.
206 void
207 write_output_sections(Output_file* of) const;
208
61ba1cf9
ILT
209 // Write out data not associated with an input file or the symbol
210 // table.
211 void
9025d29d 212 write_data(const Symbol_table*, Output_file*) const;
61ba1cf9 213
730cdc88
ILT
214 // Write out output sections which can not be written until all the
215 // input sections are complete.
216 void
27bc2bce 217 write_sections_after_input_sections(Output_file* of);
730cdc88 218
ead1e424
ILT
219 // Return an output section named NAME, or NULL if there is none.
220 Output_section*
221 find_output_section(const char* name) const;
222
223 // Return an output segment of type TYPE, with segment flags SET set
224 // and segment flags CLEAR clear. Return NULL if there is none.
225 Output_segment*
226 find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
227 elfcpp::Elf_Word clear) const;
228
535890bb
ILT
229 // Set a flag to indicate that an object file uses the static TLS model.
230 void
231 set_has_static_tls()
232 { this->has_static_tls_ = true; }
233
234 // Return true if any object file uses the static TLS model.
235 bool
236 has_static_tls() const
237 { return this->has_static_tls_; }
238
ad8f37d1
ILT
239 // Dump statistical information to stderr.
240 void
241 print_stats() const;
242
54dc6425
ILT
243 // The list of segments.
244
245 typedef std::vector<Output_segment*> Segment_list;
246
247 // The list of sections not attached to a segment.
248
a3ad94ed 249 typedef std::vector<Output_section*> Section_list;
54dc6425
ILT
250
251 // The list of information to write out which is not attached to
252 // either a section or a segment.
a3ad94ed 253 typedef std::vector<Output_data*> Data_list;
54dc6425 254
a2fb1b05
ILT
255 private:
256 Layout(const Layout&);
257 Layout& operator=(const Layout&);
258
259 // Mapping from .gnu.linkonce section names to output section names.
260 struct Linkonce_mapping
261 {
262 const char* from;
263 int fromlen;
264 const char* to;
ead1e424 265 int tolen;
a2fb1b05
ILT
266 };
267 static const Linkonce_mapping linkonce_mapping[];
268 static const int linkonce_mapping_count;
269
4f211c8b
ILT
270 // Create a .note section for gold.
271 void
35cdfc9a
ILT
272 create_gold_note();
273
274 // Record whether the stack must be executable.
275 void
276 create_executable_stack_info(const Target*);
4f211c8b 277
75f65a3e
ILT
278 // Find the first read-only PT_LOAD segment, creating one if
279 // necessary.
280 Output_segment*
281 find_first_load_seg();
282
7bf1f802
ILT
283 // Count the local symbols in the regular symbol table and the dynamic
284 // symbol table, and build the respective string pools.
285 void
286 count_local_symbols(const Input_objects*);
287
54dc6425
ILT
288 // Create the output sections for the symbol table.
289 void
9025d29d 290 create_symtab_sections(const Input_objects*, Symbol_table*, off_t*);
54dc6425 291
75f65a3e
ILT
292 // Create the .shstrtab section.
293 Output_section*
294 create_shstrtab();
295
296 // Create the section header table.
27bc2bce 297 void
9025d29d 298 create_shdrs(off_t*);
54dc6425 299
dbe717ef
ILT
300 // Create the dynamic symbol table.
301 void
7bf1f802
ILT
302 create_dynamic_symtab(const Input_objects*, const Target*,
303 Symbol_table*, Output_section** pdynstr,
14b31740
ILT
304 unsigned int* plocal_dynamic_count,
305 std::vector<Symbol*>* pdynamic_symbols,
306 Versions* versions);
dbe717ef 307
7bf1f802
ILT
308 // Assign offsets to each local portion of the dynamic symbol table.
309 void
310 assign_local_dynsym_offsets(const Input_objects*);
311
a3ad94ed 312 // Finish the .dynamic section and PT_DYNAMIC segment.
dbe717ef 313 void
16649710 314 finish_dynamic_section(const Input_objects*, const Symbol_table*);
dbe717ef
ILT
315
316 // Create the .interp section and PT_INTERP segment.
317 void
318 create_interp(const Target* target);
319
14b31740
ILT
320 // Create the version sections.
321 void
9025d29d 322 create_version_sections(const Versions*,
46fe1623 323 const Symbol_table*,
14b31740
ILT
324 unsigned int local_symcount,
325 const std::vector<Symbol*>& dynamic_symbols,
326 const Output_section* dynstr);
327
328 template<int size, bool big_endian>
329 void
330 sized_create_version_sections(const Versions* versions,
46fe1623 331 const Symbol_table*,
14b31740
ILT
332 unsigned int local_symcount,
333 const std::vector<Symbol*>& dynamic_symbols,
91da9340
ILT
334 const Output_section* dynstr
335 ACCEPT_SIZE_ENDIAN);
14b31740 336
a2fb1b05
ILT
337 // Return whether to include this section in the link.
338 template<int size, bool big_endian>
339 bool
730cdc88 340 include_section(Sized_relobj<size, big_endian>* object, const char* name,
a2fb1b05
ILT
341 const elfcpp::Shdr<size, big_endian>&);
342
ead1e424
ILT
343 // Return the output section name to use given an input section
344 // name. Set *PLEN to the length of the name. *PLEN must be
345 // initialized to the length of NAME.
346 static const char*
347 output_section_name(const char* name, size_t* plen);
348
a2fb1b05 349 // Return the output section name to use for a linkonce section
ead1e424 350 // name. PLEN is as for output_section_name.
a2fb1b05 351 static const char*
ead1e424
ILT
352 linkonce_output_name(const char* name, size_t* plen);
353
354 // Return the output section for NAME, TYPE and FLAGS.
355 Output_section*
f0641a0b
ILT
356 get_output_section(const char* name, Stringpool::Key name_key,
357 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags);
a2fb1b05
ILT
358
359 // Create a new Output_section.
360 Output_section*
361 make_output_section(const char* name, elfcpp::Elf_Word type,
362 elfcpp::Elf_Xword flags);
363
dbe717ef
ILT
364 // Set the final file offsets of all the segments.
365 off_t
366 set_segment_offsets(const Target*, Output_segment*, unsigned int* pshndx);
367
86887060 368 // Set the final file offsets of all the sections not associated
9a0910c3
ILT
369 // with a segment. We set section offsets in three passes: the
370 // first handles all allocated sections, the second sections that
371 // can be handled after input-sections are processed, and the last
372 // the late-bound STRTAB sections (probably only shstrtab, which is
373 // the one we care about because it holds section names).
374 enum Section_offset_pass
375 {
376 BEFORE_INPUT_SECTIONS_PASS,
377 AFTER_INPUT_SECTIONS_PASS,
378 STRTAB_AFTER_INPUT_SECTIONS_PASS
379 };
dbe717ef 380 off_t
9a0910c3
ILT
381 set_section_offsets(off_t, Section_offset_pass pass);
382
86887060
ILT
383 // Set the final section indexes of all the sections not associated
384 // with a segment. Returns the next unused index.
385 unsigned int
386 set_section_indexes(unsigned int pshndx);
dbe717ef 387
a2fb1b05
ILT
388 // Return whether SEG1 comes before SEG2 in the output file.
389 static bool
b3168e9d 390 segment_precedes(const Output_segment* seg1, const Output_segment* seg2);
a2fb1b05
ILT
391
392 // Map from section flags to segment flags.
393 static elfcpp::Elf_Word
394 section_flags_to_segment(elfcpp::Elf_Xword flags);
395
396 // A mapping used for group signatures.
397 typedef Unordered_map<std::string, bool> Signatures;
398
399 // Mapping from input section name/type/flags to output section. We
400 // use canonicalized strings here.
401
f0641a0b 402 typedef std::pair<Stringpool::Key,
a2fb1b05
ILT
403 std::pair<elfcpp::Elf_Word, elfcpp::Elf_Xword> > Key;
404
405 struct Hash_key
406 {
407 size_t
408 operator()(const Key& k) const;
409 };
410
411 typedef Unordered_map<Key, Output_section*, Hash_key> Section_name_map;
412
413 // A comparison class for segments.
414
415 struct Compare_segments
416 {
417 bool
418 operator()(const Output_segment* seg1, const Output_segment* seg2)
419 { return Layout::segment_precedes(seg1, seg2); }
420 };
421
a2fb1b05
ILT
422 // A reference to the options on the command line.
423 const General_options& options_;
424 // The output section names.
425 Stringpool namepool_;
75f65a3e
ILT
426 // The output symbol names.
427 Stringpool sympool_;
a3ad94ed
ILT
428 // The dynamic strings, if needed.
429 Stringpool dynpool_;
a2fb1b05
ILT
430 // The list of group sections and linkonce sections which we have seen.
431 Signatures signatures_;
432 // The mapping from input section name/type/flags to output sections.
433 Section_name_map section_name_map_;
434 // The list of output segments.
435 Segment_list segment_list_;
a3ad94ed
ILT
436 // The list of output sections.
437 Section_list section_list_;
a2fb1b05
ILT
438 // The list of output sections which are not attached to any output
439 // segment.
a3ad94ed
ILT
440 Section_list unattached_section_list_;
441 // The list of unattached Output_data objects which require special
442 // handling because they are not Output_sections.
61ba1cf9 443 Data_list special_output_list_;
27bc2bce
ILT
444 // The section headers.
445 Output_section_headers* section_headers_;
92e059d8
ILT
446 // A pointer to the PT_TLS segment if there is one.
447 Output_segment* tls_segment_;
a3ad94ed
ILT
448 // The SHT_SYMTAB output section.
449 Output_section* symtab_section_;
450 // The SHT_DYNSYM output section if there is one.
451 Output_section* dynsym_section_;
452 // The SHT_DYNAMIC output section if there is one.
453 Output_section* dynamic_section_;
16649710
ILT
454 // The dynamic data which goes into dynamic_section_.
455 Output_data_dynamic* dynamic_data_;
730cdc88 456 // The exception frame output section if there is one.
3151305a 457 Output_section* eh_frame_section_;
730cdc88
ILT
458 // The exception frame data for eh_frame_section_.
459 Eh_frame* eh_frame_data_;
460 // The exception frame header output section if there is one.
461 Output_section* eh_frame_hdr_section_;
e44fcf3b
ILT
462 // The size of the output file.
463 off_t output_file_size_;
35cdfc9a
ILT
464 // Whether we have seen an object file marked to require an
465 // executable stack.
466 bool input_requires_executable_stack_;
467 // Whether we have seen at least one object file with an executable
468 // stack marker.
469 bool input_with_gnu_stack_note_;
470 // Whether we have seen at least one object file without an
471 // executable stack marker.
472 bool input_without_gnu_stack_note_;
535890bb
ILT
473 // Whether we have seen an object file that uses the static TLS model.
474 bool has_static_tls_;
61ba1cf9
ILT
475};
476
730cdc88
ILT
477// This task handles writing out data in output sections which is not
478// part of an input section, or which requires special handling. When
479// this is done, it unblocks both output_sections_blocker and
480// final_blocker.
481
482class Write_sections_task : public Task
483{
484 public:
485 Write_sections_task(const Layout* layout, Output_file* of,
486 Task_token* output_sections_blocker,
487 Task_token* final_blocker)
488 : layout_(layout), of_(of),
489 output_sections_blocker_(output_sections_blocker),
490 final_blocker_(final_blocker)
491 { }
492
493 // The standard Task methods.
494
495 Is_runnable_type
496 is_runnable(Workqueue*);
497
498 Task_locker*
499 locks(Workqueue*);
500
501 void
502 run(Workqueue*);
503
c7912668
ILT
504 std::string
505 get_name() const
506 { return "Write_sections_task"; }
507
730cdc88
ILT
508 private:
509 class Write_sections_locker;
510
511 const Layout* layout_;
512 Output_file* of_;
513 Task_token* output_sections_blocker_;
514 Task_token* final_blocker_;
515};
516
61ba1cf9
ILT
517// This task handles writing out data which is not part of a section
518// or segment.
519
520class Write_data_task : public Task
521{
522 public:
a3ad94ed 523 Write_data_task(const Layout* layout, const Symbol_table* symtab,
9025d29d
ILT
524 Output_file* of, Task_token* final_blocker)
525 : layout_(layout), symtab_(symtab), of_(of), final_blocker_(final_blocker)
61ba1cf9
ILT
526 { }
527
528 // The standard Task methods.
529
530 Is_runnable_type
531 is_runnable(Workqueue*);
532
533 Task_locker*
534 locks(Workqueue*);
535
536 void
537 run(Workqueue*);
538
c7912668
ILT
539 std::string
540 get_name() const
541 { return "Write_data_task"; }
542
61ba1cf9
ILT
543 private:
544 const Layout* layout_;
a3ad94ed 545 const Symbol_table* symtab_;
61ba1cf9
ILT
546 Output_file* of_;
547 Task_token* final_blocker_;
548};
549
550// This task handles writing out the global symbols.
551
552class Write_symbols_task : public Task
553{
554 public:
9a2d6984
ILT
555 Write_symbols_task(const Symbol_table* symtab,
556 const Input_objects* input_objects,
16649710
ILT
557 const Stringpool* sympool, const Stringpool* dynpool,
558 Output_file* of, Task_token* final_blocker)
9a2d6984
ILT
559 : symtab_(symtab), input_objects_(input_objects), sympool_(sympool),
560 dynpool_(dynpool), of_(of), final_blocker_(final_blocker)
61ba1cf9
ILT
561 { }
562
563 // The standard Task methods.
564
565 Is_runnable_type
566 is_runnable(Workqueue*);
567
568 Task_locker*
569 locks(Workqueue*);
570
571 void
572 run(Workqueue*);
573
c7912668
ILT
574 std::string
575 get_name() const
576 { return "Write_symbols_task"; }
577
61ba1cf9
ILT
578 private:
579 const Symbol_table* symtab_;
9a2d6984 580 const Input_objects* input_objects_;
61ba1cf9 581 const Stringpool* sympool_;
16649710 582 const Stringpool* dynpool_;
61ba1cf9
ILT
583 Output_file* of_;
584 Task_token* final_blocker_;
585};
586
730cdc88
ILT
587// This task handles writing out data in output sections which can't
588// be written out until all the input sections have been handled.
589// This is for sections whose contents is based on the contents of
590// other output sections.
591
592class Write_after_input_sections_task : public Task
593{
594 public:
27bc2bce 595 Write_after_input_sections_task(Layout* layout, Output_file* of,
730cdc88
ILT
596 Task_token* input_sections_blocker,
597 Task_token* final_blocker)
598 : layout_(layout), of_(of),
599 input_sections_blocker_(input_sections_blocker),
600 final_blocker_(final_blocker)
601 { }
602
603 // The standard Task methods.
604
605 Is_runnable_type
606 is_runnable(Workqueue*);
607
608 Task_locker*
609 locks(Workqueue*);
610
611 void
612 run(Workqueue*);
613
c7912668
ILT
614 std::string
615 get_name() const
616 { return "Write_after_input_sections_task"; }
617
730cdc88
ILT
618 private:
619 class Write_sections_locker;
620
27bc2bce 621 Layout* layout_;
730cdc88
ILT
622 Output_file* of_;
623 Task_token* input_sections_blocker_;
624 Task_token* final_blocker_;
625};
626
92e059d8 627// This task function handles closing the file.
61ba1cf9 628
92e059d8 629class Close_task_runner : public Task_function_runner
61ba1cf9
ILT
630{
631 public:
92e059d8
ILT
632 Close_task_runner(Output_file* of)
633 : of_(of)
61ba1cf9
ILT
634 { }
635
92e059d8 636 // Run the operation.
61ba1cf9
ILT
637 void
638 run(Workqueue*);
639
640 private:
641 Output_file* of_;
a2fb1b05
ILT
642};
643
ead1e424
ILT
644// A small helper function to align an address.
645
646inline uint64_t
647align_address(uint64_t address, uint64_t addralign)
648{
649 if (addralign != 0)
650 address = (address + addralign - 1) &~ (addralign - 1);
651 return address;
652}
653
a2fb1b05
ILT
654} // End namespace gold.
655
656#endif // !defined(GOLD_LAYOUT_H)
This page took 0.098399 seconds and 4 git commands to generate.