[ELF] Simplify adding default atoms

Now 'writer' creates an instance of `RuntimeFile` in the constructor, then
populates the file in the virtual function `addDefaultAtoms`, then pass
owning of this file to the caller of virtual function `createImplicitFiles`.

First, we do not need to keep an instance of `RuntimeFile` so long. It is
enough to create the file, right after that populate it and pass the owning.

Second, relationship between `createImplicitFiles` and `addDefaultAtoms`
is complicated. The `createImplicitFiles` might call `addDefaultAtoms`,
overridden version of `addDefaultAtoms` might call base class `addDefaultAtoms`,
and overridden version of `createImplicitFiles` might call base class
`createImplicitFiles` as well as `addDefaultAtoms`.

The patch solves both problems above. It creates and populates runtime files
right in the createImplicitFiles(), removes `addDefaultAtoms` at all and
does not keep references to runtime files in class fields.

llvm-svn: 234347
This commit is contained in:
Simon Atanasyan 2015-04-07 20:11:38 +00:00
parent f1853c65d9
commit 64e36b67e2
12 changed files with 48 additions and 105 deletions

View File

@ -30,10 +30,6 @@ protected:
return DynamicLibraryWriter<ELFT>::finalizeDefaultAtomValues();
}
void addDefaultAtoms() override {
return DynamicLibraryWriter<ELFT>::addDefaultAtoms();
}
private:
class GOTFile : public SimpleFile {
public:

View File

@ -29,10 +29,6 @@ protected:
return ExecutableWriter<ELFT>::finalizeDefaultAtomValues();
}
void addDefaultAtoms() override{
return ExecutableWriter<ELFT>::addDefaultAtoms();
}
private:
class GOTFile : public SimpleFile {
public:

View File

@ -33,10 +33,6 @@ protected:
void finalizeDefaultAtomValues() override;
void addDefaultAtoms() override {
ExecutableWriter<ELFT>::addDefaultAtoms();
}
/// \brief Create symbol table.
unique_bump_ptr<SymbolTable<ELFT>> createSymbolTable() override;

View File

@ -23,17 +23,12 @@ template<class ELFT>
class DynamicLibraryWriter : public OutputELFWriter<ELFT> {
public:
DynamicLibraryWriter(ELFLinkingContext &ctx, TargetLayout<ELFT> &layout)
: OutputELFWriter<ELFT>(ctx, layout),
_runtimeFile(new RuntimeFile<ELFT>(ctx, "C runtime")) {}
: OutputELFWriter<ELFT>(ctx, layout) {}
protected:
void buildDynamicSymbolTable(const File &file) override;
void addDefaultAtoms() override;
void createImplicitFiles(std::vector<std::unique_ptr<File>> &) override;
void finalizeDefaultAtomValues() override;
protected:
std::unique_ptr<RuntimeFile<ELFT> > _runtimeFile;
};
//===----------------------------------------------------------------------===//
@ -59,19 +54,15 @@ void DynamicLibraryWriter<ELFT>::buildDynamicSymbolTable(const File &file) {
OutputELFWriter<ELFT>::buildDynamicSymbolTable(file);
}
template <class ELFT> void DynamicLibraryWriter<ELFT>::addDefaultAtoms() {
OutputELFWriter<ELFT>::addDefaultAtoms();
_runtimeFile->addAbsoluteAtom("_end");
}
/// \brief Hook in lld to add CRuntime file
template <class ELFT>
void DynamicLibraryWriter<ELFT>::createImplicitFiles(
std::vector<std::unique_ptr<File> > &result) {
// Add the default atoms as defined by executables
DynamicLibraryWriter<ELFT>::addDefaultAtoms();
OutputELFWriter<ELFT>::createImplicitFiles(result);
result.push_back(std::move(_runtimeFile));
// Add the default atoms as defined by executables
auto file = llvm::make_unique<RuntimeFile<ELFT>>(this->_ctx, "C runtime");
file->addAbsoluteAtom("_end");
result.push_back(std::move(file));
}
template <class ELFT>

View File

@ -23,12 +23,10 @@ template<class ELFT>
class ExecutableWriter : public OutputELFWriter<ELFT> {
public:
ExecutableWriter(ELFLinkingContext &ctx, TargetLayout<ELFT> &layout)
: OutputELFWriter<ELFT>(ctx, layout),
_runtimeFile(new RuntimeFile<ELFT>(ctx, "C runtime")) {}
: OutputELFWriter<ELFT>(ctx, layout) {}
protected:
void buildDynamicSymbolTable(const File &file) override;
void addDefaultAtoms() override;
void createImplicitFiles(std::vector<std::unique_ptr<File>> &) override;
void finalizeDefaultAtomValues() override;
void createDefaultSections() override;
@ -38,7 +36,9 @@ protected:
}
unique_bump_ptr<InterpSection<ELFT>> _interpSection;
std::unique_ptr<RuntimeFile<ELFT> > _runtimeFile;
private:
std::unique_ptr<RuntimeFile<ELFT>> createRuntimeFile();
};
//===----------------------------------------------------------------------===//
@ -73,39 +73,36 @@ void ExecutableWriter<ELFT>::buildDynamicSymbolTable(const File &file) {
OutputELFWriter<ELFT>::buildDynamicSymbolTable(file);
}
/// \brief Add absolute symbols by default. These are linker added
/// absolute symbols
template<class ELFT>
void ExecutableWriter<ELFT>::addDefaultAtoms() {
OutputELFWriter<ELFT>::addDefaultAtoms();
_runtimeFile->addUndefinedAtom(this->_ctx.entrySymbolName());
_runtimeFile->addAbsoluteAtom("__bss_start");
_runtimeFile->addAbsoluteAtom("__bss_end");
_runtimeFile->addAbsoluteAtom("_end");
_runtimeFile->addAbsoluteAtom("end");
_runtimeFile->addAbsoluteAtom("__preinit_array_start");
_runtimeFile->addAbsoluteAtom("__preinit_array_end");
_runtimeFile->addAbsoluteAtom("__init_array_start");
_runtimeFile->addAbsoluteAtom("__init_array_end");
std::unique_ptr<RuntimeFile<ELFT>> ExecutableWriter<ELFT>::createRuntimeFile() {
auto file = llvm::make_unique<RuntimeFile<ELFT>>(this->_ctx, "C runtime");
file->addUndefinedAtom(this->_ctx.entrySymbolName());
file->addAbsoluteAtom("__bss_start");
file->addAbsoluteAtom("__bss_end");
file->addAbsoluteAtom("_end");
file->addAbsoluteAtom("end");
file->addAbsoluteAtom("__preinit_array_start");
file->addAbsoluteAtom("__preinit_array_end");
file->addAbsoluteAtom("__init_array_start");
file->addAbsoluteAtom("__init_array_end");
if (this->_ctx.isRelaOutputFormat()) {
_runtimeFile->addAbsoluteAtom("__rela_iplt_start");
_runtimeFile->addAbsoluteAtom("__rela_iplt_end");
file->addAbsoluteAtom("__rela_iplt_start");
file->addAbsoluteAtom("__rela_iplt_end");
} else {
_runtimeFile->addAbsoluteAtom("__rel_iplt_start");
_runtimeFile->addAbsoluteAtom("__rel_iplt_end");
file->addAbsoluteAtom("__rel_iplt_start");
file->addAbsoluteAtom("__rel_iplt_end");
}
_runtimeFile->addAbsoluteAtom("__fini_array_start");
_runtimeFile->addAbsoluteAtom("__fini_array_end");
file->addAbsoluteAtom("__fini_array_start");
file->addAbsoluteAtom("__fini_array_end");
return file;
}
/// \brief Hook in lld to add CRuntime file
template <class ELFT>
void ExecutableWriter<ELFT>::createImplicitFiles(
std::vector<std::unique_ptr<File> > &result) {
// Add the default atoms as defined by executables
ExecutableWriter<ELFT>::addDefaultAtoms();
OutputELFWriter<ELFT>::createImplicitFiles(result);
result.push_back(std::move(_runtimeFile));
result.push_back(createRuntimeFile());
}
template <class ELFT> void ExecutableWriter<ELFT>::createDefaultSections() {

View File

@ -37,29 +37,25 @@ protected:
}
private:
void addDefaultAtoms() override {
_runtimeFile->addAbsoluteAtom("_GLOBAL_OFFSET_TABLE_");
_runtimeFile->addAbsoluteAtom("_DYNAMIC");
}
HexagonLinkingContext &_ctx;
HexagonTargetLayout<ELFT> &_targetLayout;
std::unique_ptr<HexagonRuntimeFile<ELFT>> _runtimeFile;
};
template <class ELFT>
HexagonDynamicLibraryWriter<ELFT>::HexagonDynamicLibraryWriter(
HexagonLinkingContext &ctx, HexagonTargetLayout<ELFT> &layout)
: DynamicLibraryWriter<ELFT>(ctx, layout), _ctx(ctx), _targetLayout(layout),
_runtimeFile(new HexagonRuntimeFile<ELFT>(ctx)) {}
: DynamicLibraryWriter<ELFT>(ctx, layout), _ctx(ctx),
_targetLayout(layout) {}
template <class ELFT>
void HexagonDynamicLibraryWriter<ELFT>::createImplicitFiles(
std::vector<std::unique_ptr<File>> &result) {
DynamicLibraryWriter<ELFT>::createImplicitFiles(result);
// Add the default atoms as defined for hexagon
addDefaultAtoms();
result.push_back(std::move(_runtimeFile));
auto file = llvm::make_unique<HexagonRuntimeFile<ELFT>>(_ctx);
file->addAbsoluteAtom("_GLOBAL_OFFSET_TABLE_");
file->addAbsoluteAtom("_DYNAMIC");
result.push_back(std::move(file));
}
template <class ELFT>

