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