By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,617 Members | 1,172 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,617 IT Pros & Developers. It's quick & easy.

Calc checksum of certain ELF sections

P: n/a
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;
}
Nov 14 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

On Fri, 23 Jul 2004, Kevin wrote:

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/~a...e/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

Nov 14 '05 #2

P: n/a
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.

Kevin wrote:
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.

--
Kyle A. York
Sr. Subordinate Grunt
Nov 14 '05 #3

P: n/a
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote in message news:<Pi**********************************@unix48. andrew.cmu.edu>...
On Fri, 23 Jul 2004, Kevin wrote:

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/~a...e/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.
Nov 14 '05 #4

P: n/a
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.

kyle york <ky***@cisco.com> wrote in message news:<1090617392.684382@sj-nntpcache-5>...
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.

Kevin wrote:
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.

Nov 14 '05 #5

P: n/a
Kevin posted 109 lines of which his contribution was:
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote in message news:<Pi**********************************@unix48. andrew.cmu.edu>... [...] 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.
Nov 14 '05 #6

P: n/a
On Fri, 23 Jul 2004 14:49:51 -0400 (EDT), "Arthur J. O'Dwyer"
<aj*@nospam.andrew.cmu.edu> wrote:

On Fri, 23 Jul 2004, Kevin wrote:

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?


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
Nov 14 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.