[lld] [lld][ELF] Demote symbols in discarded sections to Undefined. (PR #68777)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 14 14:42:24 PDT 2023


================
@@ -3062,6 +3062,31 @@ void LinkerDriver::link(opt::InputArgList &args) {
     script->addOrphanSections();
   }
 
+  // Explicitly demote symbols which didn't get placed. If we don't, any
+  // symbol in a discarded section will still be considered defined, even
+  // though it didn't end up in the output, and we get silently broken
+  // binaries.
+  if (script->seenDiscard) {
+    llvm::TimeTraceScope timeScope("Demote symbols in discarded sections");
+    parallelForEach(symtab.getSymbols(), [](Symbol *sym) {
+      if (Defined *d = dyn_cast<Defined>(sym))
----------------
MaskRay wrote:

This `if` use case requires `{}`. However, we can use an early return:
```
Defined *d = ...
if (!d)
  continue;
...
```

https://github.com/llvm/llvm-project/pull/68777


More information about the llvm-commits mailing list