xref: /netbsd-src/external/gpl3/binutils/dist/bfd/rs6000-core.c (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
1 /* IBM RS/6000 "XCOFF" back-end for BFD.
2    Copyright (C) 1990-2024 Free Software Foundation, Inc.
3    Written by Metin G. Ozisik, Mimi Phuong-Thao Vo, and John Gilmore.
4    Archive support from Damon A. Permezel.
5    Contributed by IBM Corporation and Cygnus Support.
6 
7    This file is part of BFD, the Binary File Descriptor library.
8 
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
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13 
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.
18 
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
21    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22    MA 02110-1301, USA.  */
23 
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 
29    This is in a separate file from coff-rs6000.c, because it includes
30    system include files that conflict with coff/rs6000.h.  */
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 
42 #include "sysdep.h"
43 #include "bfd.h"
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 
58 /* Support for core file stuff.  */
59 
60 #include <sys/user.h>
61 #define __LDINFO_PTRACE32__	/* for __ld_info32 */
62 #define __LDINFO_PTRACE64__	/* for __ld_info64 */
63 #include <sys/ldr.h>
64 #include <sys/core.h>
65 #include <sys/systemcfg.h>
66 
67 /* Borrowed from <sys/inttypes.h> on recent AIX versions.  */
68 typedef unsigned long ptr_to_uint;
69 
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
79    or else defines it as 0.  */
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
88    values.  */
89 
90 /* Union of 32-bit and 64-bit versions of ld_info.  */
91 
92 typedef union
93 {
94 #if defined (__ld_info32) || defined (__ld_info64)
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 
103 /* Union of old and new core dump structures.  */
104 
105 typedef union
106 {
107 #ifdef AIX_CORE_DUMPX_CORE
108   struct core_dumpx new_dump;	/* New AIX 4.3+ core dump.  */
109 #else
110   struct core_dump new_dump;	/* For simpler coding.  */
111 #endif
112 #ifndef BFD64			/* Use old only if gdb is 32-bit.  */
113   struct core_dump old;		/* Old AIX 4.2- core dump, still used on
114 				   4.3+ with appropriate SMIT config.  */
115 #endif
116 } CoreHdr;
117 
118 /* Union of old and new vm_info structures.  */
119 
120 #ifdef CORE_VERSION_1
121 typedef union
122 {
123 #ifdef AIX_CORE_DUMPX_CORE
124   struct vm_infox new_dump;
125 #else
126   struct vm_info new_dump;
127 #endif
128 #ifndef BFD64
129   struct vm_info old;
130 #endif
131 } VmInfo;
132 #endif
133 
134 /* Return whether CoreHdr C is in new or old format.  */
135 
136 #ifdef AIX_CORE_DUMPX_CORE
137 # ifndef BFD64
138 #  define CORE_NEW(c)	(!(c).old.c_entries)
139 # else
140 #  define CORE_NEW(c)	1
141 # endif
142 #else
143 # define CORE_NEW(c)	0
144 #endif
145 
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
153 # define CORE_DUMPXX_VERSION	267312562
154 # define CNEW_IS_CORE_DUMPXX(c) ((c).new_dump.c_version == CORE_DUMPXX_VERSION)
155 #else
156 # define CNEW_IS_CORE_DUMPXX(c) 0
157 #endif
158 
159 /* Return the c_stackorg field from struct core_dumpx C.  */
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 
167 /* Return the offset to the loader region from struct core_dump C.  */
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 
175 /* Return the offset to the loader region from struct core_dump C.  */
176 
177 #define COLD_LOADER(c)	(c).c_tab
178 
179 /* Return the c_lsize field from struct core_dumpx C.  */
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 
187 /* Return the c_dataorg field from struct core_dumpx C.  */
188 
189 #ifdef AIX_CORE_DUMPX_CORE
190 # define CNEW_DATAORG(c)	(c).c_dataorg
191 #else
192 # define CNEW_DATAORG(c)	0
193 #endif
194 
195 /* Return the c_datasize field from struct core_dumpx C.  */
196 
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 
203 /* Return the c_impl field from struct core_dumpx C.  */
204 
205 #if defined (HAVE_ST_C_IMPL) || defined (AIX_5_CORE)
206 # define CNEW_IMPL(c)	(c).c_impl
207 #else
208 # define CNEW_IMPL(c)	0
209 #endif
210 
211 /* Return the command string from struct core_dumpx C.  */
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 
219 /* Return the command string from struct core_dump C.  */
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
226 
227 /* Return the struct __context64 pointer from struct core_dumpx C.  */
228 
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
234 
235 /* Return the struct mstsave pointer from struct core_dumpx C.  */
236 
237 #ifdef AIX_CORE_DUMPX_CORE
238 # define CNEW_MSTSAVE(c)	(c).c_flt.hctx.r32
239 #else
240 # define CNEW_MSTSAVE(c)	c
241 #endif
242 
243 /* Return the struct mstsave pointer from struct core_dump C.  */
244 
245 #ifdef CORE_VERSION_1
246 # define COLD_MSTSAVE(c)	(c).c_mst
247 #else
248 # define COLD_MSTSAVE(c)	(c).c_u.u_save
249 #endif
250 
251 /* Return whether struct core_dumpx is from a 64-bit process.  */
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,
260    but I don't see any easy way to get that info right now.  */
261 
262 #ifdef CORE_VERSION_1
263 # define COLD_STACKEND	0x2ff23000
264 #else
265 # define COLD_STACKEND	0x2ff80000
266 #endif
267 
268 /* Size of the leading portion that old and new core dump structures have in
269    common.  */
270 #ifdef AIX_CORE_DUMPX_CORE
271 #define CORE_COMMONSZ  ((long) &((struct core_dumpx *) 0)->c_entries \
272 			+ sizeof (((struct core_dumpx *) 0)->c_entries))
273 #else
274 #define CORE_COMMONSZ  ((int) &((struct core_dump *) 0)->c_entries \
275 			+ sizeof (((struct core_dump *) 0)->c_entries))
276 #endif
277 /* Define prototypes for certain functions, to avoid a compiler warning
278    saying that they are missing.  */
279 
280 bfd_cleanup rs6000coff_core_p (bfd *abfd);
281 bool rs6000coff_core_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd);
282 char * rs6000coff_core_file_failing_command (bfd *abfd);
283 int rs6000coff_core_file_failing_signal (bfd *abfd);
284 
285 /* Try to read into CORE the header from the core file associated with ABFD.
286    Return success.  */
287 
288 static bool
read_hdr(bfd * abfd,CoreHdr * core)289 read_hdr (bfd *abfd, CoreHdr *core)
290 {
291   bfd_size_type size;
292 
293   if (bfd_seek (abfd, 0, SEEK_SET) != 0)
294     return false;
295 
296   /* Read the leading portion that old and new core dump structures have in
297      common.  */
298   size = CORE_COMMONSZ;
299   if (bfd_read (core, size, abfd) != size)
300     return false;
301 
302   /* Read the trailing portion of the structure.  */
303   if (CORE_NEW (*core))
304     size = sizeof (core->new_dump);
305 #ifndef BFD64
306   else
307     size = sizeof (core->old);
308 #endif
309   size -= CORE_COMMONSZ;
310   return bfd_read ((char *) core + CORE_COMMONSZ, size, abfd) == size;
311 }
312 
313 static asection *
make_bfd_asection(bfd * abfd,const char * name,flagword flags,bfd_size_type size,bfd_vma vma,file_ptr filepos)314 make_bfd_asection (bfd *abfd, const char *name, flagword flags,
315 		   bfd_size_type size, bfd_vma vma, file_ptr filepos)
316 {
317   asection *asect;
318 
319   asect = bfd_make_section_anyway_with_flags (abfd, name, flags);
320   if (!asect)
321     return NULL;
322 
323   asect->size = size;
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
332    magic number or anything like, in rs6000coff.  */
333 
334 bfd_cleanup
rs6000coff_core_p(bfd * abfd)335 rs6000coff_core_p (bfd *abfd)
336 {
337   CoreHdr core;
338   struct stat statbuf;
339   bfd_size_type size;
340   char *tmpptr;
341 
342   /* Values from new and old core structures.  */
343   int c_flag;
344   file_ptr c_stack, c_regoff, c_loader;
345   bfd_size_type c_size, c_regsize, c_lsize, c_extoff;
346   bfd_vma c_stackend;
347   void *c_regptr;
348   int proc64;
349 
350   if (!read_hdr (abfd, &core))
351     {
352       if (bfd_get_error () != bfd_error_system_call)
353 	bfd_set_error (bfd_error_wrong_format);
354       return NULL;
355     }
356 
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 
364   /* Copy fields from new or old core structure.  */
365   if (CORE_NEW (core))
366     {
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);
373       c_extoff = core.new_dump.c_extctx;
374 #ifndef BFD64
375       proc64 = CNEW_PROC64 (core.new_dump);
376     }
377   else
378     {
379       c_flag = core.old.c_flag;
380       c_stack = (file_ptr) (ptr_to_uint) core.old.c_stack;
381       c_size = core.old.c_size;
382       c_stackend = COLD_STACKEND;
383       c_lsize = 0x7ffffff;
384       c_loader = (file_ptr) (ptr_to_uint) COLD_LOADER (core.old);
385 #endif
386       proc64 = 0;
387     }
388 
389   if (proc64)
390     {
391       c_regsize = sizeof (CNEW_CONTEXT64 (core.new_dump));
392       c_regptr = &CNEW_CONTEXT64 (core.new_dump);
393     }
394   else if (CORE_NEW (core))
395     {
396       c_regsize = sizeof (CNEW_MSTSAVE (core.new_dump));
397       c_regptr = &CNEW_MSTSAVE (core.new_dump);
398     }
399 #ifndef BFD64
400   else
401     {
402       c_regsize = sizeof (COLD_MSTSAVE (core.old));
403       c_regptr = &COLD_MSTSAVE (core.old);
404     }
405 #endif
406   c_regoff = (char *) c_regptr - (char *) &core;
407 
408   if (bfd_stat (abfd, &statbuf) < 0)
409     {
410       bfd_set_error (bfd_error_system_call);
411       return NULL;
412     }
413 
414   /* If the core file ulimit is too small, the system will first
415      omit the data segment, then omit the stack, then decline to
416      dump core altogether (as far as I know UBLOCK_VALID and LE_VALID
417      are always set) (this is based on experimentation on AIX 3.2).
418      Now, the thing is that GDB users will be surprised
419      if segments just silently don't appear (well, maybe they would
420      think to check "info files", I don't know).
421 
422      For the data segment, we have no choice but to keep going if it's
423      not there, since the default behavior is not to dump it (regardless
424      of the ulimit, it's based on SA_FULLDUMP).  But for the stack segment,
425      if it's not there, we refuse to have anything to do with this core
426      file.  The usefulness of a core dump without a stack segment is pretty
427      limited anyway.  */
428 
429   if (!(c_flag & UBLOCK_VALID)
430       || !(c_flag & LE_VALID))
431     {
432       bfd_set_error (bfd_error_wrong_format);
433       return NULL;
434     }
435 
436   if (!(c_flag & USTACK_VALID))
437     {
438       bfd_set_error (bfd_error_file_truncated);
439       return NULL;
440     }
441 
442   /* Don't check the core file size for a full core, AIX 4.1 includes
443      additional shared library sections in a full core.  */
444   if (!(c_flag & (FULL_CORE | CORE_TRUNC)))
445     {
446       /* If the size is wrong, it means we're misinterpreting something.  */
447       if (c_stack + (file_ptr) c_size != statbuf.st_size)
448 	{
449 	  bfd_set_error (bfd_error_wrong_format);
450 	  return NULL;
451 	}
452     }
453 
454   /* Sanity check on the c_tab field.  */
455   if (!CORE_NEW (core)
456       && (
457 #ifndef BFD64
458 	  c_loader < (file_ptr) sizeof core.old
459 #else
460 	  c_loader < (file_ptr) sizeof core.new_dump
461 #endif
462 	  || c_loader >= statbuf.st_size
463 	  || c_loader >= c_stack))
464     {
465       bfd_set_error (bfd_error_wrong_format);
466       return NULL;
467     }
468 
469   /* Issue warning if the core file was truncated during writing.  */
470   if (c_flag & CORE_TRUNC)
471     _bfd_error_handler (_("%pB: warning core file truncated"), abfd);
472 
473   /* Allocate core file header.  */
474 #ifndef BFD64
475   size = CORE_NEW (core) ? sizeof (core.new_dump) : sizeof (core.old);
476 #else
477   size =  sizeof (core.new_dump);
478 #endif
479   tmpptr = (char *) bfd_zalloc (abfd, (bfd_size_type) size);
480   if (!tmpptr)
481     return NULL;
482 
483   /* Copy core file header.  */
484   memcpy (tmpptr, &core, size);
485   set_tdata (abfd, tmpptr);
486 
487   /* Set architecture.  */
488   if (CORE_NEW (core))
489     {
490       enum bfd_architecture arch;
491       unsigned long mach;
492 
493       switch (CNEW_IMPL (core.new_dump))
494 	{
495 	case POWER_RS1:
496 	case POWER_RSC:
497 	case POWER_RS2:
498 	  arch = bfd_arch_rs6000;
499 	  mach = bfd_mach_rs6k;
500 	  break;
501 	default:
502 	  arch = bfd_arch_powerpc;
503 	  mach = bfd_mach_ppc;
504 	  break;
505 	}
506       bfd_default_set_arch_mach (abfd, arch, mach);
507     }
508 
509   /* .stack section.  */
510   if (!make_bfd_asection (abfd, ".stack",
511 			  SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
512 			  c_size, c_stackend - c_size, c_stack))
513     goto fail;
514 
515   /* .reg section for all registers.  */
516   if (!make_bfd_asection (abfd, ".reg",
517 			  SEC_HAS_CONTENTS,
518 			  c_regsize, (bfd_vma) 0, c_regoff))
519     goto fail;
520 
521   if (c_extoff)
522     {
523       if (!make_bfd_asection (abfd, ".aix-vmx",
524 			      SEC_HAS_CONTENTS,
525 			      560, (bfd_vma) 0, c_extoff))
526 	goto fail;
527 
528       if (!make_bfd_asection (abfd, ".aix-vsx",
529 			      SEC_HAS_CONTENTS,
530 			      256, (bfd_vma) 0, c_extoff + 584))
531 	goto fail;
532     }
533 
534   /* .ldinfo section.
535      To actually find out how long this section is in this particular
536      core dump would require going down the whole list of struct ld_info's.
537      See if we can just fake it.  */
538   if (!make_bfd_asection (abfd, ".ldinfo",
539 			  SEC_HAS_CONTENTS,
540 			  c_lsize, (bfd_vma) 0, c_loader))
541     goto fail;
542 
543 #ifndef CORE_VERSION_1
544   /* .data section if present.
545      AIX 3 dumps the complete data section and sets FULL_CORE if the
546      ulimit is large enough, otherwise the data section is omitted.
547      AIX 4 sets FULL_CORE even if the core file is truncated, we have
548      to examine core.c_datasize below to find out the actual size of
549      the .data section.  */
550   if (c_flag & FULL_CORE)
551     {
552       if (!make_bfd_asection (abfd, ".data",
553 			      SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
554 			      (bfd_size_type) core.old.c_u.u_dsize,
555 			      (bfd_vma)
556 				CDATA_ADDR (core.old.c_u.u_dsize),
557 			      c_stack + c_size))
558 	goto fail;
559     }
560 #endif
561 
562 #ifdef CORE_VERSION_1
563   /* AIX 4 adds data sections from loaded objects to the core file,
564      which can be found by examining ldinfo, and anonymously mmapped
565      regions.  */
566   {
567     LdInfo ldinfo;
568     bfd_size_type ldi_datasize;
569     file_ptr ldi_core;
570     uint ldi_next;
571     bfd_vma ldi_dataorg;
572     bfd_vma core_dataorg;
573 
574     /* Fields from new and old core structures.  */
575     bfd_size_type c_datasize, c_vmregions;
576     file_ptr c_data, c_vmm;
577 
578     if (CORE_NEW (core))
579       {
580 	c_datasize = CNEW_DATASIZE (core.new_dump);
581 	c_data = (file_ptr) core.new_dump.c_data;
582 	c_vmregions = core.new_dump.c_vmregions;
583 	c_vmm = (file_ptr) core.new_dump.c_vmm;
584       }
585 #ifndef BFD64
586     else
587       {
588 	c_datasize = core.old.c_datasize;
589 	c_data = (file_ptr) (ptr_to_uint) core.old.c_data;
590 	c_vmregions = core.old.c_vmregions;
591 	c_vmm = (file_ptr) (ptr_to_uint) core.old.c_vmm;
592       }
593 #endif
594 
595     /* .data section from executable.  */
596     if (c_datasize)
597       {
598 	/* If Large Memory Model is used, then the .data segment should start from
599 	   BDATAORG which has been defined in the system header files. */
600 
601 	if (c_flag & CORE_BIGDATA)
602 	  core_dataorg = BDATAORG;
603 	else
604 	  core_dataorg = CDATA_ADDR (c_datasize);
605 
606 	if (!make_bfd_asection (abfd, ".data",
607 				SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
608 				c_datasize,
609 				(bfd_vma) core_dataorg,
610 				c_data))
611 	  goto fail;
612       }
613 
614     /* .data sections from loaded objects.  */
615     if (proc64)
616       size = (unsigned long) ((LdInfo *) 0)->l64.ldinfo_filename;
617     else
618       size = (unsigned long) ((LdInfo *) 0)->l32.ldinfo_filename;
619 
620     while (1)
621       {
622 	if (bfd_seek (abfd, c_loader, SEEK_SET) != 0)
623 	  goto fail;
624 	if (bfd_read (&ldinfo, size, abfd) != size)
625 	  goto fail;
626 
627 	if (proc64)
628 	  {
629 	    ldi_core = ldinfo.l64.ldinfo_core;
630 	    ldi_datasize = ldinfo.l64.ldinfo_datasize;
631 	    ldi_dataorg = (bfd_vma) ldinfo.l64.ldinfo_dataorg;
632 	    ldi_next = ldinfo.l64.ldinfo_next;
633 	  }
634 	else
635 	  {
636 	    ldi_core = ldinfo.l32.ldinfo_core;
637 	    ldi_datasize = ldinfo.l32.ldinfo_datasize;
638 	    ldi_dataorg = (bfd_vma) (ptr_to_uint) ldinfo.l32.ldinfo_dataorg;
639 	    ldi_next = ldinfo.l32.ldinfo_next;
640 	  }
641 
642 	if (ldi_core)
643 	  if (!make_bfd_asection (abfd, ".data",
644 				  SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
645 				  ldi_datasize, ldi_dataorg, ldi_core))
646 	    goto fail;
647 
648 	if (ldi_next == 0)
649 	  break;
650 	c_loader += ldi_next;
651       }
652 
653     /* .vmdata sections from anonymously mmapped regions.  */
654     if (c_vmregions)
655       {
656 	bfd_size_type i;
657 
658 	if (bfd_seek (abfd, c_vmm, SEEK_SET) != 0)
659 	  goto fail;
660 
661 	for (i = 0; i < c_vmregions; i++)
662 	  {
663 	    VmInfo vminfo;
664 	    bfd_size_type vminfo_size;
665 	    file_ptr vminfo_offset;
666 	    bfd_vma vminfo_addr;
667 
668 #ifndef BFD64
669 	    size = CORE_NEW (core) ? sizeof (vminfo.new_dump) : sizeof (vminfo.old);
670 #else
671 	    size = sizeof (vminfo.new_dump);
672 #endif
673 	    if (bfd_read (&vminfo, size, abfd) != size)
674 	      goto fail;
675 
676 	    if (CORE_NEW (core))
677 	      {
678 		vminfo_addr = (bfd_vma) vminfo.new_dump.vminfo_addr;
679 		vminfo_size = vminfo.new_dump.vminfo_size;
680 		vminfo_offset = vminfo.new_dump.vminfo_offset;
681 	      }
682 #ifndef BFD64
683 	    else
684 	      {
685 		vminfo_addr = (bfd_vma) (ptr_to_uint) vminfo.old.vminfo_addr;
686 		vminfo_size = vminfo.old.vminfo_size;
687 		vminfo_offset = vminfo.old.vminfo_offset;
688 	      }
689 #endif
690 
691 	    if (vminfo_offset)
692 	      if (!make_bfd_asection (abfd, ".vmdata",
693 				      SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
694 				      vminfo_size, vminfo_addr,
695 				      vminfo_offset))
696 		goto fail;
697 	  }
698       }
699   }
700 #endif
701 
702   return _bfd_no_cleanup;
703 
704  fail:
705   bfd_release (abfd, abfd->tdata.any);
706   abfd->tdata.any = NULL;
707   bfd_section_list_clear (abfd);
708   return NULL;
709 }
710 
711 /* Return `TRUE' if given core is from the given executable.  */
712 
713 bool
rs6000coff_core_file_matches_executable_p(bfd * core_bfd,bfd * exec_bfd)714 rs6000coff_core_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd)
715 {
716   CoreHdr core;
717   bfd_size_type size;
718   char *path, *s;
719   size_t alloc;
720   const char *str1, *str2;
721   bool ret;
722   file_ptr c_loader;
723 
724   if (!read_hdr (core_bfd, &core))
725     return false;
726 
727   if (CORE_NEW (core))
728     c_loader = CNEW_LOADER (core.new_dump);
729 #ifndef BFD64
730   else
731     c_loader = (file_ptr) (ptr_to_uint) COLD_LOADER (core.old);
732 #endif
733 
734   if (CORE_NEW (core) && CNEW_PROC64 (core.new_dump))
735     size = (int) ((LdInfo *) 0)->l64.ldinfo_filename;
736   else
737     size = (int) ((LdInfo *) 0)->l32.ldinfo_filename;
738 
739   if (bfd_seek (core_bfd, c_loader + size, SEEK_SET) != 0)
740     return false;
741 
742   alloc = 100;
743   path = bfd_malloc ((bfd_size_type) alloc);
744   if (path == NULL)
745     return false;
746   s = path;
747 
748   while (1)
749     {
750       if (bfd_read (s, 1, core_bfd) != 1)
751 	{
752 	  free (path);
753 	  return false;
754 	}
755       if (*s == '\0')
756 	break;
757       ++s;
758       if (s == path + alloc)
759 	{
760 	  char *n;
761 
762 	  alloc *= 2;
763 	  n = bfd_realloc (path, (bfd_size_type) alloc);
764 	  if (n == NULL)
765 	    {
766 	      free (path);
767 	      return false;
768 	    }
769 	  s = n + (path - s);
770 	  path = n;
771 	}
772     }
773 
774   str1 = strrchr (path, '/');
775   str2 = strrchr (bfd_get_filename (exec_bfd), '/');
776 
777   /* step over character '/' */
778   str1 = str1 != NULL ? str1 + 1 : path;
779   str2 = str2 != NULL ? str2 + 1 : bfd_get_filename (exec_bfd);
780 
781   if (strcmp (str1, str2) == 0)
782     ret = true;
783   else
784     ret = false;
785 
786   free (path);
787 
788   return ret;
789 }
790 
791 char *
rs6000coff_core_file_failing_command(bfd * abfd)792 rs6000coff_core_file_failing_command (bfd *abfd)
793 {
794   CoreHdr *core = core_hdr (abfd);
795 #ifndef BFD64
796   char *com = CORE_NEW (*core) ?
797     CNEW_COMM (core->new_dump) : COLD_COMM (core->old);
798 #else
799   char *com = CNEW_COMM (core->new_dump);
800 #endif
801 
802   if (*com)
803     return com;
804   else
805     return 0;
806 }
807 
808 int
rs6000coff_core_file_failing_signal(bfd * abfd)809 rs6000coff_core_file_failing_signal (bfd *abfd)
810 {
811   CoreHdr *core = core_hdr (abfd);
812 #ifndef BFD64
813   return CORE_NEW (*core) ? core->new_dump.c_signo : core->old.c_signo;
814 #else
815   return  core->new_dump.c_signo;
816 #endif
817 }
818 
819 #endif /* AIX_CORE */
820