Commit | Line | Data |
---|---|---|
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" |
c906108c | 37 | |
c906108c | 38 | #include <fcntl.h> |
dbda9972 | 39 | #include "readline/readline.h" |
0e9f083f | 40 | #include <string.h> |
c906108c SS |
41 | |
42 | #include "gdbcore.h" | |
43 | ||
44 | #include <ctype.h> | |
53ce3c39 | 45 | #include <sys/stat.h> |
c906108c | 46 | |
9a4105ab | 47 | void (*deprecated_file_changed_hook) (char *); |
c906108c SS |
48 | |
49 | /* Prototypes for local functions */ | |
50 | ||
a14ed312 | 51 | static void file_command (char *, int); |
c906108c | 52 | |
a14ed312 | 53 | static void set_section_command (char *, int); |
c906108c | 54 | |
a14ed312 | 55 | static void exec_files_info (struct target_ops *); |
c906108c | 56 | |
a14ed312 | 57 | static void init_exec_ops (void); |
c906108c | 58 | |
a14ed312 | 59 | void _initialize_exec (void); |
c906108c | 60 | |
c906108c SS |
61 | /* The target vector for executable files. */ |
62 | ||
63 | struct target_ops exec_ops; | |
64 | ||
2e73927c TT |
65 | /* Function used to implement to_find_memory_regions. */ |
66 | ||
67 | static int (*exec_do_find_memory_regions) (find_memory_region_ftype, void *); | |
68 | ||
6c95b8df PA |
69 | /* True if the exec target is pushed on the stack. */ |
70 | static int using_exec_ops; | |
07b82ea5 | 71 | |
c906108c SS |
72 | /* Whether to open exec and core files read-only or read-write. */ |
73 | ||
74 | int write_files = 0; | |
920d2a44 AC |
75 | static void |
76 | show_write_files (struct ui_file *file, int from_tty, | |
77 | struct cmd_list_element *c, const char *value) | |
78 | { | |
79 | fprintf_filtered (file, _("Writing into executable and core files is %s.\n"), | |
80 | value); | |
81 | } | |
82 | ||
c906108c | 83 | |
4c42eaff | 84 | static void |
1adeb98a FN |
85 | exec_open (char *args, int from_tty) |
86 | { | |
87 | target_preopen (from_tty); | |
88 | exec_file_attach (args, from_tty); | |
89 | } | |
90 | ||
07b82ea5 PA |
91 | /* Close and clear exec_bfd. If we end up with no target sections to |
92 | read memory from, this unpushes the exec_ops target. */ | |
93 | ||
6c95b8df PA |
94 | void |
95 | exec_close (void) | |
07b82ea5 PA |
96 | { |
97 | if (exec_bfd) | |
98 | { | |
99 | bfd *abfd = exec_bfd; | |
07b82ea5 | 100 | |
cbb099e8 | 101 | gdb_bfd_unref (abfd); |
07b82ea5 PA |
102 | |
103 | /* Removing target sections may close the exec_ops target. | |
104 | Clear exec_bfd before doing so to prevent recursion. */ | |
105 | exec_bfd = NULL; | |
106 | exec_bfd_mtime = 0; | |
107 | ||
046ac79f | 108 | remove_target_sections (&exec_bfd); |
1f0c4988 JK |
109 | |
110 | xfree (exec_filename); | |
111 | exec_filename = NULL; | |
07b82ea5 PA |
112 | } |
113 | } | |
114 | ||
6c95b8df PA |
115 | /* This is the target_close implementation. Clears all target |
116 | sections and closes all executable bfds from all program spaces. */ | |
117 | ||
c906108c | 118 | static void |
de90e03d | 119 | exec_close_1 (struct target_ops *self) |
c906108c | 120 | { |
6c95b8df PA |
121 | using_exec_ops = 0; |
122 | ||
6c95b8df PA |
123 | { |
124 | struct program_space *ss; | |
125 | struct cleanup *old_chain; | |
c906108c | 126 | |
6c95b8df PA |
127 | old_chain = save_current_program_space (); |
128 | ALL_PSPACES (ss) | |
129 | { | |
130 | set_current_program_space (ss); | |
131 | ||
132 | /* Delete all target sections. */ | |
133 | resize_section_table | |
134 | (current_target_sections, | |
135 | -resize_section_table (current_target_sections, 0)); | |
136 | ||
137 | exec_close (); | |
138 | } | |
139 | ||
140 | do_cleanups (old_chain); | |
141 | } | |
c906108c SS |
142 | } |
143 | ||
1adeb98a FN |
144 | void |
145 | exec_file_clear (int from_tty) | |
146 | { | |
147 | /* Remove exec file. */ | |
6c95b8df | 148 | exec_close (); |
1adeb98a FN |
149 | |
150 | if (from_tty) | |
a3f17187 | 151 | printf_unfiltered (_("No executable file now.\n")); |
1adeb98a FN |
152 | } |
153 | ||
907083d1 | 154 | /* Set FILENAME as the new exec file. |
c906108c | 155 | |
c5aa993b JM |
156 | This function is intended to be behave essentially the same |
157 | as exec_file_command, except that the latter will detect when | |
158 | a target is being debugged, and will ask the user whether it | |
159 | should be shut down first. (If the answer is "no", then the | |
160 | new file is ignored.) | |
c906108c | 161 | |
c5aa993b JM |
162 | This file is used by exec_file_command, to do the work of opening |
163 | and processing the exec file after any prompting has happened. | |
c906108c | 164 | |
c5aa993b JM |
165 | And, it is used by child_attach, when the attach command was |
166 | given a pid but not a exec pathname, and the attach command could | |
167 | figure out the pathname from the pid. (In this case, we shouldn't | |
168 | ask the user whether the current target should be shut down -- | |
907083d1 | 169 | we're supplying the exec pathname late for good reason.) */ |
c906108c SS |
170 | |
171 | void | |
1adeb98a | 172 | exec_file_attach (char *filename, int from_tty) |
c906108c | 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 | { |
f7545552 | 188 | struct cleanup *cleanups; |
1f0c4988 | 189 | char *scratch_pathname, *canonical_pathname; |
c906108c | 190 | int scratch_chan; |
07b82ea5 | 191 | struct target_section *sections = NULL, *sections_end = NULL; |
d18b8b7a | 192 | char **matching; |
c5aa993b | 193 | |
492c0ab7 | 194 | scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, filename, |
fbdebf46 | 195 | write_files ? O_RDWR | O_BINARY : O_RDONLY | O_BINARY, |
c906108c | 196 | &scratch_pathname); |
cfc3008e | 197 | #if defined(__GO32__) || defined(_WIN32) || defined(__CYGWIN__) |
c906108c | 198 | if (scratch_chan < 0) |
c5aa993b JM |
199 | { |
200 | char *exename = alloca (strlen (filename) + 5); | |
d7f9d729 | 201 | |
c5aa993b | 202 | strcat (strcpy (exename, filename), ".exe"); |
492c0ab7 | 203 | scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, exename, |
fbdebf46 | 204 | write_files ? O_RDWR | O_BINARY : O_RDONLY | O_BINARY, |
014d698b | 205 | &scratch_pathname); |
c5aa993b | 206 | } |
c906108c | 207 | #endif |
17d6efc4 JB |
208 | if (scratch_chan < 0) |
209 | perror_with_name (filename); | |
a4453b7e TT |
210 | |
211 | cleanups = make_cleanup (xfree, scratch_pathname); | |
212 | ||
1f0c4988 JK |
213 | /* gdb_bfd_open (and its variants) prefers canonicalized pathname for |
214 | better BFD caching. */ | |
215 | canonical_pathname = gdb_realpath (scratch_pathname); | |
216 | make_cleanup (xfree, canonical_pathname); | |
217 | ||
1c00ec6b | 218 | if (write_files) |
1f0c4988 | 219 | exec_bfd = gdb_bfd_fopen (canonical_pathname, gnutarget, |
1c00ec6b TT |
220 | FOPEN_RUB, scratch_chan); |
221 | else | |
1f0c4988 | 222 | exec_bfd = gdb_bfd_open (canonical_pathname, gnutarget, scratch_chan); |
c906108c SS |
223 | |
224 | if (!exec_bfd) | |
9fe4a216 | 225 | { |
9fe4a216 TT |
226 | error (_("\"%s\": could not open as an executable file: %s"), |
227 | scratch_pathname, bfd_errmsg (bfd_get_error ())); | |
228 | } | |
c906108c | 229 | |
1f0c4988 | 230 | gdb_assert (exec_filename == NULL); |
4856b6bc | 231 | exec_filename = gdb_realpath_keepfile (scratch_pathname); |
1f0c4988 | 232 | |
d18b8b7a | 233 | if (!bfd_check_format_matches (exec_bfd, bfd_object, &matching)) |
c906108c SS |
234 | { |
235 | /* Make sure to close exec_bfd, or else "run" might try to use | |
236 | it. */ | |
6c95b8df | 237 | exec_close (); |
8a3fe4f8 | 238 | error (_("\"%s\": not in executable format: %s"), |
d18b8b7a HZ |
239 | scratch_pathname, |
240 | gdb_bfd_errmsg (bfd_get_error (), matching)); | |
c906108c SS |
241 | } |
242 | ||
07b82ea5 | 243 | if (build_section_table (exec_bfd, §ions, §ions_end)) |
c906108c SS |
244 | { |
245 | /* Make sure to close exec_bfd, or else "run" might try to use | |
246 | it. */ | |
6c95b8df | 247 | exec_close (); |
8a3fe4f8 | 248 | error (_("\"%s\": can't find the file sections: %s"), |
c906108c SS |
249 | scratch_pathname, bfd_errmsg (bfd_get_error ())); |
250 | } | |
251 | ||
c04ea773 DJ |
252 | exec_bfd_mtime = bfd_get_mtime (exec_bfd); |
253 | ||
c906108c SS |
254 | validate_files (); |
255 | ||
256 | set_gdbarch_from_file (exec_bfd); | |
257 | ||
07b82ea5 | 258 | /* Add the executable's sections to the current address spaces' |
6c95b8df PA |
259 | list of sections. This possibly pushes the exec_ops |
260 | target. */ | |
ed9eebaf | 261 | add_target_sections (&exec_bfd, sections, sections_end); |
07b82ea5 | 262 | xfree (sections); |
c906108c SS |
263 | |
264 | /* Tell display code (if any) about the changed file name. */ | |
9a4105ab AC |
265 | if (deprecated_exec_file_display_hook) |
266 | (*deprecated_exec_file_display_hook) (filename); | |
f7545552 TT |
267 | |
268 | do_cleanups (cleanups); | |
c906108c | 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 | 282 | static void |
fba45db2 | 283 | exec_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 | ||
323 | static void | |
fba45db2 | 324 | file_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 | ||
339 | static void | |
7be0c536 AC |
340 | add_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 | ||
07b82ea5 PA |
364 | int |
365 | resize_section_table (struct target_section_table *table, int num_added) | |
366 | { | |
07b82ea5 PA |
367 | int old_count; |
368 | int new_count; | |
369 | ||
07b82ea5 PA |
370 | old_count = table->sections_end - table->sections; |
371 | ||
372 | new_count = num_added + old_count; | |
373 | ||
374 | if (new_count) | |
375 | { | |
376 | table->sections = xrealloc (table->sections, | |
377 | sizeof (struct target_section) * new_count); | |
378 | table->sections_end = table->sections + new_count; | |
379 | } | |
380 | else | |
381 | { | |
382 | xfree (table->sections); | |
383 | table->sections = table->sections_end = NULL; | |
384 | } | |
385 | ||
386 | return old_count; | |
387 | } | |
388 | ||
c906108c SS |
389 | /* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR. |
390 | Returns 0 if OK, 1 on error. */ | |
391 | ||
392 | int | |
0542c86d PA |
393 | build_section_table (struct bfd *some_bfd, struct target_section **start, |
394 | struct target_section **end) | |
c906108c SS |
395 | { |
396 | unsigned count; | |
397 | ||
398 | count = bfd_count_sections (some_bfd); | |
399 | if (*start) | |
b8c9b27d | 400 | xfree (* start); |
0542c86d | 401 | *start = (struct target_section *) xmalloc (count * sizeof (**start)); |
c906108c | 402 | *end = *start; |
c5aa993b | 403 | bfd_map_over_sections (some_bfd, add_to_section_table, (char *) end); |
c906108c | 404 | if (*end > *start + count) |
3e43a32a MS |
405 | internal_error (__FILE__, __LINE__, |
406 | _("failed internal consistency check")); | |
c906108c SS |
407 | /* We could realloc the table, but it probably loses for most files. */ |
408 | return 0; | |
409 | } | |
07b82ea5 PA |
410 | |
411 | /* Add the sections array defined by [SECTIONS..SECTIONS_END[ to the | |
412 | current set of target sections. */ | |
413 | ||
414 | void | |
046ac79f | 415 | add_target_sections (void *owner, |
ed9eebaf | 416 | struct target_section *sections, |
07b82ea5 PA |
417 | struct target_section *sections_end) |
418 | { | |
419 | int count; | |
420 | struct target_section_table *table = current_target_sections; | |
421 | ||
422 | count = sections_end - sections; | |
423 | ||
424 | if (count > 0) | |
425 | { | |
426 | int space = resize_section_table (table, count); | |
ed9eebaf | 427 | int i; |
d7f9d729 | 428 | |
ed9eebaf TT |
429 | for (i = 0; i < count; ++i) |
430 | { | |
431 | table->sections[space + i] = sections[i]; | |
046ac79f | 432 | table->sections[space + i].owner = owner; |
ed9eebaf | 433 | } |
07b82ea5 PA |
434 | |
435 | /* If these are the first file sections we can provide memory | |
436 | from, push the file_stratum target. */ | |
6c95b8df PA |
437 | if (!using_exec_ops) |
438 | { | |
439 | using_exec_ops = 1; | |
440 | push_target (&exec_ops); | |
441 | } | |
07b82ea5 PA |
442 | } |
443 | } | |
444 | ||
76ad5e1e NB |
445 | /* Add the sections of OBJFILE to the current set of target sections. */ |
446 | ||
447 | void | |
448 | add_target_sections_of_objfile (struct objfile *objfile) | |
449 | { | |
450 | struct target_section_table *table = current_target_sections; | |
451 | struct obj_section *osect; | |
452 | int space; | |
453 | unsigned count = 0; | |
454 | struct target_section *ts; | |
455 | ||
456 | if (objfile == NULL) | |
457 | return; | |
458 | ||
459 | /* Compute the number of sections to add. */ | |
460 | ALL_OBJFILE_OSECTIONS (objfile, osect) | |
461 | { | |
462 | if (bfd_get_section_size (osect->the_bfd_section) == 0) | |
463 | continue; | |
464 | count++; | |
465 | } | |
466 | ||
467 | if (count == 0) | |
468 | return; | |
469 | ||
470 | space = resize_section_table (table, count); | |
471 | ||
472 | ts = table->sections + space; | |
473 | ||
474 | ALL_OBJFILE_OSECTIONS (objfile, osect) | |
475 | { | |
476 | if (bfd_get_section_size (osect->the_bfd_section) == 0) | |
477 | continue; | |
478 | ||
479 | gdb_assert (ts < table->sections + space + count); | |
480 | ||
481 | ts->addr = obj_section_addr (osect); | |
482 | ts->endaddr = obj_section_endaddr (osect); | |
483 | ts->the_bfd_section = osect->the_bfd_section; | |
484 | ts->owner = (void *) objfile; | |
485 | ||
486 | ts++; | |
487 | } | |
488 | } | |
489 | ||
046ac79f JK |
490 | /* Remove all target sections owned by OWNER. |
491 | OWNER must be the same value passed to add_target_sections. */ | |
07b82ea5 PA |
492 | |
493 | void | |
046ac79f | 494 | remove_target_sections (void *owner) |
07b82ea5 PA |
495 | { |
496 | struct target_section *src, *dest; | |
07b82ea5 PA |
497 | struct target_section_table *table = current_target_sections; |
498 | ||
046ac79f JK |
499 | gdb_assert (owner != NULL); |
500 | ||
07b82ea5 PA |
501 | dest = table->sections; |
502 | for (src = table->sections; src < table->sections_end; src++) | |
046ac79f | 503 | if (src->owner != owner) |
07b82ea5 PA |
504 | { |
505 | /* Keep this section. */ | |
506 | if (dest < src) | |
507 | *dest = *src; | |
508 | dest++; | |
509 | } | |
510 | ||
511 | /* If we've dropped any sections, resize the section table. */ | |
512 | if (dest < src) | |
513 | { | |
514 | int old_count; | |
515 | ||
516 | old_count = resize_section_table (table, dest - src); | |
517 | ||
518 | /* If we don't have any more sections to read memory from, | |
519 | remove the file_stratum target from the stack. */ | |
520 | if (old_count + (dest - src) == 0) | |
6c95b8df PA |
521 | { |
522 | struct program_space *pspace; | |
523 | ||
524 | ALL_PSPACES (pspace) | |
525 | if (pspace->target_sections.sections | |
526 | != pspace->target_sections.sections_end) | |
527 | return; | |
528 | ||
529 | unpush_target (&exec_ops); | |
530 | } | |
07b82ea5 PA |
531 | } |
532 | } | |
533 | ||
c906108c | 534 | \f |
348f8c02 | 535 | |
e6ca34fc PA |
536 | VEC(mem_range_s) * |
537 | section_table_available_memory (VEC(mem_range_s) *memory, | |
424447ee | 538 | CORE_ADDR memaddr, ULONGEST len, |
e6ca34fc PA |
539 | struct target_section *sections, |
540 | struct target_section *sections_end) | |
541 | { | |
542 | struct target_section *p; | |
e6ca34fc PA |
543 | |
544 | for (p = sections; p < sections_end; p++) | |
545 | { | |
2b2848e2 DE |
546 | if ((bfd_get_section_flags (p->the_bfd_section->owner, |
547 | p->the_bfd_section) | |
e6ca34fc PA |
548 | & SEC_READONLY) == 0) |
549 | continue; | |
550 | ||
551 | /* Copy the meta-data, adjusted. */ | |
552 | if (mem_ranges_overlap (p->addr, p->endaddr - p->addr, memaddr, len)) | |
553 | { | |
554 | ULONGEST lo1, hi1, lo2, hi2; | |
555 | struct mem_range *r; | |
556 | ||
557 | lo1 = memaddr; | |
558 | hi1 = memaddr + len; | |
559 | ||
560 | lo2 = p->addr; | |
561 | hi2 = p->endaddr; | |
562 | ||
563 | r = VEC_safe_push (mem_range_s, memory, NULL); | |
564 | ||
565 | r->start = max (lo1, lo2); | |
566 | r->length = min (hi1, hi2) - r->start; | |
567 | } | |
568 | } | |
569 | ||
570 | return memory; | |
571 | } | |
572 | ||
9b409511 | 573 | enum target_xfer_status |
07b82ea5 | 574 | section_table_xfer_memory_partial (gdb_byte *readbuf, const gdb_byte *writebuf, |
b55e14c7 | 575 | ULONGEST offset, ULONGEST len, |
9b409511 | 576 | ULONGEST *xfered_len, |
07b82ea5 PA |
577 | struct target_section *sections, |
578 | struct target_section *sections_end, | |
579 | const char *section_name) | |
c906108c | 580 | { |
020cc13c | 581 | int res; |
0542c86d | 582 | struct target_section *p; |
07b82ea5 PA |
583 | ULONGEST memaddr = offset; |
584 | ULONGEST memend = memaddr + len; | |
c906108c | 585 | |
b55e14c7 | 586 | if (len == 0) |
3e43a32a MS |
587 | internal_error (__FILE__, __LINE__, |
588 | _("failed internal consistency check")); | |
c906108c | 589 | |
348f8c02 | 590 | for (p = sections; p < sections_end; p++) |
c906108c | 591 | { |
2b2848e2 DE |
592 | struct bfd_section *asect = p->the_bfd_section; |
593 | bfd *abfd = asect->owner; | |
594 | ||
595 | if (section_name && strcmp (section_name, asect->name) != 0) | |
0963b4bd | 596 | continue; /* not the section we need. */ |
c906108c | 597 | if (memaddr >= p->addr) |
3db26b01 JB |
598 | { |
599 | if (memend <= p->endaddr) | |
600 | { | |
601 | /* Entire transfer is within this section. */ | |
07b82ea5 | 602 | if (writebuf) |
2b2848e2 | 603 | res = bfd_set_section_contents (abfd, asect, |
07b82ea5 | 604 | writebuf, memaddr - p->addr, |
85302095 AC |
605 | len); |
606 | else | |
2b2848e2 | 607 | res = bfd_get_section_contents (abfd, asect, |
07b82ea5 | 608 | readbuf, memaddr - p->addr, |
85302095 | 609 | len); |
9b409511 YQ |
610 | |
611 | if (res != 0) | |
612 | { | |
613 | *xfered_len = len; | |
614 | return TARGET_XFER_OK; | |
615 | } | |
616 | else | |
617 | return TARGET_XFER_EOF; | |
3db26b01 JB |
618 | } |
619 | else if (memaddr >= p->endaddr) | |
620 | { | |
621 | /* This section ends before the transfer starts. */ | |
622 | continue; | |
623 | } | |
624 | else | |
625 | { | |
626 | /* This section overlaps the transfer. Just do half. */ | |
627 | len = p->endaddr - memaddr; | |
07b82ea5 | 628 | if (writebuf) |
2b2848e2 | 629 | res = bfd_set_section_contents (abfd, asect, |
07b82ea5 | 630 | writebuf, memaddr - p->addr, |
85302095 AC |
631 | len); |
632 | else | |
2b2848e2 | 633 | res = bfd_get_section_contents (abfd, asect, |
07b82ea5 | 634 | readbuf, memaddr - p->addr, |
85302095 | 635 | len); |
9b409511 YQ |
636 | if (res != 0) |
637 | { | |
638 | *xfered_len = len; | |
639 | return TARGET_XFER_OK; | |
640 | } | |
641 | else | |
642 | return TARGET_XFER_EOF; | |
3db26b01 JB |
643 | } |
644 | } | |
c906108c SS |
645 | } |
646 | ||
9b409511 | 647 | return TARGET_XFER_EOF; /* We can't help. */ |
c906108c | 648 | } |
348f8c02 | 649 | |
70221824 | 650 | static struct target_section_table * |
07b82ea5 | 651 | exec_get_section_table (struct target_ops *ops) |
348f8c02 | 652 | { |
07b82ea5 | 653 | return current_target_sections; |
348f8c02 PA |
654 | } |
655 | ||
9b409511 | 656 | static enum target_xfer_status |
07b82ea5 PA |
657 | exec_xfer_partial (struct target_ops *ops, enum target_object object, |
658 | const char *annex, gdb_byte *readbuf, | |
659 | const gdb_byte *writebuf, | |
9b409511 | 660 | ULONGEST offset, ULONGEST len, ULONGEST *xfered_len) |
348f8c02 | 661 | { |
07b82ea5 PA |
662 | struct target_section_table *table = target_get_section_table (ops); |
663 | ||
664 | if (object == TARGET_OBJECT_MEMORY) | |
665 | return section_table_xfer_memory_partial (readbuf, writebuf, | |
9b409511 | 666 | offset, len, xfered_len, |
07b82ea5 PA |
667 | table->sections, |
668 | table->sections_end, | |
669 | NULL); | |
670 | else | |
2ed4b548 | 671 | return TARGET_XFER_E_IO; |
348f8c02 | 672 | } |
c906108c | 673 | \f |
c5aa993b | 674 | |
c906108c | 675 | void |
07b82ea5 | 676 | print_section_info (struct target_section_table *t, bfd *abfd) |
c906108c | 677 | { |
5af949e3 | 678 | struct gdbarch *gdbarch = gdbarch_from_bfd (abfd); |
0542c86d | 679 | struct target_section *p; |
17a912b6 | 680 | /* FIXME: 16 is not wide enough when gdbarch_addr_bit > 64. */ |
5af949e3 | 681 | int wid = gdbarch_addr_bit (gdbarch) <= 32 ? 8 : 16; |
c906108c | 682 | |
c5aa993b | 683 | printf_filtered ("\t`%s', ", bfd_get_filename (abfd)); |
c906108c | 684 | wrap_here (" "); |
a3f17187 | 685 | printf_filtered (_("file type %s.\n"), bfd_get_target (abfd)); |
c906108c | 686 | if (abfd == exec_bfd) |
51bee8e9 | 687 | { |
3e43a32a MS |
688 | /* gcc-3.4 does not like the initialization in |
689 | <p == t->sections_end>. */ | |
d904de5b | 690 | bfd_vma displacement = 0; |
2f1bdd26 | 691 | bfd_vma entry_point; |
51bee8e9 JK |
692 | |
693 | for (p = t->sections; p < t->sections_end; p++) | |
694 | { | |
2b2848e2 DE |
695 | struct bfd_section *psect = p->the_bfd_section; |
696 | bfd *pbfd = psect->owner; | |
51bee8e9 | 697 | |
2b2848e2 | 698 | if ((bfd_get_section_flags (pbfd, psect) & (SEC_ALLOC | SEC_LOAD)) |
51bee8e9 JK |
699 | != (SEC_ALLOC | SEC_LOAD)) |
700 | continue; | |
701 | ||
2b2848e2 DE |
702 | if (bfd_get_section_vma (pbfd, psect) <= abfd->start_address |
703 | && abfd->start_address < (bfd_get_section_vma (pbfd, psect) | |
704 | + bfd_get_section_size (psect))) | |
51bee8e9 | 705 | { |
2b2848e2 | 706 | displacement = p->addr - bfd_get_section_vma (pbfd, psect); |
51bee8e9 JK |
707 | break; |
708 | } | |
709 | } | |
710 | if (p == t->sections_end) | |
b37520b6 | 711 | warning (_("Cannot find section for the entry point of %s."), |
d904de5b | 712 | bfd_get_filename (abfd)); |
51bee8e9 | 713 | |
2f1bdd26 MGD |
714 | entry_point = gdbarch_addr_bits_remove (gdbarch, |
715 | bfd_get_start_address (abfd) | |
716 | + displacement); | |
51bee8e9 | 717 | printf_filtered (_("\tEntry point: %s\n"), |
2f1bdd26 | 718 | paddress (gdbarch, entry_point)); |
51bee8e9 | 719 | } |
07b82ea5 | 720 | for (p = t->sections; p < t->sections_end; p++) |
c906108c | 721 | { |
2b2848e2 DE |
722 | struct bfd_section *psect = p->the_bfd_section; |
723 | bfd *pbfd = psect->owner; | |
724 | ||
bb599908 PH |
725 | printf_filtered ("\t%s", hex_string_custom (p->addr, wid)); |
726 | printf_filtered (" - %s", hex_string_custom (p->endaddr, wid)); | |
bcf16802 KB |
727 | |
728 | /* FIXME: A format of "08l" is not wide enough for file offsets | |
729 | larger than 4GB. OTOH, making it "016l" isn't desirable either | |
730 | since most output will then be much wider than necessary. It | |
731 | may make sense to test the size of the file and choose the | |
732 | format string accordingly. */ | |
a3f17187 | 733 | /* FIXME: i18n: Need to rewrite this sentence. */ |
c906108c SS |
734 | if (info_verbose) |
735 | printf_filtered (" @ %s", | |
2b2848e2 DE |
736 | hex_string_custom (psect->filepos, 8)); |
737 | printf_filtered (" is %s", bfd_section_name (pbfd, psect)); | |
738 | if (pbfd != abfd) | |
739 | printf_filtered (" in %s", bfd_get_filename (pbfd)); | |
c906108c SS |
740 | printf_filtered ("\n"); |
741 | } | |
742 | } | |
743 | ||
744 | static void | |
fba45db2 | 745 | exec_files_info (struct target_ops *t) |
c906108c | 746 | { |
57008375 JK |
747 | if (exec_bfd) |
748 | print_section_info (current_target_sections, exec_bfd); | |
749 | else | |
750 | puts_filtered (_("\t<no file loaded>\n")); | |
c906108c SS |
751 | } |
752 | ||
753 | static void | |
fba45db2 | 754 | set_section_command (char *args, int from_tty) |
c906108c | 755 | { |
0542c86d | 756 | struct target_section *p; |
c906108c SS |
757 | char *secname; |
758 | unsigned seclen; | |
759 | unsigned long secaddr; | |
760 | char secprint[100]; | |
761 | long offset; | |
07b82ea5 | 762 | struct target_section_table *table; |
c906108c SS |
763 | |
764 | if (args == 0) | |
8a3fe4f8 | 765 | error (_("Must specify section name and its virtual address")); |
c906108c | 766 | |
0963b4bd | 767 | /* Parse out section name. */ |
c5aa993b | 768 | for (secname = args; !isspace (*args); args++); |
c906108c SS |
769 | seclen = args - secname; |
770 | ||
0963b4bd | 771 | /* Parse out new virtual address. */ |
c906108c SS |
772 | secaddr = parse_and_eval_address (args); |
773 | ||
07b82ea5 PA |
774 | table = current_target_sections; |
775 | for (p = table->sections; p < table->sections_end; p++) | |
c5aa993b | 776 | { |
57008375 | 777 | if (!strncmp (secname, bfd_section_name (p->bfd, |
3e43a32a | 778 | p->the_bfd_section), seclen) |
57008375 | 779 | && bfd_section_name (p->bfd, p->the_bfd_section)[seclen] == '\0') |
c5aa993b JM |
780 | { |
781 | offset = secaddr - p->addr; | |
782 | p->addr += offset; | |
783 | p->endaddr += offset; | |
784 | if (from_tty) | |
785 | exec_files_info (&exec_ops); | |
786 | return; | |
787 | } | |
c906108c | 788 | } |
c906108c SS |
789 | if (seclen >= sizeof (secprint)) |
790 | seclen = sizeof (secprint) - 1; | |
791 | strncpy (secprint, secname, seclen); | |
792 | secprint[seclen] = '\0'; | |
8a3fe4f8 | 793 | error (_("Section %s not found"), secprint); |
c906108c SS |
794 | } |
795 | ||
30510692 DJ |
796 | /* If we can find a section in FILENAME with BFD index INDEX, adjust |
797 | it to ADDRESS. */ | |
c1bd25fd DJ |
798 | |
799 | void | |
800 | exec_set_section_address (const char *filename, int index, CORE_ADDR address) | |
801 | { | |
0542c86d | 802 | struct target_section *p; |
07b82ea5 | 803 | struct target_section_table *table; |
c1bd25fd | 804 | |
07b82ea5 PA |
805 | table = current_target_sections; |
806 | for (p = table->sections; p < table->sections_end; p++) | |
c1bd25fd | 807 | { |
2b2848e2 | 808 | if (filename_cmp (filename, p->the_bfd_section->owner->filename) == 0 |
30510692 | 809 | && index == p->the_bfd_section->index) |
c1bd25fd | 810 | { |
30510692 | 811 | p->endaddr += address - p->addr; |
c1bd25fd | 812 | p->addr = address; |
c1bd25fd DJ |
813 | } |
814 | } | |
815 | } | |
816 | ||
c906108c SS |
817 | /* If mourn is being called in all the right places, this could be say |
818 | `gdb internal error' (since generic_mourn calls | |
819 | breakpoint_init_inferior). */ | |
820 | ||
821 | static int | |
3db08215 MM |
822 | ignore (struct target_ops *ops, struct gdbarch *gdbarch, |
823 | struct bp_target_info *bp_tgt) | |
c906108c SS |
824 | { |
825 | return 0; | |
826 | } | |
827 | ||
c35b1492 PA |
828 | static int |
829 | exec_has_memory (struct target_ops *ops) | |
830 | { | |
831 | /* We can provide memory if we have any file/target sections to read | |
832 | from. */ | |
833 | return (current_target_sections->sections | |
834 | != current_target_sections->sections_end); | |
835 | } | |
836 | ||
0963b4bd | 837 | /* Find mapped memory. */ |
be4d1333 MS |
838 | |
839 | extern void | |
b8edc417 | 840 | exec_set_find_memory_regions (int (*func) (find_memory_region_ftype, void *)) |
be4d1333 | 841 | { |
2e73927c TT |
842 | exec_do_find_memory_regions = func; |
843 | } | |
844 | ||
845 | static int | |
846 | exec_find_memory_regions (struct target_ops *self, | |
847 | find_memory_region_ftype func, void *data) | |
848 | { | |
849 | return exec_do_find_memory_regions (func, data); | |
be4d1333 MS |
850 | } |
851 | ||
fc6691b2 | 852 | static char *exec_make_note_section (struct target_ops *self, bfd *, int *); |
be4d1333 | 853 | |
c906108c SS |
854 | /* Fill in the exec file target vector. Very few entries need to be |
855 | defined. */ | |
856 | ||
be4d1333 | 857 | static void |
fba45db2 | 858 | init_exec_ops (void) |
c906108c SS |
859 | { |
860 | exec_ops.to_shortname = "exec"; | |
861 | exec_ops.to_longname = "Local exec file"; | |
862 | exec_ops.to_doc = "Use an executable file as a target.\n\ | |
863 | Specify the filename of the executable file."; | |
1adeb98a | 864 | exec_ops.to_open = exec_open; |
6c95b8df | 865 | exec_ops.to_close = exec_close_1; |
c906108c | 866 | exec_ops.to_attach = find_default_attach; |
07b82ea5 PA |
867 | exec_ops.to_xfer_partial = exec_xfer_partial; |
868 | exec_ops.to_get_section_table = exec_get_section_table; | |
c906108c SS |
869 | exec_ops.to_files_info = exec_files_info; |
870 | exec_ops.to_insert_breakpoint = ignore; | |
871 | exec_ops.to_remove_breakpoint = ignore; | |
872 | exec_ops.to_create_inferior = find_default_create_inferior; | |
c906108c | 873 | exec_ops.to_stratum = file_stratum; |
c35b1492 | 874 | exec_ops.to_has_memory = exec_has_memory; |
be4d1333 | 875 | exec_ops.to_make_corefile_notes = exec_make_note_section; |
2e73927c | 876 | exec_ops.to_find_memory_regions = exec_find_memory_regions; |
c5aa993b | 877 | exec_ops.to_magic = OPS_MAGIC; |
c906108c SS |
878 | } |
879 | ||
880 | void | |
fba45db2 | 881 | _initialize_exec (void) |
c906108c SS |
882 | { |
883 | struct cmd_list_element *c; | |
884 | ||
885 | init_exec_ops (); | |
886 | ||
887 | if (!dbx_commands) | |
888 | { | |
1a966eab AC |
889 | c = add_cmd ("file", class_files, file_command, _("\ |
890 | Use FILE as program to be debugged.\n\ | |
c906108c SS |
891 | It is read for its symbols, for getting the contents of pure memory,\n\ |
892 | and it is the program executed when you use the `run' command.\n\ | |
893 | If FILE cannot be found as specified, your execution directory path\n\ | |
894 | ($PATH) is searched for a command of that name.\n\ | |
1a966eab | 895 | No arg means to have no executable file and no symbols."), &cmdlist); |
5ba2abeb | 896 | set_cmd_completer (c, filename_completer); |
c906108c SS |
897 | } |
898 | ||
1a966eab AC |
899 | c = add_cmd ("exec-file", class_files, exec_file_command, _("\ |
900 | Use FILE as program for getting contents of pure memory.\n\ | |
c906108c SS |
901 | If FILE cannot be found as specified, your execution directory path\n\ |
902 | is searched for a command of that name.\n\ | |
1a966eab | 903 | No arg means have no executable file."), &cmdlist); |
5ba2abeb | 904 | set_cmd_completer (c, filename_completer); |
c906108c | 905 | |
1bedd215 AC |
906 | add_com ("section", class_files, set_section_command, _("\ |
907 | Change the base address of section SECTION of the exec file to ADDR.\n\ | |
c906108c SS |
908 | This can be used if the exec file does not contain section addresses,\n\ |
909 | (such as in the a.out format), or when the addresses specified in the\n\ | |
910 | file itself are wrong. Each section must be changed separately. The\n\ | |
1bedd215 | 911 | ``info files'' command lists all the sections and their addresses.")); |
c906108c | 912 | |
5bf193a2 AC |
913 | add_setshow_boolean_cmd ("write", class_support, &write_files, _("\ |
914 | Set writing into executable and core files."), _("\ | |
915 | Show writing into executable and core files."), NULL, | |
916 | NULL, | |
920d2a44 | 917 | show_write_files, |
5bf193a2 | 918 | &setlist, &showlist); |
c5aa993b | 919 | |
9852c492 | 920 | add_target_with_completer (&exec_ops, filename_completer); |
c906108c | 921 | } |
be4d1333 MS |
922 | |
923 | static char * | |
fc6691b2 | 924 | exec_make_note_section (struct target_ops *self, bfd *obfd, int *note_size) |
be4d1333 | 925 | { |
8a3fe4f8 | 926 | error (_("Can't create a corefile")); |
be4d1333 | 927 | } |