daily update
[binutils.git] / bfd / ptrace-core.c
blob998b1b487f1cdf002f4298f022eda612b9a74a96
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)
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 boolean ptrace_unix_core_file_matches_executable_p
55 PARAMS ((bfd *core_bfd, bfd *exec_bfd));
56 static void swap_abort PARAMS ((void));
58 /* ARGSUSED */
59 const bfd_target *
60 ptrace_unix_core_file_p (abfd)
61 bfd *abfd;
64 int val;
65 struct ptrace_user u;
66 struct trad_core_struct *rawptr;
67 bfd_size_type amt;
69 val = bfd_bread ((void *)&u, (bfd_size_type) sizeof u, abfd);
70 if (val != sizeof u || u.pt_magic != _BCS_PTRACE_MAGIC
71 || u.pt_rev != _BCS_PTRACE_REV)
73 /* Too small to be a core file */
74 bfd_set_error (bfd_error_wrong_format);
75 return 0;
78 /* OK, we believe you. You're a core file (sure, sure). */
80 /* Allocate both the upage and the struct core_data at once, so
81 a single free() will free them both. */
82 amt = sizeof (struct trad_core_struct);
83 rawptr = (struct trad_core_struct *) bfd_zalloc (abfd, amt);
85 if (rawptr == NULL)
86 return 0;
88 abfd->tdata.trad_core_data = rawptr;
90 rawptr->u = u; /*Copy the uarea into the tdata part of the bfd */
92 /* Create the sections. This is raunchy, but bfd_close wants to free
93 them separately. */
95 amt = sizeof (asection);
96 core_stacksec (abfd) = (asection *) bfd_zalloc (abfd, amt);
97 if (core_stacksec (abfd) == NULL)
98 return NULL;
99 core_datasec (abfd) = (asection *) bfd_zalloc (abfd, amt);
100 if (core_datasec (abfd) == NULL)
101 return NULL;
102 core_regsec (abfd) = (asection *) bfd_zalloc (abfd, amt);
103 if (core_regsec (abfd) == NULL)
104 return NULL;
106 core_stacksec (abfd)->name = ".stack";
107 core_datasec (abfd)->name = ".data";
108 core_regsec (abfd)->name = ".reg";
110 /* FIXME: Need to worry about shared memory, library data, and library
111 text. I don't think that any of these things are supported on the
112 system on which I am developing this for though. */
114 core_stacksec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
115 core_datasec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
116 core_regsec (abfd)->flags = SEC_HAS_CONTENTS;
118 core_datasec (abfd)->_raw_size = u.pt_dsize;
119 core_stacksec (abfd)->_raw_size = u.pt_ssize;
120 core_regsec (abfd)->_raw_size = sizeof (u);
122 core_datasec (abfd)->vma = u.pt_o_data_start;
123 core_stacksec (abfd)->vma = USRSTACK - u.pt_ssize;
124 core_regsec (abfd)->vma = 0 - sizeof (u); /* see trad-core.c */
126 core_datasec (abfd)->filepos = (int) u.pt_dataptr;
127 core_stacksec (abfd)->filepos = (int) (u.pt_dataptr + u.pt_dsize);
128 core_regsec (abfd)->filepos = 0; /* Register segment is ptrace_user */
130 /* Align to word at least */
131 core_stacksec (abfd)->alignment_power = 2;
132 core_datasec (abfd)->alignment_power = 2;
133 core_regsec (abfd)->alignment_power = 2;
135 abfd->sections = core_stacksec (abfd);
136 core_stacksec (abfd)->next = core_datasec (abfd);
137 core_datasec (abfd)->next = core_regsec (abfd);
138 abfd->section_count = 3;
140 return abfd->xvec;
143 char *
144 ptrace_unix_core_file_failing_command (abfd)
145 bfd *abfd;
147 char *com = abfd->tdata.trad_core_data->u.pt_comm;
148 if (*com)
149 return com;
150 else
151 return 0;
154 /* ARGSUSED */
156 ptrace_unix_core_file_failing_signal (abfd)
157 bfd *abfd;
159 return abfd->tdata.trad_core_data->u.pt_sigframe.sig_num;
162 /* ARGSUSED */
163 boolean
164 ptrace_unix_core_file_matches_executable_p (core_bfd, exec_bfd)
165 bfd *core_bfd, *exec_bfd;
167 /* FIXME: Use pt_timdat field of the ptrace_user structure to match
168 the date of the executable */
169 return true;
172 /* If somebody calls any byte-swapping routines, shoot them. */
173 static void
174 swap_abort ()
176 abort (); /* This way doesn't require any declaration for ANSI to fuck up */
178 #define NO_GET ((bfd_vma (*) PARAMS (( const bfd_byte *))) swap_abort )
179 #define NO_PUT ((void (*) PARAMS ((bfd_vma, bfd_byte *))) swap_abort )
180 #define NO_SIGNED_GET \
181 ((bfd_signed_vma (*) PARAMS ((const bfd_byte *))) swap_abort )
183 const bfd_target ptrace_core_vec =
185 "trad-core",
186 bfd_target_unknown_flavour,
187 BFD_ENDIAN_UNKNOWN, /* target byte order */
188 BFD_ENDIAN_UNKNOWN, /* target headers byte order */
189 (HAS_RELOC | EXEC_P | /* object flags */
190 HAS_LINENO | HAS_DEBUG |
191 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
192 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
193 0, /* symbol prefix */
194 ' ', /* ar_pad_char */
195 16, /* ar_max_namelen */
196 NO_GET, NO_SIGNED_GET, NO_PUT, /* 64 bit data */
197 NO_GET, NO_SIGNED_GET, NO_PUT, /* 32 bit data */
198 NO_GET, NO_SIGNED_GET, NO_PUT, /* 16 bit data */
199 NO_GET, NO_SIGNED_GET, NO_PUT, /* 64 bit hdrs */
200 NO_GET, NO_SIGNED_GET, NO_PUT, /* 32 bit hdrs */
201 NO_GET, NO_SIGNED_GET, NO_PUT, /* 16 bit hdrs */
203 { /* bfd_check_format */
204 _bfd_dummy_target, /* unknown format */
205 _bfd_dummy_target, /* object file */
206 _bfd_dummy_target, /* archive */
207 ptrace_unix_core_file_p /* a core file */
209 { /* bfd_set_format */
210 bfd_false, bfd_false,
211 bfd_false, bfd_false
213 { /* bfd_write_contents */
214 bfd_false, bfd_false,
215 bfd_false, bfd_false
218 BFD_JUMP_TABLE_GENERIC (_bfd_generic),
219 BFD_JUMP_TABLE_COPY (_bfd_generic),
220 BFD_JUMP_TABLE_CORE (ptrace_unix),
221 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
222 BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
223 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
224 BFD_JUMP_TABLE_WRITE (_bfd_generic),
225 BFD_JUMP_TABLE_LINK (_bfd_nolink),
226 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
228 NULL,
230 (PTR) 0 /* backend_data */
233 #endif /* PTRACE_CORE */