Skip to contentSkip to conversation

libtraceevent: Fix static library to hide hidden functions

PATCH In review Steven Rostedt 2 participants 5 messages Nov 12, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover libtraceevent: Fix static library to hide hidden functions

From: "Steven Rostedt (Google)" <[email protected]>

When the static library uses the objects directly to be created, it makes all the hidden functions visible to applications that link to it.

To fix this, create a helper function in the Makefile (redefine_local) that will search for all the global functions within the shared library, and if they do not start with "tep_", they are renamed to start with "_tep_local_". This will keep these function from colliding with the namespace of applications that may link to the static library.

Changes since v1: https://lore.kernel.org/all/[email protected]/

- Instead of playing tricks to what to add to the static library, just change the static library directly.

- Modified the meson build to have this as well.

Steven Rostedt (Google) (2):
libtraceevent: Have static library to hide hidden functions
libtraceevent: Have meson build rename hidden global functions

 Makefile.meson          |  15 +-
 scripts/utils.mk        |   5 +
 src/Makefile            |   8 +-
 src/event-parse-api.c   |   6 +-
 src/event-parse-local.h |  24 ++--
 src/event-parse.c       | 298 ++++++++++++++++++++--------------------
 src/event-plugin.c      |   2 +-
 src/parse-filter.c      |  22 +--
 8 files changed, 200 insertions(+), 180 deletions(-)
Signature · 1 line
2.51.0
#1 lore raw

PATCH v2 1/2 libtraceevent: Have static library to hide hidden functions

From: "Steven Rostedt (Google)" <[email protected]>

When the static library uses the objects directly to be created, it makes all the hidden functions visible to applications that link to it.

Check all the functions that are global and if they do not start with "tep_" rename the symbol to start with "_tep_local_". This keeps the source code clean and still adds the namespace for the hidden functions within the objects.

---
 src/Makefile            |  13 +-
 src/event-parse-api.c   |   6 +-
 src/event-parse-local.h |  24 ++--
 src/event-parse.c       | 298 ++++++++++++++++++++--------------------
 src/event-plugin.c      |   2 +-
 src/parse-filter.c      |  22 +--
 6 files changed, 187 insertions(+), 178 deletions(-)
src/Makefile +11 -2
index 53bb570182d2..c9e8498c2d05 100644
@@ -18,7 +18,15 @@ DEPS := $(OBJS:$(bdir)/%.o=$(bdir)/.%.d)
 $(bdir)/%.o: %.c
 	$(Q)$(call do_fpic_compile)
 
-$(LIBTRACEEVENT_STATIC): $(OBJS)
+# Make all global functions that do not start with "tep_" have a unique name.
+redefine_local = \
+	$(shell for f in a `nm ${LIBTRACEEVENT_SHARED} | grep ' t ' | cut -d' ' -f3 | sort -u`;  do \
+	 if [ "$${f#tep_}" = "$$f" ]; then echo --redefine-sym $$f=_tep_local_$$f; fi; done)
+
+$(LIBTRACEEVENT_STATIC): $(bdir)/.tmp.libtraceevent.a $(LIBTRACEEVENT_SHARED)
+	$(Q)objcopy $(call redefine_local) $< $@
+
+$(bdir)/.tmp.libtraceevent.a: $(OBJS)
 	$(Q)$(call do_build_static_lib)
 
 $(LIBTRACEEVENT_SHARED): $(OBJS)
@@ -44,7 +52,8 @@ $(OBJS): | $(bdir)
 $(DEPS): | $(bdir)
 
 clean:
-	$(Q)$(call do_clean,$(OBJS) $(DEPS))
+	$(Q)$(call do_clean,$(OBJS) $(DEPS) $(bdir)/libtraceevent.o \
+	                    $(bdir)/.tmp.libtraceevent.a)
 
 dep_includes := $(wildcard $(DEPS))
 
src/event-parse-api.c +3 -3
index 8a0e976ade45..fd03daf310ea 100644
@@ -106,7 +106,7 @@ bool tep_test_flag(struct tep_handle *tep, enum tep_flag flag)
 	return false;
 }
 
