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