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