Skip to content
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

[Hexagon] Add support for decoding PLT symbols #123425

Merged
merged 4 commits into from
Jan 29, 2025
Merged

Conversation

quic-areg
Copy link
Contributor

Describes PLT entries for hexagon.

@llvmbot
Copy link
Member

llvmbot commented Jan 17, 2025

@llvm/pr-subscribers-lld-elf
@llvm/pr-subscribers-lld

@llvm/pr-subscribers-llvm-binary-utilities

Author: None (quic-areg)

Changes

Describes PLT entries for hexagon.


Full diff: https://github.com/llvm/llvm-project/pull/123425.diff

3 Files Affected:

  • (modified) llvm/lib/Object/ELFObjectFile.cpp (+4)
  • (modified) llvm/lib/Target/Hexagon/MCTargetDesc/HexagonMCTargetDesc.cpp (+37-1)
  • (added) llvm/test/tools/llvm-objdump/ELF/Hexagon/plt.test (+42)
diff --git a/llvm/lib/Object/ELFObjectFile.cpp b/llvm/lib/Object/ELFObjectFile.cpp
index 1ddfadaf1e2716..2d3d70db50c393 100644
--- a/llvm/lib/Object/ELFObjectFile.cpp
+++ b/llvm/lib/Object/ELFObjectFile.cpp
@@ -802,6 +802,10 @@ std::vector<ELFPltEntry> ELFObjectFileBase::getPltEntries() const {
     case Triple::aarch64_be:
       JumpSlotReloc = ELF::R_AARCH64_JUMP_SLOT;
       break;
+    case Triple::hexagon:
+      JumpSlotReloc = ELF::R_HEX_JMP_SLOT;
+      GlobDatReloc = ELF::R_HEX_GLOB_DAT;
+      break;
     default:
       return {};
   }
diff --git a/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonMCTargetDesc.cpp b/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonMCTargetDesc.cpp
index a98f6048b051cc..d9fe05d4e1ac3f 100644
--- a/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonMCTargetDesc.cpp
+++ b/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonMCTargetDesc.cpp
@@ -734,8 +734,44 @@ class HexagonMCInstrAnalysis : public MCInstrAnalysis {
     Target = Value;
     return true;
   }
+
+  uint32_t getValueFromMask(uint32_t Instruction, uint32_t Mask) const {
+    uint32_t Result = 0;
+    size_t Off = 0;
+    for (uint32_t Bit = 0; Bit != sizeof(uint32_t) * CHAR_BIT; ++Bit) {
+      const uint8_t ValBit = (Instruction >> Bit) & 1;
+      const bool MaskBit = (Mask >> Bit) & 1;
+      if (MaskBit) {
+        Result |= (ValBit << Off);
+        ++Off;
+      }
+    }
+    return Result;
+  }
+
+  std::vector<std::pair<uint64_t, uint64_t>>
+  findPltEntries(uint64_t PltSectionVA, ArrayRef<uint8_t> PltContents,
+                 const Triple &TargetTriple) const override {
+    // Do a lightweight parsing of PLT entries.
+    std::vector<std::pair<uint64_t, uint64_t>> Result;
+    for (uint64_t Byte = 0x0, End = PltContents.size(); Byte < End; Byte += 4) {
+      // Recognize immext(##gotpltn)
+      uint32_t ImmExt = support::endian::read32le(PltContents.data() + Byte);
+      if ((ImmExt & 0x00004000) != 0x00004000)
+        continue;
+      uint32_t LoadGotPlt =
+          support::endian::read32le(PltContents.data() + Byte + 4);
+      if ((LoadGotPlt & 0x6a49c00c) != 0x6a49c00c)
+        continue;
+      uint32_t Address = (getValueFromMask(ImmExt, 0xfff3fff) << 6) +
+                         getValueFromMask(LoadGotPlt, 0x1f80) + PltSectionVA +
+                         Byte;
+      Result.push_back(std::make_pair(PltSectionVA + Byte, Address));
+    }
+    return Result;
+  }
 };
