Added 1993 copyrights to files that I have changed in 1993. Some were
[external/binutils.git] / bfd / trad-core.c
1 /* BFD back end for traditional Unix core files (U-area and raw sections)
2    Copyright 1988, 1989, 1991, 1992, 1993 Free Software Foundation, Inc.
3    Written by John Gilmore of Cygnus Support.
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 /* To use this file on a particular host, configure the host with these
22    parameters in the config/h-HOST file:
23
24         HDEFINES=-DTRAD_CORE
25         HDEPFILES=trad-core.o
26
27  */
28
29 #include "bfd.h"
30 #include "sysdep.h"
31 #include "libbfd.h"
32 #include "libaout.h"           /* BFD a.out internal data structures */
33
34 #include <stdio.h>
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/dir.h>
38 #include <signal.h>
39
40 #include <sys/user.h>           /* After a.out.h  */
41 #include <sys/file.h>
42
43 #include <errno.h>
44
45   struct trad_core_struct 
46     {
47       asection *data_section;
48       asection *stack_section;
49       asection *reg_section;
50       struct user u;
51     } *rawptr;
52
53 #define core_upage(bfd) (&((bfd)->tdata.trad_core_data->u))
54 #define core_datasec(bfd) ((bfd)->tdata.trad_core_data->data_section)
55 #define core_stacksec(bfd) ((bfd)->tdata.trad_core_data->stack_section)
56 #define core_regsec(bfd) ((bfd)->tdata.trad_core_data->reg_section)
57
58 /* forward declarations */
59
60 bfd_target *    trad_unix_core_file_p PARAMS ((bfd *abfd));
61 char *          trad_unix_core_file_failing_command PARAMS ((bfd *abfd));
62 int             trad_unix_core_file_failing_signal PARAMS ((bfd *abfd));
63 boolean         trad_unix_core_file_matches_executable_p
64                          PARAMS ((bfd *core_bfd, bfd *exec_bfd));
65
66 /* Handle 4.2-style (and perhaps also sysV-style) core dump file.  */
67
68 /* ARGSUSED */
69 bfd_target *
70 trad_unix_core_file_p (abfd)
71      bfd *abfd;
72
73 {
74   int val;
75   struct user u;
76
77   val = bfd_read ((void *)&u, 1, sizeof u, abfd);
78   if (val != sizeof u)
79     return 0;                   /* Too small to be a core file */
80
81   /* Sanity check perhaps??? */
82   if (u.u_dsize > 0x1000000)    /* Remember, it's in pages... */
83     return 0;
84   if (u.u_ssize > 0x1000000)
85     return 0;
86   /* Check that the size claimed is no greater than the file size. FIXME. */
87
88   /* OK, we believe you.  You're a core file (sure, sure).  */
89
90   /* Allocate both the upage and the struct core_data at once, so
91      a single free() will free them both.  */
92   rawptr = (struct trad_core_struct *)
93                 bfd_zalloc (abfd, sizeof (struct trad_core_struct));
94   if (rawptr == NULL) {
95     bfd_error = no_memory;
96     return 0;
97   }
98   
99   abfd->tdata.trad_core_data = rawptr;
100
101   rawptr->u = u; /*Copy the uarea into the tdata part of the bfd */
102
103   /* Create the sections.  This is raunchy, but bfd_close wants to free
104      them separately.  */
105
106   core_stacksec(abfd) = (asection *) zalloc (sizeof (asection));
107   if (core_stacksec (abfd) == NULL) {
108   loser:
109     bfd_error = no_memory;
110     free ((void *)rawptr);
111     return 0;
112   }
113   core_datasec (abfd) = (asection *) zalloc (sizeof (asection));
114   if (core_datasec (abfd) == NULL) {
115   loser1:
116     free ((void *)core_stacksec (abfd));
117     goto loser;
118   }
119   core_regsec (abfd) = (asection *) zalloc (sizeof (asection));
120   if (core_regsec (abfd) == NULL) {
121     free ((void *)core_datasec (abfd));
122     goto loser1;
123   }
124
125   core_stacksec (abfd)->name = ".stack";
126   core_datasec (abfd)->name = ".data";
127   core_regsec (abfd)->name = ".reg";
128
129   core_stacksec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
130   core_datasec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
131   core_regsec (abfd)->flags = SEC_ALLOC + SEC_HAS_CONTENTS;
132
133   core_datasec (abfd)->_raw_size =  NBPG * u.u_dsize;
134   core_stacksec (abfd)->_raw_size = NBPG * u.u_ssize;
135   core_regsec (abfd)->_raw_size = NBPG * UPAGES; /* Larger than sizeof struct u */
136
137   /* What a hack... we'd like to steal it from the exec file,
138      since the upage does not seem to provide it.  FIXME.  */
139 #ifdef HOST_DATA_START_ADDR
140   core_datasec (abfd)->vma = HOST_DATA_START_ADDR;
141 #else
142   core_datasec (abfd)->vma = HOST_TEXT_START_ADDR + (NBPG * u.u_tsize);
143 #endif
144   core_stacksec (abfd)->vma = HOST_STACK_END_ADDR - (NBPG * u.u_ssize);
145   /* This is tricky.  As the "register section", we give them the entire
146      upage and stack.  u.u_ar0 points to where "register 0" is stored.
147      There are two tricks with this, though.  One is that the rest of the
148      registers might be at positive or negative (or both) displacements
149      from *u_ar0.  The other is that u_ar0 is sometimes an absolute address
150      in kernel memory, and on other systems it is an offset from the beginning
151      of the `struct user'.
152      
153      As a practical matter, we don't know where the registers actually are,
154      so we have to pass the whole area to GDB.  We encode the value of u_ar0
155      by setting the .regs section up so that its virtual memory address
156      0 is at the place pointed to by u_ar0 (by setting the vma of the start
157      of the section to -u_ar0).  GDB uses this info to locate the regs,
158      using minor trickery to get around the offset-or-absolute-addr problem. */
159   core_regsec (abfd)->vma = 0 - (int) u.u_ar0;
160
161   core_datasec (abfd)->filepos = NBPG * UPAGES;
162   core_stacksec (abfd)->filepos = (NBPG * UPAGES) + NBPG * u.u_dsize;
163   core_regsec (abfd)->filepos = 0; /* Register segment is the upage */
164
165   /* Align to word at least */
166   core_stacksec (abfd)->alignment_power = 2;
167   core_datasec (abfd)->alignment_power = 2;
168   core_regsec (abfd)->alignment_power = 2;
169
170   abfd->sections = core_stacksec (abfd);
171   core_stacksec (abfd)->next = core_datasec (abfd);
172   core_datasec (abfd)->next = core_regsec (abfd);
173   abfd->section_count = 3;
174
175   return abfd->xvec;
176 }
177
178 char *
179 trad_unix_core_file_failing_command (abfd)
180      bfd *abfd;
181 {
182 #ifndef NO_CORE_COMMAND
183   char *com = abfd->tdata.trad_core_data->u.u_comm;
184   if (*com)
185     return com;
186   else
187 #endif
188     return 0;
189 }
190
191 /* ARGSUSED */
192 int
193 trad_unix_core_file_failing_signal (ignore_abfd)
194      bfd *ignore_abfd;
195 {
196   return -1;            /* FIXME, where is it? */
197 }
198
199 /* ARGSUSED */
200 boolean
201 trad_unix_core_file_matches_executable_p  (core_bfd, exec_bfd)
202      bfd *core_bfd, *exec_bfd;
203 {
204   return true;          /* FIXME, We have no way of telling at this point */
205 }
206 \f
207 /* No archive file support via this BFD */
208 #define trad_unix_openr_next_archived_file      bfd_generic_openr_next_archived_file
209 #define trad_unix_generic_stat_arch_elt         bfd_generic_stat_arch_elt
210 #define trad_unix_slurp_armap                   bfd_false
211 #define trad_unix_slurp_extended_name_table     bfd_true
212 #define trad_unix_write_armap                   (boolean (*) PARAMS     \
213     ((bfd *arch, unsigned int elength, struct orl *map, \
214       unsigned int orl_count, int stridx))) bfd_false
215 #define trad_unix_truncate_arname               bfd_dont_truncate_arname
216 #define aout_32_openr_next_archived_file        bfd_generic_openr_next_archived_file
217
218 #define trad_unix_close_and_cleanup             bfd_generic_close_and_cleanup
219 #define trad_unix_set_section_contents          (boolean (*) PARAMS     \
220         ((bfd *abfd, asection *section, PTR data, file_ptr offset,      \
221         bfd_size_type count))) bfd_false
222 #define trad_unix_get_section_contents          bfd_generic_get_section_contents
223 #define trad_unix_new_section_hook              (boolean (*) PARAMS     \
224         ((bfd *, sec_ptr))) bfd_true
225 #define trad_unix_get_symtab_upper_bound        bfd_0u
226 #define trad_unix_get_symtab                    (unsigned int (*) PARAMS \
227         ((bfd *, struct symbol_cache_entry **))) bfd_0u
228 #define trad_unix_get_reloc_upper_bound         (unsigned int (*) PARAMS \
229         ((bfd *, sec_ptr))) bfd_0u
230 #define trad_unix_canonicalize_reloc            (unsigned int (*) PARAMS \
231         ((bfd *, sec_ptr, arelent **, struct symbol_cache_entry**))) bfd_0u
232 #define trad_unix_make_empty_symbol             (struct symbol_cache_entry * \
233         (*) (bfd *)) bfd_false
234 #define trad_unix_print_symbol                  (void (*) PARAMS        \
235         ((bfd *, PTR, struct symbol_cache_entry  *,                     \
236         bfd_print_symbol_type))) bfd_false
237 #define trad_unix_get_lineno                    (alent * (*) PARAMS     \
238         ((bfd *, struct symbol_cache_entry *))) bfd_nullvoidptr
239 #define trad_unix_set_arch_mach                 (boolean (*) PARAMS     \
240         ((bfd *, enum bfd_architecture, unsigned long))) bfd_false
241 #define trad_unix_find_nearest_line             (boolean (*) PARAMS     \
242         ((bfd *abfd, struct sec  *section,                              \
243          struct symbol_cache_entry  **symbols,bfd_vma offset,           \
244          CONST char **file, CONST char **func, unsigned int *line))) bfd_false
245 #define trad_unix_sizeof_headers                (int (*) PARAMS \
246         ((bfd *, boolean))) bfd_0
247
248 #define trad_unix_bfd_debug_info_start          bfd_void
249 #define trad_unix_bfd_debug_info_end            bfd_void
250 #define trad_unix_bfd_debug_info_accumulate     (void (*) PARAMS        \
251         ((bfd *, struct sec *))) bfd_void
252 #define trad_unix_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
253 #define trad_unix_bfd_relax_section             bfd_generic_relax_section
254 #define trad_unix_bfd_seclet_link \
255   ((boolean (*) PARAMS ((bfd *, PTR, boolean))) bfd_false)
256
257 /* If somebody calls any byte-swapping routines, shoot them.  */
258 void
259 swap_abort()
260 {
261   abort(); /* This way doesn't require any declaration for ANSI to fuck up */
262 }
263 #define NO_GET  ((bfd_vma (*) PARAMS ((         bfd_byte *))) swap_abort )
264 #define NO_PUT  ((void    (*) PARAMS ((bfd_vma, bfd_byte *))) swap_abort )
265
266 bfd_target trad_core_vec =
267   {
268     "trad-core",
269     bfd_target_unknown_flavour,
270     true,                       /* target byte order */
271     true,                       /* target headers byte order */
272     (HAS_RELOC | EXEC_P |       /* object flags */
273      HAS_LINENO | HAS_DEBUG |
274      HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
275     (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
276     0,                                                     /* symbol prefix */
277     ' ',                                                   /* ar_pad_char */
278     16,                                                    /* ar_max_namelen */
279     3,                                                     /* minimum alignment power */
280     NO_GET, NO_PUT, NO_GET, NO_PUT, NO_GET, NO_PUT, /* data */
281     NO_GET, NO_PUT, NO_GET, NO_PUT, NO_GET, NO_PUT, /* hdrs */
282
283     {_bfd_dummy_target, _bfd_dummy_target,
284      _bfd_dummy_target, trad_unix_core_file_p},
285     {bfd_false, bfd_false,      /* bfd_create_object */
286      bfd_false, bfd_false},
287     {bfd_false, bfd_false,      /* bfd_write_contents */
288      bfd_false, bfd_false},
289     
290     JUMP_TABLE(trad_unix)
291 };