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