Skip to content

Support pointer relocations in packed static initializers #930

Closed

Description

@uurl

Problem

Static initializers containing pointer relocations currently require relocation slots to be naturally aligned.

This rejects valid Rust statics such as:

#[repr(C, packed)]
struct Entry {
    tag: u8,
    ptr: &'static u32,
}

static TARGET: u32 = 0x1234_5678;

static ENTRY: Entry = Entry {
    tag: 0x7b,
    ptr: &TARGET,
};

For this layout, rustc places:

  • tag at byte offset 0
  • ptr at byte offset 1
  • total size at 9 bytes
  • ABI alignment at 1

The relocation itself is valid, but the current lowering rejects it because the
pointer slot is not naturally aligned.

A naturally laid out LLVM carrier such as:

{ [1 x i8], i64 }

cannot represent the Rust allocation byte-for-byte because LLVM aligns the
i64 field naturally.

Expected behavior

Relocation-bearing packed statics should preserve the exact rustc allocation
layout while keeping pointer provenance as an LLVM relocation.

For the example above, the physical initializer carrier can be represented as:

<{ [1 x i8], i64 }>

with alignment 1.

The packed LLVM struct should only be used as the physical storage carrier for
the initialized global. The semantic Rust aggregate should remain on the
existing fail-closed path for unsupported packed by-value operations.

Scope

Initial support should remain limited to:

  • top-level structs with explicit rustc layout metadata
  • thin-pointer relocations between supported device statics
  • non-overlapping relocation and field ranges
  • packed or otherwise under-aligned relocation slots
  • existing relocation addend and address-space semantics

This should not add support for:

  • packed aggregates passed or returned by value
  • fat pointers
  • trait objects or vtables
  • anonymous promoted allocation targets
  • function relocations
  • unsized pointees
  • overlapping field layouts
  • changes to the relocation metadata format

Activity

  1. added
    bug
    IR-lowering
    codegen
    on Aug 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    IR-loweringbugcodegen

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions