I have a very basic question.
Lets take this snippet:
#include <stdio.h>
void foo(void) {
char *s = "StackOverflow";
printf("%s\n", s);
}
int main(void) {
foo();
}
In the process execution stack, main gets loaded on to the stack, then foo() gets called.
Now, where is the memory for “StackOverflow” allocated?
Similarly where is the memroy for “%s\n” allocated when printf is called?
Consider the following code:
Now the other question I have is, considering the below code:
#include <stdio.h>
int x;
int abc = 100;
void foo(void) {
char *s = "stackoverflow";
printf("%s\n", s);
}
int main(void) {
foo();
}
So, if I do objdump -s -j .bss a.out , I should see uninitialized segment and if I do objdump -s -j .data a.out , I should see initialized segment (abc=100) rt? Is there anything wrong with this assumption?
I get the following outputs though:
test > objdump -s -j .bss a.out
a.out: file format elf32-i386
test > objdump -s -j .data a.out
a.out: file format elf32-i386
Contents of section .data:
804954c 00000000 3c960408 00000000 64000000 ….<…….d…
What am I missing here?
thanks everyone again
"StackOverflow"and"%s\n"string literals are put in.rodata(read only data ) section in most systems.On UNIX, you can dump
.rodatasection using the objdump command:As added by @FatalError in the comments,
"%s\n"is not visible with objdump in the example asgccoptimizes a call toprintf("%s\n",str)by replacing it by a call toputs(str).To see the
"%s\n"string literal in the objdump output, you can compile your program withgcc -fno-builtin.