1 //===-- MachOUtils.cpp - Mach-o specific helpers for dsymutil ------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "MachOUtils.h"
10 #include "BinaryHolder.h"
12 #include "LinkUtils.h"
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/CodeGen/NonRelocatableStringpool.h"
15 #include "llvm/MC/MCAssembler.h"
16 #include "llvm/MC/MCMachObjectWriter.h"
17 #include "llvm/MC/MCObjectStreamer.h"
18 #include "llvm/MC/MCSectionMachO.h"
19 #include "llvm/MC/MCStreamer.h"
20 #include "llvm/MC/MCSubtargetInfo.h"
21 #include "llvm/Object/MachO.h"
22 #include "llvm/Support/FileUtilities.h"
23 #include "llvm/Support/Program.h"
24 #include "llvm/Support/WithColor.h"
25 #include "llvm/Support/raw_ostream.h"
29 namespace MachOUtils
{
31 llvm::Error
ArchAndFile::createTempFile() {
33 std::error_code EC
= sys::fs::createTemporaryFile("dsym", "dwarf", FD
, SS
);
36 return errorCodeToError(EC
);
40 return Error::success();
43 llvm::StringRef
ArchAndFile::getPath() const {
44 assert(!Path
.empty() && "path called before createTempFile");
48 int ArchAndFile::getFD() const {
49 assert((FD
!= -1) && "path called before createTempFile");
53 ArchAndFile::~ArchAndFile() {
55 sys::fs::remove(Path
);
58 std::string
getArchName(StringRef Arch
) {
59 if (Arch
.starts_with("thumb"))
60 return (llvm::Twine("arm") + Arch
.drop_front(5)).str();
61 return std::string(Arch
);
64 static bool runLipo(StringRef SDKPath
, SmallVectorImpl
<StringRef
> &Args
) {
65 auto Path
= sys::findProgramByName("lipo", ArrayRef(SDKPath
));
67 Path
= sys::findProgramByName("lipo");
70 WithColor::error() << "lipo: " << Path
.getError().message() << "\n";
76 sys::ExecuteAndWait(*Path
, Args
, std::nullopt
, {}, 0, 0, &ErrMsg
);
78 WithColor::error() << "lipo: " << ErrMsg
<< "\n";
85 bool generateUniversalBinary(SmallVectorImpl
<ArchAndFile
> &ArchFiles
,
86 StringRef OutputFileName
,
87 const LinkOptions
&Options
, StringRef SDKPath
,
89 // No need to merge one file into a universal fat binary.
90 if (ArchFiles
.size() == 1) {
91 llvm::StringRef TmpPath
= ArchFiles
.front().getPath();
92 if (auto EC
= sys::fs::rename(TmpPath
, OutputFileName
)) {
93 // If we can't rename, try to copy to work around cross-device link
95 EC
= sys::fs::copy_file(TmpPath
, OutputFileName
);
97 WithColor::error() << "while keeping " << TmpPath
<< " as "
98 << OutputFileName
<< ": " << EC
.message() << "\n";
101 sys::fs::remove(TmpPath
);
106 SmallVector
<StringRef
, 8> Args
;
107 Args
.push_back("lipo");
108 Args
.push_back("-create");
110 for (auto &Thin
: ArchFiles
)
111 Args
.push_back(Thin
.getPath());
113 // Align segments to match dsymutil-classic alignment.
114 for (auto &Thin
: ArchFiles
) {
115 Thin
.Arch
= getArchName(Thin
.Arch
);
116 Args
.push_back("-segalign");
117 Args
.push_back(Thin
.Arch
);
118 Args
.push_back("20");
121 // Use a 64-bit fat header if requested.
123 Args
.push_back("-fat64");
125 Args
.push_back("-output");
126 Args
.push_back(OutputFileName
.data());
128 if (Options
.Verbose
) {
129 outs() << "Running lipo\n";
130 for (auto Arg
: Args
)
131 outs() << ' ' << Arg
;
135 return Options
.NoOutput
? true : runLipo(SDKPath
, Args
);
138 // Return a MachO::segment_command_64 that holds the same values as the passed
139 // MachO::segment_command. We do that to avoid having to duplicate the logic
140 // for 32bits and 64bits segments.
141 struct MachO::segment_command_64
adaptFrom32bits(MachO::segment_command Seg
) {
142 MachO::segment_command_64 Seg64
;
144 Seg64
.cmdsize
= Seg
.cmdsize
;
145 memcpy(Seg64
.segname
, Seg
.segname
, sizeof(Seg
.segname
));
146 Seg64
.vmaddr
= Seg
.vmaddr
;
147 Seg64
.vmsize
= Seg
.vmsize
;
148 Seg64
.fileoff
= Seg
.fileoff
;
149 Seg64
.filesize
= Seg
.filesize
;
150 Seg64
.maxprot
= Seg
.maxprot
;
151 Seg64
.initprot
= Seg
.initprot
;
152 Seg64
.nsects
= Seg
.nsects
;
153 Seg64
.flags
= Seg
.flags
;
157 // Iterate on all \a Obj segments, and apply \a Handler to them.
158 template <typename FunctionTy
>
159 static void iterateOnSegments(const object::MachOObjectFile
&Obj
,
160 FunctionTy Handler
) {
161 for (const auto &LCI
: Obj
.load_commands()) {
162 MachO::segment_command_64 Segment
;
163 if (LCI
.C
.cmd
== MachO::LC_SEGMENT
)
164 Segment
= adaptFrom32bits(Obj
.getSegmentLoadCommand(LCI
));
165 else if (LCI
.C
.cmd
== MachO::LC_SEGMENT_64
)
166 Segment
= Obj
.getSegment64LoadCommand(LCI
);
174 // Transfer the symbols described by \a NList to \a NewSymtab which is just the
175 // raw contents of the symbol table for the dSYM companion file. \returns
176 // whether the symbol was transferred or not.
177 template <typename NListTy
>
178 static bool transferSymbol(NListTy NList
, bool IsLittleEndian
,
179 StringRef Strings
, SmallVectorImpl
<char> &NewSymtab
,
180 NonRelocatableStringpool
&NewStrings
,
182 // Do not transfer undefined symbols, we want real addresses.
183 if ((NList
.n_type
& MachO::N_TYPE
) == MachO::N_UNDF
)
186 // Do not transfer N_AST symbols as their content is copied into a section of
187 // the Mach-O companion file.
188 if (NList
.n_type
== MachO::N_AST
)
191 StringRef Name
= StringRef(Strings
.begin() + NList
.n_strx
);
193 // An N_SO with a filename opens a debugging scope and another one without a
194 // name closes it. Don't transfer anything in the debugging scope.
197 (NList
.n_type
!= MachO::N_SO
) || (!Name
.empty() && Name
[0] != '\0');
199 } else if (NList
.n_type
== MachO::N_SO
) {
204 // FIXME: The + 1 is here to mimic dsymutil-classic that has 2 empty
205 // strings at the start of the generated string table (There is
206 // corresponding code in the string table emission).
207 NList
.n_strx
= NewStrings
.getStringOffset(Name
) + 1;
208 if (IsLittleEndian
!= sys::IsLittleEndianHost
)
209 MachO::swapStruct(NList
);
211 NewSymtab
.append(reinterpret_cast<char *>(&NList
),
212 reinterpret_cast<char *>(&NList
+ 1));
216 // Wrapper around transferSymbol to transfer all of \a Obj symbols
217 // to \a NewSymtab. This function does not write in the output file.
218 // \returns the number of symbols in \a NewSymtab.
219 static unsigned transferSymbols(const object::MachOObjectFile
&Obj
,
220 SmallVectorImpl
<char> &NewSymtab
,
221 NonRelocatableStringpool
&NewStrings
) {
223 StringRef Strings
= Obj
.getStringTableData();
224 bool IsLittleEndian
= Obj
.isLittleEndian();
225 bool InDebugNote
= false;
228 for (const object::SymbolRef
&Symbol
: Obj
.symbols()) {
229 object::DataRefImpl DRI
= Symbol
.getRawDataRefImpl();
230 if (transferSymbol(Obj
.getSymbol64TableEntry(DRI
), IsLittleEndian
,
231 Strings
, NewSymtab
, NewStrings
, InDebugNote
))
235 for (const object::SymbolRef
&Symbol
: Obj
.symbols()) {
236 object::DataRefImpl DRI
= Symbol
.getRawDataRefImpl();
237 if (transferSymbol(Obj
.getSymbolTableEntry(DRI
), IsLittleEndian
, Strings
,
238 NewSymtab
, NewStrings
, InDebugNote
))
245 static MachO::section
246 getSection(const object::MachOObjectFile
&Obj
,
247 const MachO::segment_command
&Seg
,
248 const object::MachOObjectFile::LoadCommandInfo
&LCI
, unsigned Idx
) {
249 return Obj
.getSection(LCI
, Idx
);
252 static MachO::section_64
253 getSection(const object::MachOObjectFile
&Obj
,
254 const MachO::segment_command_64
&Seg
,
255 const object::MachOObjectFile::LoadCommandInfo
&LCI
, unsigned Idx
) {
256 return Obj
.getSection64(LCI
, Idx
);
259 // Transfer \a Segment from \a Obj to the output file. This calls into \a Writer
260 // to write these load commands directly in the output file at the current
263 // The function also tries to find a hole in the address map to fit the __DWARF
264 // segment of \a DwarfSegmentSize size. \a EndAddress is updated to point at the
265 // highest segment address.
267 // When the __LINKEDIT segment is transferred, its offset and size are set resp.
268 // to \a LinkeditOffset and \a LinkeditSize.
270 // When the eh_frame section is transferred, its offset and size are set resp.
271 // to \a EHFrameOffset and \a EHFrameSize.
272 template <typename SegmentTy
>
273 static void transferSegmentAndSections(
274 const object::MachOObjectFile::LoadCommandInfo
&LCI
, SegmentTy Segment
,
275 const object::MachOObjectFile
&Obj
, MachObjectWriter
&Writer
,
276 uint64_t LinkeditOffset
, uint64_t LinkeditSize
, uint64_t EHFrameOffset
,
277 uint64_t EHFrameSize
, uint64_t DwarfSegmentSize
, uint64_t &GapForDwarf
,
278 uint64_t &EndAddress
) {
279 if (StringRef("__DWARF") == Segment
.segname
)
282 if (StringRef("__TEXT") == Segment
.segname
&& EHFrameSize
> 0) {
283 Segment
.fileoff
= EHFrameOffset
;
284 Segment
.filesize
= EHFrameSize
;
285 } else if (StringRef("__LINKEDIT") == Segment
.segname
) {
286 Segment
.fileoff
= LinkeditOffset
;
287 Segment
.filesize
= LinkeditSize
;
288 // Resize vmsize by rounding to the page size.
289 Segment
.vmsize
= alignTo(LinkeditSize
, 0x1000);
291 Segment
.fileoff
= Segment
.filesize
= 0;
294 // Check if the end address of the last segment and our current
295 // start address leave a sufficient gap to store the __DWARF
297 uint64_t PrevEndAddress
= EndAddress
;
298 EndAddress
= alignTo(EndAddress
, 0x1000);
299 if (GapForDwarf
== UINT64_MAX
&& Segment
.vmaddr
> EndAddress
&&
300 Segment
.vmaddr
- EndAddress
>= DwarfSegmentSize
)
301 GapForDwarf
= EndAddress
;
303 // The segments are not necessarily sorted by their vmaddr.
305 std::max
<uint64_t>(PrevEndAddress
, Segment
.vmaddr
+ Segment
.vmsize
);
306 unsigned nsects
= Segment
.nsects
;
307 if (Obj
.isLittleEndian() != sys::IsLittleEndianHost
)
308 MachO::swapStruct(Segment
);
309 Writer
.W
.OS
.write(reinterpret_cast<char *>(&Segment
), sizeof(Segment
));
310 for (unsigned i
= 0; i
< nsects
; ++i
) {
311 auto Sect
= getSection(Obj
, Segment
, LCI
, i
);
312 if (StringRef("__eh_frame") == Sect
.sectname
) {
313 Sect
.offset
= EHFrameOffset
;
314 Sect
.reloff
= Sect
.nreloc
= 0;
316 Sect
.offset
= Sect
.reloff
= Sect
.nreloc
= 0;
318 if (Obj
.isLittleEndian() != sys::IsLittleEndianHost
)
319 MachO::swapStruct(Sect
);
320 Writer
.W
.OS
.write(reinterpret_cast<char *>(&Sect
), sizeof(Sect
));
324 // Write the __DWARF segment load command to the output file.
325 static bool createDwarfSegment(const MCAssembler
& Asm
,uint64_t VMAddr
, uint64_t FileOffset
,
326 uint64_t FileSize
, unsigned NumSections
,
327 MachObjectWriter
&Writer
) {
328 Writer
.writeSegmentLoadCommand("__DWARF", NumSections
, VMAddr
,
329 alignTo(FileSize
, 0x1000), FileOffset
,
330 FileSize
, /* MaxProt */ 7,
333 for (unsigned int i
= 0, n
= Writer
.getSectionOrder().size(); i
!= n
; ++i
) {
334 MCSection
*Sec
= Writer
.getSectionOrder()[i
];
335 if (!Asm
.getSectionFileSize(*Sec
))
338 Align Alignment
= Sec
->getAlign();
340 VMAddr
= alignTo(VMAddr
, Alignment
);
341 FileOffset
= alignTo(FileOffset
, Alignment
);
342 if (FileOffset
> UINT32_MAX
)
343 return error("section " + Sec
->getName() +
344 "'s file offset exceeds 4GB."
345 " Refusing to produce an invalid Mach-O file.");
347 Writer
.writeSection(Asm
, *Sec
, VMAddr
, FileOffset
, 0, 0, 0);
349 FileOffset
+= Asm
.getSectionAddressSize(*Sec
);
350 VMAddr
+= Asm
.getSectionAddressSize(*Sec
);
355 static bool isExecutable(const object::MachOObjectFile
&Obj
) {
357 return Obj
.getHeader64().filetype
!= MachO::MH_OBJECT
;
359 return Obj
.getHeader().filetype
!= MachO::MH_OBJECT
;
362 static unsigned segmentLoadCommandSize(bool Is64Bit
, unsigned NumSections
) {
364 return sizeof(MachO::segment_command_64
) +
365 NumSections
* sizeof(MachO::section_64
);
367 return sizeof(MachO::segment_command
) + NumSections
* sizeof(MachO::section
);
370 // Stream a dSYM companion binary file corresponding to the binary referenced
371 // by \a DM to \a OutFile. The passed \a MS MCStreamer is setup to write to
372 // \a OutFile and it must be using a MachObjectWriter object to do so.
373 bool generateDsymCompanion(
374 llvm::IntrusiveRefCntPtr
<llvm::vfs::FileSystem
> VFS
, const DebugMap
&DM
,
375 MCStreamer
&MS
, raw_fd_ostream
&OutFile
,
376 const std::vector
<MachOUtils::DwarfRelocationApplicationInfo
>
377 &RelocationsToApply
) {
378 auto &ObjectStreamer
= static_cast<MCObjectStreamer
&>(MS
);
379 MCAssembler
&MCAsm
= ObjectStreamer
.getAssembler();
380 auto &Writer
= static_cast<MachObjectWriter
&>(MCAsm
.getWriter());
382 // Layout but don't emit.
385 BinaryHolder
InputBinaryHolder(VFS
, false);
387 auto ObjectEntry
= InputBinaryHolder
.getObjectEntry(DM
.getBinaryPath());
389 auto Err
= ObjectEntry
.takeError();
390 return error(Twine("opening ") + DM
.getBinaryPath() + ": " +
391 toString(std::move(Err
)),
392 "output file streaming");
396 ObjectEntry
->getObjectAs
<object::MachOObjectFile
>(DM
.getTriple());
398 auto Err
= Object
.takeError();
399 return error(Twine("opening ") + DM
.getBinaryPath() + ": " +
400 toString(std::move(Err
)),
401 "output file streaming");
404 auto &InputBinary
= *Object
;
406 bool Is64Bit
= Writer
.is64Bit();
407 MachO::symtab_command SymtabCmd
= InputBinary
.getSymtabLoadCommand();
409 // Compute the number of load commands we will need.
410 unsigned LoadCommandSize
= 0;
411 unsigned NumLoadCommands
= 0;
413 bool HasSymtab
= false;
415 // Check LC_SYMTAB and get LC_UUID and LC_BUILD_VERSION.
416 MachO::uuid_command UUIDCmd
;
417 SmallVector
<MachO::build_version_command
, 2> BuildVersionCmd
;
418 memset(&UUIDCmd
, 0, sizeof(UUIDCmd
));
419 for (auto &LCI
: InputBinary
.load_commands()) {
423 return error("Binary contains more than one UUID");
424 UUIDCmd
= InputBinary
.getUuidCommand(LCI
);
426 LoadCommandSize
+= sizeof(UUIDCmd
);
428 case MachO::LC_BUILD_VERSION
: {
429 MachO::build_version_command Cmd
;
430 memset(&Cmd
, 0, sizeof(Cmd
));
431 Cmd
= InputBinary
.getBuildVersionLoadCommand(LCI
);
433 LoadCommandSize
+= sizeof(Cmd
);
434 // LLDB doesn't care about the build tools for now.
436 BuildVersionCmd
.push_back(Cmd
);
439 case MachO::LC_SYMTAB
:
447 // If we have a valid symtab to copy, do it.
448 bool ShouldEmitSymtab
= HasSymtab
&& isExecutable(InputBinary
);
449 if (ShouldEmitSymtab
) {
450 LoadCommandSize
+= sizeof(MachO::symtab_command
);
454 // If we have a valid eh_frame to copy, do it.
455 uint64_t EHFrameSize
= 0;
456 StringRef EHFrameData
;
457 for (const object::SectionRef
&Section
: InputBinary
.sections()) {
458 Expected
<StringRef
> NameOrErr
= Section
.getName();
460 consumeError(NameOrErr
.takeError());
463 StringRef SectionName
= *NameOrErr
;
464 SectionName
= SectionName
.substr(SectionName
.find_first_not_of("._"));
465 if (SectionName
== "eh_frame") {
466 if (Expected
<StringRef
> ContentsOrErr
= Section
.getContents()) {
467 EHFrameData
= *ContentsOrErr
;
468 EHFrameSize
= Section
.getSize();
470 consumeError(ContentsOrErr
.takeError());
475 unsigned HeaderSize
=
476 Is64Bit
? sizeof(MachO::mach_header_64
) : sizeof(MachO::mach_header
);
477 // We will copy every segment that isn't __DWARF.
478 iterateOnSegments(InputBinary
, [&](const MachO::segment_command_64
&Segment
) {
479 if (StringRef("__DWARF") == Segment
.segname
)
483 LoadCommandSize
+= segmentLoadCommandSize(Is64Bit
, Segment
.nsects
);
486 // We will add our own brand new __DWARF segment if we have debug
488 unsigned NumDwarfSections
= 0;
489 uint64_t DwarfSegmentSize
= 0;
491 for (unsigned int i
= 0, n
= Writer
.getSectionOrder().size(); i
!= n
; ++i
) {
492 MCSection
*Sec
= Writer
.getSectionOrder()[i
];
493 if (Sec
->begin() == Sec
->end())
496 if (uint64_t Size
= MCAsm
.getSectionFileSize(*Sec
)) {
497 DwarfSegmentSize
= alignTo(DwarfSegmentSize
, Sec
->getAlign());
498 DwarfSegmentSize
+= Size
;
503 if (NumDwarfSections
) {
505 LoadCommandSize
+= segmentLoadCommandSize(Is64Bit
, NumDwarfSections
);
508 SmallString
<0> NewSymtab
;
509 // Legacy dsymutil puts an empty string at the start of the line table.
510 // thus we set NonRelocatableStringpool(,PutEmptyString=true)
511 NonRelocatableStringpool
NewStrings(true);
512 unsigned NListSize
= Is64Bit
? sizeof(MachO::nlist_64
) : sizeof(MachO::nlist
);
513 unsigned NumSyms
= 0;
514 uint64_t NewStringsSize
= 0;
515 if (ShouldEmitSymtab
) {
516 NewSymtab
.reserve(SymtabCmd
.nsyms
* NListSize
/ 2);
517 NumSyms
= transferSymbols(InputBinary
, NewSymtab
, NewStrings
);
518 NewStringsSize
= NewStrings
.getSize() + 1;
521 uint64_t SymtabStart
= LoadCommandSize
;
522 SymtabStart
+= HeaderSize
;
523 SymtabStart
= alignTo(SymtabStart
, 0x1000);
525 // We gathered all the information we need, start emitting the output file.
526 Writer
.writeHeader(MachO::MH_DSYM
, NumLoadCommands
, LoadCommandSize
,
527 /*SubsectionsViaSymbols=*/false);
529 // Write the load commands.
530 assert(OutFile
.tell() == HeaderSize
);
531 if (UUIDCmd
.cmd
!= 0) {
532 Writer
.W
.write
<uint32_t>(UUIDCmd
.cmd
);
533 Writer
.W
.write
<uint32_t>(sizeof(UUIDCmd
));
534 OutFile
.write(reinterpret_cast<const char *>(UUIDCmd
.uuid
), 16);
535 assert(OutFile
.tell() == HeaderSize
+ sizeof(UUIDCmd
));
537 for (auto Cmd
: BuildVersionCmd
) {
538 Writer
.W
.write
<uint32_t>(Cmd
.cmd
);
539 Writer
.W
.write
<uint32_t>(sizeof(Cmd
));
540 Writer
.W
.write
<uint32_t>(Cmd
.platform
);
541 Writer
.W
.write
<uint32_t>(Cmd
.minos
);
542 Writer
.W
.write
<uint32_t>(Cmd
.sdk
);
543 Writer
.W
.write
<uint32_t>(Cmd
.ntools
);
546 assert(SymtabCmd
.cmd
&& "No symbol table.");
547 uint64_t StringStart
= SymtabStart
+ NumSyms
* NListSize
;
548 if (ShouldEmitSymtab
)
549 Writer
.writeSymtabLoadCommand(SymtabStart
, NumSyms
, StringStart
,
552 uint64_t EHFrameStart
= StringStart
+ NewStringsSize
;
553 EHFrameStart
= alignTo(EHFrameStart
, 0x1000);
555 uint64_t DwarfSegmentStart
= EHFrameStart
+ EHFrameSize
;
556 DwarfSegmentStart
= alignTo(DwarfSegmentStart
, 0x1000);
558 // Write the load commands for the segments and sections we 'import' from
559 // the original binary.
560 uint64_t EndAddress
= 0;
561 uint64_t GapForDwarf
= UINT64_MAX
;
562 for (auto &LCI
: InputBinary
.load_commands()) {
563 if (LCI
.C
.cmd
== MachO::LC_SEGMENT
)
564 transferSegmentAndSections(
565 LCI
, InputBinary
.getSegmentLoadCommand(LCI
), InputBinary
, Writer
,
566 SymtabStart
, StringStart
+ NewStringsSize
- SymtabStart
, EHFrameStart
,
567 EHFrameSize
, DwarfSegmentSize
, GapForDwarf
, EndAddress
);
568 else if (LCI
.C
.cmd
== MachO::LC_SEGMENT_64
)
569 transferSegmentAndSections(
570 LCI
, InputBinary
.getSegment64LoadCommand(LCI
), InputBinary
, Writer
,
571 SymtabStart
, StringStart
+ NewStringsSize
- SymtabStart
, EHFrameStart
,
572 EHFrameSize
, DwarfSegmentSize
, GapForDwarf
, EndAddress
);
575 uint64_t DwarfVMAddr
= alignTo(EndAddress
, 0x1000);
576 uint64_t DwarfVMMax
= Is64Bit
? UINT64_MAX
: UINT32_MAX
;
577 if (DwarfVMAddr
+ DwarfSegmentSize
> DwarfVMMax
||
578 DwarfVMAddr
+ DwarfSegmentSize
< DwarfVMAddr
/* Overflow */) {
579 // There is no room for the __DWARF segment at the end of the
580 // address space. Look through segments to find a gap.
581 DwarfVMAddr
= GapForDwarf
;
582 if (DwarfVMAddr
== UINT64_MAX
)
583 warn("not enough VM space for the __DWARF segment.",
584 "output file streaming");
587 // Write the load command for the __DWARF segment.
588 if (!createDwarfSegment(MCAsm
, DwarfVMAddr
, DwarfSegmentStart
, DwarfSegmentSize
,
589 NumDwarfSections
, Writer
))
592 assert(OutFile
.tell() == LoadCommandSize
+ HeaderSize
);
593 OutFile
.write_zeros(SymtabStart
- (LoadCommandSize
+ HeaderSize
));
594 assert(OutFile
.tell() == SymtabStart
);
597 if (ShouldEmitSymtab
) {
598 OutFile
<< NewSymtab
.str();
599 assert(OutFile
.tell() == StringStart
);
601 // Transfer string table.
602 // FIXME: The NonRelocatableStringpool starts with an empty string, but
603 // dsymutil-classic starts the reconstructed string table with 2 of these.
604 // Reproduce that behavior for now (there is corresponding code in
607 std::vector
<DwarfStringPoolEntryRef
> Strings
=
608 NewStrings
.getEntriesForEmission();
609 for (auto EntryRef
: Strings
) {
610 OutFile
.write(EntryRef
.getString().data(),
611 EntryRef
.getString().size() + 1);
614 assert(OutFile
.tell() == StringStart
+ NewStringsSize
);
616 // Pad till the EH frame start.
617 OutFile
.write_zeros(EHFrameStart
- (StringStart
+ NewStringsSize
));
618 assert(OutFile
.tell() == EHFrameStart
);
620 // Transfer eh_frame.
622 OutFile
<< EHFrameData
;
623 assert(OutFile
.tell() == EHFrameStart
+ EHFrameSize
);
625 // Pad till the Dwarf segment start.
626 OutFile
.write_zeros(DwarfSegmentStart
- (EHFrameStart
+ EHFrameSize
));
627 assert(OutFile
.tell() == DwarfSegmentStart
);
629 // Emit the Dwarf sections contents.
630 for (const MCSection
&Sec
: MCAsm
) {
631 uint64_t Pos
= OutFile
.tell();
632 OutFile
.write_zeros(alignTo(Pos
, Sec
.getAlign()) - Pos
);
633 MCAsm
.writeSectionData(OutFile
, &Sec
);
636 // Apply relocations to the contents of the DWARF segment.
637 // We do this here because the final value written depend on the DWARF vm
638 // addr, which is only calculated in this function.
639 if (!RelocationsToApply
.empty()) {
640 if (!OutFile
.supportsSeeking())
642 "Cannot apply relocations to file that doesn't support seeking!");
644 uint64_t Pos
= OutFile
.tell();
645 for (auto &RelocationToApply
: RelocationsToApply
) {
646 OutFile
.seek(DwarfSegmentStart
+ RelocationToApply
.AddressFromDwarfStart
);
647 int32_t Value
= RelocationToApply
.Value
;
648 if (RelocationToApply
.ShouldSubtractDwarfVM
)
649 Value
-= DwarfVMAddr
;
650 OutFile
.write((char *)&Value
, sizeof(int32_t));
657 } // namespace MachOUtils
658 } // namespace dsymutil