1

I have a strange issue

➜  ASM git:(master) ✗ ./Colleen                  
DEFAULT REL
SECTION .text
global _main
extern _printf
_main:
        push rbx
        lea rdi, [code]
        mov rsi, 10
        mov rdx, 34
        lea rcx, [code]
        xor rax, rax
        call _printf
quit:
        mov eax, 0x2000001
        xor edi, edi
        syscall ;end

code: "DEFAULT REL%1$cSECTION .text%1$cglobal _main%1$cextern _printf%1$c_main:%1$c     push rbx%1$c    lea rdi, [code]%1$c     mov rsi, 10%1$c mov rdx, 34%1$c lea rcx, [code]%1$c     xor rax, rax%1$c        call _printf%1$cquit:%1$c       mov eax, 0x2000001%1$c  xor edi, edi%1$c        syscall ;end%1$c%1$ccode: %2$c%3$s%2$c, 0%1$c", 0
➜  ASM git:(master) ✗ ./Colleen > a
➜  ASM git:(master) ✗ cat a
➜  ASM git:(master) ✗

It seems like standard output can't be redirected to a file. The output is made by a simple printf, and output produced by any other way works just fine, ony my assembly is buggy. I did the same on Debian, but i had the same result.

MacOS source code :

DEFAULT REL
SECTION .text
global _main
extern _printf
_main:
    push rbx
    lea rdi, [code]
    mov rsi, 10
    mov rdx, 34
    lea rcx, [code]
    xor rax, rax
    call _printf
quit:
    mov eax, 0x2000001
    xor edi, edi
    syscall ;end

code: db "DEFAULT REL%1$cSECTION .text%1$cglobal _main%1$cextern _printf%1$c_main:%1$c  push rbx%1$c    lea rdi, [code]%1$c mov rsi, 10%1$c mov rdx, 34%1$c lea rcx, [code]%1$c xor rax, rax%1$c    call _printf%1$cquit:%1$c   mov eax, 0x2000001%1$c  xor edi, edi%1$c    syscall ;end%1$c%1$ccode: %2$c%3$s%2$c, 0%1$c", 0

compilation :

nasm -f macho64 Colleen.asm
clang -nostartfiles -arch x86_64 Colleen.o -o Colleen

Linux elf64 source code

DEFAULT REL
SECTION .rodata
code: db "DEFAULT REL%1$cSECTION .rodata%1$ccode: db %2$c%3$s%2$c%1$c%1$cSECTION .text%1$cextern printf%1$cglobal _start%1$c_start:%1$c mov edi, code%1$c   mov esi, 10%1$c mov edx, 34%1$c mov ecx, code%1$c   xor eax, eax%1$c    call printf%1$c%1$c mov eax, 60%1$c xor edi, edi%1$c    syscall ;end%1$c"

SECTION .text
extern printf
global _start
_start:
    mov edi, code
    mov esi, 10
    mov edx, 34
    mov ecx, code
    xor eax, eax
    call printf

    mov eax, 60
    xor edi, edi
    syscall ;end

compilation :

nasm -f elf64 Colleen.asm
gcc -no-pie -nostartfiles Colleen.o -o Colleen

Thank you for your help !

fuz
  • 88,405
  • 25
  • 200
  • 352
  • 1
    Do not post pictures of text or code please. I have downvoted your question for this reason and will retract the downvote once you replace your pictures with code/text. – fuz Jul 12 '20 at 20:47
  • 1
    Also general advice: you seem to be using libc functions in a binary that doesn't use the CRT code and that doesn't terminate itself through `exit`. This can cause weird issues. Don't do that. If you use the libc, always link in the CRT (i.e. don't use `-nostartfiles`) and don't do a raw exist system call. Instead, call `exit` to terminate your program. – fuz Jul 12 '20 at 20:49
  • Thanks for the answer ! I formated correctly the code – MagicWarthog Jul 12 '20 at 20:54
  • I am calling the syscall exit, i can't find anything on the net about calling exit another way, do you have an example ? – MagicWarthog Jul 12 '20 at 20:55
  • 2
    Call the `exit` function. It's in the libc. On Linux: `call exit`. On macOS: `call _exit`. Reading your code, I suppose you have the usual buffering issues because you don't give the libc a chance to flush its buffers on exit. – fuz Jul 12 '20 at 20:57
  • Yes it worked ! Thanks a lot :) – MagicWarthog Jul 12 '20 at 20:59
  • 1
    Please also consider following my other advice and avoid using `-nostartfiles` if you use the libc in any way. – fuz Jul 12 '20 at 21:00
  • And linking it manually using directly ld then ? – MagicWarthog Jul 12 '20 at 21:05
  • 2
    No. What I mean is: use the CRT which supplies `_start`. This code also initialises the libc for you. If it does not run on program start, some libc functions may not work. Have your assembly program start at `main` instead of `_start` and link through the C compiler (but without `-nostartfiles`) like normal. – fuz Jul 12 '20 at 21:06

1 Answers1

2

The correct way of exiting the program was to use a call to exit libc function instead of the raw syscall