Index: llvm/docs/CommandGuide/llvm-objcopy.rst =================================================================== --- llvm/docs/CommandGuide/llvm-objcopy.rst +++ llvm/docs/CommandGuide/llvm-objcopy.rst @@ -418,6 +418,7 @@ - `load` = if the section has `SHT_NOBITS` type, mark it as a `SHT_PROGBITS` section. - `readonly` = if this flag is not specified, add the `SHF_WRITE` flag. + - `exclude` = add the `SHF_EXCLUDE` flag. - `code` = add the `SHF_EXECINSTR` flag. - `merge` = add the `SHF_MERGE` flag. - `strings` = add the `SHF_STRINGS` flag. Index: llvm/test/tools/llvm-objcopy/ELF/rename-section-flag.test =================================================================== --- llvm/test/tools/llvm-objcopy/ELF/rename-section-flag.test +++ llvm/test/tools/llvm-objcopy/ELF/rename-section-flag.test @@ -13,6 +13,9 @@ # RUN: llvm-objcopy --rename-section=.foo=.bar,readonly \ # RUN: --rename-section=.baz=.blah,readonly %t %t.readonly # RUN: llvm-readobj --sections %t.readonly | FileCheck %s --check-prefixes=CHECK,PROGBITS +# RUN: llvm-objcopy --rename-section=.foo=.bar,exclude \ +# RUN: --rename-section=.baz=.blah,exclude %t %t.exclude +# RUN: llvm-readobj --sections %t.exclude | FileCheck %s --check-prefixes=CHECK,PROGBITS,EXCLUDE,WRITE # RUN: llvm-objcopy --rename-section=.foo=.bar,debug \ # RUN: --rename-section=.baz=.blah,debug %t %t.debug # RUN: llvm-readobj --sections %t.debug | FileCheck %s --check-prefixes=CHECK,PROGBITS,WRITE @@ -45,6 +48,9 @@ # RUN: llvm-objcopy --rename-section=.foo=.bar,alloc,code \ # RUN: --rename-section=.baz=.blah,alloc,code %t %t.alloc_code # RUN: llvm-readobj --sections %t.alloc_code | FileCheck %s --check-prefixes=CHECK,NOBITS,ALLOC,EXEC,WRITE +# RUN: llvm-objcopy --rename-section=.foo=.bar,contents,readonly,exclude \ +# RUN: --rename-section=.baz=.blah,contents,readonly,exclude %t %t.contents_ro_exclude +# RUN: llvm-readobj --sections %t.contents_ro_exclude | FileCheck %s --check-prefixes=CHECK,PROGBITS,EXCLUDE # Invalid flags: # RUN: not llvm-objcopy --rename-section=.foo=.bar,xyzzy %t %t.xyzzy 2>&1 | FileCheck %s --check-prefix=BAD-FLAG @@ -81,6 +87,7 @@ # EXEC-NEXT: SHF_EXECINSTR (0x4) # MERGE-NEXT: SHF_MERGE (0x10) # STRINGS-NEXT: SHF_STRINGS (0x20) +# EXCLUDE-NEXT: SHF_EXCLUDE (0x80000000) # WRITE-NEXT: SHF_WRITE (0x1) # CHECK-NEXT: ] @@ -92,7 +99,8 @@ # EXEC-NEXT: SHF_EXECINSTR (0x4) # MERGE-NEXT: SHF_MERGE (0x10) # STRINGS-NEXT: SHF_STRINGS (0x20) +# EXCLUDE-NEXT: SHF_EXCLUDE (0x80000000) # 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 +# BAD-FLAG: unrecognized section flag 'xyzzy'. Flags supported for GNU compatibility: alloc, load, noload, readonly, exclude, debug, code, data, rom, share, contents, merge, strings Index: llvm/test/tools/llvm-objcopy/ELF/set-section-flags.test =================================================================== --- llvm/test/tools/llvm-objcopy/ELF/set-section-flags.test +++ llvm/test/tools/llvm-objcopy/ELF/set-section-flags.test @@ -13,6 +13,9 @@ # RUN: llvm-objcopy --set-section-flags=.foo=readonly \ # RUN: --set-section-flags=.baz=readonly --set-section-flags=.rela.baz=readonly %t %t.readonly # RUN: llvm-readobj --sections %t.readonly | FileCheck %s --check-prefixes=CHECK,PROGBITS +# RUN: llvm-objcopy --set-section-flags=.foo=exclude \ +# RUN: --set-section-flags=.baz=exclude --set-section-flags=.rela.baz=exclude %t %t.exclude +# RUN: llvm-readobj --sections %t.exclude | FileCheck %s --check-prefixes=CHECK,PROGBITS,EXCLUDE,WRITE # RUN: llvm-objcopy --set-section-flags=.foo=debug \ # RUN: --set-section-flags=.baz=debug --set-section-flags=.rela.baz=debug %t %t.debug # RUN: llvm-readobj --sections %t.debug | FileCheck %s --check-prefixes=CHECK,PROGBITS,WRITE @@ -47,6 +50,10 @@ # RUN: --set-section-flags=.baz=alloc,code \ # RUN: --set-section-flags=.rela.baz=alloc,code %t %t.alloc_code # RUN: llvm-readobj --sections %t.alloc_code | FileCheck %s --check-prefixes=CHECK,NOBITS,ALLOC,EXEC,WRITE +# RUN: llvm-objcopy --set-section-flags=.foo=contents,readonly,exclude \ +# RUN: --set-section-flags=.baz=contents,readonly,exclude \ +# RUN: --set-section-flags=.rela.baz=contents,readonly,exclude %t %t.contents_ro_exclude +# RUN: llvm-readobj --sections %t.contents_ro_exclude | FileCheck %s --check-prefixes=CHECK,PROGBITS,EXCLUDE # Invalid flags: # RUN: not llvm-objcopy --set-section-flags=.foo=xyzzy %t %t.xyzzy 2>&1 | FileCheck %s --check-prefix=BAD-FLAG @@ -93,6 +100,7 @@ # EXEC-NEXT: SHF_EXECINSTR (0x4) # MERGE-NEXT: SHF_MERGE (0x10) # STRINGS-NEXT: SHF_STRINGS (0x20) +# EXCLUDE-NEXT: SHF_EXCLUDE (0x80000000) # WRITE-NEXT: SHF_WRITE (0x1) # CHECK-NEXT: ] @@ -104,6 +112,7 @@ # EXEC-NEXT: SHF_EXECINSTR (0x4) # MERGE-NEXT: SHF_MERGE (0x10) # STRINGS-NEXT: SHF_STRINGS (0x20) +# EXCLUDE-NEXT: SHF_EXCLUDE (0x80000000) # WRITE-NEXT: SHF_WRITE (0x1) # CHECK-NEXT: ] @@ -114,10 +123,11 @@ # EXEC-NEXT: SHF_EXECINSTR (0x4) # MERGE-NEXT: SHF_MERGE (0x10) # STRINGS-NEXT: SHF_STRINGS (0x20) +# EXCLUDE-NEXT: SHF_EXCLUDE (0x80000000) # 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' -# BAD-FLAG: unrecognized section flag 'xyzzy'. Flags supported for GNU compatibility: alloc, load, noload, readonly, debug, code, data, rom, share, contents, merge, strings +# BAD-FLAG: unrecognized section flag 'xyzzy'. Flags supported for GNU compatibility: alloc, load, noload, readonly, exclude, debug, code, data, rom, share, contents, merge, strings Index: llvm/tools/llvm-objcopy/CopyConfig.h =================================================================== --- llvm/tools/llvm-objcopy/CopyConfig.h +++ llvm/tools/llvm-objcopy/CopyConfig.h @@ -69,7 +69,8 @@ SecStrings = 1 << 9, SecContents = 1 << 10, SecShare = 1 << 11, - LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ SecShare) + SecExclude = 1 << 12, + LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/SecExclude) }; struct SectionRename { Index: llvm/tools/llvm-objcopy/CopyConfig.cpp =================================================================== --- llvm/tools/llvm-objcopy/CopyConfig.cpp +++ llvm/tools/llvm-objcopy/CopyConfig.cpp @@ -146,6 +146,7 @@ .CaseLower("strings", SectionFlag::SecStrings) .CaseLower("contents", SectionFlag::SecContents) .CaseLower("share", SectionFlag::SecShare) + .CaseLower("exclude", SectionFlag::SecExclude) .Default(SectionFlag::SecNone); } @@ -158,8 +159,8 @@ return createStringError( errc::invalid_argument, "unrecognized section flag '%s'. Flags supported for GNU " - "compatibility: alloc, load, noload, readonly, debug, code, data, " - "rom, share, contents, merge, strings", + "compatibility: alloc, load, noload, readonly, exclude, debug, code, " + "data, rom, share, contents, merge, strings", Flag.str().c_str()); ParsedFlags |= ParsedFlag; } Index: llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp =================================================================== --- llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp +++ llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp @@ -83,6 +83,8 @@ NewFlags |= ELF::SHF_MERGE; if (AllFlags & SectionFlag::SecStrings) NewFlags |= ELF::SHF_STRINGS; + if (AllFlags & SectionFlag::SecExclude) + NewFlags |= ELF::SHF_EXCLUDE; return NewFlags; } @@ -90,11 +92,10 @@ uint64_t NewFlags) { // Preserve some flags which should not be dropped when setting flags. // Also, preserve anything OS/processor dependant. - const uint64_t PreserveMask = ELF::SHF_COMPRESSED | ELF::SHF_EXCLUDE | - ELF::SHF_GROUP | ELF::SHF_LINK_ORDER | - ELF::SHF_MASKOS | ELF::SHF_MASKPROC | - ELF::SHF_TLS | ELF::SHF_INFO_LINK; - return (OldFlags & PreserveMask) | (NewFlags & ~PreserveMask); + const uint64_t PreserveMask = + ELF::SHF_COMPRESSED | ELF::SHF_GROUP | ELF::SHF_LINK_ORDER | + ELF::SHF_MASKOS | ELF::SHF_MASKPROC | ELF::SHF_TLS | ELF::SHF_INFO_LINK; + return (OldFlags & PreserveMask) | NewFlags; } static void setSectionFlagsAndType(SectionBase &Sec, SectionFlag Flags) {