(offset + len, r.start + r.length);
gdb_assert (end - offset <= len);
if (offset >= r.start)
status = exec_read_partial_read_only (readbuf, offset,
end - offset,
xfered_len);
else
{
*xfered_len = r.start - offset;
status = TARGET_XFER_UNAVAILABLE;
}
return status;
}
}
*xfered_len = len;
return TARGET_XFER_UNAVAILABLE;
}
enum target_xfer_status
section_table_xfer_memory_partial (gdb_byte *readbuf, const gdb_byte *writebuf,
ULONGEST offset, ULONGEST len,
ULONGEST *xfered_len,
struct target_section *sections,
struct target_section *sections_end,
const char *section_name)
{
int res;
struct target_section *p;
ULONGEST memaddr = offset;
ULONGEST memend = memaddr + len;
if (len == 0)
internal_error (__FILE__, __LINE__,
_("failed internal consistency check"));
for (p = sections; p < sections_end; p++)
{
struct bfd_section *asect = p->the_bfd_section;
bfd *abfd = asect->owner;
if (section_name && strcmp (section_name, asect->name) != 0)
continue; /* not the section we need. */
if (memaddr >= p->addr)
{
if (memend <= p->endaddr)
{
/* Entire transfer is within this section. */
if (writebuf)
res = bfd_set_section_contents (abfd, asect,
writebuf, memaddr - p->addr,
len);
else
res = bfd_get_section_contents (abfd, asect,
readbuf, memaddr - p->addr,
len);
if (res != 0)
{
*xfered_len = len;
return TARGET_XFER_OK;
}
else
return TARGET_XFER_EOF;
}
else if (memaddr >= p->endaddr)
{
/* This section ends before the transfer starts. */
continue;
}
else
{
/* This section overlaps the transfer. Just do half. */
len = p->endaddr - memaddr;
if (writebuf)
res = bfd_set_section_contents (abfd, asect,
writebuf, memaddr - p->addr,
len);
else
res = bfd_get_section_contents (abfd, asect,
readbuf, memaddr - p->addr,
len);
if (res != 0)
{
*xfered_len = len;
return TARGET_XFER_OK;
}
else
return TARGET_XFER_EOF;
}
}
}
return TARGET_XFER_EOF; /* We can't help. */
}
struct target_section_table *
exec_target::get_section_table ()
{
return current_target_sections;
}
enum target_xfer_status
exec_target::xfer_partial (enum target_object object,
const char *annex, gdb_byte *readbuf,
const gdb_byte *writebuf,
ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
{
struct target_section_table *table = get_section_table ();
if (object == TARGET_OBJECT_MEMORY)
return section_table_xfer_memory_partial (readbuf, writebuf,
offset, len, xfered_len,
table->sections,
table->sections_end,
NULL);
else
return TARGET_XFER_E_IO;
}
void
print_section_info (struct target_section_table *t, bfd *abfd)
{
struct gdbarch *gdbarch = gdbarch_from_bfd (abfd);
struct target_section *p;
/* FIXME: 16 is not wide enough when gdbarch_addr_bit > 64. */
int wid = gdbarch_addr_bit (gdbarch) <= 32 ? 8 : 16;
printf_filtered ("\t`%s', ", bfd_get_filename (abfd));
wrap_here (" ");
printf_filtered (_("file type %s.\n"), bfd_get_target (abfd));
if (abfd == exec_bfd)
{
/* gcc-3.4 does not like the initialization in
sections_end>. */
bfd_vma displacement = 0;
bfd_vma entry_point;
for (p = t->sections; p < t->sections_end; p++)
{
struct bfd_section *psect = p->the_bfd_section;
if ((bfd_section_flags (psect) & (SEC_ALLOC | SEC_LOAD))
!= (SEC_ALLOC | SEC_LOAD))
continue;
if (bfd_section_vma (psect) <= abfd->start_address
&& abfd->start_address < (bfd_section_vma (psect)
+ bfd_section_size (psect)))
{
displacement = p->addr - bfd_section_vma (psect);
break;
}
}
if (p == t->sections_end)
warning (_("Cannot find section for the entry point of %s."),
bfd_get_filename (abfd));
entry_point = gdbarch_addr_bits_remove (gdbarch,
bfd_get_start_address (abfd)
+ displacement);
printf_filtered (_("\tEntry point: %s\n"),
paddress (gdbarch, entry_point));
}
for (p = t->sections; p < t->sections_end; p++)
{
struct bfd_section *psect = p->the_bfd_section;
bfd *pbfd = psect->owner;
printf_filtered ("\t%s", hex_string_custom (p->addr, wid));
printf_filtered (" - %s", hex_string_custom (p->endaddr, wid));
/* FIXME: A format of "08l" is not wide enough for file offsets
larger than 4GB. OTOH, making it "016l" isn't desirable either
since most output will then be much wider than necessary. It
may make sense to test the size of the file and choose the
format string accordingly. */
/* FIXME: i18n: Need to rewrite this sentence. */
if (info_verbose)
printf_filtered (" @ %s",
hex_string_custom (psect->filepos, 8));
printf_filtered (" is %s", bfd_section_name (psect));
if (pbfd != abfd)
printf_filtered (" in %s", bfd_get_filename (pbfd));
printf_filtered ("\n");
}
}
void
exec_target::files_info ()
{
if (exec_bfd)
print_section_info (current_target_sections, exec_bfd);
else
puts_filtered (_("\t\n"));
}
static void
set_section_command (const char *args, int from_tty)
{
struct target_section *p;
const char *secname;
unsigned seclen;
unsigned long secaddr;
char secprint[100];
long offset;
struct target_section_table *table;
if (args == 0)
error (_("Must specify section name and its virtual address"));
/* Parse out section name. */
for (secname = args; !isspace (*args); args++);
seclen = args - secname;
/* Parse out new virtual address. */
secaddr = parse_and_eval_address (args);
table = current_target_sections;
for (p = table->sections; p < table->sections_end; p++)
{
if (!strncmp (secname, bfd_section_name (p->the_bfd_section), seclen)
&& bfd_section_name (p->the_bfd_section)[seclen] == '\0')
{
offset = secaddr - p->addr;
p->addr += offset;
p->endaddr += offset;
if (from_tty)
exec_ops.files_info ();
return;
}
}
if (seclen >= sizeof (secprint))
seclen = sizeof (secprint) - 1;
strncpy (secprint, secname, seclen);
secprint[seclen] = '\0';
error (_("Section %s not found"), secprint);
}
/* If we can find a section in FILENAME with BFD index INDEX, adjust
it to ADDRESS. */
void
exec_set_section_address (const char *filename, int index, CORE_ADDR address)
{
struct target_section *p;
struct target_section_table *table;
table = current_target_sections;
for (p = table->sections; p < table->sections_end; p++)
{
if (filename_cmp (filename, p->the_bfd_section->owner->filename) == 0
&& index == p->the_bfd_section->index)
{
p->endaddr += address - p->addr;
p->addr = address;
}
}
}
bool
exec_target::has_memory ()
{
/* We can provide memory if we have any file/target sections to read
from. */
return (current_target_sections->sections
!= current_target_sections->sections_end);
}
char *
exec_target::make_corefile_notes (bfd *obfd, int *note_size)
{
error (_("Can't create a corefile"));
}
int
exec_target::find_memory_regions (find_memory_region_ftype func, void *data)
{
return objfile_find_memory_regions (this, func, data);
}
void _initialize_exec ();
void
_initialize_exec ()
{
struct cmd_list_element *c;
if (!dbx_commands)
{
c = add_cmd ("file", class_files, file_command, _("\
Use FILE as program to be debugged.\n\
It is read for its symbols, for getting the contents of pure memory,\n\
and it is the program executed when you use the `run' command.\n\
If FILE cannot be found as specified, your execution directory path\n\
($PATH) is searched for a command of that name.\n\
No arg means to have no executable file and no symbols."), &cmdlist);
set_cmd_completer (c, filename_completer);
}
c = add_cmd ("exec-file", class_files, exec_file_command, _("\
Use FILE as program for getting contents of pure memory.\n\
If FILE cannot be found as specified, your execution directory path\n\
is searched for a command of that name.\n\
No arg means have no executable file."), &cmdlist);
set_cmd_completer (c, filename_completer);
add_com ("section", class_files, set_section_command, _("\
Change the base address of section SECTION of the exec file to ADDR.\n\
This can be used if the exec file does not contain section addresses,\n\
(such as in the a.out format), or when the addresses specified in the\n\
file itself are wrong. Each section must be changed separately. The\n\
``info files'' command lists all the sections and their addresses."));
add_setshow_boolean_cmd ("write", class_support, &write_files, _("\
Set writing into executable and core files."), _("\
Show writing into executable and core files."), NULL,
NULL,
show_write_files,
&setlist, &showlist);
add_target (exec_target_info, exec_target_open, filename_completer);
}