[project @ 2001-01-17 15:11:04 by simonmar]
[ghc-hetmet.git] / ghc / interpreter / object.h
1
2 /* --------------------------------------------------------------------------
3  * Machinery for dynamic loading and linking of object code.  Should be 
4  * completely independent from the rest of Hugs so we can use it in
5  * other applications if desired.
6  *
7  * The Hugs 98 system is Copyright (c) Mark P Jones, Alastair Reid, the
8  * Yale Haskell Group, and the Oregon Graduate Institute of Science and
9  * Technology, 1994-1999, All rights reserved.  It is distributed as
10  * free software under the license in the file "License", which is
11  * included in the distribution.
12  *
13  * ------------------------------------------------------------------------*/
14
15 #ifndef __HUGS_OBJECT_H
16 #define __HUGS_OBJECT_H
17
18 /* An entry in a very crude object symbol table */
19 typedef struct { char* nm; void* ad; } 
20    OSym;
21
22
23 /* Indication of section kinds for loaded objects.  Needed by
24    the GC for deciding whether or not a pointer on the stack
25    is a code pointer.
26 */
27 typedef enum { HUGS_SECTIONKIND_CODE_OR_RODATA,
28                HUGS_SECTIONKIND_RWDATA,
29                HUGS_SECTIONKIND_OTHER,
30                HUGS_SECTIONKIND_NOINFOAVAIL } 
31    OSectionKind;
32
33 typedef struct { void* start; void* end; OSectionKind kind; } 
34    OSection;
35
36
37 /* Indication of the status of an ObjectCode structure.
38    NOTINUSE  -- currently unused.
39    OIMAGE    -- object image is in memory, but that's all.
40    VERIFIED  -- OIMAGE + the loaded image has been verified as 
41                 a valid object file.
42    HAVENAMES -- VERIFIED + names *defined* in this image have been 
43                 extracted from the image and placed in the oTab, 
44                 and also section info placed in sectionTab.
45    RESOLVED  -- HAVENAMES + all names *used* in this image have
46                 successfully been resolved.
47     
48 */
49 typedef enum { OBJECT_NOTINUSE,
50                OBJECT_OIMAGE,
51                OBJECT_VERIFIED,
52                OBJECT_HAVENAMES,
53                OBJECT_RESOLVED }
54    OStatus;
55
56
57 /* Top-level structure for an object module.  One of these is allocated
58    for each object file in use.  This should really be an abstract type
59    to clients.
60 */
61 typedef
62    struct __ObjectCode {
63       OStatus   status;
64       char*     objFileName;
65       int       objFileSize;
66       char*     formatName;            /* eg "ELF32", "DLL", "COFF", etc. */
67
68       /* proc to call to deliver an error message to the client. */
69       void      (*errMsg)(char*);
70
71       /* proc to call to resolve symbols not defined in this module, 
72          when asked to resolve symbols in this module (in ocResolve) */
73       void*     (*clientLookup)(char*);
74
75       /* proc used during ocGetNames to ask client if it wants to
76          acquire a given symbol from the obj file. */
77       int       (*clientWantsSymbol)(char*);
78
79       /* ptr to malloc'd lump of memory holding the obj file */
80       void*     oImage;
81
82       /* ptr to object symbol table; lives in mallocville.  
83          Dynamically expands. */
84       OSym*     oTab;
85       int       sizeoTab;
86       int       usedoTab;
87
88       /* The section-kind entries for this object module.  
89          Dynamically expands. */    
90       OSection* sectionTab;
91       int       sizesectionTab;
92       int       usedsectionTab;        
93
94       /* Allow a chain of these things */
95       struct __ObjectCode * next;
96    }
97    ObjectCode;
98
99
100 /* The API */
101 extern ObjectCode*  ocNew ( void   (*errMsg)(char*),
102                             void*  (*clientLookup)(char*),
103                             int    (*clientWantsSymbol)(char*),
104                             char*  objFileName,
105                             int    objFileSize );
106                             
107 extern int /*Bool*/ ocLoadImage     ( ObjectCode* oc, int verb );
108 extern int /*Bool*/ ocVerifyImage   ( ObjectCode* oc, int verb );
109 extern int /*Bool*/ ocGetNames      ( ObjectCode* oc, int verb );
110 extern int /*Bool*/ ocResolve       ( ObjectCode* oc, int verb );
111 extern void         ocFree          ( ObjectCode* oc );
112
113 extern void*        ocLookupSym     ( ObjectCode* oc, char* sym );
114 extern char*        ocLookupAddr    ( ObjectCode* oc, void* addr );
115 extern OSectionKind ocLookupSection ( ObjectCode* oc, void* addr );
116
117 #endif
118
119 /*-------------------------------------------------------------------------*/
120