Skip to content

Commit 66117aa

Browse files
committed
Reland [dsymutil] Add support for mergeable libraries (llvm#70256)
Reland https://reviews.llvm.org/D158124 Fixed `-fpermissive` error reported by gcc only.
1 parent 1cd18ba commit 66117aa

File tree

44 files changed

+1219
-187
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1219
-187
lines changed

llvm/docs/CommandGuide/dsymutil.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,26 @@ OPTIONS
3232
architectures will be linked by default and any architectures that can't be
3333
properly linked will cause :program:`dsymutil` to return an error.
3434

35+
.. option:: --build-variant-suffix <suffix=buildvariant>
36+
37+
Specify the build variant suffix used to build the executabe file.
38+
There can be multiple variants for the binary of a product, each built
39+
slightly differently. The most common build variants are 'debug' and
40+
'profile'. Setting the DYLD_IMAGE_SUFFIX environment variable will
41+
cause dyld to load the specified variant at runtime.
42+
3543
.. option:: --dump-debug-map
3644

3745
Dump the *executable*'s debug-map (the list of the object files containing the
3846
debug information) in YAML format and exit. No DWARF link will take place.
3947

48+
.. option:: -D <path>
49+
50+
Specify a directory that contain dSYM files to search for.
51+
This is used for mergeable libraries, so dsymutil knows where to look
52+
for dSYM files with debug information about symbols present in those
53+
libraries.
54+
4055
.. option:: --fat64
4156

4257
Use a 64-bit header when emitting universal binaries.

llvm/include/llvm/BinaryFormat/Dwarf.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,8 @@ HANDLE_DW_AT(0x3fec, APPLE_objc_complete_type, 0, APPLE)
628628
HANDLE_DW_AT(0x3fed, APPLE_property, 0, APPLE)
629629
HANDLE_DW_AT(0x3fee, APPLE_objc_direct, 0, APPLE)
630630
HANDLE_DW_AT(0x3fef, APPLE_sdk, 0, APPLE)
631-
HANDLE_DW_AT(0x3ff0, APPLE_num_extra_inhabitants, 0, APPLE)
631+
HANDLE_DW_AT(0x3ff0, APPLE_origin, 0, APPLE)
632+
HANDLE_DW_AT(0x3ff1, APPLE_num_extra_inhabitants, 0, APPLE)
632633

633634
// Attribute form encodings.
634635
HANDLE_DW_FORM(0x01, addr, 2, DWARF)

