LibELF+Lagom: Work towards getting LibELF in Lagom

Mostly -Wformat fixes, some of which pointed out real (if benign) bugs.
This commit is contained in:
Nico Weber 2020-08-08 22:45:20 -04:00 committed by Andreas Kling
parent 872834320a
commit 0586924bbd
6 changed files with 33 additions and 32 deletions

View file

@ -38,15 +38,18 @@ int dbgprintf(const char* fmt, ...);
ssize_t dbgputstr(const char*, ssize_t);
int sprintf(char* buf, const char* fmt, ...);
}
template<size_t N>
inline int dbgputstr(const char (&array)[N])
{
return ::dbgputstr(array, N);
}
# endif
#else
# include <stdio.h>
# 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<size_t N>
inline int dbgputstr(const char (&array)[N])
{
return ::dbgputstr(array, N);
}

View file

@ -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<size_t N>
inline int dbgputstr(const char (&array)[N])
{
return ::dbgputstr(array, N);
}
#endif

View file

@ -30,10 +30,10 @@
#include <assert.h>
#include <dlfcn.h>
#include <mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#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;

View file

@ -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()

View file

@ -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());
}

View file

@ -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;
}
}