From 0586924bbdee390cce13928a73168f41e9158c93 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Sat, 8 Aug 2020 22:45:20 -0400 Subject: [PATCH] LibELF+Lagom: Work towards getting LibELF in Lagom Mostly -Wformat fixes, some of which pointed out real (if benign) bugs. --- AK/kstdio.h | 15 +++++++++------ Kernel/kstdio.h | 10 ---------- Libraries/LibELF/DynamicLoader.cpp | 9 ++++++++- Libraries/LibELF/DynamicObject.cpp | 4 ++-- Libraries/LibELF/Image.cpp | 7 ++++--- Libraries/LibELF/Validation.cpp | 20 ++++++++++---------- 6 files changed, 33 insertions(+), 32 deletions(-) diff --git a/AK/kstdio.h b/AK/kstdio.h index a26f54f5874..78ca52bc9a6 100644 --- a/AK/kstdio.h +++ b/AK/kstdio.h @@ -38,15 +38,18 @@ int dbgprintf(const char* fmt, ...); ssize_t dbgputstr(const char*, ssize_t); int sprintf(char* buf, const char* fmt, ...); } -template -inline int dbgputstr(const char (&array)[N]) -{ - return ::dbgputstr(array, N); -} # endif #else # include # define kprintf printf # define dbgprintf(...) fprintf(stderr, __VA_ARGS__) -# define dbgputstr(characters, length) fwrite(characters, 1, length, stderr) +inline size_t dbgputstr(const char* characters, ssize_t length) +{ + return fwrite(characters, 1, length, stderr); +} #endif +template +inline int dbgputstr(const char (&array)[N]) +{ + return ::dbgputstr(array, N); +} diff --git a/Kernel/kstdio.h b/Kernel/kstdio.h index 9910607411e..b6f3d17cf96 100644 --- a/Kernel/kstdio.h +++ b/Kernel/kstdio.h @@ -37,13 +37,3 @@ int sprintf(char* buf, const char* fmt, ...); void set_serial_debug(bool on_or_off); int get_serial_debug(); } - -#ifdef __cplusplus - -template -inline int dbgputstr(const char (&array)[N]) -{ - return ::dbgputstr(array, N); -} - -#endif diff --git a/Libraries/LibELF/DynamicLoader.cpp b/Libraries/LibELF/DynamicLoader.cpp index 5136aa4fe74..5dcd80e4381 100644 --- a/Libraries/LibELF/DynamicLoader.cpp +++ b/Libraries/LibELF/DynamicLoader.cpp @@ -30,10 +30,10 @@ #include #include -#include #include #include #include +#include #define DYNAMIC_LOAD_DEBUG //#define DYNAMIC_LOAD_VERBOSE @@ -46,6 +46,13 @@ } while (0) #endif +#ifndef __serenity__ +static void* mmap_with_name(void* addr, size_t length, int prot, int flags, int fd, off_t offset, const char*) +{ + return mmap(addr, length, prot, flags, fd, offset); +} +#endif + namespace ELF { static bool s_always_bind_now = false; diff --git a/Libraries/LibELF/DynamicObject.cpp b/Libraries/LibELF/DynamicObject.cpp index e345938b96c..d12d940ac6e 100644 --- a/Libraries/LibELF/DynamicObject.cpp +++ b/Libraries/LibELF/DynamicObject.cpp @@ -62,8 +62,8 @@ void DynamicObject::dump() const if (m_has_soname) builder.appendf("DT_SONAME: %s\n", soname()); // FIXME: Valdidate that this string is null terminated? - dbgprintf("Dynamic section at address 0x%x contains %zu entries:\n", m_dynamic_address.as_ptr(), num_dynamic_sections); - dbgprintf(builder.to_string().characters()); + dbgprintf("Dynamic section at address %p contains %zu entries:\n", m_dynamic_address.as_ptr(), num_dynamic_sections); + dbgprintf("%s", builder.to_string().characters()); } void DynamicObject::parse() diff --git a/Libraries/LibELF/Image.cpp b/Libraries/LibELF/Image.cpp index eae3c11e16f..9e401a19428 100644 --- a/Libraries/LibELF/Image.cpp +++ b/Libraries/LibELF/Image.cpp @@ -106,7 +106,7 @@ void Image::dump() const for (unsigned i = 0; i < header().e_shnum; ++i) { auto& section = this->section(i); dbgprintf(" Section %u: {\n", i); - dbgprintf(" name: %s\n", section.name()); + dbgprintf(" name: %.*s\n", (int)section.name().length(), section.name().characters_without_null_termination()); dbgprintf(" type: %x\n", section.type()); dbgprintf(" offset: %x\n", section.offset()); dbgprintf(" size: %u\n", section.size()); @@ -118,8 +118,9 @@ void Image::dump() const for (unsigned i = 1; i < symbol_count(); ++i) { auto& sym = symbol(i); dbgprintf("Symbol @%u:\n", i); - dbgprintf(" Name: %s\n", sym.name()); - dbgprintf(" In section: %s\n", section_index_to_string(sym.section_index())); + dbgprintf(" Name: %.*s\n", (int)sym.name().length(), sym.name().characters_without_null_termination()); + StringView section_index_string = section_index_to_string(sym.section_index()); + dbgprintf(" In section: %.*s\n", (int)section_index_string.length(), section_index_string.characters_without_null_termination()); dbgprintf(" Value: %x\n", sym.value()); dbgprintf(" Size: %u\n", sym.size()); } diff --git a/Libraries/LibELF/Validation.cpp b/Libraries/LibELF/Validation.cpp index e4e5c5ba9eb..f1b9533187f 100644 --- a/Libraries/LibELF/Validation.cpp +++ b/Libraries/LibELF/Validation.cpp @@ -79,7 +79,7 @@ bool validate_elf_header(const Elf32_Ehdr& elf_header, size_t file_size) } if (sizeof(Elf32_Ehdr) != elf_header.e_ehsize) { - dbgprintf("File has incorrect ELF header size..? (%d), expected (%d)!\n", elf_header.e_ehsize, sizeof(Elf32_Ehdr)); + dbgprintf("File has incorrect ELF header size..? (%d), expected (%zu)!\n", elf_header.e_ehsize, sizeof(Elf32_Ehdr)); return false; } @@ -101,24 +101,24 @@ bool validate_elf_header(const Elf32_Ehdr& elf_header, size_t file_size) } if (0 != elf_header.e_phnum && sizeof(Elf32_Phdr) != elf_header.e_phentsize) { - dbgprintf("File has incorrect program header size..? (%d), expected (%d).\n", elf_header.e_phentsize, sizeof(Elf32_Phdr)); + dbgprintf("File has incorrect program header size..? (%d), expected (%zu).\n", elf_header.e_phentsize, sizeof(Elf32_Phdr)); return false; } if (sizeof(Elf32_Shdr) != elf_header.e_shentsize) { - dbgprintf("File has incorrect section header size..? (%d), expected (%d).\n", elf_header.e_shentsize, sizeof(Elf32_Shdr)); + dbgprintf("File has incorrect section header size..? (%d), expected (%zu).\n", elf_header.e_shentsize, sizeof(Elf32_Shdr)); return false; } size_t end_of_last_program_header = elf_header.e_phoff + (elf_header.e_phnum * elf_header.e_phentsize); if (end_of_last_program_header > file_size) { - dbgprintf("SHENANIGANS! End of last program header (%d) is past the end of the file!\n", end_of_last_program_header); + dbgprintf("SHENANIGANS! End of last program header (%zu) is past the end of the file!\n", end_of_last_program_header); return false; } size_t end_of_last_section_header = elf_header.e_shoff + (elf_header.e_shnum * elf_header.e_shentsize); if (end_of_last_section_header > file_size) { - dbgprintf("SHENANIGANS! End of last section header (%d) is past the end of the file!\n", end_of_last_section_header); + dbgprintf("SHENANIGANS! End of last section header (%zu) is past the end of the file!\n", end_of_last_section_header); return false; } @@ -153,12 +153,12 @@ bool validate_program_headers(const Elf32_Ehdr& elf_header, size_t file_size, u8 switch (program_header.p_type) { case PT_INTERP: if (ET_DYN != elf_header.e_type) { - dbgprintf("Found PT_INTERP header (%d) in non-DYN ELF object! What? We can't handle this!\n", header_index); + dbgprintf("Found PT_INTERP header (%zu) in non-DYN ELF object! What? We can't handle this!\n", header_index); return false; } // We checked above that file_size was >= buffer size. We only care about buffer size anyway, we're trying to read this! if (program_header.p_offset + program_header.p_filesz > buffer_size) { - dbgprintf("Found PT_INTERP header (%d), but the .interp section was not within our buffer :( Your program will not be loaded today.\n", header_index); + dbgprintf("Found PT_INTERP header (%zu), but the .interp section was not within our buffer :( Your program will not be loaded today.\n", header_index); return false; } interpreter_path = String((const char*)&buffer[program_header.p_offset], program_header.p_filesz - 1); @@ -169,17 +169,17 @@ bool validate_program_headers(const Elf32_Ehdr& elf_header, size_t file_size, u8 case PT_PHDR: case PT_TLS: if (program_header.p_offset + program_header.p_filesz > file_size) { - dbgprintf("SHENANIGANS! Program header %d segment leaks beyond end of file!\n", header_index); + dbgprintf("SHENANIGANS! Program header %zu segment leaks beyond end of file!\n", header_index); return false; } if ((program_header.p_flags & PF_X) && (program_header.p_flags & PF_W)) { - dbgprintf("SHENANIGANS! Program header %d segment is marked write and execute\n", header_index); + dbgprintf("SHENANIGANS! Program header %zu segment is marked write and execute\n", header_index); return false; } break; default: // Not handling other program header types in other code so... let's not surprise them - dbgprintf("Found program header (%d) of unrecognized type %x!\n", header_index, program_header.p_type); + dbgprintf("Found program header (%zu) of unrecognized type %x!\n", header_index, program_header.p_type); return false; } }