* Makefile.in: Rename XDEPFILES.
[external/binutils.git] / bfd / trad-core.c
1 /* BFD back end for traditional Unix core files (U-area and raw sections)
2    Copyright (C) 1988, 1989, 1991 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=-DHOST_SYS=WHATEVER_SYS -DTRAD_CORE
25         HDEPFILES=trad-core.o
26
27  */
28
29 #include <sysdep.h>
30 #include "bfd.h"
31 #include <stdio.h>
32 #include "libbfd.h"
33 #include "libaout.h"           /* BFD a.out internal data structures */
34
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/dir.h>
38 #include <signal.h>
39 #include <machine/reg.h>
40
41 #include <sys/user.h>           /* After a.out.h  */
42 #include <sys/file.h>
43
44 #include <errno.h>
45
46 /* These are stored in the bfd's tdata */
47 struct core_data {
48   struct user *upage;             /* core file header */
49   asection *data_section;
50   asection *stack_section;
51   asection *reg_section;
52 };
53
54 #define core_hdr(bfd) (((struct core_data *) (bfd->tdata))->hdr)
55 #define core_upage(bfd) (((struct core_data *) ((bfd)->tdata))->upage)
56 #define core_datasec(bfd) (((struct core_data *) ((bfd)->tdata))->data_section)
57 #define core_stacksec(bfd) (((struct core_data*)((bfd)->tdata))->stack_section)
58 #define core_regsec(bfd) (((struct core_data *) ((bfd)->tdata))->reg_section)
59
60 /* ARGSUSED */
61 bfd_target *
62 trad_unix_core_file_p (abfd)
63      bfd *abfd;
64 {
65 #if HOST_SYS == SUN4_SYS
66   return 0;
67 #else
68   int val;
69   char *rawptr;
70   struct user u;
71   unsigned int reg_offset, fp_reg_offset;
72
73   /* 4.2-style (and perhaps also sysV-style) core dump file.  */
74
75   val = bfd_read ((void *)&u, 1, sizeof u, abfd);
76   if (val != sizeof u)
77     return 0;                   /* Too small to be a core file */
78
79   /* Sanity check perhaps??? */
80   if (u.u_dsize > 0x1000000)    /* Remember, it's in pages... */
81     return 0;
82   if (u.u_ssize > 0x1000000)
83     return 0;
84   /* Check that the size claimed is no greater than the file size. FIXME. */
85
86   /* OK, we believe you.  You're a core file (sure, sure).  */
87
88   /* Allocate both the upage and the struct core_data at once, so
89      a single free() will free them both.  */
90   rawptr = (char *)zalloc (sizeof (u) + sizeof (struct core_data));
91   if (rawptr == NULL) {
92     bfd_error = no_memory;
93     return 0;
94   }
95   
96   set_tdata (abfd, (struct core_data *)rawptr);
97   core_upage (abfd) = (struct user *)(rawptr + sizeof (struct core_data));
98   *core_upage (abfd) = u;               /* Save that upage! */
99
100   /* create the sections.  This is raunchy, but bfd_close wants to reclaim
101      them */
102   core_stacksec (abfd) = (asection *) zalloc (sizeof (asection));
103   if (core_stacksec (abfd) == NULL) {
104 loser:
105     bfd_error = no_memory;
106     free ((void *)rawptr);
107     return 0;
108   }
109   core_datasec (abfd) = (asection *) zalloc (sizeof (asection));
110   if (core_datasec (abfd) == NULL) {
111 loser1:
112     free ((void *)core_stacksec (abfd));
113     goto loser;
114   }
115   core_regsec (abfd) = (asection *) zalloc (sizeof (asection));
116   if (core_regsec (abfd) == NULL) {
117 loser2:
118     free ((void *)core_datasec (abfd));
119     goto loser1;
120   }
121
122   core_stacksec (abfd)->name = ".stack";
123   core_datasec (abfd)->name = ".data";
124   core_regsec (abfd)->name = ".reg";
125
126   core_stacksec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
127   core_datasec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
128   core_regsec (abfd)->flags = SEC_ALLOC + SEC_HAS_CONTENTS;
129
130   core_datasec (abfd)->size =  NBPG * u.u_dsize;
131   core_stacksec (abfd)->size = NBPG * u.u_ssize;
132   core_regsec (abfd)->size = NBPG * UPAGES;  /* Larger than sizeof struct u */
133
134   /* What a hack... we'd like to steal it from the exec file,
135      since the upage does not seem to provide it.  FIXME.  */
136 #ifdef HOST_DATA_START_ADDR
137   core_datasec (abfd)->vma = HOST_DATA_START_ADDR;
138 #else
139   core_datasec (abfd)->vma = HOST_TEXT_START_ADDR + (NBPG * u.u_tsize);
140 #endif
141   core_stacksec (abfd)->vma = HOST_STACK_END_ADDR - (NBPG * u.u_ssize);
142   core_regsec (abfd)->vma = -1;
143
144   core_datasec (abfd)->filepos = NBPG * UPAGES;
145   core_stacksec (abfd)->filepos = (NBPG * UPAGES) + NBPG * u.u_dsize;
146   core_regsec (abfd)->filepos = 0;      /* Register segment is the upage */
147
148   /* Align to word at least */
149   core_stacksec (abfd)->alignment_power = 2;
150   core_datasec (abfd)->alignment_power = 2;
151   core_regsec (abfd)->alignment_power = 2;
152
153   abfd->sections = core_stacksec (abfd);
154   core_stacksec (abfd)->next = core_datasec (abfd);
155   core_datasec (abfd)->next = core_regsec (abfd);
156   abfd->section_count = 3;
157
158   return abfd->xvec;
159 #endif
160 }
161
162 char *
163 trad_unix_core_file_failing_command (abfd)
164      bfd *abfd;
165 {
166   if (*core_upage (abfd)->u_comm)
167     return core_upage (abfd)->u_comm;
168   else
169     return 0;
170 }
171
172 /* ARGSUSED */
173 int
174 trad_unix_core_file_failing_signal (ignore_abfd)
175      bfd *ignore_abfd;
176 {
177   return -1;            /* FIXME, where is it? */
178 }
179
180 /* ARGSUSED */
181 boolean
182 trad_unix_core_file_matches_executable_p  (core_bfd, exec_bfd)
183      bfd *core_bfd, *exec_bfd;
184 {
185   return true;          /* FIXME, We have no way of telling at this point */
186 }
187 \f
188 /* No archive file support via this BFD */
189 #define trad_unix_openr_next_archived_file      bfd_generic_openr_next_archived_file
190 #define trad_unix_generic_stat_arch_elt         bfd_generic_stat_arch_elt
191 #define trad_unix_slurp_armap                   bfd_false
192 #define trad_unix_slurp_extended_name_table     bfd_true
193 #define trad_unix_write_armap                   (PROTO (boolean, (*),   \
194      (bfd *arch, unsigned int elength, struct orl *map, int orl_count,  \
195       int stridx))) bfd_false
196 #define trad_unix_truncate_arname               bfd_dont_truncate_arname
197 #define aout_32_openr_next_archived_file        bfd_generic_openr_next_archived_file
198
199 #define trad_unix_close_and_cleanup             bfd_generic_close_and_cleanup
200 #define trad_unix_set_section_contents          (PROTO(boolean, (*),    \
201          (bfd *abfd, asection *section, PTR data, file_ptr offset,      \
202          bfd_size_type count))) bfd_false
203 #define trad_unix_get_section_contents          bfd_generic_get_section_contents
204 #define trad_unix_new_section_hook              (PROTO (boolean, (*),   \
205         (bfd *, sec_ptr))) bfd_true
206 #define trad_unix_get_symtab_upper_bound        bfd_0u
207 #define trad_unix_get_symtab                    (PROTO (unsigned int, (*), \
208         (bfd *, struct symbol_cache_entry **))) bfd_0u
209 #define trad_unix_get_reloc_upper_bound         (PROTO (unsigned int, (*), \
210         (bfd *, sec_ptr))) bfd_0u
211 #define trad_unix_canonicalize_reloc            (PROTO (unsigned int, (*), \
212         (bfd *, sec_ptr, arelent **, struct symbol_cache_entry**))) bfd_0u
213 #define trad_unix_make_empty_symbol             (PROTO (                \
214         struct symbol_cache_entry *, (*), (bfd *))) bfd_false
215 #define trad_unix_print_symbol                  (PROTO (void, (*),      \
216         (bfd *, PTR, struct symbol_cache_entry  *,                      \
217          bfd_print_symbol_type))) bfd_false
218 #define trad_unix_get_lineno                    (PROTO (alent *, (*),   \
219         (bfd *, struct symbol_cache_entry *))) bfd_nullvoidptr
220 #define trad_unix_set_arch_mach                 (PROTO (boolean, (*),   \
221         (bfd *, enum bfd_architecture, unsigned long))) bfd_false
222 #define trad_unix_find_nearest_line             (PROTO (boolean, (*),   \
223         (bfd *abfd, struct sec  *section,                               \
224          struct symbol_cache_entry  **symbols,bfd_vma offset,           \
225          CONST char **file, CONST char **func, unsigned int *line))) bfd_false
226 #define trad_unix_sizeof_headers                (PROTO (int, (*),       \
227         (bfd *, boolean))) bfd_0
228
229 #define trad_unix_bfd_debug_info_start          bfd_void
230 #define trad_unix_bfd_debug_info_end            bfd_void
231 #define trad_unix_bfd_debug_info_accumulate     (PROTO (void, (*),      \
232         (bfd *, struct sec *))) bfd_void
233
234
235 bfd_target trad_core_big_vec =
236   {
237     "trad-core-big",
238     bfd_target_unknown_flavour,
239     true,                       /* target byte order */
240     true,                       /* target headers byte order */
241     (HAS_RELOC | EXEC_P |       /* object flags */
242      HAS_LINENO | HAS_DEBUG |
243      HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
244     (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
245     ' ',                                                   /* ar_pad_char */
246     16,                                                    /* ar_max_namelen */
247     3,                                                     /* minimum alignment power */
248     _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* data */
249     _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* hdrs */
250
251       {_bfd_dummy_target, _bfd_dummy_target,
252        _bfd_dummy_target, trad_unix_core_file_p},
253       {bfd_false, bfd_false,    /* bfd_create_object */
254        bfd_false, bfd_false},
255       {bfd_false, bfd_false,    /* bfd_write_contents */
256        bfd_false, bfd_false},
257     
258     JUMP_TABLE(trad_unix)
259 };
260
261 bfd_target trad_core_little_vec =
262 {
263   "trad-core-little",
264   bfd_target_unknown_flavour,
265   false,                        /* target byte order */
266   false,                        /* target headers byte order */
267   (HAS_RELOC | EXEC_P | /* object flags */
268    HAS_LINENO | HAS_DEBUG |
269    HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
270   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
271   ' ',                          /* ar_pad_char */
272   16,                           /* ar_max_namelen */
273   3,                            /* minimum alignment power */
274   _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putb16,
275   _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16,
276   
277   {_bfd_dummy_target, _bfd_dummy_target,
278    _bfd_dummy_target, trad_unix_core_file_p},
279   {bfd_false, bfd_false,        /* bfd_create_object */
280    bfd_false, bfd_false},
281   {bfd_false, bfd_false,        /* bfd_write_contents */
282    bfd_false, bfd_false},
283   
284   JUMP_TABLE(trad_unix)
285 };