fix calls to find_target_beneath
[deliverable/binutils-gdb.git] / gdb / exec.c
CommitLineData
c906108c 1/* Work with executable files, for GDB.
4646aa9d 2
ecd75fc8 3 Copyright (C) 1988-2014 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
21#include "frame.h"
22#include "inferior.h"
23#include "target.h"
24#include "gdbcmd.h"
25#include "language.h"
0ba1096a 26#include "filenames.h"
c906108c
SS
27#include "symfile.h"
28#include "objfiles.h"
c5f0f3d0 29#include "completer.h"
fd0407d6 30#include "value.h"
4646aa9d 31#include "exec.h"
ea53e89f 32#include "observer.h"
dacec2a8 33#include "arch-utils.h"
6c95b8df
PA
34#include "gdbthread.h"
35#include "progspace.h"
cbb099e8 36#include "gdb_bfd.h"
b427c1bc 37#include "gcore.h"
c906108c 38
c906108c 39#include <fcntl.h>
dbda9972 40#include "readline/readline.h"
0e9f083f 41#include <string.h>
c906108c
SS
42
43#include "gdbcore.h"
44
45#include <ctype.h>
53ce3c39 46#include <sys/stat.h>
c906108c 47
9a4105ab 48void (*deprecated_file_changed_hook) (char *);
c906108c
SS
49
50/* Prototypes for local functions */
51
a14ed312 52static void file_command (char *, int);
c906108c 53
a14ed312 54static void set_section_command (char *, int);
c906108c 55
a14ed312 56static void exec_files_info (struct target_ops *);
c906108c 57
a14ed312 58static void init_exec_ops (void);
c906108c 59
a14ed312 60void _initialize_exec (void);
c906108c 61
c906108c
SS
62/* The target vector for executable files. */
63
64struct target_ops exec_ops;
65
6c95b8df
PA
66/* True if the exec target is pushed on the stack. */
67static int using_exec_ops;
07b82ea5 68
c906108c
SS
69/* Whether to open exec and core files read-only or read-write. */
70
71int write_files = 0;
920d2a44
AC
72static void
73show_write_files (struct ui_file *file, int from_tty,
74 struct cmd_list_element *c, const char *value)
75{
76 fprintf_filtered (file, _("Writing into executable and core files is %s.\n"),
77 value);
78}
79
c906108c 80
4c42eaff 81static void
1adeb98a
FN
82exec_open (char *args, int from_tty)
83{
84 target_preopen (from_tty);
85 exec_file_attach (args, from_tty);
86}
87
07b82ea5
PA
88/* Close and clear exec_bfd. If we end up with no target sections to
89 read memory from, this unpushes the exec_ops target. */
90
6c95b8df
PA
91void
92exec_close (void)
07b82ea5
PA
93{
94 if (exec_bfd)
95 {
96 bfd *abfd = exec_bfd;
07b82ea5 97
cbb099e8 98 gdb_bfd_unref (abfd);
07b82ea5
PA
99
100 /* Removing target sections may close the exec_ops target.
101 Clear exec_bfd before doing so to prevent recursion. */
102 exec_bfd = NULL;
103 exec_bfd_mtime = 0;
104
046ac79f 105 remove_target_sections (&exec_bfd);
1f0c4988
JK
106
107 xfree (exec_filename);
108 exec_filename = NULL;
07b82ea5
PA
109 }
110}
111
6c95b8df
PA
112/* This is the target_close implementation. Clears all target
113 sections and closes all executable bfds from all program spaces. */
114
c906108c 115static void
de90e03d 116exec_close_1 (struct target_ops *self)
c906108c 117{
6c95b8df
PA
118 using_exec_ops = 0;
119
6c95b8df
PA
120 {
121 struct program_space *ss;
122 struct cleanup *old_chain;
c906108c 123
6c95b8df
PA
124 old_chain = save_current_program_space ();
125 ALL_PSPACES (ss)
126 {
127 set_current_program_space (ss);
a5b1fd27 128 clear_section_table (current_target_sections);
6c95b8df
PA
129 exec_close ();
130 }
131
132 do_cleanups (old_chain);
133 }
c906108c
SS
134}
135
1adeb98a
FN
136void
137exec_file_clear (int from_tty)
138{
139 /* Remove exec file. */
6c95b8df 140 exec_close ();
1adeb98a
FN
141
142 if (from_tty)
a3f17187 143 printf_unfiltered (_("No executable file now.\n"));
1adeb98a
FN
144}
145
907083d1 146/* Set FILENAME as the new exec file.
c906108c 147
c5aa993b
JM
148 This function is intended to be behave essentially the same
149 as exec_file_command, except that the latter will detect when
150 a target is being debugged, and will ask the user whether it
151 should be shut down first. (If the answer is "no", then the
152 new file is ignored.)
c906108c 153
c5aa993b
JM
154 This file is used by exec_file_command, to do the work of opening
155 and processing the exec file after any prompting has happened.
c906108c 156
c5aa993b
JM
157 And, it is used by child_attach, when the attach command was
158 given a pid but not a exec pathname, and the attach command could
159 figure out the pathname from the pid. (In this case, we shouldn't
160 ask the user whether the current target should be shut down --
907083d1 161 we're supplying the exec pathname late for good reason.) */
c906108c
SS
162
163void
1adeb98a 164exec_file_attach (char *filename, int from_tty)
c906108c 165{
9b333ba3
TT
166 struct cleanup *cleanups;
167
168 /* First, acquire a reference to the current exec_bfd. We release
169 this at the end of the function; but acquiring it now lets the
170 BFD cache return it if this call refers to the same file. */
171 gdb_bfd_ref (exec_bfd);
172 cleanups = make_cleanup_bfd_unref (exec_bfd);
173
c906108c 174 /* Remove any previous exec file. */
6c95b8df 175 exec_close ();
c906108c
SS
176
177 /* Now open and digest the file the user requested, if any. */
178
1adeb98a
FN
179 if (!filename)
180 {
181 if (from_tty)
a3f17187 182 printf_unfiltered (_("No executable file now.\n"));
7a107747
DJ
183
184 set_gdbarch_from_file (NULL);
1adeb98a
FN
185 }
186 else
c906108c 187 {
1f0c4988 188 char *scratch_pathname, *canonical_pathname;
c906108c 189 int scratch_chan;
07b82ea5 190 struct target_section *sections = NULL, *sections_end = NULL;
d18b8b7a 191 char **matching;
c5aa993b 192
492c0ab7 193 scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, filename,
fbdebf46 194 write_files ? O_RDWR | O_BINARY : O_RDONLY | O_BINARY,
c906108c 195 &scratch_pathname);
cfc3008e 196#if defined(__GO32__) || defined(_WIN32) || defined(__CYGWIN__)
c906108c 197 if (scratch_chan < 0)
c5aa993b
JM
198 {
199 char *exename = alloca (strlen (filename) + 5);
d7f9d729 200
c5aa993b 201 strcat (strcpy (exename, filename), ".exe");
492c0ab7 202 scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, exename,
fbdebf46 203 write_files ? O_RDWR | O_BINARY : O_RDONLY | O_BINARY,
014d698b 204 &scratch_pathname);
c5aa993b 205 }
c906108c 206#endif
17d6efc4
JB
207 if (scratch_chan < 0)
208 perror_with_name (filename);
a4453b7e 209
9b333ba3 210 make_cleanup (xfree, scratch_pathname);
a4453b7e 211
1f0c4988
JK
212 /* gdb_bfd_open (and its variants) prefers canonicalized pathname for
213 better BFD caching. */
214 canonical_pathname = gdb_realpath (scratch_pathname);
215 make_cleanup (xfree, canonical_pathname);
216
1c00ec6b 217 if (write_files)
1f0c4988 218 exec_bfd = gdb_bfd_fopen (canonical_pathname, gnutarget,
1c00ec6b
TT
219 FOPEN_RUB, scratch_chan);
220 else
1f0c4988 221 exec_bfd = gdb_bfd_open (canonical_pathname, gnutarget, scratch_chan);
c906108c
SS
222
223 if (!exec_bfd)
9fe4a216 224 {
9fe4a216
TT
225 error (_("\"%s\": could not open as an executable file: %s"),
226 scratch_pathname, bfd_errmsg (bfd_get_error ()));
227 }
c906108c 228
1f0c4988 229 gdb_assert (exec_filename == NULL);
4856b6bc 230 exec_filename = gdb_realpath_keepfile (scratch_pathname);
1f0c4988 231
d18b8b7a 232 if (!bfd_check_format_matches (exec_bfd, bfd_object, &matching))
c906108c
SS
233 {
234 /* Make sure to close exec_bfd, or else "run" might try to use
235 it. */
6c95b8df 236 exec_close ();
8a3fe4f8 237 error (_("\"%s\": not in executable format: %s"),
d18b8b7a
HZ
238 scratch_pathname,
239 gdb_bfd_errmsg (bfd_get_error (), matching));
c906108c
SS
240 }
241
07b82ea5 242 if (build_section_table (exec_bfd, &sections, &sections_end))
c906108c
SS
243 {
244 /* Make sure to close exec_bfd, or else "run" might try to use
245 it. */
6c95b8df 246 exec_close ();
8a3fe4f8 247 error (_("\"%s\": can't find the file sections: %s"),
c906108c
SS
248 scratch_pathname, bfd_errmsg (bfd_get_error ()));
249 }
250
c04ea773
DJ
251 exec_bfd_mtime = bfd_get_mtime (exec_bfd);
252
c906108c
SS
253 validate_files ();
254
255 set_gdbarch_from_file (exec_bfd);
256
07b82ea5 257 /* Add the executable's sections to the current address spaces'
6c95b8df
PA
258 list of sections. This possibly pushes the exec_ops
259 target. */
ed9eebaf 260 add_target_sections (&exec_bfd, sections, sections_end);
07b82ea5 261 xfree (sections);
c906108c
SS
262
263 /* Tell display code (if any) about the changed file name. */
9a4105ab
AC
264 if (deprecated_exec_file_display_hook)
265 (*deprecated_exec_file_display_hook) (filename);
c906108c 266 }
9b333ba3
TT
267
268 do_cleanups (cleanups);
269
ce7d4522 270 bfd_cache_close_all ();
781b42b0 271 observer_notify_executable_changed ();
c906108c
SS
272}
273
274/* Process the first arg in ARGS as the new exec file.
275
c5aa993b
JM
276 Note that we have to explicitly ignore additional args, since we can
277 be called from file_command(), which also calls symbol_file_command()
1adeb98a
FN
278 which can take multiple args.
279
0963b4bd 280 If ARGS is NULL, we just want to close the exec file. */
c906108c 281
1adeb98a 282static void
fba45db2 283exec_file_command (char *args, int from_tty)
c906108c 284{
1adeb98a
FN
285 char **argv;
286 char *filename;
4c42eaff
DJ
287
288 if (from_tty && target_has_execution
289 && !query (_("A program is being debugged already.\n"
290 "Are you sure you want to change the file? ")))
291 error (_("File not changed."));
1adeb98a
FN
292
293 if (args)
294 {
f7545552
TT
295 struct cleanup *cleanups;
296
1adeb98a
FN
297 /* Scan through the args and pick up the first non option arg
298 as the filename. */
299
d1a41061 300 argv = gdb_buildargv (args);
f7545552 301 cleanups = make_cleanup_freeargv (argv);
1adeb98a
FN
302
303 for (; (*argv != NULL) && (**argv == '-'); argv++)
304 {;
305 }
306 if (*argv == NULL)
8a3fe4f8 307 error (_("No executable file name was specified"));
1adeb98a
FN
308
309 filename = tilde_expand (*argv);
310 make_cleanup (xfree, filename);
311 exec_file_attach (filename, from_tty);
f7545552
TT
312
313 do_cleanups (cleanups);
1adeb98a
FN
314 }
315 else
316 exec_file_attach (NULL, from_tty);
c906108c
SS
317}
318
0963b4bd 319/* Set both the exec file and the symbol file, in one command.
c906108c
SS
320 What a novelty. Why did GDB go through four major releases before this
321 command was added? */
322
323static void
fba45db2 324file_command (char *arg, int from_tty)
c906108c
SS
325{
326 /* FIXME, if we lose on reading the symbol file, we should revert
327 the exec file, but that's rough. */
328 exec_file_command (arg, from_tty);
329 symbol_file_command (arg, from_tty);
9a4105ab
AC
330 if (deprecated_file_changed_hook)
331 deprecated_file_changed_hook (arg);
c906108c 332}
c906108c 333\f
c5aa993b 334
0963b4bd 335/* Locate all mappable sections of a BFD file.
c906108c
SS
336 table_pp_char is a char * to get it through bfd_map_over_sections;
337 we cast it back to its proper type. */
338
339static void
7be0c536
AC
340add_to_section_table (bfd *abfd, struct bfd_section *asect,
341 void *table_pp_char)
c906108c 342{
0542c86d 343 struct target_section **table_pp = (struct target_section **) table_pp_char;
c906108c
SS
344 flagword aflag;
345
2b2848e2
DE
346 gdb_assert (abfd == asect->owner);
347
0f5d55d8
JB
348 /* Check the section flags, but do not discard zero-length sections, since
349 some symbols may still be attached to this section. For instance, we
350 encountered on sparc-solaris 2.10 a shared library with an empty .bss
351 section to which a symbol named "_end" was attached. The address
352 of this symbol still needs to be relocated. */
c906108c
SS
353 aflag = bfd_get_section_flags (abfd, asect);
354 if (!(aflag & SEC_ALLOC))
355 return;
0f5d55d8 356
046ac79f 357 (*table_pp)->owner = NULL;
c906108c
SS
358 (*table_pp)->the_bfd_section = asect;
359 (*table_pp)->addr = bfd_section_vma (abfd, asect);
360 (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect);
361 (*table_pp)++;
362}
363
a5b1fd27
DE
364/* See exec.h. */
365
366void
367clear_section_table (struct target_section_table *table)
368{
369 xfree (table->sections);
370 table->sections = table->sections_end = NULL;
371}
372
373/* Resize section table TABLE by ADJUSTMENT.
374 ADJUSTMENT may be negative, in which case the caller must have already
375 removed the sections being deleted.
376 Returns the old size. */
377
378static int
379resize_section_table (struct target_section_table *table, int adjustment)
07b82ea5 380{
07b82ea5
PA
381 int old_count;
382 int new_count;
383
07b82ea5
PA
384 old_count = table->sections_end - table->sections;
385
a5b1fd27 386 new_count = adjustment + old_count;
07b82ea5
PA
387
388 if (new_count)
389 {
390 table->sections = xrealloc (table->sections,
391 sizeof (struct target_section) * new_count);
392 table->sections_end = table->sections + new_count;
393 }
394 else
a5b1fd27 395 clear_section_table (table);
07b82ea5
PA
396
397 return old_count;
398}
399
c906108c
SS
400/* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR.
401 Returns 0 if OK, 1 on error. */
402
403int
0542c86d
PA
404build_section_table (struct bfd *some_bfd, struct target_section **start,
405 struct target_section **end)
c906108c
SS
406{
407 unsigned count;
408
409 count = bfd_count_sections (some_bfd);
410 if (*start)
b8c9b27d 411 xfree (* start);
0542c86d 412 *start = (struct target_section *) xmalloc (count * sizeof (**start));
c906108c 413 *end = *start;
c5aa993b 414 bfd_map_over_sections (some_bfd, add_to_section_table, (char *) end);
c906108c 415 if (*end > *start + count)
3e43a32a
MS
416 internal_error (__FILE__, __LINE__,
417 _("failed internal consistency check"));
c906108c
SS
418 /* We could realloc the table, but it probably loses for most files. */
419 return 0;
420}
07b82ea5
PA
421
422/* Add the sections array defined by [SECTIONS..SECTIONS_END[ to the
423 current set of target sections. */
424
425void
046ac79f 426add_target_sections (void *owner,
ed9eebaf 427 struct target_section *sections,
07b82ea5
PA
428 struct target_section *sections_end)
429{
430 int count;
431 struct target_section_table *table = current_target_sections;
432
433 count = sections_end - sections;
434
435 if (count > 0)
436 {
437 int space = resize_section_table (table, count);
ed9eebaf 438 int i;
d7f9d729 439
ed9eebaf
TT
440 for (i = 0; i < count; ++i)
441 {
442 table->sections[space + i] = sections[i];
046ac79f 443 table->sections[space + i].owner = owner;
ed9eebaf 444 }
07b82ea5
PA
445
446 /* If these are the first file sections we can provide memory
447 from, push the file_stratum target. */
6c95b8df
PA
448 if (!using_exec_ops)
449 {
450 using_exec_ops = 1;
451 push_target (&exec_ops);
452 }
07b82ea5
PA
453 }
454}
455
76ad5e1e
NB
456/* Add the sections of OBJFILE to the current set of target sections. */
457
458void
459add_target_sections_of_objfile (struct objfile *objfile)
460{
461 struct target_section_table *table = current_target_sections;
462 struct obj_section *osect;
463 int space;
464 unsigned count = 0;
465 struct target_section *ts;
466
467 if (objfile == NULL)
468 return;
469
470 /* Compute the number of sections to add. */
471 ALL_OBJFILE_OSECTIONS (objfile, osect)
472 {
473 if (bfd_get_section_size (osect->the_bfd_section) == 0)
474 continue;
475 count++;
476 }
477
478 if (count == 0)
479 return;
480
481 space = resize_section_table (table, count);
482
483 ts = table->sections + space;
484
485 ALL_OBJFILE_OSECTIONS (objfile, osect)
486 {
487 if (bfd_get_section_size (osect->the_bfd_section) == 0)
488 continue;
489
490 gdb_assert (ts < table->sections + space + count);
491
492 ts->addr = obj_section_addr (osect);
493 ts->endaddr = obj_section_endaddr (osect);
494 ts->the_bfd_section = osect->the_bfd_section;
495 ts->owner = (void *) objfile;
496
497 ts++;
498 }
499}
500
046ac79f
JK
501/* Remove all target sections owned by OWNER.
502 OWNER must be the same value passed to add_target_sections. */
07b82ea5
PA
503
504void
046ac79f 505remove_target_sections (void *owner)
07b82ea5
PA
506{
507 struct target_section *src, *dest;
07b82ea5
PA
508 struct target_section_table *table = current_target_sections;
509
046ac79f
JK
510 gdb_assert (owner != NULL);
511
07b82ea5
PA
512 dest = table->sections;
513 for (src = table->sections; src < table->sections_end; src++)
046ac79f 514 if (src->owner != owner)
07b82ea5
PA
515 {
516 /* Keep this section. */
517 if (dest < src)
518 *dest = *src;
519 dest++;
520 }
521
522 /* If we've dropped any sections, resize the section table. */
523 if (dest < src)
524 {
525 int old_count;
526
527 old_count = resize_section_table (table, dest - src);
528
529 /* If we don't have any more sections to read memory from,
530 remove the file_stratum target from the stack. */
531 if (old_count + (dest - src) == 0)
6c95b8df
PA
532 {
533 struct program_space *pspace;
534
535 ALL_PSPACES (pspace)
536 if (pspace->target_sections.sections
537 != pspace->target_sections.sections_end)
538 return;
539
540 unpush_target (&exec_ops);
541 }
07b82ea5
PA
542 }
543}
544
c906108c 545\f
348f8c02 546
1ca49d37
YQ
547enum target_xfer_status
548exec_read_partial_read_only (gdb_byte *readbuf, ULONGEST offset,
549 ULONGEST len, ULONGEST *xfered_len)
550{
551 /* It's unduly pedantic to refuse to look at the executable for
552 read-only pieces; so do the equivalent of readonly regions aka
553 QTro packet. */
554 if (exec_bfd != NULL)
555 {
556 asection *s;
557 bfd_size_type size;
558 bfd_vma vma;
559
560 for (s = exec_bfd->sections; s; s = s->next)
561 {
562 if ((s->flags & SEC_LOAD) == 0
563 || (s->flags & SEC_READONLY) == 0)
564 continue;
565
566 vma = s->vma;
567 size = bfd_get_section_size (s);
568 if (vma <= offset && offset < (vma + size))
569 {
570 ULONGEST amt;
571
572 amt = (vma + size) - offset;
573 if (amt > len)
574 amt = len;
575
576 amt = bfd_get_section_contents (exec_bfd, s,
577 readbuf, offset - vma, amt);
578
579 if (amt == 0)
580 return TARGET_XFER_EOF;
581 else
582 {
583 *xfered_len = amt;
584 return TARGET_XFER_OK;
585 }
586 }
587 }
588 }
589
590 /* Indicate failure to find the requested memory block. */
591 return TARGET_XFER_E_IO;
592}
593
5a2eb0ef
YQ
594/* Appends all read-only memory ranges found in the target section
595 table defined by SECTIONS and SECTIONS_END, starting at (and
596 intersected with) MEMADDR for LEN bytes. Returns the augmented
597 VEC. */
598
599static VEC(mem_range_s) *
e6ca34fc 600section_table_available_memory (VEC(mem_range_s) *memory,
424447ee 601 CORE_ADDR memaddr, ULONGEST len,
e6ca34fc
PA
602 struct target_section *sections,
603 struct target_section *sections_end)
604{
605 struct target_section *p;
e6ca34fc
PA
606
607 for (p = sections; p < sections_end; p++)
608 {
2b2848e2
DE
609 if ((bfd_get_section_flags (p->the_bfd_section->owner,
610 p->the_bfd_section)
e6ca34fc
PA
611 & SEC_READONLY) == 0)
612 continue;
613
614 /* Copy the meta-data, adjusted. */
615 if (mem_ranges_overlap (p->addr, p->endaddr - p->addr, memaddr, len))
616 {
617 ULONGEST lo1, hi1, lo2, hi2;
618 struct mem_range *r;
619
620 lo1 = memaddr;
621 hi1 = memaddr + len;
622
623 lo2 = p->addr;
624 hi2 = p->endaddr;
625
626 r = VEC_safe_push (mem_range_s, memory, NULL);
627
628 r->start = max (lo1, lo2);
629 r->length = min (hi1, hi2) - r->start;
630 }
631 }
632
633 return memory;
634}
635
1ee79381
YQ
636enum target_xfer_status
637section_table_read_available_memory (gdb_byte *readbuf, ULONGEST offset,
638 ULONGEST len, ULONGEST *xfered_len)
639{
640 VEC(mem_range_s) *available_memory = NULL;
641 struct target_section_table *table;
642 struct cleanup *old_chain;
643 mem_range_s *r;
644 int i;
645
646 table = target_get_section_table (&exec_ops);
647 available_memory = section_table_available_memory (available_memory,
648 offset, len,
649 table->sections,
650 table->sections_end);
651
652 old_chain = make_cleanup (VEC_cleanup(mem_range_s),
653 &available_memory);
654
655 normalize_mem_ranges (available_memory);
656
657 for (i = 0;
658 VEC_iterate (mem_range_s, available_memory, i, r);
659 i++)
660 {
661 if (mem_ranges_overlap (r->start, r->length, offset, len))
662 {
663 CORE_ADDR end;
664 enum target_xfer_status status;
665
666 /* Get the intersection window. */
667 end = min (offset + len, r->start + r->length);
668
669 gdb_assert (end - offset <= len);
670
671 if (offset >= r->start)
672 status = exec_read_partial_read_only (readbuf, offset,
673 end - offset,
674 xfered_len);
675 else
676 {
677 *xfered_len = r->start - offset;
bc113b4e 678 status = TARGET_XFER_UNAVAILABLE;
1ee79381
YQ
679 }
680 do_cleanups (old_chain);
681 return status;
682 }
683 }
684 do_cleanups (old_chain);
685
686 *xfered_len = len;
bc113b4e 687 return TARGET_XFER_UNAVAILABLE;
1ee79381
YQ
688}
689
9b409511 690enum target_xfer_status
07b82ea5 691section_table_xfer_memory_partial (gdb_byte *readbuf, const gdb_byte *writebuf,
b55e14c7 692 ULONGEST offset, ULONGEST len,
9b409511 693 ULONGEST *xfered_len,
07b82ea5
PA
694 struct target_section *sections,
695 struct target_section *sections_end,
696 const char *section_name)
c906108c 697{
020cc13c 698 int res;
0542c86d 699 struct target_section *p;
07b82ea5
PA
700 ULONGEST memaddr = offset;
701 ULONGEST memend = memaddr + len;
c906108c 702
b55e14c7 703 if (len == 0)
3e43a32a
MS
704 internal_error (__FILE__, __LINE__,
705 _("failed internal consistency check"));
c906108c 706
348f8c02 707 for (p = sections; p < sections_end; p++)
c906108c 708 {
2b2848e2
DE
709 struct bfd_section *asect = p->the_bfd_section;
710 bfd *abfd = asect->owner;
711
712 if (section_name && strcmp (section_name, asect->name) != 0)
0963b4bd 713 continue; /* not the section we need. */
c906108c 714 if (memaddr >= p->addr)
3db26b01
JB
715 {
716 if (memend <= p->endaddr)
717 {
718 /* Entire transfer is within this section. */
07b82ea5 719 if (writebuf)
2b2848e2 720 res = bfd_set_section_contents (abfd, asect,
07b82ea5 721 writebuf, memaddr - p->addr,
85302095
AC
722 len);
723 else
2b2848e2 724 res = bfd_get_section_contents (abfd, asect,
07b82ea5 725 readbuf, memaddr - p->addr,
85302095 726 len);
9b409511
YQ
727
728 if (res != 0)
729 {
730 *xfered_len = len;
731 return TARGET_XFER_OK;
732 }
733 else
734 return TARGET_XFER_EOF;
3db26b01
JB
735 }
736 else if (memaddr >= p->endaddr)
737 {
738 /* This section ends before the transfer starts. */
739 continue;
740 }
741 else
742 {
743 /* This section overlaps the transfer. Just do half. */
744 len = p->endaddr - memaddr;
07b82ea5 745 if (writebuf)
2b2848e2 746 res = bfd_set_section_contents (abfd, asect,
07b82ea5 747 writebuf, memaddr - p->addr,
85302095
AC
748 len);
749 else
2b2848e2 750 res = bfd_get_section_contents (abfd, asect,
07b82ea5 751 readbuf, memaddr - p->addr,
85302095 752 len);
9b409511
YQ
753 if (res != 0)
754 {
755 *xfered_len = len;
756 return TARGET_XFER_OK;
757 }
758 else
759 return TARGET_XFER_EOF;
3db26b01
JB
760 }
761 }
c906108c
SS
762 }
763
9b409511 764 return TARGET_XFER_EOF; /* We can't help. */
c906108c 765}
348f8c02 766
70221824 767static struct target_section_table *
07b82ea5 768exec_get_section_table (struct target_ops *ops)
348f8c02 769{
07b82ea5 770 return current_target_sections;
348f8c02
PA
771}
772
9b409511 773static enum target_xfer_status
07b82ea5
PA
774exec_xfer_partial (struct target_ops *ops, enum target_object object,
775 const char *annex, gdb_byte *readbuf,
776 const gdb_byte *writebuf,
9b409511 777 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
348f8c02 778{
07b82ea5
PA
779 struct target_section_table *table = target_get_section_table (ops);
780
781 if (object == TARGET_OBJECT_MEMORY)
782 return section_table_xfer_memory_partial (readbuf, writebuf,
9b409511 783 offset, len, xfered_len,
07b82ea5
PA
784 table->sections,
785 table->sections_end,
786 NULL);
787 else
2ed4b548 788 return TARGET_XFER_E_IO;
348f8c02 789}
c906108c 790\f
c5aa993b 791
c906108c 792void
07b82ea5 793print_section_info (struct target_section_table *t, bfd *abfd)
c906108c 794{
5af949e3 795 struct gdbarch *gdbarch = gdbarch_from_bfd (abfd);
0542c86d 796 struct target_section *p;
17a912b6 797 /* FIXME: 16 is not wide enough when gdbarch_addr_bit > 64. */
5af949e3 798 int wid = gdbarch_addr_bit (gdbarch) <= 32 ? 8 : 16;
c906108c 799
c5aa993b 800 printf_filtered ("\t`%s', ", bfd_get_filename (abfd));
c906108c 801 wrap_here (" ");
a3f17187 802 printf_filtered (_("file type %s.\n"), bfd_get_target (abfd));
c906108c 803 if (abfd == exec_bfd)
51bee8e9 804 {
3e43a32a
MS
805 /* gcc-3.4 does not like the initialization in
806 <p == t->sections_end>. */
d904de5b 807 bfd_vma displacement = 0;
2f1bdd26 808 bfd_vma entry_point;
51bee8e9
JK
809
810 for (p = t->sections; p < t->sections_end; p++)
811 {
2b2848e2
DE
812 struct bfd_section *psect = p->the_bfd_section;
813 bfd *pbfd = psect->owner;
51bee8e9 814
2b2848e2 815 if ((bfd_get_section_flags (pbfd, psect) & (SEC_ALLOC | SEC_LOAD))
51bee8e9
JK
816 != (SEC_ALLOC | SEC_LOAD))
817 continue;
818
2b2848e2
DE
819 if (bfd_get_section_vma (pbfd, psect) <= abfd->start_address
820 && abfd->start_address < (bfd_get_section_vma (pbfd, psect)
821 + bfd_get_section_size (psect)))
51bee8e9 822 {
2b2848e2 823 displacement = p->addr - bfd_get_section_vma (pbfd, psect);
51bee8e9
JK
824 break;
825 }
826 }
827 if (p == t->sections_end)
b37520b6 828 warning (_("Cannot find section for the entry point of %s."),
d904de5b 829 bfd_get_filename (abfd));
51bee8e9 830
2f1bdd26
MGD
831 entry_point = gdbarch_addr_bits_remove (gdbarch,
832 bfd_get_start_address (abfd)
833 + displacement);
51bee8e9 834 printf_filtered (_("\tEntry point: %s\n"),
2f1bdd26 835 paddress (gdbarch, entry_point));
51bee8e9 836 }
07b82ea5 837 for (p = t->sections; p < t->sections_end; p++)
c906108c 838 {
2b2848e2
DE
839 struct bfd_section *psect = p->the_bfd_section;
840 bfd *pbfd = psect->owner;
841
bb599908
PH
842 printf_filtered ("\t%s", hex_string_custom (p->addr, wid));
843 printf_filtered (" - %s", hex_string_custom (p->endaddr, wid));
bcf16802
KB
844
845 /* FIXME: A format of "08l" is not wide enough for file offsets
846 larger than 4GB. OTOH, making it "016l" isn't desirable either
847 since most output will then be much wider than necessary. It
848 may make sense to test the size of the file and choose the
849 format string accordingly. */
a3f17187 850 /* FIXME: i18n: Need to rewrite this sentence. */
c906108c
SS
851 if (info_verbose)
852 printf_filtered (" @ %s",
2b2848e2
DE
853 hex_string_custom (psect->filepos, 8));
854 printf_filtered (" is %s", bfd_section_name (pbfd, psect));
855 if (pbfd != abfd)
856 printf_filtered (" in %s", bfd_get_filename (pbfd));
c906108c
SS
857 printf_filtered ("\n");
858 }
859}
860
861static void
fba45db2 862exec_files_info (struct target_ops *t)
c906108c 863{
57008375
JK
864 if (exec_bfd)
865 print_section_info (current_target_sections, exec_bfd);
866 else
867 puts_filtered (_("\t<no file loaded>\n"));
c906108c
SS
868}
869
870static void
fba45db2 871set_section_command (char *args, int from_tty)
c906108c 872{
0542c86d 873 struct target_section *p;
c906108c
SS
874 char *secname;
875 unsigned seclen;
876 unsigned long secaddr;
877 char secprint[100];
878 long offset;
07b82ea5 879 struct target_section_table *table;
c906108c
SS
880
881 if (args == 0)
8a3fe4f8 882 error (_("Must specify section name and its virtual address"));
c906108c 883
0963b4bd 884 /* Parse out section name. */
c5aa993b 885 for (secname = args; !isspace (*args); args++);
c906108c
SS
886 seclen = args - secname;
887
0963b4bd 888 /* Parse out new virtual address. */
c906108c
SS
889 secaddr = parse_and_eval_address (args);
890
07b82ea5
PA
891 table = current_target_sections;
892 for (p = table->sections; p < table->sections_end; p++)
c5aa993b 893 {
57008375 894 if (!strncmp (secname, bfd_section_name (p->bfd,
3e43a32a 895 p->the_bfd_section), seclen)
57008375 896 && bfd_section_name (p->bfd, p->the_bfd_section)[seclen] == '\0')
c5aa993b
JM
897 {
898 offset = secaddr - p->addr;
899 p->addr += offset;
900 p->endaddr += offset;
901 if (from_tty)
902 exec_files_info (&exec_ops);
903 return;
904 }
c906108c 905 }
c906108c
SS
906 if (seclen >= sizeof (secprint))
907 seclen = sizeof (secprint) - 1;
908 strncpy (secprint, secname, seclen);
909 secprint[seclen] = '\0';
8a3fe4f8 910 error (_("Section %s not found"), secprint);
c906108c
SS
911}
912
30510692
DJ
913/* If we can find a section in FILENAME with BFD index INDEX, adjust
914 it to ADDRESS. */
c1bd25fd
DJ
915
916void
917exec_set_section_address (const char *filename, int index, CORE_ADDR address)
918{
0542c86d 919 struct target_section *p;
07b82ea5 920 struct target_section_table *table;
c1bd25fd 921
07b82ea5
PA
922 table = current_target_sections;
923 for (p = table->sections; p < table->sections_end; p++)
c1bd25fd 924 {
2b2848e2 925 if (filename_cmp (filename, p->the_bfd_section->owner->filename) == 0
30510692 926 && index == p->the_bfd_section->index)
c1bd25fd 927 {
30510692 928 p->endaddr += address - p->addr;
c1bd25fd 929 p->addr = address;
c1bd25fd
DJ
930 }
931 }
932}
933
c906108c
SS
934/* If mourn is being called in all the right places, this could be say
935 `gdb internal error' (since generic_mourn calls
936 breakpoint_init_inferior). */
937
938static int
3db08215
MM
939ignore (struct target_ops *ops, struct gdbarch *gdbarch,
940 struct bp_target_info *bp_tgt)
c906108c
SS
941{
942 return 0;
943}
944
c35b1492
PA
945static int
946exec_has_memory (struct target_ops *ops)
947{
948 /* We can provide memory if we have any file/target sections to read
949 from. */
950 return (current_target_sections->sections
951 != current_target_sections->sections_end);
952}
953
83814951
TT
954static char *
955exec_make_note_section (struct target_ops *self, bfd *obfd, int *note_size)
956{
957 error (_("Can't create a corefile"));
958}
be4d1333 959
c906108c
SS
960/* Fill in the exec file target vector. Very few entries need to be
961 defined. */
962
be4d1333 963static void
fba45db2 964init_exec_ops (void)
c906108c
SS
965{
966 exec_ops.to_shortname = "exec";
967 exec_ops.to_longname = "Local exec file";
968 exec_ops.to_doc = "Use an executable file as a target.\n\
969Specify the filename of the executable file.";
1adeb98a 970 exec_ops.to_open = exec_open;
6c95b8df 971 exec_ops.to_close = exec_close_1;
07b82ea5
PA
972 exec_ops.to_xfer_partial = exec_xfer_partial;
973 exec_ops.to_get_section_table = exec_get_section_table;
c906108c
SS
974 exec_ops.to_files_info = exec_files_info;
975 exec_ops.to_insert_breakpoint = ignore;
976 exec_ops.to_remove_breakpoint = ignore;
c906108c 977 exec_ops.to_stratum = file_stratum;
c35b1492 978 exec_ops.to_has_memory = exec_has_memory;
be4d1333 979 exec_ops.to_make_corefile_notes = exec_make_note_section;
b427c1bc 980 exec_ops.to_find_memory_regions = objfile_find_memory_regions;
c5aa993b 981 exec_ops.to_magic = OPS_MAGIC;
c906108c
SS
982}
983
984void
fba45db2 985_initialize_exec (void)
c906108c
SS
986{
987 struct cmd_list_element *c;
988
989 init_exec_ops ();
990
991 if (!dbx_commands)
992 {
1a966eab
AC
993 c = add_cmd ("file", class_files, file_command, _("\
994Use FILE as program to be debugged.\n\
c906108c
SS
995It is read for its symbols, for getting the contents of pure memory,\n\
996and it is the program executed when you use the `run' command.\n\
997If FILE cannot be found as specified, your execution directory path\n\
998($PATH) is searched for a command of that name.\n\
1a966eab 999No arg means to have no executable file and no symbols."), &cmdlist);
5ba2abeb 1000 set_cmd_completer (c, filename_completer);
c906108c
SS
1001 }
1002
1a966eab
AC
1003 c = add_cmd ("exec-file", class_files, exec_file_command, _("\
1004Use FILE as program for getting contents of pure memory.\n\
c906108c
SS
1005If FILE cannot be found as specified, your execution directory path\n\
1006is searched for a command of that name.\n\
1a966eab 1007No arg means have no executable file."), &cmdlist);
5ba2abeb 1008 set_cmd_completer (c, filename_completer);
c906108c 1009
1bedd215
AC
1010 add_com ("section", class_files, set_section_command, _("\
1011Change the base address of section SECTION of the exec file to ADDR.\n\
c906108c
SS
1012This can be used if the exec file does not contain section addresses,\n\
1013(such as in the a.out format), or when the addresses specified in the\n\
1014file itself are wrong. Each section must be changed separately. The\n\
1bedd215 1015``info files'' command lists all the sections and their addresses."));
c906108c 1016
5bf193a2
AC
1017 add_setshow_boolean_cmd ("write", class_support, &write_files, _("\
1018Set writing into executable and core files."), _("\
1019Show writing into executable and core files."), NULL,
1020 NULL,
920d2a44 1021 show_write_files,
5bf193a2 1022 &setlist, &showlist);
c5aa993b 1023
9852c492 1024 add_target_with_completer (&exec_ops, filename_completer);
c906108c 1025}
This page took 1.279111 seconds and 4 git commands to generate.