NASM - The Netwide Assembler
NASM Forum => Programming with NASM => Topic started by: AntonPotapov on April 25, 2023, 12:12:47 AM
-
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
section .text
global main
main:
mov eax, 1
call sum
int 0x80
section my_section
sum:
mov ebx, 1
ret
Code compilation and linking.
Release:
#/bin/bash
nasm -f elf main.asm
gcc -m32 -o main main.o
Debug:
#/bin/bash
nasm -f elf -Lw -g main.asm
gcc -m32 -o main main.o
When I run the code I get:
Segmentation fault (core dumped)
This is what gdb says when running the Debug version:
Single stepping until exit from function sum,
which has no line number information.
Program received signal SIGSEGV, Segmentation fault.
-
section my_section ; read only!
mov ebx, 1 ; seg fault
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
-
Ignore me.
But lose the home made section.
Best,
Frank
-
I don't understand why exactly "mov ebx, 1" is causing the error? why there are no errors when linking with ld?
-
The problem is simple: If you compile and take a look at the object file:
$ 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
Where's the "CODE" attribute!? This section is not "exectable". If you add this attribute to the section:
...
section my_section exec
...
And compile again, you'll get:
$ 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
PS: Ahhhh.. I've changed the code a little:
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
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:
$ 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
-
Thanks Fred!
Hi again Anton,
I think we may need to ask WHY you are doing some of the things you do. Maybe you have something special in mind.
Best,
Frank
-
Thanks Fred!
Hi again Anton,
I think we may need to ask WHY you are doing some of the things you do. Maybe you have something special in mind.
Best,
Frank
What do you mean, Frank?
Writing about technical stuff?
Well... I like to write a lot! :)
-
Thanks Fred!
Hi again Anton,
I think we may need to ask WHY you are doing some of the things you do. Maybe you have something special in mind.
Best,
Frank
What do you mean, Frank?
Writing about technical stuff?
Well... I like to write a lot! :)
Frank was asking Anton why he is doing what he is doing :)
-
Frank was asking Anton why he is doing what he is doing :)
Oops! ;)
-
Thanks Fred!
Hi again Anton,
I think we may need to ask WHY you are doing some of the things you do. Maybe you have something special in mind.
Best,
Frank
I recently started learning assembler. And I'm trying different ways of writing code. Next I plan to learn SSE. and above mentioned ABI, I'll read about it too.
-
Thanks to fredericopissarra
-
I recently started learning assembler. And I'm trying different ways of writing code. Next I plan to learn SSE. and above mentioned ABI, I'll read about it too.
Since you are using NASM (necessarily for x86), search for SysV ABI for i386 and amd64 (or x86_64) and study it... If you are using Windows, take a look at Microsoft calling conventions (or MS ABI)...
ABI are useful if you mix code with C and use syscalls/system libraries...
-
I recently started learning assembler. And I'm trying different ways of writing code. Next I plan to learn SSE. and above mentioned ABI, I'll read about it too.
Since you are using NASM (necessarily for x86), search for SysV ABI for i386 and amd64 (or x86_64) and study it... If you are using Windows, take a look at Microsoft calling conventions (or MS ABI)...
ABI are useful if you mix code with C and use syscalls/system libraries...
Thanks, I will keep that in mind. As long as I am programming in linux. It is easier for me to write programs in linux. or so it seems to me
-
I found the types of sections: https://nasm.us/doc/nasmdoc8.html#section-8.9.2
-
I found the types of sections: https://nasm.us/doc/nasmdoc8.html#section-8.9.2
Yep. Of those, usually you'll use .text, .data, .rodata and/or .bss.
All .xxx are system sections with predefined attributes. You can create your own sections without the '.', like in mysection which will have always the attributes noexec and nowrite, by default. Of course you can override this.
Since Linux (and Windows) use paging, these sections will be placed (probably) in different pages with specific attributes. Then, in your case, as you defined mysection without attributes, this page (4 KiB) will be readonly (noexec) and not executable (noexec), hence the segmentation fault. As I told you before, you only need to set exec attribute to override this:
section mysection exec ; mysection isn't a system section, now with exec attrib set.
[]s
Fred
-
I found the types of sections: https://nasm.us/doc/nasmdoc8.html#section-8.9.2
Yep. Of those, usually you'll use .text, .data, .rodata and/or .bss.
All .xxx are system sections with predefined attributes. You can create your own sections without the '.', like in mysection which will have always the attributes noexec and nowrite, by default. Of course you can override this.
Since Linux (and Windows) use paging, these sections will be placed (probably) in different pages with specific attributes. Then, in your case, as you defined mysection without attributes, this page (4 KiB) will be readonly (noexec) and not executable (noexec), hence the segmentation fault. As I told you before, you only need to set exec attribute to override this:
section mysection exec ; mysection isn't a system section, now with exec attrib set.
[]s
Fred
Yes, thank you. Now I've got it figured out.