File: dynlink.c

package info (click to toggle)
rscheme 0.7.2-1.1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 10,672 kB
  • ctags: 12,430
  • sloc: lisp: 37,104; ansic: 29,763; cpp: 2,630; sh: 1,677; makefile: 568; yacc: 202; lex: 175; perl: 33
file content (113 lines) | stat: -rw-r--r-- 2,484 bytes parent folder | download | duplicates (4)
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
/*-----------------------------------------------------------------*-C-*---
 * File:    handc/platform/next/dynlink.c
 *
 *          Copyright (C)1997 Donovan Kolbly <d.kolbly@rscheme.org>
 *          as part of the RScheme project, licensed for free use.
 *          See <http://www.rscheme.org/> for the latest information.
 *
 * File version:     1.4
 * File mod date:    1997.11.29 23:10:48
 * System build:     v0.7.2, 97.12.21
 *
 * Purpose:          NeXTSTEP (Mach) dynamic linking interface
 *------------------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
#include <streams/streams.h>
#include <mach-o/rld.h>
#ifdef __cplusplus
}
#endif
#include "schemedl.h"

struct resolved {
    struct resolved *next;
    const char *sym;
};

struct dlfile {
    struct resolved *lookups;
    const char *path;
};

static const char *strdup( const char *str )
{
char *p = (char *)malloc( strlen(str)+1 );

    strcpy( p, str );
    return p;
}

static NXStream *errors = NULL;

void *resolve_link_symbol( void *info, const char *sym )
{
unsigned long val, rc;
struct resolved *i;
struct dlfile *f = (struct dlfile *)info;

    if (info)
    {
	i = (struct resolved *)malloc( sizeof( struct resolved ) );
	i->sym = strdup( sym );
	i->next = f->lookups;
	f->lookups = i;
    }

    if (!errors)
	errors = NXOpenFile( 2, NX_WRITEONLY );

    rc = rld_lookup( errors, sym, &val );
    NXFlush(errors);
    return (rc == 1) ? (void *)val : NULL;
}

void *dynamic_link_file( const char *path )
{
const char *(files[2]) = { path, NULL };
unsigned long rc;

    if (!errors)
	errors = NXOpenFile( 2, NX_WRITEONLY );

    rc = rld_load( errors, NULL, files, NULL );
    NXFlush(errors);
    if (rc == 1)
    {
    struct dlfile *f = (struct dlfile *)malloc( sizeof( struct dlfile ) );
    
	f->lookups = NULL;
        f->path = strdup( path );
	return f;
    }
    return NULL;
}

void done_resolving( void *info )
{
struct dlfile *f = (struct dlfile *)info;
struct resolved *i, *j;

    NXPrintf( errors, "Done with: %s\n", f->path );
    for (i=f->lookups; i; i=j)
    {
	NXPrintf( errors, "    forgetting %s\n", i->sym );
	rld_forget_symbol( errors, i->sym );
	j = i->next;
	free( (void *)i->sym );
	free( (void *)i );
    }
    rld_unload_all( errors, 0 );
    NXFlush( errors );
    free( (void *)f->path );
    free( (void *)f );
}

void init_dynamic_link( const char *argv0 )
{
}