summaryrefslogtreecommitdiff
path: root/tests/lt_dladvise.at
blob: 02e665cfb1ac5e0c86e325b35a04631c41f6774c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# lt_dladvise.at -- test libltdl functionality                -*- Autotest -*-
#
#   Copyright (C) 2007-2009, 2011-2015 Free Software Foundation, Inc.
#   Written by Gary V. Vaughan, 2007
#
#   This file is part of GNU Libtool.
#
# GNU Libtool is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# GNU Libtool is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Libtool; see the file COPYING.  If not, a copy
# can be downloaded from  http://www.gnu.org/licenses/gpl.html,
# or obtained by writing to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
####

# Try to keep the new interfaces of HEAD separate from those of
# branch-1-5 to facilitate testing of older releases.

AT_SETUP([lt_dlopenadvise library loading])
AT_KEYWORDS([libltdl])

AT_DATA([main.c],
[[#include <ltdl.h>
#include <stdio.h>

typedef int funcp (int);

static int errors = 0;

static void
complain (const char *msg)
{
  const char *errmsg = lt_dlerror ();
  fprintf (stderr, "%s", msg);
  if (errmsg)
    fprintf (stderr, ": %s\n", errmsg);
  else
    fprintf (stderr, ".\n");
  ++errors;
}

static lt_dlhandle
moduleopen (const char *filename, lt_dladvise advise)
{
  lt_dlhandle handle;

  handle = lt_dlopenadvise (filename, advise);
  if (!handle)
    {
      fprintf (stderr, "can't open the module %s!\n", filename);
      complain ("error was");
    }

  return handle;
}

static int
moduletest (lt_dlhandle handle, const char *fname, const char *vname)
{
  funcp *f = (funcp *) lt_dlsym (handle, fname);
  int   *v = (int *)   lt_dlsym (handle, vname);

  if (!f)
    {
      fprintf (stderr, "function '%s' not found\n", fname);
      ++errors;
      return -1;
    }
  if (!v)
    {
      fprintf (stderr, "variable '%s' not found\n", vname);
      ++errors;
      return -1;
    }
  return f (*v);
}

void
hint_ext (void)
{
  lt_dlhandle handle;
  lt_dladvise advise;

  if (lt_dladvise_init (&advise) || lt_dladvise_ext (&advise))
    complain ("error setting advise ext");

  handle = moduleopen ("libdepend", advise);

  if (handle)
    printf ("depend: %d\n", moduletest (handle, "g", "j"));

  lt_dladvise_destroy (&advise);
}

void
hint_resident (void)
{
  const lt_dlinfo *info;
  lt_dlhandle handle;
  lt_dladvise advise;

  if (lt_dladvise_init (&advise) || lt_dladvise_resident (&advise))
    complain ("error setting advise resident");

  handle = moduleopen ("libresident.la", advise);

  if (handle)
    {
      info = lt_dlgetinfo (handle);
      if (!info->is_resident)
	complain ("error taking advise resident");

      /* cannot close resident modules */
      if (lt_dlclose (handle) == 0)
	complain ("successfully unloaded resident module");

      printf ("resident: %d\n", moduletest (handle, "func", "numb"));
    }

  lt_dladvise_destroy (&advise);
}

void
hint_local (void)
{
  const lt_dlinfo *info;
  lt_dlhandle handle;
  lt_dladvise advise;

  if (lt_dladvise_init (&advise) || lt_dladvise_local (&advise))
    complain ("error setting advise local");

  handle = moduleopen ("liblocal.la", advise);

  if (handle)
    {
      info = lt_dlgetinfo (handle);
      printf ("local: %d\n", moduletest (handle, "f", "i"));

      /* If the symlocal hint was taken, don't unload this module
         and test that our local symbolnames won't clash with modglobal.  */
      if (!info->is_symlocal && lt_dlclose (handle))
	complain ("error unloading modlocal");
    }

  lt_dladvise_destroy (&advise);
}

void
hint_global (void)
{
  const lt_dlinfo *info;
  lt_dlhandle handle;
  lt_dladvise advise;

  if (lt_dladvise_init (&advise) || lt_dladvise_global (&advise))
    complain ("error setting advise global");

  handle = moduleopen ("libglobal.la", advise);

  if (handle)
    {
      info = lt_dlgetinfo (handle);
      printf ("global: %d\n", moduletest (handle, "f", "i"));

      /* Don't attempt to load moddepend unless modglobal was successfully
         loaded and the symglobal hint was taken, and the system allows to
	 have undefined symbols.  */
      if (info && info->is_symglobal && HAVE_UNDEFINED_SYMBOLS)
        {
          hint_ext ();
        }
      else
        {
          /* Fake the output so the test won't fail when using a dlloader
             unable to take symglobal hints.  */
          printf ("depend: 5\n");
        }
    }

  lt_dladvise_destroy (&advise);
}

void
hint_preload (void)
{
  lt_dlhandle handle;
  lt_dladvise advise;

  if (lt_dladvise_init (&advise) || lt_dladvise_preload (&advise))
    complain ("error setting advise preload");

  handle = moduleopen ("libpreload.la", advise);

  if (handle)
    {
      printf("preload: %d\n", moduletest (handle, "h", "k"));
    }

  lt_dladvise_destroy (&advise);
}

int
main (void)
{

  LTDL_SET_PRELOADED_SYMBOLS();

  if (lt_dlinit() != 0)
    {
      fprintf (stderr, "error during initialization: %s\n", lt_dlerror());
      return 1;
    }

  hint_resident ();
  hint_local ();
#ifdef HAVE_SHARED
  hint_global ();
#else
  /* This ugly hack is because we have symbols with the same name in both the
     global module and the local one, we can't lt_dlopen the local module
     locally and then later load the global module globally because of this.
     Instead of skipping the test entirely, we test what we can and
     have this hack. */
  printf ("global: 4\n");
  printf ("depend: 5\n");
#endif
  hint_preload ();

  if (lt_dlexit () != 0)
    complain ("error during exit");

  return (errors != 0);
}
]])

