Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* BFD support for the ns32k architecture. |
250d07de | 2 | Copyright (C) 1990-2021 Free Software Foundation, Inc. |
252b5132 RH |
3 | Almost totally rewritten by Ian Dall from initial work |
4 | by Andrew Cagney. | |
5 | ||
4eb6b71c | 6 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 7 | |
4eb6b71c NC |
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 | |
cd123cb7 | 10 | the Free Software Foundation; either version 3 of the License, or |
4eb6b71c | 11 | (at your option) any later version. |
252b5132 | 12 | |
4eb6b71c NC |
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. | |
252b5132 | 17 | |
4eb6b71c NC |
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 | |
cd123cb7 NC |
20 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
21 | MA 02110-1301, USA. */ | |
252b5132 | 22 | |
252b5132 | 23 | #include "sysdep.h" |
3db64b00 | 24 | #include "bfd.h" |
252b5132 RH |
25 | #include "libbfd.h" |
26 | #include "ns32k.h" | |
27 | ||
28 | #define N(machine, printable, d, next) \ | |
aebcfb76 NC |
29 | { 32, 32, 8, bfd_arch_ns32k, machine, "ns32k",printable,3,d, \ |
30 | bfd_default_compatible,bfd_default_scan,bfd_arch_default_fill,next, 0 } | |
252b5132 RH |
31 | |
32 | static const bfd_arch_info_type arch_info_struct[] = | |
71f6b586 | 33 | { |
0a1b45a2 | 34 | N (32532, "ns32k:32532", true, 0), /* The word ns32k will match this too. */ |
252b5132 RH |
35 | }; |
36 | ||
37 | const bfd_arch_info_type bfd_ns32k_arch = | |
0a1b45a2 | 38 | N (32032, "ns32k:32032", false, &arch_info_struct[0]); |
252b5132 | 39 | |
dc810e39 | 40 | bfd_vma |
2c3fc389 | 41 | _bfd_ns32k_get_displacement (bfd_byte *buffer, int size) |
252b5132 | 42 | { |
dc810e39 | 43 | bfd_signed_vma value; |
4eb6b71c | 44 | |
252b5132 RH |
45 | switch (size) |
46 | { | |
47 | case 1: | |
dc810e39 | 48 | value = ((*buffer & 0x7f) ^ 0x40) - 0x40; |
252b5132 | 49 | break; |
dc810e39 | 50 | |
252b5132 | 51 | case 2: |
dc810e39 | 52 | value = ((*buffer++ & 0x3f) ^ 0x20) - 0x20; |
252b5132 RH |
53 | value = (value << 8) | (0xff & *buffer); |
54 | break; | |
dc810e39 | 55 | |
252b5132 | 56 | case 4: |
dc810e39 | 57 | value = ((*buffer++ & 0x3f) ^ 0x20) - 0x20; |
252b5132 RH |
58 | value = (value << 8) | (0xff & *buffer++); |
59 | value = (value << 8) | (0xff & *buffer++); | |
60 | value = (value << 8) | (0xff & *buffer); | |
61 | break; | |
dc810e39 | 62 | |
252b5132 RH |
63 | default: |
64 | abort (); | |
65 | return 0; | |
66 | } | |
4eb6b71c | 67 | |
252b5132 RH |
68 | return value; |
69 | } | |
70 | ||
4eb6b71c | 71 | void |
2c3fc389 | 72 | _bfd_ns32k_put_displacement (bfd_vma value, bfd_byte *buffer, int size) |
252b5132 | 73 | { |
252b5132 RH |
74 | switch (size) |
75 | { | |
76 | case 1: | |
dc810e39 AM |
77 | value &= 0x7f; |
78 | *buffer++ = value; | |
252b5132 | 79 | break; |
dc810e39 | 80 | |
252b5132 | 81 | case 2: |
dc810e39 AM |
82 | value &= 0x3fff; |
83 | value |= 0x8000; | |
84 | *buffer++ = (value >> 8); | |
85 | *buffer++ = value; | |
252b5132 | 86 | break; |
dc810e39 | 87 | |
252b5132 | 88 | case 4: |
dc810e39 AM |
89 | value |= (bfd_vma) 0xc0000000; |
90 | *buffer++ = (value >> 24); | |
91 | *buffer++ = (value >> 16); | |
92 | *buffer++ = (value >> 8); | |
93 | *buffer++ = value; | |
252b5132 | 94 | break; |
252b5132 | 95 | } |
4eb6b71c | 96 | return; |
252b5132 RH |
97 | } |
98 | ||
dc810e39 | 99 | bfd_vma |
2c3fc389 | 100 | _bfd_ns32k_get_immediate (bfd_byte *buffer, int size) |
252b5132 | 101 | { |
dc810e39 | 102 | bfd_vma value = 0; |
4eb6b71c | 103 | |
252b5132 RH |
104 | switch (size) |
105 | { | |
106 | case 4: | |
107 | value = (value << 8) | (*buffer++ & 0xff); | |
252b5132 | 108 | value = (value << 8) | (*buffer++ & 0xff); |
1a0670f3 | 109 | /* Fall through. */ |
252b5132 RH |
110 | case 2: |
111 | value = (value << 8) | (*buffer++ & 0xff); | |
1a0670f3 | 112 | /* Fall through. */ |
252b5132 RH |
113 | case 1: |
114 | value = (value << 8) | (*buffer++ & 0xff); | |
4eb6b71c NC |
115 | break; |
116 | default: | |
117 | abort (); | |
252b5132 RH |
118 | } |
119 | return value; | |
120 | } | |
121 | ||
4eb6b71c | 122 | void |
2c3fc389 | 123 | _bfd_ns32k_put_immediate (bfd_vma value, bfd_byte *buffer, int size) |
252b5132 | 124 | { |
dc810e39 | 125 | buffer += size - 1; |
252b5132 RH |
126 | switch (size) |
127 | { | |
128 | case 4: | |
129 | *buffer-- = (value & 0xff); value >>= 8; | |
252b5132 | 130 | *buffer-- = (value & 0xff); value >>= 8; |
1a0670f3 | 131 | /* Fall through. */ |
252b5132 RH |
132 | case 2: |
133 | *buffer-- = (value & 0xff); value >>= 8; | |
1a0670f3 | 134 | /* Fall through. */ |
252b5132 RH |
135 | case 1: |
136 | *buffer-- = (value & 0xff); value >>= 8; | |
137 | } | |
252b5132 RH |
138 | } |
139 | ||
140 | /* This is just like the standard perform_relocation except we | |
4eb6b71c NC |
141 | use get_data and put_data which know about the ns32k storage |
142 | methods. This is probably a lot more complicated than it | |
143 | needs to be! */ | |
144 | ||
252b5132 | 145 | static bfd_reloc_status_type |
2c3fc389 NC |
146 | do_ns32k_reloc (bfd * abfd, |
147 | arelent * reloc_entry, | |
148 | struct bfd_symbol * symbol, | |
149 | void * data, | |
150 | asection * input_section, | |
151 | bfd * output_bfd, | |
152 | char ** error_message ATTRIBUTE_UNUSED, | |
153 | bfd_vma (* get_data) (bfd_byte *, int), | |
154 | void (* put_data) (bfd_vma, bfd_byte *, int)) | |
252b5132 RH |
155 | { |
156 | int overflow = 0; | |
157 | bfd_vma relocation; | |
158 | bfd_reloc_status_type flag = bfd_reloc_ok; | |
159 | bfd_size_type addr = reloc_entry->address; | |
160 | bfd_vma output_base = 0; | |
161 | reloc_howto_type *howto = reloc_entry->howto; | |
162 | asection *reloc_target_output_section; | |
dc810e39 | 163 | bfd_byte *location; |
252b5132 | 164 | |
45dfa85a | 165 | if (bfd_is_abs_section (symbol->section) |
252b5132 RH |
166 | && output_bfd != (bfd *) NULL) |
167 | { | |
168 | reloc_entry->address += input_section->output_offset; | |
169 | return bfd_reloc_ok; | |
170 | } | |
171 | ||
1049f94e | 172 | /* If we are not producing relocatable output, return an error if |
252b5132 RH |
173 | the symbol is not defined. An undefined weak symbol is |
174 | considered to have a value of zero (SVR4 ABI, p. 4-27). */ | |
45dfa85a | 175 | if (bfd_is_und_section (symbol->section) |
252b5132 RH |
176 | && (symbol->flags & BSF_WEAK) == 0 |
177 | && output_bfd == (bfd *) NULL) | |
178 | flag = bfd_reloc_undefined; | |
179 | ||
252b5132 | 180 | /* Is the address of the relocation really within the section? */ |
07515404 | 181 | if (reloc_entry->address > bfd_get_section_limit (abfd, input_section)) |
252b5132 RH |
182 | return bfd_reloc_outofrange; |
183 | ||
5c4491d3 | 184 | /* Work out which section the relocation is targeted at and the |
252b5132 RH |
185 | initial relocation command value. */ |
186 | ||
187 | /* Get symbol value. (Common symbols are special.) */ | |
188 | if (bfd_is_com_section (symbol->section)) | |
189 | relocation = 0; | |
190 | else | |
191 | relocation = symbol->value; | |
192 | ||
252b5132 RH |
193 | reloc_target_output_section = symbol->section->output_section; |
194 | ||
195 | /* Convert input-section-relative symbol value to absolute. */ | |
82e51918 | 196 | if (output_bfd != NULL && ! howto->partial_inplace) |
252b5132 RH |
197 | output_base = 0; |
198 | else | |
199 | output_base = reloc_target_output_section->vma; | |
200 | ||
201 | relocation += output_base + symbol->section->output_offset; | |
202 | ||
203 | /* Add in supplied addend. */ | |
204 | relocation += reloc_entry->addend; | |
205 | ||
206 | /* Here the variable relocation holds the final address of the | |
207 | symbol we are relocating against, plus any addend. */ | |
208 | ||
82e51918 | 209 | if (howto->pc_relative) |
252b5132 RH |
210 | { |
211 | /* This is a PC relative relocation. We want to set RELOCATION | |
212 | to the distance between the address of the symbol and the | |
213 | location. RELOCATION is already the address of the symbol. | |
214 | ||
215 | We start by subtracting the address of the section containing | |
216 | the location. | |
217 | ||
218 | If pcrel_offset is set, we must further subtract the position | |
219 | of the location within the section. Some targets arrange for | |
220 | the addend to be the negative of the position of the location | |
221 | within the section; for example, i386-aout does this. For | |
b34976b6 | 222 | i386-aout, pcrel_offset is FALSE. Some other targets do not |
c2bf1eec AM |
223 | include the position of the location; for example, ELF. |
224 | For those targets, pcrel_offset is TRUE. | |
252b5132 | 225 | |
1049f94e | 226 | If we are producing relocatable output, then we must ensure |
252b5132 | 227 | that this reloc will be correctly computed when the final |
b34976b6 | 228 | relocation is done. If pcrel_offset is FALSE we want to wind |
252b5132 RH |
229 | up with the negative of the location within the section, |
230 | which means we must adjust the existing addend by the change | |
b34976b6 | 231 | in the location within the section. If pcrel_offset is TRUE |
252b5132 RH |
232 | we do not want to adjust the existing addend at all. |
233 | ||
234 | FIXME: This seems logical to me, but for the case of | |
1049f94e | 235 | producing relocatable output it is not what the code |
252b5132 RH |
236 | actually does. I don't want to change it, because it seems |
237 | far too likely that something will break. */ | |
252b5132 RH |
238 | relocation -= |
239 | input_section->output_section->vma + input_section->output_offset; | |
240 | ||
82e51918 | 241 | if (howto->pcrel_offset) |
252b5132 RH |
242 | relocation -= reloc_entry->address; |
243 | } | |
244 | ||
245 | if (output_bfd != (bfd *) NULL) | |
246 | { | |
82e51918 | 247 | if (! howto->partial_inplace) |
252b5132 RH |
248 | { |
249 | /* This is a partial relocation, and we want to apply the relocation | |
250 | to the reloc entry rather than the raw data. Modify the reloc | |
251 | inplace to reflect what we now know. */ | |
252 | reloc_entry->addend = relocation; | |
253 | reloc_entry->address += input_section->output_offset; | |
254 | return flag; | |
255 | } | |
256 | else | |
257 | { | |
258 | /* This is a partial relocation, but inplace, so modify the | |
259 | reloc record a bit. | |
260 | ||
261 | If we've relocated with a symbol with a section, change | |
262 | into a ref to the section belonging to the symbol. */ | |
263 | ||
264 | reloc_entry->address += input_section->output_offset; | |
265 | ||
266 | /* WTF?? */ | |
9bd09e22 | 267 | if (abfd->xvec->flavour == bfd_target_coff_flavour) |
252b5132 | 268 | { |
252b5132 RH |
269 | /* For m68k-coff, the addend was being subtracted twice during |
270 | relocation with -r. Removing the line below this comment | |
271 | fixes that problem; see PR 2953. | |
272 | ||
4eb6b71c NC |
273 | However, Ian wrote the following, regarding removing the line |
274 | below, which explains why it is still enabled: --djm | |
275 | ||
276 | If you put a patch like that into BFD you need to check all | |
277 | the COFF linkers. I am fairly certain that patch will break | |
278 | coff-i386 (e.g., SCO); see coff_i386_reloc in coff-i386.c | |
279 | where I worked around the problem in a different way. There | |
280 | may very well be a reason that the code works as it does. | |
281 | ||
282 | Hmmm. The first obvious point is that bfd_perform_relocation | |
283 | should not have any tests that depend upon the flavour. It's | |
284 | seem like entirely the wrong place for such a thing. The | |
285 | second obvious point is that the current code ignores the | |
1049f94e | 286 | reloc addend when producing relocatable output for COFF. |
4eb6b71c NC |
287 | That's peculiar. In fact, I really have no idea what the |
288 | point of the line you want to remove is. | |
289 | ||
290 | A typical COFF reloc subtracts the old value of the symbol | |
291 | and adds in the new value to the location in the object file | |
292 | (if it's a pc relative reloc it adds the difference between | |
293 | the symbol value and the location). When relocating we need | |
294 | to preserve that property. | |
295 | ||
296 | BFD handles this by setting the addend to the negative of the | |
297 | old value of the symbol. Unfortunately it handles common | |
298 | symbols in a non-standard way (it doesn't subtract the old | |
299 | value) but that's a different story (we can't change it | |
300 | without losing backward compatibility with old object files) | |
301 | (coff-i386 does subtract the old value, to be compatible with | |
302 | existing coff-i386 targets, like SCO). | |
303 | ||
1049f94e AM |
304 | So everything works fine when not producing relocatable |
305 | output. When we are producing relocatable output, logically | |
4eb6b71c | 306 | we should do exactly what we do when not producing |
1049f94e | 307 | relocatable output. Therefore, your patch is correct. In |
4eb6b71c NC |
308 | fact, it should probably always just set reloc_entry->addend |
309 | to 0 for all cases, since it is, in fact, going to add the | |
310 | value into the object file. This won't hurt the COFF code, | |
311 | which doesn't use the addend; I'm not sure what it will do | |
312 | to other formats (the thing to check for would be whether | |
313 | any formats both use the addend and set partial_inplace). | |
314 | ||
1049f94e | 315 | When I wanted to make coff-i386 produce relocatable output, |
4eb6b71c NC |
316 | I ran into the problem that you are running into: I wanted |
317 | to remove that line. Rather than risk it, I made the | |
318 | coff-i386 relocs use a special function; it's coff_i386_reloc | |
319 | in coff-i386.c. The function specifically adds the addend | |
320 | field into the object file, knowing that bfd_perform_relocation | |
321 | is not going to. If you remove that line, then coff-i386.c | |
322 | will wind up adding the addend field in twice. It's trivial | |
323 | to fix; it just needs to be done. | |
324 | ||
325 | The problem with removing the line is just that it may break | |
326 | some working code. With BFD it's hard to be sure of anything. | |
327 | The right way to deal with this is simply to build and test at | |
328 | least all the supported COFF targets. It should be | |
329 | straightforward if time and disk space consuming. For each | |
330 | target: | |
331 | 1) build the linker | |
332 | 2) generate some executable, and link it using -r (I would | |
333 | probably use paranoia.o and link against newlib/libc.a, | |
334 | which for all the supported targets would be available in | |
335 | /usr/cygnus/progressive/H-host/target/lib/libc.a). | |
336 | 3) make the change to reloc.c | |
337 | 4) rebuild the linker | |
338 | 5) repeat step 2 | |
339 | 6) if the resulting object files are the same, you have at | |
340 | least made it no worse | |
341 | 7) if they are different you have to figure out which | |
342 | version is right. */ | |
252b5132 | 343 | relocation -= reloc_entry->addend; |
252b5132 RH |
344 | reloc_entry->addend = 0; |
345 | } | |
346 | else | |
347 | { | |
348 | reloc_entry->addend = relocation; | |
349 | } | |
350 | } | |
351 | } | |
352 | else | |
353 | { | |
354 | reloc_entry->addend = 0; | |
355 | } | |
356 | ||
357 | /* FIXME: This overflow checking is incomplete, because the value | |
358 | might have overflowed before we get here. For a correct check we | |
359 | need to compute the value in a size larger than bitsize, but we | |
360 | can't reasonably do that for a reloc the same size as a host | |
361 | machine word. | |
362 | FIXME: We should also do overflow checking on the result after | |
363 | adding in the value contained in the object file. */ | |
364 | if (howto->complain_on_overflow != complain_overflow_dont) | |
365 | { | |
366 | bfd_vma check; | |
367 | ||
368 | /* Get the value that will be used for the relocation, but | |
369 | starting at bit position zero. */ | |
370 | if (howto->rightshift > howto->bitpos) | |
371 | check = relocation >> (howto->rightshift - howto->bitpos); | |
372 | else | |
373 | check = relocation << (howto->bitpos - howto->rightshift); | |
374 | switch (howto->complain_on_overflow) | |
375 | { | |
376 | case complain_overflow_signed: | |
377 | { | |
378 | /* Assumes two's complement. */ | |
379 | bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1; | |
380 | bfd_signed_vma reloc_signed_min = ~reloc_signed_max; | |
381 | ||
382 | /* The above right shift is incorrect for a signed value. | |
383 | Fix it up by forcing on the upper bits. */ | |
384 | if (howto->rightshift > howto->bitpos | |
385 | && (bfd_signed_vma) relocation < 0) | |
386 | check |= ((bfd_vma) - 1 | |
387 | & ~((bfd_vma) - 1 | |
388 | >> (howto->rightshift - howto->bitpos))); | |
389 | if ((bfd_signed_vma) check > reloc_signed_max | |
390 | || (bfd_signed_vma) check < reloc_signed_min) | |
391 | flag = bfd_reloc_overflow; | |
392 | } | |
393 | break; | |
394 | case complain_overflow_unsigned: | |
395 | { | |
396 | /* Assumes two's complement. This expression avoids | |
397 | overflow if howto->bitsize is the number of bits in | |
398 | bfd_vma. */ | |
399 | bfd_vma reloc_unsigned_max = | |
400 | (((1 << (howto->bitsize - 1)) - 1) << 1) | 1; | |
401 | ||
402 | if ((bfd_vma) check > reloc_unsigned_max) | |
403 | flag = bfd_reloc_overflow; | |
404 | } | |
405 | break; | |
406 | case complain_overflow_bitfield: | |
407 | { | |
408 | /* Assumes two's complement. This expression avoids | |
409 | overflow if howto->bitsize is the number of bits in | |
410 | bfd_vma. */ | |
411 | bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1; | |
412 | ||
413 | if (((bfd_vma) check & ~reloc_bits) != 0 | |
dc810e39 AM |
414 | && (((bfd_vma) check & ~reloc_bits) |
415 | != (-(bfd_vma) 1 & ~reloc_bits))) | |
252b5132 RH |
416 | { |
417 | /* The above right shift is incorrect for a signed | |
418 | value. See if turning on the upper bits fixes the | |
419 | overflow. */ | |
420 | if (howto->rightshift > howto->bitpos | |
421 | && (bfd_signed_vma) relocation < 0) | |
422 | { | |
423 | check |= ((bfd_vma) - 1 | |
424 | & ~((bfd_vma) - 1 | |
425 | >> (howto->rightshift - howto->bitpos))); | |
dc810e39 AM |
426 | if (((bfd_vma) check & ~reloc_bits) |
427 | != (-(bfd_vma) 1 & ~reloc_bits)) | |
252b5132 RH |
428 | flag = bfd_reloc_overflow; |
429 | } | |
430 | else | |
431 | flag = bfd_reloc_overflow; | |
432 | } | |
433 | } | |
434 | break; | |
435 | default: | |
436 | abort (); | |
437 | } | |
438 | } | |
439 | ||
4eb6b71c NC |
440 | /* Either we are relocating all the way, or we don't want to apply |
441 | the relocation to the reloc entry (probably because there isn't | |
442 | any room in the output format to describe addends to relocs). */ | |
252b5132 RH |
443 | |
444 | /* The cast to bfd_vma avoids a bug in the Alpha OSF/1 C compiler | |
445 | (OSF version 1.3, compiler version 3.11). It miscompiles the | |
446 | following program: | |
447 | ||
448 | struct str | |
449 | { | |
450 | unsigned int i0; | |
451 | } s = { 0 }; | |
452 | ||
453 | int | |
454 | main () | |
455 | { | |
456 | unsigned long x; | |
457 | ||
458 | x = 0x100000000; | |
459 | x <<= (unsigned long) s.i0; | |
460 | if (x == 0) | |
461 | printf ("failed\n"); | |
462 | else | |
463 | printf ("succeeded (%lx)\n", x); | |
464 | } | |
465 | */ | |
466 | ||
467 | relocation >>= (bfd_vma) howto->rightshift; | |
468 | ||
4eb6b71c | 469 | /* Shift everything up to where it's going to be used. */ |
252b5132 RH |
470 | relocation <<= (bfd_vma) howto->bitpos; |
471 | ||
4eb6b71c | 472 | /* Wait for the day when all have the mask in them. */ |
252b5132 RH |
473 | |
474 | /* What we do: | |
475 | i instruction to be left alone | |
476 | o offset within instruction | |
477 | r relocation offset to apply | |
478 | S src mask | |
479 | D dst mask | |
480 | N ~dst mask | |
481 | A part 1 | |
482 | B part 2 | |
483 | R result | |
484 | ||
485 | Do this: | |
07d6d2b8 AM |
486 | i i i i i o o o o o from bfd_get<size> |
487 | and S S S S S to get the size offset we want | |
488 | + r r r r r r r r r r to get the final value to place | |
489 | and D D D D D to chop to right size | |
252b5132 RH |
490 | ----------------------- |
491 | A A A A A | |
492 | And this: | |
07d6d2b8 AM |
493 | ... i i i i i o o o o o from bfd_get<size> |
494 | and N N N N N get instruction | |
252b5132 RH |
495 | ----------------------- |
496 | ... B B B B B | |
497 | ||
498 | And then: | |
499 | B B B B B | |
07d6d2b8 | 500 | or A A A A A |
252b5132 | 501 | ----------------------- |
07d6d2b8 | 502 | R R R R R R R R R R put into bfd_put<size>. */ |
252b5132 | 503 | |
706704c8 AM |
504 | if (howto->negate) |
505 | relocation = -relocation; | |
506 | ||
252b5132 RH |
507 | #define DOIT(x) \ |
508 | x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask)) | |
509 | ||
dc810e39 | 510 | location = (bfd_byte *) data + addr; |
252b5132 RH |
511 | switch (howto->size) |
512 | { | |
513 | case 0: | |
514 | { | |
4eb6b71c | 515 | bfd_vma x = get_data (location, 1); |
252b5132 | 516 | DOIT (x); |
4eb6b71c | 517 | put_data ((bfd_vma) x, location, 1); |
252b5132 RH |
518 | } |
519 | break; | |
520 | ||
521 | case 1: | |
522 | if (relocation) | |
523 | { | |
4eb6b71c | 524 | bfd_vma x = get_data (location, 2); |
252b5132 | 525 | DOIT (x); |
4eb6b71c | 526 | put_data ((bfd_vma) x, location, 2); |
252b5132 RH |
527 | } |
528 | break; | |
529 | case 2: | |
530 | if (relocation) | |
531 | { | |
4eb6b71c | 532 | bfd_vma x = get_data (location, 4); |
252b5132 | 533 | DOIT (x); |
4eb6b71c | 534 | put_data ((bfd_vma) x, location, 4); |
252b5132 RH |
535 | } |
536 | break; | |
252b5132 RH |
537 | |
538 | case 3: | |
4eb6b71c | 539 | /* Do nothing. */ |
252b5132 RH |
540 | break; |
541 | ||
542 | case 4: | |
543 | #ifdef BFD64 | |
544 | if (relocation) | |
545 | { | |
dc810e39 | 546 | bfd_vma x = get_data (location, 8); |
252b5132 | 547 | DOIT (x); |
4eb6b71c | 548 | put_data (x, location, 8); |
252b5132 RH |
549 | } |
550 | #else | |
551 | abort (); | |
552 | #endif | |
553 | break; | |
554 | default: | |
555 | return bfd_reloc_other; | |
556 | } | |
557 | if ((howto->complain_on_overflow != complain_overflow_dont) && overflow) | |
558 | return bfd_reloc_overflow; | |
559 | ||
560 | return flag; | |
561 | } | |
562 | ||
563 | /* Relocate a given location using a given value and howto. */ | |
564 | ||
565 | bfd_reloc_status_type | |
2c3fc389 NC |
566 | _bfd_do_ns32k_reloc_contents (reloc_howto_type *howto, |
567 | bfd *input_bfd ATTRIBUTE_UNUSED, | |
568 | bfd_vma relocation, | |
569 | bfd_byte *location, | |
570 | bfd_vma (*get_data) (bfd_byte *, int), | |
571 | void (*put_data) (bfd_vma, bfd_byte *, int)) | |
252b5132 RH |
572 | { |
573 | int size; | |
574 | bfd_vma x; | |
0a1b45a2 | 575 | bool overflow; |
252b5132 | 576 | |
fde0214a | 577 | if (howto->negate) |
252b5132 RH |
578 | relocation = -relocation; |
579 | ||
580 | /* Get the value we are going to relocate. */ | |
581 | size = bfd_get_reloc_size (howto); | |
582 | switch (size) | |
583 | { | |
584 | default: | |
252b5132 | 585 | abort (); |
6346d5ca AM |
586 | case 0: |
587 | return bfd_reloc_ok; | |
252b5132 RH |
588 | case 1: |
589 | case 2: | |
590 | case 4: | |
591 | #ifdef BFD64 | |
592 | case 8: | |
593 | #endif | |
dc810e39 | 594 | x = get_data (location, size); |
252b5132 RH |
595 | break; |
596 | } | |
597 | ||
598 | /* Check for overflow. FIXME: We may drop bits during the addition | |
599 | which we don't check for. We must either check at every single | |
600 | operation, which would be tedious, or we must do the computations | |
601 | in a type larger than bfd_vma, which would be inefficient. */ | |
0a1b45a2 | 602 | overflow = false; |
252b5132 RH |
603 | if (howto->complain_on_overflow != complain_overflow_dont) |
604 | { | |
605 | bfd_vma check; | |
606 | bfd_signed_vma signed_check; | |
607 | bfd_vma add; | |
608 | bfd_signed_vma signed_add; | |
609 | ||
610 | if (howto->rightshift == 0) | |
611 | { | |
612 | check = relocation; | |
613 | signed_check = (bfd_signed_vma) relocation; | |
614 | } | |
615 | else | |
616 | { | |
617 | /* Drop unwanted bits from the value we are relocating to. */ | |
618 | check = relocation >> howto->rightshift; | |
619 | ||
620 | /* If this is a signed value, the rightshift just dropped | |
621 | leading 1 bits (assuming twos complement). */ | |
622 | if ((bfd_signed_vma) relocation >= 0) | |
623 | signed_check = check; | |
624 | else | |
625 | signed_check = (check | |
626 | | ((bfd_vma) - 1 | |
627 | & ~((bfd_vma) - 1 >> howto->rightshift))); | |
628 | } | |
629 | ||
630 | /* Get the value from the object file. */ | |
631 | add = x & howto->src_mask; | |
632 | ||
633 | /* Get the value from the object file with an appropriate sign. | |
634 | The expression involving howto->src_mask isolates the upper | |
635 | bit of src_mask. If that bit is set in the value we are | |
636 | adding, it is negative, and we subtract out that number times | |
637 | two. If src_mask includes the highest possible bit, then we | |
638 | can not get the upper bit, but that does not matter since | |
639 | signed_add needs no adjustment to become negative in that | |
640 | case. */ | |
641 | signed_add = add; | |
642 | if ((add & (((~howto->src_mask) >> 1) & howto->src_mask)) != 0) | |
643 | signed_add -= (((~howto->src_mask) >> 1) & howto->src_mask) << 1; | |
644 | ||
645 | /* Add the value from the object file, shifted so that it is a | |
646 | straight number. */ | |
647 | if (howto->bitpos == 0) | |
648 | { | |
649 | check += add; | |
650 | signed_check += signed_add; | |
651 | } | |
652 | else | |
653 | { | |
654 | check += add >> howto->bitpos; | |
655 | ||
656 | /* For the signed case we use ADD, rather than SIGNED_ADD, | |
657 | to avoid warnings from SVR4 cc. This is OK since we | |
5c4491d3 | 658 | explicitly handle the sign bits. */ |
252b5132 RH |
659 | if (signed_add >= 0) |
660 | signed_check += add >> howto->bitpos; | |
661 | else | |
662 | signed_check += ((add >> howto->bitpos) | |
663 | | ((bfd_vma) - 1 | |
664 | & ~((bfd_vma) - 1 >> howto->bitpos))); | |
665 | } | |
666 | ||
667 | switch (howto->complain_on_overflow) | |
668 | { | |
669 | case complain_overflow_signed: | |
670 | { | |
671 | /* Assumes two's complement. */ | |
672 | bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1; | |
673 | bfd_signed_vma reloc_signed_min = ~reloc_signed_max; | |
674 | ||
675 | if (signed_check > reloc_signed_max | |
676 | || signed_check < reloc_signed_min) | |
0a1b45a2 | 677 | overflow = true; |
252b5132 RH |
678 | } |
679 | break; | |
680 | case complain_overflow_unsigned: | |
681 | { | |
682 | /* Assumes two's complement. This expression avoids | |
683 | overflow if howto->bitsize is the number of bits in | |
684 | bfd_vma. */ | |
685 | bfd_vma reloc_unsigned_max = | |
686 | (((1 << (howto->bitsize - 1)) - 1) << 1) | 1; | |
687 | ||
688 | if (check > reloc_unsigned_max) | |
0a1b45a2 | 689 | overflow = true; |
252b5132 RH |
690 | } |
691 | break; | |
692 | case complain_overflow_bitfield: | |
693 | { | |
694 | /* Assumes two's complement. This expression avoids | |
695 | overflow if howto->bitsize is the number of bits in | |
696 | bfd_vma. */ | |
697 | bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1; | |
698 | ||
699 | if ((check & ~reloc_bits) != 0 | |
700 | && (((bfd_vma) signed_check & ~reloc_bits) | |
dc810e39 | 701 | != (-(bfd_vma) 1 & ~reloc_bits))) |
0a1b45a2 | 702 | overflow = true; |
252b5132 RH |
703 | } |
704 | break; | |
705 | default: | |
706 | abort (); | |
707 | } | |
708 | } | |
709 | ||
710 | /* Put RELOCATION in the right bits. */ | |
711 | relocation >>= (bfd_vma) howto->rightshift; | |
712 | relocation <<= (bfd_vma) howto->bitpos; | |
713 | ||
714 | /* Add RELOCATION to the right bits of X. */ | |
715 | x = ((x & ~howto->dst_mask) | |
716 | | (((x & howto->src_mask) + relocation) & howto->dst_mask)); | |
717 | ||
718 | /* Put the relocated value back in the object file. */ | |
719 | switch (size) | |
720 | { | |
721 | default: | |
722 | case 0: | |
723 | abort (); | |
724 | case 1: | |
725 | case 2: | |
726 | case 4: | |
727 | #ifdef BFD64 | |
728 | case 8: | |
729 | #endif | |
dc810e39 | 730 | put_data (x, location, size); |
252b5132 RH |
731 | break; |
732 | } | |
733 | ||
734 | return overflow ? bfd_reloc_overflow : bfd_reloc_ok; | |
735 | } | |
736 | ||
737 | bfd_reloc_status_type | |
2c3fc389 NC |
738 | _bfd_ns32k_reloc_disp (bfd *abfd, |
739 | arelent *reloc_entry, | |
740 | struct bfd_symbol *symbol, | |
741 | void * data, | |
742 | asection *input_section, | |
743 | bfd *output_bfd, | |
744 | char **error_message) | |
252b5132 RH |
745 | { |
746 | return do_ns32k_reloc (abfd, reloc_entry, symbol, data, input_section, | |
747 | output_bfd, error_message, | |
748 | _bfd_ns32k_get_displacement, | |
749 | _bfd_ns32k_put_displacement); | |
750 | } | |
751 | ||
752 | bfd_reloc_status_type | |
2c3fc389 NC |
753 | _bfd_ns32k_reloc_imm (bfd *abfd, |
754 | arelent *reloc_entry, | |
755 | struct bfd_symbol *symbol, | |
756 | void * data, | |
757 | asection *input_section, | |
758 | bfd *output_bfd, | |
759 | char **error_message) | |
252b5132 RH |
760 | { |
761 | return do_ns32k_reloc (abfd, reloc_entry, symbol, data, input_section, | |
762 | output_bfd, error_message, _bfd_ns32k_get_immediate, | |
763 | _bfd_ns32k_put_immediate); | |
764 | } | |
765 | ||
766 | bfd_reloc_status_type | |
2c3fc389 NC |
767 | _bfd_ns32k_final_link_relocate (reloc_howto_type *howto, |
768 | bfd *input_bfd, | |
769 | asection *input_section, | |
770 | bfd_byte *contents, | |
771 | bfd_vma address, | |
772 | bfd_vma value, | |
773 | bfd_vma addend) | |
252b5132 RH |
774 | { |
775 | bfd_vma relocation; | |
776 | ||
777 | /* Sanity check the address. */ | |
07515404 | 778 | if (address > bfd_get_section_limit (input_bfd, input_section)) |
252b5132 RH |
779 | return bfd_reloc_outofrange; |
780 | ||
781 | /* This function assumes that we are dealing with a basic relocation | |
782 | against a symbol. We want to compute the value of the symbol to | |
783 | relocate to. This is just VALUE, the value of the symbol, plus | |
784 | ADDEND, any addend associated with the reloc. */ | |
785 | relocation = value + addend; | |
786 | ||
787 | /* If the relocation is PC relative, we want to set RELOCATION to | |
788 | the distance between the symbol (currently in RELOCATION) and the | |
c2bf1eec | 789 | location we are relocating. If pcrel_offset is FALSE we do not |
252b5132 RH |
790 | need to subtract out the offset of the location within the |
791 | section (which is just ADDRESS). */ | |
792 | if (howto->pc_relative) | |
793 | { | |
794 | relocation -= (input_section->output_section->vma | |
795 | + input_section->output_offset); | |
796 | if (howto->pcrel_offset) | |
797 | relocation -= address; | |
798 | } | |
799 | ||
800 | return _bfd_ns32k_relocate_contents (howto, input_bfd, relocation, | |
801 | contents + address); | |
802 | } |