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