NASM Forum > Programming with NASM
Linux x86. call error when linking gcc
AntonPotapov:
Linux x86.
When I link with ld, everything works.
I can't understand why when linking with gcc I can't call my function from the declared my_section?
Here's my code. If executed correctly, it should return error 1
main.asm
--- Code: ---section .text
global main
main:
mov eax, 1
call sum
int 0x80
section my_section
sum:
mov ebx, 1
ret
--- End code ---
Code compilation and linking.
Release:
--- Code: ---#/bin/bash
nasm -f elf main.asm
gcc -m32 -o main main.o
--- End code ---
Debug:
--- Code: ---#/bin/bash
nasm -f elf -Lw -g main.asm
gcc -m32 -o main main.o
--- End code ---
When I run the code I get:
--- Code: ---Segmentation fault (core dumped)
--- End code ---
This is what gdb says when running the Debug version:
--- Code: ---Single stepping until exit from function sum,
which has no line number information.
Program received signal SIGSEGV, Segmentation fault.
--- End code ---
Frank Kotler:
--- Code: ---section my_section ; read only!
mov ebx, 1 ; seg fault
--- End code ---
I think that's your problem.
Why the "home made" section?
If you must... call it "rw"...
gdb will like it better with "-F dwarf" on Nasm's command line...
untested!
Best,
Frank
Frank Kotler:
Ignore me.
But lose the home made section.
Best,
Frank
AntonPotapov:
I don't understand why exactly "mov ebx, 1" is causing the error? why there are no errors when linking with ld?
fredericopissarra:
The problem is simple: If you compile and take a look at the object file:
--- Code: ---$ nasm -felf32 -o main.o main.asm
$ objdump -h main.o
Sections:
Idx Name Siz. VMA LMA File off Algn
0 .text 0000000b 00000000 00000000 00000160 2**4
CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
1 my_section 00000006 00000000 00000000 00000170 2**0
CONTENTS, ALLOC, LOAD, READONLY, DATA
--- End code ---
Where's the "CODE" attribute!? This section is not "exectable". If you add this attribute to the section:
--- Code: ---...
section my_section exec
...
--- End code ---
And compile again, you'll get:
--- Code: ---$ nasm -felf32 -o main.o main.asm
$ objdump -h main.o
Sections:
Idx Name Siz. VMA LMA File off Algn
0 .text 0000000b 00000000 00000000 00000160 2**4
CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
1 my_section 00000006 00000000 00000000 00000170 2**0
CONTENTS, ALLOC, LOAD, READONLY, CODE
$ gcc -s -m32 -o main main.o
$ ./main
$ echo $?
1
--- End code ---
PS: Ahhhh.. I've changed the code a little:
--- Code: --- bits 32
section .text
global main
main:
mov eax, 1
call sum
; Since you are using the main() function, called from crt0.o, the
; end of your program is a simple return.
ret
section my_section exec
sum:
;mov ebx, 1 ; This should be avoided, since EBX should be preserved!
ret
--- End code ---
In general, if you are building a standalone program in assembly, avoid using external libraries like libc... But if you still want to use it, you MUST obey the ABI.
The difference:
--- Code: ---$ nasm -felf32 -o main.o main.asm # exit with ret
$ gcc -s -m32 main.o -o main
$ nasm -felf32 -o main2.o main2.asm # exit with sys_exit.
$ ld -s -melf_i386 -e main main2.o -o main2
$ ls -goh main*
-rwxr-xr-x 1 14K apr 25 11:40 main
-rwxr-xr-x 1 4,5K apr 25 11:42 main2
...
$ ldd main main2
main:
linux-gate.so.1 (0xf7f7c000)
libc.so.6 => /lib32/libc.so.6 (0xf7d78000)
/lib/ld-linux.so.2 (0xf7f7e000)
main2:
Not a dynamic executable
--- End code ---
Navigation
[0] Message Index
[#] Next page
Go to full version