diff --git a/llvm/test/tools/llvm-objcopy/ELF/rename-section-flag.test b/llvm/test/tools/llvm-objcopy/ELF/rename-section-flag.test --- a/llvm/test/tools/llvm-objcopy/ELF/rename-section-flag.test +++ b/llvm/test/tools/llvm-objcopy/ELF/rename-section-flag.test @@ -2,35 +2,35 @@ # Single flags on a section with no flags: # RUN: llvm-objcopy --rename-section=.foo=.bar,alloc %t %t.alloc -# RUN: llvm-readobj --sections %t.alloc | FileCheck %s --check-prefixes=CHECK,ALLOC,WRITE +# RUN: llvm-readobj --sections %t.alloc | FileCheck %s --check-prefixes=CHECK,NOBITS,ALLOC,WRITE # RUN: llvm-objcopy --rename-section=.foo=.bar,load %t %t.load -# RUN: llvm-readobj --sections %t.load | FileCheck %s --check-prefixes=CHECK,WRITE +# RUN: llvm-readobj --sections %t.load | FileCheck %s --check-prefixes=CHECK,PROGBITS,WRITE # RUN: llvm-objcopy --rename-section=.foo=.bar,noload %t %t.noload -# RUN: llvm-readobj --sections %t.noload | FileCheck %s --check-prefixes=CHECK,WRITE +# RUN: llvm-readobj --sections %t.noload | FileCheck %s --check-prefixes=CHECK,PROGBITS,WRITE # RUN: llvm-objcopy --rename-section=.foo=.bar,readonly %t %t.readonly -# RUN: llvm-readobj --sections %t.readonly | FileCheck %s --check-prefixes=CHECK +# RUN: llvm-readobj --sections %t.readonly | FileCheck %s --check-prefixes=CHECK,NOBITS # RUN: llvm-objcopy --rename-section=.foo=.bar,debug %t %t.debug -# RUN: llvm-readobj --sections %t.debug | FileCheck %s --check-prefixes=CHECK,WRITE +# RUN: llvm-readobj --sections %t.debug | FileCheck %s --check-prefixes=CHECK,PROGBITS,WRITE # RUN: llvm-objcopy --rename-section=.foo=.bar,code %t %t.code -# RUN: llvm-readobj --sections %t.code | FileCheck %s --check-prefixes=CHECK,EXEC,WRITE +# RUN: llvm-readobj --sections %t.code | FileCheck %s --check-prefixes=CHECK,PROGBITS,EXEC,WRITE # RUN: llvm-objcopy --rename-section=.foo=.bar,data %t %t.data -# RUN: llvm-readobj --sections %t.data | FileCheck %s --check-prefixes=CHECK,WRITE +# RUN: llvm-readobj --sections %t.data | FileCheck %s --check-prefixes=CHECK,PROGBITS,WRITE # RUN: llvm-objcopy --rename-section=.foo=.bar,rom %t %t.rom -# RUN: llvm-readobj --sections %t.rom | FileCheck %s --check-prefixes=CHECK,WRITE +# RUN: llvm-readobj --sections %t.rom | FileCheck %s --check-prefixes=CHECK,PROGBITS,WRITE # RUN: llvm-objcopy --rename-section=.foo=.bar,contents %t %t.contents -# RUN: llvm-readobj --sections %t.contents | FileCheck %s --check-prefixes=CHECK,WRITE +# RUN: llvm-readobj --sections %t.contents | FileCheck %s --check-prefixes=CHECK,PROGBITS,WRITE # RUN: llvm-objcopy --rename-section=.foo=.bar,merge %t %t.merge -# RUN: llvm-readobj --sections %t.merge | FileCheck %s --check-prefixes=CHECK,MERGE,WRITE +# RUN: llvm-readobj --sections %t.merge | FileCheck %s --check-prefixes=CHECK,NOBITS,MERGE,WRITE # RUN: llvm-objcopy --rename-section=.foo=.bar,strings %t %t.strings -# RUN: llvm-readobj --sections %t.strings | FileCheck %s --check-prefixes=CHECK,STRINGS,WRITE +# RUN: llvm-readobj --sections %t.strings | FileCheck %s --check-prefixes=CHECK,NOBITS,STRINGS,WRITE # RUN: llvm-objcopy --rename-section=.foo=.bar,share %t %t.share -# RUN: llvm-readobj --sections %t.share | FileCheck %s --check-prefixes=CHECK,WRITE +# RUN: llvm-readobj --sections %t.share | FileCheck %s --check-prefixes=CHECK,NOBITS,WRITE # Multiple flags: # RUN: llvm-objcopy --rename-section=.foo=.bar,alloc,readonly,strings %t %t.alloc_ro_strings -# RUN: llvm-readobj --sections %t.alloc_ro_strings | FileCheck %s --check-prefixes=CHECK,ALLOC,STRINGS +# RUN: llvm-readobj --sections %t.alloc_ro_strings | FileCheck %s --check-prefixes=CHECK,NOBITS,ALLOC,STRINGS # RUN: llvm-objcopy --rename-section=.foo=.bar,alloc,code %t %t.alloc_code -# RUN: llvm-readobj --sections %t.alloc_code | FileCheck %s --check-prefixes=CHECK,ALLOC,EXEC,WRITE +# RUN: llvm-readobj --sections %t.alloc_code | FileCheck %s --check-prefixes=CHECK,PROGBITS,ALLOC,EXEC,WRITE # Invalid flags: # RUN: not llvm-objcopy --rename-section=.foo=.bar,xyzzy %t %t.xyzzy 2>&1 | FileCheck %s --check-prefix=BAD-FLAG @@ -43,18 +43,18 @@ Machine: EM_X86_64 Sections: - Name: .foo - Type: SHT_PROGBITS + Type: SHT_NOBITS Flags: [ ] - Content: "c3c3c3c3" -# CHECK: Name: .bar -# CHECK-NEXT: Type: SHT_PROGBITS -# CHECK-NEXT: Flags [ -# ALLOC-NEXT: SHF_ALLOC (0x2) -# EXEC-NEXT: SHF_EXECINSTR (0x4) -# MERGE-NEXT: SHF_MERGE (0x10) -# STRINGS-NEXT: SHF_STRINGS (0x20) -# WRITE-NEXT: SHF_WRITE (0x1) -# CHECK-NEXT: ] +# CHECK: Name: .bar +# NOBITS-NEXT: Type: SHT_NOBITS +# PROGBITS-NEXT: Type: SHT_PROGBITS +# CHECK-NEXT: Flags [ +# ALLOC-NEXT: SHF_ALLOC (0x2) +# EXEC-NEXT: SHF_EXECINSTR (0x4) +# MERGE-NEXT: SHF_MERGE (0x10) +# STRINGS-NEXT: SHF_STRINGS (0x20) +# WRITE-NEXT: SHF_WRITE (0x1) +# CHECK-NEXT: ] # BAD-FLAG: Unrecognized section flag 'xyzzy'. Flags supported for GNU compatibility: alloc, load, noload, readonly, debug, code, data, rom, share, contents, merge, strings diff --git a/llvm/test/tools/llvm-objcopy/ELF/set-section-flags.test b/llvm/test/tools/llvm-objcopy/ELF/set-section-flags.test --- a/llvm/test/tools/llvm-objcopy/ELF/set-section-flags.test +++ b/llvm/test/tools/llvm-objcopy/ELF/set-section-flags.test @@ -2,35 +2,35 @@ # Single flags on a section with no flags: # RUN: llvm-objcopy --set-section-flags=.foo=alloc %t %t.alloc -# RUN: llvm-readobj --sections %t.alloc | FileCheck %s --check-prefixes=CHECK,ALLOC,WRITE +# RUN: llvm-readobj --sections %t.alloc | FileCheck %s --check-prefixes=CHECK,NOBITS,ALLOC,WRITE # RUN: llvm-objcopy --set-section-flags=.foo=load %t %t.load -# RUN: llvm-readobj --sections %t.load | FileCheck %s --check-prefixes=CHECK,WRITE +# RUN: llvm-readobj --sections %t.load | FileCheck %s --check-prefixes=CHECK,PROGBITS,WRITE # RUN: llvm-objcopy --set-section-flags=.foo=noload %t %t.noload -# RUN: llvm-readobj --sections %t.noload | FileCheck %s --check-prefixes=CHECK,WRITE +# RUN: llvm-readobj --sections %t.noload | FileCheck %s --check-prefixes=CHECK,PROGBITS,WRITE # RUN: llvm-objcopy --set-section-flags=.foo=readonly %t %t.readonly -# RUN: llvm-readobj --sections %t.readonly | FileCheck %s --check-prefixes=CHECK +# RUN: llvm-readobj --sections %t.readonly | FileCheck %s --check-prefixes=CHECK,NOBITS # RUN: llvm-objcopy --set-section-flags=.foo=debug %t %t.debug -# RUN: llvm-readobj --sections %t.debug | FileCheck %s --check-prefixes=CHECK,WRITE +# RUN: llvm-readobj --sections %t.debug | FileCheck %s --check-prefixes=CHECK,PROGBITS,WRITE # RUN: llvm-objcopy --set-section-flags=.foo=code %t %t.code -# RUN: llvm-readobj --sections %t.code | FileCheck %s --check-prefixes=CHECK,EXEC,WRITE +# RUN: llvm-readobj --sections %t.code | FileCheck %s --check-prefixes=CHECK,PROGBITS,EXEC,WRITE # RUN: llvm-objcopy --set-section-flags=.foo=data %t %t.data -# RUN: llvm-readobj --sections %t.data | FileCheck %s --check-prefixes=CHECK,WRITE +# RUN: llvm-readobj --sections %t.data | FileCheck %s --check-prefixes=CHECK,PROGBITS,WRITE # RUN: llvm-objcopy --set-section-flags=.foo=rom %t %t.rom -# RUN: llvm-readobj --sections %t.rom | FileCheck %s --check-prefixes=CHECK,WRITE +# RUN: llvm-readobj --sections %t.rom | FileCheck %s --check-prefixes=CHECK,PROGBITS,WRITE # RUN: llvm-objcopy --set-section-flags=.foo=contents %t %t.contents -# RUN: llvm-readobj --sections %t.contents | FileCheck %s --check-prefixes=CHECK,WRITE +# RUN: llvm-readobj --sections %t.contents | FileCheck %s --check-prefixes=CHECK,PROGBITS,WRITE # RUN: llvm-objcopy --set-section-flags=.foo=merge %t %t.merge -# RUN: llvm-readobj --sections %t.merge | FileCheck %s --check-prefixes=CHECK,MERGE,WRITE +# RUN: llvm-readobj --sections %t.merge | FileCheck %s --check-prefixes=CHECK,MERGE,NOBITS,WRITE # RUN: llvm-objcopy --set-section-flags=.foo=strings %t %t.strings -# RUN: llvm-readobj --sections %t.strings | FileCheck %s --check-prefixes=CHECK,STRINGS,WRITE +# RUN: llvm-readobj --sections %t.strings | FileCheck %s --check-prefixes=CHECK,STRINGS,NOBITS,WRITE # RUN: llvm-objcopy --set-section-flags=.foo=share %t %t.share -# RUN: llvm-readobj --sections %t.share | FileCheck %s --check-prefixes=CHECK,WRITE +# RUN: llvm-readobj --sections %t.share | FileCheck %s --check-prefixes=CHECK,NOBITS,WRITE # Multiple flags: # RUN: llvm-objcopy --set-section-flags=.foo=alloc,readonly,strings %t %t.alloc_ro_strings -# RUN: llvm-readobj --sections %t.alloc_ro_strings | FileCheck %s --check-prefixes=CHECK,ALLOC,STRINGS +# RUN: llvm-readobj --sections %t.alloc_ro_strings | FileCheck %s --check-prefixes=CHECK,NOBITS,ALLOC,STRINGS # RUN: llvm-objcopy --set-section-flags=.foo=alloc,code %t %t.alloc_code -# RUN: llvm-readobj --sections %t.alloc_code | FileCheck %s --check-prefixes=CHECK,ALLOC,EXEC,WRITE +# RUN: llvm-readobj --sections %t.alloc_code | FileCheck %s --check-prefixes=CHECK,PROGBITS,ALLOC,EXEC,WRITE # Invalid flags: # RUN: not llvm-objcopy --set-section-flags=.foo=xyzzy %t %t.xyzzy 2>&1 | FileCheck %s --check-prefix=BAD-FLAG @@ -49,18 +49,19 @@ Machine: EM_X86_64 Sections: - Name: .foo - Type: SHT_PROGBITS + Type: SHT_NOBITS Flags: [ ] -# CHECK: Name: .foo -# CHECK-NEXT: Type: SHT_PROGBITS -# CHECK-NEXT: Flags [ -# ALLOC-NEXT: SHF_ALLOC (0x2) -# EXEC-NEXT: SHF_EXECINSTR (0x4) -# MERGE-NEXT: SHF_MERGE (0x10) -# STRINGS-NEXT: SHF_STRINGS (0x20) -# WRITE-NEXT: SHF_WRITE (0x1) -# CHECK-NEXT: ] +# CHECK: Name: .foo +# NOBITS-NEXT: Type: SHT_NOBITS +# PROGBITS-NEXT: Type: SHT_PROGBITS +# CHECK-NEXT: Flags [ +# ALLOC-NEXT: SHF_ALLOC (0x2) +# EXEC-NEXT: SHF_EXECINSTR (0x4) +# MERGE-NEXT: SHF_MERGE (0x10) +# STRINGS-NEXT: SHF_STRINGS (0x20) +# WRITE-NEXT: SHF_WRITE (0x1) +# CHECK-NEXT: ] # BAD-FORMAT: Bad format for --set-section-flags: missing '=' # MULTIPLE-SETS: --set-section-flags set multiple times for section .foo diff --git a/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp b/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp --- a/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp +++ b/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp @@ -96,6 +96,17 @@ return (OldFlags & PreserveMask) | (NewFlags & ~PreserveMask); } +static void setSectionFlags(SectionBase &Sec, SectionFlag NewFlags) { + Sec.Flags = setSectionFlagsPreserveMask(Sec.Flags, getNewShfFlags(NewFlags)); + + // Certain flags also promote SHT_NOBITS to SHT_PROGBITS + if (NewFlags & + (SectionFlag::SecContents | SectionFlag::SecLoad | + SectionFlag::SecNoload | SectionFlag::SecCode | SectionFlag::SecData | + SectionFlag::SecRom | SectionFlag::SecDebug)) + Sec.Type = SHT_PROGBITS; +} + static ElfType getOutputElfType(const Binary &Bin) { // Infer output ELF type from the input ELF object if (isa>(Bin)) @@ -574,8 +585,7 @@ const SectionRename &SR = Iter->second; Sec.Name = SR.NewName; if (SR.NewFlags.hasValue()) - Sec.Flags = setSectionFlagsPreserveMask( - Sec.Flags, getNewShfFlags(SR.NewFlags.getValue())); + setSectionFlags(Sec, SR.NewFlags.getValue()); } } } @@ -585,8 +595,7 @@ const auto Iter = Config.SetSectionFlags.find(Sec.Name); if (Iter != Config.SetSectionFlags.end()) { const SectionFlagsUpdate &SFU = Iter->second; - Sec.Flags = setSectionFlagsPreserveMask(Sec.Flags, - getNewShfFlags(SFU.NewFlags)); + setSectionFlags(Sec, SFU.NewFlags); } } }