assembly in c file without using gcc inline asm?
-
- Member
- Posts: 70
- Joined: Tue Jul 14, 2020 4:01 am
- Libera.chat IRC: clementttttttttt
assembly in c file without using gcc inline asm?
I remember I've seen glibc did that before in the source code, but I forgot where.
Re: assembly in c file without using gcc inline asm?
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?
- AndrewAPrice
- 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?
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.
Re: assembly in c file without using gcc inline asm?
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.
Re: assembly in c file without using gcc inline asm?
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.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.
Carpe diem!
-
- 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?
By the way, I think they did it by using some kind of compiler directive.
Re: assembly in c file without using gcc inline asm?
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.nullplan wrote: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.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.