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