Clang Linking error: undefined reference to functi

2019-06-14 10:08发布

问题:

So I am following this tutorial https://www.cs.cornell.edu/~asampson/blog/llvm.html to make a pass that instruments a program by adding calls to an external function (which is logop in rtlib.c). But unlike the tutorial I am trying to instrument a larger code-base which is masstree: https://github.com/kohler/masstree-beta.

So as instructed for masstree I run ./configure first but then I edit the generated Makefile to use clang (instead of gcc/g++) and run my pass. I also add rtlib.c in the masstree source files so that it gets converted to rtlib.o with the rest of the masstree source files. Here is the relevant part of the modified Makefile with my changes highlighted with an arrow (I also added the $(rtlib) to link it with other .o files to generate executables):

AR = ar
rtlib = rtlib.o  <===
CC2 = clang -w -v -Xclang -load -Xclang  /.../llvm-3.4/Release+Asserts/lib/SkeletonPass.so `llvm-config --cflags` 
CXX2 = clang++ -v -w -Xclang -load -Xclang /.../llvm-3.4/Release+Asserts/lib/SkeletonPass.so -std=c++11 `llvm-config --cppflags --libs --cflags --cxxflags core --ldflags` <===
CC = clang -v <===
CXX = clang++ -v -std=c++11 <===
CPPFLAGS = 
CXXFLAGS = -g -W -Wall -O3
DEPSDIR := .deps
DEPCFLAGS = -MD -MF $(DEPSDIR)/$*.d -MP
ifeq ($(strip $(MEMMGR)), )
  MEMMGR = 
endif
ifneq ($(strip $(KEYSWAP)), )
  CPPFLAGS += -DKEYSWAP
endif
ifneq ($(strip $(NOPREFETCH)), )
  CPPFLAGS += -DNOPREFETCH
endif
ifneq ($(strip $(NOSUPERPAGE)), )
  CPPFLAGS += -DNOSUPERPAGE
endif
LIBS =  -lpthread -lm
LDFLAGS = 

all: test_atomics mtd mtclient mttest

%.o: %.c config.h $(DEPSDIR)/stamp
    $(CXX2) $(CPPFLAGS) $(CXXFLAGS) $(DEPCFLAGS) -include config.h -c -o $@ $<

%.o: %.cc config.h $(DEPSDIR)/stamp
    $(CXX2) $(CPPFLAGS) $(CXXFLAGS) $(DEPCFLAGS) -include config.h -c -o $@ $<

%.S: %.o
    objdump -S $< > $@

libjson.a: json.o string.o straccum.o str.o msgpack.o \
    clp.o kvrandom.o compiler.o memdebug.o kvthread.o
    @/bin/rm -f $@
    $(AR) cru $@ $^

KVTREES = query_masstree.o \
    value_string.o value_array.o value_versioned_array.o \
    string_slice.o

mtd: mtd.o log.o checkpoint.o file.o misc.o $(rtlib) $(KVTREES) \
    kvio.o libjson.a
    $(CXX) $(CXXFLAGS) -o $@ $^ $(MEMMGR) $(LDFLAGS) $(LIBS)

mtclient: mtclient.o misc.o testrunner.o kvio.o $(rtlib) libjson.a
    $(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS)

mttest: mttest.o misc.o checkpoint.o $(rtlib) $(KVTREES) testrunner.o \
    kvio.o libjson.a
    $(CXX) $(CXXFLAGS) -o $@ $^ $(MEMMGR) $(LDFLAGS) $(LIBS)

test_string: test_string.o string.o $(rtlib) straccum.o compiler.o
    $(CXX) $(CXXFLAGS) -o $@ $^ $(MEMMGR) $(LDFLAGS) $(LIBS)

test_atomics: test_atomics.o string.o straccum.o kvrandom.o $(rtlib) \
    json.o compiler.o kvio.o
    $(CXX) $(CXXFLAGS) -o $@ $^ $(MEMMGR) $(LDFLAGS) $(LIBS)

jsontest: jsontest.o string.o straccum.o json.o compiler.o $(rtlib)
    $(CXX) $(CXXFLAGS) -o $@ $^ $(MEMMGR) $(LDFLAGS) $(LIBS)

msgpacktest: msgpacktest.o string.o straccum.o json.o compiler.o msgpack.o $(rtlib)
    $(CXX) $(CXXFLAGS) -o $@ $^ $(MEMMGR) $(LDFLAGS) $(LIBS)

scantest: scantest.o compiler.o misc.o $(rtlib) $(KVTREES) libjson.a
    $(CXX) $(CXXFLAGS) -o $@ $^ $(MEMMGR) $(LDFLAGS) $(LIBS)

I use CC2 and CXX2 to generate the instrumented .o files while CC and CXX to link them into executables. Here is the error I get when I run make:

mtd.o: In function main': /home/.../masstree-beta-master/mtd.cc:730: undefined reference tologop' /home/.../masstree-beta-master/mtd.cc:730: undefined reference to logop' /home/.../masstree-beta-master/mtd.cc:732: undefined reference tologop' /home/.../masstree-beta-master/mtd.cc:732: undefined reference to logop' /home/.../masstree-beta-master/mtd.cc:736: undefined reference tologop' mtd.o:/home/.../masstree-beta-master/mtd.cc:736: more undefined references to `logop' follow clang: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [mtd] Error 1.

Any Idea on why is the reference to my function logop (which is in rtlib.c) undefined even though I add rtlib.o in the linking part to generate executables?

Thank you so much!

回答1:

The symbol exported were not the same. I checked using nm --format sysv *file.o* to make sure the symbols exported from rtlib.o and wherever they are used is the same.