summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFreeArtMan <dos21h@gmail.com>2016-09-03 17:29:10 +0100
committerFreeArtMan <dos21h@gmail.com>2016-09-03 17:29:10 +0100
commitcdfdf8982f9abe9515fac24a9b3d57ddb73bc7c2 (patch)
treebe5812ca8ec64c1c403115d4a686e20823e87334
parent885658f5475b4ab35254b86ce0507790807299b6 (diff)
downloadlibelf-master.tar.gz
libelf-master.zip
Fixed strtab searchingHEADmaster
-rw-r--r--elfreader.c58
-rw-r--r--elfreader.h2
2 files changed, 36 insertions, 24 deletions
diff --git a/elfreader.c b/elfreader.c
index b514da3..eb0edf7 100644
--- a/elfreader.c
+++ b/elfreader.c
@@ -347,6 +347,7 @@ Elf_Ehdr* elf_header( FILE *f, Elf_Ident *ident, int sysorder )
elf_ehdr_32( ret, (Elf32_Ehdr *)buf, byteorder );
ret->type = ELF_TYPE_32;
ret->byteorder = byteorder;
+ PNL();
} else if ( class == ELF_TYPE_64 )
{
PNL();
@@ -648,10 +649,17 @@ Elf_Shdr** elf_shdr( FILE *f, Elf_Ehdr *ehdr )
void section_info( Elf_Shdr* shdr, Elf_Shdr_strtab *strtab )
{
printf("ELF SECTION HEADER:\n");
+ if ( NULL == shdr || NULL == strtab )
+ {
+ ENL();
+ return;
+ }
if ( strtab != NULL )
{
char *s = NULL;
s = strtab_name( strtab, shdr->sh_name);
+ //printf("sh_name,0x%x\n",shdr->sh_name);
+ //printf("offset,0x%x\n",strtab->offset+shdr->sh_name);
if ( s != NULL )
{
printf("NAME :%s\n", s);
@@ -659,41 +667,45 @@ void section_info( Elf_Shdr* shdr, Elf_Shdr_strtab *strtab )
//s = NULL;
}
}
- printf("TYPE : 0x%02x\n", shdr->sh_type);
- printf("OFFSET: 0x%08x\n", shdr->sh_offset);
+ printf("NAME : 0x%08x ", shdr->sh_name);
+ printf("TYPE : 0x%02x ", shdr->sh_type);
+ printf("OFFSET: 0x%08x ", shdr->sh_offset);
printf("SIZE : 0x%08x\n", shdr->sh_size);
}
-Elf_Shdr_strtab* strtab_search( FILE *f, Elf_Shdr **shdr )
+Elf_Shdr_strtab* strtab_search( FILE *f, Elf_Ehdr *ehdr, Elf_Shdr **shdr )
{
+ uint16_t strndx;
+ int i;
+
Elf_Shdr_strtab *ret = NULL;
Elf_Shdr *iter = NULL;
- if ( shdr == NULL )
+ if ( NULL == shdr || NULL == *shdr || NULL == ehdr || NULL == f )
{
return NULL;
}
- iter = shdr[0];
- while ( iter != NULL )
+ strndx = ehdr->e_shstrndx;
+ if (strndx != 0)
{
- if ( iter->sh_type == SHT_STRTAB )
- {
- ret = malloc( sizeof(Elf_Shdr_strtab) );
- ret->offset = iter->sh_offset;
- ret->sz_tab = iter->sh_size;
- ret->strtab = malloc(ret->sz_tab);
- fseek( f, ret->offset, SEEK_SET );
- if ( fread( ret->strtab, 1, ret->sz_tab, f ) != ret->sz_tab )
- {
- free( ret->strtab );
- free( ret );
- printf("Cannot read strtab\n");
- return NULL;
- }
- return ret;
- }
- iter++;
+ iter = shdr[strndx];
+ } else
+ {
+ return NULL;
+ }
+
+ ret = malloc( sizeof(Elf_Shdr_strtab) );
+ ret->offset = iter->sh_offset;
+ ret->sz_tab = iter->sh_size;
+ ret->strtab = malloc(ret->sz_tab);
+ fseek( f, ret->offset, SEEK_SET );
+ if ( fread( ret->strtab, 1, ret->sz_tab, f ) != ret->sz_tab )
+ {
+ free( ret->strtab );
+ free( ret );
+ printf("Cannot read strtab\n");
+ return NULL;
}
return ret;
diff --git a/elfreader.h b/elfreader.h
index 50e08a7..06a5747 100644
--- a/elfreader.h
+++ b/elfreader.h
@@ -152,7 +152,7 @@ Elf_Shdr** elf_shdr( FILE *f, Elf_Ehdr *ehdr );
void section_info( Elf_Shdr* shdr, Elf_Shdr_strtab *strtab );
/* Search for strtab section */
-Elf_Shdr_strtab* strtab_search( FILE *f, Elf_Shdr **shdr );
+Elf_Shdr_strtab* strtab_search( FILE *f, Elf_Ehdr *ehdr, Elf_Shdr **shdr );
unsigned char* strtab_name( Elf_Shdr_strtab *strtab, long offset );
#endif \ No newline at end of file