[llvm] 5441812 - [llvm-objdump][AArch64] Fix ADRP target label calculation

Kristina Bessonova via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 18 04:25:50 PST 2022


Author: Kristina Bessonova
Date: 2022-12-18T14:25:27+02:00
New Revision: 54418125191ce83af65569348301a23a3dd892a5

URL: https://github.com/llvm/llvm-project/commit/54418125191ce83af65569348301a23a3dd892a5
DIFF: https://github.com/llvm/llvm-project/commit/54418125191ce83af65569348301a23a3dd892a5.diff

LOG: [llvm-objdump][AArch64] Fix ADRP target label calculation

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

Added: 
    

Modified: 
    llvm/lib/Target/AArch64/MCTargetDesc/AArch64MCTargetDesc.cpp
    llvm/test/tools/llvm-objdump/ELF/AArch64/pcrel-address.yaml

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64MCTargetDesc.cpp b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64MCTargetDesc.cpp
index 451f0b26ed091..6db431f92d4ff 100644
--- a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64MCTargetDesc.cpp
+++ b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64MCTargetDesc.cpp
@@ -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;
       }
     }

diff  --git a/llvm/test/tools/llvm-objdump/ELF/AArch64/pcrel-address.yaml b/llvm/test/tools/llvm-objdump/ELF/AArch64/pcrel-address.yaml
index 48b2f2c208890..483274fa324c1 100644
--- a/llvm/test/tools/llvm-objdump/ELF/AArch64/pcrel-address.yaml
+++ b/llvm/test/tools/llvm-objdump/ELF/AArch64/pcrel-address.yaml
@@ -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:
@@ -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]


        


More information about the llvm-commits mailing list