[PATCH] D74510: [ELF] Fix a null pointer dereference when --emit-relocs and --strip-debug are used together
Fangrui Song via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Feb 12 15:00:03 PST 2020
MaskRay created this revision.
MaskRay added reviewers: grimar, kees.
Herald added subscribers: llvm-commits, arichardson, emaste.
Herald added a reviewer: espindola.
Herald added a project: LLVM.
MaskRay marked an inline comment as done.
MaskRay added inline comments.
================
Comment at: lld/test/ELF/emit-relocs-debug.s:3
+## Test --emit-relocs handles .debug*
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
----------------
I'm a bit reluctant to add another test for the obsoleted feature .zdebug*
Fixes https://bugs.llvm.org//show_bug.cgi?id=44878
When --strip-debug is specified, .debug* are removed from inputSections
while .rel[a].debug* (incorrectly) remain.
LinkerScript::addOrphanSections() requires the output section of a relocated
InputSectionBase to be created first.
.debug* are not in inputSections -> output sections .debug* are not
created -> getOutputSectionName(.rel[a].debug*) dereferences a null
pointer.
Fix the null pointer dereference by deleting .rel[a].debug* from inputSections as well.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D74510
Files:
lld/ELF/Driver.cpp
lld/ELF/InputSection.cpp
lld/ELF/InputSection.h
lld/test/ELF/emit-relocs-debug.s
Index: lld/test/ELF/emit-relocs-debug.s
===================================================================
--- /dev/null
+++ lld/test/ELF/emit-relocs-debug.s
@@ -0,0 +1,20 @@
+# REQUIRES: x86
+## Test --emit-relocs handles .debug*
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
+# RUN: ld.lld --emit-relocs %t.o -o %t
+# RUN: llvm-readobj -r %t | FileCheck %s
+# RUN: ld.lld --emit-relocs --strip-debug %t.o -o %t.no
+# RUN: llvm-readobj -r %t.no | FileCheck --check-prefix=NO %s
+
+# CHECK: Section {{.*}} .rela.debug_info {
+# CHECK-NEXT: R_X86_64_64 .text 0x0
+# CHECK-NEXT: }
+
+# NO: Relocations [
+# NO-NEXT: ]
+
+foo:
+
+.section .debug_info
+.quad foo
Index: lld/ELF/InputSection.h
===================================================================
--- lld/ELF/InputSection.h
+++ lld/ELF/InputSection.h
@@ -357,6 +357,10 @@
template <class ELFT> void copyShtGroup(uint8_t *buf);
};
+inline bool isDebugSection(InputSectionBase *sec) {
+ return sec->name.startswith(".debug") || sec->name.startswith(".zdebug");
+}
+
// The list of all input sections.
extern std::vector<InputSectionBase *> inputSections;
Index: lld/ELF/InputSection.cpp
===================================================================
--- lld/ELF/InputSection.cpp
+++ lld/ELF/InputSection.cpp
@@ -441,8 +441,7 @@
// See the comment in maybeReportUndefined for PPC64 .toc .
auto *d = dyn_cast<Defined>(&sym);
if (!d) {
- if (!sec->name.startswith(".debug") &&
- !sec->name.startswith(".zdebug") && sec->name != ".eh_frame" &&
+ if (!isDebugSection(sec) && sec->name != ".eh_frame" &&
sec->name != ".gcc_except_table" && sec->name != ".toc") {
uint32_t secIdx = cast<Undefined>(sym).discardedSecIdx;
Elf_Shdr_Impl<ELFT> sec =
Index: lld/ELF/Driver.cpp
===================================================================
--- lld/ELF/Driver.cpp
+++ lld/ELF/Driver.cpp
@@ -1938,8 +1938,17 @@
// We do not want to emit debug sections if --strip-all
// or -strip-debug are given.
- return config->strip != StripPolicy::None &&
- (s->name.startswith(".debug") || s->name.startswith(".zdebug"));
+ if (config->strip == StripPolicy::None)
+ return false;
+
+ if (isDebugSection(s))
+ return true;
+ if (auto *isec = dyn_cast<InputSection>(s))
+ if (InputSectionBase *rel = isec->getRelocatedSection())
+ if (isDebugSection(rel))
+ return true;
+
+ return false;
});
// Now that the number of partitions is fixed, save a pointer to the main
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D74510.244284.patch
Type: text/x-patch
Size: 2601 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200212/9d3715e7/attachment.bin>
More information about the llvm-commits
mailing list