/src/binutils-gdb/bfd/cpu-ns32k.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* BFD support for the ns32k architecture. |
2 | | Copyright (C) 1990-2025 Free Software Foundation, Inc. |
3 | | Almost totally rewritten by Ian Dall from initial work |
4 | | by Andrew Cagney. |
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 | | #include "sysdep.h" |
24 | | #include "bfd.h" |
25 | | #include "libbfd.h" |
26 | | #include "ns32k.h" |
27 | | |
28 | | #define N(machine, printable, d, next) \ |
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 } |
31 | | |
32 | | static const bfd_arch_info_type arch_info_struct[] = |
33 | | { |
34 | | N (32532, "ns32k:32532", true, 0), /* The word ns32k will match this too. */ |
35 | | }; |
36 | | |
37 | | const bfd_arch_info_type bfd_ns32k_arch = |
38 | | N (32032, "ns32k:32032", false, &arch_info_struct[0]); |
39 | | |
40 | | bfd_vma |
41 | | _bfd_ns32k_get_displacement (bfd_byte *buffer, int size) |
42 | 0 | { |
43 | 0 | bfd_signed_vma value; |
44 | |
|
45 | 0 | switch (size) |
46 | 0 | { |
47 | 0 | case 1: |
48 | 0 | value = ((*buffer & 0x7f) ^ 0x40) - 0x40; |
49 | 0 | break; |
50 | | |
51 | 0 | case 2: |
52 | 0 | value = ((*buffer++ & 0x3f) ^ 0x20) - 0x20; |
53 | 0 | value = (value << 8) | (0xff & *buffer); |
54 | 0 | break; |
55 | | |
56 | 0 | case 4: |
57 | 0 | value = ((*buffer++ & 0x3f) ^ 0x20) - 0x20; |
58 | 0 | value = (value << 8) | (0xff & *buffer++); |
59 | 0 | value = (value << 8) | (0xff & *buffer++); |
60 | 0 | value = (value << 8) | (0xff & *buffer); |
61 | 0 | break; |
62 | | |
63 | 0 | default: |
64 | 0 | abort (); |
65 | 0 | return 0; |
66 | 0 | } |
67 | | |
68 | 0 | return value; |
69 | 0 | } |
70 | | |
71 | | void |
72 | | _bfd_ns32k_put_displacement (bfd_vma value, bfd_byte *buffer, int size) |
73 | 0 | { |
74 | 0 | switch (size) |
75 | 0 | { |
76 | 0 | case 1: |
77 | 0 | value &= 0x7f; |
78 | 0 | *buffer++ = value; |
79 | 0 | break; |
80 | | |
81 | 0 | case 2: |
82 | 0 | value &= 0x3fff; |
83 | 0 | value |= 0x8000; |
84 | 0 | *buffer++ = (value >> 8); |
85 | 0 | *buffer++ = value; |
86 | 0 | break; |
87 | | |
88 | 0 | case 4: |
89 | 0 | value |= (bfd_vma) 0xc0000000; |
90 | 0 | *buffer++ = (value >> 24); |
91 | 0 | *buffer++ = (value >> 16); |
92 | 0 | *buffer++ = (value >> 8); |
93 | 0 | *buffer++ = value; |
94 | 0 | break; |
95 | 0 | } |
96 | 0 | return; |
97 | 0 | } |
98 | | |
99 | | bfd_vma |
100 | | _bfd_ns32k_get_immediate (bfd_byte *buffer, int size) |
101 | 0 | { |
102 | 0 | bfd_vma value = 0; |
103 | |
|
104 | 0 | switch (size) |
105 | 0 | { |
106 | 0 | case 4: |
107 | 0 | value = (value << 8) | (*buffer++ & 0xff); |
108 | 0 | value = (value << 8) | (*buffer++ & 0xff); |
109 | | /* Fall through. */ |
110 | 0 | case 2: |
111 | 0 | value = (value << 8) | (*buffer++ & 0xff); |
112 | | /* Fall through. */ |
113 | 0 | case 1: |
114 | 0 | value = (value << 8) | (*buffer++ & 0xff); |
115 | 0 | break; |
116 | 0 | default: |
117 | 0 | abort (); |
118 | 0 | } |
119 | 0 | return value; |
120 | 0 | } |
121 | | |
122 | | void |
123 | | _bfd_ns32k_put_immediate (bfd_vma value, bfd_byte *buffer, int size) |
124 | 0 | { |
125 | 0 | buffer += size - 1; |
126 | 0 | switch (size) |
127 | 0 | { |
128 | 0 | case 4: |
129 | 0 | *buffer-- = (value & 0xff); value >>= 8; |
130 | 0 | *buffer-- = (value & 0xff); value >>= 8; |
131 | | /* Fall through. */ |
132 | 0 | case 2: |
133 | 0 | *buffer-- = (value & 0xff); value >>= 8; |
134 | | /* Fall through. */ |
135 | 0 | case 1: |
136 | 0 | *buffer-- = (value & 0xff); value >>= 8; |
137 | 0 | } |
138 | 0 | } |
139 | | |
140 | | /* This is just like the standard perform_relocation except we |
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 | | |
145 | | static bfd_reloc_status_type |
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)) |
155 | 0 | { |
156 | 0 | int overflow = 0; |
157 | 0 | bfd_vma relocation; |
158 | 0 | bfd_reloc_status_type flag = bfd_reloc_ok; |
159 | 0 | bfd_size_type addr = reloc_entry->address; |
160 | 0 | bfd_vma output_base = 0; |
161 | 0 | reloc_howto_type *howto = reloc_entry->howto; |
162 | 0 | asection *reloc_target_output_section; |
163 | 0 | bfd_byte *location; |
164 | |
|
165 | 0 | if (bfd_is_abs_section (symbol->section) |
166 | 0 | && output_bfd != (bfd *) NULL) |
167 | 0 | { |
168 | 0 | reloc_entry->address += input_section->output_offset; |
169 | 0 | return bfd_reloc_ok; |
170 | 0 | } |
171 | | |
172 | | /* If we are not producing relocatable output, return an error if |
173 | | the symbol is not defined. An undefined weak symbol is |
174 | | considered to have a value of zero (SVR4 ABI, p. 4-27). */ |
175 | 0 | if (bfd_is_und_section (symbol->section) |
176 | 0 | && (symbol->flags & BSF_WEAK) == 0 |
177 | 0 | && output_bfd == (bfd *) NULL) |
178 | 0 | flag = bfd_reloc_undefined; |
179 | | |
180 | | /* Is the address of the relocation really within the section? */ |
181 | 0 | if (reloc_entry->address > bfd_get_section_limit (abfd, input_section)) |
182 | 0 | return bfd_reloc_outofrange; |
183 | | |
184 | | /* Work out which section the relocation is targeted at and the |
185 | | initial relocation command value. */ |
186 | | |
187 | | /* Get symbol value. (Common symbols are special.) */ |
188 | 0 | if (bfd_is_com_section (symbol->section)) |
189 | 0 | relocation = 0; |
190 | 0 | else |
191 | 0 | relocation = symbol->value; |
192 | |
|
193 | 0 | reloc_target_output_section = symbol->section->output_section; |
194 | | |
195 | | /* Convert input-section-relative symbol value to absolute. */ |
196 | 0 | if (output_bfd != NULL && ! howto->partial_inplace) |
197 | 0 | output_base = 0; |
198 | 0 | else |
199 | 0 | output_base = reloc_target_output_section->vma; |
200 | |
|
201 | 0 | relocation += output_base + symbol->section->output_offset; |
202 | | |
203 | | /* Add in supplied addend. */ |
204 | 0 | 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 | |
|
209 | 0 | if (howto->pc_relative) |
210 | 0 | { |
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 |
222 | | i386-aout, pcrel_offset is FALSE. Some other targets do not |
223 | | include the position of the location; for example, ELF. |
224 | | For those targets, pcrel_offset is TRUE. |
225 | | |
226 | | If we are producing relocatable output, then we must ensure |
227 | | that this reloc will be correctly computed when the final |
228 | | relocation is done. If pcrel_offset is FALSE we want to wind |
229 | | up with the negative of the location within the section, |
230 | | which means we must adjust the existing addend by the change |
231 | | in the location within the section. If pcrel_offset is TRUE |
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 |
235 | | producing relocatable output it is not what the code |
236 | | actually does. I don't want to change it, because it seems |
237 | | far too likely that something will break. */ |
238 | 0 | relocation -= |
239 | 0 | input_section->output_section->vma + input_section->output_offset; |
240 | |
|
241 | 0 | if (howto->pcrel_offset) |
242 | 0 | relocation -= reloc_entry->address; |
243 | 0 | } |
244 | |
|
245 | 0 | if (output_bfd != (bfd *) NULL) |
246 | 0 | { |
247 | 0 | if (! howto->partial_inplace) |
248 | 0 | { |
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 | 0 | reloc_entry->addend = relocation; |
253 | 0 | reloc_entry->address += input_section->output_offset; |
254 | 0 | return flag; |
255 | 0 | } |
256 | 0 | else |
257 | 0 | { |
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 | 0 | reloc_entry->address += input_section->output_offset; |
265 | | |
266 | | /* WTF?? */ |
267 | 0 | if (abfd->xvec->flavour == bfd_target_coff_flavour) |
268 | 0 | { |
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 | | |
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 |
286 | | reloc addend when producing relocatable output for COFF. |
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 | | |
304 | | So everything works fine when not producing relocatable |
305 | | output. When we are producing relocatable output, logically |
306 | | we should do exactly what we do when not producing |
307 | | relocatable output. Therefore, your patch is correct. In |
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 | | |
315 | | When I wanted to make coff-i386 produce relocatable output, |
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. */ |
343 | 0 | relocation -= reloc_entry->addend; |
344 | 0 | reloc_entry->addend = 0; |
345 | 0 | } |
346 | 0 | else |
347 | 0 | { |
348 | 0 | reloc_entry->addend = relocation; |
349 | 0 | } |
350 | 0 | } |
351 | 0 | } |
352 | 0 | else |
353 | 0 | { |
354 | 0 | reloc_entry->addend = 0; |
355 | 0 | } |
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 | 0 | if (howto->complain_on_overflow != complain_overflow_dont) |
365 | 0 | { |
366 | 0 | bfd_vma check; |
367 | | |
368 | | /* Get the value that will be used for the relocation, but |
369 | | starting at bit position zero. */ |
370 | 0 | if (howto->rightshift > howto->bitpos) |
371 | 0 | check = relocation >> (howto->rightshift - howto->bitpos); |
372 | 0 | else |
373 | 0 | check = relocation << (howto->bitpos - howto->rightshift); |
374 | 0 | switch (howto->complain_on_overflow) |
375 | 0 | { |
376 | 0 | case complain_overflow_signed: |
377 | 0 | { |
378 | | /* Assumes two's complement. */ |
379 | 0 | bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1; |
380 | 0 | 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 | 0 | if (howto->rightshift > howto->bitpos |
385 | 0 | && (bfd_signed_vma) relocation < 0) |
386 | 0 | check |= ((bfd_vma) - 1 |
387 | 0 | & ~((bfd_vma) - 1 |
388 | 0 | >> (howto->rightshift - howto->bitpos))); |
389 | 0 | if ((bfd_signed_vma) check > reloc_signed_max |
390 | 0 | || (bfd_signed_vma) check < reloc_signed_min) |
391 | 0 | flag = bfd_reloc_overflow; |
392 | 0 | } |
393 | 0 | break; |
394 | 0 | case complain_overflow_unsigned: |
395 | 0 | { |
396 | | /* Assumes two's complement. This expression avoids |
397 | | overflow if howto->bitsize is the number of bits in |
398 | | bfd_vma. */ |
399 | 0 | bfd_vma reloc_unsigned_max = |
400 | 0 | (((1 << (howto->bitsize - 1)) - 1) << 1) | 1; |
401 | |
|
402 | 0 | if ((bfd_vma) check > reloc_unsigned_max) |
403 | 0 | flag = bfd_reloc_overflow; |
404 | 0 | } |
405 | 0 | break; |
406 | 0 | case complain_overflow_bitfield: |
407 | 0 | { |
408 | | /* Assumes two's complement. This expression avoids |
409 | | overflow if howto->bitsize is the number of bits in |
410 | | bfd_vma. */ |
411 | 0 | bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1; |
412 | |
|
413 | 0 | if (((bfd_vma) check & ~reloc_bits) != 0 |
414 | 0 | && (((bfd_vma) check & ~reloc_bits) |
415 | 0 | != (-(bfd_vma) 1 & ~reloc_bits))) |
416 | 0 | { |
417 | | /* The above right shift is incorrect for a signed |
418 | | value. See if turning on the upper bits fixes the |
419 | | overflow. */ |
420 | 0 | if (howto->rightshift > howto->bitpos |
421 | 0 | && (bfd_signed_vma) relocation < 0) |
422 | 0 | { |
423 | 0 | check |= ((bfd_vma) - 1 |
424 | 0 | & ~((bfd_vma) - 1 |
425 | 0 | >> (howto->rightshift - howto->bitpos))); |
426 | 0 | if (((bfd_vma) check & ~reloc_bits) |
427 | 0 | != (-(bfd_vma) 1 & ~reloc_bits)) |
428 | 0 | flag = bfd_reloc_overflow; |
429 | 0 | } |
430 | 0 | else |
431 | 0 | flag = bfd_reloc_overflow; |
432 | 0 | } |
433 | 0 | } |
434 | 0 | break; |
435 | 0 | default: |
436 | 0 | abort (); |
437 | 0 | } |
438 | 0 | } |
439 | | |
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). */ |
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 | 0 | relocation >>= (bfd_vma) howto->rightshift; |
468 | | |
469 | | /* Shift everything up to where it's going to be used. */ |
470 | 0 | relocation <<= (bfd_vma) howto->bitpos; |
471 | | |
472 | | /* Wait for the day when all have the mask in them. */ |
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: |
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 |
490 | | ----------------------- |
491 | | A A A A A |
492 | | And this: |
493 | | ... i i i i i o o o o o from bfd_get<size> |
494 | | and N N N N N get instruction |
495 | | ----------------------- |
496 | | ... B B B B B |
497 | | |
498 | | And then: |
499 | | B B B B B |
500 | | or A A A A A |
501 | | ----------------------- |
502 | | R R R R R R R R R R put into bfd_put<size>. */ |
503 | |
|
504 | 0 | if (howto->negate) |
505 | 0 | relocation = -relocation; |
506 | |
|
507 | 0 | #define DOIT(x) \ |
508 | 0 | x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask)) |
509 | |
|
510 | 0 | location = (bfd_byte *) data + addr; |
511 | 0 | switch (bfd_get_reloc_size (howto)) |
512 | 0 | { |
513 | 0 | case 0: |
514 | 0 | break; |
515 | | |
516 | 0 | case 1: |
517 | 0 | { |
518 | 0 | bfd_vma x = get_data (location, 1); |
519 | 0 | DOIT (x); |
520 | 0 | put_data ((bfd_vma) x, location, 1); |
521 | 0 | } |
522 | 0 | break; |
523 | | |
524 | 0 | case 2: |
525 | 0 | if (relocation) |
526 | 0 | { |
527 | 0 | bfd_vma x = get_data (location, 2); |
528 | 0 | DOIT (x); |
529 | 0 | put_data ((bfd_vma) x, location, 2); |
530 | 0 | } |
531 | 0 | break; |
532 | 0 | case 4: |
533 | 0 | if (relocation) |
534 | 0 | { |
535 | 0 | bfd_vma x = get_data (location, 4); |
536 | 0 | DOIT (x); |
537 | 0 | put_data ((bfd_vma) x, location, 4); |
538 | 0 | } |
539 | 0 | break; |
540 | | |
541 | 0 | case 8: |
542 | 0 | #ifdef BFD64 |
543 | 0 | if (relocation) |
544 | 0 | { |
545 | 0 | bfd_vma x = get_data (location, 8); |
546 | 0 | DOIT (x); |
547 | 0 | put_data (x, location, 8); |
548 | 0 | } |
549 | | #else |
550 | | abort (); |
551 | | #endif |
552 | 0 | break; |
553 | 0 | default: |
554 | 0 | return bfd_reloc_other; |
555 | 0 | } |
556 | 0 | if ((howto->complain_on_overflow != complain_overflow_dont) && overflow) |
557 | 0 | return bfd_reloc_overflow; |
558 | | |
559 | 0 | return flag; |
560 | 0 | } |
561 | | |
562 | | /* Relocate a given location using a given value and howto. */ |
563 | | |
564 | | bfd_reloc_status_type |
565 | | _bfd_do_ns32k_reloc_contents (reloc_howto_type *howto, |
566 | | bfd *input_bfd ATTRIBUTE_UNUSED, |
567 | | bfd_vma relocation, |
568 | | bfd_byte *location, |
569 | | bfd_vma (*get_data) (bfd_byte *, int), |
570 | | void (*put_data) (bfd_vma, bfd_byte *, int)) |
571 | 0 | { |
572 | 0 | int size; |
573 | 0 | bfd_vma x; |
574 | 0 | bool overflow; |
575 | |
|
576 | 0 | if (howto->negate) |
577 | 0 | relocation = -relocation; |
578 | | |
579 | | /* Get the value we are going to relocate. */ |
580 | 0 | size = bfd_get_reloc_size (howto); |
581 | 0 | switch (size) |
582 | 0 | { |
583 | 0 | default: |
584 | 0 | abort (); |
585 | 0 | case 0: |
586 | 0 | return bfd_reloc_ok; |
587 | 0 | case 1: |
588 | 0 | case 2: |
589 | 0 | case 4: |
590 | 0 | #ifdef BFD64 |
591 | 0 | case 8: |
592 | 0 | #endif |
593 | 0 | x = get_data (location, size); |
594 | 0 | break; |
595 | 0 | } |
596 | | |
597 | | /* Check for overflow. FIXME: We may drop bits during the addition |
598 | | which we don't check for. We must either check at every single |
599 | | operation, which would be tedious, or we must do the computations |
600 | | in a type larger than bfd_vma, which would be inefficient. */ |
601 | 0 | overflow = false; |
602 | 0 | if (howto->complain_on_overflow != complain_overflow_dont) |
603 | 0 | { |
604 | 0 | bfd_vma check; |
605 | 0 | bfd_signed_vma signed_check; |
606 | 0 | bfd_vma add; |
607 | 0 | bfd_signed_vma signed_add; |
608 | |
|
609 | 0 | if (howto->rightshift == 0) |
610 | 0 | { |
611 | 0 | check = relocation; |
612 | 0 | signed_check = (bfd_signed_vma) relocation; |
613 | 0 | } |
614 | 0 | else |
615 | 0 | { |
616 | | /* Drop unwanted bits from the value we are relocating to. */ |
617 | 0 | check = relocation >> howto->rightshift; |
618 | | |
619 | | /* If this is a signed value, the rightshift just dropped |
620 | | leading 1 bits (assuming twos complement). */ |
621 | 0 | if ((bfd_signed_vma) relocation >= 0) |
622 | 0 | signed_check = check; |
623 | 0 | else |
624 | 0 | signed_check = (check |
625 | 0 | | ((bfd_vma) - 1 |
626 | 0 | & ~((bfd_vma) - 1 >> howto->rightshift))); |
627 | 0 | } |
628 | | |
629 | | /* Get the value from the object file. */ |
630 | 0 | add = x & howto->src_mask; |
631 | | |
632 | | /* Get the value from the object file with an appropriate sign. |
633 | | The expression involving howto->src_mask isolates the upper |
634 | | bit of src_mask. If that bit is set in the value we are |
635 | | adding, it is negative, and we subtract out that number times |
636 | | two. If src_mask includes the highest possible bit, then we |
637 | | can not get the upper bit, but that does not matter since |
638 | | signed_add needs no adjustment to become negative in that |
639 | | case. */ |
640 | 0 | signed_add = add; |
641 | 0 | if ((add & (((~howto->src_mask) >> 1) & howto->src_mask)) != 0) |
642 | 0 | signed_add -= (((~howto->src_mask) >> 1) & howto->src_mask) << 1; |
643 | | |
644 | | /* Add the value from the object file, shifted so that it is a |
645 | | straight number. */ |
646 | 0 | if (howto->bitpos == 0) |
647 | 0 | { |
648 | 0 | check += add; |
649 | 0 | signed_check += signed_add; |
650 | 0 | } |
651 | 0 | else |
652 | 0 | { |
653 | 0 | check += add >> howto->bitpos; |
654 | | |
655 | | /* For the signed case we use ADD, rather than SIGNED_ADD, |
656 | | to avoid warnings from SVR4 cc. This is OK since we |
657 | | explicitly handle the sign bits. */ |
658 | 0 | if (signed_add >= 0) |
659 | 0 | signed_check += add >> howto->bitpos; |
660 | 0 | else |
661 | 0 | signed_check += ((add >> howto->bitpos) |
662 | 0 | | ((bfd_vma) - 1 |
663 | 0 | & ~((bfd_vma) - 1 >> howto->bitpos))); |
664 | 0 | } |
665 | |
|
666 | 0 | switch (howto->complain_on_overflow) |
667 | 0 | { |
668 | 0 | case complain_overflow_signed: |
669 | 0 | { |
670 | | /* Assumes two's complement. */ |
671 | 0 | bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1; |
672 | 0 | bfd_signed_vma reloc_signed_min = ~reloc_signed_max; |
673 | |
|
674 | 0 | if (signed_check > reloc_signed_max |
675 | 0 | || signed_check < reloc_signed_min) |
676 | 0 | overflow = true; |
677 | 0 | } |
678 | 0 | break; |
679 | 0 | case complain_overflow_unsigned: |
680 | 0 | { |
681 | | /* Assumes two's complement. This expression avoids |
682 | | overflow if howto->bitsize is the number of bits in |
683 | | bfd_vma. */ |
684 | 0 | bfd_vma reloc_unsigned_max = |
685 | 0 | (((1 << (howto->bitsize - 1)) - 1) << 1) | 1; |
686 | |
|
687 | 0 | if (check > reloc_unsigned_max) |
688 | 0 | overflow = true; |
689 | 0 | } |
690 | 0 | break; |
691 | 0 | case complain_overflow_bitfield: |
692 | 0 | { |
693 | | /* Assumes two's complement. This expression avoids |
694 | | overflow if howto->bitsize is the number of bits in |
695 | | bfd_vma. */ |
696 | 0 | bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1; |
697 | |
|
698 | 0 | if ((check & ~reloc_bits) != 0 |
699 | 0 | && (((bfd_vma) signed_check & ~reloc_bits) |
700 | 0 | != (-(bfd_vma) 1 & ~reloc_bits))) |
701 | 0 | overflow = true; |
702 | 0 | } |
703 | 0 | break; |
704 | 0 | default: |
705 | 0 | abort (); |
706 | 0 | } |
707 | 0 | } |
708 | | |
709 | | /* Put RELOCATION in the right bits. */ |
710 | 0 | relocation >>= (bfd_vma) howto->rightshift; |
711 | 0 | relocation <<= (bfd_vma) howto->bitpos; |
712 | | |
713 | | /* Add RELOCATION to the right bits of X. */ |
714 | 0 | x = ((x & ~howto->dst_mask) |
715 | 0 | | (((x & howto->src_mask) + relocation) & howto->dst_mask)); |
716 | | |
717 | | /* Put the relocated value back in the object file. */ |
718 | 0 | switch (size) |
719 | 0 | { |
720 | 0 | default: |
721 | 0 | case 0: |
722 | 0 | abort (); |
723 | 0 | case 1: |
724 | 0 | case 2: |
725 | 0 | case 4: |
726 | 0 | #ifdef BFD64 |
727 | 0 | case 8: |
728 | 0 | #endif |
729 | 0 | put_data (x, location, size); |
730 | 0 | break; |
731 | 0 | } |
732 | | |
733 | 0 | return overflow ? bfd_reloc_overflow : bfd_reloc_ok; |
734 | 0 | } |
735 | | |
736 | | bfd_reloc_status_type |
737 | | _bfd_ns32k_reloc_disp (bfd *abfd, |
738 | | arelent *reloc_entry, |
739 | | struct bfd_symbol *symbol, |
740 | | void * data, |
741 | | asection *input_section, |
742 | | bfd *output_bfd, |
743 | | char **error_message) |
744 | 0 | { |
745 | 0 | return do_ns32k_reloc (abfd, reloc_entry, symbol, data, input_section, |
746 | 0 | output_bfd, error_message, |
747 | 0 | _bfd_ns32k_get_displacement, |
748 | 0 | _bfd_ns32k_put_displacement); |
749 | 0 | } |
750 | | |
751 | | bfd_reloc_status_type |
752 | | _bfd_ns32k_reloc_imm (bfd *abfd, |
753 | | arelent *reloc_entry, |
754 | | struct bfd_symbol *symbol, |
755 | | void * data, |
756 | | asection *input_section, |
757 | | bfd *output_bfd, |
758 | | char **error_message) |
759 | 0 | { |
760 | 0 | return do_ns32k_reloc (abfd, reloc_entry, symbol, data, input_section, |
761 | 0 | output_bfd, error_message, _bfd_ns32k_get_immediate, |
762 | 0 | _bfd_ns32k_put_immediate); |
763 | 0 | } |
764 | | |
765 | | bfd_reloc_status_type |
766 | | _bfd_ns32k_final_link_relocate (reloc_howto_type *howto, |
767 | | bfd *input_bfd, |
768 | | asection *input_section, |
769 | | bfd_byte *contents, |
770 | | bfd_vma address, |
771 | | bfd_vma value, |
772 | | bfd_vma addend) |
773 | 0 | { |
774 | 0 | bfd_vma relocation; |
775 | | |
776 | | /* Sanity check the address. */ |
777 | 0 | if (address > bfd_get_section_limit (input_bfd, input_section)) |
778 | 0 | return bfd_reloc_outofrange; |
779 | | |
780 | | /* This function assumes that we are dealing with a basic relocation |
781 | | against a symbol. We want to compute the value of the symbol to |
782 | | relocate to. This is just VALUE, the value of the symbol, plus |
783 | | ADDEND, any addend associated with the reloc. */ |
784 | 0 | relocation = value + addend; |
785 | | |
786 | | /* If the relocation is PC relative, we want to set RELOCATION to |
787 | | the distance between the symbol (currently in RELOCATION) and the |
788 | | location we are relocating. If pcrel_offset is FALSE we do not |
789 | | need to subtract out the offset of the location within the |
790 | | section (which is just ADDRESS). */ |
791 | 0 | if (howto->pc_relative) |
792 | 0 | { |
793 | 0 | relocation -= (input_section->output_section->vma |
794 | 0 | + input_section->output_offset); |
795 | 0 | if (howto->pcrel_offset) |
796 | 0 | relocation -= address; |
797 | 0 | } |
798 | |
|
799 | 0 | return _bfd_ns32k_relocate_contents (howto, input_bfd, relocation, |
800 | 0 | contents + address); |
801 | 0 | } |