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