Index: lld/ELF/Driver.cpp =================================================================== --- lld/ELF/Driver.cpp +++ lld/ELF/Driver.cpp @@ -710,11 +710,11 @@ return false; } -static DiscardPolicy getDiscard(opt::InputArgList &args) { +static DiscardPolicy getDiscard(opt::InputArgList &args, uint16_t emachine) { auto *arg = args.getLastArg(OPT_discard_all, OPT_discard_locals, OPT_discard_none); if (!arg) - return DiscardPolicy::Default; + return emachine == EM_RISCV ? DiscardPolicy::Locals : DiscardPolicy::Default; if (arg->getOption().getID() == OPT_discard_all) return DiscardPolicy::All; if (arg->getOption().getID() == OPT_discard_locals) @@ -1098,7 +1098,7 @@ config->dependencyFile = args.getLastArgValue(OPT_dependency_file); config->dependentLibraries = args.hasFlag(OPT_dependent_libraries, OPT_no_dependent_libraries, true); config->disableVerify = args.hasArg(OPT_disable_verify); - config->discard = getDiscard(args); + config->discard = getDiscard(args, config->emachine); config->dwoDir = args.getLastArgValue(OPT_plugin_opt_dwo_dir_eq); config->dynamicLinker = getDynamicLinker(args); config->ehFrameHdr = Index: lld/test/ELF/discard-locals-riscv.s =================================================================== --- /dev/null +++ lld/test/ELF/discard-locals-riscv.s @@ -0,0 +1,12 @@ +# REQUIRES: riscv + +# RUN: llvm-mc -filetype=obj -triple=riscv64 %s -o %t.o +# RUN: ld.lld %t.o -o - | llvm-readelf -s - | FileCheck %s + +# RUN: llvm-mc -filetype=obj -triple=riscv64 %s -o %t.o -save-temp-labels +# RUN: ld.lld %t.o -o - -discard-none | llvm-readelf -s - | FileCheck %s --check-prefix=NONE + +# CHECK-NOT: .Llabel +# NONE: .Llabel + +.Llabel: