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