source: git/kernel/mod_raw.cc @ f02645b

spielwiese
Last change on this file since f02645b was f02645b, checked in by Hans Schoenemann <hannes@…>, 13 years ago
allow unknown architectures (#299) git-svn-id: file:///usr/local/Singular/svn/trunk@13673 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 7.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 <kernel/mod2.h>
21#include <Singular/static.h>
22
23#include <kernel/mod_raw.h>
24#include <kernel/febase.h>
25
26#ifdef HAVE_STATIC
27#undef HAVE_DL
28#endif
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  if( (strncmp(buf, &mach_o[0], 4)==0)) /* generic Mach-O module */
72  {
73    LT = LT_MACH_O;
74    //omFree(newlib);
75    //newlib = omStrDup(libnamebuf);
76    goto lib_type_end;
77  }
78  if( (strncmp(buf, "\02\020\01\016\05\022@", 7)==0))
79  {
80    LT = LT_HPUX;
81    //omFree(newlib);
82    //newlib = omStrDup(libnamebuf);
83    goto lib_type_end;
84  }
85  if(isprint(buf[0]) || buf[0]=='\n')
86  { LT = LT_SINGULAR; goto lib_type_end; }
87
88  lib_type_end:
89  fclose(fp);
90  return LT;
91}
92/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
93#if defined(HAVE_DL)
94
95/*****************************************************************************
96 *
97 * General section
98 * These are just wrappers around the repsective dynl_* calls
99 * which look for the binary in the bin_dir of Singular and ommit warnings if
100 * somethings goes wrong
101 *
102 *****************************************************************************/
103static BOOLEAN warn_handle = FALSE;
104static BOOLEAN warn_proc = FALSE;
105#ifndef DL_TAIL
106#define DL_TAIL "so"
107#endif
108
109void* dynl_open_binary_warn(const char* binary_name, const char* msg)
110{
111  void* handle = NULL;
112  const char* bin_dir = feGetResource('b');
113  if (bin_dir != NULL)
114  {
115    char path_name[MAXPATHLEN];
116    sprintf(path_name, "%s%s%s.%s", bin_dir, DIR_SEPP, binary_name,
117            DL_TAIL);
118    handle = dynl_open(path_name);
119    if (handle == NULL && ! warn_handle)
120    {
121      Warn("Could not open dynamic library: %s", path_name);
122      Warn("Error message from system: %s", dynl_error());
123      if (msg != NULL) Warn("%s", msg);
124      Warn("See the INSTALL section in the Singular manual for details.");
125      warn_handle = TRUE;
126    }
127  }
128  return  handle;
129}
130
131void* dynl_sym_warn(void* handle, const char* proc, const char* msg)
132{
133  void *proc_ptr = NULL;
134  if (handle != NULL)
135  {
136    proc_ptr = dynl_sym(handle, proc);
137    if (proc_ptr == NULL && ! warn_proc)
138    {
139      Warn("Could load a procedure from a dynamic library");
140      Warn("Error message from system: %s", dynl_error());
141      if (msg != NULL) Warn("%s", msg);
142      Warn("See the INSTALL section in the Singular manual for details.");
143      warn_proc = TRUE;
144    }
145  }
146  return proc_ptr;
147}
148
149#ifdef __cplusplus
150extern "C" {
151#endif
152
153/*****************************************************************************
154 * SECTION generic ELF: ix86-linux / alpha-linux / IA64-linux /x86_64_Linux  *
155 *                      SunOS-5 / IRIX-6 / ppcMac-Darwin / FreeeBSD          *
156 *****************************************************************************/
157// relying on gcc to define __ELF__, check with cpp -dM /dev/null
158#if defined(__ELF__)
159#define HAVE_ELF_SYSTEM
160#endif
161
162#if defined(HAVE_ELF_SYSTEM)
163#include <dlfcn.h>
164
165static void* kernel_handle = NULL;
166void *dynl_open(
167  char *filename    /* I: filename to load */
168  )
169{
170  return(dlopen(filename, RTLD_NOW|RTLD_GLOBAL));
171}
172
173void *dynl_sym(void *handle, const char *symbol)
174{
175  if (handle == DYNL_KERNEL_HANDLE)
176  {
177    if (kernel_handle == NULL)
178      kernel_handle = dynl_open(NULL);
179    handle = kernel_handle;
180  }
181  return(dlsym(handle, symbol));
182}
183
184int dynl_close (void *handle)
185{
186  return(dlclose (handle));
187}
188
189const char *dynl_error()
190{
191  return(dlerror());
192}
193#endif /* ELF_SYSTEM */
194
195/*****************************************************************************
196 * SECTION HPUX-9/10                                                         *
197 *****************************************************************************/
198#if defined(HPUX_9) || defined(HPUX_10)
199#include <dl.h>
200
201typedef char *((*func_ptr) ());
202
203void *dynl_open(char *filename)
204{
205  shl_t           handle = shl_load(filename, BIND_DEFERRED, 0);
206
207  return ((void *) handle);
208}
209
210void *dynl_sym(void *handle, const char *symbol)
211{
212  func_ptr        f;
213
214  if (handle == DYNL_KERNEL_HANDLE)
215    handle = PROG_HANDLE;
216
217  if (shl_findsym((shl_t *) & handle, symbol, TYPE_PROCEDURE, &f) == -1)
218  {
219    if (shl_findsym((shl_t *) & handle, symbol, TYPE_UNDEFINED, &f) == -1)
220    {
221      f = (func_ptr) NULL;
222    }
223  }
224  return ((void *)f);
225}
226
227int dynl_close (void *handle)
228{
229  shl_unload((shl_t) handle);
230  return(0);
231}
232
233const char *dynl_error()
234{
235  static char errmsg[] = "shl_load failed";
236
237  return errmsg;
238}
239#endif /* HPUX_9  or HPUX_10 */
240
241/*****************************************************************************
242 * SECTION AIX-4                                                             *
243 *****************************************************************************/
244#ifdef AIX_4
245#define DL_NOT_IMPLEMENTED
246#endif
247
248/*****************************************************************************
249 * SECTION Sun3OS-4                                                          *
250 *****************************************************************************/
251#ifdef Sun3OS_4
252#define DL_NOT_IMPLEMENTED
253#endif
254
255/*****************************************************************************
256 * SECTION SunOS-4                                                         *
257 *****************************************************************************/
258#if defined(SunOS_4)
259#define DL_NOT_IMPLEMENTED
260#endif
261
262/*****************************************************************************
263 * SECTION generic: dynamic madules not available
264 *****************************************************************************/
265#ifdef DL_NOT_IMPLEMEMENTED
266
267void *dynl_open(char *filename)
268{
269  return(NULL);
270}
271
272void *dynl_sym(void *handle, const char *symbol)
273{
274  return(NULL);
275}
276
277int dynl_close (void *handle)
278{
279  return(0);
280}
281
282const char *dynl_error()
283{
284  static char errmsg[] = "support for dynamic loading not implemented";
285  return errmsg;
286}
287#endif
288
289#ifdef __cplusplus
290}
291#endif
292
293#endif /* HAVE_DL */
Note: See TracBrowser for help on using the repository browser.