daily update
[binutils.git] / bfd / ptrace-core.c
blob4f441ce6c90c472c337381870ac20f03fe3c4686
1 /* BFD backend for core files which use the ptrace_user structure
2 Copyright 1993, 1994, 1995, 1996, 1998, 1999, 2001, 2002
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)
9 This file is part of BFD, the Binary File Descriptor library.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #ifdef PTRACE_CORE
27 #include "bfd.h"
28 #include "sysdep.h"
29 #include "libbfd.h"
31 #include <sys/param.h>
32 #include <sys/dir.h>
33 #include <signal.h>
34 #include <sys/ptrace.h>
36 struct trad_core_struct
38 asection *data_section;
39 asection *stack_section;
40 asection *reg_section;
41 struct ptrace_user u;
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)
49 /* forward declarations */
51 const bfd_target *ptrace_unix_core_file_p PARAMS ((bfd *abfd));
52 char * ptrace_unix_core_file_failing_command PARAMS ((bfd *abfd));
53 int ptrace_unix_core_file_failing_signal PARAMS ((bfd *abfd));
54 bfd_boolean ptrace_unix_core_file_matches_executable_p
55 PARAMS ((bfd *core_bfd, bfd *exec_bfd));
56 static void swap_abort PARAMS ((void));
58 const bfd_target *
59 ptrace_unix_core_file_p (abfd)
60 bfd *abfd;
63 int val;
64 struct ptrace_user u;
65 struct trad_core_struct *rawptr;
66 bfd_size_type amt;
68 val = bfd_bread ((void *)&u, (bfd_size_type) sizeof u, abfd);
69 if (val != sizeof u || u.pt_magic != _BCS_PTRACE_MAGIC
70 || u.pt_rev != _BCS_PTRACE_REV)
72 /* Too small to be a core file */
73 bfd_set_error (bfd_error_wrong_format);
74 return 0;
77 /* OK, we believe you. You're a core file (sure, sure). */
79 /* Allocate both the upage and the struct core_data at once, so
80 a single free() will free them both. */
81 amt = sizeof (struct trad_core_struct);
82 rawptr = (struct trad_core_struct *) bfd_zalloc (abfd, amt);
84 if (rawptr == NULL)
85 return 0;
87 abfd->tdata.trad_core_data = rawptr;
89 rawptr->u = u; /*Copy the uarea into the tdata part of the bfd */
91 /* Create the sections. */
93 core_stacksec (abfd) = bfd_make_section_anyway (abfd, ".stack");
94 if (core_stacksec (abfd) == NULL)
95 goto fail;
96 core_datasec (abfd) = bfd_make_section_anyway (abfd, ".data");
97 if (core_datasec (abfd) == NULL)
98 goto fail;
99 core_regsec (abfd) = bfd_make_section_anyway (abfd, ".reg");
100 if (core_regsec (abfd) == NULL)
101 goto fail;
103 /* FIXME: Need to worry about shared memory, library data, and library
104 text. I don't think that any of these things are supported on the
105 system on which I am developing this for though. */
107 core_stacksec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
108 core_datasec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
109 core_regsec (abfd)->flags = SEC_HAS_CONTENTS;
111 core_datasec (abfd)->_raw_size = u.pt_dsize;
112 core_stacksec (abfd)->_raw_size = u.pt_ssize;
113 core_regsec (abfd)->_raw_size = sizeof (u);
115 core_datasec (abfd)->vma = u.pt_o_data_start;
116 core_stacksec (abfd)->vma = USRSTACK - u.pt_ssize;
117 core_regsec (abfd)->vma = 0 - sizeof (u); /* see trad-core.c */
119 core_datasec (abfd)->filepos = (int) u.pt_dataptr;
120 core_stacksec (abfd)->filepos = (int) (u.pt_dataptr + u.pt_dsize);
121 core_regsec (abfd)->filepos = 0; /* Register segment is ptrace_user */
123 /* Align to word at least */
124 core_stacksec (abfd)->alignment_power = 2;
125 core_datasec (abfd)->alignment_power = 2;
126 core_regsec (abfd)->alignment_power = 2;
128 return abfd->xvec;
130 fail:
131 bfd_release (abfd, abfd->tdata.any);
132 abfd->tdata.any = NULL;
133 bfd_section_list_clear (abfd);
134 return NULL;
137 char *
138 ptrace_unix_core_file_failing_command (abfd)
139 bfd *abfd;
141 char *com = abfd->tdata.trad_core_data->u.pt_comm;
142 if (*com)
143 return com;
144 else
145 return 0;
149 ptrace_unix_core_file_failing_signal (abfd)
150 bfd *abfd;
152 return abfd->tdata.trad_core_data->u.pt_sigframe.sig_num;
155 bfd_boolean
156 ptrace_unix_core_file_matches_executable_p (core_bfd, exec_bfd)
157 bfd *core_bfd, *exec_bfd;
159 /* FIXME: Use pt_timdat field of the ptrace_user structure to match
160 the date of the executable */
161 return TRUE;
164 /* If somebody calls any byte-swapping routines, shoot them. */
165 static void
166 swap_abort ()
168 abort (); /* This way doesn't require any declaration for ANSI to fuck up */
170 #define NO_GET ((bfd_vma (*) PARAMS (( const bfd_byte *))) swap_abort )
171 #define NO_PUT ((void (*) PARAMS ((bfd_vma, bfd_byte *))) swap_abort )
172 #define NO_SIGNED_GET \
173 ((bfd_signed_vma (*) PARAMS ((const bfd_byte *))) swap_abort )
175 const bfd_target ptrace_core_vec =
177 "trad-core",
178 bfd_target_unknown_flavour,
179 BFD_ENDIAN_UNKNOWN, /* target byte order */
180 BFD_ENDIAN_UNKNOWN, /* target headers byte order */
181 (HAS_RELOC | EXEC_P | /* object flags */
182 HAS_LINENO | HAS_DEBUG |
183 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
184 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
185 0, /* symbol prefix */
186 ' ', /* ar_pad_char */
187 16, /* ar_max_namelen */
188 NO_GET, NO_SIGNED_GET, NO_PUT, /* 64 bit data */
189 NO_GET, NO_SIGNED_GET, NO_PUT, /* 32 bit data */
190 NO_GET, NO_SIGNED_GET, NO_PUT, /* 16 bit data */
191 NO_GET, NO_SIGNED_GET, NO_PUT, /* 64 bit hdrs */
192 NO_GET, NO_SIGNED_GET, NO_PUT, /* 32 bit hdrs */
193 NO_GET, NO_SIGNED_GET, NO_PUT, /* 16 bit hdrs */
195 { /* bfd_check_format */
196 _bfd_dummy_target, /* unknown format */
197 _bfd_dummy_target, /* object file */
198 _bfd_dummy_target, /* archive */
199 ptrace_unix_core_file_p /* a core file */
201 { /* bfd_set_format */
202 bfd_false, bfd_false,
203 bfd_false, bfd_false
205 { /* bfd_write_contents */
206 bfd_false, bfd_false,
207 bfd_false, bfd_false
210 BFD_JUMP_TABLE_GENERIC (_bfd_generic),
211 BFD_JUMP_TABLE_COPY (_bfd_generic),
212 BFD_JUMP_TABLE_CORE (ptrace_unix),
213 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
214 BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
215 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
216 BFD_JUMP_TABLE_WRITE (_bfd_generic),
217 BFD_JUMP_TABLE_LINK (_bfd_nolink),
218 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
220 NULL,
222 (PTR) 0 /* backend_data */
225 #endif /* PTRACE_CORE */