PR ld/11843
[binutils.git] / bfd / aix5ppc-core.c
blob7b0695aafc1459e3a90cd2d75c5b93036284a99d
1 /* IBM RS/6000 "XCOFF" back-end for BFD.
2 Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Written by Tom Rix
5 Contributed by Red Hat Inc.
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. */
24 #include "sysdep.h"
25 #include "bfd.h"
27 const bfd_target *xcoff64_core_p (bfd *);
28 bfd_boolean xcoff64_core_file_matches_executable_p (bfd *, bfd *);
29 char *xcoff64_core_file_failing_command (bfd *);
30 int xcoff64_core_file_failing_signal (bfd *);
32 #ifdef AIX_5_CORE
34 #include "libbfd.h"
36 /* Aix 5.1 system include file. */
38 /* Need to define this macro so struct ld_info64 get included. */
39 #define __LDINFO_PTRACE64__
40 #include <sys/ldr.h>
41 #include <core.h>
43 /* The default architecture and machine for matching core files. */
44 #define DEFAULT_ARCHITECTURE bfd_arch_powerpc
45 #define DEFAULT_MACHINE bfd_mach_ppc_620
47 #define core_hdr(abfd) ((struct core_dumpxx *) abfd->tdata.any)
49 #define CHECK_FILE_OFFSET(s, v) \
50 ((bfd_signed_vma)(v) < 0 || (bfd_signed_vma)(v) > (bfd_signed_vma)(s).st_size)
52 const bfd_target *
53 xcoff64_core_p (bfd *abfd)
55 enum bfd_architecture arch;
56 unsigned long mach;
57 struct core_dumpxx core, *new_core_hdr;
58 struct stat statbuf;
59 asection *sec;
60 struct __ld_info64 ldinfo;
61 bfd_vma ld_offset;
62 bfd_size_type i;
63 struct vm_infox vminfo;
64 const bfd_target *return_value = NULL;
65 flagword flags;
67 /* Get the header. */
68 if (bfd_seek (abfd, 0, SEEK_SET) != 0)
69 goto xcoff64_core_p_error;
71 if (sizeof (struct core_dumpxx)
72 != bfd_bread (&core, sizeof (struct core_dumpxx), abfd))
73 goto xcoff64_core_p_error;
75 if (bfd_stat (abfd, &statbuf) < 0)
76 goto xcoff64_core_p_error;
78 /* Sanity checks
79 c_flag has CORE_VERSION_1, Aix 4+
80 c_entries = 0 for Aix 4.3+
81 IS_PROC64 is a macro defined in procinfo.h, test for 64 bit process.
83 We will still be confused if a Aix 4.3 64 bit core file is
84 copied over to a Aix 5 machine.
86 Check file header offsets
88 See rs6000-core.c for comment on size of core
89 If there isn't enough of a real core file, bail. */
91 if ((CORE_VERSION_1 != (core.c_flag & CORE_VERSION_1))
92 || (0 != core.c_entries)
93 || (! (IS_PROC64 (&core.c_u.U_proc)))
94 || ((CHECK_FILE_OFFSET (statbuf, core.c_fdsinfox)))
95 || ((CHECK_FILE_OFFSET (statbuf, core.c_loader)))
96 || ((CHECK_FILE_OFFSET (statbuf, core.c_loader + core.c_lsize)))
97 || ((CHECK_FILE_OFFSET (statbuf, core.c_thr)))
98 || ((CHECK_FILE_OFFSET (statbuf, core.c_segregion)))
99 || ((CHECK_FILE_OFFSET (statbuf, core.c_stack)))
100 || ((CHECK_FILE_OFFSET (statbuf, core.c_stack + core.c_size)))
101 || ((CHECK_FILE_OFFSET (statbuf, core.c_data)))
102 || ((CHECK_FILE_OFFSET (statbuf, core.c_data + core.c_datasize)))
103 || (! (core.c_flag & UBLOCK_VALID))
104 || (! (core.c_flag & LE_VALID)))
105 goto xcoff64_core_p_error;
107 /* Check for truncated stack or general truncating. */
108 if ((! (core.c_flag & USTACK_VALID))
109 || (core.c_flag & CORE_TRUNC))
111 bfd_set_error (bfd_error_file_truncated);
113 return return_value;
116 new_core_hdr = bfd_zalloc (abfd, sizeof (struct core_dumpxx));
117 if (NULL == new_core_hdr)
118 return return_value;
120 memcpy (new_core_hdr, &core, sizeof (struct core_dumpxx));
121 /* The core_hdr() macro is no longer used here because it would
122 expand to code relying on gcc's cast-as-lvalue extension,
123 which was removed in gcc 4.0. */
124 abfd->tdata.any = new_core_hdr;
126 /* .stack section. */
127 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
128 sec = bfd_make_section_anyway_with_flags (abfd, ".stack", flags);
129 if (NULL == sec)
130 return return_value;
132 sec->size = core.c_size;
133 sec->vma = core.c_stackorg;
134 sec->filepos = core.c_stack;
136 /* .reg section for all registers. */
137 flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
138 sec = bfd_make_section_anyway_with_flags (abfd, ".reg", flags);
139 if (NULL == sec)
140 return return_value;
142 sec->size = sizeof (struct __context64);
143 sec->vma = 0;
144 sec->filepos = 0;
145 sec->contents = (bfd_byte *)&new_core_hdr->c_flt.r64;
147 /* .ldinfo section.
148 To actually find out how long this section is in this particular
149 core dump would require going down the whole list of struct
150 ld_info's. See if we can just fake it. */
151 flags = SEC_HAS_CONTENTS;
152 sec = bfd_make_section_anyway_with_flags (abfd, ".ldinfo", flags);
153 if (NULL == sec)
154 return return_value;
156 sec->size = core.c_lsize;
157 sec->vma = 0;
158 sec->filepos = core.c_loader;
160 /* AIX 4 adds data sections from loaded objects to the core file,
161 which can be found by examining ldinfo, and anonymously mmapped
162 regions. */
164 /* .data section from executable. */
165 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
166 sec = bfd_make_section_anyway_with_flags (abfd, ".data", flags);
167 if (NULL == sec)
168 return return_value;
170 sec->size = core.c_datasize;
171 sec->vma = core.c_dataorg;
172 sec->filepos = core.c_data;
174 /* .data sections from loaded objects. */
175 ld_offset = core.c_loader;
177 while (1)
179 if (bfd_seek (abfd, ld_offset, SEEK_SET) != 0)
180 return return_value;
182 if (sizeof (struct __ld_info64) !=
183 bfd_bread (&ldinfo, sizeof (struct __ld_info64), abfd))
184 return return_value;
186 if (ldinfo.ldinfo_core)
188 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
189 sec = bfd_make_section_anyway_with_flags (abfd, ".data", flags);
190 if (NULL == sec)
191 return return_value;
193 sec->size = ldinfo.ldinfo_datasize;
194 sec->vma = ldinfo.ldinfo_dataorg;
195 sec->filepos = ldinfo.ldinfo_core;
198 if (0 == ldinfo.ldinfo_next)
199 break;
200 ld_offset += ldinfo.ldinfo_next;
203 /* .vmdata sections from anonymously mmapped regions. */
204 if (core.c_vmregions)
206 if (bfd_seek (abfd, core.c_vmm, SEEK_SET) != 0)
207 return return_value;
209 for (i = 0; i < core.c_vmregions; i++)
210 if (sizeof (struct vm_infox) !=
211 bfd_bread (&vminfo, sizeof (struct vm_infox), abfd))
212 return return_value;
214 if (vminfo.vminfo_offset)
216 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
217 sec = bfd_make_section_anyway_with_flags (abfd, ".vmdata", flags);
218 if (NULL == sec)
219 return return_value;
221 sec->size = vminfo.vminfo_size;
222 sec->vma = vminfo.vminfo_addr;
223 sec->filepos = vminfo.vminfo_offset;
227 /* Set the architecture and machine. */
228 arch = DEFAULT_ARCHITECTURE;
229 mach = DEFAULT_MACHINE;
230 bfd_default_set_arch_mach (abfd, arch, mach);
232 return_value = (bfd_target *) abfd->xvec; /* This is garbage for now. */
234 xcoff64_core_p_error:
235 if (bfd_get_error () != bfd_error_system_call)
236 bfd_set_error (bfd_error_wrong_format);
238 return return_value;
241 /* Return `TRUE' if given core is from the given executable. */
243 bfd_boolean
244 xcoff64_core_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd)
246 struct core_dumpxx core;
247 char *path, *s;
248 size_t alloc;
249 const char *str1, *str2;
250 bfd_boolean return_value = FALSE;
252 /* Get the header. */
253 if (bfd_seek (core_bfd, 0, SEEK_SET) != 0)
254 return return_value;
256 if (sizeof (struct core_dumpxx) !=
257 bfd_bread (&core, sizeof (struct core_dumpxx), core_bfd))
258 return return_value;
260 if (bfd_seek (core_bfd, core.c_loader, SEEK_SET) != 0)
261 return return_value;
263 alloc = 100;
264 path = bfd_malloc (alloc);
265 if (path == NULL)
266 return return_value;
268 s = path;
270 while (1)
272 if (bfd_bread (s, 1, core_bfd) != 1)
273 goto xcoff64_core_file_matches_executable_p_end_1;
275 if (*s == '\0')
276 break;
277 ++s;
278 if (s == path + alloc)
280 char *n;
282 alloc *= 2;
283 n = bfd_realloc (path, alloc);
284 if (n == NULL)
285 goto xcoff64_core_file_matches_executable_p_end_1;
287 s = n + (path - s);
288 path = n;
292 str1 = strrchr (path, '/');
293 str2 = strrchr (exec_bfd->filename, '/');
295 /* Step over character '/'. */
296 str1 = str1 != NULL ? str1 + 1 : path;
297 str2 = str2 != NULL ? str2 + 1 : exec_bfd->filename;
299 if (strcmp (str1, str2) == 0)
300 return_value = TRUE;
302 xcoff64_core_file_matches_executable_p_end_1:
303 free (path);
304 return return_value;
307 char *
308 xcoff64_core_file_failing_command (bfd *abfd)
310 struct core_dumpxx *c = core_hdr (abfd);
311 char *return_value = 0;
313 if (NULL != c)
314 return_value = c->c_u.U_proc.pi_comm;
316 return return_value;
320 xcoff64_core_file_failing_signal (bfd *abfd)
322 struct core_dumpxx *c = core_hdr (abfd);
323 int return_value = 0;
325 if (NULL != c)
326 return_value = c->c_signo;
328 return return_value;
331 #else /* AIX_5_CORE */
333 const bfd_target *
334 xcoff64_core_p (bfd *abfd ATTRIBUTE_UNUSED)
336 bfd_set_error (bfd_error_wrong_format);
337 return 0;
340 bfd_boolean
341 xcoff64_core_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd)
343 return generic_core_file_matches_executable_p (core_bfd, exec_bfd);
346 char *
347 xcoff64_core_file_failing_command (bfd *abfd ATTRIBUTE_UNUSED)
349 return 0;
353 xcoff64_core_file_failing_signal (bfd *abfd ATTRIBUTE_UNUSED)
355 return 0;
358 #endif /* AIX_5_CORE */