Skip to content

Conversation

@bulbazord
Copy link
Member

The code to apply relocations was sometimes creating unaligned destination pointers. Instead of giving them an explicit type (i.e. uint64_t *) and forcing the compiler to generate unaligned stores, mark the pointer as void *. The compiler will figure out the correct series of store instructions.

@bulbazord bulbazord requested a review from clayborg October 30, 2025 18:14
@llvmbot llvmbot added the lldb label Oct 30, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 30, 2025

@llvm/pr-subscribers-lldb

Author: Alex Langford (bulbazord)

Changes

The code to apply relocations was sometimes creating unaligned destination pointers. Instead of giving them an explicit type (i.e. uint64_t *) and forcing the compiler to generate unaligned stores, mark the pointer as void *. The compiler will figure out the correct series of store instructions.


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

1 Files Affected:

  • (modified) lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp (+4-6)
diff --git a/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp b/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
index 097c91b623e8f..9ad8b2d621876 100644
--- a/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
+++ b/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
@@ -2735,9 +2735,8 @@ static void ApplyELF64ABS64Relocation(Symtab *symtab, ELFRelocation &rel,
     // ObjectFileELF creates a WritableDataBuffer in CreateInstance.
     WritableDataBuffer *data_buffer =
         llvm::cast<WritableDataBuffer>(data_buffer_sp.get());
-    uint64_t *dst = reinterpret_cast<uint64_t *>(
-        data_buffer->GetBytes() + rel_section->GetFileOffset() +
-        ELFRelocation::RelocOffset64(rel));
+    void *const dst = data_buffer->GetBytes() + rel_section->GetFileOffset() +
+                      ELFRelocation::RelocOffset64(rel);
     uint64_t val_offset = value + ELFRelocation::RelocAddend64(rel);
     memcpy(dst, &val_offset, sizeof(uint64_t));
   }
@@ -2762,9 +2761,8 @@ static void ApplyELF64ABS32Relocation(Symtab *symtab, ELFRelocation &rel,
     // ObjectFileELF creates a WritableDataBuffer in CreateInstance.
     WritableDataBuffer *data_buffer =
         llvm::cast<WritableDataBuffer>(data_buffer_sp.get());
-    uint32_t *dst = reinterpret_cast<uint32_t *>(
-        data_buffer->GetBytes() + rel_section->GetFileOffset() +
-        ELFRelocation::RelocOffset32(rel));
+    void *const dst = data_buffer->GetBytes() + rel_section->GetFileOffset() +
+                      ELFRelocation::RelocOffset32(rel);
     memcpy(dst, &truncated_addr, sizeof(uint32_t));
   }
 }

The code to apply relocations was sometimes creating unaligned
destination pointers. Instead of giving them an explicit type (i.e.
`uint64_t *`) and forcing the compiler to generate unaligned stores,
mark the pointer as `void *`. The compiler will figure out the correct
series of store instructions.
@bulbazord bulbazord force-pushed the unaligned-writes-obj-file-elf branch from db9a0f6 to ecf95a5 Compare October 30, 2025 23:16
@bulbazord bulbazord requested a review from Michael137 October 30, 2025 23:18
@bulbazord bulbazord merged commit 425fe33 into llvm:main Nov 3, 2025
10 checks passed
@bulbazord bulbazord deleted the unaligned-writes-obj-file-elf branch November 3, 2025 18:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants