[llvm] [dsymutil] Add option to filter debug map objects by allowlist (PR #182083)

Roy Shi via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 18 10:12:18 PST 2026


https://github.com/royitaqi created https://github.com/llvm/llvm-project/pull/182083

# Motivation

When using `dsymutil` to generate dSYM for large binaries, sometimes we want to keep only some of the symbols. This has the benefits of reduced dSYM size and improved performance of LLDB and other tools which consume the dSYM.

The current way to achieve this is to use YAML input (the `dsymutil -y` option). However, it has the drawback of having to replicate all relevant info from the binary into the YAML. This includes object file paths, symbol names and addresses.

This patch propose a new way, by adding a new option (`dsymutil --allowed-debug-map-objects <path>`), which will only process object files listed in the given text file.

Important details:
* Currently, this only filters N_OSO entries. This does _not_ affect N_LIB and N_AST entries.
    * The name of the option is intentionally generic, in case one want to extend the support to N_LIB and N_AST in the future.
* The input text file should contain paths of allowed object files, one per line.
* Exact match is used when comparing N_OSO entries in the binary and the allowlist.
* `--oso-prepend-path` does not apply to the paths in the allowlist.

# Test

```
% bin/llvm-lit -v \
    ../llvm-project/llvm/test/tools/dsymutil/cmdline.test \
    ../llvm-project/llvm/test/tools/dsymutil/AArch64/allowed-debug-map-objects.test

-- Testing: 2 tests, 2 workers --
PASS: LLVM :: tools/dsymutil/cmdline.test (1 of 2)
PASS: LLVM :: tools/dsymutil/AArch64/allowed-debug-map-objects.test (2 of 2)

Testing Time: 4.32s

Total Discovered Tests: 2
  Passed: 2 (100.00%)
```


>From f27234e39afa32f052b0e2288d8b582de217fcd3 Mon Sep 17 00:00:00 2001
From: Roy Shi <royshi at meta.com>
Date: Wed, 18 Feb 2026 07:41:12 -0800
Subject: [PATCH] [dsymutil] Allow filtering the debug map objects according to
 input text file

---
 llvm/docs/CommandGuide/dsymutil.rst         |  5 +++
 llvm/test/tools/dsymutil/cmdline.test       |  4 +++
 llvm/tools/dsymutil/MachODebugMapParser.cpp | 30 ++++++++++++++---
 llvm/tools/dsymutil/Options.td              |  6 ++++
 llvm/tools/dsymutil/dsymutil.cpp            | 37 ++++++++++++++++++++-
 llvm/tools/dsymutil/dsymutil.h              |  6 +++-
 6 files changed, 82 insertions(+), 6 deletions(-)

diff --git a/llvm/docs/CommandGuide/dsymutil.rst b/llvm/docs/CommandGuide/dsymutil.rst
index 0e442d657e987..3c91cb1959525 100644
--- a/llvm/docs/CommandGuide/dsymutil.rst
+++ b/llvm/docs/CommandGuide/dsymutil.rst
@@ -23,6 +23,11 @@ OPTIONS
  Specify the desired type of accelerator table. Valid options are 'Apple',
  'Dwarf', 'Default' and 'None'.
 
+.. option:: --allowed-debug-map-objects <path>
+
+ Only process debug map objects listed in <path> (one object path per line,
+ exact match).
+
 .. option:: --arch <arch>
 
  Link DWARF debug information only for specified CPU architecture types.
diff --git a/llvm/test/tools/dsymutil/cmdline.test b/llvm/test/tools/dsymutil/cmdline.test
index 0b0bce194d575..524a70efa25b1 100644
--- a/llvm/test/tools/dsymutil/cmdline.test
+++ b/llvm/test/tools/dsymutil/cmdline.test
@@ -6,6 +6,7 @@ HELP: USAGE: {{.*}}dsymutil{{[^ ]*}} [options] <input files>
 HELP-NOT: -reverse-iterate
 HELP: Dsymutil Options:
 CHECK: -accelerator
+CHECK: -allowed-debug-map-objects <path>
 CHECK: -arch <arch>
 CHECK: -build-variant-suffix <suffix=buildvariant>
 CHECK: -dump-debug-map
@@ -52,3 +53,6 @@ BOGUS: warning: ignoring unknown option: -bogus
 
 RUN: not dsymutil --quiet --verbose 2>&1 | FileCheck --check-prefix=CONFLICT %s
 CONFLICT: error: --quiet and --verbose cannot be specified together
+
+RUN: not dsymutil -y file1 --allowed-debug-map-objects file2 2>&1 | FileCheck --check-prefix=CONFLICT2 %s
+CONFLICT2: error: -y and --allowed-debug-map-objects cannot be specified together
diff --git a/llvm/tools/dsymutil/MachODebugMapParser.cpp b/llvm/tools/dsymutil/MachODebugMapParser.cpp
index a0ba2512e12f2..1db0f12979f3a 100644
--- a/llvm/tools/dsymutil/MachODebugMapParser.cpp
+++ b/llvm/tools/dsymutil/MachODebugMapParser.cpp
@@ -12,6 +12,7 @@
 #include "RelocationMap.h"
 #include "llvm/ADT/DenseSet.h"
 #include "llvm/ADT/SmallSet.h"
+#include "llvm/ADT/StringSet.h"
 #include "llvm/Object/MachO.h"
 #include "llvm/Support/Chrono.h"
 #include "llvm/Support/Path.h"
@@ -31,11 +32,14 @@ class MachODebugMapParser {
                       ArrayRef<std::string> Archs,
                       ArrayRef<std::string> DSYMSearchPaths,
                       StringRef PathPrefix = "", StringRef VariantSuffix = "",
-                      bool Verbose = false)
+                      bool Verbose = false,
+                      const std::optional<StringSet<>>
+                          &AllowedDebugMapObjects = std::nullopt)
       : BinaryPath(std::string(BinaryPath)), Archs(Archs),
         DSYMSearchPaths(DSYMSearchPaths), PathPrefix(std::string(PathPrefix)),
         VariantSuffix(std::string(VariantSuffix)), BinHolder(BinHolder),
