Author Topic: Linux x86. call error when linking gcc  (Read 3329 times)

Offline AntonPotapov

  • Jr. Member
  • *
  • Posts: 13
Linux x86. call error when linking gcc
« 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
Code: [Select]
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:
Code: [Select]
#/bin/bash
nasm -f elf main.asm
gcc -m32 -o main main.o

Debug:
Code: [Select]
#/bin/bash
nasm -f elf -Lw -g main.asm
gcc -m32 -o main main.o

When I run the code I get:
Code: [Select]
Segmentation fault (core dumped)

This is what gdb says when running the Debug version:
Code: [Select]
Single stepping until exit from function sum,
which has no line number information.

Program received signal SIGSEGV, Segmentation fault.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Linux x86. call error when linking gcc
« Reply #1 on: April 25, 2023, 01:53:45 AM »

Code: [Select]
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


Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Linux x86. call error when linking gcc
« Reply #2 on: April 25, 2023, 02:51:28 AM »
Ignore me.

But lose the home made section.

Best,
Frank


Offline AntonPotapov

  • Jr. Member
  • *
  • Posts: 13
Re: Linux x86. call error when linking gcc
« Reply #3 on: April 25, 2023, 12:42:01 PM »
I don't understand why exactly "mov ebx, 1" is causing the error?  why there are no errors when linking with ld?

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 368
  • Country: br
Re: Linux x86. call error when linking gcc
« Reply #4 on: April 25, 2023, 02:27:34 PM »
The problem is simple: If you compile and take a look at the object file:
Code: [Select]
$ 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:
Code: [Select]
...
  section my_section exec
...
And compile again, you'll get:
Code: [Select]
$ 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:
Code: [Select]
  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:
Code: [Select]
$ 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
« Last Edit: April 25, 2023, 02:56:57 PM by fredericopissarra »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Linux x86. call error when linking gcc
« Reply #5 on: April 25, 2023, 05:14:23 PM »
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



Offline fredericopissarra

  • Full Member
  • **
  • Posts: 368
  • Country: br
Re: Linux x86. call error when linking gcc
« Reply #6 on: April 25, 2023, 06:01:10 PM »
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! :)

Offline debs3759

  • Global Moderator
  • Full Member
  • *****
  • Posts: 221
  • Country: gb
    • GPUZoo
Re: Linux x86. call error when linking gcc
« Reply #7 on: April 25, 2023, 07:10:50 PM »
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 :)
My graphics card database: www.gpuzoo.com

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 368
  • Country: br
Re: Linux x86. call error when linking gcc
« Reply #8 on: April 25, 2023, 08:18:42 PM »
Frank was asking Anton why he is doing what he is doing :)
Oops! ;)

Offline AntonPotapov

  • Jr. Member
  • *
  • Posts: 13
Re: Linux x86. call error when linking gcc
« Reply #9 on: April 26, 2023, 01:59:25 PM »
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.
« Last Edit: April 26, 2023, 02:09:01 PM by AntonPotapov »

Offline AntonPotapov

  • Jr. Member
  • *
  • Posts: 13
Re: Linux x86. call error when linking gcc
« Reply #10 on: April 26, 2023, 02:01:14 PM »
Thanks to fredericopissarra

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 368
  • Country: br
Re: Linux x86. call error when linking gcc
« Reply #11 on: April 26, 2023, 02:04:20 PM »
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...

Offline AntonPotapov

  • Jr. Member
  • *
  • Posts: 13
Re: Linux x86. call error when linking gcc
« Reply #12 on: April 26, 2023, 02:06:37 PM »
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

Offline AntonPotapov

  • Jr. Member
  • *
  • Posts: 13
Re: Linux x86. call error when linking gcc
« Reply #13 on: April 26, 2023, 02:26:49 PM »

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 368
  • Country: br
Re: Linux x86. call error when linking gcc
« Reply #14 on: April 26, 2023, 06:36:32 PM »
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:
Code: [Select]
  section mysection exec  ; mysection isn't a system section, now with exec attrib set.

[]s
Fred