[lld] 8669028 - [ELF] Remove unneeded sym->file check

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 7 20:46:07 PST 2024


Author: Fangrui Song
Date: 2024-12-07T20:46:02-08:00
New Revision: 8669028c1898ac95a5b14a9eacb1d084ab7d7781

URL: https://github.com/llvm/llvm-project/commit/8669028c1898ac95a5b14a9eacb1d084ab7d7781
DIFF: https://github.com/llvm/llvm-project/commit/8669028c1898ac95a5b14a9eacb1d084ab7d7781.diff

LOG: [ELF] Remove unneeded sym->file check

After #78944 and some follow-ups, sym->file, unless in the initial
Placeholder stage, is guaranteed to be non-null.

Added: 
    

Modified: 
    lld/ELF/Arch/Mips.cpp
    lld/ELF/Arch/X86_64.cpp
    lld/ELF/InputFiles.cpp
    lld/ELF/Relocations.cpp
    lld/ELF/Symbols.cpp
    lld/ELF/Writer.cpp

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Arch/Mips.cpp b/lld/ELF/Arch/Mips.cpp
index 121127ae6b21d8..c4848fecc6f28e 100644
--- a/lld/ELF/Arch/Mips.cpp
+++ b/lld/ELF/Arch/Mips.cpp
@@ -369,7 +369,7 @@ bool MIPS<ELFT>::needsThunk(RelExpr expr, RelType type, const InputFile *file,
   if (type != R_MIPS_26 && type != R_MIPS_PC26_S2 &&
       type != R_MICROMIPS_26_S1 && type != R_MICROMIPS_PC26_S1)
     return false;
-  auto *f = dyn_cast_or_null<ObjFile<ELFT>>(file);
+  auto *f = dyn_cast<ObjFile<ELFT>>(file);
   if (!f)
     return false;
   // If current file has PIC code, LA25 stub is not required.

diff  --git a/lld/ELF/Arch/X86_64.cpp b/lld/ELF/Arch/X86_64.cpp
index 076176ebc9a4c4..0c4fd00cab65c3 100644
--- a/lld/ELF/Arch/X86_64.cpp
+++ b/lld/ELF/Arch/X86_64.cpp
@@ -1083,7 +1083,7 @@ static void relaxGot(uint8_t *loc, const Relocation &rel, uint64_t val) {
   if (op != 0xff) {
     // We are relaxing a rip relative to an absolute, so compensate
     // for the old -4 addend.
-    assert(!rel.sym->file || !rel.sym->file->ctx.arg.isPic);
+    assert(!rel.sym->file->ctx.arg.isPic);
     relaxGotNoPic(loc, val + 4, op, modRm,
                   rel.type == R_X86_64_CODE_4_GOTPCRELX);
     return;

diff  --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index 373292b4391c56..97b3e164bdc3f4 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -1219,7 +1219,7 @@ template <class ELFT> void ObjFile<ELFT>::postParse() {
 
     // Handle non-COMMON defined symbol below. !sym.file allows a symbol
     // assignment to redefine a symbol without an error.
-    if (!sym.file || !sym.isDefined() || secIdx == SHN_UNDEF)
+    if (!sym.isDefined() || secIdx == SHN_UNDEF)
       continue;
     if (LLVM_UNLIKELY(secIdx >= SHN_LORESERVE)) {
       if (secIdx == SHN_COMMON)

diff  --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp
index 4de24a21f6f43d..818eef3e2e78aa 100644
--- a/lld/ELF/Relocations.cpp
+++ b/lld/ELF/Relocations.cpp
@@ -513,7 +513,7 @@ int64_t RelocationScanner::computeMipsAddend(const RelTy &rel, RelExpr expr,
 // Custom error message if Sym is defined in a discarded section.
 template <class ELFT>
 static void maybeReportDiscarded(Ctx &ctx, ELFSyncStream &msg, Undefined &sym) {
-  auto *file = dyn_cast_or_null<ObjFile<ELFT>>(sym.file);
+  auto *file = dyn_cast<ObjFile<ELFT>>(sym.file);
   if (!file || !sym.discardedSecIdx)
     return;
   ArrayRef<typename ELFT::Shdr> objSections =
@@ -569,11 +569,11 @@ static const Symbol *getAlternativeSpelling(Ctx &ctx, const Undefined &sym,
                                             std::string &pre_hint,
                                             std::string &post_hint) {
   DenseMap<StringRef, const Symbol *> map;
-  if (sym.file && sym.file->kind() == InputFile::ObjKind) {
+  if (sym.file->kind() == InputFile::ObjKind) {
     auto *file = cast<ELFFileBase>(sym.file);
     // If sym is a symbol defined in a discarded section, maybeReportDiscarded()
     // will give an error. Don't suggest an alternative spelling.
-    if (file && sym.discardedSecIdx != 0 &&
+    if (sym.discardedSecIdx != 0 &&
         file->getSections()[sym.discardedSecIdx] == &InputSection::discarded)
       return nullptr;
 
@@ -743,9 +743,8 @@ static void reportUndefinedSymbol(Ctx &ctx, const UndefinedDiag &undef,
     std::string pre_hint = ": ", post_hint;
     if (const Symbol *corrected =
             getAlternativeSpelling(ctx, sym, pre_hint, post_hint)) {
-      msg << "\n>>> did you mean" << pre_hint << corrected << post_hint;
-      if (corrected->file)
-        msg << "\n>>> defined in: " << corrected->file;
+      msg << "\n>>> did you mean" << pre_hint << corrected << post_hint
+          << "\n>>> defined in: " << corrected->file;
     }
   }
 

diff  --git a/lld/ELF/Symbols.cpp b/lld/ELF/Symbols.cpp
index 2c7b59f32cf308..c520c5912ec4fb 100644
--- a/lld/ELF/Symbols.cpp
+++ b/lld/ELF/Symbols.cpp
@@ -463,8 +463,7 @@ void Symbol::resolve(Ctx &ctx, const Undefined &other) {
     // A forms group 0. B form group 1. C and D (including their member object
     // files) form group 2. E forms group 3. I think that you can see how this
     // group assignment rule simulates the traditional linker's semantics.
-    bool backref = ctx.arg.warnBackrefs && other.file &&
-                   file->groupId < other.file->groupId;
+    bool backref = ctx.arg.warnBackrefs && file->groupId < other.file->groupId;
     extract(ctx);
 
     if (!ctx.arg.whyExtract.empty())
@@ -477,7 +476,7 @@ void Symbol::resolve(Ctx &ctx, const Undefined &other) {
     // sandwich), where def2 may or may not be the same as def1. We don't want
     // to warn for this case, so dismiss the warning if we see a subsequent lazy
     // definition. this->file needs to be saved because in the case of LTO it
-    // may be reset to nullptr or be replaced with a file named lto.tmp.
+    // may be reset to internalFile or be replaced with a file named lto.tmp.
     if (backref && !isWeak())
       ctx.backwardReferences.try_emplace(this,
                                          std::make_pair(other.file, file));
@@ -485,7 +484,7 @@ void Symbol::resolve(Ctx &ctx, const Undefined &other) {
   }
 
   // Undefined symbols in a SharedFile do not change the binding.
-  if (isa_and_nonnull<SharedFile>(other.file))
+  if (isa<SharedFile>(other.file))
     return;
 
   if (isUndefined() || isShared()) {

diff  --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 49616fa03e63c6..8cd16704ba3ee3 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -1890,7 +1890,7 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
 
       if (sym->includeInDynsym(ctx)) {
         ctx.partitions[sym->partition - 1].dynSymTab->addSymbol(sym);
-        if (auto *file = dyn_cast_or_null<SharedFile>(sym->file))
+        if (auto *file = dyn_cast<SharedFile>(sym->file))
           if (file->isNeeded && !sym->isUndefined())
             addVerneed(ctx, *sym);
       }


        


More information about the llvm-commits mailing list