PR binutils/13534
[binutils.git] / bfd / ptrace-core.c
blob77eaaeb46a1a51d5c81247150cab71dd2361f5ef
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 2005, 2006, 2007 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 3 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., 51 Franklin Street - Fifth Floor, Boston,
24 MA 02110-1301, USA. */
26 #ifdef PTRACE_CORE
28 #include "sysdep.h"
29 #include "bfd.h"
30 #include "libbfd.h"
32 #include <sys/param.h>
33 #include <sys/dir.h>
34 #include <signal.h>
35 #include <sys/ptrace.h>
37 struct trad_core_struct
39 asection *data_section;
40 asection *stack_section;
41 asection *reg_section;
42 struct ptrace_user u;
45 #define core_upage(bfd) (&((bfd)->tdata.trad_core_data->u))
46 #define core_datasec(bfd) ((bfd)->tdata.trad_core_data->data_section)
47 #define core_stacksec(bfd) ((bfd)->tdata.trad_core_data->stack_section)
48 #define core_regsec(bfd) ((bfd)->tdata.trad_core_data->reg_section)
50 /* forward declarations */
52 const bfd_target *ptrace_unix_core_file_p PARAMS ((bfd *abfd));
53 char * ptrace_unix_core_file_failing_command PARAMS ((bfd *abfd));
54 int ptrace_unix_core_file_failing_signal PARAMS ((bfd *abfd));
55 #define ptrace_unix_core_file_matches_executable_p generic_core_file_matches_executable_p
56 #define ptrace_unix_core_file_pid _bfd_nocore_core_file_pid
57 static void swap_abort PARAMS ((void));
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;
68 flagword flags;
70 val = bfd_bread ((void *)&u, (bfd_size_type) sizeof u, abfd);
71 if (val != sizeof u || u.pt_magic != _BCS_PTRACE_MAGIC
72 || u.pt_rev != _BCS_PTRACE_REV)
74 /* Too small to be a core file */
75 bfd_set_error (bfd_error_wrong_format);
76 return 0;
79 /* OK, we believe you. You're a core file (sure, sure). */
81 /* Allocate both the upage and the struct core_data at once, so
82 a single free() will free them both. */
83 amt = sizeof (struct trad_core_struct);
84 rawptr = (struct trad_core_struct *) bfd_zalloc (abfd, amt);
86 if (rawptr == NULL)
87 return 0;
89 abfd->tdata.trad_core_data = rawptr;
91 rawptr->u = u; /*Copy the uarea into the tdata part of the bfd */
93 /* Create the sections. */
95 flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
96 core_stacksec (abfd) = bfd_make_section_anyway_with_flags (abfd, ".stack",
97 flags);
98 if (core_stacksec (abfd) == NULL)
99 goto fail;
100 core_datasec (abfd) = bfd_make_section_anyway_with_flags (abfd, ".data",
101 flags);
102 if (core_datasec (abfd) == NULL)
103 goto fail;
104 core_regsec (abfd) = bfd_make_section_anyway_with_flags (abfd, ".reg",
105 SEC_HAS_CONTENTS);
106 if (core_regsec (abfd) == NULL)
107 goto fail;
109 /* FIXME: Need to worry about shared memory, library data, and library
110 text. I don't think that any of these things are supported on the
111 system on which I am developing this for though. */
113 core_datasec (abfd)->size = u.pt_dsize;
114 core_stacksec (abfd)->size = u.pt_ssize;
115 core_regsec (abfd)->size = sizeof (u);
117 core_datasec (abfd)->vma = u.pt_o_data_start;
118 core_stacksec (abfd)->vma = USRSTACK - u.pt_ssize;
119 core_regsec (abfd)->vma = 0 - sizeof (u); /* see trad-core.c */
121 core_datasec (abfd)->filepos = (int) u.pt_dataptr;
122 core_stacksec (abfd)->filepos = (int) (u.pt_dataptr + u.pt_dsize);
123 core_regsec (abfd)->filepos = 0; /* Register segment is ptrace_user */
125 /* Align to word at least */
126 core_stacksec (abfd)->alignment_power = 2;
127 core_datasec (abfd)->alignment_power = 2;
128 core_regsec (abfd)->alignment_power = 2;
130 return abfd->xvec;
132 fail:
133 bfd_release (abfd, abfd->tdata.any);
134 abfd->tdata.any = NULL;
135 bfd_section_list_clear (abfd);
136 return NULL;
139 char *
140 ptrace_unix_core_file_failing_command (abfd)
141 bfd *abfd;
143 char *com = abfd->tdata.trad_core_data->u.pt_comm;
144 if (*com)
145 return com;
146 else
147 return 0;
151 ptrace_unix_core_file_failing_signal (abfd)
152 bfd *abfd;
154 return abfd->tdata.trad_core_data->u.pt_sigframe.sig_num;
157 /* If somebody calls any byte-swapping routines, shoot them. */
158 static void
159 swap_abort ()
161 abort (); /* This way doesn't require any declaration for ANSI to fuck up */
164 #define NO_GET ((bfd_vma (*) (const void *)) swap_abort)
165 #define NO_PUT ((void (*) (bfd_vma, void *)) swap_abort)
166 #define NO_GETS ((bfd_signed_vma (*) (const void *)) swap_abort)
167 #define NO_GET64 ((bfd_uint64_t (*) (const void *)) swap_abort)
168 #define NO_PUT64 ((void (*) (bfd_uint64_t, void *)) swap_abort)
169 #define NO_GETS64 ((bfd_int64_t (*) (const void *)) swap_abort)
171 const bfd_target ptrace_core_vec =
173 "trad-core",
174 bfd_target_unknown_flavour,
175 BFD_ENDIAN_UNKNOWN, /* target byte order */
176 BFD_ENDIAN_UNKNOWN, /* target headers byte order */
177 (HAS_RELOC | EXEC_P | /* object flags */
178 HAS_LINENO | HAS_DEBUG |
179 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
180 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
181 0, /* symbol prefix */
182 ' ', /* ar_pad_char */
183 16, /* ar_max_namelen */
184 NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit data */
185 NO_GET, NO_GETS, NO_PUT, /* 32 bit data */
186 NO_GET, NO_GETS, NO_PUT, /* 16 bit data */
187 NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit hdrs */
188 NO_GET, NO_GETS, NO_PUT, /* 32 bit hdrs */
189 NO_GET, NO_GETS, NO_PUT, /* 16 bit hdrs */
191 { /* bfd_check_format */
192 _bfd_dummy_target, /* unknown format */
193 _bfd_dummy_target, /* object file */
194 _bfd_dummy_target, /* archive */
195 ptrace_unix_core_file_p /* a core file */
197 { /* bfd_set_format */
198 bfd_false, bfd_false,
199 bfd_false, bfd_false
201 { /* bfd_write_contents */
202 bfd_false, bfd_false,
203 bfd_false, bfd_false
206 BFD_JUMP_TABLE_GENERIC (_bfd_generic),
207 BFD_JUMP_TABLE_COPY (_bfd_generic),
208 BFD_JUMP_TABLE_CORE (ptrace_unix),
209 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
210 BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
211 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
212 BFD_JUMP_TABLE_WRITE (_bfd_generic),
213 BFD_JUMP_TABLE_LINK (_bfd_nolink),
214 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
216 NULL,
218 (PTR) 0 /* backend_data */
221 #endif /* PTRACE_CORE */