1*3d8817e4Smiod /* BFD back end for Lynx core files
2*3d8817e4Smiod    Copyright 1993, 1994, 1995, 2001, 2002, 2004
3*3d8817e4Smiod    Free Software Foundation, Inc.
4*3d8817e4Smiod    Written by Stu Grossman of Cygnus Support.
5*3d8817e4Smiod 
6*3d8817e4Smiod This file is part of BFD, the Binary File Descriptor library.
7*3d8817e4Smiod 
8*3d8817e4Smiod This program is free software; you can redistribute it and/or modify
9*3d8817e4Smiod it under the terms of the GNU General Public License as published by
10*3d8817e4Smiod the Free Software Foundation; either version 2 of the License, or
11*3d8817e4Smiod (at your option) any later version.
12*3d8817e4Smiod 
13*3d8817e4Smiod This program is distributed in the hope that it will be useful,
14*3d8817e4Smiod but WITHOUT ANY WARRANTY; without even the implied warranty of
15*3d8817e4Smiod MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*3d8817e4Smiod GNU General Public License for more details.
17*3d8817e4Smiod 
18*3d8817e4Smiod You should have received a copy of the GNU General Public License
19*3d8817e4Smiod along with this program; if not, write to the Free Software
20*3d8817e4Smiod Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
21*3d8817e4Smiod 
22*3d8817e4Smiod #include "bfd.h"
23*3d8817e4Smiod #include "sysdep.h"
24*3d8817e4Smiod #include "libbfd.h"
25*3d8817e4Smiod 
26*3d8817e4Smiod #ifdef LYNX_CORE
27*3d8817e4Smiod 
28*3d8817e4Smiod #include <sys/conf.h>
29*3d8817e4Smiod #include <sys/kernel.h>
30*3d8817e4Smiod /* sys/kernel.h should define this, but doesn't always, sigh.  */
31*3d8817e4Smiod #ifndef __LYNXOS
32*3d8817e4Smiod #define __LYNXOS
33*3d8817e4Smiod #endif
34*3d8817e4Smiod #include <sys/mem.h>
35*3d8817e4Smiod #include <sys/signal.h>
36*3d8817e4Smiod #include <sys/time.h>
37*3d8817e4Smiod #include <sys/resource.h>
38*3d8817e4Smiod #include <sys/itimer.h>
39*3d8817e4Smiod #include <sys/file.h>
40*3d8817e4Smiod #include <sys/proc.h>
41*3d8817e4Smiod 
42*3d8817e4Smiod /* These are stored in the bfd's tdata */
43*3d8817e4Smiod 
44*3d8817e4Smiod struct lynx_core_struct
45*3d8817e4Smiod {
46*3d8817e4Smiod   int sig;
47*3d8817e4Smiod   char cmd[PNMLEN + 1];
48*3d8817e4Smiod };
49*3d8817e4Smiod 
50*3d8817e4Smiod #define core_hdr(bfd) ((bfd)->tdata.lynx_core_data)
51*3d8817e4Smiod #define core_signal(bfd) (core_hdr(bfd)->sig)
52*3d8817e4Smiod #define core_command(bfd) (core_hdr(bfd)->cmd)
53*3d8817e4Smiod 
54*3d8817e4Smiod #define lynx_core_file_matches_executable_p generic_core_file_matches_executable_p
55*3d8817e4Smiod 
56*3d8817e4Smiod /* Handle Lynx core dump file.  */
57*3d8817e4Smiod 
58*3d8817e4Smiod static asection *
make_bfd_asection(abfd,name,flags,size,vma,filepos)59*3d8817e4Smiod make_bfd_asection (abfd, name, flags, size, vma, filepos)
60*3d8817e4Smiod      bfd *abfd;
61*3d8817e4Smiod      const char *name;
62*3d8817e4Smiod      flagword flags;
63*3d8817e4Smiod      bfd_size_type size;
64*3d8817e4Smiod      bfd_vma vma;
65*3d8817e4Smiod      file_ptr filepos;
66*3d8817e4Smiod {
67*3d8817e4Smiod   asection *asect;
68*3d8817e4Smiod   char *newname;
69*3d8817e4Smiod 
70*3d8817e4Smiod   newname = bfd_alloc (abfd, (bfd_size_type) strlen (name) + 1);
71*3d8817e4Smiod   if (!newname)
72*3d8817e4Smiod     return NULL;
73*3d8817e4Smiod 
74*3d8817e4Smiod   strcpy (newname, name);
75*3d8817e4Smiod 
76*3d8817e4Smiod   asect = bfd_make_section (abfd, newname);
77*3d8817e4Smiod   if (!asect)
78*3d8817e4Smiod     return NULL;
79*3d8817e4Smiod 
80*3d8817e4Smiod   asect->flags = flags;
81*3d8817e4Smiod   asect->size = size;
82*3d8817e4Smiod   asect->vma = vma;
83*3d8817e4Smiod   asect->filepos = filepos;
84*3d8817e4Smiod   asect->alignment_power = 2;
85*3d8817e4Smiod 
86*3d8817e4Smiod   return asect;
87*3d8817e4Smiod }
88*3d8817e4Smiod 
89*3d8817e4Smiod const bfd_target *
lynx_core_file_p(abfd)90*3d8817e4Smiod lynx_core_file_p (abfd)
91*3d8817e4Smiod      bfd *abfd;
92*3d8817e4Smiod {
93*3d8817e4Smiod   int secnum;
94*3d8817e4Smiod   struct pssentry pss;
95*3d8817e4Smiod   bfd_size_type tcontext_size;
96*3d8817e4Smiod   core_st_t *threadp;
97*3d8817e4Smiod   int pagesize;
98*3d8817e4Smiod   asection *newsect;
99*3d8817e4Smiod   bfd_size_type amt;
100*3d8817e4Smiod 
101*3d8817e4Smiod   pagesize = getpagesize ();	/* Serious cross-target issue here...  This
102*3d8817e4Smiod 				   really needs to come from a system-specific
103*3d8817e4Smiod 				   header file.  */
104*3d8817e4Smiod 
105*3d8817e4Smiod   /* Get the pss entry from the core file */
106*3d8817e4Smiod 
107*3d8817e4Smiod   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
108*3d8817e4Smiod     return NULL;
109*3d8817e4Smiod 
110*3d8817e4Smiod   amt = sizeof pss;
111*3d8817e4Smiod   if (bfd_bread ((void *) &pss, amt, abfd) != amt)
112*3d8817e4Smiod     {
113*3d8817e4Smiod       /* Too small to be a core file */
114*3d8817e4Smiod       if (bfd_get_error () != bfd_error_system_call)
115*3d8817e4Smiod 	bfd_set_error (bfd_error_wrong_format);
116*3d8817e4Smiod       return NULL;
117*3d8817e4Smiod     }
118*3d8817e4Smiod 
119*3d8817e4Smiod   amt = sizeof (struct lynx_core_struct);
120*3d8817e4Smiod   core_hdr (abfd) = (struct lynx_core_struct *) bfd_zalloc (abfd, amt);
121*3d8817e4Smiod 
122*3d8817e4Smiod   if (!core_hdr (abfd))
123*3d8817e4Smiod     return NULL;
124*3d8817e4Smiod 
125*3d8817e4Smiod   strncpy (core_command (abfd), pss.pname, PNMLEN + 1);
126*3d8817e4Smiod 
127*3d8817e4Smiod   /* Compute the size of the thread contexts */
128*3d8817e4Smiod 
129*3d8817e4Smiod   tcontext_size = pss.threadcnt * sizeof (core_st_t);
130*3d8817e4Smiod 
131*3d8817e4Smiod   /* Allocate space for the thread contexts */
132*3d8817e4Smiod 
133*3d8817e4Smiod   threadp = (core_st_t *) bfd_alloc (abfd, tcontext_size);
134*3d8817e4Smiod   if (!threadp)
135*3d8817e4Smiod     goto fail;
136*3d8817e4Smiod 
137*3d8817e4Smiod   /* Save thread contexts */
138*3d8817e4Smiod 
139*3d8817e4Smiod   if (bfd_seek (abfd, (file_ptr) pagesize, SEEK_SET) != 0)
140*3d8817e4Smiod     goto fail;
141*3d8817e4Smiod 
142*3d8817e4Smiod   if (bfd_bread ((void *) threadp, tcontext_size, abfd) != tcontext_size)
143*3d8817e4Smiod     {
144*3d8817e4Smiod       /* Probably too small to be a core file */
145*3d8817e4Smiod       if (bfd_get_error () != bfd_error_system_call)
146*3d8817e4Smiod 	bfd_set_error (bfd_error_wrong_format);
147*3d8817e4Smiod       goto fail;
148*3d8817e4Smiod     }
149*3d8817e4Smiod 
150*3d8817e4Smiod   core_signal (abfd) = threadp->currsig;
151*3d8817e4Smiod 
152*3d8817e4Smiod   newsect = make_bfd_asection (abfd, ".stack",
153*3d8817e4Smiod 			       SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS,
154*3d8817e4Smiod 			       pss.ssize,
155*3d8817e4Smiod 			       pss.slimit,
156*3d8817e4Smiod 			       pagesize + tcontext_size);
157*3d8817e4Smiod   if (!newsect)
158*3d8817e4Smiod     goto fail;
159*3d8817e4Smiod 
160*3d8817e4Smiod   newsect = make_bfd_asection (abfd, ".data",
161*3d8817e4Smiod 			       SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS,
162*3d8817e4Smiod 			       pss.data_len + pss.bss_len,
163*3d8817e4Smiod 			       pss.data_start,
164*3d8817e4Smiod 			       pagesize + tcontext_size + pss.ssize
165*3d8817e4Smiod #if defined (SPARC) || defined (__SPARC__)
166*3d8817e4Smiod 			       /* SPARC Lynx seems to start dumping
167*3d8817e4Smiod                                   the .data section at a page
168*3d8817e4Smiod                                   boundary.  It's OK to check a
169*3d8817e4Smiod                                   #define like SPARC here because this
170*3d8817e4Smiod                                   file can only be compiled on a Lynx
171*3d8817e4Smiod                                   host.  */
172*3d8817e4Smiod 			       + pss.data_start % pagesize
173*3d8817e4Smiod #endif
174*3d8817e4Smiod 			       );
175*3d8817e4Smiod   if (!newsect)
176*3d8817e4Smiod     goto fail;
177*3d8817e4Smiod 
178*3d8817e4Smiod /* And, now for the .reg/XXX pseudo sections.  Each thread has it's own
179*3d8817e4Smiod    .reg/XXX section, where XXX is the thread id (without leading zeros).  The
180*3d8817e4Smiod    currently running thread (at the time of the core dump) also has an alias
181*3d8817e4Smiod    called `.reg' (just to keep GDB happy).  Note that we use `.reg/XXX' as
182*3d8817e4Smiod    opposed to `.regXXX' because GDB expects that .reg2 will be the floating-
183*3d8817e4Smiod    point registers.  */
184*3d8817e4Smiod 
185*3d8817e4Smiod   newsect = make_bfd_asection (abfd, ".reg",
186*3d8817e4Smiod 			       SEC_HAS_CONTENTS,
187*3d8817e4Smiod 			       sizeof (core_st_t),
188*3d8817e4Smiod 			       0,
189*3d8817e4Smiod 			       pagesize);
190*3d8817e4Smiod   if (!newsect)
191*3d8817e4Smiod     goto fail;
192*3d8817e4Smiod 
193*3d8817e4Smiod   for (secnum = 0; secnum < pss.threadcnt; secnum++)
194*3d8817e4Smiod     {
195*3d8817e4Smiod       char secname[100];
196*3d8817e4Smiod 
197*3d8817e4Smiod       sprintf (secname, ".reg/%d", BUILDPID (0, threadp[secnum].tid));
198*3d8817e4Smiod       newsect = make_bfd_asection (abfd, secname,
199*3d8817e4Smiod 				   SEC_HAS_CONTENTS,
200*3d8817e4Smiod 				   sizeof (core_st_t),
201*3d8817e4Smiod 				   0,
202*3d8817e4Smiod 				   pagesize + secnum * sizeof (core_st_t));
203*3d8817e4Smiod       if (!newsect)
204*3d8817e4Smiod 	goto fail;
205*3d8817e4Smiod     }
206*3d8817e4Smiod 
207*3d8817e4Smiod   return abfd->xvec;
208*3d8817e4Smiod 
209*3d8817e4Smiod  fail:
210*3d8817e4Smiod   bfd_release (abfd, core_hdr (abfd));
211*3d8817e4Smiod   core_hdr (abfd) = NULL;
212*3d8817e4Smiod   bfd_section_list_clear (abfd);
213*3d8817e4Smiod   return NULL;
214*3d8817e4Smiod }
215*3d8817e4Smiod 
216*3d8817e4Smiod char *
lynx_core_file_failing_command(abfd)217*3d8817e4Smiod lynx_core_file_failing_command (abfd)
218*3d8817e4Smiod      bfd *abfd;
219*3d8817e4Smiod {
220*3d8817e4Smiod   return core_command (abfd);
221*3d8817e4Smiod }
222*3d8817e4Smiod 
223*3d8817e4Smiod int
lynx_core_file_failing_signal(abfd)224*3d8817e4Smiod lynx_core_file_failing_signal (abfd)
225*3d8817e4Smiod      bfd *abfd;
226*3d8817e4Smiod {
227*3d8817e4Smiod   return core_signal (abfd);
228*3d8817e4Smiod }
229*3d8817e4Smiod 
230*3d8817e4Smiod #endif /* LYNX_CORE */
231