2017-04-25 01:21:38 +02:00
|
|
|
//===- ModuleSymbolTable.cpp - symbol table for in-memory IR --------------===//
|
2016-12-01 07:51:47 +01:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This class represents a symbol table built from in-memory IR. It provides
|
|
|
|
// access to GlobalValues and should only be used if such access is required
|
|
|
|
// (e.g. in the LTO implementation).
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-06 13:49:48 +02:00
|
|
|
#include "llvm/Object/ModuleSymbolTable.h"
|
2016-12-01 07:51:47 +01:00
|
|
|
#include "RecordStreamer.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2017-06-06 13:49:48 +02:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2017-04-25 01:21:38 +02:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/Triple.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/GlobalAlias.h"
|
|
|
|
#include "llvm/IR/GlobalValue.h"
|
|
|
|
#include "llvm/IR/GlobalVariable.h"
|
2016-12-01 07:51:47 +01:00
|
|
|
#include "llvm/IR/Mangler.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
2017-04-25 01:21:38 +02:00
|
|
|
#include "llvm/MC/MCDirectives.h"
|
2016-12-01 07:51:47 +01:00
|
|
|
#include "llvm/MC/MCInstrInfo.h"
|
|
|
|
#include "llvm/MC/MCObjectFileInfo.h"
|
|
|
|
#include "llvm/MC/MCParser/MCAsmParser.h"
|
|
|
|
#include "llvm/MC/MCParser/MCTargetAsmParser.h"
|
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
|
|
|
#include "llvm/MC/MCSubtargetInfo.h"
|
2017-04-25 01:21:38 +02:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
|
|
|
#include "llvm/MC/MCTargetOptions.h"
|
|
|
|
#include "llvm/Object/SymbolicFile.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/CodeGen.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2016-12-01 07:51:47 +01:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2017-04-25 01:21:38 +02:00
|
|
|
#include "llvm/Support/SMLoc.h"
|
2016-12-01 07:51:47 +01:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2017-06-06 13:49:48 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-04-25 01:21:38 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
|
2016-12-01 07:51:47 +01:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace object;
|
|
|
|
|
|
|
|
void ModuleSymbolTable::addModule(Module *M) {
|
|
|
|
if (FirstMod)
|
|
|
|
assert(FirstMod->getTargetTriple() == M->getTargetTriple());
|
|
|
|
else
|
|
|
|
FirstMod = M;
|
|
|
|
|
2017-03-29 21:26:26 +02:00
|
|
|
for (GlobalValue &GV : M->global_values())
|
2016-12-01 07:51:47 +01:00
|
|
|
SymTab.push_back(&GV);
|
|
|
|
|
Perform symbol binding for .symver versioned symbols
Summary:
In a .symver assembler directive like:
.symver name, name2@@nodename
"name2@@nodename" should get the same symbol binding as "name".
While the ELF object writer is updating the symbol binding for .symver
aliases before emitting the object file, not doing so when the module
inline assembly is handled by the RecordStreamer is causing the wrong
behavior in *LTO mode.
E.g. when "name" is global, "name2@@nodename" must also be marked as
global. Otherwise, the symbol is skipped when iterating over the LTO
InputFile symbols (InputFile::Symbol::shouldSkip). So, for example,
when performing any *LTO via the gold-plugin, the versioned symbol
definition is not recorded by the plugin and passed back to the
linker. If the object was in an archive, and there were no other symbols
needed from that object, the object would not be included in the final
link and references to the versioned symbol are undefined.
The llvm-lto2 tests added will give an error about an unused symbol
resolution without the fix.
Reviewers: rafael, pcc
Reviewed By: pcc
Subscribers: mehdi_amini, llvm-commits
Differential Revision: https://reviews.llvm.org/D30485
llvm-svn: 297332
2017-03-09 01:19:49 +01:00
|
|
|
CollectAsmSymbols(*M, [this](StringRef Name, BasicSymbolRef::Flags Flags) {
|
|
|
|
SymTab.push_back(new (AsmSymbols.Allocate()) AsmSymbol(Name, Flags));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure ELF .symver aliases get the same binding as the defined symbol
|
|
|
|
// they alias with.
|
|
|
|
static void handleSymverAliases(const Module &M, RecordStreamer &Streamer) {
|
|
|
|
if (Streamer.symverAliases().empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// The name in the assembler will be mangled, but the name in the IR
|
|
|
|
// might not, so we first compute a mapping from mangled name to GV.
|
|
|
|
Mangler Mang;
|
|
|
|
SmallString<64> MangledName;
|
|
|
|
StringMap<const GlobalValue *> MangledNameMap;
|
|
|
|
auto GetMangledName = [&](const GlobalValue &GV) {
|
|
|
|
if (!GV.hasName())
|
|
|
|
return;
|
|
|
|
|
|
|
|
MangledName.clear();
|
|
|
|
MangledName.reserve(GV.getName().size() + 1);
|
|
|
|
Mang.getNameWithPrefix(MangledName, &GV, /*CannotUsePrivateLabel=*/false);
|
|
|
|
MangledNameMap[MangledName] = &GV;
|
|
|
|
};
|
|
|
|
for (const Function &F : M)
|
|
|
|
GetMangledName(F);
|
|
|
|
for (const GlobalVariable &GV : M.globals())
|
|
|
|
GetMangledName(GV);
|
|
|
|
for (const GlobalAlias &GA : M.aliases())
|
|
|
|
GetMangledName(GA);
|
|
|
|
|
|
|
|
// Walk all the recorded .symver aliases, and set up the binding
|
|
|
|
// for each alias.
|
|
|
|
for (auto &Symver : Streamer.symverAliases()) {
|
|
|
|
const MCSymbol *Aliasee = Symver.first;
|
|
|
|
MCSymbolAttr Attr = MCSA_Invalid;
|
|
|
|
|
|
|
|
// First check if the aliasee binding was recorded in the asm.
|
|
|
|
RecordStreamer::State state = Streamer.getSymbolState(Aliasee);
|
|
|
|
switch (state) {
|
|
|
|
case RecordStreamer::Global:
|
|
|
|
case RecordStreamer::DefinedGlobal:
|
|
|
|
Attr = MCSA_Global;
|
|
|
|
break;
|
|
|
|
case RecordStreamer::UndefinedWeak:
|
|
|
|
case RecordStreamer::DefinedWeak:
|
|
|
|
Attr = MCSA_Weak;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't have a symbol attribute from assembly, then check if
|
|
|
|
// the aliasee was defined in the IR.
|
|
|
|
if (Attr == MCSA_Invalid) {
|
|
|
|
const auto *GV = M.getNamedValue(Aliasee->getName());
|
|
|
|
if (!GV) {
|
|
|
|
auto MI = MangledNameMap.find(Aliasee->getName());
|
|
|
|
if (MI != MangledNameMap.end())
|
|
|
|
GV = MI->second;
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (GV->hasExternalLinkage())
|
|
|
|
Attr = MCSA_Global;
|
|
|
|
else if (GV->hasLocalLinkage())
|
|
|
|
Attr = MCSA_Local;
|
|
|
|
else if (GV->isWeakForLinker())
|
|
|
|
Attr = MCSA_Weak;
|
|
|
|
}
|
|
|
|
if (Attr == MCSA_Invalid)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Set the detected binding on each alias with this aliasee.
|
|
|
|
for (auto &Alias : Symver.second)
|
|
|
|
Streamer.EmitSymbolAttribute(Alias, Attr);
|
|
|
|
}
|
2016-12-01 07:51:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleSymbolTable::CollectAsmSymbols(
|
Perform symbol binding for .symver versioned symbols
Summary:
In a .symver assembler directive like:
.symver name, name2@@nodename
"name2@@nodename" should get the same symbol binding as "name".
While the ELF object writer is updating the symbol binding for .symver
aliases before emitting the object file, not doing so when the module
inline assembly is handled by the RecordStreamer is causing the wrong
behavior in *LTO mode.
E.g. when "name" is global, "name2@@nodename" must also be marked as
global. Otherwise, the symbol is skipped when iterating over the LTO
InputFile symbols (InputFile::Symbol::shouldSkip). So, for example,
when performing any *LTO via the gold-plugin, the versioned symbol
definition is not recorded by the plugin and passed back to the
linker. If the object was in an archive, and there were no other symbols
needed from that object, the object would not be included in the final
link and references to the versioned symbol are undefined.
The llvm-lto2 tests added will give an error about an unused symbol
resolution without the fix.
Reviewers: rafael, pcc
Reviewed By: pcc
Subscribers: mehdi_amini, llvm-commits
Differential Revision: https://reviews.llvm.org/D30485
llvm-svn: 297332
2017-03-09 01:19:49 +01:00
|
|
|
const Module &M,
|
2016-12-01 07:51:47 +01:00
|
|
|
function_ref<void(StringRef, BasicSymbolRef::Flags)> AsmSymbol) {
|
Perform symbol binding for .symver versioned symbols
Summary:
In a .symver assembler directive like:
.symver name, name2@@nodename
"name2@@nodename" should get the same symbol binding as "name".
While the ELF object writer is updating the symbol binding for .symver
aliases before emitting the object file, not doing so when the module
inline assembly is handled by the RecordStreamer is causing the wrong
behavior in *LTO mode.
E.g. when "name" is global, "name2@@nodename" must also be marked as
global. Otherwise, the symbol is skipped when iterating over the LTO
InputFile symbols (InputFile::Symbol::shouldSkip). So, for example,
when performing any *LTO via the gold-plugin, the versioned symbol
definition is not recorded by the plugin and passed back to the
linker. If the object was in an archive, and there were no other symbols
needed from that object, the object would not be included in the final
link and references to the versioned symbol are undefined.
The llvm-lto2 tests added will give an error about an unused symbol
resolution without the fix.
Reviewers: rafael, pcc
Reviewed By: pcc
Subscribers: mehdi_amini, llvm-commits
Differential Revision: https://reviews.llvm.org/D30485
llvm-svn: 297332
2017-03-09 01:19:49 +01:00
|
|
|
StringRef InlineAsm = M.getModuleInlineAsm();
|
2016-12-01 07:51:47 +01:00
|
|
|
if (InlineAsm.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::string Err;
|
Perform symbol binding for .symver versioned symbols
Summary:
In a .symver assembler directive like:
.symver name, name2@@nodename
"name2@@nodename" should get the same symbol binding as "name".
While the ELF object writer is updating the symbol binding for .symver
aliases before emitting the object file, not doing so when the module
inline assembly is handled by the RecordStreamer is causing the wrong
behavior in *LTO mode.
E.g. when "name" is global, "name2@@nodename" must also be marked as
global. Otherwise, the symbol is skipped when iterating over the LTO
InputFile symbols (InputFile::Symbol::shouldSkip). So, for example,
when performing any *LTO via the gold-plugin, the versioned symbol
definition is not recorded by the plugin and passed back to the
linker. If the object was in an archive, and there were no other symbols
needed from that object, the object would not be included in the final
link and references to the versioned symbol are undefined.
The llvm-lto2 tests added will give an error about an unused symbol
resolution without the fix.
Reviewers: rafael, pcc
Reviewed By: pcc
Subscribers: mehdi_amini, llvm-commits
Differential Revision: https://reviews.llvm.org/D30485
llvm-svn: 297332
2017-03-09 01:19:49 +01:00
|
|
|
const Triple TT(M.getTargetTriple());
|
2016-12-01 07:51:47 +01:00
|
|
|
const Target *T = TargetRegistry::lookupTarget(TT.str(), Err);
|
|
|
|
assert(T && T->hasMCAsmParser());
|
|
|
|
|
|
|
|
std::unique_ptr<MCRegisterInfo> MRI(T->createMCRegInfo(TT.str()));
|
|
|
|
if (!MRI)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::unique_ptr<MCAsmInfo> MAI(T->createMCAsmInfo(*MRI, TT.str()));
|
|
|
|
if (!MAI)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::unique_ptr<MCSubtargetInfo> STI(
|
|
|
|
T->createMCSubtargetInfo(TT.str(), "", ""));
|
|
|
|
if (!STI)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::unique_ptr<MCInstrInfo> MCII(T->createMCInstrInfo());
|
|
|
|
if (!MCII)
|
|
|
|
return;
|
|
|
|
|
|
|
|
MCObjectFileInfo MOFI;
|
|
|
|
MCContext MCCtx(MAI.get(), MRI.get(), &MOFI);
|
2017-08-02 22:32:26 +02:00
|
|
|
MOFI.InitMCObjectFileInfo(TT, /*PIC*/ false, MCCtx);
|
2016-12-01 07:51:47 +01:00
|
|
|
RecordStreamer Streamer(MCCtx);
|
|
|
|
T->createNullTargetStreamer(Streamer);
|
|
|
|
|
|
|
|
std::unique_ptr<MemoryBuffer> Buffer(MemoryBuffer::getMemBuffer(InlineAsm));
|
|
|
|
SourceMgr SrcMgr;
|
|
|
|
SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
|
|
|
|
std::unique_ptr<MCAsmParser> Parser(
|
|
|
|
createMCAsmParser(SrcMgr, MCCtx, Streamer, *MAI));
|
|
|
|
|
|
|
|
MCTargetOptions MCOptions;
|
|
|
|
std::unique_ptr<MCTargetAsmParser> TAP(
|
|
|
|
T->createMCAsmParser(*STI, *Parser, *MCII, MCOptions));
|
|
|
|
if (!TAP)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Parser->setTargetParser(*TAP);
|
|
|
|
if (Parser->Run(false))
|
|
|
|
return;
|
|
|
|
|
Perform symbol binding for .symver versioned symbols
Summary:
In a .symver assembler directive like:
.symver name, name2@@nodename
"name2@@nodename" should get the same symbol binding as "name".
While the ELF object writer is updating the symbol binding for .symver
aliases before emitting the object file, not doing so when the module
inline assembly is handled by the RecordStreamer is causing the wrong
behavior in *LTO mode.
E.g. when "name" is global, "name2@@nodename" must also be marked as
global. Otherwise, the symbol is skipped when iterating over the LTO
InputFile symbols (InputFile::Symbol::shouldSkip). So, for example,
when performing any *LTO via the gold-plugin, the versioned symbol
definition is not recorded by the plugin and passed back to the
linker. If the object was in an archive, and there were no other symbols
needed from that object, the object would not be included in the final
link and references to the versioned symbol are undefined.
The llvm-lto2 tests added will give an error about an unused symbol
resolution without the fix.
Reviewers: rafael, pcc
Reviewed By: pcc
Subscribers: mehdi_amini, llvm-commits
Differential Revision: https://reviews.llvm.org/D30485
llvm-svn: 297332
2017-03-09 01:19:49 +01:00
|
|
|
handleSymverAliases(M, Streamer);
|
|
|
|
|
2016-12-01 07:51:47 +01:00
|
|
|
for (auto &KV : Streamer) {
|
|
|
|
StringRef Key = KV.first();
|
|
|
|
RecordStreamer::State Value = KV.second;
|
2016-12-01 07:53:47 +01:00
|
|
|
// FIXME: For now we just assume that all asm symbols are executable.
|
|
|
|
uint32_t Res = BasicSymbolRef::SF_Executable;
|
2016-12-01 07:51:47 +01:00
|
|
|
switch (Value) {
|
|
|
|
case RecordStreamer::NeverSeen:
|
|
|
|
llvm_unreachable("NeverSeen should have been replaced earlier");
|
|
|
|
case RecordStreamer::DefinedGlobal:
|
|
|
|
Res |= BasicSymbolRef::SF_Global;
|
|
|
|
break;
|
|
|
|
case RecordStreamer::Defined:
|
|
|
|
break;
|
|
|
|
case RecordStreamer::Global:
|
|
|
|
case RecordStreamer::Used:
|
|
|
|
Res |= BasicSymbolRef::SF_Undefined;
|
|
|
|
Res |= BasicSymbolRef::SF_Global;
|
|
|
|
break;
|
|
|
|
case RecordStreamer::DefinedWeak:
|
|
|
|
Res |= BasicSymbolRef::SF_Weak;
|
|
|
|
Res |= BasicSymbolRef::SF_Global;
|
|
|
|
break;
|
|
|
|
case RecordStreamer::UndefinedWeak:
|
|
|
|
Res |= BasicSymbolRef::SF_Weak;
|
|
|
|
Res |= BasicSymbolRef::SF_Undefined;
|
|
|
|
}
|
|
|
|
AsmSymbol(Key, BasicSymbolRef::Flags(Res));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleSymbolTable::printSymbolName(raw_ostream &OS, Symbol S) const {
|
|
|
|
if (S.is<AsmSymbol *>()) {
|
|
|
|
OS << S.get<AsmSymbol *>()->first;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto *GV = S.get<GlobalValue *>();
|
|
|
|
if (GV->hasDLLImportStorageClass())
|
|
|
|
OS << "__imp_";
|
|
|
|
|
|
|
|
Mang.getNameWithPrefix(OS, GV, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ModuleSymbolTable::getSymbolFlags(Symbol S) const {
|
|
|
|
if (S.is<AsmSymbol *>())
|
|
|
|
return S.get<AsmSymbol *>()->second;
|
|
|
|
|
|
|
|
auto *GV = S.get<GlobalValue *>();
|
|
|
|
|
|
|
|
uint32_t Res = BasicSymbolRef::SF_None;
|
|
|
|
if (GV->isDeclarationForLinker())
|
|
|
|
Res |= BasicSymbolRef::SF_Undefined;
|
|
|
|
else if (GV->hasHiddenVisibility() && !GV->hasLocalLinkage())
|
|
|
|
Res |= BasicSymbolRef::SF_Hidden;
|
|
|
|
if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
|
|
|
|
if (GVar->isConstant())
|
|
|
|
Res |= BasicSymbolRef::SF_Const;
|
|
|
|
}
|
2016-12-01 07:53:47 +01:00
|
|
|
if (dyn_cast_or_null<Function>(GV->getBaseObject()))
|
|
|
|
Res |= BasicSymbolRef::SF_Executable;
|
2016-12-01 08:00:35 +01:00
|
|
|
if (isa<GlobalAlias>(GV))
|
|
|
|
Res |= BasicSymbolRef::SF_Indirect;
|
2016-12-01 07:51:47 +01:00
|
|
|
if (GV->hasPrivateLinkage())
|
|
|
|
Res |= BasicSymbolRef::SF_FormatSpecific;
|
|
|
|
if (!GV->hasLocalLinkage())
|
|
|
|
Res |= BasicSymbolRef::SF_Global;
|
|
|
|
if (GV->hasCommonLinkage())
|
|
|
|
Res |= BasicSymbolRef::SF_Common;
|
|
|
|
if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() ||
|
|
|
|
GV->hasExternalWeakLinkage())
|
|
|
|
Res |= BasicSymbolRef::SF_Weak;
|
|
|
|
|
|
|
|
if (GV->getName().startswith("llvm."))
|
|
|
|
Res |= BasicSymbolRef::SF_FormatSpecific;
|
|
|
|
else if (auto *Var = dyn_cast<GlobalVariable>(GV)) {
|
|
|
|
if (Var->getSection() == "llvm.metadata")
|
|
|
|
Res |= BasicSymbolRef::SF_FormatSpecific;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|