Framework for relocation scanning. Implement simple static TLS
[external/binutils.git] / gold / output.h
1 // output.h -- manage the output file for gold   -*- C++ -*-
2
3 #ifndef GOLD_OUTPUT_H
4 #define GOLD_OUTPUT_H
5
6 #include <cassert>
7 #include <list>
8
9 #include "elfcpp.h"
10 #include "layout.h"
11
12 namespace gold
13 {
14
15 class General_options;
16 class Object;
17 class Output_file;
18
19 template<int size, bool big_endian>
20 class Sized_target;
21
22 // An abtract class for data which has to go into the output file.
23
24 class Output_data
25 {
26  public:
27   explicit Output_data(off_t data_size = 0)
28     : address_(0), data_size_(data_size), offset_(-1)
29   { }
30
31   virtual
32   ~Output_data();
33
34   // Return the address.
35   uint64_t
36   address() const
37   { return this->address_; }
38
39   // Return the size of the data.
40   off_t
41   data_size() const
42   { return this->data_size_; }
43
44   // Return the file offset.
45   off_t
46   offset() const
47   { return this->offset_; }
48
49   // Return the required alignment.
50   uint64_t
51   addralign() const
52   { return this->do_addralign(); }
53
54   // Return whether this is an Output_section.
55   bool
56   is_section() const
57   { return this->do_is_section(); }
58
59   // Return whether this is an Output_section of the specified type.
60   bool
61   is_section_type(elfcpp::Elf_Word stt) const
62   { return this->do_is_section_type(stt); }
63
64   // Return whether this is an Output_section with the specified flag
65   // set.
66   bool
67   is_section_flag_set(elfcpp::Elf_Xword shf) const
68   { return this->do_is_section_flag_set(shf); }
69
70   // Set the address and file offset of this data.
71   void
72   set_address(uint64_t addr, off_t off);
73
74   // Write the data to the output file.
75   void
76   write(Output_file* file)
77   { this->do_write(file); }
78
79  protected:
80   // Functions that child classes may or in some cases must implement.
81
82   // Write the data to the output file.
83   virtual void
84   do_write(Output_file*) = 0;
85
86   // Return the required alignment.
87   virtual uint64_t
88   do_addralign() const = 0;
89
90   // Return whether this is an Output_section.
91   virtual bool
92   do_is_section() const
93   { return false; }
94
95   // Return whether this is an Output_section of the specified type.
96   // This only needs to be implement by Output_section.
97   virtual bool
98   do_is_section_type(elfcpp::Elf_Word) const
99   { return false; }
100
101   // Return whether this is an Output_section with the specific flag
102   // set.  This only needs to be implemented by Output_section.
103   virtual bool
104   do_is_section_flag_set(elfcpp::Elf_Xword) const
105   { return false; }
106
107   // Set the address and file offset of the data.  This only needs to
108   // be implemented if the child needs to know.
109   virtual void
110   do_set_address(uint64_t, off_t)
111   { }
112
113   // Functions that child classes may call.
114
115   // Set the size of the data.
116   void
117   set_data_size(off_t data_size)
118   { this->data_size_ = data_size; }
119
120   // Return default alignment for a size--32 or 64.
121   static uint64_t
122   default_alignment(int size);
123
124  private:
125   Output_data(const Output_data&);
126   Output_data& operator=(const Output_data&);
127
128   // Memory address in file (not always meaningful).
129   uint64_t address_;
130   // Size of data in file.
131   off_t data_size_;
132   // Offset within file.
133   off_t offset_;
134 };
135
136 // A simple case of Output_data in which we have constant data to
137 // output.
138
139 class Output_data_const : public Output_data
140 {
141  public:
142   Output_data_const(const std::string& data, uint64_t addralign)
143     : Output_data(data.size()), data_(data), addralign_(addralign)
144   { }
145
146   Output_data_const(const char* p, off_t len, uint64_t addralign)
147     : Output_data(len), data_(p, len), addralign_(addralign)
148   { }
149
150   // Write the data to the file.
151   void
152   do_write(Output_file* output);
153
154   // Return the required alignment.
155   uint64_t
156   do_addralign() const
157   { return this->addralign_; }
158
159  private:
160   std::string data_;
161   uint64_t addralign_;
162 };
163
164 // Output the section headers.
165
166 class Output_section_headers : public Output_data
167 {
168  public:
169   Output_section_headers(int size,
170                          bool big_endian,
171                          const Layout::Segment_list&,
172                          const Layout::Section_list&,
173                          const Stringpool*);
174
175   // Write the data to the file.
176   void
177   do_write(Output_file*);
178
179   // Return the required alignment.
180   uint64_t
181   do_addralign() const
182   { return Output_data::default_alignment(this->size_); }
183
184  private:
185   // Write the data to the file with the right size and endianness.
186   template<int size, bool big_endian>
187   void
188   do_sized_write(Output_file*);
189
190   int size_;
191   bool big_endian_;
192   const Layout::Segment_list& segment_list_;
193   const Layout::Section_list& section_list_;
194   const Stringpool* secnamepool_;
195 };
196
197 // Output the segment headers.
198
199 class Output_segment_headers : public Output_data
200 {
201  public:
202   Output_segment_headers(int size, bool big_endian,
203                          const Layout::Segment_list& segment_list);
204
205   // Write the data to the file.
206   void
207   do_write(Output_file*);
208
209   // Return the required alignment.
210   uint64_t
211   do_addralign() const
212   { return Output_data::default_alignment(this->size_); }
213
214  private:
215   // Write the data to the file with the right size and endianness.
216   template<int size, bool big_endian>
217   void
218   do_sized_write(Output_file*);
219
220   int size_;
221   bool big_endian_;
222   const Layout::Segment_list& segment_list_;
223 };
224
225 // Output the ELF file header.
226
227 class Output_file_header : public Output_data
228 {
229  public:
230   Output_file_header(int size,
231                      bool big_endian,
232                      const General_options&,
233                      const Target*,
234                      const Symbol_table*,
235                      const Output_segment_headers*);
236
237   // Add information about the section headers.  We lay out the ELF
238   // file header before we create the section headers.
239   void set_section_info(const Output_section_headers*,
240                         const Output_section* shstrtab);
241
242   // Write the data to the file.
243   void
244   do_write(Output_file*);
245
246   // Return the required alignment.
247   uint64_t
248   do_addralign() const
249   { return Output_data::default_alignment(this->size_); }
250
251   // Set the address and offset--we only implement this for error
252   // checking.
253   void
254   do_set_address(uint64_t, off_t off) const
255   { assert(off == 0); }
256
257  private:
258   // Write the data to the file with the right size and endianness.
259   template<int size, bool big_endian>
260   void
261   do_sized_write(Output_file*);
262
263   int size_;
264   bool big_endian_;
265   const General_options& options_;
266   const Target* target_;
267   const Symbol_table* symtab_;
268   const Output_segment_headers* segment_header_;
269   const Output_section_headers* section_header_;
270   const Output_section* shstrtab_;
271 };
272
273 // An output section.  We don't expect to have too many output
274 // sections, so we don't bother to do a template on the size.
275
276 class Output_section : public Output_data
277 {
278  public:
279   // Create an output section, giving the name, type, and flags.
280   Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword,
281                  unsigned int shndx);
282   virtual ~Output_section();
283
284   // Add a new input section named NAME with header SHDR from object
285   // OBJECT.  Return the offset within the output section.
286   template<int size, bool big_endian>
287   off_t
288   add_input_section(Object* object, const char *name,
289                     const elfcpp::Shdr<size, big_endian>& shdr);
290
291   // Return the section name.
292   const char*
293   name() const
294   { return this->name_; }
295
296   // Return the section type.
297   elfcpp::Elf_Word
298   type() const
299   { return this->type_; }
300
301   // Return the section flags.
302   elfcpp::Elf_Xword
303   flags() const
304   { return this->flags_; }
305
306   // Return the address alignment.
307   uint64_t
308   addralign() const
309   { return this->addralign_; }
310
311   // Return the section index.
312   unsigned int
313   shndx() const
314   { return this->shndx_; }
315
316   // Set the entsize field.
317   void
318   set_entsize(uint64_t v)
319   { this->entsize_ = v; }
320
321   // Set the link field.
322   void
323   set_link(unsigned int v)
324   { this->link_ = v; }
325
326   // Set the info field.
327   void
328   set_info(unsigned int v)
329   { this->info_ = v; }
330
331   // Set the addralign field.
332   void
333   set_addralign(uint64_t v)
334   { this->addralign_ = v; }
335
336   // Write the data to the file.  For a typical Output_section, this
337   // does nothing.  We write out the data by looping over all the
338   // input sections.
339   virtual void
340   do_write(Output_file*)
341   { }
342
343   // Return the address alignment--function required by parent class.
344   uint64_t
345   do_addralign() const
346   { return this->addralign_; }
347
348   // Return whether this is an Output_section.
349   bool
350   do_is_section() const
351   { return true; }
352
353   // Return whether this is a section of the specified type.
354   bool
355   do_is_section_type(elfcpp::Elf_Word type) const
356   { return this->type_ == type; }
357
358   // Return whether the specified section flag is set.
359   bool
360   do_is_section_flag_set(elfcpp::Elf_Xword flag) const
361   { return (this->flags_ & flag) != 0; }
362
363   // Write the section header into *OPHDR.
364   template<int size, bool big_endian>
365   void
366   write_header(const Stringpool*, elfcpp::Shdr_write<size, big_endian>*) const;
367
368  private:
369   // Most of these fields are only valid after layout.
370
371   // The name of the section.  This will point into a Stringpool.
372   const char* name_;
373   // The section address is in the parent class.
374   // The section alignment.
375   uint64_t addralign_;
376   // The section entry size.
377   uint64_t entsize_;
378   // The file offset is in the parent class.
379   // The section link field.
380   unsigned int link_;
381   // The section info field.
382   unsigned int info_;
383   // The section type.
384   elfcpp::Elf_Word type_;
385   // The section flags.
386   elfcpp::Elf_Xword flags_;
387   // The section index.
388   unsigned int shndx_;
389 };
390
391 // A special Output_section which represents the symbol table
392 // (SHT_SYMTAB).
393
394 class Output_section_symtab : public Output_section
395 {
396  public:
397   Output_section_symtab(const char* name, off_t size, unsigned int shndx);
398 };
399
400 // A special Output_section which holds a string table.
401
402 class Output_section_strtab : public Output_section
403 {
404  public:
405   Output_section_strtab(const char* name, Stringpool* contents,
406                         unsigned int shndx);
407
408   // Write out the data.
409   void
410   do_write(Output_file*);
411
412  private:
413   Stringpool* contents_;
414 };
415
416 // An output segment.  PT_LOAD segments are built from collections of
417 // output sections.  Other segments typically point within PT_LOAD
418 // segments, and are built directly as needed.
419
420 class Output_segment
421 {
422  public:
423   // Create an output segment, specifying the type and flags.
424   Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
425
426   // Return the virtual address.
427   uint64_t
428   vaddr() const
429   { return this->vaddr_; }
430
431   // Return the physical address.
432   uint64_t
433   paddr() const
434   { return this->paddr_; }
435
436   // Return the segment type.
437   elfcpp::Elf_Word
438   type() const
439   { return this->type_; }
440
441   // Return the segment flags.
442   elfcpp::Elf_Word
443   flags() const
444   { return this->flags_; }
445
446   // Return the memory size.
447   uint64_t
448   memsz() const
449   { return this->memsz_; }
450
451   // Return the maximum alignment of the Output_data.
452   uint64_t
453   max_data_align() const;
454
455   // Add an Output_section to this segment.
456   void
457   add_output_section(Output_section*, elfcpp::Elf_Word seg_flags);
458
459   // Add an Output_data (which is not an Output_section) to the start
460   // of this segment.
461   void
462   add_initial_output_data(Output_data*);
463
464   // Set the address of the segment to ADDR and the offset to *POFF
465   // (aligned if necessary), and set the addresses and offsets of all
466   // contained output sections accordingly.  Return the address of the
467   // immediately following segment.  Update *POFF.  This should only
468   // be called for a PT_LOAD segment.
469   uint64_t
470   set_section_addresses(uint64_t addr, off_t* poff);
471
472   // Set the offset of this segment based on the section.  This should
473   // only be called for a non-PT_LOAD segment.
474   void
475   set_offset();
476
477   // Return the number of output sections.
478   unsigned int
479   output_section_count() const;
480
481   // Write the segment header into *OPHDR.
482   template<int size, bool big_endian>
483   void
484   write_header(elfcpp::Phdr_write<size, big_endian>*) const;
485
486   // Write the section headers of associated sections into V.
487   template<int size, bool big_endian>
488   unsigned char*
489   write_section_headers(const Stringpool*,
490                         unsigned char* v ACCEPT_SIZE_ENDIAN) const;
491
492  private:
493   Output_segment(const Output_segment&);
494   Output_segment& operator=(const Output_segment&);
495
496   typedef std::list<Output_data*> Output_data_list;
497
498   // Set the section addresses in an Output_data_list.
499   uint64_t
500   set_section_list_addresses(Output_data_list*, uint64_t addr, off_t* poff);
501
502   // Return the number of Output_sections in an Output_data_list.
503   unsigned int
504   output_section_count_list(const Output_data_list*) const;
505
506   // Write the section headers in the list into V.
507   template<int size, bool big_endian>
508   unsigned char*
509   write_section_headers_list(const Stringpool*, const Output_data_list*,
510                              unsigned char* v ACCEPT_SIZE_ENDIAN) const;
511
512   // The list of output data with contents attached to this segment.
513   Output_data_list output_data_;
514   // The list of output data without contents attached to this segment.
515   Output_data_list output_bss_;
516   // The segment virtual address.
517   uint64_t vaddr_;
518   // The segment physical address.
519   uint64_t paddr_;
520   // The size of the segment in memory.
521   uint64_t memsz_;
522   // The segment alignment.
523   uint64_t align_;
524   // The offset of the segment data within the file.
525   off_t offset_;
526   // The size of the segment data in the file.
527   off_t filesz_;
528   // The segment type;
529   elfcpp::Elf_Word type_;
530   // The segment flags.
531   elfcpp::Elf_Word flags_;
532 };
533
534 // This class represents the output file.
535
536 class Output_file
537 {
538  public:
539   Output_file(const General_options& options);
540
541   // Open the output file.  FILE_SIZE is the final size of the file.
542   void
543   open(off_t file_size);
544
545   // Close the output file and make sure there are no error.
546   void
547   close();
548
549   // We currently always use mmap which makes the view handling quite
550   // simple.  In the future we may support other approaches.
551
552   // Write data to the output file.
553   void
554   write(off_t offset, const void* data, off_t len)
555   { memcpy(this->base_ + offset, data, len); }
556
557   // Get a buffer to use to write to the file, given the offset into
558   // the file and the size.
559   unsigned char*
560   get_output_view(off_t start, off_t size)
561   {
562     assert(start >= 0 && size >= 0 && start + size <= this->file_size_);
563     return this->base_ + start;
564   }
565
566   // VIEW must have been returned by get_output_view.  Write the
567   // buffer to the file, passing in the offset and the size.
568   void
569   write_output_view(off_t, off_t, unsigned char*)
570   { }
571
572  private:
573   // General options.
574   const General_options& options_;
575   // File name.
576   const char* name_;
577   // File descriptor.
578   int o_;
579   // File size.
580   off_t file_size_;
581   // Base of file mapped into memory.
582   unsigned char* base_;
583 };
584
585 } // End namespace gold.
586
587 #endif // !defined(GOLD_OUTPUT_H)