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