📄 loader.hxx
字号:
// PLT relocation entries
Elf32_Addr jmprel; // address of relocation table
Elf32_Word pltrelsz; // size of jmprel table in bytes
Elf32_Word pltrel; // type of table entries: DT_REL
// or DT_RELA
// Relocation table with implicit addends
Elf32_Rel *rel; // table address
Elf32_Word relsize; // table size in bytes
Elf32_Word relent; // size of entry
// Relocation table with explicit addends
Elf32_Rela *rela; // table address
Elf32_Word relasize; // table size in bytes
Elf32_Word relaent; // size of entry
// Init and Fini support
Elf32_Addr init; // address of init function
Elf32_Addr fini; // address of fini function
Elf32_Addr *init_array; // address of init array
Elf32_Word init_array_sz; // size of init array
Elf32_Addr *fini_array; // address of fini array
Elf32_Word fini_array_sz; // size of fini array
Elf32_Addr *pre_init_array; // address of pre_init array
Elf32_Word pre_init_array_sz; // size of pre_init array
Elf32_Dyn *dynamic; // address of _DYNAMIC section
CYG_ADDRESS base; // base address used in address and
// relocation calcualtions
Cyg_CList_T<Cyg_LoaderMemBlock> segs; // chain of loaded segments
cyg_code error; // most recent error code
// private functions
void parse_dynamic( Elf32_Dyn *dynamic );
// Translate a symbol from its symbol table index into its
// actual address
CYG_ADDRESS get_sym_addr_from_ix( Elf32_Word sym );
// Get a symbol from this object's symbol table.
Elf32_Sym *get_sym( Elf32_Word ix );
// Get a name from this object's string table.
char *get_name( Elf32_Word offset );
// Look up the name in the hash table and return its symbol
// table entry, or NULL if not found.
Elf32_Sym *hash_lookup( const char *name );
// Look up the name in the hash table and return its absolute
// address or CYG_LOADER_NULLSYMADDR if it is not found.
CYG_ADDRESS hash_lookup_addr( const char *name );
// Standard ELF-defined hash function for symbol table.
static unsigned long elf_hash( const unsigned char *name );
public:
// Constructor - reads and allocates the executable.
Cyg_LoadObject_Base();
Cyg_LoadObject_Base( Cyg_LoaderStream& stream,
cyg_uint32 mode,
Cyg_LoaderMemAlloc *mem );
~Cyg_LoadObject_Base(); // Destructor
inline cyg_code get_error() { return error; };
// Translate a symbol into its address.
void *symbol( const char *name );
// Start this executable running
cyg_code exec(int argc, char **argv, char **envv);
};
#endif // __cplusplus
#endif // CYG_LOADER_DYNAMIC_LD
// -------------------------------------------------------------------------
// All the following files should have suitable ifdefs so that only
// one actually provides content.
#include <cyg/loader/mips_elf.h> // MIPS ELF support
#include <cyg/loader/arm_elf.h> // ARM ELF support
#include <cyg/loader/i386_elf.h> // i386 ELF support
#include <cyg/loader/ppc_elf.h> // PowerPC ELF support
//#include <cyg/loader/sparc_elf.h> // Sparc ELF support
//#include <cyg/loader/sh_elf.h> // SH ELF support
// -------------------------------------------------------------------------
#ifndef CYG_LOADER_DYNAMIC_LD
#ifdef __cplusplus
class Cyg_LoadObject :
public Cyg_LoadObject_Proc
{
Cyg_LoaderMemBlock *block;
public:
inline Cyg_LoadObject()
: Cyg_LoadObject_Proc()
{
};
inline Cyg_LoadObject( Cyg_LoaderStream& stream,
cyg_uint32 mode,
Cyg_LoaderMemAlloc *mem,
Cyg_LoaderMemBlock *ablock = NULL)
: Cyg_LoadObject_Proc( stream, mode, mem )
{
block = ablock;
};
inline ~Cyg_LoadObject() {};
Cyg_LoaderMemBlock *get_block() { return block; };
// Apply dynamic relocations to the executable
void relocate();
// Apply PLT relocations
void relocate_plt();
};
// -------------------------------------------------------------------------
// Main loader class
class Cyg_Loader
{
// current error code.
cyg_code error;
// Default memory allocator.
Cyg_LoaderMemAlloc *mem_default;
// Load object for main program
Cyg_LoadObject *main;
// List of loaded executables, including the main
// program and all libraries.
Cyg_CList_T<Cyg_LoadObject> loadlist;
public:
Cyg_Loader( Cyg_LoaderMemAlloc *mem); // Constructor
~Cyg_Loader(); // Destructor
// Load an object and all its dependencies.
cyg_code load( Cyg_LoaderStream& stream,
cyg_uint32 mode,
Cyg_LoadObject **object);
// Close an object and remove it from memory
cyg_code close( Cyg_LoadObject *object );
// Translate current error code into a string.
const char *error_string( );
// Look up a symbol
Elf32_Sym *hash_lookup( const char *name );
// look up a named symbol in loadlist and return its
// address relocated to its load address.
CYG_ADDRESS hash_lookup_addr( const char *name );
// Static pointer to loader object
static Cyg_Loader *loader;
};
//==========================================================================
// Memory based loader stream
class Cyg_LoaderStream_Mem
: public Cyg_LoaderStream
{
CYG_ADDRESS base;
CYG_ADDRESS pos;
CYG_ADDRESS end;
public:
Cyg_LoaderStream_Mem( const void *addr, cyg_int32 size );
~Cyg_LoaderStream_Mem(); // Destructor
cyg_code get_byte(CYG_BYTE *val); // Get a byte from the stream
cyg_code get_data(CYG_BYTE *addr, CYG_ADDRWORD size);
cyg_code seek(CYG_ADDRWORD pos); // seek to given position
};
//==========================================================================
// Fileio based loader stream
#ifdef CYGPKG_IO_FILEIO
class Cyg_LoaderStream_File
: public Cyg_LoaderStream
{
int fd;
public:
Cyg_LoaderStream_File( int afd );
~Cyg_LoaderStream_File(); // Destructor
cyg_code get_byte(CYG_BYTE *val); // Get a byte from the stream
cyg_code get_data(CYG_BYTE *addr, CYG_ADDRWORD size);
cyg_code seek(CYG_ADDRWORD pos); // seek to given position
};
#endif
// -------------------------------------------------------------------------
#endif // __cplusplus
#endif // CYG_LOADER_DYNAMIC_LD
// -------------------------------------------------------------------------
#endif // ifndef CYGONCE_LOADER_LOADER_HXX
// EOF loader.hxx
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -