Re: [RFC][PATCH] libtraceevent: Fix static library to hide hidden functions

Steven Rostedt <[email protected]> Tue, 19 Aug 2025 16:05:04 -0400
Newsgroups org.kernel.vger.linux-trace-devel
Message-ID <[email protected]>
On Tue, 19 Aug 2025 15:56:17 -0400
Steven Rostedt <[email protected]> wrote:

> Oops, I forgot that trace_seq* and kbuffer_* are also supplied. I made
> everything that didn't start with 'tep_*' "static". I could add kbuffer_*
> and trace_seq_* to that list (the easy way out), or I can parse
> `nm libtraceevent.so` and convert everything that's static in it to static
> in the static library.

Actually, this was easier than I though!

diff --git a/src/Makefile b/src/Makefile
index a8e9bfadfda5..dcbca123ecca 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -19,7 +19,7 @@ DEPS := $(OBJS:$(bdir)/%.o=$(bdir)/.%.d)
 $(bdir)/%.o: %.c
 	$(Q)$(call do_fpic_compile)
 
-$(LIBTRACEEVENT_STATIC): $(LIBTRACEEVENT_SHARED)
+$(LIBTRACEEVENT_STATIC): $(bdir)/libtraceevent.o
 	$(Q)$(call do_build_static_lib)
 
 $(LIBTRACEEVENT_SHARED): $(OBJS)
@@ -31,6 +31,17 @@ $(LIBTRACEEVENT_SHARED_VERSION): $(LIBTRACEEVENT_SHARED)
 $(LIBTRACEEVENT_SHARED_SO): $(LIBTRACEEVENT_SHARED_VERSION)
 	@ln -sf $(<F) $@
 
+$(bdir)/libtraceevent-global.o: $(OBJS)
+	ld -r -o $@ $^
+
+# Make static library match dynamic library wrt hidden functions
+find_local = \
+	$(shell for f in a `nm ${LIBTRACEEVENT_SHARED} | grep ' t ' | cut -d' ' -f3`;  do \
+	 if [ "$${f#tep_}" = "$$f" ]; then echo --localize-symbol $$f; fi; done)
+
+$(bdir)/libtraceevent.o: ${LIBTRACEEVENT_SHARED} $(bdir)/libtraceevent-global.o
+	objcopy $(call find_local) $< $@
+
 libtraceevent.so: $(LIBTRACEEVENT_SHARED_SO)
 
 libtraceevent: $(libtraceevent-y)
@@ -45,7 +56,7 @@ $(OBJS): | $(bdir)
 $(DEPS): | $(bdir)
 
 clean:
-	$(Q)$(call do_clean,$(OBJS) $(DEPS))
+	$(Q)$(call do_clean,$(OBJS) $(DEPS) $(bdir)libtraceevent.o)
 
 dep_includes := $(wildcard $(DEPS))
 


-- Steve