1 /* BFD support for the ns32k architecture.
2 Copyright 1990, 1991, 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2007, 2012 Free Software Foundation, Inc.
4 Almost totally rewritten by Ian Dall from initial work
7 This file is part of BFD, the Binary File Descriptor library.
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
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
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.
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
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
29 #define N(machine, printable, d, next) \
30 { 32, 32, 8, bfd_arch_ns32k, machine, "ns32k",printable,3,d, \
31 bfd_default_compatible,bfd_default_scan,bfd_arch_default_fill,next, }
33 static const bfd_arch_info_type arch_info_struct[] =
35 N(32532,"ns32k:32532",TRUE, 0), /* The word ns32k will match this too. */
38 const bfd_arch_info_type bfd_ns32k_arch =
39 N(32032,"ns32k:32032",FALSE, &arch_info_struct[0]);
42 _bfd_ns32k_get_displacement (bfd_byte *buffer, int size)
49 value = ((*buffer & 0x7f) ^ 0x40) - 0x40;
53 value = ((*buffer++ & 0x3f) ^ 0x20) - 0x20;
54 value = (value << 8) | (0xff & *buffer);
58 value = ((*buffer++ & 0x3f) ^ 0x20) - 0x20;
59 value = (value << 8) | (0xff & *buffer++);
60 value = (value << 8) | (0xff & *buffer++);
61 value = (value << 8) | (0xff & *buffer);
73 _bfd_ns32k_put_displacement (bfd_vma value, bfd_byte *buffer, int size)
85 *buffer++ = (value >> 8);
90 value |= (bfd_vma) 0xc0000000;
91 *buffer++ = (value >> 24);
92 *buffer++ = (value >> 16);
93 *buffer++ = (value >> 8);
101 _bfd_ns32k_get_immediate (bfd_byte *buffer, int size)
108 value = (value << 8) | (*buffer++ & 0xff);
109 value = (value << 8) | (*buffer++ & 0xff);
111 value = (value << 8) | (*buffer++ & 0xff);
113 value = (value << 8) | (*buffer++ & 0xff);
122 _bfd_ns32k_put_immediate (bfd_vma value, bfd_byte *buffer, int size)
128 *buffer-- = (value & 0xff); value >>= 8;
129 *buffer-- = (value & 0xff); value >>= 8;
131 *buffer-- = (value & 0xff); value >>= 8;
133 *buffer-- = (value & 0xff); value >>= 8;
137 /* This is just like the standard perform_relocation except we
138 use get_data and put_data which know about the ns32k storage
139 methods. This is probably a lot more complicated than it
142 static bfd_reloc_status_type
143 do_ns32k_reloc (bfd * abfd,
144 arelent * reloc_entry,
145 struct bfd_symbol * symbol,
147 asection * input_section,
149 char ** error_message ATTRIBUTE_UNUSED,
150 bfd_vma (* get_data) (bfd_byte *, int),
151 void (* put_data) (bfd_vma, bfd_byte *, int))
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;
162 if (bfd_is_abs_section (symbol->section)
163 && output_bfd != (bfd *) NULL)
165 reloc_entry->address += input_section->output_offset;
169 /* If we are not producing relocatable output, return an error if
170 the symbol is not defined. An undefined weak symbol is
171 considered to have a value of zero (SVR4 ABI, p. 4-27). */
172 if (bfd_is_und_section (symbol->section)
173 && (symbol->flags & BSF_WEAK) == 0
174 && output_bfd == (bfd *) NULL)
175 flag = bfd_reloc_undefined;
177 /* Is the address of the relocation really within the section? */
178 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
179 return bfd_reloc_outofrange;
181 /* Work out which section the relocation is targeted at and the
182 initial relocation command value. */
184 /* Get symbol value. (Common symbols are special.) */
185 if (bfd_is_com_section (symbol->section))
188 relocation = symbol->value;
190 reloc_target_output_section = symbol->section->output_section;
192 /* Convert input-section-relative symbol value to absolute. */
193 if (output_bfd != NULL && ! howto->partial_inplace)
196 output_base = reloc_target_output_section->vma;
198 relocation += output_base + symbol->section->output_offset;
200 /* Add in supplied addend. */
201 relocation += reloc_entry->addend;
203 /* Here the variable relocation holds the final address of the
204 symbol we are relocating against, plus any addend. */
206 if (howto->pc_relative)
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.
212 We start by subtracting the address of the section containing
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
219 i386-aout, pcrel_offset is FALSE. Some other targets do not
220 include the position of the location; for example, m88kbcs,
221 or ELF. For those targets, pcrel_offset is TRUE.
223 If we are producing relocatable output, then we must ensure
224 that this reloc will be correctly computed when the final
225 relocation is done. If pcrel_offset is FALSE we want to wind
226 up with the negative of the location within the section,
227 which means we must adjust the existing addend by the change
228 in the location within the section. If pcrel_offset is TRUE
229 we do not want to adjust the existing addend at all.
231 FIXME: This seems logical to me, but for the case of
232 producing relocatable output it is not what the code
233 actually does. I don't want to change it, because it seems
234 far too likely that something will break. */
236 input_section->output_section->vma + input_section->output_offset;
238 if (howto->pcrel_offset)
239 relocation -= reloc_entry->address;
242 if (output_bfd != (bfd *) NULL)
244 if (! howto->partial_inplace)
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;
255 /* This is a partial relocation, but inplace, so modify the
258 If we've relocated with a symbol with a section, change
259 into a ref to the section belonging to the symbol. */
261 reloc_entry->address += input_section->output_offset;
264 if (abfd->xvec->flavour == bfd_target_coff_flavour)
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.
270 However, Ian wrote the following, regarding removing the line
271 below, which explains why it is still enabled: --djm
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.
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
283 reloc addend when producing relocatable output for COFF.
284 That's peculiar. In fact, I really have no idea what the
285 point of the line you want to remove is.
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.
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).
301 So everything works fine when not producing relocatable
302 output. When we are producing relocatable output, logically
303 we should do exactly what we do when not producing
304 relocatable output. Therefore, your patch is correct. In
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).
312 When I wanted to make coff-i386 produce relocatable output,
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.
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
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
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
340 relocation -= reloc_entry->addend;
341 reloc_entry->addend = 0;
345 reloc_entry->addend = relocation;
351 reloc_entry->addend = 0;
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
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)
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);
370 check = relocation << (howto->bitpos - howto->rightshift);
371 switch (howto->complain_on_overflow)
373 case complain_overflow_signed:
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;
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
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;
391 case complain_overflow_unsigned:
393 /* Assumes two's complement. This expression avoids
394 overflow if howto->bitsize is the number of bits in
396 bfd_vma reloc_unsigned_max =
397 (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
399 if ((bfd_vma) check > reloc_unsigned_max)
400 flag = bfd_reloc_overflow;
403 case complain_overflow_bitfield:
405 /* Assumes two's complement. This expression avoids
406 overflow if howto->bitsize is the number of bits in
408 bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
410 if (((bfd_vma) check & ~reloc_bits) != 0
411 && (((bfd_vma) check & ~reloc_bits)
412 != (-(bfd_vma) 1 & ~reloc_bits)))
414 /* The above right shift is incorrect for a signed
415 value. See if turning on the upper bits fixes the
417 if (howto->rightshift > howto->bitpos
418 && (bfd_signed_vma) relocation < 0)
420 check |= ((bfd_vma) - 1
422 >> (howto->rightshift - howto->bitpos)));
423 if (((bfd_vma) check & ~reloc_bits)
424 != (-(bfd_vma) 1 & ~reloc_bits))
425 flag = bfd_reloc_overflow;
428 flag = bfd_reloc_overflow;
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). */
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
456 x <<= (unsigned long) s.i0;
460 printf ("succeeded (%lx)\n", x);
464 relocation >>= (bfd_vma) howto->rightshift;
466 /* Shift everything up to where it's going to be used. */
467 relocation <<= (bfd_vma) howto->bitpos;
469 /* Wait for the day when all have the mask in them. */
472 i instruction to be left alone
473 o offset within instruction
474 r relocation offset to apply
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 -----------------------
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 -----------------------
498 -----------------------
499 R R R R R R R R R R put into bfd_put<size>. */
502 x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask))
504 location = (bfd_byte *) data + addr;
509 bfd_vma x = get_data (location, 1);
511 put_data ((bfd_vma) x, location, 1);
518 bfd_vma x = get_data (location, 2);
520 put_data ((bfd_vma) x, location, 2);
526 bfd_vma x = get_data (location, 4);
528 put_data ((bfd_vma) x, location, 4);
533 bfd_vma x = get_data (location, 4);
534 relocation = -relocation;
536 put_data ((bfd_vma) x, location, 4);
548 bfd_vma x = get_data (location, 8);
550 put_data (x, location, 8);
557 return bfd_reloc_other;
559 if ((howto->complain_on_overflow != complain_overflow_dont) && overflow)
560 return bfd_reloc_overflow;
565 /* Relocate a given location using a given value and howto. */
567 bfd_reloc_status_type
568 _bfd_do_ns32k_reloc_contents (reloc_howto_type *howto,
569 bfd *input_bfd ATTRIBUTE_UNUSED,
572 bfd_vma (*get_data) (bfd_byte *, int),
573 void (*put_data) (bfd_vma, bfd_byte *, int))
577 bfd_boolean overflow;
579 /* If the size is negative, negate RELOCATION. This isn't very
582 relocation = -relocation;
584 /* Get the value we are going to relocate. */
585 size = bfd_get_reloc_size (howto);
597 x = get_data (location, size);
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. */
606 if (howto->complain_on_overflow != complain_overflow_dont)
609 bfd_signed_vma signed_check;
611 bfd_signed_vma signed_add;
613 if (howto->rightshift == 0)
616 signed_check = (bfd_signed_vma) relocation;
620 /* Drop unwanted bits from the value we are relocating to. */
621 check = relocation >> howto->rightshift;
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;
628 signed_check = (check
630 & ~((bfd_vma) - 1 >> howto->rightshift)));
633 /* Get the value from the object file. */
634 add = x & howto->src_mask;
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
645 if ((add & (((~howto->src_mask) >> 1) & howto->src_mask)) != 0)
646 signed_add -= (((~howto->src_mask) >> 1) & howto->src_mask) << 1;
648 /* Add the value from the object file, shifted so that it is a
650 if (howto->bitpos == 0)
653 signed_check += signed_add;
657 check += add >> howto->bitpos;
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. */
663 signed_check += add >> howto->bitpos;
665 signed_check += ((add >> howto->bitpos)
667 & ~((bfd_vma) - 1 >> howto->bitpos)));
670 switch (howto->complain_on_overflow)
672 case complain_overflow_signed:
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;
678 if (signed_check > reloc_signed_max
679 || signed_check < reloc_signed_min)
683 case complain_overflow_unsigned:
685 /* Assumes two's complement. This expression avoids
686 overflow if howto->bitsize is the number of bits in
688 bfd_vma reloc_unsigned_max =
689 (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
691 if (check > reloc_unsigned_max)
695 case complain_overflow_bitfield:
697 /* Assumes two's complement. This expression avoids
698 overflow if howto->bitsize is the number of bits in
700 bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
702 if ((check & ~reloc_bits) != 0
703 && (((bfd_vma) signed_check & ~reloc_bits)
704 != (-(bfd_vma) 1 & ~reloc_bits)))
713 /* Put RELOCATION in the right bits. */
714 relocation >>= (bfd_vma) howto->rightshift;
715 relocation <<= (bfd_vma) howto->bitpos;
717 /* Add RELOCATION to the right bits of X. */
718 x = ((x & ~howto->dst_mask)
719 | (((x & howto->src_mask) + relocation) & howto->dst_mask));
721 /* Put the relocated value back in the object file. */
733 put_data (x, location, size);
737 return overflow ? bfd_reloc_overflow : bfd_reloc_ok;
740 bfd_reloc_status_type
741 _bfd_ns32k_reloc_disp (bfd *abfd,
742 arelent *reloc_entry,
743 struct bfd_symbol *symbol,
745 asection *input_section,
747 char **error_message)
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);
755 bfd_reloc_status_type
756 _bfd_ns32k_reloc_imm (bfd *abfd,
757 arelent *reloc_entry,
758 struct bfd_symbol *symbol,
760 asection *input_section,
762 char **error_message)
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);
769 bfd_reloc_status_type
770 _bfd_ns32k_final_link_relocate (reloc_howto_type *howto,
772 asection *input_section,
780 /* Sanity check the address. */
781 if (address > bfd_get_section_limit (input_bfd, input_section))
782 return bfd_reloc_outofrange;
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;
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)
802 relocation -= (input_section->output_section->vma
803 + input_section->output_offset);
804 if (howto->pcrel_offset)
805 relocation -= address;
808 return _bfd_ns32k_relocate_contents (howto, input_bfd, relocation,