Skip to content

Commit

Permalink
[llvm-objdump][AArch64] Fix ADRP target label calculation
Browse files Browse the repository at this point in the history
This patch makes ADRP target label address calculations the same as
label address calculations (see AArch64InstPrinter::printAdrpLabel()).

Otherwise the target label looks misleading as ADRP's immediate offset is,
actually, not an offset to this PC, but an offset to the current PC's
page address in pages.

See for example, `llvm-objdump/ELF/AArch64/pcrel-address.yaml`.
Before this patch the target label `<_start+0x80>` represents the
address `0x200100 + 0x80` while `0x220000` is expected.

Note that with this patch llvm-objdump output matches GNU objdump.

Reviewed By: simon_tatham

Differential Revision: https://reviews.llvm.org/D139407
  • Loading branch information
chbessonova committed Dec 18, 2022
1 parent 6dcf937 commit 5441812
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
7 changes: 5 additions & 2 deletions llvm/lib/Target/AArch64/MCTargetDesc/AArch64MCTargetDesc.cpp
Expand Up @@ -412,8 +412,11 @@ class AArch64MCInstrAnalysis : public MCInstrAnalysis {
const auto &Desc = Info->get(Inst.getOpcode());
for (unsigned i = 0, e = Inst.getNumOperands(); i != e; i++) {
if (Desc.OpInfo[i].OperandType == MCOI::OPERAND_PCREL) {
int64_t Imm = Inst.getOperand(i).getImm() * 4;
Target = Addr + Imm;
int64_t Imm = Inst.getOperand(i).getImm();
if (Inst.getOpcode() == AArch64::ADRP)
Target = (Addr & -4096) + Imm * 4096;
else
Target = Addr + Imm * 4;
return true;
}
}
Expand Down
5 changes: 3 additions & 2 deletions llvm/test/tools/llvm-objdump/ELF/AArch64/pcrel-address.yaml
Expand Up @@ -2,7 +2,8 @@
# RUN: llvm-objdump %t -d --no-show-raw-insn --no-leading-addr | FileCheck %s

# CHECK-LABEL: <_start>:
# CHECK-NEXT: adrp x2, 0x220000 <_start+0x80>
# CHECK-NEXT: adrp x2, 0x220000
# CHECK-NEXT: adrp x2, 0x201000 <_start+0xf00>

--- !ELF
FileHeader:
Expand All @@ -15,7 +16,7 @@ Sections:
Type: SHT_PROGBITS
Address: 0x200100
Flags: [SHF_ALLOC, SHF_EXECINSTR]
Content: '02010090'
Content: '02010090020000B0'
- Name: .data
Type: SHT_PROGBITS
Flags: [SHF_ALLOC, SHF_WRITE]
Expand Down

0 comments on commit 5441812

Please sign in to comment.