diff options
author | Daniel Dunbar <daniel@zuster.org> | 2009-08-26 22:49:51 +0000 |
---|---|---|
committer | Daniel Dunbar <daniel@zuster.org> | 2009-08-26 22:49:51 +0000 |
commit | 418706d9a8a304630ddecc3709598d823bee8188 (patch) | |
tree | f98fc31749e21c56fa7e77437b0c1258e07e632c | |
parent | 1ea86df7ddb44659fbf694934abb2177beed8637 (diff) | |
download | external_llvm-418706d9a8a304630ddecc3709598d823bee8188.zip external_llvm-418706d9a8a304630ddecc3709598d823bee8188.tar.gz external_llvm-418706d9a8a304630ddecc3709598d823bee8188.tar.bz2 |
llvm-mc/Mach-O: Unique sections properly, so we don't get duplicate text
sections, etc.
- The quick and dirty way, just clone the TargetLoweringObjectFile
code. Eventually this should be shared... somehow.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@80168 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | test/MC/MachO/sections.s | 3 | ||||
-rw-r--r-- | tools/llvm-mc/AsmParser.cpp | 71 | ||||
-rw-r--r-- | tools/llvm-mc/AsmParser.h | 17 | ||||
-rw-r--r-- | tools/llvm-mc/llvm-mc.cpp | 6 |
4 files changed, 74 insertions, 23 deletions
diff --git a/test/MC/MachO/sections.s b/test/MC/MachO/sections.s index 11a86a9..bcbd9f4 100644 --- a/test/MC/MachO/sections.s +++ b/test/MC/MachO/sections.s @@ -1,5 +1,8 @@ // RUN: llvm-mc -triple i386-apple-darwin9 %s -filetype=obj -o - | macho-dump | FileCheck %s + .text + .section __TEXT,__text,regular,pure_instructions + .const .static_const .cstring diff --git a/tools/llvm-mc/AsmParser.cpp b/tools/llvm-mc/AsmParser.cpp index 467f809..2c855d9 100644 --- a/tools/llvm-mc/AsmParser.cpp +++ b/tools/llvm-mc/AsmParser.cpp @@ -14,6 +14,7 @@ #include "AsmParser.h" #include "AsmExpr.h" +#include "llvm/ADT/SmallString.h" #include "llvm/ADT/Twine.h" #include "llvm/MC/MCContext.h" #include "llvm/MC/MCInst.h" @@ -25,6 +26,48 @@ #include "llvm/Target/TargetAsmParser.h" using namespace llvm; +// Mach-O section uniquing. +// +// FIXME: Figure out where this should live, it should be shared by +// TargetLoweringObjectFile. +typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy; + +AsmParser::~AsmParser() { + // If we have the MachO uniquing map, free it. + delete (MachOUniqueMapTy*)SectionUniquingMap; +} + +const MCSection *AsmParser::getMachOSection(const StringRef &Segment, + const StringRef &Section, + unsigned TypeAndAttributes, + unsigned Reserved2, + SectionKind Kind) const { + // We unique sections by their segment/section pair. The returned section + // may not have the same flags as the requested section, if so this should be + // diagnosed by the client as an error. + + // Create the map if it doesn't already exist. + if (SectionUniquingMap == 0) + SectionUniquingMap = new MachOUniqueMapTy(); + MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)SectionUniquingMap; + + // Form the name to look up. + SmallString<64> Name; + Name += Segment; + Name.push_back(','); + Name += Section; + + // Do the lookup, if we have a hit, return it. + const MCSectionMachO *&Entry = Map[Name.str()]; + + // FIXME: This should validate the type and attributes. + if (Entry) return Entry; + + // Otherwise, return a new section. + return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes, + Reserved2, Kind, Ctx); +} + void AsmParser::Warning(SMLoc L, const Twine &Msg) { Lexer.PrintMessage(L, Msg.str(), "warning"); } @@ -40,6 +83,15 @@ bool AsmParser::TokError(const char *Msg) { } bool AsmParser::Run() { + // Create the initial section. + // + // FIXME: Support -n. + // FIXME: Target hook & command line option for initial section. + Out.SwitchSection(getMachOSection("__TEXT", "__text", + MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, + 0, SectionKind())); + + // Prime the lexer. Lexer.Lex(); @@ -770,15 +822,9 @@ bool AsmParser::ParseDirectiveDarwinSection() { if (!ErrorStr.empty()) return Error(Loc, ErrorStr.c_str()); - // FIXME: CACHE THESE. - // FIXME: Arch specific. - MCSection *S = 0; //Ctx.GetSection(Section); - if (S == 0) - S = MCSectionMachO::Create(Segment, Section, TAA, StubSize, - SectionKind(), Ctx); - - Out.SwitchSection(S); + Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize, + SectionKind())); return false; } @@ -792,13 +838,8 @@ bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment, Lexer.Lex(); // FIXME: Arch specific. - // FIXME: Cache this! - MCSection *S = 0; // Ctx.GetSection(Section); - if (S == 0) - S = MCSectionMachO::Create(Segment, Section, TAA, StubSize, - SectionKind(), Ctx); - - Out.SwitchSection(S); + Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize, + SectionKind())); // Set the implicit alignment, if any. // diff --git a/tools/llvm-mc/AsmParser.h b/tools/llvm-mc/AsmParser.h index 21a7f46..8ab4b43 100644 --- a/tools/llvm-mc/AsmParser.h +++ b/tools/llvm-mc/AsmParser.h @@ -18,6 +18,7 @@ #include "AsmLexer.h" #include "AsmCond.h" #include "llvm/MC/MCAsmParser.h" +#include "llvm/MC/MCSectionMachO.h" #include "llvm/MC/MCStreamer.h" namespace llvm { @@ -40,10 +41,15 @@ private: AsmCond TheCondState; std::vector<AsmCond> TheCondStack; + // FIXME: Figure out where this should leave, the code is a copy of that which + // is also used by TargetLoweringObjectFile. + mutable void *SectionUniquingMap; + public: AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out) - : Lexer(_SM), Ctx(_Ctx), Out(_Out), TargetParser(0) {} - ~AsmParser() {} + : Lexer(_SM), Ctx(_Ctx), Out(_Out), TargetParser(0), + SectionUniquingMap(0) {} + ~AsmParser(); bool Run(); @@ -71,6 +77,13 @@ public: private: MCSymbol *CreateSymbol(StringRef Name); + // FIXME: See comment on SectionUniquingMap. + const MCSection *getMachOSection(const StringRef &Segment, + const StringRef &Section, + unsigned TypeAndAttributes, + unsigned Reserved2, + SectionKind Kind) const; + bool ParseStatement(); bool TokError(const char *Msg); diff --git a/tools/llvm-mc/llvm-mc.cpp b/tools/llvm-mc/llvm-mc.cpp index 27fb53b..a9c7fe1 100644 --- a/tools/llvm-mc/llvm-mc.cpp +++ b/tools/llvm-mc/llvm-mc.cpp @@ -248,12 +248,6 @@ static int AssembleInput(const char *ProgName) { Str.reset(createMachOStreamer(Ctx, *Out)); } - // FIXME: Target hook & command line option for initial section. - Str.get()->SwitchSection(MCSectionMachO::Create("__TEXT","__text", - MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, - 0, SectionKind::getText(), - Ctx)); - AsmParser Parser(SrcMgr, Ctx, *Str.get()); OwningPtr<TargetAsmParser> TAP(TheTarget->createAsmParser(Parser)); if (!TAP) { |