From 0cc87d3c851448e669797c790ee4c6534016735d Mon Sep 17 00:00:00 2001 From: Sebastian Sumpf Date: Wed, 26 Sep 2018 13:47:04 +0200 Subject: [PATCH] ldso: check for DYNAMIC segment in ELF files If the DYNAMIC segment cannot be located the ELF file may be statically linked. In this case an error is raised. Fixes #3000 --- repos/base/src/lib/ldso/include/file.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/repos/base/src/lib/ldso/include/file.h b/repos/base/src/lib/ldso/include/file.h index 05cfb07452..d951211f97 100644 --- a/repos/base/src/lib/ldso/include/file.h +++ b/repos/base/src/lib/ldso/include/file.h @@ -183,9 +183,13 @@ struct Linker::Elf_file : File */ void loadable_segments(Phdr &result) { + bool dynamic = false; for (unsigned i = 0; i < phdr.count; i++) { Elf::Phdr *ph = &phdr.phdr[i]; + if (ph->p_type == PT_DYNAMIC) + dynamic = true; + if (ph->p_type != PT_LOAD) continue; @@ -196,6 +200,16 @@ struct Linker::Elf_file : File result.phdr[result.count++] = *ph; } + + /* + * Check for 'DYNAMIC' segment which should be present in all dynamic ELF + * files. + */ + if (!dynamic) { + error("LD: ELF without DYNAMIC segment appears to be statically linked ", + "(ld=\"no\")"); + throw Incompatible(); + } } bool is_rx(Elf::Phdr const &ph) {