summaryrefslogtreecommitdiffstats
path: root/lld/lib/Core/SymbolTable.cpp
blob: 223f15e6cdaf23c3b2e05ddaea76f3178be858cb (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
153
154
155
156
157
158
159
160
161
162
163
//===- Core/SymbolTable.cpp - Main Symbol Table ---------------------------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lld/Core/SymbolTable.h"
#include "lld/Core/Atom.h"
#include "lld/Core/File.h"
#include "lld/Core/InputFiles.h"
#include "lld/Core/Resolver.h"
#include "lld/Core/UndefinedAtom.h"
#include "lld/Platform/Platform.h"

#include "llvm/Support/ErrorHandling.h"

#include <algorithm>
#include <cassert>
#include <stdlib.h>
#include <vector>

namespace lld {

SymbolTable::SymbolTable(Platform& plat)
  : _platform(plat) {
}

void SymbolTable::add(const Atom &atom) {
  assert(atom.scope() != Atom::scopeTranslationUnit);
  if ( !atom.internalName() ) {
    this->addByName(atom);
  }
  else if ( atom.mergeDuplicates() ) {
    // TO DO: support constants merging
  }
}

enum NameCollisionResolution {
  NCR_First,
  NCR_Second,
  NCR_Weak,
  NCR_Larger,
  NCR_Dup,
  NCR_Error
};

static NameCollisionResolution cases[6][6] = {
  //regular     weak         tentative   absolute    undef      sharedLib
  {
    // first is regular
    NCR_Dup,    NCR_First,   NCR_First,  NCR_Error,  NCR_First, NCR_First
  },
  {
    // first is weak
    NCR_Second, NCR_Weak,   NCR_Larger, NCR_Error,  NCR_First, NCR_First
  },
  {
    // first is tentative
    NCR_Second, NCR_Second, NCR_Larger, NCR_Error,  NCR_First, NCR_First
  },
  {
    // first is absolute
    NCR_Error,  NCR_Error,  NCR_Error,  NCR_Error,  NCR_First, NCR_First
  },
  {
    // first is undef
    NCR_Second, NCR_Second, NCR_Second, NCR_Second, NCR_First, NCR_Second
  },
  {
    // first is sharedLib
    NCR_Second, NCR_Second, NCR_Second, NCR_Second, NCR_First, NCR_First
  }
};

static NameCollisionResolution collide(Atom::Definition first,
                                       Atom::Definition second) {
  return cases[first][second];
}

void SymbolTable::addByName(const Atom & newAtom) {
  llvm::StringRef name = newAtom.name();
  const Atom *existing = this->findByName(name);
  if (existing == NULL) {
    // name is not in symbol table yet, add it associate with this atom
    _nameTable[name] = &newAtom;
  } else {
    // name is already in symbol table and associated with another atom
    switch (collide(existing->definition(), newAtom.definition())) {
    case NCR_First:
      // using first, just add new to _replacedAtoms
      _replacedAtoms[&newAtom] = existing;
      break;
    case NCR_Second:
      // using second, update tables
      _nameTable[name] = &newAtom;
      _replacedAtoms[existing] = &newAtom;
      break;
    case NCR_Dup:
      if ( existing->mergeDuplicates() && newAtom.mergeDuplicates() ) {
          // using existing atom, add new atom to _replacedAtoms
        _replacedAtoms[&newAtom] = existing;
      }
      else {
        const Atom& use = _platform.handleMultipleDefinitions(*existing, newAtom);
        if ( &use == existing ) {
          // using existing atom, add new atom to _replacedAtoms
          _replacedAtoms[&newAtom] = existing;
        }
        else {
          // using new atom, update tables
          _nameTable[name] = &newAtom;
          _replacedAtoms[existing] = &newAtom;
        }
      }
      break;
    default:
      llvm::report_fatal_error("SymbolTable::addByName(): unhandled switch clause");
    }
  }
}

const Atom *SymbolTable::findByName(llvm::StringRef sym) {
  NameToAtom::iterator pos = _nameTable.find(sym);
  if (pos == _nameTable.end())
    return NULL;
  return pos->second;
}

bool SymbolTable::isDefined(llvm::StringRef sym) {
  const Atom *atom = this->findByName(sym);
  if (atom == NULL)
    return false;
  if (atom->definition() == Atom::definitionUndefined)
    return false;
  return true;
}

const Atom *SymbolTable::replacement(const Atom *atom) {
  AtomToAtom::iterator pos = _replacedAtoms.find(atom);
  if (pos == _replacedAtoms.end())
    return atom;
  // might be chain, recurse to end
  return this->replacement(pos->second);
}

unsigned int SymbolTable::size() {
  return _nameTable.size();
}

void SymbolTable::undefines(std::vector<const Atom *> &undefs) {
  for (NameToAtom::iterator it = _nameTable.begin(),
       end = _nameTable.end(); it != end; ++it) {
    const Atom *atom = it->second;
    assert(atom != NULL);
    if (atom->definition() == Atom::definitionUndefined)
      undefs.push_back(atom);
  }
}

} // namespace lld
OpenPOWER on IntegriCloud