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