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