[WebAssembly] Fix --export of LTO symbols

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

llvm-svn: 335881
This commit is contained in:
Sam Clegg 2018-06-28 17:21:46 +00:00
parent 5ce0a08cf6
commit 52b2456a95
4 changed files with 53 additions and 13 deletions

View File

@ -0,0 +1,38 @@
; RUN: llvm-as -o %t.bc %s
; RUN: not wasm-ld --export=missing -o %t.wasm %t.bc 2>&1 | FileCheck -check-prefix=CHECK-ERROR %s
; RUN: wasm-ld --export=hidden_function -o %t.wasm %t.bc
; RUN: obj2yaml %t.wasm | FileCheck %s
target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
target triple = "wasm32-unknown-unknown"
define hidden i32 @hidden_function() local_unnamed_addr {
entry:
ret i32 0
}
define void @_start() local_unnamed_addr {
entry:
ret void
}
; CHECK-ERROR: error: symbol exported via --export not found: missing
; CHECK: - Type: EXPORT
; CHECK-NEXT: Exports:
; CHECK-NEXT: - Name: memory
; CHECK-NEXT: Kind: MEMORY
; CHECK-NEXT: Index: 0
; CHECK-NEXT: - Name: __heap_base
; CHECK-NEXT: Kind: GLOBAL
; CHECK-NEXT: Index: 1
; CHECK-NEXT: - Name: __data_end
; CHECK-NEXT: Kind: GLOBAL
; CHECK-NEXT: Index: 2
; CHECK-NEXT: - Name: _start
; CHECK-NEXT: Kind: FUNCTION
; CHECK-NEXT: Index: 2
; CHECK-NEXT: - Name: hidden_function
; CHECK-NEXT: Kind: FUNCTION
; CHECK-NEXT: Index: 1
; CHECK-NEXT: - Type: CODE

View File

@ -439,6 +439,16 @@ void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
if (!Config->Relocatable) if (!Config->Relocatable)
handleWeakUndefines(); handleWeakUndefines();
// Handle --export.
for (auto *Arg : Args.filtered(OPT_export)) {
StringRef Name = Arg->getValue();
Symbol *Sym = Symtab->find(Name);
if (Sym && Sym->isDefined())
Sym->ForceExport = true;
else if (!Config->AllowUndefined)
error("symbol exported via --export not found: " + Name);
}
// Do link-time optimization if given files are LLVM bitcode files. // Do link-time optimization if given files are LLVM bitcode files.
// This compiles bitcode files into real object files. // This compiles bitcode files into real object files.
Symtab->addCombinedLTOObject(); Symtab->addCombinedLTOObject();
@ -466,16 +476,6 @@ void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
if (errorCount()) if (errorCount())
return; return;
// Handle --export.
for (auto *Arg : Args.filtered(OPT_export)) {
StringRef Name = Arg->getValue();
Symbol *Sym = Symtab->find(Name);
if (Sym && Sym->isDefined())
Sym->setHidden(false);
else if (!Config->AllowUndefined)
error("symbol exported via --export not found: " + Name);
}
if (EntrySym) if (EntrySym)
EntrySym->setHidden(false); EntrySym->setHidden(false);

View File

@ -102,7 +102,7 @@ bool Symbol::isExported() const {
if (!isDefined() || isLocal()) if (!isDefined() || isLocal())
return false; return false;
if (Config->ExportAll) if (ForceExport || Config->ExportAll)
return true; return true;
return !isHidden(); return !isHidden();

View File

@ -94,11 +94,12 @@ public:
// True if this symbol was referenced by a regular (non-bitcode) object. // True if this symbol was referenced by a regular (non-bitcode) object.
unsigned IsUsedInRegularObj : 1; unsigned IsUsedInRegularObj : 1;
unsigned ForceExport : 1;
protected: protected:
Symbol(StringRef Name, Kind K, uint32_t Flags, InputFile *F) Symbol(StringRef Name, Kind K, uint32_t Flags, InputFile *F)
: IsUsedInRegularObj(false), Name(Name), SymbolKind(K), Flags(Flags), : IsUsedInRegularObj(false), ForceExport(false), Name(Name),
File(F), Referenced(!Config->GcSections) {} SymbolKind(K), Flags(Flags), File(F), Referenced(!Config->GcSections) {}
StringRef Name; StringRef Name;
Kind SymbolKind; Kind SymbolKind;
@ -339,6 +340,7 @@ T *replaceSymbol(Symbol *S, ArgT &&... Arg) {
T *S2 = new (S) T(std::forward<ArgT>(Arg)...); T *S2 = new (S) T(std::forward<ArgT>(Arg)...);
S2->IsUsedInRegularObj = SymCopy.IsUsedInRegularObj; S2->IsUsedInRegularObj = SymCopy.IsUsedInRegularObj;
S2->ForceExport = SymCopy.ForceExport;
return S2; return S2;
} }