paul@23 | 1 | /* |
paul@23 | 2 | * Dynamic loading support. |
paul@23 | 3 | * |
paul@31 | 4 | * Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk> |
paul@23 | 5 | * |
paul@23 | 6 | * This program is free software; you can redistribute it and/or |
paul@23 | 7 | * modify it under the terms of the GNU General Public License as |
paul@23 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@23 | 9 | * the License, or (at your option) any later version. |
paul@23 | 10 | * |
paul@23 | 11 | * This program is distributed in the hope that it will be useful, |
paul@23 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@23 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@23 | 14 | * GNU General Public License for more details. |
paul@23 | 15 | * |
paul@23 | 16 | * You should have received a copy of the GNU General Public License |
paul@23 | 17 | * along with this program; if not, write to the Free Software |
paul@23 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@23 | 19 | * Boston, MA 02110-1301, USA |
paul@23 | 20 | */ |
paul@23 | 21 | |
paul@23 | 22 | #include <l4/devices/dl.h> |
paul@23 | 23 | #include <dlfcn.h> |
paul@23 | 24 | #include <stdio.h> |
paul@23 | 25 | |
paul@23 | 26 | void *load_function(const char *filename, const char *function) |
paul@23 | 27 | { |
paul@23 | 28 | char libname[64]; |
paul@23 | 29 | FILE *f = fopen(filename, "r"); |
paul@23 | 30 | size_t nread; |
paul@23 | 31 | void *handle; |
paul@23 | 32 | |
paul@23 | 33 | /* Obtain the library name from the file. */ |
paul@23 | 34 | |
paul@23 | 35 | if (f == NULL) return 0; |
paul@23 | 36 | |
paul@23 | 37 | nread = fread(libname, sizeof(char), 63, f); |
paul@23 | 38 | fclose(f); |
paul@23 | 39 | |
paul@23 | 40 | if (ferror(f)) return 0; |
paul@23 | 41 | |
paul@23 | 42 | libname[nread] = '\0'; |
paul@23 | 43 | |
paul@23 | 44 | /* Obtain the linked data. */ |
paul@23 | 45 | |
paul@23 | 46 | handle = dlopen(libname, RTLD_NOW); |
paul@23 | 47 | if (handle == NULL) return 0; |
paul@23 | 48 | |
paul@23 | 49 | return dlsym(handle, function); |
paul@23 | 50 | } |