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((char *)&NList, (char *)(&NList + 1));
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) {
177 StringRef Strings = Obj.getStringTableData();
178 bool IsLittleEndian = Obj.isLittleEndian();
179 bool InDebugNote = false;
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))
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))
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);
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);
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
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)
230 Segment.fileoff = Segment.filesize = 0;
232 if (StringRef("__LINKEDIT") == Segment.segname) {
233 Segment.fileoff = LinkeditOffset;
234 Segment.filesize = LinkeditSize;
237 // Check if the end address of the last segment and our current
238 // start address leave a sufficient gap to store the __DWARF
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;
246 // The segments are not necessarily sorted by their vmaddr.
248 std::max<uint64_t>(PrevEndAddress, Segment.vmaddr + Segment.vmsize);
249 if (Obj.isLittleEndian() != sys::IsLittleEndianHost)
250 MachO::swapStruct(Segment);
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)));
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,
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))
276 unsigned Align = Sec->getAlignment();
278 VMAddr = RoundUpToAlignment(VMAddr, Align);
279 FileOffset = RoundUpToAlignment(FileOffset, Align);
281 Writer.writeSection(Layout, *Sec, VMAddr, FileOffset, 0, 0, 0);
283 FileOffset += Layout.getSectionAddressSize(Sec);
284 VMAddr += Layout.getSectionAddressSize(Sec);
288 static bool isExecutable(const object::MachOObjectFile &Obj) {
290 return Obj.getHeader64().filetype != MachO::MH_OBJECT;
292 return Obj.getHeader().filetype != MachO::MH_OBJECT;
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;
301 return HasLinkEditSegment;
304 static unsigned segmentLoadCommandSize(bool Is64Bit, unsigned NumSections) {
306 return sizeof(MachO::segment_command_64) +
307 NumSections * sizeof(MachO::section_64);
309 return sizeof(MachO::segment_command) + NumSections * sizeof(MachO::section);
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);
322 MCAsm.layout(Layout);
324 BinaryHolder InputBinaryHolder(false);
325 auto ErrOrObjs = InputBinaryHolder.GetObjectFiles(DM.getBinaryPath());
326 if (auto Error = ErrOrObjs.getError())
327 return error(Twine("opening ") + DM.getBinaryPath() + ": " +
329 "output file streaming");
331 auto ErrOrInputBinary =
332 InputBinaryHolder.GetAs<object::MachOObjectFile>(DM.getTriple());
333 if (auto Error = ErrOrInputBinary.getError())
334 return error(Twine("opening ") + DM.getBinaryPath() + ": " +
336 "output file streaming");
337 auto &InputBinary = *ErrOrInputBinary;
339 bool Is64Bit = Writer.is64Bit();
340 MachO::symtab_command SymtabCmd = InputBinary.getSymtabLoadCommand();
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);
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) {
360 LoadCommandSize += sizeof(MachO::uuid_command);
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);
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)
379 LoadCommandSize += segmentLoadCommandSize(Is64Bit, Segment.nsects);
382 // We will add our own brand new __DWARF segment if we have debug
384 unsigned NumDwarfSections = 0;
385 uint64_t DwarfSegmentSize = 0;
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())
392 if (uint64_t Size = Layout.getSectionFileSize(Sec)) {
394 RoundUpToAlignment(DwarfSegmentSize, Sec->getAlignment());
395 DwarfSegmentSize += Size;
400 if (NumDwarfSections) {
402 LoadCommandSize += segmentLoadCommandSize(Is64Bit, NumDwarfSections);
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;
416 uint64_t SymtabStart = LoadCommandSize;
417 SymtabStart += HeaderSize;
418 SymtabStart = RoundUpToAlignment(SymtabStart, 0x1000);
420 // We gathered all the information we need, start emitting the output file.
421 Writer.writeHeader(MachO::MH_DSYM, NumLoadCommands, LoadCommandSize, false);
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));
432 assert(SymtabCmd.cmd && "No symbol table.");
433 uint64_t StringStart = SymtabStart + NumSyms * NListSize;
434 if (ShouldEmitSymtab)
435 Writer.writeSymtabLoadCommand(SymtabStart, NumSyms, StringStart,
438 uint64_t DwarfSegmentStart = StringStart + NewStringsSize;
439 DwarfSegmentStart = RoundUpToAlignment(DwarfSegmentStart, 0x1000);
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);
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");
470 // Write the load command for the __DWARF segment.
471 createDwarfSegment(DwarfVMAddr, DwarfSegmentStart, DwarfSegmentSize,
472 NumDwarfSections, Layout, Writer);
474 assert(OutFile.tell() == LoadCommandSize + HeaderSize);
475 Writer.WriteZeros(SymtabStart - (LoadCommandSize + HeaderSize));
476 assert(OutFile.tell() == SymtabStart);
479 if (ShouldEmitSymtab) {
480 Writer.writeBytes(NewSymtab.str());
481 assert(OutFile.tell() == StringStart);
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
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))
493 StringRef(Entry->getKey().data(), Entry->getKey().size() + 1));
496 assert(OutFile.tell() == StringStart + NewStringsSize);
498 // Pad till the Dwarf segment start.
499 Writer.WriteZeros(DwarfSegmentStart - (StringStart + NewStringsSize));
500 assert(OutFile.tell() == DwarfSegmentStart);
502 // Emit the Dwarf sections contents.
503 for (const MCSection &Sec : MCAsm) {
504 if (Sec.begin() == Sec.end())
507 uint64_t Pos = OutFile.tell();
508 Writer.WriteZeros(RoundUpToAlignment(Pos, Sec.getAlignment()) - Pos);
509 MCAsm.writeSectionData(&Sec, Layout);