[WebAssembly] MC: Fix crash when -g specified.

At this point we don't output any debug sections or thier
relocations.

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

llvm-svn: 316240
This commit is contained in:
Sam Clegg 2017-10-20 21:28:38 +00:00
parent c420b61b26
commit 12fd3da9d1
9 changed files with 99 additions and 59 deletions

View File

@ -441,25 +441,25 @@ namespace llvm {
getAssociativeCOFFSection(MCSectionCOFF *Sec, const MCSymbol *KeySym,
unsigned UniqueID = GenericSectionID);
MCSectionWasm *getWasmSection(const Twine &Section, unsigned Type) {
return getWasmSection(Section, Type, nullptr);
MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K) {
return getWasmSection(Section, K, nullptr);
}
MCSectionWasm *getWasmSection(const Twine &Section, unsigned Type,
MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K,
const char *BeginSymName) {
return getWasmSection(Section, Type, "", ~0, BeginSymName);
return getWasmSection(Section, K, "", ~0, BeginSymName);
}
MCSectionWasm *getWasmSection(const Twine &Section, unsigned Type,
MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K,
const Twine &Group, unsigned UniqueID) {
return getWasmSection(Section, Type, Group, UniqueID, nullptr);
return getWasmSection(Section, K, Group, UniqueID, nullptr);
}
MCSectionWasm *getWasmSection(const Twine &Section, unsigned Type,
MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K,
const Twine &Group, unsigned UniqueID,
const char *BeginSymName);
MCSectionWasm *getWasmSection(const Twine &Section, unsigned Type,
MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K,
const MCSymbolWasm *Group, unsigned UniqueID,
const char *BeginSymName);

View File

@ -27,13 +27,11 @@ class MCSymbol;
/// This represents a section on wasm.
class MCSectionWasm final : public MCSection {
private:
/// This is the name of the section. The referenced memory is owned by
/// TargetLoweringObjectFileWasm's WasmUniqueMap.
StringRef SectionName;
/// This is the type of the section, from the enums in BinaryFormat/Wasm.h
unsigned Type;
unsigned UniqueID;
const MCSymbolWasm *Group;
@ -48,12 +46,10 @@ private:
uint64_t MemoryOffset;
friend class MCContext;
MCSectionWasm(StringRef Section, unsigned type, SectionKind K,
const MCSymbolWasm *group, unsigned UniqueID, MCSymbol *Begin)
: MCSection(SV_Wasm, K, Begin), SectionName(Section), Type(type),
UniqueID(UniqueID), Group(group), SectionOffset(0) {
assert(type == wasm::WASM_SEC_CODE || type == wasm::WASM_SEC_DATA);
}
MCSectionWasm(StringRef Section, SectionKind K, const MCSymbolWasm *group,
unsigned UniqueID, MCSymbol *Begin)
: MCSection(SV_Wasm, K, Begin), SectionName(Section), UniqueID(UniqueID),
Group(group), SectionOffset(0) {}
void setSectionName(StringRef Name) { SectionName = Name; }
@ -65,7 +61,6 @@ public:
bool ShouldOmitSectionDirective(StringRef Name, const MCAsmInfo &MAI) const;
StringRef getSectionName() const { return SectionName; }
unsigned getType() const { return Type; }
const MCSymbolWasm *getGroup() const { return Group; }
void PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
@ -74,6 +69,10 @@ public:
bool UseCodeAlign() const override;
bool isVirtualSection() const override;
bool isWasmData() const {
return Kind.isGlobalWriteableData() || Kind.isReadOnly();
}
bool isUnique() const { return UniqueID != ~0U; }
unsigned getUniqueID() const { return UniqueID; }

View File

@ -168,8 +168,7 @@ const MCExpr *TargetLoweringObjectFileELF::getTTypeGlobalReference(
MMI, Streamer);
}
static SectionKind
getELFKindForNamedSection(StringRef Name, SectionKind K) {
static SectionKind getELFKindForNamedSection(StringRef Name, SectionKind K) {
// N.B.: The defaults used in here are no the same ones used in MC.
// We follow gcc, MC follows gas. For example, given ".section .eh_frame",
// both gas and MC will produce a section with no flags. Given
@ -1249,7 +1248,7 @@ static const Comdat *getWasmComdat(const GlobalValue *GV) {
MCSection *TargetLoweringObjectFileWasm::getExplicitSectionGlobal(
const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
StringRef Name = GO->getSection();
return getContext().getWasmSection(Name, wasm::WASM_SEC_DATA);
return getContext().getWasmSection(Name, SectionKind::getData());
}
static MCSectionWasm *selectWasmSectionForGlobal(
@ -1262,12 +1261,10 @@ static MCSectionWasm *selectWasmSectionForGlobal(
bool UniqueSectionNames = TM.getUniqueSectionNames();
SmallString<128> Name = getSectionPrefixForGlobal(Kind);
uint32_t Type = wasm::WASM_SEC_DATA;
if (const auto *F = dyn_cast<Function>(GO)) {
const auto &OptionalPrefix = F->getSectionPrefix();
if (OptionalPrefix)
Name += *OptionalPrefix;
Type = wasm::WASM_SEC_CODE;
}
if (EmitUniqueSection && UniqueSectionNames) {
@ -1279,7 +1276,7 @@ static MCSectionWasm *selectWasmSectionForGlobal(
UniqueID = *NextUniqueID;
(*NextUniqueID)++;
}
return Ctx.getWasmSection(Name, Type, Group, UniqueID);
return Ctx.getWasmSection(Name, Kind, Group, UniqueID);
}
MCSection *TargetLoweringObjectFileWasm::SelectSectionForGlobal(
@ -1330,7 +1327,7 @@ const MCExpr *TargetLoweringObjectFileWasm::lowerRelativeReference(
void TargetLoweringObjectFileWasm::InitializeWasm() {
StaticCtorSection =
getContext().getWasmSection(".init_array", wasm::WASM_SEC_DATA);
getContext().getWasmSection(".init_array", SectionKind::getData());
StaticDtorSection =
getContext().getWasmSection(".fini_array", wasm::WASM_SEC_DATA);
getContext().getWasmSection(".fini_array", SectionKind::getData());
}

View File

@ -486,17 +486,17 @@ MCSectionCOFF *MCContext::getAssociativeCOFFSection(MCSectionCOFF *Sec,
"", 0, UniqueID);
}
MCSectionWasm *MCContext::getWasmSection(const Twine &Section, unsigned Type,
MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind K,
const Twine &Group, unsigned UniqueID,
const char *BeginSymName) {
MCSymbolWasm *GroupSym = nullptr;
if (!Group.isTriviallyEmpty() && !Group.str().empty())
GroupSym = cast<MCSymbolWasm>(getOrCreateSymbol(Group));
return getWasmSection(Section, Type, GroupSym, UniqueID, BeginSymName);
return getWasmSection(Section, K, GroupSym, UniqueID, BeginSymName);
}
MCSectionWasm *MCContext::getWasmSection(const Twine &Section, unsigned Type,
MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind Kind,
const MCSymbolWasm *GroupSym,
unsigned UniqueID,
const char *BeginSymName) {
@ -512,14 +512,12 @@ MCSectionWasm *MCContext::getWasmSection(const Twine &Section, unsigned Type,
StringRef CachedName = Entry.first.SectionName;
SectionKind Kind = SectionKind::getText();
MCSymbol *Begin = nullptr;
if (BeginSymName)
Begin = createTempSymbol(BeginSymName, false);
MCSectionWasm *Result = new (WasmAllocator.Allocate())
MCSectionWasm(CachedName, Type, Kind, GroupSym, UniqueID, Begin);
MCSectionWasm(CachedName, Kind, GroupSym, UniqueID, Begin);
Entry.second = Result;
return Result;
}

View File

@ -824,24 +824,24 @@ void MCObjectFileInfo::initCOFFMCObjectFileInfo(const Triple &T) {
void MCObjectFileInfo::initWasmMCObjectFileInfo(const Triple &T) {
// TODO: Set the section types and flags.
TextSection = Ctx->getWasmSection(".text", wasm::WASM_SEC_CODE);
DataSection = Ctx->getWasmSection(".data", wasm::WASM_SEC_DATA);
TextSection = Ctx->getWasmSection(".text", SectionKind::getText());
DataSection = Ctx->getWasmSection(".data", SectionKind::getData());
// TODO: Set the section types and flags.
DwarfLineSection = Ctx->getWasmSection(".debug_line", wasm::WASM_SEC_DATA);
DwarfStrSection = Ctx->getWasmSection(".debug_str", wasm::WASM_SEC_DATA);
DwarfLocSection = Ctx->getWasmSection(".debug_loc", wasm::WASM_SEC_DATA);
DwarfAbbrevSection = Ctx->getWasmSection(".debug_abbrev", wasm::WASM_SEC_DATA, "section_abbrev");
DwarfARangesSection = Ctx->getWasmSection(".debug_aranges", wasm::WASM_SEC_DATA);
DwarfRangesSection = Ctx->getWasmSection(".debug_ranges", wasm::WASM_SEC_DATA, "debug_range");
DwarfMacinfoSection = Ctx->getWasmSection(".debug_macinfo", wasm::WASM_SEC_DATA, "debug_macinfo");
DwarfAddrSection = Ctx->getWasmSection(".debug_addr", wasm::WASM_SEC_DATA);
DwarfCUIndexSection = Ctx->getWasmSection(".debug_cu_index", wasm::WASM_SEC_DATA);
DwarfTUIndexSection = Ctx->getWasmSection(".debug_tu_index", wasm::WASM_SEC_DATA);
DwarfInfoSection = Ctx->getWasmSection(".debug_info", wasm::WASM_SEC_DATA, "section_info");
DwarfFrameSection = Ctx->getWasmSection(".debug_frame", wasm::WASM_SEC_DATA);
DwarfPubNamesSection = Ctx->getWasmSection(".debug_pubnames", wasm::WASM_SEC_DATA);
DwarfPubTypesSection = Ctx->getWasmSection(".debug_pubtypes", wasm::WASM_SEC_DATA);
DwarfLineSection = Ctx->getWasmSection(".debug_line", SectionKind::getMetadata());
DwarfStrSection = Ctx->getWasmSection(".debug_str", SectionKind::getMetadata());
DwarfLocSection = Ctx->getWasmSection(".debug_loc", SectionKind::getMetadata());
DwarfAbbrevSection = Ctx->getWasmSection(".debug_abbrev", SectionKind::getMetadata(), "section_abbrev");
DwarfARangesSection = Ctx->getWasmSection(".debug_aranges", SectionKind::getMetadata());
DwarfRangesSection = Ctx->getWasmSection(".debug_ranges", SectionKind::getMetadata(), "debug_range");
DwarfMacinfoSection = Ctx->getWasmSection(".debug_macinfo", SectionKind::getMetadata(), "debug_macinfo");
DwarfAddrSection = Ctx->getWasmSection(".debug_addr", SectionKind::getMetadata());
DwarfCUIndexSection = Ctx->getWasmSection(".debug_cu_index", SectionKind::getMetadata());
DwarfTUIndexSection = Ctx->getWasmSection(".debug_tu_index", SectionKind::getMetadata());
DwarfInfoSection = Ctx->getWasmSection(".debug_info", SectionKind::getMetadata(), "section_info");
DwarfFrameSection = Ctx->getWasmSection(".debug_frame", SectionKind::getMetadata());
DwarfPubNamesSection = Ctx->getWasmSection(".debug_pubnames", SectionKind::getMetadata());
DwarfPubTypesSection = Ctx->getWasmSection(".debug_pubtypes", SectionKind::getMetadata());
// TODO: Define more sections.
}

View File

@ -157,7 +157,7 @@ void MCWasmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
void MCWasmStreamer::EmitIdent(StringRef IdentString) {
MCSection *Comment = getAssembler().getContext().getWasmSection(
".comment", wasm::WASM_SEC_DATA);
".comment", SectionKind::getMetadata());
PushSection();
SwitchSection(Comment);
if (!SeenIdent) {

View File

@ -437,10 +437,13 @@ void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
WasmRelocationEntry Rec(FixupOffset, SymA, C, Type, &FixupSection);
DEBUG(dbgs() << "WasmReloc: " << Rec << "\n");
if (FixupSection.hasInstructions())
CodeRelocations.push_back(Rec);
else
if (FixupSection.isWasmData())
DataRelocations.push_back(Rec);
else if (FixupSection.getKind().isText())
CodeRelocations.push_back(Rec);
else if (!FixupSection.getKind().isMetadata())
// TODO(sbc): Add support for debug sections.
llvm_unreachable("unexpected section type");
}
// Write X as an (unsigned) LEB value at offset Offset in Stream, padded
@ -1060,7 +1063,8 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm,
// In the special .global_variables section, we've encoded global
// variables used by the function. Translate them into the Globals
// list.
MCSectionWasm *GlobalVars = Ctx.getWasmSection(".global_variables", wasm::WASM_SEC_DATA);
MCSectionWasm *GlobalVars =
Ctx.getWasmSection(".global_variables", SectionKind::getMetadata());
if (!GlobalVars->getFragmentList().empty()) {
if (GlobalVars->getFragmentList().size() != 1)
report_fatal_error("only one .global_variables fragment supported");
@ -1116,7 +1120,8 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm,
// In the special .stack_pointer section, we've encoded the stack pointer
// index.
MCSectionWasm *StackPtr = Ctx.getWasmSection(".stack_pointer", wasm::WASM_SEC_DATA);
MCSectionWasm *StackPtr =
Ctx.getWasmSection(".stack_pointer", SectionKind::getMetadata());
if (!StackPtr->getFragmentList().empty()) {
if (StackPtr->getFragmentList().size() != 1)
report_fatal_error("only one .stack_pointer fragment supported");
@ -1135,7 +1140,7 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm,
for (MCSection &Sec : Asm) {
auto &Section = static_cast<MCSectionWasm &>(Sec);
if (Section.getType() != wasm::WASM_SEC_DATA)
if (!Section.isWasmData())
continue;
DataSize = alignTo(DataSize, Section.getAlignment());

View File

@ -219,8 +219,8 @@ void WebAssemblyTargetWasmStreamer::emitGlobal(
// section. This will later be decoded and turned into contents for the
// Globals Section.
Streamer.PushSection();
Streamer.SwitchSection(Streamer.getContext()
.getWasmSection(".global_variables", 0, 0));
Streamer.SwitchSection(Streamer.getContext().getWasmSection(
".global_variables", SectionKind::getMetadata()));
for (const wasm::Global &G : Globals) {
Streamer.EmitIntValue(int32_t(G.Type), 1);
Streamer.EmitIntValue(G.Mutable, 1);
@ -240,8 +240,8 @@ void WebAssemblyTargetWasmStreamer::emitGlobal(
void WebAssemblyTargetWasmStreamer::emitStackPointer(uint32_t Index) {
Streamer.PushSection();
Streamer.SwitchSection(Streamer.getContext()
.getWasmSection(".stack_pointer", 0, 0));
Streamer.SwitchSection(Streamer.getContext().getWasmSection(
".stack_pointer", SectionKind::getMetadata()));
Streamer.EmitIntValue(Index, 4);
Streamer.PopSection();
}

View File

@ -0,0 +1,41 @@
; RUN: llc -mtriple wasm32-unknown-unknown-wasm -filetype=obj %s -o - | llvm-readobj -r -s -expand-relocs
; Debug information is currently not supported. This test simply verifies that
; a valid object generated.
source_filename = "test.c"
@myextern = external global i32, align 4
@foo = hidden global i32* @myextern, align 4, !dbg !0
@ptr2 = hidden global void ()* @f2, align 4, !dbg !6
; Function Attrs: noinline nounwind optnone
define hidden void @f2() #0 !dbg !17 {
entry:
ret void, !dbg !18
}
attributes #0 = { noinline nounwind optnone "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="generic" "unsafe-fp-math"="false" "use-soft-float"="false" }
!llvm.dbg.cu = !{!2}
!llvm.module.flags = !{!13, !14, !15}
!llvm.ident = !{!16}
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
!1 = distinct !DIGlobalVariable(name: "foo", scope: !2, file: !3, line: 4, type: !11, isLocal: false, isDefinition: true)
!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 6.0.0 (trunk 315924) (llvm/trunk 315960)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5)
!3 = !DIFile(filename: "test.c", directory: "/usr/local/google/home/sbc/dev/wasm/simple")
!4 = !{}
!5 = !{!0, !6}
!6 = !DIGlobalVariableExpression(var: !7, expr: !DIExpression())
!7 = distinct !DIGlobalVariable(name: "ptr2", scope: !2, file: !3, line: 5, type: !8, isLocal: false, isDefinition: true)
!8 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !9, size: 32)
!9 = !DISubroutineType(types: !10)
!10 = !{null}
!11 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !12, size: 32)
!12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!13 = !{i32 2, !"Dwarf Version", i32 4}
!14 = !{i32 2, !"Debug Info Version", i32 3}
!15 = !{i32 1, !"wchar_size", i32 4}
!16 = !{!"clang version 6.0.0 (trunk 315924) (llvm/trunk 315960)"}
!17 = distinct !DISubprogram(name: "f2", scope: !3, file: !3, line: 2, type: !9, isLocal: false, isDefinition: true, scopeLine: 2, flags: DIFlagPrototyped, isOptimized: false, unit: !2, variables: !4)
!18 = !DILocation(line: 2, column: 16, scope: !17)