* ldmain.c (main): Rename BufferSize to ld_bufsz because HPUX
[binutils.git] / bfd / lynx-core.c
blobee070cbc2ee79c50509da7633df4aa8506b80641
1 /* BFD back end for Lynx core files
2 Copyright 1993, 1994, 1995, 2001 Free Software Foundation, Inc.
3 Written by Stu Grossman of Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
25 #ifdef LYNX_CORE
27 #include <sys/conf.h>
28 #include <sys/kernel.h>
29 /* sys/kernel.h should define this, but doesn't always, sigh. */
30 #ifndef __LYNXOS
31 #define __LYNXOS
32 #endif
33 #include <sys/mem.h>
34 #include <sys/signal.h>
35 #include <sys/time.h>
36 #include <sys/resource.h>
37 #include <sys/itimer.h>
38 #include <sys/file.h>
39 #include <sys/proc.h>
41 /* These are stored in the bfd's tdata */
43 struct lynx_core_struct
45 int sig;
46 char cmd[PNMLEN + 1];
49 #define core_hdr(bfd) ((bfd)->tdata.lynx_core_data)
50 #define core_signal(bfd) (core_hdr(bfd)->sig)
51 #define core_command(bfd) (core_hdr(bfd)->cmd)
53 /* Handle Lynx core dump file. */
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;
64 asection *asect;
65 char *newname;
67 newname = bfd_alloc (abfd, strlen (name) + 1);
68 if (!newname)
69 return NULL;
71 strcpy (newname, name);
73 asect = bfd_make_section (abfd, newname);
74 if (!asect)
75 return NULL;
77 asect->flags = flags;
78 asect->_raw_size = _raw_size;
79 asect->vma = vma;
80 asect->filepos = filepos;
81 asect->alignment_power = 2;
83 return asect;
86 const bfd_target *
87 lynx_core_file_p (abfd)
88 bfd *abfd;
90 int val;
91 int secnum;
92 struct pssentry pss;
93 size_t tcontext_size;
94 core_st_t *threadp;
95 int pagesize;
96 asection *newsect;
98 pagesize = getpagesize (); /* Serious cross-target issue here... This
99 really needs to come from a system-specific
100 header file. */
102 /* Get the pss entry from the core file */
104 if (bfd_seek (abfd, 0, SEEK_SET) != 0)
105 return NULL;
107 val = bfd_read ((void *)&pss, 1, sizeof pss, abfd);
108 if (val != sizeof pss)
110 /* Too small to be a core file */
111 if (bfd_get_error () != bfd_error_system_call)
112 bfd_set_error (bfd_error_wrong_format);
113 return NULL;
116 core_hdr (abfd) = (struct lynx_core_struct *)
117 bfd_zalloc (abfd, sizeof (struct lynx_core_struct));
119 if (!core_hdr (abfd))
120 return NULL;
122 strncpy (core_command (abfd), pss.pname, PNMLEN + 1);
124 /* Compute the size of the thread contexts */
126 tcontext_size = pss.threadcnt * sizeof (core_st_t);
128 /* Allocate space for the thread contexts */
130 threadp = (core_st_t *)bfd_alloc (abfd, tcontext_size);
131 if (!threadp)
132 return NULL;
134 /* Save thread contexts */
136 if (bfd_seek (abfd, pagesize, SEEK_SET) != 0)
137 return NULL;
139 val = bfd_read ((void *)threadp, pss.threadcnt, sizeof (core_st_t), abfd);
141 if (val != tcontext_size)
143 /* Probably too small to be a core file */
144 if (bfd_get_error () != bfd_error_system_call)
145 bfd_set_error (bfd_error_wrong_format);
146 return NULL;
149 core_signal (abfd) = threadp->currsig;
151 newsect = make_bfd_asection (abfd, ".stack",
152 SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS,
153 pss.ssize,
154 pss.slimit,
155 pagesize + tcontext_size);
156 if (!newsect)
157 return NULL;
159 newsect = make_bfd_asection (abfd, ".data",
160 SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS,
161 pss.data_len + pss.bss_len,
162 pss.data_start,
163 pagesize + tcontext_size + pss.ssize
164 #if defined (SPARC) || defined (__SPARC__)
165 /* SPARC Lynx seems to start dumping
166 the .data section at a page
167 boundary. It's OK to check a
168 #define like SPARC here because this
169 file can only be compiled on a Lynx
170 host. */
171 + pss.data_start % pagesize
172 #endif
174 if (!newsect)
175 return NULL;
177 /* And, now for the .reg/XXX pseudo sections. Each thread has it's own
178 .reg/XXX section, where XXX is the thread id (without leading zeros). The
179 currently running thread (at the time of the core dump) also has an alias
180 called `.reg' (just to keep GDB happy). Note that we use `.reg/XXX' as
181 opposed to `.regXXX' because GDB expects that .reg2 will be the floating-
182 point registers. */
184 newsect = make_bfd_asection (abfd, ".reg",
185 SEC_HAS_CONTENTS,
186 sizeof (core_st_t),
188 pagesize);
189 if (!newsect)
190 return NULL;
192 for (secnum = 0; secnum < pss.threadcnt; secnum++)
194 char secname[100];
196 sprintf (secname, ".reg/%d", BUILDPID (0, threadp[secnum].tid));
197 newsect = make_bfd_asection (abfd, secname,
198 SEC_HAS_CONTENTS,
199 sizeof (core_st_t),
201 pagesize + secnum * sizeof (core_st_t));
202 if (!newsect)
203 return NULL;
206 return abfd->xvec;
209 char *
210 lynx_core_file_failing_command (abfd)
211 bfd *abfd;
213 return core_command (abfd);
217 lynx_core_file_failing_signal (abfd)
218 bfd *abfd;
220 return core_signal (abfd);
223 boolean
224 lynx_core_file_matches_executable_p (core_bfd, exec_bfd)
225 bfd *core_bfd, *exec_bfd;
227 return true; /* FIXME, We have no way of telling at this point */
230 #endif /* LYNX_CORE */