1//===- tools/dsymutil/DwarfLinkerForBinary.cpp ----------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "DwarfLinkerForBinary.h"
10#include "BinaryHolder.h"
11#include "DebugMap.h"
12#include "MachOUtils.h"
13#include "PseudoProbeLinker.h"
14#include "SwiftModule.h"
15#include "llvm/ADT/ArrayRef.h"
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/SmallString.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/ADT/StringSet.h"
21#include "llvm/ADT/Twine.h"
22#include "llvm/BinaryFormat/Dwarf.h"
23#include "llvm/BinaryFormat/MachO.h"
24#include "llvm/BinaryFormat/Swift.h"
25#include "llvm/CodeGen/AccelTable.h"
26#include "llvm/CodeGen/AsmPrinter.h"
27#include "llvm/CodeGen/DIE.h"
28#include "llvm/Config/config.h"
29#include "llvm/DWARFLinker/Classic/DWARFLinker.h"
30#include "llvm/DWARFLinker/Classic/DWARFStreamer.h"
31#include "llvm/DWARFLinker/Parallel/DWARFLinker.h"
32#include "llvm/DebugInfo/DIContext.h"
33#include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
34#include "llvm/DebugInfo/DWARF/DWARFContext.h"
35#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
36#include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
37#include "llvm/DebugInfo/DWARF/DWARFDie.h"
38#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
39#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
40#include "llvm/DebugInfo/DWARF/LowLevel/DWARFExpression.h"
41#include "llvm/MC/MCAsmBackend.h"
42#include "llvm/MC/MCAsmInfo.h"
43#include "llvm/MC/MCDwarf.h"
44#include "llvm/MC/MCSection.h"
45#include "llvm/MC/MCStreamer.h"
46#include "llvm/MC/MCTargetOptions.h"
47#include "llvm/MC/TargetRegistry.h"
48#include "llvm/Object/MachO.h"
49#include "llvm/Object/ObjectFile.h"
50#include "llvm/Object/SymbolicFile.h"
51#include "llvm/Support/Casting.h"
52#include "llvm/Support/Compiler.h"
53#include "llvm/Support/DataExtractor.h"
54#include "llvm/Support/Error.h"
55#include "llvm/Support/ErrorHandling.h"
56#include "llvm/Support/ErrorOr.h"
57#include "llvm/Support/FileSystem.h"
58#include "llvm/Support/Format.h"
59#include "llvm/Support/JSON.h"
60#include "llvm/Support/LEB128.h"
61#include "llvm/Support/MathExtras.h"
62#include "llvm/Support/MemoryBuffer.h"
63#include "llvm/Support/Path.h"
64#include "llvm/Support/VirtualFileSystem.h"
65#include "llvm/Support/WithColor.h"
66#include "llvm/Support/raw_ostream.h"
67#include "llvm/Target/TargetOptions.h"
68#include "llvm/TargetParser/Triple.h"
69#include <algorithm>
70#include <cassert>
71#include <cinttypes>
72#include <cstdint>
73#include <cstring>
74#include <limits>
75#include <memory>
76#include <optional>
77#include <string>
78#include <system_error>
79#include <tuple>
80#include <utility>
81#include <vector>
82
83namespace llvm {
84
85using namespace dwarf_linker;
86
87namespace dsymutil {
88
89static void dumpDIE(const DWARFDie *DIE, bool Verbose) {
90 if (!DIE || !Verbose)
91 return;
92
93 DIDumpOptions DumpOpts;
94 DumpOpts.ChildRecurseDepth = 0;
95 DumpOpts.Verbose = Verbose;
96
97 WithColor::note() << " in DIE:\n";
98 DIE->dump(OS&: errs(), indent: 6 /* Indent */, DumpOpts);
99}
100
101/// Report a warning to the user, optionally including information about a
102/// specific \p DIE related to the warning.
103void DwarfLinkerForBinary::reportWarning(Twine Warning, Twine Context,
104 const DWARFDie *DIE) const {
105 // FIXME: implement warning logging which does not block other threads.
106 if (ErrorHandlerMutex.try_lock()) {
107 warn(Warning, Context);
108 dumpDIE(DIE, Verbose: Options.Verbose);
109 ErrorHandlerMutex.unlock();
110 }
111}
112
113void DwarfLinkerForBinary::reportError(Twine Error, Twine Context,
114 const DWARFDie *DIE) const {
115 // FIXME: implement error logging which does not block other threads.
116 if (ErrorHandlerMutex.try_lock()) {
117 error(Error, Context);
118 dumpDIE(DIE, Verbose: Options.Verbose);
119 ErrorHandlerMutex.unlock();
120 }
121}
122
123ErrorOr<const object::ObjectFile &>
124DwarfLinkerForBinary::loadObject(const DebugMapObject &Obj,
125 const Triple &Triple) {
126 auto ObjectEntry =
127 BinHolder.getObjectEntry(Filename: Obj.getObjectFilename(), Timestamp: Obj.getTimestamp());
128 if (!ObjectEntry) {
129 auto Err = ObjectEntry.takeError();
130 reportWarning(Warning: Twine(Obj.getObjectFilename()) + ": " +
131 toStringWithoutConsuming(E: Err),
132 Context: Obj.getObjectFilename());
133 return errorToErrorCode(Err: std::move(Err));
134 }
135
136 auto Object = ObjectEntry->getObject(T: Triple);
137 if (!Object) {
138 auto Err = Object.takeError();
139 reportWarning(Warning: Twine(Obj.getObjectFilename()) + ": " +
140 toStringWithoutConsuming(E: Err),
141 Context: Obj.getObjectFilename());
142 return errorToErrorCode(Err: std::move(Err));
143 }
144
145 return *Object;
146}
147
148static Error remarksErrorHandler(const DebugMapObject &DMO,
149 DwarfLinkerForBinary &Linker,
150 std::unique_ptr<FileError> FE) {
151 bool IsArchive = DMO.getObjectFilename().ends_with(Suffix: ")");
152 // Don't report errors for missing remark files from static
153 // archives.
154 if (!IsArchive)
155 return Error(std::move(FE));
156
157 std::string Message = FE->message();
158 Error E = FE->takeError();
159 Error NewE = handleErrors(E: std::move(E), Hs: [&](std::unique_ptr<ECError> EC) {
160 if (EC->convertToErrorCode() != std::errc::no_such_file_or_directory)
161 return Error(std::move(EC));
162
163 Linker.reportWarning(Warning: Message, Context: DMO.getObjectFilename());
164 return Error(Error::success());
165 });
166
167 if (!NewE)
168 return Error::success();
169
170 return createFileError(F: FE->getFileName(), E: std::move(NewE));
171}
172Error DwarfLinkerForBinary::emitRelocations(
173 const DebugMap &DM, std::vector<ObjectWithRelocMap> &ObjectsForLinking) {
174 // Return early if the "Resources" directory is not being written to.
175 if (!Options.ResourceDir)
176 return Error::success();
177
178 StringRef ArchName = Triple::getArchName(Kind: DM.getTriple().getArch(),
179 SubArch: DM.getTriple().getSubArch());
180 std::string FileName =
181 (sys::path::filename(path: DM.getBinaryPath()) + ".yml").str();
182
183 // A dSYM being updated has no relocations, its addresses being final, so it
184 // keeps the relocation map it has.
185 SmallString<128> InputPath;
186 if (Options.Update) {
187 InputPath =
188 sys::path::parent_path(path: sys::path::parent_path(path: DM.getBinaryPath()));
189 sys::path::append(path&: InputPath, a: "Relocations", b: ArchName, c: FileName);
190 if (!sys::fs::exists(Path: InputPath))
191 return Error::success();
192 }
193
194 SmallString<128> Path;
195 // Create the "Relocations" directory in the "Resources" directory, and
196 // create an architecture-specific directory in the "Relocations" directory.
197 sys::path::append(path&: Path, a: *Options.ResourceDir, b: "Relocations", c: ArchName);
198 if (std::error_code EC = sys::fs::create_directories(path: Path.str(), IgnoreExisting: true,
199 Perms: sys::fs::perms::all_all))
200 return errorCodeToError(EC);
201
202 // Append the file name.
203 sys::path::append(path&: Path, a: FileName);
204
205 if (Options.Update) {
206 bool IsInput = false;
207 if (sys::fs::exists(Path) &&
208 !sys::fs::equivalent(A: InputPath, B: Path, result&: IsInput) && IsInput)
209 return Error::success();
210 return errorCodeToError(EC: sys::fs::copy_file(From: InputPath, To: Path));
211 }
212
213 RelocationMap RM(DM.getTriple(), DM.getBinaryPath());
214 for (auto &Obj : ObjectsForLinking) {
215 if (!Obj.OutRelocs->isInitialized())
216 continue;
217 Obj.OutRelocs->addValidRelocs(RM);
218 }
219
220 std::error_code EC;
221 raw_fd_ostream OS(Path.str(), EC, sys::fs::OF_Text);
222 if (EC)
223 return errorCodeToError(EC);
224
225 RM.print(OS);
226 return Error::success();
227}
228
229static Error emitRemarks(const LinkOptions &Options, StringRef BinaryPath,
230 StringRef ArchName, const remarks::RemarkLinker &RL) {
231 // Make sure we don't create the directories and the file if there is nothing
232 // to serialize.
233 if (RL.empty())
234 return Error::success();
235
236 SmallString<128> Path;
237 // Create the "Remarks" directory in the "Resources" directory.
238 sys::path::append(path&: Path, a: *Options.ResourceDir, b: "Remarks");
239 if (std::error_code EC = sys::fs::create_directories(path: Path.str(), IgnoreExisting: true,
240 Perms: sys::fs::perms::all_all))
241 return errorCodeToError(EC);
242
243 // Append the file name.
244 // For fat binaries, also append a dash and the architecture name.
245 sys::path::append(path&: Path, a: sys::path::filename(path: BinaryPath));
246 if (Options.NumDebugMaps > 1) {
247 // More than one debug map means we have a fat binary.
248 Path += '-';
249 Path += ArchName;
250 }
251
252 std::error_code EC;
253 raw_fd_ostream OS(Options.NoOutput ? "-" : Path.str(), EC,
254 Options.RemarksFormat == remarks::Format::Bitstream
255 ? sys::fs::OF_None
256 : sys::fs::OF_Text);
257 if (EC)
258 return errorCodeToError(EC);
259
260 if (Error E = RL.serialize(OS, RemarksFormat: Options.RemarksFormat))
261 return E;
262
263 return Error::success();
264}
265
266ErrorOr<std::unique_ptr<DWARFFile>> DwarfLinkerForBinary::loadObject(
267 const DebugMapObject &Obj, const DebugMap &DebugMap,
268 remarks::RemarkLinker &RL, PseudoProbeLinker &PL,
269 std::shared_ptr<DwarfLinkerForBinaryRelocationMap> DLBRM) {
270 auto ErrorOrObj = loadObject(Obj, Triple: DebugMap.getTriple());
271 std::unique_ptr<DWARFFile> Res;
272
273 if (ErrorOrObj) {
274 auto Context = DWARFContext::create(
275 Obj: *ErrorOrObj, RelocAction: DWARFContext::ProcessDebugRelocations::Process, L: nullptr,
276 DWPName: "",
277 RecoverableErrorHandler: [&](Error Err) {
278 handleAllErrors(E: std::move(Err), Handlers: [&](ErrorInfoBase &Info) {
279 reportError(Error: Info.message());
280 });
281 },
282 WarningHandler: [&](Error Warning) {
283 handleAllErrors(E: std::move(Warning), Handlers: [&](ErrorInfoBase &Info) {
284 reportWarning(Warning: Info.message());
285 });
286 });
287 DLBRM->init(Context&: *Context);
288 Res = std::make_unique<DWARFFile>(
289 args: Obj.getObjectFilename(), args: std::move(Context),
290 args: std::make_unique<AddressManager>(args&: *this, args: *ErrorOrObj, args: Obj, args&: DLBRM),
291 args: [&](StringRef FileName) { BinHolder.eraseObjectEntry(Filename: FileName); });
292
293 if (Error E = PL.collect(Obj: *ErrorOrObj))
294 reportWarning(Warning: toString(E: std::move(E)), Context: Obj.getObjectFilename());
295
296 Error E = RL.link(Obj: *ErrorOrObj);
297 // FIXME: Remark parsing errors are not propagated to the user.
298 if (Error NewE = handleErrors(
299 E: std::move(E), Hs: [&](std::unique_ptr<FileError> EC) -> Error {
300 return remarksErrorHandler(DMO: Obj, Linker&: *this, FE: std::move(EC));
301 }))
302 return errorToErrorCode(Err: std::move(NewE));
303
304 return std::move(Res);
305 }
306
307 return ErrorOrObj.getError();
308}
309
310static bool binaryHasStrippableSwiftReflectionSections(
311 const DebugMap &Map, const LinkOptions &Options, BinaryHolder &BinHolder) {
312 // If the input binary has strippable swift5 reflection sections, there is no
313 // need to copy them to the .dSYM. Only copy them for binaries where the
314 // linker omitted the reflection metadata.
315 if (!Map.getBinaryPath().empty() &&
316 Options.FileType == DWARFLinkerBase::OutputFileType::Object) {
317
318 auto ObjectEntry = BinHolder.getObjectEntry(Filename: Map.getBinaryPath());
319 // If ObjectEntry or Object has an error, no binary exists, therefore no
320 // reflection sections exist.
321 if (!ObjectEntry) {
322 // Any errors will be diagnosed later in the main loop, ignore them here.
323 llvm::consumeError(Err: ObjectEntry.takeError());
324 return false;
325 }
326
327 auto Object =
328 ObjectEntry->getObjectAs<object::MachOObjectFile>(T: Map.getTriple());
329 if (!Object) {
330 // Any errors will be diagnosed later in the main loop, ignore them here.
331 llvm::consumeError(Err: Object.takeError());
332 return false;
333 }
334
335 for (auto &Section : Object->sections()) {
336 llvm::Expected<llvm::StringRef> NameOrErr =
337 Object->getSectionName(Sec: Section.getRawDataRefImpl());
338 if (!NameOrErr) {
339 llvm::consumeError(Err: NameOrErr.takeError());
340 continue;
341 }
342 NameOrErr->consume_back(Suffix: "__TEXT");
343 auto ReflectionSectionKind =
344 Object->mapReflectionSectionNameToEnumValue(SectionName: *NameOrErr);
345 if (Object->isReflectionSectionStrippable(ReflectionSectionKind)) {
346 return true;
347 }
348 }
349 }
350 return false;
351}
352
353/// Calculate the start of the strippable swift reflection sections in Dwarf.
354/// Note that there's an assumption that the reflection sections will appear
355/// in alphabetic order.
356static std::vector<uint64_t>
357calculateStartOfStrippableReflectionSections(const DebugMap &Map) {
358 using llvm::binaryformat::Swift5ReflectionSectionKind;
359 uint64_t AssocTySize = 0;
360 uint64_t FieldMdSize = 0;
361 for (const auto &Obj : Map.objects()) {
362 auto OF =
363 llvm::object::ObjectFile::createObjectFile(ObjectPath: Obj->getObjectFilename());
364 if (!OF) {
365 llvm::consumeError(Err: OF.takeError());
366 continue;
367 }
368 if (auto *MO = dyn_cast<llvm::object::MachOObjectFile>(Val: OF->getBinary())) {
369 for (auto &Section : MO->sections()) {
370 llvm::Expected<llvm::StringRef> NameOrErr =
371 MO->getSectionName(Sec: Section.getRawDataRefImpl());
372 if (!NameOrErr) {
373 llvm::consumeError(Err: NameOrErr.takeError());
374 continue;
375 }
376 NameOrErr->consume_back(Suffix: "__TEXT");
377 auto ReflSectionKind =
378 MO->mapReflectionSectionNameToEnumValue(SectionName: *NameOrErr);
379 switch (ReflSectionKind) {
380 case Swift5ReflectionSectionKind::assocty:
381 AssocTySize += Section.getSize();
382 break;
383 case Swift5ReflectionSectionKind::fieldmd:
384 FieldMdSize += Section.getSize();
385 break;
386 default:
387 break;
388 }
389 }
390 }
391 }
392 // Initialize the vector with enough space to fit every reflection section
393 // kind.
394 std::vector<uint64_t> SectionToOffset(Swift5ReflectionSectionKind::last, 0);
395 SectionToOffset[Swift5ReflectionSectionKind::assocty] = 0;
396 SectionToOffset[Swift5ReflectionSectionKind::fieldmd] =
397 llvm::alignTo(Value: AssocTySize, Align: 4);
398 SectionToOffset[Swift5ReflectionSectionKind::reflstr] = llvm::alignTo(
399 Value: SectionToOffset[Swift5ReflectionSectionKind::fieldmd] + FieldMdSize, Align: 4);
400
401 return SectionToOffset;
402}
403
404void DwarfLinkerForBinary::collectRelocationsToApplyToSwiftReflectionSections(
405 const object::SectionRef &Section, StringRef &Contents,
406 const llvm::object::MachOObjectFile *MO,
407 const std::vector<uint64_t> &SectionToOffsetInDwarf,
408 const llvm::dsymutil::DebugMapObject *Obj,
409 std::vector<MachOUtils::DwarfRelocationApplicationInfo> &RelocationsToApply)
410 const {
411 for (auto It = Section.relocation_begin(); It != Section.relocation_end();
412 ++It) {
413 object::DataRefImpl RelocDataRef = It->getRawDataRefImpl();
414 MachO::any_relocation_info MachOReloc = MO->getRelocation(Rel: RelocDataRef);
415
416 if (!object::MachOObjectFile::isMachOPairedReloc(
417 RelocType: MO->getAnyRelocationType(RE: MachOReloc), Arch: MO->getArch())) {
418 reportWarning(
419 Warning: "Unimplemented relocation type in strippable reflection section ",
420 Context: Obj->getObjectFilename());
421 continue;
422 }
423
424 auto CalculateAddressOfSymbolInDwarfSegment =
425 [&]() -> std::optional<int64_t> {
426 auto Symbol = It->getSymbol();
427 auto SymbolAbsoluteAddress = Symbol->getAddress();
428 if (!SymbolAbsoluteAddress)
429 return {};
430 auto Section = Symbol->getSection();
431 if (!Section) {
432 llvm::consumeError(Err: Section.takeError());
433 return {};
434 }
435
436 if ((*Section)->getObject()->section_end() == *Section)
437 return {};
438
439 auto SectionStart = (*Section)->getAddress();
440 auto SymbolAddressInSection = *SymbolAbsoluteAddress - SectionStart;
441 auto SectionName = (*Section)->getName();
442 if (!SectionName)
443 return {};
444 auto ReflSectionKind =
445 MO->mapReflectionSectionNameToEnumValue(SectionName: *SectionName);
446
447 int64_t SectionStartInLinkedBinary =
448 SectionToOffsetInDwarf[ReflSectionKind];
449
450 auto Addr = SectionStartInLinkedBinary + SymbolAddressInSection;
451 return Addr;
452 };
453
454 // The first symbol should always be in the section we're currently
455 // iterating over.
456 auto FirstSymbolAddress = CalculateAddressOfSymbolInDwarfSegment();
457 ++It;
458
459 bool ShouldSubtractDwarfVM = false;
460 // For the second symbol there are two possibilities.
461 std::optional<int64_t> SecondSymbolAddress;
462 auto Sym = It->getSymbol();
463 if (Sym != MO->symbol_end()) {
464 Expected<StringRef> SymbolName = Sym->getName();
465 if (SymbolName) {
466 if (const auto *Mapping = Obj->lookupSymbol(SymbolName: *SymbolName)) {
467 // First possibility: the symbol exists in the binary, and exists in a
468 // non-strippable section (for example, typeref, or __TEXT,__const),
469 // in which case we look up its address in the binary, which dsymutil
470 // will copy verbatim.
471 SecondSymbolAddress = Mapping->getValue().BinaryAddress;
472 // Since the symbols live in different segments, we have to substract
473 // the start of the Dwarf's vmaddr so the value calculated points to
474 // the correct place.
475 ShouldSubtractDwarfVM = true;
476 }
477 }
478 }
479
480 if (!SecondSymbolAddress) {
481 // Second possibility, this symbol is not present in the main binary, and
482 // must be in one of the strippable sections (for example, reflstr).
483 // Calculate its address in the same way as we did the first one.
484 SecondSymbolAddress = CalculateAddressOfSymbolInDwarfSegment();
485 }
486
487 if (!FirstSymbolAddress || !SecondSymbolAddress)
488 continue;
489
490 auto SectionName = Section.getName();
491 if (!SectionName)
492 continue;
493
494 int32_t Addend;
495 memcpy(dest: &Addend, src: Contents.data() + It->getOffset(), n: sizeof(int32_t));
496 int32_t Value = (*SecondSymbolAddress + Addend) - *FirstSymbolAddress;
497 auto ReflSectionKind =
498 MO->mapReflectionSectionNameToEnumValue(SectionName: *SectionName);
499 uint64_t AddressFromDwarfVM =
500 SectionToOffsetInDwarf[ReflSectionKind] + It->getOffset();
501 RelocationsToApply.emplace_back(args&: AddressFromDwarfVM, args&: Value,
502 args&: ShouldSubtractDwarfVM);
503 }
504}
505
506std::shared_ptr<PrefixMapper>
507DwarfLinkerForBinary::findCompilationPrefixMap(StringRef ObjectPath) {
508 // With compilation caching, the compiler can prefix map the paths it sees,
509 // so that cache keys don't depend on where the build happens. The build
510 // system then records the mapping in compilation-prefix-map.json in a parent
511 // directory of the object files, as a JSON object from each remapped prefix
512 // to the original one.
513 StringRef ObjectDir = sys::path::parent_path(path: ObjectPath);
514 auto [It, Inserted] = CompilationPrefixMaps.try_emplace(Key: ObjectDir);
515 if (!Inserted)
516 return It->second;
517
518 std::optional<std::string> Path = vfs::findFileInParentDirectories(
519 FS&: *Options.VFS, Dir: ObjectDir, FileNames: {"compilation-prefix-map.json"});
520 if (!Path)
521 return nullptr;
522
523 if (Options.Verbose)
524 outs() << "found compilation prefix map " << *Path << '\n';
525 auto BufferOrErr = Options.VFS->getBufferForFile(Name: *Path);
526 if (!BufferOrErr) {
527 reportWarning(Warning: "cannot read compilation prefix map: " +
528 BufferOrErr.getError().message(),
529 Context: *Path);
530 return nullptr;
531 }
532 Expected<json::Value> Root = json::parse(JSON: (*BufferOrErr)->getBuffer());
533 if (!Root) {
534 reportWarning(Warning: "cannot parse compilation prefix map: " +
535 toString(E: Root.takeError()),
536 Context: *Path);
537 return nullptr;
538 }
539 const json::Object *Entries = Root->getAsObject();
540 if (!Entries) {
541 reportWarning(Warning: "cannot parse compilation prefix map: expected a JSON "
542 "object",
543 Context: *Path);
544 return nullptr;
545 }
546 auto PrefixMap = std::make_shared<PrefixMapper>();
547 for (const auto &Entry : *Entries)
548 if (std::optional<StringRef> Original = Entry.second.getAsString())
549 PrefixMap->add(MP: MappedPrefix(Entry.first, *Original));
550 PrefixMap->sort();
551 return It->second = std::move(PrefixMap);
552}
553
554Error DwarfLinkerForBinary::copySwiftInterfaces(StringRef Architecture) const {
555 std::error_code EC;
556 SmallString<128> InputPath;
557 SmallString<128> Path;
558 sys::path::append(path&: Path, a: *Options.ResourceDir, b: "Swift", c: Architecture);
559 if ((EC = sys::fs::create_directories(path: Path.str(), IgnoreExisting: true,
560 Perms: sys::fs::perms::all_all)))
561 return make_error<StringError>(
562 Args: "cannot create directory: " + toString(E: errorCodeToError(EC)), Args&: EC);
563 unsigned BaseLength = Path.size();
564
565 for (auto &I : ParseableSwiftInterfaces) {
566 StringRef ModuleName = I.first;
567 StringRef InterfaceFile = I.second;
568 if (!Options.PrependPath.empty()) {
569 InputPath.clear();
570 sys::path::append(path&: InputPath, a: Options.PrependPath, b: InterfaceFile);
571 InterfaceFile = InputPath;
572 }
573 sys::path::append(path&: Path, a: ModuleName);
574 Path.append(RHS: ".swiftinterface");
575 if (Options.Verbose)
576 outs() << "copy parseable Swift interface " << InterfaceFile << " -> "
577 << Path.str() << '\n';
578
579 // copy_file attempts an APFS clone first, so this should be cheap.
580 if ((EC = sys::fs::copy_file(From: InterfaceFile, To: Path.str())))
581 reportWarning(Warning: Twine("cannot copy parseable Swift interface ") +
582 InterfaceFile + ": " + toString(E: errorCodeToError(EC)));
583 Path.resize(N: BaseLength);
584 }
585 return Error::success();
586}
587
588Error DwarfLinkerForBinary::copyEmbeddedResources() const {
589 if (!Options.ResourceDir || Options.EmbedResources.empty())
590 return Error::success();
591
592 auto copyOneFile = [&](StringRef SrcPath,
593 StringRef DstPath) -> std::error_code {
594 if (auto EC = sys::fs::create_directories(path: sys::path::parent_path(path: DstPath),
595 IgnoreExisting: true, Perms: sys::fs::perms::all_all))
596 return EC;
597
598 if (Options.Verbose)
599 outs() << "embed resource " << SrcPath << " -> " << DstPath << '\n';
600
601 return sys::fs::copy_file(From: SrcPath, To: DstPath);
602 };
603
604 for (const auto &Entry : Options.EmbedResources) {
605 StringRef Dst = Entry.first();
606 StringRef Src = Entry.second;
607 bool IsDir = false;
608 if (auto EC = sys::fs::is_directory(path: Src, result&: IsDir))
609 return make_error<StringError>(Args: "cannot embed resource " + Src + ": " +
610 toString(E: errorCodeToError(EC)),
611 Args&: EC);
612
613 if (IsDir) {
614 std::error_code EC;
615 for (sys::fs::recursive_directory_iterator I(Src, EC), E; I != E && !EC;
616 I.increment(ec&: EC)) {
617 if (I->type() == sys::fs::file_type::directory_file)
618 continue;
619 StringRef FilePath = I->path();
620 StringRef Relative = FilePath.substr(Start: StringRef(Src).size());
621 if (!Relative.empty() && sys::path::is_separator(value: Relative.front()))
622 Relative = Relative.drop_front();
623 SmallString<128> DestPath;
624 sys::path::append(path&: DestPath, a: *Options.ResourceDir, b: Dst, c: Relative);
625 if (auto CopyEC = copyOneFile(FilePath, DestPath))
626 return make_error<StringError>(Args: "cannot embed resource " + FilePath +
627 ": " +
628 toString(E: errorCodeToError(EC: CopyEC)),
629 Args&: CopyEC);
630 }
631 if (EC)
632 return make_error<StringError>(Args: "cannot read directory " + Src + ": " +
633 toString(E: errorCodeToError(EC)),
634 Args&: EC);
635 } else {
636 SmallString<128> DestPath;
637 sys::path::append(path&: DestPath, a: *Options.ResourceDir, b: Dst);
638 if (auto EC = copyOneFile(Src, DestPath))
639 return make_error<StringError>(Args: "cannot embed resource " + Src + ": " +
640 toString(E: errorCodeToError(EC)),
641 Args&: EC);
642 }
643 }
644 return Error::success();
645}
646
647void DwarfLinkerForBinary::copySwiftReflectionMetadata(
648 const llvm::dsymutil::DebugMapObject *Obj, classic::DwarfStreamer *Streamer,
649 std::vector<uint64_t> &SectionToOffsetInDwarf,
650 std::vector<MachOUtils::DwarfRelocationApplicationInfo>
651 &RelocationsToApply) {
652 using binaryformat::Swift5ReflectionSectionKind;
653 auto OF =
654 llvm::object::ObjectFile::createObjectFile(ObjectPath: Obj->getObjectFilename());
655 if (!OF) {
656 llvm::consumeError(Err: OF.takeError());
657 return;
658 }
659 if (auto *MO = dyn_cast<llvm::object::MachOObjectFile>(Val: OF->getBinary())) {
660 // Collect the swift reflection sections before emitting them. This is
661 // done so we control the order they're emitted.
662 std::array<std::optional<object::SectionRef>,
663 Swift5ReflectionSectionKind::last + 1>
664 SwiftSections;
665 for (auto &Section : MO->sections()) {
666 llvm::Expected<llvm::StringRef> NameOrErr =
667 MO->getSectionName(Sec: Section.getRawDataRefImpl());
668 if (!NameOrErr) {
669 llvm::consumeError(Err: NameOrErr.takeError());
670 continue;
671 }
672 NameOrErr->consume_back(Suffix: "__TEXT");
673 auto ReflSectionKind =
674 MO->mapReflectionSectionNameToEnumValue(SectionName: *NameOrErr);
675 if (MO->isReflectionSectionStrippable(ReflectionSectionKind: ReflSectionKind))
676 SwiftSections[ReflSectionKind] = Section;
677 }
678 // Make sure we copy the sections in alphabetic order.
679 auto SectionKindsToEmit = {Swift5ReflectionSectionKind::assocty,
680 Swift5ReflectionSectionKind::fieldmd,
681 Swift5ReflectionSectionKind::reflstr};
682 for (auto SectionKind : SectionKindsToEmit) {
683 if (!SwiftSections[SectionKind])
684 continue;
685 auto &Section = *SwiftSections[SectionKind];
686 llvm::Expected<llvm::StringRef> SectionContents = Section.getContents();
687 if (!SectionContents)
688 continue;
689 const auto *MO =
690 llvm::cast<llvm::object::MachOObjectFile>(Val: Section.getObject());
691 collectRelocationsToApplyToSwiftReflectionSections(
692 Section, Contents&: *SectionContents, MO, SectionToOffsetInDwarf, Obj,
693 RelocationsToApply);
694 // Update the section start with the current section's contribution, so
695 // the next section we copy from a different .o file points to the correct
696 // place.
697 SectionToOffsetInDwarf[SectionKind] += Section.getSize();
698 Streamer->emitSwiftReflectionSection(ReflSectionKind: SectionKind, Buffer: *SectionContents,
699 Alignment: Section.getAlignment().value(),
700 Size: Section.getSize());
701 }
702 }
703}
704
705bool DwarfLinkerForBinary::link(const DebugMap &Map) {
706 if (Options.DWARFLinkerType == DsymutilDWARFLinkerType::Parallel)
707 return linkImpl<parallel::DWARFLinker>(Map, ObjectType: Options.FileType);
708
709 return linkImpl<classic::DWARFLinker>(Map, ObjectType: Options.FileType);
710}
711
712template <typename Linker>
713void setAcceleratorTables(Linker &GeneralLinker,
714 DsymutilAccelTableKind TableKind,
715 uint16_t MaxDWARFVersion) {
716 switch (TableKind) {
717 case DsymutilAccelTableKind::Apple:
718 GeneralLinker.addAccelTableKind(Linker::AccelTableKind::Apple);
719 return;
720 case DsymutilAccelTableKind::Dwarf:
721 GeneralLinker.addAccelTableKind(Linker::AccelTableKind::DebugNames);
722 return;
723 case DsymutilAccelTableKind::Pub:
724 GeneralLinker.addAccelTableKind(Linker::AccelTableKind::Pub);
725 return;
726 case DsymutilAccelTableKind::Default:
727 if (MaxDWARFVersion >= 5)
728 GeneralLinker.addAccelTableKind(Linker::AccelTableKind::DebugNames);
729 else
730 GeneralLinker.addAccelTableKind(Linker::AccelTableKind::Apple);
731 return;
732 case DsymutilAccelTableKind::None:
733 // Nothing to do.
734 return;
735 }
736
737 llvm_unreachable("All cases handled above!");
738}
739
740template <typename Linker>
741bool DwarfLinkerForBinary::linkImpl(
742 const DebugMap &Map, typename Linker::OutputFileType ObjectType) {
743
744 std::vector<ObjectWithRelocMap> ObjectsForLinking;
745
746 DebugMap DebugMap(Map.getTriple(), Map.getBinaryPath());
747
748 std::unique_ptr<Linker> GeneralLinker = Linker::createLinker(
749 [&](const Twine &Error, StringRef Context, const DWARFDie *DIE) {
750 reportError(Error, Context, DIE);
751 },
752 [&](const Twine &Warning, StringRef Context, const DWARFDie *DIE) {
753 reportWarning(Warning, Context, DIE);
754 });
755
756 std::unique_ptr<classic::DwarfStreamer> Streamer;
757 if (!Options.NoOutput) {
758 if (Expected<std::unique_ptr<classic::DwarfStreamer>> StreamerOrErr =
759 classic::DwarfStreamer::createStreamer(
760 TheTriple: Map.getTriple(), FileType: ObjectType, OutFile,
761 Warning: [&](const Twine &Warning, StringRef Context,
762 const DWARFDie *DIE) {
763 reportWarning(Warning, Context, DIE);
764 }))
765 Streamer = std::move(*StreamerOrErr);
766 else {
767 handleAllErrors(StreamerOrErr.takeError(), [&](const ErrorInfoBase &EI) {
768 reportError(Error: EI.message(), Context: "dwarf streamer init");
769 });
770 return false;
771 }
772
773 if constexpr (std::is_same<Linker, parallel::DWARFLinker>::value) {
774 GeneralLinker->setOutputDWARFHandler(
775 Map.getTriple(),
776 [&](std::shared_ptr<parallel::SectionDescriptorBase> Section) {
777 Streamer->emitSectionContents(SecData: Section->getContents(),
778 SecKind: Section->getKind());
779 });
780 } else
781 GeneralLinker->setOutputDWARFEmitter(Streamer.get());
782 }
783
784 PseudoProbeLinker PL(Options);
785 remarks::RemarkLinker RL;
786 if (!Options.RemarksPrependPath.empty())
787 RL.setExternalFilePrependPath(Options.RemarksPrependPath);
788 RL.setKeepAllRemarks(Options.RemarksKeepAll);
789 GeneralLinker->setObjectPrefixMap(&Options.ObjectPrefixMap);
790
791 GeneralLinker->setVerbosity(Options.Verbose);
792 GeneralLinker->setStatistics(Options.Statistics);
793 GeneralLinker->setVerifyInputDWARF(Options.VerifyInputDWARF);
794 GeneralLinker->setNoODR(Options.NoODR);
795 GeneralLinker->setUpdateIndexTablesOnly(Options.Update);
796 GeneralLinker->setNumThreads(Options.Threads);
797 GeneralLinker->setPrependPath(Options.PrependPath);
798 GeneralLinker->setKeepFunctionForStatic(Options.KeepFunctionForStatic);
799 GeneralLinker->setThreadPool(ThreadPool);
800 GeneralLinker->setInputVerificationHandler(
801 [&](const DWARFFile &File, llvm::StringRef Output) {
802 std::lock_guard<std::mutex> Guard(ErrorHandlerMutex);
803 if (Options.Verbose)
804 errs() << Output;
805 warn(Warning: "input verification failed", Context: File.FileName);
806 HasVerificationErrors = true;
807 });
808 auto Loader = [&](StringRef ContainerName,
809 StringRef Path) -> ErrorOr<DWARFFile &> {
810 auto &Obj = DebugMap.addDebugMapObject(
811 ObjectFilePath: Path, Timestamp: sys::TimePoint<std::chrono::seconds>(), Type: MachO::N_OSO);
812
813 auto DLBRelocMap = std::make_shared<DwarfLinkerForBinaryRelocationMap>();
814 if (ErrorOr<std::unique_ptr<DWARFFile>> ErrorOrObj =
815 loadObject(Obj, DebugMap, RL, PL, DLBRM: DLBRelocMap)) {
816 ObjectsForLinking.emplace_back(args: std::move(*ErrorOrObj), args&: DLBRelocMap);
817 return *ObjectsForLinking.back().Object;
818 } else {
819 // Try and emit more helpful warnings by applying some heuristics.
820 StringRef ObjFile = ContainerName;
821 bool IsClangModule = sys::path::extension(path: Path) == ".pcm";
822 bool IsArchive = ObjFile.ends_with(Suffix: ")");
823
824 if (IsClangModule) {
825 StringRef ModuleCacheDir = sys::path::parent_path(path: Path);
826 if (sys::fs::exists(Path: ModuleCacheDir)) {
827 // If the module's parent directory exists, we assume that the
828 // module cache has expired and was pruned by clang. A more
829 // adventurous dsymutil would invoke clang to rebuild the module
830 // now.
831 if (!ModuleCacheHintDisplayed) {
832 WithColor::note()
833 << "The clang module cache may have expired since "
834 "this object file was built. Rebuilding the "
835 "object file will rebuild the module cache.\n";
836 ModuleCacheHintDisplayed = true;
837 }
838 } else if (IsArchive) {
839 // If the module cache directory doesn't exist at all and the
840 // object file is inside a static library, we assume that the
841 // static library was built on a different machine. We don't want
842 // to discourage module debugging for convenience libraries within
843 // a project though.
844 if (!ArchiveHintDisplayed) {
845 WithColor::note()
846 << "Linking a static library that was built with "
847 "-gmodules, but the module cache was not found. "
848 "Redistributable static libraries should never be "
849 "built with module debugging enabled. The debug "
850 "experience will be degraded due to incomplete "
851 "debug information.\n";
852 ArchiveHintDisplayed = true;
853 }
854 }
855 }
856
857 return ErrorOrObj.getError();
858 }
859
860 llvm_unreachable("Unhandled DebugMap object");
861 };
862 GeneralLinker->setSwiftInterfacesMap(&ParseableSwiftInterfaces);
863 bool ReflectionSectionsPresentInBinary = false;
864 // If there is no output specified, no point in checking the binary for swift5
865 // reflection sections.
866 if (!Options.NoOutput) {
867 ReflectionSectionsPresentInBinary =
868 binaryHasStrippableSwiftReflectionSections(Map, Options, BinHolder);
869 }
870
871 std::vector<MachOUtils::DwarfRelocationApplicationInfo> RelocationsToApply;
872 if (!Options.NoOutput && !ReflectionSectionsPresentInBinary) {
873 auto SectionToOffsetInDwarf =
874 calculateStartOfStrippableReflectionSections(Map);
875 for (const auto &Obj : Map.objects())
876 copySwiftReflectionMetadata(Obj: Obj.get(), Streamer: Streamer.get(),
877 SectionToOffsetInDwarf, RelocationsToApply);
878 }
879
880 uint16_t MaxDWARFVersion = 0;
881 std::function<void(const DWARFUnit &Unit)> OnCUDieLoaded =
882 [&MaxDWARFVersion](const DWARFUnit &Unit) {
883 MaxDWARFVersion = std::max(a: Unit.getVersion(), b: MaxDWARFVersion);
884 };
885
886 if (Options.ResourceDir) {
887 // Collect .cas-config files. The build system might put these
888 // anywhere in the build directory, so dsymutil scans all parent
889 // paths of each object file. Their contents is a JSON dictionary,
890 // so this loop aggregates them in a JSON array.
891 llvm::StringSet<> VisitedPaths;
892 std::string CASConfigs = "[\n";
893 raw_string_ostream CASConfigStream(CASConfigs);
894 bool First = true;
895 for (const auto &Obj : Map.objects()) {
896 StringRef ObjPath = Obj->getObjectFilename();
897 for (StringRef Dir = sys::path::parent_path(path: ObjPath); !Dir.empty();
898 Dir = sys::path::parent_path(path: Dir)) {
899 if (!VisitedPaths.insert(key: Dir).second)
900 break;
901
902 SmallString<256> CASConfigPath(Dir);
903 sys::path::append(path&: CASConfigPath, a: ".cas-config");
904 auto BufferOrErr = MemoryBuffer::getFile(Filename: CASConfigPath);
905 if (!BufferOrErr)
906 continue;
907
908 if (!First)
909 CASConfigStream << ",\n";
910 First = false;
911 CASConfigStream << (*BufferOrErr)->getBuffer().rtrim(Char: '\n');
912 }
913 }
914 CASConfigStream << "\n]\n";
915 if (!First) {
916 std::error_code EC;
917 SmallString<128> CASConfigsPath;
918 sys::path::append(path&: CASConfigsPath, a: *Options.ResourceDir);
919 EC = sys::fs::create_directories(path: CASConfigsPath.str(), IgnoreExisting: true,
920 Perms: sys::fs::perms::all_all);
921 if (EC) {
922 reportWarning(Warning: "could not create directory '" + CASConfigsPath +
923 "': " + EC.message());
924 } else {
925 sys::path::append(path&: CASConfigsPath, a: "CASConfigs.json");
926 raw_fd_ostream OS(CASConfigsPath.str(), EC, sys::fs::OF_Text);
927 if (EC)
928 reportWarning(Warning: "could not open '" + CASConfigsPath +
929 "': " + EC.message());
930 else
931 OS << CASConfigs;
932 }
933 }
934 }
935
936 llvm::StringSet<> SwiftModules;
937 for (const auto &Obj : Map.objects()) {
938 // N_AST objects (swiftmodule files) should get dumped directly into the
939 // appropriate DWARF section.
940 if (Obj->getType() == MachO::N_AST) {
941 if (Options.Verbose)
942 outs() << "DEBUG MAP OBJECT: " << Obj->getObjectFilename() << "\n";
943
944 StringRef File = Obj->getObjectFilename();
945 if (!SwiftModules.insert(key: File).second)
946 continue;
947
948 auto ErrorOrMem = MemoryBuffer::getFile(Filename: File);
949 if (!ErrorOrMem) {
950 reportWarning(Warning: "could not open '" + File + "'");
951 continue;
952 }
953 auto FromInterfaceOrErr =
954 IsBuiltFromSwiftInterface(data: (*ErrorOrMem)->getBuffer());
955 if (!FromInterfaceOrErr) {
956 reportWarning(Warning: "could not parse binary Swift module: " +
957 toString(E: FromInterfaceOrErr.takeError()),
958 Context: Obj->getObjectFilename());
959 // Only skip swiftmodules that could be parsed and are positively
960 // identified as textual. Do so only when the option allows.
961 } else if (*FromInterfaceOrErr &&
962 !Options.IncludeSwiftModulesFromInterface) {
963 if (Options.Verbose)
964 outs() << "Skipping compiled textual Swift interface: "
965 << Obj->getObjectFilename() << "\n";
966 continue;
967 }
968
969 sys::fs::file_status Stat;
970 if (auto Err = sys::fs::status(path: File, result&: Stat)) {
971 reportWarning(Warning: Err.message());
972 continue;
973 }
974 if (!Options.NoTimestamp) {
975 // The modification can have sub-second precision so we need to cast
976 // away the extra precision that's not present in the debug map.
977 auto ModificationTime =
978 std::chrono::time_point_cast<std::chrono::seconds>(
979 t: Stat.getLastModificationTime());
980 if (Obj->getTimestamp() != sys::TimePoint<>() &&
981 ModificationTime != Obj->getTimestamp()) {
982 // Not using the helper here as we can easily stream TimePoint<>.
983 WithColor::warning()
984 << File << ": timestamp mismatch between swift interface file ("
985 << sys::TimePoint<>(ModificationTime) << ") and debug map ("
986 << sys::TimePoint<>(Obj->getTimestamp()) << ")\n";
987 continue;
988 }
989 }
990
991 // Copy the module into the .swift_ast section.
992 if (!Options.NoOutput)
993 Streamer->emitSwiftAST(Buffer: (*ErrorOrMem)->getBuffer());
994
995 continue;
996 }
997
998 auto DLBRelocMap = std::make_shared<DwarfLinkerForBinaryRelocationMap>();
999 if (ErrorOr<std::unique_ptr<DWARFFile>> ErrorOrObj =
1000 loadObject(Obj: *Obj, DebugMap: Map, RL, PL, DLBRM: DLBRelocMap)) {
1001 (*ErrorOrObj)->PrefixMap =
1002 findCompilationPrefixMap(ObjectPath: Obj->getObjectFilename());
1003 ObjectsForLinking.emplace_back(args: std::move(*ErrorOrObj), args&: DLBRelocMap);
1004 GeneralLinker->addObjectFile(*ObjectsForLinking.back().Object, Loader,
1005 OnCUDieLoaded);
1006 } else {
1007 ObjectsForLinking.push_back(
1008 x: {std::make_unique<DWARFFile>(args: Obj->getObjectFilename(), args: nullptr,
1009 args: nullptr),
1010 DLBRelocMap});
1011 GeneralLinker->addObjectFile(*ObjectsForLinking.back().Object);
1012 }
1013 }
1014
1015 // If we haven't seen any CUs, pick an arbitrary valid Dwarf version anyway.
1016 if (MaxDWARFVersion == 0)
1017 MaxDWARFVersion = 3;
1018
1019 if (Error E = GeneralLinker->setTargetDWARFVersion(MaxDWARFVersion))
1020 return error(Error: toString(E: std::move(E)));
1021
1022 setAcceleratorTables<Linker>(*GeneralLinker, Options.TheAccelTableKind,
1023 MaxDWARFVersion);
1024
1025 // link debug info for loaded object files.
1026 if (Error E = GeneralLinker->link())
1027 return error(Error: toString(E: std::move(E)));
1028
1029 StringRef ArchName = Map.getTriple().getArchName();
1030 if (Error E = emitRemarks(Options, BinaryPath: Map.getBinaryPath(), ArchName, RL))
1031 return error(Error: toString(E: std::move(E)));
1032
1033 if (Error E = PL.emit(TheTriple: Map.getTriple()))
1034 return error(Error: toString(E: std::move(E)));
1035
1036 if (Options.NoOutput)
1037 return true;
1038
1039 if (Error E = emitRelocations(DM: Map, ObjectsForLinking))
1040 return error(Error: toString(E: std::move(E)));
1041
1042 if (Options.ResourceDir && !ParseableSwiftInterfaces.empty()) {
1043 StringRef ArchName = Triple::getArchTypeName(Kind: Map.getTriple().getArch());
1044 if (auto E = copySwiftInterfaces(Architecture: ArchName))
1045 return error(Error: toString(E: std::move(E)));
1046 }
1047
1048 if (auto E = copyEmbeddedResources())
1049 return error(Error: toString(E: std::move(E)));
1050
1051 auto MapTriple = Map.getTriple();
1052 if ((MapTriple.isOSDarwin() || MapTriple.isOSBinFormatMachO()) &&
1053 !Map.getBinaryPath().empty() &&
1054 ObjectType == Linker::OutputFileType::Object)
1055 return MachOUtils::generateDsymCompanion(
1056 VFS: Options.VFS, DM: Map, MS&: *Streamer->getAsmPrinter().OutStreamer, OutFile,
1057 RelocationsToApply, AllowSectionHeaderOffsetOverflow: Options.AllowSectionHeaderOffsetOverflow);
1058
1059 Streamer->finish();
1060 return true;
1061}
1062
1063static bool isDwarfSection(const object::MachOObjectFile &Obj,
1064 object::section_iterator Sec) {
1065 return Sec != Obj.section_end() &&
1066 Obj.getSectionFinalSegmentName(Sec: Sec->getRawDataRefImpl()) == "__DWARF";
1067}
1068
1069/// Iterate over the relocations of the given \p Section and
1070/// store the ones that correspond to debug map entries into the
1071/// ValidRelocs array.
1072void DwarfLinkerForBinary::AddressManager::findValidRelocsMachO(
1073 const object::SectionRef &Section, const object::MachOObjectFile &Obj,
1074 const DebugMapObject &DMO, std::vector<ValidReloc> &ValidRelocs) {
1075 Expected<StringRef> ContentsOrErr = Section.getContents();
1076 if (!ContentsOrErr) {
1077 consumeError(Err: ContentsOrErr.takeError());
1078 Linker.reportWarning(Warning: "error reading section", Context: DMO.getObjectFilename());
1079 return;
1080 }
1081 DataExtractor Data(*ContentsOrErr, Obj.isLittleEndian());
1082 bool SkipNext = false;
1083
1084 for (const object::RelocationRef &Reloc : Section.relocations()) {
1085 if (SkipNext) {
1086 SkipNext = false;
1087 continue;
1088 }
1089
1090 object::DataRefImpl RelocDataRef = Reloc.getRawDataRefImpl();
1091 MachO::any_relocation_info MachOReloc = Obj.getRelocation(Rel: RelocDataRef);
1092
1093 if (object::MachOObjectFile::isMachOPairedReloc(RelocType: Obj.getAnyRelocationType(RE: MachOReloc),
1094 Arch: Obj.getArch())) {
1095 SkipNext = true;
1096 Linker.reportWarning(Warning: "unsupported relocation in " + *Section.getName() +
1097 " section.",
1098 Context: DMO.getObjectFilename());
1099 continue;
1100 }
1101
1102 unsigned RelocSize = 1 << Obj.getAnyRelocationLength(RE: MachOReloc);
1103 uint64_t Offset64 = Reloc.getOffset();
1104 if ((RelocSize != 4 && RelocSize != 8)) {
1105 Linker.reportWarning(Warning: "unsupported relocation in " + *Section.getName() +
1106 " section.",
1107 Context: DMO.getObjectFilename());
1108 continue;
1109 }
1110 uint64_t OffsetCopy = Offset64;
1111 // Mach-o uses REL relocations, the addend is at the relocation offset.
1112 uint64_t Addend = Data.getUnsigned(offset_ptr: &OffsetCopy, byte_size: RelocSize);
1113 uint64_t SymAddress;
1114 int64_t SymOffset;
1115
1116 if (Obj.isRelocationScattered(RE: MachOReloc)) {
1117 // The address of the base symbol for scattered relocations is
1118 // stored in the reloc itself. The actual addend will store the
1119 // base address plus the offset.
1120 SymAddress = Obj.getScatteredRelocationValue(RE: MachOReloc);
1121 SymOffset = int64_t(Addend) - SymAddress;
1122 } else {
1123 SymAddress = Addend;
1124 SymOffset = 0;
1125 }
1126
1127 auto Sym = Reloc.getSymbol();
1128 if (Sym != Obj.symbol_end()) {
1129 Expected<StringRef> SymbolName = Sym->getName();
1130 if (!SymbolName) {
1131 consumeError(Err: SymbolName.takeError());
1132 Linker.reportWarning(Warning: "error getting relocation symbol name.",
1133 Context: DMO.getObjectFilename());
1134 continue;
1135 }
1136 if (const auto *Mapping = DMO.lookupSymbol(SymbolName: *SymbolName))
1137 ValidRelocs.emplace_back(args&: Offset64, args&: RelocSize, args&: Addend, args: Mapping->getKey(),
1138 args: Mapping->getValue());
1139 } else if (isDwarfSection(Obj, Sec: Obj.getRelocationSection(Rel: RelocDataRef))) {
1140 // A reference into a DWARF section holds an offset into that section,
1141 // not an object address, so it must not be matched against the debug
1142 // map. The linker rewrites these offsets itself.
1143 continue;
1144 } else if (const auto *Mapping = DMO.lookupObjectAddress(Address: SymAddress)) {
1145 // Do not store the addend. The addend was the address of the symbol in
1146 // the object file, the address in the binary that is stored in the debug
1147 // map doesn't need to be offset.
1148 ValidRelocs.emplace_back(args&: Offset64, args&: RelocSize, args&: SymOffset,
1149 args: Mapping->getKey(), args: Mapping->getValue());
1150 }
1151 }
1152}
1153
1154/// Dispatch the valid relocation finding logic to the
1155/// appropriate handler depending on the object file format.
1156bool DwarfLinkerForBinary::AddressManager::findValidRelocs(
1157 const object::SectionRef &Section, const object::ObjectFile &Obj,
1158 const DebugMapObject &DMO, std::vector<ValidReloc> &Relocs) {
1159 // Dispatch to the right handler depending on the file type.
1160 if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(Val: &Obj))
1161 findValidRelocsMachO(Section, Obj: *MachOObj, DMO, ValidRelocs&: Relocs);
1162 else
1163 Linker.reportWarning(Warning: Twine("unsupported object file type: ") +
1164 Obj.getFileName(),
1165 Context: DMO.getObjectFilename());
1166 if (Relocs.empty())
1167 return false;
1168
1169 // Sort the relocations by offset. We will walk the DIEs linearly in
1170 // the file, this allows us to just keep an index in the relocation
1171 // array that we advance during our walk, rather than resorting to
1172 // some associative container. See DwarfLinkerForBinary::NextValidReloc.
1173 llvm::sort(C&: Relocs);
1174 return true;
1175}
1176
1177/// Look for relocations in the debug_info and debug_addr section that match
1178/// entries in the debug map. These relocations will drive the Dwarf link by
1179/// indicating which DIEs refer to symbols present in the linked binary.
1180/// \returns whether there are any valid relocations in the debug info.
1181bool DwarfLinkerForBinary::AddressManager::findValidRelocsInDebugSections(
1182 const object::ObjectFile &Obj, const DebugMapObject &DMO) {
1183 // Find the debug_info section.
1184 bool FoundValidRelocs = false;
1185 for (const object::SectionRef &Section : Obj.sections()) {
1186 StringRef SectionName;
1187 if (Expected<StringRef> NameOrErr = Section.getName())
1188 SectionName = *NameOrErr;
1189 else
1190 consumeError(Err: NameOrErr.takeError());
1191
1192 SectionName = SectionName.substr(Start: SectionName.find_first_not_of(Chars: "._"));
1193 if (SectionName == "debug_info")
1194 FoundValidRelocs |=
1195 findValidRelocs(Section, Obj, DMO, Relocs&: ValidDebugInfoRelocs);
1196 if (SectionName == "debug_addr")
1197 FoundValidRelocs |=
1198 findValidRelocs(Section, Obj, DMO, Relocs&: ValidDebugAddrRelocs);
1199 }
1200 return FoundValidRelocs;
1201}
1202
1203std::vector<ValidReloc> DwarfLinkerForBinary::AddressManager::getRelocations(
1204 const std::vector<ValidReloc> &Relocs, uint64_t StartPos, uint64_t EndPos) {
1205 std::vector<ValidReloc> Res;
1206
1207 auto CurReloc = partition_point(Range: Relocs, P: [StartPos](const ValidReloc &Reloc) {
1208 return (uint64_t)Reloc.Offset < StartPos;
1209 });
1210
1211 while (CurReloc != Relocs.end() && CurReloc->Offset >= StartPos &&
1212 (uint64_t)CurReloc->Offset < EndPos) {
1213 Res.push_back(x: *CurReloc);
1214 CurReloc++;
1215 }
1216
1217 return Res;
1218}
1219
1220void DwarfLinkerForBinary::AddressManager::printReloc(const ValidReloc &Reloc) {
1221 const auto &Mapping = Reloc.SymbolMapping;
1222 const uint64_t ObjectAddress = Mapping.ObjectAddress
1223 ? uint64_t(*Mapping.ObjectAddress)
1224 : std::numeric_limits<uint64_t>::max();
1225
1226 outs() << "Found valid debug map entry: " << Reloc.SymbolName << "\t"
1227 << format(Fmt: "0x%016" PRIx64 " => 0x%016" PRIx64 "\n", Vals: ObjectAddress,
1228 Vals: uint64_t(Mapping.BinaryAddress));
1229}
1230
1231int64_t
1232DwarfLinkerForBinary::AddressManager::getRelocValue(const ValidReloc &Reloc) {
1233 int64_t AddrAdjust = relocate(Reloc);
1234 if (Reloc.SymbolMapping.ObjectAddress)
1235 AddrAdjust -= uint64_t(*Reloc.SymbolMapping.ObjectAddress);
1236 return AddrAdjust;
1237}
1238
1239std::optional<int64_t>
1240DwarfLinkerForBinary::AddressManager::hasValidRelocationAt(
1241 const std::vector<ValidReloc> &AllRelocs, uint64_t StartOffset,
1242 uint64_t EndOffset, bool Verbose) {
1243 std::vector<ValidReloc> Relocs =
1244 getRelocations(Relocs: AllRelocs, StartPos: StartOffset, EndPos: EndOffset);
1245 if (Relocs.size() == 0)
1246 return std::nullopt;
1247
1248 if (Verbose)
1249 printReloc(Reloc: Relocs[0]);
1250
1251 return getRelocValue(Reloc: Relocs[0]);
1252}
1253
1254/// Get the starting and ending (exclusive) offset for the
1255/// attribute with index \p Idx descibed by \p Abbrev. \p Offset is
1256/// supposed to point to the position of the first attribute described
1257/// by \p Abbrev.
1258/// \return [StartOffset, EndOffset) as a pair.
1259static std::pair<uint64_t, uint64_t>
1260getAttributeOffsets(const DWARFAbbreviationDeclaration *Abbrev, unsigned Idx,
1261 uint64_t Offset, const DWARFUnit &Unit) {
1262 DataExtractor Data = Unit.getDebugInfoExtractor();
1263
1264 for (unsigned I = 0; I < Idx; ++I)
1265 DWARFFormValue::skipValue(Form: Abbrev->getFormByIndex(idx: I), DebugInfoData: Data, OffsetPtr: &Offset,
1266 FormParams: Unit.getFormParams());
1267
1268 uint64_t End = Offset;
1269 DWARFFormValue::skipValue(Form: Abbrev->getFormByIndex(idx: Idx), DebugInfoData: Data, OffsetPtr: &End,
1270 FormParams: Unit.getFormParams());
1271
1272 return std::make_pair(x&: Offset, y&: End);
1273}
1274
1275std::optional<int64_t>
1276DwarfLinkerForBinary::AddressManager::getExprOpAddressRelocAdjustment(
1277 DWARFUnit &U, const DWARFExpression::Operation &Op, uint64_t StartOffset,
1278 uint64_t EndOffset, bool Verbose) {
1279 switch (Op.getCode()) {
1280 default: {
1281 assert(false && "Specified operation does not have address operand");
1282 } break;
1283 case dwarf::DW_OP_const2u:
1284 case dwarf::DW_OP_const4u:
1285 case dwarf::DW_OP_const8u:
1286 case dwarf::DW_OP_const2s:
1287 case dwarf::DW_OP_const4s:
1288 case dwarf::DW_OP_const8s:
1289 case dwarf::DW_OP_addr: {
1290 return hasValidRelocationAt(AllRelocs: ValidDebugInfoRelocs, StartOffset, EndOffset,
1291 Verbose);
1292 } break;
1293 case dwarf::DW_OP_constx:
1294 case dwarf::DW_OP_addrx: {
1295 return hasValidRelocationAt(AllRelocs: ValidDebugAddrRelocs, StartOffset, EndOffset,
1296 Verbose);
1297 } break;
1298 }
1299
1300 return std::nullopt;
1301}
1302
1303std::optional<int64_t>
1304DwarfLinkerForBinary::AddressManager::getSubprogramRelocAdjustment(
1305 const DWARFDie &DIE, bool Verbose) {
1306 const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
1307
1308 std::optional<uint32_t> LowPcIdx =
1309 Abbrev->findAttributeIndex(attr: dwarf::DW_AT_low_pc);
1310 if (!LowPcIdx)
1311 return std::nullopt;
1312
1313 dwarf::Form Form = Abbrev->getFormByIndex(idx: *LowPcIdx);
1314
1315 switch (Form) {
1316 case dwarf::DW_FORM_addr: {
1317 uint64_t Offset = DIE.getOffset() + getULEB128Size(Value: Abbrev->getCode());
1318 uint64_t LowPcOffset, LowPcEndOffset;
1319 std::tie(args&: LowPcOffset, args&: LowPcEndOffset) =
1320 getAttributeOffsets(Abbrev, Idx: *LowPcIdx, Offset, Unit: *DIE.getDwarfUnit());
1321 return hasValidRelocationAt(AllRelocs: ValidDebugInfoRelocs, StartOffset: LowPcOffset,
1322 EndOffset: LowPcEndOffset, Verbose);
1323 }
1324 case dwarf::DW_FORM_addrx:
1325 case dwarf::DW_FORM_addrx1:
1326 case dwarf::DW_FORM_addrx2:
1327 case dwarf::DW_FORM_addrx3:
1328 case dwarf::DW_FORM_addrx4: {
1329 std::optional<DWARFFormValue> AddrValue = DIE.find(Attr: dwarf::DW_AT_low_pc);
1330 if (std::optional<uint64_t> AddressOffset =
1331 DIE.getDwarfUnit()->getIndexedAddressOffset(
1332 Index: AddrValue->getRawUValue()))
1333 return hasValidRelocationAt(
1334 AllRelocs: ValidDebugAddrRelocs, StartOffset: *AddressOffset,
1335 EndOffset: *AddressOffset + DIE.getDwarfUnit()->getAddressByteSize(), Verbose);
1336
1337 Linker.reportWarning(Warning: "no base offset for address table", Context: SrcFileName);
1338 return std::nullopt;
1339 }
1340 default:
1341 return std::nullopt;
1342 }
1343}
1344
1345std::optional<StringRef>
1346DwarfLinkerForBinary::AddressManager::getLibraryInstallName() {
1347 return LibInstallName;
1348}
1349
1350uint64_t
1351DwarfLinkerForBinary::AddressManager::relocate(const ValidReloc &Reloc) const {
1352 return Reloc.SymbolMapping.BinaryAddress + Reloc.Addend;
1353}
1354
1355bool DwarfLinkerForBinary::AddressManager::updateAndSaveValidRelocs(
1356 RelocSectionKind InSection, RelocSectionKind OutSection,
1357 uint64_t OriginalUnitOffset, int64_t LinkedOffset, uint64_t StartOffset,
1358 uint64_t EndOffset) {
1359 std::vector<ValidReloc> InRelocs = getRelocations(
1360 Relocs: InSection == RelocSectionKind::DebugAddr ? ValidDebugAddrRelocs
1361 : ValidDebugInfoRelocs,
1362 StartPos: StartOffset, EndPos: EndOffset);
1363 DwarfLinkerRelocMap->updateAndSaveValidRelocs(
1364 OutSection, InRelocs, UnitOffset: OriginalUnitOffset, LinkedOffset);
1365 return !InRelocs.empty();
1366}
1367
1368/// Apply the valid relocations found by findValidRelocs() to
1369/// the buffer \p Data, taking into account that Data is at \p BaseOffset
1370/// in the debug_info section.
1371///
1372/// Like for findValidRelocs(), this function must be called with
1373/// monotonic \p BaseOffset values.
1374///
1375/// \returns whether any reloc has been applied.
1376bool DwarfLinkerForBinary::AddressManager::applyValidRelocs(
1377 MutableArrayRef<char> Data, uint64_t BaseOffset, bool IsLittleEndian) {
1378
1379 std::vector<ValidReloc> Relocs = getRelocations(
1380 Relocs: ValidDebugInfoRelocs, StartPos: BaseOffset, EndPos: BaseOffset + Data.size());
1381
1382 for (const ValidReloc &CurReloc : Relocs) {
1383 assert(CurReloc.Offset - BaseOffset < Data.size());
1384 assert(CurReloc.Offset - BaseOffset + CurReloc.Size <= Data.size());
1385 char Buf[8];
1386 uint64_t Value = relocate(Reloc: CurReloc);
1387 for (unsigned I = 0; I != CurReloc.Size; ++I) {
1388 unsigned Index = IsLittleEndian ? I : (CurReloc.Size - I - 1);
1389 Buf[I] = uint8_t(Value >> (Index * 8));
1390 }
1391 assert(CurReloc.Size <= sizeof(Buf));
1392 memcpy(dest: &Data[CurReloc.Offset - BaseOffset], src: Buf, n: CurReloc.Size);
1393 }
1394 return Relocs.size() > 0;
1395}
1396
1397void DwarfLinkerForBinaryRelocationMap::init(DWARFContext &Context) {
1398 for (const std::unique_ptr<DWARFUnit> &CU : Context.compile_units())
1399 Units.try_emplace(Key: CU->getOffset());
1400}
1401
1402void DwarfLinkerForBinaryRelocationMap::addValidRelocs(RelocationMap &RM) {
1403 std::vector<ValidReloc> DebugInfoRelocs;
1404 std::vector<ValidReloc> DebugAddrRelocs;
1405 for (const auto &[UnitOffset, Relocs] : Units) {
1406 llvm::append_range(C&: DebugInfoRelocs, R: Relocs.DebugInfo);
1407 llvm::append_range(C&: DebugAddrRelocs, R: Relocs.DebugAddr);
1408 }
1409
1410 // The units are unordered.
1411 llvm::stable_sort(Range&: DebugInfoRelocs);
1412 llvm::stable_sort(Range&: DebugAddrRelocs);
1413 for (const ValidReloc &R : DebugInfoRelocs)
1414 RM.addDebugInfoRelocationMapEntry(Relocation: R);
1415 for (const ValidReloc &R : DebugAddrRelocs)
1416 RM.addDebugAddrRelocationMapEntry(Relocation: R);
1417}
1418
1419void DwarfLinkerForBinaryRelocationMap::updateAndSaveValidRelocs(
1420 RelocSectionKind OutSection, const std::vector<ValidReloc> &InRelocs,
1421 uint64_t UnitOffset, int64_t LinkedOffset) {
1422 auto It = Units.find(Val: UnitOffset);
1423 assert(It != Units.end() && "relocations saved before init()");
1424 std::vector<ValidReloc> &OutRelocs = OutSection == RelocSectionKind::DebugAddr
1425 ? It->second.DebugAddr
1426 : It->second.DebugInfo;
1427
1428 for (const ValidReloc &R : InRelocs) {
1429 OutRelocs.emplace_back(args: R.Offset + LinkedOffset, args: R.Size, args: R.Addend,
1430 args: R.SymbolName, args: R.SymbolMapping);
1431 }
1432}
1433
1434} // namespace dsymutil
1435} // namespace llvm
1436