]>
Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* IBM RS/6000 native-dependent code for GDB, the GNU debugger. |
2 | Copyright 1986, 1987, 1989, 1991, 1992, 1994, 1995, 1996, 1997, 1998 | |
c5aa993b | 3 | Free Software Foundation, Inc. |
c906108c | 4 | |
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
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. | |
c906108c | 11 | |
c5aa993b JM |
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. | |
c906108c | 16 | |
c5aa993b JM |
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., 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
21 | |
22 | #include "defs.h" | |
23 | #include "inferior.h" | |
24 | #include "target.h" | |
25 | #include "gdbcore.h" | |
26 | #include "xcoffsolib.h" | |
27 | #include "symfile.h" | |
28 | #include "objfiles.h" | |
c5aa993b | 29 | #include "libbfd.h" /* For bfd_cache_lookup (FIXME) */ |
c906108c SS |
30 | #include "bfd.h" |
31 | #include "gdb-stabs.h" | |
32 | ||
33 | #include <sys/ptrace.h> | |
34 | #include <sys/reg.h> | |
35 | ||
36 | #include <sys/param.h> | |
37 | #include <sys/dir.h> | |
38 | #include <sys/user.h> | |
39 | #include <signal.h> | |
40 | #include <sys/ioctl.h> | |
41 | #include <fcntl.h> | |
42 | ||
43 | #include <a.out.h> | |
44 | #include <sys/file.h> | |
45 | #include "gdb_stat.h" | |
46 | #include <sys/core.h> | |
47 | #include <sys/ldr.h> | |
48 | ||
49 | extern int errno; | |
50 | ||
c5aa993b | 51 | extern struct vmap *map_vmap PARAMS ((bfd * bf, bfd * arch)); |
c906108c SS |
52 | |
53 | extern struct target_ops exec_ops; | |
54 | ||
55 | static void | |
56 | vmap_exec PARAMS ((void)); | |
57 | ||
58 | static void | |
59 | vmap_ldinfo PARAMS ((struct ld_info *)); | |
60 | ||
61 | static struct vmap * | |
c5aa993b | 62 | add_vmap PARAMS ((struct ld_info *)); |
c906108c SS |
63 | |
64 | static int | |
65 | objfile_symbol_add PARAMS ((char *)); | |
66 | ||
67 | static void | |
68 | vmap_symtab PARAMS ((struct vmap *)); | |
69 | ||
70 | static void | |
71 | fetch_core_registers PARAMS ((char *, unsigned int, int, CORE_ADDR)); | |
72 | ||
73 | static void | |
74 | exec_one_dummy_insn PARAMS ((void)); | |
75 | ||
76 | extern void | |
77 | fixup_breakpoints PARAMS ((CORE_ADDR low, CORE_ADDR high, CORE_ADDR delta)); | |
78 | ||
79 | /* Conversion from gdb-to-system special purpose register numbers.. */ | |
80 | ||
c5aa993b JM |
81 | static int special_regs[] = |
82 | { | |
83 | IAR, /* PC_REGNUM */ | |
84 | MSR, /* PS_REGNUM */ | |
85 | CR, /* CR_REGNUM */ | |
86 | LR, /* LR_REGNUM */ | |
87 | CTR, /* CTR_REGNUM */ | |
c906108c | 88 | XER, /* XER_REGNUM */ |
c5aa993b | 89 | MQ /* MQ_REGNUM */ |
c906108c SS |
90 | }; |
91 | ||
92 | void | |
93 | fetch_inferior_registers (regno) | |
c5aa993b | 94 | int regno; |
c906108c SS |
95 | { |
96 | int ii; | |
c906108c | 97 | |
c5aa993b JM |
98 | if (regno < 0) |
99 | { /* for all registers */ | |
c906108c | 100 | |
c5aa993b | 101 | /* read 32 general purpose registers. */ |
c906108c | 102 | |
c5aa993b JM |
103 | for (ii = 0; ii < 32; ++ii) |
104 | *(int *) ®isters[REGISTER_BYTE (ii)] = | |
105 | ptrace (PT_READ_GPR, inferior_pid, (PTRACE_ARG3_TYPE) ii, 0, 0); | |
c906108c | 106 | |
c5aa993b | 107 | /* read general purpose floating point registers. */ |
c906108c | 108 | |
c5aa993b JM |
109 | for (ii = 0; ii < 32; ++ii) |
110 | ptrace (PT_READ_FPR, inferior_pid, | |
111 | (PTRACE_ARG3_TYPE) & registers[REGISTER_BYTE (FP0_REGNUM + ii)], | |
112 | FPR0 + ii, 0); | |
c906108c | 113 | |
c5aa993b JM |
114 | /* read special registers. */ |
115 | for (ii = 0; ii <= LAST_UISA_SP_REGNUM - FIRST_UISA_SP_REGNUM; ++ii) | |
116 | *(int *) ®isters[REGISTER_BYTE (FIRST_UISA_SP_REGNUM + ii)] = | |
117 | ptrace (PT_READ_GPR, inferior_pid, (PTRACE_ARG3_TYPE) special_regs[ii], | |
118 | 0, 0); | |
c906108c | 119 | |
c5aa993b JM |
120 | registers_fetched (); |
121 | return; | |
122 | } | |
c906108c SS |
123 | |
124 | /* else an individual register is addressed. */ | |
125 | ||
c5aa993b JM |
126 | else if (regno < FP0_REGNUM) |
127 | { /* a GPR */ | |
128 | *(int *) ®isters[REGISTER_BYTE (regno)] = | |
c906108c | 129 | ptrace (PT_READ_GPR, inferior_pid, (PTRACE_ARG3_TYPE) regno, 0, 0); |
c5aa993b JM |
130 | } |
131 | else if (regno <= FPLAST_REGNUM) | |
132 | { /* a FPR */ | |
133 | ptrace (PT_READ_FPR, inferior_pid, | |
134 | (PTRACE_ARG3_TYPE) & registers[REGISTER_BYTE (regno)], | |
135 | (regno - FP0_REGNUM + FPR0), 0); | |
136 | } | |
137 | else if (regno <= LAST_UISA_SP_REGNUM) | |
138 | { /* a special register */ | |
139 | *(int *) ®isters[REGISTER_BYTE (regno)] = | |
c906108c | 140 | ptrace (PT_READ_GPR, inferior_pid, |
c5aa993b | 141 | (PTRACE_ARG3_TYPE) special_regs[regno - FIRST_UISA_SP_REGNUM], |
c906108c | 142 | 0, 0); |
c5aa993b | 143 | } |
c906108c | 144 | else |
c5aa993b | 145 | fprintf_unfiltered (gdb_stderr, |
c906108c SS |
146 | "gdb error: register no %d not implemented.\n", |
147 | regno); | |
148 | ||
c5aa993b | 149 | register_valid[regno] = 1; |
c906108c SS |
150 | } |
151 | ||
152 | /* Store our register values back into the inferior. | |
153 | If REGNO is -1, do this for all registers. | |
154 | Otherwise, REGNO specifies which register (so we can save time). */ | |
155 | ||
156 | void | |
157 | store_inferior_registers (regno) | |
158 | int regno; | |
159 | { | |
c906108c SS |
160 | |
161 | errno = 0; | |
162 | ||
163 | if (regno == -1) | |
c5aa993b | 164 | { /* for all registers.. */ |
c906108c SS |
165 | int ii; |
166 | ||
c5aa993b JM |
167 | /* execute one dummy instruction (which is a breakpoint) in inferior |
168 | process. So give kernel a chance to do internal house keeping. | |
169 | Otherwise the following ptrace(2) calls will mess up user stack | |
170 | since kernel will get confused about the bottom of the stack (%sp) */ | |
c906108c | 171 | |
c5aa993b | 172 | exec_one_dummy_insn (); |
c906108c SS |
173 | |
174 | /* write general purpose registers first! */ | |
c5aa993b | 175 | for (ii = GPR0; ii <= GPR31; ++ii) |
c906108c SS |
176 | { |
177 | ptrace (PT_WRITE_GPR, inferior_pid, (PTRACE_ARG3_TYPE) ii, | |
c5aa993b | 178 | *(int *) ®isters[REGISTER_BYTE (ii)], 0); |
c906108c | 179 | if (errno) |
c5aa993b | 180 | { |
c906108c SS |
181 | perror ("ptrace write_gpr"); |
182 | errno = 0; | |
183 | } | |
184 | } | |
185 | ||
186 | /* write floating point registers now. */ | |
c5aa993b | 187 | for (ii = 0; ii < 32; ++ii) |
c906108c | 188 | { |
c5aa993b JM |
189 | ptrace (PT_WRITE_FPR, inferior_pid, |
190 | (PTRACE_ARG3_TYPE) & registers[REGISTER_BYTE (FP0_REGNUM + ii)], | |
191 | FPR0 + ii, 0); | |
c906108c SS |
192 | if (errno) |
193 | { | |
194 | perror ("ptrace write_fpr"); | |
195 | errno = 0; | |
196 | } | |
197 | } | |
198 | ||
199 | /* write special registers. */ | |
c5aa993b | 200 | for (ii = 0; ii <= LAST_UISA_SP_REGNUM - FIRST_UISA_SP_REGNUM; ++ii) |
c906108c SS |
201 | { |
202 | ptrace (PT_WRITE_GPR, inferior_pid, | |
203 | (PTRACE_ARG3_TYPE) special_regs[ii], | |
c5aa993b | 204 | *(int *) ®isters[REGISTER_BYTE (FIRST_UISA_SP_REGNUM + ii)], |
c906108c SS |
205 | 0); |
206 | if (errno) | |
207 | { | |
208 | perror ("ptrace write_gpr"); | |
209 | errno = 0; | |
210 | } | |
211 | } | |
212 | } | |
213 | ||
214 | /* else, a specific register number is given... */ | |
215 | ||
c5aa993b | 216 | else if (regno < FP0_REGNUM) /* a GPR */ |
c906108c | 217 | { |
f6077098 KB |
218 | if (regno == SP_REGNUM) |
219 | exec_one_dummy_insn (); | |
c906108c | 220 | ptrace (PT_WRITE_GPR, inferior_pid, (PTRACE_ARG3_TYPE) regno, |
c5aa993b | 221 | *(int *) ®isters[REGISTER_BYTE (regno)], 0); |
c906108c SS |
222 | } |
223 | ||
c5aa993b | 224 | else if (regno <= FPLAST_REGNUM) /* a FPR */ |
c906108c | 225 | { |
c5aa993b JM |
226 | ptrace (PT_WRITE_FPR, inferior_pid, |
227 | (PTRACE_ARG3_TYPE) & registers[REGISTER_BYTE (regno)], | |
c906108c SS |
228 | regno - FP0_REGNUM + FPR0, 0); |
229 | } | |
230 | ||
c5aa993b | 231 | else if (regno <= LAST_UISA_SP_REGNUM) /* a special register */ |
c906108c SS |
232 | { |
233 | ptrace (PT_WRITE_GPR, inferior_pid, | |
c5aa993b JM |
234 | (PTRACE_ARG3_TYPE) special_regs[regno - FIRST_UISA_SP_REGNUM], |
235 | *(int *) ®isters[REGISTER_BYTE (regno)], 0); | |
c906108c SS |
236 | } |
237 | ||
f6077098 KB |
238 | else if (regno < NUM_REGS) |
239 | { | |
240 | /* Ignore it. */ | |
241 | } | |
242 | ||
c906108c SS |
243 | else |
244 | fprintf_unfiltered (gdb_stderr, | |
245 | "Gdb error: register no %d not implemented.\n", | |
246 | regno); | |
247 | ||
248 | if (errno) | |
249 | { | |
250 | perror ("ptrace write"); | |
251 | errno = 0; | |
252 | } | |
253 | } | |
254 | ||
255 | /* Execute one dummy breakpoint instruction. This way we give the kernel | |
256 | a chance to do some housekeeping and update inferior's internal data, | |
257 | including u_area. */ | |
258 | ||
259 | static void | |
260 | exec_one_dummy_insn () | |
261 | { | |
262 | #define DUMMY_INSN_ADDR (TEXT_SEGMENT_BASE)+0x200 | |
263 | ||
c5aa993b | 264 | char shadow_contents[BREAKPOINT_MAX]; /* Stash old bkpt addr contents */ |
c906108c SS |
265 | int status, pid; |
266 | CORE_ADDR prev_pc; | |
267 | ||
268 | /* We plant one dummy breakpoint into DUMMY_INSN_ADDR address. We | |
269 | assume that this address will never be executed again by the real | |
270 | code. */ | |
271 | ||
272 | target_insert_breakpoint (DUMMY_INSN_ADDR, shadow_contents); | |
273 | ||
274 | errno = 0; | |
275 | ||
276 | /* You might think this could be done with a single ptrace call, and | |
277 | you'd be correct for just about every platform I've ever worked | |
278 | on. However, rs6000-ibm-aix4.1.3 seems to have screwed this up -- | |
279 | the inferior never hits the breakpoint (it's also worth noting | |
280 | powerpc-ibm-aix4.1.3 works correctly). */ | |
281 | prev_pc = read_pc (); | |
282 | write_pc (DUMMY_INSN_ADDR); | |
c5aa993b | 283 | ptrace (PT_CONTINUE, inferior_pid, (PTRACE_ARG3_TYPE) 1, 0, 0); |
c906108c SS |
284 | |
285 | if (errno) | |
286 | perror ("pt_continue"); | |
287 | ||
c5aa993b JM |
288 | do |
289 | { | |
290 | pid = wait (&status); | |
291 | } | |
292 | while (pid != inferior_pid); | |
293 | ||
c906108c SS |
294 | write_pc (prev_pc); |
295 | target_remove_breakpoint (DUMMY_INSN_ADDR, shadow_contents); | |
296 | } | |
297 | ||
298 | static void | |
299 | fetch_core_registers (core_reg_sect, core_reg_size, which, reg_addr) | |
300 | char *core_reg_sect; | |
301 | unsigned core_reg_size; | |
302 | int which; | |
303 | CORE_ADDR reg_addr; /* Unused in this version */ | |
304 | { | |
305 | /* fetch GPRs and special registers from the first register section | |
306 | in core bfd. */ | |
307 | if (which == 0) | |
308 | { | |
309 | /* copy GPRs first. */ | |
310 | memcpy (registers, core_reg_sect, 32 * 4); | |
311 | ||
312 | /* gdb's internal register template and bfd's register section layout | |
c5aa993b | 313 | should share a common include file. FIXMEmgo */ |
c906108c | 314 | /* then comes special registes. They are supposed to be in the same |
c5aa993b | 315 | order in gdb template and bfd `.reg' section. */ |
c906108c | 316 | core_reg_sect += (32 * 4); |
c5aa993b JM |
317 | memcpy (®isters[REGISTER_BYTE (FIRST_UISA_SP_REGNUM)], |
318 | core_reg_sect, | |
c906108c SS |
319 | (LAST_UISA_SP_REGNUM - FIRST_UISA_SP_REGNUM + 1) * 4); |
320 | } | |
321 | ||
322 | /* fetch floating point registers from register section 2 in core bfd. */ | |
323 | else if (which == 2) | |
c5aa993b | 324 | memcpy (®isters[REGISTER_BYTE (FP0_REGNUM)], core_reg_sect, 32 * 8); |
c906108c SS |
325 | |
326 | else | |
c5aa993b JM |
327 | fprintf_unfiltered |
328 | (gdb_stderr, | |
c906108c SS |
329 | "Gdb error: unknown parameter to fetch_core_registers().\n"); |
330 | } | |
331 | \f | |
332 | /* handle symbol translation on vmapping */ | |
333 | ||
334 | static void | |
335 | vmap_symtab (vp) | |
336 | register struct vmap *vp; | |
337 | { | |
338 | register struct objfile *objfile; | |
339 | struct section_offsets *new_offsets; | |
340 | int i; | |
c5aa993b | 341 | |
c906108c SS |
342 | objfile = vp->objfile; |
343 | if (objfile == NULL) | |
344 | { | |
345 | /* OK, it's not an objfile we opened ourselves. | |
c5aa993b JM |
346 | Currently, that can only happen with the exec file, so |
347 | relocate the symbols for the symfile. */ | |
c906108c SS |
348 | if (symfile_objfile == NULL) |
349 | return; | |
350 | objfile = symfile_objfile; | |
351 | } | |
352 | ||
d4f3574e | 353 | new_offsets = (struct section_offsets *) alloca (SIZEOF_SECTION_OFFSETS); |
c906108c SS |
354 | |
355 | for (i = 0; i < objfile->num_sections; ++i) | |
356 | ANOFFSET (new_offsets, i) = ANOFFSET (objfile->section_offsets, i); | |
c5aa993b | 357 | |
c906108c SS |
358 | /* The symbols in the object file are linked to the VMA of the section, |
359 | relocate them VMA relative. */ | |
360 | ANOFFSET (new_offsets, SECT_OFF_TEXT) = vp->tstart - vp->tvma; | |
361 | ANOFFSET (new_offsets, SECT_OFF_DATA) = vp->dstart - vp->dvma; | |
362 | ANOFFSET (new_offsets, SECT_OFF_BSS) = vp->dstart - vp->dvma; | |
363 | ||
364 | objfile_relocate (objfile, new_offsets); | |
365 | } | |
366 | \f | |
367 | /* Add symbols for an objfile. */ | |
368 | ||
369 | static int | |
370 | objfile_symbol_add (arg) | |
371 | char *arg; | |
372 | { | |
373 | struct objfile *obj = (struct objfile *) arg; | |
374 | ||
2acceee2 | 375 | syms_from_objfile (obj, NULL, 0, 0); |
c906108c SS |
376 | new_symfile_objfile (obj, 0, 0); |
377 | return 1; | |
378 | } | |
379 | ||
380 | /* Add a new vmap entry based on ldinfo() information. | |
381 | ||
382 | If ldi->ldinfo_fd is not valid (e.g. this struct ld_info is from a | |
383 | core file), the caller should set it to -1, and we will open the file. | |
384 | ||
385 | Return the vmap new entry. */ | |
386 | ||
387 | static struct vmap * | |
388 | add_vmap (ldi) | |
c5aa993b | 389 | register struct ld_info *ldi; |
c906108c SS |
390 | { |
391 | bfd *abfd, *last; | |
392 | register char *mem, *objname; | |
393 | struct objfile *obj; | |
394 | struct vmap *vp; | |
395 | ||
396 | /* This ldi structure was allocated using alloca() in | |
397 | xcoff_relocate_symtab(). Now we need to have persistent object | |
398 | and member names, so we should save them. */ | |
399 | ||
400 | mem = ldi->ldinfo_filename + strlen (ldi->ldinfo_filename) + 1; | |
401 | mem = savestring (mem, strlen (mem)); | |
402 | objname = savestring (ldi->ldinfo_filename, strlen (ldi->ldinfo_filename)); | |
403 | ||
404 | if (ldi->ldinfo_fd < 0) | |
405 | /* Note that this opens it once for every member; a possible | |
406 | enhancement would be to only open it once for every object. */ | |
407 | abfd = bfd_openr (objname, gnutarget); | |
408 | else | |
409 | abfd = bfd_fdopenr (objname, gnutarget, ldi->ldinfo_fd); | |
410 | if (!abfd) | |
411 | error ("Could not open `%s' as an executable file: %s", | |
412 | objname, bfd_errmsg (bfd_get_error ())); | |
413 | ||
414 | /* make sure we have an object file */ | |
415 | ||
416 | if (bfd_check_format (abfd, bfd_object)) | |
417 | vp = map_vmap (abfd, 0); | |
418 | ||
419 | else if (bfd_check_format (abfd, bfd_archive)) | |
420 | { | |
421 | last = 0; | |
422 | /* FIXME??? am I tossing BFDs? bfd? */ | |
423 | while ((last = bfd_openr_next_archived_file (abfd, last))) | |
424 | if (STREQ (mem, last->filename)) | |
425 | break; | |
426 | ||
427 | if (!last) | |
428 | { | |
429 | bfd_close (abfd); | |
430 | /* FIXME -- should be error */ | |
431 | warning ("\"%s\": member \"%s\" missing.", abfd->filename, mem); | |
432 | return 0; | |
433 | } | |
434 | ||
c5aa993b | 435 | if (!bfd_check_format (last, bfd_object)) |
c906108c | 436 | { |
c5aa993b | 437 | bfd_close (last); /* XXX??? */ |
c906108c SS |
438 | goto obj_err; |
439 | } | |
440 | ||
441 | vp = map_vmap (last, abfd); | |
442 | } | |
443 | else | |
444 | { | |
445 | obj_err: | |
446 | bfd_close (abfd); | |
447 | error ("\"%s\": not in executable format: %s.", | |
448 | objname, bfd_errmsg (bfd_get_error ())); | |
c5aa993b | 449 | /*NOTREACHED */ |
c906108c | 450 | } |
2df3850c | 451 | obj = allocate_objfile (vp->bfd, 0); |
c906108c SS |
452 | vp->objfile = obj; |
453 | ||
454 | #ifndef SOLIB_SYMBOLS_MANUAL | |
c5aa993b | 455 | if (catch_errors (objfile_symbol_add, (char *) obj, |
c906108c SS |
456 | "Error while reading shared library symbols:\n", |
457 | RETURN_MASK_ALL)) | |
458 | { | |
459 | /* Note this is only done if symbol reading was successful. */ | |
460 | vmap_symtab (vp); | |
461 | vp->loaded = 1; | |
462 | } | |
463 | #endif | |
464 | return vp; | |
465 | } | |
466 | \f | |
467 | /* update VMAP info with ldinfo() information | |
468 | Input is ptr to ldinfo() results. */ | |
469 | ||
470 | static void | |
471 | vmap_ldinfo (ldi) | |
472 | register struct ld_info *ldi; | |
473 | { | |
474 | struct stat ii, vi; | |
475 | register struct vmap *vp; | |
476 | int got_one, retried; | |
477 | int got_exec_file = 0; | |
478 | ||
479 | /* For each *ldi, see if we have a corresponding *vp. | |
480 | If so, update the mapping, and symbol table. | |
481 | If not, add an entry and symbol table. */ | |
482 | ||
c5aa993b JM |
483 | do |
484 | { | |
485 | char *name = ldi->ldinfo_filename; | |
486 | char *memb = name + strlen (name) + 1; | |
487 | ||
488 | retried = 0; | |
489 | ||
490 | if (fstat (ldi->ldinfo_fd, &ii) < 0) | |
491 | { | |
492 | /* The kernel sets ld_info to -1, if the process is still using the | |
493 | object, and the object is removed. Keep the symbol info for the | |
494 | removed object and issue a warning. */ | |
495 | warning ("%s (fd=%d) has disappeared, keeping its symbols", | |
496 | name, ldi->ldinfo_fd); | |
c906108c | 497 | continue; |
c5aa993b JM |
498 | } |
499 | retry: | |
500 | for (got_one = 0, vp = vmap; vp; vp = vp->nxt) | |
501 | { | |
502 | struct objfile *objfile; | |
c906108c | 503 | |
c5aa993b JM |
504 | /* First try to find a `vp', which is the same as in ldinfo. |
505 | If not the same, just continue and grep the next `vp'. If same, | |
506 | relocate its tstart, tend, dstart, dend values. If no such `vp' | |
507 | found, get out of this for loop, add this ldi entry as a new vmap | |
508 | (add_vmap) and come back, find its `vp' and so on... */ | |
509 | ||
510 | /* The filenames are not always sufficient to match on. */ | |
511 | ||
512 | if ((name[0] == '/' && !STREQ (name, vp->name)) | |
513 | || (memb[0] && !STREQ (memb, vp->member))) | |
c906108c | 514 | continue; |
c906108c | 515 | |
c5aa993b JM |
516 | /* See if we are referring to the same file. |
517 | We have to check objfile->obfd, symfile.c:reread_symbols might | |
518 | have updated the obfd after a change. */ | |
519 | objfile = vp->objfile == NULL ? symfile_objfile : vp->objfile; | |
520 | if (objfile == NULL | |
521 | || objfile->obfd == NULL | |
522 | || bfd_stat (objfile->obfd, &vi) < 0) | |
523 | { | |
524 | warning ("Unable to stat %s, keeping its symbols", name); | |
525 | continue; | |
526 | } | |
c906108c | 527 | |
c5aa993b JM |
528 | if (ii.st_dev != vi.st_dev || ii.st_ino != vi.st_ino) |
529 | continue; | |
c906108c | 530 | |
c5aa993b JM |
531 | if (!retried) |
532 | close (ldi->ldinfo_fd); | |
c906108c | 533 | |
c5aa993b | 534 | ++got_one; |
c906108c | 535 | |
c5aa993b | 536 | /* Found a corresponding VMAP. Remap! */ |
c906108c | 537 | |
c5aa993b JM |
538 | /* We can assume pointer == CORE_ADDR, this code is native only. */ |
539 | vp->tstart = (CORE_ADDR) ldi->ldinfo_textorg; | |
540 | vp->tend = vp->tstart + ldi->ldinfo_textsize; | |
541 | vp->dstart = (CORE_ADDR) ldi->ldinfo_dataorg; | |
542 | vp->dend = vp->dstart + ldi->ldinfo_datasize; | |
c906108c | 543 | |
c5aa993b JM |
544 | /* The run time loader maps the file header in addition to the text |
545 | section and returns a pointer to the header in ldinfo_textorg. | |
546 | Adjust the text start address to point to the real start address | |
547 | of the text section. */ | |
548 | vp->tstart += vp->toffs; | |
c906108c | 549 | |
c5aa993b JM |
550 | /* The objfile is only NULL for the exec file. */ |
551 | if (vp->objfile == NULL) | |
552 | got_exec_file = 1; | |
c906108c | 553 | |
c5aa993b JM |
554 | /* relocate symbol table(s). */ |
555 | vmap_symtab (vp); | |
c906108c | 556 | |
c5aa993b JM |
557 | /* There may be more, so we don't break out of the loop. */ |
558 | } | |
559 | ||
560 | /* if there was no matching *vp, we must perforce create the sucker(s) */ | |
561 | if (!got_one && !retried) | |
562 | { | |
563 | add_vmap (ldi); | |
564 | ++retried; | |
565 | goto retry; | |
566 | } | |
567 | } | |
568 | while (ldi->ldinfo_next | |
569 | && (ldi = (void *) (ldi->ldinfo_next + (char *) ldi))); | |
c906108c SS |
570 | |
571 | /* If we don't find the symfile_objfile anywhere in the ldinfo, it | |
572 | is unlikely that the symbol file is relocated to the proper | |
573 | address. And we might have attached to a process which is | |
574 | running a different copy of the same executable. */ | |
575 | if (symfile_objfile != NULL && !got_exec_file) | |
576 | { | |
577 | warning_begin (); | |
578 | fputs_unfiltered ("Symbol file ", gdb_stderr); | |
579 | fputs_unfiltered (symfile_objfile->name, gdb_stderr); | |
580 | fputs_unfiltered ("\nis not mapped; discarding it.\n\ | |
581 | If in fact that file has symbols which the mapped files listed by\n\ | |
582 | \"info files\" lack, you can load symbols with the \"symbol-file\" or\n\ | |
583 | \"add-symbol-file\" commands (note that you must take care of relocating\n\ | |
584 | symbols to the proper address).\n", gdb_stderr); | |
585 | free_objfile (symfile_objfile); | |
586 | symfile_objfile = NULL; | |
587 | } | |
588 | breakpoint_re_set (); | |
589 | } | |
590 | \f | |
591 | /* As well as symbol tables, exec_sections need relocation. After | |
592 | the inferior process' termination, there will be a relocated symbol | |
593 | table exist with no corresponding inferior process. At that time, we | |
594 | need to use `exec' bfd, rather than the inferior process's memory space | |
595 | to look up symbols. | |
596 | ||
597 | `exec_sections' need to be relocated only once, as long as the exec | |
598 | file remains unchanged. | |
c5aa993b | 599 | */ |
c906108c SS |
600 | |
601 | static void | |
602 | vmap_exec () | |
603 | { | |
604 | static bfd *execbfd; | |
605 | int i; | |
606 | ||
607 | if (execbfd == exec_bfd) | |
608 | return; | |
609 | ||
610 | execbfd = exec_bfd; | |
611 | ||
612 | if (!vmap || !exec_ops.to_sections) | |
613 | error ("vmap_exec: vmap or exec_ops.to_sections == 0\n"); | |
614 | ||
c5aa993b | 615 | for (i = 0; &exec_ops.to_sections[i] < exec_ops.to_sections_end; i++) |
c906108c | 616 | { |
c5aa993b | 617 | if (STREQ (".text", exec_ops.to_sections[i].the_bfd_section->name)) |
c906108c SS |
618 | { |
619 | exec_ops.to_sections[i].addr += vmap->tstart - vmap->tvma; | |
620 | exec_ops.to_sections[i].endaddr += vmap->tstart - vmap->tvma; | |
621 | } | |
c5aa993b | 622 | else if (STREQ (".data", exec_ops.to_sections[i].the_bfd_section->name)) |
c906108c SS |
623 | { |
624 | exec_ops.to_sections[i].addr += vmap->dstart - vmap->dvma; | |
625 | exec_ops.to_sections[i].endaddr += vmap->dstart - vmap->dvma; | |
626 | } | |
c5aa993b | 627 | else if (STREQ (".bss", exec_ops.to_sections[i].the_bfd_section->name)) |
c906108c SS |
628 | { |
629 | exec_ops.to_sections[i].addr += vmap->dstart - vmap->dvma; | |
630 | exec_ops.to_sections[i].endaddr += vmap->dstart - vmap->dvma; | |
631 | } | |
632 | } | |
633 | } | |
634 | \f | |
c5aa993b | 635 | /* xcoff_relocate_symtab - hook for symbol table relocation. |
c906108c SS |
636 | also reads shared libraries.. */ |
637 | ||
638 | void | |
639 | xcoff_relocate_symtab (pid) | |
640 | unsigned int pid; | |
641 | { | |
c5aa993b | 642 | #define MAX_LOAD_SEGS 64 /* maximum number of load segments */ |
c906108c SS |
643 | |
644 | struct ld_info *ldi; | |
645 | ||
c5aa993b | 646 | ldi = (void *) alloca (MAX_LOAD_SEGS * sizeof (*ldi)); |
c906108c SS |
647 | |
648 | /* According to my humble theory, AIX has some timing problems and | |
649 | when the user stack grows, kernel doesn't update stack info in time | |
650 | and ptrace calls step on user stack. That is why we sleep here a little, | |
651 | and give kernel to update its internals. */ | |
652 | ||
653 | usleep (36000); | |
654 | ||
655 | errno = 0; | |
656 | ptrace (PT_LDINFO, pid, (PTRACE_ARG3_TYPE) ldi, | |
c5aa993b | 657 | MAX_LOAD_SEGS * sizeof (*ldi), (int *) ldi); |
c906108c SS |
658 | if (errno) |
659 | perror_with_name ("ptrace ldinfo"); | |
660 | ||
661 | vmap_ldinfo (ldi); | |
662 | ||
663 | /* relocate the exec and core sections as well. */ | |
664 | vmap_exec (); | |
665 | } | |
666 | \f | |
667 | /* Core file stuff. */ | |
668 | ||
669 | /* Relocate symtabs and read in shared library info, based on symbols | |
670 | from the core file. */ | |
671 | ||
672 | void | |
673 | xcoff_relocate_core (target) | |
674 | struct target_ops *target; | |
675 | { | |
676 | /* Offset of member MEMBER in a struct of type TYPE. */ | |
677 | #ifndef offsetof | |
678 | #define offsetof(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER) | |
679 | #endif | |
680 | ||
681 | /* Size of a struct ld_info except for the variable-length filename. */ | |
682 | #define LDINFO_SIZE (offsetof (struct ld_info, ldinfo_filename)) | |
683 | ||
684 | sec_ptr ldinfo_sec; | |
685 | int offset = 0; | |
686 | struct ld_info *ldip; | |
687 | struct vmap *vp; | |
688 | ||
689 | /* Allocated size of buffer. */ | |
690 | int buffer_size = LDINFO_SIZE; | |
691 | char *buffer = xmalloc (buffer_size); | |
692 | struct cleanup *old = make_cleanup (free_current_contents, &buffer); | |
c5aa993b | 693 | |
c906108c SS |
694 | /* FIXME, this restriction should not exist. For now, though I'll |
695 | avoid coredumps with error() pending a real fix. */ | |
696 | if (vmap == NULL) | |
697 | error | |
698 | ("Can't debug a core file without an executable file (on the RS/6000)"); | |
c5aa993b | 699 | |
c906108c SS |
700 | ldinfo_sec = bfd_get_section_by_name (core_bfd, ".ldinfo"); |
701 | if (ldinfo_sec == NULL) | |
702 | { | |
703 | bfd_err: | |
704 | fprintf_filtered (gdb_stderr, "Couldn't get ldinfo from core file: %s\n", | |
705 | bfd_errmsg (bfd_get_error ())); | |
706 | do_cleanups (old); | |
707 | return; | |
708 | } | |
709 | do | |
710 | { | |
711 | int i; | |
712 | int names_found = 0; | |
713 | ||
714 | /* Read in everything but the name. */ | |
715 | if (bfd_get_section_contents (core_bfd, ldinfo_sec, buffer, | |
716 | offset, LDINFO_SIZE) == 0) | |
717 | goto bfd_err; | |
718 | ||
719 | /* Now the name. */ | |
720 | i = LDINFO_SIZE; | |
721 | do | |
722 | { | |
723 | if (i == buffer_size) | |
724 | { | |
725 | buffer_size *= 2; | |
726 | buffer = xrealloc (buffer, buffer_size); | |
727 | } | |
728 | if (bfd_get_section_contents (core_bfd, ldinfo_sec, &buffer[i], | |
729 | offset + i, 1) == 0) | |
730 | goto bfd_err; | |
731 | if (buffer[i++] == '\0') | |
732 | ++names_found; | |
c5aa993b JM |
733 | } |
734 | while (names_found < 2); | |
c906108c SS |
735 | |
736 | ldip = (struct ld_info *) buffer; | |
737 | ||
738 | /* Can't use a file descriptor from the core file; need to open it. */ | |
739 | ldip->ldinfo_fd = -1; | |
c5aa993b | 740 | |
c906108c SS |
741 | /* The first ldinfo is for the exec file, allocated elsewhere. */ |
742 | if (offset == 0) | |
743 | vp = vmap; | |
744 | else | |
745 | vp = add_vmap (ldip); | |
746 | ||
747 | offset += ldip->ldinfo_next; | |
748 | ||
749 | /* We can assume pointer == CORE_ADDR, this code is native only. */ | |
750 | vp->tstart = (CORE_ADDR) ldip->ldinfo_textorg; | |
751 | vp->tend = vp->tstart + ldip->ldinfo_textsize; | |
752 | vp->dstart = (CORE_ADDR) ldip->ldinfo_dataorg; | |
753 | vp->dend = vp->dstart + ldip->ldinfo_datasize; | |
754 | ||
755 | /* The run time loader maps the file header in addition to the text | |
c5aa993b JM |
756 | section and returns a pointer to the header in ldinfo_textorg. |
757 | Adjust the text start address to point to the real start address | |
758 | of the text section. */ | |
c906108c SS |
759 | vp->tstart += vp->toffs; |
760 | ||
761 | /* Unless this is the exec file, | |
c5aa993b | 762 | add our sections to the section table for the core target. */ |
c906108c SS |
763 | if (vp != vmap) |
764 | { | |
c906108c | 765 | struct section_table *stp; |
6426a772 JM |
766 | |
767 | target_resize_to_sections (target, 2); | |
c906108c SS |
768 | stp = target->to_sections_end - 2; |
769 | ||
770 | stp->bfd = vp->bfd; | |
771 | stp->the_bfd_section = bfd_get_section_by_name (stp->bfd, ".text"); | |
772 | stp->addr = vp->tstart; | |
773 | stp->endaddr = vp->tend; | |
774 | stp++; | |
c5aa993b | 775 | |
c906108c SS |
776 | stp->bfd = vp->bfd; |
777 | stp->the_bfd_section = bfd_get_section_by_name (stp->bfd, ".data"); | |
778 | stp->addr = vp->dstart; | |
779 | stp->endaddr = vp->dend; | |
780 | } | |
781 | ||
782 | vmap_symtab (vp); | |
c5aa993b JM |
783 | } |
784 | while (ldip->ldinfo_next != 0); | |
c906108c SS |
785 | vmap_exec (); |
786 | breakpoint_re_set (); | |
787 | do_cleanups (old); | |
788 | } | |
789 | ||
790 | int | |
791 | kernel_u_size () | |
792 | { | |
793 | return (sizeof (struct user)); | |
794 | } | |
795 | \f | |
796 | /* Under AIX, we have to pass the correct TOC pointer to a function | |
797 | when calling functions in the inferior. | |
798 | We try to find the relative toc offset of the objfile containing PC | |
799 | and add the current load address of the data segment from the vmap. */ | |
800 | ||
801 | static CORE_ADDR | |
802 | find_toc_address (pc) | |
803 | CORE_ADDR pc; | |
804 | { | |
805 | struct vmap *vp; | |
806 | ||
807 | for (vp = vmap; vp; vp = vp->nxt) | |
808 | { | |
809 | if (pc >= vp->tstart && pc < vp->tend) | |
810 | { | |
811 | /* vp->objfile is only NULL for the exec file. */ | |
812 | return vp->dstart + get_toc_offset (vp->objfile == NULL | |
813 | ? symfile_objfile | |
814 | : vp->objfile); | |
815 | } | |
816 | } | |
817 | error ("Unable to find TOC entry for pc 0x%x\n", pc); | |
818 | } | |
819 | \f | |
820 | /* Register that we are able to handle rs6000 core file formats. */ | |
821 | ||
822 | static struct core_fns rs6000_core_fns = | |
823 | { | |
2acceee2 JM |
824 | bfd_target_coff_flavour, /* core_flavour */ |
825 | default_check_format, /* check_format */ | |
826 | default_core_sniffer, /* core_sniffer */ | |
827 | fetch_core_registers, /* core_read_registers */ | |
828 | NULL /* next */ | |
c906108c SS |
829 | }; |
830 | ||
831 | void | |
832 | _initialize_core_rs6000 () | |
833 | { | |
834 | /* Initialize hook in rs6000-tdep.c for determining the TOC address when | |
835 | calling functions in the inferior. */ | |
836 | find_toc_address_hook = &find_toc_address; | |
837 | ||
838 | /* For native configurations, where this module is included, inform | |
839 | the xcoffsolib module where it can find the function for symbol table | |
840 | relocation at runtime. */ | |
841 | xcoff_relocate_symtab_hook = &xcoff_relocate_symtab; | |
842 | add_core_fns (&rs6000_core_fns); | |
843 | } |