blob: c99f158299684b862af49282ab888950a4570416 [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:07 +00001//===- SymbolTable.cpp ----------------------------------------------------===//
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 "SymbolTable.h"
Rafael Espindola192e1fa2015-08-06 15:08:23 +000011#include "Error.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000012#include "Symbols.h"
13
14using namespace llvm;
Rafael Espindoladaa92a62015-08-31 01:16:19 +000015using namespace llvm::object;
Michael J. Spencer84487f12015-07-24 21:03:07 +000016
17using namespace lld;
18using namespace lld::elf2;
19
Rafael Espindola1bd885a2015-08-14 16:46:28 +000020SymbolTable::SymbolTable() {
Rafael Espindola1bd885a2015-08-14 16:46:28 +000021}
Michael J. Spencer84487f12015-07-24 21:03:07 +000022
Rafael Espindola2ffdd4d2015-08-04 14:29:01 +000023void SymbolTable::addFile(std::unique_ptr<InputFile> File) {
Michael J. Spencer84487f12015-07-24 21:03:07 +000024 File->parse();
25 InputFile *FileP = File.release();
Rafael Espindola2ffdd4d2015-08-04 14:29:01 +000026 auto *P = cast<ObjectFileBase>(FileP);
Michael J. Spencer84487f12015-07-24 21:03:07 +000027 addObject(P);
Michael J. Spencer84487f12015-07-24 21:03:07 +000028}
29
Rafael Espindolaf7d45f02015-08-31 01:46:20 +000030template <class ELFT> void SymbolTable::init() {
31 resolve<ELFT>(new (Alloc)
32 Undefined<ELFT>("_start", Undefined<ELFT>::Synthetic));
33}
34
Rafael Espindola2ffdd4d2015-08-04 14:29:01 +000035void SymbolTable::addObject(ObjectFileBase *File) {
Rafael Espindola3c9cb4b2015-08-05 12:03:34 +000036 if (!ObjectFiles.empty()) {
37 ObjectFileBase &Old = *ObjectFiles[0];
38 if (!Old.isCompatibleWith(*File))
39 error(Twine(Old.getName() + " is incompatible with " + File->getName()));
Rafael Espindoladaa92a62015-08-31 01:16:19 +000040 } else {
Rafael Espindoladaa92a62015-08-31 01:16:19 +000041 switch (File->kind()) {
42 case InputFile::Object32LEKind:
Rafael Espindolaf7d45f02015-08-31 01:46:20 +000043 init<ELF32LE>();
Rafael Espindoladaa92a62015-08-31 01:16:19 +000044 break;
45 case InputFile::Object32BEKind:
Rafael Espindolaf7d45f02015-08-31 01:46:20 +000046 init<ELF32BE>();
Rafael Espindoladaa92a62015-08-31 01:16:19 +000047 break;
48 case InputFile::Object64LEKind:
Rafael Espindolaf7d45f02015-08-31 01:46:20 +000049 init<ELF64LE>();
Rafael Espindoladaa92a62015-08-31 01:16:19 +000050 break;
51 case InputFile::Object64BEKind:
Rafael Espindolaf7d45f02015-08-31 01:46:20 +000052 init<ELF64BE>();
Rafael Espindoladaa92a62015-08-31 01:16:19 +000053 break;
54 }
Rafael Espindola3c9cb4b2015-08-05 12:03:34 +000055 }
56
Michael J. Spencer84487f12015-07-24 21:03:07 +000057 ObjectFiles.emplace_back(File);
Rafael Espindoladaa92a62015-08-31 01:16:19 +000058 for (SymbolBody *Body : File->getSymbols()) {
59 switch (File->kind()) {
60 case InputFile::Object32LEKind:
61 resolve<ELF32LE>(Body);
62 break;
63 case InputFile::Object32BEKind:
64 resolve<ELF32BE>(Body);
65 break;
66 case InputFile::Object64LEKind:
67 resolve<ELF64LE>(Body);
68 break;
69 case InputFile::Object64BEKind:
70 resolve<ELF64BE>(Body);
71 break;
72 }
73 }
Michael J. Spencer84487f12015-07-24 21:03:07 +000074}
75
Rafael Espindola2ffdd4d2015-08-04 14:29:01 +000076void SymbolTable::reportRemainingUndefines() {
Michael J. Spencer84487f12015-07-24 21:03:07 +000077 for (auto &I : Symtab) {
Rafael Espindola1bd885a2015-08-14 16:46:28 +000078 SymbolBody *Body = I.second->Body;
79 if (Body->isStrongUndefined())
80 error(Twine("undefined symbol: ") + Body->getName());
Michael J. Spencer84487f12015-07-24 21:03:07 +000081 }
82}
83
84// This function resolves conflicts if there's an existing symbol with
85// the same name. Decisions are made based on symbol type.
Rafael Espindoladaa92a62015-08-31 01:16:19 +000086template <class ELFT> void SymbolTable::resolve(SymbolBody *New) {
Michael J. Spencer84487f12015-07-24 21:03:07 +000087 // Find an existing Symbol or create and insert a new one.
88 StringRef Name = New->getName();
Rafael Espindola62b81b82015-08-14 13:07:05 +000089 Builder.add(Name);
Michael J. Spencer84487f12015-07-24 21:03:07 +000090 Symbol *&Sym = Symtab[Name];
91 if (!Sym) {
92 Sym = new (Alloc) Symbol(New);
93 New->setBackref(Sym);
94 return;
95 }
96 New->setBackref(Sym);
97
98 // compare() returns -1, 0, or 1 if the lhs symbol is less preferable,
99 // equivalent (conflicting), or more preferable, respectively.
100 SymbolBody *Existing = Sym->Body;
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000101 int comp = Existing->compare<ELFT>(New);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000102 if (comp < 0)
103 Sym->Body = New;
104 if (comp == 0)
105 error(Twine("duplicate symbol: ") + Name);
106}