* readelf.c (byte_get_little_endian): Comment typo fix.
[binutils.git] / bfd / trad-core.c
blobe27cd35e8672e6dc9dcf027f1e148f3a0cb1b1bb
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, 2001, 2002
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;
83 bfd_size_type amt;
85 #ifdef TRAD_CORE_USER_OFFSET
86 /* If defined, this macro is the file position of the user struct. */
87 if (bfd_seek (abfd, (file_ptr) TRAD_CORE_USER_OFFSET, SEEK_SET) != 0)
88 return 0;
89 #endif
91 val = bfd_bread ((void *) &u, (bfd_size_type) sizeof u, abfd);
92 if (val != sizeof u)
94 /* Too small to be a core file */
95 bfd_set_error (bfd_error_wrong_format);
96 return 0;
99 /* Sanity check perhaps??? */
100 if (u.u_dsize > 0x1000000) /* Remember, it's in pages... */
102 bfd_set_error (bfd_error_wrong_format);
103 return 0;
105 if (u.u_ssize > 0x1000000)
107 bfd_set_error (bfd_error_wrong_format);
108 return 0;
111 /* Check that the size claimed is no greater than the file size. */
113 FILE *stream = bfd_cache_lookup (abfd);
114 struct stat statbuf;
115 if (stream == NULL)
116 return 0;
117 if (fstat (fileno (stream), &statbuf) < 0)
119 bfd_set_error (bfd_error_system_call);
120 return 0;
122 if ((unsigned long) (NBPG * (UPAGES + u.u_dsize
123 #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE
124 - u.u_tsize
125 #endif
126 + u.u_ssize))
127 > (unsigned long) statbuf.st_size)
129 bfd_set_error (bfd_error_wrong_format);
130 return 0;
132 #ifndef TRAD_CORE_ALLOW_ANY_EXTRA_SIZE
133 if ((unsigned long) (NBPG * (UPAGES + u.u_dsize + u.u_ssize)
134 #ifdef TRAD_CORE_EXTRA_SIZE_ALLOWED
135 /* Some systems write the file too big. */
136 + TRAD_CORE_EXTRA_SIZE_ALLOWED
137 #endif
139 < (unsigned long) statbuf.st_size)
141 /* The file is too big. Maybe it's not a core file
142 or we otherwise have bad values for u_dsize and u_ssize). */
143 bfd_set_error (bfd_error_wrong_format);
144 return 0;
146 #endif
149 /* OK, we believe you. You're a core file (sure, sure). */
151 /* Allocate both the upage and the struct core_data at once, so
152 a single free() will free them both. */
153 amt = sizeof (struct trad_core_struct);
154 rawptr = (struct trad_core_struct *) bfd_zmalloc (amt);
155 if (rawptr == NULL)
156 return 0;
158 abfd->tdata.trad_core_data = rawptr;
160 rawptr->u = u; /*Copy the uarea into the tdata part of the bfd */
162 /* Create the sections. */
164 core_stacksec(abfd) = bfd_make_section_anyway (abfd, ".stack");
165 if (core_stacksec (abfd) == NULL)
166 goto fail;
167 core_datasec (abfd) = bfd_make_section_anyway (abfd, ".data");
168 if (core_datasec (abfd) == NULL)
169 goto fail;
170 core_regsec (abfd) = bfd_make_section_anyway (abfd, ".reg");
171 if (core_regsec (abfd) == NULL)
172 goto fail;
174 core_stacksec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
175 core_datasec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
176 core_regsec (abfd)->flags = SEC_HAS_CONTENTS;
178 core_datasec (abfd)->_raw_size = NBPG * u.u_dsize
179 #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE
180 - NBPG * u.u_tsize
181 #endif
183 core_stacksec (abfd)->_raw_size = NBPG * u.u_ssize;
184 core_regsec (abfd)->_raw_size = NBPG * UPAGES; /* Larger than sizeof struct u */
186 /* What a hack... we'd like to steal it from the exec file,
187 since the upage does not seem to provide it. FIXME. */
188 #ifdef HOST_DATA_START_ADDR
189 core_datasec (abfd)->vma = HOST_DATA_START_ADDR;
190 #else
191 core_datasec (abfd)->vma = HOST_TEXT_START_ADDR + (NBPG * u.u_tsize);
192 #endif
194 #ifdef HOST_STACK_START_ADDR
195 core_stacksec (abfd)->vma = HOST_STACK_START_ADDR;
196 #else
197 core_stacksec (abfd)->vma = HOST_STACK_END_ADDR - (NBPG * u.u_ssize);
198 #endif
200 /* This is tricky. As the "register section", we give them the entire
201 upage and stack. u.u_ar0 points to where "register 0" is stored.
202 There are two tricks with this, though. One is that the rest of the
203 registers might be at positive or negative (or both) displacements
204 from *u_ar0. The other is that u_ar0 is sometimes an absolute address
205 in kernel memory, and on other systems it is an offset from the beginning
206 of the `struct user'.
208 As a practical matter, we don't know where the registers actually are,
209 so we have to pass the whole area to GDB. We encode the value of u_ar0
210 by setting the .regs section up so that its virtual memory address
211 0 is at the place pointed to by u_ar0 (by setting the vma of the start
212 of the section to -u_ar0). GDB uses this info to locate the regs,
213 using minor trickery to get around the offset-or-absolute-addr problem. */
214 core_regsec (abfd)->vma = - (bfd_vma) (unsigned long) u.u_ar0;
216 core_datasec (abfd)->filepos = NBPG * UPAGES;
217 core_stacksec (abfd)->filepos = (NBPG * UPAGES) + NBPG * u.u_dsize
218 #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE
219 - NBPG * u.u_tsize
220 #endif
222 core_regsec (abfd)->filepos = 0; /* Register segment is the upage */
224 /* Align to word at least */
225 core_stacksec (abfd)->alignment_power = 2;
226 core_datasec (abfd)->alignment_power = 2;
227 core_regsec (abfd)->alignment_power = 2;
229 return abfd->xvec;
231 fail:
232 bfd_release (abfd, abfd->tdata.any);
233 abfd->tdata.any = NULL;
234 bfd_section_list_clear (abfd);
235 return NULL;
238 char *
239 trad_unix_core_file_failing_command (abfd)
240 bfd *abfd;
242 #ifndef NO_CORE_COMMAND
243 char *com = abfd->tdata.trad_core_data->u.u_comm;
244 if (*com)
245 return com;
246 else
247 #endif
248 return 0;
251 /* ARGSUSED */
253 trad_unix_core_file_failing_signal (ignore_abfd)
254 bfd *ignore_abfd ATTRIBUTE_UNUSED;
256 #ifdef TRAD_UNIX_CORE_FILE_FAILING_SIGNAL
257 return TRAD_UNIX_CORE_FILE_FAILING_SIGNAL(ignore_abfd);
258 #else
259 return -1; /* FIXME, where is it? */
260 #endif
263 /* ARGSUSED */
264 boolean
265 trad_unix_core_file_matches_executable_p (core_bfd, exec_bfd)
266 bfd *core_bfd ATTRIBUTE_UNUSED;
267 bfd *exec_bfd ATTRIBUTE_UNUSED;
269 return true; /* FIXME, We have no way of telling at this point */
272 /* If somebody calls any byte-swapping routines, shoot them. */
273 static void
274 swap_abort ()
276 abort (); /* This way doesn't require any declaration for ANSI to fuck up */
278 #define NO_GET ((bfd_vma (*) PARAMS (( const bfd_byte *))) swap_abort )
279 #define NO_PUT ((void (*) PARAMS ((bfd_vma, bfd_byte *))) swap_abort )
280 #define NO_SIGNED_GET \
281 ((bfd_signed_vma (*) PARAMS ((const bfd_byte *))) swap_abort )
283 const bfd_target trad_core_vec =
285 "trad-core",
286 bfd_target_unknown_flavour,
287 BFD_ENDIAN_UNKNOWN, /* target byte order */
288 BFD_ENDIAN_UNKNOWN, /* target headers byte order */
289 (HAS_RELOC | EXEC_P | /* object flags */
290 HAS_LINENO | HAS_DEBUG |
291 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
292 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
293 0, /* symbol prefix */
294 ' ', /* ar_pad_char */
295 16, /* ar_max_namelen */
296 NO_GET, NO_SIGNED_GET, NO_PUT, /* 64 bit data */
297 NO_GET, NO_SIGNED_GET, NO_PUT, /* 32 bit data */
298 NO_GET, NO_SIGNED_GET, NO_PUT, /* 16 bit data */
299 NO_GET, NO_SIGNED_GET, NO_PUT, /* 64 bit hdrs */
300 NO_GET, NO_SIGNED_GET, NO_PUT, /* 32 bit hdrs */
301 NO_GET, NO_SIGNED_GET, NO_PUT, /* 16 bit hdrs */
303 { /* bfd_check_format */
304 _bfd_dummy_target, /* unknown format */
305 _bfd_dummy_target, /* object file */
306 _bfd_dummy_target, /* archive */
307 trad_unix_core_file_p /* a core file */
309 { /* bfd_set_format */
310 bfd_false, bfd_false,
311 bfd_false, bfd_false
313 { /* bfd_write_contents */
314 bfd_false, bfd_false,
315 bfd_false, bfd_false
318 BFD_JUMP_TABLE_GENERIC (_bfd_generic),
319 BFD_JUMP_TABLE_COPY (_bfd_generic),
320 BFD_JUMP_TABLE_CORE (trad_unix),
321 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
322 BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
323 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
324 BFD_JUMP_TABLE_WRITE (_bfd_generic),
325 BFD_JUMP_TABLE_LINK (_bfd_nolink),
326 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
328 NULL,
330 (PTR) 0 /* backend_data */