74b80101d4165b41fc05d292fb8a29ee52f038cd
[platform/upstream/binutils.git] / bfd / mipsbsd.c
1 /* BFD backend for MIPS BSD (a.out) binaries.
2    Copyright (C) 1993 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 MY_symbol_leading_char '\0'
36
37 #define MY(OP) CAT(mipsbsd_,OP)
38
39 #include "bfd.h"
40 #include "sysdep.h"
41 #include "libbfd.h"
42 #include "libaout.h"
43
44 #define SET_ARCH_MACH(ABFD, EXEC) \
45   MY(set_arch_mach)(ABFD, N_MACHTYPE (EXEC)); \
46   MY(choose_reloc_size)(ABFD);
47 void MY(set_arch_mach) PARAMS ((bfd *abfd, int machtype));
48 static void MY(choose_reloc_size) PARAMS ((bfd *abfd));
49
50 #define MY_write_object_contents MY(write_object_contents)
51 static boolean MY(write_object_contents) PARAMS ((bfd *abfd));
52
53 #define MY_reloc_howto_type_lookup MY(reloc_howto_type_lookup)
54 #define MY_canonicalize_reloc MY(canonicalize_reloc)
55
56 #define MY_backend_data &MY(backend_data)
57 #define MY_BFD_TARGET
58
59 #include "aout-target.h"
60
61 void
62 MY(set_arch_mach) (abfd, machtype)
63      bfd *abfd;
64      int machtype;
65 {
66   enum bfd_architecture arch;
67   long machine;
68
69   /* Determine the architecture and machine type of the object file. */
70   switch (machtype) {
71
72   case M_MIPS1:
73     arch = bfd_arch_mips;
74     machine = 3000;
75     break;
76
77   case M_MIPS2:
78     arch = bfd_arch_mips;
79     machine = 4000;
80     break;
81
82   default:
83     arch = bfd_arch_obscure;
84     machine = 0;
85     break;
86   }
87   bfd_set_arch_mach(abfd, arch, machine);  
88 }
89
90 /* Determine the size of a relocation entry, based on the architecture */
91 static void
92 MY(choose_reloc_size) (abfd)
93      bfd *abfd;
94 {
95   switch (bfd_get_arch(abfd)) {
96   case bfd_arch_sparc:
97   case bfd_arch_a29k:
98   case bfd_arch_mips:
99     obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
100     break;
101   default:
102     obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
103     break;
104   }
105 }
106
107 /* Write an object file in BSD a.out format.
108   Section contents have already been written.  We write the
109   file header, symbols, and relocation.  */
110
111 static boolean
112 MY(write_object_contents) (abfd)
113      bfd *abfd;
114 {
115   struct external_exec exec_bytes;
116   struct internal_exec *execp = exec_hdr (abfd);
117
118   /* Magic number, maestro, please!  */
119   switch (bfd_get_arch(abfd)) {
120   case bfd_arch_m68k:
121     switch (bfd_get_mach(abfd)) {
122     case 68010:
123       N_SET_MACHTYPE(*execp, M_68010);
124       break;
125     default:
126     case 68020:
127       N_SET_MACHTYPE(*execp, M_68020);
128       break;
129     }
130     break;
131   case bfd_arch_sparc:
132     N_SET_MACHTYPE(*execp, M_SPARC);
133     break;
134   case bfd_arch_i386:
135     N_SET_MACHTYPE(*execp, M_386);
136     break;
137   case bfd_arch_a29k:
138     N_SET_MACHTYPE(*execp, M_29K);
139     break;
140   case bfd_arch_mips:
141     switch (bfd_get_mach(abfd)) {
142     case 4000:
143     case 6000:
144       N_SET_MACHTYPE(*execp, M_MIPS2);
145       break;
146     default:
147       N_SET_MACHTYPE(*execp, M_MIPS1);
148       break;
149     }
150     break;
151   default:
152     N_SET_MACHTYPE(*execp, M_UNKNOWN);
153   }
154
155   MY(choose_reloc_size)(abfd);
156
157   WRITE_HEADERS(abfd, execp);
158
159   return true;
160 }
161
162 /*
163  * MIPS relocation types.
164  */
165 #define MIPS_RELOC_32           0
166 #define MIPS_RELOC_JMP          1
167 #define MIPS_RELOC_WDISP16      2
168 #define MIPS_RELOC_HI16         3
169 #define MIPS_RELOC_HI16_S       4
170 #define MIPS_RELOC_LO16         5
171
172 /*
173  * This is only called when performing a BFD_RELOC_HI16_S relocation.
174  * We need to see if bit 15 is set in the result. If it is, we add
175  * 0x10000 and continue normally. This will compensate for the sign extension
176  * when the low bits are added at run time.
177  */
178 bfd_reloc_status_type
179 mips_fix_hi16_s (abfd,reloc_entry,symbol,data,input_section,output_bfd)
180      bfd *abfd;
181      arelent *reloc_entry;
182      struct symbol_cache_entry *symbol;
183      PTR data;
184      asection *input_section;
185      bfd *output_bfd;
186 {
187   bfd_vma relocation;
188  
189   /* If this is a partial relocation, just continue. */
190   if (output_bfd != (bfd *)NULL)
191     return bfd_reloc_continue;
192
193   /* 
194    * Work out which section the relocation is targetted at and the
195    * initial relocation command value.
196    */
197   if (symbol->section == &bfd_com_section)
198     relocation = 0;
199   else
200     relocation = symbol->value;
201
202   relocation += symbol->section->output_section->vma;
203   relocation += symbol->section->output_offset;
204   relocation += reloc_entry->addend;
205
206   if (relocation & 0x8000)
207     reloc_entry->addend += 0x10000;
208
209   return bfd_reloc_continue;
210 }
211
212 static reloc_howto_type mips_howto_table_ext[] = {
213   MIPS_RELOC_32,      0, 2, 32, false, 0, true,  true, 0,
214         "32",       false, 0, 0xffffffff, false,
215   MIPS_RELOC_JMP,     2, 2, 26, false, 0, false, true, 0,
216         "MIPS_JMP", false, 0, 0x03ffffff, false,
217   MIPS_RELOC_WDISP16, 2, 1, 16, true,  0, false, true, 0,
218         "WDISP16",  false, 0, 0x0000ffff, false,
219   MIPS_RELOC_HI16,   16, 1, 16, false, 0, false, true, 0,
220         "HI16",     false, 0, 0x0000ffff, false,
221   MIPS_RELOC_HI16_S, 16, 1, 16, false, 0, false, true, mips_fix_hi16_s,
222         "HI16_S",   false, 0, 0x0000ffff, false,
223   MIPS_RELOC_LO16,    0, 1, 16, false, 0, false, true, 0,
224         "LO16",     false, 0, 0x0000ffff, false,
225 };
226
227 static reloc_howto_type *
228 MY(reloc_howto_type_lookup) (abfd, code)
229      bfd *abfd;
230      bfd_reloc_code_real_type code;
231 {
232   extern reloc_howto_type NAME(aout,ext_howto_table)[];
233
234   if (bfd_get_arch (abfd) != bfd_arch_mips)
235     return 0;
236
237   switch (code)
238     {
239     case BFD_RELOC_32:
240       return (&mips_howto_table_ext[MIPS_RELOC_32]);
241     case BFD_RELOC_MIPS_JMP:
242       return (&mips_howto_table_ext[MIPS_RELOC_JMP]);
243     case BFD_RELOC_16_PCREL_S2:
244       return (&mips_howto_table_ext[MIPS_RELOC_WDISP16]);
245     case BFD_RELOC_HI16:
246       return (&mips_howto_table_ext[MIPS_RELOC_HI16]);
247     case BFD_RELOC_HI16_S:
248       return (&mips_howto_table_ext[MIPS_RELOC_HI16_S]);
249     case BFD_RELOC_LO16:
250       return (&mips_howto_table_ext[MIPS_RELOC_LO16]);
251     default:
252       return 0;
253     }
254 }
255
256 /*
257  * This is just like the standard aoutx.h version but we need to do our
258  * own mapping of external reloc type values to howto entries.
259  */
260 unsigned int
261 MY(canonicalize_reloc)(abfd, section, relptr, symbols)
262       bfd *abfd;
263       sec_ptr section;
264       arelent **relptr;
265       asymbol **symbols;
266 {
267   arelent *tblptr = section->relocation;
268   unsigned int count, c;
269   extern reloc_howto_type NAME(aout,ext_howto_table)[];
270
271   /* If we have already read in the relocation table, return the values. */
272   if (section->flags & SEC_CONSTRUCTOR) {
273     arelent_chain *chain = section->constructor_chain;
274
275     for (count = 0; count < section->reloc_count; count++) {
276       *relptr++ = &chain->relent;
277       chain = chain->next;
278     }
279     *relptr = 0;
280     return section->reloc_count;
281   }
282   if (tblptr && section->reloc_count) {
283     for (count = 0; count++ < section->reloc_count;) 
284       *relptr++ = tblptr++;
285     *relptr = 0;
286     return section->reloc_count;
287   }
288
289   if (!NAME(aout,slurp_reloc_table)(abfd, section, symbols))
290     return 0;
291   tblptr = section->relocation;
292   if (!tblptr)
293     return 0;
294
295   /* fix up howto entries */
296   for (count = 0; count++ < section->reloc_count;) 
297     {
298       c = tblptr->howto - NAME(aout,ext_howto_table);
299       tblptr->howto = &mips_howto_table_ext[c];
300
301       *relptr++ = tblptr++;
302     }
303   *relptr = 0;
304   return section->reloc_count;
305 }
306
307 static CONST struct aout_backend_data MY(backend_data) = {
308   0,                            /* zmagic contiguous */
309   1,                            /* text incl header */
310   PAGE_SIZE,                    /* text vma */
311   MY_set_sizes,
312   0,                            /* text size includes exec header */
313 };
314
315 bfd_target aout_mips_little_vec =
316 {
317   "aout-mips-little",           /* name */
318   bfd_target_aout_flavour,
319   false,                        /* target byte order (little) */
320   false,                        /* target headers byte order (little) */
321   (HAS_RELOC | EXEC_P |         /* object flags */
322    HAS_LINENO | HAS_DEBUG |
323    HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
324   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
325   MY_symbol_leading_char,
326   ' ',                          /* ar_pad_char */
327   15,                           /* ar_max_namelen */
328   1,                            /* minimum alignment */
329   _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16, /* data */
330   _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16, /* hdrs */
331     {_bfd_dummy_target, MY_object_p, /* bfd_check_format */
332        bfd_generic_archive_p, MY_core_file_p},
333     {bfd_false, MY_mkobject,    /* bfd_set_format */
334        _bfd_generic_mkarchive, bfd_false},
335     {bfd_false, MY_write_object_contents, /* bfd_write_contents */
336        _bfd_write_archive_contents, bfd_false},
337
338   MY_core_file_failing_command,
339   MY_core_file_failing_signal,
340   MY_core_file_matches_executable_p,
341   MY_slurp_armap,
342   MY_slurp_extended_name_table,
343   MY_truncate_arname,
344   MY_write_armap,
345   MY_close_and_cleanup,
346   MY_set_section_contents,
347   MY_get_section_contents,
348   MY_new_section_hook,
349   MY_get_symtab_upper_bound,
350   MY_get_symtab,
351   MY_get_reloc_upper_bound,
352   MY_canonicalize_reloc,
353   MY_make_empty_symbol,
354   MY_print_symbol,
355   MY_get_lineno,
356   MY_set_arch_mach,
357   MY_openr_next_archived_file,
358   MY_find_nearest_line,
359   MY_generic_stat_arch_elt,
360   MY_sizeof_headers,
361   MY_bfd_debug_info_start,
362   MY_bfd_debug_info_end,
363   MY_bfd_debug_info_accumulate,
364   bfd_generic_get_relocated_section_contents,
365   bfd_generic_relax_section,
366   bfd_generic_seclet_link,
367   MY_reloc_howto_type_lookup,
368   MY_make_debug_symbol,
369   (PTR) MY_backend_data,
370 };
371
372 bfd_target aout_mips_big_vec =
373 {
374   "aout-mips-big",              /* name */
375   bfd_target_aout_flavour,
376   true,                         /* target byte order (big) */
377   true,                         /* target headers byte order (big) */
378   (HAS_RELOC | EXEC_P |         /* object flags */
379    HAS_LINENO | HAS_DEBUG |
380    HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
381   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
382   MY_symbol_leading_char,
383   ' ',                          /* ar_pad_char */
384   15,                           /* ar_max_namelen */
385   1,                            /* minimum alignment */
386   _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* data */
387   _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* hdrs */
388     {_bfd_dummy_target, MY_object_p, /* bfd_check_format */
389        bfd_generic_archive_p, MY_core_file_p},
390     {bfd_false, MY_mkobject,    /* bfd_set_format */
391        _bfd_generic_mkarchive, bfd_false},
392     {bfd_false, MY_write_object_contents, /* bfd_write_contents */
393        _bfd_write_archive_contents, bfd_false},
394
395   MY_core_file_failing_command,
396   MY_core_file_failing_signal,
397   MY_core_file_matches_executable_p,
398   MY_slurp_armap,
399   MY_slurp_extended_name_table,
400   MY_truncate_arname,
401   MY_write_armap,
402   MY_close_and_cleanup,
403   MY_set_section_contents,
404   MY_get_section_contents,
405   MY_new_section_hook,
406   MY_get_symtab_upper_bound,
407   MY_get_symtab,
408   MY_get_reloc_upper_bound,
409   MY_canonicalize_reloc,
410   MY_make_empty_symbol,
411   MY_print_symbol,
412   MY_get_lineno,
413   MY_set_arch_mach,
414   MY_openr_next_archived_file,
415   MY_find_nearest_line,
416   MY_generic_stat_arch_elt,
417   MY_sizeof_headers,
418   MY_bfd_debug_info_start,
419   MY_bfd_debug_info_end,
420   MY_bfd_debug_info_accumulate,
421   bfd_generic_get_relocated_section_contents,
422   bfd_generic_relax_section,
423   bfd_generic_seclet_link,
424   MY_reloc_howto_type_lookup,
425   MY_make_debug_symbol,
426   (PTR) MY_backend_data,
427 };