Code Search for Developers
 
 
  

dlfcn.cc from Gdb at Krugle


Show dlfcn.cc syntax highlighted

/* dlfcn.cc

   Copyright 1998, 2000, 2001, 2002, 2003, 2004 Red Hat, Inc.

This file is part of Cygwin.

This software is a copyrighted work licensed under the terms of the
Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
details. */

#include "winsup.h"
#include <psapi.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include "cygerrno.h"
#include "security.h"
#include "path.h"
#include "fhandler.h"
#include "perprocess.h"
#include "thread.h"
#include "dlfcn.h"
#include "dll_init.h"
#include "cygtls.h"

static void __stdcall
set_dl_error (const char *str)
{
  strcpy (_my_tls.locals.dl_buffer, strerror (get_errno ()));
  _my_tls.locals.dl_error = 1;
}

/* Look for an executable file given the name and the environment
   variable to use for searching (eg., PATH); returns the full
   pathname (static buffer) if found or NULL if not. */
inline const char * __stdcall
check_path_access (const char *mywinenv, const char *name, path_conv& buf)
{
  return find_exec (name, buf, mywinenv, FE_NNF | FE_NATIVE | FE_CWD | FE_DLL);
}

/* Search LD_LIBRARY_PATH for dll, if it exists.
   Return Windows version of given path. */
static const char * __stdcall
get_full_path_of_dll (const char* str, char *name)
{
  int len = strlen (str);

  /* empty string or too long to be legal win32 pathname? */
  if (len == 0 || len >= CYG_MAX_PATH)
    return str;		/* Yes.  Let caller deal with it. */

  const char *ret;

  strcpy (name, str);	/* Put it somewhere where we can manipulate it. */

  /* Add extension if necessary */
  if (str[len - 1] != '.')
    {
      /* Add .dll only if no extension provided. */
      const char *p = strrchr (str, '.');
      if (!p || strpbrk (p, "\\/"))
	strcat (name, ".dll");
    }

  path_conv real_filename;

  if (isabspath (name) ||
      (ret = check_path_access ("LD_LIBRARY_PATH=", name, real_filename)
	     ?: check_path_access ("/usr/lib", name, real_filename)) == NULL)
    real_filename.check (name, PC_SYM_FOLLOW | PC_NOFULL | PC_NULLEMPTY);	/* Convert */

  if (!real_filename.error)
    ret = strcpy (name, real_filename);
  else
    {
      set_errno (real_filename.error);
      ret = NULL;
    }

  return ret;
}

void *
dlopen (const char *name, int)
{
  void *ret;

  if (name == NULL)
    ret = (void *) GetModuleHandle (NULL); /* handle for the current module */
  else
    {
      char buf[CYG_MAX_PATH];
      /* handle for the named library */
      const char *fullpath = get_full_path_of_dll (name, buf);
      if (!fullpath)
	ret = NULL;
      else
	{
	  ret = (void *) LoadLibrary (fullpath);
	  if (ret == NULL)
	    __seterrno ();
	}
    }

  if (!ret)
    set_dl_error ("dlopen");
  debug_printf ("ret %p", ret);

  return ret;
}

void *
dlsym (void *handle, const char *name)
{
  void *ret = NULL;
  if (handle == RTLD_DEFAULT)
    { /* search all modules */
      HANDLE cur_proc = GetCurrentProcess ();
      HMODULE *modules;
      DWORD needed, i;
      if (!EnumProcessModules (cur_proc, NULL, 0, &needed))
	{
	dlsym_fail:
	  set_dl_error ("dlsym");
	  return NULL;
	}
      modules = (HMODULE*) alloca (needed);
      if (!EnumProcessModules (cur_proc, modules, needed, &needed))
	goto dlsym_fail;
      for (i = 0; i < needed / sizeof (HMODULE); i++)
	if ((ret = (void *) GetProcAddress (modules[i], name)))
	  break;
    }
  else
    ret = (void *) GetProcAddress ((HMODULE)handle, name);
  if (!ret)
    set_dl_error ("dlsym");
  debug_printf ("ret %p", ret);
  return ret;
}

