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

Metin Kaya <[email protected]> Wed, 20 Aug 2025 11:39:55 +0100
Newsgroups org.kernel.vger.linux-trace-devel
Message-ID <[email protected]>
On 19/08/2025 9:05 PM, Steven Rostedt wrote:
> 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

The last error is new:

   BUILD                  trace-cmd
/usr/bin/ld: /ssd/tracecmd-work/trace-cmd/tracecmd/trace-setup-guest.o: 
in function `trace_setup_guest':
trace-setup-guest.c:(.text+0x5c8): warning: Using 'getgrnam' in 
statically linked applications requires at runtime the shared libraries 
from the glibc version used for linking
/usr/bin/ld: /ssd/tracecmd-work/trace-cmd/tracecmd/trace-record.o: in 
function `record_trace.isra.0':
trace-record.c:(.text+0xe705): warning: Using 'initgroups' in statically 
linked applications requires at runtime the shared libraries from the 
glibc version used for linking
/usr/bin/ld: /ssd/tracecmd-work/trace-cmd/tracecmd/trace-record.o: in 
function `do_getaddrinfo':
trace-record.c:(.text+0x865): warning: Using 'getaddrinfo' in statically 
linked applications requires at runtime the shared libraries from the 
glibc version used for linking
/usr/bin/ld: /ssd/tracecmd-work/trace-cmd/tracecmd/trace-record.o: in 
function `record_trace.isra.0':
trace-record.c:(.text+0xe6ee): warning: Using 'getpwnam' in statically 
linked applications requires at runtime the shared libraries from the 
glibc version used for linking
/usr/bin/ld: attempted static link of dynamic object 
`/ssd/tracecmd-work/out/lib64/libtraceevent.a(libtraceevent.o)'

I've updated your patch. This version works fine:

diff --git a/src/Makefile b/src/Makefile
index 53bb570..3b2d10f 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -18,7 +18,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)
@@ -30,6 +30,20 @@ $(LIBTRACEEVENT_SHARED_VERSION): $(LIBTRACEEVENT_SHARED)
  $(LIBTRACEEVENT_SHARED_SO): $(LIBTRACEEVENT_SHARED_VERSION)
  	@ln -sf $(<F) $@

+$(bdir)/libtraceevent-global.o: $(OBJS)
+	$(Q)ld -r -o $@ $^
+
+# Make static library match dynamic library wrt hidden functions
+find_local = \
+	$(shell for f in a `nm  $(bdir)/libtraceevent-global.o | grep ' t ' | 
cut -d' ' -f3`; do \
+	 if [ "$${f#tep_}" = "$$f" ] && [ "$${f#trace_seq_}" = "$$f" ] && [ 
"$${f#kbuffer_}" = "$$f" ]; then \
+		echo --localize-symbol $$f; \
+	 fi; \
+	 done)
+
+$(bdir)/libtraceevent.o: $(bdir)/libtraceevent-global.o
+	$(Q)objcopy $(call find_local) $< $@
+
  libtraceevent.so: $(LIBTRACEEVENT_SHARED_SO)

  libtraceevent: $(libtraceevent-y)
@@ -44,7 +58,7 @@ $(OBJS): | $(bdir)
  $(DEPS): | $(bdir)

  clean:
-	$(Q)$(call do_clean,$(OBJS) $(DEPS))
+	$(Q)$(call do_clean,$(OBJS) $(DEPS) $(bdir)/libtraceevent-global.o 
$(bdir)/libtraceevent.o)

  dep_includes := $(wildcard $(DEPS))

Note that I also have this change in my stash in addition to reverting 
name change patch:

diff --git a/src/event-parse.c b/src/event-parse.c
index 9232f11..8daf91d 100644
--- a/src/event-parse.c
+++ b/src/event-parse.c
@@ -3184,7 +3184,7 @@ process_int_dynamic_array(struct tep_event *event, 
struct tep_print_arg *arg, ch
  	 * The first item within the parenthesis is another field that holds
  	 * the index into where the array starts.
  	 */
-	type = tep_read_token(event->tep, &token);
+	type = read_token(event->tep, &token);
  	if (type != TEP_EVENT_ITEM)
  		return TEP_EVENT_ERROR;

@@ -3213,7 +3213,7 @@ process_int_dynamic_array(struct tep_event *event, 
struct tep_print_arg *arg, ch

  	return read_token_item(event->tep, tok);
  out:
-	tep_free_token(token);
+	free_token(token);
  	*tok = NULL;
  	return TEP_EVENT_ERROR;
  }
@@ -3720,7 +3720,7 @@ process_function(struct tep_event *event, struct 
tep_print_arg *arg,
  		return process_int_array(event, arg, tok);
  	}
  	if (strcmp(token, "__print_dynamic_array") == 0) {
-		tep_free_token(token);
+		free_token(token);
  		return process_int_dynamic_array(event, arg, tok);
  	}
  	if (strcmp(token, "__get_str") == 0 ||

Thanks,