Index: llvm/trunk/test/tools/llvm-objcopy/COFF/Inputs/discard-locals.yaml =================================================================== --- llvm/trunk/test/tools/llvm-objcopy/COFF/Inputs/discard-locals.yaml +++ llvm/trunk/test/tools/llvm-objcopy/COFF/Inputs/discard-locals.yaml @@ -6,11 +6,14 @@ - Name: .text Characteristics: [ ] Alignment: 4 - SectionData: E800000000C3C3C3 + SectionData: E800000000E800000000C3C3C3 Relocations: - VirtualAddress: 1 SymbolName: local_referenced Type: IMAGE_REL_AMD64_REL32 + - VirtualAddress: 5 + SymbolName: external_undefined + Type: IMAGE_REL_AMD64_REL32 symbols: - Name: external Value: 0 @@ -24,6 +27,12 @@ SimpleType: IMAGE_SYM_TYPE_NULL ComplexType: IMAGE_SYM_DTYPE_NULL StorageClass: IMAGE_SYM_CLASS_EXTERNAL + - Name: external_undefined_unreferenced + Value: 0 + SectionNumber: 0 + SimpleType: IMAGE_SYM_TYPE_NULL + ComplexType: IMAGE_SYM_DTYPE_NULL + StorageClass: IMAGE_SYM_CLASS_EXTERNAL - Name: local_unreferenced Value: 6 SectionNumber: 1 Index: llvm/trunk/test/tools/llvm-objcopy/COFF/discard-all.test =================================================================== --- llvm/trunk/test/tools/llvm-objcopy/COFF/discard-all.test +++ llvm/trunk/test/tools/llvm-objcopy/COFF/discard-all.test @@ -19,6 +19,7 @@ SYMBOLS: SYMBOL TABLE: SYMBOLS-NEXT: external SYMBOLS-NEXT: external_undefined +SYMBOLS-NEXT: external_undefined_unreferenced SYMBOLS-PRE-NEXT: local_unreferenced SYMBOLS-NEXT: local_referenced SYMBOLS-NEXT: local_undefined_unreferenced Index: llvm/trunk/test/tools/llvm-objcopy/COFF/strip-unneeded.test =================================================================== --- llvm/trunk/test/tools/llvm-objcopy/COFF/strip-unneeded.test +++ llvm/trunk/test/tools/llvm-objcopy/COFF/strip-unneeded.test @@ -8,6 +8,7 @@ SYMBOLS: SYMBOL TABLE: SYMBOLS-NEXT: external SYMBOLS-NEXT: external_undefined +SYMBOLS-PRE-NEXT: external_undefined_unreferenced SYMBOLS-PRE-NEXT: local_unreferenced SYMBOLS-NEXT: local_referenced SYMBOLS-PRE-NEXT: local_undefined_unreferenced Index: llvm/trunk/tools/llvm-objcopy/COFF/COFFObjcopy.cpp =================================================================== --- llvm/trunk/tools/llvm-objcopy/COFF/COFFObjcopy.cpp +++ llvm/trunk/tools/llvm-objcopy/COFF/COFFObjcopy.cpp @@ -47,13 +47,19 @@ return true; } - if (!Sym.Referenced && Sym.Sym.StorageClass == IMAGE_SYM_CLASS_STATIC) { - if (Config.StripUnneeded) + if (!Sym.Referenced) { + // With --strip-unneeded, GNU objcopy removes all unreferenced local + // symbols, and any unreferenced undefined external. + if (Config.StripUnneeded && + (Sym.Sym.StorageClass == IMAGE_SYM_CLASS_STATIC || + Sym.Sym.SectionNumber == 0)) return true; + // GNU objcopy keeps referenced local symbols and external symbols // if --discard-all is set, similar to what --strip-unneeded does, // but undefined local symbols are kept when --discard-all is set. - if (Config.DiscardAll && Sym.Sym.SectionNumber != 0) + if (Config.DiscardAll && Sym.Sym.StorageClass == IMAGE_SYM_CLASS_STATIC && + Sym.Sym.SectionNumber != 0) return true; }