by andy_threos_io · 5y ago · 0 comments
First thing first, macro assemblers have been around for at least 40+ years. So don't use numbers in your code. use macros or defines.
Ex. you can compile with gcc flags -x assembler-with-cpp
and you can have nice defines in your code.
Second on a decent OS even in assembly link against operating system call library, no matter what. The system call numbers can change. so use symbols.
Also don't write the string length in the code, it's total lame.
and use .asciz not .ascii
and define your function symbols to function gcc:
.global my_func_name
.type my_func_name, function
my_func_name:
if you use C preprocessor for assembly compile just make an include like this: #define _FUNCTION(A) .global A ;\
.type A, function
EDIT:
For the not well informed HN readers:https://developer.apple.com/library/archive/qa/qa1118/_index...
"Apple does not support statically linked binaries on Mac OS X. A statically linked binary assumes binary compatibility at the kernel system call interface, and we do not make any guarantees on that front. Rather, we strive to ensure binary compatibility in each dynamically linked system library and framework."
So DON'T write direct system call numbers in your code!
You don't even need the flag. Just use the .S file extension rather than .s
bl _Open ; whatever syscall name you have
and the linker or the OS dynamic linker will get you the proper system call code, with the numbers.BTW even in our small threos.io os we use dynamic linked system calls.
Golang used to run into this, they switched to using libSystem in 1.11: https://golang.org/doc/go1.11#runtime
/usr/lib/system/libsystem_kernel.dylib
ex. https://stackoverflow.com/questions/37656016/osx-setgid-syst...