source: git/libpolys/polys/mod_raw.cc @ 76fead

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