assembly in c file without using gcc inline asm?

Programming, for all ages and all languages.
Post Reply
clementttttttttt
Member
Member
Posts: 70
Joined: Tue Jul 14, 2020 4:01 am
Libera.chat IRC: clementttttttttt

assembly in c file without using gcc inline asm?

Post by clementttttttttt »

I remember I've seen glibc did that before in the source code, but I forgot where.
User avatar
iansjack
Member
Member
Posts: 4695
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: assembly in c file without using gcc inline asm?

Post by iansjack »

You can use inline assembly or link in an object file produced by a separate assembler. I can't think of any other way to get assembly code into a C program. How else do you think you could specify the instructions that you wanted?
User avatar
AndrewAPrice
Member
Member
Posts: 2299
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Re: assembly in c file without using gcc inline asm?

Post by AndrewAPrice »

You can also assemble your assembly code into machine code, then define it as a hardcoded byte array in C and call it.
My OS is Perception.
User avatar
iansjack
Member
Member
Posts: 4695
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: assembly in c file without using gcc inline asm?

Post by iansjack »

I hadn't thought of that. But wouldn't that require your data pages to be executable, which doesn't seem like a good idea to me.
nullplan
Member
Member
Posts: 1779
Joined: Wed Aug 30, 2017 8:24 am

Re: assembly in c file without using gcc inline asm?

Post by nullplan »

iansjack wrote:I hadn't thought of that. But wouldn't that require your data pages to be executable, which doesn't seem like a good idea to me.
Well, in my standard linker script, constant data is usually made part of the executable segment, so as long as you declare the array as constant it should work out. Though recently, binutils has shipped a new standard linker script that puts constant data into a nonexecutable segment, with the rationale of making it harder to find ROP gadgets. Of course, the additional page alignment overhead of this is pretty bad, especially for applications that try to limit memory consumption.
Carpe diem!
clementttttttttt
Member
Member
Posts: 70
Joined: Tue Jul 14, 2020 4:01 am
Libera.chat IRC: clementttttttttt

Re: assembly in c file without using gcc inline asm?

Post by clementttttttttt »

By the way, I think they did it by using some kind of compiler directive.
User avatar
iansjack
Member
Member
Posts: 4695
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: assembly in c file without using gcc inline asm?

Post by iansjack »

nullplan wrote:
iansjack wrote:I hadn't thought of that. But wouldn't that require your data pages to be executable, which doesn't seem like a good idea to me.
Well, in my standard linker script, constant data is usually made part of the executable segment, so as long as you declare the array as constant it should work out. Though recently, binutils has shipped a new standard linker script that puts constant data into a nonexecutable segment, with the rationale of making it harder to find ROP gadgets. Of course, the additional page alignment overhead of this is pretty bad, especially for applications that try to limit memory consumption.
I'm sure that you are correct but, in any case, it doesn't seem to be a very useful way of including machine code into a C program unless you are looking at self-modifying code.
Post Reply