Skip to content

Commit

Permalink
Handle sections with an unknown section type as errors
Browse files Browse the repository at this point in the history
Fixes #1215
  • Loading branch information
rui314 committed Mar 12, 2024
1 parent c4b69a1 commit d21207c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
5 changes: 5 additions & 0 deletions elf/elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
30 changes: 30 additions & 0 deletions elf/input-files.cc
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,32 @@ static void read_riscv_attributes(Context<E> &ctx, ObjectFile<E> &file,
}
}

template <typename E>
static bool is_known_section_type(const ElfShdr<E> &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<E> && ty == SHT_X86_64_UNWIND)
return true;
if (is_arm32<E> && (ty == SHT_ARM_EXIDX || ty == SHT_ARM_ATTRIBUTES))
return true;
if (is_riscv<E> && ty == SHT_RISCV_ATTRIBUTES)
return true;
return false;
}

template <typename E>
void ObjectFile<E>::initialize_sections(Context<E> &ctx) {
// Read sections
Expand Down Expand Up @@ -305,6 +331,10 @@ void ObjectFile<E>::initialize_sections(Context<E> &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
Expand Down
12 changes: 12 additions & 0 deletions test/elf/unkown-section-type.sh
Original file line number Diff line number Diff line change
@@ -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 <<EOF | $CC -o $t/a.o -c -xassembler -
.section .my_section,"a",@0x80000000
EOF

! $CC -B. -o $t/exe $t/a.o >& $t/log1
grep -q 'unsupported section type: 0x80000000' $t/log1

0 comments on commit d21207c

Please sign in to comment.