1/* BFD back-end for HP/UX core files. 2 Copyright 1993, 1994, 1996, 1998, 1999, 2001, 2002, 2003, 2004, 2006 3 Free Software Foundation, Inc. 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, 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#ifdef HPUX_CORE 65#include <machine/reg.h> 66#endif 67#include <sys/user.h> /* After a.out.h */ 68#include <sys/file.h> 69 70/* Kludge: There's no explicit mechanism provided by sys/core.h to 71 conditionally know whether a proc_info has thread id fields. 72 However, CORE_ANON_SHMEM shows up first at 10.30, which is 73 happily also when meaningful thread id's show up in proc_info. */ 74#if defined(CORE_ANON_SHMEM) 75#define PROC_INFO_HAS_THREAD_ID (1) 76#endif 77 78/* This type appears at HP-UX 10.30. Defining it if not defined 79 by sys/core.h allows us to build for older HP-UX's, and (since 80 it won't be encountered in core-dumps from older HP-UX's) is 81 harmless. */ 82#if !defined(CORE_ANON_SHMEM) 83#define CORE_ANON_SHMEM 0x00000200 /* anonymous shared memory */ 84#endif 85 86/* These are stored in the bfd's tdata */ 87 88/* .lwpid and .user_tid are only valid if PROC_INFO_HAS_THREAD_ID, else they 89 are set to 0. Also, until HP-UX implements MxN threads, .user_tid and 90 .lwpid are synonymous. */ 91struct hpux_core_struct 92{ 93 int sig; 94 int lwpid; /* Kernel thread ID. */ 95 unsigned long user_tid; /* User thread ID. */ 96 char cmd[MAXCOMLEN + 1]; 97}; 98 99#define core_hdr(bfd) ((bfd)->tdata.hpux_core_data) 100#define core_signal(bfd) (core_hdr(bfd)->sig) 101#define core_command(bfd) (core_hdr(bfd)->cmd) 102#define core_kernel_thread_id(bfd) (core_hdr(bfd)->lwpid) 103#define core_user_thread_id(bfd) (core_hdr(bfd)->user_tid) 104#define hpux_core_core_file_matches_executable_p generic_core_file_matches_executable_p 105 106static asection *make_bfd_asection (bfd *, const char *, flagword, 107 bfd_size_type, bfd_vma, unsigned int); 108static const bfd_target *hpux_core_core_file_p (bfd *); 109static char *hpux_core_core_file_failing_command (bfd *); 110static int hpux_core_core_file_failing_signal (bfd *); 111static void swap_abort (void); 112 113static asection * 114make_bfd_asection (bfd *abfd, const char *name, flagword flags, 115 bfd_size_type size, bfd_vma vma, 116 unsigned int alignment_power) 117{ 118 asection *asect; 119 char *newname; 120 121 newname = bfd_alloc (abfd, (bfd_size_type) strlen (name) + 1); 122 if (!newname) 123 return NULL; 124 125 strcpy (newname, name); 126 127 asect = bfd_make_section_anyway_with_flags (abfd, newname, flags); 128 if (!asect) 129 return NULL; 130 131 asect->size = size; 132 asect->vma = vma; 133 asect->filepos = bfd_tell (abfd); 134 asect->alignment_power = alignment_power; 135 136 return asect; 137} 138 139/* Return true if the given core file section corresponds to a thread, 140 based on its name. */ 141 142static int 143thread_section_p (bfd *abfd ATTRIBUTE_UNUSED, 144 asection *sect, 145 void *obj ATTRIBUTE_UNUSED) 146{ 147 return CONST_STRNEQ (sect->name, ".reg/"); 148} 149 150/* this function builds a bfd target if the file is a corefile. 151 It returns null or 0 if it finds out thaat it is not a core file. 152 The way it checks this is by looking for allowed 'type' field values. 153 These are declared in sys/core.h 154 There are some values which are 'reserved for future use'. In particular 155 CORE_NONE is actually defined as 0. This may be a catch-all for cases 156 in which the core file is generated by some non-hpux application. 157 (I am just guessing here!) 158*/ 159static const bfd_target * 160hpux_core_core_file_p (bfd *abfd) 161{ 162 int good_sections = 0; 163 int unknown_sections = 0; 164 165 core_hdr (abfd) = (struct hpux_core_struct *) 166 bfd_zalloc (abfd, (bfd_size_type) sizeof (struct hpux_core_struct)); 167 if (!core_hdr (abfd)) 168 return NULL; 169 170 while (1) 171 { 172 int val; 173 struct corehead core_header; 174 175 val = bfd_bread ((void *) &core_header, 176 (bfd_size_type) sizeof core_header, abfd); 177 if (val <= 0) 178 break; 179 switch (core_header.type) 180 { 181 case CORE_KERNEL: 182 case CORE_FORMAT: 183 /* Just skip this. */ 184 bfd_seek (abfd, (file_ptr) core_header.len, SEEK_CUR); 185 good_sections++; 186 break; 187 case CORE_EXEC: 188 { 189 struct proc_exec proc_exec; 190 if (bfd_bread ((void *) &proc_exec, (bfd_size_type) core_header.len, 191 abfd) != core_header.len) 192 break; 193 strncpy (core_command (abfd), proc_exec.cmd, MAXCOMLEN + 1); 194 good_sections++; 195 } 196 break; 197 case CORE_PROC: 198 { 199 struct proc_info proc_info; 200 char secname[100]; /* Of arbitrary size, but plenty large. */ 201 202 /* We need to read this section, 'cause we need to determine 203 whether the core-dumped app was threaded before we create 204 any .reg sections. */ 205 if (bfd_bread (&proc_info, (bfd_size_type) core_header.len, abfd) 206 != core_header.len) 207 break; 208 209 /* However, we also want to create those sections with the 210 file positioned at the start of the record, it seems. */ 211 if (bfd_seek (abfd, -((file_ptr) core_header.len), SEEK_CUR) != 0) 212 break; 213 214#if defined(PROC_INFO_HAS_THREAD_ID) 215 core_kernel_thread_id (abfd) = proc_info.lwpid; 216 core_user_thread_id (abfd) = proc_info.user_tid; 217#else 218 core_kernel_thread_id (abfd) = 0; 219 core_user_thread_id (abfd) = 0; 220#endif 221 /* If the program was unthreaded, then we'll just create a 222 .reg section. 223 224 If the program was threaded, then we'll create .reg/XXXXX 225 section for each thread, where XXXXX is a printable 226 representation of the kernel thread id. We'll also 227 create a .reg section for the thread that was running 228 and signalled at the time of the core-dump (i.e., this 229 is effectively an alias, needed to keep GDB happy.) 230 231 Note that we use `.reg/XXXXX' as opposed to '.regXXXXX' 232 because GDB expects that .reg2 will be the floating- 233 point registers. */ 234 if (core_kernel_thread_id (abfd) == 0) 235 { 236 if (!make_bfd_asection (abfd, ".reg", 237 SEC_HAS_CONTENTS, 238 core_header.len, 239 (bfd_vma) offsetof (struct proc_info, 240 hw_regs), 241 2)) 242 goto fail; 243 } 244 else 245 { 246 /* There are threads. Is this the one that caused the 247 core-dump? We'll claim it was the running thread. */ 248 if (proc_info.sig != -1) 249 { 250 if (!make_bfd_asection (abfd, ".reg", 251 SEC_HAS_CONTENTS, 252 core_header.len, 253 (bfd_vma)offsetof (struct proc_info, 254 hw_regs), 255 2)) 256 goto fail; 257 } 258 /* We always make one of these sections, for every thread. */ 259 sprintf (secname, ".reg/%d", core_kernel_thread_id (abfd)); 260 if (!make_bfd_asection (abfd, secname, 261 SEC_HAS_CONTENTS, 262 core_header.len, 263 (bfd_vma) offsetof (struct proc_info, 264 hw_regs), 265 2)) 266 goto fail; 267 } 268 core_signal (abfd) = proc_info.sig; 269 if (bfd_seek (abfd, (file_ptr) core_header.len, SEEK_CUR) != 0) 270 break; 271 good_sections++; 272 } 273 break; 274 275 case CORE_DATA: 276 case CORE_STACK: 277 case CORE_TEXT: 278 case CORE_MMF: 279 case CORE_SHM: 280 case CORE_ANON_SHMEM: 281 if (!make_bfd_asection (abfd, ".data", 282 SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS, 283 core_header.len, 284 (bfd_vma) core_header.addr, 2)) 285 goto fail; 286 287 bfd_seek (abfd, (file_ptr) core_header.len, SEEK_CUR); 288 good_sections++; 289 break; 290 291 case CORE_NONE: 292 /* Let's not punt if we encounter a section of unknown 293 type. Rather, let's make a note of it. If we later 294 see that there were also "good" sections, then we'll 295 declare that this a core file, but we'll also warn that 296 it may be incompatible with this gdb. 297 */ 298 unknown_sections++; 299 break; 300 301 default: 302 goto fail; /*unrecognized core file type */ 303 } 304 } 305 306 /* OK, we believe you. You're a core file (sure, sure). */ 307 308 /* On HP/UX, we sometimes encounter core files where none of the threads 309 was found to be the running thread (ie the signal was set to -1 for 310 all threads). This happens when the program was aborted externally 311 via a TT_CORE ttrace system call. In that case, we just pick one 312 thread at random to be the active thread. */ 313 if (core_kernel_thread_id (abfd) != 0 314 && bfd_get_section_by_name (abfd, ".reg") == NULL) 315 { 316 asection *asect = bfd_sections_find_if (abfd, thread_section_p, NULL); 317 asection *reg_sect; 318 319 if (asect != NULL) 320 { 321 reg_sect = make_bfd_asection (abfd, ".reg", asect->flags, 322 asect->size, asect->vma, 323 asect->alignment_power); 324 if (reg_sect == NULL) 325 goto fail; 326 327 reg_sect->filepos = asect->filepos; 328 } 329 } 330 331 /* Were there sections of unknown type? If so, yet there were 332 at least some complete sections of known type, then, issue 333 a warning. Possibly the core file was generated on a version 334 of HP-UX that is incompatible with that for which this gdb was 335 built. 336 */ 337 if ((unknown_sections > 0) && (good_sections > 0)) 338 (*_bfd_error_handler) 339 ("%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", 340 abfd->filename); 341 342 return abfd->xvec; 343 344 fail: 345 bfd_release (abfd, core_hdr (abfd)); 346 core_hdr (abfd) = NULL; 347 bfd_section_list_clear (abfd); 348 return NULL; 349} 350 351static char * 352hpux_core_core_file_failing_command (bfd *abfd) 353{ 354 return core_command (abfd); 355} 356 357static int 358hpux_core_core_file_failing_signal (bfd *abfd) 359{ 360 return core_signal (abfd); 361} 362 363 364/* If somebody calls any byte-swapping routines, shoot them. */ 365static void 366swap_abort (void) 367{ 368 abort(); /* This way doesn't require any declaration for ANSI to fuck up */ 369} 370 371#define NO_GET ((bfd_vma (*) (const void *)) swap_abort) 372#define NO_PUT ((void (*) (bfd_vma, void *)) swap_abort) 373#define NO_GETS ((bfd_signed_vma (*) (const void *)) swap_abort) 374#define NO_GET64 ((bfd_uint64_t (*) (const void *)) swap_abort) 375#define NO_PUT64 ((void (*) (bfd_uint64_t, void *)) swap_abort) 376#define NO_GETS64 ((bfd_int64_t (*) (const void *)) swap_abort) 377 378const bfd_target hpux_core_vec = 379 { 380 "hpux-core", 381 bfd_target_unknown_flavour, 382 BFD_ENDIAN_BIG, /* target byte order */ 383 BFD_ENDIAN_BIG, /* target headers byte order */ 384 (HAS_RELOC | EXEC_P | /* object flags */ 385 HAS_LINENO | HAS_DEBUG | 386 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED), 387 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */ 388 0, /* symbol prefix */ 389 ' ', /* ar_pad_char */ 390 16, /* ar_max_namelen */ 391 NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit data */ 392 NO_GET, NO_GETS, NO_PUT, /* 32 bit data */ 393 NO_GET, NO_GETS, NO_PUT, /* 16 bit data */ 394 NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit hdrs */ 395 NO_GET, NO_GETS, NO_PUT, /* 32 bit hdrs */ 396 NO_GET, NO_GETS, NO_PUT, /* 16 bit hdrs */ 397 398 { /* bfd_check_format */ 399 _bfd_dummy_target, /* unknown format */ 400 _bfd_dummy_target, /* object file */ 401 _bfd_dummy_target, /* archive */ 402 hpux_core_core_file_p /* a core file */ 403 }, 404 { /* bfd_set_format */ 405 bfd_false, bfd_false, 406 bfd_false, bfd_false 407 }, 408 { /* bfd_write_contents */ 409 bfd_false, bfd_false, 410 bfd_false, bfd_false 411 }, 412 413 BFD_JUMP_TABLE_GENERIC (_bfd_generic), 414 BFD_JUMP_TABLE_COPY (_bfd_generic), 415 BFD_JUMP_TABLE_CORE (hpux_core), 416 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive), 417 BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols), 418 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs), 419 BFD_JUMP_TABLE_WRITE (_bfd_generic), 420 BFD_JUMP_TABLE_LINK (_bfd_nolink), 421 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), 422 423 NULL, 424 425 (PTR) 0 /* backend_data */ 426 }; 427