]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/rs6000-core.c
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / bfd / rs6000-core.c
CommitLineData
252b5132 1/* IBM RS/6000 "XCOFF" back-end for BFD.
a2c58332 2 Copyright (C) 1990-2022 Free Software Foundation, Inc.
2ce18a16 3 Written by Metin G. Ozisik, Mimi Phuong-Thao Vo, and John Gilmore.
252b5132
RH
4 Archive support from Damon A. Permezel.
5 Contributed by IBM Corporation and Cygnus Support.
6
eb1e0e80 7 This file is part of BFD, the Binary File Descriptor library.
252b5132 8
eb1e0e80
NC
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
cd123cb7 11 the Free Software Foundation; either version 3 of the License, or
eb1e0e80 12 (at your option) any later version.
252b5132 13
eb1e0e80
NC
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
252b5132 18
eb1e0e80
NC
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
cd123cb7
NC
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
23
252b5132
RH
24
25/* This port currently only handles reading object files, except when
26 compiled on an RS/6000 host. -- no archive support, no core files.
27 In all cases, it does not support writing.
28
252b5132 29 This is in a separate file from coff-rs6000.c, because it includes
6a125b16 30 system include files that conflict with coff/rs6000.h. */
252b5132
RH
31
32/* Internalcoff.h and coffcode.h modify themselves based on this flag. */
33#define RS6000COFF_C 1
34
35/* The AIX 4.1 kernel is obviously compiled with -D_LONG_LONG, so
36 we have to define _LONG_LONG for older versions of gcc to get the
37 proper alignments in the user structure. */
38#if defined(_AIX41) && !defined(_LONG_LONG)
39#define _LONG_LONG
40#endif
41
252b5132 42#include "sysdep.h"
3db64b00 43#include "bfd.h"
252b5132
RH
44#include "libbfd.h"
45
46#ifdef AIX_CORE
47
48/* AOUTHDR is defined by the above. We need another defn of it, from the
49 system include files. Punt the old one and get us a new name for the
50 typedef in the system include files. */
51#ifdef AOUTHDR
52#undef AOUTHDR
53#endif
54#define AOUTHDR second_AOUTHDR
55
56#undef SCNHDR
57
d6867a75 58/* Support for core file stuff. */
252b5132
RH
59
60#include <sys/user.h>
59d9f049
ND
61#define __LDINFO_PTRACE32__ /* for __ld_info32 */
62#define __LDINFO_PTRACE64__ /* for __ld_info64 */
252b5132
RH
63#include <sys/ldr.h>
64#include <sys/core.h>
59d9f049
ND
65#include <sys/systemcfg.h>
66
cc429f57
JB
67/* Borrowed from <sys/inttypes.h> on recent AIX versions. */
68typedef unsigned long ptr_to_uint;
69
59d9f049
ND
70#define core_hdr(bfd) ((CoreHdr *) bfd->tdata.any)
71
72/* AIX 4.1 changed the names and locations of a few items in the core file.
73 AIX 4.3 defined an entirely new structure, core_dumpx, but kept support for
74 the previous 4.1 structure, core_dump.
75
76 AIX_CORE_DUMPX_CORE is defined (by configure) on AIX 4.3+, and
77 CORE_VERSION_1 is defined (by AIX core.h) as 2 on AIX 4.3+ and as 1 on AIX
78 4.1 and 4.2. AIX pre-4.1 (aka 3.x) either doesn't define CORE_VERSION_1
3d855632 79 or else defines it as 0. */
59d9f049
ND
80
81#if defined(CORE_VERSION_1) && !CORE_VERSION_1
82# undef CORE_VERSION_1
83#endif
84
85/* The following union and macros allow this module to compile on all AIX
86 versions and to handle both core_dumpx and core_dump on 4.3+. CNEW_*()
87 and COLD_*() macros respectively retrieve core_dumpx and core_dump
3d855632 88 values. */
59d9f049 89
3d855632 90/* Union of 32-bit and 64-bit versions of ld_info. */
59d9f049 91
d6867a75
SM
92typedef union
93{
94#if defined (__ld_info32) || defined (__ld_info64)
59d9f049
ND
95 struct __ld_info32 l32;
96 struct __ld_info64 l64;
97#else
98 struct ld_info l32;
99 struct ld_info l64;
100#endif
101} LdInfo;
102
3d855632 103/* Union of old and new core dump structures. */
59d9f049 104
d6867a75
SM
105typedef union
106{
59d9f049 107#ifdef AIX_CORE_DUMPX_CORE
d6867a75 108 struct core_dumpx new_dump; /* New AIX 4.3+ core dump. */
59d9f049 109#else
d6867a75
SM
110 struct core_dump new_dump; /* For simpler coding. */
111#endif
07d6d2b8 112#ifndef BFD64 /* Use old only if gdb is 32-bit. */
d6867a75
SM
113 struct core_dump old; /* Old AIX 4.2- core dump, still used on
114 4.3+ with appropriate SMIT config. */
59d9f049 115#endif
59d9f049
ND
116} CoreHdr;
117
3d855632 118/* Union of old and new vm_info structures. */
59d9f049 119
1605fc4f 120#ifdef CORE_VERSION_1
d6867a75
SM
121typedef union
122{
59d9f049 123#ifdef AIX_CORE_DUMPX_CORE
d3ce72d0 124 struct vm_infox new_dump;
59d9f049 125#else
d3ce72d0 126 struct vm_info new_dump;
59d9f049 127#endif
d6867a75 128#ifndef BFD64
6a125b16 129 struct vm_info old;
d6867a75 130#endif
59d9f049 131} VmInfo;
1605fc4f 132#endif
59d9f049 133
3d855632 134/* Return whether CoreHdr C is in new or old format. */
59d9f049
ND
135
136#ifdef AIX_CORE_DUMPX_CORE
d6867a75 137# ifndef BFD64
f03265d9 138# define CORE_NEW(c) (!(c).old.c_entries)
d6867a75 139# else
f03265d9 140# define CORE_NEW(c) 1
d6867a75 141# endif
59d9f049
ND
142#else
143# define CORE_NEW(c) 0
144#endif
145
e0c88096
NC
146/* Return whether CoreHdr C usese core_dumpxx structure.
147
148 FIXME: the core file format version number used here definitely indicates
149 that struct core_dumpxx should be used to represent the core file header,
150 but that may not be the only such format version number. */
151
152#ifdef AIX_5_CORE
07d6d2b8 153# define CORE_DUMPXX_VERSION 267312562
d3ce72d0 154# define CNEW_IS_CORE_DUMPXX(c) ((c).new_dump.c_version == CORE_DUMPXX_VERSION)
e0c88096
NC
155#else
156# define CNEW_IS_CORE_DUMPXX(c) 0
157#endif
158
3d855632 159/* Return the c_stackorg field from struct core_dumpx C. */
59d9f049
ND
160
161#ifdef AIX_CORE_DUMPX_CORE
162# define CNEW_STACKORG(c) (c).c_stackorg
163#else
164# define CNEW_STACKORG(c) 0
165#endif
166
3d855632 167/* Return the offset to the loader region from struct core_dump C. */
59d9f049
ND
168
169#ifdef AIX_CORE_DUMPX_CORE
170# define CNEW_LOADER(c) (c).c_loader
171#else
172# define CNEW_LOADER(c) 0
173#endif
174
3d855632 175/* Return the offset to the loader region from struct core_dump C. */
59d9f049
ND
176
177#define COLD_LOADER(c) (c).c_tab
178
3d855632 179/* Return the c_lsize field from struct core_dumpx C. */
59d9f049
ND
180
181#ifdef AIX_CORE_DUMPX_CORE
182# define CNEW_LSIZE(c) (c).c_lsize
183#else
184# define CNEW_LSIZE(c) 0
185#endif
186
3d855632 187/* Return the c_dataorg field from struct core_dumpx C. */
252b5132 188
59d9f049
ND
189#ifdef AIX_CORE_DUMPX_CORE
190# define CNEW_DATAORG(c) (c).c_dataorg
191#else
192# define CNEW_DATAORG(c) 0
193#endif
252b5132 194
3d855632 195/* Return the c_datasize field from struct core_dumpx C. */
252b5132 196
59d9f049
ND
197#ifdef AIX_CORE_DUMPX_CORE
198# define CNEW_DATASIZE(c) (c).c_datasize
199#else
200# define CNEW_DATASIZE(c) 0
201#endif
202
3d855632 203/* Return the c_impl field from struct core_dumpx C. */
59d9f049 204
83954261 205#if defined (HAVE_ST_C_IMPL) || defined (AIX_5_CORE)
59d9f049
ND
206# define CNEW_IMPL(c) (c).c_impl
207#else
208# define CNEW_IMPL(c) 0
209#endif
210
3d855632 211/* Return the command string from struct core_dumpx C. */
59d9f049
ND
212
213#ifdef AIX_CORE_DUMPX_CORE
214# define CNEW_COMM(c) (c).c_u.U_proc.pi_comm
215#else
216# define CNEW_COMM(c) 0
217#endif
218
3d855632 219/* Return the command string from struct core_dump C. */
59d9f049
ND
220
221#ifdef CORE_VERSION_1
222# define COLD_COMM(c) (c).c_u.U_comm
223#else
224# define COLD_COMM(c) (c).c_u.u_comm
225#endif
252b5132 226
3d855632 227/* Return the struct __context64 pointer from struct core_dumpx C. */
252b5132 228
59d9f049
ND
229#ifdef AIX_CORE_DUMPX_CORE
230# define CNEW_CONTEXT64(c) (c).c_flt.hctx.r64
231#else
232# define CNEW_CONTEXT64(c) c
233#endif
252b5132 234
3d855632 235/* Return the struct mstsave pointer from struct core_dumpx C. */
6ad49cca
KB
236
237#ifdef AIX_CORE_DUMPX_CORE
59d9f049 238# define CNEW_MSTSAVE(c) (c).c_flt.hctx.r32
6ad49cca 239#else
59d9f049
ND
240# define CNEW_MSTSAVE(c) c
241#endif
242
3d855632 243/* Return the struct mstsave pointer from struct core_dump C. */
59d9f049 244
252b5132 245#ifdef CORE_VERSION_1
59d9f049 246# define COLD_MSTSAVE(c) (c).c_mst
252b5132 247#else
59d9f049 248# define COLD_MSTSAVE(c) (c).c_u.u_save
6ad49cca 249#endif
59d9f049 250
3d855632 251/* Return whether struct core_dumpx is from a 64-bit process. */
59d9f049
ND
252
253#ifdef AIX_CORE_DUMPX_CORE
254# define CNEW_PROC64(c) IS_PROC64(&(c).c_u.U_proc)
255#else
256# define CNEW_PROC64(c) 0
257#endif
258
259/* Magic end-of-stack addresses for old core dumps. This is _very_ fragile,
3d855632 260 but I don't see any easy way to get that info right now. */
59d9f049
ND
261
262#ifdef CORE_VERSION_1
263# define COLD_STACKEND 0x2ff23000
264#else
265# define COLD_STACKEND 0x2ff80000
252b5132
RH
266#endif
267
59d9f049 268/* Size of the leading portion that old and new core dump structures have in
3d855632 269 common. */
d6867a75
SM
270#ifdef AIX_CORE_DUMPX_CORE
271#define CORE_COMMONSZ ((long) &((struct core_dumpx *) 0)->c_entries \
07d6d2b8 272 + sizeof (((struct core_dumpx *) 0)->c_entries))
d6867a75 273#else
ba1c4c6f
AM
274#define CORE_COMMONSZ ((int) &((struct core_dump *) 0)->c_entries \
275 + sizeof (((struct core_dump *) 0)->c_entries))
d6867a75 276#endif
cc429f57
JB
277/* Define prototypes for certain functions, to avoid a compiler warning
278 saying that they are missing. */
279
0b7733b6 280bfd_cleanup rs6000coff_core_p (bfd *abfd);
0a1b45a2 281bool rs6000coff_core_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd);
cc429f57
JB
282char * rs6000coff_core_file_failing_command (bfd *abfd);
283int rs6000coff_core_file_failing_signal (bfd *abfd);
284
3d855632
KH
285/* Try to read into CORE the header from the core file associated with ABFD.
286 Return success. */
59d9f049 287
0a1b45a2 288static bool
59d9f049
ND
289read_hdr (bfd *abfd, CoreHdr *core)
290{
291 bfd_size_type size;
292
dc810e39 293 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
0a1b45a2 294 return false;
59d9f049
ND
295
296 /* Read the leading portion that old and new core dump structures have in
3d855632 297 common. */
dc810e39
AM
298 size = CORE_COMMONSZ;
299 if (bfd_bread (core, size, abfd) != size)
0a1b45a2 300 return false;
59d9f049 301
3d855632 302 /* Read the trailing portion of the structure. */
dc810e39 303 if (CORE_NEW (*core))
342e3c85 304 size = sizeof (core->new_dump);
d6867a75 305#ifndef BFD64
dc810e39
AM
306 else
307 size = sizeof (core->old);
d6867a75 308#endif
dc810e39
AM
309 size -= CORE_COMMONSZ;
310 return bfd_bread ((char *) core + CORE_COMMONSZ, size, abfd) == size;
59d9f049 311}
252b5132
RH
312
313static asection *
cc429f57
JB
314make_bfd_asection (bfd *abfd, const char *name, flagword flags,
315 bfd_size_type size, bfd_vma vma, file_ptr filepos)
252b5132
RH
316{
317 asection *asect;
318
117ed4f8 319 asect = bfd_make_section_anyway_with_flags (abfd, name, flags);
252b5132
RH
320 if (!asect)
321 return NULL;
322
eea6121a 323 asect->size = size;
252b5132
RH
324 asect->vma = vma;
325 asect->filepos = filepos;
326 asect->alignment_power = 8;
327
328 return asect;
329}
330
331/* Decide if a given bfd represents a `core' file or not. There really is no
3d855632 332 magic number or anything like, in rs6000coff. */
252b5132 333
cb001c0d 334bfd_cleanup
cc429f57 335rs6000coff_core_p (bfd *abfd)
252b5132 336{
59d9f049 337 CoreHdr core;
252b5132 338 struct stat statbuf;
59d9f049 339 bfd_size_type size;
252b5132
RH
340 char *tmpptr;
341
3d855632 342 /* Values from new and old core structures. */
59d9f049
ND
343 int c_flag;
344 file_ptr c_stack, c_regoff, c_loader;
345 bfd_size_type c_size, c_regsize, c_lsize;
346 bfd_vma c_stackend;
347 void *c_regptr;
348 int proc64;
252b5132 349
59d9f049 350 if (!read_hdr (abfd, &core))
252b5132
RH
351 {
352 if (bfd_get_error () != bfd_error_system_call)
353 bfd_set_error (bfd_error_wrong_format);
354 return NULL;
355 }
356
e0c88096
NC
357 /* This isn't the right handler for 64-bit core files on AIX 5.x. */
358 if (CORE_NEW (core) && CNEW_IS_CORE_DUMPXX (core))
359 {
360 bfd_set_error (bfd_error_wrong_format);
361 return NULL;
362 }
363
3d855632 364 /* Copy fields from new or old core structure. */
59d9f049
ND
365 if (CORE_NEW (core))
366 {
d3ce72d0
NC
367 c_flag = core.new_dump.c_flag;
368 c_stack = (file_ptr) core.new_dump.c_stack;
369 c_size = core.new_dump.c_size;
370 c_stackend = CNEW_STACKORG (core.new_dump) + c_size;
371 c_lsize = CNEW_LSIZE (core.new_dump);
372 c_loader = CNEW_LOADER (core.new_dump);
d6867a75 373#ifndef BFD64
d3ce72d0 374 proc64 = CNEW_PROC64 (core.new_dump);
59d9f049
ND
375 }
376 else
377 {
378 c_flag = core.old.c_flag;
cc429f57 379 c_stack = (file_ptr) (ptr_to_uint) core.old.c_stack;
59d9f049
ND
380 c_size = core.old.c_size;
381 c_stackend = COLD_STACKEND;
382 c_lsize = 0x7ffffff;
cc429f57 383 c_loader = (file_ptr) (ptr_to_uint) COLD_LOADER (core.old);
d6867a75 384#endif
59d9f049
ND
385 proc64 = 0;
386 }
387
388 if (proc64)
389 {
d3ce72d0
NC
390 c_regsize = sizeof (CNEW_CONTEXT64 (core.new_dump));
391 c_regptr = &CNEW_CONTEXT64 (core.new_dump);
59d9f049
ND
392 }
393 else if (CORE_NEW (core))
394 {
d3ce72d0
NC
395 c_regsize = sizeof (CNEW_MSTSAVE (core.new_dump));
396 c_regptr = &CNEW_MSTSAVE (core.new_dump);
59d9f049 397 }
d6867a75 398#ifndef BFD64
59d9f049
ND
399 else
400 {
401 c_regsize = sizeof (COLD_MSTSAVE (core.old));
402 c_regptr = &COLD_MSTSAVE (core.old);
403 }
d6867a75 404#endif
6a125b16 405 c_regoff = (char *) c_regptr - (char *) &core;
59d9f049 406
252b5132
RH
407 if (bfd_stat (abfd, &statbuf) < 0)
408 {
409 bfd_set_error (bfd_error_system_call);
410 return NULL;
411 }
412
413 /* If the core file ulimit is too small, the system will first
414 omit the data segment, then omit the stack, then decline to
415 dump core altogether (as far as I know UBLOCK_VALID and LE_VALID
416 are always set) (this is based on experimentation on AIX 3.2).
417 Now, the thing is that GDB users will be surprised
418 if segments just silently don't appear (well, maybe they would
419 think to check "info files", I don't know).
420
421 For the data segment, we have no choice but to keep going if it's
422 not there, since the default behavior is not to dump it (regardless
6a125b16 423 of the ulimit, it's based on SA_FULLDUMP). But for the stack segment,
252b5132
RH
424 if it's not there, we refuse to have anything to do with this core
425 file. The usefulness of a core dump without a stack segment is pretty
426 limited anyway. */
59d9f049
ND
427
428 if (!(c_flag & UBLOCK_VALID)
429 || !(c_flag & LE_VALID))
252b5132
RH
430 {
431 bfd_set_error (bfd_error_wrong_format);
432 return NULL;
433 }
434
59d9f049 435 if (!(c_flag & USTACK_VALID))
252b5132
RH
436 {
437 bfd_set_error (bfd_error_file_truncated);
438 return NULL;
439 }
440
441 /* Don't check the core file size for a full core, AIX 4.1 includes
6a125b16 442 additional shared library sections in a full core. */
59d9f049 443 if (!(c_flag & (FULL_CORE | CORE_TRUNC)))
252b5132 444 {
3d855632 445 /* If the size is wrong, it means we're misinterpreting something. */
59d9f049
ND
446 if (c_stack + (file_ptr) c_size != statbuf.st_size)
447 {
448 bfd_set_error (bfd_error_wrong_format);
449 return NULL;
450 }
252b5132
RH
451 }
452
453 /* Sanity check on the c_tab field. */
d6867a75
SM
454 if (!CORE_NEW (core)
455 && (
456#ifndef BFD64
457 c_loader < (file_ptr) sizeof core.old
458#else
459 c_loader < (file_ptr) sizeof core.new_dump
460#endif
461 || c_loader >= statbuf.st_size
462 || c_loader >= c_stack))
252b5132
RH
463 {
464 bfd_set_error (bfd_error_wrong_format);
465 return NULL;
466 }
467
468 /* Issue warning if the core file was truncated during writing. */
59d9f049 469 if (c_flag & CORE_TRUNC)
871b3ab2 470 _bfd_error_handler (_("%pB: warning core file truncated"), abfd);
252b5132 471
6a125b16 472 /* Allocate core file header. */
d6867a75 473#ifndef BFD64
d3ce72d0 474 size = CORE_NEW (core) ? sizeof (core.new_dump) : sizeof (core.old);
d6867a75
SM
475#else
476 size = sizeof (core.new_dump);
477#endif
dc810e39 478 tmpptr = (char *) bfd_zalloc (abfd, (bfd_size_type) size);
252b5132
RH
479 if (!tmpptr)
480 return NULL;
252b5132
RH
481
482 /* Copy core file header. */
59d9f049
ND
483 memcpy (tmpptr, &core, size);
484 set_tdata (abfd, tmpptr);
485
3d855632 486 /* Set architecture. */
59d9f049
ND
487 if (CORE_NEW (core))
488 {
489 enum bfd_architecture arch;
490 unsigned long mach;
491
d3ce72d0 492 switch (CNEW_IMPL (core.new_dump))
59d9f049
ND
493 {
494 case POWER_RS1:
495 case POWER_RSC:
496 case POWER_RS2:
497 arch = bfd_arch_rs6000;
498 mach = bfd_mach_rs6k;
499 break;
500 default:
501 arch = bfd_arch_powerpc;
502 mach = bfd_mach_ppc;
503 break;
6a125b16 504 }
59d9f049
ND
505 bfd_default_set_arch_mach (abfd, arch, mach);
506 }
252b5132 507
3d855632 508 /* .stack section. */
252b5132 509 if (!make_bfd_asection (abfd, ".stack",
59d9f049
ND
510 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
511 c_size, c_stackend - c_size, c_stack))
9e7b37b3 512 goto fail;
252b5132 513
3d855632 514 /* .reg section for all registers. */
252b5132 515 if (!make_bfd_asection (abfd, ".reg",
59d9f049
ND
516 SEC_HAS_CONTENTS,
517 c_regsize, (bfd_vma) 0, c_regoff))
9e7b37b3 518 goto fail;
252b5132
RH
519
520 /* .ldinfo section.
521 To actually find out how long this section is in this particular
522 core dump would require going down the whole list of struct ld_info's.
523 See if we can just fake it. */
524 if (!make_bfd_asection (abfd, ".ldinfo",
59d9f049
ND
525 SEC_HAS_CONTENTS,
526 c_lsize, (bfd_vma) 0, c_loader))
9e7b37b3 527 goto fail;
252b5132
RH
528
529#ifndef CORE_VERSION_1
530 /* .data section if present.
531 AIX 3 dumps the complete data section and sets FULL_CORE if the
532 ulimit is large enough, otherwise the data section is omitted.
533 AIX 4 sets FULL_CORE even if the core file is truncated, we have
59d9f049 534 to examine core.c_datasize below to find out the actual size of
6a125b16 535 the .data section. */
59d9f049 536 if (c_flag & FULL_CORE)
252b5132
RH
537 {
538 if (!make_bfd_asection (abfd, ".data",
539 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
59d9f049 540 (bfd_size_type) core.old.c_u.u_dsize,
252b5132 541 (bfd_vma)
59d9f049
ND
542 CDATA_ADDR (core.old.c_u.u_dsize),
543 c_stack + c_size))
9e7b37b3 544 goto fail;
252b5132
RH
545 }
546#endif
547
548#ifdef CORE_VERSION_1
549 /* AIX 4 adds data sections from loaded objects to the core file,
550 which can be found by examining ldinfo, and anonymously mmapped
551 regions. */
552 {
59d9f049
ND
553 LdInfo ldinfo;
554 bfd_size_type ldi_datasize;
555 file_ptr ldi_core;
556 uint ldi_next;
557 bfd_vma ldi_dataorg;
d6867a75 558 bfd_vma core_dataorg;
59d9f049 559
3d855632 560 /* Fields from new and old core structures. */
59d9f049
ND
561 bfd_size_type c_datasize, c_vmregions;
562 file_ptr c_data, c_vmm;
563
564 if (CORE_NEW (core))
565 {
d3ce72d0
NC
566 c_datasize = CNEW_DATASIZE (core.new_dump);
567 c_data = (file_ptr) core.new_dump.c_data;
568 c_vmregions = core.new_dump.c_vmregions;
569 c_vmm = (file_ptr) core.new_dump.c_vmm;
59d9f049 570 }
d6867a75 571#ifndef BFD64
59d9f049
ND
572 else
573 {
574 c_datasize = core.old.c_datasize;
cc429f57 575 c_data = (file_ptr) (ptr_to_uint) core.old.c_data;
59d9f049 576 c_vmregions = core.old.c_vmregions;
cc429f57 577 c_vmm = (file_ptr) (ptr_to_uint) core.old.c_vmm;
59d9f049 578 }
d6867a75 579#endif
252b5132
RH
580
581 /* .data section from executable. */
59d9f049 582 if (c_datasize)
252b5132 583 {
d6867a75
SM
584 /* If Large Memory Model is used, then the .data segment should start from
585 BDATAORG which has been defined in the system header files. */
586
07d6d2b8
AM
587 if (c_flag & CORE_BIGDATA)
588 core_dataorg = BDATAORG;
589 else
590 core_dataorg = CDATA_ADDR (c_datasize);
d6867a75 591
252b5132
RH
592 if (!make_bfd_asection (abfd, ".data",
593 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
59d9f049 594 c_datasize,
d6867a75 595 (bfd_vma) core_dataorg,
59d9f049 596 c_data))
9e7b37b3 597 goto fail;
252b5132
RH
598 }
599
600 /* .data sections from loaded objects. */
59d9f049 601 if (proc64)
d6867a75 602 size = (unsigned long) ((LdInfo *) 0)->l64.ldinfo_filename;
59d9f049 603 else
d6867a75 604 size = (unsigned long) ((LdInfo *) 0)->l32.ldinfo_filename;
59d9f049 605
252b5132
RH
606 while (1)
607 {
59d9f049 608 if (bfd_seek (abfd, c_loader, SEEK_SET) != 0)
9e7b37b3 609 goto fail;
dc810e39 610 if (bfd_bread (&ldinfo, size, abfd) != size)
9e7b37b3 611 goto fail;
59d9f049
ND
612
613 if (proc64)
252b5132 614 {
59d9f049
ND
615 ldi_core = ldinfo.l64.ldinfo_core;
616 ldi_datasize = ldinfo.l64.ldinfo_datasize;
617 ldi_dataorg = (bfd_vma) ldinfo.l64.ldinfo_dataorg;
618 ldi_next = ldinfo.l64.ldinfo_next;
619 }
620 else
621 {
622 ldi_core = ldinfo.l32.ldinfo_core;
623 ldi_datasize = ldinfo.l32.ldinfo_datasize;
c38e8559 624 ldi_dataorg = (bfd_vma) (ptr_to_uint) ldinfo.l32.ldinfo_dataorg;
59d9f049 625 ldi_next = ldinfo.l32.ldinfo_next;
252b5132 626 }
59d9f049
ND
627
628 if (ldi_core)
629 if (!make_bfd_asection (abfd, ".data",
630 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
631 ldi_datasize, ldi_dataorg, ldi_core))
9e7b37b3 632 goto fail;
59d9f049
ND
633
634 if (ldi_next == 0)
252b5132 635 break;
59d9f049 636 c_loader += ldi_next;
252b5132
RH
637 }
638
639 /* .vmdata sections from anonymously mmapped regions. */
59d9f049 640 if (c_vmregions)
252b5132 641 {
59d9f049 642 bfd_size_type i;
252b5132 643
59d9f049 644 if (bfd_seek (abfd, c_vmm, SEEK_SET) != 0)
9e7b37b3 645 goto fail;
252b5132 646
59d9f049 647 for (i = 0; i < c_vmregions; i++)
252b5132 648 {
59d9f049
ND
649 VmInfo vminfo;
650 bfd_size_type vminfo_size;
651 file_ptr vminfo_offset;
652 bfd_vma vminfo_addr;
252b5132 653
d6867a75 654#ifndef BFD64
d3ce72d0 655 size = CORE_NEW (core) ? sizeof (vminfo.new_dump) : sizeof (vminfo.old);
d6867a75 656#else
07d6d2b8 657 size = sizeof (vminfo.new_dump);
d6867a75 658#endif
dc810e39 659 if (bfd_bread (&vminfo, size, abfd) != size)
9e7b37b3 660 goto fail;
59d9f049
ND
661
662 if (CORE_NEW (core))
252b5132 663 {
d3ce72d0
NC
664 vminfo_addr = (bfd_vma) vminfo.new_dump.vminfo_addr;
665 vminfo_size = vminfo.new_dump.vminfo_size;
666 vminfo_offset = vminfo.new_dump.vminfo_offset;
252b5132 667 }
d6867a75 668#ifndef BFD64
59d9f049
ND
669 else
670 {
c38e8559 671 vminfo_addr = (bfd_vma) (ptr_to_uint) vminfo.old.vminfo_addr;
59d9f049
ND
672 vminfo_size = vminfo.old.vminfo_size;
673 vminfo_offset = vminfo.old.vminfo_offset;
674 }
d6867a75 675#endif
59d9f049
ND
676
677 if (vminfo_offset)
678 if (!make_bfd_asection (abfd, ".vmdata",
679 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
680 vminfo_size, vminfo_addr,
681 vminfo_offset))
9e7b37b3 682 goto fail;
252b5132
RH
683 }
684 }
685 }
686#endif
687
cb001c0d 688 return _bfd_no_cleanup;
9e7b37b3
AM
689
690 fail:
691 bfd_release (abfd, abfd->tdata.any);
692 abfd->tdata.any = NULL;
693 bfd_section_list_clear (abfd);
694 return NULL;
252b5132
RH
695}
696
b34976b6 697/* Return `TRUE' if given core is from the given executable. */
6a125b16 698
0a1b45a2 699bool
cc429f57 700rs6000coff_core_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd)
252b5132 701{
59d9f049 702 CoreHdr core;
252b5132
RH
703 bfd_size_type size;
704 char *path, *s;
705 size_t alloc;
706 const char *str1, *str2;
0a1b45a2 707 bool ret;
59d9f049 708 file_ptr c_loader;
252b5132 709
59d9f049 710 if (!read_hdr (core_bfd, &core))
0a1b45a2 711 return false;
252b5132 712
59d9f049 713 if (CORE_NEW (core))
d3ce72d0 714 c_loader = CNEW_LOADER (core.new_dump);
d6867a75 715#ifndef BFD64
59d9f049 716 else
cc429f57 717 c_loader = (file_ptr) (ptr_to_uint) COLD_LOADER (core.old);
d6867a75 718#endif
252b5132 719
d3ce72d0 720 if (CORE_NEW (core) && CNEW_PROC64 (core.new_dump))
6a125b16 721 size = (int) ((LdInfo *) 0)->l64.ldinfo_filename;
59d9f049 722 else
6a125b16 723 size = (int) ((LdInfo *) 0)->l32.ldinfo_filename;
59d9f049
ND
724
725 if (bfd_seek (core_bfd, c_loader + size, SEEK_SET) != 0)
0a1b45a2 726 return false;
252b5132
RH
727
728 alloc = 100;
dc810e39 729 path = bfd_malloc ((bfd_size_type) alloc);
252b5132 730 if (path == NULL)
0a1b45a2 731 return false;
252b5132
RH
732 s = path;
733
734 while (1)
735 {
dc810e39 736 if (bfd_bread (s, (bfd_size_type) 1, core_bfd) != 1)
252b5132
RH
737 {
738 free (path);
0a1b45a2 739 return false;
252b5132
RH
740 }
741 if (*s == '\0')
742 break;
743 ++s;
744 if (s == path + alloc)
745 {
746 char *n;
747
748 alloc *= 2;
dc810e39 749 n = bfd_realloc (path, (bfd_size_type) alloc);
252b5132
RH
750 if (n == NULL)
751 {
752 free (path);
0a1b45a2 753 return false;
252b5132
RH
754 }
755 s = n + (path - s);
756 path = n;
757 }
758 }
59d9f049 759
252b5132 760 str1 = strrchr (path, '/');
765cf5f6 761 str2 = strrchr (bfd_get_filename (exec_bfd), '/');
252b5132
RH
762
763 /* step over character '/' */
764 str1 = str1 != NULL ? str1 + 1 : path;
765cf5f6 765 str2 = str2 != NULL ? str2 + 1 : bfd_get_filename (exec_bfd);
252b5132
RH
766
767 if (strcmp (str1, str2) == 0)
0a1b45a2 768 ret = true;
252b5132 769 else
0a1b45a2 770 ret = false;
252b5132
RH
771
772 free (path);
773
774 return ret;
775}
776
777char *
cc429f57 778rs6000coff_core_file_failing_command (bfd *abfd)
252b5132 779{
59d9f049 780 CoreHdr *core = core_hdr (abfd);
d6867a75 781#ifndef BFD64
59d9f049 782 char *com = CORE_NEW (*core) ?
d3ce72d0 783 CNEW_COMM (core->new_dump) : COLD_COMM (core->old);
d6867a75
SM
784#else
785 char *com = CNEW_COMM (core->new_dump);
786#endif
59d9f049 787
252b5132
RH
788 if (*com)
789 return com;
790 else
791 return 0;
792}
793
794int
cc429f57 795rs6000coff_core_file_failing_signal (bfd *abfd)
252b5132 796{
59d9f049 797 CoreHdr *core = core_hdr (abfd);
d6867a75 798#ifndef BFD64
d3ce72d0 799 return CORE_NEW (*core) ? core->new_dump.c_signo : core->old.c_signo;
d6867a75
SM
800#else
801 return core->new_dump.c_signo;
802#endif
252b5132
RH
803}
804
805#endif /* AIX_CORE */