#include "llvm/MC/MCTargetOptions.h"
#include "llvm/Object/ELFObjectFile.h"
#include "llvm/Support/Compression.h"
+#include "llvm/Support/Errc.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileOutputBuffer.h"
#include "llvm/Support/Path.h"
Phdr.p_align = Seg.Align;
}
-void SectionBase::removeSectionReferences(const SectionBase *Sec) {}
-void SectionBase::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {}
+Error SectionBase::removeSectionReferences(const SectionBase *Sec) {
+ return Error::success();
+}
+
+Error SectionBase::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
+ return Error::success();
+}
+
void SectionBase::initialize(SectionTableRef SecTable) {}
void SectionBase::finalize() {}
void SectionBase::markSymbols() {}
Size += this->EntrySize;
}
-void SymbolTableSection::removeSectionReferences(const SectionBase *Sec) {
+Error SymbolTableSection::removeSectionReferences(const SectionBase *Sec) {
if (SectionIndexTable == Sec)
SectionIndexTable = nullptr;
if (SymbolNames == Sec) {
- error("String table " + SymbolNames->Name +
- " cannot be removed because it is referenced by the symbol table " +
- this->Name);
+ return createStringError(llvm::errc::invalid_argument,
+ "String table %s cannot be removed because it is "
+ "referenced by the symbol table %s",
+ SymbolNames->Name.data(), this->Name.data());
}
- removeSymbols([Sec](const Symbol &Sym) { return Sym.DefinedIn == Sec; });
+ return removeSymbols(
+ [Sec](const Symbol &Sym) { return Sym.DefinedIn == Sec; });
}
void SymbolTableSection::updateSymbols(function_ref<void(Symbol &)> Callable) {
assignIndices();
}
-void SymbolTableSection::removeSymbols(
+Error SymbolTableSection::removeSymbols(
function_ref<bool(const Symbol &)> ToRemove) {
Symbols.erase(
std::remove_if(std::begin(Symbols) + 1, std::end(Symbols),
std::end(Symbols));
Size = Symbols.size() * EntrySize;
assignIndices();
+ return Error::success();
}
void SymbolTableSection::initialize(SectionTableRef SecTable) {
}
template <class SymTabType>
-void RelocSectionWithSymtabBase<SymTabType>::removeSectionReferences(
+Error RelocSectionWithSymtabBase<SymTabType>::removeSectionReferences(
const SectionBase *Sec) {
- if (Symbols == Sec) {
- error("Symbol table " + Symbols->Name +
- " cannot be removed because it is "
- "referenced by the relocation "
- "section " +
- this->Name);
- }
+ if (Symbols == Sec)
+ return createStringError(llvm::errc::invalid_argument,
+ "Symbol table %s cannot be removed because it is "
+ "referenced by the relocation section %s.",
+ Symbols->Name.data(), this->Name.data());
+ return Error::success();
}
template <class SymTabType>
Visitor.visit(*this);
}
-void RelocationSection::removeSymbols(
+Error RelocationSection::removeSymbols(
function_ref<bool(const Symbol &)> ToRemove) {
for (const Relocation &Reloc : Relocations)
if (ToRemove(*Reloc.RelocSymbol))
- error("not stripping symbol '" + Reloc.RelocSymbol->Name +
- "' because it is named in a relocation");
+ return createStringError(
+ llvm::errc::invalid_argument,
+ "not stripping symbol '%s' because it is named in a relocation.",
+ Reloc.RelocSymbol->Name.data());
+ return Error::success();
}
void RelocationSection::markSymbols() {
Visitor.visit(*this);
}
-void Section::removeSectionReferences(const SectionBase *Sec) {
- if (LinkSection == Sec) {
- error("Section " + LinkSection->Name +
- " cannot be removed because it is "
- "referenced by the section " +
- this->Name);
- }
+Error Section::removeSectionReferences(const SectionBase *Sec) {
+ if (LinkSection == Sec)
+ return createStringError(llvm::errc::invalid_argument,
+ "Section %s cannot be removed because it is "
+ "referenced by the section %s",
+ LinkSection->Name.data(), this->Name.data());
+ return Error::success();
}
void GroupSection::finalize() {
this->Link = SymTab->Index;
}
-void GroupSection::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
- if (ToRemove(*Sym)) {
- error("Symbol " + Sym->Name +
- " cannot be removed because it is "
- "referenced by the section " +
- this->Name + "[" + Twine(this->Index) + "]");
- }
+Error GroupSection::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
+ if (ToRemove(*Sym))
+ return createStringError(llvm::errc::invalid_argument,
+ "Symbol %s cannot be removed because it is "
+ "referenced by the section %s[%d].",
+ Sym->Name.data(), this->Name.data(), this->Index);
+ return Error::success();
}
void GroupSection::markSymbols() {
Sec.accept(*SecWriter);
}
-void Object::removeSections(std::function<bool(const SectionBase &)> ToRemove) {
+Error Object::removeSections(
+ std::function<bool(const SectionBase &)> ToRemove) {
auto Iter = std::stable_partition(
std::begin(Sections), std::end(Sections), [=](const SecPtr &Sec) {
for (auto &Segment : Segments)
Segment->removeSection(RemoveSec.get());
for (auto &KeepSec : make_range(std::begin(Sections), Iter))
- KeepSec->removeSectionReferences(RemoveSec.get());
+ if (Error E = KeepSec->removeSectionReferences(RemoveSec.get()))
+ return E;
}
// Now finally get rid of them all togethor.
Sections.erase(Iter, std::end(Sections));
+ return Error::success();
}
-void Object::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
- if (!SymbolTable)
- return;
-
- for (const SecPtr &Sec : Sections)
- Sec->removeSymbols(ToRemove);
+Error Object::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
+ if (SymbolTable)
+ for (const SecPtr &Sec : Sections)
+ if (Error E = Sec->removeSymbols(ToRemove))
+ return E;
+ return Error::success();
}
void Object::sortSections() {
// a section header table output. We need to throw an error if a user tries
// to do that.
if (Obj.SectionNames == nullptr && WriteSectionHeaders)
- error("Cannot write section header table because section header string "
- "table was removed.");
+ return createStringError(llvm::errc::invalid_argument,
+ "Cannot write section header table because "
+ "section header string table was removed.");
Obj.sortSections();
// Since we don't need SectionIndexTable we should remove it and all
// references to it.
if (Obj.SectionIndexTable != nullptr) {
- Obj.removeSections([this](const SectionBase &Sec) {
- return &Sec == Obj.SectionIndexTable;
- });
+ if (Error E = Obj.removeSections([this](const SectionBase &Sec) {
+ return &Sec == Obj.SectionIndexTable;
+ }))
+ return E;
}
}
virtual void initialize(SectionTableRef SecTable);
virtual void finalize();
- virtual void removeSectionReferences(const SectionBase *Sec);
- virtual void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
+ virtual Error removeSectionReferences(const SectionBase *Sec);
+ virtual Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
virtual void accept(SectionVisitor &Visitor) const = 0;
virtual void accept(MutableSectionVisitor &Visitor) = 0;
virtual void markSymbols();
void accept(SectionVisitor &Visitor) const override;
void accept(MutableSectionVisitor &Visitor) override;
- void removeSectionReferences(const SectionBase *Sec) override;
+ Error removeSectionReferences(const SectionBase *Sec) override;
void initialize(SectionTableRef SecTable) override;
void finalize() override;
};
Symbol *getSymbolByIndex(uint32_t Index);
void updateSymbols(function_ref<void(Symbol &)> Callable);
- void removeSectionReferences(const SectionBase *Sec) override;
+ Error removeSectionReferences(const SectionBase *Sec) override;
void initialize(SectionTableRef SecTable) override;
void finalize() override;
void accept(SectionVisitor &Visitor) const override;
void accept(MutableSectionVisitor &Visitor) override;
- void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
+ Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
static bool classof(const SectionBase *S) {
return S->Type == ELF::SHT_SYMTAB;
RelocSectionWithSymtabBase() = default;
public:
- void removeSectionReferences(const SectionBase *Sec) override;
+ Error removeSectionReferences(const SectionBase *Sec) override;
void initialize(SectionTableRef SecTable) override;
void finalize() override;
};
void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
void accept(SectionVisitor &Visitor) const override;
void accept(MutableSectionVisitor &Visitor) override;
- void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
+ Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
void markSymbols() override;
static bool classof(const SectionBase *S) {
void accept(SectionVisitor &) const override;
void accept(MutableSectionVisitor &Visitor) override;
void finalize() override;
- void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
+ Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
void markSymbols() override;
static bool classof(const SectionBase *S) {
Range<Segment> segments() { return make_pointee_range(Segments); }
ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
- void removeSections(std::function<bool(const SectionBase &)> ToRemove);
- void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
+ Error removeSections(std::function<bool(const SectionBase &)> ToRemove);
+ Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
template <class T, class... Ts> T &addSection(Ts &&... Args) {
auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
auto Ptr = Sec.get();