[ELF] Move invalid binding diagnostic from initializeSymbols to postParse

It is excessive to have a diagnostic for STB_LOCAL. Just reuse the invalid
binding diagnostic for STB_LOCAL.
This commit is contained in:
Fangrui Song 2022-03-16 00:31:29 -07:00
parent c008a697ff
commit c9dbf407af
3 changed files with 17 additions and 20 deletions

View 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});

View 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:

View 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: