blob: 874ffef9e02e03045215835e5955b06518f50a7a [file] [log] [blame]
Rafael Espindolabeee25e2015-08-14 14:12:54 +00001//===- SymbolTable.h --------------------------------------------*- C++ -*-===//
Michael J. Spencer84487f12015-07-24 21:03:07 +00002//
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#ifndef LLD_ELF_SYMBOL_TABLE_H
11#define LLD_ELF_SYMBOL_TABLE_H
12
13#include "InputFiles.h"
Rui Ueyamab4731a52015-07-29 16:30:40 +000014#include "llvm/ADT/DenseMap.h"
15#include "llvm/ADT/DenseMapInfo.h"
Rafael Espindola62b81b82015-08-14 13:07:05 +000016#include "llvm/MC/StringTableBuilder.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000017
18namespace lld {
19namespace elf2 {
20class Defined;
21struct Symbol;
22
23// SymbolTable is a bucket of all known symbols, including defined,
24// undefined, or lazy symbols (the last one is symbols in archive
25// files whose archive members are not yet loaded).
26//
27// We put all symbols of all files to a SymbolTable, and the
28// SymbolTable selects the "best" symbols if there are name
29// conflicts. For example, obviously, a defined symbol is better than
30// an undefined symbol. Or, if there's a conflict between a lazy and a
31// undefined, it'll read an archive member to read a real definition
32// to replace the lazy symbol. The logic is implemented in resolve().
Rafael Espindola2ffdd4d2015-08-04 14:29:01 +000033class SymbolTable {
Michael J. Spencer84487f12015-07-24 21:03:07 +000034public:
35 SymbolTable();
36
37 void addFile(std::unique_ptr<InputFile> File);
38
39 // Print an error message on undefined symbols.
40 void reportRemainingUndefines();
41
Michael J. Spencer84487f12015-07-24 21:03:07 +000042 // The writer needs to infer the machine type from the object files.
Rafael Espindola2ffdd4d2015-08-04 14:29:01 +000043 std::vector<std::unique_ptr<ObjectFileBase>> ObjectFiles;
Michael J. Spencer84487f12015-07-24 21:03:07 +000044
Rafael Espindola62b81b82015-08-14 13:07:05 +000045 unsigned getNumSymbols() { return Symtab.size(); }
46 llvm::StringTableBuilder &getStringBuilder() { return Builder; };
47
48 const llvm::DenseMap<StringRef, Symbol *> &getSymbols() const {
49 return Symtab;
50 }
51
Michael J. Spencer84487f12015-07-24 21:03:07 +000052private:
Rafael Espindola2ffdd4d2015-08-04 14:29:01 +000053 void addObject(ObjectFileBase *File);
Michael J. Spencer84487f12015-07-24 21:03:07 +000054
55 void resolve(SymbolBody *Body);
56
Rui Ueyamab4731a52015-07-29 16:30:40 +000057 llvm::DenseMap<StringRef, Symbol *> Symtab;
Michael J. Spencer84487f12015-07-24 21:03:07 +000058 llvm::BumpPtrAllocator Alloc;
Rafael Espindola62b81b82015-08-14 13:07:05 +000059 llvm::StringTableBuilder Builder;
Michael J. Spencer84487f12015-07-24 21:03:07 +000060};
61
62} // namespace elf2
63} // namespace lld
64
65#endif