CRC32 on ELF in C problem

From: Kevin (kevin_at_netkev.com)
Date: 07/26/04


Date: 26 Jul 2004 07:54:41 -0700

I need to calculate the checksum of certain sections in an ELF binary
file. 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.

I've verified the starting offset and size on a test ELF file through
gdump -map and my program is correct in how it gathers those. My
problem lies in
actually calculating the checksum using a provided crc32 algorithm.
Following is my method for storing the binary data of the needed
sections into a buffer array and then the crc32 alg. You'll notice
that I have commented out fread and used fprintf instead. This is a
suggestion from the programmer who tasked me with the program but I do
not understand how it could work.

I do have the correct checksum to see if my program works. My
question is (obvious to some but not others), what do I need to change
in order to output the correct checksum?

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 fread*/
    if(fseek(bin_fp,i_addr,SEEK_SET) == 0)
    {
        data = (char*) malloc(i_nbytes);
        //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. was a prev sugg*/
            fprintf(data,"%c",(i_addr+l) & 0xFF);
        }

        //CRC_POLYNOMIAL is def earlier as 0x04c11db7L
        csum = CalculateCRC32(data, i_nbytes, 0,
CRC_POLYNOMIAL,0xffffffff);
        free(data);
    }
    else {
        csum=0;
        printf("Error: could not seek to start section address in
file\n");
    }

    return csum;
}

//provided crc32 alg
int CalculateCRC32(unsigned char *s, int len, int endianswap, unsigned
int polynomial, unsigned int seedvalue)
{
    int csum=seedvalue;
    int data;
    int j, i;

    printf("received 0x%8.8x byte char array\n",len);
    
    for (j = 0; j < len ; j++)
    {
        data = s[endianswap ? j ^ 3 : j] << 24; // read 1 byte, int
= 4, so shift left 3(24bits)
        //printf("j= %d, data=%d\n",j,data); //debug
      
      for (i=0; i<8; ++i)
      { /* Loop for each
bit in byte */
         if ((data^csum) & 0x80000000)
         { /* Shift CRC, feed
back hi bit */
            csum = (csum<<1) ^ polynomial;
         }
         else
         {
            csum <<= 1;
         }

         data <<= 1;
      }
    }

    return csum;
}



Relevant Pages

  • Re: Translating c code help required
    ... Checksum = checksum + Chr) ... unsigned int ... mag_checksum(const char * const buf) ... int csum = 0; ...
    (comp.lang.c)
  • Re: Translating c code help required
    ... I am trying to find out how the checksum is computed for a Megellan ... unsigned int ... mag_checksum(const char * const buf) ... int csum = 0; ...
    (comp.lang.c)
  • [PATCH] add checksum selftest
    ... Start a checksum internal testsuite for arch porters and people mucking ... -static unsigned int do_csum(const unsigned char *buff, ...
    (Linux-Kernel)
  • Re: About virtual and abstract method
    ... "when we call a virtual method, the runtime will check the instance who ... called the method and then choose the suitable override method, ... public abstract bool Calc(bytearray, int offset, int length, out int ... public bool Calc(bytearray, out int checksum) {return Calc(array, 0, ...
    (microsoft.public.dotnet.languages.csharp)
  • Calc checksum of certain ELF sections
    ... I'm on Sun 0S 5.8 and need to calculate the checksum of certain ... sections in an ELF binary file. ... able to parse through to the program header table and then find the ... int get_chksum ...
    (comp.lang.c)