2013-08-07 06:31:59 +08:00
|
|
|
//===- lib/ReaderWriter/MachO/MachOLinkingContext.cpp ---------------------===//
|
2013-01-22 10:15:30 +08:00
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
#include "lld/ReaderWriter/MachOLinkingContext.h"
|
2014-07-17 03:49:02 +08:00
|
|
|
#include "ArchHandler.h"
|
2014-08-14 07:55:41 +08:00
|
|
|
#include "File.h"
|
2015-09-29 04:25:14 +08:00
|
|
|
#include "FlatNamespaceFile.h"
|
2014-09-05 04:08:30 +08:00
|
|
|
#include "MachONormalizedFile.h"
|
2014-07-17 03:49:02 +08:00
|
|
|
#include "MachOPasses.h"
|
2015-10-24 16:20:51 +08:00
|
|
|
#include "SectCreateFile.h"
|
2015-01-15 12:34:31 +08:00
|
|
|
#include "lld/Core/ArchiveLibraryFile.h"
|
2013-01-24 04:03:10 +08:00
|
|
|
#include "lld/Core/PassManager.h"
|
2015-01-22 06:54:56 +08:00
|
|
|
#include "lld/Core/Reader.h"
|
|
|
|
#include "lld/Core/Writer.h"
|
2015-01-15 12:34:31 +08:00
|
|
|
#include "lld/Driver/Driver.h"
|
2015-03-02 08:48:06 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2013-04-05 02:59:24 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2013-01-22 10:15:30 +08:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2014-10-01 07:15:39 +08:00
|
|
|
#include "llvm/Config/config.h"
|
2014-12-10 08:33:00 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2015-01-14 19:26:52 +08:00
|
|
|
#include "llvm/Support/Errc.h"
|
2013-11-07 05:36:55 +08:00
|
|
|
#include "llvm/Support/Host.h"
|
2013-09-28 06:50:00 +08:00
|
|
|
#include "llvm/Support/MachO.h"
|
2014-07-10 19:21:06 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2014-08-07 03:37:35 +08:00
|
|
|
#include <algorithm>
|
|
|
|
|
2014-10-27 15:44:40 +08:00
|
|
|
#if defined(HAVE_CXXABI_H)
|
2014-10-01 07:15:39 +08:00
|
|
|
#include <cxxabi.h>
|
|
|
|
#endif
|
|
|
|
|
2014-07-17 03:49:02 +08:00
|
|
|
using lld::mach_o::ArchHandler;
|
2016-01-15 07:25:06 +08:00
|
|
|
using lld::mach_o::MachOFile;
|
2014-08-14 07:55:41 +08:00
|
|
|
using lld::mach_o::MachODylibFile;
|
2013-11-07 05:36:55 +08:00
|
|
|
using namespace llvm::MachO;
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-01-22 10:15:30 +08:00
|
|
|
namespace lld {
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-09-11 07:46:57 +08:00
|
|
|
bool MachOLinkingContext::parsePackedVersion(StringRef str, uint32_t &result) {
|
|
|
|
result = 0;
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
if (str.empty())
|
2013-04-05 02:59:24 +08:00
|
|
|
return false;
|
2013-08-07 06:31:59 +08:00
|
|
|
|
2013-04-05 02:59:24 +08:00
|
|
|
SmallVector<StringRef, 3> parts;
|
|
|
|
llvm::SplitString(str, parts, ".");
|
2013-08-07 06:31:59 +08:00
|
|
|
|
2013-04-05 02:59:24 +08:00
|
|
|
unsigned long long num;
|
|
|
|
if (llvm::getAsUnsignedInteger(parts[0], 10, num))
|
|
|
|
return true;
|
|
|
|
if (num > 65535)
|
|
|
|
return true;
|
2013-09-11 07:46:57 +08:00
|
|
|
result = num << 16;
|
2013-08-07 06:31:59 +08:00
|
|
|
|
2013-04-05 02:59:24 +08:00
|
|
|
if (parts.size() > 1) {
|
|
|
|
if (llvm::getAsUnsignedInteger(parts[1], 10, num))
|
|
|
|
return true;
|
|
|
|
if (num > 255)
|
|
|
|
return true;
|
2013-09-11 07:46:57 +08:00
|
|
|
result |= (num << 8);
|
2013-04-05 02:59:24 +08:00
|
|
|
}
|
2013-08-07 06:31:59 +08:00
|
|
|
|
2013-04-05 02:59:24 +08:00
|
|
|
if (parts.size() > 2) {
|
|
|
|
if (llvm::getAsUnsignedInteger(parts[2], 10, num))
|
|
|
|
return true;
|
|
|
|
if (num > 255)
|
|
|
|
return true;
|
2013-09-11 07:46:57 +08:00
|
|
|
result |= num;
|
2013-04-05 02:59:24 +08:00
|
|
|
}
|
2013-08-07 06:31:59 +08:00
|
|
|
|
2013-04-05 02:59:24 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-04 10:45:23 +08:00
|
|
|
bool MachOLinkingContext::parsePackedVersion(StringRef str, uint64_t &result) {
|
|
|
|
result = 0;
|
|
|
|
|
|
|
|
if (str.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SmallVector<StringRef, 5> parts;
|
|
|
|
llvm::SplitString(str, parts, ".");
|
|
|
|
|
2016-02-04 10:50:47 +08:00
|
|
|
unsigned long long num;
|
2016-02-04 10:45:23 +08:00
|
|
|
if (llvm::getAsUnsignedInteger(parts[0], 10, num))
|
|
|
|
return true;
|
|
|
|
if (num > 0xFFFFFF)
|
|
|
|
return true;
|
|
|
|
result = num << 40;
|
|
|
|
|
|
|
|
unsigned Shift = 30;
|
|
|
|
for (StringRef str : llvm::makeArrayRef(parts).slice(1)) {
|
|
|
|
if (llvm::getAsUnsignedInteger(str, 10, num))
|
|
|
|
return true;
|
|
|
|
if (num > 0x3FF)
|
|
|
|
return true;
|
|
|
|
result |= (num << Shift);
|
|
|
|
Shift -= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
MachOLinkingContext::ArchInfo MachOLinkingContext::_s_archInfos[] = {
|
|
|
|
{ "x86_64", arch_x86_64, true, CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL },
|
|
|
|
{ "i386", arch_x86, true, CPU_TYPE_I386, CPU_SUBTYPE_X86_ALL },
|
|
|
|
{ "ppc", arch_ppc, false, CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_ALL },
|
|
|
|
{ "armv6", arch_armv6, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V6 },
|
|
|
|
{ "armv7", arch_armv7, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7 },
|
|
|
|
{ "armv7s", arch_armv7s, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7S },
|
2014-09-10 07:52:59 +08:00
|
|
|
{ "arm64", arch_arm64, true, CPU_TYPE_ARM64, CPU_SUBTYPE_ARM64_ALL },
|
2013-11-07 05:36:55 +08:00
|
|
|
{ "", arch_unknown,false, 0, 0 }
|
2013-07-20 10:08:23 +08:00
|
|
|
};
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
MachOLinkingContext::Arch
|
|
|
|
MachOLinkingContext::archFromCpuType(uint32_t cputype, uint32_t cpusubtype) {
|
2013-11-07 05:36:55 +08:00
|
|
|
for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
|
|
|
|
if ((info->cputype == cputype) && (info->cpusubtype == cpusubtype))
|
2013-07-20 10:08:23 +08:00
|
|
|
return info->arch;
|
|
|
|
}
|
|
|
|
return arch_unknown;
|
|
|
|
}
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
MachOLinkingContext::Arch
|
|
|
|
MachOLinkingContext::archFromName(StringRef archName) {
|
2013-11-07 05:36:55 +08:00
|
|
|
for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
|
|
|
|
if (info->archName.equals(archName))
|
2013-07-20 10:08:23 +08:00
|
|
|
return info->arch;
|
|
|
|
}
|
|
|
|
return arch_unknown;
|
|
|
|
}
|
|
|
|
|
[lld] Introduce registry and Reference kind tuple
The main changes are in:
include/lld/Core/Reference.h
include/lld/ReaderWriter/Reader.h
Everything else is details to support the main change.
1) Registration based Readers
Previously, lld had a tangled interdependency with all the Readers. It would
have been impossible to make a streamlined linker (say for a JIT) which
just supported one file format and one architecture (no yaml, no archives, etc).
The old model also required a LinkingContext to read an object file, which
would have made .o inspection tools awkward.
The new model is that there is a global Registry object. You programmatically
register the Readers you want with the registry object. Whenever you need to
read/parse a file, you ask the registry to do it, and the registry tries each
registered reader.
For ease of use with the existing lld code base, there is one Registry
object inside the LinkingContext object.
2) Changing kind value to be a tuple
Beside Readers, the registry also keeps track of the mapping for Reference
Kind values to and from strings. Along with that, this patch also fixes
an ambiguity with the previous Reference::Kind values. The problem was that
we wanted to reuse existing relocation type values as Reference::Kind values.
But then how can the YAML write know how to convert a value to a string? The
fix is to change the 32-bit Reference::Kind into a tuple with an 8-bit namespace
(e.g. ELF, COFFF, etc), an 8-bit architecture (e.g. x86_64, PowerPC, etc), and
a 16-bit value. This tuple system allows conversion to and from strings with
no ambiguities.
llvm-svn: 197727
2013-12-20 05:58:00 +08:00
|
|
|
StringRef MachOLinkingContext::nameFromArch(Arch arch) {
|
|
|
|
for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
|
|
|
|
if (info->arch == arch)
|
|
|
|
return info->archName;
|
|
|
|
}
|
|
|
|
return "<unknown>";
|
|
|
|
}
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
uint32_t MachOLinkingContext::cpuTypeFromArch(Arch arch) {
|
2013-07-20 10:08:23 +08:00
|
|
|
assert(arch != arch_unknown);
|
2013-11-07 05:36:55 +08:00
|
|
|
for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
|
|
|
|
if (info->arch == arch)
|
2013-07-20 10:08:23 +08:00
|
|
|
return info->cputype;
|
|
|
|
}
|
|
|
|
llvm_unreachable("Unknown arch type");
|
|
|
|
}
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
uint32_t MachOLinkingContext::cpuSubtypeFromArch(Arch arch) {
|
2013-07-20 10:08:23 +08:00
|
|
|
assert(arch != arch_unknown);
|
2013-11-07 05:36:55 +08:00
|
|
|
for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
|
|
|
|
if (info->arch == arch)
|
2013-07-20 10:08:23 +08:00
|
|
|
return info->cpusubtype;
|
|
|
|
}
|
|
|
|
llvm_unreachable("Unknown arch type");
|
|
|
|
}
|
|
|
|
|
2014-09-05 04:08:30 +08:00
|
|
|
bool MachOLinkingContext::isThinObjectFile(StringRef path, Arch &arch) {
|
|
|
|
return mach_o::normalized::isThinObjectFile(path, arch);
|
|
|
|
}
|
|
|
|
|
2015-04-28 06:48:51 +08:00
|
|
|
bool MachOLinkingContext::sliceFromFatFile(MemoryBufferRef mb, uint32_t &offset,
|
2014-10-08 09:48:10 +08:00
|
|
|
uint32_t &size) {
|
|
|
|
return mach_o::normalized::sliceFromFatFile(mb, _arch, offset, size);
|
|
|
|
}
|
|
|
|
|
2016-03-03 03:06:20 +08:00
|
|
|
MachOLinkingContext::MachOLinkingContext() {}
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2016-03-22 12:00:41 +08:00
|
|
|
MachOLinkingContext::~MachOLinkingContext() {}
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-12-21 09:47:17 +08:00
|
|
|
void MachOLinkingContext::configure(HeaderFileType type, Arch arch, OS os,
|
2016-01-23 05:13:24 +08:00
|
|
|
uint32_t minOSVersion,
|
|
|
|
bool exportDynamicSymbols) {
|
2014-06-20 23:59:00 +08:00
|
|
|
_outputMachOType = type;
|
2013-12-21 09:47:17 +08:00
|
|
|
_arch = arch;
|
|
|
|
_os = os;
|
|
|
|
_osMinVersion = minOSVersion;
|
|
|
|
|
2014-10-09 09:01:16 +08:00
|
|
|
// If min OS not specified on command line, use reasonable defaults.
|
2016-02-04 09:57:59 +08:00
|
|
|
// Note that we only do sensible defaults when emitting something other than
|
|
|
|
// object and preload.
|
|
|
|
if (_outputMachOType != llvm::MachO::MH_OBJECT &&
|
|
|
|
_outputMachOType != llvm::MachO::MH_PRELOAD) {
|
|
|
|
if (minOSVersion == 0) {
|
|
|
|
switch (_arch) {
|
|
|
|
case arch_x86_64:
|
|
|
|
case arch_x86:
|
|
|
|
parsePackedVersion("10.8", _osMinVersion);
|
|
|
|
_os = MachOLinkingContext::OS::macOSX;
|
|
|
|
break;
|
|
|
|
case arch_armv6:
|
|
|
|
case arch_armv7:
|
|
|
|
case arch_armv7s:
|
|
|
|
case arch_arm64:
|
|
|
|
parsePackedVersion("7.0", _osMinVersion);
|
|
|
|
_os = MachOLinkingContext::OS::iOS;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-10-09 09:01:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-20 23:59:00 +08:00
|
|
|
switch (_outputMachOType) {
|
2013-12-21 09:47:17 +08:00
|
|
|
case llvm::MachO::MH_EXECUTE:
|
|
|
|
// If targeting newer OS, use _main
|
|
|
|
if (minOS("10.8", "6.0")) {
|
|
|
|
_entrySymbolName = "_main";
|
|
|
|
} else {
|
|
|
|
// If targeting older OS, use start (in crt1.o)
|
|
|
|
_entrySymbolName = "start";
|
|
|
|
}
|
|
|
|
|
|
|
|
// __PAGEZERO defaults to 4GB on 64-bit (except for PP64 which lld does not
|
|
|
|
// support) and 4KB on 32-bit.
|
|
|
|
if (is64Bit(_arch)) {
|
|
|
|
_pageZeroSize = 0x100000000;
|
|
|
|
} else {
|
|
|
|
_pageZeroSize = 0x1000;
|
|
|
|
}
|
|
|
|
|
2015-09-22 06:06:02 +08:00
|
|
|
// Initial base address is __PAGEZERO size.
|
|
|
|
_baseAddress = _pageZeroSize;
|
|
|
|
|
2014-09-09 08:17:52 +08:00
|
|
|
// Make PIE by default when targetting newer OSs.
|
|
|
|
switch (os) {
|
|
|
|
case OS::macOSX:
|
|
|
|
if (minOSVersion >= 0x000A0700) // MacOSX 10.7
|
|
|
|
_pie = true;
|
|
|
|
break;
|
|
|
|
case OS::iOS:
|
|
|
|
if (minOSVersion >= 0x00040300) // iOS 4.3
|
|
|
|
_pie = true;
|
|
|
|
break;
|
|
|
|
case OS::iOS_simulator:
|
|
|
|
_pie = true;
|
|
|
|
break;
|
|
|
|
case OS::unknown:
|
|
|
|
break;
|
|
|
|
}
|
2016-01-23 05:13:24 +08:00
|
|
|
setGlobalsAreDeadStripRoots(exportDynamicSymbols);
|
2013-12-21 09:47:17 +08:00
|
|
|
break;
|
|
|
|
case llvm::MachO::MH_DYLIB:
|
2016-01-23 05:13:24 +08:00
|
|
|
setGlobalsAreDeadStripRoots(exportDynamicSymbols);
|
2013-12-21 09:47:17 +08:00
|
|
|
break;
|
|
|
|
case llvm::MachO::MH_BUNDLE:
|
|
|
|
break;
|
|
|
|
case llvm::MachO::MH_OBJECT:
|
|
|
|
_printRemainingUndefines = false;
|
|
|
|
_allowRemainingUndefines = true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-09-10 07:52:59 +08:00
|
|
|
|
|
|
|
// Set default segment page sizes based on arch.
|
|
|
|
if (arch == arch_arm64)
|
|
|
|
_pageSize = 4*4096;
|
2013-12-21 09:47:17 +08:00
|
|
|
}
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
uint32_t MachOLinkingContext::getCPUType() const {
|
2013-07-20 10:08:23 +08:00
|
|
|
return cpuTypeFromArch(_arch);
|
2013-01-22 10:15:30 +08:00
|
|
|
}
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
uint32_t MachOLinkingContext::getCPUSubType() const {
|
2013-07-20 10:08:23 +08:00
|
|
|
return cpuSubtypeFromArch(_arch);
|
2013-01-22 10:15:30 +08:00
|
|
|
}
|
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
bool MachOLinkingContext::is64Bit(Arch arch) {
|
|
|
|
for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
|
|
|
|
if (info->arch == arch) {
|
|
|
|
return (info->cputype & CPU_ARCH_ABI64);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// unknown archs are not 64-bit.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MachOLinkingContext::isHostEndian(Arch arch) {
|
|
|
|
assert(arch != arch_unknown);
|
|
|
|
for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
|
|
|
|
if (info->arch == arch) {
|
|
|
|
return (info->littleEndian == llvm::sys::IsLittleEndianHost);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
llvm_unreachable("Unknown arch type");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MachOLinkingContext::isBigEndian(Arch arch) {
|
|
|
|
assert(arch != arch_unknown);
|
|
|
|
for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
|
|
|
|
if (info->arch == arch) {
|
|
|
|
return ! info->littleEndian;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
llvm_unreachable("Unknown arch type");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MachOLinkingContext::is64Bit() const {
|
|
|
|
return is64Bit(_arch);
|
|
|
|
}
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
bool MachOLinkingContext::outputTypeHasEntry() const {
|
2014-06-20 23:59:00 +08:00
|
|
|
switch (_outputMachOType) {
|
2013-11-07 05:36:55 +08:00
|
|
|
case MH_EXECUTE:
|
|
|
|
case MH_DYLINKER:
|
|
|
|
case MH_PRELOAD:
|
2013-01-23 09:18:43 +08:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-17 03:49:02 +08:00
|
|
|
bool MachOLinkingContext::needsStubsPass() const {
|
|
|
|
switch (_outputMachOType) {
|
|
|
|
case MH_EXECUTE:
|
|
|
|
return !_outputMachOTypeStatic;
|
|
|
|
case MH_DYLIB:
|
|
|
|
case MH_BUNDLE:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MachOLinkingContext::needsGOTPass() const {
|
2014-09-10 07:52:59 +08:00
|
|
|
// GOT pass not used in -r mode.
|
|
|
|
if (_outputMachOType == MH_OBJECT)
|
2014-07-17 03:49:02 +08:00
|
|
|
return false;
|
2014-09-10 07:52:59 +08:00
|
|
|
// Only some arches use GOT pass.
|
|
|
|
switch (_arch) {
|
|
|
|
case arch_x86_64:
|
|
|
|
case arch_arm64:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2014-07-17 03:49:02 +08:00
|
|
|
}
|
|
|
|
|
2014-10-01 05:29:54 +08:00
|
|
|
bool MachOLinkingContext::needsCompactUnwindPass() const {
|
|
|
|
switch (_outputMachOType) {
|
|
|
|
case MH_EXECUTE:
|
|
|
|
case MH_DYLIB:
|
|
|
|
case MH_BUNDLE:
|
|
|
|
return archHandler().needsCompactUnwind();
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-07-17 03:49:02 +08:00
|
|
|
|
2016-01-20 05:54:21 +08:00
|
|
|
bool MachOLinkingContext::needsObjCPass() const {
|
|
|
|
// ObjC pass is only needed if any of the inputs were ObjC.
|
|
|
|
return _objcConstraint != objc_unknown;
|
|
|
|
}
|
|
|
|
|
2014-10-14 09:51:42 +08:00
|
|
|
bool MachOLinkingContext::needsShimPass() const {
|
|
|
|
// Shim pass only used in final executables.
|
|
|
|
if (_outputMachOType == MH_OBJECT)
|
|
|
|
return false;
|
|
|
|
// Only 32-bit arm arches use Shim pass.
|
|
|
|
switch (_arch) {
|
|
|
|
case arch_armv6:
|
|
|
|
case arch_armv7:
|
|
|
|
case arch_armv7s:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-24 04:35:31 +08:00
|
|
|
bool MachOLinkingContext::needsTLVPass() const {
|
|
|
|
switch (_outputMachOType) {
|
|
|
|
case MH_BUNDLE:
|
|
|
|
case MH_EXECUTE:
|
|
|
|
case MH_DYLIB:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-17 03:49:02 +08:00
|
|
|
StringRef MachOLinkingContext::binderSymbolName() const {
|
|
|
|
return archHandler().stubInfo().binderSymbolName;
|
|
|
|
}
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
bool MachOLinkingContext::minOS(StringRef mac, StringRef iOS) const {
|
2013-10-08 08:43:34 +08:00
|
|
|
uint32_t parsedVersion;
|
2013-04-05 02:59:24 +08:00
|
|
|
switch (_os) {
|
2013-10-08 08:43:34 +08:00
|
|
|
case OS::macOSX:
|
2013-09-11 07:46:57 +08:00
|
|
|
if (parsePackedVersion(mac, parsedVersion))
|
|
|
|
return false;
|
|
|
|
return _osMinVersion >= parsedVersion;
|
2013-04-05 02:59:24 +08:00
|
|
|
case OS::iOS:
|
2013-10-08 08:43:34 +08:00
|
|
|
case OS::iOS_simulator:
|
2013-09-11 07:46:57 +08:00
|
|
|
if (parsePackedVersion(iOS, parsedVersion))
|
|
|
|
return false;
|
|
|
|
return _osMinVersion >= parsedVersion;
|
2013-10-08 08:43:34 +08:00
|
|
|
case OS::unknown:
|
2016-02-04 09:57:59 +08:00
|
|
|
// If we don't know the target, then assume that we don't meet the min OS.
|
|
|
|
// This matches the ld64 behaviour
|
|
|
|
return false;
|
2013-04-05 02:59:24 +08:00
|
|
|
}
|
2016-02-11 03:28:13 +08:00
|
|
|
llvm_unreachable("invalid OS enum");
|
2013-04-05 02:59:24 +08:00
|
|
|
}
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
bool MachOLinkingContext::addEntryPointLoadCommand() const {
|
2014-06-20 23:59:00 +08:00
|
|
|
if ((_outputMachOType == MH_EXECUTE) && !_outputMachOTypeStatic) {
|
2013-04-05 02:59:24 +08:00
|
|
|
return minOS("10.8", "6.0");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
bool MachOLinkingContext::addUnixThreadLoadCommand() const {
|
2014-06-20 23:59:00 +08:00
|
|
|
switch (_outputMachOType) {
|
2013-11-07 05:36:55 +08:00
|
|
|
case MH_EXECUTE:
|
2014-06-20 23:59:00 +08:00
|
|
|
if (_outputMachOTypeStatic)
|
2013-04-05 02:59:24 +08:00
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return !minOS("10.8", "6.0");
|
|
|
|
break;
|
2013-11-07 05:36:55 +08:00
|
|
|
case MH_DYLINKER:
|
|
|
|
case MH_PRELOAD:
|
2013-01-23 09:18:43 +08:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-10 19:21:06 +08:00
|
|
|
bool MachOLinkingContext::pathExists(StringRef path) const {
|
2014-08-16 03:53:41 +08:00
|
|
|
if (!_testingFileUsage)
|
2014-07-10 19:21:06 +08:00
|
|
|
return llvm::sys::fs::exists(path.str());
|
|
|
|
|
|
|
|
// Otherwise, we're in test mode: only files explicitly provided on the
|
|
|
|
// command-line exist.
|
2014-08-07 03:37:35 +08:00
|
|
|
std::string key = path.str();
|
|
|
|
std::replace(key.begin(), key.end(), '\\', '/');
|
|
|
|
return _existingPaths.find(key) != _existingPaths.end();
|
2014-07-10 19:21:06 +08:00
|
|
|
}
|
|
|
|
|
2014-10-04 08:16:13 +08:00
|
|
|
bool MachOLinkingContext::fileExists(StringRef path) const {
|
|
|
|
bool found = pathExists(path);
|
|
|
|
// Log search misses.
|
|
|
|
if (!found)
|
|
|
|
addInputFileNotFound(path);
|
|
|
|
|
|
|
|
// When testing, file is never opened, so logging is done here.
|
|
|
|
if (_testingFileUsage && found)
|
|
|
|
addInputFileDependency(path);
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2014-08-15 06:20:41 +08:00
|
|
|
void MachOLinkingContext::setSysLibRoots(const StringRefVector &paths) {
|
|
|
|
_syslibRoots = paths;
|
|
|
|
}
|
|
|
|
|
2014-12-19 05:33:38 +08:00
|
|
|
void MachOLinkingContext::addRpath(StringRef rpath) {
|
|
|
|
_rpaths.push_back(rpath);
|
|
|
|
}
|
|
|
|
|
2014-08-14 07:55:41 +08:00
|
|
|
void MachOLinkingContext::addModifiedSearchDir(StringRef libPath,
|
|
|
|
bool isSystemPath) {
|
2014-07-10 19:21:06 +08:00
|
|
|
bool addedModifiedPath = false;
|
|
|
|
|
2014-08-15 06:20:41 +08:00
|
|
|
// -syslibroot only applies to absolute paths.
|
|
|
|
if (libPath.startswith("/")) {
|
2014-08-14 07:55:41 +08:00
|
|
|
for (auto syslibRoot : _syslibRoots) {
|
2014-07-10 19:21:06 +08:00
|
|
|
SmallString<256> path(syslibRoot);
|
|
|
|
llvm::sys::path::append(path, libPath);
|
|
|
|
if (pathExists(path)) {
|
|
|
|
_searchDirs.push_back(path.str().copy(_allocator));
|
|
|
|
addedModifiedPath = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addedModifiedPath)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Finally, if only one -syslibroot is given, system paths which aren't in it
|
|
|
|
// get suppressed.
|
2014-08-14 07:55:41 +08:00
|
|
|
if (_syslibRoots.size() != 1 || !isSystemPath) {
|
2014-07-10 19:21:06 +08:00
|
|
|
if (pathExists(libPath)) {
|
|
|
|
_searchDirs.push_back(libPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-15 06:20:41 +08:00
|
|
|
void MachOLinkingContext::addFrameworkSearchDir(StringRef fwPath,
|
|
|
|
bool isSystemPath) {
|
|
|
|
bool pathAdded = false;
|
|
|
|
|
|
|
|
// -syslibroot only used with to absolute framework search paths.
|
|
|
|
if (fwPath.startswith("/")) {
|
|
|
|
for (auto syslibRoot : _syslibRoots) {
|
|
|
|
SmallString<256> path(syslibRoot);
|
|
|
|
llvm::sys::path::append(path, fwPath);
|
|
|
|
if (pathExists(path)) {
|
|
|
|
_frameworkDirs.push_back(path.str().copy(_allocator));
|
|
|
|
pathAdded = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If fwPath found in any -syslibroot, then done.
|
|
|
|
if (pathAdded)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If only one -syslibroot, system paths not in that SDK are suppressed.
|
|
|
|
if (isSystemPath && (_syslibRoots.size() == 1))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Only use raw fwPath if that directory exists.
|
|
|
|
if (pathExists(fwPath))
|
|
|
|
_frameworkDirs.push_back(fwPath);
|
|
|
|
}
|
|
|
|
|
2014-07-10 19:21:06 +08:00
|
|
|
ErrorOr<StringRef>
|
|
|
|
MachOLinkingContext::searchDirForLibrary(StringRef path,
|
|
|
|
StringRef libName) const {
|
|
|
|
SmallString<256> fullPath;
|
|
|
|
if (libName.endswith(".o")) {
|
|
|
|
// A request ending in .o is special: just search for the file directly.
|
|
|
|
fullPath.assign(path);
|
|
|
|
llvm::sys::path::append(fullPath, libName);
|
2014-10-04 08:16:13 +08:00
|
|
|
if (fileExists(fullPath))
|
2014-07-10 19:21:06 +08:00
|
|
|
return fullPath.str().copy(_allocator);
|
|
|
|
return make_error_code(llvm::errc::no_such_file_or_directory);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search for dynamic library
|
|
|
|
fullPath.assign(path);
|
|
|
|
llvm::sys::path::append(fullPath, Twine("lib") + libName + ".dylib");
|
2014-10-04 08:16:13 +08:00
|
|
|
if (fileExists(fullPath))
|
2014-07-10 19:21:06 +08:00
|
|
|
return fullPath.str().copy(_allocator);
|
|
|
|
|
|
|
|
// If not, try for a static library
|
|
|
|
fullPath.assign(path);
|
|
|
|
llvm::sys::path::append(fullPath, Twine("lib") + libName + ".a");
|
2014-10-04 08:16:13 +08:00
|
|
|
if (fileExists(fullPath))
|
2014-07-10 19:21:06 +08:00
|
|
|
return fullPath.str().copy(_allocator);
|
|
|
|
|
|
|
|
return make_error_code(llvm::errc::no_such_file_or_directory);
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<StringRef> MachOLinkingContext::searchLibrary(StringRef libName) const {
|
|
|
|
SmallString<256> path;
|
|
|
|
for (StringRef dir : searchDirs()) {
|
|
|
|
ErrorOr<StringRef> ec = searchDirForLibrary(dir, libName);
|
|
|
|
if (ec)
|
|
|
|
return ec;
|
|
|
|
}
|
|
|
|
|
|
|
|
return make_error_code(llvm::errc::no_such_file_or_directory);
|
|
|
|
}
|
|
|
|
|
2014-08-15 06:20:41 +08:00
|
|
|
ErrorOr<StringRef> MachOLinkingContext::findPathForFramework(StringRef fwName) const{
|
|
|
|
SmallString<256> fullPath;
|
|
|
|
for (StringRef dir : frameworkDirs()) {
|
|
|
|
fullPath.assign(dir);
|
|
|
|
llvm::sys::path::append(fullPath, Twine(fwName) + ".framework", fwName);
|
2014-10-04 08:16:13 +08:00
|
|
|
if (fileExists(fullPath))
|
2014-08-15 06:20:41 +08:00
|
|
|
return fullPath.str().copy(_allocator);
|
|
|
|
}
|
|
|
|
|
|
|
|
return make_error_code(llvm::errc::no_such_file_or_directory);
|
|
|
|
}
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
bool MachOLinkingContext::validateImpl(raw_ostream &diagnostics) {
|
2013-11-07 05:36:55 +08:00
|
|
|
// TODO: if -arch not specified, look at arch of first .o file.
|
|
|
|
|
2014-06-20 23:59:00 +08:00
|
|
|
if (_currentVersion && _outputMachOType != MH_DYLIB) {
|
2013-09-11 07:55:14 +08:00
|
|
|
diagnostics << "error: -current_version can only be used with dylibs\n";
|
2013-09-25 07:26:34 +08:00
|
|
|
return false;
|
2013-09-11 07:55:14 +08:00
|
|
|
}
|
|
|
|
|
2014-06-20 23:59:00 +08:00
|
|
|
if (_compatibilityVersion && _outputMachOType != MH_DYLIB) {
|
2013-09-11 07:55:14 +08:00
|
|
|
diagnostics
|
|
|
|
<< "error: -compatibility_version can only be used with dylibs\n";
|
2013-09-25 07:26:34 +08:00
|
|
|
return false;
|
2013-09-11 07:55:14 +08:00
|
|
|
}
|
|
|
|
|
2014-06-20 23:59:00 +08:00
|
|
|
if (_deadStrippableDylib && _outputMachOType != MH_DYLIB) {
|
2013-09-11 07:55:14 +08:00
|
|
|
diagnostics
|
|
|
|
<< "error: -mark_dead_strippable_dylib can only be used with dylibs.\n";
|
2013-09-25 07:26:34 +08:00
|
|
|
return false;
|
2013-09-11 07:55:14 +08:00
|
|
|
}
|
|
|
|
|
2014-06-20 23:59:00 +08:00
|
|
|
if (!_bundleLoader.empty() && outputMachOType() != MH_BUNDLE) {
|
2013-09-11 07:55:14 +08:00
|
|
|
diagnostics
|
|
|
|
<< "error: -bundle_loader can only be used with Mach-O bundles\n";
|
2013-09-25 07:26:34 +08:00
|
|
|
return false;
|
2013-09-11 07:55:14 +08:00
|
|
|
}
|
|
|
|
|
2014-08-21 09:59:11 +08:00
|
|
|
// If -exported_symbols_list used, all exported symbols must be defined.
|
|
|
|
if (_exportMode == ExportMode::whiteList) {
|
|
|
|
for (const auto &symbol : _exportedSymbols)
|
|
|
|
addInitialUndefinedSymbol(symbol.getKey());
|
|
|
|
}
|
|
|
|
|
2014-08-22 04:25:50 +08:00
|
|
|
// If -dead_strip, set up initial live symbols.
|
|
|
|
if (deadStrip()) {
|
|
|
|
// Entry point is live.
|
|
|
|
if (outputTypeHasEntry())
|
|
|
|
addDeadStripRoot(entrySymbolName());
|
|
|
|
// Lazy binding helper is live.
|
|
|
|
if (needsStubsPass())
|
|
|
|
addDeadStripRoot(binderSymbolName());
|
|
|
|
// If using -exported_symbols_list, make all exported symbols live.
|
|
|
|
if (_exportMode == ExportMode::whiteList) {
|
2015-03-09 14:05:42 +08:00
|
|
|
setGlobalsAreDeadStripRoots(false);
|
2014-08-22 04:25:50 +08:00
|
|
|
for (const auto &symbol : _exportedSymbols)
|
|
|
|
addDeadStripRoot(symbol.getKey());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-04 08:16:13 +08:00
|
|
|
addOutputFileDependency(outputPath());
|
|
|
|
|
2013-09-25 07:26:34 +08:00
|
|
|
return true;
|
2013-04-05 02:59:24 +08:00
|
|
|
}
|
|
|
|
|
2013-10-29 13:12:14 +08:00
|
|
|
void MachOLinkingContext::addPasses(PassManager &pm) {
|
2016-01-20 05:54:21 +08:00
|
|
|
// objc pass should be before layout pass. Otherwise test cases may contain
|
|
|
|
// no atoms which confuses the layout pass.
|
|
|
|
if (needsObjCPass())
|
|
|
|
mach_o::addObjCPass(pm, *this);
|
2015-02-06 04:05:33 +08:00
|
|
|
mach_o::addLayoutPass(pm, *this);
|
2014-07-17 03:49:02 +08:00
|
|
|
if (needsStubsPass())
|
|
|
|
mach_o::addStubsPass(pm, *this);
|
2014-10-01 05:29:54 +08:00
|
|
|
if (needsCompactUnwindPass())
|
|
|
|
mach_o::addCompactUnwindPass(pm, *this);
|
2014-07-17 03:49:02 +08:00
|
|
|
if (needsGOTPass())
|
|
|
|
mach_o::addGOTPass(pm, *this);
|
2015-06-24 04:35:31 +08:00
|
|
|
if (needsTLVPass())
|
|
|
|
mach_o::addTLVPass(pm, *this);
|
2014-10-14 09:51:42 +08:00
|
|
|
if (needsShimPass())
|
|
|
|
mach_o::addShimPass(pm, *this); // Shim pass must run after stubs pass.
|
2013-04-05 02:59:24 +08:00
|
|
|
}
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
Writer &MachOLinkingContext::writer() const {
|
2014-06-20 23:59:00 +08:00
|
|
|
if (!_writer)
|
2013-04-05 02:59:24 +08:00
|
|
|
_writer = createWriterMachO(*this);
|
|
|
|
return *_writer;
|
|
|
|
}
|
|
|
|
|
2015-01-24 07:26:13 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>>
|
|
|
|
MachOLinkingContext::getMemoryBuffer(StringRef path) {
|
|
|
|
addInputFileDependency(path);
|
|
|
|
|
2015-01-15 12:34:31 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> mbOrErr =
|
2015-01-24 07:26:13 +08:00
|
|
|
MemoryBuffer::getFileOrSTDIN(path);
|
|
|
|
if (std::error_code ec = mbOrErr.getError())
|
|
|
|
return ec;
|
|
|
|
std::unique_ptr<MemoryBuffer> mb = std::move(mbOrErr.get());
|
|
|
|
|
|
|
|
// If buffer contains a fat file, find required arch in fat buffer
|
|
|
|
// and switch buffer to point to just that required slice.
|
|
|
|
uint32_t offset;
|
|
|
|
uint32_t size;
|
2015-04-28 06:48:51 +08:00
|
|
|
if (sliceFromFatFile(mb->getMemBufferRef(), offset, size))
|
2015-01-24 07:26:13 +08:00
|
|
|
return MemoryBuffer::getFileSlice(path, size, offset);
|
|
|
|
return std::move(mb);
|
|
|
|
}
|
|
|
|
|
|
|
|
MachODylibFile* MachOLinkingContext::loadIndirectDylib(StringRef path) {
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> mbOrErr = getMemoryBuffer(path);
|
2015-01-15 12:34:31 +08:00
|
|
|
if (mbOrErr.getError())
|
2014-08-14 07:55:41 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2015-04-25 02:51:30 +08:00
|
|
|
ErrorOr<std::unique_ptr<File>> fileOrErr =
|
|
|
|
registry().loadFile(std::move(mbOrErr.get()));
|
|
|
|
if (!fileOrErr)
|
2015-01-15 12:34:31 +08:00
|
|
|
return nullptr;
|
2015-04-25 03:01:30 +08:00
|
|
|
std::unique_ptr<File> &file = fileOrErr.get();
|
|
|
|
file->parse();
|
|
|
|
MachODylibFile *result = reinterpret_cast<MachODylibFile *>(file.get());
|
2014-08-14 07:55:41 +08:00
|
|
|
// Node object now owned by _indirectDylibs vector.
|
2015-04-25 03:01:30 +08:00
|
|
|
_indirectDylibs.push_back(std::move(file));
|
2014-08-14 07:55:41 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-10-02 04:24:30 +08:00
|
|
|
MachODylibFile* MachOLinkingContext::findIndirectDylib(StringRef path) {
|
2014-08-14 07:55:41 +08:00
|
|
|
// See if already loaded.
|
|
|
|
auto pos = _pathToDylibMap.find(path);
|
|
|
|
if (pos != _pathToDylibMap.end())
|
|
|
|
return pos->second;
|
|
|
|
|
|
|
|
// Search -L paths if of the form "libXXX.dylib"
|
|
|
|
std::pair<StringRef, StringRef> split = path.rsplit('/');
|
|
|
|
StringRef leafName = split.second;
|
|
|
|
if (leafName.startswith("lib") && leafName.endswith(".dylib")) {
|
|
|
|
// FIXME: Need to enhance searchLibrary() to only look for .dylib
|
|
|
|
auto libPath = searchLibrary(leafName);
|
|
|
|
if (!libPath.getError()) {
|
|
|
|
return loadIndirectDylib(libPath.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try full path with sysroot.
|
|
|
|
for (StringRef sysPath : _syslibRoots) {
|
|
|
|
SmallString<256> fullPath;
|
|
|
|
fullPath.assign(sysPath);
|
|
|
|
llvm::sys::path::append(fullPath, path);
|
|
|
|
if (pathExists(fullPath))
|
|
|
|
return loadIndirectDylib(fullPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try full path.
|
|
|
|
if (pathExists(path)) {
|
|
|
|
return loadIndirectDylib(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-11-19 10:21:53 +08:00
|
|
|
uint32_t MachOLinkingContext::dylibCurrentVersion(StringRef installName) const {
|
|
|
|
auto pos = _pathToDylibMap.find(installName);
|
|
|
|
if (pos != _pathToDylibMap.end())
|
|
|
|
return pos->second->currentVersion();
|
|
|
|
else
|
|
|
|
return 0x1000; // 1.0
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t MachOLinkingContext::dylibCompatVersion(StringRef installName) const {
|
|
|
|
auto pos = _pathToDylibMap.find(installName);
|
|
|
|
if (pos != _pathToDylibMap.end())
|
|
|
|
return pos->second->compatVersion();
|
|
|
|
else
|
|
|
|
return 0x1000; // 1.0
|
|
|
|
}
|
|
|
|
|
2015-04-07 04:43:35 +08:00
|
|
|
void MachOLinkingContext::createImplicitFiles(
|
2014-10-02 04:24:30 +08:00
|
|
|
std::vector<std::unique_ptr<File> > &result) {
|
2014-08-14 07:55:41 +08:00
|
|
|
// Add indirect dylibs by asking each linked dylib to add its indirects.
|
|
|
|
// Iterate until no more dylibs get loaded.
|
|
|
|
size_t dylibCount = 0;
|
|
|
|
while (dylibCount != _allDylibs.size()) {
|
|
|
|
dylibCount = _allDylibs.size();
|
|
|
|
for (MachODylibFile *dylib : _allDylibs) {
|
|
|
|
dylib->loadReExportedDylibs([this] (StringRef path) -> MachODylibFile* {
|
|
|
|
return findIndirectDylib(path); });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let writer add output type specific extras.
|
2015-04-07 04:43:35 +08:00
|
|
|
writer().createImplicitFiles(result);
|
2015-09-29 04:25:14 +08:00
|
|
|
|
2015-09-29 04:52:21 +08:00
|
|
|
// If undefinedMode is != error, add a FlatNamespaceFile instance. This will
|
|
|
|
// provide a SharedLibraryAtom for symbols that aren't defined elsewhere.
|
|
|
|
if (undefinedMode() != UndefinedMode::error) {
|
|
|
|
result.emplace_back(new mach_o::FlatNamespaceFile(*this));
|
2015-09-29 04:25:14 +08:00
|
|
|
_flatNamespaceFile = result.back().get();
|
|
|
|
}
|
2014-08-14 07:55:41 +08:00
|
|
|
}
|
|
|
|
|
2014-10-17 03:31:28 +08:00
|
|
|
void MachOLinkingContext::registerDylib(MachODylibFile *dylib,
|
|
|
|
bool upward) const {
|
2015-05-13 08:17:08 +08:00
|
|
|
std::lock_guard<std::mutex> lock(_dylibsMutex);
|
2014-08-14 07:55:41 +08:00
|
|
|
_allDylibs.insert(dylib);
|
|
|
|
_pathToDylibMap[dylib->installName()] = dylib;
|
|
|
|
// If path is different than install name, register path too.
|
|
|
|
if (!dylib->path().equals(dylib->installName()))
|
|
|
|
_pathToDylibMap[dylib->path()] = dylib;
|
2014-10-17 03:31:28 +08:00
|
|
|
if (upward)
|
|
|
|
_upwardDylibs.insert(dylib);
|
2014-08-14 07:55:41 +08:00
|
|
|
}
|
|
|
|
|
2014-10-17 03:31:28 +08:00
|
|
|
bool MachOLinkingContext::isUpwardDylib(StringRef installName) const {
|
|
|
|
for (MachODylibFile *dylib : _upwardDylibs) {
|
|
|
|
if (dylib->installName().equals(installName))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-17 03:49:02 +08:00
|
|
|
ArchHandler &MachOLinkingContext::archHandler() const {
|
|
|
|
if (!_archHandler)
|
|
|
|
_archHandler = ArchHandler::create(_arch);
|
|
|
|
return *_archHandler;
|
2013-04-05 02:59:24 +08:00
|
|
|
}
|
2013-01-22 10:15:30 +08:00
|
|
|
|
2014-07-30 08:58:06 +08:00
|
|
|
void MachOLinkingContext::addSectionAlignment(StringRef seg, StringRef sect,
|
2015-03-26 10:23:45 +08:00
|
|
|
uint16_t align) {
|
|
|
|
SectionAlign entry = { seg, sect, align };
|
2014-07-30 08:58:06 +08:00
|
|
|
_sectAligns.push_back(entry);
|
|
|
|
}
|
|
|
|
|
2015-10-24 16:20:51 +08:00
|
|
|
void MachOLinkingContext::addSectCreateSection(
|
|
|
|
StringRef seg, StringRef sect,
|
|
|
|
std::unique_ptr<MemoryBuffer> content) {
|
|
|
|
|
|
|
|
if (!_sectCreateFile) {
|
|
|
|
auto sectCreateFile = llvm::make_unique<mach_o::SectCreateFile>();
|
|
|
|
_sectCreateFile = sectCreateFile.get();
|
|
|
|
getNodes().push_back(llvm::make_unique<FileNode>(std::move(sectCreateFile)));
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(_sectCreateFile && "sectcreate file does not exist.");
|
|
|
|
_sectCreateFile->addSection(seg, sect, std::move(content));
|
|
|
|
}
|
|
|
|
|
2014-07-30 08:58:06 +08:00
|
|
|
bool MachOLinkingContext::sectionAligned(StringRef seg, StringRef sect,
|
2015-03-26 10:23:45 +08:00
|
|
|
uint16_t &align) const {
|
2014-07-30 08:58:06 +08:00
|
|
|
for (const SectionAlign &entry : _sectAligns) {
|
|
|
|
if (seg.equals(entry.segmentName) && sect.equals(entry.sectionName)) {
|
2015-03-26 10:23:45 +08:00
|
|
|
align = entry.align;
|
2014-07-30 08:58:06 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-21 09:59:11 +08:00
|
|
|
void MachOLinkingContext::addExportSymbol(StringRef sym) {
|
2014-10-25 06:28:54 +08:00
|
|
|
// Support old crufty export lists with bogus entries.
|
|
|
|
if (sym.endswith(".eh") || sym.startswith(".objc_category_name_")) {
|
|
|
|
llvm::errs() << "warning: ignoring " << sym << " in export list\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Only i386 MacOSX uses old ABI, so don't change those.
|
|
|
|
if ((_os != OS::macOSX) || (_arch != arch_x86)) {
|
|
|
|
// ObjC has two differnent ABIs. Be nice and allow one export list work for
|
|
|
|
// both ABIs by renaming symbols.
|
|
|
|
if (sym.startswith(".objc_class_name_")) {
|
|
|
|
std::string abi2className("_OBJC_CLASS_$_");
|
|
|
|
abi2className += sym.substr(17);
|
|
|
|
_exportedSymbols.insert(copy(abi2className));
|
|
|
|
std::string abi2metaclassName("_OBJC_METACLASS_$_");
|
|
|
|
abi2metaclassName += sym.substr(17);
|
|
|
|
_exportedSymbols.insert(copy(abi2metaclassName));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-21 09:59:11 +08:00
|
|
|
// FIXME: Support wildcards.
|
|
|
|
_exportedSymbols.insert(sym);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MachOLinkingContext::exportSymbolNamed(StringRef sym) const {
|
|
|
|
switch (_exportMode) {
|
|
|
|
case ExportMode::globals:
|
|
|
|
llvm_unreachable("exportSymbolNamed() should not be called in this mode");
|
|
|
|
break;
|
|
|
|
case ExportMode::whiteList:
|
|
|
|
return _exportedSymbols.count(sym);
|
|
|
|
case ExportMode::blackList:
|
|
|
|
return !_exportedSymbols.count(sym);
|
|
|
|
}
|
2014-09-21 13:07:44 +08:00
|
|
|
llvm_unreachable("_exportMode unknown enum value");
|
2014-08-21 09:59:11 +08:00
|
|
|
}
|
|
|
|
|
2014-10-01 07:15:39 +08:00
|
|
|
std::string MachOLinkingContext::demangle(StringRef symbolName) const {
|
|
|
|
// Only try to demangle symbols if -demangle on command line
|
2015-02-18 11:54:21 +08:00
|
|
|
if (!demangleSymbols())
|
2014-10-01 07:15:39 +08:00
|
|
|
return symbolName;
|
|
|
|
|
|
|
|
// Only try to demangle symbols that look like C++ symbols
|
|
|
|
if (!symbolName.startswith("__Z"))
|
|
|
|
return symbolName;
|
|
|
|
|
2014-10-27 15:44:40 +08:00
|
|
|
#if defined(HAVE_CXXABI_H)
|
2014-10-01 07:15:39 +08:00
|
|
|
SmallString<256> symBuff;
|
|
|
|
StringRef nullTermSym = Twine(symbolName).toNullTerminatedStringRef(symBuff);
|
|
|
|
// Mach-O has extra leading underscore that needs to be removed.
|
|
|
|
const char *cstr = nullTermSym.data() + 1;
|
|
|
|
int status;
|
|
|
|
char *demangled = abi::__cxa_demangle(cstr, nullptr, nullptr, &status);
|
2015-10-02 08:36:00 +08:00
|
|
|
if (demangled) {
|
2014-10-01 07:15:39 +08:00
|
|
|
std::string result(demangled);
|
|
|
|
// __cxa_demangle() always uses a malloc'ed buffer to return the result.
|
|
|
|
free(demangled);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return symbolName;
|
|
|
|
}
|
|
|
|
|
2014-10-04 08:16:13 +08:00
|
|
|
std::error_code MachOLinkingContext::createDependencyFile(StringRef path) {
|
|
|
|
std::error_code ec;
|
|
|
|
_dependencyInfo = std::unique_ptr<llvm::raw_fd_ostream>(new
|
|
|
|
llvm::raw_fd_ostream(path, ec, llvm::sys::fs::F_None));
|
|
|
|
if (ec) {
|
|
|
|
_dependencyInfo.reset();
|
|
|
|
return ec;
|
|
|
|
}
|
|
|
|
|
|
|
|
char linkerVersionOpcode = 0x00;
|
|
|
|
*_dependencyInfo << linkerVersionOpcode;
|
|
|
|
*_dependencyInfo << "lld"; // FIXME
|
|
|
|
*_dependencyInfo << '\0';
|
|
|
|
|
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachOLinkingContext::addInputFileDependency(StringRef path) const {
|
|
|
|
if (!_dependencyInfo)
|
|
|
|
return;
|
|
|
|
|
|
|
|
char inputFileOpcode = 0x10;
|
|
|
|
*_dependencyInfo << inputFileOpcode;
|
|
|
|
*_dependencyInfo << path;
|
|
|
|
*_dependencyInfo << '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachOLinkingContext::addInputFileNotFound(StringRef path) const {
|
|
|
|
if (!_dependencyInfo)
|
|
|
|
return;
|
|
|
|
|
|
|
|
char inputFileOpcode = 0x11;
|
|
|
|
*_dependencyInfo << inputFileOpcode;
|
|
|
|
*_dependencyInfo << path;
|
|
|
|
*_dependencyInfo << '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachOLinkingContext::addOutputFileDependency(StringRef path) const {
|
|
|
|
if (!_dependencyInfo)
|
|
|
|
return;
|
|
|
|
|
|
|
|
char outputFileOpcode = 0x40;
|
|
|
|
*_dependencyInfo << outputFileOpcode;
|
|
|
|
*_dependencyInfo << path;
|
|
|
|
*_dependencyInfo << '\0';
|
|
|
|
}
|
|
|
|
|
2014-11-08 05:01:21 +08:00
|
|
|
void MachOLinkingContext::appendOrderedSymbol(StringRef symbol,
|
|
|
|
StringRef filename) {
|
|
|
|
// To support sorting static functions which may have the same name in
|
|
|
|
// multiple .o files, _orderFiles maps the symbol name to a vector
|
|
|
|
// of OrderFileNode each of which can specify a file prefix.
|
|
|
|
OrderFileNode info;
|
|
|
|
if (!filename.empty())
|
|
|
|
info.fileFilter = copy(filename);
|
|
|
|
info.order = _orderFileEntries++;
|
|
|
|
_orderFiles[symbol].push_back(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MachOLinkingContext::findOrderOrdinal(const std::vector<OrderFileNode> &nodes,
|
|
|
|
const DefinedAtom *atom,
|
|
|
|
unsigned &ordinal) {
|
|
|
|
const File *objFile = &atom->file();
|
|
|
|
assert(objFile);
|
|
|
|
StringRef objName = objFile->path();
|
|
|
|
std::pair<StringRef, StringRef> dirAndLeaf = objName.rsplit('/');
|
|
|
|
if (!dirAndLeaf.second.empty())
|
|
|
|
objName = dirAndLeaf.second;
|
|
|
|
for (const OrderFileNode &info : nodes) {
|
|
|
|
if (info.fileFilter.empty()) {
|
|
|
|
// Have unprefixed symbol name in order file that matches this atom.
|
|
|
|
ordinal = info.order;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (info.fileFilter.equals(objName)) {
|
|
|
|
// Have prefixed symbol name in order file that matches atom's path.
|
|
|
|
ordinal = info.order;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MachOLinkingContext::customAtomOrderer(const DefinedAtom *left,
|
|
|
|
const DefinedAtom *right,
|
2015-02-06 04:05:33 +08:00
|
|
|
bool &leftBeforeRight) const {
|
2014-11-08 05:01:21 +08:00
|
|
|
// No custom sorting if no order file entries.
|
|
|
|
if (!_orderFileEntries)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Order files can only order named atoms.
|
|
|
|
StringRef leftName = left->name();
|
|
|
|
StringRef rightName = right->name();
|
|
|
|
if (leftName.empty() || rightName.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If neither is in order file list, no custom sorter.
|
|
|
|
auto leftPos = _orderFiles.find(leftName);
|
|
|
|
auto rightPos = _orderFiles.find(rightName);
|
|
|
|
bool leftIsOrdered = (leftPos != _orderFiles.end());
|
|
|
|
bool rightIsOrdered = (rightPos != _orderFiles.end());
|
|
|
|
if (!leftIsOrdered && !rightIsOrdered)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// There could be multiple symbols with same name but different file prefixes.
|
|
|
|
unsigned leftOrder;
|
|
|
|
unsigned rightOrder;
|
|
|
|
bool foundLeft =
|
|
|
|
leftIsOrdered && findOrderOrdinal(leftPos->getValue(), left, leftOrder);
|
|
|
|
bool foundRight = rightIsOrdered &&
|
|
|
|
findOrderOrdinal(rightPos->getValue(), right, rightOrder);
|
|
|
|
if (!foundLeft && !foundRight)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If only one is in order file list, ordered one goes first.
|
|
|
|
if (foundLeft != foundRight)
|
|
|
|
leftBeforeRight = foundLeft;
|
|
|
|
else
|
|
|
|
leftBeforeRight = (leftOrder < rightOrder);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2014-08-21 09:59:11 +08:00
|
|
|
|
2015-01-15 16:31:46 +08:00
|
|
|
static bool isLibrary(const std::unique_ptr<Node> &elem) {
|
2015-01-15 16:51:23 +08:00
|
|
|
if (FileNode *node = dyn_cast<FileNode>(const_cast<Node *>(elem.get()))) {
|
|
|
|
File *file = node->getFile();
|
|
|
|
return isa<SharedLibraryFile>(file) || isa<ArchiveLibraryFile>(file);
|
|
|
|
}
|
|
|
|
return false;
|
2014-12-10 08:33:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// The darwin linker processes input files in two phases. The first phase
|
|
|
|
// links in all object (.o) files in command line order. The second phase
|
|
|
|
// links in libraries in command line order.
|
|
|
|
// In this function we reorder the input files so that all the object files
|
|
|
|
// comes before any library file. We also make a group for the library files
|
|
|
|
// so that the Resolver will reiterate over the libraries as long as we find
|
|
|
|
// new undefines from libraries.
|
2015-03-14 18:34:43 +08:00
|
|
|
void MachOLinkingContext::finalizeInputFiles() {
|
2015-01-15 16:46:36 +08:00
|
|
|
std::vector<std::unique_ptr<Node>> &elements = getNodes();
|
2014-12-10 08:33:00 +08:00
|
|
|
std::stable_sort(elements.begin(), elements.end(),
|
2015-01-15 16:31:46 +08:00
|
|
|
[](const std::unique_ptr<Node> &a,
|
|
|
|
const std::unique_ptr<Node> &b) {
|
2014-12-10 08:33:00 +08:00
|
|
|
return !isLibrary(a) && isLibrary(b);
|
|
|
|
});
|
|
|
|
size_t numLibs = std::count_if(elements.begin(), elements.end(), isLibrary);
|
|
|
|
elements.push_back(llvm::make_unique<GroupEnd>(numLibs));
|
|
|
|
}
|
|
|
|
|
2016-01-15 05:53:13 +08:00
|
|
|
std::error_code MachOLinkingContext::handleLoadedFile(File &file) {
|
2016-01-15 07:25:06 +08:00
|
|
|
auto *machoFile = dyn_cast<MachOFile>(&file);
|
|
|
|
if (!machoFile)
|
|
|
|
return std::error_code();
|
|
|
|
|
|
|
|
// Check that the arch of the context matches that of the file.
|
|
|
|
// Also set the arch of the context if it didn't have one.
|
|
|
|
if (_arch == arch_unknown) {
|
|
|
|
_arch = machoFile->arch();
|
|
|
|
} else if (machoFile->arch() != arch_unknown && machoFile->arch() != _arch) {
|
|
|
|
// Archs are different.
|
|
|
|
return make_dynamic_error_code(file.path() +
|
|
|
|
Twine(" cannot be linked due to incompatible architecture"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the OS of the context matches that of the file.
|
|
|
|
// Also set the OS of the context if it didn't have one.
|
|
|
|
if (_os == OS::unknown) {
|
|
|
|
_os = machoFile->OS();
|
|
|
|
} else if (machoFile->OS() != OS::unknown && machoFile->OS() != _os) {
|
|
|
|
// OSes are different.
|
|
|
|
return make_dynamic_error_code(file.path() +
|
|
|
|
Twine(" cannot be linked due to incompatible operating systems"));
|
|
|
|
}
|
2016-01-16 08:07:22 +08:00
|
|
|
|
2016-01-20 03:46:41 +08:00
|
|
|
// Check that if the objc info exists, that it is compatible with the target
|
|
|
|
// OS.
|
|
|
|
switch (machoFile->objcConstraint()) {
|
|
|
|
case objc_unknown:
|
|
|
|
// The file is not compiled with objc, so skip the checks.
|
|
|
|
break;
|
|
|
|
case objc_gc_only:
|
|
|
|
case objc_supports_gc:
|
|
|
|
llvm_unreachable("GC support should already have thrown an error");
|
|
|
|
case objc_retainReleaseForSimulator:
|
|
|
|
// The file is built with simulator objc, so make sure that the context
|
|
|
|
// is also building with simulator support.
|
|
|
|
if (_os != OS::iOS_simulator)
|
|
|
|
return make_dynamic_error_code(file.path() +
|
|
|
|
Twine(" cannot be linked. It contains ObjC built for the simulator"
|
|
|
|
" while we are linking a non-simulator target"));
|
|
|
|
assert((_objcConstraint == objc_unknown ||
|
|
|
|
_objcConstraint == objc_retainReleaseForSimulator) &&
|
|
|
|
"Must be linking with retain/release for the simulator");
|
|
|
|
_objcConstraint = objc_retainReleaseForSimulator;
|
|
|
|
break;
|
|
|
|
case objc_retainRelease:
|
|
|
|
// The file is built without simulator objc, so make sure that the
|
|
|
|
// context is also building without simulator support.
|
|
|
|
if (_os == OS::iOS_simulator)
|
|
|
|
return make_dynamic_error_code(file.path() +
|
|
|
|
Twine(" cannot be linked. It contains ObjC built for a non-simulator"
|
|
|
|
" target while we are linking a simulator target"));
|
|
|
|
assert((_objcConstraint == objc_unknown ||
|
|
|
|
_objcConstraint == objc_retainRelease) &&
|
|
|
|
"Must be linking with retain/release for a non-simulator target");
|
|
|
|
_objcConstraint = objc_retainRelease;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-01-16 08:07:22 +08:00
|
|
|
// Check that the swift version of the context matches that of the file.
|
|
|
|
// Also set the swift version of the context if it didn't have one.
|
|
|
|
if (!_swiftVersion) {
|
|
|
|
_swiftVersion = machoFile->swiftVersion();
|
|
|
|
} else if (machoFile->swiftVersion() &&
|
|
|
|
machoFile->swiftVersion() != _swiftVersion) {
|
|
|
|
// Swift versions are different.
|
|
|
|
return make_dynamic_error_code("different swift versions");
|
|
|
|
}
|
2016-01-20 05:54:21 +08:00
|
|
|
|
2016-01-15 05:53:13 +08:00
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
|
2013-01-22 10:15:30 +08:00
|
|
|
} // end namespace lld
|