Back to index

cell-binutils  2.17cvs20070401
trad-core.c
Go to the documentation of this file.
00001 /* BFD back end for traditional Unix core files (U-area and raw sections)
00002    Copyright 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999,
00003    2000, 2001, 2002, 2003, 2004, 2005, 2006
00004    Free Software Foundation, Inc.
00005    Written by John Gilmore of Cygnus Support.
00006 
00007 This file is part of BFD, the Binary File Descriptor library.
00008 
00009 This program is free software; you can redistribute it and/or modify
00010 it under the terms of the GNU General Public License as published by
00011 the Free Software Foundation; either version 2 of the License, or
00012 (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00022 
00023 #include "bfd.h"
00024 #include "sysdep.h"
00025 #include "libbfd.h"
00026 #include "libaout.h"           /* BFD a.out internal data structures */
00027 
00028 #include <sys/param.h>
00029 #ifdef HAVE_DIRENT_H
00030 # include <dirent.h>
00031 #else
00032 # ifdef HAVE_SYS_NDIR_H
00033 #  include <sys/ndir.h>
00034 # endif
00035 # ifdef HAVE_SYS_DIR_H
00036 #  include <sys/dir.h>
00037 # endif
00038 # ifdef HAVE_NDIR_H
00039 #  include <ndir.h>
00040 # endif
00041 #endif
00042 #include <signal.h>
00043 
00044 #include <sys/user.h>              /* After a.out.h  */
00045 
00046 #ifdef TRAD_HEADER
00047 #include TRAD_HEADER
00048 #endif
00049 
00050 struct trad_core_struct
00051 {
00052   asection *data_section;
00053   asection *stack_section;
00054   asection *reg_section;
00055   struct user u;
00056 };
00057 
00058 #define core_upage(bfd) (&((bfd)->tdata.trad_core_data->u))
00059 #define core_datasec(bfd) ((bfd)->tdata.trad_core_data->data_section)
00060 #define core_stacksec(bfd) ((bfd)->tdata.trad_core_data->stack_section)
00061 #define core_regsec(bfd) ((bfd)->tdata.trad_core_data->reg_section)
00062 
00063 /* forward declarations */
00064 
00065 const bfd_target *trad_unix_core_file_p PARAMS ((bfd *abfd));
00066 char * trad_unix_core_file_failing_command PARAMS ((bfd *abfd));
00067 int trad_unix_core_file_failing_signal PARAMS ((bfd *abfd));
00068 #define trad_unix_core_file_matches_executable_p generic_core_file_matches_executable_p
00069 static void swap_abort PARAMS ((void));
00070 
00071 /* Handle 4.2-style (and perhaps also sysV-style) core dump file.  */
00072 
00073 const bfd_target *
00074 trad_unix_core_file_p (abfd)
00075      bfd *abfd;
00076 
00077 {
00078   int val;
00079   struct user u;
00080   struct trad_core_struct *rawptr;
00081   bfd_size_type amt;
00082   flagword flags;
00083 
00084 #ifdef TRAD_CORE_USER_OFFSET
00085   /* If defined, this macro is the file position of the user struct.  */
00086   if (bfd_seek (abfd, (file_ptr) TRAD_CORE_USER_OFFSET, SEEK_SET) != 0)
00087     return 0;
00088 #endif
00089 
00090   val = bfd_bread ((void *) &u, (bfd_size_type) sizeof u, abfd);
00091   if (val != sizeof u)
00092     {
00093       /* Too small to be a core file */
00094       bfd_set_error (bfd_error_wrong_format);
00095       return 0;
00096     }
00097 
00098   /* Sanity check perhaps??? */
00099   if (u.u_dsize > 0x1000000)       /* Remember, it's in pages...  */
00100     {
00101       bfd_set_error (bfd_error_wrong_format);
00102       return 0;
00103     }
00104   if (u.u_ssize > 0x1000000)
00105     {
00106       bfd_set_error (bfd_error_wrong_format);
00107       return 0;
00108     }
00109 
00110   /* Check that the size claimed is no greater than the file size.  */
00111   {
00112     struct stat statbuf;
00113 
00114     if (bfd_stat (abfd, &statbuf) < 0)
00115       return 0;
00116 
00117     if ((ufile_ptr) NBPG * (UPAGES + u.u_dsize
00118 #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE
00119                          - u.u_tsize
00120 #endif
00121                          + u.u_ssize)
00122        > (ufile_ptr) statbuf.st_size)
00123       {
00124        bfd_set_error (bfd_error_wrong_format);
00125        return 0;
00126       }
00127 #ifndef TRAD_CORE_ALLOW_ANY_EXTRA_SIZE
00128     if (((ufile_ptr) NBPG * (UPAGES + u.u_dsize + u.u_ssize)
00129 #ifdef TRAD_CORE_EXTRA_SIZE_ALLOWED
00130        /* Some systems write the file too big.  */
00131         + TRAD_CORE_EXTRA_SIZE_ALLOWED
00132 #endif
00133         )
00134        < (ufile_ptr) statbuf.st_size)
00135       {
00136        /* The file is too big.  Maybe it's not a core file
00137           or we otherwise have bad values for u_dsize and u_ssize).  */
00138        bfd_set_error (bfd_error_wrong_format);
00139        return 0;
00140       }
00141 #endif
00142   }
00143 
00144   /* OK, we believe you.  You're a core file (sure, sure).  */
00145 
00146   /* Allocate both the upage and the struct core_data at once, so
00147      a single free() will free them both.  */
00148   amt = sizeof (struct trad_core_struct);
00149   rawptr = (struct trad_core_struct *) bfd_zmalloc (amt);
00150   if (rawptr == NULL)
00151     return 0;
00152 
00153   abfd->tdata.trad_core_data = rawptr;
00154 
00155   rawptr->u = u; /*Copy the uarea into the tdata part of the bfd */
00156 
00157   /* Create the sections.  */
00158 
00159   flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
00160   core_stacksec(abfd) = bfd_make_section_anyway_with_flags (abfd, ".stack",
00161                                                      flags);
00162   if (core_stacksec (abfd) == NULL)
00163     goto fail;
00164   core_datasec (abfd) = bfd_make_section_anyway_with_flags (abfd, ".data",
00165                                                      flags);
00166   if (core_datasec (abfd) == NULL)
00167     goto fail;
00168   core_regsec (abfd) = bfd_make_section_anyway_with_flags (abfd, ".reg",
00169                                                     SEC_HAS_CONTENTS);
00170   if (core_regsec (abfd) == NULL)
00171     goto fail;
00172 
00173   core_datasec (abfd)->size =  NBPG * u.u_dsize
00174 #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE
00175     - NBPG * u.u_tsize
00176 #endif
00177       ;
00178   core_stacksec (abfd)->size = NBPG * u.u_ssize;
00179   core_regsec (abfd)->size = NBPG * UPAGES; /* Larger than sizeof struct u */
00180 
00181   /* What a hack... we'd like to steal it from the exec file,
00182      since the upage does not seem to provide it.  FIXME.  */
00183 #ifdef HOST_DATA_START_ADDR
00184   core_datasec (abfd)->vma = HOST_DATA_START_ADDR;
00185 #else
00186   core_datasec (abfd)->vma = HOST_TEXT_START_ADDR + (NBPG * u.u_tsize);
00187 #endif
00188 
00189 #ifdef HOST_STACK_START_ADDR
00190   core_stacksec (abfd)->vma = HOST_STACK_START_ADDR;
00191 #else
00192   core_stacksec (abfd)->vma = HOST_STACK_END_ADDR - (NBPG * u.u_ssize);
00193 #endif
00194 
00195   /* This is tricky.  As the "register section", we give them the entire
00196      upage and stack.  u.u_ar0 points to where "register 0" is stored.
00197      There are two tricks with this, though.  One is that the rest of the
00198      registers might be at positive or negative (or both) displacements
00199      from *u_ar0.  The other is that u_ar0 is sometimes an absolute address
00200      in kernel memory, and on other systems it is an offset from the beginning
00201      of the `struct user'.
00202 
00203      As a practical matter, we don't know where the registers actually are,
00204      so we have to pass the whole area to GDB.  We encode the value of u_ar0
00205      by setting the .regs section up so that its virtual memory address
00206      0 is at the place pointed to by u_ar0 (by setting the vma of the start
00207      of the section to -u_ar0).  GDB uses this info to locate the regs,
00208      using minor trickery to get around the offset-or-absolute-addr problem.  */
00209   core_regsec (abfd)->vma = - (bfd_vma) (unsigned long) u.u_ar0;
00210 
00211   core_datasec (abfd)->filepos = NBPG * UPAGES;
00212   core_stacksec (abfd)->filepos = (NBPG * UPAGES) + NBPG * u.u_dsize
00213 #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE
00214     - NBPG * u.u_tsize
00215 #endif
00216       ;
00217   core_regsec (abfd)->filepos = 0; /* Register segment is the upage */
00218 
00219   /* Align to word at least */
00220   core_stacksec (abfd)->alignment_power = 2;
00221   core_datasec (abfd)->alignment_power = 2;
00222   core_regsec (abfd)->alignment_power = 2;
00223 
00224   return abfd->xvec;
00225 
00226  fail:
00227   bfd_release (abfd, abfd->tdata.any);
00228   abfd->tdata.any = NULL;
00229   bfd_section_list_clear (abfd);
00230   return NULL;
00231 }
00232 
00233 char *
00234 trad_unix_core_file_failing_command (abfd)
00235      bfd *abfd;
00236 {
00237 #ifndef NO_CORE_COMMAND
00238   char *com = abfd->tdata.trad_core_data->u.u_comm;
00239   if (*com)
00240     return com;
00241   else
00242 #endif
00243     return 0;
00244 }
00245 
00246 int
00247 trad_unix_core_file_failing_signal (ignore_abfd)
00248      bfd *ignore_abfd ATTRIBUTE_UNUSED;
00249 {
00250 #ifdef TRAD_UNIX_CORE_FILE_FAILING_SIGNAL
00251   return TRAD_UNIX_CORE_FILE_FAILING_SIGNAL(ignore_abfd);
00252 #else
00253   return -1;         /* FIXME, where is it? */
00254 #endif
00255 }
00256 
00257 /* If somebody calls any byte-swapping routines, shoot them.  */
00258 static void
00259 swap_abort ()
00260 {
00261   abort (); /* This way doesn't require any declaration for ANSI to fuck up */
00262 }
00263 
00264 #define       NO_GET ((bfd_vma (*) (const void *)) swap_abort)
00265 #define       NO_PUT ((void (*) (bfd_vma, void *)) swap_abort)
00266 #define       NO_GETS ((bfd_signed_vma (*) (const void *)) swap_abort)
00267 #define       NO_GET64 ((bfd_uint64_t (*) (const void *)) swap_abort)
00268 #define       NO_PUT64 ((void (*) (bfd_uint64_t, void *)) swap_abort)
00269 #define       NO_GETS64 ((bfd_int64_t (*) (const void *)) swap_abort)
00270 
00271 const bfd_target trad_core_vec =
00272   {
00273     "trad-core",
00274     bfd_target_unknown_flavour,
00275     BFD_ENDIAN_UNKNOWN,            /* target byte order */
00276     BFD_ENDIAN_UNKNOWN,            /* target headers byte order */
00277     (HAS_RELOC | EXEC_P |   /* object flags */
00278      HAS_LINENO | HAS_DEBUG |
00279      HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
00280     (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
00281     0,                                                  /* symbol prefix */
00282     ' ',                                            /* ar_pad_char */
00283     16,                                                    /* ar_max_namelen */
00284     NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit data */
00285     NO_GET, NO_GETS, NO_PUT,              /* 32 bit data */
00286     NO_GET, NO_GETS, NO_PUT,              /* 16 bit data */
00287     NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit hdrs */
00288     NO_GET, NO_GETS, NO_PUT,              /* 32 bit hdrs */
00289     NO_GET, NO_GETS, NO_PUT,              /* 16 bit hdrs */
00290 
00291     {                       /* bfd_check_format */
00292       _bfd_dummy_target,           /* unknown format */
00293       _bfd_dummy_target,           /* object file */
00294       _bfd_dummy_target,           /* archive */
00295       trad_unix_core_file_p        /* a core file */
00296     },
00297     {                       /* bfd_set_format */
00298       bfd_false, bfd_false,
00299       bfd_false, bfd_false
00300     },
00301     {                       /* bfd_write_contents */
00302       bfd_false, bfd_false,
00303       bfd_false, bfd_false
00304     },
00305 
00306     BFD_JUMP_TABLE_GENERIC (_bfd_generic),
00307     BFD_JUMP_TABLE_COPY (_bfd_generic),
00308     BFD_JUMP_TABLE_CORE (trad_unix),
00309     BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
00310     BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
00311     BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
00312     BFD_JUMP_TABLE_WRITE (_bfd_generic),
00313     BFD_JUMP_TABLE_LINK (_bfd_nolink),
00314     BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
00315 
00316     NULL,
00317 
00318     (PTR) 0                 /* backend_data */
00319   };