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

arch: aarch64: Compile with -fPIC #31984

Conversation

carlocaione
Copy link
Collaborator

As usual a bit of rationale.

This is a general fix to a very specific problem. The problem is described in #31952 and it is caused by the symbol:

#define Z_KERNEL_VIRT_SIZE ((size_t)(&z_mapped_size))

when compiling on very high SRAM addresses, for example CONFIG_SRAM_BASE_ADDRESS=0x2000000000

The obvious local fix in this case is just to use:

#define Z_KERNEL_VIRT_SIZE	((size_t)Z_KERNEL_VIRT_END - Z_KERNEL_VIRT_START) 

but doing so we are just hiding the real issue so here I propose a more general fix.

Under some circumstances (for example when having a SRAM base address
very high in memory) the compilation fails with:

  relocation truncated to fit: R_AARCH64_ADR_PREL_PG_HI21 against symbol

One reasonable fix is to compile the code using '-fPIC' and at the same
time reintroduce the discarded PLT/GOT sections.

Signed-off-by: Carlo Caione <[email protected]>
@github-actions github-actions bot added area: API Changes to public APIs area: ARM ARM (32-bit) Architecture area: ARM_64 labels Feb 4, 2021
@carlocaione carlocaione linked an issue Feb 4, 2021 that may be closed by this pull request
@npitre
Copy link
Collaborator

npitre commented Feb 4, 2021 via email

Copy link
Contributor

@andyross andyross left a comment

Choose a reason for hiding this comment

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

Is there a better explanation of the root cause? What's the specific relocation data structure involved? Surely binutils can handle a 64 bit address natively. Is this a relative address for a jump or something? This smells more like a workaround than a fix to me.

@npitre
Copy link
Collaborator

npitre commented Feb 5, 2021 via email

@andyross
Copy link
Contributor

andyross commented Feb 5, 2021

OK, got it.[1] Then maybe what we want is an arch API like ARCH_KERNEL_VIRT_SIZE (and I guess a CONFIG_HAVE_ARCH_KERNEL_VIRT_SIZE or some other way to detect non-default behavior), then on this platform you could have:

#define ARM64_KERNEL_VIRT_SIZE (z_mapped_end - z_mapped_start)

Basically you pay for this with one runtime subtraction, but avoid the need to compile the entire application position-independent.

[1] Well, not quite: why is this entering code as a runtime relocation and not being inserted into the binary at link time as an immediate storing the absolute address? Linkers on most architectures with this kind of limitation are generally pretty smart about that.

@npitre
Copy link
Collaborator

npitre commented Feb 5, 2021 via email

@carlocaione
Copy link
Collaborator Author

Solved by #32053

@carlocaione carlocaione closed this Feb 5, 2021
@carlocaione carlocaione deleted the fix_relocation_aarch64 branch March 3, 2021 15:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: API Changes to public APIs area: ARM ARM (32-bit) Architecture
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Linking fails with latest master on ARM64 platform
4 participants