How to force an array literal into a read-only data segment?

Max Samukha maxsamukha at
Thu Sep 12 07:04:19 UTC 2019

__gshared t = "text".ptr;

As expected, the "text" literal ends up in a read-only data 
segment, with a pointer to it stored in a writable data segment 
(_TMP0 pointing into .rodata.str1.1):

.data   segment
         dd      offset FLAT:_TMP0 at 64
         db      000h,000h,000h,000h     ;....
.data   ends

Hex dump of section '.rodata.str1.1':
   0x00000000 74657874 00                         text.

How to achieve the same for an array literal? The closest I could 

enum immutable(int[3]) _tmp = [1, 2, 3];
__gshared a = _tmp.ptr;

But the array is still placed into the writable segment:

.data   segment
         db      001h,000h,000h,000h,002h,000h,000h,000h ;........
         db      003h,000h,000h,000h,000h,000h,000h,000h ;........
         dd      offset FLAT:internal at 64
         db      000h,000h,000h,000h     ;....
.data   ends

Is it possible to force the array into rodata?

More information about the Digitalmars-d-learn mailing list