1 /* Work with executable files, for GDB. 2 Copyright 1988, 1989, 1991, 1992 Free Software Foundation, Inc. 3 4 This file is part of GDB. 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program; if not, write to the Free Software 18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ 19 20 #include "defs.h" 21 #include "frame.h" 22 #include "inferior.h" 23 #include "target.h" 24 #include "gdbcmd.h" 25 26 #ifdef KERNELDEBUG 27 #include "kernel.h" 28 #endif 29 30 #ifdef USG 31 #include <sys/types.h> 32 #endif 33 34 #include <sys/param.h> 35 #include <fcntl.h> 36 #include <string.h> 37 38 #include "gdbcore.h" 39 40 #include <ctype.h> 41 #include <sys/stat.h> 42 #ifndef O_BINARY 43 #define O_BINARY 0 44 #endif 45 46 /* Prototypes for local functions */ 47 48 static void 49 add_to_section_table PARAMS ((bfd *, sec_ptr, PTR)); 50 51 static void 52 exec_close PARAMS ((int)); 53 54 static void 55 file_command PARAMS ((char *, int)); 56 57 static void 58 set_section_command PARAMS ((char *, int)); 59 60 static void 61 exec_files_info PARAMS ((struct target_ops *)); 62 63 extern int info_verbose; 64 65 /* The Binary File Descriptor handle for the executable file. */ 66 67 bfd *exec_bfd = NULL; 68 69 /* Whether to open exec and core files read-only or read-write. */ 70 71 int write_files = 0; 72 73 /* Text start and end addresses (KLUDGE) if needed */ 74 75 #ifdef NEED_TEXT_START_END 76 CORE_ADDR text_start = 0; 77 CORE_ADDR text_end = 0; 78 #endif 79 80 /* Forward decl */ 81 82 extern struct target_ops exec_ops; 83 84 /* ARGSUSED */ 85 static void 86 exec_close (quitting) 87 int quitting; 88 { 89 if (exec_bfd) { 90 bfd_close (exec_bfd); 91 exec_bfd = NULL; 92 } 93 if (exec_ops.to_sections) { 94 free ((PTR)exec_ops.to_sections); 95 exec_ops.to_sections = NULL; 96 exec_ops.to_sections_end = NULL; 97 } 98 } 99 100 /* Process the first arg in ARGS as the new exec file. 101 102 Note that we have to explicitly ignore additional args, since we can 103 be called from file_command(), which also calls symbol_file_command() 104 which can take multiple args. */ 105 106 void 107 exec_file_command (args, from_tty) 108 char *args; 109 int from_tty; 110 { 111 char **argv; 112 char *filename; 113 114 target_preopen (from_tty); 115 116 /* Remove any previous exec file. */ 117 unpush_target (&exec_ops); 118 119 /* Now open and digest the file the user requested, if any. */ 120 121 if (args) 122 { 123 char *scratch_pathname; 124 int scratch_chan; 125 126 /* Scan through the args and pick up the first non option arg 127 as the filename. */ 128 129 if ((argv = buildargv (args)) == NULL) 130 { 131 nomem (0); 132 } 133 make_cleanup (freeargv, (char *) argv); 134 135 for (; (*argv != NULL) && (**argv == '-'); argv++) {;} 136 if (*argv == NULL) 137 { 138 error ("no exec file name was specified"); 139 } 140 141 filename = tilde_expand (*argv); 142 make_cleanup (free, filename); 143 144 scratch_chan = openp (getenv ("PATH"), 1, filename, 145 write_files? O_RDWR|O_BINARY: O_RDONLY|O_BINARY, 0, 146 &scratch_pathname); 147 if (scratch_chan < 0) 148 perror_with_name (filename); 149 150 exec_bfd = bfd_fdopenr (scratch_pathname, NULL, 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 if (!bfd_check_format (exec_bfd, bfd_object)) 155 error ("\"%s\": not in executable format: %s.", 156 scratch_pathname, bfd_errmsg (bfd_error)); 157 158 if (build_section_table (exec_bfd, &exec_ops.to_sections, 159 &exec_ops.to_sections_end)) 160 error ("Can't find the file sections in `%s': %s", 161 exec_bfd->filename, bfd_errmsg (bfd_error)); 162 163 #ifdef NEED_TEXT_START_END 164 /* This is a KLUDGE (FIXME) because a few places in a few ports 165 (29K springs to mind) need this info for now. */ 166 { 167 struct section_table *p; 168 for (p = exec_ops.to_sections; p < exec_ops.to_sections_end; p++) 169 if (!strcmp (".text", bfd_section_name (p->bfd, p->sec_ptr))) 170 { 171 text_start = p->addr; 172 text_end = p->endaddr; 173 break; 174 } 175 } 176 #endif 177 178 validate_files (); 179 180 push_target (&exec_ops); 181 182 /* Tell display code (if any) about the changed file name. */ 183 if (exec_file_display_hook) 184 (*exec_file_display_hook) (filename); 185 #ifdef KERNELDEBUG 186 /* Kludge to set kernel debugging for "vmunix" exec file. 187 It's easy to forget -k. */ 188 if (!kernel_debugging && is_a_vmunix(filename)) 189 { 190 kernel_debugging = 1; 191 set_prompt("(kgdb) "); 192 } 193 #endif 194 } 195 else if (from_tty) 196 printf ("No exec file now.\n"); 197 } 198 199 /* Set both the exec file and the symbol file, in one command. 200 What a novelty. Why did GDB go through four major releases before this 201 command was added? */ 202 203 static void 204 file_command (arg, from_tty) 205 char *arg; 206 int from_tty; 207 { 208 /* FIXME, if we lose on reading the symbol file, we should revert 209 the exec file, but that's rough. */ 210 exec_file_command (arg, from_tty); 211 symbol_file_command (arg, from_tty); 212 } 213 214 215 /* Locate all mappable sections of a BFD file. 216 table_pp_char is a char * to get it through bfd_map_over_sections; 217 we cast it back to its proper type. */ 218 219 static void 220 add_to_section_table (abfd, asect, table_pp_char) 221 bfd *abfd; 222 sec_ptr asect; 223 PTR table_pp_char; 224 { 225 struct section_table **table_pp = (struct section_table **)table_pp_char; 226 flagword aflag; 227 228 aflag = bfd_get_section_flags (abfd, asect); 229 /* FIXME, we need to handle BSS segment here...it alloc's but doesn't load */ 230 if (!(aflag & SEC_LOAD)) 231 return; 232 if (0 == bfd_section_size (abfd, asect)) 233 return; 234 (*table_pp)->bfd = abfd; 235 (*table_pp)->sec_ptr = asect; 236 (*table_pp)->addr = bfd_section_vma (abfd, asect); 237 (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect); 238 (*table_pp)++; 239 } 240 241 /* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR. 242 Returns 0 if OK, 1 on error. */ 243 244 int 245 build_section_table (some_bfd, start, end) 246 bfd *some_bfd; 247 struct section_table **start, **end; 248 { 249 unsigned count; 250 251 count = bfd_count_sections (some_bfd); 252 if (*start) 253 free ((PTR)*start); 254 *start = (struct section_table *) xmalloc (count * sizeof (**start)); 255 *end = *start; 256 bfd_map_over_sections (some_bfd, add_to_section_table, (char *)end); 257 if (*end > *start + count) 258 abort(); 259 /* We could realloc the table, but it probably loses for most files. */ 260 return 0; 261 } 262 263 /* Read or write the exec file. 264 265 Args are address within a BFD file, address within gdb address-space, 266 length, and a flag indicating whether to read or write. 267 268 Result is a length: 269 270 0: We cannot handle this address and length. 271 > 0: We have handled N bytes starting at this address. 272 (If N == length, we did it all.) We might be able 273 to handle more bytes beyond this length, but no 274 promises. 275 < 0: We cannot handle this address, but if somebody 276 else handles (-N) bytes, we can start from there. 277 278 The same routine is used to handle both core and exec files; 279 we just tail-call it with more arguments to select between them. */ 280 281 int 282 xfer_memory (memaddr, myaddr, len, write, target) 283 CORE_ADDR memaddr; 284 char *myaddr; 285 int len; 286 int write; 287 struct target_ops *target; 288 { 289 boolean res; 290 struct section_table *p; 291 CORE_ADDR nextsectaddr, memend; 292 boolean (*xfer_fn) PARAMS ((bfd *, sec_ptr, PTR, file_ptr, bfd_size_type)); 293 294 if (len <= 0) 295 abort(); 296 297 memend = memaddr + len; 298 xfer_fn = write? bfd_set_section_contents: bfd_get_section_contents; 299 nextsectaddr = memend; 300 301 for (p = target->to_sections; p < target->to_sections_end; p++) 302 { 303 if (p->addr <= memaddr) 304 if (p->endaddr >= memend) 305 { 306 /* Entire transfer is within this section. */ 307 res = xfer_fn (p->bfd, p->sec_ptr, myaddr, memaddr - p->addr, len); 308 return (res != false)? len: 0; 309 } 310 else if (p->endaddr <= memaddr) 311 { 312 /* This section ends before the transfer starts. */ 313 continue; 314 } 315 else 316 { 317 /* This section overlaps the transfer. Just do half. */ 318 len = p->endaddr - memaddr; 319 res = xfer_fn (p->bfd, p->sec_ptr, myaddr, memaddr - p->addr, len); 320 return (res != false)? len: 0; 321 } 322 else if (p->addr < nextsectaddr) 323 nextsectaddr = p->addr; 324 } 325 326 if (nextsectaddr >= memend) 327 return 0; /* We can't help */ 328 else 329 return - (nextsectaddr - memaddr); /* Next boundary where we can help */ 330 } 331 332 #ifdef FIXME 333 #ifdef REG_STACK_SEGMENT 334 /* MOVE TO BFD... */ 335 /* Pyramids and AM29000s have an extra segment in the virtual address space 336 for the (control) stack of register-window frames. The AM29000 folk 337 call it the "register stack" rather than the "memory stack". */ 338 else if (memaddr >= reg_stack_start && memaddr < reg_stack_end) 339 { 340 i = min (len, reg_stack_end - memaddr); 341 fileptr = memaddr - reg_stack_start + reg_stack_offset; 342 wanna_xfer = coredata; 343 } 344 #endif /* REG_STACK_SEGMENT */ 345 #endif FIXME 346 347 void 348 print_section_info (t, abfd) 349 struct target_ops *t; 350 bfd *abfd; 351 { 352 struct section_table *p; 353 354 printf_filtered ("\t`%s', ", bfd_get_filename(abfd)); 355 wrap_here (" "); 356 printf_filtered ("file type %s.\n", bfd_get_target(abfd)); 357 358 for (p = t->to_sections; p < t->to_sections_end; p++) { 359 printf_filtered ("\t%s", local_hex_string_custom (p->addr, "08")); 360 printf_filtered (" - %s", local_hex_string_custom (p->endaddr, "08")); 361 if (info_verbose) 362 printf_filtered (" @ %s", 363 local_hex_string_custom (p->sec_ptr->filepos, "08")); 364 printf_filtered (" is %s", bfd_section_name (p->bfd, p->sec_ptr)); 365 if (p->bfd != abfd) { 366 printf_filtered (" in %s", bfd_get_filename (p->bfd)); 367 } 368 printf_filtered ("\n"); 369 } 370 } 371 372 char * 373 get_exec_file_name() 374 { 375 if (exec_bfd != 0) 376 return (bfd_get_filename(exec_bfd)); 377 return (0); 378 } 379 380 static void 381 exec_files_info (t) 382 struct target_ops *t; 383 { 384 print_section_info (t, exec_bfd); 385 } 386 387 static void 388 set_section_command (args, from_tty) 389 char *args; 390 int from_tty; 391 { 392 struct section_table *p; 393 char *secname; 394 unsigned seclen; 395 unsigned long secaddr; 396 char secprint[100]; 397 long offset; 398 399 if (args == 0) 400 error ("Must specify section name and its virtual address"); 401 402 /* Parse out section name */ 403 for (secname = args; !isspace(*args); args++) ; 404 seclen = args - secname; 405 406 /* Parse out new virtual address */ 407 secaddr = parse_and_eval_address (args); 408 409 for (p = exec_ops.to_sections; p < exec_ops.to_sections_end; p++) { 410 if (!strncmp (secname, bfd_section_name (exec_bfd, p->sec_ptr), seclen) 411 && bfd_section_name (exec_bfd, p->sec_ptr)[seclen] == '\0') { 412 offset = secaddr - p->addr; 413 p->addr += offset; 414 p->endaddr += offset; 415 if (from_tty) 416 exec_files_info(&exec_ops); 417 return; 418 } 419 } 420 if (seclen >= sizeof (secprint)) 421 seclen = sizeof (secprint) - 1; 422 strncpy (secprint, secname, seclen); 423 secprint[seclen] = '\0'; 424 error ("Section %s not found", secprint); 425 } 426 427 struct target_ops exec_ops = { 428 "exec", "Local exec file", 429 "Use an executable file as a target.\n\ 430 Specify the filename of the executable file.", 431 exec_file_command, exec_close, /* open, close */ 432 find_default_attach, 0, 0, 0, /* attach, detach, resume, wait, */ 433 0, 0, /* fetch_registers, store_registers, */ 434 0, /* prepare_to_store, */ 435 xfer_memory, exec_files_info, 436 0, 0, /* insert_breakpoint, remove_breakpoint, */ 437 0, 0, 0, 0, 0, /* terminal stuff */ 438 0, 0, /* kill, load */ 439 0, /* lookup sym */ 440 find_default_create_inferior, 441 0, /* mourn_inferior */ 442 0, /* can_run */ 443 0, /* notice_signals */ 444 file_stratum, 0, /* next */ 445 0, 1, 0, 0, 0, /* all mem, mem, stack, regs, exec */ 446 0, 0, /* section pointers */ 447 OPS_MAGIC, /* Always the last thing */ 448 }; 449 450 void 451 _initialize_exec() 452 { 453 454 add_com ("file", class_files, file_command, 455 "Use FILE as program to be debugged.\n\ 456 It is read for its symbols, for getting the contents of pure memory,\n\ 457 and it is the program executed when you use the `run' command.\n\ 458 If FILE cannot be found as specified, your execution directory path\n\ 459 ($PATH) is searched for a command of that name.\n\ 460 No arg means to have no executable file and no symbols."); 461 462 add_com ("exec-file", class_files, exec_file_command, 463 "Use FILE as program for getting contents of pure memory.\n\ 464 If FILE cannot be found as specified, your execution directory path\n\ 465 is searched for a command of that name.\n\ 466 No arg means have no executable file."); 467 468 add_com ("section", class_files, set_section_command, 469 "Change the base address of section SECTION of the exec file to ADDR.\n\ 470 This can be used if the exec file does not contain section addresses,\n\ 471 (such as in the a.out format), or when the addresses specified in the\n\ 472 file itself are wrong. Each section must be changed separately. The\n\ 473 ``info files'' command lists all the sections and their addresses."); 474 475 add_show_from_set 476 (add_set_cmd ("write", class_support, var_boolean, (char *)&write_files, 477 "Set writing into executable and core files.", 478 &setlist), 479 &showlist); 480 481 add_target (&exec_ops); 482 } 483