Skip to content

Commit 621e5cd

Browse files
committed
Revert "[llvm-objcopy][ELF] Add an option to remove notes (#118739)"
This reverts commit 9324e6a.
1 parent 4f03258 commit 621e5cd

File tree

10 files changed

+16
-422
lines changed

10 files changed

+16
-422
lines changed

llvm/docs/CommandGuide/llvm-objcopy.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -477,11 +477,6 @@ them.
477477

478478
Preserve access and modification timestamps in the output.
479479

480-
.. option:: --remove-note [<name>/]<type>
481-
482-
Remove notes of integer type ``<type>`` and name ``<name>`` from SHT_NOTE
483-
sections that are not in a segment. Can be specified multiple times.
484-
485480
.. option:: --rename-section <old>=<new>[,<flag>,...]
486481

487482
Rename sections called ``<old>`` to ``<new>`` in the output, and apply any

llvm/include/llvm/ObjCopy/CommonConfig.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,6 @@ struct CommonConfig {
281281

282282
SmallVector<std::pair<NameMatcher, llvm::DebugCompressionType>, 0>
283283
compressSections;
284-
285-
// ErrorCallback is used to handle recoverable errors. An Error returned
286-
// by the callback aborts the execution and is then returned to the caller.
287-
// If the callback is not set, the errors are not issued.
288-
std::function<Error(Error)> ErrorCallback;
289284
};
290285

291286
} // namespace objcopy

