]>
Commit | Line | Data |
---|---|---|
41abdfbd | 1 | /* Execute AIXcoff files, for GDB. |
e17960fb | 2 | Copyright 1988, 1989, 1991, 1992 Free Software Foundation, Inc. |
41abdfbd JG |
3 | Derived from exec.c. Modified by IBM Corporation. |
4 | Donated by IBM Corporation and Cygnus Support. | |
5 | ||
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
21 | ||
22 | /* xcoff-exec - deal with executing XCOFF files. */ | |
23 | ||
d747e0af MT |
24 | #include "defs.h" |
25 | ||
41abdfbd JG |
26 | #include <sys/types.h> |
27 | #include <sys/param.h> | |
28 | #include <fcntl.h> | |
29 | #include <string.h> | |
30 | #include <ctype.h> | |
31 | #include <sys/stat.h> | |
32 | #include <sys/ldr.h> | |
33 | ||
41abdfbd JG |
34 | #include "frame.h" |
35 | #include "inferior.h" | |
36 | #include "target.h" | |
37 | #include "gdbcmd.h" | |
38 | #include "gdbcore.h" | |
39 | #include "symfile.h" | |
5e2e79f8 | 40 | #include "objfiles.h" |
41abdfbd JG |
41 | |
42 | #include "libbfd.h" /* BFD internals (sigh!) FIXME */ | |
1eeba686 | 43 | #include "xcoffsolib.h" |
41abdfbd | 44 | |
1ab3bf1b JG |
45 | /* Prototypes for local functions */ |
46 | ||
1ab3bf1b JG |
47 | static void |
48 | file_command PARAMS ((char *, int)); | |
49 | ||
50 | static void | |
51 | exec_close PARAMS ((int)); | |
52 | ||
41abdfbd JG |
53 | struct section_table *exec_sections, *exec_sections_end; |
54 | ||
41abdfbd JG |
55 | /* Whether to open exec and core files read-only or read-write. */ |
56 | ||
57 | int write_files = 0; | |
58 | ||
818de002 PB |
59 | extern int info_verbose; |
60 | ||
41abdfbd JG |
61 | bfd *exec_bfd; /* needed by core.c */ |
62 | ||
63 | extern char *getenv(); | |
41abdfbd JG |
64 | extern void add_syms_addr_command (); |
65 | extern void symbol_file_command (); | |
66 | static void exec_files_info(); | |
818de002 | 67 | extern struct objfile *lookup_objfile_bfd (); |
41abdfbd | 68 | |
1eeba686 | 69 | #if 0 |
41abdfbd JG |
70 | /* |
71 | * the vmap struct is used to describe the virtual address space of | |
72 | * the target we are manipulating. The first entry is always the "exec" | |
73 | * file. Subsequent entries correspond to other objects that are | |
74 | * mapped into the address space of a process created from the "exec" file. | |
75 | * These are either in response to exec()ing the file, in which case all | |
76 | * shared libraries are loaded, or a "load" system call, followed by the | |
77 | * user's issuance of a "load" command. | |
78 | */ | |
79 | struct vmap { | |
80 | struct vmap *nxt; /* ^ to next in chain */ | |
81 | bfd *bfd; /* BFD for mappable object library */ | |
82 | char *name; /* ^ to object file name */ | |
83 | char *member; /* ^ to member name */ | |
84 | CORE_ADDR tstart; /* virtual addr where member is mapped */ | |
85 | CORE_ADDR tend; /* virtual upper bound of member */ | |
86 | CORE_ADDR tadj; /* heuristically derived adjustment */ | |
87 | CORE_ADDR dstart; /* virtual address of data start */ | |
88 | CORE_ADDR dend; /* vitrual address of data end */ | |
89 | }; | |
90 | ||
91 | ||
92 | struct vmap_and_bfd { | |
93 | bfd *pbfd; | |
94 | struct vmap *pvmap; | |
95 | }; | |
96 | ||
97 | static struct vmap *vmap; /* current vmap */ | |
1eeba686 PB |
98 | #endif /* 0 */ |
99 | ||
100 | struct vmap *vmap; /* current vmap */ | |
41abdfbd JG |
101 | |
102 | extern struct target_ops exec_ops; | |
103 | ||
104 | ||
105 | /* exec_close - done with exec file, clean up all resources. */ | |
106 | ||
1ab3bf1b | 107 | static void |
818de002 PB |
108 | exec_close(quitting) |
109 | { | |
110 | register struct vmap *vp, *nxt; | |
111 | struct objfile *obj; | |
112 | ||
113 | for (nxt = vmap; vp = nxt; ) | |
114 | { | |
115 | nxt = vp->nxt; | |
116 | ||
117 | /* if there is an objfile associated with this bfd, | |
118 | free_objfile() will do proper cleanup of objfile *and* bfd. */ | |
119 | ||
120 | if (obj = lookup_objfile_bfd (vp->bfd)) | |
121 | free_objfile (obj); | |
122 | else | |
123 | bfd_close(vp->bfd); | |
124 | ||
125 | free_named_symtabs(vp->name); | |
126 | free(vp); | |
127 | } | |
128 | ||
129 | vmap = 0; | |
507e4004 PB |
130 | |
131 | if (exec_bfd) { | |
132 | bfd_close (exec_bfd); | |
133 | exec_bfd = NULL; | |
134 | } | |
135 | if (exec_ops.to_sections) { | |
136 | free (exec_ops.to_sections); | |
137 | exec_ops.to_sections = NULL; | |
138 | exec_ops.to_sections_end = NULL; | |
139 | } | |
41abdfbd JG |
140 | } |
141 | ||
142 | /* | |
143 | * exec_file_command - handle the "exec" command, &c. | |
144 | */ | |
145 | void | |
146 | exec_file_command(filename, from_tty) | |
147 | char *filename; | |
148 | { | |
41abdfbd | 149 | target_preopen(from_tty); |
507e4004 PB |
150 | |
151 | /* Remove any previous exec file. */ | |
41abdfbd JG |
152 | unpush_target(&exec_ops); |
153 | ||
154 | /* Now open and digest the file the user requested, if any. */ | |
155 | ||
156 | if (filename) { | |
157 | char *scratch_pathname; | |
158 | int scratch_chan; | |
159 | ||
160 | filename = tilde_expand(filename); | |
1ab3bf1b | 161 | make_cleanup (free, filename); |
41abdfbd | 162 | |
507e4004 PB |
163 | scratch_chan = openp(getenv("PATH"), 1, filename, |
164 | write_files? O_RDWR: O_RDONLY, 0, | |
165 | &scratch_pathname); | |
41abdfbd JG |
166 | if (scratch_chan < 0) |
167 | perror_with_name(filename); | |
168 | ||
507e4004 PB |
169 | exec_bfd = bfd_fdopenr(scratch_pathname, NULL, scratch_chan); |
170 | if (!exec_bfd) | |
41abdfbd JG |
171 | error("Could not open `%s' as an executable file: %s" |
172 | , scratch_pathname, bfd_errmsg(bfd_error)); | |
173 | ||
174 | /* make sure we have an object file */ | |
175 | ||
507e4004 PB |
176 | if (!bfd_check_format(exec_bfd, bfd_object)) |
177 | error("\"%s\": not in executable format: %s.", | |
178 | scratch_pathname, bfd_errmsg(bfd_error)); | |
41abdfbd JG |
179 | |
180 | ||
181 | /* setup initial vmap */ | |
182 | ||
507e4004 | 183 | map_vmap (exec_bfd, 0); |
41abdfbd | 184 | if (!vmap) |
507e4004 PB |
185 | error("Can't find the file sections in `%s': %s", |
186 | exec_bfd->filename, bfd_errmsg(bfd_error)); | |
41abdfbd | 187 | |
507e4004 PB |
188 | if (build_section_table (exec_bfd, &exec_ops.to_sections, |
189 | &exec_ops.to_sections_end)) | |
41abdfbd JG |
190 | error ("Can't find the file sections in `%s': %s", |
191 | exec_bfd->filename, bfd_errmsg (bfd_error)); | |
192 | ||
193 | /* make sure core, if present, matches */ | |
194 | validate_files(); | |
195 | ||
196 | push_target(&exec_ops); | |
197 | ||
198 | /* Tell display code(if any) about the changed file name. */ | |
199 | ||
200 | if (exec_file_display_hook) | |
201 | (*exec_file_display_hook)(filename); | |
202 | } | |
203 | else { | |
204 | exec_close(0); /* just in case */ | |
205 | if (from_tty) | |
206 | printf("No exec file now.\n"); | |
207 | } | |
208 | } | |
209 | ||
210 | /* Set both the exec file and the symbol file, in one command. What a | |
211 | * novelty. Why did GDB go through four major releases before this | |
212 | * command was added? | |
213 | */ | |
1ab3bf1b | 214 | static void |
41abdfbd JG |
215 | file_command(arg, from_tty) |
216 | char *arg; { | |
217 | ||
218 | exec_file_command(arg, from_tty); | |
219 | symbol_file_command(arg, from_tty); | |
220 | } | |
221 | ||
222 | /* Locate all mappable sections of a BFD file. | |
223 | table_pp_char is a char * to get it through bfd_map_over_sections; | |
224 | we cast it back to its proper type. */ | |
225 | ||
1ab3bf1b | 226 | static void |
41abdfbd JG |
227 | add_to_section_table (abfd, asect, table_pp_char) |
228 | bfd *abfd; | |
229 | sec_ptr asect; | |
230 | char *table_pp_char; | |
231 | { | |
232 | struct section_table **table_pp = (struct section_table **)table_pp_char; | |
233 | flagword aflag; | |
234 | ||
235 | aflag = bfd_get_section_flags (abfd, asect); | |
236 | /* FIXME, we need to handle BSS segment here...it alloc's but doesn't load */ | |
237 | if (!(aflag & SEC_LOAD)) | |
238 | return; | |
507e4004 PB |
239 | if (0 == bfd_section_size (abfd, asect)) |
240 | return; | |
241 | (*table_pp)->bfd = abfd; | |
41abdfbd JG |
242 | (*table_pp)->sec_ptr = asect; |
243 | (*table_pp)->addr = bfd_section_vma (abfd, asect); | |
244 | (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect); | |
245 | (*table_pp)++; | |
246 | } | |
247 | ||
248 | int | |
249 | build_section_table (some_bfd, start, end) | |
250 | bfd *some_bfd; | |
251 | struct section_table **start, **end; | |
252 | { | |
253 | unsigned count; | |
254 | ||
255 | count = bfd_count_sections (some_bfd); | |
256 | if (count == 0) | |
257 | abort(); /* return 1? */ | |
258 | if (*start) | |
259 | free (*start); | |
260 | *start = (struct section_table *) xmalloc (count * sizeof (**start)); | |
261 | *end = *start; | |
262 | bfd_map_over_sections (some_bfd, add_to_section_table, (char *)end); | |
263 | if (*end > *start + count) | |
264 | abort(); | |
265 | /* We could realloc the table, but it probably loses for most files. */ | |
266 | return 0; | |
267 | } | |
268 | ||
269 | /* | |
270 | * lookup_symtab_bfd - find if we currently have any symbol tables from bfd | |
271 | */ | |
272 | struct objfile * | |
273 | lookup_objfile_bfd(bfd *bfd) { | |
274 | register struct objfile *s; | |
275 | ||
276 | for (s = object_files; s; s = s->next) | |
277 | if (s->obfd == bfd) | |
278 | return s; | |
279 | return 0; | |
280 | } | |
281 | ||
282 | ||
283 | void | |
284 | sex_to_vmap(bfd *bf, sec_ptr sex, struct vmap_and_bfd *vmap_bfd) | |
285 | { | |
286 | register struct vmap *vp, **vpp; | |
287 | register struct symtab *syms; | |
288 | bfd *arch = vmap_bfd->pbfd; | |
289 | vp = vmap_bfd->pvmap; | |
290 | ||
291 | if ((bfd_get_section_flags(bf, sex) & SEC_LOAD) == 0) | |
292 | return; | |
293 | ||
2e4964ad | 294 | if (STREQ(bfd_section_name(bf, sex), ".text")) { |
41abdfbd JG |
295 | vp->tstart = 0; |
296 | vp->tend = vp->tstart + bfd_section_size(bf, sex); | |
297 | ||
507e4004 PB |
298 | /* When it comes to this adjustment value, in contrast to our previous |
299 | belief shared objects should behave the same as the main load segment. | |
300 | This is the offset from the beginning of text section to the first | |
301 | real instruction. */ | |
302 | ||
303 | vp->tadj = sex->filepos - bfd_section_vma(bf, sex); | |
41abdfbd JG |
304 | } |
305 | ||
2e4964ad | 306 | else if (STREQ(bfd_section_name(bf, sex), ".data")) { |
41abdfbd JG |
307 | vp->dstart = 0; |
308 | vp->dend = vp->dstart + bfd_section_size(bf, sex); | |
309 | } | |
310 | ||
2e4964ad | 311 | else if (STREQ(bfd_section_name(bf, sex), ".bss")) /* FIXMEmgo */ |
41abdfbd JG |
312 | printf ("bss section in exec! Don't know what the heck to do!\n"); |
313 | } | |
314 | ||
315 | /* Make a vmap for the BFD "bf", which might be a member of the archive | |
316 | BFD "arch". If we have not yet read in symbols for this file, do so. */ | |
317 | ||
318 | map_vmap (bfd *bf, bfd *arch) | |
319 | { | |
320 | struct vmap_and_bfd vmap_bfd; | |
321 | struct vmap *vp, **vpp; | |
322 | struct objfile *obj; | |
41abdfbd JG |
323 | |
324 | vp = (void*) xmalloc (sizeof (*vp)); | |
1eeba686 | 325 | bzero (vp, sizeof (*vp)); |
41abdfbd JG |
326 | vp->nxt = 0; |
327 | vp->bfd = bf; | |
328 | vp->name = bfd_get_filename(arch ? arch : bf); | |
329 | vp->member = arch ? bfd_get_filename(bf) : ""; | |
330 | ||
331 | vmap_bfd.pbfd = arch; | |
332 | vmap_bfd.pvmap = vp; | |
333 | bfd_map_over_sections (bf, sex_to_vmap, &vmap_bfd); | |
334 | ||
335 | obj = lookup_objfile_bfd (bf); | |
336 | if (exec_bfd && !obj) { | |
b0246b3b | 337 | obj = allocate_objfile (bf, 0); |
1eeba686 PB |
338 | |
339 | #if 0 | |
d1140eb5 FF |
340 | /* This is only needed if we want to load shared libraries no matter what. |
341 | Since we provide the choice of incremental loading of shared objects | |
427d778c | 342 | now, we do not have to load them as default anymore. */ |
1eeba686 | 343 | |
4369a140 | 344 | syms_from_objfile (obj, 0, 0, 0); |
2d6d969c | 345 | new_symfile_objfile (obj, 0, 0); |
1eeba686 | 346 | #endif |
41abdfbd JG |
347 | } |
348 | ||
349 | /* find the end of the list, and append. */ | |
350 | for (vpp = &vmap; *vpp; vpp = &(*vpp)->nxt) | |
351 | ; | |
352 | *vpp = vp; | |
353 | } | |
354 | ||
507e4004 | 355 | |
1ab3bf1b | 356 | /* true, if symbol table and minimal symbol table are relocated. */ |
41abdfbd JG |
357 | |
358 | int symtab_relocated = 0; | |
359 | ||
360 | ||
361 | /* vmap_symtab - handle symbol translation on vmapping */ | |
362 | ||
363 | vmap_symtab(vp, old_start, vip) | |
364 | register struct vmap *vp; | |
365 | CORE_ADDR old_start; | |
366 | struct stat *vip; | |
367 | { | |
1ab3bf1b JG |
368 | register struct symtab *s; |
369 | register struct objfile *objfile; | |
84ffdec2 | 370 | register struct minimal_symbol *msymbol; |
1ab3bf1b JG |
371 | |
372 | /* | |
373 | * for each symbol table generated from the vp->bfd | |
374 | */ | |
84ffdec2 | 375 | ALL_OBJFILES (objfile) |
1ab3bf1b JG |
376 | { |
377 | for (s = objfile -> symtabs; s != NULL; s = s -> next) { | |
378 | ||
379 | /* skip over if this is not relocatable and doesn't have a line table */ | |
380 | if (s->nonreloc && !LINETABLE (s)) | |
381 | continue; | |
382 | ||
383 | /* matching the symbol table's BFD and the *vp's BFD is hairy. | |
384 | exec_file creates a seperate BFD for possibly the | |
385 | same file as symbol_file.FIXME ALL THIS MUST BE RECTIFIED. */ | |
386 | ||
387 | if (objfile->obfd == vp->bfd) { | |
388 | /* if they match, we luck out. */ | |
389 | ; | |
390 | } else if (vp->member[0]) { | |
391 | /* no match, and member present, not this one. */ | |
392 | continue; | |
cbf15d9a JG |
393 | } else if (vip) { |
394 | /* No match, and no member. need to be sure. | |
395 | If we were given a stat structure, see if the open file | |
396 | underlying this BFD matches. */ | |
1ab3bf1b JG |
397 | struct stat si; |
398 | FILE *io; | |
399 | ||
1ab3bf1b JG |
400 | io = bfd_cache_lookup(objfile->obfd); |
401 | if (!io) | |
402 | fatal("cannot find BFD's iostream for sym"); | |
cbf15d9a JG |
403 | |
404 | /* see if we are referring to the same file */ | |
1ab3bf1b JG |
405 | if (fstat(fileno(io), &si) < 0) |
406 | fatal("cannot fstat BFD for sym"); | |
407 | ||
cbf15d9a | 408 | if ((si.st_dev != vip->st_dev |
1eeba686 | 409 | || si.st_ino != vip->st_ino)) |
41abdfbd | 410 | continue; |
cbf15d9a JG |
411 | } else { |
412 | continue; /* No stat struct: no way to match it */ | |
41abdfbd | 413 | } |
cbf15d9a | 414 | |
507e4004 PB |
415 | if (vp->tstart != old_start) { |
416 | ||
417 | /* Once we find a relocation base address for one of the symtabs | |
418 | in this objfile, it will be the same for all symtabs in this | |
419 | objfile. Clean this algorithm. FIXME. */ | |
420 | ||
421 | for (; s; s = s->next) | |
422 | if (!s->nonreloc || LINETABLE(s)) | |
423 | vmap_symtab_1(s, vp, old_start); | |
556f3d90 PB |
424 | |
425 | #if 0 | |
426 | Himm.., recently we nullified trampoline entry names in order not | |
427 | to confuse them with real symbols. Appearently this turned into a | |
428 | problem, and msymbol vector did not get relocated properly. If | |
429 | msymbols have to have non-null names, then we should name | |
430 | trampoline entries with empty strings. | |
431 | ||
432 | ALL_MSYMBOLS (objfile, msymbol) | |
433 | #else | |
434 | for (msymbol = objfile->msymbols; | |
435 | msymbol->name || msymbol->address; (msymbol)++) | |
436 | #endif | |
437 | if (msymbol->address < TEXT_SEGMENT_BASE) | |
438 | msymbol -> address += vp->tstart - old_start; | |
439 | ||
507e4004 PB |
440 | break; |
441 | } | |
1ab3bf1b JG |
442 | } |
443 | } | |
507e4004 | 444 | |
818de002 | 445 | if (vp->tstart != old_start) { |
818de002 PB |
446 | /* breakpoints need to be relocated as well. */ |
447 | fixup_breakpoints (0, TEXT_SEGMENT_BASE, vp->tstart - old_start); | |
448 | } | |
1ab3bf1b JG |
449 | |
450 | symtab_relocated = 1; | |
41abdfbd JG |
451 | } |
452 | ||
507e4004 | 453 | |
41abdfbd JG |
454 | vmap_symtab_1(s, vp, old_start) |
455 | register struct symtab *s; | |
456 | register struct vmap *vp; | |
457 | CORE_ADDR old_start; | |
458 | { | |
459 | register int i, j; | |
460 | int len, blen; | |
461 | register struct linetable *l; | |
462 | struct blockvector *bv; | |
463 | register struct block *b; | |
464 | int depth; | |
465 | register ulong reloc, dreloc; | |
466 | ||
467 | if ((reloc = vp->tstart - old_start) == 0) | |
468 | return; | |
469 | ||
470 | dreloc = vp->dstart; /* data relocation */ | |
471 | ||
472 | /* | |
473 | * The line table must be relocated. This is only present for | |
818de002 | 474 | * .text sections, so only vp->text type maps need be considered. |
41abdfbd JG |
475 | */ |
476 | l = LINETABLE (s); | |
818de002 PB |
477 | if (l) { |
478 | len = l->nitems; | |
479 | for (i = 0; i < len; i++) | |
41abdfbd | 480 | l->item[i].pc += reloc; |
818de002 | 481 | } |
41abdfbd JG |
482 | |
483 | /* if this symbol table is not relocatable, only line table should | |
484 | be relocated and the rest ignored. */ | |
485 | if (s->nonreloc) | |
486 | return; | |
487 | ||
488 | bv = BLOCKVECTOR(s); | |
489 | len = BLOCKVECTOR_NBLOCKS(bv); | |
490 | ||
491 | for (i = 0; i < len; i++) { | |
492 | b = BLOCKVECTOR_BLOCK(bv, i); | |
493 | ||
494 | BLOCK_START(b) += reloc; | |
495 | BLOCK_END(b) += reloc; | |
496 | ||
497 | blen = BLOCK_NSYMS(b); | |
498 | for (j = 0; j < blen; j++) { | |
499 | register struct symbol *sym; | |
500 | ||
501 | sym = BLOCK_SYM(b, j); | |
502 | switch (SYMBOL_NAMESPACE(sym)) { | |
503 | case STRUCT_NAMESPACE: | |
504 | case UNDEF_NAMESPACE: | |
505 | continue; | |
506 | ||
507 | case LABEL_NAMESPACE: | |
508 | case VAR_NAMESPACE: | |
509 | break; | |
510 | } | |
511 | ||
512 | switch (SYMBOL_CLASS(sym)) { | |
513 | case LOC_CONST: | |
514 | case LOC_CONST_BYTES: | |
515 | case LOC_LOCAL: | |
516 | case LOC_REGISTER: | |
517 | case LOC_ARG: | |
518 | case LOC_LOCAL_ARG: | |
519 | case LOC_REF_ARG: | |
520 | case LOC_REGPARM: | |
521 | case LOC_TYPEDEF: | |
522 | continue; | |
523 | ||
524 | #ifdef FIXME | |
525 | case LOC_EXTERNAL: | |
526 | #endif | |
527 | case LOC_LABEL: | |
528 | SYMBOL_VALUE_ADDRESS(sym) += reloc; | |
529 | break; | |
530 | ||
531 | case LOC_STATIC: | |
532 | SYMBOL_VALUE_ADDRESS(sym) += dreloc; | |
533 | break; | |
534 | ||
535 | case LOC_BLOCK: | |
536 | break; | |
537 | ||
538 | default: | |
539 | fatal("botched symbol class %x" | |
540 | , SYMBOL_CLASS(sym)); | |
541 | break; | |
542 | } | |
543 | } | |
544 | } | |
545 | } | |
546 | ||
547 | /* | |
548 | * add_vmap - add a new vmap entry based on ldinfo() information | |
549 | */ | |
550 | add_vmap(ldi) | |
551 | register struct ld_info *ldi; { | |
552 | bfd *bfd, *last; | |
507e4004 PB |
553 | register char *mem, *objname; |
554 | ||
555 | /* This ldi structure was allocated using alloca() in | |
9b280a7f | 556 | xcoff_relocate_symtab(). Now we need to have persistent object |
507e4004 | 557 | and member names, so we should save them. */ |
41abdfbd JG |
558 | |
559 | mem = ldi->ldinfo_filename + strlen(ldi->ldinfo_filename) + 1; | |
507e4004 PB |
560 | mem = savestring (mem, strlen (mem)); |
561 | objname = savestring (ldi->ldinfo_filename, strlen (ldi->ldinfo_filename)); | |
562 | ||
563 | bfd = bfd_fdopenr(objname, NULL, ldi->ldinfo_fd); | |
41abdfbd | 564 | if (!bfd) |
507e4004 PB |
565 | error("Could not open `%s' as an executable file: %s", |
566 | objname, bfd_errmsg(bfd_error)); | |
41abdfbd JG |
567 | |
568 | ||
569 | /* make sure we have an object file */ | |
570 | ||
571 | if (bfd_check_format(bfd, bfd_object)) | |
507e4004 | 572 | map_vmap (bfd, 0); |
41abdfbd JG |
573 | |
574 | else if (bfd_check_format(bfd, bfd_archive)) { | |
575 | last = 0; | |
576 | /* | |
577 | * FIXME??? am I tossing BFDs? bfd? | |
578 | */ | |
579 | while (last = bfd_openr_next_archived_file(bfd, last)) | |
2e4964ad | 580 | if (STREQ(mem, last->filename)) |
41abdfbd JG |
581 | break; |
582 | ||
583 | if (!last) { | |
507e4004 PB |
584 | bfd_close(bfd); |
585 | /* FIXME -- should be error */ | |
586 | warning("\"%s\": member \"%s\" missing.", bfd->filename, mem); | |
587 | return; | |
41abdfbd JG |
588 | } |
589 | ||
590 | if (!bfd_check_format(last, bfd_object)) { | |
591 | bfd_close(last); /* XXX??? */ | |
592 | goto obj_err; | |
593 | } | |
594 | ||
595 | map_vmap (last, bfd); | |
596 | } | |
597 | else { | |
598 | obj_err: | |
599 | bfd_close(bfd); | |
600 | /* FIXME -- should be error */ | |
601 | warning("\"%s\": not in executable format: %s." | |
507e4004 | 602 | , objname, bfd_errmsg(bfd_error)); |
41abdfbd JG |
603 | return; |
604 | } | |
605 | } | |
606 | ||
607 | ||
507e4004 PB |
608 | /* As well as symbol tables, exec_sections need relocation. After |
609 | the inferior process' termination, there will be a relocated symbol | |
610 | table exist with no corresponding inferior process. At that time, we | |
611 | need to use `exec' bfd, rather than the inferior process's memory space | |
612 | to look up symbols. | |
613 | ||
614 | `exec_sections' need to be relocated only once, as long as the exec | |
615 | file remains unchanged. | |
41abdfbd JG |
616 | */ |
617 | vmap_exec () | |
618 | { | |
619 | static bfd *execbfd; | |
1e939db1 SG |
620 | int i; |
621 | ||
41abdfbd JG |
622 | if (execbfd == exec_bfd) |
623 | return; | |
624 | ||
625 | execbfd = exec_bfd; | |
626 | ||
1e939db1 SG |
627 | if (!vmap || !exec_ops.to_sections) |
628 | error ("vmap_exec: vmap or exec_ops.to_sections == 0\n"); | |
507e4004 | 629 | |
1e939db1 SG |
630 | for (i=0; &exec_ops.to_sections[i] < exec_ops.to_sections_end; i++) |
631 | { | |
2e4964ad | 632 | if (STREQ(".text", exec_ops.to_sections[i].sec_ptr->name)) |
1e939db1 SG |
633 | { |
634 | exec_ops.to_sections[i].addr += vmap->tstart; | |
635 | exec_ops.to_sections[i].endaddr += vmap->tstart; | |
636 | } | |
2e4964ad | 637 | else if (STREQ(".data", exec_ops.to_sections[i].sec_ptr->name)) |
1e939db1 SG |
638 | { |
639 | exec_ops.to_sections[i].addr += vmap->dstart; | |
640 | exec_ops.to_sections[i].endaddr += vmap->dstart; | |
641 | } | |
642 | } | |
41abdfbd JG |
643 | } |
644 | ||
645 | ||
646 | int | |
647 | text_adjustment (abfd) | |
648 | bfd *abfd; | |
649 | { | |
650 | static bfd *execbfd; | |
651 | static int adjustment; | |
652 | sec_ptr sect; | |
653 | ||
654 | if (exec_bfd == execbfd) | |
655 | return adjustment; | |
656 | ||
657 | sect = bfd_get_section_by_name (abfd, ".text"); | |
658 | if (sect) | |
659 | adjustment = sect->filepos - sect->vma; | |
660 | else | |
661 | adjustment = 0x200; /* just a wild assumption */ | |
662 | ||
663 | return adjustment; | |
664 | } | |
665 | ||
666 | ||
667 | /* | |
668 | * vmap_ldinfo - update VMAP info with ldinfo() information | |
669 | * | |
670 | * Input: | |
671 | * ldi - ^ to ldinfo() results. | |
672 | */ | |
673 | vmap_ldinfo(ldi) | |
674 | register struct ld_info *ldi; | |
675 | { | |
676 | struct stat ii, vi; | |
677 | register struct vmap *vp; | |
678 | register got_one, retried; | |
679 | CORE_ADDR ostart; | |
680 | ||
681 | /* | |
682 | * for each *ldi, see if we have a corresponding *vp | |
683 | * if so, update the mapping, and symbol table. | |
684 | * if not, add an entry and symbol table. | |
685 | */ | |
686 | do { | |
687 | char *name = ldi->ldinfo_filename; | |
688 | char *memb = name + strlen(name) + 1; | |
689 | ||
690 | retried = 0; | |
691 | ||
692 | if (fstat(ldi->ldinfo_fd, &ii) < 0) | |
693 | fatal("cannot fstat(%d) on %s" | |
694 | , ldi->ldinfo_fd | |
695 | , name); | |
696 | retry: | |
697 | for (got_one = 0, vp = vmap; vp; vp = vp->nxt) { | |
1eeba686 PB |
698 | FILE *io; |
699 | ||
700 | /* First try to find a `vp', which is the same as in ldinfo. | |
701 | If not the same, just continue and grep the next `vp'. If same, | |
702 | relocate its tstart, tend, dstart, dend values. If no such `vp' | |
703 | found, get out of this for loop, add this ldi entry as a new vmap | |
704 | (add_vmap) and come back, fins its `vp' and so on... */ | |
705 | ||
706 | /* The filenames are not always sufficient to match on. */ | |
41abdfbd | 707 | |
2e4964ad FF |
708 | if ((name[0] == "/" && !STREQ(name, vp->name)) |
709 | || (memb[0] && !STREQ(memb, vp->member))) | |
1eeba686 | 710 | continue; |
41abdfbd | 711 | |
1eeba686 PB |
712 | io = bfd_cache_lookup(vp->bfd); /* totally opaque! */ |
713 | if (!io) | |
714 | fatal("cannot find BFD's iostream for %s", vp->name); | |
41abdfbd | 715 | |
1eeba686 | 716 | /* see if we are referring to the same file */ |
41abdfbd | 717 | |
1eeba686 PB |
718 | if (fstat(fileno(io), &vi) < 0) |
719 | fatal("cannot fstat BFD for %s", vp->name); | |
41abdfbd | 720 | |
1eeba686 PB |
721 | if (ii.st_dev != vi.st_dev || ii.st_ino != vi.st_ino) |
722 | continue; | |
41abdfbd | 723 | |
1eeba686 PB |
724 | if (!retried) |
725 | close(ldi->ldinfo_fd); | |
41abdfbd | 726 | |
1eeba686 | 727 | ++got_one; |
41abdfbd | 728 | |
1eeba686 PB |
729 | /* found a corresponding VMAP. remap! */ |
730 | ostart = vp->tstart; | |
41abdfbd | 731 | |
1eeba686 PB |
732 | vp->tstart = ldi->ldinfo_textorg; |
733 | vp->tend = vp->tstart + ldi->ldinfo_textsize; | |
734 | vp->dstart = ldi->ldinfo_dataorg; | |
735 | vp->dend = vp->dstart + ldi->ldinfo_datasize; | |
41abdfbd | 736 | |
1eeba686 PB |
737 | if (vp->tadj) { |
738 | vp->tstart += vp->tadj; | |
739 | vp->tend += vp->tadj; | |
740 | } | |
741 | ||
742 | /* relocate symbol table(s). */ | |
743 | vmap_symtab(vp, ostart, &vi); | |
41abdfbd | 744 | |
1eeba686 | 745 | /* there may be more, so we don't break out of the loop. */ |
41abdfbd JG |
746 | } |
747 | ||
1eeba686 PB |
748 | /* if there was no matching *vp, we must perforce create the sucker(s) */ |
749 | if (!got_one && !retried) { | |
750 | add_vmap(ldi); | |
751 | ++retried; | |
752 | goto retry; | |
41abdfbd JG |
753 | } |
754 | } while (ldi->ldinfo_next | |
755 | && (ldi = (void *) (ldi->ldinfo_next + (char *) ldi))); | |
756 | ||
41abdfbd JG |
757 | } |
758 | ||
759 | /* | |
760 | * vmap_inferior - print VMAP info for inferior | |
761 | */ | |
762 | vmap_inferior() { | |
763 | ||
764 | if (inferior_pid == 0) | |
507e4004 | 765 | return 0; /* normal processing */ |
41abdfbd JG |
766 | |
767 | exec_files_info(); | |
41abdfbd JG |
768 | return 1; |
769 | } | |
770 | ||
771 | /* Read or write the exec file. | |
772 | ||
773 | Args are address within exec file, address within gdb address-space, | |
774 | length, and a flag indicating whether to read or write. | |
775 | ||
776 | Result is a length: | |
777 | ||
778 | 0: We cannot handle this address and length. | |
779 | > 0: We have handled N bytes starting at this address. | |
780 | (If N == length, we did it all.) We might be able | |
781 | to handle more bytes beyond this length, but no | |
782 | promises. | |
783 | < 0: We cannot handle this address, but if somebody | |
784 | else handles (-N) bytes, we can start from there. | |
785 | ||
786 | The same routine is used to handle both core and exec files; | |
787 | we just tail-call it with more arguments to select between them. */ | |
788 | ||
789 | int | |
818de002 | 790 | xfer_memory (memaddr, myaddr, len, write, target) |
41abdfbd JG |
791 | CORE_ADDR memaddr; |
792 | char *myaddr; | |
793 | int len; | |
794 | int write; | |
818de002 | 795 | struct target_ops *target; |
41abdfbd JG |
796 | { |
797 | boolean res; | |
798 | struct section_table *p; | |
799 | CORE_ADDR nextsectaddr, memend; | |
818de002 | 800 | boolean (*xfer_fn) PARAMS ((bfd *, sec_ptr, PTR, file_ptr, bfd_size_type)); |
41abdfbd JG |
801 | |
802 | if (len <= 0) | |
803 | abort(); | |
804 | ||
805 | memend = memaddr + len; | |
806 | xfer_fn = write? bfd_set_section_contents: bfd_get_section_contents; | |
807 | nextsectaddr = memend; | |
808 | ||
818de002 | 809 | for (p = target->to_sections; p < target->to_sections_end; p++) |
41abdfbd JG |
810 | { |
811 | if (p->addr <= memaddr) | |
812 | if (p->endaddr >= memend) | |
813 | { | |
814 | /* Entire transfer is within this section. */ | |
818de002 | 815 | res = xfer_fn (p->bfd, p->sec_ptr, myaddr, memaddr - p->addr, len); |
41abdfbd JG |
816 | return (res != false)? len: 0; |
817 | } | |
818 | else if (p->endaddr <= memaddr) | |
819 | { | |
820 | /* This section ends before the transfer starts. */ | |
821 | continue; | |
822 | } | |
823 | else | |
824 | { | |
825 | /* This section overlaps the transfer. Just do half. */ | |
826 | len = p->endaddr - memaddr; | |
818de002 | 827 | res = xfer_fn (p->bfd, p->sec_ptr, myaddr, memaddr - p->addr, len); |
41abdfbd JG |
828 | return (res != false)? len: 0; |
829 | } | |
830 | else if (p->addr < nextsectaddr) | |
831 | nextsectaddr = p->addr; | |
832 | } | |
833 | ||
834 | if (nextsectaddr >= memend) | |
835 | return 0; /* We can't help */ | |
836 | else | |
837 | return - (nextsectaddr - memaddr); /* Next boundary where we can help */ | |
838 | } | |
839 | ||
818de002 PB |
840 | void |
841 | print_section_info (t, abfd) | |
842 | struct target_ops *t; | |
843 | bfd *abfd; | |
41abdfbd | 844 | { |
818de002 | 845 | struct section_table *p; |
41abdfbd | 846 | |
818de002 PB |
847 | printf_filtered ("\t`%s', ", bfd_get_filename(abfd)); |
848 | wrap_here (" "); | |
849 | printf_filtered ("file type %s.\n", bfd_get_target(abfd)); | |
850 | ||
851 | for (p = t->to_sections; p < t->to_sections_end; p++) { | |
852 | printf_filtered ("\t%s", local_hex_string_custom (p->addr, "08")); | |
853 | printf_filtered (" - %s", local_hex_string_custom (p->endaddr, "08")); | |
854 | if (info_verbose) | |
855 | printf_filtered (" @ %s", | |
856 | local_hex_string_custom (p->sec_ptr->filepos, "08")); | |
857 | printf_filtered (" is %s", bfd_section_name (p->bfd, p->sec_ptr)); | |
858 | if (p->bfd != abfd) { | |
859 | printf_filtered (" in %s", bfd_get_filename (p->bfd)); | |
860 | } | |
861 | printf_filtered ("\n"); | |
862 | } | |
818de002 PB |
863 | } |
864 | ||
507e4004 | 865 | |
818de002 PB |
866 | static void |
867 | exec_files_info (t) | |
868 | struct target_ops *t; | |
869 | { | |
507e4004 PB |
870 | register struct vmap *vp = vmap; |
871 | ||
818de002 | 872 | print_section_info (t, exec_bfd); |
507e4004 PB |
873 | |
874 | if (!vp) | |
875 | return; | |
876 | ||
1eeba686 PB |
877 | printf("\tMapping info for file `%s'.\n", vp->name); |
878 | ||
879 | printf("\t %8.8s %8.8s %8.8s %8.8s %8.8s %s\n", | |
880 | "tstart", "tend", "dstart", "dend", "section", "file(member)"); | |
507e4004 PB |
881 | |
882 | for (; vp; vp = vp->nxt) | |
1eeba686 PB |
883 | printf("\t0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x %s%s%s%s\n", |
884 | vp->tstart, | |
885 | vp->tend, | |
886 | vp->dstart, | |
887 | vp->dend, | |
888 | vp->name, | |
889 | *vp->member ? "(" : "", | |
890 | vp->member, | |
891 | *vp->member ? ")" : ""); | |
41abdfbd JG |
892 | } |
893 | ||
894 | #ifdef DAMON | |
76b28d05 | 895 | /* Damon's implementation of set_section_command! It is based on the sex member |
41abdfbd JG |
896 | (which is a section pointer from vmap) of vmap. |
897 | We will not have multiple vmap entries (one for each section), rather transmit | |
898 | text and data base offsets and fix them at the same time. Elimination of sex | |
899 | entry in vmap make this function obsolute, use the one from exec.c. | |
76b28d05 | 900 | Need further testing!! FIXMEmgo. */ |
41abdfbd JG |
901 | |
902 | static void | |
903 | set_section_command(args, from_tty) | |
904 | char *args; | |
905 | { | |
906 | register struct vmap *vp = vmap; | |
907 | char *secname; | |
908 | unsigned seclen; | |
909 | unsigned long secaddr; | |
910 | char secprint[100]; | |
911 | long offset; | |
912 | ||
913 | if (args == 0) | |
914 | error("Must specify section name and its virtual address"); | |
915 | ||
916 | /* Parse out section name */ | |
917 | for (secname = args; !isspace(*args); args++) | |
918 | ; | |
919 | seclen = args - secname; | |
920 | ||
921 | /* Parse out new virtual address */ | |
922 | secaddr = parse_and_eval_address(args); | |
923 | ||
924 | for (vp = vmap; vp; vp = vp->nxt) { | |
925 | if (!strncmp(secname | |
926 | , bfd_section_name(vp->bfd, vp->sex), seclen) | |
927 | && bfd_section_name(vp->bfd, vp->sex)[seclen] == '\0') { | |
928 | offset = secaddr - vp->tstart; | |
929 | vp->tstart += offset; | |
930 | vp->tend += offset; | |
931 | exec_files_info(); | |
932 | return; | |
933 | } | |
934 | } | |
935 | ||
936 | if (seclen >= sizeof(secprint)) | |
937 | seclen = sizeof(secprint) - 1; | |
938 | strncpy(secprint, secname, seclen); | |
939 | secprint[seclen] = '\0'; | |
940 | error("Section %s not found", secprint); | |
941 | } | |
942 | #else | |
943 | static void | |
944 | set_section_command (args, from_tty) | |
945 | char *args; | |
946 | int from_tty; | |
947 | { | |
948 | struct section_table *p; | |
949 | char *secname; | |
950 | unsigned seclen; | |
951 | unsigned long secaddr; | |
952 | char secprint[100]; | |
953 | long offset; | |
954 | ||
955 | if (args == 0) | |
956 | error ("Must specify section name and its virtual address"); | |
957 | ||
958 | /* Parse out section name */ | |
959 | for (secname = args; !isspace(*args); args++) ; | |
960 | seclen = args - secname; | |
961 | ||
962 | /* Parse out new virtual address */ | |
963 | secaddr = parse_and_eval_address (args); | |
964 | ||
507e4004 | 965 | for (p = exec_ops.to_sections; p < exec_ops.to_sections_end; p++) { |
41abdfbd JG |
966 | if (!strncmp (secname, bfd_section_name (exec_bfd, p->sec_ptr), seclen) |
967 | && bfd_section_name (exec_bfd, p->sec_ptr)[seclen] == '\0') { | |
968 | offset = secaddr - p->addr; | |
969 | p->addr += offset; | |
970 | p->endaddr += offset; | |
507e4004 PB |
971 | if (from_tty) |
972 | exec_files_info(&exec_ops); | |
41abdfbd JG |
973 | return; |
974 | } | |
975 | } | |
976 | if (seclen >= sizeof (secprint)) | |
977 | seclen = sizeof (secprint) - 1; | |
978 | strncpy (secprint, secname, seclen); | |
979 | secprint[seclen] = '\0'; | |
980 | error ("Section %s not found", secprint); | |
981 | } | |
982 | ||
983 | #endif /* !DAMON */ | |
984 | ||
985 | struct target_ops exec_ops = { | |
986 | "exec", "Local exec file", | |
987 | "Use an executable file as a target.\n\ | |
988 | Specify the filename of the executable file.", | |
989 | exec_file_command, exec_close, /* open, close */ | |
050d45c5 | 990 | find_default_attach, 0, 0, 0, /* attach, detach, resume, wait, */ |
41abdfbd | 991 | 0, 0, /* fetch_registers, store_registers, */ |
a03d4f8e | 992 | 0, /* prepare_to_store */ |
818de002 | 993 | xfer_memory, exec_files_info, |
41abdfbd JG |
994 | 0, 0, /* insert_breakpoint, remove_breakpoint, */ |
995 | 0, 0, 0, 0, 0, /* terminal stuff */ | |
996 | 0, 0, /* kill, load */ | |
e17960fb | 997 | 0, /* lookup sym */ |
050d45c5 | 998 | find_default_create_inferior, |
41abdfbd | 999 | 0, /* mourn_inferior */ |
5ee4e16c | 1000 | 0, /* can_run */ |
3950a34e | 1001 | 0, /* notice_signals */ |
41abdfbd JG |
1002 | file_stratum, 0, /* next */ |
1003 | 0, 1, 0, 0, 0, /* all mem, mem, stack, regs, exec */ | |
1004 | 0, 0, /* section pointers */ | |
1005 | OPS_MAGIC, /* Always the last thing */ | |
1006 | }; | |
1007 | ||
1008 | ||
1009 | void | |
1010 | _initialize_exec() | |
1011 | { | |
1012 | ||
1013 | add_com("file", class_files, file_command, | |
1014 | "Use FILE as program to be debugged.\n\ | |
1015 | It is read for its symbols, for getting the contents of pure memory,\n\ | |
1016 | and it is the program executed when you use the `run' command.\n\ | |
1017 | If FILE cannot be found as specified, your execution directory path\n\ | |
1018 | ($PATH) is searched for a command of that name.\n\ | |
1019 | No arg means to have no executable file and no symbols."); | |
1020 | ||
1021 | add_com("exec-file", class_files, exec_file_command, | |
1022 | "Use FILE as program for getting contents of pure memory.\n\ | |
1023 | If FILE cannot be found as specified, your execution directory path\n\ | |
1024 | is searched for a command of that name.\n\ | |
1025 | No arg means have no executable file."); | |
1026 | ||
1027 | add_com("section", class_files, set_section_command, | |
1028 | "Change the base address of section SECTION of the exec file to ADDR.\n\ | |
1029 | This can be used if the exec file does not contain section addresses,\n\ | |
1030 | (such as in the a.out format), or when the addresses specified in the\n\ | |
1031 | file itself are wrong. Each section must be changed separately. The\n\ | |
1032 | ``info files'' command lists all the sections and their addresses."); | |
1033 | ||
1034 | add_target(&exec_ops); | |
1035 | } |