blob: 8c54563eab98dae2f89ba28b7196ce0b1e9e8b7b [file] [log] [blame]
Jonas Devlieghere928fea22018-06-27 16:13:40 +00001//===-- MachOUtils.cpp - Mach-o specific helpers for dsymutil ------------===//
Frederic Riss7a425782015-08-05 18:27:44 +00002//
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"
Frederic Rissef8ad012015-09-02 16:49:13 +000011#include "BinaryHolder.h"
12#include "DebugMap.h"
Jonas Devlieghere928fea22018-06-27 16:13:40 +000013#include "LinkUtils.h"
Frederic Rissef8ad012015-09-02 16:49:13 +000014#include "NonRelocatableStringpool.h"
Frederic Rissef8ad012015-09-02 16:49:13 +000015#include "llvm/MC/MCAsmLayout.h"
Reid Klecknere68240c2016-06-22 23:30:43 +000016#include "llvm/MC/MCMachObjectWriter.h"
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +000017#include "llvm/MC/MCObjectStreamer.h"
18#include "llvm/MC/MCSectionMachO.h"
Frederic Rissef8ad012015-09-02 16:49:13 +000019#include "llvm/MC/MCStreamer.h"
20#include "llvm/Object/MachO.h"
Frederic Riss7a425782015-08-05 18:27:44 +000021#include "llvm/Support/FileUtilities.h"
22#include "llvm/Support/Program.h"
Jonas Devlieghere5baab4c2018-04-14 21:36:42 +000023#include "llvm/Support/WithColor.h"
Frederic Riss7a425782015-08-05 18:27:44 +000024#include "llvm/Support/raw_ostream.h"
25
26namespace llvm {
27namespace dsymutil {
28namespace MachOUtils {
29
Jonas Devlieghere58d4a392018-07-29 14:56:15 +000030llvm::Error ArchAndFile::createTempFile() {
31 llvm::SmallString<128> TmpModel;
32 llvm::sys::path::system_temp_directory(true, TmpModel);
33 llvm::sys::path::append(TmpModel, "dsym.tmp%%%%%.dwarf");
34 Expected<sys::fs::TempFile> T = sys::fs::TempFile::create(TmpModel);
35
36 if (!T)
37 return T.takeError();
38
39 File = llvm::Optional<sys::fs::TempFile>(std::move(*T));
40 return Error::success();
41}
42
43llvm::StringRef ArchAndFile::path() const { return File->TmpName; }
44
45ArchAndFile::~ArchAndFile() {
46 if (File)
47 if (auto E = File->discard())
48 llvm::consumeError(std::move(E));
49}
50
Frederic Riss3abc9b62015-08-25 23:15:26 +000051std::string getArchName(StringRef Arch) {
52 if (Arch.startswith("thumb"))
53 return (llvm::Twine("arm") + Arch.drop_front(5)).str();
54 return Arch;
55}
56
Zachary Turner0dcc1152018-06-12 17:43:52 +000057static bool runLipo(StringRef SDKPath, SmallVectorImpl<StringRef> &Args) {
Frederic Rissa642d2c2015-10-08 22:35:53 +000058 auto Path = sys::findProgramByName("lipo", makeArrayRef(SDKPath));
59 if (!Path)
60 Path = sys::findProgramByName("lipo");
Frederic Riss7a425782015-08-05 18:27:44 +000061
62 if (!Path) {
Jonas Devlieghere5baab4c2018-04-14 21:36:42 +000063 WithColor::error() << "lipo: " << Path.getError().message() << "\n";
Frederic Riss7a425782015-08-05 18:27:44 +000064 return false;
65 }
66
67 std::string ErrMsg;
Zachary Turner0dcc1152018-06-12 17:43:52 +000068 int result = sys::ExecuteAndWait(*Path, Args, None, {}, 0, 0, &ErrMsg);
Frederic Riss7a425782015-08-05 18:27:44 +000069 if (result) {
Jonas Devlieghere5baab4c2018-04-14 21:36:42 +000070 WithColor::error() << "lipo: " << ErrMsg << "\n";
Frederic Riss7a425782015-08-05 18:27:44 +000071 return false;
72 }
73
74 return true;
75}
76
Jonas Devlieghere58d4a392018-07-29 14:56:15 +000077bool generateUniversalBinary(SmallVectorImpl<ArchAndFile> &ArchFiles,
Frederic Riss7a425782015-08-05 18:27:44 +000078 StringRef OutputFileName,
Frederic Rissa642d2c2015-10-08 22:35:53 +000079 const LinkOptions &Options, StringRef SDKPath) {
Jonas Devlieghere58d4a392018-07-29 14:56:15 +000080 // No need to merge one file into a universal fat binary.
Frederic Riss7a425782015-08-05 18:27:44 +000081 if (ArchFiles.size() == 1) {
Jonas Devlieghere58d4a392018-07-29 14:56:15 +000082 if (auto E = ArchFiles.front().File->keep(OutputFileName)) {
83 WithColor::error() << "while keeping " << ArchFiles.front().path()
84 << " as " << OutputFileName << ": "
85 << toString(std::move(E)) << "\n";
86 return false;
Frederic Riss7a425782015-08-05 18:27:44 +000087 }
88 return true;
89 }
90
Zachary Turner0dcc1152018-06-12 17:43:52 +000091 SmallVector<StringRef, 8> Args;
Frederic Riss7a425782015-08-05 18:27:44 +000092 Args.push_back("lipo");
93 Args.push_back("-create");
94
95 for (auto &Thin : ArchFiles)
Jonas Devlieghere58d4a392018-07-29 14:56:15 +000096 Args.push_back(Thin.path());
Frederic Riss7a425782015-08-05 18:27:44 +000097
98 // Align segments to match dsymutil-classic alignment
99 for (auto &Thin : ArchFiles) {
Frederic Riss3abc9b62015-08-25 23:15:26 +0000100 Thin.Arch = getArchName(Thin.Arch);
Frederic Riss7a425782015-08-05 18:27:44 +0000101 Args.push_back("-segalign");
Zachary Turner0dcc1152018-06-12 17:43:52 +0000102 Args.push_back(Thin.Arch);
Frederic Riss7a425782015-08-05 18:27:44 +0000103 Args.push_back("20");
104 }
105
106 Args.push_back("-output");
107 Args.push_back(OutputFileName.data());
Frederic Riss7a425782015-08-05 18:27:44 +0000108
109 if (Options.Verbose) {
110 outs() << "Running lipo\n";
111 for (auto Arg : Args)
Zachary Turner0dcc1152018-06-12 17:43:52 +0000112 outs() << ' ' << Arg;
113 outs() << "\n";
Frederic Riss7a425782015-08-05 18:27:44 +0000114 }
115
Frederic Rissa642d2c2015-10-08 22:35:53 +0000116 return Options.NoOutput ? true : runLipo(SDKPath, Args);
Frederic Riss7a425782015-08-05 18:27:44 +0000117}
Frederic Rissef8ad012015-09-02 16:49:13 +0000118
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +0000119// Return a MachO::segment_command_64 that holds the same values as the passed
120// MachO::segment_command. We do that to avoid having to duplicate the logic
121// for 32bits and 64bits segments.
Frederic Rissef8ad012015-09-02 16:49:13 +0000122struct MachO::segment_command_64 adaptFrom32bits(MachO::segment_command Seg) {
123 MachO::segment_command_64 Seg64;
124 Seg64.cmd = Seg.cmd;
125 Seg64.cmdsize = Seg.cmdsize;
126 memcpy(Seg64.segname, Seg.segname, sizeof(Seg.segname));
127 Seg64.vmaddr = Seg.vmaddr;
128 Seg64.vmsize = Seg.vmsize;
129 Seg64.fileoff = Seg.fileoff;
130 Seg64.filesize = Seg.filesize;
131 Seg64.maxprot = Seg.maxprot;
132 Seg64.initprot = Seg.initprot;
133 Seg64.nsects = Seg.nsects;
134 Seg64.flags = Seg.flags;
135 return Seg64;
136}
137
138// Iterate on all \a Obj segments, and apply \a Handler to them.
139template <typename FunctionTy>
140static void iterateOnSegments(const object::MachOObjectFile &Obj,
141 FunctionTy Handler) {
142 for (const auto &LCI : Obj.load_commands()) {
143 MachO::segment_command_64 Segment;
144 if (LCI.C.cmd == MachO::LC_SEGMENT)
145 Segment = adaptFrom32bits(Obj.getSegmentLoadCommand(LCI));
146 else if (LCI.C.cmd == MachO::LC_SEGMENT_64)
147 Segment = Obj.getSegment64LoadCommand(LCI);
148 else
149 continue;
150
151 Handler(Segment);
152 }
153}
154
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +0000155// Transfer the symbols described by \a NList to \a NewSymtab which is just the
156// raw contents of the symbol table for the dSYM companion file. \returns
157// whether the symbol was transferred or not.
Frederic Rissef8ad012015-09-02 16:49:13 +0000158template <typename NListTy>
159static bool transferSymbol(NListTy NList, bool IsLittleEndian,
160 StringRef Strings, SmallVectorImpl<char> &NewSymtab,
161 NonRelocatableStringpool &NewStrings,
162 bool &InDebugNote) {
163 // Do not transfer undefined symbols, we want real addresses.
164 if ((NList.n_type & MachO::N_TYPE) == MachO::N_UNDF)
165 return false;
166
167 StringRef Name = StringRef(Strings.begin() + NList.n_strx);
168 if (InDebugNote) {
169 InDebugNote =
170 (NList.n_type != MachO::N_SO) || (!Name.empty() && Name[0] != '\0');
171 return false;
172 } else if (NList.n_type == MachO::N_SO) {
173 InDebugNote = true;
174 return false;
175 }
176
177 // FIXME: The + 1 is here to mimic dsymutil-classic that has 2 empty
178 // strings at the start of the generated string table (There is
179 // corresponding code in the string table emission).
180 NList.n_strx = NewStrings.getStringOffset(Name) + 1;
181 if (IsLittleEndian != sys::IsLittleEndianHost)
182 MachO::swapStruct(NList);
183
184 NewSymtab.append(reinterpret_cast<char *>(&NList),
185 reinterpret_cast<char *>(&NList + 1));
186 return true;
187}
188
189// Wrapper around transferSymbol to transfer all of \a Obj symbols
190// to \a NewSymtab. This function does not write in the output file.
191// \returns the number of symbols in \a NewSymtab.
192static unsigned transferSymbols(const object::MachOObjectFile &Obj,
193 SmallVectorImpl<char> &NewSymtab,
194 NonRelocatableStringpool &NewStrings) {
195 unsigned Syms = 0;
196 StringRef Strings = Obj.getStringTableData();
197 bool IsLittleEndian = Obj.isLittleEndian();
198 bool InDebugNote = false;
199
200 if (Obj.is64Bit()) {
201 for (const object::SymbolRef &Symbol : Obj.symbols()) {
202 object::DataRefImpl DRI = Symbol.getRawDataRefImpl();
203 if (transferSymbol(Obj.getSymbol64TableEntry(DRI), IsLittleEndian,
204 Strings, NewSymtab, NewStrings, InDebugNote))
205 ++Syms;
206 }
207 } else {
208 for (const object::SymbolRef &Symbol : Obj.symbols()) {
209 object::DataRefImpl DRI = Symbol.getRawDataRefImpl();
210 if (transferSymbol(Obj.getSymbolTableEntry(DRI), IsLittleEndian, Strings,
211 NewSymtab, NewStrings, InDebugNote))
212 ++Syms;
213 }
214 }
215 return Syms;
216}
217
218static MachO::section
219getSection(const object::MachOObjectFile &Obj,
220 const MachO::segment_command &Seg,
221 const object::MachOObjectFile::LoadCommandInfo &LCI, unsigned Idx) {
222 return Obj.getSection(LCI, Idx);
223}
224
225static MachO::section_64
226getSection(const object::MachOObjectFile &Obj,
227 const MachO::segment_command_64 &Seg,
228 const object::MachOObjectFile::LoadCommandInfo &LCI, unsigned Idx) {
229 return Obj.getSection64(LCI, Idx);
230}
231
232// Transfer \a Segment from \a Obj to the output file. This calls into \a Writer
233// to write these load commands directly in the output file at the current
234// position.
235// The function also tries to find a hole in the address map to fit the __DWARF
236// segment of \a DwarfSegmentSize size. \a EndAddress is updated to point at the
237// highest segment address.
Simon Pilgrim428e6932017-03-30 12:59:53 +0000238// When the __LINKEDIT segment is transferred, its offset and size are set resp.
Frederic Rissef8ad012015-09-02 16:49:13 +0000239// to \a LinkeditOffset and \a LinkeditSize.
240template <typename SegmentTy>
241static void transferSegmentAndSections(
242 const object::MachOObjectFile::LoadCommandInfo &LCI, SegmentTy Segment,
Peter Collingbourne8e93a952018-05-21 18:17:42 +0000243 const object::MachOObjectFile &Obj, MachObjectWriter &Writer,
Frederic Rissef8ad012015-09-02 16:49:13 +0000244 uint64_t LinkeditOffset, uint64_t LinkeditSize, uint64_t DwarfSegmentSize,
245 uint64_t &GapForDwarf, uint64_t &EndAddress) {
246 if (StringRef("__DWARF") == Segment.segname)
247 return;
248
249 Segment.fileoff = Segment.filesize = 0;
250
251 if (StringRef("__LINKEDIT") == Segment.segname) {
252 Segment.fileoff = LinkeditOffset;
253 Segment.filesize = LinkeditSize;
Steven Wu9a41e592017-02-02 00:00:13 +0000254 // Resize vmsize by rounding to the page size.
255 Segment.vmsize = alignTo(LinkeditSize, 0x1000);
Frederic Rissef8ad012015-09-02 16:49:13 +0000256 }
257
258 // Check if the end address of the last segment and our current
259 // start address leave a sufficient gap to store the __DWARF
260 // segment.
261 uint64_t PrevEndAddress = EndAddress;
Rui Ueyama3edb0ec2016-01-14 21:06:47 +0000262 EndAddress = alignTo(EndAddress, 0x1000);
Frederic Rissef8ad012015-09-02 16:49:13 +0000263 if (GapForDwarf == UINT64_MAX && Segment.vmaddr > EndAddress &&
264 Segment.vmaddr - EndAddress >= DwarfSegmentSize)
265 GapForDwarf = EndAddress;
266
267 // The segments are not necessarily sorted by their vmaddr.
268 EndAddress =
269 std::max<uint64_t>(PrevEndAddress, Segment.vmaddr + Segment.vmsize);
270 unsigned nsects = Segment.nsects;
271 if (Obj.isLittleEndian() != sys::IsLittleEndianHost)
272 MachO::swapStruct(Segment);
Peter Collingbourne8e93a952018-05-21 18:17:42 +0000273 Writer.W.OS.write(reinterpret_cast<char *>(&Segment), sizeof(Segment));
Frederic Rissef8ad012015-09-02 16:49:13 +0000274 for (unsigned i = 0; i < nsects; ++i) {
275 auto Sect = getSection(Obj, Segment, LCI, i);
276 Sect.offset = Sect.reloff = Sect.nreloc = 0;
277 if (Obj.isLittleEndian() != sys::IsLittleEndianHost)
278 MachO::swapStruct(Sect);
Peter Collingbourne8e93a952018-05-21 18:17:42 +0000279 Writer.W.OS.write(reinterpret_cast<char *>(&Sect), sizeof(Sect));
Frederic Rissef8ad012015-09-02 16:49:13 +0000280 }
281}
282
283// Write the __DWARF segment load command to the output file.
284static void createDwarfSegment(uint64_t VMAddr, uint64_t FileOffset,
285 uint64_t FileSize, unsigned NumSections,
286 MCAsmLayout &Layout, MachObjectWriter &Writer) {
287 Writer.writeSegmentLoadCommand("__DWARF", NumSections, VMAddr,
Rui Ueyama3edb0ec2016-01-14 21:06:47 +0000288 alignTo(FileSize, 0x1000), FileOffset,
289 FileSize, /* MaxProt */ 7,
Frederic Rissef8ad012015-09-02 16:49:13 +0000290 /* InitProt =*/3);
291
292 for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) {
293 MCSection *Sec = Layout.getSectionOrder()[i];
294 if (Sec->begin() == Sec->end() || !Layout.getSectionFileSize(Sec))
295 continue;
296
297 unsigned Align = Sec->getAlignment();
298 if (Align > 1) {
Rui Ueyama3edb0ec2016-01-14 21:06:47 +0000299 VMAddr = alignTo(VMAddr, Align);
300 FileOffset = alignTo(FileOffset, Align);
Frederic Rissef8ad012015-09-02 16:49:13 +0000301 }
302 Writer.writeSection(Layout, *Sec, VMAddr, FileOffset, 0, 0, 0);
303
304 FileOffset += Layout.getSectionAddressSize(Sec);
305 VMAddr += Layout.getSectionAddressSize(Sec);
306 }
307}
308
309static bool isExecutable(const object::MachOObjectFile &Obj) {
310 if (Obj.is64Bit())
311 return Obj.getHeader64().filetype != MachO::MH_OBJECT;
312 else
313 return Obj.getHeader().filetype != MachO::MH_OBJECT;
314}
315
316static bool hasLinkEditSegment(const object::MachOObjectFile &Obj) {
317 bool HasLinkEditSegment = false;
318 iterateOnSegments(Obj, [&](const MachO::segment_command_64 &Segment) {
319 if (StringRef("__LINKEDIT") == Segment.segname)
320 HasLinkEditSegment = true;
321 });
322 return HasLinkEditSegment;
323}
324
325static unsigned segmentLoadCommandSize(bool Is64Bit, unsigned NumSections) {
326 if (Is64Bit)
327 return sizeof(MachO::segment_command_64) +
328 NumSections * sizeof(MachO::section_64);
329
330 return sizeof(MachO::segment_command) + NumSections * sizeof(MachO::section);
331}
332
333// Stream a dSYM companion binary file corresponding to the binary referenced
334// by \a DM to \a OutFile. The passed \a MS MCStreamer is setup to write to
335// \a OutFile and it must be using a MachObjectWriter object to do so.
Jonas Devlieghere20767d12019-01-07 23:27:25 +0000336bool generateDsymCompanion(const DebugMap &DM, SymbolMapTranslator &Translator,
337 MCStreamer &MS, raw_fd_ostream &OutFile) {
Frederic Rissef8ad012015-09-02 16:49:13 +0000338 auto &ObjectStreamer = static_cast<MCObjectStreamer &>(MS);
339 MCAssembler &MCAsm = ObjectStreamer.getAssembler();
340 auto &Writer = static_cast<MachObjectWriter &>(MCAsm.getWriter());
Frederic Rissef8ad012015-09-02 16:49:13 +0000341
Jonas Devlieghere74dc4042018-07-10 15:32:17 +0000342 // Layout but don't emit.
343 ObjectStreamer.flushPendingLabels();
344 MCAsmLayout Layout(MCAsm);
Frederic Rissef8ad012015-09-02 16:49:13 +0000345 MCAsm.layout(Layout);
346
347 BinaryHolder InputBinaryHolder(false);
Frederic Rissef8ad012015-09-02 16:49:13 +0000348
Jonas Devlieghere9f33bbfe2018-06-29 16:51:52 +0000349 auto ObjectEntry = InputBinaryHolder.getObjectEntry(DM.getBinaryPath());
350 if (!ObjectEntry) {
351 auto Err = ObjectEntry.takeError();
Frederic Rissef8ad012015-09-02 16:49:13 +0000352 return error(Twine("opening ") + DM.getBinaryPath() + ": " +
Jonas Devlieghere9f33bbfe2018-06-29 16:51:52 +0000353 toString(std::move(Err)),
Frederic Rissef8ad012015-09-02 16:49:13 +0000354 "output file streaming");
Jonas Devlieghere9f33bbfe2018-06-29 16:51:52 +0000355 }
356
357 auto Object =
358 ObjectEntry->getObjectAs<object::MachOObjectFile>(DM.getTriple());
359 if (!Object) {
360 auto Err = Object.takeError();
361 return error(Twine("opening ") + DM.getBinaryPath() + ": " +
362 toString(std::move(Err)),
363 "output file streaming");
364 }
365
366 auto &InputBinary = *Object;
Frederic Rissef8ad012015-09-02 16:49:13 +0000367
368 bool Is64Bit = Writer.is64Bit();
369 MachO::symtab_command SymtabCmd = InputBinary.getSymtabLoadCommand();
370
Frederic Rissef8ad012015-09-02 16:49:13 +0000371 // Compute the number of load commands we will need.
372 unsigned LoadCommandSize = 0;
373 unsigned NumLoadCommands = 0;
Adrian Prantla3a30c02018-11-08 16:54:59 +0000374
375 // Get LC_UUID and LC_BUILD_VERSION.
376 MachO::uuid_command UUIDCmd;
Adrian Prantl0ee0dff2018-11-13 23:31:25 +0000377 SmallVector<MachO::build_version_command, 2> BuildVersionCmd;
Adrian Prantla3a30c02018-11-08 16:54:59 +0000378 memset(&UUIDCmd, 0, sizeof(UUIDCmd));
Adrian Prantla3a30c02018-11-08 16:54:59 +0000379 for (auto &LCI : InputBinary.load_commands()) {
380 switch (LCI.C.cmd) {
381 case MachO::LC_UUID:
Adrian Prantl0ee0dff2018-11-13 23:31:25 +0000382 if (UUIDCmd.cmd)
383 return error("Binary contains more than one UUID");
Adrian Prantla3a30c02018-11-08 16:54:59 +0000384 UUIDCmd = InputBinary.getUuidCommand(LCI);
385 ++NumLoadCommands;
Adrian Prantl0ee0dff2018-11-13 23:31:25 +0000386 LoadCommandSize += sizeof(UUIDCmd);
Adrian Prantla3a30c02018-11-08 16:54:59 +0000387 break;
Adrian Prantl0ee0dff2018-11-13 23:31:25 +0000388 case MachO::LC_BUILD_VERSION: {
389 MachO::build_version_command Cmd;
390 memset(&Cmd, 0, sizeof(Cmd));
391 Cmd = InputBinary.getBuildVersionLoadCommand(LCI);
Adrian Prantla3a30c02018-11-08 16:54:59 +0000392 ++NumLoadCommands;
Adrian Prantl0ee0dff2018-11-13 23:31:25 +0000393 LoadCommandSize += sizeof(Cmd);
Adrian Prantla3a30c02018-11-08 16:54:59 +0000394 // LLDB doesn't care about the build tools for now.
Adrian Prantl0ee0dff2018-11-13 23:31:25 +0000395 Cmd.ntools = 0;
396 BuildVersionCmd.push_back(Cmd);
Adrian Prantla3a30c02018-11-08 16:54:59 +0000397 break;
Adrian Prantl0ee0dff2018-11-13 23:31:25 +0000398 }
Adrian Prantla3a30c02018-11-08 16:54:59 +0000399 default:
400 break;
401 }
Frederic Rissef8ad012015-09-02 16:49:13 +0000402 }
403
404 // If we have a valid symtab to copy, do it.
405 bool ShouldEmitSymtab =
406 isExecutable(InputBinary) && hasLinkEditSegment(InputBinary);
407 if (ShouldEmitSymtab) {
408 LoadCommandSize += sizeof(MachO::symtab_command);
409 ++NumLoadCommands;
410 }
411
412 unsigned HeaderSize =
413 Is64Bit ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header);
414 // We will copy every segment that isn't __DWARF.
415 iterateOnSegments(InputBinary, [&](const MachO::segment_command_64 &Segment) {
416 if (StringRef("__DWARF") == Segment.segname)
417 return;
418
419 ++NumLoadCommands;
420 LoadCommandSize += segmentLoadCommandSize(Is64Bit, Segment.nsects);
421 });
422
423 // We will add our own brand new __DWARF segment if we have debug
424 // info.
425 unsigned NumDwarfSections = 0;
426 uint64_t DwarfSegmentSize = 0;
427
428 for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) {
429 MCSection *Sec = Layout.getSectionOrder()[i];
430 if (Sec->begin() == Sec->end())
431 continue;
432
433 if (uint64_t Size = Layout.getSectionFileSize(Sec)) {
Rui Ueyama3edb0ec2016-01-14 21:06:47 +0000434 DwarfSegmentSize = alignTo(DwarfSegmentSize, Sec->getAlignment());
Frederic Rissef8ad012015-09-02 16:49:13 +0000435 DwarfSegmentSize += Size;
436 ++NumDwarfSections;
437 }
438 }
439
440 if (NumDwarfSections) {
441 ++NumLoadCommands;
442 LoadCommandSize += segmentLoadCommandSize(Is64Bit, NumDwarfSections);
443 }
444
445 SmallString<0> NewSymtab;
Jonas Devlieghere20767d12019-01-07 23:27:25 +0000446 NonRelocatableStringpool NewStrings(Translator);
Frederic Rissef8ad012015-09-02 16:49:13 +0000447 unsigned NListSize = Is64Bit ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist);
448 unsigned NumSyms = 0;
449 uint64_t NewStringsSize = 0;
450 if (ShouldEmitSymtab) {
451 NewSymtab.reserve(SymtabCmd.nsyms * NListSize / 2);
452 NumSyms = transferSymbols(InputBinary, NewSymtab, NewStrings);
453 NewStringsSize = NewStrings.getSize() + 1;
454 }
455
456 uint64_t SymtabStart = LoadCommandSize;
457 SymtabStart += HeaderSize;
Rui Ueyama3edb0ec2016-01-14 21:06:47 +0000458 SymtabStart = alignTo(SymtabStart, 0x1000);
Frederic Rissef8ad012015-09-02 16:49:13 +0000459
460 // We gathered all the information we need, start emitting the output file.
461 Writer.writeHeader(MachO::MH_DSYM, NumLoadCommands, LoadCommandSize, false);
462
463 // Write the load commands.
464 assert(OutFile.tell() == HeaderSize);
465 if (UUIDCmd.cmd != 0) {
Peter Collingbourne8e93a952018-05-21 18:17:42 +0000466 Writer.W.write<uint32_t>(UUIDCmd.cmd);
Adrian Prantla3a30c02018-11-08 16:54:59 +0000467 Writer.W.write<uint32_t>(sizeof(UUIDCmd));
Peter Collingbourne8e93a952018-05-21 18:17:42 +0000468 OutFile.write(reinterpret_cast<const char *>(UUIDCmd.uuid), 16);
Frederic Rissef8ad012015-09-02 16:49:13 +0000469 assert(OutFile.tell() == HeaderSize + sizeof(UUIDCmd));
470 }
Adrian Prantl0ee0dff2018-11-13 23:31:25 +0000471 for (auto Cmd : BuildVersionCmd) {
472 Writer.W.write<uint32_t>(Cmd.cmd);
473 Writer.W.write<uint32_t>(sizeof(Cmd));
474 Writer.W.write<uint32_t>(Cmd.platform);
475 Writer.W.write<uint32_t>(Cmd.minos);
476 Writer.W.write<uint32_t>(Cmd.sdk);
477 Writer.W.write<uint32_t>(Cmd.ntools);
Adrian Prantla3a30c02018-11-08 16:54:59 +0000478 }
Frederic Rissef8ad012015-09-02 16:49:13 +0000479
480 assert(SymtabCmd.cmd && "No symbol table.");
481 uint64_t StringStart = SymtabStart + NumSyms * NListSize;
482 if (ShouldEmitSymtab)
483 Writer.writeSymtabLoadCommand(SymtabStart, NumSyms, StringStart,
484 NewStringsSize);
485
486 uint64_t DwarfSegmentStart = StringStart + NewStringsSize;
Rui Ueyama3edb0ec2016-01-14 21:06:47 +0000487 DwarfSegmentStart = alignTo(DwarfSegmentStart, 0x1000);
Frederic Rissef8ad012015-09-02 16:49:13 +0000488
489 // Write the load commands for the segments and sections we 'import' from
490 // the original binary.
491 uint64_t EndAddress = 0;
492 uint64_t GapForDwarf = UINT64_MAX;
493 for (auto &LCI : InputBinary.load_commands()) {
494 if (LCI.C.cmd == MachO::LC_SEGMENT)
495 transferSegmentAndSections(LCI, InputBinary.getSegmentLoadCommand(LCI),
496 InputBinary, Writer, SymtabStart,
497 StringStart + NewStringsSize - SymtabStart,
498 DwarfSegmentSize, GapForDwarf, EndAddress);
499 else if (LCI.C.cmd == MachO::LC_SEGMENT_64)
500 transferSegmentAndSections(LCI, InputBinary.getSegment64LoadCommand(LCI),
501 InputBinary, Writer, SymtabStart,
502 StringStart + NewStringsSize - SymtabStart,
503 DwarfSegmentSize, GapForDwarf, EndAddress);
504 }
505
Rui Ueyama3edb0ec2016-01-14 21:06:47 +0000506 uint64_t DwarfVMAddr = alignTo(EndAddress, 0x1000);
Frederic Rissef8ad012015-09-02 16:49:13 +0000507 uint64_t DwarfVMMax = Is64Bit ? UINT64_MAX : UINT32_MAX;
508 if (DwarfVMAddr + DwarfSegmentSize > DwarfVMMax ||
509 DwarfVMAddr + DwarfSegmentSize < DwarfVMAddr /* Overflow */) {
510 // There is no room for the __DWARF segment at the end of the
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +0000511 // address space. Look through segments to find a gap.
Frederic Rissef8ad012015-09-02 16:49:13 +0000512 DwarfVMAddr = GapForDwarf;
513 if (DwarfVMAddr == UINT64_MAX)
514 warn("not enough VM space for the __DWARF segment.",
515 "output file streaming");
516 }
517
518 // Write the load command for the __DWARF segment.
519 createDwarfSegment(DwarfVMAddr, DwarfSegmentStart, DwarfSegmentSize,
520 NumDwarfSections, Layout, Writer);
521
522 assert(OutFile.tell() == LoadCommandSize + HeaderSize);
Peter Collingbourne8e93a952018-05-21 18:17:42 +0000523 OutFile.write_zeros(SymtabStart - (LoadCommandSize + HeaderSize));
Frederic Rissef8ad012015-09-02 16:49:13 +0000524 assert(OutFile.tell() == SymtabStart);
525
526 // Transfer symbols.
527 if (ShouldEmitSymtab) {
Peter Collingbourne8e93a952018-05-21 18:17:42 +0000528 OutFile << NewSymtab.str();
Frederic Rissef8ad012015-09-02 16:49:13 +0000529 assert(OutFile.tell() == StringStart);
530
531 // Transfer string table.
532 // FIXME: The NonRelocatableStringpool starts with an empty string, but
533 // dsymutil-classic starts the reconstructed string table with 2 of these.
534 // Reproduce that behavior for now (there is corresponding code in
535 // transferSymbol).
Peter Collingbourne8e93a952018-05-21 18:17:42 +0000536 OutFile << '\0';
Pavel Labath23332c52018-08-07 09:54:52 +0000537 std::vector<DwarfStringPoolEntryRef> Strings =
538 NewStrings.getEntriesForEmission();
Jonas Devlieghereef0ae3d2018-01-24 16:16:43 +0000539 for (auto EntryRef : Strings) {
Peter Collingbourne8e93a952018-05-21 18:17:42 +0000540 OutFile.write(EntryRef.getString().data(),
541 EntryRef.getString().size() + 1);
Jonas Devlieghereef0ae3d2018-01-24 16:16:43 +0000542 }
Frederic Rissef8ad012015-09-02 16:49:13 +0000543 }
544
545 assert(OutFile.tell() == StringStart + NewStringsSize);
546
547 // Pad till the Dwarf segment start.
Peter Collingbourne8e93a952018-05-21 18:17:42 +0000548 OutFile.write_zeros(DwarfSegmentStart - (StringStart + NewStringsSize));
Frederic Rissef8ad012015-09-02 16:49:13 +0000549 assert(OutFile.tell() == DwarfSegmentStart);
550
551 // Emit the Dwarf sections contents.
552 for (const MCSection &Sec : MCAsm) {
553 if (Sec.begin() == Sec.end())
554 continue;
555
556 uint64_t Pos = OutFile.tell();
Peter Collingbourne8e93a952018-05-21 18:17:42 +0000557 OutFile.write_zeros(alignTo(Pos, Sec.getAlignment()) - Pos);
Peter Collingbourne74bda0b2018-05-21 18:11:35 +0000558 MCAsm.writeSectionData(OutFile, &Sec, Layout);
Frederic Rissef8ad012015-09-02 16:49:13 +0000559 }
560
561 return true;
562}
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +0000563} // namespace MachOUtils
564} // namespace dsymutil
565} // namespace llvm