File: link_SYSV.c

package info (click to toggle)
scilab 4.0-12
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 100,640 kB
  • ctags: 57,333
  • sloc: ansic: 377,889; fortran: 242,862; xml: 179,819; tcl: 42,062; sh: 10,593; ml: 9,441; makefile: 4,377; cpp: 1,354; java: 621; csh: 260; yacc: 247; perl: 130; lex: 126; asm: 72; lisp: 30
file content (634 lines) | stat: -rw-r--r-- 14,654 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/*--------------------------------------------------------------
 * Link version for SYSV machine 
 * Copyright (C) Inria/Enpc 
 *--------------------------------------------------------------*/

#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/file.h>

#include "../stack-def.h"

#ifndef hppa
#include <dlfcn.h>
#else
#include <dl.h>
#endif

#if (defined(sun) && defined(SYSV)) 
#include <unistd.h>
#include <sys/wait.h>
#endif

#if defined(sun)  && !defined(netbsd) && !defined(freebsd)
#if defined(SYSV)
#include <sys/vnode.h>
#include <archives.h>
#else
#define RTLD_NOW 1
#include <dlfcn.h>
#endif
#endif


#if defined(linux) || defined(netbsd) || defined(freebsd)
#include <unistd.h>
#include <sys/wait.h>
#endif 

#if !defined(linux) && !defined(netbsd)  && !defined(freebsd) && !defined(__APPLE__)

#ifndef hppa
#if (defined(sun) && ! defined(SYSV)) 
#else 
#include <sys/mode.h>
#endif 
#endif
#endif 

#if !defined(linux) && !defined(netbsd) && !defined(freebsd)
#ifdef __alpha
#include <c_asm.h>
#define RTLD_GLOBAL 0
#endif
#endif


#ifdef sgi
#define vfork fork
#endif

#if (defined(__alpha) && !defined(netbsd) && !defined(freebsd)) || defined sgi
#include <a.out.h>
#endif

#include <string.h>

#define round(x,s) (((x) + ((s)-1)) & ~((s)-1))
#define Min(x,y)	(((x)<(y))?(x):(y))
#define Max(x,y)	(((x)>(y))?(x):(y))

#define debug C2F(iop).ddt==1

/* extern char *strchr();  */
extern int Use_cpp_code;
extern char * Use_c_cpp;

extern char * get_sci_tmp_dir();

static void Sci_Delsym __PARAMS((int ));
static int Sci_dlopen __PARAMS((char *loaded_files[],int global));
static int Sci_dlsym __PARAMS((char *ename,int  ishared,char * strf));
static int SetArgv  __PARAMS((char *argv[], char *files[],int first,int max,int *err));
static int SetArgv1  __PARAMS((char *argv[], char *files,int first,int max,int *err));
static int CreateCppShared(char **loaded_files, char *tmp_file);
static int CreateShared(char **loaded_files, char *tmp_file);

int Call_shl_load = 0;



/*************************************
 * New version : link entry names 
 *   from new shared lib created with 
 *   files.
 *   return in ilib the number of the shared archive 
 *   or -1 or -5
 *   -1 : the shared archive was not loaded 
 *   -5 : pb with one of the entry point 
 *************************************/

void SciLink(int iflag, int *rhs, int *ilib, char **files, char **en_names, char *strf)
{
  int i;
  if ( iflag == 0 )
    {
      if ( * rhs == 1 ) 
	/* if no entry names are given we try a dl_open with global option*/
	*ilib  = Sci_dlopen(files,1);
      else 
	*ilib  = Sci_dlopen(files,0);
    }
  if ( *ilib  == -1 ) return;
  if ( iflag == 0) sciprint("shared archive loaded\r\n");
  if ( *rhs >= 2) 
    {
      i=0 ;
      while ( en_names[i] != (char *) 0)
	{
	  if ( Sci_dlsym(en_names[i],*ilib,strf) == FAIL) 
	    *ilib=-5;
	  i++;
	}
    }
}

/**************************************
 * return 1 if link accepts multiple file iin one call
 * or 0 elsewhere 
 *************************************/

int LinkStatus(void)
{
  return(1);
}

/**************************************
 * Unlink a shared lib 
 *************************************/

void C2F(isciulink)(integer *i)
{
  /* delete entry points in shared lib *i */
  Sci_Delsym(*i);
  /* delete entry points used in addinter in shared lib *i */
  RemoveInterf(*i);
}


#if defined(hppa)
int main() {}

/**************************************
 * This routine force the call of global constructors when
 * "shl_load" is called, and the call of global destructors
 * when "shl_unload" is called (ONLY on HPUX systems). 
 *************************************/

