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