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