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