1/*
2 *  dlf.h
3 *
4 *  $Id: dlf.h,v 1.2 2006/01/20 15:58:35 source Exp $
5 *
6 *  Dynamic Library Loader (mapping to SVR4)
7 *
8 *  The iODBC driver manager.
9 *
10 *  Copyright (C) 1995 by Ke Jin <kejin@empress.com>
11 *  Copyright (C) 1996-2006 by OpenLink Software <iodbc@openlinksw.com>
12 *  All Rights Reserved.
13 *
14 *  This software is released under the terms of either of the following
15 *  licenses:
16 *
17 *      - GNU Library General Public License (see LICENSE.LGPL)
18 *      - The BSD License (see LICENSE.BSD).
19 *
20 *  Note that the only valid version of the LGPL license as far as this
21 *  project is concerned is the original GNU Library General Public License
22 *  Version 2, dated June 1991.
23 *
24 *  While not mandated by the BSD license, any patches you make to the
25 *  iODBC source code may be contributed back into the iODBC project
26 *  at your discretion. Contributions will benefit the Open Source and
27 *  Data Access community as a whole. Submissions may be made at:
28 *
29 *      http://www.iodbc.org
30 *
31 *
32 *  GNU Library Generic Public License Version 2
33 *  ============================================
34 *  This library is free software; you can redistribute it and/or
35 *  modify it under the terms of the GNU Library General Public
36 *  License as published by the Free Software Foundation; only
37 *  Version 2 of the License dated June 1991.
38 *
39 *  This library is distributed in the hope that it will be useful,
40 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
41 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
42 *  Library General Public License for more details.
43 *
44 *  You should have received a copy of the GNU Library General Public
45 *  License along with this library; if not, write to the Free
46 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
47 *
48 *
49 *  The BSD License
50 *  ===============
51 *  Redistribution and use in source and binary forms, with or without
52 *  modification, are permitted provided that the following conditions
53 *  are met:
54 *
55 *  1. Redistributions of source code must retain the above copyright
56 *     notice, this list of conditions and the following disclaimer.
57 *  2. Redistributions in binary form must reproduce the above copyright
58 *     notice, this list of conditions and the following disclaimer in
59 *     the documentation and/or other materials provided with the
60 *     distribution.
61 *  3. Neither the name of OpenLink Software Inc. nor the names of its
62 *     contributors may be used to endorse or promote products derived
63 *     from this software without specific prior written permission.
64 *
65 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
66 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
67 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
68 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL OPENLINK OR
69 *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
70 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
71 *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
72 *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
73 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
74 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
75 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
76 */
77
78#ifndef	_DLF_H
79#define _DLF_H
80#include <iodbc.h>
81
82#if defined(HAVE_SHL_LOAD)
83#define DLDAPI_HP_SHL
84#elif defined(HAVE_LIBDL)
85#define DLDAPI_SVR4_DLFCN
86#elif defined(HAVE_DYLD)
87#define DLDAPI_MACX
88#endif
89
90#if defined(DLDAPI_SVR4_DLFCN)
91#include <dlfcn.h>
92#elif defined(DLDAPI_AIX_LOAD)
93#include <dlfcn.h>
94#elif defined(DLDAPI_VMS_IODBC) || defined(DLDAPI_MACX)
95extern void *iodbc_dlopen (char * path, int mode);
96extern void *iodbc_dlsym (void * hdll, char * sym);
97extern char *iodbc_dlerror ();
98extern int iodbc_dlclose (void * hdll);
99#else
100extern void *dlopen (char * path, int mode);
101extern void *dlsym (void * hdll, char * sym);
102extern char *dlerror ();
103extern int dlclose (void * hdll);
104#endif
105
106
107#ifdef DLDAPI_MACX
108#include <stdio.h>
109#include <stdlib.h>
110#include <string.h>
111#include <errno.h>
112#include <sys/types.h>
113#include <sys/stat.h>
114#include "mach-o/dyld.h"
115
116#define RTLD_LAZY		0x1
117#define RTLD_NOW		0x2
118#define RTLD_LOCAL		0x4
119#define RTLD_GLOBAL		0x8
120#define RTLD_NOLOAD		0x10
121#define RTLD_SHARED		0x20	/* not used, the default */
122#define RTLD_UNSHARED		0x40
123#define RTLD_NODELETE		0x80
124#define RTLD_LAZY_UNDEF		0x100
125
126
127enum ofile_type
128{
129  OFILE_UNKNOWN,
130  OFILE_FAT,
131  OFILE_ARCHIVE,
132  OFILE_Mach_O
133};
134
135enum byte_sex
136{
137  UNKNOWN_BYTE_SEX,
138  BIG_ENDIAN_BYTE_SEX,
139  LITTLE_ENDIAN_BYTE_SEX
140};
141
142
143/*
144 * The structure describing an architecture flag with the string of the flag
145 * name, and the cputype and cpusubtype.
146 */
147struct arch_flag
148{
149  char *name;
150  cpu_type_t cputype;
151  cpu_subtype_t cpusubtype;
152};
153
154/*
155 * The structure used by ofile_*() routines for object files.
156 */
157struct ofile
158{
159  char *file_name;		   /* pointer to name malloc'ed by ofile_map */
160  char *file_addr;		   /* pointer to vm_allocate'ed memory       */
161  unsigned long file_size;	   /* size of vm_allocate'ed memory          */
162  enum ofile_type file_type;	   /* type of the file                       */
163
164  struct fat_header *fat_header;   /* If a fat file these are filled in and  */
165  struct fat_arch *fat_archs;	   /*   if needed converted to host byte sex */
166
167  /*
168   *  If this is a fat file then these are valid and filled in
169   */
170  unsigned long narch;		   /* the current architecture               */
171  enum ofile_type arch_type;	   /* the type of file for this arch.        */
172  struct arch_flag arch_flag;	   /* the arch_flag for this arch, the name  */
173				   /*   field is pointing at space malloc'ed */
174				   /*   by ofile_map.                        */
175
176  /*
177   *  If this structure is currently referencing an archive member or
178   *  an object file that is an archive member these are valid and filled in.
179   */
180  unsigned long member_offset;	   /* logical offset to the member starting  */
181  char *member_addr;		   /* pointer to the member contents         */
182  unsigned long member_size;	   /* actual size of the member (not rounded)*/
183  struct ar_hdr *member_ar_hdr;	   /* pointer to the ar_hdr for this member  */
184  char *member_name;		   /* name of this member                    */
185  unsigned long member_name_size;  /* size of the member name                */
186  enum ofile_type member_type;	   /* the type of file for this member       */
187  cpu_type_t archive_cputype;	   /* if the archive contains objects then   */
188   cpu_subtype_t		   /*   these two fields reflect the object  */
189   archive_cpusubtype;		   /*   at are in the archive.               */
190
191  /*
192   *  If this structure is currently referencing a dynamic library module
193   *  these are valid and filled in.
194   */
195  struct dylib_module *modtab;	   /* the module table                       */
196  unsigned long nmodtab;	   /* the number of module table entries     */
197  struct dylib_module		   /* pointer to the dylib_module for this   */
198      *dylib_module;		   /*   module                               */
199  char *dylib_module_name;	   /* the name of the module                 */
200
201  /*
202   *  If this structure is currently referencing an object file these are
203   *  valid and filled in.  The mach_header and load commands have been
204   *  converted to the host byte sex if needed
205   */
206  char *object_addr;		   /* the address of the object file         */
207  unsigned long object_size;	   /* the size of the object file            */
208  enum byte_sex object_byte_sex;   /* the byte sex of the object file        */
209  struct mach_header *mh;	   /* the mach_header of the object file     */
210  struct load_command		   /* the start of the load commands         */
211      *load_commands;
212};
213
214
215/*
216 * The structure of a dlopen() handle.
217 */
218struct dlopen_handle
219{
220  dev_t dev;		/* the path's device and inode number from stat(2) */
221  ino_t ino;
222  int dlopen_mode;	/* current dlopen mode for this handle */
223  int dlopen_count;	/* number of times dlopen() called on this handle */
224  NSModule module;	/* the NSModule returned by NSLinkModule() */
225  struct dlopen_handle *prev;
226  struct dlopen_handle *next;
227};
228#endif /* DLDAPI_MACX */
229
230#ifndef RTLD_LOCAL
231#define RTLD_LOCAL	0	/* Only if not defined by dlfcn.h */
232#endif
233#ifndef RTLD_LAZY
234#define RTLD_LAZY	1
235#endif
236
237#ifdef RTLD_NOW
238#define OPL_DL_MODE	(RTLD_NOW | RTLD_LOCAL)
239#else
240#define OPL_DL_MODE	(RTLD_LAZY | RTLD_LOCAL)
241#endif
242
243#if defined(DLDAPI_VMS_IODBC) || defined(DLDAPI_MACX)
244#define	DLL_OPEN(dll)		(void*)iodbc_dlopen((char*)(dll), OPL_DL_MODE)
245#define	DLL_PROC(hdll, sym)	(void*)iodbc_dlsym((void*)(hdll), (char*)sym)
246#define	DLL_ERROR()		(char*)iodbc_dlerror()
247#define	DLL_CLOSE(hdll)		iodbc_dlclose((void*)(hdll))
248#else
249#define	DLL_OPEN(dll)		(void*)dlopen((char*)(dll), OPL_DL_MODE)
250#define	DLL_PROC(hdll, sym)	(void*)dlsym((void*)(hdll), (char*)sym)
251#define	DLL_ERROR()		(char*)dlerror()
252#define	DLL_CLOSE(hdll)		dlclose((void*)(hdll))
253#endif
254
255#endif /* _DLF_H */
256