* Makefile.in (all-bootstrap): Added all-texinfo and all-zlib.
[binutils.git] / bfd / rs6000-core.c
blob7f6bec462e2313b0491ca13b2181a4a3283b23dc
1 /* IBM RS/6000 "XCOFF" back-end for BFD.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 98, 2000
3 Free Software Foundation, Inc.
4 FIXME: Can someone provide a transliteration of this name into ASCII?
5 Using the following chars caused a compiler warning on HIUX (so I replaced
6 them with octal escapes), and isn't useful without an understanding of what
7 character set it is.
8 Written by Metin G. Ozisik, Mimi Ph\373\364ng-Th\345o V\365,
9 and John Gilmore.
10 Archive support from Damon A. Permezel.
11 Contributed by IBM Corporation and Cygnus Support.
13 This file is part of BFD, the Binary File Descriptor library.
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
29 /* This port currently only handles reading object files, except when
30 compiled on an RS/6000 host. -- no archive support, no core files.
31 In all cases, it does not support writing.
33 This is in a separate file from coff-rs6000.c, because it includes
34 system include files that conflict with coff/rs6000.h.
37 /* Internalcoff.h and coffcode.h modify themselves based on this flag. */
38 #define RS6000COFF_C 1
40 /* The AIX 4.1 kernel is obviously compiled with -D_LONG_LONG, so
41 we have to define _LONG_LONG for older versions of gcc to get the
42 proper alignments in the user structure. */
43 #if defined(_AIX41) && !defined(_LONG_LONG)
44 #define _LONG_LONG
45 #endif
47 #include "bfd.h"
48 #include "sysdep.h"
49 #include "libbfd.h"
51 #ifdef AIX_CORE
53 /* AOUTHDR is defined by the above. We need another defn of it, from the
54 system include files. Punt the old one and get us a new name for the
55 typedef in the system include files. */
56 #ifdef AOUTHDR
57 #undef AOUTHDR
58 #endif
59 #define AOUTHDR second_AOUTHDR
61 #undef SCNHDR
64 /* ------------------------------------------------------------------------ */
65 /* Support for core file stuff.. */
66 /* ------------------------------------------------------------------------ */
68 #include <sys/user.h>
69 #define __LDINFO_PTRACE32__ /* for __ld_info32 */
70 #define __LDINFO_PTRACE64__ /* for __ld_info64 */
71 #include <sys/ldr.h>
72 #include <sys/core.h>
73 #include <sys/systemcfg.h>
75 #define core_hdr(bfd) ((CoreHdr *) bfd->tdata.any)
77 /* AIX 4.1 changed the names and locations of a few items in the core file.
78 AIX 4.3 defined an entirely new structure, core_dumpx, but kept support for
79 the previous 4.1 structure, core_dump.
81 AIX_CORE_DUMPX_CORE is defined (by configure) on AIX 4.3+, and
82 CORE_VERSION_1 is defined (by AIX core.h) as 2 on AIX 4.3+ and as 1 on AIX
83 4.1 and 4.2. AIX pre-4.1 (aka 3.x) either doesn't define CORE_VERSION_1
84 or else defines it as 0. */
86 #if defined(CORE_VERSION_1) && !CORE_VERSION_1
87 # undef CORE_VERSION_1
88 #endif
90 /* The following union and macros allow this module to compile on all AIX
91 versions and to handle both core_dumpx and core_dump on 4.3+. CNEW_*()
92 and COLD_*() macros respectively retrieve core_dumpx and core_dump
93 values. */
95 /* Union of 32-bit and 64-bit versions of ld_info. */
97 typedef union {
98 #ifdef __ld_info32
99 struct __ld_info32 l32;
100 struct __ld_info64 l64;
101 #else
102 struct ld_info l32;
103 struct ld_info l64;
104 #endif
105 } LdInfo;
107 /* Union of old and new core dump structures. */
109 typedef union {
110 #ifdef AIX_CORE_DUMPX_CORE
111 struct core_dumpx new; /* new AIX 4.3+ core dump */
112 #else
113 struct core_dump new; /* for simpler coding */
114 #endif
115 struct core_dump old; /* old AIX 4.2- core dump, still used on
116 4.3+ with appropriate SMIT config */
117 } CoreHdr;
119 /* Union of old and new vm_info structures. */
121 #ifdef CORE_VERSION_1
122 typedef union {
123 #ifdef AIX_CORE_DUMPX_CORE
124 struct vm_infox new;
125 #else
126 struct vm_info new;
127 #endif
128 struct vm_info old;
129 } VmInfo;
130 #endif
132 /* Return whether CoreHdr C is in new or old format. */
134 #ifdef AIX_CORE_DUMPX_CORE
135 # define CORE_NEW(c) (!(c).old.c_entries)
136 #else
137 # define CORE_NEW(c) 0
138 #endif
140 /* Return the c_stackorg field from struct core_dumpx C. */
142 #ifdef AIX_CORE_DUMPX_CORE
143 # define CNEW_STACKORG(c) (c).c_stackorg
144 #else
145 # define CNEW_STACKORG(c) 0
146 #endif
148 /* Return the offset to the loader region from struct core_dump C. */
150 #ifdef AIX_CORE_DUMPX_CORE
151 # define CNEW_LOADER(c) (c).c_loader
152 #else
153 # define CNEW_LOADER(c) 0
154 #endif
156 /* Return the offset to the loader region from struct core_dump C. */
158 #define COLD_LOADER(c) (c).c_tab
160 /* Return the c_lsize field from struct core_dumpx C. */
162 #ifdef AIX_CORE_DUMPX_CORE
163 # define CNEW_LSIZE(c) (c).c_lsize
164 #else
165 # define CNEW_LSIZE(c) 0
166 #endif
168 /* Return the c_dataorg field from struct core_dumpx C. */
170 #ifdef AIX_CORE_DUMPX_CORE
171 # define CNEW_DATAORG(c) (c).c_dataorg
172 #else
173 # define CNEW_DATAORG(c) 0
174 #endif
176 /* Return the c_datasize field from struct core_dumpx C. */
178 #ifdef AIX_CORE_DUMPX_CORE
179 # define CNEW_DATASIZE(c) (c).c_datasize
180 #else
181 # define CNEW_DATASIZE(c) 0
182 #endif
184 /* Return the c_impl field from struct core_dumpx C. */
186 #ifdef AIX_CORE_DUMPX_CORE
187 # define CNEW_IMPL(c) (c).c_impl
188 #else
189 # define CNEW_IMPL(c) 0
190 #endif
192 /* Return the command string from struct core_dumpx C. */
194 #ifdef AIX_CORE_DUMPX_CORE
195 # define CNEW_COMM(c) (c).c_u.U_proc.pi_comm
196 #else
197 # define CNEW_COMM(c) 0
198 #endif
200 /* Return the command string from struct core_dump C. */
202 #ifdef CORE_VERSION_1
203 # define COLD_COMM(c) (c).c_u.U_comm
204 #else
205 # define COLD_COMM(c) (c).c_u.u_comm
206 #endif
208 /* Return the struct __context64 pointer from struct core_dumpx C. */
210 #ifdef AIX_CORE_DUMPX_CORE
211 # define CNEW_CONTEXT64(c) (c).c_flt.hctx.r64
212 #else
213 # define CNEW_CONTEXT64(c) c
214 #endif
216 /* Return the struct mstsave pointer from struct core_dumpx C. */
218 #ifdef AIX_CORE_DUMPX_CORE
219 # define CNEW_MSTSAVE(c) (c).c_flt.hctx.r32
220 #else
221 # define CNEW_MSTSAVE(c) c
222 #endif
224 /* Return the struct mstsave pointer from struct core_dump C. */
226 #ifdef CORE_VERSION_1
227 # define COLD_MSTSAVE(c) (c).c_mst
228 #else
229 # define COLD_MSTSAVE(c) (c).c_u.u_save
230 #endif
232 /* Return whether struct core_dumpx is from a 64-bit process. */
234 #ifdef AIX_CORE_DUMPX_CORE
235 # define CNEW_PROC64(c) IS_PROC64(&(c).c_u.U_proc)
236 #else
237 # define CNEW_PROC64(c) 0
238 #endif
240 /* Magic end-of-stack addresses for old core dumps. This is _very_ fragile,
241 but I don't see any easy way to get that info right now. */
243 #ifdef CORE_VERSION_1
244 # define COLD_STACKEND 0x2ff23000
245 #else
246 # define COLD_STACKEND 0x2ff80000
247 #endif
249 /* Size of the leading portion that old and new core dump structures have in
250 common. */
251 #define CORE_COMMONSZ ((int)&((struct core_dump *)0)->c_entries + \
252 sizeof (((struct core_dump *)0)->c_entries))
254 /* Try to read into CORE the header from the core file associated with ABFD.
255 Return success. */
257 static boolean
258 read_hdr (bfd *abfd, CoreHdr *core)
260 bfd_size_type size;
262 if (bfd_seek (abfd, 0, SEEK_SET) != 0)
263 return false;
265 /* Read the leading portion that old and new core dump structures have in
266 common. */
267 if (bfd_read (core, CORE_COMMONSZ, 1, abfd) != CORE_COMMONSZ)
268 return false;
270 /* Read the trailing portion of the structure. */
271 size = CORE_NEW (*core) ? sizeof (core->new) : sizeof (core->old)
272 - CORE_COMMONSZ;
273 return bfd_read ((char *)core + CORE_COMMONSZ, size, 1, abfd) == size;
276 static asection *
277 make_bfd_asection (abfd, name, flags, _raw_size, vma, filepos)
278 bfd *abfd;
279 CONST char *name;
280 flagword flags;
281 bfd_size_type _raw_size;
282 bfd_vma vma;
283 file_ptr filepos;
285 asection *asect;
287 asect = bfd_make_section_anyway (abfd, name);
288 if (!asect)
289 return NULL;
291 asect->flags = flags;
292 asect->_raw_size = _raw_size;
293 asect->vma = vma;
294 asect->filepos = filepos;
295 asect->alignment_power = 8;
297 return asect;
300 /* Decide if a given bfd represents a `core' file or not. There really is no
301 magic number or anything like, in rs6000coff. */
303 const bfd_target *
304 rs6000coff_core_p (abfd)
305 bfd *abfd;
307 CoreHdr core;
308 struct stat statbuf;
309 bfd_size_type size;
310 char *tmpptr;
312 /* Values from new and old core structures. */
313 int c_flag;
314 file_ptr c_stack, c_regoff, c_loader;
315 bfd_size_type c_size, c_regsize, c_lsize;
316 bfd_vma c_stackend;
317 void *c_regptr;
318 int proc64;
320 if (!read_hdr (abfd, &core))
322 if (bfd_get_error () != bfd_error_system_call)
323 bfd_set_error (bfd_error_wrong_format);
324 return NULL;
327 /* Copy fields from new or old core structure. */
328 if (CORE_NEW (core))
330 c_flag = core.new.c_flag;
331 c_stack = (file_ptr) core.new.c_stack;
332 c_size = core.new.c_size;
333 c_stackend = CNEW_STACKORG (core.new) + c_size;
334 c_lsize = CNEW_LSIZE (core.new);
335 c_loader = CNEW_LOADER (core.new);
336 proc64 = CNEW_PROC64 (core.new);
338 else
340 c_flag = core.old.c_flag;
341 c_stack = (file_ptr) core.old.c_stack;
342 c_size = core.old.c_size;
343 c_stackend = COLD_STACKEND;
344 c_lsize = 0x7ffffff;
345 c_loader = (file_ptr) COLD_LOADER (core.old);
346 proc64 = 0;
349 if (proc64)
351 c_regsize = sizeof (CNEW_CONTEXT64 (core.new));
352 c_regptr = &CNEW_CONTEXT64 (core.new);
354 else if (CORE_NEW (core))
356 c_regsize = sizeof (CNEW_MSTSAVE (core.new));
357 c_regptr = &CNEW_MSTSAVE (core.new);
359 else
361 c_regsize = sizeof (COLD_MSTSAVE (core.old));
362 c_regptr = &COLD_MSTSAVE (core.old);
364 c_regoff = (char *)c_regptr - (char *)&core;
366 if (bfd_stat (abfd, &statbuf) < 0)
368 bfd_set_error (bfd_error_system_call);
369 return NULL;
372 /* If the core file ulimit is too small, the system will first
373 omit the data segment, then omit the stack, then decline to
374 dump core altogether (as far as I know UBLOCK_VALID and LE_VALID
375 are always set) (this is based on experimentation on AIX 3.2).
376 Now, the thing is that GDB users will be surprised
377 if segments just silently don't appear (well, maybe they would
378 think to check "info files", I don't know).
380 For the data segment, we have no choice but to keep going if it's
381 not there, since the default behavior is not to dump it (regardless
382 of the ulimit, it's based on SA_FULLDUMP). But for the stack segment,
383 if it's not there, we refuse to have anything to do with this core
384 file. The usefulness of a core dump without a stack segment is pretty
385 limited anyway. */
387 if (!(c_flag & UBLOCK_VALID)
388 || !(c_flag & LE_VALID))
390 bfd_set_error (bfd_error_wrong_format);
391 return NULL;
394 if (!(c_flag & USTACK_VALID))
396 bfd_set_error (bfd_error_file_truncated);
397 return NULL;
400 /* Don't check the core file size for a full core, AIX 4.1 includes
401 additional shared library sections in a full core. */
402 if (!(c_flag & (FULL_CORE | CORE_TRUNC)))
404 /* If the size is wrong, it means we're misinterpreting something. */
405 if (c_stack + (file_ptr) c_size != statbuf.st_size)
407 bfd_set_error (bfd_error_wrong_format);
408 return NULL;
412 /* Sanity check on the c_tab field. */
413 if (!CORE_NEW (core) && (c_loader < (file_ptr) sizeof core.old ||
414 c_loader >= statbuf.st_size ||
415 c_loader >= c_stack))
417 bfd_set_error (bfd_error_wrong_format);
418 return NULL;
421 /* Issue warning if the core file was truncated during writing. */
422 if (c_flag & CORE_TRUNC)
423 (*_bfd_error_handler) (_("%s: warning core file truncated"),
424 bfd_get_filename (abfd));
426 /* Allocate core file header. */
427 size = CORE_NEW (core) ? sizeof (core.new) : sizeof (core.old);
428 tmpptr = (char*) bfd_zalloc (abfd, size);
429 if (!tmpptr)
430 return NULL;
432 /* Copy core file header. */
433 memcpy (tmpptr, &core, size);
434 set_tdata (abfd, tmpptr);
436 /* Set architecture. */
437 if (CORE_NEW (core))
439 enum bfd_architecture arch;
440 unsigned long mach;
442 switch (CNEW_IMPL (core.new))
444 case POWER_RS1:
445 case POWER_RSC:
446 case POWER_RS2:
447 arch = bfd_arch_rs6000;
448 mach = bfd_mach_rs6k;
449 break;
450 default:
451 arch = bfd_arch_powerpc;
452 mach = bfd_mach_ppc;
453 break;
455 bfd_default_set_arch_mach (abfd, arch, mach);
458 /* .stack section. */
459 if (!make_bfd_asection (abfd, ".stack",
460 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
461 c_size, c_stackend - c_size, c_stack))
462 return NULL;
464 /* .reg section for all registers. */
465 if (!make_bfd_asection (abfd, ".reg",
466 SEC_HAS_CONTENTS,
467 c_regsize, (bfd_vma) 0, c_regoff))
468 return NULL;
470 /* .ldinfo section.
471 To actually find out how long this section is in this particular
472 core dump would require going down the whole list of struct ld_info's.
473 See if we can just fake it. */
474 if (!make_bfd_asection (abfd, ".ldinfo",
475 SEC_HAS_CONTENTS,
476 c_lsize, (bfd_vma) 0, c_loader))
477 return NULL;
479 #ifndef CORE_VERSION_1
480 /* .data section if present.
481 AIX 3 dumps the complete data section and sets FULL_CORE if the
482 ulimit is large enough, otherwise the data section is omitted.
483 AIX 4 sets FULL_CORE even if the core file is truncated, we have
484 to examine core.c_datasize below to find out the actual size of
485 the .data section. */
486 if (c_flag & FULL_CORE)
488 if (!make_bfd_asection (abfd, ".data",
489 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
490 (bfd_size_type) core.old.c_u.u_dsize,
491 (bfd_vma)
492 CDATA_ADDR (core.old.c_u.u_dsize),
493 c_stack + c_size))
494 return NULL;
496 #endif
498 #ifdef CORE_VERSION_1
499 /* AIX 4 adds data sections from loaded objects to the core file,
500 which can be found by examining ldinfo, and anonymously mmapped
501 regions. */
503 LdInfo ldinfo;
504 bfd_size_type ldi_datasize;
505 file_ptr ldi_core;
506 uint ldi_next;
507 bfd_vma ldi_dataorg;
509 /* Fields from new and old core structures. */
510 bfd_size_type c_datasize, c_vmregions;
511 file_ptr c_data, c_vmm;
513 if (CORE_NEW (core))
515 c_datasize = CNEW_DATASIZE (core.new);
516 c_data = (file_ptr) core.new.c_data;
517 c_vmregions = core.new.c_vmregions;
518 c_vmm = (file_ptr) core.new.c_vmm;
520 else
522 c_datasize = core.old.c_datasize;
523 c_data = (file_ptr) core.old.c_data;
524 c_vmregions = core.old.c_vmregions;
525 c_vmm = (file_ptr) core.old.c_vmm;
528 /* .data section from executable. */
529 if (c_datasize)
531 if (!make_bfd_asection (abfd, ".data",
532 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
533 c_datasize,
534 (bfd_vma) CDATA_ADDR (c_datasize),
535 c_data))
536 return NULL;
539 /* .data sections from loaded objects. */
540 if (proc64)
541 size = (int)((LdInfo *)0)->l64.ldinfo_filename;
542 else
543 size = (int)((LdInfo *)0)->l32.ldinfo_filename;
545 while (1)
547 if (bfd_seek (abfd, c_loader, SEEK_SET) != 0)
548 return NULL;
549 if (bfd_read (&ldinfo, size, 1, abfd) != size)
550 return NULL;
552 if (proc64)
554 ldi_core = ldinfo.l64.ldinfo_core;
555 ldi_datasize = ldinfo.l64.ldinfo_datasize;
556 ldi_dataorg = (bfd_vma) ldinfo.l64.ldinfo_dataorg;
557 ldi_next = ldinfo.l64.ldinfo_next;
559 else
561 ldi_core = ldinfo.l32.ldinfo_core;
562 ldi_datasize = ldinfo.l32.ldinfo_datasize;
563 ldi_dataorg = (bfd_vma)(long) ldinfo.l32.ldinfo_dataorg;
564 ldi_next = ldinfo.l32.ldinfo_next;
567 if (ldi_core)
568 if (!make_bfd_asection (abfd, ".data",
569 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
570 ldi_datasize, ldi_dataorg, ldi_core))
571 return NULL;
573 if (ldi_next == 0)
574 break;
575 c_loader += ldi_next;
578 /* .vmdata sections from anonymously mmapped regions. */
579 if (c_vmregions)
581 bfd_size_type i;
583 if (bfd_seek (abfd, c_vmm, SEEK_SET) != 0)
584 return NULL;
586 for (i = 0; i < c_vmregions; i++)
588 VmInfo vminfo;
589 bfd_size_type vminfo_size;
590 file_ptr vminfo_offset;
591 bfd_vma vminfo_addr;
593 size = CORE_NEW (core) ? sizeof (vminfo.new) : sizeof (vminfo.old);
594 if (bfd_read (&vminfo, size, 1, abfd) != size)
595 return NULL;
597 if (CORE_NEW (core))
599 vminfo_addr = (bfd_vma) vminfo.new.vminfo_addr;
600 vminfo_size = vminfo.new.vminfo_size;
601 vminfo_offset = vminfo.new.vminfo_offset;
603 else
605 vminfo_addr = (bfd_vma)(long) vminfo.old.vminfo_addr;
606 vminfo_size = vminfo.old.vminfo_size;
607 vminfo_offset = vminfo.old.vminfo_offset;
610 if (vminfo_offset)
611 if (!make_bfd_asection (abfd, ".vmdata",
612 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
613 vminfo_size, vminfo_addr,
614 vminfo_offset))
615 return NULL;
619 #endif
621 return abfd->xvec; /* this is garbage for now. */
626 /* return `true' if given core is from the given executable.. */
627 boolean
628 rs6000coff_core_file_matches_executable_p (core_bfd, exec_bfd)
629 bfd *core_bfd;
630 bfd *exec_bfd;
632 CoreHdr core;
633 bfd_size_type size;
634 char *path, *s;
635 size_t alloc;
636 const char *str1, *str2;
637 boolean ret;
638 file_ptr c_loader;
640 if (!read_hdr (core_bfd, &core))
641 return false;
643 if (CORE_NEW (core))
644 c_loader = CNEW_LOADER (core.new);
645 else
646 c_loader = (file_ptr) COLD_LOADER (core.old);
648 if (CORE_NEW (core) && CNEW_PROC64 (core.new))
649 size = (int)((LdInfo *)0)->l64.ldinfo_filename;
650 else
651 size = (int)((LdInfo *)0)->l32.ldinfo_filename;
653 if (bfd_seek (core_bfd, c_loader + size, SEEK_SET) != 0)
654 return false;
656 alloc = 100;
657 path = bfd_malloc (alloc);
658 if (path == NULL)
659 return false;
660 s = path;
662 while (1)
664 if (bfd_read (s, 1, 1, core_bfd) != 1)
666 free (path);
667 return false;
669 if (*s == '\0')
670 break;
671 ++s;
672 if (s == path + alloc)
674 char *n;
676 alloc *= 2;
677 n = bfd_realloc (path, alloc);
678 if (n == NULL)
680 free (path);
681 return false;
683 s = n + (path - s);
684 path = n;
688 str1 = strrchr (path, '/');
689 str2 = strrchr (exec_bfd->filename, '/');
691 /* step over character '/' */
692 str1 = str1 != NULL ? str1 + 1 : path;
693 str2 = str2 != NULL ? str2 + 1 : exec_bfd->filename;
695 if (strcmp (str1, str2) == 0)
696 ret = true;
697 else
698 ret = false;
700 free (path);
702 return ret;
705 char *
706 rs6000coff_core_file_failing_command (abfd)
707 bfd *abfd;
709 CoreHdr *core = core_hdr (abfd);
710 char *com = CORE_NEW (*core) ?
711 CNEW_COMM (core->new) : COLD_COMM (core->old);
713 if (*com)
714 return com;
715 else
716 return 0;
720 rs6000coff_core_file_failing_signal (abfd)
721 bfd *abfd;
723 CoreHdr *core = core_hdr (abfd);
724 return CORE_NEW (*core) ? core->new.c_signo : core->old.c_signo;
727 #endif /* AIX_CORE */