1 /* IBM RS/6000 "XCOFF" back-end for BFD.
2 Copyright (C) 1990-2016 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.
7 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
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.
29 This is in a separate file from coff-rs6000.c, because it includes
30 system include files that conflict with coff/rs6000.h. */
32 /* Internalcoff.h and coffcode.h modify themselves based on this flag. */
33 #define RS6000COFF_C 1
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)
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. */
54 #define AOUTHDR second_AOUTHDR
58 /* Support for core file stuff. */
61 #define __LDINFO_PTRACE32__ /* for __ld_info32 */
62 #define __LDINFO_PTRACE64__ /* for __ld_info64 */
65 #include <sys/systemcfg.h>
67 /* Borrowed from <sys/inttypes.h> on recent AIX versions. */
68 typedef unsigned long ptr_to_uint;
70 #define core_hdr(bfd) ((CoreHdr *) bfd->tdata.any)
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.
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. */
81 #if defined(CORE_VERSION_1) && !CORE_VERSION_1
82 # undef CORE_VERSION_1
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
90 /* Union of 32-bit and 64-bit versions of ld_info. */
94 #if defined (__ld_info32) || defined (__ld_info64)
95 struct __ld_info32 l32;
96 struct __ld_info64 l64;
103 /* Union of old and new core dump structures. */
107 #ifdef AIX_CORE_DUMPX_CORE
108 struct core_dumpx new_dump; /* New AIX 4.3+ core dump. */
110 struct core_dump new_dump; /* For simpler coding. */
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. */
118 /* Union of old and new vm_info structures. */
120 #ifdef CORE_VERSION_1
123 #ifdef AIX_CORE_DUMPX_CORE
124 struct vm_infox new_dump;
126 struct vm_info new_dump;
134 /* Return whether CoreHdr C is in new or old format. */
136 #ifdef AIX_CORE_DUMPX_CORE
138 # define CORE_NEW(c) (!(c).old.c_entries)
140 # define CORE_NEW(c) (!(c).new_dump.c_entries)
143 # define CORE_NEW(c) 0
146 /* Return whether CoreHdr C usese core_dumpxx structure.
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. */
153 # define CORE_DUMPXX_VERSION 267312562
154 # define CNEW_IS_CORE_DUMPXX(c) ((c).new_dump.c_version == CORE_DUMPXX_VERSION)
156 # define CNEW_IS_CORE_DUMPXX(c) 0
159 /* Return the c_stackorg field from struct core_dumpx C. */
161 #ifdef AIX_CORE_DUMPX_CORE
162 # define CNEW_STACKORG(c) (c).c_stackorg
164 # define CNEW_STACKORG(c) 0
167 /* Return the offset to the loader region from struct core_dump C. */
169 #ifdef AIX_CORE_DUMPX_CORE
170 # define CNEW_LOADER(c) (c).c_loader
172 # define CNEW_LOADER(c) 0
175 /* Return the offset to the loader region from struct core_dump C. */
177 #define COLD_LOADER(c) (c).c_tab
179 /* Return the c_lsize field from struct core_dumpx C. */
181 #ifdef AIX_CORE_DUMPX_CORE
182 # define CNEW_LSIZE(c) (c).c_lsize
184 # define CNEW_LSIZE(c) 0
187 /* Return the c_dataorg field from struct core_dumpx C. */
189 #ifdef AIX_CORE_DUMPX_CORE
190 # define CNEW_DATAORG(c) (c).c_dataorg
192 # define CNEW_DATAORG(c) 0
195 /* Return the c_datasize field from struct core_dumpx C. */
197 #ifdef AIX_CORE_DUMPX_CORE
198 # define CNEW_DATASIZE(c) (c).c_datasize
200 # define CNEW_DATASIZE(c) 0
203 /* Return the c_impl field from struct core_dumpx C. */
205 #if defined (HAVE_ST_C_IMPL) || defined (AIX_5_CORE)
206 # define CNEW_IMPL(c) (c).c_impl
208 # define CNEW_IMPL(c) 0
211 /* Return the command string from struct core_dumpx C. */
213 #ifdef AIX_CORE_DUMPX_CORE
214 # define CNEW_COMM(c) (c).c_u.U_proc.pi_comm
216 # define CNEW_COMM(c) 0
219 /* Return the command string from struct core_dump C. */
221 #ifdef CORE_VERSION_1
222 # define COLD_COMM(c) (c).c_u.U_comm
224 # define COLD_COMM(c) (c).c_u.u_comm
227 /* Return the struct __context64 pointer from struct core_dumpx C. */
229 #ifdef AIX_CORE_DUMPX_CORE
230 # define CNEW_CONTEXT64(c) (c).c_flt.hctx.r64
232 # define CNEW_CONTEXT64(c) c
235 /* Return the struct mstsave pointer from struct core_dumpx C. */
237 #ifdef AIX_CORE_DUMPX_CORE
238 # define CNEW_MSTSAVE(c) (c).c_flt.hctx.r32
240 # define CNEW_MSTSAVE(c) c
243 /* Return the struct mstsave pointer from struct core_dump C. */
245 #ifdef CORE_VERSION_1
246 # define COLD_MSTSAVE(c) (c).c_mst
248 # define COLD_MSTSAVE(c) (c).c_u.u_save
251 /* Return whether struct core_dumpx is from a 64-bit process. */
253 #ifdef AIX_CORE_DUMPX_CORE
254 # define CNEW_PROC64(c) IS_PROC64(&(c).c_u.U_proc)
256 # define CNEW_PROC64(c) 0
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. */
262 #ifdef CORE_VERSION_1
263 # define COLD_STACKEND 0x2ff23000
265 # define COLD_STACKEND 0x2ff80000
268 /* Size of the leading portion that old and new core dump structures have in
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))
274 #define CORE_COMMONSZ ((int) &((struct core_dump *) 0)->c_entries \
275 + sizeof (((struct core_dump *) 0)->c_entries)
277 /* Define prototypes for certain functions, to avoid a compiler warning
278 saying that they are missing. */
280 const bfd_target * rs6000coff_core_p (bfd *abfd);
281 bfd_boolean rs6000coff_core_file_matches_executable_p (bfd *core_bfd,
283 char * rs6000coff_core_file_failing_command (bfd *abfd);
284 int rs6000coff_core_file_failing_signal (bfd *abfd);
286 /* Try to read into CORE the header from the core file associated with ABFD.
290 read_hdr (bfd *abfd, CoreHdr *core)
294 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
297 /* Read the leading portion that old and new core dump structures have in
299 size = CORE_COMMONSZ;
300 if (bfd_bread (core, size, abfd) != size)
303 /* Read the trailing portion of the structure. */
304 if (CORE_NEW (*core))
305 size = sizeof (core->new_dump);
308 size = sizeof (core->old);
310 size -= CORE_COMMONSZ;
311 return bfd_bread ((char *) core + CORE_COMMONSZ, size, abfd) == size;
315 make_bfd_asection (bfd *abfd, const char *name, flagword flags,
316 bfd_size_type size, bfd_vma vma, file_ptr filepos)
320 asect = bfd_make_section_anyway_with_flags (abfd, name, flags);
326 asect->filepos = filepos;
327 asect->alignment_power = 8;
332 /* Decide if a given bfd represents a `core' file or not. There really is no
333 magic number or anything like, in rs6000coff. */
336 rs6000coff_core_p (bfd *abfd)
343 /* Values from new and old core structures. */
345 file_ptr c_stack, c_regoff, c_loader;
346 bfd_size_type c_size, c_regsize, c_lsize;
351 if (!read_hdr (abfd, &core))
353 if (bfd_get_error () != bfd_error_system_call)
354 bfd_set_error (bfd_error_wrong_format);
358 /* This isn't the right handler for 64-bit core files on AIX 5.x. */
359 if (CORE_NEW (core) && CNEW_IS_CORE_DUMPXX (core))
361 bfd_set_error (bfd_error_wrong_format);
365 /* Copy fields from new or old core structure. */
368 c_flag = core.new_dump.c_flag;
369 c_stack = (file_ptr) core.new_dump.c_stack;
370 c_size = core.new_dump.c_size;
371 c_stackend = CNEW_STACKORG (core.new_dump) + c_size;
372 c_lsize = CNEW_LSIZE (core.new_dump);
373 c_loader = CNEW_LOADER (core.new_dump);
375 proc64 = CNEW_PROC64 (core.new_dump);
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;
384 c_loader = (file_ptr) (ptr_to_uint) COLD_LOADER (core.old);
391 c_regsize = sizeof (CNEW_CONTEXT64 (core.new_dump));
392 c_regptr = &CNEW_CONTEXT64 (core.new_dump);
394 else if (CORE_NEW (core))
396 c_regsize = sizeof (CNEW_MSTSAVE (core.new_dump));
397 c_regptr = &CNEW_MSTSAVE (core.new_dump);
402 c_regsize = sizeof (COLD_MSTSAVE (core.old));
403 c_regptr = &COLD_MSTSAVE (core.old);
406 c_regoff = (char *) c_regptr - (char *) &core;
408 if (bfd_stat (abfd, &statbuf) < 0)
410 bfd_set_error (bfd_error_system_call);
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).
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
429 if (!(c_flag & UBLOCK_VALID)
430 || !(c_flag & LE_VALID))
432 bfd_set_error (bfd_error_wrong_format);
436 if (!(c_flag & USTACK_VALID))
438 bfd_set_error (bfd_error_file_truncated);
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)))
446 /* If the size is wrong, it means we're misinterpreting something. */
447 if (c_stack + (file_ptr) c_size != statbuf.st_size)
449 bfd_set_error (bfd_error_wrong_format);
454 /* Sanity check on the c_tab field. */
458 c_loader < (file_ptr) sizeof core.old
460 c_loader < (file_ptr) sizeof core.new_dump
462 || c_loader >= statbuf.st_size
463 || c_loader >= c_stack))
465 bfd_set_error (bfd_error_wrong_format);
469 /* Issue warning if the core file was truncated during writing. */
470 if (c_flag & CORE_TRUNC)
471 (*_bfd_error_handler) (_("%s: warning core file truncated"),
472 bfd_get_filename (abfd));
474 /* Allocate core file header. */
476 size = CORE_NEW (core) ? sizeof (core.new_dump) : sizeof (core.old);
478 size = sizeof (core.new_dump);
480 tmpptr = (char *) bfd_zalloc (abfd, (bfd_size_type) size);
484 /* Copy core file header. */
485 memcpy (tmpptr, &core, size);
486 set_tdata (abfd, tmpptr);
488 /* Set architecture. */
491 enum bfd_architecture arch;
494 switch (CNEW_IMPL (core.new_dump))
499 arch = bfd_arch_rs6000;
500 mach = bfd_mach_rs6k;
503 arch = bfd_arch_powerpc;
507 bfd_default_set_arch_mach (abfd, arch, mach);
510 /* .stack section. */
511 if (!make_bfd_asection (abfd, ".stack",
512 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
513 c_size, c_stackend - c_size, c_stack))
516 /* .reg section for all registers. */
517 if (!make_bfd_asection (abfd, ".reg",
519 c_regsize, (bfd_vma) 0, c_regoff))
523 To actually find out how long this section is in this particular
524 core dump would require going down the whole list of struct ld_info's.
525 See if we can just fake it. */
526 if (!make_bfd_asection (abfd, ".ldinfo",
528 c_lsize, (bfd_vma) 0, c_loader))
531 #ifndef CORE_VERSION_1
532 /* .data section if present.
533 AIX 3 dumps the complete data section and sets FULL_CORE if the
534 ulimit is large enough, otherwise the data section is omitted.
535 AIX 4 sets FULL_CORE even if the core file is truncated, we have
536 to examine core.c_datasize below to find out the actual size of
537 the .data section. */
538 if (c_flag & FULL_CORE)
540 if (!make_bfd_asection (abfd, ".data",
541 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
542 (bfd_size_type) core.old.c_u.u_dsize,
544 CDATA_ADDR (core.old.c_u.u_dsize),
550 #ifdef CORE_VERSION_1
551 /* AIX 4 adds data sections from loaded objects to the core file,
552 which can be found by examining ldinfo, and anonymously mmapped
556 bfd_size_type ldi_datasize;
560 bfd_vma core_dataorg;
562 /* Fields from new and old core structures. */
563 bfd_size_type c_datasize, c_vmregions;
564 file_ptr c_data, c_vmm;
568 c_datasize = CNEW_DATASIZE (core.new_dump);
569 c_data = (file_ptr) core.new_dump.c_data;
570 c_vmregions = core.new_dump.c_vmregions;
571 c_vmm = (file_ptr) core.new_dump.c_vmm;
576 c_datasize = core.old.c_datasize;
577 c_data = (file_ptr) (ptr_to_uint) core.old.c_data;
578 c_vmregions = core.old.c_vmregions;
579 c_vmm = (file_ptr) (ptr_to_uint) core.old.c_vmm;
583 /* .data section from executable. */
586 /* If Large Memory Model is used, then the .data segment should start from
587 BDATAORG which has been defined in the system header files. */
589 if (c_flag & CORE_BIGDATA)
590 core_dataorg = BDATAORG;
592 core_dataorg = CDATA_ADDR (c_datasize);
594 if (!make_bfd_asection (abfd, ".data",
595 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
597 (bfd_vma) core_dataorg,
602 /* .data sections from loaded objects. */
604 size = (unsigned long) ((LdInfo *) 0)->l64.ldinfo_filename;
606 size = (unsigned long) ((LdInfo *) 0)->l32.ldinfo_filename;
610 if (bfd_seek (abfd, c_loader, SEEK_SET) != 0)
612 if (bfd_bread (&ldinfo, size, abfd) != size)
617 ldi_core = ldinfo.l64.ldinfo_core;
618 ldi_datasize = ldinfo.l64.ldinfo_datasize;
619 ldi_dataorg = (bfd_vma) ldinfo.l64.ldinfo_dataorg;
620 ldi_next = ldinfo.l64.ldinfo_next;
624 ldi_core = ldinfo.l32.ldinfo_core;
625 ldi_datasize = ldinfo.l32.ldinfo_datasize;
626 ldi_dataorg = (bfd_vma) (ptr_to_uint) ldinfo.l32.ldinfo_dataorg;
627 ldi_next = ldinfo.l32.ldinfo_next;
631 if (!make_bfd_asection (abfd, ".data",
632 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
633 ldi_datasize, ldi_dataorg, ldi_core))
638 c_loader += ldi_next;
641 /* .vmdata sections from anonymously mmapped regions. */
646 if (bfd_seek (abfd, c_vmm, SEEK_SET) != 0)
649 for (i = 0; i < c_vmregions; i++)
652 bfd_size_type vminfo_size;
653 file_ptr vminfo_offset;
657 size = CORE_NEW (core) ? sizeof (vminfo.new_dump) : sizeof (vminfo.old);
659 size = sizeof (vminfo.new_dump);
661 if (bfd_bread (&vminfo, size, abfd) != size)
666 vminfo_addr = (bfd_vma) vminfo.new_dump.vminfo_addr;
667 vminfo_size = vminfo.new_dump.vminfo_size;
668 vminfo_offset = vminfo.new_dump.vminfo_offset;
673 vminfo_addr = (bfd_vma) (ptr_to_uint) vminfo.old.vminfo_addr;
674 vminfo_size = vminfo.old.vminfo_size;
675 vminfo_offset = vminfo.old.vminfo_offset;
680 if (!make_bfd_asection (abfd, ".vmdata",
681 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
682 vminfo_size, vminfo_addr,
690 return abfd->xvec; /* This is garbage for now. */
693 bfd_release (abfd, abfd->tdata.any);
694 abfd->tdata.any = NULL;
695 bfd_section_list_clear (abfd);
699 /* Return `TRUE' if given core is from the given executable. */
702 rs6000coff_core_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd)
708 const char *str1, *str2;
712 if (!read_hdr (core_bfd, &core))
716 c_loader = CNEW_LOADER (core.new_dump);
719 c_loader = (file_ptr) (ptr_to_uint) COLD_LOADER (core.old);
722 if (CORE_NEW (core) && CNEW_PROC64 (core.new_dump))
723 size = (int) ((LdInfo *) 0)->l64.ldinfo_filename;
725 size = (int) ((LdInfo *) 0)->l32.ldinfo_filename;
727 if (bfd_seek (core_bfd, c_loader + size, SEEK_SET) != 0)
731 path = bfd_malloc ((bfd_size_type) alloc);
738 if (bfd_bread (s, (bfd_size_type) 1, core_bfd) != 1)
746 if (s == path + alloc)
751 n = bfd_realloc (path, (bfd_size_type) alloc);
762 str1 = strrchr (path, '/');
763 str2 = strrchr (exec_bfd->filename, '/');
765 /* step over character '/' */
766 str1 = str1 != NULL ? str1 + 1 : path;
767 str2 = str2 != NULL ? str2 + 1 : exec_bfd->filename;
769 if (strcmp (str1, str2) == 0)
780 rs6000coff_core_file_failing_command (bfd *abfd)
782 CoreHdr *core = core_hdr (abfd);
784 char *com = CORE_NEW (*core) ?
785 CNEW_COMM (core->new_dump) : COLD_COMM (core->old);
787 char *com = CNEW_COMM (core->new_dump);
797 rs6000coff_core_file_failing_signal (bfd *abfd)
799 CoreHdr *core = core_hdr (abfd);
801 return CORE_NEW (*core) ? core->new_dump.c_signo : core->old.c_signo;
803 return core->new_dump.c_signo;
807 #endif /* AIX_CORE */