Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions llvm/docs/CommandGuide/llvm-objcopy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ multiple file formats.
specified ``<flag>`` values. Can be specified multiple times to update multiple
sections.

.. option:: --verbose

List all object files modified.

Supported flag names are `alloc`, `load`, `noload`, `readonly`, `exclude`,
`debug`, `code`, `data`, `rom`, `share`, `contents`, `merge`, `strings`, and
`large`. Not all flags are meaningful for all object file formats or target
Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/ObjCopy/CommonConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ struct CommonConfig {
bool StripNonAlloc = false;
bool StripSections = false;
bool StripUnneeded = false;
bool Verbose = false;
bool Weaken = false;
bool DecompressDebugSections = false;

Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ static Error replaceAndRemoveSections(const CommonConfig &Config,
};
}

if (Error E = Obj.removeSections(ELFConfig.AllowBrokenLinks, RemovePred))
if (Error E = Obj.removeSections(ELFConfig.AllowBrokenLinks, RemovePred, Config.Verbose))
return E;

if (Error E = Obj.compressOrDecompressSections(Config))
Expand Down Expand Up @@ -782,6 +782,7 @@ static Error verifyNoteSection(StringRef Name, endianness Endianness,
// system. The only priority is that keeps/copies overrule removes.
static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
ElfType OutputElfType, Object &Obj) {
Obj.VerboseOutput = Config.Verbose;
if (Config.OutputArch) {
Obj.Machine = Config.OutputArch->EMachine;
Obj.OSABI = Config.OutputArch->OSABI;
Expand All @@ -790,7 +791,7 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
if (!Config.SplitDWO.empty() && Config.ExtractDWO) {
return Obj.removeSections(
ELFConfig.AllowBrokenLinks,
[&Obj](const SectionBase &Sec) { return onlyKeepDWOPred(Obj, Sec); });
[&Obj](const SectionBase &Sec) { return onlyKeepDWOPred(Obj, Sec); }, Config.Verbose);
}

// Dump sections before add/remove for compatibility with GNU objcopy.
Expand Down
30 changes: 21 additions & 9 deletions llvm/lib/ObjCopy/ELF/ELFObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -766,14 +766,20 @@ Error SymbolTableSection::removeSymbols(
function_ref<bool(const Symbol &)> ToRemove) {
Symbols.erase(
std::remove_if(std::begin(Symbols) + 1, std::end(Symbols),
[ToRemove](const SymPtr &Sym) { return ToRemove(*Sym); }),
std::end(Symbols));
[&](const SymPtr &Sym) {
if (ToRemove(*Sym)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need for the braces here (see other comment about coding standards).

if(VerboseOutput)
outs() << "Symbols Removed:" << Sym->Name<< "\n";
return true;
}
return false;
}));

auto PrevSize = Size;
Size = Symbols.size() * EntrySize;
if (Size < PrevSize)
IndicesChanged = true;
assignIndices();
return Error::success();
}

void SymbolTableSection::replaceSectionReferences(
Expand Down Expand Up @@ -2195,7 +2201,7 @@ Error Object::updateSectionData(SectionBase &S, ArrayRef<uint8_t> Data) {
}

Error Object::removeSections(
bool AllowBrokenLinks, std::function<bool(const SectionBase &)> ToRemove) {
bool AllowBrokenLinks, std::function<bool(const SectionBase &)> ToRemove, bool VerboseOutput) {

auto Iter = std::stable_partition(
std::begin(Sections), std::end(Sections), [=](const SecPtr &Sec) {
Expand Down Expand Up @@ -2230,6 +2236,9 @@ Error Object::removeSections(
for (auto &RemoveSec : make_range(Iter, std::end(Sections))) {
for (auto &Segment : Segments)
Segment->removeSection(RemoveSec.get());
if (VerboseOutput) {
outs() << "removed section: " << (RemoveSec.get()->Name);
}
RemoveSec->onRemove();
RemoveSections.insert(RemoveSec.get());
}
Expand Down Expand Up @@ -2273,17 +2282,20 @@ Error Object::replaceSections(

if (Error E = removeSections(
/*AllowBrokenLinks=*/false,
[=](const SectionBase &Sec) { return FromTo.count(&Sec) > 0; }))
[=](const SectionBase &Sec) { return FromTo.count(&Sec) > 0; }, false))
return E;
llvm::sort(Sections, SectionIndexLess);
return Error::success();
}

Error Object::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
if (SymbolTable)
for (const SecPtr &Sec : Sections)
if (SymbolTable){
for (const SecPtr &Sec : Sections){
if (Error E = Sec->removeSymbols(ToRemove))
return E;
outs() << "removed symbols:" << Sec->Name;
}
}
return Error::success();
}

Expand Down Expand Up @@ -2570,7 +2582,7 @@ static Error removeUnneededSections(Object &Obj) {
: Obj.SymbolTable->getStrTab();
return Obj.removeSections(false, [&](const SectionBase &Sec) {
return &Sec == Obj.SymbolTable || &Sec == StrTab;
});
}, false);
}

template <class ELFT> Error ELFWriter<ELFT>::finalize() {
Expand Down Expand Up @@ -2624,7 +2636,7 @@ template <class ELFT> Error ELFWriter<ELFT>::finalize() {
if (Error E = Obj.removeSections(false /*AllowBrokenLinks*/,
[this](const SectionBase &Sec) {
return &Sec == Obj.SectionIndexTable;
}))
}, false))
return E;
}
}
Expand Down
6 changes: 5 additions & 1 deletion llvm/lib/ObjCopy/ELF/ELFObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,8 @@ class SymbolTableSection : public SectionBase {
void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
void assignIndices();

private:
bool VerboseOutput;
protected:
std::vector<std::unique_ptr<Symbol>> Symbols;
StringTableSection *SymbolNames = nullptr;
Expand Down Expand Up @@ -856,6 +858,7 @@ class SymbolTableSection : public SectionBase {
static bool classof(const SectionBase *S) {
return S->OriginalType == ELF::SHT_SYMTAB;
}
bool getVerboseOutput() { return VerboseOutput; }
};

struct Relocation {
Expand Down Expand Up @@ -1195,6 +1198,7 @@ class Object {
uint32_t Flags;

bool HadShdrs = true;
bool VerboseOutput;
bool MustBeRelocatable = false;
StringTableSection *SectionNames = nullptr;
SymbolTableSection *SymbolTable = nullptr;
Expand Down Expand Up @@ -1224,7 +1228,7 @@ class Object {
ConstRange<Segment> segments() const { return make_pointee_range(Segments); }

Error removeSections(bool AllowBrokenLinks,
std::function<bool(const SectionBase &)> ToRemove);
std::function<bool(const SectionBase &)> ToRemove, bool VerboseOutput);
Error compressOrDecompressSections(const CommonConfig &Config);
Error replaceSections(const DenseMap<SectionBase *, SectionBase *> &FromTo);
Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Expand Down
Loading