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