2018-06-28 00:13:40 +08:00
|
|
|
//===-- MachOUtils.cpp - Mach-o specific helpers for dsymutil ------------===//
|
2015-08-06 02:27:44 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-08-06 02:27:44 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MachOUtils.h"
|
2015-09-03 00:49:13 +08:00
|
|
|
#include "BinaryHolder.h"
|
|
|
|
#include "DebugMap.h"
|
2018-06-28 00:13:40 +08:00
|
|
|
#include "LinkUtils.h"
|
2015-09-03 00:49:13 +08:00
|
|
|
#include "NonRelocatableStringpool.h"
|
|
|
|
#include "llvm/MC/MCAsmLayout.h"
|
2016-06-23 07:30:43 +08:00
|
|
|
#include "llvm/MC/MCMachObjectWriter.h"
|
2018-02-22 19:32:51 +08:00
|
|
|
#include "llvm/MC/MCObjectStreamer.h"
|
|
|
|
#include "llvm/MC/MCSectionMachO.h"
|
2015-09-03 00:49:13 +08:00
|
|
|
#include "llvm/MC/MCStreamer.h"
|
|
|
|
#include "llvm/Object/MachO.h"
|
2015-08-06 02:27:44 +08:00
|
|
|
#include "llvm/Support/FileUtilities.h"
|
|
|
|
#include "llvm/Support/Program.h"
|
2018-04-15 05:36:42 +08:00
|
|
|
#include "llvm/Support/WithColor.h"
|
2015-08-06 02:27:44 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace dsymutil {
|
|
|
|
namespace MachOUtils {
|
|
|
|
|
2018-07-29 22:56:15 +08:00
|
|
|
llvm::Error ArchAndFile::createTempFile() {
|
|
|
|
llvm::SmallString<128> TmpModel;
|
|
|
|
llvm::sys::path::system_temp_directory(true, TmpModel);
|
|
|
|
llvm::sys::path::append(TmpModel, "dsym.tmp%%%%%.dwarf");
|
|
|
|
Expected<sys::fs::TempFile> T = sys::fs::TempFile::create(TmpModel);
|
|
|
|
|
|
|
|
if (!T)
|
|
|
|
return T.takeError();
|
|
|
|
|
2019-06-06 01:14:32 +08:00
|
|
|
File = llvm::make_unique<sys::fs::TempFile>(std::move(*T));
|
2018-07-29 22:56:15 +08:00
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::StringRef ArchAndFile::path() const { return File->TmpName; }
|
|
|
|
|
|
|
|
ArchAndFile::~ArchAndFile() {
|
|
|
|
if (File)
|
|
|
|
if (auto E = File->discard())
|
|
|
|
llvm::consumeError(std::move(E));
|
|
|
|
}
|
|
|
|
|
2015-08-26 07:15:26 +08:00
|
|
|
std::string getArchName(StringRef Arch) {
|
|
|
|
if (Arch.startswith("thumb"))
|
|
|
|
return (llvm::Twine("arm") + Arch.drop_front(5)).str();
|
|
|
|
return Arch;
|
|
|
|
}
|
|
|
|
|
2018-06-13 01:43:52 +08:00
|
|
|
static bool runLipo(StringRef SDKPath, SmallVectorImpl<StringRef> &Args) {
|
2015-10-09 06:35:53 +08:00
|
|
|
auto Path = sys::findProgramByName("lipo", makeArrayRef(SDKPath));
|
|
|
|
if (!Path)
|
|
|
|
Path = sys::findProgramByName("lipo");
|
2015-08-06 02:27:44 +08:00
|
|
|
|
|
|
|
if (!Path) {
|
2018-04-15 05:36:42 +08:00
|
|
|
WithColor::error() << "lipo: " << Path.getError().message() << "\n";
|
2015-08-06 02:27:44 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ErrMsg;
|
2018-06-13 01:43:52 +08:00
|
|
|
int result = sys::ExecuteAndWait(*Path, Args, None, {}, 0, 0, &ErrMsg);
|
2015-08-06 02:27:44 +08:00
|
|
|
if (result) {
|
2018-04-15 05:36:42 +08:00
|
|
|
WithColor::error() << "lipo: " << ErrMsg << "\n";
|
2015-08-06 02:27:44 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-29 22:56:15 +08:00
|
|
|
bool generateUniversalBinary(SmallVectorImpl<ArchAndFile> &ArchFiles,
|
2015-08-06 02:27:44 +08:00
|
|
|
StringRef OutputFileName,
|
2015-10-09 06:35:53 +08:00
|
|
|
const LinkOptions &Options, StringRef SDKPath) {
|
2018-07-29 22:56:15 +08:00
|
|
|
// No need to merge one file into a universal fat binary.
|
2015-08-06 02:27:44 +08:00
|
|
|
if (ArchFiles.size() == 1) {
|
2018-07-29 22:56:15 +08:00
|
|
|
if (auto E = ArchFiles.front().File->keep(OutputFileName)) {
|
|
|
|
WithColor::error() << "while keeping " << ArchFiles.front().path()
|
|
|
|
<< " as " << OutputFileName << ": "
|
|
|
|
<< toString(std::move(E)) << "\n";
|
|
|
|
return false;
|
2015-08-06 02:27:44 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-13 01:43:52 +08:00
|
|
|
SmallVector<StringRef, 8> Args;
|
2015-08-06 02:27:44 +08:00
|
|
|
Args.push_back("lipo");
|
|
|
|
Args.push_back("-create");
|
|
|
|
|
|
|
|
for (auto &Thin : ArchFiles)
|
2018-07-29 22:56:15 +08:00
|
|
|
Args.push_back(Thin.path());
|
2015-08-06 02:27:44 +08:00
|
|
|
|
|
|
|
// Align segments to match dsymutil-classic alignment
|
|
|
|
for (auto &Thin : ArchFiles) {
|
2015-08-26 07:15:26 +08:00
|
|
|
Thin.Arch = getArchName(Thin.Arch);
|
2015-08-06 02:27:44 +08:00
|
|
|
Args.push_back("-segalign");
|
2018-06-13 01:43:52 +08:00
|
|
|
Args.push_back(Thin.Arch);
|
2015-08-06 02:27:44 +08:00
|
|
|
Args.push_back("20");
|
|
|
|
}
|
|
|
|
|
|
|
|
Args.push_back("-output");
|
|
|
|
Args.push_back(OutputFileName.data());
|
|
|
|
|
|
|
|
if (Options.Verbose) {
|
|
|
|
outs() << "Running lipo\n";
|
|
|
|
for (auto Arg : Args)
|
2018-06-13 01:43:52 +08:00
|
|
|
outs() << ' ' << Arg;
|
|
|
|
outs() << "\n";
|
2015-08-06 02:27:44 +08:00
|
|
|
}
|
|
|
|
|
2015-10-09 06:35:53 +08:00
|
|
|
return Options.NoOutput ? true : runLipo(SDKPath, Args);
|
2015-08-06 02:27:44 +08:00
|
|
|
}
|
2015-09-03 00:49:13 +08:00
|
|
|
|
2018-02-22 19:32:51 +08:00
|
|
|
// Return a MachO::segment_command_64 that holds the same values as the passed
|
|
|
|
// MachO::segment_command. We do that to avoid having to duplicate the logic
|
|
|
|
// for 32bits and 64bits segments.
|
2015-09-03 00:49:13 +08:00
|
|
|
struct MachO::segment_command_64 adaptFrom32bits(MachO::segment_command Seg) {
|
|
|
|
MachO::segment_command_64 Seg64;
|
|
|
|
Seg64.cmd = Seg.cmd;
|
|
|
|
Seg64.cmdsize = Seg.cmdsize;
|
|
|
|
memcpy(Seg64.segname, Seg.segname, sizeof(Seg.segname));
|
|
|
|
Seg64.vmaddr = Seg.vmaddr;
|
|
|
|
Seg64.vmsize = Seg.vmsize;
|
|
|
|
Seg64.fileoff = Seg.fileoff;
|
|
|
|
Seg64.filesize = Seg.filesize;
|
|
|
|
Seg64.maxprot = Seg.maxprot;
|
|
|
|
Seg64.initprot = Seg.initprot;
|
|
|
|
Seg64.nsects = Seg.nsects;
|
|
|
|
Seg64.flags = Seg.flags;
|
|
|
|
return Seg64;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate on all \a Obj segments, and apply \a Handler to them.
|
|
|
|
template <typename FunctionTy>
|
|
|
|
static void iterateOnSegments(const object::MachOObjectFile &Obj,
|
|
|
|
FunctionTy Handler) {
|
|
|
|
for (const auto &LCI : Obj.load_commands()) {
|
|
|
|
MachO::segment_command_64 Segment;
|
|
|
|
if (LCI.C.cmd == MachO::LC_SEGMENT)
|
|
|
|
Segment = adaptFrom32bits(Obj.getSegmentLoadCommand(LCI));
|
|
|
|
else if (LCI.C.cmd == MachO::LC_SEGMENT_64)
|
|
|
|
Segment = Obj.getSegment64LoadCommand(LCI);
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Handler(Segment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-22 19:32:51 +08:00
|
|
|
// Transfer the symbols described by \a NList to \a NewSymtab which is just the
|
|
|
|
// raw contents of the symbol table for the dSYM companion file. \returns
|
|
|
|
// whether the symbol was transferred or not.
|
2015-09-03 00:49:13 +08:00
|
|
|
template <typename NListTy>
|
|
|
|
static bool transferSymbol(NListTy NList, bool IsLittleEndian,
|
|
|
|
StringRef Strings, SmallVectorImpl<char> &NewSymtab,
|
|
|
|
NonRelocatableStringpool &NewStrings,
|
|
|
|
bool &InDebugNote) {
|
|
|
|
// Do not transfer undefined symbols, we want real addresses.
|
|
|
|
if ((NList.n_type & MachO::N_TYPE) == MachO::N_UNDF)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
StringRef Name = StringRef(Strings.begin() + NList.n_strx);
|
|
|
|
if (InDebugNote) {
|
|
|
|
InDebugNote =
|
|
|
|
(NList.n_type != MachO::N_SO) || (!Name.empty() && Name[0] != '\0');
|
|
|
|
return false;
|
|
|
|
} else if (NList.n_type == MachO::N_SO) {
|
|
|
|
InDebugNote = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: The + 1 is here to mimic dsymutil-classic that has 2 empty
|
|
|
|
// strings at the start of the generated string table (There is
|
|
|
|
// corresponding code in the string table emission).
|
|
|
|
NList.n_strx = NewStrings.getStringOffset(Name) + 1;
|
|
|
|
if (IsLittleEndian != sys::IsLittleEndianHost)
|
|
|
|
MachO::swapStruct(NList);
|
|
|
|
|
|
|
|
NewSymtab.append(reinterpret_cast<char *>(&NList),
|
|
|
|
reinterpret_cast<char *>(&NList + 1));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapper around transferSymbol to transfer all of \a Obj symbols
|
|
|
|
// to \a NewSymtab. This function does not write in the output file.
|
|
|
|
// \returns the number of symbols in \a NewSymtab.
|
|
|
|
static unsigned transferSymbols(const object::MachOObjectFile &Obj,
|
|
|
|
SmallVectorImpl<char> &NewSymtab,
|
|
|
|
NonRelocatableStringpool &NewStrings) {
|
|
|
|
unsigned Syms = 0;
|
|
|
|
StringRef Strings = Obj.getStringTableData();
|
|
|
|
bool IsLittleEndian = Obj.isLittleEndian();
|
|
|
|
bool InDebugNote = false;
|
|
|
|
|
|
|
|
if (Obj.is64Bit()) {
|
|
|
|
for (const object::SymbolRef &Symbol : Obj.symbols()) {
|
|
|
|
object::DataRefImpl DRI = Symbol.getRawDataRefImpl();
|
|
|
|
if (transferSymbol(Obj.getSymbol64TableEntry(DRI), IsLittleEndian,
|
|
|
|
Strings, NewSymtab, NewStrings, InDebugNote))
|
|
|
|
++Syms;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (const object::SymbolRef &Symbol : Obj.symbols()) {
|
|
|
|
object::DataRefImpl DRI = Symbol.getRawDataRefImpl();
|
|
|
|
if (transferSymbol(Obj.getSymbolTableEntry(DRI), IsLittleEndian, Strings,
|
|
|
|
NewSymtab, NewStrings, InDebugNote))
|
|
|
|
++Syms;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Syms;
|
|
|
|
}
|
|
|
|
|
|
|
|
static MachO::section
|
|
|
|
getSection(const object::MachOObjectFile &Obj,
|
|
|
|
const MachO::segment_command &Seg,
|
|
|
|
const object::MachOObjectFile::LoadCommandInfo &LCI, unsigned Idx) {
|
|
|
|
return Obj.getSection(LCI, Idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static MachO::section_64
|
|
|
|
getSection(const object::MachOObjectFile &Obj,
|
|
|
|
const MachO::segment_command_64 &Seg,
|
|
|
|
const object::MachOObjectFile::LoadCommandInfo &LCI, unsigned Idx) {
|
|
|
|
return Obj.getSection64(LCI, Idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transfer \a Segment from \a Obj to the output file. This calls into \a Writer
|
|
|
|
// to write these load commands directly in the output file at the current
|
|
|
|
// position.
|
|
|
|
// The function also tries to find a hole in the address map to fit the __DWARF
|
|
|
|
// segment of \a DwarfSegmentSize size. \a EndAddress is updated to point at the
|
|
|
|
// highest segment address.
|
2017-03-30 20:59:53 +08:00
|
|
|
// When the __LINKEDIT segment is transferred, its offset and size are set resp.
|
2015-09-03 00:49:13 +08:00
|
|
|
// to \a LinkeditOffset and \a LinkeditSize.
|
|
|
|
template <typename SegmentTy>
|
|
|
|
static void transferSegmentAndSections(
|
|
|
|
const object::MachOObjectFile::LoadCommandInfo &LCI, SegmentTy Segment,
|
2018-05-22 02:17:42 +08:00
|
|
|
const object::MachOObjectFile &Obj, MachObjectWriter &Writer,
|
2015-09-03 00:49:13 +08:00
|
|
|
uint64_t LinkeditOffset, uint64_t LinkeditSize, uint64_t DwarfSegmentSize,
|
|
|
|
uint64_t &GapForDwarf, uint64_t &EndAddress) {
|
|
|
|
if (StringRef("__DWARF") == Segment.segname)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Segment.fileoff = Segment.filesize = 0;
|
|
|
|
|
|
|
|
if (StringRef("__LINKEDIT") == Segment.segname) {
|
|
|
|
Segment.fileoff = LinkeditOffset;
|
|
|
|
Segment.filesize = LinkeditSize;
|
2017-02-02 08:00:13 +08:00
|
|
|
// Resize vmsize by rounding to the page size.
|
|
|
|
Segment.vmsize = alignTo(LinkeditSize, 0x1000);
|
2015-09-03 00:49:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the end address of the last segment and our current
|
|
|
|
// start address leave a sufficient gap to store the __DWARF
|
|
|
|
// segment.
|
|
|
|
uint64_t PrevEndAddress = EndAddress;
|
2016-01-15 05:06:47 +08:00
|
|
|
EndAddress = alignTo(EndAddress, 0x1000);
|
2015-09-03 00:49:13 +08:00
|
|
|
if (GapForDwarf == UINT64_MAX && Segment.vmaddr > EndAddress &&
|
|
|
|
Segment.vmaddr - EndAddress >= DwarfSegmentSize)
|
|
|
|
GapForDwarf = EndAddress;
|
|
|
|
|
|
|
|
// The segments are not necessarily sorted by their vmaddr.
|
|
|
|
EndAddress =
|
|
|
|
std::max<uint64_t>(PrevEndAddress, Segment.vmaddr + Segment.vmsize);
|
|
|
|
unsigned nsects = Segment.nsects;
|
|
|
|
if (Obj.isLittleEndian() != sys::IsLittleEndianHost)
|
|
|
|
MachO::swapStruct(Segment);
|
2018-05-22 02:17:42 +08:00
|
|
|
Writer.W.OS.write(reinterpret_cast<char *>(&Segment), sizeof(Segment));
|
2015-09-03 00:49:13 +08:00
|
|
|
for (unsigned i = 0; i < nsects; ++i) {
|
|
|
|
auto Sect = getSection(Obj, Segment, LCI, i);
|
|
|
|
Sect.offset = Sect.reloff = Sect.nreloc = 0;
|
|
|
|
if (Obj.isLittleEndian() != sys::IsLittleEndianHost)
|
|
|
|
MachO::swapStruct(Sect);
|
2018-05-22 02:17:42 +08:00
|
|
|
Writer.W.OS.write(reinterpret_cast<char *>(&Sect), sizeof(Sect));
|
2015-09-03 00:49:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the __DWARF segment load command to the output file.
|
|
|
|
static void createDwarfSegment(uint64_t VMAddr, uint64_t FileOffset,
|
|
|
|
uint64_t FileSize, unsigned NumSections,
|
|
|
|
MCAsmLayout &Layout, MachObjectWriter &Writer) {
|
|
|
|
Writer.writeSegmentLoadCommand("__DWARF", NumSections, VMAddr,
|
2016-01-15 05:06:47 +08:00
|
|
|
alignTo(FileSize, 0x1000), FileOffset,
|
|
|
|
FileSize, /* MaxProt */ 7,
|
2015-09-03 00:49:13 +08:00
|
|
|
/* InitProt =*/3);
|
|
|
|
|
|
|
|
for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) {
|
|
|
|
MCSection *Sec = Layout.getSectionOrder()[i];
|
|
|
|
if (Sec->begin() == Sec->end() || !Layout.getSectionFileSize(Sec))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
unsigned Align = Sec->getAlignment();
|
|
|
|
if (Align > 1) {
|
2016-01-15 05:06:47 +08:00
|
|
|
VMAddr = alignTo(VMAddr, Align);
|
|
|
|
FileOffset = alignTo(FileOffset, Align);
|
2015-09-03 00:49:13 +08:00
|
|
|
}
|
|
|
|
Writer.writeSection(Layout, *Sec, VMAddr, FileOffset, 0, 0, 0);
|
|
|
|
|
|
|
|
FileOffset += Layout.getSectionAddressSize(Sec);
|
|
|
|
VMAddr += Layout.getSectionAddressSize(Sec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isExecutable(const object::MachOObjectFile &Obj) {
|
|
|
|
if (Obj.is64Bit())
|
|
|
|
return Obj.getHeader64().filetype != MachO::MH_OBJECT;
|
|
|
|
else
|
|
|
|
return Obj.getHeader().filetype != MachO::MH_OBJECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool hasLinkEditSegment(const object::MachOObjectFile &Obj) {
|
|
|
|
bool HasLinkEditSegment = false;
|
|
|
|
iterateOnSegments(Obj, [&](const MachO::segment_command_64 &Segment) {
|
|
|
|
if (StringRef("__LINKEDIT") == Segment.segname)
|
|
|
|
HasLinkEditSegment = true;
|
|
|
|
});
|
|
|
|
return HasLinkEditSegment;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned segmentLoadCommandSize(bool Is64Bit, unsigned NumSections) {
|
|
|
|
if (Is64Bit)
|
|
|
|
return sizeof(MachO::segment_command_64) +
|
|
|
|
NumSections * sizeof(MachO::section_64);
|
|
|
|
|
|
|
|
return sizeof(MachO::segment_command) + NumSections * sizeof(MachO::section);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stream a dSYM companion binary file corresponding to the binary referenced
|
|
|
|
// by \a DM to \a OutFile. The passed \a MS MCStreamer is setup to write to
|
|
|
|
// \a OutFile and it must be using a MachObjectWriter object to do so.
|
2019-01-08 07:27:25 +08:00
|
|
|
bool generateDsymCompanion(const DebugMap &DM, SymbolMapTranslator &Translator,
|
|
|
|
MCStreamer &MS, raw_fd_ostream &OutFile) {
|
2015-09-03 00:49:13 +08:00
|
|
|
auto &ObjectStreamer = static_cast<MCObjectStreamer &>(MS);
|
|
|
|
MCAssembler &MCAsm = ObjectStreamer.getAssembler();
|
|
|
|
auto &Writer = static_cast<MachObjectWriter &>(MCAsm.getWriter());
|
|
|
|
|
2018-07-10 23:32:17 +08:00
|
|
|
// Layout but don't emit.
|
|
|
|
ObjectStreamer.flushPendingLabels();
|
|
|
|
MCAsmLayout Layout(MCAsm);
|
2015-09-03 00:49:13 +08:00
|
|
|
MCAsm.layout(Layout);
|
|
|
|
|
|
|
|
BinaryHolder InputBinaryHolder(false);
|
2018-06-30 00:51:52 +08:00
|
|
|
|
|
|
|
auto ObjectEntry = InputBinaryHolder.getObjectEntry(DM.getBinaryPath());
|
|
|
|
if (!ObjectEntry) {
|
|
|
|
auto Err = ObjectEntry.takeError();
|
2015-09-03 00:49:13 +08:00
|
|
|
return error(Twine("opening ") + DM.getBinaryPath() + ": " +
|
2018-06-30 00:51:52 +08:00
|
|
|
toString(std::move(Err)),
|
2015-09-03 00:49:13 +08:00
|
|
|
"output file streaming");
|
2018-06-30 00:51:52 +08:00
|
|
|
}
|
2015-09-03 00:49:13 +08:00
|
|
|
|
2018-06-30 00:51:52 +08:00
|
|
|
auto Object =
|
|
|
|
ObjectEntry->getObjectAs<object::MachOObjectFile>(DM.getTriple());
|
|
|
|
if (!Object) {
|
|
|
|
auto Err = Object.takeError();
|
2015-09-03 00:49:13 +08:00
|
|
|
return error(Twine("opening ") + DM.getBinaryPath() + ": " +
|
2018-06-30 00:51:52 +08:00
|
|
|
toString(std::move(Err)),
|
2015-09-03 00:49:13 +08:00
|
|
|
"output file streaming");
|
2018-06-30 00:51:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
auto &InputBinary = *Object;
|
2015-09-03 00:49:13 +08:00
|
|
|
|
|
|
|
bool Is64Bit = Writer.is64Bit();
|
|
|
|
MachO::symtab_command SymtabCmd = InputBinary.getSymtabLoadCommand();
|
|
|
|
|
2018-11-09 00:54:59 +08:00
|
|
|
// Compute the number of load commands we will need.
|
|
|
|
unsigned LoadCommandSize = 0;
|
|
|
|
unsigned NumLoadCommands = 0;
|
|
|
|
|
|
|
|
// Get LC_UUID and LC_BUILD_VERSION.
|
2015-09-03 00:49:13 +08:00
|
|
|
MachO::uuid_command UUIDCmd;
|
2018-11-14 07:31:25 +08:00
|
|
|
SmallVector<MachO::build_version_command, 2> BuildVersionCmd;
|
2015-09-03 00:49:13 +08:00
|
|
|
memset(&UUIDCmd, 0, sizeof(UUIDCmd));
|
|
|
|
for (auto &LCI : InputBinary.load_commands()) {
|
2018-11-09 00:54:59 +08:00
|
|
|
switch (LCI.C.cmd) {
|
|
|
|
case MachO::LC_UUID:
|
2018-11-14 07:31:25 +08:00
|
|
|
if (UUIDCmd.cmd)
|
|
|
|
return error("Binary contains more than one UUID");
|
2015-09-03 00:49:13 +08:00
|
|
|
UUIDCmd = InputBinary.getUuidCommand(LCI);
|
2018-11-09 00:54:59 +08:00
|
|
|
++NumLoadCommands;
|
2018-11-14 07:31:25 +08:00
|
|
|
LoadCommandSize += sizeof(UUIDCmd);
|
2018-11-09 00:54:59 +08:00
|
|
|
break;
|
2018-11-14 07:31:25 +08:00
|
|
|
case MachO::LC_BUILD_VERSION: {
|
|
|
|
MachO::build_version_command Cmd;
|
|
|
|
memset(&Cmd, 0, sizeof(Cmd));
|
|
|
|
Cmd = InputBinary.getBuildVersionLoadCommand(LCI);
|
2018-11-09 00:54:59 +08:00
|
|
|
++NumLoadCommands;
|
2018-11-14 07:31:25 +08:00
|
|
|
LoadCommandSize += sizeof(Cmd);
|
2018-11-09 00:54:59 +08:00
|
|
|
// LLDB doesn't care about the build tools for now.
|
2018-11-14 07:31:25 +08:00
|
|
|
Cmd.ntools = 0;
|
|
|
|
BuildVersionCmd.push_back(Cmd);
|
2018-11-09 00:54:59 +08:00
|
|
|
break;
|
2018-11-14 07:31:25 +08:00
|
|
|
}
|
2018-11-09 00:54:59 +08:00
|
|
|
default:
|
2015-09-03 00:49:13 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a valid symtab to copy, do it.
|
|
|
|
bool ShouldEmitSymtab =
|
|
|
|
isExecutable(InputBinary) && hasLinkEditSegment(InputBinary);
|
|
|
|
if (ShouldEmitSymtab) {
|
|
|
|
LoadCommandSize += sizeof(MachO::symtab_command);
|
|
|
|
++NumLoadCommands;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned HeaderSize =
|
|
|
|
Is64Bit ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header);
|
|
|
|
// We will copy every segment that isn't __DWARF.
|
|
|
|
iterateOnSegments(InputBinary, [&](const MachO::segment_command_64 &Segment) {
|
|
|
|
if (StringRef("__DWARF") == Segment.segname)
|
|
|
|
return;
|
|
|
|
|
|
|
|
++NumLoadCommands;
|
|
|
|
LoadCommandSize += segmentLoadCommandSize(Is64Bit, Segment.nsects);
|
|
|
|
});
|
|
|
|
|
|
|
|
// We will add our own brand new __DWARF segment if we have debug
|
|
|
|
// info.
|
|
|
|
unsigned NumDwarfSections = 0;
|
|
|
|
uint64_t DwarfSegmentSize = 0;
|
|
|
|
|
|
|
|
for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) {
|
|
|
|
MCSection *Sec = Layout.getSectionOrder()[i];
|
|
|
|
if (Sec->begin() == Sec->end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (uint64_t Size = Layout.getSectionFileSize(Sec)) {
|
2016-01-15 05:06:47 +08:00
|
|
|
DwarfSegmentSize = alignTo(DwarfSegmentSize, Sec->getAlignment());
|
2015-09-03 00:49:13 +08:00
|
|
|
DwarfSegmentSize += Size;
|
|
|
|
++NumDwarfSections;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NumDwarfSections) {
|
|
|
|
++NumLoadCommands;
|
|
|
|
LoadCommandSize += segmentLoadCommandSize(Is64Bit, NumDwarfSections);
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallString<0> NewSymtab;
|
2019-01-08 07:27:25 +08:00
|
|
|
NonRelocatableStringpool NewStrings(Translator);
|
2015-09-03 00:49:13 +08:00
|
|
|
unsigned NListSize = Is64Bit ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist);
|
|
|
|
unsigned NumSyms = 0;
|
|
|
|
uint64_t NewStringsSize = 0;
|
|
|
|
if (ShouldEmitSymtab) {
|
|
|
|
NewSymtab.reserve(SymtabCmd.nsyms * NListSize / 2);
|
|
|
|
NumSyms = transferSymbols(InputBinary, NewSymtab, NewStrings);
|
|
|
|
NewStringsSize = NewStrings.getSize() + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t SymtabStart = LoadCommandSize;
|
|
|
|
SymtabStart += HeaderSize;
|
2016-01-15 05:06:47 +08:00
|
|
|
SymtabStart = alignTo(SymtabStart, 0x1000);
|
2015-09-03 00:49:13 +08:00
|
|
|
|
|
|
|
// We gathered all the information we need, start emitting the output file.
|
|
|
|
Writer.writeHeader(MachO::MH_DSYM, NumLoadCommands, LoadCommandSize, false);
|
|
|
|
|
|
|
|
// Write the load commands.
|
|
|
|
assert(OutFile.tell() == HeaderSize);
|
|
|
|
if (UUIDCmd.cmd != 0) {
|
2018-05-22 02:17:42 +08:00
|
|
|
Writer.W.write<uint32_t>(UUIDCmd.cmd);
|
2018-11-09 00:54:59 +08:00
|
|
|
Writer.W.write<uint32_t>(sizeof(UUIDCmd));
|
2018-05-22 02:17:42 +08:00
|
|
|
OutFile.write(reinterpret_cast<const char *>(UUIDCmd.uuid), 16);
|
2015-09-03 00:49:13 +08:00
|
|
|
assert(OutFile.tell() == HeaderSize + sizeof(UUIDCmd));
|
|
|
|
}
|
2018-11-14 07:31:25 +08:00
|
|
|
for (auto Cmd : BuildVersionCmd) {
|
|
|
|
Writer.W.write<uint32_t>(Cmd.cmd);
|
|
|
|
Writer.W.write<uint32_t>(sizeof(Cmd));
|
|
|
|
Writer.W.write<uint32_t>(Cmd.platform);
|
|
|
|
Writer.W.write<uint32_t>(Cmd.minos);
|
|
|
|
Writer.W.write<uint32_t>(Cmd.sdk);
|
|
|
|
Writer.W.write<uint32_t>(Cmd.ntools);
|
2018-11-09 00:54:59 +08:00
|
|
|
}
|
2015-09-03 00:49:13 +08:00
|
|
|
|
|
|
|
assert(SymtabCmd.cmd && "No symbol table.");
|
|
|
|
uint64_t StringStart = SymtabStart + NumSyms * NListSize;
|
|
|
|
if (ShouldEmitSymtab)
|
|
|
|
Writer.writeSymtabLoadCommand(SymtabStart, NumSyms, StringStart,
|
|
|
|
NewStringsSize);
|
|
|
|
|
|
|
|
uint64_t DwarfSegmentStart = StringStart + NewStringsSize;
|
2016-01-15 05:06:47 +08:00
|
|
|
DwarfSegmentStart = alignTo(DwarfSegmentStart, 0x1000);
|
2015-09-03 00:49:13 +08:00
|
|
|
|
|
|
|
// Write the load commands for the segments and sections we 'import' from
|
|
|
|
// the original binary.
|
|
|
|
uint64_t EndAddress = 0;
|
|
|
|
uint64_t GapForDwarf = UINT64_MAX;
|
|
|
|
for (auto &LCI : InputBinary.load_commands()) {
|
|
|
|
if (LCI.C.cmd == MachO::LC_SEGMENT)
|
|
|
|
transferSegmentAndSections(LCI, InputBinary.getSegmentLoadCommand(LCI),
|
|
|
|
InputBinary, Writer, SymtabStart,
|
|
|
|
StringStart + NewStringsSize - SymtabStart,
|
|
|
|
DwarfSegmentSize, GapForDwarf, EndAddress);
|
|
|
|
else if (LCI.C.cmd == MachO::LC_SEGMENT_64)
|
|
|
|
transferSegmentAndSections(LCI, InputBinary.getSegment64LoadCommand(LCI),
|
|
|
|
InputBinary, Writer, SymtabStart,
|
|
|
|
StringStart + NewStringsSize - SymtabStart,
|
|
|
|
DwarfSegmentSize, GapForDwarf, EndAddress);
|
|
|
|
}
|
|
|
|
|
2016-01-15 05:06:47 +08:00
|
|
|
uint64_t DwarfVMAddr = alignTo(EndAddress, 0x1000);
|
2015-09-03 00:49:13 +08:00
|
|
|
uint64_t DwarfVMMax = Is64Bit ? UINT64_MAX : UINT32_MAX;
|
|
|
|
if (DwarfVMAddr + DwarfSegmentSize > DwarfVMMax ||
|
|
|
|
DwarfVMAddr + DwarfSegmentSize < DwarfVMAddr /* Overflow */) {
|
|
|
|
// There is no room for the __DWARF segment at the end of the
|
2018-02-22 19:32:51 +08:00
|
|
|
// address space. Look through segments to find a gap.
|
2015-09-03 00:49:13 +08:00
|
|
|
DwarfVMAddr = GapForDwarf;
|
|
|
|
if (DwarfVMAddr == UINT64_MAX)
|
|
|
|
warn("not enough VM space for the __DWARF segment.",
|
|
|
|
"output file streaming");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the load command for the __DWARF segment.
|
|
|
|
createDwarfSegment(DwarfVMAddr, DwarfSegmentStart, DwarfSegmentSize,
|
|
|
|
NumDwarfSections, Layout, Writer);
|
|
|
|
|
|
|
|
assert(OutFile.tell() == LoadCommandSize + HeaderSize);
|
2018-05-22 02:17:42 +08:00
|
|
|
OutFile.write_zeros(SymtabStart - (LoadCommandSize + HeaderSize));
|
2015-09-03 00:49:13 +08:00
|
|
|
assert(OutFile.tell() == SymtabStart);
|
|
|
|
|
|
|
|
// Transfer symbols.
|
|
|
|
if (ShouldEmitSymtab) {
|
2018-05-22 02:17:42 +08:00
|
|
|
OutFile << NewSymtab.str();
|
2015-09-03 00:49:13 +08:00
|
|
|
assert(OutFile.tell() == StringStart);
|
|
|
|
|
|
|
|
// Transfer string table.
|
|
|
|
// FIXME: The NonRelocatableStringpool starts with an empty string, but
|
|
|
|
// dsymutil-classic starts the reconstructed string table with 2 of these.
|
|
|
|
// Reproduce that behavior for now (there is corresponding code in
|
|
|
|
// transferSymbol).
|
2018-05-22 02:17:42 +08:00
|
|
|
OutFile << '\0';
|
[DebugInfo] Reduce debug_str_offsets section size
Summary:
The accelerator tables use the debug_str section to store their strings.
However, they do not support the indirect method of access that is
available for the debug_info section (DW_FORM_strx et al.).
Currently our code is assuming that all strings can/will be referenced
indirectly, and puts all of them into the debug_str_offsets section.
This is generally true for regular (unsplit) dwarf, but in the DWO case,
most of the strings in the debug_str section will only be used from the
accelerator tables. Therefore the contents of the debug_str_offsets
section will be largely unused and bloating the main executable.
This patch rectifies this by teaching the DwarfStringPool to
differentiate between strings accessed directly and indirectly. When a
user inserts a string into the pool it has to declare whether that
string will be referenced directly or not. If at least one user requsts
indirect access, that string will be assigned an index ID and put into
debug_str_offsets table. Otherwise, the offset table is skipped.
This approach reduces the overall binary size (when compiled with
-gdwarf-5 -gsplit-dwarf) in my tests by about 2% (debug_str_offsets is
shrunk by 99%).
Reviewers: probinson, dblaikie, JDevlieghere
Subscribers: aprantl, mgrang, llvm-commits
Differential Revision: https://reviews.llvm.org/D49493
llvm-svn: 339122
2018-08-07 17:54:52 +08:00
|
|
|
std::vector<DwarfStringPoolEntryRef> Strings =
|
|
|
|
NewStrings.getEntriesForEmission();
|
2018-01-25 00:16:43 +08:00
|
|
|
for (auto EntryRef : Strings) {
|
2018-05-22 02:17:42 +08:00
|
|
|
OutFile.write(EntryRef.getString().data(),
|
|
|
|
EntryRef.getString().size() + 1);
|
2018-01-25 00:16:43 +08:00
|
|
|
}
|
2015-09-03 00:49:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(OutFile.tell() == StringStart + NewStringsSize);
|
|
|
|
|
|
|
|
// Pad till the Dwarf segment start.
|
2018-05-22 02:17:42 +08:00
|
|
|
OutFile.write_zeros(DwarfSegmentStart - (StringStart + NewStringsSize));
|
2015-09-03 00:49:13 +08:00
|
|
|
assert(OutFile.tell() == DwarfSegmentStart);
|
|
|
|
|
|
|
|
// Emit the Dwarf sections contents.
|
|
|
|
for (const MCSection &Sec : MCAsm) {
|
|
|
|
if (Sec.begin() == Sec.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
uint64_t Pos = OutFile.tell();
|
2018-05-22 02:17:42 +08:00
|
|
|
OutFile.write_zeros(alignTo(Pos, Sec.getAlignment()) - Pos);
|
2018-05-22 02:11:35 +08:00
|
|
|
MCAsm.writeSectionData(OutFile, &Sec, Layout);
|
2015-09-03 00:49:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-02-22 19:32:51 +08:00
|
|
|
} // namespace MachOUtils
|
|
|
|
} // namespace dsymutil
|
|
|
|
} // namespace llvm
|