Back to index

cell-binutils  2.17cvs20070401
hpux-core.c
Go to the documentation of this file.
00001 /* BFD back-end for HP/UX core files.
00002    Copyright 1993, 1994, 1996, 1998, 1999, 2001, 2002, 2003, 2004, 2006
00003    Free Software Foundation, Inc.
00004    Written by Stu Grossman, Cygnus Support.
00005    Converted to back-end form by Ian Lance Taylor, 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 /* This file can only be compiled on systems which use HP/UX style
00024    core files.  */
00025 
00026 #include "bfd.h"
00027 #include "sysdep.h"
00028 #include "libbfd.h"
00029 
00030 #if defined (HOST_HPPAHPUX) || defined (HOST_HP300HPUX) || defined (HOST_HPPAMPEIX)
00031 
00032 /* FIXME: sys/core.h doesn't exist for HPUX version 7.  HPUX version
00033    5, 6, and 7 core files seem to be standard trad-core.c type core
00034    files; can we just use trad-core.c in addition to this file?  */
00035 
00036 #include <sys/core.h>
00037 #include <sys/utsname.h>
00038 
00039 #endif /* HOST_HPPAHPUX */
00040 
00041 #ifdef HOST_HPPABSD
00042 
00043 /* Not a very swift place to put it, but that's where the BSD port
00044    puts them.  */
00045 #include "/hpux/usr/include/sys/core.h"
00046 
00047 #endif /* HOST_HPPABSD */
00048 
00049 #include <sys/param.h>
00050 #ifdef HAVE_DIRENT_H
00051 # include <dirent.h>
00052 #else
00053 # ifdef HAVE_SYS_NDIR_H
00054 #  include <sys/ndir.h>
00055 # endif
00056 # ifdef HAVE_SYS_DIR_H
00057 #  include <sys/dir.h>
00058 # endif
00059 # ifdef HAVE_NDIR_H
00060 #  include <ndir.h>
00061 # endif
00062 #endif
00063 #include <signal.h>
00064 #ifdef HPUX_CORE
00065 #include <machine/reg.h>
00066 #endif
00067 #include <sys/user.h>              /* After a.out.h  */
00068 #include <sys/file.h>
00069 
00070 /* Kludge: There's no explicit mechanism provided by sys/core.h to
00071    conditionally know whether a proc_info has thread id fields.
00072    However, CORE_ANON_SHMEM shows up first at 10.30, which is
00073    happily also when meaningful thread id's show up in proc_info. */
00074 #if defined(CORE_ANON_SHMEM)
00075 #define PROC_INFO_HAS_THREAD_ID (1)
00076 #endif
00077 
00078 /* This type appears at HP-UX 10.30.  Defining it if not defined
00079    by sys/core.h allows us to build for older HP-UX's, and (since
00080    it won't be encountered in core-dumps from older HP-UX's) is
00081    harmless. */
00082 #if !defined(CORE_ANON_SHMEM)
00083 #define CORE_ANON_SHMEM 0x00000200         /* anonymous shared memory */
00084 #endif
00085 
00086 /* These are stored in the bfd's tdata */
00087 
00088 /* .lwpid and .user_tid are only valid if PROC_INFO_HAS_THREAD_ID, else they
00089    are set to 0.  Also, until HP-UX implements MxN threads, .user_tid and
00090    .lwpid are synonymous. */
00091 struct hpux_core_struct
00092 {
00093   int sig;
00094   int lwpid;               /* Kernel thread ID. */
00095   unsigned long user_tid;  /* User thread ID. */
00096   char cmd[MAXCOMLEN + 1];
00097 };
00098 
00099 #define core_hdr(bfd) ((bfd)->tdata.hpux_core_data)
00100 #define core_signal(bfd) (core_hdr(bfd)->sig)
00101 #define core_command(bfd) (core_hdr(bfd)->cmd)
00102 #define core_kernel_thread_id(bfd) (core_hdr(bfd)->lwpid)
00103 #define core_user_thread_id(bfd) (core_hdr(bfd)->user_tid)
00104 #define hpux_core_core_file_matches_executable_p generic_core_file_matches_executable_p
00105 
00106 static asection *make_bfd_asection (bfd *, const char *, flagword,
00107                                     bfd_size_type, bfd_vma, unsigned int);
00108 static const bfd_target *hpux_core_core_file_p (bfd *);
00109 static char *hpux_core_core_file_failing_command (bfd *);
00110 static int hpux_core_core_file_failing_signal (bfd *);
00111 static void swap_abort (void);
00112 
00113 static asection *
00114 make_bfd_asection (bfd *abfd, const char *name, flagword flags,
00115                    bfd_size_type size, bfd_vma vma,
00116                    unsigned int alignment_power)
00117 {
00118   asection *asect;
00119   char *newname;
00120 
00121   newname = bfd_alloc (abfd, (bfd_size_type) strlen (name) + 1);
00122   if (!newname)
00123     return NULL;
00124 
00125   strcpy (newname, name);
00126 
00127   asect = bfd_make_section_anyway_with_flags (abfd, newname, flags);
00128   if (!asect)
00129     return NULL;
00130 
00131   asect->size = size;
00132   asect->vma = vma;
00133   asect->filepos = bfd_tell (abfd);
00134   asect->alignment_power = alignment_power;
00135 
00136   return asect;
00137 }
00138 
00139 /* Return true if the given core file section corresponds to a thread,
00140    based on its name.  */
00141 
00142 static int
00143 thread_section_p (bfd *abfd ATTRIBUTE_UNUSED,
00144                   asection *sect,
00145                   void *obj ATTRIBUTE_UNUSED)
00146 {
00147   return CONST_STRNEQ (sect->name, ".reg/");
00148 }
00149 
00150 /* this function builds a bfd target if the file is a corefile.
00151    It returns null or 0 if it finds out thaat it is not a core file.
00152    The way it checks this is by looking for allowed 'type' field values.
00153    These are declared in sys/core.h
00154    There are some values which are 'reserved for future use'. In particular
00155    CORE_NONE is actually defined as 0. This may be a catch-all for cases
00156    in which the core file is generated by some non-hpux application.
00157    (I am just guessing here!)
00158 */
00159 static const bfd_target *
00160 hpux_core_core_file_p (bfd *abfd)
00161 {
00162   int  good_sections = 0;
00163   int  unknown_sections = 0;
00164 
00165   core_hdr (abfd) = (struct hpux_core_struct *)
00166     bfd_zalloc (abfd, (bfd_size_type) sizeof (struct hpux_core_struct));
00167   if (!core_hdr (abfd))
00168     return NULL;
00169 
00170   while (1)
00171     {
00172       int val;
00173       struct corehead core_header;
00174 
00175       val = bfd_bread ((void *) &core_header,
00176                     (bfd_size_type) sizeof core_header, abfd);
00177       if (val <= 0)
00178        break;
00179       switch (core_header.type)
00180        {
00181        case CORE_KERNEL:
00182        case CORE_FORMAT:
00183          /* Just skip this.  */
00184          bfd_seek (abfd, (file_ptr) core_header.len, SEEK_CUR);
00185           good_sections++;
00186          break;
00187        case CORE_EXEC:
00188          {
00189            struct proc_exec proc_exec;
00190            if (bfd_bread ((void *) &proc_exec, (bfd_size_type) core_header.len,
00191                        abfd) != core_header.len)
00192              break;
00193            strncpy (core_command (abfd), proc_exec.cmd, MAXCOMLEN + 1);
00194             good_sections++;
00195          }
00196          break;
00197        case CORE_PROC:
00198          {
00199            struct proc_info proc_info;
00200            char  secname[100];  /* Of arbitrary size, but plenty large. */
00201 
00202             /* We need to read this section, 'cause we need to determine
00203                whether the core-dumped app was threaded before we create
00204                any .reg sections. */
00205            if (bfd_bread (&proc_info, (bfd_size_type) core_header.len, abfd)
00206               != core_header.len)
00207              break;
00208 
00209               /* However, we also want to create those sections with the
00210                  file positioned at the start of the record, it seems. */
00211             if (bfd_seek (abfd, -((file_ptr) core_header.len), SEEK_CUR) != 0)
00212               break;
00213 
00214 #if defined(PROC_INFO_HAS_THREAD_ID)
00215             core_kernel_thread_id (abfd) = proc_info.lwpid;
00216             core_user_thread_id (abfd) = proc_info.user_tid;
00217 #else
00218             core_kernel_thread_id (abfd) = 0;
00219             core_user_thread_id (abfd) = 0;
00220 #endif
00221             /* If the program was unthreaded, then we'll just create a
00222                .reg section.
00223 
00224                If the program was threaded, then we'll create .reg/XXXXX
00225                section for each thread, where XXXXX is a printable
00226                representation of the kernel thread id.  We'll also
00227                create a .reg section for the thread that was running
00228                and signalled at the time of the core-dump (i.e., this
00229                is effectively an alias, needed to keep GDB happy.)
00230 
00231                Note that we use `.reg/XXXXX' as opposed to '.regXXXXX'
00232                because GDB expects that .reg2 will be the floating-
00233                point registers. */
00234             if (core_kernel_thread_id (abfd) == 0)
00235               {
00236                 if (!make_bfd_asection (abfd, ".reg",
00237                                    SEC_HAS_CONTENTS,
00238                                    core_header.len,
00239                                    (bfd_vma) offsetof (struct proc_info,
00240                                                      hw_regs),
00241                                    2))
00242                 goto fail;
00243               }
00244             else
00245               {
00246                 /* There are threads.  Is this the one that caused the
00247                    core-dump?  We'll claim it was the running thread. */
00248                 if (proc_info.sig != -1)
00249                   {
00250                   if (!make_bfd_asection (abfd, ".reg",
00251                                        SEC_HAS_CONTENTS,
00252                                        core_header.len,
00253                                        (bfd_vma)offsetof (struct proc_info,
00254                                                         hw_regs),
00255                                        2))
00256                     goto fail;
00257                   }
00258                 /* We always make one of these sections, for every thread. */
00259                 sprintf (secname, ".reg/%d", core_kernel_thread_id (abfd));
00260                 if (!make_bfd_asection (abfd, secname,
00261                                    SEC_HAS_CONTENTS,
00262                                    core_header.len,
00263                                    (bfd_vma) offsetof (struct proc_info,
00264                                                      hw_regs),
00265                                    2))
00266                 goto fail;
00267               }
00268            core_signal (abfd) = proc_info.sig;
00269             if (bfd_seek (abfd, (file_ptr) core_header.len, SEEK_CUR) != 0)
00270               break;
00271             good_sections++;
00272          }
00273          break;
00274 
00275        case CORE_DATA:
00276        case CORE_STACK:
00277        case CORE_TEXT:
00278        case CORE_MMF:
00279        case CORE_SHM:
00280        case CORE_ANON_SHMEM:
00281          if (!make_bfd_asection (abfd, ".data",
00282                               SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS,
00283                               core_header.len,
00284                               (bfd_vma) core_header.addr, 2))
00285            goto fail;
00286 
00287          bfd_seek (abfd, (file_ptr) core_header.len, SEEK_CUR);
00288           good_sections++;
00289          break;
00290 
00291        case CORE_NONE:
00292           /* Let's not punt if we encounter a section of unknown
00293              type.  Rather, let's make a note of it.  If we later
00294              see that there were also "good" sections, then we'll
00295              declare that this a core file, but we'll also warn that
00296              it may be incompatible with this gdb.
00297              */
00298          unknown_sections++;
00299           break;
00300 
00301          default:
00302           goto fail; /*unrecognized core file type */
00303        }
00304     }
00305 
00306   /* OK, we believe you.  You're a core file (sure, sure).  */
00307 
00308   /* On HP/UX, we sometimes encounter core files where none of the threads
00309      was found to be the running thread (ie the signal was set to -1 for
00310      all threads).  This happens when the program was aborted externally
00311      via a TT_CORE ttrace system call.  In that case, we just pick one
00312      thread at random to be the active thread.  */
00313   if (core_kernel_thread_id (abfd) != 0
00314       && bfd_get_section_by_name (abfd, ".reg") == NULL)
00315     {
00316       asection *asect = bfd_sections_find_if (abfd, thread_section_p, NULL);
00317       asection *reg_sect;
00318 
00319       if (asect != NULL)
00320         {
00321           reg_sect = make_bfd_asection (abfd, ".reg", asect->flags,
00322                                         asect->size, asect->vma,
00323                                         asect->alignment_power);
00324           if (reg_sect == NULL)
00325             goto fail;
00326 
00327           reg_sect->filepos = asect->filepos;
00328         }
00329     }
00330 
00331   /* Were there sections of unknown type?  If so, yet there were
00332      at least some complete sections of known type, then, issue
00333      a warning.  Possibly the core file was generated on a version
00334      of HP-UX that is incompatible with that for which this gdb was
00335      built.
00336      */
00337   if ((unknown_sections > 0) && (good_sections > 0))
00338     (*_bfd_error_handler)
00339       ("%s appears to be a core file,\nbut contains unknown sections.  It may have been created on an incompatible\nversion of HP-UX.  As a result, some information may be unavailable.\n",
00340        abfd->filename);
00341 
00342   return abfd->xvec;
00343 
00344  fail:
00345   bfd_release (abfd, core_hdr (abfd));
00346   core_hdr (abfd) = NULL;
00347   bfd_section_list_clear (abfd);
00348   return NULL;
00349 }
00350 
00351 static char *
00352 hpux_core_core_file_failing_command (bfd *abfd)
00353 {
00354   return core_command (abfd);
00355 }
00356 
00357 static int
00358 hpux_core_core_file_failing_signal (bfd *abfd)
00359 {
00360   return core_signal (abfd);
00361 }
00362 
00363 
00364 /* If somebody calls any byte-swapping routines, shoot them.  */
00365 static void
00366 swap_abort (void)
00367 {
00368   abort(); /* This way doesn't require any declaration for ANSI to fuck up */
00369 }
00370 
00371 #define       NO_GET ((bfd_vma (*) (const void *)) swap_abort)
00372 #define       NO_PUT ((void (*) (bfd_vma, void *)) swap_abort)
00373 #define       NO_GETS ((bfd_signed_vma (*) (const void *)) swap_abort)
00374 #define       NO_GET64 ((bfd_uint64_t (*) (const void *)) swap_abort)
00375 #define       NO_PUT64 ((void (*) (bfd_uint64_t, void *)) swap_abort)
00376 #define       NO_GETS64 ((bfd_int64_t (*) (const void *)) swap_abort)
00377 
00378 const bfd_target hpux_core_vec =
00379   {
00380     "hpux-core",
00381     bfd_target_unknown_flavour,
00382     BFD_ENDIAN_BIG,         /* target byte order */
00383     BFD_ENDIAN_BIG,         /* target headers byte order */
00384     (HAS_RELOC | EXEC_P |   /* object flags */
00385      HAS_LINENO | HAS_DEBUG |
00386      HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
00387     (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
00388     0,                                                  /* symbol prefix */
00389     ' ',                                            /* ar_pad_char */
00390     16,                                                    /* ar_max_namelen */
00391     NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit data */
00392     NO_GET, NO_GETS, NO_PUT,              /* 32 bit data */
00393     NO_GET, NO_GETS, NO_PUT,              /* 16 bit data */
00394     NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit hdrs */
00395     NO_GET, NO_GETS, NO_PUT,              /* 32 bit hdrs */
00396     NO_GET, NO_GETS, NO_PUT,              /* 16 bit hdrs */
00397 
00398     {                       /* bfd_check_format */
00399       _bfd_dummy_target,           /* unknown format */
00400       _bfd_dummy_target,           /* object file */
00401       _bfd_dummy_target,           /* archive */
00402       hpux_core_core_file_p        /* a core file */
00403     },
00404     {                       /* bfd_set_format */
00405       bfd_false, bfd_false,
00406       bfd_false, bfd_false
00407     },
00408     {                       /* bfd_write_contents */
00409       bfd_false, bfd_false,
00410       bfd_false, bfd_false
00411     },
00412 
00413     BFD_JUMP_TABLE_GENERIC (_bfd_generic),
00414     BFD_JUMP_TABLE_COPY (_bfd_generic),
00415     BFD_JUMP_TABLE_CORE (hpux_core),
00416     BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
00417     BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
00418     BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
00419     BFD_JUMP_TABLE_WRITE (_bfd_generic),
00420     BFD_JUMP_TABLE_LINK (_bfd_nolink),
00421     BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
00422 
00423     NULL,
00424 
00425     (PTR) 0                 /* backend_data */
00426   };