Coverage Report

Created: 2025-06-24 06:45

/src/binutils-gdb/bfd/aix5ppc-core.c
Line
Count
Source (jump to first uncovered line)
1
/* IBM RS/6000 "XCOFF" back-end for BFD.
2
   Copyright (C) 2001-2025 Free Software Foundation, Inc.
3
   Written by Tom Rix
4
   Contributed by Red Hat Inc.
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
#include "sysdep.h"
24
#include "bfd.h"
25
26
bfd_cleanup xcoff64_core_p (bfd *);
27
bool xcoff64_core_file_matches_executable_p (bfd *, bfd *);
28
char *xcoff64_core_file_failing_command (bfd *);
29
int xcoff64_core_file_failing_signal (bfd *);
30
31
#ifdef AIX_5_CORE
32
33
#include "libbfd.h"
34
35
/* Aix 5.1 system include file.  */
36
37
/* Need to define this macro so struct ld_info64 get included.  */
38
#define __LDINFO_PTRACE64__
39
#include <sys/ldr.h>
40
#include <core.h>
41
42
/* The default architecture and machine for matching core files.  */
43
#define DEFAULT_ARCHITECTURE  bfd_arch_powerpc
44
#define DEFAULT_MACHINE   bfd_mach_ppc_620
45
46
#define core_hdr(abfd)    ((struct core_dumpxx *) abfd->tdata.any)
47
48
#define CHECK_FILE_OFFSET(s, v) \
49
  ((bfd_signed_vma)(v) < 0 || (bfd_signed_vma)(v) > (bfd_signed_vma)(s).st_size)
