[lld] 0930f62 - [ELF] -r: fix crash when SHF_LINK_ORDER linked-to section has a larger index

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 8 21:13:31 PST 2024


Author: Fangrui Song
Date: 2024-01-08T21:13:27-08:00
New Revision: 0930f62cf600d9e2e9a45fef1b3a422d50be89d5

URL: https://github.com/llvm/llvm-project/commit/0930f62cf600d9e2e9a45fef1b3a422d50be89d5
DIFF: https://github.com/llvm/llvm-project/commit/0930f62cf600d9e2e9a45fef1b3a422d50be89d5.diff

LOG: [ELF] -r: fix crash when SHF_LINK_ORDER linked-to section has a larger index

Fixes: b8dface221f4490933b0d39deb769e97ca134e5f

ThinLTO asan build may place `asan_globals` before the associated `.bss.xxx` section.
`rel->getOutputSection()` is nullptr because `rel->parent` hasn't been
set, leading to a crash. Simplify return `s->name` in this case.

Added: 
    lld/test/ELF/linkorder-group.test

Modified: 
    lld/ELF/LinkerScript.cpp

Removed: 
    


################################################################################
diff  --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 28ae4b8543062f..03aec187668a43 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -57,6 +57,10 @@ static StringRef getOutputSectionName(const InputSectionBase *s) {
   if (auto *isec = dyn_cast<InputSection>(s)) {
     if (InputSectionBase *rel = isec->getRelocatedSection()) {
       OutputSection *out = rel->getOutputSection();
+      if (!out) {
+        assert(config->relocatable && (rel->flags & SHF_LINK_ORDER));
+        return s->name;
+      }
       if (s->type == SHT_RELA)
         return saver().save(".rela" + out->name);
       return saver().save(".rel" + out->name);

diff  --git a/lld/test/ELF/linkorder-group.test b/lld/test/ELF/linkorder-group.test
new file mode 100644
index 00000000000000..988f793cf63272
--- /dev/null
+++ b/lld/test/ELF/linkorder-group.test
@@ -0,0 +1,58 @@
+# REQUIRES: x86
+## Test SHF_LINK_ORDER when the linked-to section has a larger index.
+# RUN: yaml2obj %s -o %t.o
+# RUN: ld.lld -r %t.o -o %t.ro
+# RUN: llvm-readelf -x asan_globals %t.ro | FileCheck %s
+
+# CHECK:      Hex dump of section 'asan_globals':
+# CHECK-NEXT: 0x00000000 00                                  .
+
+--- !ELF
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  Type:            ET_REL
+  Machine:         EM_X86_64
+  SectionHeaderStringTable: .strtab
+Sections:
+  - Name:            .text
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+  - Name:            .bss
+    Type:            SHT_NOBITS
+    Flags:           [ SHF_WRITE, SHF_ALLOC, SHF_GROUP ]
+    Size:            0x1
+  - Name:            asan_globals
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_LINK_ORDER, SHF_GROUP ]
+    Link:            .bss
+    Content:         '00'
+  - Name:            .group
+    Type:            SHT_GROUP
+    Link:            .symtab
+    Info:            foo
+    Members:
+      - SectionOrType:   GRP_COMDAT
+      - SectionOrType:   .bss
+      - SectionOrType:   asan_globals
+      - SectionOrType:   .relaasan_globals
+  - Name:            .relaasan_globals
+    Type:            SHT_RELA
+    Flags:           [ SHF_GROUP ]
+    Link:            .symtab
+    Info:            asan_globals
+    Relocations:
+      - Type:            R_X86_64_NONE
+  - Type:            SectionHeaderTable
+    Sections:
+      - Name:            .strtab
+      - Name:            .text
+      - Name:            .group
+      - Name:            asan_globals
+      - Name:            .relaasan_globals
+      - Name:            .bss
+      - Name:            .symtab
+Symbols:
+  - Name:            foo
+    Section:         .bss
+    Binding:         STB_WEAK


        


More information about the llvm-commits mailing list