Calc checksum of certain ELF sections

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;
}
 
A

Arthur J. O'Dwyer

I'm on Sun 0S 5.8 [...]

You naturally realize that all this system-specific stuff is
either irrelevant or off-topic, or both, right? Not to say you
shouldn't have provided /some/ context, but if your problem turns
out to depend on someone's running Sun "0S", you've come to the
wrong place. Ditto if your problem is that you can't write a CRC
algorithm; try comp.programming for that.

Your code is next to unreadable for me; must be the //icky comments
(which BTW are Outlook-unfriendly on Usenet, or so I've heard, and
are C90- and eye-unfriendly in any case) and the lack of whitespace.
I've made cosmetic improvements in the following.
int get_chksum(int i_addr, int i_nbytes, FILE* bin_fp)
{
int csum, l;

Please please /please/ don't name variables 'l'! Quick, what's
l+l? If you answered "2", kick yourself in the head.
From the code below, I see you (try to) use 'l' as a loop counter.
A better name in this case would be 'i', except that it would get
mixed up with those Hungarian-notation 'i_'s prepended to all your
other variable names. Lose the Hungarian while you're at it; your
maintenance programmer will thank you.
char *data = NULL;

//i realize theres no reason for this line, it was for a prev. try
:)

Syntax error. Watch those long lines on Usenet. (Shameless plug:
http://www.contrib.andrew.cmu.edu/~ajo/free-software/usenetify2.c
) Ditto those tabs; hard tabs are Evil. Never mind why, right now.
if (fseek(bin_fp, i_addr, SEEK_SET) == 0)
{
data = (char*) malloc(i_nbytes);

Lose the cast, unless you're writing C++ (and then, you're in the
wrong newsgroup).

data = 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 but was a prev. sugg.
fprintf(data, "%c", (i_addr+l) & 0xFF);

First, you realize this loop just prints some ascending numbers,
and doesn't do anything involving this 'data' array you've carefully
allocated and then forgotten about?
Second, you realize the comment text is completely unreadable?
}

// exec crc algorithm and free mem.
// CRC_POLYNOMIAL=0x04c11db7L (unsure what this is for)

Well, why did you comment it out, then?
csum = CalculateCRC32(data, i_nbytes, 0, CRC_POLYNOMIAL,
0xffffffff);

'data' holds garbage at this point; why are you trying to
compute its CRC?
free(data);
}
else {
// failed
csum = 0;
printf("Error: could not seek to start section address in file\n");

Nitpick: I suggest using 'puts' in place of 'printf' whenever possible.

puts("Error: could not seek to start section address in file");
}

return csum;
}

I see no definition for 'CalculateCRC32' here. Nor a declaration
for it, before its use. Have you done much programming in C before?
I (and everyone else I've ever met) recommend Kernighan and Ritchie's
"The C Programming Language," 2nd edition. (Also Kernighan and
Plauger's "Elements of Programming Style," if you can find it; it'll
help you clean up that icky code.)

Post a complete, compilable program that exhibits your bug, or
at least a complete question. I see no questions in your original
post, although you do hint that you might be having a problem of
some sort with the (unshown) CRC routine.

And remember, no tabs, no // icky C++ comments, no malloc casts,
and a *complete, compilable program!* :)

HTH,
-Arthur
 
K

kyle york

Greetings,

Apparently Arthur is having a bad-Dan day & so insists on not helping
you, but rather complaining about your comment style which is standard
C. Had you posted CalculateCRC32 he'd have complained that you're in the
wrong group.

I will agree variable names like 'l', 'i', 'o', and 'Q' are not a good idea.
data = (char*) malloc(i_nbytes); //allocate/read section into mem
//fread(data,i_nbytes,1,bin_fp);

Here you've commented out the read, so data is simply random bytes. That
would be a good start. Failing that, comp.programming is probably where
you ought to be.
 
K

Kevin

Arthur J. O'Dwyer said:
I'm on Sun 0S 5.8 [...]

You naturally realize that all this system-specific stuff is
either irrelevant or off-topic, or both, right? Not to say you
shouldn't have provided /some/ context, but if your problem turns
out to depend on someone's running Sun "0S", you've come to the
wrong place. Ditto if your problem is that you can't write a CRC
algorithm; try comp.programming for that.

