1 //===-- MachOUtils.h - Mach-o specific helpers for dsymutil --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "MachOUtils.h"
11 #include "BinaryHolder.h"
14 #include "NonRelocatableStringpool.h"
15 #include "llvm/MC/MCSectionMachO.h"
16 #include "llvm/MC/MCAsmLayout.h"
17 #include "llvm/MC/MCSectionMachO.h"
18 #include "llvm/MC/MCObjectStreamer.h"
19 #include "llvm/MC/MCStreamer.h"
20 #include "llvm/Object/MachO.h"
21 #include "llvm/Support/FileUtilities.h"
22 #include "llvm/Support/Program.h"
23 #include "llvm/Support/raw_ostream.h"
27 namespace MachOUtils {
29 std::string getArchName(StringRef Arch) {
30 if (Arch.startswith("thumb"))
31 return (llvm::Twine("arm") + Arch.drop_front(5)).str();
35 static bool runLipo(SmallVectorImpl<const char *> &Args) {
36 auto Path = sys::findProgramByName("lipo");
39 errs() << "error: lipo: " << Path.getError().message() << "\n";
45 sys::ExecuteAndWait(*Path, Args.data(), nullptr, nullptr, 0, 0, &ErrMsg);
47 errs() << "error: lipo: " << ErrMsg << "\n";
54 bool generateUniversalBinary(SmallVectorImpl<ArchAndFilename> &ArchFiles,
55 StringRef OutputFileName,
56 const LinkOptions &Options) {
57 // No need to merge one file into a universal fat binary. First, try
58 // to move it (rename) to the final location. If that fails because
59 // of cross-device link issues then copy and delete.
60 if (ArchFiles.size() == 1) {
61 StringRef From(ArchFiles.front().Path);
62 if (sys::fs::rename(From, OutputFileName)) {
63 if (std::error_code EC = sys::fs::copy_file(From, OutputFileName)) {
64 errs() << "error: while copying " << From << " to " << OutputFileName
65 << ": " << EC.message() << "\n";
68 sys::fs::remove(From);
73 SmallVector<const char *, 8> Args;
74 Args.push_back("lipo");
75 Args.push_back("-create");
77 for (auto &Thin : ArchFiles)
78 Args.push_back(Thin.Path.c_str());
80 // Align segments to match dsymutil-classic alignment
81 for (auto &Thin : ArchFiles) {
82 Thin.Arch = getArchName(Thin.Arch);
83 Args.push_back("-segalign");
84 Args.push_back(Thin.Arch.c_str());
88 Args.push_back("-output");
89 Args.push_back(OutputFileName.data());
90 Args.push_back(nullptr);
92 if (Options.Verbose) {
93 outs() << "Running lipo\n";
95 outs() << ' ' << ((Arg == nullptr) ? "\n" : Arg);
98 return Options.NoOutput ? true : runLipo(Args);
101 // Return a MachO::segment_command_64 that holds the same values as
102 // the passed MachO::segment_command. We do that to avoid having to
103 // duplicat the logic for 32bits and 64bits segments.
104 struct MachO::segment_command_64 adaptFrom32bits(MachO::segment_command Seg) {
105 MachO::segment_command_64 Seg64;
107 Seg64.cmdsize = Seg.cmdsize;
108 memcpy(Seg64.segname, Seg.segname, sizeof(Seg.segname));
109 Seg64.vmaddr = Seg.vmaddr;
110 Seg64.vmsize = Seg.vmsize;
111 Seg64.fileoff = Seg.fileoff;
112 Seg64.filesize = Seg.filesize;
113 Seg64.maxprot = Seg.maxprot;
114 Seg64.initprot = Seg.initprot;
115 Seg64.nsects = Seg.nsects;
116 Seg64.flags = Seg.flags;
120 // Iterate on all \a Obj segments, and apply \a Handler to them.
121 template <typename FunctionTy>
122 static void iterateOnSegments(const object::MachOObjectFile &Obj,
123 FunctionTy Handler) {
124 for (const auto &LCI : Obj.load_commands()) {
125 MachO::segment_command_64 Segment;
126 if (LCI.C.cmd == MachO::LC_SEGMENT)
127 Segment = adaptFrom32bits(Obj.getSegmentLoadCommand(LCI));
128 else if (LCI.C.cmd == MachO::LC_SEGMENT_64)
129 Segment = Obj.getSegment64LoadCommand(LCI);
137 // Transfer the symbols described by \a NList to \a NewSymtab which is
138 // just the raw contents of the symbol table for the dSYM companion file.
139 // \returns whether the symbol was tranfered or not.
140 template <typename NListTy>
141 static bool transferSymbol(NListTy NList, bool IsLittleEndian,
142 StringRef Strings, SmallVectorImpl<char> &NewSymtab,
143 NonRelocatableStringpool &NewStrings,
145 // Do not transfer undefined symbols, we want real addresses.
146 if ((NList.n_type & MachO::N_TYPE) == MachO::N_UNDF)
149 StringRef Name = StringRef(Strings.begin() + NList.n_strx);
152 (NList.n_type != MachO::N_SO) || (!Name.empty() && Name[0] != '\0');
154 } else if (NList.n_type == MachO::N_SO) {
159 // FIXME: The + 1 is here to mimic dsymutil-classic that has 2 empty
160 // strings at the start of the generated string table (There is
161 // corresponding code in the string table emission).
162 NList.n_strx = NewStrings.getStringOffset(Name) + 1;
163 if (IsLittleEndian != sys::IsLittleEndianHost)
164 MachO::swapStruct(NList);
166 NewSymtab.append(reinterpret_cast<char *>(&NList),
167 reinterpret_cast<char *>(&NList + 1));
171 // Wrapper around transferSymbol to transfer all of \a Obj symbols
172 // to \a NewSymtab. This function does not write in the output file.
173 // \returns the number of symbols in \a NewSymtab.
174 static unsigned transferSymbols(const object::MachOObjectFile &Obj,
175 SmallVectorImpl<char> &NewSymtab,
176 NonRelocatableStringpool &NewStrings) {
178 StringRef Strings = Obj.getStringTableData();
179 bool IsLittleEndian = Obj.isLittleEndian();
180 bool InDebugNote = false;
183 for (const object::SymbolRef &Symbol : Obj.symbols()) {
184 object::DataRefImpl DRI = Symbol.getRawDataRefImpl();
185 if (transferSymbol(Obj.getSymbol64TableEntry(DRI), IsLittleEndian,
186 Strings, NewSymtab, NewStrings, InDebugNote))
190 for (const object::SymbolRef &Symbol : Obj.symbols()) {
191 object::DataRefImpl DRI = Symbol.getRawDataRefImpl();
192 if (transferSymbol(Obj.getSymbolTableEntry(DRI), IsLittleEndian, Strings,
193 NewSymtab, NewStrings, InDebugNote))
200 static MachO::section
201 getSection(const object::MachOObjectFile &Obj,
202 const MachO::segment_command &Seg,
203 const object::MachOObjectFile::LoadCommandInfo &LCI, unsigned Idx) {
204 return Obj.getSection(LCI, Idx);
207 static MachO::section_64
208 getSection(const object::MachOObjectFile &Obj,
209 const MachO::segment_command_64 &Seg,
210 const object::MachOObjectFile::LoadCommandInfo &LCI, unsigned Idx) {
211 return Obj.getSection64(LCI, Idx);
214 // Transfer \a Segment from \a Obj to the output file. This calls into \a Writer
215 // to write these load commands directly in the output file at the current
217 // The function also tries to find a hole in the address map to fit the __DWARF
218 // segment of \a DwarfSegmentSize size. \a EndAddress is updated to point at the
219 // highest segment address.
220 // When the __LINKEDIT segment is transfered, its offset and size are set resp.
221 // to \a LinkeditOffset and \a LinkeditSize.
222 template <typename SegmentTy>
223 static void transferSegmentAndSections(
224 const object::MachOObjectFile::LoadCommandInfo &LCI, SegmentTy Segment,
225 const object::MachOObjectFile &Obj, MCObjectWriter &Writer,
226 uint64_t LinkeditOffset, uint64_t LinkeditSize, uint64_t DwarfSegmentSize,
227 uint64_t &GapForDwarf, uint64_t &EndAddress) {
228 if (StringRef("__DWARF") == Segment.segname)
231 Segment.fileoff = Segment.filesize = 0;
233 if (StringRef("__LINKEDIT") == Segment.segname) {
234 Segment.fileoff = LinkeditOffset;
235 Segment.filesize = LinkeditSize;
238 // Check if the end address of the last segment and our current
239 // start address leave a sufficient gap to store the __DWARF
241 uint64_t PrevEndAddress = EndAddress;
242 EndAddress = RoundUpToAlignment(EndAddress, 0x1000);
243 if (GapForDwarf == UINT64_MAX && Segment.vmaddr > EndAddress &&
244 Segment.vmaddr - EndAddress >= DwarfSegmentSize)
245 GapForDwarf = EndAddress;
247 // The segments are not necessarily sorted by their vmaddr.
249 std::max<uint64_t>(PrevEndAddress, Segment.vmaddr + Segment.vmsize);
250 unsigned nsects = Segment.nsects;
251 if (Obj.isLittleEndian() != sys::IsLittleEndianHost)
252 MachO::swapStruct(Segment);
254 StringRef(reinterpret_cast<char *>(&Segment), sizeof(Segment)));
255 for (unsigned i = 0; i < nsects; ++i) {
256 auto Sect = getSection(Obj, Segment, LCI, i);
257 Sect.offset = Sect.reloff = Sect.nreloc = 0;
258 if (Obj.isLittleEndian() != sys::IsLittleEndianHost)
259 MachO::swapStruct(Sect);
260 Writer.writeBytes(StringRef(reinterpret_cast<char *>(&Sect), sizeof(Sect)));
264 // Write the __DWARF segment load command to the output file.
265 static void createDwarfSegment(uint64_t VMAddr, uint64_t FileOffset,
266 uint64_t FileSize, unsigned NumSections,
267 MCAsmLayout &Layout, MachObjectWriter &Writer) {
268 Writer.writeSegmentLoadCommand("__DWARF", NumSections, VMAddr,
269 RoundUpToAlignment(FileSize, 0x1000),
270 FileOffset, FileSize, /* MaxProt */ 7,
273 for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) {
274 MCSection *Sec = Layout.getSectionOrder()[i];
275 if (Sec->begin() == Sec->end() || !Layout.getSectionFileSize(Sec))
278 unsigned Align = Sec->getAlignment();
280 VMAddr = RoundUpToAlignment(VMAddr, Align);
281 FileOffset = RoundUpToAlignment(FileOffset, Align);
283 Writer.writeSection(Layout, *Sec, VMAddr, FileOffset, 0, 0, 0);
285 FileOffset += Layout.getSectionAddressSize(Sec);
286 VMAddr += Layout.getSectionAddressSize(Sec);
290 static bool isExecutable(const object::MachOObjectFile &Obj) {
292 return Obj.getHeader64().filetype != MachO::MH_OBJECT;
294 return Obj.getHeader().filetype != MachO::MH_OBJECT;
297 static bool hasLinkEditSegment(const object::MachOObjectFile &Obj) {
298 bool HasLinkEditSegment = false;
299 iterateOnSegments(Obj, [&](const MachO::segment_command_64 &Segment) {
300 if (StringRef("__LINKEDIT") == Segment.segname)
301 HasLinkEditSegment = true;
303 return HasLinkEditSegment;
306 static unsigned segmentLoadCommandSize(bool Is64Bit, unsigned NumSections) {
308 return sizeof(MachO::segment_command_64) +
309 NumSections * sizeof(MachO::section_64);
311 return sizeof(MachO::segment_command) + NumSections * sizeof(MachO::section);
314 // Stream a dSYM companion binary file corresponding to the binary referenced
315 // by \a DM to \a OutFile. The passed \a MS MCStreamer is setup to write to
316 // \a OutFile and it must be using a MachObjectWriter object to do so.
317 bool generateDsymCompanion(const DebugMap &DM, MCStreamer &MS,
318 raw_fd_ostream &OutFile) {
319 auto &ObjectStreamer = static_cast<MCObjectStreamer &>(MS);
320 MCAssembler &MCAsm = ObjectStreamer.getAssembler();
321 auto &Writer = static_cast<MachObjectWriter &>(MCAsm.getWriter());
322 MCAsmLayout Layout(MCAsm);
324 MCAsm.layout(Layout);
326 BinaryHolder InputBinaryHolder(false);
327 auto ErrOrObjs = InputBinaryHolder.GetObjectFiles(DM.getBinaryPath());
328 if (auto Error = ErrOrObjs.getError())
329 return error(Twine("opening ") + DM.getBinaryPath() + ": " +
331 "output file streaming");
333 auto ErrOrInputBinary =
334 InputBinaryHolder.GetAs<object::MachOObjectFile>(DM.getTriple());
335 if (auto Error = ErrOrInputBinary.getError())
336 return error(Twine("opening ") + DM.getBinaryPath() + ": " +
338 "output file streaming");
339 auto &InputBinary = *ErrOrInputBinary;
341 bool Is64Bit = Writer.is64Bit();
342 MachO::symtab_command SymtabCmd = InputBinary.getSymtabLoadCommand();
345 MachO::uuid_command UUIDCmd;
346 memset(&UUIDCmd, 0, sizeof(UUIDCmd));
347 UUIDCmd.cmd = MachO::LC_UUID;
348 UUIDCmd.cmdsize = sizeof(MachO::uuid_command);
349 for (auto &LCI : InputBinary.load_commands()) {
350 if (LCI.C.cmd == MachO::LC_UUID) {
351 UUIDCmd = InputBinary.getUuidCommand(LCI);
356 // Compute the number of load commands we will need.
357 unsigned LoadCommandSize = 0;
358 unsigned NumLoadCommands = 0;
359 // We will copy the UUID if there is one.
360 if (UUIDCmd.cmd != 0) {
362 LoadCommandSize += sizeof(MachO::uuid_command);
365 // If we have a valid symtab to copy, do it.
366 bool ShouldEmitSymtab =
367 isExecutable(InputBinary) && hasLinkEditSegment(InputBinary);
368 if (ShouldEmitSymtab) {
369 LoadCommandSize += sizeof(MachO::symtab_command);
373 unsigned HeaderSize =
374 Is64Bit ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header);
375 // We will copy every segment that isn't __DWARF.
376 iterateOnSegments(InputBinary, [&](const MachO::segment_command_64 &Segment) {
377 if (StringRef("__DWARF") == Segment.segname)
381 LoadCommandSize += segmentLoadCommandSize(Is64Bit, Segment.nsects);
384 // We will add our own brand new __DWARF segment if we have debug
386 unsigned NumDwarfSections = 0;
387 uint64_t DwarfSegmentSize = 0;
389 for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) {
390 MCSection *Sec = Layout.getSectionOrder()[i];
391 if (Sec->begin() == Sec->end())
394 if (uint64_t Size = Layout.getSectionFileSize(Sec)) {
396 RoundUpToAlignment(DwarfSegmentSize, Sec->getAlignment());
397 DwarfSegmentSize += Size;
402 if (NumDwarfSections) {
404 LoadCommandSize += segmentLoadCommandSize(Is64Bit, NumDwarfSections);
407 SmallString<0> NewSymtab;
408 NonRelocatableStringpool NewStrings;
409 unsigned NListSize = Is64Bit ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist);
410 unsigned NumSyms = 0;
411 uint64_t NewStringsSize = 0;
412 if (ShouldEmitSymtab) {
413 NewSymtab.reserve(SymtabCmd.nsyms * NListSize / 2);
414 NumSyms = transferSymbols(InputBinary, NewSymtab, NewStrings);
415 NewStringsSize = NewStrings.getSize() + 1;
418 uint64_t SymtabStart = LoadCommandSize;
419 SymtabStart += HeaderSize;
420 SymtabStart = RoundUpToAlignment(SymtabStart, 0x1000);
422 // We gathered all the information we need, start emitting the output file.
423 Writer.writeHeader(MachO::MH_DSYM, NumLoadCommands, LoadCommandSize, false);
425 // Write the load commands.
426 assert(OutFile.tell() == HeaderSize);
427 if (UUIDCmd.cmd != 0) {
428 Writer.write32(UUIDCmd.cmd);
429 Writer.write32(UUIDCmd.cmdsize);
431 StringRef(reinterpret_cast<const char *>(UUIDCmd.uuid), 16));
432 assert(OutFile.tell() == HeaderSize + sizeof(UUIDCmd));
435 assert(SymtabCmd.cmd && "No symbol table.");
436 uint64_t StringStart = SymtabStart + NumSyms * NListSize;
437 if (ShouldEmitSymtab)
438 Writer.writeSymtabLoadCommand(SymtabStart, NumSyms, StringStart,
441 uint64_t DwarfSegmentStart = StringStart + NewStringsSize;
442 DwarfSegmentStart = RoundUpToAlignment(DwarfSegmentStart, 0x1000);
444 // Write the load commands for the segments and sections we 'import' from
445 // the original binary.
446 uint64_t EndAddress = 0;
447 uint64_t GapForDwarf = UINT64_MAX;
448 for (auto &LCI : InputBinary.load_commands()) {
449 if (LCI.C.cmd == MachO::LC_SEGMENT)
450 transferSegmentAndSections(LCI, InputBinary.getSegmentLoadCommand(LCI),
451 InputBinary, Writer, SymtabStart,
452 StringStart + NewStringsSize - SymtabStart,
453 DwarfSegmentSize, GapForDwarf, EndAddress);
454 else if (LCI.C.cmd == MachO::LC_SEGMENT_64)
455 transferSegmentAndSections(LCI, InputBinary.getSegment64LoadCommand(LCI),
456 InputBinary, Writer, SymtabStart,
457 StringStart + NewStringsSize - SymtabStart,
458 DwarfSegmentSize, GapForDwarf, EndAddress);
461 uint64_t DwarfVMAddr = RoundUpToAlignment(EndAddress, 0x1000);
462 uint64_t DwarfVMMax = Is64Bit ? UINT64_MAX : UINT32_MAX;
463 if (DwarfVMAddr + DwarfSegmentSize > DwarfVMMax ||
464 DwarfVMAddr + DwarfSegmentSize < DwarfVMAddr /* Overflow */) {
465 // There is no room for the __DWARF segment at the end of the
466 // address space. Look trhough segments to find a gap.
467 DwarfVMAddr = GapForDwarf;
468 if (DwarfVMAddr == UINT64_MAX)
469 warn("not enough VM space for the __DWARF segment.",
470 "output file streaming");
473 // Write the load command for the __DWARF segment.
474 createDwarfSegment(DwarfVMAddr, DwarfSegmentStart, DwarfSegmentSize,
475 NumDwarfSections, Layout, Writer);
477 assert(OutFile.tell() == LoadCommandSize + HeaderSize);
478 Writer.WriteZeros(SymtabStart - (LoadCommandSize + HeaderSize));
479 assert(OutFile.tell() == SymtabStart);
482 if (ShouldEmitSymtab) {
483 Writer.writeBytes(NewSymtab.str());
484 assert(OutFile.tell() == StringStart);
486 // Transfer string table.
487 // FIXME: The NonRelocatableStringpool starts with an empty string, but
488 // dsymutil-classic starts the reconstructed string table with 2 of these.
489 // Reproduce that behavior for now (there is corresponding code in
491 Writer.WriteZeros(1);
492 typedef NonRelocatableStringpool::MapTy MapTy;
493 for (auto *Entry = NewStrings.getFirstEntry(); Entry;
494 Entry = static_cast<MapTy::MapEntryTy *>(Entry->getValue().second))
496 StringRef(Entry->getKey().data(), Entry->getKey().size() + 1));
499 assert(OutFile.tell() == StringStart + NewStringsSize);
501 // Pad till the Dwarf segment start.
502 Writer.WriteZeros(DwarfSegmentStart - (StringStart + NewStringsSize));
503 assert(OutFile.tell() == DwarfSegmentStart);
505 // Emit the Dwarf sections contents.
506 for (const MCSection &Sec : MCAsm) {
507 if (Sec.begin() == Sec.end())
510 uint64_t Pos = OutFile.tell();
511 Writer.WriteZeros(RoundUpToAlignment(Pos, Sec.getAlignment()) - Pos);
512 MCAsm.writeSectionData(&Sec, Layout);