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