1/* BFD backend for core files which use the ptrace_user structure
2   Copyright 1993, 1994, 1995, 1996, 1998, 1999, 2001, 2002, 2003, 2004
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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, 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));
54#define ptrace_unix_core_file_matches_executable_p generic_core_file_matches_executable_p
55static void swap_abort PARAMS ((void));
56
57const bfd_target *
58ptrace_unix_core_file_p (abfd)
59     bfd *abfd;
60
61{
62  int val;
63  struct ptrace_user u;
64  struct trad_core_struct *rawptr;
65  bfd_size_type amt;
66
67  val = bfd_bread ((void *)&u, (bfd_size_type) 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  amt = sizeof (struct trad_core_struct);
81  rawptr = (struct trad_core_struct *) bfd_zalloc (abfd, amt);
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.  */
91
92  core_stacksec (abfd) = bfd_make_section_anyway (abfd, ".stack");
93  if (core_stacksec (abfd) == NULL)
94    goto fail;
95  core_datasec (abfd) = bfd_make_section_anyway (abfd, ".data");
96  if (core_datasec (abfd) == NULL)
97    goto fail;
98  core_regsec (abfd) = bfd_make_section_anyway (abfd, ".reg");
99  if (core_regsec (abfd) == NULL)
100    goto fail;
101
102  /* FIXME:  Need to worry about shared memory, library data, and library
103     text.  I don't think that any of these things are supported on the
104     system on which I am developing this for though.  */
105
106  core_stacksec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
107  core_datasec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
108  core_regsec (abfd)->flags = SEC_HAS_CONTENTS;
109
110  core_datasec (abfd)->size =  u.pt_dsize;
111  core_stacksec (abfd)->size = u.pt_ssize;
112  core_regsec (abfd)->size = sizeof (u);
113
114  core_datasec (abfd)->vma = u.pt_o_data_start;
115  core_stacksec (abfd)->vma = USRSTACK - u.pt_ssize;
116  core_regsec (abfd)->vma = 0 - sizeof (u);	/* see trad-core.c */
117
118  core_datasec (abfd)->filepos = (int) u.pt_dataptr;
119  core_stacksec (abfd)->filepos = (int) (u.pt_dataptr + u.pt_dsize);
120  core_regsec (abfd)->filepos = 0; /* Register segment is ptrace_user */
121
122  /* Align to word at least */
123  core_stacksec (abfd)->alignment_power = 2;
124  core_datasec (abfd)->alignment_power = 2;
125  core_regsec (abfd)->alignment_power = 2;
126
127  return abfd->xvec;
128
129 fail:
130  bfd_release (abfd, abfd->tdata.any);
131  abfd->tdata.any = NULL;
132  bfd_section_list_clear (abfd);
133  return NULL;
134}
135
136char *
137ptrace_unix_core_file_failing_command (abfd)
138     bfd *abfd;
139{
140  char *com = abfd->tdata.trad_core_data->u.pt_comm;
141  if (*com)
142    return com;
143  else
144    return 0;
145}
146
147int
148ptrace_unix_core_file_failing_signal (abfd)
149     bfd *abfd;
150{
151  return abfd->tdata.trad_core_data->u.pt_sigframe.sig_num;
152}
153
154/* If somebody calls any byte-swapping routines, shoot them.  */
155static void
156swap_abort ()
157{
158  abort (); /* This way doesn't require any declaration for ANSI to fuck up */
159}
160
161#define	NO_GET ((bfd_vma (*) (const void *)) swap_abort)
162#define	NO_PUT ((void (*) (bfd_vma, void *)) swap_abort)
163#define	NO_GETS ((bfd_signed_vma (*) (const void *)) swap_abort)
164#define	NO_GET64 ((bfd_uint64_t (*) (const void *)) swap_abort)
165#define	NO_PUT64 ((void (*) (bfd_uint64_t, void *)) swap_abort)
166#define	NO_GETS64 ((bfd_int64_t (*) (const void *)) swap_abort)
167
168const bfd_target ptrace_core_vec =
169  {
170    "trad-core",
171    bfd_target_unknown_flavour,
172    BFD_ENDIAN_UNKNOWN,		/* target byte order */
173    BFD_ENDIAN_UNKNOWN,		/* target headers byte order */
174    (HAS_RELOC | EXEC_P |	/* object flags */
175     HAS_LINENO | HAS_DEBUG |
176     HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
177    (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
178    0,			                                   /* symbol prefix */
179    ' ',						   /* ar_pad_char */
180    16,							   /* ar_max_namelen */
181    NO_GET64, NO_GETS64, NO_PUT64,	/* 64 bit data */
182    NO_GET, NO_GETS, NO_PUT,		/* 32 bit data */
183    NO_GET, NO_GETS, NO_PUT,		/* 16 bit data */
184    NO_GET64, NO_GETS64, NO_PUT64,	/* 64 bit hdrs */
185    NO_GET, NO_GETS, NO_PUT,		/* 32 bit hdrs */
186    NO_GET, NO_GETS, NO_PUT,		/* 16 bit hdrs */
187
188    {				/* bfd_check_format */
189      _bfd_dummy_target,		/* unknown format */
190      _bfd_dummy_target,		/* object file */
191      _bfd_dummy_target,		/* archive */
192      ptrace_unix_core_file_p		/* a core file */
193    },
194    {				/* bfd_set_format */
195      bfd_false, bfd_false,
196      bfd_false, bfd_false
197    },
198    {				/* bfd_write_contents */
199      bfd_false, bfd_false,
200      bfd_false, bfd_false
201    },
202
203    BFD_JUMP_TABLE_GENERIC (_bfd_generic),
204    BFD_JUMP_TABLE_COPY (_bfd_generic),
205    BFD_JUMP_TABLE_CORE (ptrace_unix),
206    BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
207    BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
208    BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
209    BFD_JUMP_TABLE_WRITE (_bfd_generic),
210    BFD_JUMP_TABLE_LINK (_bfd_nolink),
211    BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
212
213    NULL,
214
215    (PTR) 0			/* backend_data */
216  };
217
218#endif /* PTRACE_CORE */
219