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