ptrace-core.c revision 77298
1/* BFD backend for core files which use the ptrace_user structure
2   Copyright 1993, 94, 95, 96, 1998 Free Software Foundation, Inc.
3   The structure of this file is based on trad-core.c written by John Gilmore
4   of Cygnus Support.
5   Modified to work with the ptrace_user structure by Kevin A. Buettner.
6   (Longterm it may be better to merge this file with trad-core.c)
7
8This file is part of BFD, the Binary File Descriptor library.
9
10This program is free software; you can redistribute it and/or modify
11it under the terms of the GNU General Public License as published by
12the Free Software Foundation; either version 2 of the License, or
13(at your option) any later version.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
23
24#ifdef PTRACE_CORE
25
26#include "bfd.h"
27#include "sysdep.h"
28#include "libbfd.h"
29
30#include <sys/param.h>
31#include <sys/dir.h>
32#include <signal.h>
33#include <sys/ptrace.h>
34
35struct trad_core_struct
36  {
37    asection *data_section;
38    asection *stack_section;
39    asection *reg_section;
40    struct ptrace_user u;
41  };
42
43#define core_upage(bfd) (&((bfd)->tdata.trad_core_data->u))
44#define core_datasec(bfd) ((bfd)->tdata.trad_core_data->data_section)
45#define core_stacksec(bfd) ((bfd)->tdata.trad_core_data->stack_section)
46#define core_regsec(bfd) ((bfd)->tdata.trad_core_data->reg_section)
47
48/* forward declarations */
49
50const bfd_target *ptrace_unix_core_file_p PARAMS ((bfd *abfd));
51char *		ptrace_unix_core_file_failing_command PARAMS ((bfd *abfd));
52int		ptrace_unix_core_file_failing_signal PARAMS ((bfd *abfd));
53boolean		ptrace_unix_core_file_matches_executable_p
54			 PARAMS ((bfd *core_bfd, bfd *exec_bfd));
55static void	swap_abort PARAMS ((void));
56
57/* ARGSUSED */
58const bfd_target *
59ptrace_unix_core_file_p (abfd)
60     bfd *abfd;
61
62{
63  int val;
64  struct ptrace_user u;
65  struct trad_core_struct *rawptr;
66
67  val = bfd_read ((void *)&u, 1, sizeof u, abfd);
68  if (val != sizeof u || u.pt_magic != _BCS_PTRACE_MAGIC
69      || u.pt_rev != _BCS_PTRACE_REV)
70    {
71      /* Too small to be a core file */
72      bfd_set_error (bfd_error_wrong_format);
73      return 0;
74    }
75
76  /* OK, we believe you.  You're a core file (sure, sure).  */
77
78  /* Allocate both the upage and the struct core_data at once, so
79     a single free() will free them both.  */
80  rawptr = (struct trad_core_struct *)
81		bfd_zalloc (abfd, sizeof (struct trad_core_struct));
82
83  if (rawptr == NULL)
84    return 0;
85
86  abfd->tdata.trad_core_data = rawptr;
87
88  rawptr->u = u; /*Copy the uarea into the tdata part of the bfd */
89
90  /* Create the sections.  This is raunchy, but bfd_close wants to free
91     them separately.  */
92
93  core_stacksec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
94  if (core_stacksec (abfd) == NULL)
95    return NULL;
96  core_datasec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
97  if (core_datasec (abfd) == NULL)
98    return NULL;
99  core_regsec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
100  if (core_regsec (abfd) == NULL)
101    return NULL;
102
103  core_stacksec (abfd)->name = ".stack";
104  core_datasec (abfd)->name = ".data";
105  core_regsec (abfd)->name = ".reg";
106
107  /* FIXME:  Need to worry about shared memory, library data, and library
108     text.  I don't think that any of these things are supported on the
109     system on which I am developing this for though.  */
110
111  core_stacksec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
112  core_datasec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
113  core_regsec (abfd)->flags = SEC_HAS_CONTENTS;
114
115  core_datasec (abfd)->_raw_size =  u.pt_dsize;
116  core_stacksec (abfd)->_raw_size = u.pt_ssize;
117  core_regsec (abfd)->_raw_size = sizeof (u);
118
119  core_datasec (abfd)->vma = u.pt_o_data_start;
120  core_stacksec (abfd)->vma = USRSTACK - u.pt_ssize;
121  core_regsec (abfd)->vma = 0 - sizeof (u);	/* see trad-core.c */
122
123  core_datasec (abfd)->filepos = (int) u.pt_dataptr;
124  core_stacksec (abfd)->filepos = (int) (u.pt_dataptr + u.pt_dsize);
125  core_regsec (abfd)->filepos = 0; /* Register segment is ptrace_user */
126
127  /* Align to word at least */
128  core_stacksec (abfd)->alignment_power = 2;
129  core_datasec (abfd)->alignment_power = 2;
130  core_regsec (abfd)->alignment_power = 2;
131
132  abfd->sections = core_stacksec (abfd);
133  core_stacksec (abfd)->next = core_datasec (abfd);
134  core_datasec (abfd)->next = core_regsec (abfd);
135  abfd->section_count = 3;
136
137  return abfd->xvec;
138}
139
140char *
141ptrace_unix_core_file_failing_command (abfd)
142     bfd *abfd;
143{
144  char *com = abfd->tdata.trad_core_data->u.pt_comm;
145  if (*com)
146    return com;
147  else
148    return 0;
149}
150
151/* ARGSUSED */
152int
153ptrace_unix_core_file_failing_signal (abfd)
154     bfd *abfd;
155{
156  return abfd->tdata.trad_core_data->u.pt_sigframe.sig_num;
157}
158
159/* ARGSUSED */
160boolean
161ptrace_unix_core_file_matches_executable_p  (core_bfd, exec_bfd)
162     bfd *core_bfd, *exec_bfd;
163{
164  /* FIXME: Use pt_timdat field of the ptrace_user structure to match
165     the date of the executable */
166  return true;
167}
168
169/* If somebody calls any byte-swapping routines, shoot them.  */
170static void
171swap_abort ()
172{
173  abort (); /* This way doesn't require any declaration for ANSI to fuck up */
174}
175#define	NO_GET	((bfd_vma (*) PARAMS ((   const bfd_byte *))) swap_abort )
176#define	NO_PUT	((void    (*) PARAMS ((bfd_vma, bfd_byte *))) swap_abort )
177#define	NO_SIGNED_GET \
178  ((bfd_signed_vma (*) PARAMS ((const bfd_byte *))) swap_abort )
179
180const bfd_target ptrace_core_vec =
181  {
182    "trad-core",
183    bfd_target_unknown_flavour,
184    BFD_ENDIAN_UNKNOWN,		/* target byte order */
185    BFD_ENDIAN_UNKNOWN,		/* target headers byte order */
186    (HAS_RELOC | EXEC_P |	/* object flags */
187     HAS_LINENO | HAS_DEBUG |
188     HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
189    (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
190    0,			                                   /* symbol prefix */
191    ' ',						   /* ar_pad_char */
192    16,							   /* ar_max_namelen */
193    NO_GET, NO_SIGNED_GET, NO_PUT,	/* 64 bit data */
194    NO_GET, NO_SIGNED_GET, NO_PUT,	/* 32 bit data */
195    NO_GET, NO_SIGNED_GET, NO_PUT,	/* 16 bit data */
196    NO_GET, NO_SIGNED_GET, NO_PUT,	/* 64 bit hdrs */
197    NO_GET, NO_SIGNED_GET, NO_PUT,	/* 32 bit hdrs */
198    NO_GET, NO_SIGNED_GET, NO_PUT,	/* 16 bit hdrs */
199
200    {				/* bfd_check_format */
201     _bfd_dummy_target,		/* unknown format */
202     _bfd_dummy_target,		/* object file */
203     _bfd_dummy_target,		/* archive */
204     ptrace_unix_core_file_p	/* a core file */
205    },
206    {				/* bfd_set_format */
207     bfd_false, bfd_false,
208     bfd_false, bfd_false
209    },
210    {				/* bfd_write_contents */
211     bfd_false, bfd_false,
212     bfd_false, bfd_false
213    },
214
215       BFD_JUMP_TABLE_GENERIC (_bfd_generic),
216       BFD_JUMP_TABLE_COPY (_bfd_generic),
217       BFD_JUMP_TABLE_CORE (ptrace_unix),
218       BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
219       BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
220       BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
221       BFD_JUMP_TABLE_WRITE (_bfd_generic),
222       BFD_JUMP_TABLE_LINK (_bfd_nolink),
223       BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
224
225    NULL,
226
227    (PTR) 0			/* backend_data */
228};
229
230#endif /* PTRACE_CORE */
231