OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [services/] [loader/] [v2_0/] [include/] [loader.hxx] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
#ifndef CYGONCE_LOADER_LOADER_HXX
2
#define CYGONCE_LOADER_LOADER_HXX
3
 
4
//==========================================================================
5
//
6
//      loader.hxx
7
//
8
//      ELF dynamic loader definitions
9
//
10
//==========================================================================
11
//####ECOSGPLCOPYRIGHTBEGIN####
12
// -------------------------------------------
13
// This file is part of eCos, the Embedded Configurable Operating System.
14
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
15
//
16
// eCos is free software; you can redistribute it and/or modify it under
17
// the terms of the GNU General Public License as published by the Free
18
// Software Foundation; either version 2 or (at your option) any later version.
19
//
20
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
21
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
22
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
23
// for more details.
24
//
25
// You should have received a copy of the GNU General Public License along
26
// with eCos; if not, write to the Free Software Foundation, Inc.,
27
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
28
//
29
// As a special exception, if other files instantiate templates or use macros
30
// or inline functions from this file, or you compile this file and link it
31
// with other works to produce a work based on this file, this file does not
32
// by itself cause the resulting work to be covered by the GNU General Public
33
// License. However the source code for this file must still be made available
34
// in accordance with section (3) of the GNU General Public License.
35
//
36
// This exception does not invalidate any other reasons why a work based on
37
// this file might be covered by the GNU General Public License.
38
//
39
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
40
// at http://sources.redhat.com/ecos/ecos-license/
41
// -------------------------------------------
42
//####ECOSGPLCOPYRIGHTEND####
43
//==========================================================================
44
//#####DESCRIPTIONBEGIN####
45
//
46
// Author(s):           nickg
47
// Contributors:        nickg
48
// Date:                2000-11-15
49
// Purpose:             Define ELF dynamic loader
50
// Description: The classes defined here collectively implement the
51
//              internal API of the ELF dynamic loader.
52
// Usage:       #include 
53
//
54
//####DESCRIPTIONEND####
55
//
56
//==========================================================================
57
 
58
#include 
59
 
60
#ifndef CYG_LOADER_DYNAMIC_LD
61
 
62
#include            // ELF data structures
63
 
64
#ifdef __cplusplus
65
 
66
#include 
67
#include          // assertion macros
68
 
69
#include 
70
 
71
// -------------------------------------------------------------------------
72
// Forward definitions
73
 
74
class Cyg_LoaderStream;
75
class Cyg_LoaderMemBlock;
76
class Cyg_LoaderMemAlloc;
77
class Cyg_LoadObject_Base;
78
class Cyg_LoadObject_Proc;
79
class Cyg_LoadObject;
80
class Cyg_Loader;
81
 
82
// -------------------------------------------------------------------------
83
// Error codes
84
 
85
#define CYG_LOADERR_NOERROR             0       // No error
86
#define CYG_LOADERR_NOT_ELF             1       // Not ELF format file
87
#define CYG_LOADERR_INVALID_CLASS       2       // Not expected file class
88
#define CYG_LOADERR_INVALID_BYTEORDER   3       // Not expected byte order
89
#define CYG_LOADERR_INVALID_VERSION     4       // Not expected ELF version
90
#define CYG_LOADERR_INVALID_MACHINE     5       // Not expected machine type
91
#define CYG_LOADERR_NO_MEMORY           6       // No memory
92
#define CYG_LOADERR_EOF                 7       // End of input stream
93
#define CYG_LOADERR_SEEK                8       // Cannot seek to stream position
94
#define CYG_LOADERR_INVALID_RELOC       9       // Invalid or unexpected relocation
95
#define CYG_LOADERR_NO_HASHTABLE        10      // No hash table in ELF file
96
#define CYG_LOADERR_NO_SYMTAB           11      // No symbol table in ELF file
97
#define CYG_LOADERR_NO_STRTAB           12      // No srting table in ELF file
98
#define CYG_LOADERR_NO_SYMBOL           13      // Symbol not found
99
 
100
// -------------------------------------------------------------------------
101
// Value for undefined or otherwise invalid symbol addresses.
102
 
103
#define CYG_LOADER_NULLSYMADDR          0
104
 
105
// -------------------------------------------------------------------------
106
// Loader Stream base class.
107
// This defines the interface to data streams that are ELF dynamic executable
108
// files.
109
 
