]>
Commit | Line | Data |
---|---|---|
9712c6e2 | 1 | /* BFD back end for traditional Unix core files (U-area and raw sections) |
b7577823 | 2 | Copyright 1988, 1989, 1991, 1992, 1993, 1994 Free Software Foundation, Inc. |
9712c6e2 SG |
3 | Written by John Gilmore of Cygnus Support. |
4 | ||
5 | This file is part of BFD, the Binary File Descriptor library. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
b7577823 | 19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
6a469027 | 20 | |
ff37ea55 | 21 | #include "bfd.h" |
637942e4 | 22 | #include "sysdep.h" |
ff37ea55 | 23 | #include "libbfd.h" |
359f1dee | 24 | #include "libaout.h" /* BFD a.out internal data structures */ |
ff37ea55 | 25 | |
637942e4 | 26 | #include <stdio.h> |
ff37ea55 JG |
27 | #include <sys/types.h> |
28 | #include <sys/param.h> | |
29 | #include <sys/dir.h> | |
30 | #include <signal.h> | |
ff37ea55 JG |
31 | |
32 | #include <sys/user.h> /* After a.out.h */ | |
ff37ea55 | 33 | |
b7577823 ILT |
34 | #ifdef TRAD_HEADER |
35 | #include TRAD_HEADER | |
36 | #endif | |
ff37ea55 | 37 | |
1f29e30b JG |
38 | struct trad_core_struct |
39 | { | |
40 | asection *data_section; | |
41 | asection *stack_section; | |
42 | asection *reg_section; | |
43 | struct user u; | |
44 | } *rawptr; | |
45 | ||
46 | #define core_upage(bfd) (&((bfd)->tdata.trad_core_data->u)) | |
47 | #define core_datasec(bfd) ((bfd)->tdata.trad_core_data->data_section) | |
48 | #define core_stacksec(bfd) ((bfd)->tdata.trad_core_data->stack_section) | |
49 | #define core_regsec(bfd) ((bfd)->tdata.trad_core_data->reg_section) | |
50 | ||
51 | /* forward declarations */ | |
ff37ea55 | 52 | |
b7577823 | 53 | const bfd_target *trad_unix_core_file_p PARAMS ((bfd *abfd)); |
1f29e30b JG |
54 | char * trad_unix_core_file_failing_command PARAMS ((bfd *abfd)); |
55 | int trad_unix_core_file_failing_signal PARAMS ((bfd *abfd)); | |
56 | boolean trad_unix_core_file_matches_executable_p | |
57 | PARAMS ((bfd *core_bfd, bfd *exec_bfd)); | |
6a469027 | 58 | |
637942e4 JG |
59 | /* Handle 4.2-style (and perhaps also sysV-style) core dump file. */ |
60 | ||
7ed4093a | 61 | /* ARGSUSED */ |
b7577823 | 62 | const bfd_target * |
ff37ea55 JG |
63 | trad_unix_core_file_p (abfd) |
64 | bfd *abfd; | |
1f29e30b | 65 | |
ff37ea55 | 66 | { |
ff37ea55 | 67 | int val; |
ff37ea55 | 68 | struct user u; |
ff37ea55 | 69 | |
4c3721d5 ILT |
70 | #ifdef TRAD_CORE_USER_OFFSET |
71 | /* If defined, this macro is the file position of the user struct. */ | |
baf200d4 | 72 | if (bfd_seek (abfd, TRAD_CORE_USER_OFFSET, SEEK_SET) != 0) |
4c3721d5 ILT |
73 | return 0; |
74 | #endif | |
75 | ||
ff37ea55 JG |
76 | val = bfd_read ((void *)&u, 1, sizeof u, abfd); |
77 | if (val != sizeof u) | |
31568a6f JK |
78 | { |
79 | /* Too small to be a core file */ | |
baf200d4 | 80 | bfd_set_error (bfd_error_wrong_format); |
31568a6f JK |
81 | return 0; |
82 | } | |
ff37ea55 JG |
83 | |
84 | /* Sanity check perhaps??? */ | |
85 | if (u.u_dsize > 0x1000000) /* Remember, it's in pages... */ | |
31568a6f | 86 | { |
baf200d4 | 87 | bfd_set_error (bfd_error_wrong_format); |
31568a6f JK |
88 | return 0; |
89 | } | |
ff37ea55 | 90 | if (u.u_ssize > 0x1000000) |
31568a6f | 91 | { |
baf200d4 | 92 | bfd_set_error (bfd_error_wrong_format); |
31568a6f JK |
93 | return 0; |
94 | } | |
95 | ||
96 | /* Check that the size claimed is no greater than the file size. */ | |
97 | { | |
98 | FILE *stream = bfd_cache_lookup (abfd); | |
99 | struct stat statbuf; | |
100 | if (stream == NULL) | |
101 | return 0; | |
102 | if (fstat (fileno (stream), &statbuf) < 0) | |
103 | { | |
baf200d4 | 104 | bfd_set_error (bfd_error_system_call); |
31568a6f JK |
105 | return 0; |
106 | } | |
baf200d4 JK |
107 | if (NBPG * (UPAGES + u.u_dsize |
108 | #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE | |
109 | - u.u_tsize | |
110 | #endif | |
111 | + u.u_ssize) > statbuf.st_size) | |
31568a6f | 112 | { |
baf200d4 | 113 | bfd_set_error (bfd_error_file_truncated); |
31568a6f JK |
114 | return 0; |
115 | } | |
4c3721d5 | 116 | #ifndef TRAD_CORE_ALLOW_ANY_EXTRA_SIZE |
4d09e8ac JK |
117 | if (NBPG * (UPAGES + u.u_dsize + u.u_ssize) |
118 | #ifdef TRAD_CORE_EXTRA_SIZE_ALLOWED | |
119 | /* Some systems write the file too big. */ | |
120 | + TRAD_CORE_EXTRA_SIZE_ALLOWED | |
121 | #endif | |
122 | < statbuf.st_size) | |
31568a6f JK |
123 | { |
124 | /* The file is too big. Maybe it's not a core file | |
125 | or we otherwise have bad values for u_dsize and u_ssize). */ | |
baf200d4 | 126 | bfd_set_error (bfd_error_wrong_format); |
31568a6f JK |
127 | return 0; |
128 | } | |
4c3721d5 | 129 | #endif |
31568a6f | 130 | } |
ff37ea55 JG |
131 | |
132 | /* OK, we believe you. You're a core file (sure, sure). */ | |
133 | ||
134 | /* Allocate both the upage and the struct core_data at once, so | |
135 | a single free() will free them both. */ | |
1f29e30b | 136 | rawptr = (struct trad_core_struct *) |
326e32d7 | 137 | bfd_zmalloc (sizeof (struct trad_core_struct)); |
ff37ea55 | 138 | if (rawptr == NULL) { |
baf200d4 | 139 | bfd_set_error (bfd_error_no_memory); |
ff37ea55 JG |
140 | return 0; |
141 | } | |
142 | ||
1f29e30b JG |
143 | abfd->tdata.trad_core_data = rawptr; |
144 | ||
145 | rawptr->u = u; /*Copy the uarea into the tdata part of the bfd */ | |
ff37ea55 | 146 | |
637942e4 JG |
147 | /* Create the sections. This is raunchy, but bfd_close wants to free |
148 | them separately. */ | |
1f29e30b | 149 | |
baf200d4 | 150 | core_stacksec(abfd) = (asection *) bfd_zmalloc (sizeof (asection)); |
ff37ea55 | 151 | if (core_stacksec (abfd) == NULL) { |
1f29e30b | 152 | loser: |
baf200d4 | 153 | bfd_set_error (bfd_error_no_memory); |
ff37ea55 JG |
154 | free ((void *)rawptr); |
155 | return 0; | |
156 | } | |
baf200d4 | 157 | core_datasec (abfd) = (asection *) bfd_zmalloc (sizeof (asection)); |
ff37ea55 | 158 | if (core_datasec (abfd) == NULL) { |
1f29e30b | 159 | loser1: |
ff37ea55 JG |
160 | free ((void *)core_stacksec (abfd)); |
161 | goto loser; | |
162 | } | |
baf200d4 | 163 | core_regsec (abfd) = (asection *) bfd_zmalloc (sizeof (asection)); |
ff37ea55 | 164 | if (core_regsec (abfd) == NULL) { |
ff37ea55 JG |
165 | free ((void *)core_datasec (abfd)); |
166 | goto loser1; | |
167 | } | |
168 | ||
169 | core_stacksec (abfd)->name = ".stack"; | |
170 | core_datasec (abfd)->name = ".data"; | |
171 | core_regsec (abfd)->name = ".reg"; | |
172 | ||
6a469027 JG |
173 | core_stacksec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS; |
174 | core_datasec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS; | |
b7577823 | 175 | core_regsec (abfd)->flags = SEC_HAS_CONTENTS; |
ff37ea55 | 176 | |
baf200d4 JK |
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 | ; | |
1f29e30b JG |
182 | core_stacksec (abfd)->_raw_size = NBPG * u.u_ssize; |
183 | core_regsec (abfd)->_raw_size = NBPG * UPAGES; /* Larger than sizeof struct u */ | |
ff37ea55 JG |
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. */ | |
9712c6e2 SG |
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 | |
4c3721d5 ILT |
192 | |
193 | #ifdef HOST_STACK_START_ADDR | |
194 | core_stacksec (abfd)->vma = HOST_STACK_START_ADDR; | |
195 | #else | |
9712c6e2 | 196 | core_stacksec (abfd)->vma = HOST_STACK_END_ADDR - (NBPG * u.u_ssize); |
4c3721d5 ILT |
197 | #endif |
198 | ||
637942e4 JG |
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'. | |
1f29e30b | 206 | |
637942e4 JG |
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, | |
212 | using minor trickery to get around the offset-or-absolute-addr problem. */ | |
213 | core_regsec (abfd)->vma = 0 - (int) u.u_ar0; | |
ff37ea55 JG |
214 | |
215 | core_datasec (abfd)->filepos = NBPG * UPAGES; | |
baf200d4 JK |
216 | core_stacksec (abfd)->filepos = (NBPG * UPAGES) + NBPG * u.u_dsize |
217 | #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE | |
218 | - NBPG * u.u_tsize | |
4c3721d5 | 219 | #endif |
baf200d4 | 220 | ; |
1f29e30b | 221 | core_regsec (abfd)->filepos = 0; /* Register segment is the upage */ |
ff37ea55 JG |
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 | ||
228 | abfd->sections = core_stacksec (abfd); | |
229 | core_stacksec (abfd)->next = core_datasec (abfd); | |
230 | core_datasec (abfd)->next = core_regsec (abfd); | |
231 | abfd->section_count = 3; | |
232 | ||
233 | return abfd->xvec; | |
ff37ea55 JG |
234 | } |
235 | ||
236 | char * | |
237 | trad_unix_core_file_failing_command (abfd) | |
238 | bfd *abfd; | |
239 | { | |
1f29e30b JG |
240 | #ifndef NO_CORE_COMMAND |
241 | char *com = abfd->tdata.trad_core_data->u.u_comm; | |
242 | if (*com) | |
243 | return com; | |
ff37ea55 | 244 | else |
1f29e30b | 245 | #endif |
ff37ea55 JG |
246 | return 0; |
247 | } | |
248 | ||
7ed4093a | 249 | /* ARGSUSED */ |
ff37ea55 | 250 | int |
7ed4093a SC |
251 | trad_unix_core_file_failing_signal (ignore_abfd) |
252 | bfd *ignore_abfd; | |
ff37ea55 | 253 | { |
31568a6f JK |
254 | #ifdef TRAD_UNIX_CORE_FILE_FAILING_SIGNAL |
255 | return TRAD_UNIX_CORE_FILE_FAILING_SIGNAL(ignore_abfd); | |
256 | #else | |
ff37ea55 | 257 | return -1; /* FIXME, where is it? */ |
31568a6f | 258 | #endif |
ff37ea55 JG |
259 | } |
260 | ||
7ed4093a | 261 | /* ARGSUSED */ |
ff37ea55 JG |
262 | boolean |
263 | trad_unix_core_file_matches_executable_p (core_bfd, exec_bfd) | |
264 | bfd *core_bfd, *exec_bfd; | |
265 | { | |
266 | return true; /* FIXME, We have no way of telling at this point */ | |
267 | } | |
6a469027 | 268 | \f |
637942e4 JG |
269 | /* If somebody calls any byte-swapping routines, shoot them. */ |
270 | void | |
271 | swap_abort() | |
272 | { | |
273 | abort(); /* This way doesn't require any declaration for ANSI to fuck up */ | |
274 | } | |
baf200d4 | 275 | #define NO_GET ((bfd_vma (*) PARAMS (( const bfd_byte *))) swap_abort ) |
1f29e30b | 276 | #define NO_PUT ((void (*) PARAMS ((bfd_vma, bfd_byte *))) swap_abort ) |
baf200d4 JK |
277 | #define NO_SIGNED_GET \ |
278 | ((bfd_signed_vma (*) PARAMS ((const bfd_byte *))) swap_abort ) | |
6a469027 | 279 | |
b7577823 | 280 | const bfd_target trad_core_vec = |
6a469027 | 281 | { |
637942e4 | 282 | "trad-core", |
6a469027 JG |
283 | bfd_target_unknown_flavour, |
284 | true, /* target byte order */ | |
285 | true, /* target headers byte order */ | |
286 | (HAS_RELOC | EXEC_P | /* object flags */ | |
287 | HAS_LINENO | HAS_DEBUG | | |
4c3721d5 | 288 | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED), |
6a469027 | 289 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */ |
0dc1bc8b | 290 | 0, /* symbol prefix */ |
6a469027 JG |
291 | ' ', /* ar_pad_char */ |
292 | 16, /* ar_max_namelen */ | |
293 | 3, /* minimum alignment power */ | |
4d09e8ac JK |
294 | NO_GET, NO_SIGNED_GET, NO_PUT, /* 64 bit data */ |
295 | NO_GET, NO_SIGNED_GET, NO_PUT, /* 32 bit data */ | |
296 | NO_GET, NO_SIGNED_GET, NO_PUT, /* 16 bit data */ | |
297 | NO_GET, NO_SIGNED_GET, NO_PUT, /* 64 bit hdrs */ | |
298 | NO_GET, NO_SIGNED_GET, NO_PUT, /* 32 bit hdrs */ | |
299 | NO_GET, NO_SIGNED_GET, NO_PUT, /* 16 bit hdrs */ | |
31568a6f JK |
300 | |
301 | { /* bfd_check_format */ | |
302 | _bfd_dummy_target, /* unknown format */ | |
303 | _bfd_dummy_target, /* object file */ | |
304 | _bfd_dummy_target, /* archive */ | |
305 | trad_unix_core_file_p /* a core file */ | |
306 | }, | |
307 | { /* bfd_set_format */ | |
308 | bfd_false, bfd_false, | |
309 | bfd_false, bfd_false | |
310 | }, | |
311 | { /* bfd_write_contents */ | |
312 | bfd_false, bfd_false, | |
313 | bfd_false, bfd_false | |
314 | }, | |
6a469027 | 315 | |
6812b607 ILT |
316 | BFD_JUMP_TABLE_GENERIC (_bfd_generic), |
317 | BFD_JUMP_TABLE_COPY (_bfd_generic), | |
318 | BFD_JUMP_TABLE_CORE (trad_unix), | |
319 | BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive), | |
320 | BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols), | |
321 | BFD_JUMP_TABLE_RELOCS (_bfd_norelocs), | |
322 | BFD_JUMP_TABLE_WRITE (_bfd_generic), | |
323 | BFD_JUMP_TABLE_LINK (_bfd_nolink), | |
b7577823 | 324 | BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), |
6812b607 | 325 | |
31568a6f | 326 | (PTR) 0 /* backend_data */ |
6a469027 | 327 | }; |