File indexing completed on 2024-05-12 05:43:30

0001 /*
0002     Copyright (C) 2015 Volker Krause <vkrause@kde.org>
0003 
0004     This program is free software; you can redistribute it and/or modify it
0005     under the terms of the GNU Library General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or (at your
0007     option) any later version.
0008 
0009     This program is distributed in the hope that it will be useful, but WITHOUT
0010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0012     License for more details.
0013 
0014     You should have received a copy of the GNU General Public License
0015     along with this program.  If not, see <https://www.gnu.org/licenses/>.
0016 */
0017 
0018 #include "elfprinter.h"
0019 #include "printerutils_p.h"
0020 
0021 #include <elf.h>
0022 
0023 #include <QByteArray>
0024 
0025 static const LookupTableEntry<uint16_t> file_type_table[] {
0026     { ET_NONE, "none" },
0027     { ET_REL, "relocatable file" },
0028     { ET_EXEC, "executable file" },
0029     { ET_DYN, "shared object file" },
0030     { ET_CORE, "core file" }
0031 };
0032 
0033 QByteArray ElfPrinter::fileType(uint16_t fileType)
0034 {
0035     return lookupLabel(fileType, file_type_table);
0036 }
0037 
0038 QByteArray ElfPrinter::machine(uint16_t machineType)
0039 {
0040     #define M(x) case EM_ ## x: return QByteArray::fromRawData(#x, strlen(#x));
0041     switch (machineType) {
0042         M(NONE)
0043         M(386)
0044         M(ARM)
0045         M(X86_64)
0046         M(AVR)
0047 #ifdef EM_AARCH64
0048         M(AARCH64)
0049 #endif
0050     }
0051     return QByteArray("Unknown machine type (" ) + QByteArray::number(machineType) + ')';
0052 }
0053 
0054 static const LookupTableEntry<uint32_t> section_type_table[] {
0055     { SHT_NULL, "null" },
0056     { SHT_PROGBITS, "program data" },
0057     { SHT_SYMTAB, "symbol table" },
0058     { SHT_STRTAB, "string table" },
0059     { SHT_RELA, "relocation entries with addends" },
0060     { SHT_HASH, "symbol hash table" },
0061     { SHT_DYNAMIC, "dynamic linking information" },
0062     { SHT_NOTE, "notes" },
0063     { SHT_NOBITS, "bss" },
0064     { SHT_REL, "relocation entries, no addends" },
0065     { SHT_SHLIB, "reserved" },
0066     { SHT_DYNSYM, "dynamic linker symbol table" },
0067     { SHT_INIT_ARRAY, "array of constructors" },
0068     { SHT_FINI_ARRAY, "array of destructors" },
0069     { SHT_PREINIT_ARRAY, "array of preconstructors" },
0070     { SHT_GROUP, "section group" },
0071     { SHT_SYMTAB_SHNDX, "extended section indices" },
0072 
0073     { SHT_GNU_ATTRIBUTES, "GNU object attributes" },
0074     { SHT_GNU_HASH, "GNU-style hash table" },
0075     { SHT_GNU_LIBLIST, "GNU prelink library list" },
0076 #ifdef SHT_CHECKSUM
0077     { SHT_CHECKSUM, "checksum for DSO content" },
0078 #endif
0079     { SHT_GNU_verdef, "GNU version definition" },
0080     { SHT_GNU_verneed, "GNU version needs" },
0081     { SHT_GNU_versym, "GNU version symbol table" }
0082 };
0083 
0084 QByteArray ElfPrinter::sectionType(uint32_t sectionType)
0085 {
0086     return lookupLabel(sectionType, section_type_table);
0087 }
0088 
0089 static const LookupTableEntry<uint64_t> section_flags_table[] {
0090     { SHF_WRITE, "writable" },
0091     { SHF_ALLOC, "occupies memory during execution" },
0092     { SHF_EXECINSTR, "executable" },
0093     { SHF_MERGE, "might be merged" },
0094     { SHF_STRINGS, "contains nul-terminated strings" },
0095     { SHF_INFO_LINK, "sh_info contains SHT index" },
0096     { SHF_LINK_ORDER, "preserve order after combining" },
0097     { SHF_OS_NONCONFORMING, "non-standard OS specific handling required" },
0098     { SHF_GROUP, "group member" },
0099     { SHF_TLS, "holds thread-local data" }
0100 };
0101 
0102 QByteArray ElfPrinter::sectionFlags(uint64_t flags)
0103 {
0104     return lookupFlags(flags, section_flags_table);
0105 }
0106 
0107 static const LookupTableEntry<uint8_t> os_abi_table[] {
0108     { ELFOSABI_SYSV, "UNIX System V ABI" },
0109     { ELFOSABI_HPUX, "HP-UX" },
0110     { ELFOSABI_NETBSD, "NetBSD" },
0111     { ELFOSABI_GNU, "Object uses GNU ELF extensions" },
0112     { ELFOSABI_SOLARIS, "Sun Solaris" },
0113     { ELFOSABI_AIX, "IBM AIX" },
0114     { ELFOSABI_IRIX, "SGI Irix" },
0115     { ELFOSABI_FREEBSD, "FreeBSD" },
0116     { ELFOSABI_TRU64, "Compaq TRU64 UNIX" },
0117     { ELFOSABI_MODESTO, "Novell Modesto" },
0118     { ELFOSABI_OPENBSD, "OpenBSD" },
0119     { ELFOSABI_ARM_AEABI, "ARM EABI" },
0120     { ELFOSABI_ARM, "ARM" },
0121     { ELFOSABI_STANDALONE, "Standalone (embedded) application" }
0122 };
0123 
0124 QByteArray ElfPrinter::osAbi(uint8_t abi)
0125 {
0126     return lookupLabel(abi, os_abi_table);
0127 }
0128 
0129 static const LookupTableEntry<uint32_t> segment_type_table[] {
0130     { PT_NULL, "Program header table entry unused" },
0131     { PT_LOAD, "Loadable program segment" },
0132     { PT_DYNAMIC, "Dynamic linking information" },
0133     { PT_INTERP, "Program interpreter" },
0134     { PT_NOTE, "Auxiliary information" },
0135     { PT_SHLIB, "Reserved" },
0136     { PT_PHDR, "Entry for header table itself" },
0137     { PT_TLS, "Thread-local storage segment" },
0138 #ifdef PT_NUM
0139     { PT_NUM, "Number of defined types" },
0140 #endif
0141     { PT_GNU_EH_FRAME, "GCC .eh_frame_hdr segment" },
0142     { PT_GNU_STACK, "Indicates stack executability" },
0143     { PT_GNU_RELRO, "Read-only after relocation" },
0144     { PT_SUNWBSS, "Sun Specific segment" },
0145     { PT_SUNWSTACK, "Stack segment" }
0146 };
0147 
0148 QByteArray ElfPrinter::segmentType(uint32_t segmentType)
0149 {
0150     return lookupLabel(segmentType, segment_type_table);
0151 }
0152 
0153 static const LookupTableEntry<uint32_t> segment_flags_table[] {
0154     { PF_X, "executable" },
0155     { PF_W, "writable" },
0156     { PF_R, "readable" }
0157 };
0158 
0159 QByteArray ElfPrinter::segmentFlags(uint32_t flags)
0160 {
0161     return lookupFlags(flags, segment_flags_table);
0162 }