コード例 #1
0
ファイル: 1182.c プロジェクト: ALurker/exploit-database
/*
 * search_ldso(): search for a symbol inside ld.so.1
 */
int search_ldso(char *sym)
{
	int		addr;
	void		*handle;
	Link_map	*lm;

	/* open the executable object file */
	if ((handle = dlmopen(LM_ID_LDSO, NULL, RTLD_LAZY)) == NULL) {
		perror("dlopen");
		exit(1);
	}

	/* get dynamic load information */
	if ((dlinfo(handle, RTLD_DI_LINKMAP, &lm)) == -1) {
		perror("dlinfo");
		exit(1);
	}

	/* search for the address of the symbol */
	if ((addr = (int)dlsym(handle, sym)) == NULL) {
		fprintf(stderr, "sorry, function %s() not found\n", sym);
		exit(1);
	}

	/* close the executable object file */
	dlclose(handle);

	check_zero(addr - 4, sym);
	return(addr);
}
コード例 #2
0
mdb_demangler_t *
mdb_dem_load(const char *path)
{
	mdb_demangler_t *dmp;
	void *hdl, *func;

	if (access(path, F_OK) == -1)
		return (NULL);

	if ((hdl = dlmopen(LM_ID_BASE, path, RTLD_LAZY | RTLD_LOCAL)) == NULL) {
		(void) set_errno(EMDB_RTLD);
		return (NULL);
	}

	if ((func = dlsym(hdl, "cplus_demangle")) == NULL) {
		(void) dlclose(hdl);
		(void) set_errno(EMDB_NODEM);
		return (NULL);
	}

	dmp = mdb_alloc(sizeof (mdb_demangler_t), UM_SLEEP);
	(void) strncpy(dmp->dm_pathname, path, MAXPATHLEN);
	dmp->dm_pathname[MAXPATHLEN - 1] = '\0';
	dmp->dm_handle = hdl;
	dmp->dm_convert = (int (*)())func;
	dmp->dm_len = MDB_SYM_NAMLEN * 2;
	dmp->dm_buf = mdb_alloc(dmp->dm_len, UM_SLEEP);
	dmp->dm_flags = MDB_DM_SCOPE;

	return (dmp);
}
コード例 #3
0
ファイル: test.c プロジェクト: desphunter/shadow
int main(int argc, char* argv[]) {
    int ret;
    if (!getenv("LD_STATIC_TLS_EXTRA")) {
        /* in this path, we calculate the static TLS size we would need */
        dlerror();
        unsigned long tls_size_start;
        /* we need a handle for dlinfo to work, even though we're not using it */
        void* handle = dlmopen(LM_ID_NEWLM, PLUGIN_PATH,
                RTLD_LAZY | RTLD_LOCAL);
        int result = dlinfo(handle, RTLD_DI_STATIC_TLS_SIZE, &tls_size_start);
        if (result != 0) {
            fprintf(stdout, "error in dlinfo() for handle %p, dlerror is '%s'\n",
                    handle, dlerror());
        }
        dlerror();
        handle = dlmopen(LM_ID_NEWLM, PLUGIN_PATH, RTLD_LAZY | RTLD_LOCAL);
        unsigned long tls_size_end;
        result = dlinfo(handle, RTLD_DI_STATIC_TLS_SIZE, &tls_size_end);
        if (result != 0) {
            fprintf(stdout,  "error in dlinfo() for handle %p, dlerror is '%s'\n",
                    handle, dlerror());
        }
        char call[100];
        unsigned long tls_size_to_allocate = (tls_size_end - tls_size_start)
                * NUM_LOADS;
        /* this strips environment variables, but there are ways to fix that */
        sprintf(call, "env LD_STATIC_TLS_EXTRA=%lu ./test",
                tls_size_to_allocate);
        system(call);
        ret = EXIT_SUCCESS;
    } else {
        /* the correct sized buffer has been allocated, run things as normal */
        ret = run();
    }
    return ret;
}
コード例 #4
0
ファイル: test.c プロジェクト: desphunter/shadow
int run(void) {
    void* handles[NUM_LOADS];

    for(int i = 0; i < NUM_LOADS; i++) {
        /* clear dlerror */
        dlerror();

        /* load the plugin */
        handles[i] = dlmopen(LM_ID_NEWLM, PLUGIN_PATH, RTLD_LAZY|RTLD_LOCAL);

        /* check for plugin load error */
        if(!handles[i]) {
            fprintf(stdout, "dlmopen() for path '%s' returned NULL, dlerror is '%s'\n",
                    PLUGIN_PATH, dlerror());
            return EXIT_FAILURE;
        }

        /* clear dlerror */
        dlerror();

        /* check the id of the new handle */
        Lmid_t lmid = 0;
        int result = dlinfo(handles[i], RTLD_DI_LMID, &lmid);

        if(result == 0) {
            fprintf(stdout, "found id %lu for handle %p\n", (long)lmid, handles[i]);
        } else {
            fprintf(stdout, "error in dlinfo() for handle %p, dlerror is '%s'\n",
                    handles[i], dlerror());
        }

        /* clear dlerror */
        dlerror();

        /* lookup a function symbol in the plugin we just loaded */
        void* func = dlsym(handles[i], PLUGIN_SYM);
        if(!func) {
            fprintf(stdout, "dlsym() for symbol '%s' returned NULL, dlerror is '%s'\n",
                    PLUGIN_SYM, dlerror());
            return EXIT_FAILURE;
        }
    }

    _print_tls_size(handles[NUM_LOADS-1]);

    // TODO close the dlmopened handles
    return EXIT_SUCCESS;
}
コード例 #5
0
ファイル: 8305_0.c プロジェクト: B-Rich/osf_db
/* ********************************************** *
 * *************** GET_STRCPY_ADDR ************** *
 * ********************************************** */