View File

@ -37,32 +37,27 @@ protected:
}
private:
void addDefaultAtoms() override {
_runtimeFile->addAbsoluteAtom("_SDA_BASE_");
if (this->_ctx.isDynamic()) {
_runtimeFile->addAbsoluteAtom("_GLOBAL_OFFSET_TABLE_");
_runtimeFile->addAbsoluteAtom("_DYNAMIC");
}
}
HexagonLinkingContext &_ctx;
HexagonTargetLayout<ELFT> &_targetLayout;
std::unique_ptr<HexagonRuntimeFile<ELFT>> _runtimeFile;
};
template <class ELFT>
HexagonExecutableWriter<ELFT>::HexagonExecutableWriter(
HexagonLinkingContext &ctx, HexagonTargetLayout<ELFT> &layout)
: ExecutableWriter<ELFT>(ctx, layout), _ctx(ctx), _targetLayout(layout),
_runtimeFile(new HexagonRuntimeFile<ELFT>(ctx)) {}
: ExecutableWriter<ELFT>(ctx, layout), _ctx(ctx), _targetLayout(layout) {}
template <class ELFT>
void HexagonExecutableWriter<ELFT>::createImplicitFiles(
std::vector<std::unique_ptr<File>> &result) {
ExecutableWriter<ELFT>::createImplicitFiles(result);
// Add the default atoms as defined for hexagon
addDefaultAtoms();
result.push_back(std::move(_runtimeFile));
auto file = llvm::make_unique<HexagonRuntimeFile<ELFT>>(_ctx);
file->addAbsoluteAtom("_SDA_BASE_");
if (this->_ctx.isDynamic()) {
file->addAbsoluteAtom("_GLOBAL_OFFSET_TABLE_");
file->addAbsoluteAtom("_DYNAMIC");
}
result.push_back(std::move(file));
}
template <class ELFT>

