diff --git a/llvm/test/tools/llvm-objcopy/MachO/bitcode-strip.test b/llvm/test/tools/llvm-objcopy/MachO/bitcode-strip-basic.test rename from llvm/test/tools/llvm-objcopy/MachO/bitcode-strip.test rename to llvm/test/tools/llvm-objcopy/MachO/bitcode-strip-basic.test --- a/llvm/test/tools/llvm-objcopy/MachO/bitcode-strip.test +++ b/llvm/test/tools/llvm-objcopy/MachO/bitcode-strip-basic.test @@ -1,10 +1,11 @@ -## Test output flag is required. +## Test output flag and action flag are required. # RUN: yaml2obj %s -o %t -# RUN: not llvm-bitcode-strip %t 2>&1 | FileCheck --check-prefix=MISSING-ARG %s -# RUN: llvm-bitcode-strip %t -o %t2 -# RUN: cmp %t %t2 +# RUN: not llvm-bitcode-strip %t 2>&1 | FileCheck --check-prefix=MISSING-OUTPUT %s +# RUN: not llvm-bitcode-strip %t -o %t2 2>&1 | FileCheck --check-prefix=MISSING-ACTION %s +# RUN: llvm-bitcode-strip -r %t -o %t2 -# MISSING-ARG: error: -o is a required argument +# MISSING-OUTPUT: error: -o is a required argument +# MISSING-ACTION: error: no action specified --- !mach-o FileHeader: diff --git a/llvm/test/tools/llvm-objcopy/MachO/bitcode-strip-remove.test b/llvm/test/tools/llvm-objcopy/MachO/bitcode-strip-remove.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-objcopy/MachO/bitcode-strip-remove.test @@ -0,0 +1,87 @@ +## Test bitcode section removal. +# RUN: yaml2obj %s -o %t +# RUN: llvm-bitcode-strip -r %t -o %t2 +# RUN: llvm-readobj --sections %t2 | FileCheck --implicit-check-not=Name: %s + +# CHECK: Name: __text +# CHECK-NEXT: Segment: __TEXT +# CHECK: Name: __bundle +# CHECK-NEXT: Segment: __DATA +# CHECK: Name: __notbundle +# CHECK-NEXT: Segment: __LLVM + +--- !mach-o +FileHeader: + magic: 0xFEEDFACF + cputype: 0x01000007 + cpusubtype: 0x00000003 + filetype: 0x00000001 + ncmds: 1 + sizeofcmds: 392 + flags: 0x00002000 + reserved: 0x00000000 +LoadCommands: + - cmd: LC_SEGMENT_64 + cmdsize: 392 + segname: '' + vmaddr: 0 + vmsize: 16 + fileoff: 424 + filesize: 16 + maxprot: 7 + initprot: 7 + nsects: 4 + flags: 0 + Sections: + - sectname: __text + segname: __TEXT + addr: 0x0000000000000000 + content: 'AABBCCDD' + size: 4 + offset: 424 + align: 0 + reloff: 0x00000000 + nreloc: 0 + flags: 0x80000400 + reserved1: 0x00000000 + reserved2: 0x00000000 + reserved3: 0x00000000 + - sectname: __bundle + segname: __DATA + addr: 0x0000000000000004 + content: 'DDAADDAA' + size: 4 + offset: 428 + align: 0 + reloff: 0x00000000 + nreloc: 0 + flags: 0x00000000 + reserved1: 0x00000000 + reserved2: 0x00000000 + reserved3: 0x00000000 + - sectname: __bundle + segname: __LLVM + addr: 0x0000000000000008 + content: 'EEFFEEFF' + size: 4 + offset: 432 + align: 0 + reloff: 0x00000000 + nreloc: 0 + flags: 0x00000000 + reserved1: 0x00000000 + reserved2: 0x00000000 + reserved3: 0x00000000 + - sectname: __notbundle + segname: __LLVM + addr: 0x0000000000000008 + content: 'EEFFEEFF' + size: 4 + offset: 436 + align: 0 + reloff: 0x00000000 + nreloc: 0 + flags: 0x00000000 + reserved1: 0x00000000 + reserved2: 0x00000000 + reserved3: 0x00000000 diff --git a/llvm/tools/llvm-objcopy/BitcodeStripOpts.td b/llvm/tools/llvm-objcopy/BitcodeStripOpts.td --- a/llvm/tools/llvm-objcopy/BitcodeStripOpts.td +++ b/llvm/tools/llvm-objcopy/BitcodeStripOpts.td @@ -23,5 +23,8 @@ Alias, HelpText<"Alias for --version">; +def remove : Flag<["-"], "r">, + HelpText<"Remove the __LLVM bitcode segment entirely">; + def output : JoinedOrSeparate<["-"], "o">, HelpText<"Write output to ">, MetaVarName<"">; diff --git a/llvm/tools/llvm-objcopy/ObjcopyOptions.h b/llvm/tools/llvm-objcopy/ObjcopyOptions.h --- a/llvm/tools/llvm-objcopy/ObjcopyOptions.h +++ b/llvm/tools/llvm-objcopy/ObjcopyOptions.h @@ -41,7 +41,9 @@ // ParseBitcodeStripOptions returns the config and sets the input arguments. // If a help flag is set then ParseBitcodeStripOptions will print the help // messege and exit. -Expected parseBitcodeStripOptions(ArrayRef ArgsArr); +Expected +parseBitcodeStripOptions(ArrayRef ArgsArr, + llvm::function_ref ErrorCallback); // ParseStripOptions returns the config and sets the input arguments. If a // help flag is set then ParseStripOptions will print the help messege and diff --git a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp --- a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp +++ b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp @@ -12,6 +12,7 @@ #include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringSet.h" #include "llvm/BinaryFormat/COFF.h" +#include "llvm/ObjCopy/CommonConfig.h" #include "llvm/ObjCopy/ConfigManager.h" #include "llvm/Option/Arg.h" #include "llvm/Option/ArgList.h" @@ -1183,7 +1184,8 @@ } Expected -objcopy::parseBitcodeStripOptions(ArrayRef ArgsArr) { +objcopy::parseBitcodeStripOptions(ArrayRef ArgsArr, + function_ref ErrorCallback) { DriverConfig DC; ConfigManager ConfigMgr; CommonConfig &Config = ConfigMgr.Common; @@ -1228,6 +1230,13 @@ } Config.OutputFilename = InputArgs.getLastArgValue(BITCODE_STRIP_output); + if (!InputArgs.hasArg(BITCODE_STRIP_remove)) + return createStringError(errc::invalid_argument, "no action specified"); + + // We only support -r for now, which removes all bitcode sections. + cantFail(Config.ToRemove.addMatcher(NameOrPattern::create( + "__LLVM,__bundle", MatchStyle::Literal, ErrorCallback))); + DC.CopyConfigs.push_back(std::move(ConfigMgr)); return std::move(DC); } diff --git a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp --- a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp +++ b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp @@ -86,7 +86,7 @@ }; if (Is("bitcode-strip") || Is("bitcode_strip")) - return parseBitcodeStripOptions(Args); + return parseBitcodeStripOptions(Args, reportWarning); else if (Is("strip")) return parseStripOptions(Args, reportWarning); else if (Is("install-name-tool") || Is("install_name_tool"))