1 /* BFD back-end for Intel arm COFF files.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 #include "coff/internal.h"
36 static bfd_reloc_status_type
37 aoutarm_fix_pcrel_26_done
PARAMS ((bfd
*, arelent
*, asymbol
*, PTR
,
38 asection
*, bfd
*, char **));
40 static bfd_reloc_status_type
41 aoutarm_fix_pcrel_26
PARAMS ((bfd
*, arelent
*, asymbol
*, PTR
,
42 asection
*, bfd
*, char **));
45 static bfd_reloc_status_type coff_arm_reloc
46 PARAMS ((bfd
*, arelent
*, asymbol
*, PTR
, asection
*, bfd
*, char **));
48 static bfd_reloc_status_type
49 coff_arm_reloc (abfd
, reloc_entry
, symbol
, data
, input_section
, output_bfd
,
55 asection
*input_section
;
61 if (output_bfd
== (bfd
*) NULL
)
62 return bfd_reloc_continue
;
64 if (bfd_is_com_section (symbol
->section
))
66 /* We are relocating a common symbol. The current value in the
67 object file is ORIG + OFFSET, where ORIG is the value of the
68 common symbol as seen by the object file when it was compiled
69 (this may be zero if the symbol was undefined) and OFFSET is
70 the offset into the common symbol (normally zero, but may be
71 non-zero when referring to a field in a common structure).
72 ORIG is the negative of reloc_entry->addend, which is set by
73 the CALC_ADDEND macro below. We want to replace the value in
74 the object file with NEW + OFFSET, where NEW is the value of
75 the common symbol which we are going to put in the final
76 object file. NEW is symbol->value. */
77 diff
= symbol
->value
+ reloc_entry
->addend
;
81 /* For some reason bfd_perform_relocation always effectively
82 ignores the addend for a COFF target when producing
83 relocateable output. This seems to be always wrong for 386
84 COFF, so we handle the addend here instead. */
85 diff
= reloc_entry
->addend
;
89 x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
93 reloc_howto_type
*howto
= reloc_entry
->howto
;
94 unsigned char *addr
= (unsigned char *) data
+ reloc_entry
->address
;
100 char x
= bfd_get_8 (abfd
, addr
);
102 bfd_put_8 (abfd
, x
, addr
);
108 short x
= bfd_get_16 (abfd
, addr
);
110 bfd_put_16 (abfd
, x
, addr
);
116 long x
= bfd_get_32 (abfd
, addr
);
118 bfd_put_32 (abfd
, x
, addr
);
127 /* Now let bfd_perform_relocation finish everything up. */
128 return bfd_reloc_continue
;
131 static reloc_howto_type aoutarm_std_reloc_howto
[] =
133 /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone */
140 complain_overflow_bitfield
, /* ovf */
141 coff_arm_reloc
, /* sf */
144 0x000000ff, /*read mask */
145 0x000000ff, /* setmask */
146 PCRELOFFSET
/* pcdone */),
153 complain_overflow_bitfield
,
166 complain_overflow_bitfield
,
179 complain_overflow_signed
,
180 aoutarm_fix_pcrel_26
,
192 complain_overflow_signed
,
205 complain_overflow_signed
,
218 complain_overflow_signed
,
231 complain_overflow_signed
,
232 aoutarm_fix_pcrel_26_done
,
245 complain_overflow_bitfield
,
258 complain_overflow_bitfield
,
269 #define RTYPE2HOWTO(cache_ptr, dst) \
270 (cache_ptr)->howto = aoutarm_std_reloc_howto + (dst)->r_type;
272 static bfd_reloc_status_type
273 aoutarm_fix_pcrel_26_done (abfd
, reloc_entry
, symbol
, data
, input_section
,
274 output_bfd
, error_message
)
276 arelent
*reloc_entry
;
279 asection
*input_section
;
281 char **error_message
;
283 /* This is dead simple at present. */
287 static bfd_reloc_status_type
288 aoutarm_fix_pcrel_26 (abfd
, reloc_entry
, symbol
, data
, input_section
,
289 output_bfd
, error_message
)
291 arelent
*reloc_entry
;
294 asection
*input_section
;
296 char **error_message
;
299 bfd_size_type addr
= reloc_entry
->address
;
300 long target
= bfd_get_32 (abfd
, (bfd_byte
*) data
+ addr
);
301 bfd_reloc_status_type flag
= bfd_reloc_ok
;
303 /* If this is an undefined symbol, return error */
304 if (symbol
->section
== &bfd_und_section
305 && (symbol
->flags
& BSF_WEAK
) == 0)
306 return output_bfd
? bfd_reloc_continue
: bfd_reloc_undefined
;
308 /* If the sections are different, and we are doing a partial relocation,
309 just ignore it for now. */
310 if (symbol
->section
->name
!= input_section
->name
311 && output_bfd
!= (bfd
*)NULL
)
312 return bfd_reloc_continue
;
314 relocation
= (target
& 0x00ffffff) << 2;
315 relocation
= (relocation
^ 0x02000000) - 0x02000000; /* Sign extend */
316 relocation
+= symbol
->value
;
317 relocation
+= symbol
->section
->output_section
->vma
;
318 relocation
+= symbol
->section
->output_offset
;
319 relocation
+= reloc_entry
->addend
;
320 relocation
-= input_section
->output_section
->vma
;
321 relocation
-= input_section
->output_offset
;
324 return bfd_reloc_overflow
;
326 /* Check for overflow */
327 if (relocation
& 0x02000000)
329 if ((relocation
& ~0x03ffffff) != ~0x03ffffff)
330 flag
= bfd_reloc_overflow
;
332 else if (relocation
& ~0x03ffffff)
333 flag
= bfd_reloc_overflow
;
335 target
&= ~0x00ffffff;
336 target
|= (relocation
>> 2) & 0x00ffffff;
337 bfd_put_32 (abfd
, target
, (bfd_byte
*) data
+ addr
);
339 /* Now the ARM magic... Change the reloc type so that it is marked as done.
340 Strictly this is only necessary if we are doing a partial relocation. */
341 reloc_entry
->howto
= &aoutarm_std_reloc_howto
[7];
346 static CONST
struct reloc_howto_struct
*
347 arm_reloc_type_lookup(abfd
,code
)
349 bfd_reloc_code_real_type code
;
351 #define ASTD(i,j) case i: return &aoutarm_std_reloc_howto[j]
352 if (code
== BFD_RELOC_CTOR
)
353 switch (bfd_get_arch_info (abfd
)->bits_per_address
)
358 default: return (CONST
struct reloc_howto_struct
*) 0;
363 ASTD (BFD_RELOC_16
, 1);
364 ASTD (BFD_RELOC_32
, 2);
365 ASTD (BFD_RELOC_ARM_PCREL_BRANCH
, 3);
366 ASTD (BFD_RELOC_8_PCREL
, 4);
367 ASTD (BFD_RELOC_16_PCREL
, 5);
368 ASTD (BFD_RELOC_32_PCREL
, 6);
369 default: return (CONST
struct reloc_howto_struct
*) 0;
374 #define coff_bfd_reloc_type_lookup arm_reloc_type_lookup
376 static reloc_howto_type
*coff_arm_rtype_to_howto
377 PARAMS ((bfd
*, asection
*, struct internal_reloc
*,
378 struct coff_link_hash_entry
*, struct internal_syment
*,
382 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
383 /* The page size is a guess based on ELF. */
384 #define COFF_PAGE_SIZE 0x1000
386 /* For some reason when using arm COFF the value stored in the .text
387 section for a reference to a common symbol is the value itself plus
388 any desired offset. Ian Taylor, Cygnus Support. */
390 /* If we are producing relocateable output, we need to do some
391 adjustments to the object file that are not done by the
392 bfd_perform_relocation function. This function is called by every
393 reloc type to make any required adjustments. */
395 static bfd_reloc_status_type
396 aacoff_arm_reloc (abfd
, reloc_entry
, symbol
, data
, input_section
, output_bfd
,
399 arelent
*reloc_entry
;
402 asection
*input_section
;
404 char **error_message
;
408 if (output_bfd
== (bfd
*) NULL
)
409 return bfd_reloc_continue
;
411 if (bfd_is_com_section (symbol
->section
))
413 /* We are relocating a common symbol. The current value in the
414 object file is ORIG + OFFSET, where ORIG is the value of the
415 common symbol as seen by the object file when it was compiled
416 (this may be zero if the symbol was undefined) and OFFSET is
417 the offset into the common symbol (normally zero, but may be
418 non-zero when referring to a field in a common structure).
419 ORIG is the negative of reloc_entry->addend, which is set by
420 the CALC_ADDEND macro below. We want to replace the value in
421 the object file with NEW + OFFSET, where NEW is the value of
422 the common symbol which we are going to put in the final
423 object file. NEW is symbol->value. */
424 diff
= symbol
->value
+ reloc_entry
->addend
;
428 /* For some reason bfd_perform_relocation always effectively
429 ignores the addend for a COFF target when producing
430 relocateable output. This seems to be always wrong for arm
431 COFF, so we handle the addend here instead. */
432 diff
= reloc_entry
->addend
;
436 x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
440 reloc_howto_type
*howto
= reloc_entry
->howto
;
441 unsigned char *addr
= (unsigned char *) data
+ reloc_entry
->address
;
447 char x
= bfd_get_8 (abfd
, addr
);
449 bfd_put_8 (abfd
, x
, addr
);
455 short x
= bfd_get_16 (abfd
, addr
);
457 bfd_put_16 (abfd
, x
, addr
);
463 long x
= bfd_get_32 (abfd
, addr
);
465 bfd_put_32 (abfd
, x
, addr
);
474 /* Now let bfd_perform_relocation finish everything up. */
475 return bfd_reloc_continue
;
482 /* Turn a howto into a reloc nunmber */
484 #define SELECT_RELOC(x,howto) { x.r_type = howto->type; }
485 #define BADMAG(x) ARMBADMAG(x)
486 #define ARM 1 /* Customize coffcode.h */
489 /* On SCO Unix 3.2.2 the native assembler generates two .data
490 sections. We handle that by renaming the second one to .data2. It
491 does no harm to do this for any arm COFF target. */
492 #define TWO_DATA_SECS
494 /* For arm COFF a STYP_NOLOAD | STYP_BSS section is part of a shared
495 library. On some other COFF targets STYP_BSS is normally
497 #define BSS_NOLOAD_IS_SHARED_LIBRARY
500 /* We use the special COFF backend linker. */
501 #define coff_relocate_section _bfd_coff_generic_relocate_section
505 #include "coffcode.h"
507 static const bfd_target
*
511 return coff_object_p(a
);
524 "coff-arm", /* name */
526 bfd_target_coff_flavour
,
527 false, /* data byte order is little */
528 false, /* header byte order is little */
530 (HAS_RELOC
| EXEC_P
| /* object flags */
531 HAS_LINENO
| HAS_DEBUG
|
532 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| D_PAGED
),
534 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
535 #ifdef TARGET_UNDERSCORE
536 TARGET_UNDERSCORE
, /* leading underscore */
538 0, /* leading underscore */
540 '/', /* ar_pad_char */
541 15, /* ar_max_namelen */
543 2, /* minimum alignment power */
544 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
545 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
546 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* data */
547 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
548 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
549 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* hdrs */
551 /* Note that we allow an object file to be treated as a core file as well. */
552 {_bfd_dummy_target
, i3coff_object_p
, /* bfd_check_format */
553 bfd_generic_archive_p
, i3coff_object_p
},
554 {bfd_false
, coff_mkobject
, _bfd_generic_mkarchive
, /* bfd_set_format */
556 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
557 _bfd_write_archive_contents
, bfd_false
},
559 BFD_JUMP_TABLE_GENERIC (coff
),
560 BFD_JUMP_TABLE_COPY (coff
),
561 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
562 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
563 BFD_JUMP_TABLE_SYMBOLS (coff
),
564 BFD_JUMP_TABLE_RELOCS (coff
),
565 BFD_JUMP_TABLE_WRITE (coff
),
566 BFD_JUMP_TABLE_LINK (coff
),
567 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),