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