Initial revision
[platform/upstream/binutils.git] / bfd / newsos3.c
1 /*** bfd backend for NewsOS3 (Sony, 68k) binaries */
2
3 /* Copyright (C) 1990, 1991 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Diddler.
6
7 BFD 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 1, or (at your option)
10 any later version.
11
12 BFD 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 BFD; see the file COPYING.  If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
20
21 #define TARGET_BYTE_ORDER_BIG_P 1
22
23 #include <ansidecl.h>
24 #include "sysdep.h"
25 #include "bfd.h"
26 #include "libbfd.h"
27
28 int vfprintf(file, format, args) /* Temporary crock! */
29      FILE *file; char *format; char *args;
30 {
31     return _doprnt (format, args, file);
32 }
33
34 #define PAGE_SIZE       4096
35 #define SEGMENT_SIZE    0x2000          /* FIXME, is this right?? */
36 #define TEXT_START_ADDR PAGE_SIZE
37
38 #include "a.out.gnu.h"
39 #include "stab.gnu.h"
40 #include "ar.h"
41 #include "liba.out.h"           /* BFD a.out internal data structures */
42
43 bfd_target *newsos3_callback ();
44
45 bfd_target *
46 newsos3_object_p (abfd)
47      bfd *abfd;
48 {
49   unsigned char magicbuf[LONG_SIZE]; /* Raw bytes of magic number from file */
50   unsigned long magic;          /* Swapped magic number */
51
52   bfd_error = system_call_error;
53
54   if (bfd_read ((PTR)magicbuf, 1, sizeof (magicbuf), abfd) !=
55       sizeof (magicbuf))
56     return 0;
57   magic = bfd_h_getlong (abfd, magicbuf);
58
59   if (N_BADMAG (*((struct exec *) &magic))) return 0;
60
61   return some_aout_object_p (abfd, newsos3_callback);
62 }
63
64 /* Finish up the reading of a NEWS-OS a.out file header */
65 bfd_target *
66 newsos3_callback (abfd)
67      bfd *abfd;
68 {
69   struct exec *execp = exec_hdr (abfd);
70
71   /* The virtual memory addresses of the sections */
72   obj_datasec (abfd)->vma = N_DATADDR(*execp);
73   obj_bsssec (abfd)->vma = N_BSSADDR(*execp);
74   obj_textsec (abfd)->vma = N_TXTADDR(*execp);
75
76   /* The file offsets of the sections */
77   obj_textsec (abfd)->filepos = N_TXTOFF(*execp);
78   obj_datasec (abfd)->filepos = N_DATOFF(*execp);
79
80   /* The file offsets of the relocation info */
81   obj_textsec (abfd)->rel_filepos = N_TRELOFF(*execp);
82   obj_datasec (abfd)->rel_filepos = N_DRELOFF(*execp);
83
84   /* The file offsets of the string table and symbol table.  */
85   obj_str_filepos (abfd) = N_STROFF (*execp);
86   obj_sym_filepos (abfd) = N_SYMOFF (*execp);
87
88   /* Determine the architecture and machine type of the object file.  */
89   abfd->obj_arch = bfd_arch_m68k;
90   abfd->obj_machine = 0;
91
92   return abfd->xvec;
93 }
94
95 /* Write an object file in NEWS-OS format.
96    Section contents have already been written.  We write the
97    file header, symbols, and relocation.  */
98
99 boolean
100 newsos3_write_object_contents (abfd)
101      bfd *abfd;
102 {
103   size_t data_pad = 0;
104   unsigned char exec_bytes[EXEC_BYTES_SIZE];
105   struct exec *execp = exec_hdr (abfd);
106
107   execp->a_text = obj_textsec (abfd)->size;
108
109   if (abfd->flags & D_PAGED)
110     execp->a_info = ZMAGIC;
111   else if (abfd->flags & WP_TEXT)
112     execp->a_info = NMAGIC;
113   else
114     execp->a_info = OMAGIC;
115
116   if (abfd->flags & D_PAGED) 
117       {
118         data_pad = ((obj_datasec(abfd)->size + PAGE_SIZE -1)
119                     & (- PAGE_SIZE)) - obj_datasec(abfd)->size;
120
121         if (data_pad > obj_bsssec(abfd)->size)
122           execp->a_bss = 0;
123         else 
124           execp->a_bss = obj_bsssec(abfd)->size - data_pad;
125         execp->a_data = obj_datasec(abfd)->size + data_pad;
126
127       }
128   else {
129     execp->a_data = obj_datasec (abfd)->size;
130     execp->a_bss = obj_bsssec (abfd)->size;
131   }
132
133   execp->a_syms = bfd_get_symcount (abfd) * sizeof (struct nlist);
134   execp->a_entry = bfd_get_start_address (abfd);
135
136   execp->a_trsize = ((obj_textsec (abfd)->reloc_count) *
137                      obj_reloc_entry_size (abfd));
138                        
139   execp->a_drsize = ((obj_datasec (abfd)->reloc_count) *
140                      obj_reloc_entry_size (abfd));
141
142   bfd_aout_swap_exec_header_out (abfd, execp, exec_bytes);
143
144   bfd_seek (abfd, 0L, false);
145   bfd_write ((PTR) exec_bytes, 1, EXEC_BYTES_SIZE, abfd);
146
147   /* Now write out reloc info, followed by syms and strings */
148
149   if (bfd_get_symcount (abfd) != 0) 
150     {
151       bfd_seek (abfd, (long)(N_SYMOFF(*execp)), false);
152       aout_write_syms (abfd);
153
154       bfd_seek (abfd, (long)(N_TRELOFF(*execp)), false);
155       if (!aout_squirt_out_relocs (abfd, obj_textsec (abfd))) return false;
156
157       bfd_seek (abfd, (long)(N_DRELOFF(*execp)), false);
158       if (!aout_squirt_out_relocs (abfd, obj_datasec (abfd))) return false;
159     }
160   return true;
161 }
162 \f
163 /* Transfer vectors for NEWS-OS version 3 */
164
165 /* We use BFD generic archive files.  */
166 #define aout_openr_next_archived_file   bfd_generic_openr_next_archived_file
167 #define aout_generic_stat_arch_elt      bfd_generic_stat_arch_elt
168 #define aout_slurp_armap                bfd_slurp_bsd_armap
169 #define aout_slurp_extended_name_table  bfd_true
170 #define aout_write_armap                bsd_write_armap
171 #define aout_truncate_arname            bfd_bsd_truncate_arname
172
173 /* We don't support core files yet.  FIXME.  */
174 #define aout_core_file_failing_command  _bfd_dummy_core_file_failing_command
175 #define aout_core_file_failing_signal   _bfd_dummy_core_file_failing_signal
176 #define aout_core_file_matches_executable_p     \
177                                 _bfd_dummy_core_file_matches_executable_p
178 #define aout_core_file_p                _bfd_dummy_target
179
180 /* We define our own versions of these routines.  */
181
182
183 bfd_target newsos3_vec = /* Sony 68k-based machines running newos3 */
184 {
185   "a.out-newos3",               /* name */
186   bfd_target_aout_flavour_enum,
187   true,                         /* target byte order */
188   true,                         /* target headers byte order */
189   (HAS_RELOC | EXEC_P |         /* object flags */
190    HAS_LINENO | HAS_DEBUG |
191    HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
192   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
193   ' ',                          /* ar_pad_char */
194   16,                           /* ar_max_namelen */
195
196   _do_getblong, _do_putblong, _do_getbshort, _do_putbshort, /* data */
197   _do_getblong, _do_putblong, _do_getbshort, _do_putbshort, /* hdrs */
198
199   {_bfd_dummy_target, newsos3_object_p, /* bfd_check_format */
200      bfd_generic_archive_p, aout_core_file_p},
201   {bfd_false, aout_mkobject,            /* bfd_set_format */
202      _bfd_generic_mkarchive, bfd_false},
203   {bfd_false, newsos3_write_object_contents,    /* bfd_write_contents */
204      _bfd_write_archive_contents, bfd_false},
205
206   JUMP_TABLE(aout)
207 };