match_cb)
{
int res;
ULONGEST memaddr = offset;
ULONGEST memend = memaddr + len;
gdb_assert (len != 0);
for (const target_section &p : sections)
{
struct bfd_section *asect = p.the_bfd_section;
bfd *abfd = asect->owner;
if (match_cb != nullptr && !match_cb (&p))
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. */
}
target_section_table *
exec_target::get_section_table ()
{
return ¤t_program_space->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)
{
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);
else
return TARGET_XFER_E_IO;
}
void
print_section_info (target_section_table *t, bfd *abfd)
{
struct gdbarch *gdbarch = gdbarch_from_bfd (abfd);
/* FIXME: 16 is not wide enough when gdbarch_addr_bit > 64. */
int wid = gdbarch_addr_bit (gdbarch) <= 32 ? 8 : 16;
printf_filtered ("\t`%ps', ",
styled_string (file_name_style.style (),
bfd_get_filename (abfd)));
wrap_here (" ");
printf_filtered (_("file type %s.\n"), bfd_get_target (abfd));
if (abfd == current_program_space->exec_bfd ())
{
/* gcc-3.4 does not like the initialization in
sections_end>. */
bfd_vma displacement = 0;
bfd_vma entry_point;
bool found = false;
for (const target_section &p : *t)
{
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);
found = true;
break;
}
}
if (!found)
warning (_("Cannot find section for the entry point of %ps."),
styled_string (file_name_style.style (),
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 (const target_section &p : *t)
{
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 %ps",
styled_string (file_name_style.style (),
bfd_get_filename (pbfd)));
printf_filtered ("\n");
}
}
void
exec_target::files_info ()
{
if (current_program_space->exec_bfd ())
print_section_info (¤t_program_space->target_sections,
current_program_space->exec_bfd ());
else
puts_filtered (_("\t\n"));
}
static void
set_section_command (const char *args, int from_tty)
{
const char *secname;
unsigned seclen;
unsigned long secaddr;
char secprint[100];
long offset;
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);
for (target_section &p : current_program_space->target_sections)
{
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)
{
for (target_section &p : current_program_space->target_sections)
{
if (filename_cmp (filename,
bfd_get_filename (p.the_bfd_section->owner)) == 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_program_space->target_sections.empty ();
}
gdb::unique_xmalloc_ptr
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_setshow_enum_cmd ("exec-file-mismatch", class_support,
exec_file_mismatch_names,
&exec_file_mismatch,
_("\
Set exec-file-mismatch handling (ask|warn|off)."),
_("\
Show exec-file-mismatch handling (ask|warn|off)."),
_("\
Specifies how to handle a mismatch between the current exec-file\n\
loaded by GDB and the exec-file automatically determined when attaching\n\
to a process:\n\n\
ask - warn the user and ask whether to load the determined exec-file.\n\
warn - warn the user, but do not change the exec-file.\n\
off - do not check for mismatch.\n\
\n\
GDB detects a mismatch by comparing the build IDs of the files.\n\
If the user confirms loading the determined exec-file, then its symbols\n\
will be loaded as well."),
set_exec_file_mismatch_command,
show_exec_file_mismatch_command,
&setlist, &showlist);
add_target (exec_target_info, exec_target_open, filename_completer);
}