[Concept,13/15] ulib: Provide a bit more info in the library-test program

Message ID 20250905170132.182249-14-sjg@u-boot.org
State New
Headers
Series ulib: Provide test programs and documentation |

Commit Message

Simon Glass Sept. 5, 2025, 5:01 p.m. UTC
  From: Simon Glass <sjg@chromium.org>

When the test is run, indicate which library the test was linked
against, static or dynamic.

Drop the printf() at the start of main(). Since all printf() calls go to
U-Boot, this message is not shown anyway, since the global-data pointer
gd is NULL at this point and console_puts() just returns immediately.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 test/ulib/ulib_test.c | 39 ++++++++++++++++++++++++++++++++++++---
 1 file changed, 36 insertions(+), 3 deletions(-)
  

Patch

diff --git a/test/ulib/ulib_test.c b/test/ulib/ulib_test.c
index a68290ff484..51b1c62e6b2 100644
--- a/test/ulib/ulib_test.c
+++ b/test/ulib/ulib_test.c
@@ -2,7 +2,7 @@ 
 /*
  * Test application for U-Boot shared library
  *
- * This demonstrates linking against libu-boot.so
+ * This demonstrates linking against libu-boot.so and libu-boot.a
  *
  * Copyright 2025 Canonical
  * Written by Simon Glass <simon.glass@canonical.com>
@@ -11,17 +11,50 @@ 
 /* Use system headers, not U-Boot headers */
 #include <stdio.h>
 #include <string.h>
+
+#include <os.h>
 #include <u-boot-lib.h>
 
+/* Runtime detection of link type using /proc/self/maps */
+static const char *detect_link_type(void)
+{
+	char line[512];
+	int fd;
+	int found_libuboot = 0;
+
+	/* Open /proc/self/maps to check loaded libraries */
+	fd = os_open("/proc/self/maps", 0);
+	if (fd < 0)
+		return "unable to detect linkage";
+
+	/* Read line by line to avoid boundary issues */
+	while (os_fgets(line, sizeof(line), fd)) {
+		if (strstr(line, "libu-boot.so")) {
+			found_libuboot = 1;
+			break;
+		}
+	}
+
+	os_close(fd);
+
+	/* Return appropriate message based on what we found */
+	if (found_libuboot)
+		return "dynamically linked (uses libu-boot.so)";
+	else
+		return "statically linked (uses libu-boot.a)";
+}
+
 int main(int argc, char *argv[])
 {
 	int ret;
 
-	printf("Calling U-Boot initialization via shared library...\n");
-
 	ret = ulib_init(argv[0]);
 	if (ret)
 		return 1;
 
+	printf("Hello, world\n");
+	printf("\n- U-Boot\n");
+	printf("\nPS: This program is %s\n", detect_link_type());
+
 	return ret;
 }