| 1 | /* BFD support for the ns32k architecture. |
| 2 | Copyright (C) 1990-2015 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, } |
| 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 | { |
| 43 | bfd_signed_vma value; |
| 44 | |
| 45 | switch (size) |
| 46 | { |
| 47 | case 1: |
| 48 | value = ((*buffer & 0x7f) ^ 0x40) - 0x40; |
| 49 | break; |
| 50 | |
| 51 | case 2: |
| 52 | value = ((*buffer++ & 0x3f) ^ 0x20) - 0x20; |
| 53 | value = (value << 8) | (0xff & *buffer); |
| 54 | break; |
| 55 | |
| 56 | case 4: |
| 57 | value = ((*buffer++ & 0x3f) ^ 0x20) - 0x20; |
| 58 | value = (value << 8) | (0xff & *buffer++); |
| 59 | value = (value << 8) | (0xff & *buffer++); |
| 60 | value = (value << 8) | (0xff & *buffer); |
| 61 | break; |
| 62 | |
| 63 | default: |
| 64 | abort (); |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | return value; |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | _bfd_ns32k_put_displacement (bfd_vma value, bfd_byte *buffer, int size) |
| 73 | { |
| 74 | switch (size) |
| 75 | { |
| 76 | case 1: |
| 77 | value &= 0x7f; |
| 78 | *buffer++ = value; |
| 79 | break; |
| 80 | |
| 81 | case 2: |
| 82 | value &= 0x3fff; |
| 83 | value |= 0x8000; |
| 84 | *buffer++ = (value >> 8); |
| 85 | *buffer++ = value; |
| 86 | break; |
| 87 | |
| 88 | case 4: |
| 89 | value |= (bfd_vma) 0xc0000000; |
| 90 | *buffer++ = (value >> 24); |
| 91 | *buffer++ = (value >> 16); |
| 92 | *buffer++ = (value >> 8); |
| 93 | *buffer++ = value; |
| 94 | break; |
| 95 | } |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | bfd_vma |
| 100 | _bfd_ns32k_get_immediate (bfd_byte *buffer, int size) |
| 101 | { |
| 102 | bfd_vma value = 0; |
| 103 | |
| 104 | switch (size) |
| 105 | { |
| 106 | case 4: |
| 107 | value = (value << 8) | (*buffer++ & 0xff); |
| 108 | value = (value << 8) | (*buffer++ & 0xff); |
| 109 | case 2: |
| 110 | value = (value << 8) | (*buffer++ & 0xff); |
| 111 | case 1: |
| 112 | value = (value << 8) | (*buffer++ & 0xff); |
| 113 | break; |
| 114 | default: |
| 115 | abort (); |
| 116 | } |
| 117 | return value; |
| 118 | } |
| 119 | |
| 120 | void |
| 121 | _bfd_ns32k_put_immediate (bfd_vma value, bfd_byte *buffer, int size) |
| 122 | { |
| 123 | buffer += size - 1; |
| 124 | switch (size) |
| 125 | { |
| 126 | case 4: |
| 127 | *buffer-- = (value & 0xff); value >>= 8; |
| 128 | *buffer-- = (value & 0xff); value >>= 8; |
| 129 | case 2: |
| 130 | *buffer-- = (value & 0xff); value >>= 8; |
| 131 | case 1: |
| 132 | *buffer-- = (value & 0xff); value >>= 8; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /* This is just like the standard perform_relocation except we |
| 137 | use get_data and put_data which know about the ns32k storage |
| 138 | methods. This is probably a lot more complicated than it |
| 139 | needs to be! */ |
| 140 | |
| 141 | static bfd_reloc_status_type |
| 142 | do_ns32k_reloc (bfd * abfd, |
| 143 | arelent * reloc_entry, |
| 144 | struct bfd_symbol * symbol, |
| 145 | void * data, |
| 146 | asection * input_section, |
| 147 | bfd * output_bfd, |
| 148 | char ** error_message ATTRIBUTE_UNUSED, |
| 149 | bfd_vma (* get_data) (bfd_byte *, int), |
| 150 | void (* put_data) (bfd_vma, bfd_byte *, int)) |
| 151 | { |
| 152 | int overflow = 0; |
| 153 | bfd_vma relocation; |
| 154 | bfd_reloc_status_type flag = bfd_reloc_ok; |
| 155 | bfd_size_type addr = reloc_entry->address; |
| 156 | bfd_vma output_base = 0; |
| 157 | reloc_howto_type *howto = reloc_entry->howto; |
| 158 | asection *reloc_target_output_section; |
| 159 | bfd_byte *location; |
| 160 | |
| 161 | if (bfd_is_abs_section (symbol->section) |
| 162 | && output_bfd != (bfd *) NULL) |
| 163 | { |
| 164 | reloc_entry->address += input_section->output_offset; |
| 165 | return bfd_reloc_ok; |
| 166 | } |
| 167 | |
| 168 | /* If we are not producing relocatable output, return an error if |
| 169 | the symbol is not defined. An undefined weak symbol is |
| 170 | considered to have a value of zero (SVR4 ABI, p. 4-27). */ |
| 171 | if (bfd_is_und_section (symbol->section) |
| 172 | && (symbol->flags & BSF_WEAK) == 0 |
| 173 | && output_bfd == (bfd *) NULL) |
| 174 | flag = bfd_reloc_undefined; |
| 175 | |
| 176 | /* Is the address of the relocation really within the section? */ |
| 177 | if (reloc_entry->address > bfd_get_section_limit (abfd, input_section)) |
| 178 | return bfd_reloc_outofrange; |
| 179 | |
| 180 | /* Work out which section the relocation is targeted at and the |
| 181 | initial relocation command value. */ |
| 182 | |
| 183 | /* Get symbol value. (Common symbols are special.) */ |
| 184 | if (bfd_is_com_section (symbol->section)) |
| 185 | relocation = 0; |
| 186 | else |
| 187 | relocation = symbol->value; |
| 188 | |
| 189 | reloc_target_output_section = symbol->section->output_section; |
| 190 | |
| 191 | /* Convert input-section-relative symbol value to absolute. */ |
| 192 | if (output_bfd != NULL && ! howto->partial_inplace) |
| 193 | output_base = 0; |
| 194 | else |
| 195 | output_base = reloc_target_output_section->vma; |
| 196 | |
| 197 | relocation += output_base + symbol->section->output_offset; |
| 198 | |
| 199 | /* Add in supplied addend. */ |
| 200 | relocation += reloc_entry->addend; |
| 201 | |
| 202 | /* Here the variable relocation holds the final address of the |
| 203 | symbol we are relocating against, plus any addend. */ |
| 204 | |
| 205 | if (howto->pc_relative) |
| 206 | { |
| 207 | /* This is a PC relative relocation. We want to set RELOCATION |
| 208 | to the distance between the address of the symbol and the |
| 209 | location. RELOCATION is already the address of the symbol. |
| 210 | |
| 211 | We start by subtracting the address of the section containing |
| 212 | the location. |
| 213 | |
| 214 | If pcrel_offset is set, we must further subtract the position |
| 215 | of the location within the section. Some targets arrange for |
| 216 | the addend to be the negative of the position of the location |
| 217 | within the section; for example, i386-aout does this. For |
| 218 | i386-aout, pcrel_offset is FALSE. Some other targets do not |
| 219 | include the position of the location; for example, m88kbcs, |
| 220 | or ELF. For those targets, pcrel_offset is TRUE. |
| 221 | |
| 222 | If we are producing relocatable output, then we must ensure |
| 223 | that this reloc will be correctly computed when the final |
| 224 | relocation is done. If pcrel_offset is FALSE we want to wind |
| 225 | up with the negative of the location within the section, |
| 226 | which means we must adjust the existing addend by the change |
| 227 | in the location within the section. If pcrel_offset is TRUE |
| 228 | we do not want to adjust the existing addend at all. |
| 229 | |
| 230 | FIXME: This seems logical to me, but for the case of |
| 231 | producing relocatable output it is not what the code |
| 232 | actually does. I don't want to change it, because it seems |
| 233 | far too likely that something will break. */ |
| 234 | relocation -= |
| 235 | input_section->output_section->vma + input_section->output_offset; |
| 236 | |
| 237 | if (howto->pcrel_offset) |
| 238 | relocation -= reloc_entry->address; |
| 239 | } |
| 240 | |
| 241 | if (output_bfd != (bfd *) NULL) |
| 242 | { |
| 243 | if (! howto->partial_inplace) |
| 244 | { |
| 245 | /* This is a partial relocation, and we want to apply the relocation |
| 246 | to the reloc entry rather than the raw data. Modify the reloc |
| 247 | inplace to reflect what we now know. */ |
| 248 | reloc_entry->addend = relocation; |
| 249 | reloc_entry->address += input_section->output_offset; |
| 250 | return flag; |
| 251 | } |
| 252 | else |
| 253 | { |
| 254 | /* This is a partial relocation, but inplace, so modify the |
| 255 | reloc record a bit. |
| 256 | |
| 257 | If we've relocated with a symbol with a section, change |
| 258 | into a ref to the section belonging to the symbol. */ |
| 259 | |
| 260 | reloc_entry->address += input_section->output_offset; |
| 261 | |
| 262 | /* WTF?? */ |
| 263 | if (abfd->xvec->flavour == bfd_target_coff_flavour) |
| 264 | { |
| 265 | /* For m68k-coff, the addend was being subtracted twice during |
| 266 | relocation with -r. Removing the line below this comment |
| 267 | fixes that problem; see PR 2953. |
| 268 | |
| 269 | However, Ian wrote the following, regarding removing the line |
| 270 | below, which explains why it is still enabled: --djm |
| 271 | |
| 272 | If you put a patch like that into BFD you need to check all |
| 273 | the COFF linkers. I am fairly certain that patch will break |
| 274 | coff-i386 (e.g., SCO); see coff_i386_reloc in coff-i386.c |
| 275 | where I worked around the problem in a different way. There |
| 276 | may very well be a reason that the code works as it does. |
| 277 | |
| 278 | Hmmm. The first obvious point is that bfd_perform_relocation |
| 279 | should not have any tests that depend upon the flavour. It's |
| 280 | seem like entirely the wrong place for such a thing. The |
| 281 | second obvious point is that the current code ignores the |
| 282 | reloc addend when producing relocatable output for COFF. |
| 283 | That's peculiar. In fact, I really have no idea what the |
| 284 | point of the line you want to remove is. |
| 285 | |
| 286 | A typical COFF reloc subtracts the old value of the symbol |
| 287 | and adds in the new value to the location in the object file |
| 288 | (if it's a pc relative reloc it adds the difference between |
| 289 | the symbol value and the location). When relocating we need |
| 290 | to preserve that property. |
| 291 | |
| 292 | BFD handles this by setting the addend to the negative of the |
| 293 | old value of the symbol. Unfortunately it handles common |
| 294 | symbols in a non-standard way (it doesn't subtract the old |
| 295 | value) but that's a different story (we can't change it |
| 296 | without losing backward compatibility with old object files) |
| 297 | (coff-i386 does subtract the old value, to be compatible with |
| 298 | existing coff-i386 targets, like SCO). |
| 299 | |
| 300 | So everything works fine when not producing relocatable |
| 301 | output. When we are producing relocatable output, logically |
| 302 | we should do exactly what we do when not producing |
| 303 | relocatable output. Therefore, your patch is correct. In |
| 304 | fact, it should probably always just set reloc_entry->addend |
| 305 | to 0 for all cases, since it is, in fact, going to add the |
| 306 | value into the object file. This won't hurt the COFF code, |
| 307 | which doesn't use the addend; I'm not sure what it will do |
| 308 | to other formats (the thing to check for would be whether |
| 309 | any formats both use the addend and set partial_inplace). |
| 310 | |
| 311 | When I wanted to make coff-i386 produce relocatable output, |
| 312 | I ran into the problem that you are running into: I wanted |
| 313 | to remove that line. Rather than risk it, I made the |
| 314 | coff-i386 relocs use a special function; it's coff_i386_reloc |
| 315 | in coff-i386.c. The function specifically adds the addend |
| 316 | field into the object file, knowing that bfd_perform_relocation |
| 317 | is not going to. If you remove that line, then coff-i386.c |
| 318 | will wind up adding the addend field in twice. It's trivial |
| 319 | to fix; it just needs to be done. |
| 320 | |
| 321 | The problem with removing the line is just that it may break |
| 322 | some working code. With BFD it's hard to be sure of anything. |
| 323 | The right way to deal with this is simply to build and test at |
| 324 | least all the supported COFF targets. It should be |
| 325 | straightforward if time and disk space consuming. For each |
| 326 | target: |
| 327 | 1) build the linker |
| 328 | 2) generate some executable, and link it using -r (I would |
| 329 | probably use paranoia.o and link against newlib/libc.a, |
| 330 | which for all the supported targets would be available in |
| 331 | /usr/cygnus/progressive/H-host/target/lib/libc.a). |
| 332 | 3) make the change to reloc.c |
| 333 | 4) rebuild the linker |
| 334 | 5) repeat step 2 |
| 335 | 6) if the resulting object files are the same, you have at |
| 336 | least made it no worse |
| 337 | 7) if they are different you have to figure out which |
| 338 | version is right. */ |
| 339 | relocation -= reloc_entry->addend; |
| 340 | reloc_entry->addend = 0; |
| 341 | } |
| 342 | else |
| 343 | { |
| 344 | reloc_entry->addend = relocation; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | else |
| 349 | { |
| 350 | reloc_entry->addend = 0; |
| 351 | } |
| 352 | |
| 353 | /* FIXME: This overflow checking is incomplete, because the value |
| 354 | might have overflowed before we get here. For a correct check we |
| 355 | need to compute the value in a size larger than bitsize, but we |
| 356 | can't reasonably do that for a reloc the same size as a host |
| 357 | machine word. |
| 358 | FIXME: We should also do overflow checking on the result after |
| 359 | adding in the value contained in the object file. */ |
| 360 | if (howto->complain_on_overflow != complain_overflow_dont) |
| 361 | { |
| 362 | bfd_vma check; |
| 363 | |
| 364 | /* Get the value that will be used for the relocation, but |
| 365 | starting at bit position zero. */ |
| 366 | if (howto->rightshift > howto->bitpos) |
| 367 | check = relocation >> (howto->rightshift - howto->bitpos); |
| 368 | else |
| 369 | check = relocation << (howto->bitpos - howto->rightshift); |
| 370 | switch (howto->complain_on_overflow) |
| 371 | { |
| 372 | case complain_overflow_signed: |
| 373 | { |
| 374 | /* Assumes two's complement. */ |
| 375 | bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1; |
| 376 | bfd_signed_vma reloc_signed_min = ~reloc_signed_max; |
| 377 | |
| 378 | /* The above right shift is incorrect for a signed value. |
| 379 | Fix it up by forcing on the upper bits. */ |
| 380 | if (howto->rightshift > howto->bitpos |
| 381 | && (bfd_signed_vma) relocation < 0) |
| 382 | check |= ((bfd_vma) - 1 |
| 383 | & ~((bfd_vma) - 1 |
| 384 | >> (howto->rightshift - howto->bitpos))); |
| 385 | if ((bfd_signed_vma) check > reloc_signed_max |
| 386 | || (bfd_signed_vma) check < reloc_signed_min) |
| 387 | flag = bfd_reloc_overflow; |
| 388 | } |
| 389 | break; |
| 390 | case complain_overflow_unsigned: |
| 391 | { |
| 392 | /* Assumes two's complement. This expression avoids |
| 393 | overflow if howto->bitsize is the number of bits in |
| 394 | bfd_vma. */ |
| 395 | bfd_vma reloc_unsigned_max = |
| 396 | (((1 << (howto->bitsize - 1)) - 1) << 1) | 1; |
| 397 | |
| 398 | if ((bfd_vma) check > reloc_unsigned_max) |
| 399 | flag = bfd_reloc_overflow; |
| 400 | } |
| 401 | break; |
| 402 | case complain_overflow_bitfield: |
| 403 | { |
| 404 | /* Assumes two's complement. This expression avoids |
| 405 | overflow if howto->bitsize is the number of bits in |
| 406 | bfd_vma. */ |
| 407 | bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1; |
| 408 | |
| 409 | if (((bfd_vma) check & ~reloc_bits) != 0 |
| 410 | && (((bfd_vma) check & ~reloc_bits) |
| 411 | != (-(bfd_vma) 1 & ~reloc_bits))) |
| 412 | { |
| 413 | /* The above right shift is incorrect for a signed |
| 414 | value. See if turning on the upper bits fixes the |
| 415 | overflow. */ |
| 416 | if (howto->rightshift > howto->bitpos |
| 417 | && (bfd_signed_vma) relocation < 0) |
| 418 | { |
| 419 | check |= ((bfd_vma) - 1 |
| 420 | & ~((bfd_vma) - 1 |
| 421 | >> (howto->rightshift - howto->bitpos))); |
| 422 | if (((bfd_vma) check & ~reloc_bits) |
| 423 | != (-(bfd_vma) 1 & ~reloc_bits)) |
| 424 | flag = bfd_reloc_overflow; |
| 425 | } |
| 426 | else |
| 427 | flag = bfd_reloc_overflow; |
| 428 | } |
| 429 | } |
| 430 | break; |
| 431 | default: |
| 432 | abort (); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | /* Either we are relocating all the way, or we don't want to apply |
| 437 | the relocation to the reloc entry (probably because there isn't |
| 438 | any room in the output format to describe addends to relocs). */ |
| 439 | |
| 440 | /* The cast to bfd_vma avoids a bug in the Alpha OSF/1 C compiler |
| 441 | (OSF version 1.3, compiler version 3.11). It miscompiles the |
| 442 | following program: |
| 443 | |
| 444 | struct str |
| 445 | { |
| 446 | unsigned int i0; |
| 447 | } s = { 0 }; |
| 448 | |
| 449 | int |
| 450 | main () |
| 451 | { |
| 452 | unsigned long x; |
| 453 | |
| 454 | x = 0x100000000; |
| 455 | x <<= (unsigned long) s.i0; |
| 456 | if (x == 0) |
| 457 | printf ("failed\n"); |
| 458 | else |
| 459 | printf ("succeeded (%lx)\n", x); |
| 460 | } |
| 461 | */ |
| 462 | |
| 463 | relocation >>= (bfd_vma) howto->rightshift; |
| 464 | |
| 465 | /* Shift everything up to where it's going to be used. */ |
| 466 | relocation <<= (bfd_vma) howto->bitpos; |
| 467 | |
| 468 | /* Wait for the day when all have the mask in them. */ |
| 469 | |
| 470 | /* What we do: |
| 471 | i instruction to be left alone |
| 472 | o offset within instruction |
| 473 | r relocation offset to apply |
| 474 | S src mask |
| 475 | D dst mask |
| 476 | N ~dst mask |
| 477 | A part 1 |
| 478 | B part 2 |
| 479 | R result |
| 480 | |
| 481 | Do this: |
| 482 | i i i i i o o o o o from bfd_get<size> |
| 483 | and S S S S S to get the size offset we want |
| 484 | + r r r r r r r r r r to get the final value to place |
| 485 | and D D D D D to chop to right size |
| 486 | ----------------------- |
| 487 | A A A A A |
| 488 | And this: |
| 489 | ... i i i i i o o o o o from bfd_get<size> |
| 490 | and N N N N N get instruction |
| 491 | ----------------------- |
| 492 | ... B B B B B |
| 493 | |
| 494 | And then: |
| 495 | B B B B B |
| 496 | or A A A A A |
| 497 | ----------------------- |
| 498 | R R R R R R R R R R put into bfd_put<size>. */ |
| 499 | |
| 500 | #define DOIT(x) \ |
| 501 | x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask)) |
| 502 | |
| 503 | location = (bfd_byte *) data + addr; |
| 504 | switch (howto->size) |
| 505 | { |
| 506 | case 0: |
| 507 | { |
| 508 | bfd_vma x = get_data (location, 1); |
| 509 | DOIT (x); |
| 510 | put_data ((bfd_vma) x, location, 1); |
| 511 | } |
| 512 | break; |
| 513 | |
| 514 | case 1: |
| 515 | if (relocation) |
| 516 | { |
| 517 | bfd_vma x = get_data (location, 2); |
| 518 | DOIT (x); |
| 519 | put_data ((bfd_vma) x, location, 2); |
| 520 | } |
| 521 | break; |
| 522 | case 2: |
| 523 | if (relocation) |
| 524 | { |
| 525 | bfd_vma x = get_data (location, 4); |
| 526 | DOIT (x); |
| 527 | put_data ((bfd_vma) x, location, 4); |
| 528 | } |
| 529 | break; |
| 530 | case -2: |
| 531 | { |
| 532 | bfd_vma x = get_data (location, 4); |
| 533 | relocation = -relocation; |
| 534 | DOIT(x); |
| 535 | put_data ((bfd_vma) x, location, 4); |
| 536 | } |
| 537 | break; |
| 538 | |
| 539 | case 3: |
| 540 | /* Do nothing. */ |
| 541 | break; |
| 542 | |
| 543 | case 4: |
| 544 | #ifdef BFD64 |
| 545 | if (relocation) |
| 546 | { |
| 547 | bfd_vma x = get_data (location, 8); |
| 548 | DOIT (x); |
| 549 | put_data (x, location, 8); |
| 550 | } |
| 551 | #else |
| 552 | abort (); |
| 553 | #endif |
| 554 | break; |
| 555 | default: |
| 556 | return bfd_reloc_other; |
| 557 | } |
| 558 | if ((howto->complain_on_overflow != complain_overflow_dont) && overflow) |
| 559 | return bfd_reloc_overflow; |
| 560 | |
| 561 | return flag; |
| 562 | } |
| 563 | |
| 564 | /* Relocate a given location using a given value and howto. */ |
| 565 | |
| 566 | bfd_reloc_status_type |
| 567 | _bfd_do_ns32k_reloc_contents (reloc_howto_type *howto, |
| 568 | bfd *input_bfd ATTRIBUTE_UNUSED, |
| 569 | bfd_vma relocation, |
| 570 | bfd_byte *location, |
| 571 | bfd_vma (*get_data) (bfd_byte *, int), |
| 572 | void (*put_data) (bfd_vma, bfd_byte *, int)) |
| 573 | { |
| 574 | int size; |
| 575 | bfd_vma x; |
| 576 | bfd_boolean overflow; |
| 577 | |
| 578 | /* If the size is negative, negate RELOCATION. This isn't very |
| 579 | general. */ |
| 580 | if (howto->size < 0) |
| 581 | relocation = -relocation; |
| 582 | |
| 583 | /* Get the value we are going to relocate. */ |
| 584 | size = bfd_get_reloc_size (howto); |
| 585 | switch (size) |
| 586 | { |
| 587 | default: |
| 588 | abort (); |
| 589 | case 0: |
| 590 | return bfd_reloc_ok; |
| 591 | case 1: |
| 592 | case 2: |
| 593 | case 4: |
| 594 | #ifdef BFD64 |
| 595 | case 8: |
| 596 | #endif |
| 597 | x = get_data (location, size); |
| 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. */ |
| 605 | overflow = FALSE; |
| 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 |
| 661 | explicitly handle the sign bits. */ |
| 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) |
| 680 | overflow = TRUE; |
| 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) |
| 692 | overflow = TRUE; |
| 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) |
| 704 | != (-(bfd_vma) 1 & ~reloc_bits))) |
| 705 | overflow = TRUE; |
| 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 |
| 733 | put_data (x, location, size); |
| 734 | break; |
| 735 | } |
| 736 | |
| 737 | return overflow ? bfd_reloc_overflow : bfd_reloc_ok; |
| 738 | } |
| 739 | |
| 740 | bfd_reloc_status_type |
| 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) |
| 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 |
| 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) |
| 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 |
| 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) |
| 777 | { |
| 778 | bfd_vma relocation; |
| 779 | |
| 780 | /* Sanity check the address. */ |
| 781 | if (address > bfd_get_section_limit (input_bfd, input_section)) |
| 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 |
| 795 | pcrel_offset is FALSE. Other targets (e.g., m88kbcs or ELF) |
| 796 | simply leave the contents of the section as zero; for such |
| 797 | targets pcrel_offset is TRUE. If pcrel_offset is FALSE we do not |
| 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 | } |