* hppa-tdep.c (hppa_fix_call_dummy): Use value_ptr.
[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>
41abdfbd 32
41abdfbd
JG
33#include "frame.h"
34#include "inferior.h"
35#include "target.h"
36#include "gdbcmd.h"
37#include "gdbcore.h"
38#include "symfile.h"
5e2e79f8 39#include "objfiles.h"
41abdfbd 40
2aefe6e4 41#include "bfd.h"
1eeba686 42#include "xcoffsolib.h"
41abdfbd 43
1ab3bf1b
JG
44/* Prototypes for local functions */
45
1ab3bf1b
JG
46static void
47file_command PARAMS ((char *, int));
48
49static void
50exec_close PARAMS ((int));
51
d87d7b10 52struct vmap *
2aefe6e4
JK
53map_vmap PARAMS ((bfd *, bfd *));
54
41abdfbd
JG
55struct section_table *exec_sections, *exec_sections_end;
56
41abdfbd
JG
57/* Whether to open exec and core files read-only or read-write. */
58
59int write_files = 0;
60
818de002
PB
61extern int info_verbose;
62
41abdfbd
JG
63bfd *exec_bfd; /* needed by core.c */
64
65extern char *getenv();
41abdfbd
JG
66extern void add_syms_addr_command ();
67extern void symbol_file_command ();
68static void exec_files_info();
1eeba686
PB
69
70struct vmap *vmap; /* current vmap */
41abdfbd
JG
71
72extern struct target_ops exec_ops;
73
41abdfbd
JG
74/* exec_close - done with exec file, clean up all resources. */
75
1ab3bf1b 76static void
818de002
PB
77exec_close(quitting)
78{
79 register struct vmap *vp, *nxt;
80 struct objfile *obj;
0eb22669 81 int need_symtab_cleanup = 0;
818de002
PB
82
83 for (nxt = vmap; vp = nxt; )
84 {
85 nxt = vp->nxt;
86
87 /* if there is an objfile associated with this bfd,
88 free_objfile() will do proper cleanup of objfile *and* bfd. */
89
2aefe6e4 90 if (vp->objfile)
0eb22669
PS
91 {
92 free_objfile (vp->objfile);
93 need_symtab_cleanup = 1;
94 }
818de002
PB
95 else
96 bfd_close(vp->bfd);
2aefe6e4
JK
97
98 /* FIXME: This routine is #if 0'd in symfile.c. What should we
99 be doing here? Should we just free everything in
100 vp->objfile->symtabs? Should free_objfile do that? */
818de002
PB
101 free_named_symtabs(vp->name);
102 free(vp);
103 }
104
105 vmap = 0;
507e4004 106
c161de74
JK
107 /* exec_bfd was already closed (the exec file has a vmap entry). */
108 exec_bfd = NULL;
109
507e4004
PB
110 if (exec_ops.to_sections) {
111 free (exec_ops.to_sections);
112 exec_ops.to_sections = NULL;
113 exec_ops.to_sections_end = NULL;
114 }
0eb22669
PS
115
116 if (need_symtab_cleanup)
117 clear_symtab_users ();
41abdfbd
JG
118}
119
120/*
121 * exec_file_command - handle the "exec" command, &c.
122 */
123void
d87d7b10
SG
124exec_file_command (filename, from_tty)
125 char *filename;
126 int from_tty;
41abdfbd 127{
41abdfbd 128 target_preopen(from_tty);
507e4004
PB
129
130 /* Remove any previous exec file. */
41abdfbd
JG
131 unpush_target(&exec_ops);
132
133 /* Now open and digest the file the user requested, if any. */
134
d87d7b10
SG
135 if (filename)
136 {
137 char *scratch_pathname;
138 int scratch_chan;
41abdfbd 139
d87d7b10
SG
140 filename = tilde_expand(filename);
141 make_cleanup (free, filename);
41abdfbd 142
d87d7b10
SG
143 scratch_chan = openp(getenv("PATH"), 1, filename,
144 write_files? O_RDWR: O_RDONLY, 0,
145 &scratch_pathname);
146 if (scratch_chan < 0)
147 perror_with_name(filename);
41abdfbd 148
d87d7b10
SG
149 exec_bfd = bfd_fdopenr(scratch_pathname, gnutarget, scratch_chan);
150 if (!exec_bfd)
151 error("Could not open `%s' as an executable file: %s",
c4a081e1 152 scratch_pathname, bfd_errmsg(bfd_get_error ()));
41abdfbd 153
d87d7b10 154 /* make sure we have an object file */
41abdfbd 155
d87d7b10
SG
156 if (!bfd_check_format(exec_bfd, bfd_object))
157 error("\"%s\": not in executable format: %s.", scratch_pathname,
c4a081e1 158 bfd_errmsg(bfd_get_error ()));
41abdfbd 159
d87d7b10 160 /* setup initial vmap */
41abdfbd 161
d87d7b10
SG
162 map_vmap (exec_bfd, 0);
163 if (!vmap)
164 error("Can't find the file sections in `%s': %s", exec_bfd->filename,
c4a081e1 165 bfd_errmsg(bfd_get_error ()));
41abdfbd 166
d87d7b10
SG
167 if (build_section_table (exec_bfd, &exec_ops.to_sections,
168 &exec_ops.to_sections_end))
169 error ("Can't find the file sections in `%s': %s", exec_bfd->filename,
c4a081e1 170 bfd_errmsg (bfd_get_error ()));
41abdfbd 171
d87d7b10
SG
172 /* make sure core, if present, matches */
173 validate_files();
41abdfbd 174
d87d7b10 175 push_target(&exec_ops);
41abdfbd 176
d87d7b10 177 /* Tell display code(if any) about the changed file name. */
41abdfbd 178
d87d7b10
SG
179 if (exec_file_display_hook)
180 (*exec_file_display_hook)(filename);
181 }
182 else
183 {
184 exec_close(0); /* just in case */
185 if (from_tty)
186 printf_unfiltered("No exec file now.\n");
187 }
41abdfbd
JG
188}
189
190/* Set both the exec file and the symbol file, in one command. What a
191 * novelty. Why did GDB go through four major releases before this
192 * command was added?
193 */
1ab3bf1b 194static void
41abdfbd
JG
195file_command(arg, from_tty)
196char *arg; {
197
198 exec_file_command(arg, from_tty);
199 symbol_file_command(arg, from_tty);
200}
201
202/* Locate all mappable sections of a BFD file.
203 table_pp_char is a char * to get it through bfd_map_over_sections;
204 we cast it back to its proper type. */
205
1ab3bf1b 206static void
41abdfbd
JG
207add_to_section_table (abfd, asect, table_pp_char)
208 bfd *abfd;
209 sec_ptr asect;
210 char *table_pp_char;
211{
212 struct section_table **table_pp = (struct section_table **)table_pp_char;
213 flagword aflag;
214
215 aflag = bfd_get_section_flags (abfd, asect);
216 /* FIXME, we need to handle BSS segment here...it alloc's but doesn't load */
217 if (!(aflag & SEC_LOAD))
218 return;
507e4004
PB
219 if (0 == bfd_section_size (abfd, asect))
220 return;
221 (*table_pp)->bfd = abfd;
94d4b713 222 (*table_pp)->the_bfd_section = asect;
41abdfbd
JG
223 (*table_pp)->addr = bfd_section_vma (abfd, asect);
224 (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect);
225 (*table_pp)++;
226}
227
228int
229build_section_table (some_bfd, start, end)
230 bfd *some_bfd;
231 struct section_table **start, **end;
232{
233 unsigned count;
234
235 count = bfd_count_sections (some_bfd);
236 if (count == 0)
0848ad1c 237 fatal ("aborting"); /* return 1? */
41abdfbd
JG
238 if (*start)
239 free (*start);
240 *start = (struct section_table *) xmalloc (count * sizeof (**start));
241 *end = *start;
242 bfd_map_over_sections (some_bfd, add_to_section_table, (char *)end);
243 if (*end > *start + count)
0848ad1c 244 fatal ("aborting");
41abdfbd
JG
245 /* We could realloc the table, but it probably loses for most files. */
246 return 0;
247}
2aefe6e4 248\f
d87d7b10
SG
249static void
250bfdsec_to_vmap(bf, sect, arg3)
251 bfd *bf;
252 sec_ptr sect;
253 PTR arg3;
41abdfbd 254{
359a097f 255 struct vmap_and_bfd *vmap_bfd = (struct vmap_and_bfd *)arg3;
41abdfbd
JG
256 register struct vmap *vp, **vpp;
257 register struct symtab *syms;
258 bfd *arch = vmap_bfd->pbfd;
259 vp = vmap_bfd->pvmap;
260
d87d7b10 261 if ((bfd_get_section_flags(bf, sect) & SEC_LOAD) == 0)
41abdfbd
JG
262 return;
263
d87d7b10
SG
264 if (STREQ(bfd_section_name(bf, sect), ".text"))
265 {
266 vp->tstart = 0;
267 vp->tend = vp->tstart + bfd_section_size(bf, sect);
41abdfbd 268
d87d7b10
SG
269 /* When it comes to this adjustment value, in contrast to our previous
270 belief shared objects should behave the same as the main load segment.
271 This is the offset from the beginning of text section to the first
272 real instruction. */
41abdfbd 273
d87d7b10
SG
274 vp->tadj = sect->filepos - bfd_section_vma(bf, sect);
275 }
276 else if (STREQ(bfd_section_name(bf, sect), ".data"))
277 {
278 vp->dstart = 0;
279 vp->dend = vp->dstart + bfd_section_size(bf, sect);
280 }
281 else if (STREQ(bfd_section_name(bf, sect), ".bss")) /* FIXMEmgo */
199b2450 282 printf_unfiltered ("bss section in exec! Don't know what the heck to do!\n");
41abdfbd
JG
283}
284
285/* Make a vmap for the BFD "bf", which might be a member of the archive
2aefe6e4 286 BFD "arch". Return the new vmap. */
d87d7b10 287
2aefe6e4 288struct vmap *
d87d7b10
SG
289map_vmap (bf, arch)
290 bfd *bf;
291 bfd *arch;
41abdfbd
JG
292{
293 struct vmap_and_bfd vmap_bfd;
294 struct vmap *vp, **vpp;
295 struct objfile *obj;
41abdfbd
JG
296
297 vp = (void*) xmalloc (sizeof (*vp));
4ed97c9a 298 memset (vp, '\0', sizeof (*vp));
41abdfbd
JG
299 vp->nxt = 0;
300 vp->bfd = bf;
301 vp->name = bfd_get_filename(arch ? arch : bf);
302 vp->member = arch ? bfd_get_filename(bf) : "";
303
304 vmap_bfd.pbfd = arch;
305 vmap_bfd.pvmap = vp;
d87d7b10 306 bfd_map_over_sections (bf, bfdsec_to_vmap, &vmap_bfd);
41abdfbd 307
41abdfbd
JG
308 /* find the end of the list, and append. */
309 for (vpp = &vmap; *vpp; vpp = &(*vpp)->nxt)
310 ;
311 *vpp = vp;
2aefe6e4
JK
312
313 return vp;
41abdfbd
JG
314}
315
41abdfbd
JG
316/* Read or write the exec file.
317
318 Args are address within exec file, address within gdb address-space,
319 length, and a flag indicating whether to read or write.
320
321 Result is a length:
322
323 0: We cannot handle this address and length.
324 > 0: We have handled N bytes starting at this address.
325 (If N == length, we did it all.) We might be able
326 to handle more bytes beyond this length, but no
327 promises.
328 < 0: We cannot handle this address, but if somebody
329 else handles (-N) bytes, we can start from there.
330
331 The same routine is used to handle both core and exec files;
332 we just tail-call it with more arguments to select between them. */
333
334int
818de002 335xfer_memory (memaddr, myaddr, len, write, target)
41abdfbd
JG
336 CORE_ADDR memaddr;
337 char *myaddr;
338 int len;
339 int write;
818de002 340 struct target_ops *target;
41abdfbd
JG
341{
342 boolean res;
343 struct section_table *p;
344 CORE_ADDR nextsectaddr, memend;
818de002 345 boolean (*xfer_fn) PARAMS ((bfd *, sec_ptr, PTR, file_ptr, bfd_size_type));
41abdfbd
JG
346
347 if (len <= 0)
0848ad1c 348 fatal ("aborting");
41abdfbd
JG
349
350 memend = memaddr + len;
351 xfer_fn = write? bfd_set_section_contents: bfd_get_section_contents;
352 nextsectaddr = memend;
353
818de002 354 for (p = target->to_sections; p < target->to_sections_end; p++)
41abdfbd
JG
355 {
356 if (p->addr <= memaddr)
357 if (p->endaddr >= memend)
358 {
359 /* Entire transfer is within this section. */
94d4b713 360 res = xfer_fn (p->bfd, p->the_bfd_section, myaddr, memaddr - p->addr, len);
41abdfbd
JG
361 return (res != false)? len: 0;
362 }
363 else if (p->endaddr <= memaddr)
364 {
365 /* This section ends before the transfer starts. */
366 continue;
367 }
368 else
369 {
370 /* This section overlaps the transfer. Just do half. */
371 len = p->endaddr - memaddr;
94d4b713 372 res = xfer_fn (p->bfd, p->the_bfd_section, myaddr, memaddr - p->addr, len);
41abdfbd
JG
373 return (res != false)? len: 0;
374 }
375 else if (p->addr < nextsectaddr)
376 nextsectaddr = p->addr;
377 }
378
379 if (nextsectaddr >= memend)
380 return 0; /* We can't help */
381 else
382 return - (nextsectaddr - memaddr); /* Next boundary where we can help */
383}
384
818de002
PB
385void
386print_section_info (t, abfd)
387 struct target_ops *t;
388 bfd *abfd;
41abdfbd 389{
818de002 390 struct section_table *p;
41abdfbd 391
833e0d94 392 /* FIXME-32x64: Need a version of print_address_numeric with field width. */
818de002
PB
393 printf_filtered ("\t`%s', ", bfd_get_filename(abfd));
394 wrap_here (" ");
395 printf_filtered ("file type %s.\n", bfd_get_target(abfd));
396
397 for (p = t->to_sections; p < t->to_sections_end; p++) {
5573d7d4
JK
398 printf_filtered ("\t%s",
399 local_hex_string_custom ((unsigned long) p->addr, "08l"));
400 printf_filtered (" - %s",
401 local_hex_string_custom ((unsigned long) p->endaddr, "08l"));
818de002
PB
402 if (info_verbose)
403 printf_filtered (" @ %s",
94d4b713
JK
404 local_hex_string_custom ((unsigned long) p->the_bfd_section->filepos, "08l"));
405 printf_filtered (" is %s", bfd_section_name (p->bfd, p->the_bfd_section));
95171817
JK
406 if (p->bfd != abfd) {
407 printf_filtered (" in %s", bfd_get_filename (p->bfd));
818de002
PB
408 }
409 printf_filtered ("\n");
410 }
818de002
PB
411}
412
507e4004 413
818de002
PB
414static void
415exec_files_info (t)
416 struct target_ops *t;
417{
507e4004
PB
418 register struct vmap *vp = vmap;
419
818de002 420 print_section_info (t, exec_bfd);
507e4004
PB
421
422 if (!vp)
423 return;
424
199b2450 425 printf_unfiltered("\tMapping info for file `%s'.\n", vp->name);
1eeba686 426
199b2450 427 printf_unfiltered("\t %8.8s %8.8s %8.8s %8.8s %8.8s %s\n",
1eeba686 428 "tstart", "tend", "dstart", "dend", "section", "file(member)");
507e4004
PB
429
430 for (; vp; vp = vp->nxt)
199b2450 431 printf_unfiltered("\t0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x %s%s%s%s\n",
1eeba686
PB
432 vp->tstart,
433 vp->tend,
434 vp->dstart,
435 vp->dend,
436 vp->name,
437 *vp->member ? "(" : "",
438 vp->member,
439 *vp->member ? ")" : "");
41abdfbd
JG
440}
441
442#ifdef DAMON
76b28d05 443/* Damon's implementation of set_section_command! It is based on the sex member
41abdfbd
JG
444 (which is a section pointer from vmap) of vmap.
445 We will not have multiple vmap entries (one for each section), rather transmit
446 text and data base offsets and fix them at the same time. Elimination of sex
447 entry in vmap make this function obsolute, use the one from exec.c.
76b28d05 448 Need further testing!! FIXMEmgo. */
41abdfbd
JG
449
450static void
451set_section_command(args, from_tty)
452char *args;
453{
454 register struct vmap *vp = vmap;
455 char *secname;
456 unsigned seclen;
457 unsigned long secaddr;
458 char secprint[100];
459 long offset;
460
461 if (args == 0)
462 error("Must specify section name and its virtual address");
463
464 /* Parse out section name */
465 for (secname = args; !isspace(*args); args++)
466 ;
467 seclen = args - secname;
468
469 /* Parse out new virtual address */
470 secaddr = parse_and_eval_address(args);
471
472 for (vp = vmap; vp; vp = vp->nxt) {
473 if (!strncmp(secname
95171817
JK
474 , bfd_section_name(vp->bfd, vp->sex), seclen)
475 && bfd_section_name(vp->bfd, vp->sex)[seclen] == '\0') {
41abdfbd
JG
476 offset = secaddr - vp->tstart;
477 vp->tstart += offset;
478 vp->tend += offset;
479 exec_files_info();
480 return;
481 }
482 }
483
484 if (seclen >= sizeof(secprint))
485 seclen = sizeof(secprint) - 1;
486 strncpy(secprint, secname, seclen);
487 secprint[seclen] = '\0';
488 error("Section %s not found", secprint);
489}
490#else
491static void
492set_section_command (args, from_tty)
493 char *args;
494 int from_tty;
495{
496 struct section_table *p;
497 char *secname;
498 unsigned seclen;
499 unsigned long secaddr;
500 char secprint[100];
501 long offset;
502
503 if (args == 0)
504 error ("Must specify section name and its virtual address");
505
506 /* Parse out section name */
507 for (secname = args; !isspace(*args); args++) ;
508 seclen = args - secname;
509
510 /* Parse out new virtual address */
511 secaddr = parse_and_eval_address (args);
512
507e4004 513 for (p = exec_ops.to_sections; p < exec_ops.to_sections_end; p++) {
94d4b713
JK
514 if (!strncmp (secname, bfd_section_name (exec_bfd, p->the_bfd_section), seclen)
515 && bfd_section_name (exec_bfd, p->the_bfd_section)[seclen] == '\0') {
41abdfbd
JG
516 offset = secaddr - p->addr;
517 p->addr += offset;
518 p->endaddr += offset;
507e4004
PB
519 if (from_tty)
520 exec_files_info(&exec_ops);
41abdfbd
JG
521 return;
522 }
523 }
524 if (seclen >= sizeof (secprint))
525 seclen = sizeof (secprint) - 1;
526 strncpy (secprint, secname, seclen);
527 secprint[seclen] = '\0';
528 error ("Section %s not found", secprint);
529}
530
531#endif /* !DAMON */
532
533struct target_ops exec_ops = {
534 "exec", "Local exec file",
535 "Use an executable file as a target.\n\
536Specify the filename of the executable file.",
537 exec_file_command, exec_close, /* open, close */
050d45c5 538 find_default_attach, 0, 0, 0, /* attach, detach, resume, wait, */
41abdfbd 539 0, 0, /* fetch_registers, store_registers, */
a03d4f8e 540 0, /* prepare_to_store */
818de002 541 xfer_memory, exec_files_info,
41abdfbd
JG
542 0, 0, /* insert_breakpoint, remove_breakpoint, */
543 0, 0, 0, 0, 0, /* terminal stuff */
544 0, 0, /* kill, load */
e17960fb 545 0, /* lookup sym */
050d45c5 546 find_default_create_inferior,
41abdfbd 547 0, /* mourn_inferior */
5ee4e16c 548 0, /* can_run */
3950a34e 549 0, /* notice_signals */
41abdfbd
JG
550 file_stratum, 0, /* next */
551 0, 1, 0, 0, 0, /* all mem, mem, stack, regs, exec */
552 0, 0, /* section pointers */
553 OPS_MAGIC, /* Always the last thing */
554};
41abdfbd
JG
555
556void
557_initialize_exec()
558{
559
560 add_com("file", class_files, file_command,
561 "Use FILE as program to be debugged.\n\
562It is read for its symbols, for getting the contents of pure memory,\n\
563and it is the program executed when you use the `run' command.\n\
564If FILE cannot be found as specified, your execution directory path\n\
565($PATH) is searched for a command of that name.\n\
566No arg means to have no executable file and no symbols.");
567
568 add_com("exec-file", class_files, exec_file_command,
569 "Use FILE as program for getting contents of pure memory.\n\
570If FILE cannot be found as specified, your execution directory path\n\
571is searched for a command of that name.\n\
572No arg means have no executable file.");
573
574 add_com("section", class_files, set_section_command,
575 "Change the base address of section SECTION of the exec file to ADDR.\n\
576This can be used if the exec file does not contain section addresses,\n\
577(such as in the a.out format), or when the addresses specified in the\n\
578file itself are wrong. Each section must be changed separately. The\n\
579``info files'' command lists all the sections and their addresses.");
580
581 add_target(&exec_ops);
582}
This page took 0.193049 seconds and 4 git commands to generate.