cpu-ns32k.c 24 KB

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