xref: /freebsd/crypto/openssl/test/moduleloadtest.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  */
9*e0c4386eSCy Schubert 
10*e0c4386eSCy Schubert /*
11*e0c4386eSCy Schubert  * Extremely simple dynamic loader, must never be linked with anything other
12*e0c4386eSCy Schubert  * than the standard C library.  Its purpose is to try to load a dynamic module
13*e0c4386eSCy Schubert  * and verify the presence of one symbol, if that's given.
14*e0c4386eSCy Schubert  */
15*e0c4386eSCy Schubert 
16*e0c4386eSCy Schubert #include <stdio.h>
17*e0c4386eSCy Schubert #include <stdlib.h>
18*e0c4386eSCy Schubert #include <openssl/core.h>
19*e0c4386eSCy Schubert #include "simpledynamic.h"
20*e0c4386eSCy Schubert 
test_load(const char * path,const char * symbol)21*e0c4386eSCy Schubert static int test_load(const char *path, const char *symbol)
22*e0c4386eSCy Schubert {
23*e0c4386eSCy Schubert #ifdef SD_INIT
24*e0c4386eSCy Schubert     SD sd = SD_INIT;
25*e0c4386eSCy Schubert     SD_SYM sym;
26*e0c4386eSCy Schubert     int ret;
27*e0c4386eSCy Schubert 
28*e0c4386eSCy Schubert     if (!sd_load(path, &sd, SD_MODULE))
29*e0c4386eSCy Schubert         return 0;
30*e0c4386eSCy Schubert     ret = symbol == NULL || sd_sym(sd, symbol, &sym);
31*e0c4386eSCy Schubert     if (!sd_close(sd))
32*e0c4386eSCy Schubert         ret = 0;
33*e0c4386eSCy Schubert     return ret;
34*e0c4386eSCy Schubert #else
35*e0c4386eSCy Schubert     fprintf(stderr, "No dynamic loader\n");
36*e0c4386eSCy Schubert     return 0;
37*e0c4386eSCy Schubert #endif
38*e0c4386eSCy Schubert }
39*e0c4386eSCy Schubert 
main(int argc,char * argv[])40*e0c4386eSCy Schubert int main(int argc, char *argv[])
41*e0c4386eSCy Schubert {
42*e0c4386eSCy Schubert     const char *m, *s;
43*e0c4386eSCy Schubert 
44*e0c4386eSCy Schubert     if (argc != 2 && argc != 3) {
45*e0c4386eSCy Schubert         fprintf(stderr, "Usage: %s sharedobject [ entrypoint ]\n", argv[0]);
46*e0c4386eSCy Schubert         return 1;
47*e0c4386eSCy Schubert     }
48*e0c4386eSCy Schubert 
49*e0c4386eSCy Schubert     m = argv[1];
50*e0c4386eSCy Schubert     s = argc == 3 ? argv[2] : NULL;
51*e0c4386eSCy Schubert 
52*e0c4386eSCy Schubert     return test_load(m, s) ? 0 : 1;
53*e0c4386eSCy Schubert }
54