From 412fa4b12ffdbb56513439da23ad03dbdb6ab6aa Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sun, 11 Aug 2024 23:00:38 +0200 Subject: [PATCH] Use PIE/PIC on Linux --- releasenotes.md | 1 + src/compiler/target.c | 16 ++++++---------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/releasenotes.md b/releasenotes.md index 218d79710..d7af990c9 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -31,6 +31,7 @@ - Printable values passed to the Formatter as pointers, will print as if passed by value. - Pointers are rendered with "0x" prefix when passed to '%s'. - Add temp allocator scribble. +- Use PIC by default on Linux. ### Fixes diff --git a/src/compiler/target.c b/src/compiler/target.c index 7a9248207..3e0e4e0d9 100644 --- a/src/compiler/target.c +++ b/src/compiler/target.c @@ -1661,10 +1661,11 @@ static RelocModel arch_os_reloc_default(ArchType arch, OsType os, EnvironmentTyp return ARCH_TYPE_X86_64 == arch ? RELOC_SMALL_PIC : RELOC_NONE; case OS_TYPE_WASI: return RELOC_NONE; + case OS_TYPE_LINUX: + return RELOC_SMALL_PIC; case OS_TYPE_UNKNOWN: case OS_TYPE_NONE: case OS_TYPE_FREE_BSD: - case OS_TYPE_LINUX: case OS_TYPE_NETBSD: switch (arch) { @@ -1688,13 +1689,8 @@ static RelocModel arch_os_reloc_default(ArchType arch, OsType os, EnvironmentTyp case OS_TYPE_WASI: case OS_TYPE_FREE_BSD: case OS_TYPE_NETBSD: - return RELOC_SMALL_PIE; case OS_TYPE_LINUX: - if (env == ENV_TYPE_MUSLEABI || env == ENV_TYPE_MUSLEABIHF || env == ENV_TYPE_ANDROID) - { - return RELOC_SMALL_PIE; - } - return RELOC_NONE; + return RELOC_SMALL_PIE; case OS_UNSUPPORTED: UNREACHABLE } @@ -2021,9 +2017,9 @@ void target_setup(BuildTarget *target) compiler.platform.arch, compiler.platform.environment_type); compiler.platform.reloc_model = arch_os_reloc_default(compiler.platform.arch, - compiler.platform.os, - compiler.platform.environment_type, - compiler.build.type != TARGET_TYPE_EXECUTABLE); + compiler.platform.os, + compiler.platform.environment_type, + compiler.build.type != TARGET_TYPE_EXECUTABLE); compiler.platform.pic_required = arch_os_pic_default_forced(compiler.platform.arch, compiler.platform.os); // Override PIC, but only if the platform does not require PIC if (target->reloc_model != RELOC_DEFAULT