-}
+} // namespace
 
 static MCInstrAnalysis *createHexagonMCInstrAnalysis(const MCInstrInfo *Info) {
   return new HexagonMCInstrAnalysis(Info);
diff --git a/llvm/test/tools/llvm-objdump/ELF/Hexagon/plt.test b/llvm/test/tools/llvm-objdump/ELF/Hexagon/plt.test
new file mode 100644
index 00000000000000..5d2b8576b9b848
--- /dev/null
+++ b/llvm/test/tools/llvm-objdump/ELF/Hexagon/plt.test
@@ -0,0 +1,42 @@
+# RUN: yaml2obj %s | llvm-objdump -d - | FileCheck %s
+
+#      CHECK: 00000310 <printf@plt>:
+# CHECK-NEXT:      310:       36 40 00 00     00004036 {      immext(#0xd80)
+# CHECK-NEXT:      314:       0e de 49 6a     6a49de0e        r14 = add(pc,##0xdbc) }
+# CHECK-NEXT:      318:       1c c0 8e 91     918ec01c {      r28 = memw(r14+#0x0) }
+# CHECK-NEXT:      31c:       00 c0 9c 52     529cc000 {      jumpr r28 }
+
+--- !ELF
+FileHeader:
+  Class:           ELFCLASS32
+  Data:            ELFDATA2LSB
+  Type:            ET_DYN
+  Machine:         EM_HEXAGON
+Sections:
+  - Name:            .rela.plt
+    Type:            SHT_RELA
+    Flags:           [ SHF_ALLOC ]
+    Info:            .got.plt
+    Relocations:
+      - Offset:          0x10CC
+        Symbol:          printf
+        Type:            R_HEX_JMP_SLOT
+  - Name:            .plt
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    Address:         0x2B0
+    Content:         384000001CC0496A0E429CE24F409C913CC09C910E420E8C00C09C520000000000000000000000000000000000000000374000000ED0496A1CC08E9100C09C52374000000ECA496A1CC08E9100C09C52374000000EC4496A1CC08E9100C09C52364000000EDE496A1CC08E9100C09C52
+  - Name:            .text
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    Address:         0x320
+    Content:         0240096AC97FFF0F01C6007802C221F3FF7FFF0F80C7007800C002F300C0809100C0007500C05F5300C0096ADAFFFF5901C09DA082FFFEBF00C0423C0140000000D4496AD6FFFF5B00C000781EC01E96
+  - Name:            .got.plt
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_WRITE, SHF_ALLOC ]
+    Address:         0x10B0
+    Content:         00000000000000000000000000000000B0020000B0020000B0020000B0020000
+Symbols:
+  - Name:            printf
+    Binding:         STB_GLOBAL
+...

@llvmbot
Copy link
Member

llvmbot commented Jan 17, 2025

@llvm/pr-subscribers-backend-hexagon

Author: None (quic-areg)

Changes

Describes PLT entries for hexagon.


Full diff: https://github.com/llvm/llvm-project/pull/123425.diff

3 Files Affected:

  • (modified) llvm/lib/Object/ELFObjectFile.cpp (+4)
  • (modified) llvm/lib/Target/Hexagon/MCTargetDesc/HexagonMCTargetDesc.cpp (+37-1)
  • (added) llvm/test/tools/llvm-objdump/ELF/Hexagon/plt.test (+42)
