🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gcc.help
Article #49592

Re: C++ explicit dynamic linking?

#49592
From: a904209
Date: Mon, 10 Jan 1994 09:35
86 lines
2593 bytes
On SunOS I have used the following to load g++ code,
If you don't want to unload (dlclose) and still need destructors called
you need to use an on_exit() scheme like in libgcc2.c - __main

Note that you "link" the loadable modules with gcc so that "collect2"
builds tables :

   gcc -nostartfiles -nostdlibs -o whatever xxxx.o -lg++ -lgcc

Sadly as libg++ and libgcc and not shared you have to link to them
in case caller has not included routines you need.

Here is the code


#include <stdio.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "externC.h"

typedef int (*entry_p)(int,char **,char **);

externC void *dlopen(char *,int);
externC void *dlsym(void *,char *);
externC int  dlclose(void *);
externC char *dlerror(void);

typedef void (*cons_p)(void);

void C_or_D_tor(void *obj,char *sym)
{cons_p *list = dlsym(obj,sym);
 if (list)
  {unsigned count = (unsigned) (*list++);
   fprintf(stderr,"%s has %u entries\n",sym,count);
   while (count--)
    {
     (*list++)();
    }
   if (*list)
    abort();
  }
 else
  fprintf(stderr,"No %s\n",sym);
}
                                                            
int 
load_it(char *prog,char *sym,int argc,char *argv[],char *env[])
{
 void *obj = dlopen(prog,1);
 int code  = 1;
 if (obj)
  {entry_p proc = (entry_p) dlsym(obj,sym);
   if (proc)
    {
     C_or_D_tor(obj,"___CTOR_LIST__");
     code = (*proc)(argc,argv,env);
     C_or_D_tor(obj,"___DTOR_LIST__");
    }
   dlclose(obj);
  }
 else
  {
   fprintf(stderr,"Could not load %s:%s\n",prog,dlerror());
  }
 return code;
}

int 
main(int argc,char *argv[],char *env[])
{int fd = open("/dev/zero", 0, 7);
 char *prog = strdup(argv[1]);
 if (fd >= 0)
  {
   if (fd != 4)
    {
     dup2(fd, 4);
     close(fd);
    }
  }
 else
  perror("/dev/zero");
 return load_it(argv[1],"_main",argc-1,argv+1,env);
}
Technical Details
Message-ID: <CJEqn4.24z@csc.ti.com>
Path: arc1-ca.pugleaf.net!gmd.de!newsserver.jvnc.net!howland.reston.ans.net!cs.utexas.edu!csc.ti.com!tilde.csc.ti.com!cauldron!ra.csc.ti.com!usenet
References: <CJ9y6D.ECM@lab-cdi.es>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 11318c,9eef53718da8ba54
X-Google-Attributes: gid11318c,public
X-Google-ArrivalTime: 1994-01-10 02:29:36 PST
Newsgroups: gnu.gcc.help
Path: arc1-ca.pugleaf.net!gmd.de!newsserver.jvnc.net!howland.reston.ans.net!cs.utexas.edu!csc.ti.com!tilde.csc.ti.com!cauldron!ra.csc.ti.com!usenet
From: a904209@pluto.tiuk.ti.com
Subject: Re: C++ explicit dynamic linking?
Message-ID: <CJEqn4.24z@csc.ti.com>
Sender: usenet@csc.ti.com
Nntp-Posting-Host: pluto.tiuk.ti.com
Organization: Texas Instruments Ltd.
References:  <CJ9y6D.ECM@lab-cdi.es>
Date: Mon, 10 Jan 1994 09:35:27 GMT
Lines: 88