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