diff --git a/llvm/lib/Object/ELFObjectFile.cpp b/llvm/lib/Object/ELFObjectFile.cpp
index 1ddfadaf1e2716..2d3d70db50c393 100644
--- a/llvm/lib/Object/ELFObjectFile.cpp
+++ b/llvm/lib/Object/ELFObjectFile.cpp
@@ -802,6 +802,10 @@ std::vector<ELFPltEntry> ELFObjectFileBase::getPltEntries() const {
     case Triple::aarch64_be:
       JumpSlotReloc = ELF::R_AARCH64_JUMP_SLOT;
       break;
+    case Triple::hexagon:
+      JumpSlotReloc = ELF::R_HEX_JMP_SLOT;
+      GlobDatReloc = ELF::R_HEX_GLOB_DAT;
+      break;
     default:
       return {};
   }
diff --git a/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonMCTargetDesc.cpp b/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonMCTargetDesc.cpp
index a98f6048b051cc..d9fe05d4e1ac3f 100644
--- a/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonMCTargetDesc.cpp
+++ b/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonMCTargetDesc.cpp
@@ -734,8 +734,44 @@ class HexagonMCInstrAnalysis : public MCInstrAnalysis {
     Target = Value;
     return true;
   }
+
+  uint32_t getValueFromMask(uint32_t Instruction, uint32_t Mask) const {
+    uint32_t Result = 0;
+    size_t Off = 0;
+    for (uint32_t Bit = 0; Bit != sizeof(uint32_t) * CHAR_BIT; ++Bit) {
+      const uint8_t ValBit = (Instruction >> Bit) & 1;
+      const bool MaskBit = (Mask >> Bit) & 1;
+      if (MaskBit) {
+        Result |= (ValBit << Off);
+        ++Off;
+      }
+    }
+    return Result;
+  }
+
+  std::vector<std::pair<uint64_t, uint64_t>>
+  findPltEntries(uint64_t PltSectionVA, ArrayRef<uint8_t> PltContents,
+                 const Triple &TargetTriple) const override {
+    // Do a lightweight parsing of PLT entries.
+    std::vector<std::pair<uint64_t, uint64_t>> Result;
+    for (uint64_t Byte = 0x0, End = PltContents.size(); Byte < End; Byte += 4) {
+      // Recognize immext(##gotpltn)
+      uint32_t ImmExt = support::endian::read32le(PltContents.data() + Byte);
+      if ((ImmExt & 0x00004000) != 0x00004000)
+        continue;
+      uint32_t LoadGotPlt =
+          support::endian::read32le(PltContents.data() + Byte + 4);
+      if ((LoadGotPlt & 0x6a49c00c) != 0x6a49c00c)
+        continue;
+      uint32_t Address = (getValueFromMask(ImmExt, 0xfff3fff) << 6) +
+                         getValueFromMask(LoadGotPlt, 0x1f80) + PltSectionVA +
+                         Byte;
+      Result.push_back(std::make_pair(PltSectionVA + Byte, Address));
+    }
+    return Result;
+  }
 };
-}
+} // namespace
 
 static MCInstrAnalysis *createHexagonMCInstrAnalysis(const MCInstrInfo *Info) {
   return new HexagonMCInstrAnalysis(Info);
diff --git a/llvm/test/tools/llvm-objdump/ELF/Hexagon/plt.test b/llvm/test/tools/llvm-objdump/ELF/Hexagon/plt.test
new file mode 100644
index 00000000000000..5d2b8576b9b848
--- /dev/null
+++ b/llvm/test/tools/llvm-objdump/ELF/Hexagon/plt.test
@@ -0,0 +1,42 @@
+# RUN: yaml2obj %s | llvm-objdump -d - | FileCheck %s
+
+#      CHECK: 00000310 <printf@plt>:
+# CHECK-NEXT:      310:       36 40 00 00     00004036 {      immext(#0xd80)
+# CHECK-NEXT:      314:       0e de 49 6a     6a49de0e        r14 = add(pc,##0xdbc) }
+# CHECK-NEXT:      318:       1c c0 8e 91     918ec01c {      r28 = memw(r14+#0x0) }
+# CHECK-NEXT:      31c:       00 c0 9c 52     529cc000 {      jumpr r28 }
+
+--- !ELF
+FileHeader:
+  Class:           ELFCLASS32
+  Data:            ELFDATA2LSB
+  Type:            ET_DYN
+  Machine:         EM_HEXAGON
+Sections:
+  - Name:            .rela.plt
+    Type:            SHT_RELA
+    Flags:           [ SHF_ALLOC ]
+    Info:            .got.plt
+    Relocations:
+      - Offset:          0x10CC
+        Symbol:          printf
+        Type:            R_HEX_JMP_SLOT
+  - Name:            .plt
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    Address:         0x2B0
+    Content:         384000001CC0496A0E429CE24F409C913CC09C910E420E8C00C09C520000000000000000000000000000000000000000374000000ED0496A1CC08E9100C09C52374000000ECA496A1CC08E9100C09C52374000000EC4496A1CC08E9100C09C52364000000EDE496A1CC08E9100C09C52
+  - Name:            .text
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    Address:         0x320
+    Content:         0240096AC97FFF0F01C6007802C221F3FF7FFF0F80C7007800C002F300C0809100C0007500C05F5300C0096ADAFFFF5901C09DA082FFFEBF00C0423C0140000000D4496AD6FFFF5B00C000781EC01E96
+  - Name:            .got.plt
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_WRITE, SHF_ALLOC ]
+    Address:         0x10B0
+    Content:         00000000000000000000000000000000B0020000B0020000B0020000B0020000
+Symbols:
+  - Name:            printf
+    Binding:         STB_GLOBAL
+...

@quic-areg
Copy link
Contributor Author

Copy link
Member

@androm3da androm3da left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but someone else should approve.

size_t Off = 0;
for (uint32_t Bit = 0; Bit != sizeof(uint32_t) * CHAR_BIT; ++Bit) {
const uint8_t ValBit = (Instruction >> Bit) & 1;
const bool MaskBit = (Mask >> Bit) & 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inline the only-used-once variable

uint32_t getValueFromMask(uint32_t Instruction, uint32_t Mask) const {
uint32_t Result = 0;
size_t Off = 0;
for (uint32_t Bit = 0; Bit != sizeof(uint32_t) * CHAR_BIT; ++Bit) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function seems inefficient. You can compute Result based on popcount(Instruction & Mask)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tried to make it more efficient. the loop should run popcount(Mask) times.

Copy link
Member

@androm3da androm3da Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tried to make it more efficient. the loop should run popcount(Mask) times.

What if you invoke llvm::popcount instead? I think that was the suggestion here.

uint32_t Result = llvm::popcount(Instruction & Mask);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will give the number of set bits in Result, but was not sure where to go from there. I think I still need to extract and shift the bits accordingly. @MaskRay thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the value (1u << llvm::popcount(Instruction & Mask)) - 1 ?

If (Instruction & Mask) could be UINT32_MAX, 1u<<32 would trigger UB, but I suspect it would not happen here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the value (1u << llvm::popcount(Instruction & Mask)) - 1 ?

It is not. The value will have popcount(Instruction & Mask) set bits but the number of significant bits is larger. This function tries to extract immediate value from an instruction encoding. It could be done by manually extracting and shifting the bits, but I thought this function was cleaner since the immediate bits are non-contiguous. The expression would look like this:

      uint32_t Address =
          (((ImmExt & 0x0fff) | (ImmExt & 0xfff3 >> 16) >> 2) << 6) +
          ((LoadGotPlt & 0x1f80) >> 7) + PltSectionVA + Byte;

uint32_t Address = (getValueFromMask(ImmExt, 0xfff3fff) << 6) +
getValueFromMask(LoadGotPlt, 0x1f80) + PltSectionVA +
Byte;
Result.push_back(std::make_pair(PltSectionVA + Byte, Address));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emplace_back

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@quic-areg
Copy link
Contributor Author

@androm3da can you merge this?

@androm3da androm3da merged commit 61ea63b into llvm:main Jan 29, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants