* Many files: Changes to avoid gcc warnings: Add ATTRIBUTE_UNUSED
[external/binutils.git] / bfd / trad-core.c
1 /* BFD back end for traditional Unix core files (U-area and raw sections)
2    Copyright 1988, 89, 91, 92, 93, 94, 95, 96, 98, 1999
3    Free Software Foundation, Inc.
4    Written by John Gilmore of Cygnus Support.
5
6 This file is part of BFD, the Binary File Descriptor library.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libbfd.h"
25 #include "libaout.h"           /* BFD a.out internal data structures */
26
27 #include <sys/param.h>
28 #include <sys/dir.h>
29 #include <signal.h>
30
31 #include <sys/user.h>           /* After a.out.h  */
32
33 #ifdef TRAD_HEADER
34 #include TRAD_HEADER
35 #endif
36
37   struct trad_core_struct 
38     {
39       asection *data_section;
40       asection *stack_section;
41       asection *reg_section;
42       struct user u;
43     };
44
45 #define core_upage(bfd) (&((bfd)->tdata.trad_core_data->u))
46 #define core_datasec(bfd) ((bfd)->tdata.trad_core_data->data_section)
47 #define core_stacksec(bfd) ((bfd)->tdata.trad_core_data->stack_section)
48 #define core_regsec(bfd) ((bfd)->tdata.trad_core_data->reg_section)
49
50 /* forward declarations */
51
52 const bfd_target *trad_unix_core_file_p PARAMS ((bfd *abfd));
53 char *          trad_unix_core_file_failing_command PARAMS ((bfd *abfd));
54 int             trad_unix_core_file_failing_signal PARAMS ((bfd *abfd));
55 boolean         trad_unix_core_file_matches_executable_p
56                          PARAMS ((bfd *core_bfd, bfd *exec_bfd));
57 static void     swap_abort PARAMS ((void));
58
59 /* Handle 4.2-style (and perhaps also sysV-style) core dump file.  */
60
61 /* ARGSUSED */
62 const bfd_target *
63 trad_unix_core_file_p (abfd)
64      bfd *abfd;
65
66 {
67   int val;
68   struct user u;
69   struct trad_core_struct *rawptr;
70
71 #ifdef TRAD_CORE_USER_OFFSET
72   /* If defined, this macro is the file position of the user struct.  */
73   if (bfd_seek (abfd, TRAD_CORE_USER_OFFSET, SEEK_SET) != 0)
74     return 0;
75 #endif
76     
77   val = bfd_read ((void *)&u, 1, sizeof u, abfd);
78   if (val != sizeof u)
79     {
80       /* Too small to be a core file */
81       bfd_set_error (bfd_error_wrong_format);
82       return 0;
83     }
84
85   /* Sanity check perhaps??? */
86   if (u.u_dsize > 0x1000000)    /* Remember, it's in pages... */
87     {
88       bfd_set_error (bfd_error_wrong_format);
89       return 0;
90     }
91   if (u.u_ssize > 0x1000000)
92     {
93       bfd_set_error (bfd_error_wrong_format);
94       return 0;
95     }
96
97   /* Check that the size claimed is no greater than the file size.  */
98   {
99     FILE *stream = bfd_cache_lookup (abfd);
100     struct stat statbuf;
101     if (stream == NULL)
102       return 0;
103     if (fstat (fileno (stream), &statbuf) < 0)
104       {
105         bfd_set_error (bfd_error_system_call);
106         return 0;
107       }
108     if ((unsigned long) (NBPG * (UPAGES + u.u_dsize
109 #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE
110                                  - u.u_tsize
111 #endif
112                                  + u.u_ssize))
113         > (unsigned long) statbuf.st_size)
114       {
115         bfd_set_error (bfd_error_file_truncated);
116         return 0;
117       }
118 #ifndef TRAD_CORE_ALLOW_ANY_EXTRA_SIZE
119     if ((unsigned long) (NBPG * (UPAGES + u.u_dsize + u.u_ssize)
120 #ifdef TRAD_CORE_EXTRA_SIZE_ALLOWED
121         /* Some systems write the file too big.  */
122                          + TRAD_CORE_EXTRA_SIZE_ALLOWED
123 #endif
124                          )
125         < (unsigned long) statbuf.st_size)
126       {
127         /* The file is too big.  Maybe it's not a core file
128            or we otherwise have bad values for u_dsize and u_ssize).  */
129         bfd_set_error (bfd_error_wrong_format);
130         return 0;
131       }
132 #endif
133   }
134
135   /* OK, we believe you.  You're a core file (sure, sure).  */
136
137   /* Allocate both the upage and the struct core_data at once, so
138      a single free() will free them both.  */
139   rawptr = (struct trad_core_struct *)
140                 bfd_zmalloc (sizeof (struct trad_core_struct));
141   if (rawptr == NULL)
142     return 0;
143   
144   abfd->tdata.trad_core_data = rawptr;
145
146   rawptr->u = u; /*Copy the uarea into the tdata part of the bfd */
147
148   /* Create the sections.  This is raunchy, but bfd_close wants to free
149      them separately.  */
150
151   core_stacksec(abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
152   if (core_stacksec (abfd) == NULL)
153     return NULL;
154   core_datasec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
155   if (core_datasec (abfd) == NULL)
156     return NULL;
157   core_regsec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
158   if (core_regsec (abfd) == NULL)
159     return NULL;
160
161   core_stacksec (abfd)->name = ".stack";
162   core_datasec (abfd)->name = ".data";
163   core_regsec (abfd)->name = ".reg";
164
165   core_stacksec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
166   core_datasec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
167   core_regsec (abfd)->flags = SEC_HAS_CONTENTS;
168
169   core_datasec (abfd)->_raw_size =  NBPG * u.u_dsize
170 #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE
171     - NBPG * u.u_tsize
172 #endif
173       ;
174   core_stacksec (abfd)->_raw_size = NBPG * u.u_ssize;
175   core_regsec (abfd)->_raw_size = NBPG * UPAGES; /* Larger than sizeof struct u */
176
177   /* What a hack... we'd like to steal it from the exec file,
178      since the upage does not seem to provide it.  FIXME.  */
179 #ifdef HOST_DATA_START_ADDR
180   core_datasec (abfd)->vma = HOST_DATA_START_ADDR;
181 #else
182   core_datasec (abfd)->vma = HOST_TEXT_START_ADDR + (NBPG * u.u_tsize);
183 #endif
184
185 #ifdef HOST_STACK_START_ADDR
186   core_stacksec (abfd)->vma = HOST_STACK_START_ADDR;
187 #else
188   core_stacksec (abfd)->vma = HOST_STACK_END_ADDR - (NBPG * u.u_ssize);
189 #endif
190
191   /* This is tricky.  As the "register section", we give them the entire
192      upage and stack.  u.u_ar0 points to where "register 0" is stored.
193      There are two tricks with this, though.  One is that the rest of the
194      registers might be at positive or negative (or both) displacements
195      from *u_ar0.  The other is that u_ar0 is sometimes an absolute address
196      in kernel memory, and on other systems it is an offset from the beginning
197      of the `struct user'.
198      
199      As a practical matter, we don't know where the registers actually are,
200      so we have to pass the whole area to GDB.  We encode the value of u_ar0
201      by setting the .regs section up so that its virtual memory address
202      0 is at the place pointed to by u_ar0 (by setting the vma of the start
203      of the section to -u_ar0).  GDB uses this info to locate the regs,
204      using minor trickery to get around the offset-or-absolute-addr problem. */
205   core_regsec (abfd)->vma = 0 - (bfd_vma) u.u_ar0;
206
207   core_datasec (abfd)->filepos = NBPG * UPAGES;
208   core_stacksec (abfd)->filepos = (NBPG * UPAGES) + NBPG * u.u_dsize
209 #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE
210     - NBPG * u.u_tsize
211 #endif
212       ;
213   core_regsec (abfd)->filepos = 0; /* Register segment is the upage */
214
215   /* Align to word at least */
216   core_stacksec (abfd)->alignment_power = 2;
217   core_datasec (abfd)->alignment_power = 2;
218   core_regsec (abfd)->alignment_power = 2;
219
220   abfd->sections = core_stacksec (abfd);
221   core_stacksec (abfd)->next = core_datasec (abfd);
222   core_datasec (abfd)->next = core_regsec (abfd);
223   abfd->section_count = 3;
224
225   return abfd->xvec;
226 }
227
228 char *
229 trad_unix_core_file_failing_command (abfd)
230      bfd *abfd;
231 {
232 #ifndef NO_CORE_COMMAND
233   char *com = abfd->tdata.trad_core_data->u.u_comm;
234   if (*com)
235     return com;
236   else
237 #endif
238     return 0;
239 }
240
241 /* ARGSUSED */
242 int
243 trad_unix_core_file_failing_signal (ignore_abfd)
244      bfd *ignore_abfd ATTRIBUTE_UNUSED;
245 {
246 #ifdef TRAD_UNIX_CORE_FILE_FAILING_SIGNAL
247   return TRAD_UNIX_CORE_FILE_FAILING_SIGNAL(ignore_abfd);
248 #else
249   return -1;            /* FIXME, where is it? */
250 #endif
251 }
252
253 /* ARGSUSED */
254 boolean
255 trad_unix_core_file_matches_executable_p  (core_bfd, exec_bfd)
256      bfd *core_bfd ATTRIBUTE_UNUSED;
257      bfd *exec_bfd ATTRIBUTE_UNUSED;
258 {
259   return true;          /* FIXME, We have no way of telling at this point */
260 }
261 \f
262 /* If somebody calls any byte-swapping routines, shoot them.  */
263 static void
264 swap_abort()
265 {
266   abort(); /* This way doesn't require any declaration for ANSI to fuck up */
267 }
268 #define NO_GET  ((bfd_vma (*) PARAMS ((   const bfd_byte *))) swap_abort )
269 #define NO_PUT  ((void    (*) PARAMS ((bfd_vma, bfd_byte *))) swap_abort )
270 #define NO_SIGNED_GET \
271   ((bfd_signed_vma (*) PARAMS ((const bfd_byte *))) swap_abort )
272
273 const bfd_target trad_core_vec =
274   {
275     "trad-core",
276     bfd_target_unknown_flavour,
277     BFD_ENDIAN_UNKNOWN,         /* target byte order */
278     BFD_ENDIAN_UNKNOWN,         /* target headers byte order */
279     (HAS_RELOC | EXEC_P |       /* object flags */
280      HAS_LINENO | HAS_DEBUG |
281      HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
282     (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
283     0,                                                     /* symbol prefix */
284     ' ',                                                   /* ar_pad_char */
285     16,                                                    /* ar_max_namelen */
286     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 64 bit data */
287     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 32 bit data */
288     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 16 bit data */
289     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 64 bit hdrs */
290     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 32 bit hdrs */
291     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 16 bit hdrs */
292
293     {                           /* bfd_check_format */
294      _bfd_dummy_target,         /* unknown format */
295      _bfd_dummy_target,         /* object file */
296      _bfd_dummy_target,         /* archive */
297      trad_unix_core_file_p      /* a core file */
298     },
299     {                           /* bfd_set_format */
300      bfd_false, bfd_false,
301      bfd_false, bfd_false
302     },
303     {                           /* bfd_write_contents */
304      bfd_false, bfd_false,
305      bfd_false, bfd_false
306     },
307     
308        BFD_JUMP_TABLE_GENERIC (_bfd_generic),
309        BFD_JUMP_TABLE_COPY (_bfd_generic),
310        BFD_JUMP_TABLE_CORE (trad_unix),
311        BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
312        BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
313        BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
314        BFD_JUMP_TABLE_WRITE (_bfd_generic),
315        BFD_JUMP_TABLE_LINK (_bfd_nolink),
316        BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
317
318     (PTR) 0                     /* backend_data */
319 };