[arch-general] Issue linking 32-bit GAS assembly

Eli Schwartz eschwartz at archlinux.org
Wed Oct 17 22:01:44 UTC 2018


On 10/17/18 3:36 PM, Dutch Ingraham wrote:
> Hi all:
> 
> I'm having a problem linking 32-bit GAS assembly code that references external
> C libraries on an up-to-date 64-bit Arch installation.
> 
> I have enabled the 32-bit repositories and installed the multilib-devel group
> and lib32-glibc. I have updated the library cache with ldconfig and rebooted.
> 
> The command to assemble is: <as --32 -o filename.o filename.s>.  Assembly
> succeeds.
> 
> However, when linking using the command
> <ld -melf_i386 --dynamic-linker /lib32/ld-linux.so.2 -lc -o filename filename.o>
> 
> the command fails with:
> 	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
> 
> Note this linker command (or a hierarchy-appropriate one) seems standard, and 
> succeeds on a 64-bit Debian OS.

So, this is magical, why do you think it's supposed to work? Since Arch
isn't Debian, the first major issue is it violates the hierarchy... and
you didn't fix it, because there is no /lib32.

Second issue: the *error* it is showing is that it cannot find a
libc.so/libc.a, so the question you should be asking yourself is, what's
the search path? Apparently, the default search path on Debian works,
but here on Arch, /usr/lib32 is not in fact in the search path.

Fix those two problems and it compiles fine, and runs too.

But no.....

> There is a fairly recent Forum question (but pre-dating the discontinuance of
> i686 support) regarding the same issue at 
> https://bbs.archlinux.org/viewtopic.php?id=229235 which indicates the command
> should be:
> 
> <ld -melf_i386 -shared -L /usr/lib32 -dynamic-linker /lib/ld-linux.so.2 -o \
> filename filename.o -lc>
> 
> This command succeeds, insofar as the linker returns an exit code of 0. However,
> running the program (and all other similar programs) fails with "Illegal
> instruction (core dumped)." Assembling with debugging symbols and running a
> backtrace didn't enlighten me.

This fixes both problems, therefore it compiles cleanly. But it also
adds the "-shared" flag, which you have not asked yourself why you are
using it. Did you read the manpage for this?

-shared
    Create a shared library.  This is currently only supported on ELF,
    XCOFF and SunOS platforms.  On SunOS, the linker will automatically
    create a shared library if the -e option is not used and there are
    undefined symbols in the link.

...

Think, then do.

-- 
Eli Schwartz
Bug Wrangler and Trusted User

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <https://lists.archlinux.org/pipermail/arch-general/attachments/20181017/55b739d0/attachment-0001.asc>


More information about the arch-general mailing list