llvm.org GIT mirror llvm / 7912c32
[llvm-objcopy] Simplify SHT_NOBITS -> SHT_PROGBITS promotion GNU objcopy uses bfd_elf_get_default_section_type to decide the candidate section type, which roughly translates to our [a] (I assume SEC_COMMON implies SHF_ALLOC): (!(Sec.Flags & ELF::SHF_ALLOC) || Flags & (SectionFlag::SecContents | SectionFlag::SecLoad))) Then, it updates the section type in bfd/elf.c:elf_fake_sections if: if (this_hdr->sh_type == SHT_NULL) this_hdr->sh_type = sh_type; // common case else if (this_hdr->sh_type == SHT_NOBITS && sh_type == SHT_PROGBITS && (asect->flags & SEC_ALLOC) != 0) // uncommon case ... this_hdr->sh_type = sh_type; If the following condition is met the uncommon branch is executed: if (elf_section_type (osec) == SHT_NULL && (osec->flags == isec->flags || (final_link && ((osec->flags ^ isec->flags) & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0))) I suggest we just ignore this clause and follow the common case behavior, which is done in this patch. Rationales to do so: If --set-section-flags is a no-op (osec->flags == isec->flags) (corresponds to the "readonly" test in set-section-flags.test), GNU objcopy will require (Sec.Flags & ELF::SHF_ALLOC). [a] is essentially: Flags & (SectionFlag::SecContents | SectionFlag::SecLoad) This special case is not really useful. Non-SHF_ALLOC SHT_NOBITS sections do not make much sense and it doesn't matter if they are SHT_NOBITS or SHT_PROGBITS. For all other RUN lines in set-section-flags.test, the new behavior matches GNU objcopy, i.e. this patch improves compatibility. Differential Revision: https://reviews.llvm.org/D60189 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@359639 91177308-0d34-0410-b5e6-96231b3b80d8 Fangrui Song 1 year, 5 months ago
3 changed file(s) with 16 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
1111 # RUN: llvm-readobj --sections %t.noload | FileCheck %s --check-prefixes=CHECK,PROGBITS,WRITE
1212 # RUN: llvm-objcopy --rename-section=.foo=.bar,readonly \
1313 # RUN: --rename-section=.baz=.blah,readonly %t %t.readonly
14 # RUN: llvm-readobj --sections %t.readonly | FileCheck %s --check-prefixes=CHECK,NOBITS
14 # RUN: llvm-readobj --sections %t.readonly | FileCheck %s --check-prefixes=CHECK,PROGBITS
1515 # RUN: llvm-objcopy --rename-section=.foo=.bar,debug \
1616 # RUN: --rename-section=.baz=.blah,debug %t %t.debug
1717 # RUN: llvm-readobj --sections %t.debug | FileCheck %s --check-prefixes=CHECK,PROGBITS,WRITE
2929 # RUN: llvm-readobj --sections %t.contents | FileCheck %s --check-prefixes=CHECK,PROGBITS,WRITE
3030 # RUN: llvm-objcopy --rename-section=.foo=.bar,merge \
3131 # RUN: --rename-section=.baz=.blah,merge %t %t.merge
32 # RUN: llvm-readobj --sections %t.merge | FileCheck %s --check-prefixes=CHECK,NOBITS,MERGE,WRITE
32 # RUN: llvm-readobj --sections %t.merge | FileCheck %s --check-prefixes=CHECK,PROGBITS,MERGE,WRITE
3333 # RUN: llvm-objcopy --rename-section=.foo=.bar,strings \
3434 # RUN: --rename-section=.baz=.blah,strings %t %t.strings
35 # RUN: llvm-readobj --sections %t.strings | FileCheck %s --check-prefixes=CHECK,NOBITS,STRINGS,WRITE
35 # RUN: llvm-readobj --sections %t.strings | FileCheck %s --check-prefixes=CHECK,PROGBITS,STRINGS,WRITE
3636 # RUN: llvm-objcopy --rename-section=.foo=.bar,share \
3737 # RUN: --rename-section=.baz=.blah,share %t %t.share
38 # RUN: llvm-readobj --sections %t.share | FileCheck %s --check-prefixes=CHECK,NOBITS,WRITE
38 # RUN: llvm-readobj --sections %t.share | FileCheck %s --check-prefixes=CHECK,PROGBITS,WRITE
3939
4040 # Multiple flags:
4141 # RUN: llvm-objcopy --rename-section=.foo=.bar,alloc,readonly,strings \
4343 # RUN: llvm-readobj --sections %t.alloc_ro_strings | FileCheck %s --check-prefixes=CHECK,NOBITS,ALLOC,STRINGS
4444 # RUN: llvm-objcopy --rename-section=.foo=.bar,alloc,code \
4545 # RUN: --rename-section=.baz=.blah,alloc,code %t %t.alloc_code
46 # RUN: llvm-readobj --sections %t.alloc_code | FileCheck %s --check-prefixes=CHECK,PROGBITS,ALLOC,EXEC,WRITE
46 # RUN: llvm-readobj --sections %t.alloc_code | FileCheck %s --check-prefixes=CHECK,NOBITS,ALLOC,EXEC,WRITE
4747
4848 # Invalid flags:
4949 # RUN: not llvm-objcopy --rename-section=.foo=.bar,xyzzy %t %t.xyzzy 2>&1 | FileCheck %s --check-prefix=BAD-FLAG
1111 # RUN: llvm-readobj --sections %t.noload | FileCheck %s --check-prefixes=CHECK,PROGBITS,WRITE
1212 # RUN: llvm-objcopy --set-section-flags=.foo=readonly \
1313 # RUN: --set-section-flags=.baz=readonly --set-section-flags=.rela.baz=readonly %t %t.readonly
14 # RUN: llvm-readobj --sections %t.readonly | FileCheck %s --check-prefixes=CHECK,NOBITS
14 # RUN: llvm-readobj --sections %t.readonly | FileCheck %s --check-prefixes=CHECK,PROGBITS
1515 # RUN: llvm-objcopy --set-section-flags=.foo=debug \
1616 # RUN: --set-section-flags=.baz=debug --set-section-flags=.rela.baz=debug %t %t.debug
1717 # RUN: llvm-readobj --sections %t.debug | FileCheck %s --check-prefixes=CHECK,PROGBITS,WRITE
2929 # RUN: llvm-readobj --sections %t.contents | FileCheck %s --check-prefixes=CHECK,PROGBITS,WRITE
3030 # RUN: llvm-objcopy --set-section-flags=.foo=merge \
3131 # RUN: --set-section-flags=.baz=merge --set-section-flags=.rela.baz=merge %t %t.merge
32 # RUN: llvm-readobj --sections %t.merge | FileCheck %s --check-prefixes=CHECK,MERGE,NOBITS,WRITE
32 # RUN: llvm-readobj --sections %t.merge | FileCheck %s --check-prefixes=CHECK,MERGE,PROGBITS,WRITE
3333 # RUN: llvm-objcopy --set-section-flags=.foo=strings \
3434 # RUN: --set-section-flags=.baz=strings --set-section-flags=.rela.baz=strings %t %t.strings
35 # RUN: llvm-readobj --sections %t.strings | FileCheck %s --check-prefixes=CHECK,STRINGS,NOBITS,WRITE
35 # RUN: llvm-readobj --sections %t.strings | FileCheck %s --check-prefixes=CHECK,STRINGS,PROGBITS,WRITE
3636 # RUN: llvm-objcopy --set-section-flags=.foo=share \
3737 # RUN: --set-section-flags=.baz=share --set-section-flags=.rela.baz=share %t %t.share
38 # RUN: llvm-readobj --sections %t.share | FileCheck %s --check-prefixes=CHECK,NOBITS,WRITE
38 # RUN: llvm-readobj --sections %t.share | FileCheck %s --check-prefixes=CHECK,PROGBITS,WRITE
3939
4040 # Multiple flags:
4141 # RUN: llvm-objcopy --set-section-flags=.foo=alloc,readonly,strings \
4545 # RUN: llvm-objcopy --set-section-flags=.foo=alloc,code \
4646 # RUN: --set-section-flags=.baz=alloc,code \
4747 # RUN: --set-section-flags=.rela.baz=alloc,code %t %t.alloc_code
48 # RUN: llvm-readobj --sections %t.alloc_code | FileCheck %s --check-prefixes=CHECK,PROGBITS,ALLOC,EXEC,WRITE
48 # RUN: llvm-readobj --sections %t.alloc_code | FileCheck %s --check-prefixes=CHECK,NOBITS,ALLOC,EXEC,WRITE
4949
5050 # Invalid flags:
5151 # RUN: not llvm-objcopy --set-section-flags=.foo=xyzzy %t %t.xyzzy 2>&1 | FileCheck %s --check-prefix=BAD-FLAG
9898 static void setSectionFlagsAndType(SectionBase &Sec, SectionFlag Flags) {
9999 Sec.Flags = getSectionFlagsPreserveMask(Sec.Flags, getNewShfFlags(Flags));
100100
101 // Certain flags also promote SHT_NOBITS to SHT_PROGBITS. Don't change other
102 // section types (RELA, SYMTAB, etc.).
103 const SectionFlag NoBitsToProgBitsMask =
104 SectionFlag::SecContents | SectionFlag::SecLoad | SectionFlag::SecNoload |
105 SectionFlag::SecCode | SectionFlag::SecData | SectionFlag::SecRom |
106 SectionFlag::SecDebug;
107 if (Sec.Type == SHT_NOBITS && (Flags & NoBitsToProgBitsMask))
101 // In GNU objcopy, certain flags promote SHT_NOBITS to SHT_PROGBITS. This rule
102 // may promote more non-ALLOC sections than GNU objcopy, but it is fine as
103 // non-ALLOC SHT_NOBITS sections do not make much sense.
104 if (Sec.Type == SHT_NOBITS &&
105 (!(Sec.Flags & ELF::SHF_ALLOC) ||
106 Flags & (SectionFlag::SecContents | SectionFlag::SecLoad)))
108107 Sec.Type = SHT_PROGBITS;
109108 }
110109