2

I have an sample application and an make file.

  • main.c file contents
    #include <stdio.h> /*int mainentry_entry(int argc, char *argv[]) {*/ int main(int argc, char *argv[]) { int i; printf("argc = %d\n", argc); for (i = 0; i < argc; i++) { printf("argv[%d] = %s\n", i, argv[i]); } return 0; } 
  • makefile contents
    Cl = i686-linux-gnu-ld CC = x86_i586-gcc CFLAGS = -Wall -Wextra -g CFLAGS += -I/usr/i686-linux-gnu/include TARGET = test_program OBJS = main.o all: $(TARGET) $(TARGET): $(OBJS) $(Cl) -L/lib/i386-linux-gnu/ -lc -o $(TARGET) $(OBJS) main.o: main.c $(CC) $(CFLAGS) -c main.c clean: rm -f $(TARGET) $(OBJS) 

I am trying to compile with PikeOS (x86_i586-gcc) tool chain and link with the 32 bit Debian (i686-linux-gnu-ld) tool chain, but the output I am getting is an test_program which has reference to /usr/lib/libc.so.1 which is not present in the /usr/lib/ folder path.

Can someone explain me why it's not taking the libc.so.6 which is present in /lib/i386-linux-gnu/libc.so.6?

~$ ldd test_program linux-gate.so.1 (0xea48c000) libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xea200000) /usr/lib/libc.so.1 => /lib/ld-linux.so.2 (0xea48e000) 
0

1 Answer 1

2

ld is picking up libc.so in /lib/i386-linux-gnu, that’s how you end up with the

 libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xea200000) 

entry in ldd’s output.

The /usr/lib/libc.so.1 entry is the dynamic linker; this is hinted at by the resolution given by ldd (pointing to the real 32-bit x86 dynamic linker), and more explicitly by file’s output:

$ file main main: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /usr/lib/libc.so.1, with debug_info, not stripped 

This happens because the default dynamic linker used by ld is /usr/lib/libc.so.1, and you didn’t mention anything else on the ld command line.

The real underlying issue is that your ld command isn’t the right way to link a C program with GCC; instead of ld, you should use gcc itself:

Cl = i686-linux-gnu-gcc 

This will ensure that the correct arguments are provided to ld, including the appropriate dynamic linker and the necessary C startup files. You can see this in detail by running gcc verbosely:

$ i686-linux-gnu-gcc -v -L/lib/i386-linux-gnu/ -lc -o main main.o […] /usr/lib/gcc-cross/i686-linux-gnu/10/collect2 -plugin /usr/lib/gcc-cross/i686-linux-gnu/10/liblto_plugin.so -plugin-opt=/usr/lib/gcc-cross/i686-linux-gnu/10/lto-wrapper -plugin-opt=-fresolution=/tmp/user/1000/cciJ4Cer.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --sysroot=/ --build-id --eh-frame-hdr -m elf_i386 --hash-style=gnu --as-needed -dynamic-linker /lib/ld-linux.so.2 -pie -o main /usr/lib/gcc-cross/i686-linux-gnu/10/../../../../i686-linux-gnu/lib/../lib/Scrt1.o /usr/lib/gcc-cross/i686-linux-gnu/10/../../../../i686-linux-gnu/lib/../lib/crti.o /usr/lib/gcc-cross/i686-linux-gnu/10/crtbeginS.o -L/lib/i386-linux-gnu/ -L/usr/lib/gcc-cross/i686-linux-gnu/10 -L/usr/lib/gcc-cross/i686-linux-gnu/10/../../../../i686-linux-gnu/lib/../lib -L/lib/i386-linux-gnu -L/lib/../lib -L/usr/lib/i386-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc-cross/i686-linux-gnu/10/../../../../i686-linux-gnu/lib -lc main.o -lgcc --push-state --as-needed -lgcc_s --pop-state -lc -lgcc --push-state --as-needed -lgcc_s --pop-state /usr/lib/gcc-cross/i686-linux-gnu/10/crtendS.o /usr/lib/gcc-cross/i686-linux-gnu/10/../../../../i686-linux-gnu/lib/../lib/crtn.o COLLECT_GCC_OPTIONS='-v' '-L/lib/i386-linux-gnu/' '-o' 'main' '-mtune=generic' '-march=i686' 

As you can see, there’s quite a bit more to linking main than your ld command accounts for. (Strictly speaking, using i686-linux-gnu-gcc here also means you don’t need to specify -L/lib/i386-linux-gnu/ -lc.)

Incidentally, if you’re building for 32-bit x86 on Debian, I’m not sure there’s any reason to use the PikeOS toolchain — you should use the GCC cross-compiler for everything.

1
  • Thanks for your input, it worked for me. Commented Nov 22, 2024 at 11:52

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.