📄 elflwlv.c
字号:
}
static orl_reloc_type convert386Reloc( elf_reloc_type elf_type )
{
switch( elf_type ) {
case R_386_NONE:
case R_386_COPY:
return( ORL_RELOC_TYPE_ABSOLUTE );
case R_386_GOT32:
return( ORL_RELOC_TYPE_GOT_32 );
case R_386_PLT32:
return( ORL_RELOC_TYPE_PLT_32 );
case R_386_JMP_SLOT:
return( ORL_RELOC_TYPE_PLTREL_32 );
case R_386_32:
case R_386_GOTOFF:
return( ORL_RELOC_TYPE_WORD_32 );
case R_386_PC32:
case R_386_GOTPC:
return( ORL_RELOC_TYPE_REL_32_NOADJ );
default:
assert( 0 );
}
return( ORL_RELOC_TYPE_NONE );
}
static orl_reloc_type convertMIPSReloc( elf_reloc_type elf_type )
{
switch( elf_type ) {
case R_MIPS_NONE:
return( ORL_RELOC_TYPE_ABSOLUTE );
case R_MIPS_16:
return( ORL_RELOC_TYPE_WORD_16 );
case R_MIPS_32:
return( ORL_RELOC_TYPE_WORD_32 );
case R_MIPS_REL32:
return( ORL_RELOC_TYPE_REL_32 );
case R_MIPS_HI16:
return( ORL_RELOC_TYPE_HALF_HI );
case R_MIPS_LO16:
return( ORL_RELOC_TYPE_HALF_LO );
case R_MIPS_GOT16:
return( ORL_RELOC_TYPE_GOT_16 );
case R_MIPS_CALL16:
return( ORL_RELOC_TYPE_GOT_16 );
case R_MIPS_26:
return( ORL_RELOC_TYPE_WORD_26 );
case R_MIPS_PC16:
return( ORL_RELOC_TYPE_REL_16 );
default:
assert( 0 );
}
return( ORL_RELOC_TYPE_NONE );
}
orl_reloc_type ElfConvertRelocType( elf_file_handle elf_file_hnd, elf_reloc_type elf_type )
{
switch( elf_file_hnd->machine_type ) {
case ORL_MACHINE_TYPE_PPC601:
return( convertPPCReloc( elf_type ) );
case ORL_MACHINE_TYPE_I386:
return( convert386Reloc( elf_type ) );
case ORL_MACHINE_TYPE_R3000:
case ORL_MACHINE_TYPE_R4000:
return( convertMIPSReloc( elf_type ) );
default:
assert( 0 );
}
return( ORL_RELOC_TYPE_NONE );
}
orl_return ElfCreateRelocs( elf_sec_handle orig_sec, elf_sec_handle reloc_sec )
{
orl_return return_val;
int num_relocs;
int loop;
Elf32_Rel *rel;
Elf32_Rela *rela;
orl_reloc *o_rel;
if( reloc_sec->assoc.reloc.symbol_table->assoc.sym.symbols == NULL ) {
return_val = ElfCreateSymbolHandles( reloc_sec->assoc.reloc.symbol_table );
if( return_val != ORL_OKAY )
return( return_val );
}
switch( reloc_sec->type ) {
case ORL_SEC_TYPE_RELOCS:
num_relocs = reloc_sec->size / sizeof( Elf32_Rel );
reloc_sec->assoc.reloc.relocs = (orl_reloc *) _ClientSecAlloc( reloc_sec, sizeof( orl_reloc ) * num_relocs );
if( reloc_sec->assoc.reloc.relocs == NULL )
return( ORL_OUT_OF_MEMORY );
rel = (Elf32_Rel *) reloc_sec->contents;
o_rel = (orl_reloc *) reloc_sec->assoc.reloc.relocs;
for( loop = 0; loop < num_relocs; loop++ ) {
fix_rel_byte_order( reloc_sec->elf_file_hnd, rel );
o_rel->section = (orl_sec_handle) orig_sec;
o_rel->symbol = (orl_symbol_handle) &(reloc_sec->assoc.reloc.symbol_table->assoc.sym.symbols[ELF32_R_SYM( rel->r_info )]);
o_rel->type = ElfConvertRelocType( reloc_sec->elf_file_hnd, ELF32_R_TYPE( rel->r_info ) );
o_rel->offset = rel->r_offset;
o_rel->addend = 0;
o_rel->frame = NULL;
rel++;
o_rel++;
}
break;
case ORL_SEC_TYPE_RELOCS_EXPADD:
num_relocs = reloc_sec->size / sizeof( Elf32_Rela );
reloc_sec->assoc.reloc.relocs = (orl_reloc *) _ClientSecAlloc( reloc_sec, sizeof( orl_reloc ) * num_relocs );
if( reloc_sec->assoc.reloc.relocs == NULL )
return( ORL_OUT_OF_MEMORY );
rela = (Elf32_Rela *) reloc_sec->contents;
o_rel = (orl_reloc *) reloc_sec->assoc.reloc.relocs;
for( loop = 0; loop < num_relocs; loop++ ) {
fix_rela_byte_order( reloc_sec->elf_file_hnd, rela );
o_rel->section = (orl_sec_handle) orig_sec;
o_rel->symbol = (orl_symbol_handle) &(reloc_sec->assoc.reloc.symbol_table->assoc.sym.symbols[ELF32_R_SYM( rela->r_info )]);
o_rel->type = ElfConvertRelocType( reloc_sec->elf_file_hnd, ELF32_R_TYPE( rela->r_info ) );
o_rel->offset = rela->r_offset;
o_rel->addend = rela->r_addend;
o_rel->frame = NULL;
rela++;
o_rel++;
}
break;
default:
break;
}
return( ORL_OKAY );
}
static size_t strncspn( char *s, char *charset, int len)
{
char chartable[32];
int i;
unsigned char ch;
memset( chartable, 0, sizeof( chartable ) );
for( ; *charset != 0; charset++ ) {
ch = *charset;
chartable[ch / 8] |= 1 << ch % 8;
}
for (i = 0; i < len; i++) {
ch = s[i];
if( chartable[ch/8] & (1 << ch % 8) ) {
break;
}
}
return( i );
}
static char *pstrncspn( char *s, char *charset, int *len )
{
int l = strncspn( s, charset, *len );
*len -= l;
return( s + l );
}
static void EatWhite( char **contents, int *len )
/***********************************************/
{
char ch = **contents;
while( (ch == ' ' || ch == '\t' || ch == '=' || ch == ',') && *len > 0 ) {
(*len)--;
*contents += 1;
ch = **contents;
}
}
static orl_return ParseExport( char **contents, int *len,
orl_note_callbacks *cb, void *cookie )
/********************************************************************/
{
char *arg;
int l;
l = strncspn( *contents, ", \t", *len );
arg = alloca( l + 1 );
memcpy(arg, *contents, l);
arg[l] = 0;
*len -= l;
*contents += l;
return( cb->export_fn( arg, cookie ) );
}
static orl_return ParseDefLibEntry( char **contents, int *len,
orl_return (*deflibentry_fn)( char *, void * ), void *cookie )
/*****************************************************************/
{
char *arg;
int l;
orl_return retval;
for( ;; ) {
l = strncspn( *contents, ", \t", *len );
arg = alloca( l + 1 );
memcpy(arg, *contents, l);
arg[l] = 0;
*len -= l;
*contents += l;
retval = deflibentry_fn( arg, cookie );
if( retval != ORL_OKAY || **contents != ',' )
break;
(*contents)++;
}
return( retval );
}
orl_return ElfParseDrectve( char *contents, int len, orl_note_callbacks *cb,
void *cookie)
/**************************************************************************/
{
char *cmd;
EatWhite( &contents, &len );
while( len > 0 ) {
if( *contents != '-' )
break; // - should be start of token
contents++; len--;
cmd = contents;
contents = pstrncspn( contents, ":", &len);
if( contents == NULL )
break;
contents++; len--;
if( memicmp( cmd, "export", 6 ) == 0 ) {
if( ParseExport( &contents, &len, cb, cookie ) != ORL_OKAY )
break;
} else if( memicmp( cmd, "defaultlib", 10 ) == 0 ) {
if( ParseDefLibEntry( &contents, &len, cb->deflib_fn, cookie )
!= ORL_OKAY ) break;
} else if( memicmp( cmd, "entry", 5 ) == 0 ) {
if( ParseDefLibEntry( &contents, &len, cb->entry_fn, cookie )
!= ORL_OKAY ) break;
}
EatWhite( &contents, &len );
}
return( ORL_OKAY );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -