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