Skip to content

Commit af1328e

Browse files
committed
[ELF] Simplify EhInputSection::split. NFC
* Inline getReloc * Fold the UINT32_MAX length check into the section size check. This transformation is valid because we don't support .eh_frame input sections larger than 32-bit (unrealistic even for large code models).
1 parent 3e9adff commit af1328e

File tree

1 file changed

+15
-29
lines changed

1 file changed

+15
-29
lines changed

lld/ELF/InputSection.cpp

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,25 +1260,6 @@ SyntheticSection *EhInputSection::getParent() const {
12601260
return cast_or_null<SyntheticSection>(parent);
12611261
}
12621262

1263-
// Returns the index of the first relocation that points to a region between
1264-
// Begin and Begin+Size.
1265-
template <class IntTy, class RelTy>
1266-
static unsigned getReloc(IntTy begin, IntTy size, const ArrayRef<RelTy> &rels,
1267-
unsigned &relocI) {
1268-
// Start search from RelocI for fast access. That works because the
1269-
// relocations are sorted in .eh_frame.
1270-
for (unsigned n = rels.size(); relocI < n; ++relocI) {
1271-
const RelTy &rel = rels[relocI];
1272-
if (rel.r_offset < begin)
1273-
continue;
1274-
1275-
if (rel.r_offset < begin + size)
1276-
return relocI;
1277-
return -1;
1278-
}
1279-
return -1;
1280-
}
1281-
12821263
// .eh_frame is a sequence of CIE or FDE records.
12831264
// This function splits an input section into records and returns them.
12841265
template <class ELFT> void EhInputSection::split() {
@@ -1308,20 +1289,25 @@ void EhInputSection::split(ArrayRef<RelTy> rels) {
13081289
if (size == 0) // ZERO terminator
13091290
break;
13101291
uint32_t id = endian::read32<ELFT::TargetEndianness>(d.data() + 4);
1311-
// If it is 0xFFFFFFFF, the next 8 bytes contain the size instead,
1312-
// but we do not support that format yet.
1313-
if (size == UINT32_MAX) {
1314-
msg = "CIE/FDE too large";
1315-
break;
1316-
}
13171292
size += 4;
1318-
if (size > d.size()) {
1319-
msg = "CIE/FDE ends past the end of the section";
1293+
if (LLVM_UNLIKELY(size > d.size())) {
1294+
// If it is 0xFFFFFFFF, the next 8 bytes contain the size instead,
1295+
// but we do not support that format yet.
1296+
msg = size == UINT32_MAX + uint64_t(4)
1297+
? "CIE/FDE too large"
1298+
: "CIE/FDE ends past the end of the section";
13201299
break;
13211300
}
13221301

1323-
uint64_t off = d.data() - rawData.data();
1324-
(id == 0 ? cies : fdes).emplace_back(off, this, size, getReloc(off, size, rels, relI));
1302+
// Find the first relocation that points to [off,off+size). Relocations
1303+
// have been sorted by r_offset.
1304+
const uint64_t off = d.data() - rawData.data();
1305+
while (relI != rels.size() && rels[relI].r_offset < off)
1306+
++relI;
1307+
unsigned firstRel = -1;
1308+
if (relI != rels.size() && rels[relI].r_offset < off + size)
1309+
firstRel = relI;
1310+
(id == 0 ? cies : fdes).emplace_back(off, this, size, firstRel);
13251311
d = d.slice(size);
13261312
}
13271313
if (msg)

0 commit comments

Comments
 (0)