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

Steven Rostedt <[email protected]> Fri, 15 Aug 2025 13:26:03 -0400
Newsgroups org.kernel.vger.linux-trace-devel
Message-ID <[email protected]>
On Fri, 15 Aug 2025 10:25:52 +0100
Metin Kaya <[email protected]> wrote:

> >    Care to test this?  
> 
> Hi Steven,
> 
> I tried cherry-picking this patch on my stash (my HEAD is "b3f5849 - 
> libtraceevent: Save missed events in kbuffer_read_buffer()"), but it failed:
> 
> error: patch failed: src/event-parse-local.h:111
> error: src/event-parse-local.h: patch does not apply
> error: patch failed: src/event-parse.c:8724
> error: src/event-parse.c: patch does not apply

Doh!


> BTW, this is how I build trace-cmd statically:
> 
> TRACECMD_INSTALL_PATH="..."
> export LDFLAGS="-static" NO_AUDIT=yes NO_PYTHON=yes CFLAGS="-O3"
> make prefix=${TRACECMD_INSTALL_PATH}
> 
> Reverting the changes in libtraceevent/src/Makefile resolves this build 
> failure, but I guess it's the whole point of this patch?

Right. I see the issue. So back to getting to my hack that appears to work.
I decided to link all the objects together into a single object:

  ld -r -o libtraceevent-global.o $(OBJS)

And then use objcopy to make any functions that do not start with tep_ into
a static function.

Can you test this? I only included the change to the Make file. To make
sure it works, you still need to do the revert of your name change patch.

Thanks!

-- Steve

diff --git a/src/Makefile b/src/Makefile
index 1ff669ce8deb..f8bea4520f58 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): $(OBJS)
+$(LIBTRACEEVENT_STATIC): $(bdir)/libtraceevent.o
 	$(Q)$(call do_build_static_lib)
 
 $(LIBTRACEEVENT_SHARED): $(OBJS)
@@ -31,6 +31,19 @@ $(LIBTRACEEVENT_SHARED_VERSION): $(LIBTRACEEVENT_SHARED)
 $(LIBTRACEEVENT_SHARED_SO): $(LIBTRACEEVENT_SHARED_VERSION)
 	@ln -sf $(<F) $@
 
+$(bdir)/libtraceevent-global.o: $(OBJS)
+	ld -r -o $@ $^
+
+# Make all functions that do not start with tep_ to static
+find_local = \
+	 $(shell for f in a `nm $^ | grep ' T ' | cut -d' ' -f3`;  do \
+	  if [ "$${f#tep_}" = "$$f" ]; then echo --localize-symbol $$f; fi; done)
+
+$(bdir)/libtraceevent.o: $(bdir)/libtraceevent-global.o
+	objcopy $(call find_local) $< $@
+
+
+
 libtraceevent.so: $(LIBTRACEEVENT_SHARED_SO)
 
 libtraceevent: $(libtraceevent-y)