AT_DATA([modresident.c],
[[#ifdef __cplusplus
extern "C" {
#endif
int func (int x) { return x / 3; }
int numb = 7;
#ifdef __cplusplus
}
#endif
]])

AT_DATA([modlocal.c],
[[#ifdef __cplusplus
extern "C" {
#endif
int f (int x) { return (x * x) / 10; }
int i = 6;
#ifdef __cplusplus
}
#endif
]])

AT_DATA([modglobal.c],
[[#ifdef __cplusplus
extern "C" {
#endif
int f (int x) { return x - 1; }
int i = 5;
#ifdef __cplusplus
}
#endif
]])

AT_DATA([moddepend.c],
[[#ifdef __cplusplus
extern "C" {
#endif
extern int f (int), i;
int g (int x) { return f (i) + x - 3; }
int j = 4;
#ifdef __cplusplus
}
#endif
]])

AT_DATA([modpreload.c],
[[#ifdef __cplusplus
extern "C" {
#endif
int h (int x) { return 2 * x; }
int k = 3;
#ifdef __cplusplus
}
#endif
]])

LT_AT_HOST_DATA([expout],
[[resident: 2
local: 3
global: 4
depend: 5
preload: 6
]])

: ${LTDLINCL="-I$abs_top_srcdir/libltdl"}
: ${LIBLTDL="$abs_builddir/../libltdl/libltdlc.la"}

# Skip this test when called from:
#    make distcheck DISTCHECK_CONFIGURE_FLAGS=--disable-ltdl-install
AT_CHECK([case $LIBLTDL in #(
 */_inst/lib/*) test -f "$LIBLTDL" || (exit 77) ;;
esac], [], [ignore])

CPPFLAGS="$LTDLINCL $CPPFLAGS"

dlopenable='resident local global'
dlpreloadable='preload'

# ------------------------------------------------------------------------- #
# The depend test makes no sense unless compiled without -no-undefined. By  #
# not building the depend module in that case, when this test passes we can #
# be sure that hint_global() above, is being correctly informed those hosts #
# are reporting not able to accept the global hint to lt_dlopenadvise().    #
# ------------------------------------------------------------------------- #

have_shared=false
$LIBTOOL --features | $GREP 'enable shared libraries' >/dev/null && have_shared=:

case $host_os,$have_shared in
cygwin* | mingw* | cegcc* | *,false)
  # These hosts do not support linking without -no-undefined
  CPPFLAGS="$CPPFLAGS -DHAVE_UNDEFINED_SYMBOLS=0"
  ;;
*)
  CPPFLAGS="$CPPFLAGS -DHAVE_UNDEFINED_SYMBOLS=1"
  dlopenable="$dlopen depend"
  $LIBTOOL --mode=compile $CC $CPPFLAGS $CFLAGS -c moddepend.c
  AT_CHECK([$LIBTOOL --mode=link $CC -module $CFLAGS $LDFLAGS -o libdepend.la ]dnl
           [moddepend.lo -rpath /foo -avoid-version], [], [ignore], [ignore])
  ;;
esac

$have_shared && CPPFLAGS="$CPPFLAGS -DHAVE_SHARED"

# ------------------------------------------------------------------------- #
# Other modules can be tested correctly even when built with -no-undefined. #
# ------------------------------------------------------------------------- #

LDFLAGS="$LDFLAGS -no-undefined"

$CC $CPPFLAGS $CFLAGS -c main.c
for name in resident local global preload; do
  # FIXME: adding -static to libpreload shouldn't be necessary.
  #        Fix this properly in ltmain, then remove this workaround.
  if test preload = "$name"; then
    st=-static
  else
    st=
  fi
  $LIBTOOL --mode=compile $CC $CPPFLAGS $CFLAGS -c mod$name.c
  AT_CHECK([$LIBTOOL --mode=link $CC -module $CFLAGS $LDFLAGS -o lib$name.la ]dnl
           [mod$name.lo $st -rpath /foo -avoid-version], [], [ignore], [ignore])
done

preloaded=
for module in $dlpreloadable; do
  preloaded="${preloaded+$preloaded }-dlpreopen lib$module.la"
done

modules=
for module in $dlopenable; do
  modules="${modules+$modules }-dlopen lib$module.la"
done

AT_CHECK([$LIBTOOL --mode=link $CC $CFLAGS $LDFLAGS -o main$EXEEXT main.$OBJEXT $preloaded $modules $LIBLTDL],
	 [], [ignore], [ignore])

# Remove loadable libpreload module, so we know it is the preloaded module
# that is being executed by a successful test invocation:
AT_CHECK([$LIBTOOL --mode=clean rm -f libpreload.la], [], [ignore], [ignore])

LT_AT_NOINST_EXEC_CHECK([./main], [$modules], [], [expout], [])

AT_CLEANUP