Manufacturing ELF

Programming, for all ages and all languages.
Post Reply
User avatar
Js2xxx
Member
Member
Posts: 48
Joined: Sat Dec 31, 2016 1:43 am
Libera.chat IRC: wrgq
Location: China

Manufacturing ELF

Post by Js2xxx »

How to relocate ELF file to 64-bit higher address (0xFFFFFF80********) ?

I use LD to link and set Ttext to the high address but it shouts "Relocation truncated to fit: R_X86_64_32 against ***(A series of symbols even .text)". How to fix it?

EDIT: I searched almost the whole Internet and found a lot of answers saying "add gcc option -fPIC", so I tried it. However, it didn't work.
Also, I found that some of ELF relocation table items' infos are R_X86_64_32 and others are R_X86_64_64. Why the compiler creates R_X86_64_32? Will it works if there's no *_32?

Thanks for any help.
Doing steadfastly, or doing nil.
User avatar
Velko
Member
Member
Posts: 153
Joined: Fri Oct 03, 2008 4:13 am
Location: Ogre, Latvia, EU

Re: Manufacturing ELF

Post by Velko »

You need to compile your code using -mcmodel=large switch.

GCC's default code model is 'small', meaning that program and its symbols must be linked in the lower 2 GiB of the address space. It generates smaller and more efficient code than 'large'. You could also use '-mcmodel=kernel' (with similar benefits), but it means you have to move your kernel even further up - to uppermost 2 GiB (0xFFFFFFFF80000000) addresses.

Take a look at the very end of x86-64 Options manual page.
If something looks overcomplicated, most likely it is.
User avatar
Js2xxx
Member
Member
Posts: 48
Joined: Sat Dec 31, 2016 1:43 am
Libera.chat IRC: wrgq
Location: China

Re: Manufacturing ELF

Post by Js2xxx »

Dear Velko,
Velko wrote:You need to compile your code using -mcmodel=large switch.
Thanks a lot for help. Now I fix it for GCC. However, make the NASM generate R_X86_64_32 in the other part of my program, the code language of which are both C and assembly, though I write [bits 64] in the code. The symbol which has got the type *_32 is extern to pass the value between C and asm. Then how to fix the rest? Is there any options like GCC?

I'll be grateful if there's another help.

EDIT: If there's no option, should I compile my own NASM?
Doing steadfastly, or doing nil.
Post Reply