Skip to content

Clang incorrectly assumes function alignment on arm64e #140226

@LIJI32

Description

@LIJI32

When targeting arm64e (but not arm64) with -O1 or higher, clang assumes functions are aligned to 8 bytes rather than 4 bytes, generating wrong code that might not work.

Minimal reproducing code:

extern void external_func(void);
unsigned long get_func(void)
{
    return ((unsigned long)(external_func) & 0xFFFFFFFFFFF);
}

Output from Apple clang version 17.0.0 (clang-1700.0.13.3):

clang -O0 -arch arm64e

_get_func:                              ; @get_func
	adrp	x16, _external_func@GOTPAGE
	ldr	x16, [x16, _external_func@GOTPAGEOFF]
	paciza	x16
	and	x0, x16, #0xfffffffffff ; Correct
	ret

clang -O1 -arch arm64e

_get_func:                              ; @get_func
	adrp	x16, _external_func@GOTPAGE
	ldr	x16, [x16, _external_func@GOTPAGEOFF]
	paciza	x16
	and	x0, x16, #0xffffffffff8 ; Incorrect
	ret

clang -O1 -arch arm64

_get_func:                              ; @get_func
	adrp	x8, _external_func@GOTPAGE
	ldr	x8, [x8, _external_func@GOTPAGEOFF]
	and	x0, x8, #0xffffffffffc ; Correct, but why bother changing the immediate here?
	ret

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:codegenIR generation bugs: mangling, exceptions, etc.invalidResolved as invalid, i.e. not a bugmiscompilation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions