diff --git a/elf/elf.h b/elf/elf.h index 225b4d5c..2e65e5ff 100644 --- a/elf/elf.h +++ b/elf/elf.h @@ -81,15 +81,19 @@ enum : u32 { SHT_GROUP = 17, SHT_SYMTAB_SHNDX = 18, SHT_RELR = 19, + SHT_LOOS = 0x60000000, SHT_LLVM_ADDRSIG = 0x6fff4c03, SHT_GNU_HASH = 0x6ffffff6, SHT_GNU_VERDEF = 0x6ffffffd, SHT_GNU_VERNEED = 0x6ffffffe, SHT_GNU_VERSYM = 0x6fffffff, + SHT_HIOS = 0x6fffffff, SHT_X86_64_UNWIND = 0x70000001, SHT_ARM_EXIDX = 0x70000001, SHT_ARM_ATTRIBUTES = 0x70000003, SHT_RISCV_ATTRIBUTES = 0x70000003, + SHT_LOUSER = 0x80000000, + SHT_HIUSER = 0xffffffff, }; enum : u32 { @@ -100,6 +104,7 @@ enum : u32 { SHF_STRINGS = 0x20, SHF_INFO_LINK = 0x40, SHF_LINK_ORDER = 0x80, + SHF_OS_NONCONFORMING = 0x100, SHF_GROUP = 0x200, SHF_TLS = 0x400, SHF_COMPRESSED = 0x800, diff --git a/elf/input-files.cc b/elf/input-files.cc index c5478092..b8c2f4c7 100644 --- a/elf/input-files.cc +++ b/elf/input-files.cc @@ -233,6 +233,32 @@ static void read_riscv_attributes(Context &ctx, ObjectFile &file, } } +template +static bool is_known_section_type(const ElfShdr &shdr) { + u32 ty = shdr.sh_type; + u32 flags = shdr.sh_flags; + + if (ty == SHT_PROGBITS || + ty == SHT_NOTE || + ty == SHT_NOBITS || + ty == SHT_INIT_ARRAY || + ty == SHT_FINI_ARRAY || + ty == SHT_PREINIT_ARRAY) + return true; + + if (SHT_LOUSER <= ty && ty <= SHT_HIUSER && !(flags & SHF_ALLOC)) + return true; + if (SHT_LOOS <= ty && ty <= SHT_HIOS && !(flags & SHF_OS_NONCONFORMING)) + return true; + if (is_x86 && ty == SHT_X86_64_UNWIND) + return true; + if (is_arm32 && (ty == SHT_ARM_EXIDX || ty == SHT_ARM_ATTRIBUTES)) + return true; + if (is_riscv && ty == SHT_RISCV_ATTRIBUTES) + return true; + return false; +} + template void ObjectFile::initialize_sections(Context &ctx) { // Read sections @@ -305,6 +331,10 @@ void ObjectFile::initialize_sections(Context &ctx) { case SHT_NULL: break; default: + if (!is_known_section_type(shdr)) + Fatal(ctx) << *this << ": " << name << ": unsupported section type: 0x" + << std::hex << (u32)shdr.sh_type; + // .note.GNU-stack section controls executable-ness of the stack // area in GNU linkers. We ignore that section because silently // making the stack area executable is too dangerous. Tell our diff --git a/test/elf/unkown-section-type.sh b/test/elf/unkown-section-type.sh new file mode 100755 index 00000000..15c759f2 --- /dev/null +++ b/test/elf/unkown-section-type.sh @@ -0,0 +1,12 @@ +#!/bin/bash +. $(dirname $0)/common.inc + +# ARM assembler does not seem to accept a hexnum after the atsign +[ $MACHINE = arm ] && skip + +cat <& $t/log1 +grep -q 'unsupported section type: 0x80000000' $t/log1