Touches most files in bfd/, so likely will be blamed for everything..
[external/binutils.git] / bfd / netbsd-core.c
1 /* BFD back end for NetBSD style core files
2    Copyright 1988, 1989, 1991, 1992, 1993, 1996, 1998, 1999, 2000, 2001
3    Free Software Foundation, Inc.
4    Written by Paul Kranenburg, EUR
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
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libbfd.h"
25 #include "libaout.h"           /* BFD a.out internal data structures */
26
27 #include <sys/param.h>
28 #include <sys/dir.h>
29 #include <signal.h>
30 #include <sys/core.h>
31
32 /*
33  * FIXME: On NetBSD/sparc CORE_FPU_OFFSET should be (sizeof (struct trapframe))
34  */
35
36 struct netbsd_core_struct {
37         struct core core;
38 } *rawptr;
39
40 /* forward declarations */
41
42 static const bfd_target *netbsd_core_file_p PARAMS ((bfd *abfd));
43 static char *netbsd_core_file_failing_command PARAMS ((bfd *abfd));
44 static int netbsd_core_file_failing_signal PARAMS ((bfd *abfd));
45 static boolean netbsd_core_file_matches_executable_p
46   PARAMS ((bfd *core_bfd, bfd *exec_bfd));
47 static void swap_abort PARAMS ((void));
48
49 /* Handle NetBSD-style core dump file.  */
50
51 /* ARGSUSED */
52 static const bfd_target *
53 netbsd_core_file_p (abfd)
54      bfd *abfd;
55
56 {
57   int i, val;
58   file_ptr offset;
59   asection *asect, *asect2;
60   struct core core;
61   struct coreseg coreseg;
62   bfd_size_type amt = sizeof core;
63
64   val = bfd_bread ((void *) &core, amt, abfd);
65   if (val != sizeof core)
66     {
67       /* Too small to be a core file */
68       bfd_set_error (bfd_error_wrong_format);
69       return 0;
70     }
71
72   if (CORE_GETMAGIC (core) != COREMAGIC)
73     {
74       bfd_set_error (bfd_error_wrong_format);
75       return 0;
76     }
77
78   amt = sizeof (struct netbsd_core_struct);
79   rawptr = (struct netbsd_core_struct *) bfd_zalloc (abfd, amt);
80   if (rawptr == NULL)
81     {
82       bfd_set_error (bfd_error_no_memory);
83       return 0;
84     }
85
86   rawptr->core = core;
87   abfd->tdata.netbsd_core_data = rawptr;
88
89   offset = core.c_hdrsize;
90   for (i = 0; i < core.c_nseg; i++)
91     {
92
93       if (bfd_seek (abfd, offset, SEEK_SET) != 0)
94         goto punt;
95
96       val = bfd_bread ((void *) &coreseg, (bfd_size_type) sizeof coreseg, abfd);
97       if (val != sizeof coreseg)
98         {
99           bfd_set_error (bfd_error_file_truncated);
100           goto punt;
101         }
102       if (CORE_GETMAGIC (coreseg) != CORESEGMAGIC)
103         {
104           bfd_set_error (bfd_error_wrong_format);
105           goto punt;
106         }
107
108       offset += core.c_seghdrsize;
109
110       amt = sizeof (asection);
111       asect = (asection *) bfd_zalloc (abfd, amt);
112       if (asect == NULL)
113         {
114           bfd_set_error (bfd_error_no_memory);
115           goto punt;
116         }
117
118       asect->_raw_size = coreseg.c_size;
119       asect->vma = coreseg.c_addr;
120       asect->filepos = offset;
121       asect->alignment_power = 2;
122       asect->next = abfd->sections;
123       abfd->sections = asect;
124       abfd->section_count++;
125       offset += coreseg.c_size;
126
127       switch (CORE_GETFLAG(coreseg))
128         {
129         case CORE_CPU:
130           asect->name = ".reg";
131           asect->flags = SEC_ALLOC + SEC_HAS_CONTENTS;
132 #ifdef CORE_FPU_OFFSET
133           /* Hackish...  */
134           asect->_raw_size = CORE_FPU_OFFSET;
135           amt = sizeof (asection);
136           asect2 = (asection *) bfd_zalloc (abfd, amt);
137           if (asect2 == NULL)
138             {
139               bfd_set_error (bfd_error_no_memory);
140               goto punt;
141             }
142           asect2->_raw_size = coreseg.c_size - CORE_FPU_OFFSET;
143           asect2->vma = 0;
144           asect2->filepos = asect->filepos + CORE_FPU_OFFSET;
145           asect2->alignment_power = 2;
146           asect2->next = abfd->sections;
147           asect2->name = ".reg2";
148           asect2->flags = SEC_ALLOC + SEC_HAS_CONTENTS;
149           abfd->sections = asect2;
150           abfd->section_count++;
151 #endif
152
153           break;
154         case CORE_DATA:
155           asect->name = ".data";
156           asect->flags = SEC_ALLOC+SEC_LOAD+SEC_HAS_CONTENTS;
157           break;
158         case CORE_STACK:
159           asect->name = ".stack";
160           asect->flags = SEC_ALLOC+SEC_LOAD+SEC_HAS_CONTENTS;
161           break;
162         }
163     }
164
165   /* OK, we believe you.  You're a core file (sure, sure).  */
166   return abfd->xvec;
167
168  punt:
169   {
170     asection *anext;
171     for (asect = abfd->sections; asect; asect = anext)
172       {
173         anext = asect->next;
174         free ((void *) asect);
175       }
176   }
177
178   free ((void *) rawptr);
179   abfd->tdata.netbsd_core_data = NULL;
180   abfd->sections = NULL;
181   abfd->section_count = 0;
182   return 0;
183 }
184
185 static char*
186 netbsd_core_file_failing_command (abfd)
187         bfd *abfd;
188 {
189  /*return core_command (abfd);*/
190   return abfd->tdata.netbsd_core_data->core.c_name;
191 }
192
193 /* ARGSUSED */
194 static int
195 netbsd_core_file_failing_signal (abfd)
196         bfd *abfd;
197 {
198   /*return core_signal (abfd);*/
199   return abfd->tdata.netbsd_core_data->core.c_signo;
200 }
201
202 /* ARGSUSED */
203 static boolean
204 netbsd_core_file_matches_executable_p  (core_bfd, exec_bfd)
205      bfd *core_bfd ATTRIBUTE_UNUSED;
206      bfd *exec_bfd ATTRIBUTE_UNUSED;
207 {
208   return true;          /* FIXME, We have no way of telling at this point */
209 }
210 \f
211 /* If somebody calls any byte-swapping routines, shoot them.  */
212 static void
213 swap_abort ()
214 {
215   abort (); /* This way doesn't require any declaration for ANSI to fuck up */
216 }
217 #define NO_GET  ((bfd_vma (*) PARAMS ((   const bfd_byte *))) swap_abort )
218 #define NO_PUT  ((void    (*) PARAMS ((bfd_vma, bfd_byte *))) swap_abort )
219 #define NO_SIGNED_GET \
220   ((bfd_signed_vma (*) PARAMS ((const bfd_byte *))) swap_abort )
221
222 const bfd_target netbsd_core_vec =
223   {
224     "netbsd-core",
225     bfd_target_unknown_flavour,
226     BFD_ENDIAN_UNKNOWN,         /* target byte order */
227     BFD_ENDIAN_UNKNOWN,         /* target headers byte order */
228     (HAS_RELOC | EXEC_P |       /* object flags */
229      HAS_LINENO | HAS_DEBUG |
230      HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
231     (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
232     0,                                                     /* symbol prefix */
233     ' ',                                                   /* ar_pad_char */
234     16,                                                    /* ar_max_namelen */
235     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 64 bit data */
236     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 32 bit data */
237     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 16 bit data */
238     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 64 bit hdrs */
239     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 32 bit hdrs */
240     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 16 bit hdrs */
241
242     {                           /* bfd_check_format */
243      _bfd_dummy_target,         /* unknown format */
244      _bfd_dummy_target,         /* object file */
245      _bfd_dummy_target,         /* archive */
246      netbsd_core_file_p         /* a core file */
247     },
248     {                           /* bfd_set_format */
249      bfd_false, bfd_false,
250      bfd_false, bfd_false
251     },
252     {                           /* bfd_write_contents */
253      bfd_false, bfd_false,
254      bfd_false, bfd_false
255     },
256
257        BFD_JUMP_TABLE_GENERIC (_bfd_generic),
258        BFD_JUMP_TABLE_COPY (_bfd_generic),
259        BFD_JUMP_TABLE_CORE (netbsd),
260        BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
261        BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
262        BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
263        BFD_JUMP_TABLE_WRITE (_bfd_generic),
264        BFD_JUMP_TABLE_LINK (_bfd_nolink),
265        BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
266
267     NULL,
268
269     (PTR) 0                     /* backend_data */
270 };