]>
Commit | Line | Data |
---|---|---|
9712c6e2 | 1 | /* BFD back end for traditional Unix core files (U-area and raw sections) |
0dc1bc8b | 2 | Copyright 1988, 1989, 1991, 1992, 1993 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 | |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
ff37ea55 | 20 | |
6a469027 JG |
21 | /* To use this file on a particular host, configure the host with these |
22 | parameters in the config/h-HOST file: | |
23 | ||
637942e4 | 24 | HDEFINES=-DTRAD_CORE |
6a469027 JG |
25 | HDEPFILES=trad-core.o |
26 | ||
27 | */ | |
28 | ||
ff37ea55 | 29 | #include "bfd.h" |
637942e4 | 30 | #include "sysdep.h" |
ff37ea55 | 31 | #include "libbfd.h" |
359f1dee | 32 | #include "libaout.h" /* BFD a.out internal data structures */ |
ff37ea55 | 33 | |
637942e4 | 34 | #include <stdio.h> |
ff37ea55 JG |
35 | #include <sys/types.h> |
36 | #include <sys/param.h> | |
37 | #include <sys/dir.h> | |
38 | #include <signal.h> | |
ff37ea55 JG |
39 | |
40 | #include <sys/user.h> /* After a.out.h */ | |
4d09e8ac JK |
41 | #if 0 |
42 | /* file.h is included by std-host.h. So we better not try to include it | |
43 | twice; on some systems (dpx2) it is not protected against multiple | |
44 | inclusion. I have checked that all the hosts which use this file | |
45 | include sys/file.h in the hosts file. */ | |
ff37ea55 | 46 | #include <sys/file.h> |
4d09e8ac | 47 | #endif |
ff37ea55 JG |
48 | |
49 | #include <errno.h> | |
50 | ||
1f29e30b JG |
51 | struct trad_core_struct |
52 | { | |
53 | asection *data_section; | |
54 | asection *stack_section; | |
55 | asection *reg_section; | |
56 | struct user u; | |
57 | } *rawptr; | |
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 */ | |
ff37ea55 | 65 | |
1f29e30b JG |
66 | 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 | boolean trad_unix_core_file_matches_executable_p | |
70 | PARAMS ((bfd *core_bfd, bfd *exec_bfd)); | |
6a469027 | 71 | |
637942e4 JG |
72 | /* Handle 4.2-style (and perhaps also sysV-style) core dump file. */ |
73 | ||
7ed4093a | 74 | /* ARGSUSED */ |
ff37ea55 JG |
75 | bfd_target * |
76 | trad_unix_core_file_p (abfd) | |
77 | bfd *abfd; | |
1f29e30b | 78 | |
ff37ea55 | 79 | { |
ff37ea55 | 80 | int val; |
ff37ea55 | 81 | struct user u; |
ff37ea55 | 82 | |
4c3721d5 ILT |
83 | #ifdef TRAD_CORE_USER_OFFSET |
84 | /* If defined, this macro is the file position of the user struct. */ | |
baf200d4 | 85 | if (bfd_seek (abfd, TRAD_CORE_USER_OFFSET, SEEK_SET) != 0) |
4c3721d5 ILT |
86 | return 0; |
87 | #endif | |
88 | ||
ff37ea55 JG |
89 | val = bfd_read ((void *)&u, 1, sizeof u, abfd); |
90 | if (val != sizeof u) | |
31568a6f JK |
91 | { |
92 | /* Too small to be a core file */ | |
baf200d4 | 93 | bfd_set_error (bfd_error_wrong_format); |
31568a6f JK |
94 | return 0; |
95 | } | |
ff37ea55 JG |
96 | |
97 | /* Sanity check perhaps??? */ | |
98 | if (u.u_dsize > 0x1000000) /* Remember, it's in pages... */ | |
31568a6f | 99 | { |
baf200d4 | 100 | bfd_set_error (bfd_error_wrong_format); |
31568a6f JK |
101 | return 0; |
102 | } | |
ff37ea55 | 103 | if (u.u_ssize > 0x1000000) |
31568a6f | 104 | { |
baf200d4 | 105 | bfd_set_error (bfd_error_wrong_format); |
31568a6f JK |
106 | return 0; |
107 | } | |
108 | ||
109 | /* Check that the size claimed is no greater than the file size. */ | |
110 | { | |
111 | FILE *stream = bfd_cache_lookup (abfd); | |
112 | struct stat statbuf; | |
113 | if (stream == NULL) | |
114 | return 0; | |
115 | if (fstat (fileno (stream), &statbuf) < 0) | |
116 | { | |
baf200d4 | 117 | bfd_set_error (bfd_error_system_call); |
31568a6f JK |
118 | return 0; |
119 | } | |
baf200d4 JK |
120 | if (NBPG * (UPAGES + u.u_dsize |
121 | #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE | |
122 | - u.u_tsize | |
123 | #endif | |
124 | + u.u_ssize) > statbuf.st_size) | |
31568a6f | 125 | { |
baf200d4 | 126 | bfd_set_error (bfd_error_file_truncated); |
31568a6f JK |
127 | return 0; |
128 | } | |
4c3721d5 | 129 | #ifndef TRAD_CORE_ALLOW_ANY_EXTRA_SIZE |
4d09e8ac JK |
130 | if (NBPG * (UPAGES + u.u_dsize + u.u_ssize) |
131 | #ifdef TRAD_CORE_EXTRA_SIZE_ALLOWED | |
132 | /* Some systems write the file too big. */ | |
133 | + TRAD_CORE_EXTRA_SIZE_ALLOWED | |
134 | #endif | |
135 | < statbuf.st_size) | |
31568a6f JK |
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). */ | |
baf200d4 | 139 | bfd_set_error (bfd_error_wrong_format); |
31568a6f JK |
140 | return 0; |
141 | } | |
4c3721d5 | 142 | #endif |
31568a6f | 143 | } |
ff37ea55 JG |
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. */ | |
1f29e30b | 149 | rawptr = (struct trad_core_struct *) |
326e32d7 | 150 | bfd_zmalloc (sizeof (struct trad_core_struct)); |
ff37ea55 | 151 | if (rawptr == NULL) { |
baf200d4 | 152 | bfd_set_error (bfd_error_no_memory); |
ff37ea55 JG |
153 | return 0; |
154 | } | |
155 | ||
1f29e30b JG |
156 | abfd->tdata.trad_core_data = rawptr; |
157 | ||
158 | rawptr->u = u; /*Copy the uarea into the tdata part of the bfd */ | |
ff37ea55 | 159 | |
637942e4 JG |
160 | /* Create the sections. This is raunchy, but bfd_close wants to free |
161 | them separately. */ | |
1f29e30b | 162 | |
baf200d4 | 163 | core_stacksec(abfd) = (asection *) bfd_zmalloc (sizeof (asection)); |
ff37ea55 | 164 | if (core_stacksec (abfd) == NULL) { |
1f29e30b | 165 | loser: |
baf200d4 | 166 | bfd_set_error (bfd_error_no_memory); |
ff37ea55 JG |
167 | free ((void *)rawptr); |
168 | return 0; | |
169 | } | |
baf200d4 | 170 | core_datasec (abfd) = (asection *) bfd_zmalloc (sizeof (asection)); |
ff37ea55 | 171 | if (core_datasec (abfd) == NULL) { |
1f29e30b | 172 | loser1: |
ff37ea55 JG |
173 | free ((void *)core_stacksec (abfd)); |
174 | goto loser; | |
175 | } | |
baf200d4 | 176 | core_regsec (abfd) = (asection *) bfd_zmalloc (sizeof (asection)); |
ff37ea55 | 177 | if (core_regsec (abfd) == NULL) { |
ff37ea55 JG |
178 | free ((void *)core_datasec (abfd)); |
179 | goto loser1; | |
180 | } | |
181 | ||
182 | core_stacksec (abfd)->name = ".stack"; | |
183 | core_datasec (abfd)->name = ".data"; | |
184 | core_regsec (abfd)->name = ".reg"; | |
185 | ||
6a469027 JG |
186 | core_stacksec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS; |
187 | core_datasec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS; | |
188 | core_regsec (abfd)->flags = SEC_ALLOC + SEC_HAS_CONTENTS; | |
ff37ea55 | 189 | |
baf200d4 JK |
190 | core_datasec (abfd)->_raw_size = NBPG * u.u_dsize |
191 | #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE | |
192 | - NBPG * u.u_tsize | |
193 | #endif | |
194 | ; | |
1f29e30b JG |
195 | core_stacksec (abfd)->_raw_size = NBPG * u.u_ssize; |
196 | core_regsec (abfd)->_raw_size = NBPG * UPAGES; /* Larger than sizeof struct u */ | |
ff37ea55 JG |
197 | |
198 | /* What a hack... we'd like to steal it from the exec file, | |
199 | since the upage does not seem to provide it. FIXME. */ | |
9712c6e2 SG |
200 | #ifdef HOST_DATA_START_ADDR |
201 | core_datasec (abfd)->vma = HOST_DATA_START_ADDR; | |
202 | #else | |
203 | core_datasec (abfd)->vma = HOST_TEXT_START_ADDR + (NBPG * u.u_tsize); | |
204 | #endif | |
4c3721d5 ILT |
205 | |
206 | #ifdef HOST_STACK_START_ADDR | |
207 | core_stacksec (abfd)->vma = HOST_STACK_START_ADDR; | |
208 | #else | |
9712c6e2 | 209 | core_stacksec (abfd)->vma = HOST_STACK_END_ADDR - (NBPG * u.u_ssize); |
4c3721d5 ILT |
210 | #endif |
211 | ||
637942e4 JG |
212 | /* This is tricky. As the "register section", we give them the entire |
213 | upage and stack. u.u_ar0 points to where "register 0" is stored. | |
214 | There are two tricks with this, though. One is that the rest of the | |
215 | registers might be at positive or negative (or both) displacements | |
216 | from *u_ar0. The other is that u_ar0 is sometimes an absolute address | |
217 | in kernel memory, and on other systems it is an offset from the beginning | |
218 | of the `struct user'. | |
1f29e30b | 219 | |
637942e4 JG |
220 | As a practical matter, we don't know where the registers actually are, |
221 | so we have to pass the whole area to GDB. We encode the value of u_ar0 | |
222 | by setting the .regs section up so that its virtual memory address | |
223 | 0 is at the place pointed to by u_ar0 (by setting the vma of the start | |
224 | of the section to -u_ar0). GDB uses this info to locate the regs, | |
225 | using minor trickery to get around the offset-or-absolute-addr problem. */ | |
226 | core_regsec (abfd)->vma = 0 - (int) u.u_ar0; | |
ff37ea55 JG |
227 | |
228 | core_datasec (abfd)->filepos = NBPG * UPAGES; | |
baf200d4 JK |
229 | core_stacksec (abfd)->filepos = (NBPG * UPAGES) + NBPG * u.u_dsize |
230 | #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE | |
231 | - NBPG * u.u_tsize | |
4c3721d5 | 232 | #endif |
baf200d4 | 233 | ; |
1f29e30b | 234 | core_regsec (abfd)->filepos = 0; /* Register segment is the upage */ |
ff37ea55 JG |
235 | |
236 | /* Align to word at least */ | |
237 | core_stacksec (abfd)->alignment_power = 2; | |
238 | core_datasec (abfd)->alignment_power = 2; | |
239 | core_regsec (abfd)->alignment_power = 2; | |
240 | ||
241 | abfd->sections = core_stacksec (abfd); | |
242 | core_stacksec (abfd)->next = core_datasec (abfd); | |
243 | core_datasec (abfd)->next = core_regsec (abfd); | |
244 | abfd->section_count = 3; | |
245 | ||
246 | return abfd->xvec; | |
ff37ea55 JG |
247 | } |
248 | ||
249 | char * | |
250 | trad_unix_core_file_failing_command (abfd) | |
251 | bfd *abfd; | |
252 | { | |
1f29e30b JG |
253 | #ifndef NO_CORE_COMMAND |
254 | char *com = abfd->tdata.trad_core_data->u.u_comm; | |
255 | if (*com) | |
256 | return com; | |
ff37ea55 | 257 | else |
1f29e30b | 258 | #endif |
ff37ea55 JG |
259 | return 0; |
260 | } | |
261 | ||
7ed4093a | 262 | /* ARGSUSED */ |
ff37ea55 | 263 | int |
7ed4093a SC |
264 | trad_unix_core_file_failing_signal (ignore_abfd) |
265 | bfd *ignore_abfd; | |
ff37ea55 | 266 | { |
31568a6f JK |
267 | #ifdef TRAD_UNIX_CORE_FILE_FAILING_SIGNAL |
268 | return TRAD_UNIX_CORE_FILE_FAILING_SIGNAL(ignore_abfd); | |
269 | #else | |
ff37ea55 | 270 | return -1; /* FIXME, where is it? */ |
31568a6f | 271 | #endif |
ff37ea55 JG |
272 | } |
273 | ||
7ed4093a | 274 | /* ARGSUSED */ |
ff37ea55 JG |
275 | boolean |
276 | trad_unix_core_file_matches_executable_p (core_bfd, exec_bfd) | |
277 | bfd *core_bfd, *exec_bfd; | |
278 | { | |
279 | return true; /* FIXME, We have no way of telling at this point */ | |
280 | } | |
6a469027 | 281 | \f |
637942e4 JG |
282 | /* If somebody calls any byte-swapping routines, shoot them. */ |
283 | void | |
284 | swap_abort() | |
285 | { | |
286 | abort(); /* This way doesn't require any declaration for ANSI to fuck up */ | |
287 | } | |
baf200d4 | 288 | #define NO_GET ((bfd_vma (*) PARAMS (( const bfd_byte *))) swap_abort ) |
1f29e30b | 289 | #define NO_PUT ((void (*) PARAMS ((bfd_vma, bfd_byte *))) swap_abort ) |
baf200d4 JK |
290 | #define NO_SIGNED_GET \ |
291 | ((bfd_signed_vma (*) PARAMS ((const bfd_byte *))) swap_abort ) | |
6a469027 | 292 | |
637942e4 | 293 | bfd_target trad_core_vec = |
6a469027 | 294 | { |
637942e4 | 295 | "trad-core", |
6a469027 JG |
296 | bfd_target_unknown_flavour, |
297 | true, /* target byte order */ | |
298 | true, /* target headers byte order */ | |
299 | (HAS_RELOC | EXEC_P | /* object flags */ | |
300 | HAS_LINENO | HAS_DEBUG | | |
4c3721d5 | 301 | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED), |
6a469027 | 302 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */ |
0dc1bc8b | 303 | 0, /* symbol prefix */ |
6a469027 JG |
304 | ' ', /* ar_pad_char */ |
305 | 16, /* ar_max_namelen */ | |
306 | 3, /* minimum alignment power */ | |
4d09e8ac JK |
307 | NO_GET, NO_SIGNED_GET, NO_PUT, /* 64 bit data */ |
308 | NO_GET, NO_SIGNED_GET, NO_PUT, /* 32 bit data */ | |
309 | NO_GET, NO_SIGNED_GET, NO_PUT, /* 16 bit data */ | |
310 | NO_GET, NO_SIGNED_GET, NO_PUT, /* 64 bit hdrs */ | |
311 | NO_GET, NO_SIGNED_GET, NO_PUT, /* 32 bit hdrs */ | |
312 | NO_GET, NO_SIGNED_GET, NO_PUT, /* 16 bit hdrs */ | |
31568a6f JK |
313 | |
314 | { /* bfd_check_format */ | |
315 | _bfd_dummy_target, /* unknown format */ | |
316 | _bfd_dummy_target, /* object file */ | |
317 | _bfd_dummy_target, /* archive */ | |
318 | trad_unix_core_file_p /* a core file */ | |
319 | }, | |
320 | { /* bfd_set_format */ | |
321 | bfd_false, bfd_false, | |
322 | bfd_false, bfd_false | |
323 | }, | |
324 | { /* bfd_write_contents */ | |
325 | bfd_false, bfd_false, | |
326 | bfd_false, bfd_false | |
327 | }, | |
6a469027 | 328 | |
6812b607 ILT |
329 | BFD_JUMP_TABLE_GENERIC (_bfd_generic), |
330 | BFD_JUMP_TABLE_COPY (_bfd_generic), | |
331 | BFD_JUMP_TABLE_CORE (trad_unix), | |
332 | BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive), | |
333 | BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols), | |
334 | BFD_JUMP_TABLE_RELOCS (_bfd_norelocs), | |
335 | BFD_JUMP_TABLE_WRITE (_bfd_generic), | |
336 | BFD_JUMP_TABLE_LINK (_bfd_nolink), | |
337 | ||
31568a6f | 338 | (PTR) 0 /* backend_data */ |
6a469027 | 339 | }; |