int
dlclose (void *handle)
{
  int ret = -1;
  void *temphandle = (void *) GetModuleHandle (NULL);
  if (temphandle == handle || FreeLibrary ((HMODULE) handle))
    ret = 0;
  if (ret)
    set_dl_error ("dlclose");
  CloseHandle ((HMODULE) temphandle);
  return ret;
}

char *
dlerror ()
{
  char *res;
  if (!_my_tls.locals.dl_error)
    res = NULL;
  else
    {
      _my_tls.locals.dl_error = 0;
      res = _my_tls.locals.dl_buffer;
    }
  return res;
}




See more files for this project here

Gdb

GDB, the GNU Project debugger, allows you to see what is going on `inside' another program while it executes -- or what another program was doing at the moment it crashed.

Project homepage: http://sources.redhat.com/gdb/
Programming language(s): Assembly,C,C++,Expect
License: other

  config/
  include/
  lib/
  libc/
  regex/
  regexp/
  CYGWIN_LICENSE
  ChangeLog
  ChangeLog-1995
  ChangeLog-1996
  ChangeLog-1997
  ChangeLog-1998
  ChangeLog-1999
  ChangeLog-2000
  ChangeLog-2001
  ChangeLog-2002
  ChangeLog-2003
  ChangeLog-2004
  ChangeLog-2005
  ChangeLog-2006
  Makefile.in
  ROADMAP
  acconfig.h
  aclocal.m4
  analyze_sigfe
  ansi.sgml
  assert.cc
  autoload.cc
  automode.c
  binmode.c
  child_info.h
  config.h.in
  configure
  configure.in
  cpuid.h
  crt0.c
  ctype.cc
  cxx.cc
  cygerrno.h
  cygheap.cc
  cygheap.h
  cyglsa.h
  cygmagic
  cygmalloc.h
  cygserver.h
  cygserver_ipc.h
  cygserver_msg.h
  cygserver_sem.h
  cygserver_shm.h
  cygthread.cc
  cygthread.h
  cygtls.cc
  cygtls.h
  cygwin-shilka
  cygwin.din
  cygwin.sc
  cygwin_version.h
  dcrt0.cc
  debug.cc
  debug.h
  delqueue.cc
  devices.cc
  devices.h
  devices.in
  dir.cc
  dlfcn.cc
  dll_init.cc
  dll_init.h
  dll_init.sgml
  dllfixdbg
  dlmalloc.c
  dlmalloc.h
  dtable.cc
  dtable.h
  dtable.sgml
  environ.cc
  environ.h
  errno.cc
  exceptions.cc
  exec.cc
  external.cc
  external.sgml
  fcntl.cc
  fhandler.cc
  fhandler.h
  fhandler_clipboard.cc
  fhandler_console.cc
  fhandler_disk_file.cc
  fhandler_dsp.cc
  fhandler_fifo.cc
  fhandler_floppy.cc
  fhandler_mailslot.cc
  fhandler_mem.cc
  fhandler_netdrive.cc
  fhandler_nodevice.cc
  fhandler_proc.cc
  fhandler_process.cc
  fhandler_procnet.cc
  fhandler_random.cc
  fhandler_raw.cc
  fhandler_registry.cc
  fhandler_serial.cc
  fhandler_socket.cc
  fhandler_tape.cc
  fhandler_termios.cc
  fhandler_tty.cc
  fhandler_virtual.cc
  fhandler_windows.cc
  fhandler_zero.cc
  flock.cc
  fork.cc
  gcrt0.c
  gendef
  gendevices
  gentls_offsets
  glob.cc
  gmon.c
  gmon.h
  grp.cc
  heap.cc
  heap.h
  hires.h
  hookapi.cc
  how-autoload-works.txt
  how-cygheap-works.txt