[lld-macho] handle option -headerpad_max_install_names

Differential Revision: https://reviews.llvm.org/D88064
This commit is contained in:
Greg McGary 2020-09-21 11:04:13 -07:00
parent 10092291d7
commit 145ce86dba
5 changed files with 80 additions and 9 deletions

View File

@ -36,6 +36,7 @@ struct Configuration {
bool allLoad = false;
bool forceLoadObjC = false;
bool staticLink = false;
bool headerPadMaxInstallNames = false;
uint32_t headerPad;
llvm::StringRef installName;
llvm::StringRef outputFile;

View File

@ -521,6 +521,8 @@ bool macho::link(llvm::ArrayRef<const char *> argsArr, bool canExitEarly,
config->installName =
args.getLastArgValue(OPT_install_name, config->outputFile);
config->headerPad = args::getHex(args, OPT_headerpad, /*Default=*/32);
config->headerPadMaxInstallNames =
args.hasArg(OPT_headerpad_max_install_names);
config->outputType = args.hasArg(OPT_dylib) ? MH_DYLIB : MH_EXECUTE;
config->runtimePaths = args::getStrings(args, OPT_rpath);
config->allLoad = args.hasArg(OPT_all_load);
@ -599,6 +601,7 @@ bool macho::link(llvm::ArrayRef<const char *> argsArr, bool canExitEarly,
case OPT_L:
case OPT_ObjC:
case OPT_headerpad:
case OPT_headerpad_max_install_names:
case OPT_install_name:
case OPT_rpath:
case OPT_sub_library:

View File

@ -689,11 +689,10 @@ def umbrella : Separate<["-"], "umbrella">,
Group<grp_rare>;
def headerpad : Separate<["-"], "headerpad">,
MetaVarName<"<size>">,
HelpText<"Allocate hex <size> extra space for future expansion of the load commands via install_name_tool">,
HelpText<"Allocate hex <size> extra space for future expansion of the load commands via install_name_tool (default is 0x20)">,
Group<grp_rare>;
def headerpad_max_install_names : Flag<["-"], "headerpad_max_install_names">,
HelpText<"Allocate extra space so all load-command paths can expand to MAXPATHLEN via install_name_tool">,
Flags<[HelpHidden]>,
Group<grp_rare>;
def bind_at_load : Flag<["-"], "bind_at_load">,
HelpText<"Tell dyld to bind all symbols at load time, rather than lazily">,

View File

@ -27,6 +27,8 @@
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/Path.h"
#include <algorithm>
using namespace llvm;
using namespace llvm::MachO;
using namespace lld;
@ -208,7 +210,9 @@ public:
// * LC_REEXPORT_DYLIB
class LCDylib : public LoadCommand {
public:
LCDylib(LoadCommandType type, StringRef path) : type(type), path(path) {}
LCDylib(LoadCommandType type, StringRef path) : type(type), path(path) {
instanceCount++;
}
uint32_t getSize() const override {
return alignTo(sizeof(dylib_command) + path.size() + 1, 8);
@ -226,11 +230,16 @@ public:
buf[path.size()] = '\0';
}
static uint32_t getInstanceCount() { return instanceCount; }
private:
LoadCommandType type;
StringRef path;
static uint32_t instanceCount;
};
uint32_t LCDylib::instanceCount = 0;
class LCLoadDylinker : public LoadCommand {
public:
uint32_t getSize() const override {
@ -366,6 +375,12 @@ void Writer::createLoadCommands() {
make<LCDylib>(LC_REEXPORT_DYLIB, dylibFile->dylibName));
}
}
const uint32_t MACOS_MAXPATHLEN = 1024;
config->headerPad = std::max(
config->headerPad, (config->headerPadMaxInstallNames
? LCDylib::getInstanceCount() * MACOS_MAXPATHLEN
: 0));
}
static size_t getSymbolPriority(const SymbolPriorityEntry &entry,

View File

@ -8,31 +8,84 @@
## just enforces a lower bound. We should consider implementing the same
## alignment behavior.
################ Check default behavior
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o
# RUN: lld -flavor darwinnew -o %t %t.o
# RUN: llvm-objdump --macho --all-headers %t | FileCheck %s --check-prefix=PADx
#
# PADx: magic {{.+}} ncmds sizeofcmds flags
# PADx-NEXT: MH_MAGIC_64 {{.+}} 9 [[#%u, CMDSIZE:]] {{.*}}
# PADx: sectname __text
# PADx-NEXT: segname __TEXT
# PADx-NEXT: addr
# PADx-NEXT: size
# PADx-NEXT: offset [[#%u, CMDSIZE + 0x20 + 0x20]]
################ Zero pad, no LCDylibs
# RUN: lld -flavor darwinnew -o %t %t.o -headerpad 0
# RUN: llvm-objdump --macho --all-headers %t | FileCheck %s --check-prefix=PAD0
# PAD0: magic cputype cpusubtype caps filetype ncmds sizeofcmds flags
# PAD0-NEXT: MH_MAGIC_64 X86_64 ALL LIB64 EXECUTE 9 [[#%u, CMDSIZE:]] {{.*}}
# RUN: lld -flavor darwinnew -o %t %t.o -headerpad 0 -headerpad_max_install_names
# RUN: llvm-objdump --macho --all-headers %t | FileCheck %s --check-prefix=PAD0
#
# PAD0: magic {{.+}} ncmds sizeofcmds flags
# PAD0-NEXT: MH_MAGIC_64 {{.+}} 9 [[#%u, CMDSIZE:]] {{.*}}
# PAD0: sectname __text
# PAD0-NEXT: segname __TEXT
# PAD0-NEXT: addr
# PAD0-NEXT: size
# PAD0-NEXT: offset [[#%u, CMDSIZE + 32]]
# PAD0-NEXT: offset [[#%u, CMDSIZE + 0x20 + 0]]
################ Each lexical form of a hex number, no LCDylibs
# RUN: lld -flavor darwinnew -o %t %t.o -headerpad 11
# RUN: llvm-objdump --macho --all-headers %t | FileCheck %s --check-prefix=PAD11
# RUN: lld -flavor darwinnew -o %t %t.o -headerpad 0x11
# RUN: llvm-objdump --macho --all-headers %t | FileCheck %s --check-prefix=PAD11
# RUN: lld -flavor darwinnew -o %t %t.o -headerpad 0X11
# RUN: lld -flavor darwinnew -o %t %t.o -headerpad 0X11 -headerpad_max_install_names
# RUN: llvm-objdump --macho --all-headers %t | FileCheck %s --check-prefix=PAD11
#
# PAD11: magic {{.+}} ncmds sizeofcmds flags
# PAD11-NEXT: MH_MAGIC_64 {{.+}} 9 [[#%u, CMDSIZE:]] {{.*}}
# PAD11: sectname __text
# PAD11-NEXT: segname __TEXT
# PAD11-NEXT: addr
# PAD11-NEXT: size
# PAD11-NEXT: offset [[#%u, CMDSIZE + 32 + 0x11]]
# PAD11-NEXT: offset [[#%u, CMDSIZE + 0x20 + 0x11]]
################ Each & all 3 kinds of LCDylib
# RUN: echo "" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %T/null.o
# RUN: lld -flavor darwinnew -o %T/libnull.dylib %T/null.o -dylib \
# RUN: -headerpad_max_install_names
# RUN: llvm-objdump --macho --all-headers %T/libnull.dylib | FileCheck %s --check-prefix=PADMAX
# RUN: lld -flavor darwinnew -o %T/libnull.dylib %T/null.o -dylib \
# RUN: -headerpad_max_install_names \
# RUN: -syslibroot %S/Inputs/MacOSX.sdk -lSystem
# RUN: llvm-objdump --macho --all-headers %T/libnull.dylib | FileCheck %s --check-prefix=PADMAX
# RUN: lld -flavor darwinnew -o %T/libnull.dylib %T/null.o -dylib \
# RUN: -headerpad_max_install_names \
# RUN: -syslibroot %S/Inputs/MacOSX.sdk -lSystem -sub_library libSystem
# RUN: llvm-objdump --macho --all-headers %T/libnull.dylib | FileCheck %s --check-prefix=PADMAX
#
# PADMAX: magic {{.+}} ncmds sizeofcmds flags
# PADMAX-NEXT: MH_MAGIC_64 {{.+}} [[#%u, N:]] [[#%u, CMDSIZE:]] {{.*}}
# PADMAX: sectname __text
# PADMAX-NEXT: segname __TEXT
# PADMAX-NEXT: addr
# PADMAX-NEXT: size
# PADMAX-NEXT: offset [[#%u, CMDSIZE + 0x20 + mul(0x400, N - 6)]]
################ All 3 kinds of LCDylib swamped by a larger override
# RUN: lld -flavor darwinnew -o %T/libnull.dylib %T/null.o -dylib \
# RUN: -headerpad_max_install_names -headerpad 0x1001 \
# RUN: -syslibroot %S/Inputs/MacOSX.sdk -lSystem -sub_library libSystem
# RUN: llvm-objdump --macho --all-headers %T/libnull.dylib | FileCheck %s --check-prefix=PADOVR
#
# PADOVR: magic {{.+}} ncmds sizeofcmds flags
# PADOVR-NEXT: MH_MAGIC_64 {{.+}} [[#%u, N:]] [[#%u, CMDSIZE:]] {{.*}}
# PADOVR: sectname __text
# PADOVR-NEXT: segname __TEXT
# PADOVR-NEXT: addr
# PADOVR-NEXT: size
# PADOVR-NEXT: offset [[#%u, CMDSIZE + 0x20 + 0x1001]]
.globl _main
_main: