Terraspace / UASM

UASM - Macro Assembler
http://www.terraspace.co.uk/uasm.html
Other
220 stars 49 forks source link

Position Independent Code #74

Closed tux3 closed 2 months ago

tux3 commented 6 years ago

Hi,

Is there any plan for some sort of PIC support?

Having a fully usable assembler that's just a bit more powerful than NASM on Linux would be really great!

john-terraspace commented 6 years ago

Hi,

Definitely, there should already be a fair amount of support for writing PIC code as one would on Windows using all the RIP relative forms of addressing and LEA.

What features specifically are you interested in seing ?

(I’m always looking to get more input on the Linux side as it’s not my main platform).

From: Tux3 [mailto:notifications@github.com] Sent: Monday, January 8, 2018 12:29 AM To: Terraspace/UASM UASM@noreply.github.com Cc: Subscribed subscribed@noreply.github.com Subject: [Terraspace/UASM] Position Independent Code (#74)

Hi,

Is there any plan for some sort of PIC support?

Having a fully usable assembler that's just a bit more powerful than NASM on Linux would be really great!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Terraspace/UASM/issues/74 , or mute the thread https://github.com/notifications/unsubscribe-auth/AQGQVJ0vXWMJ8rfBLofnf6e8mKDrN4POks5tIWFUgaJpZM4RV2Sw .

tux3 commented 6 years ago

Hi,

My understanding is that there's currently support for RIP-relative addressing in UASM, but I couldn't find a way to use different relocation types, to get for example GOT or PLT relative addressing.

NASM for example supports choosing which relocation type to use with the wrt operator.

In NASM if I write the following (broken w/ PIC) 64bit code:

extern exit
global main
main: call exit

I think this is roughly equivalent to this UASM code:

.code
extern exit: proc
main: call exit
end main

This assembles to e8 00 00 00 00 with a R_X86_64_PC32 relocation, which is not going to work if I ask GCC to link it as -pie code (which is enabled by default on many Linux distributions).

Or I could do something like:

extern exit
global main
main:
mov rax, exit wrt ..sym
call rax

Which gives a plain R_X86_64_64 relocation applied to 48 b8 00 00 00 00 00 00 00 00 ff d0. This works in NASM and in UASM. But in NASM I can also do call exit wrt ..plt which gives a R_X86_64_PLT32 relocation, While both of those examples will work fine with -pie enabled, the second one is preferable.

If it doesn't already exist, some sort of counterpart to the wrt operator in UASM would be very useful, or at least some way to get PLT or GOT relative addressing.

Note that I'm not very familiar with all of this yet, so I might have overlooked something obvious :) Thank you!

(edited for clarity)

john-terraspace commented 6 years ago

You are correct. We’ve actually just started a big thread on the masm32 forum about this stuff exactly.

UASM, /all jwasm derivatives have no support for GOT/PLT.

NASM has support, but apparently from reports it’s not working in all cases.

YASM has died a death, although it had a degree of support.

What we’re considering is whether the whole process can be automated and we can avoid needing any sort of operator. From what I understand thus far it would seem that the GOT and PLT relocation

Types even under PIE are only relevant to accessing data and functions in a different compilation unit/module ? In which case we could automatically convert any such reference to it’s PLT or GOT form.

It would be helpful if you’d join the masm32 thread/discussion as we’re trying to accumulate all the ideas and opinions on the matter to put a plan of action in place and get the support built-in, either fully automated with a command line switch -pie or if required operators, although I’m hoping to avoid that as it just makes the code really ugly.

From: Tux3 [mailto:notifications@github.com] Sent: Tuesday, January 9, 2018 8:38 PM To: Terraspace/UASM UASM@noreply.github.com Cc: John Hankinson john@terraspace.co.uk; Comment comment@noreply.github.com Subject: Re: [Terraspace/UASM] Position Independent Code (#74)

Hi,

My understanding is that there's currently support for RIP-relative addressing in UASM, but I couldn't find a way to use different relocation types, to get for example GOT or PLT relative addressing.

NASM for example supports choosing which relocation type to use with the wrt operator http://www.nasm.us/doc/nasmdoc7.html#section-7.9.3 .

In NASM if I write the following (broken w/ PIC) 64bit code:

extern exit global main main: call exit

I think this is roughly equivalent to this UASM code:

.code extern exit: proc main: call exit end main

This assembles to e8 00 00 00 00 with a R_X86_64_PC32 relocation, which is not going to work if I ask GCC to link it as -pie code (which is enabled by default on many Linux distributions).

Or I could do something like:

extern exit global main main: mov rax, exit wrt ..sym call rax

Which gives a plain R_X86_64_64 relocation applied to 48 b8 00 00 00 00 00 00 00 00 ff d0. This works in NASM and in UASM. But in NASM I can also do call exit wrt ..plt which gives a R_X86_64_PLT32 relocation, While both of those examples will work fine with -pie enabled, the second one is preferable.

If it doesn't already exist, some sort of counterpart to the wrt operator in UASM would be very useful, or at least some way to get PLT or GOT relative addressing.

Note that I'm not very familiar with all of this yet, so I might have overlooked something obvious :) Thank you!

(edited for clarity)

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/Terraspace/UASM/issues/74#issuecomment-356406825 , or mute the thread https://github.com/notifications/unsubscribe-auth/AQGQVPh8uG-Px8ScA8S4eZF2u2AjjSh6ks5tI840gaJpZM4RV2Sw .

tastewar commented 5 years ago

Hi! Can you please let me know what the status is of PIC support in UASM (specifically for Linux/x64)? We need to take some Windows dll's and be able to recast them as Linux .so shared libraries. From your roadmap, I get the impression that this is slated for 2.50, but I guess I am a bit uncertain (seems like PLT/GOT Support is what enables this?). Any clarification would be appreciated! And how far along the development is in the trunk. And is the trunk available elsewhere? Thank you!!

john-terraspace commented 5 years ago

Hi,

It was tentatively planned for 2.50, or at least the first part of it. We’ve had quite a list of fixes which are ready to go so 2.50 will probably just be those and exclude the PIC support. I’m hoping to have at least the first stage of that ready for 2.51.

I’ve actually been looking to try and get some additional hands to help out on the PIC support. I’ve done some initial testing of 2.50 as it stands without PIC and have been able to create several .SO files without issue (thus far) as well as all the example executables as long they are compiled with -no-pie.

I guess my biggest concern was trying to find a way to automate the work of PLT/GOT from the user/coder perspective but it looks like that isn’t going to be possible due to the multiple indirections. It would land up obfuscating what is actually going on with addresses and variable lookups.

To be honest I still can’t fathom why for x64 Linux went with PLT/GOT at all.. it’s a really stupid system, although I guess as a C/C++ programmer you’d be oblivious to it !

If you have thoughts on how the implementation should/could work I’m still in planning mode so happy to take suggestions.

Cheers,

John

From: Tom Stewart notifications@github.com Sent: 22 August 2019 20:31 To: Terraspace/UASM UASM@noreply.github.com Cc: John Hankinson john@terraspace.co.uk; Comment comment@noreply.github.com Subject: Re: [Terraspace/UASM] Position Independent Code (#74)

Hi! Can you please let me know what the status is of PIC support in UASM (specifically for Linux/x64)? We need to take some Windows dll's and be able to recast them as Linux .so shared libraries. From your roadmap, I get the impression that this is slated for 2.50, but I guess I am a bit uncertain (seems like PLT/GOT Support is what enables this?). Any clarification would be appreciated! And how far along the development is in the trunk. And is the trunk available elsewhere? Thank you!!

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/Terraspace/UASM/issues/74?email_source=notifications&email_token=AEAZAVGPAW2RJAOXGY32S33QF3SO7A5CNFSM4EKXMSYKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD46EQZA#issuecomment-524044388 , or mute the thread https://github.com/notifications/unsubscribe-auth/AEAZAVE6YVJQEYECKCUDALDQF3SO7ANCNFSM4EKXMSYA .

john-terraspace commented 2 months ago

Closing this issue as there has been no input or progress made on finding a way to deal with PLT or GOT in Linux. It's shit.. and I hope they suffer some excruciating pain post-life for ever having come up with it as an architecture.