llvm / llvm/llvm-project

lld-link erroneously permits alignment < 4k when using the small code model

Open
#172,660 3 comments 1 reaction 0 assignees View on GitHub
backend:AArch64 lld:COFF
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

The AArch64 code model uses ADRP/ADD instruction pairs to emit symbol references with range of -/+ 4 GiB. The ADRP instruction generates a place-relative value rounded down to 4 KiB, and the lower 12 bits are provided by the subsequent ADD (or LDR) instruction.

Once fully linked, these immediate fields are set in stone (i.e., they are not runtime relocatable). The upshot of this is that the placement of the binary modulo 4 KiB in memory cannot deviate from the placement that the linker anticipated. In practice, this means that the PE/COFF section alignment must be at least 4 KiB.

However, `lld-link` happily permits `/ALIGN:x` directives with values of `x` as small as 32, resulting in incorrect behavior when a loader places the binary at an offset modulo 4k != 0.

This is an issue for EFI, which relies on PE/COFF executables that execute in place for NOR flash, and the way these flash images are constructed (i.e., many different executables loaded dynamically at boot), section alignment is usually set as low as practically feasible.

So ideally, `lld-link` should either relax ADRP/ADD pairs to ADR, or reject `/ALIGN:` directives on the command line that are incompatible with the small code model.

Contributor guide

Open the contributing guide

Research direction

Start by tracing lld-link's handling of /ALIGN:x alongside the AArch64 small code model's ADRP/ADD relocations. Confirm the behavior with an alignment below 4 KiB and an affected executable; done means incompatible alignment is rejected or the relevant instruction pairs are safely relaxed.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.