xref: /openbsd-src/gnu/usr.bin/binutils/bfd/netbsd-core.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /* BFD back end for NetBSD style core files
2    Copyright 1988, 1989, 1991, 1992, 1993, 1996, 1998, 1999, 2000
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, offset;
58 	asection	*asect, *asect2;
59 	struct core	core;
60 	struct coreseg	coreseg;
61 
62 	val = bfd_read ((void *)&core, 1, sizeof core, abfd);
63 	if (val != sizeof core) {
64 		/* Too small to be a core file */
65 		bfd_set_error(bfd_error_wrong_format);
66 		return 0;
67 	}
68 
69 	if (CORE_GETMAGIC(core) != COREMAGIC) {
70 		bfd_set_error(bfd_error_wrong_format);
71 		return 0;
72 	}
73 
74 	rawptr = (struct netbsd_core_struct *)
75 		bfd_zalloc (abfd, sizeof (struct netbsd_core_struct));
76 	if (rawptr == NULL) {
77 		bfd_set_error(bfd_error_no_memory);
78 		return 0;
79 	}
80 
81 	rawptr->core = core;
82 	abfd->tdata.netbsd_core_data = rawptr;
83 
84 	offset = core.c_hdrsize;
85 	for (i = 0; i < core.c_nseg; i++) {
86 
87 		if (bfd_seek (abfd, offset, SEEK_SET) != 0)
88 			goto punt;
89 
90 		val = bfd_read ((void *)&coreseg, 1, sizeof coreseg, abfd);
91 		if (val != sizeof coreseg) {
92 			bfd_set_error(bfd_error_file_truncated);
93 			goto punt;
94 		}
95 		if (CORE_GETMAGIC(coreseg) != CORESEGMAGIC) {
96 			bfd_set_error(bfd_error_wrong_format);
97 			goto punt;
98 		}
99 
100 		offset += core.c_seghdrsize;
101 
102 		asect = (asection *) bfd_zalloc (abfd, sizeof (asection));
103 		if (asect == NULL) {
104 			bfd_set_error(bfd_error_no_memory);
105 			goto punt;
106 		}
107 
108 		asect->_raw_size = coreseg.c_size;
109 		asect->vma = coreseg.c_addr;
110 		asect->filepos = offset;
111 		asect->alignment_power = 2;
112 		asect->next = abfd->sections;
113 		abfd->sections = asect;
114 		abfd->section_count++;
115 		offset += coreseg.c_size;
116 
117 		switch (CORE_GETFLAG(coreseg)) {
118 		case CORE_CPU:
119 			asect->name = ".reg";
120 			asect->flags = SEC_ALLOC + SEC_HAS_CONTENTS;
121 #ifdef CORE_FPU_OFFSET
122 			/* Hackish...  */
123 			asect->_raw_size = CORE_FPU_OFFSET;
124 			asect2 = (asection *)bfd_zalloc (abfd,
125 							 sizeof (asection));
126 			if (asect2 == NULL) {
127 				bfd_set_error(bfd_error_no_memory);
128 				goto punt;
129 			}
130 			asect2->_raw_size = coreseg.c_size - CORE_FPU_OFFSET;
131 			asect2->vma = 0;
132 			asect2->filepos = asect->filepos + CORE_FPU_OFFSET;
133 			asect2->alignment_power = 2;
134 			asect2->next = abfd->sections;
135 			asect2->name = ".reg2";
136 			asect2->flags = SEC_ALLOC + SEC_HAS_CONTENTS;
137 			abfd->sections = asect2;
138 			abfd->section_count++;
139 #endif
140 
141 			break;
142 		case CORE_DATA:
143 			asect->name = ".data";
144 			asect->flags = SEC_ALLOC+SEC_LOAD+SEC_HAS_CONTENTS;
145 			break;
146 		case CORE_STACK:
147 			asect->name = ".stack";
148 			asect->flags = SEC_ALLOC+SEC_LOAD+SEC_HAS_CONTENTS;
149 			break;
150 		}
151 	}
152 
153 	/* OK, we believe you.  You're a core file (sure, sure).  */
154 	return abfd->xvec;
155 
156 punt:	{
157 		asection	*anext;
158 		for (asect = abfd->sections; asect; asect = anext) {
159 			anext = asect->next;
160 			free((void *)asect);
161 		}
162 	}
163 	free ((void *)rawptr);
164 	abfd->tdata.netbsd_core_data = NULL;
165 	abfd->sections = NULL;
166 	abfd->section_count = 0;
167 	return 0;
168 }
169 
170 static char*
171 netbsd_core_file_failing_command (abfd)
172 	bfd *abfd;
173 {
174  /*return core_command (abfd);*/
175   return abfd->tdata.netbsd_core_data->core.c_name;
176 }
177 
178 /* ARGSUSED */
179 static int
180 netbsd_core_file_failing_signal (abfd)
181 	bfd *abfd;
182 {
183   /*return core_signal (abfd);*/
184   return abfd->tdata.netbsd_core_data->core.c_signo;
185 }
186 
187 /* ARGSUSED */
188 static boolean
189 netbsd_core_file_matches_executable_p  (core_bfd, exec_bfd)
190      bfd *core_bfd, *exec_bfd;
191 {
192   return true;		/* FIXME, We have no way of telling at this point */
193 }
194 
195 /* If somebody calls any byte-swapping routines, shoot them.  */
196 static void
197 swap_abort ()
198 {
199   abort (); /* This way doesn't require any declaration for ANSI to fuck up */
200 }
201 #define	NO_GET	((bfd_vma (*) PARAMS ((   const bfd_byte *))) swap_abort )
202 #define	NO_PUT	((void    (*) PARAMS ((bfd_vma, bfd_byte *))) swap_abort )
203 #define	NO_SIGNED_GET \
204   ((bfd_signed_vma (*) PARAMS ((const bfd_byte *))) swap_abort )
205 
206 const bfd_target netbsd_core_vec =
207   {
208     "netbsd-core",
209     bfd_target_unknown_flavour,
210     BFD_ENDIAN_UNKNOWN,		/* target byte order */
211     BFD_ENDIAN_UNKNOWN,		/* target headers byte order */
212     (HAS_RELOC | EXEC_P |	/* object flags */
213      HAS_LINENO | HAS_DEBUG |
214      HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
215     (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
216     0,			                                   /* symbol prefix */
217     ' ',						   /* ar_pad_char */
218     16,							   /* ar_max_namelen */
219     NO_GET, NO_SIGNED_GET, NO_PUT,	/* 64 bit data */
220     NO_GET, NO_SIGNED_GET, NO_PUT,	/* 32 bit data */
221     NO_GET, NO_SIGNED_GET, NO_PUT,	/* 16 bit data */
222     NO_GET, NO_SIGNED_GET, NO_PUT,	/* 64 bit hdrs */
223     NO_GET, NO_SIGNED_GET, NO_PUT,	/* 32 bit hdrs */
224     NO_GET, NO_SIGNED_GET, NO_PUT,	/* 16 bit hdrs */
225 
226     {				/* bfd_check_format */
227      _bfd_dummy_target,		/* unknown format */
228      _bfd_dummy_target,		/* object file */
229      _bfd_dummy_target,		/* archive */
230      netbsd_core_file_p		/* a core file */
231     },
232     {				/* bfd_set_format */
233      bfd_false, bfd_false,
234      bfd_false, bfd_false
235     },
236     {				/* bfd_write_contents */
237      bfd_false, bfd_false,
238      bfd_false, bfd_false
239     },
240 
241        BFD_JUMP_TABLE_GENERIC (_bfd_generic),
242        BFD_JUMP_TABLE_COPY (_bfd_generic),
243        BFD_JUMP_TABLE_CORE (netbsd),
244        BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
245        BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
246        BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
247        BFD_JUMP_TABLE_WRITE (_bfd_generic),
248        BFD_JUMP_TABLE_LINK (_bfd_nolink),
249        BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
250 
251     NULL,
252 
253     (PTR) 0			/* backend_data */
254 };
255