Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-base

Source of release 0.41.0

0.41.0 / src / back / native / x86_64 / object.lucb

#==============================================================================================
#
#   object - How x86-64 code loads an address, on ELF and COFF
#
#   DESCRIPTION:
#       An external symbol's address (through the GOT on ELF) and a thread-local's (the
#       initial-exec model, or COFF's TLS index). Everything else about the object format
#       is the shared `objects.Format`.
#
#==============================================================================================

import back.native.objects
import back.target as build_target
import support.buffer

## Load the address of external symbol `name` into `reg`, through the GOT on ELF.
pub func external_address(object_format: const objects.Format*, out: buffer.Buffer*, name: str, reg: str) -> !:
    # a function's address, or another image's data (`import_ref`), goes through the
    # GOT, so a symbol defined in a linked shared object resolves
    try line(out, "leaq " if object_format.os == build_target.Os.windows else "movq ")
    try object_format.symbol(out, name)
    try out.put("(%rip), " if object_format.os == build_target.Os.windows else "@GOTPCREL(%rip), ")
    try out.put(reg)

## Load the address of thread-local `name` under the initial-exec model.
pub func thread_address(object_format: const objects.Format*, out: buffer.Buffer*, name: str) -> !:
    # the initial-exec model: the thread pointer plus the variable's offset in
    # the static thread block, which the linker writes into the GOT
    if object_format.os == build_target.Os.windows:
        try line(out, "movl _tls_index(%rip), %eax")
        try line(out, "movq %gs:88, %r11")
        try line(out, "movq (%r11,%rax,8), %rax")
        try line(out, "leaq ")
        try object_format.symbol(out, name)
        try out.put("@SECREL32(%rax), %rax")
    else:
        try line(out, "movq %fs:0, %rax")
        try line(out, "addq ")
        try object_format.symbol(out, name)
        try out.put("@GOTTPOFF(%rip), %rax")

func line(out: buffer.Buffer*, text: str) -> !:
    try out.put("\n    ")
    try out.put(text)