Now full of documentation. Yum Yum.
[platform/upstream/binutils.git] / bfd / host-aout.c
1 /* BFD backend for local host's a.out 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 #include <ansidecl.h>
22 #include <sysdep.h>
23 #include "bfd.h"
24 #include "libbfd.h"
25
26 #include <a.out.h>
27 #include "liba.out.h"           /* BFD a.out internal data structures */
28
29 #include "trad-core.h"          /* Traditional Unix core files */
30
31 void (*bfd_error_trap)();
32
33 static bfd_target *host_aout_callback ();
34
35 /*SUPPRESS558*/
36 /*SUPPRESS529*/
37
38 bfd_target *
39 host_aout_object_p (abfd)
40      bfd *abfd;
41 {
42   unsigned char magicbuf[4];    /* Raw bytes of magic number from file */
43   unsigned long magic;          /* Swapped magic number */
44
45   bfd_error = system_call_error;
46
47   if (bfd_read ((PTR)magicbuf, 1, sizeof (magicbuf), abfd) !=
48       sizeof (magicbuf))
49     return 0;
50   magic = bfd_h_getlong (abfd, magicbuf);
51
52   if (N_BADMAG (*((struct exec *) &magic))) return 0;
53
54   return some_aout_object_p (abfd, host_aout_callback);
55 }
56
57 /* Set parameters about this a.out file that are machine-dependent.
58    This routine is called from some_aout_object_p just before it returns.  */
59
60 static bfd_target *
61 host_aout_callback (abfd)
62      bfd *abfd;
63 {
64   struct exec *execp = exec_hdr (abfd);
65
66   /* The virtual memory addresses of the sections */
67   obj_datasec (abfd)->vma = N_DATADDR(*execp);
68   obj_bsssec (abfd)->vma = N_BSSADDR(*execp);
69   obj_textsec (abfd)->vma = N_TXTADDR(*execp);
70
71   /* The file offsets of the sections */
72   obj_textsec (abfd)->filepos = N_TXTOFF(*execp);
73   obj_datasec (abfd)->filepos = N_DATOFF(*execp);
74
75   /* The file offsets of the relocation info */
76   obj_textsec (abfd)->rel_filepos = N_TRELOFF(*execp);
77   obj_datasec (abfd)->rel_filepos = N_DRELOFF(*execp);
78
79   /* The file offsets of the string table and symbol table.  */
80   obj_str_filepos (abfd) = N_STROFF (*execp);
81   obj_sym_filepos (abfd) = N_SYMOFF (*execp);
82
83 #ifdef HOST_MACHINE_ARCH
84   abfd->obj_arch = HOST_MACHINE_ARCH;
85 #endif
86 #ifdef HOST_MACHINE_MACHINE
87   abfd->obj_machine = HOST_MACHINE_MACHINE;
88 #endif
89
90   return abfd->xvec;
91 }
92
93
94 boolean
95 host_aout_mkobject (abfd)
96      bfd *abfd;
97 {
98   char *rawptr;
99
100   bfd_error = system_call_error;
101
102   /* Use an intermediate variable for clarity */
103   rawptr = bfd_zalloc (abfd, sizeof (struct aoutdata) + sizeof (struct exec));
104
105   if (rawptr == NULL) {
106     bfd_error = no_memory;
107     return false;
108   }
109
110   set_tdata (abfd, (struct aoutdata *) rawptr);
111   exec_hdr (abfd) = (struct exec *) (rawptr + sizeof (struct aoutdata));
112
113   /* For simplicity's sake we just make all the sections right here. */
114
115   obj_textsec (abfd) = (asection *)NULL;
116   obj_datasec (abfd) = (asection *)NULL;
117   obj_bsssec (abfd) = (asection *)NULL;
118   bfd_make_section (abfd, ".text");
119   bfd_make_section (abfd, ".data");
120   bfd_make_section (abfd, ".bss");
121
122   return true;
123 }
124
125 /* Write an object file in host a.out format.
126    Section contents have already been written.  We write the
127    file header, symbols, and relocation.  */
128
129 boolean
130 host_aout_write_object_contents (abfd)
131      bfd *abfd;
132 {
133 /* This works because we are on the host system */
134 #define EXEC_BYTES_SIZE sizeof (struct exec)
135   size_t data_pad = 0;
136   unsigned char exec_bytes[EXEC_BYTES_SIZE];
137   struct exec *execp = exec_hdr (abfd);
138
139   execp->a_text = obj_textsec (abfd)->size;
140
141
142   N_SET_MAGIC (*execp, OMAGIC);
143   if (abfd->flags & D_PAGED) {
144     /* This is not strictly true, but will probably do for the default
145         case.  FIXME.  
146         */
147
148     execp->a_text = obj_textsec (abfd)->size + EXEC_BYTES_SIZE;
149     N_SET_MAGIC (*execp, ZMAGIC);
150   } else if (abfd->flags & WP_TEXT) {
151     N_SET_MAGIC (*execp, NMAGIC);
152   }
153   N_SET_FLAGS (*execp, 0x1);    /* copied from ld.c; who the hell knows? */
154
155   if (abfd->flags & D_PAGED) 
156       {
157         data_pad = ((obj_datasec(abfd)->size + PAGE_SIZE -1)
158                     & (- PAGE_SIZE)) - obj_datasec(abfd)->size;
159
160         if (data_pad > obj_bsssec(abfd)->size)
161           execp->a_bss = 0;
162         else 
163           execp->a_bss = obj_bsssec(abfd)->size - data_pad;
164         execp->a_data = obj_datasec(abfd)->size + data_pad;
165
166       }
167   else {
168     execp->a_data = obj_datasec (abfd)->size;
169     execp->a_bss = obj_bsssec (abfd)->size;
170   }
171
172   execp->a_syms = bfd_get_symcount (abfd) * sizeof (struct nlist);
173   execp->a_entry = bfd_get_start_address (abfd);
174
175   execp->a_trsize = ((obj_textsec (abfd)->reloc_count) *
176                      obj_reloc_entry_size (abfd));
177                        
178   execp->a_drsize = ((obj_datasec (abfd)->reloc_count) *
179                      obj_reloc_entry_size (abfd));
180
181   bfd_aout_swap_exec_header_out (abfd, execp, exec_bytes);
182
183   bfd_seek (abfd, 0L, false);
184   bfd_write ((PTR) exec_bytes, 1, EXEC_BYTES_SIZE, abfd);
185
186   /* Now write out reloc info, followed by syms and strings */
187
188   if (bfd_get_symcount (abfd) != 0) 
189     {
190       bfd_seek (abfd,
191                 (long)(N_SYMOFF(*execp)), false);
192
193       aout_write_syms (abfd);
194
195       bfd_seek (abfd,   (long)(N_TRELOFF(*execp)), false);
196
197       if (!aout_squirt_out_relocs (abfd, obj_textsec (abfd))) return false;
198       bfd_seek (abfd, (long)(N_DRELOFF(*execp)), false);
199
200       if (!aout_squirt_out_relocs (abfd, obj_datasec (abfd))) return false;
201     }
202   return true;
203 }
204 \f
205 /* We use BFD generic archive files.  */
206 #define aout_openr_next_archived_file   bfd_generic_openr_next_archived_file
207 #define aout_generic_stat_arch_elt      bfd_generic_stat_arch_elt
208 #define aout_slurp_armap                bfd_slurp_bsd_armap
209 #define aout_slurp_extended_name_table  bfd_true
210 #define aout_write_armap                bsd_write_armap
211 #define aout_truncate_arname            bfd_bsd_truncate_arname
212
213 /* We use traditional Unix core file format.  */
214 #define aout_core_file_failing_command  trad_unix_core_file_failing_command
215 #define aout_core_file_failing_signal   trad_unix_core_file_failing_signal
216 #define aout_core_file_matches_executable_p     \
217                                         trad_unix_core_file_matches_executable_p
218
219 /* We replace this function */
220 #define aout_write_object_contents      host_aout_write_object_contents
221
222 bfd_target host_aout_big_vec =
223 {
224   "a.out-host-big",             /* name */
225   bfd_target_aout_flavour_enum,
226   true,                         /* target byte order */
227   true,                         /* target headers byte order */
228   (HAS_RELOC | EXEC_P |         /* object flags */
229    HAS_LINENO | HAS_DEBUG |
230    HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
231   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
232   ' ',                          /* ar_pad_char */
233   16,                           /* ar_max_namelen */
234   _do_getblong, _do_putblong, _do_getbshort, _do_putbshort, /* data */
235   _do_getblong, _do_putblong, _do_getbshort, _do_putbshort, /* hdrs */
236
237     {_bfd_dummy_target, host_aout_object_p,
238        bfd_generic_archive_p, trad_unix_core_file_p},
239     {bfd_false, host_aout_mkobject,
240        _bfd_generic_mkarchive, bfd_false},
241     {bfd_false, aout_write_object_contents,     /* bfd_write_contents */
242        _bfd_write_archive_contents, bfd_false},
243
244   JUMP_TABLE(aout)
245 };
246
247 bfd_target host_aout_little_vec =
248 {
249   "a.out-host-little",          /* name */
250   bfd_target_aout_flavour_enum,
251   false,                        /* target byte order */
252   false,                        /* target headers byte order */
253   (HAS_RELOC | EXEC_P |         /* object flags */
254    HAS_LINENO | HAS_DEBUG |
255    HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
256   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
257   ' ',                          /* ar_pad_char */
258   16,                           /* ar_max_namelen */
259   _do_getllong, _do_putllong, _do_getlshort, _do_putlshort, /* data */
260   _do_getllong, _do_putllong, _do_getlshort, _do_putlshort, /* hdrs */
261
262     {_bfd_dummy_target, host_aout_object_p,
263        bfd_generic_archive_p, trad_unix_core_file_p},
264     {bfd_false, host_aout_mkobject,
265        _bfd_generic_mkarchive, bfd_false},
266     {bfd_false, aout_write_object_contents,     /* bfd_write_contents */
267        _bfd_write_archive_contents, bfd_false},
268
269   JUMP_TABLE(aout)
270 };