]> Git Repo - binutils.git/blob - gold/output.h
377864cddfd2428f459ef5046e22d859587ae073
[binutils.git] / gold / output.h
1 // output.h -- manage the output file for gold   -*- C++ -*-
2
3 // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <[email protected]>.
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_OUTPUT_H
24 #define GOLD_OUTPUT_H
25
26 #include <list>
27 #include <vector>
28
29 #include "elfcpp.h"
30 #include "mapfile.h"
31 #include "layout.h"
32 #include "reloc-types.h"
33
34 namespace gold
35 {
36
37 class General_options;
38 class Object;
39 class Symbol;
40 class Output_file;
41 class Output_section;
42 class Relocatable_relocs;
43 class Target;
44 template<int size, bool big_endian>
45 class Sized_target;
46 template<int size, bool big_endian>
47 class Sized_relobj;
48
49 // An abtract class for data which has to go into the output file.
50
51 class Output_data
52 {
53  public:
54   explicit Output_data()
55     : address_(0), data_size_(0), offset_(-1),
56       is_address_valid_(false), is_data_size_valid_(false),
57       is_offset_valid_(false), is_data_size_fixed_(false),
58       dynamic_reloc_count_(0)
59   { }
60
61   virtual
62   ~Output_data();
63
64   // Return the address.  For allocated sections, this is only valid
65   // after Layout::finalize is finished.
66   uint64_t
67   address() const
68   {
69     gold_assert(this->is_address_valid_);
70     return this->address_;
71   }
72
73   // Return the size of the data.  For allocated sections, this must
74   // be valid after Layout::finalize calls set_address, but need not
75   // be valid before then.
76   off_t
77   data_size() const
78   {
79     gold_assert(this->is_data_size_valid_);
80     return this->data_size_;
81   }
82
83   // Return true if data size is fixed.
84   bool
85   is_data_size_fixed() const
86   { return this->is_data_size_fixed_; }
87   
88   // Return the file offset.  This is only valid after
89   // Layout::finalize is finished.  For some non-allocated sections,
90   // it may not be valid until near the end of the link.
91   off_t
92   offset() const
93   {
94     gold_assert(this->is_offset_valid_);
95     return this->offset_;
96   }
97
98   // Reset the address and file offset.  This essentially disables the
99   // sanity testing about duplicate and unknown settings.
100   void
101   reset_address_and_file_offset()
102   {
103     this->is_address_valid_ = false;
104     this->is_offset_valid_ = false;
105     if (!this->is_data_size_fixed_)
106       this->is_data_size_valid_ = false;
107     this->do_reset_address_and_file_offset();
108   }
109
110   // Return true if address and file offset already have reset values. In
111   // other words, calling reset_address_and_file_offset will not change them.
112   bool
113   address_and_file_offset_have_reset_values() const
114   { return this->do_address_and_file_offset_have_reset_values(); }
115
116   // Return the required alignment.
117   uint64_t
118   addralign() const
119   { return this->do_addralign(); }
120
121   // Return whether this has a load address.
122   bool
123   has_load_address() const
124   { return this->do_has_load_address(); }
125
126   // Return the load address.
127   uint64_t
128   load_address() const
129   { return this->do_load_address(); }
130
131   // Return whether this is an Output_section.
132   bool
133   is_section() const
134   { return this->do_is_section(); }
135
136   // Return whether this is an Output_section of the specified type.
137   bool
138   is_section_type(elfcpp::Elf_Word stt) const
139   { return this->do_is_section_type(stt); }
140
141   // Return whether this is an Output_section with the specified flag
142   // set.
143   bool
144   is_section_flag_set(elfcpp::Elf_Xword shf) const
145   { return this->do_is_section_flag_set(shf); }
146
147   // Return the output section that this goes in, if there is one.
148   Output_section*
149   output_section()
150   { return this->do_output_section(); }
151
152   // Return the output section index, if there is an output section.
153   unsigned int
154   out_shndx() const
155   { return this->do_out_shndx(); }
156
157   // Set the output section index, if this is an output section.
158   void
159   set_out_shndx(unsigned int shndx)
160   { this->do_set_out_shndx(shndx); }
161
162   // Set the address and file offset of this data, and finalize the
163   // size of the data.  This is called during Layout::finalize for
164   // allocated sections.
165   void
166   set_address_and_file_offset(uint64_t addr, off_t off)
167   {
168     this->set_address(addr);
169     this->set_file_offset(off);
170     this->finalize_data_size();
171   }
172
173   // Set the address.
174   void
175   set_address(uint64_t addr)
176   {
177     gold_assert(!this->is_address_valid_);
178     this->address_ = addr;
179     this->is_address_valid_ = true;
180   }
181
182   // Set the file offset.
183   void
184   set_file_offset(off_t off)
185   {
186     gold_assert(!this->is_offset_valid_);
187     this->offset_ = off;
188     this->is_offset_valid_ = true;
189   }
190
191   // Finalize the data size.
192   void
193   finalize_data_size()
194   {
195     if (!this->is_data_size_valid_)
196       {
197         // Tell the child class to set the data size.
198         this->set_final_data_size();
199         gold_assert(this->is_data_size_valid_);
200       }
201   }
202
203   // Set the TLS offset.  Called only for SHT_TLS sections.
204   void
205   set_tls_offset(uint64_t tls_base)
206   { this->do_set_tls_offset(tls_base); }
207
208   // Return the TLS offset, relative to the base of the TLS segment.
209   // Valid only for SHT_TLS sections.
210   uint64_t
211   tls_offset() const
212   { return this->do_tls_offset(); }
213
214   // Write the data to the output file.  This is called after
215   // Layout::finalize is complete.
216   void
217   write(Output_file* file)
218   { this->do_write(file); }
219
220   // This is called by Layout::finalize to note that the sizes of
221   // allocated sections must now be fixed.
222   static void
223   layout_complete()
224   { Output_data::allocated_sizes_are_fixed = true; }
225
226   // Used to check that layout has been done.
227   static bool
228   is_layout_complete()
229   { return Output_data::allocated_sizes_are_fixed; }
230
231   // Count the number of dynamic relocations applied to this section.
232   void
233   add_dynamic_reloc()
234   { ++this->dynamic_reloc_count_; }
235
236   // Return the number of dynamic relocations applied to this section.
237   unsigned int
238   dynamic_reloc_count() const
239   { return this->dynamic_reloc_count_; }
240
241   // Whether the address is valid.
242   bool
243   is_address_valid() const
244   { return this->is_address_valid_; }
245
246   // Whether the file offset is valid.
247   bool
248   is_offset_valid() const
249   { return this->is_offset_valid_; }
250
251   // Whether the data size is valid.
252   bool
253   is_data_size_valid() const
254   { return this->is_data_size_valid_; }
255
256   // Print information to the map file.
257   void
258   print_to_mapfile(Mapfile* mapfile) const
259   { return this->do_print_to_mapfile(mapfile); }
260
261  protected:
262   // Functions that child classes may or in some cases must implement.
263
264   // Write the data to the output file.
265   virtual void
266   do_write(Output_file*) = 0;
267
268   // Return the required alignment.
269   virtual uint64_t
270   do_addralign() const = 0;
271
272   // Return whether this has a load address.
273   virtual bool
274   do_has_load_address() const
275   { return false; }
276
277   // Return the load address.
278   virtual uint64_t
279   do_load_address() const
280   { gold_unreachable(); }
281
282   // Return whether this is an Output_section.
283   virtual bool
284   do_is_section() const
285   { return false; }
286
287   // Return whether this is an Output_section of the specified type.
288   // This only needs to be implement by Output_section.
289   virtual bool
290   do_is_section_type(elfcpp::Elf_Word) const
291   { return false; }
292
293   // Return whether this is an Output_section with the specific flag
294   // set.  This only needs to be implemented by Output_section.
295   virtual bool
296   do_is_section_flag_set(elfcpp::Elf_Xword) const
297   { return false; }
298
299   // Return the output section, if there is one.
300   virtual Output_section*
301   do_output_section()
302   { return NULL; }
303
304   // Return the output section index, if there is an output section.
305   virtual unsigned int
306   do_out_shndx() const
307   { gold_unreachable(); }
308
309   // Set the output section index, if this is an output section.
310   virtual void
311   do_set_out_shndx(unsigned int)
312   { gold_unreachable(); }
313
314   // This is a hook for derived classes to set the data size.  This is
315   // called by finalize_data_size, normally called during
316   // Layout::finalize, when the section address is set.
317   virtual void
318   set_final_data_size()
319   { gold_unreachable(); }
320
321   // A hook for resetting the address and file offset.
322   virtual void
323   do_reset_address_and_file_offset()
324   { }
325
326   // Return true if address and file offset already have reset values. In
327   // other words, calling reset_address_and_file_offset will not change them.
328   // A child class overriding do_reset_address_and_file_offset may need to
329   // also override this.
330   virtual bool
331   do_address_and_file_offset_have_reset_values() const
332   { return !this->is_address_valid_ && !this->is_offset_valid_; }
333
334   // Set the TLS offset.  Called only for SHT_TLS sections.
335   virtual void
336   do_set_tls_offset(uint64_t)
337   { gold_unreachable(); }
338
339   // Return the TLS offset, relative to the base of the TLS segment.
340   // Valid only for SHT_TLS sections.
341   virtual uint64_t
342   do_tls_offset() const
343   { gold_unreachable(); }
344
345   // Print to the map file.  This only needs to be implemented by
346   // classes which may appear in a PT_LOAD segment.
347   virtual void
348   do_print_to_mapfile(Mapfile*) const
349   { gold_unreachable(); }
350
351   // Functions that child classes may call.
352
353   // Reset the address.  The Output_section class needs this when an
354   // SHF_ALLOC input section is added to an output section which was
355   // formerly not SHF_ALLOC.
356   void
357   mark_address_invalid()
358   { this->is_address_valid_ = false; }
359
360   // Set the size of the data.
361   void
362   set_data_size(off_t data_size)
363   {
364     gold_assert(!this->is_data_size_valid_
365                 && !this->is_data_size_fixed_);
366     this->data_size_ = data_size;
367     this->is_data_size_valid_ = true;
368   }
369
370   // Fix the data size.  Once it is fixed, it cannot be changed
371   // and the data size remains always valid. 
372   void
373   fix_data_size()
374   {
375     gold_assert(this->is_data_size_valid_);
376     this->is_data_size_fixed_ = true;
377   }
378
379   // Get the current data size--this is for the convenience of
380   // sections which build up their size over time.
381   off_t
382   current_data_size_for_child() const
383   { return this->data_size_; }
384
385   // Set the current data size--this is for the convenience of
386   // sections which build up their size over time.
387   void
388   set_current_data_size_for_child(off_t data_size)
389   {
390     gold_assert(!this->is_data_size_valid_);
391     this->data_size_ = data_size;
392   }
393
394   // Return default alignment for the target size.
395   static uint64_t
396   default_alignment();
397
398   // Return default alignment for a specified size--32 or 64.
399   static uint64_t
400   default_alignment_for_size(int size);
401
402  private:
403   Output_data(const Output_data&);
404   Output_data& operator=(const Output_data&);
405
406   // This is used for verification, to make sure that we don't try to
407   // change any sizes of allocated sections after we set the section
408   // addresses.
409   static bool allocated_sizes_are_fixed;
410
411   // Memory address in output file.
412   uint64_t address_;
413   // Size of data in output file.
414   off_t data_size_;
415   // File offset of contents in output file.
416   off_t offset_;
417   // Whether address_ is valid.
418   bool is_address_valid_;
419   // Whether data_size_ is valid.
420   bool is_data_size_valid_;
421   // Whether offset_ is valid.
422   bool is_offset_valid_;
423   // Whether data size is fixed.
424   bool is_data_size_fixed_;
425   // Count of dynamic relocations applied to this section.
426   unsigned int dynamic_reloc_count_;
427 };
428
429 // Output the section headers.
430
431 class Output_section_headers : public Output_data
432 {
433  public:
434   Output_section_headers(const Layout*,
435                          const Layout::Segment_list*,
436                          const Layout::Section_list*,
437                          const Layout::Section_list*,
438                          const Stringpool*,
439                          const Output_section*);
440
441  protected:
442   // Write the data to the file.
443   void
444   do_write(Output_file*);
445
446   // Return the required alignment.
447   uint64_t
448   do_addralign() const
449   { return Output_data::default_alignment(); }
450
451   // Write to a map file.
452   void
453   do_print_to_mapfile(Mapfile* mapfile) const
454   { mapfile->print_output_data(this, _("** section headers")); }
455
456   // Set final data size.
457   void
458   set_final_data_size()
459   { this->set_data_size(this->do_size()); }
460
461  private:
462   // Write the data to the file with the right size and endianness.
463   template<int size, bool big_endian>
464   void
465   do_sized_write(Output_file*);
466
467   // Compute data size.
468   off_t
469   do_size() const;
470
471   const Layout* layout_;
472   const Layout::Segment_list* segment_list_;
473   const Layout::Section_list* section_list_;
474   const Layout::Section_list* unattached_section_list_;
475   const Stringpool* secnamepool_;
476   const Output_section* shstrtab_section_;
477 };
478
479 // Output the segment headers.
480
481 class Output_segment_headers : public Output_data
482 {
483  public:
484   Output_segment_headers(const Layout::Segment_list& segment_list);
485
486  protected:
487   // Write the data to the file.
488   void
489   do_write(Output_file*);
490
491   // Return the required alignment.
492   uint64_t
493   do_addralign() const
494   { return Output_data::default_alignment(); }
495
496   // Write to a map file.
497   void
498   do_print_to_mapfile(Mapfile* mapfile) const
499   { mapfile->print_output_data(this, _("** segment headers")); }
500
501   // Set final data size.
502   void
503   set_final_data_size()
504   { this->set_data_size(this->do_size()); }
505
506  private:
507   // Write the data to the file with the right size and endianness.
508   template<int size, bool big_endian>
509   void
510   do_sized_write(Output_file*);
511
512   // Compute the current size.
513   off_t
514   do_size() const;
515
516   const Layout::Segment_list& segment_list_;
517 };
518
519 // Output the ELF file header.
520
521 class Output_file_header : public Output_data
522 {
523  public:
524   Output_file_header(const Target*,
525                      const Symbol_table*,
526                      const Output_segment_headers*,
527                      const char* entry);
528
529   // Add information about the section headers.  We lay out the ELF
530   // file header before we create the section headers.
531   void set_section_info(const Output_section_headers*,
532                         const Output_section* shstrtab);
533
534  protected:
535   // Write the data to the file.
536   void
537   do_write(Output_file*);
538
539   // Return the required alignment.
540   uint64_t
541   do_addralign() const
542   { return Output_data::default_alignment(); }
543
544   // Write to a map file.
545   void
546   do_print_to_mapfile(Mapfile* mapfile) const
547   { mapfile->print_output_data(this, _("** file header")); }
548
549   // Set final data size.
550   void
551   set_final_data_size(void)
552   { this->set_data_size(this->do_size()); }
553
554  private:
555   // Write the data to the file with the right size and endianness.
556   template<int size, bool big_endian>
557   void
558   do_sized_write(Output_file*);
559
560   // Return the value to use for the entry address.
561   template<int size>
562   typename elfcpp::Elf_types<size>::Elf_Addr
563   entry();
564
565   // Compute the current data size.
566   off_t
567   do_size() const;
568
569   const Target* target_;
570   const Symbol_table* symtab_;
571   const Output_segment_headers* segment_header_;
572   const Output_section_headers* section_header_;
573   const Output_section* shstrtab_;
574   const char* entry_;
575 };
576
577 // Output sections are mainly comprised of input sections.  However,
578 // there are cases where we have data to write out which is not in an
579 // input section.  Output_section_data is used in such cases.  This is
580 // an abstract base class.
581
582 class Output_section_data : public Output_data
583 {
584  public:
585   Output_section_data(off_t data_size, uint64_t addralign,
586                       bool is_data_size_fixed)
587     : Output_data(), output_section_(NULL), addralign_(addralign)
588   {
589     this->set_data_size(data_size);
590     if (is_data_size_fixed)
591       this->fix_data_size();
592   }
593
594   Output_section_data(uint64_t addralign)
595     : Output_data(), output_section_(NULL), addralign_(addralign)
596   { }
597
598   // Return the output section.
599   const Output_section*
600   output_section() const
601   { return this->output_section_; }
602
603   // Record the output section.
604   void
605   set_output_section(Output_section* os);
606
607   // Add an input section, for SHF_MERGE sections.  This returns true
608   // if the section was handled.
609   bool
610   add_input_section(Relobj* object, unsigned int shndx)
611   { return this->do_add_input_section(object, shndx); }
612
613   // Given an input OBJECT, an input section index SHNDX within that
614   // object, and an OFFSET relative to the start of that input
615   // section, return whether or not the corresponding offset within
616   // the output section is known.  If this function returns true, it
617   // sets *POUTPUT to the output offset.  The value -1 indicates that
618   // this input offset is being discarded.
619   bool
620   output_offset(const Relobj* object, unsigned int shndx,
621                 section_offset_type offset,
622                 section_offset_type *poutput) const
623   { return this->do_output_offset(object, shndx, offset, poutput); }
624
625   // Return whether this is the merge section for the input section
626   // SHNDX in OBJECT.  This should return true when output_offset
627   // would return true for some values of OFFSET.
628   bool
629   is_merge_section_for(const Relobj* object, unsigned int shndx) const
630   { return this->do_is_merge_section_for(object, shndx); }
631
632   // Write the contents to a buffer.  This is used for sections which
633   // require postprocessing, such as compression.
634   void
635   write_to_buffer(unsigned char* buffer)
636   { this->do_write_to_buffer(buffer); }
637
638   // Print merge stats to stderr.  This should only be called for
639   // SHF_MERGE sections.
640   void
641   print_merge_stats(const char* section_name)
642   { this->do_print_merge_stats(section_name); }
643
644  protected:
645   // The child class must implement do_write.
646
647   // The child class may implement specific adjustments to the output
648   // section.
649   virtual void
650   do_adjust_output_section(Output_section*)
651   { }
652
653   // May be implemented by child class.  Return true if the section
654   // was handled.
655   virtual bool
656   do_add_input_section(Relobj*, unsigned int)
657   { gold_unreachable(); }
658
659   // The child class may implement output_offset.
660   virtual bool
661   do_output_offset(const Relobj*, unsigned int, section_offset_type,
662                    section_offset_type*) const
663   { return false; }
664
665   // The child class may implement is_merge_section_for.
666   virtual bool
667   do_is_merge_section_for(const Relobj*, unsigned int) const
668   { return false; }
669
670   // The child class may implement write_to_buffer.  Most child
671   // classes can not appear in a compressed section, and they do not
672   // implement this.
673   virtual void
674   do_write_to_buffer(unsigned char*)
675   { gold_unreachable(); }
676
677   // Print merge statistics.
678   virtual void
679   do_print_merge_stats(const char*)
680   { gold_unreachable(); }
681
682   // Return the required alignment.
683   uint64_t
684   do_addralign() const
685   { return this->addralign_; }
686
687   // Return the output section.
688   Output_section*
689   do_output_section()
690   { return this->output_section_; }
691
692   // Return the section index of the output section.
693   unsigned int
694   do_out_shndx() const;
695
696   // Set the alignment.
697   void
698   set_addralign(uint64_t addralign);
699
700  private:
701   // The output section for this section.
702   Output_section* output_section_;
703   // The required alignment.
704   uint64_t addralign_;
705 };
706
707 // Some Output_section_data classes build up their data step by step,
708 // rather than all at once.  This class provides an interface for
709 // them.
710
711 class Output_section_data_build : public Output_section_data
712 {
713  public:
714   Output_section_data_build(uint64_t addralign)
715     : Output_section_data(addralign)
716   { }
717
718   // Get the current data size.
719   off_t
720   current_data_size() const
721   { return this->current_data_size_for_child(); }
722
723   // Set the current data size.
724   void
725   set_current_data_size(off_t data_size)
726   { this->set_current_data_size_for_child(data_size); }
727
728  protected:
729   // Set the final data size.
730   virtual void
731   set_final_data_size()
732   { this->set_data_size(this->current_data_size_for_child()); }
733 };
734
735 // A simple case of Output_data in which we have constant data to
736 // output.
737
738 class Output_data_const : public Output_section_data
739 {
740  public:
741   Output_data_const(const std::string& data, uint64_t addralign)
742     : Output_section_data(data.size(), addralign, true), data_(data)
743   { }
744
745   Output_data_const(const char* p, off_t len, uint64_t addralign)
746     : Output_section_data(len, addralign, true), data_(p, len)
747   { }
748
749   Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
750     : Output_section_data(len, addralign, true),
751       data_(reinterpret_cast<const char*>(p), len)
752   { }
753
754  protected:
755   // Write the data to the output file.
756   void
757   do_write(Output_file*);
758
759   // Write the data to a buffer.
760   void
761   do_write_to_buffer(unsigned char* buffer)
762   { memcpy(buffer, this->data_.data(), this->data_.size()); }
763
764   // Write to a map file.
765   void
766   do_print_to_mapfile(Mapfile* mapfile) const
767   { mapfile->print_output_data(this, _("** fill")); }
768
769  private:
770   std::string data_;
771 };
772
773 // Another version of Output_data with constant data, in which the
774 // buffer is allocated by the caller.
775
776 class Output_data_const_buffer : public Output_section_data
777 {
778  public:
779   Output_data_const_buffer(const unsigned char* p, off_t len,
780                            uint64_t addralign, const char* map_name)
781     : Output_section_data(len, addralign, true),
782       p_(p), map_name_(map_name)
783   { }
784
785  protected:
786   // Write the data the output file.
787   void
788   do_write(Output_file*);
789
790   // Write the data to a buffer.
791   void
792   do_write_to_buffer(unsigned char* buffer)
793   { memcpy(buffer, this->p_, this->data_size()); }
794
795   // Write to a map file.
796   void
797   do_print_to_mapfile(Mapfile* mapfile) const
798   { mapfile->print_output_data(this, _(this->map_name_)); }
799
800  private:
801   // The data to output.
802   const unsigned char* p_;
803   // Name to use in a map file.  Maps are a rarely used feature, but
804   // the space usage is minor as aren't very many of these objects.
805   const char* map_name_;
806 };
807
808 // A place holder for a fixed amount of data written out via some
809 // other mechanism.
810
811 class Output_data_fixed_space : public Output_section_data
812 {
813  public:
814   Output_data_fixed_space(off_t data_size, uint64_t addralign,
815                           const char* map_name)
816     : Output_section_data(data_size, addralign, true),
817       map_name_(map_name)
818   { }
819
820  protected:
821   // Write out the data--the actual data must be written out
822   // elsewhere.
823   void
824   do_write(Output_file*)
825   { }
826
827   // Write to a map file.
828   void
829   do_print_to_mapfile(Mapfile* mapfile) const
830   { mapfile->print_output_data(this, _(this->map_name_)); }
831
832  private:
833   // Name to use in a map file.  Maps are a rarely used feature, but
834   // the space usage is minor as aren't very many of these objects.
835   const char* map_name_;
836 };
837
838 // A place holder for variable sized data written out via some other
839 // mechanism.
840
841 class Output_data_space : public Output_section_data_build
842 {
843  public:
844   explicit Output_data_space(uint64_t addralign, const char* map_name)
845     : Output_section_data_build(addralign),
846       map_name_(map_name)
847   { }
848
849   // Set the alignment.
850   void
851   set_space_alignment(uint64_t align)
852   { this->set_addralign(align); }
853
854  protected:
855   // Write out the data--the actual data must be written out
856   // elsewhere.
857   void
858   do_write(Output_file*)
859   { }
860
861   // Write to a map file.
862   void
863   do_print_to_mapfile(Mapfile* mapfile) const
864   { mapfile->print_output_data(this, _(this->map_name_)); }
865
866  private:
867   // Name to use in a map file.  Maps are a rarely used feature, but
868   // the space usage is minor as aren't very many of these objects.
869   const char* map_name_;
870 };
871
872 // Fill fixed space with zeroes.  This is just like
873 // Output_data_fixed_space, except that the map name is known.
874
875 class Output_data_zero_fill : public Output_section_data
876 {
877  public:
878   Output_data_zero_fill(off_t data_size, uint64_t addralign)
879     : Output_section_data(data_size, addralign, true)
880   { }
881
882  protected:
883   // There is no data to write out.
884   void
885   do_write(Output_file*)
886   { }
887
888   // Write to a map file.
889   void
890   do_print_to_mapfile(Mapfile* mapfile) const
891   { mapfile->print_output_data(this, "** zero fill"); }
892 };
893
894 // A string table which goes into an output section.
895
896 class Output_data_strtab : public Output_section_data
897 {
898  public:
899   Output_data_strtab(Stringpool* strtab)
900     : Output_section_data(1), strtab_(strtab)
901   { }
902
903  protected:
904   // This is called to set the address and file offset.  Here we make
905   // sure that the Stringpool is finalized.
906   void
907   set_final_data_size();
908
909   // Write out the data.
910   void
911   do_write(Output_file*);
912
913   // Write the data to a buffer.
914   void
915   do_write_to_buffer(unsigned char* buffer)
916   { this->strtab_->write_to_buffer(buffer, this->data_size()); }
917
918   // Write to a map file.
919   void
920   do_print_to_mapfile(Mapfile* mapfile) const
921   { mapfile->print_output_data(this, _("** string table")); }
922
923  private:
924   Stringpool* strtab_;
925 };
926
927 // This POD class is used to represent a single reloc in the output
928 // file.  This could be a private class within Output_data_reloc, but
929 // the templatization is complex enough that I broke it out into a
930 // separate class.  The class is templatized on either elfcpp::SHT_REL
931 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
932 // relocation or an ordinary relocation.
933
934 // A relocation can be against a global symbol, a local symbol, a
935 // local section symbol, an output section, or the undefined symbol at
936 // index 0.  We represent the latter by using a NULL global symbol.
937
938 template<int sh_type, bool dynamic, int size, bool big_endian>
939 class Output_reloc;
940
941 template<bool dynamic, int size, bool big_endian>
942 class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
943 {
944  public:
945   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
946   typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
947
948   static const Address invalid_address = static_cast<Address>(0) - 1;
949
950   // An uninitialized entry.  We need this because we want to put
951   // instances of this class into an STL container.
952   Output_reloc()
953     : local_sym_index_(INVALID_CODE)
954   { }
955
956   // We have a bunch of different constructors.  They come in pairs
957   // depending on how the address of the relocation is specified.  It
958   // can either be an offset in an Output_data or an offset in an
959   // input section.
960
961   // A reloc against a global symbol.
962
963   Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
964                Address address, bool is_relative);
965
966   Output_reloc(Symbol* gsym, unsigned int type,
967                Sized_relobj<size, big_endian>* relobj,
968                unsigned int shndx, Address address, bool is_relative);
969
970   // A reloc against a local symbol or local section symbol.
971
972   Output_reloc(Sized_relobj<size, big_endian>* relobj,
973                unsigned int local_sym_index, unsigned int type,
974                Output_data* od, Address address, bool is_relative,
975                bool is_section_symbol);
976
977   Output_reloc(Sized_relobj<size, big_endian>* relobj,
978                unsigned int local_sym_index, unsigned int type,
979                unsigned int shndx, Address address, bool is_relative,
980                bool is_section_symbol);
981
982   // A reloc against the STT_SECTION symbol of an output section.
983
984   Output_reloc(Output_section* os, unsigned int type, Output_data* od,
985                Address address);
986
987   Output_reloc(Output_section* os, unsigned int type,
988                Sized_relobj<size, big_endian>* relobj,
989                unsigned int shndx, Address address);
990
991   // Return TRUE if this is a RELATIVE relocation.
992   bool
993   is_relative() const
994   { return this->is_relative_; }
995
996   // Return whether this is against a local section symbol.
997   bool
998   is_local_section_symbol() const
999   {
1000     return (this->local_sym_index_ != GSYM_CODE
1001             && this->local_sym_index_ != SECTION_CODE
1002             && this->local_sym_index_ != INVALID_CODE
1003             && this->is_section_symbol_);
1004   }
1005
1006   // For a local section symbol, return the offset of the input
1007   // section within the output section.  ADDEND is the addend being
1008   // applied to the input section.
1009   Address
1010   local_section_offset(Addend addend) const;
1011
1012   // Get the value of the symbol referred to by a Rel relocation when
1013   // we are adding the given ADDEND.
1014   Address
1015   symbol_value(Addend addend) const;
1016
1017   // Write the reloc entry to an output view.
1018   void
1019   write(unsigned char* pov) const;
1020
1021   // Write the offset and info fields to Write_rel.
1022   template<typename Write_rel>
1023   void write_rel(Write_rel*) const;
1024
1025   // This is used when sorting dynamic relocs.  Return -1 to sort this
1026   // reloc before R2, 0 to sort the same as R2, 1 to sort after R2.
1027   int
1028   compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
1029     const;
1030
1031   // Return whether this reloc should be sorted before the argument
1032   // when sorting dynamic relocs.
1033   bool
1034   sort_before(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>&
1035               r2) const
1036   { return this->compare(r2) < 0; }
1037
1038  private:
1039   // Record that we need a dynamic symbol index.
1040   void
1041   set_needs_dynsym_index();
1042
1043   // Return the symbol index.
1044   unsigned int
1045   get_symbol_index() const;
1046
1047   // Return the output address.
1048   Address
1049   get_address() const;
1050
1051   // Codes for local_sym_index_.
1052   enum
1053   {
1054     // Global symbol.
1055     GSYM_CODE = -1U,
1056     // Output section.
1057     SECTION_CODE = -2U,
1058     // Invalid uninitialized entry.
1059     INVALID_CODE = -3U
1060   };
1061
1062   union
1063   {
1064     // For a local symbol or local section symbol
1065     // (this->local_sym_index_ >= 0), the object.  We will never
1066     // generate a relocation against a local symbol in a dynamic
1067     // object; that doesn't make sense.  And our callers will always
1068     // be templatized, so we use Sized_relobj here.
1069     Sized_relobj<size, big_endian>* relobj;
1070     // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
1071     // symbol.  If this is NULL, it indicates a relocation against the
1072     // undefined 0 symbol.
1073     Symbol* gsym;
1074     // For a relocation against an output section
1075     // (this->local_sym_index_ == SECTION_CODE), the output section.
1076     Output_section* os;
1077   } u1_;
1078   union
1079   {
1080     // If this->shndx_ is not INVALID CODE, the object which holds the
1081     // input section being used to specify the reloc address.
1082     Sized_relobj<size, big_endian>* relobj;
1083     // If this->shndx_ is INVALID_CODE, the output data being used to
1084     // specify the reloc address.  This may be NULL if the reloc
1085     // address is absolute.
1086     Output_data* od;
1087   } u2_;
1088   // The address offset within the input section or the Output_data.
1089   Address address_;
1090   // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
1091   // relocation against an output section, or INVALID_CODE for an
1092   // uninitialized value.  Otherwise, for a local symbol
1093   // (this->is_section_symbol_ is false), the local symbol index.  For
1094   // a local section symbol (this->is_section_symbol_ is true), the
1095   // section index in the input file.
1096   unsigned int local_sym_index_;
1097   // The reloc type--a processor specific code.
1098   unsigned int type_ : 30;
1099   // True if the relocation is a RELATIVE relocation.
1100   bool is_relative_ : 1;
1101   // True if the relocation is against a section symbol.
1102   bool is_section_symbol_ : 1;
1103   // If the reloc address is an input section in an object, the
1104   // section index.  This is INVALID_CODE if the reloc address is
1105   // specified in some other way.
1106   unsigned int shndx_;
1107 };
1108
1109 // The SHT_RELA version of Output_reloc<>.  This is just derived from
1110 // the SHT_REL version of Output_reloc, but it adds an addend.
1111
1112 template<bool dynamic, int size, bool big_endian>
1113 class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1114 {
1115  public:
1116   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1117   typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
1118
1119   // An uninitialized entry.
1120   Output_reloc()
1121     : rel_()
1122   { }
1123
1124   // A reloc against a global symbol.
1125
1126   Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
1127                Address address, Addend addend, bool is_relative)
1128     : rel_(gsym, type, od, address, is_relative), addend_(addend)
1129   { }
1130
1131   Output_reloc(Symbol* gsym, unsigned int type,
1132                Sized_relobj<size, big_endian>* relobj,
1133                unsigned int shndx, Address address, Addend addend,
1134                bool is_relative)
1135     : rel_(gsym, type, relobj, shndx, address, is_relative), addend_(addend)
1136   { }
1137
1138   // A reloc against a local symbol.
1139
1140   Output_reloc(Sized_relobj<size, big_endian>* relobj,
1141                unsigned int local_sym_index, unsigned int type,
1142                Output_data* od, Address address,
1143                Addend addend, bool is_relative, bool is_section_symbol)
1144     : rel_(relobj, local_sym_index, type, od, address, is_relative,
1145            is_section_symbol),
1146       addend_(addend)
1147   { }
1148
1149   Output_reloc(Sized_relobj<size, big_endian>* relobj,
1150                unsigned int local_sym_index, unsigned int type,
1151                unsigned int shndx, Address address,
1152                Addend addend, bool is_relative, bool is_section_symbol)
1153     : rel_(relobj, local_sym_index, type, shndx, address, is_relative,
1154            is_section_symbol),
1155       addend_(addend)
1156   { }
1157
1158   // A reloc against the STT_SECTION symbol of an output section.
1159
1160   Output_reloc(Output_section* os, unsigned int type, Output_data* od,
1161                Address address, Addend addend)
1162     : rel_(os, type, od, address), addend_(addend)
1163   { }
1164
1165   Output_reloc(Output_section* os, unsigned int type,
1166                Sized_relobj<size, big_endian>* relobj,
1167                unsigned int shndx, Address address, Addend addend)
1168     : rel_(os, type, relobj, shndx, address), addend_(addend)
1169   { }
1170
1171   // Write the reloc entry to an output view.
1172   void
1173   write(unsigned char* pov) const;
1174
1175   // Return whether this reloc should be sorted before the argument
1176   // when sorting dynamic relocs.
1177   bool
1178   sort_before(const Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>&
1179               r2) const
1180   {
1181     int i = this->rel_.compare(r2.rel_);
1182     if (i < 0)
1183       return true;
1184     else if (i > 0)
1185       return false;
1186     else
1187       return this->addend_ < r2.addend_;
1188   }
1189
1190  private:
1191   // The basic reloc.
1192   Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
1193   // The addend.
1194   Addend addend_;
1195 };
1196
1197 // Output_data_reloc is used to manage a section containing relocs.
1198 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA.  DYNAMIC
1199 // indicates whether this is a dynamic relocation or a normal
1200 // relocation.  Output_data_reloc_base is a base class.
1201 // Output_data_reloc is the real class, which we specialize based on
1202 // the reloc type.
1203
1204 template<int sh_type, bool dynamic, int size, bool big_endian>
1205 class Output_data_reloc_base : public Output_section_data_build
1206 {
1207  public:
1208   typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
1209   typedef typename Output_reloc_type::Address Address;
1210   static const int reloc_size =
1211     Reloc_types<sh_type, size, big_endian>::reloc_size;
1212
1213   // Construct the section.
1214   Output_data_reloc_base(bool sort_relocs)
1215     : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1216       sort_relocs_(sort_relocs)
1217   { }
1218
1219  protected:
1220   // Write out the data.
1221   void
1222   do_write(Output_file*);
1223
1224   // Set the entry size and the link.
1225   void
1226   do_adjust_output_section(Output_section *os);
1227
1228   // Write to a map file.
1229   void
1230   do_print_to_mapfile(Mapfile* mapfile) const
1231   {
1232     mapfile->print_output_data(this,
1233                                (dynamic
1234                                 ? _("** dynamic relocs")
1235                                 : _("** relocs")));
1236   }
1237
1238   // Add a relocation entry.
1239   void
1240   add(Output_data *od, const Output_reloc_type& reloc)
1241   {
1242     this->relocs_.push_back(reloc);
1243     this->set_current_data_size(this->relocs_.size() * reloc_size);
1244     od->add_dynamic_reloc();
1245   }
1246
1247  private:
1248   typedef std::vector<Output_reloc_type> Relocs;
1249
1250   // The class used to sort the relocations.
1251   struct Sort_relocs_comparison
1252   {
1253     bool
1254     operator()(const Output_reloc_type& r1, const Output_reloc_type& r2) const
1255     { return r1.sort_before(r2); }
1256   };
1257
1258   // The relocations in this section.
1259   Relocs relocs_;
1260   // Whether to sort the relocations when writing them out, to make
1261   // the dynamic linker more efficient.
1262   bool sort_relocs_;
1263 };
1264
1265 // The class which callers actually create.
1266
1267 template<int sh_type, bool dynamic, int size, bool big_endian>
1268 class Output_data_reloc;
1269
1270 // The SHT_REL version of Output_data_reloc.
1271
1272 template<bool dynamic, int size, bool big_endian>
1273 class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1274   : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
1275 {
1276  private:
1277   typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
1278                                  big_endian> Base;
1279
1280  public:
1281   typedef typename Base::Output_reloc_type Output_reloc_type;
1282   typedef typename Output_reloc_type::Address Address;
1283
1284   Output_data_reloc(bool sr)
1285     : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>(sr)
1286   { }
1287
1288   // Add a reloc against a global symbol.
1289
1290   void
1291   add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
1292   { this->add(od, Output_reloc_type(gsym, type, od, address, false)); }
1293
1294   void
1295   add_global(Symbol* gsym, unsigned int type, Output_data* od,
1296              Sized_relobj<size, big_endian>* relobj,
1297              unsigned int shndx, Address address)
1298   { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1299                                     false)); }
1300
1301   // These are to simplify the Copy_relocs class.
1302
1303   void
1304   add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address,
1305              Address addend)
1306   {
1307     gold_assert(addend == 0);
1308     this->add_global(gsym, type, od, address);
1309   }
1310
1311   void
1312   add_global(Symbol* gsym, unsigned int type, Output_data* od,
1313              Sized_relobj<size, big_endian>* relobj,
1314              unsigned int shndx, Address address, Address addend)
1315   {
1316     gold_assert(addend == 0);
1317     this->add_global(gsym, type, od, relobj, shndx, address);
1318   }
1319
1320   // Add a RELATIVE reloc against a global symbol.  The final relocation
1321   // will not reference the symbol.
1322
1323   void
1324   add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1325                       Address address)
1326   { this->add(od, Output_reloc_type(gsym, type, od, address, true)); }
1327
1328   void
1329   add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1330                       Sized_relobj<size, big_endian>* relobj,
1331                       unsigned int shndx, Address address)
1332   {
1333     this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1334                                     true));
1335   }
1336
1337   // Add a reloc against a local symbol.
1338
1339   void
1340   add_local(Sized_relobj<size, big_endian>* relobj,
1341             unsigned int local_sym_index, unsigned int type,
1342             Output_data* od, Address address)
1343   {
1344     this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1345                                     address, false, false));
1346   }
1347
1348   void
1349   add_local(Sized_relobj<size, big_endian>* relobj,
1350             unsigned int local_sym_index, unsigned int type,
1351             Output_data* od, unsigned int shndx, Address address)
1352   {
1353     this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1354                                     address, false, false));
1355   }
1356
1357   // Add a RELATIVE reloc against a local symbol.
1358
1359   void
1360   add_local_relative(Sized_relobj<size, big_endian>* relobj,
1361                      unsigned int local_sym_index, unsigned int type,
1362                      Output_data* od, Address address)
1363   {
1364     this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1365                                     address, true, false));
1366   }
1367
1368   void
1369   add_local_relative(Sized_relobj<size, big_endian>* relobj,
1370                      unsigned int local_sym_index, unsigned int type,
1371                      Output_data* od, unsigned int shndx, Address address)
1372   {
1373     this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1374                                     address, true, false));
1375   }
1376
1377   // Add a reloc against a local section symbol.  This will be
1378   // converted into a reloc against the STT_SECTION symbol of the
1379   // output section.
1380
1381   void
1382   add_local_section(Sized_relobj<size, big_endian>* relobj,
1383                     unsigned int input_shndx, unsigned int type,
1384                     Output_data* od, Address address)
1385   {
1386     this->add(od, Output_reloc_type(relobj, input_shndx, type, od,
1387                                     address, false, true));
1388   }
1389
1390   void
1391   add_local_section(Sized_relobj<size, big_endian>* relobj,
1392                     unsigned int input_shndx, unsigned int type,
1393                     Output_data* od, unsigned int shndx, Address address)
1394   {
1395     this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1396                                     address, false, true));
1397   }
1398
1399   // A reloc against the STT_SECTION symbol of an output section.
1400   // OS is the Output_section that the relocation refers to; OD is
1401   // the Output_data object being relocated.
1402
1403   void
1404   add_output_section(Output_section* os, unsigned int type,
1405                      Output_data* od, Address address)
1406   { this->add(od, Output_reloc_type(os, type, od, address)); }
1407
1408   void
1409   add_output_section(Output_section* os, unsigned int type, Output_data* od,
1410                      Sized_relobj<size, big_endian>* relobj,
1411                      unsigned int shndx, Address address)
1412   { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); }
1413 };
1414
1415 // The SHT_RELA version of Output_data_reloc.
1416
1417 template<bool dynamic, int size, bool big_endian>
1418 class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1419   : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1420 {
1421  private:
1422   typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1423                                  big_endian> Base;
1424
1425  public:
1426   typedef typename Base::Output_reloc_type Output_reloc_type;
1427   typedef typename Output_reloc_type::Address Address;
1428   typedef typename Output_reloc_type::Addend Addend;
1429
1430   Output_data_reloc(bool sr)
1431     : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>(sr)
1432   { }
1433
1434   // Add a reloc against a global symbol.
1435
1436   void
1437   add_global(Symbol* gsym, unsigned int type, Output_data* od,
1438              Address address, Addend addend)
1439   { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1440                                     false)); }
1441
1442   void
1443   add_global(Symbol* gsym, unsigned int type, Output_data* od,
1444              Sized_relobj<size, big_endian>* relobj,
1445              unsigned int shndx, Address address,
1446              Addend addend)
1447   { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1448                                     addend, false)); }
1449
1450   // Add a RELATIVE reloc against a global symbol.  The final output
1451   // relocation will not reference the symbol, but we must keep the symbol
1452   // information long enough to set the addend of the relocation correctly
1453   // when it is written.
1454
1455   void
1456   add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1457                       Address address, Addend addend)
1458   { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true)); }
1459
1460   void
1461   add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1462                       Sized_relobj<size, big_endian>* relobj,
1463                       unsigned int shndx, Address address, Addend addend)
1464   { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1465                                     addend, true)); }
1466
1467   // Add a reloc against a local symbol.
1468
1469   void
1470   add_local(Sized_relobj<size, big_endian>* relobj,
1471             unsigned int local_sym_index, unsigned int type,
1472             Output_data* od, Address address, Addend addend)
1473   {
1474     this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1475                                     addend, false, false));
1476   }
1477
1478   void
1479   add_local(Sized_relobj<size, big_endian>* relobj,
1480             unsigned int local_sym_index, unsigned int type,
1481             Output_data* od, unsigned int shndx, Address address,
1482             Addend addend)
1483   {
1484     this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1485                                     address, addend, false, false));
1486   }
1487
1488   // Add a RELATIVE reloc against a local symbol.
1489
1490   void
1491   add_local_relative(Sized_relobj<size, big_endian>* relobj,
1492                      unsigned int local_sym_index, unsigned int type,
1493                      Output_data* od, Address address, Addend addend)
1494   {
1495     this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1496                                     addend, true, false));
1497   }
1498
1499   void
1500   add_local_relative(Sized_relobj<size, big_endian>* relobj,
1501                      unsigned int local_sym_index, unsigned int type,
1502                      Output_data* od, unsigned int shndx, Address address,
1503                      Addend addend)
1504   {
1505     this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1506                                     address, addend, true, false));
1507   }
1508
1509   // Add a reloc against a local section symbol.  This will be
1510   // converted into a reloc against the STT_SECTION symbol of the
1511   // output section.
1512
1513   void
1514   add_local_section(Sized_relobj<size, big_endian>* relobj,
1515                     unsigned int input_shndx, unsigned int type,
1516                     Output_data* od, Address address, Addend addend)
1517   {
1518     this->add(od, Output_reloc_type(relobj, input_shndx, type, od, address,
1519                                     addend, false, true));
1520   }
1521
1522   void
1523   add_local_section(Sized_relobj<size, big_endian>* relobj,
1524                      unsigned int input_shndx, unsigned int type,
1525                      Output_data* od, unsigned int shndx, Address address,
1526                      Addend addend)
1527   {
1528     this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1529                                     address, addend, false, true));
1530   }
1531
1532   // A reloc against the STT_SECTION symbol of an output section.
1533
1534   void
1535   add_output_section(Output_section* os, unsigned int type, Output_data* od,
1536                      Address address, Addend addend)
1537   { this->add(os, Output_reloc_type(os, type, od, address, addend)); }
1538
1539   void
1540   add_output_section(Output_section* os, unsigned int type,
1541                      Sized_relobj<size, big_endian>* relobj,
1542                      unsigned int shndx, Address address, Addend addend)
1543   { this->add(os, Output_reloc_type(os, type, relobj, shndx, address,
1544                                     addend)); }
1545 };
1546
1547 // Output_relocatable_relocs represents a relocation section in a
1548 // relocatable link.  The actual data is written out in the target
1549 // hook relocate_for_relocatable.  This just saves space for it.
1550
1551 template<int sh_type, int size, bool big_endian>
1552 class Output_relocatable_relocs : public Output_section_data
1553 {
1554  public:
1555   Output_relocatable_relocs(Relocatable_relocs* rr)
1556     : Output_section_data(Output_data::default_alignment_for_size(size)),
1557       rr_(rr)
1558   { }
1559
1560   void
1561   set_final_data_size();
1562
1563   // Write out the data.  There is nothing to do here.
1564   void
1565   do_write(Output_file*)
1566   { }
1567
1568   // Write to a map file.
1569   void
1570   do_print_to_mapfile(Mapfile* mapfile) const
1571   { mapfile->print_output_data(this, _("** relocs")); }
1572
1573  private:
1574   // The relocs associated with this input section.
1575   Relocatable_relocs* rr_;
1576 };
1577
1578 // Handle a GROUP section.
1579
1580 template<int size, bool big_endian>
1581 class Output_data_group : public Output_section_data
1582 {
1583  public:
1584   // The constructor clears *INPUT_SHNDXES.
1585   Output_data_group(Sized_relobj<size, big_endian>* relobj,
1586                     section_size_type entry_count,
1587                     elfcpp::Elf_Word flags,
1588                     std::vector<unsigned int>* input_shndxes);
1589
1590   void
1591   do_write(Output_file*);
1592
1593   // Write to a map file.
1594   void
1595   do_print_to_mapfile(Mapfile* mapfile) const
1596   { mapfile->print_output_data(this, _("** group")); }
1597
1598   // Set final data size.
1599   void
1600   set_final_data_size()
1601   { this->set_data_size((this->input_shndxes_.size() + 1) * 4); }
1602
1603  private:
1604   // The input object.
1605   Sized_relobj<size, big_endian>* relobj_;
1606   // The group flag word.
1607   elfcpp::Elf_Word flags_;
1608   // The section indexes of the input sections in this group.
1609   std::vector<unsigned int> input_shndxes_;
1610 };
1611
1612 // Output_data_got is used to manage a GOT.  Each entry in the GOT is
1613 // for one symbol--either a global symbol or a local symbol in an
1614 // object.  The target specific code adds entries to the GOT as
1615 // needed.
1616
1617 template<int size, bool big_endian>
1618 class Output_data_got : public Output_section_data_build
1619 {
1620  public:
1621   typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1622   typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> Rel_dyn;
1623   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1624
1625   Output_data_got()
1626     : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1627       entries_()
1628   { }
1629
1630   // Add an entry for a global symbol to the GOT.  Return true if this
1631   // is a new GOT entry, false if the symbol was already in the GOT.
1632   bool
1633   add_global(Symbol* gsym, unsigned int got_type);
1634
1635   // Add an entry for a global symbol to the GOT, and add a dynamic
1636   // relocation of type R_TYPE for the GOT entry.
1637   void
1638   add_global_with_rel(Symbol* gsym, unsigned int got_type,
1639                       Rel_dyn* rel_dyn, unsigned int r_type);
1640
1641   void
1642   add_global_with_rela(Symbol* gsym, unsigned int got_type,
1643                        Rela_dyn* rela_dyn, unsigned int r_type);
1644
1645   // Add a pair of entries for a global symbol to the GOT, and add
1646   // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1647   void
1648   add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
1649                            Rel_dyn* rel_dyn, unsigned int r_type_1,
1650                            unsigned int r_type_2);
1651
1652   void
1653   add_global_pair_with_rela(Symbol* gsym, unsigned int got_type,
1654                             Rela_dyn* rela_dyn, unsigned int r_type_1,
1655                             unsigned int r_type_2);
1656
1657   // Add an entry for a local symbol to the GOT.  This returns true if
1658   // this is a new GOT entry, false if the symbol already has a GOT
1659   // entry.
1660   bool
1661   add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index,
1662             unsigned int got_type);
1663
1664   // Add an entry for a local symbol to the GOT, and add a dynamic
1665   // relocation of type R_TYPE for the GOT entry.
1666   void
1667   add_local_with_rel(Sized_relobj<size, big_endian>* object,
1668                      unsigned int sym_index, unsigned int got_type,
1669                      Rel_dyn* rel_dyn, unsigned int r_type);
1670
1671   void
1672   add_local_with_rela(Sized_relobj<size, big_endian>* object,
1673                       unsigned int sym_index, unsigned int got_type,
1674                       Rela_dyn* rela_dyn, unsigned int r_type);
1675
1676   // Add a pair of entries for a local symbol to the GOT, and add
1677   // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1678   void
1679   add_local_pair_with_rel(Sized_relobj<size, big_endian>* object,
1680                           unsigned int sym_index, unsigned int shndx,
1681                           unsigned int got_type, Rel_dyn* rel_dyn,
1682                           unsigned int r_type_1, unsigned int r_type_2);
1683
1684   void
1685   add_local_pair_with_rela(Sized_relobj<size, big_endian>* object,
1686                           unsigned int sym_index, unsigned int shndx,
1687                           unsigned int got_type, Rela_dyn* rela_dyn,
1688                           unsigned int r_type_1, unsigned int r_type_2);
1689
1690   // Add a constant to the GOT.  This returns the offset of the new
1691   // entry from the start of the GOT.
1692   unsigned int
1693   add_constant(Valtype constant)
1694   {
1695     this->entries_.push_back(Got_entry(constant));
1696     this->set_got_size();
1697     return this->last_got_offset();
1698   }
1699
1700  protected:
1701   // Write out the GOT table.
1702   void
1703   do_write(Output_file*);
1704
1705   // Write to a map file.
1706   void
1707   do_print_to_mapfile(Mapfile* mapfile) const
1708   { mapfile->print_output_data(this, _("** GOT")); }
1709
1710  private:
1711   // This POD class holds a single GOT entry.
1712   class Got_entry
1713   {
1714    public:
1715     // Create a zero entry.
1716     Got_entry()
1717       : local_sym_index_(CONSTANT_CODE)
1718     { this->u_.constant = 0; }
1719
1720     // Create a global symbol entry.
1721     explicit Got_entry(Symbol* gsym)
1722       : local_sym_index_(GSYM_CODE)
1723     { this->u_.gsym = gsym; }
1724
1725     // Create a local symbol entry.
1726     Got_entry(Sized_relobj<size, big_endian>* object,
1727               unsigned int local_sym_index)
1728       : local_sym_index_(local_sym_index)
1729     {
1730       gold_assert(local_sym_index != GSYM_CODE
1731                   && local_sym_index != CONSTANT_CODE);
1732       this->u_.object = object;
1733     }
1734
1735     // Create a constant entry.  The constant is a host value--it will
1736     // be swapped, if necessary, when it is written out.
1737     explicit Got_entry(Valtype constant)
1738       : local_sym_index_(CONSTANT_CODE)
1739     { this->u_.constant = constant; }
1740
1741     // Write the GOT entry to an output view.
1742     void
1743     write(unsigned char* pov) const;
1744
1745    private:
1746     enum
1747     {
1748       GSYM_CODE = -1U,
1749       CONSTANT_CODE = -2U
1750     };
1751
1752     union
1753     {
1754       // For a local symbol, the object.
1755       Sized_relobj<size, big_endian>* object;
1756       // For a global symbol, the symbol.
1757       Symbol* gsym;
1758       // For a constant, the constant.
1759       Valtype constant;
1760     } u_;
1761     // For a local symbol, the local symbol index.  This is GSYM_CODE
1762     // for a global symbol, or CONSTANT_CODE for a constant.
1763     unsigned int local_sym_index_;
1764   };
1765
1766   typedef std::vector<Got_entry> Got_entries;
1767
1768   // Return the offset into the GOT of GOT entry I.
1769   unsigned int
1770   got_offset(unsigned int i) const
1771   { return i * (size / 8); }
1772
1773   // Return the offset into the GOT of the last entry added.
1774   unsigned int
1775   last_got_offset() const
1776   { return this->got_offset(this->entries_.size() - 1); }
1777
1778   // Set the size of the section.
1779   void
1780   set_got_size()
1781   { this->set_current_data_size(this->got_offset(this->entries_.size())); }
1782
1783   // The list of GOT entries.
1784   Got_entries entries_;
1785 };
1786
1787 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1788 // section.
1789
1790 class Output_data_dynamic : public Output_section_data
1791 {
1792  public:
1793   Output_data_dynamic(Stringpool* pool)
1794     : Output_section_data(Output_data::default_alignment()),
1795       entries_(), pool_(pool)
1796   { }
1797
1798   // Add a new dynamic entry with a fixed numeric value.
1799   void
1800   add_constant(elfcpp::DT tag, unsigned int val)
1801   { this->add_entry(Dynamic_entry(tag, val)); }
1802
1803   // Add a new dynamic entry with the address of output data.
1804   void
1805   add_section_address(elfcpp::DT tag, const Output_data* od)
1806   { this->add_entry(Dynamic_entry(tag, od, false)); }
1807
1808   // Add a new dynamic entry with the address of output data
1809   // plus a constant offset.
1810   void
1811   add_section_plus_offset(elfcpp::DT tag, const Output_data* od,
1812                           unsigned int offset)
1813   { this->add_entry(Dynamic_entry(tag, od, offset)); }
1814
1815   // Add a new dynamic entry with the size of output data.
1816   void
1817   add_section_size(elfcpp::DT tag, const Output_data* od)
1818   { this->add_entry(Dynamic_entry(tag, od, true)); }
1819
1820   // Add a new dynamic entry with the address of a symbol.
1821   void
1822   add_symbol(elfcpp::DT tag, const Symbol* sym)
1823   { this->add_entry(Dynamic_entry(tag, sym)); }
1824
1825   // Add a new dynamic entry with a string.
1826   void
1827   add_string(elfcpp::DT tag, const char* str)
1828   { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
1829
1830   void
1831   add_string(elfcpp::DT tag, const std::string& str)
1832   { this->add_string(tag, str.c_str()); }
1833
1834  protected:
1835   // Adjust the output section to set the entry size.
1836   void
1837   do_adjust_output_section(Output_section*);
1838
1839   // Set the final data size.
1840   void
1841   set_final_data_size();
1842
1843   // Write out the dynamic entries.
1844   void
1845   do_write(Output_file*);
1846
1847   // Write to a map file.
1848   void
1849   do_print_to_mapfile(Mapfile* mapfile) const
1850   { mapfile->print_output_data(this, _("** dynamic")); }
1851
1852  private:
1853   // This POD class holds a single dynamic entry.
1854   class Dynamic_entry
1855   {
1856    public:
1857     // Create an entry with a fixed numeric value.
1858     Dynamic_entry(elfcpp::DT tag, unsigned int val)
1859       : tag_(tag), offset_(DYNAMIC_NUMBER)
1860     { this->u_.val = val; }
1861
1862     // Create an entry with the size or address of a section.
1863     Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
1864       : tag_(tag),
1865         offset_(section_size
1866                 ? DYNAMIC_SECTION_SIZE
1867                 : DYNAMIC_SECTION_ADDRESS)
1868     { this->u_.od = od; }
1869
1870     // Create an entry with the address of a section plus a constant offset.
1871     Dynamic_entry(elfcpp::DT tag, const Output_data* od, unsigned int offset)
1872       : tag_(tag),
1873         offset_(offset)
1874     { this->u_.od = od; }
1875
1876     // Create an entry with the address of a symbol.
1877     Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
1878       : tag_(tag), offset_(DYNAMIC_SYMBOL)
1879     { this->u_.sym = sym; }
1880
1881     // Create an entry with a string.
1882     Dynamic_entry(elfcpp::DT tag, const char* str)
1883       : tag_(tag), offset_(DYNAMIC_STRING)
1884     { this->u_.str = str; }
1885
1886     // Return the tag of this entry.
1887     elfcpp::DT
1888     tag() const
1889     { return this->tag_; }
1890
1891     // Write the dynamic entry to an output view.
1892     template<int size, bool big_endian>
1893     void
1894     write(unsigned char* pov, const Stringpool*) const;
1895
1896    private:
1897     // Classification is encoded in the OFFSET field.
1898     enum Classification
1899     {
1900       // Section address.
1901       DYNAMIC_SECTION_ADDRESS = 0,
1902       // Number.
1903       DYNAMIC_NUMBER = -1U,
1904       // Section size.
1905       DYNAMIC_SECTION_SIZE = -2U,
1906       // Symbol adress.
1907       DYNAMIC_SYMBOL = -3U,
1908       // String.
1909       DYNAMIC_STRING = -4U
1910       // Any other value indicates a section address plus OFFSET.
1911     };
1912
1913     union
1914     {
1915       // For DYNAMIC_NUMBER.
1916       unsigned int val;
1917       // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
1918       const Output_data* od;
1919       // For DYNAMIC_SYMBOL.
1920       const Symbol* sym;
1921       // For DYNAMIC_STRING.
1922       const char* str;
1923     } u_;
1924     // The dynamic tag.
1925     elfcpp::DT tag_;
1926     // The type of entry (Classification) or offset within a section.
1927     unsigned int offset_;
1928   };
1929
1930   // Add an entry to the list.
1931   void
1932   add_entry(const Dynamic_entry& entry)
1933   { this->entries_.push_back(entry); }
1934
1935   // Sized version of write function.
1936   template<int size, bool big_endian>
1937   void
1938   sized_write(Output_file* of);
1939
1940   // The type of the list of entries.
1941   typedef std::vector<Dynamic_entry> Dynamic_entries;
1942
1943   // The entries.
1944   Dynamic_entries entries_;
1945   // The pool used for strings.
1946   Stringpool* pool_;
1947 };
1948
1949 // Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
1950 // which may be required if the object file has more than
1951 // SHN_LORESERVE sections.
1952
1953 class Output_symtab_xindex : public Output_section_data
1954 {
1955  public:
1956   Output_symtab_xindex(size_t symcount)
1957     : Output_section_data(symcount * 4, 4, true),
1958       entries_()
1959   { }
1960
1961   // Add an entry: symbol number SYMNDX has section SHNDX.
1962   void
1963   add(unsigned int symndx, unsigned int shndx)
1964   { this->entries_.push_back(std::make_pair(symndx, shndx)); }
1965
1966  protected:
1967   void
1968   do_write(Output_file*);
1969
1970   // Write to a map file.
1971   void
1972   do_print_to_mapfile(Mapfile* mapfile) const
1973   { mapfile->print_output_data(this, _("** symtab xindex")); }
1974
1975  private:
1976   template<bool big_endian>
1977   void
1978   endian_do_write(unsigned char*);
1979
1980   // It is likely that most symbols will not require entries.  Rather
1981   // than keep a vector for all symbols, we keep pairs of symbol index
1982   // and section index.
1983   typedef std::vector<std::pair<unsigned int, unsigned int> > Xindex_entries;
1984
1985   // The entries we need.
1986   Xindex_entries entries_;
1987 };
1988
1989 // A relaxed input section.
1990 class Output_relaxed_input_section : public Output_section_data
1991 {
1992  public:
1993   // We would like to call relobj->section_addralign(shndx) to get the
1994   // alignment but we do not want the constructor to fail.  So callers
1995   // are repsonsible for ensuring that.
1996   Output_relaxed_input_section(Relobj* relobj, unsigned int shndx,
1997                                uint64_t addralign)
1998     : Output_section_data(addralign), relobj_(relobj), shndx_(shndx)
1999   { }
2000  
2001   // Return the Relobj of this relaxed input section.
2002   Relobj*
2003   relobj() const
2004   { return this->relobj_; }
2005  
2006   // Return the section index of this relaxed input section.
2007   unsigned int
2008   shndx() const
2009   { return this->shndx_; }
2010
2011  private:
2012   Relobj* relobj_;
2013   unsigned int shndx_;
2014 };
2015
2016 // An output section.  We don't expect to have too many output
2017 // sections, so we don't bother to do a template on the size.
2018
2019 class Output_section : public Output_data
2020 {
2021  public:
2022   // Create an output section, giving the name, type, and flags.
2023   Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
2024   virtual ~Output_section();
2025
2026   // Add a new input section SHNDX, named NAME, with header SHDR, from
2027   // object OBJECT.  RELOC_SHNDX is the index of a relocation section
2028   // which applies to this section, or 0 if none, or -1 if more than
2029   // one.  HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
2030   // in a linker script; in that case we need to keep track of input
2031   // sections associated with an output section.  Return the offset
2032   // within the output section.
2033   template<int size, bool big_endian>
2034   off_t
2035   add_input_section(Sized_relobj<size, big_endian>* object, unsigned int shndx,
2036                     const char *name,
2037                     const elfcpp::Shdr<size, big_endian>& shdr,
2038                     unsigned int reloc_shndx, bool have_sections_script);
2039
2040   // Add generated data POSD to this output section.
2041   void
2042   add_output_section_data(Output_section_data* posd);
2043
2044   // Return the section name.
2045   const char*
2046   name() const
2047   { return this->name_; }
2048
2049   // Return the section type.
2050   elfcpp::Elf_Word
2051   type() const
2052   { return this->type_; }
2053
2054   // Return the section flags.
2055   elfcpp::Elf_Xword
2056   flags() const
2057   { return this->flags_; }
2058
2059   // Update the output section flags based on input section flags.
2060   void
2061   update_flags_for_input_section(elfcpp::Elf_Xword flags);
2062
2063   // Return the entsize field.
2064   uint64_t
2065   entsize() const
2066   { return this->entsize_; }
2067
2068   // Set the entsize field.
2069   void
2070   set_entsize(uint64_t v);
2071
2072   // Set the load address.
2073   void
2074   set_load_address(uint64_t load_address)
2075   {
2076     this->load_address_ = load_address;
2077     this->has_load_address_ = true;
2078   }
2079
2080   // Set the link field to the output section index of a section.
2081   void
2082   set_link_section(const Output_data* od)
2083   {
2084     gold_assert(this->link_ == 0
2085                 && !this->should_link_to_symtab_
2086                 && !this->should_link_to_dynsym_);
2087     this->link_section_ = od;
2088   }
2089
2090   // Set the link field to a constant.
2091   void
2092   set_link(unsigned int v)
2093   {
2094     gold_assert(this->link_section_ == NULL
2095                 && !this->should_link_to_symtab_
2096                 && !this->should_link_to_dynsym_);
2097     this->link_ = v;
2098   }
2099
2100   // Record that this section should link to the normal symbol table.
2101   void
2102   set_should_link_to_symtab()
2103   {
2104     gold_assert(this->link_section_ == NULL
2105                 && this->link_ == 0
2106                 && !this->should_link_to_dynsym_);
2107     this->should_link_to_symtab_ = true;
2108   }
2109
2110   // Record that this section should link to the dynamic symbol table.
2111   void
2112   set_should_link_to_dynsym()
2113   {
2114     gold_assert(this->link_section_ == NULL
2115                 && this->link_ == 0
2116                 && !this->should_link_to_symtab_);
2117     this->should_link_to_dynsym_ = true;
2118   }
2119
2120   // Return the info field.
2121   unsigned int
2122   info() const
2123   {
2124     gold_assert(this->info_section_ == NULL
2125                 && this->info_symndx_ == NULL);
2126     return this->info_;
2127   }
2128
2129   // Set the info field to the output section index of a section.
2130   void
2131   set_info_section(const Output_section* os)
2132   {
2133     gold_assert((this->info_section_ == NULL
2134                  || (this->info_section_ == os
2135                      && this->info_uses_section_index_))
2136                 && this->info_symndx_ == NULL
2137                 && this->info_ == 0);
2138     this->info_section_ = os;
2139     this->info_uses_section_index_= true;
2140   }
2141
2142   // Set the info field to the symbol table index of a symbol.
2143   void
2144   set_info_symndx(const Symbol* sym)
2145   {
2146     gold_assert(this->info_section_ == NULL
2147                 && (this->info_symndx_ == NULL
2148                     || this->info_symndx_ == sym)
2149                 && this->info_ == 0);
2150     this->info_symndx_ = sym;
2151   }
2152
2153   // Set the info field to the symbol table index of a section symbol.
2154   void
2155   set_info_section_symndx(const Output_section* os)
2156   {
2157     gold_assert((this->info_section_ == NULL
2158                  || (this->info_section_ == os
2159                      && !this->info_uses_section_index_))
2160                 && this->info_symndx_ == NULL
2161                 && this->info_ == 0);
2162     this->info_section_ = os;
2163     this->info_uses_section_index_ = false;
2164   }
2165
2166   // Set the info field to a constant.
2167   void
2168   set_info(unsigned int v)
2169   {
2170     gold_assert(this->info_section_ == NULL
2171                 && this->info_symndx_ == NULL
2172                 && (this->info_ == 0
2173                     || this->info_ == v));
2174     this->info_ = v;
2175   }
2176
2177   // Set the addralign field.
2178   void
2179   set_addralign(uint64_t v)
2180   { this->addralign_ = v; }
2181
2182   // Whether the output section index has been set.
2183   bool
2184   has_out_shndx() const
2185   { return this->out_shndx_ != -1U; }
2186
2187   // Indicate that we need a symtab index.
2188   void
2189   set_needs_symtab_index()
2190   { this->needs_symtab_index_ = true; }
2191
2192   // Return whether we need a symtab index.
2193   bool
2194   needs_symtab_index() const
2195   { return this->needs_symtab_index_; }
2196
2197   // Get the symtab index.
2198   unsigned int
2199   symtab_index() const
2200   {
2201     gold_assert(this->symtab_index_ != 0);
2202     return this->symtab_index_;
2203   }
2204
2205   // Set the symtab index.
2206   void
2207   set_symtab_index(unsigned int index)
2208   {
2209     gold_assert(index != 0);
2210     this->symtab_index_ = index;
2211   }
2212
2213   // Indicate that we need a dynsym index.
2214   void
2215   set_needs_dynsym_index()
2216   { this->needs_dynsym_index_ = true; }
2217
2218   // Return whether we need a dynsym index.
2219   bool
2220   needs_dynsym_index() const
2221   { return this->needs_dynsym_index_; }
2222
2223   // Get the dynsym index.
2224   unsigned int
2225   dynsym_index() const
2226   {
2227     gold_assert(this->dynsym_index_ != 0);
2228     return this->dynsym_index_;
2229   }
2230
2231   // Set the dynsym index.
2232   void
2233   set_dynsym_index(unsigned int index)
2234   {
2235     gold_assert(index != 0);
2236     this->dynsym_index_ = index;
2237   }
2238
2239   // Return whether the input sections sections attachd to this output
2240   // section may require sorting.  This is used to handle constructor
2241   // priorities compatibly with GNU ld.
2242   bool
2243   may_sort_attached_input_sections() const
2244   { return this->may_sort_attached_input_sections_; }
2245
2246   // Record that the input sections attached to this output section
2247   // may require sorting.
2248   void
2249   set_may_sort_attached_input_sections()
2250   { this->may_sort_attached_input_sections_ = true; }
2251
2252   // Return whether the input sections attached to this output section
2253   // require sorting.  This is used to handle constructor priorities
2254   // compatibly with GNU ld.
2255   bool
2256   must_sort_attached_input_sections() const
2257   { return this->must_sort_attached_input_sections_; }
2258
2259   // Record that the input sections attached to this output section
2260   // require sorting.
2261   void
2262   set_must_sort_attached_input_sections()
2263   { this->must_sort_attached_input_sections_ = true; }
2264
2265   // Return whether this section holds relro data--data which has
2266   // dynamic relocations but which may be marked read-only after the
2267   // dynamic relocations have been completed.
2268   bool
2269   is_relro() const
2270   { return this->is_relro_; }
2271
2272   // Record that this section holds relro data.
2273   void
2274   set_is_relro()
2275   { this->is_relro_ = true; }
2276
2277   // Record that this section does not hold relro data.
2278   void
2279   clear_is_relro()
2280   { this->is_relro_ = false; }
2281
2282   // True if this section holds relro local data--relro data for which
2283   // the dynamic relocations are all RELATIVE relocations.
2284   bool
2285   is_relro_local() const
2286   { return this->is_relro_local_; }
2287
2288   // Record that this section holds relro local data.
2289   void
2290   set_is_relro_local()
2291   { this->is_relro_local_ = true; }
2292
2293   // True if this is a small section: a section which holds small
2294   // variables.
2295   bool
2296   is_small_section() const
2297   { return this->is_small_section_; }
2298
2299   // Record that this is a small section.
2300   void
2301   set_is_small_section()
2302   { this->is_small_section_ = true; }
2303
2304   // True if this is a large section: a section which holds large
2305   // variables.
2306   bool
2307   is_large_section() const
2308   { return this->is_large_section_; }
2309
2310   // Record that this is a large section.
2311   void
2312   set_is_large_section()
2313   { this->is_large_section_ = true; }
2314
2315   // True if this is a large data (not BSS) section.
2316   bool
2317   is_large_data_section()
2318   { return this->is_large_section_ && this->type_ != elfcpp::SHT_NOBITS; }
2319
2320   // Return whether this section should be written after all the input
2321   // sections are complete.
2322   bool
2323   after_input_sections() const
2324   { return this->after_input_sections_; }
2325
2326   // Record that this section should be written after all the input
2327   // sections are complete.
2328   void
2329   set_after_input_sections()
2330   { this->after_input_sections_ = true; }
2331
2332   // Return whether this section requires postprocessing after all
2333   // relocations have been applied.
2334   bool
2335   requires_postprocessing() const
2336   { return this->requires_postprocessing_; }
2337
2338   // If a section requires postprocessing, return the buffer to use.
2339   unsigned char*
2340   postprocessing_buffer() const
2341   {
2342     gold_assert(this->postprocessing_buffer_ != NULL);
2343     return this->postprocessing_buffer_;
2344   }
2345
2346   // If a section requires postprocessing, create the buffer to use.
2347   void
2348   create_postprocessing_buffer();
2349
2350   // If a section requires postprocessing, this is the size of the
2351   // buffer to which relocations should be applied.
2352   off_t
2353   postprocessing_buffer_size() const
2354   { return this->current_data_size_for_child(); }
2355
2356   // Modify the section name.  This is only permitted for an
2357   // unallocated section, and only before the size has been finalized.
2358   // Otherwise the name will not get into Layout::namepool_.
2359   void
2360   set_name(const char* newname)
2361   {
2362     gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
2363     gold_assert(!this->is_data_size_valid());
2364     this->name_ = newname;
2365   }
2366
2367   // Return whether the offset OFFSET in the input section SHNDX in
2368   // object OBJECT is being included in the link.
2369   bool
2370   is_input_address_mapped(const Relobj* object, unsigned int shndx,
2371                           off_t offset) const;
2372
2373   // Return the offset within the output section of OFFSET relative to
2374   // the start of input section SHNDX in object OBJECT.
2375   section_offset_type
2376   output_offset(const Relobj* object, unsigned int shndx,
2377                 section_offset_type offset) const;
2378
2379   // Return the output virtual address of OFFSET relative to the start
2380   // of input section SHNDX in object OBJECT.
2381   uint64_t
2382   output_address(const Relobj* object, unsigned int shndx,
2383                  off_t offset) const;
2384
2385   // Look for the merged section for input section SHNDX in object
2386   // OBJECT.  If found, return true, and set *ADDR to the address of
2387   // the start of the merged section.  This is not necessary the
2388   // output offset corresponding to input offset 0 in the section,
2389   // since the section may be mapped arbitrarily.
2390   bool
2391   find_starting_output_address(const Relobj* object, unsigned int shndx,
2392                                uint64_t* addr) const;
2393
2394   // Record that this output section was found in the SECTIONS clause
2395   // of a linker script.
2396   void
2397   set_found_in_sections_clause()
2398   { this->found_in_sections_clause_ = true; }
2399
2400   // Return whether this output section was found in the SECTIONS
2401   // clause of a linker script.
2402   bool
2403   found_in_sections_clause() const
2404   { return this->found_in_sections_clause_; }
2405
2406   // Write the section header into *OPHDR.
2407   template<int size, bool big_endian>
2408   void
2409   write_header(const Layout*, const Stringpool*,
2410                elfcpp::Shdr_write<size, big_endian>*) const;
2411
2412   // The next few calls are for linker script support.
2413
2414   // We need to export the input sections to linker scripts.  Previously
2415   // we export a pair of Relobj pointer and section index.  We now need to
2416   // handle relaxed input sections as well.  So we use this class.
2417   class Simple_input_section
2418   {
2419    private:
2420     static const unsigned int invalid_shndx = static_cast<unsigned int>(-1);
2421
2422    public:
2423     Simple_input_section(Relobj *relobj, unsigned int shndx)
2424       : shndx_(shndx)
2425     {
2426       gold_assert(shndx != invalid_shndx);
2427       this->u_.relobj = relobj;
2428     }
2429  
2430     Simple_input_section(Output_relaxed_input_section* section)
2431       : shndx_(invalid_shndx)
2432     { this->u_.relaxed_input_section = section; }
2433
2434     // Whether this is a relaxed section.
2435     bool
2436     is_relaxed_input_section() const
2437     { return this->shndx_ == invalid_shndx; }
2438
2439     // Return object of an input section.
2440     Relobj*
2441     relobj() const
2442     {
2443       return ((this->shndx_ != invalid_shndx)
2444               ? this->u_.relobj
2445               : this->u_.relaxed_input_section->relobj());
2446     }
2447
2448     // Return index of an input section.
2449     unsigned int
2450     shndx() const
2451     {
2452       return ((this->shndx_ != invalid_shndx)
2453               ? this->shndx_
2454               : this->u_.relaxed_input_section->shndx());
2455     }
2456
2457     // Return the Output_relaxed_input_section object of a relaxed section.
2458     Output_relaxed_input_section*
2459     relaxed_input_section() const
2460     {
2461       gold_assert(this->shndx_ == invalid_shndx);
2462       return this->u_.relaxed_input_section;
2463     }
2464
2465    private:
2466     // Pointer to either an Relobj or an Output_relaxed_input_section.
2467     union
2468     {
2469       Relobj* relobj;
2470       Output_relaxed_input_section* relaxed_input_section;
2471     } u_;
2472     // Section index for an non-relaxed section or invalid_shndx for
2473     // a relaxed section.
2474     unsigned int shndx_;
2475   };
2476  
2477   // Store the list of input sections for this Output_section into the
2478   // list passed in.  This removes the input sections, leaving only
2479   // any Output_section_data elements.  This returns the size of those
2480   // Output_section_data elements.  ADDRESS is the address of this
2481   // output section.  FILL is the fill value to use, in case there are
2482   // any spaces between the remaining Output_section_data elements.
2483   uint64_t
2484   get_input_sections(uint64_t address, const std::string& fill,
2485                      std::list<Simple_input_section>*);
2486
2487   // Add an input section from a script.
2488   void
2489   add_input_section_for_script(const Simple_input_section& input_section,
2490                                off_t data_size, uint64_t addralign);
2491
2492   // Set the current size of the output section.
2493   void
2494   set_current_data_size(off_t size)
2495   { this->set_current_data_size_for_child(size); }
2496
2497   // Get the current size of the output section.
2498   off_t
2499   current_data_size() const
2500   { return this->current_data_size_for_child(); }
2501
2502   // End of linker script support.
2503
2504   // Save states before doing section layout.
2505   // This is used for relaxation.
2506   void
2507   save_states();
2508
2509   // Restore states prior to section layout.
2510   void
2511   restore_states();
2512
2513   // Print merge statistics to stderr.
2514   void
2515   print_merge_stats();
2516
2517  protected:
2518   // Return the output section--i.e., the object itself.
2519   Output_section*
2520   do_output_section()
2521   { return this; }
2522
2523   // Return the section index in the output file.
2524   unsigned int
2525   do_out_shndx() const
2526   {
2527     gold_assert(this->out_shndx_ != -1U);
2528     return this->out_shndx_;
2529   }
2530
2531   // Set the output section index.
2532   void
2533   do_set_out_shndx(unsigned int shndx)
2534   {
2535     gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
2536     this->out_shndx_ = shndx;
2537   }
2538
2539   // Set the final data size of the Output_section.  For a typical
2540   // Output_section, there is nothing to do, but if there are any
2541   // Output_section_data objects we need to set their final addresses
2542   // here.
2543   virtual void
2544   set_final_data_size();
2545
2546   // Reset the address and file offset.
2547   void
2548   do_reset_address_and_file_offset();
2549
2550   // Return true if address and file offset already have reset values. In
2551   // other words, calling reset_address_and_file_offset will not change them.
2552   bool
2553   do_address_and_file_offset_have_reset_values() const;
2554
2555   // Write the data to the file.  For a typical Output_section, this
2556   // does nothing: the data is written out by calling Object::Relocate
2557   // on each input object.  But if there are any Output_section_data
2558   // objects we do need to write them out here.
2559   virtual void
2560   do_write(Output_file*);
2561
2562   // Return the address alignment--function required by parent class.
2563   uint64_t
2564   do_addralign() const
2565   { return this->addralign_; }
2566
2567   // Return whether there is a load address.
2568   bool
2569   do_has_load_address() const
2570   { return this->has_load_address_; }
2571
2572   // Return the load address.
2573   uint64_t
2574   do_load_address() const
2575   {
2576     gold_assert(this->has_load_address_);
2577     return this->load_address_;
2578   }
2579
2580   // Return whether this is an Output_section.
2581   bool
2582   do_is_section() const
2583   { return true; }
2584
2585   // Return whether this is a section of the specified type.
2586   bool
2587   do_is_section_type(elfcpp::Elf_Word type) const
2588   { return this->type_ == type; }
2589
2590   // Return whether the specified section flag is set.
2591   bool
2592   do_is_section_flag_set(elfcpp::Elf_Xword flag) const
2593   { return (this->flags_ & flag) != 0; }
2594
2595   // Set the TLS offset.  Called only for SHT_TLS sections.
2596   void
2597   do_set_tls_offset(uint64_t tls_base);
2598
2599   // Return the TLS offset, relative to the base of the TLS segment.
2600   // Valid only for SHT_TLS sections.
2601   uint64_t
2602   do_tls_offset() const
2603   { return this->tls_offset_; }
2604
2605   // This may be implemented by a child class.
2606   virtual void
2607   do_finalize_name(Layout*)
2608   { }
2609
2610   // Print to the map file.
2611   virtual void
2612   do_print_to_mapfile(Mapfile*) const;
2613
2614   // Record that this section requires postprocessing after all
2615   // relocations have been applied.  This is called by a child class.
2616   void
2617   set_requires_postprocessing()
2618   {
2619     this->requires_postprocessing_ = true;
2620     this->after_input_sections_ = true;
2621   }
2622
2623   // Write all the data of an Output_section into the postprocessing
2624   // buffer.
2625   void
2626   write_to_postprocessing_buffer();
2627
2628  private:
2629   // In some cases we need to keep a list of the input sections
2630   // associated with this output section.  We only need the list if we
2631   // might have to change the offsets of the input section within the
2632   // output section after we add the input section.  The ordinary
2633   // input sections will be written out when we process the object
2634   // file, and as such we don't need to track them here.  We do need
2635   // to track Output_section_data objects here.  We store instances of
2636   // this structure in a std::vector, so it must be a POD.  There can
2637   // be many instances of this structure, so we use a union to save
2638   // some space.
2639   class Input_section
2640   {
2641    public:
2642     Input_section()
2643       : shndx_(0), p2align_(0)
2644     {
2645       this->u1_.data_size = 0;
2646       this->u2_.object = NULL;
2647     }
2648
2649     // For an ordinary input section.
2650     Input_section(Relobj* object, unsigned int shndx, off_t data_size,
2651                   uint64_t addralign)
2652       : shndx_(shndx),
2653         p2align_(ffsll(static_cast<long long>(addralign)))
2654     {
2655       gold_assert(shndx != OUTPUT_SECTION_CODE
2656                   && shndx != MERGE_DATA_SECTION_CODE
2657                   && shndx != MERGE_STRING_SECTION_CODE
2658                   && shndx != RELAXED_INPUT_SECTION_CODE);
2659       this->u1_.data_size = data_size;
2660       this->u2_.object = object;
2661     }
2662
2663     // For a non-merge output section.
2664     Input_section(Output_section_data* posd)
2665       : shndx_(OUTPUT_SECTION_CODE), p2align_(0)
2666     {
2667       this->u1_.data_size = 0;
2668       this->u2_.posd = posd;
2669     }
2670
2671     // For a merge section.
2672     Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
2673       : shndx_(is_string
2674                ? MERGE_STRING_SECTION_CODE
2675                : MERGE_DATA_SECTION_CODE),
2676         p2align_(0)
2677     {
2678       this->u1_.entsize = entsize;
2679       this->u2_.posd = posd;
2680     }
2681
2682     // For a relaxed input section.
2683     Input_section(Output_relaxed_input_section *psection)
2684       : shndx_(RELAXED_INPUT_SECTION_CODE), p2align_(0)
2685     {
2686       this->u1_.data_size = 0;
2687       this->u2_.poris = psection;
2688     }
2689
2690     // The required alignment.
2691     uint64_t
2692     addralign() const
2693     {
2694       if (!this->is_input_section())
2695         return this->u2_.posd->addralign();
2696       return (this->p2align_ == 0
2697               ? 0
2698               : static_cast<uint64_t>(1) << (this->p2align_ - 1));
2699     }
2700
2701     // Return the required size.
2702     off_t
2703     data_size() const;
2704
2705     // Whether this is an input section.
2706     bool
2707     is_input_section() const
2708     {
2709       return (this->shndx_ != OUTPUT_SECTION_CODE
2710               && this->shndx_ != MERGE_DATA_SECTION_CODE
2711               && this->shndx_ != MERGE_STRING_SECTION_CODE
2712               && this->shndx_ != RELAXED_INPUT_SECTION_CODE);
2713     }
2714
2715     // Return whether this is a merge section which matches the
2716     // parameters.
2717     bool
2718     is_merge_section(bool is_string, uint64_t entsize,
2719                      uint64_t addralign) const
2720     {
2721       return (this->shndx_ == (is_string
2722                                ? MERGE_STRING_SECTION_CODE
2723                                : MERGE_DATA_SECTION_CODE)
2724               && this->u1_.entsize == entsize
2725               && this->addralign() == addralign);
2726     }
2727
2728     // Return whether this is a relaxed input section.
2729     bool
2730     is_relaxed_input_section() const
2731     { return this->shndx_ == RELAXED_INPUT_SECTION_CODE; }
2732
2733     // Return whether this is a generic Output_section_data.
2734     bool
2735     is_output_section_data() const
2736     {
2737       return this->shndx_ == OUTPUT_SECTION_CODE;
2738     }
2739
2740     // Return the object for an input section.
2741     Relobj*
2742     relobj() const
2743     {
2744       gold_assert(this->is_input_section());
2745       return this->u2_.object;
2746     }
2747
2748     // Return the input section index for an input section.
2749     unsigned int
2750     shndx() const
2751     {
2752       gold_assert(this->is_input_section());
2753       return this->shndx_;
2754     }
2755
2756     // For non-input-sections, return the associated Output_section_data
2757     // object.
2758     Output_section_data*
2759     output_section_data() const
2760     {
2761       gold_assert(!this->is_input_section());
2762       return this->u2_.posd;
2763     }
2764  
2765     // Return the Output_relaxed_input_section object.
2766     Output_relaxed_input_section*
2767     relaxed_input_section() const
2768     {
2769       gold_assert(this->is_relaxed_input_section());
2770       return this->u2_.poris;
2771     }
2772
2773     // Set the output section.
2774     void
2775     set_output_section(Output_section* os)
2776     {
2777       gold_assert(!this->is_input_section());
2778       Output_section_data *posd = 
2779         this->is_relaxed_input_section() ? this->u2_.poris : this->u2_.posd;
2780       posd->set_output_section(os);
2781     }
2782
2783     // Set the address and file offset.  This is called during
2784     // Layout::finalize.  SECTION_FILE_OFFSET is the file offset of
2785     // the enclosing section.
2786     void
2787     set_address_and_file_offset(uint64_t address, off_t file_offset,
2788                                 off_t section_file_offset);
2789
2790     // Reset the address and file offset.
2791     void
2792     reset_address_and_file_offset();
2793
2794     // Finalize the data size.
2795     void
2796     finalize_data_size();
2797
2798     // Add an input section, for SHF_MERGE sections.
2799     bool
2800     add_input_section(Relobj* object, unsigned int shndx)
2801     {
2802       gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
2803                   || this->shndx_ == MERGE_STRING_SECTION_CODE);
2804       return this->u2_.posd->add_input_section(object, shndx);
2805     }
2806
2807     // Given an input OBJECT, an input section index SHNDX within that
2808     // object, and an OFFSET relative to the start of that input
2809     // section, return whether or not the output offset is known.  If
2810     // this function returns true, it sets *POUTPUT to the offset in
2811     // the output section, relative to the start of the input section
2812     // in the output section.  *POUTPUT may be different from OFFSET
2813     // for a merged section.
2814     bool
2815     output_offset(const Relobj* object, unsigned int shndx,
2816                   section_offset_type offset,
2817                   section_offset_type *poutput) const;
2818
2819     // Return whether this is the merge section for the input section
2820     // SHNDX in OBJECT.
2821     bool
2822     is_merge_section_for(const Relobj* object, unsigned int shndx) const;
2823
2824     // Write out the data.  This does nothing for an input section.
2825     void
2826     write(Output_file*);
2827
2828     // Write the data to a buffer.  This does nothing for an input
2829     // section.
2830     void
2831     write_to_buffer(unsigned char*);
2832
2833     // Print to a map file.
2834     void
2835     print_to_mapfile(Mapfile*) const;
2836
2837     // Print statistics about merge sections to stderr.
2838     void
2839     print_merge_stats(const char* section_name)
2840     {
2841       if (this->shndx_ == MERGE_DATA_SECTION_CODE
2842           || this->shndx_ == MERGE_STRING_SECTION_CODE)
2843         this->u2_.posd->print_merge_stats(section_name);
2844     }
2845
2846    private:
2847     // Code values which appear in shndx_.  If the value is not one of
2848     // these codes, it is the input section index in the object file.
2849     enum
2850     {
2851       // An Output_section_data.
2852       OUTPUT_SECTION_CODE = -1U,
2853       // An Output_section_data for an SHF_MERGE section with
2854       // SHF_STRINGS not set.
2855       MERGE_DATA_SECTION_CODE = -2U,
2856       // An Output_section_data for an SHF_MERGE section with
2857       // SHF_STRINGS set.
2858       MERGE_STRING_SECTION_CODE = -3U,
2859       // An Output_section_data for a relaxed input section.
2860       RELAXED_INPUT_SECTION_CODE = -4U
2861     };
2862
2863     // For an ordinary input section, this is the section index in the
2864     // input file.  For an Output_section_data, this is
2865     // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2866     // MERGE_STRING_SECTION_CODE.
2867     unsigned int shndx_;
2868     // The required alignment, stored as a power of 2.
2869     unsigned int p2align_;
2870     union
2871     {
2872       // For an ordinary input section, the section size.
2873       off_t data_size;
2874       // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not
2875       // used.  For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
2876       // entity size.
2877       uint64_t entsize;
2878     } u1_;
2879     union
2880     {
2881       // For an ordinary input section, the object which holds the
2882       // input section.
2883       Relobj* object;
2884       // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2885       // MERGE_STRING_SECTION_CODE, the data.
2886       Output_section_data* posd;
2887       // For RELAXED_INPUT_SECTION_CODE, the data.
2888       Output_relaxed_input_section* poris;
2889     } u2_;
2890   };
2891
2892   typedef std::vector<Input_section> Input_section_list;
2893
2894   // We only save enough information to undo the effects of section layout.
2895   class Checkpoint_output_section
2896   {
2897    public:
2898     Checkpoint_output_section(uint64_t addralign, elfcpp::Elf_Xword flags,
2899                               const Input_section_list& input_sections,
2900                               off_t first_input_offset,
2901                               bool attached_input_sections_are_sorted)
2902       : addralign_(addralign), flags_(flags),
2903         input_sections_(input_sections),
2904         input_sections_size_(input_sections_.size()),
2905         input_sections_copy_(), first_input_offset_(first_input_offset),
2906         attached_input_sections_are_sorted_(attached_input_sections_are_sorted)
2907     { }
2908
2909     virtual
2910     ~Checkpoint_output_section()
2911     { }
2912
2913     // Return the address alignment.
2914     uint64_t
2915     addralign() const
2916     { return this->addralign_; }
2917
2918     // Return the section flags.
2919     elfcpp::Elf_Xword
2920     flags() const
2921     { return this->flags_; }
2922
2923     // Return a reference to the input section list copy.
2924     const Input_section_list&
2925     input_sections() const
2926     { return this->input_sections_copy_; }
2927
2928     // Return the size of input_sections at the time when checkpoint is
2929     // taken.
2930     size_t
2931     input_sections_size() const
2932     { return this->input_sections_size_; }
2933
2934     // Whether input sections are copied.
2935     bool
2936     input_sections_saved() const
2937     { return this->input_sections_copy_.size() == this->input_sections_size_; }
2938
2939     off_t
2940     first_input_offset() const
2941     { return this->first_input_offset_; }
2942
2943     bool
2944     attached_input_sections_are_sorted() const
2945     { return this->attached_input_sections_are_sorted_; }
2946
2947     // Save input sections.
2948     void
2949     save_input_sections()
2950     {
2951       this->input_sections_copy_.reserve(this->input_sections_size_);
2952       this->input_sections_copy_.clear();
2953       Input_section_list::const_iterator p = this->input_sections_.begin();
2954       gold_assert(this->input_sections_size_ >= this->input_sections_.size());
2955       for(size_t i = 0; i < this->input_sections_size_ ; i++, ++p)
2956         this->input_sections_copy_.push_back(*p);
2957     }
2958
2959    private:
2960     // The section alignment.
2961     uint64_t addralign_;
2962     // The section flags.
2963     elfcpp::Elf_Xword flags_;
2964     // Reference to the input sections to be checkpointed.
2965     const Input_section_list& input_sections_;
2966     // Size of the checkpointed portion of input_sections_;
2967     size_t input_sections_size_;
2968     // Copy of input sections.
2969     Input_section_list input_sections_copy_;
2970     // The offset of the first entry in input_sections_.
2971     off_t first_input_offset_;
2972     // True if the input sections attached to this output section have
2973     // already been sorted.
2974     bool attached_input_sections_are_sorted_;
2975   };
2976
2977  private:
2978   // This class is used to sort the input sections.
2979   class Input_section_sort_entry;
2980
2981   // This is the sort comparison function.
2982   struct Input_section_sort_compare
2983   {
2984     bool
2985     operator()(const Input_section_sort_entry&,
2986                const Input_section_sort_entry&) const;
2987   };
2988
2989   // Fill data.  This is used to fill in data between input sections.
2990   // It is also used for data statements (BYTE, WORD, etc.) in linker
2991   // scripts.  When we have to keep track of the input sections, we
2992   // can use an Output_data_const, but we don't want to have to keep
2993   // track of input sections just to implement fills.
2994   class Fill
2995   {
2996    public:
2997     Fill(off_t section_offset, off_t length)
2998       : section_offset_(section_offset),
2999         length_(convert_to_section_size_type(length))
3000     { }
3001
3002     // Return section offset.
3003     off_t
3004     section_offset() const
3005     { return this->section_offset_; }
3006
3007     // Return fill length.
3008     section_size_type
3009     length() const
3010     { return this->length_; }
3011
3012    private:
3013     // The offset within the output section.
3014     off_t section_offset_;
3015     // The length of the space to fill.
3016     section_size_type length_;
3017   };
3018
3019   typedef std::vector<Fill> Fill_list;
3020
3021   // Add a new output section by Input_section.
3022   void
3023   add_output_section_data(Input_section*);
3024
3025   // Add an SHF_MERGE input section.  Returns true if the section was
3026   // handled.
3027   bool
3028   add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
3029                           uint64_t entsize, uint64_t addralign);
3030
3031   // Add an output SHF_MERGE section POSD to this output section.
3032   // IS_STRING indicates whether it is a SHF_STRINGS section, and
3033   // ENTSIZE is the entity size.  This returns the entry added to
3034   // input_sections_.
3035   void
3036   add_output_merge_section(Output_section_data* posd, bool is_string,
3037                            uint64_t entsize);
3038
3039   // Relax an existing input section.
3040   void
3041   relax_input_section(Output_relaxed_input_section*);
3042
3043   // Sort the attached input sections.
3044   void
3045   sort_attached_input_sections();
3046
3047   // Most of these fields are only valid after layout.
3048
3049   // The name of the section.  This will point into a Stringpool.
3050   const char* name_;
3051   // The section address is in the parent class.
3052   // The section alignment.
3053   uint64_t addralign_;
3054   // The section entry size.
3055   uint64_t entsize_;
3056   // The load address.  This is only used when using a linker script
3057   // with a SECTIONS clause.  The has_load_address_ field indicates
3058   // whether this field is valid.
3059   uint64_t load_address_;
3060   // The file offset is in the parent class.
3061   // Set the section link field to the index of this section.
3062   const Output_data* link_section_;
3063   // If link_section_ is NULL, this is the link field.
3064   unsigned int link_;
3065   // Set the section info field to the index of this section.
3066   const Output_section* info_section_;
3067   // If info_section_ is NULL, set the info field to the symbol table
3068   // index of this symbol.
3069   const Symbol* info_symndx_;
3070   // If info_section_ and info_symndx_ are NULL, this is the section
3071   // info field.
3072   unsigned int info_;
3073   // The section type.
3074   const elfcpp::Elf_Word type_;
3075   // The section flags.
3076   elfcpp::Elf_Xword flags_;
3077   // The section index.
3078   unsigned int out_shndx_;
3079   // If there is a STT_SECTION for this output section in the normal
3080   // symbol table, this is the symbol index.  This starts out as zero.
3081   // It is initialized in Layout::finalize() to be the index, or -1U
3082   // if there isn't one.
3083   unsigned int symtab_index_;
3084   // If there is a STT_SECTION for this output section in the dynamic
3085   // symbol table, this is the symbol index.  This starts out as zero.
3086   // It is initialized in Layout::finalize() to be the index, or -1U
3087   // if there isn't one.
3088   unsigned int dynsym_index_;
3089   // The input sections.  This will be empty in cases where we don't
3090   // need to keep track of them.
3091   Input_section_list input_sections_;
3092   // The offset of the first entry in input_sections_.
3093   off_t first_input_offset_;
3094   // The fill data.  This is separate from input_sections_ because we
3095   // often will need fill sections without needing to keep track of
3096   // input sections.
3097   Fill_list fills_;
3098   // If the section requires postprocessing, this buffer holds the
3099   // section contents during relocation.
3100   unsigned char* postprocessing_buffer_;
3101   // Whether this output section needs a STT_SECTION symbol in the
3102   // normal symbol table.  This will be true if there is a relocation
3103   // which needs it.
3104   bool needs_symtab_index_ : 1;
3105   // Whether this output section needs a STT_SECTION symbol in the
3106   // dynamic symbol table.  This will be true if there is a dynamic
3107   // relocation which needs it.
3108   bool needs_dynsym_index_ : 1;
3109   // Whether the link field of this output section should point to the
3110   // normal symbol table.
3111   bool should_link_to_symtab_ : 1;
3112   // Whether the link field of this output section should point to the
3113   // dynamic symbol table.
3114   bool should_link_to_dynsym_ : 1;
3115   // Whether this section should be written after all the input
3116   // sections are complete.
3117   bool after_input_sections_ : 1;
3118   // Whether this section requires post processing after all
3119   // relocations have been applied.
3120   bool requires_postprocessing_ : 1;
3121   // Whether an input section was mapped to this output section
3122   // because of a SECTIONS clause in a linker script.
3123   bool found_in_sections_clause_ : 1;
3124   // Whether this section has an explicitly specified load address.
3125   bool has_load_address_ : 1;
3126   // True if the info_section_ field means the section index of the
3127   // section, false if it means the symbol index of the corresponding
3128   // section symbol.
3129   bool info_uses_section_index_ : 1;
3130   // True if the input sections attached to this output section may
3131   // need sorting.
3132   bool may_sort_attached_input_sections_ : 1;
3133   // True if the input sections attached to this output section must
3134   // be sorted.
3135   bool must_sort_attached_input_sections_ : 1;
3136   // True if the input sections attached to this output section have
3137   // already been sorted.
3138   bool attached_input_sections_are_sorted_ : 1;
3139   // True if this section holds relro data.
3140   bool is_relro_ : 1;
3141   // True if this section holds relro local data.
3142   bool is_relro_local_ : 1;
3143   // True if this is a small section.
3144   bool is_small_section_ : 1;
3145   // True if this is a large section.
3146   bool is_large_section_ : 1;
3147   // For SHT_TLS sections, the offset of this section relative to the base
3148   // of the TLS segment.
3149   uint64_t tls_offset_;
3150   // Saved checkpoint.
3151   Checkpoint_output_section* checkpoint_;
3152 };
3153
3154 // An output segment.  PT_LOAD segments are built from collections of
3155 // output sections.  Other segments typically point within PT_LOAD
3156 // segments, and are built directly as needed.
3157 //
3158 // NOTE: We want to use the copy constructor for this class.  During
3159 // relaxation, we may try built the segments multiple times.  We do
3160 // that by copying the original segment list before lay-out, doing
3161 // a trial lay-out and roll-back to the saved copied if we need to
3162 // to the lay-out again.
3163
3164 class Output_segment
3165 {
3166  public:
3167   // Create an output segment, specifying the type and flags.
3168   Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
3169
3170   // Return the virtual address.
3171   uint64_t
3172   vaddr() const
3173   { return this->vaddr_; }
3174
3175   // Return the physical address.
3176   uint64_t
3177   paddr() const
3178   { return this->paddr_; }
3179
3180   // Return the segment type.
3181   elfcpp::Elf_Word
3182   type() const
3183   { return this->type_; }
3184
3185   // Return the segment flags.
3186   elfcpp::Elf_Word
3187   flags() const
3188   { return this->flags_; }
3189
3190   // Return the memory size.
3191   uint64_t
3192   memsz() const
3193   { return this->memsz_; }
3194
3195   // Return the file size.
3196   off_t
3197   filesz() const
3198   { return this->filesz_; }
3199
3200   // Return the file offset.
3201   off_t
3202   offset() const
3203   { return this->offset_; }
3204
3205   // Whether this is a segment created to hold large data sections.
3206   bool
3207   is_large_data_segment() const
3208   { return this->is_large_data_segment_; }
3209
3210   // Record that this is a segment created to hold large data
3211   // sections.
3212   void
3213   set_is_large_data_segment()
3214   { this->is_large_data_segment_ = true; }
3215
3216   // Return the maximum alignment of the Output_data.
3217   uint64_t
3218   maximum_alignment();
3219
3220   // Add an Output_section to this segment.
3221   void
3222   add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags);
3223
3224   // Remove an Output_section from this segment.  It is an error if it
3225   // is not present.
3226   void
3227   remove_output_section(Output_section* os);
3228
3229   // Add an Output_data (which is not an Output_section) to the start
3230   // of this segment.
3231   void
3232   add_initial_output_data(Output_data*);
3233
3234   // Return true if this segment has any sections which hold actual
3235   // data, rather than being a BSS section.
3236   bool
3237   has_any_data_sections() const
3238   { return !this->output_data_.empty(); }
3239
3240   // Return the number of dynamic relocations applied to this segment.
3241   unsigned int
3242   dynamic_reloc_count() const;
3243
3244   // Return the address of the first section.
3245   uint64_t
3246   first_section_load_address() const;
3247
3248   // Return whether the addresses have been set already.
3249   bool
3250   are_addresses_set() const
3251   { return this->are_addresses_set_; }
3252
3253   // Set the addresses.
3254   void
3255   set_addresses(uint64_t vaddr, uint64_t paddr)
3256   {
3257     this->vaddr_ = vaddr;
3258     this->paddr_ = paddr;
3259     this->are_addresses_set_ = true;
3260   }
3261
3262   // Set the segment flags.  This is only used if we have a PHDRS
3263   // clause which explicitly specifies the flags.
3264   void
3265   set_flags(elfcpp::Elf_Word flags)
3266   { this->flags_ = flags; }
3267
3268   // Set the address of the segment to ADDR and the offset to *POFF
3269   // and set the addresses and offsets of all contained output
3270   // sections accordingly.  Set the section indexes of all contained
3271   // output sections starting with *PSHNDX.  If RESET is true, first
3272   // reset the addresses of the contained sections.  Return the
3273   // address of the immediately following segment.  Update *POFF and
3274   // *PSHNDX.  This should only be called for a PT_LOAD segment.
3275   uint64_t
3276   set_section_addresses(const Layout*, bool reset, uint64_t addr, off_t* poff,
3277                         unsigned int* pshndx);
3278
3279   // Set the minimum alignment of this segment.  This may be adjusted
3280   // upward based on the section alignments.
3281   void
3282   set_minimum_p_align(uint64_t align)
3283   { this->min_p_align_ = align; }
3284
3285   // Set the offset of this segment based on the section.  This should
3286   // only be called for a non-PT_LOAD segment.
3287   void
3288   set_offset();
3289
3290   // Set the TLS offsets of the sections contained in the PT_TLS segment.
3291   void
3292   set_tls_offsets();
3293
3294   // Return the number of output sections.
3295   unsigned int
3296   output_section_count() const;
3297
3298   // Return the section attached to the list segment with the lowest
3299   // load address.  This is used when handling a PHDRS clause in a
3300   // linker script.
3301   Output_section*
3302   section_with_lowest_load_address() const;
3303
3304   // Write the segment header into *OPHDR.
3305   template<int size, bool big_endian>
3306   void
3307   write_header(elfcpp::Phdr_write<size, big_endian>*);
3308
3309   // Write the section headers of associated sections into V.
3310   template<int size, bool big_endian>
3311   unsigned char*
3312   write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
3313                         unsigned int* pshndx) const;
3314
3315   // Print the output sections in the map file.
3316   void
3317   print_sections_to_mapfile(Mapfile*) const;
3318
3319  private:
3320   typedef std::list<Output_data*> Output_data_list;
3321
3322   // Find the maximum alignment in an Output_data_list.
3323   static uint64_t
3324   maximum_alignment_list(const Output_data_list*);
3325
3326   // Return whether the first data section is a relro section.
3327   bool
3328   is_first_section_relro() const;
3329
3330   // Set the section addresses in an Output_data_list.
3331   uint64_t
3332   set_section_list_addresses(const Layout*, bool reset, Output_data_list*,
3333                              uint64_t addr, off_t* poff, unsigned int* pshndx,
3334                              bool* in_tls, bool* in_relro);
3335
3336   // Return the number of Output_sections in an Output_data_list.
3337   unsigned int
3338   output_section_count_list(const Output_data_list*) const;
3339
3340   // Return the number of dynamic relocs in an Output_data_list.
3341   unsigned int
3342   dynamic_reloc_count_list(const Output_data_list*) const;
3343
3344   // Find the section with the lowest load address in an
3345   // Output_data_list.
3346   void
3347   lowest_load_address_in_list(const Output_data_list* pdl,
3348                               Output_section** found,
3349                               uint64_t* found_lma) const;
3350
3351   // Write the section headers in the list into V.
3352   template<int size, bool big_endian>
3353   unsigned char*
3354   write_section_headers_list(const Layout*, const Stringpool*,
3355                              const Output_data_list*, unsigned char* v,
3356                              unsigned int* pshdx) const;
3357
3358   // Print a section list to the mapfile.
3359   void
3360   print_section_list_to_mapfile(Mapfile*, const Output_data_list*) const;
3361
3362   // NOTE: We want to use the copy constructor.  Currently, shallow copy
3363   // works for us so we do not need to write our own copy constructor.
3364   
3365   // The list of output data with contents attached to this segment.
3366   Output_data_list output_data_;
3367   // The list of output data without contents attached to this segment.
3368   Output_data_list output_bss_;
3369   // The segment virtual address.
3370   uint64_t vaddr_;
3371   // The segment physical address.
3372   uint64_t paddr_;
3373   // The size of the segment in memory.
3374   uint64_t memsz_;
3375   // The maximum section alignment.  The is_max_align_known_ field
3376   // indicates whether this has been finalized.
3377   uint64_t max_align_;
3378   // The required minimum value for the p_align field.  This is used
3379   // for PT_LOAD segments.  Note that this does not mean that
3380   // addresses should be aligned to this value; it means the p_paddr
3381   // and p_vaddr fields must be congruent modulo this value.  For
3382   // non-PT_LOAD segments, the dynamic linker works more efficiently
3383   // if the p_align field has the more conventional value, although it
3384   // can align as needed.
3385   uint64_t min_p_align_;
3386   // The offset of the segment data within the file.
3387   off_t offset_;
3388   // The size of the segment data in the file.
3389   off_t filesz_;
3390   // The segment type;
3391   elfcpp::Elf_Word type_;
3392   // The segment flags.
3393   elfcpp::Elf_Word flags_;
3394   // Whether we have finalized max_align_.
3395   bool is_max_align_known_ : 1;
3396   // Whether vaddr and paddr were set by a linker script.
3397   bool are_addresses_set_ : 1;
3398   // Whether this segment holds large data sections.
3399   bool is_large_data_segment_ : 1;
3400 };
3401
3402 // This class represents the output file.
3403
3404 class Output_file
3405 {
3406  public:
3407   Output_file(const char* name);
3408
3409   // Indicate that this is a temporary file which should not be
3410   // output.
3411   void
3412   set_is_temporary()
3413   { this->is_temporary_ = true; }
3414
3415   // Try to open an existing file. Returns false if the file doesn't
3416   // exist, has a size of 0 or can't be mmaped.  This method is
3417   // thread-unsafe.
3418   bool
3419   open_for_modification();
3420
3421   // Open the output file.  FILE_SIZE is the final size of the file.
3422   // If the file already exists, it is deleted/truncated.  This method
3423   // is thread-unsafe.
3424   void
3425   open(off_t file_size);
3426
3427   // Resize the output file.  This method is thread-unsafe.
3428   void
3429   resize(off_t file_size);
3430
3431   // Close the output file (flushing all buffered data) and make sure
3432   // there are no errors.  This method is thread-unsafe.
3433   void
3434   close();
3435
3436   // Return the size of this file.
3437   off_t
3438   filesize()
3439   { return this->file_size_; }
3440
3441   // We currently always use mmap which makes the view handling quite
3442   // simple.  In the future we may support other approaches.
3443
3444   // Write data to the output file.
3445   void
3446   write(off_t offset, const void* data, size_t len)
3447   { memcpy(this->base_ + offset, data, len); }
3448
3449   // Get a buffer to use to write to the file, given the offset into
3450   // the file and the size.
3451   unsigned char*
3452   get_output_view(off_t start, size_t size)
3453   {
3454     gold_assert(start >= 0
3455                 && start + static_cast<off_t>(size) <= this->file_size_);
3456     return this->base_ + start;
3457   }
3458
3459   // VIEW must have been returned by get_output_view.  Write the
3460   // buffer to the file, passing in the offset and the size.
3461   void
3462   write_output_view(off_t, size_t, unsigned char*)
3463   { }
3464
3465   // Get a read/write buffer.  This is used when we want to write part
3466   // of the file, read it in, and write it again.
3467   unsigned char*
3468   get_input_output_view(off_t start, size_t size)
3469   { return this->get_output_view(start, size); }
3470
3471   // Write a read/write buffer back to the file.
3472   void
3473   write_input_output_view(off_t, size_t, unsigned char*)
3474   { }
3475
3476   // Get a read buffer.  This is used when we just want to read part
3477   // of the file back it in.
3478   const unsigned char*
3479   get_input_view(off_t start, size_t size)
3480   { return this->get_output_view(start, size); }
3481
3482   // Release a read bfufer.
3483   void
3484   free_input_view(off_t, size_t, const unsigned char*)
3485   { }
3486
3487  private:
3488   // Map the file into memory or, if that fails, allocate anonymous
3489   // memory.
3490   void
3491   map();
3492
3493   // Allocate anonymous memory for the file.
3494   bool
3495   map_anonymous();
3496
3497   // Map the file into memory.
3498   bool
3499   map_no_anonymous();
3500
3501   // Unmap the file from memory (and flush to disk buffers).
3502   void
3503   unmap();
3504
3505   // File name.
3506   const char* name_;
3507   // File descriptor.
3508   int o_;
3509   // File size.
3510   off_t file_size_;
3511   // Base of file mapped into memory.
3512   unsigned char* base_;
3513   // True iff base_ points to a memory buffer rather than an output file.
3514   bool map_is_anonymous_;
3515   // True if this is a temporary file which should not be output.
3516   bool is_temporary_;
3517 };
3518
3519 } // End namespace gold.
3520
3521 #endif // !defined(GOLD_OUTPUT_H)
This page took 0.210588 seconds and 2 git commands to generate.