Add missing prototypes
[external/binutils.git] / bfd / mipsbsd.c
1 /* BFD backend for MIPS BSD (a.out) binaries.
2    Copyright 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2001
3    Free Software Foundation, Inc.
4    Written by Ralph Campbell.
5
6 This file is part of BFD, the Binary File Descriptor library.
7
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.
12
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.
17
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.  */
21
22 #define BYTES_IN_WORD 4
23 /* #define ENTRY_CAN_BE_ZERO */
24 #define N_HEADER_IN_TEXT(x) 1
25 #define N_SHARED_LIB(x) 0
26 #define N_TXTADDR(x) \
27     (N_MAGIC(x) != ZMAGIC ? (x).a_entry :       /* object file or NMAGIC */\
28             TEXT_START_ADDR + EXEC_BYTES_SIZE   /* no padding */\
29     )
30 #define N_DATADDR(x) (N_TXTADDR(x)+N_TXTSIZE(x))
31 #define TEXT_START_ADDR 4096
32 #define TARGET_PAGE_SIZE 4096
33 #define SEGMENT_SIZE TARGET_PAGE_SIZE
34 #define DEFAULT_ARCH bfd_arch_mips
35 #define MACHTYPE_OK(mtype) ((mtype) == M_UNKNOWN \
36                             || (mtype) == M_MIPS1 || (mtype) == M_MIPS2)
37 #define MY_symbol_leading_char '\0'
38
39 #define MY(OP) CAT(mipsbsd_,OP)
40
41 #include "bfd.h"
42 #include "sysdep.h"
43 #include "libbfd.h"
44 #include "libaout.h"
45
46 #define SET_ARCH_MACH(ABFD, EXEC) \
47   MY(set_arch_mach) (ABFD, N_MACHTYPE (EXEC)); \
48   MY(choose_reloc_size) (ABFD);
49 static void MY(set_arch_mach) PARAMS ((bfd *abfd, int machtype));
50 static void MY(choose_reloc_size) PARAMS ((bfd *abfd));
51
52 #define MY_write_object_contents MY(write_object_contents)
53 static boolean MY(write_object_contents) PARAMS ((bfd *abfd));
54
55 /* We can't use MY(x) here because it leads to a recursive call to CAT
56    when expanded inside JUMP_TABLE.  */
57 #define MY_bfd_reloc_type_lookup mipsbsd_reloc_howto_type_lookup
58 #define MY_canonicalize_reloc mipsbsd_canonicalize_reloc
59
60 #define MY_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
61 #define MY_bfd_link_add_symbols _bfd_generic_link_add_symbols
62 #define MY_final_link_callback unused
63 #define MY_bfd_final_link _bfd_generic_final_link
64
65 #define MY_backend_data &MY(backend_data)
66 #define MY_BFD_TARGET
67
68 #include "aout-target.h"
69
70 static bfd_reloc_status_type mips_fix_jmp_addr PARAMS ((bfd *, arelent *, struct symbol_cache_entry *, PTR, asection *, bfd *));
71 static reloc_howto_type *    MY(reloc_howto_type_lookup) PARAMS ((bfd *, bfd_reloc_code_real_type));
72
73 long MY(canonicalize_reloc) PARAMS ((bfd *, sec_ptr, arelent **, asymbol **));
74
75 static void
76 MY(set_arch_mach) (abfd, machtype)
77      bfd *abfd;
78      int machtype;
79 {
80   enum bfd_architecture arch;
81   long machine;
82
83   /* Determine the architecture and machine type of the object file.  */
84   switch (machtype)
85     {
86     case M_MIPS1:
87       arch = bfd_arch_mips;
88       machine = 3000;
89       break;
90
91     case M_MIPS2:
92       arch = bfd_arch_mips;
93       machine = 4000;
94       break;
95
96     default:
97       arch = bfd_arch_obscure;
98       machine = 0;
99       break;
100     }
101
102   bfd_set_arch_mach (abfd, arch, machine);
103 }
104
105 /* Determine the size of a relocation entry, based on the architecture */
106 static void
107 MY (choose_reloc_size) (abfd)
108      bfd *abfd;
109 {
110   switch (bfd_get_arch (abfd))
111     {
112     case bfd_arch_sparc:
113     case bfd_arch_a29k:
114     case bfd_arch_mips:
115       obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
116       break;
117     default:
118       obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
119       break;
120     }
121 }
122
123 /* Write an object file in BSD a.out format.
124   Section contents have already been written.  We write the
125   file header, symbols, and relocation.  */
126
127 static boolean
128 MY (write_object_contents) (abfd)
129      bfd *abfd;
130 {
131   struct external_exec exec_bytes;
132   struct internal_exec *execp = exec_hdr (abfd);
133
134   /* Magic number, maestro, please!  */
135   switch (bfd_get_arch (abfd))
136     {
137     case bfd_arch_m68k:
138       switch (bfd_get_mach (abfd))
139         {
140         case bfd_mach_m68010:
141           N_SET_MACHTYPE (*execp, M_68010);
142           break;
143         default:
144         case bfd_mach_m68020:
145           N_SET_MACHTYPE (*execp, M_68020);
146           break;
147         }
148       break;
149     case bfd_arch_sparc:
150       N_SET_MACHTYPE (*execp, M_SPARC);
151       break;
152     case bfd_arch_i386:
153       N_SET_MACHTYPE (*execp, M_386);
154       break;
155     case bfd_arch_a29k:
156       N_SET_MACHTYPE (*execp, M_29K);
157       break;
158     case bfd_arch_mips:
159       switch (bfd_get_mach (abfd))
160         {
161         case 4000:
162         case 6000:
163           N_SET_MACHTYPE (*execp, M_MIPS2);
164           break;
165         default:
166           N_SET_MACHTYPE (*execp, M_MIPS1);
167           break;
168         }
169       break;
170     default:
171       N_SET_MACHTYPE (*execp, M_UNKNOWN);
172     }
173
174   MY (choose_reloc_size) (abfd);
175
176   WRITE_HEADERS (abfd, execp);
177
178   return true;
179 }
180
181 /* MIPS relocation types.  */
182 #define MIPS_RELOC_32           0
183 #define MIPS_RELOC_JMP          1
184 #define MIPS_RELOC_WDISP16      2
185 #define MIPS_RELOC_HI16         3
186 #define MIPS_RELOC_HI16_S       4
187 #define MIPS_RELOC_LO16         5
188
189 /* This is only called when performing a BFD_RELOC_MIPS_JMP relocation.
190    The jump destination address is formed from the upper 4 bits of the
191    "current" program counter concatenated with the jump instruction's
192    26 bit field and two trailing zeros.
193    If the destination address is not in the same segment as the "current"
194    program counter, then we need to signal an error.  */
195
196 static bfd_reloc_status_type
197 mips_fix_jmp_addr (abfd,reloc_entry,symbol,data,input_section,output_bfd)
198      bfd *abfd ATTRIBUTE_UNUSED;
199      arelent *reloc_entry;
200      struct symbol_cache_entry *symbol;
201      PTR data ATTRIBUTE_UNUSED;
202      asection *input_section;
203      bfd *output_bfd;
204 {
205   bfd_vma relocation, pc;
206
207   /* If this is a partial relocation, just continue.  */
208   if (output_bfd != (bfd *)NULL)
209     return bfd_reloc_continue;
210
211   /* If this is an undefined symbol, return error */
212   if (bfd_is_und_section (symbol->section)
213       && (symbol->flags & BSF_WEAK) == 0)
214     return bfd_reloc_undefined;
215
216   /* Work out which section the relocation is targetted at and the
217      initial relocation command value.  */
218   if (bfd_is_com_section (symbol->section))
219     relocation = 0;
220   else
221     relocation = symbol->value;
222
223   relocation += symbol->section->output_section->vma;
224   relocation += symbol->section->output_offset;
225   relocation += reloc_entry->addend;
226
227   pc = input_section->output_section->vma + input_section->output_offset +
228     reloc_entry->address + 4;
229
230   if ((relocation & 0xF0000000) != (pc & 0xF0000000))
231     return bfd_reloc_overflow;
232
233   return bfd_reloc_continue;
234 }
235
236 /* This is only called when performing a BFD_RELOC_HI16_S relocation.
237    We need to see if bit 15 is set in the result. If it is, we add
238    0x10000 and continue normally. This will compensate for the sign extension
239    when the low bits are added at run time.  */
240
241 static bfd_reloc_status_type
242 mips_fix_hi16_s PARAMS ((bfd *, arelent *, asymbol *, PTR,
243                          asection *, bfd *, char **));
244
245 static bfd_reloc_status_type
246 mips_fix_hi16_s (abfd, reloc_entry, symbol, data, input_section,
247                  output_bfd, error_message)
248      bfd *abfd ATTRIBUTE_UNUSED;
249      arelent *reloc_entry;
250      asymbol *symbol;
251      PTR data ATTRIBUTE_UNUSED;
252      asection *input_section ATTRIBUTE_UNUSED;
253      bfd *output_bfd;
254      char **error_message ATTRIBUTE_UNUSED;
255 {
256   bfd_vma relocation;
257
258   /* If this is a partial relocation, just continue.  */
259   if (output_bfd != (bfd *)NULL)
260     return bfd_reloc_continue;
261
262   /* If this is an undefined symbol, return error.  */
263   if (bfd_is_und_section (symbol->section)
264       && (symbol->flags & BSF_WEAK) == 0)
265     return bfd_reloc_undefined;
266
267   /* Work out which section the relocation is targetted at and the
268      initial relocation command value.  */
269   if (bfd_is_com_section (symbol->section))
270     relocation = 0;
271   else
272     relocation = symbol->value;
273
274   relocation += symbol->section->output_section->vma;
275   relocation += symbol->section->output_offset;
276   relocation += reloc_entry->addend;
277
278   if (relocation & 0x8000)
279     reloc_entry->addend += 0x10000;
280
281   return bfd_reloc_continue;
282 }
283
284 static reloc_howto_type mips_howto_table_ext[] = {
285   {MIPS_RELOC_32,      0, 2, 32, false, 0,  complain_overflow_bitfield, 0,
286         "32",       false, 0, 0xffffffff, false},
287   {MIPS_RELOC_JMP,     2, 2, 26, false, 0, complain_overflow_dont,
288         mips_fix_jmp_addr,
289         "MIPS_JMP", false, 0, 0x03ffffff, false},
290   {MIPS_RELOC_WDISP16, 2, 2, 16, true,  0, complain_overflow_signed, 0,
291         "WDISP16",  false, 0, 0x0000ffff, false},
292   {MIPS_RELOC_HI16,   16, 2, 16, false, 0, complain_overflow_bitfield, 0,
293         "HI16",     false, 0, 0x0000ffff, false},
294   {MIPS_RELOC_HI16_S, 16, 2, 16, false, 0, complain_overflow_bitfield,
295         mips_fix_hi16_s,
296         "HI16_S",   false, 0, 0x0000ffff, false},
297   {MIPS_RELOC_LO16,    0, 2, 16, false, 0, complain_overflow_dont, 0,
298         "LO16",     false, 0, 0x0000ffff, false},
299 };
300
301 static reloc_howto_type *
302 MY(reloc_howto_type_lookup) (abfd, code)
303      bfd *abfd;
304      bfd_reloc_code_real_type code;
305 {
306
307   if (bfd_get_arch (abfd) != bfd_arch_mips)
308     return 0;
309
310   switch (code)
311     {
312     case BFD_RELOC_CTOR:
313     case BFD_RELOC_32:
314       return (&mips_howto_table_ext[MIPS_RELOC_32]);
315     case BFD_RELOC_MIPS_JMP:
316       return (&mips_howto_table_ext[MIPS_RELOC_JMP]);
317     case BFD_RELOC_16_PCREL_S2:
318       return (&mips_howto_table_ext[MIPS_RELOC_WDISP16]);
319     case BFD_RELOC_HI16:
320       return (&mips_howto_table_ext[MIPS_RELOC_HI16]);
321     case BFD_RELOC_HI16_S:
322       return (&mips_howto_table_ext[MIPS_RELOC_HI16_S]);
323     case BFD_RELOC_LO16:
324       return (&mips_howto_table_ext[MIPS_RELOC_LO16]);
325     default:
326       return 0;
327     }
328 }
329
330 /* This is just like the standard aoutx.h version but we need to do our
331    own mapping of external reloc type values to howto entries.  */
332 long
333 MY(canonicalize_reloc) (abfd, section, relptr, symbols)
334       bfd *abfd;
335       sec_ptr section;
336       arelent **relptr;
337       asymbol **symbols;
338 {
339   arelent *tblptr = section->relocation;
340   unsigned int count, c;
341   extern reloc_howto_type NAME(aout,ext_howto_table)[];
342
343   /* If we have already read in the relocation table, return the values.  */
344   if (section->flags & SEC_CONSTRUCTOR)
345     {
346       arelent_chain *chain = section->constructor_chain;
347
348       for (count = 0; count < section->reloc_count; count++)
349         {
350           *relptr++ = &chain->relent;
351           chain = chain->next;
352         }
353       *relptr = 0;
354       return section->reloc_count;
355     }
356
357   if (tblptr && section->reloc_count)
358     {
359       for (count = 0; count++ < section->reloc_count;)
360         *relptr++ = tblptr++;
361       *relptr = 0;
362       return section->reloc_count;
363     }
364
365   if (!NAME(aout,slurp_reloc_table) (abfd, section, symbols))
366     return -1;
367   tblptr = section->relocation;
368
369   /* fix up howto entries.  */
370   for (count = 0; count++ < section->reloc_count;)
371     {
372       c = tblptr->howto - NAME(aout,ext_howto_table);
373       tblptr->howto = &mips_howto_table_ext[c];
374
375       *relptr++ = tblptr++;
376     }
377   *relptr = 0;
378   return section->reloc_count;
379 }
380
381 static CONST struct aout_backend_data MY(backend_data) = {
382   0,                            /* zmagic contiguous */
383   1,                            /* text incl header */
384   0,                            /* entry is text address */
385   0,                            /* exec_hdr_flags */
386   TARGET_PAGE_SIZE,                     /* text vma */
387   MY_set_sizes,
388   0,                            /* text size includes exec header */
389   0,                            /* add_dynamic_symbols */
390   0,                            /* add_one_symbol */
391   0,                            /* link_dynamic_object */
392   0,                            /* write_dynamic_symbol */
393   0,                            /* check_dynamic_reloc */
394   0                             /* finish_dynamic_link */
395 };
396
397 extern const bfd_target aout_mips_big_vec;
398
399 const bfd_target aout_mips_little_vec =
400   {
401     "a.out-mips-little",                /* name */
402     bfd_target_aout_flavour,
403     BFD_ENDIAN_LITTLE,          /* target byte order (little) */
404     BFD_ENDIAN_LITTLE,          /* target headers byte order (little) */
405     (HAS_RELOC | EXEC_P |               /* object flags */
406      HAS_LINENO | HAS_DEBUG |
407      HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
408     (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
409     MY_symbol_leading_char,
410     ' ',                                /* ar_pad_char */
411     15,                         /* ar_max_namelen */
412     bfd_getl64, bfd_getl_signed_64, bfd_putl64,
413     bfd_getl32, bfd_getl_signed_32, bfd_putl32,
414     bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
415     bfd_getl64, bfd_getl_signed_64, bfd_putl64,
416     bfd_getl32, bfd_getl_signed_32, bfd_putl32,
417     bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
418     {_bfd_dummy_target, MY_object_p, /* bfd_check_format */
419      bfd_generic_archive_p, MY_core_file_p},
420     {bfd_false, MY_mkobject,    /* bfd_set_format */
421      _bfd_generic_mkarchive, bfd_false},
422     {bfd_false, MY_write_object_contents, /* bfd_write_contents */
423      _bfd_write_archive_contents, bfd_false},
424
425     BFD_JUMP_TABLE_GENERIC (MY),
426     BFD_JUMP_TABLE_COPY (MY),
427     BFD_JUMP_TABLE_CORE (MY),
428     BFD_JUMP_TABLE_ARCHIVE (MY),
429     BFD_JUMP_TABLE_SYMBOLS (MY),
430     BFD_JUMP_TABLE_RELOCS (MY),
431     BFD_JUMP_TABLE_WRITE (MY),
432     BFD_JUMP_TABLE_LINK (MY),
433     BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
434
435     & aout_mips_big_vec,
436
437     (PTR) MY_backend_data
438   };
439
440 const bfd_target aout_mips_big_vec =
441   {
442     "a.out-mips-big",           /* name */
443     bfd_target_aout_flavour,
444     BFD_ENDIAN_BIG,             /* target byte order (big) */
445     BFD_ENDIAN_BIG,             /* target headers byte order (big) */
446     (HAS_RELOC | EXEC_P |               /* object flags */
447      HAS_LINENO | HAS_DEBUG |
448      HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
449     (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
450     MY_symbol_leading_char,
451     ' ',                                /* ar_pad_char */
452     15,                         /* ar_max_namelen */
453     bfd_getb64, bfd_getb_signed_64, bfd_putb64,
454     bfd_getb32, bfd_getb_signed_32, bfd_putb32,
455     bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
456     bfd_getb64, bfd_getb_signed_64, bfd_putb64,
457     bfd_getb32, bfd_getb_signed_32, bfd_putb32,
458     bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
459     {_bfd_dummy_target, MY_object_p, /* bfd_check_format */
460      bfd_generic_archive_p, MY_core_file_p},
461     {bfd_false, MY_mkobject,    /* bfd_set_format */
462      _bfd_generic_mkarchive, bfd_false},
463     {bfd_false, MY_write_object_contents, /* bfd_write_contents */
464      _bfd_write_archive_contents, bfd_false},
465
466     BFD_JUMP_TABLE_GENERIC (MY),
467     BFD_JUMP_TABLE_COPY (MY),
468     BFD_JUMP_TABLE_CORE (MY),
469     BFD_JUMP_TABLE_ARCHIVE (MY),
470     BFD_JUMP_TABLE_SYMBOLS (MY),
471     BFD_JUMP_TABLE_RELOCS (MY),
472     BFD_JUMP_TABLE_WRITE (MY),
473     BFD_JUMP_TABLE_LINK (MY),
474     BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
475
476     & aout_mips_little_vec,
477
478     (PTR) MY_backend_data
479   };