source: git/kernel/mod_raw.cc @ e40798

spielwiese
Last change on this file since e40798 was e40798, checked in by Michael Brickenstein <bricken@…>, 18 years ago
*bricken: i386Mac-darwin git-svn-id: file:///usr/local/Singular/svn/trunk@9120 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 7.6 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: mod_raw.cc,v 1.10 2006-05-09 12:01:11 bricken Exp $ */
5/*
6 * ABSTRACT: machine depend code for dynamic modules
7 *
8 * Provides: dynl_open()
9 *           dynl_sym()
10 *           dynl_error()
11 *           dynl_close()
12*/
13
14#include <stdio.h>
15#include <string.h>
16#include <ctype.h>
17#include <sys/stat.h>
18
19
20#include "mod2.h"
21#include "static.h"
22#ifdef HAVE_STATIC
23#undef HAVE_DL
24#endif
25
26#include "mod_raw.h"
27#include "febase.h"
28
29/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
30#define BYTES_TO_CHECK 7
31
32lib_types type_of_LIB(char *newlib, char *libnamebuf)
33{
34  const char mach_o[]={0xfe,0xed,0xfa,0xce,0};
35  char        buf[BYTES_TO_CHECK+1];        /* one extra for terminating '\0' */
36  struct stat sb;
37  int nbytes = 0;
38  int ret;
39  lib_types LT=LT_NONE;
40
41  FILE * fp = feFopen( newlib, "r", libnamebuf, FALSE );
42  ret = stat(libnamebuf, &sb);
43
44  if (fp==NULL)
45  {
46    return LT_NOTFOUND;
47  }
48  if((sb.st_mode & S_IFMT) != S_IFREG)
49  {
50    goto lib_type_end;
51  }
52  if ((nbytes = fread((char *)buf, sizeof(char), BYTES_TO_CHECK, fp)) == -1)
53  {
54    goto lib_type_end;
55    /*NOTREACHED*/
56  }
57  if (nbytes == 0)
58    goto lib_type_end;
59  else
60  {
61    buf[nbytes++] = '\0';        /* null-terminate it */
62  }
63  if( (strncmp(buf, "\177ELF", 4)==0)) /* generic ELF */
64  {
65    LT = LT_ELF;
66    //omFree(newlib);
67    //newlib = omStrDup(libnamebuf);
68    goto lib_type_end;
69  }
70
71 
72 
73  if( (strncmp(buf, &mach_o[0], 4)==0)) /* generic Mach-O module */
74  {
75    LT = LT_MACH_O;
76    //omFree(newlib);
77    //newlib = omStrDup(libnamebuf);
78    goto lib_type_end;
79  }
80  if( (strncmp(buf, "\02\020\01\016\05\022@", 7)==0))
81  {
82    LT = LT_HPUX;
83    //omFree(newlib);
84    //newlib = omStrDup(libnamebuf);
85    goto lib_type_end;
86  }
87  if(isprint(buf[0]) || buf[0]=='\n')
88  { LT = LT_SINGULAR; goto lib_type_end; }
89
90  lib_type_end:
91  fclose(fp);
92  return LT;
93}
94/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
95#if defined(HAVE_DL)
96
97/*****************************************************************************
98 *
99 * General section
100 * These are just wrappers around the repsective dynl_* calls
101 * which look for the binary in the bin_dir of Singular and ommit warnings if
102 * somethings goes wrong
103 *
104 *****************************************************************************/
105static BOOLEAN warn_handle = FALSE;
106static BOOLEAN warn_proc = FALSE;
107#ifndef DL_TAIL
108#define DL_TAIL "so"
109#endif
110
111void* dynl_open_binary_warn(char* binary_name, const char* msg)
112{
113  void* handle = NULL;
114  const char* bin_dir = feGetResource('b');
115  if (bin_dir != NULL)
116  {
117    char path_name[MAXPATHLEN];
118    sprintf(path_name, "%s%s%s.%s", bin_dir, DIR_SEPP, binary_name, 
119            DL_TAIL);
120    handle = dynl_open(path_name);
121    if (handle == NULL && ! warn_handle)
122    {
123      Warn("Could not open dynamic library: %s", path_name);
124      Warn("Error message from system: %s", dynl_error());
125      if (msg != NULL) Warn("%s", msg);
126      Warn("See the INSTALL section in the Singular manual for details.");
127      warn_handle = TRUE;
128    }
129  }
130  return  handle;
131}
132
133void* dynl_sym_warn(void* handle, char* proc, const char* msg)
134{
135  void *proc_ptr = NULL;
136  if (handle != NULL)
137  {
138    proc_ptr = dynl_sym(handle, proc);
139    if (proc_ptr == NULL && ! warn_proc)
140    {
141      Warn("Could load a procedure from a dynamic library");
142      Warn("Error message from system: %s", dynl_error());
143      if (msg != NULL) Warn("%s", msg);
144      Warn("See the INSTALL section in the Singular manual for details.");
145      warn_proc = TRUE;
146    }
147  }
148  return proc_ptr;
149}
150
151#ifdef __cplusplus
152extern "C" {
153#endif
154
155/*****************************************************************************
156 * SECTION generic ELF: ix86-linux / alpha-linux / IA64-linux /x86_64_Linux  *
157 *                      SunOS-5 / IRIX-6 / ppcMac-Darwin / FreeeBSD          *
158 *****************************************************************************/
159#if defined(ix86_Linux)
160#define HAVE_ELF_SYSTEM
161#endif
162
163#if defined(ix86_Linux_libc5)
164#define HAVE_ELF_SYSTEM
165#endif
166
167#if defined(DecAlpha_Linux)
168#define HAVE_ELF_SYSTEM
169#endif
170
171#if defined(IA64_Linux)
172#define HAVE_ELF_SYSTEM
173#endif
174
175#if defined(x86_64_Linux)
176#define HAVE_ELF_SYSTEM
177#endif
178
179#if defined(ppc_Linux)
180#define HAVE_ELF_SYSTEM
181#endif
182
183#if defined(hppa_Linux)
184#define HAVE_ELF_SYSTEM
185#endif
186
187#if defined(SunOS_5)
188#define HAVE_ELF_SYSTEM
189#endif
190
191#ifdef IRIX_6
192#define HAVE_ELF_SYSTEM
193#endif
194
195#ifdef ppcMac_darwin
196#define HAVE_ELF_SYSTEM
197#endif
198
199#ifdef i386Mac_darwin
200#define HAVE_ELF_SYSTEM
201#endif
202
203#ifdef ix86_freebsd
204#define HAVE_ELF_SYSTEM
205#endif
206
207#if defined(HAVE_ELF_SYSTEM)
208#include <dlfcn.h>
209
210static void* kernel_handle = NULL;
211void *dynl_open(
212  char *filename    /* I: filename to load */
213  )
214{
215  return(dlopen(filename, RTLD_NOW|RTLD_GLOBAL));
216}
217
218void *dynl_sym(void *handle, char *symbol)
219{
220  if (handle == DYNL_KERNEL_HANDLE)
221  {
222    if (kernel_handle == NULL)
223      kernel_handle = dynl_open(NULL);
224    handle = kernel_handle;
225  }
226  return(dlsym(handle, symbol));
227}
228
229int dynl_close (void *handle)
230{
231  return(dlclose (handle));
232}
233
234const char *dynl_error()
235{
236  return(dlerror());
237}
238#endif /* ELF_SYSTEM */
239
240/*****************************************************************************
241 * SECTION HPUX-9/10                                                         *
242 *****************************************************************************/
243#if defined(HPUX_9) || defined(HPUX_10)
244#include <dl.h>
245
246typedef char *((*func_ptr) ());
247
248void *dynl_open(char *filename)
249{
250  shl_t           handle = shl_load(filename, BIND_DEFERRED, 0);
251
252  return ((void *) handle);
253}
254
255void *dynl_sym(void *handle, char *symbol)
256{
257  func_ptr        f;
258 
259  if (handle == DYNL_KERNEL_HANDLE)
260    handle = PROG_HANDLE;
261 
262  if (shl_findsym((shl_t *) & handle, symbol, TYPE_PROCEDURE, &f) == -1)
263  {
264    if (shl_findsym((shl_t *) & handle, symbol, TYPE_UNDEFINED, &f) == -1)
265    {
266      f = (func_ptr) NULL;
267    }
268  }
269  return ((void *)f);
270}
271
272int dynl_close (void *handle)
273{
274  shl_unload((shl_t) handle);
275  return(0);
276}
277
278const char *dynl_error()
279{
280  static char errmsg[] = "shl_load failed";
281
282  return errmsg;
283}
284#endif /* HPUX_9  or HPUX_10 */
285
286/*****************************************************************************
287 * SECTION AIX-4                                                             *
288 *****************************************************************************/
289#ifdef AIX_4
290#define DL_NOT_IMPLEMENTED
291#endif
292
293/*****************************************************************************
294 * SECTION Sun3OS-4                                                          *
295 *****************************************************************************/
296#ifdef Sun3OS_4
297#define DL_NOT_IMPLEMENTED
298#endif
299
300/*****************************************************************************
301 * SECTION SunOS-4                                                         *
302 *****************************************************************************/
303#if defined(SunOS_4)
304#define DL_NOT_IMPLEMENTED
305#endif
306 
307/*****************************************************************************
308 * SECTION generic: dynamic madules not available
309 *****************************************************************************/
310#ifdef DL_NOT_IMPLEMEMENTED
311
312void *dynl_open(char *filename)
313{
314  return(NULL);
315}
316
317void *dynl_sym(void *handle, char *symbol)
318{
319  return(NULL);
320}
321
322int dynl_close (void *handle)
323{
324  return(0);
325}
326
327const char *dynl_error()
328{
329  static char errmsg[] = "support for dynamic loading not implemented";
330  return errmsg;
331}
332#endif
333
334#ifdef __cplusplus
335}
336#endif
337
338#endif /* HAVE_DL */
Note: See TracBrowser for help on using the repository browser.