From 7ea82243ec3e556518116c51b528c3a4eb6d16f3 Mon Sep 17 00:00:00 2001 From: Mark Wielaard Date: Mon, 12 Aug 2019 00:43:22 +0200 Subject: libdwelf: Make dwelf_elf_begin return NULL only when there is an error. dwelf_elf_begin was slightly different from elf_begin in case the file turned out to not be an ELF file. elf_begin would return an Elf handle with ELF_K_NONE. But dwelf_elf_begin would return NULL. This made it impossible to tell the difference between a file or decompression error and a (decompressed) file not being an ELF file. Since dwelf_elf_begin could still return different kinds of ELF files (ELF_K_ELF or ELF_K_AR - and theoretically ELF_K_COFF) this was not really useful anyway. So make it so that dwelf_elf_begin always returns an Elf handle unless there was a real error reading or decompressing the file. Otherwise return NULL to make clear there was a real error. Make sure that the decompression function returns DWFL_E_BADELF only when the file isn't compressed. In which case the Elf handle won't be replaced and can be returned (as ELF_K_NONE). Add a new version to dwelf_elf_begin so programs can rely on it returning NULL only for real errors. Signed-off-by: Mark Wielaard --- libdwelf/ChangeLog | 6 ++++++ libdwelf/dwelf_elf_begin.c | 12 +++++++----- libdwelf/libdwelf.h | 9 ++++++--- 3 files changed, 19 insertions(+), 8 deletions(-) (limited to 'libdwelf') diff --git a/libdwelf/ChangeLog b/libdwelf/ChangeLog index 29f9a509..5b48ed8f 100644 --- a/libdwelf/ChangeLog +++ b/libdwelf/ChangeLog @@ -1,3 +1,9 @@ +2019-08-12 Mark Wielaard + + * libdwelf.h (dwelf_elf_begin): Update documentation. + * dwelf_elf_begin.c (dwelf_elf_begin): Don't suppress ELF_K_NONE. + Mark old and new version. + 2019-06-28 Mark Wielaard * Makefile.am (libdwelf_a_SOURCES): Add dwelf_elf_e_machine_string.c. diff --git a/libdwelf/dwelf_elf_begin.c b/libdwelf/dwelf_elf_begin.c index 79825338..c7d63a1c 100644 --- a/libdwelf/dwelf_elf_begin.c +++ b/libdwelf/dwelf_elf_begin.c @@ -41,13 +41,13 @@ dwelf_elf_begin (int fd) { Elf *elf = NULL; Dwfl_Error e = __libdw_open_elf (fd, &elf); - if (elf != NULL && elf_kind (elf) != ELF_K_NONE) + if (e == DWFL_E_NOERROR) return elf; - /* Elf wasn't usable. Make sure there is a proper elf error message. */ - - if (elf != NULL) - elf_end (elf); + /* Elf wasn't usable. Make sure there is a proper elf error + message. This is probably not the real error, because there is + no good way to propagate errnos or decompression errors, but + better than nothing. */ if (e != DWFL_E_LIBELF) { @@ -60,3 +60,5 @@ dwelf_elf_begin (int fd) return NULL; } +OLD_VERSION (dwelf_elf_begin, ELFUTILS_0.175) +NEW_VERSION (dwelf_elf_begin, ELFUTILS_0.177) diff --git a/libdwelf/libdwelf.h b/libdwelf/libdwelf.h index cb7ea091..dbb8f08c 100644 --- a/libdwelf/libdwelf.h +++ b/libdwelf/libdwelf.h @@ -128,9 +128,12 @@ extern void dwelf_strtab_free (Dwelf_Strtab *st) /* Creates a read-only Elf handle from the given file handle. The file may be compressed and/or contain a linux kernel image header, in which case it is eagerly decompressed in full and the Elf handle - is created as if created with elf_memory (). On error NULL is - returned. The Elf handle should be closed with elf_end (). The - file handle will not be closed. Does not return ELF_K_NONE handles. */ + is created as if created with elf_memory (). On decompression or + file errors NULL is returned (and elf_errno will be set). If there + was no error, but the file is not an ELF file, then an ELF_K_NONE + Elf handle is returned (just like with elf_begin). The Elf handle + should be closed with elf_end (). The file handle will not be + closed. */ extern Elf *dwelf_elf_begin (int fd); /* Returns a human readable string for the given ELF header e_machine -- cgit v1.2.1