Skip to content

Commit

Permalink
Automerge: Revert "[llvm-objcopy][ELF] Add an option to remove notes …
Browse files Browse the repository at this point in the history
…(#118739)"

This reverts commit 9324e6a.
  • Loading branch information
igorkudrin authored and github-actions[bot] committed Jan 23, 2025
2 parents e9b1d63 + 621e5cd commit 09ff133
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 422 deletions.
5 changes: 0 additions & 5 deletions llvm/docs/CommandGuide/llvm-objcopy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -477,11 +477,6 @@ them.

Preserve access and modification timestamps in the output.

.. option:: --remove-note [<name>/]<type>

Remove notes of integer type ``<type>`` and name ``<name>`` from SHT_NOTE
sections that are not in a segment. Can be specified multiple times.

.. option:: --rename-section <old>=<new>[,<flag>,...]

Rename sections called ``<old>`` to ``<new>`` in the output, and apply any
Expand Down
5 changes: 0 additions & 5 deletions llvm/include/llvm/ObjCopy/CommonConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,6 @@ struct CommonConfig {

SmallVector<std::pair<NameMatcher, llvm::DebugCompressionType>, 0>
compressSections;

// ErrorCallback is used to handle recoverable errors. An Error returned
// by the callback aborts the execution and is then returned to the caller.
// If the callback is not set, the errors are not issued.
std::function<Error(Error)> ErrorCallback;
};

} // namespace objcopy
Expand Down
9 changes: 0 additions & 9 deletions llvm/include/llvm/ObjCopy/ELF/ELFConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
namespace llvm {
namespace objcopy {

// Note to remove info specified by --remove-note option.
struct RemoveNoteInfo {
StringRef Name;
uint32_t TypeId;
};

// ELF specific configuration for copying/stripping a single file.
struct ELFConfig {
uint8_t NewSymbolVisibility = (uint8_t)ELF::STV_DEFAULT;
Expand All @@ -37,9 +31,6 @@ struct ELFConfig {
bool KeepFileSymbols = false;
bool LocalizeHidden = false;
bool VerifyNoteSections = true;

// Notes specified by --remove-note option.
SmallVector<RemoveNoteInfo, 0> NotesToRemove;
};

} // namespace objcopy
Expand Down
112 changes: 0 additions & 112 deletions llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,112 +609,6 @@ static void addSymbol(Object &Obj, const NewSymbolInfo &SymInfo,
Sec ? (uint16_t)SYMBOL_SIMPLE_INDEX : (uint16_t)SHN_ABS, 0);
}

namespace {
struct RemoveNoteDetail {
struct DeletedRange {
uint64_t OldFrom;
uint64_t OldTo;
};

template <class ELFT>
static std::vector<DeletedRange>
findNotesToRemove(ArrayRef<uint8_t> Data, size_t Align,
ArrayRef<RemoveNoteInfo> NotesToRemove);
static std::vector<uint8_t> updateData(ArrayRef<uint8_t> OldData,
ArrayRef<DeletedRange> ToRemove);
};
} // namespace

template <class ELFT>
std::vector<RemoveNoteDetail::DeletedRange>
RemoveNoteDetail::findNotesToRemove(ArrayRef<uint8_t> Data, size_t Align,
ArrayRef<RemoveNoteInfo> NotesToRemove) {
LLVM_ELF_IMPORT_TYPES_ELFT(ELFT);
std::vector<DeletedRange> ToRemove;
uint64_t CurPos = 0;
while (CurPos + sizeof(Elf_Nhdr) <= Data.size()) {
auto Nhdr = reinterpret_cast<const Elf_Nhdr *>(Data.data() + CurPos);
size_t FullSize = Nhdr->getSize(Align);
if (CurPos + FullSize > Data.size())
break;
Elf_Note Note(*Nhdr);
bool ShouldRemove =
llvm::any_of(NotesToRemove, [&Note](const RemoveNoteInfo &NoteInfo) {
return NoteInfo.TypeId == Note.getType() &&
(NoteInfo.Name.empty() || NoteInfo.Name == Note.getName());
});
if (ShouldRemove)
ToRemove.push_back({CurPos, CurPos + FullSize});
CurPos += FullSize;
}
return ToRemove;
}

std::vector<uint8_t>
RemoveNoteDetail::updateData(ArrayRef<uint8_t> OldData,
ArrayRef<DeletedRange> ToRemove) {
std::vector<uint8_t> NewData;
NewData.reserve(OldData.size());
uint64_t CurPos = 0;
for (const DeletedRange &RemRange : ToRemove) {
if (CurPos < RemRange.OldFrom) {
auto Slice = OldData.slice(CurPos, RemRange.OldFrom - CurPos);
NewData.insert(NewData.end(), Slice.begin(), Slice.end());
}
CurPos = RemRange.OldTo;
}
if (CurPos < OldData.size()) {
auto Slice = OldData.slice(CurPos);
NewData.insert(NewData.end(), Slice.begin(), Slice.end());
}
return NewData;
}

static Error removeNotes(Object &Obj, endianness Endianness,
ArrayRef<RemoveNoteInfo> NotesToRemove,
function_ref<Error(Error)> ErrorCallback) {
// TODO: Support note segments.
if (ErrorCallback) {
for (Segment &Seg : Obj.segments()) {
if (Seg.Type == PT_NOTE) {
if (Error E = ErrorCallback(createStringError(
errc::not_supported, "note segments are not supported")))
return E;
break;
}
}
}
for (auto &Sec : Obj.sections()) {
if (Sec.Type != SHT_NOTE || !Sec.hasContents())
continue;
// TODO: Support note sections in segments.
if (Sec.ParentSegment) {
if (ErrorCallback)
if (Error E = ErrorCallback(createStringError(
errc::not_supported,
"cannot remove note(s) from " + Sec.Name +
": sections in segments are not supported")))
return E;
continue;
}
ArrayRef<uint8_t> OldData = Sec.getContents();
size_t Align = std::max<size_t>(4, Sec.Align);
// Note: notes for both 32-bit and 64-bit ELF files use 4-byte words in the
// header, so the parsers are the same.
auto ToRemove = (Endianness == endianness::little)
? RemoveNoteDetail::findNotesToRemove<ELF64LE>(
OldData, Align, NotesToRemove)
: RemoveNoteDetail::findNotesToRemove<ELF64BE>(
OldData, Align, NotesToRemove);
if (!ToRemove.empty()) {
if (Error E = Obj.updateSectionData(
Sec, RemoveNoteDetail::updateData(OldData, ToRemove)))
return E;
}
}
return Error::success();
}

static Error
handleUserSection(const NewSectionInfo &NewSection,
function_ref<Error(StringRef, ArrayRef<uint8_t>)> F) {
Expand Down Expand Up @@ -905,12 +799,6 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
? endianness::little
: endianness::big;

if (!ELFConfig.NotesToRemove.empty()) {
if (Error Err =
removeNotes(Obj, E, ELFConfig.NotesToRemove, Config.ErrorCallback))
return Err;
}

for (const NewSectionInfo &AddedSection : Config.AddSection) {
auto AddSection = [&](StringRef Name, ArrayRef<uint8_t> Data) -> Error {
OwnedDataSection &NewSection =
Expand Down
41 changes: 16 additions & 25 deletions llvm/lib/ObjCopy/ELF/ELFObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2154,46 +2154,37 @@ ELFWriter<ELFT>::ELFWriter(Object &Obj, raw_ostream &Buf, bool WSH,
: Writer(Obj, Buf), WriteSectionHeaders(WSH && Obj.HadShdrs),
OnlyKeepDebug(OnlyKeepDebug) {}

Error Object::updateSectionData(SecPtr &Sec, ArrayRef<uint8_t> Data) {
if (!Sec->hasContents())
Error Object::updateSection(StringRef Name, ArrayRef<uint8_t> Data) {
auto It = llvm::find_if(Sections,
[&](const SecPtr &Sec) { return Sec->Name == Name; });
if (It == Sections.end())
return createStringError(errc::invalid_argument, "section '%s' not found",
Name.str().c_str());

auto *OldSec = It->get();
if (!OldSec->hasContents())
return createStringError(
errc::invalid_argument,
"section '%s' cannot be updated because it does not have contents",
Sec->Name.c_str());
Name.str().c_str());

if (Data.size() > Sec->Size && Sec->ParentSegment)
if (Data.size() > OldSec->Size && OldSec->ParentSegment)
return createStringError(errc::invalid_argument,
"cannot fit data of size %zu into section '%s' "
"with size %" PRIu64 " that is part of a segment",
Data.size(), Sec->Name.c_str(), Sec->Size);
Data.size(), Name.str().c_str(), OldSec->Size);

if (!Sec->ParentSegment) {
Sec = std::make_unique<OwnedDataSection>(*Sec, Data);
if (!OldSec->ParentSegment) {
*It = std::make_unique<OwnedDataSection>(*OldSec, Data);
} else {
// The segment writer will be in charge of updating these contents.
Sec->Size = Data.size();
UpdatedSections[Sec.get()] = Data;
OldSec->Size = Data.size();
UpdatedSections[OldSec] = Data;
}

return Error::success();
}

Error Object::updateSection(StringRef Name, ArrayRef<uint8_t> Data) {
auto It = llvm::find_if(Sections,
[&](const SecPtr &Sec) { return Sec->Name == Name; });
if (It == Sections.end())
return createStringError(errc::invalid_argument, "section '%s' not found",
Name.str().c_str());
return updateSectionData(*It, Data);
}

Error Object::updateSectionData(SectionBase &S, ArrayRef<uint8_t> Data) {
auto It = llvm::find_if(Sections,
[&](const SecPtr &Sec) { return Sec.get() == &S; });
assert(It != Sections.end() && "The section should belong to the object");
return updateSectionData(*It, Data);
}

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

Expand Down
7 changes: 0 additions & 7 deletions llvm/lib/ObjCopy/ELF/ELFObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,6 @@ class SectionBase {
virtual void
replaceSectionReferences(const DenseMap<SectionBase *, SectionBase *> &);
virtual bool hasContents() const { return false; }
virtual ArrayRef<uint8_t> getContents() const { return {}; }
// Notify the section that it is subject to removal.
virtual void onRemove();

Expand Down Expand Up @@ -620,8 +619,6 @@ class Section : public SectionBase {
bool hasContents() const override {
return Type != ELF::SHT_NOBITS && Type != ELF::SHT_NULL;
}
ArrayRef<uint8_t> getContents() const override { return Contents; }

void restoreSymTabLink(SymbolTableSection &SymTab) override;
};

Expand Down Expand Up @@ -657,7 +654,6 @@ class OwnedDataSection : public SectionBase {
Error accept(SectionVisitor &Sec) const override;
Error accept(MutableSectionVisitor &Visitor) override;
bool hasContents() const override { return true; }
ArrayRef<uint8_t> getContents() const override { return Data; }
};

class CompressedSection : public SectionBase {
Expand Down Expand Up @@ -1168,8 +1164,6 @@ class Object {
return Sec.Flags & ELF::SHF_ALLOC;
};

Error updateSectionData(SecPtr &Sec, ArrayRef<uint8_t> Data);

public:
template <class T>
using ConstRange = iterator_range<pointee_iterator<
Expand Down Expand Up @@ -1212,7 +1206,6 @@ class Object {

const auto &getUpdatedSections() const { return UpdatedSections; }
Error updateSection(StringRef Name, ArrayRef<uint8_t> Data);
Error updateSectionData(SectionBase &S, ArrayRef<uint8_t> Data);

SectionBase *findSection(StringRef Name) {
auto SecIt =
Expand Down
Loading

0 comments on commit 09ff133

Please sign in to comment.