microsoft / PQCrypto-SIDH

SIDH Library is a fast and portable software library that implements state-of-the-art supersingular isogeny cryptographic schemes. The chosen parameters aim to provide security against attackers running a large-scale quantum computer, and security against classical algorithms.
MIT License
317 stars 101 forks source link

Linking with ARM64 assembly code fails for larger APKs #2

Closed wernerd closed 5 years ago

wernerd commented 7 years ago

First of all thanks a lot for this code :-)

I ran several tests with the ARM64 assembly code and the results are impressive on my Android 6P device. However, when I tried to use the assembly code in a larger project the linker complained about several relocation problems:

relocation truncated to fit: R_AARCH64_LD_PREL_LO19 against `.data'

I checked the code and the ARM64 documentation and the culprit are the simple ldr instructions. The offset between the instruction and the data (in the data section) cannot be bigger than 1MB according to the ARM64 doc. For larger programs this will be usually the case. I changed the assembly code and replaced the simple ldr instructions with this sequence:

// ldr x16, p751x2
adrp x16, p751x2
ldr  x16, [x16, #:lo12:p751x2]

After changing and adapting all the simple ldr instructions I ran the tests, everything works and no errors reported.

After adding the code to my larger project the linker was also happy, no complaints. First tests were also OK. I'm not a specialist in ARM assembler coding, thus a better solution may exist.

Attached the complete modified assembler file, compressed in a ZIP.

Werner

fp_arm64_asm.S.zip

dburbani commented 7 years ago

What if you just move the constants to the .text section? Or even delete the section labels entirely?

wernerd commented 7 years ago

I'm not familiar with the AMR64 assembler :-) - I just found the fix (computing the addresses) in some other project and just took it over. Thus I'm not sure what happens if I move the data to the .text section. I do some testing and see what happens.

patricklonga commented 5 years ago

Have you tried to compile with the flag -fPIC? This should solve the problem.

patricklonga commented 5 years ago

@dburbani Moving the constants to the .text section worked in my tests. I'll update this part in the upcoming release.