Skip to content

Commit 12fd3da

Browse files
committed
[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
1 parent c420b61 commit 12fd3da

File tree

9 files changed

+99
-59
lines changed

9 files changed

+99
-59
lines changed

Diff for: llvm/include/llvm/MC/MCContext.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -441,25 +441,25 @@ namespace llvm {
441441
getAssociativeCOFFSection(MCSectionCOFF *Sec, const MCSymbol *KeySym,
442442
unsigned UniqueID = GenericSectionID);
443443

444-
MCSectionWasm *getWasmSection(const Twine &Section, unsigned Type) {
445-
return getWasmSection(Section, Type, nullptr);
444+
MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K) {
445+
return getWasmSection(Section, K, nullptr);
446446
}
447447

448-
MCSectionWasm *getWasmSection(const Twine &Section, unsigned Type,
448+
MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K,
449449
const char *BeginSymName) {
450-
return getWasmSection(Section, Type, "", ~0, BeginSymName);
450+
return getWasmSection(Section, K, "", ~0, BeginSymName);
451451
}
452452

453-
MCSectionWasm *getWasmSection(const Twine &Section, unsigned Type,
453+
MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K,
454454
const Twine &Group, unsigned UniqueID) {
455-
return getWasmSection(Section, Type, Group, UniqueID, nullptr);
455+
return getWasmSection(Section, K, Group, UniqueID, nullptr);
456456
}
457457

458-
MCSectionWasm *getWasmSection(const Twine &Section, unsigned Type,
458+
MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K,
459459
const Twine &Group, unsigned UniqueID,
460460
const char *BeginSymName);
461461

462-
MCSectionWasm *getWasmSection(const Twine &Section, unsigned Type,
462+
MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K,
463463
const MCSymbolWasm *Group, unsigned UniqueID,
464464
const char *BeginSymName);
465465

Diff for: llvm/include/llvm/MC/MCSectionWasm.h

+9-10
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,11 @@ class MCSymbol;
2727
/// This represents a section on wasm.
2828
class MCSectionWasm final : public MCSection {
2929
private:
30+
3031
/// This is the name of the section. The referenced memory is owned by
3132
/// TargetLoweringObjectFileWasm's WasmUniqueMap.
3233
StringRef SectionName;
3334

34-
/// This is the type of the section, from the enums in BinaryFormat/Wasm.h
35-
unsigned Type;
36-
3735
unsigned UniqueID;
3836

3937
const MCSymbolWasm *Group;
@@ -48,12 +46,10 @@ class MCSectionWasm final : public MCSection {
4846
uint64_t MemoryOffset;
4947

5048
friend class MCContext;
51-
MCSectionWasm(StringRef Section, unsigned type, SectionKind K,
52-
const MCSymbolWasm *group, unsigned UniqueID, MCSymbol *Begin)
53-
: MCSection(SV_Wasm, K, Begin), SectionName(Section), Type(type),
54-
UniqueID(UniqueID), Group(group), SectionOffset(0) {
55-
assert(type == wasm::WASM_SEC_CODE || type == wasm::WASM_SEC_DATA);
56-
}
49+
MCSectionWasm(StringRef Section, SectionKind K, const MCSymbolWasm *group,
50+
unsigned UniqueID, MCSymbol *Begin)
51+
: MCSection(SV_Wasm, K, Begin), SectionName(Section), UniqueID(UniqueID),
52+
Group(group), SectionOffset(0) {}
5753

5854
void setSectionName(StringRef Name) { SectionName = Name; }
5955

@@ -65,7 +61,6 @@ class MCSectionWasm final : public MCSection {
6561
bool ShouldOmitSectionDirective(StringRef Name, const MCAsmInfo &MAI) const;
6662

6763
StringRef getSectionName() const { return SectionName; }
68-
unsigned getType() const { return Type; }
6964
const MCSymbolWasm *getGroup() const { return Group; }
7065

7166
void PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
@@ -74,6 +69,10 @@ class MCSectionWasm final : public MCSection {
7469
bool UseCodeAlign() const override;
7570
bool isVirtualSection() const override;
7671

72+
bool isWasmData() const {
73+
return Kind.isGlobalWriteableData() || Kind.isReadOnly();
74+
}
75+
7776
bool isUnique() const { return UniqueID != ~0U; }
7877
unsigned getUniqueID() const { return UniqueID; }
7978

Diff for: llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

+5-8
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,7 @@ const MCExpr *TargetLoweringObjectFileELF::getTTypeGlobalReference(
168168
MMI, Streamer);
169169
}
170170

171-
static SectionKind
172-
getELFKindForNamedSection(StringRef Name, SectionKind K) {
171+
static SectionKind getELFKindForNamedSection(StringRef Name, SectionKind K) {
173172
// N.B.: The defaults used in here are no the same ones used in MC.
174173
// We follow gcc, MC follows gas. For example, given ".section .eh_frame",
175174
// both gas and MC will produce a section with no flags. Given
@@ -1249,7 +1248,7 @@ static const Comdat *getWasmComdat(const GlobalValue *GV) {
12491248
MCSection *TargetLoweringObjectFileWasm::getExplicitSectionGlobal(
12501249
const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
12511250
StringRef Name = GO->getSection();
1252-
return getContext().getWasmSection(Name, wasm::WASM_SEC_DATA);
1251+
return getContext().getWasmSection(Name, SectionKind::getData());
12531252
}
12541253

12551254
static MCSectionWasm *selectWasmSectionForGlobal(
@@ -1262,12 +1261,10 @@ static MCSectionWasm *selectWasmSectionForGlobal(
12621261
bool UniqueSectionNames = TM.getUniqueSectionNames();
12631262
SmallString<128> Name = getSectionPrefixForGlobal(Kind);
12641263

1265-
uint32_t Type = wasm::WASM_SEC_DATA;
12661264
if (const auto *F = dyn_cast<Function>(GO)) {
12671265
const auto &OptionalPrefix = F->getSectionPrefix();
12681266
if (OptionalPrefix)
12691267
Name += *OptionalPrefix;
1270-
Type = wasm::WASM_SEC_CODE;
12711268
}
12721269

12731270
if (EmitUniqueSection && UniqueSectionNames) {
@@ -1279,7 +1276,7 @@ static MCSectionWasm *selectWasmSectionForGlobal(
12791276
UniqueID = *NextUniqueID;
12801277
(*NextUniqueID)++;
12811278
}
1282-
return Ctx.getWasmSection(Name, Type, Group, UniqueID);
1279+
return Ctx.getWasmSection(Name, Kind, Group, UniqueID);
12831280
}
12841281

12851282
MCSection *TargetLoweringObjectFileWasm::SelectSectionForGlobal(
@@ -1330,7 +1327,7 @@ const MCExpr *TargetLoweringObjectFileWasm::lowerRelativeReference(
13301327

13311328
void TargetLoweringObjectFileWasm::InitializeWasm() {
13321329
StaticCtorSection =
1333-
getContext().getWasmSection(".init_array", wasm::WASM_SEC_DATA);
1330+
getContext().getWasmSection(".init_array", SectionKind::getData());
13341331
StaticDtorSection =
1335-
getContext().getWasmSection(".fini_array", wasm::WASM_SEC_DATA);
1332+
getContext().getWasmSection(".fini_array", SectionKind::getData());
13361333
}

Diff for: llvm/lib/MC/MCContext.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -486,17 +486,17 @@ MCSectionCOFF *MCContext::getAssociativeCOFFSection(MCSectionCOFF *Sec,
486486
"", 0, UniqueID);
487487
}
488488

489-
MCSectionWasm *MCContext::getWasmSection(const Twine &Section, unsigned Type,
489+
MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind K,
490490
const Twine &Group, unsigned UniqueID,
491491
const char *BeginSymName) {
492492
MCSymbolWasm *GroupSym = nullptr;
493493
if (!Group.isTriviallyEmpty() && !Group.str().empty())
494494
GroupSym = cast<MCSymbolWasm>(getOrCreateSymbol(Group));
495495

496-
return getWasmSection(Section, Type, GroupSym, UniqueID, BeginSymName);
496+
return getWasmSection(Section, K, GroupSym, UniqueID, BeginSymName);
497497
}
498498

499-
MCSectionWasm *MCContext::getWasmSection(const Twine &Section, unsigned Type,
499+
MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind Kind,
500500
const MCSymbolWasm *GroupSym,
501501
unsigned UniqueID,
502502
const char *BeginSymName) {
@@ -512,14 +512,12 @@ MCSectionWasm *MCContext::getWasmSection(const Twine &Section, unsigned Type,
512512

513513
StringRef CachedName = Entry.first.SectionName;
514514

515-
SectionKind Kind = SectionKind::getText();
516-
517515
MCSymbol *Begin = nullptr;
518516
if (BeginSymName)
519517
Begin = createTempSymbol(BeginSymName, false);
520518

521519
MCSectionWasm *Result = new (WasmAllocator.Allocate())
522-
MCSectionWasm(CachedName, Type, Kind, GroupSym, UniqueID, Begin);
520+
MCSectionWasm(CachedName, Kind, GroupSym, UniqueID, Begin);
523521
Entry.second = Result;
524522
return Result;
525523
}

Diff for: llvm/lib/MC/MCObjectFileInfo.cpp

+16-16
Original file line numberDiff line numberDiff line change
@@ -824,24 +824,24 @@ void MCObjectFileInfo::initCOFFMCObjectFileInfo(const Triple &T) {
824824

825825
void MCObjectFileInfo::initWasmMCObjectFileInfo(const Triple &T) {
826826
// TODO: Set the section types and flags.
827-
TextSection = Ctx->getWasmSection(".text", wasm::WASM_SEC_CODE);
828-
DataSection = Ctx->getWasmSection(".data", wasm::WASM_SEC_DATA);
827+
TextSection = Ctx->getWasmSection(".text", SectionKind::getText());
828+
DataSection = Ctx->getWasmSection(".data", SectionKind::getData());
829829

830830
// TODO: Set the section types and flags.
831-
DwarfLineSection = Ctx->getWasmSection(".debug_line", wasm::WASM_SEC_DATA);
832-
DwarfStrSection = Ctx->getWasmSection(".debug_str", wasm::WASM_SEC_DATA);
833-
DwarfLocSection = Ctx->getWasmSection(".debug_loc", wasm::WASM_SEC_DATA);
834-
DwarfAbbrevSection = Ctx->getWasmSection(".debug_abbrev", wasm::WASM_SEC_DATA, "section_abbrev");
835-
DwarfARangesSection = Ctx->getWasmSection(".debug_aranges", wasm::WASM_SEC_DATA);
836-
DwarfRangesSection = Ctx->getWasmSection(".debug_ranges", wasm::WASM_SEC_DATA, "debug_range");
837-
DwarfMacinfoSection = Ctx->getWasmSection(".debug_macinfo", wasm::WASM_SEC_DATA, "debug_macinfo");
838-
DwarfAddrSection = Ctx->getWasmSection(".debug_addr", wasm::WASM_SEC_DATA);
839-
DwarfCUIndexSection = Ctx->getWasmSection(".debug_cu_index", wasm::WASM_SEC_DATA);
840-
DwarfTUIndexSection = Ctx->getWasmSection(".debug_tu_index", wasm::WASM_SEC_DATA);
841-
DwarfInfoSection = Ctx->getWasmSection(".debug_info", wasm::WASM_SEC_DATA, "section_info");
842-
DwarfFrameSection = Ctx->getWasmSection(".debug_frame", wasm::WASM_SEC_DATA);
843-
DwarfPubNamesSection = Ctx->getWasmSection(".debug_pubnames", wasm::WASM_SEC_DATA);
844-
DwarfPubTypesSection = Ctx->getWasmSection(".debug_pubtypes", wasm::WASM_SEC_DATA);
831+
DwarfLineSection = Ctx->getWasmSection(".debug_line", SectionKind::getMetadata());
832+
DwarfStrSection = Ctx->getWasmSection(".debug_str", SectionKind::getMetadata());
833+
DwarfLocSection = Ctx->getWasmSection(".debug_loc", SectionKind::getMetadata());
834+
DwarfAbbrevSection = Ctx->getWasmSection(".debug_abbrev", SectionKind::getMetadata(), "section_abbrev");
835+
DwarfARangesSection = Ctx->getWasmSection(".debug_aranges", SectionKind::getMetadata());
836+
DwarfRangesSection = Ctx->getWasmSection(".debug_ranges", SectionKind::getMetadata(), "debug_range");
837+
DwarfMacinfoSection = Ctx->getWasmSection(".debug_macinfo", SectionKind::getMetadata(), "debug_macinfo");
838+
DwarfAddrSection = Ctx->getWasmSection(".debug_addr", SectionKind::getMetadata());
839+
DwarfCUIndexSection = Ctx->getWasmSection(".debug_cu_index", SectionKind::getMetadata());
840+
DwarfTUIndexSection = Ctx->getWasmSection(".debug_tu_index", SectionKind::getMetadata());
841+
DwarfInfoSection = Ctx->getWasmSection(".debug_info", SectionKind::getMetadata(), "section_info");
842+
DwarfFrameSection = Ctx->getWasmSection(".debug_frame", SectionKind::getMetadata());
843+
DwarfPubNamesSection = Ctx->getWasmSection(".debug_pubnames", SectionKind::getMetadata());
844+
DwarfPubTypesSection = Ctx->getWasmSection(".debug_pubtypes", SectionKind::getMetadata());
845845

846846
// TODO: Define more sections.
847847
}

Diff for: llvm/lib/MC/MCWasmStreamer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ void MCWasmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
157157

158158
void MCWasmStreamer::EmitIdent(StringRef IdentString) {
159159
MCSection *Comment = getAssembler().getContext().getWasmSection(
160-
".comment", wasm::WASM_SEC_DATA);
160+
".comment", SectionKind::getMetadata());
161161
PushSection();
162162
SwitchSection(Comment);
163163
if (!SeenIdent) {

Diff for: llvm/lib/MC/WasmObjectWriter.cpp

+11-6
Original file line numberDiff line numberDiff line change
@@ -437,10 +437,13 @@ void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
437437
WasmRelocationEntry Rec(FixupOffset, SymA, C, Type, &FixupSection);
438438
DEBUG(dbgs() << "WasmReloc: " << Rec << "\n");
439439

440-
if (FixupSection.hasInstructions())
441-
CodeRelocations.push_back(Rec);
442-
else
440+
if (FixupSection.isWasmData())
443441
DataRelocations.push_back(Rec);
442+
else if (FixupSection.getKind().isText())
443+
CodeRelocations.push_back(Rec);
444+
else if (!FixupSection.getKind().isMetadata())
445+
// TODO(sbc): Add support for debug sections.
446+
llvm_unreachable("unexpected section type");
444447
}
445448

446449
// Write X as an (unsigned) LEB value at offset Offset in Stream, padded
@@ -1060,7 +1063,8 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm,
10601063
// In the special .global_variables section, we've encoded global
10611064
// variables used by the function. Translate them into the Globals
10621065
// list.
1063-
MCSectionWasm *GlobalVars = Ctx.getWasmSection(".global_variables", wasm::WASM_SEC_DATA);
1066+
MCSectionWasm *GlobalVars =
1067+
Ctx.getWasmSection(".global_variables", SectionKind::getMetadata());
10641068
if (!GlobalVars->getFragmentList().empty()) {
10651069
if (GlobalVars->getFragmentList().size() != 1)
10661070
report_fatal_error("only one .global_variables fragment supported");
@@ -1116,7 +1120,8 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm,
11161120

11171121
// In the special .stack_pointer section, we've encoded the stack pointer
11181122
// index.
1119-
MCSectionWasm *StackPtr = Ctx.getWasmSection(".stack_pointer", wasm::WASM_SEC_DATA);
1123+
MCSectionWasm *StackPtr =
1124+
Ctx.getWasmSection(".stack_pointer", SectionKind::getMetadata());
11201125
if (!StackPtr->getFragmentList().empty()) {
11211126
if (StackPtr->getFragmentList().size() != 1)
11221127
report_fatal_error("only one .stack_pointer fragment supported");
@@ -1135,7 +1140,7 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm,
11351140

11361141
for (MCSection &Sec : Asm) {
11371142
auto &Section = static_cast<MCSectionWasm &>(Sec);
1138-
if (Section.getType() != wasm::WASM_SEC_DATA)
1143+
if (!Section.isWasmData())
11391144
continue;
11401145

11411146
DataSize = alignTo(DataSize, Section.getAlignment());

Diff for: llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ void WebAssemblyTargetWasmStreamer::emitGlobal(
219219
// section. This will later be decoded and turned into contents for the
220220
// Globals Section.
221221
Streamer.PushSection();
222-
Streamer.SwitchSection(Streamer.getContext()
223-
.getWasmSection(".global_variables", 0, 0));
222+
Streamer.SwitchSection(Streamer.getContext().getWasmSection(
223+
".global_variables", SectionKind::getMetadata()));
224224
for (const wasm::Global &G : Globals) {
225225
Streamer.EmitIntValue(int32_t(G.Type), 1);
226226
Streamer.EmitIntValue(G.Mutable, 1);
@@ -240,8 +240,8 @@ void WebAssemblyTargetWasmStreamer::emitGlobal(
240240

241241
void WebAssemblyTargetWasmStreamer::emitStackPointer(uint32_t Index) {
242242
Streamer.PushSection();
243-
Streamer.SwitchSection(Streamer.getContext()
244-
.getWasmSection(".stack_pointer", 0, 0));
243+
Streamer.SwitchSection(Streamer.getContext().getWasmSection(
244+
".stack_pointer", SectionKind::getMetadata()));
245245
Streamer.EmitIntValue(Index, 4);
246246
Streamer.PopSection();
247247
}

Diff for: llvm/test/MC/WebAssembly/debug-info.ll

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
; RUN: llc -mtriple wasm32-unknown-unknown-wasm -filetype=obj %s -o - | llvm-readobj -r -s -expand-relocs
2+
3+
; Debug information is currently not supported. This test simply verifies that
4+
; a valid object generated.
5+
source_filename = "test.c"
6+
7+
@myextern = external global i32, align 4
8+
@foo = hidden global i32* @myextern, align 4, !dbg !0
9+
@ptr2 = hidden global void ()* @f2, align 4, !dbg !6
10+
11+
; Function Attrs: noinline nounwind optnone
12+
define hidden void @f2() #0 !dbg !17 {
13+
entry:
14+
ret void, !dbg !18
15+
}
16+
17+
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" }
18+
19+
!llvm.dbg.cu = !{!2}
20+
!llvm.module.flags = !{!13, !14, !15}
21+
!llvm.ident = !{!16}
22+
23+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
24+
!1 = distinct !DIGlobalVariable(name: "foo", scope: !2, file: !3, line: 4, type: !11, isLocal: false, isDefinition: true)
25+
!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)
26+
!3 = !DIFile(filename: "test.c", directory: "/usr/local/google/home/sbc/dev/wasm/simple")
27+
!4 = !{}
28+
!5 = !{!0, !6}
29+
!6 = !DIGlobalVariableExpression(var: !7, expr: !DIExpression())
30+
!7 = distinct !DIGlobalVariable(name: "ptr2", scope: !2, file: !3, line: 5, type: !8, isLocal: false, isDefinition: true)
31+
!8 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !9, size: 32)
32+
!9 = !DISubroutineType(types: !10)
33+
!10 = !{null}
34+
!11 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !12, size: 32)
35+
!12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
36+
!13 = !{i32 2, !"Dwarf Version", i32 4}
37+
!14 = !{i32 2, !"Debug Info Version", i32 3}
38+
!15 = !{i32 1, !"wchar_size", i32 4}
39+
!16 = !{!"clang version 6.0.0 (trunk 315924) (llvm/trunk 315960)"}
40+
!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)
41+
!18 = !DILocation(line: 2, column: 16, scope: !17)

0 commit comments

Comments
 (0)