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