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