* gdb.disasm/mn10300.exp: Fix buglets in "other" tests.
[deliverable/binutils-gdb.git] / gdb / rs6000-nat.c
CommitLineData
ef6f3a8b 1/* IBM RS/6000 native-dependent code for GDB, the GNU debugger.
7531f36e 2 Copyright 1986, 1987, 1989, 1991, 1992, 1994, 1995, 1996
df1e1074 3 Free Software Foundation, Inc.
ef6f3a8b
RP
4
5This file is part of GDB.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
6c9638b4 19Foundation, 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
48extern int errno;
0c4b30ea 49
d87d7b10
SG
50extern struct vmap * map_vmap PARAMS ((bfd *bf, bfd *arch));
51
52extern struct target_ops exec_ops;
ef6f3a8b 53
a95d92fa
FF
54static void
55vmap_exec PARAMS ((void));
56
57static void
58vmap_ldinfo PARAMS ((struct ld_info *));
59
60static struct vmap *
61add_vmap PARAMS ((struct ld_info *));
62
63static int
64objfile_symbol_add PARAMS ((char *));
65
66static void
67vmap_symtab PARAMS ((struct vmap *));
68
69static void
948a9d92 70fetch_core_registers PARAMS ((char *, unsigned int, int, CORE_ADDR));
a95d92fa 71
ef6f3a8b
RP
72static void
73exec_one_dummy_insn PARAMS ((void));
74
d87d7b10
SG
75extern void
76add_text_to_loadinfo PARAMS ((CORE_ADDR textaddr, CORE_ADDR dataaddr));
77
0c4b30ea
SS
78extern void
79fixup_breakpoints PARAMS ((CORE_ADDR low, CORE_ADDR high, CORE_ADDR delta));
80
ef6f3a8b
RP
81/* Conversion from gdb-to-system special purpose register numbers.. */
82
83static int special_regs[] = {
84 IAR, /* PC_REGNUM */
85 MSR, /* PS_REGNUM */
86 CR, /* CR_REGNUM */
87 LR, /* LR_REGNUM */
88 CTR, /* CTR_REGNUM */
89 XER, /* XER_REGNUM */
90 MQ /* MQ_REGNUM */
91};
92
93void
94fetch_inferior_registers (regno)
95 int regno;
96{
97 int ii;
98 extern char registers[];
99
100 if (regno < 0) { /* for all registers */
101
102 /* read 32 general purpose registers. */
103
104 for (ii=0; ii < 32; ++ii)
105 *(int*)&registers[REGISTER_BYTE (ii)] =
106 ptrace (PT_READ_GPR, inferior_pid, (PTRACE_ARG3_TYPE) ii, 0, 0);
107
108 /* read general purpose floating point registers. */
109
110 for (ii=0; ii < 32; ++ii)
111 ptrace (PT_READ_FPR, inferior_pid,
0c4b30ea 112 (PTRACE_ARG3_TYPE) &registers [REGISTER_BYTE (FP0_REGNUM+ii)],
ef6f3a8b
RP
113 FPR0+ii, 0);
114
115 /* read special registers. */
116 for (ii=0; ii <= LAST_SP_REGNUM-FIRST_SP_REGNUM; ++ii)
117 *(int*)&registers[REGISTER_BYTE (FIRST_SP_REGNUM+ii)] =
118 ptrace (PT_READ_GPR, inferior_pid, (PTRACE_ARG3_TYPE) special_regs[ii],
119 0, 0);
120
121 registers_fetched ();
122 return;
123 }
124
125 /* else an individual register is addressed. */
126
127 else if (regno < FP0_REGNUM) { /* a GPR */
128 *(int*)&registers[REGISTER_BYTE (regno)] =
129 ptrace (PT_READ_GPR, inferior_pid, (PTRACE_ARG3_TYPE) regno, 0, 0);
130 }
131 else if (regno <= FPLAST_REGNUM) { /* a FPR */
132 ptrace (PT_READ_FPR, inferior_pid,
0c4b30ea 133 (PTRACE_ARG3_TYPE) &registers [REGISTER_BYTE (regno)],
ef6f3a8b
RP
134 (regno-FP0_REGNUM+FPR0), 0);
135 }
136 else if (regno <= LAST_SP_REGNUM) { /* a special register */
137 *(int*)&registers[REGISTER_BYTE (regno)] =
138 ptrace (PT_READ_GPR, inferior_pid,
139 (PTRACE_ARG3_TYPE) special_regs[regno-FIRST_SP_REGNUM], 0, 0);
140 }
141 else
199b2450 142 fprintf_unfiltered (gdb_stderr, "gdb error: register no %d not implemented.\n", regno);
ef6f3a8b
RP
143
144 register_valid [regno] = 1;
145}
146
147/* Store our register values back into the inferior.
148 If REGNO is -1, do this for all registers.
149 Otherwise, REGNO specifies which register (so we can save time). */
150
151void
152store_inferior_registers (regno)
153 int regno;
154{
155 extern char registers[];
156
157 errno = 0;
158
0c4b30ea
SS
159 if (regno == -1)
160 { /* for all registers.. */
ef6f3a8b
RP
161 int ii;
162
163 /* execute one dummy instruction (which is a breakpoint) in inferior
164 process. So give kernel a chance to do internal house keeping.
165 Otherwise the following ptrace(2) calls will mess up user stack
166 since kernel will get confused about the bottom of the stack (%sp) */
167
168 exec_one_dummy_insn ();
169
170 /* write general purpose registers first! */
0c4b30ea
SS
171 for ( ii=GPR0; ii<=GPR31; ++ii)
172 {
173 ptrace (PT_WRITE_GPR, inferior_pid, (PTRACE_ARG3_TYPE) ii,
174 *(int*)&registers[REGISTER_BYTE (ii)], 0);
175 if (errno)
176 {
177 perror ("ptrace write_gpr");
178 errno = 0;
179 }
ef6f3a8b 180 }
ef6f3a8b
RP
181
182 /* write floating point registers now. */
0c4b30ea
SS
183 for ( ii=0; ii < 32; ++ii)
184 {
185 ptrace (PT_WRITE_FPR, inferior_pid,
ef6f3a8b 186 (PTRACE_ARG3_TYPE) &registers[REGISTER_BYTE (FP0_REGNUM+ii)],
0c4b30ea
SS
187 FPR0+ii, 0);
188 if (errno)
189 {
190 perror ("ptrace write_fpr");
191 errno = 0;
192 }
193 }
ef6f3a8b
RP
194
195 /* write special registers. */
0c4b30ea
SS
196 for (ii=0; ii <= LAST_SP_REGNUM-FIRST_SP_REGNUM; ++ii)
197 {
198 ptrace (PT_WRITE_GPR, inferior_pid,
199 (PTRACE_ARG3_TYPE) special_regs[ii],
200 *(int*)&registers[REGISTER_BYTE (FIRST_SP_REGNUM+ii)], 0);
201 if (errno)
202 {
203 perror ("ptrace write_gpr");
204 errno = 0;
205 }
ef6f3a8b 206 }
0c4b30ea 207 }
ef6f3a8b
RP
208
209 /* else, a specific register number is given... */
210
0c4b30ea
SS
211 else if (regno < FP0_REGNUM) /* a GPR */
212 {
213 ptrace (PT_WRITE_GPR, inferior_pid, (PTRACE_ARG3_TYPE) regno,
214 *(int*)&registers[REGISTER_BYTE (regno)], 0);
215 }
ef6f3a8b 216
0c4b30ea
SS
217 else if (regno <= FPLAST_REGNUM) /* a FPR */
218 {
219 ptrace (PT_WRITE_FPR, inferior_pid,
220 (PTRACE_ARG3_TYPE) &registers[REGISTER_BYTE (regno)],
221 regno - FP0_REGNUM + FPR0, 0);
222 }
ef6f3a8b 223
0c4b30ea
SS
224 else if (regno <= LAST_SP_REGNUM) /* a special register */
225 {
226 ptrace (PT_WRITE_GPR, inferior_pid,
227 (PTRACE_ARG3_TYPE) special_regs [regno-FIRST_SP_REGNUM],
228 *(int*)&registers[REGISTER_BYTE (regno)], 0);
229 }
ef6f3a8b
RP
230
231 else
199b2450 232 fprintf_unfiltered (gdb_stderr, "Gdb error: register no %d not implemented.\n", regno);
ef6f3a8b 233
0c4b30ea
SS
234 if (errno)
235 {
236 perror ("ptrace write");
237 errno = 0;
238 }
ef6f3a8b
RP
239}
240
241/* Execute one dummy breakpoint instruction. This way we give the kernel
242 a chance to do some housekeeping and update inferior's internal data,
243 including u_area. */
0c4b30ea 244
ef6f3a8b
RP
245static void
246exec_one_dummy_insn ()
247{
248#define DUMMY_INSN_ADDR (TEXT_SEGMENT_BASE)+0x200
249
0c4b30ea 250 char shadow_contents[BREAKPOINT_MAX]; /* Stash old bkpt addr contents */
ef6f3a8b 251 unsigned int status, pid;
a466b86a 252 CORE_ADDR prev_pc;
ef6f3a8b
RP
253
254 /* We plant one dummy breakpoint into DUMMY_INSN_ADDR address. We assume that
255 this address will never be executed again by the real code. */
256
0c4b30ea 257 target_insert_breakpoint (DUMMY_INSN_ADDR, shadow_contents);
ef6f3a8b
RP
258
259 errno = 0;
a0d76829
JL
260
261 /* You might think this could be done with a single ptrace call, and
262 you'd be correct for just about every platform I've ever worked
263 on. However, rs6000-ibm-aix4.1.3 seems to have screwed this up --
264 the inferior never hits the breakpoint (it's also worth noting
265 powerpc-ibm-aix4.1.3 works correctly). */
a466b86a 266 prev_pc = read_pc ();
a0d76829
JL
267 write_pc (DUMMY_INSN_ADDR);
268 ptrace (PT_CONTINUE, inferior_pid, (PTRACE_ARG3_TYPE)1, 0, 0);
269
ef6f3a8b
RP
270 if (errno)
271 perror ("pt_continue");
272
273 do {
274 pid = wait (&status);
275 } while (pid != inferior_pid);
276
a466b86a 277 write_pc (prev_pc);
0c4b30ea 278 target_remove_breakpoint (DUMMY_INSN_ADDR, shadow_contents);
ef6f3a8b
RP
279}
280
a1df8e78 281static void
ef6f3a8b
RP
282fetch_core_registers (core_reg_sect, core_reg_size, which, reg_addr)
283 char *core_reg_sect;
284 unsigned core_reg_size;
285 int which;
948a9d92 286 CORE_ADDR reg_addr; /* Unused in this version */
ef6f3a8b
RP
287{
288 /* fetch GPRs and special registers from the first register section
289 in core bfd. */
0c4b30ea
SS
290 if (which == 0)
291 {
292 /* copy GPRs first. */
293 memcpy (registers, core_reg_sect, 32 * 4);
294
295 /* gdb's internal register template and bfd's register section layout
296 should share a common include file. FIXMEmgo */
297 /* then comes special registes. They are supposed to be in the same
298 order in gdb template and bfd `.reg' section. */
299 core_reg_sect += (32 * 4);
300 memcpy (&registers [REGISTER_BYTE (FIRST_SP_REGNUM)], core_reg_sect,
301 (LAST_SP_REGNUM - FIRST_SP_REGNUM + 1) * 4);
302 }
ef6f3a8b
RP
303
304 /* fetch floating point registers from register section 2 in core bfd. */
305 else if (which == 2)
ade40d31 306 memcpy (&registers [REGISTER_BYTE (FP0_REGNUM)], core_reg_sect, 32 * 8);
ef6f3a8b
RP
307
308 else
199b2450 309 fprintf_unfiltered (gdb_stderr, "Gdb error: unknown parameter to fetch_core_registers().\n");
ef6f3a8b 310}
d87d7b10 311\f
0c4b30ea 312/* handle symbol translation on vmapping */
d87d7b10
SG
313
314static void
315vmap_symtab (vp)
316 register struct vmap *vp;
317{
318 register struct objfile *objfile;
d87d7b10
SG
319 CORE_ADDR text_delta;
320 CORE_ADDR data_delta;
321 CORE_ADDR bss_delta;
322 struct section_offsets *new_offsets;
323 int i;
324
325 objfile = vp->objfile;
326 if (objfile == NULL)
327 {
328 /* OK, it's not an objfile we opened ourselves.
329 Currently, that can only happen with the exec file, so
330 relocate the symbols for the symfile. */
331 if (symfile_objfile == NULL)
332 return;
333 objfile = symfile_objfile;
334 }
335
336 new_offsets = alloca
337 (sizeof (struct section_offsets)
338 + sizeof (new_offsets->offsets) * objfile->num_sections);
339
340 for (i = 0; i < objfile->num_sections; ++i)
341 ANOFFSET (new_offsets, i) = ANOFFSET (objfile->section_offsets, i);
342
d87d7b10 343 text_delta =
e2adc41a
JK
344 vp->tstart - ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT);
345 ANOFFSET (new_offsets, SECT_OFF_TEXT) = vp->tstart;
d87d7b10 346
d87d7b10 347 data_delta =
e2adc41a
JK
348 vp->dstart - ANOFFSET (objfile->section_offsets, SECT_OFF_DATA);
349 ANOFFSET (new_offsets, SECT_OFF_DATA) = vp->dstart;
d87d7b10 350
d87d7b10 351 bss_delta =
e2adc41a
JK
352 vp->dstart - ANOFFSET (objfile->section_offsets, SECT_OFF_BSS);
353 ANOFFSET (new_offsets, SECT_OFF_BSS) = vp->dstart;
d87d7b10
SG
354
355 objfile_relocate (objfile, new_offsets);
d87d7b10
SG
356}
357\f
358/* Add symbols for an objfile. */
0c4b30ea 359
d87d7b10
SG
360static int
361objfile_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 378static struct vmap *
0c4b30ea 379add_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);
a95d92fa 423 return 0;
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
461static void
0c4b30ea 462vmap_ldinfo (ldi)
d87d7b10
SG
463 register struct ld_info *ldi;
464{
465 struct stat ii, vi;
466 register struct vmap *vp;
88a5c3fc 467 int got_one, retried;
a95d92fa 468 int got_exec_file = 0;
d87d7b10 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 {
0c4b30ea
SS
485 /* First try to find a `vp', which is the same as in ldinfo.
486 If not the same, just continue and grep the next `vp'. If same,
487 relocate its tstart, tend, dstart, dend values. If no such `vp'
488 found, get out of this for loop, add this ldi entry as a new vmap
489 (add_vmap) and come back, fins its `vp' and so on... */
d87d7b10 490
0c4b30ea 491 /* The filenames are not always sufficient to match on. */
d87d7b10 492
0c4b30ea
SS
493 if ((name[0] == '/' && !STREQ(name, vp->name))
494 || (memb[0] && !STREQ(memb, vp->member)))
495 continue;
d87d7b10 496
0c4b30ea 497 /* See if we are referring to the same file. */
fb494327
JK
498 if (bfd_stat (vp->bfd, &vi) < 0)
499 /* An error here is innocuous, most likely meaning that
500 the file descriptor has become worthless.
501 FIXME: What does it mean for a file descriptor to become
502 "worthless"? What makes it happen? What error does it
503 produce (ENOENT? others?)? Should we at least provide
504 a warning? */
523ca9d0 505 continue;
d87d7b10 506
0c4b30ea
SS
507 if (ii.st_dev != vi.st_dev || ii.st_ino != vi.st_ino)
508 continue;
d87d7b10 509
0c4b30ea
SS
510 if (!retried)
511 close (ldi->ldinfo_fd);
d87d7b10 512
0c4b30ea 513 ++got_one;
d87d7b10 514
fb494327 515 /* Found a corresponding VMAP. Remap! */
d87d7b10 516
0c4b30ea
SS
517 /* We can assume pointer == CORE_ADDR, this code is native only. */
518 vp->tstart = (CORE_ADDR) ldi->ldinfo_textorg;
519 vp->tend = vp->tstart + ldi->ldinfo_textsize;
520 vp->dstart = (CORE_ADDR) ldi->ldinfo_dataorg;
521 vp->dend = vp->dstart + ldi->ldinfo_datasize;
d87d7b10 522
0c4b30ea
SS
523 if (vp->tadj)
524 {
d87d7b10
SG
525 vp->tstart += vp->tadj;
526 vp->tend += vp->tadj;
527 }
528
88a5c3fc
JK
529 /* The objfile is only NULL for the exec file. */
530 if (vp->objfile == NULL)
531 got_exec_file = 1;
532
fbc3f191
JL
533#ifdef DONT_RELOCATE_SYMFILE_OBJFILE
534 if (vp->objfile == symfile_objfile
535 || vp->objfile == NULL)
536 {
537 ldi->ldinfo_dataorg = 0;
538 vp->dstart = (CORE_ADDR) 0;
539 vp->dend = ldi->ldinfo_datasize;
540 }
541#endif
542
0c4b30ea
SS
543 /* relocate symbol table(s). */
544 vmap_symtab (vp);
d87d7b10 545
fb494327 546 /* There may be more, so we don't break out of the loop. */
0c4b30ea 547 }
d87d7b10 548
0c4b30ea
SS
549 /* if there was no matching *vp, we must perforce create the sucker(s) */
550 if (!got_one && !retried)
551 {
552 add_vmap (ldi);
553 ++retried;
554 goto retry;
555 }
d87d7b10
SG
556 } while (ldi->ldinfo_next
557 && (ldi = (void *) (ldi->ldinfo_next + (char *) ldi)));
558
8989d4fc
JK
559 /* If we don't find the symfile_objfile anywhere in the ldinfo, it
560 is unlikely that the symbol file is relocated to the proper
561 address. And we might have attached to a process which is
562 running a different copy of the same executable. */
88a5c3fc 563 if (symfile_objfile != NULL && !got_exec_file)
8989d4fc
JK
564 {
565 warning_begin ();
566 fputs_unfiltered ("Symbol file ", gdb_stderr);
567 fputs_unfiltered (symfile_objfile->name, gdb_stderr);
568 fputs_unfiltered ("\nis not mapped; discarding it.\n\
569If in fact that file has symbols which the mapped files listed by\n\
570\"info files\" lack, you can load symbols with the \"symbol-file\" or\n\
571\"add-symbol-file\" commands (note that you must take care of relocating\n\
572symbols to the proper address).\n", gdb_stderr);
573 free_objfile (symfile_objfile);
574 symfile_objfile = NULL;
575 }
e2adc41a 576 breakpoint_re_set ();
d87d7b10
SG
577}
578\f
579/* As well as symbol tables, exec_sections need relocation. After
580 the inferior process' termination, there will be a relocated symbol
581 table exist with no corresponding inferior process. At that time, we
582 need to use `exec' bfd, rather than the inferior process's memory space
583 to look up symbols.
584
585 `exec_sections' need to be relocated only once, as long as the exec
586 file remains unchanged.
587*/
588
589static void
590vmap_exec ()
591{
592 static bfd *execbfd;
593 int i;
594
595 if (execbfd == exec_bfd)
596 return;
597
598 execbfd = exec_bfd;
599
600 if (!vmap || !exec_ops.to_sections)
601 error ("vmap_exec: vmap or exec_ops.to_sections == 0\n");
602
603 for (i=0; &exec_ops.to_sections[i] < exec_ops.to_sections_end; i++)
604 {
94d4b713 605 if (STREQ(".text", exec_ops.to_sections[i].the_bfd_section->name))
d87d7b10
SG
606 {
607 exec_ops.to_sections[i].addr += vmap->tstart;
608 exec_ops.to_sections[i].endaddr += vmap->tstart;
609 }
94d4b713 610 else if (STREQ(".data", exec_ops.to_sections[i].the_bfd_section->name))
d87d7b10
SG
611 {
612 exec_ops.to_sections[i].addr += vmap->dstart;
613 exec_ops.to_sections[i].endaddr += vmap->dstart;
614 }
615 }
616}
617\f
618/* xcoff_relocate_symtab - hook for symbol table relocation.
619 also reads shared libraries.. */
620
0c4b30ea 621void
d87d7b10 622xcoff_relocate_symtab (pid)
0c4b30ea 623 unsigned int pid;
d87d7b10
SG
624{
625#define MAX_LOAD_SEGS 64 /* maximum number of load segments */
626
0c4b30ea 627 struct ld_info *ldi;
d87d7b10 628
0c4b30ea 629 ldi = (void *) alloca(MAX_LOAD_SEGS * sizeof (*ldi));
d87d7b10 630
0c4b30ea
SS
631 /* According to my humble theory, AIX has some timing problems and
632 when the user stack grows, kernel doesn't update stack info in time
633 and ptrace calls step on user stack. That is why we sleep here a little,
634 and give kernel to update its internals. */
d87d7b10 635
0c4b30ea 636 usleep (36000);
d87d7b10 637
0c4b30ea
SS
638 errno = 0;
639 ptrace (PT_LDINFO, pid, (PTRACE_ARG3_TYPE) ldi,
640 MAX_LOAD_SEGS * sizeof(*ldi), ldi);
641 if (errno)
642 perror_with_name ("ptrace ldinfo");
d87d7b10 643
0c4b30ea 644 vmap_ldinfo (ldi);
d87d7b10 645
0c4b30ea
SS
646 do {
647 /* We are allowed to assume CORE_ADDR == pointer. This code is
648 native only. */
649 add_text_to_loadinfo ((CORE_ADDR) ldi->ldinfo_textorg,
650 (CORE_ADDR) ldi->ldinfo_dataorg);
651 } while (ldi->ldinfo_next
652 && (ldi = (void *) (ldi->ldinfo_next + (char *) ldi)));
d87d7b10
SG
653
654#if 0
655 /* Now that we've jumbled things around, re-sort them. */
656 sort_minimal_symbols ();
657#endif
658
659 /* relocate the exec and core sections as well. */
660 vmap_exec ();
661}
662\f
663/* Core file stuff. */
664
665/* Relocate symtabs and read in shared library info, based on symbols
666 from the core file. */
0c4b30ea 667
d87d7b10 668void
9137a6f4
PS
669xcoff_relocate_core (target)
670 struct target_ops *target;
d87d7b10
SG
671{
672/* Offset of member MEMBER in a struct of type TYPE. */
673#ifndef offsetof
674#define offsetof(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER)
675#endif
676
677/* Size of a struct ld_info except for the variable-length filename. */
678#define LDINFO_SIZE (offsetof (struct ld_info, ldinfo_filename))
679
680 sec_ptr ldinfo_sec;
681 int offset = 0;
682 struct ld_info *ldip;
683 struct vmap *vp;
684
685 /* Allocated size of buffer. */
686 int buffer_size = LDINFO_SIZE;
687 char *buffer = xmalloc (buffer_size);
688 struct cleanup *old = make_cleanup (free_current_contents, &buffer);
689
690 /* FIXME, this restriction should not exist. For now, though I'll
691 avoid coredumps with error() pending a real fix. */
692 if (vmap == NULL)
693 error
694 ("Can't debug a core file without an executable file (on the RS/6000)");
695
696 ldinfo_sec = bfd_get_section_by_name (core_bfd, ".ldinfo");
697 if (ldinfo_sec == NULL)
698 {
0c4b30ea 699 bfd_err:
d87d7b10 700 fprintf_filtered (gdb_stderr, "Couldn't get ldinfo from core file: %s\n",
c4a081e1 701 bfd_errmsg (bfd_get_error ()));
d87d7b10
SG
702 do_cleanups (old);
703 return;
704 }
705 do
706 {
707 int i;
708 int names_found = 0;
709
710 /* Read in everything but the name. */
711 if (bfd_get_section_contents (core_bfd, ldinfo_sec, buffer,
712 offset, LDINFO_SIZE) == 0)
713 goto bfd_err;
714
715 /* Now the name. */
716 i = LDINFO_SIZE;
717 do
718 {
719 if (i == buffer_size)
720 {
721 buffer_size *= 2;
722 buffer = xrealloc (buffer, buffer_size);
723 }
724 if (bfd_get_section_contents (core_bfd, ldinfo_sec, &buffer[i],
725 offset + i, 1) == 0)
726 goto bfd_err;
727 if (buffer[i++] == '\0')
728 ++names_found;
729 } while (names_found < 2);
730
0c4b30ea 731 ldip = (struct ld_info *) buffer;
d87d7b10
SG
732
733 /* Can't use a file descriptor from the core file; need to open it. */
734 ldip->ldinfo_fd = -1;
735
736 /* The first ldinfo is for the exec file, allocated elsewhere. */
737 if (offset == 0)
738 vp = vmap;
739 else
740 vp = add_vmap (ldip);
741
742 offset += ldip->ldinfo_next;
743
744 /* We can assume pointer == CORE_ADDR, this code is native only. */
745 vp->tstart = (CORE_ADDR) ldip->ldinfo_textorg;
746 vp->tend = vp->tstart + ldip->ldinfo_textsize;
747 vp->dstart = (CORE_ADDR) ldip->ldinfo_dataorg;
748 vp->dend = vp->dstart + ldip->ldinfo_datasize;
749
df1e1074
PS
750#ifdef DONT_RELOCATE_SYMFILE_OBJFILE
751 if (vp == vmap)
752 {
753 vp->dstart = (CORE_ADDR) 0;
754 vp->dend = ldip->ldinfo_datasize;
755 }
756#endif
757
523ca9d0
SS
758 if (vp->tadj != 0)
759 {
760 vp->tstart += vp->tadj;
761 vp->tend += vp->tadj;
762 }
d87d7b10
SG
763
764 /* Unless this is the exec file,
765 add our sections to the section table for the core target. */
766 if (vp != vmap)
767 {
768 int count;
769 struct section_table *stp;
148070cc
JL
770 int update_coreops;
771
772 /* We must update the to_sections field in the core_ops structure
773 now to avoid dangling pointer dereferences. */
09af5868 774 update_coreops = core_ops.to_sections == target->to_sections;
d87d7b10 775
9137a6f4 776 count = target->to_sections_end - target->to_sections;
d87d7b10 777 count += 2;
9137a6f4
PS
778 target->to_sections = (struct section_table *)
779 xrealloc (target->to_sections,
d87d7b10 780 sizeof (struct section_table) * count);
9137a6f4 781 target->to_sections_end = target->to_sections + count;
148070cc
JL
782
783 /* Update the to_sections field in the core_ops structure
784 if needed. */
785 if (update_coreops)
786 {
787 core_ops.to_sections = target->to_sections;
788 core_ops.to_sections_end = target->to_sections_end;
789 }
9137a6f4 790 stp = target->to_sections_end - 2;
d87d7b10
SG
791
792 /* "Why do we add bfd_section_vma?", I hear you cry.
793 Well, the start of the section in the file is actually
794 that far into the section as the struct vmap understands it.
795 So for text sections, bfd_section_vma tends to be 0x200,
796 and if vp->tstart is 0xd0002000, then the first byte of
797 the text section on disk corresponds to address 0xd0002200. */
798 stp->bfd = vp->bfd;
94d4b713
JK
799 stp->the_bfd_section = bfd_get_section_by_name (stp->bfd, ".text");
800 stp->addr = bfd_section_vma (stp->bfd, stp->the_bfd_section) + vp->tstart;
801 stp->endaddr = bfd_section_vma (stp->bfd, stp->the_bfd_section) + vp->tend;
d87d7b10
SG
802 stp++;
803
804 stp->bfd = vp->bfd;
94d4b713
JK
805 stp->the_bfd_section = bfd_get_section_by_name (stp->bfd, ".data");
806 stp->addr = bfd_section_vma (stp->bfd, stp->the_bfd_section) + vp->dstart;
807 stp->endaddr = bfd_section_vma (stp->bfd, stp->the_bfd_section) + vp->dend;
d87d7b10
SG
808 }
809
810 vmap_symtab (vp);
811
812 add_text_to_loadinfo ((CORE_ADDR)ldip->ldinfo_textorg,
813 (CORE_ADDR)ldip->ldinfo_dataorg);
814 } while (ldip->ldinfo_next != 0);
815 vmap_exec ();
e2adc41a 816 breakpoint_re_set ();
d87d7b10
SG
817 do_cleanups (old);
818}
7531f36e
FF
819
820int
821kernel_u_size ()
822{
823 return (sizeof (struct user));
824}
825
a1df8e78
FF
826\f
827/* Register that we are able to handle rs6000 core file formats. */
828
829static struct core_fns rs6000_core_fns =
830{
831 bfd_target_coff_flavour,
832 fetch_core_registers,
833 NULL
834};
835
836void
837_initialize_core_rs6000 ()
838{
062cb0d3
FF
839 /* For native configurations, where this module is included, inform
840 the xcoffsolib module where it can find the function for symbol table
841 relocation at runtime. */
842 xcoff_relocate_symtab_hook = &xcoff_relocate_symtab;
a1df8e78
FF
843 add_core_fns (&rs6000_core_fns);
844}
This page took 0.300181 seconds and 4 git commands to generate.