12159047fSniklas /* BFD back-end for HP/UX core files.
2*007c2a45Smiod Copyright 1993, 1994, 1996, 1998, 1999, 2001, 2002, 2003, 2004
3b55d4692Sfgsch Free Software Foundation, Inc.
42159047fSniklas Written by Stu Grossman, Cygnus Support.
52159047fSniklas Converted to back-end form by Ian Lance Taylor, Cygnus SUpport
62159047fSniklas
72159047fSniklas This file is part of BFD, the Binary File Descriptor library.
82159047fSniklas
92159047fSniklas This program is free software; you can redistribute it and/or modify
102159047fSniklas it under the terms of the GNU General Public License as published by
112159047fSniklas the Free Software Foundation; either version 2 of the License, or
122159047fSniklas (at your option) any later version.
132159047fSniklas
142159047fSniklas This program is distributed in the hope that it will be useful,
152159047fSniklas but WITHOUT ANY WARRANTY; without even the implied warranty of
162159047fSniklas MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
172159047fSniklas GNU General Public License for more details.
182159047fSniklas
192159047fSniklas You should have received a copy of the GNU General Public License
202159047fSniklas along with this program; if not, write to the Free Software
212159047fSniklas Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
222159047fSniklas
232159047fSniklas /* This file can only be compiled on systems which use HP/UX style
242159047fSniklas core files. */
252159047fSniklas
262159047fSniklas #include "bfd.h"
272159047fSniklas #include "sysdep.h"
282159047fSniklas #include "libbfd.h"
292159047fSniklas
30b305b0f1Sespie #if defined (HOST_HPPAHPUX) || defined (HOST_HP300HPUX) || defined (HOST_HPPAMPEIX)
312159047fSniklas
322159047fSniklas /* FIXME: sys/core.h doesn't exist for HPUX version 7. HPUX version
332159047fSniklas 5, 6, and 7 core files seem to be standard trad-core.c type core
342159047fSniklas files; can we just use trad-core.c in addition to this file? */
352159047fSniklas
362159047fSniklas #include <sys/core.h>
372159047fSniklas #include <sys/utsname.h>
382159047fSniklas
392159047fSniklas #endif /* HOST_HPPAHPUX */
402159047fSniklas
412159047fSniklas #ifdef HOST_HPPABSD
422159047fSniklas
432159047fSniklas /* Not a very swift place to put it, but that's where the BSD port
442159047fSniklas puts them. */
452159047fSniklas #include "/hpux/usr/include/sys/core.h"
462159047fSniklas
472159047fSniklas #endif /* HOST_HPPABSD */
482159047fSniklas
492159047fSniklas #include <sys/param.h>
50b305b0f1Sespie #ifdef HAVE_DIRENT_H
51b305b0f1Sespie # include <dirent.h>
52b305b0f1Sespie #else
53b305b0f1Sespie # ifdef HAVE_SYS_NDIR_H
54b305b0f1Sespie # include <sys/ndir.h>
55b305b0f1Sespie # endif
56b305b0f1Sespie # ifdef HAVE_SYS_DIR_H
572159047fSniklas # include <sys/dir.h>
58b305b0f1Sespie # endif
59b305b0f1Sespie # ifdef HAVE_NDIR_H
60b305b0f1Sespie # include <ndir.h>
61b305b0f1Sespie # endif
62b305b0f1Sespie #endif
632159047fSniklas #include <signal.h>
642159047fSniklas #include <machine/reg.h>
652159047fSniklas #include <sys/user.h> /* After a.out.h */
662159047fSniklas #include <sys/file.h>
67b305b0f1Sespie
68b305b0f1Sespie /* Kludge: There's no explicit mechanism provided by sys/core.h to
69b305b0f1Sespie conditionally know whether a proc_info has thread id fields.
70b305b0f1Sespie However, CORE_ANON_SHMEM shows up first at 10.30, which is
71b305b0f1Sespie happily also when meaningful thread id's show up in proc_info. */
72b305b0f1Sespie #if defined(CORE_ANON_SHMEM)
73b305b0f1Sespie #define PROC_INFO_HAS_THREAD_ID (1)
74b305b0f1Sespie #endif
75b305b0f1Sespie
76b305b0f1Sespie /* This type appears at HP-UX 10.30. Defining it if not defined
77b305b0f1Sespie by sys/core.h allows us to build for older HP-UX's, and (since
78b305b0f1Sespie it won't be encountered in core-dumps from older HP-UX's) is
79b305b0f1Sespie harmless. */
80b305b0f1Sespie #if !defined(CORE_ANON_SHMEM)
81b305b0f1Sespie #define CORE_ANON_SHMEM 0x00000200 /* anonymous shared memory */
82b305b0f1Sespie #endif
832159047fSniklas
842159047fSniklas /* These are stored in the bfd's tdata */
852159047fSniklas
86b305b0f1Sespie /* .lwpid and .user_tid are only valid if PROC_INFO_HAS_THREAD_ID, else they
87b305b0f1Sespie are set to 0. Also, until HP-UX implements MxN threads, .user_tid and
88b305b0f1Sespie .lwpid are synonymous. */
892159047fSniklas struct hpux_core_struct
902159047fSniklas {
912159047fSniklas int sig;
92b305b0f1Sespie int lwpid; /* Kernel thread ID. */
93b305b0f1Sespie unsigned long user_tid; /* User thread ID. */
942159047fSniklas char cmd[MAXCOMLEN + 1];
952159047fSniklas };
962159047fSniklas
972159047fSniklas #define core_hdr(bfd) ((bfd)->tdata.hpux_core_data)
982159047fSniklas #define core_signal(bfd) (core_hdr(bfd)->sig)
992159047fSniklas #define core_command(bfd) (core_hdr(bfd)->cmd)
100b305b0f1Sespie #define core_kernel_thread_id(bfd) (core_hdr(bfd)->lwpid)
101b305b0f1Sespie #define core_user_thread_id(bfd) (core_hdr(bfd)->user_tid)
1022159047fSniklas
103c074d1c9Sdrahn static asection *make_bfd_asection
104c074d1c9Sdrahn PARAMS ((bfd *, const char *, flagword, bfd_size_type, bfd_vma,
105c074d1c9Sdrahn unsigned int));
106c074d1c9Sdrahn static const bfd_target *hpux_core_core_file_p
107c074d1c9Sdrahn PARAMS ((bfd *));
108c074d1c9Sdrahn static char *hpux_core_core_file_failing_command
109c074d1c9Sdrahn PARAMS ((bfd *));
110c074d1c9Sdrahn static int hpux_core_core_file_failing_signal
111c074d1c9Sdrahn PARAMS ((bfd *));
112c074d1c9Sdrahn static bfd_boolean hpux_core_core_file_matches_executable_p
113c074d1c9Sdrahn PARAMS ((bfd *, bfd *));
114c074d1c9Sdrahn static void swap_abort
115c074d1c9Sdrahn PARAMS ((void));
116e93f7393Sniklas
1172159047fSniklas static asection *
make_bfd_asection(abfd,name,flags,_raw_size,vma,alignment_power)1182159047fSniklas make_bfd_asection (abfd, name, flags, _raw_size, vma, alignment_power)
1192159047fSniklas bfd *abfd;
120c074d1c9Sdrahn const char *name;
1212159047fSniklas flagword flags;
1222159047fSniklas bfd_size_type _raw_size;
1232159047fSniklas bfd_vma vma;
1242159047fSniklas unsigned int alignment_power;
1252159047fSniklas {
1262159047fSniklas asection *asect;
127b305b0f1Sespie char *newname;
1282159047fSniklas
129c074d1c9Sdrahn newname = bfd_alloc (abfd, (bfd_size_type) strlen (name) + 1);
130b305b0f1Sespie if (!newname)
131b305b0f1Sespie return NULL;
132b305b0f1Sespie
133b305b0f1Sespie strcpy (newname, name);
134b305b0f1Sespie
135b305b0f1Sespie asect = bfd_make_section_anyway (abfd, newname);
1362159047fSniklas if (!asect)
1372159047fSniklas return NULL;
1382159047fSniklas
1392159047fSniklas asect->flags = flags;
1402159047fSniklas asect->_raw_size = _raw_size;
1412159047fSniklas asect->vma = vma;
1422159047fSniklas asect->filepos = bfd_tell (abfd);
1432159047fSniklas asect->alignment_power = alignment_power;
1442159047fSniklas
1452159047fSniklas return asect;
1462159047fSniklas }
1472159047fSniklas
148b305b0f1Sespie /* this function builds a bfd target if the file is a corefile.
149b305b0f1Sespie It returns null or 0 if it finds out thaat it is not a core file.
150b305b0f1Sespie The way it checks this is by looking for allowed 'type' field values.
151b305b0f1Sespie These are declared in sys/core.h
152b305b0f1Sespie There are some values which are 'reserved for future use'. In particular
153b305b0f1Sespie CORE_NONE is actually defined as 0. This may be a catch-all for cases
154b305b0f1Sespie in which the core file is generated by some non-hpux application.
155b305b0f1Sespie (I am just guessing here!)
156b305b0f1Sespie */
1572159047fSniklas static const bfd_target *
hpux_core_core_file_p(abfd)1582159047fSniklas hpux_core_core_file_p (abfd)
1592159047fSniklas bfd *abfd;
1602159047fSniklas {
161b305b0f1Sespie int good_sections = 0;
162b305b0f1Sespie int unknown_sections = 0;
163b305b0f1Sespie
1642159047fSniklas core_hdr (abfd) = (struct hpux_core_struct *)
165c074d1c9Sdrahn bfd_zalloc (abfd, (bfd_size_type) sizeof (struct hpux_core_struct));
1662159047fSniklas if (!core_hdr (abfd))
1672159047fSniklas return NULL;
1682159047fSniklas
1692159047fSniklas while (1)
1702159047fSniklas {
1712159047fSniklas int val;
1722159047fSniklas struct corehead core_header;
1732159047fSniklas
174c074d1c9Sdrahn val = bfd_bread ((void *) &core_header,
175c074d1c9Sdrahn (bfd_size_type) sizeof core_header, abfd);
1762159047fSniklas if (val <= 0)
1772159047fSniklas break;
1782159047fSniklas switch (core_header.type)
1792159047fSniklas {
1802159047fSniklas case CORE_KERNEL:
1812159047fSniklas case CORE_FORMAT:
182c074d1c9Sdrahn /* Just skip this. */
183c074d1c9Sdrahn bfd_seek (abfd, (file_ptr) core_header.len, SEEK_CUR);
184b305b0f1Sespie good_sections++;
1852159047fSniklas break;
1862159047fSniklas case CORE_EXEC:
1872159047fSniklas {
1882159047fSniklas struct proc_exec proc_exec;
189c074d1c9Sdrahn if (bfd_bread ((void *) &proc_exec, (bfd_size_type) core_header.len,
190c074d1c9Sdrahn abfd) != core_header.len)
1912159047fSniklas break;
1922159047fSniklas strncpy (core_command (abfd), proc_exec.cmd, MAXCOMLEN + 1);
193b305b0f1Sespie good_sections++;
1942159047fSniklas }
1952159047fSniklas break;
1962159047fSniklas case CORE_PROC:
1972159047fSniklas {
1982159047fSniklas struct proc_info proc_info;
199b305b0f1Sespie char secname[100]; /* Of arbitrary size, but plenty large. */
200b305b0f1Sespie
201b305b0f1Sespie /* We need to read this section, 'cause we need to determine
202b305b0f1Sespie whether the core-dumped app was threaded before we create
203b305b0f1Sespie any .reg sections. */
204c074d1c9Sdrahn if (bfd_bread (&proc_info, (bfd_size_type) core_header.len, abfd)
205b305b0f1Sespie != core_header.len)
206b305b0f1Sespie break;
207b305b0f1Sespie
208b305b0f1Sespie /* However, we also want to create those sections with the
209b305b0f1Sespie file positioned at the start of the record, it seems. */
210c074d1c9Sdrahn if (bfd_seek (abfd, (file_ptr) -core_header.len, SEEK_CUR) != 0)
211b305b0f1Sespie break;
212b305b0f1Sespie
213b305b0f1Sespie #if defined(PROC_INFO_HAS_THREAD_ID)
214b305b0f1Sespie core_kernel_thread_id (abfd) = proc_info.lwpid;
215b305b0f1Sespie core_user_thread_id (abfd) = proc_info.user_tid;
216b305b0f1Sespie #else
217b305b0f1Sespie core_kernel_thread_id (abfd) = 0;
218b305b0f1Sespie core_user_thread_id (abfd) = 0;
219b305b0f1Sespie #endif
220b305b0f1Sespie /* If the program was unthreaded, then we'll just create a
221b305b0f1Sespie .reg section.
222b305b0f1Sespie
223b305b0f1Sespie If the program was threaded, then we'll create .reg/XXXXX
224b305b0f1Sespie section for each thread, where XXXXX is a printable
225b305b0f1Sespie representation of the kernel thread id. We'll also
226b305b0f1Sespie create a .reg section for the thread that was running
227b305b0f1Sespie and signalled at the time of the core-dump (i.e., this
228b305b0f1Sespie is effectively an alias, needed to keep GDB happy.)
229b305b0f1Sespie
230b305b0f1Sespie Note that we use `.reg/XXXXX' as opposed to '.regXXXXX'
231b305b0f1Sespie because GDB expects that .reg2 will be the floating-
232b305b0f1Sespie point registers. */
233b305b0f1Sespie if (core_kernel_thread_id (abfd) == 0)
234b305b0f1Sespie {
2352159047fSniklas if (!make_bfd_asection (abfd, ".reg",
2362159047fSniklas SEC_HAS_CONTENTS,
2372159047fSniklas core_header.len,
2382159047fSniklas (int) &proc_info - (int) & proc_info.hw_regs,
2392159047fSniklas 2))
240c074d1c9Sdrahn goto fail;
241b305b0f1Sespie }
242b305b0f1Sespie else
243b305b0f1Sespie {
244b305b0f1Sespie /* There are threads. Is this the one that caused the
245b305b0f1Sespie core-dump? We'll claim it was the running thread. */
246b305b0f1Sespie if (proc_info.sig != -1)
247b305b0f1Sespie {
248b305b0f1Sespie if (!make_bfd_asection (abfd, ".reg",
249b305b0f1Sespie SEC_HAS_CONTENTS,
250b305b0f1Sespie core_header.len,
251b305b0f1Sespie (int) &proc_info - (int) & proc_info.hw_regs,
252b305b0f1Sespie 2))
253c074d1c9Sdrahn goto fail;
254b305b0f1Sespie }
255b305b0f1Sespie /* We always make one of these sections, for every thread. */
256b305b0f1Sespie sprintf (secname, ".reg/%d", core_kernel_thread_id (abfd));
257b305b0f1Sespie if (!make_bfd_asection (abfd, secname,
258b305b0f1Sespie SEC_HAS_CONTENTS,
259b305b0f1Sespie core_header.len,
260b305b0f1Sespie (int) &proc_info - (int) & proc_info.hw_regs,
261b305b0f1Sespie 2))
262c074d1c9Sdrahn goto fail;
263b305b0f1Sespie }
2642159047fSniklas core_signal (abfd) = proc_info.sig;
265c074d1c9Sdrahn if (bfd_seek (abfd, (file_ptr) core_header.len, SEEK_CUR) != 0)
266b305b0f1Sespie break;
267b305b0f1Sespie good_sections++;
2682159047fSniklas }
2692159047fSniklas break;
2702159047fSniklas
2712159047fSniklas case CORE_DATA:
2722159047fSniklas case CORE_STACK:
2732159047fSniklas case CORE_TEXT:
2742159047fSniklas case CORE_MMF:
2752159047fSniklas case CORE_SHM:
276b305b0f1Sespie case CORE_ANON_SHMEM:
2772159047fSniklas if (!make_bfd_asection (abfd, ".data",
2782159047fSniklas SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS,
2792159047fSniklas core_header.len, core_header.addr, 2))
280c074d1c9Sdrahn goto fail;
2812159047fSniklas
282c074d1c9Sdrahn bfd_seek (abfd, (file_ptr) core_header.len, SEEK_CUR);
283b305b0f1Sespie good_sections++;
2842159047fSniklas break;
2852159047fSniklas
286b305b0f1Sespie case CORE_NONE:
287b305b0f1Sespie /* Let's not punt if we encounter a section of unknown
288b305b0f1Sespie type. Rather, let's make a note of it. If we later
289b305b0f1Sespie see that there were also "good" sections, then we'll
290b305b0f1Sespie declare that this a core file, but we'll also warn that
291b305b0f1Sespie it may be incompatible with this gdb.
292b305b0f1Sespie */
293b305b0f1Sespie unknown_sections++;
294b305b0f1Sespie break;
295b305b0f1Sespie
296c074d1c9Sdrahn default:
297c074d1c9Sdrahn goto fail; /*unrecognized core file type */
2982159047fSniklas }
2992159047fSniklas }
3002159047fSniklas
3012159047fSniklas /* OK, we believe you. You're a core file (sure, sure). */
3022159047fSniklas
303b305b0f1Sespie /* Were there sections of unknown type? If so, yet there were
304b305b0f1Sespie at least some complete sections of known type, then, issue
305b305b0f1Sespie a warning. Possibly the core file was generated on a version
306b305b0f1Sespie of HP-UX that is incompatible with that for which this gdb was
307b305b0f1Sespie built.
308b305b0f1Sespie */
309b305b0f1Sespie if ((unknown_sections > 0) && (good_sections > 0))
310b305b0f1Sespie (*_bfd_error_handler)
311b305b0f1Sespie ("%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",
312b305b0f1Sespie abfd->filename);
313b305b0f1Sespie
3142159047fSniklas return abfd->xvec;
315c074d1c9Sdrahn
316c074d1c9Sdrahn fail:
317c074d1c9Sdrahn bfd_release (abfd, core_hdr (abfd));
318c074d1c9Sdrahn core_hdr (abfd) = NULL;
319c074d1c9Sdrahn bfd_section_list_clear (abfd);
320c074d1c9Sdrahn return NULL;
3212159047fSniklas }
3222159047fSniklas
3232159047fSniklas static char *
hpux_core_core_file_failing_command(abfd)3242159047fSniklas hpux_core_core_file_failing_command (abfd)
3252159047fSniklas bfd *abfd;
3262159047fSniklas {
3272159047fSniklas return core_command (abfd);
3282159047fSniklas }
3292159047fSniklas
3302159047fSniklas static int
hpux_core_core_file_failing_signal(abfd)3312159047fSniklas hpux_core_core_file_failing_signal (abfd)
3322159047fSniklas bfd *abfd;
3332159047fSniklas {
3342159047fSniklas return core_signal (abfd);
3352159047fSniklas }
3362159047fSniklas
337c074d1c9Sdrahn static bfd_boolean
hpux_core_core_file_matches_executable_p(core_bfd,exec_bfd)3382159047fSniklas hpux_core_core_file_matches_executable_p (core_bfd, exec_bfd)
339c074d1c9Sdrahn bfd *core_bfd ATTRIBUTE_UNUSED;
340c074d1c9Sdrahn bfd *exec_bfd ATTRIBUTE_UNUSED;
3412159047fSniklas {
342c074d1c9Sdrahn return TRUE; /* FIXME, We have no way of telling at this point */
3432159047fSniklas }
3442159047fSniklas
3452159047fSniklas /* If somebody calls any byte-swapping routines, shoot them. */
346e93f7393Sniklas static void
swap_abort()3472159047fSniklas swap_abort ()
3482159047fSniklas {
3492159047fSniklas abort(); /* This way doesn't require any declaration for ANSI to fuck up */
3502159047fSniklas }
351*007c2a45Smiod
352*007c2a45Smiod #define NO_GET ((bfd_vma (*) (const void *)) swap_abort)
353*007c2a45Smiod #define NO_PUT ((void (*) (bfd_vma, void *)) swap_abort)
354*007c2a45Smiod #define NO_GETS ((bfd_signed_vma (*) (const void *)) swap_abort)
355*007c2a45Smiod #define NO_GET64 ((bfd_uint64_t (*) (const void *)) swap_abort)
356*007c2a45Smiod #define NO_PUT64 ((void (*) (bfd_uint64_t, void *)) swap_abort)
357*007c2a45Smiod #define NO_GETS64 ((bfd_int64_t (*) (const void *)) swap_abort)
3582159047fSniklas
3592159047fSniklas const bfd_target hpux_core_vec =
3602159047fSniklas {
3612159047fSniklas "hpux-core",
3622159047fSniklas bfd_target_unknown_flavour,
363c88b1d6cSniklas BFD_ENDIAN_BIG, /* target byte order */
364c88b1d6cSniklas BFD_ENDIAN_BIG, /* target headers byte order */
3652159047fSniklas (HAS_RELOC | EXEC_P | /* object flags */
3662159047fSniklas HAS_LINENO | HAS_DEBUG |
3672159047fSniklas HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
3682159047fSniklas (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
3692159047fSniklas 0, /* symbol prefix */
3702159047fSniklas ' ', /* ar_pad_char */
3712159047fSniklas 16, /* ar_max_namelen */
372*007c2a45Smiod NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit data */
373*007c2a45Smiod NO_GET, NO_GETS, NO_PUT, /* 32 bit data */
374*007c2a45Smiod NO_GET, NO_GETS, NO_PUT, /* 16 bit data */
375*007c2a45Smiod NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit hdrs */
376*007c2a45Smiod NO_GET, NO_GETS, NO_PUT, /* 32 bit hdrs */
377*007c2a45Smiod NO_GET, NO_GETS, NO_PUT, /* 16 bit hdrs */
3782159047fSniklas
3792159047fSniklas { /* bfd_check_format */
3802159047fSniklas _bfd_dummy_target, /* unknown format */
3812159047fSniklas _bfd_dummy_target, /* object file */
3822159047fSniklas _bfd_dummy_target, /* archive */
3832159047fSniklas hpux_core_core_file_p /* a core file */
3842159047fSniklas },
3852159047fSniklas { /* bfd_set_format */
3862159047fSniklas bfd_false, bfd_false,
3872159047fSniklas bfd_false, bfd_false
3882159047fSniklas },
3892159047fSniklas { /* bfd_write_contents */
3902159047fSniklas bfd_false, bfd_false,
3912159047fSniklas bfd_false, bfd_false
3922159047fSniklas },
3932159047fSniklas
3942159047fSniklas BFD_JUMP_TABLE_GENERIC (_bfd_generic),
3952159047fSniklas BFD_JUMP_TABLE_COPY (_bfd_generic),
3962159047fSniklas BFD_JUMP_TABLE_CORE (hpux_core),
3972159047fSniklas BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
398c074d1c9Sdrahn BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
3992159047fSniklas BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
4002159047fSniklas BFD_JUMP_TABLE_WRITE (_bfd_generic),
4012159047fSniklas BFD_JUMP_TABLE_LINK (_bfd_nolink),
4022159047fSniklas BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
4032159047fSniklas
404b305b0f1Sespie NULL,
405b305b0f1Sespie
4062159047fSniklas (PTR) 0 /* backend_data */
4072159047fSniklas };
408