Back to index

cell-binutils  2.17cvs20070401
ptrace-core.c
Go to the documentation of this file.
00001 /* BFD backend for core files which use the ptrace_user structure
00002    Copyright 1993, 1994, 1995, 1996, 1998, 1999, 2001, 2002, 2003, 2004, 2006
00003    Free Software Foundation, Inc.
00004    The structure of this file is based on trad-core.c written by John Gilmore
00005    of Cygnus Support.
00006    Modified to work with the ptrace_user structure by Kevin A. Buettner.
00007    (Longterm it may be better to merge this file with trad-core.c)
00008 
00009 This file is part of BFD, the Binary File Descriptor library.
00010 
00011 This program is free software; you can redistribute it and/or modify
00012 it under the terms of the GNU General Public License as published by
00013 the Free Software Foundation; either version 2 of the License, or
00014 (at your option) any later version.
00015 
00016 This program is distributed in the hope that it will be useful,
00017 but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 GNU General Public License for more details.
00020 
00021 You should have received a copy of the GNU General Public License
00022 along with this program; if not, write to the Free Software
00023 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00024 
00025 #ifdef PTRACE_CORE
00026 
00027 #include "bfd.h"
00028 #include "sysdep.h"
00029 #include "libbfd.h"
00030 
00031 #include <sys/param.h>
00032 #include <sys/dir.h>
00033 #include <signal.h>
00034 #include <sys/ptrace.h>
00035 
00036 struct trad_core_struct
00037   {
00038     asection *data_section;
00039     asection *stack_section;
00040     asection *reg_section;
00041     struct ptrace_user u;
00042   };
00043 
00044 #define core_upage(bfd) (&((bfd)->tdata.trad_core_data->u))
00045 #define core_datasec(bfd) ((bfd)->tdata.trad_core_data->data_section)
00046 #define core_stacksec(bfd) ((bfd)->tdata.trad_core_data->stack_section)
00047 #define core_regsec(bfd) ((bfd)->tdata.trad_core_data->reg_section)
00048 
00049 /* forward declarations */
00050 
00051 const bfd_target *ptrace_unix_core_file_p PARAMS ((bfd *abfd));
00052 char * ptrace_unix_core_file_failing_command PARAMS ((bfd *abfd));
00053 int ptrace_unix_core_file_failing_signal PARAMS ((bfd *abfd));
00054 #define ptrace_unix_core_file_matches_executable_p generic_core_file_matches_executable_p
00055 static void swap_abort PARAMS ((void));
00056 
00057 const bfd_target *
00058 ptrace_unix_core_file_p (abfd)
00059      bfd *abfd;
00060 
00061 {
00062   int val;
00063   struct ptrace_user u;
00064   struct trad_core_struct *rawptr;
00065   bfd_size_type amt;
00066   flagword flags;
00067 
00068   val = bfd_bread ((void *)&u, (bfd_size_type) sizeof u, abfd);
00069   if (val != sizeof u || u.pt_magic != _BCS_PTRACE_MAGIC
00070       || u.pt_rev != _BCS_PTRACE_REV)
00071     {
00072       /* Too small to be a core file */
00073       bfd_set_error (bfd_error_wrong_format);
00074       return 0;
00075     }
00076 
00077   /* OK, we believe you.  You're a core file (sure, sure).  */
00078 
00079   /* Allocate both the upage and the struct core_data at once, so
00080      a single free() will free them both.  */
00081   amt = sizeof (struct trad_core_struct);
00082   rawptr = (struct trad_core_struct *) bfd_zalloc (abfd, amt);
00083 
00084   if (rawptr == NULL)
00085     return 0;
00086 
00087   abfd->tdata.trad_core_data = rawptr;
00088 
00089   rawptr->u = u; /*Copy the uarea into the tdata part of the bfd */
00090 
00091   /* Create the sections.  */
00092 
00093   flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
00094   core_stacksec (abfd) = bfd_make_section_anyway_with_flags (abfd, ".stack",
00095                                                       flags);
00096   if (core_stacksec (abfd) == NULL)
00097     goto fail;
00098   core_datasec (abfd) = bfd_make_section_anyway_with_flags (abfd, ".data",
00099                                                      flags);
00100   if (core_datasec (abfd) == NULL)
00101     goto fail;
00102   core_regsec (abfd) = bfd_make_section_anyway_with_flags (abfd, ".reg",
00103                                                     SEC_HAS_CONTENTS);
00104   if (core_regsec (abfd) == NULL)
00105     goto fail;
00106 
00107   /* FIXME:  Need to worry about shared memory, library data, and library
00108      text.  I don't think that any of these things are supported on the
00109      system on which I am developing this for though.  */
00110 
00111   core_datasec (abfd)->size =  u.pt_dsize;
00112   core_stacksec (abfd)->size = u.pt_ssize;
00113   core_regsec (abfd)->size = sizeof (u);
00114 
00115   core_datasec (abfd)->vma = u.pt_o_data_start;
00116   core_stacksec (abfd)->vma = USRSTACK - u.pt_ssize;
00117   core_regsec (abfd)->vma = 0 - sizeof (u);      /* see trad-core.c */
00118 
00119   core_datasec (abfd)->filepos = (int) u.pt_dataptr;
00120   core_stacksec (abfd)->filepos = (int) (u.pt_dataptr + u.pt_dsize);
00121   core_regsec (abfd)->filepos = 0; /* Register segment is ptrace_user */
00122 
00123   /* Align to word at least */
00124   core_stacksec (abfd)->alignment_power = 2;
00125   core_datasec (abfd)->alignment_power = 2;
00126   core_regsec (abfd)->alignment_power = 2;
00127 
00128   return abfd->xvec;
00129 
00130  fail:
00131   bfd_release (abfd, abfd->tdata.any);
00132   abfd->tdata.any = NULL;
00133   bfd_section_list_clear (abfd);
00134   return NULL;
00135 }
00136 
00137 char *
00138 ptrace_unix_core_file_failing_command (abfd)
00139      bfd *abfd;
00140 {
00141   char *com = abfd->tdata.trad_core_data->u.pt_comm;
00142   if (*com)
00143     return com;
00144   else
00145     return 0;
00146 }
00147 
00148 int
00149 ptrace_unix_core_file_failing_signal (abfd)
00150      bfd *abfd;
00151 {
00152   return abfd->tdata.trad_core_data->u.pt_sigframe.sig_num;
00153 }
00154 
00155 /* If somebody calls any byte-swapping routines, shoot them.  */
00156 static void
00157 swap_abort ()
00158 {
00159   abort (); /* This way doesn't require any declaration for ANSI to fuck up */
00160 }
00161 
00162 #define       NO_GET ((bfd_vma (*) (const void *)) swap_abort)
00163 #define       NO_PUT ((void (*) (bfd_vma, void *)) swap_abort)
00164 #define       NO_GETS ((bfd_signed_vma (*) (const void *)) swap_abort)
00165 #define       NO_GET64 ((bfd_uint64_t (*) (const void *)) swap_abort)
00166 #define       NO_PUT64 ((void (*) (bfd_uint64_t, void *)) swap_abort)
00167 #define       NO_GETS64 ((bfd_int64_t (*) (const void *)) swap_abort)
00168 
00169 const bfd_target ptrace_core_vec =
00170   {
00171     "trad-core",
00172     bfd_target_unknown_flavour,
00173     BFD_ENDIAN_UNKNOWN,            /* target byte order */
00174     BFD_ENDIAN_UNKNOWN,            /* target headers byte order */
00175     (HAS_RELOC | EXEC_P |   /* object flags */
00176      HAS_LINENO | HAS_DEBUG |
00177      HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
00178     (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
00179     0,                                                  /* symbol prefix */
00180     ' ',                                            /* ar_pad_char */
00181     16,                                                    /* ar_max_namelen */
00182     NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit data */
00183     NO_GET, NO_GETS, NO_PUT,              /* 32 bit data */
00184     NO_GET, NO_GETS, NO_PUT,              /* 16 bit data */
00185     NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit hdrs */
00186     NO_GET, NO_GETS, NO_PUT,              /* 32 bit hdrs */
00187     NO_GET, NO_GETS, NO_PUT,              /* 16 bit hdrs */
00188 
00189     {                       /* bfd_check_format */
00190       _bfd_dummy_target,           /* unknown format */
00191       _bfd_dummy_target,           /* object file */
00192       _bfd_dummy_target,           /* archive */
00193       ptrace_unix_core_file_p             /* a core file */
00194     },
00195     {                       /* bfd_set_format */
00196       bfd_false, bfd_false,
00197       bfd_false, bfd_false
00198     },
00199     {                       /* bfd_write_contents */
00200       bfd_false, bfd_false,
00201       bfd_false, bfd_false
00202     },
00203 
00204     BFD_JUMP_TABLE_GENERIC (_bfd_generic),
00205     BFD_JUMP_TABLE_COPY (_bfd_generic),
00206     BFD_JUMP_TABLE_CORE (ptrace_unix),
00207     BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
00208     BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
00209     BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
00210     BFD_JUMP_TABLE_WRITE (_bfd_generic),
00211     BFD_JUMP_TABLE_LINK (_bfd_nolink),
00212     BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
00213 
00214     NULL,
00215 
00216     (PTR) 0                 /* backend_data */
00217   };
00218 
00219 #endif /* PTRACE_CORE */