From 6141ad6e6f2d3eb38e7e0962f61b78510b2e2d2c Mon Sep 17 00:00:00 2001 From: Jeffy Chen Date: Wed, 27 Jan 2021 16:51:03 +0800 Subject: [PATCH] gbm_wrapper: Load mali library before searching the symbols The RTLD_NEXT would not work if the gbm library was loaded with RTLD_LOCAL. Change-Id: I4256b52271a92e03dbcb490b1e7fabeb32be6401 Signed-off-by: Jeffy Chen --- gbm_wrapper.c | 46 ++++++++++++++++++++++++++++++++++++++-------- meson.build | 10 +++++++--- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/gbm_wrapper.c b/gbm_wrapper.c index 999f62b..abcb011 100644 --- a/gbm_wrapper.c +++ b/gbm_wrapper.c @@ -19,6 +19,8 @@ #include #include #include +#include +#include #include #include #include @@ -187,6 +189,30 @@ gbm_bo_unmap(struct gbm_bo *bo, void *map_data) } #endif +static inline void * +mali_dlsym(const char *func) +{ + void *handle, *symbol; + + /* The libmali should be already loaded */ + handle = dlopen(LIBMALI_SO, RTLD_LAZY | RTLD_NOLOAD); + if (!handle) { + /* Should not reach here */ + fprintf(stderr, "FATAL: " LIBMALI_SO " not loaded\n"); + exit(-1); + } + + /* Clear error */ + dlerror(); + + symbol = dlsym(handle, func); + if (!symbol) + fprintf(stderr, "%s\n", dlerror()); + + dlclose(handle); + return symbol; +} + /* Wrappers for unsupported flags */ struct gbm_surface * gbm_surface_create(struct gbm_device *gbm, @@ -195,10 +221,12 @@ gbm_surface_create(struct gbm_device *gbm, { struct gbm_surface *surface; - static struct gbm_surface * (*surface_create)(); - if(!surface_create) - surface_create = - (struct gbm_surface *(*)()) dlsym(RTLD_NEXT, "gbm_surface_create"); + static struct gbm_surface * (* surface_create) (); + if (!surface_create) { + surface_create = mali_dlsym(__func__); + if(!surface_create) + return NULL; + } surface = surface_create(gbm, width, height, format, flags); if (surface) @@ -216,10 +244,12 @@ gbm_bo_create(struct gbm_device *gbm, { struct gbm_bo *bo; - static struct gbm_bo * (*bo_create)(); - if(!bo_create) - bo_create = - (struct gbm_bo *(*)()) dlsym(RTLD_NEXT, "gbm_bo_create"); + static struct gbm_bo * (* bo_create) (); + if (!bo_create) { + bo_create = mali_dlsym(__func__); + if(!bo_create) + return NULL; + } bo = bo_create(gbm, width, height, format, flags); if (bo) diff --git a/meson.build b/meson.build index 245c7a4..5b55ecb 100644 --- a/meson.build +++ b/meson.build @@ -4,6 +4,8 @@ project( meson_version : '>=0.49.0', default_options : ['b_asneeded=false', 'b_lundef=false']) +mali_version = meson.project_version() + pkgconfig = import('pkgconfig') if get_option('arch') != 'auto' @@ -68,7 +70,7 @@ cl_headers = { # Package name : required symbol, wrappers, headers, package version map = { - 'mali' : ['', mali_wrappers, mali_headers, meson.project_version()], + 'mali' : ['', mali_wrappers, mali_headers, mali_version], 'gbm' : ['gbm_create_device', gbm_wrappers, gbm_headers, '20.1.5'], 'egl' : ['eglCreateContext', egl_wrappers, egl_headers, '7.10'], 'glesv1_cm' : ['eglCreateContext', glesv1_wrappers, glesv1_headers, '7.10'], @@ -86,7 +88,7 @@ libmali = shared_library( 'mali', dummy_source, install : true, - version : meson.project_version()) + version : mali_version) # The gbm functions might be missing gbm_check_funcs = [ @@ -109,7 +111,9 @@ libgbm = [] gbm_symbol = map['gbm'][0] if run_command('grep', '-q', gbm_symbol, default_lib).returncode() == 0 libgbm_version = gbm_wrappers['gbm'] - libgbm_cflags = [] + libgbm_cflags = [ + '-DLIBMALI_SO="libmali.so.' + mali_version.split('.')[0] + '"', + ] libdrm_dep = dependency('libdrm', version : '>= 2.4.0') if not libdrm_dep.found()