How to create an immovable character

How can I create an immovable character with the last (with PIC enabled) gcc? I basically want to have the following C version of the NULL program:

#include <stdio.h>

extern int mem_;

int main(void) {
  printf("%p\n", &mem_);
  return 0;
}

What I tried is a small assembler file:

    .data
    .globl  mem_
    .type mem_,@object
    .set mem_, 0

but this creates a roaming character that does not have a value of 0 at runtime.

Background: I'm trying to run an old program that uses this trick to directly access (allocated) Fortran memory as an array. Since the program has "10⁵ LOC", it would be impossible to rewrite everything.

[edit] The GNU Assembler Guide documents the "absolute section" as

. 0 "" 0. , , ld . , "": .

, , , (?), . .struct " " ; :

    .globl  mem_
    .struct 0
mem_:   

*ABS* objdump:

$ objdump -t memc

memc:     file format elf64-x86-64
[...]
0000000000000540 g     F .text  000000000000002b              _start
0000000000201030 g       .bss   0000000000000000              __bss_start
000000000000064a g     F .text  000000000000003c              main
0000000000000000 g       *ABS*  0000000000000000              mem_
[...]

.

+4
1

"fixedloc" :

#include <stdio.h>
int mem_ __attribute__ ((section ("fixedloc")));

int main (void) {
        printf("%p\n", &mem_);
        return 0;
}

/

gcc -O2 mem.c -o mem -Wl,--section-start=fixedloc=0x1230000

$ nm mem
...
0000000001230000 D mem
...
$ ./mem
0x1230000

, 0 linux, /proc/sys/vm/mmap _min_addr 0 ( 65536), . .

+4

Source: https://habr.com/ru/post/1687201/


All Articles