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