You are not logged in.

#1 2017-08-19 00:05:36

Diogo Gomes
Member
Registered: 2016-07-29
Posts: 17

Building 32-bit assembly on 64-bit computer

Hi,

I'm trying to learn 32-bit assembly, and i'm running into trouble when i try to use c functions.
I can assemble and link statically using as --32 test.s -o test.o and ld --melf_i386 test.o -o test, but i can't get ld to dynamically link 32-bit objects.
This is my code:

#Test using libs

.section .data
  helloworld:
    .ascii "hello wordl\n\0"

.section .text
.globl _start
_start:
  pushl $helloworld
  call printf

  pushl $0
  call exit

This is my call to ld and it's output:

% ld -melf_i386 -dynamic-linker /lib/ld-linux.so.2 -o helloworld-lib helloworld-lib.o -lc  
ld: skipping incompatible /usr/lib/libc.so when searching for -lc
ld: skipping incompatible /usr/lib/libc.a when searching for -lc
ld: cannot find -lc

I have installed gcc-multilib, but it didn't change anything. I really need to use 32-bit assembly, and not 64-bit. If this doesn't work, i'll have to use a VM, but i'm working on a laptop, so batery life does matter to me tongue

Thanks in advance.

Offline

#2 2017-08-19 09:43:25

Lone_Wolf
Administrator
From: Netherlands, Europe
Registered: 2005-10-04
Posts: 13,069

Re: Building 32-bit assembly on 64-bit computer

$ pacman -Fs libc.so
core/glibc 2.25-7
    usr/lib/libc.so
community/aarch64-linux-gnu-glibc 2.26-1
    usr/aarch64-linux-gnu/usr/lib/libc.so
community/musl 1.1.16-2
    usr/lib/musl/lib/libc.so
multilib/lib32-glibc 2.25-7
    usr/lib32/libc.so
$ 

Is lib32-glibc installed ?


Disliking systemd intensely, but not satisfied with alternatives so focusing on taming systemd.

clean chroot building not flexible enough ?
Try clean chroot manager by graysky

Offline

#3 2017-08-19 21:17:38

Diogo Gomes
Member
Registered: 2016-07-29
Posts: 17

Re: Building 32-bit assembly on 64-bit computer

Lone_Wolf wrote:

Is lib32-glic installed ?

Yes, it's installed

Offline

#4 2017-08-20 01:30:47

progandy
Member
Registered: 2012-05-17
Posts: 5,271

Re: Building 32-bit assembly on 64-bit computer

The linker scripts for ld are not written for archs multilib. You can try to add -L /usr/lib32. Maybe that is worth a bug report?
Edit: When linking to libc, I suggest you do it with gcc, then you don't have to manage the whole libc initialization manually:

gcc -m32 -o test test.o -lc

If you wish to do it yourself, maybe this will help you: https://dev.gentoo.org/~vapier/crt.txt

Last edited by progandy (2017-08-20 01:55:35)


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Offline

#5 2017-08-20 12:24:14

Diogo Gomes
Member
Registered: 2016-07-29
Posts: 17

Re: Building 32-bit assembly on 64-bit computer

Okay, so the ld -melf_i386 -shared -L /usr/lib32 -dynamic-linker /lib/ld-linux.so.2 -o helloworld-lib helloworld-lib.o -lc worked! Now I can actually link it and run it correctly.
The gcc option gives a linker error:

% gcc -m32 -o helloworld-lib helloworld-lib.o -lc

helloworld-lib.o: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../lib32/Scrt1.o:(.text+0x0): first defined here
/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../lib32/Scrt1.o: In function `_start':
(.text+0x28): undefined reference to `main'
collect2: error: ld returned 1 exit status
progandy wrote:

Maybe that is worth a bug report?

How should I do that?

Offline

Board footer

Powered by FluxBB