I have a program that generates Nasm and LD commands and executes them via Shell. The LD commands link the X64 object file to libSystem.dylib but under Big Sur such dynamic libraries no longer seem to be present. What approach is now needed to produce X64 executables using system functions?
Mac OS 11.0.1 Linking X64 system calls to System Library
Big Sur has moved the system libraries to a cache that is no longer in the file system. Eskimo from Apple answered how to figure it out for my 'how to link to the libraries in C' question in another thread, and that answer should work for a Nasm generated .o file as well.
I also figured out how to do it using the gnu assembler that comes with XCode and this is how I did it:
Using this gnu assembler assembly language source code:
I was able to compile and link using these commands:
as -mmacosx-version-min=10.11 ./helloworldbigsur.s -o helloworldbigsur.o
ld -e myhelloworldstart -nouuid -noehlabels -demangle -dynamic -arch x8664 -platformversion macos 11.0.0 11.1 -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -o helloworldbigsur -L/usr/local/lib helloworldbigsur.o -lSystem
It looks like /usr/local/lib is the path to the dynamic library cache even though the files are not in the file system. When I used otool to look at the executable, it looks like the system loads dyld from /usr/lib and then adds /usr/local/lib to the library search path.
I also figured out how to do it using the gnu assembler that comes with XCode and this is how I did it:
Using this gnu assembler assembly language source code:
Code Block .text _helloworldmsg:.asciz "Hello World!\n" .globl _myhelloworldstart _myhelloworldstart: // syscall is deprecated. // Last I checked, if you use syscall, the assembler compiles an imported function call. // If you hard code the syscall opcode, macos will not execute it and will give you an error // so I am using imported function calls to access the system library functions. andq $0xfffffffffffffff0, %rsp // Force alignment. Without this exit will seg fault. // In other words, the system functions require that the return stack is 16 byte aligned before you call them. movq $1, %rdi leaq _helloworldmsg(%rip), %rsi movq $13, %rdx call _write xorq %rdi, %rdi // clear rdi to return success call _exit
I was able to compile and link using these commands:
as -mmacosx-version-min=10.11 ./helloworldbigsur.s -o helloworldbigsur.o
ld -e myhelloworldstart -nouuid -noehlabels -demangle -dynamic -arch x8664 -platformversion macos 11.0.0 11.1 -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -o helloworldbigsur -L/usr/local/lib helloworldbigsur.o -lSystem
It looks like /usr/local/lib is the path to the dynamic library cache even though the files are not in the file system. When I used otool to look at the executable, it looks like the system loads dyld from /usr/lib and then adds /usr/local/lib to the library search path.
Thank you. I did see the reply to you about the SDK but couldn't get it to work in my context. I will pursue as per your reply. Hard to understand why simple things that work have to be made more difficult.
I tried the above but the LD command failed with various unknown options -nouuid noehlabels platform version - and I set the arch to x86_64. Then the response was -lSystem not found as in my own attempts previously. Not sure why my version of LD differs from yours and still fails.