[dsymutil] Emit real dSYM companion binaries.
[oota-llvm.git] / tools / dsymutil / MachOUtils.cpp
1 //===-- MachOUtils.h - Mach-o specific helpers for dsymutil  --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "MachOUtils.h"
11 #include "BinaryHolder.h"
12 #include "DebugMap.h"
13 #include "dsymutil.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"
24
25 namespace llvm {
26 namespace dsymutil {
27 namespace MachOUtils {
28
29 std::string getArchName(StringRef Arch) {
30   if (Arch.startswith("thumb"))
31     return (llvm::Twine("arm") + Arch.drop_front(5)).str();
32   return Arch;
33 }
34
35 static bool runLipo(SmallVectorImpl<const char *> &Args) {
36   auto Path = sys::findProgramByName("lipo");
37
38   if (!Path) {
39     errs() << "error: lipo: " << Path.getError().message() << "\n";
40     return false;
41   }
42
43   std::string ErrMsg;
44   int result =
45       sys::ExecuteAndWait(*Path, Args.data(), nullptr, nullptr, 0, 0, &ErrMsg);
46   if (result) {
47     errs() << "error: lipo: " << ErrMsg << "\n";
48     return false;
49   }
50
51   return true;
52 }
53
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";
66         return false;
67       }
68       sys::fs::remove(From);
69     }
70     return true;
71   }
72
73   SmallVector<const char *, 8> Args;
74   Args.push_back("lipo");
75   Args.push_back("-create");
76
77   for (auto &Thin : ArchFiles)
78     Args.push_back(Thin.Path.c_str());
79
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());
85     Args.push_back("20");
86   }
87
88   Args.push_back("-output");
89   Args.push_back(OutputFileName.data());
90   Args.push_back(nullptr);
91
92   if (Options.Verbose) {
93     outs() << "Running lipo\n";
94     for (auto Arg : Args)
95       outs() << ' ' << ((Arg == nullptr) ? "\n" : Arg);
96   }
97
98   return Options.NoOutput ? true : runLipo(Args);
99 }
100
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;
106   Seg64.cmd = Seg.cmd;
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;
117   return Seg64;
118 }
119
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);
130     else
131       continue;
132
133     Handler(Segment);
134   }
135 }
136
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,
144                            bool &InDebugNote) {
145   // Do not transfer undefined symbols, we want real addresses.
146   if ((NList.n_type & MachO::N_TYPE) == MachO::N_UNDF)
147     return false;
148
149   StringRef Name = StringRef(Strings.begin() + NList.n_strx);
150   if (InDebugNote) {
151     InDebugNote =
152         (NList.n_type != MachO::N_SO) || (!Name.empty() && Name[0] != '\0');
153     return false;
154   } else if (NList.n_type == MachO::N_SO) {
155     InDebugNote = true;
156     return false;
157   }
158
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);
165
166   NewSymtab.append((char *)&NList, (char *)(&NList + 1));
167   return true;
168 }
169
170 // Wrapper around transferSymbol to transfer all of \a Obj symbols
171 // to \a NewSymtab. This function does not write in the output file.
172 // \returns the number of symbols in \a NewSymtab.
173 static unsigned transferSymbols(const object::MachOObjectFile &Obj,
174                                 SmallVectorImpl<char> &NewSymtab,
175                                 NonRelocatableStringpool &NewStrings) {
176   unsigned Syms = 0;
177   StringRef Strings = Obj.getStringTableData();
178   bool IsLittleEndian = Obj.isLittleEndian();
179   bool InDebugNote = false;
180
181   if (Obj.is64Bit()) {
182     for (const object::SymbolRef &Symbol : Obj.symbols()) {
183       object::DataRefImpl DRI = Symbol.getRawDataRefImpl();
184       if (transferSymbol(Obj.getSymbol64TableEntry(DRI), IsLittleEndian,
185                          Strings, NewSymtab, NewStrings, InDebugNote))
186         ++Syms;
187     }
188   } else {
189     for (const object::SymbolRef &Symbol : Obj.symbols()) {
190       object::DataRefImpl DRI = Symbol.getRawDataRefImpl();
191       if (transferSymbol(Obj.getSymbolTableEntry(DRI), IsLittleEndian, Strings,
192                          NewSymtab, NewStrings, InDebugNote))
193         ++Syms;
194     }
195   }
196   return Syms;
197 }
198
199 static MachO::section
200 getSection(const object::MachOObjectFile &Obj,
201            const MachO::segment_command &Seg,
202            const object::MachOObjectFile::LoadCommandInfo &LCI, unsigned Idx) {
203   return Obj.getSection(LCI, Idx);
204 }
205
206 static MachO::section_64
207 getSection(const object::MachOObjectFile &Obj,
208            const MachO::segment_command_64 &Seg,
209            const object::MachOObjectFile::LoadCommandInfo &LCI, unsigned Idx) {
210   return Obj.getSection64(LCI, Idx);
211 }
212
213 // Transfer \a Segment from \a Obj to the output file. This calls into \a Writer
214 // to write these load commands directly in the output file at the current
215 // position.
216 // The function also tries to find a hole in the address map to fit the __DWARF
217 // segment of \a DwarfSegmentSize size. \a EndAddress is updated to point at the
218 // highest segment address.
219 // When the __LINKEDIT segment is transfered, its offset and size are set resp.
220 // to \a LinkeditOffset and \a LinkeditSize.
221 template <typename SegmentTy>
222 static void transferSegmentAndSections(
223     const object::MachOObjectFile::LoadCommandInfo &LCI, SegmentTy Segment,
224     const object::MachOObjectFile &Obj, MCObjectWriter &Writer,
225     uint64_t LinkeditOffset, uint64_t LinkeditSize, uint64_t DwarfSegmentSize,
226     uint64_t &GapForDwarf, uint64_t &EndAddress) {
227   if (StringRef("__DWARF") == Segment.segname)
228     return;
229
230   Segment.fileoff = Segment.filesize = 0;
231
232   if (StringRef("__LINKEDIT") == Segment.segname) {
233     Segment.fileoff = LinkeditOffset;
234     Segment.filesize = LinkeditSize;
235   }
236
237   // Check if the end address of the last segment and our current
238   // start address leave a sufficient gap to store the __DWARF
239   // segment.
240   uint64_t PrevEndAddress = EndAddress;
241   EndAddress = RoundUpToAlignment(EndAddress, 0x1000);
242   if (GapForDwarf == UINT64_MAX && Segment.vmaddr > EndAddress &&
243       Segment.vmaddr - EndAddress >= DwarfSegmentSize)
244     GapForDwarf = EndAddress;
245
246   // The segments are not necessarily sorted by their vmaddr.
247   EndAddress =
248       std::max<uint64_t>(PrevEndAddress, Segment.vmaddr + Segment.vmsize);
249   if (Obj.isLittleEndian() != sys::IsLittleEndianHost)
250     MachO::swapStruct(Segment);
251   Writer.writeBytes(
252       StringRef(reinterpret_cast<char *>(&Segment), sizeof(Segment)));
253   for (unsigned i = 0; i < Segment.nsects; ++i) {
254     auto Sect = getSection(Obj, Segment, LCI, i);
255     Sect.offset = Sect.reloff = Sect.nreloc = 0;
256     if (Obj.isLittleEndian() != sys::IsLittleEndianHost)
257       MachO::swapStruct(Sect);
258     Writer.writeBytes(StringRef(reinterpret_cast<char *>(&Sect), sizeof(Sect)));
259   }
260 }
261
262 // Write the __DWARF segment load command to the output file.
263 static void createDwarfSegment(uint64_t VMAddr, uint64_t FileOffset,
264                                uint64_t FileSize, unsigned NumSections,
265                                MCAsmLayout &Layout, MachObjectWriter &Writer) {
266   Writer.writeSegmentLoadCommand("__DWARF", NumSections, VMAddr,
267                                  RoundUpToAlignment(FileSize, 0x1000),
268                                  FileOffset, FileSize, /* MaxProt */ 7,
269                                  /* InitProt =*/3);
270
271   for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) {
272     MCSection *Sec = Layout.getSectionOrder()[i];
273     if (Sec->begin() == Sec->end() || !Layout.getSectionFileSize(Sec))
274       continue;
275
276     unsigned Align = Sec->getAlignment();
277     if (Align > 1) {
278       VMAddr = RoundUpToAlignment(VMAddr, Align);
279       FileOffset = RoundUpToAlignment(FileOffset, Align);
280     }
281     Writer.writeSection(Layout, *Sec, VMAddr, FileOffset, 0, 0, 0);
282
283     FileOffset += Layout.getSectionAddressSize(Sec);
284     VMAddr += Layout.getSectionAddressSize(Sec);
285   }
286 }
287
288 static bool isExecutable(const object::MachOObjectFile &Obj) {
289   if (Obj.is64Bit())
290     return Obj.getHeader64().filetype != MachO::MH_OBJECT;
291   else
292     return Obj.getHeader().filetype != MachO::MH_OBJECT;
293 }
294
295 static bool hasLinkEditSegment(const object::MachOObjectFile &Obj) {
296   bool HasLinkEditSegment = false;
297   iterateOnSegments(Obj, [&](const MachO::segment_command_64 &Segment) {
298     if (StringRef("__LINKEDIT") == Segment.segname)
299       HasLinkEditSegment = true;
300   });
301   return HasLinkEditSegment;
302 }
303
304 static unsigned segmentLoadCommandSize(bool Is64Bit, unsigned NumSections) {
305   if (Is64Bit)
306     return sizeof(MachO::segment_command_64) +
307            NumSections * sizeof(MachO::section_64);
308
309   return sizeof(MachO::segment_command) + NumSections * sizeof(MachO::section);
310 }
311
312 // Stream a dSYM companion binary file corresponding to the binary referenced
313 // by \a DM to \a OutFile. The passed \a MS MCStreamer is setup to write to
314 // \a OutFile and it must be using a MachObjectWriter object to do so.
315 bool generateDsymCompanion(const DebugMap &DM, MCStreamer &MS,
316                            raw_fd_ostream &OutFile) {
317   auto &ObjectStreamer = static_cast<MCObjectStreamer &>(MS);
318   MCAssembler &MCAsm = ObjectStreamer.getAssembler();
319   auto &Writer = static_cast<MachObjectWriter &>(MCAsm.getWriter());
320   MCAsmLayout Layout(MCAsm);
321
322   MCAsm.layout(Layout);
323
324   BinaryHolder InputBinaryHolder(false);
325   auto ErrOrObjs = InputBinaryHolder.GetObjectFiles(DM.getBinaryPath());
326   if (auto Error = ErrOrObjs.getError())
327     return error(Twine("opening ") + DM.getBinaryPath() + ": " +
328                      Error.message(),
329                  "output file streaming");
330
331   auto ErrOrInputBinary =
332       InputBinaryHolder.GetAs<object::MachOObjectFile>(DM.getTriple());
333   if (auto Error = ErrOrInputBinary.getError())
334     return error(Twine("opening ") + DM.getBinaryPath() + ": " +
335                      Error.message(),
336                  "output file streaming");
337   auto &InputBinary = *ErrOrInputBinary;
338
339   bool Is64Bit = Writer.is64Bit();
340   MachO::symtab_command SymtabCmd = InputBinary.getSymtabLoadCommand();
341
342   // Get UUID.
343   MachO::uuid_command UUIDCmd;
344   memset(&UUIDCmd, 0, sizeof(UUIDCmd));
345   UUIDCmd.cmd = MachO::LC_UUID;
346   UUIDCmd.cmdsize = sizeof(MachO::uuid_command);
347   for (auto &LCI : InputBinary.load_commands()) {
348     if (LCI.C.cmd == MachO::LC_UUID) {
349       UUIDCmd = InputBinary.getUuidCommand(LCI);
350       break;
351     }
352   }
353
354   // Compute the number of load commands we will need.
355   unsigned LoadCommandSize = 0;
356   unsigned NumLoadCommands = 0;
357   // We will copy the UUID if there is one.
358   if (UUIDCmd.cmd != 0) {
359     ++NumLoadCommands;
360     LoadCommandSize += sizeof(MachO::uuid_command);
361   }
362
363   // If we have a valid symtab to copy, do it.
364   bool ShouldEmitSymtab =
365       isExecutable(InputBinary) && hasLinkEditSegment(InputBinary);
366   if (ShouldEmitSymtab) {
367     LoadCommandSize += sizeof(MachO::symtab_command);
368     ++NumLoadCommands;
369   }
370
371   unsigned HeaderSize =
372       Is64Bit ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header);
373   // We will copy every segment that isn't __DWARF.
374   iterateOnSegments(InputBinary, [&](const MachO::segment_command_64 &Segment) {
375     if (StringRef("__DWARF") == Segment.segname)
376       return;
377
378     ++NumLoadCommands;
379     LoadCommandSize += segmentLoadCommandSize(Is64Bit, Segment.nsects);
380   });
381
382   // We will add our own brand new __DWARF segment if we have debug
383   // info.
384   unsigned NumDwarfSections = 0;
385   uint64_t DwarfSegmentSize = 0;
386
387   for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) {
388     MCSection *Sec = Layout.getSectionOrder()[i];
389     if (Sec->begin() == Sec->end())
390       continue;
391
392     if (uint64_t Size = Layout.getSectionFileSize(Sec)) {
393       DwarfSegmentSize =
394           RoundUpToAlignment(DwarfSegmentSize, Sec->getAlignment());
395       DwarfSegmentSize += Size;
396       ++NumDwarfSections;
397     }
398   }
399
400   if (NumDwarfSections) {
401     ++NumLoadCommands;
402     LoadCommandSize += segmentLoadCommandSize(Is64Bit, NumDwarfSections);
403   }
404
405   SmallString<0> NewSymtab;
406   NonRelocatableStringpool NewStrings;
407   unsigned NListSize = Is64Bit ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist);
408   unsigned NumSyms = 0;
409   uint64_t NewStringsSize = 0;
410   if (ShouldEmitSymtab) {
411     NewSymtab.reserve(SymtabCmd.nsyms * NListSize / 2);
412     NumSyms = transferSymbols(InputBinary, NewSymtab, NewStrings);
413     NewStringsSize = NewStrings.getSize() + 1;
414   }
415
416   uint64_t SymtabStart = LoadCommandSize;
417   SymtabStart += HeaderSize;
418   SymtabStart = RoundUpToAlignment(SymtabStart, 0x1000);
419
420   // We gathered all the information we need, start emitting the output file.
421   Writer.writeHeader(MachO::MH_DSYM, NumLoadCommands, LoadCommandSize, false);
422
423   // Write the load commands.
424   assert(OutFile.tell() == HeaderSize);
425   if (UUIDCmd.cmd != 0) {
426     Writer.write32(UUIDCmd.cmd);
427     Writer.write32(UUIDCmd.cmdsize);
428     Writer.writeBytes(StringRef((const char *)UUIDCmd.uuid, 16));
429     assert(OutFile.tell() == HeaderSize + sizeof(UUIDCmd));
430   }
431
432   assert(SymtabCmd.cmd && "No symbol table.");
433   uint64_t StringStart = SymtabStart + NumSyms * NListSize;
434   if (ShouldEmitSymtab)
435     Writer.writeSymtabLoadCommand(SymtabStart, NumSyms, StringStart,
436                                   NewStringsSize);
437
438   uint64_t DwarfSegmentStart = StringStart + NewStringsSize;
439   DwarfSegmentStart = RoundUpToAlignment(DwarfSegmentStart, 0x1000);
440
441   // Write the load commands for the segments and sections we 'import' from
442   // the original binary.
443   uint64_t EndAddress = 0;
444   uint64_t GapForDwarf = UINT64_MAX;
445   for (auto &LCI : InputBinary.load_commands()) {
446     if (LCI.C.cmd == MachO::LC_SEGMENT)
447       transferSegmentAndSections(LCI, InputBinary.getSegmentLoadCommand(LCI),
448                                  InputBinary, Writer, SymtabStart,
449                                  StringStart + NewStringsSize - SymtabStart,
450                                  DwarfSegmentSize, GapForDwarf, EndAddress);
451     else if (LCI.C.cmd == MachO::LC_SEGMENT_64)
452       transferSegmentAndSections(LCI, InputBinary.getSegment64LoadCommand(LCI),
453                                  InputBinary, Writer, SymtabStart,
454                                  StringStart + NewStringsSize - SymtabStart,
455                                  DwarfSegmentSize, GapForDwarf, EndAddress);
456   }
457
458   uint64_t DwarfVMAddr = RoundUpToAlignment(EndAddress, 0x1000);
459   uint64_t DwarfVMMax = Is64Bit ? UINT64_MAX : UINT32_MAX;
460   if (DwarfVMAddr + DwarfSegmentSize > DwarfVMMax ||
461       DwarfVMAddr + DwarfSegmentSize < DwarfVMAddr /* Overflow */) {
462     // There is no room for the __DWARF segment at the end of the
463     // address space. Look trhough segments to find a gap.
464     DwarfVMAddr = GapForDwarf;
465     if (DwarfVMAddr == UINT64_MAX)
466       warn("not enough VM space for the __DWARF segment.",
467            "output file streaming");
468   }
469
470   // Write the load command for the __DWARF segment.
471   createDwarfSegment(DwarfVMAddr, DwarfSegmentStart, DwarfSegmentSize,
472                      NumDwarfSections, Layout, Writer);
473
474   assert(OutFile.tell() == LoadCommandSize + HeaderSize);
475   Writer.WriteZeros(SymtabStart - (LoadCommandSize + HeaderSize));
476   assert(OutFile.tell() == SymtabStart);
477
478   // Transfer symbols.
479   if (ShouldEmitSymtab) {
480     Writer.writeBytes(NewSymtab.str());
481     assert(OutFile.tell() == StringStart);
482
483     // Transfer string table.
484     // FIXME: The NonRelocatableStringpool starts with an empty string, but
485     // dsymutil-classic starts the reconstructed string table with 2 of these.
486     // Reproduce that behavior for now (there is corresponding code in
487     // transferSymbol).
488     Writer.WriteZeros(1);
489     typedef NonRelocatableStringpool::MapTy MapTy;
490     for (auto *Entry = NewStrings.getFirstEntry(); Entry;
491          Entry = static_cast<MapTy::MapEntryTy *>(Entry->getValue().second))
492       Writer.writeBytes(
493           StringRef(Entry->getKey().data(), Entry->getKey().size() + 1));
494   }
495
496   assert(OutFile.tell() == StringStart + NewStringsSize);
497
498   // Pad till the Dwarf segment start.
499   Writer.WriteZeros(DwarfSegmentStart - (StringStart + NewStringsSize));
500   assert(OutFile.tell() == DwarfSegmentStart);
501
502   // Emit the Dwarf sections contents.
503   for (const MCSection &Sec : MCAsm) {
504     if (Sec.begin() == Sec.end())
505       continue;
506
507     uint64_t Pos = OutFile.tell();
508     Writer.WriteZeros(RoundUpToAlignment(Pos, Sec.getAlignment()) - Pos);
509     MCAsm.writeSectionData(&Sec, Layout);
510   }
511
512   return true;
513 }
514 }
515 }
516 }