EDIT:
The other answer and comments are correct - as is, your variables will be optimized out because they aren't even used. But let's have a little fun and actually use them to see what happens.
I compiled the op's program as-is with gcc -S trial.c
, and although myFunc was never called, nothing else about this answer changes.
I've slightly modified your program to actually use those variables so we can learn a little more about what the compiler and linker will do. Here it is:
#include <stdio.h>
int myFunc()
{
static const char* str1 = "Hello";
const char* str2 = "World";
int var1 = 1;
static int var2 = 8;
printf("%s %s %d %d\n", str1, str2, var1, var2);
return 0;
}
int main()
{
return myFunc();
}
I compiled with gcc -S trial.c
and got the following assembly file:
.file "trial.c"
.section .rdata,"dr"
.LC0:
.ascii "World\0"
.LC1:
.ascii "%s %s %d %d\12\0"
.text
.globl myFunc
.def myFunc; .scl 2; .type 32; .endef
.seh_proc myFunc
myFunc:
pushq %rbp
.seh_pushreg %rbp
movq %rsp, %rbp
.seh_setframe %rbp, 0
subq $64, %rsp
.seh_stackalloc 64
.seh_endprologue
leaq .LC0(%rip), %rax
movq %rax, -8(%rbp)
movl $1, -12(%rbp)
movl var2.3086(%rip), %edx
movq str1.3083(%rip), %rax
movl -12(%rbp), %r8d
movq -8(%rbp), %rcx
movl %edx, 32(%rsp)
movl %r8d, %r9d
movq %rcx, %r8
movq %rax, %rdx
leaq .LC1(%rip), %rcx
call printf
movl $0, %eax
addq $64, %rsp
popq %rbp
ret
.seh_endproc
.def __main; .scl 2; .type 32; .endef
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
pushq %rbp
.seh_pushreg %rbp
movq %rsp, %rbp
.seh_setframe %rbp, 0
subq $32, %rsp
.seh_stackalloc 32
.seh_endprologue
call __main
call myFunc
addq $32, %rsp
popq %rbp
ret
.seh_endproc
.data
.align 4
var2.3086:
.long 8
.section .rdata,"dr"
.LC2:
.ascii "Hello\0"
.data
.align 8
str1.3083:
.quad .LC2
.ident "GCC: (Rev1, Built by MSYS2 project) 5.4.0"
.def printf; .scl 2; .type 32; .endef
var1 isn't even found in the assembly file. It's actually just a constant that gets loaded onto the stack.
At the top of the assembly file, we see "World" (str2) in the .rdata section. Lower down in the assembly file, the string "Hello" is in the .rdata section, but the label for str1 (which contains the label, or address, for "Hello") is in the .data section. var2 is also in the .data section.
Here's a stackoverflow question that delves a little deeper into why this happens.
Another stackoverflow question points out that the .rdata section is the read-only section of .data and explains the different sections.
Hope this helps.
EDIT:
I decided to try this with the -O3 compiler flag (high optimizations). Here's the assembly file that I got:
.file "trial.c"
.section .rdata,"dr"
.LC0:
.ascii "World\0"
.LC1:
.ascii "Hello\0"
.LC2:
.ascii "%s %s %d %d\12\0"
.section .text.unlikely,"x"
.LCOLDB3:
.text
.LHOTB3:
.p2align 4,,15
.globl myFunc
.def myFunc; .scl 2; .type 32; .endef
.seh_proc myFunc
myFunc:
subq $56, %rsp
.seh_stackalloc 56
.seh_endprologue
leaq .LC0(%rip), %r8
leaq .LC1(%rip), %rdx
leaq .LC2(%rip), %rcx
movl $8, 32(%rsp)
movl $1, %r9d
call printf
nop
addq $56, %rsp
ret
.seh_endproc
.section .text.unlikely,"x"
.LCOLDE3:
.text
.LHOTE3:
.def __main; .scl 2; .type 32; .endef
.section .text.unlikely,"x"
.LCOLDB4:
.section .text.startup,"x"
.LHOTB4:
.p2align 4,,15
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
subq $40, %rsp
.seh_stackalloc 40
.seh_endprologue
call __main
xorl %eax, %eax
addq $40, %rsp
ret
.seh_endproc
.section .text.unlikely,"x"
.LCOLDE4:
.section .text.startup,"x"
.LHOTE4:
.ident "GCC: (Rev1, Built by MSYS2 project) 5.4.0"
.def printf; .scl 2; .type 32; .endef
var1 is now just a constant 1 that is placed in a register (r9d). var2 is also just a constant, but it's placed on the stack. Also, the strings "Hello" and "World" are accessed in a more direct (efficient) way.
So, I decided that I wanted to try something slightly different:
#include <stdio.h>
void myFunc()
{
static const char* str1 = "Hello";
const char* str2 = "World";
int var1 = 1;
static int var2 = 8;
printf("%s %s %d %d\n", str1, str2, var1, var2);
var1++;
var2++;
printf("%d %d", var1, var2);
}
int main()
{
myFunc();
myFunc();
return 0;
}
And the associated assembly using gcc -O3 -S trial.c
.file "trial.c"
.section .rdata,"dr"
.LC0:
.ascii "World\0"
.LC1:
.ascii "Hello\0"
.LC2:
.ascii "%s %s %d %d\12\0"
.LC3:
.ascii "%d %d\0"
.section .text.unlikely,"x"
.LCOLDB4:
.text
.LHOTB4:
.p2align 4,,15
.globl myFunc
.def myFunc; .scl 2; .type 32; .endef
.seh_proc myFunc
myFunc:
subq $56, %rsp
.seh_stackalloc 56
.seh_endprologue
movl var2.3086(%rip), %eax
leaq .LC0(%rip), %r8
leaq .LC1(%rip), %rdx
leaq .LC2(%rip), %rcx
movl $1, %r9d
movl %eax, 32(%rsp)
call printf
movl var2.3086(%rip), %eax
leaq .LC3(%rip), %rcx
movl $2, %edx
leal 1(%rax), %r8d
movl %r8d, var2.3086(%rip)
addq $56, %rsp
jmp printf
.seh_endproc
.section .text.unlikely,"x"
.LCOLDE4:
.text
.LHOTE4:
.def __main; .scl 2; .type 32; .endef
.section .text.unlikely,"x"
.LCOLDB5:
.section .text.startup,"x"
.LHOTB5:
.p2align 4,,15
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
subq $40, %rsp
.seh_stackalloc 40
.seh_endprologue
call __main
call myFunc
call myFunc
xorl %eax, %eax
addq $40, %rsp
ret
.seh_endproc
.section .text.unlikely,"x"
.LCOLDE5:
.section .text.startup,"x"
.LHOTE5:
.data
.align 4
var2.3086:
.long 8
.ident "GCC: (Rev1, Built by MSYS2 project) 5.4.0"
.def printf; .scl 2; .type 32; .endef
This is looking a little more like the original. var1 is still optimized to just constants, but var2 is now in the .data section again. "Hello" and "World" are still in the .rdata section because they are constant.
One of the comments points out that this would be different on different platforms with different compilers. I encourage you to try it out.