0

Is it possible to build a shared object file on Linux without using libc? I tried building the shared object using -nostdlib, and it complains that there is a conflicting type for built-in function 'memset'(I have my own version of the function defined within the shared object I am trying to build).

I am not using any libc functions from within the shared object file. I am building the shared object as follows :-

CC      = gcc
CFLAGS  = -Wall -Wextra -Werror -nostdlib
OUTPUTDIR = ./build
test: outputdir
    $(CC) $(CFLAGS) -c -fPIC test.c -o ${OUTPUTDIR}/test.o
    $(CC) $(CFLAGS) ${OUTPUTDIR}/test.o -shared -o ${OUTPUTDIR}/libtest.so

outputdir:
    mkdir -p ${OUTPUTDIR}

clean:
    rm -rf ${OUTPUTDIR}

1 Answers1

1

If you link with -nostdlib, you should also compile with -ffreestanding and/or -fno-builtin as well.

You also have to be careful that you do not reference a libc.so.6 symbol without linking against glibc. Things may appear to work superficially, but it tends to introduce breakage in certain environments, especially once additional IFUNCs are added to glibc. (Intel did that with the ICC 16 compiler library.)

Florian Weimer
  • 32,022
  • 3
  • 48
  • 92