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