Correct handling of non-section symbol in merged section. Avoid some
[external/binutils.git] / gold / output.h
1 // output.h -- manage the output file for gold   -*- C++ -*-
2
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #ifndef GOLD_OUTPUT_H
24 #define GOLD_OUTPUT_H
25
26 #include <list>
27 #include <vector>
28
29 #include "elfcpp.h"
30 #include "layout.h"
31 #include "reloc-types.h"
32
33 namespace gold
34 {
35
36 class General_options;
37 class Object;
38 class Symbol;
39 class Output_file;
40 class Output_section;
41 class Target;
42 template<int size, bool big_endian>
43 class Sized_target;
44 template<int size, bool big_endian>
45 class Sized_relobj;
46
47 // An abtract class for data which has to go into the output file.
48
49 class Output_data
50 {
51  public:
52   explicit Output_data()
53     : address_(0), data_size_(0), offset_(-1),
54       is_address_valid_(false), is_data_size_valid_(false),
55       is_offset_valid_(false),
56       dynamic_reloc_count_(0)
57   { }
58
59   virtual
60   ~Output_data();
61
62   // Return the address.  For allocated sections, this is only valid
63   // after Layout::finalize is finished.
64   uint64_t
65   address() const
66   {
67     gold_assert(this->is_address_valid_);
68     return this->address_;
69   }
70
71   // Return the size of the data.  For allocated sections, this must
72   // be valid after Layout::finalize calls set_address, but need not
73   // be valid before then.
74   off_t
75   data_size() const
76   {
77     gold_assert(this->is_data_size_valid_);
78     return this->data_size_;
79   }
80
81   // Return the file offset.  This is only valid after
82   // Layout::finalize is finished.  For some non-allocated sections,
83   // it may not be valid until near the end of the link.
84   off_t
85   offset() const
86   {
87     gold_assert(this->is_offset_valid_);
88     return this->offset_;
89   }
90
91   // Return the required alignment.
92   uint64_t
93   addralign() const
94   { return this->do_addralign(); }
95
96   // Return whether this is an Output_section.
97   bool
98   is_section() const
99   { return this->do_is_section(); }
100
101   // Return whether this is an Output_section of the specified type.
102   bool
103   is_section_type(elfcpp::Elf_Word stt) const
104   { return this->do_is_section_type(stt); }
105
106   // Return whether this is an Output_section with the specified flag
107   // set.
108   bool
109   is_section_flag_set(elfcpp::Elf_Xword shf) const
110   { return this->do_is_section_flag_set(shf); }
111
112   // Return the output section index, if there is an output section.
113   unsigned int
114   out_shndx() const
115   { return this->do_out_shndx(); }
116
117   // Set the output section index, if this is an output section.
118   void
119   set_out_shndx(unsigned int shndx)
120   { this->do_set_out_shndx(shndx); }
121
122   // Set the address and file offset of this data, and finalize the
123   // size of the data.  This is called during Layout::finalize for
124   // allocated sections.
125   void
126   set_address_and_file_offset(uint64_t addr, off_t off)
127   {
128     this->set_address(addr);
129     this->set_file_offset(off);
130     this->finalize_data_size();
131   }
132
133   // Set the address.
134   void
135   set_address(uint64_t addr)
136   {
137     gold_assert(!this->is_address_valid_);
138     this->address_ = addr;
139     this->is_address_valid_ = true;
140   }
141
142   // Set the file offset.
143   void
144   set_file_offset(off_t off)
145   {
146     gold_assert(!this->is_offset_valid_);
147     this->offset_ = off;
148     this->is_offset_valid_ = true;
149   }
150
151   // Finalize the data size.
152   void
153   finalize_data_size()
154   {
155     if (!this->is_data_size_valid_)
156       {
157         // Tell the child class to set the data size.
158         this->set_final_data_size();
159         gold_assert(this->is_data_size_valid_);
160       }
161   }
162
163   // Set the TLS offset.  Called only for SHT_TLS sections.
164   void
165   set_tls_offset(uint64_t tls_base)
166   { this->do_set_tls_offset(tls_base); }
167
168   // Return the TLS offset, relative to the base of the TLS segment.
169   // Valid only for SHT_TLS sections.
170   uint64_t
171   tls_offset() const
172   { return this->do_tls_offset(); }
173
174   // Write the data to the output file.  This is called after
175   // Layout::finalize is complete.
176   void
177   write(Output_file* file)
178   { this->do_write(file); }
179
180   // This is called by Layout::finalize to note that the sizes of
181   // allocated sections must now be fixed.
182   static void
183   layout_complete()
184   { Output_data::allocated_sizes_are_fixed = true; }
185
186   // Used to check that layout has been done.
187   static bool
188   is_layout_complete()
189   { return Output_data::allocated_sizes_are_fixed; }
190
191   // Count the number of dynamic relocations applied to this section.
192   void
193   add_dynamic_reloc()
194   { ++this->dynamic_reloc_count_; }
195
196   // Return the number of dynamic relocations applied to this section.
197   unsigned int
198   dynamic_reloc_count() const
199   { return this->dynamic_reloc_count_; }
200
201   // Whether the address is valid.
202   bool
203   is_address_valid() const
204   { return this->is_address_valid_; }
205
206   // Whether the file offset is valid.
207   bool
208   is_offset_valid() const
209   { return this->is_offset_valid_; }
210
211   // Whether the data size is valid.
212   bool
213   is_data_size_valid() const
214   { return this->is_data_size_valid_; }
215
216  protected:
217   // Functions that child classes may or in some cases must implement.
218
219   // Write the data to the output file.
220   virtual void
221   do_write(Output_file*) = 0;
222
223   // Return the required alignment.
224   virtual uint64_t
225   do_addralign() const = 0;
226
227   // Return whether this is an Output_section.
228   virtual bool
229   do_is_section() const
230   { return false; }
231
232   // Return whether this is an Output_section of the specified type.
233   // This only needs to be implement by Output_section.
234   virtual bool
235   do_is_section_type(elfcpp::Elf_Word) const
236   { return false; }
237
238   // Return whether this is an Output_section with the specific flag
239   // set.  This only needs to be implemented by Output_section.
240   virtual bool
241   do_is_section_flag_set(elfcpp::Elf_Xword) const
242   { return false; }
243
244   // Return the output section index, if there is an output section.
245   virtual unsigned int
246   do_out_shndx() const
247   { gold_unreachable(); }
248
249   // Set the output section index, if this is an output section.
250   virtual void
251   do_set_out_shndx(unsigned int)
252   { gold_unreachable(); }
253
254   // This is a hook for derived classes to set the data size.  This is
255   // called by finalize_data_size, normally called during
256   // Layout::finalize, when the section address is set.
257   virtual void
258   set_final_data_size()
259   { gold_unreachable(); }
260
261   // Set the TLS offset.  Called only for SHT_TLS sections.
262   virtual void
263   do_set_tls_offset(uint64_t)
264   { gold_unreachable(); }
265
266   // Return the TLS offset, relative to the base of the TLS segment.
267   // Valid only for SHT_TLS sections.
268   virtual uint64_t
269   do_tls_offset() const
270   { gold_unreachable(); }
271
272   // Functions that child classes may call.
273
274   // Set the size of the data.
275   void
276   set_data_size(off_t data_size)
277   {
278     gold_assert(!this->is_data_size_valid_);
279     this->data_size_ = data_size;
280     this->is_data_size_valid_ = true;
281   }
282
283   // Get the current data size--this is for the convenience of
284   // sections which build up their size over time.
285   off_t
286   current_data_size_for_child() const
287   { return this->data_size_; }
288
289   // Set the current data size--this is for the convenience of
290   // sections which build up their size over time.
291   void
292   set_current_data_size_for_child(off_t data_size)
293   {
294     gold_assert(!this->is_data_size_valid_);
295     this->data_size_ = data_size;
296   }
297
298   // Return default alignment for the target size.
299   static uint64_t
300   default_alignment();
301
302   // Return default alignment for a specified size--32 or 64.
303   static uint64_t
304   default_alignment_for_size(int size);
305
306  private:
307   Output_data(const Output_data&);
308   Output_data& operator=(const Output_data&);
309
310   // This is used for verification, to make sure that we don't try to
311   // change any sizes of allocated sections after we set the section
312   // addresses.
313   static bool allocated_sizes_are_fixed;
314
315   // Memory address in output file.
316   uint64_t address_;
317   // Size of data in output file.
318   off_t data_size_;
319   // File offset of contents in output file.
320   off_t offset_;
321   // Whether address_ is valid.
322   bool is_address_valid_;
323   // Whether data_size_ is valid.
324   bool is_data_size_valid_;
325   // Whether offset_ is valid.
326   bool is_offset_valid_;
327   // Count of dynamic relocations applied to this section.
328   unsigned int dynamic_reloc_count_;
329 };
330
331 // Output the section headers.
332
333 class Output_section_headers : public Output_data
334 {
335  public:
336   Output_section_headers(const Layout*,
337                          const Layout::Segment_list*,
338                          const Layout::Section_list*,
339                          const Stringpool*);
340
341  protected:
342   // Write the data to the file.
343   void
344   do_write(Output_file*);
345
346   // Return the required alignment.
347   uint64_t
348   do_addralign() const
349   { return Output_data::default_alignment(); }
350
351  private:
352   // Write the data to the file with the right size and endianness.
353   template<int size, bool big_endian>
354   void
355   do_sized_write(Output_file*);
356
357   const Layout* layout_;
358   const Layout::Segment_list* segment_list_;
359   const Layout::Section_list* unattached_section_list_;
360   const Stringpool* secnamepool_;
361 };
362
363 // Output the segment headers.
364
365 class Output_segment_headers : public Output_data
366 {
367  public:
368   Output_segment_headers(const Layout::Segment_list& segment_list);
369
370  protected:
371   // Write the data to the file.
372   void
373   do_write(Output_file*);
374
375   // Return the required alignment.
376   uint64_t
377   do_addralign() const
378   { return Output_data::default_alignment(); }
379
380  private:
381   // Write the data to the file with the right size and endianness.
382   template<int size, bool big_endian>
383   void
384   do_sized_write(Output_file*);
385
386   const Layout::Segment_list& segment_list_;
387 };
388
389 // Output the ELF file header.
390
391 class Output_file_header : public Output_data
392 {
393  public:
394   Output_file_header(const Target*,
395                      const Symbol_table*,
396                      const Output_segment_headers*);
397
398   // Add information about the section headers.  We lay out the ELF
399   // file header before we create the section headers.
400   void set_section_info(const Output_section_headers*,
401                         const Output_section* shstrtab);
402
403  protected:
404   // Write the data to the file.
405   void
406   do_write(Output_file*);
407
408   // Return the required alignment.
409   uint64_t
410   do_addralign() const
411   { return Output_data::default_alignment(); }
412
413  private:
414   // Write the data to the file with the right size and endianness.
415   template<int size, bool big_endian>
416   void
417   do_sized_write(Output_file*);
418
419   const Target* target_;
420   const Symbol_table* symtab_;
421   const Output_segment_headers* segment_header_;
422   const Output_section_headers* section_header_;
423   const Output_section* shstrtab_;
424 };
425
426 // Output sections are mainly comprised of input sections.  However,
427 // there are cases where we have data to write out which is not in an
428 // input section.  Output_section_data is used in such cases.  This is
429 // an abstract base class.
430
431 class Output_section_data : public Output_data
432 {
433  public:
434   Output_section_data(off_t data_size, uint64_t addralign)
435     : Output_data(), output_section_(NULL), addralign_(addralign)
436   { this->set_data_size(data_size); }
437
438   Output_section_data(uint64_t addralign)
439     : Output_data(), output_section_(NULL), addralign_(addralign)
440   { }
441
442   // Return the output section.
443   const Output_section*
444   output_section() const
445   { return this->output_section_; }
446
447   // Record the output section.
448   void
449   set_output_section(Output_section* os);
450
451   // Add an input section, for SHF_MERGE sections.  This returns true
452   // if the section was handled.
453   bool
454   add_input_section(Relobj* object, unsigned int shndx)
455   { return this->do_add_input_section(object, shndx); }
456
457   // Given an input OBJECT, an input section index SHNDX within that
458   // object, and an OFFSET relative to the start of that input
459   // section, return whether or not the corresponding offset within
460   // the output section is known.  If this function returns true, it
461   // sets *POUTPUT to the output offset.  The value -1 indicates that
462   // this input offset is being discarded.
463   bool
464   output_offset(const Relobj* object, unsigned int shndx,
465                 section_offset_type offset,
466                 section_offset_type *poutput) const
467   { return this->do_output_offset(object, shndx, offset, poutput); }
468
469   // Return whether this is the merge section for the input section
470   // SHNDX in OBJECT.  This should return true when output_offset
471   // would return true for some values of OFFSET.
472   bool
473   is_merge_section_for(const Relobj* object, unsigned int shndx) const
474   { return this->do_is_merge_section_for(object, shndx); }
475
476   // Write the contents to a buffer.  This is used for sections which
477   // require postprocessing, such as compression.
478   void
479   write_to_buffer(unsigned char* buffer)
480   { this->do_write_to_buffer(buffer); }
481
482   // Print merge stats to stderr.  This should only be called for
483   // SHF_MERGE sections.
484   void
485   print_merge_stats(const char* section_name)
486   { this->do_print_merge_stats(section_name); }
487
488  protected:
489   // The child class must implement do_write.
490
491   // The child class may implement specific adjustments to the output
492   // section.
493   virtual void
494   do_adjust_output_section(Output_section*)
495   { }
496
497   // May be implemented by child class.  Return true if the section
498   // was handled.
499   virtual bool
500   do_add_input_section(Relobj*, unsigned int)
501   { gold_unreachable(); }
502
503   // The child class may implement output_offset.
504   virtual bool
505   do_output_offset(const Relobj*, unsigned int, section_offset_type,
506                    section_offset_type*) const
507   { return false; }
508
509   // The child class may implement is_merge_section_for.
510   virtual bool
511   do_is_merge_section_for(const Relobj*, unsigned int) const
512   { return false; }
513
514   // The child class may implement write_to_buffer.  Most child
515   // classes can not appear in a compressed section, and they do not
516   // implement this.
517   virtual void
518   do_write_to_buffer(unsigned char*)
519   { gold_unreachable(); }
520
521   // Print merge statistics.
522   virtual void
523   do_print_merge_stats(const char*)
524   { gold_unreachable(); }
525
526   // Return the required alignment.
527   uint64_t
528   do_addralign() const
529   { return this->addralign_; }
530
531   // Return the section index of the output section.
532   unsigned int
533   do_out_shndx() const;
534
535   // Set the alignment.
536   void
537   set_addralign(uint64_t addralign)
538   { this->addralign_ = addralign; }
539
540  private:
541   // The output section for this section.
542   const Output_section* output_section_;
543   // The required alignment.
544   uint64_t addralign_;
545 };
546
547 // Some Output_section_data classes build up their data step by step,
548 // rather than all at once.  This class provides an interface for
549 // them.
550
551 class Output_section_data_build : public Output_section_data
552 {
553  public:
554   Output_section_data_build(uint64_t addralign)
555     : Output_section_data(addralign)
556   { }
557
558   // Get the current data size.
559   off_t
560   current_data_size() const
561   { return this->current_data_size_for_child(); }
562
563   // Set the current data size.
564   void
565   set_current_data_size(off_t data_size)
566   { this->set_current_data_size_for_child(data_size); }
567
568  protected:
569   // Set the final data size.
570   virtual void
571   set_final_data_size()
572   { this->set_data_size(this->current_data_size_for_child()); }
573 };
574
575 // A simple case of Output_data in which we have constant data to
576 // output.
577
578 class Output_data_const : public Output_section_data
579 {
580  public:
581   Output_data_const(const std::string& data, uint64_t addralign)
582     : Output_section_data(data.size(), addralign), data_(data)
583   { }
584
585   Output_data_const(const char* p, off_t len, uint64_t addralign)
586     : Output_section_data(len, addralign), data_(p, len)
587   { }
588
589   Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
590     : Output_section_data(len, addralign),
591       data_(reinterpret_cast<const char*>(p), len)
592   { }
593
594  protected:
595   // Write the data to the output file.
596   void
597   do_write(Output_file*);
598
599   // Write the data to a buffer.
600   void
601   do_write_to_buffer(unsigned char* buffer)
602   { memcpy(buffer, this->data_.data(), this->data_.size()); }
603
604  private:
605   std::string data_;
606 };
607
608 // Another version of Output_data with constant data, in which the
609 // buffer is allocated by the caller.
610
611 class Output_data_const_buffer : public Output_section_data
612 {
613  public:
614   Output_data_const_buffer(const unsigned char* p, off_t len,
615                            uint64_t addralign)
616     : Output_section_data(len, addralign), p_(p)
617   { }
618
619  protected:
620   // Write the data the output file.
621   void
622   do_write(Output_file*);
623
624   // Write the data to a buffer.
625   void
626   do_write_to_buffer(unsigned char* buffer)
627   { memcpy(buffer, this->p_, this->data_size()); }
628
629  private:
630   const unsigned char* p_;
631 };
632
633 // A place holder for a fixed amount of data written out via some
634 // other mechanism.
635
636 class Output_data_fixed_space : public Output_section_data
637 {
638  public:
639   Output_data_fixed_space(off_t data_size, uint64_t addralign)
640     : Output_section_data(data_size, addralign)
641   { }
642
643  protected:
644   // Write out the data--the actual data must be written out
645   // elsewhere.
646   void
647   do_write(Output_file*)
648   { }
649 };
650
651 // A place holder for variable sized data written out via some other
652 // mechanism.
653
654 class Output_data_space : public Output_section_data_build
655 {
656  public:
657   explicit Output_data_space(uint64_t addralign)
658     : Output_section_data_build(addralign)
659   { }
660
661   // Set the alignment.
662   void
663   set_space_alignment(uint64_t align)
664   { this->set_addralign(align); }
665
666  protected:
667   // Write out the data--the actual data must be written out
668   // elsewhere.
669   void
670   do_write(Output_file*)
671   { }
672 };
673
674 // A string table which goes into an output section.
675
676 class Output_data_strtab : public Output_section_data
677 {
678  public:
679   Output_data_strtab(Stringpool* strtab)
680     : Output_section_data(1), strtab_(strtab)
681   { }
682
683  protected:
684   // This is called to set the address and file offset.  Here we make
685   // sure that the Stringpool is finalized.
686   void
687   set_final_data_size();
688
689   // Write out the data.
690   void
691   do_write(Output_file*);
692
693   // Write the data to a buffer.
694   void
695   do_write_to_buffer(unsigned char* buffer)
696   { this->strtab_->write_to_buffer(buffer, this->data_size()); }
697
698  private:
699   Stringpool* strtab_;
700 };
701
702 // This POD class is used to represent a single reloc in the output
703 // file.  This could be a private class within Output_data_reloc, but
704 // the templatization is complex enough that I broke it out into a
705 // separate class.  The class is templatized on either elfcpp::SHT_REL
706 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
707 // relocation or an ordinary relocation.
708
709 // A relocation can be against a global symbol, a local symbol, an
710 // output section, or the undefined symbol at index 0.  We represent
711 // the latter by using a NULL global symbol.
712
713 template<int sh_type, bool dynamic, int size, bool big_endian>
714 class Output_reloc;
715
716 template<bool dynamic, int size, bool big_endian>
717 class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
718 {
719  public:
720   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
721
722   // An uninitialized entry.  We need this because we want to put
723   // instances of this class into an STL container.
724   Output_reloc()
725     : local_sym_index_(INVALID_CODE)
726   { }
727
728   // A reloc against a global symbol.
729
730   Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
731                Address address, bool is_relative);
732
733   Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
734                unsigned int shndx, Address address, bool is_relative);
735
736   // A reloc against a local symbol.
737
738   Output_reloc(Sized_relobj<size, big_endian>* relobj,
739                unsigned int local_sym_index, unsigned int type,
740                Output_data* od, Address address, bool is_relative);
741
742   Output_reloc(Sized_relobj<size, big_endian>* relobj,
743                unsigned int local_sym_index, unsigned int type,
744                unsigned int shndx, Address address, bool is_relative);
745
746   // A reloc against the STT_SECTION symbol of an output section.
747
748   Output_reloc(Output_section* os, unsigned int type, Output_data* od,
749                Address address);
750
751   Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
752                unsigned int shndx, Address address);
753
754   // Return TRUE if this is a RELATIVE relocation.
755   bool
756   is_relative() const
757   { return this->is_relative_; }
758
759   // Get the value of the symbol referred to by a Rel relocation.
760
761   Address
762   symbol_value() const;
763
764   // Write the reloc entry to an output view.
765   void
766   write(unsigned char* pov) const;
767
768   // Write the offset and info fields to Write_rel.
769   template<typename Write_rel>
770   void write_rel(Write_rel*) const;
771
772  private:
773   // Return the symbol index.  We can't do a double template
774   // specialization, so we do a secondary template here.
775   unsigned int
776   get_symbol_index() const;
777
778   // Codes for local_sym_index_.
779   enum
780   {
781     // Global symbol.
782     GSYM_CODE = -1U,
783     // Output section.
784     SECTION_CODE = -2U,
785     // Invalid uninitialized entry.
786     INVALID_CODE = -3U
787   };
788
789   union
790   {
791     // For a local symbol, the object.  We will never generate a
792     // relocation against a local symbol in a dynamic object; that
793     // doesn't make sense.  And our callers will always be
794     // templatized, so we use Sized_relobj here.
795     Sized_relobj<size, big_endian>* relobj;
796     // For a global symbol, the symbol.  If this is NULL, it indicates
797     // a relocation against the undefined 0 symbol.
798     Symbol* gsym;
799     // For a relocation against an output section, the output section.
800     Output_section* os;
801   } u1_;
802   union
803   {
804     // If shndx_ is not INVALID CODE, the object which holds the input
805     // section being used to specify the reloc address.
806     Relobj* relobj;
807     // If shndx_ is INVALID_CODE, the output data being used to
808     // specify the reloc address.  This may be NULL if the reloc
809     // address is absolute.
810     Output_data* od;
811   } u2_;
812   // The address offset within the input section or the Output_data.
813   Address address_;
814   // For a local symbol, the local symbol index.  This is GSYM_CODE
815   // for a global symbol, or INVALID_CODE for an uninitialized value.
816   unsigned int local_sym_index_;
817   // The reloc type--a processor specific code.
818   unsigned int type_ : 31;
819   // True if the relocation is a RELATIVE relocation.
820   bool is_relative_ : 1;
821   // If the reloc address is an input section in an object, the
822   // section index.  This is INVALID_CODE if the reloc address is
823   // specified in some other way.
824   unsigned int shndx_;
825 };
826
827 // The SHT_RELA version of Output_reloc<>.  This is just derived from
828 // the SHT_REL version of Output_reloc, but it adds an addend.
829
830 template<bool dynamic, int size, bool big_endian>
831 class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
832 {
833  public:
834   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
835   typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
836
837   // An uninitialized entry.
838   Output_reloc()
839     : rel_()
840   { }
841
842   // A reloc against a global symbol.
843
844   Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
845                Address address, Addend addend, bool is_relative)
846     : rel_(gsym, type, od, address, is_relative), addend_(addend)
847   { }
848
849   Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
850                unsigned int shndx, Address address, Addend addend,
851                bool is_relative)
852     : rel_(gsym, type, relobj, shndx, address, is_relative), addend_(addend)
853   { }
854
855   // A reloc against a local symbol.
856
857   Output_reloc(Sized_relobj<size, big_endian>* relobj,
858                unsigned int local_sym_index, unsigned int type,
859                Output_data* od, Address address,
860                Addend addend, bool is_relative)
861     : rel_(relobj, local_sym_index, type, od, address, is_relative),
862       addend_(addend)
863   { }
864
865   Output_reloc(Sized_relobj<size, big_endian>* relobj,
866                unsigned int local_sym_index, unsigned int type,
867                unsigned int shndx, Address address,
868                Addend addend, bool is_relative)
869     : rel_(relobj, local_sym_index, type, shndx, address, is_relative),
870       addend_(addend)
871   { }
872
873   // A reloc against the STT_SECTION symbol of an output section.
874
875   Output_reloc(Output_section* os, unsigned int type, Output_data* od,
876                Address address, Addend addend)
877     : rel_(os, type, od, address), addend_(addend)
878   { }
879
880   Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
881                unsigned int shndx, Address address, Addend addend)
882     : rel_(os, type, relobj, shndx, address), addend_(addend)
883   { }
884
885   // Write the reloc entry to an output view.
886   void
887   write(unsigned char* pov) const;
888
889  private:
890   // The basic reloc.
891   Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
892   // The addend.
893   Addend addend_;
894 };
895
896 // Output_data_reloc is used to manage a section containing relocs.
897 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA.  DYNAMIC
898 // indicates whether this is a dynamic relocation or a normal
899 // relocation.  Output_data_reloc_base is a base class.
900 // Output_data_reloc is the real class, which we specialize based on
901 // the reloc type.
902
903 template<int sh_type, bool dynamic, int size, bool big_endian>
904 class Output_data_reloc_base : public Output_section_data_build
905 {
906  public:
907   typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
908   typedef typename Output_reloc_type::Address Address;
909   static const int reloc_size =
910     Reloc_types<sh_type, size, big_endian>::reloc_size;
911
912   // Construct the section.
913   Output_data_reloc_base()
914     : Output_section_data_build(Output_data::default_alignment_for_size(size))
915   { }
916
917  protected:
918   // Write out the data.
919   void
920   do_write(Output_file*);
921
922   // Set the entry size and the link.
923   void
924   do_adjust_output_section(Output_section *os);
925
926   // Add a relocation entry.
927   void
928   add(Output_data *od, const Output_reloc_type& reloc)
929   {
930     this->relocs_.push_back(reloc);
931     this->set_current_data_size(this->relocs_.size() * reloc_size);
932     od->add_dynamic_reloc();
933   }
934
935  private:
936   typedef std::vector<Output_reloc_type> Relocs;
937
938   Relocs relocs_;
939 };
940
941 // The class which callers actually create.
942
943 template<int sh_type, bool dynamic, int size, bool big_endian>
944 class Output_data_reloc;
945
946 // The SHT_REL version of Output_data_reloc.
947
948 template<bool dynamic, int size, bool big_endian>
949 class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
950   : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
951 {
952  private: 
953   typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
954                                  big_endian> Base;
955
956  public:
957   typedef typename Base::Output_reloc_type Output_reloc_type;
958   typedef typename Output_reloc_type::Address Address;
959
960   Output_data_reloc()
961     : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>()
962   { }
963
964   // Add a reloc against a global symbol.
965
966   void
967   add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
968   { this->add(od, Output_reloc_type(gsym, type, od, address, false)); }
969
970   void
971   add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
972              unsigned int shndx, Address address)
973   { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
974                                     false)); }
975
976   // Add a RELATIVE reloc against a global symbol.  The final relocation
977   // will not reference the symbol.
978
979   void
980   add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
981                       Address address)
982   { this->add(od, Output_reloc_type(gsym, type, od, address, true)); }
983
984   void
985   add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
986                       Relobj* relobj, unsigned int shndx, Address address)
987   { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
988                                     true)); }
989
990   // Add a reloc against a local symbol.
991
992   void
993   add_local(Sized_relobj<size, big_endian>* relobj,
994             unsigned int local_sym_index, unsigned int type,
995             Output_data* od, Address address)
996   { this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
997                                     address, false)); }
998
999   void
1000   add_local(Sized_relobj<size, big_endian>* relobj,
1001             unsigned int local_sym_index, unsigned int type,
1002             Output_data* od, unsigned int shndx, Address address)
1003   { this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1004                                     address, false)); }
1005
1006   // Add a RELATIVE reloc against a local symbol.
1007
1008   void
1009   add_local_relative(Sized_relobj<size, big_endian>* relobj,
1010                      unsigned int local_sym_index, unsigned int type,
1011                      Output_data* od, Address address)
1012   { this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1013                                     address, true)); }
1014
1015   void
1016   add_local_relative(Sized_relobj<size, big_endian>* relobj,
1017                      unsigned int local_sym_index, unsigned int type,
1018                      Output_data* od, unsigned int shndx, Address address)
1019   { this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1020                                     address, true)); }
1021
1022   // A reloc against the STT_SECTION symbol of an output section.
1023   // OS is the Output_section that the relocation refers to; OD is
1024   // the Output_data object being relocated.
1025
1026   void
1027   add_output_section(Output_section* os, unsigned int type,
1028                      Output_data* od, Address address)
1029   { this->add(od, Output_reloc_type(os, type, od, address)); }
1030
1031   void
1032   add_output_section(Output_section* os, unsigned int type, Output_data* od,
1033                      Relobj* relobj, unsigned int shndx, Address address)
1034   { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); }
1035 };
1036
1037 // The SHT_RELA version of Output_data_reloc.
1038
1039 template<bool dynamic, int size, bool big_endian>
1040 class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1041   : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1042 {
1043  private: 
1044   typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1045                                  big_endian> Base;
1046
1047  public:
1048   typedef typename Base::Output_reloc_type Output_reloc_type;
1049   typedef typename Output_reloc_type::Address Address;
1050   typedef typename Output_reloc_type::Addend Addend;
1051
1052   Output_data_reloc()
1053     : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>()
1054   { }
1055
1056   // Add a reloc against a global symbol.
1057
1058   void
1059   add_global(Symbol* gsym, unsigned int type, Output_data* od,
1060              Address address, Addend addend)
1061   { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1062                                     false)); }
1063
1064   void
1065   add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
1066              unsigned int shndx, Address address,
1067              Addend addend)
1068   { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1069                                     addend, false)); }
1070
1071   // Add a RELATIVE reloc against a global symbol.  The final output
1072   // relocation will not reference the symbol, but we must keep the symbol
1073   // information long enough to set the addend of the relocation correctly
1074   // when it is written.
1075
1076   void
1077   add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1078                       Address address, Addend addend)
1079   { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true)); }
1080
1081   void
1082   add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1083                       Relobj* relobj, unsigned int shndx, Address address,
1084                       Addend addend)
1085   { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1086                                     addend, true)); }
1087
1088   // Add a reloc against a local symbol.
1089
1090   void
1091   add_local(Sized_relobj<size, big_endian>* relobj,
1092             unsigned int local_sym_index, unsigned int type,
1093             Output_data* od, Address address, Addend addend)
1094   {
1095     this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1096                                     addend, false));
1097   }
1098
1099   void
1100   add_local(Sized_relobj<size, big_endian>* relobj,
1101             unsigned int local_sym_index, unsigned int type,
1102             Output_data* od, unsigned int shndx, Address address,
1103             Addend addend)
1104   {
1105     this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1106                                     address, addend, false));
1107   }
1108
1109   // Add a RELATIVE reloc against a local symbol.
1110
1111   void
1112   add_local_relative(Sized_relobj<size, big_endian>* relobj,
1113                      unsigned int local_sym_index, unsigned int type,
1114                      Output_data* od, Address address, Addend addend)
1115   {
1116     this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1117                                     addend, true));
1118   }
1119
1120   void
1121   add_local_relative(Sized_relobj<size, big_endian>* relobj,
1122                      unsigned int local_sym_index, unsigned int type,
1123                      Output_data* od, unsigned int shndx, Address address,
1124                      Addend addend)
1125   {
1126     this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1127                                     address, addend, true));
1128   }
1129
1130   // A reloc against the STT_SECTION symbol of an output section.
1131
1132   void
1133   add_output_section(Output_section* os, unsigned int type, Output_data* od,
1134                      Address address, Addend addend)
1135   { this->add(os, Output_reloc_type(os, type, od, address, addend)); }
1136
1137   void
1138   add_output_section(Output_section* os, unsigned int type, Relobj* relobj,
1139                      unsigned int shndx, Address address, Addend addend)
1140   { this->add(os, Output_reloc_type(os, type, relobj, shndx, address,
1141                                     addend)); }
1142 };
1143
1144 // Output_data_got is used to manage a GOT.  Each entry in the GOT is
1145 // for one symbol--either a global symbol or a local symbol in an
1146 // object.  The target specific code adds entries to the GOT as
1147 // needed.
1148
1149 template<int size, bool big_endian>
1150 class Output_data_got : public Output_section_data_build
1151 {
1152  public:
1153   typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1154   typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> Rel_dyn;
1155   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1156
1157   Output_data_got()
1158     : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1159       entries_()
1160   { }
1161
1162   // Add an entry for a global symbol to the GOT.  Return true if this
1163   // is a new GOT entry, false if the symbol was already in the GOT.
1164   bool
1165   add_global(Symbol* gsym);
1166
1167   // Add an entry for a global symbol to the GOT, and add a dynamic
1168   // relocation of type R_TYPE for the GOT entry.
1169   void
1170   add_global_with_rel(Symbol* gsym, Rel_dyn* rel_dyn, unsigned int r_type);
1171
1172   void
1173   add_global_with_rela(Symbol* gsym, Rela_dyn* rela_dyn, unsigned int r_type);
1174
1175   // Add an entry for a local symbol to the GOT.  This returns true if
1176   // this is a new GOT entry, false if the symbol already has a GOT
1177   // entry.
1178   bool
1179   add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index);
1180
1181   // Add an entry for a global symbol to the GOT, and add a dynamic
1182   // relocation of type R_TYPE for the GOT entry.
1183   void
1184   add_local_with_rel(Sized_relobj<size, big_endian>* object,
1185                      unsigned int sym_index, Rel_dyn* rel_dyn,
1186                      unsigned int r_type);
1187
1188   void
1189   add_local_with_rela(Sized_relobj<size, big_endian>* object,
1190                       unsigned int sym_index, Rela_dyn* rela_dyn,
1191                       unsigned int r_type);
1192
1193   // Add an entry (or pair of entries) for a global TLS symbol to the GOT.
1194   // Return true if this is a new GOT entry, false if the symbol was
1195   // already in the GOT.
1196   bool
1197   add_global_tls(Symbol* gsym, bool need_pair);
1198
1199   // Add an entry for a global TLS symbol to the GOT, and add a dynamic
1200   // relocation of type R_TYPE.
1201   void
1202   add_global_tls_with_rel(Symbol* gsym, Rel_dyn* rel_dyn,
1203                           unsigned int r_type);
1204
1205   void
1206   add_global_tls_with_rela(Symbol* gsym, Rela_dyn* rela_dyn,
1207                            unsigned int r_type);
1208
1209   // Add a pair of entries for a global TLS symbol to the GOT, and add
1210   // dynamic relocations of type MOD_R_TYPE and DTV_R_TYPE, respectively.
1211   void
1212   add_global_tls_with_rel(Symbol* gsym, Rel_dyn* rel_dyn,
1213                           unsigned int mod_r_type,
1214                           unsigned int dtv_r_type);
1215
1216   void
1217   add_global_tls_with_rela(Symbol* gsym, Rela_dyn* rela_dyn,
1218                            unsigned int mod_r_type,
1219                            unsigned int dtv_r_type);
1220
1221   // Add an entry (or pair of entries) for a local TLS symbol to the GOT.
1222   // This returns true if this is a new GOT entry, false if the symbol
1223   // already has a GOT entry.
1224   bool
1225   add_local_tls(Sized_relobj<size, big_endian>* object,
1226                 unsigned int sym_index, bool need_pair);
1227
1228   // Add an entry (or pair of entries) for a local TLS symbol to the GOT,
1229   // and add a dynamic relocation of type R_TYPE for the first GOT entry.
1230   // Because this is a local symbol, the first GOT entry can be relocated
1231   // relative to a section symbol, and the second GOT entry will have an
1232   // dtv-relative value that can be computed at link time.
1233   void
1234   add_local_tls_with_rel(Sized_relobj<size, big_endian>* object,
1235                          unsigned int sym_index, unsigned int shndx,
1236                          bool need_pair, Rel_dyn* rel_dyn,
1237                          unsigned int r_type);
1238
1239   void
1240   add_local_tls_with_rela(Sized_relobj<size, big_endian>* object,
1241                          unsigned int sym_index, unsigned int shndx,
1242                          bool need_pair, Rela_dyn* rela_dyn,
1243                          unsigned int r_type);
1244
1245   // Add a constant to the GOT.  This returns the offset of the new
1246   // entry from the start of the GOT.
1247   unsigned int
1248   add_constant(Valtype constant)
1249   {
1250     this->entries_.push_back(Got_entry(constant));
1251     this->set_got_size();
1252     return this->last_got_offset();
1253   }
1254
1255  protected:
1256   // Write out the GOT table.
1257   void
1258   do_write(Output_file*);
1259
1260  private:
1261   // This POD class holds a single GOT entry.
1262   class Got_entry
1263   {
1264    public:
1265     // Create a zero entry.
1266     Got_entry()
1267       : local_sym_index_(CONSTANT_CODE)
1268     { this->u_.constant = 0; }
1269
1270     // Create a global symbol entry.
1271     explicit Got_entry(Symbol* gsym)
1272       : local_sym_index_(GSYM_CODE)
1273     { this->u_.gsym = gsym; }
1274
1275     // Create a local symbol entry.
1276     Got_entry(Sized_relobj<size, big_endian>* object,
1277               unsigned int local_sym_index)
1278       : local_sym_index_(local_sym_index)
1279     {
1280       gold_assert(local_sym_index != GSYM_CODE
1281                   && local_sym_index != CONSTANT_CODE);
1282       this->u_.object = object;
1283     }
1284
1285     // Create a constant entry.  The constant is a host value--it will
1286     // be swapped, if necessary, when it is written out.
1287     explicit Got_entry(Valtype constant)
1288       : local_sym_index_(CONSTANT_CODE)
1289     { this->u_.constant = constant; }
1290
1291     // Write the GOT entry to an output view.
1292     void
1293     write(unsigned char* pov) const;
1294
1295    private:
1296     enum
1297     {
1298       GSYM_CODE = -1U,
1299       CONSTANT_CODE = -2U
1300     };
1301
1302     union
1303     {
1304       // For a local symbol, the object.
1305       Sized_relobj<size, big_endian>* object;
1306       // For a global symbol, the symbol.
1307       Symbol* gsym;
1308       // For a constant, the constant.
1309       Valtype constant;
1310     } u_;
1311     // For a local symbol, the local symbol index.  This is GSYM_CODE
1312     // for a global symbol, or CONSTANT_CODE for a constant.
1313     unsigned int local_sym_index_;
1314   };
1315
1316   typedef std::vector<Got_entry> Got_entries;
1317
1318   // Return the offset into the GOT of GOT entry I.
1319   unsigned int
1320   got_offset(unsigned int i) const
1321   { return i * (size / 8); }
1322
1323   // Return the offset into the GOT of the last entry added.
1324   unsigned int
1325   last_got_offset() const
1326   { return this->got_offset(this->entries_.size() - 1); }
1327
1328   // Set the size of the section.
1329   void
1330   set_got_size()
1331   { this->set_current_data_size(this->got_offset(this->entries_.size())); }
1332
1333   // The list of GOT entries.
1334   Got_entries entries_;
1335 };
1336
1337 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1338 // section.
1339
1340 class Output_data_dynamic : public Output_section_data
1341 {
1342  public:
1343   Output_data_dynamic(Stringpool* pool)
1344     : Output_section_data(Output_data::default_alignment()),
1345       entries_(), pool_(pool)
1346   { }
1347
1348   // Add a new dynamic entry with a fixed numeric value.
1349   void
1350   add_constant(elfcpp::DT tag, unsigned int val)
1351   { this->add_entry(Dynamic_entry(tag, val)); }
1352
1353   // Add a new dynamic entry with the address of output data.
1354   void
1355   add_section_address(elfcpp::DT tag, const Output_data* od)
1356   { this->add_entry(Dynamic_entry(tag, od, false)); }
1357
1358   // Add a new dynamic entry with the size of output data.
1359   void
1360   add_section_size(elfcpp::DT tag, const Output_data* od)
1361   { this->add_entry(Dynamic_entry(tag, od, true)); }
1362
1363   // Add a new dynamic entry with the address of a symbol.
1364   void
1365   add_symbol(elfcpp::DT tag, const Symbol* sym)
1366   { this->add_entry(Dynamic_entry(tag, sym)); }
1367
1368   // Add a new dynamic entry with a string.
1369   void
1370   add_string(elfcpp::DT tag, const char* str)
1371   { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
1372
1373   void
1374   add_string(elfcpp::DT tag, const std::string& str)
1375   { this->add_string(tag, str.c_str()); }
1376
1377  protected:
1378   // Adjust the output section to set the entry size.
1379   void
1380   do_adjust_output_section(Output_section*);
1381
1382   // Set the final data size.
1383   void
1384   set_final_data_size();
1385
1386   // Write out the dynamic entries.
1387   void
1388   do_write(Output_file*);
1389
1390  private:
1391   // This POD class holds a single dynamic entry.
1392   class Dynamic_entry
1393   {
1394    public:
1395     // Create an entry with a fixed numeric value.
1396     Dynamic_entry(elfcpp::DT tag, unsigned int val)
1397       : tag_(tag), classification_(DYNAMIC_NUMBER)
1398     { this->u_.val = val; }
1399
1400     // Create an entry with the size or address of a section.
1401     Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
1402       : tag_(tag),
1403         classification_(section_size
1404                         ? DYNAMIC_SECTION_SIZE
1405                         : DYNAMIC_SECTION_ADDRESS)
1406     { this->u_.od = od; }
1407
1408     // Create an entry with the address of a symbol.
1409     Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
1410       : tag_(tag), classification_(DYNAMIC_SYMBOL)
1411     { this->u_.sym = sym; }
1412
1413     // Create an entry with a string.
1414     Dynamic_entry(elfcpp::DT tag, const char* str)
1415       : tag_(tag), classification_(DYNAMIC_STRING)
1416     { this->u_.str = str; }
1417
1418     // Write the dynamic entry to an output view.
1419     template<int size, bool big_endian>
1420     void
1421     write(unsigned char* pov, const Stringpool* ACCEPT_SIZE_ENDIAN) const;
1422
1423    private:
1424     enum Classification
1425     {
1426       // Number.
1427       DYNAMIC_NUMBER,
1428       // Section address.
1429       DYNAMIC_SECTION_ADDRESS,
1430       // Section size.
1431       DYNAMIC_SECTION_SIZE,
1432       // Symbol adress.
1433       DYNAMIC_SYMBOL,
1434       // String.
1435       DYNAMIC_STRING
1436     };
1437
1438     union
1439     {
1440       // For DYNAMIC_NUMBER.
1441       unsigned int val;
1442       // For DYNAMIC_SECTION_ADDRESS and DYNAMIC_SECTION_SIZE.
1443       const Output_data* od;
1444       // For DYNAMIC_SYMBOL.
1445       const Symbol* sym;
1446       // For DYNAMIC_STRING.
1447       const char* str;
1448     } u_;
1449     // The dynamic tag.
1450     elfcpp::DT tag_;
1451     // The type of entry.
1452     Classification classification_;
1453   };
1454
1455   // Add an entry to the list.
1456   void
1457   add_entry(const Dynamic_entry& entry)
1458   { this->entries_.push_back(entry); }
1459
1460   // Sized version of write function.
1461   template<int size, bool big_endian>
1462   void
1463   sized_write(Output_file* of);
1464
1465   // The type of the list of entries.
1466   typedef std::vector<Dynamic_entry> Dynamic_entries;
1467
1468   // The entries.
1469   Dynamic_entries entries_;
1470   // The pool used for strings.
1471   Stringpool* pool_;
1472 };
1473
1474 // An output section.  We don't expect to have too many output
1475 // sections, so we don't bother to do a template on the size.
1476
1477 class Output_section : public Output_data
1478 {
1479  public:
1480   // Create an output section, giving the name, type, and flags.
1481   Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
1482   virtual ~Output_section();
1483
1484   // Add a new input section SHNDX, named NAME, with header SHDR, from
1485   // object OBJECT.  RELOC_SHNDX is the index of a relocation section
1486   // which applies to this section, or 0 if none, or -1U if more than
1487   // one.  Return the offset within the output section.
1488   template<int size, bool big_endian>
1489   off_t
1490   add_input_section(Sized_relobj<size, big_endian>* object, unsigned int shndx,
1491                     const char *name,
1492                     const elfcpp::Shdr<size, big_endian>& shdr,
1493                     unsigned int reloc_shndx);
1494
1495   // Add generated data POSD to this output section.
1496   void
1497   add_output_section_data(Output_section_data* posd);
1498
1499   // Return the section name.
1500   const char*
1501   name() const
1502   { return this->name_; }
1503
1504   // Return the section type.
1505   elfcpp::Elf_Word
1506   type() const
1507   { return this->type_; }
1508
1509   // Return the section flags.
1510   elfcpp::Elf_Xword
1511   flags() const
1512   { return this->flags_; }
1513
1514   // Return the entsize field.
1515   uint64_t
1516   entsize() const
1517   { return this->entsize_; }
1518
1519   // Set the entsize field.
1520   void
1521   set_entsize(uint64_t v);
1522
1523   // Set the link field to the output section index of a section.
1524   void
1525   set_link_section(const Output_data* od)
1526   {
1527     gold_assert(this->link_ == 0
1528                 && !this->should_link_to_symtab_
1529                 && !this->should_link_to_dynsym_);
1530     this->link_section_ = od;
1531   }
1532
1533   // Set the link field to a constant.
1534   void
1535   set_link(unsigned int v)
1536   {
1537     gold_assert(this->link_section_ == NULL
1538                 && !this->should_link_to_symtab_
1539                 && !this->should_link_to_dynsym_);
1540     this->link_ = v;
1541   }
1542
1543   // Record that this section should link to the normal symbol table.
1544   void
1545   set_should_link_to_symtab()
1546   {
1547     gold_assert(this->link_section_ == NULL
1548                 && this->link_ == 0
1549                 && !this->should_link_to_dynsym_);
1550     this->should_link_to_symtab_ = true;
1551   }
1552
1553   // Record that this section should link to the dynamic symbol table.
1554   void
1555   set_should_link_to_dynsym()
1556   {
1557     gold_assert(this->link_section_ == NULL
1558                 && this->link_ == 0
1559                 && !this->should_link_to_symtab_);
1560     this->should_link_to_dynsym_ = true;
1561   }
1562
1563   // Return the info field.
1564   unsigned int
1565   info() const
1566   {
1567     gold_assert(this->info_section_ == NULL);
1568     return this->info_;
1569   }
1570
1571   // Set the info field to the output section index of a section.
1572   void
1573   set_info_section(const Output_data* od)
1574   {
1575     gold_assert(this->info_ == 0);
1576     this->info_section_ = od;
1577   }
1578
1579   // Set the info field to a constant.
1580   void
1581   set_info(unsigned int v)
1582   {
1583     gold_assert(this->info_section_ == NULL);
1584     this->info_ = v;
1585   }
1586
1587   // Set the addralign field.
1588   void
1589   set_addralign(uint64_t v)
1590   { this->addralign_ = v; }
1591
1592   // Indicate that we need a symtab index.
1593   void
1594   set_needs_symtab_index()
1595   { this->needs_symtab_index_ = true; }
1596
1597   // Return whether we need a symtab index.
1598   bool
1599   needs_symtab_index() const
1600   { return this->needs_symtab_index_; }
1601
1602   // Get the symtab index.
1603   unsigned int
1604   symtab_index() const
1605   {
1606     gold_assert(this->symtab_index_ != 0);
1607     return this->symtab_index_;
1608   }
1609
1610   // Set the symtab index.
1611   void
1612   set_symtab_index(unsigned int index)
1613   {
1614     gold_assert(index != 0);
1615     this->symtab_index_ = index;
1616   }
1617
1618   // Indicate that we need a dynsym index.
1619   void
1620   set_needs_dynsym_index()
1621   { this->needs_dynsym_index_ = true; }
1622
1623   // Return whether we need a dynsym index.
1624   bool
1625   needs_dynsym_index() const
1626   { return this->needs_dynsym_index_; }
1627
1628   // Get the dynsym index.
1629   unsigned int
1630   dynsym_index() const
1631   {
1632     gold_assert(this->dynsym_index_ != 0);
1633     return this->dynsym_index_;
1634   }
1635
1636   // Set the dynsym index.
1637   void
1638   set_dynsym_index(unsigned int index)
1639   {
1640     gold_assert(index != 0);
1641     this->dynsym_index_ = index;
1642   }
1643
1644   // Return whether this section should be written after all the input
1645   // sections are complete.
1646   bool
1647   after_input_sections() const
1648   { return this->after_input_sections_; }
1649
1650   // Record that this section should be written after all the input
1651   // sections are complete.
1652   void
1653   set_after_input_sections()
1654   { this->after_input_sections_ = true; }
1655
1656   // Return whether this section requires postprocessing after all
1657   // relocations have been applied.
1658   bool
1659   requires_postprocessing() const
1660   { return this->requires_postprocessing_; }
1661
1662   // If a section requires postprocessing, return the buffer to use.
1663   unsigned char*
1664   postprocessing_buffer() const
1665   {
1666     gold_assert(this->postprocessing_buffer_ != NULL);
1667     return this->postprocessing_buffer_;
1668   }
1669
1670   // If a section requires postprocessing, create the buffer to use.
1671   void
1672   create_postprocessing_buffer();
1673
1674   // If a section requires postprocessing, this is the size of the
1675   // buffer to which relocations should be applied.
1676   off_t
1677   postprocessing_buffer_size() const
1678   { return this->current_data_size_for_child(); }
1679
1680   // Return whether the offset OFFSET in the input section SHNDX in
1681   // object OBJECT is being included in the link.
1682   bool
1683   is_input_address_mapped(const Relobj* object, unsigned int shndx,
1684                           off_t offset) const;
1685
1686   // Return the offset within the output section of OFFSET relative to
1687   // the start of input section SHNDX in object OBJECT.
1688   section_offset_type
1689   output_offset(const Relobj* object, unsigned int shndx,
1690                 section_offset_type offset) const;
1691
1692   // Return the output virtual address of OFFSET relative to the start
1693   // of input section SHNDX in object OBJECT.
1694   uint64_t
1695   output_address(const Relobj* object, unsigned int shndx,
1696                  off_t offset) const;
1697
1698   // Return the output address of the start of the merged section for
1699   // input section SHNDX in object OBJECT.  This is not necessarily
1700   // the offset corresponding to input offset 0 in the section, since
1701   // the section may be mapped arbitrarily.
1702   uint64_t
1703   starting_output_address(const Relobj* object, unsigned int shndx) const;
1704
1705   // Write the section header into *OPHDR.
1706   template<int size, bool big_endian>
1707   void
1708   write_header(const Layout*, const Stringpool*,
1709                elfcpp::Shdr_write<size, big_endian>*) const;
1710
1711   // Print merge statistics to stderr.
1712   void
1713   print_merge_stats();
1714
1715  protected:
1716   // Return the section index in the output file.
1717   unsigned int
1718   do_out_shndx() const
1719   {
1720     gold_assert(this->out_shndx_ != -1U);
1721     return this->out_shndx_;
1722   }
1723
1724   // Set the output section index.
1725   void
1726   do_set_out_shndx(unsigned int shndx)
1727   {
1728     gold_assert(this->out_shndx_ == -1U);
1729     this->out_shndx_ = shndx;
1730   }
1731
1732   // Set the final data size of the Output_section.  For a typical
1733   // Output_section, there is nothing to do, but if there are any
1734   // Output_section_data objects we need to set their final addresses
1735   // here.
1736   virtual void
1737   set_final_data_size();
1738
1739   // Write the data to the file.  For a typical Output_section, this
1740   // does nothing: the data is written out by calling Object::Relocate
1741   // on each input object.  But if there are any Output_section_data
1742   // objects we do need to write them out here.
1743   virtual void
1744   do_write(Output_file*);
1745
1746   // Return the address alignment--function required by parent class.
1747   uint64_t
1748   do_addralign() const
1749   { return this->addralign_; }
1750
1751   // Return whether this is an Output_section.
1752   bool
1753   do_is_section() const
1754   { return true; }
1755
1756   // Return whether this is a section of the specified type.
1757   bool
1758   do_is_section_type(elfcpp::Elf_Word type) const
1759   { return this->type_ == type; }
1760
1761   // Return whether the specified section flag is set.
1762   bool
1763   do_is_section_flag_set(elfcpp::Elf_Xword flag) const
1764   { return (this->flags_ & flag) != 0; }
1765
1766   // Set the TLS offset.  Called only for SHT_TLS sections.
1767   void
1768   do_set_tls_offset(uint64_t tls_base);
1769
1770   // Return the TLS offset, relative to the base of the TLS segment.
1771   // Valid only for SHT_TLS sections.
1772   uint64_t
1773   do_tls_offset() const
1774   { return this->tls_offset_; }
1775
1776   // Modify the section name.  This is only permitted for an
1777   // unallocated section, and only before the size has been finalized.
1778   // Otherwise the name will not get into Layout::namepool_.
1779   void
1780   set_name(const char* newname)
1781   {
1782     gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
1783     gold_assert(!this->is_data_size_valid());
1784     this->name_ = newname;
1785   }
1786
1787   // This may be implemented by a child class.
1788   virtual void
1789   do_finalize_name(Layout*)
1790   { }
1791
1792   // Record that this section requires postprocessing after all
1793   // relocations have been applied.  This is called by a child class.
1794   void
1795   set_requires_postprocessing()
1796   {
1797     this->requires_postprocessing_ = true;
1798     this->after_input_sections_ = true;
1799   }
1800
1801   // Write all the data of an Output_section into the postprocessing
1802   // buffer.
1803   void
1804   write_to_postprocessing_buffer();
1805
1806  private:
1807   // In some cases we need to keep a list of the input sections
1808   // associated with this output section.  We only need the list if we
1809   // might have to change the offsets of the input section within the
1810   // output section after we add the input section.  The ordinary
1811   // input sections will be written out when we process the object
1812   // file, and as such we don't need to track them here.  We do need
1813   // to track Output_section_data objects here.  We store instances of
1814   // this structure in a std::vector, so it must be a POD.  There can
1815   // be many instances of this structure, so we use a union to save
1816   // some space.
1817   class Input_section
1818   {
1819    public:
1820     Input_section()
1821       : shndx_(0), p2align_(0)
1822     {
1823       this->u1_.data_size = 0;
1824       this->u2_.object = NULL;
1825     }
1826
1827     // For an ordinary input section.
1828     Input_section(Relobj* object, unsigned int shndx, off_t data_size,
1829                   uint64_t addralign)
1830       : shndx_(shndx),
1831         p2align_(ffsll(static_cast<long long>(addralign)))
1832     {
1833       gold_assert(shndx != OUTPUT_SECTION_CODE
1834                   && shndx != MERGE_DATA_SECTION_CODE
1835                   && shndx != MERGE_STRING_SECTION_CODE);
1836       this->u1_.data_size = data_size;
1837       this->u2_.object = object;
1838     }
1839
1840     // For a non-merge output section.
1841     Input_section(Output_section_data* posd)
1842       : shndx_(OUTPUT_SECTION_CODE),
1843         p2align_(ffsll(static_cast<long long>(posd->addralign())))
1844     {
1845       this->u1_.data_size = 0;
1846       this->u2_.posd = posd;
1847     }
1848
1849     // For a merge section.
1850     Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
1851       : shndx_(is_string
1852                ? MERGE_STRING_SECTION_CODE
1853                : MERGE_DATA_SECTION_CODE),
1854         p2align_(ffsll(static_cast<long long>(posd->addralign())))
1855     {
1856       this->u1_.entsize = entsize;
1857       this->u2_.posd = posd;
1858     }
1859
1860     // The required alignment.
1861     uint64_t
1862     addralign() const
1863     {
1864       return (this->p2align_ == 0
1865               ? 0
1866               : static_cast<uint64_t>(1) << (this->p2align_ - 1));
1867     }
1868
1869     // Return the required size.
1870     off_t
1871     data_size() const;
1872
1873     // Return whether this is a merge section which matches the
1874     // parameters.
1875     bool
1876     is_merge_section(bool is_string, uint64_t entsize,
1877                      uint64_t addralign) const
1878     {
1879       return (this->shndx_ == (is_string
1880                                ? MERGE_STRING_SECTION_CODE
1881                                : MERGE_DATA_SECTION_CODE)
1882               && this->u1_.entsize == entsize
1883               && this->addralign() == addralign);
1884     }
1885
1886     // Set the output section.
1887     void
1888     set_output_section(Output_section* os)
1889     {
1890       gold_assert(!this->is_input_section());
1891       this->u2_.posd->set_output_section(os);
1892     }
1893
1894     // Set the address and file offset.  This is called during
1895     // Layout::finalize.  SECTION_FILE_OFFSET is the file offset of
1896     // the enclosing section.
1897     void
1898     set_address_and_file_offset(uint64_t address, off_t file_offset,
1899                                 off_t section_file_offset);
1900
1901     // Finalize the data size.
1902     void
1903     finalize_data_size();
1904
1905     // Add an input section, for SHF_MERGE sections.
1906     bool
1907     add_input_section(Relobj* object, unsigned int shndx)
1908     {
1909       gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
1910                   || this->shndx_ == MERGE_STRING_SECTION_CODE);
1911       return this->u2_.posd->add_input_section(object, shndx);
1912     }
1913
1914     // Given an input OBJECT, an input section index SHNDX within that
1915     // object, and an OFFSET relative to the start of that input
1916     // section, return whether or not the output offset is known.  If
1917     // this function returns true, it sets *POUTPUT to the offset in
1918     // the output section, relative to the start of the input section
1919     // in the output section.  *POUTPUT may be different from OFFSET
1920     // for a merged section.
1921     bool
1922     output_offset(const Relobj* object, unsigned int shndx,
1923                   section_offset_type offset,
1924                   section_offset_type *poutput) const;
1925
1926     // Return whether this is the merge section for the input section
1927     // SHNDX in OBJECT.
1928     bool
1929     is_merge_section_for(const Relobj* object, unsigned int shndx) const;
1930
1931     // Write out the data.  This does nothing for an input section.
1932     void
1933     write(Output_file*);
1934
1935     // Write the data to a buffer.  This does nothing for an input
1936     // section.
1937     void
1938     write_to_buffer(unsigned char*);
1939
1940     // Print statistics about merge sections to stderr.
1941     void
1942     print_merge_stats(const char* section_name)
1943     {
1944       if (this->shndx_ == MERGE_DATA_SECTION_CODE
1945           || this->shndx_ == MERGE_STRING_SECTION_CODE)
1946         this->u2_.posd->print_merge_stats(section_name);
1947     }
1948
1949    private:
1950     // Code values which appear in shndx_.  If the value is not one of
1951     // these codes, it is the input section index in the object file.
1952     enum
1953     {
1954       // An Output_section_data.
1955       OUTPUT_SECTION_CODE = -1U,
1956       // An Output_section_data for an SHF_MERGE section with
1957       // SHF_STRINGS not set.
1958       MERGE_DATA_SECTION_CODE = -2U,
1959       // An Output_section_data for an SHF_MERGE section with
1960       // SHF_STRINGS set.
1961       MERGE_STRING_SECTION_CODE = -3U
1962     };
1963
1964     // Whether this is an input section.
1965     bool
1966     is_input_section() const
1967     {
1968       return (this->shndx_ != OUTPUT_SECTION_CODE
1969               && this->shndx_ != MERGE_DATA_SECTION_CODE
1970               && this->shndx_ != MERGE_STRING_SECTION_CODE);
1971     }
1972
1973     // For an ordinary input section, this is the section index in the
1974     // input file.  For an Output_section_data, this is
1975     // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
1976     // MERGE_STRING_SECTION_CODE.
1977     unsigned int shndx_;
1978     // The required alignment, stored as a power of 2.
1979     unsigned int p2align_;
1980     union
1981     {
1982       // For an ordinary input section, the section size.
1983       off_t data_size;
1984       // For OUTPUT_SECTION_CODE, this is not used.  For
1985       // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
1986       // entity size.
1987       uint64_t entsize;
1988     } u1_;
1989     union
1990     {
1991       // For an ordinary input section, the object which holds the
1992       // input section.
1993       Relobj* object;
1994       // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
1995       // MERGE_STRING_SECTION_CODE, the data.
1996       Output_section_data* posd;
1997     } u2_;
1998   };
1999
2000   typedef std::vector<Input_section> Input_section_list;
2001
2002   // Fill data.  This is used to fill in data between input sections.
2003   // When we have to keep track of the input sections, we can use an
2004   // Output_data_const, but we don't want to have to keep track of
2005   // input sections just to implement fills.  For a fill we record the
2006   // offset, and the actual data to be written out.
2007   class Fill
2008   {
2009    public:
2010     Fill(off_t section_offset, off_t length)
2011       : section_offset_(section_offset), length_(length)
2012     { }
2013
2014     // Return section offset.
2015     off_t
2016     section_offset() const
2017     { return this->section_offset_; }
2018
2019     // Return fill length.
2020     off_t
2021     length() const
2022     { return this->length_; }
2023
2024    private:
2025     // The offset within the output section.
2026     off_t section_offset_;
2027     // The length of the space to fill.
2028     off_t length_;
2029   };
2030
2031   typedef std::vector<Fill> Fill_list;
2032
2033   // Add a new output section by Input_section.
2034   void
2035   add_output_section_data(Input_section*);
2036
2037   // Add an SHF_MERGE input section.  Returns true if the section was
2038   // handled.
2039   bool
2040   add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
2041                           uint64_t entsize, uint64_t addralign);
2042
2043   // Add an output SHF_MERGE section POSD to this output section.
2044   // IS_STRING indicates whether it is a SHF_STRINGS section, and
2045   // ENTSIZE is the entity size.  This returns the entry added to
2046   // input_sections_.
2047   void
2048   add_output_merge_section(Output_section_data* posd, bool is_string,
2049                            uint64_t entsize);
2050
2051   // Most of these fields are only valid after layout.
2052
2053   // The name of the section.  This will point into a Stringpool.
2054   const char* name_;
2055   // The section address is in the parent class.
2056   // The section alignment.
2057   uint64_t addralign_;
2058   // The section entry size.
2059   uint64_t entsize_;
2060   // The file offset is in the parent class.
2061   // Set the section link field to the index of this section.
2062   const Output_data* link_section_;
2063   // If link_section_ is NULL, this is the link field.
2064   unsigned int link_;
2065   // Set the section info field to the index of this section.
2066   const Output_data* info_section_;
2067   // If info_section_ is NULL, this is the section info field.
2068   unsigned int info_;
2069   // The section type.
2070   const elfcpp::Elf_Word type_;
2071   // The section flags.
2072   const elfcpp::Elf_Xword flags_;
2073   // The section index.
2074   unsigned int out_shndx_;
2075   // If there is a STT_SECTION for this output section in the normal
2076   // symbol table, this is the symbol index.  This starts out as zero.
2077   // It is initialized in Layout::finalize() to be the index, or -1U
2078   // if there isn't one.
2079   unsigned int symtab_index_;
2080   // If there is a STT_SECTION for this output section in the dynamic
2081   // symbol table, this is the symbol index.  This starts out as zero.
2082   // It is initialized in Layout::finalize() to be the index, or -1U
2083   // if there isn't one.
2084   unsigned int dynsym_index_;
2085   // The input sections.  This will be empty in cases where we don't
2086   // need to keep track of them.
2087   Input_section_list input_sections_;
2088   // The offset of the first entry in input_sections_.
2089   off_t first_input_offset_;
2090   // The fill data.  This is separate from input_sections_ because we
2091   // often will need fill sections without needing to keep track of
2092   // input sections.
2093   Fill_list fills_;
2094   // If the section requires postprocessing, this buffer holds the
2095   // section contents during relocation.
2096   unsigned char* postprocessing_buffer_;
2097   // Whether this output section needs a STT_SECTION symbol in the
2098   // normal symbol table.  This will be true if there is a relocation
2099   // which needs it.
2100   bool needs_symtab_index_ : 1;
2101   // Whether this output section needs a STT_SECTION symbol in the
2102   // dynamic symbol table.  This will be true if there is a dynamic
2103   // relocation which needs it.
2104   bool needs_dynsym_index_ : 1;
2105   // Whether the link field of this output section should point to the
2106   // normal symbol table.
2107   bool should_link_to_symtab_ : 1;
2108   // Whether the link field of this output section should point to the
2109   // dynamic symbol table.
2110   bool should_link_to_dynsym_ : 1;
2111   // Whether this section should be written after all the input
2112   // sections are complete.
2113   bool after_input_sections_ : 1;
2114   // Whether this section requires post processing after all
2115   // relocations have been applied.
2116   bool requires_postprocessing_ : 1;
2117   // For SHT_TLS sections, the offset of this section relative to the base
2118   // of the TLS segment.
2119   uint64_t tls_offset_;
2120 };
2121
2122 // An output segment.  PT_LOAD segments are built from collections of
2123 // output sections.  Other segments typically point within PT_LOAD
2124 // segments, and are built directly as needed.
2125
2126 class Output_segment
2127 {
2128  public:
2129   // Create an output segment, specifying the type and flags.
2130   Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
2131
2132   // Return the virtual address.
2133   uint64_t
2134   vaddr() const
2135   { return this->vaddr_; }
2136
2137   // Return the physical address.
2138   uint64_t
2139   paddr() const
2140   { return this->paddr_; }
2141
2142   // Return the segment type.
2143   elfcpp::Elf_Word
2144   type() const
2145   { return this->type_; }
2146
2147   // Return the segment flags.
2148   elfcpp::Elf_Word
2149   flags() const
2150   { return this->flags_; }
2151
2152   // Return the memory size.
2153   uint64_t
2154   memsz() const
2155   { return this->memsz_; }
2156
2157   // Return the file size.
2158   off_t
2159   filesz() const
2160   { return this->filesz_; }
2161
2162   // Return the maximum alignment of the Output_data.
2163   uint64_t
2164   addralign();
2165
2166   // Add an Output_section to this segment.
2167   void
2168   add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
2169   { this->add_output_section(os, seg_flags, false); }
2170
2171   // Add an Output_section to the start of this segment.
2172   void
2173   add_initial_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
2174   { this->add_output_section(os, seg_flags, true); }
2175
2176   // Add an Output_data (which is not an Output_section) to the start
2177   // of this segment.
2178   void
2179   add_initial_output_data(Output_data*);
2180
2181   // Return the number of dynamic relocations applied to this segment.
2182   unsigned int
2183   dynamic_reloc_count() const;
2184
2185   // Set the address of the segment to ADDR and the offset to *POFF
2186   // (aligned if necessary), and set the addresses and offsets of all
2187   // contained output sections accordingly.  Set the section indexes
2188   // of all contained output sections starting with *PSHNDX.  Return
2189   // the address of the immediately following segment.  Update *POFF
2190   // and *PSHNDX.  This should only be called for a PT_LOAD segment.
2191   uint64_t
2192   set_section_addresses(uint64_t addr, off_t* poff, unsigned int* pshndx);
2193
2194   // Set the minimum alignment of this segment.  This may be adjusted
2195   // upward based on the section alignments.
2196   void
2197   set_minimum_addralign(uint64_t align)
2198   {
2199     gold_assert(!this->is_align_known_);
2200     this->align_ = align;
2201   }
2202
2203   // Set the offset of this segment based on the section.  This should
2204   // only be called for a non-PT_LOAD segment.
2205   void
2206   set_offset();
2207
2208   // Set the TLS offsets of the sections contained in the PT_TLS segment.
2209   void
2210   set_tls_offsets();
2211
2212   // Return the number of output sections.
2213   unsigned int
2214   output_section_count() const;
2215
2216   // Write the segment header into *OPHDR.
2217   template<int size, bool big_endian>
2218   void
2219   write_header(elfcpp::Phdr_write<size, big_endian>*);
2220
2221   // Write the section headers of associated sections into V.
2222   template<int size, bool big_endian>
2223   unsigned char*
2224   write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
2225                         unsigned int* pshndx ACCEPT_SIZE_ENDIAN) const;
2226
2227  private:
2228   Output_segment(const Output_segment&);
2229   Output_segment& operator=(const Output_segment&);
2230
2231   typedef std::list<Output_data*> Output_data_list;
2232
2233   // Add an Output_section to this segment, specifying front or back.
2234   void
2235   add_output_section(Output_section*, elfcpp::Elf_Word seg_flags,
2236                      bool front);
2237
2238   // Find the maximum alignment in an Output_data_list.
2239   static uint64_t
2240   maximum_alignment(const Output_data_list*);
2241
2242   // Set the section addresses in an Output_data_list.
2243   uint64_t
2244   set_section_list_addresses(Output_data_list*, uint64_t addr, off_t* poff,
2245                              unsigned int* pshndx);
2246
2247   // Return the number of Output_sections in an Output_data_list.
2248   unsigned int
2249   output_section_count_list(const Output_data_list*) const;
2250
2251   // Return the number of dynamic relocs in an Output_data_list.
2252   unsigned int
2253   dynamic_reloc_count_list(const Output_data_list*) const;
2254
2255   // Write the section headers in the list into V.
2256   template<int size, bool big_endian>
2257   unsigned char*
2258   write_section_headers_list(const Layout*, const Stringpool*,
2259                              const Output_data_list*, unsigned char* v,
2260                              unsigned int* pshdx ACCEPT_SIZE_ENDIAN) const;
2261
2262   // The list of output data with contents attached to this segment.
2263   Output_data_list output_data_;
2264   // The list of output data without contents attached to this segment.
2265   Output_data_list output_bss_;
2266   // The segment virtual address.
2267   uint64_t vaddr_;
2268   // The segment physical address.
2269   uint64_t paddr_;
2270   // The size of the segment in memory.
2271   uint64_t memsz_;
2272   // The segment alignment.  The is_align_known_ field indicates
2273   // whether this has been finalized.  It can be set to a minimum
2274   // value before it is finalized.
2275   uint64_t align_;
2276   // The offset of the segment data within the file.
2277   off_t offset_;
2278   // The size of the segment data in the file.
2279   off_t filesz_;
2280   // The segment type;
2281   elfcpp::Elf_Word type_;
2282   // The segment flags.
2283   elfcpp::Elf_Word flags_;
2284   // Whether we have finalized align_.
2285   bool is_align_known_;
2286 };
2287
2288 // This class represents the output file.
2289
2290 class Output_file
2291 {
2292  public:
2293   Output_file(const General_options& options, Target*);
2294
2295   // Get a pointer to the target.
2296   Target*
2297   target() const
2298   { return this->target_; }
2299
2300   // Open the output file.  FILE_SIZE is the final size of the file.
2301   void
2302   open(off_t file_size);
2303
2304   // Resize the output file.
2305   void
2306   resize(off_t file_size);
2307
2308   // Close the output file (flushing all buffered data) and make sure
2309   // there are no errors.
2310   void
2311   close();
2312
2313   // We currently always use mmap which makes the view handling quite
2314   // simple.  In the future we may support other approaches.
2315
2316   // Write data to the output file.
2317   void
2318   write(off_t offset, const void* data, size_t len)
2319   { memcpy(this->base_ + offset, data, len); }
2320
2321   // Get a buffer to use to write to the file, given the offset into
2322   // the file and the size.
2323   unsigned char*
2324   get_output_view(off_t start, size_t size)
2325   {
2326     gold_assert(start >= 0
2327                 && start + static_cast<off_t>(size) <= this->file_size_);
2328     return this->base_ + start;
2329   }
2330
2331   // VIEW must have been returned by get_output_view.  Write the
2332   // buffer to the file, passing in the offset and the size.
2333   void
2334   write_output_view(off_t, size_t, unsigned char*)
2335   { }
2336
2337   // Get a read/write buffer.  This is used when we want to write part
2338   // of the file, read it in, and write it again.
2339   unsigned char*
2340   get_input_output_view(off_t start, size_t size)
2341   { return this->get_output_view(start, size); }
2342
2343   // Write a read/write buffer back to the file.
2344   void
2345   write_input_output_view(off_t, size_t, unsigned char*)
2346   { }
2347
2348   // Get a read buffer.  This is used when we just want to read part
2349   // of the file back it in.
2350   const unsigned char*
2351   get_input_view(off_t start, size_t size)
2352   { return this->get_output_view(start, size); }
2353
2354   // Release a read bfufer.
2355   void
2356   free_input_view(off_t, size_t, const unsigned char*)
2357   { }
2358
2359  private:
2360   // Map the file into memory and return a pointer to the map.
2361   void
2362   map();
2363
2364   // Unmap the file from memory (and flush to disk buffers).
2365   void
2366   unmap();
2367
2368
2369   // General options.
2370   const General_options& options_;
2371   // Target.
2372   Target* target_;
2373   // File name.
2374   const char* name_;
2375   // File descriptor.
2376   int o_;
2377   // File size.
2378   off_t file_size_;
2379   // Base of file mapped into memory.
2380   unsigned char* base_;
2381   // True iff base_ points to a memory buffer rather than an output file.
2382   bool map_is_anonymous_;
2383 };
2384
2385 } // End namespace gold.
2386
2387 #endif // !defined(GOLD_OUTPUT_H)