ec4fc0cdaf00668956f42c96b2f94c27bbb2402c
[external/binutils.git] / bfd / aix386-core.c
1 /* BFD back-end for AIX on PS/2 core files.
2    This was based on trad-core.c, which was written by John Gilmore of
3         Cygnus Support.
4    Copyright 1988, 1989, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
5    Written by Minh Tran-Le <TRANLE@INTELLICORP.COM>.
6    Converted to back end form by Ian Lance Taylor <ian@cygnus.com>.
7
8 This file is part of BFD, the Binary File Descriptor library.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
23
24 #include "bfd.h"
25 #include "sysdep.h"
26 #include "libbfd.h"
27 #include "obstack.h"
28 #include "coff/i386.h"
29 #include "coff/internal.h"
30 #include "libcoff.h"
31
32 #include <stdio.h>
33 #include <stddef.h>
34 #include <signal.h>
35
36 #include <errno.h>
37
38 #if defined (_AIX) && defined (_I386)
39 #define NOCHECKS                /* this is for coredump.h */
40 #define _h_USER                 /* avoid including user.h from coredump.h */
41 #include <uinfo.h>
42 #include <sys/i386/coredump.h>
43 #endif /* _AIX && _I386 */
44
45 /* maybe this could work on some other i386 but I have not tried it
46  * mtranle@paris - Tue Sep 24 12:49:35 1991
47  */
48
49 #ifndef COR_MAGIC
50 # define COR_MAGIC "core"
51 #endif
52
53 /* need this cast because ptr is really void * */
54 #define core_hdr(bfd) \
55     (((bfd->tdata.trad_core_data))->hdr)
56 #define core_section(bfd,n) \
57     (((bfd)->tdata.trad_core_data)->sections[n])
58 #define core_regsec(bfd) \
59     (((bfd)->tdata.trad_core_data)->reg_section)
60 #define core_reg2sec(bfd) \
61     (((bfd)->tdata.trad_core_data)->reg2_section)
62
63 /* These are stored in the bfd's tdata */
64 struct trad_core_struct {
65   struct corehdr *hdr;          /* core file header */
66   asection *reg_section;
67   asection *reg2_section;
68   asection *sections[MAX_CORE_SEGS];
69 };
70
71 static const bfd_target *
72 aix386_core_file_p (abfd)
73      bfd *abfd;
74 {
75   int i,n;
76   unsigned char longbuf[4];     /* Raw bytes of various header fields */
77   int core_size = sizeof (struct corehdr);
78   struct corehdr *core;
79   struct mergem {
80     struct trad_core_struct coredata;
81     struct corehdr internal_core;
82   } *mergem;
83
84   if (bfd_read ((PTR)longbuf, 1, sizeof (longbuf), abfd) != sizeof (longbuf))
85     {
86       if (bfd_get_error () != bfd_error_system_call)
87         bfd_set_error (bfd_error_wrong_format);
88       return 0;
89     }
90
91   if (strncmp(longbuf,COR_MAGIC,4)) return 0;
92
93   if (bfd_seek (abfd, 0L, false) < 0) return 0;
94
95   mergem = (struct mergem *)bfd_zalloc (abfd, sizeof (struct mergem));
96   if (mergem == NULL)
97     return 0;
98
99   core = &mergem->internal_core;
100
101   if ((bfd_read ((PTR) core, 1, core_size, abfd)) != core_size)
102     {
103       if (bfd_get_error () != bfd_error_system_call)
104         bfd_set_error (bfd_error_wrong_format);
105       bfd_release (abfd, (char *)mergem);
106       return 0;
107     }
108
109   set_tdata (abfd, &mergem->coredata);
110   core_hdr (abfd) = core;
111
112   /* create the sections.  This is raunchy, but bfd_close wants to reclaim
113      them */
114   core_regsec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
115   if (core_regsec (abfd) == NULL)
116     {
117     loser:
118       bfd_release (abfd, (char *)mergem);
119       return 0;
120     }
121   core_reg2sec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
122   if (core_reg2sec (abfd) == NULL)
123     {
124     loser1:
125      bfd_release (abfd, core_regsec (abfd));
126       goto loser;
127     }
128
129   for (i=0, n=0 ; (i < MAX_CORE_SEGS) && (core->cd_segs[i].cs_type) ; i++)
130     {
131       if (core->cd_segs[i].cs_offset == 0)
132         continue;
133       core_section (abfd,n) =
134         (asection *) bfd_zalloc (abfd, sizeof (asection));
135       if (core_section (abfd,n) == NULL)
136         {
137           int j;
138           if (n > 0)
139             {
140               for (j=0; j < n; j++)
141                 bfd_release (abfd, core_section(abfd, j));
142             }
143           bfd_release (abfd, (char *)mergem);
144           goto loser1;
145         }
146
147       switch (core->cd_segs[i].cs_type)
148         {
149         case COR_TYPE_DATA:
150           core_section (abfd, n)->name = ".data";
151           core_section (abfd, n)->flags = (SEC_ALLOC + SEC_LOAD +
152                                            SEC_HAS_CONTENTS);
153           break;
154         case COR_TYPE_STACK:
155           core_section (abfd, n)->name = ".stack";
156           core_section (abfd, n)->flags = (SEC_ALLOC + SEC_LOAD +
157                                            SEC_HAS_CONTENTS);
158           break;
159         case COR_TYPE_LIBDATA:
160           core_section (abfd, n)->name = ".libdata";
161           core_section (abfd, n)->flags = (SEC_ALLOC + SEC_HAS_CONTENTS);
162           break;
163         case COR_TYPE_WRITE:
164           core_section (abfd, n)->name = ".writeable";
165           core_section (abfd, n)->flags = (SEC_ALLOC + SEC_HAS_CONTENTS);
166           break;
167         case COR_TYPE_MSC:
168           core_section (abfd, n)->name = ".misc";
169           core_section (abfd, n)->flags = (SEC_ALLOC + SEC_HAS_CONTENTS);
170           break;
171         default:
172           core_section (abfd, n)->name = ".unknown";
173           core_section (abfd, n)->flags = (SEC_ALLOC + SEC_HAS_CONTENTS);
174           break;
175         }
176       core_section (abfd, n)->_raw_size = core->cd_segs[i].cs_len;
177       core_section (abfd, n)->vma       = core->cd_segs[i].cs_address;
178       core_section (abfd, n)->filepos   = core->cd_segs[i].cs_offset;
179       core_section (abfd, n)->alignment_power = 2;
180       core_section (abfd, n)->next      = NULL;
181       if (n > 0)
182         core_section (abfd, (n-1))->next = core_section (abfd, n);
183
184       abfd->section_count = ++n;
185     }
186
187   core_regsec (abfd)->name = ".reg";
188   core_reg2sec (abfd)->name = ".reg2";
189
190   core_regsec (abfd)->flags = SEC_HAS_CONTENTS;
191   core_reg2sec (abfd)->flags = SEC_HAS_CONTENTS;
192
193   core_regsec (abfd)->_raw_size = sizeof(core->cd_regs);
194   core_reg2sec (abfd)->_raw_size = sizeof(core->cd_fpregs);
195
196   core_regsec (abfd)->vma = -1;
197   core_reg2sec (abfd)->vma = -1;
198
199   /* We'll access the regs afresh in the core file, like any section: */
200   core_regsec (abfd)->filepos = (file_ptr)offsetof(struct corehdr,cd_regs[0]);
201   core_reg2sec (abfd)->filepos = (file_ptr)offsetof(struct corehdr,
202                                                     cd_fpregs);
203
204   /* add the 2 reg fake sections to abfd */
205   abfd->section_count += 2;
206   abfd->sections = core_regsec (abfd);
207   core_regsec (abfd)->next = core_reg2sec (abfd);
208   core_reg2sec (abfd)->next = core_section (abfd, 0);
209
210   return abfd->xvec;
211 }
212
213 static char *
214 aix386_core_file_failing_command (abfd)
215      bfd *abfd;
216 {
217   return core_hdr (abfd)->cd_comm;
218 }
219
220 static int
221 aix386_core_file_failing_signal (abfd)
222      bfd *abfd;
223 {
224   return core_hdr (abfd)->cd_cursig;
225 }
226
227 static boolean
228 aix386_core_file_matches_executable_p (core_bfd, exec_bfd)
229      bfd *core_bfd;
230      bfd *exec_bfd;
231 {
232   return true;                  /* FIXME, We have no way of telling at this
233                                    point */
234 }
235
236 /* If somebody calls any byte-swapping routines, shoot them.  */
237 void
238 swap_abort()
239 {
240   abort(); /* This way doesn't require any declaration for ANSI to fuck up */
241 }
242 #define NO_GET  ((PROTO(bfd_vma, (*), (       const bfd_byte *))) swap_abort )
243 #define NO_GETS ((PROTO(bfd_signed_vma, (*), (const bfd_byte *))) swap_abort )
244 #define NO_PUT  ((PROTO(void,        (*), (bfd_vma, bfd_byte *))) swap_abort )
245
246 const bfd_target aix386_core_vec =
247   {
248     "aix386-core",
249     bfd_target_unknown_flavour,
250     true,                       /* target byte order */
251     true,                       /* target headers byte order */
252   (HAS_RELOC | EXEC_P |         /* object flags */
253    HAS_LINENO | HAS_DEBUG |
254    HAS_SYMS | HAS_LOCALS | WP_TEXT),
255
256   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
257     0,                                          /* leading underscore */
258     ' ',                                        /* ar_pad_char */
259     16,                                         /* ar_max_namelen */
260     NO_GET, NO_GETS, NO_PUT,
261     NO_GET, NO_GETS, NO_PUT,
262     NO_GET, NO_GETS, NO_PUT, /* data */
263     NO_GET, NO_GETS, NO_PUT,
264     NO_GET, NO_GETS, NO_PUT,
265     NO_GET, NO_GETS, NO_PUT, /* hdrs */
266
267     {_bfd_dummy_target, _bfd_dummy_target,
268      _bfd_dummy_target, aix386_core_file_p},
269     {bfd_false, bfd_false,      /* bfd_create_object */
270      bfd_false, bfd_false},
271     {bfd_false, bfd_false,      /* bfd_write_contents */
272      bfd_false, bfd_false},
273
274      BFD_JUMP_TABLE_GENERIC (_bfd_generic),
275      BFD_JUMP_TABLE_COPY (_bfd_generic),
276      BFD_JUMP_TABLE_CORE (aix386),
277      BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
278      BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
279      BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
280      BFD_JUMP_TABLE_WRITE (_bfd_generic),
281      BFD_JUMP_TABLE_LINK (_bfd_nolink),
282      BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
283
284     (PTR) 0
285 };