Skip to content

Commit

Permalink
[ELF] - Make few members of Writer to be global and export them for r…
Browse files Browse the repository at this point in the history
…euse

Creating sections on linkerscript side requires some methods
that can be reused if are exported from writer.

Patch implements that change.

Differential revision: http://reviews.llvm.org/D20104

llvm-svn: 275162
  • Loading branch information
George Rimar committed Jul 12, 2016
1 parent e51f7f4 commit 5d53d1f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 33 deletions.
11 changes: 11 additions & 0 deletions lld/ELF/InputSection.cpp
Expand Up @@ -12,6 +12,7 @@
#include "EhFrame.h"
#include "Error.h"
#include "InputFiles.h"
#include "LinkerScript.h"
#include "OutputSections.h"
#include "Target.h"
#include "Thunks.h"
Expand All @@ -27,6 +28,11 @@ using namespace llvm::support::endian;
using namespace lld;
using namespace lld::elf;

template <class ELFT> bool elf::isDiscarded(InputSectionBase<ELFT> *S) {
return !S || S == &InputSection<ELFT>::Discarded || !S->Live ||
Script<ELFT>::X->isDiscarded(S);
}

template <class ELFT>
InputSectionBase<ELFT>::InputSectionBase(elf::ObjectFile<ELFT> *File,
const Elf_Shdr *Header,
Expand Down Expand Up @@ -644,6 +650,11 @@ bool MipsOptionsInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
return S->SectionKind == InputSectionBase<ELFT>::MipsOptions;
}

template bool elf::isDiscarded<ELF32LE>(InputSectionBase<ELF32LE> *);
template bool elf::isDiscarded<ELF32BE>(InputSectionBase<ELF32BE> *);
template bool elf::isDiscarded<ELF64LE>(InputSectionBase<ELF64LE> *);
template bool elf::isDiscarded<ELF64BE>(InputSectionBase<ELF64BE> *);

template class elf::InputSectionBase<ELF32LE>;
template class elf::InputSectionBase<ELF32BE>;
template class elf::InputSectionBase<ELF64LE>;
Expand Down
2 changes: 2 additions & 0 deletions lld/ELF/InputSection.h
Expand Up @@ -21,6 +21,8 @@
namespace lld {
namespace elf {

template <class ELFT> bool isDiscarded(InputSectionBase<ELFT> *S);

class SymbolBody;

template <class ELFT> class ICF;
Expand Down
56 changes: 24 additions & 32 deletions lld/ELF/Writer.cpp
Expand Up @@ -74,8 +74,6 @@ template <class ELFT> class Writer {
void writeHeader();
void writeSections();
void writeBuildId();
bool isDiscarded(InputSectionBase<ELFT> *IS) const;
StringRef getOutputSectionName(InputSectionBase<ELFT> *S) const;
bool needsInterpSection() const {
return !Symtab.getSharedFiles().empty() && !Config->DynamicLinker.empty();
}
Expand Down Expand Up @@ -103,6 +101,30 @@ template <class ELFT> class Writer {
};
} // anonymous namespace

template <class ELFT>
StringRef elf::getOutputSectionName(InputSectionBase<ELFT> *S) {
StringRef Dest = Script<ELFT>::X->getOutputSection(S);
if (!Dest.empty())
return Dest;

StringRef Name = S->getSectionName();
for (StringRef V : {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.",
".init_array.", ".fini_array.", ".ctors.", ".dtors.",
".tbss.", ".gcc_except_table.", ".tdata."})
if (Name.startswith(V))
return V.drop_back();
return Name;
}

template <class ELFT>
void elf::reportDiscarded(InputSectionBase<ELFT> *IS,
const std::unique_ptr<elf::ObjectFile<ELFT>> &File) {
if (!Config->PrintGcSections || !IS || IS->Live)
return;
errs() << "removing unused section from '" << IS->getSectionName()
<< "' in file '" << File->getName() << "'\n";
}

template <class ELFT> void elf::writeResult(SymbolTable<ELFT> *Symtab) {
typedef typename ELFT::uint uintX_t;
typedef typename ELFT::Ehdr Elf_Ehdr;
Expand Down Expand Up @@ -473,36 +495,6 @@ void Writer<ELFT>::addCommonSymbols(std::vector<DefinedCommon *> &Syms) {
Out<ELFT>::Bss->setSize(Off);
}

template <class ELFT>
StringRef Writer<ELFT>::getOutputSectionName(InputSectionBase<ELFT> *S) const {
StringRef Dest = Script<ELFT>::X->getOutputSection(S);
if (!Dest.empty())
return Dest;

StringRef Name = S->getSectionName();
for (StringRef V : {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.",
".init_array.", ".fini_array.", ".ctors.", ".dtors.",
".tbss.", ".gcc_except_table.", ".tdata."})
if (Name.startswith(V))
return V.drop_back();
return Name;
}

template <class ELFT>
void reportDiscarded(InputSectionBase<ELFT> *IS,
const std::unique_ptr<elf::ObjectFile<ELFT>> &File) {
if (!Config->PrintGcSections || !IS || IS->Live)
return;
llvm::errs() << "removing unused section from '" << IS->getSectionName()
<< "' in file '" << File->getName() << "'\n";
}

template <class ELFT>
bool Writer<ELFT>::isDiscarded(InputSectionBase<ELFT> *S) const {
return !S || S == &InputSection<ELFT>::Discarded || !S->Live ||
Script<ELFT>::X->isDiscarded(S);
}

template <class ELFT>
static Symbol *addOptionalSynthetic(SymbolTable<ELFT> &Table, StringRef Name,
OutputSectionBase<ELFT> *Sec,
Expand Down
16 changes: 15 additions & 1 deletion lld/ELF/Writer.h
Expand Up @@ -10,14 +10,28 @@
#ifndef LLD_ELF_WRITER_H
#define LLD_ELF_WRITER_H

#include <memory>

namespace llvm {
class StringRef;
}

namespace lld {
namespace elf {

template <class ELFT> class InputSectionBase;
template <class ELFT> class ObjectFile;
template <class ELFT> class SymbolTable;

template <class ELFT> void writeResult(SymbolTable<ELFT> *Symtab);

template <class ELFT> void markLive();

template <class ELFT>
llvm::StringRef getOutputSectionName(InputSectionBase<ELFT> *S);

template <class ELFT>
void reportDiscarded(InputSectionBase<ELFT> *IS,
const std::unique_ptr<elf::ObjectFile<ELFT>> &File);
}
}

Expand Down

0 comments on commit 5d53d1f

Please sign in to comment.