unsigned long get_strcpy_addr()
{
  void *handle;
  Link_map *lm;
  unsigned long addr;

  if((handle = dlmopen(LM_ID_LDSO, NULL, RTLD_LAZY)) == NULL)
  {
    perror("dlmopen");
    return 0;
  }


  if((dlinfo(handle, RTLD_DI_LINKMAP, &lm)) == -1)
  {
    perror("dlinfo");
    return 0;
  }


  if((addr = (unsigned long)dlsym(handle, "strcpy")) == NULL)
  {
    perror("dlsym");
    return 0;
  } 
  
  /* -4 to skip save and use 
   * our fake frame instead */
  addr -= 4;
 
  /* make sure addr doesn't contain any 0x00 bytes,
   * or '/' characters (as this is where strcpy will
   * cutoff in ld.so.1) */
  if( !(addr & 0xFF) || !(addr & 0xFF00) || 
      !(addr & 0xFF0000) || !(addr & 0xFF000000) ||
      ((addr & 0xFF) == 0x2f) ||
      ((addr & 0xFF00) == 0x2f) ||
      ((addr & 0xFF0000) == 0x2f) ||
      ((addr & 0xFF000000) == 0x2f) )
  {
    printf("ERROR: strcpy address (0x%x) contains unusable bytes somewhere.\n", addr);
    printf("       -> consider using strncpy, memcpy, or another similar substitute instead.\n");
    return 0;
  }

  return addr;
}
コード例 #6
0
ファイル: shd-main.c プロジェクト: desphunter/shadow
static gboolean _main_loadShadowPreload(Options* options) {
    const gchar* preloadOptionStr = options_getPreloadString(options);

    /* clear error message */
    dlerror();

    /* open the shadow preload lib as a preload and in the base program namespace */
    void* handle = dlmopen(LM_ID_BASE, preloadOptionStr, RTLD_LAZY|RTLD_PRELOAD);

    const gchar* errorMessage = dlerror();

    if(handle && !errorMessage) {
        return TRUE;
    } else {
        return FALSE;
    }
}
コード例 #7
0
ファイル: tststatic2.c プロジェクト: AubrCool/glibc
static int
do_test (void)
{
  void *handle = dlopen ("modstatic2-nonexistent.so", RTLD_LAZY);
  if (handle == NULL)
    printf ("nonexistent: %s\n", dlerror ());
  else
    exit (1);

  handle = dlopen ("modstatic2.so", RTLD_LAZY);
  if (handle == NULL)
    {
      printf ("%s\n", dlerror ());
      exit (1);
    }

  int (*test) (FILE *, int);
  test = dlsym (handle, "test");
  if (test == NULL)
    {
      printf ("%s\n", dlerror ());
      exit (1);
    }

  Dl_info info;
  int res = dladdr (test, &info);
  if (res == 0)
    {
      puts ("dladdr returned 0");
      exit (1);
    }
  else
    {
      if (strstr (info.dli_fname, "modstatic2.so") == NULL
	  || strcmp (info.dli_sname, "test") != 0)
	{
	  printf ("fname %s sname %s\n", info.dli_fname, info.dli_sname);
	  exit (1);
	}
      if (info.dli_saddr != (void *) test)
	{
	  printf ("saddr %p != test %p\n", info.dli_saddr, test);
	  exit (1);
	}
    }

  ElfW(Sym) *sym;
  void *symp;
  res = dladdr1 (test, &info, &symp, RTLD_DL_SYMENT);
  if (res == 0)
    {
      puts ("dladdr1 returned 0");
      exit (1);
    }
  else
    {
      if (strstr (info.dli_fname, "modstatic2.so") == NULL
	  || strcmp (info.dli_sname, "test") != 0)
	{
	  printf ("fname %s sname %s\n", info.dli_fname, info.dli_sname);
	  exit (1);
	}
      if (info.dli_saddr != (void *) test)
	{
	  printf ("saddr %p != test %p\n", info.dli_saddr, test);
	  exit (1);
	}
      sym = symp;
      if (sym == NULL)
	{
	  puts ("sym == NULL\n");
	  exit (1);
	}
      if (ELF32_ST_BIND (sym->st_info) != STB_GLOBAL
	  || ELF32_ST_VISIBILITY (sym->st_other) != STV_DEFAULT)
	{
	  printf ("bind %d visibility %d\n",
		  (int) ELF32_ST_BIND (sym->st_info),
		  (int) ELF32_ST_VISIBILITY (sym->st_other));
	  exit (1);
	}
    }

  Lmid_t lmid;
  res = dlinfo (handle, RTLD_DI_LMID, &lmid);
  if (res != 0)
    {
      printf ("dlinfo returned %d %s\n", res, dlerror ());
      exit (1);
    }
  else if (lmid != LM_ID_BASE)
    {
      printf ("lmid %d != %d\n", (int) lmid, (int) LM_ID_BASE);
      exit (1);
    }

  res = test (stdout, 2);
  if (res != 4)
    {
      printf ("Got %i, expected 4\n", res);
      exit (1);
    }

  void *handle2 = dlopen (LIBDL_SO, RTLD_LAZY);
  if (handle2 == NULL)
    {
      printf ("libdl.so: %s\n", dlerror ());
      exit (1);
    }

  if (dlvsym (handle2, "_dlfcn_hook", "GLIBC_PRIVATE") == NULL)
    {
      printf ("dlvsym: %s\n", dlerror ());
      exit (1);
    }

  void *(*dlsymfn) (void *, const char *);
  dlsymfn = dlsym (handle2, "dlsym");
  if (dlsymfn == NULL)
    {
      printf ("dlsym \"dlsym\": %s\n", dlerror ());
      exit (1);
    }
  void *test2 = dlsymfn (handle, "test");
  if (test2 == NULL)
    {
      printf ("%s\n", dlerror ());
      exit (1);
    }
  else if (test2 != (void *) test)
    {
      printf ("test %p != test2 %p\n", test, test2);
      exit (1);
    }

  dlclose (handle2);
  dlclose (handle);

  handle = dlmopen (LM_ID_BASE, "modstatic2.so", RTLD_LAZY);
  if (handle == NULL)
    {
      printf ("%s\n", dlerror ());
      exit (1);
    }
  dlclose (handle);

  handle = dlmopen (LM_ID_NEWLM, "modstatic2.so", RTLD_LAZY);
  if (handle == NULL)
    printf ("LM_ID_NEWLM: %s\n", dlerror ());
  else
    {
      puts ("LM_ID_NEWLM unexpectedly succeeded");
      exit (1);
    }

  return 0;
}
コード例 #8
0
ファイル: mdb_tdb.c プロジェクト: andreiw/polaris
const mdb_tdb_ops_t *
mdb_tdb_load(const char *path)
{
	td_err_e (*tdb_init)(void);
	mdb_tdb_lib_t *t;
	td_err_e err;
	void *hdl;

	/*
	 * Search through the existing cache of thread_db libraries and see if
	 * we have this one loaded already.  If so, just return its ops vector.
	 */
	for (t = tdb_list; t != NULL; t = t->tdb_next) {
		if (strcmp(path, t->tdb_pathname) == 0)
			break;
	}

	if (t != NULL)
		return (&t->tdb_ops);

	/*
	 * Otherwise dlmopen the new library, look up its td_init() function,
	 * and call it.  If any of this fails, we return NULL for failure.
	 */
	if (access(path, F_OK) == -1)
		return (NULL);

	if ((hdl = dlmopen(LM_ID_BASE, path, RTLD_LAZY | RTLD_LOCAL)) == NULL) {
		(void) set_errno(EMDB_RTLD);
		return (NULL);
	}

	if ((tdb_init = (td_err_e (*)(void))dlsym(hdl, "td_init")) == NULL) {
		(void) dlclose(hdl);
		(void) set_errno(tdb_to_errno(TD_NOCAPAB));
		return (NULL);
	}

	if ((err = tdb_init()) != TD_OK) {
		(void) dlclose(hdl);
		(void) set_errno(tdb_to_errno(err));
		return (NULL);
	}

	/*
	 * If td_init() succeeds, we can't fail from here on.  Allocate a new
	 * library entry and add it to our linked list.
	 */
	t = mdb_alloc(sizeof (mdb_tdb_lib_t), UM_SLEEP);

	(void) strncpy(t->tdb_pathname, path, MAXPATHLEN);
	t->tdb_pathname[MAXPATHLEN - 1] = '\0';
	t->tdb_handle = hdl;
	t->tdb_next = tdb_list;
	tdb_list = t;

	/*
	 * For each function we need to call in the thread_db library, look it
	 * up using dlsym().  If we find it, add it to the ops vector.  If not,
	 * put the address of our default function (see above) in that slot.
	 */

	t->tdb_ops.td_ta_new = (td_err_e (*)())dlsym(hdl, "td_ta_new");
	if (t->tdb_ops.td_ta_new == NULL)
		t->tdb_ops.td_ta_new = (td_err_e (*)())tdb_notsup;

	t->tdb_ops.td_ta_delete = (td_err_e (*)())dlsym(hdl, "td_ta_delete");
	if (t->tdb_ops.td_ta_delete == NULL)
		t->tdb_ops.td_ta_delete = (td_err_e (*)())tdb_notsup;

	t->tdb_ops.td_ta_thr_iter = (td_err_e (*)())
	    dlsym(hdl, "td_ta_thr_iter");
	if (t->tdb_ops.td_ta_thr_iter == NULL)
		t->tdb_ops.td_ta_thr_iter = (td_err_e (*)())tdb_notsup;

	t->tdb_ops.td_ta_map_id2thr = (td_err_e (*)())
	    dlsym(hdl, "td_ta_map_id2thr");
	if (t->tdb_ops.td_ta_map_id2thr == NULL)
		t->tdb_ops.td_ta_map_id2thr = (td_err_e (*)())tdb_notsup;

	t->tdb_ops.td_ta_map_lwp2thr = (td_err_e (*)())
	    dlsym(hdl, "td_ta_map_lwp2thr");
	if (t->tdb_ops.td_ta_map_lwp2thr == NULL)
		t->tdb_ops.td_ta_map_lwp2thr = (td_err_e (*)())tdb_notsup;

	t->tdb_ops.td_thr_get_info = (td_err_e (*)())
	    dlsym(hdl, "td_thr_get_info");
	if (t->tdb_ops.td_thr_get_info == NULL)
		t->tdb_ops.td_thr_get_info = (td_err_e (*)())tdb_notsup;

	t->tdb_ops.td_thr_getgregs = (td_err_e (*)())
	    dlsym(hdl, "td_thr_getgregs");
	if (t->tdb_ops.td_thr_getgregs == NULL)
		t->tdb_ops.td_thr_getgregs = (td_err_e (*)())tdb_notsup;

	t->tdb_ops.td_thr_setgregs = (td_err_e (*)())
	    dlsym(hdl, "td_thr_setgregs");
	if (t->tdb_ops.td_thr_setgregs == NULL)
		t->tdb_ops.td_thr_setgregs = (td_err_e (*)())tdb_notsup;

	t->tdb_ops.td_thr_getfpregs = (td_err_e (*)())
	    dlsym(hdl, "td_thr_getfpregs");
	if (t->tdb_ops.td_thr_getfpregs == NULL)
		t->tdb_ops.td_thr_getfpregs = (td_err_e (*)())tdb_notsup;

	t->tdb_ops.td_thr_setfpregs = (td_err_e (*)())
	    dlsym(hdl, "td_thr_setfpregs");
	if (t->tdb_ops.td_thr_setfpregs == NULL)
		t->tdb_ops.td_thr_setfpregs = (td_err_e (*)())tdb_notsup;

	t->tdb_ops.td_thr_tlsbase = (td_err_e (*)())
	    dlsym(hdl, "td_thr_tlsbase");
	if (t->tdb_ops.td_thr_tlsbase == NULL)
		t->tdb_ops.td_thr_tlsbase = (td_err_e (*)())tdb_notsup;

#ifdef __sparc
	t->tdb_ops.td_thr_getxregs = (td_err_e (*)())
	    dlsym(hdl, "td_thr_getxregs");
	if (t->tdb_ops.td_thr_getxregs == NULL)
		t->tdb_ops.td_thr_getxregs = (td_err_e (*)())tdb_notsup;

	t->tdb_ops.td_thr_setxregs = (td_err_e (*)())
	    dlsym(hdl, "td_thr_setxregs");
	if (t->tdb_ops.td_thr_setxregs == NULL)
		t->tdb_ops.td_thr_setxregs = (td_err_e (*)())tdb_notsup;
#endif	/* __sparc */

	return (&t->tdb_ops);
}
コード例 #9
0
ファイル: test20.c プロジェクト: shadow/shadow
#define _GNU_SOURCE 1
#include <dlfcn.h>
#include <stdio.h>

