为什么不将 const char 放入 rodata 段?

why is not const char put in rodata segment?

有这个:

#include <stdio.h>
#include <stdlib.h>

void f(const char *str){
    char *p = (char*)str;
    *p=97;
}

int main(){
    char c;
    f(&c);

    char *p = malloc(10);
    if (p) { f(p); printf("p:%s\n",p); free(p); }

    const char d = 0; //only this part in interest
    f(&d); // here the function modifies the the char, but since it is NOT in rodata, no problem
    printf("d:%c\n",d);

    printf("c:%c\n",c);
}

会产生气体:

...
.L3:
# a.c:16:   const char d = 0;
    movb    [=11=], -10(%rbp)   #, d
# a.c:17:   f(&d);
    leaq    -10(%rbp), %rax #, tmp98
    movq    %rax, %rdi  # tmp98,
    call    f   #
# a.c:18:   printf("d:%c\n",d);
    movzbl  -10(%rbp), %eax # d, d.0_1
    movsbl  %al, %eax   # d.0_1, _2
    movl    %eax, %esi  # _2,
    leaq    .LC1(%rip), %rdi    #,
    movl    [=11=], %eax    #,
    call    printf@PLT  #
# a.c:20:   printf("c:%c\n",c);
...

这里,d const char变量只是mov入栈,但它的名字(rip位置)不在.section .rodata中,这是为什么呢?当它有 const 修饰符时。因为它是 char* 字符串,所以它 自动放在 rodata 上(char* 甚至不需要 const 修饰符)。我在某处读到 constness is inherited(这意味着一旦用 const 修饰符声明了一个变量,那么即使强制转换导致 cast-away-constness,也不会改变 constness - 即它会保留)。但是这里甚至没有考虑 const char 修饰符(直接通过堆栈操作,就像数组一样)。为什么?

变量d不是静态变量,而是函数局部变量。如果包含它的函数被多次调用(递归地,或者在多个线程中并发地),你会得到变量的多个实例(在函数的堆栈帧内),每个实例都有自己的地址,即使它们都是包含相同的数据。 C 标准要求这些实例是不同的。如果您将变量定义为 static,编译器可能会将其移动到 .rodata 部分,这样您就只会得到一个实例。

字符串字面量(例如 "foo")出现在(递归)函数中时不需要具有单独的地址(除非它们用于初始化 char 数组),因此编译器通常将它们放入 .rodata 部分。