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