]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/gdb/bfd/trad-core.c
This commit was generated by cvs2svn to compensate for changes in r41227,
[FreeBSD/FreeBSD.git] / contrib / gdb / 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, 1994 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
24 #include "libaout.h"           /* BFD a.out internal data structures */
25
26 #include <stdio.h>
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <signal.h>
30
31 #include <sys/user.h>           /* After a.out.h  */
32
33 #ifdef TRAD_HEADER
34 #include TRAD_HEADER
35 #endif
36
37   struct trad_core_struct 
38     {
39       asection *data_section;
40       asection *stack_section;
41       asection *reg_section;
42       struct user u;
43     };
44
45 #define core_upage(bfd) (&((bfd)->tdata.trad_core_data->u))
46 #define core_datasec(bfd) ((bfd)->tdata.trad_core_data->data_section)
47 #define core_stacksec(bfd) ((bfd)->tdata.trad_core_data->stack_section)
48 #define core_regsec(bfd) ((bfd)->tdata.trad_core_data->reg_section)
49
50 /* forward declarations */
51
52 const bfd_target *trad_unix_core_file_p PARAMS ((bfd *abfd));
53 char *          trad_unix_core_file_failing_command PARAMS ((bfd *abfd));
54 int             trad_unix_core_file_failing_signal PARAMS ((bfd *abfd));
55 boolean         trad_unix_core_file_matches_executable_p
56                          PARAMS ((bfd *core_bfd, bfd *exec_bfd));
57
58 /* Handle 4.2-style (and perhaps also sysV-style) core dump file.  */
59
60 /* ARGSUSED */
61 const bfd_target *
62 trad_unix_core_file_p (abfd)
63      bfd *abfd;
64
65 {
66   int val;
67   struct user u;
68   struct trad_core_struct *rawptr;
69
70 #ifdef TRAD_CORE_USER_OFFSET
71   /* If defined, this macro is the file position of the user struct.  */
72   if (bfd_seek (abfd, TRAD_CORE_USER_OFFSET, SEEK_SET) != 0)
73     return 0;
74 #endif
75     
76   val = bfd_read ((void *)&u, 1, sizeof u, abfd);
77   if (val != sizeof u)
78     {
79       /* Too small to be a core file */
80       bfd_set_error (bfd_error_wrong_format);
81       return 0;
82     }
83
84   /* Sanity check perhaps??? */
85   if (u.u_dsize > 0x1000000)    /* Remember, it's in pages... */
86     {
87       bfd_set_error (bfd_error_wrong_format);
88       return 0;
89     }
90   if (u.u_ssize > 0x1000000)
91     {
92       bfd_set_error (bfd_error_wrong_format);
93       return 0;
94     }
95
96   /* Check that the size claimed is no greater than the file size.  */
97   {
98     FILE *stream = bfd_cache_lookup (abfd);
99     struct stat statbuf;
100     if (stream == NULL)
101       return 0;
102     if (fstat (fileno (stream), &statbuf) < 0)
103       {
104         bfd_set_error (bfd_error_system_call);
105         return 0;
106       }
107     if (NBPG * (UPAGES + u.u_dsize
108 #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE
109                 - u.u_tsize
110 #endif
111                 + u.u_ssize) > statbuf.st_size)
112       {
113         bfd_set_error (bfd_error_file_truncated);
114         return 0;
115       }
116 #ifndef TRAD_CORE_ALLOW_ANY_EXTRA_SIZE
117     if (NBPG * (UPAGES + u.u_dsize + u.u_ssize)
118 #ifdef TRAD_CORE_EXTRA_SIZE_ALLOWED
119         /* Some systems write the file too big.  */
120         + TRAD_CORE_EXTRA_SIZE_ALLOWED
121 #endif
122         < statbuf.st_size)
123       {
124         /* The file is too big.  Maybe it's not a core file
125            or we otherwise have bad values for u_dsize and u_ssize).  */
126         bfd_set_error (bfd_error_wrong_format);
127         return 0;
128       }
129 #endif
130   }
131
132   /* OK, we believe you.  You're a core file (sure, sure).  */
133
134   /* Allocate both the upage and the struct core_data at once, so
135      a single free() will free them both.  */
136   rawptr = (struct trad_core_struct *)
137                 bfd_zmalloc (sizeof (struct trad_core_struct));
138   if (rawptr == NULL)
139     return 0;
140   
141   abfd->tdata.trad_core_data = rawptr;
142
143   rawptr->u = u; /*Copy the uarea into the tdata part of the bfd */
144
145   /* Create the sections.  This is raunchy, but bfd_close wants to free
146      them separately.  */
147
148   core_stacksec(abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
149   if (core_stacksec (abfd) == NULL)
150     return NULL;
151   core_datasec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
152   if (core_datasec (abfd) == NULL)
153     return NULL;
154   core_regsec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
155   if (core_regsec (abfd) == NULL)
156     return NULL;
157
158   core_stacksec (abfd)->name = ".stack";
159   core_datasec (abfd)->name = ".data";
160   core_regsec (abfd)->name = ".reg";
161
162   core_stacksec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
163   core_datasec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
164   core_regsec (abfd)->flags = SEC_HAS_CONTENTS;
165
166   core_datasec (abfd)->_raw_size =  NBPG * u.u_dsize
167 #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE
168     - NBPG * u.u_tsize
169 #endif
170       ;
171   core_stacksec (abfd)->_raw_size = NBPG * u.u_ssize;
172   core_regsec (abfd)->_raw_size = NBPG * UPAGES; /* Larger than sizeof struct u */
173
174   /* What a hack... we'd like to steal it from the exec file,
175      since the upage does not seem to provide it.  FIXME.  */
176 #ifdef HOST_DATA_START_ADDR
177   core_datasec (abfd)->vma = HOST_DATA_START_ADDR;
178 #else
179   core_datasec (abfd)->vma = HOST_TEXT_START_ADDR + (NBPG * u.u_tsize);
180 #endif
181
182 #ifdef HOST_STACK_START_ADDR
183   core_stacksec (abfd)->vma = HOST_STACK_START_ADDR;
184 #else
185   core_stacksec (abfd)->vma = HOST_STACK_END_ADDR - (NBPG * u.u_ssize);
186 #endif
187
188   /* This is tricky.  As the "register section", we give them the entire
189      upage and stack.  u.u_ar0 points to where "register 0" is stored.
190      There are two tricks with this, though.  One is that the rest of the
191      registers might be at positive or negative (or both) displacements
192      from *u_ar0.  The other is that u_ar0 is sometimes an absolute address
193      in kernel memory, and on other systems it is an offset from the beginning
194      of the `struct user'.
195      
196      As a practical matter, we don't know where the registers actually are,
197      so we have to pass the whole area to GDB.  We encode the value of u_ar0
198      by setting the .regs section up so that its virtual memory address
199      0 is at the place pointed to by u_ar0 (by setting the vma of the start
200      of the section to -u_ar0).  GDB uses this info to locate the regs,
201      using minor trickery to get around the offset-or-absolute-addr problem. */
202   core_regsec (abfd)->vma = 0 - (bfd_vma) u.u_ar0;
203
204   core_datasec (abfd)->filepos = NBPG * UPAGES;
205   core_stacksec (abfd)->filepos = (NBPG * UPAGES) + NBPG * u.u_dsize
206 #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE
207     - NBPG * u.u_tsize
208 #endif
209       ;
210   core_regsec (abfd)->filepos = 0; /* Register segment is the upage */
211
212   /* Align to word at least */
213   core_stacksec (abfd)->alignment_power = 2;
214   core_datasec (abfd)->alignment_power = 2;
215   core_regsec (abfd)->alignment_power = 2;
216
217   abfd->sections = core_stacksec (abfd);
218   core_stacksec (abfd)->next = core_datasec (abfd);
219   core_datasec (abfd)->next = core_regsec (abfd);
220   abfd->section_count = 3;
221
222   return abfd->xvec;
223 }
224
225 char *
226 trad_unix_core_file_failing_command (abfd)
227      bfd *abfd;
228 {
229 #ifndef NO_CORE_COMMAND
230   char *com = abfd->tdata.trad_core_data->u.u_comm;
231   if (*com)
232     return com;
233   else
234 #endif
235     return 0;
236 }
237
238 /* ARGSUSED */
239 int
240 trad_unix_core_file_failing_signal (ignore_abfd)
241      bfd *ignore_abfd;
242 {
243 #ifdef TRAD_UNIX_CORE_FILE_FAILING_SIGNAL
244   return TRAD_UNIX_CORE_FILE_FAILING_SIGNAL(ignore_abfd);
245 #else
246   return -1;            /* FIXME, where is it? */
247 #endif
248 }
249
250 /* ARGSUSED */
251 boolean
252 trad_unix_core_file_matches_executable_p  (core_bfd, exec_bfd)
253      bfd *core_bfd, *exec_bfd;
254 {
255   return true;          /* FIXME, We have no way of telling at this point */
256 }
257 \f
258 /* If somebody calls any byte-swapping routines, shoot them.  */
259 void
260 swap_abort()
261 {
262   abort(); /* This way doesn't require any declaration for ANSI to fuck up */
263 }
264 #define NO_GET  ((bfd_vma (*) PARAMS ((   const bfd_byte *))) swap_abort )
265 #define NO_PUT  ((void    (*) PARAMS ((bfd_vma, bfd_byte *))) swap_abort )
266 #define NO_SIGNED_GET \
267   ((bfd_signed_vma (*) PARAMS ((const bfd_byte *))) swap_abort )
268
269 const bfd_target trad_core_vec =
270   {
271     "trad-core",
272     bfd_target_unknown_flavour,
273     BFD_ENDIAN_UNKNOWN,         /* target byte order */
274     BFD_ENDIAN_UNKNOWN,         /* target headers byte order */
275     (HAS_RELOC | EXEC_P |       /* object flags */
276      HAS_LINENO | HAS_DEBUG |
277      HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
278     (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
279     0,                                                     /* symbol prefix */
280     ' ',                                                   /* ar_pad_char */
281     16,                                                    /* ar_max_namelen */
282     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 64 bit data */
283     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 32 bit data */
284     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 16 bit data */
285     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 64 bit hdrs */
286     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 32 bit hdrs */
287     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 16 bit hdrs */
288
289     {                           /* bfd_check_format */
290      _bfd_dummy_target,         /* unknown format */
291      _bfd_dummy_target,         /* object file */
292      _bfd_dummy_target,         /* archive */
293      trad_unix_core_file_p      /* a core file */
294     },
295     {                           /* bfd_set_format */
296      bfd_false, bfd_false,
297      bfd_false, bfd_false
298     },
299     {                           /* bfd_write_contents */
300      bfd_false, bfd_false,
301      bfd_false, bfd_false
302     },
303     
304        BFD_JUMP_TABLE_GENERIC (_bfd_generic),
305        BFD_JUMP_TABLE_COPY (_bfd_generic),
306        BFD_JUMP_TABLE_CORE (trad_unix),
307        BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
308        BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
309        BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
310        BFD_JUMP_TABLE_WRITE (_bfd_generic),
311        BFD_JUMP_TABLE_LINK (_bfd_nolink),
312        BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
313
314     (PTR) 0                     /* backend_data */
315 };