Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright headers.
[simgrid.git] / tools / cmake / test_prog / prog_gnu_dynlinker.c
1 /* prog_gnu_dynlinker.c -- check that RTLD_NEXT is defined as in GNU linker */
2 /* Copyright (c) 2012-2018. The SimGrid Team.
3  * All rights reserved.                                                     */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #define _GNU_SOURCE 1
9 #include <dlfcn.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13
14 static void * (*real_malloc) (size_t);
15
16 int main(void) {
17    char *error;
18    dlerror(); // clear any previous error
19    real_malloc = (void * (*) (size_t)) dlsym(RTLD_NEXT, "malloc");
20    error = dlerror();
21    if (!error && real_malloc) {
22       char *A = real_malloc(20);
23       strcpy(A,"epic success");
24       free(A);
25       return 0; // SUCCESS
26    } else {
27       if (error)
28    printf("Error while checking for dlsym: %s\n",error);
29       else
30    printf("dlsym did not return any error, but failed to find malloc()\n");
31       return 1; // FAILED
32    }
33 }