I delve into Linux and C and am curious how functions are stored in memory. I have the following function:
void test(){ printf( "test\n" ); }
Simple enough. When I run objdump in an executable that has this function, I get the following:
08048464 <test>: 8048464: 55 push %ebp 8048465: 89 e5 mov %esp,%ebp 8048467: 83 ec 18 sub $0x18,%esp 804846a: b8 20 86 04 08 mov $0x8048620,%eax 804846f: 89 04 24 mov %eax,(%esp) 8048472: e8 11 ff ff ff call 8048388 <printf@plt> 8048477: c9 leave 8048478: c3 ret
That everything looks right. The interesting part is when I run the following code snippet:
int main( void ) { char data[20]; int i; memset( data, 0, sizeof( data ) ); memcpy( data, test, 20 * sizeof( char ) ); for( i = 0; i < 20; ++i ) { printf( "%x\n", data[i] ); } return 0; }
I get the following (which is wrong):
55 ffffff89 ffffffe5 ffffff83 ffffffec 18 ffffffc7 4 24 10 ffffff86 4 8 ffffffe8 22 ffffffff ffffffff ffffffff ffffffc9 ffffffc3
If I choose to leave memset (data, 0, sizeof (data)); line, then the rightmost byte is correct, but some of them still have leading 1s.
Does anyone have any explanation why
A) using memset to clear my array leads to an incorrect (edit: inaccurate) representation of the function and
SOLUTION: was associated with the use of memset (data, 0, sizeof (data)), not memset (data, 0, 20 * sizeof (unsigned char)). The memory was not fully installed because it looked only at the size of the pointer than the size of the entire array.
B) what is this byte stored in memory? Ints? char? I don’t quite understand what is going on here. (clarification: what type of pointer would I use to move such data into memory?)
SOLUTION: I'm dumb. I forgot the unsigned keyword, and this is where the whole problem arose :(
Any help would be greatly appreciated - I could not find anything when I was looking for it.
Neil
PS: I immediately thought that this is the result of x86 having instructions that do not end on a byte or nibble border. But this does not make much sense and should not cause any problems.
Thanks Will for pointing out my error with type char. It must be unsigned char. I'm still interested in learning how to access individual bytes.