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