From 356b85edd78311ae7376f4b271c48b9f0156aeea Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Wed, 19 May 2021 13:33:24 -0700 Subject: [PATCH] [lld][WebAssembly] Fix for string tail merging and -r/--relocatable Ensure that both SyntheticMergedChunk and all MergeInfoChunks that it comprises are assigned the correct output section. Without this we would crash when outputting relocations in --relocatable mode. Fixes: https://github.com/emscripten-core/emscripten/issues/14220 Differential Revision: https://reviews.llvm.org/D102806 --- lld/test/wasm/merge-string-debug.s | 10 ++++++++++ lld/wasm/OutputSections.cpp | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lld/test/wasm/merge-string-debug.s b/lld/test/wasm/merge-string-debug.s index 4a663501959f..961cdabb0c85 100644 --- a/lld/test/wasm/merge-string-debug.s +++ b/lld/test/wasm/merge-string-debug.s @@ -4,13 +4,23 @@ # RUN: wasm-ld %t.o %t2.o -o %t.wasm --no-entry # RUN: llvm-readobj -x .debug_str %t.wasm | FileCheck %s --check-prefixes CHECK,CHECK-O1 +# Check that we -r/--reclocatable can handle string merging too +# RUN: wasm-ld --relocatable %t.o %t2.o -o %t3.o +# RUN: wasm-ld -O1 %t3.o -o %t.wasm --no-entry +# RUN: llvm-readobj -x .debug_str %t.wasm | FileCheck %s --check-prefixes CHECK,CHECK-O1 + # RUN: wasm-ld -O0 %t.o %t2.o -o %tO0.wasm --no-entry # RUN: llvm-readobj -x .debug_str %tO0.wasm | FileCheck %s --check-prefixes CHECK,CHECK-O0 .section .debug_str,"S",@ +.Linfo_string0: .asciz "clang version 13.0.0" +.Linfo_string1: .asciz "foobar" +.section .debug_other,"",@ + .int32 .Linfo_string0 + # CHECK: Hex dump of section '.debug_str': # CHECK-O0: 0x00000000 636c616e 67207665 7273696f 6e203133 clang version 13 diff --git a/lld/wasm/OutputSections.cpp b/lld/wasm/OutputSections.cpp index e39c3331641a..28f611712536 100644 --- a/lld/wasm/OutputSections.cpp +++ b/lld/wasm/OutputSections.cpp @@ -241,6 +241,7 @@ void CustomSection::finalizeInputSections() { std::vector newSections; for (InputChunk *s : inputSections) { + s->outputSec = this; MergeInputChunk *ms = dyn_cast(s); if (!ms) { newSections.push_back(s); @@ -251,6 +252,7 @@ void CustomSection::finalizeInputSections() { mergedSection = make(name, 0, WASM_SEG_FLAG_STRINGS); newSections.push_back(mergedSection); + mergedSection->outputSec = this; } mergedSection->addMergeChunk(ms); } @@ -272,7 +274,6 @@ void CustomSection::finalizeContents() { for (InputChunk *section : inputSections) { assert(!section->discarded); - section->outputSec = this; section->outSecOff = payloadSize; payloadSize += section->getSize(); }