Automatic date update in version.in
[binutils-gdb.git] / bfd / trad-core.c
blob06b6bdadd879a25f17cf38d258cc5f6614877b8d
1 /* BFD back end for traditional Unix core files (U-area and raw sections)
2 Copyright (C) 1988-2024 Free Software Foundation, Inc.
3 Written by John Gilmore of Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
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 3 of the License, or
10 (at your option) any later version.
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.
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., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "libbfd.h"
25 #include "libaout.h" /* BFD a.out internal data structures */
27 #include <sys/param.h>
28 #include <dirent.h>
29 #include <signal.h>
31 #include <sys/user.h> /* After a.out.h */
33 #ifdef TRAD_HEADER
34 #include TRAD_HEADER
35 #endif
37 #ifndef NBPG
38 # define NBPG getpagesize()
39 #endif
41 struct trad_core_struct
43 asection *data_section;
44 asection *stack_section;
45 asection *reg_section;
46 struct user u;
49 #define core_upage(bfd) (&((bfd)->tdata.trad_core_data->u))
50 #define core_datasec(bfd) ((bfd)->tdata.trad_core_data->data_section)
51 #define core_stacksec(bfd) ((bfd)->tdata.trad_core_data->stack_section)
52 #define core_regsec(bfd) ((bfd)->tdata.trad_core_data->reg_section)
54 /* forward declarations */
56 #define trad_unix_core_file_matches_executable_p generic_core_file_matches_executable_p
57 #define trad_unix_core_file_pid _bfd_nocore_core_file_pid
60 /* Handle 4.2-style (and perhaps also sysV-style) core dump file. */
62 static bfd_cleanup
63 trad_unix_core_file_p (bfd *abfd)
65 int val;
66 struct user u;
67 struct trad_core_struct *rawptr;
68 flagword flags;
70 #ifdef TRAD_CORE_USER_OFFSET
71 /* If defined, this macro is the file position of the user struct. */
72 if (bfd_seek (abfd, TRAD_CORE_USER_OFFSET, SEEK_SET) != 0)
73 return 0;
74 #endif
76 val = bfd_read (&u, sizeof u, abfd);
77 if (val != sizeof u)
79 /* Too small to be a core file */
80 bfd_set_error (bfd_error_wrong_format);
81 return 0;
84 /* Sanity check perhaps??? */
85 if (u.u_dsize > 0x1000000) /* Remember, it's in pages... */
87 bfd_set_error (bfd_error_wrong_format);
88 return 0;
90 if (u.u_ssize > 0x1000000)
92 bfd_set_error (bfd_error_wrong_format);
93 return 0;
96 /* Check that the size claimed is no greater than the file size. */
98 struct stat statbuf;
100 if (bfd_stat (abfd, &statbuf) < 0)
101 return 0;
103 if ((ufile_ptr) NBPG * (UPAGES + u.u_dsize
104 #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE
105 - u.u_tsize
106 #endif
107 + u.u_ssize)
108 > (ufile_ptr) statbuf.st_size)
110 bfd_set_error (bfd_error_wrong_format);
111 return 0;
113 #ifndef TRAD_CORE_ALLOW_ANY_EXTRA_SIZE
114 if (((ufile_ptr) NBPG * (UPAGES + u.u_dsize + u.u_ssize)
115 #ifdef TRAD_CORE_EXTRA_SIZE_ALLOWED
116 /* Some systems write the file too big. */
117 + TRAD_CORE_EXTRA_SIZE_ALLOWED
118 #endif
120 < (ufile_ptr) statbuf.st_size)
122 /* The file is too big. Maybe it's not a core file
123 or we otherwise have bad values for u_dsize and u_ssize). */
124 bfd_set_error (bfd_error_wrong_format);
125 return 0;
127 #endif
130 /* OK, we believe you. You're a core file (sure, sure). */
132 /* Allocate both the upage and the struct core_data at once, so
133 a single free() will free them both. */
134 rawptr = bfd_alloc (abfd, sizeof (*rawptr) + 1);
135 if (rawptr == NULL)
136 return 0;
138 abfd->tdata.trad_core_data = rawptr;
140 rawptr->u = u; /*Copy the uarea into the tdata part of the bfd */
142 /* Ensure core_file_failing_command string is terminated. This is
143 just to stop buffer overflows on fuzzed files. */
144 ((char *) rawptr)[sizeof (*rawptr)] = 0;
146 /* Create the sections. */
148 flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
149 core_stacksec(abfd) = bfd_make_section_anyway_with_flags (abfd, ".stack",
150 flags);
151 if (core_stacksec (abfd) == NULL)
152 goto fail;
153 core_datasec (abfd) = bfd_make_section_anyway_with_flags (abfd, ".data",
154 flags);
155 if (core_datasec (abfd) == NULL)
156 goto fail;
157 core_regsec (abfd) = bfd_make_section_anyway_with_flags (abfd, ".reg",
158 SEC_HAS_CONTENTS);
159 if (core_regsec (abfd) == NULL)
160 goto fail;
162 core_datasec (abfd)->size = NBPG * u.u_dsize
163 #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE
164 - NBPG * u.u_tsize
165 #endif
167 core_stacksec (abfd)->size = NBPG * u.u_ssize;
168 core_regsec (abfd)->size = NBPG * UPAGES; /* Larger than sizeof struct u */
170 /* What a hack... we'd like to steal it from the exec file,
171 since the upage does not seem to provide it. FIXME. */
172 #ifdef HOST_DATA_START_ADDR
173 core_datasec (abfd)->vma = HOST_DATA_START_ADDR;
174 #else
175 core_datasec (abfd)->vma = HOST_TEXT_START_ADDR + (NBPG * u.u_tsize);
176 #endif
178 #ifdef HOST_STACK_START_ADDR
179 core_stacksec (abfd)->vma = HOST_STACK_START_ADDR;
180 #else
181 core_stacksec (abfd)->vma = HOST_STACK_END_ADDR - (NBPG * u.u_ssize);
182 #endif
184 /* This is tricky. As the "register section", we give them the entire
185 upage and stack. u.u_ar0 points to where "register 0" is stored.
186 There are two tricks with this, though. One is that the rest of the
187 registers might be at positive or negative (or both) displacements
188 from *u_ar0. The other is that u_ar0 is sometimes an absolute address
189 in kernel memory, and on other systems it is an offset from the beginning
190 of the `struct user'.
192 As a practical matter, we don't know where the registers actually are,
193 so we have to pass the whole area to GDB. We encode the value of u_ar0
194 by setting the .regs section up so that its virtual memory address
195 0 is at the place pointed to by u_ar0 (by setting the vma of the start
196 of the section to -u_ar0). GDB uses this info to locate the regs,
197 using minor trickery to get around the offset-or-absolute-addr problem. */
198 core_regsec (abfd)->vma = - (bfd_vma) (unsigned long) u.u_ar0;
200 core_datasec (abfd)->filepos = NBPG * UPAGES;
201 core_stacksec (abfd)->filepos = (NBPG * UPAGES) + NBPG * u.u_dsize
202 #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE
203 - NBPG * u.u_tsize
204 #endif
206 core_regsec (abfd)->filepos = 0; /* Register segment is the upage */
208 /* Align to word at least */
209 core_stacksec (abfd)->alignment_power = 2;
210 core_datasec (abfd)->alignment_power = 2;
211 core_regsec (abfd)->alignment_power = 2;
213 return _bfd_no_cleanup;
215 fail:
216 bfd_release (abfd, abfd->tdata.any);
217 abfd->tdata.any = NULL;
218 bfd_section_list_clear (abfd);
219 return NULL;
222 static char *
223 trad_unix_core_file_failing_command (bfd *abfd)
225 #ifndef NO_CORE_COMMAND
226 char *com = abfd->tdata.trad_core_data->u.u_comm;
227 if (*com)
228 return com;
229 else
230 #endif
231 return 0;
234 static int
235 trad_unix_core_file_failing_signal (bfd *ignore_abfd ATTRIBUTE_UNUSED)
237 #ifdef TRAD_UNIX_CORE_FILE_FAILING_SIGNAL
238 return TRAD_UNIX_CORE_FILE_FAILING_SIGNAL(ignore_abfd);
239 #else
240 return -1; /* FIXME, where is it? */
241 #endif
244 /* If somebody calls any byte-swapping routines, shoot them. */
245 static void
246 swap_abort (void)
248 abort (); /* This way doesn't require any declaration for ANSI to fuck up */
251 #define NO_GET ((bfd_vma (*) (const void *)) swap_abort)
252 #define NO_PUT ((void (*) (bfd_vma, void *)) swap_abort)
253 #define NO_GETS ((bfd_signed_vma (*) (const void *)) swap_abort)
254 #define NO_GET64 ((uint64_t (*) (const void *)) swap_abort)
255 #define NO_PUT64 ((void (*) (uint64_t, void *)) swap_abort)
256 #define NO_GETS64 ((int64_t (*) (const void *)) swap_abort)
258 const bfd_target core_trad_vec =
260 "trad-core",
261 bfd_target_unknown_flavour,
262 BFD_ENDIAN_UNKNOWN, /* target byte order */
263 BFD_ENDIAN_UNKNOWN, /* target headers byte order */
264 (HAS_RELOC | EXEC_P | /* object flags */
265 HAS_LINENO | HAS_DEBUG |
266 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
267 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
268 0, /* symbol prefix */
269 ' ', /* ar_pad_char */
270 16, /* ar_max_namelen */
271 0, /* match priority. */
272 TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
273 NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit data */
274 NO_GET, NO_GETS, NO_PUT, /* 32 bit data */
275 NO_GET, NO_GETS, NO_PUT, /* 16 bit data */
276 NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit hdrs */
277 NO_GET, NO_GETS, NO_PUT, /* 32 bit hdrs */
278 NO_GET, NO_GETS, NO_PUT, /* 16 bit hdrs */
280 { /* bfd_check_format */
281 _bfd_dummy_target, /* unknown format */
282 _bfd_dummy_target, /* object file */
283 _bfd_dummy_target, /* archive */
284 trad_unix_core_file_p /* a core file */
286 { /* bfd_set_format */
287 _bfd_bool_bfd_false_error,
288 _bfd_bool_bfd_false_error,
289 _bfd_bool_bfd_false_error,
290 _bfd_bool_bfd_false_error
292 { /* bfd_write_contents */
293 _bfd_bool_bfd_false_error,
294 _bfd_bool_bfd_false_error,
295 _bfd_bool_bfd_false_error,
296 _bfd_bool_bfd_false_error
299 BFD_JUMP_TABLE_GENERIC (_bfd_generic),
300 BFD_JUMP_TABLE_COPY (_bfd_generic),
301 BFD_JUMP_TABLE_CORE (trad_unix),
302 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
303 BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
304 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
305 BFD_JUMP_TABLE_WRITE (_bfd_generic),
306 BFD_JUMP_TABLE_LINK (_bfd_nolink),
307 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
309 NULL,
311 NULL /* backend_data */