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