From ea8a0b8b29eb08d3f0f6ac40942a2d8e98ab57ee Mon Sep 17 00:00:00 2001 From: Georgii Rymar Date: Tue, 3 Nov 2020 14:02:01 +0300 Subject: [llvm-readelf/obj] - Allow dumping of ELF header even if some elements are corrupt. For creating `ELFObjectFile` instances we have the factory method `ELFObjectFile::create(MemoryBufferRef Object)`. The problem of this method is that it scans the section header to locate some sections. When a file is truncated or has broken fields in the ELF header, this approach does not allow us to create the `ELFObjectFile` and dump the ELF header. This is https://bugs.llvm.org/show_bug.cgi?id=40804 This patch suggests a solution - it allows to delay scaning sections in the `ELFObjectFile::create`. It now allows user code to call an object initialization (`initContent()`) later. With that it is possible, for example, for dumpers just to dump the file header and exit. By default initialization is still performed as before, what helps to keep the logic of existent callers untouched. I've experimented with different approaches when worked on this patch. I think this approach is better than doing initialization of sections (i.e. scan of them) on demand, because normally users of `ELFObjectFile` API expect to work with a valid object. In most cases when a section header table can't be read (because of an error), we don't have to continue to work with object. So we probably don't need to implement a more complex API. Differential revision: https://reviews.llvm.org/D90903 --- llvm/lib/Object/ObjectFile.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'llvm/lib/Object/ObjectFile.cpp') diff --git a/llvm/lib/Object/ObjectFile.cpp b/llvm/lib/Object/ObjectFile.cpp index 61b36ea..cf09a66 100644 --- a/llvm/lib/Object/ObjectFile.cpp +++ b/llvm/lib/Object/ObjectFile.cpp @@ -132,7 +132,8 @@ Triple ObjectFile::makeTriple() const { } Expected> -ObjectFile::createObjectFile(MemoryBufferRef Object, file_magic Type) { +ObjectFile::createObjectFile(MemoryBufferRef Object, file_magic Type, + bool InitContent) { StringRef Data = Object.getBuffer(); if (Type == file_magic::unknown) Type = identify_magic(Data); @@ -154,7 +155,7 @@ ObjectFile::createObjectFile(MemoryBufferRef Object, file_magic Type) { case file_magic::elf_executable: case file_magic::elf_shared_object: case file_magic::elf_core: - return createELFObjectFile(Object); + return createELFObjectFile(Object, InitContent); case file_magic::macho_object: case file_magic::macho_executable: case file_magic::macho_fixed_virtual_memory_shared_lib: -- cgit v1.1