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