[libcamera-devel] [PATCH 1/5] libcamera: utils: Provide helper to get dynamic library runpath

Kieran Bingham kieran.bingham at ideasonboard.com
Wed Feb 5 14:04:16 CET 2020


Provide a helper that will identify the DT_RUNPATH string from the
ELF dynamic library string table entries.

Signed-off-by: Kieran Bingham <kieran.bingham at ideasonboard.com>
---
 src/libcamera/include/utils.h |  1 +
 src/libcamera/utils.cpp       | 26 ++++++++++++++++++++++++++
 2 files changed, 27 insertions(+)

diff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h
index e467eb21c518..069b327bb436 100644
--- a/src/libcamera/include/utils.h
+++ b/src/libcamera/include/utils.h
@@ -33,6 +33,7 @@ namespace utils {
 const char *basename(const char *path);
 
 char *secure_getenv(const char *name);
+const char *get_runpath();
 
 template<class InputIt1, class InputIt2>
 unsigned int set_overlap(InputIt1 first1, InputIt1 last1,
diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp
index 4beffdab5eb6..e48cf2b6a48e 100644
--- a/src/libcamera/utils.cpp
+++ b/src/libcamera/utils.cpp
@@ -7,7 +7,9 @@
 
 #include "utils.h"
 
+#include <elf.h>
 #include <iomanip>
+#include <link.h>
 #include <sstream>
 #include <stdlib.h>
 #include <string.h>
@@ -70,6 +72,30 @@ char *secure_getenv(const char *name)
 #endif
 }
 
+/**
+ * \brief Obtain the runpath string from the dynamic symbol table
+ * \returns a const char pointer to the runpath entry in the elf string table
+ * or NULL if it is not found.
+ */
+const char *get_runpath()
+{
+	const ElfW(Dyn) *dyn = _DYNAMIC;
+	const ElfW(Dyn) *runpath = NULL;
+	const char *strtab = NULL;
+
+	for (; dyn->d_tag != DT_NULL; ++dyn) {
+		if (dyn->d_tag == DT_STRTAB)
+			strtab = reinterpret_cast<const char *>(dyn->d_un.d_val);
+		else if (dyn->d_tag == DT_RUNPATH)
+			runpath = dyn;
+	}
+
+	if (strtab && runpath)
+		return strtab + runpath->d_un.d_val;
+
+	return NULL;
+}
+
 /**
  * \fn libcamera::utils::set_overlap(InputIt1 first1, InputIt1 last1,
  *				     InputIt2 first2, InputIt2 last2)
-- 
2.20.1



More information about the libcamera-devel mailing list