OSDN Git Service

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