110
class Cyg_LoaderStream
111
{
112
public:
113
 
114
    Cyg_LoaderStream();                         // Constructor
115
 
116
    virtual ~Cyg_LoaderStream();                // Destructor
117
 
118
 
119
    // The following functions are virtual and are supplied by a
120
    // derived class to access to data stream.
121
 
122
    virtual cyg_code get_byte(CYG_BYTE *val); // Get a byte from the stream
123
 
124
    virtual cyg_code get_data(CYG_BYTE *addr, CYG_ADDRWORD size);
125
 
126
    virtual cyg_code seek(CYG_ADDRWORD pos);  // seek to given position
127
 
128
    // The following functions all make use of the virtual functions
129
    // to access the data stream.
130
 
131
    cyg_code get_word16(CYG_WORD16 *val);       // get a 16 bit value
132
 
133
    cyg_code get_word32(CYG_WORD32 *val);       // get a 32 bit value
134
 
135
    cyg_code get_word64(CYG_WORD64 *val);       // get a 64 bit value
136
};
137
 
138
// -------------------------------------------------------------------------
139
// Memory allocation object. All memory allocated by the Loader is described
140
// by one of these.
141
 
142
struct Cyg_LoaderMemBlock
143
    : public Cyg_DNode_T
144
{
145
    void                *address;       // data address
146
    cyg_int32           size;           // block size
147
    cyg_int32           alignment;      // block alignment
148
    Cyg_LoaderMemAlloc  *mem;           // allocator used
149
    CYG_ADDRESS         actual_address; // allocator specific actual address for block
150
    CYG_WORD32          actual_size;    // allocator specific actual size for block
151
 
152
    // Convenience free() function
153
    void free();
154
};
155
 
156
// -------------------------------------------------------------------------
157
// Memory allocator base class
158
// This defines the interface to a memory allocator used by the loader.
159
 
160
class Cyg_LoaderMemAlloc
161
{
162
 
163
public:
164
 
165
    Cyg_LoaderMemAlloc();
166
 
167
    virtual ~Cyg_LoaderMemAlloc();
168
 
169
    // The following functions are virtual so that alternative memory
170
    // allocators may be implemented. The default behaviour of this
171
    // class is to use malloc/realloc/free to support these functions.
172
 
173
    // Allocate memory of the supplied size and alignment.
174
    // size      - size in bytes
175
    // alignment - alignment expressed as a power of 2
176
    //             defaults to 8 byte alignment
177
    virtual Cyg_LoaderMemBlock *alloc( cyg_int32 size,
178
                                       cyg_int32 alignment = 8);
179
 
180
    // Reallocate block
181
    // block    - block to reallocate
182
    // size     - new size in bytes
183
    // alignment - new alignment, -1 if old alignment is to be used.
184
    virtual Cyg_LoaderMemBlock *realloc( Cyg_LoaderMemBlock *block,
185
                                         cyg_int32 size,
186
                                         cyg_int32 alignment = -1);
187
 
188
    // Free a previously allocated memory segment.
189
    virtual void free( Cyg_LoaderMemBlock *block );
190
 
191
};
192
 
193
// -------------------------------------------------------------------------
194
// Cyg_LoaderMemBlock convenience free() function implementation.
195
 
196
inline void Cyg_LoaderMemBlock::free() { mem->free(this); };
197
 
198
// -------------------------------------------------------------------------
199
// A loaded object
200
 
201
class Cyg_LoadObject_Base
202
    : public Cyg_DNode_T
