Skip to content

Commit

Permalink
[LLD] [MinGW] Add more options for disabling flags in the executable
Browse files Browse the repository at this point in the history
In e72403f, we added the flag
"--no-dynamicbase" for disabling the dynamicbase flag which we set
by default. At the time, ld.bfd didn't have any corresponding
option (as ld.bfd defaulted to not setting the flag). Almost at
the same time, corresponding options were added to ld.bfd for
disabling it (while it was being enabled by default), with a
different name, "--disable-dynamicbase".

Thus add the "--disable-dynamicbase" option. Make this default
one advertised in the help listing, but keep the "--no-dynamicbase"
form as an alias. Also improve checking for the last option set
if there are multiple ones on the same command line.

Also add corresponding disable options for a lot of other flags
that we set by default, also added in ld.bfd in the same commit:
https://sourceware.org/git/?p=binutils-gdb.git;a=commitdiff;h=514b4e191d5f46de8e142fe216e677a35fa9c4bb

Differential Revision: https://reviews.llvm.org/D107930

(cherry picked from commit f8340c8)
  • Loading branch information
mstorsjo authored and tstellar committed Aug 17, 2021
1 parent b9be17a commit 87d56ad
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
10 changes: 8 additions & 2 deletions lld/MinGW/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,13 +308,19 @@ bool mingw::link(ArrayRef<const char *> argsArr, bool canExitEarly,
add("-kill-at");
if (args.hasArg(OPT_appcontainer))
add("-appcontainer");
if (args.hasArg(OPT_no_seh))
if (args.hasFlag(OPT_no_seh, OPT_disable_no_seh, false))
add("-noseh");

if (args.getLastArgValue(OPT_m) != "thumb2pe" &&
args.getLastArgValue(OPT_m) != "arm64pe" &&
args.hasArg(OPT_no_dynamicbase))
args.hasFlag(OPT_disable_dynamicbase, OPT_dynamicbase, false))
add("-dynamicbase:no");
if (args.hasFlag(OPT_disable_high_entropy_va, OPT_high_entropy_va, false))
add("-highentropyva:no");
if (args.hasFlag(OPT_disable_nxcompat, OPT_nxcompat, false))
add("-nxcompat:no");
if (args.hasFlag(OPT_disable_tsaware, OPT_tsaware, false))
add("-tsaware:no");

if (args.hasFlag(OPT_no_insert_timestamp, OPT_insert_timestamp, false))
add("-timestamp:0");
Expand Down
20 changes: 15 additions & 5 deletions lld/MinGW/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ multiclass B<string name, string help1, string help2> {
def no_ # NAME: Flag<["--", "-"], "no-" # name>, HelpText<help2>;
}

multiclass B_disable<string name, string help1, string help2> {
def NAME: Flag<["--", "-"], name>, HelpText<help1>;
def disable_ # NAME: Flag<["--", "-"], "disable-" # name>, HelpText<help2>;
}

