summaryrefslogtreecommitdiff
path: root/lib/Object/IRObjectFile.cpp
blob: e8d51ecbf67b227539c949162bbf5e6f782093c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//===- 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, 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;
}

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;
  if (GV.hasPrivateLinkage() || GV.hasLinkerPrivateLinkage() ||
      GV.hasLinkerPrivateWeakLinkage())
    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) {
  error_code EC;
  OwningPtr<IRObjectFile> Ret(
      new IRObjectFile(Object, EC, Context, BufferOwned));
  if (EC)
    return EC;
  return Ret.release();
}