Skip to content

Commit

Permalink
[WebAssembly] MC: Ensure that FUNCTION_OFFSET relocations are always …
Browse files Browse the repository at this point in the history
…against function symbols.

The getAtom() method wasn't doing what we needed in all cases. We want
the symbols for the function which defines that section. We can compute
this easily enough and we know that we have at most one function in each
section.

Once this lands I will revert rL331412 which is no longer needed.

Fixes PR37409

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

llvm-svn: 332517
  • Loading branch information
sbc100 committed May 16, 2018
1 parent 35d3398 commit 6ccb59b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
18 changes: 17 additions & 1 deletion llvm/lib/MC/WasmObjectWriter.cpp
Expand Up @@ -225,6 +225,9 @@ class WasmObjectWriter : public MCObjectWriter {
DenseMap<const MCSectionWasm *, std::vector<WasmRelocationEntry>>
CustomSectionsRelocations;

// Map from section to fintining function.
DenseMap<const MCSection *, const MCSymbol *> SectionFunctions;

DenseMap<WasmFunctionType, int32_t, WasmFunctionTypeDenseMapInfo>
FunctionTypeIndices;
SmallVector<WasmFunctionType, 4> FunctionTypes;
Expand Down Expand Up @@ -265,6 +268,7 @@ class WasmObjectWriter : public MCObjectWriter {
FunctionTypes.clear();
Globals.clear();
DataSegments.clear();
SectionFunctions.clear();
MCObjectWriter::reset();
NumFunctionImports = 0;
NumGlobalImports = 0;
Expand Down Expand Up @@ -383,6 +387,18 @@ void WasmObjectWriter::writeHeader(const MCAssembler &Asm) {

void WasmObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
const MCAsmLayout &Layout) {
// Build a map of sections to the function that defines them, for use
// in recordRelocation.
for (const MCSymbol &S : Asm.symbols()) {
const auto &WS = static_cast<const MCSymbolWasm &>(S);
if (WS.isDefined() && WS.isFunction() && !WS.isVariable()) {
const auto &Sec = static_cast<const MCSectionWasm &>(S.getSection());
auto Pair = SectionFunctions.insert(std::make_pair(&Sec, &S));
if (!Pair.second)
report_fatal_error("section already has a defining function: " +
Sec.getSectionName());
}
}
}

void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
Expand Down Expand Up @@ -475,7 +491,7 @@ void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
const MCSymbol *SectionSymbol = nullptr;
const MCSection &SecA = SymA->getSection();
if (SecA.getKind().isText())
SectionSymbol = SecA.begin()->getAtom();
SectionSymbol = SectionFunctions.find(&SecA)->second;
else
SectionSymbol = SecA.getBeginSymbol();
if (!SectionSymbol)
Expand Down
13 changes: 10 additions & 3 deletions llvm/test/MC/WebAssembly/debug-info.ll
Expand Up @@ -204,6 +204,13 @@
; CHECK-NEXT: }
; CHECK-NEXT:]

; generated from the following C code using: clang --target=wasm32 -g -O0 -S -emit-llvm test.c
; extern int myextern;
; void f2(void) { return; }
;
; int* foo = &myextern;
; void (*ptr2)(void) = f2;

target triple = "wasm32-unknown-unknown"

source_filename = "test.c"
Expand All @@ -226,7 +233,7 @@ attributes #0 = { noinline nounwind optnone "correctly-rounded-divide-sqrt-fp-ma

!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)
!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 7.0.0 (trunk 332303) (llvm/trunk 332406)", 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}
Expand All @@ -240,6 +247,6 @@ attributes #0 = { noinline nounwind optnone "correctly-rounded-divide-sqrt-fp-ma
!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)"}
!16 = !{!"clang version 7.0.0 (trunk 332303) (llvm/trunk 332406)"}
!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, retainedNodes: !4)
!18 = !DILocation(line: 2, column: 16, scope: !17)
!18 = !DILocation(line: 2, column: 17, scope: !17)

0 comments on commit 6ccb59b

Please sign in to comment.