cat > bad.c << 'EOF'
extern void non_existent(void);
int test() {
non_existent();
return 42;
}
EOF
gcc -shared -fPIC -o libbad.so bad.c
cat > test.c << 'EOF'
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
void *h;
int (*f)(void);
char *error;
printf("=== RTLD_LAZY ===\n");
h = dlopen("./libbad.so", RTLD_LAZY);
if (!h) {
printf("dlopen ошибка: %s\n", dlerror());
return 1;
}
printf("dlopen: OK\n");
dlerror();
f = dlsym(h, "test");
error = dlerror();
if (error) {
printf("dlsym ошибка: %s\n", error);
} else {
printf("dlsym: OK, пробуем вызвать...\n");
f();
}
dlclose(h);
return 0;
}
EOF
gcc test.c -ldl -o test
./test
sed -i 's/RTLD_LAZY/RTLD_NOW/' test.c
gcc test.c -ldl -o test
./test
задание2
cat > plugin1.c << 'EOF'
#include <stdio.h>
void info(void) {
printf(" Плагин 1: Математика v1.0, Иванов\n");
}
EOF
cat > plugin2.c << 'EOF'
#include <stdio.h>
void info(void) {
printf(" Плагин 2: Строки v2.0, Петров\n");
}
EOF
cat > plugin3.c << 'EOF'
void nothing(void) {}
EOF
gcc -shared -fPIC -o plugin1.so plugin1.c
gcc -shared -fPIC -o plugin2.so plugin2.c
gcc -shared -fPIC -o plugin3.so plugin3.c
cat > loader.c << 'EOF'
#include <dlfcn.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
int main() {
DIR *d = opendir(".");
struct dirent *e;
while ((e = readdir(d))) {
if (strstr(e->d_name, ".so")) {
printf("\nФайл: %s\n", e->d_name);
void *h = dlopen(e->d_name, RTLD_LAZY);
if (!h) {
printf(" Ошибка загрузки: %s\n", dlerror());
continue;
}
dlerror();
void (*info)(void) = dlsym(h, "info");
char *err = dlerror();
if (err) {
printf(" info() не найдена\n");
} else {
printf(" info(): ");
info();
}
dlclose(h);
}
}
closedir(d);
return 0;
}
EOF
gcc loader.c -ldl -o loader
./loader