#1645·gperftools

Use "GROUP" trick to make libprofiler.{a,so} alwaylink

Author: alkCreated Sep 15, 2026Updated Sep 15, 2026

See https://sourceware.org/bugzilla/show_bug.cgi?id=32816

The basic idea is to have a text file in place of actual library that says link "reference" .o and {.a,.so}. This is the same trick used by libc to "embed" pthread_atfork stuff into the binaries.

Pasting makefile example from the ticket:

CC=g++
CC=gcc
CC+= -B./
CFLAGS+= -O2
CFLAGS+= -g
LDFLAGS+=-Wl,--as-needed
#LDFLAGS+=-fuse-ld=gold
LD=./ld
#AS=./as

all: x y libprofiler.so libprofiler.a
        CPUPROFILE=p ./x
        CPUPROFILE=p ./y

cpuprofiler.o: CFLAGS += -fPIC
cpuprofiler-ref.o: CFLAGS += -fPIC

x: program.o libprofiler.so
        $(CC) -Wl,--as-need,-R,. -o $@ program.o libprofiler.so

y: program.o libprofiler.a
        $(CC) -Wl,--as-need -o $@ program.o libprofiler.a

libprofiler-core.so: cpuprofiler.o
        $(CC) -shared -o $@ $^

libprofiler.so: cpuprofiler-ref.o libprofiler-core.so
        echo "GROUP ($^)" > $@

libprofiler-core.a: libprofiler-core.a(cpuprofiler.o)

libprofiler.a: cpuprofiler-ref.o libprofiler-core.a
        echo "GROUP ($^)" > $@

.s.o:
        $(AS) $(ASFLAGS) -o $@ $<

.i.o:
        $(CC) -c $(CFLAGS) $<

clean:
        rm -f *.o x *.a *.so a.out *.lto* *.ltrans* *.res *.wpa*

tar:
        tar cfJ bug.tar.xz Makefile *.c