# HG changeset patch # User Paul Boddie # Date 1527543865 -7200 # Node ID 4087e1c2b12a29c0e738ce0032c1a770f299a1de # Parent 86ad5faff7f2195c71e0c9507be1ced7f79b17fd Added utilities for dynamic library loading. diff -r 86ad5faff7f2 -r 4087e1c2b12a pkg/devices/util/include/dl.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/devices/util/include/dl.h Mon May 28 23:44:25 2018 +0200 @@ -0,0 +1,30 @@ +/* + * Dynamic loading support. + * + * Copyright (C) 2018 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#pragma once + +#include + +EXTERN_C_BEGIN + +void *load_function(const char *filename, const char *function); + +EXTERN_C_END diff -r 86ad5faff7f2 -r 4087e1c2b12a pkg/devices/util/src/Makefile --- a/pkg/devices/util/src/Makefile Sun May 27 22:08:58 2018 +0200 +++ b/pkg/devices/util/src/Makefile Mon May 28 23:44:25 2018 +0200 @@ -4,10 +4,10 @@ TARGET = libdevice_util.o.a PC_FILENAME := libdevice-util -SRC_CC := dataspace.cc event-loop.cc memory.cc +SRC_CC := dataspace.cc dl.cc event-loop.cc memory.cc PRIVATE_INCDIR += $(PKGDIR)/util/include -REQUIRES_LIBS := libio l4re_c l4re_c-util +REQUIRES_LIBS := libio l4re_c l4re_c-util libdl include $(L4DIR)/mk/lib.mk diff -r 86ad5faff7f2 -r 4087e1c2b12a pkg/devices/util/src/dl.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/devices/util/src/dl.cc Mon May 28 23:44:25 2018 +0200 @@ -0,0 +1,50 @@ +/* + * Dynamic loading support. + * + * (c) 2018 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#include +#include +#include + +void *load_function(const char *filename, const char *function) +{ + char libname[64]; + FILE *f = fopen(filename, "r"); + size_t nread; + void *handle; + + /* Obtain the library name from the file. */ + + if (f == NULL) return 0; + + nread = fread(libname, sizeof(char), 63, f); + fclose(f); + + if (ferror(f)) return 0; + + libname[nread] = '\0'; + + /* Obtain the linked data. */ + + handle = dlopen(libname, RTLD_NOW); + if (handle == NULL) return 0; + + return dlsym(handle, function); +}