OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Mar 19, 2024 5:05 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: assembly in c file without using gcc inline asm?
PostPosted: Fri Feb 26, 2021 2:09 am 
Offline
Member
Member

Joined: Tue Jul 14, 2020 4:01 am
Posts: 70
I remember I've seen glibc did that before in the source code, but I forgot where.


Top
 Profile  
 
 Post subject: Re: assembly in c file without using gcc inline asm?
PostPosted: Fri Feb 26, 2021 3:41 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4591
Location: Chichester, UK
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?


Top
 Profile  
 
 Post subject: Re: assembly in c file without using gcc inline asm?
PostPosted: Fri Feb 26, 2021 7:02 pm 
Offline
Member
Member
User avatar

Joined: Mon Jun 05, 2006 11:00 pm
Posts: 2292
Location: USA (and Australia)
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.


Top
 Profile  
 
 Post subject: Re: assembly in c file without using gcc inline asm?
PostPosted: Sat Feb 27, 2021 3:13 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4591
Location: Chichester, UK
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.


Top
 Profile  
 
 Post subject: Re: assembly in c file without using gcc inline asm?
PostPosted: Sat Feb 27, 2021 4:26 am 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1590
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!


Top
 Profile  
 
 Post subject: Re: assembly in c file without using gcc inline asm?
PostPosted: Sat Feb 27, 2021 7:40 am 
Offline
Member
Member

Joined: Tue Jul 14, 2020 4:01 am
Posts: 70
By the way, I think they did it by using some kind of compiler directive.


Top
 Profile  
 
 Post subject: Re: assembly in c file without using gcc inline asm?
PostPosted: Sat Feb 27, 2021 8:38 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4591
Location: Chichester, UK
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group