* elf32-spu.c (build_stub): Fix malloc under-allocation.
[binutils.git] / bfd / lynx-core.c
blobab6a834014cd98d62e6efbd53efd48a0b0538478
1 /* BFD back end for Lynx core files
2 Copyright 1993, 1994, 1995, 2001, 2002, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Written by Stu Grossman of Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
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 3 of the License, or
11 (at your option) any later version.
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.
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., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "libbfd.h"
27 #ifdef LYNX_CORE
29 #include <sys/conf.h>
30 #include <sys/kernel.h>
31 /* sys/kernel.h should define this, but doesn't always, sigh. */
32 #ifndef __LYNXOS
33 #define __LYNXOS
34 #endif
35 #include <sys/mem.h>
36 #include <sys/signal.h>
37 #include <sys/time.h>
38 #include <sys/resource.h>
39 #include <sys/itimer.h>
40 #include <sys/file.h>
41 #include <sys/proc.h>
43 /* These are stored in the bfd's tdata */
45 struct lynx_core_struct
47 int sig;
48 char cmd[PNMLEN + 1];
51 #define core_hdr(bfd) ((bfd)->tdata.lynx_core_data)
52 #define core_signal(bfd) (core_hdr(bfd)->sig)
53 #define core_command(bfd) (core_hdr(bfd)->cmd)
55 #define lynx_core_file_matches_executable_p generic_core_file_matches_executable_p
56 #define lynx_core_file_pid _bfd_nocore_core_file_pid
58 /* Handle Lynx core dump file. */
60 static asection *
61 make_bfd_asection (abfd, name, flags, size, vma, filepos)
62 bfd *abfd;
63 const char *name;
64 flagword flags;
65 bfd_size_type size;
66 bfd_vma vma;
67 file_ptr filepos;
69 asection *asect;
70 char *newname;
72 newname = bfd_alloc (abfd, (bfd_size_type) strlen (name) + 1);
73 if (!newname)
74 return NULL;
76 strcpy (newname, name);
78 asect = bfd_make_section_with_flags (abfd, newname, flags);
79 if (!asect)
80 return NULL;
82 asect->size = size;
83 asect->vma = vma;
84 asect->filepos = filepos;
85 asect->alignment_power = 2;
87 return asect;
90 const bfd_target *
91 lynx_core_file_p (abfd)
92 bfd *abfd;
94 int secnum;
95 struct pssentry pss;
96 bfd_size_type tcontext_size;
97 core_st_t *threadp;
98 int pagesize;
99 asection *newsect;
100 bfd_size_type amt;
102 pagesize = getpagesize (); /* Serious cross-target issue here... This
103 really needs to come from a system-specific
104 header file. */
106 /* Get the pss entry from the core file */
108 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
109 return NULL;
111 amt = sizeof pss;
112 if (bfd_bread ((void *) &pss, amt, abfd) != amt)
114 /* Too small to be a core file */
115 if (bfd_get_error () != bfd_error_system_call)
116 bfd_set_error (bfd_error_wrong_format);
117 return NULL;
120 amt = sizeof (struct lynx_core_struct);
121 core_hdr (abfd) = (struct lynx_core_struct *) bfd_zalloc (abfd, amt);
123 if (!core_hdr (abfd))
124 return NULL;
126 strncpy (core_command (abfd), pss.pname, PNMLEN + 1);
128 /* Compute the size of the thread contexts */
130 tcontext_size = pss.threadcnt * sizeof (core_st_t);
132 /* Allocate space for the thread contexts */
134 threadp = (core_st_t *) bfd_alloc (abfd, tcontext_size);
135 if (!threadp)
136 goto fail;
138 /* Save thread contexts */
140 if (bfd_seek (abfd, (file_ptr) pagesize, SEEK_SET) != 0)
141 goto fail;
143 if (bfd_bread ((void *) threadp, tcontext_size, abfd) != tcontext_size)
145 /* Probably too small to be a core file */
146 if (bfd_get_error () != bfd_error_system_call)
147 bfd_set_error (bfd_error_wrong_format);
148 goto fail;
151 core_signal (abfd) = threadp->currsig;
153 newsect = make_bfd_asection (abfd, ".stack",
154 SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS,
155 pss.ssize,
156 pss.slimit,
157 pagesize + tcontext_size);
158 if (!newsect)
159 goto fail;
161 newsect = make_bfd_asection (abfd, ".data",
162 SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS,
163 pss.data_len + pss.bss_len,
164 pss.data_start,
165 pagesize + tcontext_size + pss.ssize
166 #if defined (SPARC) || defined (__SPARC__)
167 /* SPARC Lynx seems to start dumping
168 the .data section at a page
169 boundary. It's OK to check a
170 #define like SPARC here because this
171 file can only be compiled on a Lynx
172 host. */
173 + pss.data_start % pagesize
174 #endif
176 if (!newsect)
177 goto fail;
179 /* And, now for the .reg/XXX pseudo sections. Each thread has it's own
180 .reg/XXX section, where XXX is the thread id (without leading zeros). The
181 currently running thread (at the time of the core dump) also has an alias
182 called `.reg' (just to keep GDB happy). Note that we use `.reg/XXX' as
183 opposed to `.regXXX' because GDB expects that .reg2 will be the floating-
184 point registers. */
186 newsect = make_bfd_asection (abfd, ".reg",
187 SEC_HAS_CONTENTS,
188 sizeof (core_st_t),
190 pagesize);
191 if (!newsect)
192 goto fail;
194 for (secnum = 0; secnum < pss.threadcnt; secnum++)
196 char secname[100];
198 sprintf (secname, ".reg/%d", BUILDPID (0, threadp[secnum].tid));
199 newsect = make_bfd_asection (abfd, secname,
200 SEC_HAS_CONTENTS,
201 sizeof (core_st_t),
203 pagesize + secnum * sizeof (core_st_t));
204 if (!newsect)
205 goto fail;
208 return abfd->xvec;
210 fail:
211 bfd_release (abfd, core_hdr (abfd));
212 core_hdr (abfd) = NULL;
213 bfd_section_list_clear (abfd);
214 return NULL;
217 char *
218 lynx_core_file_failing_command (abfd)
219 bfd *abfd;
221 return core_command (abfd);
225 lynx_core_file_failing_signal (abfd)
226 bfd *abfd;
228 return core_signal (abfd);
231 #endif /* LYNX_CORE */