2001-05-25 H.J. Lu <hjl@gnu.org>
[binutils.git] / bfd / aout-arm.c
blobddb03257f41d842bb31946554e7d6f6ddfb351ea
1 /* BFD back-end for raw ARM a.out binaries.
2 Copyright 1994, 1995, 1997, 1998, 1999, 2000
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. */
22 #define N_TXTADDR(x) \
23 ((N_MAGIC(x) == NMAGIC) ? 0x8000 : \
24 (N_MAGIC(x) != ZMAGIC) ? 0 : \
25 (N_SHARED_LIB(x)) ? ((x).a_entry & ~(TARGET_PAGE_SIZE - 1)) : \
26 TEXT_START_ADDR)
28 #define TEXT_START_ADDR 0x8000
29 #define TARGET_PAGE_SIZE 0x8000
30 #define SEGMENT_SIZE TARGET_PAGE_SIZE
31 #define DEFAULT_ARCH bfd_arch_arm
33 #define MY(OP) CAT(aoutarm_,OP)
34 #define N_BADMAG(x) ((((x).a_info & ~007200) != ZMAGIC) && \
35 (((x).a_info & ~006000) != OMAGIC) && \
36 ((x).a_info != NMAGIC))
37 #define N_MAGIC(x) ((x).a_info & ~07200)
39 #include "bfd.h"
40 #include "sysdep.h"
42 #define MYARM(OP) CAT(aoutarm_,OP)
43 reloc_howto_type *MYARM(bfd_reloc_type_lookup)
44 PARAMS((bfd *, bfd_reloc_code_real_type));
45 static boolean MYARM(write_object_contents) PARAMS((bfd *));
47 /* Avoid multiple defininitions from aoutx if supporting standarad a.out
48 as well as our own. */
49 #define NAME(x,y) CAT3(aoutarm,_32_,y)
51 #define MY_bfd_reloc_type_lookup aoutarm_bfd_reloc_type_lookup
53 #include "libaout.h"
54 #include "aout/aout64.h"
56 static bfd_reloc_status_type
57 MY(fix_pcrel_26_done) PARAMS ((bfd *, arelent *, asymbol *, PTR,
58 asection *, bfd *, char **));
60 static bfd_reloc_status_type
61 MY(fix_pcrel_26) PARAMS ((bfd *, arelent *, asymbol *, PTR,
62 asection *, bfd *, char **));
63 static void MY(swap_std_reloc_in) PARAMS ((bfd *, struct reloc_std_external *,
64 arelent *, asymbol **,
65 bfd_size_type));
66 void MY(swap_std_reloc_out) PARAMS ((bfd *, arelent *,
67 struct reloc_std_external *));
69 reloc_howto_type MY(howto_table)[] =
71 /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask
72 pcdone */
73 HOWTO (0, 0, 0, 8, false, 0, complain_overflow_bitfield, 0, "8", true,
74 0x000000ff, 0x000000ff, false),
75 HOWTO (1, 0, 1, 16, false, 0, complain_overflow_bitfield, 0, "16", true,
76 0x0000ffff, 0x0000ffff, false),
77 HOWTO (2, 0, 2, 32, false, 0, complain_overflow_bitfield, 0, "32", true,
78 0xffffffff, 0xffffffff, false),
79 HOWTO (3, 2, 2, 26, true, 0, complain_overflow_signed, MY(fix_pcrel_26),
80 "ARM26", true, 0x00ffffff, 0x00ffffff, true),
81 HOWTO (4, 0, 0, 8, true, 0, complain_overflow_signed, 0, "DISP8", true,
82 0x000000ff, 0x000000ff, true),
83 HOWTO (5, 0, 1, 16, true, 0, complain_overflow_signed, 0, "DISP16", true,
84 0x0000ffff, 0x0000ffff, true),
85 HOWTO (6, 0, 2, 32, true, 0, complain_overflow_signed, 0, "DISP32", true,
86 0xffffffff, 0xffffffff, true),
87 HOWTO (7, 2, 2, 26, false, 0, complain_overflow_signed,
88 MY(fix_pcrel_26_done), "ARM26D", true, 0x0, 0x0,
89 false),
90 EMPTY_HOWTO (-1),
91 HOWTO (9, 0, -1, 16, false, 0, complain_overflow_bitfield, 0, "NEG16", true,
92 0x0000ffff, 0x0000ffff, false),
93 HOWTO (10, 0, -2, 32, false, 0, complain_overflow_bitfield, 0, "NEG32", true,
94 0xffffffff, 0xffffffff, false)
97 #define RELOC_ARM_BITS_NEG_BIG ((unsigned int) 0x08)
98 #define RELOC_ARM_BITS_NEG_LITTLE ((unsigned int) 0x10)
100 reloc_howto_type *
101 MY(reloc_howto) (abfd, rel, r_index, r_extern, r_pcrel)
102 bfd *abfd;
103 struct reloc_std_external *rel;
104 int *r_index;
105 int *r_extern;
106 int *r_pcrel;
108 unsigned int r_length;
109 unsigned int r_pcrel_done;
110 unsigned int r_neg;
111 int index;
113 *r_pcrel = 0;
114 if (bfd_header_big_endian (abfd))
116 *r_index = ((rel->r_index[0] << 16)
117 | (rel->r_index[1] << 8)
118 | rel->r_index[2]);
119 *r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_BIG));
120 r_pcrel_done = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
121 r_neg = (0 != (rel->r_type[0] & RELOC_ARM_BITS_NEG_BIG));
122 r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_BIG)
123 >> RELOC_STD_BITS_LENGTH_SH_BIG);
125 else
127 *r_index = ((rel->r_index[2] << 16)
128 | (rel->r_index[1] << 8)
129 | rel->r_index[0]);
130 *r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE));
131 r_pcrel_done = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
132 r_neg = (0 != (rel->r_type[0] & RELOC_ARM_BITS_NEG_LITTLE));
133 r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE)
134 >> RELOC_STD_BITS_LENGTH_SH_LITTLE);
136 index = r_length + 4 * r_pcrel_done + 8 * r_neg;
137 if (index == 3)
138 *r_pcrel = 1;
140 return MY(howto_table) + index;
143 #define MY_reloc_howto(BFD, REL, IN, EX, PC) \
144 MY(reloc_howto) (BFD, REL, &IN, &EX, &PC)
146 void
147 MY(put_reloc) (abfd, r_extern, r_index, value, howto, reloc)
148 bfd *abfd;
149 int r_extern;
150 int r_index;
151 long value;
152 reloc_howto_type *howto;
153 struct reloc_std_external *reloc;
155 unsigned int r_length;
156 int r_pcrel;
157 int r_neg;
159 PUT_WORD (abfd, value, reloc->r_address);
160 r_length = howto->size ; /* Size as a power of two */
162 /* Special case for branch relocations. */
163 if (howto->type == 3 || howto->type == 7)
164 r_length = 3;
166 r_pcrel = howto->type & 4; /* PC Relative done? */
167 r_neg = howto->type & 8; /* Negative relocation */
168 if (bfd_header_big_endian (abfd))
170 reloc->r_index[0] = r_index >> 16;
171 reloc->r_index[1] = r_index >> 8;
172 reloc->r_index[2] = r_index;
173 reloc->r_type[0] =
174 ((r_extern ? RELOC_STD_BITS_EXTERN_BIG : 0)
175 | (r_pcrel ? RELOC_STD_BITS_PCREL_BIG : 0)
176 | (r_neg ? RELOC_ARM_BITS_NEG_BIG : 0)
177 | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG));
179 else
181 reloc->r_index[2] = r_index >> 16;
182 reloc->r_index[1] = r_index >> 8;
183 reloc->r_index[0] = r_index;
184 reloc->r_type[0] =
185 ((r_extern ? RELOC_STD_BITS_EXTERN_LITTLE : 0)
186 | (r_pcrel ? RELOC_STD_BITS_PCREL_LITTLE : 0)
187 | (r_neg ? RELOC_ARM_BITS_NEG_LITTLE : 0)
188 | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE));
192 #define MY_put_reloc(BFD, EXT, IDX, VAL, HOWTO, RELOC) \
193 MY(put_reloc) (BFD, EXT, IDX, VAL, HOWTO, RELOC)
195 void
196 MY(relocatable_reloc) (howto, abfd, reloc, amount, r_addr)
197 reloc_howto_type *howto;
198 bfd *abfd;
199 struct reloc_std_external *reloc;
200 bfd_vma *amount;
201 bfd_vma r_addr;
203 if (howto->type == 3)
205 if (reloc->r_type[0]
206 & (bfd_header_big_endian (abfd)
207 ? RELOC_STD_BITS_EXTERN_BIG : RELOC_STD_BITS_EXTERN_LITTLE))
209 /* The reloc is still external, so don't modify anything. */
210 *amount = 0;
212 else
214 *amount -= r_addr;
215 /* Change the r_pcrel value -- on the ARM, this bit is set once the
216 relocation is done. */
217 if (bfd_header_big_endian (abfd))
218 reloc->r_type[0] |= RELOC_STD_BITS_PCREL_BIG;
219 else
220 reloc->r_type[0] |= RELOC_STD_BITS_PCREL_LITTLE;
223 else if (howto->type == 7)
224 *amount = 0;
227 #define MY_relocatable_reloc(HOW, BFD, REL, AMOUNT, ADDR) \
228 MY(relocatable_reloc) (HOW, BFD, REL, &(AMOUNT), ADDR)
230 static bfd_reloc_status_type
231 MY(fix_pcrel_26_done) (abfd, reloc_entry, symbol, data, input_section,
232 output_bfd, error_message)
233 bfd *abfd ATTRIBUTE_UNUSED;
234 arelent *reloc_entry ATTRIBUTE_UNUSED;
235 asymbol *symbol ATTRIBUTE_UNUSED;
236 PTR data ATTRIBUTE_UNUSED;
237 asection *input_section ATTRIBUTE_UNUSED;
238 bfd *output_bfd ATTRIBUTE_UNUSED;
239 char **error_message ATTRIBUTE_UNUSED;
241 /* This is dead simple at present. */
242 return bfd_reloc_ok;
245 static bfd_reloc_status_type
246 MY(fix_pcrel_26) (abfd, reloc_entry, symbol, data, input_section,
247 output_bfd, error_message)
248 bfd *abfd;
249 arelent *reloc_entry;
250 asymbol *symbol;
251 PTR data;
252 asection *input_section;
253 bfd *output_bfd;
254 char **error_message ATTRIBUTE_UNUSED;
256 bfd_vma relocation;
257 bfd_size_type addr = reloc_entry->address;
258 long target = bfd_get_32 (abfd, (bfd_byte *) data + addr);
259 bfd_reloc_status_type flag = bfd_reloc_ok;
261 /* If this is an undefined symbol, return error */
262 if (symbol->section == &bfd_und_section
263 && (symbol->flags & BSF_WEAK) == 0)
264 return output_bfd ? bfd_reloc_ok : bfd_reloc_undefined;
266 /* If the sections are different, and we are doing a partial relocation,
267 just ignore it for now. */
268 if (symbol->section->name != input_section->name
269 && output_bfd != (bfd *)NULL)
270 return bfd_reloc_ok;
272 relocation = (target & 0x00ffffff) << 2;
273 relocation = (relocation ^ 0x02000000) - 0x02000000; /* Sign extend */
274 relocation += symbol->value;
275 relocation += symbol->section->output_section->vma;
276 relocation += symbol->section->output_offset;
277 relocation += reloc_entry->addend;
278 relocation -= input_section->output_section->vma;
279 relocation -= input_section->output_offset;
280 relocation -= addr;
281 if (relocation & 3)
282 return bfd_reloc_overflow;
284 /* Check for overflow */
285 if (relocation & 0x02000000)
287 if ((relocation & ~ (bfd_vma) 0x03ffffff) != ~ (bfd_vma) 0x03ffffff)
288 flag = bfd_reloc_overflow;
290 else if (relocation & ~0x03ffffff)
291 flag = bfd_reloc_overflow;
293 target &= ~0x00ffffff;
294 target |= (relocation >> 2) & 0x00ffffff;
295 bfd_put_32 (abfd, target, (bfd_byte *) data + addr);
297 /* Now the ARM magic... Change the reloc type so that it is marked as done.
298 Strictly this is only necessary if we are doing a partial relocation. */
299 reloc_entry->howto = &MY(howto_table)[7];
301 return flag;
304 reloc_howto_type *
305 MY(bfd_reloc_type_lookup) (abfd,code)
306 bfd *abfd;
307 bfd_reloc_code_real_type code;
309 #define ASTD(i,j) case i: return &MY(howto_table)[j]
310 if (code == BFD_RELOC_CTOR)
311 switch (bfd_get_arch_info (abfd)->bits_per_address)
313 case 32:
314 code = BFD_RELOC_32;
315 break;
316 default: return (CONST struct reloc_howto_struct *) 0;
319 switch (code)
321 ASTD (BFD_RELOC_16, 1);
322 ASTD (BFD_RELOC_32, 2);
323 ASTD (BFD_RELOC_ARM_PCREL_BRANCH, 3);
324 ASTD (BFD_RELOC_8_PCREL, 4);
325 ASTD (BFD_RELOC_16_PCREL, 5);
326 ASTD (BFD_RELOC_32_PCREL, 6);
327 default: return (CONST struct reloc_howto_struct *) 0;
331 #define MY_swap_std_reloc_in MY(swap_std_reloc_in)
332 #define MY_swap_std_reloc_out MY(swap_std_reloc_out)
333 #define MY_get_section_contents _bfd_generic_get_section_contents
334 /* #define MY_bfd_link_hash_table_create _bfd_generic_link_hash_table_create */
335 /* #define MY_bfd_link_add_symbols _bfd_generic_link_add_symbols */
336 /* #define MY_bfd_final_link _bfd_generic_final_link */
338 #include "aoutx.h"
340 static void
341 MY_swap_std_reloc_in (abfd, bytes, cache_ptr, symbols, symcount)
342 bfd *abfd;
343 struct reloc_std_external *bytes;
344 arelent *cache_ptr;
345 asymbol **symbols;
346 bfd_size_type symcount ATTRIBUTE_UNUSED;
348 int r_index;
349 int r_extern;
350 int r_pcrel;
351 struct aoutdata *su = &(abfd->tdata.aout_data->a);
353 cache_ptr->address = bfd_h_get_32 (abfd, bytes->r_address);
355 cache_ptr->howto = MY_reloc_howto (abfd, bytes, r_index, r_extern, r_pcrel);
357 MOVE_ADDRESS (0);
360 void
361 MY_swap_std_reloc_out (abfd, g, natptr)
362 bfd *abfd;
363 arelent *g;
364 struct reloc_std_external *natptr;
366 int r_index;
367 asymbol *sym = *(g->sym_ptr_ptr);
368 int r_extern;
369 int r_length;
370 int r_pcrel;
371 int r_neg = 0; /* Negative relocs use the BASEREL bit. */
372 asection *output_section = sym->section->output_section;
374 PUT_WORD(abfd, g->address, natptr->r_address);
376 r_length = g->howto->size ; /* Size as a power of two */
377 if (r_length < 0)
379 r_length = -r_length;
380 r_neg = 1;
383 r_pcrel = (int) g->howto->pc_relative; /* Relative to PC? */
385 /* For RISC iX, in pc-relative relocs the r_pcrel bit means that the
386 relocation has been done already (Only for the 26-bit one I think)???!!!
389 if (g->howto->type == 3)
391 r_length = 3;
392 r_pcrel = 0;
394 else if (g->howto->type == 7)
396 r_length = 3;
397 r_pcrel = 1;
400 #if 0
401 /* For a standard reloc, the addend is in the object file. */
402 r_addend = g->addend + (*(g->sym_ptr_ptr))->section->output_section->vma;
403 #endif
405 /* name was clobbered by aout_write_syms to be symbol index */
407 /* If this relocation is relative to a symbol then set the
408 r_index to the symbols index, and the r_extern bit.
410 Absolute symbols can come in in two ways, either as an offset
411 from the abs section, or as a symbol which has an abs value.
412 check for that here
415 if (bfd_is_com_section (output_section)
416 || output_section == &bfd_abs_section
417 || output_section == &bfd_und_section)
419 if (bfd_abs_section.symbol == sym)
421 /* Whoops, looked like an abs symbol, but is really an offset
422 from the abs section */
423 r_index = 0;
424 r_extern = 0;
426 else
428 /* Fill in symbol */
429 r_extern = 1;
430 r_index = (*(g->sym_ptr_ptr))->KEEPIT;
433 else
435 /* Just an ordinary section */
436 r_extern = 0;
437 r_index = output_section->target_index;
440 /* now the fun stuff */
441 if (bfd_header_big_endian (abfd))
443 natptr->r_index[0] = r_index >> 16;
444 natptr->r_index[1] = r_index >> 8;
445 natptr->r_index[2] = r_index;
446 natptr->r_type[0] =
447 ( (r_extern ? RELOC_STD_BITS_EXTERN_BIG: 0)
448 | (r_pcrel ? RELOC_STD_BITS_PCREL_BIG: 0)
449 | (r_neg ? RELOC_ARM_BITS_NEG_BIG: 0)
450 | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG));
452 else
454 natptr->r_index[2] = r_index >> 16;
455 natptr->r_index[1] = r_index >> 8;
456 natptr->r_index[0] = r_index;
457 natptr->r_type[0] =
458 ( (r_extern ? RELOC_STD_BITS_EXTERN_LITTLE: 0)
459 | (r_pcrel ? RELOC_STD_BITS_PCREL_LITTLE: 0)
460 | (r_neg ? RELOC_ARM_BITS_NEG_LITTLE: 0)
461 | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE));
465 #define MY_BFD_TARGET
467 #include "aout-target.h"
469 extern const bfd_target aout_arm_big_vec;
471 const bfd_target aout_arm_little_vec =
473 "a.out-arm-little", /* name */
474 bfd_target_aout_flavour,
475 BFD_ENDIAN_LITTLE, /* target byte order (little) */
476 BFD_ENDIAN_LITTLE, /* target headers byte order (little) */
477 (HAS_RELOC | EXEC_P | /* object flags */
478 HAS_LINENO | HAS_DEBUG |
479 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
480 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
481 MY_symbol_leading_char,
482 AR_PAD_CHAR, /* ar_pad_char */
483 15, /* ar_max_namelen */
484 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
485 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
486 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
487 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
488 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
489 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
490 {_bfd_dummy_target, MY_object_p, /* bfd_check_format */
491 bfd_generic_archive_p, MY_core_file_p},
492 {bfd_false, MY_mkobject, /* bfd_set_format */
493 _bfd_generic_mkarchive, bfd_false},
494 {bfd_false, MY_write_object_contents, /* bfd_write_contents */
495 _bfd_write_archive_contents, bfd_false},
497 BFD_JUMP_TABLE_GENERIC (MY),
498 BFD_JUMP_TABLE_COPY (MY),
499 BFD_JUMP_TABLE_CORE (MY),
500 BFD_JUMP_TABLE_ARCHIVE (MY),
501 BFD_JUMP_TABLE_SYMBOLS (MY),
502 BFD_JUMP_TABLE_RELOCS (MY),
503 BFD_JUMP_TABLE_WRITE (MY),
504 BFD_JUMP_TABLE_LINK (MY),
505 BFD_JUMP_TABLE_DYNAMIC (MY),
507 & aout_arm_big_vec,
509 (PTR) MY_backend_data,
512 const bfd_target aout_arm_big_vec =
514 "a.out-arm-big", /* name */
515 bfd_target_aout_flavour,
516 BFD_ENDIAN_BIG, /* target byte order (big) */
517 BFD_ENDIAN_BIG, /* target headers byte order (big) */
518 (HAS_RELOC | EXEC_P | /* object flags */
519 HAS_LINENO | HAS_DEBUG |
520 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
521 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
522 MY_symbol_leading_char,
523 AR_PAD_CHAR, /* ar_pad_char */
524 15, /* ar_max_namelen */
525 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
526 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
527 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
528 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
529 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
530 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
531 {_bfd_dummy_target, MY_object_p, /* bfd_check_format */
532 bfd_generic_archive_p, MY_core_file_p},
533 {bfd_false, MY_mkobject, /* bfd_set_format */
534 _bfd_generic_mkarchive, bfd_false},
535 {bfd_false, MY_write_object_contents, /* bfd_write_contents */
536 _bfd_write_archive_contents, bfd_false},
538 BFD_JUMP_TABLE_GENERIC (MY),
539 BFD_JUMP_TABLE_COPY (MY),
540 BFD_JUMP_TABLE_CORE (MY),
541 BFD_JUMP_TABLE_ARCHIVE (MY),
542 BFD_JUMP_TABLE_SYMBOLS (MY),
543 BFD_JUMP_TABLE_RELOCS (MY),
544 BFD_JUMP_TABLE_WRITE (MY),
545 BFD_JUMP_TABLE_LINK (MY),
546 BFD_JUMP_TABLE_DYNAMIC (MY),
548 & aout_arm_little_vec,
550 (PTR) MY_backend_data,