Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-base

Source of release 0.41.0

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

#==============================================================================================
#
#   object - How arm64 code loads an address on Mach-O
#
#   DESCRIPTION:
#       A symbol's or a local label's address through its page, a library symbol's through
#       the GOT, and a thread-local's through its descriptor. Everything else about the
#       object format is the shared `objects.Format`.
#
#==============================================================================================

import back.native.objects
import support.buffer

## `adrp` and `add` to the address of a symbol or a local label in `reg`.
pub func page_address(object_format: const objects.Format*, out: buffer.Buffer*, reg: str, name: str, is_symbol: bool) -> !:
    try line(out, "adrp ")
    try out.put(reg)
    try out.put(", ")
    try put_name(object_format, out, name, is_symbol)
    try out.put("@PAGE")
    try line(out, "add ")
    try out.put(reg)
    try out.put(", ")
    try out.put(reg)
    try out.put(", ")
    try put_name(object_format, out, name, is_symbol)
    try out.put("@PAGEOFF")

## `name`, behind the symbol prefix when it is a symbol rather than a local label.
func put_name(object_format: const objects.Format*, out: buffer.Buffer*, name: str, is_symbol: bool) -> !:
    if is_symbol:
        try object_format.symbol(out, name)
    else:
        try out.put(name)

## The address of a function or another image's data through the GOT, so a symbol
## defined in a linked shared library resolves.
pub func got_address(object_format: const objects.Format*, out: buffer.Buffer*, reg: str, name: str) -> !:
    try line(out, "adrp ")
    try out.put(reg)
    try out.put(", ")
    try object_format.symbol(out, name)
    try out.put("@GOTPAGE")
    try line(out, "ldr ")
    try out.put(reg)
    try out.put(", [")
    try out.put(reg)
    try out.put(", ")
    try object_format.symbol(out, name)
    try out.put("@GOTPAGEOFF]")

## The address of a thread-local into x0, x16 clobbered: Mach-O's descriptor and its
## accessor.
pub func thread_address(object_format: const objects.Format*, out: buffer.Buffer*, name: str) -> !:
    try line(out, "adrp x0, ")
    try object_format.symbol(out, name)
    try out.put("@TLVPPAGE")
    try line(out, "ldr x0, [x0, ")
    try object_format.symbol(out, name)
    try out.put("@TLVPPAGEOFF]")
    try line(out, "ldr x16, [x0]")
    try line(out, "blr x16")

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