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,
3db64b00 3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
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
3e110533 21Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
252b5132 22
252b5132 23#include "sysdep.h"
3db64b00 24#include "bfd.h"
252b5132
RH
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));
69d246d9 68#define trad_unix_core_file_matches_executable_p generic_core_file_matches_executable_p
b34976b6 69static void swap_abort PARAMS ((void));
252b5132
RH
70
71/* Handle 4.2-style (and perhaps also sysV-style) core dump file. */
72
252b5132
RH
73const bfd_target *
74trad_unix_core_file_p (abfd)
75 bfd *abfd;
76
77{
78 int val;
79 struct user u;
80 struct trad_core_struct *rawptr;
dc810e39 81 bfd_size_type amt;
117ed4f8 82 flagword flags;
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 {
252b5132 112 struct stat statbuf;
33216455 113
b677b8c0 114 if (bfd_stat (abfd, &statbuf) < 0)
3dff57e8 115 return 0;
b677b8c0 116
74633dd0 117 if ((ufile_ptr) NBPG * (UPAGES + u.u_dsize
252b5132 118#ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE
74633dd0 119 - u.u_tsize
252b5132 120#endif
74633dd0
AM
121 + u.u_ssize)
122 > (ufile_ptr) statbuf.st_size)
252b5132 123 {
b35efbcd 124 bfd_set_error (bfd_error_wrong_format);
252b5132
RH
125 return 0;
126 }
127#ifndef TRAD_CORE_ALLOW_ANY_EXTRA_SIZE
74633dd0 128 if (((ufile_ptr) NBPG * (UPAGES + u.u_dsize + u.u_ssize)
252b5132
RH
129#ifdef TRAD_CORE_EXTRA_SIZE_ALLOWED
130 /* Some systems write the file too big. */
74633dd0 131 + TRAD_CORE_EXTRA_SIZE_ALLOWED
252b5132 132#endif
74633dd0
AM
133 )
134 < (ufile_ptr) statbuf.st_size)
252b5132
RH
135 {
136 /* The file is too big. Maybe it's not a core file
137 or we otherwise have bad values for u_dsize and u_ssize). */
138 bfd_set_error (bfd_error_wrong_format);
139 return 0;
140 }
141#endif
142 }
143
144 /* OK, we believe you. You're a core file (sure, sure). */
145
146 /* Allocate both the upage and the struct core_data at once, so
147 a single free() will free them both. */
dc810e39
AM
148 amt = sizeof (struct trad_core_struct);
149 rawptr = (struct trad_core_struct *) bfd_zmalloc (amt);
252b5132
RH
150 if (rawptr == NULL)
151 return 0;
5bff4f56 152
252b5132
RH
153 abfd->tdata.trad_core_data = rawptr;
154
155 rawptr->u = u; /*Copy the uarea into the tdata part of the bfd */
156
9e7b37b3 157 /* Create the sections. */
252b5132 158
117ed4f8
AM
159 flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
160 core_stacksec(abfd) = bfd_make_section_anyway_with_flags (abfd, ".stack",
161 flags);
252b5132 162 if (core_stacksec (abfd) == NULL)
9e7b37b3 163 goto fail;
117ed4f8
AM
164 core_datasec (abfd) = bfd_make_section_anyway_with_flags (abfd, ".data",
165 flags);
252b5132 166 if (core_datasec (abfd) == NULL)
9e7b37b3 167 goto fail;
117ed4f8
AM
168 core_regsec (abfd) = bfd_make_section_anyway_with_flags (abfd, ".reg",
169 SEC_HAS_CONTENTS);
252b5132 170 if (core_regsec (abfd) == NULL)
9e7b37b3 171 goto fail;
252b5132 172
eea6121a 173 core_datasec (abfd)->size = NBPG * u.u_dsize
252b5132
RH
174#ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE
175 - NBPG * u.u_tsize
176#endif
177 ;
eea6121a
AM
178 core_stacksec (abfd)->size = NBPG * u.u_ssize;
179 core_regsec (abfd)->size = NBPG * UPAGES; /* Larger than sizeof struct u */
252b5132
RH
180
181 /* What a hack... we'd like to steal it from the exec file,
182 since the upage does not seem to provide it. FIXME. */
183#ifdef HOST_DATA_START_ADDR
184 core_datasec (abfd)->vma = HOST_DATA_START_ADDR;
185#else
186 core_datasec (abfd)->vma = HOST_TEXT_START_ADDR + (NBPG * u.u_tsize);
187#endif
188
189#ifdef HOST_STACK_START_ADDR
190 core_stacksec (abfd)->vma = HOST_STACK_START_ADDR;
191#else
192 core_stacksec (abfd)->vma = HOST_STACK_END_ADDR - (NBPG * u.u_ssize);
193#endif
194
195 /* This is tricky. As the "register section", we give them the entire
196 upage and stack. u.u_ar0 points to where "register 0" is stored.
197 There are two tricks with this, though. One is that the rest of the
198 registers might be at positive or negative (or both) displacements
199 from *u_ar0. The other is that u_ar0 is sometimes an absolute address
200 in kernel memory, and on other systems it is an offset from the beginning
201 of the `struct user'.
5bff4f56 202
252b5132
RH
203 As a practical matter, we don't know where the registers actually are,
204 so we have to pass the whole area to GDB. We encode the value of u_ar0
205 by setting the .regs section up so that its virtual memory address
206 0 is at the place pointed to by u_ar0 (by setting the vma of the start
207 of the section to -u_ar0). GDB uses this info to locate the regs,
5bff4f56 208 using minor trickery to get around the offset-or-absolute-addr problem. */
d426c6b0 209 core_regsec (abfd)->vma = - (bfd_vma) (unsigned long) u.u_ar0;
252b5132
RH
210
211 core_datasec (abfd)->filepos = NBPG * UPAGES;
212 core_stacksec (abfd)->filepos = (NBPG * UPAGES) + NBPG * u.u_dsize
213#ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE
214 - NBPG * u.u_tsize
215#endif
216 ;
217 core_regsec (abfd)->filepos = 0; /* Register segment is the upage */
218
219 /* Align to word at least */
220 core_stacksec (abfd)->alignment_power = 2;
221 core_datasec (abfd)->alignment_power = 2;
222 core_regsec (abfd)->alignment_power = 2;
223
252b5132 224 return abfd->xvec;
9e7b37b3
AM
225
226 fail:
227 bfd_release (abfd, abfd->tdata.any);
228 abfd->tdata.any = NULL;
229 bfd_section_list_clear (abfd);
230 return NULL;
252b5132
RH
231}
232
233char *
234trad_unix_core_file_failing_command (abfd)
235 bfd *abfd;
236{
237#ifndef NO_CORE_COMMAND
238 char *com = abfd->tdata.trad_core_data->u.u_comm;
239 if (*com)
240 return com;
241 else
242#endif
243 return 0;
244}
245
252b5132
RH
246int
247trad_unix_core_file_failing_signal (ignore_abfd)
7442e600 248 bfd *ignore_abfd ATTRIBUTE_UNUSED;
252b5132
RH
249{
250#ifdef TRAD_UNIX_CORE_FILE_FAILING_SIGNAL
251 return TRAD_UNIX_CORE_FILE_FAILING_SIGNAL(ignore_abfd);
252#else
253 return -1; /* FIXME, where is it? */
254#endif
255}
252b5132
RH
256\f
257/* If somebody calls any byte-swapping routines, shoot them. */
258static void
5bff4f56 259swap_abort ()
252b5132 260{
5bff4f56 261 abort (); /* This way doesn't require any declaration for ANSI to fuck up */
252b5132 262}
8ce8c090 263
edeb6e24
AM
264#define NO_GET ((bfd_vma (*) (const void *)) swap_abort)
265#define NO_PUT ((void (*) (bfd_vma, void *)) swap_abort)
266#define NO_GETS ((bfd_signed_vma (*) (const void *)) swap_abort)
8ce8c090
AM
267#define NO_GET64 ((bfd_uint64_t (*) (const void *)) swap_abort)
268#define NO_PUT64 ((void (*) (bfd_uint64_t, void *)) swap_abort)
269#define NO_GETS64 ((bfd_int64_t (*) (const void *)) swap_abort)
252b5132
RH
270
271const bfd_target trad_core_vec =
272 {
273 "trad-core",
274 bfd_target_unknown_flavour,
275 BFD_ENDIAN_UNKNOWN, /* target byte order */
276 BFD_ENDIAN_UNKNOWN, /* target headers byte order */
277 (HAS_RELOC | EXEC_P | /* object flags */
278 HAS_LINENO | HAS_DEBUG |
279 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
280 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
281 0, /* symbol prefix */
282 ' ', /* ar_pad_char */
283 16, /* ar_max_namelen */
8ce8c090 284 NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit data */
edeb6e24
AM
285 NO_GET, NO_GETS, NO_PUT, /* 32 bit data */
286 NO_GET, NO_GETS, NO_PUT, /* 16 bit data */
8ce8c090 287 NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit hdrs */
edeb6e24
AM
288 NO_GET, NO_GETS, NO_PUT, /* 32 bit hdrs */
289 NO_GET, NO_GETS, NO_PUT, /* 16 bit hdrs */
252b5132
RH
290
291 { /* bfd_check_format */
8ce8c090
AM
292 _bfd_dummy_target, /* unknown format */
293 _bfd_dummy_target, /* object file */
294 _bfd_dummy_target, /* archive */
295 trad_unix_core_file_p /* a core file */
252b5132
RH
296 },
297 { /* bfd_set_format */
8ce8c090
AM
298 bfd_false, bfd_false,
299 bfd_false, bfd_false
252b5132
RH
300 },
301 { /* bfd_write_contents */
8ce8c090
AM
302 bfd_false, bfd_false,
303 bfd_false, bfd_false
252b5132 304 },
5bff4f56 305
8ce8c090
AM
306 BFD_JUMP_TABLE_GENERIC (_bfd_generic),
307 BFD_JUMP_TABLE_COPY (_bfd_generic),
308 BFD_JUMP_TABLE_CORE (trad_unix),
309 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
310 BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
311 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
312 BFD_JUMP_TABLE_WRITE (_bfd_generic),
313 BFD_JUMP_TABLE_LINK (_bfd_nolink),
314 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
252b5132 315
c3c89269 316 NULL,
5bff4f56 317
252b5132 318 (PTR) 0 /* backend_data */
8ce8c090 319 };
This page took 0.420869 seconds and 4 git commands to generate.