203
{
204
    friend class Cyg_Loader;
205
 
206
protected:
207
    // The following fields come from the constructor arguments
208
 
209
    cyg_uint32          mode;           // mode flags
210
 
211
    Cyg_LoaderMemAlloc  *memalloc;      // Memory allocator
212
 
213
 
214
    // The following fields are derived from the ELF header
215
 
216
    Elf32_Word          e_type;         // File type
217
 
218
    Elf32_Addr          e_entry;        // Executable entry point
219
 
220
 
221
 
222
    // The following fields are derived from the PT_DYNAMIC segment in
223
    // the program header. These fields are named after the DT_ tags
224
    // that their values are derived from.
225
 
226
    Elf32_Word          flags;          // flags from DT_FLAGS
227
 
228
    Elf32_Word          soname;         // name of module, if defined
229
 
230
    Elf_Hash            *hash;          // address of hash table
231
    Elf32_Word          *bucket;        // derived bucket array address
232
    Elf32_Word          *chain;         // derived chain array address
233
 
234
    // String table
235
    unsigned char       *strtab;        // address of table
236
    Elf32_Word          strsize;        // size of table in bytes
237
 
238
    // Symbol table
239
    Elf32_Sym           *symtab;        // address of table
240
    Elf32_Word          syment;         // size of entry
241
 
242
    // PTL and GOT
243
    Elf32_Addr          pltgot;         // address of PLT and/or GOT
244
 
245
    // PLT relocation entries
246
    Elf32_Addr          jmprel;         // address of relocation table
247
    Elf32_Word          pltrelsz;       // size of jmprel table in bytes
248
    Elf32_Word          pltrel;         // type of table entries: DT_REL
249
                                        //   or DT_RELA
250
 
251
    // Relocation table with implicit addends
252
    Elf32_Rel           *rel;           // table address
253
    Elf32_Word          relsize;        // table size in bytes
254
    Elf32_Word          relent;         // size of entry
255
 
256
    // Relocation table with explicit addends
257
    Elf32_Rela          *rela;          // table address
258
    Elf32_Word          relasize;       // table size in bytes
259
    Elf32_Word          relaent;        // size of entry
260
 
261
    // Init and Fini support
262
    Elf32_Addr          init;           // address of init function
263
    Elf32_Addr          fini;           // address of fini function
264
    Elf32_Addr          *init_array;    // address of init array
265
    Elf32_Word          init_array_sz;  // size of init array
266
    Elf32_Addr          *fini_array;    // address of fini array
267
    Elf32_Word          fini_array_sz;  // size of fini array
268
    Elf32_Addr          *pre_init_array;   // address of pre_init array
269
    Elf32_Word          pre_init_array_sz; // size of pre_init array
270
 
271
    Elf32_Dyn           *dynamic;       // address of _DYNAMIC section
272
 
273
    CYG_ADDRESS         base;           // base address used in address and
274
                                        // relocation calcualtions
275
 
276
    Cyg_CList_T segs;      // chain of loaded segments
277
 
278
    cyg_code            error;          // most recent error code
279
 
280
 
281
    // private functions
282
 
283
    void parse_dynamic( Elf32_Dyn *dynamic );
284
 
285
    // Translate a symbol from its symbol table index into its
286
    // actual address
287
 
288
    CYG_ADDRESS get_sym_addr_from_ix( Elf32_Word sym );
289
 
290
    // Get a symbol from this object's symbol table.
291
    Elf32_Sym *get_sym( Elf32_Word ix );
292
 
293
    // Get a name from this object's string table.
294
    char *get_name( Elf32_Word offset );
295
 
296
    // Look up the name in the hash table and return its symbol
297
    // table entry, or NULL if not found.
298
    Elf32_Sym *hash_lookup( const char *name );
299
 
300
 
301
    // Look up the name in the hash table and return its absolute
302
    // address or CYG_LOADER_NULLSYMADDR if it is not found.
303
    CYG_ADDRESS hash_lookup_addr( const char *name );
304
 
305
    // Standard ELF-defined hash function for symbol table.
306
    static unsigned long elf_hash( const unsigned char *name );
307
 
308
public:
309
 
310
    // Constructor - reads and allocates the executable.
311
 
312
    Cyg_LoadObject_Base();
313
 
314
    Cyg_LoadObject_Base( Cyg_LoaderStream& stream,
315
                         cyg_uint32 mode,
316
                         Cyg_LoaderMemAlloc *mem );
317
 
318
    ~Cyg_LoadObject_Base();                          // Destructor
319
 
320
    inline cyg_code get_error() { return error; };
321
 
322
    // Translate a symbol into its address.
323
    void *symbol( const char *name );
324
 
325
    // Start this executable running
326
    cyg_code exec(int argc, char **argv, char **envv);
327
};
328
 
329
#endif // __cplusplus
330
 
331
#endif // CYG_LOADER_DYNAMIC_LD
332
 
333
// -------------------------------------------------------------------------
334
// All the following files should have suitable ifdefs so that only
335
// one actually provides content.
336
 
337
#include         // MIPS ELF support
338
#include          // ARM ELF support
339
#include         // i386 ELF support
340
#include          // PowerPC ELF support
341
//#include        // Sparc ELF support
342
//#include           // SH ELF support
343
 
344
// -------------------------------------------------------------------------
345
 
346
#ifndef CYG_LOADER_DYNAMIC_LD
347
 
