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