diff options
author | Andreas Kling <kling@serenityos.org> | 2021-01-10 09:33:56 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-01-10 09:40:47 +0100 |
commit | e855aac1f584e83d4e6532212bdf56819d605fb3 (patch) | |
tree | 74e0a5a541b21019b7c602ffa1ea64e912345216 | |
parent | d3f51ee5c32436683b1d6f35d48756e80bb57210 (diff) | |
download | serenity-e855aac1f584e83d4e6532212bdf56819d605fb3.zip |
LibELF: Convert many dbgprintf() to dbgln() and tweak debug macro name
-rw-r--r-- | Libraries/LibELF/Image.cpp | 80 | ||||
-rw-r--r-- | Libraries/LibELF/Validation.cpp | 58 | ||||
-rw-r--r-- | Meta/CMake/all_the_debug_macros.cmake | 2 |
3 files changed, 72 insertions, 68 deletions
diff --git a/Libraries/LibELF/Image.cpp b/Libraries/LibELF/Image.cpp index 54fabf8411..42c5583750 100644 --- a/Libraries/LibELF/Image.cpp +++ b/Libraries/LibELF/Image.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> + * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org> * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -32,6 +32,8 @@ #include <LibELF/Image.h> #include <LibELF/Validation.h> +//#define ELF_IMAGE_DEBUG + namespace ELF { Image::Image(const u8* buffer, size_t size, bool verbose_logging) @@ -46,6 +48,7 @@ Image::~Image() { } +#ifdef ELF_IMAGE_DEBUG static const char* object_file_type_to_string(Elf32_Half type) { switch (type) { @@ -63,6 +66,7 @@ static const char* object_file_type_to_string(Elf32_Half type) return "(?)"; } } +#endif StringView Image::section_index_to_string(unsigned index) const { @@ -84,56 +88,56 @@ unsigned Image::symbol_count() const void Image::dump() const { - dbgprintf("Image{%p} {\n", this); - dbgprintf(" is_valid: %u\n", is_valid()); +#ifdef ELF_IMAGE_DEBUG + dbgln("ELF::Image({:p}) {{", this); + dbgln(" is_valid: {}", is_valid()); if (!is_valid()) { - dbgprintf("}\n"); + dbgln("}}"); return; } - dbgprintf(" type: %s\n", object_file_type_to_string(header().e_type)); - dbgprintf(" machine: %u\n", header().e_machine); - dbgprintf(" entry: %x\n", header().e_entry); - dbgprintf(" shoff: %u\n", header().e_shoff); - dbgprintf(" shnum: %u\n", header().e_shnum); - dbgprintf(" phoff: %u\n", header().e_phoff); - dbgprintf(" phnum: %u\n", header().e_phnum); - dbgprintf(" shstrndx: %u\n", header().e_shstrndx); + dbgln(" type: {}", object_file_type_to_string(header().e_type)); + dbgln(" machine: {}", header().e_machine); + dbgln(" entry: {:x}", header().e_entry); + dbgln(" shoff: {}", header().e_shoff); + dbgln(" shnum: {}", header().e_shnum); + dbgln(" phoff: {}", header().e_phoff); + dbgln(" phnum: {}", header().e_phnum); + dbgln(" shstrndx: {}", header().e_shstrndx); for_each_program_header([&](const ProgramHeader& program_header) { - dbgprintf(" Program Header %d: {\n", program_header.index()); - dbgprintf(" type: %x\n", program_header.type()); - dbgprintf(" offset: %x\n", program_header.offset()); - dbgprintf(" flags: %x\n", program_header.flags()); - dbgprintf(" \n"); - dbgprintf(" }\n"); + dbgln(" Program Header {}: {{", program_header.index()); + dbgln(" type: {:x}", program_header.type()); + dbgln(" offset: {:x}", program_header.offset()); + dbgln(" flags: {:x}", program_header.flags()); + dbgln(" }}"); return IterationDecision::Continue; }); for (unsigned i = 0; i < header().e_shnum; ++i) { auto& section = this->section(i); - dbgprintf(" Section %u: {\n", i); - 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()); - dbgprintf(" \n"); - dbgprintf(" }\n"); + dbgln(" Section {}: {{", i); + dbgln(" name: {}", section.name()); + dbgln(" type: {:x}", section.type()); + dbgln(" offset: {:x}", section.offset()); + dbgln(" size: {}", section.size()); + dbgln(" "); + dbgln(" }}"); } - dbgprintf("Symbol count: %u (table is %u)\n", symbol_count(), m_symbol_table_section_index); + dbgln("Symbol count: {} (table is {})", symbol_count(), m_symbol_table_section_index); for (unsigned i = 1; i < symbol_count(); ++i) { auto& sym = symbol(i); - dbgprintf("Symbol @%u:\n", i); - 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()); + dbgln("Symbol @{}:", i); + dbgln(" Name: {}", sym.name()); + dbgln(" In section: {}", section_index_to_string(sym.section_index())); + dbgln(" Value: {}", sym.value()); + dbgln(" Size: {}", sym.size()); } - dbgprintf("}\n"); + dbgln("}}"); +#endif } unsigned Image::section_count() const @@ -152,13 +156,13 @@ bool Image::parse() { if (m_size < sizeof(Elf32_Ehdr) || !validate_elf_header(header(), m_size, m_verbose_logging)) { if (m_verbose_logging) - dbgputstr("Image::parse(): ELF Header not valid\n"); + dbgln("ELF::Image::parse(): ELF Header not valid"); return m_valid = false; } if (!validate_program_headers(header(), m_size, m_buffer, m_size, nullptr, m_verbose_logging)) { if (m_verbose_logging) - dbgputstr("Image::parse(): ELF Program Headers not valid\n"); + dbgln("ELF::Image::parse(): ELF Program Headers not valid"); return m_valid = false; } @@ -196,7 +200,7 @@ StringView Image::table_string(unsigned table_index, unsigned offset) const size_t computed_offset = sh.sh_offset + offset; if (computed_offset >= m_size) { if (m_verbose_logging) - dbgprintf("SHENANIGANS! Image::table_string() computed offset outside image.\n"); + dbgln("SHENANIGANS! Image::table_string() computed offset outside image."); return {}; } size_t max_length = m_size - computed_offset; @@ -286,8 +290,8 @@ const Image::RelocationSection Image::Section::relocations() const if (relocation_section.type() != SHT_REL) return static_cast<const RelocationSection>(m_image.section(0)); -#ifdef Image_DEBUG - dbgprintf("Found relocations for %s in %s\n", name().to_string().characters(), relocation_section.name().to_string().characters()); +#ifdef ELF_IMAGE_DEBUG + dbgln("Found relocations for {} in {}", name(), relocation_section.name()); #endif return static_cast<const RelocationSection>(relocation_section); } diff --git a/Libraries/LibELF/Validation.cpp b/Libraries/LibELF/Validation.cpp index 69f04ee2c5..1700a48b1e 100644 --- a/Libraries/LibELF/Validation.cpp +++ b/Libraries/LibELF/Validation.cpp @@ -35,67 +35,67 @@ bool validate_elf_header(const Elf32_Ehdr& elf_header, size_t file_size, bool ve { if (!IS_ELF(elf_header)) { if (verbose) - dbgputstr("File is not an ELF file.\n"); + dbgln("File is not an ELF file."); return false; } if (ELFCLASS32 != elf_header.e_ident[EI_CLASS]) { if (verbose) - dbgputstr("File is not a 32 bit ELF file.\n"); + dbgln("File is not a 32 bit ELF file."); return false; } if (ELFDATA2LSB != elf_header.e_ident[EI_DATA]) { if (verbose) - dbgputstr("File is not a little endian ELF file.\n"); + dbgln("File is not a little endian ELF file."); return false; } if (EV_CURRENT != elf_header.e_ident[EI_VERSION]) { if (verbose) - dbgprintf("File has unrecognized ELF version (%d), expected (%d)!\n", elf_header.e_ident[EI_VERSION], EV_CURRENT); + dbgln("File has unrecognized ELF version ({}), expected ({})!", elf_header.e_ident[EI_VERSION], EV_CURRENT); return false; } if (ELFOSABI_SYSV != elf_header.e_ident[EI_OSABI]) { if (verbose) - dbgprintf("File has unknown OS ABI (%d), expected SYSV(0)!\n", elf_header.e_ident[EI_OSABI]); + dbgln("File has unknown OS ABI ({}), expected SYSV(0)!", elf_header.e_ident[EI_OSABI]); return false; } if (0 != elf_header.e_ident[EI_ABIVERSION]) { if (verbose) - dbgprintf("File has unknown SYSV ABI version (%d)!\n", elf_header.e_ident[EI_ABIVERSION]); + dbgln("File has unknown SYSV ABI version ({})!", elf_header.e_ident[EI_ABIVERSION]); return false; } if (EM_386 != elf_header.e_machine) { if (verbose) - dbgprintf("File has unknown machine (%d), expected i386 (3)!\n", elf_header.e_machine); + dbgln("File has unknown machine ({}), expected i386 (3)!", elf_header.e_machine); return false; } if (ET_EXEC != elf_header.e_type && ET_DYN != elf_header.e_type && ET_REL != elf_header.e_type && ET_CORE != elf_header.e_type) { if (verbose) - dbgprintf("File has unloadable ELF type (%d), expected REL (1), EXEC (2), DYN (3) or CORE(4)!\n", elf_header.e_type); + dbgln("File has unloadable ELF type ({}), expected REL (1), EXEC (2), DYN (3) or CORE(4)!", elf_header.e_type); return false; } if (EV_CURRENT != elf_header.e_version) { if (verbose) - dbgprintf("File has unrecognized ELF version (%d), expected (%d)!\n", elf_header.e_version, EV_CURRENT); + dbgln("File has unrecognized ELF version ({}), expected ({})!", elf_header.e_version, EV_CURRENT); return false; } if (sizeof(Elf32_Ehdr) != elf_header.e_ehsize) { if (verbose) - dbgprintf("File has incorrect ELF header size..? (%d), expected (%zu)!\n", elf_header.e_ehsize, sizeof(Elf32_Ehdr)); + dbgln("File has incorrect ELF header size..? ({}), expected ({})!", elf_header.e_ehsize, sizeof(Elf32_Ehdr)); return false; } if (elf_header.e_phoff < elf_header.e_ehsize || (elf_header.e_shnum != SHN_UNDEF && elf_header.e_shoff < elf_header.e_ehsize)) { if (verbose) { - dbgprintf("SHENANIGANS! program header offset (%d) or section header offset (%d) overlap with ELF header!\n", + dbgln("SHENANIGANS! program header offset ({}) or section header offset ({}) overlap with ELF header!", elf_header.e_phoff, elf_header.e_shoff); } return false; @@ -103,7 +103,7 @@ bool validate_elf_header(const Elf32_Ehdr& elf_header, size_t file_size, bool ve if (elf_header.e_phoff > file_size || elf_header.e_shoff > file_size) { if (verbose) { - dbgprintf("SHENANIGANS! program header offset (%d) or section header offset (%d) are past the end of the file!\n", + dbgln("SHENANIGANS! program header offset ({}) or section header offset ({}) are past the end of the file!", elf_header.e_phoff, elf_header.e_shoff); } return false; @@ -111,13 +111,13 @@ bool validate_elf_header(const Elf32_Ehdr& elf_header, size_t file_size, bool ve if (elf_header.e_phnum == 0 && elf_header.e_phoff != 0) { if (verbose) - dbgputstr("SHENANIGANS! File has no program headers, but it does have a program header offset (%d)!\n", elf_header.e_phoff); + dbgln("SHENANIGANS! File has no program headers, but it does have a program header offset ({})!", elf_header.e_phoff); return false; } if (elf_header.e_phnum != 0 && elf_header.e_phoff != elf_header.e_ehsize) { if (verbose) { - dbgprintf("File does not have program headers directly after the ELF header? program header offset (%d), expected (%d).\n", + dbgln("File does not have program headers directly after the ELF header? program header offset ({}), expected ({}).", elf_header.e_phoff, elf_header.e_ehsize); } return false; @@ -125,32 +125,32 @@ bool validate_elf_header(const Elf32_Ehdr& elf_header, size_t file_size, bool ve if (0 != elf_header.e_flags) { if (verbose) - dbgprintf("File has incorrect ELF header flags...? (%d), expected (%d).\n", elf_header.e_flags, 0); + dbgln("File has incorrect ELF header flags...? ({}), expected ({}).", elf_header.e_flags, 0); return false; } if (0 != elf_header.e_phnum && sizeof(Elf32_Phdr) != elf_header.e_phentsize) { if (verbose) - dbgprintf("File has incorrect program header size..? (%d), expected (%zu).\n", elf_header.e_phentsize, sizeof(Elf32_Phdr)); + dbgln("File has incorrect program header size..? ({}), expected ({}).", elf_header.e_phentsize, sizeof(Elf32_Phdr)); return false; } if (sizeof(Elf32_Shdr) != elf_header.e_shentsize) { if (verbose) - dbgprintf("File has incorrect section header size..? (%d), expected (%zu).\n", elf_header.e_shentsize, sizeof(Elf32_Shdr)); + dbgln("File has incorrect section header size..? ({}), expected ({}).", 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) { if (verbose) - dbgprintf("SHENANIGANS! End of last program header (%zu) is past the end of the file!\n", end_of_last_program_header); + dbgln("SHENANIGANS! End of last program header ({}) is past the end of the file!", end_of_last_program_header); return false; } if (elf_header.e_shoff != SHN_UNDEF && elf_header.e_shoff < end_of_last_program_header) { if (verbose) { - dbgprintf("SHENANIGANS! Section header table begins at file offset %d, which is within program headers [ %d - %zu ]!\n", + dbgln("SHENANIGANS! Section header table begins at file offset {}, which is within program headers [ {} - {} ]!", elf_header.e_shoff, elf_header.e_phoff, end_of_last_program_header); } return false; @@ -159,13 +159,13 @@ bool validate_elf_header(const Elf32_Ehdr& elf_header, size_t file_size, bool ve 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) { if (verbose) - dbgprintf("SHENANIGANS! End of last section header (%zu) is past the end of the file!\n", end_of_last_section_header); + dbgln("SHENANIGANS! End of last section header ({}) is past the end of the file!", end_of_last_section_header); return false; } if (elf_header.e_shstrndx != SHN_UNDEF && elf_header.e_shstrndx >= elf_header.e_shnum) { if (verbose) - dbgprintf("SHENANIGANS! Section header string table index (%d) is not a valid index given we have %d section headers!\n", elf_header.e_shstrndx, elf_header.e_shnum); + dbgln("SHENANIGANS! Section header string table index ({}) is not a valid index given we have {} section headers!", elf_header.e_shstrndx, elf_header.e_shnum); return false; } @@ -178,13 +178,13 @@ bool validate_program_headers(const Elf32_Ehdr& elf_header, size_t file_size, co 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 > buffer_size) { if (verbose) - dbgprintf("Unable to parse program headers from buffer, buffer too small! Buffer size: %zu, End of program headers %zu\n", + dbgln("Unable to parse program headers from buffer, buffer too small! Buffer size: {}, End of program headers {}", buffer_size, end_of_last_program_header); return false; } if (file_size < buffer_size) { - dbgputstr("We somehow read more from a file than was in the file in the first place!\n"); + dbgln("We somehow read more from a file than was in the file in the first place!"); ASSERT_NOT_REACHED(); } @@ -219,7 +219,7 @@ bool validate_program_headers(const Elf32_Ehdr& elf_header, size_t file_size, co // 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) { if (verbose) - 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); + dbgln("Found PT_INTERP header ({}), but the .interp section was not within the buffer :(", header_index); return false; } if (interpreter_path) @@ -232,32 +232,32 @@ bool validate_program_headers(const Elf32_Ehdr& elf_header, size_t file_size, co case PT_TLS: if (program_header.p_offset + program_header.p_filesz > file_size) { if (verbose) - dbgprintf("SHENANIGANS! Program header %zu segment leaks beyond end of file!\n", header_index); + dbgln("SHENANIGANS! Program header {} segment leaks beyond end of file!", header_index); return false; } if ((program_header.p_flags & PF_X) && (program_header.p_flags & PF_W)) { if (verbose) - dbgprintf("SHENANIGANS! Program header %zu segment is marked write and execute\n", header_index); + dbgln("SHENANIGANS! Program header {} segment is marked write and execute", header_index); return false; } break; case PT_GNU_STACK: if (program_header.p_flags & PF_X) { if (verbose) - dbgprintf("Possible shenanigans! Validating an ELF with executable stack.\n"); + dbgln("Possible shenanigans! Validating an ELF with executable stack."); } break; case PT_GNU_RELRO: if ((program_header.p_flags & PF_X) && (program_header.p_flags & PF_W)) { if (verbose) - dbgprintf("SHENANIGANS! Program header %zu segment is marked write and execute\n", header_index); + dbgln("SHENANIGANS! Program header {} segment is marked write and execute", header_index); return false; } break; default: // Not handling other program header types in other code so... let's not surprise them if (verbose) - dbgprintf("Found program header (%zu) of unrecognized type %x!\n", header_index, program_header.p_type); + dbgln("Found program header ({}) of unrecognized type {}!", header_index, program_header.p_type); return false; } } diff --git a/Meta/CMake/all_the_debug_macros.cmake b/Meta/CMake/all_the_debug_macros.cmake index 37ffac6f26..b205f59959 100644 --- a/Meta/CMake/all_the_debug_macros.cmake +++ b/Meta/CMake/all_the_debug_macros.cmake @@ -47,6 +47,7 @@ add_compile_definitions("DYNAMIC_LOAD_DEBUG") add_compile_definitions("E1000_DEBUG") add_compile_definitions("EBR_DEBUG") add_compile_definitions("EDITOR_DEBUG") +add_compile_definitions("ELF_IMAGE_DEBUG") add_compile_definitions("EMOJI_DEBUG") add_compile_definitions("ETHERNET_DEBUG") add_compile_definitions("ETHERNET_VERY_DEBUG") @@ -85,7 +86,6 @@ add_compile_definitions("IPV4_DEBUG") add_compile_definitions("IPV4_SOCKET_DEBUG") add_compile_definitions("IRC_DEBUG") add_compile_definitions("IRQ_DEBUG") -add_compile_definitions("Image_DEBUG") add_compile_definitions("JOB_DEBUG") add_compile_definitions("JPG_DEBUG") add_compile_definitions("KEYBOARD_DEBUG") |