Skip to content

Commit

Permalink
[MC] Set defaults based on section names and support name suffixes
Browse files Browse the repository at this point in the history
Set correct default flags and section type based on its name for .text,
.data, .bss, .init_array, .fini_array, .preinit_array, .tdata, and .tbss
and support section name suffixes for .data.*, .rodata.*, .text.*,
.bss.*, .tdata.* and .tbss.* which matches the behavior of GAS.

Fixes PR31888.

Differential Revision: https://reviews.llvm.org/D30229

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@299484 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
petrhosek committed Apr 4, 2017
1 parent 6f50c7f commit 802fcd9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
21 changes: 15 additions & 6 deletions lib/MC/MCParser/ELFAsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,11 +493,20 @@ bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) {
int64_t UniqueID = ~0;

// Set the defaults first.
if (SectionName == ".fini" || SectionName == ".init" ||
SectionName == ".rodata")
if (hasPrefix(SectionName, ".rodata.") || SectionName == ".rodata1")
Flags |= ELF::SHF_ALLOC;
if (SectionName == ".fini" || SectionName == ".init")
Flags |= ELF::SHF_EXECINSTR;
if (SectionName == ".fini" || SectionName == ".init" ||
hasPrefix(SectionName, ".text."))
Flags |= ELF::SHF_ALLOC | ELF::SHF_EXECINSTR;
if (hasPrefix(SectionName, ".data.") || SectionName == ".data1" ||
hasPrefix(SectionName, ".bss.") ||
hasPrefix(SectionName, ".init_array.") ||
hasPrefix(SectionName, ".fini_array.") ||
hasPrefix(SectionName, ".preinit_array."))
Flags |= ELF::SHF_ALLOC | ELF::SHF_WRITE;
if (hasPrefix(SectionName, ".tdata.") ||
hasPrefix(SectionName, ".tbss."))
Flags |= ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_TLS;

if (getLexer().is(AsmToken::Comma)) {
Lex();
Expand Down Expand Up @@ -575,9 +584,9 @@ bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) {
Type = ELF::SHT_NOBITS;
else if (hasPrefix(SectionName, ".tbss."))
Type = ELF::SHT_NOBITS;
else if (SectionName == ".fini_array")
else if (hasPrefix(SectionName, ".fini_array."))
Type = ELF::SHT_FINI_ARRAY;
else if (SectionName == ".preinit_array")
else if (hasPrefix(SectionName, ".preinit_array."))
Type = ELF::SHT_PREINIT_ARRAY;
} else {
if (TypeName == "init_array")
Expand Down
47 changes: 47 additions & 0 deletions test/MC/ELF/section.s
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,50 @@ bar:
// CHECK-NEXT: Size:
// CHECK-NEXT: Link: 22
// CHECK-NEXT: Info: 0

.section .text.foo
// CHECK: Section {
// CHECK: Name: .text.foo
// CHECK-NEXT: Type: SHT_PROGBITS
// CHECK-NEXT: Flags [
// CHECK-NEXT: SHF_ALLOC
// CHECK-NEXT: SHF_EXECINSTR
// CHECK-NEXT: ]

.section .bss
// CHECK: Section {
// CHECK: Name: .bss
// CHECK-NEXT: Type: SHT_NOBITS
// CHECK-NEXT: Flags [
// CHECK-NEXT: SHF_ALLOC
// CHECK-NEXT: SHF_WRITE
// CHECK-NEXT: ]

.section .bss.foo
// CHECK: Section {
// CHECK: Name: .bss.foo
// CHECK-NEXT: Type: SHT_NOBITS
// CHECK-NEXT: Flags [
// CHECK-NEXT: SHF_ALLOC
// CHECK-NEXT: SHF_WRITE
// CHECK-NEXT: ]

.section .tbss
// CHECK: Section {
// CHECK: Name: .tbss
// CHECK-NEXT: Type: SHT_NOBITS
// CHECK-NEXT: Flags [
// CHECK-NEXT: SHF_ALLOC
// CHECK-NEXT: SHF_TLS
// CHECK-NEXT: SHF_WRITE
// CHECK-NEXT: ]

.section .tbss.foo
// CHECK: Section {
// CHECK: Name: .tbss.foo
// CHECK-NEXT: Type: SHT_NOBITS
// CHECK-NEXT: Flags [
// CHECK-NEXT: SHF_ALLOC
// CHECK-NEXT: SHF_TLS
// CHECK-NEXT: SHF_WRITE
// CHECK-NEXT: ]

0 comments on commit 802fcd9

Please sign in to comment.