summaryrefslogtreecommitdiff
path: root/hw/xfree86/loader/loader.h
blob: 307b9bb48a9850025d5568c4786ef95d0d048087 (plain)
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
/* $XFree86: xc/programs/Xserver/hw/xfree86/loader/loader.h,v 1.28 2003/11/06 18:38:14 tsi Exp $ */

/*
 *
 * Copyright 1995-1998 by Metro Link, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Metro Link, Inc. not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Metro Link, Inc. makes no
 * representations about the suitability of this software for any purpose.
 *  It is provided "as is" without express or implied warranty.
 *
 * METRO LINK, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL METRO LINK, INC. BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */
/*
 * Copyright (c) 1997-2001 by The XFree86 Project, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of the copyright holder(s)
 * and author(s) shall not be used in advertising or otherwise to promote
 * the sale, use or other dealings in this Software without prior written
 * authorization from the copyright holder(s) and author(s).
 */

#ifdef HAVE_XORG_CONFIG_H
#include <xorg-config.h>
#endif

#ifndef _LOADER_H
#define _LOADER_H

#include "sym.h"

#if defined(Lynx) && defined(sun)
#define const /**/
#endif
#if (defined(__i386__) || defined(__ix86)) && !defined(i386)
#define i386
#endif
#include <X11/Xosdefs.h>
#include <X11/Xfuncproto.h>
#include <X11/Xmd.h>
/* For LOOKUP definition */
#include "sym.h"
#define LD_UNKNOWN	-1
#define LD_ARCHIVE	0
#define LD_ELFOBJECT	1
#define LD_COFFOBJECT	2
#define LD_XCOFFOBJECT	3
#define LD_AOUTOBJECT   4
#define LD_AOUTDLOBJECT	5
#define LD_ELFDLOBJECT	6
#define LD_PROCESSED_ARCHIVE -1
/* #define UNINIT_SECTION */
#define HANDLE_IN_HASH_ENTRY
/*
 * COFF Section nmumbers
 */
#define N_TEXT       1
#define N_DATA       2
#define N_BSS        3
#define N_COMMENT    4
#define TestFree(a) if (a) { xfree (a); a = NULL; }
#define HASHDIV 10
#define HASHSIZE (1<<HASHDIV)
typedef struct _elf_reloc *ELFRelocPtr;
typedef struct _elf_COMMON *ELFCommonPtr;
typedef struct _coff_reloc *COFFRelocPtr;
typedef struct _coff_COMMON *COFFCommonPtr;
typedef struct AOUT_RELOC *AOUTRelocPtr;
typedef struct AOUT_COMMON *AOUTCommonPtr;

typedef struct _LoaderReloc {
    int modtype;
    struct _LoaderReloc *next;
    COFFRelocPtr coff_reloc;
    ELFRelocPtr elf_reloc;
    AOUTRelocPtr aout_reloc;
} LoaderRelocRec, *LoaderRelocPtr;

typedef struct _loader_item *itemPtr;
typedef struct _loader_item {
    char *name;
    void *address;
    itemPtr next;
    int handle;
    int module;
    itemPtr exports;
#if defined(__powerpc__)
    /*
     * PowerPC file formats require special routines in some circumstances
     * to assist in the linking process. See the specific loader for
     * more details.
     */
    union {
	unsigned short plt[8];	/* ELF */
	unsigned short glink[14];	/* XCOFF */
    } code;
#endif
} itemRec;

/* The following structures provide an interface to GDB (note that GDB
   has copies of the definitions - if you change anything here make
   sure that the changes are also made to GDB */

typedef struct {
    char *name;			/* Name of this symbol */
    unsigned int namelen;	/* Name of this module */
    void *addr;			/* Start address of the .text section */
} LDRCommon, *LDRCommonPtr;

typedef struct x_LDRModuleRec {
    unsigned int version;	/* Version of this struct */
    char *name;			/* Name of this module */
    unsigned int namelen;	/* Length of name */
    void *text;			/* Start address of the .text section */
    void *data;			/* Start address of the .data section */
    void *rodata;		/* Start address of the .rodata section */
    void *bss;			/* Start address of the .bss section */
    LDRCommonPtr commons;	/* List of commmon symbols */
    int commonslen;		/* Number of common symbols */
    struct x_LDRModuleRec *next;	/* Next module record in chain */
} LDRModuleRec, *LDRModulePtr;

extern char DebuggerPresent;
extern LDRModulePtr ModList;
extern LDRCommonPtr ldrCommons;
extern int nCommons;

/*
 * The loader uses loader specific alloc/calloc/free functions that
 * are mapped to either to the regular Xserver functions, or in a couple
 * of special cases, mapped to the C library functions.
 */
