[PATCH] D81988: [ELF] Fix a dyn_cast<Defined>(nullptr) crash if a local symbol appears in InputFile::symbols

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 17 09:40:50 PDT 2020


MaskRay updated this revision to Diff 271387.
MaskRay marked an inline comment as done.
MaskRay added a comment.

Add comment to code. Improve test.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D81988/new/

https://reviews.llvm.org/D81988

Files:
  lld/ELF/InputSection.cpp
  lld/test/ELF/invalid/broken-symtab-duplicate-symbol.test


Index: lld/test/ELF/invalid/broken-symtab-duplicate-symbol.test
===================================================================
--- /dev/null
+++ lld/test/ELF/invalid/broken-symtab-duplicate-symbol.test
@@ -0,0 +1,28 @@
+# REQUIRES: x86
+## The ELF spec says all symbols with STB_LOCAL binding precede the weak and #
+## global symbols. Out-of-order local symbols are represented by null entries in
+## InputFile::symbols. Test that we don't crash.
+
+# RUN: yaml2obj %s -o %t.o
+# RUN: not ld.lld %t.o %t.o -o /dev/null 2>&1 | FileCheck %s
+# CHECK:      error: duplicate symbol: _start
+# CHECK-NEXT: >>> defined at {{.*}}.o:(.text+0x0)
+# CHECK-NEXT: >>> defined at {{.*}}.o:(.text+0x0)
+
+!ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+  Machine: EM_X86_64
+Sections:
+  - Type:  SHT_PROGBITS
+    Name:  .text
+    Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
+Symbols:
+  - Name:    _start
+    Section: .text
+    Binding: STB_GLOBAL
+  - Name:    local
+    Section: .text
+    Binding: STB_LOCAL
Index: lld/ELF/InputSection.cpp
===================================================================
--- lld/ELF/InputSection.cpp
+++ lld/ELF/InputSection.cpp
@@ -345,9 +345,10 @@
   if (!file->archiveName.empty())
     archive = " in archive " + file->archiveName;
 
-  // Find a symbol that encloses a given location.
+  // Find a symbol that encloses a given location. An out-of-order STB_LOCAL
+  // symbol is represented by a null entry.
   for (Symbol *b : file->getSymbols())
-    if (auto *d = dyn_cast<Defined>(b))
+    if (auto *d = dyn_cast_or_null<Defined>(b))
       if (d->section == this && d->value <= off && off < d->value + d->size)
         return filename + ":(" + toString(*d) + ")" + archive;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D81988.271387.patch
Type: text/x-patch
Size: 1754 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200617/f5c5ad9c/attachment.bin>


More information about the llvm-commits mailing list