0

I have problem with assembly program on my VirtualBox.
On VB I have Linux Mint(64 bit). I tried to compile this assembler code(in file my_file.asm):

extern printf  
extern scanf  

section .data  
format: db "%d",0  
napis: db "a/b=%d",10,0  

section .bss  
a: resd 1  
b: resd 1  

global main  
section .text  
main:  

mov rdi, format  
mov rsi, a  
xor rax, rax  
call scanf  

mov rdi, format  
mov rsi, b  
xor rax, rax  
call scanf  

mov eax, [a]  
xor edx, edx  
div dword[b]  

ret

At first I type: nasm -f elf64 myfile.asm
Then I type: gcc myfile.o -o myfile
And after this i have this error message:
"/usr/bin/ld: plik.o: relocation R_X86_64_32S against .bss' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status "

So i add -no-pie to gcc command: gcc -o test test.o -no-pie

Now, when I use ./my_file I have this error: Segmentation fault (core dumped) and I don't know what to do.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 1
    Looks about right except for not maintaining stack alignment. Put `sub rsp, 8` at `main` and `add rsp, 8` before the `ret`. Also, use a debugger to see where the crash is. – Jester Mar 21 '19 at 14:12
  • 1
    @lurker this is not a standalone program. It has `main` and it's linked to libc. – Jester Mar 21 '19 at 14:21
  • Not related to your segfault(I think Jester covers that with his stack alignment comment). If you wanted a position independent version of the NASM code without requiring `-no-pie` you can add `default rel` directive to the beginning of your ASM file and then change `call scanf` to `call scanf wrt ..plt` (there is a space between `wrt` and the `..`) – Michael Petch Mar 21 '19 at 14:26
  • @MichaelPetch: they'd also need `default rel` for PIE, otherwise `[a]` will be an absolute `[disp32]` addressing mode. PIE does allow 64-bit absolute addresses, though, so `mov rdi, format` is just inefficient, not an actual error. (Because NASM chooses to assemble that to `mov r64, imm64`, unlike YASM which chooses `mov r/m64, sign_extended_imm32`.) – Peter Cordes Mar 21 '19 at 14:58
  • @PeterCordes : Pretty sure I said they'd have to add `default rel` to their ASM file. From my comment: _"you can add `default rel` directive to the beginning of your ASM file and then "..._ – Michael Petch Mar 21 '19 at 15:06
  • @MichaelPetch: IDK how I missed that, sorry. – Peter Cordes Mar 21 '19 at 15:11

0 Answers0