From a8488b04e183d1faac3fdda858d98f4178d4ac77 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Thu, 25 Nov 2010 15:32:56 +0000 Subject: [PATCH] Factor some code to parseSectionFlags and fix the default type of a section. llvm-svn: 120145 --- lib/MC/MCParser/ELFAsmParser.cpp | 107 +++++++++++++++++-------------- test/MC/ELF/section.s | 13 ++++ 2 files changed, 71 insertions(+), 49 deletions(-) diff --git a/lib/MC/MCParser/ELFAsmParser.cpp b/lib/MC/MCParser/ELFAsmParser.cpp index 42cd9194726..0aa89c20a2f 100644 --- a/lib/MC/MCParser/ELFAsmParser.cpp +++ b/lib/MC/MCParser/ELFAsmParser.cpp @@ -201,6 +201,46 @@ static SectionKind computeSectionKind(unsigned Flags) { return SectionKind::getDataRel(); } +static int parseSectionFlags(StringRef flagsStr) { + int flags = 0; + + for (unsigned i = 0; i < flagsStr.size(); i++) { + switch (flagsStr[i]) { + case 'a': + flags |= MCSectionELF::SHF_ALLOC; + break; + case 'x': + flags |= MCSectionELF::SHF_EXECINSTR; + break; + case 'w': + flags |= MCSectionELF::SHF_WRITE; + break; + case 'M': + flags |= MCSectionELF::SHF_MERGE; + break; + case 'S': + flags |= MCSectionELF::SHF_STRINGS; + break; + case 'T': + flags |= MCSectionELF::SHF_TLS; + break; + case 'c': + flags |= MCSectionELF::XCORE_SHF_CP_SECTION; + break; + case 'd': + flags |= MCSectionELF::XCORE_SHF_DP_SECTION; + break; + case 'G': + flags |= MCSectionELF::SHF_GROUP; + break; + default: + return -1; + } + } + + return flags; +} + // FIXME: This is a work in progress. bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) { StringRef SectionName; @@ -208,21 +248,34 @@ bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) { if (ParseSectionName(SectionName)) return TokError("expected identifier in directive"); - StringRef FlagsStr; StringRef TypeName; int64_t Size = 0; StringRef GroupName; + unsigned Flags = 0; + + // Set the defaults first. + if (SectionName == ".fini" || SectionName == ".init" || + SectionName == ".rodata") + Flags |= MCSectionELF::SHF_ALLOC; + if (SectionName == ".fini" || SectionName == ".init") + Flags |= MCSectionELF::SHF_EXECINSTR; + if (getLexer().is(AsmToken::Comma)) { Lex(); if (getLexer().isNot(AsmToken::String)) return TokError("expected string in directive"); - FlagsStr = getTok().getStringContents(); + StringRef FlagsStr = getTok().getStringContents(); Lex(); - bool Mergeable = FlagsStr.find('M') != StringRef::npos; - bool Group = FlagsStr.find('G') != StringRef::npos; + int extraFlags = parseSectionFlags(FlagsStr); + if (extraFlags < 0) + return TokError("unknown flag"); + Flags |= extraFlags; + + bool Mergeable = Flags & MCSectionELF::SHF_MERGE; + bool Group = Flags & MCSectionELF::SHF_GROUP; if (getLexer().isNot(AsmToken::Comma)) { if (Mergeable) @@ -269,51 +322,7 @@ bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) { if (getLexer().isNot(AsmToken::EndOfStatement)) return TokError("unexpected token in directive"); - unsigned Flags = 0; - unsigned Type = MCSectionELF::SHT_NULL; - - // Set the defaults first. - if (SectionName == ".fini" || SectionName == ".init" || SectionName == ".rodata") { - Type = MCSectionELF::SHT_PROGBITS; - Flags |= MCSectionELF::SHF_ALLOC; - } - if (SectionName == ".fini" || SectionName == ".init") { - Flags |= MCSectionELF::SHF_EXECINSTR; - } - - for (unsigned i = 0; i < FlagsStr.size(); i++) { - switch (FlagsStr[i]) { - case 'a': - Flags |= MCSectionELF::SHF_ALLOC; - break; - case 'x': - Flags |= MCSectionELF::SHF_EXECINSTR; - break; - case 'w': - Flags |= MCSectionELF::SHF_WRITE; - break; - case 'M': - Flags |= MCSectionELF::SHF_MERGE; - break; - case 'S': - Flags |= MCSectionELF::SHF_STRINGS; - break; - case 'T': - Flags |= MCSectionELF::SHF_TLS; - break; - case 'c': - Flags |= MCSectionELF::XCORE_SHF_CP_SECTION; - break; - case 'd': - Flags |= MCSectionELF::XCORE_SHF_DP_SECTION; - break; - case 'G': - Flags |= MCSectionELF::SHF_GROUP; - break; - default: - return TokError("unknown flag"); - } - } + unsigned Type = MCSectionELF::SHT_PROGBITS; if (!TypeName.empty()) { if (TypeName == "init_array") diff --git a/test/MC/ELF/section.s b/test/MC/ELF/section.s index f3700cae679..38cf8ad4356 100644 --- a/test/MC/ELF/section.s +++ b/test/MC/ELF/section.s @@ -17,6 +17,7 @@ .section .init .section .fini .section .rodata +.section zed, "" // CHECK: (('sh_name', 0x00000049) # '.init' // CHECK-NEXT: ('sh_type', 0x00000001) @@ -53,6 +54,18 @@ // CHECK-NEXT: ('sh_addralign', 0x00000001) // CHECK-NEXT: ('sh_entsize', 0x00000000) // CHECK-NEXT: ), +// CHECK-NEXT: # Section 0x0000000d +// CHECK-NEXT: (('sh_name', 0x0000005d) # 'zed' +// CHECK-NEXT: ('sh_type', 0x00000001) +// CHECK-NEXT: ('sh_flags', 0x00000000) +// CHECK-NEXT: ('sh_addr', 0x00000000) +// CHECK-NEXT: ('sh_offset', 0x00000050) +// CHECK-NEXT: ('sh_size', 0x00000000) +// CHECK-NEXT: ('sh_link', 0x00000000) +// CHECK-NEXT: ('sh_info', 0x00000000) +// CHECK-NEXT: ('sh_addralign', 0x00000001) +// CHECK-NEXT: ('sh_entsize', 0x00000000) +// CHECK-NEXT: ), // Test that we can parse these foo: