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