bfd/
[deliverable/binutils-gdb.git] / bfd / ptrace-core.c
CommitLineData
252b5132 1/* BFD backend for core files which use the ptrace_user structure
3db64b00
AM
2 Copyright 1993, 1994, 1995, 1996, 1998, 1999, 2001, 2002, 2003, 2004,
3 2006, 2007 Free Software Foundation, Inc.
252b5132
RH
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
3e110533 23Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
252b5132
RH
24
25#ifdef PTRACE_CORE
26
252b5132 27#include "sysdep.h"
3db64b00 28#include "bfd.h"
252b5132
RH
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
252b5132
RH
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));
b34976b6
AM
52char * ptrace_unix_core_file_failing_command PARAMS ((bfd *abfd));
53int ptrace_unix_core_file_failing_signal PARAMS ((bfd *abfd));
69d246d9 54#define ptrace_unix_core_file_matches_executable_p generic_core_file_matches_executable_p
b34976b6 55static void swap_abort PARAMS ((void));
252b5132 56
252b5132
RH
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;
dc810e39 65 bfd_size_type amt;
117ed4f8 66 flagword flags;
252b5132 67
dc810e39 68 val = bfd_bread ((void *)&u, (bfd_size_type) sizeof u, abfd);
892339ee 69 if (val != sizeof u || u.pt_magic != _BCS_PTRACE_MAGIC
252b5132
RH
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. */
dc810e39
AM
81 amt = sizeof (struct trad_core_struct);
82 rawptr = (struct trad_core_struct *) bfd_zalloc (abfd, amt);
252b5132
RH
83
84 if (rawptr == NULL)
85 return 0;
892339ee 86
252b5132
RH
87 abfd->tdata.trad_core_data = rawptr;
88
89 rawptr->u = u; /*Copy the uarea into the tdata part of the bfd */
90
9e7b37b3 91 /* Create the sections. */
252b5132 92
117ed4f8
AM
93 flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
94 core_stacksec (abfd) = bfd_make_section_anyway_with_flags (abfd, ".stack",
95 flags);
252b5132 96 if (core_stacksec (abfd) == NULL)
9e7b37b3 97 goto fail;
117ed4f8
AM
98 core_datasec (abfd) = bfd_make_section_anyway_with_flags (abfd, ".data",
99 flags);
252b5132 100 if (core_datasec (abfd) == NULL)
9e7b37b3 101 goto fail;
117ed4f8
AM
102 core_regsec (abfd) = bfd_make_section_anyway_with_flags (abfd, ".reg",
103 SEC_HAS_CONTENTS);
252b5132 104 if (core_regsec (abfd) == NULL)
9e7b37b3 105 goto fail;
252b5132
RH
106
107 /* FIXME: Need to worry about shared memory, library data, and library
108 text. I don't think that any of these things are supported on the
892339ee 109 system on which I am developing this for though. */
252b5132 110
eea6121a
AM
111 core_datasec (abfd)->size = u.pt_dsize;
112 core_stacksec (abfd)->size = u.pt_ssize;
113 core_regsec (abfd)->size = sizeof (u);
252b5132
RH
114
115 core_datasec (abfd)->vma = u.pt_o_data_start;
116 core_stacksec (abfd)->vma = USRSTACK - u.pt_ssize;
892339ee 117 core_regsec (abfd)->vma = 0 - sizeof (u); /* see trad-core.c */
252b5132
RH
118
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 */
122
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;
127
252b5132 128 return abfd->xvec;
9e7b37b3
AM
129
130 fail:
131 bfd_release (abfd, abfd->tdata.any);
132 abfd->tdata.any = NULL;
133 bfd_section_list_clear (abfd);
134 return NULL;
252b5132
RH
135}
136
137char *
138ptrace_unix_core_file_failing_command (abfd)
139 bfd *abfd;
140{
141 char *com = abfd->tdata.trad_core_data->u.pt_comm;
142 if (*com)
143 return com;
144 else
145 return 0;
146}
147
252b5132
RH
148int
149ptrace_unix_core_file_failing_signal (abfd)
150 bfd *abfd;
151{
152 return abfd->tdata.trad_core_data->u.pt_sigframe.sig_num;
153}
252b5132
RH
154\f
155/* If somebody calls any byte-swapping routines, shoot them. */
156static void
892339ee 157swap_abort ()
252b5132 158{
892339ee 159 abort (); /* This way doesn't require any declaration for ANSI to fuck up */
252b5132 160}
8ce8c090 161
edeb6e24
AM
162#define NO_GET ((bfd_vma (*) (const void *)) swap_abort)
163#define NO_PUT ((void (*) (bfd_vma, void *)) swap_abort)
164#define NO_GETS ((bfd_signed_vma (*) (const void *)) swap_abort)
8ce8c090
AM
165#define NO_GET64 ((bfd_uint64_t (*) (const void *)) swap_abort)
166#define NO_PUT64 ((void (*) (bfd_uint64_t, void *)) swap_abort)
167#define NO_GETS64 ((bfd_int64_t (*) (const void *)) swap_abort)
252b5132
RH
168
169const bfd_target ptrace_core_vec =
170 {
171 "trad-core",
172 bfd_target_unknown_flavour,
173 BFD_ENDIAN_UNKNOWN, /* target byte order */
174 BFD_ENDIAN_UNKNOWN, /* target headers byte order */
175 (HAS_RELOC | EXEC_P | /* object flags */
176 HAS_LINENO | HAS_DEBUG |
177 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
178 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
179 0, /* symbol prefix */
180 ' ', /* ar_pad_char */
181 16, /* ar_max_namelen */
8ce8c090 182 NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit data */
edeb6e24
AM
183 NO_GET, NO_GETS, NO_PUT, /* 32 bit data */
184 NO_GET, NO_GETS, NO_PUT, /* 16 bit data */
8ce8c090 185 NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit hdrs */
edeb6e24
AM
186 NO_GET, NO_GETS, NO_PUT, /* 32 bit hdrs */
187 NO_GET, NO_GETS, NO_PUT, /* 16 bit hdrs */
252b5132
RH
188
189 { /* bfd_check_format */
8ce8c090
AM
190 _bfd_dummy_target, /* unknown format */
191 _bfd_dummy_target, /* object file */
192 _bfd_dummy_target, /* archive */
193 ptrace_unix_core_file_p /* a core file */
252b5132
RH
194 },
195 { /* bfd_set_format */
8ce8c090
AM
196 bfd_false, bfd_false,
197 bfd_false, bfd_false
252b5132
RH
198 },
199 { /* bfd_write_contents */
8ce8c090
AM
200 bfd_false, bfd_false,
201 bfd_false, bfd_false
252b5132 202 },
892339ee 203
8ce8c090
AM
204 BFD_JUMP_TABLE_GENERIC (_bfd_generic),
205 BFD_JUMP_TABLE_COPY (_bfd_generic),
206 BFD_JUMP_TABLE_CORE (ptrace_unix),
207 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
208 BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
209 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
210 BFD_JUMP_TABLE_WRITE (_bfd_generic),
211 BFD_JUMP_TABLE_LINK (_bfd_nolink),
212 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
252b5132 213
c3c89269 214 NULL,
892339ee 215
252b5132 216 (PTR) 0 /* backend_data */
8ce8c090 217 };
252b5132
RH
218
219#endif /* PTRACE_CORE */
This page took 0.43481 seconds and 4 git commands to generate.