|
From: Joseph K. <jk...@us...> - 2011-10-01 02:24:29
|
> I'm on Ubuntu 10.04, and I want to link libelf, libdwarf, and libelftc
> into a shared library I'm building for symbol lookup. I tried editing
> mk/os.Linux to set MKPIC to "yes", but things seem to be going very
> wrong.
> Each .c file gets turned into a .so file, and I end up with a
> lib(elf|dwarf)_pic.a file. There happens to be a libdwarf.c file, so
> when the build system tries to link addr2line with -ldwarf, it picks
> up libdwarf.so instead of libdwarf.a, and the build fails.
> I think for my purposes I can just life libdwarf_pic.a, libelf_pic.a,
> and libelftc_pic.a, but it would be nice if the build didn't fail.
The ruleset that is part of the "pmake" package on Ubuntu does not
appear to handle shared library creation very well.
I needed the following changes to successfully build a .so:
First, a hack/patch was needed for "/usr/share/mk/bsd.lib.mk":
--- bsd.lib.mk-- 2010-01-05 12:01:04.000000000 +0530
+++ bsd.lib.mk 2011-09-30 15:20:29.986766086 +0530
@@ -114,9 +114,9 @@
# Platform-independent linker flags for ELF shared libraries
.if ${OBJECT_FMT} == "ELF"
SHLIB_SOVERSION=${SHLIB_MAJOR}
-SHLIB_SHFLAGS=-soname lib${LIB}.so.${SHLIB_SOVERSION}
-SHLIB_LDSTARTFILE= ${DESTDIR}/usr/lib/crtbeginS.o
-SHLIB_LDENDFILE= ${DESTDIR}/usr/lib/crtendS.o
+SHLIB_SHFLAGS=-Wl,-soname=lib${LIB}.so.${SHLIB_SOVERSION}
+#SHLIB_LDSTARTFILE= ${DESTDIR}/usr/lib/crtbeginS.o
+#SHLIB_LDENDFILE= ${DESTDIR}/usr/lib/crtendS.o
.endif
CFLAGS+= ${COPTS}
@@ -256,24 +256,22 @@
.if defined(DESTDIR)
$(CC) -shared ${SHLIB_SHFLAGS} -o ${.TARGET} \
${SHLIB_LDSTARTFILE} \
- --whole-archive lib${LIB}_pic.a \
+ -Wl,--whole-archive lib${LIB}_pic.a \
-nostdlib -L${DESTDIR}${LIBDIR} -R${LIBDIR} \
- --no-whole-archive ${LDADD} \
+ -Wl,--no-whole-archive ${LDADD} \
${SHLIB_LDENDFILE}
.else
$(CC) -shared ${SHLIB_SHFLAGS} -o ${.TARGET} \
${SHLIB_LDSTARTFILE} \
- --whole-archive lib${LIB}_pic.a --no-whole-archive ${LDADD} \
- ${SHLIB_LDENDFILE}
+ -Wl,--whole-archive lib${LIB}_pic.a -Wl,--no-whole-archive \
+ ${LDADD} ${SHLIB_LDENDFILE}
.endif
-.if ${OBJECT_FMT} == "ELF"
rm -f lib${LIB}.so.${SHLIB_MAJOR}
ln -s lib${LIB}.so.${SHLIB_MAJOR}.${SHLIB_MINOR} \
lib${LIB}.so.${SHLIB_MAJOR}
rm -f lib${LIB}.so
ln -s lib${LIB}.so.${SHLIB_MAJOR}.${SHLIB_MINOR} \
lib${LIB}.so
-.endif
LOBJS+= ${LSRCS:.c=.ln} ${SRCS:M*.c:.c=.ln}
LLIBS?= -lc
You also need to set the SHLIB_{MAJOR,MINOR}, OBJECT_FMT and MKPIC
knobs in the library Makefiles. For example, for libelf:
diff --git a/trunk/libelf/Makefile b/trunk/libelf/Makefile
index 6fb27d0..b903f1c 100644
--- a/trunk/libelf/Makefile
+++ b/trunk/libelf/Makefile
@@ -155,4 +155,9 @@ libelf_convert.c: elf_types.m4 libelf_convert.m4
libelf_fsize.c: elf_types.m4 libelf_fsize.m4
libelf_msize.c: elf_types.m4 libelf_msize.m4
+MKPIC= yes
+SHLIB_MAJOR=1
+SHLIB_MINOR=0
+OBJECT_FMT=ELF
+
.include "${TOP}/mk/elftoolchain.lib.mk"
With these two changes, "pmake" should build a working
"lib{LIB}.so.1.0" file:
% pmake
...snip...
building shared elf library (version 1.0)
cc -shared -Wl,-soname=libelf.so.1 -o libelf.so.1.0 \
-Wl,--whole-archive libelf_pic.a -Wl,--no-whole-archive
rm -f libelf.so.1
ln -s libelf.so.1.0 libelf.so.1
rm -f libelf.so
ln -s libelf.so.1.0 libelf.so
Notes:
* NetBSD [bmake][1] could possibly be an alternative "BSD make" for
GNU/Linux, but I haven't checked it out yet.
[1]: http://www.crufty.net/help/sjg/bmake.h
* The other thing to keep in mind is that the base Ubuntu system
ships with a "libelf.so.1"; you would want to ensure that
applications compiled against our headers don't use this library
at runtime.
HTH,
Koshy
|