llvm/include/llvm/ObjCopy/ELF/ELFConfig.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@
1515
namespace llvm {
1616
namespace objcopy {
1717

18-
// Note to remove info specified by --remove-note option.
19-
struct RemoveNoteInfo {
20-
StringRef Name;
21-
uint32_t TypeId;
22-
};
23-
2418
// ELF specific configuration for copying/stripping a single file.
2519
struct ELFConfig {
2620
uint8_t NewSymbolVisibility = (uint8_t)ELF::STV_DEFAULT;
@@ -37,9 +31,6 @@ struct ELFConfig {
3731
bool KeepFileSymbols = false;
3832
bool LocalizeHidden = false;
3933
bool VerifyNoteSections = true;
40-
41-
// Notes specified by --remove-note option.
42-
SmallVector<RemoveNoteInfo, 0> NotesToRemove;
4334
};
4435

4536
} // namespace objcopy

llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp

Lines changed: 0 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -609,112 +609,6 @@ static void addSymbol(Object &Obj, const NewSymbolInfo &SymInfo,
609609
Sec ? (uint16_t)SYMBOL_SIMPLE_INDEX : (uint16_t)SHN_ABS, 0);
610610
}
611611

612-
namespace {
613-
struct RemoveNoteDetail {
614-
struct DeletedRange {
615-
uint64_t OldFrom;
616-
uint64_t OldTo;
617-
};
618-
619-
template <class ELFT>
620-
static std::vector<DeletedRange>
621-
findNotesToRemove(ArrayRef<uint8_t> Data, size_t Align,
622-
ArrayRef<RemoveNoteInfo> NotesToRemove);
623-
static std::vector<uint8_t> updateData(ArrayRef<uint8_t> OldData,
624-
ArrayRef<DeletedRange> ToRemove);
625-
};
626-
} // namespace
627-
628-
template <class ELFT>
629-
std::vector<RemoveNoteDetail::DeletedRange>
630-
RemoveNoteDetail::findNotesToRemove(ArrayRef<uint8_t> Data, size_t Align,
631-
ArrayRef<RemoveNoteInfo> NotesToRemove) {
632-
LLVM_ELF_IMPORT_TYPES_ELFT(ELFT);
633-
std::vector<DeletedRange> ToRemove;
634-
uint64_t CurPos = 0;
635-
while (CurPos + sizeof(Elf_Nhdr) <= Data.size()) {
636-
auto Nhdr = reinterpret_cast<const Elf_Nhdr *>(Data.data() + CurPos);
637-
size_t FullSize = Nhdr->getSize(Align);
638-
if (CurPos + FullSize > Data.size())
639-
break;
640-
Elf_Note Note(*Nhdr);
641-
bool ShouldRemove =
642-
llvm::any_of(NotesToRemove, [&Note](const RemoveNoteInfo &NoteInfo) {
643-
return NoteInfo.TypeId == Note.getType() &&
644-
(NoteInfo.Name.empty() || NoteInfo.Name == Note.getName());
645-
});
646-
if (ShouldRemove)
647-
ToRemove.push_back({CurPos, CurPos + FullSize});
648-
CurPos += FullSize;
649-
}
650-
return ToRemove;
651-
}
652-
653-
std::vector<uint8_t>
654-
RemoveNoteDetail::updateData(ArrayRef<uint8_t> OldData,
655-
ArrayRef<DeletedRange> ToRemove) {
656-
std::vector<uint8_t> NewData;
657-
NewData.reserve(OldData.size());
658-
uint64_t CurPos = 0;
659-
for (const DeletedRange &RemRange : ToRemove) {
660-
if (CurPos < RemRange.OldFrom) {
661-
auto Slice = OldData.slice(CurPos, RemRange.OldFrom - CurPos);
662-
NewData.insert(NewData.end(), Slice.begin(), Slice.end());
663-
}
664-
CurPos = RemRange.OldTo;
665-
}
666-
if (CurPos < OldData.size()) {
667-
auto Slice = OldData.slice(CurPos);
668-
NewData.insert(NewData.end(), Slice.begin(), Slice.end());
669-
}
670-
return NewData;
671-
}
672-
673-
static Error removeNotes(Object &Obj, endianness Endianness,
674-
ArrayRef<RemoveNoteInfo> NotesToRemove,
675-
function_ref<Error(Error)> ErrorCallback) {
676-
// TODO: Support note segments.
677-
if (ErrorCallback) {
678-
for (Segment &Seg : Obj.segments()) {
679-
if (Seg.Type == PT_NOTE) {
680-
if (Error E = ErrorCallback(createStringError(
681-
errc::not_supported, "note segments are not supported")))
682-
return E;
683-
break;
684-
}
685-
}
686-
}
687-
for (auto &Sec : Obj.sections()) {
688-
if (Sec.Type != SHT_NOTE || !Sec.hasContents())
689-
continue;
690-
// TODO: Support note sections in segments.
691-
if (Sec.ParentSegment) {
692-
if (ErrorCallback)
693-
if (Error E = ErrorCallback(createStringError(
694-
errc::not_supported,
695-
"cannot remove note(s) from " + Sec.Name +
696-
": sections in segments are not supported")))
697-
return E;
698-
continue;
699-
}
700-
ArrayRef<uint8_t> OldData = Sec.getContents();
701-
size_t Align = std::max<size_t>(4, Sec.Align);
702-
// Note: notes for both 32-bit and 64-bit ELF files use 4-byte words in the
703-
// header, so the parsers are the same.
704-
auto ToRemove = (Endianness == endianness::little)
705-
? RemoveNoteDetail::findNotesToRemove<ELF64LE>(
706-
OldData, Align, NotesToRemove)
707-
: RemoveNoteDetail::findNotesToRemove<ELF64BE>(
708-
OldData, Align, NotesToRemove);
709-
if (!ToRemove.empty()) {
710-
if (Error E = Obj.updateSectionData(
711-
Sec, RemoveNoteDetail::updateData(OldData, ToRemove)))
712-
return E;
713-
}
714-
}
715-
return Error::success();
716-
}
717-
718612
static Error
719613
handleUserSection(const NewSectionInfo &NewSection,
720614
function_ref<Error(StringRef, ArrayRef<uint8_t>)> F) {
@@ -905,12 +799,6 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
905799
? endianness::little
906800
: endianness::big;
907801

908-
if (!ELFConfig.NotesToRemove.empty()) {
909-
if (Error Err =
910-
removeNotes(Obj, E, ELFConfig.NotesToRemove, Config.ErrorCallback))
911-
return Err;
912-
}
913-
914802
for (const NewSectionInfo &AddedSection : Config.AddSection) {
915803
auto AddSection = [&](StringRef Name, ArrayRef<uint8_t> Data) -> Error {
916804
OwnedDataSection &NewSection =

llvm/lib/ObjCopy/ELF/ELFObject.cpp

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,46 +2154,37 @@ ELFWriter<ELFT>::ELFWriter(Object &Obj, raw_ostream &Buf, bool WSH,
21542154
: Writer(Obj, Buf), WriteSectionHeaders(WSH && Obj.HadShdrs),
21552155
OnlyKeepDebug(OnlyKeepDebug) {}
21562156

2157-
Error Object::updateSectionData(SecPtr &Sec, ArrayRef<uint8_t> Data) {
2158-
if (!Sec->hasContents())
2157+
Error Object::updateSection(StringRef Name, ArrayRef<uint8_t> Data) {
2158+
auto It = llvm::find_if(Sections,
2159+
[&](const SecPtr &Sec) { return Sec->Name == Name; });
2160+
if (It == Sections.end())
2161+
return createStringError(errc::invalid_argument, "section '%s' not found",
2162+
Name.str().c_str());
2163+
2164+
auto *OldSec = It->get();
2165+
if (!OldSec->hasContents())
21592166
return createStringError(
21602167
errc::invalid_argument,
21612168
"section '%s' cannot be updated because it does not have contents",
2162-
Sec->Name.c_str());
2169+
Name.str().c_str());
21632170

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

2170-
if (!Sec->ParentSegment) {
2171-
Sec = std::make_unique<OwnedDataSection>(*Sec, Data);
2177+
if (!OldSec->ParentSegment) {
2178+
*It = std::make_unique<OwnedDataSection>(*OldSec, Data);
21722179
} else {
21732180
// The segment writer will be in charge of updating these contents.
2174-
Sec->Size = Data.size();
2175-
UpdatedSections[Sec.get()] = Data;
2181+
OldSec->Size = Data.size();
2182+
UpdatedSections[OldSec] = Data;
21762183
}
21772184

21782185
return Error::success();
21792186
}
21802187

2181-
Error Object::updateSection(StringRef Name, ArrayRef<uint8_t> Data) {
2182-
auto It = llvm::find_if(Sections,
2183-
[&](const SecPtr &Sec) { return Sec->Name == Name; });
2184-
if (It == Sections.end())
2185-
return createStringError(errc::invalid_argument, "section '%s' not found",
2186-
Name.str().c_str());
2187-
return updateSectionData(*It, Data);
2188-
}
2189-
2190-
Error Object::updateSectionData(SectionBase &S, ArrayRef<uint8_t> Data) {
2191-
auto It = llvm::find_if(Sections,
2192-
[&](const SecPtr &Sec) { return Sec.get() == &S; });
2193-
assert(It != Sections.end() && "The section should belong to the object");
2194-
return updateSectionData(*It, Data);
2195-
}
2196-
21972188
Error Object::removeSections(
21982189
bool AllowBrokenLinks, std::function<bool(const SectionBase &)> ToRemove) {
21992190

llvm/lib/ObjCopy/ELF/ELFObject.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,6 @@ class SectionBase {
549549
virtual void
550550
replaceSectionReferences(const DenseMap<SectionBase *, SectionBase *> &);
551551
virtual bool hasContents() const { return false; }
552-
virtual ArrayRef<uint8_t> getContents() const { return {}; }
553552
// Notify the section that it is subject to removal.
554553
virtual void onRemove();
555554

@@ -620,8 +619,6 @@ class Section : public SectionBase {
620619
bool hasContents() const override {
621620
return Type != ELF::SHT_NOBITS && Type != ELF::SHT_NULL;
622621
}
623-
ArrayRef<uint8_t> getContents() const override { return Contents; }
624-
625622
void restoreSymTabLink(SymbolTableSection &SymTab) override;
626623
};
627624

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

663659
class CompressedSection : public SectionBase {
@@ -1168,8 +1164,6 @@ class Object {
11681164
return Sec.Flags & ELF::SHF_ALLOC;
11691165
};
11701166

1171-
Error updateSectionData(SecPtr &Sec, ArrayRef<uint8_t> Data);
1172-
11731167
public:
11741168
template <class T>
11751169
using ConstRange = iterator_range<pointee_iterator<
@@ -1212,7 +1206,6 @@ class Object {
12121206

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

12171210
SectionBase *findSection(StringRef Name) {
12181211
auto SecIt =

0 commit comments

Comments
 (0)