createdump utility: adding program header, sections and other info
Now sections are added to minidump in incorrect way. We try to read section headers and strtab from process memory while they can be not loaded. If they aren't loaded into memory we will read some other data and get crash.
According to dotnet/runtime#8373 it's needed for symbol acquisition tool. Does the symbol acquisition tool need section headers and strtab in minidump if there is no guarantees that they are in memory? During minidump creating we can read section headers and strtab from file and add only sections theyself (.eh_frame, .eh_frame_hdr, .note.gnu.build-id, .note.gnu.ABI-tag, .gnu_debuglink). Is it enough?
Also we add sections using sh_offset value:
InsertMemoryRegion(baseAddress + sh.sh_offset, sh.sh_size);
I think we should use sh_addr instead of sh_offset. Is it right?
Thanks.
@mikem8361 PTAL
CC @Dmitri-Botcharnikov @ayuckhulk
I'm not sure the exact logic for when the section headers are in-memory or not, but they are usually there. And when they are not, createdump doesn't crash, either it doesn't write anything (read memory failure) or write garbage to the dump. The tools (the new symbol acquisition tool I'm working on and debuggers like lldb) are resilient to the garbage.
Some of the sections (.eh_frame and .note.gnu.build-id) usually have program headers that are already added, but the .gnu_debuglink is very useful if it exists for the symbol tool.
The .eh_frame and .eh_frame_hdr sections are needed by stack frame unwind code.
"sh_offset" works in all the cases I've tested and for the interesting sections like .gnu_debuglink it sh_addr is 0 and sh_offset has the proper in-memory offset.
Actually, minidump utility crashes all the time on my machine 😄
There are to cases:
- Reading garbage leads to throwing
std::bad_allocbecause of huge memory allocations; - TRACE() function causes SIGSEGV when it tries to print garbage strings, but the strings are not null-terminated (because they contain garbage).
ELF in memory is not the same as ELF on disk so you can not just use file offset to read thr sections. More info about loading ELFs is here http://refspecs.linuxfoundation.org/ELF/zSeries/lzsabi0_zSeries/c2083.html Reading ELF from memory to get the sections data is not a trivial task - you can get the basic idea from https://github.com/cuviper/elfutils/blob/master/libdwfl/elf-from-memory.c
I suggest to skip reading sections from memory if they are available on disk - debuggers load them just fine from the executable and there is no need to implement logic for reading ELF from memory.
Yes, I guess reading the sections should be removed if you are having all those problems since others will have them too. I only ran into for debug builds of coreclr not release builds (that seems strange) and only for a couple of modules (libcoreclr.so and libclrjit.so). I looked all over the internet for info on when it was valid to read the sections from memory and never found anything. It sounds like from you it is never deterministic. The doc you reference only really talks about program headers and those are always in memory and valid since you can use “dl_iterate_phdr” to enumerate them. The code you pointed to only seems to read the program headers not the section headers, but I could be mistaken.
Yes, debuggers like lldb/gdb will read the ELF file on disk if exists, but my symbol tool may only have a core dump to parse. It could attempt to download the ELF module from the core first and then parse that to get the linked symbol info (.gnu_debuglink).
The only problem is that I added this code to 2.0 for which it may be too late to remove this code.
I'm curious if createdump still crashes for you even after @kbaladurin recent valid section header changes?
We can read section headers from memory when there are in loadable segments (they are described in program headers with PT_LOAD type). Function elf_from_remote_memory from elf-from-memory.c checks whether section headers are in memory using information from PT_LOAD program headers (it used to calculate contents_size):
/* If the segments visible in memory didn't include the section
headers, then clear them from the file header. */
if (contents_size < shdrs_end)
{
ehdr.e32.e_shoff = 0;
ehdr.e32.e_shnum = 0;
ehdr.e32.e_shstrndx = 0;
}
I tried this approach and got that all libraries don't contain sections headers in loadable segments.
They appear in memory due to features of ld.so. If loadable segments aren't contiguous (there are gapes between them) it maps them to continuous memory region and change permissions of gapes to PROT_NONE.
For example, libc.so has following loadable segments:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flags Align
LOAD 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x00000000001bfad0 0x00000000001bfad0 R E 200000
LOAD 0x00000000001c07c0 0x00000000003c07c0 0x00000000003c07c0 0x0000000000004f60 0x00000000000091e0 RW 200000
and offset of section headers is 0x1c72b8.
ld maps this segments to one continuous region:
7ffff6d4b000-7ffff6f0b000 r-xp 00000000 08:01 13430788 /lib/x86_64-linux-gnu/libc-2.23.so
7ffff6f0b000-7ffff710b000 ---p 001c0000 08:01 13430788 /lib/x86_64-linux-gnu/libc-2.23.so
7ffff710b000-7ffff710f000 r--p 001c0000 08:01 13430788 /lib/x86_64-linux-gnu/libc-2.23.so
7ffff710f000-7ffff7111000 rw-p 001c4000 08:01 13430788 /lib/x86_64-linux-gnu/libc-2.23.so
0x200000 bytes from the offset 0x001c0000 are mapped to gap between section. And this region contains section headers. Size of the gap depends from alignment of segment. So if alignment will be smaller section headers will not be loaded. Also other implementations of loader can not map anything to gap.
With my changes createdump doesn't crash. But my check works not in all cases. Even if address of the section header in maps, data may be still invalid. For example, if the section header will be in the range [p_offset + p_filesz, p_offset + p_memsz] it will be zero out.
@mikem8361 Well, crashes became less frequent.
The reasons I had for adding the section headers and some of the sections are not as important any more in light of all the problems reading them cause. One was for the symbol acquisition tool I'm working to get the .gnu_debuglink section for the name of the linked symbol file. But for all the coreclr built modules, I can assume is the module name + ".dbg".
The other reason was for the ".eh_frame_hdr" for a future stack unwind fix on core dumps I'll be working on. The EH_GNU_EH_FRAME type program header/contents should be enough for that. The PT_NOTE for the build id is in is also covered by a program header.
The only other scenario I was thinking with lldb loading a "full" core dump with just the symbol module files and not the modules themselves. lldb will use the in-memory section info if all the section headers and section contents are valid and in-memory and then it will automatically load the linked symbol files. This isn't a very important scenario at all because most core dumps won't be "full: and include the section info and it will be easy with the symbol acquisition tool to get the modules too.
l'm sorry @kbaladurin and @ayuckhulk for all your time and effort trying to make this work, but I do think the best thing to do is to remove the section header and content reading code.
Removed the section header/content code.
@mikem8361 thank you!