* gas/mips/mips.exp: Invoke the tests smartmips, mips32-dsp,
[deliverable/binutils-gdb.git] / gold / target-reloc.h
CommitLineData
61ba1cf9
ILT
1// target-reloc.h -- target specific relocation support -*- C++ -*-
2
6cb15b7f
ILT
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
61ba1cf9
ILT
23#ifndef GOLD_TARGET_RELOC_H
24#define GOLD_TARGET_RELOC_H
25
26#include "elfcpp.h"
27#include "symtab.h"
c06b7b0b 28#include "reloc-types.h"
61ba1cf9
ILT
29
30namespace gold
31{
32
92e059d8 33// This function implements the generic part of reloc scanning. This
436ca963
ILT
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.
92e059d8 38
ead1e424
ILT
39template<int size, bool big_endian, typename Target_type, int sh_type,
40 typename Scan>
92e059d8
ILT
41inline void
42scan_relocs(
43 const General_options& options,
44 Symbol_table* symtab,
ead1e424
ILT
45 Layout* layout,
46 Target_type* target,
f6ce93d6 47 Sized_relobj<size, big_endian>* object,
a3ad94ed 48 unsigned int data_shndx,
92e059d8
ILT
49 const unsigned char* prelocs,
50 size_t reloc_count,
730cdc88
ILT
51 Output_section* output_section,
52 bool needs_special_offset_handling,
92e059d8 53 size_t local_count,
730cdc88 54 const unsigned char* plocal_syms)
92e059d8
ILT
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
730cdc88
ILT
65 if (needs_special_offset_handling
66 && !output_section->is_input_address_mapped(object, data_shndx,
67 reloc.get_r_offset()))
68 continue;
69
92e059d8
ILT
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 {
a3ad94ed 76 gold_assert(plocal_syms != NULL);
92e059d8
ILT
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
ead1e424 81 && shndx != elfcpp::SHN_UNDEF
92e059d8
ILT
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
a3ad94ed 102 scan.local(options, symtab, layout, target, object, data_shndx,
07f397ab 103 output_section, reloc, r_type, lsym);
92e059d8
ILT
104 }
105 else
106 {
730cdc88 107 Symbol* gsym = object->global_symbol(r_sym);
a3ad94ed 108 gold_assert(gsym != NULL);
92e059d8
ILT
109 if (gsym->is_forwarder())
110 gsym = symtab->resolve_forwards(gsym);
111
a3ad94ed 112 scan.global(options, symtab, layout, target, object, data_shndx,
07f397ab 113 output_section, reloc, r_type, gsym);
92e059d8
ILT
114 }
115 }
116}
117
118// This function implements the generic part of relocation processing.
72a2eed7 119// This is an inline function which take a class whose relocate()
61ba1cf9
ILT
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
92e059d8
ILT
126// the data. SH_TYPE is the section type: SHT_REL or SHT_RELA.
127// RELOCATE implements operator() to do a relocation.
61ba1cf9 128
92e059d8 129// PRELOCS points to the relocation data. RELOC_COUNT is the number
730cdc88
ILT
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.
61ba1cf9 138
ead1e424
ILT
139template<int size, bool big_endian, typename Target_type, int sh_type,
140 typename Relocate>
61ba1cf9
ILT
141inline void
142relocate_section(
92e059d8 143 const Relocate_info<size, big_endian>* relinfo,
ead1e424 144 Target_type* target,
61ba1cf9
ILT
145 const unsigned char* prelocs,
146 size_t reloc_count,
730cdc88
ILT
147 Output_section* output_section,
148 bool needs_special_offset_handling,
61ba1cf9
ILT
149 unsigned char* view,
150 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
fe8718a4 151 section_size_type view_size)
61ba1cf9
ILT
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
730cdc88
ILT
157 Sized_relobj<size, big_endian>* object = relinfo->object;
158 unsigned int local_count = object->local_symbol_count();
92e059d8 159
61ba1cf9
ILT
160 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
161 {
162 Reltype reloc(prelocs);
163
fe8718a4
ILT
164 section_offset_type offset =
165 convert_to_section_size_type(reloc.get_r_offset());
61ba1cf9 166
730cdc88
ILT
167 if (needs_special_offset_handling)
168 {
169 offset = output_section->output_offset(relinfo->object,
170 relinfo->data_shndx,
171 offset);
172 if (offset == -1)
173 continue;
174 }
175
61ba1cf9
ILT
176 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
177 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
178 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
179
c06b7b0b 180 const Sized_symbol<size>* sym;
61ba1cf9 181
b8e6aad9
ILT
182 Symbol_value<size> symval;
183 const Symbol_value<size> *psymval;
61ba1cf9
ILT
184 if (r_sym < local_count)
185 {
186 sym = NULL;
730cdc88 187 psymval = object->local_symbol(r_sym);
61ba1cf9
ILT
188 }
189 else
190 {
730cdc88 191 const Symbol* gsym = object->global_symbol(r_sym);
a3ad94ed 192 gold_assert(gsym != NULL);
61ba1cf9 193 if (gsym->is_forwarder())
92e059d8 194 gsym = relinfo->symtab->resolve_forwards(gsym);
61ba1cf9 195
c06b7b0b 196 sym = static_cast<const Sized_symbol<size>*>(gsym);
b8e6aad9
ILT
197 if (sym->has_symtab_index())
198 symval.set_output_symtab_index(sym->symtab_index());
199 else
200 symval.set_no_output_symtab_entry();
201 symval.set_output_value(sym->value());
202 psymval = &symval;
ead1e424 203 }
61ba1cf9 204
b8e6aad9 205 if (!relocate.relocate(relinfo, target, i, reloc, r_type, sym, psymval,
ead1e424
ILT
206 view + offset, view_address + offset, view_size))
207 continue;
208
fe8718a4 209 if (offset < 0 || static_cast<section_size_type>(offset) >= view_size)
ead1e424 210 {
75f2446e
ILT
211 gold_error_at_location(relinfo, i, offset,
212 _("reloc has bad offset %zu"),
213 static_cast<size_t>(offset));
214 continue;
61ba1cf9
ILT
215 }
216
ead1e424
ILT
217 if (sym != NULL
218 && sym->is_undefined()
436ca963
ILT
219 && sym->binding() != elfcpp::STB_WEAK
220 && !parameters->output_is_shared())
75f2446e 221 gold_undefined_symbol(sym, relinfo, i, offset);
f6ce93d6
ILT
222
223 if (sym != NULL && sym->has_warning())
75f2446e 224 relinfo->symtab->issue_warning(sym, relinfo, i, offset);
61ba1cf9
ILT
225 }
226}
227
228} // End namespace gold.
229
230#endif // !defined(GOLD_TARGET_RELOC_H)
This page took 0.084419 seconds and 4 git commands to generate.