Extensive changes to move the bulk of the linker into BFD so that
[external/binutils.git] / bfd / trad-core.c
1 /* BFD back end for traditional Unix core files (U-area and raw sections)
2    Copyright 1988, 1989, 1991, 1992, 1993 Free Software Foundation, Inc.
3    Written by John Gilmore of Cygnus Support.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
20
21 /* To use this file on a particular host, configure the host with these
22    parameters in the config/h-HOST file:
23
24         HDEFINES=-DTRAD_CORE
25         HDEPFILES=trad-core.o
26
27  */
28
29 #include "bfd.h"
30 #include "sysdep.h"
31 #include "libbfd.h"
32 #include "libaout.h"           /* BFD a.out internal data structures */
33
34 #include <stdio.h>
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/dir.h>
38 #include <signal.h>
39
40 #include <sys/user.h>           /* After a.out.h  */
41 #if 0
42 /* file.h is included by std-host.h.  So we better not try to include it
43    twice; on some systems (dpx2) it is not protected against multiple
44    inclusion.  I have checked that all the hosts which use this file
45    include sys/file.h in the hosts file.  */
46 #include <sys/file.h>
47 #endif
48
49 #include <errno.h>
50
51   struct trad_core_struct 
52     {
53       asection *data_section;
54       asection *stack_section;
55       asection *reg_section;
56       struct user u;
57     } *rawptr;
58
59 #define core_upage(bfd) (&((bfd)->tdata.trad_core_data->u))
60 #define core_datasec(bfd) ((bfd)->tdata.trad_core_data->data_section)
61 #define core_stacksec(bfd) ((bfd)->tdata.trad_core_data->stack_section)
62 #define core_regsec(bfd) ((bfd)->tdata.trad_core_data->reg_section)
63
64 /* forward declarations */
65
66 bfd_target *    trad_unix_core_file_p PARAMS ((bfd *abfd));
67 char *          trad_unix_core_file_failing_command PARAMS ((bfd *abfd));
68 int             trad_unix_core_file_failing_signal PARAMS ((bfd *abfd));
69 boolean         trad_unix_core_file_matches_executable_p
70                          PARAMS ((bfd *core_bfd, bfd *exec_bfd));
71
72 /* Handle 4.2-style (and perhaps also sysV-style) core dump file.  */
73
74 /* ARGSUSED */
75 bfd_target *
76 trad_unix_core_file_p (abfd)
77      bfd *abfd;
78
79 {
80   int val;
81   struct user u;
82
83 #ifdef TRAD_CORE_USER_OFFSET
84   /* If defined, this macro is the file position of the user struct.  */
85   if (bfd_seek (abfd, TRAD_CORE_USER_OFFSET, SEEK_SET) == 0)
86     return 0;
87 #endif
88     
89   val = bfd_read ((void *)&u, 1, sizeof u, abfd);
90   if (val != sizeof u)
91     {
92       /* Too small to be a core file */
93       bfd_error = wrong_format;
94       return 0;
95     }
96
97   /* Sanity check perhaps??? */
98   if (u.u_dsize > 0x1000000)    /* Remember, it's in pages... */
99     {
100       bfd_error = wrong_format;
101       return 0;
102     }
103   if (u.u_ssize > 0x1000000)
104     {
105       bfd_error = wrong_format;
106       return 0;
107     }
108
109   /* Check that the size claimed is no greater than the file size.  */
110   {
111     FILE *stream = bfd_cache_lookup (abfd);
112     struct stat statbuf;
113     if (stream == NULL)
114       return 0;
115     if (fstat (fileno (stream), &statbuf) < 0)
116       {
117         bfd_error = system_call_error;
118         return 0;
119       }
120     if (NBPG * (UPAGES + u.u_dsize + u.u_ssize) > statbuf.st_size)
121       {
122         bfd_error = file_truncated;
123         return 0;
124       }
125 #ifndef TRAD_CORE_ALLOW_ANY_EXTRA_SIZE
126     if (NBPG * (UPAGES + u.u_dsize + u.u_ssize)
127 #ifdef TRAD_CORE_EXTRA_SIZE_ALLOWED
128         /* Some systems write the file too big.  */
129         + TRAD_CORE_EXTRA_SIZE_ALLOWED
130 #endif
131         < statbuf.st_size)
132       {
133         /* The file is too big.  Maybe it's not a core file
134            or we otherwise have bad values for u_dsize and u_ssize).  */
135         bfd_error = wrong_format;
136         return 0;
137       }
138 #endif
139   }
140
141   /* OK, we believe you.  You're a core file (sure, sure).  */
142
143   /* Allocate both the upage and the struct core_data at once, so
144      a single free() will free them both.  */
145   rawptr = (struct trad_core_struct *)
146                 bfd_zalloc (abfd, sizeof (struct trad_core_struct));
147   if (rawptr == NULL) {
148     bfd_error = no_memory;
149     return 0;
150   }
151   
152   abfd->tdata.trad_core_data = rawptr;
153
154   rawptr->u = u; /*Copy the uarea into the tdata part of the bfd */
155
156   /* Create the sections.  This is raunchy, but bfd_close wants to free
157      them separately.  */
158
159   core_stacksec(abfd) = (asection *) zalloc (sizeof (asection));
160   if (core_stacksec (abfd) == NULL) {
161   loser:
162     bfd_error = no_memory;
163     free ((void *)rawptr);
164     return 0;
165   }
166   core_datasec (abfd) = (asection *) zalloc (sizeof (asection));
167   if (core_datasec (abfd) == NULL) {
168   loser1:
169     free ((void *)core_stacksec (abfd));
170     goto loser;
171   }
172   core_regsec (abfd) = (asection *) zalloc (sizeof (asection));
173   if (core_regsec (abfd) == NULL) {
174     free ((void *)core_datasec (abfd));
175     goto loser1;
176   }
177
178   core_stacksec (abfd)->name = ".stack";
179   core_datasec (abfd)->name = ".data";
180   core_regsec (abfd)->name = ".reg";
181
182   core_stacksec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
183   core_datasec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
184   core_regsec (abfd)->flags = SEC_ALLOC + SEC_HAS_CONTENTS;
185
186   core_datasec (abfd)->_raw_size =  NBPG * u.u_dsize;
187   core_stacksec (abfd)->_raw_size = NBPG * u.u_ssize;
188   core_regsec (abfd)->_raw_size = NBPG * UPAGES; /* Larger than sizeof struct u */
189
190   /* What a hack... we'd like to steal it from the exec file,
191      since the upage does not seem to provide it.  FIXME.  */
192 #ifdef HOST_DATA_START_ADDR
193   core_datasec (abfd)->vma = HOST_DATA_START_ADDR;
194 #else
195   core_datasec (abfd)->vma = HOST_TEXT_START_ADDR + (NBPG * u.u_tsize);
196 #endif
197
198 #ifdef HOST_STACK_START_ADDR
199   core_stacksec (abfd)->vma = HOST_STACK_START_ADDR;
200 #else
201   core_stacksec (abfd)->vma = HOST_STACK_END_ADDR - (NBPG * u.u_ssize);
202 #endif
203
204   /* This is tricky.  As the "register section", we give them the entire
205      upage and stack.  u.u_ar0 points to where "register 0" is stored.
206      There are two tricks with this, though.  One is that the rest of the
207      registers might be at positive or negative (or both) displacements
208      from *u_ar0.  The other is that u_ar0 is sometimes an absolute address
209      in kernel memory, and on other systems it is an offset from the beginning
210      of the `struct user'.
211      
212      As a practical matter, we don't know where the registers actually are,
213      so we have to pass the whole area to GDB.  We encode the value of u_ar0
214      by setting the .regs section up so that its virtual memory address
215      0 is at the place pointed to by u_ar0 (by setting the vma of the start
216      of the section to -u_ar0).  GDB uses this info to locate the regs,
217      using minor trickery to get around the offset-or-absolute-addr problem. */
218   core_regsec (abfd)->vma = 0 - (int) u.u_ar0;
219
220   core_datasec (abfd)->filepos = NBPG * UPAGES;
221 #ifdef TRAD_CORE_STACK_FILEPOS
222   core_stacksec (abfd)->filepos = TRAD_CORE_STACK_FILEPOS;
223 #else
224   core_stacksec (abfd)->filepos = (NBPG * UPAGES) + NBPG * u.u_dsize;
225 #endif
226   core_regsec (abfd)->filepos = 0; /* Register segment is the upage */
227
228   /* Align to word at least */
229   core_stacksec (abfd)->alignment_power = 2;
230   core_datasec (abfd)->alignment_power = 2;
231   core_regsec (abfd)->alignment_power = 2;
232
233   abfd->sections = core_stacksec (abfd);
234   core_stacksec (abfd)->next = core_datasec (abfd);
235   core_datasec (abfd)->next = core_regsec (abfd);
236   abfd->section_count = 3;
237
238   return abfd->xvec;
239 }
240
241 char *
242 trad_unix_core_file_failing_command (abfd)
243      bfd *abfd;
244 {
245 #ifndef NO_CORE_COMMAND
246   char *com = abfd->tdata.trad_core_data->u.u_comm;
247   if (*com)
248     return com;
249   else
250 #endif
251     return 0;
252 }
253
254 /* ARGSUSED */
255 int
256 trad_unix_core_file_failing_signal (ignore_abfd)
257      bfd *ignore_abfd;
258 {
259 #ifdef TRAD_UNIX_CORE_FILE_FAILING_SIGNAL
260   return TRAD_UNIX_CORE_FILE_FAILING_SIGNAL(ignore_abfd);
261 #else
262   return -1;            /* FIXME, where is it? */
263 #endif
264 }
265
266 /* ARGSUSED */
267 boolean
268 trad_unix_core_file_matches_executable_p  (core_bfd, exec_bfd)
269      bfd *core_bfd, *exec_bfd;
270 {
271   return true;          /* FIXME, We have no way of telling at this point */
272 }
273 \f
274 /* No archive file support via this BFD */
275 #define trad_unix_openr_next_archived_file      bfd_generic_openr_next_archived_file
276 #define trad_unix_generic_stat_arch_elt         bfd_generic_stat_arch_elt
277 #define trad_unix_slurp_armap                   bfd_false
278 #define trad_unix_slurp_extended_name_table     bfd_true
279 #define trad_unix_write_armap                   (boolean (*) PARAMS     \
280     ((bfd *arch, unsigned int elength, struct orl *map, \
281       unsigned int orl_count, int stridx))) bfd_false
282 #define trad_unix_truncate_arname               bfd_dont_truncate_arname
283 #define aout_32_openr_next_archived_file        bfd_generic_openr_next_archived_file
284
285 #define trad_unix_close_and_cleanup             bfd_generic_close_and_cleanup
286 #define trad_unix_set_section_contents          (boolean (*) PARAMS     \
287         ((bfd *abfd, asection *section, PTR data, file_ptr offset,      \
288         bfd_size_type count))) bfd_false
289 #define trad_unix_get_section_contents          bfd_generic_get_section_contents
290 #define trad_unix_new_section_hook              (boolean (*) PARAMS     \
291         ((bfd *, sec_ptr))) bfd_true
292 #define trad_unix_get_symtab_upper_bound        bfd_0u
293 #define trad_unix_get_symtab                    (unsigned int (*) PARAMS \
294         ((bfd *, struct symbol_cache_entry **))) bfd_0u
295 #define trad_unix_get_reloc_upper_bound         (unsigned int (*) PARAMS \
296         ((bfd *, sec_ptr))) bfd_0u
297 #define trad_unix_canonicalize_reloc            (unsigned int (*) PARAMS \
298         ((bfd *, sec_ptr, arelent **, struct symbol_cache_entry**))) bfd_0u
299 #define trad_unix_make_empty_symbol             (struct symbol_cache_entry * \
300         (*) PARAMS ((bfd *))) bfd_false
301 #define trad_unix_print_symbol                  (void (*) PARAMS        \
302         ((bfd *, PTR, struct symbol_cache_entry  *,                     \
303         bfd_print_symbol_type))) bfd_false
304 #define trad_unix_get_symbol_info               (void (*) PARAMS        \
305         ((bfd *, struct symbol_cache_entry  *,                  \
306         symbol_info *))) bfd_false
307 #define trad_unix_get_lineno                    (alent * (*) PARAMS     \
308         ((bfd *, struct symbol_cache_entry *))) bfd_nullvoidptr
309 #define trad_unix_set_arch_mach                 (boolean (*) PARAMS     \
310         ((bfd *, enum bfd_architecture, unsigned long))) bfd_false
311 #define trad_unix_find_nearest_line             (boolean (*) PARAMS     \
312         ((bfd *abfd, struct sec  *section,                              \
313          struct symbol_cache_entry  **symbols,bfd_vma offset,           \
314          CONST char **file, CONST char **func, unsigned int *line))) bfd_false
315 #define trad_unix_sizeof_headers                (int (*) PARAMS \
316         ((bfd *, boolean))) bfd_0
317
318 #define trad_unix_bfd_debug_info_start          bfd_void
319 #define trad_unix_bfd_debug_info_end            bfd_void
320 #define trad_unix_bfd_debug_info_accumulate     (void (*) PARAMS        \
321         ((bfd *, struct sec *))) bfd_void
322 #define trad_unix_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
323 #define trad_unix_bfd_relax_section             bfd_generic_relax_section
324 #define trad_unix_bfd_reloc_type_lookup \
325   ((CONST struct reloc_howto_struct *(*) PARAMS ((bfd *, bfd_reloc_code_real_type))) bfd_nullvoidptr)
326 #define trad_unix_bfd_make_debug_symbol \
327   ((asymbol *(*) PARAMS ((bfd *, void *, unsigned long))) bfd_nullvoidptr)
328 #define trad_unix_bfd_link_hash_table_create \
329   ((struct bfd_link_hash_table *(*) PARAMS ((bfd *))) bfd_nullvoidptr)
330 #define trad_unix_bfd_link_add_symbols \
331   ((boolean (*) PARAMS ((bfd *, struct bfd_link_info *))) bfd_false)
332 #define trad_unix_bfd_final_link \
333   ((boolean (*) PARAMS ((bfd *, struct bfd_link_info *))) bfd_false)
334
335 /* If somebody calls any byte-swapping routines, shoot them.  */
336 void
337 swap_abort()
338 {
339   abort(); /* This way doesn't require any declaration for ANSI to fuck up */
340 }
341 #define NO_GET  ((bfd_vma (*) PARAMS ((         bfd_byte *))) swap_abort )
342 #define NO_PUT  ((void    (*) PARAMS ((bfd_vma, bfd_byte *))) swap_abort )
343 #define NO_SIGNED_GET ((bfd_signed_vma (*) PARAMS ((bfd_byte *))) swap_abort )
344
345 bfd_target trad_core_vec =
346   {
347     "trad-core",
348     bfd_target_unknown_flavour,
349     true,                       /* target byte order */
350     true,                       /* target headers byte order */
351     (HAS_RELOC | EXEC_P |       /* object flags */
352      HAS_LINENO | HAS_DEBUG |
353      HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
354     (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
355     0,                                                     /* symbol prefix */
356     ' ',                                                   /* ar_pad_char */
357     16,                                                    /* ar_max_namelen */
358     3,                                                     /* minimum alignment power */
359     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 64 bit data */
360     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 32 bit data */
361     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 16 bit data */
362     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 64 bit hdrs */
363     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 32 bit hdrs */
364     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 16 bit hdrs */
365
366     {                           /* bfd_check_format */
367      _bfd_dummy_target,         /* unknown format */
368      _bfd_dummy_target,         /* object file */
369      _bfd_dummy_target,         /* archive */
370      trad_unix_core_file_p      /* a core file */
371     },
372     {                           /* bfd_set_format */
373      bfd_false, bfd_false,
374      bfd_false, bfd_false
375     },
376     {                           /* bfd_write_contents */
377      bfd_false, bfd_false,
378      bfd_false, bfd_false
379     },
380     
381     JUMP_TABLE(trad_unix),
382     (PTR) 0                     /* backend_data */
383 };