1 /* BFD back-end for raw ARM a.out binaries.
2 Copyright 1994, 1995, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4 Contributed by Richard Earnshaw (rwe@pegasus.esprit.ec.org)
6 This file is part of BFD, the Binary File Descriptor library.
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 2 of the License, or
11 (at your option) any later version.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 /* Avoid multiple defininitions from aoutx if supporting standarad a.out
25 as well as our own. */
26 #define NAME(x,y) CAT3(aoutarm,_32_,y)
29 #include "aout/aout64.h"
31 #define N_TXTADDR(x) \
32 ((N_MAGIC(x) == NMAGIC) ? 0x8000 : \
33 (N_MAGIC(x) != ZMAGIC) ? 0 : \
34 (N_SHARED_LIB(x)) ? ((x).a_entry & ~(TARGET_PAGE_SIZE - 1)) : \
37 #define TEXT_START_ADDR 0x8000
38 #define TARGET_PAGE_SIZE 0x8000
39 #define SEGMENT_SIZE TARGET_PAGE_SIZE
40 #define DEFAULT_ARCH bfd_arch_arm
42 #define MY(OP) CAT(aoutarm_,OP)
43 #define N_BADMAG(x) ((((x).a_info & ~007200) != ZMAGIC) && \
44 (((x).a_info & ~006000) != OMAGIC) && \
45 ((x).a_info != NMAGIC))
46 #define N_MAGIC(x) ((x).a_info & ~07200)
48 #define MY_bfd_reloc_type_lookup aoutarm_bfd_reloc_type_lookup
50 static boolean
MY(write_object_contents
) PARAMS ((bfd
*));
51 static bfd_reloc_status_type
MY(fix_pcrel_26_done
) PARAMS ((bfd
*, arelent
*, asymbol
*, PTR
, asection
*, bfd
*, char **));
52 static bfd_reloc_status_type
MY(fix_pcrel_26
) PARAMS ((bfd
*, arelent
*, asymbol
*, PTR
, asection
*, bfd
*, char **));
53 static void MY(swap_std_reloc_in
) PARAMS ((bfd
*, struct reloc_std_external
*, arelent
*, asymbol
**, bfd_size_type
));
54 reloc_howto_type
* MY(bfd_reloc_type_lookup
) PARAMS ((bfd
*, bfd_reloc_code_real_type
));
55 reloc_howto_type
* MY(reloc_howto
) PARAMS ((bfd
*, struct reloc_std_external
*, int *, int *, int *));
56 void MY(put_reloc
) PARAMS ((bfd
*, int, int, long, reloc_howto_type
*, struct reloc_std_external
*));
57 void MY(relocatable_reloc
) PARAMS ((reloc_howto_type
*, bfd
*, struct reloc_std_external
*, bfd_vma
*, bfd_vma
));
58 void MY(swap_std_reloc_out
) PARAMS ((bfd
*, arelent
*, struct reloc_std_external
*));
60 reloc_howto_type
MY(howto_table
)[] =
62 /* Type rs size bsz pcrel bitpos ovrf sf name part_inpl
63 readmask setmask pcdone. */
64 HOWTO (0, 0, 0, 8, false, 0, complain_overflow_bitfield
, 0, "8", true,
65 0x000000ff, 0x000000ff, false),
66 HOWTO (1, 0, 1, 16, false, 0, complain_overflow_bitfield
, 0, "16", true,
67 0x0000ffff, 0x0000ffff, false),
68 HOWTO (2, 0, 2, 32, false, 0, complain_overflow_bitfield
, 0, "32", true,
69 0xffffffff, 0xffffffff, false),
70 HOWTO (3, 2, 2, 26, true, 0, complain_overflow_signed
, MY(fix_pcrel_26
),
71 "ARM26", true, 0x00ffffff, 0x00ffffff, true),
72 HOWTO (4, 0, 0, 8, true, 0, complain_overflow_signed
, 0, "DISP8", true,
73 0x000000ff, 0x000000ff, true),
74 HOWTO (5, 0, 1, 16, true, 0, complain_overflow_signed
, 0, "DISP16", true,
75 0x0000ffff, 0x0000ffff, true),
76 HOWTO (6, 0, 2, 32, true, 0, complain_overflow_signed
, 0, "DISP32", true,
77 0xffffffff, 0xffffffff, true),
78 HOWTO (7, 2, 2, 26, false, 0, complain_overflow_signed
,
79 MY(fix_pcrel_26_done
), "ARM26D", true, 0x0, 0x0,
82 HOWTO (9, 0, -1, 16, false, 0, complain_overflow_bitfield
, 0, "NEG16", true,
83 0x0000ffff, 0x0000ffff, false),
84 HOWTO (10, 0, -2, 32, false, 0, complain_overflow_bitfield
, 0, "NEG32", true,
85 0xffffffff, 0xffffffff, false)
88 #define RELOC_ARM_BITS_NEG_BIG ((unsigned int) 0x08)
89 #define RELOC_ARM_BITS_NEG_LITTLE ((unsigned int) 0x10)
92 MY(reloc_howto
) (abfd
, rel
, r_index
, r_extern
, r_pcrel
)
94 struct reloc_std_external
*rel
;
99 unsigned int r_length
;
100 unsigned int r_pcrel_done
;
105 if (bfd_header_big_endian (abfd
))
107 *r_index
= ((rel
->r_index
[0] << 16)
108 | (rel
->r_index
[1] << 8)
110 *r_extern
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_EXTERN_BIG
));
111 r_pcrel_done
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_PCREL_BIG
));
112 r_neg
= (0 != (rel
->r_type
[0] & RELOC_ARM_BITS_NEG_BIG
));
113 r_length
= ((rel
->r_type
[0] & RELOC_STD_BITS_LENGTH_BIG
)
114 >> RELOC_STD_BITS_LENGTH_SH_BIG
);
118 *r_index
= ((rel
->r_index
[2] << 16)
119 | (rel
->r_index
[1] << 8)
121 *r_extern
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_EXTERN_LITTLE
));
122 r_pcrel_done
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_PCREL_LITTLE
));
123 r_neg
= (0 != (rel
->r_type
[0] & RELOC_ARM_BITS_NEG_LITTLE
));
124 r_length
= ((rel
->r_type
[0] & RELOC_STD_BITS_LENGTH_LITTLE
)
125 >> RELOC_STD_BITS_LENGTH_SH_LITTLE
);
127 index
= r_length
+ 4 * r_pcrel_done
+ 8 * r_neg
;
131 return MY(howto_table
) + index
;
134 #define MY_reloc_howto(BFD, REL, IN, EX, PC) \
135 MY(reloc_howto) (BFD, REL, &IN, &EX, &PC)
138 MY(put_reloc
) (abfd
, r_extern
, r_index
, value
, howto
, reloc
)
143 reloc_howto_type
*howto
;
144 struct reloc_std_external
*reloc
;
146 unsigned int r_length
;
150 PUT_WORD (abfd
, value
, reloc
->r_address
);
151 /* Size as a power of two. */
152 r_length
= howto
->size
;
154 /* Special case for branch relocations. */
155 if (howto
->type
== 3 || howto
->type
== 7)
158 r_pcrel
= howto
->type
& 4; /* PC Relative done? */
159 r_neg
= howto
->type
& 8; /* Negative relocation. */
161 if (bfd_header_big_endian (abfd
))
163 reloc
->r_index
[0] = r_index
>> 16;
164 reloc
->r_index
[1] = r_index
>> 8;
165 reloc
->r_index
[2] = r_index
;
167 ((r_extern
? RELOC_STD_BITS_EXTERN_BIG
: 0)
168 | (r_pcrel
? RELOC_STD_BITS_PCREL_BIG
: 0)
169 | (r_neg
? RELOC_ARM_BITS_NEG_BIG
: 0)
170 | (r_length
<< RELOC_STD_BITS_LENGTH_SH_BIG
));
174 reloc
->r_index
[2] = r_index
>> 16;
175 reloc
->r_index
[1] = r_index
>> 8;
176 reloc
->r_index
[0] = r_index
;
178 ((r_extern
? RELOC_STD_BITS_EXTERN_LITTLE
: 0)
179 | (r_pcrel
? RELOC_STD_BITS_PCREL_LITTLE
: 0)
180 | (r_neg
? RELOC_ARM_BITS_NEG_LITTLE
: 0)
181 | (r_length
<< RELOC_STD_BITS_LENGTH_SH_LITTLE
));
185 #define MY_put_reloc(BFD, EXT, IDX, VAL, HOWTO, RELOC) \
186 MY(put_reloc) (BFD, EXT, IDX, VAL, HOWTO, RELOC)
189 MY(relocatable_reloc
) (howto
, abfd
, reloc
, amount
, r_addr
)
190 reloc_howto_type
*howto
;
192 struct reloc_std_external
*reloc
;
196 if (howto
->type
== 3)
199 & (bfd_header_big_endian (abfd
)
200 ? RELOC_STD_BITS_EXTERN_BIG
: RELOC_STD_BITS_EXTERN_LITTLE
))
202 /* The reloc is still external, so don't modify anything. */
208 /* Change the r_pcrel value -- on the ARM, this bit is set once the
209 relocation is done. */
210 if (bfd_header_big_endian (abfd
))
211 reloc
->r_type
[0] |= RELOC_STD_BITS_PCREL_BIG
;
213 reloc
->r_type
[0] |= RELOC_STD_BITS_PCREL_LITTLE
;
216 else if (howto
->type
== 7)
220 #define MY_relocatable_reloc(HOW, BFD, REL, AMOUNT, ADDR) \
221 MY(relocatable_reloc) (HOW, BFD, REL, &(AMOUNT), ADDR)
223 static bfd_reloc_status_type
224 MY(fix_pcrel_26_done
) (abfd
, reloc_entry
, symbol
, data
, input_section
,
225 output_bfd
, error_message
)
226 bfd
*abfd ATTRIBUTE_UNUSED
;
227 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
228 asymbol
*symbol ATTRIBUTE_UNUSED
;
229 PTR data ATTRIBUTE_UNUSED
;
230 asection
*input_section ATTRIBUTE_UNUSED
;
231 bfd
*output_bfd ATTRIBUTE_UNUSED
;
232 char **error_message ATTRIBUTE_UNUSED
;
234 /* This is dead simple at present. */
238 static bfd_reloc_status_type
239 MY(fix_pcrel_26
) (abfd
, reloc_entry
, symbol
, data
, input_section
,
240 output_bfd
, error_message
)
242 arelent
*reloc_entry
;
245 asection
*input_section
;
247 char **error_message ATTRIBUTE_UNUSED
;
250 bfd_size_type addr
= reloc_entry
->address
;
251 long target
= bfd_get_32 (abfd
, (bfd_byte
*) data
+ addr
);
252 bfd_reloc_status_type flag
= bfd_reloc_ok
;
254 /* If this is an undefined symbol, return error. */
255 if (symbol
->section
== &bfd_und_section
256 && (symbol
->flags
& BSF_WEAK
) == 0)
257 return output_bfd
? bfd_reloc_ok
: bfd_reloc_undefined
;
259 /* If the sections are different, and we are doing a partial relocation,
260 just ignore it for now. */
261 if (symbol
->section
->name
!= input_section
->name
262 && output_bfd
!= (bfd
*)NULL
)
265 relocation
= (target
& 0x00ffffff) << 2;
266 relocation
= (relocation
^ 0x02000000) - 0x02000000; /* Sign extend. */
267 relocation
+= symbol
->value
;
268 relocation
+= symbol
->section
->output_section
->vma
;
269 relocation
+= symbol
->section
->output_offset
;
270 relocation
+= reloc_entry
->addend
;
271 relocation
-= input_section
->output_section
->vma
;
272 relocation
-= input_section
->output_offset
;
275 return bfd_reloc_overflow
;
277 /* Check for overflow. */
278 if (relocation
& 0x02000000)
280 if ((relocation
& ~ (bfd_vma
) 0x03ffffff) != ~ (bfd_vma
) 0x03ffffff)
281 flag
= bfd_reloc_overflow
;
283 else if (relocation
& ~0x03ffffff)
284 flag
= bfd_reloc_overflow
;
286 target
&= ~0x00ffffff;
287 target
|= (relocation
>> 2) & 0x00ffffff;
288 bfd_put_32 (abfd
, target
, (bfd_byte
*) data
+ addr
);
290 /* Now the ARM magic... Change the reloc type so that it is marked as done.
291 Strictly this is only necessary if we are doing a partial relocation. */
292 reloc_entry
->howto
= &MY(howto_table
)[7];
298 MY(bfd_reloc_type_lookup
) (abfd
,code
)
300 bfd_reloc_code_real_type code
;
302 #define ASTD(i,j) case i: return &MY(howto_table)[j]
303 if (code
== BFD_RELOC_CTOR
)
304 switch (bfd_get_arch_info (abfd
)->bits_per_address
)
310 return (CONST
struct reloc_howto_struct
*) 0;
315 ASTD (BFD_RELOC_16
, 1);
316 ASTD (BFD_RELOC_32
, 2);
317 ASTD (BFD_RELOC_ARM_PCREL_BRANCH
, 3);
318 ASTD (BFD_RELOC_8_PCREL
, 4);
319 ASTD (BFD_RELOC_16_PCREL
, 5);
320 ASTD (BFD_RELOC_32_PCREL
, 6);
322 return (CONST
struct reloc_howto_struct
*) 0;
326 #define MY_swap_std_reloc_in MY(swap_std_reloc_in)
327 #define MY_swap_std_reloc_out MY(swap_std_reloc_out)
328 #define MY_get_section_contents _bfd_generic_get_section_contents
329 /* #define MY_bfd_link_hash_table_create _bfd_generic_link_hash_table_create */
330 /* #define MY_bfd_link_add_symbols _bfd_generic_link_add_symbols */
331 /* #define MY_bfd_final_link _bfd_generic_final_link */
336 MY_swap_std_reloc_in (abfd
, bytes
, cache_ptr
, symbols
, symcount
)
338 struct reloc_std_external
*bytes
;
341 bfd_size_type symcount ATTRIBUTE_UNUSED
;
346 struct aoutdata
*su
= &(abfd
->tdata
.aout_data
->a
);
348 cache_ptr
->address
= bfd_h_get_32 (abfd
, bytes
->r_address
);
350 cache_ptr
->howto
= MY_reloc_howto (abfd
, bytes
, r_index
, r_extern
, r_pcrel
);
356 MY_swap_std_reloc_out (abfd
, g
, natptr
)
359 struct reloc_std_external
*natptr
;
362 asymbol
*sym
= *(g
->sym_ptr_ptr
);
366 int r_neg
= 0; /* Negative relocs use the BASEREL bit. */
367 asection
*output_section
= sym
->section
->output_section
;
369 PUT_WORD(abfd
, g
->address
, natptr
->r_address
);
371 r_length
= g
->howto
->size
; /* Size as a power of two */
374 r_length
= -r_length
;
378 r_pcrel
= (int) g
->howto
->pc_relative
; /* Relative to PC? */
380 /* For RISC iX, in pc-relative relocs the r_pcrel bit means that the
381 relocation has been done already (Only for the 26-bit one I think)???!!!
384 if (g
->howto
->type
== 3)
389 else if (g
->howto
->type
== 7)
396 /* For a standard reloc, the addend is in the object file. */
397 r_addend
= g
->addend
+ (*(g
->sym_ptr_ptr
))->section
->output_section
->vma
;
400 /* name was clobbered by aout_write_syms to be symbol index */
402 /* If this relocation is relative to a symbol then set the
403 r_index to the symbols index, and the r_extern bit.
405 Absolute symbols can come in in two ways, either as an offset
406 from the abs section, or as a symbol which has an abs value.
410 if (bfd_is_com_section (output_section
)
411 || output_section
== &bfd_abs_section
412 || output_section
== &bfd_und_section
)
414 if (bfd_abs_section
.symbol
== sym
)
416 /* Whoops, looked like an abs symbol, but is really an offset
417 from the abs section. */
423 /* Fill in symbol. */
425 r_index
= (*(g
->sym_ptr_ptr
))->KEEPIT
;
430 /* Just an ordinary section. */
432 r_index
= output_section
->target_index
;
435 /* Now the fun stuff. */
436 if (bfd_header_big_endian (abfd
))
438 natptr
->r_index
[0] = r_index
>> 16;
439 natptr
->r_index
[1] = r_index
>> 8;
440 natptr
->r_index
[2] = r_index
;
442 ( (r_extern
? RELOC_STD_BITS_EXTERN_BIG
: 0)
443 | (r_pcrel
? RELOC_STD_BITS_PCREL_BIG
: 0)
444 | (r_neg
? RELOC_ARM_BITS_NEG_BIG
: 0)
445 | (r_length
<< RELOC_STD_BITS_LENGTH_SH_BIG
));
449 natptr
->r_index
[2] = r_index
>> 16;
450 natptr
->r_index
[1] = r_index
>> 8;
451 natptr
->r_index
[0] = r_index
;
453 ( (r_extern
? RELOC_STD_BITS_EXTERN_LITTLE
: 0)
454 | (r_pcrel
? RELOC_STD_BITS_PCREL_LITTLE
: 0)
455 | (r_neg
? RELOC_ARM_BITS_NEG_LITTLE
: 0)
456 | (r_length
<< RELOC_STD_BITS_LENGTH_SH_LITTLE
));
460 #define MY_BFD_TARGET
462 #include "aout-target.h"
464 extern const bfd_target aout_arm_big_vec
;
466 const bfd_target aout_arm_little_vec
=
468 "a.out-arm-little", /* name */
469 bfd_target_aout_flavour
,
470 BFD_ENDIAN_LITTLE
, /* target byte order (little) */
471 BFD_ENDIAN_LITTLE
, /* target headers byte order (little) */
472 (HAS_RELOC
| EXEC_P
| /* object flags */
473 HAS_LINENO
| HAS_DEBUG
|
474 HAS_SYMS
| HAS_LOCALS
| DYNAMIC
| WP_TEXT
| D_PAGED
),
475 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
| SEC_CODE
| SEC_DATA
),
476 MY_symbol_leading_char
,
477 AR_PAD_CHAR
, /* ar_pad_char */
478 15, /* ar_max_namelen */
479 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
480 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
481 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* data */
482 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
483 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
484 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* hdrs */
485 {_bfd_dummy_target
, MY_object_p
, /* bfd_check_format */
486 bfd_generic_archive_p
, MY_core_file_p
},
487 {bfd_false
, MY_mkobject
, /* bfd_set_format */
488 _bfd_generic_mkarchive
, bfd_false
},
489 {bfd_false
, MY_write_object_contents
, /* bfd_write_contents */
490 _bfd_write_archive_contents
, bfd_false
},
492 BFD_JUMP_TABLE_GENERIC (MY
),
493 BFD_JUMP_TABLE_COPY (MY
),
494 BFD_JUMP_TABLE_CORE (MY
),
495 BFD_JUMP_TABLE_ARCHIVE (MY
),
496 BFD_JUMP_TABLE_SYMBOLS (MY
),
497 BFD_JUMP_TABLE_RELOCS (MY
),
498 BFD_JUMP_TABLE_WRITE (MY
),
499 BFD_JUMP_TABLE_LINK (MY
),
500 BFD_JUMP_TABLE_DYNAMIC (MY
),
504 (PTR
) MY_backend_data
,
507 const bfd_target aout_arm_big_vec
=
509 "a.out-arm-big", /* name */
510 bfd_target_aout_flavour
,
511 BFD_ENDIAN_BIG
, /* target byte order (big) */
512 BFD_ENDIAN_BIG
, /* target headers byte order (big) */
513 (HAS_RELOC
| EXEC_P
| /* object flags */
514 HAS_LINENO
| HAS_DEBUG
|
515 HAS_SYMS
| HAS_LOCALS
| DYNAMIC
| WP_TEXT
| D_PAGED
),
516 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
| SEC_CODE
| SEC_DATA
),
517 MY_symbol_leading_char
,
518 AR_PAD_CHAR
, /* ar_pad_char */
519 15, /* ar_max_namelen */
520 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
521 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
522 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* data */
523 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
524 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
525 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* hdrs */
526 {_bfd_dummy_target
, MY_object_p
, /* bfd_check_format */
527 bfd_generic_archive_p
, MY_core_file_p
},
528 {bfd_false
, MY_mkobject
, /* bfd_set_format */
529 _bfd_generic_mkarchive
, bfd_false
},
530 {bfd_false
, MY_write_object_contents
, /* bfd_write_contents */
531 _bfd_write_archive_contents
, bfd_false
},
533 BFD_JUMP_TABLE_GENERIC (MY
),
534 BFD_JUMP_TABLE_COPY (MY
),
535 BFD_JUMP_TABLE_CORE (MY
),
536 BFD_JUMP_TABLE_ARCHIVE (MY
),
537 BFD_JUMP_TABLE_SYMBOLS (MY
),
538 BFD_JUMP_TABLE_RELOCS (MY
),
539 BFD_JUMP_TABLE_WRITE (MY
),
540 BFD_JUMP_TABLE_LINK (MY
),
541 BFD_JUMP_TABLE_DYNAMIC (MY
),
543 & aout_arm_little_vec
,
545 (PTR
) MY_backend_data
,