Index: tools/llvm-objcopy/llvm-objcopy.cpp =================================================================== --- tools/llvm-objcopy/llvm-objcopy.cpp +++ tools/llvm-objcopy/llvm-objcopy.cpp @@ -83,6 +83,8 @@ cl::aliasopt(ToRemove)); static cl::opt StripSections("strip-sections", cl::desc("Remove all section headers")); +static cl::opt OnlyKeepAlloc("only-keep-alloc", + cl::desc("Remove all non-allocated sections")); static cl::opt StripDWO("strip-dwo", cl::desc("remove all DWARF .dwo sections from file")); static cl::opt ExtractDWO( @@ -177,6 +179,15 @@ Obj->WriteSectionHeaders = false; } + if (OnlyKeepAlloc) + RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { + if (RemovePred(Sec)) + return true; + if (&Sec == Obj->getSectionHeaderStrTab()) + return false; + return (Sec.Flags & SHF_ALLOC) == 0; + }; + Obj->removeSections(RemovePred); Obj->finalize(); WriteObjectFile(*Obj, OutputFilename.getValue());