diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -160,7 +160,6 @@ bool compressDebugSections; bool cref; std::vector> deadRelocInNonAlloc; - bool defineCommon; bool demangle = true; bool dependentLibraries; bool disableVerify; @@ -172,6 +171,7 @@ bool exportDynamic; bool fixCortexA53Errata843419; bool fixCortexA8; + bool forceCommonDefinition; bool formatBinary = false; bool fortranCommon; bool gcSections; diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -327,9 +327,6 @@ if (!config->shared && !config->auxiliaryList.empty()) error("-f may not be used without -shared"); - if (!config->relocatable && !config->defineCommon) - error("-no-define-common not supported in non relocatable output"); - if (config->strip == StripPolicy::All && config->emitRelocs) error("--strip-all and --emit-relocs may not be used together"); @@ -989,8 +986,6 @@ config->chroot = args.getLastArgValue(OPT_chroot); config->compressDebugSections = getCompressDebugSections(args); config->cref = args.hasArg(OPT_cref); - config->defineCommon = args.hasFlag(OPT_define_common, OPT_no_define_common, - !args.hasArg(OPT_relocatable)); config->optimizeBBJumps = args.hasFlag(OPT_optimize_bb_jumps, OPT_no_optimize_bb_jumps, false); config->demangle = args.hasFlag(OPT_demangle, OPT_no_demangle, true); @@ -1023,6 +1018,8 @@ !args.hasArg(OPT_relocatable); config->fixCortexA8 = args.hasArg(OPT_fix_cortex_a8) && !args.hasArg(OPT_relocatable); + config->forceCommonDefinition = + !args.hasArg(OPT_relocatable) || args.hasArg(OPT_dc); config->fortranCommon = args.hasFlag(OPT_fortran_common, OPT_no_fortran_common, true); config->gcSections = args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, false); diff --git a/lld/ELF/Options.td b/lld/ELF/Options.td --- a/lld/ELF/Options.td +++ b/lld/ELF/Options.td @@ -129,10 +129,6 @@ def cref: FF<"cref">, HelpText<"Output cross reference table. If -Map is specified, print to the map file">; -defm define_common: B<"define-common", - "Assign space to common symbols", - "Do not assign space to common symbols">; - defm demangle: B<"demangle", "Demangle symbol names (default)", "Do not demangle symbol names">; @@ -512,9 +508,9 @@ def: F<"dn">, Alias, HelpText<"Alias for --Bstatic">; def: F<"non_shared">, Alias, HelpText<"Alias for --Bstatic">; def: F<"static">, Alias, HelpText<"Alias for --Bstatic">; -def: Flag<["-"], "d">, Alias, HelpText<"Alias for --define-common">; -def: F<"dc">, Alias, HelpText<"Alias for --define-common">; -def: F<"dp">, Alias, HelpText<"Alias for --define-common">; +def dc: Flag<["-"], "dc">, HelpText<"(-r specific) Assign space to COMMON symbols like building an executable or shared object">; +def: F<"d">, Alias, HelpText<"Alias for -dc">; +def: F<"dp">, Alias, HelpText<"Alias for -dc">; def: Flag<["-"], "x">, Alias, HelpText<"Alias for --discard-all">; def: Flag<["-"], "X">, Alias, HelpText<"Alias for --discard-locals">; def: Flag<["-"], "q">, Alias, HelpText<"Alias for --emit-relocs">; diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -2193,7 +2193,7 @@ } static BssSection *getCommonSec(Symbol *sym) { - if (!config->defineCommon) + if (!config->forceCommonDefinition) if (auto *d = dyn_cast(sym)) return dyn_cast_or_null(d->section); return nullptr; @@ -2235,8 +2235,8 @@ eSym->st_other |= sym->stOther & STO_AARCH64_VARIANT_PCS; if (BssSection *commonSec = getCommonSec(sym)) { - // st_value is usually an address of a symbol, but that has a special - // meaning for uninstantiated common symbols (--no-define-common). + // When -r is specified and -dc is not specified, a COMMON symbol's + // st_shndx holds SHN_COMMON and st_value holds the alignment. eSym->st_shndx = SHN_COMMON; eSym->st_value = commonSec->alignment; eSym->st_size = cast(sym)->size; diff --git a/lld/docs/ld.lld.1 b/lld/docs/ld.lld.1 --- a/lld/docs/ld.lld.1 +++ b/lld/docs/ld.lld.1 @@ -144,8 +144,9 @@ Output cross reference table. If .Fl Map is specified, print to the map file. -.It Fl -define-common , Fl d -Assign space to common symbols. +.It Fl dc +.Fl r +specific: assign space to COMMON symbols like building an executable or shared object. .It Fl -defsym Ns = Ns Ar symbol Ns = Ns Ar expression Define a symbol alias. .Ar expression @@ -326,8 +327,6 @@ for shared libraries. .It Fl -no-color-diagnostics Do not use colors in diagnostics. -.It Fl -no-define-common -Do not assign space to common symbols. .It Fl -no-demangle Do not demangle symbol names. .It Fl -no-dynamic-linker diff --git a/lld/test/ELF/common.s b/lld/test/ELF/common.s --- a/lld/test/ELF/common.s +++ b/lld/test/ELF/common.s @@ -4,6 +4,10 @@ // RUN: ld.lld %t %t2 -o %t3 // RUN: llvm-readobj --symbols -S %t3 | FileCheck %s +/// -dc is ignored for executable/shared links. +// RUN: ld.lld -dc %t %t2 -o %t3 +// RUN: llvm-readobj --symbols -S %t3 | FileCheck %s + // CHECK: Name: .bss // CHECK-NEXT: Type: SHT_NOBITS // CHECK-NEXT: Flags [ diff --git a/lld/test/ELF/relocatable-common.s b/lld/test/ELF/relocatable-common.s --- a/lld/test/ELF/relocatable-common.s +++ b/lld/test/ELF/relocatable-common.s @@ -2,10 +2,6 @@ # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o # RUN: ld.lld -r %t1.o -o %t # RUN: llvm-readobj --symbols -r %t | FileCheck %s -# RUN: ld.lld -r --no-define-common %t1.o -o %t -# RUN: llvm-readobj --symbols -r %t | FileCheck %s -# RUN: ld.lld -r --define-common %t1.o -o %t -# RUN: llvm-readobj --symbols -r %t | FileCheck -check-prefix=DEFCOMM %s # RUN: ld.lld -r -d %t1.o -o %t # RUN: llvm-readobj --symbols -r %t | FileCheck -check-prefix=DEFCOMM %s # RUN: ld.lld -r -dc %t1.o -o %t @@ -33,7 +29,4 @@ # DEFCOMM-NEXT: Section: COMMON # DEFCOMM-NEXT: } -# RUN: not ld.lld -shared --no-define-common %t1.o -o /dev/null 2>&1 | FileCheck --check-prefix=ERROR %s -# ERROR: error: -no-define-common not supported in non relocatable output - .comm common,4,4