Skip to content

[RuntimeDyld][ELF][AArch64] Fix resolveAArch64ShortBranch. #8761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,8 @@ uint32_t RuntimeDyldELF::getMatchingLoRelocation(uint32_t RelType,
bool RuntimeDyldELF::resolveAArch64ShortBranch(
unsigned SectionID, relocation_iterator RelI,
const RelocationValueRef &Value) {
uint64_t Address;
uint64_t TargetOffset;
unsigned TargetSectionID;
if (Value.SymbolName) {
auto Loc = GlobalSymbolTable.find(Value.SymbolName);

Expand All @@ -1137,23 +1138,32 @@ bool RuntimeDyldELF::resolveAArch64ShortBranch(
return false;

const auto &SymInfo = Loc->second;
Address =
uint64_t(Sections[SymInfo.getSectionID()].getLoadAddressWithOffset(
SymInfo.getOffset()));

TargetSectionID = SymInfo.getSectionID();
TargetOffset = SymInfo.getOffset();
} else {
Address = uint64_t(Sections[Value.SectionID].getLoadAddress());
TargetSectionID = Value.SectionID;
TargetOffset = 0;
}
uint64_t Offset = RelI->getOffset();
uint64_t SourceAddress = Sections[SectionID].getLoadAddressWithOffset(Offset);

// We don't actually know the load addresses at this point, so if the
// branch is cross-section, we don't know exactly how far away it is.
if (TargetSectionID != SectionID)
return false;

uint64_t SourceOffset = RelI->getOffset();

// R_AARCH64_CALL26 requires immediate to be in range -2^27 <= imm < 2^27
// If distance between source and target is out of range then we should
// create thunk.
if (!isInt<28>(Address + Value.Addend - SourceAddress))
if (!isInt<28>(TargetOffset + Value.Addend - SourceOffset))
return false;

resolveRelocation(Sections[SectionID], Offset, Address, RelI->getType(),
Value.Addend);
RelocationEntry RE(SectionID, SourceOffset, RelI->getType(), Value.Addend);
if (Value.SymbolName)
addRelocationForSymbol(RE, Value.SymbolName);
else
addRelocationForSection(RE, Value.SectionID);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# RUN: llvm-mc -triple=arm64-none-linux-gnu -filetype=obj -o %t %s
# RUN: llvm-rtdyld -triple=arm64-none-linux-gnu -verify -check=%s %t

.globl _main
.weak _label1

.section .text.label1,"ax"
_label1:
nop

.section .text.main,"ax"
_main:
b _label1

# Branch must be to stub in .text.main, *not* back to _label1, because
# in general sections could be loaded at arbitrary addresses in target memory,
# and when initially processing locations and generating stubs we don't know
# the final layout yet, so we can't tell if the branch offset is within range.

# rtdyld-check: *{4}(_main) = 0x14000001