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

James Henderson via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 11 01:15:03 PDT 2025


================
@@ -2015,80 +2016,102 @@ void COFFDumper::printCOFFPseudoReloc() {
                                      ? "___RUNTIME_PSEUDO_RELOC_LIST_END__"
                                      : "__RUNTIME_PSEUDO_RELOC_LIST_END__";
 
-  auto Count = Obj->getNumberOfSymbols();
-  if (Count == 0) {
-    W.startLine() << "the symbol table has been stripped\n";
+  uint32_t Count = Obj->getNumberOfSymbols();
+  // Skip if no symbol was found (maybe stripped).
+  if (Count == 0)
     return;
-  }
 
   struct SymbolEntry {
+    uint32_t RVA;
     COFFSymbolRef Symbol;
     const coff_section *Section;
     StringRef SymbolName;
   };
-  std::map<uint32_t, SymbolEntry> RVASymbolMap;
+  SmallVector<SymbolEntry> RVASymbolMap;
   COFFSymbolRef RelocBegin, RelocEnd;
-  for (auto i = 0u; i < Count; ++i) {
-    auto Sym = Obj->getSymbol(i);
-    if (!Sym) {
-      consumeError(Sym.takeError());
+  for (uint32_t i = 0; i < Count; ++i) {
+    COFFSymbolRef Sym;
+    if (Expected<COFFSymbolRef> SymOrErr = Obj->getSymbol(i))
+      Sym = *SymOrErr;
+    else {
+      reportWarning(SymOrErr.takeError(), Obj->getFileName());
       continue;
     }
-    i += Sym->getNumberOfAuxSymbols();
 
-    if (Sym->getSectionNumber() <= 0)
+    i += Sym.getNumberOfAuxSymbols();
+
+    if (Sym.getSectionNumber() <= 0)
       continue;
-    auto Name = Obj->getSymbolName(*Sym);
-    if (!Name) {
-      consumeError(Name.takeError());
+
+    StringRef Name;
+    if (Expected<StringRef> NameOrErr = Obj->getSymbolName(Sym))
+      Name = *NameOrErr;
+    else {
+      reportWarning(NameOrErr.takeError(), Obj->getFileName());
       continue;
     }
 
-    if (*Name == RelocBeginName)
-      RelocBegin = *Sym;
-    else if (*Name == RelocEndName)
-      RelocEnd = *Sym;
-
-    auto Sec = Obj->getSection(Sym->getSectionNumber());
-    if (!Sec) {
-      consumeError(Sec.takeError());
+    if (Name == RelocBeginName)
+      RelocBegin = Sym;
+    else if (Name == RelocEndName)
+      RelocEnd = Sym;
+
+    const coff_section *Sec = nullptr;
+    if (Expected<const coff_section *> SecOrErr =
+            Obj->getSection(Sym.getSectionNumber()))
+      Sec = *SecOrErr;
+    else {
+      reportWarning(SecOrErr.takeError(), Obj->getFileName());
       continue;
     }
-    RVASymbolMap.emplace((*Sec)->VirtualAddress + Sym->getValue(),
-                         SymbolEntry{*Sym, *Sec, *Name});
+
+    RVASymbolMap.push_back(
+        {Sec->VirtualAddress + Sym.getValue(), Sym, Sec, Name});
   }
+
   if (!RelocBegin.getRawPtr() || !RelocEnd.getRawPtr()) {
-    W.startLine()
-        << "the symbols for runtime pseudo-relocation are not found\n";
+    reportWarning(
+        createStringError(
+            "the marker symbols for runtime pseudo-relocation were not found"),
+        Obj->getFileName());
     return;
   }
 
-  if (RelocEnd.getValue() < RelocBegin.getValue()) {
-    reportWarning(createStringError("the symbols for runtime pseudo-relocation "
-                                    "don't consist a valid region"),
-                  Obj->getFileName());
-    return;
-  }
+  const coff_section *Section = nullptr;
+  if (Expected<const coff_section *> SecOrErr =
+          Obj->getSection(RelocBegin.getSectionNumber()))
+    Section = *SecOrErr;
+  else
+    return reportWarning(SecOrErr.takeError(), Obj->getFileName());
 
-  auto Section = Obj->getSection(RelocBegin.getSectionNumber());
-  if (auto E = Section.takeError()) {
-    reportWarning(std::move(E), Obj->getFileName());
+  if (RelocBegin.getSectionNumber() != RelocEnd.getSectionNumber())
+    return reportWarning(
+        createStringError(
+            "the marker symbols for runtime pseudo-relocation must "
+            "point a same section"),
+        Obj->getFileName());
+
+  // Skip if the relocation list is empty.
+  if (RelocBegin.getValue() == RelocEnd.getValue())
     return;
-  }
+
+  if (RelocEnd.getValue() < RelocBegin.getValue())
+    return reportWarning(
+        createStringError(
+            "the begin marker symbol for runtime pseudo-relocation must point "
+            "lower address than where the end marker points"),
----------------
jh7370 wrote:

```suggestion
            "the begin marker symbol for runtime pseudo-relocation must point "
            "to a lower address than where the end marker points"),
```
I'd also include the actual addresses.

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


More information about the llvm-commits mailing list