-        CurrentDebugMapObject(nullptr), SkipDebugMapObject(false) {}
+        CurrentDebugMapObject(nullptr), SkipDebugMapObject(false),
+        AllowedDebugMapObjects(AllowedDebugMapObjects) {}
 
   /// Parses and returns the DebugMaps of the input binary. The binary contains
   /// multiple maps in case it is a universal binary.
@@ -80,6 +84,10 @@ class MachODebugMapParser {
   /// Whether we need to skip the current debug map object.
   bool SkipDebugMapObject;
 
+  /// Optional set of allowed debug map object paths. If set, only objects
+  /// whose path is in this set will be included.
+  const std::optional<StringSet<>> &AllowedDebugMapObjects;
+
   /// Holds function info while function scope processing.
   const char *CurrentFunctionName;
   uint64_t CurrentFunctionAddress;
@@ -120,6 +128,13 @@ class MachODebugMapParser {
 
   void addCommonSymbols();
 
+  /// Check if a debug map object should be included based on the allowed list.
+  bool shouldIncludeObject(StringRef Path) const {
+    if (!AllowedDebugMapObjects.has_value())
+      return true;
+    return AllowedDebugMapObjects->contains(Path);
+  }
+
   /// Dump the symbol table output header.
   void dumpSymTabHeader(raw_ostream &OS, StringRef Arch);
 
@@ -191,6 +206,11 @@ void MachODebugMapParser::switchToNewDebugMapObject(
   SmallString<80> Path(PathPrefix);
   sys::path::append(Path, Filename);
 
+  if (!shouldIncludeObject(Path)) {
+    SkipDebugMapObject = true;
+    return;
+  }
+
   auto ObjectEntry = BinHolder.getObjectEntry(Path, Timestamp);
   if (!ObjectEntry) {
     auto Err = ObjectEntry.takeError();
@@ -857,13 +877,15 @@ llvm::ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
 parseDebugMap(BinaryHolder &BinHolder, StringRef InputFile,
               ArrayRef<std::string> Archs,
               ArrayRef<std::string> DSYMSearchPaths, StringRef PrependPath,
-              StringRef VariantSuffix, bool Verbose, bool InputIsYAML) {
+              StringRef VariantSuffix, bool Verbose, bool InputIsYAML,
+              const std::optional<StringSet<>> &AllowedDebugMapObjects) {
   if (InputIsYAML)
     return DebugMap::parseYAMLDebugMap(BinHolder, InputFile, PrependPath,
                                        Verbose);
 
   MachODebugMapParser Parser(BinHolder, InputFile, Archs, DSYMSearchPaths,
-                             PrependPath, VariantSuffix, Verbose);
+                             PrependPath, VariantSuffix, Verbose,
+                             AllowedDebugMapObjects);
 
   return Parser.parse();
 }
diff --git a/llvm/tools/dsymutil/Options.td b/llvm/tools/dsymutil/Options.td
index 5334aa0a747b1..6d6e915c2e61d 100644
--- a/llvm/tools/dsymutil/Options.td
+++ b/llvm/tools/dsymutil/Options.td
@@ -229,6 +229,12 @@ def build_variant_suffix: Separate<["--", "-"], "build-variant-suffix">,
   Group<grp_general>;
 def: Joined<["--", "-"], "build-variant-suffix=">, Alias<build_variant_suffix>;
 
+def allowed_debug_map_objects: Separate<["--", "-"], "allowed-debug-map-objects">,
+  MetaVarName<"<path>">,
+  HelpText<"Only process debug map objects listed in <path> (one object path per line, exact match).">,
+  Group<grp_general>;
+def: Joined<["--", "-"], "allowed-debug-map-objects=">, Alias<allowed_debug_map_objects>;
+
 def dsym_search_path: Separate<["-", "--"], "D">,
   MetaVarName<"<path>">,
   HelpText<"Specify a directory that contain dSYM files to search for.">,
diff --git a/llvm/tools/dsymutil/dsymutil.cpp b/llvm/tools/dsymutil/dsymutil.cpp
index ce55b7f6183e6..b5166d47b3f69 100644
--- a/llvm/tools/dsymutil/dsymutil.cpp
+++ b/llvm/tools/dsymutil/dsymutil.cpp
@@ -23,6 +23,7 @@
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/StringSet.h"
 #include "llvm/DebugInfo/DIContext.h"
 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
 #include "llvm/DebugInfo/DWARF/DWARFVerifier.h"
@@ -38,6 +39,7 @@
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/FormatVariadic.h"
 #include "llvm/Support/LLVMDriver.h"
+#include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/TargetSelect.h"
 #include "llvm/Support/ThreadPool.h"
@@ -114,6 +116,7 @@ struct DsymutilOptions {
   std::string OutputFile;
   std::string Toolchain;
   std::string ReproducerPath;
+  std::string AllowedDebugMapObjectsFile;
   std::vector<std::string> Archs;
   std::vector<std::string> InputFiles;
   unsigned NumThreads;
@@ -200,6 +203,11 @@ static Error verifyOptions(const DsymutilOptions &Options) {
         "cannot combine --gen-reproducer and --use-reproducer.",
         errc::invalid_argument);
 
+  if (Options.InputIsYAMLDebugMap && !Options.AllowedDebugMapObjectsFile.empty())
+    return make_error<StringError>(
+        "-y and --allowed-debug-map-objects cannot be specified together",
+        errc::invalid_argument);
+
   return Error::success();
 }
 
@@ -402,6 +410,10 @@ static Expected<DsymutilOptions> getOptions(opt::InputArgList &Args) {
   for (auto *SearchPath : Args.filtered(OPT_dsym_search_path))
     Options.LinkOpts.DSYMSearchPaths.push_back(SearchPath->getValue());
 
+  if (opt::Arg *AllowedObjs =
+          Args.getLastArg(OPT_allowed_debug_map_objects))
+    Options.AllowedDebugMapObjectsFile = AllowedObjs->getValue();
+
   if (Error E = verifyOptions(Options))
     return std::move(E);
   return Options;
@@ -688,10 +700,33 @@ int dsymutil_main(int argc, char **argv, const llvm::ToolContext &) {
       continue;
     }
 
+    // Read the allowed debug map objects file if specified.
+    std::optional<StringSet<>> AllowedDebugMapObjects;
+    if (!Options.AllowedDebugMapObjectsFile.empty()) {
+      auto BufOrErr =
+          MemoryBuffer::getFile(Options.AllowedDebugMapObjectsFile);
+      if (!BufOrErr) {
+        WithColor::error() << "cannot open allowed debug map objects file '"
+                           << Options.AllowedDebugMapObjectsFile
+                           << "': " << BufOrErr.getError().message() << '\n';
+        return EXIT_FAILURE;
+      }
+      AllowedDebugMapObjects.emplace();
+      StringRef Content = (*BufOrErr)->getBuffer();
+      SmallVector<StringRef, 0> Lines;
+      Content.split(Lines, '\n');
+      for (StringRef Line : Lines) {
+        Line = Line.trim();
+        if (!Line.empty())
+          AllowedDebugMapObjects->insert(Line);
+      }
+    }
+
     auto DebugMapPtrsOrErr = parseDebugMap(
         BinHolder, InputFile, Options.Archs, Options.LinkOpts.DSYMSearchPaths,
         Options.LinkOpts.PrependPath, Options.LinkOpts.BuildVariantSuffix,
-        Options.LinkOpts.Verbose, Options.InputIsYAMLDebugMap);
+        Options.LinkOpts.Verbose, Options.InputIsYAMLDebugMap,
+        AllowedDebugMapObjects);
 
     if (auto EC = DebugMapPtrsOrErr.getError()) {
       WithColor::error() << "cannot parse the debug map for '" << InputFile
diff --git a/llvm/tools/dsymutil/dsymutil.h b/llvm/tools/dsymutil/dsymutil.h
index 7b97b8bcd3a25..04d2cf1a5954a 100644
--- a/llvm/tools/dsymutil/dsymutil.h
+++ b/llvm/tools/dsymutil/dsymutil.h
@@ -21,9 +21,11 @@
 #include "LinkUtils.h"
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/StringSet.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/ErrorOr.h"
 #include <memory>
+#include <optional>
 #include <string>
 #include <vector>
 
@@ -37,7 +39,9 @@ ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
 parseDebugMap(BinaryHolder &BinHolder, StringRef InputFile,
               ArrayRef<std::string> Archs,
               ArrayRef<std::string> DSYMSearchPaths, StringRef PrependPath,
-              StringRef VariantSuffix, bool Verbose, bool InputIsYAML);
+              StringRef VariantSuffix, bool Verbose, bool InputIsYAML,
+              const std::optional<StringSet<>> &AllowedDebugMapObjects =
+                  std::nullopt);
 
 /// Dump the symbol table.
 bool dumpStab(BinaryHolder &BinHolder, StringRef InputFile,



More information about the llvm-commits mailing list