This commit was generated by cvs2svn to track changes on a CVS vendor
[external/binutils.git] / bfd / ptrace-core.c
1 /* BFD backend for core files which use the ptrace_user structure
2    Copyright 1993, 1994 Free Software Foundation, Inc.
3    The structure of this file is based on trad-core.c written by John Gilmore
4    of Cygnus Support.
5    Modified to work with the ptrace_user structure by Kevin A. Buettner.
6    (Longterm it may be better to merge this file with trad-core.c)
7
8 This file is part of BFD, the Binary File Descriptor library.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
23
24 #ifdef PTRACE_CORE
25
26 #include "bfd.h"
27 #include "sysdep.h"
28 #include "libbfd.h"
29
30 #include <stdio.h>
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/dir.h>
34 #include <signal.h>
35 #include <errno.h>
36 #include <unistd.h>
37 #include <sys/ptrace.h>
38
39
40 struct trad_core_struct
41   {
42     asection *data_section;
43     asection *stack_section;
44     asection *reg_section;
45     struct ptrace_user u;
46   };
47
48 #define core_upage(bfd) (&((bfd)->tdata.trad_core_data->u))
49 #define core_datasec(bfd) ((bfd)->tdata.trad_core_data->data_section)
50 #define core_stacksec(bfd) ((bfd)->tdata.trad_core_data->stack_section)
51 #define core_regsec(bfd) ((bfd)->tdata.trad_core_data->reg_section)
52
53 /* forward declarations */
54
55 const bfd_target *ptrace_unix_core_file_p PARAMS ((bfd *abfd));
56 char *          ptrace_unix_core_file_failing_command PARAMS ((bfd *abfd));
57 int             ptrace_unix_core_file_failing_signal PARAMS ((bfd *abfd));
58 boolean         ptrace_unix_core_file_matches_executable_p
59                          PARAMS ((bfd *core_bfd, bfd *exec_bfd));
60
61 /* ARGSUSED */
62 const bfd_target *
63 ptrace_unix_core_file_p (abfd)
64      bfd *abfd;
65
66 {
67   int val;
68   struct ptrace_user u;
69   struct trad_core_struct *rawptr;
70
71   val = bfd_read ((void *)&u, 1, sizeof u, abfd);
72   if (val != sizeof u || u.pt_magic != _BCS_PTRACE_MAGIC 
73       || u.pt_rev != _BCS_PTRACE_REV)
74     {
75       /* Too small to be a core file */
76       bfd_set_error (bfd_error_wrong_format);
77       return 0;
78     }
79
80   /* OK, we believe you.  You're a core file (sure, sure).  */
81
82   /* Allocate both the upage and the struct core_data at once, so
83      a single free() will free them both.  */
84   rawptr = (struct trad_core_struct *)
85                 bfd_zalloc (abfd, sizeof (struct trad_core_struct));
86
87   if (rawptr == NULL)
88     return 0;
89   
90   abfd->tdata.trad_core_data = rawptr;
91
92   rawptr->u = u; /*Copy the uarea into the tdata part of the bfd */
93
94   /* Create the sections.  This is raunchy, but bfd_close wants to free
95      them separately.  */
96
97   core_stacksec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
98   if (core_stacksec (abfd) == NULL)
99     return NULL;
100   core_datasec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
101   if (core_datasec (abfd) == NULL)
102     return NULL;
103   core_regsec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
104   if (core_regsec (abfd) == NULL)
105     return NULL;
106
107   core_stacksec (abfd)->name = ".stack";
108   core_datasec (abfd)->name = ".data";
109   core_regsec (abfd)->name = ".reg";
110
111   /* FIXME:  Need to worry about shared memory, library data, and library
112      text.  I don't think that any of these things are supported on the
113      system on which I am developing this for though. */
114
115
116   core_stacksec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
117   core_datasec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
118   core_regsec (abfd)->flags = SEC_HAS_CONTENTS;
119
120   core_datasec (abfd)->_raw_size =  u.pt_dsize;
121   core_stacksec (abfd)->_raw_size = u.pt_ssize;
122   core_regsec (abfd)->_raw_size = sizeof(u);
123
124   core_datasec (abfd)->vma = u.pt_o_data_start;
125   core_stacksec (abfd)->vma = USRSTACK - u.pt_ssize;
126   core_regsec (abfd)->vma = 0 - sizeof(u);      /* see trad-core.c */
127
128   core_datasec (abfd)->filepos = (int) u.pt_dataptr;
129   core_stacksec (abfd)->filepos = (int) (u.pt_dataptr + u.pt_dsize);
130   core_regsec (abfd)->filepos = 0; /* Register segment is ptrace_user */
131
132   /* Align to word at least */
133   core_stacksec (abfd)->alignment_power = 2;
134   core_datasec (abfd)->alignment_power = 2;
135   core_regsec (abfd)->alignment_power = 2;
136
137   abfd->sections = core_stacksec (abfd);
138   core_stacksec (abfd)->next = core_datasec (abfd);
139   core_datasec (abfd)->next = core_regsec (abfd);
140   abfd->section_count = 3;
141
142   return abfd->xvec;
143 }
144
145 char *
146 ptrace_unix_core_file_failing_command (abfd)
147      bfd *abfd;
148 {
149   char *com = abfd->tdata.trad_core_data->u.pt_comm;
150   if (*com)
151     return com;
152   else
153     return 0;
154 }
155
156 /* ARGSUSED */
157 int
158 ptrace_unix_core_file_failing_signal (abfd)
159      bfd *abfd;
160 {
161   return abfd->tdata.trad_core_data->u.pt_sigframe.sig_num;
162 }
163
164 /* ARGSUSED */
165 boolean
166 ptrace_unix_core_file_matches_executable_p  (core_bfd, exec_bfd)
167      bfd *core_bfd, *exec_bfd;
168 {
169   /* FIXME: Use pt_timdat field of the ptrace_user structure to match 
170      the date of the executable */
171   return true;
172 }
173 \f
174 /* If somebody calls any byte-swapping routines, shoot them.  */
175 void
176 swap_abort()
177 {
178   abort(); /* This way doesn't require any declaration for ANSI to fuck up */
179 }
180 #define NO_GET  ((bfd_vma (*) PARAMS ((   const bfd_byte *))) swap_abort )
181 #define NO_PUT  ((void    (*) PARAMS ((bfd_vma, bfd_byte *))) swap_abort )
182 #define NO_SIGNED_GET \
183   ((bfd_signed_vma (*) PARAMS ((const bfd_byte *))) swap_abort )
184
185 const bfd_target ptrace_core_vec =
186   {
187     "trad-core",
188     bfd_target_unknown_flavour,
189     true,                       /* target byte order */
190     true,                       /* target headers byte order */
191     (HAS_RELOC | EXEC_P |       /* object flags */
192      HAS_LINENO | HAS_DEBUG |
193      HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
194     (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
195     0,                                                     /* symbol prefix */
196     ' ',                                                   /* ar_pad_char */
197     16,                                                    /* ar_max_namelen */
198     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 64 bit data */
199     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 32 bit data */
200     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 16 bit data */
201     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 64 bit hdrs */
202     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 32 bit hdrs */
203     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 16 bit hdrs */
204
205     {                           /* bfd_check_format */
206      _bfd_dummy_target,         /* unknown format */
207      _bfd_dummy_target,         /* object file */
208      _bfd_dummy_target,         /* archive */
209      ptrace_unix_core_file_p    /* a core file */
210     },
211     {                           /* bfd_set_format */
212      bfd_false, bfd_false,
213      bfd_false, bfd_false
214     },
215     {                           /* bfd_write_contents */
216      bfd_false, bfd_false,
217      bfd_false, bfd_false
218     },
219     
220        BFD_JUMP_TABLE_GENERIC (_bfd_generic),
221        BFD_JUMP_TABLE_COPY (_bfd_generic),
222        BFD_JUMP_TABLE_CORE (ptrace_unix),
223        BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
224        BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
225        BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
226        BFD_JUMP_TABLE_WRITE (_bfd_generic),
227        BFD_JUMP_TABLE_LINK (_bfd_nolink),
228        BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
229
230     (PTR) 0                     /* backend_data */
231 };
232
233 #endif /* PTRACE_CORE */