2001-03-26 Andreas Jaeger <aj@suse.de>
[binutils.git] / bfd / trad-core.c
blob872ad54ab858c119ab5ee3219c8ee05548732008
1 /* BFD back end for traditional Unix core files (U-area and raw sections)
2 Copyright 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999,
3 2000
4 Free Software Foundation, Inc.
5 Written by John Gilmore of Cygnus Support.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "libbfd.h"
26 #include "libaout.h" /* BFD a.out internal data structures */
28 #include <sys/param.h>
29 #ifdef HAVE_DIRENT_H
30 # include <dirent.h>
31 #else
32 # ifdef HAVE_SYS_NDIR_H
33 # include <sys/ndir.h>
34 # endif
35 # ifdef HAVE_SYS_DIR_H
36 # include <sys/dir.h>
37 # endif
38 # ifdef HAVE_NDIR_H
39 # include <ndir.h>
40 # endif
41 #endif
42 #include <signal.h>
44 #include <sys/user.h> /* After a.out.h */
46 #ifdef TRAD_HEADER
47 #include TRAD_HEADER
48 #endif
50 struct trad_core_struct
52 asection *data_section;
53 asection *stack_section;
54 asection *reg_section;
55 struct user u;
58 #define core_upage(bfd) (&((bfd)->tdata.trad_core_data->u))
59 #define core_datasec(bfd) ((bfd)->tdata.trad_core_data->data_section)
60 #define core_stacksec(bfd) ((bfd)->tdata.trad_core_data->stack_section)
61 #define core_regsec(bfd) ((bfd)->tdata.trad_core_data->reg_section)
63 /* forward declarations */
65 const bfd_target *trad_unix_core_file_p PARAMS ((bfd *abfd));
66 char * trad_unix_core_file_failing_command PARAMS ((bfd *abfd));
67 int trad_unix_core_file_failing_signal PARAMS ((bfd *abfd));
68 boolean trad_unix_core_file_matches_executable_p
69 PARAMS ((bfd *core_bfd, bfd *exec_bfd));
70 static void swap_abort PARAMS ((void));
72 /* Handle 4.2-style (and perhaps also sysV-style) core dump file. */
74 /* ARGSUSED */
75 const bfd_target *
76 trad_unix_core_file_p (abfd)
77 bfd *abfd;
80 int val;
81 struct user u;
82 struct trad_core_struct *rawptr;
84 #ifdef TRAD_CORE_USER_OFFSET
85 /* If defined, this macro is the file position of the user struct. */
86 if (bfd_seek (abfd, TRAD_CORE_USER_OFFSET, SEEK_SET) != 0)
87 return 0;
88 #endif
90 val = bfd_read ((void *)&u, 1, sizeof u, abfd);
91 if (val != sizeof u)
93 /* Too small to be a core file */
94 bfd_set_error (bfd_error_wrong_format);
95 return 0;
98 /* Sanity check perhaps??? */
99 if (u.u_dsize > 0x1000000) /* Remember, it's in pages... */
101 bfd_set_error (bfd_error_wrong_format);
102 return 0;
104 if (u.u_ssize > 0x1000000)
106 bfd_set_error (bfd_error_wrong_format);
107 return 0;
110 /* Check that the size claimed is no greater than the file size. */
112 FILE *stream = bfd_cache_lookup (abfd);
113 struct stat statbuf;
114 if (stream == NULL)
115 return 0;
116 if (fstat (fileno (stream), &statbuf) < 0)
118 bfd_set_error (bfd_error_system_call);
119 return 0;
121 if ((unsigned long) (NBPG * (UPAGES + u.u_dsize
122 #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE
123 - u.u_tsize
124 #endif
125 + u.u_ssize))
126 > (unsigned long) statbuf.st_size)
128 bfd_set_error (bfd_error_wrong_format);
129 return 0;
131 #ifndef TRAD_CORE_ALLOW_ANY_EXTRA_SIZE
132 if ((unsigned long) (NBPG * (UPAGES + u.u_dsize + u.u_ssize)
133 #ifdef TRAD_CORE_EXTRA_SIZE_ALLOWED
134 /* Some systems write the file too big. */
135 + TRAD_CORE_EXTRA_SIZE_ALLOWED
136 #endif
138 < (unsigned long) statbuf.st_size)
140 /* The file is too big. Maybe it's not a core file
141 or we otherwise have bad values for u_dsize and u_ssize). */
142 bfd_set_error (bfd_error_wrong_format);
143 return 0;
145 #endif
148 /* OK, we believe you. You're a core file (sure, sure). */
150 /* Allocate both the upage and the struct core_data at once, so
151 a single free() will free them both. */
152 rawptr = (struct trad_core_struct *)
153 bfd_zmalloc (sizeof (struct trad_core_struct));
154 if (rawptr == NULL)
155 return 0;
157 abfd->tdata.trad_core_data = rawptr;
159 rawptr->u = u; /*Copy the uarea into the tdata part of the bfd */
161 /* Create the sections. This is raunchy, but bfd_close wants to free
162 them separately. */
164 core_stacksec(abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
165 if (core_stacksec (abfd) == NULL)
166 return NULL;
167 core_datasec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
168 if (core_datasec (abfd) == NULL)
169 return NULL;
170 core_regsec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
171 if (core_regsec (abfd) == NULL)
172 return NULL;
174 core_stacksec (abfd)->name = ".stack";
175 core_datasec (abfd)->name = ".data";
176 core_regsec (abfd)->name = ".reg";
178 core_stacksec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
179 core_datasec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
180 core_regsec (abfd)->flags = SEC_HAS_CONTENTS;
182 core_datasec (abfd)->_raw_size = NBPG * u.u_dsize
183 #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE
184 - NBPG * u.u_tsize
185 #endif
187 core_stacksec (abfd)->_raw_size = NBPG * u.u_ssize;
188 core_regsec (abfd)->_raw_size = NBPG * UPAGES; /* Larger than sizeof struct u */
190 /* What a hack... we'd like to steal it from the exec file,
191 since the upage does not seem to provide it. FIXME. */
192 #ifdef HOST_DATA_START_ADDR
193 core_datasec (abfd)->vma = HOST_DATA_START_ADDR;
194 #else
195 core_datasec (abfd)->vma = HOST_TEXT_START_ADDR + (NBPG * u.u_tsize);
196 #endif
198 #ifdef HOST_STACK_START_ADDR
199 core_stacksec (abfd)->vma = HOST_STACK_START_ADDR;
200 #else
201 core_stacksec (abfd)->vma = HOST_STACK_END_ADDR - (NBPG * u.u_ssize);
202 #endif
204 /* This is tricky. As the "register section", we give them the entire
205 upage and stack. u.u_ar0 points to where "register 0" is stored.
206 There are two tricks with this, though. One is that the rest of the
207 registers might be at positive or negative (or both) displacements
208 from *u_ar0. The other is that u_ar0 is sometimes an absolute address
209 in kernel memory, and on other systems it is an offset from the beginning
210 of the `struct user'.
212 As a practical matter, we don't know where the registers actually are,
213 so we have to pass the whole area to GDB. We encode the value of u_ar0
214 by setting the .regs section up so that its virtual memory address
215 0 is at the place pointed to by u_ar0 (by setting the vma of the start
216 of the section to -u_ar0). GDB uses this info to locate the regs,
217 using minor trickery to get around the offset-or-absolute-addr problem. */
218 core_regsec (abfd)->vma = - (bfd_vma) u.u_ar0;
220 core_datasec (abfd)->filepos = NBPG * UPAGES;
221 core_stacksec (abfd)->filepos = (NBPG * UPAGES) + NBPG * u.u_dsize
222 #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE
223 - NBPG * u.u_tsize
224 #endif
226 core_regsec (abfd)->filepos = 0; /* Register segment is the upage */
228 /* Align to word at least */
229 core_stacksec (abfd)->alignment_power = 2;
230 core_datasec (abfd)->alignment_power = 2;
231 core_regsec (abfd)->alignment_power = 2;
233 abfd->sections = core_stacksec (abfd);
234 core_stacksec (abfd)->next = core_datasec (abfd);
235 core_datasec (abfd)->next = core_regsec (abfd);
236 abfd->section_count = 3;
238 return abfd->xvec;
241 char *
242 trad_unix_core_file_failing_command (abfd)
243 bfd *abfd;
245 #ifndef NO_CORE_COMMAND
246 char *com = abfd->tdata.trad_core_data->u.u_comm;
247 if (*com)
248 return com;
249 else
250 #endif
251 return 0;
254 /* ARGSUSED */
256 trad_unix_core_file_failing_signal (ignore_abfd)
257 bfd *ignore_abfd ATTRIBUTE_UNUSED;
259 #ifdef TRAD_UNIX_CORE_FILE_FAILING_SIGNAL
260 return TRAD_UNIX_CORE_FILE_FAILING_SIGNAL(ignore_abfd);
261 #else
262 return -1; /* FIXME, where is it? */
263 #endif
266 /* ARGSUSED */
267 boolean
268 trad_unix_core_file_matches_executable_p (core_bfd, exec_bfd)
269 bfd *core_bfd ATTRIBUTE_UNUSED;
270 bfd *exec_bfd ATTRIBUTE_UNUSED;
272 return true; /* FIXME, We have no way of telling at this point */
275 /* If somebody calls any byte-swapping routines, shoot them. */
276 static void
277 swap_abort ()
279 abort (); /* This way doesn't require any declaration for ANSI to fuck up */
281 #define NO_GET ((bfd_vma (*) PARAMS (( const bfd_byte *))) swap_abort )
282 #define NO_PUT ((void (*) PARAMS ((bfd_vma, bfd_byte *))) swap_abort )
283 #define NO_SIGNED_GET \
284 ((bfd_signed_vma (*) PARAMS ((const bfd_byte *))) swap_abort )
286 const bfd_target trad_core_vec =
288 "trad-core",
289 bfd_target_unknown_flavour,
290 BFD_ENDIAN_UNKNOWN, /* target byte order */
291 BFD_ENDIAN_UNKNOWN, /* target headers byte order */
292 (HAS_RELOC | EXEC_P | /* object flags */
293 HAS_LINENO | HAS_DEBUG |
294 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
295 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
296 0, /* symbol prefix */
297 ' ', /* ar_pad_char */
298 16, /* ar_max_namelen */
299 NO_GET, NO_SIGNED_GET, NO_PUT, /* 64 bit data */
300 NO_GET, NO_SIGNED_GET, NO_PUT, /* 32 bit data */
301 NO_GET, NO_SIGNED_GET, NO_PUT, /* 16 bit data */
302 NO_GET, NO_SIGNED_GET, NO_PUT, /* 64 bit hdrs */
303 NO_GET, NO_SIGNED_GET, NO_PUT, /* 32 bit hdrs */
304 NO_GET, NO_SIGNED_GET, NO_PUT, /* 16 bit hdrs */
306 { /* bfd_check_format */
307 _bfd_dummy_target, /* unknown format */
308 _bfd_dummy_target, /* object file */
309 _bfd_dummy_target, /* archive */
310 trad_unix_core_file_p /* a core file */
312 { /* bfd_set_format */
313 bfd_false, bfd_false,
314 bfd_false, bfd_false
316 { /* bfd_write_contents */
317 bfd_false, bfd_false,
318 bfd_false, bfd_false
321 BFD_JUMP_TABLE_GENERIC (_bfd_generic),
322 BFD_JUMP_TABLE_COPY (_bfd_generic),
323 BFD_JUMP_TABLE_CORE (trad_unix),
324 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
325 BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
326 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
327 BFD_JUMP_TABLE_WRITE (_bfd_generic),
328 BFD_JUMP_TABLE_LINK (_bfd_nolink),
329 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
331 NULL,
333 (PTR) 0 /* backend_data */