def L: JoinedOrSeparate<["-"], "L">, MetaVarName<"<dir>">,
HelpText<"Add a directory to the library search path">;
defm allow_multiple_definition: B<"allow-multiple-definition",
Expand All @@ -42,7 +47,7 @@ def disable_runtime_pseudo_reloc: F<"disable-runtime-pseudo-reloc">,
HelpText<"Don't do automatic imports that require runtime fixups">;
def disable_stdcall_fixup: F<"disable-stdcall-fixup">,
HelpText<"Don't resolve stdcall/fastcall/vectorcall to undecorated symbols">;
defm dynamicbase: B<"dynamicbase", "Enable ASLR", "Disable ASLR">;
defm dynamicbase: B_disable<"dynamicbase", "Enable ASLR", "Disable ASLR">;
def enable_auto_import: F<"enable-auto-import">,
HelpText<"Automatically import data symbols from other DLLs where needed">;
def enable_runtime_pseudo_reloc: F<"enable-runtime-pseudo-reloc">,
Expand All @@ -62,6 +67,8 @@ defm gc_sections: B<"gc-sections",
"Remove unused sections",
"Don't remove unused sections">;
def help: F<"help">, HelpText<"Print option help">;
defm high_entropy_va: B_disable<"high-entropy-va",
"Set the 'high entropy VA' flag", "Don't set the 'high entropy VA' flag">;
defm icf: Eq<"icf", "Identical code folding">;
defm image_base: Eq<"image-base", "Base address of the program">;
defm insert_timestamp: B<"insert-timestamp",
Expand All @@ -80,7 +87,10 @@ defm minor_os_version: EqLong<"minor-os-version",
"Set the OS and subsystem minor version">;
defm minor_subsystem_version: EqLong<"minor-subsystem-version",
"Set the OS and subsystem minor version">;
def no_seh: F<"no-seh">, HelpText<"Set the 'no SEH' flag in the executable">;
defm no_seh: B_disable<"no-seh",
"Set the 'no SEH' flag in the executable", "Don't set the 'no SEH' flag">;
defm nxcompat: B_disable<"nxcompat",
"Set the 'nxcompat' flag in the executable", "Don't set the 'nxcompat' flag">;
def large_address_aware: Flag<["--"], "large-address-aware">,
HelpText<"Enable large addresses">;
def o: JoinedOrSeparate<["-"], "o">, MetaVarName<"<path>">,
Expand All @@ -99,6 +109,8 @@ defm reproduce: Eq<"reproduce",
"Write a tar file containing input files and command line options to reproduce link">;
defm require_defined: Eq<"require-defined",
"Force symbol to be added to symbol table as an undefined one">;
defm tsaware: B_disable<"tsaware",
"Set the 'Terminal Server aware' flag", "Don't set the 'Terminal Server aware' flag">;
defm undefined: Eq<"undefined", "Include symbol in the link, if available">;
defm whole_archive: B<"whole-archive",
"Include all object files for following archives",
Expand Down Expand Up @@ -127,6 +139,7 @@ def alias_Bstatic_dn: Flag<["-"], "dn">, Alias<Bstatic>;
def alias_Bstatic_non_shared: Flag<["-"], "non_shared">, Alias<Bstatic>;
def alias_Bstatic_static: Flag<["-"], "static">, Alias<Bstatic>;
def alias_entry_e: JoinedOrSeparate<["-"], "e">, Alias<entry>;
def alias_no_dynamicbase: F<"no-dynamicbase">, Alias<disable_dynamicbase>;
def alias_strip_s: Flag<["-"], "s">, Alias<strip_all>;
def alias_strip_S: Flag<["-"], "S">, Alias<strip_debug>;
def alias_undefined_u: JoinedOrSeparate<["-"], "u">, Alias<undefined>;
Expand All @@ -138,14 +151,11 @@ def: F<"disable-auto-image-base">;
def: F<"enable-auto-image-base">;
def: F<"end-group">;
def: Flag<["--"], "full-shutdown">;
def: F<"high-entropy-va">;
defm: EqNoHelp<"major-image-version">;
defm: EqNoHelp<"minor-image-version">;
def: F<"no-undefined">;
def: F<"nxcompat">;
def: F<"pic-executable">;
defm: EqNoHelp<"plugin">;
defm: EqNoHelp<"plugin-opt">;
defm: EqNoHelp<"sysroot">;
def: F<"start-group">;
def: F<"tsaware">;
33 changes: 29 additions & 4 deletions lld/test/MinGW/driver.test
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,10 @@ PDB-DEFAULT-NOT: -pdb:{{.*}}
RUN: ld.lld -### -m i386pep foo.o --large-address-aware 2>&1 | FileCheck -check-prefix LARGE-ADDRESS-AWARE %s
LARGE-ADDRESS-AWARE: -largeaddressaware

RUN: ld.lld -### -m i386pe foo.o --no-dynamicbase 2>&1 | FileCheck -check-prefix DISABLE-DYNAMICBASE %s
RUN: ld.lld -### -m i386pe foo.o --dynamicbase --no-dynamicbase 2>&1 | FileCheck -check-prefix DISABLE-DYNAMICBASE %s
RUN: ld.lld -### -m i386pe foo.o --dynamicbase --disable-dynamicbase 2>&1 | FileCheck -check-prefix DISABLE-DYNAMICBASE %s
DISABLE-DYNAMICBASE: -dynamicbase:no
RUN: ld.lld -### -m i386pe --dynamicbase foo.o 2>&1 | FileCheck -check-prefix NO-DISABLE-DYNAMICBASE %s
RUN: ld.lld -### -m i386pe --disable-dynamicbase --dynamicbase foo.o 2>&1 | FileCheck -check-prefix NO-DISABLE-DYNAMICBASE %s
RUN: ld.lld -### -m i386pep -dynamicbase foo.o 2>&1 | FileCheck -check-prefix NO-DISABLE-DYNAMICBASE %s
RUN: ld.lld -### -m i386pe foo.o 2>&1 | FileCheck -check-prefix NO-DISABLE-DYNAMICBASE %s
RUN: ld.lld -### -m i386pep foo.o 2>&1 | FileCheck -check-prefix NO-DISABLE-DYNAMICBASE %s
Expand All @@ -159,6 +160,27 @@ RUN: ld.lld -### -m thumb2pe foo.o --no-dynamicbase 2>&1 | FileCheck -check-pref
RUN: ld.lld -### -m arm64pe foo.o --no-dynamicbase 2>&1 | FileCheck -check-prefix NO-DISABLE-DYNAMICBASE %s
NO-DISABLE-DYNAMICBASE-NOT: -dynamicbase:no

