1 // object.h -- support for an object file for linking in gold  -*- C++ -*-
2 
3 // Copyright (C) 2006-2020 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_OBJECT_H
24 #define GOLD_OBJECT_H
25 
26 #include <string>
27 #include <vector>
28 
29 #include "elfcpp.h"
30 #include "elfcpp_file.h"
31 #include "fileread.h"
32 #include "target.h"
33 #include "archive.h"
34 
35 namespace gold
36 {
37 
38 class General_options;
39 class Task;
40 class Cref;
41 class Layout;
42 class Kept_section;
43 class Output_data;
44 class Output_section;
45 class Output_section_data;
46 class Output_file;
47 class Output_symtab_xindex;
48 class Pluginobj;
49 class Dynobj;
50 class Object_merge_map;
51 class Relocatable_relocs;
52 struct Symbols_data;
53 
54 template<typename Stringpool_char>
55 class Stringpool_template;
56 
57 // Data to pass from read_symbols() to add_symbols().
58 
59 struct Read_symbols_data
60 {
61   Read_symbols_data()
62     : section_headers(NULL), section_names(NULL), symbols(NULL),
63       symbol_names(NULL), versym(NULL), verdef(NULL), verneed(NULL)
64   { }
65 
66   ~Read_symbols_data();
67 
68   // Section headers.
69   File_view* section_headers;
70   // Section names.
71   File_view* section_names;
72   // Size of section name data in bytes.
73   section_size_type section_names_size;
74   // Symbol data.
75   File_view* symbols;
76   // Size of symbol data in bytes.
77   section_size_type symbols_size;
78   // Offset of external symbols within symbol data.  This structure
79   // sometimes contains only external symbols, in which case this will
80   // be zero.  Sometimes it contains all symbols.
81   section_offset_type external_symbols_offset;
82   // Symbol names.
83   File_view* symbol_names;
84   // Size of symbol name data in bytes.
85   section_size_type symbol_names_size;
86 
87   // Version information.  This is only used on dynamic objects.
88   // Version symbol data (from SHT_GNU_versym section).
89   File_view* versym;
90   section_size_type versym_size;
91   // Version definition data (from SHT_GNU_verdef section).
92   File_view* verdef;
93   section_size_type verdef_size;
94   unsigned int verdef_info;
95   // Needed version data  (from SHT_GNU_verneed section).
96   File_view* verneed;
97   section_size_type verneed_size;
98   unsigned int verneed_info;
99 };
100 
101 // Information used to print error messages.
102 
103 struct Symbol_location_info
104 {
105   std::string source_file;
106   std::string enclosing_symbol_name;
107   elfcpp::STT enclosing_symbol_type;
108 };
109 
110 // Data about a single relocation section.  This is read in
111 // read_relocs and processed in scan_relocs.
112 
113 struct Section_relocs
114 {
115   Section_relocs()
116     : contents(NULL)
117   { }
118 
119   ~Section_relocs()
120   { delete this->contents; }
121 
122   // Index of reloc section.
123   unsigned int reloc_shndx;
124   // Index of section that relocs apply to.
125   unsigned int data_shndx;
126   // Contents of reloc section.
127   File_view* contents;
128   // Reloc section type.
129   unsigned int sh_type;
130   // Number of reloc entries.
131   size_t reloc_count;
132   // Output section.
133   Output_section* output_section;
134   // Whether this section has special handling for offsets.
135   bool needs_special_offset_handling;
136   // Whether the data section is allocated (has the SHF_ALLOC flag set).
137   bool is_data_section_allocated;
138 };
139 
140 // Relocations in an object file.  This is read in read_relocs and
141 // processed in scan_relocs.
142 
143 struct Read_relocs_data
144 {
145   Read_relocs_data()
146     : local_symbols(NULL)
147   { }
148 
149   ~Read_relocs_data()
150   { delete this->local_symbols; }
151 
152   typedef std::vector<Section_relocs> Relocs_list;
153   // The relocations.
154   Relocs_list relocs;
155   // The local symbols.
156   File_view* local_symbols;
157 };
158 
159 // The Xindex class manages section indexes for objects with more than
160 // 0xff00 sections.
161 
162 class Xindex
163 {
164  public:
165   Xindex(int large_shndx_offset)
166     : large_shndx_offset_(large_shndx_offset), symtab_xindex_()
167   { }
168 
169   // Initialize the symtab_xindex_ array, given the object and the
170   // section index of the symbol table to use.
171   template<int size, bool big_endian>
172   void
173   initialize_symtab_xindex(Object*, unsigned int symtab_shndx);
174 
175   // Read in the symtab_xindex_ array, given its section index.
176   // PSHDRS may optionally point to the section headers.
177   template<int size, bool big_endian>
178   void
179   read_symtab_xindex(Object*, unsigned int xindex_shndx,
180 		     const unsigned char* pshdrs);
181 
182   // Symbol SYMNDX in OBJECT has a section of SHN_XINDEX; return the
183   // real section index.
184   unsigned int
185   sym_xindex_to_shndx(Object* object, unsigned int symndx);
186 
187  private:
188   // The type of the array giving the real section index for symbols
189   // whose st_shndx field holds SHN_XINDEX.
190   typedef std::vector<unsigned int> Symtab_xindex;
191 
192   // Adjust a section index if necessary.  This should only be called
193   // for ordinary section indexes.
194   unsigned int
195   adjust_shndx(unsigned int shndx)
196   {
197     if (shndx >= elfcpp::SHN_LORESERVE)
198       shndx += this->large_shndx_offset_;
199     return shndx;
200   }
201 
202   // Adjust to apply to large section indexes.
203   int large_shndx_offset_;
204   // The data from the SHT_SYMTAB_SHNDX section.
205   Symtab_xindex symtab_xindex_;
206 };
207 
208 // A GOT offset list.  A symbol may have more than one GOT offset
209 // (e.g., when mixing modules compiled with two different TLS models),
210 // but will usually have at most one.  GOT_TYPE identifies the type of
211 // GOT entry; its values are specific to each target.
212 
213 class Got_offset_list
214 {
215  public:
216   Got_offset_list()
217     : got_type_(-1U), got_offset_(0), got_next_(NULL)
218   { }
219 
220   Got_offset_list(unsigned int got_type, unsigned int got_offset)
221     : got_type_(got_type), got_offset_(got_offset), got_next_(NULL)
222   { }
223 
224   ~Got_offset_list()
225   {
226     if (this->got_next_ != NULL)
227       {
228         delete this->got_next_;
229         this->got_next_ = NULL;
230       }
231   }
232 
233   // Initialize the fields to their default values.
234   void
235   init()
236   {
237     this->got_type_ = -1U;
238     this->got_offset_ = 0;
239     this->got_next_ = NULL;
240   }
241 
242   // Set the offset for the GOT entry of type GOT_TYPE.
243   void
244   set_offset(unsigned int got_type, unsigned int got_offset)
245   {
246     if (this->got_type_ == -1U)
247       {
248         this->got_type_ = got_type;
249         this->got_offset_ = got_offset;
250       }
251     else
252       {
253         for (Got_offset_list* g = this; g != NULL; g = g->got_next_)
254           {
255             if (g->got_type_ == got_type)
256               {
257                 g->got_offset_ = got_offset;
258                 return;
259               }
260           }
261         Got_offset_list* g = new Got_offset_list(got_type, got_offset);
262         g->got_next_ = this->got_next_;
263         this->got_next_ = g;
264       }
265   }
266 
267   // Return the offset for a GOT entry of type GOT_TYPE.
268   unsigned int
269   get_offset(unsigned int got_type) const
270   {
271     for (const Got_offset_list* g = this; g != NULL; g = g->got_next_)
272       {
273         if (g->got_type_ == got_type)
274           return g->got_offset_;
275       }
276     return -1U;
277   }
278 
279   // Return a pointer to the list, or NULL if the list is empty.
280   const Got_offset_list*
281   get_list() const
282   {
283     if (this->got_type_ == -1U)
284       return NULL;
285     return this;
286   }
287 
288   // Abstract visitor class for iterating over GOT offsets.
289   class Visitor
290   {
291    public:
292     Visitor()
293     { }
294 
295     virtual
296     ~Visitor()
297     { }
298 
299     virtual void
300     visit(unsigned int, unsigned int) = 0;
301   };
302 
303   // Loop over all GOT offset entries, calling a visitor class V for each.
304   void
305   for_all_got_offsets(Visitor* v) const
306   {
307     if (this->got_type_ == -1U)
308       return;
309     for (const Got_offset_list* g = this; g != NULL; g = g->got_next_)
310       v->visit(g->got_type_, g->got_offset_);
311   }
312 
313  private:
314   unsigned int got_type_;
315   unsigned int got_offset_;
316   Got_offset_list* got_next_;
317 };
318 
319 // The Local_got_entry_key used to index the GOT offsets for local
320 // non-TLS symbols, and tp-relative offsets for TLS symbols.
321 
322 class Local_got_entry_key
323 {
324  public:
325   Local_got_entry_key(unsigned int symndx, uint64_t addend)
326     : symndx_(symndx), addend_(addend)
327   {}
328 
329   // Whether this equals to another Local_got_entry_key.
330   bool
331   eq(const Local_got_entry_key& key) const
332   {
333     return (this->symndx_ == key.symndx_ && this->addend_ == key.addend_);
334   }
335 
336   // Compute a hash value for this using 64-bit FNV-1a hash.
337   size_t
338   hash_value() const
339   {
340     uint64_t h = 14695981039346656037ULL; // FNV offset basis.
341     uint64_t prime = 1099511628211ULL;
342     h = (h ^ static_cast<uint64_t>(this->symndx_)) * prime;
343     h = (h ^ static_cast<uint64_t>(this->addend_)) * prime;
344     return h;
345   }
346 
347   // Functors for associative containers.
348   struct equal_to
349   {
350     bool
351     operator()(const Local_got_entry_key& key1,
352                const Local_got_entry_key& key2) const
353     { return key1.eq(key2); }
354   };
355 
356   struct hash
357   {
358     size_t
359     operator()(const Local_got_entry_key& key) const
360     { return key.hash_value(); }
361   };
362 
363  private:
364   // The local symbol index.
365   unsigned int symndx_;
366   // The addend.
367   uint64_t addend_;
368 };
369 
370 // Type for mapping section index to uncompressed size and contents.
371 
372 struct Compressed_section_info
373 {
374   section_size_type size;
375   elfcpp::Elf_Xword flag;
376   uint64_t addralign;
377   const unsigned char* contents;
378 };
379 typedef std::map<unsigned int, Compressed_section_info> Compressed_section_map;
380 
381 template<int size, bool big_endian>
382 Compressed_section_map*
383 build_compressed_section_map(const unsigned char* pshdrs, unsigned int shnum,
384 			     const char* names, section_size_type names_size,
385 			     Object* obj, bool decompress_if_needed);
386 
387 // Object is an abstract base class which represents either a 32-bit
388 // or a 64-bit input object.  This can be a regular object file
389 // (ET_REL) or a shared object (ET_DYN).
390 
391 class Object
392 {
393  public:
394   typedef std::vector<Symbol*> Symbols;
395 
396   // NAME is the name of the object as we would report it to the user
397   // (e.g., libfoo.a(bar.o) if this is in an archive.  INPUT_FILE is
398   // used to read the file.  OFFSET is the offset within the input
399   // file--0 for a .o or .so file, something else for a .a file.
400   Object(const std::string& name, Input_file* input_file, bool is_dynamic,
401 	 off_t offset = 0)
402     : name_(name), input_file_(input_file), offset_(offset), shnum_(-1U),
403       is_dynamic_(is_dynamic), is_needed_(false), uses_split_stack_(false),
404       has_no_split_stack_(false), no_export_(false),
405       is_in_system_directory_(false), as_needed_(false), xindex_(NULL),
406       compressed_sections_(NULL)
407   {
408     if (input_file != NULL)
409       {
410 	input_file->file().add_object();
411 	this->is_in_system_directory_ = input_file->is_in_system_directory();
412 	this->as_needed_ = input_file->options().as_needed();
413       }
414   }
415 
416   virtual ~Object()
417   {
418     if (this->input_file_ != NULL)
419       this->input_file_->file().remove_object();
420   }
421 
422   // Return the name of the object as we would report it to the user.
423   const std::string&
424   name() const
425   { return this->name_; }
426 
427   // Get the offset into the file.
428   off_t
429   offset() const
430   { return this->offset_; }
431 
432   // Return whether this is a dynamic object.
433   bool
434   is_dynamic() const
435   { return this->is_dynamic_; }
436 
437   // Return the word size of the object file.
438   virtual int elfsize() const = 0;
439 
440   // Return TRUE if this is a big-endian object file.
441   virtual bool is_big_endian() const = 0;
442 
443   // Return whether this object is needed--true if it is a dynamic
444   // object which defines some symbol referenced by a regular object.
445   // We keep the flag here rather than in Dynobj for convenience when
446   // setting it.
447   bool
448   is_needed() const
449   { return this->is_needed_; }
450 
451   // Record that this object is needed.
452   void
453   set_is_needed()
454   { this->is_needed_ = true; }
455 
456   // Return whether this object was compiled with -fsplit-stack.
457   bool
458   uses_split_stack() const
459   { return this->uses_split_stack_; }
460 
461   // Return whether this object contains any functions compiled with
462   // the no_split_stack attribute.
463   bool
464   has_no_split_stack() const
465   { return this->has_no_split_stack_; }
466 
467   // Returns NULL for Objects that are not dynamic objects.  This method
468   // is overridden in the Dynobj class.
469   Dynobj*
470   dynobj()
471   { return this->do_dynobj(); }
472 
473   // Returns NULL for Objects that are not plugin objects.  This method
474   // is overridden in the Pluginobj class.
475   Pluginobj*
476   pluginobj()
477   { return this->do_pluginobj(); }
478 
479   // Get the file.  We pass on const-ness.
480   Input_file*
481   input_file()
482   {
483     gold_assert(this->input_file_ != NULL);
484     return this->input_file_;
485   }
486 
487   const Input_file*
488   input_file() const
489   {
490     gold_assert(this->input_file_ != NULL);
491     return this->input_file_;
492   }
493 
494   // Lock the underlying file.
495   void
496   lock(const Task* t)
497   {
498     if (this->input_file_ != NULL)
499       this->input_file_->file().lock(t);
500   }
501 
502   // Unlock the underlying file.
503   void
504   unlock(const Task* t)
505   {
506     if (this->input_file_ != NULL)
507       this->input_file()->file().unlock(t);
508   }
509 
510   // Return whether the underlying file is locked.
511   bool
512   is_locked() const
513   { return this->input_file_ != NULL && this->input_file_->file().is_locked(); }
514 
515   // Return the token, so that the task can be queued.
516   Task_token*
517   token()
518   {
519     if (this->input_file_ == NULL)
520       return NULL;
521     return this->input_file()->file().token();
522   }
523 
524   // Release the underlying file.
525   void
526   release()
527   {
528     if (this->input_file_ != NULL)
529       this->input_file()->file().release();
530   }
531 
532   // Return whether we should just read symbols from this file.
533   bool
534   just_symbols() const
535   { return this->input_file()->just_symbols(); }
536 
537   // Return whether this is an incremental object.
538   bool
539   is_incremental() const
540   { return this->do_is_incremental(); }
541 
542   // Return the last modified time of the file.
543   Timespec
544   get_mtime()
545   { return this->do_get_mtime(); }
546 
547   // Get the number of sections.
548   unsigned int
549   shnum() const
550   { return this->shnum_; }
551 
552   // Return a view of the contents of a section.  Set *PLEN to the
553   // size.  CACHE is a hint as in File_read::get_view.
554   const unsigned char*
555   section_contents(unsigned int shndx, section_size_type* plen, bool cache);
556 
557   // Adjust a symbol's section index as needed.  SYMNDX is the index
558   // of the symbol and SHNDX is the symbol's section from
559   // get_st_shndx.  This returns the section index.  It sets
560   // *IS_ORDINARY to indicate whether this is a normal section index,
561   // rather than a special code between SHN_LORESERVE and
562   // SHN_HIRESERVE.
563   unsigned int
564   adjust_sym_shndx(unsigned int symndx, unsigned int shndx, bool* is_ordinary)
565   {
566     if (shndx < elfcpp::SHN_LORESERVE)
567       *is_ordinary = true;
568     else if (shndx == elfcpp::SHN_XINDEX)
569       {
570 	if (this->xindex_ == NULL)
571 	  this->xindex_ = this->do_initialize_xindex();
572 	shndx = this->xindex_->sym_xindex_to_shndx(this, symndx);
573 	*is_ordinary = true;
574       }
575     else
576       *is_ordinary = false;
577     return shndx;
578   }
579 
580   // Return the size of a section given a section index.
581   uint64_t
582   section_size(unsigned int shndx)
583   { return this->do_section_size(shndx); }
584 
585   // Return the name of a section given a section index.
586   std::string
587   section_name(unsigned int shndx) const
588   { return this->do_section_name(shndx); }
589 
590   // Return the section flags given a section index.
591   uint64_t
592   section_flags(unsigned int shndx)
593   { return this->do_section_flags(shndx); }
594 
595   // Return the section entsize given a section index.
596   uint64_t
597   section_entsize(unsigned int shndx)
598   { return this->do_section_entsize(shndx); }
599 
600   // Return the section address given a section index.
601   uint64_t
602   section_address(unsigned int shndx)
603   { return this->do_section_address(shndx); }
604 
605   // Return the section type given a section index.
606   unsigned int
607   section_type(unsigned int shndx)
608   { return this->do_section_type(shndx); }
609 
610   // Return the section link field given a section index.
611   unsigned int
612   section_link(unsigned int shndx)
613   { return this->do_section_link(shndx); }
614 
615   // Return the section info field given a section index.
616   unsigned int
617   section_info(unsigned int shndx)
618   { return this->do_section_info(shndx); }
619 
620   // Return the required section alignment given a section index.
621   uint64_t
622   section_addralign(unsigned int shndx)
623   { return this->do_section_addralign(shndx); }
624 
625   // Return the output section given a section index.
626   Output_section*
627   output_section(unsigned int shndx) const
628   { return this->do_output_section(shndx); }
629 
630   // Given a section index, return its address.
631   // The return value will be -1U if the section is specially mapped,
632   // such as a merge section.
633   uint64_t
634   output_section_address(unsigned int shndx)
635   { return this->do_output_section_address(shndx); }
636 
637   // Given a section index, return the offset in the Output_section.
638   // The return value will be -1U if the section is specially mapped,
639   // such as a merge section.
640   uint64_t
641   output_section_offset(unsigned int shndx) const
642   { return this->do_output_section_offset(shndx); }
643 
644   // Read the symbol information.
645   void
646   read_symbols(Read_symbols_data* sd)
647   { return this->do_read_symbols(sd); }
648 
649   // Pass sections which should be included in the link to the Layout
650   // object, and record where the sections go in the output file.
651   void
652   layout(Symbol_table* symtab, Layout* layout, Read_symbols_data* sd)
653   { this->do_layout(symtab, layout, sd); }
654 
655   // Add symbol information to the global symbol table.
656   void
657   add_symbols(Symbol_table* symtab, Read_symbols_data* sd, Layout *layout)
658   { this->do_add_symbols(symtab, sd, layout); }
659 
660   // Add symbol information to the global symbol table.
661   Archive::Should_include
662   should_include_member(Symbol_table* symtab, Layout* layout,
663 			Read_symbols_data* sd, std::string* why)
664   { return this->do_should_include_member(symtab, layout, sd, why); }
665 
666   // Iterate over global symbols, calling a visitor class V for each.
667   void
668   for_all_global_symbols(Read_symbols_data* sd,
669 			 Library_base::Symbol_visitor_base* v)
670   { return this->do_for_all_global_symbols(sd, v); }
671 
672   // Iterate over local symbols, calling a visitor class V for each GOT offset
673   // associated with a local symbol.
674   void
675   for_all_local_got_entries(Got_offset_list::Visitor* v) const
676   { this->do_for_all_local_got_entries(v); }
677 
678   // Functions and types for the elfcpp::Elf_file interface.  This
679   // permit us to use Object as the File template parameter for
680   // elfcpp::Elf_file.
681 
682   // The View class is returned by view.  It must support a single
683   // method, data().  This is trivial, because get_view does what we
684   // need.
685   class View
686   {
687    public:
688     View(const unsigned char* p)
689       : p_(p)
690     { }
691 
692     const unsigned char*
693     data() const
694     { return this->p_; }
695 
696    private:
697     const unsigned char* p_;
698   };
699 
700   // Return a View.
701   View
702   view(off_t file_offset, section_size_type data_size)
703   { return View(this->get_view(file_offset, data_size, true, true)); }
704 
705   // Report an error.
706   void
707   error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
708 
709   // A location in the file.
710   struct Location
711   {
712     off_t file_offset;
713     off_t data_size;
714 
715     Location(off_t fo, section_size_type ds)
716       : file_offset(fo), data_size(ds)
717     { }
718   };
719 
720   // Get a View given a Location.
721   View view(Location loc)
722   { return View(this->get_view(loc.file_offset, loc.data_size, true, true)); }
723 
724   // Get a view into the underlying file.
725   const unsigned char*
726   get_view(off_t start, section_size_type size, bool aligned, bool cache)
727   {
728     return this->input_file()->file().get_view(this->offset_, start, size,
729 					       aligned, cache);
730   }
731 
732   // Get a lasting view into the underlying file.
733   File_view*
734   get_lasting_view(off_t start, section_size_type size, bool aligned,
735 		   bool cache)
736   {
737     return this->input_file()->file().get_lasting_view(this->offset_, start,
738 						       size, aligned, cache);
739   }
740 
741   // Read data from the underlying file.
742   void
743   read(off_t start, section_size_type size, void* p)
744   { this->input_file()->file().read(start + this->offset_, size, p); }
745 
746   // Read multiple data from the underlying file.
747   void
748   read_multiple(const File_read::Read_multiple& rm)
749   { this->input_file()->file().read_multiple(this->offset_, rm); }
750 
751   // Stop caching views in the underlying file.
752   void
753   clear_view_cache_marks()
754   {
755     if (this->input_file_ != NULL)
756       this->input_file_->file().clear_view_cache_marks();
757   }
758 
759   // Get the number of global symbols defined by this object, and the
760   // number of the symbols whose final definition came from this
761   // object.
762   void
763   get_global_symbol_counts(const Symbol_table* symtab, size_t* defined,
764 			   size_t* used) const
765   { this->do_get_global_symbol_counts(symtab, defined, used); }
766 
767   // Get the symbols defined in this object.
768   const Symbols*
769   get_global_symbols() const
770   { return this->do_get_global_symbols(); }
771 
772   // Set flag that this object was found in a system directory.
773   void
774   set_is_in_system_directory()
775   { this->is_in_system_directory_ = true; }
776 
777   // Return whether this object was found in a system directory.
778   bool
779   is_in_system_directory() const
780   { return this->is_in_system_directory_; }
781 
782   // Set flag that this object was linked with --as-needed.
783   void
784   set_as_needed()
785   { this->as_needed_ = true; }
786 
787   // Clear flag that this object was linked with --as-needed.
788   void
789   clear_as_needed()
790   { this->as_needed_ = false; }
791 
792   // Return whether this object was linked with --as-needed.
793   bool
794   as_needed() const
795   { return this->as_needed_; }
796 
797   // Return whether we found this object by searching a directory.
798   bool
799   searched_for() const
800   { return this->input_file()->will_search_for(); }
801 
802   bool
803   no_export() const
804   { return this->no_export_; }
805 
806   void
807   set_no_export(bool value)
808   { this->no_export_ = value; }
809 
810   bool
811   section_is_compressed(unsigned int shndx,
812 			section_size_type* uncompressed_size,
813 			elfcpp::Elf_Xword* palign = NULL) const
814   {
815     if (this->compressed_sections_ == NULL)
816       return false;
817     Compressed_section_map::const_iterator p =
818         this->compressed_sections_->find(shndx);
819     if (p != this->compressed_sections_->end())
820       {
821 	if (uncompressed_size != NULL)
822 	  *uncompressed_size = p->second.size;
823 	if (palign != NULL)
824 	  *palign = p->second.addralign;
825 	return true;
826       }
827     return false;
828   }
829 
830   // Return a view of the decompressed contents of a section.  Set *PLEN
831   // to the size.  Set *IS_NEW to true if the contents need to be freed
832   // by the caller.
833   const unsigned char*
834   decompressed_section_contents(unsigned int shndx, section_size_type* plen,
835 				bool* is_cached, uint64_t* palign = NULL);
836 
837   // Discard any buffers of decompressed sections.  This is done
838   // at the end of the Add_symbols task.
839   void
840   discard_decompressed_sections();
841 
842   // Return the index of the first incremental relocation for symbol SYMNDX.
843   unsigned int
844   get_incremental_reloc_base(unsigned int symndx) const
845   { return this->do_get_incremental_reloc_base(symndx); }
846 
847   // Return the number of incremental relocations for symbol SYMNDX.
848   unsigned int
849   get_incremental_reloc_count(unsigned int symndx) const
850   { return this->do_get_incremental_reloc_count(symndx); }
851 
852   // Return the output view for section SHNDX.
853   unsigned char*
854   get_output_view(unsigned int shndx, section_size_type* plen) const
855   { return this->do_get_output_view(shndx, plen); }
856 
857  protected:
858   // Returns NULL for Objects that are not dynamic objects.  This method
859   // is overridden in the Dynobj class.
860   virtual Dynobj*
861   do_dynobj()
862   { return NULL; }
863 
864   // Returns NULL for Objects that are not plugin objects.  This method
865   // is overridden in the Pluginobj class.
866   virtual Pluginobj*
867   do_pluginobj()
868   { return NULL; }
869 
870   // Return TRUE if this is an incremental (unchanged) input file.
871   // We return FALSE by default; the incremental object classes
872   // override this method.
873   virtual bool
874   do_is_incremental() const
875   { return false; }
876 
877   // Return the last modified time of the file.  This method may be
878   // overridden for subclasses that don't use an actual file (e.g.,
879   // Incremental objects).
880   virtual Timespec
881   do_get_mtime()
882   { return this->input_file()->file().get_mtime(); }
883 
884   // Read the symbols--implemented by child class.
885   virtual void
886   do_read_symbols(Read_symbols_data*) = 0;
887 
888   // Lay out sections--implemented by child class.
889   virtual void
890   do_layout(Symbol_table*, Layout*, Read_symbols_data*) = 0;
891 
892   // Add symbol information to the global symbol table--implemented by
893   // child class.
894   virtual void
895   do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*) = 0;
896 
897   virtual Archive::Should_include
898   do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
899                            std::string* why) = 0;
900 
901   // Iterate over global symbols, calling a visitor class V for each.
902   virtual void
903   do_for_all_global_symbols(Read_symbols_data* sd,
904 			    Library_base::Symbol_visitor_base* v) = 0;
905 
906   // Iterate over local symbols, calling a visitor class V for each GOT offset
907   // associated with a local symbol.
908   virtual void
909   do_for_all_local_got_entries(Got_offset_list::Visitor* v) const = 0;
910 
911   // Return the location of the contents of a section.  Implemented by
912   // child class.
913   virtual const unsigned char*
914   do_section_contents(unsigned int shndx, section_size_type* plen,
915 		      bool cache) = 0;
916 
917   // Get the size of a section--implemented by child class.
918   virtual uint64_t
919   do_section_size(unsigned int shndx) = 0;
920 
921   // Get the name of a section--implemented by child class.
922   virtual std::string
923   do_section_name(unsigned int shndx) const = 0;
924 
925   // Get section flags--implemented by child class.
926   virtual uint64_t
927   do_section_flags(unsigned int shndx) = 0;
928 
929   // Get section entsize--implemented by child class.
930   virtual uint64_t
931   do_section_entsize(unsigned int shndx) = 0;
932 
933   // Get section address--implemented by child class.
934   virtual uint64_t
935   do_section_address(unsigned int shndx) = 0;
936 
937   // Get section type--implemented by child class.
938   virtual unsigned int
939   do_section_type(unsigned int shndx) = 0;
940 
941   // Get section link field--implemented by child class.
942   virtual unsigned int
943   do_section_link(unsigned int shndx) = 0;
944 
945   // Get section info field--implemented by child class.
946   virtual unsigned int
947   do_section_info(unsigned int shndx) = 0;
948 
949   // Get section alignment--implemented by child class.
950   virtual uint64_t
951   do_section_addralign(unsigned int shndx) = 0;
952 
953   // Return the output section given a section index--implemented
954   // by child class.
955   virtual Output_section*
956   do_output_section(unsigned int) const
957   { gold_unreachable(); }
958 
959   // Get the address of a section--implemented by child class.
960   virtual uint64_t
961   do_output_section_address(unsigned int)
962   { gold_unreachable(); }
963 
964   // Get the offset of a section--implemented by child class.
965   virtual uint64_t
966   do_output_section_offset(unsigned int) const
967   { gold_unreachable(); }
968 
969   // Return the Xindex structure to use.
970   virtual Xindex*
971   do_initialize_xindex() = 0;
972 
973   // Implement get_global_symbol_counts--implemented by child class.
974   virtual void
975   do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const = 0;
976 
977   virtual const Symbols*
978   do_get_global_symbols() const = 0;
979 
980   // Set the number of sections.
981   void
982   set_shnum(int shnum)
983   { this->shnum_ = shnum; }
984 
985   // Functions used by both Sized_relobj_file and Sized_dynobj.
986 
987   // Read the section data into a Read_symbols_data object.
988   template<int size, bool big_endian>
989   void
990   read_section_data(elfcpp::Elf_file<size, big_endian, Object>*,
991 		    Read_symbols_data*);
992 
993   // Find the section header with the given NAME.  If HDR is non-NULL
994   // then it is a section header returned from a previous call to this
995   // function and the next section header with the same name will be
996   // returned.
997   template<int size, bool big_endian>
998   const unsigned char*
999   find_shdr(const unsigned char* pshdrs, const char* name,
1000 	    const char* names, section_size_type names_size,
1001 	    const unsigned char* hdr) const;
1002 
1003   // Let the child class initialize the xindex object directly.
1004   void
1005   set_xindex(Xindex* xindex)
1006   {
1007     gold_assert(this->xindex_ == NULL);
1008     this->xindex_ = xindex;
1009   }
1010 
1011   // If NAME is the name of a special .gnu.warning section, arrange
1012   // for the warning to be issued.  SHNDX is the section index.
1013   // Return whether it is a warning section.
1014   bool
1015   handle_gnu_warning_section(const char* name, unsigned int shndx,
1016 			     Symbol_table*);
1017 
1018   // If NAME is the name of the special section which indicates that
1019   // this object was compiled with -fsplit-stack, mark it accordingly,
1020   // and return true.  Otherwise return false.
1021   bool
1022   handle_split_stack_section(const char* name);
1023 
1024   // Discard any buffers of decompressed sections.  This is done
1025   // at the end of the Add_symbols task.
1026   virtual void
1027   do_discard_decompressed_sections()
1028   { }
1029 
1030   // Return the index of the first incremental relocation for symbol SYMNDX--
1031   // implemented by child class.
1032   virtual unsigned int
1033   do_get_incremental_reloc_base(unsigned int) const
1034   { gold_unreachable(); }
1035 
1036   // Return the number of incremental relocations for symbol SYMNDX--
1037   // implemented by child class.
1038   virtual unsigned int
1039   do_get_incremental_reloc_count(unsigned int) const
1040   { gold_unreachable(); }
1041 
1042   // Return the output view for a section.
1043   virtual unsigned char*
1044   do_get_output_view(unsigned int, section_size_type*) const
1045   { gold_unreachable(); }
1046 
1047   void
1048   set_compressed_sections(Compressed_section_map* compressed_sections)
1049   { this->compressed_sections_ = compressed_sections; }
1050 
1051   Compressed_section_map*
1052   compressed_sections()
1053   { return this->compressed_sections_; }
1054 
1055  private:
1056   // This class may not be copied.
1057   Object(const Object&);
1058   Object& operator=(const Object&);
1059 
1060   // Name of object as printed to user.
1061   std::string name_;
1062   // For reading the file.
1063   Input_file* input_file_;
1064   // Offset within the file--0 for an object file, non-0 for an
1065   // archive.
1066   off_t offset_;
1067   // Number of input sections.
1068   unsigned int shnum_;
1069   // Whether this is a dynamic object.
1070   bool is_dynamic_ : 1;
1071   // Whether this object is needed.  This is only set for dynamic
1072   // objects, and means that the object defined a symbol which was
1073   // used by a reference from a regular object.
1074   bool is_needed_ : 1;
1075   // Whether this object was compiled with -fsplit-stack.
1076   bool uses_split_stack_ : 1;
1077   // Whether this object contains any functions compiled with the
1078   // no_split_stack attribute.
1079   bool has_no_split_stack_ : 1;
1080   // True if exclude this object from automatic symbol export.
1081   // This is used only for archive objects.
1082   bool no_export_ : 1;
1083   // True if the object was found in a system directory.
1084   bool is_in_system_directory_ : 1;
1085   // True if the object was linked with --as-needed.
1086   bool as_needed_ : 1;
1087   // Many sections for objects with more than SHN_LORESERVE sections.
1088   Xindex* xindex_;
1089   // For compressed debug sections, map section index to uncompressed size
1090   // and contents.
1091   Compressed_section_map* compressed_sections_;
1092 };
1093 
1094 // A regular object (ET_REL).  This is an abstract base class itself.
1095 // The implementation is the template class Sized_relobj_file.
1096 
1097 class Relobj : public Object
1098 {
1099  public:
1100   Relobj(const std::string& name, Input_file* input_file, off_t offset = 0)
1101     : Object(name, input_file, false, offset),
1102       output_sections_(),
1103       map_to_relocatable_relocs_(NULL),
1104       object_merge_map_(NULL),
1105       relocs_must_follow_section_writes_(false),
1106       sd_(NULL),
1107       reloc_counts_(NULL),
1108       reloc_bases_(NULL),
1109       first_dyn_reloc_(0),
1110       dyn_reloc_count_(0)
1111   { }
1112 
1113   // During garbage collection, the Read_symbols_data pass for
1114   // each object is stored as layout needs to be done after
1115   // reloc processing.
1116   Symbols_data*
1117   get_symbols_data()
1118   { return this->sd_; }
1119 
1120   // Decides which section names have to be included in the worklist
1121   // as roots.
1122   bool
1123   is_section_name_included(const char* name);
1124 
1125   void
1126   copy_symbols_data(Symbols_data* gc_sd, Read_symbols_data* sd,
1127                     unsigned int section_header_size);
1128 
1129   void
1130   set_symbols_data(Symbols_data* sd)
1131   { this->sd_ = sd; }
1132 
1133   // During garbage collection, the Read_relocs pass for all objects
1134   // is done before scanning the relocs.  In that case, this->rd_ is
1135   // used to store the information from Read_relocs for each object.
1136   // This data is also used to compute the list of relevant sections.
1137   Read_relocs_data*
1138   get_relocs_data()
1139   { return this->rd_; }
1140 
1141   void
1142   set_relocs_data(Read_relocs_data* rd)
1143   { this->rd_ = rd; }
1144 
1145   virtual bool
1146   is_output_section_offset_invalid(unsigned int shndx) const = 0;
1147 
1148   // Read the relocs.
1149   void
1150   read_relocs(Read_relocs_data* rd)
1151   { return this->do_read_relocs(rd); }
1152 
1153   // Process the relocs, during garbage collection only.
1154   void
1155   gc_process_relocs(Symbol_table* symtab, Layout* layout, Read_relocs_data* rd)
1156   { return this->do_gc_process_relocs(symtab, layout, rd); }
1157 
1158   // Scan the relocs and adjust the symbol table.
1159   void
1160   scan_relocs(Symbol_table* symtab, Layout* layout, Read_relocs_data* rd)
1161   { return this->do_scan_relocs(symtab, layout, rd); }
1162 
1163   // Return the value of the local symbol whose index is SYMNDX, plus
1164   // ADDEND.  ADDEND is passed in so that we can correctly handle the
1165   // section symbol for a merge section.
1166   uint64_t
1167   local_symbol_value(unsigned int symndx, uint64_t addend) const
1168   { return this->do_local_symbol_value(symndx, addend); }
1169 
1170   // Return the PLT offset for a local symbol.  It is an error to call
1171   // this if it doesn't have one.
1172   unsigned int
1173   local_plt_offset(unsigned int symndx) const
1174   { return this->do_local_plt_offset(symndx); }
1175 
1176   // Return whether the local symbol SYMNDX has a GOT offset of type
1177   // GOT_TYPE.
1178   bool
1179   local_has_got_offset(unsigned int symndx, unsigned int got_type) const
1180   { return this->do_local_has_got_offset(symndx, got_type, 0); }
1181 
1182   // Return whether the local symbol SYMNDX plus ADDEND has a GOT offset
1183   // of type GOT_TYPE.
1184   bool
1185   local_has_got_offset(unsigned int symndx, unsigned int got_type,
1186 		       uint64_t addend) const
1187   { return this->do_local_has_got_offset(symndx, got_type, addend); }
1188 
1189   // Return the GOT offset of type GOT_TYPE of the local symbol
1190   // SYMNDX.  It is an error to call this if the symbol does not have
1191   // a GOT offset of the specified type.
1192   unsigned int
1193   local_got_offset(unsigned int symndx, unsigned int got_type) const
1194   { return this->do_local_got_offset(symndx, got_type, 0); }
1195 
1196   // Return the GOT offset of type GOT_TYPE of the local symbol
1197   // SYMNDX plus ADDEND.  It is an error to call this if the symbol
1198   // does not have a GOT offset of the specified type.
1199   unsigned int
1200   local_got_offset(unsigned int symndx, unsigned int got_type,
1201 		       uint64_t addend) const
1202   { return this->do_local_got_offset(symndx, got_type, addend); }
1203 
1204   // Set the GOT offset with type GOT_TYPE of the local symbol SYMNDX
1205   // to GOT_OFFSET.
1206   void
1207   set_local_got_offset(unsigned int symndx, unsigned int got_type,
1208 		       unsigned int got_offset)
1209   { this->do_set_local_got_offset(symndx, got_type, got_offset, 0); }
1210 
1211   // Set the GOT offset with type GOT_TYPE of the local symbol SYMNDX
1212   // plus ADDEND to GOT_OFFSET.
1213   void
1214   set_local_got_offset(unsigned int symndx, unsigned int got_type,
1215 		       unsigned int got_offset, uint64_t addend)
1216   { this->do_set_local_got_offset(symndx, got_type, got_offset, addend); }
1217 
1218   // Return whether the local symbol SYMNDX is a TLS symbol.
1219   bool
1220   local_is_tls(unsigned int symndx) const
1221   { return this->do_local_is_tls(symndx); }
1222 
1223   // The number of local symbols in the input symbol table.
1224   virtual unsigned int
1225   local_symbol_count() const
1226   { return this->do_local_symbol_count(); }
1227 
1228   // The number of local symbols in the output symbol table.
1229   virtual unsigned int
1230   output_local_symbol_count() const
1231   { return this->do_output_local_symbol_count(); }
1232 
1233   // The file offset for local symbols in the output symbol table.
1234   virtual off_t
1235   local_symbol_offset() const
1236   { return this->do_local_symbol_offset(); }
1237 
1238   // Initial local symbol processing: count the number of local symbols
1239   // in the output symbol table and dynamic symbol table; add local symbol
1240   // names to *POOL and *DYNPOOL.
1241   void
1242   count_local_symbols(Stringpool_template<char>* pool,
1243                       Stringpool_template<char>* dynpool)
1244   { return this->do_count_local_symbols(pool, dynpool); }
1245 
1246   // Set the values of the local symbols, set the output symbol table
1247   // indexes for the local variables, and set the offset where local
1248   // symbol information will be stored. Returns the new local symbol index.
1249   unsigned int
1250   finalize_local_symbols(unsigned int index, off_t off, Symbol_table* symtab)
1251   { return this->do_finalize_local_symbols(index, off, symtab); }
1252 
1253   // Set the output dynamic symbol table indexes for the local variables.
1254   unsigned int
1255   set_local_dynsym_indexes(unsigned int index)
1256   { return this->do_set_local_dynsym_indexes(index); }
1257 
1258   // Set the offset where local dynamic symbol information will be stored.
1259   unsigned int
1260   set_local_dynsym_offset(off_t off)
1261   { return this->do_set_local_dynsym_offset(off); }
1262 
1263   // Record a dynamic relocation against an input section from this object.
1264   void
1265   add_dyn_reloc(unsigned int index)
1266   {
1267     if (this->dyn_reloc_count_ == 0)
1268       this->first_dyn_reloc_ = index;
1269     ++this->dyn_reloc_count_;
1270   }
1271 
1272   // Return the index of the first dynamic relocation.
1273   unsigned int
1274   first_dyn_reloc() const
1275   { return this->first_dyn_reloc_; }
1276 
1277   // Return the count of dynamic relocations.
1278   unsigned int
1279   dyn_reloc_count() const
1280   { return this->dyn_reloc_count_; }
1281 
1282   // Relocate the input sections and write out the local symbols.
1283   void
1284   relocate(const Symbol_table* symtab, const Layout* layout, Output_file* of)
1285   { return this->do_relocate(symtab, layout, of); }
1286 
1287   // Return whether an input section is being included in the link.
1288   bool
1289   is_section_included(unsigned int shndx) const
1290   {
1291     gold_assert(shndx < this->output_sections_.size());
1292     return this->output_sections_[shndx] != NULL;
1293   }
1294 
1295   // The output section of the input section with index SHNDX.
1296   // This is only used currently to remove a section from the link in
1297   // relaxation.
1298   void
1299   set_output_section(unsigned int shndx, Output_section* os)
1300   {
1301     gold_assert(shndx < this->output_sections_.size());
1302     this->output_sections_[shndx] = os;
1303   }
1304 
1305   // Set the offset of an input section within its output section.
1306   void
1307   set_section_offset(unsigned int shndx, uint64_t off)
1308   { this->do_set_section_offset(shndx, off); }
1309 
1310   // Return true if we need to wait for output sections to be written
1311   // before we can apply relocations.  This is true if the object has
1312   // any relocations for sections which require special handling, such
1313   // as the exception frame section.
1314   bool
1315   relocs_must_follow_section_writes() const
1316   { return this->relocs_must_follow_section_writes_; }
1317 
1318   Object_merge_map*
1319   get_or_create_merge_map();
1320 
1321   template<int size>
1322   void
1323   initialize_input_to_output_map(unsigned int shndx,
1324       typename elfcpp::Elf_types<size>::Elf_Addr starting_address,
1325       Unordered_map<section_offset_type,
1326 	    typename elfcpp::Elf_types<size>::Elf_Addr>* output_address) const;
1327 
1328   void
1329   add_merge_mapping(Output_section_data *output_data,
1330                     unsigned int shndx, section_offset_type offset,
1331                     section_size_type length,
1332                     section_offset_type output_offset);
1333 
1334   bool
1335   merge_output_offset(unsigned int shndx, section_offset_type offset,
1336                       section_offset_type *poutput) const;
1337 
1338   const Output_section_data*
1339   find_merge_section(unsigned int shndx) const;
1340 
1341   // Record the relocatable reloc info for an input reloc section.
1342   void
1343   set_relocatable_relocs(unsigned int reloc_shndx, Relocatable_relocs* rr)
1344   {
1345     gold_assert(reloc_shndx < this->shnum());
1346     (*this->map_to_relocatable_relocs_)[reloc_shndx] = rr;
1347   }
1348 
1349   // Get the relocatable reloc info for an input reloc section.
1350   Relocatable_relocs*
1351   relocatable_relocs(unsigned int reloc_shndx)
1352   {
1353     gold_assert(reloc_shndx < this->shnum());
1354     return (*this->map_to_relocatable_relocs_)[reloc_shndx];
1355   }
1356 
1357   // Layout sections whose layout was deferred while waiting for
1358   // input files from a plugin.
1359   void
1360   layout_deferred_sections(Layout* layout)
1361   { this->do_layout_deferred_sections(layout); }
1362 
1363   // Return the index of the first incremental relocation for symbol SYMNDX.
1364   virtual unsigned int
1365   do_get_incremental_reloc_base(unsigned int symndx) const
1366   { return this->reloc_bases_[symndx]; }
1367 
1368   // Return the number of incremental relocations for symbol SYMNDX.
1369   virtual unsigned int
1370   do_get_incremental_reloc_count(unsigned int symndx) const
1371   { return this->reloc_counts_[symndx]; }
1372 
1373   // Return the word size of the object file.
1374   int
1375   elfsize() const
1376   { return this->do_elfsize(); }
1377 
1378   // Return TRUE if this is a big-endian object file.
1379   bool
1380   is_big_endian() const
1381   { return this->do_is_big_endian(); }
1382 
1383  protected:
1384   // The output section to be used for each input section, indexed by
1385   // the input section number.  The output section is NULL if the
1386   // input section is to be discarded.
1387   typedef std::vector<Output_section*> Output_sections;
1388 
1389   // Read the relocs--implemented by child class.
1390   virtual void
1391   do_read_relocs(Read_relocs_data*) = 0;
1392 
1393   // Process the relocs--implemented by child class.
1394   virtual void
1395   do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*) = 0;
1396 
1397   // Scan the relocs--implemented by child class.
1398   virtual void
1399   do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*) = 0;
1400 
1401   // Return the value of a local symbol.
1402   virtual uint64_t
1403   do_local_symbol_value(unsigned int symndx, uint64_t addend) const = 0;
1404 
1405   // Return the PLT offset of a local symbol.
1406   virtual unsigned int
1407   do_local_plt_offset(unsigned int symndx) const = 0;
1408 
1409   // Return whether a local symbol plus addend has a GOT offset
1410   // of a given type.
1411   virtual bool
1412   do_local_has_got_offset(unsigned int symndx,
1413 			  unsigned int got_type, uint64_t addend) const = 0;
1414 
1415   // Return the GOT offset of a given type of a local symbol plus addend.
1416   virtual unsigned int
1417   do_local_got_offset(unsigned int symndx, unsigned int got_type,
1418 		      uint64_t addend) const = 0;
1419 
1420   // Set the GOT offset with a given type for a local symbol plus addend.
1421   virtual void
1422   do_set_local_got_offset(unsigned int symndx, unsigned int got_type,
1423 			  unsigned int got_offset, uint64_t addend) = 0;
1424 
1425   // Return whether local symbol SYMNDX is a TLS symbol.
1426   virtual bool
1427   do_local_is_tls(unsigned int symndx) const = 0;
1428 
1429   // Return the number of local symbols--implemented by child class.
1430   virtual unsigned int
1431   do_local_symbol_count() const = 0;
1432 
1433   // Return the number of output local symbols--implemented by child class.
1434   virtual unsigned int
1435   do_output_local_symbol_count() const = 0;
1436 
1437   // Return the file offset for local symbols--implemented by child class.
1438   virtual off_t
1439   do_local_symbol_offset() const = 0;
1440 
1441   // Count local symbols--implemented by child class.
1442   virtual void
1443   do_count_local_symbols(Stringpool_template<char>*,
1444 			 Stringpool_template<char>*) = 0;
1445 
1446   // Finalize the local symbols.  Set the output symbol table indexes
1447   // for the local variables, and set the offset where local symbol
1448   // information will be stored.
1449   virtual unsigned int
1450   do_finalize_local_symbols(unsigned int, off_t, Symbol_table*) = 0;
1451 
1452   // Set the output dynamic symbol table indexes for the local variables.
1453   virtual unsigned int
1454   do_set_local_dynsym_indexes(unsigned int) = 0;
1455 
1456   // Set the offset where local dynamic symbol information will be stored.
1457   virtual unsigned int
1458   do_set_local_dynsym_offset(off_t) = 0;
1459 
1460   // Relocate the input sections and write out the local
1461   // symbols--implemented by child class.
1462   virtual void
1463   do_relocate(const Symbol_table* symtab, const Layout*, Output_file* of) = 0;
1464 
1465   // Set the offset of a section--implemented by child class.
1466   virtual void
1467   do_set_section_offset(unsigned int shndx, uint64_t off) = 0;
1468 
1469   // Layout sections whose layout was deferred while waiting for
1470   // input files from a plugin--implemented by child class.
1471   virtual void
1472   do_layout_deferred_sections(Layout*) = 0;
1473 
1474   // Given a section index, return the corresponding Output_section.
1475   // The return value will be NULL if the section is not included in
1476   // the link.
1477   Output_section*
1478   do_output_section(unsigned int shndx) const
1479   {
1480     gold_assert(shndx < this->output_sections_.size());
1481     return this->output_sections_[shndx];
1482   }
1483 
1484   // Return the vector mapping input sections to output sections.
1485   Output_sections&
1486   output_sections()
1487   { return this->output_sections_; }
1488 
1489   const Output_sections&
1490   output_sections() const
1491   { return this->output_sections_; }
1492 
1493   // Set the size of the relocatable relocs array.
1494   void
1495   size_relocatable_relocs()
1496   {
1497     this->map_to_relocatable_relocs_ =
1498       new std::vector<Relocatable_relocs*>(this->shnum());
1499   }
1500 
1501   // Record that we must wait for the output sections to be written
1502   // before applying relocations.
1503   void
1504   set_relocs_must_follow_section_writes()
1505   { this->relocs_must_follow_section_writes_ = true; }
1506 
1507   // Allocate the array for counting incremental relocations.
1508   void
1509   allocate_incremental_reloc_counts()
1510   {
1511     unsigned int nsyms = this->do_get_global_symbols()->size();
1512     this->reloc_counts_ = new unsigned int[nsyms];
1513     gold_assert(this->reloc_counts_ != NULL);
1514     memset(this->reloc_counts_, 0, nsyms * sizeof(unsigned int));
1515   }
1516 
1517   // Record a relocation in this object referencing global symbol SYMNDX.
1518   // Used for tracking incremental link information.
1519   void
1520   count_incremental_reloc(unsigned int symndx)
1521   {
1522     unsigned int nsyms = this->do_get_global_symbols()->size();
1523     gold_assert(symndx < nsyms);
1524     gold_assert(this->reloc_counts_ != NULL);
1525     ++this->reloc_counts_[symndx];
1526   }
1527 
1528   // Finalize the incremental relocation information.
1529   void
1530   finalize_incremental_relocs(Layout* layout, bool clear_counts);
1531 
1532   // Return the index of the next relocation to be written for global symbol
1533   // SYMNDX.  Only valid after finalize_incremental_relocs() has been called.
1534   unsigned int
1535   next_incremental_reloc_index(unsigned int symndx)
1536   {
1537     unsigned int nsyms = this->do_get_global_symbols()->size();
1538 
1539     gold_assert(this->reloc_counts_ != NULL);
1540     gold_assert(this->reloc_bases_ != NULL);
1541     gold_assert(symndx < nsyms);
1542 
1543     unsigned int counter = this->reloc_counts_[symndx]++;
1544     return this->reloc_bases_[symndx] + counter;
1545   }
1546 
1547   // Return the word size of the object file--
1548   // implemented by child class.
1549   virtual int
1550   do_elfsize() const = 0;
1551 
1552   // Return TRUE if this is a big-endian object file--
1553   // implemented by child class.
1554   virtual bool
1555   do_is_big_endian() const = 0;
1556 
1557  private:
1558   // Mapping from input sections to output section.
1559   Output_sections output_sections_;
1560   // Mapping from input section index to the information recorded for
1561   // the relocations.  This is only used for a relocatable link.
1562   std::vector<Relocatable_relocs*>* map_to_relocatable_relocs_;
1563   // Mappings for merge sections.  This is managed by the code in the
1564   // Merge_map class.
1565   Object_merge_map* object_merge_map_;
1566   // Whether we need to wait for output sections to be written before
1567   // we can apply relocations.
1568   bool relocs_must_follow_section_writes_;
1569   // Used to store the relocs data computed by the Read_relocs pass.
1570   // Used during garbage collection of unused sections.
1571   Read_relocs_data* rd_;
1572   // Used to store the symbols data computed by the Read_symbols pass.
1573   // Again used during garbage collection when laying out referenced
1574   // sections.
1575   gold::Symbols_data* sd_;
1576   // Per-symbol counts of relocations, for incremental links.
1577   unsigned int* reloc_counts_;
1578   // Per-symbol base indexes of relocations, for incremental links.
1579   unsigned int* reloc_bases_;
1580   // Index of the first dynamic relocation for this object.
1581   unsigned int first_dyn_reloc_;
1582   // Count of dynamic relocations for this object.
1583   unsigned int dyn_reloc_count_;
1584 };
1585 
1586 // This class is used to handle relocations against a section symbol
1587 // in an SHF_MERGE section.  For such a symbol, we need to know the
1588 // addend of the relocation before we can determine the final value.
1589 // The addend gives us the location in the input section, and we can
1590 // determine how it is mapped to the output section.  For a
1591 // non-section symbol, we apply the addend to the final value of the
1592 // symbol; that is done in finalize_local_symbols, and does not use
1593 // this class.
1594 
1595 template<int size>
1596 class Merged_symbol_value
1597 {
1598  public:
1599   typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
1600 
1601   // We use a hash table to map offsets in the input section to output
1602   // addresses.
1603   typedef Unordered_map<section_offset_type, Value> Output_addresses;
1604 
1605   Merged_symbol_value(Value input_value, Value output_start_address)
1606     : input_value_(input_value), output_start_address_(output_start_address),
1607       output_addresses_()
1608   { }
1609 
1610   // Initialize the hash table.
1611   void
1612   initialize_input_to_output_map(const Relobj*, unsigned int input_shndx);
1613 
1614   // Release the hash table to save space.
1615   void
1616   free_input_to_output_map()
1617   { this->output_addresses_.clear(); }
1618 
1619   // Get the output value corresponding to an addend.  The object and
1620   // input section index are passed in because the caller will have
1621   // them; otherwise we could store them here.
1622   Value
1623   value(const Relobj* object, unsigned int input_shndx, Value addend) const
1624   {
1625     // This is a relocation against a section symbol.  ADDEND is the
1626     // offset in the section.  The result should be the start of some
1627     // merge area.  If the object file wants something else, it should
1628     // use a regular symbol rather than a section symbol.
1629     // Unfortunately, PR 6658 shows a case in which the object file
1630     // refers to the section symbol, but uses a negative ADDEND to
1631     // compensate for a PC relative reloc.  We can't handle the
1632     // general case.  However, we can handle the special case of a
1633     // negative addend, by assuming that it refers to the start of the
1634     // section.  Of course, that means that we have to guess when
1635     // ADDEND is negative.  It is normal to see a 32-bit value here
1636     // even when the template parameter size is 64, as 64-bit object
1637     // file formats have 32-bit relocations.  We know this is a merge
1638     // section, so we know it has to fit into memory.  So we assume
1639     // that we won't see a value larger than a large 32-bit unsigned
1640     // value.  This will break objects with very very large merge
1641     // sections; they probably break in other ways anyhow.
1642     Value input_offset = this->input_value_;
1643     if (addend < 0xffffff00)
1644       {
1645 	input_offset += addend;
1646 	addend = 0;
1647       }
1648     typename Output_addresses::const_iterator p =
1649       this->output_addresses_.find(input_offset);
1650     if (p != this->output_addresses_.end())
1651       return p->second + addend;
1652 
1653     return (this->value_from_output_section(object, input_shndx, input_offset)
1654 	    + addend);
1655   }
1656 
1657  private:
1658   // Get the output value for an input offset if we couldn't find it
1659   // in the hash table.
1660   Value
1661   value_from_output_section(const Relobj*, unsigned int input_shndx,
1662 			    Value input_offset) const;
1663 
1664   // The value of the section symbol in the input file.  This is
1665   // normally zero, but could in principle be something else.
1666   Value input_value_;
1667   // The start address of this merged section in the output file.
1668   Value output_start_address_;
1669   // A hash table which maps offsets in the input section to output
1670   // addresses.  This only maps specific offsets, not all offsets.
1671   Output_addresses output_addresses_;
1672 };
1673 
1674 // This POD class is holds the value of a symbol.  This is used for
1675 // local symbols, and for all symbols during relocation processing.
1676 // For special sections, such as SHF_MERGE sections, this calls a
1677 // function to get the final symbol value.
1678 
1679 template<int size>
1680 class Symbol_value
1681 {
1682  public:
1683   typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
1684 
1685   Symbol_value()
1686     : output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0),
1687       is_ordinary_shndx_(false), is_section_symbol_(false),
1688       is_tls_symbol_(false), is_ifunc_symbol_(false), has_output_value_(true)
1689   { this->u_.value = 0; }
1690 
1691   ~Symbol_value()
1692   {
1693     if (!this->has_output_value_)
1694       delete this->u_.merged_symbol_value;
1695   }
1696 
1697   // Get the value of this symbol.  OBJECT is the object in which this
1698   // symbol is defined, and ADDEND is an addend to add to the value.
1699   template<bool big_endian>
1700   Value
1701   value(const Sized_relobj_file<size, big_endian>* object, Value addend) const
1702   {
1703     if (this->has_output_value_)
1704       return this->u_.value + addend;
1705     else
1706       {
1707 	gold_assert(this->is_ordinary_shndx_);
1708 	return this->u_.merged_symbol_value->value(object, this->input_shndx_,
1709 						   addend);
1710       }
1711   }
1712 
1713   // Set the value of this symbol in the output symbol table.
1714   void
1715   set_output_value(Value value)
1716   { this->u_.value = value; }
1717 
1718   // For a section symbol in a merged section, we need more
1719   // information.
1720   void
1721   set_merged_symbol_value(Merged_symbol_value<size>* msv)
1722   {
1723     gold_assert(this->is_section_symbol_);
1724     this->has_output_value_ = false;
1725     this->u_.merged_symbol_value = msv;
1726   }
1727 
1728   // Initialize the input to output map for a section symbol in a
1729   // merged section.  We also initialize the value of a non-section
1730   // symbol in a merged section.
1731   void
1732   initialize_input_to_output_map(const Relobj* object)
1733   {
1734     if (!this->has_output_value_)
1735       {
1736 	gold_assert(this->is_section_symbol_ && this->is_ordinary_shndx_);
1737 	Merged_symbol_value<size>* msv = this->u_.merged_symbol_value;
1738 	msv->initialize_input_to_output_map(object, this->input_shndx_);
1739       }
1740   }
1741 
1742   // Free the input to output map for a section symbol in a merged
1743   // section.
1744   void
1745   free_input_to_output_map()
1746   {
1747     if (!this->has_output_value_)
1748       this->u_.merged_symbol_value->free_input_to_output_map();
1749   }
1750 
1751   // Set the value of the symbol from the input file.  This is only
1752   // called by count_local_symbols, to communicate the value to
1753   // finalize_local_symbols.
1754   void
1755   set_input_value(Value value)
1756   { this->u_.value = value; }
1757 
1758   // Return the input value.  This is only called by
1759   // finalize_local_symbols and (in special cases) relocate_section.
1760   Value
1761   input_value() const
1762   { return this->u_.value; }
1763 
1764   // Return whether we have set the index in the output symbol table
1765   // yet.
1766   bool
1767   is_output_symtab_index_set() const
1768   {
1769     return (this->output_symtab_index_ != 0
1770 	    && this->output_symtab_index_ != -2U);
1771   }
1772 
1773   // Return whether this symbol may be discarded from the normal
1774   // symbol table.
1775   bool
1776   may_be_discarded_from_output_symtab() const
1777   {
1778     gold_assert(!this->is_output_symtab_index_set());
1779     return this->output_symtab_index_ != -2U;
1780   }
1781 
1782   // Return whether this symbol has an entry in the output symbol
1783   // table.
1784   bool
1785   has_output_symtab_entry() const
1786   {
1787     gold_assert(this->is_output_symtab_index_set());
1788     return this->output_symtab_index_ != -1U;
1789   }
1790 
1791   // Return the index in the output symbol table.
1792   unsigned int
1793   output_symtab_index() const
1794   {
1795     gold_assert(this->is_output_symtab_index_set()
1796 		&& this->output_symtab_index_ != -1U);
1797     return this->output_symtab_index_;
1798   }
1799 
1800   // Set the index in the output symbol table.
1801   void
1802   set_output_symtab_index(unsigned int i)
1803   {
1804     gold_assert(!this->is_output_symtab_index_set());
1805     gold_assert(i != 0 && i != -1U && i != -2U);
1806     this->output_symtab_index_ = i;
1807   }
1808 
1809   // Record that this symbol should not go into the output symbol
1810   // table.
1811   void
1812   set_no_output_symtab_entry()
1813   {
1814     gold_assert(this->output_symtab_index_ == 0);
1815     this->output_symtab_index_ = -1U;
1816   }
1817 
1818   // Record that this symbol must go into the output symbol table,
1819   // because it there is a relocation that uses it.
1820   void
1821   set_must_have_output_symtab_entry()
1822   {
1823     gold_assert(!this->is_output_symtab_index_set());
1824     this->output_symtab_index_ = -2U;
1825   }
1826 
1827   // Set the index in the output dynamic symbol table.
1828   void
1829   set_needs_output_dynsym_entry()
1830   {
1831     gold_assert(!this->is_section_symbol());
1832     this->output_dynsym_index_ = 0;
1833   }
1834 
1835   // Return whether this symbol should go into the dynamic symbol
1836   // table.
1837   bool
1838   needs_output_dynsym_entry() const
1839   {
1840     return this->output_dynsym_index_ != -1U;
1841   }
1842 
1843   // Return whether this symbol has an entry in the dynamic symbol
1844   // table.
1845   bool
1846   has_output_dynsym_entry() const
1847   {
1848     gold_assert(this->output_dynsym_index_ != 0);
1849     return this->output_dynsym_index_ != -1U;
1850   }
1851 
1852   // Record that this symbol should go into the dynamic symbol table.
1853   void
1854   set_output_dynsym_index(unsigned int i)
1855   {
1856     gold_assert(this->output_dynsym_index_ == 0);
1857     gold_assert(i != 0 && i != -1U);
1858     this->output_dynsym_index_ = i;
1859   }
1860 
1861   // Return the index in the output dynamic symbol table.
1862   unsigned int
1863   output_dynsym_index() const
1864   {
1865     gold_assert(this->output_dynsym_index_ != 0
1866                 && this->output_dynsym_index_ != -1U);
1867     return this->output_dynsym_index_;
1868   }
1869 
1870   // Set the index of the input section in the input file.
1871   void
1872   set_input_shndx(unsigned int i, bool is_ordinary)
1873   {
1874     this->input_shndx_ = i;
1875     // input_shndx_ field is a bitfield, so make sure that the value
1876     // fits.
1877     gold_assert(this->input_shndx_ == i);
1878     this->is_ordinary_shndx_ = is_ordinary;
1879   }
1880 
1881   // Return the index of the input section in the input file.
1882   unsigned int
1883   input_shndx(bool* is_ordinary) const
1884   {
1885     *is_ordinary = this->is_ordinary_shndx_;
1886     return this->input_shndx_;
1887   }
1888 
1889   // Whether this is a section symbol.
1890   bool
1891   is_section_symbol() const
1892   { return this->is_section_symbol_; }
1893 
1894   // Record that this is a section symbol.
1895   void
1896   set_is_section_symbol()
1897   {
1898     gold_assert(!this->needs_output_dynsym_entry());
1899     this->is_section_symbol_ = true;
1900   }
1901 
1902   // Record that this is a TLS symbol.
1903   void
1904   set_is_tls_symbol()
1905   { this->is_tls_symbol_ = true; }
1906 
1907   // Return true if this is a TLS symbol.
1908   bool
1909   is_tls_symbol() const
1910   { return this->is_tls_symbol_; }
1911 
1912   // Record that this is an IFUNC symbol.
1913   void
1914   set_is_ifunc_symbol()
1915   { this->is_ifunc_symbol_ = true; }
1916 
1917   // Return true if this is an IFUNC symbol.
1918   bool
1919   is_ifunc_symbol() const
1920   { return this->is_ifunc_symbol_; }
1921 
1922   // Return true if this has output value.
1923   bool
1924   has_output_value() const
1925   { return this->has_output_value_; }
1926 
1927  private:
1928   // The index of this local symbol in the output symbol table.  This
1929   // will be 0 if no value has been assigned yet, and the symbol may
1930   // be omitted.  This will be -1U if the symbol should not go into
1931   // the symbol table.  This will be -2U if the symbol must go into
1932   // the symbol table, but no index has been assigned yet.
1933   unsigned int output_symtab_index_;
1934   // The index of this local symbol in the dynamic symbol table.  This
1935   // will be -1U if the symbol should not go into the symbol table.
1936   unsigned int output_dynsym_index_;
1937   // The section index in the input file in which this symbol is
1938   // defined.
1939   unsigned int input_shndx_ : 27;
1940   // Whether the section index is an ordinary index, not a special
1941   // value.
1942   bool is_ordinary_shndx_ : 1;
1943   // Whether this is a STT_SECTION symbol.
1944   bool is_section_symbol_ : 1;
1945   // Whether this is a STT_TLS symbol.
1946   bool is_tls_symbol_ : 1;
1947   // Whether this is a STT_GNU_IFUNC symbol.
1948   bool is_ifunc_symbol_ : 1;
1949   // Whether this symbol has a value for the output file.  This is
1950   // normally set to true during Layout::finalize, by
1951   // finalize_local_symbols.  It will be false for a section symbol in
1952   // a merge section, as for such symbols we can not determine the
1953   // value to use in a relocation until we see the addend.
1954   bool has_output_value_ : 1;
1955   union
1956   {
1957     // This is used if has_output_value_ is true.  Between
1958     // count_local_symbols and finalize_local_symbols, this is the
1959     // value in the input file.  After finalize_local_symbols, it is
1960     // the value in the output file.
1961     Value value;
1962     // This is used if has_output_value_ is false.  It points to the
1963     // information we need to get the value for a merge section.
1964     Merged_symbol_value<size>* merged_symbol_value;
1965   } u_;
1966 };
1967 
1968 // This type is used to modify relocations for -fsplit-stack.  It is
1969 // indexed by relocation index, and means that the relocation at that
1970 // index should use the symbol from the vector, rather than the one
1971 // indicated by the relocation.
1972 
1973 class Reloc_symbol_changes
1974 {
1975  public:
1976   Reloc_symbol_changes(size_t count)
1977     : vec_(count, NULL)
1978   { }
1979 
1980   void
1981   set(size_t i, Symbol* sym)
1982   { this->vec_[i] = sym; }
1983 
1984   const Symbol*
1985   operator[](size_t i) const
1986   { return this->vec_[i]; }
1987 
1988  private:
1989   std::vector<Symbol*> vec_;
1990 };
1991 
1992 // Abstract base class for a regular object file, either a real object file
1993 // or an incremental (unchanged) object.  This is size and endian specific.
1994 
1995 template<int size, bool big_endian>
1996 class Sized_relobj : public Relobj
1997 {
1998  public:
1999   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2000   typedef Relobj::Symbols Symbols;
2001 
2002   static const Address invalid_address = static_cast<Address>(0) - 1;
2003 
2004   Sized_relobj(const std::string& name, Input_file* input_file)
2005     : Relobj(name, input_file), local_got_offsets_(), section_offsets_()
2006   { }
2007 
2008   Sized_relobj(const std::string& name, Input_file* input_file,
2009 		    off_t offset)
2010     : Relobj(name, input_file, offset), local_got_offsets_(), section_offsets_()
2011   { }
2012 
2013   ~Sized_relobj()
2014   { }
2015 
2016   // If this is a regular object, return a pointer to the Sized_relobj_file
2017   // object.  Otherwise, return NULL.
2018   virtual Sized_relobj_file<size, big_endian>*
2019   sized_relobj()
2020   { return NULL; }
2021 
2022   const virtual Sized_relobj_file<size, big_endian>*
2023   sized_relobj() const
2024   { return NULL; }
2025 
2026   // Checks if the offset of input section SHNDX within its output
2027   // section is invalid.
2028   bool
2029   is_output_section_offset_invalid(unsigned int shndx) const
2030   { return this->get_output_section_offset(shndx) == invalid_address; }
2031 
2032   // Get the offset of input section SHNDX within its output section.
2033   // This is -1 if the input section requires a special mapping, such
2034   // as a merge section.  The output section can be found in the
2035   // output_sections_ field of the parent class Relobj.
2036   Address
2037   get_output_section_offset(unsigned int shndx) const
2038   {
2039     gold_assert(shndx < this->section_offsets_.size());
2040     return this->section_offsets_[shndx];
2041   }
2042 
2043   // Iterate over local symbols, calling a visitor class V for each GOT offset
2044   // associated with a local symbol.
2045   void
2046   do_for_all_local_got_entries(Got_offset_list::Visitor* v) const;
2047 
2048  protected:
2049   typedef Relobj::Output_sections Output_sections;
2050 
2051   // Clear the local symbol information.
2052   void
2053   clear_got_offsets()
2054   { this->local_got_offsets_.clear(); }
2055 
2056   // Return the vector of section offsets.
2057   std::vector<Address>&
2058   section_offsets()
2059   { return this->section_offsets_; }
2060 
2061   // Get the address of an output section.
2062   uint64_t
2063   do_output_section_address(unsigned int shndx);
2064 
2065   // Get the offset of a section.
2066   uint64_t
2067   do_output_section_offset(unsigned int shndx) const
2068   {
2069     Address off = this->get_output_section_offset(shndx);
2070     if (off == invalid_address)
2071       return -1ULL;
2072     return off;
2073   }
2074 
2075   // Set the offset of a section.
2076   void
2077   do_set_section_offset(unsigned int shndx, uint64_t off)
2078   {
2079     gold_assert(shndx < this->section_offsets_.size());
2080     this->section_offsets_[shndx] =
2081       (off == static_cast<uint64_t>(-1)
2082        ? invalid_address
2083        : convert_types<Address, uint64_t>(off));
2084   }
2085 
2086   // Return whether the local symbol SYMNDX plus ADDEND has a GOT offset
2087   // of type GOT_TYPE.
2088   bool
2089   do_local_has_got_offset(unsigned int symndx, unsigned int got_type,
2090 			  uint64_t addend) const
2091   {
2092     Local_got_entry_key key(symndx, addend);
2093     Local_got_offsets::const_iterator p =
2094         this->local_got_offsets_.find(key);
2095     return (p != this->local_got_offsets_.end()
2096             && p->second->get_offset(got_type) != -1U);
2097   }
2098 
2099   // Return the GOT offset of type GOT_TYPE of the local symbol
2100   // SYMNDX plus ADDEND.
2101   unsigned int
2102   do_local_got_offset(unsigned int symndx, unsigned int got_type,
2103 			  uint64_t addend) const
2104   {
2105     Local_got_entry_key key(symndx, addend);
2106     Local_got_offsets::const_iterator p =
2107         this->local_got_offsets_.find(key);
2108     gold_assert(p != this->local_got_offsets_.end());
2109     unsigned int off = p->second->get_offset(got_type);
2110     gold_assert(off != -1U);
2111     return off;
2112   }
2113 
2114   // Set the GOT offset with type GOT_TYPE of the local symbol SYMNDX
2115   // plus ADDEND to GOT_OFFSET.
2116   void
2117   do_set_local_got_offset(unsigned int symndx, unsigned int got_type,
2118 			  unsigned int got_offset, uint64_t addend)
2119   {
2120     Local_got_entry_key key(symndx, addend);
2121     Local_got_offsets::const_iterator p =
2122         this->local_got_offsets_.find(key);
2123     if (p != this->local_got_offsets_.end())
2124       p->second->set_offset(got_type, got_offset);
2125     else
2126       {
2127         Got_offset_list* g = new Got_offset_list(got_type, got_offset);
2128         std::pair<Local_got_offsets::iterator, bool> ins =
2129             this->local_got_offsets_.insert(std::make_pair(key, g));
2130         gold_assert(ins.second);
2131       }
2132   }
2133 
2134   // Return the word size of the object file.
2135   virtual int
2136   do_elfsize() const
2137   { return size; }
2138 
2139   // Return TRUE if this is a big-endian object file.
2140   virtual bool
2141   do_is_big_endian() const
2142   { return big_endian; }
2143 
2144  private:
2145   // The GOT offsets of local symbols. This map also stores GOT offsets
2146   // for tp-relative offsets for TLS symbols.
2147   typedef Unordered_map<Local_got_entry_key, Got_offset_list*,
2148                         Local_got_entry_key::hash,
2149                         Local_got_entry_key::equal_to> Local_got_offsets;
2150 
2151   // GOT offsets for local non-TLS symbols, and tp-relative offsets
2152   // for TLS symbols, indexed by local got entry key class.
2153   Local_got_offsets local_got_offsets_;
2154   // For each input section, the offset of the input section in its
2155   // output section.  This is INVALID_ADDRESS if the input section requires a
2156   // special mapping.
2157   std::vector<Address> section_offsets_;
2158 };
2159 
2160 // A regular object file.  This is size and endian specific.
2161 
2162 template<int size, bool big_endian>
2163 class Sized_relobj_file : public Sized_relobj<size, big_endian>
2164 {
2165  public:
2166   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2167   typedef typename Sized_relobj<size, big_endian>::Symbols Symbols;
2168   typedef std::vector<Symbol_value<size> > Local_values;
2169 
2170   static const Address invalid_address = static_cast<Address>(0) - 1;
2171 
2172   enum Compute_final_local_value_status
2173   {
2174     // No error.
2175     CFLV_OK,
2176     // An error occurred.
2177     CFLV_ERROR,
2178     // The local symbol has no output section.
2179     CFLV_DISCARDED
2180   };
2181 
2182   Sized_relobj_file(const std::string& name,
2183 		    Input_file* input_file,
2184 		    off_t offset,
2185 		    const typename elfcpp::Ehdr<size, big_endian>&);
2186 
2187   ~Sized_relobj_file();
2188 
2189   // Set up the object file based on TARGET.
2190   void
2191   setup()
2192   { this->do_setup(); }
2193 
2194   // Return a pointer to the Sized_relobj_file object.
2195   Sized_relobj_file<size, big_endian>*
2196   sized_relobj()
2197   { return this; }
2198 
2199   const Sized_relobj_file<size, big_endian>*
2200   sized_relobj() const
2201   { return this; }
2202 
2203   // Return the ELF file type.
2204   int
2205   e_type() const
2206   { return this->e_type_; }
2207 
2208   // Return the number of symbols.  This is only valid after
2209   // Object::add_symbols has been called.
2210   unsigned int
2211   symbol_count() const
2212   { return this->local_symbol_count_ + this->symbols_.size(); }
2213 
2214   // If SYM is the index of a global symbol in the object file's
2215   // symbol table, return the Symbol object.  Otherwise, return NULL.
2216   Symbol*
2217   global_symbol(unsigned int sym) const
2218   {
2219     if (sym >= this->local_symbol_count_)
2220       {
2221 	gold_assert(sym - this->local_symbol_count_ < this->symbols_.size());
2222 	return this->symbols_[sym - this->local_symbol_count_];
2223       }
2224     return NULL;
2225   }
2226 
2227   // Return the section index of symbol SYM.  Set *VALUE to its value
2228   // in the object file.  Set *IS_ORDINARY if this is an ordinary
2229   // section index, not a special code between SHN_LORESERVE and
2230   // SHN_HIRESERVE.  Note that for a symbol which is not defined in
2231   // this object file, this will set *VALUE to 0 and return SHN_UNDEF;
2232   // it will not return the final value of the symbol in the link.
2233   unsigned int
2234   symbol_section_and_value(unsigned int sym, Address* value, bool* is_ordinary);
2235 
2236   // Return a pointer to the Symbol_value structure which holds the
2237   // value of a local symbol.
2238   const Symbol_value<size>*
2239   local_symbol(unsigned int sym) const
2240   {
2241     gold_assert(sym < this->local_values_.size());
2242     return &this->local_values_[sym];
2243   }
2244 
2245   // Return the index of local symbol SYM in the ordinary symbol
2246   // table.  A value of -1U means that the symbol is not being output.
2247   unsigned int
2248   symtab_index(unsigned int sym) const
2249   {
2250     gold_assert(sym < this->local_values_.size());
2251     return this->local_values_[sym].output_symtab_index();
2252   }
2253 
2254   // Return the index of local symbol SYM in the dynamic symbol
2255   // table.  A value of -1U means that the symbol is not being output.
2256   unsigned int
2257   dynsym_index(unsigned int sym) const
2258   {
2259     gold_assert(sym < this->local_values_.size());
2260     return this->local_values_[sym].output_dynsym_index();
2261   }
2262 
2263   // Return the input section index of local symbol SYM.
2264   unsigned int
2265   local_symbol_input_shndx(unsigned int sym, bool* is_ordinary) const
2266   {
2267     gold_assert(sym < this->local_values_.size());
2268     return this->local_values_[sym].input_shndx(is_ordinary);
2269   }
2270 
2271   // Record that local symbol SYM must be in the output symbol table.
2272   void
2273   set_must_have_output_symtab_entry(unsigned int sym)
2274   {
2275     gold_assert(sym < this->local_values_.size());
2276     this->local_values_[sym].set_must_have_output_symtab_entry();
2277   }
2278 
2279   // Record that local symbol SYM needs a dynamic symbol entry.
2280   void
2281   set_needs_output_dynsym_entry(unsigned int sym)
2282   {
2283     gold_assert(sym < this->local_values_.size());
2284     this->local_values_[sym].set_needs_output_dynsym_entry();
2285   }
2286 
2287   // Return whether the local symbol SYMNDX has a PLT offset.
2288   bool
2289   local_has_plt_offset(unsigned int symndx) const;
2290 
2291   // Set the PLT offset of the local symbol SYMNDX.
2292   void
2293   set_local_plt_offset(unsigned int symndx, unsigned int plt_offset);
2294 
2295   // Adjust this local symbol value.  Return false if the symbol
2296   // should be discarded from the output file.
2297   bool
2298   adjust_local_symbol(Symbol_value<size>* lv) const
2299   { return this->do_adjust_local_symbol(lv); }
2300 
2301   // Return the name of the symbol that spans the given offset in the
2302   // specified section in this object.  This is used only for error
2303   // messages and is not particularly efficient.
2304   bool
2305   get_symbol_location_info(unsigned int shndx, off_t offset,
2306 			   Symbol_location_info* info);
2307 
2308   // Look for a kept section corresponding to the given discarded section,
2309   // and return its output address.  This is used only for relocations in
2310   // debugging sections.
2311   Address
2312   map_to_kept_section(unsigned int shndx, std::string& section_name,
2313 		      bool* found) const;
2314 
2315   // Look for a kept section corresponding to the given discarded section,
2316   // and return its object file.
2317   Relobj*
2318   find_kept_section_object(unsigned int shndx, unsigned int* symndx_p) const;
2319 
2320   // Return the name of symbol SYMNDX.
2321   const char*
2322   get_symbol_name(unsigned int symndx);
2323 
2324   // Compute final local symbol value.  R_SYM is the local symbol index.
2325   // LV_IN points to a local symbol value containing the input value.
2326   // LV_OUT points to a local symbol value storing the final output value,
2327   // which must not be a merged symbol value since before calling this
2328   // method to avoid memory leak.  SYMTAB points to a symbol table.
2329   //
2330   // The method returns a status code at return.  If the return status is
2331   // CFLV_OK, *LV_OUT contains the final value.  If the return status is
2332   // CFLV_ERROR, *LV_OUT is 0.  If the return status is CFLV_DISCARDED,
2333   // *LV_OUT is not modified.
2334   Compute_final_local_value_status
2335   compute_final_local_value(unsigned int r_sym,
2336 			    const Symbol_value<size>* lv_in,
2337 			    Symbol_value<size>* lv_out,
2338 			    const Symbol_table* symtab);
2339 
2340   // Return true if the layout for this object was deferred.
2341   bool is_deferred_layout() const
2342   { return this->is_deferred_layout_; }
2343 
2344  protected:
2345   typedef typename Sized_relobj<size, big_endian>::Output_sections
2346       Output_sections;
2347 
2348   // Set up.
2349   virtual void
2350   do_setup();
2351 
2352   // Read the symbols.
2353   void
2354   do_read_symbols(Read_symbols_data*);
2355 
2356   // Read the symbols.  This is common code for all target-specific
2357   // overrides of do_read_symbols.
2358   void
2359   base_read_symbols(Read_symbols_data*);
2360 
2361   // Return the value of a local symbol.
2362   uint64_t
2363   do_local_symbol_value(unsigned int symndx, uint64_t addend) const
2364   {
2365     const Symbol_value<size>* symval = this->local_symbol(symndx);
2366     return symval->value(this, addend);
2367   }
2368 
2369   // Return the PLT offset for a local symbol.  It is an error to call
2370   // this if it doesn't have one.
2371   unsigned int
2372   do_local_plt_offset(unsigned int symndx) const;
2373 
2374   // Return whether local symbol SYMNDX is a TLS symbol.
2375   bool
2376   do_local_is_tls(unsigned int symndx) const
2377   { return this->local_symbol(symndx)->is_tls_symbol(); }
2378 
2379   // Return the number of local symbols.
2380   unsigned int
2381   do_local_symbol_count() const
2382   { return this->local_symbol_count_; }
2383 
2384   // Return the number of local symbols in the output symbol table.
2385   unsigned int
2386   do_output_local_symbol_count() const
2387   { return this->output_local_symbol_count_; }
2388 
2389   // Return the number of local symbols in the output symbol table.
2390   off_t
2391   do_local_symbol_offset() const
2392   { return this->local_symbol_offset_; }
2393 
2394   // Lay out the input sections.
2395   void
2396   do_layout(Symbol_table*, Layout*, Read_symbols_data*);
2397 
2398   // Layout sections whose layout was deferred while waiting for
2399   // input files from a plugin.
2400   void
2401   do_layout_deferred_sections(Layout*);
2402 
2403   // Add the symbols to the symbol table.
2404   void
2405   do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
2406 
2407   Archive::Should_include
2408   do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
2409                            std::string* why);
2410 
2411   // Iterate over global symbols, calling a visitor class V for each.
2412   void
2413   do_for_all_global_symbols(Read_symbols_data* sd,
2414 			    Library_base::Symbol_visitor_base* v);
2415 
2416   // Read the relocs.
2417   void
2418   do_read_relocs(Read_relocs_data*);
2419 
2420   // Process the relocs to find list of referenced sections. Used only
2421   // during garbage collection.
2422   void
2423   do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
2424 
2425   // Scan the relocs and adjust the symbol table.
2426   void
2427   do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*);
2428 
2429   // Count the local symbols.
2430   void
2431   do_count_local_symbols(Stringpool_template<char>*,
2432                             Stringpool_template<char>*);
2433 
2434   // Finalize the local symbols.
2435   unsigned int
2436   do_finalize_local_symbols(unsigned int, off_t, Symbol_table*);
2437 
2438   // Set the offset where local dynamic symbol information will be stored.
2439   unsigned int
2440   do_set_local_dynsym_indexes(unsigned int);
2441 
2442   // Set the offset where local dynamic symbol information will be stored.
2443   unsigned int
2444   do_set_local_dynsym_offset(off_t);
2445 
2446   // Relocate the input sections and write out the local symbols.
2447   void
2448   do_relocate(const Symbol_table* symtab, const Layout*, Output_file* of);
2449 
2450   // Get the size of a section.
2451   uint64_t
2452   do_section_size(unsigned int shndx)
2453   { return this->elf_file_.section_size(shndx); }
2454 
2455   // Get the name of a section.
2456   std::string
2457   do_section_name(unsigned int shndx) const
2458   { return this->elf_file_.section_name(shndx); }
2459 
2460   // Return the location of the contents of a section.
2461   const unsigned char*
2462   do_section_contents(unsigned int shndx, section_size_type* plen,
2463 		      bool cache)
2464   {
2465     Object::Location loc(this->elf_file_.section_contents(shndx));
2466     *plen = convert_to_section_size_type(loc.data_size);
2467     if (*plen == 0)
2468       {
2469 	static const unsigned char empty[1] = { '\0' };
2470 	return empty;
2471       }
2472     return this->get_view(loc.file_offset, *plen, true, cache);
2473   }
2474 
2475   // Return section flags.
2476   uint64_t
2477   do_section_flags(unsigned int shndx);
2478 
2479   // Return section entsize.
2480   uint64_t
2481   do_section_entsize(unsigned int shndx);
2482 
2483   // Return section address.
2484   uint64_t
2485   do_section_address(unsigned int shndx)
2486   { return this->elf_file_.section_addr(shndx); }
2487 
2488   // Return section type.
2489   unsigned int
2490   do_section_type(unsigned int shndx)
2491   { return this->elf_file_.section_type(shndx); }
2492 
2493   // Return the section link field.
2494   unsigned int
2495   do_section_link(unsigned int shndx)
2496   { return this->elf_file_.section_link(shndx); }
2497 
2498   // Return the section info field.
2499   unsigned int
2500   do_section_info(unsigned int shndx)
2501   { return this->elf_file_.section_info(shndx); }
2502 
2503   // Return the section alignment.
2504   uint64_t
2505   do_section_addralign(unsigned int shndx)
2506   { return this->elf_file_.section_addralign(shndx); }
2507 
2508   // Return the Xindex structure to use.
2509   Xindex*
2510   do_initialize_xindex();
2511 
2512   // Get symbol counts.
2513   void
2514   do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
2515 
2516   // Get the global symbols.
2517   const Symbols*
2518   do_get_global_symbols() const
2519   { return &this->symbols_; }
2520 
2521   // Adjust a section index if necessary.
2522   unsigned int
2523   adjust_shndx(unsigned int shndx)
2524   {
2525     if (shndx >= elfcpp::SHN_LORESERVE)
2526       shndx += this->elf_file_.large_shndx_offset();
2527     return shndx;
2528   }
2529 
2530   // Initialize input to output maps for section symbols in merged
2531   // sections.
2532   void
2533   initialize_input_to_output_maps();
2534 
2535   // Free the input to output maps for section symbols in merged
2536   // sections.
2537   void
2538   free_input_to_output_maps();
2539 
2540   // Return symbol table section index.
2541   unsigned int
2542   symtab_shndx() const
2543   { return this->symtab_shndx_; }
2544 
2545   // Allow a child class to access the ELF file.
2546   elfcpp::Elf_file<size, big_endian, Object>*
2547   elf_file()
2548   { return &this->elf_file_; }
2549 
2550   // Allow a child class to access the local values.
2551   Local_values*
2552   local_values()
2553   { return &this->local_values_; }
2554 
2555   // Views and sizes when relocating.
2556   struct View_size
2557   {
2558     unsigned char* view;
2559     typename elfcpp::Elf_types<size>::Elf_Addr address;
2560     off_t offset;
2561     section_size_type view_size;
2562     bool is_input_output_view;
2563     bool is_postprocessing_view;
2564     bool is_ctors_reverse_view;
2565   };
2566 
2567   typedef std::vector<View_size> Views;
2568 
2569   // Stash away info for a number of special sections.
2570   // Return true if any of the sections found require local symbols to be read.
2571   virtual bool
2572   do_find_special_sections(Read_symbols_data* sd);
2573 
2574   // This may be overriden by a child class.
2575   virtual void
2576   do_relocate_sections(const Symbol_table* symtab, const Layout* layout,
2577 		       const unsigned char* pshdrs, Output_file* of,
2578 		       Views* pviews);
2579 
2580   // Relocate section data for a range of sections.
2581   void
2582   relocate_section_range(const Symbol_table* symtab, const Layout* layout,
2583 			 const unsigned char* pshdrs, Output_file* of,
2584 			 Views* pviews, unsigned int start_shndx,
2585 			 unsigned int end_shndx);
2586 
2587   // Adjust this local symbol value.  Return false if the symbol
2588   // should be discarded from the output file.
2589   virtual bool
2590   do_adjust_local_symbol(Symbol_value<size>*) const
2591   { return true; }
2592 
2593   // Allow a child to set output local symbol count.
2594   void
2595   set_output_local_symbol_count(unsigned int value)
2596   { this->output_local_symbol_count_ = value; }
2597 
2598   // Return the output view for a section.
2599   unsigned char*
2600   do_get_output_view(unsigned int, section_size_type*) const;
2601 
2602  private:
2603   // For convenience.
2604   typedef Sized_relobj_file<size, big_endian> This;
2605   static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
2606   static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2607   static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2608   typedef elfcpp::Shdr<size, big_endian> Shdr;
2609   typedef elfcpp::Shdr_write<size, big_endian> Shdr_write;
2610 
2611   // To keep track of discarded comdat sections, we need to map a member
2612   // section index to the object and section index of the corresponding
2613   // kept section.
2614   struct Kept_comdat_section
2615   {
2616     Kept_comdat_section(uint64_t a_sh_size, Kept_section* a_kept_section,
2617 			unsigned int a_symndx, bool a_is_comdat)
2618       : sh_size(a_sh_size), kept_section(a_kept_section),
2619 	symndx (a_symndx), is_comdat(a_is_comdat)
2620     { }
2621     uint64_t sh_size;		// Section size
2622     Kept_section* kept_section;	// Kept section info
2623     unsigned int symndx;	// Index of key symbol
2624     bool is_comdat;		// True if comdat group, false if linkonce
2625   };
2626   typedef std::map<unsigned int, Kept_comdat_section>
2627       Kept_comdat_section_table;
2628 
2629   // Find the SHT_SYMTAB section, given the section headers.
2630   void
2631   find_symtab(const unsigned char* pshdrs);
2632 
2633   // Return whether SHDR has the right flags for a GNU style exception
2634   // frame section.
2635   bool
2636   check_eh_frame_flags(const elfcpp::Shdr<size, big_endian>* shdr) const;
2637 
2638   // Return whether there is a section named .eh_frame which might be
2639   // a GNU style exception frame section.
2640   bool
2641   find_eh_frame(const unsigned char* pshdrs, const char* names,
2642 		section_size_type names_size) const;
2643 
2644   // Whether to include a section group in the link.
2645   bool
2646   include_section_group(Symbol_table*, Layout*, unsigned int, const char*,
2647 			const unsigned char*, const char*, section_size_type,
2648 			std::vector<bool>*);
2649 
2650   // Whether to include a linkonce section in the link.
2651   bool
2652   include_linkonce_section(Layout*, unsigned int, const char*,
2653 			   const elfcpp::Shdr<size, big_endian>&);
2654 
2655   // Layout an input section.
2656   void
2657   layout_section(Layout* layout, unsigned int shndx, const char* name,
2658                  const typename This::Shdr& shdr, unsigned int sh_type,
2659                  unsigned int reloc_shndx, unsigned int reloc_type);
2660 
2661   // Layout an input .eh_frame section.
2662   void
2663   layout_eh_frame_section(Layout* layout, const unsigned char* symbols_data,
2664 			  section_size_type symbols_size,
2665 			  const unsigned char* symbol_names_data,
2666 			  section_size_type symbol_names_size,
2667 			  unsigned int shndx, const typename This::Shdr&,
2668 			  unsigned int reloc_shndx, unsigned int reloc_type);
2669 
2670   // Layout an input .note.gnu.property section.
2671   void
2672   layout_gnu_property_section(Layout* layout, unsigned int shndx);
2673 
2674   // Write section data to the output file.  Record the views and
2675   // sizes in VIEWS for use when relocating.
2676   void
2677   write_sections(const Layout*, const unsigned char* pshdrs, Output_file*,
2678 		 Views*);
2679 
2680   // Relocate the sections in the output file.
2681   void
2682   relocate_sections(const Symbol_table* symtab, const Layout* layout,
2683 		    const unsigned char* pshdrs, Output_file* of,
2684 		    Views* pviews)
2685   { this->do_relocate_sections(symtab, layout, pshdrs, of, pviews); }
2686 
2687   // Reverse the words in a section.  Used for .ctors sections mapped
2688   // to .init_array sections.
2689   void
2690   reverse_words(unsigned char*, section_size_type);
2691 
2692   // Scan the input relocations for --emit-relocs.
2693   void
2694   emit_relocs_scan(Symbol_table*, Layout*, const unsigned char* plocal_syms,
2695 		   const Read_relocs_data::Relocs_list::iterator&);
2696 
2697   // Scan the input relocations for --emit-relocs, templatized on the
2698   // type of the relocation section.
2699   template<int sh_type>
2700   void
2701   emit_relocs_scan_reltype(Symbol_table*, Layout*,
2702 			   const unsigned char* plocal_syms,
2703 			   const Read_relocs_data::Relocs_list::iterator&,
2704 			   Relocatable_relocs*);
2705 
2706   // Scan the input relocations for --incremental.
2707   void
2708   incremental_relocs_scan(const Read_relocs_data::Relocs_list::iterator&);
2709 
2710   // Scan the input relocations for --incremental, templatized on the
2711   // type of the relocation section.
2712   template<int sh_type>
2713   void
2714   incremental_relocs_scan_reltype(
2715       const Read_relocs_data::Relocs_list::iterator&);
2716 
2717   void
2718   incremental_relocs_write(const Relocate_info<size, big_endian>*,
2719 			   unsigned int sh_type,
2720 			   const unsigned char* prelocs,
2721 			   size_t reloc_count,
2722 			   Output_section*,
2723 			   Address output_offset,
2724 			   Output_file*);
2725 
2726   template<int sh_type>
2727   void
2728   incremental_relocs_write_reltype(const Relocate_info<size, big_endian>*,
2729 				   const unsigned char* prelocs,
2730 				   size_t reloc_count,
2731 				   Output_section*,
2732 				   Address output_offset,
2733 				   Output_file*);
2734 
2735   // A type shared by split_stack_adjust_reltype and find_functions.
2736   typedef std::map<section_offset_type, section_size_type> Function_offsets;
2737 
2738   // Check for -fsplit-stack routines calling non-split-stack routines.
2739   void
2740   split_stack_adjust(const Symbol_table*, const unsigned char* pshdrs,
2741 		     unsigned int sh_type, unsigned int shndx,
2742 		     const unsigned char* prelocs, size_t reloc_count,
2743 		     unsigned char* view, section_size_type view_size,
2744 		     Reloc_symbol_changes** reloc_map,
2745 		     const Sized_target<size, big_endian>* target);
2746 
2747   template<int sh_type>
2748   void
2749   split_stack_adjust_reltype(const Symbol_table*, const unsigned char* pshdrs,
2750 			     unsigned int shndx, const unsigned char* prelocs,
2751 			     size_t reloc_count, unsigned char* view,
2752 			     section_size_type view_size,
2753 			     Reloc_symbol_changes** reloc_map,
2754 			     const Sized_target<size, big_endian>* target);
2755 
2756   // Find all functions in a section.
2757   void
2758   find_functions(const unsigned char* pshdrs, unsigned int shndx,
2759 		 Function_offsets*);
2760 
2761   // Write out the local symbols.
2762   void
2763   write_local_symbols(Output_file*,
2764 		      const Stringpool_template<char>*,
2765 		      const Stringpool_template<char>*,
2766 		      Output_symtab_xindex*,
2767 		      Output_symtab_xindex*,
2768 		      off_t);
2769 
2770   // Record a mapping from discarded section SHNDX to the corresponding
2771   // kept section.
2772   void
2773   set_kept_comdat_section(unsigned int shndx, bool is_comdat,
2774 			  unsigned int symndx, uint64_t sh_size,
2775 			  Kept_section* kept_section)
2776   {
2777     Kept_comdat_section kept(sh_size, kept_section, symndx, is_comdat);
2778     this->kept_comdat_sections_.insert(std::make_pair(shndx, kept));
2779   }
2780 
2781   // Find the kept section corresponding to the discarded section
2782   // SHNDX.  Return true if found.
2783   bool
2784   get_kept_comdat_section(unsigned int shndx, bool* is_comdat,
2785 			  unsigned int *symndx, uint64_t* sh_size,
2786 			  Kept_section** kept_section) const
2787   {
2788     typename Kept_comdat_section_table::const_iterator p =
2789       this->kept_comdat_sections_.find(shndx);
2790     if (p == this->kept_comdat_sections_.end())
2791       return false;
2792     *is_comdat = p->second.is_comdat;
2793     *symndx = p->second.symndx;
2794     *sh_size = p->second.sh_size;
2795     *kept_section = p->second.kept_section;
2796     return true;
2797   }
2798 
2799   // Compute final local symbol value.  R_SYM is the local symbol index.
2800   // LV_IN points to a local symbol value containing the input value.
2801   // LV_OUT points to a local symbol value storing the final output value,
2802   // which must not be a merged symbol value since before calling this
2803   // method to avoid memory leak.  RELOCATABLE indicates whether we are
2804   // linking a relocatable output.  OUT_SECTIONS is an array of output
2805   // sections.  OUT_OFFSETS is an array of offsets of the sections.  SYMTAB
2806   // points to a symbol table.
2807   //
2808   // The method returns a status code at return.  If the return status is
2809   // CFLV_OK, *LV_OUT contains the final value.  If the return status is
2810   // CFLV_ERROR, *LV_OUT is 0.  If the return status is CFLV_DISCARDED,
2811   // *LV_OUT is not modified.
2812   inline Compute_final_local_value_status
2813   compute_final_local_value_internal(unsigned int r_sym,
2814 				     const Symbol_value<size>* lv_in,
2815 				     Symbol_value<size>* lv_out,
2816 				     bool relocatable,
2817 				     const Output_sections& out_sections,
2818 				     const std::vector<Address>& out_offsets,
2819 				     const Symbol_table* symtab);
2820 
2821   // The PLT offsets of local symbols.
2822   typedef Unordered_map<unsigned int, unsigned int> Local_plt_offsets;
2823 
2824   // Saved information for sections whose layout was deferred.
2825   struct Deferred_layout
2826   {
2827     static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2828     Deferred_layout(unsigned int shndx, const char* name,
2829                     unsigned int sh_type,
2830                     const unsigned char* pshdr,
2831                     unsigned int reloc_shndx, unsigned int reloc_type)
2832       : name_(name), shndx_(shndx), reloc_shndx_(reloc_shndx),
2833         reloc_type_(reloc_type)
2834     {
2835       typename This::Shdr_write shdr(this->shdr_data_);
2836       memcpy(this->shdr_data_, pshdr, shdr_size);
2837       shdr.put_sh_type(sh_type);
2838     }
2839     std::string name_;
2840     unsigned int shndx_;
2841     unsigned int reloc_shndx_;
2842     unsigned int reloc_type_;
2843     unsigned char shdr_data_[shdr_size];
2844   };
2845 
2846   // General access to the ELF file.
2847   elfcpp::Elf_file<size, big_endian, Object> elf_file_;
2848   // Type of ELF file (ET_REL or ET_EXEC).  ET_EXEC files are allowed
2849   // as input files only for the --just-symbols option.
2850   int e_type_;
2851   // Index of SHT_SYMTAB section.
2852   unsigned int symtab_shndx_;
2853   // The number of local symbols.
2854   unsigned int local_symbol_count_;
2855   // The number of local symbols which go into the output file.
2856   unsigned int output_local_symbol_count_;
2857   // The number of local symbols which go into the output file's dynamic
2858   // symbol table.
2859   unsigned int output_local_dynsym_count_;
2860   // The entries in the symbol table for the external symbols.
2861   Symbols symbols_;
2862   // Number of symbols defined in object file itself.
2863   size_t defined_count_;
2864   // File offset for local symbols (relative to start of symbol table).
2865   off_t local_symbol_offset_;
2866   // File offset for local dynamic symbols (absolute).
2867   off_t local_dynsym_offset_;
2868   // Values of local symbols.
2869   Local_values local_values_;
2870   // PLT offsets for local symbols.
2871   Local_plt_offsets local_plt_offsets_;
2872   // Table mapping discarded comdat sections to corresponding kept sections.
2873   Kept_comdat_section_table kept_comdat_sections_;
2874   // Whether this object has a GNU style .eh_frame section.
2875   bool has_eh_frame_;
2876   // True if the layout of this object was deferred, waiting for plugin
2877   // replacement files.
2878   bool is_deferred_layout_;
2879   // The list of sections whose layout was deferred.
2880   std::vector<Deferred_layout> deferred_layout_;
2881   // The list of relocation sections whose layout was deferred.
2882   std::vector<Deferred_layout> deferred_layout_relocs_;
2883   // Pointer to the list of output views; valid only during do_relocate().
2884   const Views* output_views_;
2885 };
2886 
2887 // A class to manage the list of all objects.
2888 
2889 class Input_objects
2890 {
2891  public:
2892   Input_objects()
2893     : relobj_list_(), dynobj_list_(), sonames_(), cref_(NULL)
2894   { }
2895 
2896   // The type of the list of input relocateable objects.
2897   typedef std::vector<Relobj*> Relobj_list;
2898   typedef Relobj_list::const_iterator Relobj_iterator;
2899 
2900   // The type of the list of input dynamic objects.
2901   typedef std::vector<Dynobj*> Dynobj_list;
2902   typedef Dynobj_list::const_iterator Dynobj_iterator;
2903 
2904   // Add an object to the list.  Return true if all is well, or false
2905   // if this object should be ignored.
2906   bool
2907   add_object(Object*);
2908 
2909   // Start processing an archive.
2910   void
2911   archive_start(Archive*);
2912 
2913   // Stop processing an archive.
2914   void
2915   archive_stop(Archive*);
2916 
2917   // For each dynamic object, check whether we've seen all of its
2918   // explicit dependencies.
2919   void
2920   check_dynamic_dependencies() const;
2921 
2922   // Return whether an object was found in the system library
2923   // directory.
2924   bool
2925   found_in_system_library_directory(const Object*) const;
2926 
2927   // Print symbol counts.
2928   void
2929   print_symbol_counts(const Symbol_table*) const;
2930 
2931   // Print a cross reference table.
2932   void
2933   print_cref(const Symbol_table*, FILE*) const;
2934 
2935   // Iterate over all regular objects.
2936 
2937   Relobj_iterator
2938   relobj_begin() const
2939   { return this->relobj_list_.begin(); }
2940 
2941   Relobj_iterator
2942   relobj_end() const
2943   { return this->relobj_list_.end(); }
2944 
2945   // Iterate over all dynamic objects.
2946 
2947   Dynobj_iterator
2948   dynobj_begin() const
2949   { return this->dynobj_list_.begin(); }
2950 
2951   Dynobj_iterator
2952   dynobj_end() const
2953   { return this->dynobj_list_.end(); }
2954 
2955   // Return whether we have seen any dynamic objects.
2956   bool
2957   any_dynamic() const
2958   { return !this->dynobj_list_.empty(); }
2959 
2960   // Return the number of non dynamic objects.
2961   int
2962   number_of_relobjs() const
2963   { return this->relobj_list_.size(); }
2964 
2965   // Return the number of input objects.
2966   int
2967   number_of_input_objects() const
2968   { return this->relobj_list_.size() + this->dynobj_list_.size(); }
2969 
2970  private:
2971   Input_objects(const Input_objects&);
2972   Input_objects& operator=(const Input_objects&);
2973 
2974   // The list of ordinary objects included in the link.
2975   Relobj_list relobj_list_;
2976   // The list of dynamic objects included in the link.
2977   Dynobj_list dynobj_list_;
2978   // SONAMEs that we have seen.
2979   Unordered_map<std::string, Object*> sonames_;
2980   // Manage cross-references if requested.
2981   Cref* cref_;
2982 };
2983 
2984 // Some of the information we pass to the relocation routines.  We
2985 // group this together to avoid passing a dozen different arguments.
2986 
2987 template<int size, bool big_endian>
2988 struct Relocate_info
2989 {
2990   // Symbol table.
2991   const Symbol_table* symtab;
2992   // Layout.
2993   const Layout* layout;
2994   // Object being relocated.
2995   Sized_relobj_file<size, big_endian>* object;
2996   // Section index of relocation section.
2997   unsigned int reloc_shndx;
2998   // Section header of relocation section.
2999   const unsigned char* reloc_shdr;
3000   // Info about how relocs should be handled
3001   Relocatable_relocs* rr;
3002   // Section index of section being relocated.
3003   unsigned int data_shndx;
3004   // Section header of data section.
3005   const unsigned char* data_shdr;
3006 
3007   // Return a string showing the location of a relocation.  This is
3008   // only used for error messages.
3009   std::string
3010   location(size_t relnum, off_t reloffset) const;
3011 };
3012 
3013 // This is used to represent a section in an object and is used as the
3014 // key type for various section maps.
3015 typedef std::pair<Relobj*, unsigned int> Section_id;
3016 
3017 // This is similar to Section_id but is used when the section
3018 // pointers are const.
3019 typedef std::pair<const Relobj*, unsigned int> Const_section_id;
3020 
3021 // The hash value is based on the address of an object in memory during
3022 // linking.  It is okay to use this for looking up sections but never use
3023 // this in an unordered container that we want to traverse in a repeatable
3024 // manner.
3025 
3026 struct Section_id_hash
3027 {
3028   size_t operator()(const Section_id& loc) const
3029   { return reinterpret_cast<uintptr_t>(loc.first) ^ loc.second; }
3030 };
3031 
3032 struct Const_section_id_hash
3033 {
3034   size_t operator()(const Const_section_id& loc) const
3035   { return reinterpret_cast<uintptr_t>(loc.first) ^ loc.second; }
3036 };
3037 
3038 // Return whether INPUT_FILE contains an ELF object start at file
3039 // offset OFFSET.  This sets *START to point to a view of the start of
3040 // the file.  It sets *READ_SIZE to the number of bytes in the view.
3041 
3042 extern bool
3043 is_elf_object(Input_file* input_file, off_t offset,
3044 	      const unsigned char** start, int* read_size);
3045 
3046 // Return an Object appropriate for the input file.  P is BYTES long,
3047 // and holds the ELF header.  If PUNCONFIGURED is not NULL, then if
3048 // this sees an object the linker is not configured to support, it
3049 // sets *PUNCONFIGURED to true and returns NULL without giving an
3050 // error message.
3051 
3052 extern Object*
3053 make_elf_object(const std::string& name, Input_file*,
3054 		off_t offset, const unsigned char* p,
3055 		section_offset_type bytes, bool* punconfigured);
3056 
3057 } // end namespace gold
3058 
3059 #endif // !defined(GOLD_OBJECT_H)
3060