-__hidden unsigned short tep_data2host2(struct tep_handle *tep, unsigned short data)
+__hidden unsigned short data2host2(struct tep_handle *tep, unsigned short data)
 {
 	unsigned short swap;
 
@@ -119,7 +119,7 @@ __hidden unsigned short tep_data2host2(struct tep_handle *tep, unsigned short da
 	return swap;
 }
 
-__hidden unsigned int tep_data2host4(struct tep_handle *tep, unsigned int data)
+__hidden unsigned int data2host4(struct tep_handle *tep, unsigned int data)
 {
 	unsigned int swap;
 
@@ -135,7 +135,7 @@ __hidden unsigned int tep_data2host4(struct tep_handle *tep, unsigned int data)
 }
 
 __hidden  unsigned long long
-tep_data2host8(struct tep_handle *tep, unsigned long long data)
+data2host8(struct tep_handle *tep, unsigned long long data)
 {
 	unsigned long long swap;
 
src/event-parse-local.h +12 -12
index d9e9faf649d1..c6bfc611bfbd 100644
@@ -108,20 +108,20 @@ struct tep_print_parse {
 	struct tep_print_arg		*len_as_arg;
 };
 
-void tep_free_event(struct tep_event *event);
-void tep_free_format_field(struct tep_format_field *field);
-void tep_free_plugin_paths(struct tep_handle *tep);
+void free_tep_event(struct tep_event *event);
+void free_tep_format_field(struct tep_format_field *field);
+void free_tep_plugin_paths(struct tep_handle *tep);
 
-unsigned short tep_data2host2(struct tep_handle *tep, unsigned short data);
-unsigned int tep_data2host4(struct tep_handle *tep, unsigned int data);
-unsigned long long tep_data2host8(struct tep_handle *tep, unsigned long long data);
+unsigned short data2host2(struct tep_handle *tep, unsigned short data);
+unsigned int data2host4(struct tep_handle *tep, unsigned int data);
+unsigned long long data2host8(struct tep_handle *tep, unsigned long long data);
 
 /* access to the internal parser */
-int tep_peek_char(struct tep_handle *tep);
-void tep_init_input_buf(struct tep_handle *tep, const char *buf, unsigned long long size);
-unsigned long long tep_get_input_buf_ptr(struct tep_handle *tep);
-const char *tep_get_input_buf(struct tep_handle *tep);
-enum tep_event_type tep_read_token(struct tep_handle *tep, char **tok);
-void tep_free_token(char *tok);
+int peek_char(struct tep_handle *tep);
+void init_input_buf(struct tep_handle *tep, const char *buf, unsigned long long size);
+unsigned long long get_input_buf_ptr(struct tep_handle *tep);
+const char *get_input_buf(struct tep_handle *tep);
+enum tep_event_type read_token(struct tep_handle *tep, char **tok);
+void free_token(char *tok);
 
 #endif /* _PARSE_EVENTS_INT_H */
src/event-parse.c +149 -149

1,064 lines not on this page · raw message

src/event-plugin.c +1 -1
index 236b96bceab4..3b14fd2c02e6 100644
@@ -662,7 +662,7 @@ int tep_add_plugin_path(struct tep_handle *tep, char *path,
 	return 0;
 }
 
-__hidden void tep_free_plugin_paths(struct tep_handle *tep)
+__hidden void free_tep_plugin_paths(struct tep_handle *tep)
 {
 	struct tep_plugins_dir *dir;
 
src/parse-filter.c +11 -11
index 8045b10e7bcb..75b84a0cc4ae 100644
@@ -38,8 +38,8 @@ static void show_error(struct tep_handle *tep, char *error_buf, const char *fmt,
 	int len;
 	int i;
 
-	input = tep_get_input_buf(tep);
-	index = tep_get_input_buf_ptr(tep);
+	input = get_input_buf(tep);
+	index = get_input_buf_ptr(tep);
 	len = input ? strlen(input) : 0;
 
 	if (len) {
@@ -63,25 +63,25 @@ static enum tep_event_type filter_read_token(struct tep_handle *tep, char **tok)
 	char *token = NULL;
 
 	do {
-		tep_free_token(token);
-		type = tep_read_token(tep, &token);
+		free_token(token);
+		type = read_token(tep, &token);
 	} while (type == TEP_EVENT_NEWLINE || type == TEP_EVENT_SPACE);
 
 	/* If token is = or ! check to see if the next char is ~ */
 	if (token &&
 	    (strcmp(token, "=") == 0 || strcmp(token, "!") == 0) &&
-	    tep_peek_char(tep) == '~') {
+	    peek_char(tep) == '~') {
 		/* append it */
 		*tok = malloc(3);
 		if (*tok == NULL) {
-			tep_free_token(token);
+			free_token(token);
 			return TEP_EVENT_ERROR;
 		}
 		sprintf(*tok, "%c%c", *token, '~');
-		tep_free_token(token);
+		free_token(token);
 		/* Now remove the '~' from the buffer */
-		tep_read_token(tep, &token);
-		tep_free_token(token);
+		read_token(tep, &token);
+		free_token(token);
 	} else
 		*tok = token;
 
@@ -1182,7 +1182,7 @@ process_event(struct tep_event *event, const char *filter_str,
 {
 	int ret;
 
-	tep_init_input_buf(event->tep, filter_str, strlen(filter_str));
+	init_input_buf(event->tep, filter_str, strlen(filter_str));
 
 	ret = process_filter(event, parg, error_str, 0);
 	if (ret < 0)
@@ -1240,7 +1240,7 @@ filter_event(struct tep_event_filter *filter, struct tep_event *event,
 static void filter_init_error_buf(struct tep_event_filter *filter)
 {
 	/* clear buffer to reset show error */
-	tep_init_input_buf(filter->tep, "", 0);
+	init_input_buf(filter->tep, "", 0);
 	filter->error_buffer[0] = '\0';
 }
 
Signature · 1 line
2.51.0
#2 lore raw

PATCH v2 2/2 libtraceevent: Have meson build rename hidden global functions

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

The meson build needs to perform the same operation as the normal make build with respect to global hidden functions within the static library.

Move the redefine_local function to utils.mk and have it take a parameter for what shared library it will read to find the global functions with. Then separate out a target in Makefile.meson for the static library to use this helper function to rename the non "tep_*" global functions to have a "_tep_local_*" prefix.

---
 Makefile.meson   | 15 +++++++++++++--
 scripts/utils.mk |  5 +++++
 src/Makefile     |  7 +------
 3 files changed, 19 insertions(+), 8 deletions(-)
Makefile.meson +13 -2
index 71d6bf36c654..28c307ff6d9f 100644
@@ -16,17 +16,28 @@ endef
 $(call allow-override,MESON,meson)
 $(call allow-override,MESON_BUILD_DIR,build)
 
+include $(src)/scripts/utils.mk
 
-all: compile
+LIBTRACEEVENT_SHARED_MESON = $(srctree)/build/src/libtraceevent.so
+LIBTRACEEVENT_STATIC_MESON = $(srctree)/build/src/libtraceevent.a
+TEMP_MESON = $(srctree)/build/src/.tmp.libtraceevent.a
+
+all: $(LIBTRACEEVENT_STATIC_MESON) $(LIBTRACEEVENT_SHARED_MESON)
 
 PHONY += compile
 compile: $(MESON_BUILD_DIR) force
 	$(MESON) compile -C $(MESON_BUILD_DIR)
 
+$(LIBTRACEEVENT_SHARED_MESON): compile
+
+$(LIBTRACEEVENT_STATIC_MESON): $(LIBTRACEEVENT_SHARED_MESON)
+	$(Q)objcopy $(call redefine_local,$(LIBTRACEEVENT_SHARED_MESON)) $(LIBTRACEEVENT_STATIC_MESON) $(TEMP_MESON)
+	mv $(TEMP_MESON) $(LIBTRACEEVENT_STATIC_MESON)
+
 $(MESON_BUILD_DIR):
 	$(MESON) setup --prefix=$(prefix) $(MESON_BUILD_DIR)
 
-install: compile
+install: $(LIBTRACEEVENT_STATIC_MESON)
 	$(MESON) install -C $(MESON_BUILD_DIR)
 
 docs: $(MESON_BUILD_DIR)
scripts/utils.mk +5 -0
index 7139088fbec2..7cd7355b811f 100644
@@ -180,3 +180,8 @@ define do_install_pkgconfig_file
 		(echo Failed to locate pkg-config directory) 1>&2;		\
 	fi
 endef
+
+# Make all global functions that do not start with "tep_" have a unique name.
+redefine_local = \
+	$(shell for f in a `nm $(1) 2>/dev/null | grep ' t ' | cut -d' ' -f3 | sort -u`;  do \
+	 if [ "$${f#tep_}" = "$$f" ]; then echo --redefine-sym $$f=_tep_local_$$f; fi; done)
src/Makefile +1 -6
index c9e8498c2d05..169bc6af1cbc 100644
@@ -18,13 +18,8 @@ DEPS := $(OBJS:$(bdir)/%.o=$(bdir)/.%.d)
 $(bdir)/%.o: %.c
 	$(Q)$(call do_fpic_compile)
 
-# Make all global functions that do not start with "tep_" have a unique name.
-redefine_local = \
-	$(shell for f in a `nm ${LIBTRACEEVENT_SHARED} | grep ' t ' | cut -d' ' -f3 | sort -u`;  do \
-	 if [ "$${f#tep_}" = "$$f" ]; then echo --redefine-sym $$f=_tep_local_$$f; fi; done)
-
 $(LIBTRACEEVENT_STATIC): $(bdir)/.tmp.libtraceevent.a $(LIBTRACEEVENT_SHARED)
-	$(Q)objcopy $(call redefine_local) $< $@
+	$(Q)objcopy $(call redefine_local,${LIBTRACEEVENT_SHARED}) $< $@
 
 $(bdir)/.tmp.libtraceevent.a: $(OBJS)
 	$(Q)$(call do_build_static_lib)
Signature · 1 line
2.51.0
Tested-by Reviewed-by on the v2 series
#3 lore raw
In reply to Steven Rostedt
Quoted 22 lines · Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

When the static library uses the objects directly to be created, it makes all the hidden functions visible to applications that link to it.

To fix this, create a helper function in the Makefile (redefine_local) that will search for all the global functions within the shared library, and if they do not start with "tep_", they are renamed to start with "_tep_local_". This will keep these function from colliding with the namespace of applications that may link to the static library.

Changes since v1: https://lore.kernel.org/all/[email protected]/

- Instead of playing tricks to what to add to the static library, just change the static library directly.

- Modified the meson build to have this as well.

Steven Rostedt (Google) (2):
libtraceevent: Have static library to hide hidden functions
libtraceevent: Have meson build rename hidden global functions

Quoted 10 lines
  Makefile.meson          |  15 +-
  scripts/utils.mk        |   5 +
  src/Makefile            |   8 +-
  src/event-parse-api.c   |   6 +-
  src/event-parse-local.h |  24 ++--
  src/event-parse.c       | 298 ++++++++++++++++++++--------------------
  src/event-plugin.c      |   2 +-
  src/parse-filter.c      |  22 +--
  8 files changed, 200 insertions(+), 180 deletions(-)

Tested the changes on my Ubuntu-25.04 (6.14.0-34-generic x86_64) with HEAD of libtraceevent, libtracefs and trace-cmd.

Could build trace-cmd statically. Did not see any regressions in unit tests. Basic smoke test (e.g., trace-cmd record/report) looks fine.

You can have my:

Thanks,

#4 lore raw
Quoted 10 lines · On Wed, 12 Nov 2025 16:19:54 +0000 Metin Kaya <[email protected]> wrote:

Tested the changes on my Ubuntu-25.04 (6.14.0-34-generic x86_64) with HEAD of libtraceevent, libtracefs and trace-cmd.

Could build trace-cmd statically. Did not see any regressions in unit tests. Basic smoke test (e.g., trace-cmd record/report) looks fine.

You can have my:

Awesome! Thanks,

-- Steve