Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* BFD back end for SCO5 core files (U-area and raw sections) |
edeb6e24 AM |
2 | Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004 |
3 | Free Software Foundation, Inc. | |
252b5132 RH |
4 | Written by Jouke Numan <jnuman@hiscom.nl> |
5 | ||
6 | This file is part of BFD, the Binary File Descriptor library. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
3e110533 | 20 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ |
252b5132 RH |
21 | |
22 | #include "bfd.h" | |
23 | #include "sysdep.h" | |
24 | #include "libbfd.h" | |
b34976b6 | 25 | #include "libaout.h" /* BFD a.out internal data structures */ |
252b5132 RH |
26 | |
27 | #include <stdio.h> | |
28 | #include <sys/types.h> | |
29 | #include <sys/param.h> | |
30 | #include <sys/dir.h> | |
31 | #include <signal.h> | |
32 | ||
b34976b6 | 33 | #include <sys/user.h> /* After a.out.h */ |
252b5132 RH |
34 | #include <sys/paccess.h> |
35 | #include <sys/region.h> | |
36 | ||
37 | struct sco5_core_struct | |
38 | { | |
39 | struct user u; | |
40 | }; | |
41 | ||
42 | /* forward declarations */ | |
43 | ||
b34976b6 AM |
44 | static asection *make_bfd_asection |
45 | PARAMS ((bfd *, const char *, flagword, bfd_size_type, bfd_vma, file_ptr)); | |
252b5132 RH |
46 | static struct user *read_uarea PARAMS ((bfd *, int)); |
47 | const bfd_target *sco5_core_file_p PARAMS ((bfd *abfd)); | |
48 | char *sco5_core_file_failing_command PARAMS ((bfd *abfd)); | |
49 | int sco5_core_file_failing_signal PARAMS ((bfd *abfd)); | |
b34976b6 AM |
50 | bfd_boolean sco5_core_file_matches_executable_p |
51 | PARAMS ((bfd *core_bfd, bfd *exec_bfd)); | |
252b5132 RH |
52 | static void swap_abort PARAMS ((void)); |
53 | ||
54 | static asection * | |
eea6121a | 55 | make_bfd_asection (abfd, name, flags, size, vma, filepos) |
252b5132 RH |
56 | bfd *abfd; |
57 | const char *name; | |
58 | flagword flags; | |
eea6121a | 59 | bfd_size_type size; |
252b5132 RH |
60 | bfd_vma vma; |
61 | file_ptr filepos; | |
62 | { | |
63 | asection *asect; | |
64 | ||
65 | asect = bfd_make_section_anyway (abfd, name); | |
66 | if (!asect) | |
67 | return NULL; | |
68 | asect->flags = flags; | |
eea6121a | 69 | asect->size = size; |
252b5132 RH |
70 | asect->vma = vma; |
71 | asect->filepos = filepos; | |
72 | asect->alignment_power = 2; | |
73 | ||
74 | return asect; | |
75 | } | |
76 | ||
252b5132 RH |
77 | static struct user * |
78 | read_uarea(abfd, filepos) | |
79 | bfd *abfd; | |
80 | int filepos; | |
81 | ||
82 | { | |
83 | struct sco5_core_struct *rawptr; | |
dc810e39 | 84 | bfd_size_type amt = sizeof (struct sco5_core_struct); |
252b5132 | 85 | |
dc810e39 | 86 | rawptr = (struct sco5_core_struct *) bfd_zmalloc (amt); |
252b5132 RH |
87 | if (rawptr == NULL) |
88 | return NULL; | |
89 | ||
90 | abfd->tdata.sco5_core_data = rawptr; | |
91 | ||
dc810e39 AM |
92 | if (bfd_seek (abfd, (file_ptr) filepos, SEEK_SET) != 0 |
93 | || bfd_bread ((void *) &rawptr->u, (bfd_size_type) sizeof rawptr->u, | |
94 | abfd) != sizeof rawptr->u) | |
252b5132 RH |
95 | { |
96 | bfd_set_error (bfd_error_wrong_format); | |
97 | return NULL; | |
98 | } | |
99 | ||
100 | /* Sanity check perhaps??? */ | |
7b82c249 | 101 | if (rawptr->u.u_dsize > 0x1000000) /* Remember, it's in pages... */ |
252b5132 RH |
102 | { |
103 | bfd_set_error (bfd_error_wrong_format); | |
104 | return NULL; | |
105 | } | |
106 | if (rawptr->u.u_ssize > 0x1000000) | |
107 | { | |
108 | bfd_set_error (bfd_error_wrong_format); | |
109 | return NULL; | |
110 | } | |
111 | return &rawptr->u; | |
112 | } | |
113 | ||
252b5132 RH |
114 | const bfd_target * |
115 | sco5_core_file_p (abfd) | |
116 | bfd *abfd; | |
117 | { | |
118 | int coffset_siz, val, nsecs, cheadoffs; | |
119 | int coresize; | |
120 | struct user *u; | |
121 | struct coreoffsets coffsets; | |
122 | struct coresecthead chead; | |
123 | char *secname; | |
124 | flagword flags; | |
125 | ||
33216455 | 126 | /* Read coreoffsets region at end of core (see core(FP)). */ |
252b5132 RH |
127 | |
128 | { | |
129 | FILE *stream = bfd_cache_lookup (abfd); | |
130 | struct stat statbuf; | |
33216455 | 131 | |
3dff57e8 AM |
132 | if (stream == NULL) |
133 | return NULL; | |
252b5132 RH |
134 | if (fstat (fileno (stream), &statbuf) < 0) |
135 | { | |
b34976b6 AM |
136 | bfd_set_error (bfd_error_system_call); |
137 | return NULL; | |
252b5132 RH |
138 | } |
139 | coresize = statbuf.st_size; | |
140 | } | |
141 | /* Last long in core is sizeof struct coreoffsets, read it */ | |
dc810e39 AM |
142 | if ((bfd_seek (abfd, (file_ptr) (coresize - sizeof coffset_siz), |
143 | SEEK_SET) != 0) | |
144 | || bfd_bread ((void *) &coffset_siz, (bfd_size_type) sizeof coffset_siz, | |
145 | abfd) != sizeof coffset_siz) | |
252b5132 | 146 | { |
7b82c249 | 147 | bfd_set_error (bfd_error_wrong_format); |
252b5132 RH |
148 | return NULL; |
149 | } | |
150 | ||
151 | /* Use it to seek start of coreoffsets region, read it and determine | |
152 | validity */ | |
dc810e39 AM |
153 | if ((bfd_seek (abfd, (file_ptr) (coresize - coffset_siz), SEEK_SET) != 0) |
154 | || (bfd_bread ((void *) &coffsets, (bfd_size_type) sizeof coffsets, abfd) | |
252b5132 RH |
155 | != sizeof coffsets) |
156 | || ((coffsets.u_info != 1) && (coffsets.u_info != C_VERSION))) | |
157 | { | |
7b82c249 | 158 | bfd_set_error (bfd_error_wrong_format); |
252b5132 RH |
159 | return NULL; |
160 | } | |
161 | ||
7b82c249 KH |
162 | if (coffsets.u_info == 1) |
163 | { | |
252b5132 RH |
164 | /* Old version, no section heads, read info from user struct */ |
165 | ||
9e7b37b3 | 166 | u = read_uarea (abfd, coffsets.u_user); |
252b5132 | 167 | if (! u) |
9e7b37b3 | 168 | goto fail; |
252b5132 | 169 | |
7b82c249 | 170 | if (!make_bfd_asection (abfd, ".reg", SEC_HAS_CONTENTS, |
b34976b6 AM |
171 | (bfd_size_type) coffsets.u_usize, |
172 | 0 - (bfd_vma) u->u_ar0, | |
173 | (file_ptr) coffsets.u_user)) | |
9e7b37b3 | 174 | goto fail; |
7b82c249 KH |
175 | |
176 | if (!make_bfd_asection (abfd, ".data", | |
252b5132 | 177 | SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS, |
b34976b6 | 178 | ((bfd_size_type) u->u_exdata.ux_dsize |
252b5132 | 179 | + u->u_exdata.ux_bsize), |
b34976b6 AM |
180 | (bfd_vma) u->u_exdata.ux_datorg, |
181 | (file_ptr) coffsets.u_data)) | |
9e7b37b3 | 182 | goto fail; |
7b82c249 KH |
183 | |
184 | if (!make_bfd_asection (abfd, ".stack", | |
185 | SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS, | |
b34976b6 AM |
186 | (bfd_size_type) u->u_ssize * NBPC, |
187 | (bfd_vma) u->u_sub, | |
188 | (file_ptr) coffsets.u_stack)) | |
9e7b37b3 | 189 | goto fail; |
252b5132 RH |
190 | |
191 | return abfd->xvec; /* Done for version 1 */ | |
7b82c249 | 192 | } |
252b5132 RH |
193 | |
194 | /* Immediately before coreoffsets region is a long with offset in core | |
195 | to first coresecthead (CORES_OFFSETS), the long before this is the | |
196 | number of section heads in the list. Read both longs and read the | |
197 | coresecthead and check its validity */ | |
7b82c249 KH |
198 | |
199 | if ((bfd_seek (abfd, | |
dc810e39 | 200 | (file_ptr) (coresize - coffset_siz - 2 * sizeof coffset_siz), |
252b5132 | 201 | SEEK_SET) != 0) |
dc810e39 AM |
202 | || (bfd_bread ((void *) &nsecs, (bfd_size_type) sizeof nsecs, abfd) |
203 | != sizeof nsecs) | |
204 | || (bfd_bread ((void *) &cheadoffs, (bfd_size_type) sizeof cheadoffs, | |
205 | abfd) != sizeof cheadoffs) | |
206 | || (bfd_seek (abfd, (file_ptr) cheadoffs, SEEK_SET) != 0) | |
207 | || (bfd_bread ((void *) &chead, (bfd_size_type) sizeof chead, abfd) | |
208 | != sizeof chead) | |
252b5132 RH |
209 | || (chead.cs_stype != CORES_OFFSETS) |
210 | || (chead.cs_x.csx_magic != COREMAGIC_NUMBER)) | |
211 | { | |
212 | bfd_set_error (bfd_error_wrong_format); | |
9e7b37b3 | 213 | goto fail; |
252b5132 RH |
214 | } |
215 | ||
216 | /* OK, we believe you. You're a core file (sure, sure). */ | |
217 | ||
218 | /* Now loop over all regions and map them */ | |
219 | nsecs--; /* We've seen CORES_OFFSETS already */ | |
7b82c249 | 220 | for (; nsecs; nsecs--) |
252b5132 | 221 | { |
dc810e39 AM |
222 | if ((bfd_seek (abfd, (file_ptr) chead.cs_hseek, SEEK_SET) != 0) |
223 | || (bfd_bread ((void *) &chead, (bfd_size_type) sizeof chead, abfd) | |
224 | != sizeof chead)) | |
b34976b6 AM |
225 | { |
226 | bfd_set_error (bfd_error_wrong_format); | |
9e7b37b3 | 227 | goto fail; |
b34976b6 | 228 | } |
252b5132 | 229 | |
7b82c249 | 230 | switch (chead.cs_stype) |
252b5132 RH |
231 | { |
232 | case CORES_MAGIC: /* Core header, check magic */ | |
233 | if (chead.cs_x.csx_magic != COREMAGIC_NUMBER) | |
234 | { | |
235 | bfd_set_error (bfd_error_wrong_format); | |
9e7b37b3 | 236 | goto fail; |
252b5132 RH |
237 | } |
238 | secname = NULL; | |
239 | nsecs++; /* MAGIC not in section cnt!*/ | |
240 | break; | |
241 | case CORES_UAREA: /* U-area, read in tdata */ | |
9e7b37b3 | 242 | u = read_uarea (abfd, chead.cs_sseek); |
252b5132 | 243 | if (! u) |
9e7b37b3 | 244 | goto fail; |
252b5132 | 245 | |
b34976b6 | 246 | /* This is tricky. As the "register section", we give them |
252b5132 RH |
247 | the entire upage and stack. u.u_ar0 points to where |
248 | "register 0" is stored. There are two tricks with this, | |
249 | though. One is that the rest of the registers might be | |
250 | at positive or negative (or both) displacements from | |
251 | *u_ar0. The other is that u_ar0 is sometimes an absolute | |
252 | address in kernel memory, and on other systems it is an | |
253 | offset from the beginning of the `struct user'. | |
7b82c249 | 254 | |
252b5132 RH |
255 | As a practical matter, we don't know where the registers |
256 | actually are, so we have to pass the whole area to GDB. | |
257 | We encode the value of u_ar0 by setting the .regs section | |
258 | up so that its virtual memory address 0 is at the place | |
259 | pointed to by u_ar0 (by setting the vma of the start of | |
260 | the section to -u_ar0). GDB uses this info to locate the | |
261 | regs, using minor trickery to get around the | |
7b82c249 | 262 | offset-or-absolute-addr problem. */ |
252b5132 RH |
263 | |
264 | chead.cs_vaddr = 0 - (bfd_vma) u->u_ar0; | |
265 | ||
b34976b6 AM |
266 | secname = ".reg"; |
267 | flags = SEC_HAS_CONTENTS; | |
252b5132 | 268 | |
b34976b6 | 269 | break; |
252b5132 RH |
270 | case CORES_PREGION: /* A program region, map it */ |
271 | switch (chead.cs_x.csx_preg.csxp_rtyp) | |
272 | { | |
273 | case PT_DATA: | |
b34976b6 | 274 | secname = ".data"; /* Data region. */ |
252b5132 RH |
275 | break; |
276 | case PT_STACK: | |
b34976b6 | 277 | secname = ".stack"; /* Stack region. */ |
252b5132 RH |
278 | break; |
279 | case PT_SHMEM: | |
b34976b6 | 280 | secname = ".shmem"; /* Shared memory */ |
252b5132 RH |
281 | break; |
282 | case PT_LIBDAT: | |
b34976b6 | 283 | secname = ".libdat"; /* Shared library data */ |
252b5132 RH |
284 | break; |
285 | case PT_V86: | |
b34976b6 | 286 | secname = ".virt86"; /* Virtual 8086 mode */ |
252b5132 RH |
287 | break; |
288 | case PT_SHFIL: | |
b34976b6 | 289 | secname = ".mmfile"; /* Memory mapped file */ |
252b5132 RH |
290 | break; |
291 | case PT_XDATA0: | |
b34976b6 | 292 | secname = ".Xdat0"; /* XENIX data region, virtual 0 */ |
252b5132 RH |
293 | break; |
294 | default: | |
295 | secname = ""; | |
296 | } | |
297 | flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS; | |
298 | break; | |
299 | case CORES_PROC: /* struct proc */ | |
300 | case CORES_ITIMER: /* interval timers */ | |
301 | case CORES_SCOUTSNAME: /* struct scoutsname */ | |
302 | secname = NULL; /* Ignore these */ | |
303 | break; | |
b34976b6 AM |
304 | default: |
305 | (*_bfd_error_handler) ("Unhandled SCO core file section type %d\n", | |
306 | chead.cs_stype); | |
307 | continue; | |
308 | } | |
252b5132 RH |
309 | |
310 | if (secname | |
311 | && !make_bfd_asection (abfd, secname, flags, | |
312 | (bfd_size_type) chead.cs_vsize, | |
313 | (bfd_vma) chead.cs_vaddr, | |
314 | (file_ptr) chead.cs_sseek)) | |
9e7b37b3 | 315 | goto fail; |
252b5132 RH |
316 | |
317 | } | |
318 | ||
319 | return abfd->xvec; | |
320 | ||
9e7b37b3 AM |
321 | fail: |
322 | if (abfd->tdata.any) | |
323 | { | |
324 | bfd_release (abfd, abfd->tdata.any); | |
325 | abfd->tdata.any = NULL; | |
326 | } | |
327 | bfd_section_list_clear (abfd); | |
328 | return NULL; | |
252b5132 RH |
329 | } |
330 | ||
331 | char * | |
332 | sco5_core_file_failing_command (abfd) | |
333 | bfd *abfd; | |
334 | { | |
335 | char *com = abfd->tdata.sco5_core_data->u.u_comm; | |
336 | if (*com) | |
337 | return com; | |
338 | else | |
339 | return NULL; | |
340 | } | |
341 | ||
252b5132 RH |
342 | int |
343 | sco5_core_file_failing_signal (ignore_abfd) | |
344 | bfd *ignore_abfd; | |
345 | { | |
7b82c249 KH |
346 | return ((ignore_abfd->tdata.sco5_core_data->u.u_sysabort != 0) |
347 | ? ignore_abfd->tdata.sco5_core_data->u.u_sysabort | |
252b5132 RH |
348 | : -1); |
349 | } | |
350 | ||
b34976b6 | 351 | bfd_boolean |
252b5132 | 352 | sco5_core_file_matches_executable_p (core_bfd, exec_bfd) |
dc810e39 AM |
353 | bfd *core_bfd ATTRIBUTE_UNUSED; |
354 | bfd *exec_bfd ATTRIBUTE_UNUSED; | |
252b5132 | 355 | { |
b34976b6 | 356 | return TRUE; /* FIXME, We have no way of telling at this point */ |
252b5132 RH |
357 | } |
358 | ||
252b5132 RH |
359 | /* If somebody calls any byte-swapping routines, shoot them. */ |
360 | static void | |
7b82c249 | 361 | swap_abort () |
252b5132 | 362 | { |
7b82c249 | 363 | abort (); /* This way doesn't require any declaration for ANSI to fuck up */ |
252b5132 | 364 | } |
8ce8c090 | 365 | |
edeb6e24 AM |
366 | #define NO_GET ((bfd_vma (*) (const void *)) swap_abort) |
367 | #define NO_PUT ((void (*) (bfd_vma, void *)) swap_abort) | |
368 | #define NO_GETS ((bfd_signed_vma (*) (const void *)) swap_abort) | |
8ce8c090 AM |
369 | #define NO_GET64 ((bfd_uint64_t (*) (const void *)) swap_abort) |
370 | #define NO_PUT64 ((void (*) (bfd_uint64_t, void *)) swap_abort) | |
371 | #define NO_GETS64 ((bfd_int64_t (*) (const void *)) swap_abort) | |
252b5132 RH |
372 | |
373 | const bfd_target sco5_core_vec = | |
374 | { | |
375 | "sco5-core", | |
376 | bfd_target_unknown_flavour, | |
b34976b6 AM |
377 | BFD_ENDIAN_LITTLE, /* target byte order */ |
378 | BFD_ENDIAN_LITTLE, /* target headers byte order */ | |
379 | (HAS_RELOC | EXEC_P | /* object flags */ | |
252b5132 RH |
380 | HAS_LINENO | HAS_DEBUG | |
381 | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED), | |
382 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */ | |
b34976b6 AM |
383 | 0, /* symbol prefix */ |
384 | ' ', /* ar_pad_char */ | |
385 | 16, /* ar_max_namelen */ | |
8ce8c090 | 386 | NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit data */ |
edeb6e24 AM |
387 | NO_GET, NO_GETS, NO_PUT, /* 32 bit data */ |
388 | NO_GET, NO_GETS, NO_PUT, /* 16 bit data */ | |
8ce8c090 | 389 | NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit hdrs */ |
edeb6e24 AM |
390 | NO_GET, NO_GETS, NO_PUT, /* 32 bit hdrs */ |
391 | NO_GET, NO_GETS, NO_PUT, /* 16 bit hdrs */ | |
b34976b6 AM |
392 | |
393 | { /* bfd_check_format */ | |
8ce8c090 AM |
394 | _bfd_dummy_target, /* unknown format */ |
395 | _bfd_dummy_target, /* object file */ | |
396 | _bfd_dummy_target, /* archive */ | |
397 | sco5_core_file_p /* a core file */ | |
252b5132 | 398 | }, |
b34976b6 | 399 | { /* bfd_set_format */ |
8ce8c090 AM |
400 | bfd_false, bfd_false, |
401 | bfd_false, bfd_false | |
252b5132 | 402 | }, |
b34976b6 | 403 | { /* bfd_write_contents */ |
8ce8c090 AM |
404 | bfd_false, bfd_false, |
405 | bfd_false, bfd_false | |
252b5132 RH |
406 | }, |
407 | ||
3f3c5c34 AM |
408 | BFD_JUMP_TABLE_GENERIC (_bfd_generic), |
409 | BFD_JUMP_TABLE_COPY (_bfd_generic), | |
410 | BFD_JUMP_TABLE_CORE (sco5), | |
411 | BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive), | |
412 | BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols), | |
413 | BFD_JUMP_TABLE_RELOCS (_bfd_norelocs), | |
414 | BFD_JUMP_TABLE_WRITE (_bfd_generic), | |
415 | BFD_JUMP_TABLE_LINK (_bfd_nolink), | |
416 | BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), | |
252b5132 | 417 | |
c3c89269 | 418 | NULL, |
7b82c249 | 419 | |
b34976b6 | 420 | (PTR) 0 /* backend_data */ |
8ce8c090 | 421 | }; |