[ELF] -r: don't create .interp

`{clang,gcc} -nostdlib -r a.c` passes --dynamic-linker to the linker,
and the expected behavior is to ignore it.

If .interp is kept in the relocatable object file, a final link will get
PT_INTERP even if --dynamic-linker is not specified. glibc ld.so expects
to see PT_DYNAMIC and the executable will likely fail to run.

Ignore --dynamic-linker in -r mode as well as -shared.
This commit is contained in:
Fangrui Song 2020-01-16 12:00:24 -08:00
parent d0b02aecac
commit 2d7a8cf904
2 changed files with 9 additions and 4 deletions

View File

@ -135,8 +135,8 @@ StringRef getOutputSectionName(const InputSectionBase *s) {
}
static bool needsInterpSection() {
return !config->shared && !config->dynamicLinker.empty() &&
script->needsInterpSection();
return !config->relocatable && !config->shared &&
!config->dynamicLinker.empty() && script->needsInterpSection();
}
template <class ELFT> void writeResult() { Writer<ELFT>().run(); }

View File

@ -10,11 +10,16 @@
# CHECK: [Requesting program interpreter: foo]
# RUN: ld.lld %t.o -o %t
# RUN: llvm-readelf -program-headers %t | FileCheck --check-prefix=NO %s
# RUN: llvm-readelf -S -l %t | FileCheck --check-prefix=NO %s
# RUN: ld.lld --dynamic-linker foo --no-dynamic-linker %t.o -o %t
# RUN: llvm-readelf --program-headers %t | FileCheck --check-prefix=NO %s
# RUN: llvm-readelf -S -l %t | FileCheck --check-prefix=NO %s
## {clang,gcc} -nostdlib -r passes --dynamic-linker, and the expected behavior is to ignore it.
# RUN: ld.lld -r --dynamic-linker foo %t.o -o %t
# RUN: llvm-readelf -S -l %t | FileCheck --check-prefix=NO %s
# NO-NOT: .interp
# NO-NOT: PT_INTERP
.globl _start