Blame gmodule/gmodule-ar.c

Packit ae235b
/* GMODULE - GLIB wrapper code for dynamic module loading
Packit ae235b
 * Copyright (C) 1998, 2000 Tim Janik
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/* 
Packit ae235b
 * MT safe
Packit ae235b
 */
Packit ae235b
Packit ae235b
/* because we are compatible with archive format only since AIX 4.3 */
Packit ae235b
Packit ae235b
#define __AR_BIG__
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <ar.h>
Packit ae235b
#include <stdlib.h>
Packit ae235b
Packit ae235b
#include <dlfcn.h>
Packit ae235b
Packit ae235b
/* --- functions --- */
Packit ae235b
static gchar*
Packit ae235b
fetch_dlerror (gboolean replace_null)
Packit ae235b
{
Packit ae235b
  gchar *msg = dlerror ();
Packit ae235b
Packit ae235b
  /* make sure we always return an error message != NULL, if
Packit ae235b
   * expected to do so. */
Packit ae235b
Packit ae235b
  if (!msg && replace_null)
Packit ae235b
    return "unknown dl-error";
Packit ae235b
Packit ae235b
  return msg;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gchar* _g_module_get_member(const gchar* file_name)
Packit ae235b
{
Packit ae235b
  gchar* member = NULL;
Packit ae235b
  struct fl_hdr file_header;
Packit ae235b
  struct ar_hdr ar_header;
Packit ae235b
  long first_member;
Packit ae235b
  long name_len;
Packit ae235b
  int fd;
Packit ae235b
Packit ae235b
  fd = open(file_name, O_RDONLY);
Packit ae235b
  if (fd == -1)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  if (read(fd, (void*)&file_header, FL_HSZ) != FL_HSZ)
Packit ae235b
    goto exit;
Packit ae235b
Packit ae235b
  if (strncmp(file_header.fl_magic, AIAMAGBIG, SAIAMAG) != 0)
Packit ae235b
    goto exit;
Packit ae235b
Packit ae235b
  /* read first archive file member header */
Packit ae235b
Packit ae235b
  first_member = atol(file_header.fl_fstmoff);
Packit ae235b
Packit ae235b
  if (lseek(fd, first_member, SEEK_SET) != first_member)
Packit ae235b
    goto exit;
Packit ae235b
Packit ae235b
  if (read(fd, (void*)&ar_header, AR_HSZ - 2) != AR_HSZ - 2)
Packit ae235b
    goto exit;
Packit ae235b
Packit ae235b
  /* read member name */
Packit ae235b
Packit ae235b
  name_len = atol(ar_header.ar_namlen);
Packit ae235b
Packit ae235b
  member = g_malloc(name_len+1);
Packit ae235b
  if (!member)
Packit ae235b
    goto exit;
Packit ae235b
Packit ae235b
  if (read(fd, (void*)member, name_len) != name_len)
Packit ae235b
    {
Packit ae235b
      g_free(member);
Packit ae235b
      member = NULL;
Packit ae235b
      goto exit;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  member[name_len] = 0;
Packit ae235b
Packit ae235b
exit:
Packit ae235b
  close(fd);
Packit ae235b
Packit ae235b
  return member;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
_g_module_open (const gchar *file_name,
Packit ae235b
		gboolean     bind_lazy,
Packit ae235b
		gboolean     bind_local)
Packit ae235b
{
Packit ae235b
  gpointer handle;
Packit ae235b
  gchar* member;
Packit ae235b
  gchar* full_name;
Packit ae235b
Packit ae235b
  /* extract name of first member of archive */
Packit ae235b
Packit ae235b
  member = _g_module_get_member (file_name);
Packit ae235b
  if (member != NULL)
Packit ae235b
    {
Packit ae235b
      full_name = g_strconcat (file_name, "(", member, ")", NULL);
Packit ae235b
      g_free (member);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    full_name = g_strdup (file_name);
Packit ae235b
  
Packit ae235b
  handle = dlopen (full_name, 
Packit ae235b
		   (bind_local ? RTLD_LOCAL : RTLD_GLOBAL) | RTLD_MEMBER | (bind_lazy ? RTLD_LAZY : RTLD_NOW));
Packit ae235b
Packit ae235b
  g_free (full_name);
Packit ae235b
Packit ae235b
  if (!handle)
Packit ae235b
    g_module_set_error (fetch_dlerror (TRUE));
Packit ae235b
  
Packit ae235b
  return handle;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
_g_module_self (void)
Packit ae235b
{
Packit ae235b
  gpointer handle;
Packit ae235b
Packit ae235b
  handle = dlopen (NULL, RTLD_GLOBAL | RTLD_LAZY);
Packit ae235b
  if (!handle)
Packit ae235b
    g_module_set_error (fetch_dlerror (TRUE));
Packit ae235b
  
Packit ae235b
  return handle;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
_g_module_close (gpointer handle,
Packit ae235b
		 gboolean is_unref)
Packit ae235b
{
Packit ae235b
  /* are there any systems out there that have dlopen()/dlclose()
Packit ae235b
   * without a reference count implementation?
Packit ae235b
   */
Packit ae235b
  is_unref |= 1;
Packit ae235b
  
Packit ae235b
  if (is_unref)
Packit ae235b
    {
Packit ae235b
      if (dlclose (handle) != 0)
Packit ae235b
	g_module_set_error (fetch_dlerror (TRUE));
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
_g_module_symbol (gpointer     handle,
Packit ae235b
		  const gchar *symbol_name)
Packit ae235b
{
Packit ae235b
  gpointer p;
Packit ae235b
  
Packit ae235b
  p = dlsym (handle, symbol_name);
Packit ae235b
  if (!p)
Packit ae235b
    g_module_set_error (fetch_dlerror (FALSE));
Packit ae235b
  
Packit ae235b
  return p;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gchar*
Packit ae235b
_g_module_build_path (const gchar *directory,
Packit ae235b
		      const gchar *module_name)
Packit ae235b
{
Packit ae235b
  if (directory && *directory) {
Packit ae235b
    if (strncmp (module_name, "lib", 3) == 0)
Packit ae235b
      return g_strconcat (directory, "/", module_name, NULL);
Packit ae235b
    else
Packit ae235b
      return g_strconcat (directory, "/lib", module_name, "." G_MODULE_SUFFIX, NULL);
Packit ae235b
  } else if (strncmp (module_name, "lib", 3) == 0)
Packit ae235b
    return g_strdup (module_name);
Packit ae235b
  else
Packit ae235b
    return g_strconcat ("lib", module_name, "." G_MODULE_SUFFIX, NULL);
Packit ae235b
}