Skip to content

Commit 80a29e4

Browse files
committed
[llvm-objcopy] Remove references for empty section groups
Otherwise, llvm-objcopy fails with use-after-free when built under sanitizers. Simple repro can be running the test ELF/remove-section-in-group.test under asan. This is due to symbol table references to empty section groups.
1 parent 5e56d74 commit 80a29e4

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

llvm/lib/ObjCopy/ELF/ELFObject.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,6 +2203,11 @@ Error Object::removeSections(
22032203
if (auto ToRelSec = RelSec->getSection())
22042204
return !ToRemove(*ToRelSec);
22052205
}
2206+
// Remove empty group sections.
2207+
if (Sec->Type == ELF::SHT_GROUP) {
2208+
auto GroupSec = cast<GroupSection>(Sec.get());
2209+
return !llvm::all_of(GroupSec->members(), ToRemove);
2210+
}
22062211
return true;
22072212
});
22082213
if (SymbolTable != nullptr && ToRemove(*SymbolTable))

llvm/lib/ObjCopy/ELF/ELFObject.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,9 @@ class GroupSection : public SectionBase {
941941
SmallVector<SectionBase *, 3> GroupMembers;
942942

943943
public:
944+
template <class T>
945+
using ConstRange = iterator_range<
946+
pointee_iterator<typename llvm::SmallVector<T *, 3>::const_iterator>>;
944947
// TODO: Contents is present in several classes of the hierarchy.
945948
// This needs to be refactored to avoid duplication.
946949
ArrayRef<uint8_t> Contents;
@@ -964,6 +967,10 @@ class GroupSection : public SectionBase {
964967
const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
965968
void onRemove() override;
966969

970+
ConstRange<SectionBase> members() const {
971+
return make_pointee_range(GroupMembers);
972+
}
973+
967974
static bool classof(const SectionBase *S) {
968975
return S->OriginalType == ELF::SHT_GROUP;
969976
}

0 commit comments

Comments
 (0)