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