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