blob: b88ff0a11c6c593d9e39261df1649913a65456a8 [file] [log] [blame]
Michael J. Spencer773a8fb2011-12-18 08:27:59 +00001//===- Core/SymbolTable.cpp - Main Symbol Table ---------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lld/Core/SymbolTable.h"
11#include "lld/Core/Atom.h"
12#include "lld/Core/File.h"
13#include "lld/Core/InputFiles.h"
14#include "lld/Core/Resolver.h"
15#include "lld/Core/UndefinedAtom.h"
16#include "lld/Platform/Platform.h"
17
18#include "llvm/Support/ErrorHandling.h"
19
20#include <algorithm>
21#include <cassert>
22#include <stdlib.h>
23#include <vector>
24
25namespace lld {
26
Nick Kledzik38eec3d2011-12-22 02:38:01 +000027SymbolTable::SymbolTable(Platform& plat)
28 : _platform(plat) {
29}
30
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000031void SymbolTable::add(const Atom &atom) {
32 assert(atom.scope() != Atom::scopeTranslationUnit);
Nick Kledzikf96d0ad2011-12-20 02:18:44 +000033 if ( !atom.internalName() ) {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000034 this->addByName(atom);
Nick Kledzikf96d0ad2011-12-20 02:18:44 +000035 }
36 else if ( atom.mergeDuplicates() ) {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000037 // TO DO: support constants merging
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000038 }
39}
40
41enum NameCollisionResolution {
42 NCR_First,
43 NCR_Second,
44 NCR_Weak,
45 NCR_Larger,
Nick Kledzik38eec3d2011-12-22 02:38:01 +000046 NCR_Dup,
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000047 NCR_Error
48};
49
Nick Kledzikf96d0ad2011-12-20 02:18:44 +000050static NameCollisionResolution cases[6][6] = {
51 //regular weak tentative absolute undef sharedLib
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000052 {
53 // first is regular
Nick Kledzik38eec3d2011-12-22 02:38:01 +000054 NCR_Dup, NCR_First, NCR_First, NCR_Error, NCR_First, NCR_First
Nick Kledzikf96d0ad2011-12-20 02:18:44 +000055 },
56 {
57 // first is weak
58 NCR_Second, NCR_Weak, NCR_Larger, NCR_Error, NCR_First, NCR_First
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000059 },
60 {
61 // first is tentative
Nick Kledzikf96d0ad2011-12-20 02:18:44 +000062 NCR_Second, NCR_Second, NCR_Larger, NCR_Error, NCR_First, NCR_First
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000063 },
64 {
65 // first is absolute
Nick Kledzikf96d0ad2011-12-20 02:18:44 +000066 NCR_Error, NCR_Error, NCR_Error, NCR_Error, NCR_First, NCR_First
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000067 },
68 {
69 // first is undef
Nick Kledzikf96d0ad2011-12-20 02:18:44 +000070 NCR_Second, NCR_Second, NCR_Second, NCR_Second, NCR_First, NCR_Second
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000071 },
72 {
73 // first is sharedLib
Nick Kledzikf96d0ad2011-12-20 02:18:44 +000074 NCR_Second, NCR_Second, NCR_Second, NCR_Second, NCR_First, NCR_First
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000075 }
76};
77
78static NameCollisionResolution collide(Atom::Definition first,
79 Atom::Definition second) {
80 return cases[first][second];
81}
82
Nick Kledzik38eec3d2011-12-22 02:38:01 +000083void SymbolTable::addByName(const Atom & newAtom) {
84 llvm::StringRef name = newAtom.name();
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000085 const Atom *existing = this->findByName(name);
86 if (existing == NULL) {
Nick Kledzik7735a7d2012-01-04 23:58:17 +000087 // Name is not in symbol table yet, add it associate with this atom.
Nick Kledzik38eec3d2011-12-22 02:38:01 +000088 _nameTable[name] = &newAtom;
Nick Kledzik7735a7d2012-01-04 23:58:17 +000089 }
90 else {
91 // Name is already in symbol table and associated with another atom.
92 bool useNew = true;
Nick Kledzik38eec3d2011-12-22 02:38:01 +000093 switch (collide(existing->definition(), newAtom.definition())) {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000094 case NCR_First:
Nick Kledzik7735a7d2012-01-04 23:58:17 +000095 useNew = false;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000096 break;
97 case NCR_Second:
Nick Kledzik7735a7d2012-01-04 23:58:17 +000098 useNew = true;
Nick Kledzik38eec3d2011-12-22 02:38:01 +000099 break;
100 case NCR_Dup:
101 if ( existing->mergeDuplicates() && newAtom.mergeDuplicates() ) {
Nick Kledzik7735a7d2012-01-04 23:58:17 +0000102 // Both mergeable. Use auto-hide bit as tie breaker
103 if ( existing->autoHide() != newAtom.autoHide() ) {
104 // They have different autoHide values, keep non-autohide one
105 useNew = existing->autoHide();
106 }
107 else {
108 // They have same autoHide, so just keep using existing
109 useNew = false;
110 }
Nick Kledzik38eec3d2011-12-22 02:38:01 +0000111 }
112 else {
113 const Atom& use = _platform.handleMultipleDefinitions(*existing, newAtom);
Nick Kledzik7735a7d2012-01-04 23:58:17 +0000114 useNew = ( &use != existing );
Nick Kledzik38eec3d2011-12-22 02:38:01 +0000115 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000116 break;
117 default:
Nick Kledzik38eec3d2011-12-22 02:38:01 +0000118 llvm::report_fatal_error("SymbolTable::addByName(): unhandled switch clause");
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000119 }
Nick Kledzik7735a7d2012-01-04 23:58:17 +0000120 if ( useNew ) {
121 // Update name table to use new atom.
122 _nameTable[name] = &newAtom;
123 // Add existing atom to replacement table.
124 _replacedAtoms[existing] = &newAtom;
125 }
126 else {
127 // New atom is not being used. Add it to replacement table.
128 _replacedAtoms[&newAtom] = existing;
129 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000130 }
131}
132
133const Atom *SymbolTable::findByName(llvm::StringRef sym) {
134 NameToAtom::iterator pos = _nameTable.find(sym);
135 if (pos == _nameTable.end())
136 return NULL;
137 return pos->second;
138}
139
140bool SymbolTable::isDefined(llvm::StringRef sym) {
141 const Atom *atom = this->findByName(sym);
142 if (atom == NULL)
143 return false;
144 if (atom->definition() == Atom::definitionUndefined)
145 return false;
146 return true;
147}
148
149const Atom *SymbolTable::replacement(const Atom *atom) {
150 AtomToAtom::iterator pos = _replacedAtoms.find(atom);
151 if (pos == _replacedAtoms.end())
152 return atom;
153 // might be chain, recurse to end
154 return this->replacement(pos->second);
155}
156
157unsigned int SymbolTable::size() {
158 return _nameTable.size();
159}
160
161void SymbolTable::undefines(std::vector<const Atom *> &undefs) {
162 for (NameToAtom::iterator it = _nameTable.begin(),
163 end = _nameTable.end(); it != end; ++it) {
164 const Atom *atom = it->second;
165 assert(atom != NULL);
166 if (atom->definition() == Atom::definitionUndefined)
167 undefs.push_back(atom);
168 }
169}
170
171} // namespace lld