1 // target-reloc.h -- target specific relocation support  -*- C++ -*-
2 
3 // Copyright (C) 2006-2021 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_TARGET_RELOC_H
24 #define GOLD_TARGET_RELOC_H
25 
26 #include "elfcpp.h"
27 #include "symtab.h"
28 #include "object.h"
29 #include "reloc.h"
30 #include "reloc-types.h"
31 
32 namespace gold
33 {
34 
35 // This function implements the generic part of reloc scanning.  The
36 // template parameter Scan must be a class type which provides two
37 // functions: local() and global().  Those functions implement the
38 // machine specific part of scanning.  We do it this way to
39 // avoid making a function call for each relocation, and to avoid
40 // repeating the generic code for each target.
41 
42 template<int size, bool big_endian, typename Target_type,
43 	 typename Scan, typename Classify_reloc>
44 inline void
scan_relocs(Symbol_table * symtab,Layout * layout,Target_type * target,Sized_relobj_file<size,big_endian> * object,unsigned int data_shndx,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,bool needs_special_offset_handling,size_t local_count,const unsigned char * plocal_syms)45 scan_relocs(
46     Symbol_table* symtab,
47     Layout* layout,
48     Target_type* target,
49     Sized_relobj_file<size, big_endian>* object,
50     unsigned int data_shndx,
51     const unsigned char* prelocs,
52     size_t reloc_count,
53     Output_section* output_section,
54     bool needs_special_offset_handling,
55     size_t local_count,
56     const unsigned char* plocal_syms)
57 {
58   typedef typename Classify_reloc::Reltype Reltype;
59   const int reloc_size = Classify_reloc::reloc_size;
60   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
61   Scan scan;
62 
63   for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
64     {
65       Reltype reloc(prelocs);
66 
67       if (needs_special_offset_handling
68 	  && !output_section->is_input_address_mapped(object, data_shndx,
69 						      reloc.get_r_offset()))
70 	continue;
71 
72       unsigned int r_sym = Classify_reloc::get_r_sym(&reloc);
73       unsigned int r_type = Classify_reloc::get_r_type(&reloc);
74 
75       if (r_sym < local_count)
76 	{
77 	  gold_assert(plocal_syms != NULL);
78 	  typename elfcpp::Sym<size, big_endian> lsym(plocal_syms
79 						      + r_sym * sym_size);
80 	  unsigned int shndx = lsym.get_st_shndx();
81 	  bool is_ordinary;
82 	  shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
83 	  // If RELOC is a relocation against a local symbol in a
84 	  // section we are discarding then we can ignore it.  It will
85 	  // eventually become a reloc against the value zero.
86 	  //
87 	  // FIXME: We should issue a warning if this is an
88 	  // allocated section; is this the best place to do it?
89 	  //
90 	  // FIXME: The old GNU linker would in some cases look
91 	  // for the linkonce section which caused this section to
92 	  // be discarded, and, if the other section was the same
93 	  // size, change the reloc to refer to the other section.
94 	  // That seems risky and weird to me, and I don't know of
95 	  // any case where it is actually required.
96 	  bool is_discarded = (is_ordinary
97 			       && shndx != elfcpp::SHN_UNDEF
98 			       && !object->is_section_included(shndx)
99 			       && !symtab->is_section_folded(object, shndx));
100 	  scan.local(symtab, layout, target, object, data_shndx,
101 		     output_section, reloc, r_type, lsym, is_discarded);
102 	}
103       else
104 	{
105 	  Symbol* gsym = object->global_symbol(r_sym);
106 	  gold_assert(gsym != NULL);
107 	  if (gsym->is_forwarder())
108 	    gsym = symtab->resolve_forwards(gsym);
109 
110 	  scan.global(symtab, layout, target, object, data_shndx,
111 		      output_section, reloc, r_type, gsym);
112 	}
113     }
114 }
115 
116 // Behavior for relocations to discarded comdat sections.
117 
118 enum Comdat_behavior
119 {
120   CB_UNDETERMINED,   // Not yet determined -- need to look at section name.
121   CB_PRETEND,        // Attempt to map to the corresponding kept section.
122   CB_IGNORE,         // Ignore the relocation.
123   CB_ERROR           // Print an error.
124 };
125 
126 class Default_comdat_behavior
127 {
128  public:
129   // Decide what the linker should do for relocations that refer to
130   // discarded comdat sections.  This decision is based on the name of
131   // the section being relocated.
132 
133   inline Comdat_behavior
get(const char * name)134   get(const char* name)
135   {
136     if (Layout::is_debug_info_section(name))
137       return CB_PRETEND;
138     if (strcmp(name, ".eh_frame") == 0
139 	|| is_prefix_of (".gnu.build.attributes", name)
140 	|| strcmp(name, ".gcc_except_table") == 0)
141       return CB_IGNORE;
142     return CB_ERROR;
143   }
144 };
145 
146 // Give an error for a symbol with non-default visibility which is not
147 // defined locally.
148 
149 inline void
visibility_error(const Symbol * sym)150 visibility_error(const Symbol* sym)
151 {
152   const char* v;
153   switch (sym->visibility())
154     {
155     case elfcpp::STV_INTERNAL:
156       v = _("internal");
157       break;
158     case elfcpp::STV_HIDDEN:
159       v = _("hidden");
160       break;
161     case elfcpp::STV_PROTECTED:
162       v = _("protected");
163       break;
164     default:
165       gold_unreachable();
166     }
167   gold_error(_("%s symbol '%s' is not defined locally"),
168 	     v, sym->name());
169 }
170 
171 // Return true if we are should issue an error saying that SYM is an
172 // undefined symbol.  This is called if there is a relocation against
173 // SYM.
174 
175 inline bool
issue_undefined_symbol_error(const Symbol * sym)176 issue_undefined_symbol_error(const Symbol* sym)
177 {
178   // We only report global symbols.
179   if (sym == NULL)
180     return false;
181 
182   // We only report undefined symbols.
183   if (!sym->is_undefined() && !sym->is_placeholder())
184     return false;
185 
186   // We don't report weak symbols.
187   if (sym->is_weak_undefined())
188     return false;
189 
190   // We don't report symbols defined in discarded sections,
191   // unless they're placeholder symbols that should have been
192   // provided by a plugin.
193   if (sym->is_defined_in_discarded_section() && !sym->is_placeholder())
194     return false;
195 
196   // If the target defines this symbol, don't report it here.
197   if (parameters->target().is_defined_by_abi(sym))
198     return false;
199 
200   // See if we've been told to ignore whether this symbol is
201   // undefined.
202   const char* const u = parameters->options().unresolved_symbols();
203   if (u != NULL)
204     {
205       if (strcmp(u, "ignore-all") == 0)
206 	return false;
207       if (strcmp(u, "report-all") == 0)
208 	return true;
209       if (strcmp(u, "ignore-in-object-files") == 0 && !sym->in_dyn())
210 	return false;
211       if (strcmp(u, "ignore-in-shared-libs") == 0 && !sym->in_reg())
212 	return false;
213     }
214 
215   // If the symbol is hidden, report it.
216   if (sym->visibility() == elfcpp::STV_HIDDEN)
217     return true;
218 
219   // When creating a shared library, only report unresolved symbols if
220   // -z defs was used.
221   if (parameters->options().shared() && !parameters->options().defs())
222     return false;
223 
224   // Otherwise issue a warning.
225   return true;
226 }
227 
228 template<int size, bool big_endian>
229 inline void
issue_discarded_error(const Relocate_info<size,big_endian> * relinfo,size_t shndx,section_offset_type offset,unsigned int r_sym,const Symbol * gsym)230 issue_discarded_error(
231   const Relocate_info<size, big_endian>* relinfo,
232   size_t shndx,
233   section_offset_type offset,
234   unsigned int r_sym,
235   const Symbol* gsym)
236 {
237   Sized_relobj_file<size, big_endian>* object = relinfo->object;
238 
239   if (gsym == NULL)
240     {
241       gold_error_at_location(
242 	  relinfo, shndx, offset,
243 	  _("relocation refers to local symbol \"%s\" [%u], "
244 	    "which is defined in a discarded section"),
245 	  object->get_symbol_name(r_sym), r_sym);
246     }
247   else
248     {
249       gold_error_at_location(
250 	  relinfo, shndx, offset,
251 	  _("relocation refers to global symbol \"%s\", "
252 	    "which is defined in a discarded section"),
253 	  gsym->demangled_name().c_str());
254     }
255 
256   bool is_ordinary;
257   typename elfcpp::Elf_types<size>::Elf_Addr value;
258   unsigned int orig_shndx = object->symbol_section_and_value(r_sym, &value,
259 							     &is_ordinary);
260   if (orig_shndx != elfcpp::SHN_UNDEF)
261     {
262       unsigned int key_symndx = 0;
263       Relobj* kept_obj = object->find_kept_section_object(orig_shndx,
264 							  &key_symndx);
265       if (key_symndx != 0)
266 	gold_info(_("  section group signature: \"%s\""),
267 		  object->get_symbol_name(key_symndx));
268       if (kept_obj != NULL)
269 	gold_info(_("  prevailing definition is from %s"),
270 		  kept_obj->name().c_str());
271     }
272 }
273 
274 // This function implements the generic part of relocation processing.
275 // The template parameter Relocate must be a class type which provides
276 // a single function, relocate(), which implements the machine
277 // specific part of a relocation.
278 
279 // The template parameter Relocate_comdat_behavior is a class type
280 // which provides a single function, get(), which determines what the
281 // linker should do for relocations that refer to discarded comdat
282 // sections.
283 
284 // SIZE is the ELF size: 32 or 64.  BIG_ENDIAN is the endianness of
285 // the data.  SH_TYPE is the section type: SHT_REL or SHT_RELA.
286 // RELOCATE implements operator() to do a relocation.
287 
288 // PRELOCS points to the relocation data.  RELOC_COUNT is the number
289 // of relocs.  OUTPUT_SECTION is the output section.
290 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
291 // mapped to output offsets.
292 
293 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
294 // VIEW_SIZE is the size.  These refer to the input section, unless
295 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
296 // the output section.
297 
298 // RELOC_SYMBOL_CHANGES is used for -fsplit-stack support.  If it is
299 // not NULL, it is a vector indexed by relocation index.  If that
300 // entry is not NULL, it points to a global symbol which used as the
301 // symbol for the relocation, ignoring the symbol index in the
302 // relocation.
303 
304 template<int size, bool big_endian, typename Target_type,
305 	 typename Relocate,
306 	 typename Relocate_comdat_behavior,
307 	 typename Classify_reloc>
308 inline void
relocate_section(const Relocate_info<size,big_endian> * relinfo,Target_type * target,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,bool needs_special_offset_handling,unsigned char * view,typename elfcpp::Elf_types<size>::Elf_Addr view_address,section_size_type view_size,const Reloc_symbol_changes * reloc_symbol_changes)309 relocate_section(
310     const Relocate_info<size, big_endian>* relinfo,
311     Target_type* target,
312     const unsigned char* prelocs,
313     size_t reloc_count,
314     Output_section* output_section,
315     bool needs_special_offset_handling,
316     unsigned char* view,
317     typename elfcpp::Elf_types<size>::Elf_Addr view_address,
318     section_size_type view_size,
319     const Reloc_symbol_changes* reloc_symbol_changes)
320 {
321   typedef typename Classify_reloc::Reltype Reltype;
322   const int reloc_size = Classify_reloc::reloc_size;
323   Relocate relocate;
324   Relocate_comdat_behavior relocate_comdat_behavior;
325 
326   Sized_relobj_file<size, big_endian>* object = relinfo->object;
327   unsigned int local_count = object->local_symbol_count();
328 
329   Comdat_behavior comdat_behavior = CB_UNDETERMINED;
330 
331   for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
332     {
333       Reltype reloc(prelocs);
334 
335       section_offset_type offset =
336 	convert_to_section_size_type(reloc.get_r_offset());
337 
338       if (needs_special_offset_handling)
339 	{
340 	  offset = output_section->output_offset(relinfo->object,
341 						 relinfo->data_shndx,
342 						 offset);
343 	  if (offset == -1)
344 	    continue;
345 	}
346 
347       unsigned int r_sym = Classify_reloc::get_r_sym(&reloc);
348 
349       const Sized_symbol<size>* sym;
350 
351       Symbol_value<size> symval;
352       const Symbol_value<size> *psymval;
353       bool is_defined_in_discarded_section;
354       unsigned int shndx;
355       const Symbol* gsym = NULL;
356       if (r_sym < local_count
357 	  && (reloc_symbol_changes == NULL
358 	      || (*reloc_symbol_changes)[i] == NULL))
359 	{
360 	  sym = NULL;
361 	  psymval = object->local_symbol(r_sym);
362 
363           // If the local symbol belongs to a section we are discarding,
364           // and that section is a debug section, try to find the
365           // corresponding kept section and map this symbol to its
366           // counterpart in the kept section.  The symbol must not
367           // correspond to a section we are folding.
368 	  bool is_ordinary;
369 	  shndx = psymval->input_shndx(&is_ordinary);
370 	  is_defined_in_discarded_section =
371 	    (is_ordinary
372 	     && shndx != elfcpp::SHN_UNDEF
373 	     && !object->is_section_included(shndx)
374 	     && !relinfo->symtab->is_section_folded(object, shndx));
375 	}
376       else
377 	{
378 	  if (reloc_symbol_changes != NULL
379 	      && (*reloc_symbol_changes)[i] != NULL)
380 	    gsym = (*reloc_symbol_changes)[i];
381 	  else
382 	    {
383 	      gsym = object->global_symbol(r_sym);
384 	      gold_assert(gsym != NULL);
385 	      if (gsym->is_forwarder())
386 		gsym = relinfo->symtab->resolve_forwards(gsym);
387 	    }
388 
389 	  sym = static_cast<const Sized_symbol<size>*>(gsym);
390 	  if (sym->has_symtab_index() && sym->symtab_index() != -1U)
391 	    symval.set_output_symtab_index(sym->symtab_index());
392 	  else
393 	    symval.set_no_output_symtab_entry();
394 	  symval.set_output_value(sym->value());
395 	  if (gsym->type() == elfcpp::STT_TLS)
396 	    symval.set_is_tls_symbol();
397 	  else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
398 	    symval.set_is_ifunc_symbol();
399 	  psymval = &symval;
400 
401 	  is_defined_in_discarded_section =
402 	    (gsym->is_defined_in_discarded_section()
403 	     && gsym->is_undefined());
404 	  shndx = 0;
405 	}
406 
407       Symbol_value<size> symval2;
408       if (is_defined_in_discarded_section)
409 	{
410 	  std::string name = object->section_name(relinfo->data_shndx);
411 
412 	  if (comdat_behavior == CB_UNDETERMINED)
413 	      comdat_behavior = relocate_comdat_behavior.get(name.c_str());
414 
415 	  if (comdat_behavior == CB_PRETEND)
416 	    {
417 	      // FIXME: This case does not work for global symbols.
418 	      // We have no place to store the original section index.
419 	      // Fortunately this does not matter for comdat sections,
420 	      // only for sections explicitly discarded by a linker
421 	      // script.
422 	      bool found;
423 	      typename elfcpp::Elf_types<size>::Elf_Addr value =
424 		  object->map_to_kept_section(shndx, name, &found);
425 	      if (found)
426 		symval2.set_output_value(value + psymval->input_value());
427 	      else
428 		symval2.set_output_value(0);
429 	    }
430 	  else
431 	    {
432 	      if (comdat_behavior == CB_ERROR)
433 	        issue_discarded_error(relinfo, i, offset, r_sym, gsym);
434 	      symval2.set_output_value(0);
435 	    }
436 	  symval2.set_no_output_symtab_entry();
437 	  psymval = &symval2;
438 	}
439 
440       // If OFFSET is out of range, still let the target decide to
441       // ignore the relocation.  Pass in NULL as the VIEW argument so
442       // that it can return quickly without trashing an invalid memory
443       // address.
444       unsigned char *v = view + offset;
445       if (offset < 0 || static_cast<section_size_type>(offset) >= view_size)
446 	v = NULL;
447 
448       if (!relocate.relocate(relinfo, Classify_reloc::sh_type, target,
449 			     output_section, i, prelocs, sym, psymval,
450 			     v, view_address + offset, view_size))
451 	continue;
452 
453       if (v == NULL)
454 	{
455 	  gold_error_at_location(relinfo, i, offset,
456 				 _("reloc has bad offset %zu"),
457 				 static_cast<size_t>(offset));
458 	  continue;
459 	}
460 
461       if (issue_undefined_symbol_error(sym))
462 	gold_undefined_symbol_at_location(sym, relinfo, i, offset);
463       else if (sym != NULL
464 	       && sym->visibility() != elfcpp::STV_DEFAULT
465 	       && (sym->is_strong_undefined() || sym->is_from_dynobj()))
466 	visibility_error(sym);
467 
468       if (sym != NULL && sym->has_warning())
469 	relinfo->symtab->issue_warning(sym, relinfo, i, offset);
470     }
471 }
472 
473 // Apply an incremental relocation.
474 
475 template<int size, bool big_endian, typename Target_type,
476 	 typename Relocate>
477 void
apply_relocation(const Relocate_info<size,big_endian> * relinfo,Target_type * target,typename elfcpp::Elf_types<size>::Elf_Addr r_offset,unsigned int r_type,typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,const Symbol * gsym,unsigned char * view,typename elfcpp::Elf_types<size>::Elf_Addr address,section_size_type view_size)478 apply_relocation(const Relocate_info<size, big_endian>* relinfo,
479 		 Target_type* target,
480 		 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
481 		 unsigned int r_type,
482 		 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
483 		 const Symbol* gsym,
484 		 unsigned char* view,
485 		 typename elfcpp::Elf_types<size>::Elf_Addr address,
486 		 section_size_type view_size)
487 {
488   // Construct the ELF relocation in a temporary buffer.
489   const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
490   unsigned char relbuf[reloc_size];
491   elfcpp::Rela_write<size, big_endian> orel(relbuf);
492   orel.put_r_offset(r_offset);
493   orel.put_r_info(elfcpp::elf_r_info<size>(0, r_type));
494   orel.put_r_addend(r_addend);
495 
496   // Setup a Symbol_value for the global symbol.
497   const Sized_symbol<size>* sym = static_cast<const Sized_symbol<size>*>(gsym);
498   Symbol_value<size> symval;
499   gold_assert(sym->has_symtab_index() && sym->symtab_index() != -1U);
500   symval.set_output_symtab_index(sym->symtab_index());
501   symval.set_output_value(sym->value());
502   if (gsym->type() == elfcpp::STT_TLS)
503     symval.set_is_tls_symbol();
504   else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
505     symval.set_is_ifunc_symbol();
506 
507   Relocate relocate;
508   relocate.relocate(relinfo, elfcpp::SHT_RELA, target, NULL,
509 		    -1U, relbuf, sym, &symval,
510 		    view + r_offset, address + r_offset, view_size);
511 }
512 
513 // A class for inquiring about properties of a relocation,
514 // used while scanning relocs during a relocatable link and
515 // garbage collection. This class may be used as the default
516 // for SHT_RELA targets, but SHT_REL targets must implement
517 // a derived class that overrides get_size_for_reloc.
518 // The MIPS-64 target also needs to override the methods
519 // for accessing the r_sym and r_type fields of a relocation,
520 // due to its non-standard use of the r_info field.
521 
522 template<int sh_type_, int size, bool big_endian>
523 class Default_classify_reloc
524 {
525  public:
526   typedef typename Reloc_types<sh_type_, size, big_endian>::Reloc
527       Reltype;
528   typedef typename Reloc_types<sh_type_, size, big_endian>::Reloc_write
529       Reltype_write;
530   static const int reloc_size =
531       Reloc_types<sh_type_, size, big_endian>::reloc_size;
532   static const int sh_type = sh_type_;
533 
534   // Return the symbol referred to by the relocation.
535   static inline unsigned int
get_r_sym(const Reltype * reloc)536   get_r_sym(const Reltype* reloc)
537   { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
538 
539   // Return the type of the relocation.
540   static inline unsigned int
get_r_type(const Reltype * reloc)541   get_r_type(const Reltype* reloc)
542   { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
543 
544   // Return the explicit addend of the relocation (return 0 for SHT_REL).
545   static inline typename elfcpp::Elf_types<size>::Elf_Swxword
get_r_addend(const Reltype * reloc)546   get_r_addend(const Reltype* reloc)
547   { return Reloc_types<sh_type_, size, big_endian>::get_reloc_addend(reloc); }
548 
549   // Write the r_info field to a new reloc, using the r_info field from
550   // the original reloc, replacing the r_sym field with R_SYM.
551   static inline void
put_r_info(Reltype_write * new_reloc,Reltype * reloc,unsigned int r_sym)552   put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
553   {
554     unsigned int r_type = elfcpp::elf_r_type<size>(reloc->get_r_info());
555     new_reloc->put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
556   }
557 
558   // Write the r_addend field to a new reloc.
559   static inline void
put_r_addend(Reltype_write * to,typename elfcpp::Elf_types<size>::Elf_Swxword addend)560   put_r_addend(Reltype_write* to,
561 	       typename elfcpp::Elf_types<size>::Elf_Swxword addend)
562   { Reloc_types<sh_type_, size, big_endian>::set_reloc_addend(to, addend); }
563 
564   // Return the size of the addend of the relocation (only used for SHT_REL).
565   static unsigned int
get_size_for_reloc(unsigned int,Relobj *)566   get_size_for_reloc(unsigned int, Relobj*)
567   {
568     gold_unreachable();
569     return 0;
570   }
571 };
572 
573 // This class may be used as a typical class for the
574 // Scan_relocatable_reloc parameter to scan_relocatable_relocs.
575 // This class is intended to capture the most typical target behaviour,
576 // while still permitting targets to define their own independent class
577 // for Scan_relocatable_reloc.
578 
579 template<typename Classify_reloc>
580 class Default_scan_relocatable_relocs
581 {
582  public:
583   typedef typename Classify_reloc::Reltype Reltype;
584   static const int reloc_size = Classify_reloc::reloc_size;
585   static const int sh_type = Classify_reloc::sh_type;
586 
587   // Return the symbol referred to by the relocation.
588   static inline unsigned int
get_r_sym(const Reltype * reloc)589   get_r_sym(const Reltype* reloc)
590   { return Classify_reloc::get_r_sym(reloc); }
591 
592   // Return the type of the relocation.
593   static inline unsigned int
get_r_type(const Reltype * reloc)594   get_r_type(const Reltype* reloc)
595   { return Classify_reloc::get_r_type(reloc); }
596 
597   // Return the strategy to use for a local symbol which is not a
598   // section symbol, given the relocation type.
599   inline Relocatable_relocs::Reloc_strategy
local_non_section_strategy(unsigned int r_type,Relobj *,unsigned int r_sym)600   local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
601   {
602     // We assume that relocation type 0 is NONE.  Targets which are
603     // different must override.
604     if (r_type == 0 && r_sym == 0)
605       return Relocatable_relocs::RELOC_DISCARD;
606     return Relocatable_relocs::RELOC_COPY;
607   }
608 
609   // Return the strategy to use for a local symbol which is a section
610   // symbol, given the relocation type.
611   inline Relocatable_relocs::Reloc_strategy
local_section_strategy(unsigned int r_type,Relobj * object)612   local_section_strategy(unsigned int r_type, Relobj* object)
613   {
614     if (sh_type == elfcpp::SHT_RELA)
615       return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
616     else
617       {
618 	switch (Classify_reloc::get_size_for_reloc(r_type, object))
619 	  {
620 	  case 0:
621 	    return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0;
622 	  case 1:
623 	    return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1;
624 	  case 2:
625 	    return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2;
626 	  case 4:
627 	    return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4;
628 	  case 8:
629 	    return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8;
630 	  default:
631 	    gold_unreachable();
632 	  }
633       }
634   }
635 
636   // Return the strategy to use for a global symbol, given the
637   // relocation type, the object, and the symbol index.
638   inline Relocatable_relocs::Reloc_strategy
global_strategy(unsigned int,Relobj *,unsigned int)639   global_strategy(unsigned int, Relobj*, unsigned int)
640   { return Relocatable_relocs::RELOC_COPY; }
641 };
642 
643 // This is a strategy class used with scan_relocatable_relocs
644 // and --emit-relocs.
645 
646 template<typename Classify_reloc>
647 class Default_emit_relocs_strategy
648 {
649  public:
650   typedef typename Classify_reloc::Reltype Reltype;
651   static const int reloc_size = Classify_reloc::reloc_size;
652   static const int sh_type = Classify_reloc::sh_type;
653 
654   // Return the symbol referred to by the relocation.
655   static inline unsigned int
get_r_sym(const Reltype * reloc)656   get_r_sym(const Reltype* reloc)
657   { return Classify_reloc::get_r_sym(reloc); }
658 
659   // Return the type of the relocation.
660   static inline unsigned int
get_r_type(const Reltype * reloc)661   get_r_type(const Reltype* reloc)
662   { return Classify_reloc::get_r_type(reloc); }
663 
664   // A local non-section symbol.
665   inline Relocatable_relocs::Reloc_strategy
local_non_section_strategy(unsigned int,Relobj *,unsigned int)666   local_non_section_strategy(unsigned int, Relobj*, unsigned int)
667   { return Relocatable_relocs::RELOC_COPY; }
668 
669   // A local section symbol.
670   inline Relocatable_relocs::Reloc_strategy
local_section_strategy(unsigned int,Relobj *)671   local_section_strategy(unsigned int, Relobj*)
672   {
673     if (sh_type == elfcpp::SHT_RELA)
674       return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
675     else
676       {
677 	// The addend is stored in the section contents.  Since this
678 	// is not a relocatable link, we are going to apply the
679 	// relocation contents to the section as usual.  This means
680 	// that we have no way to record the original addend.  If the
681 	// original addend is not zero, there is basically no way for
682 	// the user to handle this correctly.  Caveat emptor.
683 	return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0;
684       }
685   }
686 
687   // A global symbol.
688   inline Relocatable_relocs::Reloc_strategy
global_strategy(unsigned int,Relobj *,unsigned int)689   global_strategy(unsigned int, Relobj*, unsigned int)
690   { return Relocatable_relocs::RELOC_COPY; }
691 };
692 
693 // Scan relocs during a relocatable link.  This is a default
694 // definition which should work for most targets.
695 // Scan_relocatable_reloc must name a class type which provides three
696 // functions which return a Relocatable_relocs::Reloc_strategy code:
697 // global_strategy, local_non_section_strategy, and
698 // local_section_strategy.  Most targets should be able to use
699 // Default_scan_relocatable_relocs as this class.
700 
701 template<int size, bool big_endian, typename Scan_relocatable_reloc>
702 void
scan_relocatable_relocs(Symbol_table *,Layout *,Sized_relobj_file<size,big_endian> * object,unsigned int data_shndx,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,bool needs_special_offset_handling,size_t local_symbol_count,const unsigned char * plocal_syms,Relocatable_relocs * rr)703 scan_relocatable_relocs(
704     Symbol_table*,
705     Layout*,
706     Sized_relobj_file<size, big_endian>* object,
707     unsigned int data_shndx,
708     const unsigned char* prelocs,
709     size_t reloc_count,
710     Output_section* output_section,
711     bool needs_special_offset_handling,
712     size_t local_symbol_count,
713     const unsigned char* plocal_syms,
714     Relocatable_relocs* rr)
715 {
716   typedef typename Scan_relocatable_reloc::Reltype Reltype;
717   const int reloc_size = Scan_relocatable_reloc::reloc_size;
718   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
719   Scan_relocatable_reloc scan;
720 
721   for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
722     {
723       Reltype reloc(prelocs);
724 
725       Relocatable_relocs::Reloc_strategy strategy;
726 
727       if (needs_special_offset_handling
728 	  && !output_section->is_input_address_mapped(object, data_shndx,
729 						      reloc.get_r_offset()))
730 	strategy = Relocatable_relocs::RELOC_DISCARD;
731       else
732 	{
733 	  const unsigned int r_sym = Scan_relocatable_reloc::get_r_sym(&reloc);
734 	  const unsigned int r_type =
735 	      Scan_relocatable_reloc::get_r_type(&reloc);
736 
737 	  if (r_sym >= local_symbol_count)
738 	    strategy = scan.global_strategy(r_type, object, r_sym);
739 	  else
740 	    {
741 	      gold_assert(plocal_syms != NULL);
742 	      typename elfcpp::Sym<size, big_endian> lsym(plocal_syms
743 							  + r_sym * sym_size);
744 	      unsigned int shndx = lsym.get_st_shndx();
745 	      bool is_ordinary;
746 	      shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
747 	      if (is_ordinary
748 		  && shndx != elfcpp::SHN_UNDEF
749 		  && !object->is_section_included(shndx))
750 		{
751 		  // RELOC is a relocation against a local symbol
752 		  // defined in a section we are discarding.  Discard
753 		  // the reloc.  FIXME: Should we issue a warning?
754 		  strategy = Relocatable_relocs::RELOC_DISCARD;
755 		}
756 	      else if (lsym.get_st_type() != elfcpp::STT_SECTION)
757 		strategy = scan.local_non_section_strategy(r_type, object,
758 							   r_sym);
759 	      else
760 		{
761 		  strategy = scan.local_section_strategy(r_type, object);
762 		  if (strategy != Relocatable_relocs::RELOC_DISCARD)
763                     object->output_section(shndx)->set_needs_symtab_index();
764 		}
765 
766 	      if (strategy == Relocatable_relocs::RELOC_COPY)
767 		object->set_must_have_output_symtab_entry(r_sym);
768 	    }
769 	}
770 
771       rr->set_next_reloc_strategy(strategy);
772     }
773 }
774 
775 // Relocate relocs.  Called for a relocatable link, and for --emit-relocs.
776 // This is a default definition which should work for most targets.
777 
778 template<int size, bool big_endian, typename Classify_reloc>
779 void
relocate_relocs(const Relocate_info<size,big_endian> * relinfo,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,unsigned char * view,typename elfcpp::Elf_types<size>::Elf_Addr view_address,section_size_type view_size,unsigned char * reloc_view,section_size_type reloc_view_size)780 relocate_relocs(
781     const Relocate_info<size, big_endian>* relinfo,
782     const unsigned char* prelocs,
783     size_t reloc_count,
784     Output_section* output_section,
785     typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
786     unsigned char* view,
787     typename elfcpp::Elf_types<size>::Elf_Addr view_address,
788     section_size_type view_size,
789     unsigned char* reloc_view,
790     section_size_type reloc_view_size)
791 {
792   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
793   typedef typename Classify_reloc::Reltype Reltype;
794   typedef typename Classify_reloc::Reltype_write Reltype_write;
795   const int reloc_size = Classify_reloc::reloc_size;
796   const Address invalid_address = static_cast<Address>(0) - 1;
797 
798   Sized_relobj_file<size, big_endian>* const object = relinfo->object;
799   const unsigned int local_count = object->local_symbol_count();
800 
801   unsigned char* pwrite = reloc_view;
802 
803   const bool relocatable = parameters->options().relocatable();
804 
805   for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
806     {
807       Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
808       if (strategy == Relocatable_relocs::RELOC_DISCARD)
809 	continue;
810 
811       if (strategy == Relocatable_relocs::RELOC_SPECIAL)
812 	{
813 	  // Target wants to handle this relocation.
814 	  Sized_target<size, big_endian>* target =
815 	    parameters->sized_target<size, big_endian>();
816 	  target->relocate_special_relocatable(relinfo, Classify_reloc::sh_type,
817 					       prelocs, i, output_section,
818 					       offset_in_output_section,
819 					       view, view_address,
820 					       view_size, pwrite);
821 	  pwrite += reloc_size;
822 	  continue;
823 	}
824       Reltype reloc(prelocs);
825       Reltype_write reloc_write(pwrite);
826 
827       const unsigned int r_sym = Classify_reloc::get_r_sym(&reloc);
828 
829       // Get the new symbol index.
830 
831       Output_section* os = NULL;
832       unsigned int new_symndx;
833       if (r_sym < local_count)
834 	{
835 	  switch (strategy)
836 	    {
837 	    case Relocatable_relocs::RELOC_COPY:
838 	      if (r_sym == 0)
839 		new_symndx = 0;
840 	      else
841 		{
842 		  new_symndx = object->symtab_index(r_sym);
843 		  gold_assert(new_symndx != -1U);
844 		}
845 	      break;
846 
847 	    case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
848 	    case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0:
849 	    case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1:
850 	    case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2:
851 	    case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4:
852 	    case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8:
853 	    case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4_UNALIGNED:
854 	      {
855 		// We are adjusting a section symbol.  We need to find
856 		// the symbol table index of the section symbol for
857 		// the output section corresponding to input section
858 		// in which this symbol is defined.
859 		gold_assert(r_sym < local_count);
860 		bool is_ordinary;
861 		unsigned int shndx =
862 		  object->local_symbol_input_shndx(r_sym, &is_ordinary);
863 		gold_assert(is_ordinary);
864 		os = object->output_section(shndx);
865 		gold_assert(os != NULL);
866 		gold_assert(os->needs_symtab_index());
867 		new_symndx = os->symtab_index();
868 	      }
869 	      break;
870 
871 	    default:
872 	      gold_unreachable();
873 	    }
874 	}
875       else
876 	{
877 	  const Symbol* gsym = object->global_symbol(r_sym);
878 	  gold_assert(gsym != NULL);
879 	  if (gsym->is_forwarder())
880 	    gsym = relinfo->symtab->resolve_forwards(gsym);
881 
882 	  gold_assert(gsym->has_symtab_index());
883 	  new_symndx = gsym->symtab_index();
884 	}
885 
886       // Get the new offset--the location in the output section where
887       // this relocation should be applied.
888 
889       Address offset = reloc.get_r_offset();
890       Address new_offset;
891       if (offset_in_output_section != invalid_address)
892 	new_offset = offset + offset_in_output_section;
893       else
894 	{
895           section_offset_type sot_offset =
896               convert_types<section_offset_type, Address>(offset);
897 	  section_offset_type new_sot_offset =
898               output_section->output_offset(object, relinfo->data_shndx,
899                                             sot_offset);
900 	  gold_assert(new_sot_offset != -1);
901           new_offset = new_sot_offset;
902 	}
903 
904       // In an object file, r_offset is an offset within the section.
905       // In an executable or dynamic object, generated by
906       // --emit-relocs, r_offset is an absolute address.
907       if (!relocatable)
908 	{
909 	  new_offset += view_address;
910 	  if (offset_in_output_section != invalid_address)
911 	    new_offset -= offset_in_output_section;
912 	}
913 
914       reloc_write.put_r_offset(new_offset);
915       Classify_reloc::put_r_info(&reloc_write, &reloc, new_symndx);
916 
917       // Handle the reloc addend based on the strategy.
918 
919       if (strategy == Relocatable_relocs::RELOC_COPY)
920 	{
921 	  if (Classify_reloc::sh_type == elfcpp::SHT_RELA)
922 	    Classify_reloc::put_r_addend(&reloc_write,
923 					 Classify_reloc::get_r_addend(&reloc));
924 	}
925       else
926 	{
927 	  // The relocation uses a section symbol in the input file.
928 	  // We are adjusting it to use a section symbol in the output
929 	  // file.  The input section symbol refers to some address in
930 	  // the input section.  We need the relocation in the output
931 	  // file to refer to that same address.  This adjustment to
932 	  // the addend is the same calculation we use for a simple
933 	  // absolute relocation for the input section symbol.
934 
935 	  const Symbol_value<size>* psymval = object->local_symbol(r_sym);
936 
937 	  unsigned char* padd = view + offset;
938 	  switch (strategy)
939 	    {
940 	    case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
941 	      {
942 		typename elfcpp::Elf_types<size>::Elf_Swxword addend
943 		    = Classify_reloc::get_r_addend(&reloc);
944 		addend = psymval->value(object, addend);
945 		// In a relocatable link, the symbol value is relative to
946 		// the start of the output section. For a non-relocatable
947 		// link, we need to adjust the addend.
948 		if (!relocatable)
949 		  {
950 		    gold_assert(os != NULL);
951 		    addend -= os->address();
952 		  }
953 		Classify_reloc::put_r_addend(&reloc_write, addend);
954 	      }
955 	      break;
956 
957 	    case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0:
958 	      break;
959 
960 	    case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1:
961 	      Relocate_functions<size, big_endian>::rel8(padd, object,
962 							 psymval);
963 	      break;
964 
965 	    case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2:
966 	      Relocate_functions<size, big_endian>::rel16(padd, object,
967 							  psymval);
968 	      break;
969 
970 	    case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4:
971 	      Relocate_functions<size, big_endian>::rel32(padd, object,
972 							  psymval);
973 	      break;
974 
975 	    case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8:
976 	      Relocate_functions<size, big_endian>::rel64(padd, object,
977 							  psymval);
978 	      break;
979 
980 	    case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4_UNALIGNED:
981 	      Relocate_functions<size, big_endian>::rel32_unaligned(padd,
982 								    object,
983 								    psymval);
984 	      break;
985 
986 	    default:
987 	      gold_unreachable();
988 	    }
989 	}
990 
991       pwrite += reloc_size;
992     }
993 
994   gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
995 	      == reloc_view_size);
996 }
997 
998 } // End namespace gold.
999 
1000 #endif // !defined(GOLD_TARGET_RELOC_H)
1001