Skip to content

Commit

Permalink
Move SymbolTable::addCombinedLTOObject() to LinkerDriver.
Browse files Browse the repository at this point in the history
Also renames it LinkerDriver::compileBitcodeFiles.

The function doesn't logically belong to SymbolTable. We added this
function to the symbol table because symbol table used to be a
container of input files. This is no longer the case.

Differential Revision: https://reviews.llvm.org/D62291

llvm-svn: 361469
  • Loading branch information
rui314 committed May 23, 2019
1 parent 3919204 commit 0baaf45
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 34 deletions.
25 changes: 24 additions & 1 deletion lld/ELF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1436,6 +1436,29 @@ template <class ELFT> static Symbol *addUndefined(StringRef Name) {
Undefined{nullptr, Name, STB_GLOBAL, STV_DEFAULT, 0});
}

// This function is where all the optimizations of link-time
// optimization takes place. When LTO is in use, some input files are
// not in native object file format but in the LLVM bitcode format.
// This function compiles bitcode files into a few big native files
// using LLVM functions and replaces bitcode symbols with the results.
// Because all bitcode files that the program consists of are passed to
// the compiler at once, it can do a whole-program optimization.
template <class ELFT> void LinkerDriver::compileBitcodeFiles() {
// Compile bitcode files and replace bitcode symbols.
LTO.reset(new BitcodeCompiler);
for (BitcodeFile *File : BitcodeFiles)
LTO->add(*File);

for (InputFile *File : LTO->compile()) {
DenseMap<CachedHashStringRef, const InputFile *> DummyGroups;
auto *Obj = cast<ObjFile<ELFT>>(File);
Obj->parse(DummyGroups);
for (Symbol *Sym : Obj->getGlobalSymbols())
Sym->parseSymbolVersion();
ObjectFiles.push_back(File);
}
}

// The --wrap option is a feature to rename symbols so that you can write
// wrappers for existing functions. If you pass `-wrap=foo`, all
// occurrences of symbol `foo` are resolved to `wrap_foo` (so, you are
Expand Down Expand Up @@ -1645,7 +1668,7 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) {
//
// With this the symbol table should be complete. After this, no new names
// except a few linker-synthesized ones will be added to the symbol table.
Symtab->addCombinedLTOObject<ELFT>();
compileBitcodeFiles<ELFT>();
if (errorCount())
return;

Expand Down
5 changes: 5 additions & 0 deletions lld/ELF/Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef LLD_ELF_DRIVER_H
#define LLD_ELF_DRIVER_H

#include "LTO.h"
#include "SymbolTable.h"
#include "lld/Common/LLVM.h"
#include "lld/Common/Reproduce.h"
Expand All @@ -33,13 +34,17 @@ class LinkerDriver {
void createFiles(llvm::opt::InputArgList &Args);
void inferMachineType();
template <class ELFT> void link(llvm::opt::InputArgList &Args);
template <class ELFT> void compileBitcodeFiles();

// True if we are in --whole-archive and --no-whole-archive.
bool InWholeArchive = false;

// True if we are in --start-lib and --end-lib.
bool InLib = false;

// For LTO.
std::unique_ptr<BitcodeCompiler> LTO;

std::vector<InputFile *> Files;
};

Expand Down
28 changes: 0 additions & 28 deletions lld/ELF/SymbolTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,6 @@ using namespace lld::elf;

SymbolTable *elf::Symtab;

// This function is where all the optimizations of link-time
// optimization happens. When LTO is in use, some input files are
// not in native object file format but in the LLVM bitcode format.
// This function compiles bitcode files into a few big native files
// using LLVM functions and replaces bitcode symbols with the results.
// Because all bitcode files that the program consists of are passed
// to the compiler at once, it can do whole-program optimization.
template <class ELFT> void SymbolTable::addCombinedLTOObject() {
// Compile bitcode files and replace bitcode symbols.
LTO.reset(new BitcodeCompiler);
for (BitcodeFile *F : BitcodeFiles)
LTO->add(*F);

for (InputFile *File : LTO->compile()) {
DenseMap<CachedHashStringRef, const InputFile *> DummyGroups;
auto *Obj = cast<ObjFile<ELFT>>(File);
Obj->parse(DummyGroups);
for (Symbol *Sym : Obj->getGlobalSymbols())
Sym->parseSymbolVersion();
ObjectFiles.push_back(File);
}
}

// Set a flag for --trace-symbol so that we can print out a log message
// if a new symbol with the same name is inserted into the symbol table.
void SymbolTable::trace(StringRef Name) {
Expand Down Expand Up @@ -609,8 +586,3 @@ void elf::resolveSymbol(Symbol *Old, const Symbol &New) {
llvm_unreachable("bad symbol kind");
}
}

template void SymbolTable::addCombinedLTOObject<ELF32LE>();
template void SymbolTable::addCombinedLTOObject<ELF32BE>();
template void SymbolTable::addCombinedLTOObject<ELF64LE>();
template void SymbolTable::addCombinedLTOObject<ELF64BE>();
5 changes: 0 additions & 5 deletions lld/ELF/SymbolTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#define LLD_ELF_SYMBOL_TABLE_H

#include "InputFiles.h"
#include "LTO.h"
#include "lld/Common/Strings.h"
#include "llvm/ADT/CachedHashString.h"
#include "llvm/ADT/DenseMap.h"
Expand Down Expand Up @@ -40,7 +39,6 @@ class Undefined;
// is one add* function per symbol type.
class SymbolTable {
public:
template <class ELFT> void addCombinedLTOObject();
void wrap(Symbol *Sym, Symbol *Real, Symbol *Wrap);

ArrayRef<Symbol *> getSymbols() const { return SymVector; }
Expand Down Expand Up @@ -92,9 +90,6 @@ class SymbolTable {
// can have the same name. We use this map to handle "extern C++ {}"
// directive in version scripts.
llvm::Optional<llvm::StringMap<std::vector<Symbol *>>> DemangledSyms;

// For LTO.
std::unique_ptr<BitcodeCompiler> LTO;
};

extern SymbolTable *Symtab;
Expand Down

0 comments on commit 0baaf45

Please sign in to comment.