void call_ctor_dtor(handle,loading)
     shl_t handle;
     int loading;
{
  void (*call_sym)();

  if (loading)
    {
      if (shl_findsym(&handle,"_GLOBAL__DI",TYPE_PROCEDURE,
		      (void *) &call_sym))
	{
	  sciprint("No global constructor(s) call\r\n");
	  return;
	}
    }
  else
    {
      if (shl_findsym(&handle,"_GLOBAL__DD",TYPE_PROCEDURE,
		      (void *) &call_sym))
	{
	  sciprint("No global destructor(s) call\n");
	  return;
	}
    }

  (*call_sym)();
}
#endif

/*************************************
 * This routine 
 *   changes the file list <<loaded_files>>
 *   to a shared archive and call dlopen 
 *   the shared lib handler is stored in a Table 
 *   The return value is == -1 if the dlopen failed 
 *************************************/

#define MAXARGV 128



#ifndef hppa
#define SHARED_SUF ".so"
#else
#define SHARED_SUF ".sl"
#endif

#ifndef RTLD_GLOBAL
#define RTLD_GLOBAL 0
#endif



static int Sci_dlopen(char **loaded_files,int global)
{
  int i=0;
#ifndef hppa
  void *hd1 = (void *) 0;
#else
  shl_t hd1;
  void (*init_ctor)() = 0;
#endif
  char tmp_file[TMPL],*getenv(const char *);
  /** XXXXX **/
  if ( strncmp(loaded_files[0],"scilab",6) !=0)
    {
      if ( loaded_files[0] != NULL && loaded_files[1] == NULL 
	   && strstr(loaded_files[0],SHARED_SUF)!= NULL)
	{
	  strcpy(tmp_file,loaded_files[0]);
	  if (debug) sciprint_nd("Loading shared executable %s\r\n",loaded_files[0]);
	}
      else 
	{
	  int rep;
	  if (Use_cpp_code)
	    rep=CreateCppShared(loaded_files,tmp_file);
	  else
	    rep=CreateShared(loaded_files,tmp_file);
	  if (rep == -1) 
	    return(-1);
	}
      /* this will load the shared library */
#ifndef hppa
      Call_shl_load = 0;
      if ( global == 1) 
	{
	  /* sciprint("Using global\r\n"); */ 
	  hd1 = dlopen(tmp_file, RTLD_NOW | RTLD_GLOBAL );
	}
      else 
	hd1 = dlopen(tmp_file, RTLD_NOW  );
#else
      Call_shl_load = 1;
      hd1 = shl_load(tmp_file, BIND_IMMEDIATE | BIND_VERBOSE,0L);
#endif
    }
  else
    {
      /* use symbols from scilab */
      /* XXXXXX Only on sun  */
#if defined(sun) && !defined(netbsd) && !defined(freebsd)
      hd1 = dlopen((char *)0, RTLD_NOW);
#else
#ifdef hppa
      hd1 = PROG_HANDLE;
#else
      sciprint("Link : scilab is not a valid first argument on your machine\r\n");
      return(-1);
#endif
#endif
    }
  /* this will load the shared library */
#ifndef hppa
  if ( hd1 == (void *) NULL || hd1 < (void *) 0 ) {
    sciprint("%s\r\n",dlerror());
#else
  if (  hd1 == NULL) {
    sciprint("link error\r\n");
#endif
    return(-1);
  }
  for ( i = 0 ; i < Nshared ; i++ ) 
    {
      if ( hd[i].ok == FAIL) 
	{
	  hd[i].shl =  (unsigned long)hd1;
	  strcpy(hd[i].tmp_file,tmp_file);
	  hd[i].ok = OK;
	  return(i);
	}
    }
  
  if ( Nshared == ENTRYMAX ) 
    {
      sciprint("You can't open shared files maxentry %d reached\r\n",ENTRYMAX);
      return(FAIL);
    }

  strcpy(hd[Nshared].tmp_file,tmp_file);
  hd[Nshared].shl = (unsigned long)hd1;
  hd[Nshared].ok = OK;
  Nshared ++;
  return(Nshared-1);
}

/** creates a shared executable from the set of files **/
  
static int CreateShared(char **loaded_files, char *tmp_file)
{
  int argc=3,err=0;
  static int count=0;
  int i=0;
  char *libs,*tmpdir;
  libs=getenv("SYSLIBS");
   /** XXXXX **/
  if (debug) sciprint_nd("linking files ");
  while ( loaded_files[i] != NULL) 
    {
      if (debug) sciprint_nd("%s ",loaded_files[i]);
      i++;
    }

  if (debug) sciprint_nd(" to create a shared executable\r\n");
  count++;
  /* be sure that tmpdir exists */
  tmpdir = get_sci_tmp_dir();
  sprintf(tmp_file, "%s/SL_%d_XXXXXX",tmpdir,count);
  
  mktemp(tmp_file);
  {
    int pid, status, wpid;
    static char *argv[MAXARGV] = {
      /*   0        1         2    3  4   */
#if defined(sun) && !defined(netbsd) && !defined(freebsd)
#if defined(SYSV)
      "/usr/ucb/ld", "-r", "-o", 0, 0
#else 
      "/usr/bin/ld", "-o", 0, 0,0 
#endif
#else
#ifdef linux
      "/usr/bin/ld", "-shared", "-o", 0, 0  
#else
#ifdef hppa
      "/bin/ld", "-b", "-o", 0, 0
#else
#if defined(netbsd) || defined(freebsd)
      "/usr/bin/ld", "-shared", "-o", 0, 0  
#else
      "/bin/ld", "-shared", "-o", 0, 0  
#endif
#endif
#endif
#endif
    };
#if (defined(sun) && !defined(SYSV) && !defined(netbsd) && !defined(freebsd)) 
     argc = 2;
#endif
     argv[argc] = tmp_file; argc++;
     argc = SetArgv(argv,loaded_files,argc,MAXARGV,&err);
     if ( err == 1 ) return(-1);
     if (libs) 
       {
	 argc = SetArgv1(argv,libs,argc,MAXARGV,&err);
	 if ( err == 1 ) return(-1);
       }
     argv[argc] = (char *) 0;

#ifdef DEBUG
     for ( i=0 ; i < argc ; i++) 
       sciprint("arg[%d]=%s\r\n",i,argv[i]);
#endif	
     
     if ((pid = vfork()) == 0) {
       execv(argv[0], argv);
       _exit(1);
     }
     if (pid < 0) {
       sciprint("can't create new process: \r\n");
       return(-1);
     }
     while ((wpid = wait(&status)) != pid)
       if (wpid < 0) {
	 sciprint("no child !\r\n");
	 return(-1);
       }
     if (status != 0) {
       sciprint("ld returned bad status: %x\r\n", status);
       return(-1);
     }
   }

   return 0;
}

/** creates a shared executable from the set of C++ files **/

static int CreateCppShared(char **loaded_files, char *tmp_file)
{
  int argc=3,err=0;
  static int count=0;
  int i=0;
  char *libs, *exec_cpp, *tmpdir;
  libs=getenv("SYSLIBS");
   /** XXXXX **/
  if (debug) sciprint_nd("linking files ");
  while ( loaded_files[i] != NULL) 
    {
      if (debug) sciprint_nd("%s ",loaded_files[i]);
      i++;
    }

  if (debug) sciprint_nd(" to create a shared executable\r\n");
  count++;
  tmpdir = get_sci_tmp_dir();
  sprintf(tmp_file, "%s/SL_%d_XXXXXX",tmpdir,(int) getpid()); /*,count);*/
  
  mktemp(tmp_file);
  {
    int pid, status, wpid;
    static char *argv[MAXARGV];
    argv[0] = "sc_cpp";
    argv[1] = (char *) malloc((strlen(Use_c_cpp)+1)*sizeof(char));
    strcpy(argv[1],Use_c_cpp);
    argv[2] = "-o";
    argv[argc] = tmp_file; argc++;
    argc = SetArgv(argv,loaded_files,argc,MAXARGV,&err);
    if ( err == 1 ) return(-1);
    if (libs) 
      {
	argc = SetArgv1(argv,libs,argc,MAXARGV,&err);
	if ( err == 1 ) return(-1);
      }
    argv[argc] = (char *) 0;
    
#ifdef DEBUG
     for ( i=0 ; i < argc ; i++) 
       sciprint("arg[%d]=%s\r\n",i,argv[i]);
#endif	
     exec_cpp = (char *) malloc((strlen(getenv("SCI")) + 16) * 
			     sizeof(char));
     strcpy(exec_cpp,getenv("SCI"));
     strcat(exec_cpp,"/scripts/sc_cpp");

     if ((pid = vfork()) == 0) {
       execv(exec_cpp,argv);
       _exit(1);
     }
     if (pid < 0) {
       sciprint("can't create new process: \r\n");
       return(-1);
     }
     while ((wpid = wait(&status)) != pid)
       if (wpid < 0) {
	 sciprint("no child !\r\n");
	 return(-1);
       }
     if (status != 0) {
       sciprint("collect2 returned bad status: %x\r\n", status);
       return(-1);
     }
  }

   return 0;
}


/*************************************
 * This routine load the entryname ename 
 *     from shared lib ishared 
 * return FAIL or OK 
 *************************************/

static int Sci_dlsym(char *ename, int ishared, char *strf)
{
#ifdef hppa
  shl_t hd1;
  int irep = 0;
#endif
  int ish = Min(Max(0,ishared),ENTRYMAX-1);
  char enamebuf[MAXNAME];
  if ( strf[0] == 'f' )
    Underscores(1,ename,enamebuf);
  else 
    Underscores(0,ename,enamebuf);

  /* lookup the address of the function to be called */
  if ( NEpoints == ENTRYMAX ) 
    {
      sciprint("You can't link more functions maxentry %d reached\r\n",ENTRYMAX);
      return(FAIL);
    }
  if ( hd[ish].ok == FAIL ) 
    {
      sciprint("Shared lib %d does not exists\r\n",ish);
      return(FAIL);
    }
  /** entry was previously loaded **/
  if ( SearchFandS(ename,ish) >= 0 ) 
    {
      sciprint("Entry name %s is already loaded from lib %d\r\n",ename,ish);
      return(OK);
    }
#ifndef hppa
  EP[NEpoints].epoint = (function) dlsym((void *) hd[ish].shl, enamebuf);
  if ( (unsigned long) EP[NEpoints].epoint == (unsigned long) 0 )

#else
  hd1 = (shl_t)  hd[ish].shl;
  irep= shl_findsym(&hd1, enamebuf,TYPE_PROCEDURE,&(EP[NEpoints].epoint));
  if ( irep == -1 )
#endif
    {
#ifdef linux 
      const char *loc;
#else
      char *loc;
#endif
      sciprint("%s is not an entry point \r\n",enamebuf);
#ifndef hppa
      loc = dlerror();
      if ( loc != NULL) sciprint("%s \r\n",loc);
#else
      sciprint("link error\r\n");
#endif
      return(FAIL);
    }
  else 
    {
      /* we don't add the _ in the table */
      if (debug) sciprint("Linking %s \r\n",ename);
      strncpy(EP[NEpoints].name,ename,MAXNAME);
      EP[NEpoints].Nshared = ish;
      NEpoints++;
    }
  return(OK);  
}


/***************************************************
 * Delete entry points associated with shared lib ishared
 * then delete the shared lib 
 ****************************************************/


static void Sci_Delsym(int ishared)
{
  int ish = Min(Max(0,ishared),ENTRYMAX-1);
  int i=0;
  for ( i = NEpoints-1 ; i >=0 ; i--) 
    {
      if ( EP[i].Nshared == ish )
	{
	  int j;
	  for ( j = i ; j <= NEpoints - 2 ; j++ )
	    {
	      EP[j].epoint = EP[j+1].epoint;
	      EP[j].Nshared = EP[j+1].Nshared;
	      strcpy(EP[j].name,EP[j+1].name);
	    }
	  NEpoints--;
	}
    }
  if ( hd[ish].ok != FAIL)
    {
#ifndef hppa
      dlclose((void *) hd[ish].shl);
#else
      shl_unload((shl_t)  hd[ish].shl);
#endif
      /* Warning : two cases are possible here 
       *  the .so archive was created by the user and we must no delete it 
       *  or the .so archive was built during link call and we must delete it. 
       *  if the shared object .so name begins with "/tmp/SD_......" we assume 
       *  that we are in the second case 
       */
      if ( strncmp(hd[ish].tmp_file,"/tmp/SD_",8)==0) 
	{
	  unlink(hd[ish].tmp_file);
	}
      hd[ish].ok = FAIL;
    }
}

  
/******************************************************
 * Utility function 
 * files is a null terminated array of char pointers 
 * files[i] is broken into pieces ( separated by ' ') 
 * and positions are stored in argv starting at position 
 * first 
 *******************************************************/

static int SetArgv(char **argv, char **files, int first, int max, int *err)
{
  int i=0,j=first;
  *err=0;
  while ( files[i] != (char *) NULL) 
    {
      j= SetArgv1(argv,files[i],j,max,err);
      if (*err == 1) return(j);
      i++;
    }
  return(j);
}

static int SetArgv1(char **argv, char *files, int first, int max, int *err)
{
  int j=first;
  char *loc = files;
  while ( *loc == ' ' && *loc != '\0'  ) loc++;
  while ( *loc != '\0' )
    {
      argv[j] = loc; j++;
      if ( j == max ) 
	{
	  sciprint("Link too many files \r\n");
	  *err=1;
	  break;
	}
      if ( ( loc  = strchr(loc, ' ')) != (char *) 0) 
	{ 
	  *loc = 0;	loc++;
	  while ( *loc == ' ' && *loc != '\0'  ) loc++;
	}
      else
	{
	  break;
	}
    }
  return(j);
}