source: git/libpolys/polys/mod_raw.cc @ fad7c58

spielwiese
Last change on this file since fad7c58 was fad7c58, checked in by Hans Schoenemann <hannes@…>, 13 years ago
p_Mult_q.h is an internal header, mod_raw.* moved to polys (it is not a template)
  • Property mode set to 100644
File size: 8.0 KB
RevLine 
[35aab3]1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
[341696]4/* $Id$ */
[35aab3]5/*
6 * ABSTRACT: machine depend code for dynamic modules
7 *
8 * Provides: dynl_open()
9 *           dynl_sym()
10 *           dynl_error()
[4c7d73]11 *           dynl_close()
[35aab3]12*/
13
14#include <stdio.h>
[9ed245]15#include <string.h>
16#include <ctype.h>
17#include <sys/stat.h>
18
[35aab3]19
[8c9912]20#include "config.h"
21#include <misc/auxiliary.h>
[35aab3]22
[f9dc520]23#include <omalloc/omalloc.h>
[8c9912]24
25#include <reporter/reporter.h>
26
27#include <resources/feResource.h>
28#include <resources/feFopen.h>
29
[fad7c58]30#include "mod_raw.h"
[35aab3]31
[abaf13]32#ifdef HAVE_STATIC
33#undef HAVE_DL
34#endif
[9ed245]35/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
36#define BYTES_TO_CHECK 7
37
38lib_types type_of_LIB(char *newlib, char *libnamebuf)
39{
[7dce2d7]40  const unsigned char mach_o[]={0xfe,0xed,0xfa,0xce,0};
[05b917]41  const unsigned char mach_O[]={0xce,0xfa,0xed,0xfe,0};
[9ed245]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  }
[1ea9a7]77
[05b917]78  if( (strncmp(buf, (const char *)mach_o, 4)==0) || (strncmp(buf, (const char *)mach_O, 4)==0)) /* generic Mach-O module */
[1ea9a7]79  {
80    LT = LT_MACH_O;
81    //omFree(newlib);
82    //newlib = omStrDup(libnamebuf);
83    goto lib_type_end;
84  }
[27fc50]85
86  if( (strncmp(buf, &mach_o_module[0], 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   
[9ed245]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
[35aab3]112/*****************************************************************************
113 *
114 * General section
[abaf13]115 * These are just wrappers around the repsective dynl_* calls
[35aab3]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
[9ef9d0]126void* dynl_open_binary_warn(const char* binary_name, const char* msg)
[35aab3]127{
128  void* handle = NULL;
129  const char* bin_dir = feGetResource('b');
130  if (bin_dir != NULL)
131  {
[f9dc520]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
[05b917]149    if( type != LT_SINGULAR && type != LT_NONE && type != LT_NOTFOUND )
[f9dc520]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);
[35aab3]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  }
[f9dc520]163
[35aab3]164  return  handle;
165}
166
[85e68dd]167void* dynl_sym_warn(void* handle, const char* proc, const char* msg)
[35aab3]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/*****************************************************************************
[fc300f]190 * SECTION generic ELF: ix86-linux / alpha-linux / IA64-linux /x86_64_Linux  *
[4287a0]191 *                      SunOS-5 / IRIX-6 / ppcMac-Darwin / FreeeBSD          *
[35aab3]192 *****************************************************************************/
[f02645b]193// relying on gcc to define __ELF__, check with cpp -dM /dev/null
[9b3de5]194// Mac OsX is an ELF system, but does not define __ELF__
[c3ab214]195// Solaris is an ELF system, but does not define __ELF__
[d44ced]196#if defined(__ELF__)
[fde597]197#define HAVE_ELF_SYSTEM
[9b3de5]198#endif
199
[0fd6d8]200#if defined(ppcMac_darwin)
201#define HAVE_ELF_SYSTEM
202#endif
203
[d142260]204#if defined(__APPLE__) && defined(__MACH__)
205#define HAVE_ELF_SYSTEM
206#endif
[01d9463]207
[c3ab214]208#if defined(SunOS_5)
209#define HAVE_ELF_SYSTEM
[fde597]210#endif
211
[35aab3]212#if defined(HAVE_ELF_SYSTEM)
213#include <dlfcn.h>
[3dbe0bf]214#define DL_IMPLEMENTED
[35aab3]215
216static void* kernel_handle = NULL;
217void *dynl_open(
218  char *filename    /* I: filename to load */
219  )
220{
[a513d10]221// glibc 2.2:
[a11e71]222  if ((filename==NULL) || (dlopen(filename,RTLD_NOW|RTLD_NOLOAD)==NULL))
[a513d10]223    return(dlopen(filename, RTLD_NOW|RTLD_GLOBAL));
224  else
225    Werror("module %s already loaded",filename);
226  return NULL;
[3dbe0bf]227// alternative
[a513d10]228//    return(dlopen(filename, RTLD_NOW|RTLD_GLOBAL));
[35aab3]229}
230
[85e68dd]231void *dynl_sym(void *handle, const char *symbol)
[35aab3]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}
[fc300f]251#endif /* ELF_SYSTEM */
[35aab3]252
253/*****************************************************************************
254 * SECTION HPUX-9/10                                                         *
255 *****************************************************************************/
256#if defined(HPUX_9) || defined(HPUX_10)
[3dbe0bf]257#define DL_IMPLEMENTED
[35aab3]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
[85e68dd]269void *dynl_sym(void *handle, const char *symbol)
[35aab3]270{
271  func_ptr        f;
[abaf13]272
[35aab3]273  if (handle == DYNL_KERNEL_HANDLE)
274    handle = PROG_HANDLE;
[abaf13]275
[35aab3]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/*****************************************************************************
[fc300f]301 * SECTION generic: dynamic madules not available
[35aab3]302 *****************************************************************************/
[3dbe0bf]303#ifndef DL_IMPLEMENTED
[35aab3]304
305void *dynl_open(char *filename)
306{
307  return(NULL);
308}
309
[85e68dd]310void *dynl_sym(void *handle, const char *symbol)
[35aab3]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}
[fc300f]325#endif
[35aab3]326
327#ifdef __cplusplus
328}
329#endif
330
331#endif /* HAVE_DL */
Note: See TracBrowser for help on using the repository browser.