1

Okay, I just wanted to run a simple GNU Assembler program, but something is going wrong.

.data                         
 
hello_str:                    
        .string "Hello, world!\n"
 
                              
        .set hello_str_length, . - hello_str - 1
 
.text                         
 
.globl  main                  
.type   main, @function       
 
 
main:
        movl    $4, %eax      
        movl    $1, %ebx      
        movl    $hello_str, %ecx  
        movl    $hello_str_length, %edx 
 
        int     $0x80         
 
        movl    $1, %eax      
        movl    $0, %ebx      
        int     $0x80         
 
        .size   main, . - main    

I type gcc -no-pie hello.s -o hello, then ./hello and gets Segmentation fault (core dumped).

I thought maybe the whole point is that I'm running it on a 64-bit system, so I added .code32 at the beginning - it didn't help.

I tried to run it differently. For example, gcc -m32 -no-pie hello.s and gcc -m32 --no-pie hello.s gets me the same message:

/usr/bin/ld: cannot find Scrt1.o: No such file or directory
/usr/bin/ld: cannot find crti.o: No such file or directory
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/11/libgcc.a when searching for -lgcc
/usr/bin/ld: cannot find -lgcc: No such file or directory
/usr/bin/ld: skipping incompatible /usr/lib/x86_64-linux-gnu/libgcc_s.so.1 when searching for libgcc_s.so.1
/usr/bin/ld: cannot find libgcc_s.so.1: No such file or directory
/usr/bin/ld: skipping incompatible /usr/lib/x86_64-linux-gnu/libgcc_s.so.1 when searching for libgcc_s.so.1
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/11/libgcc.a when searching for -lgcc
/usr/bin/ld: cannot find -lgcc: No such file or directory
collect2: error: ld returned 1 exit status

Installed gcc-multilib then I finally can type gcc -m32 -no-pie hello.s, but when I type ./a.out it throws:

-bash: ./a.out: cannot execute binary file: Exec format error

I am using Ubuntu on Windows.

DeBug
  • 13
  • 3
  • 2
    You need the `-m32` as this is 32 bit code. The `.code32` directive is a red herring. It is not what you are looking for. Do not use this directive, it will only break your code. The solution is that you need to install the support libraries for building 32 bit programs. On Debian-derived systems, this might be `gcc-multilib` or similar. As you have not stated which operating system or distribution you are programming for, I cannot say for sure. Always put this information into your question! – fuz Mar 18 '23 at 18:01
  • Are you using WSL1? `int $0x80` system calls in 64-bit code (like from `gcc -no-pie`) are a bad idea, but they do technically work ([What happens if you use the 32-bit int 0x80 Linux ABI in 64-bit code?](https://stackoverflow.com/q/46087730)) if the kernel supports CONFIG_IA32_EMULATION at all. Getting a segfault is surprising, and might indicate that you won't be able to use 32-bit code at all on this Linux install, even in a static executable without libraries where you put this code in `_start:` and build with `gcc -m32 -no-pie -static -nostdlib hello.s`. Use `strace ./a.out` – Peter Cordes Mar 19 '23 at 03:35
  • Use WLS2 if you want to use 32-bit binaries instead of 64-bit. (e.g. [32-bit system calls or executables on 64bit Windows Subsystem For Linux? Or a working 64-bit Hello World?](https://stackoverflow.com/q/57915303) shows a 64-bit hello world.) – Peter Cordes Mar 19 '23 at 06:01

0 Answers0