Touches most files in bfd/, so likely will be blamed for everything..
[binutils.git] / bfd / aout-arm.c
blob5ac8a61ccd44e0ea5e2013777ed98945cccd235b
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. */
22 #include "bfd.h"
23 #include "sysdep.h"
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)
28 #define N_TXTADDR(x) \
29 ((N_MAGIC (x) == NMAGIC) \
30 ? (bfd_vma) 0x8000 \
31 : ((N_MAGIC (x) != ZMAGIC) \
32 ? (bfd_vma) 0 \
33 : ((N_SHARED_LIB (x)) \
34 ? ((x).a_entry & ~(bfd_vma) (TARGET_PAGE_SIZE - 1)) \
35 : (bfd_vma) TEXT_START_ADDR)))
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 #include "libaout.h"
51 #include "aout/aout64.h"
53 static boolean MY(write_object_contents)
54 PARAMS ((bfd *));
55 static bfd_reloc_status_type MY(fix_pcrel_26_done)
56 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
57 static bfd_reloc_status_type MY(fix_pcrel_26)
58 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
59 static void MY(swap_std_reloc_in)
60 PARAMS ((bfd *, struct reloc_std_external *, arelent *, asymbol **,
61 bfd_size_type));
62 reloc_howto_type *MY(bfd_reloc_type_lookup)
63 PARAMS ((bfd *, bfd_reloc_code_real_type));
64 reloc_howto_type * MY(reloc_howto)
65 PARAMS ((bfd *, struct reloc_std_external *, int *, int *, int *));
66 void MY(put_reloc)
67 PARAMS ((bfd *, int, int, bfd_vma, reloc_howto_type *,
68 struct reloc_std_external *));
69 void MY(relocatable_reloc)
70 PARAMS ((reloc_howto_type *, bfd *, struct reloc_std_external *, bfd_vma *,
71 bfd_vma));
72 void MY(swap_std_reloc_out)
73 PARAMS ((bfd *, arelent *, struct reloc_std_external *));
75 reloc_howto_type MY(howto_table)[] =
77 /* Type rs size bsz pcrel bitpos ovrf sf name part_inpl
78 readmask setmask pcdone. */
79 HOWTO (0, 0, 0, 8, false, 0, complain_overflow_bitfield, 0, "8", true,
80 0x000000ff, 0x000000ff, false),
81 HOWTO (1, 0, 1, 16, false, 0, complain_overflow_bitfield, 0, "16", true,
82 0x0000ffff, 0x0000ffff, false),
83 HOWTO (2, 0, 2, 32, false, 0, complain_overflow_bitfield, 0, "32", true,
84 0xffffffff, 0xffffffff, false),
85 HOWTO (3, 2, 2, 26, true, 0, complain_overflow_signed, MY(fix_pcrel_26),
86 "ARM26", true, 0x00ffffff, 0x00ffffff, true),
87 HOWTO (4, 0, 0, 8, true, 0, complain_overflow_signed, 0, "DISP8", true,
88 0x000000ff, 0x000000ff, true),
89 HOWTO (5, 0, 1, 16, true, 0, complain_overflow_signed, 0, "DISP16", true,
90 0x0000ffff, 0x0000ffff, true),
91 HOWTO (6, 0, 2, 32, true, 0, complain_overflow_signed, 0, "DISP32", true,
92 0xffffffff, 0xffffffff, true),
93 HOWTO (7, 2, 2, 26, false, 0, complain_overflow_signed,
94 MY(fix_pcrel_26_done), "ARM26D", true, 0x0, 0x0,
95 false),
96 EMPTY_HOWTO (-1),
97 HOWTO (9, 0, -1, 16, false, 0, complain_overflow_bitfield, 0, "NEG16", true,
98 0x0000ffff, 0x0000ffff, false),
99 HOWTO (10, 0, -2, 32, false, 0, complain_overflow_bitfield, 0, "NEG32", true,
100 0xffffffff, 0xffffffff, false)
103 #define RELOC_ARM_BITS_NEG_BIG ((unsigned int) 0x08)
104 #define RELOC_ARM_BITS_NEG_LITTLE ((unsigned int) 0x10)
106 reloc_howto_type *
107 MY(reloc_howto) (abfd, rel, r_index, r_extern, r_pcrel)
108 bfd *abfd;
109 struct reloc_std_external *rel;
110 int *r_index;
111 int *r_extern;
112 int *r_pcrel;
114 unsigned int r_length;
115 unsigned int r_pcrel_done;
116 unsigned int r_neg;
117 int index;
119 *r_pcrel = 0;
120 if (bfd_header_big_endian (abfd))
122 *r_index = ((rel->r_index[0] << 16)
123 | (rel->r_index[1] << 8)
124 | rel->r_index[2]);
125 *r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_BIG));
126 r_pcrel_done = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
127 r_neg = (0 != (rel->r_type[0] & RELOC_ARM_BITS_NEG_BIG));
128 r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_BIG)
129 >> RELOC_STD_BITS_LENGTH_SH_BIG);
131 else
133 *r_index = ((rel->r_index[2] << 16)
134 | (rel->r_index[1] << 8)
135 | rel->r_index[0]);
136 *r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE));
137 r_pcrel_done = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
138 r_neg = (0 != (rel->r_type[0] & RELOC_ARM_BITS_NEG_LITTLE));
139 r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE)
140 >> RELOC_STD_BITS_LENGTH_SH_LITTLE);
142 index = r_length + 4 * r_pcrel_done + 8 * r_neg;
143 if (index == 3)
144 *r_pcrel = 1;
146 return MY(howto_table) + index;
149 #define MY_reloc_howto(BFD, REL, IN, EX, PC) \
150 MY(reloc_howto) (BFD, REL, &IN, &EX, &PC)
152 void
153 MY(put_reloc) (abfd, r_extern, r_index, value, howto, reloc)
154 bfd *abfd;
155 int r_extern;
156 int r_index;
157 bfd_vma value;
158 reloc_howto_type *howto;
159 struct reloc_std_external *reloc;
161 unsigned int r_length;
162 int r_pcrel;
163 int r_neg;
165 PUT_WORD (abfd, value, reloc->r_address);
166 /* Size as a power of two. */
167 r_length = howto->size;
169 /* Special case for branch relocations. */
170 if (howto->type == 3 || howto->type == 7)
171 r_length = 3;
173 r_pcrel = howto->type & 4; /* PC Relative done? */
174 r_neg = howto->type & 8; /* Negative relocation. */
176 if (bfd_header_big_endian (abfd))
178 reloc->r_index[0] = r_index >> 16;
179 reloc->r_index[1] = r_index >> 8;
180 reloc->r_index[2] = r_index;
181 reloc->r_type[0] =
182 ((r_extern ? RELOC_STD_BITS_EXTERN_BIG : 0)
183 | (r_pcrel ? RELOC_STD_BITS_PCREL_BIG : 0)
184 | (r_neg ? RELOC_ARM_BITS_NEG_BIG : 0)
185 | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG));
187 else
189 reloc->r_index[2] = r_index >> 16;
190 reloc->r_index[1] = r_index >> 8;
191 reloc->r_index[0] = r_index;
192 reloc->r_type[0] =
193 ((r_extern ? RELOC_STD_BITS_EXTERN_LITTLE : 0)
194 | (r_pcrel ? RELOC_STD_BITS_PCREL_LITTLE : 0)
195 | (r_neg ? RELOC_ARM_BITS_NEG_LITTLE : 0)
196 | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE));
200 #define MY_put_reloc(BFD, EXT, IDX, VAL, HOWTO, RELOC) \
201 MY(put_reloc) (BFD, EXT, IDX, VAL, HOWTO, RELOC)
203 void
204 MY(relocatable_reloc) (howto, abfd, reloc, amount, r_addr)
205 reloc_howto_type *howto;
206 bfd *abfd;
207 struct reloc_std_external *reloc;
208 bfd_vma *amount;
209 bfd_vma r_addr;
211 if (howto->type == 3)
213 if (reloc->r_type[0]
214 & (bfd_header_big_endian (abfd)
215 ? RELOC_STD_BITS_EXTERN_BIG : RELOC_STD_BITS_EXTERN_LITTLE))
217 /* The reloc is still external, so don't modify anything. */
218 *amount = 0;
220 else
222 *amount -= r_addr;
223 /* Change the r_pcrel value -- on the ARM, this bit is set once the
224 relocation is done. */
225 if (bfd_header_big_endian (abfd))
226 reloc->r_type[0] |= RELOC_STD_BITS_PCREL_BIG;
227 else
228 reloc->r_type[0] |= RELOC_STD_BITS_PCREL_LITTLE;
231 else if (howto->type == 7)
232 *amount = 0;
235 #define MY_relocatable_reloc(HOW, BFD, REL, AMOUNT, ADDR) \
236 MY(relocatable_reloc) (HOW, BFD, REL, &(AMOUNT), ADDR)
238 static bfd_reloc_status_type
239 MY(fix_pcrel_26_done) (abfd, reloc_entry, symbol, data, input_section,
240 output_bfd, error_message)
241 bfd *abfd ATTRIBUTE_UNUSED;
242 arelent *reloc_entry ATTRIBUTE_UNUSED;
243 asymbol *symbol ATTRIBUTE_UNUSED;
244 PTR data ATTRIBUTE_UNUSED;
245 asection *input_section ATTRIBUTE_UNUSED;
246 bfd *output_bfd ATTRIBUTE_UNUSED;
247 char **error_message ATTRIBUTE_UNUSED;
249 /* This is dead simple at present. */
250 return bfd_reloc_ok;
253 static bfd_reloc_status_type
254 MY(fix_pcrel_26) (abfd, reloc_entry, symbol, data, input_section,
255 output_bfd, error_message)
256 bfd *abfd;
257 arelent *reloc_entry;
258 asymbol *symbol;
259 PTR data;
260 asection *input_section;
261 bfd *output_bfd;
262 char **error_message ATTRIBUTE_UNUSED;
264 bfd_vma relocation;
265 bfd_size_type addr = reloc_entry->address;
266 bfd_vma target = bfd_get_32 (abfd, (bfd_byte *) data + addr);
267 bfd_reloc_status_type flag = bfd_reloc_ok;
269 /* If this is an undefined symbol, return error. */
270 if (symbol->section == &bfd_und_section
271 && (symbol->flags & BSF_WEAK) == 0)
272 return output_bfd ? bfd_reloc_ok : bfd_reloc_undefined;
274 /* If the sections are different, and we are doing a partial relocation,
275 just ignore it for now. */
276 if (symbol->section->name != input_section->name
277 && output_bfd != (bfd *)NULL)
278 return bfd_reloc_ok;
280 relocation = (target & 0x00ffffff) << 2;
281 relocation = (relocation ^ 0x02000000) - 0x02000000; /* Sign extend. */
282 relocation += symbol->value;
283 relocation += symbol->section->output_section->vma;
284 relocation += symbol->section->output_offset;
285 relocation += reloc_entry->addend;
286 relocation -= input_section->output_section->vma;
287 relocation -= input_section->output_offset;
288 relocation -= addr;
289 if (relocation & 3)
290 return bfd_reloc_overflow;
292 /* Check for overflow. */
293 if (relocation & 0x02000000)
295 if ((relocation & ~ (bfd_vma) 0x03ffffff) != ~ (bfd_vma) 0x03ffffff)
296 flag = bfd_reloc_overflow;
298 else if (relocation & ~ (bfd_vma) 0x03ffffff)
299 flag = bfd_reloc_overflow;
301 target &= ~ (bfd_vma) 0x00ffffff;
302 target |= (relocation >> 2) & 0x00ffffff;
303 bfd_put_32 (abfd, target, (bfd_byte *) data + addr);
305 /* Now the ARM magic... Change the reloc type so that it is marked as done.
306 Strictly this is only necessary if we are doing a partial relocation. */
307 reloc_entry->howto = &MY(howto_table)[7];
309 return flag;
312 reloc_howto_type *
313 MY(bfd_reloc_type_lookup) (abfd,code)
314 bfd *abfd;
315 bfd_reloc_code_real_type code;
317 #define ASTD(i,j) case i: return &MY(howto_table)[j]
318 if (code == BFD_RELOC_CTOR)
319 switch (bfd_get_arch_info (abfd)->bits_per_address)
321 case 32:
322 code = BFD_RELOC_32;
323 break;
324 default:
325 return (const struct reloc_howto_struct *) 0;
328 switch (code)
330 ASTD (BFD_RELOC_16, 1);
331 ASTD (BFD_RELOC_32, 2);
332 ASTD (BFD_RELOC_ARM_PCREL_BRANCH, 3);
333 ASTD (BFD_RELOC_8_PCREL, 4);
334 ASTD (BFD_RELOC_16_PCREL, 5);
335 ASTD (BFD_RELOC_32_PCREL, 6);
336 default:
337 return (const struct reloc_howto_struct *) 0;
341 #define MY_swap_std_reloc_in MY(swap_std_reloc_in)
342 #define MY_swap_std_reloc_out MY(swap_std_reloc_out)
343 #define MY_get_section_contents _bfd_generic_get_section_contents
344 /* #define MY_bfd_link_hash_table_create _bfd_generic_link_hash_table_create */
345 /* #define MY_bfd_link_add_symbols _bfd_generic_link_add_symbols */
346 /* #define MY_bfd_final_link _bfd_generic_final_link */
348 #include "aoutx.h"
350 static void
351 MY_swap_std_reloc_in (abfd, bytes, cache_ptr, symbols, symcount)
352 bfd *abfd;
353 struct reloc_std_external *bytes;
354 arelent *cache_ptr;
355 asymbol **symbols;
356 bfd_size_type symcount ATTRIBUTE_UNUSED;
358 int r_index;
359 int r_extern;
360 int r_pcrel;
361 struct aoutdata *su = &(abfd->tdata.aout_data->a);
363 cache_ptr->address = H_GET_32 (abfd, bytes->r_address);
365 cache_ptr->howto = MY_reloc_howto (abfd, bytes, r_index, r_extern, r_pcrel);
367 MOVE_ADDRESS (0);
370 void
371 MY_swap_std_reloc_out (abfd, g, natptr)
372 bfd *abfd;
373 arelent *g;
374 struct reloc_std_external *natptr;
376 int r_index;
377 asymbol *sym = *(g->sym_ptr_ptr);
378 int r_extern;
379 int r_length;
380 int r_pcrel;
381 int r_neg = 0; /* Negative relocs use the BASEREL bit. */
382 asection *output_section = sym->section->output_section;
384 PUT_WORD(abfd, g->address, natptr->r_address);
386 r_length = g->howto->size ; /* Size as a power of two */
387 if (r_length < 0)
389 r_length = -r_length;
390 r_neg = 1;
393 r_pcrel = (int) g->howto->pc_relative; /* Relative to PC? */
395 /* For RISC iX, in pc-relative relocs the r_pcrel bit means that the
396 relocation has been done already (Only for the 26-bit one I think)???!!!
399 if (g->howto->type == 3)
401 r_length = 3;
402 r_pcrel = 0;
404 else if (g->howto->type == 7)
406 r_length = 3;
407 r_pcrel = 1;
410 #if 0
411 /* For a standard reloc, the addend is in the object file. */
412 r_addend = g->addend + (*(g->sym_ptr_ptr))->section->output_section->vma;
413 #endif
415 /* name was clobbered by aout_write_syms to be symbol index */
417 /* If this relocation is relative to a symbol then set the
418 r_index to the symbols index, and the r_extern bit.
420 Absolute symbols can come in in two ways, either as an offset
421 from the abs section, or as a symbol which has an abs value.
422 check for that here
425 if (bfd_is_com_section (output_section)
426 || output_section == &bfd_abs_section
427 || output_section == &bfd_und_section)
429 if (bfd_abs_section.symbol == sym)
431 /* Whoops, looked like an abs symbol, but is really an offset
432 from the abs section. */
433 r_index = 0;
434 r_extern = 0;
436 else
438 /* Fill in symbol. */
439 r_extern = 1;
440 r_index = (*(g->sym_ptr_ptr))->KEEPIT;
443 else
445 /* Just an ordinary section. */
446 r_extern = 0;
447 r_index = output_section->target_index;
450 /* Now the fun stuff. */
451 if (bfd_header_big_endian (abfd))
453 natptr->r_index[0] = r_index >> 16;
454 natptr->r_index[1] = r_index >> 8;
455 natptr->r_index[2] = r_index;
456 natptr->r_type[0] =
457 ( (r_extern ? RELOC_STD_BITS_EXTERN_BIG: 0)
458 | (r_pcrel ? RELOC_STD_BITS_PCREL_BIG: 0)
459 | (r_neg ? RELOC_ARM_BITS_NEG_BIG: 0)
460 | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG));
462 else
464 natptr->r_index[2] = r_index >> 16;
465 natptr->r_index[1] = r_index >> 8;
466 natptr->r_index[0] = r_index;
467 natptr->r_type[0] =
468 ( (r_extern ? RELOC_STD_BITS_EXTERN_LITTLE: 0)
469 | (r_pcrel ? RELOC_STD_BITS_PCREL_LITTLE: 0)
470 | (r_neg ? RELOC_ARM_BITS_NEG_LITTLE: 0)
471 | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE));
475 #define MY_BFD_TARGET
477 #include "aout-target.h"
479 extern const bfd_target aout_arm_big_vec;
481 const bfd_target aout_arm_little_vec =
483 "a.out-arm-little", /* name */
484 bfd_target_aout_flavour,
485 BFD_ENDIAN_LITTLE, /* target byte order (little) */
486 BFD_ENDIAN_LITTLE, /* target headers byte order (little) */
487 (HAS_RELOC | EXEC_P | /* object flags */
488 HAS_LINENO | HAS_DEBUG |
489 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
490 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
491 MY_symbol_leading_char,
492 AR_PAD_CHAR, /* ar_pad_char */
493 15, /* ar_max_namelen */
494 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
495 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
496 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
497 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
498 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
499 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
500 {_bfd_dummy_target, MY_object_p, /* bfd_check_format */
501 bfd_generic_archive_p, MY_core_file_p},
502 {bfd_false, MY_mkobject, /* bfd_set_format */
503 _bfd_generic_mkarchive, bfd_false},
504 {bfd_false, MY_write_object_contents, /* bfd_write_contents */
505 _bfd_write_archive_contents, bfd_false},
507 BFD_JUMP_TABLE_GENERIC (MY),
508 BFD_JUMP_TABLE_COPY (MY),
509 BFD_JUMP_TABLE_CORE (MY),
510 BFD_JUMP_TABLE_ARCHIVE (MY),
511 BFD_JUMP_TABLE_SYMBOLS (MY),
512 BFD_JUMP_TABLE_RELOCS (MY),
513 BFD_JUMP_TABLE_WRITE (MY),
514 BFD_JUMP_TABLE_LINK (MY),
515 BFD_JUMP_TABLE_DYNAMIC (MY),
517 & aout_arm_big_vec,
519 (PTR) MY_backend_data,
522 const bfd_target aout_arm_big_vec =
524 "a.out-arm-big", /* name */
525 bfd_target_aout_flavour,
526 BFD_ENDIAN_BIG, /* target byte order (big) */
527 BFD_ENDIAN_BIG, /* target headers byte order (big) */
528 (HAS_RELOC | EXEC_P | /* object flags */
529 HAS_LINENO | HAS_DEBUG |
530 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
531 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
532 MY_symbol_leading_char,
533 AR_PAD_CHAR, /* ar_pad_char */
534 15, /* ar_max_namelen */
535 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
536 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
537 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
538 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
539 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
540 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
541 {_bfd_dummy_target, MY_object_p, /* bfd_check_format */
542 bfd_generic_archive_p, MY_core_file_p},
543 {bfd_false, MY_mkobject, /* bfd_set_format */
544 _bfd_generic_mkarchive, bfd_false},
545 {bfd_false, MY_write_object_contents, /* bfd_write_contents */
546 _bfd_write_archive_contents, bfd_false},
548 BFD_JUMP_TABLE_GENERIC (MY),
549 BFD_JUMP_TABLE_COPY (MY),
550 BFD_JUMP_TABLE_CORE (MY),
551 BFD_JUMP_TABLE_ARCHIVE (MY),
552 BFD_JUMP_TABLE_SYMBOLS (MY),
553 BFD_JUMP_TABLE_RELOCS (MY),
554 BFD_JUMP_TABLE_WRITE (MY),
555 BFD_JUMP_TABLE_LINK (MY),
556 BFD_JUMP_TABLE_DYNAMIC (MY),
558 & aout_arm_little_vec,
560 (PTR) MY_backend_data,