llvm/include/llvm/BinaryFormat/MachO.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ enum StabType {
373373
N_SSYM = 0x60u,
374374
N_SO = 0x64u,
375375
N_OSO = 0x66u,
376+
N_LIB = 0x68u,
376377
N_LSYM = 0x80u,
377378
N_BINCL = 0x82u,
378379
N_SOL = 0x84u,

llvm/include/llvm/DWARFLinker/DWARFLinker.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,33 @@ class AddressesMap {
6262
virtual std::optional<int64_t>
6363
getSubprogramRelocAdjustment(const DWARFDie &DIE) = 0;
6464

65+
/// Returns the file name associated to the AddessesMap
66+
virtual std::optional<StringRef> getLibraryInstallName() = 0;
67+
6568
/// Apply the valid relocations to the buffer \p Data, taking into
6669
/// account that Data is at \p BaseOffset in the .debug_info section.
6770
///
6871
/// \returns true whether any reloc has been applied.
6972
virtual bool applyValidRelocs(MutableArrayRef<char> Data, uint64_t BaseOffset,
7073
bool IsLittleEndian) = 0;
7174

75+
/// Check if the linker needs to gather and save relocation info.
76+
virtual bool needToSaveValidRelocs() = 0;
77+
78+
/// Update and save original relocations located in between StartOffset and
79+
/// EndOffset. LinkedOffset is the value which should be added to the original
80+
/// relocation offset to get new relocation offset in linked binary.
81+
virtual void updateAndSaveValidRelocs(bool IsDWARF5,
82+
uint64_t OriginalUnitOffset,
83+
int64_t LinkedOffset,
84+
uint64_t StartOffset,
85+
uint64_t EndOffset) = 0;
86+
87+
/// Update the valid relocations that used OriginalUnitOffset as the compile
88+
/// unit offset, and update their values to reflect OutputUnitOffset.
89+
virtual void updateRelocationsWithUnitOffset(uint64_t OriginalUnitOffset,
90+
uint64_t OutputUnitOffset) = 0;
91+
7292
/// Erases all data.
7393
virtual void clear() = 0;
7494
};
@@ -744,6 +764,9 @@ class DWARFLinker {
744764
/// Is there a DW_AT_str_offsets_base in the CU?
745765
bool AttrStrOffsetBaseSeen = false;
746766

767+
/// Is there a DW_AT_APPLE_origin in the CU?
768+
bool HasAppleOrigin = false;
769+
747770
AttributesInfo() = default;
748771
};
749772

llvm/include/llvm/DWARFLinkerParallel/AddressesMap.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,31 @@ class AddressesMap {
5353
virtual std::optional<int64_t>
5454
getSubprogramRelocAdjustment(const DWARFDie &DIE) = 0;
5555

56+
// Returns the library install name associated to the AddessesMap.
57+
virtual std::optional<StringRef> getLibraryInstallName() = 0;
58+
5659
/// Apply the valid relocations to the buffer \p Data, taking into
5760
/// account that Data is at \p BaseOffset in the .debug_info section.
5861
///
5962
/// \returns true whether any reloc has been applied.
6063
virtual bool applyValidRelocs(MutableArrayRef<char> Data, uint64_t BaseOffset,
6164
bool IsLittleEndian) = 0;
6265

66+
/// Check if the linker needs to gather and save relocation info.
67+
virtual bool needToSaveValidRelocs() = 0;
68+
69+
/// Update and save relocation values to be serialized
70+
virtual void updateAndSaveValidRelocs(bool IsDWARF5,
71+
uint64_t OriginalUnitOffset,
72+
int64_t LinkedOffset,
73+
uint64_t StartOffset,
74+
uint64_t EndOffset) = 0;
75+
76+
/// Update the valid relocations that used OriginalUnitOffset as the compile
77+
/// unit offset, and update their values to reflect OutputUnitOffset.
78+
virtual void updateRelocationsWithUnitOffset(uint64_t OriginalUnitOffset,
79+
uint64_t OutputUnitOffset) = 0;
80+
6381
/// Erases all data.
6482
virtual void clear() = 0;
6583
};

llvm/include/llvm/TargetParser/Triple.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,9 +420,6 @@ class Triple {
420420
/// Get the architecture (first) component of the triple.
421421
StringRef getArchName() const;
422422

423-
/// Get the architecture name based on Kind and SubArch.
424-
StringRef getArchName(ArchType Kind, SubArchType SubArch = NoSubArch) const;
425-
426423
/// Get the vendor (second) component of the triple.
427424
StringRef getVendorName() const;
428425

@@ -1102,6 +1099,9 @@ class Triple {
11021099
/// Get the canonical name for the \p Kind architecture.
11031100
static StringRef getArchTypeName(ArchType Kind);
11041101

1102+
/// Get the architecture name based on \p Kind and \p SubArch.
1103+
static StringRef getArchName(ArchType Kind, SubArchType SubArch = NoSubArch);
1104+
11051105
/// Get the "prefix" canonical name for the \p Kind architecture. This is the
11061106
/// prefix used by the architecture specific builtins, and is suitable for
11071107
/// passing to \see Intrinsic::getIntrinsicForClangBuiltin().

llvm/lib/DWARFLinker/DWARFLinker.cpp

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,15 @@ unsigned DWARFLinker::DIECloner::cloneStringAttribute(DIE &Die,
10241024
StringEntry = DebugLineStrPool.getEntry(*String);
10251025
} else {
10261026
StringEntry = DebugStrPool.getEntry(*String);
1027+
1028+
if (AttrSpec.Attr == dwarf::DW_AT_APPLE_origin) {
1029+
Info.HasAppleOrigin = true;
1030+
if (std::optional<StringRef> FileName =
1031+
ObjFile.Addresses->getLibraryInstallName()) {
1032+
StringEntry = DebugStrPool.getEntry(*FileName);
1033+
}
1034+
}
1035+
10271036
// Update attributes info.
10281037
if (AttrSpec.Attr == dwarf::DW_AT_name)
10291038
Info.Name = StringEntry;
@@ -1635,6 +1644,12 @@ shouldSkipAttribute(bool Update,
16351644
}
16361645
}
16371646

1647+
struct AttributeLinkedOffsetFixup {
1648+
int64_t LinkedOffsetFixupVal;
1649+
uint64_t InputAttrStartOffset;
1650+
uint64_t InputAttrEndOffset;
1651+
};
1652+
16381653
DIE *DWARFLinker::DIECloner::cloneDIE(const DWARFDie &InputDIE,
16391654
const DWARFFile &File, CompileUnit &Unit,
16401655
int64_t PCOffset, uint32_t OutOffset,
@@ -1718,24 +1733,51 @@ DIE *DWARFLinker::DIECloner::cloneDIE(const DWARFDie &InputDIE,
17181733
Flags |= TF_SkipPC;
17191734
}
17201735

1736+
std::optional<StringRef> LibraryInstallName =
1737+
ObjFile.Addresses->getLibraryInstallName();
1738+
SmallVector<AttributeLinkedOffsetFixup> AttributesFixups;
17211739
for (const auto &AttrSpec : Abbrev->attributes()) {
17221740
if (shouldSkipAttribute(Update, AttrSpec, Flags & TF_SkipPC)) {
17231741
DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset,
17241742
U.getFormParams());
17251743
continue;
17261744
}
17271745

1746+
AttributeLinkedOffsetFixup CurAttrFixup;
1747+
CurAttrFixup.InputAttrStartOffset = InputDIE.getOffset() + Offset;
1748+
CurAttrFixup.LinkedOffsetFixupVal =
1749+
Unit.getStartOffset() + OutOffset - CurAttrFixup.InputAttrStartOffset;
1750+
17281751
DWARFFormValue Val = AttrSpec.getFormValue();
17291752
uint64_t AttrSize = Offset;
17301753
Val.extractValue(Data, &Offset, U.getFormParams(), &U);
1754+
CurAttrFixup.InputAttrEndOffset = InputDIE.getOffset() + Offset;
17311755
AttrSize = Offset - AttrSize;
17321756

1733-
OutOffset += cloneAttribute(*Die, InputDIE, File, Unit, Val, AttrSpec,
1734-
AttrSize, AttrInfo, IsLittleEndian);
1757+
uint64_t FinalAttrSize =
1758+
cloneAttribute(*Die, InputDIE, File, Unit, Val, AttrSpec, AttrSize,
1759+
AttrInfo, IsLittleEndian);
1760+
if (FinalAttrSize != 0 && ObjFile.Addresses->needToSaveValidRelocs())
1761+
AttributesFixups.push_back(CurAttrFixup);
1762+
1763+
OutOffset += FinalAttrSize;
17351764
}
17361765

1737-
// Look for accelerator entries.
17381766
uint16_t Tag = InputDIE.getTag();
1767+
// Add the DW_AT_APPLE_origin attribute to Compile Unit die if we have
1768+
// an install name and the DWARF doesn't have the attribute yet.
1769+
const bool NeedsAppleOrigin = (Tag == dwarf::DW_TAG_compile_unit) &&
1770+
LibraryInstallName.has_value() &&
1771+
!AttrInfo.HasAppleOrigin;
1772+
if (NeedsAppleOrigin) {
1773+
auto StringEntry = DebugStrPool.getEntry(LibraryInstallName.value());
1774+
Die->addValue(DIEAlloc, dwarf::Attribute(dwarf::DW_AT_APPLE_origin),
1775+
dwarf::DW_FORM_strp, DIEInteger(StringEntry.getOffset()));
1776+
AttrInfo.Name = StringEntry;
1777+
OutOffset += 4;
1778+
}
1779+
1780+
// Look for accelerator entries.
17391781
// FIXME: This is slightly wrong. An inline_subroutine without a
17401782
// low_pc, but with AT_ranges might be interesting to get into the
17411783
// accelerator tables too. For now stick with dsymutil's behavior.
@@ -1804,8 +1846,19 @@ DIE *DWARFLinker::DIECloner::cloneDIE(const DWARFDie &InputDIE,
18041846
Linker.assignAbbrev(NewAbbrev);
18051847
Die->setAbbrevNumber(NewAbbrev.getNumber());
18061848

1849+
uint64_t AbbrevNumberSize = getULEB128Size(Die->getAbbrevNumber());
1850+
18071851
// Add the size of the abbreviation number to the output offset.
1808-
OutOffset += getULEB128Size(Die->getAbbrevNumber());
1852+
OutOffset += AbbrevNumberSize;
1853+
1854+
// Update fixups with the size of the abbreviation number
1855+
for (AttributeLinkedOffsetFixup &F : AttributesFixups)
1856+
F.LinkedOffsetFixupVal += AbbrevNumberSize;
1857+
1858+
for (AttributeLinkedOffsetFixup &F : AttributesFixups)
1859+
ObjFile.Addresses->updateAndSaveValidRelocs(
1860+
Unit.getOrigUnit().getVersion() >= 5, Unit.getOrigUnit().getOffset(),
1861+
F.LinkedOffsetFixupVal, F.InputAttrStartOffset, F.InputAttrEndOffset);
18091862

18101863
if (!HasChildren) {
18111864
// Update our size.

llvm/lib/TargetParser/Triple.cpp

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,36 @@ StringRef Triple::getArchTypeName(ArchType Kind) {
8989
llvm_unreachable("Invalid ArchType!");
9090
}
9191

92+
StringRef Triple::getArchName(ArchType Kind, SubArchType SubArch) {
93+
switch (Kind) {
94+
case Triple::mips:
95+
if (SubArch == MipsSubArch_r6)
96+
return "mipsisa32r6";
97+
break;
98+
case Triple::mipsel:
99+
if (SubArch == MipsSubArch_r6)
100+
return "mipsisa32r6el";
101+
break;
102+
case Triple::mips64:
103+
if (SubArch == MipsSubArch_r6)
104+
return "mipsisa64r6";
105+
break;
106+
case Triple::mips64el:
107+
if (SubArch == MipsSubArch_r6)
108+
return "mipsisa64r6el";
109+
break;
110+
case Triple::aarch64:
111+
if (SubArch == AArch64SubArch_arm64ec)
112+
return "arm64ec";
113+
if (SubArch == AArch64SubArch_arm64e)
114+
return "arm64e";
115+
break;
116+
default:
117+
break;
118+
}
119+
return getArchTypeName(Kind);
120+
}
121+
92122
StringRef Triple::getArchTypePrefix(ArchType Kind) {
93123
switch (Kind) {
94124
default:
@@ -1145,34 +1175,6 @@ StringRef Triple::getArchName() const {
11451175
return StringRef(Data).split('-').first; // Isolate first component
11461176
}
11471177

1148-
StringRef Triple::getArchName(ArchType Kind, SubArchType SubArch) const {
1149-
switch (Kind) {
1150-
case Triple::mips:
1151-
if (SubArch == MipsSubArch_r6)
1152-
return "mipsisa32r6";
1153-
break;
1154-
case Triple::mipsel:
1155-
if (SubArch == MipsSubArch_r6)
1156-
return "mipsisa32r6el";
1157-
break;
1158-
case Triple::mips64:
1159-
if (SubArch == MipsSubArch_r6)
1160-
return "mipsisa64r6";
1161-
break;
1162-
case Triple::mips64el:
1163-
if (SubArch == MipsSubArch_r6)
1164-
return "mipsisa64r6el";
1165-
break;
1166-
case Triple::aarch64:
1167-
if (SubArch == AArch64SubArch_arm64ec)
1168-
return "arm64ec";
1169-
break;
1170-
default:
1171-
break;
1172-
}
1173-
return getArchTypeName(Kind);
1174-
}
1175-
11761178
StringRef Triple::getVendorName() const {
11771179
StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
11781180
return Tmp.split('-').first; // Isolate second component
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>English</string>
7+
<key>CFBundleIdentifier</key>
8+
<string>com.apple.xcode.dsym.bar-relink-variant.dylib</string>
9+
<key>CFBundleInfoDictionaryVersion</key>
10+
<string>6.0</string>
11+
<key>CFBundlePackageType</key>
12+
<string>dSYM</string>
13+
<key>CFBundleSignature</key>
14+
<string>????</string>
15+
<key>CFBundleShortVersionString</key>
16+
<string>1.0</string>
17+
<key>CFBundleVersion</key>
18+
<string>1</string>
19+
</dict>
20+
</plist>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
triple: 'arm64-apple-darwin'
3+
binary-path: bar-relink-variant.dylib
4+
relocations:
5+
- { offset: 0x26, size: 0x8, addend: 0x0, symName: _bar, symObjAddr: 0x0, symBinAddr: 0x3FA0, symSize: 0x8 }
6+
- { offset: 0x3F, size: 0x8, addend: 0x0, symName: _baz, symObjAddr: 0x8, symBinAddr: 0x4000, symSize: 0x0 }
7+
- { offset: 0x4F, size: 0x8, addend: 0x0, symName: _bar, symObjAddr: 0x0, symBinAddr: 0x3FA0, symSize: 0x8 }
8+
...
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>English</string>
7+
<key>CFBundleIdentifier</key>
8+
<string>com.apple.xcode.dsym.bar-relink.dylib</string>
9+
<key>CFBundleInfoDictionaryVersion</key>
10+
<string>6.0</string>
11+
<key>CFBundlePackageType</key>
12+
<string>dSYM</string>
13+
<key>CFBundleSignature</key>
14+
<string>????</string>
15+
<key>CFBundleShortVersionString</key>
16+
<string>1.0</string>
17+
<key>CFBundleVersion</key>
18+
<string>1</string>
19+
</dict>
20+
</plist>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
triple: 'arm64-apple-darwin'
3+
binary-path: bar-relink.dylib
4+
relocations:
5+
- { offset: 0x26, size: 0x8, addend: 0x0, symName: _bar, symObjAddr: 0x0, symBinAddr: 0x3FA0, symSize: 0x8 }
6+
- { offset: 0x3F, size: 0x8, addend: 0x0, symName: _baz, symObjAddr: 0x8, symBinAddr: 0x4000, symSize: 0x0 }
7+
- { offset: 0x4F, size: 0x8, addend: 0x0, symName: _bar, symObjAddr: 0x0, symBinAddr: 0x3FA0, symSize: 0x8 }
8+
...
Binary file not shown.
Binary file not shown.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>English</string>
7+
<key>CFBundleIdentifier</key>
8+
<string>com.apple.xcode.dsym.foo-relink-variant.dylib</string>
9+
<key>CFBundleInfoDictionaryVersion</key>
10+
<string>6.0</string>
11+
<key>CFBundlePackageType</key>
12+
<string>dSYM</string>
13+
<key>CFBundleSignature</key>
14+
<string>????</string>
15+
<key>CFBundleShortVersionString</key>
16+
<string>1.0</string>
17+
<key>CFBundleVersion</key>
18+
<string>1</string>
19+
</dict>
20+
</plist>

0 commit comments

Comments
 (0)