Oops - fix typos and finish of ISO-C90'fication of ieee.c
[binutils.git] / bfd / aout-arm.c
blob0cdadf1d09a673fdb2b720bf2509f7d124881b5d
1 /* BFD back-end for raw ARM a.out binaries.
2 Copyright 1994, 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2005
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"
25 /* Avoid multiple definitions from aoutx if supporting standard a.out
26 as well as our own. */
27 /* Do not "beautify" the CONCAT* macro args. Traditional C will not
28 remove whitespace added here, and thus will fail to concatenate
29 the tokens. */
30 #define NAME(x,y) CONCAT3 (aoutarm,_32_,y)
32 #define N_TXTADDR(x) \
33 ((N_MAGIC (x) == NMAGIC) \
34 ? (bfd_vma) 0x8000 \
35 : ((N_MAGIC (x) != ZMAGIC) \
36 ? (bfd_vma) 0 \
37 : ((N_SHARED_LIB (x)) \
38 ? ((x).a_entry & ~(bfd_vma) (TARGET_PAGE_SIZE - 1)) \
39 : (bfd_vma) TEXT_START_ADDR)))
41 #define TEXT_START_ADDR 0x8000
42 #define TARGET_PAGE_SIZE 0x8000
43 #define SEGMENT_SIZE TARGET_PAGE_SIZE
44 #define DEFAULT_ARCH bfd_arch_arm
46 #define MY(OP) CONCAT2 (aoutarm_,OP)
47 #define N_BADMAG(x) ((((x).a_info & ~007200) != ZMAGIC) && \
48 (((x).a_info & ~006000) != OMAGIC) && \
49 ((x).a_info != NMAGIC))
50 #define N_MAGIC(x) ((x).a_info & ~07200)
52 #define MY_bfd_reloc_type_lookup aoutarm_bfd_reloc_type_lookup
54 #include "libaout.h"
55 #include "aout/aout64.h"
57 static bfd_boolean MY(write_object_contents)
58 PARAMS ((bfd *));
59 static bfd_reloc_status_type MY(fix_pcrel_26_done)
60 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
61 static bfd_reloc_status_type MY(fix_pcrel_26)
62 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
63 static void MY(swap_std_reloc_in)
64 PARAMS ((bfd *, struct reloc_std_external *, arelent *, asymbol **,
65 bfd_size_type));
66 reloc_howto_type *MY(bfd_reloc_type_lookup)
67 PARAMS ((bfd *, bfd_reloc_code_real_type));
68 reloc_howto_type * MY(reloc_howto)
69 PARAMS ((bfd *, struct reloc_std_external *, int *, int *, int *));
70 void MY(put_reloc)
71 PARAMS ((bfd *, int, int, bfd_vma, reloc_howto_type *,
72 struct reloc_std_external *));
73 void MY(relocatable_reloc)
74 PARAMS ((reloc_howto_type *, bfd *, struct reloc_std_external *, bfd_vma *,
75 bfd_vma));
76 void MY(swap_std_reloc_out)
77 PARAMS ((bfd *, arelent *, struct reloc_std_external *));
79 reloc_howto_type MY(howto_table)[] =
81 /* Type rs size bsz pcrel bitpos ovrf sf name part_inpl
82 readmask setmask pcdone. */
83 HOWTO (0, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, "8", TRUE,
84 0x000000ff, 0x000000ff, FALSE),
85 HOWTO (1, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, 0, "16", TRUE,
86 0x0000ffff, 0x0000ffff, FALSE),
87 HOWTO (2, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 0, "32", TRUE,
88 0xffffffff, 0xffffffff, FALSE),
89 HOWTO (3, 2, 2, 26, TRUE, 0, complain_overflow_signed, MY(fix_pcrel_26),
90 "ARM26", TRUE, 0x00ffffff, 0x00ffffff, TRUE),
91 HOWTO (4, 0, 0, 8, TRUE, 0, complain_overflow_signed, 0, "DISP8", TRUE,
92 0x000000ff, 0x000000ff, TRUE),
93 HOWTO (5, 0, 1, 16, TRUE, 0, complain_overflow_signed, 0, "DISP16", TRUE,
94 0x0000ffff, 0x0000ffff, TRUE),
95 HOWTO (6, 0, 2, 32, TRUE, 0, complain_overflow_signed, 0, "DISP32", TRUE,
96 0xffffffff, 0xffffffff, TRUE),
97 HOWTO (7, 2, 2, 26, FALSE, 0, complain_overflow_signed,
98 MY(fix_pcrel_26_done), "ARM26D", TRUE, 0x0, 0x0,
99 FALSE),
100 EMPTY_HOWTO (-1),
101 HOWTO (9, 0, -1, 16, FALSE, 0, complain_overflow_bitfield, 0, "NEG16", TRUE,
102 0x0000ffff, 0x0000ffff, FALSE),
103 HOWTO (10, 0, -2, 32, FALSE, 0, complain_overflow_bitfield, 0, "NEG32", TRUE,
104 0xffffffff, 0xffffffff, FALSE)
107 #define RELOC_ARM_BITS_NEG_BIG ((unsigned int) 0x08)
108 #define RELOC_ARM_BITS_NEG_LITTLE ((unsigned int) 0x10)
110 reloc_howto_type *
111 MY(reloc_howto) (abfd, rel, r_index, r_extern, r_pcrel)
112 bfd *abfd;
113 struct reloc_std_external *rel;
114 int *r_index;
115 int *r_extern;
116 int *r_pcrel;
118 unsigned int r_length;
119 unsigned int r_pcrel_done;
120 unsigned int r_neg;
121 int index;
123 *r_pcrel = 0;
124 if (bfd_header_big_endian (abfd))
126 *r_index = ((rel->r_index[0] << 16)
127 | (rel->r_index[1] << 8)
128 | rel->r_index[2]);
129 *r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_BIG));
130 r_pcrel_done = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
131 r_neg = (0 != (rel->r_type[0] & RELOC_ARM_BITS_NEG_BIG));
132 r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_BIG)
133 >> RELOC_STD_BITS_LENGTH_SH_BIG);
135 else
137 *r_index = ((rel->r_index[2] << 16)
138 | (rel->r_index[1] << 8)
139 | rel->r_index[0]);
140 *r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE));
141 r_pcrel_done = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
142 r_neg = (0 != (rel->r_type[0] & RELOC_ARM_BITS_NEG_LITTLE));
143 r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE)
144 >> RELOC_STD_BITS_LENGTH_SH_LITTLE);
146 index = r_length + 4 * r_pcrel_done + 8 * r_neg;
147 if (index == 3)
148 *r_pcrel = 1;
150 return MY(howto_table) + index;
153 #define MY_reloc_howto(BFD, REL, IN, EX, PC) \
154 MY(reloc_howto) (BFD, REL, &IN, &EX, &PC)
156 void
157 MY(put_reloc) (abfd, r_extern, r_index, value, howto, reloc)
158 bfd *abfd;
159 int r_extern;
160 int r_index;
161 bfd_vma value;
162 reloc_howto_type *howto;
163 struct reloc_std_external *reloc;
165 unsigned int r_length;
166 int r_pcrel;
167 int r_neg;
169 PUT_WORD (abfd, value, reloc->r_address);
170 /* Size as a power of two. */
171 r_length = howto->size;
173 /* Special case for branch relocations. */
174 if (howto->type == 3 || howto->type == 7)
175 r_length = 3;
177 r_pcrel = howto->type & 4; /* PC Relative done? */
178 r_neg = howto->type & 8; /* Negative relocation. */
180 if (bfd_header_big_endian (abfd))
182 reloc->r_index[0] = r_index >> 16;
183 reloc->r_index[1] = r_index >> 8;
184 reloc->r_index[2] = r_index;
185 reloc->r_type[0] =
186 ((r_extern ? RELOC_STD_BITS_EXTERN_BIG : 0)
187 | (r_pcrel ? RELOC_STD_BITS_PCREL_BIG : 0)
188 | (r_neg ? RELOC_ARM_BITS_NEG_BIG : 0)
189 | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG));
191 else
193 reloc->r_index[2] = r_index >> 16;
194 reloc->r_index[1] = r_index >> 8;
195 reloc->r_index[0] = r_index;
196 reloc->r_type[0] =
197 ((r_extern ? RELOC_STD_BITS_EXTERN_LITTLE : 0)
198 | (r_pcrel ? RELOC_STD_BITS_PCREL_LITTLE : 0)
199 | (r_neg ? RELOC_ARM_BITS_NEG_LITTLE : 0)
200 | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE));
204 #define MY_put_reloc(BFD, EXT, IDX, VAL, HOWTO, RELOC) \
205 MY(put_reloc) (BFD, EXT, IDX, VAL, HOWTO, RELOC)
207 void
208 MY(relocatable_reloc) (howto, abfd, reloc, amount, r_addr)
209 reloc_howto_type *howto;
210 bfd *abfd;
211 struct reloc_std_external *reloc;
212 bfd_vma *amount;
213 bfd_vma r_addr;
215 if (howto->type == 3)
217 if (reloc->r_type[0]
218 & (bfd_header_big_endian (abfd)
219 ? RELOC_STD_BITS_EXTERN_BIG : RELOC_STD_BITS_EXTERN_LITTLE))
221 /* The reloc is still external, so don't modify anything. */
222 *amount = 0;
224 else
226 *amount -= r_addr;
227 /* Change the r_pcrel value -- on the ARM, this bit is set once the
228 relocation is done. */
229 if (bfd_header_big_endian (abfd))
230 reloc->r_type[0] |= RELOC_STD_BITS_PCREL_BIG;
231 else
232 reloc->r_type[0] |= RELOC_STD_BITS_PCREL_LITTLE;
235 else if (howto->type == 7)
236 *amount = 0;
239 #define MY_relocatable_reloc(HOW, BFD, REL, AMOUNT, ADDR) \
240 MY(relocatable_reloc) (HOW, BFD, REL, &(AMOUNT), ADDR)
242 static bfd_reloc_status_type
243 MY(fix_pcrel_26_done) (abfd, reloc_entry, symbol, data, input_section,
244 output_bfd, error_message)
245 bfd *abfd ATTRIBUTE_UNUSED;
246 arelent *reloc_entry ATTRIBUTE_UNUSED;
247 asymbol *symbol ATTRIBUTE_UNUSED;
248 PTR data ATTRIBUTE_UNUSED;
249 asection *input_section ATTRIBUTE_UNUSED;
250 bfd *output_bfd ATTRIBUTE_UNUSED;
251 char **error_message ATTRIBUTE_UNUSED;
253 /* This is dead simple at present. */
254 return bfd_reloc_ok;
257 static bfd_reloc_status_type
258 MY(fix_pcrel_26) (abfd, reloc_entry, symbol, data, input_section,
259 output_bfd, error_message)
260 bfd *abfd;
261 arelent *reloc_entry;
262 asymbol *symbol;
263 PTR data;
264 asection *input_section;
265 bfd *output_bfd;
266 char **error_message ATTRIBUTE_UNUSED;
268 bfd_vma relocation;
269 bfd_size_type addr = reloc_entry->address;
270 bfd_vma target = bfd_get_32 (abfd, (bfd_byte *) data + addr);
271 bfd_reloc_status_type flag = bfd_reloc_ok;
273 /* If this is an undefined symbol, return error. */
274 if (symbol->section == &bfd_und_section
275 && (symbol->flags & BSF_WEAK) == 0)
276 return output_bfd ? bfd_reloc_ok : bfd_reloc_undefined;
278 /* If the sections are different, and we are doing a partial relocation,
279 just ignore it for now. */
280 if (symbol->section->name != input_section->name
281 && output_bfd != (bfd *)NULL)
282 return bfd_reloc_ok;
284 relocation = (target & 0x00ffffff) << 2;
285 relocation = (relocation ^ 0x02000000) - 0x02000000; /* Sign extend. */
286 relocation += symbol->value;
287 relocation += symbol->section->output_section->vma;
288 relocation += symbol->section->output_offset;
289 relocation += reloc_entry->addend;
290 relocation -= input_section->output_section->vma;
291 relocation -= input_section->output_offset;
292 relocation -= addr;
293 if (relocation & 3)
294 return bfd_reloc_overflow;
296 /* Check for overflow. */
297 if (relocation & 0x02000000)
299 if ((relocation & ~ (bfd_vma) 0x03ffffff) != ~ (bfd_vma) 0x03ffffff)
300 flag = bfd_reloc_overflow;
302 else if (relocation & ~ (bfd_vma) 0x03ffffff)
303 flag = bfd_reloc_overflow;
305 target &= ~ (bfd_vma) 0x00ffffff;
306 target |= (relocation >> 2) & 0x00ffffff;
307 bfd_put_32 (abfd, target, (bfd_byte *) data + addr);
309 /* Now the ARM magic... Change the reloc type so that it is marked as done.
310 Strictly this is only necessary if we are doing a partial relocation. */
311 reloc_entry->howto = &MY(howto_table)[7];
313 return flag;
316 reloc_howto_type *
317 MY(bfd_reloc_type_lookup) (abfd,code)
318 bfd *abfd;
319 bfd_reloc_code_real_type code;
321 #define ASTD(i,j) case i: return &MY(howto_table)[j]
322 if (code == BFD_RELOC_CTOR)
323 switch (bfd_get_arch_info (abfd)->bits_per_address)
325 case 32:
326 code = BFD_RELOC_32;
327 break;
328 default:
329 return (const struct reloc_howto_struct *) 0;
332 switch (code)
334 ASTD (BFD_RELOC_16, 1);
335 ASTD (BFD_RELOC_32, 2);
336 ASTD (BFD_RELOC_ARM_PCREL_BRANCH, 3);
337 ASTD (BFD_RELOC_8_PCREL, 4);
338 ASTD (BFD_RELOC_16_PCREL, 5);
339 ASTD (BFD_RELOC_32_PCREL, 6);
340 default:
341 return (const struct reloc_howto_struct *) 0;
345 #define MY_swap_std_reloc_in MY(swap_std_reloc_in)
346 #define MY_swap_std_reloc_out MY(swap_std_reloc_out)
347 #define MY_get_section_contents _bfd_generic_get_section_contents
348 /* #define MY_bfd_link_hash_table_create _bfd_generic_link_hash_table_create */
349 /* #define MY_bfd_link_add_symbols _bfd_generic_link_add_symbols */
350 /* #define MY_bfd_final_link _bfd_generic_final_link */
352 #include "aoutx.h"
354 static void
355 MY_swap_std_reloc_in (abfd, bytes, cache_ptr, symbols, symcount)
356 bfd *abfd;
357 struct reloc_std_external *bytes;
358 arelent *cache_ptr;
359 asymbol **symbols;
360 bfd_size_type symcount ATTRIBUTE_UNUSED;
362 int r_index;
363 int r_extern;
364 int r_pcrel;
365 struct aoutdata *su = &(abfd->tdata.aout_data->a);
367 cache_ptr->address = H_GET_32 (abfd, bytes->r_address);
369 cache_ptr->howto = MY_reloc_howto (abfd, bytes, r_index, r_extern, r_pcrel);
371 MOVE_ADDRESS (0);
374 void
375 MY_swap_std_reloc_out (abfd, g, natptr)
376 bfd *abfd;
377 arelent *g;
378 struct reloc_std_external *natptr;
380 int r_index;
381 asymbol *sym = *(g->sym_ptr_ptr);
382 int r_extern;
383 int r_length;
384 int r_pcrel;
385 int r_neg = 0; /* Negative relocs use the BASEREL bit. */
386 asection *output_section = sym->section->output_section;
388 PUT_WORD(abfd, g->address, natptr->r_address);
390 r_length = g->howto->size ; /* Size as a power of two */
391 if (r_length < 0)
393 r_length = -r_length;
394 r_neg = 1;
397 r_pcrel = (int) g->howto->pc_relative; /* Relative to PC? */
399 /* For RISC iX, in pc-relative relocs the r_pcrel bit means that the
400 relocation has been done already (Only for the 26-bit one I think)???!!!
403 if (g->howto->type == 3)
405 r_length = 3;
406 r_pcrel = 0;
408 else if (g->howto->type == 7)
410 r_length = 3;
411 r_pcrel = 1;
414 /* name was clobbered by aout_write_syms to be symbol index */
416 /* If this relocation is relative to a symbol then set the
417 r_index to the symbols index, and the r_extern bit.
419 Absolute symbols can come in in two ways, either as an offset
420 from the abs section, or as a symbol which has an abs value.
421 check for that here
424 if (bfd_is_com_section (output_section)
425 || output_section == &bfd_abs_section
426 || output_section == &bfd_und_section)
428 if (bfd_abs_section.symbol == sym)
430 /* Whoops, looked like an abs symbol, but is really an offset
431 from the abs section. */
432 r_index = 0;
433 r_extern = 0;
435 else
437 /* Fill in symbol. */
438 r_extern = 1;
439 r_index = (*(g->sym_ptr_ptr))->KEEPIT;
442 else
444 /* Just an ordinary section. */
445 r_extern = 0;
446 r_index = output_section->target_index;
449 /* Now the fun stuff. */
450 if (bfd_header_big_endian (abfd))
452 natptr->r_index[0] = r_index >> 16;
453 natptr->r_index[1] = r_index >> 8;
454 natptr->r_index[2] = r_index;
455 natptr->r_type[0] =
456 ( (r_extern ? RELOC_STD_BITS_EXTERN_BIG: 0)
457 | (r_pcrel ? RELOC_STD_BITS_PCREL_BIG: 0)
458 | (r_neg ? RELOC_ARM_BITS_NEG_BIG: 0)
459 | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG));
461 else
463 natptr->r_index[2] = r_index >> 16;
464 natptr->r_index[1] = r_index >> 8;
465 natptr->r_index[0] = r_index;
466 natptr->r_type[0] =
467 ( (r_extern ? RELOC_STD_BITS_EXTERN_LITTLE: 0)
468 | (r_pcrel ? RELOC_STD_BITS_PCREL_LITTLE: 0)
469 | (r_neg ? RELOC_ARM_BITS_NEG_LITTLE: 0)
470 | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE));
474 #define MY_BFD_TARGET
476 #include "aout-target.h"
478 extern const bfd_target aout_arm_big_vec;
480 const bfd_target aout_arm_little_vec =
482 "a.out-arm-little", /* name */
483 bfd_target_aout_flavour,
484 BFD_ENDIAN_LITTLE, /* target byte order (little) */
485 BFD_ENDIAN_LITTLE, /* target headers byte order (little) */
486 (HAS_RELOC | EXEC_P | /* object flags */
487 HAS_LINENO | HAS_DEBUG |
488 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
489 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
490 MY_symbol_leading_char,
491 AR_PAD_CHAR, /* ar_pad_char */
492 15, /* ar_max_namelen */
493 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
494 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
495 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
496 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
497 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
498 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
499 {_bfd_dummy_target, MY_object_p, /* bfd_check_format */
500 bfd_generic_archive_p, MY_core_file_p},
501 {bfd_false, MY_mkobject, /* bfd_set_format */
502 _bfd_generic_mkarchive, bfd_false},
503 {bfd_false, MY_write_object_contents, /* bfd_write_contents */
504 _bfd_write_archive_contents, bfd_false},
506 BFD_JUMP_TABLE_GENERIC (MY),
507 BFD_JUMP_TABLE_COPY (MY),
508 BFD_JUMP_TABLE_CORE (MY),
509 BFD_JUMP_TABLE_ARCHIVE (MY),
510 BFD_JUMP_TABLE_SYMBOLS (MY),
511 BFD_JUMP_TABLE_RELOCS (MY),
512 BFD_JUMP_TABLE_WRITE (MY),
513 BFD_JUMP_TABLE_LINK (MY),
514 BFD_JUMP_TABLE_DYNAMIC (MY),
516 & aout_arm_big_vec,
518 (PTR) MY_backend_data,
521 const bfd_target aout_arm_big_vec =
523 "a.out-arm-big", /* name */
524 bfd_target_aout_flavour,
525 BFD_ENDIAN_BIG, /* target byte order (big) */
526 BFD_ENDIAN_BIG, /* target headers byte order (big) */
527 (HAS_RELOC | EXEC_P | /* object flags */
528 HAS_LINENO | HAS_DEBUG |
529 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
530 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
531 MY_symbol_leading_char,
532 AR_PAD_CHAR, /* ar_pad_char */
533 15, /* ar_max_namelen */
534 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
535 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
536 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
537 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
538 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
539 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
540 {_bfd_dummy_target, MY_object_p, /* bfd_check_format */
541 bfd_generic_archive_p, MY_core_file_p},
542 {bfd_false, MY_mkobject, /* bfd_set_format */
543 _bfd_generic_mkarchive, bfd_false},
544 {bfd_false, MY_write_object_contents, /* bfd_write_contents */
545 _bfd_write_archive_contents, bfd_false},
547 BFD_JUMP_TABLE_GENERIC (MY),
548 BFD_JUMP_TABLE_COPY (MY),
549 BFD_JUMP_TABLE_CORE (MY),
550 BFD_JUMP_TABLE_ARCHIVE (MY),
551 BFD_JUMP_TABLE_SYMBOLS (MY),
552 BFD_JUMP_TABLE_RELOCS (MY),
553 BFD_JUMP_TABLE_WRITE (MY),
554 BFD_JUMP_TABLE_LINK (MY),
555 BFD_JUMP_TABLE_DYNAMIC (MY),
557 & aout_arm_little_vec,
559 (PTR) MY_backend_data,