348
#ifdef __cplusplus
349
 
350
class Cyg_LoadObject :
351
    public Cyg_LoadObject_Proc
352
{
353
    Cyg_LoaderMemBlock  *block;
354
 
355
public:
356
 
357
    inline Cyg_LoadObject()
358
        : Cyg_LoadObject_Proc()
359
        {
360
        };
361
 
362
    inline Cyg_LoadObject( Cyg_LoaderStream& stream,
363
                    cyg_uint32 mode,
364
                    Cyg_LoaderMemAlloc *mem,
365
                    Cyg_LoaderMemBlock *ablock = NULL)
366
        : Cyg_LoadObject_Proc( stream, mode, mem )
367
        {
368
            block = ablock;
369
        };
370
 
371
    inline ~Cyg_LoadObject() {};
372
 
373
    Cyg_LoaderMemBlock *get_block() { return block; };
374
 
375
    // Apply dynamic relocations to the executable
376
    void relocate();
377
 
378
    // Apply PLT relocations
379
    void relocate_plt();
380
};
381
 
382
// -------------------------------------------------------------------------
383
// Main loader class
384
 
385
class Cyg_Loader
386
{
387
    // current error code.
388
    cyg_code            error;
389
 
390
    // Default memory allocator.
391
    Cyg_LoaderMemAlloc  *mem_default;
392
 
393
    // Load object for main program
394
    Cyg_LoadObject      *main;
395
 
396
    // List of loaded executables, including the main
397
    // program and all libraries.
398
    Cyg_CList_T loadlist;
399
 
400
public:
401
 
402
    Cyg_Loader( Cyg_LoaderMemAlloc *mem);       // Constructor
403
 
404
    ~Cyg_Loader();                              // Destructor
405
 
406
 
407
    // Load an object and all its dependencies.
408
    cyg_code load( Cyg_LoaderStream& stream,
409
                   cyg_uint32 mode,
410
                   Cyg_LoadObject **object);
411
 
412
    // Close an object and remove it from memory
413
    cyg_code close( Cyg_LoadObject *object );
414
 
415
    // Translate current error code into a string.
416
    const char *error_string( );
417
 
418
    // Look up a symbol
419
    Elf32_Sym *hash_lookup( const char *name );
420
 
421
    // look up a named symbol in loadlist and return its
422
    // address relocated to its load address.
423
    CYG_ADDRESS hash_lookup_addr( const char *name );
424
 
425
    // Static pointer to loader object
426
    static Cyg_Loader *loader;
427
};
428
 
429
//==========================================================================
430
// Memory based loader stream
431
 
432
class Cyg_LoaderStream_Mem
433
    : public Cyg_LoaderStream
434
{
435
    CYG_ADDRESS         base;
436
    CYG_ADDRESS         pos;
437
    CYG_ADDRESS         end;
438
 
439
public:
440
 
441
    Cyg_LoaderStream_Mem( const void *addr, cyg_int32 size );
442
 
443
    ~Cyg_LoaderStream_Mem();                // Destructor
444
 
445
    cyg_code get_byte(CYG_BYTE *val); // Get a byte from the stream
446
 
447
    cyg_code get_data(CYG_BYTE *addr, CYG_ADDRWORD size);
448
 
449
    cyg_code seek(CYG_ADDRWORD pos);  // seek to given position
450
 
451
};
452
 
453
//==========================================================================
454
// Fileio based loader stream
455
 
456
#ifdef CYGPKG_IO_FILEIO
457
 
458
class Cyg_LoaderStream_File
459
    : public Cyg_LoaderStream
460
{
461
    int         fd;
462
 
463
public:
464
 
465
    Cyg_LoaderStream_File( int afd );
466
 
467
    ~Cyg_LoaderStream_File();                // Destructor
468
 
469
    cyg_code get_byte(CYG_BYTE *val); // Get a byte from the stream
470
 
471
    cyg_code get_data(CYG_BYTE *addr, CYG_ADDRWORD size);
472
 
473
    cyg_code seek(CYG_ADDRWORD pos);  // seek to given position
474
 
475
};
476
 
477
#endif
478
 
479
// -------------------------------------------------------------------------
480
 
481
#endif // __cplusplus
482
 
483
#endif // CYG_LOADER_DYNAMIC_LD
484
 
485
// -------------------------------------------------------------------------
486
#endif // ifndef CYGONCE_LOADER_LOADER_HXX
487
// EOF loader.hxx

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.