[llvm] [llvm-symbolizer] Make symbolizer parse section relative syntax (PR #168524)

James Henderson via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 25 03:05:09 PDT 2026


================
@@ -53,44 +53,39 @@ LLVMSymbolizer::LLVMSymbolizer(const Options &Opts)
 LLVMSymbolizer::~LLVMSymbolizer() = default;
 
 Expected<uint64_t>
-LLVMSymbolizer::getXCOFFSectionAddress(StringRef ModuleName,
+LLVMSymbolizer::getXCOFFSectionAddress(StringRef ModulePath,
                                        XCOFF::SectionTypeFlags SectionTypeFlag,
                                        StringRef SectionTypeName) {
   // Check the cache first.
-  auto CacheKey = std::make_pair(ModuleName.str(), SectionTypeFlag);
-  auto It = XCOFFSectionBaseCache.find(CacheKey);
-  if (It != XCOFFSectionBaseCache.end())
-    return It->second;
-
-  Expected<object::OwningBinary<object::Binary>> BinaryOrErr =
-      object::createBinary(ModuleName);
-  if (!BinaryOrErr)
-    return BinaryOrErr.takeError();
-
-  const auto *XCOFFObj =
-      dyn_cast<object::XCOFFObjectFile>(BinaryOrErr->getBinary());
+  auto &FlagMap = XCOFFSectionBaseCache[ModulePath];
+  auto CachedIt = FlagMap.find(SectionTypeFlag);
+  if (CachedIt != FlagMap.end())
+    return CachedIt->second;
+
+  Expected<ObjectFile *> ObjOrErr =
+      getOrCreateObject(ModulePath.str(), Opts.DefaultArch);
+  if (!ObjOrErr)
+    return ObjOrErr.takeError();
+
+  const auto *XCOFFObj = dyn_cast<object::XCOFFObjectFile>(*ObjOrErr);
   if (!XCOFFObj)
     return createStringError(
         "section type syntax is only supported for XCOFF objects");
 
-  std::optional<uint64_t> SectionBase;
-  for (const object::SectionRef &Section : XCOFFObj->sections()) {
-    DataRefImpl SecRef = Section.getRawDataRefImpl();
-    int32_t Flags = XCOFFObj->getSectionFlags(SecRef);
-    if ((Flags & 0xFFFF) != SectionTypeFlag)
-      continue;
-    if (SectionBase)
-      return createStringError("multiple '" + SectionTypeName +
-                               "' sections found in XCOFF object");
-    SectionBase = Section.getAddress();
+  Expected<DataRefImpl> DRIOrErr = XCOFFObj->getSectionByType(SectionTypeFlag);
+  if (!DRIOrErr) {
+    consumeError(DRIOrErr.takeError());
+    return createStringError("multiple '" + SectionTypeName +
+                             "' sections found in XCOFF object");
----------------
jh7370 wrote:

This is not the way to propagate an error for lower-level API calls. You should at least simply return the lower error up the stack, but optionally you may want to add additional context relevant to the call site. `consumeError` to replace it with a different error entirely is almost always wrong. How do you even know that the "multiple sections" error is the error that the lower-level code ran into?

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


More information about the llvm-commits mailing list