Add support for inline asm symbols to IRObjectFile.

This also enables it in llvm-nm so that it can be tested.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@212282 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola 2014-07-03 18:59:23 +00:00
parent c150516f36
commit 0a7a2ef6b5
10 changed files with 300 additions and 127 deletions

View File

@ -25,6 +25,7 @@ namespace object {
class IRObjectFile : public SymbolicFile {
std::unique_ptr<Module> M;
std::unique_ptr<Mangler> Mang;
std::vector<std::pair<std::string, uint32_t>> AsmSymbols;
public:
IRObjectFile(std::unique_ptr<MemoryBuffer> Object, std::error_code &EC,
@ -34,7 +35,7 @@ public:
std::error_code printSymbolName(raw_ostream &OS,
DataRefImpl Symb) const override;
uint32_t getSymbolFlags(DataRefImpl Symb) const override;
const GlobalValue &getSymbolGV(DataRefImpl Symb) const;
const GlobalValue *getSymbolGV(DataRefImpl Symb) const;
basic_symbol_iterator symbol_begin_impl() const override;
basic_symbol_iterator symbol_end_impl() const override;

View File

@ -0,0 +1,42 @@
//===-- RecordStreamer.h - Record asm defined and used symbols ---*- C++ -*===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_OBJECT_RECORD_STREAMER
#define LLVM_OBJECT_RECORD_STREAMER
#include "llvm/MC/MCStreamer.h"
namespace llvm {
class RecordStreamer : public MCStreamer {
public:
enum State { NeverSeen, Global, Defined, DefinedGlobal, Used };
private:
StringMap<State> Symbols;
void markDefined(const MCSymbol &Symbol);
void markGlobal(const MCSymbol &Symbol);
void markUsed(const MCSymbol &Symbol);
void visitUsedSymbol(const MCSymbol &Sym) override;
public:
typedef StringMap<State>::const_iterator const_iterator;
const_iterator begin();
const_iterator end();
RecordStreamer(MCContext &Context);
void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override;
void EmitLabel(MCSymbol *Symbol) override;
void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
void EmitZerofill(const MCSection *Section, MCSymbol *Symbol, uint64_t Size,
unsigned ByteAlignment) override;
void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
unsigned ByteAlignment) override;
};
}
#endif

View File

@ -24,11 +24,11 @@
#include "llvm/MC/MCInstrInfo.h"
#include "llvm/MC/MCParser/MCAsmParser.h"
#include "llvm/MC/MCSection.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/MC/MCTargetAsmParser.h"
#include "llvm/MC/SubtargetFeature.h"
#include "llvm/Object/RecordStreamer.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Host.h"
@ -552,105 +552,6 @@ LTOModule::addPotentialUndefinedSymbol(const GlobalValue *decl, bool isFunc) {
entry.setValue(info);
}
namespace {
class RecordStreamer : public MCStreamer {
public:
enum State { NeverSeen, Global, Defined, DefinedGlobal, Used };
private:
StringMap<State> Symbols;
void markDefined(const MCSymbol &Symbol) {
State &S = Symbols[Symbol.getName()];
switch (S) {
case DefinedGlobal:
case Global:
S = DefinedGlobal;
break;
case NeverSeen:
case Defined:
case Used:
S = Defined;
break;
}
}
void markGlobal(const MCSymbol &Symbol) {
State &S = Symbols[Symbol.getName()];
switch (S) {
case DefinedGlobal:
case Defined:
S = DefinedGlobal;
break;
case NeverSeen:
case Global:
case Used:
S = Global;
break;
}
}
void markUsed(const MCSymbol &Symbol) {
State &S = Symbols[Symbol.getName()];
switch (S) {
case DefinedGlobal:
case Defined:
case Global:
break;
case NeverSeen:
case Used:
S = Used;
break;
}
}
void visitUsedSymbol(const MCSymbol &Sym) override {
markUsed(Sym);
}
public:
typedef StringMap<State>::const_iterator const_iterator;
const_iterator begin() {
return Symbols.begin();
}
const_iterator end() {
return Symbols.end();
}
RecordStreamer(MCContext &Context) : MCStreamer(Context) {}
void EmitInstruction(const MCInst &Inst,
const MCSubtargetInfo &STI) override {
MCStreamer::EmitInstruction(Inst, STI);
}
void EmitLabel(MCSymbol *Symbol) override {
MCStreamer::EmitLabel(Symbol);
markDefined(*Symbol);
}
void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) override {
markDefined(*Symbol);
MCStreamer::EmitAssignment(Symbol, Value);
}
bool EmitSymbolAttribute(MCSymbol *Symbol,
MCSymbolAttr Attribute) override {
if (Attribute == MCSA_Global)
markGlobal(*Symbol);
return true;
}
void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
uint64_t Size , unsigned ByteAlignment) override {
markDefined(*Symbol);
}
void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
unsigned ByteAlignment) override {
markDefined(*Symbol);
}
};
} // end anonymous namespace
/// addAsmGlobalSymbols - Add global symbols from module-level ASM to the
/// defined or undefined lists.
bool LTOModule::addAsmGlobalSymbols(std::string &errMsg) {

View File

@ -12,5 +12,6 @@ add_llvm_library(LLVMObject
MachOUniversal.cpp
Object.cpp
ObjectFile.cpp
RecordStreamer.cpp
SymbolicFile.cpp
)

View File

@ -17,7 +17,17 @@
#include "llvm/IR/Mangler.h"
#include "llvm/IR/Module.h"
#include "llvm/Object/IRObjectFile.h"
#include "llvm/Object/RecordStreamer.h"
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCInstrInfo.h"
#include "llvm/MC/MCObjectFileInfo.h"
#include "llvm/MC/MCTargetAsmParser.h"
#include "llvm/MC/MCParser/MCAsmParser.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
using namespace object;
@ -37,12 +47,85 @@ IRObjectFile::IRObjectFile(std::unique_ptr<MemoryBuffer> Object,
return;
Mang.reset(new Mangler(DL));
const std::string &InlineAsm = M->getModuleInlineAsm();
if (InlineAsm.empty())
return;
StringRef Triple = M->getTargetTriple();
std::string Err;
const Target *T = TargetRegistry::lookupTarget(Triple, Err);
if (!T)
return;
std::unique_ptr<MCRegisterInfo> MRI(T->createMCRegInfo(Triple));
if (!MRI)
return;
std::unique_ptr<MCAsmInfo> MAI(T->createMCAsmInfo(*MRI, Triple));
if (!MAI)
return;
std::unique_ptr<MCSubtargetInfo> STI(
T->createMCSubtargetInfo(Triple, "", ""));
if (!STI)
return;
std::unique_ptr<MCInstrInfo> MCII(T->createMCInstrInfo());
if (!MCII)
return;
MCObjectFileInfo MOFI;
MCContext MCCtx(MAI.get(), MRI.get(), &MOFI);
MOFI.InitMCObjectFileInfo(Triple, Reloc::Default, CodeModel::Default, MCCtx);
std::unique_ptr<RecordStreamer> Streamer(new RecordStreamer(MCCtx));
std::unique_ptr<MemoryBuffer> Buffer(MemoryBuffer::getMemBuffer(InlineAsm));
SourceMgr SrcMgr;
SrcMgr.AddNewSourceBuffer(Buffer.release(), 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;
for (auto &KV : *Streamer) {
StringRef Key = KV.first();
RecordStreamer::State Value = KV.second;
uint32_t Res = BasicSymbolRef::SF_None;
switch (Value) {
case RecordStreamer::NeverSeen:
llvm_unreachable("foo");
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;
}
AsmSymbols.push_back(
std::make_pair<std::string, uint32_t>(Key, std::move(Res)));
}
}
IRObjectFile::~IRObjectFile() { M->getMaterializer()->releaseBuffer(); }
static const GlobalValue &getGV(DataRefImpl &Symb) {
return *reinterpret_cast<GlobalValue*>(Symb.p & ~uintptr_t(3));
static const GlobalValue *getGV(DataRefImpl &Symb) {
if ((Symb.p & 3) == 3)
return nullptr;
return reinterpret_cast<GlobalValue*>(Symb.p & ~uintptr_t(3));
}
static uintptr_t skipEmpty(Module::const_alias_iterator I, const Module &M) {
@ -66,31 +149,43 @@ static uintptr_t skipEmpty(Module::const_iterator I, const Module &M) {
return reinterpret_cast<uintptr_t>(GV) | 0;
}
static unsigned getAsmSymIndex(DataRefImpl Symb) {
assert((Symb.p & uintptr_t(3)) == 3);
uintptr_t Index = Symb.p & ~uintptr_t(3);
Index >>= 2;
return Index;
}
void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
const GlobalValue *GV = &getGV(Symb);
const Module &M = *GV->getParent();
const GlobalValue *GV = getGV(Symb);
uintptr_t Res;
switch (Symb.p & 3) {
case 0: {
Module::const_iterator Iter(static_cast<const Function*>(GV));
++Iter;
Res = skipEmpty(Iter, M);
Res = skipEmpty(Iter, *M);
break;
}
case 1: {
Module::const_global_iterator Iter(static_cast<const GlobalVariable*>(GV));
++Iter;
Res = skipEmpty(Iter, M);
Res = skipEmpty(Iter, *M);
break;
}
case 2: {
Module::const_alias_iterator Iter(static_cast<const GlobalAlias*>(GV));
++Iter;
Res = skipEmpty(Iter, M);
Res = skipEmpty(Iter, *M);
break;
}
case 3: {
unsigned Index = getAsmSymIndex(Symb);
assert(Index < AsmSymbols.size());
++Index;
Res = (Index << 2) | 3;
break;
}
case 3:
llvm_unreachable("Invalid symbol reference");
}
Symb.p = Res;
@ -98,12 +193,18 @@ void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
std::error_code IRObjectFile::printSymbolName(raw_ostream &OS,
DataRefImpl Symb) const {
const GlobalValue &GV = getGV(Symb);
const GlobalValue *GV = getGV(Symb);
if (!GV) {
unsigned Index = getAsmSymIndex(Symb);
assert(Index <= AsmSymbols.size());
OS << AsmSymbols[Index].first;
return object_error::success;;
}
if (Mang)
Mang->getNameWithPrefix(OS, &GV, false);
Mang->getNameWithPrefix(OS, GV, false);
else
OS << GV.getName();
OS << GV->getName();
return object_error::success;
}
@ -119,25 +220,31 @@ static bool isDeclaration(const GlobalValue &V) {
}
uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const {
const GlobalValue &GV = getGV(Symb);
const GlobalValue *GV = getGV(Symb);
if (!GV) {
unsigned Index = getAsmSymIndex(Symb);
assert(Index <= AsmSymbols.size());
return AsmSymbols[Index].second;
}
uint32_t Res = BasicSymbolRef::SF_None;
if (isDeclaration(GV))
if (isDeclaration(*GV))
Res |= BasicSymbolRef::SF_Undefined;
if (GV.hasPrivateLinkage())
if (GV->hasPrivateLinkage())
Res |= BasicSymbolRef::SF_FormatSpecific;
if (!GV.hasLocalLinkage())
if (!GV->hasLocalLinkage())
Res |= BasicSymbolRef::SF_Global;
if (GV.hasCommonLinkage())
if (GV->hasCommonLinkage())
Res |= BasicSymbolRef::SF_Common;
if (GV.hasLinkOnceLinkage() || GV.hasWeakLinkage())
if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage())
Res |= BasicSymbolRef::SF_Weak;
return Res;
}
const GlobalValue &IRObjectFile::getSymbolGV(DataRefImpl Symb) const {
const GlobalValue &GV = getGV(Symb);
const GlobalValue *IRObjectFile::getSymbolGV(DataRefImpl Symb) const {
const GlobalValue *GV = getGV(Symb);
return GV;
}
@ -150,7 +257,9 @@ basic_symbol_iterator IRObjectFile::symbol_begin_impl() const {
basic_symbol_iterator IRObjectFile::symbol_end_impl() const {
DataRefImpl Ret;
Ret.p = 3;
uint64_t NumAsm = AsmSymbols.size();
NumAsm <<= 2;
Ret.p = 3 | NumAsm;
return basic_symbol_iterator(BasicSymbolRef(Ret, this));
}

View File

@ -19,4 +19,4 @@
type = Library
name = Object
parent = Libraries
required_libraries = BitReader Core Support MC
required_libraries = BitReader Core Support MC MCParser

View File

@ -0,0 +1,100 @@
//===-- RecordStreamer.cpp - Record asm definde and used symbols ----------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/Object/RecordStreamer.h"
#include "llvm/MC/MCSymbol.h"
using namespace llvm;
void RecordStreamer::markDefined(const MCSymbol &Symbol) {
State &S = Symbols[Symbol.getName()];
switch (S) {
case DefinedGlobal:
case Global:
S = DefinedGlobal;
break;
case NeverSeen:
case Defined:
case Used:
S = Defined;
break;
}
}
void RecordStreamer::markGlobal(const MCSymbol &Symbol) {
State &S = Symbols[Symbol.getName()];
switch (S) {
case DefinedGlobal:
case Defined:
S = DefinedGlobal;
break;
case NeverSeen:
case Global:
case Used:
S = Global;
break;
}
}
void RecordStreamer::markUsed(const MCSymbol &Symbol) {
State &S = Symbols[Symbol.getName()];
switch (S) {
case DefinedGlobal:
case Defined:
case Global:
break;
case NeverSeen:
case Used:
S = Used;
break;
}
}
void RecordStreamer::visitUsedSymbol(const MCSymbol &Sym) { markUsed(Sym); }
RecordStreamer::const_iterator RecordStreamer::begin() {
return Symbols.begin();
}
RecordStreamer::const_iterator RecordStreamer::end() { return Symbols.end(); }
RecordStreamer::RecordStreamer(MCContext &Context) : MCStreamer(Context) {}
void RecordStreamer::EmitInstruction(const MCInst &Inst,
const MCSubtargetInfo &STI) {
MCStreamer::EmitInstruction(Inst, STI);
}
void RecordStreamer::EmitLabel(MCSymbol *Symbol) {
MCStreamer::EmitLabel(Symbol);
markDefined(*Symbol);
}
void RecordStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
markDefined(*Symbol);
MCStreamer::EmitAssignment(Symbol, Value);
}
bool RecordStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
MCSymbolAttr Attribute) {
if (Attribute == MCSA_Global)
markGlobal(*Symbol);
return true;
}
void RecordStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
uint64_t Size, unsigned ByteAlignment) {
markDefined(*Symbol);
}
void RecordStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
unsigned ByteAlignment) {
markDefined(*Symbol);
}

View File

@ -10,6 +10,17 @@
; CHECK-NEXT: d g2
; CHECK-NEXT: C g3
; CHECK-NOT: g4
; CHECK-NEXT: T global_asm_sym
; CHECK-NEXT: t local_asm_sym
; CHECK-NEXT: U undef_asm_sy
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
module asm ".global global_asm_sym"
module asm "global_asm_sym:"
module asm "local_asm_sym:"
module asm ".long undef_asm_sym"
@g1 = global i32 42
@g2 = internal global i32 42

View File

@ -1,4 +1,5 @@
set(LLVM_LINK_COMPONENTS
${LLVM_TARGETS_TO_BUILD}
Object
Support
)

View File

@ -37,6 +37,7 @@
#include "llvm/Support/Program.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/TargetSelect.h"
#include <algorithm>
#include <cctype>
#include <cerrno>
@ -637,8 +638,10 @@ static char getSymbolNMTypeChar(const GlobalValue &GV) {
}
static char getSymbolNMTypeChar(IRObjectFile &Obj, basic_symbol_iterator I) {
const GlobalValue &GV = Obj.getSymbolGV(I->getRawDataRefImpl());
return getSymbolNMTypeChar(GV);
const GlobalValue *GV = Obj.getSymbolGV(I->getRawDataRefImpl());
if (!GV)
return 't';
return getSymbolNMTypeChar(*GV);
}
template <class ELFT>
@ -723,8 +726,8 @@ static void dumpSymbolNamesFromObject(SymbolicFile *Obj, bool printName) {
continue;
if (WithoutAliases) {
if (IRObjectFile *IR = dyn_cast<IRObjectFile>(Obj)) {
const GlobalValue &GV = IR->getSymbolGV(I->getRawDataRefImpl());
if (isa<GlobalAlias>(GV))
const GlobalValue *GV = IR->getSymbolGV(I->getRawDataRefImpl());
if (GV && isa<GlobalAlias>(GV))
continue;
}
}
@ -993,6 +996,10 @@ int main(int argc, char **argv) {
if (error(sys::ChangeStdinToBinary()))
return 1;
llvm::InitializeAllTargetInfos();
llvm::InitializeAllTargetMCs();
llvm::InitializeAllAsmParsers();
ToolName = argv[0];
if (BSDFormat)
OutputFormat = bsd;