1 |
24 |
jeremybenn |
/* 8 and 16 bit COFF relocation functions, for BFD.
|
2 |
|
|
Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 2000, 2001,
|
3 |
|
|
2002, 2003, 2004, 2007 Free Software Foundation, Inc.
|
4 |
|
|
Written by Cygnus Support.
|
5 |
|
|
|
6 |
|
|
This file is part of BFD, the Binary File Descriptor library.
|
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 |
|
|
|
24 |
|
|
/* Most of this hacked by Steve Chamberlain <sac@cygnus.com>. */
|
25 |
|
|
|
26 |
|
|
/* These routines are used by coff-h8300 and coff-z8k to do
|
27 |
|
|
relocation.
|
28 |
|
|
|
29 |
|
|
FIXME: This code should be rewritten to support the new COFF
|
30 |
|
|
linker. Basically, they need to deal with COFF relocs rather than
|
31 |
|
|
BFD generic relocs. They should store the relocs in some location
|
32 |
|
|
where coff_link_input_bfd can find them (and coff_link_input_bfd
|
33 |
|
|
should be changed to use this location rather than rereading the
|
34 |
|
|
file) (unless info->keep_memory is FALSE, in which case they should
|
35 |
|
|
free up the relocs after dealing with them). */
|
36 |
|
|
|
37 |
|
|
#include "sysdep.h"
|
38 |
|
|
#include "bfd.h"
|
39 |
|
|
#include "libbfd.h"
|
40 |
|
|
#include "bfdlink.h"
|
41 |
|
|
#include "genlink.h"
|
42 |
|
|
#include "coff/internal.h"
|
43 |
|
|
#include "libcoff.h"
|
44 |
|
|
|
45 |
|
|
bfd_vma
|
46 |
|
|
bfd_coff_reloc16_get_value (reloc, link_info, input_section)
|
47 |
|
|
arelent *reloc;
|
48 |
|
|
struct bfd_link_info *link_info;
|
49 |
|
|
asection *input_section;
|
50 |
|
|
{
|
51 |
|
|
bfd_vma value;
|
52 |
|
|
asymbol *symbol = *(reloc->sym_ptr_ptr);
|
53 |
|
|
/* A symbol holds a pointer to a section, and an offset from the
|
54 |
|
|
base of the section. To relocate, we find where the section will
|
55 |
|
|
live in the output and add that in. */
|
56 |
|
|
|
57 |
|
|
if (bfd_is_und_section (symbol->section)
|
58 |
|
|
|| bfd_is_com_section (symbol->section))
|
59 |
|
|
{
|
60 |
|
|
struct bfd_link_hash_entry *h;
|
61 |
|
|
|
62 |
|
|
/* The symbol is undefined in this BFD. Look it up in the
|
63 |
|
|
global linker hash table. FIXME: This should be changed when
|
64 |
|
|
we convert this stuff to use a specific final_link function
|
65 |
|
|
and change the interface to bfd_relax_section to not require
|
66 |
|
|
the generic symbols. */
|
67 |
|
|
h = bfd_wrapped_link_hash_lookup (input_section->owner, link_info,
|
68 |
|
|
bfd_asymbol_name (symbol),
|
69 |
|
|
FALSE, FALSE, TRUE);
|
70 |
|
|
if (h != (struct bfd_link_hash_entry *) NULL
|
71 |
|
|
&& (h->type == bfd_link_hash_defined
|
72 |
|
|
|| h->type == bfd_link_hash_defweak))
|
73 |
|
|
value = (h->u.def.value
|
74 |
|
|
+ h->u.def.section->output_section->vma
|
75 |
|
|
+ h->u.def.section->output_offset);
|
76 |
|
|
else if (h != (struct bfd_link_hash_entry *) NULL
|
77 |
|
|
&& h->type == bfd_link_hash_common)
|
78 |
|
|
value = h->u.c.size;
|
79 |
|
|
else
|
80 |
|
|
{
|
81 |
|
|
if (!((*link_info->callbacks->undefined_symbol)
|
82 |
|
|
(link_info, bfd_asymbol_name (symbol),
|
83 |
|
|
input_section->owner, input_section, reloc->address,
|
84 |
|
|
TRUE)))
|
85 |
|
|
abort ();
|
86 |
|
|
value = 0;
|
87 |
|
|
}
|
88 |
|
|
}
|
89 |
|
|
else
|
90 |
|
|
{
|
91 |
|
|
value = symbol->value
|
92 |
|
|
+ symbol->section->output_offset
|
93 |
|
|
+ symbol->section->output_section->vma;
|
94 |
|
|
}
|
95 |
|
|
|
96 |
|
|
/* Add the value contained in the relocation. */
|
97 |
|
|
value += reloc->addend;
|
98 |
|
|
|
99 |
|
|
return value;
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
void
|
103 |
|
|
bfd_perform_slip (abfd, slip, input_section, value)
|
104 |
|
|
bfd *abfd;
|
105 |
|
|
unsigned int slip;
|
106 |
|
|
asection *input_section;
|
107 |
|
|
bfd_vma value;
|
108 |
|
|
{
|
109 |
|
|
asymbol **s;
|
110 |
|
|
|
111 |
|
|
s = _bfd_generic_link_get_symbols (abfd);
|
112 |
|
|
BFD_ASSERT (s != (asymbol **) NULL);
|
113 |
|
|
|
114 |
|
|
/* Find all symbols past this point, and make them know
|
115 |
|
|
what's happened. */
|
116 |
|
|
while (*s)
|
117 |
|
|
{
|
118 |
|
|
asymbol *p = *s;
|
119 |
|
|
if (p->section == input_section)
|
120 |
|
|
{
|
121 |
|
|
/* This was pointing into this section, so mangle it. */
|
122 |
|
|
if (p->value > value)
|
123 |
|
|
{
|
124 |
|
|
p->value -= slip;
|
125 |
|
|
if (p->udata.p != NULL)
|
126 |
|
|
{
|
127 |
|
|
struct generic_link_hash_entry *h;
|
128 |
|
|
|
129 |
|
|
h = (struct generic_link_hash_entry *) p->udata.p;
|
130 |
|
|
BFD_ASSERT (h->root.type == bfd_link_hash_defined
|
131 |
|
|
|| h->root.type == bfd_link_hash_defweak);
|
132 |
|
|
h->root.u.def.value -= slip;
|
133 |
|
|
BFD_ASSERT (h->root.u.def.value == p->value);
|
134 |
|
|
}
|
135 |
|
|
}
|
136 |
|
|
}
|
137 |
|
|
s++;
|
138 |
|
|
}
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
bfd_boolean
|
142 |
|
|
bfd_coff_reloc16_relax_section (abfd, input_section, link_info, again)
|
143 |
|
|
bfd *abfd;
|
144 |
|
|
asection *input_section;
|
145 |
|
|
struct bfd_link_info *link_info;
|
146 |
|
|
bfd_boolean *again;
|
147 |
|
|
{
|
148 |
|
|
/* Get enough memory to hold the stuff. */
|
149 |
|
|
bfd *input_bfd = input_section->owner;
|
150 |
|
|
unsigned *shrinks;
|
151 |
|
|
unsigned shrink = 0;
|
152 |
|
|
long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
|
153 |
|
|
arelent **reloc_vector = NULL;
|
154 |
|
|
long reloc_count;
|
155 |
|
|
|
156 |
|
|
/* We only do global relaxation once. It is not safe to do it multiple
|
157 |
|
|
times (see discussion of the "shrinks" array below). */
|
158 |
|
|
*again = FALSE;
|
159 |
|
|
|
160 |
|
|
if (reloc_size < 0)
|
161 |
|
|
return FALSE;
|
162 |
|
|
|
163 |
|
|
reloc_vector = (arelent **) bfd_malloc ((bfd_size_type) reloc_size);
|
164 |
|
|
if (!reloc_vector && reloc_size > 0)
|
165 |
|
|
return FALSE;
|
166 |
|
|
|
167 |
|
|
/* Get the relocs and think about them. */
|
168 |
|
|
reloc_count =
|
169 |
|
|
bfd_canonicalize_reloc (input_bfd, input_section, reloc_vector,
|
170 |
|
|
_bfd_generic_link_get_symbols (input_bfd));
|
171 |
|
|
if (reloc_count < 0)
|
172 |
|
|
{
|
173 |
|
|
free (reloc_vector);
|
174 |
|
|
return FALSE;
|
175 |
|
|
}
|
176 |
|
|
|
177 |
|
|
/* The reloc16.c and related relaxing code is very simple, the price
|
178 |
|
|
for that simplicity is we can only call this function once for
|
179 |
|
|
each section.
|
180 |
|
|
|
181 |
|
|
So, to get the best results within that limitation, we do multiple
|
182 |
|
|
relaxing passes over each section here. That involves keeping track
|
183 |
|
|
of the "shrink" at each reloc in the section. This allows us to
|
184 |
|
|
accurately determine the relative location of two relocs within
|
185 |
|
|
this section.
|
186 |
|
|
|
187 |
|
|
In theory, if we kept the "shrinks" array for each section for the
|
188 |
|
|
entire link, we could use the generic relaxing code in the linker
|
189 |
|
|
and get better results, particularly for jsr->bsr and 24->16 bit
|
190 |
|
|
memory reference relaxations. */
|
191 |
|
|
|
192 |
|
|
if (reloc_count > 0)
|
193 |
|
|
{
|
194 |
|
|
int another_pass = 0;
|
195 |
|
|
bfd_size_type amt;
|
196 |
|
|
|
197 |
|
|
/* Allocate and initialize the shrinks array for this section.
|
198 |
|
|
The last element is used as an accumulator of shrinks. */
|
199 |
|
|
amt = reloc_count + 1;
|
200 |
|
|
amt *= sizeof (unsigned);
|
201 |
|
|
shrinks = (unsigned *) bfd_zmalloc (amt);
|
202 |
|
|
|
203 |
|
|
/* Loop until nothing changes in this section. */
|
204 |
|
|
do
|
205 |
|
|
{
|
206 |
|
|
arelent **parent;
|
207 |
|
|
unsigned int i;
|
208 |
|
|
long j;
|
209 |
|
|
|
210 |
|
|
another_pass = 0;
|
211 |
|
|
|
212 |
|
|
for (i = 0, parent = reloc_vector; *parent; parent++, i++)
|
213 |
|
|
{
|
214 |
|
|
/* Let the target/machine dependent code examine each reloc
|
215 |
|
|
in this section and attempt to shrink it. */
|
216 |
|
|
shrink = bfd_coff_reloc16_estimate (abfd, input_section, *parent,
|
217 |
|
|
shrinks[i], link_info);
|
218 |
|
|
|
219 |
|
|
/* If it shrunk, note it in the shrinks array and set up for
|
220 |
|
|
another pass. */
|
221 |
|
|
if (shrink != shrinks[i])
|
222 |
|
|
{
|
223 |
|
|
another_pass = 1;
|
224 |
|
|
for (j = i + 1; j <= reloc_count; j++)
|
225 |
|
|
shrinks[j] += shrink - shrinks[i];
|
226 |
|
|
}
|
227 |
|
|
}
|
228 |
|
|
}
|
229 |
|
|
while (another_pass);
|
230 |
|
|
|
231 |
|
|
shrink = shrinks[reloc_count];
|
232 |
|
|
free ((char *) shrinks);
|
233 |
|
|
}
|
234 |
|
|
|
235 |
|
|
input_section->rawsize = input_section->size;
|
236 |
|
|
input_section->size -= shrink;
|
237 |
|
|
free ((char *) reloc_vector);
|
238 |
|
|
return TRUE;
|
239 |
|
|
}
|
240 |
|
|
|
241 |
|
|
bfd_byte *
|
242 |
|
|
bfd_coff_reloc16_get_relocated_section_contents (in_abfd,
|
243 |
|
|
link_info,
|
244 |
|
|
link_order,
|
245 |
|
|
data,
|
246 |
|
|
relocatable,
|
247 |
|
|
symbols)
|
248 |
|
|
bfd *in_abfd;
|
249 |
|
|
struct bfd_link_info *link_info;
|
250 |
|
|
struct bfd_link_order *link_order;
|
251 |
|
|
bfd_byte *data;
|
252 |
|
|
bfd_boolean relocatable;
|
253 |
|
|
asymbol **symbols;
|
254 |
|
|
{
|
255 |
|
|
/* Get enough memory to hold the stuff. */
|
256 |
|
|
bfd *input_bfd = link_order->u.indirect.section->owner;
|
257 |
|
|
asection *input_section = link_order->u.indirect.section;
|
258 |
|
|
long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
|
259 |
|
|
arelent **reloc_vector;
|
260 |
|
|
long reloc_count;
|
261 |
|
|
bfd_size_type sz;
|
262 |
|
|
|
263 |
|
|
if (reloc_size < 0)
|
264 |
|
|
return NULL;
|
265 |
|
|
|
266 |
|
|
/* If producing relocatable output, don't bother to relax. */
|
267 |
|
|
if (relocatable)
|
268 |
|
|
return bfd_generic_get_relocated_section_contents (in_abfd, link_info,
|
269 |
|
|
link_order,
|
270 |
|
|
data, relocatable,
|
271 |
|
|
symbols);
|
272 |
|
|
|
273 |
|
|
/* Read in the section. */
|
274 |
|
|
sz = input_section->rawsize ? input_section->rawsize : input_section->size;
|
275 |
|
|
if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
|
276 |
|
|
return NULL;
|
277 |
|
|
|
278 |
|
|
reloc_vector = (arelent **) bfd_malloc ((bfd_size_type) reloc_size);
|
279 |
|
|
if (!reloc_vector && reloc_size != 0)
|
280 |
|
|
return NULL;
|
281 |
|
|
|
282 |
|
|
reloc_count = bfd_canonicalize_reloc (input_bfd,
|
283 |
|
|
input_section,
|
284 |
|
|
reloc_vector,
|
285 |
|
|
symbols);
|
286 |
|
|
if (reloc_count < 0)
|
287 |
|
|
{
|
288 |
|
|
free (reloc_vector);
|
289 |
|
|
return NULL;
|
290 |
|
|
}
|
291 |
|
|
|
292 |
|
|
if (reloc_count > 0)
|
293 |
|
|
{
|
294 |
|
|
arelent **parent = reloc_vector;
|
295 |
|
|
arelent *reloc;
|
296 |
|
|
unsigned int dst_address = 0;
|
297 |
|
|
unsigned int src_address = 0;
|
298 |
|
|
unsigned int run;
|
299 |
|
|
unsigned int idx;
|
300 |
|
|
|
301 |
|
|
/* Find how long a run we can do. */
|
302 |
|
|
while (dst_address < link_order->size)
|
303 |
|
|
{
|
304 |
|
|
reloc = *parent;
|
305 |
|
|
if (reloc)
|
306 |
|
|
{
|
307 |
|
|
/* Note that the relaxing didn't tie up the addresses in the
|
308 |
|
|
relocation, so we use the original address to work out the
|
309 |
|
|
run of non-relocated data. */
|
310 |
|
|
run = reloc->address - src_address;
|
311 |
|
|
parent++;
|
312 |
|
|
}
|
313 |
|
|
else
|
314 |
|
|
{
|
315 |
|
|
run = link_order->size - dst_address;
|
316 |
|
|
}
|
317 |
|
|
|
318 |
|
|
/* Copy the bytes. */
|
319 |
|
|
for (idx = 0; idx < run; idx++)
|
320 |
|
|
data[dst_address++] = data[src_address++];
|
321 |
|
|
|
322 |
|
|
/* Now do the relocation. */
|
323 |
|
|
if (reloc)
|
324 |
|
|
{
|
325 |
|
|
bfd_coff_reloc16_extra_cases (input_bfd, link_info, link_order,
|
326 |
|
|
reloc, data, &src_address,
|
327 |
|
|
&dst_address);
|
328 |
|
|
}
|
329 |
|
|
}
|
330 |
|
|
}
|
331 |
|
|
free ((char *) reloc_vector);
|
332 |
|
|
return data;
|
333 |
|
|
}
|