#if !defined(PowerMAX_OS) && !(defined(linux) && (defined(__alpha__) || defined(__powerpc__) || defined(__ia64__))) && 0
#define xf86loadermalloc(size) xalloc(size)
#define xf86loaderrealloc(ptr,size) xrealloc(ptr,size)
#define xf86loadercalloc(num,size) xcalloc(num,size)
#define xf86loaderfree(ptr) xfree(ptr)
#define xf86loaderstrdup(ptr) xstrdup(ptr)
#else
/*
 * On Some OSes, xalloc() et al uses mmap to allocate space for large
 * allocation. This has the effect of placing the text section of some
 * modules very far away from the rest which are placed on the heap.
 * Certain relocations are limited in the size of the offsets that can be
 * handled, and this seperation causes these relocation to overflow. This
 * is fixed by just using the C library allocation functions for the loader
 * to ensure that all text sections are located on the heap. OSes that have
 * this problem are:
 *	PowerMAX_OS/PPC
 * 	Linux/Alpha
 * 	Linux/PPC
 *	Linux/IA-64
 */
#define xf86loadermalloc(size) malloc(size)
#define xf86loaderrealloc(ptr,size) realloc(ptr,size)
#define xf86loadercalloc(num,size) calloc(num,size)
#define xf86loaderfree(ptr) free(ptr)
#define xf86loaderstrdup(ptr) strdup(ptr)
#endif

typedef struct _loader *loaderPtr;

/*
 * _loader_funcs hold the entry points for a module format.
 */

typedef void *(*LoadModuleProcPtr) (loaderPtr modrec, int fd, LOOKUP **);
typedef void (*ResolveSymbolsProcPtr) (void *);
typedef int (*CheckForUnresolvedProcPtr) (void *);
typedef char *(*AddressToSectionProcPtr) (void *, unsigned long);
typedef void (*LoaderUnloadProcPtr) (void *);

typedef struct _loader_funcs {
    LoadModuleProcPtr LoadModule;
    ResolveSymbolsProcPtr ResolveSymbols;
    CheckForUnresolvedProcPtr CheckForUnresolved;
    AddressToSectionProcPtr AddressToSection;
    LoaderUnloadProcPtr LoaderUnload;
    LoaderRelocRec pRelocs;	/* type specific relocations */
} loader_funcs;

/* Each module loaded has a loaderRec */
typedef struct _loader {
    int handle;			/* Unique id used to remove symbols from
				 * this module when it is unloaded */
    int module;			/* Unique id to identify compilation units */
    char *name;
    char *cname;
    void *private;		/* format specific data */
    loader_funcs *funcs;	/* funcs for operating on this module */
    loaderPtr next;
} loaderRec;

/* Compiled-in version information */
typedef struct {
    int xf86Version;
    int ansicVersion;
    int videodrvVersion;
    int xinputVersion;
    int extensionVersion;
    int fontVersion;
} ModuleVersions;
extern ModuleVersions LoaderVersionInfo;

extern unsigned long LoaderOptions;

/* Internal Functions */

void LoaderAddSymbols(int, int, LOOKUP *);
void LoaderDefaultFunc(void);
void LoaderDuplicateSymbol(const char *, const int);

#if 0
void LoaderFixups(void);
#endif
void LoaderResolve(void);
int LoaderResolveSymbols(void);
int _LoaderHandleUnresolved(char *, char *);
void LoaderHashAdd(itemPtr);
itemPtr LoaderHashDelete(const char *);
itemPtr LoaderHashFind(const char *);
void LoaderHashTraverse(void *, int (*)(void *, itemPtr));
void LoaderPrintAddress(const char *);
void LoaderPrintItem(itemPtr);
void LoaderPrintSymbol(unsigned long);
void LoaderDumpSymbols(void);
char *_LoaderModuleToName(int);
int _LoaderAddressToSection(const unsigned long, const char **,
			    const char **);
int LoaderOpen(const char *, const char *, int, int *, int *, int *);
int LoaderHandleOpen(int);

/*
 * File interface functions
 */
void *_LoaderFileToMem(int fd, unsigned long offset, int size, char *label);
void _LoaderFreeFileMem(void *addr, int size);
int _LoaderFileRead(int fd, unsigned int offset, void *addr, int size);

/*
 * Relocation list manipulation routines
 */
LoaderRelocPtr _LoaderGetRelocations(void *);

/*
 * object to name lookup routines
 */
char *_LoaderHandleToName(int handle);
char *_LoaderHandleToCanonicalName(int handle);

/*
 * Entry points for the different loader types
 */
#include "aoutloader.h"
#include "coffloader.h"
#include "elfloader.h"
#include "dlloader.h"
/* LD_ARCHIVE */
void *ARCHIVELoadModule(loaderPtr, int, LOOKUP **);

extern void _loader_debug_state(void);

#endif /* _LOADER_H */