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