[llvm] [llvm-readobj][COFF] Implement --coff-pseudoreloc in llvm-readobj to dump runtime pseudo-relocation records (PR #151816)

Tomohiro Kashiwada via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 10 20:07:01 PDT 2025


================
@@ -2002,51 +2002,94 @@ void COFFDumper::printCOFFBaseReloc() {
 }
 
 void COFFDumper::printCOFFPseudoReloc() {
+  if (!Obj->getDOSHeader()) {
+    W.startLine()
+        << "pseudo-relocation is only meaningful for a PE image file\n";
+    return;
+  }
+
   const StringRef RelocBeginName = Obj->getArch() == Triple::x86
                                        ? "___RUNTIME_PSEUDO_RELOC_LIST__"
                                        : "__RUNTIME_PSEUDO_RELOC_LIST__";
   const StringRef RelocEndName = Obj->getArch() == Triple::x86
                                      ? "___RUNTIME_PSEUDO_RELOC_LIST_END__"
                                      : "__RUNTIME_PSEUDO_RELOC_LIST_END__";
 
-  COFFSymbolRef RelocBegin, RelocEnd;
   auto Count = Obj->getNumberOfSymbols();
   if (Count == 0) {
-    W.startLine() << "The symbol table has been stripped\n";
+    W.startLine() << "the symbol table has been stripped\n";
     return;
   }
-  for (auto i = 0u;
-       i < Count && (!RelocBegin.getRawPtr() || !RelocEnd.getRawPtr()); ++i) {
+
+  struct SymbolEntry {
+    COFFSymbolRef Symbol;
+    const coff_section *Section;
+    StringRef SymbolName;
+  };
+  std::map<uint32_t, SymbolEntry> RVASymbolMap;
+  COFFSymbolRef RelocBegin, RelocEnd;
+  for (auto i = 0u; i < Count; ++i) {
     auto Sym = Obj->getSymbol(i);
-    if (Sym.takeError())
+    if (!Sym) {
+      consumeError(Sym.takeError());
+      continue;
+    }
+    i += Sym->getNumberOfAuxSymbols();
+
+    if (Sym->getSectionNumber() <= 0)
       continue;
     auto Name = Obj->getSymbolName(*Sym);
-    if (Name.takeError())
+    if (!Name) {
+      consumeError(Name.takeError());
       continue;
-    if (*Name == RelocBeginName) {
-      if (Sym->getSectionNumber() > 0)
-        RelocBegin = *Sym;
-    } else if (*Name == RelocEndName) {
-      if (Sym->getSectionNumber() > 0)
-        RelocEnd = *Sym;
     }
+
+    if (*Name == RelocBeginName)
+      RelocBegin = *Sym;
+    else if (*Name == RelocEndName)
+      RelocEnd = *Sym;
+
+    auto Sec = Obj->getSection(Sym->getSectionNumber());
+    if (!Sec) {
+      consumeError(Sec.takeError());
+      continue;
+    }
+    RVASymbolMap.emplace((*Sec)->VirtualAddress + Sym->getValue(),
+                         SymbolEntry{*Sym, *Sec, *Name});
   }
   if (!RelocBegin.getRawPtr() || !RelocEnd.getRawPtr()) {
     W.startLine()
-        << "The symbols for runtime pseudo-relocation are not found\n";
+        << "the symbols for runtime pseudo-relocation are not found\n";
----------------
kikairoya wrote:

It's usually suspicious state to be warned, but theoretically not.
- Although `link.exe` (and `lld-link` without `-debug:dwarf` ) omits the symbol table, the PE spec allows a PE image with a symbol table.
- With custom `crt0.o` and/or a custom linker script can omit the marker of the pseudo relocation.

That said, making this message a warning makes sense to me as those conditions are extremely rare.

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


More information about the llvm-commits mailing list