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