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