[ELF] Move invalid binding diagnostic from initializeSymbols to postParse
authorFangrui Song <i@maskray.me>
Wed, 16 Mar 2022 07:31:29 +0000 (00:31 -0700)
committerFangrui Song <i@maskray.me>
Wed, 16 Mar 2022 07:31:29 +0000 (00:31 -0700)
It is excessive to have a diagnostic for STB_LOCAL. Just reuse the invalid
binding diagnostic for STB_LOCAL.

lld/ELF/InputFiles.cpp
lld/test/ELF/invalid/invalid-binding.test
lld/test/ELF/invalid/symtab-sh-info-dup.test

index 362263e..1bce8de 100644 (file)
@@ -1025,19 +1025,13 @@ void ObjFile<ELFT>::initializeSymbols(const object::ELFFile<ELFT> &obj) {
   SmallVector<unsigned, 32> undefineds;
   for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) {
     const Elf_Sym &eSym = eSyms[i];
-    uint8_t binding = eSym.getBinding();
-    if (LLVM_UNLIKELY(binding == STB_LOCAL)) {
-      errorOrWarn(toString(this) + ": STB_LOCAL symbol (" + Twine(i) +
-                  ") found at index >= .symtab's sh_info (" +
-                  Twine(firstGlobal) + ")");
-      continue;
-    }
     uint32_t secIdx = eSym.st_shndx;
     if (secIdx == SHN_UNDEF) {
       undefineds.push_back(i);
       continue;
     }
 
+    uint8_t binding = eSym.getBinding();
     uint8_t stOther = eSym.st_other;
     uint8_t type = eSym.getType();
     uint64_t value = eSym.st_value;
@@ -1056,14 +1050,8 @@ void ObjFile<ELFT>::initializeSymbols(const object::ELFFile<ELFT> &obj) {
     }
 
     // Handle global defined symbols. Defined::section will be set in postParse.
-    if (binding == STB_GLOBAL || binding == STB_WEAK ||
-        binding == STB_GNU_UNIQUE) {
-      sym->resolve(Defined{this, StringRef(), binding, stOther, type, value,
-                           size, nullptr});
-      continue;
-    }
-
-    fatal(toString(this) + ": unexpected binding: " + Twine((int)binding));
+    sym->resolve(Defined{this, StringRef(), binding, stOther, type, value, size,
+                         nullptr});
   }
 
   // Undefined symbols (excluding those defined relative to non-prevailing
@@ -1130,6 +1118,11 @@ template <class ELFT> void ObjFile<ELFT>::postParse() {
     const Elf_Sym &eSym = eSyms[i];
     Symbol &sym = *symbols[i];
     uint32_t secIdx = eSym.st_shndx;
+    uint8_t binding = eSym.getBinding();
+    if (LLVM_UNLIKELY(binding != STB_GLOBAL && binding != STB_WEAK &&
+                      binding != STB_GNU_UNIQUE))
+      errorOrWarn(toString(this) + ": symbol (" + Twine(i) +
+                  ") has invalid binding: " + Twine((int)binding));
 
     // st_value of STT_TLS represents the assigned offset, not the actual
     // address which is used by STT_FUNC and STT_OBJECT. STT_TLS symbols can
@@ -1171,7 +1164,7 @@ template <class ELFT> void ObjFile<ELFT>::postParse() {
       continue;
     }
 
-    if (eSym.getBinding() == STB_WEAK)
+    if (binding == STB_WEAK)
       continue;
     std::lock_guard<std::mutex> lock(mu);
     ctx->duplicates.push_back({&sym, this, sec, eSym.st_value});
index 4d89633..75ec32f 100644 (file)
@@ -1,6 +1,6 @@
 # RUN: yaml2obj %s -o %t.o
 # RUN: not ld.lld %t.o -o /dev/null 2>&1 | FileCheck %s
-# CHECK: error: {{.*}}.o: unexpected binding: 9
+# CHECK: error: {{.*}}.o: symbol (1) has invalid binding: 9
 
 --- !ELF
 FileHeader:
index 8b0833d..f48938d 100644 (file)
@@ -8,15 +8,19 @@
 # RUN: yaml2obj %s -o %t.o
 # RUN: not ld.lld %t.o %t.o -o /dev/null 2>&1 | FileCheck %s
 
-# CHECK:      error: {{.*}}.o: STB_LOCAL symbol (2) found at index >= .symtab's sh_info (1)
-# CHECK-NEXT: error: {{.*}}.o: STB_LOCAL symbol (2) found at index >= .symtab's sh_info (1)
+# CHECK:      error: {{.*}}.o: symbol (2) has invalid binding: 0
+# CHECK-NEXT: error: {{.*}}.o: symbol (2) has invalid binding: 0
 # CHECK-NEXT: error: duplicate symbol: _start
 # CHECK-NEXT: >>> defined at {{.*}}.o:(.text+0x0)
 # CHECK-NEXT: >>> defined at {{.*}}.o:(.text+0x0)
 # CHECK-EMPTY:
+# CHECK-NEXT: error: duplicate symbol: local
+# CHECK-NEXT: >>> defined at {{.*}}.o:(.text+0x0)
+# CHECK-NEXT: >>> defined at {{.*}}.o:(.text+0x0)
+# CHECK-EMPTY:
 
 # RUN: ld.lld --noinhibit-exec %t.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=WARN
-# WARN: warning: {{.*}}.o: STB_LOCAL symbol (2) found at index >= .symtab's sh_info (1)
+# WARN: warning: {{.*}}.o: symbol (2) has invalid binding: 0
 
 !ELF
 FileHeader: