How Globals are written to the object files by Compilers?


Notes

  • $gp value can be
    • a fixed address (static or pic relocation with static link), the start of global address table.
      • All global varaible names in C are statically linked 1
    • a dyn loaded address (pic relocation with dynamic link)

Reference: Cpu0 – Global Variables

The global variable DAG translation is different from local variable ones. It creates IR DAG nodes at run time in backend C++ code according to llc -relocation-model option while the other DAG just do IR DAG to Machine DAG translation directly according to the input file of IR DAGs(except the Pseudo instruction REtLR used in Chapter 3_4).

Just like Mips, Cpu0 supports both static and pic mode:

  • relocation-model=pic: (default) Use position independent address.
  • relocation-model=static: Use absolute address.

Two different layouts of global variables for each mode:

  • -cpu0-use-small-section=false: (default) Will be put in .data or .bss, 32 bits addressable.
  • -cpu0-use-small-section=true: .sdata or .sbss, 16 bits addressable.

When relocation-model=static:

option: cpu0-use-small-section false true
addressing mode absolute $gp relative
addressing absolute $gp + offset
Legalized selection DAG (add Cpu0ISD::Hi Cpu0ISD::Lo) (add register %GP, Cpu0ISD::GPRel)
Cpu0 lui $2, %hi(gl); ori $2, $2, %lo(gl); ori $2, $gp, %gp_rel(gl);
relocation records solved link time link time
  • In static, cpu0-use-small-section=true, offset between gl and .data can be calculated since the $gp is assigned at fixed address – the start of global address table.
  • In general relocation-model is used to generate either Absolute Addressing or Position Independent Addressing. The exception is -relocation-model=static and -cpu0-use-small-section=false. In this case, Cpu0 uses $gp relative addressing in this mode.

When relocation-model=pic:

option: cpu0-use-small-section false true
addressing mode $gp relative $gp relative
addressing $gp + offset $gp + offset
Legalized selection DAG (load (Cpu0ISD::Wrapper register %GP, )) (load EntryToken, (Cpu0ISD::Wrapper (add Cpu0ISD::Hi, Register %GP), Cpu0ISD::Lo))
Cpu0 ld $2, %got(gl)($gp); lui $2, %got_hi(gl); add $2, $2, $gp; ld $2, $got_lo(gl)($2);
relocation records solved link/load time link/load time
  • In pic, if the function is loaded at run time (dynamic link), offset between gl and .data cannot be calculated; if use static link, the offset can be calculated;
  • In C, all variable names binding statically. In C++, the overload variable or function are binding dynamically.
  • More dynamic loading (with OS layer): Lindkers and Loaders

Global variable print

Chapter6_1/Cpu0MCInstLower.cpp

Created Jul 13, 2020 // Last Updated Jul 15, 2020

If you could revise
the fundmental principles of
computer system design
to improve security...

... what would you change?