Generate a complete exception frame header. Discard duplicate
[external/binutils.git] / gold / target-reloc.h
1 // target-reloc.h -- target specific relocation support  -*- C++ -*-
2
3 // Copyright 2006, 2007 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 "reloc-types.h"
29
30 namespace gold
31 {
32
33 // This function implements the generic part of reloc scanning.  This
34 // is an inline function which takes a class whose member functions
35 // local() and global() implement the machine specific part of scanning.
36 // We do it this way to avoidmaking a function call for each relocation,
37 // and to avoid repeating the generic code for each target.
38
39 template<int size, bool big_endian, typename Target_type, int sh_type,
40          typename Scan>
41 inline void
42 scan_relocs(
43     const General_options& options,
44     Symbol_table* symtab,
45     Layout* layout,
46     Target_type* target,
47     Sized_relobj<size, big_endian>* object,
48     unsigned int data_shndx,
49     const unsigned char* prelocs,
50     size_t reloc_count,
51     Output_section* output_section,
52     bool needs_special_offset_handling,
53     size_t local_count,
54     const unsigned char* plocal_syms)
55 {
56   typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
57   const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
58   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
59   Scan scan;
60
61   for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
62     {
63       Reltype reloc(prelocs);
64
65       if (needs_special_offset_handling
66           && !output_section->is_input_address_mapped(object, data_shndx,
67                                                       reloc.get_r_offset()))
68         continue;
69
70       typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
71       unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
72       unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
73
74       if (r_sym < local_count)
75         {
76           gold_assert(plocal_syms != NULL);
77           typename elfcpp::Sym<size, big_endian> lsym(plocal_syms
78                                                       + r_sym * sym_size);
79           const unsigned int shndx = lsym.get_st_shndx();
80           if (shndx < elfcpp::SHN_LORESERVE
81               && shndx != elfcpp::SHN_UNDEF
82               && !object->is_section_included(lsym.get_st_shndx()))
83             {
84               // RELOC is a relocation against a local symbol in a
85               // section we are discarding.  We can ignore this
86               // relocation.  It will eventually become a reloc
87               // 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
99               continue;
100             }
101
102           scan.local(options, symtab, layout, target, object, data_shndx,
103                      reloc, r_type, lsym);
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(options, symtab, layout, target, object, data_shndx,
113                       reloc, r_type, gsym);
114         }
115     }
116 }
117
118 // This function implements the generic part of relocation processing.
119 // This is an inline function which take a class whose relocate()
120 // implements the machine specific part of relocation.  We do it this
121 // way to avoid making a function call for each relocation, and to
122 // avoid repeating the generic relocation handling code for each
123 // target.
124
125 // SIZE is the ELF size: 32 or 64.  BIG_ENDIAN is the endianness of
126 // the data.  SH_TYPE is the section type: SHT_REL or SHT_RELA.
127 // RELOCATE implements operator() to do a relocation.
128
129 // PRELOCS points to the relocation data.  RELOC_COUNT is the number
130 // of relocs.  OUTPUT_SECTION is the output section.
131 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
132 // mapped to output offsets.
133
134 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
135 // VIEW_SIZE is the size.  These refer to the input section, unless
136 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
137 // the output section.
138
139 template<int size, bool big_endian, typename Target_type, int sh_type,
140          typename Relocate>
141 inline void
142 relocate_section(
143     const Relocate_info<size, big_endian>* relinfo,
144     Target_type* target,
145     const unsigned char* prelocs,
146     size_t reloc_count,
147     Output_section* output_section,
148     bool needs_special_offset_handling,
149     unsigned char* view,
150     typename elfcpp::Elf_types<size>::Elf_Addr view_address,
151     off_t view_size)
152 {
153   typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
154   const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
155   Relocate relocate;
156
157   Sized_relobj<size, big_endian>* object = relinfo->object;
158   unsigned int local_count = object->local_symbol_count();
159
160   for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
161     {
162       Reltype reloc(prelocs);
163
164       off_t offset = reloc.get_r_offset();
165
166       if (needs_special_offset_handling)
167         {
168           offset = output_section->output_offset(relinfo->object,
169                                                  relinfo->data_shndx,
170                                                  offset);
171           if (offset == -1)
172             continue;
173         }
174
175       typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
176       unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
177       unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
178
179       const Sized_symbol<size>* sym;
180
181       Symbol_value<size> symval;
182       const Symbol_value<size> *psymval;
183       if (r_sym < local_count)
184         {
185           sym = NULL;
186           psymval = object->local_symbol(r_sym);
187         }
188       else
189         {
190           const Symbol* gsym = object->global_symbol(r_sym);
191           gold_assert(gsym != NULL);
192           if (gsym->is_forwarder())
193             gsym = relinfo->symtab->resolve_forwards(gsym);
194
195           sym = static_cast<const Sized_symbol<size>*>(gsym);
196           if (sym->has_symtab_index())
197             symval.set_output_symtab_index(sym->symtab_index());
198           else
199             symval.set_no_output_symtab_entry();
200           symval.set_output_value(sym->value());
201           psymval = &symval;
202         }
203
204       if (!relocate.relocate(relinfo, target, i, reloc, r_type, sym, psymval,
205                              view + offset, view_address + offset, view_size))
206         continue;
207
208       if (offset < 0 || offset >= view_size)
209         {
210           gold_error_at_location(relinfo, i, offset,
211                                  _("reloc has bad offset %zu"),
212                                  static_cast<size_t>(offset));
213           continue;
214         }
215
216       if (sym != NULL
217           && sym->is_undefined()
218           && sym->binding() != elfcpp::STB_WEAK
219           && !parameters->output_is_shared())
220         gold_undefined_symbol(sym, relinfo, i, offset);
221
222       if (sym != NULL && sym->has_warning())
223         relinfo->symtab->issue_warning(sym, relinfo, i, offset);
224     }
225 }
226
227 } // End namespace gold.
228
229 #endif // !defined(GOLD_TARGET_RELOC_H)