int main (__attribute__((unused)) int argc, char *argv[])
{
  printf ("enter\n");
  void *h = dlmopen (LM_ID_NEWLM, argv[0], RTLD_LAZY | RTLD_GLOBAL);
  if (h != 0)
    {
      printf ("loaded %s second time\n", argv[0]);
    }
  dlclose (h);
  printf ("leave\n");
  return 0;
}
コード例 #10
0
ファイル: apptrace.c プロジェクト: NanXiao/illumos-joyent
/* ARGSUSED */
void
la_preinit(uintptr_t *cookie)
{
	void	*h = NULL;

	(void) sigfillset(&abisigset);

	h = dlmopen(LM_ID_BASE, libcpath, RTLD_LAZY | RTLD_NOLOAD);
	if (h == NULL)
		apptrace_preinit_fail();

	if ((abi_thr_self =
	    (thread_t (*)(void)) dlsym(h, thr_self_sym)) == NULL)
		apptrace_preinit_fail();
	if ((abi_thr_main =
	    (int (*)(void)) dlsym(h, thr_main_sym)) == NULL)
		apptrace_preinit_fail();

	/* Do printf style pointers */
	if ((ABI_VFPRINTF =
	    (int (*)(FILE *, char const *, va_list))
	    dlsym(h, vfprintf_sym)) == NULL)
		apptrace_preinit_fail();

	if ((ABI_VFWPRINTF =
	    (int (*)(FILE *, const wchar_t *, va_list))
	    dlsym(h, vfwprintf_sym)) == NULL)
		apptrace_preinit_fail();

	if ((ABI_VPRINTF =
	    (int (*)(char const *, va_list))
	    dlsym(h, vprintf_sym)) == NULL)
		apptrace_preinit_fail();

	if ((ABI_VSNPRINTF =
	    (int (*)(char *, size_t, char const *, va_list))
	    dlsym(h, vsnprintf_sym)) == NULL)
		apptrace_preinit_fail();

	if ((ABI_VSPRINTF =
	    (int (*)(char *, char const *, va_list))
	    dlsym(h, vsprintf_sym)) == NULL)
		apptrace_preinit_fail();

	if ((ABI_VSWPRINTF =
	    (int (*)(wchar_t *, size_t, const wchar_t *, va_list))
	    dlsym(h, vswprintf_sym)) == NULL)
		apptrace_preinit_fail();

	if ((ABI_VWPRINTF =
	    (int (*)(const wchar_t *, va_list))
	    dlsym(h, vwprintf_sym)) == NULL)
		apptrace_preinit_fail();

	if ((__abi_real_errno =
	    (int *(*)(void))
	    dlsym(h, errno_sym)) == NULL)
		apptrace_preinit_fail();

	(void) dlclose(h);
}
コード例 #11
0
ファイル: shd-main.c プロジェクト: desphunter/shadow
static gulong _main_computeLoadSize(Lmid_t lmid, const gchar* pluginPath, const gchar* preloadPath, gint dlmopenFlags) {
    gulong tlsSizeStart = 0, tlsSizeEnd = 0, tlsSizePerLoad = 0;
    gpointer handle1 = NULL, handle2 = NULL;
    gint result = 0;

    debug("computing static TLS size needed for library at path '%s' and preload at path '%s'",
            pluginPath, preloadPath ? preloadPath : "NULL");

    /* clear error */
    dlerror();

    /* get the initial TLS size used */
    result = dlinfo(NULL, RTLD_DI_STATIC_TLS_SIZE, &tlsSizeStart);

    if (result != 0) {
        warning("error in dlinfo() while computing TLS start size, dlerror is '%s'", dlerror());
        goto err;
    }

    /* open the plugin library in the requested lmid */
    handle1 = dlmopen(lmid, pluginPath, dlmopenFlags);

    if(handle1 == NULL) {
        warning("error in plugin dlmopen() while computing TLS size, dlerror is '%s'", dlerror());
        goto err;
    }

    if(preloadPath) {
        /* get the LMID so we can load it in the same namespace as the plugin */
        Lmid_t prev_lmid = 0;
        result = dlinfo(handle1, RTLD_DI_LMID, &prev_lmid);

        if(result != 0) {
            warning("dlinfo() failed when querying for previous LMID: %s", dlerror());
            goto err;
        }

        handle2 = dlmopen(prev_lmid, preloadPath, dlmopenFlags|RTLD_INTERPOSE);
        if(handle2 == NULL) {
            warning("error in preload dlmopen() while computing TLS size, dlerror is '%s'", dlerror());
            goto err;
        }
    }

    result = dlinfo(NULL, RTLD_DI_STATIC_TLS_SIZE, &tlsSizeEnd);

    if (result != 0) {
        warning("error in dlinfo() while computing TLS end size, dlerror is '%s'", dlerror());
        goto err;
    }

// TODO close the handles to clean up, after elf loader supports interleaving dlopens and dlcloses
//    dlclose(handle1);
//    dlclose(handle2);

    tlsSizePerLoad = (tlsSizeEnd - tlsSizeStart);

    /* log the result */
    GString* msg = g_string_new(NULL);
    g_string_printf(msg, "we need %lu bytes of static TLS per load of namespace with library at path '%s'", tlsSizePerLoad, pluginPath);
    if(preloadPath) {
        g_string_append_printf(msg, " and preload at path '%s'", preloadPath);
    }
    message("%s", msg->str);
    g_string_free(msg, TRUE);

    /* make sure we dont return 0 when successful */
    if(tlsSizePerLoad < 1) {
        tlsSizePerLoad = 1;
    }
    return tlsSizePerLoad;

err:
// TODO close the handles to clean up, after elf loader supports interleaving dlopens and dlcloses
//    if(handle1) {
//        dlclose(handle1);
//    }
//    if(handle2) {
//        dlclose(handle2);
//    }
    return 0;
}
コード例 #12
0
ファイル: modstatic2.c プロジェクト: AdvancedC/glibc
int
test (FILE *out, int a)
{
  fputs ("in modstatic2.c (test)\n", out);

  void *handle = dlopen ("modstatic2-nonexistent.so", RTLD_LAZY);
  if (handle == NULL)
    fprintf (out, "nonexistent: %s\n", dlerror ());
  else
    exit (1);

  handle = dlopen ("modstatic2.so", RTLD_LAZY);
  if (handle == NULL)
    {
      fprintf (out, "%s\n", dlerror ());
      exit (1);
    }

  int (*test2) (FILE *, int);
  test2 = dlsym (handle, "test");
  if (test2 == NULL)
    {
      fprintf (out, "%s\n", dlerror ());
      exit (1);
    }
  if (test2 != test)
    {
      fprintf (out, "test %p != test2 %p\n", test, test2);
      exit (1);
    }

  Dl_info info;
  int res = dladdr (test2, &info);
  if (res == 0)
    {
      fputs ("dladdr returned 0\n", out);
      exit (1);
    }
  else
    {
      if (strstr (info.dli_fname, "modstatic2.so") == NULL
	  || strcmp (info.dli_sname, "test") != 0)
	{
	  fprintf (out, "fname %s sname %s\n", info.dli_fname, info.dli_sname);
	  exit (1);
	}
      if (info.dli_saddr != (void *) test2)
	{
	  fprintf (out, "saddr %p != test %p\n", info.dli_saddr, test2);
	  exit (1);
	}
    }

  ElfW(Sym) *sym;
  void *symp;
  res = dladdr1 (test2, &info, &symp, RTLD_DL_SYMENT);
  if (res == 0)
    {
      fputs ("dladdr1 returned 0\n", out);
      exit (1);
    }
  else
    {
      if (strstr (info.dli_fname, "modstatic2.so") == NULL
	  || strcmp (info.dli_sname, "test") != 0)
	{
	  fprintf (out, "fname %s sname %s\n", info.dli_fname, info.dli_sname);
	  exit (1);
	}
      if (info.dli_saddr != (void *) test2)
	{
	  fprintf (out, "saddr %p != test %p\n", info.dli_saddr, test2);
	  exit (1);
	}
      sym = symp;
      if (sym == NULL)
	{
	  fputs ("sym == NULL\n", out);
	  exit (1);
	}
      if (ELF32_ST_BIND (sym->st_info) != STB_GLOBAL
	  || ELF32_ST_VISIBILITY (sym->st_other) != STV_DEFAULT)
	{
	  fprintf (out, "bind %d visibility %d\n",
		   (int) ELF32_ST_BIND (sym->st_info),
		   (int) ELF32_ST_VISIBILITY (sym->st_other));
	  exit (1);
	}
    }

  Lmid_t lmid;
  res = dlinfo (handle, RTLD_DI_LMID, &lmid);
  if (res != 0)
    {
      fprintf (out, "dlinfo returned %d %s\n", res, dlerror ());
      exit (1);
    }
  else if (lmid != LM_ID_BASE)
    {
      fprintf (out, "lmid %d != %d\n", (int) lmid, (int) LM_ID_BASE);
      exit (1);
    }

  void *handle2 = dlopen (LIBDL_SO, RTLD_LAZY);
  if (handle2 == NULL)
    {
      fprintf (out, "libdl.so: %s\n", dlerror ());
      exit (1);
    }

#ifdef DO_VERSIONING
  if (dlvsym (handle2, "_dlfcn_hook", "GLIBC_PRIVATE") == NULL)
    {
      fprintf (out, "dlvsym: %s\n", dlerror ());
      exit (1);
    }
#endif

  void *(*dlsymfn) (void *, const char *);
  dlsymfn = dlsym (handle2, "dlsym");
  if (dlsymfn == NULL)
    {
      fprintf (out, "dlsym \"dlsym\": %s\n", dlerror ());
      exit (1);
    }
  void *test3 = dlsymfn (handle, "test");
  if (test3 == NULL)
    {
      fprintf (out, "%s\n", dlerror ());
      exit (1);
    }
  else if (test3 != (void *) test2)
    {
      fprintf (out, "test2 %p != test3 %p\n", test2, test3);
      exit (1);
    }

  dlclose (handle2);
  dlclose (handle);

  handle = dlmopen (LM_ID_BASE, "modstatic2.so", RTLD_LAZY);
  if (handle == NULL)
    {
      fprintf (out, "%s\n", dlerror ());
      exit (1);
    }
  dlclose (handle);

  handle = dlmopen (LM_ID_NEWLM, "modstatic2.so", RTLD_LAZY);
  if (handle == NULL)
    fprintf (out, "LM_ID_NEWLM: %s\n", dlerror ());
  else
    {
      fputs ("LM_ID_NEWLM unexpectedly succeeded\n", out);
      exit (1);
    }

  handle = dlopen ("modstatic.so", RTLD_LAZY);
  if (handle == NULL)
    {
      fprintf (out, "%s\n", dlerror ());
      exit (1);
    }

  int (*test4) (int);
  test4 = dlsym (handle, "test");
  if (test4 == NULL)
    {
      fprintf (out, "%s\n", dlerror ());
      exit (1);
    }

  res = test4 (16);
  if (res != 16 + 16)
    {
      fprintf (out, "modstatic.so (test) returned %d\n", res);
      exit (1);
    }

  res = dladdr1 (test4, &info, &symp, RTLD_DL_SYMENT);
  if (res == 0)
    {
      fputs ("dladdr1 returned 0\n", out);
      exit (1);
    }
  else
    {
      if (strstr (info.dli_fname, "modstatic.so") == NULL
	  || strcmp (info.dli_sname, "test") != 0)
	{
	  fprintf (out, "fname %s sname %s\n", info.dli_fname, info.dli_sname);
	  exit (1);
	}
      if (info.dli_saddr != (void *) test4)
	{
	  fprintf (out, "saddr %p != test %p\n", info.dli_saddr, test4);
	  exit (1);
	}
      sym = symp;
      if (sym == NULL)
	{
	  fputs ("sym == NULL\n", out);
	  exit (1);
	}
      if (ELF32_ST_BIND (sym->st_info) != STB_GLOBAL
	  || ELF32_ST_VISIBILITY (sym->st_other) != STV_DEFAULT)
	{
	  fprintf (out, "bind %d visibility %d\n",
		   (int) ELF32_ST_BIND (sym->st_info),
		   (int) ELF32_ST_VISIBILITY (sym->st_other));
	  exit (1);
	}
    }

  dlclose (handle);

  fputs ("leaving modstatic2.c (test)\n", out);
  return a + a;
}