View File

@ -129,9 +129,6 @@ protected:
// section header table, string table etc
virtual void assignSectionsWithNoSegments();
// Add default atoms that need to be present in the output file
virtual void addDefaultAtoms();
// Add any runtime files and their atoms to the output
void createImplicitFiles(std::vector<std::unique_ptr<File>> &) override;
@ -194,7 +191,6 @@ protected:
unique_bump_ptr<HashSection<ELFT>> _hashTable;
llvm::StringSet<> _soNeeded;
/// @}
std::unique_ptr<RuntimeFile<ELFT>> _scriptFile;
private:
static StringRef maybeGetSOName(Node *node);
@ -206,8 +202,7 @@ private:
template <class ELFT>
OutputELFWriter<ELFT>::OutputELFWriter(ELFLinkingContext &ctx,
TargetLayout<ELFT> &layout)
: _ctx(ctx), _targetHandler(ctx.getTargetHandler()), _layout(layout),
_scriptFile(new RuntimeFile<ELFT>(ctx, "Linker script runtime")) {}
: _ctx(ctx), _targetHandler(ctx.getTargetHandler()), _layout(layout) {}
template <class ELFT>
void OutputELFWriter<ELFT>::buildChunks(const File &file) {
@ -355,13 +350,6 @@ void OutputELFWriter<ELFT>::assignSectionsWithNoSegments() {
_shdrtab->updateSection(section);
}
template <class ELFT> void OutputELFWriter<ELFT>::addDefaultAtoms() {
const llvm::StringSet<> &symbols =
_ctx.linkerScriptSema().getScriptDefinedSymbols();
for (auto &sym : symbols)
_scriptFile->addAbsoluteAtom(sym.getKey());
}
template <class ELFT>
void OutputELFWriter<ELFT>::createImplicitFiles(
std::vector<std::unique_ptr<File>> &result) {
@ -373,7 +361,11 @@ void OutputELFWriter<ELFT>::createImplicitFiles(
_ctx.setUndefinesResolver(
llvm::make_unique<DynamicSymbolFile<ELFT>>(_ctx, std::move(callback)));
// Add script defined symbols
result.push_back(std::move(_scriptFile));
auto file =
llvm::make_unique<RuntimeFile<ELFT>>(_ctx, "Linker script runtime");
for (auto &sym : this->_ctx.linkerScriptSema().getScriptDefinedSymbols())
file->addAbsoluteAtom(sym.getKey());
result.push_back(std::move(file));
}
template <class ELFT>

View File

@ -28,10 +28,6 @@ protected:
return DynamicLibraryWriter<ELFT>::finalizeDefaultAtomValues();
}
void addDefaultAtoms() override {
return DynamicLibraryWriter<ELFT>::addDefaultAtoms();
}
private:
class GOTFile : public SimpleFile {
public:

View File

@ -28,10 +28,6 @@ protected:
return ExecutableWriter<ELFT>::finalizeDefaultAtomValues();
}
void addDefaultAtoms() override {
return ExecutableWriter<ELFT>::addDefaultAtoms();
}
private:
X86LinkingContext &_ctx;
TargetLayout<ELFT> &_layout;

View File

@ -30,10 +30,6 @@ protected:
return DynamicLibraryWriter::finalizeDefaultAtomValues();
}
void addDefaultAtoms() override {
return DynamicLibraryWriter::addDefaultAtoms();
}
private:
class GOTFile : public SimpleFile {
public:

View File

@ -37,10 +37,6 @@ protected:
return ExecutableWriter::finalizeDefaultAtomValues();
}
void addDefaultAtoms() override {
return ExecutableWriter::addDefaultAtoms();
}
private:
class GOTFile : public SimpleFile {
public: