[llvm-branch-commits] [llvm] [dsymutil] Support compilation-prefix-map.json from compilation caching (PR #242436)
Steven Wu via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Sat Nov 14 15:19:03 PST 2026
https://github.com/cachemeifyoucan updated https://github.com/llvm/llvm-project/pull/242436
>From 36ca7c7f4439f02f7f0d160f59e3911611c9effe Mon Sep 17 00:00:00 2001
From: Steven Wu <stevenwu at apple.com>
Date: Fri, 13 Nov 2026 10:31:10 -0800
Subject: [PATCH] [dsymutil] Support compilation-prefix-map.json from
compilation caching
Compilation caching can prefix map the paths that the compiler sees, so
that cache keys don't depend on where the build happens, for example
/path/to/project becomes /^src. The build system records the mapping in
a compilation-prefix-map.json file in a parent directory of the object
files, as a JSON object from each remapped prefix to the original one.
dsymutil didn't support it, so it used the remapped paths from the
debug info as is: it failed to copy the Swift interfaces that the debug
info refers to, didn't recognize interfaces from the SDK or the
toolchain that it should skip, and wrote the remapped paths into the
dSYM.
Load the compilation-prefix-map.json closest to each object file, and
use it to find Swift interfaces and Clang modules, and to restore the
original paths in the path attributes and line tables of the linked
debug info. Clang modules use the map of the object file that refers to
them. -object-prefix-map still only applies to looking up Clang
modules.
Also check whether Swift interface paths are absolute on either POSIX
or Windows, like other paths from the debug info, which can come from
another platform. On Windows, a POSIX absolute path was combined with
the compilation directory.
---
.../Classic/DWARFLinkerCompileUnit.h | 12 +-
.../llvm/DWARFLinker/Classic/DWARFStreamer.h | 16 +-
llvm/include/llvm/DWARFLinker/DWARFFile.h | 14 +
llvm/include/llvm/DWARFLinker/Utils.h | 20 +
llvm/lib/DWARFLinker/Classic/DWARFLinker.cpp | 50 ++-
.../lib/DWARFLinker/Classic/DWARFStreamer.cpp | 39 +-
.../Parallel/DIEAttributeCloner.cpp | 9 +
.../Parallel/DWARFLinkerCompileUnit.cpp | 15 +-
.../Parallel/DWARFLinkerCompileUnit.h | 4 +
.../DWARFLinker/Parallel/DWARFLinkerImpl.cpp | 30 +-
.../DWARFLinker/Parallel/DWARFLinkerUnit.h | 4 +
.../Parallel/DebugLineSectionEmitter.h | 15 +-
.../dsymutil/Inputs/compilation-prefix-map.s | 349 ++++++++++++++++++
.../Inputs/swift-interface-prefix-map.s | 262 +++++++++++++
.../dsymutil/X86/compilation-prefix-map.test | 58 +++
.../X86/swift-interface-prefix-map.test | 70 ++++
llvm/tools/dsymutil/DwarfLinkerForBinary.cpp | 52 +++
llvm/tools/dsymutil/DwarfLinkerForBinary.h | 10 +
18 files changed, 972 insertions(+), 57 deletions(-)
create mode 100644 llvm/test/tools/dsymutil/Inputs/compilation-prefix-map.s
create mode 100644 llvm/test/tools/dsymutil/Inputs/swift-interface-prefix-map.s
create mode 100644 llvm/test/tools/dsymutil/X86/compilation-prefix-map.test
create mode 100644 llvm/test/tools/dsymutil/X86/swift-interface-prefix-map.test
diff --git a/llvm/include/llvm/DWARFLinker/Classic/DWARFLinkerCompileUnit.h b/llvm/include/llvm/DWARFLinker/Classic/DWARFLinkerCompileUnit.h
index 538572338dc8bc..4406791bd878f3 100644
--- a/llvm/include/llvm/DWARFLinker/Classic/DWARFLinkerCompileUnit.h
+++ b/llvm/include/llvm/DWARFLinker/Classic/DWARFLinkerCompileUnit.h
@@ -12,6 +12,7 @@
#include "llvm/ADT/AddressRanges.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/CodeGen/DIE.h"
+#include "llvm/DWARFLinker/DWARFFile.h"
#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
#include "llvm/Support/Compiler.h"
#include <optional>
@@ -107,9 +108,10 @@ class CompileUnit {
#endif // if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
};
- CompileUnit(DWARFUnit &OrigUnit, unsigned ID, bool CanUseODR,
- StringRef ClangModuleName)
- : OrigUnit(OrigUnit), ID(ID), ClangModuleName(ClangModuleName) {
+ CompileUnit(DWARFUnit &OrigUnit, const DWARFFile &File, unsigned ID,
+ bool CanUseODR, StringRef ClangModuleName)
+ : OrigUnit(OrigUnit), File(File), ID(ID),
+ ClangModuleName(ClangModuleName) {
Info.resize(OrigUnit.getNumDIEs());
auto CUDie = OrigUnit.getUnitDIE(false);
@@ -129,6 +131,9 @@ class CompileUnit {
DWARFUnit &getOrigUnit() const { return OrigUnit; }
+ /// Returns the input file containing the original unit.
+ const DWARFFile &getContainingFile() const { return File; }
+
unsigned getUniqueID() const { return ID; }
void createOutputDIE() {
@@ -290,6 +295,7 @@ class CompileUnit {
private:
DWARFUnit &OrigUnit;
+ const DWARFFile &File;
unsigned ID;
std::vector<DIEInfo> Info; ///< DIE info indexed by DIE index.
std::optional<BasicDIEUnit> NewUnit;
diff --git a/llvm/include/llvm/DWARFLinker/Classic/DWARFStreamer.h b/llvm/include/llvm/DWARFLinker/Classic/DWARFStreamer.h
index f5af3790282a47..33c6ef53b9e1bd 100644
--- a/llvm/include/llvm/DWARFLinker/Classic/DWARFStreamer.h
+++ b/llvm/include/llvm/DWARFLinker/Classic/DWARFStreamer.h
@@ -263,21 +263,27 @@ class LLVM_ABI DwarfStreamer : public DwarfEmitter {
/// \defgroup Line table emission
/// @{
void emitLineTablePrologue(const DWARFDebugLine::Prologue &P,
+ const CompileUnit &Unit,
OffsetsStringPool &DebugStrPool,
OffsetsStringPool &DebugLineStrPool);
+ /// Emit \p String. If \p PathUnit is provided, \p String is a path from
+ /// the input line table of \p PathUnit and the prefix map of its input
+ /// file is undone.
void emitLineTableString(const DWARFDebugLine::Prologue &P,
const DWARFFormValue &String,
OffsetsStringPool &DebugStrPool,
- OffsetsStringPool &DebugLineStrPool);
+ OffsetsStringPool &DebugLineStrPool,
+ const CompileUnit *PathUnit = nullptr);
void emitLineTableProloguePayload(const DWARFDebugLine::Prologue &P,
+ const CompileUnit &Unit,
OffsetsStringPool &DebugStrPool,
OffsetsStringPool &DebugLineStrPool);
void emitLineTablePrologueV2IncludeAndFileTable(
- const DWARFDebugLine::Prologue &P, OffsetsStringPool &DebugStrPool,
- OffsetsStringPool &DebugLineStrPool);
+ const DWARFDebugLine::Prologue &P, const CompileUnit &Unit,
+ OffsetsStringPool &DebugStrPool, OffsetsStringPool &DebugLineStrPool);
void emitLineTablePrologueV5IncludeAndFileTable(
- const DWARFDebugLine::Prologue &P, OffsetsStringPool &DebugStrPool,
- OffsetsStringPool &DebugLineStrPool);
+ const DWARFDebugLine::Prologue &P, const CompileUnit &Unit,
+ OffsetsStringPool &DebugStrPool, OffsetsStringPool &DebugLineStrPool);
void emitLineTableRows(const DWARFDebugLine::LineTable &LineTable,
MCSymbol *LineEndSym, unsigned AddressByteSize,
std::vector<uint64_t> *RowOffsets = nullptr);
diff --git a/llvm/include/llvm/DWARFLinker/DWARFFile.h b/llvm/include/llvm/DWARFLinker/DWARFFile.h
index c1d0fd87c7d711..e299794347d08e 100644
--- a/llvm/include/llvm/DWARFLinker/DWARFFile.h
+++ b/llvm/include/llvm/DWARFLinker/DWARFFile.h
@@ -12,8 +12,10 @@
#include "AddressesMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
+#include "llvm/Support/PrefixMapper.h"
#include <functional>
#include <memory>
+#include <string>
namespace llvm {
namespace dwarf_linker {
@@ -44,6 +46,18 @@ class DWARFFile {
/// Callback to the module keeping object file to unload.
UnloadCallbackTy UnloadFunc;
+ /// Maps the path prefixes that the compiler remapped in this file's debug
+ /// info back to the original ones.
+ std::shared_ptr<PrefixMapper> PrefixMap;
+
+ /// \returns \p Path, a path from this file's debug info, with the prefix
+ /// mapping undone. This is used both to locate build artifacts referenced by
+ /// the debug info, such as Swift interfaces and Clang modules, and to
+ /// restore the original paths in the linked debug info.
+ std::string remapPath(StringRef Path) const {
+ return PrefixMap ? PrefixMap->mapToString(Path) : Path.str();
+ }
+
/// Unloads object file and corresponding AddressesMap and Dwarf Context.
void unload() {
Addresses.reset();
diff --git a/llvm/include/llvm/DWARFLinker/Utils.h b/llvm/include/llvm/DWARFLinker/Utils.h
index 68af93bd674e58..274a8c7f219f17 100644
--- a/llvm/include/llvm/DWARFLinker/Utils.h
+++ b/llvm/include/llvm/DWARFLinker/Utils.h
@@ -145,6 +145,26 @@ inline bool isInToolchainDir(StringRef Path) {
return false;
}
+/// \returns true if the attribute \p Attr of a DIE with tag \p Tag holds a
+/// file system path.
+inline bool isPathAttribute(dwarf::Tag Tag, dwarf::Attribute Attr) {
+ switch (Attr) {
+ case dwarf::DW_AT_comp_dir:
+ case dwarf::DW_AT_dwo_name:
+ case dwarf::DW_AT_GNU_dwo_name:
+ case dwarf::DW_AT_LLVM_include_path:
+ case dwarf::DW_AT_LLVM_sysroot:
+ case dwarf::DW_AT_LLVM_apinotes:
+ return true;
+ case dwarf::DW_AT_name:
+ return Tag == dwarf::DW_TAG_compile_unit ||
+ Tag == dwarf::DW_TAG_partial_unit ||
+ Tag == dwarf::DW_TAG_skeleton_unit;
+ default:
+ return false;
+ }
+}
+
inline bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) {
// Debug info can contain paths from any OS, not necessarily
// an OS we're currently running on. Moreover different compilation units can
diff --git a/llvm/lib/DWARFLinker/Classic/DWARFLinker.cpp b/llvm/lib/DWARFLinker/Classic/DWARFLinker.cpp
index 8bdf05456b9191..3d56ec5639d9c5 100644
--- a/llvm/lib/DWARFLinker/Classic/DWARFLinker.cpp
+++ b/llvm/lib/DWARFLinker/Classic/DWARFLinker.cpp
@@ -243,10 +243,12 @@ bool DWARFLinker::DIECloner::getDIENames(
return Info.Name || Info.MangledName;
}
-/// Resolve the relative path to a build artifact referenced by DWARF by
-/// applying DW_AT_comp_dir.
-static void resolveRelativeObjectPath(SmallVectorImpl<char> &Buf, DWARFDie CU) {
- sys::path::append(Buf, dwarf::toString(CU.find(dwarf::DW_AT_comp_dir), ""));
+/// Resolve the relative path to a build artifact referenced by DWARF of \p File
+/// by applying DW_AT_comp_dir.
+static void resolveRelativeObjectPath(SmallVectorImpl<char> &Buf, DWARFDie CU,
+ const DWARFFile &File) {
+ sys::path::append(
+ Buf, File.remapPath(dwarf::toStringRef(CU.find(dwarf::DW_AT_comp_dir))));
}
/// Collect references to parseable Swift interfaces in imported
@@ -264,10 +266,16 @@ static void analyzeImportedModule(
StringRef Path = dwarf::toStringRef(DIE.find(dwarf::DW_AT_LLVM_include_path));
if (!Path.ends_with(".swiftinterface"))
return;
+ // Undo prefix mapping before comparing against the SDK and toolchain.
+ const DWARFFile &File = CU.getContainingFile();
+ std::string RemappedPath = File.remapPath(Path);
+ Path = RemappedPath;
// Don't track interfaces that are part of the SDK.
StringRef SysRoot = dwarf::toStringRef(DIE.find(dwarf::DW_AT_LLVM_sysroot));
if (SysRoot.empty())
SysRoot = CU.getSysRoot();
+ std::string RemappedSysRoot = File.remapPath(SysRoot);
+ SysRoot = RemappedSysRoot;
if (!SysRoot.empty() && Path.starts_with(SysRoot))
return;
// Don't track interfaces that are part of the toolchain.
@@ -285,8 +293,8 @@ static void analyzeImportedModule(
// The prepend path is applied later when copying.
DWARFDie CUDie = CU.getOrigUnit().getUnitDIE();
SmallString<128> ResolvedPath;
- if (sys::path::is_relative(Path))
- resolveRelativeObjectPath(ResolvedPath, CUDie);
+ if (!isPathAbsoluteOnWindowsOrPosix(Path))
+ resolveRelativeObjectPath(ResolvedPath, CUDie, File);
sys::path::append(ResolvedPath, Path);
if (!Entry.empty() && Entry != ResolvedPath)
ReportWarning(Twine("Conflicting parseable interfaces for Swift Module ") +
@@ -1130,6 +1138,11 @@ unsigned DWARFLinker::DIECloner::cloneStringAttribute(DIE &Die,
std::optional<const char *> String = dwarf::toString(Val);
if (!String)
return 0;
+ std::string RemappedPath;
+ if (ObjFile.PrefixMap && isPathAttribute(Die.getTag(), AttrSpec.Attr)) {
+ RemappedPath = ObjFile.remapPath(*String);
+ String = RemappedPath.c_str();
+ }
DwarfStringPoolEntryRef StringEntry;
if (AttrSpec.Form == dwarf::DW_FORM_line_strp) {
StringEntry = DebugLineStrPool.getEntry(*String);
@@ -2728,7 +2741,8 @@ remapPath(StringRef Path,
static std::string
getPCMFile(const DWARFDie &CUDie,
- const DWARFLinkerBase::ObjectPrefixMapTy *ObjectPrefixMap) {
+ const DWARFLinkerBase::ObjectPrefixMapTy *ObjectPrefixMap,
+ const DWARFFile &File) {
std::string PCMFile = dwarf::toString(
CUDie.find({dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}), "");
@@ -2738,7 +2752,7 @@ getPCMFile(const DWARFDie &CUDie,
if (ObjectPrefixMap)
PCMFile = remapPath(PCMFile, *ObjectPrefixMap);
- return PCMFile;
+ return File.remapPath(PCMFile);
}
std::pair<bool, bool> DWARFLinker::isClangModuleRef(const DWARFDie &CUDie,
@@ -2788,7 +2802,8 @@ bool DWARFLinker::registerModuleReference(const DWARFDie &CUDie,
ObjFileLoaderTy Loader,
CompileUnitHandlerTy OnCUDieLoaded,
unsigned Indent) {
- std::string PCMFile = getPCMFile(CUDie, Options.ObjectPrefixMap);
+ std::string PCMFile =
+ getPCMFile(CUDie, Options.ObjectPrefixMap, Context.File);
std::pair<bool, bool> IsClangModuleRef =
isClangModuleRef(CUDie, PCMFile, Context, Indent, false);
@@ -2823,7 +2838,7 @@ Error DWARFLinker::loadClangModule(
/// Using a SmallString<0> because loadClangModule() is recursive.
SmallString<0> Path(Options.PrependPath);
if (sys::path::is_relative(PCMFile))
- resolveRelativeObjectPath(Path, CUDie);
+ resolveRelativeObjectPath(Path, CUDie, Context.File);
sys::path::append(Path, PCMFile);
// Don't use the cached binary holder because we have no thread-safety
// guarantee and the lifetime is limited.
@@ -2838,6 +2853,11 @@ Error DWARFLinker::loadClangModule(
if (!ErrOrObj)
return Error::success();
+ // The module was built by the same build as the object file referencing it,
+ // so the same prefix map applies.
+ if (!ErrOrObj->PrefixMap)
+ ErrOrObj->PrefixMap = Context.File.PrefixMap;
+
std::unique_ptr<CompileUnit> Unit;
for (const auto &CU : ErrOrObj->Dwarf->compile_units()) {
OnCUDieLoaded(*CU);
@@ -2870,8 +2890,8 @@ Error DWARFLinker::loadClangModule(
}
// Add this module.
- Unit = std::make_unique<CompileUnit>(*CU, UniqueUnitID++, !Options.NoODR,
- ModuleName);
+ Unit = std::make_unique<CompileUnit>(*CU, *ErrOrObj, UniqueUnitID++,
+ !Options.NoODR, ModuleName);
}
}
@@ -3111,12 +3131,14 @@ Error DWARFLinker::link() {
// Previously we only extracted the unit DIEs. We need the full debug info
// now.
auto CUDie = CU->getUnitDIE(/*ExtractUnitDIEOnly=*/false);
- std::string PCMFile = getPCMFile(CUDie, Options.ObjectPrefixMap);
+ std::string PCMFile =
+ getPCMFile(CUDie, Options.ObjectPrefixMap, Context.File);
if (!CUDie || LLVM_UNLIKELY(Options.Update) ||
!isClangModuleRef(CUDie, PCMFile, Context, 0, true).first) {
Context.CompileUnits.push_back(std::make_unique<CompileUnit>(
- *CU, UniqueUnitID++, !Options.NoODR && !Options.Update, ""));
+ *CU, Context.File, UniqueUnitID++,
+ !Options.NoODR && !Options.Update, ""));
}
}
diff --git a/llvm/lib/DWARFLinker/Classic/DWARFStreamer.cpp b/llvm/lib/DWARFLinker/Classic/DWARFStreamer.cpp
index 81544c4dff54f9..26c79eda14efbd 100644
--- a/llvm/lib/DWARFLinker/Classic/DWARFStreamer.cpp
+++ b/llvm/lib/DWARFLinker/Classic/DWARFStreamer.cpp
@@ -849,7 +849,8 @@ void DwarfStreamer::emitLineTableForUnit(
Asm->OutStreamer->emitLabel(LineStartSym);
// Emit prologue.
- emitLineTablePrologue(LineTable.Prologue, DebugStrPool, DebugLineStrPool);
+ emitLineTablePrologue(LineTable.Prologue, Unit, DebugStrPool,
+ DebugLineStrPool);
// Emit rows.
emitLineTableRows(LineTable, LineEndSym,
@@ -857,6 +858,7 @@ void DwarfStreamer::emitLineTableForUnit(
}
void DwarfStreamer::emitLineTablePrologue(const DWARFDebugLine::Prologue &P,
+ const CompileUnit &Unit,
OffsetsStringPool &DebugStrPool,
OffsetsStringPool &DebugLineStrPool) {
MCSymbol *PrologueStartSym = MC->createTempSymbol();
@@ -880,16 +882,16 @@ void DwarfStreamer::emitLineTablePrologue(const DWARFDebugLine::Prologue &P,
LineSectionSize);
Asm->OutStreamer->emitLabel(PrologueStartSym);
- emitLineTableProloguePayload(P, DebugStrPool, DebugLineStrPool);
+ emitLineTableProloguePayload(P, Unit, DebugStrPool, DebugLineStrPool);
Asm->OutStreamer->emitLabel(PrologueEndSym);
}
void DwarfStreamer::emitLineTablePrologueV2IncludeAndFileTable(
- const DWARFDebugLine::Prologue &P, OffsetsStringPool &DebugStrPool,
- OffsetsStringPool &DebugLineStrPool) {
+ const DWARFDebugLine::Prologue &P, const CompileUnit &Unit,
+ OffsetsStringPool &DebugStrPool, OffsetsStringPool &DebugLineStrPool) {
// include_directories (sequence of path names).
for (const DWARFFormValue &Include : P.IncludeDirectories)
- emitLineTableString(P, Include, DebugStrPool, DebugLineStrPool);
+ emitLineTableString(P, Include, DebugStrPool, DebugLineStrPool, &Unit);
// The last entry is followed by a single null byte.
MS->emitInt8(0);
LineSectionSize += 1;
@@ -898,7 +900,7 @@ void DwarfStreamer::emitLineTablePrologueV2IncludeAndFileTable(
for (const DWARFDebugLine::FileNameEntry &File : P.FileNames) {
// A null-terminated string containing the full or relative path name of a
// source file.
- emitLineTableString(P, File.Name, DebugStrPool, DebugLineStrPool);
+ emitLineTableString(P, File.Name, DebugStrPool, DebugLineStrPool, &Unit);
// An unsigned LEB128 number representing the directory index of a directory
// in the include_directories section.
LineSectionSize += MS->emitULEB128IntValue(File.DirIdx);
@@ -915,8 +917,8 @@ void DwarfStreamer::emitLineTablePrologueV2IncludeAndFileTable(
}
void DwarfStreamer::emitLineTablePrologueV5IncludeAndFileTable(
- const DWARFDebugLine::Prologue &P, OffsetsStringPool &DebugStrPool,
- OffsetsStringPool &DebugLineStrPool) {
+ const DWARFDebugLine::Prologue &P, const CompileUnit &Unit,
+ OffsetsStringPool &DebugStrPool, OffsetsStringPool &DebugLineStrPool) {
if (P.IncludeDirectories.empty()) {
// directory_entry_format_count(ubyte).
MS->emitInt8(0);
@@ -936,7 +938,7 @@ void DwarfStreamer::emitLineTablePrologueV5IncludeAndFileTable(
LineSectionSize += MS->emitULEB128IntValue(P.IncludeDirectories.size());
// directories (sequence of directory names).
for (auto Include : P.IncludeDirectories)
- emitLineTableString(P, Include, DebugStrPool, DebugLineStrPool);
+ emitLineTableString(P, Include, DebugStrPool, DebugLineStrPool, &Unit);
bool HasChecksums = P.ContentTypes.HasMD5;
bool HasInlineSources = P.ContentTypes.HasSource;
@@ -974,7 +976,7 @@ void DwarfStreamer::emitLineTablePrologueV5IncludeAndFileTable(
// file_names (sequence of file name entries).
for (auto File : P.FileNames) {
- emitLineTableString(P, File.Name, DebugStrPool, DebugLineStrPool);
+ emitLineTableString(P, File.Name, DebugStrPool, DebugLineStrPool, &Unit);
LineSectionSize += MS->emitULEB128IntValue(File.DirIdx);
if (HasChecksums) {
MS->emitBinaryData(
@@ -990,13 +992,20 @@ void DwarfStreamer::emitLineTablePrologueV5IncludeAndFileTable(
void DwarfStreamer::emitLineTableString(const DWARFDebugLine::Prologue &P,
const DWARFFormValue &String,
OffsetsStringPool &DebugStrPool,
- OffsetsStringPool &DebugLineStrPool) {
+ OffsetsStringPool &DebugLineStrPool,
+ const CompileUnit *PathUnit) {
std::optional<const char *> StringVal = dwarf::toString(String);
if (!StringVal) {
warn("Cann't read string from line table.");
return;
}
+ std::string RemappedPath;
+ if (PathUnit && PathUnit->getContainingFile().PrefixMap) {
+ RemappedPath = PathUnit->getContainingFile().remapPath(*StringVal);
+ StringVal = RemappedPath.c_str();
+ }
+
switch (String.getForm()) {
case dwarf::DW_FORM_string: {
StringRef Str = *StringVal;
@@ -1020,8 +1029,8 @@ void DwarfStreamer::emitLineTableString(const DWARFDebugLine::Prologue &P,
}
void DwarfStreamer::emitLineTableProloguePayload(
- const DWARFDebugLine::Prologue &P, OffsetsStringPool &DebugStrPool,
- OffsetsStringPool &DebugLineStrPool) {
+ const DWARFDebugLine::Prologue &P, const CompileUnit &Unit,
+ OffsetsStringPool &DebugStrPool, OffsetsStringPool &DebugLineStrPool) {
// minimum_instruction_length (ubyte).
MS->emitInt8(P.MinInstLength);
LineSectionSize += 1;
@@ -1050,10 +1059,10 @@ void DwarfStreamer::emitLineTableProloguePayload(
}
if (P.FormParams.Version < 5)
- emitLineTablePrologueV2IncludeAndFileTable(P, DebugStrPool,
+ emitLineTablePrologueV2IncludeAndFileTable(P, Unit, DebugStrPool,
DebugLineStrPool);
else
- emitLineTablePrologueV5IncludeAndFileTable(P, DebugStrPool,
+ emitLineTablePrologueV5IncludeAndFileTable(P, Unit, DebugStrPool,
DebugLineStrPool);
}
diff --git a/llvm/lib/DWARFLinker/Parallel/DIEAttributeCloner.cpp b/llvm/lib/DWARFLinker/Parallel/DIEAttributeCloner.cpp
index c8a83ba7021572..5c513d0d5b56f0 100644
--- a/llvm/lib/DWARFLinker/Parallel/DIEAttributeCloner.cpp
+++ b/llvm/lib/DWARFLinker/Parallel/DIEAttributeCloner.cpp
@@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "DIEAttributeCloner.h"
+#include "llvm/DWARFLinker/Utils.h"
#include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h"
using namespace llvm;
@@ -207,6 +208,14 @@ size_t DIEAttributeCloner::cloneStringAttr(
return 0;
}
+ const DWARFFile &File = InUnit.getContainingFile();
+ std::string RemappedPath;
+ if (File.PrefixMap &&
+ isPathAttribute(InputDieEntry->getTag(), AttrSpec.Attr)) {
+ RemappedPath = File.remapPath(*String);
+ String = RemappedPath.c_str();
+ }
+
StringEntry *StringInPool =
InUnit.getGlobalData().getStringPool().insert(*String).first;
diff --git a/llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.cpp b/llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.cpp
index 59a67faaf6d157..7b24403af0fdd4 100644
--- a/llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.cpp
+++ b/llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.cpp
@@ -348,11 +348,16 @@ void CompileUnit::analyzeImportedModule(const DWARFDebugInfoEntry *DieEntry) {
dwarf::toStringRef(find(DieEntry, dwarf::DW_AT_LLVM_include_path));
if (!Path.ends_with(".swiftinterface"))
return;
+ // Undo prefix mapping before comparing against the SDK and toolchain.
+ std::string RemappedPath = File.remapPath(Path);
+ Path = RemappedPath;
// Don't track interfaces that are part of the SDK.
StringRef SysRoot =
dwarf::toStringRef(find(DieEntry, dwarf::DW_AT_LLVM_sysroot));
if (SysRoot.empty())
SysRoot = getSysRoot();
+ std::string RemappedSysRoot = File.remapPath(SysRoot);
+ SysRoot = RemappedSysRoot;
if (!SysRoot.empty() && Path.starts_with(SysRoot))
return;
// Don't track interfaces that are part of the toolchain.
@@ -371,10 +376,9 @@ void CompileUnit::analyzeImportedModule(const DWARFDebugInfoEntry *DieEntry) {
// The prepend path is applied later when copying.
SmallString<128> ResolvedPath;
- if (sys::path::is_relative(Path))
- sys::path::append(
- ResolvedPath,
- dwarf::toString(getUnitDIE().find(dwarf::DW_AT_comp_dir), ""));
+ if (!isPathAbsoluteOnWindowsOrPosix(Path))
+ ResolvedPath = File.remapPath(
+ dwarf::toStringRef(getUnitDIE().find(dwarf::DW_AT_comp_dir)));
sys::path::append(ResolvedPath, Path);
// Stage the entry. It will be merged into the shared
@@ -1965,6 +1969,7 @@ CompileUnit::getDirAndFilenameFromLineTable(uint64_t FileIdx) {
std::string FileName = *Name;
if (isPathAbsoluteOnWindowsOrPosix(FileName)) {
+ FileName = File.remapPath(FileName);
FileNamesCache::iterator FileData =
FileNames
.insert({FileIdx,
@@ -2020,7 +2025,7 @@ CompileUnit::getDirAndFilenameFromLineTable(uint64_t FileIdx) {
FileNames
.insert({FileIdx,
std::make_unique<std::pair<std::string, std::string>>(
- std::string(FilePath), std::move(FileName))})
+ File.remapPath(FilePath), std::move(FileName))})
.first;
return {{StringRef(FileData->second->first),
StringRef(FileData->second->second)}};
diff --git a/llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.h b/llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.h
index c8c1b8822aa642..84308dd7b19f01 100644
--- a/llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.h
+++ b/llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.h
@@ -114,6 +114,10 @@ class alignas(8) CompileUnit : public DwarfUnit {
/// Returns DWARFFile containing this compile unit.
const DWARFFile &getContainingFile() const { return File; }
+ std::string remapPath(StringRef Path) const override {
+ return File.remapPath(Path);
+ }
+
/// Appends the names of the DW_TAG_module enclosing \p DieEntry, outermost
/// first. Returns false when one of them has no name making the module
/// unidentifiable across units.
diff --git a/llvm/lib/DWARFLinker/Parallel/DWARFLinkerImpl.cpp b/llvm/lib/DWARFLinker/Parallel/DWARFLinkerImpl.cpp
index f0fd044b09e762..42f1787f6ec02a 100644
--- a/llvm/lib/DWARFLinker/Parallel/DWARFLinkerImpl.cpp
+++ b/llvm/lib/DWARFLinker/Parallel/DWARFLinkerImpl.cpp
@@ -289,10 +289,12 @@ Error DWARFLinkerImpl::validateAndUpdateOptions() {
return Error::success();
}
-/// Resolve the relative path to a build artifact referenced by DWARF by
-/// applying DW_AT_comp_dir.
-static void resolveRelativeObjectPath(SmallVectorImpl<char> &Buf, DWARFDie CU) {
- sys::path::append(Buf, dwarf::toString(CU.find(dwarf::DW_AT_comp_dir), ""));
+/// Resolve the relative path to a build artifact referenced by DWARF of \p File
+/// by applying DW_AT_comp_dir.
+static void resolveRelativeObjectPath(SmallVectorImpl<char> &Buf, DWARFDie CU,
+ const DWARFFile &File) {
+ sys::path::append(
+ Buf, File.remapPath(dwarf::toStringRef(CU.find(dwarf::DW_AT_comp_dir))));
}
static uint64_t getDwoId(const DWARFDie &CUDie) {
@@ -317,7 +319,8 @@ remapPath(StringRef Path,
}
static std::string getPCMFile(const DWARFDie &CUDie,
- DWARFLinker::ObjectPrefixMapTy *ObjectPrefixMap) {
+ DWARFLinker::ObjectPrefixMapTy *ObjectPrefixMap,
+ const DWARFFile &File) {
std::string PCMFile = dwarf::toString(
CUDie.find({dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}), "");
@@ -327,7 +330,7 @@ static std::string getPCMFile(const DWARFDie &CUDie,
if (ObjectPrefixMap)
PCMFile = remapPath(PCMFile, *ObjectPrefixMap);
- return PCMFile;
+ return File.remapPath(PCMFile);
}
std::pair<bool, bool> DWARFLinkerImpl::LinkContext::isClangModuleRef(
@@ -379,8 +382,8 @@ std::pair<bool, bool> DWARFLinkerImpl::LinkContext::isClangModuleRef(
bool DWARFLinkerImpl::LinkContext::registerModuleReference(
const DWARFDie &CUDie, ObjFileLoaderTy Loader,
CompileUnitHandlerTy OnCUDieLoaded, unsigned Indent) {
- std::string PCMFile =
- getPCMFile(CUDie, GlobalData.getOptions().ObjectPrefixMap);
+ std::string PCMFile = getPCMFile(
+ CUDie, GlobalData.getOptions().ObjectPrefixMap, InputDWARFFile);
std::pair<bool, bool> IsClangModuleRef =
isClangModuleRef(CUDie, PCMFile, Indent, false);
@@ -415,7 +418,7 @@ Error DWARFLinkerImpl::LinkContext::loadClangModule(
/// Using a SmallString<0> because loadClangModule() is recursive.
SmallString<0> Path(GlobalData.getOptions().PrependPath);
if (sys::path::is_relative(PCMFile))
- resolveRelativeObjectPath(Path, CUDie);
+ resolveRelativeObjectPath(Path, CUDie, InputDWARFFile);
sys::path::append(Path, PCMFile);
// Don't use the cached binary holder because we have no thread-safety
// guarantee and the lifetime is limited.
@@ -430,6 +433,11 @@ Error DWARFLinkerImpl::LinkContext::loadClangModule(
if (!ErrOrObj)
return Error::success();
+ // The module was built by the same build as the object file referencing it,
+ // so the same prefix map applies.
+ if (!ErrOrObj->PrefixMap)
+ ErrOrObj->PrefixMap = InputDWARFFile.PrefixMap;
+
std::unique_ptr<CompileUnit> Unit;
for (const auto &CU : ErrOrObj->Dwarf->compile_units()) {
OnCUDieLoaded(*CU);
@@ -521,8 +529,8 @@ Error DWARFLinkerImpl::LinkContext::link(TypeUnit *ArtificialTypeUnit) {
for (const auto &OrigCU : InputDWARFFile.Dwarf->compile_units()) {
// Load only unit DIE at this stage.
auto CUDie = OrigCU->getUnitDIE();
- std::string PCMFile =
- getPCMFile(CUDie, GlobalData.getOptions().ObjectPrefixMap);
+ std::string PCMFile = getPCMFile(
+ CUDie, GlobalData.getOptions().ObjectPrefixMap, InputDWARFFile);
// The !isClangModuleRef condition effectively skips over fully resolved
// skeleton units.
diff --git a/llvm/lib/DWARFLinker/Parallel/DWARFLinkerUnit.h b/llvm/lib/DWARFLinker/Parallel/DWARFLinkerUnit.h
index 2bb990593b56a0..7eac306f1adac6 100644
--- a/llvm/lib/DWARFLinker/Parallel/DWARFLinkerUnit.h
+++ b/llvm/lib/DWARFLinker/Parallel/DWARFLinkerUnit.h
@@ -171,6 +171,10 @@ class DwarfUnit : public OutputSections {
/// @}
+ /// \returns \p Path, a path from the input debug info of this unit, with
+ /// the prefix mapping of the input file undone.
+ virtual std::string remapPath(StringRef Path) const { return Path.str(); }
+
/// Returns index(inside .debug_str_offsets) of specified string.
virtual uint64_t getDebugStrIndex(const StringEntry *String) {
return DebugStringIndexMap.getValueIndex(String);
diff --git a/llvm/lib/DWARFLinker/Parallel/DebugLineSectionEmitter.h b/llvm/lib/DWARFLinker/Parallel/DebugLineSectionEmitter.h
index 2b71776ff2e9cd..21e057442a5053 100644
--- a/llvm/lib/DWARFLinker/Parallel/DebugLineSectionEmitter.h
+++ b/llvm/lib/DWARFLinker/Parallel/DebugLineSectionEmitter.h
@@ -135,7 +135,7 @@ class DebugLineSectionEmitter {
return;
}
- Section.emitString(Include.getForm(), *IncludeStr);
+ emitPath(Section, Include.getForm(), *IncludeStr);
}
// The last entry is followed by a single null byte.
Section.emitIntVal(0, 1);
@@ -150,7 +150,7 @@ class DebugLineSectionEmitter {
// A null-terminated string containing the full or relative path name of a
// source file.
- Section.emitString(File.Name.getForm(), *FileNameStr);
+ emitPath(Section, File.Name.getForm(), *FileNameStr);
// An unsigned LEB128 number representing the directory index of a
// directory in the include_directories section.
@@ -191,7 +191,7 @@ class DebugLineSectionEmitter {
return;
}
- Section.emitString(Include.getForm(), *IncludeStr);
+ emitPath(Section, Include.getForm(), *IncludeStr);
}
bool HasChecksums = P.ContentTypes.HasMD5;
@@ -242,7 +242,7 @@ class DebugLineSectionEmitter {
// A null-terminated string containing the full or relative path name of a
// source file.
- Section.emitString(FileNameForm, *FileNameStr);
+ emitPath(Section, FileNameForm, *FileNameStr);
encodeULEB128(File.DirIdx, Section.OS);
if (HasChecksums) {
@@ -435,6 +435,13 @@ class DebugLineSectionEmitter {
}
}
+ /// Emit \p Path, a path from the input line table, as a string of form
+ /// \p Form.
+ void emitPath(SectionDescriptor &Section, dwarf::Form Form,
+ const char *Path) {
+ Section.emitString(Form, U.remapPath(Path).c_str());
+ }
+
Triple TheTriple;
DwarfUnit &U;
diff --git a/llvm/test/tools/dsymutil/Inputs/compilation-prefix-map.s b/llvm/test/tools/dsymutil/Inputs/compilation-prefix-map.s
new file mode 100644
index 00000000000000..097eed2fdcf908
--- /dev/null
+++ b/llvm/test/tools/dsymutil/Inputs/compilation-prefix-map.s
@@ -0,0 +1,349 @@
+##; Generated with llc -O0 from the following IR, which mimics a C++ program
+##; compiled with prefix mapping:
+##;
+##; target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
+##; target triple = "x86_64-apple-macosx10.15.0"
+##;
+##; %struct.Foo = type { i32 }
+##;
+##; define i32 @main() !dbg !20 {
+##; entry:
+##; %f = alloca %struct.Foo, align 4
+##; #dbg_declare(ptr %f, !24, !DIExpression(), !23)
+##; ret i32 0, !dbg !23
+##; }
+##;
+##; !llvm.dbg.cu = !{!0}
+##; !llvm.module.flags = !{!30, !31}
+##;
+##; !0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, sysroot: "/^sdk", sdk: "MacOSX.sdk")
+##; !1 = !DIFile(filename: "main.cpp", directory: "/^src")
+##; !10 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "Foo", file: !11, line: 1, size: 32, flags: DIFlagTypePassByValue, elements: !12, identifier: "_ZTS3Foo")
+##; !11 = !DIFile(filename: "foo.h", directory: "/^src/include")
+##; !12 = !{!13}
+##; !13 = !DIDerivedType(tag: DW_TAG_member, name: "x", scope: !10, file: !11, line: 1, baseType: !14, size: 32)
+##; !14 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+##; !20 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 3, type: !21, scopeLine: 3, spFlags: DISPFlagDefinition, unit: !0)
+##; !21 = !DISubroutineType(types: !22)
+##; !22 = !{!14}
+##; !23 = !DILocation(line: 4, column: 7, scope: !20)
+##; !24 = !DILocalVariable(name: "f", scope: !20, file: !1, line: 4, type: !10)
+##; !30 = !{i32 7, !"Dwarf Version", i32 5}
+##; !31 = !{i32 2, !"Debug Info Version", i32 3}
+
+ .build_version macos, 10, 15
+ .section __TEXT,__text,regular,pure_instructions
+ .att_syntax
+ .globl _main ## -- Begin function main
+ .p2align 4
+_main: ## @main
+Lfunc_begin0:
+ .file 0 "/^src" "main.cpp"
+ .cfi_startproc
+## %bb.0: ## %entry
+ .loc 0 4 7 prologue_end ## main.cpp:4:7
+ xorl %eax, %eax
+ retq
+Ltmp0:
+Lfunc_end0:
+ .cfi_endproc
+ ## -- End function
+ .file 1 "/^src/include" "foo.h"
+ .section __DWARF,__debug_abbrev,regular,debug
+Lsection_abbrev:
+ .byte 1 ## Abbreviation Code
+ .byte 17 ## DW_TAG_compile_unit
+ .byte 1 ## DW_CHILDREN_yes
+ .byte 37 ## DW_AT_producer
+ .byte 37 ## DW_FORM_strx1
+ .byte 19 ## DW_AT_language
+ .byte 5 ## DW_FORM_data2
+ .byte 3 ## DW_AT_name
+ .byte 37 ## DW_FORM_strx1
+ .ascii "\202|" ## DW_AT_LLVM_sysroot
+ .byte 37 ## DW_FORM_strx1
+ .ascii "\357\177" ## DW_AT_APPLE_sdk
+ .byte 37 ## DW_FORM_strx1
+ .byte 114 ## DW_AT_str_offsets_base
+ .byte 23 ## DW_FORM_sec_offset
+ .byte 16 ## DW_AT_stmt_list
+ .byte 23 ## DW_FORM_sec_offset
+ .byte 27 ## DW_AT_comp_dir
+ .byte 37 ## DW_FORM_strx1
+ .byte 17 ## DW_AT_low_pc
+ .byte 27 ## DW_FORM_addrx
+ .byte 18 ## DW_AT_high_pc
+ .byte 6 ## DW_FORM_data4
+ .byte 115 ## DW_AT_addr_base
+ .byte 23 ## DW_FORM_sec_offset
+ .byte 0 ## EOM(1)
+ .byte 0 ## EOM(2)
+ .byte 2 ## Abbreviation Code
+ .byte 46 ## DW_TAG_subprogram
+ .byte 1 ## DW_CHILDREN_yes
+ .byte 17 ## DW_AT_low_pc
+ .byte 27 ## DW_FORM_addrx
+ .byte 18 ## DW_AT_high_pc
+ .byte 6 ## DW_FORM_data4
+ .ascii "\347\177" ## DW_AT_APPLE_omit_frame_ptr
+ .byte 25 ## DW_FORM_flag_present
+ .byte 64 ## DW_AT_frame_base
+ .byte 24 ## DW_FORM_exprloc
+ .byte 3 ## DW_AT_name
+ .byte 37 ## DW_FORM_strx1
+ .byte 58 ## DW_AT_decl_file
+ .byte 11 ## DW_FORM_data1
+ .byte 59 ## DW_AT_decl_line
+ .byte 11 ## DW_FORM_data1
+ .byte 73 ## DW_AT_type
+ .byte 19 ## DW_FORM_ref4
+ .byte 63 ## DW_AT_external
+ .byte 25 ## DW_FORM_flag_present
+ .byte 0 ## EOM(1)
+ .byte 0 ## EOM(2)
+ .byte 3 ## Abbreviation Code
+ .byte 52 ## DW_TAG_variable
+ .byte 0 ## DW_CHILDREN_no
+ .byte 2 ## DW_AT_location
+ .byte 24 ## DW_FORM_exprloc
+ .byte 3 ## DW_AT_name
+ .byte 37 ## DW_FORM_strx1
+ .byte 58 ## DW_AT_decl_file
+ .byte 11 ## DW_FORM_data1
+ .byte 59 ## DW_AT_decl_line
+ .byte 11 ## DW_FORM_data1
+ .byte 73 ## DW_AT_type
+ .byte 19 ## DW_FORM_ref4
+ .byte 0 ## EOM(1)
+ .byte 0 ## EOM(2)
+ .byte 4 ## Abbreviation Code
+ .byte 36 ## DW_TAG_base_type
+ .byte 0 ## DW_CHILDREN_no
+ .byte 3 ## DW_AT_name
+ .byte 37 ## DW_FORM_strx1
+ .byte 62 ## DW_AT_encoding
+ .byte 11 ## DW_FORM_data1
+ .byte 11 ## DW_AT_byte_size
+ .byte 11 ## DW_FORM_data1
+ .byte 0 ## EOM(1)
+ .byte 0 ## EOM(2)
+ .byte 5 ## Abbreviation Code
+ .byte 19 ## DW_TAG_structure_type
+ .byte 1 ## DW_CHILDREN_yes
+ .byte 54 ## DW_AT_calling_convention
+ .byte 11 ## DW_FORM_data1
+ .byte 3 ## DW_AT_name
+ .byte 37 ## DW_FORM_strx1
+ .byte 11 ## DW_AT_byte_size
+ .byte 11 ## DW_FORM_data1
+ .byte 58 ## DW_AT_decl_file
+ .byte 11 ## DW_FORM_data1
+ .byte 59 ## DW_AT_decl_line
+ .byte 11 ## DW_FORM_data1
+ .byte 0 ## EOM(1)
+ .byte 0 ## EOM(2)
+ .byte 6 ## Abbreviation Code
+ .byte 13 ## DW_TAG_member
+ .byte 0 ## DW_CHILDREN_no
+ .byte 3 ## DW_AT_name
+ .byte 37 ## DW_FORM_strx1
+ .byte 73 ## DW_AT_type
+ .byte 19 ## DW_FORM_ref4
+ .byte 58 ## DW_AT_decl_file
+ .byte 11 ## DW_FORM_data1
+ .byte 59 ## DW_AT_decl_line
+ .byte 11 ## DW_FORM_data1
+ .byte 56 ## DW_AT_data_member_location
+ .byte 11 ## DW_FORM_data1
+ .byte 0 ## EOM(1)
+ .byte 0 ## EOM(2)
+ .byte 0 ## EOM(3)
+ .section __DWARF,__debug_info,regular,debug
+Lsection_info:
+Lcu_begin0:
+Lset0 = Ldebug_info_end0-Ldebug_info_start0 ## Length of Unit
+ .long Lset0
+Ldebug_info_start0:
+ .short 5 ## DWARF version number
+ .byte 1 ## DWARF Unit Type
+ .byte 8 ## Address Size (in bytes)
+Lset1 = Lsection_abbrev-Lsection_abbrev ## Offset Into Abbrev. Section
+ .long Lset1
+ .byte 1 ## Abbrev [1] 0xc:0x49 DW_TAG_compile_unit
+ .byte 0 ## DW_AT_producer
+ .short 33 ## DW_AT_language
+ .byte 1 ## DW_AT_name
+ .byte 2 ## DW_AT_LLVM_sysroot
+ .byte 3 ## DW_AT_APPLE_sdk
+Lset2 = Lstr_offsets_base0-Lsection_str_off ## DW_AT_str_offsets_base
+ .long Lset2
+Lset3 = Lline_table_start0-Lsection_line ## DW_AT_stmt_list
+ .long Lset3
+ .byte 4 ## DW_AT_comp_dir
+ .byte 0 ## DW_AT_low_pc
+Lset4 = Lfunc_end0-Lfunc_begin0 ## DW_AT_high_pc
+ .long Lset4
+Lset5 = Laddr_table_base0-Lsection_info0 ## DW_AT_addr_base
+ .long Lset5
+ .byte 2 ## Abbrev [2] 0x25:0x1b DW_TAG_subprogram
+ .byte 0 ## DW_AT_low_pc
+Lset6 = Lfunc_end0-Lfunc_begin0 ## DW_AT_high_pc
+ .long Lset6
+ ## DW_AT_APPLE_omit_frame_ptr
+ .byte 1 ## DW_AT_frame_base
+ .byte 87
+ .byte 5 ## DW_AT_name
+ .byte 0 ## DW_AT_decl_file
+ .byte 3 ## DW_AT_decl_line
+ .long 64 ## DW_AT_type
+ ## DW_AT_external
+ .byte 3 ## Abbrev [3] 0x34:0xb DW_TAG_variable
+ .byte 2 ## DW_AT_location
+ .byte 145
+ .byte 124
+ .byte 7 ## DW_AT_name
+ .byte 0 ## DW_AT_decl_file
+ .byte 4 ## DW_AT_decl_line
+ .long 68 ## DW_AT_type
+ .byte 0 ## End Of Children Mark
+ .byte 4 ## Abbrev [4] 0x40:0x4 DW_TAG_base_type
+ .byte 6 ## DW_AT_name
+ .byte 5 ## DW_AT_encoding
+ .byte 4 ## DW_AT_byte_size
+ .byte 5 ## Abbrev [5] 0x44:0x10 DW_TAG_structure_type
+ .byte 5 ## DW_AT_calling_convention
+ .byte 9 ## DW_AT_name
+ .byte 4 ## DW_AT_byte_size
+ .byte 1 ## DW_AT_decl_file
+ .byte 1 ## DW_AT_decl_line
+ .byte 6 ## Abbrev [6] 0x4a:0x9 DW_TAG_member
+ .byte 8 ## DW_AT_name
+ .long 64 ## DW_AT_type
+ .byte 1 ## DW_AT_decl_file
+ .byte 1 ## DW_AT_decl_line
+ .byte 0 ## DW_AT_data_member_location
+ .byte 0 ## End Of Children Mark
+ .byte 0 ## End Of Children Mark
+Ldebug_info_end0:
+ .section __DWARF,__debug_str_offs,regular,debug
+Lsection_str_off:
+ .long 44 ## Length of String Offsets Set
+ .short 5
+ .short 0
+Lstr_offsets_base0:
+ .section __DWARF,__debug_str,regular,debug
+Linfo_string:
+ .asciz "clang" ## string offset=0 ; clang
+ .asciz "main.cpp" ## string offset=6 ; main.cpp
+ .asciz "/^sdk" ## string offset=15 ; /^sdk
+ .asciz "MacOSX.sdk" ## string offset=21 ; MacOSX.sdk
+ .asciz "/^src" ## string offset=32 ; /^src
+ .asciz "main" ## string offset=38 ; main
+ .asciz "int" ## string offset=43 ; int
+ .asciz "f" ## string offset=47 ; f
+ .asciz "Foo" ## string offset=49 ; Foo
+ .asciz "x" ## string offset=53 ; x
+ .section __DWARF,__debug_str_offs,regular,debug
+ .long 0
+ .long 6
+ .long 15
+ .long 21
+ .long 32
+ .long 38
+ .long 43
+ .long 47
+ .long 53
+ .long 49
+ .section __DWARF,__debug_addr,regular,debug
+Lsection_info0:
+Lset7 = Ldebug_addr_end0-Ldebug_addr_start0 ## Length of contribution
+ .long Lset7
+Ldebug_addr_start0:
+ .short 5 ## DWARF version number
+ .byte 8 ## Address size
+ .byte 0 ## Segment selector size
+Laddr_table_base0:
+ .quad Lfunc_begin0
+Ldebug_addr_end0:
+ .section __DWARF,__debug_names,regular,debug
+Ldebug_names_begin:
+Lset8 = Lnames_end0-Lnames_start0 ## Header: unit length
+ .long Lset8
+Lnames_start0:
+ .short 5 ## Header: version
+ .short 0 ## Header: padding
+ .long 1 ## Header: compilation unit count
+ .long 0 ## Header: local type unit count
+ .long 0 ## Header: foreign type unit count
+ .long 3 ## Header: bucket count
+ .long 3 ## Header: name count
+Lset9 = Lnames_abbrev_end0-Lnames_abbrev_start0 ## Header: abbreviation table size
+ .long Lset9
+ .long 8 ## Header: augmentation string size
+ .ascii "LLVM0700" ## Header: augmentation string
+Lset10 = Lcu_begin0-Lsection_info ## Compilation unit 0
+ .long Lset10
+ .long 1 ## Bucket 0
+ .long 2 ## Bucket 1
+ .long 3 ## Bucket 2
+ .long 193491849 ## Hash in Bucket 0
+ .long 2090499946 ## Hash in Bucket 1
+ .long 193495088 ## Hash in Bucket 2
+ .long 49 ## String in Bucket 0: Foo
+ .long 38 ## String in Bucket 1: main
+ .long 43 ## String in Bucket 2: int
+Lset11 = Lnames0-Lnames_entries0 ## Offset in Bucket 0
+ .long Lset11
+Lset12 = Lnames1-Lnames_entries0 ## Offset in Bucket 1
+ .long Lset12
+Lset13 = Lnames2-Lnames_entries0 ## Offset in Bucket 2
+ .long Lset13
+Lnames_abbrev_start0:
+ .byte 1 ## Abbrev code
+ .byte 19 ## DW_TAG_structure_type
+ .byte 3 ## DW_IDX_die_offset
+ .byte 19 ## DW_FORM_ref4
+ .byte 4 ## DW_IDX_parent
+ .byte 25 ## DW_FORM_flag_present
+ .byte 0 ## End of abbrev
+ .byte 0 ## End of abbrev
+ .byte 2 ## Abbrev code
+ .byte 46 ## DW_TAG_subprogram
+ .byte 3 ## DW_IDX_die_offset
+ .byte 19 ## DW_FORM_ref4
+ .byte 4 ## DW_IDX_parent
+ .byte 25 ## DW_FORM_flag_present
+ .byte 0 ## End of abbrev
+ .byte 0 ## End of abbrev
+ .byte 3 ## Abbrev code
+ .byte 36 ## DW_TAG_base_type
+ .byte 3 ## DW_IDX_die_offset
+ .byte 19 ## DW_FORM_ref4
+ .byte 4 ## DW_IDX_parent
+ .byte 25 ## DW_FORM_flag_present
+ .byte 0 ## End of abbrev
+ .byte 0 ## End of abbrev
+ .byte 0 ## End of abbrev list
+Lnames_abbrev_end0:
+Lnames_entries0:
+Lnames0:
+ .byte 1 ## Abbreviation code
+ .long 68 ## DW_IDX_die_offset
+ .byte 0 ## DW_IDX_parent
+ ## End of list: Foo
+Lnames1:
+ .byte 2 ## Abbreviation code
+ .long 37 ## DW_IDX_die_offset
+ .byte 0 ## DW_IDX_parent
+ ## End of list: main
+Lnames2:
+ .byte 3 ## Abbreviation code
+ .long 64 ## DW_IDX_die_offset
+ .byte 0 ## DW_IDX_parent
+ ## End of list: int
+ .p2align 2, 0x0
+Lnames_end0:
+.subsections_via_symbols
+ .section __DWARF,__debug_line,regular,debug
+Lsection_line:
+Lline_table_start0:
diff --git a/llvm/test/tools/dsymutil/Inputs/swift-interface-prefix-map.s b/llvm/test/tools/dsymutil/Inputs/swift-interface-prefix-map.s
new file mode 100644
index 00000000000000..0b2b52bf93d8a9
--- /dev/null
+++ b/llvm/test/tools/dsymutil/Inputs/swift-interface-prefix-map.s
@@ -0,0 +1,262 @@
+##; Generated with llc -O0 from the following IR, which mimics an empty Swift
+##; program compiled with prefix mapping:
+##;
+##; target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
+##; target triple = "x86_64-apple-macosx10.9.0"
+##;
+##; define i32 @main(i32 %0, ptr %1) !dbg !20 {
+##; entry:
+##; ret i32 0, !dbg !23
+##; }
+##;
+##; !llvm.dbg.cu = !{!0}
+##; !llvm.module.flags = !{!30, !31}
+##;
+##; !0 = distinct !DICompileUnit(language: DW_LANG_Swift, file: !1, isOptimized: false, runtimeVersion: 5, emissionKind: FullDebug, imports: !3, sysroot: "/^sdk")
+##; !1 = !DIFile(filename: "main.swift", directory: "/^src")
+##; !2 = !{}
+##; !3 = !{!4, !6, !8, !10, !12}
+##; !4 = !DIImportedEntity(tag: DW_TAG_imported_module, scope: !1, entity: !5, file: !1)
+##; !5 = !DIModule(scope: null, name: "Foo", includePath: "/^src/Foo/x86_64.swiftinterface")
+##; !6 = !DIImportedEntity(tag: DW_TAG_imported_module, scope: !1, entity: !7, file: !1)
+##; !7 = !DIModule(scope: null, name: "Bar", includePath: "Bar/x86_64.swiftinterface")
+##; !8 = !DIImportedEntity(tag: DW_TAG_imported_module, scope: !1, entity: !9, file: !1)
+##; !9 = !DIModule(scope: null, name: "Swift", includePath: "/^sdk/usr/lib/swift/Swift.swiftmodule/x86_64-apple-macos.swiftinterface")
+##; !10 = !DIImportedEntity(tag: DW_TAG_imported_module, scope: !1, entity: !11, file: !1)
+##; !11 = !DIModule(scope: null, name: "Testing", includePath: "/^xcode/Platforms/MacOSX.platform/Developer/Library/Frameworks/Testing.framework/Modules/Testing.swiftmodule/x86_64-apple-macos.swiftinterface")
+##; !12 = !DIImportedEntity(tag: DW_TAG_imported_module, scope: !1, entity: !13, file: !1)
+##; !13 = !DIModule(scope: null, name: "_Concurrency", includePath: "/^toolchain/usr/lib/swift/macosx/_Concurrency.swiftmodule/x86_64-apple-macos.swiftinterface")
+##; !20 = distinct !DISubprogram(name: "main", linkageName: "main", scope: !1, file: !1, line: 1, type: !21, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
+##; !21 = !DISubroutineType(types: !2)
+##; !23 = !DILocation(line: 1, scope: !20)
+##; !30 = !{i32 2, !"Dwarf Version", i32 4}
+##; !31 = !{i32 2, !"Debug Info Version", i32 3}
+
+ .macosx_version_min 10, 9
+ .section __TEXT,__text,regular,pure_instructions
+ .att_syntax
+ .globl _main ## -- Begin function main
+ .p2align 4
+_main: ## @main
+Lfunc_begin0:
+ .cfi_startproc
+## %bb.0: ## %entry
+ .file 1 "/^src" "main.swift"
+ .loc 1 1 0 prologue_end ## main.swift:1
+ xorl %eax, %eax
+ retq
+Ltmp0:
+Lfunc_end0:
+ .cfi_endproc
+ ## -- End function
+ .section __DWARF,__debug_abbrev,regular,debug
+Lsection_abbrev:
+ .byte 1 ## Abbreviation Code
+ .byte 17 ## DW_TAG_compile_unit
+ .byte 1 ## DW_CHILDREN_yes
+ .byte 37 ## DW_AT_producer
+ .byte 14 ## DW_FORM_strp
+ .byte 19 ## DW_AT_language
+ .byte 5 ## DW_FORM_data2
+ .byte 3 ## DW_AT_name
+ .byte 14 ## DW_FORM_strp
+ .ascii "\202|" ## DW_AT_LLVM_sysroot
+ .byte 14 ## DW_FORM_strp
+ .byte 16 ## DW_AT_stmt_list
+ .byte 23 ## DW_FORM_sec_offset
+ .byte 27 ## DW_AT_comp_dir
+ .byte 14 ## DW_FORM_strp
+ .ascii "\345\177" ## DW_AT_APPLE_major_runtime_vers
+ .byte 11 ## DW_FORM_data1
+ .byte 17 ## DW_AT_low_pc
+ .byte 1 ## DW_FORM_addr
+ .byte 18 ## DW_AT_high_pc
+ .byte 6 ## DW_FORM_data4
+ .byte 0 ## EOM(1)
+ .byte 0 ## EOM(2)
+ .byte 2 ## Abbreviation Code
+ .byte 46 ## DW_TAG_subprogram
+ .byte 0 ## DW_CHILDREN_no
+ .byte 17 ## DW_AT_low_pc
+ .byte 1 ## DW_FORM_addr
+ .byte 18 ## DW_AT_high_pc
+ .byte 6 ## DW_FORM_data4
+ .ascii "\347\177" ## DW_AT_APPLE_omit_frame_ptr
+ .byte 25 ## DW_FORM_flag_present
+ .byte 64 ## DW_AT_frame_base
+ .byte 24 ## DW_FORM_exprloc
+ .byte 110 ## DW_AT_linkage_name
+ .byte 14 ## DW_FORM_strp
+ .byte 3 ## DW_AT_name
+ .byte 14 ## DW_FORM_strp
+ .byte 58 ## DW_AT_decl_file
+ .byte 11 ## DW_FORM_data1
+ .byte 59 ## DW_AT_decl_line
+ .byte 11 ## DW_FORM_data1
+ .byte 63 ## DW_AT_external
+ .byte 25 ## DW_FORM_flag_present
+ .byte 0 ## EOM(1)
+ .byte 0 ## EOM(2)
+ .byte 3 ## Abbreviation Code
+ .byte 30 ## DW_TAG_module
+ .byte 0 ## DW_CHILDREN_no
+ .byte 3 ## DW_AT_name
+ .byte 14 ## DW_FORM_strp
+ .ascii "\200|" ## DW_AT_LLVM_include_path
+ .byte 14 ## DW_FORM_strp
+ .byte 0 ## EOM(1)
+ .byte 0 ## EOM(2)
+ .byte 4 ## Abbreviation Code
+ .byte 58 ## DW_TAG_imported_module
+ .byte 0 ## DW_CHILDREN_no
+ .byte 24 ## DW_AT_import
+ .byte 19 ## DW_FORM_ref4
+ .byte 0 ## EOM(1)
+ .byte 0 ## EOM(2)
+ .byte 0 ## EOM(3)
+ .section __DWARF,__debug_info,regular,debug
+Lsection_info:
+Lcu_begin0:
+Lset0 = Ldebug_info_end0-Ldebug_info_start0 ## Length of Unit
+ .long Lset0
+Ldebug_info_start0:
+ .short 4 ## DWARF version number
+Lset1 = Lsection_abbrev-Lsection_abbrev ## Offset Into Abbrev. Section
+ .long Lset1
+ .byte 8 ## Address Size (in bytes)
+ .byte 1 ## Abbrev [1] 0xb:0x84 DW_TAG_compile_unit
+ .long 0 ## DW_AT_producer
+ .short 30 ## DW_AT_language
+ .long 1 ## DW_AT_name
+ .long 12 ## DW_AT_LLVM_sysroot
+Lset2 = Lline_table_start0-Lsection_line ## DW_AT_stmt_list
+ .long Lset2
+ .long 18 ## DW_AT_comp_dir
+ .byte 5 ## DW_AT_APPLE_major_runtime_vers
+ .quad Lfunc_begin0 ## DW_AT_low_pc
+Lset3 = Lfunc_end0-Lfunc_begin0 ## DW_AT_high_pc
+ .long Lset3
+ .byte 2 ## Abbrev [2] 0x2f:0x19 DW_TAG_subprogram
+ .quad Lfunc_begin0 ## DW_AT_low_pc
+Lset4 = Lfunc_end0-Lfunc_begin0 ## DW_AT_high_pc
+ .long Lset4
+ ## DW_AT_APPLE_omit_frame_ptr
+ .byte 1 ## DW_AT_frame_base
+ .byte 87
+ .long 24 ## DW_AT_linkage_name
+ .long 24 ## DW_AT_name
+ .byte 1 ## DW_AT_decl_file
+ .byte 1 ## DW_AT_decl_line
+ ## DW_AT_external
+ .byte 3 ## Abbrev [3] 0x48:0x9 DW_TAG_module
+ .long 29 ## DW_AT_name
+ .long 33 ## DW_AT_LLVM_include_path
+ .byte 4 ## Abbrev [4] 0x51:0x5 DW_TAG_imported_module
+ .long 72 ## DW_AT_import
+ .byte 3 ## Abbrev [3] 0x56:0x9 DW_TAG_module
+ .long 65 ## DW_AT_name
+ .long 69 ## DW_AT_LLVM_include_path
+ .byte 4 ## Abbrev [4] 0x5f:0x5 DW_TAG_imported_module
+ .long 86 ## DW_AT_import
+ .byte 3 ## Abbrev [3] 0x64:0x9 DW_TAG_module
+ .long 95 ## DW_AT_name
+ .long 101 ## DW_AT_LLVM_include_path
+ .byte 4 ## Abbrev [4] 0x6d:0x5 DW_TAG_imported_module
+ .long 100 ## DW_AT_import
+ .byte 3 ## Abbrev [3] 0x72:0x9 DW_TAG_module
+ .long 173 ## DW_AT_name
+ .long 181 ## DW_AT_LLVM_include_path
+ .byte 4 ## Abbrev [4] 0x7b:0x5 DW_TAG_imported_module
+ .long 114 ## DW_AT_import
+ .byte 3 ## Abbrev [3] 0x80:0x9 DW_TAG_module
+ .long 324 ## DW_AT_name
+ .long 337 ## DW_AT_LLVM_include_path
+ .byte 4 ## Abbrev [4] 0x89:0x5 DW_TAG_imported_module
+ .long 128 ## DW_AT_import
+ .byte 0 ## End Of Children Mark
+Ldebug_info_end0:
+ .section __DWARF,__debug_str,regular,debug
+Linfo_string:
+ .byte 0 ## string offset=0 ;
+ .asciz "main.swift" ## string offset=1 ; main.swift
+ .asciz "/^sdk" ## string offset=12 ; /^sdk
+ .asciz "/^src" ## string offset=18 ; /^src
+ .asciz "main" ## string offset=24 ; main
+ .asciz "Foo" ## string offset=29 ; Foo
+ .asciz "/^src/Foo/x86_64.swiftinterface" ## string offset=33 ; /^src/Foo/x86_64.swiftinterface
+ .asciz "Bar" ## string offset=65 ; Bar
+ .asciz "Bar/x86_64.swiftinterface" ## string offset=69 ; Bar/x86_64.swiftinterface
+ .asciz "Swift" ## string offset=95 ; Swift
+ .asciz "/^sdk/usr/lib/swift/Swift.swiftmodule/x86_64-apple-macos.swiftinterface" ## string offset=101 ; /^sdk/usr/lib/swift/Swift.swiftmodule/x86_64-apple-macos.swiftinterface
+ .asciz "Testing" ## string offset=173 ; Testing
+ .asciz "/^xcode/Platforms/MacOSX.platform/Developer/Library/Frameworks/Testing.framework/Modules/Testing.swiftmodule/x86_64-apple-macos.swiftinterface" ## string offset=181 ; /^xcode/Platforms/MacOSX.platform/Developer/Library/Frameworks/Testing.framework/Modules/Testing.swiftmodule/x86_64-apple-macos.swiftinterface
+ .asciz "_Concurrency" ## string offset=324 ; _Concurrency
+ .asciz "/^toolchain/usr/lib/swift/macosx/_Concurrency.swiftmodule/x86_64-apple-macos.swiftinterface" ## string offset=337 ; /^toolchain/usr/lib/swift/macosx/_Concurrency.swiftmodule/x86_64-apple-macos.swiftinterface
+ .section __DWARF,__apple_names,regular,debug
+Lnames_begin:
+ .long 1212240712 ## Header Magic
+ .short 1 ## Header Version
+ .short 0 ## Header Hash Function
+ .long 1 ## Header Bucket Count
+ .long 1 ## Header Hash Count
+ .long 12 ## Header Data Length
+ .long 0 ## HeaderData Die Offset Base
+ .long 1 ## HeaderData Atom Count
+ .short 1 ## DW_ATOM_die_offset
+ .short 6 ## DW_FORM_data4
+ .long 0 ## Bucket 0
+ .long 2090499946 ## Hash in Bucket 0
+Lset5 = LNames0-Lnames_begin ## Offset in Bucket 0
+ .long Lset5
+LNames0:
+ .long 24 ## main
+ .long 1 ## Num DIEs
+ .long 47
+ .long 0
+ .section __DWARF,__apple_objc,regular,debug
+Lobjc_begin:
+ .long 1212240712 ## Header Magic
+ .short 1 ## Header Version
+ .short 0 ## Header Hash Function
+ .long 1 ## Header Bucket Count
+ .long 0 ## Header Hash Count
+ .long 12 ## Header Data Length
+ .long 0 ## HeaderData Die Offset Base
+ .long 1 ## HeaderData Atom Count
+ .short 1 ## DW_ATOM_die_offset
+ .short 6 ## DW_FORM_data4
+ .long -1 ## Bucket 0
+ .section __DWARF,__apple_namespac,regular,debug
+Lnamespac_begin:
+ .long 1212240712 ## Header Magic
+ .short 1 ## Header Version
+ .short 0 ## Header Hash Function
+ .long 1 ## Header Bucket Count
+ .long 0 ## Header Hash Count
+ .long 12 ## Header Data Length
+ .long 0 ## HeaderData Die Offset Base
+ .long 1 ## HeaderData Atom Count
+ .short 1 ## DW_ATOM_die_offset
+ .short 6 ## DW_FORM_data4
+ .long -1 ## Bucket 0
+ .section __DWARF,__apple_types,regular,debug
+Ltypes_begin:
+ .long 1212240712 ## Header Magic
+ .short 1 ## Header Version
+ .short 0 ## Header Hash Function
+ .long 1 ## Header Bucket Count
+ .long 0 ## Header Hash Count
+ .long 20 ## Header Data Length
+ .long 0 ## HeaderData Die Offset Base
+ .long 3 ## HeaderData Atom Count
+ .short 1 ## DW_ATOM_die_offset
+ .short 6 ## DW_FORM_data4
+ .short 3 ## DW_ATOM_die_tag
+ .short 5 ## DW_FORM_data2
+ .short 4 ## DW_ATOM_type_flags
+ .short 11 ## DW_FORM_data1
+ .long -1 ## Bucket 0
+.subsections_via_symbols
+ .section __DWARF,__debug_line,regular,debug
+Lsection_line:
+Lline_table_start0:
diff --git a/llvm/test/tools/dsymutil/X86/compilation-prefix-map.test b/llvm/test/tools/dsymutil/X86/compilation-prefix-map.test
new file mode 100644
index 00000000000000..1f7a79aea3e797
--- /dev/null
+++ b/llvm/test/tools/dsymutil/X86/compilation-prefix-map.test
@@ -0,0 +1,58 @@
+# Test that paths in prefix-mapped debug info are restored in the linked debug
+# info using the compilation-prefix-map.json closest to the object file.
+
+# RUN: rm -rf %t.dir
+# RUN: mkdir -p %t.dir/build/Objects
+# RUN: llvm-mc -triple x86_64-apple-macos -filetype=obj \
+# RUN: -o %t.dir/build/Objects/1.o %p/../Inputs/compilation-prefix-map.s
+# RUN: echo '{"/^src": "/src", "/^sdk": "/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk"}' \
+# RUN: > %t.dir/build/compilation-prefix-map.json
+
+## The DWARF 5 line table refers to paths in .debug_line_str. The parallel
+## linker also moves the type into an artificial type unit with its own line
+## table. No remapped path may be left anywhere.
+# RUN: dsymutil --linker classic -oso-prepend-path %t.dir -y %s \
+# RUN: -o %t.dir/classic.dSYM
+# RUN: llvm-dwarfdump -a -v %t.dir/classic.dSYM \
+# RUN: | FileCheck %s --check-prefixes=CHECK,CLASSIC --implicit-check-not=/^
+# RUN: dsymutil --linker parallel -oso-prepend-path %t.dir -y %s \
+# RUN: -o %t.dir/parallel.dSYM
+# RUN: llvm-dwarfdump -a -v %t.dir/parallel.dSYM \
+# RUN: | FileCheck %s --check-prefixes=CHECK,PARALLEL --implicit-check-not=/^
+
+# PARALLEL: DW_TAG_compile_unit
+# PARALLEL: DW_AT_name [DW_FORM_strp] {{.*}} "__artificial_type_unit"
+# PARALLEL: DW_TAG_structure_type
+# PARALLEL: DW_AT_decl_file {{.*}} ("/src{{[/\\]}}include{{[/\\]}}foo.h")
+
+# CHECK: DW_TAG_compile_unit
+# CHECK: DW_AT_name {{.*}} "main.cpp"
+# CHECK: DW_AT_LLVM_sysroot {{.*}} "/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk"
+# CHECK: DW_AT_comp_dir {{.*}} "/src"
+# CHECK: DW_AT_decl_file {{.*}} ("/src{{[/\\]}}main.cpp")
+
+# CLASSIC: DW_TAG_structure_type
+# CLASSIC: DW_AT_decl_file {{.*}} ("/src{{[/\\]}}include{{[/\\]}}foo.h")
+
+# PARALLEL: include_directories[ 0] = {{.*}}"/src{{[/\\]}}include"
+# CHECK: include_directories[ 0] = .debug_line_str[{{.*}}] = "/src"
+# CHECK: include_directories[ 1] = .debug_line_str[{{.*}}] = "/src{{[/\\]}}include"
+
+## Paths without an entry in the closest compilation prefix map are left alone.
+## The prefix map passed on the command line only applies to looking up Clang
+## modules.
+# RUN: echo '{}' > %t.dir/build/compilation-prefix-map.json
+# RUN: dsymutil --linker classic -oso-prepend-path %t.dir -y %s \
+# RUN: -o %t.dir/unmapped.dSYM -object-prefix-map '/^src=/src'
+# RUN: llvm-dwarfdump --debug-info %t.dir/unmapped.dSYM \
+# RUN: | FileCheck %s --check-prefix=UNMAPPED
+
+# UNMAPPED: DW_AT_comp_dir ("/^src")
+
+---
+triple: 'x86_64-apple-darwin'
+objects:
+ - filename: build/Objects/1.o
+ symbols:
+ - { sym: _main, objAddr: 0x0, binAddr: 0x10000, size: 0x10 }
+...
diff --git a/llvm/test/tools/dsymutil/X86/swift-interface-prefix-map.test b/llvm/test/tools/dsymutil/X86/swift-interface-prefix-map.test
new file mode 100644
index 00000000000000..17f0597409ebae
--- /dev/null
+++ b/llvm/test/tools/dsymutil/X86/swift-interface-prefix-map.test
@@ -0,0 +1,70 @@
+# Test that paths to Swift interfaces in prefix-mapped debug info are remapped
+# using the compilation-prefix-map.json closest to the object file before they
+# are filtered and copied.
+
+# RUN: rm -rf %t.dir
+# RUN: mkdir -p %t.dir/build/Objects
+# RUN: llvm-mc -triple x86_64-apple-macos -filetype=obj \
+# RUN: -o %t.dir/build/Objects/1.o %p/../Inputs/swift-interface-prefix-map.s
+# RUN: echo '{"/^src": "/src", "/^sdk": "/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk", "/^xcode": "/Xcode.app/Contents/Developer", "/^toolchain": "/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain"}' \
+# RUN: > %t.dir/build/compilation-prefix-map.json
+
+## Interfaces from the SDK, Xcode, and the toolchain are skipped. The remaining
+## ones are looked up at their remapped location.
+# RUN: dsymutil --linker classic -oso-prepend-path %t.dir -y %s \
+# RUN: -o %t.dir/classic.dSYM 2>&1 \
+# RUN: | FileCheck %s --check-prefix=MISSING --implicit-check-not=interface
+# RUN: dsymutil --linker parallel -oso-prepend-path %t.dir -y %s \
+# RUN: -o %t.dir/parallel.dSYM 2>&1 \
+# RUN: | FileCheck %s --check-prefix=MISSING --implicit-check-not=interface
+
+# MISSING: cannot copy parseable Swift interface {{.*}}.dir{{[/\\]}}src{{[/\\]}}Bar{{[/\\]}}x86_64.swiftinterface
+# MISSING: cannot copy parseable Swift interface {{.*}}.dir{{[/\\]}}src{{[/\\]}}Foo{{[/\\]}}x86_64.swiftinterface
+
+# RUN: mkdir -p %t.dir/src/Foo %t.dir/src/Bar
+# RUN: echo "// module Foo" > %t.dir/src/Foo/x86_64.swiftinterface
+# RUN: echo "// module Bar" > %t.dir/src/Bar/x86_64.swiftinterface
+# RUN: rm -rf %t.dir/classic.dSYM %t.dir/parallel.dSYM
+# RUN: dsymutil --linker classic -oso-prepend-path %t.dir -y %s \
+# RUN: -o %t.dir/classic.dSYM 2>&1 | count 0
+# RUN: ls %t.dir/classic.dSYM/Contents/Resources/Swift/x86_64 \
+# RUN: | FileCheck %s --check-prefix=COPIED --implicit-check-not=interface
+# RUN: dsymutil --linker parallel -oso-prepend-path %t.dir -y %s \
+# RUN: -o %t.dir/parallel.dSYM 2>&1 | count 0
+# RUN: ls %t.dir/parallel.dSYM/Contents/Resources/Swift/x86_64 \
+# RUN: | FileCheck %s --check-prefix=COPIED --implicit-check-not=interface
+
+# COPIED: Bar.swiftinterface
+# COPIED: Foo.swiftinterface
+
+## The original paths are restored in the linked debug info.
+# RUN: llvm-dwarfdump -a %t.dir/classic.dSYM \
+# RUN: | FileCheck %s --check-prefix=DWARF --implicit-check-not=/^
+# RUN: llvm-dwarfdump -a %t.dir/parallel.dSYM \
+# RUN: | FileCheck %s --check-prefix=DWARF --implicit-check-not=/^
+
+# DWARF: DW_TAG_compile_unit
+# DWARF: DW_AT_LLVM_sysroot ("/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk")
+# DWARF: DW_AT_comp_dir ("/src")
+# DWARF: DW_AT_name ("Foo")
+# DWARF-NEXT: DW_AT_LLVM_include_path ("/src{{[/\\]}}Foo{{[/\\]}}x86_64.swiftinterface")
+# DWARF: DW_AT_name ("Testing")
+# DWARF-NEXT: DW_AT_LLVM_include_path ("/Xcode.app/Contents/Developer{{[/\\]}}Platforms/MacOSX.platform/Developer/Library/Frameworks/Testing.framework/Modules/Testing.swiftmodule/x86_64-apple-macos.swiftinterface")
+# DWARF: include_directories[ 1] = "/src"
+
+## A malformed prefix map is diagnosed and ignored.
+# RUN: echo 'not json' > %t.dir/build/compilation-prefix-map.json
+# RUN: dsymutil --linker classic -oso-prepend-path %t.dir -y %s \
+# RUN: -o %t.dir/malformed.dSYM 2>&1 | FileCheck %s --check-prefix=MALFORMED
+
+# MALFORMED: warning: cannot parse compilation prefix map: {{.*}}Invalid JSON value
+# MALFORMED-NEXT: note: while processing {{.*}}.dir{{[/\\]}}build{{[/\\]}}compilation-prefix-map.json
+# MALFORMED: cannot copy parseable Swift interface {{.*}}.dir{{[/\\]}}^src{{[/\\]}}Bar{{[/\\]}}x86_64.swiftinterface
+
+---
+triple: 'x86_64-apple-darwin'
+objects:
+ - filename: build/Objects/1.o
+ symbols:
+ - { sym: _main, objAddr: 0x0, binAddr: 0x10000, size: 0x10 }
+...
diff --git a/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp b/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp
index 7e8201236144b2..b925d2eebc2368 100644
--- a/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp
+++ b/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp
@@ -56,10 +56,12 @@
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Format.h"
+#include "llvm/Support/JSON.h"
#include "llvm/Support/LEB128.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
+#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Support/WithColor.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetOptions.h"
@@ -480,6 +482,54 @@ void DwarfLinkerForBinary::collectRelocationsToApplyToSwiftReflectionSections(
}
}
+std::shared_ptr<PrefixMapper>
+DwarfLinkerForBinary::findCompilationPrefixMap(StringRef ObjectPath) {
+ // With compilation caching, the compiler can prefix map the paths it sees,
+ // so that cache keys don't depend on where the build happens. The build
+ // system then records the mapping in compilation-prefix-map.json in a parent
+ // directory of the object files, as a JSON object from each remapped prefix
+ // to the original one.
+ StringRef ObjectDir = sys::path::parent_path(ObjectPath);
+ auto [It, Inserted] = CompilationPrefixMaps.try_emplace(ObjectDir);
+ if (!Inserted)
+ return It->second;
+
+ std::optional<std::string> Path = vfs::findFileInParentDirectories(
+ *Options.VFS, ObjectDir, {"compilation-prefix-map.json"});
+ if (!Path)
+ return nullptr;
+
+ if (Options.Verbose)
+ outs() << "found compilation prefix map " << *Path << '\n';
+ auto BufferOrErr = Options.VFS->getBufferForFile(*Path);
+ if (!BufferOrErr) {
+ reportWarning("cannot read compilation prefix map: " +
+ BufferOrErr.getError().message(),
+ *Path);
+ return nullptr;
+ }
+ Expected<json::Value> Root = json::parse((*BufferOrErr)->getBuffer());
+ if (!Root) {
+ reportWarning("cannot parse compilation prefix map: " +
+ toString(Root.takeError()),
+ *Path);
+ return nullptr;
+ }
+ const json::Object *Entries = Root->getAsObject();
+ if (!Entries) {
+ reportWarning("cannot parse compilation prefix map: expected a JSON "
+ "object",
+ *Path);
+ return nullptr;
+ }
+ auto PrefixMap = std::make_shared<PrefixMapper>();
+ for (const auto &Entry : *Entries)
+ if (std::optional<StringRef> Original = Entry.second.getAsString())
+ PrefixMap->add(MappedPrefix(Entry.first, *Original));
+ PrefixMap->sort();
+ return It->second = std::move(PrefixMap);
+}
+
Error DwarfLinkerForBinary::copySwiftInterfaces(StringRef Architecture) const {
std::error_code EC;
SmallString<128> InputPath;
@@ -927,6 +977,8 @@ bool DwarfLinkerForBinary::linkImpl(
auto DLBRelocMap = std::make_shared<DwarfLinkerForBinaryRelocationMap>();
if (ErrorOr<std::unique_ptr<DWARFFile>> ErrorOrObj =
loadObject(*Obj, Map, RL, PL, DLBRelocMap)) {
+ (*ErrorOrObj)->PrefixMap =
+ findCompilationPrefixMap(Obj->getObjectFilename());
ObjectsForLinking.emplace_back(std::move(*ErrorOrObj), DLBRelocMap);
GeneralLinker->addObjectFile(*ObjectsForLinking.back().Object, Loader,
OnCUDieLoaded);
diff --git a/llvm/tools/dsymutil/DwarfLinkerForBinary.h b/llvm/tools/dsymutil/DwarfLinkerForBinary.h
index 8b6e2acb37de10..d4dbff6ec27f91 100644
--- a/llvm/tools/dsymutil/DwarfLinkerForBinary.h
+++ b/llvm/tools/dsymutil/DwarfLinkerForBinary.h
@@ -14,6 +14,8 @@
#include "LinkUtils.h"
#include "MachOUtils.h"
#include "RelocationMap.h"
+#include "llvm/ADT/StringMap.h"
+#include "llvm/DWARFLinker/DWARFFile.h"
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
#include "llvm/Remarks/RemarkFormat.h"
#include "llvm/Remarks/RemarkLinker.h"
@@ -289,6 +291,10 @@ class DwarfLinkerForBinary {
std::vector<MachOUtils::DwarfRelocationApplicationInfo>
&RelocationsToApply) const;
+ /// Find the compilation-prefix-map.json closest to \p ObjectPath, which
+ /// describes how the compiler remapped paths in the object's debug info.
+ std::shared_ptr<PrefixMapper> findCompilationPrefixMap(StringRef ObjectPath);
+
Error copySwiftInterfaces(StringRef Architecture) const;
Error copyEmbeddedResources() const;
@@ -321,6 +327,10 @@ class DwarfLinkerForBinary {
/// per compile unit, which is why this is a std::map.
std::map<std::string, std::string> ParseableSwiftInterfaces;
+ /// The compilation prefix map of the objects in each directory, or nullptr
+ /// if there is none.
+ StringMap<std::shared_ptr<PrefixMapper>> CompilationPrefixMaps;
+
bool ModuleCacheHintDisplayed = false;
bool ArchiveHintDisplayed = false;
bool HasVerificationErrors = false;
More information about the llvm-branch-commits
mailing list