OSDN Git Service

2001-01-23 Kazu Hirata <kazu@hxi.com>
[pf3gnuchains/pf3gnuchains3x.git] / bfd / hpux-core.c
1 /* BFD back-end for HP/UX core files.
2    Copyright 1993, 94, 95, 96, 97, 1998 Free Software Foundation, Inc.
3    Written by Stu Grossman, Cygnus Support.
4    Converted to back-end form by Ian Lance Taylor, Cygnus SUpport
5
6 This file is part of BFD, the Binary File Descriptor library.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22 /* This file can only be compiled on systems which use HP/UX style
23    core files.  */
24
25 #include "bfd.h"
26 #include "sysdep.h"
27 #include "libbfd.h"
28
29 #if defined (HOST_HPPAHPUX) || defined (HOST_HP300HPUX) || defined (HOST_HPPAMPEIX)
30
31 /* FIXME: sys/core.h doesn't exist for HPUX version 7.  HPUX version
32    5, 6, and 7 core files seem to be standard trad-core.c type core
33    files; can we just use trad-core.c in addition to this file?  */
34
35 #include <sys/core.h>
36 #include <sys/utsname.h>
37
38 #endif /* HOST_HPPAHPUX */
39
40 #ifdef HOST_HPPABSD
41
42 /* Not a very swift place to put it, but that's where the BSD port
43    puts them.  */
44 #include "/hpux/usr/include/sys/core.h"
45
46 #endif /* HOST_HPPABSD */
47
48 #include <sys/param.h>
49 #ifdef HAVE_DIRENT_H
50 # include <dirent.h>
51 #else
52 # ifdef HAVE_SYS_NDIR_H
53 #  include <sys/ndir.h>
54 # endif
55 # ifdef HAVE_SYS_DIR_H
56 #  include <sys/dir.h>
57 # endif
58 # ifdef HAVE_NDIR_H
59 #  include <ndir.h>
60 # endif
61 #endif
62 #include <signal.h>
63 #include <machine/reg.h>
64 #include <sys/user.h>           /* After a.out.h  */
65 #include <sys/file.h>
66
67 /* Kludge: There's no explicit mechanism provided by sys/core.h to
68    conditionally know whether a proc_info has thread id fields.
69    However, CORE_ANON_SHMEM shows up first at 10.30, which is
70    happily also when meaningful thread id's show up in proc_info. */
71 #if defined(CORE_ANON_SHMEM)
72 #define PROC_INFO_HAS_THREAD_ID (1)
73 #endif
74
75 /* This type appears at HP-UX 10.30.  Defining it if not defined
76    by sys/core.h allows us to build for older HP-UX's, and (since
77    it won't be encountered in core-dumps from older HP-UX's) is
78    harmless. */
79 #if !defined(CORE_ANON_SHMEM)
80 #define CORE_ANON_SHMEM 0x00000200         /* anonymous shared memory */
81 #endif
82
83 /* These are stored in the bfd's tdata */
84
85 /* .lwpid and .user_tid are only valid if PROC_INFO_HAS_THREAD_ID, else they
86    are set to 0.  Also, until HP-UX implements MxN threads, .user_tid and
87    .lwpid are synonymous. */
88 struct hpux_core_struct
89 {
90   int sig;
91   int lwpid;               /* Kernel thread ID. */
92   unsigned long user_tid;  /* User thread ID. */
93   char cmd[MAXCOMLEN + 1];
94 };
95
96 #define core_hdr(bfd) ((bfd)->tdata.hpux_core_data)
97 #define core_signal(bfd) (core_hdr(bfd)->sig)
98 #define core_command(bfd) (core_hdr(bfd)->cmd)
99 #define core_kernel_thread_id(bfd) (core_hdr(bfd)->lwpid)
100 #define core_user_thread_id(bfd) (core_hdr(bfd)->user_tid)
101
102 static void swap_abort PARAMS ((void));
103
104 static asection *
105 make_bfd_asection (abfd, name, flags, _raw_size, vma, alignment_power)
106      bfd *abfd;
107      CONST char *name;
108      flagword flags;
109      bfd_size_type _raw_size;
110      bfd_vma vma;
111      unsigned int alignment_power;
112 {
113   asection *asect;
114   char *newname;
115
116   newname = bfd_alloc (abfd, strlen (name) + 1);
117   if (!newname)
118     return NULL;
119
120   strcpy (newname, name);
121
122   asect = bfd_make_section_anyway (abfd, newname);
123   if (!asect)
124     return NULL;
125
126   asect->flags = flags;
127   asect->_raw_size = _raw_size;
128   asect->vma = vma;
129   asect->filepos = bfd_tell (abfd);
130   asect->alignment_power = alignment_power;
131
132   return asect;
133 }
134
135 static asymbol *
136 hpux_core_make_empty_symbol (abfd)
137      bfd *abfd;
138 {
139   asymbol *new = (asymbol *) bfd_zalloc (abfd, sizeof (asymbol));
140   if (new)
141     new->the_bfd = abfd;
142   return new;
143 }
144
145 /* this function builds a bfd target if the file is a corefile.
146    It returns null or 0 if it finds out thaat it is not a core file.
147    The way it checks this is by looking for allowed 'type' field values.
148    These are declared in sys/core.h
149    There are some values which are 'reserved for future use'. In particular
150    CORE_NONE is actually defined as 0. This may be a catch-all for cases
151    in which the core file is generated by some non-hpux application.
152    (I am just guessing here!)
153 */
154 static const bfd_target *
155 hpux_core_core_file_p (abfd)
156      bfd *abfd;
157 {
158   int  good_sections = 0;
159   int  unknown_sections = 0;
160
161   core_hdr (abfd) = (struct hpux_core_struct *)
162     bfd_zalloc (abfd, sizeof (struct hpux_core_struct));
163   if (!core_hdr (abfd))
164     return NULL;
165
166   while (1)
167     {
168       int val;
169       struct corehead core_header;
170
171       val = bfd_read ((void *) &core_header, 1, sizeof core_header, abfd);
172       if (val <= 0)
173         break;
174       switch (core_header.type)
175         {
176         case CORE_KERNEL:
177         case CORE_FORMAT:
178           bfd_seek (abfd, core_header.len, SEEK_CUR);   /* Just skip this */
179           good_sections++;
180           break;
181         case CORE_EXEC:
182           {
183             struct proc_exec proc_exec;
184             if (bfd_read ((void *) &proc_exec, 1, core_header.len, abfd)
185                 != core_header.len)
186               break;
187             strncpy (core_command (abfd), proc_exec.cmd, MAXCOMLEN + 1);
188             good_sections++;
189           }
190           break;
191         case CORE_PROC:
192           {
193             struct proc_info proc_info;
194             char  secname[100];  /* Of arbitrary size, but plenty large. */
195
196             /* We need to read this section, 'cause we need to determine
197                whether the core-dumped app was threaded before we create
198                any .reg sections. */
199             if (bfd_read (&proc_info, 1, core_header.len, abfd)
200                 != core_header.len)
201               break;
202
203               /* However, we also want to create those sections with the
204                  file positioned at the start of the record, it seems. */
205             if (bfd_seek (abfd, -core_header.len, SEEK_CUR) != 0)
206               break;
207
208 #if defined(PROC_INFO_HAS_THREAD_ID)
209             core_kernel_thread_id (abfd) = proc_info.lwpid;
210             core_user_thread_id (abfd) = proc_info.user_tid;
211 #else
212             core_kernel_thread_id (abfd) = 0;
213             core_user_thread_id (abfd) = 0;
214 #endif
215             /* If the program was unthreaded, then we'll just create a
216                .reg section.
217
218                If the program was threaded, then we'll create .reg/XXXXX
219                section for each thread, where XXXXX is a printable
220                representation of the kernel thread id.  We'll also
221                create a .reg section for the thread that was running
222                and signalled at the time of the core-dump (i.e., this
223                is effectively an alias, needed to keep GDB happy.)
224
225                Note that we use `.reg/XXXXX' as opposed to '.regXXXXX'
226                because GDB expects that .reg2 will be the floating-
227                point registers. */
228             if (core_kernel_thread_id (abfd) == 0)
229               {
230                 if (!make_bfd_asection (abfd, ".reg",
231                                         SEC_HAS_CONTENTS,
232                                         core_header.len,
233                                         (int) &proc_info - (int) & proc_info.hw_regs,
234                                         2))
235                   return NULL;
236               }
237             else
238               {
239                 /* There are threads.  Is this the one that caused the
240                    core-dump?  We'll claim it was the running thread. */
241                 if (proc_info.sig != -1)
242                   {
243                     if (!make_bfd_asection (abfd, ".reg",
244                                             SEC_HAS_CONTENTS,
245                                             core_header.len,
246                                             (int) &proc_info - (int) & proc_info.hw_regs,
247                                             2))
248                       return NULL;
249                   }
250                 /* We always make one of these sections, for every thread. */
251                 sprintf (secname, ".reg/%d", core_kernel_thread_id (abfd));
252                 if (!make_bfd_asection (abfd, secname,
253                                         SEC_HAS_CONTENTS,
254                                         core_header.len,
255                                         (int) &proc_info - (int) & proc_info.hw_regs,
256                                         2))
257                   return NULL;
258               }
259             core_signal (abfd) = proc_info.sig;
260             if (bfd_seek (abfd, core_header.len, SEEK_CUR) != 0)
261               break;
262             good_sections++;
263           }
264           break;
265
266         case CORE_DATA:
267         case CORE_STACK:
268         case CORE_TEXT:
269         case CORE_MMF:
270         case CORE_SHM:
271         case CORE_ANON_SHMEM:
272           if (!make_bfd_asection (abfd, ".data",
273                                   SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS,
274                                   core_header.len, core_header.addr, 2))
275             return NULL;
276
277           bfd_seek (abfd, core_header.len, SEEK_CUR);
278           good_sections++;
279           break;
280
281         case CORE_NONE:
282           /* Let's not punt if we encounter a section of unknown
283              type.  Rather, let's make a note of it.  If we later
284              see that there were also "good" sections, then we'll
285              declare that this a core file, but we'll also warn that
286              it may be incompatible with this gdb.
287              */
288           unknown_sections++;
289           break;
290
291          default: return 0; /*unrecognized core file type */
292         }
293     }
294
295   /* OK, we believe you.  You're a core file (sure, sure).  */
296
297   /* Were there sections of unknown type?  If so, yet there were
298      at least some complete sections of known type, then, issue
299      a warning.  Possibly the core file was generated on a version
300      of HP-UX that is incompatible with that for which this gdb was
301      built.
302      */
303   if ((unknown_sections > 0) && (good_sections > 0))
304     (*_bfd_error_handler)
305       ("%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",
306        abfd->filename);
307
308   return abfd->xvec;
309 }
310
311 static char *
312 hpux_core_core_file_failing_command (abfd)
313      bfd *abfd;
314 {
315   return core_command (abfd);
316 }
317
318 /* ARGSUSED */
319 static int
320 hpux_core_core_file_failing_signal (abfd)
321      bfd *abfd;
322 {
323   return core_signal (abfd);
324 }
325
326 /* ARGSUSED */
327 static boolean
328 hpux_core_core_file_matches_executable_p (core_bfd, exec_bfd)
329      bfd *core_bfd, *exec_bfd;
330 {
331   return true;                  /* FIXME, We have no way of telling at this point */
332 }
333 \f
334 #define hpux_core_get_symtab_upper_bound _bfd_nosymbols_get_symtab_upper_bound
335 #define hpux_core_get_symtab _bfd_nosymbols_get_symtab
336 #define hpux_core_print_symbol _bfd_nosymbols_print_symbol
337 #define hpux_core_get_symbol_info _bfd_nosymbols_get_symbol_info
338 #define hpux_core_bfd_is_local_label_name \
339   _bfd_nosymbols_bfd_is_local_label_name
340 #define hpux_core_get_lineno _bfd_nosymbols_get_lineno
341 #define hpux_core_find_nearest_line _bfd_nosymbols_find_nearest_line
342 #define hpux_core_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
343 #define hpux_core_read_minisymbols _bfd_nosymbols_read_minisymbols
344 #define hpux_core_minisymbol_to_symbol _bfd_nosymbols_minisymbol_to_symbol
345
346 /* If somebody calls any byte-swapping routines, shoot them.  */
347 static void
348 swap_abort()
349 {
350   abort(); /* This way doesn't require any declaration for ANSI to fuck up */
351 }
352 #define NO_GET  ((bfd_vma (*) PARAMS ((   const bfd_byte *))) swap_abort )
353 #define NO_PUT  ((void    (*) PARAMS ((bfd_vma, bfd_byte *))) swap_abort )
354 #define NO_SIGNED_GET \
355   ((bfd_signed_vma (*) PARAMS ((const bfd_byte *))) swap_abort )
356
357 const bfd_target hpux_core_vec =
358   {
359     "hpux-core",
360     bfd_target_unknown_flavour,
361     BFD_ENDIAN_BIG,             /* target byte order */
362     BFD_ENDIAN_BIG,             /* target headers byte order */
363     (HAS_RELOC | EXEC_P |       /* object flags */
364      HAS_LINENO | HAS_DEBUG |
365      HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
366     (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
367     0,                                                     /* symbol prefix */
368     ' ',                                                   /* ar_pad_char */
369     16,                                                    /* ar_max_namelen */
370     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 64 bit data */
371     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 32 bit data */
372     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 16 bit data */
373     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 64 bit hdrs */
374     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 32 bit hdrs */
375     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 16 bit hdrs */
376
377     {                           /* bfd_check_format */
378      _bfd_dummy_target,         /* unknown format */
379      _bfd_dummy_target,         /* object file */
380      _bfd_dummy_target,         /* archive */
381      hpux_core_core_file_p      /* a core file */
382     },
383     {                           /* bfd_set_format */
384      bfd_false, bfd_false,
385      bfd_false, bfd_false
386     },
387     {                           /* bfd_write_contents */
388      bfd_false, bfd_false,
389      bfd_false, bfd_false
390     },
391
392        BFD_JUMP_TABLE_GENERIC (_bfd_generic),
393        BFD_JUMP_TABLE_COPY (_bfd_generic),
394        BFD_JUMP_TABLE_CORE (hpux_core),
395        BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
396        BFD_JUMP_TABLE_SYMBOLS (hpux_core),
397        BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
398        BFD_JUMP_TABLE_WRITE (_bfd_generic),
399        BFD_JUMP_TABLE_LINK (_bfd_nolink),
400        BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
401
402     NULL,
403
404     (PTR) 0                     /* backend_data */
405 };