Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i#7046: Add loadable segment program headers to memory dump file. #7104

Merged
merged 4 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 64 additions & 9 deletions core/unix/coredump.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@
#define PROGRAM_HEADER_NOTE_LENGTH \
(sizeof(ELF_NOTE_HEADER_TYPE) + NOTE_OWNER_LENGTH + sizeof(struct elf_prstatus) + \
sizeof(ELF_NOTE_HEADER_TYPE) + NOTE_OWNER_LENGTH + sizeof(elf_fpregset_t))
#define PROGRAM_HEADER_LENGTH \
(sizeof(ELF_PROGRAM_HEADER_TYPE) + PROGRAM_HEADER_NOTE_LENGTH)
#define PROGRAM_HEADER_LOAD_ALIGNMENT 0x1000
ivankyluk marked this conversation as resolved.
Show resolved Hide resolved

typedef struct _section_header_info_t {
app_pc vm_start;
Expand Down Expand Up @@ -274,6 +273,23 @@ mcontext_to_user_regs(DR_PARAM_IN priv_mcontext_t *mcontext,
#endif
}

/*
* Return the entry point of the program.
*/
uint64_t
get_entry_point(DR_PARAM_IN priv_mcontext_t *mcontext)
{
#ifdef DR_HOST_NOT_TARGET
return 0;
ivankyluk marked this conversation as resolved.
Show resolved Hide resolved
#elif defined(X86)
return (uint64_t)mcontext->rip;
#elif defined(AARCH64)
return (uint64_t)mcontext->pc;
ivankyluk marked this conversation as resolved.
Show resolved Hide resolved
#else
# error Unsupported architecture
#endif
}

/*
* Write prstatus structure to the file. Returns true if the note is written to
* the file, false otherwise.
Expand Down Expand Up @@ -479,26 +495,62 @@ os_dump_core_internal(dcontext_t *dcontext)
return false;
}

if (!write_elf_header(elf_file, /*entry_point=*/0,
if (!write_elf_header(elf_file, /*entry_point=*/get_entry_point(&mc),
/*section_header_table_offset*/ sizeof(ELF_HEADER_TYPE) +
PROGRAM_HEADER_LENGTH + section_data_size,
PROGRAM_HEADER_NOTE_LENGTH +
sizeof(ELF_PROGRAM_HEADER_TYPE) * section_count +
section_data_size,
/*flags=*/0,
/*program_header_count=*/1,
/*program_header_count=*/section_count,
ivankyluk marked this conversation as resolved.
Show resolved Hide resolved
/*section_header_count=*/section_count,
/*section_string_table_index=*/section_count - 1)) {
os_close(elf_file);
return false;
}
// Write program header table entry.
if (!write_program_header(elf_file, PT_NOTE, /*flags=*/0,
/*offset=*/sizeof(ELF_HEADER_TYPE) +
sizeof(ELF_PROGRAM_HEADER_TYPE),
sizeof(ELF_PROGRAM_HEADER_TYPE) * section_count,
ivankyluk marked this conversation as resolved.
Show resolved Hide resolved
/*virtual_address=*/0,
/*physical_address=*/0,
/*file_size=*/PROGRAM_HEADER_NOTE_LENGTH,
/*memory_size=*/0, /*alignment=*/4)) {
/*memory_size=*/PROGRAM_HEADER_NOTE_LENGTH,
/*alignment=*/sizeof(ELF_HALF))) {
os_close(elf_file);
return false;
}
// Write loadable program segment program headers.
ELF_OFF file_offset = sizeof(ELF_HEADER_TYPE) +
sizeof(ELF_PROGRAM_HEADER_TYPE) * section_count + PROGRAM_HEADER_NOTE_LENGTH;
// TODO i#7046: Merge adjacent sections with the same prot values.
for (int section_index = 0; section_index < section_count - 1; ++section_index) {
ivankyluk marked this conversation as resolved.
Show resolved Hide resolved
ELF_WORD flags = 0;
if (TEST(PROT_EXEC, section_header_info[section_index].prot)) {
flags |= PF_X;
}
if (TEST(PROT_WRITE, section_header_info[section_index].prot)) {
flags |= PF_W;
}
if (TEST(PROT_READ, section_header_info[section_index].prot)) {
flags |= PF_R;
}
const ELF_WORD size = section_header_info[section_index].vm_end -
section_header_info[section_index].vm_start;
if (!write_program_header(
elf_file, PT_LOAD, flags,
/*offset=*/file_offset,
/*virtual_address=*/(ELF_ADDR)section_header_info[section_index].vm_start,
/*physical_address=*/
(ELF_ADDR)section_header_info[section_index].vm_start,
/*file_size=*/size,
/*memory_size=*/size,
/*alignment=*/PROGRAM_HEADER_LOAD_ALIGNMENT)) {
os_close(elf_file);
return false;
}
file_offset += section_header_info[section_index].vm_end -
section_header_info[section_index].vm_start;
}
if (!write_prstatus_note(&mc, elf_file)) {
os_close(elf_file);
return false;
Expand Down Expand Up @@ -533,8 +585,8 @@ os_dump_core_internal(dcontext_t *dcontext)
return false;
}
// Write section headers to the core dump file.
// TODO i#7046: Handle multiple program headers.
ELF_OFF file_offset = sizeof(ELF_HEADER_TYPE) + PROGRAM_HEADER_LENGTH;
file_offset = sizeof(ELF_HEADER_TYPE) +
sizeof(ELF_PROGRAM_HEADER_TYPE) * section_count + PROGRAM_HEADER_NOTE_LENGTH;
ivankyluk marked this conversation as resolved.
Show resolved Hide resolved

// The section_count includes the section name section, so we need to skip
// it in the loop. The section name section is handled differently after
Expand All @@ -544,6 +596,9 @@ os_dump_core_internal(dcontext_t *dcontext)
if (TEST(PROT_WRITE, section_header_info[section_index].prot)) {
flags |= SHF_WRITE;
}
if (TEST(PROT_EXEC, section_header_info[section_index].prot)) {
flags |= SHF_EXECINSTR;
}
if (!write_section_header(
elf_file, section_header_info[section_index].name_offset, SHT_PROGBITS,
flags, (ELF_ADDR)section_header_info[section_index].vm_start, file_offset,
Expand Down
5 changes: 3 additions & 2 deletions suite/tests/client-interface/memory_dump_test.templatex
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ ELF Header:
Type: CORE \(Core file\)
Machine: .*
Version: 0x1
Entry point address: 0x0
Entry point address: 0x[0-9a-f]+
Start of program headers: 64 \(bytes into file\)
Start of section headers: [0-9]+ \(bytes into file\)
Flags: 0x0
Size of this header: 64 \(bytes\)
Size of program headers: 56 \(bytes\)
Number of program headers: 1
Number of program headers: [0-9]+
ivankyluk marked this conversation as resolved.
Show resolved Hide resolved
Size of section headers: 64 \(bytes\)
Number of section headers: [0-9]+
Section header string table index: [0-9]+
Expand All @@ -33,6 +33,7 @@ Program Headers:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags Align
NOTE.*
LOAD.*
.*
Displaying notes found at file offset 0x[0-9a-f]+ with length 0x[0-9a-f]+:
Owner.*Data size.*Description
Expand Down
Loading