Switch sources over to use the GPL version 3
[deliverable/binutils-gdb.git] / bfd / trad-core.c
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, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
5 Written by John Gilmore of Cygnus Support.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
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 3 of the License, or
12 (at your option) any later version.
13
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.
18
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., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
23
24 #include "sysdep.h"
25 #include "bfd.h"
26 #include "libbfd.h"
27 #include "libaout.h" /* BFD a.out internal data structures */
28
29 #include <sys/param.h>
30 #ifdef HAVE_DIRENT_H
31 # include <dirent.h>
32 #else
33 # ifdef HAVE_SYS_NDIR_H
34 # include <sys/ndir.h>
35 # endif
36 # ifdef HAVE_SYS_DIR_H
37 # include <sys/dir.h>
38 # endif
39 # ifdef HAVE_NDIR_H
40 # include <ndir.h>
41 # endif
42 #endif
43 #include <signal.h>
44
45 #include <sys/user.h> /* After a.out.h */
46
47 #ifdef TRAD_HEADER
48 #include TRAD_HEADER
49 #endif
50
51 struct trad_core_struct
52 {
53 asection *data_section;
54 asection *stack_section;
55 asection *reg_section;
56 struct user u;
57 };
58
59 #define core_upage(bfd) (&((bfd)->tdata.trad_core_data->u))
60 #define core_datasec(bfd) ((bfd)->tdata.trad_core_data->data_section)
61 #define core_stacksec(bfd) ((bfd)->tdata.trad_core_data->stack_section)
62 #define core_regsec(bfd) ((bfd)->tdata.trad_core_data->reg_section)
63
64 /* forward declarations */
65
66 const bfd_target *trad_unix_core_file_p PARAMS ((bfd *abfd));
67 char * trad_unix_core_file_failing_command PARAMS ((bfd *abfd));
68 int trad_unix_core_file_failing_signal PARAMS ((bfd *abfd));
69 #define trad_unix_core_file_matches_executable_p generic_core_file_matches_executable_p
70 static void swap_abort PARAMS ((void));
71
72 /* Handle 4.2-style (and perhaps also sysV-style) core dump file. */
73
74 const bfd_target *
75 trad_unix_core_file_p (abfd)
76 bfd *abfd;
77
78 {
79 int val;
80 struct user u;
81 struct trad_core_struct *rawptr;
82 bfd_size_type amt;
83 flagword flags;
84
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
90
91 val = bfd_bread ((void *) &u, (bfd_size_type) sizeof u, abfd);
92 if (val != sizeof u)
93 {
94 /* Too small to be a core file */
95 bfd_set_error (bfd_error_wrong_format);
96 return 0;
97 }
98
99 /* Sanity check perhaps??? */
100 if (u.u_dsize > 0x1000000) /* Remember, it's in pages... */
101 {
102 bfd_set_error (bfd_error_wrong_format);
103 return 0;
104 }
105 if (u.u_ssize > 0x1000000)
106 {
107 bfd_set_error (bfd_error_wrong_format);
108 return 0;
109 }
110
111 /* Check that the size claimed is no greater than the file size. */
112 {
113 struct stat statbuf;
114
115 if (bfd_stat (abfd, &statbuf) < 0)
116 return 0;
117
118 if ((ufile_ptr) NBPG * (UPAGES + u.u_dsize
119 #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE
120 - u.u_tsize
121 #endif
122 + u.u_ssize)
123 > (ufile_ptr) statbuf.st_size)
124 {
125 bfd_set_error (bfd_error_wrong_format);
126 return 0;
127 }
128 #ifndef TRAD_CORE_ALLOW_ANY_EXTRA_SIZE
129 if (((ufile_ptr) NBPG * (UPAGES + u.u_dsize + u.u_ssize)
130 #ifdef TRAD_CORE_EXTRA_SIZE_ALLOWED
131 /* Some systems write the file too big. */
132 + TRAD_CORE_EXTRA_SIZE_ALLOWED
133 #endif
134 )
135 < (ufile_ptr) statbuf.st_size)
136 {
137 /* The file is too big. Maybe it's not a core file
138 or we otherwise have bad values for u_dsize and u_ssize). */
139 bfd_set_error (bfd_error_wrong_format);
140 return 0;
141 }
142 #endif
143 }
144
145 /* OK, we believe you. You're a core file (sure, sure). */
146
147 /* Allocate both the upage and the struct core_data at once, so
148 a single free() will free them both. */
149 amt = sizeof (struct trad_core_struct);
150 rawptr = (struct trad_core_struct *) bfd_zmalloc (amt);
151 if (rawptr == NULL)
152 return 0;
153
154 abfd->tdata.trad_core_data = rawptr;
155
156 rawptr->u = u; /*Copy the uarea into the tdata part of the bfd */
157
158 /* Create the sections. */
159
160 flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
161 core_stacksec(abfd) = bfd_make_section_anyway_with_flags (abfd, ".stack",
162 flags);
163 if (core_stacksec (abfd) == NULL)
164 goto fail;
165 core_datasec (abfd) = bfd_make_section_anyway_with_flags (abfd, ".data",
166 flags);
167 if (core_datasec (abfd) == NULL)
168 goto fail;
169 core_regsec (abfd) = bfd_make_section_anyway_with_flags (abfd, ".reg",
170 SEC_HAS_CONTENTS);
171 if (core_regsec (abfd) == NULL)
172 goto fail;
173
174 core_datasec (abfd)->size = NBPG * u.u_dsize
175 #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE
176 - NBPG * u.u_tsize
177 #endif
178 ;
179 core_stacksec (abfd)->size = NBPG * u.u_ssize;
180 core_regsec (abfd)->size = NBPG * UPAGES; /* Larger than sizeof struct u */
181
182 /* What a hack... we'd like to steal it from the exec file,
183 since the upage does not seem to provide it. FIXME. */
184 #ifdef HOST_DATA_START_ADDR
185 core_datasec (abfd)->vma = HOST_DATA_START_ADDR;
186 #else
187 core_datasec (abfd)->vma = HOST_TEXT_START_ADDR + (NBPG * u.u_tsize);
188 #endif
189
190 #ifdef HOST_STACK_START_ADDR
191 core_stacksec (abfd)->vma = HOST_STACK_START_ADDR;
192 #else
193 core_stacksec (abfd)->vma = HOST_STACK_END_ADDR - (NBPG * u.u_ssize);
194 #endif
195
196 /* This is tricky. As the "register section", we give them the entire
197 upage and stack. u.u_ar0 points to where "register 0" is stored.
198 There are two tricks with this, though. One is that the rest of the
199 registers might be at positive or negative (or both) displacements
200 from *u_ar0. The other is that u_ar0 is sometimes an absolute address
201 in kernel memory, and on other systems it is an offset from the beginning
202 of the `struct user'.
203
204 As a practical matter, we don't know where the registers actually are,
205 so we have to pass the whole area to GDB. We encode the value of u_ar0
206 by setting the .regs section up so that its virtual memory address
207 0 is at the place pointed to by u_ar0 (by setting the vma of the start
208 of the section to -u_ar0). GDB uses this info to locate the regs,
209 using minor trickery to get around the offset-or-absolute-addr problem. */
210 core_regsec (abfd)->vma = - (bfd_vma) (unsigned long) u.u_ar0;
211
212 core_datasec (abfd)->filepos = NBPG * UPAGES;
213 core_stacksec (abfd)->filepos = (NBPG * UPAGES) + NBPG * u.u_dsize
214 #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE
215 - NBPG * u.u_tsize
216 #endif
217 ;
218 core_regsec (abfd)->filepos = 0; /* Register segment is the upage */
219
220 /* Align to word at least */
221 core_stacksec (abfd)->alignment_power = 2;
222 core_datasec (abfd)->alignment_power = 2;
223 core_regsec (abfd)->alignment_power = 2;
224
225 return abfd->xvec;
226
227 fail:
228 bfd_release (abfd, abfd->tdata.any);
229 abfd->tdata.any = NULL;
230 bfd_section_list_clear (abfd);
231 return NULL;
232 }
233
234 char *
235 trad_unix_core_file_failing_command (abfd)
236 bfd *abfd;
237 {
238 #ifndef NO_CORE_COMMAND
239 char *com = abfd->tdata.trad_core_data->u.u_comm;
240 if (*com)
241 return com;
242 else
243 #endif
244 return 0;
245 }
246
247 int
248 trad_unix_core_file_failing_signal (ignore_abfd)
249 bfd *ignore_abfd ATTRIBUTE_UNUSED;
250 {
251 #ifdef TRAD_UNIX_CORE_FILE_FAILING_SIGNAL
252 return TRAD_UNIX_CORE_FILE_FAILING_SIGNAL(ignore_abfd);
253 #else
254 return -1; /* FIXME, where is it? */
255 #endif
256 }
257 \f
258 /* If somebody calls any byte-swapping routines, shoot them. */
259 static void
260 swap_abort ()
261 {
262 abort (); /* This way doesn't require any declaration for ANSI to fuck up */
263 }
264
265 #define NO_GET ((bfd_vma (*) (const void *)) swap_abort)
266 #define NO_PUT ((void (*) (bfd_vma, void *)) swap_abort)
267 #define NO_GETS ((bfd_signed_vma (*) (const void *)) swap_abort)
268 #define NO_GET64 ((bfd_uint64_t (*) (const void *)) swap_abort)
269 #define NO_PUT64 ((void (*) (bfd_uint64_t, void *)) swap_abort)
270 #define NO_GETS64 ((bfd_int64_t (*) (const void *)) swap_abort)
271
272 const bfd_target trad_core_vec =
273 {
274 "trad-core",
275 bfd_target_unknown_flavour,
276 BFD_ENDIAN_UNKNOWN, /* target byte order */
277 BFD_ENDIAN_UNKNOWN, /* target headers byte order */
278 (HAS_RELOC | EXEC_P | /* object flags */
279 HAS_LINENO | HAS_DEBUG |
280 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
281 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
282 0, /* symbol prefix */
283 ' ', /* ar_pad_char */
284 16, /* ar_max_namelen */
285 NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit data */
286 NO_GET, NO_GETS, NO_PUT, /* 32 bit data */
287 NO_GET, NO_GETS, NO_PUT, /* 16 bit data */
288 NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit hdrs */
289 NO_GET, NO_GETS, NO_PUT, /* 32 bit hdrs */
290 NO_GET, NO_GETS, NO_PUT, /* 16 bit hdrs */
291
292 { /* bfd_check_format */
293 _bfd_dummy_target, /* unknown format */
294 _bfd_dummy_target, /* object file */
295 _bfd_dummy_target, /* archive */
296 trad_unix_core_file_p /* a core file */
297 },
298 { /* bfd_set_format */
299 bfd_false, bfd_false,
300 bfd_false, bfd_false
301 },
302 { /* bfd_write_contents */
303 bfd_false, bfd_false,
304 bfd_false, bfd_false
305 },
306
307 BFD_JUMP_TABLE_GENERIC (_bfd_generic),
308 BFD_JUMP_TABLE_COPY (_bfd_generic),
309 BFD_JUMP_TABLE_CORE (trad_unix),
310 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
311 BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
312 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
313 BFD_JUMP_TABLE_WRITE (_bfd_generic),
314 BFD_JUMP_TABLE_LINK (_bfd_nolink),
315 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
316
317 NULL,
318
319 (PTR) 0 /* backend_data */
320 };
This page took 0.111141 seconds and 4 git commands to generate.