]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/rs6000-nat.c
* config/sh/tm-sh.h (BELIEVE_PCC_PROMOTION): Define, so that
[thirdparty/binutils-gdb.git] / gdb / rs6000-nat.c
CommitLineData
ef6f3a8b 1/* IBM RS/6000 native-dependent code for GDB, the GNU debugger.
8b150a6b 2 Copyright 1986, 1987, 1989, 1991, 1992, 1994, 1995, 1996, 1997, 1998
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
0c4b30ea
SS
75extern void
76fixup_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
80static 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
90void
91fetch_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*)&registers[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) &registers [REGISTER_BYTE (FP0_REGNUM+ii)],
ef6f3a8b
RP
110 FPR0+ii, 0);
111
112 /* read special registers. */
2ac058fd
JB
113 for (ii=0; ii <= LAST_UISA_SP_REGNUM-FIRST_UISA_SP_REGNUM; ++ii)
114 *(int*)&registers[REGISTER_BYTE (FIRST_UISA_SP_REGNUM+ii)] =
ef6f3a8b
RP
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*)&registers[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) &registers [REGISTER_BYTE (regno)],
ef6f3a8b
RP
131 (regno-FP0_REGNUM+FPR0), 0);
132 }
2ac058fd 133 else if (regno <= LAST_UISA_SP_REGNUM) { /* a special register */
ef6f3a8b
RP
134 *(int*)&registers[REGISTER_BYTE (regno)] =
135 ptrace (PT_READ_GPR, inferior_pid,
2ac058fd
JB
136 (PTRACE_ARG3_TYPE) special_regs[regno-FIRST_UISA_SP_REGNUM],
137 0, 0);
ef6f3a8b
RP
138 }
139 else
2ac058fd
JB
140 fprintf_unfiltered (gdb_stderr,
141 "gdb error: register no %d not implemented.\n",
142 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. */
2ac058fd 196 for (ii=0; ii <= LAST_UISA_SP_REGNUM-FIRST_UISA_SP_REGNUM; ++ii)
0c4b30ea
SS
197 {
198 ptrace (PT_WRITE_GPR, inferior_pid,
199 (PTRACE_ARG3_TYPE) special_regs[ii],
2ac058fd
JB
200 *(int*)&registers[REGISTER_BYTE (FIRST_UISA_SP_REGNUM+ii)],
201 0);
0c4b30ea
SS
202 if (errno)
203 {
204 perror ("ptrace write_gpr");
205 errno = 0;
206 }
ef6f3a8b 207 }
0c4b30ea 208 }
ef6f3a8b
RP
209
210 /* else, a specific register number is given... */
211
0c4b30ea
SS
212 else if (regno < FP0_REGNUM) /* a GPR */
213 {
214 ptrace (PT_WRITE_GPR, inferior_pid, (PTRACE_ARG3_TYPE) regno,
215 *(int*)&registers[REGISTER_BYTE (regno)], 0);
216 }
ef6f3a8b 217
0c4b30ea
SS
218 else if (regno <= FPLAST_REGNUM) /* a FPR */
219 {
220 ptrace (PT_WRITE_FPR, inferior_pid,
221 (PTRACE_ARG3_TYPE) &registers[REGISTER_BYTE (regno)],
222 regno - FP0_REGNUM + FPR0, 0);
223 }
ef6f3a8b 224
2ac058fd 225 else if (regno <= LAST_UISA_SP_REGNUM) /* a special register */
0c4b30ea
SS
226 {
227 ptrace (PT_WRITE_GPR, inferior_pid,
2ac058fd 228 (PTRACE_ARG3_TYPE) special_regs [regno-FIRST_UISA_SP_REGNUM],
0c4b30ea
SS
229 *(int*)&registers[REGISTER_BYTE (regno)], 0);
230 }
ef6f3a8b
RP
231
232 else
2ac058fd
JB
233 fprintf_unfiltered (gdb_stderr,
234 "Gdb error: register no %d not implemented.\n",
235 regno);
ef6f3a8b 236
0c4b30ea
SS
237 if (errno)
238 {
239 perror ("ptrace write");
240 errno = 0;
241 }
ef6f3a8b
RP
242}
243
244/* Execute one dummy breakpoint instruction. This way we give the kernel
245 a chance to do some housekeeping and update inferior's internal data,
246 including u_area. */
0c4b30ea 247
ef6f3a8b
RP
248static void
249exec_one_dummy_insn ()
250{
251#define DUMMY_INSN_ADDR (TEXT_SEGMENT_BASE)+0x200
252
0c4b30ea 253 char shadow_contents[BREAKPOINT_MAX]; /* Stash old bkpt addr contents */
05d52ace 254 int status, pid;
a466b86a 255 CORE_ADDR prev_pc;
ef6f3a8b 256
2ac058fd
JB
257 /* We plant one dummy breakpoint into DUMMY_INSN_ADDR address. We
258 assume that this address will never be executed again by the real
259 code. */
ef6f3a8b 260
0c4b30ea 261 target_insert_breakpoint (DUMMY_INSN_ADDR, shadow_contents);
ef6f3a8b
RP
262
263 errno = 0;
a0d76829
JL
264
265 /* You might think this could be done with a single ptrace call, and
266 you'd be correct for just about every platform I've ever worked
267 on. However, rs6000-ibm-aix4.1.3 seems to have screwed this up --
268 the inferior never hits the breakpoint (it's also worth noting
269 powerpc-ibm-aix4.1.3 works correctly). */
a466b86a 270 prev_pc = read_pc ();
a0d76829
JL
271 write_pc (DUMMY_INSN_ADDR);
272 ptrace (PT_CONTINUE, inferior_pid, (PTRACE_ARG3_TYPE)1, 0, 0);
273
ef6f3a8b
RP
274 if (errno)
275 perror ("pt_continue");
276
277 do {
278 pid = wait (&status);
279 } while (pid != inferior_pid);
280
a466b86a 281 write_pc (prev_pc);
0c4b30ea 282 target_remove_breakpoint (DUMMY_INSN_ADDR, shadow_contents);
ef6f3a8b
RP
283}
284
a1df8e78 285static void
ef6f3a8b
RP
286fetch_core_registers (core_reg_sect, core_reg_size, which, reg_addr)
287 char *core_reg_sect;
288 unsigned core_reg_size;
289 int which;
948a9d92 290 CORE_ADDR reg_addr; /* Unused in this version */
ef6f3a8b
RP
291{
292 /* fetch GPRs and special registers from the first register section
293 in core bfd. */
0c4b30ea
SS
294 if (which == 0)
295 {
296 /* copy GPRs first. */
297 memcpy (registers, core_reg_sect, 32 * 4);
298
299 /* gdb's internal register template and bfd's register section layout
300 should share a common include file. FIXMEmgo */
301 /* then comes special registes. They are supposed to be in the same
302 order in gdb template and bfd `.reg' section. */
303 core_reg_sect += (32 * 4);
2ac058fd
JB
304 memcpy (&registers [REGISTER_BYTE (FIRST_UISA_SP_REGNUM)],
305 core_reg_sect,
306 (LAST_UISA_SP_REGNUM - FIRST_UISA_SP_REGNUM + 1) * 4);
0c4b30ea 307 }
ef6f3a8b
RP
308
309 /* fetch floating point registers from register section 2 in core bfd. */
310 else if (which == 2)
ade40d31 311 memcpy (&registers [REGISTER_BYTE (FP0_REGNUM)], core_reg_sect, 32 * 8);
ef6f3a8b
RP
312
313 else
2ac058fd
JB
314 fprintf_unfiltered
315 (gdb_stderr,
316 "Gdb error: unknown parameter to fetch_core_registers().\n");
ef6f3a8b 317}
d87d7b10 318\f
0c4b30ea 319/* handle symbol translation on vmapping */
d87d7b10
SG
320
321static void
322vmap_symtab (vp)
323 register struct vmap *vp;
324{
325 register struct objfile *objfile;
d87d7b10
SG
326 struct section_offsets *new_offsets;
327 int i;
328
329 objfile = vp->objfile;
330 if (objfile == NULL)
331 {
332 /* OK, it's not an objfile we opened ourselves.
333 Currently, that can only happen with the exec file, so
334 relocate the symbols for the symfile. */
335 if (symfile_objfile == NULL)
336 return;
337 objfile = symfile_objfile;
338 }
339
340 new_offsets = alloca
341 (sizeof (struct section_offsets)
342 + sizeof (new_offsets->offsets) * objfile->num_sections);
343
344 for (i = 0; i < objfile->num_sections; ++i)
345 ANOFFSET (new_offsets, i) = ANOFFSET (objfile->section_offsets, i);
346
211b564e
PS
347 /* The symbols in the object file are linked to the VMA of the section,
348 relocate them VMA relative. */
349 ANOFFSET (new_offsets, SECT_OFF_TEXT) = vp->tstart - vp->tvma;
350 ANOFFSET (new_offsets, SECT_OFF_DATA) = vp->dstart - vp->dvma;
351 ANOFFSET (new_offsets, SECT_OFF_BSS) = vp->dstart - vp->dvma;
d87d7b10
SG
352
353 objfile_relocate (objfile, new_offsets);
d87d7b10
SG
354}
355\f
356/* Add symbols for an objfile. */
0c4b30ea 357
d87d7b10
SG
358static int
359objfile_symbol_add (arg)
360 char *arg;
361{
362 struct objfile *obj = (struct objfile *) arg;
0c4b30ea 363
d87d7b10
SG
364 syms_from_objfile (obj, 0, 0, 0);
365 new_symfile_objfile (obj, 0, 0);
366 return 1;
367}
368
369/* Add a new vmap entry based on ldinfo() information.
370
371 If ldi->ldinfo_fd is not valid (e.g. this struct ld_info is from a
372 core file), the caller should set it to -1, and we will open the file.
373
374 Return the vmap new entry. */
0c4b30ea 375
d87d7b10 376static struct vmap *
0c4b30ea 377add_vmap (ldi)
d87d7b10
SG
378 register struct ld_info *ldi;
379{
0c4b30ea
SS
380 bfd *abfd, *last;
381 register char *mem, *objname;
382 struct objfile *obj;
383 struct vmap *vp;
384
385 /* This ldi structure was allocated using alloca() in
386 xcoff_relocate_symtab(). Now we need to have persistent object
387 and member names, so we should save them. */
388
389 mem = ldi->ldinfo_filename + strlen (ldi->ldinfo_filename) + 1;
390 mem = savestring (mem, strlen (mem));
391 objname = savestring (ldi->ldinfo_filename, strlen (ldi->ldinfo_filename));
392
393 if (ldi->ldinfo_fd < 0)
394 /* Note that this opens it once for every member; a possible
395 enhancement would be to only open it once for every object. */
396 abfd = bfd_openr (objname, gnutarget);
397 else
398 abfd = bfd_fdopenr (objname, gnutarget, ldi->ldinfo_fd);
399 if (!abfd)
400 error ("Could not open `%s' as an executable file: %s",
401 objname, bfd_errmsg (bfd_get_error ()));
402
403 /* make sure we have an object file */
404
405 if (bfd_check_format (abfd, bfd_object))
406 vp = map_vmap (abfd, 0);
407
408 else if (bfd_check_format (abfd, bfd_archive))
409 {
410 last = 0;
411 /* FIXME??? am I tossing BFDs? bfd? */
412 while ((last = bfd_openr_next_archived_file (abfd, last)))
413 if (STREQ (mem, last->filename))
414 break;
415
416 if (!last)
417 {
418 bfd_close (abfd);
419 /* FIXME -- should be error */
420 warning ("\"%s\": member \"%s\" missing.", abfd->filename, mem);
a95d92fa 421 return 0;
d87d7b10 422 }
0c4b30ea
SS
423
424 if (!bfd_check_format(last, bfd_object))
425 {
426 bfd_close (last); /* XXX??? */
427 goto obj_err;
d87d7b10 428 }
0c4b30ea
SS
429
430 vp = map_vmap (last, abfd);
431 }
432 else
433 {
434 obj_err:
435 bfd_close (abfd);
436 error ("\"%s\": not in executable format: %s.",
437 objname, bfd_errmsg (bfd_get_error ()));
438 /*NOTREACHED*/
439 }
4ef1f467 440 obj = allocate_objfile (vp->bfd, 0, 0, 0);
0c4b30ea 441 vp->objfile = obj;
d87d7b10
SG
442
443#ifndef SOLIB_SYMBOLS_MANUAL
0c4b30ea
SS
444 if (catch_errors (objfile_symbol_add, (char *)obj,
445 "Error while reading shared library symbols:\n",
446 RETURN_MASK_ALL))
447 {
448 /* Note this is only done if symbol reading was successful. */
449 vmap_symtab (vp);
450 vp->loaded = 1;
451 }
d87d7b10 452#endif
0c4b30ea 453 return vp;
d87d7b10
SG
454}
455\f
0c4b30ea
SS
456/* update VMAP info with ldinfo() information
457 Input is ptr to ldinfo() results. */
d87d7b10
SG
458
459static void
0c4b30ea 460vmap_ldinfo (ldi)
d87d7b10
SG
461 register struct ld_info *ldi;
462{
463 struct stat ii, vi;
464 register struct vmap *vp;
88a5c3fc 465 int got_one, retried;
a95d92fa 466 int got_exec_file = 0;
d87d7b10 467
0c4b30ea
SS
468 /* For each *ldi, see if we have a corresponding *vp.
469 If so, update the mapping, and symbol table.
470 If not, add an entry and symbol table. */
d87d7b10 471
0c4b30ea
SS
472 do {
473 char *name = ldi->ldinfo_filename;
474 char *memb = name + strlen(name) + 1;
d87d7b10 475
0c4b30ea 476 retried = 0;
d87d7b10 477
0c4b30ea 478 if (fstat (ldi->ldinfo_fd, &ii) < 0)
8b150a6b
PS
479 {
480 /* The kernel sets ld_info to -1, if the process is still using the
481 object, and the object is removed. Keep the symbol info for the
482 removed object and issue a warning. */
483 warning ("%s (fd=%d) has disappeared, keeping its symbols",
484 name, ldi->ldinfo_fd);
485 continue;
486 }
0c4b30ea
SS
487 retry:
488 for (got_one = 0, vp = vmap; vp; vp = vp->nxt)
489 {
8b150a6b
PS
490 struct objfile *objfile;
491
0c4b30ea
SS
492 /* First try to find a `vp', which is the same as in ldinfo.
493 If not the same, just continue and grep the next `vp'. If same,
494 relocate its tstart, tend, dstart, dend values. If no such `vp'
495 found, get out of this for loop, add this ldi entry as a new vmap
8b150a6b 496 (add_vmap) and come back, find its `vp' and so on... */
d87d7b10 497
0c4b30ea 498 /* The filenames are not always sufficient to match on. */
d87d7b10 499
0c4b30ea
SS
500 if ((name[0] == '/' && !STREQ(name, vp->name))
501 || (memb[0] && !STREQ(memb, vp->member)))
502 continue;
d87d7b10 503
8b150a6b
PS
504 /* See if we are referring to the same file.
505 We have to check objfile->obfd, symfile.c:reread_symbols might
506 have updated the obfd after a change. */
507 objfile = vp->objfile == NULL ? symfile_objfile : vp->objfile;
508 if (objfile == NULL
509 || objfile->obfd == NULL
510 || bfd_stat (objfile->obfd, &vi) < 0)
511 {
512 warning ("Unable to stat %s, keeping its symbols", name);
513 continue;
514 }
d87d7b10 515
0c4b30ea
SS
516 if (ii.st_dev != vi.st_dev || ii.st_ino != vi.st_ino)
517 continue;
d87d7b10 518
0c4b30ea
SS
519 if (!retried)
520 close (ldi->ldinfo_fd);
d87d7b10 521
0c4b30ea 522 ++got_one;
d87d7b10 523
fb494327 524 /* Found a corresponding VMAP. Remap! */
d87d7b10 525
0c4b30ea
SS
526 /* We can assume pointer == CORE_ADDR, this code is native only. */
527 vp->tstart = (CORE_ADDR) ldi->ldinfo_textorg;
528 vp->tend = vp->tstart + ldi->ldinfo_textsize;
529 vp->dstart = (CORE_ADDR) ldi->ldinfo_dataorg;
530 vp->dend = vp->dstart + ldi->ldinfo_datasize;
d87d7b10 531
211b564e
PS
532 /* The run time loader maps the file header in addition to the text
533 section and returns a pointer to the header in ldinfo_textorg.
534 Adjust the text start address to point to the real start address
535 of the text section. */
536 vp->tstart += vp->toffs;
d87d7b10 537
88a5c3fc
JK
538 /* The objfile is only NULL for the exec file. */
539 if (vp->objfile == NULL)
540 got_exec_file = 1;
541
0c4b30ea
SS
542 /* relocate symbol table(s). */
543 vmap_symtab (vp);
d87d7b10 544
fb494327 545 /* There may be more, so we don't break out of the loop. */
0c4b30ea 546 }
d87d7b10 547
0c4b30ea
SS
548 /* if there was no matching *vp, we must perforce create the sucker(s) */
549 if (!got_one && !retried)
550 {
551 add_vmap (ldi);
552 ++retried;
553 goto retry;
554 }
d87d7b10
SG
555 } while (ldi->ldinfo_next
556 && (ldi = (void *) (ldi->ldinfo_next + (char *) ldi)));
557
8989d4fc
JK
558 /* If we don't find the symfile_objfile anywhere in the ldinfo, it
559 is unlikely that the symbol file is relocated to the proper
560 address. And we might have attached to a process which is
561 running a different copy of the same executable. */
88a5c3fc 562 if (symfile_objfile != NULL && !got_exec_file)
8989d4fc
JK
563 {
564 warning_begin ();
565 fputs_unfiltered ("Symbol file ", gdb_stderr);
566 fputs_unfiltered (symfile_objfile->name, gdb_stderr);
567 fputs_unfiltered ("\nis not mapped; discarding it.\n\
568If in fact that file has symbols which the mapped files listed by\n\
569\"info files\" lack, you can load symbols with the \"symbol-file\" or\n\
570\"add-symbol-file\" commands (note that you must take care of relocating\n\
571symbols to the proper address).\n", gdb_stderr);
572 free_objfile (symfile_objfile);
573 symfile_objfile = NULL;
574 }
e2adc41a 575 breakpoint_re_set ();
d87d7b10
SG
576}
577\f
578/* As well as symbol tables, exec_sections need relocation. After
579 the inferior process' termination, there will be a relocated symbol
580 table exist with no corresponding inferior process. At that time, we
581 need to use `exec' bfd, rather than the inferior process's memory space
582 to look up symbols.
583
584 `exec_sections' need to be relocated only once, as long as the exec
585 file remains unchanged.
586*/
587
588static void
589vmap_exec ()
590{
591 static bfd *execbfd;
592 int i;
593
594 if (execbfd == exec_bfd)
595 return;
596
597 execbfd = exec_bfd;
598
599 if (!vmap || !exec_ops.to_sections)
600 error ("vmap_exec: vmap or exec_ops.to_sections == 0\n");
601
602 for (i=0; &exec_ops.to_sections[i] < exec_ops.to_sections_end; i++)
603 {
94d4b713 604 if (STREQ(".text", exec_ops.to_sections[i].the_bfd_section->name))
d87d7b10 605 {
211b564e
PS
606 exec_ops.to_sections[i].addr += vmap->tstart - vmap->tvma;
607 exec_ops.to_sections[i].endaddr += vmap->tstart - vmap->tvma;
d87d7b10 608 }
94d4b713 609 else if (STREQ(".data", exec_ops.to_sections[i].the_bfd_section->name))
d87d7b10 610 {
211b564e
PS
611 exec_ops.to_sections[i].addr += vmap->dstart - vmap->dvma;
612 exec_ops.to_sections[i].endaddr += vmap->dstart - vmap->dvma;
613 }
614 else if (STREQ(".bss", exec_ops.to_sections[i].the_bfd_section->name))
615 {
616 exec_ops.to_sections[i].addr += vmap->dstart - vmap->dvma;
617 exec_ops.to_sections[i].endaddr += vmap->dstart - vmap->dvma;
d87d7b10
SG
618 }
619 }
620}
621\f
622/* xcoff_relocate_symtab - hook for symbol table relocation.
623 also reads shared libraries.. */
624
0c4b30ea 625void
d87d7b10 626xcoff_relocate_symtab (pid)
0c4b30ea 627 unsigned int pid;
d87d7b10
SG
628{
629#define MAX_LOAD_SEGS 64 /* maximum number of load segments */
630
0c4b30ea 631 struct ld_info *ldi;
d87d7b10 632
0c4b30ea 633 ldi = (void *) alloca(MAX_LOAD_SEGS * sizeof (*ldi));
d87d7b10 634
0c4b30ea
SS
635 /* According to my humble theory, AIX has some timing problems and
636 when the user stack grows, kernel doesn't update stack info in time
637 and ptrace calls step on user stack. That is why we sleep here a little,
638 and give kernel to update its internals. */
d87d7b10 639
0c4b30ea 640 usleep (36000);
d87d7b10 641
0c4b30ea
SS
642 errno = 0;
643 ptrace (PT_LDINFO, pid, (PTRACE_ARG3_TYPE) ldi,
05d52ace 644 MAX_LOAD_SEGS * sizeof(*ldi), (int *) ldi);
0c4b30ea
SS
645 if (errno)
646 perror_with_name ("ptrace ldinfo");
d87d7b10 647
0c4b30ea 648 vmap_ldinfo (ldi);
d87d7b10 649
d87d7b10
SG
650 /* relocate the exec and core sections as well. */
651 vmap_exec ();
652}
653\f
654/* Core file stuff. */
655
656/* Relocate symtabs and read in shared library info, based on symbols
657 from the core file. */
0c4b30ea 658
d87d7b10 659void
9137a6f4
PS
660xcoff_relocate_core (target)
661 struct target_ops *target;
d87d7b10
SG
662{
663/* Offset of member MEMBER in a struct of type TYPE. */
664#ifndef offsetof
665#define offsetof(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER)
666#endif
667
668/* Size of a struct ld_info except for the variable-length filename. */
669#define LDINFO_SIZE (offsetof (struct ld_info, ldinfo_filename))
670
671 sec_ptr ldinfo_sec;
672 int offset = 0;
673 struct ld_info *ldip;
674 struct vmap *vp;
675
676 /* Allocated size of buffer. */
677 int buffer_size = LDINFO_SIZE;
678 char *buffer = xmalloc (buffer_size);
679 struct cleanup *old = make_cleanup (free_current_contents, &buffer);
680
681 /* FIXME, this restriction should not exist. For now, though I'll
682 avoid coredumps with error() pending a real fix. */
683 if (vmap == NULL)
684 error
685 ("Can't debug a core file without an executable file (on the RS/6000)");
686
687 ldinfo_sec = bfd_get_section_by_name (core_bfd, ".ldinfo");
688 if (ldinfo_sec == NULL)
689 {
0c4b30ea 690 bfd_err:
d87d7b10 691 fprintf_filtered (gdb_stderr, "Couldn't get ldinfo from core file: %s\n",
c4a081e1 692 bfd_errmsg (bfd_get_error ()));
d87d7b10
SG
693 do_cleanups (old);
694 return;
695 }
696 do
697 {
698 int i;
699 int names_found = 0;
700
701 /* Read in everything but the name. */
702 if (bfd_get_section_contents (core_bfd, ldinfo_sec, buffer,
703 offset, LDINFO_SIZE) == 0)
704 goto bfd_err;
705
706 /* Now the name. */
707 i = LDINFO_SIZE;
708 do
709 {
710 if (i == buffer_size)
711 {
712 buffer_size *= 2;
713 buffer = xrealloc (buffer, buffer_size);
714 }
715 if (bfd_get_section_contents (core_bfd, ldinfo_sec, &buffer[i],
716 offset + i, 1) == 0)
717 goto bfd_err;
718 if (buffer[i++] == '\0')
719 ++names_found;
720 } while (names_found < 2);
721
0c4b30ea 722 ldip = (struct ld_info *) buffer;
d87d7b10
SG
723
724 /* Can't use a file descriptor from the core file; need to open it. */
725 ldip->ldinfo_fd = -1;
726
727 /* The first ldinfo is for the exec file, allocated elsewhere. */
728 if (offset == 0)
729 vp = vmap;
730 else
731 vp = add_vmap (ldip);
732
733 offset += ldip->ldinfo_next;
734
735 /* We can assume pointer == CORE_ADDR, this code is native only. */
736 vp->tstart = (CORE_ADDR) ldip->ldinfo_textorg;
737 vp->tend = vp->tstart + ldip->ldinfo_textsize;
738 vp->dstart = (CORE_ADDR) ldip->ldinfo_dataorg;
739 vp->dend = vp->dstart + ldip->ldinfo_datasize;
740
211b564e
PS
741 /* The run time loader maps the file header in addition to the text
742 section and returns a pointer to the header in ldinfo_textorg.
743 Adjust the text start address to point to the real start address
744 of the text section. */
745 vp->tstart += vp->toffs;
d87d7b10
SG
746
747 /* Unless this is the exec file,
748 add our sections to the section table for the core target. */
749 if (vp != vmap)
750 {
751 int count;
752 struct section_table *stp;
148070cc
JL
753 int update_coreops;
754
755 /* We must update the to_sections field in the core_ops structure
756 now to avoid dangling pointer dereferences. */
09af5868 757 update_coreops = core_ops.to_sections == target->to_sections;
d87d7b10 758
9137a6f4 759 count = target->to_sections_end - target->to_sections;
d87d7b10 760 count += 2;
9137a6f4
PS
761 target->to_sections = (struct section_table *)
762 xrealloc (target->to_sections,
d87d7b10 763 sizeof (struct section_table) * count);
9137a6f4 764 target->to_sections_end = target->to_sections + count;
148070cc
JL
765
766 /* Update the to_sections field in the core_ops structure
767 if needed. */
768 if (update_coreops)
769 {
770 core_ops.to_sections = target->to_sections;
771 core_ops.to_sections_end = target->to_sections_end;
772 }
9137a6f4 773 stp = target->to_sections_end - 2;
d87d7b10 774
d87d7b10 775 stp->bfd = vp->bfd;
94d4b713 776 stp->the_bfd_section = bfd_get_section_by_name (stp->bfd, ".text");
211b564e
PS
777 stp->addr = vp->tstart;
778 stp->endaddr = vp->tend;
d87d7b10
SG
779 stp++;
780
781 stp->bfd = vp->bfd;
94d4b713 782 stp->the_bfd_section = bfd_get_section_by_name (stp->bfd, ".data");
211b564e
PS
783 stp->addr = vp->dstart;
784 stp->endaddr = vp->dend;
d87d7b10
SG
785 }
786
787 vmap_symtab (vp);
d87d7b10
SG
788 } while (ldip->ldinfo_next != 0);
789 vmap_exec ();
e2adc41a 790 breakpoint_re_set ();
d87d7b10
SG
791 do_cleanups (old);
792}
7531f36e
FF
793
794int
795kernel_u_size ()
796{
797 return (sizeof (struct user));
798}
05d52ace
PS
799\f
800/* Under AIX, we have to pass the correct TOC pointer to a function
801 when calling functions in the inferior.
802 We try to find the relative toc offset of the objfile containing PC
803 and add the current load address of the data segment from the vmap. */
804
805static CORE_ADDR
806find_toc_address (pc)
807 CORE_ADDR pc;
808{
809 struct vmap *vp;
7531f36e 810
05d52ace
PS
811 for (vp = vmap; vp; vp = vp->nxt)
812 {
813 if (pc >= vp->tstart && pc < vp->tend)
814 {
815 /* vp->objfile is only NULL for the exec file. */
816 return vp->dstart + get_toc_offset (vp->objfile == NULL
817 ? symfile_objfile
818 : vp->objfile);
819 }
820 }
821 error ("Unable to find TOC entry for pc 0x%x\n", pc);
822}
a1df8e78
FF
823\f
824/* Register that we are able to handle rs6000 core file formats. */
825
826static struct core_fns rs6000_core_fns =
827{
828 bfd_target_coff_flavour,
829 fetch_core_registers,
830 NULL
831};
832
833void
834_initialize_core_rs6000 ()
835{
05d52ace
PS
836 /* Initialize hook in rs6000-tdep.c for determining the TOC address when
837 calling functions in the inferior. */
838 find_toc_address_hook = &find_toc_address;
839
062cb0d3
FF
840 /* For native configurations, where this module is included, inform
841 the xcoffsolib module where it can find the function for symbol table
842 relocation at runtime. */
843 xcoff_relocate_symtab_hook = &xcoff_relocate_symtab;
a1df8e78
FF
844 add_core_fns (&rs6000_core_fns);
845}