11556Srgrimes#!/bin/sh
21556Srgrimes
31556Srgrimes# program
41556Srgrimes#
51556Srgrimes# dlopen():
61556Srgrimes# liba.so
71556Srgrimes# <- libb.so
81556Srgrimes#    <- libb_dependency.so
91556Srgrimes# libd.so
101556Srgrimes# libe.so
111556Srgrimes#
121556Srgrimes# Expected: Undefined symbol in libe.so resolves to symbol in
131556Srgrimes# libb_dependency.so, not to symbol in libd.so.
141556Srgrimes
151556Srgrimes
161556Srgrimes. ./test_setup
171556Srgrimes
181556Srgrimes
191556Srgrimes# create libb_dependency.so
201556Srgrimescat > libb_dependency.c << EOI
211556Srgrimesint c() { return 1; }
221556SrgrimesEOI
231556Srgrimes
241556Srgrimes# build
251556Srgrimescompile_lib -o libb_dependency.so libb_dependency.c
261556Srgrimes
271556Srgrimes
281556Srgrimes# create libb.so
291556Srgrimescat > libb.c << EOI
301556Srgrimesint b() { return 1; }
3135773ScharnierEOI
3236003Scharnier
3335773Scharnier# build
341556Srgrimescompile_lib -o libb.so libb.c ./libb_dependency.so
3599109Sobrien
3699109Sobrien
371556Srgrimes# create libd.so
38149790Scsjpcat > libd.c << EOI
39149790Scsjpint c() { return 2; }
401556SrgrimesEOI
411556Srgrimes
4235773Scharnier# build
431556Srgrimescompile_lib -o libd.so libd.c
4435773Scharnier
451556Srgrimes
461556Srgrimes# create liba.so
471556Srgrimescat > liba.c << EOI
481556Srgrimesextern int c();
491556Srgrimesint e() { return 0; }
5076693SimpEOI
511556Srgrimes
5278469Sdes# build
5356420Smharocompile_lib -o liba.so liba.c ./libb.so
541556Srgrimes
551556Srgrimes
561556Srgrimes# create libe.so
571556Srgrimescat > libe.c << EOI
58163074Smaximextern int c();
59163074Smaximint a() { return c(); }
60184471SivorasEOI
61184471Sivoras
62184471Sivoras# build
63184471Sivorascompile_lib -o libe.so libe.c
64184471Sivoras
65184471Sivoras
66184471Sivoras# create program
67184471Sivorascat > program.c << EOI
68184471Sivoras#include <dlfcn.h>
69184471Sivoras#include <stdio.h>
70184471Sivoras#include <stdlib.h>
711556Srgrimesint
72105395Smarkmmain()
731556Srgrimes{
74184471Sivoras	void* liba;
75184471Sivoras	void* libd;
7678070Sbde	void* libe;
7799363Smarkm	int (*a)();
7899363Smarkm
79163049Smaxim	liba = dlopen("./liba.so", RTLD_NOW | RTLD_GLOBAL);
80163075Smaxim	if (liba == NULL) {
8132540Sbde		fprintf(stderr, "Error opening liba.so: %s\n", dlerror());
821556Srgrimes		exit(117);
831556Srgrimes	}
841556Srgrimes
858855Srgrimes	libd = dlopen("./libd.so", RTLD_NOW | RTLD_GLOBAL);
861556Srgrimes	if (libd == NULL) {
871556Srgrimes		fprintf(stderr, "Error opening libd.so: %s\n", dlerror());
881556Srgrimes		exit(117);
891556Srgrimes	}
901556Srgrimes
911556Srgrimes	libe = dlopen("./libe.so", RTLD_NOW | RTLD_GLOBAL);
921556Srgrimes	if (libe == NULL) {
931556Srgrimes		fprintf(stderr, "Error opening libe.so: %s\n", dlerror());
941556Srgrimes		exit(117);
951556Srgrimes	}
961556Srgrimes
971556Srgrimes	a = (int (*)())dlsym(libe, "a");
981556Srgrimes	if (a == NULL) {
991556Srgrimes		fprintf(stderr, "Error getting symbol a: %s\n", dlerror());
1001556Srgrimes		exit(116);
1011556Srgrimes	}
10230088Swosch
103100538Sjohan	return a();
104100538Sjohan}
105100538SjohanEOI
106130102Stjr
107100538Sjohan# build
108100538Sjohancompile_program_dl -o program program.c
10930088Swosch
11030088Swosch# run
1111556Srgrimestest_run_ok ./program 1
1121556Srgrimes