RUN: ld.lld -### -m i386pe foo.o -high-entropy-va -disable-high-entropy-va 2>&1 | FileCheck -check-prefix DISABLE-HIGH-ENTROPY-VA %s
RUN: ld.lld -### -m i386pe foo.o --high-entropy-va --disable-high-entropy-va 2>&1 | FileCheck -check-prefix DISABLE-HIGH-ENTROPY-VA %s
DISABLE-HIGH-ENTROPY-VA: -highentropyva:no
RUN: ld.lld -### -m i386pe foo.o -disable-high-entropy-va -high-entropy-va 2>&1 | FileCheck -check-prefix NO-DISABLE-HIGH-ENTROPY-VA %s
RUN: ld.lld -### -m i386pe foo.o --disable-high-entropy-va --high-entropy-va 2>&1 | FileCheck -check-prefix NO-DISABLE-HIGH-ENTROPY-VA %s
NO-DISABLE-HIGH-ENTROPY-VA-NOT: -highentropyva:no

RUN: ld.lld -### -m i386pe foo.o -nxcompat -disable-nxcompat 2>&1 | FileCheck -check-prefix DISABLE-NXCOMPAT %s
RUN: ld.lld -### -m i386pe foo.o --nxcompat --disable-nxcompat 2>&1 | FileCheck -check-prefix DISABLE-NXCOMPAT %s
DISABLE-NXCOMPAT: -nxcompat:no
RUN: ld.lld -### -m i386pe foo.o -disable-nxcompat -nxcompat 2>&1 | FileCheck -check-prefix NO-DISABLE-NXCOMPAT %s
RUN: ld.lld -### -m i386pe foo.o --disable-nxcompat --nxcompat 2>&1 | FileCheck -check-prefix NO-DISABLE-NXCOMPAT %s
NO-DISABLE-NXCOMPAT-NOT: -nxcompat:no

RUN: ld.lld -### -m i386pe foo.o -tsaware -disable-tsaware 2>&1 | FileCheck -check-prefix DISABLE-TSAWARE %s
RUN: ld.lld -### -m i386pe foo.o --tsaware --disable-tsaware 2>&1 | FileCheck -check-prefix DISABLE-TSAWARE %s
DISABLE-TSAWARE: -tsaware:no
RUN: ld.lld -### -m i386pe foo.o -disable-tsaware -tsaware 2>&1 | FileCheck -check-prefix NO-DISABLE-TSAWARE %s
RUN: ld.lld -### -m i386pe foo.o --disable-tsaware --tsaware 2>&1 | FileCheck -check-prefix NO-DISABLE-TSAWARE %s
NO-DISABLE-TSAWARE-NOT: -tsaware:no

RUN: ld.lld -### -m i386pep foo.o --image-base 0x1230000 2>&1 | FileCheck -check-prefix IMAGE-BASE %s
RUN: ld.lld -### -m i386pep foo.o -image-base 0x1230000 2>&1 | FileCheck -check-prefix IMAGE-BASE %s
RUN: ld.lld -### -m i386pep foo.o --image-base=0x1230000 2>&1 | FileCheck -check-prefix IMAGE-BASE %s
Expand Down Expand Up @@ -273,9 +295,12 @@ RUN: ld.lld -### -m i386pep foo.o --section-alignment=0x2000 2>&1 | FileCheck -c
RUN: ld.lld -### -m i386pep foo.o -section-alignment=0x2000 2>&1 | FileCheck -check-prefix ALIGN %s
ALIGN: -align:0x2000

RUN: ld.lld -### -m i386pe foo.o -no-seh 2>&1 | FileCheck -check-prefix NOSEH %s
RUN: ld.lld -### -m i386pe foo.o --no-seh 2>&1 | FileCheck -check-prefix NOSEH %s
RUN: ld.lld -### -m i386pe foo.o -disable-no-seh -no-seh 2>&1 | FileCheck -check-prefix NOSEH %s
RUN: ld.lld -### -m i386pe foo.o --disable-no-seh --no-seh 2>&1 | FileCheck -check-prefix NOSEH %s
RUN: ld.lld -### -m i386pe foo.o -no-seh -disable-no-seh 2>&1 | FileCheck -check-prefix DISABLE-NOSEH %s
RUN: ld.lld -### -m i386pe foo.o --no-seh --disable-no-seh 2>&1 | FileCheck -check-prefix DISABLE-NOSEH %s
NOSEH: -noseh
DISABLE-NOSEH-NOT: -noseh

RUN: ld.lld -### -m i386pep foo.o --no-allow-multiple-definition --allow-multiple-definition 2>&1 | FileCheck -check-prefix ALLOW_MULTIPLE_DEFINITION %s
RUN: ld.lld -### -m i386pep foo.o -no-allow-multiple-definition -allow-multiple-definition 2>&1 | FileCheck -check-prefix ALLOW_MULTIPLE_DEFINITION %s
Expand Down

0 comments on commit 87d56ad

Please sign in to comment.