Global initialized variables declared as “const” go to text segment, while those declared “Static” go to data segment. Why?

孤街醉人 提交于 2019-12-05 06:28:41

You're confused. There is no dichotomy between const and static; the two are independent. Assuming all data is initialized, both static const and external (global) const will go in text and both non-const-qualified static and non-const-qualified external will go in data.

As for bss, modern binary formats like ELF actually have separate bss for constant and nonconstant zero data. The output of the size command just doesn't show it.

From Kernighan & Ritchie:

static is a storage class specifier. Other storage class specifiers are: auto, register, extern & typedef. The static specifier gives the declared objects static storage class. The static declaration, applied to an external variable or function, limits the scope of that object to the rest of the source file being compiled. Static objects may be local to a block or external to all blocks, but in either case retain their values across exit from and reentry to functions and blocks.

Whereas,

const is a type qualifier. The other type qualifier is volatile. The purpose of const is to announce objects that may be placed in read-only memory, and perhaps to increase opportunities for optimization.

I suppose one can infer that both these keywords serve distinct purposes; that of const variables being in text/code segment is quite clear from its purpose.

To allow memory protection to work. Any attempt to write to a const will trigger a segfault.

Praetorian

When you declare a variable const you're telling the compiler that you never intend to change its value. On the other hand, with the static declaration made at the file scope, you're telling the compiler that that variable is private to the compilation unit it has been declared in, but functions within that compilation unit are still allowed to modify this variable.

As Oli mentions in his answer, locating the const variable in the text segment allows the system to enforce memory access protection. Also, consider an embedded system, in that case the text segment is usually written to flash and is thus non-modifiable. The data, bss segments etc. are located in RAM and their modification is allowed.

By putting the const data into the text section, the compiler is trying to enforce constness.

Keep in mind that the TEXT section is loaded into memory pages marked read only in the MMU page tables. This is to guard against accidental corruption of code. By putting const data in the same area, makes that data read only as well. Any writes to this data will then invoke exceptions.

Uninitialized data declared static will go into BSS segment to conserve space in the executable file. This area is allocated in memory by the loader. Initialized data declared static will go into the DATA segment, which is read-write.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!