Skip to content

Commit fb8ea85

Browse files
authored
[RuntimeDyld][ELF] Fix unwanted sign extension. (#94482)
Casting the result of `Section.getAddressWithOffset()` goes wrong if we are on a 32-bit platform whose addresses are regarded as signed; in that case, just doing ``` (uint64_t)Section.getAddressWithOffset(...) ``` or ``` reinterpret_cast<uint64_t>(Section.getAddressWithOffset(...)) ``` will result in sign-extension. We use these expressions when constructing branch stubs, which is before we know the final load address, so we can just switch to the `Section.getLoadAddressWithOffset(...)` method instead. Doing that is also more consistent, since when calculating relative offsets for relocations, we use the load address anyway, so the code currently only works because `Section.Address` is equal to `Section.LoadAddress` at this point. Fixes #94478.
1 parent 798f201 commit fb8ea85

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,8 +1183,7 @@ void RuntimeDyldELF::resolveAArch64Branch(unsigned SectionID,
11831183
StubMap::const_iterator i = Stubs.find(Value);
11841184
if (i != Stubs.end()) {
11851185
resolveRelocation(Section, Offset,
1186-
(uint64_t)Section.getAddressWithOffset(i->second),
1187-
RelType, 0);
1186+
Section.getLoadAddressWithOffset(i->second), RelType, 0);
11881187
LLVM_DEBUG(dbgs() << " Stub function found\n");
11891188
} else if (!resolveAArch64ShortBranch(SectionID, RelI, Value)) {
11901189
// Create a new stub function.
@@ -1217,8 +1216,7 @@ void RuntimeDyldELF::resolveAArch64Branch(unsigned SectionID,
12171216
addRelocationForSection(REmovk_g0, Value.SectionID);
12181217
}
12191218
resolveRelocation(Section, Offset,
1220-
reinterpret_cast<uint64_t>(Section.getAddressWithOffset(
1221-
Section.getStubOffset())),
1219+
Section.getLoadAddressWithOffset(Section.getStubOffset()),
12221220
RelType, 0);
12231221
Section.advanceStubOffset(getMaxStubSize());
12241222
}
@@ -1349,10 +1347,9 @@ RuntimeDyldELF::processRelocationRef(
13491347
// Look for an existing stub.
13501348
StubMap::const_iterator i = Stubs.find(Value);
13511349
if (i != Stubs.end()) {
1352-
resolveRelocation(
1353-
Section, Offset,
1354-
reinterpret_cast<uint64_t>(Section.getAddressWithOffset(i->second)),
1355-
RelType, 0);
1350+
resolveRelocation(Section, Offset,
1351+
Section.getLoadAddressWithOffset(i->second), RelType,
1352+
0);
13561353
LLVM_DEBUG(dbgs() << " Stub function found\n");
13571354
} else {
13581355
// Create a new stub function.
@@ -1367,10 +1364,10 @@ RuntimeDyldELF::processRelocationRef(
13671364
else
13681365
addRelocationForSection(RE, Value.SectionID);
13691366

1370-
resolveRelocation(Section, Offset, reinterpret_cast<uint64_t>(
1371-
Section.getAddressWithOffset(
1372-
Section.getStubOffset())),
1373-
RelType, 0);
1367+
resolveRelocation(
1368+
Section, Offset,
1369+
Section.getLoadAddressWithOffset(Section.getStubOffset()), RelType,
1370+
0);
13741371
Section.advanceStubOffset(getMaxStubSize());
13751372
}
13761373
} else {
@@ -1609,8 +1606,7 @@ RuntimeDyldELF::processRelocationRef(
16091606
if (i != Stubs.end()) {
16101607
// Symbol function stub already created, just relocate to it
16111608
resolveRelocation(Section, Offset,
1612-
reinterpret_cast<uint64_t>(
1613-
Section.getAddressWithOffset(i->second)),
1609+
Section.getLoadAddressWithOffset(i->second),
16141610
RelType, 0);
16151611
LLVM_DEBUG(dbgs() << " Stub function found\n");
16161612
} else {
@@ -1652,10 +1648,10 @@ RuntimeDyldELF::processRelocationRef(
16521648
addRelocationForSection(REl, Value.SectionID);
16531649
}
16541650

1655-
resolveRelocation(Section, Offset, reinterpret_cast<uint64_t>(
1656-
Section.getAddressWithOffset(
1657-
Section.getStubOffset())),
1658-
RelType, 0);
1651+
resolveRelocation(
1652+
Section, Offset,
1653+
Section.getLoadAddressWithOffset(Section.getStubOffset()),
1654+
RelType, 0);
16591655
Section.advanceStubOffset(getMaxStubSize());
16601656
}
16611657
if (IsExtern || (AbiVariant == 2 && Value.SectionID != SectionID)) {

0 commit comments

Comments
 (0)