undo previous change
[deliverable/binutils-gdb.git] / gdb / xcoffexec.c
CommitLineData
41abdfbd 1/* Execute AIXcoff files, for GDB.
e17960fb 2 Copyright 1988, 1989, 1991, 1992 Free Software Foundation, Inc.
41abdfbd
JG
3 Derived from exec.c. Modified by IBM Corporation.
4 Donated by IBM Corporation and Cygnus Support.
5
6This file is part of GDB.
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21
22/* xcoff-exec - deal with executing XCOFF files. */
23
d747e0af
MT
24#include "defs.h"
25
41abdfbd
JG
26#include <sys/types.h>
27#include <sys/param.h>
28#include <fcntl.h>
29#include <string.h>
30#include <ctype.h>
31#include <sys/stat.h>
32#include <sys/ldr.h>
33
41abdfbd
JG
34#include "frame.h"
35#include "inferior.h"
36#include "target.h"
37#include "gdbcmd.h"
38#include "gdbcore.h"
39#include "symfile.h"
5e2e79f8 40#include "objfiles.h"
41abdfbd
JG
41
42#include "libbfd.h" /* BFD internals (sigh!) FIXME */
2aefe6e4 43#include "bfd.h"
1eeba686 44#include "xcoffsolib.h"
41abdfbd 45
1ab3bf1b
JG
46/* Prototypes for local functions */
47
1ab3bf1b
JG
48static void
49file_command PARAMS ((char *, int));
50
51static void
52exec_close PARAMS ((int));
53
2aefe6e4
JK
54static struct vmap *
55map_vmap PARAMS ((bfd *, bfd *));
56
41abdfbd
JG
57struct section_table *exec_sections, *exec_sections_end;
58
41abdfbd
JG
59/* Whether to open exec and core files read-only or read-write. */
60
61int write_files = 0;
62
818de002
PB
63extern int info_verbose;
64
41abdfbd
JG
65bfd *exec_bfd; /* needed by core.c */
66
67extern char *getenv();
41abdfbd
JG
68extern void add_syms_addr_command ();
69extern void symbol_file_command ();
70static void exec_files_info();
1eeba686
PB
71
72struct vmap *vmap; /* current vmap */
41abdfbd
JG
73
74extern struct target_ops exec_ops;
75
76
77/* exec_close - done with exec file, clean up all resources. */
78
1ab3bf1b 79static void
818de002
PB
80exec_close(quitting)
81{
82 register struct vmap *vp, *nxt;
83 struct objfile *obj;
84
85 for (nxt = vmap; vp = nxt; )
86 {
87 nxt = vp->nxt;
88
89 /* if there is an objfile associated with this bfd,
90 free_objfile() will do proper cleanup of objfile *and* bfd. */
91
2aefe6e4
JK
92 if (vp->objfile)
93 free_objfile (vp->objfile);
818de002
PB
94 else
95 bfd_close(vp->bfd);
2aefe6e4
JK
96
97 /* FIXME: This routine is #if 0'd in symfile.c. What should we
98 be doing here? Should we just free everything in
99 vp->objfile->symtabs? Should free_objfile do that? */
818de002
PB
100 free_named_symtabs(vp->name);
101 free(vp);
102 }
103
104 vmap = 0;
507e4004 105
c161de74
JK
106 /* exec_bfd was already closed (the exec file has a vmap entry). */
107 exec_bfd = NULL;
108
507e4004
PB
109 if (exec_ops.to_sections) {
110 free (exec_ops.to_sections);
111 exec_ops.to_sections = NULL;
112 exec_ops.to_sections_end = NULL;
113 }
41abdfbd
JG
114}
115
116/*
117 * exec_file_command - handle the "exec" command, &c.
118 */
119void
120exec_file_command(filename, from_tty)
121char *filename;
122{
41abdfbd 123 target_preopen(from_tty);
507e4004
PB
124
125 /* Remove any previous exec file. */
41abdfbd
JG
126 unpush_target(&exec_ops);
127
128 /* Now open and digest the file the user requested, if any. */
129
130 if (filename) {
131 char *scratch_pathname;
132 int scratch_chan;
133
134 filename = tilde_expand(filename);
1ab3bf1b 135 make_cleanup (free, filename);
41abdfbd 136
507e4004
PB
137 scratch_chan = openp(getenv("PATH"), 1, filename,
138 write_files? O_RDWR: O_RDONLY, 0,
139 &scratch_pathname);
41abdfbd
JG
140 if (scratch_chan < 0)
141 perror_with_name(filename);
142
507e4004
PB
143 exec_bfd = bfd_fdopenr(scratch_pathname, NULL, scratch_chan);
144 if (!exec_bfd)
41abdfbd
JG
145 error("Could not open `%s' as an executable file: %s"
146 , scratch_pathname, bfd_errmsg(bfd_error));
147
148 /* make sure we have an object file */
149
507e4004
PB
150 if (!bfd_check_format(exec_bfd, bfd_object))
151 error("\"%s\": not in executable format: %s.",
152 scratch_pathname, bfd_errmsg(bfd_error));
41abdfbd
JG
153
154
155 /* setup initial vmap */
156
507e4004 157 map_vmap (exec_bfd, 0);
41abdfbd 158 if (!vmap)
507e4004
PB
159 error("Can't find the file sections in `%s': %s",
160 exec_bfd->filename, bfd_errmsg(bfd_error));
41abdfbd 161
507e4004
PB
162 if (build_section_table (exec_bfd, &exec_ops.to_sections,
163 &exec_ops.to_sections_end))
41abdfbd
JG
164 error ("Can't find the file sections in `%s': %s",
165 exec_bfd->filename, bfd_errmsg (bfd_error));
166
167 /* make sure core, if present, matches */
168 validate_files();
169
170 push_target(&exec_ops);
171
172 /* Tell display code(if any) about the changed file name. */
173
174 if (exec_file_display_hook)
175 (*exec_file_display_hook)(filename);
176 }
177 else {
178 exec_close(0); /* just in case */
179 if (from_tty)
180 printf("No exec file now.\n");
181 }
182}
183
184/* Set both the exec file and the symbol file, in one command. What a
185 * novelty. Why did GDB go through four major releases before this
186 * command was added?
187 */
1ab3bf1b 188static void
41abdfbd
JG
189file_command(arg, from_tty)
190char *arg; {
191
192 exec_file_command(arg, from_tty);
193 symbol_file_command(arg, from_tty);
194}
195
196/* Locate all mappable sections of a BFD file.
197 table_pp_char is a char * to get it through bfd_map_over_sections;
198 we cast it back to its proper type. */
199
1ab3bf1b 200static void
41abdfbd
JG
201add_to_section_table (abfd, asect, table_pp_char)
202 bfd *abfd;
203 sec_ptr asect;
204 char *table_pp_char;
205{
206 struct section_table **table_pp = (struct section_table **)table_pp_char;
207 flagword aflag;
208
209 aflag = bfd_get_section_flags (abfd, asect);
210 /* FIXME, we need to handle BSS segment here...it alloc's but doesn't load */
211 if (!(aflag & SEC_LOAD))
212 return;
507e4004
PB
213 if (0 == bfd_section_size (abfd, asect))
214 return;
215 (*table_pp)->bfd = abfd;
41abdfbd
JG
216 (*table_pp)->sec_ptr = asect;
217 (*table_pp)->addr = bfd_section_vma (abfd, asect);
218 (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect);
219 (*table_pp)++;
220}
221
222int
223build_section_table (some_bfd, start, end)
224 bfd *some_bfd;
225 struct section_table **start, **end;
226{
227 unsigned count;
228
229 count = bfd_count_sections (some_bfd);
230 if (count == 0)
0848ad1c 231 fatal ("aborting"); /* return 1? */
41abdfbd
JG
232 if (*start)
233 free (*start);
234 *start = (struct section_table *) xmalloc (count * sizeof (**start));
235 *end = *start;
236 bfd_map_over_sections (some_bfd, add_to_section_table, (char *)end);
237 if (*end > *start + count)
0848ad1c 238 fatal ("aborting");
41abdfbd
JG
239 /* We could realloc the table, but it probably loses for most files. */
240 return 0;
241}
2aefe6e4 242\f
41abdfbd 243void
359a097f 244sex_to_vmap(bfd *bf, sec_ptr sex, PTR arg3)
41abdfbd 245{
359a097f 246 struct vmap_and_bfd *vmap_bfd = (struct vmap_and_bfd *)arg3;
41abdfbd
JG
247 register struct vmap *vp, **vpp;
248 register struct symtab *syms;
249 bfd *arch = vmap_bfd->pbfd;
250 vp = vmap_bfd->pvmap;
251
252 if ((bfd_get_section_flags(bf, sex) & SEC_LOAD) == 0)
253 return;
254
2e4964ad 255 if (STREQ(bfd_section_name(bf, sex), ".text")) {
41abdfbd
JG
256 vp->tstart = 0;
257 vp->tend = vp->tstart + bfd_section_size(bf, sex);
258
507e4004
PB
259 /* When it comes to this adjustment value, in contrast to our previous
260 belief shared objects should behave the same as the main load segment.
261 This is the offset from the beginning of text section to the first
262 real instruction. */
263
264 vp->tadj = sex->filepos - bfd_section_vma(bf, sex);
41abdfbd
JG
265 }
266
2e4964ad 267 else if (STREQ(bfd_section_name(bf, sex), ".data")) {
41abdfbd
JG
268 vp->dstart = 0;
269 vp->dend = vp->dstart + bfd_section_size(bf, sex);
270 }
271
2e4964ad 272 else if (STREQ(bfd_section_name(bf, sex), ".bss")) /* FIXMEmgo */
41abdfbd
JG
273 printf ("bss section in exec! Don't know what the heck to do!\n");
274}
275
276/* Make a vmap for the BFD "bf", which might be a member of the archive
2aefe6e4
JK
277 BFD "arch". Return the new vmap. */
278struct vmap *
41abdfbd
JG
279map_vmap (bfd *bf, bfd *arch)
280{
281 struct vmap_and_bfd vmap_bfd;
282 struct vmap *vp, **vpp;
283 struct objfile *obj;
41abdfbd
JG
284
285 vp = (void*) xmalloc (sizeof (*vp));
1eeba686 286 bzero (vp, sizeof (*vp));
41abdfbd
JG
287 vp->nxt = 0;
288 vp->bfd = bf;
289 vp->name = bfd_get_filename(arch ? arch : bf);
290 vp->member = arch ? bfd_get_filename(bf) : "";
291
292 vmap_bfd.pbfd = arch;
293 vmap_bfd.pvmap = vp;
294 bfd_map_over_sections (bf, sex_to_vmap, &vmap_bfd);
295
41abdfbd
JG
296 /* find the end of the list, and append. */
297 for (vpp = &vmap; *vpp; vpp = &(*vpp)->nxt)
298 ;
299 *vpp = vp;
2aefe6e4
JK
300
301 return vp;
41abdfbd
JG
302}
303
507e4004 304
41abdfbd
JG
305/* vmap_symtab - handle symbol translation on vmapping */
306
3c02636b
JK
307static void
308vmap_symtab (vp)
309 register struct vmap *vp;
41abdfbd 310{
1ab3bf1b 311 register struct objfile *objfile;
3c02636b
JK
312 asection *textsec;
313 asection *datasec;
314 asection *bsssec;
4365c36c
JK
315 CORE_ADDR text_delta;
316 CORE_ADDR data_delta;
317 CORE_ADDR bss_delta;
3c02636b
JK
318 struct section_offsets *new_offsets;
319 int i;
1ab3bf1b 320
2aefe6e4
JK
321 objfile = vp->objfile;
322 if (objfile == NULL)
1ab3bf1b 323 {
2aefe6e4
JK
324 /* OK, it's not an objfile we opened ourselves.
325 Currently, that can only happen with the exec file, so
326 relocate the symbols for the symfile. */
327 if (symfile_objfile == NULL)
328 return;
329 objfile = symfile_objfile;
330 }
cbf15d9a 331
3c02636b
JK
332 new_offsets = alloca
333 (sizeof (struct section_offsets)
334 + sizeof (new_offsets->offsets) * objfile->num_sections);
556f3d90 335
3c02636b
JK
336 for (i = 0; i < objfile->num_sections; ++i)
337 ANOFFSET (new_offsets, i) = ANOFFSET (objfile->section_offsets, i);
1ab3bf1b 338
3c02636b 339 textsec = bfd_get_section_by_name (vp->bfd, ".text");
4365c36c
JK
340 text_delta =
341 vp->tstart - ANOFFSET (objfile->section_offsets, textsec->target_index);
3c02636b 342 ANOFFSET (new_offsets, textsec->target_index) = vp->tstart;
4365c36c 343
3c02636b 344 datasec = bfd_get_section_by_name (vp->bfd, ".data");
4365c36c
JK
345 data_delta =
346 vp->dstart - ANOFFSET (objfile->section_offsets, datasec->target_index);
3c02636b 347 ANOFFSET (new_offsets, datasec->target_index) = vp->dstart;
4365c36c 348
3c02636b 349 bsssec = bfd_get_section_by_name (vp->bfd, ".bss");
4365c36c
JK
350 bss_delta =
351 vp->dstart - ANOFFSET (objfile->section_offsets, bsssec->target_index);
3c02636b
JK
352 ANOFFSET (new_offsets, bsssec->target_index) = vp->dstart;
353
354 objfile_relocate (objfile, new_offsets);
4365c36c
JK
355
356 {
357 struct obj_section *s;
358 for (s = objfile->sections; s < objfile->sections_end; ++s)
359 {
360 if (s->sec_ptr->target_index == textsec->target_index)
361 {
362 s->addr += text_delta;
363 s->endaddr += text_delta;
364 }
365 else if (s->sec_ptr->target_index == datasec->target_index)
366 {
367 s->addr += data_delta;
368 s->endaddr += data_delta;
369 }
370 else if (s->sec_ptr->target_index == bsssec->target_index)
371 {
372 s->addr += bss_delta;
373 s->endaddr += bss_delta;
374 }
375 }
376 }
3c02636b 377
4365c36c 378 if (text_delta != 0)
3c02636b 379 /* breakpoints need to be relocated as well. */
4365c36c 380 fixup_breakpoints (0, TEXT_SEGMENT_BASE, text_delta);
41abdfbd
JG
381}
382
8112a711
JK
383/* Add symbols for an objfile. */
384static int
385objfile_symbol_add (arg)
386 char *arg;
387{
388 struct objfile *obj = (struct objfile *) arg;
389 syms_from_objfile (obj, 0, 0, 0);
390 new_symfile_objfile (obj, 0, 0);
391 return 1;
392}
393
2aefe6e4
JK
394static struct vmap *add_vmap PARAMS ((struct ld_info *));
395
396/* Add a new vmap entry based on ldinfo() information.
397
398 If ldi->ldinfo_fd is not valid (e.g. this struct ld_info is from a
399 core file), the caller should set it to -1, and we will open the file.
400
401 Return the vmap new entry. */
402static struct vmap *
41abdfbd 403add_vmap(ldi)
2aefe6e4
JK
404 register struct ld_info *ldi;
405{
41abdfbd 406 bfd *bfd, *last;
507e4004 407 register char *mem, *objname;
2aefe6e4
JK
408 struct objfile *obj;
409 struct vmap *vp;
507e4004
PB
410
411 /* This ldi structure was allocated using alloca() in
9b280a7f 412 xcoff_relocate_symtab(). Now we need to have persistent object
507e4004 413 and member names, so we should save them. */
41abdfbd
JG
414
415 mem = ldi->ldinfo_filename + strlen(ldi->ldinfo_filename) + 1;
507e4004
PB
416 mem = savestring (mem, strlen (mem));
417 objname = savestring (ldi->ldinfo_filename, strlen (ldi->ldinfo_filename));
418
2aefe6e4
JK
419 if (ldi->ldinfo_fd < 0)
420 /* Note that this opens it once for every member; a possible
421 enhancement would be to only open it once for every object. */
422 bfd = bfd_openr (objname, NULL);
423 else
424 bfd = bfd_fdopenr(objname, NULL, ldi->ldinfo_fd);
41abdfbd 425 if (!bfd)
507e4004
PB
426 error("Could not open `%s' as an executable file: %s",
427 objname, bfd_errmsg(bfd_error));
41abdfbd
JG
428
429
430 /* make sure we have an object file */
431
432 if (bfd_check_format(bfd, bfd_object))
2aefe6e4 433 vp = map_vmap (bfd, 0);
41abdfbd
JG
434
435 else if (bfd_check_format(bfd, bfd_archive)) {
436 last = 0;
437 /*
438 * FIXME??? am I tossing BFDs? bfd?
439 */
440 while (last = bfd_openr_next_archived_file(bfd, last))
2e4964ad 441 if (STREQ(mem, last->filename))
41abdfbd
JG
442 break;
443
444 if (!last) {
507e4004
PB
445 bfd_close(bfd);
446 /* FIXME -- should be error */
447 warning("\"%s\": member \"%s\" missing.", bfd->filename, mem);
448 return;
41abdfbd
JG
449 }
450
451 if (!bfd_check_format(last, bfd_object)) {
452 bfd_close(last); /* XXX??? */
453 goto obj_err;
454 }
455
2aefe6e4 456 vp = map_vmap (last, bfd);
41abdfbd
JG
457 }
458 else {
459 obj_err:
460 bfd_close(bfd);
2aefe6e4
JK
461 error ("\"%s\": not in executable format: %s.",
462 objname, bfd_errmsg(bfd_error));
463 /*NOTREACHED*/
41abdfbd 464 }
2aefe6e4
JK
465 obj = allocate_objfile (vp->bfd, 0);
466 vp->objfile = obj;
467
468#ifndef SOLIB_SYMBOLS_MANUAL
8112a711 469 if (catch_errors (objfile_symbol_add, (char *)obj,
9748446f
JK
470 "Error while reading shared library symbols:\n",
471 RETURN_MASK_ALL))
8112a711
JK
472 {
473 /* Note this is only done if symbol reading was successful. */
3c02636b 474 vmap_symtab (vp);
8112a711
JK
475 vp->loaded = 1;
476 }
2aefe6e4
JK
477#endif
478 return vp;
41abdfbd
JG
479}
480
481
507e4004
PB
482/* As well as symbol tables, exec_sections need relocation. After
483 the inferior process' termination, there will be a relocated symbol
484 table exist with no corresponding inferior process. At that time, we
485 need to use `exec' bfd, rather than the inferior process's memory space
486 to look up symbols.
487
488 `exec_sections' need to be relocated only once, as long as the exec
489 file remains unchanged.
41abdfbd
JG
490*/
491vmap_exec ()
492{
493 static bfd *execbfd;
1e939db1
SG
494 int i;
495
41abdfbd
JG
496 if (execbfd == exec_bfd)
497 return;
498
499 execbfd = exec_bfd;
500
1e939db1
SG
501 if (!vmap || !exec_ops.to_sections)
502 error ("vmap_exec: vmap or exec_ops.to_sections == 0\n");
507e4004 503
1e939db1
SG
504 for (i=0; &exec_ops.to_sections[i] < exec_ops.to_sections_end; i++)
505 {
2e4964ad 506 if (STREQ(".text", exec_ops.to_sections[i].sec_ptr->name))
1e939db1
SG
507 {
508 exec_ops.to_sections[i].addr += vmap->tstart;
509 exec_ops.to_sections[i].endaddr += vmap->tstart;
510 }
2e4964ad 511 else if (STREQ(".data", exec_ops.to_sections[i].sec_ptr->name))
1e939db1
SG
512 {
513 exec_ops.to_sections[i].addr += vmap->dstart;
514 exec_ops.to_sections[i].endaddr += vmap->dstart;
515 }
516 }
41abdfbd
JG
517}
518
2aefe6e4
JK
519#if 0
520/* This was for the old, half-assed, core file support. */
41abdfbd
JG
521int
522text_adjustment (abfd)
523bfd *abfd;
524{
525 static bfd *execbfd;
526 static int adjustment;
527 sec_ptr sect;
528
529 if (exec_bfd == execbfd)
530 return adjustment;
531
532 sect = bfd_get_section_by_name (abfd, ".text");
533 if (sect)
534 adjustment = sect->filepos - sect->vma;
535 else
536 adjustment = 0x200; /* just a wild assumption */
537
538 return adjustment;
539}
2aefe6e4 540#endif
41abdfbd
JG
541
542/*
543 * vmap_ldinfo - update VMAP info with ldinfo() information
544 *
545 * Input:
546 * ldi - ^ to ldinfo() results.
547 */
548vmap_ldinfo(ldi)
549register struct ld_info *ldi;
550{
551 struct stat ii, vi;
552 register struct vmap *vp;
553 register got_one, retried;
554 CORE_ADDR ostart;
555
556 /*
557 * for each *ldi, see if we have a corresponding *vp
558 * if so, update the mapping, and symbol table.
559 * if not, add an entry and symbol table.
560 */
561 do {
562 char *name = ldi->ldinfo_filename;
563 char *memb = name + strlen(name) + 1;
564
565 retried = 0;
566
567 if (fstat(ldi->ldinfo_fd, &ii) < 0)
568 fatal("cannot fstat(%d) on %s"
569 , ldi->ldinfo_fd
570 , name);
571retry:
572 for (got_one = 0, vp = vmap; vp; vp = vp->nxt) {
1eeba686
PB
573 FILE *io;
574
575 /* First try to find a `vp', which is the same as in ldinfo.
576 If not the same, just continue and grep the next `vp'. If same,
577 relocate its tstart, tend, dstart, dend values. If no such `vp'
578 found, get out of this for loop, add this ldi entry as a new vmap
579 (add_vmap) and come back, fins its `vp' and so on... */
580
581 /* The filenames are not always sufficient to match on. */
41abdfbd 582
9116f7c7 583 if ((name[0] == '/' && !STREQ(name, vp->name))
2e4964ad 584 || (memb[0] && !STREQ(memb, vp->member)))
1eeba686 585 continue;
41abdfbd 586
1eeba686
PB
587 io = bfd_cache_lookup(vp->bfd); /* totally opaque! */
588 if (!io)
589 fatal("cannot find BFD's iostream for %s", vp->name);
41abdfbd 590
1eeba686 591 /* see if we are referring to the same file */
41abdfbd 592
1eeba686
PB
593 if (fstat(fileno(io), &vi) < 0)
594 fatal("cannot fstat BFD for %s", vp->name);
41abdfbd 595
1eeba686
PB
596 if (ii.st_dev != vi.st_dev || ii.st_ino != vi.st_ino)
597 continue;
41abdfbd 598
1eeba686
PB
599 if (!retried)
600 close(ldi->ldinfo_fd);
41abdfbd 601
1eeba686 602 ++got_one;
41abdfbd 603
1eeba686
PB
604 /* found a corresponding VMAP. remap! */
605 ostart = vp->tstart;
41abdfbd 606
359a097f
JK
607 /* We can assume pointer == CORE_ADDR, this code is native only. */
608 vp->tstart = (CORE_ADDR) ldi->ldinfo_textorg;
1eeba686 609 vp->tend = vp->tstart + ldi->ldinfo_textsize;
359a097f 610 vp->dstart = (CORE_ADDR) ldi->ldinfo_dataorg;
1eeba686 611 vp->dend = vp->dstart + ldi->ldinfo_datasize;
41abdfbd 612
1eeba686
PB
613 if (vp->tadj) {
614 vp->tstart += vp->tadj;
615 vp->tend += vp->tadj;
616 }
617
618 /* relocate symbol table(s). */
3c02636b 619 vmap_symtab (vp);
41abdfbd 620
1eeba686 621 /* there may be more, so we don't break out of the loop. */
41abdfbd
JG
622 }
623
1eeba686
PB
624 /* if there was no matching *vp, we must perforce create the sucker(s) */
625 if (!got_one && !retried) {
626 add_vmap(ldi);
627 ++retried;
628 goto retry;
41abdfbd
JG
629 }
630 } while (ldi->ldinfo_next
631 && (ldi = (void *) (ldi->ldinfo_next + (char *) ldi)));
632
41abdfbd
JG
633}
634
635/*
636 * vmap_inferior - print VMAP info for inferior
637 */
638vmap_inferior() {
639
640 if (inferior_pid == 0)
507e4004 641 return 0; /* normal processing */
41abdfbd
JG
642
643 exec_files_info();
41abdfbd
JG
644 return 1;
645}
646
647/* Read or write the exec file.
648
649 Args are address within exec file, address within gdb address-space,
650 length, and a flag indicating whether to read or write.
651
652 Result is a length:
653
654 0: We cannot handle this address and length.
655 > 0: We have handled N bytes starting at this address.
656 (If N == length, we did it all.) We might be able
657 to handle more bytes beyond this length, but no
658 promises.
659 < 0: We cannot handle this address, but if somebody
660 else handles (-N) bytes, we can start from there.
661
662 The same routine is used to handle both core and exec files;
663 we just tail-call it with more arguments to select between them. */
664
665int
818de002 666xfer_memory (memaddr, myaddr, len, write, target)
41abdfbd
JG
667 CORE_ADDR memaddr;
668 char *myaddr;
669 int len;
670 int write;
818de002 671 struct target_ops *target;
41abdfbd
JG
672{
673 boolean res;
674 struct section_table *p;
675 CORE_ADDR nextsectaddr, memend;
818de002 676 boolean (*xfer_fn) PARAMS ((bfd *, sec_ptr, PTR, file_ptr, bfd_size_type));
41abdfbd
JG
677
678 if (len <= 0)
0848ad1c 679 fatal ("aborting");
41abdfbd
JG
680
681 memend = memaddr + len;
682 xfer_fn = write? bfd_set_section_contents: bfd_get_section_contents;
683 nextsectaddr = memend;
684
818de002 685 for (p = target->to_sections; p < target->to_sections_end; p++)
41abdfbd
JG
686 {
687 if (p->addr <= memaddr)
688 if (p->endaddr >= memend)
689 {
690 /* Entire transfer is within this section. */
818de002 691 res = xfer_fn (p->bfd, p->sec_ptr, myaddr, memaddr - p->addr, len);
41abdfbd
JG
692 return (res != false)? len: 0;
693 }
694 else if (p->endaddr <= memaddr)
695 {
696 /* This section ends before the transfer starts. */
697 continue;
698 }
699 else
700 {
701 /* This section overlaps the transfer. Just do half. */
702 len = p->endaddr - memaddr;
818de002 703 res = xfer_fn (p->bfd, p->sec_ptr, myaddr, memaddr - p->addr, len);
41abdfbd
JG
704 return (res != false)? len: 0;
705 }
706 else if (p->addr < nextsectaddr)
707 nextsectaddr = p->addr;
708 }
709
710 if (nextsectaddr >= memend)
711 return 0; /* We can't help */
712 else
713 return - (nextsectaddr - memaddr); /* Next boundary where we can help */
714}
715
818de002
PB
716void
717print_section_info (t, abfd)
718 struct target_ops *t;
719 bfd *abfd;
41abdfbd 720{
818de002 721 struct section_table *p;
41abdfbd 722
818de002
PB
723 printf_filtered ("\t`%s', ", bfd_get_filename(abfd));
724 wrap_here (" ");
725 printf_filtered ("file type %s.\n", bfd_get_target(abfd));
726
727 for (p = t->to_sections; p < t->to_sections_end; p++) {
728 printf_filtered ("\t%s", local_hex_string_custom (p->addr, "08"));
729 printf_filtered (" - %s", local_hex_string_custom (p->endaddr, "08"));
730 if (info_verbose)
731 printf_filtered (" @ %s",
732 local_hex_string_custom (p->sec_ptr->filepos, "08"));
733 printf_filtered (" is %s", bfd_section_name (p->bfd, p->sec_ptr));
734 if (p->bfd != abfd) {
735 printf_filtered (" in %s", bfd_get_filename (p->bfd));
736 }
737 printf_filtered ("\n");
738 }
818de002
PB
739}
740
507e4004 741
818de002
PB
742static void
743exec_files_info (t)
744 struct target_ops *t;
745{
507e4004
PB
746 register struct vmap *vp = vmap;
747
818de002 748 print_section_info (t, exec_bfd);
507e4004
PB
749
750 if (!vp)
751 return;
752
1eeba686
PB
753 printf("\tMapping info for file `%s'.\n", vp->name);
754
755 printf("\t %8.8s %8.8s %8.8s %8.8s %8.8s %s\n",
756 "tstart", "tend", "dstart", "dend", "section", "file(member)");
507e4004
PB
757
758 for (; vp; vp = vp->nxt)
1eeba686
PB
759 printf("\t0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x %s%s%s%s\n",
760 vp->tstart,
761 vp->tend,
762 vp->dstart,
763 vp->dend,
764 vp->name,
765 *vp->member ? "(" : "",
766 vp->member,
767 *vp->member ? ")" : "");
41abdfbd
JG
768}
769
770#ifdef DAMON
76b28d05 771/* Damon's implementation of set_section_command! It is based on the sex member
41abdfbd
JG
772 (which is a section pointer from vmap) of vmap.
773 We will not have multiple vmap entries (one for each section), rather transmit
774 text and data base offsets and fix them at the same time. Elimination of sex
775 entry in vmap make this function obsolute, use the one from exec.c.
76b28d05 776 Need further testing!! FIXMEmgo. */
41abdfbd
JG
777
778static void
779set_section_command(args, from_tty)
780char *args;
781{
782 register struct vmap *vp = vmap;
783 char *secname;
784 unsigned seclen;
785 unsigned long secaddr;
786 char secprint[100];
787 long offset;
788
789 if (args == 0)
790 error("Must specify section name and its virtual address");
791
792 /* Parse out section name */
793 for (secname = args; !isspace(*args); args++)
794 ;
795 seclen = args - secname;
796
797 /* Parse out new virtual address */
798 secaddr = parse_and_eval_address(args);
799
800 for (vp = vmap; vp; vp = vp->nxt) {
801 if (!strncmp(secname
802 , bfd_section_name(vp->bfd, vp->sex), seclen)
803 && bfd_section_name(vp->bfd, vp->sex)[seclen] == '\0') {
804 offset = secaddr - vp->tstart;
805 vp->tstart += offset;
806 vp->tend += offset;
807 exec_files_info();
808 return;
809 }
810 }
811
812 if (seclen >= sizeof(secprint))
813 seclen = sizeof(secprint) - 1;
814 strncpy(secprint, secname, seclen);
815 secprint[seclen] = '\0';
816 error("Section %s not found", secprint);
817}
818#else
819static void
820set_section_command (args, from_tty)
821 char *args;
822 int from_tty;
823{
824 struct section_table *p;
825 char *secname;
826 unsigned seclen;
827 unsigned long secaddr;
828 char secprint[100];
829 long offset;
830
831 if (args == 0)
832 error ("Must specify section name and its virtual address");
833
834 /* Parse out section name */
835 for (secname = args; !isspace(*args); args++) ;
836 seclen = args - secname;
837
838 /* Parse out new virtual address */
839 secaddr = parse_and_eval_address (args);
840
507e4004 841 for (p = exec_ops.to_sections; p < exec_ops.to_sections_end; p++) {
41abdfbd
JG
842 if (!strncmp (secname, bfd_section_name (exec_bfd, p->sec_ptr), seclen)
843 && bfd_section_name (exec_bfd, p->sec_ptr)[seclen] == '\0') {
844 offset = secaddr - p->addr;
845 p->addr += offset;
846 p->endaddr += offset;
507e4004
PB
847 if (from_tty)
848 exec_files_info(&exec_ops);
41abdfbd
JG
849 return;
850 }
851 }
852 if (seclen >= sizeof (secprint))
853 seclen = sizeof (secprint) - 1;
854 strncpy (secprint, secname, seclen);
855 secprint[seclen] = '\0';
856 error ("Section %s not found", secprint);
857}
858
859#endif /* !DAMON */
860
861struct target_ops exec_ops = {
862 "exec", "Local exec file",
863 "Use an executable file as a target.\n\
864Specify the filename of the executable file.",
865 exec_file_command, exec_close, /* open, close */
050d45c5 866 find_default_attach, 0, 0, 0, /* attach, detach, resume, wait, */
41abdfbd 867 0, 0, /* fetch_registers, store_registers, */
a03d4f8e 868 0, /* prepare_to_store */
818de002 869 xfer_memory, exec_files_info,
41abdfbd
JG
870 0, 0, /* insert_breakpoint, remove_breakpoint, */
871 0, 0, 0, 0, 0, /* terminal stuff */
872 0, 0, /* kill, load */
e17960fb 873 0, /* lookup sym */
050d45c5 874 find_default_create_inferior,
41abdfbd 875 0, /* mourn_inferior */
5ee4e16c 876 0, /* can_run */
3950a34e 877 0, /* notice_signals */
41abdfbd
JG
878 file_stratum, 0, /* next */
879 0, 1, 0, 0, 0, /* all mem, mem, stack, regs, exec */
880 0, 0, /* section pointers */
881 OPS_MAGIC, /* Always the last thing */
882};
2aefe6e4
JK
883\f
884/* Core file stuff. */
885
886/* Relocate symtabs and read in shared library info, based on symbols
887 from the core file. */
888void
889xcoff_relocate_core ()
890{
891/* Offset of member MEMBER in a struct of type TYPE. */
892#ifndef offsetof
893#define offsetof(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER)
894#endif
895
896/* Size of a struct ld_info except for the variable-length filename. */
897#define LDINFO_SIZE (offsetof (struct ld_info, ldinfo_filename))
898
899 sec_ptr ldinfo_sec;
900 int offset = 0;
901 struct ld_info *ldip;
902 struct vmap *vp;
4cdcf3b9 903
2aefe6e4
JK
904 /* Allocated size of buffer. */
905 int buffer_size = LDINFO_SIZE;
906 char *buffer = xmalloc (buffer_size);
907 struct cleanup *old = make_cleanup (free_current_contents, &buffer);
908
4cdcf3b9
JK
909 /* FIXME, this restriction should not exist. For now, though I'll
910 avoid coredumps with error() pending a real fix. */
911 if (vmap == NULL)
912 error
913 ("Can't debug a core file without an executable file (on the RS/6000)");
914
2aefe6e4
JK
915 ldinfo_sec = bfd_get_section_by_name (core_bfd, ".ldinfo");
916 if (ldinfo_sec == NULL)
917 {
918bfd_err:
919 fprintf_filtered (stderr, "Couldn't get ldinfo from core file: %s\n",
920 bfd_errmsg (bfd_error));
921 do_cleanups (old);
922 return;
923 }
924 do
925 {
926 int i;
927 int names_found = 0;
928
929 /* Read in everything but the name. */
930 if (bfd_get_section_contents (core_bfd, ldinfo_sec, buffer,
931 offset, LDINFO_SIZE) == 0)
932 goto bfd_err;
933
934 /* Now the name. */
935 i = LDINFO_SIZE;
936 do
937 {
938 if (i == buffer_size)
939 {
940 buffer_size *= 2;
941 buffer = xrealloc (buffer, buffer_size);
942 }
943 if (bfd_get_section_contents (core_bfd, ldinfo_sec, &buffer[i],
944 offset + i, 1) == 0)
945 goto bfd_err;
946 if (buffer[i++] == '\0')
947 ++names_found;
948 } while (names_found < 2);
949
950 ldip = (struct ld_info *)buffer;
951
952 /* Can't use a file descriptor from the core file; need to open it. */
953 ldip->ldinfo_fd = -1;
954
955 /* The first ldinfo is for the exec file, allocated elsewhere. */
956 if (offset == 0)
957 vp = vmap;
958 else
959 vp = add_vmap (ldip);
960
961 offset += ldip->ldinfo_next;
41abdfbd 962
359a097f
JK
963 /* We can assume pointer == CORE_ADDR, this code is native only. */
964 vp->tstart = (CORE_ADDR) ldip->ldinfo_textorg;
2aefe6e4 965 vp->tend = vp->tstart + ldip->ldinfo_textsize;
359a097f 966 vp->dstart = (CORE_ADDR) ldip->ldinfo_dataorg;
2aefe6e4
JK
967 vp->dend = vp->dstart + ldip->ldinfo_datasize;
968
969 if (vp->tadj != 0) {
970 vp->tstart += vp->tadj;
971 vp->tend += vp->tadj;
972 }
973
974 /* Unless this is the exec file,
975 add our sections to the section table for the core target. */
976 if (vp != vmap)
977 {
978 int count;
979 struct section_table *stp;
980
981 count = core_ops.to_sections_end - core_ops.to_sections;
982 count += 2;
983 core_ops.to_sections = (struct section_table *)
984 xrealloc (core_ops.to_sections,
985 sizeof (struct section_table) * count);
986 core_ops.to_sections_end = core_ops.to_sections + count;
987 stp = core_ops.to_sections_end - 2;
988
989 /* "Why do we add bfd_section_vma?", I hear you cry.
990 Well, the start of the section in the file is actually
991 that far into the section as the struct vmap understands it.
992 So for text sections, bfd_section_vma tends to be 0x200,
993 and if vp->tstart is 0xd0002000, then the first byte of
994 the text section on disk corresponds to address 0xd0002200. */
995 stp->bfd = vp->bfd;
996 stp->sec_ptr = bfd_get_section_by_name (stp->bfd, ".text");
997 stp->addr = bfd_section_vma (stp->bfd, stp->sec_ptr) + vp->tstart;
998 stp->endaddr = bfd_section_vma (stp->bfd, stp->sec_ptr) + vp->tend;
999 stp++;
1000
1001 stp->bfd = vp->bfd;
1002 stp->sec_ptr = bfd_get_section_by_name (stp->bfd, ".data");
1003 stp->addr = bfd_section_vma (stp->bfd, stp->sec_ptr) + vp->dstart;
1004 stp->endaddr = bfd_section_vma (stp->bfd, stp->sec_ptr) + vp->dend;
1005 }
1006
3c02636b 1007 vmap_symtab (vp);
2aefe6e4 1008
359a097f
JK
1009 add_text_to_loadinfo ((CORE_ADDR)ldip->ldinfo_textorg,
1010 (CORE_ADDR)ldip->ldinfo_dataorg);
2aefe6e4
JK
1011 } while (ldip->ldinfo_next != 0);
1012 vmap_exec ();
1013 do_cleanups (old);
1014}
41abdfbd
JG
1015
1016void
1017_initialize_exec()
1018{
1019
1020 add_com("file", class_files, file_command,
1021 "Use FILE as program to be debugged.\n\
1022It is read for its symbols, for getting the contents of pure memory,\n\
1023and it is the program executed when you use the `run' command.\n\
1024If FILE cannot be found as specified, your execution directory path\n\
1025($PATH) is searched for a command of that name.\n\
1026No arg means to have no executable file and no symbols.");
1027
1028 add_com("exec-file", class_files, exec_file_command,
1029 "Use FILE as program for getting contents of pure memory.\n\
1030If FILE cannot be found as specified, your execution directory path\n\
1031is searched for a command of that name.\n\
1032No arg means have no executable file.");
1033
1034 add_com("section", class_files, set_section_command,
1035 "Change the base address of section SECTION of the exec file to ADDR.\n\
1036This can be used if the exec file does not contain section addresses,\n\
1037(such as in the a.out format), or when the addresses specified in the\n\
1038file itself are wrong. Each section must be changed separately. The\n\
1039``info files'' command lists all the sections and their addresses.");
1040
1041 add_target(&exec_ops);
1042}
This page took 0.159895 seconds and 4 git commands to generate.