Your code is next to unreadable for me; must be the //icky comments
(which BTW are Outlook-unfriendly on Usenet, or so I've heard, and
are C90- and eye-unfriendly in any case) and the lack of whitespace.
I've made cosmetic improvements in the following.
int get_chksum(int i_addr, int i_nbytes, FILE* bin_fp)
{
int csum, l;

Please please /please/ don't name variables 'l'! Quick, what's
l+l? If you answered "2", kick yourself in the head.
From the code below, I see you (try to) use 'l' as a loop counter.
A better name in this case would be 'i', except that it would get
mixed up with those Hungarian-notation 'i_'s prepended to all your
other variable names. Lose the Hungarian while you're at it; your
maintenance programmer will thank you.
char *data = NULL;

//i realize theres no reason for this line, it was for a prev. try
:)

Syntax error. Watch those long lines on Usenet. (Shameless plug:
http://www.contrib.andrew.cmu.edu/~ajo/free-software/usenetify2.c
) Ditto those tabs; hard tabs are Evil. Never mind why, right now.
if (fseek(bin_fp, i_addr, SEEK_SET) == 0)
{
data = (char*) malloc(i_nbytes);

Lose the cast, unless you're writing C++ (and then, you're in the
wrong newsgroup).

data = 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 but was a prev. sugg.
fprintf(data, "%c", (i_addr+l) & 0xFF);

First, you realize this loop just prints some ascending numbers,
and doesn't do anything involving this 'data' array you've carefully
allocated and then forgotten about?
Second, you realize the comment text is completely unreadable?
}

// exec crc algorithm and free mem.
// CRC_POLYNOMIAL=0x04c11db7L (unsure what this is for)

Well, why did you comment it out, then?
csum = CalculateCRC32(data, i_nbytes, 0, CRC_POLYNOMIAL,
0xffffffff);

'data' holds garbage at this point; why are you trying to
compute its CRC?
free(data);
}
else {
// failed
csum = 0;
printf("Error: could not seek to start section address in file\n");

Nitpick: I suggest using 'puts' in place of 'printf' whenever possible.

puts("Error: could not seek to start section address in file");
}

return csum;
}

I see no definition for 'CalculateCRC32' here. Nor a declaration
for it, before its use. Have you done much programming in C before?
I (and everyone else I've ever met) recommend Kernighan and Ritchie's
"The C Programming Language," 2nd edition. (Also Kernighan and
Plauger's "Elements of Programming Style," if you can find it; it'll
help you clean up that icky code.)

Post a complete, compilable program that exhibits your bug, or
at least a complete question. I see no questions in your original
post, although you do hint that you might be having a problem of
some sort with the (unshown) CRC routine.

And remember, no tabs, no // icky C++ comments, no malloc casts,
and a *complete, compilable program!* :)

HTH,
-Arthur

your post doesnt even justify a response. I hope you had a better day
after you wrote this.
 
K

Kevin

Thanks for the input Kyle. Another programmer suggested I use fprintf
instead of fread. It sounded strange to me but i tried it anyways. I
will move over to .programming.
 
M

Martin Ambuhl

Kevin posted 109 lines of which his contribution was:
your post doesnt even justify a response. I hope you had a better day
after you wrote this.

No matter what the merits of Arthur's post, your inane quoting of it all
just to post a "piss-off" line is at best anti-social. As it turns out,
many of Arthur's points were worth your attention. Your loss is not
only from ignoring those points, but from discouraging others from ever
offering any help to you in this newsgroup, ever.
 
D

Dave Thompson

Lose the cast, unless you're writing C++ (and then, you're in the
wrong newsgroup).

data = malloc(i_nbytes);


First, you realize this loop just prints some ascending numbers,
and doesn't do anything involving this 'data' array you've carefully
allocated and then forgotten about?

It doesn't even do that; it tries to use uninitialized (probably
garbage) memory pointed to by data as a FILE, which is completely
Undefined and likely to either crash or do something really crazy. If
you used a valid file pointer, like stdout, it would print
*characters* with ascending (modulo wraparound) *codes* which are not
particularly likely to be digits.


- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top