The write syscall has the following functional prototype:
size_t write(int, const void *buf, size_t nbytes);
How do I call the write syscall using inline assembler in GCC under MacOS X?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A generic solution to this type of question: Write a short test program doing the write() call you are interested in, and then use
gcc -Sto produce assembly or useotoolto disassemble the binary; find out how the write() call was assembled and transform that into the appropriate inline assembly.(edit) To see the actual syscall, follow the library call in the assembly code. In the example of
write(), following the indirections will lead you vialibSystem.B.dylibto_writeinlibsystem_kernel.dylib, which you can disassemble usingotool.(edit2) Full example below, using this test program
test.c:Compile and test, using
-O1 -fverbose-asmto get concise, readable machine code:Use
otoolto disassemblemain()in thetestbinary:List the libraries
testis dynamically linked against to find_write:Try to locate
_writeinlibSystem.B.dylib:Check the dependencies of
libSystem.B.dylib:Guess that
_writeis probably contained inlibsystem_kernel.dylib(or alternatively try all of them):Now we have all the assembly we need to construct the inline assembly version of
test:Compile and test:
That seems to work. The inline assembly above is not very refined; for example, you could pass in the first and third arguments dynamically as well, instead of hardcoding them in the assembly code.