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