K
Kevin
I'm on Sun 0S 5.8 and need to calculate the checksum of certain
sections in an ELF binary file. Specifically .text to .rodata. I'm
able to parse through to the program header table and then find the
section header table and then find the correct sections using the
section header string table. Using this method, I am able to find the
offset to the .text section and the number of bytes for the range of
sections i need to calculate the checksum for.
So basically, I've got the address and the size and I've verified this
info on a test ELF file through gdump -map. My problem lies in
actually calculating the checksum using a canned crc32 algorithm.
Following is my method for storing the binary data of the needed
sections into a buffer array and then the crc32 alg. I also have the
correct checksum which so far, I have not duplicated with my code
.
Your help/suggs/pointers are appreciated.
-Kevin
-----------
int get_chksum(int i_addr, int i_nbytes, FILE* bin_fp)
{
int csum,l;
char *data = NULL;
//i realize theres no reason for this line, it was for a prev. try

if(fseek(bin_fp,i_addr,SEEK_SET) == 0) //goto section at i_addr
{
data = (char*) malloc(i_nbytes); //allocate/read section into mem
//fread(data,i_nbytes,1,bin_fp);
for(l=0;l<i_nbytes;l++)
{
//i_addr is sec_hdr_ptr(at .text)->sh_offset
//using 0xFF mask makes no diff but was a prev. sugg.
fprintf(data,"%c",(i_addr+l) & 0xFF);
}
//exec crc algorithm and free mem.
//CRC_POLYNOMIAL=0x04c11db7L (unsure what this is for)
csum = CalculateCRC32(data, i_nbytes, 0, CRC_POLYNOMIAL,
0xffffffff);
free(data);
}
else { //failed
csum=0;
printf("Error: could not seek to start section address in file\n");
}
return csum;
}
sections in an ELF binary file. Specifically .text to .rodata. I'm
able to parse through to the program header table and then find the
section header table and then find the correct sections using the
section header string table. Using this method, I am able to find the
offset to the .text section and the number of bytes for the range of
sections i need to calculate the checksum for.
So basically, I've got the address and the size and I've verified this
info on a test ELF file through gdump -map. My problem lies in
actually calculating the checksum using a canned crc32 algorithm.
Following is my method for storing the binary data of the needed
sections into a buffer array and then the crc32 alg. I also have the
correct checksum which so far, I have not duplicated with my code
Your help/suggs/pointers are appreciated.
-Kevin
-----------
int get_chksum(int i_addr, int i_nbytes, FILE* bin_fp)
{
int csum,l;
char *data = NULL;
//i realize theres no reason for this line, it was for a prev. try
if(fseek(bin_fp,i_addr,SEEK_SET) == 0) //goto section at i_addr
{
data = (char*) malloc(i_nbytes); //allocate/read section into mem
//fread(data,i_nbytes,1,bin_fp);
for(l=0;l<i_nbytes;l++)
{
//i_addr is sec_hdr_ptr(at .text)->sh_offset
//using 0xFF mask makes no diff but was a prev. sugg.
fprintf(data,"%c",(i_addr+l) & 0xFF);
}
//exec crc algorithm and free mem.
//CRC_POLYNOMIAL=0x04c11db7L (unsure what this is for)
csum = CalculateCRC32(data, i_nbytes, 0, CRC_POLYNOMIAL,
0xffffffff);
free(data);
}
else { //failed
csum=0;
printf("Error: could not seek to start section address in file\n");
}
return csum;
}