Update copyright years
[deliverable/binutils-gdb.git] / gold / target-reloc.h
1 // target-reloc.h -- target specific relocation support -*- 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_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, int sh_type,
43 typename Scan>
44 inline void
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 Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
59 const int reloc_size = Reloc_types<sh_type, size, big_endian>::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 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
73 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
74 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
75
76 if (r_sym < local_count)
77 {
78 gold_assert(plocal_syms != NULL);
79 typename elfcpp::Sym<size, big_endian> lsym(plocal_syms
80 + r_sym * sym_size);
81 unsigned int shndx = lsym.get_st_shndx();
82 bool is_ordinary;
83 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
84 // If RELOC is a relocation against a local symbol in a
85 // section we are discarding then we can ignore it. It will
86 // eventually become a reloc against the value zero.
87 //
88 // FIXME: We should issue a warning if this is an
89 // allocated section; is this the best place to do it?
90 //
91 // FIXME: The old GNU linker would in some cases look
92 // for the linkonce section which caused this section to
93 // be discarded, and, if the other section was the same
94 // size, change the reloc to refer to the other section.
95 // That seems risky and weird to me, and I don't know of
96 // any case where it is actually required.
97 bool is_discarded = (is_ordinary
98 && shndx != elfcpp::SHN_UNDEF
99 && !object->is_section_included(shndx)
100 && !symtab->is_section_folded(object, shndx));
101 scan.local(symtab, layout, target, object, data_shndx,
102 output_section, reloc, r_type, lsym, is_discarded);
103 }
104 else
105 {
106 Symbol* gsym = object->global_symbol(r_sym);
107 gold_assert(gsym != NULL);
108 if (gsym->is_forwarder())
109 gsym = symtab->resolve_forwards(gsym);
110
111 scan.global(symtab, layout, target, object, data_shndx,
112 output_section, reloc, r_type, gsym);
113 }
114 }
115 }
116
117 // Behavior for relocations to discarded comdat sections.
118
119 enum Comdat_behavior
120 {
121 CB_UNDETERMINED, // Not yet determined -- need to look at section name.
122 CB_PRETEND, // Attempt to map to the corresponding kept section.
123 CB_IGNORE, // Ignore the relocation.
124 CB_WARNING // Print a warning.
125 };
126
127 class Default_comdat_behavior
128 {
129 public:
130 // Decide what the linker should do for relocations that refer to
131 // discarded comdat sections. This decision is based on the name of
132 // the section being relocated.
133
134 inline Comdat_behavior
135 get(const char* name)
136 {
137 if (Layout::is_debug_info_section(name))
138 return CB_PRETEND;
139 if (strcmp(name, ".eh_frame") == 0
140 || strcmp(name, ".gcc_except_table") == 0)
141 return CB_IGNORE;
142 return CB_WARNING;
143 }
144 };
145
146 inline bool
147 is_strong_undefined(const Symbol* sym)
148 {
149 return sym->is_undefined() && sym->binding() != elfcpp::STB_WEAK;
150 }
151
152 // Give an error for a symbol with non-default visibility which is not
153 // defined locally.
154
155 inline void
156 visibility_error(const Symbol* sym)
157 {
158 const char* v;
159 switch (sym->visibility())
160 {
161 case elfcpp::STV_INTERNAL:
162 v = _("internal");
163 break;
164 case elfcpp::STV_HIDDEN:
165 v = _("hidden");
166 break;
167 case elfcpp::STV_PROTECTED:
168 v = _("protected");
169 break;
170 default:
171 gold_unreachable();
172 }
173 gold_error(_("%s symbol '%s' is not defined locally"),
174 v, sym->name());
175 }
176
177 // Return true if we are should issue an error saying that SYM is an
178 // undefined symbol. This is called if there is a relocation against
179 // SYM.
180
181 inline bool
182 issue_undefined_symbol_error(const Symbol* sym)
183 {
184 // We only report global symbols.
185 if (sym == NULL)
186 return false;
187
188 // We only report undefined symbols.
189 if (!sym->is_undefined() && !sym->is_placeholder())
190 return false;
191
192 // We don't report weak symbols.
193 if (sym->binding() == elfcpp::STB_WEAK)
194 return false;
195
196 // We don't report symbols defined in discarded sections.
197 if (sym->is_defined_in_discarded_section())
198 return false;
199
200 // If the target defines this symbol, don't report it here.
201 if (parameters->target().is_defined_by_abi(sym))
202 return false;
203
204 // See if we've been told to ignore whether this symbol is
205 // undefined.
206 const char* const u = parameters->options().unresolved_symbols();
207 if (u != NULL)
208 {
209 if (strcmp(u, "ignore-all") == 0)
210 return false;
211 if (strcmp(u, "report-all") == 0)
212 return true;
213 if (strcmp(u, "ignore-in-object-files") == 0 && !sym->in_dyn())
214 return false;
215 if (strcmp(u, "ignore-in-shared-libs") == 0 && !sym->in_reg())
216 return false;
217 }
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 // This function implements the generic part of relocation processing.
229 // The template parameter Relocate must be a class type which provides
230 // a single function, relocate(), which implements the machine
231 // specific part of a relocation.
232
233 // The template parameter Relocate_comdat_behavior is a class type
234 // which provides a single function, get(), which determines what the
235 // linker should do for relocations that refer to discarded comdat
236 // sections.
237
238 // SIZE is the ELF size: 32 or 64. BIG_ENDIAN is the endianness of
239 // the data. SH_TYPE is the section type: SHT_REL or SHT_RELA.
240 // RELOCATE implements operator() to do a relocation.
241
242 // PRELOCS points to the relocation data. RELOC_COUNT is the number
243 // of relocs. OUTPUT_SECTION is the output section.
244 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
245 // mapped to output offsets.
246
247 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
248 // VIEW_SIZE is the size. These refer to the input section, unless
249 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
250 // the output section.
251
252 // RELOC_SYMBOL_CHANGES is used for -fsplit-stack support. If it is
253 // not NULL, it is a vector indexed by relocation index. If that
254 // entry is not NULL, it points to a global symbol which used as the
255 // symbol for the relocation, ignoring the symbol index in the
256 // relocation.
257
258 template<int size, bool big_endian, typename Target_type, int sh_type,
259 typename Relocate,
260 typename Relocate_comdat_behavior>
261 inline void
262 relocate_section(
263 const Relocate_info<size, big_endian>* relinfo,
264 Target_type* target,
265 const unsigned char* prelocs,
266 size_t reloc_count,
267 Output_section* output_section,
268 bool needs_special_offset_handling,
269 unsigned char* view,
270 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
271 section_size_type view_size,
272 const Reloc_symbol_changes* reloc_symbol_changes)
273 {
274 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
275 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
276 Relocate relocate;
277 Relocate_comdat_behavior relocate_comdat_behavior;
278
279 Sized_relobj_file<size, big_endian>* object = relinfo->object;
280 unsigned int local_count = object->local_symbol_count();
281
282 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
283
284 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
285 {
286 Reltype reloc(prelocs);
287
288 section_offset_type offset =
289 convert_to_section_size_type(reloc.get_r_offset());
290
291 if (needs_special_offset_handling)
292 {
293 offset = output_section->output_offset(relinfo->object,
294 relinfo->data_shndx,
295 offset);
296 if (offset == -1)
297 continue;
298 }
299
300 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
301 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
302 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
303
304 const Sized_symbol<size>* sym;
305
306 Symbol_value<size> symval;
307 const Symbol_value<size> *psymval;
308 bool is_defined_in_discarded_section;
309 unsigned int shndx;
310 if (r_sym < local_count
311 && (reloc_symbol_changes == NULL
312 || (*reloc_symbol_changes)[i] == NULL))
313 {
314 sym = NULL;
315 psymval = object->local_symbol(r_sym);
316
317 // If the local symbol belongs to a section we are discarding,
318 // and that section is a debug section, try to find the
319 // corresponding kept section and map this symbol to its
320 // counterpart in the kept section. The symbol must not
321 // correspond to a section we are folding.
322 bool is_ordinary;
323 shndx = psymval->input_shndx(&is_ordinary);
324 is_defined_in_discarded_section =
325 (is_ordinary
326 && shndx != elfcpp::SHN_UNDEF
327 && !object->is_section_included(shndx)
328 && !relinfo->symtab->is_section_folded(object, shndx));
329 }
330 else
331 {
332 const Symbol* gsym;
333 if (reloc_symbol_changes != NULL
334 && (*reloc_symbol_changes)[i] != NULL)
335 gsym = (*reloc_symbol_changes)[i];
336 else
337 {
338 gsym = object->global_symbol(r_sym);
339 gold_assert(gsym != NULL);
340 if (gsym->is_forwarder())
341 gsym = relinfo->symtab->resolve_forwards(gsym);
342 }
343
344 sym = static_cast<const Sized_symbol<size>*>(gsym);
345 if (sym->has_symtab_index() && sym->symtab_index() != -1U)
346 symval.set_output_symtab_index(sym->symtab_index());
347 else
348 symval.set_no_output_symtab_entry();
349 symval.set_output_value(sym->value());
350 if (gsym->type() == elfcpp::STT_TLS)
351 symval.set_is_tls_symbol();
352 else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
353 symval.set_is_ifunc_symbol();
354 psymval = &symval;
355
356 is_defined_in_discarded_section =
357 (gsym->is_defined_in_discarded_section()
358 && gsym->is_undefined());
359 shndx = 0;
360 }
361
362 Symbol_value<size> symval2;
363 if (is_defined_in_discarded_section)
364 {
365 if (comdat_behavior == CB_UNDETERMINED)
366 {
367 std::string name = object->section_name(relinfo->data_shndx);
368 comdat_behavior = relocate_comdat_behavior.get(name.c_str());
369 }
370 if (comdat_behavior == CB_PRETEND)
371 {
372 // FIXME: This case does not work for global symbols.
373 // We have no place to store the original section index.
374 // Fortunately this does not matter for comdat sections,
375 // only for sections explicitly discarded by a linker
376 // script.
377 bool found;
378 typename elfcpp::Elf_types<size>::Elf_Addr value =
379 object->map_to_kept_section(shndx, &found);
380 if (found)
381 symval2.set_output_value(value + psymval->input_value());
382 else
383 symval2.set_output_value(0);
384 }
385 else
386 {
387 if (comdat_behavior == CB_WARNING)
388 gold_warning_at_location(relinfo, i, offset,
389 _("relocation refers to discarded "
390 "section"));
391 symval2.set_output_value(0);
392 }
393 symval2.set_no_output_symtab_entry();
394 psymval = &symval2;
395 }
396
397 // If OFFSET is out of range, still let the target decide to
398 // ignore the relocation. Pass in NULL as the VIEW argument so
399 // that it can return quickly without trashing an invalid memory
400 // address.
401 unsigned char *v = view + offset;
402 if (offset < 0 || static_cast<section_size_type>(offset) >= view_size)
403 v = NULL;
404
405 if (!relocate.relocate(relinfo, target, output_section, i, reloc,
406 r_type, sym, psymval, v, view_address + offset,
407 view_size))
408 continue;
409
410 if (v == NULL)
411 {
412 gold_error_at_location(relinfo, i, offset,
413 _("reloc has bad offset %zu"),
414 static_cast<size_t>(offset));
415 continue;
416 }
417
418 if (issue_undefined_symbol_error(sym))
419 gold_undefined_symbol_at_location(sym, relinfo, i, offset);
420 else if (sym != NULL
421 && sym->visibility() != elfcpp::STV_DEFAULT
422 && (is_strong_undefined(sym) || sym->is_from_dynobj()))
423 visibility_error(sym);
424
425 if (sym != NULL && sym->has_warning())
426 relinfo->symtab->issue_warning(sym, relinfo, i, offset);
427 }
428 }
429
430 // Apply an incremental relocation.
431
432 template<int size, bool big_endian, typename Target_type,
433 typename Relocate>
434 void
435 apply_relocation(const Relocate_info<size, big_endian>* relinfo,
436 Target_type* target,
437 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
438 unsigned int r_type,
439 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
440 const Symbol* gsym,
441 unsigned char* view,
442 typename elfcpp::Elf_types<size>::Elf_Addr address,
443 section_size_type view_size)
444 {
445 // Construct the ELF relocation in a temporary buffer.
446 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
447 unsigned char relbuf[reloc_size];
448 elfcpp::Rela<size, big_endian> rel(relbuf);
449 elfcpp::Rela_write<size, big_endian> orel(relbuf);
450 orel.put_r_offset(r_offset);
451 orel.put_r_info(elfcpp::elf_r_info<size>(0, r_type));
452 orel.put_r_addend(r_addend);
453
454 // Setup a Symbol_value for the global symbol.
455 const Sized_symbol<size>* sym = static_cast<const Sized_symbol<size>*>(gsym);
456 Symbol_value<size> symval;
457 gold_assert(sym->has_symtab_index() && sym->symtab_index() != -1U);
458 symval.set_output_symtab_index(sym->symtab_index());
459 symval.set_output_value(sym->value());
460 if (gsym->type() == elfcpp::STT_TLS)
461 symval.set_is_tls_symbol();
462 else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
463 symval.set_is_ifunc_symbol();
464
465 Relocate relocate;
466 relocate.relocate(relinfo, target, NULL, -1U, rel, r_type, sym, &symval,
467 view + r_offset, address + r_offset, view_size);
468 }
469
470 // This class may be used as a typical class for the
471 // Scan_relocatable_reloc parameter to scan_relocatable_relocs. The
472 // template parameter Classify_reloc must be a class type which
473 // provides a function get_size_for_reloc which returns the number of
474 // bytes to which a reloc applies. This class is intended to capture
475 // the most typical target behaviour, while still permitting targets
476 // to define their own independent class for Scan_relocatable_reloc.
477
478 template<int sh_type, typename Classify_reloc>
479 class Default_scan_relocatable_relocs
480 {
481 public:
482 // Return the strategy to use for a local symbol which is not a
483 // section symbol, given the relocation type.
484 inline Relocatable_relocs::Reloc_strategy
485 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
486 {
487 // We assume that relocation type 0 is NONE. Targets which are
488 // different must override.
489 if (r_type == 0 && r_sym == 0)
490 return Relocatable_relocs::RELOC_DISCARD;
491 return Relocatable_relocs::RELOC_COPY;
492 }
493
494 // Return the strategy to use for a local symbol which is a section
495 // symbol, given the relocation type.
496 inline Relocatable_relocs::Reloc_strategy
497 local_section_strategy(unsigned int r_type, Relobj* object)
498 {
499 if (sh_type == elfcpp::SHT_RELA)
500 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
501 else
502 {
503 Classify_reloc classify;
504 switch (classify.get_size_for_reloc(r_type, object))
505 {
506 case 0:
507 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0;
508 case 1:
509 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1;
510 case 2:
511 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2;
512 case 4:
513 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4;
514 case 8:
515 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8;
516 default:
517 gold_unreachable();
518 }
519 }
520 }
521
522 // Return the strategy to use for a global symbol, given the
523 // relocation type, the object, and the symbol index.
524 inline Relocatable_relocs::Reloc_strategy
525 global_strategy(unsigned int, Relobj*, unsigned int)
526 { return Relocatable_relocs::RELOC_COPY; }
527 };
528
529 // Scan relocs during a relocatable link. This is a default
530 // definition which should work for most targets.
531 // Scan_relocatable_reloc must name a class type which provides three
532 // functions which return a Relocatable_relocs::Reloc_strategy code:
533 // global_strategy, local_non_section_strategy, and
534 // local_section_strategy. Most targets should be able to use
535 // Default_scan_relocatable_relocs as this class.
536
537 template<int size, bool big_endian, int sh_type,
538 typename Scan_relocatable_reloc>
539 void
540 scan_relocatable_relocs(
541 Symbol_table*,
542 Layout*,
543 Sized_relobj_file<size, big_endian>* object,
544 unsigned int data_shndx,
545 const unsigned char* prelocs,
546 size_t reloc_count,
547 Output_section* output_section,
548 bool needs_special_offset_handling,
549 size_t local_symbol_count,
550 const unsigned char* plocal_syms,
551 Relocatable_relocs* rr)
552 {
553 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
554 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
555 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
556 Scan_relocatable_reloc scan;
557
558 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
559 {
560 Reltype reloc(prelocs);
561
562 Relocatable_relocs::Reloc_strategy strategy;
563
564 if (needs_special_offset_handling
565 && !output_section->is_input_address_mapped(object, data_shndx,
566 reloc.get_r_offset()))
567 strategy = Relocatable_relocs::RELOC_DISCARD;
568 else
569 {
570 typename elfcpp::Elf_types<size>::Elf_WXword r_info =
571 reloc.get_r_info();
572 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
573 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
574
575 if (r_sym >= local_symbol_count)
576 strategy = scan.global_strategy(r_type, object, r_sym);
577 else
578 {
579 gold_assert(plocal_syms != NULL);
580 typename elfcpp::Sym<size, big_endian> lsym(plocal_syms
581 + r_sym * sym_size);
582 unsigned int shndx = lsym.get_st_shndx();
583 bool is_ordinary;
584 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
585 if (is_ordinary
586 && shndx != elfcpp::SHN_UNDEF
587 && !object->is_section_included(shndx))
588 {
589 // RELOC is a relocation against a local symbol
590 // defined in a section we are discarding. Discard
591 // the reloc. FIXME: Should we issue a warning?
592 strategy = Relocatable_relocs::RELOC_DISCARD;
593 }
594 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
595 strategy = scan.local_non_section_strategy(r_type, object,
596 r_sym);
597 else
598 {
599 strategy = scan.local_section_strategy(r_type, object);
600 if (strategy != Relocatable_relocs::RELOC_DISCARD)
601 object->output_section(shndx)->set_needs_symtab_index();
602 }
603
604 if (strategy == Relocatable_relocs::RELOC_COPY)
605 object->set_must_have_output_symtab_entry(r_sym);
606 }
607 }
608
609 rr->set_next_reloc_strategy(strategy);
610 }
611 }
612
613 // Relocate relocs. Called for a relocatable link, and for --emit-relocs.
614 // This is a default definition which should work for most targets.
615
616 template<int size, bool big_endian, int sh_type>
617 void
618 relocate_relocs(
619 const Relocate_info<size, big_endian>* relinfo,
620 const unsigned char* prelocs,
621 size_t reloc_count,
622 Output_section* output_section,
623 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
624 const Relocatable_relocs* rr,
625 unsigned char* view,
626 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
627 section_size_type view_size,
628 unsigned char* reloc_view,
629 section_size_type reloc_view_size)
630 {
631 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
632 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
633 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc_write
634 Reltype_write;
635 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
636 const Address invalid_address = static_cast<Address>(0) - 1;
637
638 Sized_relobj_file<size, big_endian>* const object = relinfo->object;
639 const unsigned int local_count = object->local_symbol_count();
640
641 unsigned char* pwrite = reloc_view;
642
643 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
644 {
645 Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
646 if (strategy == Relocatable_relocs::RELOC_DISCARD)
647 continue;
648
649 if (strategy == Relocatable_relocs::RELOC_SPECIAL)
650 {
651 // Target wants to handle this relocation.
652 Sized_target<size, big_endian>* target =
653 parameters->sized_target<size, big_endian>();
654 target->relocate_special_relocatable(relinfo, sh_type, prelocs,
655 i, output_section,
656 offset_in_output_section,
657 view, view_address,
658 view_size, pwrite);
659 pwrite += reloc_size;
660 continue;
661 }
662 Reltype reloc(prelocs);
663 Reltype_write reloc_write(pwrite);
664
665 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
666 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
667 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
668
669 // Get the new symbol index.
670
671 unsigned int new_symndx;
672 if (r_sym < local_count)
673 {
674 switch (strategy)
675 {
676 case Relocatable_relocs::RELOC_COPY:
677 if (r_sym == 0)
678 new_symndx = 0;
679 else
680 {
681 new_symndx = object->symtab_index(r_sym);
682 gold_assert(new_symndx != -1U);
683 }
684 break;
685
686 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
687 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0:
688 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1:
689 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2:
690 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4:
691 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8:
692 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4_UNALIGNED:
693 {
694 // We are adjusting a section symbol. We need to find
695 // the symbol table index of the section symbol for
696 // the output section corresponding to input section
697 // in which this symbol is defined.
698 gold_assert(r_sym < local_count);
699 bool is_ordinary;
700 unsigned int shndx =
701 object->local_symbol_input_shndx(r_sym, &is_ordinary);
702 gold_assert(is_ordinary);
703 Output_section* os = object->output_section(shndx);
704 gold_assert(os != NULL);
705 gold_assert(os->needs_symtab_index());
706 new_symndx = os->symtab_index();
707 }
708 break;
709
710 default:
711 gold_unreachable();
712 }
713 }
714 else
715 {
716 const Symbol* gsym = object->global_symbol(r_sym);
717 gold_assert(gsym != NULL);
718 if (gsym->is_forwarder())
719 gsym = relinfo->symtab->resolve_forwards(gsym);
720
721 gold_assert(gsym->has_symtab_index());
722 new_symndx = gsym->symtab_index();
723 }
724
725 // Get the new offset--the location in the output section where
726 // this relocation should be applied.
727
728 Address offset = reloc.get_r_offset();
729 Address new_offset;
730 if (offset_in_output_section != invalid_address)
731 new_offset = offset + offset_in_output_section;
732 else
733 {
734 section_offset_type sot_offset =
735 convert_types<section_offset_type, Address>(offset);
736 section_offset_type new_sot_offset =
737 output_section->output_offset(object, relinfo->data_shndx,
738 sot_offset);
739 gold_assert(new_sot_offset != -1);
740 new_offset = new_sot_offset;
741 }
742
743 // In an object file, r_offset is an offset within the section.
744 // In an executable or dynamic object, generated by
745 // --emit-relocs, r_offset is an absolute address.
746 if (!parameters->options().relocatable())
747 {
748 new_offset += view_address;
749 if (offset_in_output_section != invalid_address)
750 new_offset -= offset_in_output_section;
751 }
752
753 reloc_write.put_r_offset(new_offset);
754 reloc_write.put_r_info(elfcpp::elf_r_info<size>(new_symndx, r_type));
755
756 // Handle the reloc addend based on the strategy.
757
758 if (strategy == Relocatable_relocs::RELOC_COPY)
759 {
760 if (sh_type == elfcpp::SHT_RELA)
761 Reloc_types<sh_type, size, big_endian>::
762 copy_reloc_addend(&reloc_write,
763 &reloc);
764 }
765 else
766 {
767 // The relocation uses a section symbol in the input file.
768 // We are adjusting it to use a section symbol in the output
769 // file. The input section symbol refers to some address in
770 // the input section. We need the relocation in the output
771 // file to refer to that same address. This adjustment to
772 // the addend is the same calculation we use for a simple
773 // absolute relocation for the input section symbol.
774
775 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
776
777 unsigned char* padd = view + offset;
778 switch (strategy)
779 {
780 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
781 {
782 typename elfcpp::Elf_types<size>::Elf_Swxword addend;
783 addend = Reloc_types<sh_type, size, big_endian>::
784 get_reloc_addend(&reloc);
785 addend = psymval->value(object, addend);
786 Reloc_types<sh_type, size, big_endian>::
787 set_reloc_addend(&reloc_write, addend);
788 }
789 break;
790
791 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0:
792 break;
793
794 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1:
795 Relocate_functions<size, big_endian>::rel8(padd, object,
796 psymval);
797 break;
798
799 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2:
800 Relocate_functions<size, big_endian>::rel16(padd, object,
801 psymval);
802 break;
803
804 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4:
805 Relocate_functions<size, big_endian>::rel32(padd, object,
806 psymval);
807 break;
808
809 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8:
810 Relocate_functions<size, big_endian>::rel64(padd, object,
811 psymval);
812 break;
813
814 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4_UNALIGNED:
815 Relocate_functions<size, big_endian>::rel32_unaligned(padd,
816 object,
817 psymval);
818 break;
819
820 default:
821 gold_unreachable();
822 }
823 }
824
825 pwrite += reloc_size;
826 }
827
828 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
829 == reloc_view_size);
830 }
831
832 } // End namespace gold.
833
834 #endif // !defined(GOLD_TARGET_RELOC_H)
This page took 0.074181 seconds and 5 git commands to generate.