50
51
bfd_cleanup
52
xcoff64_core_p (bfd *abfd)
53
{
54
  enum bfd_architecture arch;
55
  unsigned long mach;
56
  struct core_dumpxx core, *new_core_hdr;
57
  struct stat statbuf;
58
  asection *sec;
59
  struct __ld_info64 ldinfo;
60
  bfd_vma ld_offset;
61
  bfd_size_type i;
62
  struct vm_infox vminfo;
63
  flagword flags;
64
65
  /* Get the header.  */
66
  if (bfd_seek (abfd, 0, SEEK_SET) != 0)
67
    goto xcoff64_core_p_error;
68
69
  if (sizeof core != bfd_read (&core, sizeof core, abfd))
70
    goto xcoff64_core_p_error;
71
72
  if (bfd_stat (abfd, &statbuf) < 0)
73
    goto xcoff64_core_p_error;
74
75
  /* Sanity checks
76
     c_flag has CORE_VERSION_1, Aix 4+
77
     c_entries = 0 for Aix 4.3+
78
     IS_PROC64 is a macro defined in procinfo.h, test for 64 bit process.
79
80
     We will still be confused if a Aix 4.3 64 bit core file is
81
     copied over to a Aix 5 machine.
82
83
     Check file header offsets
84
85
     See rs6000-core.c for comment on size of core
86
     If there isn't enough of a real core file, bail.  */
87
88
  if ((CORE_VERSION_1 != (core.c_flag & CORE_VERSION_1))
89
      || (0 != core.c_entries)
90
      || (! (IS_PROC64 (&core.c_u.U_proc)))
91
      || ((CHECK_FILE_OFFSET (statbuf, core.c_fdsinfox)))
92
      || ((CHECK_FILE_OFFSET (statbuf, core.c_loader)))
93
      || ((CHECK_FILE_OFFSET (statbuf, core.c_loader + core.c_lsize)))
94
      || ((CHECK_FILE_OFFSET (statbuf, core.c_thr)))
95
      || ((CHECK_FILE_OFFSET (statbuf, core.c_segregion)))
96
      || ((CHECK_FILE_OFFSET (statbuf, core.c_stack)))
97
      || ((CHECK_FILE_OFFSET (statbuf, core.c_stack + core.c_size)))
98
      || ((CHECK_FILE_OFFSET (statbuf, core.c_data)))
99
      || ((CHECK_FILE_OFFSET (statbuf, core.c_data + core.c_datasize)))
100
      || (! (core.c_flag & UBLOCK_VALID))
101
      || (! (core.c_flag & LE_VALID)))
102
    goto xcoff64_core_p_error;
103
104
  /* Check for truncated stack or general truncating.  */
105
  if ((! (core.c_flag & USTACK_VALID))
106
      || (core.c_flag & CORE_TRUNC))
107
    {
108
      bfd_set_error (bfd_error_file_truncated);
109
110
      return NULL;
111
    }
112
113
  new_core_hdr = bfd_alloc (abfd, sizeof (*new_core_hdr) + 1);
114
  if (NULL == new_core_hdr)
115
    return NULL;
116
117
  memcpy (new_core_hdr, &core, sizeof (*new_core_hdr));
118
119
  /* Ensure core_file_failing_command string is terminated.  This is
120
     just to stop buffer overflows on fuzzed files.  */
121
  ((char *) new_core_hdr)[sizeof (*new_core_hdr)] = 0;
122
123
  abfd->tdata.any = new_core_hdr;
124
125
  /* .stack section.  */
126
  flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
127
  sec = bfd_make_section_anyway_with_flags (abfd, ".stack", flags);
128
  if (NULL == sec)
129
    return NULL;
130
131
  sec->size = core.c_size;
132
  sec->vma = core.c_stackorg;
133
  sec->filepos = core.c_stack;
134
135
  /* .reg section for all registers.  */
136
  flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
137
  sec = bfd_make_section_anyway_with_flags (abfd, ".reg", flags);
138
  if (NULL == sec)
139
    return NULL;
140
141
  sec->size = sizeof (struct __context64);
142
  sec->vma = 0;
143
  sec->filepos = 0;
144
  sec->contents = (bfd_byte *)&new_core_hdr->c_flt.r64;
145
146
  if (core.c_extctx)
147
    {
148
      /* vmx section.  */
149
      flags = SEC_HAS_CONTENTS;
150
      sec = bfd_make_section_anyway_with_flags (abfd, ".aix-vmx", flags);
151
      if (sec == NULL)
152
  return NULL;
153
      sec->size = 560;
154
      sec->vma = 0;
155
      sec->filepos = core.c_extctx;
156
157
      /* vmx section.  */
158
      flags = SEC_HAS_CONTENTS;
159
      sec = bfd_make_section_anyway_with_flags (abfd, ".aix-vsx", flags);
160
      if (sec == NULL)
161
  return NULL;
162
      sec->size = 256;
163
      sec->vma = 0;
164
      sec->filepos = core.c_extctx + 584;
165
    }
166
167
  /* .ldinfo section.
168
     To actually find out how long this section is in this particular
169
     core dump would require going down the whole list of struct
170
     ld_info's.   See if we can just fake it.  */
171
  flags = SEC_HAS_CONTENTS;
172
  sec = bfd_make_section_anyway_with_flags (abfd, ".ldinfo", flags);
173
  if (NULL == sec)
174
    return NULL;
175
176
  sec->size = core.c_lsize;
177
  sec->vma = 0;
178
  sec->filepos = core.c_loader;
179
180
  /* AIX 4 adds data sections from loaded objects to the core file,
181
     which can be found by examining ldinfo, and anonymously mmapped
182
     regions.  */
183
184
  /* .data section from executable.  */
185
  flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
186
  sec = bfd_make_section_anyway_with_flags (abfd, ".data", flags);
187
  if (NULL == sec)
188
    return NULL;
189
190
  sec->size = core.c_datasize;
191
  sec->vma = core.c_dataorg;
192
  sec->filepos = core.c_data;
193
194
  /* .data sections from loaded objects.  */
195
  ld_offset = core.c_loader;
196
197
  while (1)
198
    {
199
      if (bfd_seek (abfd, ld_offset, SEEK_SET) != 0)
200
  return NULL;
201
202
      if (sizeof (struct __ld_info64) !=
203
    bfd_read (&ldinfo, sizeof (struct __ld_info64), abfd))
204
  return NULL;
205
206
      if (ldinfo.ldinfo_core)
207
  {
208
    flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
209
    sec = bfd_make_section_anyway_with_flags (abfd, ".data", flags);
210
    if (NULL == sec)
211
      return NULL;
212
213
    sec->size = ldinfo.ldinfo_datasize;
214
    sec->vma = ldinfo.ldinfo_dataorg;
215
    sec->filepos = ldinfo.ldinfo_core;
216
  }
217
218
      if (0 == ldinfo.ldinfo_next)
219
  break;
220
      ld_offset += ldinfo.ldinfo_next;
221
    }
222
223
  /* .vmdata sections from anonymously mmapped regions.  */
224
  if (core.c_vmregions)
225
    {
226
      if (bfd_seek (abfd, core.c_vmm, SEEK_SET) != 0)
227
  return NULL;
228
229
      for (i = 0; i < core.c_vmregions; i++)
230
  if (sizeof (struct vm_infox) !=
231
      bfd_read (&vminfo, sizeof (struct vm_infox), abfd))
232
    return NULL;
233
234
      if (vminfo.vminfo_offset)
235
  {
236
    flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
237
    sec = bfd_make_section_anyway_with_flags (abfd, ".vmdata", flags);
238
    if (NULL == sec)
239
      return NULL;
240
241
    sec->size = vminfo.vminfo_size;
242
    sec->vma = vminfo.vminfo_addr;
243
    sec->filepos = vminfo.vminfo_offset;
244
  }
245
    }
246
247
  /* Set the architecture and machine.  */
248
  arch = DEFAULT_ARCHITECTURE;
249
  mach = DEFAULT_MACHINE;
250
  bfd_default_set_arch_mach (abfd, arch, mach);
251
252
  return _bfd_no_cleanup;
253
254
 xcoff64_core_p_error:
255
  if (bfd_get_error () != bfd_error_system_call)
256
    bfd_set_error (bfd_error_wrong_format);
257
258
  return NULL;
259
}
260
261
/* Return `TRUE' if given core is from the given executable.  */
262
263
bool
264
xcoff64_core_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd)
265
{
266
  struct core_dumpxx core;
267
  char *path, *s;
268
  size_t alloc;
269
  const char *str1, *str2;
270
  bool return_value = false;
271
272
  /* Get the header.  */
273
  if (bfd_seek (core_bfd, 0, SEEK_SET) != 0)
274
    return return_value;
275
276
  if (sizeof (struct core_dumpxx) !=
277
      bfd_read (&core, sizeof (struct core_dumpxx), core_bfd))
278
    return return_value;
279
280
  if (bfd_seek (core_bfd, core.c_loader, SEEK_SET) != 0)
281
    return return_value;
282
283
  alloc = 100;
284
  path = bfd_malloc (alloc);
285
  if (path == NULL)
286
    return return_value;
287
288
  s = path;
289
290
  while (1)
291
    {
292
      if (bfd_read (s, 1, core_bfd) != 1)
293
  goto xcoff64_core_file_matches_executable_p_end_1;
294
295
      if (*s == '\0')
296
  break;
297
      ++s;
298
      if (s == path + alloc)
299
  {
300
    char *n;
301
302
    alloc *= 2;
303
    n = bfd_realloc (path, alloc);
304
    if (n == NULL)
305
      goto xcoff64_core_file_matches_executable_p_end_1;
306
307
    s = n + (path - s);
308
    path = n;
309
  }
310
    }
311
312
  str1 = strrchr (path, '/');
313
  str2 = strrchr (bfd_get_filename (exec_bfd), '/');
314
315
  /* Step over character '/'.  */
316
  str1 = str1 != NULL ? str1 + 1 : path;
317
  str2 = str2 != NULL ? str2 + 1 : bfd_get_filename (exec_bfd);
318
319
  if (strcmp (str1, str2) == 0)
320
    return_value = true;
321
322
 xcoff64_core_file_matches_executable_p_end_1:
323
  free (path);
324
  return return_value;
325
}
326
327
char *
328
xcoff64_core_file_failing_command (bfd *abfd)
329
{
330
  struct core_dumpxx *c = core_hdr (abfd);
331
  char *return_value = 0;
332
333
  if (NULL != c)
334
    return_value = c->c_u.U_proc.pi_comm;
335
336
  return return_value;
337
}
338
339
int
340
xcoff64_core_file_failing_signal (bfd *abfd)
341
{
342
  struct core_dumpxx *c = core_hdr (abfd);
343
  int return_value = 0;
344
345
  if (NULL != c)
346
    return_value = c->c_signo;
347
348
  return return_value;
349
}
350
351
#else /* AIX_5_CORE */
352
353
bfd_cleanup
354
xcoff64_core_p (bfd *abfd ATTRIBUTE_UNUSED)
355
16.6k
{
356
16.6k
  bfd_set_error (bfd_error_wrong_format);
357
16.6k
  return 0;
358
16.6k
}
359
360
bool
361
xcoff64_core_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd)
362
0
{
363
0
  return generic_core_file_matches_executable_p (core_bfd, exec_bfd);
364
0
}
365
366
char *
367
xcoff64_core_file_failing_command (bfd *abfd ATTRIBUTE_UNUSED)
368
0
{
369
0
  return 0;
370
0
}
371
372
int
373
xcoff64_core_file_failing_signal (bfd *abfd ATTRIBUTE_UNUSED)
374
0
{
375
0
  return 0;
376
0
}
377
378
#endif /* AIX_5_CORE */