If I define a local character array within a function and then use objdump to grab the assembly code for that particular function, can I find the memory for that array within the assembly code?
This is a question I have for a homework assignment.
Sure, as long as your array has a non-zero initializer, you should be able to find it. Here's an example I made for ARM:
char function(int i)
{
char arr[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
return arr[i];
}
Build it:
$ clang -O2 -Wall -c -o example.o example.c
Disassemble the output:
$ objdump -d example.o
example.o: file format elf32-littlearm
Disassembly of section .text:
00000000 <function>:
0: e59f1004 ldr r1, [pc, #4] ; c <function+0xc>
4: e7d10000 ldrb r0, [r1, r0]
8: e12fff1e bx lr
c: 00000000 .word 0x00000000
Hmm - notice that .word 0x0000000
at offset 0xc
? That's going to be fixed up by the linker to point to the array. Let's go check out the relocation table:
$ objdump -r example.o
example.o: file format elf32-littlearm
RELOCATION RECORDS FOR [.text]:
OFFSET TYPE VALUE
00000008 R_ARM_V4BX *ABS*
0000000c R_ARM_ABS32 .rodata.cst8
Aha! The word at 0xc
is going to get fixed up with an absolute pointer to the .rodata.cst8
section - that sounds like what we want. Let's take a peek:
$ objdump -s -j .rodata.cst8 example.o
example.o: file format elf32-littlearm
Contents of section .rodata.cst8:
0000 01020304 05060708 ........
And there you have the contents of the array!