[ELF] Initialize Target before it may be dereferenced by findAux when reporting "duplicate symbol" error

for (InputFile *F : Files)
      Symtab->addFile<ELFT>(F); // if there is a duplicate symbol error

    ...

    Target = getTarget();

When parsing .debug_info in the object file (for better diagnostics),
DWARF.cpp findAux may dereference the null pointer Target

    auto *DR = dyn_cast<Defined>(&File->getRelocTargetSym(Rel));
    if (!DR) {
      // Broken debug info may point to a non-defined symbol,
      // some asan object files may also contain R_X86_64_NONE
      RelType Type = Rel.getType(Config->IsMips64EL);
      if (Type != Target->NoneRel) /// Target is null

Move the assignment of Target to an earlier place to fix this.

Reviewed By: ruiu

Differential Revision: https://reviews.llvm.org/D61712

llvm-svn: 360305
This commit is contained in:
Fangrui Song 2019-05-09 01:45:53 +00:00
parent 2dda1269ab
commit bd086817f6
2 changed files with 9 additions and 5 deletions

View File

@ -441,6 +441,11 @@ void LinkerDriver::main(ArrayRef<const char *> ArgsArr) {
if (errorCount())
return;
// The Target instance handles target-specific stuff, such as applying
// relocations or writing a PLT section. It also contains target-dependent
// values such as a default image base address.
Target = getTarget();
switch (Config->EKind) {
case ELF32LEKind:
link<ELF32LE>(Args);
@ -1617,11 +1622,6 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) {
if (Config->Strip != StripPolicy::None)
llvm::erase_if(InputSections, [](InputSectionBase *S) { return S->Debug; });
// The Target instance handles target-specific stuff, such as applying
// relocations or writing a PLT section. It also contains target-dependent
// values such as a default image base address.
Target = getTarget();
Config->EFlags = Target->calcEFlags();
Config->MaxPageSize = getMaxPageSize(Args);
Config->ImageBase = getImageBase(Args);

View File

@ -8,6 +8,10 @@
# CHECK: error: {{.*}}.o: relocation R_X86_64_64 at 0x29 has unsupported target
# CHECK: error: undefined symbol: bar
# We used to dereference null Target in DWARF.cpp:findAux while reporting a duplicate symbol error,
# because Target wasn't initialized yet.
# RUN: not ld.lld %t.o %t.o -o /dev/null
--- !ELF
FileHeader:
Class: ELFCLASS64