bfd/
[deliverable/binutils-gdb.git] / bfd / trad-core.c
CommitLineData
252b5132 1/* BFD back end for traditional Unix core files (U-area and raw sections)
7898deda 2 Copyright 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999,
edeb6e24 3 2000, 2001, 2002, 2003, 2004
252b5132
RH
4 Free Software Foundation, Inc.
5 Written by John Gilmore of Cygnus Support.
6
7This file is part of BFD, the Binary File Descriptor library.
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22
23#include "bfd.h"
24#include "sysdep.h"
25#include "libbfd.h"
26#include "libaout.h" /* BFD a.out internal data structures */
27
28#include <sys/param.h>
12df4d3c
AC
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
252b5132
RH
42#include <signal.h>
43
44#include <sys/user.h> /* After a.out.h */
45
46#ifdef TRAD_HEADER
47#include TRAD_HEADER
48#endif
49
b34976b6
AM
50struct trad_core_struct
51{
52 asection *data_section;
53 asection *stack_section;
54 asection *reg_section;
55 struct user u;
56};
252b5132
RH
57
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)
62
63/* forward declarations */
64
65const bfd_target *trad_unix_core_file_p PARAMS ((bfd *abfd));
b34976b6
AM
66char * trad_unix_core_file_failing_command PARAMS ((bfd *abfd));
67int trad_unix_core_file_failing_signal PARAMS ((bfd *abfd));
68bfd_boolean trad_unix_core_file_matches_executable_p
69 PARAMS ((bfd *core_bfd, bfd *exec_bfd));
70static void swap_abort PARAMS ((void));
252b5132
RH
71
72/* Handle 4.2-style (and perhaps also sysV-style) core dump file. */
73
252b5132
RH
74const bfd_target *
75trad_unix_core_file_p (abfd)
76 bfd *abfd;
77
78{
79 int val;
80 struct user u;
81 struct trad_core_struct *rawptr;
dc810e39 82 bfd_size_type amt;
252b5132
RH
83
84#ifdef TRAD_CORE_USER_OFFSET
85 /* If defined, this macro is the file position of the user struct. */
dc810e39 86 if (bfd_seek (abfd, (file_ptr) TRAD_CORE_USER_OFFSET, SEEK_SET) != 0)
252b5132
RH
87 return 0;
88#endif
5bff4f56 89
dc810e39 90 val = bfd_bread ((void *) &u, (bfd_size_type) sizeof u, abfd);
252b5132
RH
91 if (val != sizeof u)
92 {
93 /* Too small to be a core file */
94 bfd_set_error (bfd_error_wrong_format);
95 return 0;
96 }
97
98 /* Sanity check perhaps??? */
5bff4f56 99 if (u.u_dsize > 0x1000000) /* Remember, it's in pages... */
252b5132
RH
100 {
101 bfd_set_error (bfd_error_wrong_format);
102 return 0;
103 }
104 if (u.u_ssize > 0x1000000)
105 {
106 bfd_set_error (bfd_error_wrong_format);
107 return 0;
108 }
109
110 /* Check that the size claimed is no greater than the file size. */
111 {
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)
117 {
118 bfd_set_error (bfd_error_system_call);
119 return 0;
120 }
7442e600 121 if ((unsigned long) (NBPG * (UPAGES + u.u_dsize
252b5132 122#ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE
7442e600 123 - u.u_tsize
252b5132 124#endif
7442e600
ILT
125 + u.u_ssize))
126 > (unsigned long) statbuf.st_size)
252b5132 127 {
b35efbcd 128 bfd_set_error (bfd_error_wrong_format);
252b5132
RH
129 return 0;
130 }
131#ifndef TRAD_CORE_ALLOW_ANY_EXTRA_SIZE
7442e600 132 if ((unsigned long) (NBPG * (UPAGES + u.u_dsize + u.u_ssize)
252b5132
RH
133#ifdef TRAD_CORE_EXTRA_SIZE_ALLOWED
134 /* Some systems write the file too big. */
7442e600 135 + TRAD_CORE_EXTRA_SIZE_ALLOWED
252b5132 136#endif
7442e600
ILT
137 )
138 < (unsigned long) statbuf.st_size)
252b5132
RH
139 {
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;
144 }
145#endif
146 }
147
148 /* OK, we believe you. You're a core file (sure, sure). */
149
150 /* Allocate both the upage and the struct core_data at once, so
151 a single free() will free them both. */
dc810e39
AM
152 amt = sizeof (struct trad_core_struct);
153 rawptr = (struct trad_core_struct *) bfd_zmalloc (amt);
252b5132
RH
154 if (rawptr == NULL)
155 return 0;
5bff4f56 156
252b5132
RH
157 abfd->tdata.trad_core_data = rawptr;
158
159 rawptr->u = u; /*Copy the uarea into the tdata part of the bfd */
160
9e7b37b3 161 /* Create the sections. */
252b5132 162
9e7b37b3 163 core_stacksec(abfd) = bfd_make_section_anyway (abfd, ".stack");
252b5132 164 if (core_stacksec (abfd) == NULL)
9e7b37b3
AM
165 goto fail;
166 core_datasec (abfd) = bfd_make_section_anyway (abfd, ".data");
252b5132 167 if (core_datasec (abfd) == NULL)
9e7b37b3
AM
168 goto fail;
169 core_regsec (abfd) = bfd_make_section_anyway (abfd, ".reg");
252b5132 170 if (core_regsec (abfd) == NULL)
9e7b37b3 171 goto fail;
252b5132
RH
172
173 core_stacksec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
174 core_datasec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
175 core_regsec (abfd)->flags = SEC_HAS_CONTENTS;
176
177 core_datasec (abfd)->_raw_size = NBPG * u.u_dsize
178#ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE
179 - NBPG * u.u_tsize
180#endif
181 ;
182 core_stacksec (abfd)->_raw_size = NBPG * u.u_ssize;
183 core_regsec (abfd)->_raw_size = NBPG * UPAGES; /* Larger than sizeof struct u */
184
185 /* What a hack... we'd like to steal it from the exec file,
186 since the upage does not seem to provide it. FIXME. */
187#ifdef HOST_DATA_START_ADDR
188 core_datasec (abfd)->vma = HOST_DATA_START_ADDR;
189#else
190 core_datasec (abfd)->vma = HOST_TEXT_START_ADDR + (NBPG * u.u_tsize);
191#endif
192
193#ifdef HOST_STACK_START_ADDR
194 core_stacksec (abfd)->vma = HOST_STACK_START_ADDR;
195#else
196 core_stacksec (abfd)->vma = HOST_STACK_END_ADDR - (NBPG * u.u_ssize);
197#endif
198
199 /* This is tricky. As the "register section", we give them the entire
200 upage and stack. u.u_ar0 points to where "register 0" is stored.
201 There are two tricks with this, though. One is that the rest of the
202 registers might be at positive or negative (or both) displacements
203 from *u_ar0. The other is that u_ar0 is sometimes an absolute address
204 in kernel memory, and on other systems it is an offset from the beginning
205 of the `struct user'.
5bff4f56 206
252b5132
RH
207 As a practical matter, we don't know where the registers actually are,
208 so we have to pass the whole area to GDB. We encode the value of u_ar0
209 by setting the .regs section up so that its virtual memory address
210 0 is at the place pointed to by u_ar0 (by setting the vma of the start
211 of the section to -u_ar0). GDB uses this info to locate the regs,
5bff4f56 212 using minor trickery to get around the offset-or-absolute-addr problem. */
d426c6b0 213 core_regsec (abfd)->vma = - (bfd_vma) (unsigned long) u.u_ar0;
252b5132
RH
214
215 core_datasec (abfd)->filepos = NBPG * UPAGES;
216 core_stacksec (abfd)->filepos = (NBPG * UPAGES) + NBPG * u.u_dsize
217#ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE
218 - NBPG * u.u_tsize
219#endif
220 ;
221 core_regsec (abfd)->filepos = 0; /* Register segment is the upage */
222
223 /* Align to word at least */
224 core_stacksec (abfd)->alignment_power = 2;
225 core_datasec (abfd)->alignment_power = 2;
226 core_regsec (abfd)->alignment_power = 2;
227
252b5132 228 return abfd->xvec;
9e7b37b3
AM
229
230 fail:
231 bfd_release (abfd, abfd->tdata.any);
232 abfd->tdata.any = NULL;
233 bfd_section_list_clear (abfd);
234 return NULL;
252b5132
RH
235}
236
237char *
238trad_unix_core_file_failing_command (abfd)
239 bfd *abfd;
240{
241#ifndef NO_CORE_COMMAND
242 char *com = abfd->tdata.trad_core_data->u.u_comm;
243 if (*com)
244 return com;
245 else
246#endif
247 return 0;
248}
249
252b5132
RH
250int
251trad_unix_core_file_failing_signal (ignore_abfd)
7442e600 252 bfd *ignore_abfd ATTRIBUTE_UNUSED;
252b5132
RH
253{
254#ifdef TRAD_UNIX_CORE_FILE_FAILING_SIGNAL
255 return TRAD_UNIX_CORE_FILE_FAILING_SIGNAL(ignore_abfd);
256#else
257 return -1; /* FIXME, where is it? */
258#endif
259}
260
b34976b6 261bfd_boolean
252b5132 262trad_unix_core_file_matches_executable_p (core_bfd, exec_bfd)
7442e600
ILT
263 bfd *core_bfd ATTRIBUTE_UNUSED;
264 bfd *exec_bfd ATTRIBUTE_UNUSED;
252b5132 265{
b34976b6 266 return TRUE; /* FIXME, We have no way of telling at this point */
252b5132
RH
267}
268\f
269/* If somebody calls any byte-swapping routines, shoot them. */
270static void
5bff4f56 271swap_abort ()
252b5132 272{
5bff4f56 273 abort (); /* This way doesn't require any declaration for ANSI to fuck up */
252b5132 274}
8ce8c090 275
edeb6e24
AM
276#define NO_GET ((bfd_vma (*) (const void *)) swap_abort)
277#define NO_PUT ((void (*) (bfd_vma, void *)) swap_abort)
278#define NO_GETS ((bfd_signed_vma (*) (const void *)) swap_abort)
8ce8c090
AM
279#define NO_GET64 ((bfd_uint64_t (*) (const void *)) swap_abort)
280#define NO_PUT64 ((void (*) (bfd_uint64_t, void *)) swap_abort)
281#define NO_GETS64 ((bfd_int64_t (*) (const void *)) swap_abort)
252b5132
RH
282
283const bfd_target trad_core_vec =
284 {
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 */
8ce8c090 296 NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit data */
edeb6e24
AM
297 NO_GET, NO_GETS, NO_PUT, /* 32 bit data */
298 NO_GET, NO_GETS, NO_PUT, /* 16 bit data */
8ce8c090 299 NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit hdrs */
edeb6e24
AM
300 NO_GET, NO_GETS, NO_PUT, /* 32 bit hdrs */
301 NO_GET, NO_GETS, NO_PUT, /* 16 bit hdrs */
252b5132
RH
302
303 { /* bfd_check_format */
8ce8c090
AM
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 */
252b5132
RH
308 },
309 { /* bfd_set_format */
8ce8c090
AM
310 bfd_false, bfd_false,
311 bfd_false, bfd_false
252b5132
RH
312 },
313 { /* bfd_write_contents */
8ce8c090
AM
314 bfd_false, bfd_false,
315 bfd_false, bfd_false
252b5132 316 },
5bff4f56 317
8ce8c090
AM
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),
252b5132 327
c3c89269 328 NULL,
5bff4f56 329
252b5132 330 (PTR) 0 /* backend_data */
8ce8c090 331 };
This page took 0.272406 seconds and 4 git commands to generate.