llvm-6502/lib/Object/IRObjectFile.cpp

152 lines
4.2 KiB
C++
Raw Normal View History

//===- IRObjectFile.cpp - IR object file implementation ---------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Part of the IRObjectFile class implementation.
//
//===----------------------------------------------------------------------===//
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Mangler.h"
#include "llvm/IR/Module.h"
#include "llvm/Object/IRObjectFile.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
using namespace object;
IRObjectFile::IRObjectFile(MemoryBuffer *Object, std::error_code &EC,
LLVMContext &Context, bool BufferOwned)
: SymbolicFile(Binary::ID_IR, Object, BufferOwned) {
ErrorOr<Module*> MOrErr = parseBitcodeFile(Object, Context);
if ((EC = MOrErr.getError()))
return;
M.reset(MOrErr.get());
// If we have a DataLayout, setup a mangler.
const DataLayout *DL = M->getDataLayout();
if (!DL)
return;
Mang.reset(new Mangler(DL));
}
static const GlobalValue &getGV(DataRefImpl &Symb) {
return *reinterpret_cast<GlobalValue*>(Symb.p & ~uintptr_t(3));
}
static uintptr_t skipEmpty(Module::const_alias_iterator I, const Module &M) {
if (I == M.alias_end())
return 3;
const GlobalValue *GV = &*I;
return reinterpret_cast<uintptr_t>(GV) | 2;
}
static uintptr_t skipEmpty(Module::const_global_iterator I, const Module &M) {
if (I == M.global_end())
return skipEmpty(M.alias_begin(), M);
const GlobalValue *GV = &*I;
return reinterpret_cast<uintptr_t>(GV) | 1;
}
static uintptr_t skipEmpty(Module::const_iterator I, const Module &M) {
if (I == M.end())
return skipEmpty(M.global_begin(), M);
const GlobalValue *GV = &*I;
return reinterpret_cast<uintptr_t>(GV) | 0;
}
void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
const GlobalValue *GV = &getGV(Symb);
const Module &M = *GV->getParent();
uintptr_t Res;
switch (Symb.p & 3) {
case 0: {
Module::const_iterator Iter(static_cast<const Function*>(GV));
++Iter;
Res = skipEmpty(Iter, M);
break;
}
case 1: {
Module::const_global_iterator Iter(static_cast<const GlobalVariable*>(GV));
++Iter;
Res = skipEmpty(Iter, M);
break;
}
case 2: {
Module::const_alias_iterator Iter(static_cast<const GlobalAlias*>(GV));
++Iter;
Res = skipEmpty(Iter, M);
break;
}
case 3:
llvm_unreachable("Invalid symbol reference");
}
Symb.p = Res;
}
std::error_code IRObjectFile::printSymbolName(raw_ostream &OS,
DataRefImpl Symb) const {
const GlobalValue &GV = getGV(Symb);
if (Mang)
Mang->getNameWithPrefix(OS, &GV, false);
else
OS << GV.getName();
return object_error::success;
}
uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const {
const GlobalValue &GV = getGV(Symb);
uint32_t Res = BasicSymbolRef::SF_None;
if (GV.isDeclaration() || GV.hasAvailableExternallyLinkage())
Res |= BasicSymbolRef::SF_Undefined;
Remove the linker_private and linker_private_weak linkages. These linkages were introduced some time ago, but it was never very clear what exactly their semantics were or what they should be used for. Some investigation found these uses: * utf-16 strings in clang. * non-unnamed_addr strings produced by the sanitizers. It turns out they were just working around a more fundamental problem. For some sections a MachO linker needs a symbol in order to split the section into atoms, and llvm had no idea that was the case. I fixed that in r201700 and it is now safe to use the private linkage. When the object ends up in a section that requires symbols, llvm will use a 'l' prefix instead of a 'L' prefix and things just work. With that, these linkages were already dead, but there was a potential future user in the objc metadata information. I am still looking at CGObjcMac.cpp, but at this point I am convinced that linker_private and linker_private_weak are not what they need. The objc uses are currently split in * Regular symbols (no '\01' prefix). LLVM already directly provides whatever semantics they need. * Uses of a private name (start with "\01L" or "\01l") and private linkage. We can drop the "\01L" and "\01l" prefixes as soon as llvm agrees with clang on L being ok or not for a given section. I have two patches in code review for this. * Uses of private name and weak linkage. The last case is the one that one could think would fit one of these linkages. That is not the case. The semantics are * the linker will merge these symbol by *name*. * the linker will hide them in the final DSO. Given that the merging is done by name, any of the private (or internal) linkages would be a bad match. They allow llvm to rename the symbols, and that is really not what we want. From the llvm point of view, these objects should really be (linkonce|weak)(_odr)?. For now, just keeping the "\01l" prefix is probably the best for these symbols. If we one day want to have a more direct support in llvm, IMHO what we should add is not a linkage, it is just a hidden_symbol attribute. It would be applicable to multiple linkages. For example, on weak it would produce the current behavior we have for objc metadata. On internal, it would be equivalent to private (and we should then remove private). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@203866 91177308-0d34-0410-b5e6-96231b3b80d8
2014-03-13 23:18:37 +00: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())
Res |= BasicSymbolRef::SF_Weak;
return Res;
}
const GlobalValue &IRObjectFile::getSymbolGV(DataRefImpl Symb) const {
const GlobalValue &GV = getGV(Symb);
return GV;
}
basic_symbol_iterator IRObjectFile::symbol_begin_impl() const {
Module::const_iterator I = M->begin();
DataRefImpl Ret;
Ret.p = skipEmpty(I, *M);
return basic_symbol_iterator(BasicSymbolRef(Ret, this));
}
basic_symbol_iterator IRObjectFile::symbol_end_impl() const {
DataRefImpl Ret;
Ret.p = 3;
return basic_symbol_iterator(BasicSymbolRef(Ret, this));
}
ErrorOr<SymbolicFile *> llvm::object::SymbolicFile::createIRObjectFile(
MemoryBuffer *Object, LLVMContext &Context, bool BufferOwned) {
std::error_code EC;
std::unique_ptr<IRObjectFile> Ret(
new IRObjectFile(Object, EC, Context, BufferOwned));
if (EC)
return EC;
return Ret.release();
}