/// tinylinker 0.1.0
///
/// Generated from 1032c65. Edit the sources, not this file.
///
/// Define TINYLINKER_IMPLEMENTATION in exactly one translation unit:
///
///     #define TINYLINKER_IMPLEMENTATION
///     #include "tinylinker.h"
///
/// That unit carries the 64-bit core and links for x86_64-elf, arm64-elf,
/// riscv64-elf, x86_64-pe, arm64-pe, x86_64-macho and arm64-macho. The
/// 32-bit targets, i386-elf, arm-elf and i386-pe, need a second unit with
/// TINYLINKER_BACKEND and TINYLINKER_CORE32 defined, named in the first unit
/// with TINYLINKER_ALSO_CORE32 so the registry expects them; the two cores
/// define the same internal names and cannot share a unit.

#ifndef TINYLINKER_H
#define TINYLINKER_H

#include <stddef.h>
#include <stdint.h>

/// ---- include/tinylinker.h ----



/// ---------------------------------------------------------------------
/// @section Linkage
/// How the entry points below are marked for export, which is the one thing
/// in this header a project may want to decide for itself.

/// Marks the entry points that leave the library, which is otherwise compiled
/// with hidden visibility. Define TL_USE_SHARED before including this header
/// when linking against tinylinker as a DLL on Windows.
/// @note Define it yourself to override the whole scheme, for a build that
///     wants every entry point static or annotated some other way.
#ifndef TL_API
# if defined _WIN32
#  if defined TL_BUILD_SHARED
#   define TL_API __declspec(dllexport)
#  elif defined TL_USE_SHARED
#   define TL_API __declspec(dllimport)
#  else
#   define TL_API
#  endif
# elif defined __GNUC__
#  define TL_API __attribute__((visibility("default")))
# else
#  define TL_API
# endif
#endif

#ifdef __cplusplus
extern "C" {
#endif

/// ---------------------------------------------------------------------
/// @section Targets
/// A target is an architecture paired with an object format, and each pair is
/// a separate backend. A build may leave some out, so ask what is there
/// rather than assuming.

/// What a link produces code for. The numbering is part of the ABI: a value
/// read out of a saved configuration still means the same target.
typedef enum tl_Target {
    /// The machine this library is running on. tl_new() resolves it to the
    /// backend built for this host, and it is the only value for which the
    /// result can be relocated into memory and run rather than only written
    /// out. Zero, so a calloc'd configuration asks for the host.
    /// @see tl_target_native
    TL_TARGET_HOST = 0,

    TL_TARGET_I386_ELF,     /// "i386-elf": 32-bit x86, ELF.
    TL_TARGET_X86_64_ELF,   /// "x86_64-elf": 64-bit x86, ELF.
    TL_TARGET_ARM_ELF,      /// "arm-elf": 32-bit ARM, ELF, hard float.
    TL_TARGET_ARM64_ELF,    /// "arm64-elf": AArch64, ELF.
    TL_TARGET_RISCV64_ELF,  /// "riscv64-elf": 64-bit RISC-V, ELF.

    TL_TARGET_I386_PE,      /// "i386-pe": 32-bit x86, Windows PE.
    TL_TARGET_X86_64_PE,    /// "x86_64-pe": 64-bit x86, Windows PE.
    TL_TARGET_ARM64_PE,     /// "arm64-pe": AArch64, Windows PE.

    TL_TARGET_X86_64_MACHO, /// "x86_64-macho": 64-bit x86, Mach-O.
    TL_TARGET_ARM64_MACHO,  /// "arm64-macho": AArch64, Mach-O.

    TL_TARGET__COUNT,       /// One past the last target; sizes an array.

    /// The name TL_TARGET_HOST had before it said what it meant. Kept as an
    /// alias so existing callers still compile; nothing in tinylinker uses
    /// it. It comes after TL_TARGET__COUNT because an alias for zero placed
    /// any earlier would restart the numbering and move every target.
    /// @deprecated Spell it TL_TARGET_HOST. This alias goes away at 1.0.
    TL_TARGET_UNKNOWN = TL_TARGET_HOST
} tl_Target;

/// The name a target is known by on a command line, such as "x86_64-elf".
/// Answered from a table covering every target, so a name comes back whether
/// or not this build has the backend.
/// @param target Any value; one outside the enum is an answer, not a crash.
/// @return The name, or NULL for TL_TARGET_HOST, TL_TARGET__COUNT and
///     anything out of range.
/// @ownership Static storage. Never freed, never invalidated.
/// @see tl_target_from_name
TL_API const char *tl_target_name(tl_Target target);

/// Inverse of tl_target_name(). Every target is recognised, not only the ones
/// this build has, so a front end can tell "no such target" from "not
/// compiled in" and say the right thing.
/// @param name A command-line target name; NULL is accepted and unrecognised.
/// @return The target, or TL_TARGET_HOST when the name matches nothing. That
///     sentinel is also a legal request for the host, so a caller validating
///     user input should reject the empty answer before passing it on.
/// @see tl_target_available
TL_API tl_Target tl_target_from_name(const char *name);

/// The target matching the host this library was built for.
/// @return That target, or TL_TARGET_HOST if the backend for this host was
///     not compiled in. The two are the same value, so use
///     tl_target_available() to tell them apart.
/// @see tl_target_is_native
TL_API tl_Target tl_target_native(void);

/// Whether this build can link for the target at all. A build may be trimmed
/// down to a handful of backends, so ask before offering one.
/// @param target The target to look for.
/// @return Nonzero if the backend is present, zero otherwise.
TL_API int32_t tl_target_available(tl_Target target);

/// Whether the target's output can be relocated into memory and run here,
/// which needs the backend and the host to agree. Everything else can only be
/// written to a file.
/// @param target The target to ask about.
/// @return Nonzero if tl_relocate(), tl_get_symbol() and tl_run() will work
///     for it; zero for a cross target or one this build lacks.
/// @see tl_relocate
TL_API int32_t tl_target_is_native(tl_Target target);

/// Fills 'out' with the compiled-in targets.
/// @param out Where to write them, or NULL to ask for the count alone.
/// @param max How many 'out' has room for. Ignored when 'out' is NULL.
/// @return How many targets this build has, which may be more than 'max':
///     counting continues past the buffer, so one call sizes an array and the
///     next fills it. TL_TARGET__COUNT is always big enough.
/// @note The order is the registry's, not the enum's.
TL_API int32_t tl_targets(tl_Target *out, int32_t max);

/// ---------------------------------------------------------------------
/// @section The linker state
/// A link is a state you create, configure, feed and then ask for output.
/// Nothing else in the library carries state between calls.

/// One link in progress: its settings, its inputs, and once relocated the
/// image they produced. Opaque, because the shape differs per backend.
/// @note One state belongs to one thread. Separate states are independent and
///     may be linked concurrently; the library serialises only the reporting
///     of diagnostics between them.
/// @ownership Created by tl_new(), released by tl_delete(), and never copied:
///     the address is the identity.
typedef struct tl_State tl_State;

/// Receives one diagnostic, already formatted and without a trailing newline.
/// Warnings arrive here too, though only errors are counted.
/// @param opaque Whatever was handed to tl_set_error_func().
/// @param msg The text, prefixed with the input being read when there is one.
/// @ownership 'msg' is freed as soon as the callback returns. Copy it to keep
///     it.
/// @see tl_set_error_func
typedef void tl_ErrorFunc(void *opaque, const char *msg);

/// Creates a linker context.
/// @param target The target to link for, or TL_TARGET_HOST for this machine.
/// @return The context, or NULL if no such backend was built in or memory ran
///     out. The two are not distinguished; tl_target_available() answers the
///     first before you ask.
/// @ownership Yours. Pass it to tl_delete() exactly once.
/// @see tl_delete
TL_API tl_State *tl_new(tl_Target target);

/// Releases the context and any image it relocated into memory.
/// @param s The context, or NULL, so a failed tl_new() needs no test.
/// @warning Everything the state handed out dies with it: pointers from
///     tl_get_symbol(), strings from tl_get_str() and tl_get_output_file(),
///     and the code an in-memory link mapped. Nothing linked in memory may
///     still be running.
TL_API void tl_delete(tl_State *s);

/// The target this context links for, which tl_new() may have chosen.
/// @param s The context.
/// @return Its target, or TL_TARGET_HOST if 's' is NULL.
TL_API tl_Target tl_state_target(const tl_State *s);

/// Routes diagnostics to a callback instead of stderr, which is what an
/// embedder with its own error reporting wants.
/// @param s The context.
/// @param opaque Passed back to 'func' untouched; the library never reads it.
/// @param func The callback, or NULL to go back to stderr.
/// @ownership 'opaque' stays the caller's, and must outlive the state or be
///     cleared before it dies.
/// @see tl_ErrorFunc
TL_API void tl_set_error_func(tl_State *s, void *opaque, tl_ErrorFunc *func);

/// How many errors have been reported so far. Nonzero means the output, if
/// one was written at all, is not to be trusted.
/// @param s The context.
/// @return The count. Warnings are not in it, and the linker stops printing
///     after a hundred errors while carrying on counting.
/// @note tl_relocate() resets it, so read it before relocating rather than
///     after.
TL_API int32_t tl_error_count(const tl_State *s);

/// ---------------------------------------------------------------------
/// @section Output type
/// What the link produces. This is the one setting with an ordering rule
/// attached, because choosing it is also what creates the linker's sections.

#define TL_OUTPUT_MEMORY 1 /// Link in memory, do not write a file.
#define TL_OUTPUT_EXE    2 /// Executable.
#define TL_OUTPUT_OBJ    3 /// Relocatable object, like ld -r.
#define TL_OUTPUT_DLL    4 /// Shared library, DLL or dylib.

/// Selects the output type and creates the linker's sections.
/// @param s The context.
/// @param output_type One of the TL_OUTPUT_* values.
/// @return 0 on success, or a negative value with a diagnostic reported.
/// @warning Ordering: call this after any option that influences it
///     (-shared, -r, -static, -nostdlib) and before adding any input. The
///     sections do not exist until it returns, and an input added before then
///     has nowhere to land.
/// @see tl_get_output_type
TL_API int32_t tl_set_output_type(tl_State *s, int32_t output_type);

/// The output type recorded so far, including one implied by an option such
/// as -shared or -r that a parse has already seen.
/// @param s The context.
/// @return A TL_OUTPUT_* value, or 0 if none has been selected yet.
/// @note This is how a driver learns what tl_parse_args() decided, so it can
///     hand the answer straight to tl_set_output_type().
TL_API int32_t tl_get_output_type(tl_State *s);

/// The -o argument seen by tl_parse_args() or tl_set_options(). A driver uses
/// it to name the output without parsing the arguments twice.
/// @param s The context.
/// @return The name, or NULL if no -o was given.
/// @ownership The state's. Valid until the setting changes or tl_delete().
TL_API const char *tl_get_output_file(tl_State *s);

/// Points the linker at the directory holding the target support files, the
/// equivalent of -B. Sets TL_STR_LIB_PATH.
/// @param s The context.
/// @param path The directory. Copied, so the caller's buffer may go away.
/// @note This is where the compiler runtime archive is looked for, and where
///     tl_run() finds the runmain shim that turns the program's exit() into a
///     return rather than the end of the host process.
TL_API void tl_set_lib_path(tl_State *s, const char *path);

/// Appends a directory to search for -l libraries, the equivalent of -L.
/// @param s The context.
/// @param pathname The directory. Copied. Searched after the ones already
///     added, so order is the command line's.
/// @return 0 on success, or a negative value.
/// @see tl_add_library
TL_API int32_t tl_add_library_path(tl_State *s, const char *pathname);

/// ---------------------------------------------------------------------
/// @section Settings
/// Everything the linker can be told is reachable as a flag, a string or a
/// number, so an embedder that already has its own command line can translate
/// into these rather than build option strings. The string parser further
/// down is kept for callers who are handed ld-style text, such as -Wl,
/// arguments.

/// The boolean settings, combined into the bitmask tl_set_flags() takes.
typedef enum tl_Flag {
    TL_F_STATIC        = 1u << 0,  /// Do not link against shared libraries.
    TL_F_SHARED        = 1u << 1,  /// Produce a shared library.
    TL_F_RELOCATABLE   = 1u << 2,  /// Produce a relocatable object, like -r.
    TL_F_RDYNAMIC      = 1u << 3,  /// Export all symbols dynamically.
    TL_F_SYMBOLIC      = 1u << 4,  /// Resolve within this module first.
    TL_F_NOSTDLIB      = 1u << 5,  /// No startup files or default libraries.
    TL_F_NOSTDLIB_PATHS = 1u << 6, /// Do not search the default library paths.
    TL_F_WHOLE_ARCHIVE = 1u << 7,  /// Take every member of following archives.
    TL_F_NEW_DTAGS     = 1u << 8,  /// Emit DT_RUNPATH rather than DT_RPATH.
    TL_F_NODELETE      = 1u << 9,  /// Set DF_1_NODELETE.
    TL_F_COMMON        = 1u << 10, /// Keep common symbols instead of .bss.
    TL_F_UNDERSCORE    = 1u << 11, /// Prefix C symbols with an underscore.
    TL_F_BACKTRACE     = 1u << 12, /// Emit the runtime context for backtraces.
    TL_F_BOUNDS_CHECK  = 1u << 13  /// Link the bounds checking runtime.
} tl_Flag;

/// Turns the named flags on or off. Flags left out of 'flags' keep the value
/// they had, so settings can be built up in any order.
/// @param s The context.
/// @param flags A tl_Flag bitmask naming which settings to touch.
/// @param on Nonzero to set them, zero to clear them.
/// @return 0 on success, or a negative value.
/// @warning TL_F_SHARED and TL_F_RELOCATABLE are not switches: each of them
///     also selects an output type, and clearing either one leaves the type at
///     TL_OUTPUT_EXE rather than at whatever it was.
/// @note TL_F_BOUNDS_CHECK implies TL_F_BACKTRACE, and both imply debug
///     information, since neither reads without it.
/// @see tl_get_flags
TL_API int32_t tl_set_flags(tl_State *s, uint32_t flags, int32_t on);

/// Every flag currently set, as a tl_Flag bitmask, so a state's switches can
/// be saved and put back.
/// @param s The context.
/// @return The mask. A flag this build compiled out always reads as clear.
TL_API uint32_t tl_get_flags(tl_State *s);

/// The settings that carry a string.
typedef enum tl_StrOpt {
    TL_STR_ENTRY = 1,      /// Entry symbol, as -e.
    TL_STR_SONAME,         /// DT_SONAME of a shared library.
    TL_STR_RPATH,          /// Runtime search path.
    TL_STR_INTERPRETER,    /// ELF interpreter, as --dynamic-linker.
    TL_STR_OUTPUT,         /// Output file name, as -o.
    TL_STR_INIT,           /// Symbol to call at load time.
    TL_STR_FINI,           /// Symbol to call at unload time.
    TL_STR_MAPFILE,        /// Write a link map here.
    TL_STR_INSTALL_NAME,   /// Mach-O install name.
    TL_STR_LIB_PATH        /// Directory holding the target support files.
} tl_StrOpt;

/// Sets a string setting.
/// @param s The context.
/// @param opt Which setting.
/// @param value The value, copied; NULL clears the setting.
/// @return 0 on success, or a negative value with a diagnostic when the
///     setting means nothing to this target, such as TL_STR_INSTALL_NAME
///     anywhere but Mach-O.
/// @see tl_get_str
TL_API int32_t tl_set_str(tl_State *s, tl_StrOpt opt, const char *value);

/// The current value of a string setting.
/// @param s The context.
/// @param opt Which setting.
/// @return The value, or NULL if it is unset or does not apply here.
/// @ownership The state's. Valid until that setting changes or tl_delete().
TL_API const char *tl_get_str(tl_State *s, tl_StrOpt opt);

/// The platform a Mach-O image says it was built for, in the LC_BUILD_VERSION
/// the loader reads before it will run the image at all. The numbers are the
/// ones <mach-o/loader.h> gives, so 5 is missing: that is bridgeOS, which
/// nothing links by hand.
/// @see tl_set_num
typedef enum tl_MachoPlatform {
    TL_MACHO_PLATFORM_MACOS = 1,           /// macOS, and the default.
    TL_MACHO_PLATFORM_IOS = 2,             /// iOS on a device.
    TL_MACHO_PLATFORM_TVOS = 3,            /// tvOS on a device.
    TL_MACHO_PLATFORM_WATCHOS = 4,         /// watchOS on a device.
    TL_MACHO_PLATFORM_MACCATALYST = 6,     /// An iOS image running on macOS.
    TL_MACHO_PLATFORM_IOSSIMULATOR = 7,    /// iOS under the simulator.
    TL_MACHO_PLATFORM_TVOSSIMULATOR = 8,   /// tvOS under the simulator.
    TL_MACHO_PLATFORM_WATCHOSSIMULATOR = 9 /// watchOS under the simulator.
} tl_MachoPlatform;

/// Which operating system an ELF image is stamped for. It is orthogonal to
/// the target rather than part of it: the differences are a handful of header
/// bytes, note sections and startup file names, so making it a setting keeps
/// ten targets from becoming forty. PE and Mach-O accept it and ignore it.
///
/// @see tl_set_num
typedef enum tl_TargetOS {
    TL_OS_LINUX = 0,  /// The default, and what an unset state means.
    TL_OS_FREEBSD,    /// Stamps ELFOSABI_FREEBSD and a .note.tag.
    TL_OS_OPENBSD,    /// Stamps ELFOSABI_OPENBSD and a .note.openbsd.ident.
    TL_OS_NETBSD      /// Stamps ELFOSABI_NETBSD and a .note.netbsd.ident.
} tl_TargetOS;

/// The settings that carry a number. Several mean something to one object
/// format only, and the rest ignore them.
typedef enum tl_NumOpt {
    TL_NUM_TEXT_ADDR = 1,  /// Where .text starts, as -Ttext.
    TL_NUM_SECTION_ALIGN,  /// Section alignment in the output.
    TL_NUM_IMAGE_BASE,     /// PE image base.
    TL_NUM_STACK_SIZE,     /// PE stack reserve.
    TL_NUM_SUBSYSTEM,      /// PE subsystem.
    TL_NUM_COMPAT_VERSION, /// Mach-O compatibility version.
    TL_NUM_CURRENT_VERSION,/// Mach-O current version.
    TL_NUM_VERBOSE,        /// How much to say while working.
    TL_NUM_BACKTRACE_DEPTH,/// How many frames a backtrace keeps.
    TL_NUM_MACHO_PLATFORM, /// Mach-O platform, one of tl_MachoPlatform.
    TL_NUM_MACHO_MINOS,    /// Mach-O minimum OS, (major<<16)|(minor<<8)|patch.
    TL_NUM_TARGET_OS,      /// Which OS the image is for, a tl_TargetOS.
    TL_NUM_STRIP           /// Non-zero to leave local symbols out of the image.
} tl_NumOpt;

/// Sets a numeric setting. Everything is passed as 64 bits; narrowing to what
/// the format can hold is the linker's business, not the caller's.
/// @param s The context.
/// @param opt Which setting.
/// @param value The value, in whatever units that setting counts.
/// @return 0 on success, or a negative value with a diagnostic when the
///     setting means nothing to this target or to this build.
/// @see tl_get_num
TL_API int32_t tl_set_num(tl_State *s, tl_NumOpt opt, uint64_t value);

/// The current value of a numeric setting, or the linker's default for it.
/// @param s The context.
/// @param opt Which setting.
/// @return The value. A setting this target does not have reads as 0 rather
///     than failing, since there is no value to distinguish it with; use the
///     return of tl_set_num() to find out whether one applies.
TL_API uint64_t tl_get_num(tl_State *s, tl_NumOpt opt);

/// Parses a whitespace-separated option string, e.g.
/// "-static -L/usr/lib -Wl,-soname,libfoo.so.1". Useful when the caller is
/// handed ld-style text; otherwise prefer the typed calls above.
/// @param s The context.
/// @param options The text. Not retained.
/// @return 0 on success, or a negative value for an option it cannot parse.
/// @note Options such as -shared and -r select an output type rather than
///     applying one; read it back with tl_get_output_type() and pass it to
///     tl_set_output_type().
TL_API int32_t tl_set_options(tl_State *s, const char *options);

/// Parses an ld-style argv, skipping argv[0] as in main().
/// @param s The context.
/// @param argc How many entries 'argv' has.
/// @param argv The arguments. Not retained.
/// @return 0 on success, or a negative value.
/// @note Input files named on the command line are only queued, not added, so
///     that the output type can still be chosen in between. Nothing is read
///     until tl_link_parsed().
/// @see tl_link_parsed
TL_API int32_t tl_parse_args(tl_State *s, int32_t argc, char **argv);

/// Adds every input file queued by tl_parse_args(), in command-line order.
/// @param s The context.
/// @return 0 on success, or a negative value if any input failed to load.
/// @see tl_parse_args
TL_API int32_t tl_link_parsed(tl_State *s);

/// ---------------------------------------------------------------------
/// @section Inputs
/// Where the linker gets what it links: files on disk, libraries found on the
/// search path, or addresses in this process.

/// Adds an input: an object file, a static archive, a shared library, or a
/// GNU ld script. Which one it is comes from the contents, not the name.
/// @param s The context.
/// @param filename The path, used as given; the library search path does not
///     apply here.
/// @return 0 on success, or a negative value if the file is missing or is not
///     something this target can read.
/// @warning Call tl_set_output_type() first. The sections an input lands in
///     do not exist before then.
/// @see tl_add_library
TL_API int32_t tl_add_file(tl_State *s, const char *filename);

/// Searches the library path for a library by short name, as -l does: "c"
/// finds libc.so or libc.a, and on Windows the import comes from a .dll or a
/// .def rather than an import archive.
/// @param s The context.
/// @param libraryname The name without the "lib" prefix or the extension.
/// @return 0 on success, or a negative value if nothing matched.
/// @note TL_F_STATIC restricts the search to archives.
/// @see tl_add_library_path
TL_API int32_t tl_add_library(tl_State *s, const char *libraryname);

/// Defines a symbol that resolves to memory in this process, so that a
/// linked-in image can call back into its host.
/// @param s The context.
/// @param name The C name. TL_F_UNDERSCORE, and the Mach-O convention, are
///     applied here rather than by the caller.
/// @param val The address. Not dereferenced, only recorded.
/// @return 0 on success, or a negative value.
/// @warning TL_OUTPUT_MEMORY only: an address in this process means nothing
///     in a file that another process will load.
/// @see tl_get_symbol
TL_API int32_t tl_add_symbol(tl_State *s, const char *name, const void *val);

/// ---------------------------------------------------------------------
/// @section Building an object in memory
/// Inputs do not have to come from files. A code generator can hand tinylinker
/// the sections, symbols and relocations it produced and then either write them
/// out as an object or link them together with real files in the same step.

/// A section handle, as returned by tl_add_section(). Zero is never a valid
/// one, which is what lets it double as the failure result.
typedef int32_t tl_Section;

#define TL_SEC_TEXT   1   /// Executable.
#define TL_SEC_RODATA 2   /// Read-only data.
#define TL_SEC_DATA   3   /// Writable data.
#define TL_SEC_BSS    4   /// Zero filled, occupies no space in the file.
#define TL_SEC_DEBUG  5   /// Debug information: present in the file, not loaded.
#define TL_SEC_TDATA  6   /// Each thread's initial copy of its thread-local data.
#define TL_SEC_TBSS   7   /// The same, zero filled, occupying no space in the file.

/// Creates a section, or returns the existing one of that name, so a
/// generator can emit into it in as many pieces as it likes.
/// @param s The context.
/// @param name The section name, or NULL to take the usual one for the kind.
///     TL_SEC_RODATA has no fixed name: the target picks between .rodata and
///     a relro section.
/// @param kind One of the TL_SEC_* values. TL_SEC_DEBUG is the one that
///     occupies space in the file and none in the image: a loader never
///     sees it, and a linker will refuse an object that claims otherwise.
///     TL_SEC_TDATA and TL_SEC_TBSS are .tdata and .tbss on ELF, where a
///     symbol defined in them is STT_TLS, and __thread_data and __thread_bss
///     on Mach-O. A Mach-O variable is reached through a descriptor, which
///     the generator writes into a TL_SEC_DATA section named ".thread_vars".
///     COFF puts both in .tls$, the section the C runtime's TLS directory
///     gathers between its _tls_start and _tls_end, zero filled for the
///     second.
/// @param align Byte alignment. Raises the section's alignment but never
///     lowers it, so the section ends up as strict as the strictest thing put
///     into it.
/// @return The handle, or 0 on failure.
/// @see tl_section_append
TL_API tl_Section tl_add_section(tl_State *s, const char *name, int32_t kind,
                                 int32_t align);

/// Appends bytes to a section.
/// @param s The context.
/// @param sec A handle from tl_add_section().
/// @param data The bytes, copied. Ignored for a TL_SEC_BSS section, which
///     takes the space without the contents.
/// @param len How many bytes.
/// @return The offset they landed at, which is what a later
///     tl_define_symbol() or tl_add_reloc() refers to, or -1 for a handle
///     that is not this state's. The offset is aligned to the section's
///     alignment, so it need not be the previous size.
/// @see tl_define_symbol
TL_API int64_t tl_section_append(tl_State *s, tl_Section sec, const void *data,
                              size_t len);

/// Appends zero-filled space, for a variable with no initialiser. In
/// TL_SEC_BSS this costs nothing in the output; elsewhere the zeroes are
/// written out.
/// @param s The context.
/// @param sec A handle from tl_add_section().
/// @param len How many bytes.
/// @return The offset it landed at, or -1 for a handle that is not this
///     state's.
TL_API int64_t tl_section_zero(tl_State *s, tl_Section sec, size_t len);

/// How much has been appended to a section so far.
/// @param s The context.
/// @param sec A handle from tl_add_section().
/// @return The size in bytes, or -1 for a handle that is not this state's.
TL_API int64_t tl_section_size(tl_State *s, tl_Section sec);

#define TL_SYM_GLOBAL 0x01 /// Visible to other objects, not only this one.
#define TL_SYM_WEAK   0x02 /// A stronger definition elsewhere wins.
#define TL_SYM_FUNC   0x04 /// Code rather than data.

/// Defines a symbol at an offset in a section.
/// @param s The context.
/// @param name The symbol name, spelled exactly as the output should carry
///     it. Unlike tl_add_symbol(), no underscore is added.
/// @param sec A handle from tl_add_section().
/// @param off Where in that section, usually a tl_section_append() result.
/// @param flags TL_SYM_* bits. Neither TL_SYM_GLOBAL nor TL_SYM_WEAK makes
///     the symbol local to this object, and TL_SYM_WEAK wins over
///     TL_SYM_GLOBAL when both are given.
/// @return 0 on success, or a negative value.
/// @see tl_declare_symbol
TL_API int32_t tl_define_symbol(tl_State *s, const char *name, tl_Section sec,
                            uint64_t off, int32_t flags);

/// Names a symbol this object refers to but does not define, so that the link
/// knows to go looking for it.
/// @param s The context.
/// @param name The symbol name.
/// @return 0 on success, or a negative value.
/// @note tl_add_reloc() declares an unknown symbol by itself, so this is only
///     needed for a reference that no relocation records.
TL_API int32_t tl_declare_symbol(tl_State *s, const char *name);

/// What a relocation means, independent of any object format. tinylinker turns
/// these into the target's own relocation types, and reports the ones an
/// architecture cannot express rather than approximating them.
typedef enum tl_RelocKind {
    TL_REL_ABS64 = 1,   /// The symbol's address, 64 bits.
    TL_REL_ABS32,       /// The symbol's address, 32 bits.
    TL_REL_PC32,        /// symbol - here, 32 bits.
    TL_REL_CALL,        /// A call or jump to the symbol.
    TL_REL_PAGE21,      /// arm64: the ADRP page of the symbol.
    TL_REL_PAGEOFF12,   /// arm64: the low bits, for the paired ADD or LDR.
    TL_REL_HI20,        /// riscv: the AUIPC half of a pc-relative address.
    TL_REL_LO12_I,      /// riscv: the low half, in an I-type instruction.
    TL_REL_LO12_S,      /// riscv: the low half, in an S-type instruction.

    /// COFF only, and wanted by CodeView: an address expressed as an
    /// offset within its own section, and the index of that section.
    /// Debug records are written that way because a debugger reads them
    /// before anything has been given a final address.
    TL_REL_SECREL32 = 10, /// The symbol's offset within its section.
    TL_REL_SECTION = 11,  /// The one-based index of the symbol's section.

    /// A load of the word that locates a thread-local variable. ELF reads
    /// it as the initial-exec offset from the thread pointer, through the
    /// GOT; Mach-O as the address of the variable's descriptor.
    TL_REL_TLS_PAGE21 = 12,   /// arm64: the ADRP page of that word.
    TL_REL_TLS_PAGEOFF12 = 13, /// arm64: the low bits, for the paired LDR.
    TL_REL_TLS_PC32 = 14,     /// x86-64: a rip-relative MOV of that word.

    /// A load of the GOT slot holding a symbol's address, which is how code
    /// reaches one a shared library defines.
    TL_REL_GOT_PAGE21 = 15,   /// arm64: the ADRP page of the slot.
    TL_REL_GOT_PAGEOFF12 = 16, /// arm64: the low bits, for the paired LDR.
    TL_REL_GOT_PC32 = 17,     /// x86-64: a rip-relative MOV of the slot.

    /// RISC-V's AUIPC halves of the two loads above. The paired LD takes
    /// TL_REL_LO12_I, naming a label on the AUIPC as it does for an address.
    TL_REL_TLS_HI20 = 18,     /// riscv: the AUIPC of the initial-exec slot.
    TL_REL_GOT_HI20 = 19,     /// riscv: the AUIPC of the GOT slot.

    /// A thread-local's offset within the module's TLS block, which on COFF
    /// is its offset within .tls. x86-64 reaches it with TL_REL_SECREL32,
    /// and arm64 builds it with a pair of ADDs, the first shifted by twelve.
    TL_REL_SECREL_HI12 = 20,  /// arm64: bits 12 to 23 of that offset.
    TL_REL_SECREL_LO12 = 21   /// arm64: bits 0 to 11 of it.
} tl_RelocKind;

/// Records that the bytes at 'off' need the address of 'sym', biased by
/// 'addend'.
/// @param s The context.
/// @param sec A handle from tl_add_section().
/// @param off Where in that section the patch goes.
/// @param sym The symbol wanted. It does not have to be defined yet; one that
///     is not known is declared undefined here.
/// @param kind What the patched bytes should end up holding.
/// @param addend Added to the symbol's address before patching.
/// @return 0 on success, or a negative value, including for a 'kind' this
///     architecture has no relocation for.
/// @see tl_RelocKind
TL_API int32_t tl_add_reloc(tl_State *s, tl_Section sec, uint64_t off,
                        const char *sym, tl_RelocKind kind, int64_t addend);

/// Records that the bytes at 'off' need the address of another section rather
/// than of a symbol.
///
/// DWARF is what wants this. A compile unit refers to its line table, its
/// abbreviations and its strings by an offset from the front of the section
/// holding them, and there is no symbol there to name: the reference is to
/// .debug_line itself. Written as a plain number it is right only while an
/// object holds a single compile unit, because the linker concatenates the
/// debug sections and everything after the first one moves.
///
/// What comes out is a relocation against the section: an STT_SECTION symbol
/// on ELF, the section's own static symbol on COFF, and a relocation with
/// r_extern clear on Mach-O. All three are what a compiler emits for
/// DW_AT_stmt_list.
///
/// @param s The context.
/// @param sec A handle from tl_add_section(), the section holding the field.
/// @param off Where in that section the patch goes.
/// @param target A handle from tl_add_section(), the section named.
/// @param kind TL_REL_ABS64 or TL_REL_ABS32, and TL_REL_SECREL32 on COFF.
///     A pc-relative or paged kind describes an instruction and is refused.
/// @param addend Added to the section's address before patching, which for a
///     debug reference is the offset within the section named.
/// @return 0 on success, or a negative value, including for a handle that
///     names no section and for a kind this format cannot express.
/// @note For an object, which is what a debug section is written into. In an
///     executable the sections have final addresses and the reference is
///     resolved rather than recorded.
/// @see tl_add_reloc()
TL_API int32_t tl_add_section_reloc(tl_State *s, tl_Section sec, uint64_t off,
                        tl_Section target, tl_RelocKind kind, int64_t addend);

/// ---------------------------------------------------------------------
/// @section Output
/// Two ways to finish: write a file, or relocate into this process and call
/// what came out.

/// Links everything added so far and writes the result to 'filename', in the
/// shape tl_set_output_type() chose.
/// @param s The context.
/// @param filename Where to write. Overwritten if it exists.
/// @return 0 on success, or a negative value. Check tl_error_count() as well:
///     a diagnostic reported earlier means the output is not to be trusted
///     even if the write itself worked.
/// @see tl_relocate
TL_API int32_t tl_output_file(tl_State *s, const char *filename);

/// Links into memory rather than into a file, which is what tl_get_symbol()
/// and tl_run() need first.
/// @param s The context.
/// @return 0 on success, or -1 for a cross target, for a second call on the
///     same state, or for a link that failed.
/// @warning Native targets only, since you cannot execute arm64 code on an
///     x86-64 machine. Ask tl_target_is_native() rather than finding out here.
/// @note It clears the error count on the way in, so a state that reported
///     errors while reading its inputs looks clean afterwards.
/// @ownership The image belongs to the state and is unmapped by tl_delete().
/// @see tl_get_symbol
TL_API int32_t tl_relocate(tl_State *s);

/// The address of a symbol in the in-memory image.
/// @param s The context.
/// @param name The C name; the target's own spelling, such as the leading
///     underscore Mach-O uses, is applied here.
/// @return The address, or NULL if the image has no such symbol. Only
///     meaningful after tl_relocate().
/// @ownership Points into the state's image. Dead after tl_delete().
/// @see tl_relocate
TL_API void *tl_get_symbol(tl_State *s, const char *name);

/// Calls 'symbol_cb' once per global symbol of the in-memory image, which is
/// how a host discovers what it just linked without knowing the names.
/// @param s The context.
/// @param ctx Passed back to the callback untouched.
/// @param symbol_cb Called with 'ctx', the symbol's name and its address.
///     Symbols with no address, and ones another module has no business
///     seeing, are skipped rather than reported.
/// @ownership The name belongs to the state's symbol table; copy it to keep
///     it past tl_delete().
/// @see tl_get_symbol
TL_API void tl_list_symbols(tl_State *s, void *ctx,
    void (*symbol_cb)(void *ctx, const char *name, const void *val));

/// Links in memory, then calls main(argc, argv) and returns its exit status.
/// The image stays alive until tl_delete().
/// @param s The context.
/// @param argc How many entries 'argv' has, as main() would see it.
/// @param argv The arguments, as main() would see them.
/// @return The program's exit status, or -1 if the link or the lookup failed.
///     A program that exits with -1 is indistinguishable from that.
/// @warning Without a support library at tl_set_lib_path(), the entry point is
///     called directly, so a program that calls exit() ends the host process
///     rather than returning here.
/// @see tl_relocate
TL_API int32_t tl_run(tl_State *s, int32_t argc, char **argv);

/// The version of the library, as "major.minor.patch".
/// @return The version string.
/// @ownership Static storage; the same pointer every time.
TL_API const char *tl_version(void);

#ifdef __cplusplus
}
#endif

#endif // TINYLINKER_H

#if defined(TINYLINKER_IMPLEMENTATION) || defined(TINYLINKER_BACKEND)
#ifndef TINYLINKER_PACKED_BODY
#define TINYLINKER_PACKED_BODY

#ifndef TL_VERSION
# define TL_VERSION "0.1.0"
#endif

/// Set before the first system header, which is the only
/// point at which these decide anything.
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#ifndef _DARWIN_C_SOURCE
#define _DARWIN_C_SOURCE
#endif

#include <errno.h>
#include <fcntl.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/// ---- core selection ----

/// One core per translation unit: the core's internal names are static to
/// their unit, and the other core would collide with every one of them.
#ifdef TINYLINKER_CORE32
# define PTR_SIZE 4
# define TL_WITH_CORE32 1
#else
# define PTR_SIZE 8
# define TL_WITH_CORE64 1
#endif

/// A core built in another unit still belongs in the registry.
#ifdef TINYLINKER_ALSO_CORE32
# define TL_WITH_CORE32 1
#endif
#ifdef TINYLINKER_ALSO_CORE64
# define TL_WITH_CORE64 1
#endif

/// ---- the linker ----

/// ---- src/tl_backend.h ----



/// ---- src/core/tl_types.h ----



/// A program compiling tinylinker's implementation into a translation unit
/// that already declares these names defines TL_HAVE_SHORT_TYPES, since C99
/// refuses the same typedef twice.
#ifndef TL_HAVE_SHORT_TYPES
typedef int8_t   i8;    /// Signed 8-bit.
typedef int16_t  i16;   /// Signed 16-bit.
typedef int32_t  i32;   /// Signed 32-bit.
typedef int64_t  i64;   /// Signed 64-bit.

typedef uint8_t  u8;    /// A byte.
typedef uint16_t u16;   /// Unsigned 16-bit.
typedef uint32_t u32;   /// Unsigned 32-bit.
typedef uint64_t u64;   /// Unsigned 64-bit.

typedef ptrdiff_t isize; /// Signed, wide enough for any offset in memory.
typedef size_t    usize; /// Unsigned, wide enough for any object's size.

typedef float  f32;     /// IEEE single precision.
typedef double f64;     /// IEEE double precision.
#endif


/// What one backend exports: a description of the target and a thunk per
/// public call. Widths that reach the published prototypes keep their
/// spelling, so that the two cannot drift apart unnoticed.
typedef struct tl_Backend {
    const char *name;   /// The command-line name, e.g. "x86_64-elf".
    tl_Target target;   /// Which tl_Target this backend answers to.
    i32 is_native;      /// Backend target == host: can relocate/run in memory.

    void *(*create)(void);                  /// Allocate state; NULL if it fails.
    void  (*destroy)(void *st);             /// Release state and any image.
    void  (*set_error_func)(void *st, void *opaque, tl_ErrorFunc *func);
    i32   (*error_count)(void *st);         /// Diagnostics reported so far.

    /// Configuration. Each of these backs the tl_ call of the same name, so
    /// only the ones that do something else carry a comment.
    i32   (*set_output_type)(void *st, i32 output_type);
    i32   (*get_output_type)(void *st);
    const char *(*get_output_file)(void *st);
    void  (*set_lib_path)(void *st, const char *path);
    i32   (*add_library_path)(void *st, const char *path);
    i32   (*set_options)(void *st, const char *options);
    i32   (*set_flags)(void *st, u32 flags, i32 on);
    u32   (*get_flags)(void *st);
    i32   (*set_str)(void *st, i32 opt, const char *value);
    const char *(*get_str)(void *st, i32 opt);
    i32   (*set_num)(void *st, i32 opt, u64 value);
    u64   (*get_num)(void *st, i32 opt);
    i32   (*parse_args)(void *st, i32 argc, char **argv);
    i32   (*link_parsed)(void *st);         /// Consume what parse_args queued.

    /// Inputs, whether they come from the filesystem or from the host's
    /// own memory.
    i32   (*add_file)(void *st, const char *filename);
    i32   (*add_library)(void *st, const char *name);
    i32   (*add_symbol)(void *st, const char *name, const void *val);

    /// Building an object in memory. The offsets these return are 64 bits
    /// wide because tl_section_append() and friends publish them as int64_t,
    /// which is one width everywhere rather than long's two.
    i32   (*add_section)(void *st, const char *name, i32 kind, i32 align);
    i64   (*section_append)(void *st, i32 sec, const void *data, usize len);
    i64   (*section_zero)(void *st, i32 sec, usize len);
    i64   (*section_size)(void *st, i32 sec);
    i32   (*define_symbol)(void *st, const char *name, i32 sec,
                           u64 off, i32 flags);
    i32   (*declare_symbol)(void *st, const char *name);
    i32   (*add_reloc)(void *st, i32 sec, u64 off,
                       const char *sym, i32 kind, i64 addend);

    /// The same where what is named is another section rather than a symbol,
    /// which is how a debug section refers to the front of another one.
    i32   (*add_section_reloc)(void *st, i32 sec, u64 off,
                               i32 target, i32 kind, i64 addend);

    i32   (*output_file)(void *st, const char *filename);

    /// The four below are NULL on a cross backend, which has no host to
    /// relocate into. The dispatcher checks before calling them.
    i32   (*relocate)(void *st);
    void *(*get_symbol)(void *st, const char *name);
    void  (*list_symbols)(void *st, void *ctx,
                          void (*cb)(void *ctx, const char *name, const void *val));
    i32   (*run)(void *st, i32 argc, char **argv);
} tl_Backend;

/// Every backend that is compiled in defines exactly one of these.  The
/// dispatcher in tinylinker.c declares them through the table in
/// tl_backends.h, which lists them by the core that defines them.


#ifdef TINYLINKER_IMPLEMENTATION

/// ---- src/tinylinker.c ----

/// tinylinker - public API and backend dispatch
///
/// Everything here is target-agnostic: it picks a backend (one per
/// architecture/object-format pair) and forwards through its vtable.
///
/// The tl_ entry points below are spelled exactly as include/tinylinker.h
/// publishes them, standard type names and all, because a definition has to
/// match its declaration. Only what stays inside the file uses the aliases.



/// ---- src/tl_backends.h ----


/// Nothing selected at all means nobody chose, rather than that everything
/// was turned off, so fall back to both cores.
#if !defined TL_WITH_CORE64 && !defined TL_WITH_CORE32
# define TL_WITH_CORE64 1
# define TL_WITH_CORE32 1
#endif

/// TL_B64(X, s) and TL_B32(X, s) expand to X(s) when that core is built and
/// to nothing when it is not.  Splitting the test out per core is what keeps
/// TL_BACKENDS(X) below free of preprocessor.
#ifdef TL_WITH_CORE64
# define TL_B64(X, s) X(s)
#else
# define TL_B64(X, s)
#endif
#ifdef TL_WITH_CORE32
# define TL_B32(X, s) X(s)
#else
# define TL_B32(X, s)
#endif

/// Applies X(symbol_suffix) to each enabled backend, in tl_Target order, so
/// that the dispatcher can declare and tabulate them without repeating the
/// list twice.
#define TL_BACKENDS(X)        \
    TL_B32(X, i386_elf)       \
    TL_B64(X, x86_64_elf)     \
    TL_B32(X, arm_elf)        \
    TL_B64(X, arm64_elf)      \
    TL_B64(X, riscv64_elf)    \
    TL_B32(X, i386_pe)        \
    TL_B64(X, x86_64_pe)      \
    TL_B64(X, arm64_pe)       \
    TL_B64(X, x86_64_macho)   \
    TL_B64(X, arm64_macho)


/// xmake generates tl_buildconfig.h; -DTL_VERSION=... replaces it when the
/// tree is compiled without a build system.
#ifndef TL_VERSION
# include "tl_buildconfig.h"
#endif

/// ---------------------------------------------------------------------
/// Names the vtable each enabled backend exports. Its definition lives in
/// that backend's own translation unit and nowhere else.
#define TL_DECLARE(sym) extern const tl_Backend tl_backend_##sym;
TL_BACKENDS(TL_DECLARE)
#undef TL_DECLARE

/// Adds one backend to the registry below.
#define TL_ENTRY(sym) &tl_backend_##sym,

/// Every backend this build has, NULL terminated so that the walks below do
/// not need a count alongside.
static const tl_Backend *const tl_backends[] = {
    TL_BACKENDS(TL_ENTRY)
    NULL
};
#undef TL_ENTRY

/// The command-line name of every target, present or not. Indexed by
/// tl_Target, so a target missing from this build still has a name.
static const char *const tl_target_names[TL_TARGET__COUNT] = {
    [TL_TARGET_HOST]          = NULL,
    [TL_TARGET_I386_ELF]      = "i386-elf",
    [TL_TARGET_X86_64_ELF]    = "x86_64-elf",
    [TL_TARGET_ARM_ELF]       = "arm-elf",
    [TL_TARGET_ARM64_ELF]     = "arm64-elf",
    [TL_TARGET_RISCV64_ELF]   = "riscv64-elf",
    [TL_TARGET_I386_PE]       = "i386-pe",
    [TL_TARGET_X86_64_PE]     = "x86_64-pe",
    [TL_TARGET_ARM64_PE]      = "arm64-pe",
    [TL_TARGET_X86_64_MACHO]  = "x86_64-macho",
    [TL_TARGET_ARM64_MACHO]   = "arm64-macho",
};

/// The backend for a target, or NULL if this build left it out. Ten entries
/// at most, so a linear walk is cheaper than anything with an index.
static const tl_Backend *find_backend(tl_Target target)
{
    i32 i;

    for (i = 0; tl_backends[i]; i++)
        if (tl_backends[i]->target == target)
            return tl_backends[i];

    return NULL;
}

/// ---------------------------------------------------------------------
/// Answers from the name table rather than the registry, so that a target
/// this build omitted still has a name to print.
const char *tl_target_name(tl_Target target)
{
    if (target <= TL_TARGET_HOST || target >= TL_TARGET__COUNT)
        return NULL;

    return tl_target_names[target];
}

/// Looks a target up by its command-line name.
tl_Target tl_target_from_name(const char *name)
{
    i32 i;

    if (!name)
        return TL_TARGET_HOST;

    // From 1: TL_TARGET_HOST has no name to match against.
    for (i = 1; i < TL_TARGET__COUNT; i++)
        if (tl_target_names[i] && 0 == strcmp(tl_target_names[i], name))
            return (tl_Target)i;

    return TL_TARGET_HOST;
}

/// The backend built for this host, if there is one. At most one backend is
/// ever marked native, so the first match is the answer.
tl_Target tl_target_native(void)
{
    i32 i;

    for (i = 0; tl_backends[i]; i++)
        if (tl_backends[i]->is_native)
            return tl_backends[i]->target;

    return TL_TARGET_HOST;
}

/// Whether this build can link for the target at all.
int32_t tl_target_available(tl_Target target)
{
    return find_backend(target) != NULL;
}

/// Whether the target's output can be relocated and run in this process.
int32_t tl_target_is_native(tl_Target target)
{
    const tl_Backend *b = find_backend(target);

    return b && b->is_native;
}

/// Lists the compiled-in targets. Counting continues past 'max' so that a
/// caller can size a buffer with one call and fill it with the next.
int32_t tl_targets(tl_Target *out, int32_t max)
{
    i32 i, n = 0;

    for (i = 0; tl_backends[i]; i++, n++)
        if (out && n < max)
            out[n] = tl_backends[i]->target;

    return n;
}

/// ---------------------------------------------------------------------
/// A backend and the state it created, which is all the dispatcher knows
/// about a link. Everything else lives behind the void * the backend owns.
struct tl_State {
    const tl_Backend *b;  /// The vtable every call below forwards through.
    void *st;             /// The backend's own state, opaque here.
};

/// Creates a context for a target, or for this host when none is named.
tl_State *tl_new(tl_Target target)
{
    const tl_Backend *b;
    tl_State *s;

    if (target == TL_TARGET_HOST)
        target = tl_target_native();

    b = find_backend(target);
    if (!b)
        return NULL;

    // Two allocations, either of which can fail: the wrapper, then the
    // backend's own state.
    s = calloc(1, sizeof *s);
    if (!s)
        return NULL;

    s->b = b;
    s->st = b->create();
    if (!s->st) {
        free(s);
        return NULL;
    }

    return s;
}

/// Releases the context. Tolerates NULL, so a failed tl_new() needs no test.
void tl_delete(tl_State *s)
{
    if (!s)
        return;

    s->b->destroy(s->st);
    free(s);
}

/// The target this context links for, which tl_new() may have chosen.
tl_Target tl_state_target(const tl_State *s)
{
    return s ? s->b->target : TL_TARGET_HOST;
}

/// Routes diagnostics to a callback instead of stderr.
void tl_set_error_func(tl_State *s, void *opaque, tl_ErrorFunc *func)
{
    s->b->set_error_func(s->st, opaque, func);
}

/// How many errors the backend has reported so far.
int32_t tl_error_count(const tl_State *s)
{
    return s->b->error_count(s->st);
}

/// ---------------------------------------------------------------------
/// Selects the output type, which is also what creates the sections.
int32_t tl_set_output_type(tl_State *s, int32_t output_type)
{
    return s->b->set_output_type(s->st, output_type);
}

/// The output type chosen so far, or implied by an option already parsed.
int32_t tl_get_output_type(tl_State *s)
{
    return s->b->get_output_type(s->st);
}

/// The -o argument a parse has seen, or NULL.
const char *tl_get_output_file(tl_State *s)
{
    return s->b->get_output_file(s->st);
}

/// Points the linker at the target support files.
void tl_set_lib_path(tl_State *s, const char *path)
{
    s->b->set_lib_path(s->st, path);
}

/// Appends a directory to search for -l libraries.
int32_t tl_add_library_path(tl_State *s, const char *pathname)
{
    return s->b->add_library_path(s->st, pathname);
}

/// Parses a whitespace-separated ld-style option string.
int32_t tl_set_options(tl_State *s, const char *options)
{
    return s->b->set_options(s->st, options);
}

/// Turns the named flags on or off.
int32_t tl_set_flags(tl_State *s, uint32_t flags, int32_t on)
{
    return s->b->set_flags(s->st, flags, on);
}

/// Every flag currently set.
uint32_t tl_get_flags(tl_State *s)
{
    return s->b->get_flags(s->st);
}

/// Sets a string setting; the backend copies the value.
int32_t tl_set_str(tl_State *s, tl_StrOpt opt, const char *value)
{
    return s->b->set_str(s->st, opt, value);
}

/// The current value of a string setting, owned by the backend.
const char *tl_get_str(tl_State *s, tl_StrOpt opt)
{
    return s->b->get_str(s->st, opt);
}

/// Sets a numeric setting.
int32_t tl_set_num(tl_State *s, tl_NumOpt opt, uint64_t value)
{
    return s->b->set_num(s->st, opt, value);
}

/// The current value of a numeric setting, or the linker's default.
uint64_t tl_get_num(tl_State *s, tl_NumOpt opt)
{
    return s->b->get_num(s->st, opt);
}

/// Parses an ld-style argv, queueing the input files it names.
int32_t tl_parse_args(tl_State *s, int32_t argc, char **argv)
{
    return s->b->parse_args(s->st, argc, argv);
}

/// Adds the files tl_parse_args() queued, in command-line order.
int32_t tl_link_parsed(tl_State *s)
{
    return s->b->link_parsed(s->st);
}

/// Adds an object, archive, shared library or ld script.
int32_t tl_add_file(tl_State *s, const char *filename)
{
    return s->b->add_file(s->st, filename);
}

/// Adds a library by short name, as -l does.
int32_t tl_add_library(tl_State *s, const char *libraryname)
{
    return s->b->add_library(s->st, libraryname);
}

/// Defines a symbol that resolves to memory in this process.
int32_t tl_add_symbol(tl_State *s, const char *name, const void *val)
{
    return s->b->add_symbol(s->st, name, val);
}

/// ---------------------------------------------------------------------
/// Creates a section, or returns the one already carrying that name.
tl_Section tl_add_section(tl_State *s, const char *name, int32_t kind, int32_t align)
{
    return s->b->add_section(s->st, name, kind, align);
}

/// Appends bytes and returns the offset they landed at.
int64_t tl_section_append(tl_State *s, tl_Section sec, const void *data, size_t len)
{
    return s->b->section_append(s->st, sec, data, len);
}

/// Appends zero-filled space and returns the offset it landed at.
int64_t tl_section_zero(tl_State *s, tl_Section sec, size_t len)
{
    return s->b->section_zero(s->st, sec, len);
}

/// How much has been appended to a section so far.
int64_t tl_section_size(tl_State *s, tl_Section sec)
{
    return s->b->section_size(s->st, sec);
}

/// Defines a symbol at an offset in a section.
int32_t tl_define_symbol(tl_State *s, const char *name, tl_Section sec, uint64_t off, int32_t flags)
{
    return s->b->define_symbol(s->st, name, sec, off, flags);
}

/// Names a symbol this object refers to but does not define.
int32_t tl_declare_symbol(tl_State *s, const char *name)
{
    return s->b->declare_symbol(s->st, name);
}

/// Records that the bytes at 'off' need the address of 'sym'.
int32_t tl_add_reloc(tl_State *s, tl_Section sec, uint64_t off, const char *sym, tl_RelocKind kind, int64_t addend)
{
    return s->b->add_reloc(s->st, sec, off, sym, kind, addend);
}

/// Records that the bytes at 'off' need the address of another section.
int32_t tl_add_section_reloc(tl_State *s, tl_Section sec, uint64_t off, tl_Section target, tl_RelocKind kind, int64_t addend)
{
    return s->b->add_section_reloc(s->st, sec, off, target, kind, addend);
}

/// ---------------------------------------------------------------------
/// Links everything added so far and writes it out.
int32_t tl_output_file(tl_State *s, const char *filename)
{
    return s->b->output_file(s->st, filename);
}

/// Links into memory. A cross backend leaves the in-memory calls NULL, so
/// each of the four below has to answer for itself rather than fail late.
int32_t tl_relocate(tl_State *s)
{
    // Cross target: nothing to run it on.
    if (!s->b->relocate)
        return -1;

    return s->b->relocate(s->st);
}

/// The address of a symbol in the in-memory image.
void *tl_get_symbol(tl_State *s, const char *name)
{
    if (!s->b->get_symbol)
        return NULL;

    return s->b->get_symbol(s->st, name);
}

/// Calls back once per global symbol of the in-memory image.
void tl_list_symbols(tl_State *s, void *ctx,
    void (*symbol_cb)(void *ctx, const char *name, const void *val))
{
    if (s->b->list_symbols)
        s->b->list_symbols(s->st, ctx, symbol_cb);
}

/// Links in memory, then calls main() and returns its exit status.
int32_t tl_run(tl_State *s, int32_t argc, char **argv)
{
    if (!s->b->run)
        return -1;

    return s->b->run(s->st, argc, argv);
}

/// The version this library was built as, from xmake.lua or -DTL_VERSION.
const char *tl_version(void)
{
    return TL_VERSION;
}


#endif // TINYLINKER_IMPLEMENTATION

/// ---- src/backends/backend.inc ----

/// tinylinker - backend body
///
/// Included by src/backends/b_core64.c and b_core32.c after each has defined
/// PTR_SIZE, the one thing the core cannot decide per link.  The unit compiles
/// the core once and defines a tl_Backend vtable for every target of that
/// width; a target differs from its siblings only in the architecture and
/// object format its create function hands to tli_new().
///
/// Everything the core defines becomes 'static' here (ONE_SOURCE
/// makes ST_FUNC/ST_DATA static, and TLI_PUBLIC is forced to static below), so
/// the only symbols this unit exports are the vtables.

#ifndef PTR_SIZE
# error "include backend.inc from b_core64.c or b_core32.c"
#endif

/// Compiling the whole core into this one unit is what lets every name in it
/// be static, and is why the two cores can coexist without colliding.
#define ONE_SOURCE 1
#define TLI_PUBLIC static
#define PUB_FUNC static


/// ---- src/core/tl_core.c ----

/// tinylinker - state, inputs and options
///
/// This is the outermost layer of the linker: the object an embedder holds, the
/// paths it searches, the way it reports what went wrong, and the two ways a
/// file gets in (named directly, or found under a -l name).  It also holds the
/// option parser, which turns a command line into settings on that object and
/// a list of files to link.
///
/// It is also the translation unit.  Everything below the include block is
/// compiled into one core per pointer width, so a library can carry both
/// widths at once without their internals meeting; see
/// src/backends/backend.inc.

#ifndef ONE_SOURCE
# define ONE_SOURCE 1
#endif

#if ONE_SOURCE
/// A core is built as a single translation unit: everything below becomes
/// 'static' (see ST_FUNC/ST_DATA in tl_internal.h), so the cores for the two
/// pointer widths can live in one library.
///
/// The order of this list is load-bearing and the single-header packer follows
/// it.  tl_sort.c comes first because it is the one that pulls in
/// tl_internal.h; the files after it, tl_build.c and tl_settings.c among them,
/// include nothing of their own and rely on that.  The generic core then
/// precedes the relocation code of every architecture of this pointer width,
/// which precedes the writers for the object formats built on it, and
/// tl_run.c comes last because it needs everything.

/// ---- src/core/tl_sort.c ----

/// tinylinker - sorting
///
/// The library has no global state and must be usable from several threads at
/// once, so it cannot pass a context to a comparison function through a static
/// variable, and qsort_r is spelled three different ways on the three
/// platforms this builds for.  Hence a sort of its own.
///
/// It is a bottom-up merge sort.  A linker sorts symbol tables, which are
/// large and whose comparisons are expensive because they end in strcmp, so
/// what matters is the number of comparisons rather than the number of moves:
/// merging is n log n on every input, including the sorted and the all-equal
/// ones that a quicksort has to be defended against.  It also reads and writes
/// each run front to back, which suits arrays that do not fit in cache.
/// Being stable is a bonus a comparison function is allowed to lean on.


/// ---- src/core/tl_internal.h ----


#define _GNU_SOURCE
#define _DARWIN_C_SOURCE

/// ---- src/core/config.h ----


/// Normally xmake generates tl_buildconfig.h from tl_buildconfig.h.in.
/// Defining TL_VERSION on the command line instead lets the tree be compiled
/// with a bare `clang src/tinylinker.c src/backends/b_*.c`, which the test
/// harness relies on when it builds inside a container.
#ifndef TL_VERSION
# include "tl_buildconfig.h"
#endif

/// The version the core reports, spelled under the linker's own prefix so
/// that nothing internal has to know which of the two names was set.
#define TLI_VERSION TL_VERSION

/// tinylinker is a linker: it never runs a preprocessor, so it has no use for
/// predefined macros, and it does not ship a compiler runtime (libtcc1.a).
#undef TLI_PREDEFS


/// glibc's headers #define __attribute__ away for compilers that are not gcc,
/// and tcc counts as one of those, so put it back before anything uses it.
#ifdef __TINYC__
# undef __attribute__
#endif

#ifndef _WIN32
# include <unistd.h>
# include <sys/time.h>
# ifndef TLI_STATIC
#  include <dlfcn.h>
# endif
#endif

#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN 1
# ifndef _WIN32_WINNT
#  define _WIN32_WINNT 0x502  /// AddVectoredExceptionHandler
# endif
# include <windows.h>
# include <io.h>  /// open, close etc.
# include <direct.h>  /// getcwd
# include <malloc.h>  /// alloca
# ifndef _MSC_VER
#  include <stdint.h>
# endif
# define inline __inline
# define snprintf _snprintf
# define vsnprintf _vsnprintf
# ifndef __GNUC__
#  define strtoull _strtoui64
# endif
# ifdef TLI_AS_DLL
#  define TLI_PUBLIC __declspec(dllexport)
#  define PUB_FUNC TLI_PUBLIC
# endif
# ifdef _MSC_VER
   /// This is old-fashioned C and MSVC has opinions about it that are not
   /// worth acting on: narrowing conversions that are known to fit, signed
   /// against unsigned comparisons, and the POSIX spellings of file calls.
#  pragma warning (disable : 4244)
#  pragma warning (disable : 4267)
#  pragma warning (disable : 4996)
#  pragma warning (disable : 4018)
#  pragma warning (disable : 4146)
#  define ssize_t intptr_t
#  ifdef _X86_
#   define __i386__ 1
#  endif
#  ifdef _AMD64_
#   define __x86_64__ 1
#  endif
# endif
# if defined(_M_ARM64) && !defined(__aarch64__)
#  define __aarch64__ 1
# endif
# ifndef va_copy
#  define va_copy(a,b) a = b
# endif
#endif

/// Opening a file in text mode would translate line endings, which would
/// corrupt every object file this reads.  Only Windows makes the distinction;
/// elsewhere the flag does not exist and asking for it is a no-op.
#ifndef O_BINARY
# define O_BINARY 0
#endif

#ifndef offsetof
# ifdef __clang__  /// -fsanitize objects to NULL + value
#  define offsetof(type, field) __builtin_offsetof(type, field)
# else
#  define offsetof(type, field) ((usize) &((type *)0)->field)
# endif
#endif

/// Elements in an array, which only works on a real array and not on a
/// pointer; every use here is on a static table.
#ifndef countof
# define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
#endif

/// The three function and object attributes this tree uses, spelled the way
/// each compiler wants them.  MSVC has no equivalent of the printf check, so
/// PRINTF_LIKE simply vanishes there.
#ifdef _MSC_VER
# define NORETURN __declspec(noreturn)
# define ALIGNED(x) __declspec(align(x))
# define PRINTF_LIKE(x,y)
#else
# define NORETURN __attribute__((noreturn))
# define ALIGNED(x) __attribute__((aligned(x)))
# define PRINTF_LIKE(x,y) __attribute__ ((format (printf, (x), (y))))
#endif

/// Path syntax of the host, not of the target: these are used on names that
/// come from the command line and from the file system.
#ifdef _WIN32
# define IS_DIRSEP(c) (c == '/' || c == '\\')
# define IS_ABSPATH(p) (IS_DIRSEP(p[0]) || (p[0] && p[1] == ':' && IS_DIRSEP(p[2])))
# define PATHCMP stricmp
# define PATHSEP ";"
#else
# define IS_DIRSEP(c) (c == '/')
# define IS_ABSPATH(p) IS_DIRSEP(p[0])
# define PATHCMP strcmp
# define PATHSEP ":"
#endif

/// ------------------------------------------------------------------------
/// The host.
///
/// Every architecture and object format a core links for is compiled in, and
/// a link picks its own at run time.  What is fixed at build time is the
/// machine the linker itself runs on, which decides whether a link's output
/// could run in this process.

/// MEM_DEBUG, defined as 1, 2 or 3, tracks allocations; single-threaded only.

/// The ELF machine number of the host, or 0 for a host no core links for,
/// which no architecture's table matches.
#if defined __i386__
# define TLI_HOST_EM EM_386
#elif defined __x86_64__
# define TLI_HOST_EM EM_X86_64
#elif defined __arm__
# define TLI_HOST_EM EM_ARM
#elif defined __aarch64__
# define TLI_HOST_EM EM_AARCH64
#elif defined __riscv && defined __LP64__
# define TLI_HOST_EM EM_RISCV
#else
# define TLI_HOST_EM 0
#endif

/// The object format the host loads.
#if defined _WIN32
# define TLI_HOST_FMT TLI_FMT_PE
#elif defined __APPLE__
# define TLI_HOST_FMT TLI_FMT_MACHO
#else
# define TLI_HOST_FMT TLI_FMT_ELF
#endif

/// Linking into this process is only meaningful when the image being built is
/// one this process could execute: same architecture, same object format.  A
/// constant expression, so that a backend's vtable can be initialised with it;
/// the cast stops clang reading the host's own format as a self-comparison.
#define TLI_IS_NATIVE(em, fmt) ((em) == TLI_HOST_EM && (i32)(fmt) == TLI_HOST_FMT)

/// The optional pieces default to on, and are turned off by being defined as
/// zero rather than by being left undefined, so that a build system can say
/// -DTLI_BACKTRACE=0 without having to know the default.
#if defined TLI_BACKTRACE && TLI_BACKTRACE==0
# undef TLI_BACKTRACE
#else
# define TLI_BACKTRACE 1  /// backtraces from a running image
#endif

#if defined TLI_BCHECK && TLI_BCHECK==0
# undef TLI_BCHECK
#else
# define TLI_BCHECK 1  /// the bounds checker's sections and runtime
#endif

#if defined CONFIG_NEW_MACHO && CONFIG_NEW_MACHO==0
# undef CONFIG_NEW_MACHO
#else
# define CONFIG_NEW_MACHO 1  /// LC_DYLD_INFO rather than the old tables
#endif

/// TARGETOS_BSD says which OS the linker itself is running on, and is all
/// that is left of what used to pick the target OS at build time.  The OS the
/// OUTPUT is for is s1->target_os, chosen per link; see TLI_OS_* below.  The
/// one remaining reader is the LD_SO override in tli_elf_new(), which is about
/// this machine's loader rather than the image's.
#if defined TARGETOS_OpenBSD \
    || defined TARGETOS_FreeBSD \
    || defined TARGETOS_NetBSD \
    || defined TARGETOS_FreeBSD_kernel
# define TARGETOS_BSD 1
#endif

/// The operating system the output image is meant for, set through
/// TL_NUM_TARGET_OS.  These values are the public tl_TargetOS ones: the
/// dispatcher passes the caller's number straight through, so the two lists
/// have to stay in step.
enum {
    TLI_OS_LINUX = 0,  /// The default, and what a zeroed state means.
    TLI_OS_FREEBSD,
    TLI_OS_OPENBSD,
    TLI_OS_NETBSD,
    TLI_OS__COUNT
};

/// Most of the differences turn on BSD versus not, rather than on which BSD.
#define TLI_OS_IS_BSD(s1) ((s1)->target_os != TLI_OS_LINUX)

/// The object format of the output image, s1->fmt.  PE and Mach-O images are
/// written by tl_pe_out.c and tl_macho_out.c, and ELF is still the only
/// relocatable object format those two read and write, so they keep the ELF
/// object paths and skip everything about ELF images.
enum {
    TLI_FMT_ELF = 0,  /// What a zeroed state means.
    TLI_FMT_PE,
    TLI_FMT_MACHO
};

/// Guard the one piece of state that is genuinely global, so that two threads
/// can each drive a link of their own.
#ifndef TLI_SEMLOCK
# define TLI_SEMLOCK 1
#endif

/// ------------------------------------------------------------------------
/// Where the files a link needs are looked for.  {B} below stands for
/// CONFIG_TLIDIR, or for whatever -B was given.

/// Prefix put in front of every system path, for a cross link whose target
/// libraries are under a directory of their own.
#ifndef CONFIG_SYSROOT
# define CONFIG_SYSROOT ""
#endif

/// Where the linker's own files live, and what {B} stands for when -B is not
/// given.  Windows works it out from the executable's path instead.
#if !defined CONFIG_TLIDIR && !defined _WIN32
# define CONFIG_TLIDIR "/usr/local/lib/tinylinker"
#endif

/// Debian and its derivatives put the libraries of each architecture in a
/// directory named after the target triplet.
#ifdef CONFIG_TRIPLET
# define USE_TRIPLET(s) s "/" CONFIG_TRIPLET
# define ALSO_TRIPLET(s) USE_TRIPLET(s) PATHSEP s
#else
# define USE_TRIPLET(s) s
# define ALSO_TRIPLET(s) s
#endif

/// Where crt1.o, crti.o and crtn.o are looked for.
#ifndef TLI_CRTPREFIX
# define TLI_CRTPREFIX USE_TRIPLET(CONFIG_SYSROOT "/usr/lib")
#endif

/// The default library search path, which -L adds to and -nostdlib drops.
/// A PE link searches TLI_PE_LIBPATHS instead.
#ifndef TLI_PE_LIBPATHS
# define TLI_PE_LIBPATHS "{B}/lib"
#endif
#ifndef TLI_LIBPATHS
# if defined _WIN32
#  define TLI_LIBPATHS "{B}/lib"
# else
#  define TLI_LIBPATHS \
    "{B}" PATHSEP ALSO_TRIPLET(CONFIG_SYSROOT "/usr/lib")
# endif
#endif

/// The program that maps a dynamically linked image, named in PT_INTERP.  Each
/// architecture's own is s1->arch->elf_interp; defining this replaces them
/// all, as GNU Hurd does, where one loader serves every machine.
#if !defined TLI_ELFINTERP && defined __GNU__
# define TLI_ELFINTERP "/lib/ld.so"
#endif

/// The compiler support library, if the caller wants one linked in.
#ifndef TLI_RUNTIME_LIB
# define TLI_RUNTIME_LIB "libtcc1.a"
#endif

/// Prefix on the names of the host tools a cross build invokes, such as
/// aarch64-linux-gnu-.
#ifndef TLI_CROSSPREFIX
# define TLI_CROSSPREFIX ""
#endif

/// --------------------------------------------

/// Whether this core can link into its own process at all: the linker has to
/// run on a machine it links for, with this core's pointer width.  Whether one
/// link may is s1->native, since its output has to be for this machine.
#if PTR_SIZE == 8 && (defined __x86_64__ || defined __aarch64__ \
                      || (defined __riscv && defined __LP64__))
# define TLI_HOST_RUN
#elif PTR_SIZE == 4 && (defined __i386__ || defined __arm__)
# define TLI_HOST_RUN
#endif


/// ---- src/core/tl_api.h ----


/// Marks the entry points a backend translation unit exports.  A build that
/// hides everything else by default defines this to whatever undoes that.
#ifndef TLI_PUBLIC
# define TLI_PUBLIC
#endif

#ifdef __cplusplus
extern "C" {
#endif

/// One link in progress: its options, its inputs and the sections being built
/// out of them.  Opaque here; the layout is in tl_internal.h.
typedef struct tli_State tli_State;

/// The machine a link is for.  Its layout is in tl_internal.h.
struct tli_Arch;

/// Create a linker context for one architecture and object format (a
/// TLI_FMT_*), either of which this core must have compiled in.
TLI_PUBLIC tli_State *tli_new(const struct tli_Arch *arch, i32 fmt);

/// Free a linker context and everything linked into it.
TLI_PUBLIC void tli_delete(tli_State *s);

/// Set the directory holding target support files at runtime.
TLI_PUBLIC void tli_set_lib_path(tli_State *s, const char *path);

/// Error and warning callback (optional).  The message arrives formatted and
/// without a trailing newline.
typedef void TLIErrorFunc(void *opaque, const char *msg);

/// Set the error/warning callback (optional).
TLI_PUBLIC void tli_set_error_func(tli_State *s, void *error_opaque, TLIErrorFunc *error_func);

/// Set options as from a command line (multiple supported).
TLI_PUBLIC i32 tli_set_options(tli_State *s, const char *str);

/// Set the output type.  MUST BE CALLED before adding any input, because it
/// decides which sections exist and how symbols are resolved.
TLI_PUBLIC i32 tli_set_output_type(tli_State *s, i32 output_type);

/// What tli_set_output_type() accepts.
#define TLI_OUTPUT_MEMORY   1 /// Output will be linked in memory.
#define TLI_OUTPUT_EXE      2 /// Executable file.
#define TLI_OUTPUT_OBJ      3 /// Relocatable object file.
#define TLI_OUTPUT_DLL      4 /// Dynamic library.

/// Add an input file: object, static/dynamic library, or ld script.
TLI_PUBLIC i32 tli_add_file(tli_State *s, const char *filename);

/// Equivalent to the -L option.
TLI_PUBLIC i32 tli_add_library_path(tli_State *s, const char *pathname);

/// The library name is the same as the argument of the -l option.
TLI_PUBLIC i32 tli_add_library(tli_State *s, const char *libraryname);

/// Define a symbol pointing at host memory (for TLI_OUTPUT_MEMORY).
TLI_PUBLIC i32 tli_add_symbol(tli_State *s, const char *name, const void *val);

/// Link and write the output file.
TLI_PUBLIC i32 tli_output_file(tli_State *s, const char *filename);

/// Running in this process, which only a core of the host's own width does.
#ifdef TLI_HOST_RUN

/// Link into memory; needed before tli_get_symbol().
TLI_PUBLIC i32 tli_relocate(tli_State *s1);

/// Return a symbol's value, or NULL if it is not found.
TLI_PUBLIC void *tli_get_symbol(tli_State *s, const char *name);

/// List all (global) symbols and their values via 'symbol_cb()'.
TLI_PUBLIC void tli_list_symbols(tli_State *s, void *ctx,
    void (*symbol_cb)(void *ctx, const char *name, const void *val));

/// Link in memory, then call main() and return its value.
TLI_PUBLIC i32 tli_run(tli_State *s, i32 argc, char **argv);

/// Arm the exception handler and hand back the jmp_buf to pass to setjmp().
/// Taking longjmp as an argument keeps this header free of <setjmp.h>.
TLI_PUBLIC void *_tcc_setjmp(tli_State *s1, void *jmp_buf, void *top_func, void *longjmp);

/// Catch runtime exceptions, optionally limiting backtraces at top_func.
#define tli_setjmp(s1,jb,f) setjmp(_tcc_setjmp(s1, jb, f, longjmp))

/// Custom error printer for runtime exceptions.  Returning 0 stops the
/// backtrace, which is how a caller limits how far up it is walked.
typedef i32 TLIBtFunc(void *udata, void *pc, const char *file, i32 line, const char* func, const char *msg);
#endif

#ifdef __cplusplus
}
#endif


/// ---- src/core/tl_elf.h ----


#ifndef _WIN32
# include <inttypes.h>
#else
/// Old Microsoft toolchains ship no <stdint.h>, so the fixed width names this
/// header is written in are declared by hand where nothing else provides
/// them.
# ifndef __int8_t_defined
#  define __int8_t_defined
typedef signed char             int8_t;
typedef short int               int16_t;
typedef int                     int32_t;
typedef long long int           int64_t;
typedef unsigned char           uint8_t;
typedef unsigned short int      uint16_t;
typedef unsigned int            uint32_t;
typedef unsigned long long int  uint64_t;
# endif
#endif

/// ---------------------------------------------------------------- types
///
/// The gABI names the scalar types once per class.  Both classes are defined
/// unconditionally: a 64-bit backend still reads the 32-bit forms when it is
/// handed an i386 object, and the ElfW() macro in tl_internal.h picks a class
/// per translation unit.

typedef u16 Elf32_Half;    /// A halfword.
typedef u32 Elf32_Word;    /// An unsigned word, the workhorse of this class.
typedef i32 Elf32_Sword;   /// A signed word, used only by d_tag and r_addend.
typedef u32 Elf32_Addr;    /// A virtual address in the image.
typedef u32 Elf32_Off;     /// An offset into the file.
typedef u16 Elf32_Section; /// A section index, hence the SHN_* escapes.

typedef u16 Elf64_Half;    /// A halfword; the same width in both classes.
typedef u32 Elf64_Word;    /// Still 32 bits: only addresses and sizes widen.
typedef i32 Elf64_Sword;   /// The signed form of the same.
typedef u64 Elf64_Xword;   /// The widened word, for sizes and packed fields.
typedef i64 Elf64_Sxword;  /// Its signed form, for d_tag and r_addend.
typedef u64 Elf64_Addr;    /// A virtual address in the image.
typedef u64 Elf64_Off;     /// An offset into the file.
typedef u16 Elf64_Section; /// A section index, hence the SHN_* escapes.

/// ---------------------------------------------------------------- header

/// Bytes of e_ident, fixed by the format so that the rest of the header can
/// be read only once its class and encoding are known.
#define EI_NIDENT 16

/// The first thing in a 32-bit file: what it is, what it runs on, and where
/// the two header tables are.
typedef struct {
    u8            e_ident[EI_NIDENT]; /// Magic, class, encoding, OS ABI, read by position.
    Elf32_Half    e_type;             /// ET_REL, ET_EXEC or ET_DYN.
    Elf32_Half    e_machine;          /// EM_* naming the processor the code is for.
    Elf32_Word    e_version;          /// Always EV_CURRENT.
    Elf32_Addr    e_entry;            /// Address the loader jumps to, or zero.
    Elf32_Off     e_phoff;            /// File offset of the program header table.
    Elf32_Off     e_shoff;            /// File offset of the section header table.
    Elf32_Word    e_flags;            /// Processor-specific flags; see the EF_* below.
    Elf32_Half    e_ehsize;           /// Size of this header.
    Elf32_Half    e_phentsize;        /// Size of one program header.
    Elf32_Half    e_phnum;            /// Entries in the program header table.
    Elf32_Half    e_shentsize;        /// Size of one section header.
    Elf32_Half    e_shnum;            /// Sections, or zero with the count in section zero.
    Elf32_Half    e_shstrndx;         /// tli_Section holding the section names, or SHN_XINDEX.
} Elf32_Ehdr;

/// The same header for a 64-bit file: identical fields, with the addresses
/// and file offsets widened.
typedef struct {
    u8            e_ident[EI_NIDENT]; /// Magic, class, encoding, OS ABI, read by position.
    Elf64_Half    e_type;             /// ET_REL, ET_EXEC or ET_DYN.
    Elf64_Half    e_machine;          /// EM_* naming the processor the code is for.
    Elf64_Word    e_version;          /// Always EV_CURRENT.
    Elf64_Addr    e_entry;            /// Address the loader jumps to, or zero.
    Elf64_Off     e_phoff;            /// File offset of the program header table.
    Elf64_Off     e_shoff;            /// File offset of the section header table.
    Elf64_Word    e_flags;            /// Processor-specific flags; see the EF_* below.
    Elf64_Half    e_ehsize;           /// Size of this header.
    Elf64_Half    e_phentsize;        /// Size of one program header.
    Elf64_Half    e_phnum;            /// Entries in the program header table.
    Elf64_Half    e_shentsize;        /// Size of one section header.
    Elf64_Half    e_shnum;            /// Sections, or zero with the count in section zero.
    Elf64_Half    e_shstrndx;         /// tli_Section holding the section names, or SHN_XINDEX.
} Elf64_Ehdr;

/// e_ident[] indices.  Only EI_OSABI is named: the four magic bytes, the
/// class, the encoding and the version are written and checked by position,
/// which is how the code below reads.
#define EI_OSABI 7

/// The four bytes every ELF file opens with, as a string and one at a time.
#define ELFMAG  "\177ELF"
#define ELFMAG0 0x7f
#define ELFMAG1 'E'
#define ELFMAG2 'L'
#define ELFMAG3 'F'

/// e_ident[EI_CLASS], which decides the width of everything after e_ident.
#define ELFCLASS32 1
#define ELFCLASS64 2

/// e_ident[EI_DATA]: tinylinker targets little-endian machines only.
#define ELFDATA2LSB 1

/// e_ident[EI_OSABI].  Only the systems whose objects need to be told apart
/// are named; the value is zero on Linux and on anything else generic.
#define ELFOSABI_NETBSD  2
#define ELFOSABI_FREEBSD 9
#define ELFOSABI_OPENBSD 12
#define ELFOSABI_ARM     97  /// Not an OS: marks an object using ARM's own conventions.

/// e_version, EV_*
#define EV_CURRENT 1

/// e_type.  ET_DYN covers both a shared library and a position-independent
/// executable, which is why the two are told apart by other means.
#define ET_REL  1
#define ET_EXEC 2
#define ET_DYN  3

/// e_machine, for the five architectures this linker has backends for.
#define EM_386     3
#define EM_ARM     40
#define EM_X86_64  62
#define EM_AARCH64 183
#define EM_RISCV   243

/// e_flags, ARM: the EABI revision lives in the top byte, the float ABI in
/// bits 9 and 10.
#define EF_ARM_EABI_VER5  0x05000000
#define EF_ARM_SOFT_FLOAT 0x00000200
#define EF_ARM_VFP_FLOAT  0x00000400

/// e_flags, RISC-V
#define EF_RISCV_FLOAT_ABI_DOUBLE 0x0004

/// ---------------------------------------------------------------- sections

/// One section of a 32-bit file: where its bytes are, where they are loaded,
/// and what reads them.
typedef struct {
    Elf32_Word sh_name;      /// Offset into the section header string table.
    Elf32_Word sh_type;      /// SHT_* saying how the contents are read.
    Elf32_Word sh_flags;     /// SHF_* bits: allocated, writable, executable, TLS.
    Elf32_Addr sh_addr;      /// Address the section is loaded at, or zero.
    Elf32_Off  sh_offset;    /// Where the contents start; unused for SHT_NOBITS.
    Elf32_Word sh_size;      /// Bytes occupied, in memory even for SHT_NOBITS.
    Elf32_Word sh_link;      /// A section this one refers to; sh_type says which.
    Elf32_Word sh_info;      /// Extra information; again sh_type decides what.
    Elf32_Word sh_addralign; /// Alignment sh_addr must satisfy; a power of two.
    Elf32_Word sh_entsize;   /// Size of one entry where this is a table, else zero.
} Elf32_Shdr;

/// The same for a 64-bit file, with the sizes and flags widened as well as
/// the address and the offset.
typedef struct {
    Elf64_Word  sh_name;      /// Offset into the section header string table.
    Elf64_Word  sh_type;      /// SHT_* saying how the contents are read.
    Elf64_Xword sh_flags;     /// SHF_* bits: allocated, writable, executable, TLS.
    Elf64_Addr  sh_addr;      /// Address the section is loaded at, or zero.
    Elf64_Off   sh_offset;    /// Where the contents start; unused for SHT_NOBITS.
    Elf64_Xword sh_size;      /// Bytes occupied, in memory even for SHT_NOBITS.
    Elf64_Word  sh_link;      /// A section this one refers to; sh_type says which.
    Elf64_Word  sh_info;      /// Extra information; again sh_type decides what.
    Elf64_Xword sh_addralign; /// Alignment sh_addr must satisfy; a power of two.
    Elf64_Xword sh_entsize;   /// Size of one entry where this is a table, else zero.
} Elf64_Shdr;

/// Reserved section indices.  Everything from SHN_LORESERVE to SHN_HIRESERVE
/// is a value in its own right rather than a section number, which is why a
/// file with that many sections needs the escapes below.
#define SHN_UNDEF     0       /// No section: an undefined symbol, or a missing link.
#define SHN_LORESERVE 0xff00  /// First index that means something other than a section.
#define SHN_LOOS      0xff20  /// First of the range an operating system may define.
#define SHN_ABS       0xfff1  /// The symbol's value is absolute and is not relocated.
#define SHN_COMMON    0xfff2  /// Tentative definition; st_value holds its alignment.

/// "The real index is elsewhere": in st_shndx it means the symbol's section
/// number is in the SHT_SYMTAB_SHNDX section beside its symbol table, and in
/// e_shstrndx it means the index is in sh_link of section header zero.
#define SHN_XINDEX    0xffff
#define SHN_HIRESERVE 0xffff

/// sh_type
#define SHT_PROGBITS      1   /// Contents the file supplies and only the program reads.
#define SHT_SYMTAB        2
#define SHT_STRTAB        3
#define SHT_RELA          4   /// Relocations carrying their addend in the record.
#define SHT_HASH          5
#define SHT_DYNAMIC       6
#define SHT_NOTE          7
#define SHT_NOBITS        8   /// Occupies memory but no file space, as .bss does.
#define SHT_REL           9   /// Relocations whose addend sits in the field patched.
#define SHT_DYNSYM        11  /// The symbols a loader needs, a subset of SHT_SYMTAB.
#define SHT_INIT_ARRAY    14
#define SHT_FINI_ARRAY    15
#define SHT_PREINIT_ARRAY 16

/// One 32-bit section number per symbol of the table named by sh_link, in the
/// same order, standing in for the st_shndx values that would not fit.
#define SHT_SYMTAB_SHNDX  18

/// Start of the range an operating system may define types in.
#define SHT_LOOS          0x60000000

/// sh_type, GNU extensions in the OS range.
#define SHT_GNU_HASH    0x6ffffff6  /// The faster hash that replaced SHT_HASH.
#define SHT_GNU_verdef  0x6ffffffd  /// Versions this object defines.
#define SHT_GNU_verneed 0x6ffffffe  /// Versions this object needs from others.
#define SHT_GNU_versym  0x6fffffff  /// One version index per dynamic symbol.

/// sh_type, processor-specific.  The base of the range is 0x70000000; each
/// psABI numbers upwards from there, so the same value means different things
/// on different machines and only the target's own name is defined.
#define SHT_X86_64_UNWIND     0x70000001
#define SHT_ARM_EXIDX         0x70000001
#define SHT_ARM_ATTRIBUTES    0x70000003
#define SHT_RISCV_ATTRIBUTES  0x70000003

/// sh_flags
#define SHF_WRITE      (1 << 0)   /// Writable once loaded.
#define SHF_ALLOC      (1 << 1)   /// Occupies memory at run time.
#define SHF_EXECINSTR  (1 << 2)   /// Holds instructions.
#define SHF_GROUP      (1 << 9)   /// A member of a section group; kept or dropped together.
#define SHF_TLS        (1 << 10)  /// Thread-local storage, not addressed like ordinary data.
#define SHF_COMPRESSED (1 << 11)  /// Contents are compressed and begin with a header.

/// ---------------------------------------------------------------- symbols
///
/// The two classes order the fields differently: the 64-bit form moves
/// st_value and st_size after st_info so that the 8-byte members stay
/// naturally aligned.

/// One symbol of a 32-bit file.
typedef struct {
    Elf32_Word    st_name;  /// Offset into the string table the symbol table links to.
    Elf32_Addr    st_value; /// Address, section offset or alignment, per file type.
    Elf32_Word    st_size;  /// Bytes the object occupies, or zero if unknown.
    u8            st_info;  /// Binding in the high nibble, type in the low one.
    u8            st_other; /// Visibility in the low two bits; the rest is reserved.
    Elf32_Section st_shndx; /// tli_Section it is defined in, or one of the SHN_* escapes.
} Elf32_Sym;

/// The same symbol for a 64-bit file, with the fields reordered for
/// alignment.
typedef struct {
    Elf64_Word    st_name;  /// Offset into the string table the symbol table links to.
    u8            st_info;  /// Binding in the high nibble, type in the low one.
    u8            st_other; /// Visibility in the low two bits; the rest is reserved.
    Elf64_Section st_shndx; /// tli_Section it is defined in, or one of the SHN_* escapes.
    Elf64_Addr    st_value; /// Address, section offset or alignment, per file type.
    Elf64_Xword   st_size;  /// Bytes the object occupies, or zero if unknown.
} Elf64_Sym;

/// st_info packs the binding in the high nibble and the type in the low one.
/// Both classes use the same layout, so the 32- and 64-bit macros are the
/// same; they are spelled out per class because ELFW() pastes the width in.
#define ELF32_ST_BIND(val)       (((u8) (val)) >> 4)
#define ELF32_ST_TYPE(val)       ((val) & 0xf)
#define ELF32_ST_INFO(bind, type) (((bind) << 4) | ((type) & 0xf))
#define ELF64_ST_BIND(val)       ELF32_ST_BIND(val)
#define ELF64_ST_TYPE(val)       ELF32_ST_TYPE(val)
#define ELF64_ST_INFO(bind, type) ELF32_ST_INFO(bind, type)

/// st_other holds the visibility in its low two bits; the rest is reserved.
#define ELF32_ST_VISIBILITY(o) ((o) & 0x03)
#define ELF64_ST_VISIBILITY(o) ELF32_ST_VISIBILITY(o)

/// Symbol binding, which decides what happens when two objects define the
/// same name.
#define STB_LOCAL  0  /// Invisible outside its own object; these come first in a table.
#define STB_GLOBAL 1
#define STB_WEAK   2  /// Yields to a global definition, and may go unresolved.

/// Symbol type.
#define STT_NOTYPE    0
#define STT_OBJECT    1
#define STT_FUNC      2
#define STT_SECTION   3   /// Names a section, so that a relocation can refer to one.
#define STT_FILE      4   /// Names the source file; carries no value.
#define STT_TLS       6   /// st_value is an offset into the TLS block, not an address.
#define STT_GNU_IFUNC 10  /// Resolved by calling the symbol, which returns the real address.

/// Symbol visibility, which limits what the dynamic linker may rebind.
#define STV_DEFAULT   0
#define STV_INTERNAL  1
#define STV_HIDDEN    2  /// Not exported, so references to it are bound at link time.
#define STV_PROTECTED 3  /// Exported, but references from inside are bound at link time.

/// ---------------------------------------------------------------- relocations
///
/// REL carries the addend in the section being patched, RELA carries it in
/// the record.  r_info packs the symbol index and the relocation type; the
/// 64-bit form widens the symbol index to 32 bits, so the shift differs by
/// class.

/// A 32-bit relocation whose addend is already in the field being patched.
typedef struct {
    Elf32_Addr r_offset; /// Where the field is: an address, or an offset in a section.
    Elf32_Word r_info;   /// Symbol index and relocation type packed together.
} Elf32_Rel;

/// The 64-bit form of the same.
typedef struct {
    Elf64_Addr  r_offset; /// Where the field is: an address, or an offset in a section.
    Elf64_Xword r_info;   /// Symbol index and relocation type packed together.
} Elf64_Rel;

/// A 32-bit relocation carrying its own addend, so the field need not hold
/// one.
typedef struct {
    Elf32_Addr  r_offset; /// Where the field is: an address, or an offset in a section.
    Elf32_Word  r_info;   /// Symbol index and relocation type packed together.
    Elf32_Sword r_addend; /// The constant part of the calculation, signed.
} Elf32_Rela;

/// The 64-bit form of the same.
typedef struct {
    Elf64_Addr   r_offset; /// Where the field is: an address, or an offset in a section.
    Elf64_Xword  r_info;   /// Symbol index and relocation type packed together.
    Elf64_Sxword r_addend; /// The constant part of the calculation, signed.
} Elf64_Rela;

/// Take r_info apart, and put it back together.  The 32-bit form leaves eight
/// bits for the type, which is why R_ARM_NUM stops at 256.
#define ELF32_R_SYM(val)        ((val) >> 8)
#define ELF32_R_TYPE(val)       ((val) & 0xff)
#define ELF32_R_INFO(sym, type) (((sym) << 8) | ((type) & 0xff))

/// The 64-bit form, with a full word for each half.
#define ELF64_R_SYM(val)        ((val) >> 32)
#define ELF64_R_TYPE(val)       ((val) & 0xffffffff)
#define ELF64_R_INFO(sym, type) ((((Elf64_Xword) (sym)) << 32) + (type))

/// i386 relocation types.  See i386-link.c for what each one calculates.
#define R_386_NONE        0
#define R_386_32          1
#define R_386_PC32        2
#define R_386_GOT32       3
#define R_386_PLT32       4
#define R_386_COPY        5
#define R_386_GLOB_DAT    6
#define R_386_JMP_SLOT    7
#define R_386_RELATIVE    8
#define R_386_GOTOFF      9
#define R_386_GOTPC       10
#define R_386_TLS_LE      17
#define R_386_TLS_GD      18
#define R_386_TLS_LDM     19
#define R_386_16          20
#define R_386_PC16        21
#define R_386_TLS_LDO_32  32
#define R_386_GOT32X      43  /// A GOT32 the linker is allowed to relax.
#define R_386_NUM         44  /// One past the highest type, so it sizes a table.

/// x86-64 relocation types.  See x86_64-link.c for what each one calculates.
#define R_X86_64_NONE           0
#define R_X86_64_64             1
#define R_X86_64_PC32           2
#define R_X86_64_GOT32          3
#define R_X86_64_PLT32          4
#define R_X86_64_COPY           5
#define R_X86_64_GLOB_DAT       6
#define R_X86_64_JUMP_SLOT      7
#define R_X86_64_RELATIVE       8
#define R_X86_64_GOTPCREL       9
#define R_X86_64_32             10  /// Truncated to 32 bits and zero extended.
#define R_X86_64_32S            11  /// Truncated to 32 bits and sign extended.
#define R_X86_64_DTPOFF64       17
#define R_X86_64_TPOFF64        18
#define R_X86_64_TLSGD          19
#define R_X86_64_TLSLD          20
#define R_X86_64_DTPOFF32       21
#define R_X86_64_GOTTPOFF       22
#define R_X86_64_TPOFF32        23
#define R_X86_64_PC64           24
#define R_X86_64_GOTOFF64       25
#define R_X86_64_GOTPC32        26
#define R_X86_64_GOT64          27
#define R_X86_64_GOTPC64        29
#define R_X86_64_PLTOFF64       31
#define R_X86_64_GOTPCRELX      41  /// A GOTPCREL the linker is allowed to relax.
#define R_X86_64_REX_GOTPCRELX  42  /// The same, on an instruction with a REX prefix.
#define R_X86_64_NUM            43  /// One past the highest type, so it sizes a table.

/// ARM relocation types.  See arm-link.c for what each one calculates.
#define R_ARM_NONE           0
#define R_ARM_PC24           1
#define R_ARM_ABS32          2
#define R_ARM_REL32          3
#define R_ARM_THM_PC22       10
#define R_ARM_COPY           20
#define R_ARM_GLOB_DAT       21
#define R_ARM_JUMP_SLOT      22
#define R_ARM_RELATIVE       23
#define R_ARM_GOTOFF         24
#define R_ARM_GOTPC          25
#define R_ARM_GOT32          26
#define R_ARM_PLT32          27
#define R_ARM_CALL           28
#define R_ARM_JUMP24         29
#define R_ARM_THM_JUMP24     30
#define R_ARM_TARGET1        38  /// Whichever of ABS32 and REL32 the platform picked.
#define R_ARM_V4BX           40  /// Marks a BX that an ARMv4 link may rewrite.
#define R_ARM_PREL31         42  /// A 31 bit offset; the top bit is a flag of its own.
#define R_ARM_MOVW_ABS_NC    43
#define R_ARM_MOVT_ABS       44
#define R_ARM_MOVW_PREL_NC   45
#define R_ARM_MOVT_PREL      46
#define R_ARM_THM_MOVW_ABS_NC 47
#define R_ARM_THM_MOVT_ABS   48
#define R_ARM_GOT_PREL       96
#define R_ARM_TLS_LE32       108
#define R_ARM_NUM            256 /// The whole eight bit range, since ELF32_R_TYPE is a byte.

/// AArch64 relocation types.  See arm64-link.c for what each one calculates.
/// The static types start at 257 and the dynamic ones at 1024.
#define R_AARCH64_ABS64                257
#define R_AARCH64_ABS32                258
#define R_AARCH64_PREL32               261
#define R_AARCH64_MOVW_UABS_G0_NC      264
#define R_AARCH64_MOVW_UABS_G1_NC      266
#define R_AARCH64_MOVW_UABS_G2_NC      268
#define R_AARCH64_MOVW_UABS_G3         269
#define R_AARCH64_ADR_PREL_PG_HI21     275
#define R_AARCH64_ADD_ABS_LO12_NC      277
#define R_AARCH64_LDST8_ABS_LO12_NC    278
#define R_AARCH64_TSTBR14              279
#define R_AARCH64_CONDBR19             280
#define R_AARCH64_JUMP26               282
#define R_AARCH64_CALL26               283
#define R_AARCH64_LDST16_ABS_LO12_NC   284
#define R_AARCH64_LDST32_ABS_LO12_NC   285
#define R_AARCH64_LDST64_ABS_LO12_NC   286
#define R_AARCH64_LDST128_ABS_LO12_NC  299
#define R_AARCH64_ADR_GOT_PAGE         311
#define R_AARCH64_LD64_GOT_LO12_NC     312
#define R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21  541
#define R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC 542
#define R_AARCH64_TLSLE_ADD_TPREL_HI12 549
#define R_AARCH64_TLSLE_ADD_TPREL_LO12 550
#define R_AARCH64_TLSLE_ADD_TPREL_LO12_NC 551
/// The descriptor form of global dynamic, which is what a compiler emits for
/// this architecture by default.  Each names one instruction of the four the
/// sequence is made of, so relaxing it needs no agreement between them.
#define R_AARCH64_TLSDESC_ADR_PAGE21   562
#define R_AARCH64_TLSDESC_LD64_LO12    563
#define R_AARCH64_TLSDESC_ADD_LO12     564
#define R_AARCH64_TLSDESC_CALL         569
#define R_AARCH64_TLS_TPREL64          1030
#define R_AARCH64_COPY                 1024
#define R_AARCH64_GLOB_DAT             1025
#define R_AARCH64_JUMP_SLOT            1026
#define R_AARCH64_RELATIVE             1027
#define R_AARCH64_NUM                  1033 /// One past the highest dynamic type.

/// RISC-V relocation types.  See riscv64-link.c for what each one calculates.
#define R_RISCV_32            1
#define R_RISCV_64            2
#define R_RISCV_RELATIVE      3
#define R_RISCV_COPY          4
#define R_RISCV_JUMP_SLOT     5
#define R_RISCV_TLS_DTPMOD64  7   /// Module half of a thread-local descriptor.
#define R_RISCV_TLS_DTPREL64  9   /// Offset half of a thread-local descriptor.
#define R_RISCV_TLS_TPREL64   11  /// A thread-pointer-relative offset in data.
#define R_RISCV_BRANCH        16
#define R_RISCV_JAL           17
#define R_RISCV_CALL          18
#define R_RISCV_CALL_PLT      19
#define R_RISCV_GOT_HI20      20
#define R_RISCV_TLS_GOT_HI20  21  /// Initial exec: the slot holds one offset.
#define R_RISCV_TLS_GD_HI20   22  /// Global dynamic: the slot holds a pair.
#define R_RISCV_PCREL_HI20    23
#define R_RISCV_PCREL_LO12_I  24
#define R_RISCV_PCREL_LO12_S  25
#define R_RISCV_HI20          26  /// Absolute addressing, the medlow model.
#define R_RISCV_LO12_I        27  /// Its low half, in a load's field.
#define R_RISCV_LO12_S        28  /// Its low half, in a store's field.
#define R_RISCV_TPREL_HI20    29
#define R_RISCV_TPREL_LO12_I  30
#define R_RISCV_TPREL_LO12_S  31  /// The same low half, in a store's field.
#define R_RISCV_TPREL_ADD     32  /// Marks the add that brings in tp.
#define R_RISCV_ADD16         34
#define R_RISCV_ADD32         35
#define R_RISCV_ADD64         36
#define R_RISCV_SUB8          37
#define R_RISCV_SUB16         38
#define R_RISCV_SUB32         39
#define R_RISCV_SUB64         40
#define R_RISCV_ALIGN         43  /// Marks padding a relaxing linker may shrink.
#define R_RISCV_RVC_BRANCH    44
#define R_RISCV_RVC_JUMP      45
#define R_RISCV_RELAX         51  /// Says the neighbouring relocation may be relaxed.
#define R_RISCV_SUB6          52
#define R_RISCV_SET6          53
#define R_RISCV_SET8          54
#define R_RISCV_SET16         55
#define R_RISCV_32_PCREL      57
#define R_RISCV_SET_ULEB128   60  /// A field whose width the assembler chose; unpatchable.
#define R_RISCV_SUB_ULEB128   61  /// The same, subtracting.
#define R_RISCV_NUM           62  /// One past the highest type, so it sizes a table.

/// ---------------------------------------------------------------- segments

/// One segment of a 32-bit image: what the loader maps, as opposed to the
/// sections the linker built it from.
typedef struct {
    Elf32_Word p_type;   /// PT_* saying what the segment is for.
    Elf32_Off  p_offset; /// Where the contents start in the file.
    Elf32_Addr p_vaddr;  /// Address the segment is loaded at.
    Elf32_Addr p_paddr;  /// Physical address, which only bare-metal loaders read.
    Elf32_Word p_filesz; /// Bytes taken from the file, at most p_memsz.
    Elf32_Word p_memsz;  /// Bytes in memory; the excess over p_filesz is zeroed.
    Elf32_Word p_flags;  /// PF_R, PF_W and PF_X.
    Elf32_Word p_align;  /// p_vaddr and p_offset must agree modulo this.
} Elf32_Phdr;

/// The same for a 64-bit image.  p_flags moves up beside p_type so that the
/// widened members stay naturally aligned.
typedef struct {
    Elf64_Word  p_type;   /// PT_* saying what the segment is for.
    Elf64_Word  p_flags;  /// PF_R, PF_W and PF_X.
    Elf64_Off   p_offset; /// Where the contents start in the file.
    Elf64_Addr  p_vaddr;  /// Address the segment is loaded at.
    Elf64_Addr  p_paddr;  /// Physical address, which only bare-metal loaders read.
    Elf64_Xword p_filesz; /// Bytes taken from the file, at most p_memsz.
    Elf64_Xword p_memsz;  /// Bytes in memory; the excess over p_filesz is zeroed.
    Elf64_Xword p_align;  /// p_vaddr and p_offset must agree modulo this.
} Elf64_Phdr;

/// p_type
#define PT_LOAD    1  /// Mapped into memory; everything else describes something.
#define PT_DYNAMIC 2  /// Where the dynamic linker finds its instructions.
#define PT_INTERP  3  /// Path of the interpreter to run the image with.
#define PT_NOTE    4
#define PT_PHDR    6  /// The program header table itself, so a program can find it.
#define PT_TLS     7  /// The template each thread's TLS block is copied from.

/// p_type, GNU extensions in the OS range.
#define PT_GNU_EH_FRAME 0x6474e550  /// The .eh_frame_hdr an unwinder starts from.
#define PT_GNU_RELRO    0x6474e552  /// Made read-only once the dynamic linker is done.

/// p_flags
#define PF_X (1 << 0)
#define PF_W (1 << 1)
#define PF_R (1 << 2)

/// ---------------------------------------------------------------- notes
///
/// A note is the header below, then n_namesz bytes of name and n_descsz bytes
/// of descriptor, each padded up to four bytes.  Both classes use 4-byte
/// fields; only the padding rule differs between implementations, and the
/// 4-byte one is what is written here.

/// The header of one note in a 32-bit file.
typedef struct {
    Elf32_Word n_namesz; /// Bytes of name that follow, padded up to four.
    Elf32_Word n_descsz; /// Bytes of descriptor after the name, padded up to four.
    Elf32_Word n_type;   /// Meaning of the descriptor, interpreted per name.
} Elf32_Nhdr;

/// The same in a 64-bit file: the fields do not widen.
typedef struct {
    Elf64_Word n_namesz; /// Bytes of name that follow, padded up to four.
    Elf64_Word n_descsz; /// Bytes of descriptor after the name, padded up to four.
    Elf64_Word n_type;   /// Meaning of the descriptor, interpreted per name.
} Elf64_Nhdr;

/// First word of an "GNU" ABI-tag note descriptor.
#define ELF_NOTE_OS_GNU 1

/// ---------------------------------------------------------------- dynamic

/// One instruction to the dynamic linker, in a 32-bit file.
typedef struct {
    Elf32_Sword d_tag;      /// DT_* naming what this entry holds.
    union {
        Elf32_Word d_val;   /// Read as an integer.
        Elf32_Addr d_ptr;   /// Read as an address.
    } d_un;                 /// Which member applies is decided by d_tag.
} Elf32_Dyn;

/// The same in a 64-bit file.
typedef struct {
    Elf64_Sxword d_tag;     /// DT_* naming what this entry holds.
    union {
        Elf64_Xword d_val;  /// Read as an integer.
        Elf64_Addr  d_ptr;  /// Read as an address.
    } d_un;                 /// Which member applies is decided by d_tag.
} Elf64_Dyn;

/// d_tag.  The table is read in order and ends at DT_NULL, so an entry that
/// is not understood is skipped rather than fatal.
#define DT_NULL            0   /// Ends the table.
#define DT_NEEDED          1   /// Name of an object this one depends on.
#define DT_PLTRELSZ        2
#define DT_PLTGOT          3   /// Address of the GOT, or of the part the PLT uses.
#define DT_HASH            4
#define DT_STRTAB          5
#define DT_SYMTAB          6
#define DT_RELA            7
#define DT_RELASZ          8
#define DT_RELAENT         9
#define DT_STRSZ           10
#define DT_SYMENT          11
#define DT_INIT            12
#define DT_FINI            13
#define DT_SONAME          14  /// The name other objects should record in DT_NEEDED.
#define DT_RPATH           15  /// Search path, consulted before LD_LIBRARY_PATH.
#define DT_SYMBOLIC        16  /// Bind references to this object's own definitions first.
#define DT_REL             17
#define DT_RELSZ           18
#define DT_RELENT          19
#define DT_PLTREL          20  /// Whether the PLT relocations are DT_REL or DT_RELA.
#define DT_DEBUG           21  /// Left for a debugger to write the link map into.
#define DT_TEXTREL         22  /// Relocations reach into read-only sections.
#define DT_JMPREL          23  /// The PLT's own relocations, which may be bound lazily.
#define DT_INIT_ARRAY      25
#define DT_FINI_ARRAY      26
#define DT_INIT_ARRAYSZ    27
#define DT_FINI_ARRAYSZ    28
#define DT_RUNPATH         29  /// Search path, consulted after LD_LIBRARY_PATH.
#define DT_FLAGS           30
#define DT_PREINIT_ARRAY   32
#define DT_PREINIT_ARRAYSZ 33

/// d_tag, GNU extensions in the OS range.
#define DT_GNU_HASH   0x6ffffef5  /// The faster hash that replaced DT_HASH.
#define DT_VERSYM     0x6ffffff0  /// One version index per dynamic symbol.
#define DT_RELACOUNT  0x6ffffff9  /// How many leading RELA entries are RELATIVE.
#define DT_RELCOUNT   0x6ffffffa  /// The same for REL.
#define DT_FLAGS_1    0x6ffffffb
#define DT_VERNEED    0x6ffffffe
#define DT_VERNEEDNUM 0x6fffffff

/// DT_FLAGS bits
#define DF_BIND_NOW 0x00000008  /// Resolve everything at load time, not on first call.

/// DT_FLAGS_1 bits
#define DF_1_NOW      0x00000001  /// The newer spelling of DF_BIND_NOW.
#define DF_1_NODELETE 0x00000008  /// Stays mapped even after it is unloaded.
#define DF_1_PIE      0x08000000  /// This ET_DYN is an executable, not a library.

/// ---------------------------------------------------------------- versions
///
/// The GNU symbol versioning records.  Both classes use identical layouts, so
/// the two typedefs describe the same bytes; they exist so ElfW() resolves.
/// vd_next / vn_next / vda_next / vna_next are byte offsets from the record
/// they appear in, not indices, and a zero next ends the chain.

/// A version this object defines, in a 32-bit file.
typedef struct {
    Elf32_Half vd_version; /// Always 1.
    Elf32_Half vd_flags;   /// Marks the entry that names the object itself.
    Elf32_Half vd_ndx;     /// The index the versym table refers to this by.
    Elf32_Half vd_cnt;     /// Verdaux records in this definition's chain.
    Elf32_Word vd_hash;    /// Hash of the version name, for a quick reject.
    Elf32_Word vd_aux;     /// Byte offset from here to the first Verdaux.
    Elf32_Word vd_next;    /// Byte offset to the next Verdef, or zero to end.
} Elf32_Verdef;

/// The same record in a 64-bit file, which is the same bytes.
typedef struct {
    Elf64_Half vd_version; /// Always 1.
    Elf64_Half vd_flags;   /// Marks the entry that names the object itself.
    Elf64_Half vd_ndx;     /// The index the versym table refers to this by.
    Elf64_Half vd_cnt;     /// Verdaux records in this definition's chain.
    Elf64_Word vd_hash;    /// Hash of the version name, for a quick reject.
    Elf64_Word vd_aux;     /// Byte offset from here to the first Verdaux.
    Elf64_Word vd_next;    /// Byte offset to the next Verdef, or zero to end.
} Elf64_Verdef;

/// The name a Verdef carries; the first is the version, the rest its parents.
typedef struct {
    Elf32_Word vda_name; /// Offset of the name in the dynamic string table.
    Elf32_Word vda_next; /// Byte offset to the next Verdaux, or zero to end.
} Elf32_Verdaux;

/// The same record in a 64-bit file, which is the same bytes.
typedef struct {
    Elf64_Word vda_name; /// Offset of the name in the dynamic string table.
    Elf64_Word vda_next; /// Byte offset to the next Verdaux, or zero to end.
} Elf64_Verdaux;

/// The versions this object needs from one of its dependencies.
typedef struct {
    Elf32_Half vn_version; /// Always 1.
    Elf32_Half vn_cnt;     /// Vernaux records in this requirement's chain.
    Elf32_Word vn_file;    /// Offset of the dependency's name in the string table.
    Elf32_Word vn_aux;     /// Byte offset from here to the first Vernaux.
    Elf32_Word vn_next;    /// Byte offset to the next Verneed, or zero to end.
} Elf32_Verneed;

/// The same record in a 64-bit file, which is the same bytes.
typedef struct {
    Elf64_Half vn_version; /// Always 1.
    Elf64_Half vn_cnt;     /// Vernaux records in this requirement's chain.
    Elf64_Word vn_file;    /// Offset of the dependency's name in the string table.
    Elf64_Word vn_aux;     /// Byte offset from here to the first Vernaux.
    Elf64_Word vn_next;    /// Byte offset to the next Verneed, or zero to end.
} Elf64_Verneed;

/// One version named by a Verneed.
typedef struct {
    Elf32_Word vna_hash;  /// Hash of the version name, for a quick reject.
    Elf32_Half vna_flags; /// Marks a dependency that is allowed to go unmet.
    Elf32_Half vna_other; /// The index the versym table refers to this by.
    Elf32_Word vna_name;  /// Offset of the name in the dynamic string table.
    Elf32_Word vna_next;  /// Byte offset to the next Vernaux, or zero to end.
} Elf32_Vernaux;

/// The same record in a 64-bit file, which is the same bytes.
typedef struct {
    Elf64_Word vna_hash;  /// Hash of the version name, for a quick reject.
    Elf64_Half vna_flags; /// Marks a dependency that is allowed to go unmet.
    Elf64_Half vna_other; /// The index the versym table refers to this by.
    Elf64_Word vna_name;  /// Offset of the name in the dynamic string table.
    Elf64_Word vna_next;  /// Byte offset to the next Vernaux, or zero to end.
} Elf64_Vernaux;


/// ---- src/core/tl_stab.h ----


/// The stab type codes a backtrace walk has to recognise: where a function
/// begins, which line an address belongs to, and how source files nest.
#define N_FUN   0x24  /// Function name; n_value is its address.
#define N_SLINE 0x44  /// Source line in text; n_desc is the line number.
#define N_SO    0x64  /// Name of the source file this object was built from.
#define N_BINCL 0x82  /// Start of an included file's stabs.
#define N_SOL   0x84  /// Name of the file the following stabs came from.
#define N_EINCL 0xa2  /// End of an included file's stabs.


/// ------------------------------------------------------------------------
/// Linkage.
///
/// A core compiles as one translation unit with ONE_SOURCE set, and then
/// everything it defines becomes static and both cores can live in one
/// library without their symbols meeting.  The three classes say what a
/// name would be if that were not so: ST_FUNC and ST_DATA are internal to the
/// core, PUB_FUNC leaves it.

#ifndef PUB_FUNC
# define PUB_FUNC
#endif

#ifndef ONE_SOURCE
# define ONE_SOURCE 0
#endif

#if ONE_SOURCE
#define ST_INLN static inline
#define ST_FUNC static
#define ST_DATA static
#else
#define ST_INLN
#define ST_FUNC
#define ST_DATA extern
#endif

#ifdef TLI_PROFILE  /// keep every function out of line, so it can be sampled
# define static
# define inline
#endif

/// ------------------------------------------------------------------------
/// Target definitions.
///
/// Upstream took these from the code generators.  Those are gone, and what a
/// linker still needs of them is the target's pointer width and, on ARM, the
/// float ABI, which shows up in the ELF header flags and in how a call is
/// relocated.

/// The pointer width in bytes of every target this core links for, which also
/// picks the ELF class.  The unit that compiles the core defines it; see
/// src/backends/backend.inc.
#if PTR_SIZE != 8 && PTR_SIZE != 4
# error "define PTR_SIZE as 8 or 4 before including the core"
#endif

#define ARM_SOFTFP_FLOAT 0  /// Floats in the integer registers.
#define ARM_HARD_FLOAT   1  /// Floats in the VFP registers.

/// ARM architecture version the output claims, which the build attributes
/// carry.  A build option rather than a fact about ARM, so it is a macro.
#ifndef TLI_CPUVER
# define TLI_CPUVER 5
#endif

/// ------------------------------------------------------------------------
/// ELF class.
///
/// The two ELF classes differ only in the width of a few fields, and the code
/// that reads and writes them is otherwise the same.  Rather than carry both
/// everywhere, a build picks one and reaches every ELF type through ElfW() and
/// every ELF accessor macro through ELFW().  Which class it is follows from
/// the target's pointer width, and nothing else in the tree needs to know.

#if PTR_SIZE == 8
# define ELFCLASSW ELFCLASS64
# define ElfW(type) Elf64_##type
# define ELFW(type) ELF64_##type
# define ElfW_Rel ElfW(Rela)
# define SHT_RELX SHT_RELA
# define REL_SECTION_FMT ".rela%s"
#else
# define ELFCLASSW ELFCLASS32
# define ElfW(type) Elf32_##type
# define ELFW(type) ELF32_##type
# define ElfW_Rel ElfW(Rel)
# define SHT_RELX SHT_REL
# define REL_SECTION_FMT ".rel%s"
#endif

/// Whether a relocation record carries its addend is a property of the class
/// in this tree: the 64-bit targets use RELA and the 32-bit ones REL.  Code
/// that has to care tests SHT_RELX against SHT_RELA.

#define addr_t ElfW(Addr)  /// an address in the image being built
#define ElfSym ElfW(Sym)  /// a symbol table entry of this build's ELF class

/// IMAGE_FILE_HEADER.Characteristics, the flags that describe the whole image.
#define PE_FILE_RELOCS_STRIPPED     0x0001
#define PE_FILE_EXECUTABLE_IMAGE    0x0002
#define PE_FILE_LINE_NUMS_STRIPPED  0x0004
#define PE_FILE_LOCAL_SYMS_STRIPPED 0x0008
#define PE_FILE_LARGE_ADDRESS_AWARE 0x0020
#define PE_FILE_32BIT_MACHINE       0x0100
#define PE_FILE_DEBUG_STRIPPED      0x0200
#define PE_FILE_DLL                 0x2000

/// ------------------------------------------------------------------------
/// The architecture a link is for.
///
/// Everything the generic code needs of a machine: the code that relocates
/// for it, the numbers it writes into headers and into the relocations it
/// makes itself.  Each *-link.c file defines one, and a link reads it through
/// s1->arch, so that the code reading it does not have to be compiled once
/// per machine.  Behaviour that differs in a line or two is not here: the
/// generic code tests em for it.
struct tli_SymAttr;
typedef struct tli_Arch {
    u16 em;              /// e_machine an object must carry to link here.

    /// Apply one relocation: patch the bytes at ptr, which will live at addr,
    /// so that they refer to val.  Called once per relocation.
    void (*relocate)(tli_State *s1, ElfW_Rel *rel, i32 type, u8 *ptr,
                     addr_t addr, addr_t val);
    /// Emit one PLT stub reading the given GOT slot, and return its offset.
    u32 (*create_plt_entry)(tli_State *s1, u32 got_offset,
                            struct tli_SymAttr *attr);
    /// Patch the finished .plt once the GOT has an address.
    void (*relocate_plt)(tli_State *s1);
    /// The RC_* byte for a relocation type, zero for one it does not know.
    u32 (*reloc_props)(i32 type);

    u32 r_data_ptr;      /// A pointer-sized absolute word.
    u32 r_data_32dw;     /// A 32-bit offset between two debug sections.
    u32 r_jmp_slot;      /// A PLT slot the loader binds.
    u32 r_glob_dat;      /// A GOT slot the loader fills with an address.
    u32 r_copy;          /// Copy a library's data into the executable.
    u32 r_relative;      /// Add the load bias to a word.
    addr_t start_addr;   /// Where an executable is loaded by default.
    u32 page_size;       /// How finely segments may be placed.
    u8 pcrel_dllplt;     /// PLT entries reach the GOT pc-relatively.
    const char *elf_interp;  /// The loader PT_INTERP names; see TLI_ELFINTERP.
    u32 r_plt32;         /// A PLT call with a plain pc-relative twin, or 0.
    u32 r_pcrel32;       /// That twin, which asks for no PLT entry.
    u16 build_reloc[22]; /// The type each tl_build_reloc() kind becomes, or 0.

    // PE/COFF.  A machine Windows does not run on leaves these zero.
    u16 pe_machine;           /// IMAGE_FILE_HEADER.Machine.
    u16 pe_os_version;        /// Oldest Windows that loads the image, major<<8 | minor.
    u16 pe_rsrc_reltype;      /// The one relocation type a .rsrc section may carry.
    u16 pe_characteristics;   /// IMAGE_FILE_* flags every image starts with.
    u64 pe_image_base_exe;    /// Where an executable prefers to be mapped.
    u64 pe_image_base_dll;    /// Where a DLL prefers to be mapped.
    u32 r_pe_abs;             /// An absolute pointer, which needs a .reloc entry.
    u32 r_pe_rva;             /// An image-relative address.
    u32 r_pe_thunk;           /// The operand of an import thunk.

    // Mach-O.  A machine Apple does not ship leaves these zero.
    u32 macho_cputype;        /// mach_header.cputype.
    u32 macho_cpusubtype;     /// mach_header.cpusubtype of an object.
} tli_Arch;

/// ------------------------------------------------------------------------
/// The types a link is carried in.

/// End of input for the linker script scanner in tl_input.c.  It is outside
/// the range of an unsigned char, so no byte of a script can be mistaken for
/// it.
#define CH_EOF   (-1)

/// A string that grows.  size is what is in it and size_allocated what it can
/// hold; data is not necessarily NUL terminated, since a tli_CString is also used
/// to accumulate bytes.
typedef struct tli_CString {
    i32 size;  /// Bytes written so far, which is also the append point.
    i32 size_allocated;  /// Capacity of data, doubled as it is outgrown.
    char *data;  /// The bytes; NULL until something is put in them.
} tli_CString;

/// A section: a byte buffer, an ELF section header, and the handful of links
/// to other sections that the header expresses as indices.
///
/// data_offset is the append point and grows as input is merged in; sh_size is
/// the size the written header will carry and is only set once layout has run,
/// which is why the two exist separately.  The name is a trailing array, so a
/// section is one allocation.
typedef struct tli_Section {
    u64 data_offset;  /// current append point
    u8 *data;  /// the bytes, NULL for SHT_NOBITS
    u64 data_allocated;  /// capacity of data
    tli_State *s1;  /// The link this section belongs to.
    i32 sh_name;  /// offset in .shstrtab, set when writing
    i32 sh_num;  /// index in s1->sections, 0 if private
    i32 sect_sym;  /// STT_SECTION symbol standing for it, 0 until one is asked for
    i32 sh_type;  /// SHT_*
    i32 sh_flags;  /// SHF_*, plus the two private flags
    i32 sh_info;  /// meaning depends on sh_type
    i32 sh_addralign;  /// Alignment, raised by section_add() as it is asked for.
    i32 sh_entsize;  /// size of one element of a table section
    u64 sh_size;  /// size in the output, set during layout
    addr_t sh_addr;  /// address the section is relocated to
    u64 sh_offset;  /// offset in the output file
    i32 nb_hashed_syms;  /// symbols in ->hash, to decide when to resize
    struct tli_Section *link;  /// sh_link: strings of a symtab, and so on
    struct tli_Section *reloc;  /// the relocations that apply to this section
    struct tli_Section *hash;  /// the hash over this symbol table
    struct tli_Section *xindex;  /// the section numbers st_shndx cannot hold
    struct tli_Section *prev;  /// previous section on the section stack
    char name[1];  /// Trailing array, so header and name are one allocation.
} tli_Section;

/// A shared library the link depends on.  level is how far removed it is: zero
/// for one named on the command line, more for one reached through another
/// library's DT_NEEDED.
typedef struct tli_DLLReference {
    i32 level;  /// 0 for a library named on the command line, more if reached.
    void *handle;  /// dlopen() handle, when the link resolves through the host.
    u8 found, index;  /// Whether it was opened, and its place in DT_NEEDED.
    char name[1];  /// Trailing array, so the record is one allocation.
} tli_DLLReference;

/// What a symbol picks up while being linked and has nowhere to live in
/// ElfW(Sym).  Indexed by the symbol's place in s1->symtab; see
/// get_sym_attr().
struct tli_SymAttr {
    u32 got_offset;  /// its slot in .got, or 0
    u32 plt_offset;  /// its stub in .plt, or 0
    i32 plt_sym;  /// the symbol naming that stub
    i32 dyn_index;  /// its index in the output's .dynsym
    u8 linker_sym:1;  /// the linker defined it, so may update it
    u8 plt_thumb_stub:1;  /// ARM: its PLT entry needs the ARM-to-Thumb veneer.
};

/// Everything one link owns.  Several of these can be alive at once, which is
/// why nothing below is a global.
/// One function's frame description, as the .eh_frame_hdr search table holds
/// it: where the function starts, and where the record describing it is.
struct tli_EhEntry {
    addr_t start;  /// First address of the function the FDE covers.
    addr_t fde;    /// Address of the FDE itself.
};

/// One "B - A" that a Mach-O SUBTRACTOR pair asked for and that could not be
/// answered on the spot, because the two symbols were bound for different
/// output sections and so neither had an address yet.
///
/// Keeping it costs a pointer and defers the subtraction to just after
/// relocate_syms(), when both values are absolute and the difference is the
/// constant the object always meant it to be.
struct tli_SymDiff {
    i32 sec;     /// Output section holding the field to patch.
    usize offset;  /// Where in it, in bytes.
    i32 sym_a;   /// Subtracted: the symbol the SUBTRACTOR named.
    i32 sym_b;   /// Added: the one the relocation after it named.
    i32 len;     /// Log2 of the field width, as Mach-O spells it.
};

struct tli_State {
    const tli_Arch *arch;  /// The machine the output is for.
    u8 verbose;  /// if true, display some information during compilation
    u8 nostdlib;  /// if true, no standard libraries are added
    u8 nostdlib_paths;  /// if true, the default paths are not searched for libraries
    u8 nocommon;  /// if true, do not use common symbols for .bss data
    u8 static_link;  /// if true, static linking is performed
    u8 rdynamic;  /// if true, all symbols are exported
    u8 symbolic;  /// if true, resolve symbols in the current module first
    u8 znodelete;  /// Set DF_1_NODELETE in dynamic section
    u8 filetype;  /// how the next input file is to be read
    u8 enable_new_dtags;  /// -Wl,--enable-new-dtags

    u8 leading_underscore;  /// the target decorates C names with _
    u8 target_os;  /// TLI_OS_*: which OS the output image is stamped for.
    u8 fmt;  /// TLI_FMT_*: which object format the output image is written in.

    /// The warning switches.  Each is on or off on its own; warn_num is how
    /// tli_warning_c() names one of them to the printer, as an offset from
    /// warn_none, which is what lets one printer serve all of them.
    u8 warn_none;  /// -w: every warning below is suppressed.
    u8 warn_all;  /// -Wall: the whole set is on.
    u8 warn_error;  /// -Werror: a warning fails the link.
    u8 warn_unsupported;  /// -Wunsupported: something the linker ignored.
    u8 warn_num;  /// Offset from warn_none of the switch being reported.

    u8 option_r;  /// option -r: write a relocatable object

    /// Debug information, and the runtime that reads it after a fault.
    u8 do_debug;  /// Keep the debug sections the inputs carry.
    u8 dwarf;  /// DWARF version to emit, 0 for stabs instead.
    u8 do_backtrace;  /// Emit the rt_context a backtrace is read through.
#ifdef TLI_BCHECK
    u8 do_bounds_check;  /// Link in the bounds checker and its sections.
#endif


    u8 float_abi;  /// float ABI of the generated code

    u8 has_text_addr;  /// Whether text_addr was given rather than defaulted.
    addr_t text_addr;  /// address of text section
    u32 section_align;  /// section alignment

    char *tli_lib_path;  /// CONFIG_TLIDIR or -B option
    char *soname;  /// as specified on the command line (-soname)
    char *rpath;  /// as specified on the command line (-Wl,-rpath=)
    char *elfint;  /// -Wl,-I... on command line
    char *elf_entryname;  /// "_start" unless set
    char *init_symbol;  /// symbols to call at load-time (not used currently)
    char *fini_symbol;  /// symbols to call at unload-time (not used currently)
    char *mapfile;  /// create a mapfile (not used currently)

    i32 output_type;  /// TLI_OUTPUT_*: executable, DLL, object or memory.
    i32 output_format;  /// TLI_OUTPUT_FORMAT_*: ELF, flat binary or COFF.
    tli_DLLReference **loaded_dlls;  /// Every library read, directly or not.
    i32 nb_loaded_dlls;  /// Entries in loaded_dlls.
    char **coff_aliases;  /// COFF weak externals: a name, then the one it stands for.
    i32 nb_coff_aliases;  /// Entries in coff_aliases, two per alias.

    char **library_paths;  /// Where -l is looked up, in order.
    i32 nb_library_paths;  /// Entries in library_paths.

    char **crt_paths;  /// Where crt1.o, crti.o and crtn.o are looked up.
    i32 nb_crt_paths;  /// Entries in crt_paths.

    /// Error handling.  error_jmp_buf is where _tcc_error() goes when the
    /// caller has armed it, so that a fatal error unwinds rather than exits.
    void *error_opaque;  /// Passed back to error_func untouched.
    void (*error_func)(void *opaque, const char *msg);  /// The embedder's sink.
    i32 error_set_jmp_enabled;  /// Whether error_jmp_buf holds a live target.
    jmp_buf error_jmp_buf;  /// Where a fatal error unwinds to.
    i32 nb_errors;  /// Errors so far; non-zero means the link failed.

    char **pragma_libs;  /// Libraries an input asked for by pragma.
    i32 nb_pragma_libs;  /// Entries in pragma_libs.

    tli_Section **sections;  /// The output sections, by section number.
    i32 nb_sections;  /// number of sections, including first dummy section

    tli_Section **priv_sections;  /// Bookkeeping sections, never written.
    i32 nb_priv_sections;  /// Entries in priv_sections.

    /// Name index over `sections`, so that merging an input does not have to
    /// walk the whole list per section. Open addressed, holding borrowed
    /// pointers; `sections` owns them.
    tli_Section **sec_by_name;  /// Open-addressed name index over sections.
    i32 sec_by_name_size;  /// Slots in sec_by_name; a power of two.

    /// The four sections every image has, created by tli_elf_new() in the
    /// order they are conventionally laid out in.  Their numbers are relied
    /// on elsewhere, so the order is part of the interface.
    tli_Section *text_section, *data_section, *rodata_section, *bss_section;
    tli_Section *common_section;  /// Stands in for SHN_COMMON until .bss takes over.
#ifdef TLI_BCHECK
    tli_Section *bounds_section;  /// contains global data bound description
    tli_Section *lbounds_section;  /// contains local data bound description
#endif
    union { tli_Section *symtab_section, *symtab; };  /// The image's own symbols.
    tli_Section *dynsymtab_section;  /// What the input libraries export, merged.
    tli_Section *dynsym;  /// The dynamic symbol table of the output, if it has one.
    tli_Section *got, *plt;  /// The global offset table and the stubs that read it.
    tli_Section *stab_section;  /// Stabs debug info; its ->link is the string table.
    tli_Section *dwarf_info_section;  /// .debug_info: the DWARF tree itself.
    tli_Section *dwarf_abbrev_section;  /// .debug_abbrev: the shapes its entries take.
    tli_Section *dwarf_line_section;  /// .debug_line: the line number program.
    tli_Section *dwarf_aranges_section;  /// .debug_aranges: address ranges per unit.
    tli_Section *dwarf_str_section;  /// .debug_str: strings the DWARF tree shares.
    tli_Section *dwarf_line_str_section;  /// .debug_line_str: DWARF 5 file names.
    i32 dwlo, dwhi;  /// dwarf section range

    struct tli_SymAttr *sym_attrs;  /// GOT and PLT state, parallel to symtab.
    i32 nb_sym_attrs;  /// Entries in sym_attrs; a power of two.
    ElfW_Rel *qrel;  /// Where relocate() writes the records to keep.
    #define qrel s1->qrel

    addr_t tls_start, tls_end;  /// Extent of the thread-local block, once laid out.

    /// PCREL_HI20 relocations still waiting for the LO12 that completes them
    struct pcrel_hi { addr_t addr, val; } **pcrel_hi_entries;
    i32 nb_pcrel_hi_entries;

    /// Set when .eh_frame could not be parsed, so that the one diagnostic
    /// about it is not repeated once per pass.
    u8 eh_frame_unreadable;

    /// Leave the local symbols out of the image, as -s asks.  What a loader
    /// needs is kept whatever this says: the exports and the imports are not
    /// in the table this drops from.
    u8 strip;

    /// Set once a dynamic thread-local sequence has been rewritten into the
    /// local-exec one, which takes its call to __tls_get_addr with it.
    u8 tls_relaxed;

    /// Symbol differences that could not be worked out when they were read,
    /// waiting for the layout that makes them constant.  See tli_SymDiff.
    struct tli_SymDiff **sym_diffs;
    i32 nb_sym_diffs;

    i32 pe_subsystem;  /// IMAGE_SUBSYSTEM_*: console, GUI or one of the EFI kinds.
    u32 pe_characteristics;  /// IMAGE_FILE_* flags of the COFF header.
    u32 pe_dll_characteristics;  /// IMAGE_DLLCHARACTERISTICS_*: ASLR, NX.
    u32 pe_file_align;  /// Alignment of a section in the file, not in memory.
    u32 pe_stack_size;  /// Stack the image reserves, in bytes.
    addr_t pe_imagebase;  /// Address the image prefers to be mapped at.
    tli_Section *uw_pdata;  /// .pdata: the unwind table these two targets require.

    char *install_name;  /// The path a dylib records for its clients.
    u32 compatibility_version;  /// Oldest version a client may bind to.
    u32 current_version;  /// Version of this dylib, as LC_ID_DYLIB records it.
    u32 macho_platform;  /// LC_BUILD_VERSION platform; zero means macOS.
    u32 macho_minos;  /// Minimum OS as x.y.z; zero takes the platform's own default.

    i32 nb_sym_versions;  /// Entries in sym_versions.
    struct tli_SymVersion *sym_versions;  /// Versions the inputs define or need.
    i32 nb_sym_to_version;  /// Entries in sym_to_version.
    i32 *sym_to_version;  /// Version of each symbol of dynsymtab_section, by index.
    i32 dt_verneednum;  /// Verneed records written, which DT_VERNEEDNUM carries.
    tli_Section *versym_section;  /// .gnu.version: one halfword per dynamic symbol.
    tli_Section *verneed_section;  /// .gnu.version_r: what each library must supply.

    u8 native;  /// The output is for this machine, so it may run in this process.

#ifdef TLI_HOST_RUN
    const char *run_main;  /// entry for tli_run()
    void *run_ptr;  /// runtime_memory
    u32 run_size;  /// size of runtime_memory
#ifdef _WIN64
    void *run_function_table;  /// unwind data
#endif
    struct tli_State *next;  /// The chain of live states a fault handler walks.
    struct rt_context *rc;  /// pointer to backtrace info block
    void *run_lj, *run_jb;  /// sj/lj for tli_setjmp()/tli_run()
    TLIBtFunc *bt_func;  /// Called instead of printing when a backtrace is taken.
    void *bt_data;  /// Passed back to bt_func untouched.
#endif

#ifdef TLI_BACKTRACE
    i32 rt_num_callers;  /// Frames a backtrace prints, from -bt=N.
#endif

    /// where tli_load_ldscript() has read up to
    u8 *ld_p;

    /// the input file a diagnostic should blame
    const char *current_filename;

    /// the archive member being loaded from it, or NULL for a plain object
    const char *current_member;

    /// Used by main and tli_parse_args only.
    struct tli_FileSpec **files;  /// files seen on command line
    i32 nb_files;  /// number thereof
    char *outfile;  /// output filename
    char **link_argv;  /// The -Wl options, split into an argv of their own.
    i32 link_argc, link_optind;  /// Length of link_argv, and how far it is read.
};

/// One input named on the command line: type is the AFF_TYPE_* it was given
/// by the options in force when it was seen.
struct tli_FileSpec {
    i8 type;    /// AFF_TYPE_*, from the options in force when it was seen.
    char name[1];  /// Trailing array, so the record is one allocation.
};

/// ------------ tl_core.c ------------

/// The state a diagnostic reports through, entered by tli_enter_state().
/// The only genuinely global thing here, which is what the semaphore guards.
ST_DATA struct tli_State *tli_state;
/// Verbosity of the internal tracing, from the TLI_DEBUG environment.

/// Copy s into buf, truncating rather than overflowing, and return buf.
ST_FUNC char *pstrcpy(char *buf, usize buf_size, const char *s);
/// Append s to buf, truncating rather than overflowing, and return buf.
ST_FUNC char *pstrcat(char *buf, usize buf_size, const char *s);
/// Copy at most num bytes of s into buf and terminate it; return buf.
/// The last component of a path, using whichever separators the host uses.
PUB_FUNC char *tli_basename(const char *name);
/// The extension of a path, or the empty string past its end.
PUB_FUNC char *tli_fileextension (const char *name);

/// Every allocation goes through these, so that one custom allocator can be
/// installed for the whole library and so that MEM_DEBUG can account for it.
PUB_FUNC void tli_free(void *ptr);
PUB_FUNC void *tli_malloc(usize size);
/// As tli_malloc(), with the bytes zeroed.
PUB_FUNC void *tli_mallocz(usize size);
PUB_FUNC void *tli_realloc(void *ptr, usize size);
PUB_FUNC char *tli_strdup(const char *str);

#ifdef MEM_DEBUG
#define tli_free(ptr)           tli_free_debug(ptr)
#define tli_malloc(size)        tli_malloc_debug(size, __FILE__, __LINE__)
#define tli_mallocz(size)       tli_mallocz_debug(size, __FILE__, __LINE__)
#define tli_realloc(ptr,size)   tli_realloc_debug(ptr, size, __FILE__, __LINE__)
#define tli_strdup(str)         tli_strdup_debug(str, __FILE__, __LINE__)
/// The same allocators again, each remembering where it was called from, so
/// that MEM_DEBUG can name the site of anything still held at the end.
PUB_FUNC void tli_free_debug(void *ptr);
PUB_FUNC void *tli_malloc_debug(usize size, const char *file, i32 line);
PUB_FUNC void *tli_mallocz_debug(usize size, const char *file, i32 line);
PUB_FUNC void *tli_realloc_debug(void *ptr, usize size, const char *file, i32 line);
PUB_FUNC char *tli_strdup_debug(const char *str, const char *file, i32 line);
#endif

/// For memory a library handed us, which its own free() has to take back.

/// Poison the allocator the core must not use, so that a slip is a link error
/// with an obvious name rather than a mismatched free at run time.
#define free(p) use_tcc_free(p)
#define malloc(s) use_tcc_malloc(s)
#define realloc(p, s) use_tcc_realloc(p, s)
#undef strdup
#define strdup(s) use_tcc_strdup(s)

/// Report an error and carry on, so that a link can name every fault it
/// finds rather than only the first.  Always returns -1.
PUB_FUNC i32 _tcc_error_noabort(const char *fmt, ...) PRINTF_LIKE(1,2);
/// Report an error and unwind, through error_jmp_buf when it is armed.
PUB_FUNC NORETURN void _tcc_error(const char *fmt, ...) PRINTF_LIKE(1,2);
/// Report something that does not stop the link, unless -Werror is on.
PUB_FUNC void _tcc_warning(const char *fmt, ...) PRINTF_LIKE(1,2);

/// Character classes, as used when deciding whether a section name can be
/// spelled as a C identifier (__start_<name>/__stop_<name>).  Written out
/// rather than taken from <ctype.h> because the answer must not depend on the
/// locale the linker happens to be running in.
static inline i32 isid(i32 c) {
    return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
}
static inline i32 isnum(i32 c) {
    return c >= '0' && c <= '9';
}

/// Append one pointer to a growable array of them, growing it if need be.
/// ptab is the address of the array pointer, which is why it is a void *.
ST_FUNC void dynarray_add(void *ptab, i32 *nb_ptr, void *data);
/// Free every element of such an array, then the array, and zero the count.
ST_FUNC void dynarray_reset(void *pp, i32 *n);

/// Append one byte to a tli_CString.
ST_INLN void cstr_ccat(tli_CString *cstr, i32 ch);
/// Append len bytes to a tli_CString.
ST_FUNC void cstr_cat(tli_CString *cstr, const char *str, i32 len);
/// Start a tli_CString off empty; it owns nothing until something is put in it.
ST_FUNC void cstr_new(tli_CString *cstr);
/// Release a tli_CString's buffer and leave it empty.
ST_FUNC void cstr_free(tli_CString *cstr);
/// Append formatted text to a tli_CString and return the length appended.
ST_FUNC i32 cstr_printf(tli_CString *cs, const char *fmt, ...) PRINTF_LIKE(2,3);
/// As cstr_printf(), for a caller that already has the argument list.
ST_FUNC i32 cstr_vprintf(tli_CString *cstr, const char *fmt, va_list ap);
/// Empty a tli_CString without giving its buffer back, so it can be reused.

/// A pointer registered here is freed if an error unwinds past its owner,
/// which is how a buffer held only in a local survives longjmp.

/// Read one input file, whatever kind it turns out to be: object, archive,
/// shared library, linker script.  The flags below say how to look for it
/// and what to do when it is not there.
ST_FUNC i32 tli_add_file_internal(tli_State *s1, const char *filename, i32 flags);
/// The flags argument of tli_add_file_internal(), above the byte the file
/// type occupies, so that the two can be passed together.
#define AFF_PRINT_ERROR     0x10  /// print error if file not found
#define AFF_REFERENCED_DLL  0x20  /// load a referenced dll from another dll
#define AFF_TYPE_BIN        0x40  /// file to add is binary
#define AFF_WHOLE_ARCHIVE   0x80  /// load all objects from archive

/// How the next input file is to be read: s->filetype, which the options in
/// force when a file is named on the command line put there.
#define AFF_TYPE_NONE   0  /// Decide from what the file turns out to contain.
#define AFF_TYPE_C      1  /// C source, which this linker cannot read.
#define AFF_TYPE_ASM    2  /// Assembly, likewise.
#define AFF_TYPE_ASMPP  4  /// Assembly wanting the preprocessor first.
#define AFF_TYPE_LIB    8  /// A library, so -l naming rules apply.
#define AFF_TYPE_MASK   (7 | AFF_TYPE_BIN)  /// The bits the type occupies.

/// What tli_object_type() found in a file's leading bytes.
#define AFF_BINTYPE_REL 1  /// ELF relocatable object
#define AFF_BINTYPE_DYN 2  /// ELF shared library
#define AFF_BINTYPE_AR  3  /// ar archive
#define AFF_BINTYPE_MACHO_REL 4  /// Mach-O relocatable object
#define AFF_BINTYPE_COFF_REL  5  /// COFF relocatable object
#define AFF_BINTYPE_COFF_IMPORT 6  /// One short import record from a `.lib`

/// What tli_add_file_internal() returns besides 0 and -1, so that a caller
/// searching a path can tell "not here" from "here and broken".
#define FILE_NOT_FOUND -2  /// no such file
#define FILE_NOT_RECOGNIZED -3  /// unrecognized file type

/// Read one of the C runtime's crt objects, looked up in crt_paths.
ST_FUNC i32 tli_add_crt(tli_State *s, const char *filename);
/// Read a shared library, looked up in library_paths.
ST_FUNC i32 tli_add_dll(tli_State *s, const char *filename, i32 flags);
/// Read a file from the linker's own directory, such as libtcc1.a.
ST_FUNC i32 tli_add_support(tli_State *s1, const char *filename, i32 quiet);
#ifdef TLI_BCHECK
/// Terminate the bounds checker's table with the null entry it looks for.
ST_FUNC void tli_add_bcheck(tli_State *s1);
#endif
#ifdef TLI_BACKTRACE
/// Build the rt_context a backtrace is read through, and export it.
ST_FUNC void tli_add_btstub(tli_State *s1);
#endif
/// Read the libraries the inputs asked for through #pragma comment(lib).
ST_FUNC void tli_add_pragma_libs(tli_State *s1);
/// Consume a command line into the state, without acting on the files in it.
PUB_FUNC i32 tli_parse_args(tli_State *s, i32 argc, char **argv);
/// Read the files tli_parse_args() recorded, in the order they were given.
PUB_FUNC i32 tli_add_parsed_files(tli_State *s1);
#ifdef _WIN32
/// Rewrite a path's backslashes as slashes, in place, and return it.
ST_FUNC char *normalize_slashes(char *path);
/// fopen() on a path that may be UTF-8 rather than in the ANSI code page.
PUB_FUNC FILE *tli_fopen(const char *f, const char *m);
PUB_FUNC i32 tli_fclose(FILE *f);
#else
# define tli_fopen fopen
# define tli_fclose fclose
#endif
/// Record a dependency on a shared library, returning the existing record if
/// the link already has one.  level is how far removed the library is.
ST_FUNC tli_DLLReference *tli_add_dllref(tli_State *s1, const char *dllname, i32 level);
/// Read a whole file into a NUL-terminated buffer the caller frees.
ST_FUNC char *tli_load_text(i32 fd);
/// Compare two paths the way the host does, after normalising separators.

/// ------------ tl_link.c ------------

#define TLI_OUTPUT_FORMAT_ELF    0  /// default output format: ELF
#define TLI_OUTPUT_FORMAT_BINARY 1  /// binary image output
/// A shared library is the one output type that is dynamic by construction;
/// an executable may or may not be, which is why the two are tested together
/// through this name.
#define TLI_OUTPUT_DYN           TLI_OUTPUT_DLL

#define ARMAG  "!<arch>\n"  /// the magic every ar archive starts with

/// One entry of a stabs debug table.  n_value is four bytes wide whatever the
/// target is, which is why a 64-bit image cannot relocate one.
typedef struct {
    u32 n_strx;  /// index into string table of name
    u8 n_type;  /// type of symbol
    u8 n_other;  /// misc info (usually empty)
    u16 n_desc;  /// description field
    u32 n_value;  /// value of symbol
} tli_StabSym;

/// Create the sections and symbol tables every link starts with.
ST_FUNC void tli_elf_new(tli_State *s);
/// Release them again, leaving the state safe to tear down.
ST_FUNC void tli_elf_delete(tli_State *s);
/// Create a section and put it on the output list, or on the private one
/// when SHF_PRIVATE is set.
ST_FUNC tli_Section *new_section(tli_State *s1, const char *name, i32 sh_type, i32 sh_flags);
/// Grow a section's buffer to hold at least new_size bytes, zeroing the rest.
ST_FUNC void section_realloc(tli_Section *sec, u64 new_size);
/// Reserve aligned space at the end of a section and return its offset.
ST_FUNC usize section_add(tli_Section *sec, addr_t size, i32 align);
/// Append unaligned bytes and return a pointer to them, valid until the
/// next call that can grow this section.
ST_FUNC void *section_ptr_add(tli_Section *sec, addr_t size);
/// The output section carrying this name, or NULL, through the name index.
ST_FUNC tli_Section *tli_find_section_by_name(tli_State *s1, const char *name);
/// As above, but creates an allocated progbits section when it is missing.
ST_FUNC tli_Section *find_section(tli_State *s1, const char *name);
/// Release a section's bytes but keep its header and name valid.
ST_FUNC void free_section(tli_Section *s);
/// Create a symbol table with the string and hash tables that serve it, and
/// wire the three to each other the way the gABI expects.
ST_FUNC tli_Section *new_symtab(tli_State *s1, const char *symtab_name, i32 sh_type, i32 sh_flags, const char *strtab_name, const char *hash_name, i32 hash_sh_flags);
/// Seed a symbol table with the empty string and the reserved symbol zero.
ST_FUNC void init_symtab(tli_Section *s);

/// Add a string to a string table and return its offset.
ST_FUNC i32 put_elf_str(tli_Section *s, const char *sym);
/// Append a symbol and return its index, without merging.
ST_FUNC i32 put_elf_sym(tli_Section *s, addr_t value, u64 size, i32 info, i32 other, i32 shndx, const char *name);
/// Define a symbol, merging it with any definition already there.
ST_FUNC i32 set_elf_sym(tli_Section *s, addr_t value, u64 size, i32 info, i32 other, i32 shndx, const char *name);
/// As above, for a section number that st_shndx cannot hold: shndx is
/// SHN_XINDEX and xshndx is the number standing behind it.
ST_FUNC i32 put_elf_sym_x(tli_Section *s, addr_t value, u64 size, i32 info, i32 other, i32 shndx, i32 xshndx, const char *name);
/// As set_elf_sym(), taking the same extended pair.
ST_FUNC i32 set_elf_sym_x(tli_Section *s, addr_t value, u64 size, i32 info, i32 other, i32 shndx, i32 xshndx, const char *name);
/// The number of the section a symbol is defined in, or zero.
ST_FUNC i32 tli_sym_section(tli_Section *symtab, i32 sym_index);
/// The extended half of a symbol's section number; zero unless SHN_XINDEX.
ST_FUNC i32 tli_sym_xindex(tli_Section *symtab, i32 sym_index);
/// Split a section's number into the pair a symbol table stores for it.
ST_FUNC void tli_shndx_of(tli_Section *sec, i32 *shndx, i32 *xshndx);
/// Record that a symbol is defined in the section with this number.
ST_FUNC void tli_sym_set_section(tli_Section *symtab, i32 sym_index, i32 shndx);
/// Find a symbol by name through the table's hash, or return zero.
ST_FUNC i32 find_elf_sym(tli_Section *s, const char *name);
/// Record a relocation against s, creating its relocation section if needed.
ST_FUNC void put_elf_reloc(tli_Section *symtab, tli_Section *s, u64 offset, i32 type, i32 symbol);
/// As put_elf_reloc(), for a target whose records carry their addend.
ST_FUNC void put_elf_reloca(tli_Section *symtab, tli_Section *s, u64 offset, i32 type, i32 symbol, addr_t addend);

/// Give every common symbol room in .bss, and define the linker's symbols.
ST_FUNC void resolve_common_syms(tli_State *s1);
/// Give every symbol in symtab its final value, now that layout has run.
ST_FUNC void relocate_syms(tli_State *s1, tli_Section *symtab, i32 do_resolve);
/// Apply every relocation table in the image.
ST_FUNC void relocate_sections(tli_State *s1);
/// Remember a symbol difference to work out once the image has been laid out.
ST_FUNC void tli_add_sym_diff(tli_State *s1, i32 sec, usize offset,
                              i32 sym_a, i32 sym_b, i32 len);

/// Append a pointer to symbol c to .init_array or .fini_array.
ST_FUNC void add_array(tli_State *s1, const char *sec, i32 c);
/// The extra state a symbol picks up while being linked, by symtab index.
ST_FUNC struct tli_SymAttr *get_sym_attr(tli_State *s1, i32 index, i32 alloc);
/// The address of a defined symbol, or -1.
ST_FUNC addr_t get_sym_addr(tli_State *s, const char *name, i32 err, i32 forc);
/// Define a global symbol at an offset in a section, and return its index.
ST_FUNC i32 set_global_sym(tli_State *s1, const char *name, tli_Section *sec, addr_t offs);

/// The C runtime objects that go before the user's, such as crt1.o.
ST_FUNC void tli_elf_add_crtbegin(tli_State *s1);
/// The ones that go after, closing what tli_elf_add_crtbegin() opened.
ST_FUNC void tli_elf_add_crtend(tli_State *s1);
/// Add the libraries an image implicitly depends on, once the command line
/// has been consumed.
ST_FUNC void tli_add_runtime(tli_State *s1);

/// Walk the elements of type <type> in section <sec>, starting at element
/// <startoff>, with <elem> as the cursor.  The bound is re-read each time
/// round, so the body may append to the section it is walking.
#define for_each_elem(sec, startoff, elem, type) \
    for (elem = (type *) sec->data + startoff; \
         elem < (type *) (sec->data + sec->data_offset); elem++)

/// ------------ tl_input.c ------------

/// read() until the whole count is in, since a short read is not an error.
ST_FUNC ssize_t full_read(i32 fd, void *buf, usize count);
/// Read size bytes from an offset in a file into a buffer the caller frees.
ST_FUNC void *load_data(i32 fd, u64 file_offset, u64 size);
/// What kind of file this is: AFF_BINTYPE_*, or 0 if nothing recognises it.
/// The ELF header it read is left in h for the caller that wants it.
ST_FUNC i32 tli_object_type(tli_State *s1, i32 fd, ElfW(Ehdr) *h);
/// Merge one object of the given AFF_BINTYPE_*, dispatching on the format.
ST_FUNC i32 tl_load_object(tli_State *s1, i32 fd, u64 file_offset, i32 obj_type,
                           u64 limit);
/// Read an ar archive: every member with alacarte clear, otherwise only the
/// members that define something the link still wants.
ST_FUNC i32 tli_load_archive(tli_State *s1, i32 fd, i32 alacarte);
/// Read what a shared library exports into dynsymtab_section.
ST_FUNC i32 tli_load_dll(tli_State *s1, i32 fd, const char *filename, i32 level);
/// Read a GNU ld script, which is what several system libraries really are.
ST_FUNC i32 tli_load_ldscript(tli_State *s1, i32 fd);

/// ------------ xxx-link.c ------------

/// Nonzero if the relocation patches an instruction, which is what decides
/// whether a symbol needs a PLT stub or only a GOT slot.
ST_FUNC i32 code_reloc (tli_State *s1, i32 reloc_type);
/// Which of the four answers below this relocation wants, or -1 if the
/// architecture does not recognise it.
ST_FUNC i32 gotplt_entry_type (tli_State *s1, i32 reloc_type);
/// The dynamic relocation a GOT slot for this type must carry, or 0 for an
/// ordinary address slot.  See tl_reloc_class.c.
ST_FUNC i32 got_tls_reloc (tli_State *s1, i32 reloc_type);
/// How many GOT words a thread-local slot for this type takes: two for a
/// global-dynamic descriptor, one for everything else.  See tl_reloc_class.c.
ST_FUNC i32 got_tls_words (tli_State *s1, i32 reloc_type);
/// Whether a relocation needs a GOT or PLT entry, and when.  NO_GOTPLT_ENTRY
/// is first so that an unrecognised relocation asks for nothing.
enum gotplt_entry {
    NO_GOTPLT_ENTRY,  /// never generate (eg. GLOB_DAT & JMP_SLOT relocs)
    BUILD_GOT_ONLY,  /// only build GOT (eg. TPOFF relocs)
    AUTO_GOTPLT_ENTRY,  /// generate if sym is UNDEF
    ALWAYS_GOTPLT_ENTRY  /// always generate (eg. PLTOFF relocs)
};
/// One byte per relocation type, in the table each backend declares: what the
/// type patches, and which entry class above it wants. RC_KNOWN separates a
/// type its architecture names from one it does not.
#define RC_KNOWN  0x10  /// Set on every type its architecture's table names.
#define RC_CODE   0x20  /// The field being patched is an instruction.
#define RC_DATA(g) (RC_KNOWN | (g))            /// Patches data.
#define RC_INSN(g) (RC_KNOWN | RC_CODE | (g))  /// Patches an instruction.

/// Walk every relocation and build the GOT and PLT entries they imply.
/// Declared beside the per-architecture questions it asks, but defined in
/// tl_link.c, since only the entries themselves are architecture-specific.
ST_FUNC void build_got_entries(tli_State *s1, i32 got_sym);

/// ------------------------------------------------------------------------
/// Reading and writing the target's byte order.
///
/// Every target this linker supports is little-endian, but the host need not
/// be, and an image is written as bytes rather than as host integers.  Going
/// through these keeps that true and keeps unaligned accesses out of it.

/// Read a little-endian halfword from wherever p points, aligned or not.
static inline u16 read16le(u8 *p) {
    return p[0] | (u16)p[1] << 8;
}
/// Write a little-endian halfword.
static inline void write16le(u8 *p, u16 x) {
    p[0] = x & 255;  p[1] = x >> 8 & 255;
}
/// Read a little-endian word.
static inline u32 read32le(u8 *p) {
  return read16le(p) | (u32)read16le(p + 2) << 16;
}
/// Write a little-endian word.
static inline void write32le(u8 *p, u32 x) {
    write16le(p, x);  write16le(p + 2, x >> 16);
}
/// Add to a little-endian word in place, which is what a REL target's
/// relocations do to the bytes they patch.
static inline void add32le(u8 *p, i32 x) {
    write32le(p, read32le(p) + x);
}
/// Read a little-endian doubleword.
static inline u64 read64le(u8 *p) {
  return read32le(p) | (u64)read32le(p + 4) << 32;
}
/// Write a little-endian doubleword.
static inline void write64le(u8 *p, u64 x) {
    write32le(p, x);  write32le(p + 4, x >> 32);
}
/// Add to a little-endian doubleword in place.
static inline void add64le(u8 *p, i64 x) {
    write64le(p, read64le(p) + x);
}

/// Whether a signed value fits a field of 'bits' bits.  Every backend asks
/// this of a displacement before encoding it, since a field too small for
/// the distance is a jump into nothing rather than a diagnosable error.
static inline i32 signed_fits(i64 v, i32 bits) {
    return v >= -((i64)1 << (bits - 1)) && v < ((i64)1 << (bits - 1));
}

/// ------------ tl_pe_out.c --------------

/// Read a .def file or an import library and record what it exports.
ST_FUNC i32 pe_load_file(struct tli_State *s1, i32 fd, const char *filename);
/// Write the finished image as a PE executable or DLL.
ST_FUNC i32 pe_output_file(tli_State * s1, const char *filename);
/// Record an import of name from DLL dllindex, giving it an indirection.
ST_FUNC i32 pe_putimport(tli_State *s1, i32 dllindex, const char *name, addr_t value);
/// Set the image's subsystem from the name given on the command line.
ST_FUNC i32 pe_setsubsy(tli_State *s1, const char *arg);
/// List what a DLL or import library exports, as NUL-separated names.
/// PE properties of a symbol, kept in the st_other of its ELF symbol, which
/// the gABI leaves free above the three visibility bits.
#define ST_PE_EXPORT 0x10  /// the image exports this name
/// The symbol came from an assembler .set, so a real definition may replace
/// it rather than collide with it.  Also lives in st_other.
#define ST_ASM_SET 0x04

/// ------------ tl_settings.c ------------

/// Turn a set of TL_FLAG_* on or off in one call.
ST_FUNC i32 tl_settings_flags(tli_State *s1, u32 flags, i32 on);
/// The flags currently in force.
ST_FUNC u32 tl_settings_get_flags(tli_State *s1);
/// Set one of the string settings, such as the soname or the interpreter.
ST_FUNC i32 tl_settings_set_str(tli_State *s1, i32 opt, const char *value);
/// Read one of them back; the state keeps ownership of what is returned.
ST_FUNC const char *tl_settings_get_str(tli_State *s1, i32 opt);
/// Set one of the numeric settings, such as the image base.
ST_FUNC i32 tl_settings_set_num(tli_State *s1, i32 opt, unsigned long long v);
/// Read one of them back.
ST_FUNC unsigned long long tl_settings_get_num(tli_State *s1, i32 opt);

/// ------------ tl_build.c ------------

/// Create a section for a caller that is generating an object rather than
/// reading one, and return the handle the calls below take.
ST_FUNC i32 tl_build_add_section(tli_State *s1, const char *name, i32 kind, i32 align);
/// Append bytes to such a section and return the offset they landed at.
ST_FUNC long tl_build_append(tli_State *s1, i32 sec, const void *data, usize len);
/// Append zeros, which for an SHT_NOBITS section only moves the offset.
ST_FUNC long tl_build_zero(tli_State *s1, i32 sec, usize len);
/// How much has been put in a section so far.
ST_FUNC long tl_build_size(tli_State *s1, i32 sec);
/// Define a symbol at an offset in one of those sections.
ST_FUNC i32 tl_build_define(tli_State *s1, const char *name, i32 sec, unsigned long long off, i32 flags);
/// Declare a symbol without defining it, so a relocation can name it.
ST_FUNC i32 tl_build_declare(tli_State *s1, const char *name);
/// Record a relocation against one of those sections.
ST_FUNC i32 tl_build_reloc(tli_State *s1, i32 sec, unsigned long long off, const char *sym, i32 kind, long long addend);
/// The same where what is named is another section rather than a symbol.
ST_FUNC i32 tl_build_section_reloc(tli_State *s1, i32 sec, unsigned long long off, i32 target, i32 kind, long long addend);

/// ------------ tl_macho_obj.c ------------

/// Whether these leading bytes are a Mach-O object, and of which kind.
ST_FUNC i32 macho_object_type(const void *hdr, i32 size);
/// Merge one Mach-O relocatable object into the link.
ST_FUNC i32 macho_load_object(tli_State *s1, i32 fd, u64 file_offset);
/// Write the link out as a Mach-O relocatable object.
ST_FUNC i32 macho_output_obj(tli_State *s1, const char *filename);

/// ------------ tl_coff_obj.c ------------

/// Whether these leading bytes are a COFF object for this target.
ST_FUNC i32 coff_object_type(tli_State *s1, const void *hdr, i32 size);
/// Whether these bytes are a short import record rather than an object.
ST_FUNC i32 coff_import_type(const void *hdr, i32 size);
/// Record what one short import record from an import library imports.
ST_FUNC i32 coff_load_import(tli_State *s1, i32 fd, u64 file_offset);
/// Merge one COFF relocatable object into the link.
ST_FUNC i32 coff_load_object(tli_State *s1, i32 fd, u64 file_offset);
/// The name a COFF weak external stands for, or NULL when it is not one.
ST_FUNC const char *coff_alias_of(tli_State *s1, const char *name);
/// Write the link out as a COFF relocatable object.
ST_FUNC i32 coff_output_obj(tli_State *s1, const char *filename);

/// ------------ tl_macho_out.c -----------------

/// Write the finished image as a Mach-O executable or dylib.
ST_FUNC i32 macho_output_file(tli_State * s1, const char *filename);
/// Read what a dylib exports into dynsymtab_section.
ST_FUNC i32 macho_load_dll(tli_State *s1, i32 fd, const char *filename, i32 lev);
/// The same for a .tbd stub, which is what recent SDKs ship instead.
ST_FUNC i32 macho_load_tbd(tli_State *s1, i32 fd, const char *filename, i32 lev);
#if defined TLI_HOST_RUN && defined __APPLE__
/// Add the library paths of the SDK xcrun reports, so a native link works
/// on a machine whose libraries live inside Xcode.
ST_FUNC void tli_add_macos_sdkpath(tli_State* s);
/// The install name a .tbd stub records, which is its soname.
ST_FUNC char* macho_tbd_soname(i32 fd);
#endif

/// ------------ tl_run.c -----------------

#ifdef TLI_HOST_RUN
#ifdef TLI_STATIC
/// A statically linked linker has no dlopen(), so tl_run.c supplies stubs and
/// resolving against the host's libraries simply finds nothing.
#define RTLD_LAZY       0x001
#define RTLD_NOW        0x002
#define RTLD_GLOBAL     0x100
#define RTLD_DEFAULT    NULL
ST_FUNC void *dlopen(const char *filename, i32 flag);
ST_FUNC void dlclose(void *p);
ST_FUNC const char *dlerror(void);
ST_FUNC void *dlsym(void *handle, const char *symbol);
#endif
/// Release the pages an in-memory link was mapped into.
ST_FUNC void tli_run_free(tli_State *s1);
#endif

/// ------------ tl_sort.c ------------

/// A comparison function is handed the caller's context, so a sort can reach
/// state that is not global.  Same sign convention as strcmp.
typedef i32 tl_SortCmp(const void *a, const void *b, void *arg);
/// Sort in place with a comparison function that takes a context.  Written
/// out rather than taken from the C library because qsort_r's argument order
/// is not the same on every platform.
ST_FUNC void tl_sort(void *base, usize nmemb, usize size,
                     tl_SortCmp *cmp, void *arg);

/// ------------ tl_ehframe.c ------------

/// How many bytes of .eh_frame_hdr this image needs, or 0 for none.  Asked
/// before layout, since the section has to have a size to be given an address.
ST_FUNC usize tli_eh_frame_hdr_size(tli_State *s1);
/// Fill it in, once every address it refers to is settled.
ST_FUNC void tli_eh_frame_hdr_build(tli_State *s1, tli_Section *hdr);

/// ------------ tl_support.c ------------

/// One more than the base-two logarithm of i, or zero when i is not a power
/// of two.  Alignments are stored that way in several object formats.
ST_FUNC i32 exact_log2p1(i32 i);

/// ------------ tl_elf_out.c ------------


/// ------------------------------------------------------------------------
/// Debug sections and the readers over them.
///
/// The names below are fields of the state, spelled unqualified the way the
/// predefined sections are, so that a file with s1 in scope can say
/// dwarf_line_section and mean s1->dwarf_line_section.

#define stab_section            s1->stab_section
#define stabstr_section         stab_section->link
#define dwarf_info_section      s1->dwarf_info_section
#define dwarf_abbrev_section    s1->dwarf_abbrev_section
#define dwarf_line_section      s1->dwarf_line_section
#define dwarf_aranges_section   s1->dwarf_aranges_section
#define dwarf_str_section       s1->dwarf_str_section
#define dwarf_line_str_section  s1->dwarf_line_str_section


/// default dwarf version for "-g". use 0 to emit stab debug infos
#ifndef CONFIG_DWARF_VERSION
# define CONFIG_DWARF_VERSION 0
#endif

/// ------------------------------------------------------------------------
/// The lock around tli_state.  Three platforms, one interface: WAIT_SEM and
/// POST_SEM bracket the window in which the global points at a particular
/// state.  Initialisation is lazy so that a TLISem can be a plain static.

#if TLI_SEMLOCK
#if defined _WIN32
/// A critical section, plus the state machine that builds it exactly once.
typedef struct { volatile LONG init; CRITICAL_SECTION cs; } TLISem;
/// Take the lock, initialising it on the way through if nobody has yet.
static inline void wait_sem(TLISem *p) {
    // init goes 0 -> 1 while one thread builds the section, then 1 -> 2 when
    // it is usable; anyone else spins until it reads 2.
    if (InterlockedCompareExchange(&p->init, 1, 0) == 0) {
        InitializeCriticalSection(&p->cs);
        InterlockedExchange(&p->init, 2);
    } else {
        while (InterlockedCompareExchange(&p->init, 2, 2) != 2)
            Sleep(0);
    }
    EnterCriticalSection(&p->cs);
}
/// Release the lock.
static inline void post_sem(TLISem *p) {
    LeaveCriticalSection(&p->cs);
}
#elif defined __APPLE__
#include <dispatch/dispatch.h>
/// macOS deprecated its unnamed POSIX semaphores, so use a dispatch one.
typedef struct { i32 init; dispatch_semaphore_t sem; } TLISem;
/// Take the lock, creating the semaphore on first use.
static inline void wait_sem(TLISem *p) {
    if (!p->init)
        p->sem = dispatch_semaphore_create(1), p->init = 1;

    dispatch_semaphore_wait(p->sem, DISPATCH_TIME_FOREVER);
}
/// Release the lock.
static inline void post_sem(TLISem *p) {
    dispatch_semaphore_signal(p->sem);
}
#else
#include <semaphore.h>
/// A POSIX semaphore used as a mutex, created on first use.
typedef struct { i32 init; sem_t sem; } TLISem;
/// Take the lock, creating the semaphore on first use.
static inline void wait_sem(TLISem *p) {
    if (!p->init)
        sem_init(&p->sem, 0, 1), p->init = 1;

    // a signal may interrupt the wait, which is not an answer
    while (sem_wait(&p->sem) < 0 && errno == EINTR);
}
/// Release the lock.
static inline void post_sem(TLISem *p) {
    sem_post(&p->sem);
}
#endif
/// The one interface the three implementations above are reached through, so
/// that a build without TLI_SEMLOCK simply compiles the lock away.
#define TLI_SEM(s) static TLISem s
#define WAIT_SEM wait_sem
#define POST_SEM post_sem
#else
#define TLI_SEM(s)
#define WAIT_SEM(p)
#define POST_SEM(p)
#endif

/// ------------------------------------------------------------------------
/// Reaching the state.
///
/// Most of the core is handed the tli_State it is working on as s1, and the
/// names below are simply fields of it.  A few places have no such parameter
/// and must go through the current state instead; those define USING_GLOBALS
/// before including this header, which switches every one of these aliases to
/// read tli_state, and makes the diagnostic functions assume the state has
/// already been entered.  The switch is at the very bottom of the file, past
/// the include guard, so that including the header again is enough to flip it.

/// Redefined here because the declarations above are done with: past this
/// point ST_DATA introduces a definition rather than an extern reference.
#undef ST_DATA
#if ONE_SOURCE
#define ST_DATA static
#else
#define ST_DATA
#endif

/// The predefined sections, spelled the way the code that uses them reads
/// best.  Each expands through TLI_STATE_VAR, so the same name reaches s1 or
/// the current global state depending on how the header was included.
#define text_section        TLI_STATE_VAR(text_section)
#define data_section        TLI_STATE_VAR(data_section)
#define rodata_section      TLI_STATE_VAR(rodata_section)
#define bss_section         TLI_STATE_VAR(bss_section)
#define common_section      TLI_STATE_VAR(common_section)
#define bounds_section      TLI_STATE_VAR(bounds_section)
#define lbounds_section     TLI_STATE_VAR(lbounds_section)
#define symtab_section      TLI_STATE_VAR(symtab_section)

/// Reporting a diagnostic needs the state, because that is what carries the
/// caller's error function; TLI_SET_STATE installs it first.
#define tli_error_noabort   TLI_SET_STATE(_tcc_error_noabort)
#define tli_error           TLI_SET_STATE(_tcc_error)
#define tli_warning         TLI_SET_STATE(_tcc_warning)

/// Take the lock and make s1 the state the global names, so that code with
/// no tli_State in scope can still report through it.
PUB_FUNC void tli_enter_state(tli_State *s1);
/// Give the lock back and forget the state again.
PUB_FUNC void tli_exit_state(tli_State *s1);

/// A warning that only appears when its switch is on.  The switch is named by
/// its offset from warn_none, which is what lets one printer handle all of
/// them without a table.
#define tli_warning_c(sw) TLI_SET_STATE((\
    tli_state->warn_num = offsetof(tli_State, sw) \
    - offsetof(tli_State, warn_none), _tcc_warning))

#endif  // _TCC_H

/// Everything past this point is outside the include guard and is redefined on
/// every inclusion, which is the whole mechanism: a file that wants the global
/// form includes the header again with USING_GLOBALS defined.

#undef TLI_STATE_VAR
#undef TLI_SET_STATE

#ifdef USING_GLOBALS
# define TLI_STATE_VAR(sym) tli_state->sym
# define TLI_SET_STATE(fn) fn
# undef USING_GLOBALS
# undef _tcc_error
#else
# define TLI_STATE_VAR(sym) s1->sym
# define TLI_SET_STATE(fn) (tli_enter_state(s1),fn)
/// Without a state in scope the aborting form cannot be used, because there
/// would be nothing to report through; the name becomes a link error.
# define _tcc_error use_tcc_error_noabort

/// Runs shorter than this are built by insertion sort.  Merging costs a
/// function call and two bounds tests per element, which only pays once a run
/// is long enough for the halving to save real comparisons.
#define SORT_RUN 16

/// Sort one short run in place.  Elements are swapped a byte at a time because
/// their size is only known at run time.
static void sort_insertion(u8 *base, usize n, usize size,
                           tl_SortCmp *cmp, void *arg)
{
    usize i;

    for (i = 1; i < n; i++) {
        u8 *p = base + i * size;

        // walk the element back over every neighbour that compares greater
        while (p > base && cmp(p - size, p, arg) > 0) {
            usize k = size;
            u8 *a = p - size, *b = p;

            do {
                u8 t = *a;
                *a++ = *b;
                *b++ = t;
            } while (--k);
            p -= size;
        }
    }
}

/// Merge two adjacent sorted runs into out.  Taking from the left run when the
/// two compare equal is what makes the whole sort stable.
static void sort_merge(u8 *out, u8 *l, usize nl, u8 *r, usize nr,
                       usize size, tl_SortCmp *cmp, void *arg)
{
    while (nl && nr) {
        if (cmp(r, l, arg) < 0) {
            memcpy(out, r, size);
            r += size;
            nr--;
        } else {
            memcpy(out, l, size);
            l += size;
            nl--;
        }
        out += size;
    }

    // whichever run is left over is already in order
    if (nl)
        memcpy(out, l, nl * size);
    else if (nr)
        memcpy(out, r, nr * size);
}

/// Sort nmemb elements of the given size in place.  cmp is handed arg on every
/// call, which is the whole reason this is not qsort().
ST_FUNC void tl_sort(void *base, usize nmemb, usize size,
                     tl_SortCmp *cmp, void *arg)
{
    u8 *a = base, *scratch, *src, *dst;
    usize run, i;

    if (nmemb < 2)
        return;

    // start from runs of SORT_RUN, the last of which may be short
    for (i = 0; i < nmemb; i += SORT_RUN)
        sort_insertion(a + i * size,
                       nmemb - i < SORT_RUN ? nmemb - i : SORT_RUN,
                       size, cmp, arg);

    if (nmemb <= SORT_RUN)
        return;

    // Merge alternately into the scratch buffer and back, so a pass costs one
    // copy of each element rather than two.
    scratch = tli_malloc(nmemb * size);
    src = a;
    dst = scratch;
    for (run = SORT_RUN; run < nmemb; run *= 2) {

        // pair up the runs of the current length, left to right
        for (i = 0; i < nmemb; i += 2 * run) {
            usize nl = nmemb - i < run ? nmemb - i : run;
            usize nr = nmemb - i - nl;

            if (nr > run)
                nr = run;

            if (nr)
                sort_merge(dst + i * size, src + i * size, nl,
                           src + (i + run) * size, nr, size, cmp, arg);
            else
                memcpy(dst + i * size, src + i * size, nl * size);
        }
        dst = src;
        src = src == a ? scratch : a;
    }

    // an odd number of passes leaves the result in the scratch buffer
    if (src != a)
        memcpy(a, src, nmemb * size);

    tli_free(scratch);
}


/// ---- src/core/tl_support.c ----

/// tinylinker - odds and ends
///
/// Two things that do not belong to any one stage of the link: the growable
/// byte buffer the rest of the core builds strings and command lines with,
/// and the .eh_frame_hdr writer, which is the only piece of DWARF the linker
/// itself has to produce rather than copy.


/// ---------------------------------------------------------------------------
/// tli_CString: a byte buffer that grows.
///
/// It is not a string in the C sense.  Nothing here appends a terminator, so a
/// caller that wants one asks for it; the printf variants are the exception,
/// since vsnprintf writes a terminator whether it is wanted or not (it sits
/// one past 'size' and is overwritten by whatever is appended next).

/// Round the buffer up to at least 'need' bytes.  Doubling keeps a run of
/// single-byte appends amortised constant, and the floor stops a path that
/// builds one character at a time from starting with a one-byte allocation.
static void cstr_realloc(tli_CString *cstr, i32 need)
{
    i32 size = cstr->size_allocated;

    if (size < 8)
        size = 8;

    while (size < need)
        size *= 2;

    cstr->data = tli_realloc(cstr->data, size);
    cstr->size_allocated = size;
}

/// Append one byte.  Inline because a whole class of callers builds its output
/// a character at a time.
ST_INLN void cstr_ccat(tli_CString *cstr, i32 ch)
{
    if (cstr->size >= cstr->size_allocated)
        cstr_realloc(cstr, cstr->size + 1);

    cstr->data[cstr->size++] = ch;
}

/// Append 'len' bytes of 'str'.  A negative or zero 'len' means "measure it":
/// 0 takes the terminator too, -1 stops just short of it.
ST_FUNC void cstr_cat(tli_CString *cstr, const char *str, i32 len)
{
    if (len <= 0)
        len = (i32)strlen(str) + 1 + len;

    if (len <= 0)
        return;

    if (cstr->size + len > cstr->size_allocated)
        cstr_realloc(cstr, cstr->size + len);

    memmove(cstr->data + cstr->size, str, len);
    cstr->size += len;
}

/// Start an empty buffer.  A tli_CString owns no memory until something is put
/// into it, so this is just three stores.
ST_FUNC void cstr_new(tli_CString *cstr)
{
    cstr->size = 0;
    cstr->size_allocated = 0;
    cstr->data = NULL;
}

/// Release the buffer and leave the tli_CString usable again, which is what lets
/// one sit in a loop and be filled repeatedly.
ST_FUNC void cstr_free(tli_CString *cstr)
{
    tli_free(cstr->data);
    cstr_new(cstr);
}

/// Append formatted text.  vsnprintf reports the length it wanted, so a buffer
/// that was too small is resized to exactly that and filled on the second
/// attempt; the doubling is only there for the pre-C99 return of -1.
ST_FUNC i32 cstr_vprintf(tli_CString *cstr, const char *fmt, va_list ap)
{
    i32 room = 80, len;

    for (;;) {
        va_list v;

        if (cstr->size + room > cstr->size_allocated)
            cstr_realloc(cstr, cstr->size + room);

        room = cstr->size_allocated - cstr->size;
        va_copy(v, ap);
        len = vsnprintf((char *)cstr->data + cstr->size, room, fmt, v);
        va_end(v);

        // it fitted, terminator and all
        if (len >= 0 && len < room)
            break;

        room = len >= 0 ? len + 1 : room * 2;
    }
    cstr->size += len;
    return len;
}

/// The variadic form of cstr_vprintf(), which is how nearly every caller
/// reaches it.
ST_FUNC i32 cstr_printf(tli_CString *cstr, const char *fmt, ...)
{
    va_list ap;
    i32 len;

    va_start(ap, fmt);
    len = cstr_vprintf(cstr, fmt, ap);
    va_end(ap);
    return len;
}

/// ---------------------------------------------------------------------------

/// Number of significant bits in 'i', which for a power of two is its base-two
/// logarithm plus one.  Mach-O stores section alignments that way.
ST_FUNC i32 exact_log2p1(i32 i)
{
    u32 v = (u32)i;
    i32 n = 0;

    while (v) {
        v >>= 1;
        n++;
    }
    return n;
}


/// ---- src/core/tl_ehframe.c ----

/// tinylinker - the .eh_frame_hdr an unwinder starts from
///
/// Anything that walks past its own frame -- a C++ throw, a backtrace, a
/// cancellation -- has to find the DWARF frame description for a return
/// address it holds.  It finds it by asking the loader for the program
/// headers, looking for PT_GNU_EH_FRAME, and reading the .eh_frame_hdr that
/// segment covers: a four byte preamble, a pointer to .eh_frame, and a table
/// of every function's start address sorted so it can be searched.
///
/// The linker is the only thing that can build that table, because it is the
/// only thing that sees every frame description at once and knows where each
/// one ended up.  Without it .eh_frame is bytes nothing can reach, which is
/// why it used to be dropped rather than carried through.
///
/// Only what is needed to find and sort the FDEs is parsed here.  The frame
/// instructions inside them mean nothing to a linker and are stepped over by
/// the length that precedes them.

/// Pointer encodings, from the LSB's definition of .eh_frame.  The low four
/// bits say how wide the value is and how it is signed, the next four what it
/// is measured from.
#define DW_EH_PE_absptr  0x00
#define DW_EH_PE_uleb128 0x01
#define DW_EH_PE_udata2  0x02
#define DW_EH_PE_udata4  0x03
#define DW_EH_PE_udata8  0x04
#define DW_EH_PE_sleb128 0x09
#define DW_EH_PE_sdata2  0x0a
#define DW_EH_PE_sdata4  0x0b
#define DW_EH_PE_sdata8  0x0c
#define DW_EH_PE_pcrel   0x10
#define DW_EH_PE_datarel 0x30
#define DW_EH_PE_omit    0xff

/// How .eh_frame_hdr itself is written.  These are the encodings every other
/// linker uses, and an unwinder is entitled to expect the table one.
#define EH_HDR_PTR_ENC   (DW_EH_PE_pcrel | DW_EH_PE_sdata4)
#define EH_HDR_COUNT_ENC DW_EH_PE_udata4
#define EH_HDR_TABLE_ENC (DW_EH_PE_datarel | DW_EH_PE_sdata4)

/// Bytes before the search table: the four encoding bytes, the pointer to
/// .eh_frame, and the entry count.
#define EH_HDR_PREAMBLE 12

/// One entry of that table: where a function starts and where its description
/// is, both as distances from the header.
#define EH_HDR_ENTRY 8

/// A bounded walk over a section's bytes.  Every read checks 'end' and sets
/// 'bad' rather than returning an error, so a parse is written as a run of
/// reads with one test after them: the input is an object file that may be
/// damaged, and a walk that ran off the end would otherwise read the heap.
struct eh_cursor {
    const u8 *p;    /// Next byte to read.
    const u8 *end;  /// One past the last readable byte.
    i32 bad;        /// Set once a read has been refused.
};

/// Take 'n' bytes, or fail and leave the cursor where it was.
static const u8 *eh_take(struct eh_cursor *c, usize n)
{
    const u8 *at = c->p;

    if (c->bad || (usize)(c->end - c->p) < n) {
        c->bad = 1;
        return NULL;
    }

    c->p += n;
    return at;
}

static u8 eh_u8(struct eh_cursor *c)
{
    const u8 *at = eh_take(c, 1);
    return at ? *at : 0;
}

static u32 eh_u32(struct eh_cursor *c)
{
    const u8 *at = eh_take(c, 4);
    return at ? read32le((u8 *)at) : 0;
}

static u64 eh_u64(struct eh_cursor *c)
{
    const u8 *at = eh_take(c, 8);
    return at ? read64le((u8 *)at) : 0;
}

/// An unsigned LEB128, which is how DWARF spells a number whose size it does
/// not want to fix.  Seven bits per byte, low first, top bit meaning more.
static u64 eh_uleb(struct eh_cursor *c)
{
    u64 v = 0;
    i32 shift = 0;

    for (;;) {
        u8 b = eh_u8(c);

        if (c->bad)
            return 0;

        if (shift < 64)
            v |= (u64)(b & 0x7f) << shift;

        shift += 7;
        if (!(b & 0x80))
            return v;

        // A number longer than this is not one an object should contain, and
        // continuing would spin to the end of the section.
        if (shift > 70) {
            c->bad = 1;
            return 0;
        }
    }
}

/// The signed form, sign extended from wherever it stopped.
static i64 eh_sleb(struct eh_cursor *c)
{
    u64 v = 0;
    i32 shift = 0;

    for (;;) {
        u8 b = eh_u8(c);

        if (c->bad)
            return 0;

        if (shift < 64)
            v |= (u64)(b & 0x7f) << shift;

        shift += 7;
        if (!(b & 0x80)) {
            if (shift < 64 && (b & 0x40))
                v |= ~(u64)0 << shift;

            return (i64)v;
        }
        if (shift > 70) {
            c->bad = 1;
            return 0;
        }
    }
}

/// Read one encoded pointer and return the address it denotes.
///
/// 'at' is the address the field itself will live at, which is what a
/// pc-relative encoding is measured from.  Only the encodings a compiler
/// actually emits into .eh_frame are handled; anything else fails the walk
/// rather than being guessed at, since a wrong address here is a table entry
/// that sends an unwinder to the wrong function.
static addr_t eh_encoded(struct eh_cursor *c, u8 enc, addr_t at, i32 *ok)
{
    addr_t base = 0;
    i64 v = 0;

    if (enc == DW_EH_PE_omit)
        return 0;

    switch (enc & 0x70) {
    case 0x00:
        break;
    case DW_EH_PE_pcrel:
        base = at;
        break;
    default:
        // datarel wants a GOT or a section base this walk is not given, and
        // the rest are for encodings no compiler emits here.
        *ok = 0;
        return 0;
    }

    switch (enc & 0x0f) {
    case DW_EH_PE_absptr:
        v = PTR_SIZE == 8 ? (i64)eh_u64(c) : (i64)(i32)eh_u32(c);
        break;
    case DW_EH_PE_udata4:
        v = (i64)(u32)eh_u32(c);
        break;
    case DW_EH_PE_sdata4:
        v = (i64)(i32)eh_u32(c);
        break;
    case DW_EH_PE_udata8:
    case DW_EH_PE_sdata8:
        v = (i64)eh_u64(c);
        break;
    case DW_EH_PE_uleb128:
        v = (i64)eh_uleb(c);
        break;
    case DW_EH_PE_sleb128:
        v = eh_sleb(c);
        break;
    default:
        *ok = 0;
        return 0;
    }

    if (c->bad)
        *ok = 0;

    return (addr_t)((i64)base + v);
}

/// Read a CIE far enough to learn how the FDEs that name it encode their
/// addresses, which is the only thing about it a linker has to understand.
///
/// The answer is in the augmentation: a string of letters, each introducing a
/// field in the data that follows.  'R' is the one that matters.  A CIE whose
/// augmentation does not begin with 'z' carries no such data at all, and its
/// FDEs use the plain pointer encoding.
static u8 eh_cie_fde_enc(const u8 *data, usize size, u64 cie_off, i32 *ok)
{
    struct eh_cursor c;
    const char *aug;
    u64 len, end;
    u8 version, enc = DW_EH_PE_absptr;
    i32 i;

    c.p = data + cie_off;
    c.end = data + size;
    c.bad = 0;

    if (cie_off >= size) {
        *ok = 0;
        return 0;
    }

    len = eh_u32(&c);
    if (len == 0xffffffffu) {
        // 64-bit DWARF.  No compiler emits it into .eh_frame, and guessing
        // would be worse than saying so.
        *ok = 0;
        return 0;
    }

    end = (u64)(c.p - data) + len;
    if (c.bad || end > size) {
        *ok = 0;
        return 0;
    }

    eh_u32(&c);                 // CIE_id, zero for a CIE
    version = eh_u8(&c);
    aug = (const char *)c.p;

    // The augmentation string, stepped over by finding its terminator inside
    // what is left rather than trusting it to have one.
    for (;;) {
        u8 ch = eh_u8(&c);

        if (c.bad) {
            *ok = 0;
            return 0;
        }
        if (!ch)
            break;
    }

    eh_uleb(&c);                // code alignment factor
    eh_sleb(&c);                // data alignment factor

    // The return address register widened from a byte to a LEB in version 3.
    if (version == 1)
        eh_u8(&c);
    else
        eh_uleb(&c);

    if (aug[0] != 'z') {
        *ok = !c.bad;
        return enc;
    }

    eh_uleb(&c);                // length of the augmentation data

    for (i = 1; aug[i]; i++) {
        switch (aug[i]) {
        case 'R':
            enc = eh_u8(&c);
            break;
        case 'L':
            eh_u8(&c);          // the LSDA encoding
            break;
        case 'P': {
            u8 penc = eh_u8(&c);
            i32 pok = 1;

            // The personality routine's address, which is stepped over for
            // its width; where it points is of no interest here.
            eh_encoded(&c, penc, 0, &pok);
            break;
        }
        case 'S':
        case 'B':
        case 'G':
            break;              // flags, with no data of their own
        default:
            // An augmentation this does not know may carry data of a width it
            // cannot guess, so everything after it would be misread.
            *ok = 0;
            return 0;
        }
    }

    if (c.bad)
        *ok = 0;

    return enc;
}

/// Walk .eh_frame, calling nothing and counting the FDEs, or filling in
/// 'out' when it is given.
///
/// One walk serves both because the count has to be known before the image is
/// laid out -- the header's size depends on it -- and the addresses only after
/// everything has been relocated.  The records themselves do not move in
/// between, so walking twice is walking the same thing.
static i32 eh_frame_walk(tli_Section *ehf, struct tli_EhEntry *out, i32 max,
                         i32 *count)
{
    const u8 *data = ehf->data;
    usize size = ehf->data_offset;
    addr_t base = ehf->sh_addr;
    u64 off = 0;
    u8 cached_enc = 0;
    u64 cached_cie = (u64)-1;
    i32 n = 0;

    while (off + 4 <= size) {
        struct eh_cursor c;
        u64 len, rec_end, cie_ptr, cie_off;
        u64 field_off;
        addr_t start;
        i32 ok = 1;

        c.p = data + off;
        c.end = data + size;
        c.bad = 0;

        len = eh_u32(&c);

        // Length zero is the terminator some producers put at the end.
        if (c.bad || len == 0)
            break;

        if (len == 0xffffffffu)
            return -1;

        rec_end = (u64)(c.p - data) + len;
        if (rec_end > size)
            return -1;

        cie_ptr = eh_u32(&c);
        if (c.bad)
            return -1;

        // Zero here means the record describes no function: it is a CIE, and
        // the FDEs that follow point back at it.
        if (cie_ptr == 0) {
            off = rec_end;
            continue;
        }

        // The pointer is a distance backwards from the field holding it.
        field_off = (u64)(c.p - data) - 4;
        if (cie_ptr > field_off)
            return -1;

        cie_off = field_off - cie_ptr;

        // Consecutive FDEs almost always share a CIE, so the last answer is
        // kept rather than parsing the same one for every function.
        if (cie_off != cached_cie) {
            cached_enc = eh_cie_fde_enc(data, size, cie_off, &ok);
            if (!ok)
                return -1;

            cached_cie = cie_off;
        }

        start = eh_encoded(&c, cached_enc,
                           base + (addr_t)(c.p - data), &ok);
        if (!ok)
            return -1;

        if (out) {
            if (n >= max)
                return -1;

            out[n].start = start;
            out[n].fde = base + (addr_t)off;
        }

        n++;
        off = rec_end;
    }

    *count = n;
    return 0;
}

/// Order the table by the address each entry describes, which is what makes
/// it searchable and is the whole reason it exists.
static i32 eh_entry_cmp(const void *a, const void *b, void *opaque)
{
    const struct tli_EhEntry *x = a, *y = b;

    (void)opaque;
    return x->start < y->start ? -1 : x->start > y->start ? 1 : 0;
}

/// How many bytes of .eh_frame_hdr this image needs, or 0 for none.
///
/// Called before layout, when the FDEs still hold whatever their relocations
/// will later replace.  Only the count is wanted here, and that does not
/// depend on any address.
ST_FUNC usize tli_eh_frame_hdr_size(tli_State *s1)
{
    tli_Section *ehf = tli_find_section_by_name(s1, ".eh_frame");
    i32 n = 0;

    if (!ehf || !ehf->data_offset)
        return 0;

    if (eh_frame_walk(ehf, NULL, 0, &n) < 0) {
        // Nothing here is worth failing a link over: a program that never
        // unwinds does not care, and one that does gets a diagnostic rather
        // than a table that would send it somewhere wrong.
        tli_warning("%s: cannot be parsed, so no .eh_frame_hdr is written and "
                    "unwinding will not work", ".eh_frame");
        s1->eh_frame_unreadable = 1;
        return 0;
    }

    return n ? EH_HDR_PREAMBLE + (usize)n * EH_HDR_ENTRY : 0;
}

/// Fill in .eh_frame_hdr, once every address in the image is settled.
ST_FUNC void tli_eh_frame_hdr_build(tli_State *s1, tli_Section *hdr)
{
    tli_Section *ehf = tli_find_section_by_name(s1, ".eh_frame");
    struct tli_EhEntry *ent;
    u8 *p;
    i32 n = 0, max, i;

    if (!hdr || !hdr->data_offset || !ehf)
        return;

    max = (i32)((hdr->data_offset - EH_HDR_PREAMBLE) / EH_HDR_ENTRY);
    ent = tli_malloc(sizeof *ent * (max ? max : 1));

    if (eh_frame_walk(ehf, ent, max, &n) < 0 || n != max) {
        // The two walks disagreed, which can only mean the section changed
        // under us.  Writing a partial table would be worse than writing none.
        tli_warning("%s: changed between sizing and writing, so no search "
                    "table is written", ".eh_frame");
        tli_free(ent);
        return;
    }

    tl_sort(ent, n, sizeof *ent, eh_entry_cmp, NULL);

    p = hdr->data;
    p[0] = 1;                   // version
    p[1] = EH_HDR_PTR_ENC;
    p[2] = EH_HDR_COUNT_ENC;
    p[3] = EH_HDR_TABLE_ENC;

    // The pointer to .eh_frame is measured from the field it sits in; every
    // table entry from the start of the header.
    write32le(p + 4, (u32)(i32)((i64)ehf->sh_addr - (i64)(hdr->sh_addr + 4)));
    write32le(p + 8, (u32)n);

    for (i = 0; i < n; i++) {
        u8 *e = p + EH_HDR_PREAMBLE + (usize)i * EH_HDR_ENTRY;

        write32le(e, (u32)(i32)((i64)ent[i].start - (i64)hdr->sh_addr));
        write32le(e + 4, (u32)(i32)((i64)ent[i].fde - (i64)hdr->sh_addr));
    }

    tli_free(ent);
}


/// ---- src/core/tl_link.c ----

/// tinylinker - sections, symbols and relocations
///
/// This is where the linker keeps everything it has read and everything it is
/// about to write.  Every other file in the core reaches through the tables
/// defined here: the input readers append to them, the output writers walk
/// them, and the per-architecture relocation code is handed pointers into
/// them.
///
/// Four things carry the whole model.
///
/// A tli_Section is a growable byte buffer with an ELF section header stapled to
/// the front of it.  Sections live in one of two lists on the state.  Those
/// destined for the output are in s1->sections, indexed by the section number
/// their header will carry; those that are pure bookkeeping, such as the
/// linker's own symbol hash tables or the symbols the input libraries export,
/// are in s1->priv_sections and never reach a file.  A flag of our own,
/// SHF_PRIVATE, decides which list a section joins.
///
/// A symbol table is a tli_Section of ElfW(Sym) whose ->link is its string table
/// and whose ->hash is a bucket chain over the non-local symbols in it.  Two
/// exist for the whole link: s1->symtab, the symbols of the image being
/// built, and s1->dynsymtab_section, the union of what the shared libraries
/// on the command line export.  A third, s1->dynsym, is created only when the
/// output needs a dynamic symbol table of its own.
///
/// A relocation table is a tli_Section of ElfW_Rel hanging off the section it
/// patches as ->reloc, with ->link naming the symbol table its indices refer
/// to and ->sh_info the number of the section it applies to.
///
/// sym_attrs is a flat array parallel to s1->symtab holding what a symbol
/// acquires while being linked and has nowhere to live in ElfW(Sym): its GOT
/// slot, its PLT slot, its index in the output's dynamic symbol table.
///
/// The ELF structures themselves are in tl_elf.h.  Where the gABI or a psABI
/// dictates a layout or a rule, the rule is cited at the point it is
/// implemented rather than here.


/// Define to have relocate_syms() report each symbol it resolves through the
/// host's dynamic loader.  A development aid; off in every build.
#undef DEBUG_RELOC

/// ---------------------------------------------------------------- flags
///
/// Two section flags of our own.  The gABI reserves the top four bits of
/// sh_flags (SHF_MASKPROC) for the processor supplement, and no supplement
/// this linker targets spends either of these two, so they are safe to
/// borrow.  Neither ever reaches a file: a section carrying SHF_PRIVATE is
/// never written, and SHF_DYNSYM only ever appears on a private one.

#define SHF_PRIVATE 0x80000000  /// bookkeeping only, not an output section
#define SHF_DYNSYM  0x40000000  /// holds what the input libraries export

/// Read-only data that still has relocations against it.  On ELF the section
/// is created merely SHF_ALLOC and is made writable again during layout, so
/// that the loader can apply those relocations and then have the write
/// permission taken away again through PT_GNU_RELRO.  PE has no equivalent
/// stage and spells the section's name differently.
#define shf_RELRO SHF_ALLOC  /// The flags such a section is created with.
/// Its name, which PE spells its own way.
#define rdata (TLI_STATE_VAR(fmt) == TLI_FMT_PE ? ".rdata" : ".data.ro")

/// One version a shared library defines or requires, as read out of its
/// .gnu.version_d / .gnu.version_r by tl_input.c.  prev_same_lib chains the
/// versions belonging to one library together, newest first, so that
/// version_add() below can emit one Verneed record per library with its
/// Vernaux entries hanging off it.
struct tli_SymVersion {
    char *lib;  /// The library it belongs to, as DT_NEEDED names it.
    char *version;  /// The version's own name, such as GLIBC_2.17.
    i32 out_index;  /// index in the output's version table, 0 if unused
    i32 prev_same_lib;  /// index of the previous version of the same lib
};

/// The version bookkeeping lives on the state but reads better unqualified,
/// the way the predefined sections do in tl_internal.h.
#define nb_sym_versions     s1->nb_sym_versions
#define sym_versions        s1->sym_versions
#define nb_sym_to_version   s1->nb_sym_to_version
#define sym_to_version      s1->sym_to_version
#define dt_verneednum       s1->dt_verneednum
#define versym_section      s1->versym_section
#define verneed_section     s1->verneed_section

/// ================================================================ sections

/// The alignment a section starts life with.  section_add() raises it whenever
/// it is asked for something stricter, so these are floors, not final values.
/// Only two types have anything to say: .gnu.version is an array of halfwords,
/// and a string table has no alignment requirement at all.  Everything else
/// this linker creates holds addresses or machine words, for which pointer
/// alignment is both sufficient and what a C compiler would have chosen.
static i32 default_sh_addralign(i32 sh_type)
{
    switch (sh_type) {
    case SHT_GNU_versym: return 2;
    case SHT_STRTAB:     return 1;
    default:             return PTR_SIZE;
    }
}

/// ------------------------------------------------------ the section name index
///
/// Merging an object asks for a section by name once per section it carries,
/// and the answer used to come from a walk of every section created so far.
/// That is quadratic in the number of sections, which nobody notices until
/// something is compiled with -ffunction-sections: a link with 72,000 sections
/// spent 96% of its time in strcmp, inside that walk.
///
/// The index below answers the same question directly. It holds borrowed
/// pointers, since `sections` owns them, and it is rebuilt rather than
/// maintained when it grows.

/// The hash the index is keyed on.  Only ever consulted in memory, so the
/// choice is ours and nothing outside this file depends on it.
static u32 sec_name_hash(const char *name)
{
    // FNV-1a: short names, and the cost has to stay well under the strcmp it
    // is replacing.
    u32 h = 2166136261u;
    while (*name)
        h = (h ^ (u8)*name++) * 16777619u;

    return h;
}

/// Insert unless the name is already there. First one wins, which is what a
/// walk from the start of the list returned.
static void sec_index_put(tli_State *s1, tli_Section *sec)
{
    u32 mask = (u32)s1->sec_by_name_size - 1;
    u32 i = sec_name_hash(sec->name) & mask;

    for (; s1->sec_by_name[i]; i = (i + 1) & mask)
        if (0 == strcmp(s1->sec_by_name[i]->name, sec->name))
            return;

    s1->sec_by_name[i] = sec;
}

/// Rebuild the name index at twice its size, since it is cheaper to redo
/// than to maintain: every insertion is a first-one-wins probe anyway.
static void sec_index_grow(tli_State *s1)
{
    i32 size = s1->sec_by_name_size ? s1->sec_by_name_size * 2 : 256;
    i32 i;

    tli_free(s1->sec_by_name);
    s1->sec_by_name = tli_mallocz(sizeof(*s1->sec_by_name) * (usize)size);
    s1->sec_by_name_size = size;

    // In creation order, so that first-one-wins still means the same one.
    for (i = 1; i < s1->nb_sections; i++)
        sec_index_put(s1, s1->sections[i]);
}

/// The section carrying this name, or NULL. Only sections in `sections` are
/// indexed; a private one never appears in an output and is not looked up by
/// name.
ST_FUNC tli_Section *tli_find_section_by_name(tli_State *s1, const char *name)
{
    u32 mask, i;

    if (!s1->sec_by_name)
        return NULL;

    mask = (u32)s1->sec_by_name_size - 1;
    for (i = sec_name_hash(name) & mask; s1->sec_by_name[i]; i = (i + 1) & mask)
        if (0 == strcmp(s1->sec_by_name[i]->name, name))
            return s1->sec_by_name[i];

    return NULL;
}

/// Forward declared because new_section() creates one as the section count
/// crosses SHN_LORESERVE, and new_xindex() creates a section to do it.
static void new_xindex(tli_Section *symtab);

/// Create a section and put it on the list its flags call for: the output
/// list, numbered by the header it will carry, or the private one.
ST_FUNC tli_Section *new_section(tli_State *s1, const char *name, i32 sh_type, i32 sh_flags)
{
    tli_Section *sec;

    // The name goes in a trailing array so that one allocation holds both the
    // header and the name, and the name never has to be freed separately.
    // tli_Section already ends in char name[1], hence strlen and not strlen + 1.
    sec = tli_mallocz(sizeof(tli_Section) + strlen(name));
    strcpy(sec->name, name);
    sec->s1 = s1;
    sec->sh_type = sh_type;
    sec->sh_flags = sh_flags;
    sec->sh_addralign = default_sh_addralign(sh_type);

    if (sh_flags & SHF_PRIVATE) {
        // No section number: it will never appear in a section header table,
        // and leaving sh_num zero means anything that mistakes it for an
        // output section lands on the reserved null section rather than on
        // somebody else's data.
        dynarray_add(&s1->priv_sections, &s1->nb_priv_sections, sec);
    } else {
        // A symbol names its section through st_shndx, sixteen bits wide,
        // whose top range from SHN_LORESERVE up is reserved for values that
        // are not section numbers at all. ELF gets past that with SHN_XINDEX
        // and a companion table; neither of the other two formats has an
        // equivalent, and both have far lower section limits of their own, so
        // there is nothing to fall back on. Refusing beats emitting an image
        // whose symbols quietly name the wrong sections.
        if (s1->fmt != TLI_FMT_ELF && s1->nb_sections == SHN_LORESERVE)
            tli_error_noabort("too many sections: a symbol cannot name one "
                              "past %d, and this output format has no "
                              "extended section indices", SHN_LORESERVE - 1);

        sec->sh_num = s1->nb_sections;
        dynarray_add(&s1->sections, &s1->nb_sections, sec);

        // Keep the load factor under about two thirds; past that the probe
        // runs get long enough to matter.
        if (s1->nb_sections * 3 >= s1->sec_by_name_size * 2)
            sec_index_grow(s1);
        else
            sec_index_put(s1, sec);

        // The section just numbered is the last one st_shndx can hold, so
        // every symbol table the link has needs its companion array before
        // the next section exists. Creating them here, the once, keeps the
        // creation of a section out of the later stages: once the output has
        // been laid out there is nowhere left to put one.
        if (s1->nb_sections == SHN_LORESERVE) {
            if (s1->symtab)
                new_xindex(s1->symtab);

            if (s1->dynsym)
                new_xindex(s1->dynsym);
        }
    }
    return sec;
}

/// Release a section's contents but not the section itself: the header and the
/// name stay valid, which is what the -run path wants after it has copied the
/// bytes into the pages it will execute from.
ST_FUNC void free_section(tli_Section *s)
{
    if (!s)
        return;

    tli_free(s->data);
    s->data = NULL;
    s->data_allocated = s->data_offset = 0;
}

/// Grow a section's buffer to hold at least new_size bytes.
///
/// Capacity doubles rather than tracking the request, because the callers that
/// matter append one element at a time: a large link puts tens of thousands of
/// symbols into .symtab through single-entry section_ptr_add() calls, and
/// anything short of geometric growth turns that into quadratic copying.
///
/// Fresh capacity is zeroed.  Callers depend on that in two ways: a hash table
/// is indexed before every slot of it has been written, and put_elf_sym()
/// leaves any padding in an ElfW(Sym) untouched, so the output would otherwise
/// carry whatever the allocator happened to hand back.
ST_FUNC void section_realloc(tli_Section *sec, u64 new_size)
{
    u64 size = sec->data_allocated;
    u8 *data;

    if (size == 0)
        size = 1;
    while (size < new_size)
        size *= 2;

    data = tli_realloc(sec->data, size);
    memset(data + sec->data_allocated, 0, size - sec->data_allocated);
    sec->data = data;
    sec->data_allocated = size;
}

/// Reserve size bytes aligned to align at the end of the section and return
/// the offset of the first of them.  align must be a power of two, which every
/// ELF alignment is; the section's own alignment is raised to match, since a
/// section that has to place something at a given alignment must itself be
/// placed at least that strictly.
ST_FUNC usize section_add(tli_Section *sec, addr_t size, i32 align)
{
    usize offset, end;

    if (align < 1)
        align = 1;

    offset = (sec->data_offset + align - 1) & ~(usize)(align - 1);
    end = offset + size;

    // SHT_NOBITS sections occupy address space and no file space, so there is
    // nothing to allocate; only the offset moves.
    if (sec->sh_type != SHT_NOBITS && end > sec->data_allocated)
        section_realloc(sec, end);

    sec->data_offset = end;
    if (align > sec->sh_addralign)
        sec->sh_addralign = align;

    return offset;
}

/// Append size unaligned bytes and return a pointer to them.  The pointer is
/// only valid until the next call that can grow this section.
ST_FUNC void *section_ptr_add(tli_Section *sec, addr_t size)
{
    // The offset has to be taken first: section_add() is what allocates the
    // buffer, so reading sec->data in the same expression could read the
    // pointer from before the allocation.
    usize offset = section_add(sec, size, 1);

    return sec->data + offset;
}

/// As tli_find_section_by_name(), but creates the section when it is missing.
/// A section conjured this way is allocated progbits, which is what the
/// callers want: they are about to put something in it that the image must
/// carry.
ST_FUNC tli_Section *find_section(tli_State *s1, const char *name)
{
    tli_Section *sec = tli_find_section_by_name(s1, name);

    if (!sec)
        sec = new_section(s1, name, SHT_PROGBITS, SHF_ALLOC);

    return sec;
}

/// Make sure a section is at least size bytes long without moving the append
/// point backwards.  The GOT is filled in through this once its final extent
/// is known, which is not necessarily the extent it has been grown to.
static void section_reserve(tli_Section *sec, u64 size)
{
    if (size > sec->data_allocated)
        section_realloc(sec, size);

    if (size > sec->data_offset)
        sec->data_offset = size;
}

/// ========================================================= string tables

/// Add a string and return its offset.  No attempt is made to share equal
/// strings: the tables this feeds are written once and never searched, and the
/// inputs already deduplicate names through the symbol table above them.
ST_FUNC i32 put_elf_str(tli_Section *s, const char *sym)
{
    i32 len = strlen(sym) + 1;
    i32 offset = s->data_offset;

    memcpy(section_ptr_add(s, len), sym, len);
    return offset;
}

/// ========================================================= symbol hashing
///
/// Two kinds of hash section exist, and they are not interchangeable.
///
/// The one attached to the output's .dynsym is written into the file and
/// walked by the dynamic loader, so its shape is dictated by the gABI: a
/// header of nbucket and nchain, then nbucket bucket heads, then one chain
/// slot per symbol.  A lookup takes hash % nbucket and follows the chain of
/// symbol indices from there.
///
/// The tables the linker keeps for its own lookups have the same shape but are
/// SHF_PRIVATE and never leave memory, which means the bucket function is ours
/// to choose.  That is worth doing, because the gABI hash puts almost nothing
/// in its low bits and the bucket index is exactly those bits: linking 46000
/// compiler-generated names filled a fifth of the buckets and walked 22 entries
/// on an average miss, where a uniform table walks one.  Mixing the value
/// before taking the remainder costs a handful of instructions and brings that
/// down to 2.4.  The emitted table cannot do it and the private ones should.

/// The hash from the gABI's symbol table chapter, unchanged: it must be, for
/// the table the loader reads and for the vna_hash of a version record.
static ElfW(Word) elf_hash(const u8 *name)
{
    ElfW(Word) h = 0, carry;

    while (*name) {
        h = (h << 4) + *name++;
        carry = h & 0xf0000000;
        if (carry)
            h ^= carry >> 24;

        h &= ~carry;
    }
    return h;
}

/// Which bucket of hs a name belongs in.  See above for why a private table
/// gets the extra mixing step.
static i32 elf_bucket(tli_Section *hs, const u8 *name, i32 nbuckets)
{
    ElfW(Word) h = elf_hash(name);

    if (hs->sh_flags & SHF_PRIVATE) {
        // the finalisation step of MurmurHash3, which is where these
        // constants come from: it spreads every input bit over the word
        h ^= h >> 16;
        h *= 0x85ebca6b;
        h ^= h >> 13;
        h *= 0xc2b2ae35;
        h ^= h >> 16;
    }
    return h % nbuckets;
}

/// Discard a symbol table's hash and build it again with nb_buckets buckets,
/// or with as many as it already had when nb_buckets is zero.
///
/// Chains are built by pushing each symbol onto the head of its bucket, so a
/// bucket ends up in descending index order and the most recently added
/// definition of a name is found first.  set_elf_sym() relies on that: it
/// patches the entry find_elf_sym() returns, and that has to be the live one.
static void rebuild_hash(tli_Section *s, u32 nb_buckets)
{
    i32 *hdr, *buckets, *chain;
    i32 nb_syms, i;
    u8 *strtab = s->link->data;

    nb_syms = s->data_offset / sizeof(ElfW(Sym));
    if (!nb_buckets)
        nb_buckets = ((i32 *)s->hash->data)[0];

    // Lay the table out again from the start: two header words, then the
    // bucket heads, then one chain slot per symbol.
    s->hash->data_offset = 0;
    hdr = section_ptr_add(s->hash, (2 + nb_buckets + nb_syms) * sizeof(i32));
    hdr[0] = nb_buckets;
    hdr[1] = nb_syms;
    buckets = hdr + 2;
    chain = buckets + nb_buckets;
    memset(buckets, 0, nb_buckets * sizeof(i32));

    // Index zero is the reserved undefined symbol and is in no bucket.
    chain[0] = 0;
    for (i = 1; i < nb_syms; i++) {
        ElfW(Sym) *sym = (ElfW(Sym) *)s->data + i;
        i32 h;

        if (ELFW(ST_BIND)(sym->st_info) == STB_LOCAL) {
            // Locals are never looked up by name; they keep their chain slot
            // so that indices line up, but it stays empty.
            chain[i] = 0;
            continue;
        }
        h = elf_bucket(s->hash, strtab + sym->st_name, nb_buckets);
        chain[i] = buckets[h];
        buckets[h] = i;
    }
}

/// ------------------------------------------- extended section indices
///
/// st_shndx is sixteen bits and everything from SHN_LORESERVE up is a value
/// in its own right rather than a section number, so a symbol cannot name a
/// section numbered at or above that.  The gABI's way out is SHN_XINDEX: the
/// field then says only that the number is elsewhere, and elsewhere is a
/// section of type SHT_SYMTAB_SHNDX holding one 32-bit number per symbol, in
/// symbol table order, zero for every symbol that does not need one.  Its
/// sh_link names the symbol table the numbers belong to.
///
/// That companion hangs off the symbol table as ->xindex and is a section
/// like any other, so the layout sizes it and the writer emits it without
/// having to know what it is for.  A link that stays under the limit never
/// grows one, which is what a reader of an ordinary object is entitled to
/// expect.
///
/// The symbol table holds in memory exactly what it will hold on disk, so
/// nothing has to be converted on the way out.  Both halves of a section
/// number are written by sym_set_shndx() and read back by tli_sym_section(),
/// and every permutation of a symbol table carries the companion with it, so
/// the number cannot drift away from the symbol it belongs to.

/// Split a section's number into the pair a symbol table stores for it.
ST_FUNC void tli_shndx_of(tli_Section *sec, i32 *shndx, i32 *xshndx)
{
    if (sec->sh_num >= SHN_LORESERVE) {
        *shndx = SHN_XINDEX;
        *xshndx = sec->sh_num;
    } else {
        *shndx = sec->sh_num;
        *xshndx = 0;
    }
}

/// Give a symbol table its companion.  It starts out as long as the table and
/// all zero, and put_elf_sym() extends it with every symbol added after that,
/// so entry i belongs to symbol i for as long as both exist.
static void new_xindex(tli_Section *symtab)
{
    tli_Section *x;
    char name[256];

    if (symtab->xindex)
        return;

    // .symtab_shndx is the name a reader looks for; the companion of any
    // other table is named after that table, though what actually ties the
    // two together is sh_link.
    snprintf(name, sizeof name, "%s_shndx", symtab->name);
    x = new_section(symtab->s1, name, SHT_SYMTAB_SHNDX, symtab->sh_flags);
    x->sh_entsize = sizeof(ElfW(Word));
    x->sh_addralign = sizeof(ElfW(Word));
    x->link = symtab;
    symtab->xindex = x;
    section_ptr_add(x, symtab->data_offset / sizeof(ElfW(Sym))
                       * sizeof(ElfW(Word)));
}

/// Where a symbol's extended index lives, or NULL when there is nowhere for
/// it: no companion is how a table says that no symbol in it needed one.
static ElfW(Word) *sym_xindex_slot(tli_Section *symtab, i32 sym_index)
{
    tli_Section *x = symtab->xindex;
    u64 at = (u64)sym_index * sizeof(ElfW(Word));

    if (!x || at >= x->data_offset)
        return NULL;

    return (ElfW(Word) *)(void *)(x->data + at);
}

/// The other half of a symbol's section number, for a caller copying the pair
/// into another symbol table.  Zero unless st_shndx is SHN_XINDEX.
ST_FUNC i32 tli_sym_xindex(tli_Section *symtab, i32 sym_index)
{
    ElfW(Word) *slot = sym_xindex_slot(symtab, sym_index);

    return slot ? (i32)*slot : 0;
}

/// The number of the section a symbol is defined in, or zero when it is not
/// defined in one: an undefined, absolute or common symbol carries a value
/// rather than a place.
///
/// Zero is also the only honest answer, because a section number and a
/// reserved value are told apart by which half of the pair they came out of
/// and not by their value.  Once the link has more than SHN_LORESERVE
/// sections, numbers in the reserved range exist as sections and can only be
/// named through the companion; a caller handed the bare number could no
/// longer tell one from SHN_ABS.
ST_FUNC i32 tli_sym_section(tli_Section *symtab, i32 sym_index)
{
    ElfW(Sym) *sym = (ElfW(Sym) *)symtab->data + sym_index;

    if (sym->st_shndx == SHN_XINDEX)
        return tli_sym_xindex(symtab, sym_index);

    return sym->st_shndx < SHN_LORESERVE ? sym->st_shndx : 0;
}

/// Record which section a symbol is defined in.  Both halves are written here
/// and nowhere else.  A symbol that no longer takes the escape has its entry
/// cleared, because the format asks that the entry be zero for every symbol
/// whose st_shndx already says what it means.
static void sym_set_shndx(tli_Section *symtab, i32 sym_index, i32 shndx, i32 xshndx)
{
    ElfW(Sym) *sym = (ElfW(Sym) *)symtab->data + sym_index;
    ElfW(Word) *slot;

    sym->st_shndx = shndx;
    if (shndx == SHN_XINDEX)
        new_xindex(symtab);

    slot = sym_xindex_slot(symtab, sym_index);
    if (slot)
        *slot = shndx == SHN_XINDEX ? (ElfW(Word))xshndx : 0;
}

/// Record that a symbol is defined in the section with this number, which the
/// caller has already established is a section number and not one of the
/// reserved values.
ST_FUNC void tli_sym_set_section(tli_Section *symtab, i32 sym_index, i32 shndx)
{
    if (shndx >= SHN_LORESERVE)
        sym_set_shndx(symtab, sym_index, SHN_XINDEX, shndx);
    else
        sym_set_shndx(symtab, sym_index, shndx, 0);
}

/// Carry the companion through a permutation of its symbol table.  This is
/// called from beside every memcpy that installs a permuted table, so that
/// the two always move as one.
static void permute_xindex(tli_Section *symtab, const i32 *old_to_new_syms,
                           i32 nb_syms)
{
    ElfW(Word) *x, *sorted;
    i32 i;

    if (!symtab->xindex)
        return;

    x = (ElfW(Word) *)(void *)symtab->xindex->data;
    sorted = tli_malloc(nb_syms * sizeof *sorted);
    for (i = 0; i < nb_syms; i++)
        sorted[old_to_new_syms[i]] = x[i];

    memcpy(x, sorted, nb_syms * sizeof *sorted);
    tli_free(sorted);
}

/// Append a symbol and return its index.  The caller has already decided that
/// this is a new symbol; set_elf_sym() is the entry point that merges.
ST_FUNC i32 put_elf_sym_x(tli_Section *s, addr_t value, u64 size,
    i32 info, i32 other, i32 shndx, i32 xshndx, const char *name)
{
    ElfW(Sym) *sym;
    tli_Section *hs;
    i32 name_offset, sym_index;

    // The entry itself, filled in field by field so that anything the
    // structure pads is left as the zeros section_realloc() guarantees.
    sym = section_ptr_add(s, sizeof(ElfW(Sym)));

    // An empty name is offset zero, which is the empty string every ELF
    // string table starts with, so there is nothing to add for it.
    name_offset = (name && name[0]) ? put_elf_str(s->link, name) : 0;
    sym->st_name = name_offset;
    sym->st_value = value;
    sym->st_size = size;
    sym->st_info = info;
    sym->st_other = other;
    sym->st_shndx = shndx;
    sym_index = sym - (ElfW(Sym) *)s->data;

    // The companion holds one entry per symbol whether or not the symbol
    // needs one, for the same reason the hash below gains a chain slot for
    // every symbol: both are read by symbol index.
    if (s->xindex)
        *(ElfW(Word) *)section_ptr_add(s->xindex, sizeof(ElfW(Word))) = 0;

    if (shndx == SHN_XINDEX)
        sym_set_shndx(s, sym_index, shndx, xshndx);

    hs = s->hash;
    if (hs) {
        i32 *chain_slot = section_ptr_add(hs, sizeof(i32));
        i32 *hdr = (i32 *)hs->data;

        // nchain counts every symbol, hashed or not, because the loader uses
        // it to size the chain array.
        hdr[1]++;
        if (ELFW(ST_BIND)(info) == STB_LOCAL) {
            *chain_slot = 0;
        } else {
            i32 nbuckets = hdr[0];
            i32 h = elf_bucket(hs, (u8 *)s->link->data + name_offset,
                               nbuckets);
            *chain_slot = hdr[2 + h];
            hdr[2 + h] = sym_index;

            // Keep the load factor near two.  Only hashed symbols count: a
            // table full of locals has nothing in it to collide.
            if (++hs->nb_hashed_syms > 2 * nbuckets)
                rebuild_hash(s, 2 * nbuckets);
        }
    }
    return sym_index;
}

/// put_elf_sym_x() for a section number st_shndx can hold on its own, which
/// is every caller that is not copying a symbol from another table.
ST_FUNC i32 put_elf_sym(tli_Section *s, addr_t value, u64 size,
    i32 info, i32 other, i32 shndx, const char *name)
{
    return put_elf_sym_x(s, value, size, info, other, shndx, 0, name);
}

/// Find a symbol by name, or zero.  Zero doubles as "not found" and as the
/// index of the reserved undefined symbol, which is harmless: that entry is
/// undefined and unnamed, so every caller that cares tests it anyway.
ST_FUNC i32 find_elf_sym(tli_Section *s, const char *name)
{
    tli_Section *hs = s->hash;
    i32 *hdr, nbuckets, i;

    if (!hs)
        return 0;

    hdr = (i32 *)hs->data;
    nbuckets = hdr[0];
    for (i = hdr[2 + elf_bucket(hs, (const u8 *)name, nbuckets)];
         i != 0;
         i = hdr[2 + nbuckets + i]) {
        ElfW(Sym) *sym = (ElfW(Sym) *)s->data + i;

        if (!strcmp(name, (char *)s->link->data + sym->st_name))
            return i;
    }
    return 0;
}

/// Seed a new symbol table with the two entries every ELF symbol table has:
/// the empty string at offset zero of its string table, and the reserved
/// undefined symbol at index zero.  One bucket is enough to start with;
/// put_elf_sym() doubles the table as soon as anything hashes into it.
ST_FUNC void init_symtab(tli_Section *s)
{
    i32 *hdr;

    put_elf_str(s->link, "");
    section_ptr_add(s, sizeof(ElfW(Sym)));

    // The companion is indexed by symbol, so it needs the reserved symbol's
    // entry as much as the table does.  Setting the length rather than
    // adding to it also seeds a table that is being built again from the
    // start, which is what the in-memory link does when it drops the symbols
    // it no longer needs.
    if (s->xindex) {
        s->xindex->data_offset = 0;
        *(ElfW(Word) *)section_ptr_add(s->xindex, sizeof(ElfW(Word))) = 0;
    }
    hdr = section_ptr_add(s->hash, (2 + 1 + 1) * sizeof(i32));
    hdr[0] = 1;  // nbucket
    hdr[1] = 1;  // nchain: symbol zero
    hdr[2] = 0;  // the one bucket, empty
    hdr[3] = 0;  // the chain slot of symbol zero
}

/// Build a symbol table together with the string table and hash table that
/// serve it, and wire the three to each other the way the gABI expects:
/// sh_link of a symbol table names its strings, sh_link of a hash table names
/// the symbols it indexes.
ST_FUNC tli_Section *new_symtab(tli_State *s1,
                            const char *symtab_name, i32 sh_type, i32 sh_flags,
                            const char *strtab_name,
                            const char *hash_name, i32 hash_sh_flags)
{
    tli_Section *symtab, *strtab, *hash;

    // The three sections, then the links between them.
    symtab = new_section(s1, symtab_name, sh_type, sh_flags);
    symtab->sh_entsize = sizeof(ElfW(Sym));
    strtab = new_section(s1, strtab_name, SHT_STRTAB, sh_flags);
    hash = new_section(s1, hash_name, SHT_HASH, hash_sh_flags);
    hash->sh_entsize = sizeof(i32);

    symtab->link = strtab;
    symtab->hash = hash;
    hash->link = symtab;

    // A table created after the link has run out of section numbers needs its
    // companion from the start: new_section() only hands them out as the
    // limit is crossed, and this one was not there to be given one.
    if (s1->nb_sections > SHN_LORESERVE)
        new_xindex(symtab);

    init_symtab(symtab);
    return symtab;
}

/// ==================================================== symbol definition

/// Define a symbol, merging it with any definition already present, and return
/// its index.
///
/// The rules are the gABI's, plus the two conventions every Unix linker has
/// settled on.  In order of precedence:
///
///   - a definition beats an undefined reference;
///   - a global definition beats a weak one, and a second weak definition of a
///     name that already has one is ignored, so the first weak wins;
///   - a definition in a real section beats a common or .bss allocation, since
///     the tentative definition is exactly what the real one is meant to
///     supersede;
///   - visibility is merged separately and always tightens, never loosens:
///     default < protected < hidden < internal, which is the order the gABI
///     gives, and STV_HIDDEN or STV_INTERNAL on the incoming symbol also stops
///     it overriding anything;
///   - anything left over is two real definitions of the same name, which is
///     an error, except in the table of what the input libraries export, where
///     two libraries defining the same name is ordinary.
ST_FUNC i32 set_elf_sym_x(tli_Section *s, addr_t value, u64 size,
                          i32 info, i32 other, i32 shndx, i32 xshndx,
                          const char *name)
{
    tli_State *s1 = s->s1;
    ElfW(Sym) *esym;
    i32 sym_bind, sym_type, esym_bind;
    u8 sym_vis, esym_vis, new_vis;
    i32 sym_index, section, esection;

    sym_bind = ELFW(ST_BIND)(info);
    sym_type = ELFW(ST_TYPE)(info);
    sym_vis = ELFW(ST_VISIBILITY)(other);

    // The rules below weigh one definition's section against another's, so
    // the incoming pair is read exactly as tli_sym_section() reads an entry
    // already in the table: the number of the section, or zero for a symbol
    // that names no section at all.
    section = shndx == SHN_XINDEX ? xshndx
              : shndx < SHN_LORESERVE ? shndx : 0;

    // A local symbol is never merged: its name means nothing outside the
    // object it came from, so it is simply appended.
    if (sym_bind == STB_LOCAL)
        goto define;

    sym_index = find_elf_sym(s, name);
    if (!sym_index)
        goto define;

    esym = (ElfW(Sym) *)s->data + sym_index;
    esection = tli_sym_section(s, sym_index);

    // The same object read twice, or a symbol repeated verbatim across
    // objects, is common enough to be worth short-circuiting.
    if (esym->st_value == value && esym->st_size == size
        && esym->st_info == info && esym->st_other == other
        && esym->st_shndx == shndx && esection == section)
        return sym_index;

    if (esym->st_shndx == SHN_UNDEF) {
        // Nothing to weigh against: the existing entry is a reference, and
        // this is either its definition or another reference.
        esym->st_other = other;
        goto patch;
    }

    // Visibility merges on its own and always tightens: whichever of the two
    // is the more restrictive wins, with STV_DEFAULT meaning no opinion.
    esym_bind = ELFW(ST_BIND)(esym->st_info);
    esym_vis = ELFW(ST_VISIBILITY)(esym->st_other);
    if (esym_vis == STV_DEFAULT)
        new_vis = sym_vis;
    else if (sym_vis == STV_DEFAULT)
        new_vis = esym_vis;
    else
        new_vis = esym_vis < sym_vis ? esym_vis : sym_vis;

    esym->st_other = (esym->st_other & ~ELFW(ST_VISIBILITY)(-1)) | new_vis;

    // The precedence rules, in the order the list above gives them.  Falling
    // out of the chain leaves the existing definition in place; reaching
    // patch replaces it.
    if (shndx == SHN_UNDEF) {
        // a reference to something already defined
    } else if (sym_bind == STB_GLOBAL && esym_bind == STB_WEAK) {
        goto patch;
    } else if (sym_bind == STB_WEAK
               && (esym_bind == STB_GLOBAL || esym_bind == STB_WEAK)) {
        // weak loses to a global, and to the weak that got here first
    } else if (sym_vis == STV_HIDDEN || sym_vis == STV_INTERNAL) {
        // a hidden definition does not displace a visible one
    } else if (s->sh_flags & SHF_DYNSYM) {
        // two libraries exporting one name is not a conflict
    } else if ((esym->st_shndx == SHN_COMMON
                || esection == bss_section->sh_num)
               && section && section != bss_section->sh_num) {
        // a real definition supersedes a tentative one
        goto patch;
    } else if (shndx == SHN_COMMON || section == bss_section->sh_num) {
        // and is not itself superseded by a later tentative one
    } else if (esym->st_other & ST_ASM_SET) {
        // what an assembler .set put there is a placeholder
        goto patch;
    } else {
        tli_error_noabort("link symbol '%s' defined twice", name);
    }
    return sym_index;

patch:
    esym->st_info = ELFW(ST_INFO)(sym_bind, sym_type);
    sym_set_shndx(s, sym_index, shndx, xshndx);
    esym->st_value = value;
    esym->st_size = size;
    return sym_index;

define:
    return put_elf_sym_x(s, value, size, ELFW(ST_INFO)(sym_bind, sym_type),
                         other, shndx, xshndx, name);
}

/// set_elf_sym_x() for a section number st_shndx can hold on its own.
ST_FUNC i32 set_elf_sym(tli_Section *s, addr_t value, u64 size,
                        i32 info, i32 other, i32 shndx, const char *name)
{
    return set_elf_sym_x(s, value, size, info, other, shndx, 0, name);
}

/// ======================================================= symbol lookup

/// The address of a defined symbol, or -1.  With forc set the name is first
/// decorated the way the target decorates C identifiers, so that a caller
/// holding a plain C name finds it.
ST_FUNC addr_t get_sym_addr(tli_State *s1, const char *name, i32 err, i32 forc)
{
    ElfW(Sym) *sym;
    i32 sym_index;
    char buf[256];

    // Decorate the name, unless it is a 32-bit PE stdcall name, which
    // already carries its underscore.
    if (forc && s1->leading_underscore
        && !(s1->fmt == TLI_FMT_PE && strchr(name, '@'))) {
        buf[0] = '_';
        pstrcpy(buf + 1, sizeof(buf) - 1, name);
        name = buf;
    }

    sym_index = find_elf_sym(s1->symtab, name);
    sym = (ElfW(Sym) *)s1->symtab->data + sym_index;
    if (!sym_index || sym->st_shndx == SHN_UNDEF) {
        if (err)
            tli_error_noabort("%s not defined", name);

        return (addr_t)-1;
    }
    return sym->st_value;
}

#ifdef TLI_HOST_RUN

/// The address of a symbol in an image linked into this process, or NULL.
/// The published spelling of get_sym_addr(), and the way an embedder reaches
/// what it has just linked.
TLI_PUBLIC void *tli_get_symbol(tli_State *s, const char *name)
{
    addr_t addr = get_sym_addr(s, name, 0, 1);

    return addr == (addr_t)-1 ? NULL : (void *)(uintptr_t)addr;
}
#endif

/// Define a symbol at an address in this process, so that the image being
/// linked can call back into whatever the embedder wants to expose.
TLI_PUBLIC i32 tli_add_symbol(tli_State *s1, const char *name, const void *val)
{
    char buf[256];

    // An address in this process is not necessarily within reach of a 32-bit
    // displacement from the image, so it goes through the import machinery
    // and gets an indirection, exactly as a real DLL symbol would.
    if (s1->fmt == TLI_FMT_PE) {
        pe_putimport(s1, 0, name, (uintptr_t)val);
        return 0;
    }

    if (s1->leading_underscore) {
        buf[0] = '_';
        pstrcpy(buf + 1, sizeof(buf) - 1, name);
        name = buf;
    }

    // A null section makes it SHN_ABS: the value is the address itself.
    set_global_sym(s1, name, NULL, (addr_t)(uintptr_t)val);
    return 0;
}

#ifdef TLI_HOST_RUN

/// Report every symbol an embedder could reasonably call.  Anything without an
/// address is skipped, as are symbols another module has no business seeing.
TLI_PUBLIC void tli_list_symbols(tli_State *s, void *ctx,
    void (*symbol_cb)(void *ctx, const char *name, const void *val))
{
    tli_Section *symtab = s->symtab;
    i32 i, nb_syms = symtab->data_offset / sizeof(ElfSym);

    for (i = 0; i < nb_syms; i++) {
        ElfW(Sym) *sym = (ElfW(Sym) *)symtab->data + i;

        if (!sym->st_value)
            continue;

        if (ELFW(ST_BIND)(sym->st_info) != STB_GLOBAL)
            continue;

        if (ELFW(ST_VISIBILITY)(sym->st_other) != STV_DEFAULT)
            continue;

        symbol_cb(ctx, (char *)symtab->link->data + sym->st_name,
                  (void *)(uintptr_t)sym->st_value);
    }
}
#endif

/// ==================================================== symbol attributes

/// The extra state a symbol picks up while being linked, indexed by its place
/// in s1->symtab.  The array is grown to the next power of two past the index
/// asked for, so the walk-everything passes below never realloc more than a
/// logarithmic number of times.
///
/// With alloc clear this will not grow the array, and an index past its end
/// yields element zero instead.  That is deliberate: element zero belongs to
/// the reserved undefined symbol and is permanently blank, so a caller reading
/// ->dyn_index or ->got_offset off it gets the right answer, which is nought,
/// without every call site having to test for a null return.
ST_FUNC struct tli_SymAttr *get_sym_attr(tli_State *s1, i32 index, i32 alloc)
{
    i32 n;

    if (index >= s1->nb_sym_attrs) {
        if (!alloc)
            return s1->sym_attrs;

        for (n = 1; n <= index; n *= 2)
            ;

        s1->sym_attrs = tli_realloc(s1->sym_attrs, n * sizeof(*s1->sym_attrs));
        memset(s1->sym_attrs + s1->nb_sym_attrs, 0,
               (n - s1->nb_sym_attrs) * sizeof(*s1->sym_attrs));
        s1->nb_sym_attrs = n;
    }
    return &s1->sym_attrs[index];
}

/// ======================================================== relocations

/// Record a relocation against s, creating s's relocation section if this is
/// the first one.  The relocation section inherits the symbol table's flags,
/// so that relocations against an allocated symbol table are themselves
/// allocated and end up in the image.
ST_FUNC void put_elf_reloca(tli_Section *symtab, tli_Section *s, u64 offset,
                            i32 type, i32 symbol, addr_t addend)
{
    tli_State *s1 = s->s1;
    ElfW_Rel *rel;
    tli_Section *sr = s->reloc;

    if (!sr) {
        char buf[256];

        snprintf(buf, sizeof(buf), REL_SECTION_FMT, s->name);
        sr = new_section(s1, buf, SHT_RELX, symtab->sh_flags);
        sr->sh_entsize = sizeof(ElfW_Rel);
        sr->link = symtab;
        sr->sh_info = s->sh_num;
        s->reloc = sr;
    }

    rel = section_ptr_add(sr, sizeof(ElfW_Rel));
    rel->r_offset = offset;
    rel->r_info = ELFW(R_INFO)(symbol, type);
#if SHT_RELX == SHT_RELA
    rel->r_addend = addend;
#endif
    if (SHT_RELX != SHT_RELA && addend)
        tli_error_noabort("non-zero addend on REL architecture");
}

/// put_elf_reloca() with no addend, which is every relocation this linker
/// generates itself; only merged input records carry one.
ST_FUNC void put_elf_reloc(tli_Section *symtab, tli_Section *s, u64 offset,
                           i32 type, i32 symbol)
{
    put_elf_reloca(symtab, s, offset, type, symbol, 0);
}

/// Rewrite every relocation that refers to symbol table s so that it names the
/// symbol's new index.  Symbols below first_sym did not move.  Used after the
/// table has been permuted, which the ELF format requires twice: locals must
/// precede globals in any symbol table, and the entries of a .dynsym must be
/// ordered to match its .gnu.hash.
static void update_relocs(tli_State *s1, tli_Section *s, i32 *old_to_new_syms,
                          i32 first_sym)
{
    i32 i;

    for (i = 1; i < s1->nb_sections; i++) {
        tli_Section *sr = s1->sections[i];
        ElfW_Rel *rel;

        if (sr->sh_type != SHT_RELX || sr->link != s)
            continue;

        for_each_elem(sr, 0, rel, ElfW_Rel) {
            i32 sym_index = ELFW(R_SYM)(rel->r_info) - first_sym;
            i32 type = ELFW(R_TYPE)(rel->r_info);

            // A relocation against symbol zero has no symbol; assembler
            // output produces those for section-relative references.
            if (sym_index < 0)
                continue;

            rel->r_info = ELFW(R_INFO)(old_to_new_syms[sym_index], type);
        }
    }
}

/// Reorder a symbol table so that every local precedes every non-local, which
/// the gABI requires of a written symbol table, and record where the boundary
/// fell in sh_info.  Both halves keep their original relative order, so the
/// result depends only on the input and not on how the table was built.
static void sort_syms(tli_State *s1, tli_Section *s)
{
    ElfW(Sym) *syms = (ElfW(Sym) *)s->data;
    ElfW(Sym) *sorted;
    i32 *old_to_new_syms;
    i32 nb_syms = s->data_offset / sizeof(ElfW(Sym));
    i32 i, nb_local = 0, next;

    for (i = 0; i < nb_syms; i++)
        nb_local += ELFW(ST_BIND)(syms[i].st_info) == STB_LOCAL;

    sorted = tli_malloc(nb_syms * sizeof(ElfW(Sym)));
    old_to_new_syms = tli_malloc(nb_syms * sizeof(i32));

    // Locals fill the front of the table and non-locals the rest, so one
    // pass with two append points places everything.
    next = nb_local;
    nb_local = 0;
    for (i = 0; i < nb_syms; i++) {
        i32 to = ELFW(ST_BIND)(syms[i].st_info) == STB_LOCAL
                 ? nb_local++ : next++;

        old_to_new_syms[i] = to;
        sorted[to] = syms[i];
    }

    // sh_size is still zero for a symbol table nothing has laid out yet, and
    // a zero sh_info would then say the table has no locals at all.  Leaving
    // it alone in that case keeps disassemblers from tripping over it.
    if (s->sh_size)
        s->sh_info = nb_local;

    memcpy(s->data, sorted, nb_syms * sizeof(ElfW(Sym)));
    tli_free(sorted);
    permute_xindex(s, old_to_new_syms, nb_syms);

    update_relocs(s1, s, old_to_new_syms, 0);
    tli_free(old_to_new_syms);
}

/// ============================================== resolution and relocation

/// Give every symbol in symtab its final value.
///
/// A symbol defined in a section is section-relative on the way in and becomes
/// absolute here, once layout has decided where that section lives.  A symbol
/// in one of the reserved section numbers (SHN_ABS above all) already holds
/// what it means and is left alone.
///
/// An undefined symbol has to come from somewhere else.  do_resolve says where
/// to look: 1 asks the host's dynamic loader, which is what linking into this
/// process wants; 0 accepts that the output's own dynamic symbol table will
/// carry the reference; 2 means symtab is that dynamic table, whose undefined
/// entries are the point of it and are left as they are.
ST_FUNC void relocate_syms(tli_State *s1, tli_Section *symtab, i32 do_resolve)
{
    ElfW(Sym) *sym;

    for_each_elem(symtab, 1, sym, ElfW(Sym)) {
        i32 shndx = tli_sym_section(symtab, sym - (ElfW(Sym) *)symtab->data);
        const char *name;

        if (sym->st_shndx != SHN_UNDEF) {
            if (shndx)
                sym->st_value += s1->sections[shndx]->sh_addr;

            continue;
        }
        if (do_resolve == 2)
            continue;

        name = (char *)symtab->link->data + sym->st_name;

        // Undefined, so it has to come from somewhere else: the host's
        // loader, or the output's own dynamic symbol table.
        if (do_resolve) {
#if defined TLI_HOST_RUN && !defined _WIN32
            // dlsym() wants the name the platform's C compiler would have
            // emitted, without whatever decoration we add on top.
            const char *name_ud = &name[s1->leading_underscore];
            void *addr = NULL;
            i32 i;

            if (!s1->nostdlib)
                addr = dlsym(RTLD_DEFAULT, name_ud);

            for (i = 0; !addr && i < s1->nb_loaded_dlls; i++)
                addr = dlsym(s1->loaded_dlls[i]->handle, name_ud);

            if (addr) {
                sym->st_value = (addr_t)addr;
#ifdef DEBUG_RELOC
                printf("relocate_sym: %s -> 0x%lx\n", name,
                       (u64)sym->st_value);
#endif
                continue;
            }
#endif
        } else if (s1->dynsym && find_elf_sym(s1->dynsym, name)) {
            // It will be resolved at load time through the dynamic symbol
            // table; relocate_section() picks the reference up from there.
            continue;
        }

        // _fp_hw is part of the ARM ABI and is expected to be missing.
        if (!strcmp(name, "_fp_hw"))
            continue;

        // Every dynamic thread-local sequence has been rewritten into the
        // local-exec one, so the calls that named this are gone.  The symbol
        // is still in the table because the object that carried them declared
        // it, and demanding a definition would refuse a link whose only need
        // for one has just been removed.  Only x86_64 relaxes them.
        if (s1->tls_relaxed && !strcmp(name, "__tls_get_addr"))
            continue;

        // An undefined weak reference is legitimate and evaluates to zero.
        if (ELFW(ST_BIND)(sym->st_info) == STB_WEAK)
            sym->st_value = 0;
        else
            tli_error_noabort("unresolved reference to '%s'", name);
    }
}

/// Apply the relocations in sr to the bytes of s.
///
/// The arithmetic is per-architecture and lives in relocate(); what happens
/// here is the part that is the same everywhere: work out where the patch goes
/// and what value it should see.  That value is the symbol's address plus the
/// addend, and for a RELA target the addend is in the record rather than in
/// the bytes being patched.
///
/// Debug information is the one exception.  A pointer-sized field inside the
/// DWARF sections that names another DWARF section is a file offset and not an
/// address, because those sections are never loaded and so have no address, so
/// the section's base is subtracted back off again.
static void relocate_section(tli_State *s1, tli_Section *s, tli_Section *sr)
{
    ElfW_Rel *rel;
    i32 is_dwarf = s->sh_num >= s1->dwlo && s->sh_num < s1->dwhi;

    // relocate() on some targets consumes and rewrites the relocation
    // records in place, keeping the ones the output must retain; qrel is
    // where it writes next.
    qrel = (ElfW_Rel *)sr->data;

    // .bss has nothing to patch, but the bookkeeping below still applies.
    if (s->data) {
        i32 nb_syms = symtab_section->data_offset / sizeof(ElfW(Sym));

        for_each_elem(sr, 0, rel, ElfW_Rel) {
            i32 sym_index = ELFW(R_SYM)(rel->r_info);
            i32 type = ELFW(R_TYPE)(rel->r_info);
            ElfW(Sym) *sym;
            u8 *ptr;
            addr_t val;

            // Both fields are read straight out of an input file and nothing
            // upstream constrains them. An index past the end of the symbol
            // table, or an offset past the end of the section, is read and
            // written all the same: on a damaged object that has been seen to
            // land in heap the linker had already freed. Refuse the record
            // rather than trusting it.
            if (sym_index >= nb_syms) {
                tli_error_noabort("%s: relocation names symbol %d of %d",
                                  s->name, sym_index, nb_syms);
                continue;
            }
            if (rel->r_offset >= s->data_offset) {
                tli_error_noabort("%s: relocation at 0x%llx is past the end "
                                  "of the section (0x%llx)", s->name,
                                  (unsigned long long)rel->r_offset,
                                  (unsigned long long)s->data_offset);
                continue;
            }

            // Where the patch goes, and the value it should see: the
            // symbol's address plus the addend, which a RELA target keeps in
            // the record and a REL one in the bytes being patched.
            sym = (ElfW(Sym) *)symtab_section->data + sym_index;
            ptr = s->data + rel->r_offset;
            val = sym->st_value;

#if SHT_RELX == SHT_RELA
            val += rel->r_addend;
#endif
            if (is_dwarf && (u32)type == s1->arch->r_data_32dw) {
                i32 shndx = tli_sym_section(symtab_section, sym_index);

                if (shndx >= s1->dwlo && shndx < s1->dwhi) {
                    add32le(ptr, val - s1->sections[shndx]->sh_addr);
                    continue;
                }
            }
            s1->arch->relocate(s1, rel, type, ptr, s->sh_addr + rel->r_offset,
                               val);
        }
    }

    if (s1->fmt == TLI_FMT_ELF && (sr->sh_flags & SHF_ALLOC)) {
        // The records that survived are now dynamic relocations, so they
        // index the output's dynamic symbol table rather than .symtab.
        sr->link = s1->dynsym;
        if (s1->output_type & TLI_OUTPUT_DYN) {
            usize kept = (u8 *)qrel - sr->data;

            // A stabs n_value is 32 bits wide.  On a 64-bit target no runtime
            // relocation can write an address into it, so rather than emit
            // records the loader would corrupt the section with, drop them
            // and leave the section unrelocated.
            if (sizeof ((tli_StabSym *)0)->n_value < PTR_SIZE
                && 0 == strcmp(s->name, ".stab"))
                kept = 0;
            sr->data_offset = sr->sh_size = kept;
        }
    }

    // The pairing between a PCREL_HI20 and the LO12 that completes it does
    // not survive across sections, so the table of pending high parts is
    // dropped rather than carried into the next one.
    if (s1->arch->em == EM_RISCV)
        dynarray_reset(&s1->pcrel_hi_entries, &s1->nb_pcrel_hi_entries);
}

/// Remember a symbol difference to work out once the image has been laid out.
ST_FUNC void tli_add_sym_diff(tli_State *s1, i32 sec, usize offset,
                              i32 sym_a, i32 sym_b, i32 len)
{
    struct tli_SymDiff *d = tli_malloc(sizeof *d);

    d->sec = sec;
    d->offset = offset;
    d->sym_a = sym_a;
    d->sym_b = sym_b;
    d->len = len;
    dynarray_add(&s1->sym_diffs, &s1->nb_sym_diffs, d);
}

/// Fold every deferred symbol difference into the field that wanted it.
///
/// By here relocate_syms() has given both ends an absolute value, so "B - A"
/// is finally a number.  A difference is not a reference to either symbol, so
/// nothing is left behind for a loader: the answer goes straight into the
/// field, which is exactly what the same-section case does when it can work
/// the difference out at the time it is read.
static void relocate_sym_diffs(tli_State *s1)
{
    i32 i;

    for (i = 0; i < s1->nb_sym_diffs; ++i) {
        struct tli_SymDiff *d = s1->sym_diffs[i];
        ElfW(Sym) *syms = (ElfW(Sym) *)symtab_section->data;
        ElfW(Sym) *a = &syms[d->sym_a];
        ElfW(Sym) *b = &syms[d->sym_b];
        tli_Section *s = s1->sections[d->sec];
        u8 *ptr = s->data + d->offset;
        i64 diff;

        // An undefined end has no address to measure from, and guessing one
        // would put a plausible number in a field nothing would ever check.
        if (a->st_shndx == SHN_UNDEF || b->st_shndx == SHN_UNDEF) {
            tli_error_noabort("difference between symbols needs both to be "
                              "defined in this link");
            continue;
        }

        diff = (i64)b->st_value - (i64)a->st_value;
        if (d->len == 3)
            write64le(ptr, read64le(ptr) + (u64)diff);
        else
            write32le(ptr, read32le(ptr) + (u32)(i32)diff);
    }

    dynarray_reset(&s1->sym_diffs, &s1->nb_sym_diffs);
}

/// Apply every relocation table in the image.
ST_FUNC void relocate_sections(tli_State *s1)
{
    i32 i;

    // Before the relocations proper: a deferred difference is a constant, and
    // working it out first keeps it out of the per-type switch below, which
    // has no way to name a relocation with two symbols in it.
    if (s1->fmt == TLI_FMT_MACHO)
        relocate_sym_diffs(s1);

    for (i = 1; i < s1->nb_sections; ++i) {
        tli_Section *sr = s1->sections[i];
        tli_Section *s;
        i32 apply = 1;

        if (sr->sh_type != SHT_RELX)
            continue;

        s = s1->sections[sr->sh_info];

        // Relocations against the GOT are the dynamic loader's work, not
        // ours, unless there will be no loader: a static image, or one being
        // built directly in this process, has to have them applied here.
        // Mach-O has no loader-filled GOT of this kind.
        if (s1->fmt != TLI_FMT_MACHO && s == s1->got && !s1->static_link
            && s1->output_type != TLI_OUTPUT_MEMORY)
            apply = 0;

        if (apply)
            relocate_section(s1, s, sr);

        // A relocation table the image carries is read by the loader, which
        // knows nothing of sections, so its offsets become addresses.
        if (s1->fmt == TLI_FMT_ELF && (sr->sh_flags & SHF_ALLOC)) {
            ElfW_Rel *rel;

            for_each_elem(sr, 0, rel, ElfW_Rel)
                rel->r_offset += s->sh_addr;
        }
    }
}

/// ============================================================ GOT and PLT

/// Create the global offset table and the symbol that names it.
///
/// The first three slots are reserved by every psABI this linker targets: the
/// address of the dynamic section, and two words the loader uses for lazy
/// binding.  They are left as zero; the loader fills them in.  The symbol is
/// defined at offset zero of the table because _GLOBAL_OFFSET_TABLE_ is what
/// position-independent code adds its GOT displacements to.
static i32 build_got(tli_State *s1)
{
    // The three reserved slots first, left as the zeros section_realloc()
    // guarantees, then the symbol at offset zero of the table.
    s1->got = new_section(s1, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
    s1->got->sh_entsize = 4;
    section_ptr_add(s1->got, 3 * PTR_SIZE);
    return set_elf_sym(symtab_section, 0, 0,
                       ELFW(ST_INFO)(STB_GLOBAL, STT_OBJECT), 0,
                       s1->got->sh_num, "_GLOBAL_OFFSET_TABLE_");
}

/// Give a symbol a GOT slot, and a PLT entry with it when the relocation asks
/// for a jump slot, and return its attributes.
///
/// A function whose address is taken as well as called ends up with two slots:
/// the code that calls it goes through the PLT, and the code that takes its
/// address must see the function itself, not the stub.  attr->got_offset and
/// attr->plt_offset therefore record separate entries and are tested
/// separately.
///
/// What fills the slot at run time depends on where the value comes from.
/// With a dynamic symbol table the answer is a dynamic relocation naming the
/// symbol, so the loader writes it.  Without one, the only consumer is
/// ourselves, and the relocation is applied against .symtab like any other.
static struct tli_SymAttr *put_got_entry(tli_State *s1, i32 dyn_reloc_type,
                                      i32 sym_index, i32 tls_words)
{
    struct tli_SymAttr *attr = get_sym_attr(s1, sym_index, 1);
    i32 need_plt_entry = (u32)dyn_reloc_type == s1->arch->r_jmp_slot;
    ElfW(Sym) *sym;
    const char *name;
    tli_Section *s_rel;
    u32 got_offset;

    if (need_plt_entry ? attr->plt_offset : attr->got_offset)
        return attr;

    s_rel = s1->got;
    if (need_plt_entry) {
        if (!s1->plt) {
            s1->plt = new_section(s1, ".plt", SHT_PROGBITS,
                                  SHF_ALLOC | SHF_EXECINSTR);
            s1->plt->sh_entsize = 4;
        }

        // A jump slot relocation belongs to .rel.plt rather than .rel.got, so
        // that the loader can bind it lazily; it still points at the GOT.
        s_rel = s1->plt;
    }

    // Take the slot, then find the symbol it is for.  A global-dynamic
    // descriptor is the only entry wider than one word; tls_words says so,
    // and is zero for everything that is not thread-local at all.
    got_offset = s1->got->data_offset;
    section_ptr_add(s1->got, PTR_SIZE * (tls_words > 1 ? tls_words : 1));

    sym = (ElfW(Sym) *)symtab_section->data + sym_index;
    name = (char *)symtab_section->link->data + sym->st_name;

    // A thread-local slot holds a position within the TLS block rather than
    // an address, and the architecture writes it itself when it relocates the
    // instruction that reads the slot.  It gets no relocation of its own: one
    // against the GOT is left for the loader, and the loader cannot know an
    // offset into a block the linker laid out.  That also fixes the answer to
    // this module, so a symbol this link does not define has nothing to take
    // an offset from and is refused rather than resolved to nonsense.
    if (tls_words) {
        if (sym->st_shndx == SHN_UNDEF)
            tli_error_noabort("%s: thread-local storage needs the symbol to "
                              "be defined in this link", name);
    } else if (!s1->dynsym) {
        put_elf_reloc(symtab_section, s1->got, got_offset, dyn_reloc_type,
                      sym_index);
    } else if (ELFW(ST_BIND)(sym->st_info) == STB_LOCAL) {
        // A local symbol has no business in a dynamic symbol table, so its
        // slot is resolved here instead.  Its value is not final yet, though,
        // so the relocation record is used as a note to self: R_RELATIVE is
        // never otherwise generated for the GOT, which makes it a safe
        // marker, and the symbol index it carries is an index into .symtab
        // rather than into .dynsym.  fill_local_got_entries() reads both back
        // and turns the record into a real relative relocation.
        //
        // It is written through s1->dynsym so that, if this is the first
        // relocation the GOT has ever had, the section it creates is linked
        // against the dynamic symbol table like the rest.
        put_elf_reloc(s1->dynsym, s1->got, got_offset, s1->arch->r_relative, sym_index);
    } else {
        if (attr->dyn_index == 0)
            attr->dyn_index = set_elf_sym_x(s1->dynsym, sym->st_value,
                                            sym->st_size, sym->st_info, 0,
                                            sym->st_shndx,
                                            tli_sym_xindex(symtab_section,
                                                           sym_index), name);

        put_elf_reloc(s1->dynsym, s_rel, got_offset, dyn_reloc_type,
                      attr->dyn_index);
    }

    if (!need_plt_entry) {
        attr->got_offset = got_offset;
        return attr;
    }

    attr->plt_offset = s1->arch->create_plt_entry(s1, got_offset, attr);

    // Name the stub, so that a call rewritten to reach it has something to
    // refer to and a backtrace through it reads sensibly.
    {
        char plt_name[200];
        usize len = strlen(name);

        if (len > sizeof plt_name - 5)
            len = sizeof plt_name - 5;

        memcpy(plt_name, name, len);
        strcpy(plt_name + len, "@plt");
        attr->plt_sym = put_elf_sym(s1->symtab, attr->plt_offset, 0,
                                    ELFW(ST_INFO)(STB_GLOBAL, STT_FUNC), 0,
                                    s1->plt->sh_num, plt_name);
    }
    return attr;
}

/// Walk every relocation and build the GOT and PLT entries they imply.
///
/// Two passes, jump slots first.  The psABIs for ARM and AArch64 require the
/// R_*_JUMP_SLOT relocations of an image to be contiguous, because the loader
/// is allowed to process them as a block when binding lazily, and generating
/// them in relocation order would interleave them with the GLOB_DATs.
///
/// got_sym is the index of _GLOBAL_OFFSET_TABLE_ if the GOT already exists and
/// zero otherwise.  It is only wanted at the end, to record the table's final
/// size in the symbol; if the table is built here, the index the caller was
/// given is stale anyway and the local copy takes over.
ST_FUNC void build_got_entries(tli_State *s1, i32 got_sym)
{
    i32 pass;

    for (pass = 0; pass < 2; pass++) {
        i32 i;

        for (i = 1; i < s1->nb_sections; i++) {
            tli_Section *s = s1->sections[i];
            ElfW_Rel *rel;

            if (s->sh_type != SHT_RELX)
                continue;

            // Relocations that are already about the GOT describe its
            // contents rather than references to it.
            if (s->link != symtab_section)
                continue;

            for_each_elem(s, 0, rel, ElfW_Rel) {
                i32 type = ELFW(R_TYPE)(rel->r_info);
                i32 sym_index = ELFW(R_SYM)(rel->r_info);
                // On x86_64 a general- or local-dynamic sequence gets
                // rewritten into the local-exec one when it is relocated, and
                // the call to __tls_get_addr that ends it goes away with it.
                // Its relocation is disarmed here rather than there because by
                // the time relocations are applied the symbol has already had
                // to resolve, and a link with no libc in it -- which is every
                // link that relaxes to local exec and then needs nothing --
                // would have been refused for want of a call it does not make.
                if (s1->arch->em == EM_X86_64
                    && (type == R_X86_64_TLSGD || type == R_X86_64_TLSLD)
                    && (u8 *)(rel + 1) < s->data + s->data_offset) {
                    rel[1].r_info = ELFW(R_INFO)(0, R_X86_64_NONE);
                    s1->tls_relaxed = 1;
                }

                ElfW(Sym) *sym = (ElfW(Sym) *)symtab_section->data + sym_index;
                struct tli_SymAttr *attr;
                i32 gotplt_entry, reloc_type, tls_reloc, want_plt = 0;

                gotplt_entry = gotplt_entry_type(s1, type);
                if (gotplt_entry == -1) {
                    tli_error_noabort("Unknown relocation type for got: %d",
                                      type);
                    continue;
                }
                if (gotplt_entry == NO_GOTPLT_ENTRY)
                    continue;

                // AUTO means the relocation can address the symbol directly
                // when the symbol is here to be addressed.  Two cases are
                // left over: a symbol that is not here at all, and one whose
                // value is an absolute address that a displacement in the
                // instruction may not reach.
                if (gotplt_entry == AUTO_GOTPLT_ENTRY) {
                    if (sym->st_shndx == SHN_UNDEF) {
                        // This linker resolves undefined references in the
                        // image rather than passing them on to the loader, so
                        // a call to a function in a shared library needs a
                        // PLT entry here.  A data symbol was turned into a
                        // copy in .bss by bind_exe_dynsyms() and is defined by
                        // now, so anything still undefined and typed as a
                        // function is one of those calls.
                        if (!s1->arch->pcrel_dllplt
                            && (s1->output_type & TLI_OUTPUT_DYN))
                            continue;
                        if (s1->dynsym) {  // not set when linking to memory
                            i32 dynindex =
                                get_sym_attr(s1, sym_index, 0)->dyn_index;
                            ElfW(Sym) *esym =
                                (ElfW(Sym) *)s1->dynsym->data + dynindex;
                            i32 etype = ELFW(ST_TYPE)(esym->st_info);

                            if (dynindex
                                && (etype == STT_FUNC
                                    || (etype == STT_NOTYPE
                                        && ELFW(ST_TYPE)(sym->st_info)
                                           == STT_FUNC)))
                                want_plt = 1;
                        }
                    } else if (sym->st_shndx == SHN_ABS) {
                        // tli_add_btstub() defines symbols at absolute zero
                        // that stand for "nothing here"; they need no slot.
                        if (sym->st_value == 0)
                            continue;
                        // Elsewhere an absolute value is one an embedder
                        // handed us through tli_add_symbol(), which only
                        // needs indirection where a pointer is wider than the
                        // displacement fields of the instruction set.  ARM
                        // takes the slot regardless.
                        if (PTR_SIZE != 8 && s1->arch->em != EM_ARM)
                            continue;
                    } else {
                        continue;
                    }
                }

                if (!want_plt) {
                    // A call through the PLT to a symbol that is defined here
                    // and cannot be interposed can go straight to it.  The
                    // relocation is rewritten to the plain pc-relative form,
                    // which the arithmetic in relocate() treats identically
                    // but which asks for no slot.  Only the two x86 targets
                    // have the pair.
                    if (s1->arch->r_plt32
                        && ((u32)type == s1->arch->r_plt32
                            || (u32)type == s1->arch->r_pcrel32)
                        && sym->st_shndx != SHN_UNDEF
                        && (ELFW(ST_VISIBILITY)(sym->st_other) != STV_DEFAULT
                            || ELFW(ST_BIND)(sym->st_info) == STB_LOCAL
                            || s1->output_type & TLI_OUTPUT_EXE)) {
                        if (pass == 0)
                            rel->r_info = ELFW(R_INFO)(sym_index,
                                                       s1->arch->r_pcrel32);

                        continue;
                    }

                    reloc_type = code_reloc(s1, type);
                    if (reloc_type == -1) {
                        tli_error_noabort("Unknown relocation type: %d", type);
                        continue;
                    }

                    // code_reloc() says whether the relocation is in
                    // instructions, which is what decides between a stub to
                    // jump to and a slot to load an address out of.
                    want_plt = reloc_type != 0;
                }

                if (want_plt != (pass == 0))
                    continue;

                // An initial-exec thread-local reference wants a slot like
                // any other, but holding an offset from the thread pointer
                // rather than an address, so the relocation that fills it is
                // the architecture's own rather than a plain data one.
                tls_reloc = got_tls_reloc(s1, type);
                reloc_type = tls_reloc ? (u32)tls_reloc
                           : want_plt ? s1->arch->r_jmp_slot : s1->arch->r_glob_dat;

                if (!s1->got)
                    got_sym = build_got(s1);

                // BUILD_GOT_ONLY relocations are relative to the table and
                // want it to exist, but address no entry in it.
                if (gotplt_entry == BUILD_GOT_ONLY)
                    continue;

                attr = put_got_entry(s1, reloc_type, sym_index,
                                     tls_reloc ? got_tls_words(s1, type) : 0);
                if ((u32)reloc_type == s1->arch->r_jmp_slot)
                    rel->r_info = ELFW(R_INFO)(attr->plt_sym, type);
            }
        }
    }

    // The relocations of .plt patch the GOT, so sh_info has to say so.
    if (s1->plt && s1->plt->reloc)
        s1->plt->reloc->sh_info = s1->got->sh_num;

    if (got_sym)
        ((ElfW(Sym) *)symtab_section->data)[got_sym].st_size
            = s1->got->data_offset;
}

/// ================================================ linker-defined symbols

/// Define a global symbol at an offset in a section, and return its index.
///
/// The section may be null, in which case the symbol is absolute and offs is
/// its value; a null name as well makes it an anonymous local, which is what
/// a relocation needs when it wants to name a place rather than a thing.  An
/// offset of -1 means the end of the section, which is how the __stop_ and
/// _end style symbols are spelled.
ST_FUNC i32 set_global_sym(tli_State *s1, const char *name, tli_Section *sec, addr_t offs)
{
    i32 shn = offs || !name ? SHN_ABS : SHN_UNDEF, xshn = 0;

    if (sec)
        tli_shndx_of(sec, &shn, &xshn);

    if (sec && offs == (addr_t)-1)
        offs = sec->data_offset;

    return set_elf_sym_x(symtab_section, offs, 0,
                         ELFW(ST_INFO)(name ? STB_GLOBAL : STB_LOCAL,
                                       STT_NOTYPE), 0, shn, xshn, name);
}

/// Bracket one of the constructor arrays with the __init_array_start style
/// symbols the C runtime walks between.  When the array is absent both ends
/// are put at the start of .text, so that the runtime sees an empty range
/// rather than an unresolved reference.
static void add_init_array_defines(tli_State *s1, const char *section_name)
{
    tli_Section *s = tli_find_section_by_name(s1, section_name);
    addr_t end_offset;
    char buf[1024];

    if (!s || !(s->sh_flags & SHF_ALLOC)) {
        s = text_section;
        end_offset = 0;
    } else {
        end_offset = s->data_offset;
    }

    // The names drop the section's leading dot: .init_array is bracketed by
    // __init_array_start and __init_array_end.
    snprintf(buf, sizeof(buf), "__%s_start", section_name + 1);
    set_global_sym(s1, buf, s, 0);
    snprintf(buf, sizeof(buf), "__%s_end", section_name + 1);
    set_global_sym(s1, buf, s, end_offset);
}

/// Append a pointer to symbol c to .init_array or .fini_array, creating the
/// section if this is the first entry.  The pointer is a relocation rather
/// than a value because the symbol has no address yet.
ST_FUNC void add_array(tli_State *s1, const char *sec, i32 c)
{
    tli_Section *s = find_section(s1, sec);

    s->sh_flags = shf_RELRO;
    s->sh_type = sec[1] == 'i' ? SHT_INIT_ARRAY : SHT_FINI_ARRAY;
    put_elf_reloc(s1->symtab, s, s->data_offset, s1->arch->r_data_ptr, c);
    section_ptr_add(s, PTR_SIZE);
}

#ifdef TLI_BCHECK
/// The bounds checker's table is terminated by a null entry.
ST_FUNC void tli_add_bcheck(tli_State *s1)
{
    if (s1->do_bounds_check)
        section_ptr_add(bounds_section, sizeof(addr_t));
}
#endif

#ifdef TLI_BACKTRACE
/// Append one pointer field of the rt_context being built in .data, holding
/// the address of offs bytes into s.  An offset of -1 means the end of s, and
/// a null s means the null pointer.  The value is a relocation, since none of
/// these sections has an address yet.
static void put_ptr(tli_State *s1, tli_Section *s, i32 offs)
{
    i32 c = set_global_sym(s1, NULL, s, offs);

    put_elf_reloc(s1->symtab, data_section, data_section->data_offset,
                  s1->arch->r_data_ptr, c);
    section_ptr_add(data_section, PTR_SIZE);
}

/// Build a struct rt_context into .data and export it as __rt_info.
///
/// The layout is the one declared in tl_run.c: ten pointers followed by two
/// ints.  It is written field by field here, and the fields the linker has
/// nothing to say about are left as the zeros section_realloc() guarantees, so
/// the two must be kept in step.  In order: the three line-table pointers,
/// three symbol-table pointers the loader of the image fills in, the base
/// address of the program, the bounds table, the top frame, the next block in
/// the chain, and then the two ints.
ST_FUNC void tli_add_btstub(tli_State *s1)
{
    tli_Section *s = data_section;
    const char *__rt_info = &"___rt_info"[!s1->leading_underscore];
    i32 o, n, *p;

    section_add(s, 0, PTR_SIZE);
    o = s->data_offset;

    if (s1->dwarf) {
        put_ptr(s1, dwarf_line_section, 0);
        put_ptr(s1, dwarf_line_section, -1);
        put_ptr(s1, s1->dwarf >= 5 ? dwarf_line_str_section
                                   : dwarf_str_section, 0);
    } else {
        // Nothing in this tree fills the stabs sections, so these are very
        // often all null.  put_ptr() copes with a null section; reaching
        // through one for its string table does not, which is why -bt used to
        // take the linker down before it wrote anything.
        put_ptr(s1, stab_section, 0);
        put_ptr(s1, stab_section, -1);
        put_ptr(s1, stab_section ? stab_section->link : NULL, 0);
    }

    // esym_start, esym_end, elf_str: filled in by whoever maps the image.
    section_ptr_add(s, 3 * PTR_SIZE);

    if (s1->output_type == TLI_OUTPUT_MEMORY && 0 == s1->dwarf) {
        // Running in this process, stabs line numbers are relative to where
        // the text ended up.
        put_ptr(s1, text_section, 0);
    } else {
        put_ptr(s1, NULL, 0);

        // A Mach-O executable starts one page-zero above nothing, and stabs
        // addresses are recorded from there.
        if (s1->fmt == TLI_FMT_MACHO && s1->dwarf == 0
            && s1->output_type == TLI_OUTPUT_EXE)
            write64le(s->data + s->data_offset - PTR_SIZE,
                      (u64)1 << 32);
    }

    // bounds_start, top_func, next
    n = 3 * PTR_SIZE;
#ifdef TLI_BCHECK
    if (s1->do_bounds_check) {
        put_ptr(s1, bounds_section, 0);
        n -= PTR_SIZE;
    }
#endif
    section_ptr_add(s, n);

    // The two ints that close the block, then the symbol naming the whole
    // of it.
    p = section_ptr_add(s, 2 * sizeof(i32));
    p[0] = s1->rt_num_callers;
    p[1] = s1->dwarf;

    set_global_sym(s1, __rt_info, s, o);
    if (s1->output_type == TLI_OUTPUT_MEMORY)
        return;

    // For an image on disk, TinyCC compiled a small constructor here that
    // handed __rt_info to __bt_init() at startup.  tinylinker has no
    // compiler, so the caller has to supply that constructor itself; the
    // block is still emitted and exported so it can be found.
    tli_warning("standalone backtrace support needs a startup constructor "
                "calling __bt_init(__rt_info, ...); only the rt_context was "
                "emitted");
}
#endif  // def TLI_BACKTRACE

/// The C runtime's startup and shutdown objects, which have to bracket the
/// user's objects: crt1 holds _start, crti and crtn hold the halves of the
/// prologue and epilogue of _init and _fini, and crtbegin and crtend hold the
/// ends of the constructor lists.  Which of them exist and what they are
/// called is a matter for each system's C library.
ST_FUNC void tli_elf_add_crtbegin(tli_State *s1)
{
    i32 dll = s1->output_type == TLI_OUTPUT_DLL;

    // OpenBSD has no crti.o at all: the prologue halves live in crtbegin.o.
    if (s1->target_os == TLI_OS_OPENBSD) {
        if (!dll)
            tli_add_crt(s1, "crt0.o");

        tli_add_crt(s1, dll ? "crtbeginS.o" : "crtbegin.o");
        return;
    }

    // Everything else opens with the entry object and crti.o, differing only
    // in what that entry object is called.
    if (!dll)
        tli_add_crt(s1, s1->target_os == TLI_OS_NETBSD ? "crt0.o" : "crt1.o");

    tli_add_crt(s1, "crti.o");

    // The BSDs also ship the constructor-list ends as separate objects, which
    // glibc folds into crti.o instead.
    if (s1->target_os == TLI_OS_FREEBSD || s1->target_os == TLI_OS_NETBSD) {
        if (s1->static_link)
            tli_add_crt(s1, "crtbeginT.o");
        else
            tli_add_crt(s1, dll ? "crtbeginS.o" : "crtbegin.o");
    }
}

/// The other end of the bracket: crtend and crtn, which close what
/// tli_elf_add_crtbegin() opened, and so must follow the user's objects.
ST_FUNC void tli_elf_add_crtend(tli_State *s1)
{
    i32 dll = s1->output_type == TLI_OUTPUT_DLL;

    // The mirror of tli_elf_add_crtbegin(): whoever opened with crtbegin
    // closes with crtend, and whoever opened with crti.o closes with crtn.o.
    if (TLI_OS_IS_BSD(s1))
        tli_add_crt(s1, dll ? "crtendS.o" : "crtend.o");

    if (s1->target_os != TLI_OS_OPENBSD)
        tli_add_crt(s1, "crtn.o");
}

/// Add the libraries an image implicitly depends on.  This runs after the
/// command line has been consumed, so that anything it pulls in is resolved
/// against what the user asked for first.
ST_FUNC void tli_add_runtime(tli_State *s1)
{
    // Anything added below is an ordinary file again, whatever the last -x
    // on the command line said.
    s1->filetype = 0;

#ifdef TLI_BCHECK
    tli_add_bcheck(s1);
#endif
    tli_add_pragma_libs(s1);

    if (s1->nostdlib)
        return;

    {
        i32 lpthread = 0;

#ifdef TLI_BCHECK
        if (s1->do_bounds_check && s1->output_type != TLI_OUTPUT_DLL) {
            tli_add_support(s1, "bcheck.o", 0);

            // OpenBSD and NetBSD keep dlopen() in libc rather than a libdl.
            if (s1->target_os != TLI_OS_OPENBSD
                && s1->target_os != TLI_OS_NETBSD)
                tli_add_library(s1, "dl");

            lpthread = 1;
        }
#endif
#ifdef TLI_BACKTRACE
        if (s1->do_backtrace) {
            if (s1->output_type & TLI_OUTPUT_EXE)
                tli_add_support(s1, "bt-exe.o", 0);

            if (s1->output_type != TLI_OUTPUT_DLL)
                tli_add_support(s1, "bt-log.o", 0);

            tli_add_btstub(s1);
            lpthread = 1;
        }
#endif
        if (lpthread)
            tli_add_library(s1, "pthread");

        // libc last of the real libraries, so that anything above can
        // override what it defines.
        tli_add_library(s1, "c");
#ifdef TLI_LIBGCC
        if (!s1->static_link) {
            if (TLI_LIBGCC[0] == '/')
                tli_add_file(s1, TLI_LIBGCC);
            else
                tli_add_dll(s1, TLI_LIBGCC, AFF_PRINT_ERROR);
        }
#endif

        // FreeBSD/arm keeps the unwinder out of libc.
        if (s1->arch->em == EM_ARM && s1->target_os == TLI_OS_FREEBSD)
            tli_add_library(s1, "gcc_s");

        if (TLI_RUNTIME_LIB[0])
            tli_add_support(s1, TLI_RUNTIME_LIB, 1);
        if (s1->fmt != TLI_FMT_MACHO && s1->output_type != TLI_OUTPUT_MEMORY)
            tli_elf_add_crtend(s1);
    }
}

/// Define one of _etext, _edata or _end at the end of a section.
///
/// These are conventions rather than requirements, which is why the rules are
/// fiddly.  f says what to do:
///
///   0  define it, and complain if something else already has;
///   1  define it only if something refers to it, which is how the spellings
///      without a leading underscore are handled: etext and friends are old
///      enough that a program may well have a variable by that name;
///   2  and above, update the value of one we defined earlier, because .bss
///      has grown since.
///
/// Only a symbol this function created is updated, which is what the
/// linker_sym attribute records.
static void set_linker_sym(tli_State *s1, const char *name, tli_Section *sec, i32 f)
{
    ElfW(Sym) *sym, *esym;
    i32 sym_index, esym_index, defined;

    sym_index = find_elf_sym(symtab_section, name);
    sym = (ElfW(Sym) *)symtab_section->data + sym_index;
    esym_index = find_elf_sym(s1->dynsymtab_section, name);
    esym = (ElfW(Sym) *)s1->dynsymtab_section->data + esym_index;

    // A shared library defining it counts, but only if it has a size: a
    // zero-sized definition is the library's own end marker, not a variable
    // the program can be expected to share.
    defined = sym->st_shndx != SHN_UNDEF
              || (esym->st_shndx != SHN_UNDEF && esym->st_size);

    // f decides between defining unconditionally, defining only when
    // something refers to the name, and updating a value we set earlier.
    switch (f) {
    case 1:
        if (!(sym_index || esym_index) || defined)
            break;

        // It is referenced and undefined, so define it.
        // fall through
    case 0:
        if (defined) {
            tli_warning("linker symbol '%s' already defined", name);
            break;
        }
        sym_index = set_global_sym(s1, name, sec, -1);
        get_sym_attr(s1, sym_index, 1)->linker_sym = 1;
        break;
    default:
        if (get_sym_attr(s1, sym_index, 0)->linker_sym)
            sym->st_value = sec->data_offset;
    }

    if (s1->fmt == TLI_FMT_ELF && name[0] == '_')
        set_linker_sym(s1, name + 1, sec, f + 1);
}

/// Define the symbols a linker is expected to provide.  This has to run after
/// the sections are full, since most of them are section ends.
static void tli_add_linker_symbols(tli_State *s1)
{
    char buf[1024];
    i32 i;

    // The three ends every Unix program may refer to.
    set_linker_sym(s1, "_etext", text_section, 0);
    set_linker_sym(s1, "_edata", data_section, 0);
    set_linker_sym(s1, "_end", bss_section, 0);

    // OpenBSD's crt0.o refers to __executable_start, and nothing else defines
    // it there.
    if (s1->target_os == TLI_OS_OPENBSD)
        set_global_sym(s1, "__executable_start", NULL, s1->arch->start_addr);

    // The RISC-V psABI puts gp in the middle of the small data area so that
    // one signed 12-bit displacement covers all of it.  There is no .sdata
    // here, so .data has to stand in.
    if (s1->arch->em == EM_RISCV)
        set_global_sym(s1, "__global_pointer$", data_section, 0x800);

    add_init_array_defines(s1, ".preinit_array");
    add_init_array_defines(s1, ".init_array");
    add_init_array_defines(s1, ".fini_array");

    // __start_<name> and __stop_<name> for every allocated section whose name
    // is spellable as a C identifier, which is how a program declares a
    // section of its own and then walks it.
    for (i = 1; i < s1->nb_sections; i++) {
        tli_Section *s = s1->sections[i];
        const char *p;
        i32 j;

        if (!(s->sh_flags & SHF_ALLOC))
            continue;

        if (s->sh_type != SHT_PROGBITS && s->sh_type != SHT_NOBITS
            && s->sh_type != SHT_STRTAB)
            continue;

        p = s->name + (s->name[0] == '.');
        for (j = 0; p[j]; j++)
            if (!isid(p[j]) && !isnum(p[j]))
                break;

        if (p[j])
            continue;

        snprintf(buf, sizeof(buf), "__start_%s", p);
        set_global_sym(s1, buf, s, 0);
        snprintf(buf, sizeof(buf), "__stop_%s", p);
        set_global_sym(s1, buf, s, -1);
    }
}

/// Give every common symbol somewhere to live.
///
/// A common symbol is a tentative definition: several objects may declare one
/// and the linker is expected to allocate a single object big enough for the
/// largest.  set_elf_sym() has already picked that one; all that is left is to
/// place it in .bss, where st_value stops being the alignment the symbol
/// wanted and becomes its offset.
ST_FUNC void resolve_common_syms(tli_State *s1)
{
    ElfW(Sym) *sym;

    for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
        if (sym->st_shndx == SHN_COMMON && sym->st_size) {
            sym->st_value = section_add(bss_section, sym->st_size,
                                        sym->st_value);
            sym->st_shndx = bss_section->sh_num;
        }
    }

    // .bss has its final size now, which is what _end wants.  A shared
    // library defines none of these: each library would carry its own _end,
    // and the executable's is the one the program means.
    if (s1->output_type != TLI_OUTPUT_DLL)
        tli_add_linker_symbols(s1);
}

/// ============================================================= lifecycle

/// Create everything a link starts with: the reserved section zero, the four
/// sections every image has, the two symbol tables, and the interpreter path
/// an image that a loader will read needs.
ST_FUNC void tli_elf_new(tli_State *s)
{
    tli_State *s1 = s;

    // tli_Section number zero is reserved and its header is all zeros, so the
    // table starts with a hole rather than a section.
    dynarray_add(&s->sections, &s->nb_sections, NULL);

    // The four sections every image has, created in the order they are
    // conventionally laid out in.  Their numbers are relied on elsewhere, so
    // this order is part of the interface.
    text_section = new_section(s, ".text", SHT_PROGBITS,
                               SHF_ALLOC | SHF_EXECINSTR);
    data_section = new_section(s, ".data", SHT_PROGBITS,
                               SHF_ALLOC | SHF_WRITE);
    rodata_section = new_section(s, rdata, SHT_PROGBITS, shf_RELRO);
    bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);

    // Not a real section: it stands in for SHN_COMMON so that a common
    // symbol has something to point at until resolve_common_syms() moves it
    // into .bss.
    common_section = new_section(s, ".common", SHT_NOBITS, SHF_PRIVATE);
    common_section->sh_num = SHN_COMMON;

    symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
                                ".strtab", ".hashtab", SHF_PRIVATE);

    // Everything the shared libraries on the command line export, merged
    // into one table.  It is only ever consulted, never written.
    s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB,
                                      SHF_PRIVATE | SHF_DYNSYM,
                                      ".dynstrtab", ".dynhashtab",
                                      SHF_PRIVATE);

    // Allocate the attributes of the reserved symbol, so that the "past the
    // end" case in get_sym_attr() has something blank to return.
    get_sym_attr(s, 0, 1);

#ifdef TLI_BCHECK
    if (s->do_bounds_check) {
        bounds_section = new_section(s, ".bounds", SHT_PROGBITS, shf_RELRO);
        lbounds_section = new_section(s, ".lbounds", SHT_PROGBITS, shf_RELRO);
    }
#endif

    // Referencing the entry point up front is what makes an archive member
    // defining it get pulled in.
    if (s->fmt == TLI_FMT_PE && s->elf_entryname)
        set_global_sym(s, s->elf_entryname, NULL, 0);

    if (s->fmt == TLI_FMT_ELF && NULL == s->elfint
        && s1->output_type != TLI_OUTPUT_OBJ) {
#ifdef TLI_ELFINTERP
        const char *p = TLI_ELFINTERP;
#else
        const char *p = s->arch->elf_interp;

        // An ARM image passing floats in VFP registers needs the loader
        // built to do the same.
        if (s->arch->em == EM_ARM && s->float_abi == ARM_HARD_FLOAT)
            p = "/lib/ld-linux-armhf.so.3";
#endif

#if defined TLI_HOST_RUN && defined TARGETOS_BSD
        // the BSDs let the loader be overridden per process
        if (s1->native) { const char *e = getenv("LD_SO"); if (e) p = e; }
#endif
        s->elfint = tli_strdup(p);
    }
}

/// Release everything tli_elf_new() and the link put on the state.  The
/// sections' contents go, and so do the version tables and symbol
/// attributes; the state itself is the caller's to free.
ST_FUNC void tli_elf_delete(tli_State *s1)
{
    i32 i;

    for (i = 0; i < nb_sym_versions; i++) {
        tli_free(sym_versions[i].version);
        tli_free(sym_versions[i].lib);
    }
    tli_free(sym_versions);
    tli_free(sym_to_version);

    // Both section lists: the ones destined for the output, and the ones
    // that were only ever bookkeeping.
    for (i = 1; i < s1->nb_sections; i++)
        free_section(s1->sections[i]);

    dynarray_reset(&s1->sections, &s1->nb_sections);

    for (i = 0; i < s1->nb_priv_sections; i++)
        free_section(s1->priv_sections[i]);

    dynarray_reset(&s1->priv_sections, &s1->nb_priv_sections);

    tli_free(s1->sec_by_name);
    s1->sec_by_name = NULL;
    s1->sec_by_name_size = 0;

    tli_free(s1->sym_attrs);

    // tl_run.c reads the symbol table when reporting a fault, so it has to be
    // able to tell that there is no longer one to read.
    symtab_section = NULL;
}

/// =========================================================================
///
/// Everything below belongs to an ELF output that a dynamic loader will read.
/// A PE or Mach-O link, which uses this file only for relocatable objects,
/// needs none of it: those formats carry their own notion of imports and
/// exports, handled in tl_pe_out.c and tl_macho_out.c.
///
/// =========================================================================

/// ================================================== symbol versioning

/// Write the version tables of the output.
///
/// Two sections are involved.  .gnu.version is one halfword per dynamic
/// symbol saying which version it belongs to; index 1 is the reserved
/// *global* entry, meaning "no particular version".  .gnu.version_r lists the
/// versions the image needs from each library it depends on, as one Verneed
/// record per library with a Vernaux per version chained after it.
///
/// Only the versions that some symbol actually names are emitted, so
/// out_index is assigned lazily during the first pass and read back during the
/// second.  A version that has been written is marked with -2, which keeps it
/// from being written twice when two of its symbols share a library.
static void version_add(tli_State *s1)
{
    tli_Section *symtab = s1->dynsym;
    ElfW(Half) *versym;
    ElfW(Verneed) *vn = NULL;
    i32 nb_versions = 2, nb_entries = 0;
    i32 i, end_sym;

    if (0 == nb_sym_versions)
        return;

    versym_section = new_section(s1, ".gnu.version", SHT_GNU_versym, SHF_ALLOC);
    versym_section->sh_entsize = sizeof(ElfW(Half));
    versym_section->link = symtab;

    end_sym = symtab->data_offset / sizeof(ElfSym);
    versym = section_ptr_add(versym_section, end_sym * sizeof(ElfW(Half)));
    for (i = 1; i < end_sym; i++) {
        ElfW(Sym) *sym = (ElfW(Sym) *)symtab->data + i;
        const char *name = (char *)symtab->link->data + sym->st_name;
        i32 dllindex = find_elf_sym(s1->dynsymtab_section, name);
        i32 verndx = (dllindex && dllindex < nb_sym_to_version)
                     ? sym_to_version[dllindex] : -1;

        // A definition in an executable is left unversioned.  Android's
        // loader refuses an image whose defined symbols carry a version above
        // 1, and a version on something like signal() in an executable
        // defeats the bounds checker's interposition of it.
        if (verndx >= 0
            && (sym->st_shndx == SHN_UNDEF
                || (s1->output_type & TLI_OUTPUT_EXE))) {
            if (!sym_versions[verndx].out_index)
                sym_versions[verndx].out_index = nb_versions++;

            versym[i] = sym_versions[verndx].out_index;
        } else {
            versym[i] = 1;
        }
    }

    // An empty .gnu.version_r is worse than none: some loaders read it
    // regardless of DT_VERNEEDNUM and of its size.
    if (nb_versions > 2) {
        verneed_section = new_section(s1, ".gnu.version_r",
                                      SHT_GNU_verneed, SHF_ALLOC);
        verneed_section->link = s1->dynsym->link;

        for (i = nb_sym_versions; i-- > 0;) {
            struct tli_SymVersion *sv = &sym_versions[i];
            ElfW(Vernaux) *vna = NULL;
            i32 n_same_libs = 0, prev;
            usize vnofs;

            if (sv->out_index < 1)
                continue;

            // Needing a version from a library implies depending on it.
            // ld-linux.so.2 is the exception: naming it in DT_NEEDED makes
            // older i386 loaders fail an assertion in their own allocator.
            if (strcmp(sv->lib, "ld-linux.so.2"))
                tli_add_dllref(s1, sv->lib, 0);

            // One Verneed record naming the library, with the offset of
            // its first Vernaux immediately after it.
            vnofs = section_add(verneed_section, sizeof(*vn), 1);
            vn = (ElfW(Verneed) *)(verneed_section->data + vnofs);
            vn->vn_version = 1;
            vn->vn_file = put_elf_str(verneed_section->link, sv->lib);
            vn->vn_aux = sizeof(*vn);

            // Then a Vernaux for each version of that library, walking the
            // chain prev_same_lib threads through them.
            do {
                prev = sv->prev_same_lib;
                if (sv->out_index > 0) {
                    vna = section_ptr_add(verneed_section, sizeof(*vna));
                    vna->vna_hash =
                        elf_hash((const u8 *)sv->version);
                    vna->vna_flags = 0;
                    vna->vna_other = sv->out_index;
                    vna->vna_name = put_elf_str(verneed_section->link,
                                                sv->version);
                    vna->vna_next = sizeof(*vna);
                    sv->out_index = -2;  // written
                    n_same_libs++;
                }
                if (prev >= 0)
                    sv = &sym_versions[prev];
            } while (prev >= 0);
            vna->vna_next = 0;

            // section_ptr_add() may have moved the section under us.
            vn = (ElfW(Verneed) *)(verneed_section->data + vnofs);
            vn->vn_cnt = n_same_libs;
            vn->vn_next = sizeof(*vn) + n_same_libs * sizeof(*vna);
            nb_entries++;
        }
        if (vn)
            vn->vn_next = 0;

        verneed_section->sh_info = nb_entries;
    }
    dt_verneednum = nb_entries;
}

/// ==================================================== the GNU hash table
///
/// The table the GNU loaders prefer over the gABI one.  Its layout is four
/// words of header (nbuckets, symoffset, bloom_size, bloom_shift), a Bloom
/// filter of bloom_size words, nbuckets bucket heads, and one chain word per
/// defined symbol.
///
/// Two things make it faster than the gABI table.  The Bloom filter answers
/// "definitely absent" for most misses without touching the buckets at all,
/// using two bits per name taken from the hash at two different shifts.  And
/// the symbols themselves are sorted by bucket, so a bucket is a contiguous
/// run: its head is a symbol index, the chain words hold each symbol's hash
/// with the low bit cleared, and the last entry of a run has that bit set.
/// Undefined symbols cannot be looked up and are excluded, which is what
/// symoffset records: the index of the first symbol the table describes.

/// Bits in a Bloom filter word, which is one machine word of the target.
#define ELFCLASS_BITS (PTR_SIZE * 8)

/// Reserve the section with the right shape for the symbols dynsym holds now.
/// It is filled in by update_gnu_hash() once the addresses are final, and the
/// symbol table is permuted then to match, so nothing may be added to dynsym
/// between the two calls.
static tli_Section *create_gnu_hash(tli_State *s1)
{
    tli_Section *dynsym = s1->dynsym;
    tli_Section *gnu_hash;
    Elf32_Word *hdr;
    i32 nb_syms, ndef, i;
    i32 nbuckets, bloom_size, bloom_shift;

    gnu_hash = new_section(s1, ".gnu.hash", SHT_GNU_HASH, SHF_ALLOC);
    gnu_hash->link = dynsym->hash->link;

    // Only the defined symbols are described by the table, so they are what
    // its size is derived from.
    nb_syms = dynsym->data_offset / sizeof(ElfW(Sym));
    ndef = 0;
    for (i = 0; i < nb_syms; i++)
        ndef += ((ElfW(Sym) *)dynsym->data)[i].st_shndx != SHN_UNDEF;

    // Four symbols to a bucket, and enough Bloom words to hold a few bits
    // per name; both are the ratios the GNU tools settled on.
    nbuckets = ndef / 4 + 1;
    bloom_shift = PTR_SIZE == 8 ? 6 : 5;
    bloom_size = 1;  // a power of two, so the index is a mask
    while (ndef >= bloom_size * (1 << (bloom_shift - 3)))
        bloom_size *= 2;

    hdr = section_ptr_add(gnu_hash, 4 * 4 + PTR_SIZE * bloom_size
                                    + nbuckets * 4 + ndef * 4);
    hdr[0] = nbuckets;
    hdr[1] = nb_syms - ndef;  // symoffset
    hdr[2] = bloom_size;
    hdr[3] = bloom_shift;
    return gnu_hash;
}

/// The djb2 hash, which is what the GNU table is defined in terms of.
static Elf32_Word elf_gnu_hash(const u8 *name)
{
    Elf32_Word h = 5381;
    u8 c;

    while ((c = *name++))
        h = h * 33 + c;

    return h;
}

/// Sort .dynsym into the order the GNU hash table requires and fill the table
/// in: undefined symbols first, in the order they were added, then the defined
/// ones grouped by bucket.  Relocations against .dynsym and the version table
/// beside it are permuted to match, and the gABI hash is rebuilt because its
/// chains index the table that has just moved.
static void update_gnu_hash(tli_State *s1, tli_Section *gnu_hash)
{
    tli_Section *dynsym = s1->dynsym;
    u8 *strtab = dynsym->link->data;
    ElfW(Sym) *syms = (ElfW(Sym) *)dynsym->data;
    ElfW(Sym) *sorted;
    Elf32_Word *hdr, *buckets, *chain, *hash;
    addr_t *bloom;
    i32 *old_to_new_syms, *bucket_pos;
    i32 nb_syms, nbuckets, bloom_size, bloom_shift;
    i32 i, nb_undef, ndef;

    nb_syms = dynsym->data_offset / sizeof(ElfW(Sym));
    sorted = tli_malloc(nb_syms * sizeof(ElfW(Sym)));
    old_to_new_syms = tli_malloc(nb_syms * sizeof(i32));
    hash = tli_malloc(nb_syms * sizeof(Elf32_Word));

    // Read the shape create_gnu_hash() chose back out of the header, and
    // point at each of the three arrays that follow it.
    hdr = (Elf32_Word *)gnu_hash->data;
    nbuckets = hdr[0];
    bloom_size = hdr[2];
    bloom_shift = hdr[3];
    bloom = (addr_t *)(void *)&hdr[4];
    buckets = (Elf32_Word *)(void *)&bloom[bloom_size];
    chain = &buckets[nbuckets];

    // The undefined symbols keep their order and go first; everything else
    // is hashed now so the bucket it lands in is known.
    nb_undef = 0;
    for (i = 0; i < nb_syms; i++) {
        if (syms[i].st_shndx == SHN_UNDEF) {
            old_to_new_syms[i] = nb_undef;
            sorted[nb_undef++] = syms[i];
        } else {
            hash[i] = elf_gnu_hash(strtab + syms[i].st_name);
        }
    }
    ndef = nb_syms - nb_undef;

    if (gnu_hash->data_offset != (u64)(4 * 4
                                                 + PTR_SIZE * bloom_size
                                                 + nbuckets * 4 + ndef * 4))
        tli_error_noabort("gnu_hash size incorrect");

    // Count what falls in each bucket, then turn the counts into the place
    // each bucket starts, which is all a stable grouping needs.
    bucket_pos = tli_mallocz((nbuckets + 1) * sizeof(i32));
    for (i = 0; i < nb_syms; i++)
        if (syms[i].st_shndx != SHN_UNDEF)
            bucket_pos[hash[i] % nbuckets + 1]++;

    for (i = 0; i < nbuckets; i++) {
        // A bucket head of zero means empty: index zero is the reserved
        // symbol and is never in a bucket.
        buckets[i] = bucket_pos[i + 1] ? nb_undef + bucket_pos[i] : 0;
        bucket_pos[i + 1] += bucket_pos[i];
    }

    for (i = 0; i < nb_syms; i++) {
        Elf32_Word h;
        i32 at;

        if (syms[i].st_shndx == SHN_UNDEF)
            continue;

        h = hash[i];
        at = bucket_pos[h % nbuckets]++;
        old_to_new_syms[i] = nb_undef + at;
        sorted[nb_undef + at] = syms[i];

        // The low bit is the end-of-bucket marker, so it is not part of the
        // hash and is cleared here and set below.
        chain[at] = h & ~1;
        bloom[(h / ELFCLASS_BITS) % bloom_size] |=
            (addr_t)1 << (h % ELFCLASS_BITS) |
            (addr_t)1 << ((h >> bloom_shift) % ELFCLASS_BITS);
    }

    // bucket_pos[b] has walked to the end of bucket b, which is where its
    // last chain word is.
    for (i = 0; i < nbuckets; i++)
        if (buckets[i])
            chain[bucket_pos[i] - 1] |= 1;

    // Install the permuted table, taking the companion with it.
    memcpy(dynsym->data, sorted, nb_syms * sizeof(ElfW(Sym)));
    permute_xindex(dynsym, old_to_new_syms, nb_syms);
    tli_free(sorted);
    tli_free(hash);
    tli_free(bucket_pos);

    update_relocs(s1, dynsym, old_to_new_syms, 0);

    // .gnu.version is indexed by dynamic symbol, so it moves with them.
    if (versym_section) {
        ElfW(Half) *versym = (ElfW(Half) *)versym_section->data;
        ElfW(Half) *newver = tli_malloc(nb_syms * sizeof(*newver));

        for (i = 0; i < nb_syms; i++)
            newver[old_to_new_syms[i]] = versym[i];

        memcpy(versym_section->data, newver, nb_syms * sizeof(*newver));
        tli_free(newver);
    }

    tli_free(old_to_new_syms);

    rebuild_hash(dynsym, ((Elf32_Word *)dynsym->hash->data)[0]);
}

/// ================================================ dynamic relocations

/// Whether a relocation stores an address, and so has to be redone by the
/// loader wherever the image ends up.
static i32 reloc_stores_address(tli_State *s1, i32 type)
{
    switch (s1->arch->em) {
    case EM_386:
        return type == R_386_32;
    case EM_X86_64:
        return type == R_X86_64_32 || type == R_X86_64_32S
            || type == R_X86_64_64;
    case EM_ARM:
        return type == R_ARM_ABS32 || type == R_ARM_TARGET1;
    case EM_AARCH64:
        return type == R_AARCH64_ABS32 || type == R_AARCH64_ABS64;
    case EM_RISCV:
        return type == R_RISCV_32 || type == R_RISCV_64;
    }
    return 0;
}

/// Whether a relocation stores the distance to a symbol.  A displacement
/// between two things in the same image survives being moved, so one is only
/// dynamic when the symbol it names could be interposed.
static i32 reloc_stores_displacement(tli_State *s1, i32 type)
{
    switch (s1->arch->em) {
    case EM_386:
        return type == R_386_PC32;
    case EM_X86_64:
        return type == R_X86_64_PC32;
    case EM_AARCH64:
        return type == R_AARCH64_PREL32;
    }
    return 0;
}

/// Count the relocations of sr that the output has to carry, so that space can
/// be reserved for them, and rewrite the ones that can be turned into
/// something cheaper on the way past.
static i32 prepare_dynamic_rel(tli_State *s1, tli_Section *sr)
{
    ElfW_Rel *rel;
    i32 count = 0;

    for_each_elem(sr, 0, rel, ElfW_Rel) {
        i32 sym_index = ELFW(R_SYM)(rel->r_info);
        i32 type = ELFW(R_TYPE)(rel->r_info);
        ElfW(Sym) *sym = (ElfW(Sym) *)symtab_section->data + sym_index;

        if (reloc_stores_address(s1, type)) {
            // On i386, a word holding the address of a symbol that is not in
            // the dynamic symbol table and is not defined anywhere resolves to
            // nothing plus the load address, which is what a relative
            // relocation says and costs no symbol lookup.
            if (s1->arch->em == EM_386 && type == R_386_32
                && !get_sym_attr(s1, sym_index, 0)->dyn_index
                && sym->st_shndx == SHN_UNDEF) {
                rel->r_info = ELFW(R_INFO)(sym_index, R_386_RELATIVE);
                continue;
            }

            count++;
            continue;
        }

        if (!reloc_stores_displacement(s1, type))
            continue;

        // A hidden symbol cannot be interposed, so the displacement to it is
        // fixed and must be resolved here.  PLT32 is the relocation that says
        // "resolve to the symbol's own address whatever else happens", even
        // in a shared object, so on the two x86 targets it is borrowed to say
        // exactly that.
        if (s1->arch->r_plt32 && sym->st_shndx != SHN_UNDEF
            && ELFW(ST_VISIBILITY)(sym->st_other) == STV_HIDDEN) {
            rel->r_info = ELFW(R_INFO)(sym_index, s1->arch->r_plt32);
            continue;
        }

        // Elsewhere only a shared object has to keep them, and only for
        // symbols the loader will be asked about.
        if (s1->output_type != TLI_OUTPUT_DLL)
            continue;

        if (get_sym_attr(s1, sym_index, 0)->dyn_index)
            count++;
    }
    return count;
}

/// ===================================================== filling the GOT

/// Write a symbol's final address into its GOT slot.
ST_FUNC void fill_got_entry(tli_State *s1, ElfW_Rel *rel)
{
    i32 sym_index = ELFW(R_SYM)(rel->r_info);
    ElfW(Sym) *sym = (ElfW(Sym) *)symtab_section->data + sym_index;
    u32 offset = get_sym_attr(s1, sym_index, 0)->got_offset;

    if (0 == offset)
        return;

    section_reserve(s1->got, offset + PTR_SIZE);
#if PTR_SIZE == 8
    write64le(s1->got->data + offset, sym->st_value);
#else
    write32le(s1->got->data + offset, sym->st_value);
#endif
}

/// Fill in the whole GOT.  A statically linked executable has no loader to do
/// it, so every slot a relocation reads has to hold its value before the image
/// is written.
///
/// The relocation types listed are the x86-64 ones that read the GOT; the
/// other architectures reach this with their own numbering, and a match is
/// then coincidental.  It costs nothing either way, because fill_got_entry()
/// does nothing unless the symbol has a slot, and writing a symbol's address
/// into its own slot is right for any relocation that led to one being
/// allocated.
ST_FUNC void fill_got(tli_State *s1)
{
    i32 i;

    // Every relocation table against .symtab, which is where a reference to
    // the GOT can still be found; the ones against .dynsym describe the
    // GOT's contents instead.
    for (i = 1; i < s1->nb_sections; i++) {
        tli_Section *s = s1->sections[i];
        ElfW_Rel *rel;

        if (s->sh_type != SHT_RELX || s->link != symtab_section)
            continue;

        for_each_elem(s, 0, rel, ElfW_Rel) {
            switch (ELFW(R_TYPE)(rel->r_info)) {
            case R_X86_64_GOT32:
            case R_X86_64_GOTPCREL:
            case R_X86_64_GOTPCRELX:
            case R_X86_64_REX_GOTPCRELX:
            case R_X86_64_PLT32:
                fill_got_entry(s1, rel);
                break;
            }
        }
    }
}

/// The second half of the trick put_got_entry() plays on local symbols: the
/// marker relocations it left in the GOT's own relocation table are turned
/// into the relative relocations they were always going to be, now that the
/// symbol has an address.
static void fill_local_got_entries(tli_State *s1)
{
    ElfW_Rel *rel;

    if (!s1->got->reloc)
        return;

    for_each_elem(s1->got->reloc, 0, rel, ElfW_Rel) {
        i32 sym_index;
        ElfW(Sym) *sym;
        u32 offset;

        if (ELFW(R_TYPE)(rel->r_info) != s1->arch->r_relative)
            continue;

        sym_index = ELFW(R_SYM)(rel->r_info);
        sym = (ElfW(Sym) *)symtab_section->data + sym_index;
        offset = get_sym_attr(s1, sym_index, 0)->got_offset;
        if (offset != rel->r_offset - s1->got->sh_addr)
            tli_error_noabort("fill_local_got_entries: huh?");

        rel->r_info = ELFW(R_INFO)(0, s1->arch->r_relative);
#if SHT_RELX == SHT_RELA
        rel->r_addend = sym->st_value;
#else
        // Every REL architecture here is also 32-bit and little-endian.
        write32le(s1->got->data + offset, sym->st_value);
#endif
    }
}

/// ================================================ dynamic symbol binding

/// Resolve what the executable's objects left undefined against what the
/// shared libraries export.
///
/// The loader would normally do this, but this linker binds references in the
/// image itself, which means each kind of symbol needs somewhere concrete to
/// point at.  A function gets a dynamic symbol of its own, and later a PLT
/// entry from build_got_entries().  A data object gets space in .bss and an
/// R_COPY relocation, which asks the loader to copy the library's initial
/// value into it at startup; that is the standard treatment, and it is why the
/// definition in the executable is the one everything must use.
static void bind_exe_dynsyms(tli_State *s1, i32 is_PIE)
{
    ElfW(Sym) *sym;

    for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
        const char *name;
        ElfW(Sym) *esym;
        i32 sym_index, type;

        // A .bss or common symbol is a candidate too: a tentative definition
        // here can be the real one in a library.
        if (sym->st_shndx != SHN_UNDEF
            && sym->st_shndx != SHN_COMMON
            && sym->st_shndx != bss_section->sh_num)
            continue;

        // What, if anything, the shared libraries have to say about it.
        name = (char *)symtab_section->link->data + sym->st_name;
        sym_index = find_elf_sym(s1->dynsymtab_section, name);
        esym = (ElfW(Sym) *)s1->dynsymtab_section->data + sym_index;

        if (!sym_index || esym->st_shndx == SHN_UNDEF) {
            if (sym->st_shndx != SHN_UNDEF)
                continue;

            // An undefined weak reference evaluates to zero, and _fp_hw is
            // part of the ARM ABI and expected to be absent.
            if (ELFW(ST_BIND)(sym->st_info) == STB_WEAK)
                continue;

            if (!strcmp(name, "_fp_hw"))
                continue;

            tli_error_noabort("unresolved reference to '%s'", name);
            continue;
        }

        // A position-independent executable is relocated as a whole, so its
        // references can be left for the loader.
        if (is_PIE)
            continue;

        type = ELFW(ST_TYPE)(esym->st_info);
        if (type == STT_FUNC || type == STT_GNU_IFUNC) {
            // Typed STT_FUNC even when the library says STT_GNU_IFUNC: after
            // lazy binding, a dlsym() of this name reads the value out of the
            // executable's dynamic symbol, and that has to be the address the
            // resolver chose rather than the address of the resolver.
            i32 dynindex = put_elf_sym(s1->dynsym, 0, esym->st_size,
                                       ELFW(ST_INFO)(STB_GLOBAL, STT_FUNC),
                                       0, 0, name);
            i32 index = sym - (ElfW(Sym) *)symtab_section->data;

            get_sym_attr(s1, index, 1)->dyn_index = dynindex;
        } else if (type == STT_OBJECT) {
            u64 offset;
            i32 index;

            if (sym->st_shndx == bss_section->sh_num) {
                // already given room by resolve_common_syms()
                offset = sym->st_value;
            } else {
                offset = (bss_section->data_offset + 16 - 1) & -16;
                bss_section->data_offset = offset + esym->st_size;
            }
            set_elf_sym(s1->symtab, offset, esym->st_size, esym->st_info, 0,
                        bss_section->sh_num, name);
            index = put_elf_sym(s1->dynsym, offset, esym->st_size,
                                esym->st_info, 0, bss_section->sh_num, name);

            // A weak definition in a library usually has a strong alias at the
            // same address, and a program may refer to either.  Both names
            // have to end up at the copy, so the alias is exported too.
            if (ELFW(ST_BIND)(esym->st_info) == STB_WEAK) {
                ElfW(Sym) *dynsym;

                for_each_elem(s1->dynsymtab_section, 1, dynsym, ElfW(Sym)) {
                    if (dynsym->st_value == esym->st_value
                        && ELFW(ST_BIND)(dynsym->st_info) == STB_GLOBAL) {
                        put_elf_sym(s1->dynsym, offset, dynsym->st_size,
                                    dynsym->st_info, 0, bss_section->sh_num,
                                    (char *)s1->dynsymtab_section->link->data
                                    + dynsym->st_name);
                        break;
                    }
                }
            }
            put_elf_reloc(s1->dynsym, bss_section, offset, s1->arch->r_copy, index);
        }
    }

    // The copies above grew .bss.
    set_linker_sym(s1, "_end", bss_section, 2);
}

/// Export what the libraries need from us.
///
/// The loader searches the executable before the libraries, so a library's
/// reference to a name the executable also defines has to resolve to the
/// executable's definition, and that only happens if the name is in the
/// executable's dynamic symbol table.  With -rdynamic everything defined is
/// exported, whether a library asked for it or not.
static void bind_libs_dynsyms(tli_State *s1)
{
    ElfW(Sym) *sym;

    for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
        const char *name = (char *)symtab_section->link->data + sym->st_name;
        i32 dynsym_index = find_elf_sym(s1->dynsymtab_section, name);

        if (sym->st_shndx != SHN_UNDEF) {
            i32 index = sym - (ElfW(Sym) *)symtab_section->data;

            if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL
                && (dynsym_index || s1->rdynamic))
                set_elf_sym_x(s1->dynsym, sym->st_value, sym->st_size,
                              sym->st_info, 0, sym->st_shndx,
                              tli_sym_xindex(symtab_section, index), name);
        } else if (dynsym_index) {
            ElfW(Sym) *esym =
                (ElfW(Sym) *)s1->dynsymtab_section->data + dynsym_index;

            // A library knowing the name does not mean it defines it.
            if (esym->st_shndx == SHN_UNDEF
                && ELFW(ST_BIND)(esym->st_info) != STB_WEAK)
                tli_warning("unresolved dynamic reference to '%s'", name);
        }
    }
}

/// Export everything a shared object defines, and record every reference it
/// makes, so that other objects and the executable can resolve against it and
/// it against them.  A library has no way of knowing who will load it, so
/// there is nothing to narrow this down by.
static void export_global_syms(tli_State *s1)
{
    ElfW(Sym) *sym;

    for_each_elem(symtab_section, 1, sym, ElfW(Sym)) {
        const char *name;
        i32 dynindex, index;

        if (ELFW(ST_BIND)(sym->st_info) == STB_LOCAL)
            continue;

        name = (char *)symtab_section->link->data + sym->st_name;
        index = sym - (ElfW(Sym) *)symtab_section->data;
        dynindex = set_elf_sym_x(s1->dynsym, sym->st_value, sym->st_size,
                                 sym->st_info, 0, sym->st_shndx,
                                 tli_sym_xindex(symtab_section, index), name);
        get_sym_attr(s1, index, 1)->dyn_index = dynindex;
    }
}


/// ---- src/core/tl_elf_out.c ----

/// tinylinker - laying an ELF image out and writing it
///
/// Everything between "the sections are filled in" and "the file is on disk":
/// deciding which sections the output carries, ordering them, assigning
/// addresses, building the program headers and the dynamic table, and writing
/// the result. The equivalent work for the other two formats lives in
/// tl_pe_out.c and tl_macho_out.c; tli_output_file at the end of this file is
/// the fork between the three.
///
/// Written from the System V gABI and the psABI supplements. Two of their
/// rules shape nearly all of it. The first is that a segment carries one set
/// of permissions over its whole extent, so sections have to be grouped by the
/// permissions they want before any address can be handed out. The second is
/// that the loader maps whole pages, and so requires p_offset and p_vaddr of a
/// PT_LOAD to be congruent modulo p_align: the layout below therefore advances
/// the file offset and the virtual address in lockstep, and when permissions
/// change it skips a whole page rather than a part of one.
///
/// Included by tl_core.c after tl_link.c, whose section and symbol tables this
/// reads.

/// Where the section headers go: straight after the program headers at the
/// front of the file, rounded up so the words in each header land aligned.
/// The layout and the writer have to agree on this, so both call it.
static u64 elf_sh_offset(i32 phnum)
{
    return (sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr)) + 3) & ~3UL;
}

/// Give each section the sh_size its header will claim, and convert the static
/// relocations of a shared object into dynamic ones. Returns the number that
/// landed in executable sections, which is what obliges us to emit DT_TEXTREL.
static i32 size_output_sections(tli_State *s1)
{
    i32 i, textrel = 0;

    // Until now a section has carried only data_offset, which is how much has
    // been written into it. sh_size is a separate field because the two
    // differ: an SHT_NOBITS section occupies no file space at all, and a
    // relocation section of a shared object shrinks when prepare_dynamic_rel
    // discards the entries the dynamic linker will never need.

    // A section left with sh_size 0 and without SHF_ALLOC gets no name in
    // alloc_sec_names and is discarded by reorder_sections. That is how the
    // scaffolding a link needs but a program does not - the symbol table, its
    // string table - stays out of an executable unless debugging was asked
    // for.
    for (i = 1; i < s1->nb_sections; i++) {
        tli_Section *s = s1->sections[i];

        if (s->sh_type == SHT_RELX && !(s->sh_flags & SHF_ALLOC)) {
            tli_Section *target;
            i32 count;

            // Only a shared object or a PIE carries relocations into the
            // output. An executable linked at a fixed address has had every
            // one of them applied already.
            if (!(s1->output_type & TLI_OUTPUT_DYN))
                continue;

            target = s1->sections[s->sh_info];
            if (!(target->sh_flags & SHF_ALLOC))
                continue;

            count = prepare_dynamic_rel(s1, s);
            if (count == 0)
                continue;

            // The dynamic linker reads these at load time, so they have to be
            // mapped, and they are now only as long as what survived.
            s->sh_flags |= SHF_ALLOC;
            s->sh_size = count * sizeof(ElfW_Rel);
            if (target->sh_flags & SHF_EXECINSTR)
                textrel += count;
        } else if ((s->sh_flags & SHF_ALLOC)
                   // The build attributes are never mapped, but they describe
                   // the image and are read off the file, so they have to be
                   // kept whether or not anything else unmapped is.
                   || (s1->arch->em == EM_ARM
                       && s->sh_type == SHT_ARM_ATTRIBUTES)
                   || (s1->arch->em == EM_RISCV
                       && s->sh_type == SHT_RISCV_ATTRIBUTES)
                   || s1->do_debug) {
            s->sh_size = s->data_offset;
        }
    }
    return textrel;
}

/// What one output file needs to remember while it is being laid out.
struct tli_OutLayout {
    tli_Section *dynamic;   /// .dynamic, or NULL when the link is static.
    tli_Section *dynstr;    /// The string table its tags index into.
    tli_Section *interp;    /// .interp, which only an executable carries.
    tli_Section *gnu_hash;  /// The DT_GNU_HASH table, built before the layout.

    /// Where the dynamic section stood before fill_dynamic first ran, so the
    /// second pass can rewind and refill it with the final addresses.
    u64 data_offset;

    /// The run of dynamic relocation sections, once merge_reloc_sections has
    /// closed the gaps in it: DT_REL/DT_RELA and their size.
    addr_t rel_addr;
    addr_t rel_size;

    ElfW(Phdr) *phdr;   /// The program headers, freed by elf_output_file.
    i32 phnum;          /// How many of them the output carries.
    i32 shnum;          /// tli_Section headers going out, the null one included.

    /// Index in phdr[] of each of the segments there is at most one of. Zero
    /// means absent, which is unambiguous because index zero always holds
    /// either PT_PHDR or the first PT_LOAD. Between order_sections and
    /// layout_sections these hold 1 as a bare "one is needed" marker.
    i32 ph_tls;
    i32 ph_relro;
    i32 ph_note;
    i32 ph_dynamic;
    i32 ph_eh_frame;

    /// The search table an unwinder starts from, when there is one.  It is
    /// built rather than merged, so the writer holds it here rather than
    /// looking it up by name twice.
    tli_Section *eh_frame_hdr;
};

/// -------------------------------------------------------------------------
/// ordering

/// The output is written in four bands, in this order. The band is the high
/// part of a section's sort key; a rank below 0x100 orders sections within it.
/// Nothing in the gABI dictates any of this - section order is entirely the
/// linker's business - but a loadable image has to keep like permissions
/// together, and putting the unmapped material after all of it means the file
/// can be truncated at the end of the image without touching anything the
/// loader reads.
#define BAND_LOAD_RO  0x100 /// Mapped, and never written to once running.
#define BAND_LOAD_RW  0x200 /// Mapped and writable.
#define BAND_UNMAPPED 0x700 /// In the file, but not part of the image.
#define BAND_DROPPED  0x900 /// Not written at all.

/// Ranks within a band. The values are spread out rather than consecutive so
/// that the relro group can be recognised by a mask: everything from
/// RANK_RELRO_FIRST to RANK_RELRO_LAST is written to while the dynamic linker
/// resolves relocations and is then made read-only again, so it must be
/// contiguous and must not share a page with anything that stays writable.
#define RANK_INTERP     0x00 /// First, so a short read of the file finds it.
#define RANK_NOTE       0x08
#define RANK_SYMTAB     0x10
#define RANK_STRTAB     0x11
#define RANK_HASH       0x12
#define RANK_VERSION    0x13
#define RANK_RELOC      0x20
#define RANK_PLT_RELOC  0x21 /// Apart from the rest: DT_JMPREL is its own run.
#define RANK_RELRO_FIRST 0x40
#define RANK_TDATA      0x40
#define RANK_TBSS       0x41
#define RANK_PREINIT    0x42
#define RANK_INIT       0x43
#define RANK_FINI       0x44
#define RANK_RELDATA    0x45 /// Data with relocations of its own to take.
#define RANK_DYNAMIC    0x46
#define RANK_GOT        0x47 /// Relro only because DT_FLAGS says BIND_NOW.
#define RANK_RELRO_LAST 0x4f
#define RANK_DATA       0x50
#define RANK_TEXT       0x60
#define RANK_BSS        0x70
#define RANK_SHSTRTAB   0xff /// Last, so e_shstrndx is the final section.

/// Per-section flags handed from order_sections to layout_sections. The low
/// bits are the SHF_ flags the segment inherits, so these two take values the
/// masked-off flags cannot.
#define SEG_OPENS_LOAD (1 << 4) /// A new PT_LOAD begins at this section.
#define SEG_IN_RELRO   (1 << 5) /// This section belongs to PT_GNU_RELRO.

/// Rank a section within its band, which is the low half of its sort key.
static i32 section_rank(tli_State *s1, tli_Section *s, i32 band, struct tli_OutLayout *d)
{
    if (s->sh_type == SHT_SYMTAB || s->sh_type == SHT_DYNSYM)
        return RANK_SYMTAB;

    if (s->sh_type == SHT_STRTAB && strcmp(s->name, ".stabstr")) {
        // alloc_sec_names creates .shstrtab last, so it holds the highest
        // section number, and it has to stay last: the writer sets e_shstrndx
        // to the index of the final section header rather than looking it up
        // by name.
        if (s->sh_num == s1->nb_sections - 1)
            return RANK_SHSTRTAB;

        return RANK_STRTAB;
    }

    if (s->sh_type == SHT_HASH || s->sh_type == SHT_GNU_HASH)
        return RANK_HASH;

    if (s->sh_type == SHT_GNU_verdef || s->sh_type == SHT_GNU_verneed
        || s->sh_type == SHT_GNU_versym)
        return RANK_VERSION;

    if (s->sh_type == SHT_RELX)
        return (s1->plt && s == s1->plt->reloc) ? RANK_PLT_RELOC : RANK_RELOC;

    if (s->sh_flags & SHF_EXECINSTR)
        return RANK_TEXT;

    if (s->sh_flags & SHF_TLS)
        return s->sh_type == SHT_NOBITS ? RANK_TBSS : RANK_TDATA;

    if (s->sh_type == SHT_PREINIT_ARRAY)
        return RANK_PREINIT;

    if (s->sh_type == SHT_INIT_ARRAY)
        return RANK_INIT;

    if (s->sh_type == SHT_FINI_ARRAY)
        return RANK_FINI;

    if (s->sh_type == SHT_DYNAMIC)
        return RANK_DYNAMIC;

    if (s == s1->got)
        return RANK_GOT;

    // Anything the dynamic linker has to write into belongs with the relro
    // group, so the page can be sealed once it has finished.
    if (s->reloc && (s->reloc->sh_flags & SHF_ALLOC) && band == BAND_LOAD_RO)
        return RANK_RELDATA;

    if (s->sh_type == SHT_NOTE) {
        d->ph_note = 1;
        return RANK_NOTE;
    }

    if (s->sh_type == SHT_NOBITS)
        return RANK_BSS;

    if (s == d->interp)
        return RANK_INTERP;

    return RANK_DATA;
}

/// Compare two section indices by the sort key computed for each. The keys
/// reach here through tl_sort's context argument rather than a global.
static i32 cmp_section_key(const void *a, const void *b, void *keys)
{
    const i32 *key = keys;

    return key[*(const i32 *)a] - key[*(const i32 *)b];
}

/// Decide the order sections go out in, and mark where the PT_LOAD segments
/// begin. order[] comes back holding section indices; seg[] holds, at the same
/// position, the flags layout_sections needs. Returns the PT_LOAD count.
static i32 order_sections(tli_State *s1, i32 *order, i32 *seg, struct tli_OutLayout *d)
{
    i32 nb = s1->nb_sections;
    i32 *key = tli_malloc(nb * sizeof(i32));
    i32 i, k, flags, open_flags, nload;

    // Give every section a sort key: its band, plus its rank within that band.
    for (i = 1; i < nb; i++) {
        tli_Section *s = s1->sections[i];
        i32 band;

        if (s->sh_name == 0) {
            band = BAND_DROPPED; // nothing named it, so nothing can find it
        } else if (s->sh_flags & SHF_ALLOC) {
            // Thread-local data is writable, but only ever as a per-thread
            // copy made from the template in the image; the template itself is
            // read-only and joins the relro group below.
            band = (s->sh_flags & (SHF_WRITE | SHF_TLS)) == SHF_WRITE
                 ? BAND_LOAD_RW : BAND_LOAD_RO;
        } else {
            band = BAND_UNMAPPED;
        }

        // A flat binary is the image and nothing but the image: it has no
        // section headers with which to describe anything unmapped.
        if (band >= BAND_UNMAPPED
            && s1->output_format != TLI_OUTPUT_FORMAT_ELF) {
            s->sh_size = 0;
            band = BAND_DROPPED;
        }

        k = band + section_rank(s1, s, band, d);

        // The four sections the linker makes for itself (.text, .data,
        // .data.ro, .bss) sort after everything of equal rank that came out of
        // an input file, so that _etext and _edata, which mark their ends, end
        // up past the input material rather than in the middle of it.
        if (s->sh_num <= bss_section->sh_num)
            k++;

        // The relro group is classified read-only above, because that is what
        // it is once the program is running. It has to be mapped writable to
        // get there, so move it into the writable band.
        if ((k & ~0xf) == BAND_LOAD_RO + RANK_RELRO_FIRST) {
            k += BAND_LOAD_RW - BAND_LOAD_RO;
            s->sh_flags |= SHF_WRITE;
        }

        key[i] = k;
        order[i] = i;
    }
    order[0] = 0;

    // Stable, so sections of equal rank keep the order they were created in,
    // which is the order the input files were read in.
    tl_sort(order + 1, nb - 1, sizeof(i32), cmp_section_key, key);

    // Walk the sorted order and hand each section the segment flags that go
    // with it, or zero for one that is written to the file but never mapped.
    d->shnum = 1; // the null section header at index zero
    nload = 0;
    open_flags = 0;
    for (i = 1; i < nb; i++) {
        tli_Section *s = s1->sections[order[i]];

        k = key[order[i]];
        flags = 0;
        if (k < BAND_DROPPED)
            d->shnum++;

        if (k < BAND_UNMAPPED) {
            flags = s->sh_flags & (SHF_ALLOC | SHF_WRITE | SHF_EXECINSTR);

            // The NetBSD and FreeBSD loaders take only two PT_LOAD segments,
            // so everything that is not writable is folded into one executable
            // segment and the relro region is given up on. OpenBSD is not in
            // this list; it wants the split. See:
            // https://blog.netbsd.org/tnf/entry/the_first_report_on_lld
            if (s1->target_os == TLI_OS_NETBSD
                || s1->target_os == TLI_OS_FREEBSD) {
                if ((flags & SHF_WRITE) == 0)
                    flags |= SHF_EXECINSTR, k = 0;
            }

            // One PT_LOAD per run of alike-permissioned sections. A section
            // with nothing in it does not open one: an empty segment tells the
            // loader nothing and can cost it a page.
            if (flags != open_flags && s->sh_size) {
                open_flags = flags;
                nload++;
                flags |= SEG_OPENS_LOAD;
            }

            if ((s->sh_flags & SHF_TLS) && s->sh_size)
                d->ph_tls = 1, flags |= SHF_TLS;

            if ((k & ~0xf) == BAND_LOAD_RW + RANK_RELRO_FIRST)
                d->ph_relro = 1, flags |= SEG_IN_RELRO;
        }

        seg[i] = flags;
    }
    tli_free(key);
    return nload;
}

/// -------------------------------------------------------------------------
/// program headers

/// Open a segment over one section. p_paddr mirrors p_vaddr: nothing
/// tinylinker targets distinguishes the two, and the gABI leaves p_paddr to
/// systems where physical addressing is relevant.
static ElfW(Phdr) *open_segment(ElfW(Phdr) *ph, i32 type, tli_Section *s)
{
    ph->p_type = type;
    ph->p_flags = PF_R;
    if (s->sh_flags & SHF_WRITE)
        ph->p_flags |= PF_W;

    ph->p_offset = s->sh_offset;
    ph->p_vaddr = ph->p_paddr = s->sh_addr;
    ph->p_filesz = ph->p_memsz = s->sh_size;
    ph->p_align = s->sh_addralign;
    return ph;
}

/// Stretch a segment to end at addr in memory and at file_offset in the file.
/// p_memsz can exceed p_filesz, and does whenever the run ends in SHT_NOBITS;
/// the loader zeroes the difference.
static void extend_segment(ElfW(Phdr) *ph, addr_t addr,
                           u64 file_offset)
{
    ph->p_filesz = file_offset - ph->p_offset;
    ph->p_memsz = addr - ph->p_vaddr;
}

/// PT_TLS, PT_GNU_RELRO and PT_NOTE each cover a run of consecutive sections.
/// The first section of the run opens the segment; every one after it
/// stretches the same header.
static ElfW(Phdr) *cover_segment(ElfW(Phdr) *ph, i32 type, tli_Section *s,
                                 addr_t addr, u64 file_offset)
{
    if (ph->p_type == 0)
        open_segment(ph, type, s);

    extend_segment(ph, addr, file_offset);
    return ph;
}

/// Assign addresses and file offsets to every section, and build the program
/// headers that describe the result. Nothing may write into a section after
/// this: the addresses relocation needs are settled here.
static void layout_sections(tli_State *s1, i32 *order, i32 *seg,
                            struct tli_OutLayout *d)
{
    tli_Section *s;
    ElfW(Phdr) *ph = NULL, *ph2;
    addr_t addr, base, align, seg_align, before;
    u64 file_offset;
    i32 i, f, n, phnum, phfill;

    phnum = order_sections(s1, order, seg, d);

    // PT_PHDR describes the program header table itself. The gABI allows it
    // only when that table is inside a loadable segment, and requires it to
    // precede every loadable segment, so it takes the first slot; PT_INTERP,
    // which is what makes it worth writing at all, takes the second. Neither
    // is emitted without an interpreter to read them.
    phfill = d->interp ? 2 : 0; // [0] PT_PHDR, [1] PT_INTERP
    phnum += phfill;

    // Then one slot apiece for the segments there is at most one of, each of
    // them recording where in phdr[] it ended up.
    if (d->dynamic)
        d->ph_dynamic = phnum++;

    if (d->ph_note)
        d->ph_note = phnum++;

    if (d->ph_tls)
        d->ph_tls = phnum++;

    if (d->ph_relro)
        d->ph_relro = phnum++;

    if (d->eh_frame_hdr)
        d->ph_eh_frame = phnum++;

    d->phnum = phnum;
    d->phdr = tli_mallocz(phnum * sizeof(ElfW(Phdr)));

    file_offset = 0;
    if (s1->output_format == TLI_OUTPUT_FORMAT_ELF) {
        // Headers first: the ELF header, then the program headers, then the
        // section headers. Keeping all three at the front lets the first
        // PT_LOAD cover them, which costs no extra memory because they share
        // its first page, and leaves the file in the shape strip and objcopy
        // expect to find it in.
        file_offset = elf_sh_offset(phnum) + d->shnum * sizeof(ElfW(Shdr));
    }

    // The unit the loader maps in. Two segments with different permissions
    // cannot share one of these, so it is also the granularity at which the
    // layout below jumps forward.
    seg_align = s1->section_align ? s1->section_align : s1->arch->page_size;

    addr = s1->arch->start_addr;
    if (s1->output_type & TLI_OUTPUT_DYN)
        addr = 0; // a shared object or PIE is placed by the loader

    if (s1->has_text_addr)
        addr = s1->text_addr;

    base = addr;

    // Advancing the address by exactly the header size keeps addr congruent to
    // file_offset modulo anything that divides seg_align, which is what the
    // gABI demands of every PT_LOAD and what lets one page serve both. -Ttext
    // is taken at its word, so an address that is not a multiple of the page
    // size will break that congruence and the loader will refuse the file.
    addr += file_offset;

    n = 0;
    for (i = 1; i < s1->nb_sections; i++) {
        s = s1->sections[order[i]];
        f = seg[i];
        align = s->sh_addralign - 1;

        if (f == 0) {
            // Not mapped: it needs a place in the file and nothing more.
            file_offset = (file_offset + align) & ~align;
            s->sh_offset = file_offset;
            if (s->sh_type != SHT_NOBITS)
                file_offset += s->sh_size;

            continue;
        }

        // A PT_LOAD other than the first one starts here, so the run that came
        // before it has to be seen off the page it ended on.
        if ((f & SEG_OPENS_LOAD) && n) {
            if (s1->output_format == TLI_OUTPUT_FORMAT_ELF) {
                // Permissions change here. If the previous segment ended part
                // way through a page, skip the rest of it: the two segments
                // would otherwise share a page, and the loader would have to
                // map that page twice, once with each set of permissions.
                if (addr & (seg_align - 1))
                    addr += seg_align;
            } else {
                // A flat binary has no segments; align the boundary anyway so
                // that whatever loads it can protect the pieces separately.
                align = seg_align - 1;
            }
        }

        // Place the section, advancing address and file offset together.
        before = addr;
        addr = (addr + align) & ~align;
        file_offset += addr - before; // in lockstep, to keep the congruence
        s->sh_offset = file_offset;
        s->sh_addr = addr;

        addr += s->sh_size;
        if (s->sh_type != SHT_NOBITS)
            file_offset += s->sh_size;

        if (f & SEG_OPENS_LOAD) {
            ph = &d->phdr[phfill + n];
            open_segment(ph, PT_LOAD, s);
            ph->p_align = seg_align;
            if (f & SHF_EXECINSTR)
                ph->p_flags |= PF_X;

            if (n == 0) {
                // Pull the first segment back to the start of the file so that
                // it covers the ELF header and the program headers. It uses no
                // more memory, since those share its first page, and tools
                // which rewrite a file are happier when nothing is outside a
                // segment.
                ph->p_offset = 0;
                ph->p_vaddr = ph->p_paddr = base;
            }

            n++;
        }

        if (ph)
            extend_segment(ph, addr, file_offset);

        // The three segments that cover runs of sections rather than one
        // section each, extended a section at a time as the run goes by.
        if (f & SEG_IN_RELRO) {
            ph2 = cover_segment(&d->phdr[d->ph_relro], PT_GNU_RELRO, s,
                                addr, file_offset);

            // Not a mapped segment of its own, only a region the loader
            // mprotects, so p_align describes nothing.
            ph2->p_align = 1;
        }

        if (f & SHF_TLS) {
            ph2 = cover_segment(&d->phdr[d->ph_tls], PT_TLS, s,
                                addr, file_offset);
            if ((addr_t)s->sh_addralign > ph2->p_align)
                ph2->p_align = s->sh_addralign;

            // .tbss exists only in each thread's copy of the block, so it
            // takes no room in the image: hand its address back. p_memsz has
            // already counted it, which is the whole point of PT_TLS.
            if (s->sh_type == SHT_NOBITS)
                addr -= s->sh_size;

            // The backends need the extent of the block to resolve the
            // thread-pointer-relative relocations. The end is rounded up the
            // same way the thread library will round it.
            s1->tls_start = ph2->p_vaddr;
            s1->tls_end = s1->tls_start + ph2->p_memsz
                        + (-ph2->p_memsz & (ph2->p_align - 1));
        }

        if (s->sh_type == SHT_NOTE)
            cover_segment(&d->phdr[d->ph_note], PT_NOTE, s, addr, file_offset);
    }

    // The segments that simply point at one section, now that the section has
    // an address.
    if (d->ph_dynamic)
        open_segment(&d->phdr[d->ph_dynamic], PT_DYNAMIC, d->dynamic);

    // How an unwinder finds the frame descriptions at all: it asks the loader
    // for the program headers and looks for this one.
    if (d->ph_eh_frame)
        open_segment(&d->phdr[d->ph_eh_frame], PT_GNU_EH_FRAME,
                     d->eh_frame_hdr);

    if (d->interp)
        open_segment(&d->phdr[1], PT_INTERP, d->interp);

    if (phfill) {
        // PT_PHDR describes the program header array itself, which sits
        // immediately after the ELF header and inside the first PT_LOAD.
        ph = &d->phdr[0];
        ph->p_type = PT_PHDR;
        ph->p_flags = PF_R;
        ph->p_offset = sizeof(ElfW(Ehdr));
        ph->p_vaddr = ph->p_paddr = base + ph->p_offset;
        ph->p_filesz = ph->p_memsz = phnum * sizeof(ElfW(Phdr));
        ph->p_align = 4;
    }
}

/// -------------------------------------------------------------------------
/// the dynamic table

/// Append one entry to the dynamic table. The gABI fixes no order for the
/// tags apart from DT_NULL, which terminates the table.
static void put_dt(tli_Section *dynamic, i32 dt, addr_t val)
{
    ElfW(Dyn) *dyn = section_ptr_add(dynamic, sizeof(ElfW(Dyn)));

    dyn->d_tag = dt;
    dyn->d_un.d_val = val;
}

/// Append the address-bearing entries of the dynamic table, which is
/// everything the loader needs to find in the image once it has been placed.
static void fill_dynamic(tli_State *s1, struct tli_OutLayout *d)
{
    tli_Section *dynamic = d->dynamic;
    tli_Section *s;

    // This runs twice: once before layout, to reserve the right number of
    // entries so that .dynamic has its final size when addresses are handed
    // out, and once after, with the addresses filled in. It must therefore
    // produce the same entries both times, whatever the addresses turn out to
    // be, so the tests below are on emptiness rather than on any address.

    // Where the dynamic linker finds the symbols and their names. Both hash
    // tables are written: DT_GNU_HASH is far the faster, but a loader that
    // does not know it falls back on DT_HASH.
    put_dt(dynamic, DT_HASH, s1->dynsym->hash->sh_addr);
    put_dt(dynamic, DT_GNU_HASH, d->gnu_hash->sh_addr);
    put_dt(dynamic, DT_STRTAB, d->dynstr->sh_addr);
    put_dt(dynamic, DT_SYMTAB, s1->dynsym->sh_addr);
    put_dt(dynamic, DT_STRSZ, d->dynstr->data_offset);
    put_dt(dynamic, DT_SYMENT, sizeof(ElfW(Sym)));

    // The relocations, and separately the subset that fills the PLT. A given
    // object uses either the RELA form or the REL form throughout, so only one
    // set of tags is ever written; DT_PLTREL names which one.
#if PTR_SIZE == 8
    put_dt(dynamic, DT_RELA, d->rel_addr);
    put_dt(dynamic, DT_RELASZ, d->rel_size);
    put_dt(dynamic, DT_RELAENT, sizeof(ElfW_Rel));
    if (s1->plt && s1->plt->reloc) {
        put_dt(dynamic, DT_PLTGOT, s1->got->sh_addr);
        put_dt(dynamic, DT_PLTRELSZ, s1->plt->reloc->data_offset);
        put_dt(dynamic, DT_JMPREL, s1->plt->reloc->sh_addr);
        put_dt(dynamic, DT_PLTREL, DT_RELA);
    }

    // Nothing gathers the relative relocations at the front of the table, so
    // there is no leading run the loader can apply without inspecting each
    // entry. The tag is written all the same, saying so.
    put_dt(dynamic, DT_RELACOUNT, 0);
#else
    put_dt(dynamic, DT_REL, d->rel_addr);
    put_dt(dynamic, DT_RELSZ, d->rel_size);
    put_dt(dynamic, DT_RELENT, sizeof(ElfW_Rel));
    if (s1->plt && s1->plt->reloc) {
        put_dt(dynamic, DT_PLTGOT, s1->got->sh_addr);
        put_dt(dynamic, DT_PLTRELSZ, s1->plt->reloc->data_offset);
        put_dt(dynamic, DT_JMPREL, s1->plt->reloc->sh_addr);
        put_dt(dynamic, DT_PLTREL, DT_REL);
    }

    put_dt(dynamic, DT_RELCOUNT, 0);
#endif

    if (versym_section && verneed_section) {
        // DT_VERSYM indexes into the version definitions and requirements;
        // without DT_VERNEED to index into, it is meaningless and glibc's
        // loader rejects it.
        put_dt(dynamic, DT_VERSYM, versym_section->sh_addr);
        put_dt(dynamic, DT_VERNEED, verneed_section->sh_addr);
        put_dt(dynamic, DT_VERNEEDNUM, dt_verneednum);
    }

    // The three arrays of function pointers the loader walks, in the order it
    // walks them, and the two single entry points that predate them. Each is
    // named only when it holds something.
    s = tli_find_section_by_name(s1, ".preinit_array");
    if (s && s->data_offset) {
        put_dt(dynamic, DT_PREINIT_ARRAY, s->sh_addr);
        put_dt(dynamic, DT_PREINIT_ARRAYSZ, s->data_offset);
    }

    s = tli_find_section_by_name(s1, ".init_array");
    if (s && s->data_offset) {
        put_dt(dynamic, DT_INIT_ARRAY, s->sh_addr);
        put_dt(dynamic, DT_INIT_ARRAYSZ, s->data_offset);
    }

    s = tli_find_section_by_name(s1, ".fini_array");
    if (s && s->data_offset) {
        put_dt(dynamic, DT_FINI_ARRAY, s->sh_addr);
        put_dt(dynamic, DT_FINI_ARRAYSZ, s->data_offset);
    }

    s = tli_find_section_by_name(s1, ".init");
    if (s && s->data_offset)
        put_dt(dynamic, DT_INIT, s->sh_addr);

    s = tli_find_section_by_name(s1, ".fini");
    if (s && s->data_offset)
        put_dt(dynamic, DT_FINI, s->sh_addr);

    // A debugger uses DT_DEBUG to find the loader's list of loaded objects;
    // the loader fills the word in at run time.
    if (s1->do_debug)
        put_dt(dynamic, DT_DEBUG, 0);

    put_dt(dynamic, DT_NULL, 0);
}

/// Close the gaps between the dynamic relocation sections and record the
/// extent of the run for DT_REL/DT_RELA. Nothing moves: only sh_addr and
/// sh_offset change, and the writer emits each section where it now claims.
static void merge_reloc_sections(tli_State *s1, struct tli_OutLayout *d)
{
    tli_Section *relocplt = s1->plt ? s1->plt->reloc : NULL;
    u64 file_offset = 0;
    i32 i;

    // The gaps open up after the layout has run: relocate_section rewrites
    // each allocated relocation section in place, keeping only the entries the
    // dynamic linker still has to apply, and shrinks sh_size to what it kept.
    // The space the layout reserved for the larger figure is left over.

    // Leaving the holes would work on most targets, since a zeroed entry reads
    // as an R_<arch>_NONE the loader skips, but OpenBSD on arm64 has no such
    // relocation and rejects the file. Closing them costs nothing and gives
    // the loader one contiguous run to walk.
    d->rel_addr = d->rel_size = 0;
    for (i = 1; i < s1->nb_sections; i++) {
        tli_Section *s = s1->sections[i];

        // The PLT's relocations stay where they are: DT_JMPREL points at them
        // separately, and they have to be a run of their own.
        if (s->sh_type != SHT_RELX || s == relocplt)
            continue;

        if (d->rel_size == 0) {
            d->rel_addr = s->sh_addr;
            file_offset = s->sh_offset;
        } else {
            s->sh_addr = d->rel_addr + d->rel_size;
            s->sh_offset = file_offset + d->rel_size;
        }

        d->rel_size += s->sh_size;
    }
}

/// -------------------------------------------------------------------------
/// writing the file

/// Advance to the offset the layout gave a section, writing zeros over the gap
/// rather than seeking past it: a hole reads back as zeros on every filesystem
/// this is likely to meet but is not guaranteed to.
static void pad_to(FILE *f, u64 *pos, u64 target)
{
    static const u8 zeros[128] = { 0 };

    // The padding often falls inside a mapped segment, where its contents are
    // the program's business. Going backwards cannot happen from a layout, but
    // merge_reloc_sections moves offsets down after the fact, so the test is
    // one-sided.
    while (*pos < target) {
        u64 n = target - *pos;

        if (n > sizeof zeros)
            n = sizeof zeros;

        fwrite(zeros, 1, n, f);
        *pos += n;
    }
}

/// Write the ELF header, the program headers, the section headers and then the
/// section contents, in that order, which is the order the layout assumed.
static i32 tli_output_elf(tli_State *s1, FILE *f, i32 phnum, ElfW(Phdr) *phdr)
{
    ElfW(Ehdr) ehdr;
    ElfW(Shdr) shdr;
    u64 pos;
    i32 i, shnum, shstrndx, file_type;

    file_type = s1->output_type;
    shnum = s1->nb_sections;
    memset(&ehdr, 0, sizeof ehdr);

    // e_ident. Indices 0 to 6 are the four magic bytes, the class, the data
    // encoding and the version of the identification itself; the gABI names
    // them EI_MAG0..EI_VERSION and tl_elf.h leaves them as literals. The rest
    // of e_ident is reserved and stays zero, which is ELFOSABI_NONE.
    ehdr.e_ident[0] = ELFMAG0;
    ehdr.e_ident[1] = ELFMAG1;
    ehdr.e_ident[2] = ELFMAG2;
    ehdr.e_ident[3] = ELFMAG3;
    ehdr.e_ident[4] = ELFCLASSW;
    ehdr.e_ident[5] = ELFDATA2LSB;
    ehdr.e_ident[6] = EV_CURRENT;

    // The architecture's own stamps. e_flags is a different field from
    // EI_OSABI and has to be written whatever OS the image is for: these used
    // to share one #if/#elif chain with the OS, so an ARM or RISC-V image for
    // a BSD lost its ABI word entirely.
    switch (s1->arch->em) {
    case EM_ARM:
        // AAELF puts the EABI version in the top byte of e_flags, and the
        // floating-point convention in a flag beside it. The two float flags
        // are mutually exclusive and a loader that sees neither assumes soft.
        ehdr.e_flags = EF_ARM_EABI_VER5;
        ehdr.e_flags |= s1->float_abi == ARM_HARD_FLOAT
            ? EF_ARM_VFP_FLOAT : EF_ARM_SOFT_FLOAT;
        break;
    case EM_RISCV:
        // The RISC-V psABI records the floating-point calling convention in
        // e_flags, and the linker refuses to mix objects that disagree. Only
        // the double convention is produced. XXX should be configurable
        ehdr.e_flags = EF_RISCV_FLOAT_ABI_DOUBLE;
        break;
    }

    // Then the OS, which is a runtime choice and so goes last: naming one
    // overrides an architecture default such as ELFOSABI_ARM. Linux is
    // ELFOSABI_NONE, which is the zero e_ident already holds.
    switch (s1->target_os) {
    case TLI_OS_FREEBSD: ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD; break;
    case TLI_OS_OPENBSD: ehdr.e_ident[EI_OSABI] = ELFOSABI_OPENBSD; break;
    case TLI_OS_NETBSD:  ehdr.e_ident[EI_OSABI] = ELFOSABI_NETBSD;  break;
    default: break;
    }

    if (file_type == TLI_OUTPUT_OBJ) {
        ehdr.e_type = ET_REL;
    } else {
        ehdr.e_type = (file_type & TLI_OUTPUT_DYN) ? ET_DYN : ET_EXEC;

        // e_entry is where the kernel starts the program. An unnamed entry
        // point is only an error for an executable: a shared object usually
        // has none, and gets the start of .text so the field is at least
        // inside the image.
        if (s1->elf_entryname)
            ehdr.e_entry = get_sym_addr(s1, s1->elf_entryname, 1, 0);
        else
            ehdr.e_entry = get_sym_addr(s1, "_start",
                                        !!(file_type & TLI_OUTPUT_EXE), 0);

        if (ehdr.e_entry == (addr_t)-1)
            ehdr.e_entry = text_section->sh_addr;

        if (s1->nb_errors)
            return -1;
    }

    // Local symbols must precede global ones and sh_info must say where the
    // boundary is; sorting is the last chance to arrange that.
    sort_syms(s1, s1->symtab);

    ehdr.e_machine = s1->arch->em;
    ehdr.e_version = EV_CURRENT;
    ehdr.e_ehsize = sizeof(ElfW(Ehdr));
    ehdr.e_shentsize = sizeof(ElfW(Shdr));
    shstrndx = shnum - 1; // alloc_sec_names put it last

    // e_shnum and e_shstrndx are sixteen bits, and the values from
    // SHN_LORESERVE up are reserved, so neither can state a large one. Each
    // has an escape that says "look at section header zero instead": that
    // header is the reserved null entry and its fields are otherwise unused.
    // e_shnum goes to SHN_UNDEF with the count in sh_size, and e_shstrndx to
    // SHN_XINDEX with the index in sh_link. (e_phnum has the same arrangement
    // through sh_info; nothing here comes close to needing it.)
    ehdr.e_shnum = shnum < SHN_LORESERVE ? shnum : 0;
    ehdr.e_shstrndx = shstrndx < SHN_LORESERVE ? shstrndx : SHN_XINDEX;
    ehdr.e_shoff = elf_sh_offset(phnum);
    if (phnum > 0) {
        ehdr.e_phoff = sizeof(ElfW(Ehdr));
        ehdr.e_phentsize = sizeof(ElfW(Phdr));
        ehdr.e_phnum = phnum;
    }

    pos = fwrite(&ehdr, 1, sizeof(ElfW(Ehdr)), f);
    if (phdr)
        pos += fwrite(phdr, 1, phnum * sizeof(ElfW(Phdr)), f);

    pad_to(f, &pos, ehdr.e_shoff);

    // tli_Section header zero is the reserved SHN_UNDEF entry and is all zero
    // but for whatever the header above could not hold.
    for (i = 0; i < shnum; i++) {
        memset(&shdr, 0, sizeof shdr);
        if (!i) {
            if (shnum >= SHN_LORESERVE)
                shdr.sh_size = shnum;

            if (shstrndx >= SHN_LORESERVE)
                shdr.sh_link = shstrndx;
        } else {
            tli_Section *s = s1->sections[i];

            shdr.sh_name = s->sh_name;
            shdr.sh_type = s->sh_type;
            shdr.sh_flags = s->sh_flags;
            shdr.sh_entsize = s->sh_entsize;
            shdr.sh_info = s->sh_info;
            if (s->link)
                shdr.sh_link = s->link->sh_num;

            shdr.sh_addralign = s->sh_addralign;
            shdr.sh_addr = s->sh_addr;
            shdr.sh_offset = s->sh_offset;
            shdr.sh_size = s->sh_size;
        }

        pos += fwrite(&shdr, 1, sizeof(ElfW(Shdr)), f);
    }

    // Then the contents, each at the offset its header claims.
    for (i = 1; i < shnum; i++) {
        tli_Section *s = s1->sections[i];

        // SHT_NOBITS claims file space nowhere; the loader zeroes it.
        if (s->sh_type == SHT_NOBITS)
            continue;

        pad_to(f, &pos, s->sh_offset);
        if (s->sh_size) {
            fwrite(s->data, 1, s->sh_size, f);
            pos += s->sh_size;
        }
    }
    return 0;
}

/// Write the image and nothing else: no headers, no unmapped sections, just
/// the mapped bytes at the offsets the layout gave them. What loads such a
/// file has to know its entry point and its load address from somewhere else.
static i32 tli_output_binary(tli_State *s1, FILE *f)
{
    u64 pos = 0;
    i32 i;

    for (i = 1; i < s1->nb_sections; i++) {
        tli_Section *s = s1->sections[i];

        if (s->sh_type == SHT_NOBITS || !(s->sh_flags & SHF_ALLOC))
            continue;

        pad_to(f, &pos, s->sh_offset);
        fwrite(s->data, 1, s->sh_size, f);
        pos += s->sh_size;
    }
    return 0;
}

/// Open the output file and hand it to whichever writer the format calls for.
static i32 tli_write_elf_file(tli_State *s1, const char *filename, i32 phnum,
                              ElfW(Phdr) *phdr)
{
    FILE *f;
    i32 fd, mode, ret;

    // An executable is made executable; a relocatable object is not, since
    // nothing is going to run it.
    mode = s1->output_type == TLI_OUTPUT_OBJ ? 0666 : 0777;

    // Unlink first rather than truncating: the old file may be running, or
    // hard-linked, and replacing the name leaves whoever holds it alone.
    unlink(filename);
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, mode);
    if (fd < 0 || (f = fdopen(fd, "wb")) == NULL)
        return tli_error_noabort("could not write '%s: %s'", filename,
                                 strerror(errno));

    if (s1->verbose)
        printf("<- %s\n", filename);

    if (s1->output_format == TLI_OUTPUT_FORMAT_ELF)
        ret = tli_output_elf(s1, f, phnum, phdr);
    else
        ret = tli_output_binary(s1, f);

    fclose(f);
    return ret;
}

/// Rebuild s1->sections in output order, dropping the sections that were never
/// named and so are not going out, and repair every reference that was by
/// section index.
static void reorder_sections(tli_State *s1, i32 *order)
{
    tli_Section **snew = NULL, *s;
    i32 *backmap;
    i32 i, nnew = 0;

    // backmap[] turns an old section number into a new one, which is what the
    // second loop needs to repair sh_info, sh_num and st_shndx.
    backmap = tli_malloc(s1->nb_sections * sizeof(*backmap));
    for (i = 0; i < s1->nb_sections; i++) {
        i32 k = order ? order[i] : i;

        s = s1->sections[k];

        // Index zero is the null section and always survives.
        if (i == 0 || s->sh_name) {
            backmap[k] = nnew;
            dynarray_add(&snew, &nnew, s);
        } else {
            // Dropped, but still owning its data: hand it to the private list
            // so tli_elf_delete frees it with everything else.
            backmap[k] = 0;
            dynarray_add(&s1->priv_sections, &s1->nb_priv_sections, s);
        }
    }

    for (i = 1; i < nnew; i++) {
        s = snew[i];
        s->sh_num = i;
        if (s->sh_type == SHT_RELX) {
            s->sh_info = backmap[s->sh_info];
        } else if (s->sh_type == SHT_SYMTAB || s->sh_type == SHT_DYNSYM) {
            i32 j, nb_syms = s->data_offset / sizeof(ElfW(Sym));

            for (j = 1; j < nb_syms; j++) {
                // A symbol that names no section has nothing to renumber;
                // SHN_ABS and SHN_COMMON reach here.  The rest move, and can
                // move to either side of the limit, so the new number goes
                // back through the pair that can hold either.
                i32 shndx = tli_sym_section(s, j);

                if (!shndx || shndx >= s1->nb_sections)
                    continue;

                tli_sym_set_section(s, j, backmap[shndx]);
            }
        }
    }

    tli_free(s1->sections);
    s1->sections = snew;
    s1->nb_sections = nnew;
    tli_free(backmap);
}

/// -------------------------------------------------------------------------
/// architecture attributes

/// The ARM and RISC-V supplements share one build-attributes format:
///     byte      'A', the format version
///     then, repeated:
///       uint32  the length of this vendor subsection, these four bytes in
///       NTBS    the vendor name
///       then, repeated:
///         uleb  the scope tag; Tag_File (1) is the only one used here
///         uint32 the length of this scope, the tag and these four bytes in
///         then, repeated:
///           uleb the attribute tag
///           value, an NTBS if the tag is odd and a uleb if it is even
/// Every tag and value written below is under 128, so each uleb is one byte.

/// The only scope tag either supplement asks for: what follows applies to the
/// whole file rather than to one section or symbol.
#define ATTR_TAG_FILE 1

/// Write one of the format's little-endian lengths, which are not aligned and
/// so cannot go out as a word.
static void attr_u32(u8 *p, u32 len)
{
    p[0] = len & 0xff;
    p[1] = len >> 8 & 0xff;
    p[2] = len >> 16 & 0xff;
    p[3] = len >> 24 & 0xff;
}

/// Build one single-vendor attributes section and patch the two lengths, which
/// are only known once everything inside them has been written.
static void put_attributes(tli_State *s1, const char *name, i32 sh_type,
                           const char *vendor, const u8 *attrs,
                           u32 n)
{
    u32 vlen = strlen(vendor) + 1;
    u32 scope = 1 + 4 + n;         // tag, its length, the attributes
    u32 section = 4 + vlen + scope; // the length, the vendor, the scope
    tli_Section *s = new_section(s1, name, sh_type, 0);
    u8 *p = section_ptr_add(s, 1 + section);

    s->sh_addralign = 1; // a byte stream: nothing in it needs alignment
    *p++ = 'A';
    attr_u32(p, section);
    p += 4;
    memcpy(p, vendor, vlen);
    p += vlen;
    *p++ = ATTR_TAG_FILE;
    attr_u32(p, scope);
    p += 4;
    memcpy(p, attrs, n);
}

/// AAELF32 build attribute tags. The values describe what a linked image may
/// assume of the code in it, and are what a DLL is checked against when it is
/// loaded, so they have to be present even though tinylinker compiles nothing
/// and takes them on trust from its inputs.
#define Tag_CPU_name             5
#define Tag_CPU_arch             6
#define Tag_ARM_ISA_use          8
#define Tag_THUMB_ISA_use        9
#define Tag_FP_arch             10
#define Tag_ABI_PCS_wchar_t     18
#define Tag_ABI_FP_denormal     20
#define Tag_ABI_FP_exceptions   21
#define Tag_ABI_FP_number_model 23
#define Tag_ABI_align_needed    24
#define Tag_ABI_align_preserved 25
#define Tag_ABI_enum_size       26
#define Tag_ABI_VFP_args        28
#define Tag_ABI_optimization_goals 30
#define Tag_CPU_unaligned_access 34

/// Claim, on behalf of every ARM object in the link, the one set of build
/// attributes this linker is prepared to produce.
static void create_arm_attribute_section(tli_State *s1)
{
    i32 hard = s1->float_abi == ARM_HARD_FLOAT;
    const u8 attrs[] = {
        // Tag_CPU_name is odd, so its value is a string.
        Tag_CPU_name, '6', 0,
#if TLI_CPUVER >= 7
        Tag_CPU_arch, 10,               // v7
#else
        Tag_CPU_arch, 6,                // v6
#endif
        Tag_ARM_ISA_use, 1,             // the ARM instruction set is used
        Tag_THUMB_ISA_use, 1,           // and 16-bit Thumb with it

        // Without floating-point hardware there is no VFP architecture to
        // name, and no AAPCS-VFP argument passing to claim either; the tag
        // that would have claimed it carries an optimisation note instead, so
        // that a soft-float image says something about itself in the same
        // number of bytes rather than falling silent.
        Tag_FP_arch, hard ? 2 : 0,      // VFPv2, or none
        Tag_ABI_PCS_wchar_t, 4,
        Tag_ABI_FP_denormal, 1,         // IEEE 754 denormals
        Tag_ABI_FP_exceptions, 1,       // IEEE 754 inexact exceptions
        Tag_ABI_FP_number_model, 3,     // full IEEE 754
        Tag_ABI_align_needed, 1,        // 8-byte alignment is required
        Tag_ABI_align_preserved, 1,     // and preserved, bar the leaf SP
        Tag_ABI_enum_size, 2,           // an enum is as wide as an int
        hard ? Tag_ABI_VFP_args : Tag_ABI_optimization_goals, hard ? 1 : 6,
        Tag_CPU_unaligned_access, 1     // v6-style unaligned access
    };

    put_attributes(s1, ".ARM.attributes", SHT_ARM_ATTRIBUTES, "aeabi",
                   attrs, sizeof attrs);
}

/// The RISC-V psABI spells the ISA out as a string rather than a bitmap, so
/// that an extension can be named before anybody has allocated a bit for it.
/// Each extension carries the version it is claimed against, major "p" minor.
#define Tag_RISCV_arch 5

/// Claim rv64gc, written out in the canonical single-letter order, plus the
/// two Zicsr and Zifencei extensions that were split out of the base I set.
/// That is what the RISC-V backend assumes of its input.
static void create_riscv_attribute_section(tli_State *s1)
{
    static const u8 attrs[] = {
        Tag_RISCV_arch, // odd, so an NTBS value
        'r','v','6','4','i','2','p','1','_','m','2','p','0','_',
        'a','2','p','1','_','f','2','p','2','_','d','2','p','2','_',
        'c','2','p','0','_','z','i','c','s','r','2','p','0','_',
        'z','i','f','e','n','c','e','i','2','p','0', 0
    };

    put_attributes(s1, ".riscv.attributes", SHT_RISCV_ATTRIBUTES, "riscv",
                   attrs, sizeof attrs);
}

/// -------------------------------------------------------------------------
/// BSD identification notes

/// Append one note, unless a note of that type is already there. The gABI note
/// format is the header, then n_namesz bytes of name and n_descsz bytes of
/// descriptor, each padded up to the section's alignment.
static void fill_bsd_note(tli_Section *s, i32 type, const char *value,
                          u32 data)
{
    u64 offset = 0;
    i32 align = s->sh_addralign;
    ElfW(Nhdr) *note;
    char *ptr;

    // Walk what is there already, stepping over each entry by the lengths in
    // its own header.
    while (offset + sizeof(ElfW(Nhdr)) < s->data_offset) {
        note = (ElfW(Nhdr) *)(s->data + offset);
        if (note->n_type == (u32)type)
            return;

        offset += (sizeof(ElfW(Nhdr)) + note->n_namesz + note->n_descsz
                   + align - 1) & -align;
    }

    // The BSD kernels all want an eight-byte name and a four-byte descriptor.
    // Both are multiples of the four-byte note alignment the section is given
    // below, so the entry goes out at its natural size with no padding.
    ptr = section_ptr_add(s, sizeof(ElfW(Nhdr)) + 8 + 4);
    note = (ElfW(Nhdr) *)ptr;
    note->n_namesz = 8;
    note->n_descsz = 4;
    note->n_type = type;
    strcpy(ptr + sizeof(ElfW(Nhdr)), value);
    memcpy(ptr + sizeof(ElfW(Nhdr)) + 8, &data, 4);
}

/// The release the identification note claims. There is no setting for it:
/// the OS is chosen per link, so the machine the linker happens to be running
/// on says nothing about the machine the image will run on, and the build-time
/// CONFIG_OS_RELEASE this used to read was never defined by anything. These
/// are the oldest releases the notes below are shaped for.
static const char *bsd_release(i32 os)
{
    switch (os) {
    case TLI_OS_FREEBSD: return "14.0.0";
    case TLI_OS_NETBSD:  return "10.0.0";
    default:             return "0.0.0";
    }
}

/// The note by which each BSD kernel recognises one of its own binaries. The
/// encoding of the release number differs per system and is what the kernel
/// compares against its own version to decide which syscall behaviour to give
/// the process. Returns NULL when the target OS wants no such note.
static tli_Section *create_bsd_note_section(tli_State *s1)
{
    u32 major = 0, minor = 0, patch = 0;
    const char *name, *value;
    tli_Section *s;

    // Each kernel looks for its own section name and its own eight-byte note
    // name; Linux wants neither.
    switch (s1->target_os) {
    case TLI_OS_OPENBSD: name = ".note.openbsd.ident"; value = "OpenBSD"; break;
    case TLI_OS_NETBSD:  name = ".note.netbsd.ident";  value = "NetBSD";  break;
    case TLI_OS_FREEBSD: name = ".note.tag";           value = "FreeBSD"; break;
    default: return NULL;
    }

    sscanf(bsd_release(s1->target_os), "%u.%u.%u", &major, &minor, &patch);

    s = find_section(s1, name);
    s->sh_type = SHT_NOTE;

    // Notes are four-byte aligned on every BSD, LP64 or not, and the entries
    // fill_bsd_note() writes are sized for that. find_section() hands back a
    // PROGBITS section aligned to a pointer, which on a 64-bit target left the
    // section claiming an alignment its own contents did not follow: readers
    // then skipped eight bytes past each name and walked off the end.
    s->sh_addralign = 4;
    switch (s1->target_os) {
    case TLI_OS_OPENBSD:
        fill_bsd_note(s, ELF_NOTE_OS_GNU, value, 0);
        break;

    case TLI_OS_NETBSD:
        // NT_NETBSD_IDENT_TAG.
        fill_bsd_note(s, 1, value,
                      major * 100000000u + (minor % 100u) * 1000000u
                      + (patch % 10000u) * 100u);
        break;

    default:
        // NT_FREEBSD_ABI_TAG, then NT_FREEBSD_FEATURE_CTL and
        // NT_FREEBSD_NOINIT_TAG. Only the feature-control note is rejected by
        // kernels before 14, so only it is held back; skipping the whole
        // section, as this used to, cost the image its brand as well.
        fill_bsd_note(s, 1, value,
                      major * 100000u + (minor % 100u) * 1000u);
        if (major >= 14)
            fill_bsd_note(s, 4, value, 0);

        fill_bsd_note(s, 2, value, 0);
        break;
    }
    return s;
}

/// -------------------------------------------------------------------------

/// Defined below, but called from the link before it is reached.
static void alloc_sec_names(tli_State *s1, i32 is_obj);

/// Link an executable or a shared object.
///
/// The order of what follows is forced by two circular-looking dependencies.
/// Addresses cannot be assigned until every section has its final size, and
/// .dynamic's size depends on how many entries it will hold, so the dynamic
/// table is built once with the addresses still unknown and rebuilt
/// afterwards. Relocation needs final addresses, and the GOT and the PLT are
/// relocated against themselves, so they are filled last.
static i32 elf_output_file(tli_State *s1, const char *filename)
{
    struct tli_OutLayout layout = {0};
    tli_Section *interp, *dynstr, *dynamic, *eh_frame_hdr;
    i32 *sec_order = NULL;
    i32 i, ret, file_type, textrel, got_sym, dt_flags_1;

    file_type = s1->output_type;
    ret = -1;
    interp = dynstr = dynamic = eh_frame_hdr = NULL;

    // The sections that describe the image rather than belong to it, made
    // first so that they take part in everything below.
    if (s1->arch->em == EM_ARM)
        create_arm_attribute_section(s1);
    else if (s1->arch->em == EM_RISCV)
        create_riscv_attribute_section(s1);

    create_bsd_note_section(s1);

    tli_add_runtime(s1);
    resolve_common_syms(s1);

    if (!s1->static_link) {
        if (file_type & TLI_OUTPUT_EXE) {
            // PT_INTERP names the program that will be run instead of this
            // file, and which then loads it. Only an executable has one: a
            // shared object is loaded by whoever needs it.
            interp = new_section(s1, ".interp", SHT_PROGBITS, SHF_ALLOC);
            interp->sh_addralign = 1;
            put_elf_str(interp, s1->elfint);
            layout.interp = interp;
        }

        s1->dynsym = new_symtab(s1, ".dynsym", SHT_DYNSYM, SHF_ALLOC,
                                ".dynstr", ".hash", SHF_ALLOC);

        // sh_info of a symbol table is the index of its first non-local
        // symbol. Nothing in the dynamic table is local, so the only entry
        // below the line is the reserved one at index zero.
        s1->dynsym->sh_info = 1;
        dynstr = s1->dynsym->link;

        dynamic = new_section(s1, ".dynamic", SHT_DYNAMIC,
                              SHF_ALLOC | SHF_WRITE);
        dynamic->link = dynstr;
        dynamic->sh_entsize = sizeof(ElfW(Dyn));

        // Decide which symbols the output exports and which it imports, then
        // give every one of them that needs it a GOT slot and a PLT entry.
        got_sym = build_got(s1);
        if (file_type & TLI_OUTPUT_EXE) {
            bind_exe_dynsyms(s1, file_type & TLI_OUTPUT_DYN);
            if (s1->nb_errors)
                goto the_end;
        }

        build_got_entries(s1, got_sym);
        if (file_type & TLI_OUTPUT_EXE)
            bind_libs_dynsyms(s1);
        else
            export_global_syms(s1); // a shared object exports everything

        layout.gnu_hash = create_gnu_hash(s1);
    } else {
        build_got_entries(s1, 0);
    }

    version_add(s1);

    // The frame descriptions are of no use to an unwinder without a table to
    // find them by, and that table has to exist before the sizes are settled
    // so that it can be given one like anything else.  Its contents wait
    // until every address it names is final, which is after the relocations
    // further down.
    {
        usize hdr_size = tli_eh_frame_hdr_size(s1);

        if (hdr_size) {
            eh_frame_hdr = new_section(s1, ".eh_frame_hdr", SHT_PROGBITS,
                                       SHF_ALLOC);
            eh_frame_hdr->sh_addralign = 4;
            section_ptr_add(eh_frame_hdr, hdr_size);
            layout.eh_frame_hdr = eh_frame_hdr;
        }
    }

    textrel = size_output_sections(s1);

    if (!s1->static_link) {
        // The tags that name strings come first, because each one appends to
        // .dynstr and .dynstr has to be complete before it is sized.
        for (i = 0; i < s1->nb_loaded_dlls; i++) {
            tli_DLLReference *dllref = s1->loaded_dlls[i];

            // Only what was asked for directly: the loader will find the rest
            // through each object's own DT_NEEDED.
            if (dllref->level == 0)
                put_dt(dynamic, DT_NEEDED, put_elf_str(dynstr, dllref->name));
        }

        if (s1->rpath)
            put_dt(dynamic, s1->enable_new_dtags ? DT_RUNPATH : DT_RPATH,
                   put_elf_str(dynstr, s1->rpath));

        dt_flags_1 = DF_1_NOW;
        if (file_type & TLI_OUTPUT_DYN) {
            if (s1->soname)
                put_dt(dynamic, DT_SONAME, put_elf_str(dynstr, s1->soname));

            // Code here is not position independent, so a shared object may
            // have relocations against read-only sections; DT_TEXTREL asks the
            // loader to make them writable while it applies them.
            if (textrel)
                put_dt(dynamic, DT_TEXTREL, 0);

            if (file_type & TLI_OUTPUT_EXE)
                dt_flags_1 = DF_1_NOW | DF_1_PIE;

            if (s1->znodelete)
                dt_flags_1 |= DF_1_NODELETE;
        }

        // Every relocation is resolved at load time. Lazy binding would need
        // the PLT to stay writable, and the relro region covers the GOT.
        put_dt(dynamic, DT_FLAGS, DF_BIND_NOW);
        put_dt(dynamic, DT_FLAGS_1, dt_flags_1);
        if (s1->symbolic)
            put_dt(dynamic, DT_SYMBOLIC, 0);

        layout.dynamic = dynamic;
        layout.dynstr = dynstr;

        // Remember where the address-bearing entries begin, so the second
        // pass below can rewind to here.
        layout.data_offset = dynamic->data_offset;
        fill_dynamic(s1, &layout);
        dynamic->sh_size = dynamic->data_offset;
        dynstr->sh_size = dynstr->data_offset;
    }

    alloc_sec_names(s1, 0);

    // Two arrays of one entry per section: the output order, and the segment
    // flags at each position.
    sec_order = tli_malloc(2 * s1->nb_sections * sizeof(i32));
    layout_sections(s1, sec_order, sec_order + s1->nb_sections, &layout);

    if (dynamic) {
        // The first GOT slot holds the address of .dynamic, which is how the
        // loader finds it before anything has been relocated. The slot is a
        // whole pointer wide, so write a whole pointer into it.
#if PTR_SIZE == 8
        write64le(s1->got->data, dynamic->sh_addr);
#else
        write32le(s1->got->data, dynamic->sh_addr);
#endif
        // Every backend writes its own PLT entries rather than leaving them
        // for the loader, so both kinds of image want them filled in here.
        if (file_type == TLI_OUTPUT_EXE || (file_type & TLI_OUTPUT_DYN))
            s1->arch->relocate_plt(s1);

        relocate_syms(s1, s1->dynsym, 2);
    }

    // Addresses are settled: patch every reference to them.
    relocate_syms(s1, s1->symtab, 0);
    if (s1->nb_errors != 0)
        goto the_end;

    relocate_sections(s1);

    // Every FDE now holds the address it describes, so the table can be built
    // and sorted.
    tli_eh_frame_hdr_build(s1, eh_frame_hdr);

    if (dynamic) {
        merge_reloc_sections(s1, &layout);

        // Rewind and write the table again, this time with the addresses. The
        // entry count is identical, so nothing moves.
        dynamic->data_offset = layout.data_offset;
        fill_dynamic(s1, &layout);
    }

    if (file_type == TLI_OUTPUT_EXE && s1->static_link)
        fill_got(s1);
    else if (s1->got)
        fill_local_got_entries(s1);

    if (layout.gnu_hash)
        update_gnu_hash(s1, layout.gnu_hash);

    reorder_sections(s1, sec_order);
    ret = tli_write_elf_file(s1, filename, layout.phnum, layout.phdr);

 the_end:
    tli_free(sec_order);
    tli_free(layout.phdr);
    return ret;
}

/// Build .shstrtab and give every section that is going out a name in it. A
/// section left without one is dropped, so this is also where an executable
/// decides what it will not carry: anything empty and unmapped.
static void alloc_sec_names(tli_State *s1, i32 is_obj)
{
    tli_Section *strsec;
    i32 i;

    strsec = new_section(s1, ".shstrtab", SHT_STRTAB, 0);
    put_elf_str(strsec, ""); // index zero is the empty name

    // A relocatable object keeps the lot, since the next link will need it.
    for (i = 1; i < s1->nb_sections; i++) {
        tli_Section *s = s1->sections[i];

        if (is_obj)
            s->sh_size = s->data_offset;

        if (s->sh_size || s == strsec || (s->sh_flags & SHF_ALLOC) || is_obj)
            s->sh_name = put_elf_str(strsec, s->name);
    }

    strsec->sh_size = strsec->data_offset;
}

/// Mark the object as not needing an executable stack.
///
/// GNU ld reads the absence of this section as "this object was built
/// before the convention existed", and gives the whole program an
/// executable stack to be safe. Present and empty, with no SHF_EXECINSTR,
/// it says the opposite. Every compiler emits one; an object without it
/// draws a warning and quietly weakens the program it is linked into.
static void elf_mark_stack_nonexec(tli_State *s1)
{
    i32 i;

    for (i = 1; i < s1->nb_sections; i++)
        if (!strcmp(s1->sections[i]->name, ".note.GNU-stack"))
            return;

    new_section(s1, ".note.GNU-stack", SHT_PROGBITS, 0);
}

/// Write a relocatable object: no addresses, no segments, no ordering. The
/// sections keep the order they were created in and are packed one after
/// another behind the section headers.
static i32 elf_output_obj(tli_State *s1, const char *filename)
{
    u64 file_offset;
    i32 i;

    elf_mark_stack_nonexec(s1);
    alloc_sec_names(s1, 1);

    file_offset = elf_sh_offset(0) + s1->nb_sections * sizeof(ElfW(Shdr));
    for (i = 1; i < s1->nb_sections; i++) {
        tli_Section *s = s1->sections[i];

        // sh_addralign constrains nothing here: the gABI ties it to sh_addr,
        // and sh_addr is zero throughout a relocatable object. Sixteen bytes
        // apiece is simply a round figure, generous enough that a consumer
        // which does read a section straight out of the file finds it on a
        // boundary it can use.
        file_offset = (file_offset + 15) & ~15UL;
        s->sh_offset = file_offset;
        if (s->sh_type != SHT_NOBITS)
            file_offset += s->sh_size;
    }
    return tli_write_elf_file(s1, filename, 0, NULL);
}

/// Write whatever the link produced, in the format the target uses. The fork
/// between the three formats, and the last thing a link does.
TLI_PUBLIC int tli_output_file(tli_State *s, const char *filename)
{
    i32 obj = s->output_type == TLI_OUTPUT_OBJ;

    // A relocatable object comes out in the target's own format, the same as
    // an executable does.
    s->nb_errors = 0;
    switch (s->fmt) {
    case TLI_FMT_PE:
        return obj ? coff_output_obj(s, filename) : pe_output_file(s, filename);
    case TLI_FMT_MACHO:
        return obj ? macho_output_obj(s, filename)
                   : macho_output_file(s, filename);
    default:
        return obj ? elf_output_obj(s, filename) : elf_output_file(s, filename);
    }
}


/// ---- src/core/tl_input.c ----

/// tinylinker - what a link reads
///
/// Everything that turns a file on disk into entries in the section and
/// symbol tables: ELF relocatable objects, static archives in both the
/// System V and the BSD dialect, shared libraries together with their symbol
/// version records, and the small corner of GNU ld script syntax that a
/// distribution's libc.so is written in.  The readers for the two other
/// object formats live in tl_coff_obj.c and tl_macho_obj.c; tl_load_object
/// picks between the three.
///
/// Everything here reads files we have no reason to trust, so every offset
/// and count taken out of a header is checked against the bytes we actually
/// hold before it is used.  A malformed input has to come back as an error,
/// never as a read past the end of a buffer.
///
/// Included by tl_core.c after tl_link.c, whose tables this fills.

/// -------------------------------------------------------------------------
/// reading files

/// Fill the buffer, however many read(2) calls that takes. Returns the number
/// of bytes read, short only at end of file, or -1 if the first read already
/// failed.
ST_FUNC isize full_read(i32 fd, void *buf, usize count)
{
    u8 *p = buf;
    usize done = 0;

    // read(2) may return early for reasons that have nothing to do with the
    // end of the file, so nothing may assume one call fills the buffer.
    while (done < count) {
        isize n = read(fd, p + done, count - done);

        if (n < 0) {
            if (errno == EINTR)
                continue;

            return done ? (isize)done : -1;
        }

        if (n == 0)
            break;

        done += n;
    }
    return done;
}

/// Read 'size' bytes at 'file_offset' into memory of our own.  A short file
/// leaves the tail zeroed rather than holding whatever malloc last had there,
/// so a caller that forgets to check reads zeroes instead of stale memory.
ST_FUNC void *load_data(i32 fd, u64 file_offset, u64 size)
{
    u8 *data = tli_malloc(size);
    isize got = -1;

    if (lseek(fd, file_offset, SEEK_SET) != (off_t)-1)
        got = full_read(fd, data, size);

    if (got < 0)
        got = 0;

    if ((u64)got < size)
        memset(data + got, 0, size - got);

    return data;
}

/// How many bytes of the file follow 'offset'.  Bounds checks need something
/// real to check against, and this is the only thing the file itself can tell
/// us before anything is read.
static u64 file_span(i32 fd, u64 offset)
{
    off_t end = lseek(fd, 0, SEEK_END);

    if (end == (off_t)-1 || (u64)end <= offset)
        return 0;

    return (u64)end - offset;
}

/// -------------------------------------------------------------------------
/// telling the formats apart

/// Read the head of the file and say which of the formats a link understands
/// it is, or 0 for none of them. 'h' comes back holding what was read.
ST_FUNC i32 tli_object_type(tli_State *s1, i32 fd, ElfW(Ehdr) *h)
{
    i32 size = full_read(fd, h, sizeof *h);

    if (size >= 8 && 0 == memcmp(h, ARMAG, 8))
        return AFF_BINTYPE_AR;

    if (size == sizeof *h && 0 == memcmp(h, ELFMAG, 4)) {
        if (h->e_type == ET_REL)
            return AFF_BINTYPE_REL;

        if (h->e_type == ET_DYN)
            return AFF_BINTYPE_DYN;
    }

    // The reader for the target's own object format decides for itself; the
    // header layouts have nothing in common with ELF.
    if (s1->fmt == TLI_FMT_MACHO && macho_object_type(h, size))
        return AFF_BINTYPE_MACHO_REL;

    if (s1->fmt == TLI_FMT_PE) {
        if (coff_object_type(s1, h, size))
            return AFF_BINTYPE_COFF_REL;

        // Most of what an import library holds is not an object at all.
        if (coff_import_type(h, size))
            return AFF_BINTYPE_COFF_IMPORT;
    }
    return 0;
}

/// -------------------------------------------------------------------------
/// an object file held in memory

/// An object is read in one go rather than a section at a time.  That is one
/// read(2) instead of a dozen, and it means every offset a header hands us can
/// be checked against a size we are holding rather than against the file.
typedef struct tli_ObjImage {
    u8 *data;    /// The whole object, with one spare byte past the end.
    usize size;  /// How much of it was actually there to read.
} tli_ObjImage;

/// Read an object into memory.  'limit' is how many bytes the caller knows
/// belong to this object, which an archive knows from the member header and
/// nobody else does; 0 means read to the end of the file.
static i32 obj_read(tli_ObjImage *im, i32 fd, u64 file_offset,
                    u64 limit)
{
    u64 avail = file_span(fd, file_offset);
    isize got;

    im->data = NULL;
    im->size = 0;
    if (limit && limit < avail)
        avail = limit;

    if (avail > (u64)((usize)-1 / 2))
        return -1;

    if (lseek(fd, file_offset, SEEK_SET) == (off_t)-1)
        return -1;

    im->data = tli_malloc((usize)avail + 1);
    got = full_read(fd, im->data, (usize)avail);
    if (got < 0)
        return -1;

    im->size = (usize)got;
    return 0;
}

/// 'len' bytes at 'off', or NULL when that range is not wholly inside the
/// image.  Written so that neither the addition nor the subtraction can wrap,
/// whatever nonsense the header held.
static void *obj_at(const tli_ObjImage *im, u64 off, u64 len)
{
    if (off > im->size || len > im->size - off)
        return NULL;

    return im->data + off;
}

/// The same for an array of 'n' items of 'size' bytes each.
static void *obj_array(const tli_ObjImage *im, u64 off, u64 n, u64 size)
{
    if (size && n > (u64)-1 / size)
        return NULL;

    return obj_at(im, off, n * size);
}

/// A string table, checked once so that the lookups need only a range test.
/// The gABI has a string table start and end with a NUL byte, so once the last
/// byte is known to be one, any index inside it names a terminated string.
typedef struct tli_StrTab {
    const char *data;  /// Into the object image, or NULL if the range was bad.
    u64 size;          /// Its length, the terminating NUL included.
} tli_StrTab;

/// Point 'st' at the table at 'off' and say whether it is one: inside the
/// image, not empty, and terminated, which is what str_at relies on.
static i32 obj_strtab(const tli_ObjImage *im, tli_StrTab *st, u64 off, u64 size)
{
    st->data = obj_at(im, off, size);
    st->size = size;
    return st->data && size && st->data[size - 1] == '\0';
}

/// The string at 'idx', or NULL if the index is not inside the table.
static const char *str_at(const tli_StrTab *st, u64 idx)
{
    return idx < st->size ? st->data + idx : NULL;
}

/// -------------------------------------------------------------------------
/// ELF relocatable objects

/// An object may hold more sections than st_shndx has room for.  The gABI
/// escape is to put the real counts in the otherwise unused section header
/// zero and to redirect each affected symbol through a table of its own.
/// SHN_XINDEX and SHT_SYMTAB_SHNDX are in tl_elf.h with the rest of the
/// format; tl_link.c writes what is read back here.

/// No real object aligns a section this hard; anything larger is a corrupt
/// header rather than a demanding one.
#define MAX_SH_ALIGN 0x1000000

/// Where one of the object's sections ended up in the merged output.
typedef struct tli_SectionMergeInfo {
    tli_Section *s;            /// The output section, NULL if this one was dropped.
    u64 offset;  /// Where its contents start inside that section.
    u8 created;            /// The output section was made for this one.
    u8 link_once;          /// Dropped: that name is already linked.
} tli_SectionMergeInfo;

/// Which of the object's sections take part in the link.  A section that is
/// not named here has no meaning to us, so it is dropped along with the
/// relocations that apply to it and the symbols defined in it.
static i32 section_is_wanted(tli_State *s1, const ElfW(Shdr) *sh,
                             const char *name, i32 compressed)
{
    // Debug information is carried through only when it was asked for, and
    // only when we can read it: a SHF_COMPRESSED section would have to be
    // inflated first, and nothing here does that.
    if (0 == strncmp(name, ".debug_", 7) || 0 == strncmp(name, ".stab", 5))
        return s1->do_debug && !compressed;

    // .eh_frame used to be dropped everywhere except the BSDs, whose crt
    // objects register the frames by hand: with no .eh_frame_hdr written,
    // keeping the frames put bytes in the output that nothing could reach.
    // There is a header now, so they are kept and an unwinder can find them.
    // .eh_frame_hdr itself is built here rather than merged from input.
    if (0 == strcmp(name, ".eh_frame_hdr"))
        return 0;

    switch (sh->sh_type) {
    case SHT_PROGBITS:      // code, data, and most of what a compiler emits
    case SHT_NOTE:          // vendor notes, concatenated as they come
    case SHT_NOBITS:        // .bss and friends: size but no contents
    case SHT_PREINIT_ARRAY: // the three arrays of startup function pointers
    case SHT_INIT_ARRAY:
    case SHT_FINI_ARRAY:
        return 1;

    // What clang gives .eh_frame on x86_64, where gcc uses SHT_PROGBITS.  The
    // number is in the processor-specific range and means something else
    // elsewhere, so it counts only there.
    case SHT_X86_64_UNWIND:
        return s1->arch->em == EM_X86_64;
    }
    return 0;
}

/// Whether a relocation carries no symbol because it marks a place rather than
/// a reference, which on ARM and RISC-V some do.
static i32 reloc_marks_place(tli_State *s1, i32 type)
{
    switch (s1->arch->em) {
    case EM_ARM:
        return type == R_ARM_V4BX;
    case EM_RISCV:
        return type == R_RISCV_ALIGN || type == R_RISCV_RELAX;
    }
    return 0;
}

/// Read one ELF relocatable object and fold it into the link: its sections
/// into the output sections of the same name, its symbols into ours, and its
/// relocations rewritten to point at both.
static i32 elf_load_object(tli_State *s1, i32 fd, u64 file_offset,
                           u64 limit)
{
    tli_ObjImage im;
    ElfW(Ehdr) *eh;
    ElfW(Shdr) *shdr, *sh;
    ElfW(Sym) *symtab = NULL;
    tli_SectionMergeInfo *sm = NULL;
    tli_StrTab shstr, symstr;
    u32 *xshndx = NULL;         // SHT_SYMTAB_SHNDX, when the object needs one
    u64 nb_xshndx = 0;
    i32 *sym_map = NULL;        // the object's symbol indices to ours
    u64 shnum, shstrndx, i;
    u64 nb_syms = 0, symtab_at = 0;
    u64 stab_at = 0, stabstr_at = 0;
    i32 compressed = 0, ret = -1;

    memset(&shstr, 0, sizeof shstr);
    memset(&symstr, 0, sizeof symstr);

    if (obj_read(&im, fd, file_offset, limit) < 0)
        goto invalid;

    eh = obj_at(&im, 0, sizeof *eh);
    if (!eh || memcmp(eh->e_ident, ELFMAG, 4) || eh->e_type != ET_REL)
        goto invalid;

    // e_ident[4] is the class and [5] the byte order.  Both are checked here
    // because a mismatch means every field below would be read at the wrong
    // offset or in the wrong order.
    if (eh->e_ident[4] != ELFCLASSW || eh->e_ident[5] != ELFDATA2LSB)
        goto invalid;

    if (eh->e_machine != s1->arch->em) {
        tli_error_noabort("object file is for a different architecture");
        goto the_end;
    }

    // The section header table.  When there are more sections than e_shnum
    // can hold it reads zero and the real count is in the reserved first
    // entry, which is why that entry has to be read before the rest.
    if (eh->e_shentsize != sizeof *shdr || !eh->e_shoff)
        goto invalid;

    shdr = obj_at(&im, eh->e_shoff, sizeof *shdr);
    if (!shdr || (eh->e_shoff & (sizeof(ElfW(Addr)) - 1)))
        goto invalid;

    shnum = eh->e_shnum ? eh->e_shnum : shdr[0].sh_size;
    shdr = obj_array(&im, eh->e_shoff, shnum, sizeof *shdr);
    if (!shdr || shnum < 1)
        goto invalid;

    shstrndx = eh->e_shstrndx == SHN_XINDEX ? shdr[0].sh_link : eh->e_shstrndx;
    if (shstrndx >= shnum)
        goto invalid;

    if (!obj_strtab(&im, &shstr, shdr[shstrndx].sh_offset,
                    shdr[shstrndx].sh_size))
        goto invalid;

    sm = tli_mallocz(sizeof *sm * (usize)shnum);

    // First look at every section header, because two of the decisions below
    // depend on sections that may come after the ones they affect.
    for (i = 1; i < shnum; i++) {
        sh = &shdr[i];
        if (sh->sh_flags & SHF_COMPRESSED)
            compressed = 1;

        if (sh->sh_type == SHT_SYMTAB_SHNDX) {
            xshndx = obj_at(&im, sh->sh_offset, sh->sh_size);
            if (!xshndx || (sh->sh_offset & 3))
                goto invalid;

            nb_xshndx = sh->sh_size / sizeof *xshndx;
        }

        if (sh->sh_type != SHT_SYMTAB)
            continue;

        if (symtab) {
            tli_error_noabort("object file has more than one symbol table");
            goto the_end;
        }

        if (sh->sh_entsize && sh->sh_entsize != sizeof *symtab)
            goto invalid;

        nb_syms = sh->sh_size / sizeof *symtab;
        symtab = obj_array(&im, sh->sh_offset, nb_syms, sizeof *symtab);
        if (!symtab || (sh->sh_offset & (sizeof(ElfW(Addr)) - 1)))
            goto invalid;

        if (sh->sh_link >= shnum
            || !obj_strtab(&im, &symstr, shdr[sh->sh_link].sh_offset,
                           shdr[sh->sh_link].sh_size))
            goto invalid;

        symtab_at = i;
    }

    // A relocation section created below links to the symbol table, so the
    // symbol table needs an entry here for the second pass to find.
    if (symtab)
        sm[symtab_at].s = symtab_section;

    // Which linkonce sections this object is about to lose, decided before any
    // of its sections exist.
    // A .gnu.linkonce section is one copy of something several objects define,
    // and the copies after the first are dropped. Their relocations have to go
    // with them: the section they patch is already there and already
    // relocated, so applying them again patches it twice. On a RELA
    // architecture that writes the same answer twice and nobody notices; on a
    // REL one the addend lives in the field, so the second application adds to
    // the first and the result is wrong. An i386 pointer through a duplicated
    // linkonce section came out at exactly twice the address it should have
    // held.
    // This runs first because it cannot be decided while merging: a
    // relocation section may be reached before or after the section it applies
    // to, and asking mid-merge whether one of this object's own sections
    // already exists answers yes for the copy that is being kept.
    for (i = 1; i < shnum; i++) {
        const char *nm;

        if (shdr[i].sh_type == SHT_RELX)
            continue;

        nm = str_at(&shstr, shdr[i].sh_name);
        if (nm && 0 == strncmp(nm, ".gnu.linkonce", 13)
            && tli_find_section_by_name(s1, nm))
            sm[i].link_once = 1;
    }

    // Merge each section that means something to us into the output section
    // of the same name, creating that section if this is the first object to
    // carry one.
    for (i = 1; i < shnum; i++) {
        const ElfW(Shdr) *governs;
        const char *name;
        u64 align;
        tli_Section *s;

        if (i == shstrndx)
            continue;

        sh = &shdr[i];

        // Relocations go where the section they apply to goes: if that one
        // is not taken then neither are they.
        governs = sh;
        if (sh->sh_type == SHT_RELX) {
            if (sh->sh_info >= shnum)
                goto invalid;

            governs = &shdr[sh->sh_info];
            if (sh->sh_entsize && sh->sh_entsize != sizeof(ElfW_Rel))
                goto invalid;

            // The copy these patch is being dropped, so they are too.
            if (sm[sh->sh_info].link_once)
                continue;
        }

        name = str_at(&shstr, governs->sh_name);
        if (!name)
            goto invalid;

        if (!section_is_wanted(s1, governs, name, compressed))
            continue;

        name = str_at(&shstr, sh->sh_name);
        if (!name)
            goto invalid;

        // section_add() rounds with a mask, so an alignment that is not a
        // power of two would quietly move the section somewhere else.
        align = sh->sh_addralign ? sh->sh_addralign : 1;
        if (align > MAX_SH_ALIGN || (align & (align - 1))) {
            tli_error_noabort("section '%s' has a bad alignment", name);
            goto the_end;
        }

        if (sh->sh_type != SHT_NOBITS && !obj_at(&im, sh->sh_offset, sh->sh_size))
            goto invalid;

        s = tli_find_section_by_name(s1, name);
        if (s) {
            if (sh->sh_type != (u32)s->sh_type
                // .eh_frame changes type between output kinds, and some
                // crt1.o carry two .note.GNU-stack sections of different
                // types.  Neither is a reason to refuse the link.
                && strcmp(name, ".eh_frame")
                && strcmp(name, ".note.GNU-stack")) {
                tli_error_noabort("section type conflict: %s %02x <> %02x",
                                  name, (u32)sh->sh_type, s->sh_type);
                goto the_end;
            }

            // A linkonce section is one copy of something that several
            // objects define.  We already have one, so this copy is dropped;
            // its symbols are dealt with below, since relocations elsewhere
            // still refer to them.
            if (0 == strncmp(name, ".gnu.linkonce", 13)) {
                sm[i].link_once = 1;
                continue;
            }

            // The stab strings move with the stab section they belong to, so
            // note where both of them came from.
            if (stab_section) {
                if (s == stab_section)
                    stab_at = i;
                else if (s == stab_section->link)
                    stabstr_at = i;
            }
        } else {
            s = new_section(s1, name, sh->sh_type, sh->sh_flags & ~SHF_GROUP);

            // Take what the object says rather than the default new_section()
            // guesses; sh_link and sh_info are section numbers in the
            // object's own numbering and are translated in the second pass.
            s->sh_addralign = (i32)align;
            s->sh_entsize = sh->sh_entsize;
            sm[i].created = 1;
        }

        sm[i].s = s;
        sm[i].offset = section_add(s, sh->sh_size, (i32)align);
        if (sh->sh_type != SHT_NOBITS && sh->sh_size)
            memcpy(s->data + sm[i].offset, im.data + sh->sh_offset,
                   (usize)sh->sh_size);

        // Keep the next object's code starting on an instruction boundary,
        // on the machines whose instructions are four bytes.
        if ((s1->arch->em == EM_ARM || s1->arch->em == EM_AARCH64
             || s1->arch->em == EM_RISCV)
            && (s->sh_flags & SHF_EXECINSTR))
            section_add(s, 0, 4);
    }

    // Stab entries name their strings by offset into a string section that
    // has just been concatenated onto an existing one, so every offset in
    // the batch we appended has moved by the same amount.
    if (stab_at && stabstr_at) {
        tli_Section *s = sm[stab_at].s;
        tli_StabSym *p = (tli_StabSym *)(s->data + sm[stab_at].offset);
        tli_StabSym *end = (tli_StabSym *)(s->data + s->data_offset);
        u32 moved = sm[stabstr_at].offset;

        for (; p < end; p++)
            if (p->n_strx)
                p->n_strx += moved;
    }

    // Second pass: sh_link and sh_info are section numbers in the object's
    // numbering, so the sections we just created still hold the wrong ones.
    for (i = 1; i < shnum; i++) {
        tli_Section *s = sm[i].s;

        if (!s || !sm[i].created)
            continue;

        sh = &shdr[i];
        if (sh->sh_link > 0 && sh->sh_link < shnum)
            s->link = sm[sh->sh_link].s;

        if (sh->sh_type == SHT_RELX) {
            tli_Section *target = sm[sh->sh_info].s;

            if (!target) {
                tli_error_noabort("relocations for a section that was dropped");
                goto the_end;
            }

            s->sh_info = target->sh_num;

            // and the link the other way, which is how the output writer
            // finds a section's relocations
            target->reloc = s;
        }
    }

    if (!symtab)
        goto done;

    // Fold the object's symbols into ours, remembering what each of its
    // symbol indices became so the relocations can be translated.
    sym_map = tli_mallocz(sizeof *sym_map * (usize)nb_syms);

    for (i = 1; i < nb_syms; i++) {
        ElfW(Sym) *sym = &symtab[i];
        u64 shndx = sym->st_shndx;
        const char *name;
        i32 in_section, out_shndx, out_xshndx = 0;

        if (shndx == SHN_XINDEX) {
            if (i >= nb_xshndx)
                goto invalid;

            shndx = xshndx[i];
            in_section = 1;
        } else {
            // The reserved indices are values in their own right: undefined,
            // absolute, common.  Only the rest name a section.
            in_section = shndx != SHN_UNDEF && shndx < SHN_LORESERVE;
        }

        // A reserved value means the same thing in our numbering as it did
        // in the object's, so it goes through untouched.
        out_shndx = (i32)shndx;

        if (in_section) {
            tli_SectionMergeInfo *from;

            if (shndx >= shnum)
                goto invalid;

            from = &sm[shndx];
            if (from->link_once) {
                // The section this symbol lives in was dropped for being a
                // copy of one we already have.  Point the relocations at the
                // definition we kept: getting this wrong would silently
                // relocate against the wrong copy.
                if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
                    name = str_at(&symstr, sym->st_name);
                    if (!name)
                        goto invalid;

                    sym_map[i] = find_elf_sym(symtab_section, name);
                }

                continue;
            }

            if (!from->s)
                continue; // defined in a section we did not take

            tli_shndx_of(from->s, &out_shndx, &out_xshndx);
            sym->st_value += from->offset;
        }

        name = str_at(&symstr, sym->st_name);
        if (!name)
            goto invalid;

        sym_map[i] = set_elf_sym_x(symtab_section, sym->st_value, sym->st_size,
                                   sym->st_info, sym->st_other,
                                   out_shndx, out_xshndx, name);
    }

    // Third pass: the relocations were copied in verbatim, so each one still
    // names a symbol by the object's index and a place by an offset into the
    // object's own section.  Both are rewritten in place, in the order the
    // object gave them: clang emits relocations in descending address order
    // and that order is part of the input, not noise to be tidied away.
    for (i = 1; i < shnum; i++) {
        tli_Section *s = sm[i].s;
        const char *name;
        ElfW_Rel *rel;
        u64 n, k, moved;

        if (!s)
            continue;

        sh = &shdr[i];
        if (sh->sh_type != SHT_RELX)
            continue;

        moved = sm[sh->sh_info].offset;
        rel = (ElfW_Rel *)(s->data + sm[i].offset);
        n = sh->sh_size / sizeof *rel;

        for (k = 0; k < n; k++, rel++) {
            i32 type = ELFW(R_TYPE)(rel->r_info);
            u64 sym_index = ELFW(R_SYM)(rel->r_info);

            // Only one symbol table per object, checked above, so there is
            // no question which one an index refers to.
            if (sym_index >= nb_syms)
                goto invalid_reloc;

            sym_index = sym_map[sym_index];
            if (!sym_index && !sm[sh->sh_info].link_once
                && !reloc_marks_place(s1, type)) {
            invalid_reloc:
                name = str_at(&shstr, sh->sh_name);
                tli_error_noabort("invalid relocation entry [%2d] '%s' @ %.8x",
                                  (i32)i, name ? name : "?",
                                  (u32)rel->r_offset);
                goto the_end;
            }

            rel->r_info = ELFW(R_INFO)(sym_index, type);
            rel->r_offset += moved;

            // A PLT entry is ARM code.  An unconditional bl from Thumb code
            // becomes a blx, but the other Thumb branches cannot change mode
            // on their own and need a stub in front of the entry; mark the
            // symbol so the PLT builder knows to make one.
            if (s1->arch->em == EM_ARM && type == R_ARM_THM_JUMP24)
                get_sym_attr(s1, (i32)sym_index, 1)->plt_thumb_stub = 1;
        }
    }

 done:
    // set_elf_sym() reports duplicate definitions and the like by counting an
    // error rather than by its return value.
    ret = s1->nb_errors ? -1 : 0;
    goto the_end;

 invalid:
    tli_error_noabort("invalid object file");
 the_end:
    tli_free(sym_map);
    tli_free(sm);
    tli_free(im.data);
    return ret;
}

/// Load a relocatable object of whichever format tli_object_type() found.
/// 'limit' bounds the object when the caller knows where it ends, as an
/// archive does; 0 means it runs to the end of the file.
ST_FUNC i32 tl_load_object(tli_State *s1, i32 fd, u64 file_offset,
                           i32 obj_type, u64 limit)
{
    switch (obj_type) {
    case AFF_BINTYPE_MACHO_REL:
        return macho_load_object(s1, fd, file_offset);
    case AFF_BINTYPE_COFF_REL:
        return coff_load_object(s1, fd, file_offset);
    case AFF_BINTYPE_COFF_IMPORT:
        return coff_load_import(s1, fd, file_offset);
    default:
        return elf_load_object(s1, fd, file_offset, limit);
    }
}

/// -------------------------------------------------------------------------
/// static archives
///
/// An archive is the eight bytes "!<arch>\n" followed by members, each of them
/// introduced by a sixty byte header of left justified ASCII fields padded
/// with spaces, and each padded out to an even offset.  A member whose name is
/// the archive's own is not an object but its symbol index: a map from a
/// symbol name to the offset of the member header that defines it, which is
/// what lets a link take only the members it needs.
///
/// Two dialects are in circulation and both have to work.  System V, which GNU
/// ar writes, calls the index "/" and stores it big-endian; BSD, which Apple's
/// ar writes, calls it "__.SYMDEF" and stores it little-endian, in a different
/// shape.  They also differ in where a name too long for the sixteen byte
/// field is kept.  Everything that differs is called out below.

/// The sixty byte member header, exactly as it sits in the file.
typedef struct tli_ArHeader {
    char ar_name[16];   /// Member name, padded with spaces.
    char ar_date[12];   /// Modification time, decimal.
    char ar_uid[6];     /// Owner, decimal.
    char ar_gid[6];     /// Group, decimal.
    char ar_mode[8];    /// File mode, octal.
    char ar_size[10];   /// Size of the contents, decimal.
    char ar_fmag[2];    /// Always ARFMAG, which is how a header is recognised.
} tli_ArHeader;

/// What ar_fmag holds: the two bytes that say a header is a header.
#define ARFMAG "`\n"

/// As long a member name as either dialect can express, plus its terminator.
#define AR_NAME_MAX 256

/// One member, decoded far enough to read it.
typedef struct tli_ArMember {
    u64 hdr;    /// Offset of the header: what the index names.
    u64 data;   /// Offset of the contents.
    u64 size;   /// Size of the contents.
    u64 next;   /// Offset of the following header.
    char name[AR_NAME_MAX]; /// Expanded out of whichever long-name scheme.
} tli_ArMember;

/// The System V index is big-endian, which is the one thing in an archive that
/// does not follow the host.
static u64 ar_be(const u8 *p, i32 n)
{
    u64 v = 0;

    while (n--)
        v = (v << 8) | *p++;

    return v;
}

/// The BSD index is little-endian, which on every host tinylinker runs on is
/// the host's own order.
static u64 ar_le(const u8 *p, i32 n)
{
    u64 v = 0;

    while (n--)
        v = (v << 8) | p[n];

    return v;
}

/// An ASCII header field, which is padded with spaces and not terminated.
static u64 ar_num(const char *field, i32 len, i32 base)
{
    char buf[16];
    i32 n = len < (i32)sizeof buf - 1 ? len : (i32)sizeof buf - 1;

    memcpy(buf, field, n);
    buf[n] = '\0';
    return strtoull(buf, NULL, base);
}

/// Read the member header at 'off'.  Returns 1 on success, 0 at the end of the
/// archive and -1 if the header is not one.  'longnames' is the contents of
/// the "//" member if the archive has one, needed to expand System V names.
static i32 ar_read_member(i32 fd, u64 off, u64 fsize,
                          const char *longnames, u64 nlongnames,
                          tli_ArMember *m)
{
    tli_ArHeader hdr;
    isize got;
    i32 len, n;

    // The last member is padded to an even offset, so the walk can end one
    // byte past the file rather than exactly on it.
    if (off >= fsize)
        return 0;

    if (fsize - off < sizeof hdr)
        return -1;

    if (lseek(fd, off, SEEK_SET) == (off_t)-1)
        return -1;

    got = full_read(fd, &hdr, sizeof hdr);
    if (got == 0)
        return 0;

    if (got != sizeof hdr || memcmp(hdr.ar_fmag, ARFMAG, sizeof hdr.ar_fmag))
        return -1;

    m->hdr = off;
    m->size = ar_num(hdr.ar_size, sizeof hdr.ar_size, 10);
    m->data = off + sizeof hdr;
    if (m->size > fsize - m->data)
        return -1;

    // The name field is not terminated when the name fills it, which
    // "__.SYMDEF SORTED" does exactly, so it has to be copied out into
    // something wider before it can be compared with anything.
    for (len = (i32)sizeof hdr.ar_name; len > 0 && hdr.ar_name[len - 1] == ' ';)
        --len;

    memcpy(m->name, hdr.ar_name, len);
    m->name[len] = '\0';

    // BSD keeps a name too long for the field in front of the contents and
    // counts it in ar_size, announcing it as "#1/<length>".  The name may be
    // padded with NULs to keep the contents aligned, which copying it out as
    // a C string takes care of.
    if (0 == strncmp(m->name, "#1/", 3) && (n = atoi(m->name + 3)) > 0) {
        if (n >= AR_NAME_MAX || (u64)n > m->size)
            return -1;

        if (full_read(fd, m->name, n) != n)
            return -1;

        m->name[n] = '\0';
        m->data += n;
        m->size -= n;
    } else if (m->name[0] == '/' && m->name[1] >= '0' && m->name[1] <= '9') {
        // System V keeps long names in a member of its own and refers to
        // them as "/<offset into it>", each terminated by "/\n".
        u64 at = ar_num(m->name + 1, len - 1, 10);
        u64 k = 0;

        if (at >= nlongnames)
            return -1;

        while (at + k < nlongnames && k < AR_NAME_MAX - 1
               && longnames[at + k] != '/' && longnames[at + k] != '\n')
            ++k;

        memcpy(m->name, longnames + at, (usize)k);
        m->name[k] = '\0';
    } else if (len > 1 && m->name[len - 1] == '/' && m->name[0] != '/') {
        // and terminates an ordinary name with a slash, which is not part of
        // it: without this every name would read as "foo.o/".  The archive's
        // own members, "/" and "//", are all slash and keep theirs.
        m->name[len - 1] = '\0';
    }

    // Members start on an even offset.
    m->next = (m->data + m->size + 1) & ~1ULL;
    return 1;
}

/// The two dialects, told apart by what the index member is called.
enum { AR_SYSV = 1, AR_BSD };

/// Is this member the symbol index rather than an object?  Returns the width
/// of one entry in it, 0 if this is an ordinary member, and reports which
/// dialect wrote it.
static i32 ar_index_kind(const char *name, i32 *dialect)
{
    // System V: big-endian, one table of member offsets.
    if (0 == strcmp(name, "/"))
        return *dialect = AR_SYSV, 4;

    if (0 == strcmp(name, "/SYM64/"))
        return *dialect = AR_SYSV, 8;

    // BSD: little-endian, pairs of (name offset, member offset).  "SORTED"
    // only says the entries are in order, which we do not rely on.
    if (0 == strcmp(name, "__.SYMDEF") || 0 == strcmp(name, "__.SYMDEF SORTED"))
        return *dialect = AR_BSD, 4;

    if (0 == strcmp(name, "__.SYMDEF_64")
        || 0 == strcmp(name, "__.SYMDEF_64 SORTED"))
        return *dialect = AR_BSD, 8;

    return 0;
}

/// The index, decoded into the one shape both dialects mean: a symbol name and
/// the offset of the member header that defines it.
typedef struct tli_ArIndex {
    u64 n;               /// How many entries it holds.
    const char *names;   /// Into 'data'.
    u64 nnames;          /// Bytes of names, never past the buffer.
    const u8 *ents;      /// Into 'data'.
    i32 entsize;         /// 4 or 8, by which of the four index names it had.
    i32 dialect;         /// AR_SYSV or AR_BSD.
    u8 *data;            /// The member's contents, with a spare NUL at the end.
} tli_ArIndex;

/// Release the index once the members it named have been taken.
static void ar_index_free(tli_ArIndex *ix)
{
    tli_free(ix->data);
    ix->data = NULL;
}

/// Read and validate the index member's contents.  Everything the entries
/// point at is checked here so that walking them cannot leave the buffer.
static i32 ar_index_read(i32 fd, const tli_ArMember *m, i32 entsize, i32 dialect,
                         tli_ArIndex *ix)
{
    u64 size = m->size, tab, strs;

    memset(ix, 0, sizeof *ix);
    if (size > (u64)((usize)-1 / 2))
        return -1;

    ix->data = tli_malloc((usize)size + 1);
    ix->entsize = entsize;
    ix->dialect = dialect;
    if (lseek(fd, m->data, SEEK_SET) == (off_t)-1
        || full_read(fd, ix->data, (usize)size) != (isize)size)
        return -1;

    ix->data[size] = '\0';

    if (dialect == AR_SYSV) {
        // count, then that many member offsets, then that many names
        if (size < (u64)entsize)
            return -1;

        ix->n = ar_be(ix->data, entsize);
        if (ix->n > (size - entsize) / entsize)
            return -1;

        ix->ents = ix->data + entsize;
        tab = (u64)entsize + ix->n * entsize;
        ix->names = (const char *)ix->data + tab;
        ix->nnames = size - tab;
    } else {
        // the size of the entry table in bytes, the entries, the size of the
        // string table in bytes, then the strings
        if (size < (u64)entsize)
            return -1;

        tab = ar_le(ix->data, entsize);
        if (tab > size - entsize)
            return -1;

        ix->n = tab / (2 * (u64)entsize);
        ix->ents = ix->data + entsize;
        if (size - entsize - tab < (u64)entsize)
            return -1;

        strs = ar_le(ix->data + entsize + tab, entsize);
        ix->names = (const char *)ix->data + 2 * (u64)entsize + tab;
        ix->nnames = size - 2 * (u64)entsize - tab;
        if (strs < ix->nnames)
            ix->nnames = strs;
    }
    return 0;
}

/// Entry 'i': the symbol it names and where the member that defines it starts.
/// Returns 0 if the entry points outside the index.  The dialects keep the
/// names differently: BSD gives every entry an offset into a string table,
/// System V simply lays the names out in entry order, so that one is walked
/// with a cursor.  Either way a name can only run as far as the spare NUL
/// ar_index_read() left at the end of the buffer.
static i32 ar_index_entry(const tli_ArIndex *ix, u64 i,
                          const char **cursor, const char **name,
                          u64 *hdr)
{
    const u8 *e;

    if (ix->dialect == AR_SYSV) {
        if (*cursor >= ix->names + ix->nnames)
            return 0;

        *name = *cursor;
        *cursor += strlen(*cursor) + 1;
        e = ix->ents + i * ix->entsize;
        *hdr = ar_be(e, ix->entsize);
    } else {
        u64 at;

        e = ix->ents + i * 2 * (u64)ix->entsize;
        at = ar_le(e, ix->entsize);
        if (at >= ix->nnames)
            return 0;

        *name = ix->names + at;
        *hdr = ar_le(e + ix->entsize, ix->entsize);
    }
    return 1;
}

/// Load one member as an object.  'skip_unknown' passes silently over a member
/// that is not one, which is what a whole-archive link wants: the index and
/// the long name table are members like any other.
static i32 ar_load_member(tli_State *s1, i32 fd, const tli_ArMember *m,
                          i32 skip_unknown)
{
    ElfW(Ehdr) ehdr;
    i32 obj_type, ret;

    if (lseek(fd, m->data, SEEK_SET) == (off_t)-1)
        return -1;

    obj_type = tli_object_type(s1, fd, &ehdr);
    if (skip_unknown && obj_type != AFF_BINTYPE_REL
        && obj_type != AFF_BINTYPE_MACHO_REL
        && obj_type != AFF_BINTYPE_COFF_REL
        && obj_type != AFF_BINTYPE_COFF_IMPORT)
        return 0;

    if (s1->verbose == 2)
        printf("   -> %s\n", m->name);

    // A COFF reader orders import sections by the member they came from.
    s1->current_member = m->name;
    ret = tl_load_object(s1, fd, m->data, obj_type, m->size);
    s1->current_member = NULL;
    return ret;
}

/// Whether an archive member is a short import record rather than an object.
///
/// Only the two words at the front are needed to tell, and reading them is
/// cheap enough to do speculatively on the first member of an archive.
static i32 ar_member_is_import(tli_State *s1, i32 fd, const tli_ArMember *m)
{
    u8 head[20];

    if (s1->fmt != TLI_FMT_PE || m->size < sizeof head)
        return 0;

    lseek(fd, (u64)m->data, SEEK_SET);
    if (full_read(fd, head, sizeof head) != (i32)sizeof head)
        return 0;

    return coff_import_type(head, sizeof head);
}

/// Take the members the link is short of, and only those.  Pulling one in can
/// leave fresh undefined symbols behind that a member earlier in the index
/// defines, so go round again until a pass binds nothing.
static i32 ar_bind(tli_State *s1, i32 fd, u64 fsize,
                   const tli_ArIndex *ix, const char *longnames,
                   u64 nlongnames)
{
    tli_ArMember m;
    u64 i, loads = 0;
    i32 bound;

    do {
        const char *cursor = ix->names;

        bound = 0;
        for (i = 0; i < ix->n; i++) {
            u64 hdr;
            const char *name;
            ElfW(Sym) *sym;
            i32 sym_index;

            if (!ar_index_entry(ix, i, &cursor, &name, &hdr))
                return tli_error_noabort("invalid archive index");

            // Only a symbol this link is still waiting for is worth a member.
            sym_index = find_elf_sym(symtab_section, name);
            if (!sym_index)
                continue;

            sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
            if (sym->st_shndx != SHN_UNDEF)
                continue;

            // A weak external already says what it stands for, and that name
            // is the one to go looking for. Its own member is loaded once.
            if (s1->fmt == TLI_FMT_PE && coff_alias_of(s1, name))
                continue;

            if (ar_read_member(fd, hdr, fsize, longnames, nlongnames, &m) != 1)
                return tli_error_noabort("invalid archive index");

            // An import record was taken as the archive was read, and the
            // name it imports stays undefined until the writer binds it,
            // so asking again would load it again for ever.
            if (ar_member_is_import(s1, fd, &m))
                continue;

            // Each entry can bring in at most one member, so an index whose
            // members do not define what it says they do would otherwise
            // have us going round for ever.
            if (++loads > ix->n)
                return tli_error_noabort("archive index does not match its members");

            if (ar_load_member(s1, fd, &m, 0) < 0)
                return -1;

            ++bound;
        }
    } while (bound);
    return 0;
}

/// Link against an archive: every member of it, or, when 'alacarte' is set,
/// only the members its index says define something still undefined.
ST_FUNC i32 tli_load_archive(tli_State *s1, i32 fd, i32 alacarte)
{
    // index is read only once entsize is set, and whatever sets entsize sets
    // index too.  The zeroing is for gcc, which cannot follow that.
    tli_ArMember m, index = {0};
    tli_ArIndex ix;
    char *longnames = NULL;
    u64 nlongnames = 0;
    u64 nmembers = 0;
    u64 fsize, off;
    i32 entsize = 0, dialect = 0, ret = 0, r, import_lib = 0;

    fsize = file_span(fd, 0);

    // the magic has already been recognised by tli_object_type()
    off = sizeof ARMAG - 1;

    for (;;) {
        r = ar_read_member(fd, off, fsize, longnames, nlongnames, &m);
        if (r == 0)
            break;

        if (r < 0) {
            ret = tli_error_noabort("invalid archive");
            goto the_end;
        }

        off = m.next;

        // The long name table is a member like any other and comes before
        // everything that refers to it, so reading it as we pass is enough.
        if (!longnames && 0 == strcmp(m.name, "//") && m.size) {
            if (m.size > (u64)((usize)-1 / 2)) {
                ret = tli_error_noabort("invalid archive");
                goto the_end;
            }

            longnames = load_data(fd, (u64)m.data, (u64)m.size);
            nlongnames = m.size;
            continue;
        }

        // Nothing past the long name table is of any interest when only the
        // members the link asks for are wanted.  The index is the first
        // member and the table, when there is one, the second, so one member
        // further on is as far as this has to go.
        if (alacarte && entsize) {
            if (s1->fmt != TLI_FMT_PE)
                break;

            // Except on PE, where the archive may be an import
            // library.  Its records define nothing that an index-driven
            // search could see them define: each says only that a name comes
            // from a DLL, which is a note kept for the writer rather than a
            // definition in the symbol table that search consults.  Asking
            // the index for one would load it, find the symbol undefined
            // still, and ask again.
            //
            // So the records are taken as they are passed, which is what
            // reading the DLL itself would have done.  The objects an import
            // library also carries are bound through the index below, like
            // any archive's.  Most are the descriptors the platform's own
            // linker assembles its import table out of, which nothing here
            // asks for, but mingw's also carry code, such as the msvcrt
            // compatibility wrappers over the universal runtime.
            nmembers++;

            if (ar_member_is_import(s1, fd, &m)) {
                import_lib = 1;

                if (ar_load_member(s1, fd, &m, 1) < 0) {
                    ret = -1;
                    goto the_end;
                }
            }

            continue;
        }

        if (!entsize && (entsize = ar_index_kind(m.name, &dialect)) != 0) {
            // The index is put aside rather than acted on: in a System V
            // archive the long name table comes after it, and expanding the
            // names of the members it points at needs that table.
            index = m;
            continue;
        }

        nmembers++;

        if (alacarte && !import_lib)
            continue; // only what the index asks for, once it is understood

        if (ar_load_member(s1, fd, &m, 1) < 0) {
            ret = -1;
            goto the_end;
        }
    }

    if (alacarte) {
        if (!entsize) {

            // An archive holding nothing needs no index, and has nothing to
            // contribute whether it has one or not.  musl ships an empty
            // libm.a, its maths being in libc, so refusing this would refuse
            // every -lm on a musl system.  An import library's records are
            // already taken.
            if (!nmembers || import_lib)
                goto the_end;

            // With members but no index there is no way to tell which one
            // defines what, so an on-demand search can only come back empty.
            // Saying so beats returning success having linked nothing: the
            // symbols the archive was meant to supply would surface one by
            // one as unresolved references, naming everything except why.
            ret = tli_error_noabort("archive has no index; run ranlib to add one");
            goto the_end;
        }

        if (ar_index_read(fd, &index, entsize, dialect, &ix) < 0)
            ret = tli_error_noabort("invalid archive index");
        else
            ret = ar_bind(s1, fd, fsize, &ix, longnames, nlongnames);

        ar_index_free(&ix);
    }
 the_end:
    tli_free(longnames);
    return ret;
}

/// -------------------------------------------------------------------------
/// shared libraries
///
/// Only the dynamic symbols of a library are of any use to a link: what it
/// defines, and under which version.  GNU symbol versioning spreads that over
/// three sections.  .gnu.version has one index per dynamic symbol.  Those
/// indices are defined either by .gnu.version_d, for the versions the library
/// itself defines, or by .gnu.version_r, for the versions it needs from
/// libraries further down; both are chains of records joined by byte offsets
/// rather than arrays, so walking them means checking every step.

/// A record at 'off' inside a version section, or NULL if the chain has left
/// the section.  Chains advance by unsigned offsets and stop at zero, so a
/// walk that checks each record can neither loop nor leave the buffer.
static void *ver_at(const void *base, u64 size, u64 off, u64 len)
{
    if (!base || off > size || len > size - off)
        return NULL;

    return (char *)base + off;
}

/// The version records of one library, as they were found in its sections.
struct tli_VersymInfo {
    i32 nb_versyms;         /// Entries in .gnu.version, one per dynamic symbol.
    ElfW(Verdef) *verdef;   /// .gnu.version_d: the versions it defines.
    ElfW(Verneed) *verneed; /// .gnu.version_r: the versions it needs.
    ElfW(Half) *versym;     /// .gnu.version: a version index per symbol.
    u64 verdef_size, verneed_size; /// Bounds for the two chain walks.
    i32 nb_local_ver;       /// How long local_ver is.
    i32 *local_ver;         /// This library's version indices to the link's.
};

/// Set LV[I] to the global index of the sym-version (LIB,VERSION), adding an
/// entry for it if this is the first time it has been seen.
static void set_ver_to_ver(tli_State *s1, i32 *n, i32 **lv, i32 i,
                           const char *lib, const char *version)
{
    i32 v, prev_same_lib = -1;

    if (i < 0)
        return;

    if (i >= *n) {
        // Version indices are sparse and a library may start at a high one,
        // so grow to what was asked for rather than one at a time.
        *lv = tli_realloc(*lv, (i + 1) * sizeof(**lv));
        while (*n <= i)
            (*lv)[(*n)++] = -1;
    }

    if ((*lv)[i] != -1)
        return;

    for (v = 0; v < nb_sym_versions; v++) {
        if (strcmp(sym_versions[v].lib, lib))
            continue;

        // The versions of one library are written out as a chain, so each
        // entry remembers the previous one from the same library.
        prev_same_lib = v;
        if (!strcmp(sym_versions[v].version, version))
            break;
    }

    if (v == nb_sym_versions) {
        sym_versions = tli_realloc(sym_versions, (v + 1) * sizeof(*sym_versions));
        sym_versions[v].lib = tli_strdup(lib);
        sym_versions[v].version = tli_strdup(version);
        sym_versions[v].out_index = 0;
        sym_versions[v].prev_same_lib = prev_same_lib;
        nb_sym_versions++;
    }

    (*lv)[i] = v;
}

/// Associate symbol SYM_INDEX in dynsymtab with sym-version VERNDX.
static void set_sym_version(tli_State *s1, i32 sym_index, i32 verndx)
{
    if (sym_index >= nb_sym_to_version) {
        i32 n = sym_index ? sym_index * 2 : 1;

        sym_to_version = tli_realloc(sym_to_version, n * sizeof(*sym_to_version));
        memset(sym_to_version + nb_sym_to_version, -1,
               (n - nb_sym_to_version) * sizeof(*sym_to_version));
        nb_sym_to_version = n;
    }

    if (sym_to_version[sym_index] < 0)
        sym_to_version[sym_index] = verndx;
}

/// Turn the library's version records into local_ver[], which maps the
/// indices in .gnu.version to entries in the link's own version table.
static void store_version(tli_State *s1, struct tli_VersymInfo *v,
                          const tli_StrTab *dynstr)
{
    u64 off;
    const char *lib = NULL, *version;
    i32 i;

    // What the library defines.  The first record is the base version, whose
    // one name is the library's own soname; everything after it is a version
    // of that library.
    for (off = 0; v->versym && v->verdef;) {
        ElfW(Verdef) *vd = ver_at(v->verdef, v->verdef_size, off, sizeof *vd);
        ElfW(Verdaux) *aux;

        if (!vd)
            break;

        aux = ver_at(v->verdef, v->verdef_size, off + vd->vd_aux, sizeof *aux);
        if (!aux)
            break;

        if (vd->vd_cnt) {
            version = str_at(dynstr, aux->vda_name);
            if (!version)
                break;

            if (!lib)
                lib = version;
            else
                set_ver_to_ver(s1, &v->nb_local_ver, &v->local_ver, vd->vd_ndx,
                               lib, version);
        }

        if (!vd->vd_next)
            break;

        off += vd->vd_next;
    }

    // What the library needs from elsewhere.  Here each record names another
    // library and carries a chain of the versions wanted from it.
    for (off = 0; v->versym && v->verneed;) {
        ElfW(Verneed) *vn = ver_at(v->verneed, v->verneed_size, off, sizeof *vn);
        u64 aux_off;

        if (!vn)
            break;

        lib = str_at(dynstr, vn->vn_file);
        if (!lib)
            break;

        aux_off = off + vn->vn_aux;
        for (i = 0; i < vn->vn_cnt; i++) {
            ElfW(Vernaux) *aux = ver_at(v->verneed, v->verneed_size, aux_off,
                                        sizeof *aux);

            if (!aux)
                break;

            // the top bit marks a hidden version, which nothing may bind to
            if ((aux->vna_other & 0x8000) == 0) {
                version = str_at(dynstr, aux->vna_name);
                if (!version)
                    break;

                set_ver_to_ver(s1, &v->nb_local_ver, &v->local_ver,
                               aux->vna_other, lib, version);
            }

            if (!aux->vna_next)
                break;

            aux_off += aux->vna_next;
        }

        if (!vn->vn_next)
            break;

        off += vn->vn_next;
    }
}

/// Read a shared library, taking its dynamic symbols and their versions into
/// the link.  'level' 0 means the user asked for this library, which is what
/// decides whether it goes into the output as a DT_NEEDED.
ST_FUNC i32 tli_load_dll(tli_State *s1, i32 fd, const char *filename, i32 level)
{
    ElfW(Ehdr) ehdr;
    ElfW(Shdr) *shdr = NULL, *sh;
    ElfW(Sym) *dynsym = NULL, *sym;
    ElfW(Dyn) *dynamic = NULL, *dt;
    struct tli_VersymInfo v;
    tli_StrTab dynstr;
    char *dynstr_data = NULL;
    u64 fsize;
    i32 i, nb_syms = 0, nb_dts = 0, ret = -1;
    const char *soname;

    memset(&v, 0, sizeof v);
    memset(&dynstr, 0, sizeof dynstr);
    fsize = file_span(fd, 0);

    if (lseek(fd, 0, SEEK_SET) == (off_t)-1
        || full_read(fd, &ehdr, sizeof ehdr) != sizeof ehdr
        || memcmp(ehdr.e_ident, ELFMAG, 4))
        return tli_error_noabort("not a shared library");

    if (ehdr.e_ident[4] != ELFCLASSW || ehdr.e_ident[5] != ELFDATA2LSB
        || ehdr.e_machine != s1->arch->em)
        return tli_error_noabort("bad architecture");

    if (ehdr.e_shentsize != sizeof *shdr
        || (u64)ehdr.e_shnum * sizeof *shdr > fsize
        || ehdr.e_shoff > fsize - (u64)ehdr.e_shnum * sizeof *shdr)
        return tli_error_noabort("not a shared library");

    shdr = load_data(fd, ehdr.e_shoff, sizeof *shdr * ehdr.e_shnum);

    // Anything a link needs from a library is reachable from the dynamic
    // section and the dynamic symbol table; the rest of the file is the
    // loader's business, not ours.
    for (i = 0, sh = shdr; i < ehdr.e_shnum; i++, sh++) {
        switch (sh->sh_type) {
        case SHT_DYNAMIC:
        case SHT_DYNSYM:
        case SHT_GNU_verdef:
        case SHT_GNU_verneed:
        case SHT_GNU_versym:
            // Only the sections that are about to be read are checked: a
            // SHT_NOBITS section is meant to reach past the end of the file
            // and refusing one would refuse every library there is.
            if (sh->sh_size > fsize || sh->sh_offset > fsize - sh->sh_size) {
                tli_error_noabort("shared library has a section outside the file");
                goto the_end;
            }

            break;
        default:
            continue;
        }

        switch (sh->sh_type) {
        case SHT_DYNAMIC:
            if (dynamic)
                break;

            nb_dts = sh->sh_size / sizeof(ElfW(Dyn));
            dynamic = load_data(fd, sh->sh_offset, sh->sh_size);
            break;
        case SHT_DYNSYM:
            if (dynsym)
                break;

            nb_syms = sh->sh_size / sizeof(ElfW(Sym));
            dynsym = load_data(fd, sh->sh_offset, sh->sh_size);
            if (sh->sh_link >= ehdr.e_shnum
                || shdr[sh->sh_link].sh_size > fsize
                || shdr[sh->sh_link].sh_offset
                   > fsize - shdr[sh->sh_link].sh_size) {
                tli_error_noabort("shared library has no dynamic string table");
                goto the_end;
            }

            dynstr.size = shdr[sh->sh_link].sh_size;
            dynstr_data = load_data(fd, shdr[sh->sh_link].sh_offset, dynstr.size);
            dynstr.data = dynstr_data;
            break;

        // Of each of these there is meant to be one.  Later copies are
        // ignored rather than leaked over the first.
        case SHT_GNU_verdef:
            if (v.verdef)
                break;

            v.verdef_size = sh->sh_size;
            v.verdef = load_data(fd, sh->sh_offset, sh->sh_size);
            break;
        case SHT_GNU_verneed:
            if (v.verneed)
                break;

            v.verneed_size = sh->sh_size;
            v.verneed = load_data(fd, sh->sh_offset, sh->sh_size);
            break;
        case SHT_GNU_versym:
            if (v.versym)
                break;

            v.nb_versyms = sh->sh_size / sizeof(ElfW(Half));
            v.versym = load_data(fd, sh->sh_offset, sh->sh_size);
            break;
        }
    }

    if (!dynamic || !dynsym || !dynstr.data || !dynstr.size
        || dynstr.data[dynstr.size - 1] != '\0') {
        tli_error_noabort("shared library has no usable dynamic section");
        goto the_end;
    }

    // What the library calls itself, which is the name that goes into the
    // output's DT_NEEDED; the file it was found in may be a symlink.
    soname = tli_basename(filename);
    for (i = 0, dt = dynamic; i < nb_dts; i++, dt++)
        if (dt->d_tag == DT_SONAME) {
            const char *s = str_at(&dynstr, dt->d_un.d_val);

            if (s)
                soname = s;
        }

    if (tli_add_dllref(s1, soname, level)->found)
        goto ret_success; // already linked against

    // .gnu.version has one entry per dynamic symbol or it is meaningless.
    if (v.nb_versyms != nb_syms)
        tli_free(v.versym), v.versym = NULL;
    else
        store_version(s1, &v, &dynstr);

    // Every global symbol the library defines becomes one the link can bind
    // against, under the version the library gave it.
    for (i = 1, sym = dynsym + 1; i < nb_syms; i++, sym++) {
        const char *name;
        i32 sym_index;

        if (ELFW(ST_BIND)(sym->st_info) == STB_LOCAL)
            continue;

        name = str_at(&dynstr, sym->st_name);
        if (!name) {
            tli_error_noabort("shared library has a symbol with no name");
            goto the_end;
        }

        sym_index = set_elf_sym(s1->dynsymtab_section, sym->st_value,
                                sym->st_size, sym->st_info, sym->st_other,
                                sym->st_shndx, name);
        if (v.versym) {
            ElfW(Half) vsym = v.versym[i];

            // the top bit marks a hidden version; index 0 and 1 are the
            // local and global base versions and name nothing
            if ((vsym & 0x8000) == 0 && vsym > 0 && vsym < v.nb_local_ver)
                set_sym_version(s1, sym_index, v.local_ver[vsym]);
        }
    }

    // The libraries this one needs are deliberately not followed.  The
    // dynamic loader will find them at run time from the DT_NEEDED entries
    // the library already carries, and pulling them in here would put their
    // symbols in front of ones the link is meant to resolve elsewhere.

 ret_success:
    ret = 0;
 the_end:
    tli_free(dynstr_data);
    tli_free(dynsym);
    tli_free(dynamic);
    tli_free(shdr);
    tli_free(v.local_ver);
    tli_free(v.verdef);
    tli_free(v.verneed);
    tli_free(v.versym);
    return ret;
}

/// -------------------------------------------------------------------------
/// linker scripts
///
/// A distribution's libc.so is often not a library at all but a short GNU ld
/// script naming the files that make it up.  Only the file list commands are
/// understood here, which is all that such a stub uses.

/// A bare word, whose text is in the caller's buffer. Above 255 so that it
/// cannot collide with a single character token.
#define LD_TOK_NAME 256

/// The end of the script.
#define LD_TOK_EOF  (-1)

/// The next character of the script, or CH_EOF at its end.
static i32 ld_inp(tli_State *s1)
{
    i32 c = *s1->ld_p;

    if (c == 0)
        return CH_EOF;

    ++s1->ld_p;
    return c;
}

/// Put one character back, which is how a word learns where it ends.
static void ld_unget(tli_State *s1, i32 c)
{
    if (c != CH_EOF)
        --s1->ld_p;
}

/// What a bare word may contain.  These words are file names, paths and output
/// format names, so the set is wide: everything that is not whitespace, a
/// bracket or one of the separators the grammar below needs.
static i32 ld_is_name_char(i32 c)
{
    return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
        || (c >= '0' && c <= '9')
        || (c > 0 && strchr("/.-_+=$:\\~", c) != NULL);
}

/// The next token: LD_TOK_NAME with the word in 'name', LD_TOK_EOF at the end,
/// or the character itself for anything else.
static i32 ld_next(tli_State *s1, char *name, i32 name_size)
{
    char *q = name;
    i32 c;

    // Skip whatever separates one token from the next, comments included.
    for (;;) {
        c = ld_inp(s1);
        if (c == ' ' || c == '\t' || c == '\f' || c == '\v'
            || c == '\r' || c == '\n')
            continue;

        if (c == '/' && *s1->ld_p == '*') {
            i32 prev = 0;

            ++s1->ld_p;
            do {
                i32 ch = ld_inp(s1);

                if (ch == CH_EOF)
                    break;

                if (ch == '/' && prev == '*')
                    break;

                prev = ch;
            } while (1);
            continue;
        }

        break;
    }

    if (c == CH_EOF) {
        *q = '\0';
        return LD_TOK_EOF;
    }

    if (!ld_is_name_char(c)) {
        *q++ = c;
        *q = '\0';
        return c;
    }

    // A word, truncated rather than overflowed if it will not fit.
    do {
        if (q - name < name_size - 1)
            *q++ = c;

        c = ld_inp(s1);
    } while (ld_is_name_char(c));
    ld_unget(s1, c);
    *q = '\0';
    return LD_TOK_NAME;
}

/// Add one file the script named, the same way the command line would have.
static i32 ld_add_file(tli_State *s1, const char filename[])
{
    if (filename[0] == '-' && filename[1] == 'l')
        return tli_add_library(s1, filename + 2);

    if (CONFIG_SYSROOT[0] != '\0' || !IS_ABSPATH(filename)) {
        // A script written for the target's own root names absolute paths
        // that mean nothing here, so try the library path first.
        i32 ret = tli_add_dll(s1, tli_basename(filename), 0);

        if (ret != FILE_NOT_FOUND)
            return ret;
    }

    return tli_add_file_internal(s1, filename, AFF_PRINT_ERROR);
}

/// Did the files just added leave anything undefined?
static i32 new_undef_sym(tli_State *s1, i32 sym_offset)
{
    while (sym_offset < (i32)symtab_section->data_offset) {
        ElfW(Sym) *sym = (void *)(symtab_section->data + sym_offset);

        if (sym->st_shndx == SHN_UNDEF)
            return 1;

        sym_offset += sizeof(ElfW(Sym));
    }
    return 0;
}

/// INPUT ( file ... ), and the three commands that share its syntax.  GROUP
/// means the same but is searched again as long as the last pass left
/// something undefined, which is how a circular dependency between two static
/// libraries is resolved.
static i32 ld_add_file_list(tli_State *s1, const char *cmd)
{
    char filename[1024];
    u8 *start = s1->ld_p;
    i32 adding = cmd[0] == 'I' || cmd[0] == 'G' || cmd[0] == 'A';
    i32 t, sym_offset, ret = 0;

 repeat:
    s1->ld_p = start;
    sym_offset = symtab_section->data_offset;

    if (ld_next(s1, filename, sizeof filename) != '(')
        return tli_error_noabort("expected '(' after %s", cmd);

    for (;;) {
        t = ld_next(s1, filename, sizeof filename);
        if (t == ')')
            break;

        if (t == LD_TOK_EOF)
            return tli_error_noabort("unexpected end of file");

        if (t == ',')
            continue;

        if (t != LD_TOK_NAME)
            return tli_error_noabort("unexpected token '%c'", t);

        if (!strcmp(filename, "AS_NEEDED"))
            ret |= ld_add_file_list(s1, filename);
        else if (adding)
            ret |= !!ld_add_file(s1, filename);

        if (ret < 0)
            return ret;
    }

    if (cmd[0] == 'G' && ret == 0 && new_undef_sym(s1, sym_offset))
        goto repeat;

    return ret;
}

/// Interpret the subset of GNU ld script syntax that the stub libc.so files
/// shipped by the BSDs and by glibc are written in.
ST_FUNC i32 tli_load_ldscript(tli_State *s1, i32 fd)
{
    char cmd[64];
    u8 *text, *saved = s1->ld_p;
    i32 t, ret = 0, first = 1;

    s1->ld_p = text = (void *)tli_load_text(fd);
    for (;;) {
        t = ld_next(s1, cmd, sizeof cmd);
        if (t == LD_TOK_EOF)
            break;

        if (!strcmp(cmd, "INPUT") || !strcmp(cmd, "GROUP")) {
            ret |= ld_add_file_list(s1, cmd);
        } else if (!strcmp(cmd, "OUTPUT_FORMAT") || !strcmp(cmd, "TARGET")) {
            // Parsed for its brackets and then ignored: the output format is
            // the caller's decision, not the script's.
            ret |= ld_add_file_list(s1, cmd);
        } else if (first) {
            // Nothing recognised at the very start means this was never a
            // script, and the caller should say so about the file itself
            // rather than complain about its syntax.
            ret = FILE_NOT_RECOGNIZED;
        } else {
            ret = tli_error_noabort("unexpected '%s'", cmd);
        }

        if (ret < 0)
            break;

        first = 0;
    }

    tli_free(text);
    s1->ld_p = saved;

    // ld_add_file_list() counts failures rather than returning one.
    return ret < 0 ? ret : -ret;
}


/// ---- src/core/tl_build.c ----

/// tinylinker - building an object from sections handed in by the caller
///
/// A code generator does not have to write a file for tinylinker to read: it
/// can create sections, fill them, define symbols in them and record what
/// needs relocating, then either write the result out as an object or link it
/// together with real files in the same step.
///
/// Relocations arrive in a target-neutral form and are translated here into
/// the architecture's ELF relocation type, which is the internal currency
/// whatever the output format ends up being.

/// The section kinds, indexed by the numbers tinylinker.h gives them.
/// Entry zero is a placeholder so that the kind itself can be the index.
static const struct {
    const char *name;  /// Default section name, NULL where the target picks it.
    i32 sh_type;       /// SHT_PROGBITS, or SHT_NOBITS for .bss.
    i32 sh_flags;      /// SHF_ bits, unused for the read-only kind.
} tl_sec_kinds[] = {
    { NULL, 0, 0 },
    { ".text",   SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR },
    { NULL,      SHT_PROGBITS, 0 },              /// rodata; name filled below
    { ".data",   SHT_PROGBITS, SHF_ALLOC | SHF_WRITE },
    { ".bss",    SHT_NOBITS,   SHF_ALLOC | SHF_WRITE },

    /// Debug information. No SHF_ALLOC: it is in the file and never in
    /// the image, and a linker asked to load it reports the sections as
    /// overlapping, since they all claim to start at zero.
    { NULL,      SHT_PROGBITS, 0 },

    /// Thread-local images. Mach-O spells these as the names its reader
    /// gives __thread_data and __thread_bss, which its writer turns back.
    { ".tdata",  SHT_PROGBITS, SHF_ALLOC | SHF_WRITE | SHF_TLS },
    { ".tbss",   SHT_NOBITS,   SHF_ALLOC | SHF_WRITE | SHF_TLS },
};

/// Find or create a section of one of the kinds and return its index.
/// Asking twice for the same name gives the same section back, so a generator
/// can emit into it in as many pieces as it likes.
ST_FUNC i32 tl_build_add_section(tli_State *s1, const char *name, i32 kind,
                                 i32 align)
{
    tli_Section *s;
    i32 i, sh_type, sh_flags;

    if (kind < 1 || kind > 7)
        return tli_error_noabort("unknown section kind %d", kind), 0;

    // The read-only kind is the one whose name and flags the target chooses:
    // where relro applies it goes somewhere other than .rodata.
    if (!name && kind >= 6 && s1->fmt == TLI_FMT_MACHO)
        name = kind == 6 ? ".thread_data" : ".thread_bss";
    else if (!name)
        name = kind == 2 ? rdata : tl_sec_kinds[kind].name;

    sh_type = tl_sec_kinds[kind].sh_type;
    sh_flags = kind == 2 ? shf_RELRO : tl_sec_kinds[kind].sh_flags;

    // A COFF thread-local is plain writable data in .tls$, which the C
    // runtime's TLS directory spans and the loader copies for each thread.
    // The template has no zero-filled part a member can claim, so the
    // zeroed kind carries its zeroes too.
    if (kind >= 6 && s1->fmt == TLI_FMT_PE) {
        name = ".tls$";
        sh_type = SHT_PROGBITS;
        sh_flags = SHF_ALLOC | SHF_WRITE;
    }

    for (i = 1; i < s1->nb_sections; i++)
        if (0 == strcmp(s1->sections[i]->name, name))
            goto found;

    s = new_section(s1, name, sh_type, sh_flags);
    i = s->sh_num;
found:
    s = s1->sections[i];

    // the section ends up as strict as the strictest thing put into it
    if (align > s->sh_addralign)
        s->sh_addralign = align;

    return i;
}

/// Turn a section index from tl_build_add_section() back into a section, or
/// NULL if the caller made it up.  Index zero is the null section, which is
/// never one a caller may write to.
static tli_Section *sec_of(tli_State *s1, i32 sec)
{
    if (sec < 1 || sec >= s1->nb_sections)
        return NULL;

    return s1->sections[sec];
}

/// Append bytes to a section and return the offset they landed at, which is
/// what a relocation against them is measured from.  A SHT_NOBITS section
/// takes the space without the data.
ST_FUNC long tl_build_append(tli_State *s1, i32 sec, const void *data,
                             usize len)
{
    tli_Section *s = sec_of(s1, sec);
    usize off;

    if (!s)
        return tli_error_noabort("no such section"), -1;

    off = section_add(s, len, s->sh_addralign ? s->sh_addralign : 1);
    if (len && s->sh_type != SHT_NOBITS)
        memcpy(s->data + off, data, len);

    return (long)off;
}

/// Reserve 'len' zero bytes, for a variable with no initialiser.  In .bss this
/// costs nothing in the output; elsewhere the zeroes are written out.
ST_FUNC long tl_build_zero(tli_State *s1, i32 sec, usize len)
{
    tli_Section *s = sec_of(s1, sec);
    usize off;

    if (!s)
        return tli_error_noabort("no such section"), -1;

    off = section_add(s, len, s->sh_addralign ? s->sh_addralign : 1);
    if (len && s->sh_type != SHT_NOBITS)
        memset(s->data + off, 0, len);

    return (long)off;
}

/// How much has been put into a section so far, or -1 if there is no such
/// section.  This is also the offset the next append will return.
ST_FUNC long tl_build_size(tli_State *s1, i32 sec)
{
    tli_Section *s = sec_of(s1, sec);
    return s ? (long)s->data_offset : -1;
}

/// Define a symbol at an offset in a section.  'flags' is the bit set
/// tinylinker.h documents: global, weak, and whether the symbol is a function.
ST_FUNC i32 tl_build_define(tli_State *s1, const char *name, i32 sec,
                            unsigned long long off, i32 flags)
{
    tli_Section *s = sec_of(s1, sec);
    i32 bind, type;

    if (!s)
        return tli_error_noabort("no such section");

    // A symbol in a thread-local section names an offset into each
    // thread's copy rather than an address, which ELF says with its type.
    bind = (flags & 0x02) ? STB_WEAK
         : (flags & 0x01) ? STB_GLOBAL : STB_LOCAL;
    type = (flags & 0x04) ? STT_FUNC
         : (s->sh_flags & SHF_TLS) ? STT_TLS : STT_OBJECT;
    set_elf_sym(symtab_section, (addr_t)off, 0, ELFW(ST_INFO)(bind, type), 0,
                s->sh_num, name);
    return s1->nb_errors ? -1 : 0;
}

/// Announce a symbol this object refers to but does not define, so that the
/// link goes looking for it elsewhere.
ST_FUNC i32 tl_build_declare(tli_State *s1, const char *name)
{
    set_elf_sym(symtab_section, 0, 0, ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
                SHN_UNDEF, name);
    return s1->nb_errors ? -1 : 0;
}

/// Markers for the relocations only COFF has, placed above every real
/// relocation number so they cannot collide with one.
#define TLI_REL_SECREL32 0x1000
#define TLI_REL_SECTION  0x1001

/// Translate a neutral relocation kind into this target's ELF type, which the
/// architecture's build_reloc table gives.  Returns -1, having reported it,
/// for a kind this architecture cannot express.
static i32 tl_build_reloc_type(tli_State *s1, i32 kind)
{
    if ((u32)kind < countof(s1->arch->build_reloc)
        && s1->arch->build_reloc[kind])
        return s1->arch->build_reloc[kind];

    // The two COFF-only kinds have no ELF counterpart to be spelled as,
    // so they travel as markers above every real type and are translated
    // where they mean something. Any other writer refuses them.
    if (kind == 10) return TLI_REL_SECREL32;
    if (kind == 11) return TLI_REL_SECTION;

    tli_error_noabort("relocation kind %d has no equivalent on this target",
                      kind);
    return -1;
}

/// The STT_SECTION symbol standing for a section, made the first time one is
/// asked for.
///
/// A section has no name in the symbol table until something wants to refer
/// to it, and all three formats express such a reference through a symbol of
/// this kind: it is local, has no name of its own, and carries the section in
/// st_shndx.  The COFF and Mach-O writers already turn one into the form
/// their format uses, so there is nothing further to translate.
static i32 tl_build_section_sym(tli_Section *s)
{
    tli_State *s1 = s->s1;

    if (!s->sect_sym)
        s->sect_sym = put_elf_sym(symtab_section, 0, 0,
                                  ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0,
                                  s->sh_num, NULL);

    return s->sect_sym;
}

/// Record that the bytes at 'off' in a section have to be patched with the
/// address of another section.  DWARF is what wants it: a compile unit names
/// its line table by an offset from the front of .debug_line, and there is no
/// symbol there to name instead.
ST_FUNC i32 tl_build_section_reloc(tli_State *s1, i32 sec,
                                   unsigned long long off, i32 target,
                                   i32 kind, long long addend)
{
    tli_Section *s = sec_of(s1, sec);
    tli_Section *t = sec_of(s1, target);
    i32 type;

    if (!s || !t)
        return tli_error_noabort("no such section");

    // Only the kinds that put an address, or an offset within a section,
    // into a field. The pc-relative and paged ones describe an instruction,
    // and an instruction is not what refers to a section this way.
    if (kind != 1 && kind != 2 && kind != 10)
        return tli_error_noabort("a relocation naming a section is absolute "
                                 "or section relative, not kind %d", kind);

    if (kind == 10 && s1->fmt != TLI_FMT_PE)
        return tli_error_noabort("a section relative relocation is a COFF "
                                 "form and has no equivalent here");

    type = tl_build_reloc_type(s1, kind);
    if (type < 0)
        return -1;

    put_elf_reloca(symtab_section, s, (u64)off, type,
                   tl_build_section_sym(t), (addr_t)addend);
    return s1->nb_errors ? -1 : 0;
}

/// Record that the bytes at 'off' in a section have to be patched with the
/// address of 'sym'.  A symbol not seen yet is declared undefined here, so a
/// relocation may be recorded before what it names is defined.
/// A local symbol by name, or zero.
///
/// Locals are appended rather than hashed, so this is a walk. It runs
/// only when the hash has already missed, and a symbol table this side
/// of a link holds what one object defines rather than what a program
/// does.
static i32 tl_build_find_local(tli_State *s1, const char *name)
{
    tli_Section *s = symtab_section;
    i32 count = (i32)(s->data_offset / sizeof(ElfW(Sym)));
    i32 i;

    for (i = 1; i < count; i++) {
        ElfW(Sym) *sym = (ElfW(Sym) *)s->data + i;

        if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL)
            continue;
        if (sym->st_shndx == SHN_UNDEF)
            continue;
        if (!strcmp(name, (char *)s->link->data + sym->st_name))
            return i;
    }

    return 0;
}

ST_FUNC i32 tl_build_reloc(tli_State *s1, i32 sec, unsigned long long off,
                           const char *sym, i32 kind, long long addend)
{
    tli_Section *s = sec_of(s1, sec);
    i32 type, sym_index;

    if (!s)
        return tli_error_noabort("no such section");

    type = tl_build_reloc_type(s1, kind);
    if (type < 0)
        return -1;

    sym_index = find_elf_sym(symtab_section, sym);

    // A local definition is not in the hash, because a local name means
    // nothing outside its object and nothing looks one up across a link.
    // A relocation inside the same object does: RISC-V names a label on
    // the AUIPC from the instruction that completes it, and taking the
    // hash miss at face value defined that label a second time as
    // undefined, which every linker but this one then refused.
    if (!sym_index)
        sym_index = tl_build_find_local(s1, sym);

    if (!sym_index)
        sym_index = set_elf_sym(symtab_section, 0, 0,
                                ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
                                SHN_UNDEF, sym);

    // A thread-local relocation names a thread-local symbol, and an ELF
    // linker refuses an undefined one that does not say so.
    if (kind >= 12 && kind <= 14) {
        ElfW(Sym) *es = (ElfW(Sym) *)symtab_section->data + sym_index;

        if (es->st_shndx == SHN_UNDEF)
            es->st_info = ELFW(ST_INFO)(ELFW(ST_BIND)(es->st_info), STT_TLS);
    }

    put_elf_reloca(symtab_section, s, (u64)off, type, sym_index,
                   (addr_t)addend);
    return s1->nb_errors ? -1 : 0;
}


/// ---- src/core/tl_settings.c ----

/// tinylinker - typed settings
///
/// The same knobs the option parser reaches through text, reachable directly.
/// An embedder that already has its own command line translates into these
/// rather than building strings for tl_set_options() to take apart again.

/// Set or clear the flags named by a bit mask.  Most map one to one onto state
/// fields, so setting and clearing are the same code with a different value;
/// the rest are spelled out below.
ST_FUNC i32 tl_settings_flags(tli_State *s1, u32 flags, i32 on)
{
    u8 v = on ? 1 : 0;

    if (flags & (1u << 0))  s1->static_link = v;
    if (flags & (1u << 3))  s1->rdynamic = v;
    if (flags & (1u << 4))  s1->symbolic = v;
    if (flags & (1u << 5))  s1->nostdlib = v;
    if (flags & (1u << 6))  s1->nostdlib_paths = v;
    if (flags & (1u << 8))  s1->enable_new_dtags = v;
    if (flags & (1u << 9))  s1->znodelete = v;
    if (flags & (1u << 11)) s1->leading_underscore = v;

    // Two of them are the inverse of how the state stores them.
    if (flags & (1u << 10)) s1->nocommon = !v;

    // Three select an output type rather than setting a switch.
    if (flags & (1u << 1))
        s1->output_type = on ? TLI_OUTPUT_DLL : TLI_OUTPUT_EXE;

    if (flags & (1u << 2)) {
        s1->option_r = v;
        s1->output_type = on ? TLI_OUTPUT_OBJ : TLI_OUTPUT_EXE;
    }

    // And one rides on the file type of whatever is added next.
    if (flags & (1u << 7)) {
        if (on)
            s1->filetype |= AFF_WHOLE_ARCHIVE;
        else
            s1->filetype &= ~AFF_WHOLE_ARCHIVE;
    }

#ifdef TLI_BACKTRACE
    // a backtrace is unreadable without the debug information behind it
    if (flags & (1u << 12)) {
        s1->do_backtrace = v;
        if (on && !s1->do_debug)
            s1->do_debug = 1;
    }
#endif

#ifdef TLI_BCHECK
    // and bounds checking reports through the backtrace
    if (flags & (1u << 13)) {
        s1->do_bounds_check = v;
        if (on) {
            s1->do_backtrace = 1;
            if (!s1->do_debug)
                s1->do_debug = 1;
        }
    }
#endif
    return 0;
}

/// Read the flags back as the mask tl_settings_flags() takes, so that an
/// embedder can save and restore a state's switches.
ST_FUNC u32 tl_settings_get_flags(tli_State *s1)
{
    u32 f = 0;

    if (s1->static_link)       f |= 1u << 0;
    if (s1->output_type == TLI_OUTPUT_DLL) f |= 1u << 1;
    if (s1->option_r)          f |= 1u << 2;
    if (s1->rdynamic)          f |= 1u << 3;
    if (s1->symbolic)          f |= 1u << 4;
    if (s1->nostdlib)          f |= 1u << 5;
    if (s1->nostdlib_paths)    f |= 1u << 6;
    if (s1->filetype & AFF_WHOLE_ARCHIVE) f |= 1u << 7;
    if (s1->enable_new_dtags)  f |= 1u << 8;
    if (s1->znodelete)         f |= 1u << 9;
    if (!s1->nocommon)         f |= 1u << 10;
    if (s1->leading_underscore) f |= 1u << 11;
    if (s1->do_backtrace)      f |= 1u << 12;
#ifdef TLI_BCHECK
    if (s1->do_bounds_check)   f |= 1u << 13;
#endif
    return f;
}

/// Where each string setting lives.  Returning the slot keeps setting and
/// reading from drifting apart.  NULL means this target has no such setting.
static char **tl_settings_str_slot(tli_State *s1, i32 opt)
{
    switch (opt) {
    case 1: return &s1->elf_entryname;
    case 2: return &s1->soname;
    case 3: return &s1->rpath;
    case 4: return &s1->elfint;
    case 5: return &s1->outfile;
    case 6: return &s1->init_symbol;
    case 7: return &s1->fini_symbol;
    case 8: return &s1->mapfile;
    case 9: return s1->fmt == TLI_FMT_MACHO ? &s1->install_name : NULL;
    case 10: return &s1->tli_lib_path;
    default: return NULL;
    }
}

/// Replace a string setting with a copy of 'value', or with nothing when it is
/// NULL.  The state owns what it is given.
ST_FUNC i32 tl_settings_set_str(tli_State *s1, i32 opt, const char *value)
{
    char **slot = tl_settings_str_slot(s1, opt);

    if (!slot)
        return tli_error_noabort("setting %d does not apply to this target", opt);

    tli_free(*slot);
    *slot = value ? tli_strdup(value) : NULL;
    return 0;
}

/// Read a string setting.  The pointer belongs to the state and lasts until
/// the setting is changed or the state is deleted.
ST_FUNC const char *tl_settings_get_str(tli_State *s1, i32 opt)
{
    char **slot = tl_settings_str_slot(s1, opt);
    return slot ? *slot : NULL;
}

/// The output format a numeric setting belongs to, or -1 for one that every
/// format has.  The others do not apply to a link in any other format.
static i32 tl_settings_num_fmt(i32 opt)
{
    switch (opt) {
    case 3: case 4: case 5:
        return TLI_FMT_PE;
    case 6: case 7: case 10: case 11:
        return TLI_FMT_MACHO;
    default:
        return -1;
    }
}

/// Set a numeric setting.  Unlike the strings these live in fields of several
/// widths, so each one narrows the value itself rather than sharing a slot.
ST_FUNC i32 tl_settings_set_num(tli_State *s1, i32 opt, unsigned long long v)
{
    i32 fmt = tl_settings_num_fmt(opt);

    if (fmt >= 0 && fmt != s1->fmt)
        return tli_error_noabort("setting %d does not apply to this target", opt);

    switch (opt) {
    case 1:
        s1->text_addr = (addr_t)v;
        s1->has_text_addr = 1;
        return 0;
    case 2:
        s1->section_align = (u32)v;
        return 0;
    case 3: s1->pe_imagebase = (addr_t)v; return 0;
    case 4: s1->pe_stack_size = (u32)v; return 0;
    case 5: s1->pe_subsystem = (i32)v; return 0;
    case 6: s1->compatibility_version = (u32)v; return 0;
    case 7: s1->current_version = (u32)v; return 0;
    case 8:
        s1->verbose = (u8)v;
        return 0;
#ifdef TLI_BACKTRACE
    case 9: s1->rt_num_callers = (i32)v; return 0;
#endif
    case 10: s1->macho_platform = (u32)v; return 0;
    case 11: s1->macho_minos = (u32)v; return 0;

    // Every backend accepts this; the ones that do not read it simply never
    // look at it.  An unknown number is refused so that a caller passing a
    // value from a newer header hears about it.
    case 12:
        if (v >= TLI_OS__COUNT)
            return tli_error_noabort("unknown target OS %llu", v);

        s1->target_os = (u8)v;
        return 0;

    // Every writer accepts this.  Only one of the three formats has anything
    // to drop -- the ELF and PE images carry no static symbol table to begin
    // with -- so on the others it is a promise that was already being kept.
    case 13:
        s1->strip = v != 0;
        return 0;
    default:
        break;
    }
    return tli_error_noabort("setting %d does not apply to this target", opt);
}

/// Read a numeric setting.  An option this target does not have reads as zero
/// rather than as an error, since there is no value to distinguish.
ST_FUNC unsigned long long tl_settings_get_num(tli_State *s1, i32 opt)
{
    i32 fmt = tl_settings_num_fmt(opt);

    if (fmt >= 0 && fmt != s1->fmt)
        return 0;

    switch (opt) {
    case 1: return s1->text_addr;
    case 2: return s1->section_align;
    case 3: return s1->pe_imagebase;
    case 4: return s1->pe_stack_size;
    case 5: return s1->pe_subsystem;
    case 6: return s1->compatibility_version;
    case 7: return s1->current_version;
    case 8: return s1->verbose;
#ifdef TLI_BACKTRACE
    case 9: return s1->rt_num_callers;
#endif
    case 10: return s1->macho_platform;
    case 11: return s1->macho_minos;
    case 12: return s1->target_os;
    case 13: return s1->strip;
    default: return 0;
    }
}

#if PTR_SIZE == 8

/// ---- src/core/x86_64-link.c ----

/// tinylinker - x86-64 relocation processing
///
/// Written from the System V Application Binary Interface, AMD64 Architecture
/// Processor Supplement, and from Ulrich Drepper's "ELF Handling For
/// Thread-Local Storage" for the shapes of the TLS access sequences.
///
/// x86-64 objects use Elf64_Rela, so the addend arrives in r_addend and the
/// caller has already folded it into val for everything that is defined as
/// S + A.  The few relocations whose formula uses A without S read r_addend
/// directly and say so.  Threads use variant II TLS: the thread pointer sits
/// at the end of the static block, so local-exec offsets are negative.
///
/// The 32 bit fields are the interesting part of this architecture.  The
/// small code model promises that every address and every displacement fits
/// in 32 bits, and a link that outgrows it produces a branch or a load that
/// silently lands somewhere else.  Every such field is range checked here.


/// ------------------------------------------------ relocation properties
///
/// What each x86-64 relocation type patches and what it needs built for it.
static const u8 x86_64_reloc_class[R_X86_64_NUM] = {
    [R_X86_64_NONE]          = RC_DATA(NO_GOTPLT_ENTRY),
    /// The absolute forms would need nothing but an address, except that code
    /// generators reach functions through them too, so a slot may still be
    /// wanted when the symbol turns out to be undefined: see the caller.
    [R_X86_64_64]            = RC_DATA(AUTO_GOTPLT_ENTRY),
    [R_X86_64_32]            = RC_DATA(AUTO_GOTPLT_ENTRY),
    [R_X86_64_32S]           = RC_DATA(AUTO_GOTPLT_ENTRY),
    [R_X86_64_PC32]          = RC_INSN(AUTO_GOTPLT_ENTRY),
    [R_X86_64_PC64]          = RC_INSN(AUTO_GOTPLT_ENTRY),
    [R_X86_64_PLT32]         = RC_INSN(ALWAYS_GOTPLT_ENTRY),
    [R_X86_64_PLTOFF64]      = RC_INSN(ALWAYS_GOTPLT_ENTRY),
    [R_X86_64_COPY]          = RC_DATA(NO_GOTPLT_ENTRY),
    [R_X86_64_GLOB_DAT]      = RC_DATA(NO_GOTPLT_ENTRY),
    [R_X86_64_JUMP_SLOT]     = RC_INSN(NO_GOTPLT_ENTRY),
    [R_X86_64_RELATIVE]      = RC_DATA(NO_GOTPLT_ENTRY),
    [R_X86_64_GOT32]         = RC_DATA(ALWAYS_GOTPLT_ENTRY),
    [R_X86_64_GOT64]         = RC_DATA(ALWAYS_GOTPLT_ENTRY),
    [R_X86_64_GOTPC32]       = RC_DATA(ALWAYS_GOTPLT_ENTRY),
    [R_X86_64_GOTPC64]       = RC_DATA(ALWAYS_GOTPLT_ENTRY),
    [R_X86_64_GOTOFF64]      = RC_DATA(ALWAYS_GOTPLT_ENTRY),
    [R_X86_64_GOTPCREL]      = RC_DATA(ALWAYS_GOTPLT_ENTRY),
    /// The relaxable forms of GOTPCREL.  A linker is allowed to rewrite the
    /// load into a lea and drop the slot; this one does not, so they behave
    /// exactly as the unrelaxed form.
    [R_X86_64_GOTPCRELX]     = RC_DATA(ALWAYS_GOTPLT_ENTRY),
    [R_X86_64_REX_GOTPCRELX] = RC_DATA(ALWAYS_GOTPLT_ENTRY),
    /// The dynamic TLS sequences are relaxed to local exec below, but the GOT
    /// has to exist before that decision is reachable.
    [R_X86_64_TLSGD]         = RC_DATA(ALWAYS_GOTPLT_ENTRY),
    [R_X86_64_TLSLD]         = RC_DATA(ALWAYS_GOTPLT_ENTRY),
    [R_X86_64_DTPOFF32]      = RC_DATA(ALWAYS_GOTPLT_ENTRY),
    [R_X86_64_DTPOFF64]      = RC_DATA(ALWAYS_GOTPLT_ENTRY),
    /// Initial exec wants a GOT slot holding the offset; only the GOT itself
    /// is built here.
    [R_X86_64_GOTTPOFF]      = RC_DATA(ALWAYS_GOTPLT_ENTRY),
    /// Local exec needs nothing at all: the offset is known now.
    [R_X86_64_TPOFF32]       = RC_DATA(NO_GOTPLT_ENTRY),
    [R_X86_64_TPOFF64]       = RC_DATA(NO_GOTPLT_ENTRY),
};

/// The table entry for a type, or zero for anything out of range, which is
/// how an unknown relocation reaches the callers as "not known here".
static u32 x86_64_reloc_props(i32 reloc_type)
{
    return (u32)reloc_type < R_X86_64_NUM ? x86_64_reloc_class[reloc_type] : 0;
}


/// ------------------------------------------------------ procedure linkage
///
/// Sixteen bytes per entry, as on i386, and the same lazy binding shape:
/// entry zero pushes the link map word at GOT+8 and jumps through the
/// resolver at GOT+16, and every other entry jumps through its own GOT slot,
/// which starts out pointing at the push that follows it.
///
/// Unlike i386 there is only one form.  Every GOT reference is written RIP
/// relative, so a position-independent PLT needs no register set up, and the
/// displacements are fixed up in x86_64_relocate_plt() once the two section
/// addresses are known.  The other difference is the argument the entry
/// pushes: x86-64 names its relocation by index into .rela.plt rather than by
/// byte offset.
#define X86_64_GRP5      0xff  /// Group 5: /4 jumps indirect, /6 pushes.
#define X86_64_PUSH_IMM  0x68
#define X86_64_JMP_REL   0xe9

/// mod=00 reg=/4 rm=101 is RIP relative on x86-64; adding 0x10 moves the reg
/// field to /6, turning the jump into a push.
#define X86_64_MODRM_RIP        0x25
#define X86_64_MODRM_PUSH(m)    ((m) + 0x10)

/// Fixed by the ABI, which is what lets the resolver index the table.
#define X86_64_PLT_ENTRY_SIZE 16

/// Append a PLT entry reaching the GOT slot at 'got_offset' and return where
/// in .plt it landed.
static u32 x86_64_create_plt_entry(tli_State *s1, u32 got_offset, struct tli_SymAttr *attr)
{
    tli_Section *plt = s1->plt;
    u32 plt_offset, relofs;
    u8 *p;

    (void)attr;

    // Entry zero, the resolver trampoline, is built the first time anything
    // asks for an entry at all.
    if (plt->data_offset == 0) {
        p = section_ptr_add(plt, X86_64_PLT_ENTRY_SIZE);
        p[0] = X86_64_GRP5;             // push GOT + 8
        p[1] = X86_64_MODRM_PUSH(X86_64_MODRM_RIP);
        write32le(p + 2, PTR_SIZE);
        p[6] = X86_64_GRP5;             // jmp *(GOT + 16)
        p[7] = X86_64_MODRM_RIP;
        write32le(p + 8, PTR_SIZE * 2);
    }

    plt_offset = plt->data_offset;

    // The entry names the relocation the resolver should apply, and on x86-64
    // it names it by index.  The entry this slot needs is the one the caller
    // is about to append, so it is the last index in the table as it stands.
    relofs = s1->plt->reloc ? s1->plt->reloc->data_offset : 0;

    p = section_ptr_add(plt, X86_64_PLT_ENTRY_SIZE);
    p[0] = X86_64_GRP5;                 // jmp *(GOT + got_offset)
    p[1] = X86_64_MODRM_RIP;
    write32le(p + 2, got_offset);
    p[6] = X86_64_PUSH_IMM;
    write32le(p + 7, relofs / sizeof (ElfW_Rel) - 1);
    p[11] = X86_64_JMP_REL;             // jmp PLT[0]
    write32le(p + 12, -(i32)plt->data_offset);
    return plt_offset;
}

/// Finish the PLT once the addresses of .plt and .got are settled: turn the
/// GOT offsets left in the entries into RIP relative displacements, and point
/// each GOT slot at the push in its own entry so the first call reaches the
/// resolver rather than the function.
static void x86_64_relocate_plt(tli_State *s1)
{
    u8 *p, *p_end;

    if (!s1->plt)
        return;

    p = s1->plt->data;
    p_end = p + s1->plt->data_offset;

    if (p < p_end) {
        // Displacements are measured from the end of the instruction.  The
        // two in entry zero are six and twelve bytes in, each six bytes long,
        // so they see the GOT base shifted by their own position.
        i32 x = s1->got->sh_addr - s1->plt->sh_addr - 6;

        add32le(p + 2, x);
        add32le(p + 8, x - 6);
        for (p += X86_64_PLT_ENTRY_SIZE; p < p_end; p += X86_64_PLT_ENTRY_SIZE)
            add32le(p + 2, x + (s1->plt->data - p));
    }

    // One .rela.plt entry per slot, in the order the entries were created.
    if (s1->plt->reloc) {
        ElfW_Rel *rel;

        // The push sits six bytes into an entry, and entry zero is the
        // resolver trampoline, so the first real entry starts at 16.
        addr_t resume = s1->plt->sh_addr + X86_64_PLT_ENTRY_SIZE + 6;

        p = s1->got->data;
        for_each_elem(s1->plt->reloc, 0, rel, ElfW_Rel) {
            write64le(p + rel->r_offset, resume);
            resume += X86_64_PLT_ENTRY_SIZE;
        }
    }
}

/// Offset of a thread-local symbol from the thread pointer.  Variant II puts
/// the thread pointer past the end of the static TLS block, so the result is
/// negative.  Before the program headers are laid out there is no recorded
/// end, in which case the symbol's own section is the whole of the block.
static i32 x86_64_tpoff(tli_State *s1, i32 sym_index, addr_t value)
{
    tli_Section *sec;

    if (s1->tls_end)
        return (i32)(value - s1->tls_end);

    sec = s1->sections[tli_sym_section(symtab_section, sym_index)];
    return (i32)(value - sec->sh_addr - sec->data_offset);
}

/// Whether a relocation names an undefined weak symbol.  It resolves to
/// address zero, which no PE image is near enough to reach, and the source is
/// expected to have guarded it with a null test.
static i32 x86_64_weak_undef(tli_State *s1, ElfW_Rel *rel)
{
    ElfW(Sym) *sym =
        &((ElfW(Sym) *)symtab_section->data)[ELFW(R_SYM)(rel->r_info)];

    return sym->st_shndx == SHN_UNDEF
           && ELFW(ST_BIND)(sym->st_info) == STB_WEAK;
}

/// Apply one relocation of type 'type' to the field at 'ptr', which will live
/// at address 'addr' and refers to a symbol whose value is 'val'.
static void x86_64_relocate(tli_State *s1, ElfW_Rel *rel, i32 type, u8 *ptr, addr_t addr, addr_t val)
{
    i32 sym_index, esym_index;
    i64 disp;

    switch (type) {
    case R_X86_64_NONE:
        return;

    // S + A.  In a shared object the sum is not known until load time, so the
    // relocation is handed on: against an exported symbol it stays symbolic,
    // otherwise the loader need only add the load bias, and R_X86_64_RELATIVE
    // says so with the whole value as its addend.
    case R_X86_64_64:
        if (s1->output_type & TLI_OUTPUT_DYN) {
            sym_index = ELFW(R_SYM)(rel->r_info);
            esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
            qrel->r_offset = rel->r_offset;
            if (esym_index) {
                qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_64);
                qrel->r_addend = rel->r_addend;
                qrel++;
                return;
            }

            qrel->r_info = ELFW(R_INFO)(0, R_X86_64_RELATIVE);
            qrel->r_addend = read64le(ptr) + val;
            qrel++;
        }

        add64le(ptr, val);
        return;

    // S + A truncated to 32 bits: zero extended for R_X86_64_32, sign
    // extended for R_X86_64_32S.  The check is that the truncation loses
    // nothing, which is exactly what distinguishes the two.
    case R_X86_64_32:
    case R_X86_64_32S:
        if (s1->output_type & TLI_OUTPUT_DYN) {
            // A shared object cannot keep an absolute address, so the loader
            // is asked to add the bias.  Whatever sits in the field is part
            // of the addend and is signed.
            qrel->r_offset = rel->r_offset;
            qrel->r_info = ELFW(R_INFO)(0, R_X86_64_RELATIVE);
            qrel->r_addend = (i32)read32le(ptr) + val;
            qrel++;
        }

        if ((type == R_X86_64_32 ? (u64)val != (u32)val
                                 : (u64)val != (u64)(i64)(i32)val)
            // Debug information is not loaded and is allowed to be nonsense
            // rather than to fail the link.
            && (stab_section == NULL
                || addr < stab_section->sh_addr
                || addr >= stab_section->sh_addr + stab_section->data_offset)) {
            tli_error_noabort("relocation 'R_X86_64_32[S]' out of range");
        }

        add32le(ptr, val);
        return;

    // S + A - P.  A shared object cannot resolve a reference to a symbol it
    // does not define, so that one is passed to the loader.
    case R_X86_64_PC32:
        if (s1->output_type == TLI_OUTPUT_DLL) {
            sym_index = ELFW(R_SYM)(rel->r_info);
            esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
            if (esym_index) {
                qrel->r_offset = rel->r_offset;
                qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_PC32);
                qrel->r_addend = (i32)read32le(ptr) + rel->r_addend;
                qrel++;
                return;
            }
        }

        // L + A - P.  The caller has already pointed val at the PLT entry
        // where one was needed, so the arithmetic is that of PC32.
        // fall through
    case R_X86_64_PLT32:
        disp = (i64)val - (i64)addr;

        // A displacement past 32 bits is the small code model being outgrown,
        // which only shows up on links large enough to spread code that far.
        if (!signed_fits(disp, 32)) {
            // A weak undefined symbol resolves to zero, which no 32 bit
            // displacement can reach from a PE image base.  The reference is
            // guarded by a null test in the source, so let it stand.  A
            // strong one is a real overflow and still has to be reported.
            if (s1->fmt != TLI_FMT_PE || !x86_64_weak_undef(s1, rel))
                tli_error_noabort("relocation '%d' out of range", type);
        }

        add32le(ptr, disp);
        return;

    // S + A - P into a full width field, so nothing can overflow.
    case R_X86_64_PC64:
        if (s1->output_type == TLI_OUTPUT_DLL) {
            sym_index = ELFW(R_SYM)(rel->r_info);
            esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
            if (esym_index) {
                qrel->r_offset = rel->r_offset;
                qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_PC64);
                qrel->r_addend = read64le(ptr) + rel->r_addend;
                qrel++;
                return;
            }
        }

        add64le(ptr, val - addr);
        return;

    // Both are plain S.  val arrived with the addend already folded in, so
    // take it back out: a GOT slot holds the symbol and nothing else.
    case R_X86_64_GLOB_DAT:
    case R_X86_64_JUMP_SLOT:
        write64le(ptr, val - rel->r_addend);
        return;

    // G + GOT + A - P: the RIP relative displacement to the symbol's GOT
    // slot.  The addend is not read from r_addend but fixed at -4, which is
    // what a compiler emits for a load whose displacement is the last four
    // bytes of the instruction.  The stubs this linker builds for itself rely
    // on that: they leave r_addend at zero.
    case R_X86_64_GOTPCREL:
    case R_X86_64_GOTPCRELX:
    case R_X86_64_REX_GOTPCRELX:
        sym_index = ELFW(R_SYM)(rel->r_info);
        disp = (i64)(s1->got->sh_addr
                     + get_sym_attr(s1, sym_index, 0)->got_offset)
               - (i64)addr - 4;
        if (!signed_fits(disp, 32))
            tli_error_noabort("relocation '%d' out of range", type);

        add32le(ptr, disp);
        return;

    // GOT + A - P, which recovers the GOT base into a register.
    case R_X86_64_GOTPC32:
        disp = (i64)s1->got->sh_addr - (i64)addr + rel->r_addend;
        if (!signed_fits(disp, 32))
            tli_error_noabort("relocation 'R_X86_64_GOTPC32' out of range");

        add32le(ptr, disp);
        return;

    // The same, into a field wide enough that nothing can overflow.
    case R_X86_64_GOTPC64:
        add64le(ptr, s1->got->sh_addr - addr + rel->r_addend);
        return;

    // G + A: the offset of the slot within the GOT, not its address.
    case R_X86_64_GOT32:
    case R_X86_64_GOT64:
        sym_index = ELFW(R_SYM)(rel->r_info);
        if (type == R_X86_64_GOT32)
            add32le(ptr, get_sym_attr(s1, sym_index, 0)->got_offset);
        else
            add64le(ptr, get_sym_attr(s1, sym_index, 0)->got_offset);

        return;

    // S + A - GOT: the symbol addressed off the GOT base.
    case R_X86_64_GOTOFF64:
        add64le(ptr, val - s1->got->sh_addr);
        return;

    // L + A - GOT.  val is already L + A.
    case R_X86_64_PLTOFF64:
        add64le(ptr, val - s1->got->sh_addr);
        return;

    // The loader copies the initialiser out of the shared object at startup.
    // Nothing to write at link time.
    case R_X86_64_COPY:
        return;

    case R_X86_64_RELATIVE:
        // PE expresses the same thing against the image base.
        if (s1->fmt == TLI_FMT_PE)
            add32le(ptr, val - s1->pe_imagebase);

        return;

    // General dynamic, relaxed to local exec.  The compiler emits the
    // sequence below with padding prefixes precisely so that a linker can
    // overwrite it in place; the relocation names the displacement four bytes
    // in, so the sequence starts at ptr-4 and is sixteen bytes long.  With a
    // whole program there is one module, so the call can go: read the thread
    // pointer from %fs and add the symbol's offset, which is negative.
    case R_X86_64_TLSGD: {
        static const u8 expect[] = {
            0x66, 0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, 0x00,   // leaq x@tlsgd(%rip),%rdi
            0x66, 0x66, 0x48, 0xe8, 0x00, 0x00, 0x00, 0x00 }; // call __tls_get_addr@plt
        static const u8 replace[] = {
            0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0,%rax
            0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 };           // lea 0(%rax),%rax

        if (memcmp(ptr - 4, expect, sizeof expect) != 0) {
            tli_error_noabort("unexpected R_X86_64_TLSGD pattern");
            return;
        }

        sym_index = ELFW(R_SYM)(rel->r_info);
        memcpy(ptr - 4, replace, sizeof replace);

        // The call carried a relocation of its own; disarm it.
        rel[1].r_info = ELFW(R_INFO)(0, R_X86_64_NONE);

        // The addend of a TLSGD relocation belongs to the GOT reference that
        // has just been thrown away, so the offset is taken from the symbol
        // itself.  The displacement of the lea is twelve bytes into the
        // replacement, that is ptr+8.
        add32le(ptr + 8, x86_64_tpoff(s1, sym_index,
                    ((ElfW(Sym) *)symtab_section->data)[sym_index].st_value));
        return;
    }

    // Local dynamic, relaxed the same way.  The call would return the
    // module's TLS base in %rax and each access then adds its own
    // R_X86_64_DTPOFF32; with one module that base is the thread pointer.
    // Twelve bytes in, twelve bytes out.
    case R_X86_64_TLSLD: {
        static const u8 expect[] = {
            0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, 0x00,   // leaq x@tlsld(%rip),%rdi
            0xe8, 0x00, 0x00, 0x00, 0x00 };             // call __tls_get_addr@plt
        static const u8 replace[] = {
            0x66, 0x66, 0x66,                                       // data16 padding
            0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 }; // mov %fs:0,%rax

        if (memcmp(ptr - 3, expect, sizeof expect) != 0) {
            tli_error_noabort("unexpected R_X86_64_TLSLD pattern");
            return;
        }

        memcpy(ptr - 3, replace, sizeof replace);
        rel[1].r_info = ELFW(R_INFO)(0, R_X86_64_NONE);
        return;
    }

    // Initial exec: the displacement to a GOT slot holding the symbol's
    // offset from the thread pointer.  Both halves are done here -- the slot
    // is filled as well as reached -- because nothing else can fill it: a
    // relocation against the GOT is left for the loader, and the loader has
    // no way to know an offset into a block this linker laid out.  The addend
    // is fixed at -4 for the same reason GOTPCREL's is.
    case R_X86_64_GOTTPOFF: {
        ElfW(Sym) *tsym;
        u32 slot;

        sym_index = ELFW(R_SYM)(rel->r_info);
        slot = get_sym_attr(s1, sym_index, 0)->got_offset;

        // The symbol's own address, not val: the -4 in val is the addend for
        // the displacement below, and has no business in an offset from the
        // thread pointer.
        tsym = (ElfW(Sym) *)symtab_section->data + sym_index;
        write64le(s1->got->data + slot,
                  (u64)(i64)x86_64_tpoff(s1, sym_index, tsym->st_value));

        disp = (i64)(s1->got->sh_addr + slot) - (i64)addr - 4;

        if (!signed_fits(disp, 32))
            tli_error_noabort("relocation '%d' out of range", type);

        add32le(ptr, disp);
        return;
    }

    // After the relaxation above both mean the symbol's offset from the
    // thread pointer.
    case R_X86_64_DTPOFF32:
    case R_X86_64_TPOFF32:
        sym_index = ELFW(R_SYM)(rel->r_info);
        add32le(ptr, x86_64_tpoff(s1, sym_index, val));
        return;

    // The same offset into a full width field.
    case R_X86_64_DTPOFF64:
    case R_X86_64_TPOFF64:
        sym_index = ELFW(R_SYM)(rel->r_info);
        add64le(ptr, x86_64_tpoff(s1, sym_index, val));
        return;

    default:
        fprintf(stderr, "FIXME: handle reloc type %d at %x [%p] to %x\n",
                type, (u32)addr, ptr, (u32)val);
        return;
    }
}

/// What the generic code reads about this machine.  The DWARF form is the
/// zero-extending one, since a section offset is unsigned.  Every GOT
/// reference in a PLT entry is RIP relative, so the one PLT works whether or
/// not the image is position independent.
ST_DATA const tli_Arch tli_arch_x86_64 = {
    .em = EM_X86_64,
    .relocate = x86_64_relocate,
    .create_plt_entry = x86_64_create_plt_entry,
    .relocate_plt = x86_64_relocate_plt,
    .reloc_props = x86_64_reloc_props,
    .r_data_ptr = R_X86_64_64,
    .r_data_32dw = R_X86_64_32,
    .r_jmp_slot = R_X86_64_JUMP_SLOT,
    .r_glob_dat = R_X86_64_GLOB_DAT,
    .r_copy = R_X86_64_COPY,
    .r_relative = R_X86_64_RELATIVE,
    .start_addr = 0x400000,
    .page_size = 0x1000,
    .pcrel_dllplt = 1,
    .elf_interp = "/lib64/ld-linux-x86-64.so.2",
    .r_plt32 = R_X86_64_PLT32,
    .r_pcrel32 = R_X86_64_PC32,
    .build_reloc = {
        [1] = R_X86_64_64,
        [2] = R_X86_64_32,
        [3] = R_X86_64_PC32,
        [4] = R_X86_64_PLT32,
        [14] = R_X86_64_GOTTPOFF,
        [17] = R_X86_64_REX_GOTPCRELX,
    },
    .pe_machine = 0x8664,
    .pe_os_version = 0x0400,  // Windows NT 4.0
    .pe_rsrc_reltype = 3,     // IMAGE_REL_AMD64_ADDR32NB
    .pe_characteristics = PE_FILE_EXECUTABLE_IMAGE | PE_FILE_LARGE_ADDRESS_AWARE
                          | PE_FILE_LINE_NUMS_STRIPPED | PE_FILE_LOCAL_SYMS_STRIPPED
                          | PE_FILE_DEBUG_STRIPPED | PE_FILE_RELOCS_STRIPPED,
    .pe_image_base_exe = 0x0000000000400000ULL,
    .pe_image_base_dll = 0x0000000010000000ULL,
    .r_pe_abs = R_X86_64_64,
    .r_pe_rva = R_X86_64_RELATIVE,
    .r_pe_thunk = R_X86_64_PC32,
    .macho_cputype = 0x01000007,  // CPU_TYPE_X86_64
    .macho_cpusubtype = 3,  // CPU_SUBTYPE_X86_ALL
};


/// ---- src/core/arm64-link.c ----

/// tinylinker - AArch64 relocation processing
///
/// Written from ELF for the Arm 64-bit Architecture (AAELF64), which gives
/// every relocation an operation in terms of S, A, P and Page(x), and names
/// the instruction field it lands in.  The encodings of those fields are in
/// arm64-insn.h.
///
/// AArch64 objects use Elf64_Rela, so the addend arrives in r_addend and the
/// caller has already folded it into val.  Almost every relocation here
/// replaces a field rather than adding to it, which is what "insn" class
/// relocations mean in the ABI: the instruction is left alone apart from the
/// immediate.
///
/// Two things about this architecture shape the code below.  Addresses are
/// reached in two steps, an ADRP naming a 4KiB page and an ADD or a load
/// supplying the offset within it, so the range that matters for most code is
/// the +-4GiB an ADRP can express rather than the +-128MiB of a branch.  And
/// the branches themselves come in one shape with one bit distinguishing B
/// from BL, which matters more than it looks: see the JUMP26 case.


/// ---- src/core/arm64-insn.h ----


/// Opcodes with every variable field zeroed, so that the macros below can be
/// ored straight in.
#define ARM64_ADD_IMM     0x11000000U  /// 32-bit form; ARM64_SF(1) widens it.
#define ARM64_LDR_X       0xF9400000U  /// Unsigned offset form, scaled by eight.
#define ARM64_STP_X_PRE   0xA9800000U  /// Pre-indexed, so it writes the base back.
#define ARM64_BR          0xD61F0000U
#define ARM64_NOP         0xD503201FU
#define ARM64_ADRP        0x90000000U
#define ARM64_B           0x14000000U
#define ARM64_BL          0x94000000U
#define ARM64_MOVZ_X      0xD2800000U  /// Zeroes the rest of the register, unlike MOVK.

/// B and BL differ in one bit, so the pair can be recognised as a class.
#define ARM64_BRANCH_MASK 0x7C000000U

/// True for a B or a BL, which is how relocate() tells a real branch from the
/// placeholder in a linker-built stub.
#define ARM64_IS_BRANCH(insn) (((insn) & ARM64_BRANCH_MASK) == ARM64_B)

/// Register fields.  Rt and Rd occupy the same bits; both names are kept so
/// that a call site reads the way the manual writes the instruction.
#define ARM64_RD(r)     ((u32)(r) & 0x1FU)
#define ARM64_RN(r)     (((u32)(r) & 0x1FU) << 5)
#define ARM64_RT(r)     ((u32)(r) & 0x1FU)
#define ARM64_RT2(r)    (((u32)(r) & 0x1FU) << 10)

/// The signed offset of a pair load or store, counted in eight byte units.
#define ARM64_IMM7(v)   (((u32)(v) & 0x7FU) << 15)

/// The bit that picks the 64-bit form of a data processing instruction.
#define ARM64_SF(s)     (((u32)(s) & 1) << 31)

/// Branch offsets are in instructions, so the byte displacement is shifted
/// before it is encoded.
#define ARM64_IMM26(v)  ((u32)(v) & 0x03FFFFFFU)          /// b, bl
#define ARM64_IMM19(v)  (((u32)(v) & 0x7FFFFU) << 5)      /// b.cond, cbz
#define ARM64_IMM14(v)  (((u32)(v) & 0x3FFFU) << 5)       /// tbz, tbnz
#define ARM64_IMM16(v)  (((u32)(v) & 0xFFFFU) << 5)       /// movz, movk
#define ARM64_IMM12(v)  (((u32)(v) & 0xFFFU) << 10)       /// add #imm

/// ADRP names a 4KiB page, and splits the signed page count so that its low
/// two bits sit up at 30:29 and the rest at 23:5.
#define ARM64_ADRP_IMM(v) \
    ((((u32)(v) & 0x1FFFFCU) << 3) | (((u32)(v) & 3U) << 29))

/// The 12 bit offset of a load or store is scaled by the access size, so the
/// bits the scale implies are dropped rather than encoded.
#define ARM64_LO12(v, shift) ((((u32)(v) & 0xFFFU) >> (shift)) << 10)

/// What each field leaves behind: the bits of the instruction a relocation
/// writing that field must preserve.
#define ARM64_KEEP_IMM26 0xFC000000U
#define ARM64_KEEP_IMM19 0xFF00001FU
#define ARM64_KEEP_IMM14 0xFFF8001FU
#define ARM64_KEEP_IMM16 0xFFE0001FU
#define ARM64_KEEP_IMM12 0xFFC003FFU
#define ARM64_KEEP_ADRP  0x9F00001FU  /// Keeps the opcode and Rd across both imm halves.


/// ------------------------------------------------ relocation properties
///
/// The linker asks two questions of every relocation type: does the field it
/// patches hold code, and does the symbol need a GOT slot, a PLT entry, both
/// or neither.  Both answers are fixed by the ABI and neither depends on the
/// link, so they are stated once here rather than in two switches that have
/// to be kept in step.  The low bits carry an enum gotplt_entry; a zero byte
/// means the backend does not know the type at all, which the callers report.
///
/// What each static AArch64 relocation type patches and what it needs built
/// for it, indexed from R_AARCH64_ABS64 rather than from zero.
static const u8 arm64_reloc_class[R_AARCH64_LD64_GOT_LO12_NC
                            - R_AARCH64_ABS64 + 1] = {
/// Rebases one relocation type onto the start of the table.
#define RCE(t) [(t) - R_AARCH64_ABS64]
    /// Data words.  Code generators reach functions through these too, hence
    /// AUTO rather than NO.
    RCE(R_AARCH64_ABS64)                = RC_DATA(AUTO_GOTPLT_ENTRY),
    RCE(R_AARCH64_ABS32)                = RC_DATA(AUTO_GOTPLT_ENTRY),
    RCE(R_AARCH64_PREL32)               = RC_DATA(NO_GOTPLT_ENTRY),
    /// An address assembled a quarter at a time, for code that cannot rely on
    /// the PC being anywhere near its data.
    RCE(R_AARCH64_MOVW_UABS_G0_NC)      = RC_DATA(NO_GOTPLT_ENTRY),
    RCE(R_AARCH64_MOVW_UABS_G1_NC)      = RC_DATA(NO_GOTPLT_ENTRY),
    RCE(R_AARCH64_MOVW_UABS_G2_NC)      = RC_DATA(NO_GOTPLT_ENTRY),
    RCE(R_AARCH64_MOVW_UABS_G3)         = RC_DATA(NO_GOTPLT_ENTRY),
    /// The ADRP and its partner, the ordinary way to reach anything.
    RCE(R_AARCH64_ADR_PREL_PG_HI21)     = RC_DATA(NO_GOTPLT_ENTRY),
    RCE(R_AARCH64_ADD_ABS_LO12_NC)      = RC_DATA(NO_GOTPLT_ENTRY),
    RCE(R_AARCH64_LDST8_ABS_LO12_NC)    = RC_DATA(NO_GOTPLT_ENTRY),
    RCE(R_AARCH64_LDST16_ABS_LO12_NC)   = RC_DATA(NO_GOTPLT_ENTRY),
    RCE(R_AARCH64_LDST32_ABS_LO12_NC)   = RC_DATA(NO_GOTPLT_ENTRY),
    RCE(R_AARCH64_LDST64_ABS_LO12_NC)   = RC_DATA(NO_GOTPLT_ENTRY),
    RCE(R_AARCH64_LDST128_ABS_LO12_NC)  = RC_DATA(NO_GOTPLT_ENTRY),
    /// Branches.  The unconditional pair can reach a PLT entry, so they may
    /// ask for one; the short conditional forms cannot, and do not.
    RCE(R_AARCH64_JUMP26)               = RC_INSN(AUTO_GOTPLT_ENTRY),
    RCE(R_AARCH64_CALL26)               = RC_INSN(AUTO_GOTPLT_ENTRY),
    RCE(R_AARCH64_CONDBR19)             = RC_INSN(NO_GOTPLT_ENTRY),
    RCE(R_AARCH64_TSTBR14)              = RC_INSN(NO_GOTPLT_ENTRY),
    /// The GOT pair, which always wants a slot.
    RCE(R_AARCH64_ADR_GOT_PAGE)         = RC_DATA(ALWAYS_GOTPLT_ENTRY),
    RCE(R_AARCH64_LD64_GOT_LO12_NC)     = RC_DATA(ALWAYS_GOTPLT_ENTRY),
#undef RCE
};

/// The properties of a type: the dense table for the static range, and a
/// switch for the dynamic types that sit far above it.
static u32 arm64_reloc_props(i32 reloc_type)
{
    u32 i = (u32)reloc_type - R_AARCH64_ABS64;

    if (i < sizeof arm64_reloc_class)
        return arm64_reloc_class[i];

    switch (reloc_type) {
    // Local exec: the offset from the thread pointer is known now, so
    // nothing has to be built for it.  The NC form is the LO12 one without
    // the range check, which is not a distinction this linker makes.
    case R_AARCH64_TLSLE_ADD_TPREL_HI12:
    case R_AARCH64_TLSLE_ADD_TPREL_LO12:
    case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:

    // Global dynamic in its descriptor form, which is rewritten into the
    // local-exec sequence below.  Nothing is built for it: the GOT slot and
    // the resolver call it would otherwise want are what the rewrite removes.
    case R_AARCH64_TLSDESC_ADR_PAGE21:
    case R_AARCH64_TLSDESC_LD64_LO12:
    case R_AARCH64_TLSDESC_ADD_LO12:
    case R_AARCH64_TLSDESC_CALL:

    // Written by the linker, consumed by the loader.
    case R_AARCH64_COPY:
    case R_AARCH64_GLOB_DAT:
    case R_AARCH64_RELATIVE:
        return RC_DATA(NO_GOTPLT_ENTRY);

    case R_AARCH64_JUMP_SLOT:
        return RC_INSN(NO_GOTPLT_ENTRY);

    // Initial exec: the pair reaches a GOT slot holding the offset from the
    // thread pointer, so a slot has to exist for them to reach.
    case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
    case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
        return RC_DATA(ALWAYS_GOTPLT_ENTRY);
    }

    return 0;
}


/// An ADRP reaches a page, not an address, so the displacement it encodes is
/// the difference of the two page numbers and not the difference of the
/// addresses rounded.
static i64 arm64_page_delta(addr_t to, addr_t from)
{
    return (i64)(to >> 12) - (i64)(from >> 12);
}

/// Whether a relocation names an undefined weak symbol.  It resolves to
/// address zero, which no PE image is near enough to reach, and the source is
/// expected to have guarded it with a null test.
static i32 arm64_weak_undef(tli_State *s1, ElfW_Rel *rel)
{
    ElfW(Sym) *sym =
        &((ElfW(Sym) *)symtab_section->data)[ELFW(R_SYM)(rel->r_info)];

    return sym->st_shndx == SHN_UNDEF
           && ELFW(ST_BIND)(sym->st_info) == STB_WEAK;
}

/// A thread-local address as an offset from the thread pointer.  glibc puts
/// its tcbhead_t at the thread pointer and the thread's own TLS block after
/// it, so everything is sixteen bytes further on; PE puts nothing there.
static i64 arm64_tp_offset(tli_State *s1, addr_t v)
{
    return (i64)v - (i64)s1->tls_start + (s1->fmt == TLI_FMT_PE ? 0 : 16);
}

/// ------------------------------------------------------ procedure linkage
///
/// Entry zero saves x16 and the link register, walks an ADRP/LDR/ADD pair to
/// the second GOT slot, and branches through it to the loader's resolver; the
/// ADD leaves x16 holding the slot address, which is how the resolver learns
/// which symbol it was asked for.  Every other entry is the same three
/// instructions without the save, reaching its own slot.
///
/// The entries cannot be written until .plt and .got have addresses, so
/// arm64_create_plt_entry() parks the GOT offset in the entry and arm64_relocate_plt()
/// overwrites it with the instructions.
#define ARM64_PLT0_SIZE      32
#define ARM64_PLT_ENTRY_SIZE 16

/// Reserve a PLT entry for the GOT slot at 'got_offset' and return where in
/// .plt it landed.  The instructions are written by arm64_relocate_plt().
static u32 arm64_create_plt_entry(tli_State *s1, u32 got_offset, struct tli_SymAttr *attr)
{
    tli_Section *plt = s1->plt;
    u32 plt_offset;
    u8 *p;

    (void)attr;

    if (plt->data_offset == 0)
        section_ptr_add(plt, ARM64_PLT0_SIZE);

    plt_offset = plt->data_offset;

    p = section_ptr_add(plt, ARM64_PLT_ENTRY_SIZE);
    write64le(p, got_offset);
    return plt_offset;
}

/// Write the three instruction sequence that reaches 'slot' from the entry at
/// 'pc', and branch through it.
static void arm64_plt_thunk(tli_State *s1, u8 *p, addr_t pc, addr_t slot)
{
    i64 page = arm64_page_delta(slot, pc);

    if (!signed_fits(page, 21))
        tli_error_noabort("Failed relocating PLT (off=0x%lx, addr=0x%lx, pc=0x%lx)",
                          (long)page, (long)slot, (long)pc);

    // adrp x16, page(slot)
    write32le(p, ARM64_ADRP | ARM64_RD(16) | ARM64_ADRP_IMM(page));

    // ldr x17, [x16, #lo12(slot)]
    write32le(p + 4, ARM64_LDR_X | ARM64_RT(17) | ARM64_RN(16)
                     | ARM64_LO12(slot, 3));

    // add x16, x16, #lo12(slot)
    write32le(p + 8, ARM64_ADD_IMM | ARM64_SF(1) | ARM64_RD(16) | ARM64_RN(16)
                     | ARM64_IMM12(slot));

    // br x17
    write32le(p + 12, ARM64_BR | ARM64_RN(17));
}

/// Relocate the PLT: compute addresses and offsets in the PLT now that the
/// final addresses for PLT and GOT are known (see fill_program_header).
static void arm64_relocate_plt(tli_State *s1)
{
    u8 *p, *p_end;

    if (!s1->plt)
        return;

    p = s1->plt->data;
    p_end = p + s1->plt->data_offset;

    if (p < p_end) {
        addr_t plt = s1->plt->sh_addr;
        addr_t got = s1->got->sh_addr;

        // stp x16, x30, [sp, #-16]!
        write32le(p, ARM64_STP_X_PRE | ARM64_RT(16) | ARM64_RT2(30)
                     | ARM64_RN(31) | ARM64_IMM7(-2));

        // GOT[2] holds the resolver.  The thunk starts one instruction in,
        // so that is the address its ADRP is measured from.
        arm64_plt_thunk(s1, p + 4, plt + 4, got + 2 * PTR_SIZE);
        write32le(p + 20, ARM64_NOP);
        write32le(p + 24, ARM64_NOP);
        write32le(p + 28, ARM64_NOP);
        p += ARM64_PLT0_SIZE;

        // Every entry after entry zero, each still holding its GOT offset.
        while (p < p_end) {
            addr_t pc = plt + (addr_t)(p - s1->plt->data);

            arm64_plt_thunk(s1, p, pc, got + read64le(p));
            p += ARM64_PLT_ENTRY_SIZE;
        }
    }

    // Lazy binding starts every slot at entry zero.
    if (s1->plt->reloc) {
        ElfW_Rel *rel;

        p = s1->got->data;
        for_each_elem(s1->plt->reloc, 0, rel, ElfW_Rel)
            write64le(p + rel->r_offset, s1->plt->sh_addr);
    }
}

/// Apply one relocation of type 'type' to the field at 'ptr', which will live
/// at address 'addr' and refers to a symbol whose value is 'val'.
static void arm64_relocate(tli_State *s1, ElfW_Rel *rel, i32 type, u8 *ptr, addr_t addr, addr_t val)
{
    i32 sym_index, esym_index;
    i64 off;

    switch (type) {
    // S + A.  In a shared object the sum is not known until load time, so the
    // relocation is handed on: against an exported symbol it stays symbolic,
    // otherwise the loader need only add the load bias.
    case R_AARCH64_ABS64:
        if (s1->output_type & TLI_OUTPUT_DYN) {
            sym_index = ELFW(R_SYM)(rel->r_info);
            esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
            qrel->r_offset = rel->r_offset;
            if (esym_index) {
                qrel->r_info = ELFW(R_INFO)(esym_index, R_AARCH64_ABS64);
                qrel->r_addend = rel->r_addend;
                qrel++;
                return;
            }

            qrel->r_info = ELFW(R_INFO)(0, R_AARCH64_RELATIVE);
            qrel->r_addend = read64le(ptr) + val;
            qrel++;
        }

        add64le(ptr, val);
        return;

    // S + A into a 32 bit word, which a shared object cannot keep absolute.
    case R_AARCH64_ABS32:
        if (s1->output_type & TLI_OUTPUT_DYN) {
            qrel->r_offset = rel->r_offset;
            qrel->r_info = ELFW(R_INFO)(0, R_AARCH64_RELATIVE);

            // Whatever sits in the field is part of the addend, and signed.
            qrel->r_addend = (i32)read32le(ptr) + val;
            qrel++;
        }

        add32le(ptr, val);
        return;

    // S + A - P
    case R_AARCH64_PREL32:
        if (s1->output_type == TLI_OUTPUT_DLL) {
            sym_index = ELFW(R_SYM)(rel->r_info);
            esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
            if (esym_index) {
                qrel->r_offset = rel->r_offset;
                qrel->r_info = ELFW(R_INFO)(esym_index, R_AARCH64_PREL32);
                qrel->r_addend = (i32)read32le(ptr) + rel->r_addend;
                qrel++;
                return;
            }
        }

        add32le(ptr, val - addr);
        return;

    // An address built from four MOVs, sixteen bits at a time.  Only the top
    // quarter is checked, because the lower three are declared no-check by
    // the ABI: they are meaningless on their own.
    case R_AARCH64_MOVW_UABS_G0_NC:
        write32le(ptr, (read32le(ptr) & ARM64_KEEP_IMM16) | ARM64_IMM16(val));
        return;
    case R_AARCH64_MOVW_UABS_G1_NC:
        write32le(ptr, (read32le(ptr) & ARM64_KEEP_IMM16) | ARM64_IMM16(val >> 16));
        return;
    case R_AARCH64_MOVW_UABS_G2_NC:
        write32le(ptr, (read32le(ptr) & ARM64_KEEP_IMM16) | ARM64_IMM16(val >> 32));
        return;
    case R_AARCH64_MOVW_UABS_G3:
        write32le(ptr, (read32le(ptr) & ARM64_KEEP_IMM16) | ARM64_IMM16(val >> 48));
        return;

    // Page(S + A) - Page(P), which is the ordinary way to reach anything
    // within +-4GiB of the code.
    case R_AARCH64_ADR_PREL_PG_HI21:
        off = arm64_page_delta(val, addr);
        if (!signed_fits(off, 21)) {
            // No ADRP reaches address zero from a 64-bit image base, so
            // materialise the zero instead and let the paired ADD supply any
            // low addend.
            if (s1->fmt == TLI_FMT_PE && arm64_weak_undef(s1, rel)) {
                write32le(ptr, ARM64_MOVZ_X | ARM64_RD(read32le(ptr)));
                return;
            }

            tli_error_noabort("R_AARCH64_ADR_PREL_PG_HI21 relocation failed");
            return;
        }

        write32le(ptr, (read32le(ptr) & ARM64_KEEP_ADRP) | ARM64_ADRP_IMM(off));
        return;

    // The other half of that pair: the offset within the page.  A load or
    // store scales its immediate by the access width, so each width drops a
    // different number of low bits, and all of them are declared no-check
    // because the ADRP has already covered the range.
    case R_AARCH64_ADD_ABS_LO12_NC:
    case R_AARCH64_LDST8_ABS_LO12_NC:
        write32le(ptr, (read32le(ptr) & ARM64_KEEP_IMM12) | ARM64_LO12(val, 0));
        return;
    case R_AARCH64_LDST16_ABS_LO12_NC:
        write32le(ptr, (read32le(ptr) & ARM64_KEEP_IMM12) | ARM64_LO12(val, 1));
        return;
    case R_AARCH64_LDST32_ABS_LO12_NC:
        write32le(ptr, (read32le(ptr) & ARM64_KEEP_IMM12) | ARM64_LO12(val, 2));
        return;
    case R_AARCH64_LDST64_ABS_LO12_NC:
        write32le(ptr, (read32le(ptr) & ARM64_KEEP_IMM12) | ARM64_LO12(val, 3));
        return;
    case R_AARCH64_LDST128_ABS_LO12_NC:
        write32le(ptr, (read32le(ptr) & ARM64_KEEP_IMM12) | ARM64_LO12(val, 4));
        return;

    // A conditional branch or a compare-and-branch: 19 bits of instructions,
    // so +-1MiB.
    case R_AARCH64_CONDBR19:
        off = (i64)val - (i64)addr;
        if ((off & 3) || !signed_fits(off, 21)) {
            tli_error_noabort("R_AARCH64_CONDBR19 relocation failed"
                              " (val=%lx, addr=%lx)", (long)val, (long)addr);
            return;
        }

        write32le(ptr, (read32le(ptr) & ARM64_KEEP_IMM19) | ARM64_IMM19(off >> 2));
        return;

    // Test one bit and branch: 14 bits of instructions, so +-32KiB.
    case R_AARCH64_TSTBR14:
        off = (i64)val - (i64)addr;
        if ((off & 3) || !signed_fits(off, 16)) {
            tli_error_noabort("R_AARCH64_TSTBR14 relocation failed"
                              " (val=%lx, addr=%lx)", (long)val, (long)addr);
            return;
        }

        write32le(ptr, (read32le(ptr) & ARM64_KEEP_IMM14) | ARM64_IMM14(off >> 2));
        return;

    // S + A - P, 26 bits of instructions, so +-128MiB.
    //
    // B and BL differ in one bit, and ELF has a relocation for each, so the
    // instruction could be rebuilt from the relocation type alone.  It is
    // not, because the other object formats carry a single branch relocation
    // for both: rebuilding from the type there turns every tail call into a
    // call, which then returns into whatever was laid out next.  The
    // instruction is the thing that knows.  Only when it is not recognisably
    // a branch, which is how a placeholder in a linker-built stub arrives,
    // does the type decide.
    case R_AARCH64_JUMP26:
    case R_AARCH64_CALL26: {
        u32 insn = read32le(ptr);

        off = (i64)val - (i64)addr;
        if ((off & 3) || !signed_fits(off, 28)) {
            // The call is guarded, so leave a nop where it cannot reach.
            if (s1->fmt == TLI_FMT_PE && arm64_weak_undef(s1, rel)) {
                write32le(ptr, ARM64_NOP);
                return;
            }

            sym_index = ELFW(R_SYM)(rel->r_info);
            tli_error_noabort("R_AARCH64_(JUMP|CALL)26 relocation failed"
                              " for '%s' (val=%lx, addr=%lx)",
                              (char *)symtab_section->link->data
                              + ((ElfW(Sym) *)symtab_section->data)[sym_index].st_name,
                              (long)val, (long)addr);
            return;
        }

        if (!ARM64_IS_BRANCH(insn))
            insn = (type == R_AARCH64_CALL26) ? ARM64_BL : ARM64_B;

        write32le(ptr, (insn & ARM64_KEEP_IMM26) | ARM64_IMM26(off >> 2));
        return;
    }

    // Page(G + GOT) - Page(P): the page the symbol's GOT slot is on.
    case R_AARCH64_ADR_GOT_PAGE:
        sym_index = ELFW(R_SYM)(rel->r_info);
        off = arm64_page_delta(s1->got->sh_addr
                               + get_sym_attr(s1, sym_index, 0)->got_offset, addr);
        if (!signed_fits(off, 21)) {
            tli_error_noabort("R_AARCH64_ADR_GOT_PAGE relocation failed");
            return;
        }

        write32le(ptr, (read32le(ptr) & ARM64_KEEP_ADRP) | ARM64_ADRP_IMM(off));
        return;

    // The offset of that slot within its page, scaled by eight.
    case R_AARCH64_LD64_GOT_LO12_NC:
        sym_index = ELFW(R_SYM)(rel->r_info);
        write32le(ptr, (read32le(ptr) & ARM64_KEEP_IMM12)
                       | ARM64_LO12(s1->got->sh_addr
                                    + get_sym_attr(s1, sym_index, 0)->got_offset, 3));
        return;

    // Both are plain S.  val arrived with the addend already folded in, so
    // take it back out: a GOT slot holds the symbol and nothing else.
    case R_AARCH64_GLOB_DAT:
    case R_AARCH64_JUMP_SLOT:
        write64le(ptr, val - rel->r_addend);
        return;

    // The loader copies the initialiser out of the shared object at startup.
    // Nothing to write at link time.
    case R_AARCH64_COPY:
        return;

    case R_AARCH64_RELATIVE:
        // PE expresses the same thing against the image base.
        if (s1->fmt == TLI_FMT_PE)
            add32le(ptr, val - s1->pe_imagebase);

        return;

    // Variant I TLS: the thread pointer is below the block, so offsets are
    // positive.  The two relocations feed an ADD pair, one shifted by twelve
    // and one not, which between them reach 24 bits.
    case R_AARCH64_TLSLE_ADD_TPREL_HI12:
    case R_AARCH64_TLSLE_ADD_TPREL_LO12:
    case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: {
        i64 tp_offset = arm64_tp_offset(s1, val);
        u32 imm = type == R_AARCH64_TLSLE_ADD_TPREL_HI12
                  ? (u32)(tp_offset >> 12) : (u32)tp_offset;

        write32le(ptr, (read32le(ptr) & ARM64_KEEP_IMM12) | ARM64_IMM12(imm));
        return;
    }

    // Global dynamic, rewritten into local exec.  The compiler asks for a
    // descriptor in the GOT and an indirect call to the resolver that reads
    // it; with one module and every thread-local in the initial block, the
    // answer is a constant, and the four instructions collapse into two that
    // build it and two that do nothing.
    //
    // Each relocation names one instruction and the replacement for it does
    // not depend on the other three, so this needs none of the agreement
    // between neighbouring relocations that rewriting usually does.  x0 is
    // where the sequence leaves its answer, by the same ABI that says the
    // resolver takes its argument there.
    case R_AARCH64_TLSDESC_ADR_PAGE21:
    case R_AARCH64_TLSDESC_LD64_LO12:
    case R_AARCH64_TLSDESC_ADD_LO12:
    case R_AARCH64_TLSDESC_CALL: {
        ElfW(Sym) *tsym;
        i64 tp_offset;

        // The add and the call carry the answer nowhere, so they go.
        if (type == R_AARCH64_TLSDESC_ADD_LO12
            || type == R_AARCH64_TLSDESC_CALL) {
            write32le(ptr, 0xd503201fu);  // nop
            return;
        }

        // The symbol's own address: an addend here belongs to the descriptor
        // reference that is being thrown away, not to the offset.
        sym_index = ELFW(R_SYM)(rel->r_info);
        tsym = (ElfW(Sym) *)symtab_section->data + sym_index;
        tp_offset = arm64_tp_offset(s1, tsym->st_value);

        // Two halves of a movz/movk pair reach 32 bits and no further, which
        // is more thread-local storage than an image has any business having.
        if (tp_offset < 0 || tp_offset > 0xffffffffLL) {
            tli_error_noabort("thread-local offset 0x%lx does not fit the "
                              "local-exec sequence", (long)tp_offset);
            return;
        }

        if (type == R_AARCH64_TLSDESC_ADR_PAGE21)
            write32le(ptr, 0xd2a00000u          // movz x0, #hi16, lsl #16
                           | (u32)(((tp_offset >> 16) & 0xffff) << 5));
        else
            write32le(ptr, 0xf2800000u          // movk x0, #lo16
                           | (u32)((tp_offset & 0xffff) << 5));

        return;
    }

    // Initial exec.  The pair addresses a GOT slot holding the offset from
    // the thread pointer, and the slot is filled here as well as reached:
    // a relocation against the GOT is left for the loader, and the loader
    // has no way to know an offset into a block this linker laid out.
    case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
    case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: {
        ElfW(Sym) *tsym;
        u32 slot;
        i64 tp_offset;

        sym_index = ELFW(R_SYM)(rel->r_info);
        slot = get_sym_attr(s1, sym_index, 0)->got_offset;

        // The symbol's own address, since val may carry an addend that
        // belongs to the reference rather than to the offset.
        tsym = (ElfW(Sym) *)symtab_section->data + sym_index;
        tp_offset = arm64_tp_offset(s1, tsym->st_value);
        write64le(s1->got->data + slot, (u64)tp_offset);

        if (type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
            write32le(ptr, (read32le(ptr) & ARM64_KEEP_IMM12)
                           | ARM64_LO12(s1->got->sh_addr + slot, 3));
            return;
        }

        off = arm64_page_delta(s1->got->sh_addr + slot, addr);
        if (!signed_fits(off, 21)) {
            tli_error_noabort("R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 "
                              "relocation failed");
            return;
        }

        write32le(ptr, (read32le(ptr) & ARM64_KEEP_ADRP) | ARM64_ADRP_IMM(off));
        return;
    }

    default:
        fprintf(stderr, "FIXME: handle reloc type %x at %x [%p] to %x\n",
                type, (u32)addr, ptr, (u32)val);
        return;
    }
}

/// What the generic code reads about this machine.  AArch64 systems have used
/// 64KiB pages, so segments are aligned to the larger size.  A PLT entry
/// reaches its GOT slot with an ADRP, which works in a position-independent
/// image unchanged.
ST_DATA const tli_Arch tli_arch_arm64 = {
    .em = EM_AARCH64,
    .relocate = arm64_relocate,
    .create_plt_entry = arm64_create_plt_entry,
    .relocate_plt = arm64_relocate_plt,
    .reloc_props = arm64_reloc_props,
    .r_data_ptr = R_AARCH64_ABS64,
    .r_data_32dw = R_AARCH64_ABS32,
    .r_jmp_slot = R_AARCH64_JUMP_SLOT,
    .r_glob_dat = R_AARCH64_GLOB_DAT,
    .r_copy = R_AARCH64_COPY,
    .r_relative = R_AARCH64_RELATIVE,
    .start_addr = 0x00400000,
    .page_size = 0x10000,
    .pcrel_dllplt = 1,
    .elf_interp = "/lib/ld-linux-aarch64.so.1",
    .build_reloc = {
        [1] = R_AARCH64_ABS64,
        [2] = R_AARCH64_ABS32,
        [3] = R_AARCH64_PREL32,
        [4] = R_AARCH64_CALL26,
        [5] = R_AARCH64_ADR_PREL_PG_HI21,
        [6] = R_AARCH64_ADD_ABS_LO12_NC,
        [12] = R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21,
        [13] = R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC,
        [15] = R_AARCH64_ADR_GOT_PAGE,
        [16] = R_AARCH64_LD64_GOT_LO12_NC,

        // A COFF thread-local's offset within .tls, which a PE link
        // resolves the way ELF resolves the local-exec offset.
        [20] = R_AARCH64_TLSLE_ADD_TPREL_HI12,
        [21] = R_AARCH64_TLSLE_ADD_TPREL_LO12_NC,
    },
    // Windows on ARM64 postdates the 4 GiB address space, hence both the high
    // image bases and the Windows 8 minimum.  The port has always produced
    // relocatable images, so it claims few characteristics.
    .pe_machine = 0xAA64,
    .pe_os_version = 0x0602,  // Windows 8
    .pe_rsrc_reltype = 2,     // IMAGE_REL_ARM64_ADDR32NB
    .pe_characteristics = PE_FILE_EXECUTABLE_IMAGE | PE_FILE_LARGE_ADDRESS_AWARE,
    .pe_image_base_exe = 0x0000000140000000ULL,
    .pe_image_base_dll = 0x0000000180000000ULL,
    .r_pe_abs = R_AARCH64_ABS64,
    .r_pe_rva = R_AARCH64_RELATIVE,
    .r_pe_thunk = R_AARCH64_ABS64,
    .macho_cputype = 0x0100000c,  // CPU_TYPE_ARM64
    .macho_cpusubtype = 0,  // CPU_SUBTYPE_ARM64_ALL
};


/// ---- src/core/riscv64-link.c ----

/// tinylinker - RISC-V relocation processing
///
/// Written from the RISC-V ELF psABI specification, which gives each
/// relocation a calculation and names the instruction field it lands in, and
/// from the unprivileged ISA manual for the encodings of those fields.
///
/// RISC-V objects use Elf64_Rela, so the addend arrives in r_addend and the
/// caller has already folded it into val.
///
/// Two features of this ISA account for most of what follows.  Immediates are
/// scattered through the instruction word rather than laid out contiguously,
/// because the fixed positions let the hardware route the bits without a
/// multiplexer, so every field here is assembled a piece at a time.  And no
/// single instruction can name an address: a PC-relative reference is an
/// AUIPC supplying the high twenty bits and a second instruction supplying
/// the low twelve, related to each other only by a label, which is why the
/// low half has to look up what the high half computed.


/// ------------------------------------------------------------ immediates
///
/// Nothing on RISC-V puts an immediate in one piece.  Each of these takes the
/// value in its natural form and returns the bits already in position, paired
/// with a mask naming what the instruction keeps.

/// What a B-type instruction keeps: the opcode, funct3 and both registers.
#define RISCV_KEEP_B 0x01fff07fu

/// B-type: a conditional branch, counted in halfwords.  Bits 4:1 and 10:5 sit
/// where the S-type store immediate puts the same bits, and only bit 11 and
/// the sign have to move.
static u32 riscv64_imm_b(i64 d)
{
    u32 u = (u32)d;

    return ((u >> 12 & 1) << 31) | ((u >> 5 & 0x3f) << 25)
         | ((u >> 1 & 0xf) << 8) | ((u >> 11 & 1) << 7);
}

/// What a J-type instruction keeps: the opcode and the destination register.
#define RISCV_KEEP_J 0x00000fffu

/// J-type: an unconditional jump, likewise in halfwords.
static u32 riscv64_imm_j(i64 d)
{
    u32 u = (u32)d;

    return ((u >> 20 & 1) << 31) | ((u >> 1 & 0x3ff) << 21)
         | ((u >> 11 & 1) << 20) | ((u >> 12 & 0xff) << 12);
}

/// U-type: the high twenty bits an AUIPC or LUI contributes.
#define RISCV_KEEP_U 0x00000fffu
#define RISCV_IMM_U(hi) (((u32)(hi) & 0xfffff) << 12)

/// I-type: the twelve bit immediate of a load, an addi or a jalr.
#define RISCV_KEEP_I 0x000fffffu
#define RISCV_IMM_I(v) (((u32)(v) & 0xfff) << 20)

/// S-type: the same twelve bits, split around the register fields so that a
/// store can name two source registers in the usual places.
#define RISCV_KEEP_S 0x01fff07fu
#define RISCV_IMM_S(v) ((((u32)(v) & 0xfe0) << 20) | (((u32)(v) & 0x1f) << 7))

/// What a CB-type instruction keeps: the opcode, funct3 and the register.
#define RISCV_KEEP_CB 0xe383u

/// CB-type: a compressed branch, reaching 256 bytes.
static u16 riscv64_imm_cb(i64 d)
{
    u32 u = (u32)d;

    return (u16)(((u >> 5 & 1) << 2) | ((u >> 1 & 3) << 3)
                 | ((u >> 6 & 3) << 5) | ((u >> 3 & 3) << 10)
                 | ((u >> 8 & 1) << 12));
}

/// What a CJ-type instruction keeps: the opcode and funct3, nothing else.
#define RISCV_KEEP_CJ 0xe003u

/// CJ-type: a compressed jump, reaching 2KiB.
static u16 riscv64_imm_cj(i64 d)
{
    u32 u = (u32)d;

    return (u16)(((u >> 5 & 1) << 2) | ((u >> 1 & 7) << 3)
                 | ((u >> 7 & 1) << 6) | ((u >> 6 & 1) << 7)
                 | ((u >> 10 & 1) << 8) | ((u >> 8 & 3) << 9)
                 | ((u >> 4 & 1) << 11) | ((u >> 11 & 1) << 12));
}

/// The high twenty bits an AUIPC must carry for the pair to reach 'd'.  The
/// instruction that completes it adds a *signed* twelve bit immediate, so the
/// high part rounds to nearest rather than truncating.
static i64 riscv64_hi20(i64 d)
{
    return (d + 0x800) >> 12;
}


/// ------------------------------------------------ relocation properties
///
/// What each RISC-V relocation type patches and what it needs built for it.
static const u8 riscv64_reloc_class[R_RISCV_NUM] = {
    /// Data words.  Code generators reach functions through these too, hence
    /// AUTO rather than NO.
    [R_RISCV_32]            = RC_DATA(AUTO_GOTPLT_ENTRY),
    [R_RISCV_64]            = RC_DATA(AUTO_GOTPLT_ENTRY),
    [R_RISCV_32_PCREL]      = RC_DATA(AUTO_GOTPLT_ENTRY),
    /// Branches and jumps.  Only the ones that can reach a PLT entry ask for
    /// one; a conditional branch reaches 4KiB and could not use it.
    [R_RISCV_BRANCH]        = RC_INSN(AUTO_GOTPLT_ENTRY),
    [R_RISCV_JAL]           = RC_INSN(AUTO_GOTPLT_ENTRY),
    [R_RISCV_CALL]          = RC_INSN(AUTO_GOTPLT_ENTRY),
    [R_RISCV_CALL_PLT]      = RC_INSN(AUTO_GOTPLT_ENTRY),
    [R_RISCV_RVC_BRANCH]    = RC_INSN(NO_GOTPLT_ENTRY),
    [R_RISCV_RVC_JUMP]      = RC_INSN(NO_GOTPLT_ENTRY),
    /// The AUIPC and whatever completes it.
    [R_RISCV_PCREL_HI20]    = RC_DATA(AUTO_GOTPLT_ENTRY),
    [R_RISCV_PCREL_LO12_I]  = RC_DATA(AUTO_GOTPLT_ENTRY),
    [R_RISCV_PCREL_LO12_S]  = RC_DATA(AUTO_GOTPLT_ENTRY),
    [R_RISCV_GOT_HI20]      = RC_DATA(ALWAYS_GOTPLT_ENTRY),
    /// Absolute addressing, which is what the default medlow code model
    /// emits.  The pair names the symbol itself rather than a distance, so
    /// neither half needs the other looked up.
    [R_RISCV_HI20]          = RC_DATA(AUTO_GOTPLT_ENTRY),
    [R_RISCV_LO12_I]        = RC_DATA(AUTO_GOTPLT_ENTRY),
    [R_RISCV_LO12_S]        = RC_DATA(AUTO_GOTPLT_ENTRY),
    /// Initial exec and global dynamic both reach a slot rather than the
    /// symbol; the descriptor the second wants is two words, not one.
    [R_RISCV_TLS_GOT_HI20]  = RC_DATA(ALWAYS_GOTPLT_ENTRY),
    [R_RISCV_TLS_GD_HI20]   = RC_DATA(ALWAYS_GOTPLT_ENTRY),
    /// Label differences, which the assembler leaves for the linker whenever
    /// relaxation might have moved one of the two labels.
    [R_RISCV_ADD16]         = RC_DATA(NO_GOTPLT_ENTRY),
    [R_RISCV_ADD32]         = RC_DATA(AUTO_GOTPLT_ENTRY),
    [R_RISCV_ADD64]         = RC_DATA(AUTO_GOTPLT_ENTRY),
    [R_RISCV_SUB8]          = RC_DATA(NO_GOTPLT_ENTRY),
    [R_RISCV_SUB16]         = RC_DATA(NO_GOTPLT_ENTRY),
    [R_RISCV_SUB32]         = RC_DATA(AUTO_GOTPLT_ENTRY),
    [R_RISCV_SUB64]         = RC_DATA(AUTO_GOTPLT_ENTRY),
    [R_RISCV_SUB6]          = RC_DATA(NO_GOTPLT_ENTRY),
    [R_RISCV_SET6]          = RC_DATA(NO_GOTPLT_ENTRY),
    [R_RISCV_SET8]          = RC_DATA(NO_GOTPLT_ENTRY),
    [R_RISCV_SET16]         = RC_DATA(NO_GOTPLT_ENTRY),
    [R_RISCV_SET_ULEB128]   = RC_DATA(NO_GOTPLT_ENTRY),
    [R_RISCV_SUB_ULEB128]   = RC_DATA(NO_GOTPLT_ENTRY),
    /// Markers for a relaxing linker.  This one does not relax, so they say
    /// nothing it needs to act on.
    [R_RISCV_ALIGN]         = RC_DATA(NO_GOTPLT_ENTRY),
    [R_RISCV_RELAX]         = RC_DATA(NO_GOTPLT_ENTRY),
    /// Local exec: the offset from the thread pointer is known now.  The
    /// low half comes in a load's field and a store's, and the add that
    /// brings in the thread pointer carries a marker for a linker that folds
    /// the sequence, which this one answers by leaving the add alone.
    [R_RISCV_TPREL_HI20]    = RC_DATA(NO_GOTPLT_ENTRY),
    [R_RISCV_TPREL_LO12_I]  = RC_DATA(NO_GOTPLT_ENTRY),
    [R_RISCV_TPREL_LO12_S]  = RC_DATA(NO_GOTPLT_ENTRY),
    [R_RISCV_TPREL_ADD]     = RC_DATA(NO_GOTPLT_ENTRY),
    /// Written by the linker, consumed by the loader.
    [R_RISCV_COPY]          = RC_DATA(NO_GOTPLT_ENTRY),
    [R_RISCV_RELATIVE]      = RC_DATA(NO_GOTPLT_ENTRY),
    [R_RISCV_JUMP_SLOT]     = RC_INSN(NO_GOTPLT_ENTRY),
};

/// The table entry for a type, or zero for anything out of range, which is
/// how an unknown relocation reaches the callers as "not known here".
static u32 riscv64_reloc_props(i32 reloc_type)
{
    return (u32)reloc_type < R_RISCV_NUM ? riscv64_reloc_class[reloc_type] : 0;
}


/// ------------------------------------------------------ procedure linkage
///
/// Entry zero works out which entry it was reached from, which is the trick
/// this ABI plays instead of pushing a word: the entries are a fixed size and
/// t1 holds the return address into the one that jumped, so subtracting the
/// PLT base and shifting gives the index.  It then loads the resolver from
/// GOT[0] and the link map from GOT[1] and jumps.  Every other entry is an
/// AUIPC/LD pair to its own slot and a jump through it, with t1 left holding
/// the return address for entry zero to read.
///
/// The entries cannot be written until .plt and .got have addresses, so
/// riscv64_create_plt_entry() parks the GOT offset in the entry and riscv64_relocate_plt()
/// overwrites it with the instructions.
#define RISCV64_PLT0_SIZE      32
#define RISCV64_PLT_ENTRY_SIZE 16

/// Reserve a PLT entry for the GOT slot at 'got_offset' and return where in
/// .plt it landed.  The instructions are written by riscv64_relocate_plt().
static u32 riscv64_create_plt_entry(tli_State *s1, u32 got_offset, struct tli_SymAttr *attr)
{
    tli_Section *plt = s1->plt;
    u32 plt_offset;
    u8 *p;

    (void)attr;

    if (plt->data_offset == 0)
        section_ptr_add(plt, RISCV64_PLT0_SIZE);

    plt_offset = plt->data_offset;

    p = section_ptr_add(plt, RISCV64_PLT_ENTRY_SIZE);
    write64le(p, got_offset);
    return plt_offset;
}

/// Relocate the PLT: compute addresses and offsets in the PLT now that the
/// final addresses for PLT and GOT are known (see fill_program_header).
static void riscv64_relocate_plt(tli_State *s1)
{
    u8 *p, *p_end;

    if (!s1->plt)
        return;

    p = s1->plt->data;
    p_end = p + s1->plt->data_offset;

    if (p < p_end) {
        addr_t plt = s1->plt->sh_addr;
        addr_t got = s1->got->sh_addr;

        i64 hi = riscv64_hi20((i64)got - (i64)plt);

        if (!signed_fits(hi, 20))
            tli_error_noabort("Failed relocating PLT (off=0x%lx, got=0x%lx, plt=0x%lx)",
                              (long)hi, (long)got, (long)plt);

        write32le(p,      0x00000397 | RISCV_IMM_U(hi));    // auipc t2,%pcrel_hi(got)
        write32le(p + 4,  0x41c30333);                      // sub   t1,t1,t3
        write32le(p + 8,  0x0003be03                        // ld    t3,%pcrel_lo(got)(t2)
                          | RISCV_IMM_I(got - plt));
        write32le(p + 12, 0xfd430313);                      // addi  t1,t1,-44
        write32le(p + 16, 0x00038293                        // addi  t0,t2,%pcrel_lo(got)
                          | RISCV_IMM_I(got - plt));
        write32le(p + 20, 0x00135313);                      // srli  t1,t1,1
        write32le(p + 24, 0x0082b283);                      // ld    t0,8(t0)
        write32le(p + 28, 0x000e0067);                      // jr    t3
        p += RISCV64_PLT0_SIZE;

        // Every entry after entry zero, each still holding its GOT offset.
        while (p < p_end) {
            addr_t pc = plt + (addr_t)(p - s1->plt->data);
            addr_t slot = got + read64le(p);
            i64 entry_hi = riscv64_hi20((i64)slot - (i64)pc);

            if (!signed_fits(entry_hi, 20))
                tli_error_noabort("Failed relocating PLT (off=0x%lx, addr=0x%lx, pc=0x%lx)",
                                  (long)entry_hi, (long)slot, (long)pc);

            write32le(p,      0x00000e17                     // auipc t3,%pcrel_hi(slot)
                              | RISCV_IMM_U(entry_hi));
            write32le(p + 4,  0x000e3e03                     // ld    t3,%pcrel_lo(slot)(t3)
                              | RISCV_IMM_I(slot - pc));
            write32le(p + 8,  0x000e0367);                   // jalr  t1,t3
            write32le(p + 12, 0x00000013);                   // nop
            p += RISCV64_PLT_ENTRY_SIZE;
        }
    }

    // Lazy binding starts every slot at entry zero.
    if (s1->plt->reloc) {
        ElfW_Rel *rel;

        p = s1->got->data;
        for_each_elem(s1->plt->reloc, 0, rel, ElfW_Rel)
            write64le(p + rel->r_offset, s1->plt->sh_addr);
    }
}

/// ------------------------------------------------------------ hi/lo pairs
///
/// R_RISCV_PCREL_LO12_I and _S do not name the symbol they are reaching.
/// They name a label on the AUIPC that started the job, because only that
/// instruction's address makes the pair add up, and the linker is expected to
/// remember what the AUIPC resolved to.  Sections are relocated one at a time
/// and the caller clears this between them.

/// Remember that the AUIPC at 'addr' was resolved against 'val', for the
/// instruction that completes it to look up.
static void riscv64_record_pcrel_hi(tli_State *s1, addr_t addr, addr_t val)
{
    struct pcrel_hi *entry = tli_malloc(sizeof *entry);

    entry->addr = addr;
    entry->val = val;
    dynarray_add(&s1->pcrel_hi_entries, &s1->nb_pcrel_hi_entries, entry);
}

/// Find what the AUIPC at 'hi_addr' resolved to.  Returns 0 and fills
/// '*hi_val' on success, or reports an error and returns non-zero.
static i32 riscv64_lookup_pcrel_hi(tli_State *s1, addr_t hi_addr, addr_t *hi_val)
{
    i32 i;

    // Backwards, because an assembler emits the low half of a pair directly
    // after the high half far more often than not.
    for (i = s1->nb_pcrel_hi_entries; i > 0; ) {
        struct pcrel_hi *entry = s1->pcrel_hi_entries[--i];

        if (entry->addr == hi_addr) {
            *hi_val = entry->val;
            return 0;
        }
    }

    return tli_error_noabort("unsupported hi/lo pcrel reloc scheme");
}

/// The name of an AUIPC-form relocation, for the one diagnostic that reports
/// a hi20 too far to reach.  Four types share that code path, and the reader
/// of the message needs to know which of them ran out of range.
static const char *riscv64_hi20_name(i32 type)
{
    switch (type) {
    case R_RISCV_GOT_HI20:     return "R_RISCV_GOT_HI20";
    case R_RISCV_TLS_GOT_HI20: return "R_RISCV_TLS_GOT_HI20";
    case R_RISCV_TLS_GD_HI20:  return "R_RISCV_TLS_GD_HI20";
    default:                   return "R_RISCV_PCREL_HI20";
    }
}

/// The bias the RISC-V ABI puts on an offset held in a thread-local
/// descriptor, so that a signed 12-bit immediate reaches the whole of the
/// first 4KiB of the block rather than half of it.  __tls_get_addr() adds it
/// back, so the linker has to take it off.
#define RISCV_DTP_OFFSET 0x800

/// Fill the GOT slot a thread-local reference reaches.
///
/// Nothing else can fill it.  A relocation against the GOT would be left for
/// the loader, and the loader cannot know a position inside a block this
/// linker laid out, so the value is written here instead.
///
/// Initial exec takes one word, the offset from the thread pointer, which on
/// this architecture addresses the start of the block.  Global dynamic takes
/// the two words __tls_get_addr() is handed: the module the variable lives
/// in, which for anything this link defines is this one, and the biased
/// offset within that module's block.
static void riscv64_fill_tls_got(tli_State *s1, i32 type, u32 slot, addr_t val)
{
    i64 off = (i64)val - (i64)s1->tls_start;

    if (type == R_RISCV_TLS_GOT_HI20) {
        write64le(s1->got->data + slot, (u64)off);
        return;
    }

    write64le(s1->got->data + slot, 1);
    write64le(s1->got->data + slot + 8, (u64)(off - RISCV_DTP_OFFSET));
}

/// Apply one relocation of type 'type' to the field at 'ptr', which will live
/// at address 'addr' and refers to a symbol whose value is 'val'.
static void riscv64_relocate(tli_State *s1, ElfW_Rel *rel, i32 type, u8 *ptr,
              addr_t addr, addr_t val)
{
    i32 sym_index, esym_index;
    i64 d;
    u32 slot;

    switch (type) {
    // Advice to a relaxing linker.  This one keeps the instruction stream as
    // the assembler laid it out, so there is nothing to act on.
    case R_RISCV_ALIGN:
    case R_RISCV_RELAX:
    case R_RISCV_TPREL_ADD:
        return;

    // S + A - P, in a field of twelve halfwords plus a sign.
    case R_RISCV_BRANCH:
        d = (i64)val - (i64)addr;
        if ((d & 1) || !signed_fits(d, 13)) {
            tli_error_noabort("R_RISCV_BRANCH relocation failed"
                              " (val=%lx, addr=%lx)", (long)val, (long)addr);
            return;
        }

        write32le(ptr, (read32le(ptr) & RISCV_KEEP_B) | riscv64_imm_b(d));
        return;

    // S + A - P, twenty halfwords plus a sign: +-1MiB.
    case R_RISCV_JAL:
        d = (i64)val - (i64)addr;
        if ((d & 1) || !signed_fits(d, 21)) {
            tli_error_noabort("R_RISCV_JAL relocation failed"
                              " (val=%lx, addr=%lx)", (long)val, (long)addr);
            return;
        }

        write32le(ptr, (read32le(ptr) & RISCV_KEEP_J) | riscv64_imm_j(d));
        return;

    // One relocation covering two instructions, an AUIPC and the JALR that
    // follows it, which between them reach +-2GiB.  Unlike the PCREL pair
    // below there is nothing to look up: the ABI fixes the second instruction
    // as the one immediately after the first.
    case R_RISCV_CALL:
    case R_RISCV_CALL_PLT:
        d = (i64)val - (i64)addr;
        if (!signed_fits(d + 0x800, 32)) {
            tli_error_noabort("R_RISCV_CALL relocation failed"
                              " (val=%lx, addr=%lx)", (long)val, (long)addr);
            return;
        }

        write32le(ptr, (read32le(ptr) & RISCV_KEEP_U)
                       | RISCV_IMM_U(riscv64_hi20(d)));
        write32le(ptr + 4, (read32le(ptr + 4) & RISCV_KEEP_I)
                           | RISCV_IMM_I(d));
        return;

    // Absolute addressing: LUI takes the high twenty bits and the instruction
    // completing it adds a signed twelve, so the high half rounds to nearest
    // the same way the PC-relative pair does.  Unlike that pair the two halves
    // are independent -- each works the symbol out on its own -- so the low
    // one needs no record of where the high one was.
    //
    // The medlow model this belongs to reaches the bottom and top 2GiB of the
    // address space and nothing else, which is a real limit rather than an
    // implementation one: an address that does not fit is refused here rather
    // than silently truncated to something that will run and be wrong.
    case R_RISCV_HI20:
    case R_RISCV_LO12_I:
    case R_RISCV_LO12_S:
        if (!signed_fits((i64)val, 32)) {
            tli_error_noabort("%s relocation failed: 0x%lx is out of reach of "
                              "the medlow code model; compile with "
                              "-mcmodel=medany",
                              type == R_RISCV_HI20 ? "R_RISCV_HI20"
                              : type == R_RISCV_LO12_I ? "R_RISCV_LO12_I"
                                                       : "R_RISCV_LO12_S",
                              (long)val);
            return;
        }

        if (type == R_RISCV_HI20)
            write32le(ptr, (read32le(ptr) & RISCV_KEEP_U)
                           | RISCV_IMM_U(riscv64_hi20((i64)val)));
        else if (type == R_RISCV_LO12_I)
            write32le(ptr, (read32le(ptr) & RISCV_KEEP_I) | RISCV_IMM_I(val));
        else
            write32le(ptr, (read32le(ptr) & RISCV_KEEP_S) | RISCV_IMM_S(val));

        return;

    // The AUIPC half of a PC-relative reference.  The three GOT forms reach
    // the symbol's slot instead of the symbol, but are otherwise the same
    // instruction with the same partner.  The two thread-local ones also fill
    // the slot they reach, since the loader cannot.
    case R_RISCV_PCREL_HI20:
    case R_RISCV_GOT_HI20:
    case R_RISCV_TLS_GOT_HI20:
    case R_RISCV_TLS_GD_HI20:
        if (type != R_RISCV_PCREL_HI20) {
            sym_index = ELFW(R_SYM)(rel->r_info);
            slot = get_sym_attr(s1, sym_index, 0)->got_offset;

            if (type != R_RISCV_GOT_HI20)
                riscv64_fill_tls_got(s1, type, slot, val);

            val = s1->got->sh_addr + slot;
        }

        d = (i64)val - (i64)addr;
        if (!signed_fits(riscv64_hi20(d), 20)) {
            tli_error_noabort("%s relocation failed (val=%lx, addr=%lx)",
                              riscv64_hi20_name(type), (long)val, (long)addr);
            return;
        }

        write32le(ptr, (read32le(ptr) & RISCV_KEEP_U)
                       | RISCV_IMM_U(riscv64_hi20(d)));

        // The instruction completing this one names this address, not the
        // symbol, so remember what it worked out to.
        riscv64_record_pcrel_hi(s1, addr, val);
        return;

    // val is the address of the AUIPC, not of anything being reached.  What
    // goes in the field is the remainder the AUIPC left behind.
    case R_RISCV_PCREL_LO12_I:
    case R_RISCV_PCREL_LO12_S: {
        addr_t hi_addr = val;
        addr_t hi_val = val;

        riscv64_lookup_pcrel_hi(s1, hi_addr, &hi_val);
        d = (i64)hi_val - (i64)hi_addr;
        if (type == R_RISCV_PCREL_LO12_I)
            write32le(ptr, (read32le(ptr) & RISCV_KEEP_I) | RISCV_IMM_I(d));
        else
            write32le(ptr, (read32le(ptr) & RISCV_KEEP_S) | RISCV_IMM_S(d));

        return;
    }

    // The compressed forms reach much less far, and the assembler only emits
    // them where it has already proved the target is close.
    case R_RISCV_RVC_BRANCH:
        d = (i64)val - (i64)addr;
        if ((d & 1) || !signed_fits(d, 9)) {
            tli_error_noabort("R_RISCV_RVC_BRANCH relocation failed"
                              " (val=%lx, addr=%lx)", (long)val, (long)addr);
            return;
        }

        write16le(ptr, (u16)((read16le(ptr) & RISCV_KEEP_CB)
                             | riscv64_imm_cb(d)));
        return;

    // The compressed jump, reaching 2KiB.
    case R_RISCV_RVC_JUMP:
        d = (i64)val - (i64)addr;
        if ((d & 1) || !signed_fits(d, 12)) {
            tli_error_noabort("R_RISCV_RVC_JUMP relocation failed"
                              " (val=%lx, addr=%lx)", (long)val, (long)addr);
            return;
        }

        write16le(ptr, (u16)((read16le(ptr) & RISCV_KEEP_CJ)
                             | riscv64_imm_cj(d)));
        return;

    // S + A.  A shared object cannot keep an absolute address, so the loader
    // is asked to add the load bias instead.
    case R_RISCV_32:
        if (s1->output_type & TLI_OUTPUT_DYN) {
            qrel->r_offset = rel->r_offset;
            qrel->r_info = ELFW(R_INFO)(0, R_RISCV_RELATIVE);

            // Whatever sits in the field is part of the addend, and signed.
            qrel->r_addend = (i32)read32le(ptr) + val;
            qrel++;
        }

        add32le(ptr, val);
        return;

    // S + A.  Against an exported symbol the relocation stays symbolic;
    // otherwise the loader need only add the bias.
    case R_RISCV_64:
        if (s1->output_type & TLI_OUTPUT_DYN) {
            sym_index = ELFW(R_SYM)(rel->r_info);
            esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
            qrel->r_offset = rel->r_offset;
            if (esym_index) {
                qrel->r_info = ELFW(R_INFO)(esym_index, R_RISCV_64);
                qrel->r_addend = rel->r_addend;
                qrel++;
                return;
            }

            qrel->r_info = ELFW(R_INFO)(0, R_RISCV_RELATIVE);
            qrel->r_addend = read64le(ptr) + val;
            qrel++;
        }

        add64le(ptr, val);
        return;

    // A GOT slot, which this psABI writes as an ordinary 64 bit word.
    case R_RISCV_JUMP_SLOT:
        add64le(ptr, val);
        return;

    // S + A - P
    case R_RISCV_32_PCREL:
        if (s1->output_type & TLI_OUTPUT_DYN) {
            sym_index = ELFW(R_SYM)(rel->r_info);
            esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
            if (esym_index) {
                qrel->r_offset = rel->r_offset;
                qrel->r_info = ELFW(R_INFO)(esym_index, R_RISCV_32_PCREL);
                qrel->r_addend = (i32)read32le(ptr) + rel->r_addend;
                qrel++;
                return;
            }
        }

        add32le(ptr, val - addr);
        return;

    // Label differences.  The assembler cannot fold these itself when
    // relaxation might move one of the labels, so it emits an add against one
    // and a subtract against the other and lets them meet in the field.
    case R_RISCV_ADD16:
        write16le(ptr, (u16)(read16le(ptr) + val));
        return;
    case R_RISCV_ADD32:
        write32le(ptr, read32le(ptr) + (u32)val);
        return;
    case R_RISCV_ADD64:
        write64le(ptr, read64le(ptr) + val);
        return;
    case R_RISCV_SUB8:
        *ptr = (u8)(*ptr - val);
        return;
    case R_RISCV_SUB16:
        write16le(ptr, (u16)(read16le(ptr) - val));
        return;
    case R_RISCV_SUB32:
        write32le(ptr, read32le(ptr) - (u32)val);
        return;
    case R_RISCV_SUB64:
        write64le(ptr, read64le(ptr) - val);
        return;

    // Six bits inside a byte, which is where the DWARF line program keeps a
    // small subtraction.
    case R_RISCV_SUB6:
        *ptr = (u8)((*ptr & ~0x3f) | ((*ptr - val) & 0x3f));
        return;

    // The SET forms replace the field instead of adding to it, which is the
    // other half of a label difference the assembler could not fold.
    case R_RISCV_SET6:
        *ptr = (u8)((*ptr & ~0x3f) | (val & 0x3f));
        return;
    case R_RISCV_SET8:
        *ptr = (u8)val;
        return;
    case R_RISCV_SET16:
        write16le(ptr, (u16)val);
        return;

    // A difference written as a variable length number, so it cannot be
    // patched without knowing how many bytes the assembler reserved.  Only
    // .debug_loclists uses them, and it is not loaded.
    case R_RISCV_SET_ULEB128:
    case R_RISCV_SUB_ULEB128:
        return;

    // The loader copies the initialiser out of the shared object at startup.
    // Nothing to write at link time.
    case R_RISCV_COPY:
        return;

    // Emitted by the R_RISCV_32 and R_RISCV_64 paths above for the loader to
    // apply.  One arriving from an input object has already had its value
    // folded in, so there is nothing left to do.
    case R_RISCV_RELATIVE:
        return;

    // Variant I TLS: the thread pointer names the start of the block, so an
    // offset within it is positive and needs no adjustment.  These work like
    // the PCREL group, except that the base is a register the thread already
    // holds, so the low half needs no lookup -- only to know whether it lands
    // in a load's immediate field or a store's.
    case R_RISCV_TPREL_HI20:
    case R_RISCV_TPREL_LO12_I:
    case R_RISCV_TPREL_LO12_S:
        d = (i64)val - (i64)s1->tls_start;
        if (type == R_RISCV_TPREL_HI20) {
            if (!signed_fits(riscv64_hi20(d), 20)) {
                tli_error_noabort("R_RISCV_TPREL_HI20 relocation failed");
                return;
            }

            write32le(ptr, (read32le(ptr) & RISCV_KEEP_U)
                           | RISCV_IMM_U(riscv64_hi20(d)));
        } else if (type == R_RISCV_TPREL_LO12_I) {
            write32le(ptr, (read32le(ptr) & RISCV_KEEP_I) | RISCV_IMM_I(d));
        } else {
            write32le(ptr, (read32le(ptr) & RISCV_KEEP_S) | RISCV_IMM_S(d));
        }

        return;

    default:
        fprintf(stderr, "FIXME: handle reloc type %x at %x [%p] to %x\n",
                type, (u32)addr, ptr, (u32)val);
        return;
    }
}

/// What the generic code reads about this machine.  This psABI has no
/// separate GLOB_DAT: a GOT slot is an ordinary 64 bit word.  A PLT entry
/// reaches its GOT slot with an AUIPC, which works in a position-independent
/// image unchanged.
ST_DATA const tli_Arch tli_arch_riscv64 = {
    .em = EM_RISCV,
    .relocate = riscv64_relocate,
    .create_plt_entry = riscv64_create_plt_entry,
    .relocate_plt = riscv64_relocate_plt,
    .reloc_props = riscv64_reloc_props,
    .r_data_ptr = R_RISCV_64,
    .r_data_32dw = R_RISCV_32,
    .r_jmp_slot = R_RISCV_JUMP_SLOT,
    .r_glob_dat = R_RISCV_64,
    .r_copy = R_RISCV_COPY,
    .r_relative = R_RISCV_RELATIVE,
    .start_addr = 0x00010000,
    .page_size = 0x1000,
    .pcrel_dllplt = 1,
    .elf_interp = "/lib/ld-linux-riscv64-lp64d.so.1",
    .build_reloc = {
        [1] = R_RISCV_64,
        [2] = R_RISCV_32,
        [3] = R_RISCV_32_PCREL,
        [4] = R_RISCV_CALL_PLT,
        [7] = R_RISCV_PCREL_HI20,
        [8] = R_RISCV_PCREL_LO12_I,
        [9] = R_RISCV_PCREL_LO12_S,
        [18] = R_RISCV_TLS_GOT_HI20,
        [19] = R_RISCV_GOT_HI20,
    },
};

#else

/// ---- src/core/i386-link.c ----

/// tinylinker - i386 relocation processing
///
/// Written from the System V Application Binary Interface, Intel386
/// Architecture Processor Supplement, chapter 4 (relocation and the procedure
/// linkage table), and from Ulrich Drepper's "ELF Handling For Thread-Local
/// Storage" for the shapes of the TLS access sequences.
///
/// Two conventions run through everything below.  First, i386 objects use
/// Elf32_Rel: there is no r_addend field, so the addend A is whatever already
/// sits in the field being patched, which is why an operation the ABI writes
/// as S + A appears here as an add and not a store.  Second, i386 threads use
/// variant II TLS, where the thread pointer sits at the *end* of the static
/// block, so every local-exec offset is negative.


/// ------------------------------------------------ relocation properties
///
/// What each i386 relocation type patches and what it needs built for it.
static const u8 i386_reloc_class[R_386_NUM] = {
    [R_386_NONE]       = RC_DATA(NO_GOTPLT_ENTRY),
    /// S + A into a data word.  Code generators also use this for function
    /// addresses, so a slot may still be wanted: see the caller.
    [R_386_32]         = RC_DATA(AUTO_GOTPLT_ENTRY),
    [R_386_PC32]       = RC_INSN(AUTO_GOTPLT_ENTRY),
    [R_386_GOT32]      = RC_DATA(ALWAYS_GOTPLT_ENTRY),
    [R_386_GOT32X]     = RC_DATA(ALWAYS_GOTPLT_ENTRY),
    [R_386_PLT32]      = RC_INSN(ALWAYS_GOTPLT_ENTRY),
    [R_386_COPY]       = RC_DATA(NO_GOTPLT_ENTRY),
    [R_386_GLOB_DAT]   = RC_DATA(NO_GOTPLT_ENTRY),
    [R_386_JMP_SLOT]   = RC_INSN(NO_GOTPLT_ENTRY),
    [R_386_RELATIVE]   = RC_DATA(NO_GOTPLT_ENTRY),
    /// GOTOFF and GOTPC only need the GOT to exist; they name no slot.
    [R_386_GOTOFF]     = RC_DATA(BUILD_GOT_ONLY),
    [R_386_GOTPC]      = RC_DATA(BUILD_GOT_ONLY),
    [R_386_16]         = RC_DATA(NO_GOTPLT_ENTRY),
    [R_386_PC16]       = RC_INSN(AUTO_GOTPLT_ENTRY),
    /// The TLS sequences are relaxed to local exec below, but the GOT has to
    /// exist before that decision is reachable.
    [R_386_TLS_LE]     = RC_DATA(ALWAYS_GOTPLT_ENTRY),
    [R_386_TLS_GD]     = RC_DATA(ALWAYS_GOTPLT_ENTRY),
    [R_386_TLS_LDM]    = RC_DATA(ALWAYS_GOTPLT_ENTRY),
    [R_386_TLS_LDO_32] = RC_DATA(ALWAYS_GOTPLT_ENTRY),
};

/// The table entry for a type, or zero for anything out of range, which is
/// how an unknown relocation reaches the callers as "not known here".
static u32 i386_reloc_props(i32 reloc_type)
{
    return (u32)reloc_type < R_386_NUM ? i386_reloc_class[reloc_type] : 0;
}


/// ------------------------------------------------------ procedure linkage
///
/// The ABI fixes the PLT at sixteen bytes per entry.  Entry zero pushes the
/// link map word at GOT+4 and jumps through the resolver address at GOT+8;
/// every other entry jumps through its own GOT slot, which the loader starts
/// off pointing at the push that follows it, so the first call falls into
/// entry zero with its own relocation named on the stack.
///
/// Two forms exist.  In an executable the GOT slot is addressed absolutely;
/// in a shared object it is addressed off %ebx, which the caller has already
/// loaded with the GOT base.  Only the ModR/M byte differs between them.
#define I386_GRP5      0xff  /// Group 5: /4 jumps indirect, /6 pushes.
#define I386_PUSH_IMM  0x68
#define I386_JMP_REL   0xe9

/// mod=00 rm=101 selects a bare disp32, mod=10 rm=011 selects disp32(%ebx).
/// Adding 0x10 moves the reg field from /4 (jump) to /6 (push).
#define I386_MODRM_ABS      0x25
#define I386_MODRM_EBX      0xa3
#define I386_MODRM_PUSH(m)  ((m) + 0x10)

/// Fixed by the ABI, which is what lets the resolver index the table.
#define I386_PLT_ENTRY_SIZE 16

/// Append a PLT entry reaching the GOT slot at 'got_offset' and return where
/// in .plt it landed.
static u32 i386_create_plt_entry(tli_State *s1, u32 got_offset, struct tli_SymAttr *attr)
{
    tli_Section *plt = s1->plt;
    u32 plt_offset, relofs;
    i32 modrm;
    u8 *p;

    (void)attr;

    modrm = (s1->output_type & TLI_OUTPUT_DYN) ? I386_MODRM_EBX : I386_MODRM_ABS;

    // Entry zero, the resolver trampoline, is built the first time anything
    // asks for an entry at all.
    if (plt->data_offset == 0) {
        p = section_ptr_add(plt, I386_PLT_ENTRY_SIZE);
        p[0] = I386_GRP5;               // push GOT + 4
        p[1] = I386_MODRM_PUSH(modrm);
        write32le(p + 2, PTR_SIZE);
        p[6] = I386_GRP5;               // jmp *(GOT + 8)
        p[7] = modrm;
        write32le(p + 8, PTR_SIZE * 2);
    }

    plt_offset = plt->data_offset;

    // The entry names the relocation the resolver should apply.  On i386 that
    // is a byte offset into .rel.plt, and the entry this slot needs is the one
    // the caller is about to append, so it is the current end of the table
    // less one entry.
    relofs = s1->plt->reloc ? s1->plt->reloc->data_offset : 0;

    p = section_ptr_add(plt, I386_PLT_ENTRY_SIZE);
    p[0] = I386_GRP5;                   // jmp *(GOT + got_offset)
    p[1] = modrm;
    write32le(p + 2, got_offset);
    p[6] = I386_PUSH_IMM;
    write32le(p + 7, relofs - sizeof (ElfW_Rel));
    p[11] = I386_JMP_REL;               // jmp PLT[0]
    write32le(p + 12, -(i32)plt->data_offset);
    return plt_offset;
}

/// Finish the PLT once the addresses of .plt and .got are settled.  Two
/// things are still outstanding: the GOT displacements, written so far as
/// offsets from the GOT base, and the GOT slots themselves, which have to
/// start out pointing at the push in their own entry so that the first call
/// reaches the resolver rather than the function.
static void i386_relocate_plt(tli_State *s1)
{
    u8 *p, *p_end;

    if (!s1->plt)
        return;

    p = s1->plt->data;
    p_end = p + s1->plt->data_offset;

    // Only the absolute form needs the base folded in; the %ebx relative form
    // already holds the right offsets.
    if (!(s1->output_type & TLI_OUTPUT_DYN) && p < p_end) {
        add32le(p + 2, s1->got->sh_addr);
        add32le(p + 8, s1->got->sh_addr);
        for (p += I386_PLT_ENTRY_SIZE; p < p_end; p += I386_PLT_ENTRY_SIZE)
            add32le(p + 2, s1->got->sh_addr);
    }

    // One .rel.plt entry per slot, in the order the entries were created.
    if (s1->plt->reloc) {
        ElfW_Rel *rel;

        // The push sits six bytes into an entry, and entry zero is the
        // resolver trampoline, so the first real entry starts at 16.
        addr_t resume = s1->plt->sh_addr + I386_PLT_ENTRY_SIZE + 6;

        p = s1->got->data;
        for_each_elem(s1->plt->reloc, 0, rel, ElfW_Rel) {
            write32le(p + rel->r_offset, resume);
            resume += I386_PLT_ENTRY_SIZE;
        }
    }
}

/// Offset of a thread-local symbol from the thread pointer.  Variant II puts
/// the thread pointer past the end of the static TLS block, so the result is
/// negative and the instructions subtract it.  Before the program headers are
/// laid out there is no recorded end, in which case the symbol's own section
/// is the whole of the block.
static i32 i386_tpoff(tli_State *s1, i32 sym_index, addr_t val)
{
    tli_Section *sec;

    if (s1->tls_end)
        return (i32)(val - s1->tls_end);

    sec = s1->sections[tli_sym_section(symtab_section, sym_index)];
    return (i32)(val - sec->sh_addr - sec->data_offset);
}

/// Apply one relocation of type 'type' to the field at 'ptr', which will live
/// at address 'addr' and refers to a symbol whose value is 'val'.
static void i386_relocate(tli_State *s1, ElfW_Rel *rel, i32 type, u8 *ptr, addr_t addr, addr_t val)
{
    i32 sym_index, esym_index;

    switch (type) {
    case R_386_NONE:
        return;

    // S + A.  In a shared object the sum is not known until load time, so the
    // relocation is handed on instead: against an exported symbol it stays
    // symbolic, otherwise the loader only has to add the load bias, which is
    // what R_386_RELATIVE says.
    case R_386_32:
        if (s1->output_type & TLI_OUTPUT_DYN) {
            sym_index = ELFW(R_SYM)(rel->r_info);
            esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
            qrel->r_offset = rel->r_offset;
            if (esym_index) {
                qrel->r_info = ELFW(R_INFO)(esym_index, R_386_32);
                qrel++;
                return;
            }

            qrel->r_info = ELFW(R_INFO)(0, R_386_RELATIVE);
            qrel++;
        }

        add32le(ptr, val);
        return;

    // S + A - P.  A shared object cannot resolve a reference to a symbol it
    // does not define, so that case is passed to the loader.
    case R_386_PC32:
        if (s1->output_type == TLI_OUTPUT_DLL) {
            sym_index = ELFW(R_SYM)(rel->r_info);
            esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
            if (esym_index) {
                qrel->r_offset = rel->r_offset;
                qrel->r_info = ELFW(R_INFO)(esym_index, R_386_PC32);
                qrel++;
                return;
            }
        }

        add32le(ptr, val - addr);
        return;

    // L + A - P.  The caller has already pointed val at the PLT entry where
    // one was needed, so the arithmetic is that of PC32.
    case R_386_PLT32:
        add32le(ptr, val - addr);
        return;

    // Both are plain S: a GOT slot carries no addend of its own.
    case R_386_GLOB_DAT:
    case R_386_JMP_SLOT:
        write32le(ptr, val);
        return;

    // GOT + A - P, which recovers the GOT base into a register.
    case R_386_GOTPC:
        add32le(ptr, s1->got->sh_addr - addr);
        return;

    // S + A - GOT: the symbol addressed off the GOT base.
    case R_386_GOTOFF:
        add32le(ptr, val - s1->got->sh_addr);
        return;

    // G + A: the offset of the slot within the GOT, not its address.
    // GOT32X differs only in permitting relaxations that would need the same
    // value written here.
    case R_386_GOT32:
    case R_386_GOT32X:
        sym_index = ELFW(R_SYM)(rel->r_info);
        add32le(ptr, get_sym_attr(s1, sym_index, 0)->got_offset);
        return;

    // The 16 bit forms belong to real mode code, which only a flat binary can
    // carry: ELF has no 16 bit program image to load it into.
    case R_386_16:
    case R_386_PC16:
        if (s1->output_format != TLI_OUTPUT_FORMAT_BINARY) {
            tli_error_noabort("can only produce 16-bit binary files");
            return;
        }

        write16le(ptr, (u16)(read16le(ptr) +
                             (type == R_386_16 ? val : val - addr)));
        return;

    case R_386_RELATIVE:
        // PE expresses the same thing against the image base.
        if (s1->fmt == TLI_FMT_PE)
            add32le(ptr, val - s1->pe_imagebase);

        return;

    // The loader copies the initialiser out of the shared object at startup.
    // Nothing to write at link time.
    case R_386_COPY:
        return;

    // General dynamic, relaxed to local exec.  The compiler emits
    //
    //     lea  x@tlsgd(,%ebx,1), %eax
    //     call __tls_get_addr@plt
    //
    // and the relocation names the displacement three bytes into the lea.
    // Once a whole program is being linked there is only ever one module, so
    // the call can go: read the thread pointer out of %gs and subtract the
    // symbol's own offset.  The replacement is the same twelve bytes, so
    // nothing downstream moves.
    case R_386_TLS_GD: {
        static const u8 expect[] = {
            0x8d, 0x04, 0x1d, 0x00, 0x00, 0x00, 0x00,     // lea 0(,%ebx,1),%eax
            0xe8, 0xfc, 0xff, 0xff, 0xff };               // call __tls_get_addr
        static const u8 replace[] = {
            0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,           // mov %gs:0,%eax
            0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 };         // sub $0,%eax

        if (memcmp(ptr - 3, expect, sizeof expect) != 0) {
            tli_error_noabort("unexpected R_386_TLS_GD pattern");
            return;
        }

        sym_index = ELFW(R_SYM)(rel->r_info);
        memcpy(ptr - 3, replace, sizeof replace);

        // The call carried a relocation of its own; disarm it.
        rel[1].r_info = ELFW(R_INFO)(0, R_386_NONE);

        // The immediate of the sub sits eight bytes into the replacement, and
        // it is subtracted, so the negated offset goes in.
        add32le(ptr + 5, -i386_tpoff(s1, sym_index, val));
        return;
    }

    // Local dynamic, relaxed the same way.  The call would return the
    // module's TLS base in %eax and each access then adds its own
    // R_386_TLS_LDO_32; with one module that base is the thread pointer.
    // The sequence is eleven bytes and the replacement pads to match.
    case R_386_TLS_LDM: {
        static const u8 expect[] = {
            0x8d, 0x83, 0x00, 0x00, 0x00, 0x00,           // lea 0(%ebx),%eax
            0xe8, 0xfc, 0xff, 0xff, 0xff };               // call __tls_get_addr
        static const u8 replace[] = {
            0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,           // mov %gs:0,%eax
            0x90,                                         // nop
            0x8d, 0x74, 0x26, 0x00 };                     // lea 0(%esi,%eiz,1),%esi

        if (memcmp(ptr - 2, expect, sizeof expect) != 0) {
            tli_error_noabort("unexpected R_386_TLS_LDM pattern");
            return;
        }

        memcpy(ptr - 2, replace, sizeof replace);
        rel[1].r_info = ELFW(R_INFO)(0, R_386_NONE);
        return;
    }

    // After the relaxation above the two mean the same thing: the symbol's
    // offset from the thread pointer.
    case R_386_TLS_LDO_32:
    case R_386_TLS_LE:
        sym_index = ELFW(R_SYM)(rel->r_info);
        add32le(ptr, i386_tpoff(s1, sym_index, val));
        return;

    default:
        fprintf(stderr, "FIXME: handle reloc type %d at %x [%p] to %x\n",
                type, (u32)addr, ptr, (u32)val);
        return;
    }
}

/// What the generic code reads about this machine.  A PLT entry names its GOT
/// slot by absolute address, so it does not survive being loaded anywhere but
/// its link address; the PC-relative form reaches the slot through %ebx and
/// would need that register set up by the caller.
ST_DATA const tli_Arch tli_arch_i386 = {
    .em = EM_386,
    .relocate = i386_relocate,
    .create_plt_entry = i386_create_plt_entry,
    .relocate_plt = i386_relocate_plt,
    .reloc_props = i386_reloc_props,
    .r_data_ptr = R_386_32,
    .r_data_32dw = R_386_32,
    .r_jmp_slot = R_386_JMP_SLOT,
    .r_glob_dat = R_386_GLOB_DAT,
    .r_copy = R_386_COPY,
    .r_relative = R_386_RELATIVE,
    .start_addr = 0x08048000,
    .page_size = 0x1000,
    .pcrel_dllplt = 0,
    .elf_interp = "/lib/ld-linux.so.2",
    .r_plt32 = R_386_PLT32,
    .r_pcrel32 = R_386_PC32,
    .build_reloc = {
        [1] = R_386_32,
        [2] = R_386_32,
        [3] = R_386_PC32,
        [4] = R_386_PLT32,
    },
    .pe_machine = 0x014C,
    .pe_os_version = 0x0400,  // Windows NT 4.0
    .pe_rsrc_reltype = 7,     // IMAGE_REL_I386_DIR32NB
    .pe_characteristics = PE_FILE_EXECUTABLE_IMAGE | PE_FILE_32BIT_MACHINE
                          | PE_FILE_LINE_NUMS_STRIPPED | PE_FILE_LOCAL_SYMS_STRIPPED
                          | PE_FILE_DEBUG_STRIPPED | PE_FILE_RELOCS_STRIPPED,
    .pe_image_base_exe = 0x00400000UL,
    .pe_image_base_dll = 0x10000000UL,
    .r_pe_abs = R_386_32,
    .r_pe_rva = R_386_RELATIVE,
    .r_pe_thunk = R_386_32,
};


/// ---- src/core/arm-link.c ----

/// tinylinker - ARM (AArch32) relocation processing
///
/// Written from the ELF for the ARM Architecture document (AAELF), which
/// gives each relocation a class, a field, and a formula in terms of S, A, P,
/// GOT and T, and from the ARM Architecture Reference Manual for the
/// instruction encodings that the fields sit inside.
///
/// ARM objects use Elf32_Rel: there is no r_addend, so the addend A is
/// whatever the field already holds, and every field here is read before it
/// is written.  For the branch and MOVW/MOVT forms that means unpacking the
/// addend out of the instruction, which is why those cases are longer than
/// the arithmetic they perform.
///
/// The other thing this architecture asks of a linker is interworking.  A
/// symbol's T bit, the low bit of its value, says whether it names Thumb
/// code, and a branch has to change instruction set when T disagrees with the
/// caller.  BL becomes BLX where the processor supports it; where it does
/// not, or where the branch is not a call and so cannot be swapped, the only
/// way across is a veneer.


/// The two halfwords that take a Thumb caller into ARM state.  They open both
/// the PLT's interworking prefix and the veneers arm_relocate() builds.
#define THUMB_BX_PC 0x4778u
#define THUMB_NOP   0x46c0u      /// mov r8, r8


/// ------------------------------------------------ relocation properties
///
/// What each ARM relocation type patches and what it needs built for it.
static const u8 arm_reloc_class[R_ARM_NUM] = {
    [R_ARM_NONE]            = RC_DATA(NO_GOTPLT_ENTRY),
    /// The branches.  All of them may need a PLT entry when the symbol turns
    /// out to be undefined, and none of them can reach one without it.
    [R_ARM_PC24]            = RC_INSN(AUTO_GOTPLT_ENTRY),
    [R_ARM_CALL]            = RC_INSN(AUTO_GOTPLT_ENTRY),
    [R_ARM_JUMP24]          = RC_INSN(AUTO_GOTPLT_ENTRY),
    [R_ARM_PLT32]           = RC_INSN(AUTO_GOTPLT_ENTRY),
    [R_ARM_THM_PC22]        = RC_INSN(AUTO_GOTPLT_ENTRY),
    [R_ARM_THM_JUMP24]      = RC_INSN(AUTO_GOTPLT_ENTRY),
    [R_ARM_PREL31]          = RC_INSN(AUTO_GOTPLT_ENTRY),
    [R_ARM_V4BX]            = RC_INSN(AUTO_GOTPLT_ENTRY),
    /// Absolute and place-relative data words.  Code generators reach
    /// functions through these too, hence AUTO rather than NO.
    [R_ARM_ABS32]           = RC_DATA(AUTO_GOTPLT_ENTRY),
    [R_ARM_REL32]           = RC_DATA(AUTO_GOTPLT_ENTRY),
    [R_ARM_TARGET1]         = RC_DATA(AUTO_GOTPLT_ENTRY),
    /// Halves of an address materialised into a register pair.
    [R_ARM_MOVW_ABS_NC]     = RC_DATA(AUTO_GOTPLT_ENTRY),
    [R_ARM_MOVT_ABS]        = RC_DATA(AUTO_GOTPLT_ENTRY),
    [R_ARM_MOVW_PREL_NC]    = RC_DATA(AUTO_GOTPLT_ENTRY),
    [R_ARM_MOVT_PREL]       = RC_DATA(AUTO_GOTPLT_ENTRY),
    [R_ARM_THM_MOVW_ABS_NC] = RC_DATA(AUTO_GOTPLT_ENTRY),
    [R_ARM_THM_MOVT_ABS]    = RC_DATA(AUTO_GOTPLT_ENTRY),
    /// GOTPC and GOTOFF only need the GOT to exist; they name no slot.
    [R_ARM_GOTPC]           = RC_DATA(BUILD_GOT_ONLY),
    [R_ARM_GOTOFF]          = RC_DATA(BUILD_GOT_ONLY),
    [R_ARM_GOT32]           = RC_DATA(ALWAYS_GOTPLT_ENTRY),
    [R_ARM_GOT_PREL]        = RC_DATA(ALWAYS_GOTPLT_ENTRY),
    /// Written by the linker, consumed by the loader.
    [R_ARM_COPY]            = RC_DATA(NO_GOTPLT_ENTRY),
    [R_ARM_GLOB_DAT]        = RC_DATA(NO_GOTPLT_ENTRY),
    [R_ARM_JUMP_SLOT]       = RC_INSN(NO_GOTPLT_ENTRY),
    [R_ARM_RELATIVE]        = RC_DATA(NO_GOTPLT_ENTRY),
    /// Local exec needs nothing: the offset is known now.
    [R_ARM_TLS_LE32]        = RC_DATA(NO_GOTPLT_ENTRY),
};

/// The table entry for a type, or zero for anything out of range, which is
/// how an unknown relocation reaches the callers as "not known here".
static u32 arm_reloc_props(i32 reloc_type)
{
    return (u32)reloc_type < R_ARM_NUM ? arm_reloc_class[reloc_type] : 0;
}


/// ------------------------------------------------------ procedure linkage
///
/// An ARM data processing immediate is an eight bit value rotated right by an
/// even number of places, so no single instruction can add an arbitrary 32
/// bit displacement.  A PLT entry therefore spends three adds walking up a
/// displacement a byte at a time before loading through it, which is why the
/// entries are sixteen bytes of pure address arithmetic and carry no literal
/// pool of their own.
///
/// Entry zero is different: it needs the GOT base rather than one slot, and
/// it has a word of its own after the code to hold it.

/// add rd, rn, #imm8 ror (2 * rot)
#define ARM_ADD_IMM(rd, rn, rot, imm8) \
    (0xe2800000u | ((u32)(rn) << 16) | ((u32)(rd) << 12) \
     | ((u32)(rot) << 8) | (u32)(imm8))

/// The fixed instructions of entry zero, and the load that ends every other
/// entry once its 12 bit offset has been ored in.
#define ARM_PUSH_LR      0xe52de004u  /// str lr, [sp, #-4]!
#define ARM_LDR_LR_PC4   0xe59fe004u  /// ldr lr, [pc, #4]
#define ARM_ADD_LR_PC_LR 0xe08fe00eu  /// add lr, pc, lr
#define ARM_LDR_PC_LR8   0xe5bef008u  /// ldr pc, [lr, #8]!
#define ARM_LDR_PC_IP    0xe5bcf000u  /// ldr pc, [ip, #imm]!

/// Entry zero is four instructions plus the displacement word that follows
/// them; every other entry is four instructions and nothing else.
#define ARM_PLT0_SIZE      20
#define ARM_PLT_ENTRY_SIZE 16

/// Append a PLT entry reaching the GOT slot at 'got_offset' and return where
/// in .plt it landed.
static u32 arm_create_plt_entry(tli_State *s1, u32 got_offset, struct tli_SymAttr *attr)
{
    tli_Section *plt = s1->plt;
    u32 plt_offset;
    u8 *p;

    // Entry zero saves the return address, loads the displacement from the
    // word that follows the code, turns it into the GOT address and jumps
    // through GOT+8, where the loader left its resolver.
    if (plt->data_offset == 0) {
        p = section_ptr_add(plt, ARM_PLT0_SIZE);
        write32le(p,      ARM_PUSH_LR);
        write32le(p + 4,  ARM_LDR_LR_PC4);
        write32le(p + 8,  ARM_ADD_LR_PC_LR);
        write32le(p + 12, ARM_LDR_PC_LR8);

        // the word at p+16 is filled in by arm_relocate_plt()
    }

    plt_offset = plt->data_offset;

    // A Thumb caller cannot branch into ARM code, so where one exists the
    // entry is prefixed with two halfwords that change state.
    if (attr->plt_thumb_stub) {
        p = section_ptr_add(plt, 4);
        write16le(p,     THUMB_BX_PC);
        write16le(p + 2, THUMB_NOP);
    }

    // The entry itself cannot be written until the GOT has an address, so
    // park the slot offset where arm_relocate_plt() will look for it.
    p = section_ptr_add(plt, ARM_PLT_ENTRY_SIZE);
    write32le(p + 4, got_offset);
    return plt_offset;
}

/// Finish the PLT once the addresses of .plt and .got are settled: entry zero
/// gets its displacement word, and every other entry gets the four
/// instructions that reach its own GOT slot.
static void arm_relocate_plt(tli_State *s1)
{
    u8 *p, *p_end;

    if (!s1->plt)
        return;

    p = s1->plt->data;
    p_end = p + s1->plt->data_offset;

    if (p < p_end) {
        addr_t plt = s1->plt->sh_addr;
        addr_t got = s1->got->sh_addr;

        // The add at offset 8 reads a PC of PLT0+16, which is where the word
        // lives, so the word holds the distance from there to the GOT.
        write32le(s1->plt->data + 16, got - plt - 16);
        p += ARM_PLT0_SIZE;

        // Every entry after entry zero, each of which may carry a prefix.
        while (p < p_end) {
            addr_t entry;
            u32 off;

            // Step over the interworking prefix before anything is measured
            // from the entry: the four instructions start after it, and so
            // does the slot offset that arm_create_plt_entry() left behind.
            if (read16le(p) == THUMB_BX_PC && read16le(p + 2) == THUMB_NOP)
                p += 4;

            entry = plt + (addr_t)(p - s1->plt->data);

            // The first add reads a PC of entry+8.  The displacement is
            // allowed to be negative: the three adds and the final load
            // offset simply sum modulo 2^32, which is the arithmetic the
            // processor does anyway.
            off = (u32)(got + read32le(p + 4) - entry - 8);

            write32le(p,      ARM_ADD_IMM(12, 15,  2, (off >> 28) & 0x0f));
            write32le(p + 4,  ARM_ADD_IMM(12, 12,  6, (off >> 20) & 0xff));
            write32le(p + 8,  ARM_ADD_IMM(12, 12, 10, (off >> 12) & 0xff));
            write32le(p + 12, ARM_LDR_PC_IP | (off & 0xfff));
            p += ARM_PLT_ENTRY_SIZE;
        }
    }

    // Lazy binding starts every slot at entry zero, which pushes the return
    // address and calls the resolver.
    if (s1->plt->reloc) {
        ElfW_Rel *rel;

        p = s1->got->data;
        for_each_elem(s1->plt->reloc, 0, rel, ElfW_Rel)
            write32le(p + rel->r_offset, s1->plt->sh_addr);
    }
}

/// Sign extend the low 'bits' of v, which is how every implicit addend in
/// this file arrives: packed into an instruction field with no room for a
/// sign.
static i32 arm_sext(u32 v, i32 bits)
{
    u32 m = (u32)1 << (bits - 1);

    return (i32)((v & (m + m - 1)) ^ m) - (i32)m;
}

/// The imm4:imm12 halves of an ARM MOVW or MOVT, which hold the addend on the
/// way in and the computed half of the address on the way out.
static i32 arm_movw_addend(u32 insn)
{
    return arm_sext(((insn >> 4) & 0xf000) | (insn & 0xfff), 16);
}

/// Put 'half' back where arm_movw_addend() found it, leaving the opcode and
/// the destination register alone.
static u32 arm_movw_insert(u32 insn, u32 half)
{
    return (insn & 0xfff0f000u) | ((half & 0xf000) << 4) | (half & 0xfff);
}

/// The same for a Thumb-2 MOVW or MOVT, whose imm16 is scattered as
/// imm4:i:imm3:imm8 across two halfwords.  Read as one little endian word the
/// first halfword is the low half, so imm4 lands at bit 0 and i at bit 10,
/// while imm3 and imm8 sit in the second halfword at bits 28 and 16.
static i32 arm_thm_movw_addend(u32 insn)
{
    return arm_sext(((insn & 0xf) << 12) | (((insn >> 10) & 1) << 11)
                    | (((insn >> 28) & 7) << 8) | ((insn >> 16) & 0xff), 16);
}

/// Put 'half' back into the four fields arm_thm_movw_addend() read it out of.
static u32 arm_thm_movw_insert(u32 insn, u32 half)
{
    return (insn & 0x8f00fbf0u)
           | ((half >> 12) & 0xf)
           | (((half >> 11) & 1) << 10)
           | (((half >> 8) & 7) << 28)
           | ((half & 0xff) << 16);
}

/// Apply one relocation of type 'type' to the field at 'ptr', which will live
/// at address 'addr' and refers to a symbol whose value is 'val'.
static void arm_relocate(tli_State *s1, ElfW_Rel *rel, i32 type, u8 *ptr, addr_t addr, addr_t val)
{
    ElfW(Sym) *sym;
    i32 sym_index, esym_index;

    switch (type) {
    // An ARM B or BL: cond 101 L imm24, reaching +-32MB in steps of four.
    // The addend is the imm24 field scaled by four, so it is recovered as a
    // 26 bit signed quantity.
    case R_ARM_PC24:
    case R_ARM_CALL:
    case R_ARM_JUMP24:
    case R_ARM_PLT32: {
        u32 insn = read32le(ptr);
        i32 x = arm_sext(insn & 0x00ffffff, 24) * 4;
        i32 blx_avail = TLI_CPUVER >= 5;
        i32 to_thumb = val & 1;
        i32 is_bl = (insn & 0xff000000) == 0xeb000000;

        // R_ARM_CALL is by definition a call; R_ARM_PC24 is whichever the
        // instruction turns out to be.
        i32 is_call = (type == R_ARM_CALL || (type == R_ARM_PC24 && is_bl));

        x += val - addr;

        // Only a call can change instruction set on its own, by becoming
        // BLX, and only from ARMv5.  Anything else has to be already aligned
        // on the four byte boundary an ARM branch can express.
        if (((x & 3) && (!blx_avail || !is_call))
            || x >= 0x2000000 || x < -0x2000000) {
            tli_error_noabort("can't relocate value at %x,%d", addr, type);
            return;
        }

        if (to_thumb) {
            // BLX (immediate) is 1111 101 H imm24 and its offset is
            // imm24:H:0, so it addresses halfwords: exactly what is needed to
            // reach a Thumb entry point two bytes off a word boundary.  H is
            // that second bit of the offset.  The encoding is unconditional,
            // so a conditional BL has no BLX to become, and neither has a
            // branch that was never a call.
            if (!is_bl) {
                tli_error_noabort("can't relocate value at %x,%d", addr, type);
                return;
            }

            write32le(ptr, 0xfa000000u | ((x & 2) << 23)
                           | (((u32)(x >> 2)) & 0x00ffffff));
        } else {
            write32le(ptr, (insn & 0xff000000u)
                           | (((u32)(x >> 2)) & 0x00ffffff));
        }

        return;
    }

    // Thumb-2 B.W and BL, whose 25 bit offset is split across two halfwords
    // as S:I1:I2:imm10:imm11:0, with I1 and I2 stored as J1 and J2
    // exclusive-ored with the sign so that a short branch encodes as a run of
    // like bits.  Bit 12 of the second halfword picks BL over BLX, which is
    // how a call changes instruction set here.
    //
    // Thumb-2 postdates BLX, so unlike the ARM branches above there is no
    // question of the processor supporting it.
    case R_ARM_THM_PC22:
    case R_ARM_THM_JUMP24: {
        u32 hi, lo;
        i32 x;
        i32 s, j1, j2, i1, i2, imm10, imm11;
        i32 to_thumb, is_call, to_plt, blx_bit = 1 << 12;

        sym_index = ELFW(R_SYM)(rel->r_info);
        sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];

        // An unresolved weak reference is meant to be guarded by a null test
        // and never taken, so leave the branch as the compiler left it.
        if (sym->st_shndx == SHN_UNDEF
            && ELFW(ST_BIND)(sym->st_info) == STB_WEAK)
            return;

        // Unpack the implicit addend out of the two halfwords.
        hi = read16le(ptr);
        lo = read16le(ptr + 2);
        s = (hi >> 10) & 1;
        j1 = (lo >> 13) & 1;
        j2 = (lo >> 11) & 1;
        i1 = (j1 ^ s) ^ 1;
        i2 = (j2 ^ s) ^ 1;
        imm10 = hi & 0x3ff;
        imm11 = lo & 0x7ff;
        x = arm_sext((s << 24) | (i1 << 23) | (i2 << 22)
                     | (imm10 << 12) | (imm11 << 1), 25);

        to_thumb = val & 1;
        to_plt = s1->plt && val >= s1->plt->sh_addr
                 && val < s1->plt->sh_addr + s1->plt->data_offset;
        is_call = type == R_ARM_THM_PC22;

        // A plain Thumb jump into ARM code: it is not a call, so it cannot
        // become BLX, and there is no PLT entry to borrow.
        if (!to_thumb && !to_plt && !is_call) {
            // Put a veneer at the end of the target's section, two halfwords
            // to leave Thumb state and an ARM branch to the function, and aim
            // the relocation at that instead.
            //
            // The branch out of the veneer is computed here rather than left
            // to another relocation: this pass has already walked the section
            // the veneer lands in and nothing will come back for it.
            char buf[1024];
            const char *name;
            tli_Section *text;
            addr_t veneer;
            i32 hop;
            u8 *p;
            i32 index;

            name = (char *) symtab_section->link->data + sym->st_name;
            text = s1->sections[tli_sym_section(symtab_section, sym_index)];

            // bx pc reads the word aligned PC, so the ARM half of the veneer
            // has to start on a word boundary.
            if (text->data_offset & 3)
                section_ptr_add(text, 4 - (text->data_offset & 3));

            veneer = text->sh_addr + text->data_offset;

            // The veneer is a symbol of its own, so that the relocation can
            // name it the way it named the function.
            snprintf(buf, sizeof buf, "%s_from_thumb", name);
            index = put_elf_sym_x(symtab_section, veneer + 1, sym->st_size,
                                  sym->st_info, 0, sym->st_shndx,
                                  tli_sym_xindex(symtab_section, sym_index),
                                  buf);
            rel->r_info = ELFW(R_INFO)(index, type);

            p = section_ptr_add(text, 8);
            write16le(p,     THUMB_BX_PC);
            write16le(p + 2, THUMB_NOP);

            // The ARM branch sits at veneer+4 and reads a PC of veneer+12.
            hop = (i32)(val - (veneer + 12)) >> 2;
            write32le(p + 4, 0xea000000u | ((u32)hop & 0x00ffffff));

            to_thumb = 1;
            val = veneer + 1;
        }

        x += val - addr;

        if (!to_thumb && is_call) {
            blx_bit = 0;            // BL becomes BLX
            x = (x + 3) & -4;       // ARM code is entered word aligned
        }

        if (x >= 0x1000000 || x < -0x1000000) {
            tli_error_noabort("can't relocate value at %x,%d", addr, type);
            return;
        }

        // Landing in ARM state is only legal at a word boundary, and only a
        // call or a PLT entry can get there at all.
        if (!to_thumb && ((val & 2) || (!is_call && !to_plt))) {
            tli_error_noabort("can't relocate value at %x,%d", addr, type);
            return;
        }

        // Pack the result back into the two halfwords.
        s = (x >> 24) & 1;
        i1 = (x >> 23) & 1;
        i2 = (x >> 22) & 1;
        j1 = s ^ (i1 ^ 1);
        j2 = s ^ (i2 ^ 1);
        imm10 = (x >> 12) & 0x3ff;
        imm11 = (x >> 1) & 0x7ff;
        write16le(ptr, (u16)((hi & 0xf800) | (s << 10) | imm10));
        write16le(ptr + 2, (u16)((lo & 0xc000) | (j1 << 13) | blx_bit
                                 | (j2 << 11) | imm11));
        return;
    }

    // Half of an address built in two instructions.  The addend lives in the
    // instruction being patched, once in each of the pair, so it is a 16 bit
    // quantity even though the address it contributes to is 32.  MOVT takes
    // the top half of the same sum.
    case R_ARM_MOVW_ABS_NC:
    case R_ARM_MOVT_ABS:
    case R_ARM_MOVW_PREL_NC:
    case R_ARM_MOVT_PREL: {
        u32 insn = read32le(ptr);
        u32 whole = (u32)(val + arm_movw_addend(insn));

        if (type == R_ARM_MOVW_PREL_NC || type == R_ARM_MOVT_PREL)
            whole -= addr;

        if (type == R_ARM_MOVT_ABS || type == R_ARM_MOVT_PREL)
            whole >>= 16;

        write32le(ptr, arm_movw_insert(insn, whole));
        return;
    }

    // The Thumb-2 forms of the same pair; only the absolute ones appear.
    case R_ARM_THM_MOVW_ABS_NC:
    case R_ARM_THM_MOVT_ABS: {
        u32 insn = read32le(ptr);
        u32 whole = (u32)(val + arm_thm_movw_addend(insn));

        if (type == R_ARM_THM_MOVT_ABS)
            whole >>= 16;

        write32le(ptr, arm_thm_movw_insert(insn, whole));
        return;
    }

    // A place-relative 31 bit offset, used by the exception tables.  The top
    // bit is not part of the offset and carries a flag of its own, so it
    // survives untouched.
    case R_ARM_PREL31: {
        u32 insn = read32le(ptr);
        i32 x = arm_sext(insn, 31) + (i32)(val - addr);

        if (x >= 0x40000000 || x < -0x40000000) {
            tli_error_noabort("can't relocate value at %x,%d", addr, type);
            return;
        }

        write32le(ptr, (insn & 0x80000000u) | ((u32)x & 0x7fffffff));
        return;
    }

    // S + A.  R_ARM_TARGET1 is whichever of ABS32 and REL32 the platform
    // chooses for .init_array entries; on this one it is ABS32.
    //
    // In a shared object the sum is not known until load time, so the
    // relocation is handed on: against an exported symbol it stays symbolic,
    // otherwise the loader need only add the load bias.
    case R_ARM_ABS32:
    case R_ARM_TARGET1:
        if (s1->output_type & TLI_OUTPUT_DYN) {
            sym_index = ELFW(R_SYM)(rel->r_info);
            esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index;
            qrel->r_offset = rel->r_offset;
            if (esym_index) {
                qrel->r_info = ELFW(R_INFO)(esym_index, R_ARM_ABS32);
                qrel++;
                return;
            }

            qrel->r_info = ELFW(R_INFO)(0, R_ARM_RELATIVE);
            qrel++;
        }

        add32le(ptr, val);
        return;

    // S + A - P
    case R_ARM_REL32:
        add32le(ptr, val - addr);
        return;

    // GOT + A - P, which recovers the GOT base into a register.
    case R_ARM_GOTPC:
        add32le(ptr, s1->got->sh_addr - addr);
        return;

    // S + A - GOT: the symbol addressed off the GOT base.
    case R_ARM_GOTOFF:
        add32le(ptr, val - s1->got->sh_addr);
        return;

    // G + A: the offset of the slot within the GOT, not its address.
    case R_ARM_GOT32:
        sym_index = ELFW(R_SYM)(rel->r_info);
        add32le(ptr, get_sym_attr(s1, sym_index, 0)->got_offset);
        return;

    // GOT + G + A - P: the same slot, reached from here.
    case R_ARM_GOT_PREL:
        sym_index = ELFW(R_SYM)(rel->r_info);
        add32le(ptr, s1->got->sh_addr
                     + get_sym_attr(s1, sym_index, 0)->got_offset - addr);
        return;

    // ARMv4 has no BX, so this marks a BX Rm that may be rewritten as
    // MOV PC, Rm.  That loses the instruction set change, which is the trade
    // the architecture offers: Thumb support for ARMv4 support.
    case R_ARM_V4BX: {
        u32 insn = read32le(ptr);

        if ((insn & 0x0ffffff0) == 0x012fff10)
            write32le(ptr, (insn & 0xf000000f) | 0x01a0f000u);

        return;
    }

    // Both are plain S: a GOT slot carries no addend of its own.
    case R_ARM_GLOB_DAT:
    case R_ARM_JUMP_SLOT:
        write32le(ptr, val);
        return;

    // The loader copies the initialiser out of the shared object at startup.
    // Nothing to write at link time.
    case R_ARM_COPY:
        return;

    // Nothing to do.  Used to record a dependency on a symbol without
    // referring to it, which is how EABI exception handling keeps the
    // personality routines alive.
    case R_ARM_NONE:
        return;

    case R_ARM_RELATIVE:
        // PE expresses the same thing against the image base.
        if (s1->fmt == TLI_FMT_PE)
            add32le(ptr, val - s1->pe_imagebase);

        return;

    // Variant I TLS: the thread pointer is below the block rather than above
    // it, so the offset is positive, and it clears the two word thread
    // control block that sits between them.
    case R_ARM_TLS_LE32: {
        i32 x;

        if (s1->tls_end) {
            x = (i32)(val - s1->tls_start + 8);
        } else {
            tli_Section *sec;

            sym_index = ELFW(R_SYM)(rel->r_info);
            sec = s1->sections[tli_sym_section(symtab_section, sym_index)];
            x = (i32)(val - sec->sh_addr + 8);
        }

        add32le(ptr, x);
        return;
    }

    default:
        fprintf(stderr, "FIXME: handle reloc type %d at %x [%p] to %x\n",
                type, (u32)addr, ptr, (u32)val);
        return;
    }
}

/// What the generic code reads about this machine.  ARM systems have used
/// 64KiB pages, so segments are aligned to the larger size.  A PLT entry
/// computes its GOT slot from the PC, so it works in a position-independent
/// image unchanged.
ST_DATA const tli_Arch tli_arch_arm = {
    .em = EM_ARM,
    .relocate = arm_relocate,
    .create_plt_entry = arm_create_plt_entry,
    .relocate_plt = arm_relocate_plt,
    .reloc_props = arm_reloc_props,
    .r_data_ptr = R_ARM_ABS32,
    .r_data_32dw = R_ARM_ABS32,
    .r_jmp_slot = R_ARM_JUMP_SLOT,
    .r_glob_dat = R_ARM_GLOB_DAT,
    .r_copy = R_ARM_COPY,
    .r_relative = R_ARM_RELATIVE,
    .start_addr = 0x00010000,
    .page_size = 0x10000,
    .pcrel_dllplt = 1,
    .elf_interp = "/lib/ld-linux.so.3",
    .build_reloc = {
        [1] = R_ARM_ABS32,
        [2] = R_ARM_ABS32,
        [3] = R_ARM_REL32,
        [4] = R_ARM_CALL,
    },
};

#endif

// The questions every architecture answers the same way, asked of s1->arch.

/// ---- src/core/tl_reloc_class.c ----

/// How a relocation is classified, asked once for every architecture.
///
/// Each architecture answers two questions about a relocation type: does it
/// patch an instruction or data, and does it need a GOT or PLT slot built for
/// it.  Both answers come out of one byte per type, in a table the
/// architecture declares and its own reloc_props() indexes.
///
/// The tables differ; the two questions do not. They lived in all five
/// architectures in identical copies until this file, which asks them of
/// whichever one s1->arch names.

/// Non-zero if this relocation patches an instruction rather than data, or -1
/// if the architecture does not name the type at all.
ST_FUNC i32 code_reloc(tli_State *s1, i32 reloc_type)
{
    u32 c = s1->arch->reloc_props(reloc_type);

    return c & RC_KNOWN ? (c & RC_CODE) != 0 : -1;
}

/// Which of NO_GOTPLT_ENTRY, BUILD_GOT_ONLY, AUTO_GOTPLT_ENTRY or
/// ALWAYS_GOTPLT_ENTRY this relocation wants, or -1 for a type the
/// architecture does not name. The class occupies the low two bits.
ST_FUNC i32 gotplt_entry_type(tli_State *s1, i32 reloc_type)
{
    u32 c = s1->arch->reloc_props(reloc_type);

    return c & RC_KNOWN ? (i32)(c & 3) : -1;
}

/// The dynamic relocation a GOT slot must carry for this relocation type, or
/// zero where the slot holds an ordinary address.  Thread-local storage is
/// the exception: its slot holds the symbol's position relative to the thread
/// pointer rather than an address, and that is not known until the TLS
/// segment has been laid out, so the slot is filled by a relocation of its
/// own rather than by the address the symbol ended up at.
///
/// This is per-architecture in a file that is otherwise not, because only
/// three of the five answer it with anything, and a copy in each of the five
/// would be two copies of "no".
ST_FUNC i32 got_tls_reloc(tli_State *s1, i32 reloc_type)
{
    switch (s1->arch->em) {
    case EM_X86_64:
        return reloc_type == R_X86_64_GOTTPOFF ? R_X86_64_TPOFF64 : 0;
    case EM_AARCH64:
        return reloc_type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21
               || reloc_type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC
               ? R_AARCH64_TLS_TPREL64 : 0;
    case EM_RISCV:
        if (reloc_type == R_RISCV_TLS_GOT_HI20)
            return R_RISCV_TLS_TPREL64;

        return reloc_type == R_RISCV_TLS_GD_HI20 ? R_RISCV_TLS_DTPMOD64 : 0;
    }
    return 0;
}

/// How many words of GOT a thread-local slot for this relocation occupies.
///
/// Initial exec needs one, holding an offset from the thread pointer.  Global
/// dynamic needs two: __tls_get_addr() is handed the address of the pair and
/// reads a module number and an offset within that module out of it.  Asking
/// about anything else gives one, which is what every other GOT entry is.
ST_FUNC i32 got_tls_words(tli_State *s1, i32 reloc_type)
{
    if (s1->arch->em == EM_RISCV && reloc_type == R_RISCV_TLS_GD_HI20)
        return 2;

    return 1;
}


// Every object format, since a link picks its own through s1->fmt.

/// ---- src/core/tl_pe_out.c ----

/// tinylinker - PE/COFF image writer
///
/// Writes the linked sections out as a Windows executable or DLL, and reads
/// back the three PE things a link needs from outside the object files: the
/// export names of a DLL, a `.def` file listing what to import from one, and
/// the compiled resources windres produces.
///
/// A PE image is an MS-DOS executable with a second header bolted on.  The
/// loader only cares about that second header: a COFF file header, an
/// "optional" header that is anything but optional for an image, and an array
/// of section headers.  Everything else the loader acts on - imports, exports,
/// base relocations, thread-local storage, the unwind tables - is ordinary
/// section contents which the optional header's sixteen data directories point
/// at by relative virtual address.  So most of the work here is building those
/// tables into a section like any other, and remembering where they landed.


#ifdef _WIN32
# define strcasecmp _stricmp
#else
# include <sys/stat.h> // chmod()
#endif

// -------------------------------------------------------------------------
// what the target fixes

/// The machine number, the two image bases, the oldest Windows that will load
/// the image and the one relocation type a .rsrc section may carry.  All four
/// are settled by the architecture, and read from its tli_Arch.
#define IMAGE_FILE_MACHINE TLI_STATE_VAR(arch)->pe_machine
#define PE_IMAGE_BASE_EXE  TLI_STATE_VAR(arch)->pe_image_base_exe
#define PE_IMAGE_BASE_DLL  TLI_STATE_VAR(arch)->pe_image_base_dll
#define PE_OS_VERSION      TLI_STATE_VAR(arch)->pe_os_version
#define PE_RSRC_RELTYPE    TLI_STATE_VAR(arch)->pe_rsrc_reltype

/// The relocation types the writer speaks to the architecture backends in.
/// R_PE_ABS is an absolute pointer, and is the only kind that needs an entry in
/// .reloc; R_PE_RVA is the backends' spelling of "store an image-relative
/// address here", which the data directories are full of; R_PE_THUNK patches
/// the import thunk's operand, which is pc-relative where the architecture can
/// reach the whole image that way and absolute where it cannot.
#define R_PE_ABS   TLI_STATE_VAR(arch)->r_pe_abs
#define R_PE_RVA   TLI_STATE_VAR(arch)->r_pe_rva
#define R_PE_THUNK TLI_STATE_VAR(arch)->r_pe_thunk

/// The three places the pointer width shows in the file: which optional header
/// is being written, how a base relocation names its fixup, and which bit of an
/// import slot marks it as naming an ordinal.
#if PTR_SIZE == 8
# define PE_OPT_MAGIC     0x020B  /// PE32+
# define PE_RELOC_KIND    10      /// IMAGE_REL_BASED_DIR64
# define PE_ORDINAL_FLAG  0x8000000000000000ULL
#else
# define PE_OPT_MAGIC     0x010B  /// PE32
# define PE_RELOC_KIND    3       /// IMAGE_REL_BASED_HIGHLOW
# define PE_ORDINAL_FLAG  0x80000000UL
#endif

/// The characteristics every image from here starts with, which differ by
/// architecture: see each *-link.c file's tli_Arch.
#define PE_CHARACTERISTICS TLI_STATE_VAR(arch)->pe_characteristics

/// IMAGE_DLLCHARACTERISTICS: only the one the writer itself reacts to.
#define PE_DLL_DYNAMIC_BASE 0x0040

/// tli_Section characteristics, of which only these seven are ever set here: the
/// three that say what a section holds, and the four page permissions.
#define PE_SCN_CNT_CODE          0x00000020
#define PE_SCN_CNT_INITIALIZED   0x00000040
#define PE_SCN_CNT_UNINITIALIZED 0x00000080
#define PE_SCN_MEM_DISCARDABLE   0x02000000
#define PE_SCN_MEM_EXECUTE       0x20000000
#define PE_SCN_MEM_READ          0x40000000
#define PE_SCN_MEM_WRITE         0x80000000

/// Data directory slots.  The numbers are the format's; the gaps are the
/// directories tinylinker never fills in.
#define PE_DIR_EXPORT    0
#define PE_DIR_IMPORT    1
#define PE_DIR_RESOURCE  2
#define PE_DIR_EXCEPTION 3
#define PE_DIR_BASERELOC 5
#define PE_DIR_TLS       9
#define PE_DIR_IAT      12
#define PE_NUM_DIRS     16

/// Sizes of the on-disk records built into section data below.  They are
/// written field by field rather than through a struct, so the sizes are
/// spelled out here instead of coming from sizeof.
#define PE_IMPORT_DESC_SIZE  20
#define PE_EXPORT_DIR_SIZE   40
#define PE_RELOC_BLOCK_SIZE   8
#define PE_TLS_DIR_SIZE      (PTR_SIZE * 4 + 8)

/// What kind of image is being produced.  PE_RUN is not a file at all: it is
/// the in-memory link, which still needs the import table filled in.
enum { PE_EXE, PE_GUI, PE_DLL, PE_RUN };

// -------------------------------------------------------------------------
// the header records

/// These records are laid out by the file format rather than by the compiler,
/// so nothing may be inserted between their fields and they are packed.  They
/// are little-endian throughout, as is every host this builds for, so a record
/// can be written straight out.
#pragma pack(push, 1)

/// A Windows host has already declared these two under exactly these names,
/// with the same layout, and tl_coff_obj.c uses whichever set is in scope.
#ifndef IMAGE_NT_SIGNATURE
#define IMAGE_NT_SIGNATURE 0x00004550  /// "PE\0\0"

/// The COFF file header, which opens an object and follows the signature in an
/// image.
typedef struct {
    u16 Machine;
    u16 NumberOfSections;
    u32 TimeDateStamp;
    u32 PointerToSymbolTable;
    u32 NumberOfSymbols;
    u16 SizeOfOptionalHeader;   /// Zero in an object, nonzero in an image.
    u16 Characteristics;
} IMAGE_FILE_HEADER;

/// One section header.  An object leaves VirtualAddress zero and uses the
/// relocation and line number fields; an image is the other way round.
typedef struct {
    u8  Name[8];                /// Not NUL-terminated when it fills up.
    union {
        u32 PhysicalAddress;
        u32 VirtualSize;        /// The image meaning; bytes of address space.
    } Misc;
    u32 VirtualAddress;
    u32 SizeOfRawData;
    u32 PointerToRawData;
    u32 PointerToRelocations;
    u32 PointerToLinenumbers;
    u16 NumberOfRelocations;
    u16 NumberOfLinenumbers;
    u32 Characteristics;
} IMAGE_SECTION_HEADER;
#endif // IMAGE_NT_SIGNATURE

/// The width of the optional header's address-sized fields, which follows the
/// target being linked for rather than the machine doing the linking.
#if PTR_SIZE == 8
typedef u64 pe_addr;
#else
typedef u32 pe_addr;
#endif

/// One of the sixteen data directories: where a table lives in the image, and
/// how big it is.
struct pe_datadir {
    u32 VirtualAddress;
    u32 Size;
};

/// The optional header, which is anything but optional in an image: 224 bytes
/// for PE32 and 240 for PE32+, which is what SizeOfOptionalHeader says.  It is
/// deliberately not taken from the host's windows.h, because the host's
/// pointer width is not necessarily the target's.
struct pe_opt_header {
    u16 Magic;
    u8  MajorLinkerVersion, MinorLinkerVersion;
    u32 SizeOfCode;
    u32 SizeOfInitializedData;
    u32 SizeOfUninitializedData;
    u32 AddressOfEntryPoint;
    u32 BaseOfCode;
#if PTR_SIZE == 4
    u32 BaseOfData;             /// PE32 only; PE32+ dropped it.
#endif
    pe_addr ImageBase;
    u32 SectionAlignment;       /// What a section is rounded to in memory.
    u32 FileAlignment;          /// And on disk; the format keeps the two apart.
    u16 MajorOperatingSystemVersion, MinorOperatingSystemVersion;
    u16 MajorImageVersion, MinorImageVersion;
    u16 MajorSubsystemVersion, MinorSubsystemVersion;
    u32 Win32VersionValue;
    u32 SizeOfImage;
    u32 SizeOfHeaders;
    u32 CheckSum;
    u16 Subsystem;
    u16 DllCharacteristics;
    pe_addr SizeOfStackReserve, SizeOfStackCommit;
    pe_addr SizeOfHeapReserve, SizeOfHeapCommit;
    u32 LoaderFlags;
    u32 NumberOfRvaAndSizes;
    struct pe_datadir DataDirectory[PE_NUM_DIRS];
};

/// The header of a compiled resource file, which is a COFF object carrying one
/// .rsrc section and nothing else.
struct pe_rsrc_header {
    IMAGE_FILE_HEADER filehdr;
    IMAGE_SECTION_HEADER sectionhdr;
};

/// A COFF symbol table entry, which is the only debug information a PE image
/// from here carries: gdb will read it when nothing better is present.
struct pe_coffsym {
    union {
        char n_name[8];          /// A name of eight characters or fewer.
        struct { i32 n_zeroes, n_offset; };  /// Otherwise a string offset.
    };
    i32 n_value;
    i16 n_scnum;    /// 1-based section, or 0/-1/-2 for the special ones.
    u16 n_type;
    u8  n_sclass;
    u8  n_numaux;
};

#pragma pack(pop)

/// n_sclass for an external symbol, which is the only class written out.
#define PE_COFF_C_EXT 2

/// Where the real header begins, and so how long the DOS part below is.
///
/// Every PE file opens with a complete MS-DOS executable.  Three things in it
/// still matter: the "MZ" signature, the doubleword at 0x3C saying where the
/// real header begins, and a 16-bit stub that prints a refusal if the image is
/// ever run under DOS.  Nothing has run DOS in decades but tools do check that
/// the stub is there, so it is written out anyway.
#define PE_HDR_OFFSET 0x80

/// The DOS executable and stub every image starts with, byte for byte.
static const u8 pe_dos_header[PE_HDR_OFFSET] = {
    // the DOS EXE header, 64 bytes
    'M', 'Z',   0x90, 0x00,  0x03, 0x00,  0x00, 0x00, // magic, cblp, cp, crlc
    0x04, 0x00, 0x00, 0x00,  0xFF, 0xFF,  0x00, 0x00, // cparhdr, minalloc, maxalloc, ss
    0xB8, 0x00, 0x00, 0x00,  0x00, 0x00,  0x00, 0x00, // sp, csum, ip, cs
    0x40, 0x00, 0x00, 0x00,                           // lfarlc, ovno
    0, 0, 0, 0, 0, 0, 0, 0,                           // res[4]
    0x00, 0x00, 0x00, 0x00,                           // oemid, oeminfo
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,                     // res2[10]
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    PE_HDR_OFFSET, 0x00, 0x00, 0x00,                  // lfanew

    // the stub, 64 bytes: DOS loads it at the paragraph after the header, so
    // the string it prints sits at ds:0x0E
    0x0E,                    // push cs
    0x1F,                    // pop  ds
    0xBA, 0x0E, 0x00,        // mov  dx, 0x000E
    0xB4, 0x09,              // mov  ah, 9
    0xCD, 0x21,              // int  0x21
    0xB8, 0x01, 0x4C,        // mov  ax, 0x4C01
    0xCD, 0x21,              // int  0x21
    'T','h','i','s',' ','p','r','o','g','r','a','m',' ',
    'c','a','n','n','o','t',' ','b','e',' ','r','u','n',' ',
    'i','n',' ','D','O','S',' ','m','o','d','e','.', '\r', '\r', '\n', '$',
    0, 0, 0, 0, 0, 0, 0
};

// -------------------------------------------------------------------------
// the writer's own state

/// Where an input section ends up.  Sections of the same kind are laid end to
/// end inside one output section, because a PE section costs a page of address
/// space and a header, and because the loader wants one contiguous run per set
/// of page permissions.
enum {
    PE_CLS_TEXT,     /// Executable.
    PE_CLS_RDATA,    /// Read-only data; also where the linker's tables go.
    PE_CLS_PDATA,    /// The exception directory.
    PE_CLS_RSRC,     /// The resource directory.
    PE_CLS_DATA,     /// Writable data.
    PE_CLS_BSS,      /// ... and its zero-filled tail, merged into the above.
    PE_CLS_TLS,      /// Thread-local data, which the loader copies per thread.
    PE_CLS_OTHER,    /// Allocated but none of the above.
    PE_CLS_DEBUG,    /// Not loaded, kept for debuggers.
    PE_CLS_RELOC,    /// The base relocation table.
    PE_CLS_DROP      /// Not part of the image at all.
};

/// One output section: the run of input sections merged into it, and the
/// header fields that describe the result.
struct pe_sect {
    char name[32];
    i32 cls;             /// Which PE_CLS_* the parts share.
    addr_t addr;         /// Virtual address, still including the image base.
    u32 vsize;           /// Bytes of address space it covers.
    u32 rawsize;         /// Bytes of it that come from the file.
    u32 flags;           /// PE_SCN_*.
    u32 file_off;        /// Where its contents start in the file.
    tli_Section **parts;     /// The input sections in it, in address order.
    i32 nparts;
};

/// One symbol imported from one DLL.
struct pe_import {
    i32 dyn_index;       /// Entry in dynsymtab_section: the name and ordinal.
    u32 thunk_off;       /// The jump thunk in .text, 0 if the symbol is data.
    i32 *patch;          /// Symtab entries to point at the IAT slot.
    i32 npatch, apatch;
};

/// One DLL being imported from, and everything wanted out of it.
struct pe_dll {
    i32 ref;             /// 1-based into s1->loaded_dlls, 0 for -run addresses.
    struct pe_import *syms;
    i32 nsyms, asyms;
};

/// The output file, with the image checksum accumulated as it goes.
struct pe_out {
    FILE *fp;
    u32 pos;             /// Bytes written so far, which is the file offset.
    u32 sum;             /// The running checksum.
    i32 odd;             /// A byte left over from an odd-length write.
    u8 half;             /// That byte, waiting for the one it pairs with.
};

/// Everything one call to pe_output_file() has to carry between its stages.
struct pe_state {
    tli_State *s1;
    const char *filename;
    i32 type;                    /// PE_EXE / PE_GUI / PE_DLL / PE_RUN.
    const char *entry_symbol;

    addr_t imagebase;
    u32 section_align, file_align;
    i32 subsystem;

    tli_Section *tables;             /// .rdata: home of the tables built here.
    tli_Section *reloc;              /// .reloc, when the image can be moved.
    tli_Section *coffsym, *coffstr;  /// The COFF symbol table, when -g.

    u32 import_rva, import_size;
    u32 iat_rva, iat_size;
    u32 export_rva, export_size;
    u32 tls_dir;                 /// Offset of the TLS directory in `tables`.
    u32 tls_size;
    u32 tls_index;               /// Offset in .data of the index block.
    u32 tls_used_rva;            /// The runtime's own TLS directory, _tls_used.

    u32 entry_rva;

    struct pe_sect *sects;
    i32 nsects, asects;
    i32 *sect_of;                /// ELF section number -> 1-based output one.
    i32 nsect_of;
    struct pe_dll *dlls;
    i32 ndlls, adlls;
};

/// Grow a value array to twice its capacity, or to eight elements from empty.
/// Values rather than pointers keeps each table in one allocation, at the price
/// of having to refetch a pointer after any further append.
static void pe_regrow(void **pa, i32 *cap, usize size)
{
    *cap = *cap ? *cap * 2 : 8;
    *pa = tli_realloc(*pa, *cap * size);
}

/// Append a zeroed element to one of those arrays and yield a pointer to it.
/// The pointer is only good until the next append to the same array.
#define pe_push(a, n, cap) \
    ((n) < (cap) ? (void)0 : pe_regrow((void **)&(a), &(cap), sizeof *(a)), \
     memset(&(a)[n], 0, sizeof *(a)), &(a)[(n)++])

// -------------------------------------------------------------------------
// odds and ends

/// Round up to a power-of-two alignment.  It takes an address because that is
/// the widest thing aligned here; a file offset is narrower and rounds the
/// same, since neither ever comes near the top of the range.
static addr_t pe_align_up(addr_t n, u32 a)
{
    return (n + a - 1) & ~(addr_t)(a - 1);
}

/// Write a value the width of a target pointer, which is how the import
/// tables and the TLS directory hold every address in them.
static void pe_write_ptr(u8 *p, addr_t v)
{
#if PTR_SIZE == 8
    write64le(p, v);
#else
    write32le(p, (u32)v);
#endif
}

/// An RVA is what nearly every field in a PE header holds: an offset from where
/// the image was loaded, which is the image base until something moves it.
static u32 pe_rva(struct pe_state *pe, addr_t addr)
{
    return (u32)(addr - pe->imagebase);
}

/// Round a section's contents up so the next thing put in it starts aligned.
static void pe_pad_section(tli_Section *s, i32 align)
{
    i32 slack = s->data_offset & (align - 1);

    if (slack)
        section_ptr_add(s, align - slack);
}

/// The name a symbol is exported or imported under, which is not always the
/// name the object file spells it with.
static const char *pe_export_name(tli_State *s1, ElfW(Sym) *sym)
{
    const char *name = (char *)symtab_section->link->data + sym->st_name;

    if (s1->leading_underscore && name[0] == '_')
        return name + 1;

    return name;
}

// -------------------------------------------------------------------------
// writing bytes, and the checksum that goes with them

/// Fold a run of bytes into the running checksum.
///
/// The image checksum is a 16-bit ones-complement sum of the whole file taken
/// as little-endian halfwords, with this field read as zero, plus the file's
/// length.  Windows only insists on it for images it loads into the kernel, but
/// it costs one add per halfword to get right.  The pairing runs across the
/// whole file, so an odd-length write has to leave its last byte pending for
/// whatever is written next.
static void pe_sum(struct pe_out *o, const u8 *p, usize n)
{
    u32 sum = o->sum;

    // Pair the byte left pending by the previous write with the first of this
    // one, before the run can be walked two bytes at a time.
    if (n && o->odd) {
        sum += o->half | (u32)*p++ << 8;
        sum = (sum + (sum >> 16)) & 0xFFFF;
        o->odd = 0;
        n--;
    }

    for (; n >= 2; n -= 2, p += 2) {
        sum += read16le((u8 *)p);
        sum = (sum + (sum >> 16)) & 0xFFFF;
    }

    if (n)
        o->odd = 1, o->half = *p;

    o->sum = sum;
}

/// Write one record out, checksumming it on the way past.
static i32 pe_emit(struct pe_out *o, const void *data, usize len)
{
    if (len == 0)
        return 0;

    pe_sum(o, data, len);
    o->pos += (u32)len;
    return fwrite(data, 1, len, o->fp) == len ? 0 : -1;
}

/// Advance to a file offset, filling the gap with zeroes.  Zeroes add nothing
/// to the checksum, so only the odd-byte carry has to be kept honest.
static void pe_emit_pad(struct pe_out *o, u32 to)
{
    static const u8 zeroes[256];
    u32 gap;

    if (to <= o->pos)
        return;

    gap = to - o->pos;

    // The one zero that pairs with a pending byte does have to go through the
    // checksum; every zero after it cannot change the sum.
    if (gap && o->odd) {
        pe_sum(o, zeroes, 1);
        fwrite(zeroes, 1, 1, o->fp);
        o->pos++;
        gap--;
    }

    while (gap) {
        u32 n = gap < sizeof zeroes ? gap : sizeof zeroes;
        fwrite(zeroes, 1, n, o->fp);
        gap -= n;
    }

    o->pos = to;
}

// -------------------------------------------------------------------------
// imports
//
// The import directory has one descriptor per DLL, naming it and pointing at
// two parallel arrays of pointer-sized slots: the lookup table, saying what to
// fetch, and the address table, which the loader overwrites with what it
// fetched.  Both start out identical; only the second is read at runtime.  The
// duplication is what lets a tool bind an image to a particular DLL version
// and still know what it originally asked for.
//
// Not every architecture can call through an address table slot in one
// instruction, so each imported function also gets a small thunk in .text that
// loads its slot and jumps.  Calls resolve to the thunk, which is why the
// thunks have to exist before any address is handed out.

/// Find, or start, the record for one DLL.  `ref` is 1-based into the loaded
/// DLLs, and zero stands for the addresses handed over by tli_add_symbol().
static struct pe_dll *pe_dll_for(struct pe_state *pe, i32 ref)
{
    struct pe_dll *d;
    i32 i;

    for (i = 0; i < pe->ndlls; i++)
        if (pe->dlls[i].ref == ref)
            return &pe->dlls[i];

    d = pe_push(pe->dlls, pe->ndlls, pe->adlls);
    d->ref = ref;
    return d;
}

/// Find, or start, the record for one symbol imported from one DLL.  The
/// returned pointer stays valid only until the next call.
static struct pe_import *pe_import_for(struct pe_state *pe, i32 dyn_index)
{
    ElfW(Sym) *dsym;
    struct pe_dll *d;
    struct pe_import *im;
    i32 i;

    dsym = (ElfW(Sym) *)pe->s1->dynsymtab_section->data + dyn_index;
    d = pe_dll_for(pe, dsym->st_size); // pe_putimport() parks it in st_size

    for (i = 0; i < d->nsyms; i++)
        if (d->syms[i].dyn_index == dyn_index)
            return &d->syms[i];

    im = pe_push(d->syms, d->nsyms, d->asyms);
    im->dyn_index = dyn_index;
    return im;
}

/// Record that this symbol table entry has to end up naming the import's slot
/// in the address table.
static void pe_patch_later(struct pe_import *im, i32 sym_index)
{
    *(i32 *)pe_push(im->patch, im->npatch, im->apatch) = sym_index;
}

/// Release the import tables, which are the writer's own and outlive nothing.
static void pe_free_imports(struct pe_state *pe)
{
    i32 i, j;

    for (i = 0; i < pe->ndlls; i++) {
        for (j = 0; j < pe->dlls[i].nsyms; j++)
            tli_free(pe->dlls[i].syms[j].patch);

        tli_free(pe->dlls[i].syms);
    }

    tli_free(pe->dlls);
    pe->dlls = NULL;
    pe->ndlls = pe->adlls = 0;
}

/// Emit the jump-to-slot thunk for one imported function and return its offset
/// in .text.  `slot` is an undefined symbol standing for the address table
/// entry; it gets a value once the tables are laid out.
static u32 pe_emit_thunk(tli_State *s1, i32 slot)
{
    u32 off = text_section->data_offset;
    u8 *p;

    // No arm64 instruction reaches an arbitrary slot, so the address of the
    // slot is stored inline behind the code and loaded pc-relative.
    if (s1->arch->em == EM_AARCH64) {
        p = section_ptr_add(text_section, 24);
        write32le(p + 0, 0x58000090);   // ldr x16, [pc, #16]
        write32le(p + 4, 0xf9400210);   // ldr x16, [x16]
        write32le(p + 8, 0xd61f0200);   // br  x16
        write32le(p + 12, 0xd503201f);  // nop, so the literal stays 8-aligned
        put_elf_reloc(symtab_section, text_section, off + 16, R_PE_THUNK, slot);
        return off;
    }

    // The two x86 machines, the only others PE is written for, jump through
    // the slot in one instruction.
    p = section_ptr_add(text_section, 8);
    write16le(p, 0x25FF);           // jmp [...]

    // rip-relative: the displacement counts from the end of the instruction,
    // so the addend is -4.
    if (s1->arch->em == EM_X86_64)
        write32le(p + 2, (u32)-4);

    put_elf_reloc(symtab_section, text_section, off + 2, R_PE_THUNK, slot);
    return off;
}

/// Build the import directory, the lookup table, the address table and the
/// hint/name entries, all into the tables section.
static void pe_build_imports(struct pe_state *pe)
{
    tli_State *s1 = pe->s1;
    tli_Section *t = pe->tables;
    u32 rva = pe_rva(pe, t->sh_addr);
    u32 desc, iat, lookup;
    i32 i, j, k, nsyms = 0;

    for (i = 0; i < pe->ndlls; i++)
        nsyms += pe->dlls[i].nsyms;

    if (nsyms == 0)
        return;

    // Reserve the fixed-size part in one go: one descriptor per DLL plus a
    // zeroed terminator; one slot per symbol plus a zeroed terminator per DLL,
    // twice over.
    pe_pad_section(t, 16);
    pe->import_size = (pe->ndlls + 1) * PE_IMPORT_DESC_SIZE;
    pe->iat_size = (nsyms + pe->ndlls) * PTR_SIZE;

    desc = (u32)t->data_offset;
    iat = desc + pe->import_size;
    lookup = iat + pe->iat_size;
    section_ptr_add(t, pe->import_size + 2 * pe->iat_size);

    pe->import_rva = desc + rva;
    pe->iat_rva = iat + rva;

    for (i = 0; i < pe->ndlls; i++, desc += PE_IMPORT_DESC_SIZE) {
        struct pe_dll *d = &pe->dlls[i];
        tli_DLLReference *ref = d->ref ? s1->loaded_dlls[d->ref - 1] : NULL;
        u32 name_off = put_elf_str(t, ref ? tli_basename(ref->name) : "");

        write32le(t->data + desc + 0, lookup + rva);    // OriginalFirstThunk
        write32le(t->data + desc + 12, name_off + rva); // Name
        write32le(t->data + desc + 16, iat + rva);      // FirstThunk

        for (j = 0; j < d->nsyms; j++, iat += PTR_SIZE, lookup += PTR_SIZE) {
            struct pe_import *im = &d->syms[j];
            ElfW(Sym) *dsym = (ElfW(Sym) *)s1->dynsymtab_section->data
                              + im->dyn_index;
            const char *name = (char *)s1->dynsymtab_section->link->data
                               + dsym->st_name;
            addr_t v;

            // Everything that referred to this import now refers to its slot,
            // either directly or through the thunk that loads it.
            for (k = 0; k < im->npatch; k++) {
                ElfW(Sym) *es = (ElfW(Sym) *)symtab_section->data + im->patch[k];
                es->st_value = iat;
                es->st_shndx = t->sh_num;
            }

#if defined TLI_HOST_RUN && defined _WIN32
            if (pe->type == PE_RUN) {

                // Nothing will map this image, so there is no loader to fill
                // the address table in and it has to be done here.
                if (ref) {
                    if (!ref->handle)
                        ref->handle = LoadLibraryA(ref->name);

                    v = (addr_t)GetProcAddress(ref->handle,
                            dsym->st_value ? (char *)0 + dsym->st_value : name);
                } else {

                    // an address handed over by tli_add_symbol()
                    v = dsym->st_value;
                }

                if (!v)
                    tli_error_noabort("could not resolve symbol '%s'", name);
            } else
#endif
            if (ref && dsym->st_value) {

                // An ordinal, from a `.def` file's "@n".  The top bit says the
                // slot names one instead of pointing at a hint/name entry.
                v = dsym->st_value | PE_ORDINAL_FLAG;
            } else {

                // A hint/name entry: a hint the loader may use to guess where
                // in the export table to look, then the name.  The format
                // requires it to start on an even offset.
                pe_pad_section(t, 2);
                v = (addr_t)t->data_offset + rva;
                section_ptr_add(t, sizeof(u16));
                put_elf_str(t, name);
            }

            pe_write_ptr(t->data + iat, v);
            pe_write_ptr(t->data + lookup, v);
        }

        // Skip the terminating zero slot in each of the two arrays.
        iat += PTR_SIZE;
        lookup += PTR_SIZE;
    }
}

// -------------------------------------------------------------------------
// exports
//
// The export directory holds three parallel arrays: the addresses of the
// exported things by ordinal, the names, and for each name the ordinal it
// belongs to.  The names have to be sorted, because that is how GetProcAddress
// finds one.

/// One name on its way into the export table, paired with the symbol whose
/// address the table will eventually hold.
struct pe_export {
    i32 sym;             /// Index into symtab_section.
    const char *name;    /// The undecorated name the DLL will export.
};

/// Order two exports by name, which is the order GetProcAddress searches in.
static i32 pe_export_cmp(const void *a, const void *b, void *arg)
{
    (void)arg;
    return strcmp(((const struct pe_export *)a)->name,
                  ((const struct pe_export *)b)->name);
}

/// Write the same list out as a linker definition file next to the image, so
/// the DLL can be imported from without reading the image back.
static void pe_write_def(struct pe_state *pe, const char *dllname,
                         const struct pe_export *ex, i32 n)
{
    tli_State *s1 = pe->s1;
    char path[1024];
    FILE *fp;
    i32 i;

    if (strlen(pe->filename) + sizeof ".def" > sizeof path)
        return;

    strcpy(path, pe->filename);
    strcpy(tli_fileextension(path), ".def");
    fp = fopen(path, "wb");
    if (!fp) {
        tli_error_noabort("could not create '%s': %s", path, strerror(errno));
        return;
    }

    fprintf(fp, "LIBRARY %s\n\nEXPORTS\n", dllname);
    for (i = 0; i < n; i++)
        fprintf(fp, "%s\n", ex[i].name);

    fclose(fp);

    if (s1->verbose)
        printf("<- %s (%d symbol%s)\n", path, n, n == 1 ? "" : "s");
}

/// Build the export directory and its three arrays into the tables section.
static void pe_build_exports(struct pe_state *pe)
{
    tli_State *s1 = pe->s1;
    tli_Section *t = pe->tables;
    u32 rva = pe_rva(pe, t->sh_addr);
    u32 base, funcs, names, ords, strings;
    struct pe_export *ex = NULL;
    i32 n = 0, cap = 0, i, last;
    const char *dllname;

    // Collect everything marked for export, which is what pe_bind_imports()
    // and the `.def` reader between them have decided.
    last = symtab_section->data_offset / sizeof(ElfW(Sym));
    for (i = 1; i < last; i++) {
        ElfW(Sym) *sym = (ElfW(Sym) *)symtab_section->data + i;
        struct pe_export *e;

        if (!(sym->st_other & ST_PE_EXPORT))
            continue;

        e = pe_push(ex, n, cap);
        e->sym = i;
        e->name = pe_export_name(s1, sym);
    }

    if (n == 0)
        return;

    tl_sort(ex, n, sizeof *ex, pe_export_cmp, NULL);

    // Reserve the directory and the three arrays; the names go on the end, one
    // at a time, as each entry is filled in.
    pe_pad_section(t, 16);
    dllname = tli_basename(pe->filename);

    base = (u32)t->data_offset;
    funcs = base + PE_EXPORT_DIR_SIZE;
    names = funcs + n * sizeof(u32);
    ords = names + n * sizeof(u32);
    strings = ords + n * sizeof(u16);
    section_ptr_add(t, strings - base);

    write32le(t->data + base + 0, 0);          // Characteristics
    write32le(t->data + base + 4, 0);          // TimeDateStamp
    write16le(t->data + base + 8, 0);          // MajorVersion
    write16le(t->data + base + 10, 0);         // MinorVersion
    write32le(t->data + base + 12, strings + rva);   // Name
    write32le(t->data + base + 16, 1);         // Base: first ordinal
    write32le(t->data + base + 20, n);         // NumberOfFunctions
    write32le(t->data + base + 24, n);         // NumberOfNames
    write32le(t->data + base + 28, funcs + rva);
    write32le(t->data + base + 32, names + rva);
    write32le(t->data + base + 36, ords + rva);

    // The directory's Name field points here, so the string has to be first.
    put_elf_str(t, dllname);

    for (i = 0; i < n; i++) {

        // The address is not known yet; a relocation puts the RVA in once the
        // symbol has one.
        put_elf_reloc(symtab_section, t, funcs + i * sizeof(u32),
                      R_PE_RVA, ex[i].sym);
        write32le(t->data + names + i * sizeof(u32),
                  (u32)t->data_offset + rva);
        write16le(t->data + ords + i * sizeof(u16), (u16)i);
        put_elf_str(t, ex[i].name);
    }

    pe->export_rva = base + rva;
    pe->export_size = (u32)t->data_offset - base;

    pe_write_def(pe, dllname, ex, n);
    tli_free(ex);
}

// -------------------------------------------------------------------------
// base relocations
//
// If the image cannot be loaded where it asked to be, the loader adds the
// difference to every absolute address in it.  .reloc says where those are,
// as a list of blocks each covering one 4 KiB page: a header giving the page
// and the block's length, then one halfword per fixup holding a four-bit kind
// and the twelve-bit offset within the page.

/// Finish the block being filled in: the format wants a whole number of
/// doublewords, so an odd number of fixups needs one more entry, and kind zero
/// is the one the loader ignores.
static void pe_close_reloc_block(struct pe_state *pe, u32 block,
                                 u32 count, addr_t page)
{
    tli_Section *out = pe->reloc;

    if (count & 1)
        section_ptr_add(out, sizeof(u16)), count++;

    write32le(out->data + block, pe_rva(pe, page));
    write32le(out->data + block + 4,
              count * sizeof(u16) + PE_RELOC_BLOCK_SIZE);
}

/// Walk every relocation in the image and write .reloc out of the absolute
/// ones, which are the only kind that survive being moved.
static void pe_build_relocs(struct pe_state *pe)
{
    tli_State *s1 = pe->s1;
    tli_Section *out = pe->reloc;
    u32 block = 0, count = 0;
    addr_t page = 0;
    i32 i, j;

    for (i = 0; i < pe->nsects; i++) {
        for (j = 0; j < pe->sects[i].nparts; j++) {
            tli_Section *s = pe->sects[i].parts[j];
            tli_Section *sr = s->reloc;
            ElfW_Rel *rel, *rel_end;
            i32 in_dwarf;

            if (!sr)
                continue;

            // Debug sections are not mapped, so a pointer from one debug
            // section into another never needs fixing up at load time.
            in_dwarf = s->sh_num >= s1->dwlo && s->sh_num < s1->dwhi;
            rel = (ElfW_Rel *)sr->data;
            rel_end = (ElfW_Rel *)(sr->data + sr->data_offset);

            for (; rel < rel_end; rel++) {
                addr_t at;

                if (ELFW(R_TYPE)(rel->r_info) != R_PE_ABS)
                    continue;

                // A debug-to-debug pointer is the case the flag above is for.
                if (in_dwarf) {
                    i32 n = ((ElfW(Sym) *)s1->symtab->data
                             + ELFW(R_SYM)(rel->r_info))->st_shndx;

                    if (n >= s1->dwlo && n < s1->dwhi)
                        continue;
                }

                // Start a new block whenever the fixup leaves the page the
                // open one covers.
                at = s->sh_addr + rel->r_offset;
                if (count == 0 || at - page >= 0x1000) {
                    if (count)
                        pe_close_reloc_block(pe, block, count, page);

                    block = (u32)out->data_offset;
                    section_ptr_add(out, PE_RELOC_BLOCK_SIZE);
                    page = at & ~(addr_t)0xFFF;
                    count = 0;
                }

                write16le(section_ptr_add(out, sizeof(u16)),
                          (u16)((at - page) | PE_RELOC_KIND << 12));
                count++;
            }
        }
    }

    if (count)
        pe_close_reloc_block(pe, block, count, page);
}

// -------------------------------------------------------------------------
// thread-local storage
//
// The TLS directory is four addresses and two words: the bounds of the
// template the loader copies for each thread, where to store the index it
// assigns the module, and a list of callbacks to run on thread creation.
// Those are addresses rather than RVAs, so each is written as an offset from
// .data and given a relocation that turns it into the real thing.

/// Reserve the TLS directory and the block in .data it points at, before any
/// TLS section has been placed.
static void pe_reserve_tls(struct pe_state *pe)
{
    tli_State *s1 = pe->s1;
    i32 sym, i;

    // The index, then a callback list that is empty but must exist.
    pe->tls_dir = (u32)section_add(pe->tables, pe->tls_size, 16);
    pe->tls_index = (u32)section_add(data_section, PTR_SIZE * 4, 16);

    sym = put_elf_sym(symtab_section, 0, 0, 0, 0, data_section->sh_num, 0);
    for (i = 0; i < 4; i++)
        put_elf_reloc(symtab_section, pe->tables,
                      pe->tls_dir + PTR_SIZE * i, R_PE_ABS, sym);

    // A code generator emitting __tls_index references can find it by name.
    set_elf_sym(symtab_section, pe->tls_index, PTR_SIZE * 4,
                ELFW(ST_INFO)(STB_GLOBAL, STT_OBJECT), 0,
                data_section->sh_num, "__tls_index");
}

/// Called once per TLS section as it is given an address, widening the template
/// to cover it.
static void pe_extend_tls(struct pe_state *pe, tli_Section *s)
{
    tli_State *s1 = pe->s1;
    u8 *d = pe->tables->data + pe->tls_dir;
    addr_t data = data_section->sh_addr;

    if (0 == s1->tls_start)
        s1->tls_start = s->sh_addr;

    pe_write_ptr(d, s1->tls_start - data);
    pe_write_ptr(d + PTR_SIZE, s->sh_addr + s->data_offset - data);
    pe_write_ptr(d + PTR_SIZE * 2, pe->tls_index);
    pe_write_ptr(d + PTR_SIZE * 3, pe->tls_index + PTR_SIZE);
    write32le(d + PTR_SIZE * 4, 0);      // SizeOfZeroFill
    write32le(d + PTR_SIZE * 4 + 4, 0);  // Characteristics

    // The architecture backends resolve TPOFF relocations against tls_end the
    // way they do on ELF; on PE the offsets are from the start.
    s1->tls_end = s1->tls_start;
}

// -------------------------------------------------------------------------
// laying the image out

/// Which output section an input section belongs in, from its name, its type
/// and its permissions.
static i32 pe_section_class(tli_Section *s)
{
    const char *name = s->name;
    i32 flags = s->sh_flags;

    if (0 == strncmp(name, ".stab", 5) || 0 == strncmp(name, ".debug_", 7))
        return PE_CLS_DEBUG;

    if (!(flags & SHF_ALLOC))
        return 0 == strcmp(name, ".reloc") ? PE_CLS_RELOC : PE_CLS_DROP;

    // What the section holds decides before what may be done with it: thread
    // data and zero-filled data are laid out differently either way.
    if (flags & SHF_TLS)
        return PE_CLS_TLS;

    if (s->sh_type == SHT_NOBITS)
        return PE_CLS_BSS;

    if (s->sh_type != SHT_PROGBITS && s->sh_type != SHT_INIT_ARRAY
        && s->sh_type != SHT_FINI_ARRAY)
        return PE_CLS_OTHER;

    if (flags & SHF_EXECINSTR)
        return PE_CLS_TEXT;

    if (flags & SHF_WRITE)
        return PE_CLS_DATA;

    // Two read-only sections get a directory of their own, so they cannot be
    // folded in with the rest of the read-only data.
    if (0 == strcmp(name, ".rsrc"))
        return PE_CLS_RSRC;

    if (0 == strcmp(name, ".pdata"))
        return PE_CLS_PDATA;

    return PE_CLS_RDATA;
}

/// One input section waiting to be placed, kept apart from the section itself
/// so the order can be sorted without disturbing s1->sections.
struct pe_order {
    i32 cls;             /// Its PE_CLS_*, which is the sort key.
    i32 index;           /// Its number in s1->sections.
    i32 group;           /// The section its group sorts at: itself, unless grouped.
    const char *suffix;  /// From the dollar on in a grouped name, else "".
};

/// Order sections by class, then keep each group where its first section was
/// made, sorted by the part of the name after the dollar. Everything else stays
/// in the order it was created, which is what puts .text first inside the code.
static i32 pe_order_cmp(const void *a, const void *b, void *arg)
{
    const struct pe_order *x = a, *y = b;

    (void)arg;
    if (x->cls != y->cls)
        return x->cls - y->cls;

    if (x->group != y->group)
        return x->group - y->group;

    return strcmp(x->suffix, y->suffix);
}

/// Fill in the group and suffix of every section, for pe_order_cmp(). A group
/// is the sections sharing the name before the dollar, ".CRT" for ".CRT$XCA",
/// and sorts where the section of that bare name sits or, failing one, where
/// its first member does. Only grouped sections are looked at here, which in
/// a link of many distinct sections is a handful.
static void pe_group_sections(tli_State *s1, struct pe_order *order, i32 n)
{
    char (*bases)[32] = NULL;
    i32 *firsts = NULL, nbases = 0, i, j;

    for (i = 0; i < n; i++) {
        const char *name = s1->sections[order[i].index]->name;
        const char *dollar = strchr(name, '$');
        tli_Section *bare;
        char base[32];

        order[i].group = order[i].index;
        order[i].suffix = dollar ? dollar : "";
        if (!dollar)
            continue;

        pstrcpy(base, sizeof base, name);
        base[dollar - name < 31 ? dollar - name : 31] = '\0';

        bare = tli_find_section_by_name(s1, base);
        if (bare) {
            order[i].group = bare->sh_num;
            continue;
        }

        for (j = 0; j < nbases && strcmp(bases[j], base); j++)
            ;

        if (j == nbases) {
            bases = tli_realloc(bases, sizeof *bases * (nbases + 1));
            firsts = tli_realloc(firsts, sizeof *firsts * (nbases + 1));
            pstrcpy(bases[j], sizeof bases[j], base);
            firsts[j] = order[i].index;
            nbases++;
        }

        order[i].group = firsts[j];
    }

    // A group is one table, such as the initialisers between .CRT$XCA and
    // .CRT$XCZ, so it lands in one output section whatever each member's
    // permissions say. order[] is still in section order here.
    for (i = 0; i < n; i++)
        if (order[i].suffix[0])
            order[i].cls = order[order[i].group - 1].cls;

    tli_free(bases);
    tli_free(firsts);
}

/// How many output sections the merging below will produce.  The headers sit in
/// front of the first one and there is a header per output section, so the
/// count has to be known before any address can be handed out.  The two
/// sections the tables are built into count whether or not they hold anything
/// yet, which at worst overstates the header by eighty bytes.
static i32 pe_count_sections(struct pe_state *pe, const struct pe_order *order,
                             i32 n)
{
    tli_State *s1 = pe->s1;
    i32 i, count = 0, cur = -1;

    for (i = 0; i < n; i++) {
        tli_Section *s = s1->sections[order[i].index];
        i32 group = order[i].cls == PE_CLS_BSS ? PE_CLS_DATA : order[i].cls;

        if (group >= PE_CLS_DROP)
            break;

        // Anything but a run of one class continuing closes the open output
        // section; debug sections never merge with each other.
        if (!(cur == group && group != PE_CLS_DEBUG))
            cur = -1;

        if (0 == s->data_offset && s != pe->tables && s != data_section)
            continue;

        if (cur == -1)
            count++, cur = group;
    }

    return count;
}

/// The characteristics an output section inherits from the input section that
/// opens it.
static u32 pe_section_flags(tli_Section *s)
{
    u32 f = PE_SCN_MEM_READ;

    if (s->sh_flags & SHF_EXECINSTR)
        f |= PE_SCN_MEM_EXECUTE | PE_SCN_CNT_CODE;
    else if (s->sh_type == SHT_NOBITS && !(s->sh_flags & SHF_TLS))
        f |= PE_SCN_CNT_UNINITIALIZED;
    else
        f |= PE_SCN_CNT_INITIALIZED;

    if (s->sh_flags & SHF_WRITE)
        f |= PE_SCN_MEM_WRITE;

    // Anything not loaded is there for a debugger; say so, so that a tool that
    // strips debug information knows what to drop.
    if (!(s->sh_flags & SHF_ALLOC))
        f |= PE_SCN_MEM_DISCARDABLE;

    return f;
}

/// Give every section an address, grouping them into as few output sections as
/// the permissions allow, and build the tables that need to know where they
/// ended up while their home section is being sized.
static void pe_assign_addresses(struct pe_state *pe)
{
    tli_State *s1 = pe->s1;
    struct pe_order *order;
    struct pe_sect *si = NULL;
    addr_t addr;
    u32 step;
    i32 i, nbs;

    // An image that says it can be moved has to say where its absolute
    // addresses are, and a DLL always has to be movable.
    if (PE_DLL == pe->type
        || (s1->pe_dll_characteristics & PE_DLL_DYNAMIC_BASE))
        pe->reloc = new_section(s1, ".reloc", SHT_PROGBITS, 0);

    // Classify everything once, which is also where the presence of any TLS
    // section is noticed.
    nbs = s1->nb_sections;
    pe->sect_of = tli_mallocz(sizeof *pe->sect_of * nbs);
    pe->nsect_of = nbs;
    order = tli_malloc(sizeof *order * nbs);
    for (i = 1; i < nbs; i++) {
        order[i - 1].index = i;
        order[i - 1].cls = pe_section_class(s1->sections[i]);

        if (order[i - 1].cls == PE_CLS_TLS)
            pe->tls_size = PE_TLS_DIR_SIZE;
    }

    // Sections of one class keep the order they were made in, groups apart:
    // that is what puts .text first inside the code and .rdata - where the
    // tables below go - first inside the read-only data.
    pe_group_sections(s1, order, nbs - 1);
    tl_sort(order, nbs - 1, sizeof *order, pe_order_cmp, NULL);

    // Where the first section can start.  Aligning to whichever of the two
    // alignments is coarser keeps the headers, which are rounded up to the
    // file alignment, from running into it: a subsystem that maps the image as
    // it lies sets the section alignment as low as 32 bytes.
    step = pe->section_align > pe->file_align ? pe->section_align
                                              : pe->file_align;
    addr = pe_align_up(pe->imagebase + PE_HDR_OFFSET + 4
                       + sizeof(IMAGE_FILE_HEADER)
                       + sizeof(struct pe_opt_header)
                       + (u32)pe_count_sections(pe, order, nbs - 1)
                         * sizeof(IMAGE_SECTION_HEADER), step);

    for (i = 0; i < nbs - 1; i++) {
        tli_Section *s = s1->sections[order[i].index];
        i32 cls = order[i].cls, group;

        if (cls >= PE_CLS_DROP)
            break;

        // A zero-filled tail belongs to the writable data it follows: the
        // loader gets it for free by mapping more address space than there are
        // bytes in the file.
        group = cls == PE_CLS_BSS ? PE_CLS_DATA : cls;

        // Carrying on inside the open output section costs only the input
        // section's own alignment; starting a new one costs a whole page.
        if (si && group == si->cls && group != PE_CLS_DEBUG) {
            i32 a = s->sh_addralign < 16 ? 16 : s->sh_addralign;

            // A group member is one piece of a table, an import descriptor
            // or one slot of the address table, and padding between pieces
            // would read as entries. Its own alignment is all it gets.
            if (order[i].suffix[0])
                a = s->sh_addralign;

            s->sh_addr = addr = pe_align_up(addr, a);
        } else {
            si = NULL;
            s->sh_addr = addr = pe_align_up(addr, pe->section_align);
        }

        // The linker's own tables live at the front of the read-only data, so
        // they have to be built the moment that section has an address and
        // before anything is measured.
        if (s == pe->tables) {
            pe_build_imports(pe);
            pe_build_exports(pe);

            if (pe->tls_size)
                pe_reserve_tls(pe);
        }

        if (s == pe->reloc)
            pe_build_relocs(pe);

        if (0 == s->data_offset)
            continue;

        if (!si) {
            si = pe_push(pe->sects, pe->nsects, pe->asects);
            pstrcpy(si->name, sizeof si->name, s->name);
            si->name[strcspn(si->name, "$")] = '\0';
            si->cls = group;
            si->addr = addr;
            si->flags = pe_section_flags(s);
        }

        // Widen the output section over what was just placed.  Only a section
        // with contents adds to what has to be written.
        pe->sect_of[s->sh_num] = pe->nsects;
        addr += s->data_offset;
        si->vsize = (u32)(addr - si->addr);
        if (s->sh_type != SHT_NOBITS) {
            dynarray_add(&si->parts, &si->nparts, s);
            si->rawsize = si->vsize;
        }

        // The loader wants one section it can copy per thread, under a name
        // the debugger recognises.
        if (s->sh_flags & SHF_TLS) {
            pstrcpy(si->name, sizeof si->name, ".tls");
            pe_extend_tls(pe, s);
        }

        // A C runtime's own TLS directory spans the .tls group the COFF
        // objects gathered, whichever output section it landed in, and a
        // thread-local's offset is measured from the group's first member.
        // The architecture backends measure from tls_end, as
        // pe_extend_tls() arranges for the linker's own directory.
        else if (!pe->tls_size && !s1->tls_start
                 && 0 == strncmp(s->name, ".tls", 4)
                 && (s->name[4] == '\0' || s->name[4] == '$')) {
            s1->tls_start = s1->tls_end = s->sh_addr;
        }
    }

    tli_free(order);
}

// -------------------------------------------------------------------------
// resolving what the objects left undefined

/// Is the rest of this string a decimal number, and not empty?
static i32 pe_all_digits(const char *p)
{
    if (!*p)
        return 0;

    while (*p >= '0' && *p <= '9')
        p++;

    return *p == '\0';
}

/// How long a name the import matcher will consider, which is generous enough
/// that nothing real is truncated.
#define PE_NAME_MAX 512

/// How many spellings of one name it tries.
#define PE_NAME_TRIES 2

/// Fill `out` with the spellings under which an undefined symbol might turn up
/// in the import table, likeliest first, and return how many there are.  The
/// name an object uses is not always the name the DLL exports:
///
///     foo         the plain name
///     _foo        an object that did not decorate a name the DLL did
///     _foo@12     stdcall decoration, which no export table carries
///     __imp_foo   a reference to the import slot rather than to what it holds,
///     _imp__foo   which is what __declspec(dllimport) means, in mingw's two
///                 spellings of it
static i32 pe_import_names(tli_State *s1, ElfW(Sym) *sym,
                           char out[PE_NAME_TRIES][PE_NAME_MAX], i32 *via_slot)
{
    const char *name = pe_export_name(s1, sym);
    const char *at;
    i32 n = 0;

    pstrcpy(out[n++], PE_NAME_MAX, name);

    // Either mingw prefix says the reference is to the slot, and nothing else
    // about the name has been changed.
    if (0 == strncmp(name, "_imp__", 6) || 0 == strncmp(name, "__imp_", 6)) {
        *via_slot = 1;
        pstrcpy(out[n++], PE_NAME_MAX, name + 6);
        return n;
    }

    // Otherwise the second try either strips stdcall decoration or adds the
    // underscore a 32-bit compiler would have.
    at = strrchr(name, '@');
    if (at && at > name && pe_all_digits(at + 1)) {
        i32 lead = name[0] == '_';
        usize len = at - name - lead;

        if (len > 0 && len < PE_NAME_MAX) {
            memcpy(out[n], name + lead, len);
            out[n++][len] = '\0';
        }
    } else if (name[0] != '_') {
        snprintf(out[n++], PE_NAME_MAX, "_%s", name);
    }

    return n;
}

/// Match every undefined symbol against the import table, and give each one
/// that matches somewhere to point.  Anything still undefined afterwards is
/// left alone, so that relocate_syms() reports it the usual way.
static i32 pe_bind_imports(struct pe_state *pe)
{
    tli_State *s1 = pe->s1;
    i32 i, last, ret = 0;

    // The thunks go on the end of .text and are pointer-sized-aligned.
    pe_pad_section(text_section, 8);

    last = symtab_section->data_offset / sizeof(ElfW(Sym));
    for (i = 1; i < last; i++) {
        ElfW(Sym) *sym = (ElfW(Sym) *)symtab_section->data + i;
        char names[PE_NAME_TRIES][PE_NAME_MAX];
        struct pe_import *im;
        const char *name;
        u32 type;
        i32 via_slot, n, k, dyn = 0;

        if (sym->st_shndx != SHN_UNDEF) {

            // -rdynamic on an image means the same as on a shared object:
            // everything that is not local goes into the export table.
            if (s1->rdynamic && ELFW(ST_BIND)(sym->st_info) != STB_LOCAL)
                sym->st_other |= ST_PE_EXPORT;

            continue;
        }

        // Try each spelling in turn against what the DLLs said they export.
        name = (char *)symtab_section->link->data + sym->st_name;
        via_slot = 0;
        n = pe_import_names(s1, sym, names, &via_slot);
        for (k = 0; k < n && !dyn; k++)
            dyn = find_elf_sym(s1->dynsymtab_section, names[k]);

        if (!dyn)
            continue;

        im = pe_import_for(pe, dyn);
        type = ELFW(ST_TYPE)(sym->st_info);

        // Assembler-written symbols often have no type at all; treat those as
        // functions unless the name says the reference is to the slot.
        if (type == STT_FUNC || (type == STT_NOTYPE && !via_slot)) {
            if (!im->thunk_off) {
                char helper[PE_NAME_MAX + 8];
                i32 slot;

                // The thunk needs a symbol for the slot before the slot has an
                // address, so make one and note that it must be filled in.
                snprintf(helper, sizeof helper, "IAT.%s", name);
                slot = put_elf_sym(symtab_section, 0, sizeof(u32),
                                   ELFW(ST_INFO)(STB_LOCAL, STT_OBJECT), 0,
                                   SHN_UNDEF, helper);
                pe_patch_later(im, slot);
                im->thunk_off = pe_emit_thunk(s1, slot);

                // put_elf_sym() may have moved the symbol table.
                sym = (ElfW(Sym) *)symtab_section->data + i;
            }

            sym->st_value = im->thunk_off;
            sym->st_shndx = text_section->sh_num;
            sym->st_other &= ~ST_PE_EXPORT; // a thunk is nobody's export
        } else {
            if (!via_slot)
                ret = tli_error_noabort(
                    "symbol '%s' is missing __declspec(dllimport)", name);

            // The symbol becomes the slot; there is no thunk to go through.
            pe_patch_later(im, i);
        }
    }

    return ret;
}

// -------------------------------------------------------------------------
// the COFF symbol table
//
// PE has no place for an ELF symbol table, and the DWARF sections carried over
// from the input do not on their own tell a debugger where a function is.  The
// old COFF symbol table does, and gdb still reads it, so the global symbols are
// written out in that form: an 18-byte record each, with names over eight
// characters spilled into a string table whose first word is its own length.

/// Write the global symbols into the .coffsym and .coffstr sections reserved
/// for them, and close the string table with its own length.
static void pe_build_coff_symbols(struct pe_state *pe)
{
    tli_State *s1 = pe->s1;
    ElfW(Sym) *esym = (ElfW(Sym) *)s1->symtab->data;
    i32 i, n = s1->symtab->data_offset / sizeof *esym;

    for (i = 1; i < n; i++) {
        struct pe_coffsym *se;
        const char *name;
        addr_t value;
        i32 shnum, len;

        if (ELFW(ST_BIND)(esym[i].st_info) != STB_GLOBAL)
            continue;

        // Translate the ELF section number into what a COFF reader expects,
        // which is a 1-based output section or one of the negative specials.
        name = (char *)s1->symtab->link->data + esym[i].st_name;
        len = strlen(name);
        value = esym[i].st_value;
        shnum = esym[i].st_shndx;
        if (shnum == SHN_ABS) {
            shnum = -1;
        } else if (shnum > 0 && shnum < pe->nsect_of && pe->sect_of[shnum]) {

            // The value is an offset into the output section, which is rarely
            // the input section the symbol was defined in.
            shnum = pe->sect_of[shnum];
            value -= pe->sects[shnum - 1].addr;
        } else {
            shnum = 0;   // undefined, as far as a debugger is concerned
        }

        // Eight characters or fewer go in the record; COFF spells a longer
        // name as four zero bytes followed by a string table offset.
        se = section_ptr_add(pe->coffsym, sizeof *se);
        se->n_value = (i32)value;
        se->n_scnum = (i16)shnum;
        se->n_sclass = PE_COFF_C_EXT;
        if (len <= 8)
            memcpy(se->n_name, name, len);
        else
            se->n_offset = put_elf_str(pe->coffstr, name);
    }

    write32le(pe->coffstr->data, pe->coffstr->data_offset);
}

// -------------------------------------------------------------------------
// writing the file

/// Point one data directory at a table, which is always an RVA and a size.
static void pe_set_dir(struct pe_opt_header *oh, i32 dir,
                       u32 addr, u32 size)
{
    oh->DataDirectory[dir].VirtualAddress = addr;
    oh->DataDirectory[dir].Size = size;
}

/// Fill the headers in from the layout, then write the whole image out.
static i32 pe_write(struct pe_state *pe)
{
    tli_State *s1 = pe->s1;
    struct pe_out out;
    IMAGE_FILE_HEADER fh;
    struct pe_opt_header oh;
    IMAGE_SECTION_HEADER *shdrs;
    u32 sig = IMAGE_NT_SIGNATURE, file_off;
    i32 i, j, ret = 0;

    memset(&fh, 0, sizeof fh);
    memset(&oh, 0, sizeof oh);

    // The COFF header, which says what the image is rather than where anything
    // in it lives.
    fh.Machine = IMAGE_FILE_MACHINE;
    fh.NumberOfSections = pe->nsects;
    fh.SizeOfOptionalHeader = sizeof oh;
    fh.Characteristics = PE_CHARACTERISTICS | s1->pe_characteristics;
    if (PE_DLL == pe->type)
        fh.Characteristics |= PE_FILE_DLL;

    // Whether the image can be moved is not a choice once .reloc exists.
    if (pe->reloc)
        fh.Characteristics &= ~PE_FILE_RELOCS_STRIPPED;

    // The optional header.  SectionAlignment and FileAlignment are separate
    // fields in the format: one is what the loader maps to, the other what the
    // file is padded to, and they are only equal for a native-subsystem image.
    oh.Magic = PE_OPT_MAGIC;
    oh.MajorLinkerVersion = 6;
    oh.AddressOfEntryPoint = pe->entry_rva;
    oh.ImageBase = (pe_addr)pe->imagebase;
    oh.SectionAlignment = pe->section_align;
    oh.FileAlignment = pe->file_align;
    oh.MajorOperatingSystemVersion = PE_OS_VERSION >> 8;
    oh.MinorOperatingSystemVersion = PE_OS_VERSION & 0xFF;
    oh.MajorSubsystemVersion = PE_OS_VERSION >> 8;
    oh.MinorSubsystemVersion = PE_OS_VERSION & 0xFF;
    oh.Subsystem = pe->subsystem;
    oh.DllCharacteristics = s1->pe_dll_characteristics;
    oh.SizeOfStackReserve = s1->pe_stack_size ? s1->pe_stack_size : 0x100000;
    oh.SizeOfStackCommit = 0x1000;
    oh.SizeOfHeapReserve = 0x100000;
    oh.SizeOfHeapCommit = 0x1000;
    oh.NumberOfRvaAndSizes = PE_NUM_DIRS;

    // The directories for the tables built into .rdata.  The other three are
    // filled in from the section walk below, which is where they get an RVA.
    if (pe->import_size) {
        pe_set_dir(&oh, PE_DIR_IMPORT, pe->import_rva, pe->import_size);
        pe_set_dir(&oh, PE_DIR_IAT, pe->iat_rva, pe->iat_size);
    }

    if (pe->export_size)
        pe_set_dir(&oh, PE_DIR_EXPORT, pe->export_rva, pe->export_size);

    if (pe->tls_size)
        pe_set_dir(&oh, PE_DIR_TLS,
                   pe->tls_dir + pe_rva(pe, pe->tables->sh_addr), pe->tls_size);
    else if (pe->tls_used_rva)
        pe_set_dir(&oh, PE_DIR_TLS, pe->tls_used_rva, PE_TLS_DIR_SIZE);

    // The headers are rounded up to the file alignment, and the first section
    // was placed on the assumption that they fit underneath it.
    oh.SizeOfHeaders = (u32)pe_align_up(PE_HDR_OFFSET + sizeof sig + sizeof fh
                                        + sizeof oh
                                        + pe->nsects * sizeof *shdrs,
                                        pe->file_align);
    if (pe->nsects && oh.SizeOfHeaders > pe_rva(pe, pe->sects[0].addr))
        return tli_error_noabort("the PE headers do not fit in %#x bytes; "
                                 "there are too many output sections",
                                 (u32)pe->section_align);

    shdrs = tli_mallocz(sizeof *shdrs * (pe->nsects ? pe->nsects : 1));
    file_off = oh.SizeOfHeaders;

    if (2 == s1->verbose)
        printf("   virt   file   size  section\n");

    for (i = 0; i < pe->nsects; i++) {
        struct pe_sect *si = &pe->sects[i];
        IMAGE_SECTION_HEADER *sh = &shdrs[i];
        u32 rva = pe_rva(pe, si->addr);
        usize len = strlen(si->name);

        if (2 == s1->verbose)
            printf("%7x %6x %6x  %s\n", rva, file_off, si->vsize, si->name);

        // Eight bytes of name, no terminator if it fills them.  A longer name
        // can only be kept by spilling it into the COFF string table, which
        // only exists when there is debug information to go with it.
        if (len > sizeof sh->Name && pe->coffstr)
            snprintf((char *)sh->Name, sizeof sh->Name, "/%d",
                     put_elf_str(pe->coffstr, si->name));
        else
            memcpy(sh->Name, si->name,
                   len < sizeof sh->Name ? len : sizeof sh->Name);

        sh->Misc.VirtualSize = si->vsize;
        sh->VirtualAddress = rva;
        sh->Characteristics = si->flags;

        // A section with contents claims the next run of the file, padded to
        // the file alignment so the loader can map it directly.
        if (si->rawsize) {
            sh->PointerToRawData = file_off;
            file_off = (u32)pe_align_up(file_off + si->rawsize, pe->file_align);
            sh->SizeOfRawData = file_off - sh->PointerToRawData;
            if (si->flags & PE_SCN_CNT_CODE)
                oh.SizeOfCode += sh->SizeOfRawData;
            else
                oh.SizeOfInitializedData += sh->SizeOfRawData;
        }

        si->file_off = sh->PointerToRawData;

        if (si->cls == PE_CLS_TEXT && !oh.BaseOfCode)
            oh.BaseOfCode = rva;
#if PTR_SIZE == 4
        if (si->cls == PE_CLS_DATA && !oh.BaseOfData)
            oh.BaseOfData = rva;
#endif

        // Three classes are nothing but a table the loader looks up through a
        // directory, so the section is the directory.
        if (si->cls == PE_CLS_RELOC)
            pe_set_dir(&oh, PE_DIR_BASERELOC, rva, si->vsize);
        else if (si->cls == PE_CLS_RSRC)
            pe_set_dir(&oh, PE_DIR_RESOURCE, rva, si->vsize);
        else if (si->cls == PE_CLS_PDATA)
            pe_set_dir(&oh, PE_DIR_EXCEPTION, rva, si->vsize);

        if (rva + si->vsize > oh.SizeOfImage)
            oh.SizeOfImage = rva + si->vsize;
    }

    oh.SizeOfImage = (u32)pe_align_up(oh.SizeOfImage, pe->section_align);

    if (pe->coffsym) {
        fh.PointerToSymbolTable = file_off;
        fh.NumberOfSymbols = pe->coffsym->data_offset / sizeof(struct pe_coffsym);
    }

    memset(&out, 0, sizeof out);
    out.fp = fopen(pe->filename, "wb");
    if (!out.fp) {
        tli_free(shdrs);
        return tli_error_noabort("could not write '%s': %s",
                                 pe->filename, strerror(errno));
    }

    // The headers, in the one order the format allows.
    ret |= pe_emit(&out, pe_dos_header, sizeof pe_dos_header);
    ret |= pe_emit(&out, &sig, sizeof sig);
    ret |= pe_emit(&out, &fh, sizeof fh);
    ret |= pe_emit(&out, &oh, sizeof oh);
    ret |= pe_emit(&out, shdrs, pe->nsects * sizeof *shdrs);

    for (i = 0; i < pe->nsects; i++) {
        struct pe_sect *si = &pe->sects[i];

        if (!si->rawsize)
            continue;

        for (j = 0; j < si->nparts; j++) {
            tli_Section *s = si->parts[j];

            // The gap a merged section leaves between its parts in memory is
            // left in the file too, so one read maps the whole run.
            pe_emit_pad(&out, si->file_off + (u32)(s->sh_addr - si->addr));
            ret |= pe_emit(&out, s->data, s->data_offset);
        }

        pe_emit_pad(&out, si->file_off + shdrs[i].SizeOfRawData);
    }

    if (pe->coffsym) {
        pe_emit_pad(&out, fh.PointerToSymbolTable);
        ret |= pe_emit(&out, pe->coffsym->data, pe->coffsym->data_offset);
        ret |= pe_emit(&out, pe->coffstr->data, pe->coffstr->data_offset);
    }

    // The checksum covers the file's own length, and the CheckSum field is the
    // one place it can go, so it is patched in once everything else is out.
    file_off = out.pos;
    if (out.odd)
        pe_sum(&out, (const u8 *)"", 1);

    out.sum += file_off;
    fseek(out.fp, PE_HDR_OFFSET + sizeof sig + sizeof fh
                  + offsetof(struct pe_opt_header, CheckSum), SEEK_SET);
    fwrite(&out.sum, sizeof out.sum, 1, out.fp);
    if (fclose(out.fp))
        ret = -1;

    tli_free(shdrs);

#ifndef _WIN32
    // The host is not Windows, but the file is still a program.
    chmod(pe->filename, 0777);
#endif
    if (ret)
        return tli_error_noabort("could not write '%s': %s",
                                 pe->filename, strerror(errno));

    if (s1->verbose)
        printf("<- %s (%u bytes)\n", pe->filename, (u32)file_off);

    return 0;
}

// -------------------------------------------------------------------------
// reading: a DLL's exports, a .def file, a compiled resource

/// Read a fixed-size record from a file offset, reporting only whether all of
/// it arrived; a short or failed read is a malformed file either way.
static i32 pe_read_at(i32 fd, u64 offset, void *buf, u32 len)
{
    if (lseek(fd, offset, SEEK_SET) < 0)
        return 0;

    return full_read(fd, buf, len) == (ssize_t)len;
}

/// An RVA in an image points into whichever section covers it; the file offset
/// is that section's, shifted by the difference between the two.
struct pe_map {
    u32 rva, size, file_off;
};

/// Does an RVA, and the bytes after it, fall inside the loaded section?
static i32 pe_in_section(const struct pe_map *m, u32 at, u32 len)
{
    return at >= m->rva && (u64)at + len <= (u64)m->rva + m->size;
}

/// The names a DLL exports, gathered into one buffer of NUL-terminated strings
/// with an empty one on the end.  This is the whole of what a link needs from a
/// DLL: PE has no notion of a symbol's type or size on this side of the
/// boundary, only its name and where in the image it lives.
static i32 pe_read_exports(i32 fd, char **pnames)
{
    u8 hdr[24], dir[40];
    u8 *sec = NULL, *names = NULL;
    struct pe_map map;
    tli_CString cs;
    u64 pe_off, sec_off;
    u32 export_rva, ndirs, nnames, names_rva, i;
    i32 nsects, opt_size, wide, ret = -1;

    *pnames = NULL;
    cstr_new(&cs);

    // "MZ", then the offset of the PE header at 0x3C.
    if (!pe_read_at(fd, 0, hdr, 4) || hdr[0] != 'M' || hdr[1] != 'Z')
        goto done;

    if (!pe_read_at(fd, 0x3C, hdr, 4))
        goto done;

    pe_off = read32le(hdr);
    if (!pe_read_at(fd, pe_off, hdr, 24) || read32le(hdr) != IMAGE_NT_SIGNATURE)
        goto done;

    nsects = read16le(hdr + 6);
    opt_size = read16le(hdr + 20);
    if (nsects <= 0 || nsects > 0x1000 || opt_size < 24)
        goto done;

    // The optional header's magic decides how wide the fields between
    // ImageBase and the data directories are, which is the only thing that
    // moves the directories about.
    if (!pe_read_at(fd, pe_off + 24, dir, 2))
        goto done;

    wide = read16le(dir) == 0x020B;
    i = wide ? 108 : 92;  // offset of NumberOfRvaAndSizes
    if ((i32)i + 4 > opt_size)
        goto done;

    if (!pe_read_at(fd, pe_off + 24 + i, dir, 4))
        goto done;

    ndirs = read32le(dir);
    if (ndirs <= PE_DIR_EXPORT)
        goto done;

    if (!pe_read_at(fd, pe_off + 24 + i + 4, dir, 8))
        goto done;

    export_rva = read32le(dir);
    if (!export_rva) {
        ret = 0;   // a DLL is allowed to export nothing
        goto done;
    }

    // Find the section the export directory falls in and read it whole: the
    // directory, the name pointers and the names themselves are all inside it,
    // and one read beats a seek per name.
    sec_off = pe_off + 24 + opt_size;
    memset(&map, 0, sizeof map);
    for (i = 0; (i32)i < nsects; i++) {
        u8 sh[40];
        u32 rva, size;

        if (!pe_read_at(fd, sec_off + i * sizeof sh, sh, sizeof sh))
            goto done;

        rva = read32le(sh + 12);
        size = read32le(sh + 16);   // SizeOfRawData
        if (export_rva >= rva && export_rva < rva + size) {
            map.rva = rva;
            map.size = size;
            map.file_off = read32le(sh + 20);
            break;
        }
    }

    if (!map.size) {
        ret = 0;   // the directory is not in any section: nothing to read
        goto done;
    }

    if (map.size > 0x08000000)
        goto done;

    sec = tli_mallocz(map.size);
    if (!pe_read_at(fd, map.file_off, sec, map.size))
        goto done;

    // The directory itself, from which only the name count and the name
    // pointer array are wanted.
    if (!pe_in_section(&map, export_rva, sizeof dir))
        goto done;

    memcpy(dir, sec + (export_rva - map.rva), sizeof dir);
    nnames = read32le(dir + 24);
    names_rva = read32le(dir + 32);
    if (nnames == 0) {
        ret = 0;
        goto done;
    }

    if (nnames > map.size / 4 || !pe_in_section(&map, names_rva, nnames * 4))
        goto done;

    names = sec + (names_rva - map.rva);

    // Every pointer has to land inside the section that was read, and every
    // name has to end inside it, or the file is not one to trust.
    for (i = 0; i < nnames; i++) {
        u32 at = read32le(names + i * 4);
        const char *s;

        if (!pe_in_section(&map, at, 1))
            goto done;

        s = (const char *)sec + (at - map.rva);
        if (!memchr(s, '\0', map.size - (at - map.rva)))
            goto done;

        cstr_cat(&cs, s, 0);
    }

    cstr_ccat(&cs, '\0');   // the empty string that ends the list
    *pnames = cs.data;
    cs.data = NULL;
    ret = 0;

done:
    tli_free(sec);
    cstr_free(&cs);
    return ret;
}

/// Take a DLL as input: record it, and enter everything it exports into the
/// dynamic symbol table so undefined symbols can bind to it.
static i32 pe_load_dll(tli_State *s1, i32 fd, const char *filename)
{
    tli_DLLReference *ref = tli_add_dllref(s1, filename, 0);
    char *names, *p;

    if (ref->found)
        return 0;

    if (pe_read_exports(fd, &names))
        return -1;

    if (names) {
        for (p = names; *p; p += strlen(p) + 1)
            pe_putimport(s1, ref->index, p, 0);

        tli_free(names);
    }

    return 0;
}

// -------------------------------------------------------------------------

/// One token of a .def file.  Words are separated by blanks, may be quoted so
/// that LIBRARY "some name.dll" works, and a line ends at the newline; `*next`
/// is the first character after the token so the caller can tell which.
static char *pe_def_token(char **line, char *next)
{
    char *p = *line, *e;
    i32 quote;

    while ((u8)*p <= ' ' && *p && *p != '\n')
        p++;

    // Inside quotes a blank is part of the token; outside them anything below
    // a space ends it, newline included.
    quote = *p == '"' ? *p++ : ' ';
    for (e = p; (u8)*e >= ' ' && *e != quote; e++)
        ;

    if (*e == '"')
        *e++ = '\0';

    // Leave the caller looking at the next token, and tell it what separated
    // the two, before the terminator goes in.
    *line = e;
    while ((u8)**line <= ' ' && **line && **line != '\n')
        (*line)++;

    *next = **line;
    *e = '\0';
    return p;
}

/// A module definition file: the name of a DLL and the names it exports, which
/// is what MSVC's tools produce to import from a DLL without the DLL.
static i32 pe_load_def(tli_State *s1, i32 fd)
{
    char dllname[256], next;
    char *buf, *line, *word;
    i32 state = 0, dll = 0, ret = -1;

    buf = tli_load_text(fd);
    if (!buf)
        return -1;

    // LIBRARY, then EXPORTS, then a name per line; anything out of order is a
    // file this reader will not pretend to understand.
    for (line = buf;; line++) {
        word = pe_def_token(&line, &next);
        if (*word && *word != ';') {
            if (state == 0) {
                if (0 != strcasecmp(word, "LIBRARY") || next == '\n')
                    goto done;

                pstrcpy(dllname, sizeof dllname, pe_def_token(&line, &next));
                if (!*tli_fileextension(dllname))
                    pstrcat(dllname, sizeof dllname, ".dll");

                state = 1;
            } else if (state == 1) {
                if (0 != strcasecmp(word, "EXPORTS"))
                    goto done;

                dll = tli_add_dllref(s1, dllname, 0)->index;
                state = 2;
            } else {

                // "name @12" pins the symbol to an ordinal, which the import
                // table can then use instead of the name.
                i32 ordinal = 0;

                if (next == '@') {
                    char *p = pe_def_token(&line, &next);
                    ordinal = (i32)strtol(p + 1, NULL, 10);
                }

                pe_putimport(s1, dll, word, ordinal);
            }
        }

        // Skip anything else on this line, then stop at the end of the text.
        while ((u8)next > ' ')
            pe_def_token(&line, &next);

        if (next != '\n')
            break;
    }

    ret = 0;
done:
    tli_free(buf);
    return ret;
}

// -------------------------------------------------------------------------

/// A compiled resource, which windres -O coff writes as a COFF object holding
/// one .rsrc section whose relocations are all image-relative.
static i32 pe_load_res(tli_State *s1, i32 fd)
{
    struct pe_rsrc_header hdr;
    tli_Section *rsrc;
    u8 *p;
    u64 off;
    i32 i, sym;

    // One section, named .rsrc, for this architecture: anything else is some
    // other kind of COFF file and not this function's business.
    if (!pe_read_at(fd, 0, &hdr, sizeof hdr))
        return -1;

    if (hdr.filehdr.Machine != IMAGE_FILE_MACHINE
        || hdr.filehdr.NumberOfSections != 1
        || 0 != memcmp(hdr.sectionhdr.Name, ".rsrc\0\0\0", 8))
        return -1;

    rsrc = new_section(s1, ".rsrc", SHT_PROGBITS, SHF_ALLOC);
    p = section_ptr_add(rsrc, hdr.sectionhdr.SizeOfRawData);
    if (!pe_read_at(fd, hdr.sectionhdr.PointerToRawData, p,
                    hdr.sectionhdr.SizeOfRawData))
        return -1;

    // The relocations become ordinary R_PE_RVA ones against the section
    // itself, which is all the writer needs to place the directory anywhere.
    sym = put_elf_sym(symtab_section, 0, 0, 0, 0, rsrc->sh_num, ".rsrc");
    off = hdr.sectionhdr.PointerToRelocations;
    for (i = 0; i < hdr.sectionhdr.NumberOfRelocations; i++) {
        u8 rel[10];

        if (!pe_read_at(fd, off, rel, sizeof rel))
            return -1;

        // Every entry inside a resource directory is an offset from the start
        // of the image, so only one relocation type can appear.
        if (read16le(rel + 8) != PE_RSRC_RELTYPE)
            return -1;

        put_elf_reloc(symtab_section, rsrc, read32le(rel), R_PE_RVA, sym);
        off += sizeof rel;
    }

    return 0;
}

/// Take one PE-side input file, whichever of the three kinds it turns out to
/// be.  Only the `.def` case can be told from the name.
ST_FUNC i32 pe_load_file(struct tli_State *s1, i32 fd, const char *filename)
{
    char magic[4];

    if (0 == strcmp(tli_fileextension(filename), ".def"))
        return pe_load_def(s1, fd);

    if (pe_load_res(s1, fd) == 0)
        return 0;

    if (pe_read_at(fd, 0, magic, 2) && 0 == memcmp(magic, "MZ", 2))
        return pe_load_dll(s1, fd, filename);

    return -1;
}

/// Record a name a DLL exports.  The dll index is parked in st_size and the
/// ordinal, if the .def file gave one, in st_value: this table is never written
/// out, so the fields are free for the writer's own use.
ST_FUNC i32 pe_putimport(tli_State *s1, i32 dllindex, const char *name,
                         addr_t value)
{
    return set_elf_sym(s1->dynsymtab_section, value, dllindex,
                       ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
                       value ? SHN_ABS : SHN_UNDEF, name);
}

// -------------------------------------------------------------------------
// deciding what to produce

/// Spell a runtime symbol the way the target's ABI does: on 32-bit Windows a C
/// name gets a leading underscore and a stdcall one gets an argument-byte
/// suffix as well; the 64-bit ABIs dropped both.
#if PTR_SIZE == 8
# define PE_STDSYM(n, s) n
#else
# define PE_STDSYM(n, s) "_" n s
#endif

/// The C runtime's entry point standing in for one of tinylinker's own, spelled
/// the way pe_add_runtime() spells those: one underscore more than the C name.
static const char *pe_crt_start(const char *start_symbol)
{
    if (0 == strcmp(start_symbol, "__wstart"))
        return "_wmainCRTStartup";

    if (0 == strcmp(start_symbol, "__winstart"))
        return "_WinMainCRTStartup";

    if (0 == strcmp(start_symbol, "__wwinstart"))
        return "_wWinMainCRTStartup";

    if (0 == strcmp(start_symbol, "__start"))
        return "_mainCRTStartup";

    return PE_STDSYM("__DllMainCRTStartup", "@12");
}

/// Settle the image kind and its entry point from what the objects define, and
/// queue the runtime and the Windows libraries a program is assumed to have.
static void pe_add_runtime(tli_State *s1, struct pe_state *pe)
{
    const char *start_symbol, *run_symbol;
    i32 type;

    // Whichever main the objects define picks both the subsystem and the
    // startup code that will call it.
    if (TLI_OUTPUT_DLL == s1->output_type) {
        type = PE_DLL;
        run_symbol = start_symbol = PE_STDSYM("__dllstart", "@12");
    } else if (find_elf_sym(symtab_section, PE_STDSYM("WinMain", "@16"))) {
        type = PE_GUI;
        start_symbol = "__winstart", run_symbol = "__runwinmain";
    } else if (find_elf_sym(symtab_section, PE_STDSYM("wWinMain", "@16"))) {
        type = PE_GUI;
        start_symbol = "__wwinstart", run_symbol = "__runwwinmain";
    } else if (find_elf_sym(symtab_section, "wmain")) {
        type = PE_EXE;
        start_symbol = "__wstart", run_symbol = "__runwmain";
    } else {
        type = s1->pe_subsystem == 2 ? PE_GUI : PE_EXE;
        start_symbol = "__start", run_symbol = "__runmain";
    }

    if (TLI_OUTPUT_MEMORY == s1->output_type && !s1->nostdlib)
        start_symbol = run_symbol;

    // Without tinylinker's own startup code the C runtime the caller named
    // brings the entry point, under the name every Windows linker defaults to.
    if (s1->nostdlib && !s1->elf_entryname) {
        const char *crt = pe_crt_start(start_symbol);

        if (get_sym_addr(s1, crt + 1, 0, 1) != (addr_t)-1)
            start_symbol = crt;
    }

    if (s1->elf_entryname) {
        pe->entry_symbol = start_symbol = s1->elf_entryname;
    } else {

        // The names above carry the underscore a 32-bit C compiler would have
        // added; get_sym_addr() adds it back when it looks the entry up, so
        // the recorded name has to be one underscore shorter.
        pe->entry_symbol = start_symbol + 1;
        if (!s1->leading_underscore || strchr(start_symbol, '@'))
            start_symbol++;
    }

#ifdef TLI_BACKTRACE
    if (s1->do_backtrace) {
# ifdef TLI_BCHECK
        if (s1->do_bounds_check && s1->output_type != TLI_OUTPUT_DLL)
            tli_add_support(s1, "bcheck.o", 0);
# endif
        if (s1->output_type == TLI_OUTPUT_EXE)
            tli_add_support(s1, "bt-exe.o", 0);

        if (s1->output_type == TLI_OUTPUT_DLL)
            tli_add_support(s1, "bt-dll.o", 0);

        if (s1->output_type != TLI_OUTPUT_DLL)
            tli_add_support(s1, "bt-log.o", 0);

        tli_add_btstub(s1);
    }
#endif

    // Referring to the entry point is what pulls the startup code out of an
    // archive, so it has to happen before the libraries are scanned.
#ifdef TLI_HOST_RUN
    if (TLI_OUTPUT_MEMORY != s1->output_type || s1->run_main)
#endif
    set_global_sym(s1, start_symbol, NULL, 0);

    if (!s1->nostdlib) {

        // The C runtime and the parts of Windows a program is assumed to have.
        // The empty entry marks where a console program can stop: a GUI one
        // carries on and picks up the window and drawing libraries too.
        static const char *const libs[] = {
            "msvcrt", "kernel32", "", "user32", "gdi32", NULL
        };
        const char *const *p;

        if (TLI_RUNTIME_LIB[0])
            tli_add_support(s1, TLI_RUNTIME_LIB, 1);

        s1->static_link = 0;   // there is no static C runtime to link
        for (p = libs; *p; p++) {
            if (**p)
                tli_add_library(s1, *p);
            else if (PE_DLL != type && PE_GUI != type)
                break;
        }
    }

    // From here on a DLL is laid out exactly like an executable; the
    // difference is a flag in the header and the export table.
    if (TLI_OUTPUT_DLL == s1->output_type)
        s1->output_type = TLI_OUTPUT_EXE;

    if (TLI_OUTPUT_MEMORY == s1->output_type)
        type = PE_RUN;

    pe->type = type;
}

/// Turn a -Wl,-subsystem= argument into the number the optional header wants.
ST_FUNC i32 pe_setsubsy(tli_State *s1, const char *arg)
{
    static const struct { const char *name; i32 value; } known[] = {
        { "native", 1 },
        { "gui", 2 },
        { "windows", 2 },
        { "console", 3 },
        { "posix", 7 },
        { "efiapp", 10 },
        { "efiboot", 11 },
        { "efiruntime", 12 },
        { "efirom", 13 },
    };
    u32 i;

    for (i = 0; i < countof(known); i++)
        if (0 == strcmp(known[i].name, arg)) {
            s1->pe_subsystem = known[i].value;
            return 0;
        }

    return -1;
}

/// Settle the image base, the subsystem and the two alignments, each of which
/// the command line may override.
static void pe_set_options(tli_State *s1, struct pe_state *pe)
{
    pe->imagebase = PE_DLL == pe->type ? PE_IMAGE_BASE_DLL : PE_IMAGE_BASE_EXE;

    if (PE_DLL == pe->type || PE_GUI == pe->type)
        pe->subsystem = 2;
    else
        pe->subsystem = 3;

    if (s1->pe_subsystem != 0)
        pe->subsystem = s1->pe_subsystem;

    // A native-subsystem image is a driver or the loader itself, which is
    // mapped as it lies rather than a page at a time.
    if (pe->subsystem == 1) {
        pe->section_align = 0x20;
        pe->file_align = 0x20;
    } else {
        pe->section_align = 0x1000;
        pe->file_align = 0x200;
    }

    if (s1->section_align)
        pe->section_align = s1->section_align;

    if (s1->pe_file_align)
        pe->file_align = s1->pe_file_align;

    // EFI images are loaded wherever firmware finds room, so they are built to
    // be relocated from zero.
    if (pe->subsystem >= 10 && pe->subsystem <= 12)
        pe->imagebase = 0;

    if (s1->has_text_addr)
        pe->imagebase = s1->text_addr;
}

/// Send every reference to a weak external that nothing defined to the symbol
/// it stands for, before imports are bound, so that a name standing for an
/// import becomes a thunk or a slot like any other. The alias itself is left
/// weak and unreferenced.
static void pe_resolve_aliases(tli_State *s1)
{
    i32 a, i;

    for (a = 0; a < s1->nb_coff_aliases; a += 2) {
        i32 from = find_elf_sym(symtab_section, s1->coff_aliases[a]);
        i32 to = find_elf_sym(symtab_section, s1->coff_aliases[a + 1]);
        ElfW(Sym) *sym;

        if (!from || !to)
            continue;

        sym = (ElfW(Sym) *)symtab_section->data + from;
        if (sym->st_shndx != SHN_UNDEF)
            continue;

        sym->st_info = ELFW(ST_INFO)(STB_WEAK, ELFW(ST_TYPE)(sym->st_info));

        // Every relocation naming the alias names the real symbol instead.
        for (i = 1; i < s1->nb_sections; i++) {
            tli_Section *sr = s1->sections[i]->reloc;
            ElfW_Rel *rel;

            if (!sr)
                continue;

            for_each_elem(sr, 0, rel, ElfW_Rel)
                if ((i32)ELFW(R_SYM)(rel->r_info) == from)
                    rel->r_info = ELFW(R_INFO)(to, ELFW(R_TYPE)(rel->r_info));
        }
    }
}

/// End the import descriptors that import libraries bring in .idata$2 with the
/// all-zero one the loader stops at. dlltool's libraries leave that to the
/// linker, and the name sorts after every other .idata$3.
static void pe_end_import_groups(tli_State *s1)
{
    tli_Section *end;
    i32 i;

    for (i = 1; i < s1->nb_sections; i++)
        if (0 == strncmp(s1->sections[i]->name, ".idata$2", 8))
            break;

    if (i == s1->nb_sections)
        return;

    end = new_section(s1, ".idata$3\377", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
    end->sh_addralign = 4;
    section_ptr_add(end, PE_IMPORT_DESC_SIZE);
}

/// Bracket gcc's constructor and destructor lists the way GNU ld's script does:
/// __CTOR_LIST__ names a -1, the entries follow, and a 0 ends them. libgcc
/// declares both names common so that a linker's definition wins, which left
/// alone makes an empty list and constructors that silently never run.
static void pe_add_ctor_lists(tli_State *s1)
{
    static const char *const lists[] = {".ctors", ".dtors"};
    static const char *const names[] = {"__CTOR_LIST__", "__DTOR_LIST__"};
    i32 i, j;

    for (i = 0; i < 2; i++) {
        tli_Section *head, *tail;
        char buf[32];
        i32 used = 0;

        // Wanted when there are entries, or when something walks the list.
        for (j = 1; j < s1->nb_sections && !used; j++)
            used = 0 == strncmp(s1->sections[j]->name, lists[i], 6)
                   && '$' == s1->sections[j]->name[6];

        snprintf(buf, sizeof buf, "_%s", names[i]);
        used = used || find_elf_sym(symtab_section, names[i])
               || find_elf_sym(symtab_section, buf);
        if (!used)
            continue;

        snprintf(buf, sizeof buf, "%s$a", lists[i]);
        head = new_section(s1, buf, SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
        memset(section_ptr_add(head, PTR_SIZE), 0xff, PTR_SIZE);

        snprintf(buf, sizeof buf, "%s$z", lists[i]);
        tail = new_section(s1, buf, SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
        section_ptr_add(tail, PTR_SIZE);

        // GNU ld defines both spellings whatever the target.
        set_global_sym(s1, names[i], head, 0);
        snprintf(buf, sizeof buf, "_%s", names[i]);
        set_global_sym(s1, buf, head, 0);
    }
}

/// Point the import and address table directories at what the import
/// libraries brought, once it has addresses: descriptors in .idata$2 and $3,
/// slots in .idata$5. Neither may be mixed with the tables built here.
static void pe_find_import_groups(struct pe_state *pe)
{
    tli_State *s1 = pe->s1;
    addr_t imp_lo = 0, imp_hi = 0, iat_lo = 0, iat_hi = 0;
    i32 i;

    for (i = 1; i < s1->nb_sections; i++) {
        tli_Section *s = s1->sections[i];
        addr_t end = s->sh_addr + s->data_offset;

        // The descriptors, then the slots the loader fills in.
        if (0 == strncmp(s->name, ".idata$2", 8)
            || 0 == strncmp(s->name, ".idata$3", 8)) {
            imp_lo = imp_lo && imp_lo < s->sh_addr ? imp_lo : s->sh_addr;
            imp_hi = imp_hi > end ? imp_hi : end;
        } else if (0 == strncmp(s->name, ".idata$5", 8)) {
            iat_lo = iat_lo && iat_lo < s->sh_addr ? iat_lo : s->sh_addr;
            iat_hi = iat_hi > end ? iat_hi : end;
        }
    }

    if (!imp_lo)
        return;

    if (pe->import_size) {
        tli_error_noabort("cannot combine dlltool import libraries with "
                          "imports bound from a DLL or a short import library");
        return;
    }

    pe->import_rva = pe_rva(pe, imp_lo);
    pe->import_size = (u32)(imp_hi - imp_lo);
    pe->iat_rva = iat_lo ? pe_rva(pe, iat_lo) : 0;
    pe->iat_size = (u32)(iat_hi - iat_lo);
}

/// Define a symbol a Windows linker provides, when something refers to it and
/// nothing defines it.
static void pe_provide(tli_State *s1, const char *name, tli_Section *sec,
                       addr_t offs)
{
    char buf[64];
    i32 i;

    snprintf(buf, sizeof buf, "%s%s", s1->leading_underscore ? "_" : "", name);
    i = find_elf_sym(symtab_section, buf);
    if (!i || ((ElfW(Sym) *)symtab_section->data)[i].st_shndx != SHN_UNDEF)
        return;

    set_global_sym(s1, buf, sec, offs);
}

/// Define the symbols the C runtime expects of the linker, once sections have
/// addresses.
///
/// __ImageBase is the address the image is mapped at, placed relative to .text
/// rather than made absolute so that a pointer to it gets a base relocation.
/// __image_base__ is GNU ld's name for it, which older mingw runtimes read.
/// The pseudo-relocation list is what GNU ld builds for data imported without
/// __declspec(dllimport). Nothing here does that, so the list is empty, and the
/// mingw runtime that walks it at startup finds nothing to do.
static void pe_define_linker_symbols(struct pe_state *pe)
{
    tli_State *s1 = pe->s1;

    pe_provide(s1, "__ImageBase", text_section,
               pe->imagebase - text_section->sh_addr);
    pe_provide(s1, "__image_base__", text_section,
               pe->imagebase - text_section->sh_addr);
    pe_provide(s1, "__RUNTIME_PSEUDO_RELOC_LIST__", data_section, 0);
    pe_provide(s1, "__RUNTIME_PSEUDO_RELOC_LIST_END__", data_section, 0);
}

/// Link everything gathered so far into a PE image, or into memory when there
/// is no filename to write to.
ST_FUNC i32 pe_output_file(tli_State *s1, const char *filename)
{
    struct pe_state pe;
    i32 i;

    memset(&pe, 0, sizeof pe);
    pe.s1 = s1;
    pe.filename = filename;
    pe.tables = rodata_section;
    s1->filetype = 0;

    // Everything that can still add a section or a symbol, in the order that
    // lets each stage see what the one before it pulled in.
#ifdef TLI_BCHECK
    tli_add_bcheck(s1);
#endif
    tli_add_pragma_libs(s1);
    pe_add_runtime(s1, &pe);
    pe_end_import_groups(s1);
    pe_add_ctor_lists(s1);
    resolve_common_syms(s1);
    pe_set_options(s1, &pe);
    pe_resolve_aliases(s1);
    pe_bind_imports(&pe);
    if (s1->nb_errors)
        goto done;

    if (!filename) {

        // -run: no image is written, and nobody will map one, so the address
        // table is filled in here with addresses fetched from the DLLs
        // themselves.  It goes in .data, which tl_run.c leaves writable.
        pe.tables = data_section;
#ifdef TLI_HOST_RUN
        pe_build_imports(&pe);
        s1->run_main = pe.entry_symbol;
        if (s1->arch->em == EM_X86_64 || s1->arch->em == EM_AARCH64)
            s1->uw_pdata = find_section(s1, ".pdata");
#endif
        goto done;
    }

    if (s1->do_debug) {

        // Reserve them now: creating a section later would disturb the layout,
        // and the string table's first word is its own length.
        pe.coffsym = new_section(s1, ".coffsym", SHT_PROGBITS, SHF_PRIVATE);
        pe.coffstr = new_section(s1, ".coffstr", SHT_PROGBITS, SHF_PRIVATE);
        section_ptr_add(pe.coffstr, 4);
    }

    // Addresses, then symbols, then the section contents those symbols are
    // relocated into.
    pe_assign_addresses(&pe);
    pe_find_import_groups(&pe);
    pe_define_linker_symbols(&pe);
    relocate_syms(s1, s1->symtab, 0);
    if (s1->nb_errors)
        goto done;

    // A runtime that brings its own TLS directory names it _tls_used.
    if (!pe.tls_size) {
        addr_t used = get_sym_addr(s1, "_tls_used", 0, 1);

        if (used != (addr_t)-1)
            pe.tls_used_rva = pe_rva(&pe, used);
    }

    s1->pe_imagebase = pe.imagebase;
    relocate_sections(s1);
    pe.entry_rva = (u32)(get_sym_addr(s1, pe.entry_symbol, 1, 1)
                         - pe.imagebase);
    if (s1->nb_errors)
        goto done;

    if (pe.coffsym)
        pe_build_coff_symbols(&pe);

    pe_write(&pe);

done:
    for (i = 0; i < pe.nsects; i++)
        tli_free(pe.sects[i].parts);

    tli_free(pe.sects);
    tli_free(pe.sect_of);
    pe_free_imports(&pe);
    return s1->nb_errors ? -1 : 0;
}

// -------------------------------------------------------------------------


/// ---- src/core/tl_coff_obj.c ----

/// tinylinker - COFF relocatable object reader
///
/// Reads a COFF .obj produced by clang or MSVC and folds it into the same
/// internal model the ELF loader builds, so section merging, symbol
/// resolution, relocation and output are all unchanged.

/// tli_Section characteristics: what a section holds, what the linker should do
/// with it, how it is aligned, and what permissions it wants.
#define IMAGE_SCN_CNT_CODE                  0x00000020
#define IMAGE_SCN_CNT_INITIALIZED_DATA      0x00000040
#define IMAGE_SCN_CNT_UNINITIALIZED_DATA    0x00000080
#define IMAGE_SCN_LNK_INFO                  0x00000200
#define IMAGE_SCN_LNK_REMOVE                0x00000800
#define IMAGE_SCN_LNK_COMDAT                0x00001000
#define IMAGE_SCN_ALIGN_MASK                0x00F00000
#define IMAGE_SCN_MEM_DISCARDABLE           0x02000000
#define IMAGE_SCN_MEM_EXECUTE               0x20000000
#define IMAGE_SCN_MEM_READ                  0x40000000
#define IMAGE_SCN_MEM_WRITE                 0x80000000

/// Storage classes, which is where COFF keeps what ELF calls a symbol's
/// binding.
#define IMAGE_SYM_CLASS_EXTERNAL        2
#define IMAGE_SYM_CLASS_STATIC          3
#define IMAGE_SYM_CLASS_LABEL           6
#define IMAGE_SYM_CLASS_FILE            103
#define IMAGE_SYM_CLASS_WEAK_EXTERNAL   105

/// tli_Section numbers below 1 are not section indices.
#define IMAGE_SYM_UNDEFINED     0
#define IMAGE_SYM_ABSOLUTE      (-1)
#define IMAGE_SYM_DEBUG         (-2)

/// The derived type in the upper nibble that marks a symbol as a function.
#define IMAGE_SYM_DTYPE_FUNCTION 2

/// The two on-disk record sizes the format fixes, neither of which a C struct
/// would give us: both are packed and neither is a multiple of its alignment.
#define COFF_SYM_SIZE  18
#define COFF_RELOC_SIZE 10

/// x86-64 relocation types.  REL32 through REL32_5 are consecutive, which the
/// reader below relies on.
#define IMAGE_REL_AMD64_ABSOLUTE 0x0000
#define IMAGE_REL_AMD64_ADDR64   0x0001
#define IMAGE_REL_AMD64_ADDR32   0x0002
#define IMAGE_REL_AMD64_ADDR32NB 0x0003
#define IMAGE_REL_AMD64_REL32    0x0004
#define IMAGE_REL_AMD64_REL32_5  0x0009
#define IMAGE_REL_AMD64_SECTION  0x000A
#define IMAGE_REL_AMD64_SECREL   0x000B

/// i386 relocation types.
#define IMAGE_REL_I386_ABSOLUTE 0x0000
#define IMAGE_REL_I386_DIR32    0x0006
#define IMAGE_REL_I386_REL32    0x0014

/// arm64 relocation types.
#define IMAGE_REL_ARM64_ABSOLUTE        0x0000
#define IMAGE_REL_ARM64_ADDR32          0x0001
#define IMAGE_REL_ARM64_ADDR32NB        0x0002
#define IMAGE_REL_ARM64_BRANCH26        0x0003
#define IMAGE_REL_ARM64_PAGEBASE_REL21  0x0004
#define IMAGE_REL_ARM64_PAGEOFFSET_12A  0x0006
#define IMAGE_REL_ARM64_PAGEOFFSET_12L  0x0007
#define IMAGE_REL_ARM64_SECREL_LOW12A   0x0009
#define IMAGE_REL_ARM64_SECREL_HIGH12A  0x000A
#define IMAGE_REL_ARM64_ADDR64          0x000e
#define IMAGE_REL_ARM64_BRANCH19        0x000f
#define IMAGE_REL_ARM64_BRANCH14        0x0010
#define IMAGE_REL_ARM64_REL32           0x0011

/// Where one input section ended up in the merged output.
struct coff_sect {
    tli_Section *s;          /// NULL if the section was dropped.
    u64 off;   /// Where its contents start inside s.
    i32 sym;             /// Lazily made STT_SECTION symbol, 0 if none yet.
};

/// One entry of the object's symbol table, decoded. The on-disk form is 18
/// bytes with no padding, which no natural C struct layout gives us.
struct coff_sym {
    char name[256];
    u32 value;
    i32 sec;             /// 1-based section, or one of IMAGE_SYM_*.
    u16 type;
    u8 cls;
    u8 naux;
};

/// Decode one 18-byte symbol record.  A name of eight characters or fewer sits
/// in the record; COFF spells a longer one as four zero bytes followed by an
/// offset into the string table.
static void coff_read_sym(const u8 *p, const char *strtab,
                          u32 strsize, struct coff_sym *sym)
{
    if (read32le((u8 *)p) == 0) {
        u32 off = read32le((u8 *)p + 4);

        if (off < strsize)
            pstrcpy(sym->name, sizeof sym->name, strtab + off);
        else
            sym->name[0] = '\0';
    } else {

        // an 8-byte field that is only NUL-terminated when it is shorter
        memcpy(sym->name, p, 8);
        sym->name[8] = '\0';
    }

    sym->value = read32le((u8 *)p + 8);
    sym->sec = (i16)read16le((u8 *)p + 12);
    sym->type = read16le((u8 *)p + 14);
    sym->cls = p[16];
    sym->naux = p[17];
}

// -------------------------------------------------------------------------

/// Decide what an input section becomes internally, or return 0 to drop it.
static i32 coff_classify(const IMAGE_SECTION_HEADER *sh, const char *sname,
                         char name[32], i32 *sh_type, i32 *sh_flags)
{
    u32 ch = sh->Characteristics;
    const char *p;
    usize n;

    // tli_Section groups are spelled ".text$mn"; everything after the dollar is
    // an ordering key for the linker, not part of the name.
    p = strchr(sname, '$');
    n = p ? (usize)(p - sname) : strlen(sname);
    if (n > 31)
        n = 31;

    memcpy(name, sname, n);
    name[n] = '\0';

    if (ch & (IMAGE_SCN_LNK_REMOVE | IMAGE_SCN_LNK_INFO | IMAGE_SCN_MEM_DISCARDABLE))
        return 0;

    // Unwind data is only meaningful together with the .pdata table that
    // indexes it, and building that needs image-relative relocations the PE
    // writer does not emit. Exceptions and stack walking lose out; ordinary
    // code does not care.
    if (0 == strcmp(name, ".pdata") || 0 == strcmp(name, ".xdata"))
        return 0;

    if (ch & IMAGE_SCN_CNT_UNINITIALIZED_DATA) {
        *sh_type = SHT_NOBITS;
        *sh_flags = SHF_ALLOC | SHF_WRITE;
        pstrcpy(name, 32, ".bss");
        return 1;
    }

    // Everything else keeps its contents, and only the permissions decide
    // which of the three output sections it joins.
    *sh_type = SHT_PROGBITS;
    if (ch & IMAGE_SCN_CNT_CODE) {
        pstrcpy(name, 32, ".text");
        *sh_flags = SHF_ALLOC | SHF_EXECINSTR;
        return 1;
    }

    if (ch & IMAGE_SCN_MEM_WRITE) {
        pstrcpy(name, 32, ".data");
        *sh_flags = SHF_ALLOC | SHF_WRITE;
        return 1;
    }

    // read-only data: .rdata and anything else that is neither code nor
    // writable, which a PE link, the only one reading COFF, calls .rdata
    pstrcpy(name, 32, ".rdata");
    *sh_flags = shf_RELRO;
    return 1;
}

/// The internal name for a grouped section, ".idata$5" or ".CRT$XCA", which
/// the PE writer sorts its group by.
///
/// The import sections are ordered by the archive member they came from as
/// well, as GNU ld does: that keeps one DLL's lookup table together, between
/// the head member and the tail member whose names sort around it. gcc's
/// constructor lists become groups too, ordered as GNU ld lays them out:
/// .ctors, then .ctor, then .ctors.<priority> by name.
///
/// @return 0 when the section is not part of a group.
static i32 coff_group_name(tli_State *s1, const char *sname, char *buf,
                           usize size)
{
    static const char *const lists[] = {".ctors", ".dtors"};
    i32 i;

    // The constructor and destructor lists, in the order they are walked.
    for (i = 0; i < 2; i++) {
        usize n = strlen(lists[i]);

        if (0 == strcmp(sname, lists[i]))
            return snprintf(buf, size, "%s$b", lists[i]), 1;

        if (0 == strncmp(sname, lists[i], n - 1) && '\0' == sname[n - 1])
            return snprintf(buf, size, "%s$c", lists[i]), 1;

        if (0 == strncmp(sname, lists[i], n) && '.' == sname[n])
            return snprintf(buf, size, "%s$d%s", lists[i], sname + n), 1;
    }

    if (0 == strncmp(sname, ".idata$", 7) && s1->current_member) {
        snprintf(buf, size, "%s\001%s(%s)", sname, s1->current_filename,
                 s1->current_member);
        return 1;
    }

    // .tls is the head of a group whose members are all spelled with the
    // dollar.
    if (!strchr(sname, '$') && 0 != strcmp(sname, ".tls"))
        return 0;

    pstrcpy(buf, size, sname);
    return 1;
}

/// The alignment a section asks for, which COFF stores as a power of two
/// biased by one so that zero can mean "unspecified".
static i32 coff_align(u32 characteristics)
{
    i32 a = (characteristics & IMAGE_SCN_ALIGN_MASK) >> 20;

    return a ? 1 << (a - 1) : 1;
}

/// The STT_SECTION symbol for one input section, made on first use, so that a
/// relocation against a section has something to name.
static i32 coff_section_sym(tli_State *s1, struct coff_sect *cs, i32 n, i32 nsects)
{
    if (n < 1 || n > nsects || !cs[n - 1].s)
        return 0;

    if (!cs[n - 1].sym)
        cs[n - 1].sym = put_elf_sym(symtab_section, 0, 0,
                                    ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0,
                                    cs[n - 1].s->sh_num, NULL);

    return cs[n - 1].sym;
}

/// Sign-extend the low 'bits' of a value.
static i64 coff_sext(u64 v, i32 bits)
{
    u64 m = (u64)1 << (bits - 1);

    return (i64)((v ^ m) - m);
}

/// IMAGE_REL_ARM64_PAGEOFFSET_12L covers every unsigned-offset load and store,
/// and the ELF relocation that means the same thing depends on the access
/// size, which is in the instruction. Returns the type and scales the addend
/// the instruction encodes.
static i32 coff_lo12_reloc(u32 insn, i64 *addend)
{
    i64 imm = (insn >> 10) & 0xfff;
    i32 size = insn >> 30 & 3;
    i32 v = insn >> 26 & 1;
    i32 opc1 = insn >> 23 & 1;

    // A 128-bit vector access is the one case the size field cannot say on its
    // own: it reuses size 0 and marks itself in the opcode instead.
    if (v && size == 0 && opc1) {
        *addend = imm << 4;
        return R_AARCH64_LDST128_ABS_LO12_NC;
    }

    *addend = imm << size;
    switch (size) {
    case 0: return R_AARCH64_LDST8_ABS_LO12_NC;
    case 1: return R_AARCH64_LDST16_ABS_LO12_NC;
    case 2: return R_AARCH64_LDST32_ABS_LO12_NC;
    default: return R_AARCH64_LDST64_ABS_LO12_NC;
    }
}

// -------------------------------------------------------------------------

/// How many bytes of section data a relocation patches, or zero for the ones
/// that patch none.  The bounds check below needs this before the type switch
/// has run, which is why it is a second, smaller switch and not part of that
/// one.
static u32 coff_reloc_width(tli_State *s1, i32 rtype)
{
    switch (s1->arch->em) {
    case EM_X86_64:
        if (rtype == IMAGE_REL_AMD64_ABSOLUTE)
            return 0;

        return rtype == IMAGE_REL_AMD64_ADDR64 ? 8 : 4;
    case EM_386:
        if (rtype == IMAGE_REL_I386_ABSOLUTE)
            return 0;

        return 4;
    case EM_AARCH64:
        if (rtype == IMAGE_REL_ARM64_ABSOLUTE)
            return 0;

        return rtype == IMAGE_REL_ARM64_ADDR64 ? 8 : 4;
    }
    return 4;
}

/// Translate one AMD64 COFF relocation, moving the addend out of its field
/// into '*addend'.  Returns the ELF type, 0 for one that asks for nothing, or
/// -1 for a type with no equivalent.
static i32 coff_in_amd64(u8 *ptr, i32 rtype, i64 *addend)
{
    i32 elf_type;

    switch (rtype) {
    case IMAGE_REL_AMD64_ABSOLUTE:
        return 0;
    case IMAGE_REL_AMD64_ADDR64:
        elf_type = R_X86_64_64;
        *addend += (i64)read64le(ptr);
        write64le(ptr, 0);
        break;
    case IMAGE_REL_AMD64_ADDR32:
        elf_type = R_X86_64_32;
        *addend += (i32)read32le(ptr);
        write32le(ptr, 0);
        break;
    case IMAGE_REL_AMD64_ADDR32NB:

        // An address relative to the image base, which is what
        // R_X86_64_RELATIVE means when the output is PE.
        elf_type = R_X86_64_RELATIVE;
        *addend += (i32)read32le(ptr);
        write32le(ptr, 0);
        break;
    case IMAGE_REL_AMD64_REL32:
    case IMAGE_REL_AMD64_REL32 + 1:
    case IMAGE_REL_AMD64_REL32 + 2:
    case IMAGE_REL_AMD64_REL32 + 3:
    case IMAGE_REL_AMD64_REL32 + 4:
    case IMAGE_REL_AMD64_REL32_5:

        // COFF measures from the end of the instruction, ELF from the
        // field; REL32_n says n more bytes follow the field.
        elf_type = R_X86_64_PC32;
        *addend += (i32)read32le(ptr) - 4
                   - (rtype - IMAGE_REL_AMD64_REL32);
        write32le(ptr, 0);
        break;
    case IMAGE_REL_AMD64_SECREL:

        // In loaded code this is a thread-local's offset within .tls,
        // which a PE link measures as the local-exec offset. Debug
        // sections, the other users, are never read in.
        elf_type = R_X86_64_TPOFF32;
        *addend += (i32)read32le(ptr);
        write32le(ptr, 0);
        break;
    default:
        return -1;
    }
    return elf_type;
}

/// The same for i386, whose relocations keep their addend in the field.
static i32 coff_in_i386(u8 *ptr, i32 rtype)
{
    i32 elf_type;

    switch (rtype) {
    case IMAGE_REL_I386_ABSOLUTE:
        return 0;
    case IMAGE_REL_I386_DIR32:
        elf_type = R_386_32;
        break;
    case IMAGE_REL_I386_REL32:

        // i386 relocations carry no addend field, so fold the four
        // byte difference between the two conventions into the data.
        elf_type = R_386_PC32;
        write32le(ptr, read32le(ptr) - 4);
        break;
    default:
        return -1;
    }
    return elf_type;
}

/// The same for ARM64.
static i32 coff_in_arm64(u8 *ptr, i32 rtype, i64 *addend)
{
    i32 elf_type;

    switch (rtype) {
    case IMAGE_REL_ARM64_ABSOLUTE:
        return 0;
    case IMAGE_REL_ARM64_ADDR64:
        elf_type = R_AARCH64_ABS64;
        *addend += (i64)read64le(ptr);
        write64le(ptr, 0);
        break;
    case IMAGE_REL_ARM64_ADDR32:
        elf_type = R_AARCH64_ABS32;
        *addend += (i32)read32le(ptr);
        write32le(ptr, 0);
        break;
    case IMAGE_REL_ARM64_ADDR32NB:

        // An address relative to the image base, which is what
        // R_AARCH64_RELATIVE means when the output is PE.
        elf_type = R_AARCH64_RELATIVE;
        *addend += (i32)read32le(ptr);
        write32le(ptr, 0);
        break;
    case IMAGE_REL_ARM64_REL32:
        elf_type = R_AARCH64_PREL32;
        *addend += (i32)read32le(ptr) - 4;
        write32le(ptr, 0);
        break;
    case IMAGE_REL_ARM64_BRANCH26:
        elf_type = R_AARCH64_CALL26;
        *addend += coff_sext(read32le(ptr) & 0x03ffffff, 26) << 2;
        break;
    case IMAGE_REL_ARM64_BRANCH19:
        elf_type = R_AARCH64_CONDBR19;
        *addend += coff_sext(read32le(ptr) >> 5 & 0x7ffff, 19) << 2;
        break;
    case IMAGE_REL_ARM64_BRANCH14:
        elf_type = R_AARCH64_TSTBR14;
        *addend += coff_sext(read32le(ptr) >> 5 & 0x3fff, 14) << 2;
        break;
    case IMAGE_REL_ARM64_PAGEBASE_REL21: {

        // the ADRP immediate doubles as the addend, exactly as
        // link.exe and lld read it
        u32 insn = read32le(ptr);

        elf_type = R_AARCH64_ADR_PREL_PG_HI21;
        *addend += coff_sext(((insn >> 29) & 3) | ((insn >> 3) & 0x1ffffc), 21);
        break;
    }
    case IMAGE_REL_ARM64_PAGEOFFSET_12A:
        elf_type = R_AARCH64_ADD_ABS_LO12_NC;
        *addend += (read32le(ptr) >> 10) & 0xfff;
        break;
    case IMAGE_REL_ARM64_PAGEOFFSET_12L: {
        i64 a;

        elf_type = coff_lo12_reloc(read32le(ptr), &a);
        *addend += a;
        break;
    }

    // A thread-local's offset within .tls, built by an ADD pair, which a PE
    // link measures as the local-exec offset.
    case IMAGE_REL_ARM64_SECREL_HIGH12A:
        elf_type = R_AARCH64_TLSLE_ADD_TPREL_HI12;
        *addend += (i64)((read32le(ptr) >> 10) & 0xfff) << 12;
        break;
    case IMAGE_REL_ARM64_SECREL_LOW12A:
        elf_type = R_AARCH64_TLSLE_ADD_TPREL_LO12_NC;
        *addend += (read32le(ptr) >> 10) & 0xfff;
        break;
    default:
        return -1;
    }
    return elf_type;
}

/// Read one COFF object into the internal model: its sections, then its
/// symbols, then its relocations, in that order because each pass needs what
/// the one before it decided.
ST_FUNC i32 coff_load_object(tli_State *s1, i32 fd, u64 file_offset)
{
    IMAGE_FILE_HEADER fh;
    IMAGE_SECTION_HEADER *shdrs = NULL;
    struct coff_sect *cs = NULL;
    struct coff_sym *syms = NULL;
    u8 *rawsyms = NULL, *rels = NULL;
    char *strtab = NULL;
    i32 *old_to_new = NULL;
    u32 strsize = 0, nsym, i;
    i32 j, ret = -1;
    u64 avail;

    lseek(fd, file_offset, SEEK_SET);
    if (full_read(fd, &fh, sizeof fh) != sizeof fh)
        return tli_error_noabort("invalid COFF object file");

    if (fh.Machine != IMAGE_FILE_MACHINE)
        return tli_error_noabort("COFF object is for a different architecture");

    // Nothing in the header has been checked against anything yet.  Measure
    // the file once and refuse a count that could not fit in it, before it is
    // multiplied into an allocation: a corrupt NumberOfSymbols otherwise asks
    // for tens of gigabytes and the link disappears into zeroing them.
    avail = file_span(fd, file_offset);

    if ((u64)fh.NumberOfSections * sizeof *shdrs > avail
        || (u64)fh.NumberOfSymbols * COFF_SYM_SIZE > avail)
        return tli_error_noabort("COFF object header describes more than the "
                                 "file holds");

    shdrs = load_data(fd, file_offset + sizeof fh + fh.SizeOfOptionalHeader,
                      sizeof *shdrs * fh.NumberOfSections);
    cs = tli_mallocz(sizeof *cs * (fh.NumberOfSections ? fh.NumberOfSections : 1));

    // Read the symbol table first: which COMDAT sections to keep depends on
    // the symbols that name them.
    nsym = fh.NumberOfSymbols;
    if (nsym && fh.PointerToSymbolTable) {
        u64 stroff = file_offset + fh.PointerToSymbolTable
                               + (u64)nsym * COFF_SYM_SIZE;
        u8 sz[4];

        // The string table follows the symbols with no header of its own; its
        // first four bytes are its total length, so four is the empty size.
        lseek(fd, stroff, SEEK_SET);
        if (full_read(fd, sz, 4) == 4) {
            strsize = read32le(sz);

            if (strsize >= 4)
                strtab = load_data(fd, stroff, strsize);
        }

        rawsyms = load_data(fd, file_offset + fh.PointerToSymbolTable,
                            (u64)nsym * COFF_SYM_SIZE);
        syms = tli_mallocz(sizeof *syms * nsym);
        for (i = 0; i < nsym; i++)
            coff_read_sym(rawsyms + (usize)i * COFF_SYM_SIZE, strtab, strsize,
                          &syms[i]);

        old_to_new = tli_mallocz(sizeof *old_to_new * nsym);
    }

    // Pass one: bring the section contents in.
    for (j = 0; j < fh.NumberOfSections; j++) {
        IMAGE_SECTION_HEADER *sh = &shdrs[j];
        char sname[32], name[32], group[512];
        i32 sh_type, sh_flags, align, k;
        tli_Section *s = NULL;

        // A name longer than eight characters is kept in the string table
        // and referenced as "/<offset>".
        if (sh->Name[0] == '/' && strtab) {
            char num[8];
            u32 off;

            memcpy(num, sh->Name + 1, 7);
            num[7] = '\0';
            off = atoi(num);
            pstrcpy(sname, sizeof sname, off < strsize ? strtab + off : "");
        } else {
            memcpy(sname, sh->Name, 8);
            sname[8] = '\0';
        }

        if (!coff_classify(sh, sname, name, &sh_type, &sh_flags))
            continue;

        // A grouped section stays apart from the rest of its kind, so the
        // writer can put its group in order.
        if (sh_type == SHT_NOBITS
            || !coff_group_name(s1, sname, group, sizeof group))
            pstrcpy(group, sizeof group, name);

        // A COMDAT section is one copy of something several objects define.
        // If its key symbol is already defined, drop this copy, as is done for
        // ".gnu.linkonce" sections in ELF.
        if ((sh->Characteristics & IMAGE_SCN_LNK_COMDAT) && syms) {
            for (i = 0; i < nsym; i++) {
                if (syms[i].sec != j + 1
                    || syms[i].cls != IMAGE_SYM_CLASS_EXTERNAL)
                    continue;

                k = find_elf_sym(symtab_section, syms[i].name);
                if (k && ((ElfW(Sym) *)symtab_section->data)[k].st_shndx
                         != SHN_UNDEF)
                    goto next_section;

                break;
            }
        }

        // Sections of one name are merged, so the contents go on the end of
        // whichever internal section already carries that name.
        for (k = 1; k < s1->nb_sections; k++) {
            if (0 == strcmp(s1->sections[k]->name, group)) {
                s = s1->sections[k];
                break;
            }
        }

        // A grouped section starts at the alignment its pieces ask for, not
        // the default for its type: padding between import descriptors
        // would read as one more, empty, descriptor.
        if (!s) {
            s = new_section(s1, group, sh_type, sh_flags);
            if (strchr(group, '$'))
                s->sh_addralign = 1;
        }

        // Same again for the section's own size, which is what section_add()
        // is about to allocate.
        if (sh_type != SHT_NOBITS && sh->SizeOfRawData > avail) {
            tli_error_noabort("%s: section '%s' is larger than the file",
                              s1->current_filename, sname);
            goto done;
        }

        align = coff_align(sh->Characteristics);
        cs[j].s = s;
        cs[j].off = section_add(s, sh->SizeOfRawData, align);
        if (align > s->sh_addralign)
            s->sh_addralign = align;

        if (sh_type != SHT_NOBITS && sh->SizeOfRawData && sh->PointerToRawData) {
            lseek(fd, file_offset + sh->PointerToRawData, SEEK_SET);
            full_read(fd, s->data + cs[j].off, sh->SizeOfRawData);
        }
        if (s1->arch->em == EM_AARCH64 && (s->sh_flags & SHF_EXECINSTR))
            section_add(s, 0, 4);
    next_section: ;
    }

    // Pass two: symbols.
    for (i = 0; syms && i < nsym; i += 1 + syms[i].naux) {
        struct coff_sym *sy = &syms[i];
        addr_t value = 0;
        u64 size = 0;
        i32 bind, type = STT_NOTYPE, shndx;

        if (sy->cls == IMAGE_SYM_CLASS_FILE || sy->cls == IMAGE_SYM_CLASS_LABEL
            || sy->sec == IMAGE_SYM_DEBUG || sy->name[0] == '\0')
            continue;

        // The section number says what kind of symbol this is before anything
        // else about it does.
        if (sy->sec == IMAGE_SYM_UNDEFINED) {
            if (sy->value) {

                // a tentative definition: the value is its size
                shndx = SHN_COMMON;
                value = 16;
                size = sy->value;
                type = STT_OBJECT;
            } else {
                shndx = SHN_UNDEF;
            }
        } else if (sy->sec == IMAGE_SYM_ABSOLUTE) {
            shndx = SHN_ABS;
            value = sy->value;
        } else if (sy->sec >= 1 && sy->sec <= fh.NumberOfSections
                   && !cs[sy->sec - 1].s) {

            // Defined in a section dropped as a duplicate COMDAT.  An external
            // name is still the copy that was kept, and this object's
            // relocations against it have to reach that one.
            if (sy->cls != IMAGE_SYM_CLASS_EXTERNAL)
                continue;

            shndx = SHN_UNDEF;
        } else if (sy->sec >= 1 && sy->sec <= fh.NumberOfSections) {
            shndx = cs[sy->sec - 1].s->sh_num;
            value = sy->value + cs[sy->sec - 1].off;
            type = (sy->type >> 4) == IMAGE_SYM_DTYPE_FUNCTION
                   ? STT_FUNC
                   : (cs[sy->sec - 1].s->sh_flags & SHF_EXECINSTR
                      ? STT_FUNC : STT_OBJECT);
        } else {
            continue;
        }

        if (sy->cls == IMAGE_SYM_CLASS_WEAK_EXTERNAL)
            bind = STB_WEAK;

        // An undefined weak external names, in its auxiliary record, the
        // symbol it stands for when nothing defines it. mingw's msvcrt
        // library spells its compatibility names that way: _XcptFilter for
        // _seh_filter_exe, with the __imp_ slot alongside.
        if (sy->cls == IMAGE_SYM_CLASS_WEAK_EXTERNAL && shndx == SHN_UNDEF
            && sy->naux && i + 1 < nsym) {
            u32 tag = read32le(rawsyms + (usize)(i + 1) * COFF_SYM_SIZE);

            if (tag < nsym && syms[tag].name[0]) {
                dynarray_add(&s1->coff_aliases, &s1->nb_coff_aliases,
                             tli_strdup(sy->name));
                dynarray_add(&s1->coff_aliases, &s1->nb_coff_aliases,
                             tli_strdup(syms[tag].name));
            }
        }
        else if (sy->cls == IMAGE_SYM_CLASS_EXTERNAL)
            bind = STB_GLOBAL;
        else
            bind = STB_LOCAL;

        old_to_new[i] = set_elf_sym(symtab_section, value, size,
                                    ELFW(ST_INFO)(bind, type), 0, shndx,
                                    sy->name);
    }

    // Pass three: relocations.
    for (j = 0; j < fh.NumberOfSections; j++) {
        IMAGE_SECTION_HEADER *sh = &shdrs[j];
        tli_Section *s = cs[j].s;
        u32 nreloc = sh->NumberOfRelocations;
        u32 r;

        if (!s || !nreloc || s->sh_type == SHT_NOBITS || !sh->PointerToRelocations)
            continue;

        rels = load_data(fd, file_offset + sh->PointerToRelocations,
                         (u64)nreloc * COFF_RELOC_SIZE);

        for (r = 0; r < nreloc; r++) {
            const u8 *re = rels + (usize)r * COFF_RELOC_SIZE;
            u32 va = read32le((u8 *)re);
            u32 idx = read32le((u8 *)re + 4);
            i32 rtype = read16le((u8 *)re + 8);
            u64 off = cs[j].off + va;
            u8 *ptr = s->data + off;
            i32 sym_index, elf_type = 0;
            i64 addend = 0;
            u32 width = coff_reloc_width(s1, rtype);

            // The offset came out of the file, so it has to be checked
            // against the section before it is turned into a pointer into
            // it.  Several of the types below write the field back, which
            // makes an unchecked offset a write anywhere in the process.
            if (width && (off > s->data_offset
                          || s->data_offset - off < width)) {
                tli_error_noabort("%s: relocation at 0x%llx is past the end of "
                                  "'%s' (0x%llx)", s1->current_filename,
                                  (unsigned long long)off, s->name,
                                  (unsigned long long)s->data_offset);
                goto done;
            }

            if (idx >= nsym || !syms) {
                tli_error_noabort("%s: relocation names symbol %u of %u",
                                  s1->current_filename, idx, nsym);
                goto done;
            }

            sym_index = old_to_new[idx];
            if (!sym_index) {

                // A relocation against a section rather than a symbol: the
                // entry names the section's own STATIC symbol.
                if (syms[idx].sec >= 1)
                    sym_index = coff_section_sym(s1, cs, syms[idx].sec,
                                                 fh.NumberOfSections);

                if (!sym_index) {
                    tli_error_noabort("%s: unresolvable relocation against '%s'",
                                      s1->current_filename, syms[idx].name);
                    goto done;
                }

                addend += cs[syms[idx].sec - 1].off + syms[idx].value;
            }

            // COFF keeps the addend in the field being relocated; ELF wants it
            // alongside, so each machine's reader moves it out and zeroes what
            // it read.  PE is refused for any machine but these three.
            switch (s1->arch->em) {
            case EM_X86_64:
                elf_type = coff_in_amd64(ptr, rtype, &addend);
                break;
            case EM_386:
                elf_type = coff_in_i386(ptr, rtype);
                break;
            case EM_AARCH64:
                elf_type = coff_in_arm64(ptr, rtype, &addend);
                break;
            default:
                elf_type = -1;
                break;
            }
            if (elf_type == 0)
                continue;

            if (elf_type < 0) {
                tli_error_noabort("%s: unsupported COFF relocation type %#x",
                                  s1->current_filename, rtype);
                goto done;
            }

            put_elf_reloca(symtab_section, s, off, elf_type, sym_index,
                           (addr_t)addend);
        }

        tli_free(rels);
        rels = NULL;
    }

    ret = s1->nb_errors ? -1 : 0;

done:
    tli_free(rels);
    tli_free(old_to_new);
    tli_free(syms);
    tli_free(rawsyms);
    tli_free(strtab);
    tli_free(cs);
    tli_free(shdrs);
    return ret;
}

/// The short import record, which is what an import library is made of.
///
/// A `.lib` is an ordinary archive, but most of its members are not objects:
/// each is one of these, twenty bytes of header followed by two NUL terminated
/// strings, the symbol and the DLL it comes from.  The two words at the front
/// are what tells one from an object -- no real object has machine zero -- and
/// they are read as a pair for exactly that reason.
struct coff_import_hdr {
    u16 sig1;          /// Zero, where an object has its machine.
    u16 sig2;          /// 0xffff, which an object's section count never is.
    u16 version;
    u16 machine;       /// The architecture, checked like an object's.
    u32 timestamp;
    u32 size_of_data;  /// Bytes of strings after this header.
    u16 ordinal;       /// An ordinal, or a hint when the import is by name.
    u16 type;          /// Two bits of kind, then three of how it is named.
};

/// The low two bits of 'type': what is being imported.
#define COFF_IMPORT_CODE  0
#define COFF_IMPORT_DATA  1
#define COFF_IMPORT_CONST 2

/// The next three bits: how the name relates to what is exported.  Only ORDINAL
/// is acted on, because it is the one that says the name is not the answer.
#define COFF_IMPORT_BY_ORDINAL 0

/// The name a weak external stands for, from the aliases coff_load_object()
/// recorded. An archive search treats the alias as provided, since what it
/// stands for is the name the search should go looking for instead.
ST_FUNC const char *coff_alias_of(tli_State *s1, const char *name)
{
    i32 i;

    for (i = 0; i < s1->nb_coff_aliases; i += 2)
        if (0 == strcmp(s1->coff_aliases[i], name))
            return s1->coff_aliases[i + 1];

    return NULL;
}

/// The machine an ARM64X import library's emulation-compatible records name.
#define IMAGE_FILE_MACHINE_ARM64EC 0xA641

/// Does this look like a short import record rather than an object?
ST_FUNC i32 coff_import_type(const void *hdr, i32 size)
{
    const struct coff_import_hdr *ih = hdr;

    return size >= (i32)sizeof *ih && ih->sig1 == 0 && ih->sig2 == 0xffff;
}

/// Read one short import record and record what it imports.
///
/// There is no code and no data here: the record says only that a name comes
/// from a DLL, which is the same thing a `.def` file says and is recorded the
/// same way.  The linker builds the thunk and the import table entry later,
/// out of this and of whoever turns out to reference it.
ST_FUNC i32 coff_load_import(tli_State *s1, i32 fd, u64 file_offset)
{
    struct coff_import_hdr ih;
    tli_DLLReference *ref;
    char *data, *name, *dll;
    u64 avail;
    i32 ret = -1;

    lseek(fd, file_offset, SEEK_SET);
    if (full_read(fd, &ih, sizeof ih) != sizeof ih)
        return tli_error_noabort("truncated import record");

    // An ARM64X library carries an ARM64EC record beside each ARM64 one, for
    // emulation-compatible code. A native link takes only its own, as the
    // Microsoft linker does.
    if (s1->arch->em == EM_AARCH64 && ih.machine == IMAGE_FILE_MACHINE_ARM64EC)
        return 0;


    if (ih.machine != IMAGE_FILE_MACHINE)
        return tli_error_noabort("import library is for a different "
                                 "architecture");

    // The two strings are counted by a field out of the file, so it is
    // measured against what is actually there before it is allocated.
    avail = file_span(fd, file_offset + sizeof ih);
    if (ih.size_of_data < 2 || (u64)ih.size_of_data > avail)
        return tli_error_noabort("import record claims %u bytes of names past "
                                 "the end of the file", ih.size_of_data);

    data = load_data(fd, file_offset + sizeof ih, ih.size_of_data);

    // Both names come out of the file, so neither is trusted to end: the
    // buffer is terminated and the DLL name is looked for only inside it.
    data[ih.size_of_data - 1] = 0;
    name = data;
    dll = name + strlen(name) + 1;

    if (dll >= data + ih.size_of_data) {
        tli_error_noabort("import record names a symbol but no library");
        goto done;
    }

    // An import by ordinal has no name to match on the far side, so the number
    // is carried through; by name, the field is a hint and means nothing here.
    ref = tli_add_dllref(s1, dll, 0);
    pe_putimport(s1, ref->index, name,
                 ((ih.type >> 2) & 7) == COFF_IMPORT_BY_ORDINAL ? ih.ordinal : 0);
    ret = 0;

done:
    tli_free(data);
    return ret;
}

/// Does this look like a COFF relocatable object we can read? An object has no
/// optional header, which is what separates it from an image.
ST_FUNC i32 coff_object_type(tli_State *s1, const void *hdr, i32 size)
{
    const IMAGE_FILE_HEADER *fh = hdr;

    if (size < (i32)sizeof *fh)
        return 0;

    return fh->Machine == IMAGE_FILE_MACHINE && fh->SizeOfOptionalHeader == 0
           && fh->NumberOfSections > 0 && fh->NumberOfSections < 0x1000;
}

// -------------------------------------------------------------------------
// writing a COFF object
//
// The inverse of everything above, so that `-r` on a PE target produces a .obj
// the platform's own tools accept rather than an ELF file wearing the wrong
// name.

/// One section on its way out, with the header being filled in for it and the
/// relocations translated so far.
struct coff_osect {
    tli_Section *s;
    IMAGE_SECTION_HEADER sh;
    u8 *rel;            /// The COFF relocation records, packed.
    u32 nreloc;
    i32 sym;            /// Index of this section's own symbol.
};

/// The string table being built alongside, for the names that do not fit in
/// their eight-byte fields.
struct coff_strtab {
    char *data;
    u32 size, alloc;
};

/// Add a name and return its offset.  COFF puts the string table's own size in
/// its first four bytes, so offsets start at 4.
static u32 coff_str(struct coff_strtab *st, const char *s)
{
    u32 len = strlen(s) + 1, at;

    if (!st->size)
        st->size = 4;

    at = st->size;
    if (st->size + len > st->alloc) {
        while (st->size + len > st->alloc)
            st->alloc = st->alloc ? st->alloc * 2 : 256;

        st->data = tli_realloc(st->data, st->alloc);
    }

    memcpy(st->data + at, s, len);
    st->size += len;
    return at;
}

/// Put a section name into its 8-byte field.  A longer one goes to the string
/// table and is referenced as "/<offset>" in ASCII.
static void coff_set_sect_name(u8 dst[8], const char *name,
                               struct coff_strtab *st)
{
    usize len = strlen(name);

    memset(dst, 0, 8);
    if (len <= 8) {
        memcpy(dst, name, len);
    } else {
        char buf[16];

        snprintf(buf, sizeof buf, "/%u", coff_str(st, name));
        len = strlen(buf);
        memcpy(dst, buf, len > 8 ? 8 : len);
    }
}

/// Put a symbol name into its 8-byte field. A longer one goes to the string
/// table too, but symbols spell the reference as four zero bytes followed by
/// the offset, which is not how sections do it.
static void coff_set_sym_name(u8 dst[8], const char *name,
                              struct coff_strtab *st)
{
    usize len = strlen(name);

    memset(dst, 0, 8);
    if (len <= 8) {
        memcpy(dst, name, len);
    } else {
        write32le(dst, 0);
        write32le(dst + 4, coff_str(st, name));
    }
}

/// The alignment field for a byte alignment, which COFF stores as one more
/// than the power of two, capped at the largest the format can name.
static u32 coff_align_bits(i32 align)
{
    i32 a = 0;

    while ((1 << a) < align && a < 13)
        a++;

    return (u32)(a + 1) << 20;
}

/// internal section -> COFF name and characteristics
static void coff_out_name(const tli_Section *s, char name[16], u32 *ch)
{
    u32 c;

    pstrcpy(name, 16, s->name);
    if (s->sh_type == SHT_NOBITS) {
        pstrcpy(name, 16, ".bss");
        c = IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ
            | IMAGE_SCN_MEM_WRITE;
    } else if (s->sh_flags & SHF_EXECINSTR) {
        pstrcpy(name, 16, ".text");
        c = IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ;
    } else if (s->sh_flags & SHF_WRITE) {
        c = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
            | IMAGE_SCN_MEM_WRITE;
    } else if (0 == strncmp(s->name, ".debug", 6)) {
        // Discardable and not loaded: a debugger reads it out of the
        // file, and the image never carries it.
        c = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
            | IMAGE_SCN_MEM_DISCARDABLE;
    } else {
        c = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
    }

    *ch = c | coff_align_bits(s->sh_addralign ? s->sh_addralign : 1);
}

/// Is this a section worth writing out?  The loadable contents are, and so
/// is debug information, which is in the file and never in the image. The
/// linker's own bookkeeping sections have no COFF equivalent.
static i32 coff_out_wanted(const tli_Section *s)
{
    if (s->sh_flags & SHF_ALLOC)
        return s->sh_type == SHT_PROGBITS || s->sh_type == SHT_NOBITS
            || s->sh_type == SHT_INIT_ARRAY || s->sh_type == SHT_FINI_ARRAY;

    // A debug section carries no SHF_ALLOC by design, so testing that
    // alone dropped every one of them from a COFF object and left a
    // build with debug information asked for indistinguishable from one
    // without.
    if (s->sh_type == SHT_PROGBITS && 0 == strncmp(s->name, ".debug", 6))
        return 1;

    return 0;
}

/// The AMD64 half of coff_out_reloc().
static i32 coff_out_amd64(u8 *ptr, i32 elf_type, i64 addend)
{
    switch (elf_type) {
    case 0x1000:
        return IMAGE_REL_AMD64_SECREL;
    case 0x1001:
        return IMAGE_REL_AMD64_SECTION;
    case R_X86_64_64:
        write64le(ptr, (u64)addend);
        return IMAGE_REL_AMD64_ADDR64;
    case R_X86_64_32:
    case R_X86_64_32S:
        write32le(ptr, (u32)addend);
        return IMAGE_REL_AMD64_ADDR32;
    case R_X86_64_PC32:
    case R_X86_64_PLT32:
        write32le(ptr, (u32)(addend + 4));
        return IMAGE_REL_AMD64_REL32;
    default:
        return -1;
    }
}

/// The i386 half of coff_out_reloc().  i386 relocations have no addend field:
/// whatever is in the data already is the addend, and only the four byte
/// difference in convention has to be put back.
static i32 coff_out_i386(u8 *ptr, i32 elf_type)
{
    switch (elf_type) {
    case 0x1000:
        return 0x000B;                  // IMAGE_REL_I386_SECREL
    case 0x1001:
        return 0x000A;                  // IMAGE_REL_I386_SECTION
    case R_386_32:
        return IMAGE_REL_I386_DIR32;
    case R_386_PC32:
    case R_386_PLT32:
        write32le(ptr, read32le(ptr) + 4);
        return IMAGE_REL_I386_REL32;
    default:
        return -1;
    }
}

/// The ARM64 half of coff_out_reloc().
static i32 coff_out_arm64(u8 *ptr, i32 elf_type, i64 addend)
{
    switch (elf_type) {
    case 0x1000:
        return 0x0008;                  // IMAGE_REL_ARM64_SECREL
    case 0x1001:
        return 0x000D;                  // IMAGE_REL_ARM64_SECTION
    case R_AARCH64_ABS64:
        write64le(ptr, (u64)addend);
        return IMAGE_REL_ARM64_ADDR64;
    case R_AARCH64_ABS32:
        write32le(ptr, (u32)addend);
        return IMAGE_REL_ARM64_ADDR32;
    case R_AARCH64_PREL32:
        write32le(ptr, (u32)(addend + 4));
        return IMAGE_REL_ARM64_REL32;
    case R_AARCH64_CALL26:
    case R_AARCH64_JUMP26:
        write32le(ptr, (read32le(ptr) & 0xfc000000)
                       | ((u32)(addend >> 2) & 0x03ffffff));
        return IMAGE_REL_ARM64_BRANCH26;
    case R_AARCH64_ADR_PREL_PG_HI21: {
        u32 v = (u32)addend & 0x1fffff;

        write32le(ptr, (read32le(ptr) & 0x9f00001f)
                       | ((v & 3) << 29) | ((v >> 2) << 5));
        return IMAGE_REL_ARM64_PAGEBASE_REL21;
    }
    case R_AARCH64_ADD_ABS_LO12_NC:
        write32le(ptr, (read32le(ptr) & ~(0xfffu << 10))
                       | (((u32)addend & 0xfff) << 10));
        return IMAGE_REL_ARM64_PAGEOFFSET_12A;
    case R_AARCH64_LDST8_ABS_LO12_NC:
    case R_AARCH64_LDST16_ABS_LO12_NC:
    case R_AARCH64_LDST32_ABS_LO12_NC:
    case R_AARCH64_LDST64_ABS_LO12_NC:
    case R_AARCH64_LDST128_ABS_LO12_NC: {
        u32 insn = read32le(ptr);
        i32 shift = elf_type - R_AARCH64_LDST8_ABS_LO12_NC;

        // The 128-bit form is out of order in the ELF numbering, so its shift
        // does not follow from the type.
        if (elf_type == R_AARCH64_LDST128_ABS_LO12_NC)
            shift = 4;

        write32le(ptr, (insn & ~(0xfffu << 10))
                       | ((((u32)addend >> shift) & 0xfff) << 10));
        return IMAGE_REL_ARM64_PAGEOFFSET_12L;
    }
    case R_AARCH64_TLSLE_ADD_TPREL_HI12:
        write32le(ptr, (read32le(ptr) & ~(0xfffu << 10))
                       | ((((u32)addend >> 12) & 0xfff) << 10));
        return IMAGE_REL_ARM64_SECREL_HIGH12A;
    case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
        write32le(ptr, (read32le(ptr) & ~(0xfffu << 10))
                       | (((u32)addend & 0xfff) << 10));
        return IMAGE_REL_ARM64_SECREL_LOW12A;
    default:
        return -1;
    }
}

/// Translate one internal relocation, writing the addend into the field the
/// way COFF carries it.  Returns the COFF type, or -1 if there is none.
///
/// The two COFF-only kinds arrive as markers, 0x1000 and 0x1001, since no ELF
/// relocation means what they mean.  Both leave the field alone: the linker
/// writes the offset or the section number into it.
static i32 coff_out_reloc(tli_State *s1, u8 *ptr, i32 elf_type, i64 addend)
{
    // PE is refused for any machine but these three.
    switch (s1->arch->em) {
    case EM_X86_64:
        return coff_out_amd64(ptr, elf_type, addend);
    case EM_386:
        return coff_out_i386(ptr, elf_type);
    case EM_AARCH64:
        return coff_out_arm64(ptr, elf_type, addend);
    }
    return -1;
}

/// Write everything linked so far back out as one COFF relocatable object,
/// which is what `-r` means on a PE target.
ST_FUNC i32 coff_output_obj(tli_State *s1, const char *filename)
{
    struct coff_osect *os;
    struct coff_strtab strt = {0};
    u8 *symtab = NULL;
    i32 *sym_map;
    IMAGE_FILE_HEADER fh;
    u64 off;
    i32 i, j, n = 0, nsyms_in, nsyms = 0, ret = -1;
    FILE *fp;

    // Pick the sections to write and give each one a header, which fixes the
    // 1-based numbering everything below refers to them by.
    os = tli_mallocz(sizeof *os * s1->nb_sections);
    for (i = 1; i < s1->nb_sections; i++) {
        tli_Section *s = s1->sections[i];
        char name[16];
        u32 ch;

        if (!coff_out_wanted(s))
            continue;

        os[n].s = s;
        coff_out_name(s, name, &ch);
        coff_set_sect_name(os[n].sh.Name, name, &strt);
        os[n].sh.Characteristics = ch;
        os[n].sh.SizeOfRawData = s->data_offset;
        n++;
    }

    // One symbol per section first, so relocations against a section have
    // something to name, then the real symbols.
    nsyms_in = symtab_section->data_offset / sizeof(ElfW(Sym));
    symtab = tli_mallocz((usize)(n + nsyms_in) * COFF_SYM_SIZE);
    sym_map = tli_mallocz(sizeof *sym_map * (nsyms_in ? nsyms_in : 1));

    for (i = 0; i < n; i++) {
        u8 *p = symtab + (usize)nsyms * COFF_SYM_SIZE;
        char name[16];
        u32 ch;

        coff_out_name(os[i].s, name, &ch);
        coff_set_sym_name(p, name, &strt);
        write32le(p + 8, 0);
        write16le(p + 12, i + 1);
        write16le(p + 14, 0);
        p[16] = IMAGE_SYM_CLASS_STATIC;
        p[17] = 0;
        os[i].sym = nsyms++;
    }

    for (i = 1; i < nsyms_in; i++) {
        ElfW(Sym) *es = &((ElfW(Sym) *)symtab_section->data)[i];
        const char *name = (char *)symtab_section->link->data + es->st_name;
        u8 *p;
        i32 sec;
        u32 value;

        if (ELFW(ST_TYPE)(es->st_info) == STT_SECTION || !name[0])
            continue;

        // Translate the ELF section number into a 1-based index into the
        // sections actually being written, or one of the COFF specials.
        if (es->st_shndx == SHN_UNDEF) {
            sec = IMAGE_SYM_UNDEFINED, value = 0;
        } else if (es->st_shndx == SHN_COMMON) {
            sec = IMAGE_SYM_UNDEFINED, value = es->st_size;
        } else if (es->st_shndx == SHN_ABS) {
            sec = IMAGE_SYM_ABSOLUTE, value = es->st_value;
        } else {
            for (j = 0; j < n; j++)
                if (os[j].s == s1->sections[es->st_shndx])
                    break;

            if (j == n)
                continue; // defined in a section we are not writing

            sec = j + 1, value = es->st_value;
        }

        p = symtab + (usize)nsyms * COFF_SYM_SIZE;
        coff_set_sym_name(p, name, &strt);
        write32le(p + 8, value);
        write16le(p + 12, (u16)(i16)sec);
        write16le(p + 14, ELFW(ST_TYPE)(es->st_info) == STT_FUNC
                          ? (IMAGE_SYM_DTYPE_FUNCTION << 4) : 0);

        // A weak symbol goes out as an ordinary external: COFF spells weakness
        // with an auxiliary record naming the fallback, which nothing here has.
        p[16] = ELFW(ST_BIND)(es->st_info) == STB_LOCAL
                ? IMAGE_SYM_CLASS_STATIC : IMAGE_SYM_CLASS_EXTERNAL;
        p[17] = 0;
        sym_map[i] = nsyms++;
    }

    // Relocations.
    for (i = 0; i < n; i++) {
        tli_Section *sr = os[i].s->reloc;
        ElfW_Rel *r;

        if (!sr || os[i].s->sh_type == SHT_NOBITS)
            continue;

        os[i].rel = tli_mallocz((usize)(sr->data_offset / sizeof *r + 1)
                                * COFF_RELOC_SIZE);

        for_each_elem(sr, 0, r, ElfW_Rel) {
            i32 elf_type = ELFW(R_TYPE)(r->r_info);
            i32 es = ELFW(R_SYM)(r->r_info);
            ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[es];
            i64 addend = 0;
            u8 *re;
            i32 idx, ctype;

#if SHT_RELX == SHT_RELA
            addend = r->r_addend;
#endif
            // A relocation against a section becomes one against that
            // section's own symbol, with the offset folded into the addend.
            if (ELFW(ST_TYPE)(sym->st_info) == STT_SECTION) {
                for (j = 0; j < n; j++)
                    if (os[j].s == s1->sections[sym->st_shndx])
                        break;

                if (j == n)
                    continue;

                idx = os[j].sym;
                addend += sym->st_value;
            } else {
                idx = sym_map[es];

                if (!idx && es)
                    continue;
            }

            ctype = coff_out_reloc(s1, os[i].s->data + r->r_offset, elf_type,
                                   addend);
            if (ctype < 0) {
                tli_error_noabort("cannot express relocation type %d in COFF",
                                  elf_type);
                goto done;
            }

            re = os[i].rel + (usize)os[i].nreloc++ * COFF_RELOC_SIZE;
            write32le(re, r->r_offset);
            write32le(re + 4, idx);
            write16le(re + 8, ctype);
        }
    }

    // Lay the file out.
    off = sizeof fh + (u64)n * sizeof(IMAGE_SECTION_HEADER);
    for (i = 0; i < n; i++) {
        if (os[i].s->sh_type == SHT_NOBITS || !os[i].sh.SizeOfRawData) {
            os[i].sh.PointerToRawData = 0;
            continue;
        }

        off = (off + 3) & ~3ul;
        os[i].sh.PointerToRawData = off;
        off += os[i].sh.SizeOfRawData;
    }

    for (i = 0; i < n; i++) {
        if (!os[i].nreloc)
            continue;

        os[i].sh.PointerToRelocations = off;
        os[i].sh.NumberOfRelocations = os[i].nreloc;
        off += (u64)os[i].nreloc * COFF_RELOC_SIZE;
    }

    // The file header, which in an object has no optional header after it and
    // says so with a zero size.
    memset(&fh, 0, sizeof fh);
    fh.Machine = IMAGE_FILE_MACHINE;
    fh.NumberOfSections = n;
    fh.PointerToSymbolTable = off;
    fh.NumberOfSymbols = nsyms;
    fh.SizeOfOptionalHeader = 0;

    unlink(filename);
    fp = fopen(filename, "wb");
    if (!fp) {
        tli_error_noabort("could not write '%s': %s", filename, strerror(errno));
        goto done;
    }

    fwrite(&fh, sizeof fh, 1, fp);
    for (i = 0; i < n; i++) {

        // NOBITS sections carry their size in VirtualSize, not raw data
        if (os[i].s->sh_type == SHT_NOBITS) {
            os[i].sh.Misc.VirtualSize = os[i].sh.SizeOfRawData;
            os[i].sh.SizeOfRawData = 0;
        }

        fwrite(&os[i].sh, sizeof os[i].sh, 1, fp);
    }

    // Contents, then relocations, each seeking to the offset its header
    // already promised.
    for (i = 0; i < n; i++) {
        if (!os[i].sh.PointerToRawData)
            continue;

        fseek(fp, os[i].sh.PointerToRawData, SEEK_SET);
        fwrite(os[i].s->data, os[i].sh.SizeOfRawData, 1, fp);
    }

    for (i = 0; i < n; i++) {
        if (!os[i].nreloc)
            continue;

        fseek(fp, os[i].sh.PointerToRelocations, SEEK_SET);
        fwrite(os[i].rel, COFF_RELOC_SIZE, os[i].nreloc, fp);
    }

    // The symbols, then the string table, whose first word is its own length
    // even when it holds nothing else.
    fseek(fp, fh.PointerToSymbolTable, SEEK_SET);
    fwrite(symtab, COFF_SYM_SIZE, nsyms, fp);
    if (strt.size >= 4) {
        write32le((u8 *)strt.data, strt.size);
        fwrite(strt.data, strt.size, 1, fp);
    } else {
        u8 four[4];

        write32le(four, 4);
        fwrite(four, 4, 1, fp);
    }

    fclose(fp);

    ret = s1->nb_errors ? -1 : 0;

done:
    for (i = 0; i < n; i++)
        tli_free(os[i].rel);

    tli_free(os);
    tli_free(symtab);
    tli_free(sym_map);
    tli_free(strt.data);
    return ret;
}


/// ---- src/core/tl_macho_out.c ----

/// tinylinker - Mach-O image writer
///
/// Writes the linked sections out as a macOS executable or dynamic library on
/// x86-64 and arm64, and reads back the one thing a link needs from outside
/// the object files: the list of names an installed dylib exports, either from
/// the dylib itself or from the text stub (.tbd) a modern SDK ships instead.
///
/// A Mach-O image is a header followed by a list of variable-length load
/// commands, and the loader acts on nothing else.  LC_SEGMENT_64 carves the
/// file into segments and names the sections inside them; LC_SYMTAB and
/// LC_DYSYMTAB describe the symbol table; LC_MAIN gives the entry point;
/// LC_DYLD_CHAINED_FIXUPS and LC_DYLD_EXPORTS_TRIE point at the two blobs dyld
/// reads to bind imports and to answer lookups from other images.  Those blobs
/// live in __LINKEDIT, a segment that has no sections of its own: everything
/// in it is addressed by file offset from a load command instead.
///
/// Three things about the platform shape what follows.
///
/// An image is always position independent, so every pointer it stores has to
/// be either slid by the load address (a "rebase") or written by dyld from a
/// symbol name (a "bind").  The encoding for both is a chain threaded through
/// the pointer words themselves: each one carries the distance to the next in
/// its own page, so the fixup table proper is only a list of where each page's
/// chain starts.  That is why the pointers have to be laid out and sorted
/// before the table can be written, and why a pointer may not straddle a page.
///
/// There is no counterpart to .fini_array.  A destructor is registered at run
/// time with __cxa_atexit rather than listed for the loader, so the array is
/// turned into a small function that registers every entry, and that function
/// is added to .init_array, which does have a counterpart.
///
/// And arm64 macOS will not execute an unsigned page, so every image ends in
/// an ad-hoc code signature: a hash of each of its own pages and nothing else.
/// Writing it here rather than shelling out to codesign(1) is what lets a host
/// that is not macOS produce something macOS will run.


/// XXX: this file uses tli_error() to the effect of exit(1)
#undef _tcc_error

/// ---------------------------------------------------------------------------
/// The format.  Names and layouts come from <mach-o/loader.h>, <mach-o/nlist.h>
/// and dyld's <mach-o/fixup-chains.h>; the relocatable-object reader in
/// tl_macho_obj.c shares the declarations below, so they are all here whether
/// or not the writer itself uses them.

/// mach_header_64 is the 32-bit header plus a padding word, and the pair is
/// spelled that way here so that the fields have one name in both.
struct mach_header {
    u32 magic;
    i32 cputype;
    i32 cpusubtype;
    u32 filetype;
    u32 ncmds;
    u32 sizeofcmds;
    u32 flags;
};

/// The 64-bit header: the same fields, plus the word that pads them out
/// to eight-byte alignment for what follows.
struct mach_header_64 {
    struct mach_header mh;
    u32                reserved;
};

#define MH_MAGIC_64     0xfeedfacf

#define MH_EXECUTE      0x2
#define MH_DYLIB        0x6

#define MH_DYLDLINK     0x4         /// The image is linked against dyld.
#define MH_PIE          0x200000    /// It may be loaded at any address.
/// Set when the image holds thread-local variable descriptors.  dyld tests this
/// bit before it goes looking for them, so an image that has them and does not
/// say so keeps _tlv_bootstrap in every descriptor, and the first thread-local
/// read calls it and aborts.
#define MH_HAS_TLV_DESCRIPTORS 0x800000

/// A fat file is a directory of images for several architectures, and is the
/// shape the system libraries used to ship in.
struct fat_header {
    u32 magic;
    u32 nfat_arch;
};

/// One member of a fat file: which architecture, and where in the file its
/// image begins.
struct fat_arch {
    i32 cputype;
    i32 cpusubtype;
    u32 offset;
    u32 size;
    u32 align;
};

#define FAT_MAGIC       0xcafebabe
#define FAT_CIGAM       0xbebafeca  /// The same, byte swapped.
#define FAT_MAGIC_64    0xcafebabf
#define FAT_CIGAM_64    0xbfbafeca

#define CPU_ARCH_ABI64          0x01000000
#define CPU_TYPE_X86            7
#define CPU_TYPE_X86_64         (CPU_TYPE_X86 | CPU_ARCH_ABI64)
#define CPU_TYPE_ARM            12
#define CPU_TYPE_ARM64          (CPU_TYPE_ARM | CPU_ARCH_ABI64)

#define CPU_SUBTYPE_LIB64       0x80000000
#define CPU_SUBTYPE_X86_ALL     3
#define CPU_SUBTYPE_ARM64_ALL   0

/// Load commands.  Every one starts with this pair, so a reader can walk the
/// list without understanding any of them.
struct load_command {
    u32 cmd;
    u32 cmdsize;
};

/// Set on a command the loader must understand or refuse the file.
#define LC_REQ_DYLD             0x80000000

#define LC_SYMTAB               0x2
#define LC_DYSYMTAB             0xb
#define LC_LOAD_DYLIB           0xc
#define LC_ID_DYLIB             0xd
#define LC_LOAD_DYLINKER        0xe
#define LC_SEGMENT_64           0x19
#define LC_RPATH                (0x1c | LC_REQ_DYLD)
#define LC_CODE_SIGNATURE       0x1d
#define LC_REEXPORT_DYLIB       (0x1f | LC_REQ_DYLD)
#define LC_MAIN                 (0x28 | LC_REQ_DYLD)
#define LC_SOURCE_VERSION       0x2a
#define LC_BUILD_VERSION        0x32
#define LC_DYLD_EXPORTS_TRIE    (0x33 | LC_REQ_DYLD)
#define LC_DYLD_CHAINED_FIXUPS  (0x34 | LC_REQ_DYLD)

/// The protection bits a segment is mapped with.
typedef i32 vm_prot_t;

#define VM_PROT_READ    1
#define VM_PROT_WRITE   2
#define VM_PROT_EXECUTE 4

/// LC_SEGMENT_64: a run of the file mapped as one, with its section headers
/// following the command itself.
struct segment_command_64 {
    u32       cmd;
    u32       cmdsize;   /// This struct plus nsects section_64 headers.
    char      segname[16];
    u64       vmaddr;
    u64       vmsize;
    u64       fileoff;
    u64       filesize;  /// May be less than vmsize: the rest is zeroed.
    vm_prot_t maxprot;
    vm_prot_t initprot;
    u32       nsects;
    u32       flags;
};

#define SG_READ_ONLY    0x10    /// Made read-only once the fixups are applied.

/// One section inside a segment.  __LINKEDIT has none: what is in it is
/// addressed by load commands instead.
struct section_64 {
    char sectname[16];
    char segname[16];
    u64  addr;
    u64  size;
    u32  offset;
    u32  align;      /// A power of two, stored as its log.
    u32  reloff;
    u32  nreloc;
    u32  flags;      /// A type in the low byte, attributes above.
    u32  reserved1;  /// Indirect symbol index, for the pointer types.
    u32  reserved2;  /// Stub size, for S_SYMBOL_STUBS.
    u32  reserved3;
};

/// The mask over the type in a section's flags; everything above it is an
/// attribute.
#define SECTION_TYPE                    0x000000ff

/// section types
#define S_REGULAR                       0x0
#define S_ZEROFILL                      0x1
#define S_NON_LAZY_SYMBOL_POINTERS      0x6
#define S_SYMBOL_STUBS                  0x8
#define S_MOD_INIT_FUNC_POINTERS        0x9
#define S_MOD_TERM_FUNC_POINTERS        0xa
/// The thread-local types.  dyld finds a program's thread-local storage by
/// these and by nothing else: the descriptors it has to rewrite are whatever
/// sits in a section typed S_THREAD_LOCAL_VARIABLES, and the block it copies
/// for each thread is the run of S_THREAD_LOCAL_REGULAR and
/// S_THREAD_LOCAL_ZEROFILL sections, taken as one span from the first to the
/// last.  tli_Section names carry no meaning here; only these bytes do.
///
/// There is a fourth, S_THREAD_LOCAL_VARIABLE_POINTERS, for the table a
/// reference reaches a descriptor through.  It is not written: those pointers
/// go in __got beside every other one, which dyld rebases without needing to
/// be told what they point at.
#define S_THREAD_LOCAL_REGULAR          0x11
#define S_THREAD_LOCAL_ZEROFILL         0x12
#define S_THREAD_LOCAL_VARIABLES        0x13

/// section attributes
#define S_ATTR_PURE_INSTRUCTIONS        0x80000000
#define S_ATTR_SOME_INSTRUCTIONS        0x00000400
#define S_ATTR_DEBUG                    0x02000000

/// A string inside a load command is stored as its byte offset from the start
/// of the command, and the command is padded out to cover it.
typedef u32 lc_str;

/// A library this image is, loads, or passes the exports of on.
struct dylib_command {
    u32    cmd;  /// LC_ID_DYLIB, LC_LOAD_DYLIB, LC_REEXPORT_DYLIB.
    u32    cmdsize;
    lc_str name;
    u32    timestamp;
    u32    current_version;
    u32    compatibility_version;
};

/// The loader itself, which an executable names and a library does not.
struct dylinker_command {
    u32    cmd;  /// LC_LOAD_DYLINKER.
    u32    cmdsize;
    lc_str name;
};

/// One directory dyld searches for the libraries named above.
struct rpath_command {
    u32    cmd;  /// LC_RPATH.
    u32    cmdsize;
    lc_str path;
};

/// The shape every command that just points at a blob in __LINKEDIT takes.
struct linkedit_data_command {
    u32 cmd;
    u32 cmdsize;
    u32 dataoff;
    u32 datasize;
};

/// The platforms LC_BUILD_VERSION can name, from <mach-o/loader.h>.  The
/// loader compares the one below against what it is, so an image stamped for
/// the wrong one is refused before a single instruction of it runs.  5 is
/// bridgeOS and is left out.
#define PLATFORM_MACOS              1
#define PLATFORM_IOS                2
#define PLATFORM_TVOS               3
#define PLATFORM_WATCHOS            4
#define PLATFORM_MACCATALYST        6
#define PLATFORM_IOSSIMULATOR       7
#define PLATFORM_TVOSSIMULATOR      8
#define PLATFORM_WATCHOSSIMULATOR   9

/// What the image was built for, which is how the loader decides whether it
/// can run it at all.
struct build_version_command {
    u32 cmd;    /// LC_BUILD_VERSION.
    u32 cmdsize;
    u32 platform;
    u32 minos;  /// X.Y.Z packed as nibbles xxxx.yy.zz.
    u32 sdk;
    u32 ntools;
};

/// What it was built from.  Nothing here has a version to declare, but the
/// command is written because tools expect it.
struct source_version_command {
    u32 cmd;  /// LC_SOURCE_VERSION.
    u32 cmdsize;
    u64 version;
};

/// LC_MAIN: where an executable starts, and how much stack it asks for.
struct entry_point_command {
    u32 cmd;       /// LC_MAIN.
    u32 cmdsize;
    u64 entryoff;  /// From the start of __TEXT, not an address.
    u64 stacksize;
};

/// LC_SYMTAB: where the symbol table and its strings are in the file.
struct symtab_command {
    u32 cmd;  /// LC_SYMTAB.
    u32 cmdsize;
    u32 symoff;
    u32 nsyms;
    u32 stroff;
    u32 strsize;
};

/// The symbol table is three consecutive runs, and this command says where each
/// begins.  Most of the rest is for the static linker's own bookkeeping and is
/// left empty; the indirect symbol table is not, because the loader uses it to
/// say which import each stub and each GOT slot stands for.
struct dysymtab_command {
    u32 cmd;  /// LC_DYSYMTAB.
    u32 cmdsize;
    u32 ilocalsym,      nlocalsym;
    u32 iextdefsym,     nextdefsym;
    u32 iundefsym,      nundefsym;
    u32 tocoff,         ntoc;
    u32 modtaboff,      nmodtab;
    u32 extrefsymoff,   nextrefsyms;
    u32 indirectsymoff, nindirectsyms;
    u32 extreloff,      nextrel;
    u32 locreloff,      nlocrel;
};

/// An indirect symbol table entry that stands for something with no name.
#define INDIRECT_SYMBOL_LOCAL   0x80000000

/// One symbol table entry.
struct nlist_64 {
    u32 n_strx;
    u8  n_type;
    u8  n_sect;  /// 1-based across the whole image, or NO_SECT.
    u16 n_desc;
    u64 n_value;
};

#define N_UNDF  0x0
#define N_ABS   0x2
#define N_SECT  0xe
#define N_EXT   0x1

#define N_WEAK_REF      0x0040
#define N_WEAK_DEF      0x0080

/// Instead of naming a dylib, an import may say how to look the name up.  -2 is
/// a flat search of every image loaded, which is what this linker uses: it
/// never records which library a symbol came from.
#define BIND_SPECIAL_DYLIB_FLAT_LOOKUP  (-2)

/// Export trie terminal flags.
#define EXPORT_SYMBOL_FLAGS_KIND_REGULAR        0x00
#define EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION     0x04

/// ---- chained fixups ----
///
/// The blob is a header, a table saying where each segment's per-page chain
/// starts, and a list of imported names.  Everything else is in the pointers.

/// The head of the fixup blob: an offset to each of the three things behind
/// it, all counted from this header.
struct dyld_chained_fixups_header {
    u32 fixups_version; /// Always 0; no other version exists.
    u32 starts_offset;  /// To a dyld_chained_starts_in_image.
    u32 imports_offset; /// To the import array.
    u32 symbols_offset; /// To the pool the imports name.
    u32 imports_count;  /// Entries in that array.
    u32 imports_format; /// DYLD_CHAINED_IMPORT, which is one word each.
    u32 symbols_format; /// 0: the pool is plain text.
};

/// imports_format: one 32-bit word per import, packed as
/// lib_ordinal:8, weak_import:1, name_offset:23.
#define DYLD_CHAINED_IMPORT     1

/// Where each segment's chain table is, or that it has none.
struct dyld_chained_starts_in_image {
    u32 seg_count;
    /// One per segment, in load command order, counted from starts_offset.
    /// Zero means the segment holds no fixups at all.
    u32 seg_info_offset[1];
};

/// One segment's chain table: a header, then where the chain starts on each
/// of its pages.
struct dyld_chained_starts_in_segment {
    u32 size;              /// Of this struct, page_start included.
    u16 page_size;         /// What a page is here, not what the host runs on.
    u16 pointer_format;    /// How a chained pointer in this segment is packed.
    u64 segment_offset;    /// Of the segment from the image's base.
    u32 max_valid_pointer; /// 32-bit images only.
    u16 page_count;        /// Entries in the array below.
    u16 page_start[1];     /// Offset of the first fixup in each page.
};

/// The one pointer format written here: the target is a vmaddr rather than an
/// offset from the image's base.
#define DYLD_CHAINED_PTR_64             2

#define DYLD_CHAINED_PTR_START_NONE     0xffff  /// No fixups on this page.

/// Both kinds of chained pointer put the step to the next one at bit 51,
/// counted in four-byte units, and the flag saying which kind at bit 63.
/// A rebase then holds target:36 and high8:8 from bit 0; a bind holds
/// ordinal:24 and addend:8.  The words are assembled by hand rather than
/// through bitfields, whose order in a word no standard fixes.
#define CHAIN_NEXT_SHIFT        51
#define CHAIN_NEXT_MAX          0xfff
#define CHAIN_IS_BIND           ((u64)1 << 63)

#define CHAIN_TARGET_MASK       0xfffffffffull   /// 36 bits: 64GB of image.
#define CHAIN_HIGH8_MASK        0xff00000000000000ull

#define CHAIN_REBASE(target) \
    (((u64)(target) & CHAIN_TARGET_MASK) \
     | ((((u64)(target) & CHAIN_HIGH8_MASK) >> 56) << 36))
#define CHAIN_BIND(ordinal, addend) \
    (CHAIN_IS_BIND | ((u64)(ordinal) & 0xffffff) \
     | (((u64)(addend) & 0xff) << 24))

/// The page the chain table is written in terms of.  arm64 macOS runs on 16KiB
/// pages and an image built for them loads on 4KiB hardware too, so this is the
/// larger of the two whatever the architecture.
#define SEG_PAGE_SIZE   16384

/// ---- code signing ----
///
/// A signature is a blob at the end of the file, outside every segment but
/// __LINKEDIT, holding the SHA-256 of each page of everything in front of it.
/// Ad-hoc means it says nothing about who produced the image: there is no
/// certificate and nothing to check the hashes against but the file itself, so
/// all it proves is that the pages were not altered after linking.  That is
/// still what arm64 macOS insists on before it will map a page executable.
///
/// None of this is in the SDK's headers.  The names come from Apple's Security
/// sources (cs_blobs.h); the layout below was worked out against what
/// codesign(1) writes, read with otool and a hex dump.
///
/// The one trap is the byte order: every field in every signature blob is big
/// endian, where the rest of Mach-O is host order.  The structs below are
/// therefore a description of the layout and nothing more; every field is
/// stored by offset, through macho_put_be32, rather than assigned.

#define CSMAGIC_EMBEDDED_SIGNATURE  0xfade0cc0
#define CSMAGIC_CODEDIRECTORY       0xfade0c02

#define CSSLOT_CODEDIRECTORY    0

#define CS_ADHOC                0x0002  /// No certificate, hashes only.
#define CS_LINKER_SIGNED        0x20000 /// The linker signed it, not codesign.

#define CS_HASHTYPE_SHA256      2
#define CS_HASH_SIZE            32

/// The signature hashes 4KiB pages whatever the architecture's own page size
/// is, which is what Apple's linker does; the kernel takes the shift from the
/// directory rather than assuming either.
#define CS_PAGE_SHIFT           12
#define CS_PAGE_SIZE            (1 << CS_PAGE_SHIFT)

/// Set on the executable segment of a main binary, which is what Apple's linker
/// does and what codesign(1) reports back as flags=0x1.  A dylib gets nothing:
/// it is loaded, not started.
#define CS_EXECSEG_MAIN_BINARY  1

/// The blob everything else hangs off: a length, a count, and that many
/// (type, offset) pairs, each offset counted from the SuperBlob itself.  An
/// ad-hoc signature needs only the code directory, so count is one; codesign(1)
/// adds an empty requirements blob, which nothing reads back.
struct cs_super_blob {
    u32 magic;
    u32 length;
    u32 count;
    /// struct cs_blob_index index[count];
};

/// One entry of the SuperBlob's index: what the blob is, and where.
struct cs_blob_index {
    u32 type;
    u32 offset;
};

/// The code directory proper.  hash_offset and ident_offset are counted from
/// the start of this struct, not from the SuperBlob.  hash_offset points at
/// code slot zero: the special slots, if there are any, are the n_special
/// hashes immediately in front of it, addressed backwards as -1, -2 and so on.
/// An ad-hoc signature from a linker has none of the things they stand for, so
/// n_special is zero and the array is code slots alone.
struct cs_code_directory {
    u32 magic;
    u32 length;          /// Of the directory, this header included.
    u32 version;
    u32 flags;
    u32 hash_offset;
    u32 ident_offset;
    u32 n_special;
    u32 n_code;          /// Pages of the file that are hashed.
    u32 code_limit;      /// Where the signature itself begins.
    u8  hash_size;
    u8  hash_type;
    u8  platform;        /// 0 unless the image is part of the OS.
    u8  page_shift;      /// Log2 of the page the hashes cover.
    u32 spare2;
    /// version 0x20100
    u32 scatter_offset;
    /// version 0x20200
    u32 team_offset;
    /// version 0x20300
    u32 spare3;
    u64 code_limit64;    /// For an image above 4GB; code_limit if not.
    /// version 0x20400
    u64 exec_seg_base;   /// File offset of the executable segment.
    u64 exec_seg_limit;  /// And how much of the file it covers.
    u64 exec_seg_flags;
};

/// The version says how much of the struct above is present.  0x20400 is the
/// oldest that carries the executable segment, and the fields are cheap, so it
/// is what is written rather than the shorter forms.
#define CS_SUPPORTS_EXEC_SEG    0x20400

/// ---------------------------------------------------------------------------
/// Internal section types this file gives to sections that exist only to be
/// placed in the output, and the section number an import gets once it is known
/// to come from a dylib rather than to be missing.

#define SHT_LINKEDIT (SHT_LOOS + 42)
#define SHN_FROMDLL  (SHN_LOOS + 2)

/// ---------------------------------------------------------------------------
/// Reading a dylib, or the text stub that stands in for one.
///
/// Only the exported names matter: they go into dynsymtab_section, which is
/// what tells the rest of the linker that an undefined symbol will be found at
/// run time rather than being an error.  Nothing about the library is recorded
/// beyond its install name, because imports are bound by a flat search.

/// Reverse a word's bytes.  Only a fat header needs it: everything else in a
/// Mach-O file is written in the host's own order.
static u32 macho_bswap32(u32 x)
{
    return (x >> 24) | ((x >> 8) & 0xff00) | ((x & 0xff00) << 8) | (x << 24);
}

/// Take the names an installed dylib exports and hand them to the linker as
/// symbols that will be found at run time.  'lev' is how many re-exports deep
/// this library was reached from, and is 0 for one named on the command line.
ST_FUNC i32 macho_load_dll(tli_State *s1, i32 fd, const char *filename, i32 lev)
{
    u8 head[sizeof(struct mach_header_64)];
    struct fat_header fh;
    struct mach_header mh;
    struct load_command *lc;
    char *cmds = NULL, *strtab = NULL;
    struct nlist_64 *symtab = NULL;
    u32 base = 0, nsyms = 0, strsize = 0, iextdef = 0, nextdef = 0;
    const char *soname = filename;
    u32 i, at;
    i32 want_cpu = s1->arch->macho_cputype, swapped = 0;

again:
    if (full_read(fd, head, sizeof head) != sizeof head)
        return -1;

    memcpy(&fh, head, sizeof fh);

    // A fat file is a directory of images, big-endian on disk whichever way
    // round the host is.  Find this architecture's member and start over at
    // the offset it gives.
    if (fh.magic == FAT_MAGIC || fh.magic == FAT_CIGAM) {
        struct fat_arch *fa;
        u32 n;

        swapped = fh.magic == FAT_CIGAM;
        n = swapped ? macho_bswap32(fh.nfat_arch) : fh.nfat_arch;
        fa = load_data(fd, sizeof fh, n * sizeof *fa);
        for (i = 0; i < n; i++) {
            u32 cpu = swapped ? macho_bswap32(fa[i].cputype)
                              : (u32)fa[i].cputype;
            if (cpu == (u32)want_cpu)
                break;
        }

        if (i == n) {
            tli_free(fa);
            return -1;
        }

        base = swapped ? macho_bswap32(fa[i].offset) : fa[i].offset;
        tli_free(fa);
        lseek(fd, base, SEEK_SET);
        goto again;
    }

    if (fh.magic == FAT_MAGIC_64 || fh.magic == FAT_CIGAM_64) {
        tli_warning("%s: 64-bit fat Mach-O files are not read", filename);
        return -1;
    }

    memcpy(&mh, head, sizeof mh);
    if (mh.magic != MH_MAGIC_64)
        return -1;

    // Walk the load commands for the symbol table, the install name, and any
    // library whose exports this one passes on as its own.
    cmds = load_data(fd, base + sizeof(struct mach_header_64), mh.sizeofcmds);
    for (i = 0, at = 0; i < mh.ncmds; i++, at += lc->cmdsize) {
        lc = (struct load_command *)(cmds + at);

        // ncmds and sizeofcmds are two separate claims about the same list;
        // believe whichever runs out first.
        if (at + sizeof *lc > mh.sizeofcmds || lc->cmdsize < sizeof *lc
            || at + lc->cmdsize > mh.sizeofcmds) {
            tli_warning("%s: malformed Mach-O load commands", filename);
            break;
        }
        switch (lc->cmd) {
        case LC_SYMTAB: {
            struct symtab_command *sc = (struct symtab_command *)lc;
            nsyms = sc->nsyms;
            strsize = sc->strsize;
            symtab = load_data(fd, base + sc->symoff, nsyms * sizeof *symtab);
            strtab = load_data(fd, base + sc->stroff, strsize);
            break;
        }
        case LC_DYSYMTAB: {
            // The externally defined run is exactly the export list.
            struct dysymtab_command *dc = (struct dysymtab_command *)lc;
            iextdef = dc->iextdefsym;
            nextdef = dc->nextdefsym;
            break;
        }
        case LC_ID_DYLIB:
            soname = (char *)lc + ((struct dylib_command *)lc)->name;
            break;
        case LC_REEXPORT_DYLIB: {
            // Names the library re-exports count as its own.  There is no
            // guard against a cycle here; the system libraries do not form
            // one.
            char *name = (char *)lc + ((struct dylib_command *)lc)->name;
            i32 sub = open(name, O_RDONLY | O_BINARY);

            if (sub < 0)
                tli_warning("can't open %s (reexported from %s)",
                            name, filename);
            else {
                macho_load_dll(s1, sub, name, lev + 1);
                close(sub);
            }
            break;
        }
        }
    }

    if (tli_add_dllref(s1, soname, lev)->found)
        goto done;

    if (!nsyms || !nextdef)
        tli_warning("%s doesn't export any symbols?", filename);

    // The externally defined run of the symbol table is the export list, and
    // an undefined symbol of that name is what the rest of the linker reads as
    // "this will be bound at run time".
    for (i = iextdef; i < iextdef + nextdef && i < nsyms; i++) {
        if (symtab[i].n_strx >= strsize)
            continue;
        set_elf_sym(s1->dynsymtab_section, 0, 0,
                    ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0, SHN_UNDEF,
                    strtab + symtab[i].n_strx);
    }

done:
    tli_free(strtab);
    tli_free(symtab);
    tli_free(cmds);
    return 0;
}

/// A .tbd is YAML, but only two keys are wanted out of it and both are written
/// as flow sequences on one line, so scanning for them beats a parser.  The
/// scanners below chew up the buffer as they go, planting terminators.

/// Move 'pos' past the next occurrence of the literal 's', or to NULL.
#define tbd_seek(s)     (pos = (pos = strstr(pos, s)) ? pos + strlen(s) : NULL)

/// Move 'pos' to the next of the characters in 'c', or to NULL.
#define tbd_seek_any(c) (pos = strpbrk(pos, c))

/// Step over the spaces and newlines a flow sequence may be broken across.
#define tbd_skip_ws     while (*pos == ' ' || *pos == '\n') ++pos

/// Terminate the token that ends here and step past the byte it took.
#define tbd_cut         *pos++ = 0

/// The same, for the quote or the space a token may be delimited by.
#define tbd_cut_quote   if (*pos == '\'' || *pos == '"') tbd_cut
#define tbd_cut_space   if (*pos == ' ') tbd_cut

/// The install name, which is the library's identity and the key the rest of
/// the linker deduplicates dylib references on.
static char *macho_tbd_name(char **ppos)
{
    char *pos = *ppos, *name;

    if (!tbd_seek("install-name: "))
        return NULL;

    tbd_skip_ws;
    tbd_cut_quote;
    name = pos;
    if (!tbd_seek_any("\n \"'"))
        return NULL;

    tbd_cut;
    *ppos = pos;
    return name;
}

#if defined TLI_HOST_RUN && defined __APPLE__
/// The install name of the stub on 'fd', as a string the caller owns.  Used to
/// name a library the driver was handed by path rather than by -l.
ST_FUNC char *macho_tbd_soname(i32 fd)
{
    char *data = tli_load_text(fd), *pos = data;
    char *name = macho_tbd_name(&pos);
    char *ret = name ? tli_strdup(name) : NULL;

    tli_free(data);
    return ret;
}
#endif

/// Take the exports of the .tbd on 'fd' the way macho_load_dll takes a real
/// dylib's.  A modern SDK ships these stubs in place of the libraries
/// themselves, so this is the usual path on a native link.
ST_FUNC i32 macho_load_tbd(tli_State *s1, i32 fd, const char *filename, i32 lev)
{
    char *data = tli_load_text(fd), *pos = data;
    char *soname = macho_tbd_name(&pos);
    i32 ret = -1;

    (void)filename;

    if (!soname)
        goto done;

    ret = 0;
    if (tli_add_dllref(s1, soname, lev)->found)
        goto done;

    // A stub may carry several "symbols: [ ... ]" lists, one per architecture
    // and per client; taking all of them is harmless because a name that does
    // not belong to this architecture will simply never be asked for.
    while (pos) {
        i32 more = 1;

        if (!tbd_seek("symbols: ") || !tbd_seek("["))
            break;

        while (more) {
            char *sym;

            tbd_skip_ws;
            tbd_cut_quote;
            sym = pos;
            if (!tbd_seek_any(",] \"'"))
                break;
            tbd_cut_quote;
            tbd_cut_space;
            tbd_skip_ws;
            if (*pos == 0 || *pos == ']')
                more = 0;

            tbd_cut;
            set_elf_sym(s1->dynsymtab_section, 0, 0,
                        ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0, SHN_UNDEF,
                        sym);
        }
    }

done:
    tli_free(data);
    return ret;
}

#if defined TLI_HOST_RUN && defined __APPLE__
/// Where the system libraries are is whatever xcode-select points at, and the
/// only supported way to ask is a function in libxcselect, which is present on
/// every macOS new enough to matter but is not in any header.
ST_FUNC void tli_add_macos_sdkpath(tli_State *s)
{
    i32 (*host_sdk_path)(u32, char **);
    char *sdkroot = NULL, *at = NULL;
    void *lib = dlopen("libxcselect.dylib", RTLD_GLOBAL | RTLD_LAZY);
    tli_CString path;

    cstr_new(&path);
    host_sdk_path = dlsym(lib, "xcselect_host_sdk_path");
    if (host_sdk_path)
        host_sdk_path(1, &sdkroot);

    // The answer names the SDK bundle; the libraries are under it.
    if (sdkroot)
        at = strstr(sdkroot, "SDKs/MacOSX");

    if (at)
        cstr_printf(&path, "%.*s.sdk/usr/lib",
                    (i32)(at - sdkroot) + 11, sdkroot);

    // xcselect_host_sdk_path returns memory from the real allocator.
#pragma push_macro("free")
#undef free
    free(sdkroot);
#pragma pop_macro("free")

    if (path.size)
        tli_add_library_path(s, (char *)path.data);
    else
        tli_add_library_path(s,
            "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib"
            ":" "/Applications/Xcode.app/Developer/SDKs/MacOSX.sdk/usr/lib");
    cstr_free(&path);
}
#endif // TLI_HOST_RUN && __APPLE__

/// ---------------------------------------------------------------------------
/// Writing an image.

/// The output sections, in the order the image lays them out.  A segment is a
/// run of consecutive entries sharing a segment name, so this one enum fixes
/// both which sections exist and how the segments are cut.  __PAGEZERO and
/// __LINKEDIT have no sections of their own and appear with a null name.
enum macho_osec {
    OS_NONE = 0,  /// Not written to the image at all.
    OS_PAGEZERO,
    OS_TEXT,
    OS_STUBS,
    OS_EH_FRAME,
    OS_RODATA,
    OS_GOT,
    OS_DEBUG_INFO,
    OS_DEBUG_ABBREV,
    OS_DEBUG_LINE,
    OS_DEBUG_ARANGES,
    OS_DEBUG_STR,
    OS_DEBUG_LINE_STR,
    OS_STAB,
    OS_STAB_STR,
    OS_INIT,
    OS_FINI,
    OS_DATA,
    OS_TLV,
    OS_TDATA,
    OS_TBSS,
    OS_BSS,
    OS_LINKEDIT,
    OS_COUNT
};

/// The segment and section name each entry of macho_osec is written under, in
/// the same order, so that the index into this table is the enum value.
static const struct {
    const char *seg;   /// The segment the section belongs to.
    const char *name;  /// NULL for a segment with no sections.
    u32 flags;         /// tli_Section type in the low byte, attributes above.
} macho_osecs[OS_COUNT] = {
    /// OS_NONE
    { NULL, NULL, 0 },
    /// OS_PAGEZERO
    { "__PAGEZERO", NULL, 0 },
    /// OS_TEXT
    { "__TEXT", "__text",
      S_REGULAR | S_ATTR_PURE_INSTRUCTIONS | S_ATTR_SOME_INSTRUCTIONS },
    /// OS_STUBS
    { "__TEXT", "__stubs",
      S_SYMBOL_STUBS | S_ATTR_PURE_INSTRUCTIONS | S_ATTR_SOME_INSTRUCTIONS },
    /// OS_EH_FRAME.  The frame descriptions, which go in __TEXT because that
    /// is where dyld looks for them and because nothing in them needs a fixup:
    /// every address they hold is written pc-relative, which is the whole
    /// reason the format encodes them that way.
    { "__TEXT", "__eh_frame", S_REGULAR },
    /// OS_RODATA.  Read-only data goes in __DATA_CONST rather than __TEXT
    /// because it may hold pointers, and a pointer needs a fixup, which __TEXT
    /// does not take.  The segment is made read-only once the fixups are
    /// applied.
    { "__DATA_CONST", "__rodata", S_REGULAR },
    /// OS_GOT
    { "__DATA_CONST", "__got", S_NON_LAZY_SYMBOL_POINTERS },
    /// OS_DEBUG_INFO
    { "__DWARF", "__debug_info", S_ATTR_DEBUG },
    /// OS_DEBUG_ABBREV
    { "__DWARF", "__debug_abbrev", S_ATTR_DEBUG },
    /// OS_DEBUG_LINE
    { "__DWARF", "__debug_line", S_ATTR_DEBUG },
    /// OS_DEBUG_ARANGES
    { "__DWARF", "__debug_aranges", S_ATTR_DEBUG },
    /// OS_DEBUG_STR
    { "__DWARF", "__debug_str", S_ATTR_DEBUG },
    /// OS_DEBUG_LINE_STR
    { "__DWARF", "__debug_line_str", S_ATTR_DEBUG },
    /// OS_STAB
    { "__DATA", "__stab", S_REGULAR },
    /// OS_STAB_STR
    { "__DATA", "__stab_str", S_REGULAR },
    /// OS_INIT
    { "__DATA", "__mod_init_func", S_MOD_INIT_FUNC_POINTERS },
    /// OS_FINI
    { "__DATA", "__mod_term_func", S_MOD_TERM_FUNC_POINTERS },
    /// OS_DATA
    { "__DATA", "__data", S_REGULAR },
    /// OS_TLV.  The thread-local descriptors, then the template whose start
    /// their offsets are measured from.  dyld takes that template as one span,
    /// from the first of the two sections below to the last, so nothing may
    /// come between them.
    { "__DATA", "__thread_vars", S_THREAD_LOCAL_VARIABLES },
    /// OS_TDATA
    { "__DATA", "__thread_data", S_THREAD_LOCAL_REGULAR },
    /// OS_TBSS
    { "__DATA", "__thread_bss", S_THREAD_LOCAL_ZEROFILL },
    /// OS_BSS.  Zero-filled data has to come last in its segment: it is the
    /// only thing that makes vmsize exceed filesize, and anything after it
    /// would be at the wrong file offset.  __thread_bss is zero-filled too, and
    /// sits just in front of this one, so the two of them form that tail
    /// together.
    { "__DATA", "__bss", S_ZEROFILL },
    /// OS_LINKEDIT
    { "__LINKEDIT", NULL, 0 },
};

/// The protections each segment gets.  Anything not named here is read-write,
/// which is what a segment carrying ordinary data wants.
static const struct {
    const char *name;             /// The segment this row is about.
    vm_prot_t maxprot, initprot;  /// What it may be mapped as, and what it is.
    u32 flags;                    /// SG_READ_ONLY, or none.
} macho_segprot[] = {
    { "__PAGEZERO",   0, 0, 0 },
    { "__TEXT",       VM_PROT_READ | VM_PROT_EXECUTE,
                      VM_PROT_READ | VM_PROT_EXECUTE, 0 },
    { "__DATA_CONST", VM_PROT_READ | VM_PROT_WRITE,
                      VM_PROT_READ | VM_PROT_WRITE, SG_READ_ONLY },
    { "__DWARF",      VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE,
                      VM_PROT_READ | VM_PROT_WRITE, 0 },
    { "__LINKEDIT",   VM_PROT_READ, VM_PROT_READ, 0 },
};

/// __PAGEZERO, __TEXT, __DATA_CONST, __DWARF, __DATA and __LINKEDIT: the
/// distinct segment names in the table above, with room to spare.
#define MACHO_MAX_SEG 8

/// Where the image is loaded before it is slid.  Four gigabytes of unmapped
/// __PAGEZERO underneath it is what turns a null dereference into a fault.
#define MACHO_IMAGE_BASE ((u64)1 << 32)

/// Room left at the front of __TEXT for the header and the load commands. It is
/// a whole page so that the first section keeps its alignment, and grows by
/// whole pages if the commands do not fit.
#define MACHO_HDR_PAGE  4096

/// attr->plt_offset holds the offset of a symbol's stub; this says there is
/// none yet.  It is not zero because zero is the first stub's offset.
#define MACHO_NO_STUB   ((u32)-1)

/// One segment of the image being built, and the section headers inside it.
/// The load command is written from this at the very end; until then the sizes
/// are still moving.
struct macho_seg {
    char name[16];               /// Padded, not necessarily terminated.
    u64 vmaddr, vmsize;          /// Where it is loaded, and how much of it.
    u64 fileoff, filesize;       /// Where it comes from, and how much.
    vm_prot_t maxprot, initprot; /// What it may be mapped as, and what it is.
    u32 flags;                   /// SG_READ_ONLY, or none.
    struct section_64 *sect;     /// The section headers, in image order.
    i32 nsect;                   /// How many, zero for a bare segment.
};

/// One pointer in the image that the loader has to write: a bind if it names an
/// imported symbol, a rebase if it just needs the load address added.
struct macho_fixup {
    tli_Section *s;        /// The section the pointer is stored in.
    u64 off; /// And where inside it.
    i32 sym;           /// ELF symbol index, 0 for a rebase.
    i32 ord;           /// Import index, filled in once they are numbered.
};

/// Everything one image being written needs, from the load commands down to the
/// signature.  One of these lives on macho_output_file's stack and is passed to
/// every pass in turn.
struct macho {
    struct macho_seg seg[MACHO_MAX_SEG];
    i32 nseg;               /// Segments in use.
    i32 text_seg;           /// The segment the image's base is in.
    u64 hdr_reserve;        /// Room at its front for header and load commands.

    struct load_command **lc;
    i32 nlc;                /// Load commands that are not segments.
    struct entry_point_command *ep;
    struct linkedit_data_command *fixups_lc, *exports_lc, *codesig_lc;
    struct symtab_command *symtab_lc;
    struct dysymtab_command *dysymtab_lc;

    char *sig;              /// The signature, its hashes still empty.
    u32 sigsize;            /// Its whole length, the SuperBlob included.
    u8 *sighash;            /// The first code slot inside it.

    tli_Section **osec_in[OS_COUNT];  /// The internal sections making up each one.
    i32 osec_n[OS_COUNT];         /// How many went into it.
    i32 osec_num[OS_COUNT];       /// Its 1-based Mach-O section number.
    i32 osec_at[OS_COUNT];        /// Its index inside its own segment.
    struct section_64 *osec_hdr[OS_COUNT];  /// And its header.
    i32 *elf_to_macho_sect;       /// ELF section index -> the same.

    tli_Section *stubs, *fixups, *exports, *symtab, *strtab, *indirect;
    i32 stub_sym, mh_sym, dso_sym;  /// The symbols the writer defines itself.

    struct nlist_64 *nl;    /// The Mach-O symbol table being built.
    i32 *m2e;               /// Its entries' ELF symbol indices.
    i32 *e2m;               /// And the mapping back.
    i32 nsyms;              /// Entries in it.
    i32 nelf;               /// Symbols in the ELF table they were made from,
                            /// which -s makes the larger of the two.
    i32 ilocal, iextdef, iundef;  /// Where each of the three runs begins.

    struct macho_fixup *fix;  /// Every pointer the loader has to write.
    i32 nfix, nbind;          /// How many, and how many of those are binds.
    i32 nstub;                /// Entries in __stubs.
    i32 ngot;                 /// Entries in __got.
};

/// ---------------------------------------------------------------------------
/// small helpers

/// Round 'v' up to the next multiple of 'a', which must be a power of two.
static u64 macho_align(u64 v, u64 a)
{
    return (v + a - 1) & ~(a - 1);
}

/// How many bytes 'v' takes as a ULEB128.  The export trie has to know before
/// it writes anything, because a node's size decides where the next one goes.
static i32 macho_uleb_len(u64 v)
{
    i32 n = 0;

    do {
        v >>= 7;
        n++;
    } while (v);
    return n;
}

/// Append 'v' to 's' as a ULEB128: seven bits a byte, low group first, with the
/// top bit set on every byte but the last.
static void macho_put_uleb(tli_Section *s, u64 v)
{
    do {
        u8 *p = section_ptr_add(s, 1);

        *p = (v & 0x7f) | (v >> 7 ? 0x80 : 0);
        v >>= 7;
    } while (v);
}

/// A Mach-O name field is a fixed sixteen bytes which the name need not
/// terminate, so it is filled rather than copied into.
static void macho_set_name(char dst[16], const char *src)
{
    usize n = strlen(src);

    memset(dst, 0, 16);
    memcpy(dst, src, n > 16 ? 16 : n);
}

/// Add a zeroed load command of 'cmdsize' bytes to the list, and hand it back
/// for the caller to fill in.  It is written out in the order commands are
/// added here, after every segment.
static void *macho_add_lc(struct macho *mo, u32 cmd, u32 cmdsize)
{
    struct load_command *lc = tli_mallocz(cmdsize);

    lc->cmd = cmd;
    lc->cmdsize = cmdsize;
    mo->lc = tli_realloc(mo->lc, sizeof *mo->lc * (mo->nlc + 1));
    mo->lc[mo->nlc++] = lc;
    return lc;
}

/// A load command whose tail is a string.  All three of them (LC_ID_DYLIB,
/// LC_LOAD_DYLINKER, LC_RPATH) put the lc_str immediately after cmd and
/// cmdsize, it always names the byte just past the fixed part, and the command
/// is padded out to cover the string.
static void *macho_add_str_lc(struct macho *mo, u32 cmd, u32 fixed,
                              const char *str, usize len)
{
    char *lc = macho_add_lc(mo, cmd,
                            (u32)macho_align(fixed + len + 1, 8));
    *(lc_str *)(lc + 2 * sizeof(u32)) = fixed;
    memcpy(lc + fixed, str, len);
    lc[fixed + len] = 0;
    return lc;
}

/// The segment called 'name', or NULL if nothing landed in one.  Segments are
/// few enough that a walk beats anything with an index in it.
static struct macho_seg *macho_find_seg(struct macho *mo, const char *name)
{
    i32 i;

    for (i = 0; i < mo->nseg; i++)
        if (0 == strcmp(mo->seg[i].name, name))
            return &mo->seg[i];

    return NULL;
}

/// ---------------------------------------------------------------------------
/// Symbols the image needs but no object supplies.

#if PTR_SIZE == 8
/// The body of the destructor thunk on x86_64: one __cxa_atexit call for each
/// .fini_array entry, inside a frame.  Only a 64-bit core has either of the
/// two Mach-O machines, and only its relocations carry the addend read here.
static void macho_fini_calls_x86_64(tli_State *s1, struct macho *mo,
                                    tli_Section *fini, i32 atexit_sym)
{
    ElfW_Rel *rel;
    u8 *p;

    // A frame, so that a debugger can walk out of the calls below.
    p = section_ptr_add(text_section, 4);
    p[0] = 0x55;                            // push %rbp
    p[1] = 0x48, p[2] = 0x89, p[3] = 0xe5;  // mov  %rsp,%rbp

    // Then one __cxa_atexit call for each entry of the array.
    for_each_elem(fini->reloc, 0, rel, ElfW_Rel) {
        // The entry may name a section and an offset into it rather than the
        // destructor itself, which is how a static function is usually
        // reached, so the addend has to come along.
        u64 at = text_section->data_offset;
        i32 dtor = ELFW(R_SYM)(rel->r_info);
        addr_t off = rel->r_addend;

        p = section_ptr_add(text_section, 21);
        p[0] = 0x48, p[1] = 0x8d, p[2] = 0x3d;   // lea dtor(%rip),%rdi
        put_elf_reloca(symtab_section, text_section, at + 3, R_X86_64_PC32,
                       dtor, off - 4);
        p[7] = 0x31, p[8] = 0xf6;                // xor %esi,%esi
        p[9] = 0x48, p[10] = 0x8d, p[11] = 0x15; // lea handle(%rip),%rdx
        put_elf_reloca(symtab_section, text_section, at + 12, R_X86_64_PC32,
                       mo->dso_sym, -4);
        p[16] = 0xe8;                            // call ___cxa_atexit
        put_elf_reloca(symtab_section, text_section, at + 17, R_X86_64_PLT32,
                       atexit_sym, -4);
    }

    p = section_ptr_add(text_section, 2);
    p[0] = 0x5d;                            // pop %rbp
    p[1] = 0xc3;                            // ret
}

/// The same on arm64.
static void macho_fini_calls_arm64(tli_State *s1, struct macho *mo,
                                   tli_Section *fini, i32 atexit_sym)
{
    ElfW_Rel *rel;
    u8 *p;

    // A frame, so that a debugger can walk out of the calls below.
    p = section_ptr_add(text_section, 8);
    write32le(p, 0xa9bf7bfd);               // stp x29,x30,[sp,#-16]!
    write32le(p + 4, 0x910003fd);           // mov x29,sp

    // Then one __cxa_atexit call for each entry of the array.
    for_each_elem(fini->reloc, 0, rel, ElfW_Rel) {
        // The entry may name a section and an offset into it rather than the
        // destructor itself, which is how a static function is usually
        // reached, so the addend has to come along.
        u64 at = text_section->data_offset;
        i32 dtor = ELFW(R_SYM)(rel->r_info);
        addr_t off = rel->r_addend;

        p = section_ptr_add(text_section, 24);
        write32le(p, 0x90000000);           // adrp x0,dtor
        put_elf_reloca(symtab_section, text_section, at,
                       R_AARCH64_ADR_PREL_PG_HI21, dtor, off);
        write32le(p + 4, 0x91000000);       // add  x0,x0,#:lo12:dtor
        put_elf_reloca(symtab_section, text_section, at + 4,
                       R_AARCH64_ADD_ABS_LO12_NC, dtor, off);
        write32le(p + 8, 0xd2800001);       // mov  x1,#0
        write32le(p + 12, 0x90000002);      // adrp x2,handle
        put_elf_reloc(symtab_section, text_section, at + 12,
                      R_AARCH64_ADR_PREL_PG_HI21, mo->dso_sym);
        write32le(p + 16, 0x91000042);      // add  x2,x2,#:lo12:handle
        put_elf_reloc(symtab_section, text_section, at + 16,
                      R_AARCH64_ADD_ABS_LO12_NC, mo->dso_sym);
        write32le(p + 20, 0x94000000);      // bl   ___cxa_atexit
        put_elf_reloc(symtab_section, text_section, at + 20,
                      R_AARCH64_CALL26, atexit_sym);
    }

    p = section_ptr_add(text_section, 8);
    write32le(p, 0xa8c17bfd);               // ldp x29,x30,[sp],#16
    write32le(p + 4, 0xd65f03c0);           // ret
}
#endif

/// Emit the function that hands every .fini_array entry to __cxa_atexit, and
/// put it on .init_array instead.  The three arguments are the destructor, the
/// argument to call it with, and the handle of the image it belongs to, which
/// is the address of the Mach header and is what ___dso_handle names.
static void macho_build_fini_thunk(tli_State *s1, struct macho *mo, tli_Section *fini)
{
    i32 thunk, atexit_sym;

    thunk = put_elf_sym(symtab_section, text_section->data_offset, 0,
                        ELFW(ST_INFO)(STB_LOCAL, STT_FUNC), 0,
                        text_section->sh_num, "l_tl_register_destructors");
    atexit_sym = put_elf_sym(symtab_section, 0, 0,
                             ELFW(ST_INFO)(STB_GLOBAL, STT_FUNC), 0,
                             SHN_UNDEF, "___cxa_atexit");

    // Mach-O is refused for any other machine, by tli_set_output_type().
#if PTR_SIZE == 8
    if (s1->arch->em == EM_X86_64)
        macho_fini_calls_x86_64(s1, mo, fini, atexit_sym);
    else if (s1->arch->em == EM_AARCH64)
        macho_fini_calls_arm64(s1, mo, fini, atexit_sym);
#else
    (void)mo;
    (void)atexit_sym;
#endif

    // The array itself is now dead.  Dropping SHF_ALLOC keeps it out of the
    // image; emptying its relocations keeps relocate_sections off it.
    fini->reloc->data_offset = fini->data_offset = 0;
    fini->sh_flags &= ~SHF_ALLOC;
    add_array(s1, ".init_array", thunk);
}

/// Define the two symbols every image is expected to have, and turn a
/// .fini_array into the function that registers what is in it.  Called before
/// anything else, while symbols can still be added.
static void macho_add_synthetic_syms(tli_State *s1, struct macho *mo)
{
    tli_Section *fini = NULL;
    i32 i;

    // Both of these name the Mach header.  The value is patched once the size
    // of the load commands is known; see macho_layout.
    mo->mh_sym = put_elf_sym(symtab_section, 0, 0,
                             ELFW(ST_INFO)(STB_GLOBAL, STT_OBJECT), 0,
                             text_section->sh_num, "__mh_execute_header");

    // Objects from a system compiler reach for ___dso_handle, which the C
    // runtime would define for them.  A link that leaves the runtime out still
    // has to answer for it.  set_elf_sym rather than put_elf_sym, so that a
    // unit which already referred to it has that reference defined instead of
    // a second entry appearing beside it.
    mo->dso_sym = set_elf_sym(symtab_section, 0, 0,
                              ELFW(ST_INFO)(STB_GLOBAL, STT_OBJECT), 0,
                              text_section->sh_num, "___dso_handle");

    // A .fini_array with nothing in it, or with no relocations naming what to
    // run, is nothing to turn into a function.
    for (i = 1; i < s1->nb_sections; i++)
        if (0 == strcmp(s1->sections[i]->name, ".fini_array"))
            fini = s1->sections[i];

    if (fini && fini->data_offset && fini->reloc)
        macho_build_fini_thunk(s1, mo, fini);
}

/// ---------------------------------------------------------------------------
/// The symbol table.
///
/// LC_DYSYMTAB describes the table as three consecutive runs, so the order is
/// not ours to choose: locals first, then the symbols this image defines and
/// exports, then the ones it imports.  Within the two external runs the entries
/// are sorted by name, which is also the order the export trie wants to be
/// built from.

/// The class a symbol sorts into, which is also the order the three runs of the
/// table come in.
#define MACHO_SYM_LOCAL   0
#define MACHO_SYM_EXTDEF  1
#define MACHO_SYM_UNDEF   2

/// What the sort reads, and nothing else.  Reaching from an nlist entry into
/// the ELF symbol table and from there into the ELF string table is two random
/// accesses into megabytes of data for every one of the n log n comparisons;
/// twelve contiguous bytes per symbol keep the whole sort in cache.
struct macho_symkey {
    u32 cls;    /// Which of the three runs the symbol belongs to.
    u32 strx;   /// The name's offset in the Mach-O string table.
    u32 index;  /// The symbol's index in the ELF table.
};

/// Order two symbols as LC_DYSYMTAB needs them: by run, then by name inside the
/// external runs.  'arg' is the Mach-O string table the names live in.
static i32 macho_symcmp(const void *_a, const void *_b, void *arg)
{
    const struct macho_symkey *a = _a, *b = _b;
    const char *strtab = arg;
    i32 r;

    if (a->cls != b->cls)
        return a->cls < b->cls ? -1 : 1;

    // Locals keep the order they were seen in, which costs nothing and makes
    // the output reproducible; the external runs go by name.
    if (a->cls != MACHO_SYM_LOCAL) {
        r = strcmp(strtab + a->strx, strtab + b->strx);
        if (r)
            return r;
    }
    return a->index < b->index ? -1 : a->index > b->index;
}

/// Create the sections the writer owns, then build the sorted symbol table.
/// The stubs section is made here rather than where its contents are, because
/// the label naming it has to be in the table before the table is sorted.
static void macho_create_symtab(tli_State *s1, struct macho *mo)
{
    struct macho_symkey *keys;
    i32 i, nsyms, nelf, run;

    mo->stubs = new_section(s1, "__stubs", SHT_PROGBITS,
                            SHF_ALLOC | SHF_EXECINSTR);
    s1->got = new_section(s1, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
    mo->stub_sym = put_elf_sym(symtab_section, 0, 0,
                               ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0,
                               mo->stubs->sh_num, ".__stubs");

    // The __LINKEDIT contents, in the order they are written.  They are
    // ordinary sections so that the layout pass places them like any other,
    // but nothing in the image refers to them by name.
    mo->fixups   = new_section(s1, "LE.fixups", SHT_LINKEDIT, SHF_ALLOC);
    mo->exports  = new_section(s1, "LE.exports", SHT_LINKEDIT, SHF_ALLOC);
    mo->symtab   = new_section(s1, "LE.symtab", SHT_LINKEDIT, SHF_ALLOC);
    mo->indirect = new_section(s1, "LE.indirect", SHT_LINKEDIT, SHF_ALLOC);
    mo->strtab   = new_section(s1, "LE.strtab", SHT_LINKEDIT, SHF_ALLOC);

    // Offset zero in the string table has to mean something harmless, since a
    // symbol with no name stores it.  A space is what the platform's own
    // linkers put there.
    put_elf_str(mo->strtab, " ");

    // The ELF table's entry zero is the null one, and has no Mach-O
    // counterpart, so the count is one short of it.
    nelf = symtab_section->data_offset / sizeof(ElfW(Sym)) - 1;
    keys = tli_malloc(sizeof *keys * (nelf ? nelf : 1));

    // Every ELF symbol has a slot in the map whether or not it reaches the
    // output, so that a lookup for one that did not comes back with -1 rather
    // than with whatever was next to it.
    mo->e2m = tli_malloc(sizeof *mo->e2m * (nelf + 1));
    for (i = 0; i <= nelf; i++)
        mo->e2m[i] = -1;

    // Name every symbol and class it, which is everything the sort reads.
    // Stripping happens here rather than at the end: leaving a local out from
    // the start keeps the maps, the counts and the section's size agreeing
    // with each other, where dropping entries later would have to renumber
    // everything that had already been built from them.
    for (i = 0, nsyms = 0; i < nelf; i++) {
        ElfW(Sym) *sym = (ElfW(Sym) *)symtab_section->data + i + 1;
        const char *name = (char *)symtab_section->link->data + sym->st_name;
        i32 cls = ELFW(ST_BIND)(sym->st_info) == STB_LOCAL
                  ? MACHO_SYM_LOCAL
                  : sym->st_shndx == SHN_UNDEF ? MACHO_SYM_UNDEF
                                               : MACHO_SYM_EXTDEF;

        // What -s may drop depends on what still has to be able to find it.
        // A local names nothing outside the image at all.  A defined global
        // is named by the export trie, which is built from the ELF table
        // rather than from this one, so an executable can lose those too --
        // that is what strip(1) leaves behind on one.  A library keeps them,
        // since its whole purpose is to be looked up in, and an undefined
        // symbol always stays: the indirect table addresses it by number.
        if (s1->strip
            && (cls == MACHO_SYM_LOCAL
                || (cls == MACHO_SYM_EXTDEF
                    && s1->output_type == TLI_OUTPUT_EXE)))
            continue;

        keys[nsyms].cls = cls;
        keys[nsyms].strx = put_elf_str(mo->strtab, name);
        keys[nsyms].index = i + 1;
        nsyms++;
    }

    mo->nsyms = nsyms;
    mo->nelf = nelf;
    mo->nl = section_ptr_add(mo->symtab, sizeof *mo->nl * nsyms);
    section_ptr_add(mo->strtab, -mo->strtab->data_offset & (PTR_SIZE - 1));

    // The string table has stopped moving, so its base can serve the sort.
    tl_sort(keys, nsyms, sizeof *keys, macho_symcmp, mo->strtab->data);

    // Write the names out in their sorted order, and keep the two maps between
    // an entry here and the ELF symbol it came from.
    mo->m2e = tli_malloc(sizeof *mo->m2e * (nsyms ? nsyms : 1));
    for (i = 0; i < nsyms; i++) {
        mo->nl[i].n_strx = keys[i].strx;
        mo->m2e[i] = keys[i].index;
        mo->e2m[keys[i].index] = i;
    }

    // Where each run begins.  A run that is empty starts where the next one
    // does, which is what makes its count come out zero.
    mo->ilocal = run = 0;
    while (run < nsyms && keys[run].cls == MACHO_SYM_LOCAL)
        run++;

    mo->iextdef = run;
    while (run < nsyms && keys[run].cls == MACHO_SYM_EXTDEF)
        run++;

    mo->iundef = run;
    tli_free(keys);
}

/// Decide what happens to every symbol still undefined once everything has been
/// read.  One that a dylib exports, or that is weak, or that a shared library
/// we are building may leave to its client, becomes an import; anything else is
/// an error.  Imports are moved out of SHN_UNDEF so that relocate_syms leaves
/// them alone: their address is not in the image, it is in a GOT slot dyld
/// writes.
static i32 macho_resolve_imports(tli_State *s1, struct macho *mo)
{
    i32 i, ret = 0;

    for (i = 1; i <= mo->nelf; i++) {
        ElfW(Sym) *sym = (ElfW(Sym) *)symtab_section->data + i;
        const char *name;

        if (sym->st_shndx != SHN_UNDEF)
            continue;

        name = (char *)symtab_section->link->data + sym->st_name;

        // Weak, or exported by a dylib, or ours to leave to whoever links
        // against the library being built: all three become imports.
        if (ELFW(ST_BIND)(sym->st_info) == STB_WEAK
            || s1->output_type != TLI_OUTPUT_EXE
            || find_elf_sym(s1->dynsymtab_section, name)) {
            sym->st_shndx = SHN_FROMDLL;
            continue;
        }
        tli_error_noabort("unresolved reference to '%s'", name);
        ret = -1;
    }

    return ret;
}

/// ---------------------------------------------------------------------------
/// GOT slots, stubs and the fixups that fill them.

/// Record that the pointer at 's + off' is for the loader to write: a bind on
/// 'sym' if that is nonzero, a rebase of whatever the field ends up holding if
/// it is not.  The order they arrive in does not matter; they are sorted later.
static void macho_add_fixup(struct macho *mo, tli_Section *s, u64 off,
                            i32 sym)
{
    struct macho_fixup *f;

    mo->fix = tli_realloc(mo->fix, sizeof *mo->fix * (mo->nfix + 1));
    f = &mo->fix[mo->nfix++];
    f->s = s;
    f->off = off;
    f->sym = sym;
    f->ord = 0;
    mo->nbind += sym != 0;
}

/// The stub an imported function is called through: load its address out of the
/// GOT slot and jump.  There is no lazy binding here, so a stub is three
/// instructions and never comes back to the linker.
static void macho_build_stub(tli_State *s1, struct macho *mo, i32 sym_index,
                             struct tli_SymAttr *attr)
{
    u8 *p;

    attr->plt_offset = mo->stubs->data_offset;
    switch (s1->arch->em) {
    case EM_X86_64:
        p = section_ptr_add(mo->stubs, 6);
        p[0] = 0xff, p[1] = 0x25;           // jmp *slot(%rip)
        put_elf_reloc(symtab_section, mo->stubs, attr->plt_offset + 2,
                      R_X86_64_GOTPCREL, sym_index);
        break;
    case EM_AARCH64:
        p = section_ptr_add(mo->stubs, 12);
        write32le(p, 0x90000010);           // adrp x16,slot
        put_elf_reloc(symtab_section, mo->stubs, attr->plt_offset,
                      R_AARCH64_ADR_GOT_PAGE, sym_index);
        write32le(p + 4, 0xf9400210);       // ldr  x16,[x16,#:lo12:slot]
        put_elf_reloc(symtab_section, mo->stubs, attr->plt_offset + 4,
                      R_AARCH64_LD64_GOT_LO12_NC, sym_index);
        write32le(p + 8, 0xd61f0200);       // br   x16
        break;
    }
    mo->nstub++;
}

/// The two sections holding what a thread's block is initialised from.  They
/// are named rather than recognised by flags because nothing in the internal
/// model distinguishes them: as far as it is concerned they are ordinary data
/// and ordinary bss, and only Mach-O cares that they are not.
static i32 macho_is_tlv_template(const tli_Section *s)
{
    return 0 == strcmp(s->name, ".thread_data")
        || 0 == strcmp(s->name, ".thread_bss");
}

/// Is this relocation the offset word of a thread-local variable descriptor?
///
/// A descriptor is three words: the resolver dyld installs, a key it fills in,
/// and the offset of the variable's initial value inside the thread-local
/// template.  Only the third of those is unusual, and nothing in the relocation
/// says so: the compiler emits an ordinary absolute pointer naming the initial
/// value's symbol, exactly as it would for a pointer to that data, and leaves
/// the linker to know that a pointer stored in __thread_vars means an offset.
/// So the section it sits in is the whole of the evidence.
static i32 macho_is_tlv_offset(tli_State *s1, tli_Section *s, ElfW_Rel *rel)
{
    ElfW(Sym) *sym;
    i32 shndx;

    if (ELFW(R_TYPE)(rel->r_info) != s1->arch->r_data_ptr
        || 0 != strcmp(s->name, ".thread_vars"))
        return 0;

    sym = (ElfW(Sym) *)symtab_section->data + ELFW(R_SYM)(rel->r_info);
    shndx = sym->st_shndx;
    if (shndx <= 0 || shndx >= s1->nb_sections)
        return 0;   // not defined in a section of this image

    return macho_is_tlv_template(s1->sections[shndx]);
}

/// Does this relocation reach its target through a stub?  Only an unconditional
/// branch can: a tail call gets there by the same route an ordinary call does,
/// which is why the jump form belongs here beside the call form.
static i32 macho_reaches_stub(tli_State *s1, i32 type)
{
    switch (s1->arch->em) {
    case EM_X86_64:
        return type == R_X86_64_PLT32;
    case EM_AARCH64:
        return type == R_AARCH64_CALL26 || type == R_AARCH64_JUMP26;
    }
    return 0;
}

/// Walk every relocation, build the GOT and the stubs it asks for, and record
/// every pointer the loader will have to write.
///
/// A GOT slot is created for any symbol this image imports and for any symbol a
/// relocation insists on reaching indirectly.  How the slot is filled depends
/// on which: an imported symbol's slot gets a bind naming it, and a defined
/// symbol's gets an ordinary relocation to compute the address plus a rebase to
/// slide it.  Deciding that once, where the slot is created, is what keeps the
/// result independent of the order the relocations arrive in.  That matters on
/// arm64, where a GOT access is a pair of instructions and clang writes the two
/// relocations in descending address order, so either half may be the one that
/// creates the slot.
static void macho_check_relocs(tli_State *s1, struct macho *mo)
{
    u32 *stub_ind = NULL, *got_ind = NULL, *pi;
    i32 i;

    for (i = 1; i < s1->nb_sections; i++) {
        tli_Section *sr = s1->sections[i], *s;
        ElfW_Rel *rel;

        if (sr->sh_type != SHT_RELX)
            continue;

        s = s1->sections[sr->sh_info];

        // A section that is not in the image has no address, so a pointer in
        // it cannot be fixed up; that covers the debug sections and the
        // .fini_array once its entries have been turned into a function.  The
        // GOT's own relocations are accounted for where its slots are made,
        // not here.
        if (!(s->sh_flags & SHF_ALLOC) || !strncmp(s->name, ".debug_", 7)
            || s == s1->got)
            continue;

        for_each_elem(sr, 0, rel, ElfW_Rel) {
            i32 type = ELFW(R_TYPE)(rel->r_info);
            i32 sym_index = ELFW(R_SYM)(rel->r_info);
            ElfW(Sym) *sym = (ElfW(Sym) *)symtab_section->data + sym_index;
            i32 import = sym->st_shndx == SHN_UNDEF;
            struct tli_SymAttr *attr;

            // Anything reached indirectly needs a slot, and every import is
            // reached indirectly whether the relocation says so or not.
            if (import || gotplt_entry_type(s1, type) == ALWAYS_GOTPLT_ENTRY) {
                attr = get_sym_attr(s1, sym_index, 1);

                if (!attr->dyn_index) {
                    attr->dyn_index = 1;    // "this symbol has a slot"
                    attr->plt_offset = MACHO_NO_STUB;
                    attr->got_offset = s1->got->data_offset;
                    section_ptr_add(s1->got, PTR_SIZE);

                    // The indirect table entry for the slot: the symbol it
                    // stands for, or a note that it stands for nothing named.
                    got_ind = tli_realloc(got_ind,
                                          sizeof *got_ind * (mo->ngot + 1));
                    if (ELFW(ST_BIND)(sym->st_info) == STB_LOCAL) {
                        if (import)
                            tli_error("unresolved local reference to '%s'",
                                      (char *)symtab_section->link->data
                                      + sym->st_name);
                        got_ind[mo->ngot++] = INDIRECT_SYMBOL_LOCAL;
                    } else {
                        // A symbol -s left out has no number to give, which is
                        // no loss: the slot it stands for is one this link
                        // fills, so there is nothing for dyld to look up.
                        i32 m = mo->e2m[sym_index];

                        got_ind[mo->ngot++] = m < 0 ? INDIRECT_SYMBOL_LOCAL
                                                    : (u32)m;
                    }

                    // And how the slot comes to hold an address: dyld writes
                    // it from a name, or the link computes it and dyld slides
                    // what it finds.
                    if (import) {
                        macho_add_fixup(mo, s1->got, attr->got_offset,
                                        sym_index);
                    } else {
                        put_elf_reloc(symtab_section, s1->got,
                                      attr->got_offset, s1->arch->r_jmp_slot, sym_index);
                        macho_add_fixup(mo, s1->got, attr->got_offset, 0);
                    }
                }

                if (import && code_reloc(s1, type) > 0) {
                    // Any other code relocation reaches its target directly,
                    // which an import has no address for.  Leave it to
                    // relocate_sections to complain about the range.
                    if (!macho_reaches_stub(s1, type))
                        continue;

                    if (attr->plt_offset == MACHO_NO_STUB) {
                        macho_build_stub(s1, mo, sym_index, attr);
                        stub_ind = tli_realloc(stub_ind, sizeof *stub_ind
                                               * mo->nstub);
                        stub_ind[mo->nstub - 1] = mo->e2m[sym_index];
                    }

                    // Send the branch to the stub instead of the symbol.
                    // Mach-O is written for 64-bit targets only, whose
                    // records carry their addend.
                    rel->r_info = ELFW(R_INFO)(mo->stub_sym, type);
#if SHT_RELX == SHT_RELA
                    rel->r_addend += attr->plt_offset;
#endif
                    continue;
                }
            }

            // A pointer-sized address stored in the image.  Either dyld writes
            // it from a name, or it is computed here and only needs sliding.
            // A descriptor's offset word is neither: it is a distance, so
            // sliding it would be as wrong as binding it.
            if ((u32)type == s1->arch->r_data_ptr && !macho_is_tlv_offset(s1, s, rel))
                macho_add_fixup(mo, s, rel->r_offset, import ? sym_index : 0);
        }
    }

    // The indirect symbol table says, for each stub and each GOT slot in turn,
    // which symbol it stands for.  Its two halves are addressed by the
    // reserved1 field of the two sections.
    pi = section_ptr_add(mo->indirect,
                         sizeof *pi * (mo->nstub + mo->ngot));
    if (mo->nstub)
        memcpy(pi, stub_ind, sizeof *pi * mo->nstub);

    if (mo->ngot)
        memcpy(pi + mo->nstub, got_ind, sizeof *pi * mo->ngot);

    tli_free(stub_ind);
    tli_free(got_ind);
}

/// ---------------------------------------------------------------------------
/// The export trie.
///
/// dyld answers a lookup by walking a trie of the exported names, so what is
/// written is not a list but an index.  Each node holds the payload for the
/// name spelled by the path that reaches it, if any, then a count of children
/// and, for each, the characters that edge adds and the offset of the node it
/// leads to.  Offsets are ULEB128 and point backwards or forwards, so the size
/// of a node depends on where the ones after it landed: the layout is settled
/// by repeating it until nothing moves, which takes two or three passes.

/// One name the image exports, and the payload the trie stores under it.
struct macho_export {
    const char *name;  /// Points into the ELF string table, not owned here.
    u32 len;           /// Measured once here, since the trie walks it often.
    u32 flags;         /// Whether the definition is weak, absolute or ordinary.
    u64 offset;        /// The address, from the Mach header.
};

/// A node of the trie under construction.  The exports are sorted, so the ones
/// below any node are a contiguous run of them.
struct macho_trie_node {
    i32 first, last;  /// The exports below this node.
    i32 depth;        /// How many characters the path here spells.
    i32 edge;         /// Where this node's own edge label starts.
    i32 term;         /// The export that ends here, or -1.
    i32 kid0, nkid;   /// Children, contiguous and always later.
    u32 off, size;    /// Where the node lands in the blob, and how big it is.
};

/// The trie itself: the sorted exports, and the nodes built over them.
struct macho_trie {
    struct macho_export *exp;     /// Not owned; the caller frees them.
    struct macho_trie_node *node; /// Parents always come before their children.
    i32 nnode, anode;             /// In use, and allocated.
};

/// How many bytes an export's payload takes: its flags and its address, both
/// ULEB128.  The layout pass and the write pass must agree on this, or every
/// node after the first lands somewhere the one naming it did not say.
static i32 macho_term_len(const struct macho_export *e)
{
    return macho_uleb_len(e->flags) + macho_uleb_len(e->offset);
}

/// Order two exports by name, which is the order the trie is built from.
static i32 macho_export_cmp(const void *_a, const void *_b, void *arg)
{
    const struct macho_export *a = _a, *b = _b;
    u32 n = a->len < b->len ? a->len : b->len;
    i32 r = memcmp(a->name, b->name, n);

    (void)arg;

    // strcmp order, computed without a second walk of either string: the
    // lengths were measured once when the export was collected.
    return r ? r : (i32)a->len - (i32)b->len;
}

/// Append a node covering exports 'first' to 'last', reached by a path of
/// 'depth' characters of which its own edge contributes the ones from 'edge'.
/// Returns its index, which never moves even though the array does.
static i32 macho_trie_add(struct macho_trie *t, i32 first, i32 last, i32 depth,
                          i32 edge)
{
    struct macho_trie_node *n;

    if (t->nnode == t->anode) {
        t->anode = t->anode ? t->anode * 2 : 32;
        t->node = tli_realloc(t->node, sizeof *t->node * t->anode);
    }

    n = &t->node[t->nnode];
    n->first = first, n->last = last, n->depth = depth, n->edge = edge;
    n->term = -1, n->kid0 = 0, n->nkid = 0, n->off = 0, n->size = 0;
    return t->nnode++;
}

/// Give node 'ni' its children.  The exports below it are sorted and all share
/// its first 'depth' characters, so each child is a run of them agreeing on the
/// next character, and the edge to it can carry every further character they
/// all agree on as well.
static void macho_trie_split(struct macho_trie *t, i32 ni)
{
    i32 first = t->node[ni].first;
    i32 last = t->node[ni].last;
    i32 depth = t->node[ni].depth;
    i32 i = first;

    // A name that stops exactly here is this node's own payload, and sorts
    // first among the exports below it.
    if (t->exp[i].len == (u32)depth) {
        t->node[ni].term = i;
        i++;
    }

    t->node[ni].kid0 = t->nnode;
    while (i < last) {
        char c = t->exp[i].name[depth];
        i32 j = i + 1, d;

        while (j < last && t->exp[j].name[depth] == c)
            j++;

        // Carry as many further characters onto this edge as every export in
        // the run agrees on, which is what keeps the trie from being one node
        // per character.
        for (d = depth + 1; t->exp[i].len > (u32)d; d++) {
            char c2 = t->exp[i].name[d];
            i32 k;

            for (k = i + 1; k < j; k++)
                if (t->exp[k].len <= (u32)d || t->exp[k].name[d] != c2)
                    break;

            if (k != j)
                break;
        }

        macho_trie_add(t, i, j, d, depth);
        t->node[ni].nkid++;
        i = j;
    }
}

/// How many bytes node 'ni' takes as it stands.  Its children's offsets are
/// part of that, so the answer changes as the layout settles.
static u32 macho_trie_size(struct macho_trie *t, i32 ni)
{
    struct macho_trie_node *n = &t->node[ni];
    u32 size = 1;      // the child count
    i32 term = 0, i;

    if (n->term >= 0)
        term = macho_term_len(&t->exp[n->term]);

    size += macho_uleb_len(term) + term;
    for (i = 0; i < n->nkid; i++) {
        struct macho_trie_node *k = &t->node[n->kid0 + i];

        size += k->depth - k->edge + 1 + macho_uleb_len(k->off);
    }

    return size;
}

/// Collect everything the image exports and write the trie for it into
/// mo->exports.  Addresses are stored as distances from 'image_base', which is
/// what dyld adds them to.
static void macho_build_export_trie(tli_State *s1, struct macho *mo,
                                    u64 image_base)
{
    struct macho_trie t;
    struct macho_export *e = NULL;
    i32 nexp = 0, i, pass;
    u32 off;

    // What is exported is every global or weak symbol defined in a section of
    // this image, and nothing else.
    for (i = 1; i <= mo->nelf; i++) {
        ElfW(Sym) *sym = (ElfW(Sym) *)symtab_section->data + i;
        i32 bind = ELFW(ST_BIND)(sym->st_info);

        if (sym->st_shndx == SHN_UNDEF || sym->st_shndx >= SHN_LORESERVE)
            continue;

        if (bind != STB_GLOBAL && bind != STB_WEAK)
            continue;

        e = tli_realloc(e, sizeof *e * (nexp + 1));
        e[nexp].name = (char *)symtab_section->link->data + sym->st_name;
        e[nexp].len = strlen(e[nexp].name);
        e[nexp].flags = EXPORT_SYMBOL_FLAGS_KIND_REGULAR
                        | (bind == STB_WEAK
                           ? EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION : 0);
        e[nexp].offset = sym->st_value + s1->sections[sym->st_shndx]->sh_addr
                         - image_base;
        nexp++;
    }

    if (!nexp)
        return;

    tl_sort(e, nexp, sizeof *e, macho_export_cmp, NULL);

    // Two symbols of the same name would send the trie round in circles, and
    // the second could only shadow the first anyway.
    for (i = 1; i < nexp; i++)
        if (macho_export_cmp(&e[i - 1], &e[i], NULL) == 0) {
            memmove(&e[i], &e[i + 1], sizeof *e * (nexp - i - 1));
            nexp--, i--;
        }

    // Build the trie, starting from a root that covers every export.  Children
    // are always appended, so walking the array front to back visits every
    // node after its parent and finishes in one pass.
    memset(&t, 0, sizeof t);
    t.exp = e;
    macho_trie_add(&t, 0, nexp, 0, 0);
    for (i = 0; i < t.nnode; i++)
        macho_trie_split(&t, i);

    // Settle where each node goes.  A node names its children by offset and
    // the offsets are ULEB128, so growing one can push the rest along and
    // lengthen another; both only ever grow, so repeating the layout reaches a
    // fixed point, in practice after two or three passes.
    for (pass = 0; pass < 32; pass++) {
        i32 moved = 0;

        for (i = 0, off = 0; i < t.nnode; i++) {
            if (t.node[i].off != off)
                t.node[i].off = off, moved = 1;

            t.node[i].size = macho_trie_size(&t, i);
            off += t.node[i].size;
        }

        if (!moved)
            break;
    }

    // Write the nodes out in that order.
    for (i = 0; i < t.nnode; i++) {
        struct macho_trie_node *n = &t.node[i];
        i32 term = 0, k;
        u8 *p;

        // Every offset written above was taken from the settled layout, so a
        // node landing anywhere else means the layout never settled and the
        // trie would be nonsense.
        if (mo->exports->data_offset != n->off) {
            tli_error_noabort("the export trie did not settle");
            break;
        }

        // The payload, its length in front of it so that a reader with no
        // interest in this node can step over it.
        if (n->term >= 0)
            term = macho_term_len(&e[n->term]);

        macho_put_uleb(mo->exports, term);
        if (term) {
            macho_put_uleb(mo->exports, e[n->term].flags);
            macho_put_uleb(mo->exports, e[n->term].offset);
        }

        // Then the edges: how many, and for each the characters it adds and
        // where the node it leads to begins.
        p = section_ptr_add(mo->exports, 1);
        *p = n->nkid;
        for (k = 0; k < n->nkid; k++) {
            struct macho_trie_node *kid = &t.node[n->kid0 + k];
            i32 len = kid->depth - kid->edge;

            p = section_ptr_add(mo->exports, len + 1);
            memcpy(p, e[kid->first].name + kid->edge, len);
            p[len] = 0;
            macho_put_uleb(mo->exports, kid->off);
        }
    }

    // dyld reads the blob as a unit; keep its end where a load command can
    // describe it without a partial word.
    section_ptr_add(mo->exports, -mo->exports->data_offset & 7);

    tli_free(t.node);
    tli_free(e);
}

/// ---------------------------------------------------------------------------
/// Laying the image out.

/// Which output section an internal one becomes, or OS_NONE to drop it.
static enum macho_osec macho_osec_of(tli_State *s1, struct macho *mo, tli_Section *s)
{
    // Debug sections carry no SHF_ALLOC but are still written out, as a
    // segment that is never mapped.
    if (!strncmp(s->name, ".debug_", 7)) {
        if (s == dwarf_info_section)     return OS_DEBUG_INFO;
        if (s == dwarf_abbrev_section)   return OS_DEBUG_ABBREV;
        if (s == dwarf_line_section)     return OS_DEBUG_LINE;
        if (s == dwarf_aranges_section)  return OS_DEBUG_ARANGES;
        if (s == dwarf_str_section)      return OS_DEBUG_STR;
        if (s == dwarf_line_str_section) return OS_DEBUG_LINE_STR;
        return OS_NONE;
    }

    if (!(s->sh_flags & SHF_ALLOC))
        return OS_NONE;

    switch (s->sh_type) {
    case SHT_LINKEDIT:   return OS_LINKEDIT;
    case SHT_INIT_ARRAY: return OS_INIT;
    case SHT_FINI_ARRAY: return OS_FINI;
    case SHT_NOBITS:
        return macho_is_tlv_template(s) ? OS_TBSS : OS_BSS;
    case SHT_STRTAB:
        return stab_section && s == stabstr_section ? OS_STAB_STR : OS_NONE;
    case SHT_PROGBITS:
        if (s == mo->stubs)      return OS_STUBS;
        if (s == s1->got)        return OS_GOT;
        if (s == stab_section)   return OS_STAB;
        if (s == rodata_section) return OS_RODATA;
        if (0 == strcmp(s->name, ".eh_frame")) return OS_EH_FRAME;
        if (0 == strcmp(s->name, ".thread_vars")) return OS_TLV;
        if (0 == strcmp(s->name, ".thread_data")) return OS_TDATA;
        if (s->sh_flags & SHF_EXECINSTR)
            return OS_TEXT;
        return s->sh_flags & SHF_WRITE ? OS_DATA : OS_RODATA;
    }
    return OS_NONE;
}

/// Build the segment list and the section headers inside it.  Both fall out of
/// the layout table: a segment is a run of output sections that name it, and it
/// exists at all only if something landed in one of them.
static void macho_build_segments(tli_State *s1, struct macho *mo)
{
    i32 i, sk, nsect = 0;

    mo->text_seg = s1->output_type == TLI_OUTPUT_EXE ? 1 : 0;
    mo->elf_to_macho_sect = tli_mallocz(sizeof(i32) * s1->nb_sections);

    // Sort every internal section into the output section it belongs to.
    for (i = 1; i < s1->nb_sections; i++) {
        tli_Section *s = s1->sections[i];
        enum macho_osec os = macho_osec_of(s1, mo, s);

        if (os == OS_NONE)
            continue;

        mo->osec_in[os] = tli_realloc(mo->osec_in[os],
                                      sizeof(tli_Section *) * (mo->osec_n[os] + 1));
        mo->osec_in[os][mo->osec_n[os]++] = s;
    }

    // __TEXT always exists: the Mach header is the first thing in it.  So does
    // __LINKEDIT, which holds the symbol table if nothing else.  __PAGEZERO is
    // what makes address zero unmapped, and only an executable gets one; a
    // dylib is placed by whoever loads it.
    for (sk = OS_PAGEZERO; sk < OS_COUNT; sk++) {
        const char *segname = macho_osecs[sk].seg;
        struct macho_seg *seg;
        struct section_64 *sec;

        if (sk == OS_PAGEZERO && s1->output_type != TLI_OUTPUT_EXE)
            continue;

        if (sk != OS_PAGEZERO && sk != OS_TEXT && sk != OS_LINKEDIT
            && !mo->osec_n[sk])
            continue;

        // A segment is a run of output sections naming it, so this one belongs
        // to the segment last opened if that is the same name, and opens a new
        // one if it is not.
        seg = mo->nseg && 0 == strcmp(mo->seg[mo->nseg - 1].name, segname)
              ? &mo->seg[mo->nseg - 1] : NULL;
        if (!seg) {
            u32 p;

            seg = &mo->seg[mo->nseg++];
            memset(seg, 0, sizeof *seg);
            macho_set_name(seg->name, segname);
            seg->maxprot = seg->initprot = VM_PROT_READ | VM_PROT_WRITE;
            for (p = 0; p < sizeof macho_segprot / sizeof *macho_segprot; p++)
                if (0 == strcmp(macho_segprot[p].name, segname)) {
                    seg->maxprot = macho_segprot[p].maxprot;
                    seg->initprot = macho_segprot[p].initprot;
                    seg->flags = macho_segprot[p].flags;
                    break;
                }
        }

        if (!macho_osecs[sk].name)
            continue;

        // Then the section header itself, numbered across the whole image
        // rather than within its segment, which is how a symbol names it.
        seg->sect = tli_realloc(seg->sect, sizeof *seg->sect * (seg->nsect + 1));
        mo->osec_at[sk] = seg->nsect;
        sec = &seg->sect[seg->nsect++];
        memset(sec, 0, sizeof *sec);
        macho_set_name(sec->sectname, macho_osecs[sk].name);
        macho_set_name(sec->segname, segname);
        sec->flags = macho_osecs[sk].flags;
        mo->osec_num[sk] = ++nsect;
        for (i = 0; i < mo->osec_n[sk]; i++)
            mo->elf_to_macho_sect[mo->osec_in[sk][i]->sh_num] = nsect;
    }

    // A segment's section array moves as it grows, so the headers can only be
    // pointed at once every segment is complete.
    for (sk = OS_PAGEZERO; sk < OS_COUNT; sk++)
        if (mo->osec_num[sk])
            mo->osec_hdr[sk] = &macho_find_seg(mo, macho_osecs[sk].seg)
                                    ->sect[mo->osec_at[sk]];
}

/// The oldest release of each platform that could load what this writer emits,
/// which is what an image says when the caller named no minimum of its own.
/// A version packs as x.y.z, sixteen bits and then two of eight.
static u32 macho_default_minos(u32 platform)
{
    switch (platform) {

    // The whole iOS family starts at 9.0: the oldest arm64 iOS, with tvOS,
    // Catalyst and the simulators counting their versions the same way.
    case PLATFORM_IOS:
    case PLATFORM_IOSSIMULATOR:
    case PLATFORM_MACCATALYST:
    case PLATFORM_TVOS:
    case PLATFORM_TVOSSIMULATOR:
        return 9 << 16;

    // watchOS at 2.0, the first release that ran anything natively
    case PLATFORM_WATCHOS:
    case PLATFORM_WATCHOSSIMULATOR:
        return 2 << 16;

    // and 10.6 for macOS, the oldest release anything here would run on
    default:
        return (10 << 16) | (6 << 8);
    }
}

/// Everything that is not a segment.  All of it is known before the layout, so
/// summing the sizes here is what says how much room the header needs.
static void macho_build_commands(tli_State *s1, struct macho *mo,
                                 const char *filename)
{
    struct build_version_command *bv;
    struct source_version_command *sv;
    i32 i;

    // A library says what it is called, since that is the name recorded in
    // everything that links against it.
    if (s1->output_type != TLI_OUTPUT_EXE) {
        const char *name = s1->install_name ? s1->install_name : filename;
        struct dylib_command *dl;

        dl = macho_add_str_lc(mo, LC_ID_DYLIB, sizeof *dl, name, strlen(name));
        dl->timestamp = 1;
        dl->current_version = s1->current_version ? s1->current_version : 1 << 16;
        dl->compatibility_version =
            s1->compatibility_version ? s1->compatibility_version : 1 << 16;
    }

    // The four that point at __LINKEDIT.  All of them are filled in by the
    // layout, which is where the blobs they describe get their offsets.
    mo->fixups_lc = macho_add_lc(mo, LC_DYLD_CHAINED_FIXUPS,
                                 sizeof *mo->fixups_lc);
    mo->exports_lc = macho_add_lc(mo, LC_DYLD_EXPORTS_TRIE,
                                  sizeof *mo->exports_lc);
    mo->symtab_lc = macho_add_lc(mo, LC_SYMTAB, sizeof *mo->symtab_lc);
    mo->dysymtab_lc = macho_add_lc(mo, LC_DYSYMTAB, sizeof *mo->dysymtab_lc);

    if (s1->output_type == TLI_OUTPUT_EXE)
        macho_add_str_lc(mo, LC_LOAD_DYLINKER,
                         sizeof(struct dylinker_command),
                         "/usr/lib/dyld", sizeof "/usr/lib/dyld" - 1);

    // What the image was built for and against.  Claiming no more than the
    // oldest release that could run this is what keeps the loader from
    // insisting on something newer.
    bv = macho_add_lc(mo, LC_BUILD_VERSION, sizeof *bv);
    bv->platform = s1->macho_platform ? s1->macho_platform : PLATFORM_MACOS;
    bv->minos = bv->sdk = s1->macho_minos ? s1->macho_minos
                                          : macho_default_minos(bv->platform);
    bv->ntools = 0;

    sv = macho_add_lc(mo, LC_SOURCE_VERSION, sizeof *sv);
    sv->version = 0;

    if (s1->output_type == TLI_OUTPUT_EXE)
        mo->ep = macho_add_lc(mo, LC_MAIN, sizeof *mo->ep);

    // Every library named on the command line.  One reached only through
    // another's re-exports is loaded by that one, and saying so twice would
    // have dyld open it twice.
    for (i = 0; i < s1->nb_loaded_dlls; i++) {
        tli_DLLReference *dll = s1->loaded_dlls[i];
        struct dylib_command *dl;

        if (dll->level)             // reached through another dylib
            continue;

        dl = macho_add_str_lc(mo, LC_LOAD_DYLIB, sizeof *dl,
                              dll->name, strlen(dll->name));
        dl->timestamp = 2;
        dl->current_version = 1 << 16;
        dl->compatibility_version = 1 << 16;
    }

    // -rpath is one colon-separated list here and one command per entry there.
    if (s1->rpath) {
        const char *path = s1->rpath, *end;

        do {
            end = strchr(path, ':');
            if (!end)
                end = strchr(path, 0);

            macho_add_str_lc(mo, LC_RPATH, sizeof(struct rpath_command),
                             path, end - path);
            path = end + 1;
        } while (*end);
    }

    // Last, which is where ld64 puts it, and empty until the layout knows
    // where in the file the signature will land.
    mo->codesig_lc = macho_add_lc(mo, LC_CODE_SIGNATURE, sizeof *mo->codesig_lc);
}

/// The size of every load command, segments included.
static u32 macho_sizeofcmds(struct macho *mo)
{
    u32 size = 0;
    i32 i;

    for (i = 0; i < mo->nseg; i++)
        size += sizeof(struct segment_command_64)
                + mo->seg[i].nsect * sizeof(struct section_64);

    for (i = 0; i < mo->nlc; i++)
        size += mo->lc[i]->cmdsize;

    return size;
}

/// Pages of chained fixups a segment needs.  page_count is a u16, so a
/// segment of more than 65535 pages cannot be described at all; the size pass
/// reports that, and both passes clamp here so the room reserved and the room
/// written stay equal whatever the answer.
static u64 macho_fixup_pages(const struct macho_seg *sg)
{
    u64 pages = (sg->vmsize + SEG_PAGE_SIZE - 1) / SEG_PAGE_SIZE;

    return pages > 0xffff ? 0xffff : pages;
}

/// How much room the chained fixups will want.  The blob has to be a section
/// like any other so that it gets an address and a file offset, but its
/// contents cannot be written until the pointers it describes have been
/// relocated, so its size is worked out here and it is filled in later.
static u64 macho_fixups_size(tli_State *s1, struct macho *mo)
{
    u64 size;
    i32 i;

    // The blob is written even when there is nothing in it to fix up: dyld
    // reads whatever LC_DYLD_CHAINED_FIXUPS points at and rejects the image if
    // it is not a whole header, so an empty command is worse than none.
    size = macho_align(sizeof(struct dyld_chained_fixups_header), 8);
    size += macho_align(4 + 4 * mo->nseg, 8);

    // A chain table for every segment that can hold a pointer, two bytes of it
    // per page whether that page has a fixup on it or not.
    for (i = mo->text_seg; i < mo->nseg - 1; i++) {
        u64 pages = macho_fixup_pages(&mo->seg[i]);

        // A gigabyte is every page the format can name.  Nothing legitimate
        // comes near it, but a damaged object can claim a symbol that large,
        // and the arithmetic below wraps long before the allocation fails.
        if (mo->seg[i].vmsize > (u64)0xffff * SEG_PAGE_SIZE)
            tli_error_noabort("segment '%s' is %llu bytes, more than chained "
                              "fixups can describe", mo->seg[i].name,
                              (unsigned long long)mo->seg[i].vmsize);

        size += macho_align(sizeof(struct dyld_chained_starts_in_segment)
                            + (pages ? pages - 1 : 0) * 2, 8);
    }

    // Then a word per import, and the pool of names they point into.
    size += 4 * (u64)mo->nbind;
    size += 1;                  // the symbol pool's leading terminator
    for (i = 0; i < mo->nfix; i++)
        if (mo->fix[i].sym) {
            ElfW(Sym) *sym = (ElfW(Sym) *)symtab_section->data + mo->fix[i].sym;

            size += strlen((char *)symtab_section->link->data + sym->st_name) + 1;
        }

    return macho_align(size, 8);
}

/// Rewrite each descriptor's offset word from an address into a distance.
///
/// The relocation already computes S + A, so taking the template's address off
/// the addend is enough to leave the offset behind; it costs one pass over the
/// descriptors and no new relocation type.  It has to happen after the layout,
/// which is where the template first has an address, and before
/// relocate_sections, which is where the sum is worked out.
///
/// That the word is an offset at all was read out of what Apple's ld leaves in
/// __thread_vars: the third word of the descriptor for a variable whose initial
/// value is sixteen bytes into __thread_data is 0x10, and dyld_info lists no
/// fixup over it, which an address in a position-independent image would
/// need.
static void macho_place_tlv_offsets(tli_State *s1, struct macho *mo)
{
    // Only a RELA record carries an addend for this to adjust.
#if SHT_RELX == SHT_RELA
    u64 base;
    i32 i;

    // The template starts wherever its first section landed, and an image with
    // no thread-local storage at all has neither of them.
    if (mo->osec_hdr[OS_TDATA])
        base = mo->osec_hdr[OS_TDATA]->addr;
    else if (mo->osec_hdr[OS_TBSS])
        base = mo->osec_hdr[OS_TBSS]->addr;
    else
        return;

    for (i = 1; i < s1->nb_sections; i++) {
        tli_Section *sr = s1->sections[i], *s;
        ElfW_Rel *rel;

        if (sr->sh_type != SHT_RELX)
            continue;

        s = s1->sections[sr->sh_info];
        if (0 != strcmp(s->name, ".thread_vars"))
            continue;

        for_each_elem(sr, 0, rel, ElfW_Rel)
            if (macho_is_tlv_offset(s1, s, rel))
                rel->r_addend -= (addr_t)base;
    }
#else
    (void)s1;
    (void)mo;
#endif
}

/// Give every section an address and a file offset, and every segment the span
/// of the sections in it.  Addresses and file offsets advance together, always
/// a whole number of pages apart, so a segment's file layout mirrors its memory
/// layout and only zero-filled data at the very end can make them differ.
static void macho_layout(tli_State *s1, struct macho *mo)
{
    struct macho_seg *cur;
    u64 addr, fileoff;
    i32 sk, i;

    macho_build_segments(s1, mo);

    if (s1->output_type == TLI_OUTPUT_EXE) {
        mo->seg[0].vmaddr = 0;
        mo->seg[0].vmsize = MACHO_IMAGE_BASE;
        mo->seg[1].vmaddr = MACHO_IMAGE_BASE;
    }

    // The header and the load commands sit in front of the first section, and
    // take a whole number of pages so that the section keeps whatever
    // alignment it asked for.  One page covers an ordinary link; a great many
    // libraries or rpaths take a second.
    mo->hdr_reserve = macho_align(sizeof(struct mach_header_64)
                                  + macho_sizeofcmds(mo), MACHO_HDR_PAGE);

    // Now that the reserve is known, the two symbols standing for the Mach
    // header can be placed: they are the start of __TEXT, which is that far in
    // front of the first section in it.
    ((ElfW(Sym) *)symtab_section->data)[mo->mh_sym].st_value =
        -(addr_t)mo->hdr_reserve;
    ((ElfW(Sym) *)symtab_section->data)[mo->dso_sym].st_value =
        -(addr_t)mo->hdr_reserve;

    // The first section of __TEXT starts past that reserve; everything else
    // follows on from wherever the section in front of it ended.
    cur = &mo->seg[mo->text_seg];
    cur->fileoff = 0;
    addr = cur->vmaddr + mo->hdr_reserve;
    fileoff = mo->hdr_reserve;

    for (sk = OS_PAGEZERO; sk < OS_COUNT; sk++) {
        struct section_64 *sec = mo->osec_hdr[sk];
        struct macho_seg *seg;
        u64 align = 1;

        if (!mo->osec_num[sk] && sk != OS_LINKEDIT)
            continue;

        seg = macho_find_seg(mo, macho_osecs[sk].seg);

        // Every segment after the first starts on a fresh page.  Closing the
        // one before it here, rather than where it was created, means a
        // segment that turned out to be empty never gets an address at all.
        if (seg != cur) {
            cur->vmsize = addr - cur->vmaddr;
            cur->filesize = fileoff - cur->fileoff;
            addr = macho_align(addr, SEG_PAGE_SIZE);
            fileoff = macho_align(fileoff, SEG_PAGE_SIZE);
            seg->vmaddr = addr;
            seg->fileoff = fileoff;
            cur = seg;
        }

        // __LINKEDIT's contents are the last thing to be built: the export
        // trie needs every section's address, and the fixup blob needs the
        // size of every segment before it.
        if (sk == OS_LINKEDIT) {
            macho_build_export_trie(s1, mo, mo->seg[mo->text_seg].vmaddr);
            section_ptr_add(mo->fixups, macho_fixups_size(s1, mo));
            if (!mo->osec_n[sk])
                continue;
        }

        // An output section is aligned for the strictest of the internal
        // sections in it, and the reserve at the front of __TEXT is only a
        // page, so nothing stricter than that can be honoured.
        for (i = 0; i < mo->osec_n[sk]; i++) {
            tli_Section *s = mo->osec_in[sk][i];

            s->sh_size = s->data_offset;
            if ((u64)s->sh_addralign > align)
                align = s->sh_addralign;
        }

        if (align > MACHO_HDR_PAGE) {
            tli_warning("section alignment %u is more than a page",
                        (u32)align);
            align = MACHO_HDR_PAGE;
        }

        addr = macho_align(addr, align);
        fileoff = macho_align(fileoff, align);
        if (sec) {
            u32 type = sec->flags & SECTION_TYPE;

            sec->addr = addr;
            sec->align = exact_log2p1((i32)align) - 1;

            // Nothing is stored for zero-filled data, so it has no offset.
            sec->offset = type == S_ZEROFILL
                          || type == S_THREAD_LOCAL_ZEROFILL ? 0 : fileoff;
        }

        // Then the internal sections inside it, one after another.  Only
        // zero-filled data takes no file space, which is why nothing may
        // follow it in its segment.
        for (i = 0; i < mo->osec_n[sk]; i++) {
            tli_Section *s = mo->osec_in[sk][i];

            addr = macho_align(addr, s->sh_addralign ? s->sh_addralign : 1);
            s->sh_addr = addr;
            addr += s->sh_size;
            if (s->sh_type != SHT_NOBITS) {
                fileoff = macho_align(fileoff,
                                      s->sh_addralign ? s->sh_addralign : 1);
                s->sh_offset = fileoff;
                fileoff += s->sh_size;
            }
        }

        if (sec)
            sec->size = addr - sec->addr;
    }

    // The last segment is closed here, since nothing came after it to do so.
    cur->vmsize = addr - cur->vmaddr;
    cur->filesize = fileoff - cur->fileoff;

    // __stubs and __got are read through the indirect symbol table, which the
    // loader finds by the index each of them names.  reserved2 is the size of
    // the stub macho_build_stub() writes: x86_64 or, the only other Mach-O
    // machine, arm64.
    if (mo->osec_hdr[OS_STUBS])
        mo->osec_hdr[OS_STUBS]->reserved2 = s1->arch->em == EM_X86_64 ? 6 : 12;

    if (mo->osec_hdr[OS_GOT])
        mo->osec_hdr[OS_GOT]->reserved1 = mo->nstub;

    // Every __LINKEDIT blob now has an offset, so the commands pointing at
    // them can be filled in.
    mo->symtab_lc->symoff = mo->symtab->sh_offset;
    mo->symtab_lc->nsyms = mo->nsyms;
    mo->symtab_lc->stroff = mo->strtab->sh_offset;
    mo->symtab_lc->strsize = mo->strtab->data_offset;

    mo->dysymtab_lc->ilocalsym = mo->ilocal;
    mo->dysymtab_lc->nlocalsym = mo->iextdef - mo->ilocal;
    mo->dysymtab_lc->iextdefsym = mo->iextdef;
    mo->dysymtab_lc->nextdefsym = mo->iundef - mo->iextdef;
    mo->dysymtab_lc->iundefsym = mo->iundef;
    mo->dysymtab_lc->nundefsym = mo->nsyms - mo->iundef;
    mo->dysymtab_lc->indirectsymoff = mo->indirect->sh_offset;
    mo->dysymtab_lc->nindirectsyms = mo->indirect->data_offset / sizeof(u32);

    if (mo->fixups->data_offset) {
        mo->fixups_lc->dataoff = mo->fixups->sh_offset;
        mo->fixups_lc->datasize = mo->fixups->data_offset;
    }

    if (mo->exports->data_offset) {
        mo->exports_lc->dataoff = mo->exports->sh_offset;
        mo->exports_lc->datasize = mo->exports->data_offset;
    }

    macho_place_tlv_offsets(s1, mo);
}

/// ---------------------------------------------------------------------------
/// Filling in the chained fixups.
///
/// By now every pointer holds either its final address, which a rebase has only
/// to slide, or nothing, because a bind will supply it.  What is written here
/// is the table saying where each page's chain begins, the chain itself, which
/// lives in the pointer words, and the names the binds refer to.
///
/// The older encoding, a stream of opcodes under LC_DYLD_INFO_ONLY with lazy
/// stubs behind it, is not written: everything this linker targets understands
/// chained fixups, and one path that is exercised beats two of which only one
/// ever is.

/// Order two fixups by the address of the pointer they are over, which is the
/// order the chains thread them in.
static i32 macho_fixup_cmp(const void *_a, const void *_b, void *arg)
{
    const struct macho_fixup *a = _a, *b = _b;
    addr_t aa = a->s->sh_addr + a->off;
    addr_t ab = b->s->sh_addr + b->off;

    (void)arg;
    return aa > ab ? 1 : aa < ab ? -1 : 0;
}

/// Write the fixup blob and thread the chains through the pointers it
/// describes.  Returns -1 if the pointers cannot be chained, which is a bug
/// here rather than anything the input can ask for.
static i32 macho_fill_fixups(tli_State *s1, struct macho *mo)
{
    struct dyld_chained_fixups_header *hdr;
    struct dyld_chained_starts_in_image *image;
    u8 *base = mo->fixups->data, *p;
    u32 *imports;
    i32 i, seg, ord = 0;

    if (!mo->fixups->data_offset)
        return 0;

    // In address order, since that is the order a chain runs in.  Two fixups
    // over one pointer would mean one of them was written twice over.
    tl_sort(mo->fix, mo->nfix, sizeof *mo->fix, macho_fixup_cmp, NULL);
    for (i = 1; i < mo->nfix; i++)
        if (mo->fix[i].s == mo->fix[i - 1].s
            && mo->fix[i].off == mo->fix[i - 1].off)
            return tli_error_noabort("two fixups at %s+%lu",
                                     mo->fix[i].s->name, mo->fix[i].off);

    // Number the imports, which is what a bind stores in place of a name.
    for (i = 0; i < mo->nfix; i++)
        if (mo->fix[i].sym)
            mo->fix[i].ord = ord++;

    hdr = (struct dyld_chained_fixups_header *)base;
    p = base + macho_align(sizeof *hdr, 8);
    hdr->starts_offset = p - base;
    hdr->imports_count = mo->nbind;
    hdr->imports_format = DYLD_CHAINED_IMPORT;
    hdr->symbols_format = 0;

    image = (struct dyld_chained_starts_in_image *)p;
    image->seg_count = mo->nseg;
    p += macho_align(4 + 4 * mo->nseg, 8);

    // __PAGEZERO has nothing in it and __LINKEDIT is not part of the image
    // proper, so neither gets a chain table.
    i = 0;
    for (seg = mo->text_seg; seg < mo->nseg - 1; seg++) {
        struct macho_seg *sg = &mo->seg[seg];
        struct dyld_chained_starts_in_segment *st;
        u64 pages = macho_fixup_pages(sg);
        u64 size = sizeof *st + (pages ? pages - 1 : 0) * 2;
        u64 page;

        image->seg_info_offset[seg] = (p - base) - hdr->starts_offset;
        st = (struct dyld_chained_starts_in_segment *)p;
        p += macho_align(size, 8);
        st->size = (u32)size;
        st->page_size = SEG_PAGE_SIZE;
        st->pointer_format = DYLD_CHAINED_PTR_64;
        st->segment_offset = sg->vmaddr - mo->seg[mo->text_seg].vmaddr;
        st->max_valid_pointer = 0;
        st->page_count = (u16)pages;

        // The fixups are in address order, so one walk of them fills in every
        // page of every segment in turn.
        for (page = 0; page < pages; page++) {
            addr_t start = sg->vmaddr + (addr_t)page * SEG_PAGE_SIZE;
            addr_t end = start + SEG_PAGE_SIZE;
            u8 *last = NULL;
            addr_t last_at = 0;

            st->page_start[page] = DYLD_CHAINED_PTR_START_NONE;
            for (; i < mo->nfix; i++) {
                struct macho_fixup *f = &mo->fix[i];
                addr_t at = f->s->sh_addr + f->off;
                u8 *word;
                u64 v;

                if (at >= end)
                    break;

                if (at < start)
                    continue;   // below this segment; nothing wrote it

                // A chain step is counted in four-byte units and a pointer's
                // two halves may not land in different pages.
                if ((at & 3) || (at & (SEG_PAGE_SIZE - 1))
                                 > SEG_PAGE_SIZE - PTR_SIZE)
                    return tli_error_noabort(
                        "%s+%lu: a fixup must be aligned and inside one page",
                        f->s->name, f->off);

                // What the pointer word becomes: an ordinal for dyld to bind
                // from, or the address it already holds, repacked.
                word = f->s->data + f->off;
                if (f->sym) {
                    v = CHAIN_BIND(f->ord, 0);
                } else {
                    // The field already holds the address; a rebase carries
                    // its low 36 bits and its top byte, and has nowhere to put
                    // anything in between.
                    u64 target = read64le(word);

                    if (target & ~(CHAIN_TARGET_MASK | CHAIN_HIGH8_MASK))
                        return tli_error_noabort(
                            "%s+%lu: 0x%llx is out of range for a rebase",
                            f->s->name, f->off, (unsigned long long)target);

                    v = CHAIN_REBASE(target);
                }

                // Then thread it on: the word before it says how far along
                // this one is, or the page's table entry does if it is first.
                if (last) {
                    u64 step = (at - last_at) / 4;

                    if (step > CHAIN_NEXT_MAX)
                        return tli_error_noabort("fixup chain step too long");

                    write64le(last, read64le(last) | (step << CHAIN_NEXT_SHIFT));
                } else {
                    st->page_start[page] = at - start;
                }

                write64le(word, v);
                last = word;
                last_at = at;
            }
        }
    }

    // The imports, and the pool of names they point into.  Offset zero in that
    // pool has to be a terminator, since it is what an unnamed import would
    // appear to hold.
    hdr->imports_offset = p - base;
    imports = (u32 *)p;
    p += 4 * (u64)mo->nbind;
    hdr->symbols_offset = p - base;
    *p++ = 0;
    for (i = 0; i < mo->nfix; i++) {
        struct macho_fixup *f = &mo->fix[i];
        ElfW(Sym) *sym;
        const char *name;
        i32 weak;

        if (!f->sym)
            continue;

        sym = (ElfW(Sym) *)symtab_section->data + f->sym;
        name = (char *)symtab_section->link->data + sym->st_name;
        weak = ELFW(ST_BIND)(sym->st_info) == STB_WEAK;

        // Every import is a flat lookup: this linker does not record which
        // library a name came from, so dyld searches all of them.
        imports[f->ord] = (u32)(BIND_SPECIAL_DYLIB_FLAT_LOOKUP & 0xff)
                          | ((u32)weak << 8)
                          | ((u32)(p - base - hdr->symbols_offset) << 9);
        strcpy((char *)p, name);
        p += strlen(name) + 1;
    }

    // The blob was sized before its contents were known, by walking the same
    // lists in the same order.  If the two ever disagreed the writes above
    // would already have run past the end of the section, so say so rather
    // than shipping the result.
    if ((u64)(p - base) > mo->fixups->data_offset)
        return tli_error_noabort("the chained fixups overran their space");

    return 0;
}

/// ---------------------------------------------------------------------------
/// Turning the ELF symbol table into nlist entries.  Only the name has been
/// filled in so far; everything else waited for the addresses.

/// Fill in the rest of every nlist entry: its type, the section it is defined
/// in and its address.  Runs after the relocations, since that is where a
/// symbol's value stops moving.
static void macho_convert_symbols(tli_State *s1, struct macho *mo)
{
    i32 i;

    for (i = 0; i < mo->nsyms; i++) {
        ElfW(Sym) *sym = (ElfW(Sym) *)symtab_section->data + mo->m2e[i];
        const char *name = (char *)symtab_section->link->data + sym->st_name;
        struct nlist_64 *n = &mo->nl[i];
        i32 bind = ELFW(ST_BIND)(sym->st_info);

        n->n_desc = 0;
        n->n_sect = 0;
        n->n_value = sym->st_value;

        // Mach-O has no symbol types to speak of: what a symbol is matters far
        // less than where it is, which is what the section below decides.
        switch (ELFW(ST_TYPE)(sym->st_info)) {
        case STT_NOTYPE: case STT_OBJECT: case STT_FUNC:
        case STT_TLS:    case STT_SECTION:
            n->n_type = N_SECT;
            break;
        case STT_FILE:
            n->n_type = N_ABS;
            break;
        default:
            tli_error("unhandled ELF symbol type %d %s",
                      ELFW(ST_TYPE)(sym->st_info), name);
        }

        // Where it is defined, if anywhere.
        if (sym->st_shndx == SHN_FROMDLL)
            n->n_type = N_UNDF;
        else if (sym->st_shndx == SHN_ABS)
            n->n_type = N_ABS;
        else if (sym->st_shndx == SHN_UNDEF)
            tli_error("should have become an import: %s", name);
        else if (sym->st_shndx >= SHN_LORESERVE)
            tli_error("unhandled ELF symbol section %d %s", sym->st_shndx, name);
        else if (mo->elf_to_macho_sect[sym->st_shndx])
            n->n_sect = mo->elf_to_macho_sect[sym->st_shndx];
        else if (!(s1->sections[sym->st_shndx]->sh_flags & SHF_ALLOC))
            // Debug sections are not mapped, and .fini_array is retired once
            // its entries have been turned into a function; a symbol left
            // pointing into either has no address in the image, so it is
            // reported as an absolute rather than dropped or invented.
            n->n_type = N_ABS;
        else
            tli_error("ELF section %d(%s) is not in the image, but %s is in it",
                      sym->st_shndx, s1->sections[sym->st_shndx]->name, name);

        // And how visible it is.  A weak reference and a weak definition are
        // separate bits, and an import can only be the first.
        if (bind == STB_GLOBAL)
            n->n_type |= N_EXT;
        else if (bind == STB_WEAK)
            n->n_desc |= N_WEAK_REF | (n->n_type != N_UNDF ? N_WEAK_DEF : 0);
    }
}

/// ---------------------------------------------------------------------------
/// SHA-256, from FIPS 180-4.
///
/// The code signature is the only thing in this linker that wants a hash, and
/// it wants exactly one of them, so it is spelled out here rather than taken
/// from a library: an embeddable linker that drags in a crypto dependency to
/// write thirty-two bytes has made a poor trade.

/// A hash in progress: the eight words of state, and the block being filled.
struct macho_sha256 {
    u32 h[8];     /// The state, which is the digest once everything is in.
    u64 len;      /// Bytes fed in, which is also what the padding is from.
    u8 buf[64];   /// The block being filled.
};

/// The round constants: the fractional parts of the cube roots of the first
/// sixty-four primes, as FIPS 180-4 gives them.
static const u32 macho_sha256_k[64] = {
    0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
    0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
    0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
    0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
    0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
    0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
    0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
    0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
    0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
    0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
    0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
    0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
    0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
    0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
    0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
    0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
};

/// Rotate a 32-bit word right, which is the one operation SHA-256 is built out
/// of that C has no operator for.
#define MACHO_ROR(x, n) (((x) >> (n)) | ((x) << (32 - (n))))

/// Store a word big endian, which is the order every field of the signature is
/// written in and the order SHA-256 works in.
static void macho_put_be32(void *p, u32 v)
{
    u8 *q = p;

    q[0] = (u8)(v >> 24);
    q[1] = (u8)(v >> 16);
    q[2] = (u8)(v >> 8);
    q[3] = (u8)v;
}

/// The same for a 64-bit field, high half first.
static void macho_put_be64(void *p, u64 v)
{
    macho_put_be32(p, (u32)(v >> 32));
    macho_put_be32((u8 *)p + 4, (u32)v);
}

/// Start a hash: the state begins as the fractional parts of the square roots
/// of the first eight primes.
static void macho_sha256_init(struct macho_sha256 *c)
{
    static const u32 iv[8] = {
        0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
        0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
    };

    memcpy(c->h, iv, sizeof iv);
    c->len = 0;
}

/// Mix one 64-byte block into the state.  This is the whole of the algorithm;
/// everything else is buffering and padding.
static void macho_sha256_block(struct macho_sha256 *c, const u8 *p)
{
    u32 w[64], a, b, cc, d, e, f, g, h;
    i32 i;

    // The message schedule: the block as sixteen big-endian words, then
    // forty-eight more derived from them.
    for (i = 0; i < 16; i++)
        w[i] = (u32)p[4 * i] << 24 | (u32)p[4 * i + 1] << 16
               | (u32)p[4 * i + 2] << 8 | (u32)p[4 * i + 3];

    for (; i < 64; i++) {
        u32 s0 = MACHO_ROR(w[i - 15], 7) ^ MACHO_ROR(w[i - 15], 18)
                 ^ (w[i - 15] >> 3);
        u32 s1 = MACHO_ROR(w[i - 2], 17) ^ MACHO_ROR(w[i - 2], 19)
                 ^ (w[i - 2] >> 10);

        w[i] = w[i - 16] + s0 + w[i - 7] + s1;
    }

    // Sixty-four rounds over a copy of the state, added back at the end.
    a = c->h[0]; b = c->h[1]; cc = c->h[2]; d = c->h[3];
    e = c->h[4]; f = c->h[5]; g = c->h[6];  h = c->h[7];

    for (i = 0; i < 64; i++) {
        u32 t1 = h + (MACHO_ROR(e, 6) ^ MACHO_ROR(e, 11) ^ MACHO_ROR(e, 25))
                 + ((e & f) ^ (~e & g)) + macho_sha256_k[i] + w[i];
        u32 t2 = (MACHO_ROR(a, 2) ^ MACHO_ROR(a, 13) ^ MACHO_ROR(a, 22))
                 + ((a & b) ^ (a & cc) ^ (b & cc));

        h = g; g = f; f = e; e = d + t1;
        d = cc; cc = b; b = a; a = t1 + t2;
    }

    c->h[0] += a; c->h[1] += b; c->h[2] += cc; c->h[3] += d;
    c->h[4] += e; c->h[5] += f; c->h[6] += g;  c->h[7] += h;
}

/// Feed 'n' bytes in.  The caller hands over whatever it has; the block
/// boundaries are this function's business.
static void macho_sha256_update(struct macho_sha256 *c, const void *data,
                                u64 n)
{
    const u8 *p = data;
    u32 have = (u32)(c->len & 63);

    c->len += n;

    // Finish the part-filled block first, if there is one and this is enough
    // to complete it.
    if (have) {
        u64 take = 64 - have;

        if (take > n)
            take = n;

        memcpy(c->buf + have, p, (usize)take);
        if (have + take < 64)
            return;

        macho_sha256_block(c, c->buf);
        p += take;
        n -= take;
    }

    // Then whole blocks straight out of the caller's buffer, and whatever is
    // left over into ours.
    for (; n >= 64; p += 64, n -= 64)
        macho_sha256_block(c, p);

    if (n)
        memcpy(c->buf, p, (usize)n);
}

/// Pad, mix the last block or two, and write the digest to 'out' as
/// thirty-two bytes.  The hash is finished; nothing more may be fed in.
static void macho_sha256_final(struct macho_sha256 *c, u8 *out)
{
    u64 bits = c->len * 8;
    u32 have = (u32)(c->len & 63);
    i32 i;

    // A one bit, then zeroes, then the length in bits as the last eight bytes
    // of a block.  If the length no longer fits in the block being padded it
    // takes one more.
    c->buf[have++] = 0x80;
    if (have > 56) {
        memset(c->buf + have, 0, 64 - have);
        macho_sha256_block(c, c->buf);
        have = 0;
    }

    memset(c->buf + have, 0, 56 - have);
    macho_put_be64(c->buf + 56, bits);
    macho_sha256_block(c, c->buf);

    for (i = 0; i < 8; i++)
        macho_put_be32(out + 4 * i, c->h[i]);
}

/// ---------------------------------------------------------------------------
/// Building the signature.
///
/// Sizing it is the awkward part.  What gets hashed is the file up to the point
/// where the signature starts, so how many hashes there are depends on that
/// offset, and the offset depends on everything in front of it having been
/// placed, the load command pointing at the signature included.  So the command
/// is created with all the others, before the layout, and only filled in here,
/// once the layout has given every other byte in the file its offset.  Nothing
/// about the size of the signature feeds back into that offset, which is what
/// keeps this to one pass.
///
/// The hashes themselves are left empty: they cannot be known until the bytes
/// they cover have been written, so macho_write fills them in as it goes.

/// Size the signature, write everything in it but the hashes, and point the
/// load command at where it will go.
static void macho_build_signature(tli_State *s1, struct macho *mo,
                                  const char *filename)
{
    struct macho_seg *le = macho_find_seg(mo, "__LINKEDIT");
    struct macho_seg *text = &mo->seg[mo->text_seg];
    const char *ident = tli_basename(filename);
    u32 identlen = strlen(ident) + 1;
    u32 cdoff = sizeof(struct cs_super_blob) + sizeof(struct cs_blob_index);
    u32 hashoff = sizeof(struct cs_code_directory) + identlen;
    u32 nslots, cdlen;
    u64 limit;
    char *cd;

    // The signature is the last thing in the file, and starts on a sixteen
    // byte boundary because that is where codesign(1) puts it.  Whatever
    // padding that takes is in front of the code limit, so it is hashed like
    // any other byte of the image.
    limit = macho_align(le->fileoff + le->filesize, 16);
    nslots = (u32)((limit + CS_PAGE_SIZE - 1) >> CS_PAGE_SHIFT);
    cdlen = hashoff + nslots * CS_HASH_SIZE;

    mo->sigsize = cdoff + cdlen;
    mo->sig = tli_mallocz(mo->sigsize);
    mo->sighash = (u8 *)mo->sig + cdoff + hashoff;

    // The SuperBlob, which for an ad-hoc signature is a header and one index
    // entry naming the code directory behind it.
    macho_put_be32(mo->sig + offsetof(struct cs_super_blob, magic),
                   CSMAGIC_EMBEDDED_SIGNATURE);
    macho_put_be32(mo->sig + offsetof(struct cs_super_blob, length),
                   mo->sigsize);
    macho_put_be32(mo->sig + offsetof(struct cs_super_blob, count), 1);
    macho_put_be32(mo->sig + sizeof(struct cs_super_blob)
                   + offsetof(struct cs_blob_index, type),
                   CSSLOT_CODEDIRECTORY);
    macho_put_be32(mo->sig + sizeof(struct cs_super_blob)
                   + offsetof(struct cs_blob_index, offset), cdoff);

    // The directory begins twenty bytes into the blob, so it is not aligned
    // for the 64-bit fields it carries and cannot be written through a struct
    // pointer.  Each field goes to its own offset instead, which also keeps
    // the big-endian store in one place.
    cd = mo->sig + cdoff;
#define CD32(f, v) macho_put_be32(cd + offsetof(struct cs_code_directory, f), (v))
#define CD64(f, v) macho_put_be64(cd + offsetof(struct cs_code_directory, f), (v))
#define CD8(f, v)  (cd[offsetof(struct cs_code_directory, f)] = (char)(v))
    CD32(magic, CSMAGIC_CODEDIRECTORY);
    CD32(length, cdlen);
    CD32(version, CS_SUPPORTS_EXEC_SEG);
    CD32(flags, CS_ADHOC | CS_LINKER_SIGNED);
    CD32(hash_offset, hashoff);
    CD32(ident_offset, sizeof(struct cs_code_directory));
    CD32(n_special, 0);
    CD32(n_code, nslots);
    CD32(code_limit, (u32)limit);
    CD8(hash_size, CS_HASH_SIZE);       // a single byte needs no swapping
    CD8(hash_type, CS_HASHTYPE_SHA256);
    CD8(platform, 0);                   // not part of the operating system
    CD8(page_shift, CS_PAGE_SHIFT);
    CD64(code_limit64, limit);
    CD64(exec_seg_base, text->fileoff);
    CD64(exec_seg_limit, text->filesize);
    CD64(exec_seg_flags,
         s1->output_type == TLI_OUTPUT_EXE ? CS_EXECSEG_MAIN_BINARY : 0);
#undef CD32
#undef CD64
#undef CD8

    // The identifier follows the directory's fixed part, and the hashes follow
    // it in turn; macho_write fills those in as it goes.
    memcpy(cd + sizeof(struct cs_code_directory), ident, identlen);

    mo->codesig_lc->dataoff = (u32)limit;
    mo->codesig_lc->datasize = mo->sigsize;

    // __LINKEDIT has to cover the signature.  The kernel finds the blob by the
    // load command but reaches it through that segment's mapping, and refuses
    // an image whose signature falls outside it.
    le->filesize = limit + mo->sigsize - le->fileoff;
    le->vmsize = macho_align(le->filesize, SEG_PAGE_SIZE);
}

/// ---------------------------------------------------------------------------
/// Writing the file.
///
/// Every byte goes out through macho_emit, which hashes it into the code slot
/// of the page it lands in on the way past.  Hashing as the file is written
/// rather than reading it back afterwards keeps the signature over exactly what
/// went to disc, and costs one pass less.

/// The file being written, and the hash of the page currently under the pen.
struct macho_writer {
    FILE *fp;                 /// Where the bytes go.
    u64 at;                   /// How many have gone there so far.
    struct macho_sha256 sha;  /// The hash of the page being written.
    u32 fill;                 /// How much of that page has been fed in.
    u8 *slot;                 /// Where its hash belongs in the signature.
};

/// Write 'n' bytes and hash them into the code slots they belong to, finishing
/// a slot whenever a page is complete.
static void macho_emit(struct macho_writer *w, const void *data, u64 n)
{
    const u8 *p = data;

    fwrite(p, 1, (usize)n, w->fp);
    w->at += n;

    while (n) {
        u64 take = CS_PAGE_SIZE - w->fill;

        if (take > n)
            take = n;

        macho_sha256_update(&w->sha, p, take);
        w->fill += (u32)take;
        p += take;
        n -= take;
        if (w->fill == CS_PAGE_SIZE) {
            macho_sha256_final(&w->sha, w->slot);
            w->slot += CS_HASH_SIZE;
            macho_sha256_init(&w->sha);
            w->fill = 0;
        }
    }
}

/// Write zeroes up to file offset 'to'.  A section's alignment is padding like
/// any other byte of the image, and is hashed with the rest.
static void macho_pad(struct macho_writer *w, u64 to)
{
    static const char zero[512];

    while (w->at < to) {
        u64 n = to - w->at;

        macho_emit(w, zero, n < sizeof zero ? n : sizeof zero);
    }
}

/// Write the whole image: header, load commands, section contents, and the
/// signature over all of it.  Everything has an offset by now, so this pass
/// decides nothing and only puts bytes where the layout said they go.
static void macho_write(tli_State *s1, struct macho *mo, FILE *fp)
{
    struct mach_header_64 mh;
    struct macho_writer w;
    i32 i, sk;

    memset(&w, 0, sizeof w);
    w.fp = fp;
    w.slot = mo->sighash;
    macho_sha256_init(&w.sha);

    // The header, which says what the image is and how much of what follows it
    // is load commands.
    memset(&mh, 0, sizeof mh);
    mh.mh.magic = MH_MAGIC_64;
    mh.mh.cputype = s1->arch->macho_cputype;
    mh.mh.cpusubtype = s1->arch->macho_cpusubtype;
    if (s1->arch->em == EM_X86_64)
        mh.mh.cpusubtype |= CPU_SUBTYPE_LIB64;

    if (s1->output_type == TLI_OUTPUT_EXE) {
        mh.mh.filetype = MH_EXECUTE;
        mh.mh.flags = MH_DYLDLINK | MH_PIE;
    } else {
        mh.mh.filetype = MH_DYLIB;
        mh.mh.flags = MH_DYLDLINK;
    }

    if (mo->osec_num[OS_TLV])
        mh.mh.flags |= MH_HAS_TLV_DESCRIPTORS;

    mh.mh.ncmds = mo->nseg + mo->nlc;
    mh.mh.sizeofcmds = macho_sizeofcmds(mo);
    macho_emit(&w, &mh, sizeof mh);

    // Then the segments, each followed by its section headers, and everything
    // else after them, which is the order macho_sizeofcmds counted them in.
    for (i = 0; i < mo->nseg; i++) {
        struct segment_command_64 sc;

        memset(&sc, 0, sizeof sc);
        sc.cmd = LC_SEGMENT_64;
        sc.cmdsize = sizeof sc + mo->seg[i].nsect * sizeof(struct section_64);
        memcpy(sc.segname, mo->seg[i].name, sizeof sc.segname);
        sc.vmaddr = mo->seg[i].vmaddr;
        sc.vmsize = mo->seg[i].vmsize;
        sc.fileoff = mo->seg[i].fileoff;
        sc.filesize = mo->seg[i].filesize;
        sc.maxprot = mo->seg[i].maxprot;
        sc.initprot = mo->seg[i].initprot;
        sc.nsects = mo->seg[i].nsect;
        sc.flags = mo->seg[i].flags;
        macho_emit(&w, &sc, sizeof sc);
        macho_emit(&w, mo->seg[i].sect,
                   mo->seg[i].nsect * sizeof(struct section_64));
    }
    for (i = 0; i < mo->nlc; i++)
        macho_emit(&w, mo->lc[i], mo->lc[i]->cmdsize);

    // The contents, in the order the layout placed them, padded out to the
    // offset each was given.  Zero-filled data is not stored.
    for (sk = OS_PAGEZERO; sk < OS_COUNT; sk++)
        for (i = 0; i < mo->osec_n[sk]; i++) {
            tli_Section *s = mo->osec_in[sk][i];

            if (s->sh_type == SHT_NOBITS || !s->sh_size)
                continue;

            macho_pad(&w, s->sh_offset);
            macho_emit(&w, s->data, s->sh_size);
        }

    // Everything up to the code limit has now been hashed, the padding in
    // front of the signature included.  The last page is usually a partial
    // one, and its slot is filled here; the signature is not hashed itself,
    // so it goes out around macho_emit.
    macho_pad(&w, mo->codesig_lc->dataoff);
    if (w.fill)
        macho_sha256_final(&w.sha, w.slot);
    fwrite(mo->sig, 1, mo->sigsize, fp);
}

/// Link everything read so far into a Mach-O image at 'filename'.  The passes
/// below run in the one order that works: nothing may be added to a table after
/// something else has been sized or placed against it.
ST_FUNC i32 macho_output_file(tli_State *s1, const char *filename)
{
    struct macho mo;
    FILE *fp;
    i32 fd, i, ret = -1;

    memset(&mo, 0, sizeof mo);
    unlink(filename);
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
              s1->output_type == TLI_OUTPUT_OBJ ? 0666 : 0777);
    if (fd < 0 || (fp = fdopen(fd, "wb")) == NULL) {
        if (fd >= 0)
            close(fd);

        return tli_error_noabort("could not write '%s': %s",
                                 filename, strerror(errno));
    }

    // Everything that can still add a symbol, then the table those symbols go
    // into, then the GOT and stubs the relocations ask for.
    tli_add_runtime(s1);
    macho_add_synthetic_syms(s1, &mo);
    resolve_common_syms(s1);
    macho_create_symtab(s1, &mo);
    macho_check_relocs(s1, &mo);
    if (macho_resolve_imports(s1, &mo))
        goto done;

    // Then the load commands, which say how much room the header wants, then
    // the layout, which gives every byte of the image its place.
    macho_build_commands(s1, &mo, filename);
    macho_layout(s1, &mo);
    macho_build_signature(s1, &mo, filename);

    relocate_syms(s1, s1->symtab, 0);
    if (s1->output_type == TLI_OUTPUT_EXE) {
        // Honour -Wl,-e,<sym> the way the ELF and PE writers do.  LC_MAIN
        // takes an offset into __TEXT rather than an address.
        const char *entry = s1->elf_entryname ? s1->elf_entryname : "main";

        mo.ep->entryoff = get_sym_addr(s1, entry, 1, !s1->elf_entryname)
                          - mo.seg[mo.text_seg].vmaddr;
    }

    if (s1->nb_errors)
        goto done;

    // Mach-O binds imports through the fixup chains rather than through a
    // dynamic symbol table, so the relocations are applied as though this
    // were a static link whatever the output really is.
    i = s1->output_type;
    s1->output_type = TLI_OUTPUT_EXE;
    relocate_sections(s1);
    s1->output_type = i;

    // The pointers hold their final values now, so the chains can be threaded
    // through them and the symbol table finished off.
    if (macho_fill_fixups(s1, &mo))
        goto done;

    macho_convert_symbols(s1, &mo);
    if (s1->verbose)
        printf("<- %s\n", filename);

    macho_write(s1, &mo, fp);
    ret = s1->nb_errors ? -1 : 0;

done:
    fclose(fp);
    for (i = 0; i < mo.nlc; i++)
        tli_free(mo.lc[i]);

    for (i = 0; i < mo.nseg; i++)
        tli_free(mo.seg[i].sect);

    for (i = 0; i < OS_COUNT; i++)
        tli_free(mo.osec_in[i]);

    tli_free(mo.lc);
    tli_free(mo.fix);
    tli_free(mo.m2e);
    tli_free(mo.e2m);
    tli_free(mo.elf_to_macho_sect);
    tli_free(mo.sig);
    return ret;
}


/// ---- src/core/tl_macho_obj.c ----

/// tinylinker - Mach-O relocatable object reader
///
/// Reads an MH_OBJECT produced by clang and folds it into the same internal
/// model the ELF loader builds, so section merging, symbol resolution,
/// relocation and output are all unchanged.

/// The file type of a relocatable object, as against an image.
#define MH_OBJECT (0x1)

/// The section types the image writer has no use for: the zerofill form for a
/// section above four gigabytes, and the literal types clang puts read-only
/// data in.
#define S_GB_ZEROFILL           0xc
#define S_CSTRING_LITERALS      0x2
#define S_4BYTE_LITERALS        0x3
#define S_8BYTE_LITERALS        0x4
#define S_16BYTE_LITERALS       0xe

/// The parts of an nlist's n_type the image writer has no use for: the two
/// bits saying a symbol is a debugger entry or a private extern, and the mask
/// over what kind of symbol it is.
#define N_STAB  0xe0
#define N_PEXT  0x10
#define N_TYPE  0x0e

/// What n_sect holds when a symbol is in no section at all.
#define NO_SECT 0

/// One relocation as an object file stores it, which is an offset and a word
/// of packed fields.
struct relocation_info {
    i32 r_address;      /// Where in the section, from its start.
    /// On the little-endian targets Mach-O is defined for, this word packs
    /// r_symbolnum:24, r_pcrel:1, r_length:2, r_extern:1, r_type:4 from the
    /// low bit up.  Kept as one word so the layout does not depend on how the
    /// host compiler assigns bitfields.
    u32 r_word;
};

/// The fields inside r_word this reader takes out: which symbol or section,
/// how wide the field is as a log2, whether the number names a symbol or a
/// section, and what kind of relocation it is.  Whether the reference is
/// PC-relative, at bit 24, follows from the kind and is not read.
#define R_SYMBOLNUM(w)  ((w) & 0x00ffffff)
#define R_LENGTH(w)     (((w) >> 25) & 3)
#define R_EXTERN(w)     (((w) >> 27) & 1)
#define R_TYPE(w)       (((w) >> 28) & 0xf)

/// Set in r_address, not r_word, on the older relocation form that carries its
/// own target address.  Nothing here reads one.
#define R_SCATTERED     0x80000000

/// The x86-64 relocation types, in the order their numbering runs.
enum {
    X86_64_RELOC_UNSIGNED = 0,
    X86_64_RELOC_SIGNED,
    X86_64_RELOC_BRANCH,
    X86_64_RELOC_GOT_LOAD,
    X86_64_RELOC_GOT,
    X86_64_RELOC_SUBTRACTOR,
    X86_64_RELOC_SIGNED_1,
    X86_64_RELOC_SIGNED_2,
    X86_64_RELOC_SIGNED_4,
    X86_64_RELOC_TLV
};

/// And the arm64 ones.  ARM64_RELOC_ADDEND is not a relocation of its own: it
/// carries the addend for the one that follows it.
enum {
    ARM64_RELOC_UNSIGNED = 0,
    ARM64_RELOC_SUBTRACTOR,
    ARM64_RELOC_BRANCH26,
    ARM64_RELOC_PAGE21,
    ARM64_RELOC_PAGEOFF12,
    ARM64_RELOC_GOT_LOAD_PAGE21,
    ARM64_RELOC_GOT_LOAD_PAGEOFF12,
    ARM64_RELOC_POINTER_TO_GOT,
    ARM64_RELOC_TLVP_LOAD_PAGE21,
    ARM64_RELOC_TLVP_LOAD_PAGEOFF12,
    ARM64_RELOC_ADDEND
};

/// Where one input section ended up in the merged output.  Relocations are
/// read after the contents, and this is what tells them where the contents
/// went.
struct macho_sect {
    tli_Section *s;         /// NULL if the section was dropped.
    u64 off;  /// Where its contents start inside s.
    u64 addr;           /// The section's address in the object's own space.
    i32 sym;            /// Lazily made STT_SECTION symbol, 0 if none yet.
};

/// ---------------------------------------------------------------------------
/// tli_Section classification.

/// Is this section in the segment 'seg'?  A name field is sixteen bytes and
/// need not be terminated, which is why the length is given.
static i32 macho_seg_is(const struct section_64 *sc, const char *seg)
{
    return 0 == strncmp(sc->segname, seg, 16);
}

/// The same for the section's own name.
static i32 macho_sect_is(const struct section_64 *sc, const char *name)
{
    return 0 == strncmp(sc->sectname, name, 16);
}

/// Copy one of those sixteen byte fields somewhere it can be read as a string.
/// A name that fills the field leaves no NUL behind it -- __debug_str_offs is
/// exactly sixteen -- so %s on the field itself runs into the one after it.
static void macho_name(char out[17], const char *field)
{
    memcpy(out, field, 16);
    out[16] = '\0';
}

/// Decide what an input section becomes internally.  Returns 0 to drop it.
/// 'name' is filled with a NUL-terminated internal section name, and the two
/// out parameters with the ELF type and flags it takes on.
static i32 macho_classify(tli_State *s1, const struct section_64 *sc,
                          char name[32], i32 *sh_type, i32 *sh_flags)
{
    u32 type = sc->flags & SECTION_TYPE;
    char sect[17];

    macho_name(sect, sc->sectname);

    // Anything Apple marks as debug is for the real ld and the debugger.
    if (sc->flags & S_ATTR_DEBUG)
        return 0;

    // Debug information keeps its own name, and comes in only when it is
    // wanted: it is bulk that nothing else in the link depends on.
    if (macho_seg_is(sc, "__DWARF")) {
        if (!s1->do_debug)
            return 0;

        // __debug_info -> .debug_info
        snprintf(name, 32, ".%s", sect[0] == '_' && sect[1] == '_'
                                  ? sect + 2 : sect);
        *sh_type = SHT_PROGBITS;
        *sh_flags = 0;
        return 1;
    }

    // The frame descriptions, which the writer now has somewhere to put.  The
    // compact unwind table beside them is a different thing and still goes:
    // building the __unwind_info a loader reads it through means compressing
    // it into a two-level index, and without that the entries are bytes
    // nothing would look at.  Unwinding falls back to these descriptions.
    if (macho_sect_is(sc, "__compact_unwind"))
        return 0;

    if (macho_sect_is(sc, "__eh_frame")) {
        pstrcpy(name, 32, ".eh_frame");
        *sh_type = SHT_PROGBITS;
        *sh_flags = SHF_ALLOC;
        return 1;
    }

    if (type == S_ZEROFILL || type == S_GB_ZEROFILL) {
        pstrcpy(name, 32, ".bss");
        *sh_type = SHT_NOBITS;
        *sh_flags = SHF_ALLOC | SHF_WRITE;
        return 1;
    }

    // Thread-local storage, which is not ELF's model wearing other names.  A
    // __thread variable is a three word descriptor in __thread_vars holding a
    // resolver, a key dyld fills in, and the offset of the variable's initial
    // value; the value itself is in __thread_data, or in __thread_bss when it
    // is zero.  Every reference goes to the descriptor and calls the resolver
    // for the running thread's copy, so all three sections are ordinary data
    // to everything between here and the writer, and are kept apart only so
    // that the writer can give them back their Mach-O types.  Without that
    // __thread_bss would come through as .data and be filled with whatever is
    // at file offset zero, since a zerofill section occupies no file space.
    if (type == S_THREAD_LOCAL_VARIABLES || type == S_THREAD_LOCAL_REGULAR
        || type == S_THREAD_LOCAL_ZEROFILL) {
        pstrcpy(name, 32, type == S_THREAD_LOCAL_VARIABLES ? ".thread_vars"
                        : type == S_THREAD_LOCAL_REGULAR   ? ".thread_data"
                                                           : ".thread_bss");
        *sh_type = type == S_THREAD_LOCAL_ZEROFILL ? SHT_NOBITS : SHT_PROGBITS;
        *sh_flags = SHF_ALLOC | SHF_WRITE;
        return 1;
    }

    if (type == S_MOD_INIT_FUNC_POINTERS) {
        pstrcpy(name, 32, ".init_array");
        *sh_type = SHT_INIT_ARRAY;
        *sh_flags = SHF_ALLOC | SHF_WRITE;
        return 1;
    }

    if (type == S_MOD_TERM_FUNC_POINTERS) {
        pstrcpy(name, 32, ".fini_array");
        *sh_type = SHT_FINI_ARRAY;
        *sh_flags = SHF_ALLOC | SHF_WRITE;
        return 1;
    }

    // Everything from here on holds bytes of one kind or another; only the
    // flags it gets are still in question.
    *sh_type = SHT_PROGBITS;

    if (sc->flags & S_ATTR_PURE_INSTRUCTIONS || macho_sect_is(sc, "__text")) {
        pstrcpy(name, 32, ".text");
        *sh_flags = SHF_ALLOC | SHF_EXECINSTR;
        return 1;
    }

    // Read-only data, wherever Apple put it: string and numeric literals live
    // in __TEXT, and __DATA,__const is the relro half of __DATA.
    if (type == S_CSTRING_LITERALS || type == S_4BYTE_LITERALS
        || type == S_8BYTE_LITERALS || type == S_16BYTE_LITERALS
        || macho_sect_is(sc, "__const") || macho_sect_is(sc, "__ustring")
        || macho_sect_is(sc, "__gcc_except_tab")) {
        pstrcpy(name, 32, rdata);
        *sh_flags = shf_RELRO;
        return 1;
    }

    if (macho_seg_is(sc, "__DATA")) {
        pstrcpy(name, 32, ".data");
        *sh_flags = SHF_ALLOC | SHF_WRITE;
        return 1;
    }

    // Something we have no opinion about: keep it under its own name so its
    // symbols still resolve, and let it sit with the read-only data.
    snprintf(name, 32, ".%s", sect[0] == '_' && sect[1] == '_'
                              ? sect + 2 : sect);
    *sh_flags = SHF_ALLOC;
    return 1;
}

/// ---------------------------------------------------------------------------
/// Relocation helpers.

/// An STT_SECTION symbol for input section 'n' (1-based), made on demand.
/// Relocations with r_extern clear name a section rather than a symbol.
static i32 macho_section_sym(tli_State *s1, struct macho_sect *sm, i32 n, i32 nsects)
{
    struct macho_sect *m;

    if (n < 1 || n > nsects)
        return 0;

    m = &sm[n - 1];
    if (!m->s)
        return 0;

    if (!m->sym)
        m->sym = put_elf_sym(symtab_section, 0, 0,
                             ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0,
                             m->s->sh_num, NULL);
    return m->sym;
}

/// ARM64_RELOC_PAGEOFF12 covers both `add xN, xM, #lo12` and the unsigned
/// offset load/store forms, and the ELF relocation that means the same thing
/// depends on which.  The access size is in the instruction, so read it.
static i32 macho_lo12_reloc(u32 insn)
{
    // add/sub immediate: sf 0 0 100010 ...
    if ((insn & 0x1f000000) == 0x11000000)
        return R_AARCH64_ADD_ABS_LO12_NC;

    // ldr/str (immediate, unsigned offset), scalar or SIMD
    if ((insn & 0x3b000000) == 0x39000000) {
        i32 size = insn >> 30 & 3;      // 0=byte 1=half 2=word 3=dword
        i32 v = insn >> 26 & 1;         // SIMD&FP
        i32 opc1 = insn >> 23 & 1;      // with size==0 and v, selects 128-bit

        if (v && size == 0 && opc1)
            return R_AARCH64_LDST128_ABS_LO12_NC;

        switch (size) {
        case 0: return R_AARCH64_LDST8_ABS_LO12_NC;
        case 1: return R_AARCH64_LDST16_ABS_LO12_NC;
        case 2: return R_AARCH64_LDST32_ABS_LO12_NC;
        default: return R_AARCH64_LDST64_ABS_LO12_NC;
        }
    }

    // Not an addressing instruction we recognise.  Treating it as an ADD is
    // the harmless choice, since the immediate lands in the same bit positions.
    return R_AARCH64_ADD_ABS_LO12_NC;
}

/// Read the implicit addend a Mach-O relocation stores in its field, then
/// clear it.  tinylinker relocations are RELA and the relocate() routines
/// *add* to whatever the field already holds, so a leftover value would be
/// counted twice.  Every field this is asked for holds a plain number, a data
/// word or an x86-64 displacement; an arm64 instruction's immediate carries no
/// addend, ARM64_RELOC_ADDEND does.
static i64 macho_take_field(u8 *ptr, i32 length)
{
    i64 v;

    switch (length) {
    case 3:
        v = (i64)read64le(ptr);
        write64le(ptr, 0);
        break;
    case 2:
        v = (i32)read32le(ptr);
        write32le(ptr, 0);
        break;
    case 1:
        v = (i16)read16le(ptr);
        write16le(ptr, 0);
        break;
    default:
        v = (i8)*ptr;
        *ptr = 0;
        break;
    }
    return v;
}

/// What an arm64 relocation becomes internally.  The field, or for an
/// instruction the ARM64_RELOC_ADDEND before it ('pending'), is added to
/// '*addend'.  Returns -1 for a type with no equivalent.
static i32 macho_in_arm64(u8 *ptr, i32 type, i32 len, i64 pending,
                          i64 *addend)
{
    i32 elf_type;

    switch (type) {
    case ARM64_RELOC_UNSIGNED:
        elf_type = len == 3 ? R_AARCH64_ABS64 : R_AARCH64_ABS32;
        *addend += macho_take_field(ptr, len);
        break;
    case ARM64_RELOC_BRANCH26:
        // Mach-O has one branch relocation where ELF has two, and the
        // linker rebuilds the instruction from whichever it is told.
        // Only the instruction still knows: bit 31 separates bl from
        // b.  Reading CALL26 for both turns every tail call into a
        // call, which then returns into whatever follows it.
        elf_type = (read32le(ptr) & 0x80000000u) ? R_AARCH64_CALL26
                                                 : R_AARCH64_JUMP26;
        *addend += pending;
        break;
    case ARM64_RELOC_PAGE21:
        elf_type = R_AARCH64_ADR_PREL_PG_HI21;
        *addend += pending;
        break;
    case ARM64_RELOC_PAGEOFF12:
        elf_type = macho_lo12_reloc(read32le(ptr));
        *addend += pending;
        break;
    // A thread-local reference names the variable's descriptor and
    // reaches it through a pointer, which is what a GOT reference is,
    // so the TLVP pair is the GOT pair and is read as one.  Apple's ld
    // instead rewrites the ldr into an add, since a descriptor is
    // always in the image that refers to it and the indirection buys
    // nothing; the slot costs a pointer and a rebase, and needs no
    // instruction rewritten to earn it.
    case ARM64_RELOC_GOT_LOAD_PAGE21:
    case ARM64_RELOC_TLVP_LOAD_PAGE21:
        elf_type = R_AARCH64_ADR_GOT_PAGE;
        *addend += pending;
        break;
    case ARM64_RELOC_GOT_LOAD_PAGEOFF12:
    case ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
        elf_type = R_AARCH64_LD64_GOT_LO12_NC;
        *addend += pending;
        break;
    default:
        return -1;
    }
    return elf_type;
}

/// What an x86_64 relocation becomes internally, adding what its field holds
/// to '*addend'.  'here' is the field's address in the object's own space.
/// Returns -1 for a type with no equivalent.
static i32 macho_in_x86_64(u8 *ptr, i32 type, i32 len, i32 ext, i64 here,
                           i64 *addend)
{
    i32 elf_type;

    switch (type) {
    case X86_64_RELOC_UNSIGNED:
        elf_type = len == 3 ? R_X86_64_64 : R_X86_64_32;
        *addend += macho_take_field(ptr, len);
        break;
    case X86_64_RELOC_SIGNED:
    case X86_64_RELOC_SIGNED_1:
    case X86_64_RELOC_SIGNED_2:
    case X86_64_RELOC_SIGNED_4:
    case X86_64_RELOC_BRANCH:
    case X86_64_RELOC_GOT_LOAD:
    case X86_64_RELOC_GOT:
    // The thread-local form is a movq out of a pointer to the
    // variable's descriptor, measured from the end of the instruction
    // like every other reference here, so the GOT carries it on the
    // same terms as X86_64_RELOC_GOT_LOAD.  Apple's ld relaxes the
    // movq into a leaq and drops the slot; nothing obliges us to.
    case X86_64_RELOC_TLV: {
        // Mach-O measures a PC-relative reference from the end of the
        // instruction; ELF measures it from the field.  The difference
        // is the four bytes of the field itself, plus however many
        // immediate bytes follow it.
        static const i32 extra[] = {
            [X86_64_RELOC_SIGNED] = 0, [X86_64_RELOC_SIGNED_1] = 1,
            [X86_64_RELOC_SIGNED_2] = 2, [X86_64_RELOC_SIGNED_4] = 4,
            [X86_64_RELOC_BRANCH] = 0, [X86_64_RELOC_GOT_LOAD] = 0,
            [X86_64_RELOC_GOT] = 0, [X86_64_RELOC_TLV] = 0,
        };
        i64 field = macho_take_field(ptr, len);

        if (!ext) {
            // The field held the distance to an address in the
            // object's own space; recover that address first.
            field += here + 4 + extra[type];
        }
        *addend += field - 4 - extra[type];
        elf_type = type == X86_64_RELOC_BRANCH ? R_X86_64_PLT32
                 : (type == X86_64_RELOC_GOT_LOAD
                    || type == X86_64_RELOC_GOT
                    || type == X86_64_RELOC_TLV) ? R_X86_64_GOTPCREL
                 : R_X86_64_PC32;
        break;
    }
    default:
        return -1;
    }
    return elf_type;
}

/// ---------------------------------------------------------------------------
/// Reading an object.

/// Load one MH_OBJECT starting at 'file_offset' and merge it into s1.  The
/// offset is where a member of an archive begins; a file of its own starts at
/// zero.
ST_FUNC i32 macho_load_object(tli_State *s1, i32 fd, u64 file_offset)
{
    struct mach_header_64 mh;
    struct section_64 *sects = NULL;
    struct macho_sect *sm = NULL;
    struct nlist_64 *syms = NULL;
    struct relocation_info *rels = NULL;
    char *cmds = NULL, *strtab = NULL;
    i32 *old_to_new = NULL;
    u32 nsects = 0, nsyms = 0;
    u64 symoff = 0, stroff = 0, strsize = 0;
    u32 i;
    i32 j, ret = -1;
    i32 want_cpu = s1->arch->macho_cputype;
    unsigned long long file_size;

    // Sizes and offsets in the headers below are whatever the file says they
    // are.  Anything that occupies file space has to fit inside the file, and
    // knowing where the file ends is what makes that checkable.
    file_size = (unsigned long long)lseek(fd, 0, SEEK_END);

    lseek(fd, file_offset, SEEK_SET);
    if (full_read(fd, &mh, sizeof mh) != sizeof mh
        || mh.mh.magic != MH_MAGIC_64
        || mh.mh.filetype != MH_OBJECT)
        return tli_error_noabort("invalid Mach-O object file");

    if (mh.mh.cputype != want_cpu)
        return tli_error_noabort("Mach-O object is for a different architecture");

    // The load commands are read in one piece, so their total has to fit the
    // file before it becomes the size of that allocation.
    if ((u64)mh.mh.sizeofcmds > file_size)
        return tli_error_noabort("Mach-O load commands are larger than the "
                                 "file holding them");

    cmds = load_data(fd, file_offset + sizeof mh, mh.mh.sizeofcmds);

    // An object has one segment holding every section, plus a symbol table.
    for (i = 0, j = 0; i < mh.mh.ncmds; i++) {
        struct load_command *lc = (struct load_command *)(cmds + j);

        // The whole command, not just its head, has to lie inside what was
        // read: everything below indexes it without looking again.
        if ((u64)j + sizeof *lc > mh.mh.sizeofcmds || lc->cmdsize == 0
            || (u64)j + lc->cmdsize > mh.mh.sizeofcmds) {
            tli_error_noabort("malformed Mach-O load commands");
            goto done;
        }

        if (lc->cmd == LC_SEGMENT_64) {
            struct segment_command_64 *sg = (struct segment_command_64 *)lc;

            if (sects) {
                tli_error_noabort("Mach-O object has more than one segment");
                goto done;
            }

            // A segment's section headers follow it inside its own command,
            // which is what bounds how many of them there can be.  Unchecked,
            // nsects is both an allocation below and an index off the end of
            // the load commands.
            if (lc->cmdsize < sizeof *sg
                || (lc->cmdsize - sizeof *sg) / sizeof(struct section_64)
                       < sg->nsects) {
                tli_error_noabort("Mach-O segment claims %u sections, more "
                                  "than its load command holds", sg->nsects);
                goto done;
            }

            nsects = sg->nsects;
            sects = (struct section_64 *)(sg + 1);
        } else if (lc->cmd == LC_SYMTAB) {
            struct symtab_command *st = (struct symtab_command *)lc;

            nsyms = st->nsyms;
            symoff = st->symoff;
            stroff = st->stroff;
            strsize = st->strsize;
        }
        j += lc->cmdsize;
    }

    sm = tli_mallocz(sizeof *sm * (nsects ? nsects : 1));

    // Pass one: bring the section contents in.
    for (i = 0; i < nsects; i++) {
        struct section_64 *sc = &sects[i];
        char name[32];
        i32 sh_type, sh_flags, align;
        tli_Section *s = NULL;

        sm[i].addr = sc->addr;
        if (!macho_classify(s1, sc, name, &sh_type, &sh_flags))
            continue;

        // Everything of one internal name merges into one section, which is
        // where the contents of several objects come together.
        for (j = 1; j < s1->nb_sections; j++) {
            if (0 == strcmp(s1->sections[j]->name, name)) {
                s = s1->sections[j];
                break;
            }
        }

        if (!s)
            s = new_section(s1, name, sh_type, sh_flags);

        // A zerofill section occupies no file space, so only the sections
        // that do are measured against the file.  Without this a damaged size
        // reaches section_add, which allocates and zeroes it: on a 64-bit
        // size that is not an error the caller ever sees, just a linker that
        // never comes back.
        if (sh_type != SHT_NOBITS
            && ((unsigned long long)sc->offset > file_size
                || (unsigned long long)sc->size
                       > file_size - (unsigned long long)sc->offset)) {
            tli_error_noabort("section '%s' claims %llu bytes at %llu, "
                              "past the end of a %llu byte file", name,
                              (unsigned long long)sc->size,
                              (unsigned long long)sc->offset, file_size);
            goto done;
        }

        // Reserve room at the end of the merged section, and remember where
        // this object's copy landed; everything else here is measured from it.
        align = sc->align < 30 ? 1 << sc->align : 1;
        sm[i].s = s;
        sm[i].off = section_add(s, sc->size, align);
        if (align > s->sh_addralign)
            s->sh_addralign = align;

        if (sh_type != SHT_NOBITS && sc->size) {
            lseek(fd, file_offset + sc->offset, SEEK_SET);
            full_read(fd, s->data + sm[i].off, sc->size);
        }

        // keep the next object's code aligned to an instruction
        if (s1->arch->em == EM_AARCH64 && (s->sh_flags & SHF_EXECINSTR))
            section_add(s, 0, 4);
    }

    // Pass two: symbols, which need the sections to exist first.
    if (nsyms) {

        // These counts come from the file as well, and reach load_data() as
        // allocation sizes; a damaged nsyms otherwise asks for tens of
        // gigabytes the same way a damaged section size does.
        if (symoff > file_size
            || (u64)nsyms * sizeof *syms > file_size - symoff
            || stroff > file_size
            || strsize > file_size - stroff) {
            tli_error_noabort("the symbol table lies outside a %llu byte file",
                              file_size);
            goto done;
        }

        syms = load_data(fd, file_offset + symoff, sizeof *syms * nsyms);
        strtab = load_data(fd, file_offset + stroff, strsize);
        old_to_new = tli_mallocz(sizeof *old_to_new * nsyms);

        for (i = 0; i < nsyms; i++) {
            struct nlist_64 *nl = &syms[i];
            const char *name;
            addr_t value = 0;
            u64 size = 0;
            i32 bind, type = STT_NOTYPE, other = 0, shndx;

            if (nl->n_type & N_STAB) // debugger entry, not a linker symbol
                continue;

            if (nl->n_strx >= strsize)
                continue;

            name = strtab + nl->n_strx;

            // Where the symbol is: nowhere, at a fixed address, or in one of
            // the sections just read.
            switch (nl->n_type & N_TYPE) {
            case N_UNDF:
                if (nl->n_value) {
                    // a tentative definition: n_value is its size and the
                    // alignment is stashed as a log2 in n_desc
                    shndx = SHN_COMMON;
                    value = (addr_t)1 << ((nl->n_desc >> 8) & 0x0f);
                    size = nl->n_value;
                    type = STT_OBJECT;
                } else {
                    shndx = SHN_UNDEF;
                }
                break;
            case N_ABS:
                shndx = SHN_ABS;
                value = nl->n_value;
                break;
            case N_SECT:
                if (nl->n_sect == NO_SECT || nl->n_sect > nsects
                    || !sm[nl->n_sect - 1].s)
                    continue; // defined in a section we dropped
                shndx = sm[nl->n_sect - 1].s->sh_num;
                value = nl->n_value - sm[nl->n_sect - 1].addr
                        + sm[nl->n_sect - 1].off;
                type = (sm[nl->n_sect - 1].s->sh_flags & SHF_EXECINSTR)
                       ? STT_FUNC : STT_OBJECT;
                break;
            default:
                continue; // N_PBUD / N_INDR do not appear in objects
            }

            // And how far it can be seen from.
            if (!(nl->n_type & N_EXT))
                bind = STB_LOCAL;
            else if (nl->n_desc & N_WEAK_DEF)
                bind = STB_WEAK;
            else
                bind = STB_GLOBAL;

            // A private extern is visible to this link and no further, which
            // is what hidden visibility means.
            if (nl->n_type & N_PEXT) {
                bind = STB_GLOBAL;
                other = STV_HIDDEN;
            }

            old_to_new[i] = set_elf_sym(symtab_section, value, size,
                                        ELFW(ST_INFO)(bind, type), other,
                                        shndx, name);
        }
    }

    // Pass three: relocations.
    for (i = 0; i < nsects; i++) {
        struct section_64 *sc = &sects[i];
        tli_Section *s = sm[i].s;
        i32 sub_sym = 0, have_sub = 0;
        i64 addend_pending = 0;
        i32 have_addend = 0;
        u32 r;

        if (!s || !sc->nreloc)
            continue;

        if (s->sh_type == SHT_NOBITS)
            continue;

        // And the relocation count, for the same reason.
        if ((u64)sc->reloff > file_size
            || (u64)sc->nreloc * sizeof *rels
                   > file_size - (u64)sc->reloff) {
            tli_error_noabort("section '%.16s' claims %u relocations past the "
                              "end of a %llu byte file", sc->sectname,
                              sc->nreloc, file_size);
            goto done;
        }

        rels = load_data(fd, file_offset + sc->reloff,
                         sizeof *rels * sc->nreloc);

        for (r = 0; r < sc->nreloc; r++) {
            struct relocation_info *ri = &rels[r];
            u64 off = sm[i].off + (u32)ri->r_address;
            u8 *ptr = s->data + off;
            i32 type = R_TYPE(ri->r_word);
            i32 len = R_LENGTH(ri->r_word);
            i32 ext = R_EXTERN(ri->r_word);
            i32 num = R_SYMBOLNUM(ri->r_word);
            i32 sym_index, elf_type = 0;
            i64 addend;

            if ((u32)ri->r_address & R_SCATTERED) {
                tli_error_noabort("%s: scattered relocations are not supported",
                                  s1->current_filename);
                goto done;
            }

            // r_address came out of the file too, and several of the types
            // below write the field back, so an unchecked one is a write
            // anywhere in the process.  r_length says how wide the field is.
            if (off > s->data_offset
                || s->data_offset - off < ((u64)1 << len)) {
                tli_error_noabort("%s: relocation at 0x%llx is past the end of "
                                  "'%s' (0x%llx)", s1->current_filename,
                                  (unsigned long long)off, s->name,
                                  (unsigned long long)s->data_offset);
                goto done;
            }

            // Carries the addend for the relocation that follows it.
            if (s1->arch->em == EM_AARCH64 && type == ARM64_RELOC_ADDEND) {
                addend_pending = num;
                have_addend = 1;
                continue;
            }

            // Resolve what is being referred to, and the offset from it that
            // the object encoded.
            if (ext) {
                if ((u32)num >= nsyms || !old_to_new)
                    goto bad_reloc;

                sym_index = old_to_new[num];
                if (!sym_index)
                    goto bad_reloc;

                addend = 0;
            } else {
                sym_index = macho_section_sym(s1, sm, num, nsects);
                if (!sym_index)
                    goto bad_reloc;

                // The reference is to an address in the object's own space;
                // rebase it onto where that section ended up.
                addend = -(i64)sm[num - 1].addr + (i64)sm[num - 1].off;
            }

            // A SUBTRACTOR/UNSIGNED pair encodes "B - A".
            if ((s1->arch->em == EM_AARCH64 && type == ARM64_RELOC_SUBTRACTOR)
                || (s1->arch->em == EM_X86_64
                    && type == X86_64_RELOC_SUBTRACTOR)) {
                sub_sym = sym_index;
                have_sub = 1;
                continue;
            }

            // The relocation after a SUBTRACTOR is the other half of it.
            if (have_sub) {
                ElfW(Sym) *a = &((ElfW(Sym) *)symtab_section->data)[sub_sym];
                ElfW(Sym) *b = &((ElfW(Sym) *)symtab_section->data)[sym_index];

                have_sub = 0;
                if (a->st_shndx == b->st_shndx && a->st_shndx != SHN_UNDEF
                    && a->st_shndx < SHN_LORESERVE) {
                    // Both ended up in the same output section, so their
                    // difference is already final: fold it in and emit no
                    // relocation.
                    i64 d = (i64)b->st_value - (i64)a->st_value;

                    if (len == 3)
                        write64le(ptr, read64le(ptr) + d);
                    else
                        write32le(ptr, read32le(ptr) + (i32)d);

                    continue;
                }

                // Different sections, so the difference is not a number yet:
                // it becomes one only once both sections have addresses.  A
                // relocatable output never gets those, so that case keeps the
                // refusal rather than deferring to a pass that will not run.
                if (s1->output_type == TLI_OUTPUT_OBJ) {
                    tli_error_noabort(
                        "%s: difference between symbols in different sections "
                        "cannot be re-emitted into a relocatable object",
                        s1->current_filename);
                    goto done;
                }

                tli_add_sym_diff(s1, s->sh_num, (usize)off,
                                 sub_sym, sym_index, len);
                continue;
            }

            // What the relocation becomes internally, and what the field it
            // covers contributes to the addend.  Mach-O is refused for any
            // machine but these two.
            switch (s1->arch->em) {
            case EM_AARCH64:
                elf_type = macho_in_arm64(ptr, type, len,
                                          have_addend ? addend_pending : 0,
                                          &addend);
                break;
            case EM_X86_64:
                elf_type = macho_in_x86_64(ptr, type, len, ext,
                                           (i64)sc->addr + (u32)ri->r_address,
                                           &addend);
                break;
            default:
                elf_type = -1;
                break;
            }
            if (elf_type < 0) {
                tli_error_noabort("%s: unsupported Mach-O relocation type %d",
                                  s1->current_filename, type);
                goto done;
            }

            have_addend = 0;
            addend_pending = 0;
            put_elf_reloca(symtab_section, s, off, elf_type, sym_index,
                           (addr_t)addend);
            continue;

        bad_reloc:
            tli_error_noabort("%s: invalid relocation in section %.16s",
                              s1->current_filename, sc->sectname);
            goto done;
        }
        tli_free(rels);
        rels = NULL;
    }

    ret = s1->nb_errors ? -1 : 0;

done:
    tli_free(rels);
    tli_free(old_to_new);
    tli_free(strtab);
    tli_free(syms);
    tli_free(sm);
    tli_free(cmds);
    return ret;
}

/// Does this look like a Mach-O relocatable object we can read?  The caller has
/// only the first few bytes of the file, which is enough for the header.
ST_FUNC i32 macho_object_type(const void *hdr, i32 size)
{
    const struct mach_header_64 *mh = hdr;

    if (size < (i32)sizeof *mh)
        return 0;

    return mh->mh.magic == MH_MAGIC_64 && mh->mh.filetype == MH_OBJECT;
}


/// ---------------------------------------------------------------------------
/// Writing an MH_OBJECT.
///
/// The inverse of everything above: take the merged sections, symbols and
/// relocations and write them back out as a Mach-O relocatable object, so that
/// `-r` on a Mach-O target produces something the platform's own tools accept.

/// One section on its way out, with the header the file will carry and the
/// relocations translated for it.
struct macho_osect {
    tli_Section *s;                   /// The internal section it comes from.
    struct section_64 sc;         /// The header, filled in as the layout runs.
    struct relocation_info *rel;  /// Its relocations in Mach-O form.
    u32 nreloc;                   /// How many, which may exceed the ELF count.
    i32 sym;                      /// Index of the label at its start.
};

/// Give 'sc' the segment, section name and flags Mach-O expects for the
/// internal section 's'.  A name with no counterpart keeps its own, since
/// nothing but the tools reads it back.
static void macho_out_name(const tli_Section *s, struct section_64 *sc)
{
    const char *seg = "__DATA", *sect = NULL;
    u32 flags = S_REGULAR;
    char buf[24];

    if (0 == strcmp(s->name, ".text")) {
        seg = "__TEXT", sect = "__text";
        flags |= S_ATTR_PURE_INSTRUCTIONS | S_ATTR_SOME_INSTRUCTIONS;
    } else if (0 == strcmp(s->name, ".data")) {
        sect = "__data";
    } else if (0 == strcmp(s->name, ".bss")) {
        sect = "__bss", flags = S_ZEROFILL;
    } else if (0 == strcmp(s->name, ".data.ro")) {
        // rdata as a Mach-O link names it.  __DATA,__const rather than
        // __TEXT,__const: this section holds merged read-only data, and the relro pointer tables among it need
        // relocating, which ld rejects inside __TEXT.
        sect = "__const";
    } else if (0 == strcmp(s->name, ".thread_vars")) {
        // The section type is the only thing that says a section holds
        // thread-local anything, here as in the image: written as S_REGULAR
        // these three would read back as ordinary data, and the descriptors
        // in them would never be recognised.
        sect = "__thread_vars", flags = S_THREAD_LOCAL_VARIABLES;
    } else if (0 == strcmp(s->name, ".thread_data")) {
        sect = "__thread_data", flags = S_THREAD_LOCAL_REGULAR;
    } else if (0 == strcmp(s->name, ".thread_bss")) {
        sect = "__thread_bss", flags = S_THREAD_LOCAL_ZEROFILL;
    } else if (0 == strcmp(s->name, ".init_array")) {
        sect = "__mod_init_func", flags = S_MOD_INIT_FUNC_POINTERS;
    } else if (0 == strcmp(s->name, ".fini_array")) {
        sect = "__mod_term_func", flags = S_MOD_TERM_FUNC_POINTERS;
    } else if (0 == strncmp(s->name, ".debug_", 7)) {
        seg = "__DWARF", flags |= S_ATTR_DEBUG;
    }

    if (!sect) {
        // ".debug_info" -> "__debug_info", ".foo" -> "__foo"
        snprintf(buf, sizeof buf, "__%s", s->name + (s->name[0] == '.'));
        sect = buf;
    }

    // A name field is filled rather than terminated, so both are cleared first.
    memset(sc->segname, 0, sizeof sc->segname);
    memset(sc->sectname, 0, sizeof sc->sectname);
    memcpy(sc->segname, seg, strnlen(seg, sizeof sc->segname));
    memcpy(sc->sectname, sect, strnlen(sect, sizeof sc->sectname));
    sc->flags = flags;
}

/// Does this section's contents belong in the object?  Everything mapped does,
/// and debug information, which is not mapped but is still wanted.
static i32 macho_out_wanted(const tli_Section *s)
{
    if (s->sh_flags & SHF_ALLOC)
        return s->sh_type == SHT_PROGBITS || s->sh_type == SHT_NOBITS
            || s->sh_type == SHT_INIT_ARRAY || s->sh_type == SHT_FINI_ARRAY;

    return 0 == strncmp(s->name, ".debug_", 7);
}

/// The log2 of 'n' rounded up, which is how Mach-O stores an alignment.  The
/// bound keeps a nonsensical size from spinning here.
static i32 macho_log2(u32 n)
{
    i32 i = 0;

    while ((1u << i) < n && i < 30)
        i++;

    return i;
}

/// The arm64 half of macho_out_reloc(), which has set the defaults.
static i32 macho_out_arm64(u8 *ptr, i32 elf_type, i64 addend,
                           i32 *len, i32 *pcrel, i32 *need_addend)
{
    switch (elf_type) {
    case R_AARCH64_ABS64:
        *len = 3;
        write64le(ptr, (u64)addend);
        return ARM64_RELOC_UNSIGNED;
    case R_AARCH64_ABS32:
        write32le(ptr, (u32)addend);
        return ARM64_RELOC_UNSIGNED;
    case R_AARCH64_CALL26:
    case R_AARCH64_JUMP26:
        *pcrel = 1, *need_addend = addend != 0;
        return ARM64_RELOC_BRANCH26;
    case R_AARCH64_ADR_PREL_PG_HI21:
        *pcrel = 1, *need_addend = addend != 0;
        return ARM64_RELOC_PAGE21;
    case R_AARCH64_ADD_ABS_LO12_NC:
    case R_AARCH64_LDST8_ABS_LO12_NC:
    case R_AARCH64_LDST16_ABS_LO12_NC:
    case R_AARCH64_LDST32_ABS_LO12_NC:
    case R_AARCH64_LDST64_ABS_LO12_NC:
    case R_AARCH64_LDST128_ABS_LO12_NC:
        *need_addend = addend != 0;
        return ARM64_RELOC_PAGEOFF12;
    case R_AARCH64_ADR_GOT_PAGE:
        *pcrel = 1, *need_addend = addend != 0;
        return ARM64_RELOC_GOT_LOAD_PAGE21;
    case R_AARCH64_LD64_GOT_LO12_NC:
        *need_addend = addend != 0;
        return ARM64_RELOC_GOT_LOAD_PAGEOFF12;

    // The initial-exec pair loads its word the way the TLVP pair loads a
    // descriptor's address, so a generator's thread-local access is written
    // as the one Apple's ld expects. Read back, it becomes the GOT pair.
    case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
        *pcrel = 1, *need_addend = addend != 0;
        return ARM64_RELOC_TLVP_LOAD_PAGE21;
    case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
        *need_addend = addend != 0;
        return ARM64_RELOC_TLVP_LOAD_PAGEOFF12;
    default:
        return -1;
    }
}

/// The x86_64 half of macho_out_reloc(), which has set the defaults.
static i32 macho_out_x86_64(u8 *ptr, i32 elf_type, i64 addend,
                            i32 *len, i32 *pcrel, i32 *need_addend)
{
    (void)need_addend;

    switch (elf_type) {
    case R_X86_64_64:
        *len = 3;
        write64le(ptr, (u64)addend);
        return X86_64_RELOC_UNSIGNED;
    case R_X86_64_32:
    case R_X86_64_32S:
        write32le(ptr, (u32)addend);
        return X86_64_RELOC_UNSIGNED;
    case R_X86_64_PC32:
        *pcrel = 1;
        write32le(ptr, (u32)(addend + 4));
        return X86_64_RELOC_SIGNED;
    case R_X86_64_PLT32:
        *pcrel = 1;
        write32le(ptr, (u32)(addend + 4));
        return X86_64_RELOC_BRANCH;
    case R_X86_64_GOTPCREL:
    case R_X86_64_GOTPCRELX:
    case R_X86_64_REX_GOTPCRELX:
        *pcrel = 1;
        write32le(ptr, (u32)(addend + 4));
        return X86_64_RELOC_GOT_LOAD;

    // The same movq, of a descriptor's address rather than a GOT slot.
    case R_X86_64_GOTTPOFF:
        *pcrel = 1;
        write32le(ptr, (u32)(addend + 4));
        return X86_64_RELOC_TLV;
    default:
        return -1;
    }
}

/// Translate one internal relocation into its Mach-O form, writing the addend
/// back into the field the way Mach-O carries it.  Returns the Mach-O type, or
/// -1 if there is no equivalent.  '*need_addend' asks for a preceding
/// ARM64_RELOC_ADDEND, which is how arm64 carries an addend that will not fit
/// in an instruction.
static i32 macho_out_reloc(tli_State *s1, u8 *ptr, i32 elf_type, i64 addend,
                           i32 *len, i32 *pcrel, i32 *need_addend)
{
    *pcrel = 0;
    *len = 2;
    *need_addend = 0;

    // Mach-O is refused for any machine but these two.
    switch (s1->arch->em) {
    case EM_AARCH64:
        return macho_out_arm64(ptr, elf_type, addend, len, pcrel, need_addend);
    case EM_X86_64:
        return macho_out_x86_64(ptr, elf_type, addend, len, pcrel,
                                need_addend);
    }
    return -1;
}

/// A growable string table, which the symbol names and nothing else use.
struct macho_strtab {
    char *data;        /// The names, each terminated, one after another.
    u32 size, alloc;   /// How much is in it, and how much room there is.
};

/// Append 's' to the table and return the offset it went to, which is what an
/// nlist entry stores in place of the name.
static u32 macho_str(struct macho_strtab *st, const char *s)
{
    u32 len = strlen(s) + 1, at = st->size;

    if (st->size + len > st->alloc) {
        while (st->size + len > st->alloc)
            st->alloc = st->alloc ? st->alloc * 2 : 256;

        st->data = tli_realloc(st->data, st->alloc);
    }

    memcpy(st->data + at, s, len);
    st->size += len;
    return at;
}

/// Write everything read so far back out as one relocatable object, which is
/// what -r asks for.  Nothing is resolved: the symbols and relocations are
/// translated, not applied.
ST_FUNC i32 macho_output_obj(tli_State *s1, const char *filename)
{
    struct macho_osect *os;
    struct nlist_64 *nl;
    i32 *sym_map;
    struct macho_strtab strt = {0};
    struct mach_header_64 mh;
    struct segment_command_64 sg;
    struct symtab_command sym_cmd;
    struct build_version_command bv;
    u64 off, cmds_size, vmsize = 0, data_start = 0, data_end = 0;
    i32 i, j, k, n = 0, nsyms_in, nsyms = 0, ret = -1;
    FILE *fp;

    // Which sections go out, and in what order.
    os = tli_mallocz(sizeof *os * s1->nb_sections);
    for (i = 1; i < s1->nb_sections; i++) {
        tli_Section *s = s1->sections[i];

        if (!macho_out_wanted(s))
            continue;

        os[n].s = s;
        macho_out_name(s, &os[n].sc);
        os[n].sc.size = s->data_offset;
        os[n].sc.align = macho_log2(s->sh_addralign ? s->sh_addralign : 1);
        n++;
    }

    // An object's sections share one flat address space, and a symbol's
    // n_value is an address in it rather than an offset into its section.
    // Work those addresses out before anything needs them.
    for (i = 0; i < n; i++) {
        vmsize = macho_align(vmsize, (u64)1 << os[i].sc.align);
        os[i].sc.addr = vmsize;
        vmsize += os[i].sc.size;
    }

    // Symbols, in the order LC_DYSYMTAB would describe: local, then defined
    // external, then undefined.  Relocations index into this, so remember
    // where each one moved to.
    nsyms_in = symtab_section->data_offset / sizeof(ElfW(Sym));

    // room for one label per section as well as the symbols themselves
    nl = tli_mallocz(sizeof *nl * (nsyms_in + n + 1));
    sym_map = tli_mallocz(sizeof *sym_map * (nsyms_in ? nsyms_in : 1));
    macho_str(&strt, "");

    // A label at the start of each section, which is what a relocation names
    // when it refers to a section rather than to a symbol.
    for (i = 0; i < n; i++) {
        char buf[16];

        snprintf(buf, sizeof buf, "ltmp%d", i);
        k = nsyms++;
        nl[k].n_strx = macho_str(&strt, buf);
        nl[k].n_type = N_SECT;
        nl[k].n_sect = i + 1;
        nl[k].n_desc = 0;
        nl[k].n_value = os[i].sc.addr;
        os[i].sym = k;
    }

    // Then three passes over the ELF table, one per run, so that the entries
    // come out in the order LC_DYSYMTAB would describe.
    for (j = 0; j < 3; j++) {
        for (i = 1; i < nsyms_in; i++) {
            ElfW(Sym) *es = &((ElfW(Sym) *)symtab_section->data)[i];
            const char *name = (char *)symtab_section->link->data + es->st_name;
            i32 bind = ELFW(ST_BIND)(es->st_info);
            i32 undef = es->st_shndx == SHN_UNDEF || es->st_shndx == SHN_COMMON;
            i32 kind = undef ? 2 : bind == STB_LOCAL ? 0 : 1;
            i32 sect = 0;

            if (ELFW(ST_TYPE)(es->st_info) == STT_SECTION || !name[0]
                || kind != j)
                continue;

            // A symbol defined in a section names it by number, so the section
            // has to be one of the ones going out.
            if (es->st_shndx != SHN_UNDEF && es->st_shndx != SHN_COMMON
                && es->st_shndx != SHN_ABS) {
                for (sect = 0; sect < n; sect++)
                    if (os[sect].s == s1->sections[es->st_shndx])
                        break;

                if (sect == n)
                    continue; // defined in a section we are not writing
            }

            k = nsyms++;
            nl[k].n_strx = macho_str(&strt, name);
            nl[k].n_desc = 0;
            nl[k].n_value = 0;
            if (es->st_shndx == SHN_UNDEF) {
                nl[k].n_type = N_UNDF | N_EXT;
                nl[k].n_sect = NO_SECT;
            } else if (es->st_shndx == SHN_COMMON) {
                // Mach-O spells a tentative definition as an undefined symbol
                // with a size, and keeps the alignment in n_desc.
                nl[k].n_type = N_UNDF | N_EXT;
                nl[k].n_sect = NO_SECT;
                nl[k].n_value = es->st_size;
                nl[k].n_desc = macho_log2(es->st_value ? es->st_value : 1) << 8;
            } else if (es->st_shndx == SHN_ABS) {
                nl[k].n_type = N_ABS | (bind != STB_LOCAL ? N_EXT : 0);
                nl[k].n_sect = NO_SECT;
                nl[k].n_value = es->st_value;
            } else {
                nl[k].n_type = N_SECT | (bind != STB_LOCAL ? N_EXT : 0);
                nl[k].n_sect = sect + 1;
                nl[k].n_value = os[sect].sc.addr + es->st_value;
            }
            if (bind == STB_WEAK)
                nl[k].n_desc |= N_WEAK_DEF;

            sym_map[i] = k;
        }
    }

    // Relocations, translated section by section.
    for (i = 0; i < n; i++) {
        tli_Section *sr = os[i].s->reloc;
        ElfW_Rel *r;
        u32 max;

        if (!sr || os[i].s->sh_type == SHT_NOBITS)
            continue;

        max = sr->data_offset / sizeof *r;

        // worst case one ARM64_RELOC_ADDEND for every entry
        os[i].rel = tli_mallocz(sizeof *os[i].rel * max * 2);

        for_each_elem(sr, 0, r, ElfW_Rel) {
            i32 elf_type = ELFW(R_TYPE)(r->r_info);
            i32 es = ELFW(R_SYM)(r->r_info);
            ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[es];
            i32 len, pcrel, need_addend, mtype, num, ext;
            i32 j;
            i64 addend = 0;
            struct relocation_info *ri;

#if SHT_RELX == SHT_RELA
            addend = r->r_addend;
#endif

            // Debug information is the exception. dsymutil and lldb read a
            // relocation in a __DWARF section as naming a section, with the
            // target's address in the field, and ignore one that names a
            // symbol. A line table relocated the other way is read as
            // beginning at zero, and every function then appears to start
            // where the first one does.
            if (0 == strncmp(os[i].s->name, ".debug", 6)) {
                for (j = 0; j < n; j++)
                    if (os[j].s == s1->sections[sym->st_shndx])
                        break;

                if (j == n)
                    continue;

                ext = 0;
                num = j + 1;            // Mach-O numbers sections from one
                addend += sym->st_value;
                goto emit;
            }

            // Everywhere else a relocation names a symbol: a reference to a
            // section is expressed as one to the label at its start, rather
            // than as a section-number relocation, which would put the
            // target's address in the field instead of an addend.
            ext = 1;
            if (ELFW(ST_TYPE)(sym->st_info) == STT_SECTION) {
                for (j = 0; j < n; j++)
                    if (os[j].s == s1->sections[sym->st_shndx])
                        break;

                if (j == n)
                    continue;

                addend += sym->st_value;
                num = os[j].sym;
            } else {
                num = sym_map[es];
                if (!num && es)
                    continue; // symbol we did not write
            }

        emit:
            mtype = macho_out_reloc(s1, os[i].s->data + r->r_offset,
                                    elf_type, addend, &len, &pcrel,
                                    &need_addend);
            if (mtype < 0) {
                tli_error_noabort("cannot express relocation type %d in Mach-O",
                                  elf_type);
                goto done;
            }

            // An addend an instruction cannot hold goes in front of the
            // relocation it belongs to, in one of its own.  Only arm64 asks.
            if (need_addend) {
                ri = &os[i].rel[os[i].nreloc++];
                ri->r_address = r->r_offset;
                ri->r_word = ((u32)addend & 0x00ffffff)
                             | (2u << 25) | ((u32)ARM64_RELOC_ADDEND << 28);
            }

            ri = &os[i].rel[os[i].nreloc++];
            ri->r_address = r->r_offset;
            ri->r_word = ((u32)num & 0x00ffffff)
                         | ((u32)pcrel << 24)
                         | ((u32)len << 25)
                         | ((u32)ext << 27)
                         | ((u32)mtype << 28);
        }
    }

    // Lay the file out: header, load commands, section data, relocations,
    // symbol table, string table.
    cmds_size = sizeof sg + (u64)n * sizeof(struct section_64)
                + sizeof bv + sizeof sym_cmd;
    off = macho_align(sizeof mh + cmds_size, 8);

    // The segment covers the section contents.  Its file offset is where the
    // first section actually starts, not where the load commands happened to
    // end: any alignment padding in between belongs to neither, and counting
    // it would make filesize exceed vmsize.
    data_start = data_end = off;
    for (i = 0; i < n; i++) {
        if (os[i].s->sh_type == SHT_NOBITS) {
            os[i].sc.offset = 0;
        } else {
            off = macho_align(off, (u64)1 << os[i].sc.align);
            if (data_end == data_start)
                data_start = off;
            os[i].sc.offset = off;
            off += os[i].sc.size;
            data_end = off;
        }
    }

    // Then the relocations, section by section, behind all of the contents.
    for (i = 0; i < n; i++) {
        if (!os[i].nreloc)
            continue;

        off = macho_align(off, 4);
        os[i].sc.reloff = off;
        os[i].sc.nreloc = os[i].nreloc;
        off += (u64)os[i].nreloc * sizeof(struct relocation_info);
    }

    // And the symbol table and its names, last of all.
    off = macho_align(off, 8);
    sym_cmd.cmd = LC_SYMTAB;
    sym_cmd.cmdsize = sizeof sym_cmd;
    sym_cmd.symoff = off;
    sym_cmd.nsyms = nsyms;
    off += (u64)nsyms * sizeof *nl;
    sym_cmd.stroff = off;
    sym_cmd.strsize = strt.size;

    // The header and the three load commands: the segment, a build version,
    // and the symbol table.
    memset(&mh, 0, sizeof mh);
    mh.mh.magic = MH_MAGIC_64;
    mh.mh.cputype = s1->arch->macho_cputype;
    mh.mh.cpusubtype = s1->arch->macho_cpusubtype;
    mh.mh.filetype = MH_OBJECT;
    mh.mh.ncmds = 3;
    mh.mh.sizeofcmds = cmds_size;

    memset(&sg, 0, sizeof sg);
    sg.cmd = LC_SEGMENT_64;
    sg.cmdsize = sizeof sg + (u64)n * sizeof(struct section_64);

    // An object's one segment is nameless and starts at zero.  It covers the
    // section contents only, not the relocations or symbol table that follow
    // them in the file.
    sg.vmsize = vmsize;
    sg.fileoff = data_start;
    sg.filesize = data_end - data_start;
    sg.maxprot = sg.initprot = 7;
    sg.nsects = n;

    unlink(filename);
    fp = fopen(filename, "wb");
    if (!fp) {
        tli_error_noabort("could not write '%s': %s", filename, strerror(errno));
        goto done;
    }

    // The header and the commands, in the order their sizes were counted.
    fwrite(&mh, sizeof mh, 1, fp);
    fwrite(&sg, sizeof sg, 1, fp);
    for (i = 0; i < n; i++)
        fwrite(&os[i].sc, sizeof os[i].sc, 1, fp);

    memset(&bv, 0, sizeof bv);
    bv.cmd = LC_BUILD_VERSION;
    bv.cmdsize = sizeof bv;
    bv.platform = PLATFORM_MACOS;
    bv.minos = 10 << 16;
    bv.sdk = 10 << 16;
    bv.ntools = 0;
    fwrite(&bv, sizeof bv, 1, fp);
    fwrite(&sym_cmd, sizeof sym_cmd, 1, fp);

    // Then everything the layout gave an offset to, seeking to each in turn
    // rather than padding between them.
    for (i = 0; i < n; i++) {
        if (os[i].s->sh_type == SHT_NOBITS || !os[i].sc.size)
            continue;

        fseek(fp, os[i].sc.offset, SEEK_SET);
        fwrite(os[i].s->data, os[i].sc.size, 1, fp);
    }

    for (i = 0; i < n; i++) {
        if (!os[i].nreloc)
            continue;

        fseek(fp, os[i].sc.reloff, SEEK_SET);
        fwrite(os[i].rel, sizeof(struct relocation_info), os[i].nreloc, fp);
    }

    fseek(fp, sym_cmd.symoff, SEEK_SET);
    fwrite(nl, sizeof *nl, nsyms, fp);
    fseek(fp, sym_cmd.stroff, SEEK_SET);
    fwrite(strt.data, strt.size, 1, fp);
    fclose(fp);

    ret = s1->nb_errors ? -1 : 0;

done:
    for (i = 0; i < n; i++)
        tli_free(os[i].rel);

    tli_free(os);
    tli_free(nl);
    tli_free(sym_map);
    tli_free(strt.data);
    return ret;
}

#ifdef TLI_HOST_RUN

/// ---- src/core/tl_run.c ----

/// tinylinker - linking into memory and running the result
///
/// An embedder that has no use for a file on disk can have the image built
/// straight into the host process instead: the allocated sections are laid
/// out in freshly mapped pages, whatever they leave undefined is resolved
/// against the libraries the process itself has loaded, and the entry point
/// is then simply called. That is the path a compiler runtime takes when it
/// wants to execute what it has just produced.
///
/// Two things make this more than a memcpy. The first is page protection. No
/// page of the image is ever writable and executable at the same time, which
/// is a hard requirement on arm64 macOS and increasingly one elsewhere, so
/// relocation is done in the section buffers and only the finished bytes are
/// copied into pages that are afterwards narrowed to what each part of the
/// image actually needs. The second is that instruction caches on ARM and
/// RISC-V do not observe ordinary stores, so freshly written code has to be
/// published to them before it can be jumped to.
///
/// The file also carries what a fault in the running program needs: a
/// registry of the live images so a faulting address can be attributed to
/// one of them, a walker over the frame-pointer chain, and the signal
/// handlers that turn a fault into a backtrace.


/// Running an image only makes sense when the target is the machine we are
/// standing on, so the whole file belongs to the native backend.
#ifdef TLI_HOST_RUN

#ifndef _WIN32
# include <sys/mman.h>
# include <signal.h>
# ifndef __OpenBSD__
#  include <sys/ucontext.h>
# endif
# if !defined MAP_ANONYMOUS && defined MAP_ANON
#  define MAP_ANONYMOUS MAP_ANON
# endif
#else
/// A Windows exception record carries the same three registers a ucontext
/// does, which lets both worlds share the frame extraction further down.
# define ucontext_t CONTEXT
#endif

#ifdef __APPLE__
# include <libkern/OSCacheControl.h>
#endif

/// Where execution is and the frame it is in, however the platform reported
/// it: a signal context, an exception record, or the caller's own registers.
typedef struct rt_frame {
    addr_t ip;  /// The program counter to attribute the fault to.
    addr_t fp;  /// Frame pointer, the head of the chain the walker follows.
    addr_t sp;  /// Only read on Windows; see rt_exception_handler().
} rt_frame;

#ifdef TLI_BACKTRACE
/// Run-time information block.
///
/// tl_link.c:tli_add_btstub() builds one of these into the image's data
/// section field by field, so the order and the widths below are a contract
/// with that function rather than a free choice: ten pointers followed by
/// two ints.
typedef struct rt_context {
    /// Either (stabs, its end, the stabs string table) or (.debug_line, its
    /// end, .debug_line_str), depending on the 'dwarf' flag at the end.
    /// tinylinker has no compiler and so never produces a line table of
    /// either kind; the slots are kept because tli_add_btstub() writes them
    /// and everything after them has to stay where it is.
    void *line_table[3];
    ElfW(Sym) *esym_start;   /// The image's symbol table, which names addresses.
    ElfW(Sym) *esym_end;     /// One past its last entry.
    char *elf_str;           /// The strings those symbols are named by.
    addr_t prog_base;        /// Where the image was placed; unused here.
    void *bounds_start;      /// Handed to __bound_init() when checking is on.
    void *top_func;          /// Where a backtrace stops; see _tcc_setjmp().
    struct rt_context *next; /// The next live block, most recent first.
    i32 num_callers;         /// Frames to print, or 0 for the default.
    i32 dwarf;               /// Which pair of line tables line_table holds.
} rt_context;

/// Live information blocks, most recent first.
static rt_context *rt_infos;

/// Whether the fault handlers are in place; they are installed once, when the
/// first image that wants a backtrace is registered.
static i32 rt_handlers_installed;

/// Both are defined at the bottom of the file and used near the top.
static void rt_install_handlers(void);
static i32 _tcc_backtrace(rt_frame *f, const char *fmt, va_list ap);
#endif // def TLI_BACKTRACE

/// How many frames a backtrace prints when nobody has said otherwise.
#define RT_DEFAULT_CALLERS 6

/// longjmp() cannot report zero, so a program exiting successfully is passed
/// this instead and translated back in tli_run().
#define RT_EXIT_ZERO ((i32)0xE0E00E0E)

/// Every state whose image has been mapped is on this list, so that a
/// faulting program counter can be attributed to the image it came from.
/// The list is per translation unit, and a backend is one translation unit;
/// that is the right granularity, because an address can only belong to an
/// image laid out by the backend that built it.
static tli_State *rt_images;

/// Guards both the image list and the information blocks, which a signal
/// handler reads while another thread may be linking.
TLI_SEM(rt_sem);

/// Take the registry lock.
static void rt_lock(void) { WAIT_SEM(&rt_sem); }

/// Give the registry lock back.
static void rt_unlock(void) { POST_SEM(&rt_sem); }

/// The stack walker and the exit path, both defined further down.
static i32 rt_caller_pc(addr_t *paddr, rt_frame *f, i32 level);
static void rt_exit(rt_frame *f, i32 code);

/// ---------------------------------------------------------------------------
/// Pages

/// The host's page size, asked for once.  Protection is only ever applied in
/// whole pages, so nearly everything below is measured in these.
static addr_t rt_page_size(void)
{
    static addr_t cached;

    if (cached == 0) {
#ifdef _WIN32
        SYSTEM_INFO si;
        GetSystemInfo(&si);
        cached = si.dwPageSize;
#elif defined _SC_PAGESIZE
        long n = sysconf(_SC_PAGESIZE);
        cached = n > 0 ? (addr_t)n : 4096;
#else
        cached = 4096;
#endif
    }
    return cached;
}

/// ELF requires an alignment to be a power of two. Anything else is a
/// malformed input, and is treated as no constraint at all rather than being
/// allowed to corrupt the layout.
static addr_t rt_align_up(addr_t n, addr_t align)
{
    if (align < 2 || (align & (align - 1)))
        return n;

    return (n + align - 1) & ~(align - 1);
}

/// Protection classes. Sections are grouped by what the program is allowed to
/// do with them once the image is live, and each class is given whole pages
/// of its own, so that one class never has to have its protection weakened to
/// suit a neighbour sharing its last page.
enum {
    RT_SEG_EXEC,        /// r-x: code.
    RT_SEG_RO,          /// r--: constants, and relocated-once data.
    RT_SEG_RW,          /// rw-: everything the program writes.
    RT_SEG_COUNT
};

/// Which class a section belongs to, or -1 for one that takes no space in the
/// image at all.
static i32 rt_class_of(tli_Section *s)
{
    i32 f = s->sh_flags;

    if (0 == (f & SHF_ALLOC))
        return -1;

    // A section asking to be both written and executed cannot have both here.
    // Code wins: that is invariably what such a section turns out to hold,
    // and giving it no address at all would leave relocations against it
    // pointing at nothing.
    if (f & SHF_EXECINSTR)
        return RT_SEG_EXEC;

    return (f & SHF_WRITE) ? RT_SEG_RW : RT_SEG_RO;
}

/// Fresh pages, writable so the image can be assembled in them, and never
/// handed out by malloc: the protection put on them afterwards would follow
/// the memory back into the allocator's arena.
///
/// On arm64 macOS this plain mapping is deliberate. The platform forbids a
/// page being writable and executable at once, but it does allow a private
/// anonymous mapping to be re-protected r-x once the writing is over, which is
/// what happens below. The MAP_JIT alternative buys the ability to flip a page
/// back and forth, at the price of an entitlement the embedding process would
/// have to be signed with; nothing here needs that.
static void *rt_map(addr_t size)
{
#ifdef _WIN32
    return VirtualAlloc(NULL, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
#else
    void *p = mmap(NULL, size, PROT_READ | PROT_WRITE,
                   MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    return p == MAP_FAILED ? NULL : p;
#endif
}

/// Give a mapping back.  Paired with rt_map(), so the size it was asked for is
/// the size released.
static void rt_unmap(void *p, addr_t size)
{
#ifdef _WIN32
    (void)size; // MEM_RELEASE frees the whole reservation
    VirtualFree(p, 0, MEM_RELEASE);
#else
    munmap(p, size);
#endif
}

/// Narrow a run of whole pages to what its protection class allows.  This only
/// ever takes rights away: the pages start out writable and end up not.
static i32 rt_protect(void *p, addr_t size, i32 cls)
{
#ifdef _WIN32
    static const DWORD prot[RT_SEG_COUNT] = {
        PAGE_EXECUTE_READ, PAGE_READONLY, PAGE_READWRITE
    };
    DWORD old;
    return VirtualProtect(p, size, prot[cls], &old) ? 0 : -1;
#else
    static const i32 prot[RT_SEG_COUNT] = {
        PROT_READ | PROT_EXEC, PROT_READ, PROT_READ | PROT_WRITE
    };
    return mprotect(p, size, prot[cls]);
#endif
}

/// Make written bytes visible as instructions. ARM and RISC-V have separate
/// instruction caches that stores do not snoop, so without this the processor
/// is free to execute whatever happened to be in the cache at that address
/// before. x86 keeps the two coherent and the call costs nothing there.
static void rt_publish_code(void *p, addr_t size)
{
#if defined _WIN32
    FlushInstructionCache(GetCurrentProcess(), p, size);
#elif defined __APPLE__
    sys_icache_invalidate(p, size);
#elif defined __GNUC__
    __builtin___clear_cache((char *)p, (char *)p + size);
#endif
}

/// ---------------------------------------------------------------------------
/// Laying the image out

/// Where each protection class sits in the image, worked out before a single
/// byte is mapped.
typedef struct rt_layout {
    addr_t start[RT_SEG_COUNT]; /// Offsets of each class from the image base.
    addr_t len[RT_SEG_COUNT];   /// How much of each class there is.
    addr_t size;                /// The whole image, rounded up to a page.
    addr_t align;               /// What the image base has to be aligned to.
} rt_layout;

/// Give every allocated section an address relative to zero and work out how
/// many bytes the image needs.
///
/// Sections are visited in protection order so that each class comes out
/// contiguous, and each class begins on a page, because a class boundary is
/// where the protection changes and protection is only ever applied to whole
/// pages. Laying out against zero and adding the base afterwards is exact as
/// long as the base honours the strictest alignment any section asked for,
/// which is what 'align' records.
static void rt_plan(tli_State *s1, rt_layout *l)
{
    addr_t page = rt_page_size();
    addr_t off = 0;
    i32 i, k;

    l->align = page;

    // one pass per protection class, each starting on a fresh page
    for (k = 0; k < RT_SEG_COUNT; ++k) {
        addr_t start = rt_align_up(off, page);

        off = start;
        for (i = 1; i < s1->nb_sections; i++) {
            tli_Section *s = s1->sections[i];

            addr_t align = s->sh_addralign;

            if (rt_class_of(s) != k)
                continue;

            // Only an alignment rt_align_up() will honour is worth widening
            // the base for; the two have to agree or the offsets computed
            // here stop meaning what they say once the base is added.
            if (align > l->align && 0 == (align & (align - 1)))
                l->align = align;

            off = rt_align_up(off, align);
            s->sh_addr = off;
            off += s->data_offset;
        }
        l->start[k] = start;
        l->len[k] = off - start;
    }
    l->size = rt_align_up(off, page);
}

/// Print the finished layout, class by class and then section by section, for
/// -vv.  Addresses are the real ones, so they can be compared with a debugger.
static void rt_report_layout(tli_State *s1, const rt_layout *l, addr_t base)
{
    static const char *const perm[RT_SEG_COUNT] = { "r-x", "r--", "rw-" };
    i32 i, k;

    for (k = 0; k < RT_SEG_COUNT; ++k) {
        if (l->len[k] == 0)
            continue;

        printf("%s %p %6u bytes\n",
               perm[k], (void *)(base + l->start[k]), (u32)l->len[k]);
        for (i = 1; i < s1->nb_sections; i++) {
            tli_Section *s = s1->sections[i];
            if (rt_class_of(s) == k)
                printf("      %-16s %p %6u\n",
                       s->name, (void *)s->sh_addr, (u32)s->data_offset);
        }
    }
}

/// ---------------------------------------------------------------------------
/// What survives the link

/// Once the image is in place the symbol table is the only section anyone
/// still asks questions of: tli_get_symbol() looks names up in it and a
/// backtrace uses it to put a name to an address. So the table is rebuilt
/// keeping only what can answer one of those. A local symbol can never
/// answer the first, since the hash table only ever indexed the globals, but
/// a local function still answers the second, and dropping it would not just
/// cost a name: it would hand the frame to whichever global happens to sit
/// below it.
///
/// Both the symbols and their names are rewritten in place. That is safe
/// because entries are only ever dropped, never added, so each symbol and
/// each name is written at or before the offset it is being read from.
static void rt_shrink_symtab(tli_State *s1)
{
    tli_Section *symtab = s1->symtab;
    i32 i, nb_syms = symtab->data_offset / sizeof(ElfW(Sym));

    symtab->data_offset = 0;
    symtab->link->data_offset = 0;
    symtab->hash->data_offset = 0;
    init_symtab(symtab);

    // keep every global, and every function whether local or not
    for (i = 1; i < nb_syms; ++i) {
        ElfW(Sym) *sym = &((ElfW(Sym) *)symtab->data)[i];
        const char *name = (char *)symtab->link->data + sym->st_name;
        i32 type = ELFW(ST_TYPE)(sym->st_info);

        if (ELFW(ST_BIND)(sym->st_info) == STB_LOCAL
            && type != STT_FUNC && type != STT_GNU_IFUNC)
            continue;

        put_elf_sym_x(symtab, sym->st_value, sym->st_size, sym->st_info,
                      sym->st_other, sym->st_shndx,
                      tli_sym_xindex(symtab, i), name);
    }
}

/// Release every section but the symbol table, its strings and its hash: their
/// contents are either already copied into the image or of no further use.
/// The slots are left behind as NULL, which tli_elf_delete() tolerates.
static void rt_release(tli_State *s1, tli_Section **secs, i32 nb_secs, i32 first)
{
    i32 i;

    for (i = first; i < nb_secs; i++) {
        tli_Section *s = secs[i];

        if (s == s1->symtab || s == s1->symtab->link || s == s1->symtab->hash) {

            // give back whatever the table over-allocated while growing
            s->data = tli_realloc(s->data, s->data_allocated = s->data_offset);
            continue;
        }
        free_section(s);
        tli_free(s);
        secs[i] = NULL;
    }
}

/// Both section arrays at once.  The public one starts at 1 because slot zero
/// is the null section, which was never allocated.
static void rt_release_sections(tli_State *s1)
{
    rt_release(s1, s1->sections, s1->nb_sections, 1);
    rt_release(s1, s1->priv_sections, s1->nb_priv_sections, 0);
}

/// ---------------------------------------------------------------------------
/// The registry

#ifdef TLI_BACKTRACE
/// Did we allocate the state's information block, or is it part of the image?
/// The address answers it, and saves keeping a flag for something that can be
/// worked out.
static i32 rt_owns_info(tli_State *s1)
{
    addr_t p = (addr_t)s1->rc;

    return s1->rc != NULL
        && !(p >= (addr_t)s1->run_ptr && p < (addr_t)s1->run_ptr + s1->run_size);
}
#endif

/// Find or make the image's run-time information block and put it on the list,
/// so that a fault anywhere in the process can be described.
static void rt_attach_info(tli_State *s1)
{
#ifdef TLI_BACKTRACE
    rt_context *rc;

    if (!s1->do_backtrace)
        return;

    // An image produced by a compiler that knows about this carries the block
    // in its own data section, already pointing at the debug information that
    // came with it. tinylinker compiles nothing and so emits none, and then
    // makes a block of its own: the symbol table alone is enough to put a
    // name to an address, and it is what the image has.
    rc = tli_get_symbol(s1, "__rt_info");
    if (!rc) {
        rc = tli_mallocz(sizeof *rc);
        rc->num_callers = s1->rt_num_callers;
    }

    // The block predates the trimming of the symbol table either way, so its
    // bounds have to be filled in here rather than by a relocation.
    rc->esym_start = (ElfW(Sym) *)symtab_section->data;
    rc->esym_end = (ElfW(Sym) *)(symtab_section->data
                                 + symtab_section->data_offset);
    rc->elf_str = (char *)symtab_section->link->data;
#ifdef TLI_BCHECK

    // the bounds checker in the image wants its tables before anything runs
    if (s1->do_bounds_check) {
        void (*init)(void *, i32) = tli_get_symbol(s1, "__bound_init");
        if (init)
            init(rc->bounds_start, 1);
    }
#endif
    rc->next = rt_infos;
    rt_infos = rc;
    s1->rc = rc;

    if (!rt_handlers_installed) {
        rt_install_handlers();
        rt_handlers_installed = 1;
    }
#endif
}

/// Put a freshly built image on the list of live ones.
static void rt_register(tli_State *s1)
{
    rt_lock();
    s1->next = rt_images;
    rt_images = s1;
    rt_attach_info(s1);
    rt_unlock();
}

/// Take an image off the list again, along with its information block, before
/// the memory both describe goes away.
static void rt_unregister(tli_State *s1)
{
    tli_State **ps;

    rt_lock();
    for (ps = &rt_images; *ps; ps = &(*ps)->next) {
        if (*ps == s1) {
            *ps = s1->next;
            break;
        }
    }
#ifdef TLI_BACKTRACE

    // the block is only ours to free when it was not built into the image
    if (s1->rc) {
        rt_context **pc;

        for (pc = &rt_infos; *pc; pc = &(*pc)->next) {
            if (*pc == s1->rc) {
                *pc = s1->rc->next;
                break;
            }
        }
        if (rt_owns_info(s1))
            tli_free(s1->rc);

        s1->rc = NULL;
    }
#endif
    rt_unlock();
}

/// Which image does this frame belong to? With a single image the question
/// does not arise. With several, the innermost frame may well be somewhere
/// else entirely, in a libc routine the program called, so climb a few frames
/// until an address lands inside one of them.
static tli_State *rt_find_state(rt_frame *f)
{
    tli_State *s;
    addr_t pc;
    i32 level;

    if (rt_images == NULL || rt_images->next == NULL)
        return rt_images;

    for (level = 0; level < 8; ++level) {
        if (rt_caller_pc(&pc, f, level) < 0)
            break;

        for (s = rt_images; s; s = s->next)
            if (pc >= (addr_t)s->run_ptr
             && pc <  (addr_t)s->run_ptr + s->run_size)
                return s;
    }
    return NULL;
}

/// ---------------------------------------------------------------------------
/// Building the image

#ifdef _WIN64
/// The unwinder finds a function's prologue description through a table the
/// process registers with it; without this a fault inside the image would
/// unwind no further than its own frame.
static void *rt_add_function_table(tli_State *s1)
{
    void *p = NULL;

    if (s1->uw_pdata) {
        p = (void *)s1->uw_pdata->sh_addr;
        RtlAddFunctionTable((RUNTIME_FUNCTION *)p,
                            s1->uw_pdata->data_offset / sizeof(RUNTIME_FUNCTION),
                            s1->pe_imagebase);
        s1->uw_pdata = NULL;
    }
    return p;
}

/// Withdraw the table again, before the pages holding it are unmapped.
static void rt_del_function_table(void *p)
{
    if (p)
        RtlDeleteFunctionTable((RUNTIME_FUNCTION *)p);
}
#endif

/// Map the pages, relocate into them, and narrow their protection.  This is
/// where the whole file's reason for existing happens.
static i32 rt_build_image(tli_State *s1)
{
    rt_layout l;
    addr_t page = rt_page_size();
    addr_t base, mapped;
    void *raw;
    i32 i, k;

    rt_plan(s1, &l);

    // One mapping holds the lot. The slack covers the case of a section
    // asking for more alignment than a page, which costs nothing at all in
    // the ordinary case where it does not.
    mapped = l.size + (l.align - page);
    raw = rt_map(mapped);
    if (raw == NULL)
        return tli_error_noabort("in-memory link: could not map %u bytes for the image",
                                 (u32)mapped);

    // Recorded straight away: from here on the memory belongs to the state and
    // is released by tli_run_free() however this ends.
    s1->run_ptr = raw;
    s1->run_size = (u32)mapped;
    base = rt_align_up((addr_t)raw, l.align);

    for (i = 1; i < s1->nb_sections; i++) {
        tli_Section *s = s1->sections[i];
        if (rt_class_of(s) >= 0)
            s->sh_addr += base;
    }

    // Undefined symbols are looked up in the host process here; this is the
    // step that lets the image call into a library the embedder has loaded.
    relocate_syms(s1, s1->symtab, 1);
    if (s1->nb_errors)
        return -1;

    if (s1->fmt == TLI_FMT_PE)
        s1->pe_imagebase = base;
    else
        s1->arch->relocate_plt(s1);

    relocate_sections(s1);
    if (s1->nb_errors)
        return -1;

    if (s1->verbose >= 2)
        rt_report_layout(s1, &l, base);

    // Copy the relocated bytes in. Sections that hold nothing are skipped
    // rather than zeroed: both mmap(MAP_ANONYMOUS) and VirtualAlloc(MEM_COMMIT)
    // hand back pages that are already zero, so a large .bss costs nothing.
    for (i = 1; i < s1->nb_sections; i++) {
        tli_Section *s = s1->sections[i];
        if (rt_class_of(s) < 0 || s->data_offset == 0)
            continue;

        if (s->data && s->sh_type != SHT_NOBITS)
            memcpy((void *)s->sh_addr, s->data, s->data_offset);
    }

    if (l.len[RT_SEG_EXEC])
        rt_publish_code((void *)(base + l.start[RT_SEG_EXEC]),
                        l.len[RT_SEG_EXEC]);

    // The pages are writable already, so only the two narrower classes need
    // saying. Whole pages, because that is the unit protection comes in.
    for (k = 0; k < RT_SEG_RW; ++k) {
        if (l.len[k] == 0)
            continue;

        if (rt_protect((void *)(base + l.start[k]),
                       rt_align_up(l.len[k], page), k) < 0)
            return tli_error_noabort("in-memory link: could not protect the image");
    }

#ifdef _WIN64
    s1->run_function_table = rt_add_function_table(s1);
#endif

    // the section buffers have served their purpose; only the symbol table is
    // still worth keeping
    rt_shrink_symtab(s1);
    rt_release_sections(s1);
    return 0;
}

/// ---------------------------------------------------------------------------

/// Relocate everything into memory. Needed before tli_get_symbol() can answer,
/// and done for you by tli_run(). Returns -1 on error.
TLI_PUBLIC i32 tli_relocate(tli_State *s1)
{
    i32 ret;

    if (s1->run_ptr)
        return tli_error_noabort("the image has already been relocated");

#ifdef TLI_BACKTRACE

    // Offered to the program so that a support object linked into it can ask
    // for a backtrace of its own.
    if (s1->do_backtrace)
        tli_add_symbol(s1, "_tcc_backtrace", _tcc_backtrace);
#endif

    s1->nb_errors = 0;
    if (s1->fmt == TLI_FMT_PE) {
        pe_output_file(s1, NULL);
    } else {
        tli_add_runtime(s1);
        resolve_common_syms(s1);
        build_got_entries(s1, 0);
    }
    if (s1->nb_errors)
        return -1;

    ret = rt_build_image(s1);
    if (ret == 0)
        rt_register(s1);

    return ret;
}

/// Undo everything tli_relocate() did.  Called from tli_delete(), and safe on
/// a state that was never relocated at all.
ST_FUNC void tli_run_free(tli_State *s1)
{
    i32 i;

    // Libraries opened to resolve symbols against the host go back first.
    for (i = 0; i < s1->nb_loaded_dlls; i++) {
        tli_DLLReference *ref = s1->loaded_dlls[i];
        if (ref->handle)
#ifdef _WIN32
            FreeLibrary((HMODULE)ref->handle);
#else
            dlclose(ref->handle);
#endif
    }

    if (s1->run_ptr == NULL)
        return;

    rt_unregister(s1);
#ifdef _WIN64
    rt_del_function_table(s1->run_function_table);
#endif
    rt_unmap(s1->run_ptr, s1->run_size);
    s1->run_ptr = NULL;
    s1->run_size = 0;
}

/// ---------------------------------------------------------------------------
/// Calling it

/// The environment to hand the program, which is the host's own.
static char **rt_environ(void)
{
#if defined __APPLE__
    // A Mach-O executable gets its environment through a pointer the dynamic
    // loader owns; there is no 'environ' to take the address of.
    extern char ***_NSGetEnviron(void);
    return *_NSGetEnviron();
#elif defined __OpenBSD__ || defined __NetBSD__ || defined __FreeBSD__
    extern char **environ;
    return environ;
#else
    return environ;
#endif
}

/// Link into memory and call the entry point, returning what the program
/// returned.  This is the whole in-memory path in one call.
TLI_PUBLIC i32 tli_run(tli_State *s1, i32 argc, char **argv)
{
    i32 (*entry)(i32, char **, char **);
    const char *top_sym;
    i32 top_forc, main_forc, ret;
    addr_t top_addr;
    jmp_buf jb;

    tli_add_symbol(s1, "__rt_exit", rt_exit);

    // 'forc' asks get_sym_addr() to spell a name the way a C compiler would
    // for this target, which on Mach-O means a leading underscore. That is
    // right for the default entry point and for the runmain shim, but a name
    // given explicitly with -Wl,-e is already the exact symbol.
    top_sym = s1->elf_entryname ? s1->elf_entryname : "main";
    top_forc = s1->elf_entryname ? 0 : 1;
    s1->run_main = top_sym;
    main_forc = top_forc;

    // runmain.o is TinyCC's shim that turns the program calling exit() into a
    // longjmp back here. tinylinker ships no support library, so it is used
    // only when the embedder has pointed tl_set_lib_path() at one; otherwise
    // the entry point is called directly and exit() ends the host process.
    if (0 == tli_add_support(s1, "runmain.o", 1))
        s1->run_main = "_runmain", main_forc = 1;

    if (tli_relocate(s1) < 0)
        return -1;

    entry = (void *)get_sym_addr(s1, s1->run_main, 1, main_forc);
    if ((addr_t)-1 == (addr_t)entry)
        return -1;

    // The program inherits the host's streams, so anything still buffered has
    // to be out of the way before it starts writing to them.
    errno = 0;
    fflush(stdout);
    fflush(stderr);

    // Tell the backtrace walker where to stop climbing, and give exit() a
    // place to come back to when the shim is in use.
    top_addr = get_sym_addr(s1, top_sym, 0, top_forc);
    ret = tli_setjmp(s1, jb, top_addr == (addr_t)-1 ? NULL : (void *)top_addr);
    if (0 == ret)
        ret = entry(argc, argv, rt_environ());
    else if (RT_EXIT_ZERO == ret)
        ret = 0;

    return ret;
}

/// Record where exit() should land and where a backtrace should stop.  Called
/// by the runmain shim, which owns the jump buffer it hands over.
TLI_PUBLIC void *_tcc_setjmp(tli_State *s1, void *p_jmp_buf, void *func,
                            void *p_longjmp)
{
    s1->run_lj = p_longjmp;
    s1->run_jb = p_jmp_buf;
#ifdef TLI_BACKTRACE
    if (s1->rc)
        s1->rc->top_func = func;
#endif
    return p_jmp_buf;
}

/// Called as __rt_exit by the exit() shim, and from the signal handlers.
/// Where the embedder has arranged a landing point, the program's exit
/// unwinds back to tli_run() and the host survives it; otherwise there is
/// nothing to do but pass the status on.
static void rt_exit(rt_frame *f, i32 code)
{
    tli_State *s;

    rt_lock();
    s = rt_find_state(f);
    rt_unlock();

    if (s && s->run_lj) {
#ifdef TLI_BCHECK

        // A set frame pointer means this came from a signal rather than from
        // the program returning, so the bounds checker never got to tidy up
        // after itself.
        if (f->fp) {
            void (*bexit)(void) = tli_get_symbol(s, "__bound_exit");
            if (bexit)
                bexit();
        }
#endif
        if (code == 0)
            code = RT_EXIT_ZERO;

        ((void (*)(void *, i32))s->run_lj)(s->run_jb, code);
    }
    exit(code);
}

/// ---------------------------------------------------------------------------
/// Walking the stack

/// Frame layout, as the ABIs describe it.
///
/// x86, x86-64, ARM and AArch64 all keep the same two-word frame record at
/// the frame pointer: the caller's frame pointer first, the return address
/// second. RISC-V puts its record immediately below s0 instead, return
/// address first. Everything here therefore depends on the code having been
/// built with frame pointers; without them the walk stops at the first frame,
/// which is still the interesting one.
#if defined __i386__ || defined __x86_64__ \
 || defined __arm__ || defined __aarch64__
# define RT_SLOT_FP  0
# define RT_SLOT_RA  1
#elif defined __riscv
# define RT_SLOT_FP  (-2)
# define RT_SLOT_RA  (-1)
#endif

/// No stack ever lives in the first page, so anything down there is garbage
/// rather than a frame.
#define RT_STACK_FLOOR 0x1000

/// Return the program counter 'level' frames out from 'f', or -1 when the
/// chain runs out. Called from a signal handler on a stack that may well be
/// the reason for the signal, so every step is checked: frames are word
/// aligned, they lie above the previous one because stacks grow down, and a
/// chain that fails either test is simply the end of the walk.
static i32 rt_caller_pc(addr_t *paddr, rt_frame *f, i32 level)
{
    if (level == 0) {
        *paddr = f->ip;
        return 0;
    }
#ifdef RT_SLOT_FP
    {
        addr_t fp = f->fp, prev = 0;

        // climb while the chain still looks like a chain
        while (fp > prev
            && fp >= RT_STACK_FLOOR
            && 0 == (fp & (sizeof(addr_t) - 1))) {
            addr_t *frame = (addr_t *)fp;

            if (0 == --level) {
                *paddr = frame[RT_SLOT_RA];
                return 0;
            }
            prev = fp;
            fp = frame[RT_SLOT_FP];
        }
    }
#endif
    return -1;
}

#ifdef TLI_BACKTRACE

/// Write to stderr and flush, because the program that is about to die may
/// take the buffers with it.
static i32 rt_vprintf(const char *fmt, va_list ap)
{
    i32 ret = vfprintf(stderr, fmt, ap);
    fflush(stderr);
    return ret;
}

/// The variadic form of rt_vprintf().
static i32 rt_printf(const char *fmt, ...)
{
    va_list ap;
    i32 ret;

    va_start(ap, fmt);
    ret = rt_vprintf(fmt, ap);
    va_end(ap);
    return ret;
}

/// Put a name to an address.
///
/// tinylinker has no compiler, and so produces no line tables: the symbol
/// table is all there is to go on, which gets a backtrace as far as the
/// enclosing function but no further.
static const char *rt_elfsym(rt_context *rc, addr_t pc,
                             addr_t lo, addr_t hi, addr_t *pfunc)
{
    ElfW(Sym) *sym, *best = NULL;

    // every function symbol is a candidate for the one that contains pc
    for (sym = rc->esym_start + 1; sym < rc->esym_end; ++sym) {
        i32 type = ELFW(ST_TYPE)(sym->st_info);

        if (type != STT_FUNC && type != STT_GNU_IFUNC)
            continue;

        if (pc < sym->st_value)
            continue;

        if (sym->st_size) {
            if (pc >= sym->st_value + sym->st_size)
                continue;

            best = sym;         // the symbol says so; nothing beats that
            break;
        }

        // A Mach-O symbol table records no sizes, so the best that can be
        // done is the nearest name below the address. That is only allowed
        // inside [lo, hi), the image the address was attributed to: without
        // the bound, an imported name would end up labelling the library it
        // was imported from, which is worse than saying nothing.
        if (pc >= lo && pc < hi
            && sym->st_value >= lo && sym->st_value < hi
            && (best == NULL || sym->st_value > best->st_value))
            best = sym;
    }

    if (best == NULL)
        return NULL;

    *pfunc = best->st_value;
    return rc->elf_str + best->st_name;
}

/// Print a message and the frames beneath it.
///
/// Reachable from the program as well as from the signal handlers, because
/// tli_relocate() offers it under the name _tcc_backtrace; the two prefixes
/// understood on the format string are the calling convention the support
/// objects that do so expect. "^file.c^" asked for frames from that file to
/// be dropped, which needs the line information tinylinker does not have, so
/// it is stripped rather than printed. '\001' asks for the message and the
/// innermost frame only, with no trailing newline.
static i32 _tcc_backtrace(rt_frame *f, const char *fmt, va_list ap)
{
    rt_context *rc, *found;
    tli_State *s;
    addr_t pc, func_pc, lo, hi;
    const char *name, *end;
    char msg[200];
    i32 level, depth, one = 0;

    // strip the two prefixes the support objects prepend
    if (fmt[0] == '^' && (end = strchr(fmt + 1, '^')) != NULL)
        fmt = end + 1;

    if (fmt[0] == '\001')
        ++fmt, one = 1;

    vsnprintf(msg, sizeof msg, fmt, ap);

    rt_lock();
    s = rt_find_state(f);

    // How deep to go is the faulting image's decision where we can tell which
    // one it was, and the most recently linked one's otherwise.
    rc = s && s->rc ? s->rc : rt_infos;
    depth = rc && rc->num_callers ? rc->num_callers : RT_DEFAULT_CALLERS;
    lo = s ? (addr_t)s->run_ptr : 0;
    hi = s ? lo + s->run_size : 0;

    for (level = 0; level < depth; ++level) {
        if (rt_caller_pc(&pc, f, level) < 0)
            break;

        name = NULL;
        func_pc = 0;

        // the address may belong to any of the live images, not just this one
        for (found = rc; found; found = found->next)
            if ((name = rt_elfsym(found, pc, lo, hi, &func_pc)) != NULL)
                break;

        if (s && s->bt_func) {
            if (0 == s->bt_func(s->bt_data, (void *)pc, NULL, 0, name,
                                level == 0 ? msg : NULL))
                break;
        } else {
            rt_printf("0x%08llx: %s %s", (long long)pc,
                      level ? "by" : "at", name ? name : "???");
            if (level == 0) {
                rt_printf(": %s", msg);
                if (one)
                    break;
            }
            rt_printf("\n");
        }

        // Stop where the embedder said the program begins; the frames above
        // that belong to whoever called tli_run().
        if (found && func_pc && func_pc == (addr_t)found->top_func)
            break;
    }
    rt_unlock();
    return 0;
}

/// Report a fault at 'f'. The message is folded into the format string rather
/// than formatted here, because the caller's arguments still have to be
/// applied to it.
static i32 rt_error(rt_frame *f, const char *fmt, ...)
{
    va_list ap;
    char msg[200];
    i32 ret;

    snprintf(msg, sizeof msg, "RUNTIME ERROR: %s", fmt);
    va_start(ap, fmt);
    ret = _tcc_backtrace(f, msg, ap);
    va_end(ap);
    return ret;
}

/// ---------------------------------------------------------------------------
/// Where the machine keeps the frame when it faults

/// The instruction and frame pointers of an interrupted thread, which every
/// platform stores somewhere different in the context it hands its handler.
static void rt_frame_of_context(ucontext_t *uc, rt_frame *f)
{
    f->ip = f->fp = f->sp = 0;
#if defined _WIN64 && defined __aarch64__
    f->ip = uc->Pc, f->fp = uc->Fp, f->sp = uc->Sp;
#elif defined _WIN64
    f->ip = uc->Rip, f->fp = uc->Rbp, f->sp = uc->Rsp;
#elif defined _WIN32
    f->ip = uc->Eip, f->fp = uc->Ebp, f->sp = uc->Esp;

#elif defined __i386__
# if defined __APPLE__
    f->ip = uc->uc_mcontext->__ss.__eip, f->fp = uc->uc_mcontext->__ss.__ebp;
# elif defined __FreeBSD__ || defined __FreeBSD_kernel__ || defined __DragonFly__
    f->ip = uc->uc_mcontext.mc_eip, f->fp = uc->uc_mcontext.mc_ebp;
# elif defined __NetBSD__
    f->ip = uc->uc_mcontext.__gregs[_REG_EIP];
    f->fp = uc->uc_mcontext.__gregs[_REG_EBP];
# elif defined __OpenBSD__
    f->ip = uc->sc_eip, f->fp = uc->sc_ebp;
# else
    f->ip = uc->uc_mcontext.gregs[REG_EIP], f->fp = uc->uc_mcontext.gregs[REG_EBP];
# endif

#elif defined __x86_64__
# if defined __APPLE__
    f->ip = uc->uc_mcontext->__ss.__rip, f->fp = uc->uc_mcontext->__ss.__rbp;
# elif defined __FreeBSD__ || defined __FreeBSD_kernel__ || defined __DragonFly__
    f->ip = uc->uc_mcontext.mc_rip, f->fp = uc->uc_mcontext.mc_rbp;
# elif defined __NetBSD__
    f->ip = uc->uc_mcontext.__gregs[_REG_RIP];
    f->fp = uc->uc_mcontext.__gregs[_REG_RBP];
# elif defined __OpenBSD__
    f->ip = uc->sc_rip, f->fp = uc->sc_rbp;
# else
    f->ip = uc->uc_mcontext.gregs[REG_RIP], f->fp = uc->uc_mcontext.gregs[REG_RBP];
# endif

#elif defined __arm__
# if defined __NetBSD__ || defined __FreeBSD__
    f->ip = uc->uc_mcontext.__gregs[_REG_PC], f->fp = uc->uc_mcontext.__gregs[_REG_FP];
# elif defined __OpenBSD__
    f->ip = uc->sc_pc, f->fp = uc->sc_r11;
# else
    f->ip = uc->uc_mcontext.arm_pc, f->fp = uc->uc_mcontext.arm_fp;
# endif

#elif defined __aarch64__
# if defined __APPLE__
    // mach/arm/_structs.h: the thread state hangs off the context by pointer
    f->ip = uc->uc_mcontext->__ss.__pc, f->fp = uc->uc_mcontext->__ss.__fp;
# elif defined __FreeBSD__
    f->ip = uc->uc_mcontext.mc_gpregs.gp_elr; // the exception link register
    f->fp = uc->uc_mcontext.mc_gpregs.gp_x[29];
# elif defined __NetBSD__
    f->ip = uc->uc_mcontext.__gregs[_REG_PC], f->fp = uc->uc_mcontext.__gregs[_REG_FP];
# elif defined __OpenBSD__
    f->ip = uc->sc_elr, f->fp = uc->sc_x[29];
# else
    f->ip = uc->uc_mcontext.pc, f->fp = uc->uc_mcontext.regs[29];
# endif

#elif defined __riscv
# if defined __OpenBSD__
    f->ip = uc->sc_sepc, f->fp = uc->sc_s[0];
# else
    f->ip = uc->uc_mcontext.__gregs[REG_PC], f->fp = uc->uc_mcontext.__gregs[REG_S0];
# endif
#endif
}

/// ---------------------------------------------------------------------------
/// Catching the fault

#ifndef _WIN32

/// Turn a fatal signal into a backtrace and then an exit.  Everything reached
/// from here has to be safe to run on the stack that caused the signal.
static void rt_signal_handler(i32 signum, siginfo_t *info, void *puc)
{
    rt_frame f;

    rt_frame_of_context(puc, &f);

    switch (signum) {
    case SIGFPE:
        if (info->si_code == FPE_INTDIV || info->si_code == FPE_FLTDIV)
            rt_error(&f, "division by zero");
        else
            rt_error(&f, "floating point exception");
        break;
    case SIGBUS:
    case SIGSEGV:
        rt_error(&f, "invalid memory access");
        break;
    case SIGILL:
        rt_error(&f, "illegal instruction");
        break;
    case SIGABRT:
        rt_error(&f, "abort() called");
        break;
    default:
        rt_error(&f, "caught signal %d", signum);
        break;
    }

    // rt_exit() may longjmp straight out of this handler, and the signal
    // would then stay blocked for the rest of the process's life. Unblock it
    // here, while there is still a handler frame to do it from.
    {
        sigset_t set;
        sigemptyset(&set);
        sigaddset(&set, signum);
        sigprocmask(SIG_UNBLOCK, &set, NULL);
    }
    rt_exit(&f, 255);
}

/// Install the handler for every signal a generated program can raise.  Done
/// once, when the first image asking for a backtrace is registered.
static void rt_install_handlers(void)
{
    struct sigaction sa;
    static const i32 signals[] = {
        SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGABRT
    };
    u32 i;

    memset(&sa, 0, sizeof sa);
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_SIGINFO;   // si_code tells the kinds of SIGFPE apart
    sa.sa_sigaction = rt_signal_handler;
    for (i = 0; i < sizeof signals / sizeof *signals; ++i)
        sigaction(signals[i], &sa, NULL);
}

#else // _WIN32

/// The same, for the structured exceptions Windows reports instead of signals.
static long __stdcall rt_exception_handler(EXCEPTION_POINTERS *ex_info)
{
    rt_frame f;
    u32 code = ex_info->ExceptionRecord->ExceptionCode;

    rt_frame_of_context(ex_info->ContextRecord, &f);

    switch (code) {
    case EXCEPTION_ACCESS_VIOLATION:
        rt_error(&f, "invalid memory access");
        break;
    case EXCEPTION_STACK_OVERFLOW:
        rt_error(&f, "stack overflow");
        break;
    case EXCEPTION_INT_DIVIDE_BY_ZERO:
        rt_error(&f, "division by zero");
        break;
    case EXCEPTION_BREAKPOINT:
    case EXCEPTION_SINGLE_STEP:

        // A debugger may well want these, so report and pass them on. The
        // address to blame is the one the trap pushed, not the trap itself.
        f.ip = *(addr_t *)f.sp;
        rt_error(&f, "breakpoint/single-step exception:");
        return EXCEPTION_CONTINUE_SEARCH;
    default:
        rt_error(&f, "caught exception %08x", code);
        break;
    }
    rt_exit(&f, 255);
    return EXCEPTION_EXECUTE_HANDLER;
}

/// Register the exception handler.  64-bit Windows takes a vectored handler,
/// which runs before any the program installs for itself.
static void rt_install_handlers(void)
{
#ifdef _WIN64
    AddVectoredExceptionHandler(1, rt_exception_handler);
#else
    SetUnhandledExceptionFilter(rt_exception_handler);
#endif
}

#endif // _WIN32
#endif // def TLI_BACKTRACE

/// ---------------------------------------------------------------------------
/// Standing in for the dynamic loader

#ifdef TLI_STATIC
/// Built without a dynamic loader, nothing can be opened at run time and the
/// only symbols an image can be given are the ones this library was itself
/// linked against. The table below is what tli_add_symbol() would otherwise
/// have to be called for; add to it as the programs being run require.
static const struct {
    const char *name; /// The symbol as an image would refer to it.
    void *addr;       /// Where this build of the library has it.
} rt_static_syms[] = {
#define RT_SYM(f) { #f, (void *)&f },
    RT_SYM(printf)
    RT_SYM(fprintf)
    RT_SYM(fopen)
    RT_SYM(fclose)
#undef RT_SYM
    { NULL, NULL }
};

/// There is nothing to open, so nothing opens.
ST_FUNC void *dlopen(const char *filename, i32 flag)
{
    return NULL;
}

/// Nothing was ever opened, so nothing needs closing.
ST_FUNC void dlclose(void *p)
{
}

/// Say why the other three are so unhelpful.
ST_FUNC const char *dlerror(void)
{
    return "this build has no dynamic loader";
}

/// Resolve against the built-in table instead of against the process.
ST_FUNC void *dlsym(void *handle, const char *symbol)
{
    i32 i;

    for (i = 0; rt_static_syms[i].name; i++)
        if (0 == strcmp(rt_static_syms[i].name, symbol))
            return rt_static_syms[i].addr;

    return NULL;
}
#endif // def TLI_STATIC

#endif // def TLI_HOST_RUN

#endif
#endif // ONE_SOURCE


/// ---------------------------------------------------------------------------
/// Global state
///
/// The linker keeps everything it needs in the tli_State the caller owns, but
/// the error macros in tl_internal.h reach the current state through a global
/// so that a call ten frames down can report which link it belongs to.  A
/// semaphore makes that safe when two threads link at once; tli_enter_state()
/// takes it and the error path gives it back.

/// The link currently in progress on this thread, for the error macros.
ST_DATA struct tli_State *tli_state;

/// TLI_SEM() supplies the storage class, so that the macro can expand to
/// nothing when locking is compiled out.
TLI_SEM(tli_compile_sem);

/// ---------------------------------------------------------------------------
#ifdef _WIN32
/// Windows accepts either separator; the rest of the core only looks for '/'.
ST_FUNC char *normalize_slashes(char *path)
{
    char *p;

    for (p = path; *p; ++p)
        if (*p == '\\')
            *p = '/';

    return path;
}

#if defined TLI_AS_DLL && !defined CONFIG_TLIDIR
/// The module the library was loaded as, which is where its support files sit.
static HMODULE tli_module;

/// Windows hands a DLL its own handle here and nowhere else, so it is caught
/// on the way past.
BOOL WINAPI DllMain (HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved)
{
    if (DLL_PROCESS_ATTACH == dwReason)
        tli_module = hDll;

    return TRUE;
}
#else
#define tli_module NULL // NULL means executable itself
#endif

#ifndef CONFIG_TLIDIR
/// Nothing is installed at a fixed prefix on Windows, so the support files are
/// looked for beside the binary that is running.
static inline char *config_tlidir_w32(char *path)
{
    char *p;

    GetModuleFileNameA(tli_module, path, MAX_PATH);
    p = tli_basename(normalize_slashes(strlwr(path)));

    // step back onto the separator, so that the directory is what is left
    if (p > path)
        --p;

    *p = 0;
    return path;
}
#define CONFIG_TLIDIR config_tlidir_w32(alloca(MAX_PATH))
#endif

#ifdef TLI_HOST_RUN
/// Where Windows keeps the system DLLs, so that one can be named directly.
static void tli_add_systemdir(tli_State *s1)
{
    char buf[1000];

    GetSystemDirectoryA(buf, sizeof buf);
    tli_add_library_path(s1, normalize_slashes(buf));
}
#endif

/// A FILE* has to be created and closed by the same module on Windows, so
/// callers outside the library go through these rather than fopen().
PUB_FUNC FILE *tli_fopen(const char *f, const char *m) {
    return fopen(f, m);
}

/// The other half of tli_fopen(); see there.
PUB_FUNC i32 tli_fclose(FILE *f) {
    return fclose(f);
}
#endif
/// ---------------------------------------------------------------------------

/// Make 's1' the state the error macros report against, taking the lock that
/// keeps two concurrent links from claiming it at once.
PUB_FUNC void tli_enter_state(tli_State *s1)
{
    // Once the caller has installed a jump buffer it owns the state for the
    // whole link, so the lock is taken once rather than per call.
    if (s1->error_set_jmp_enabled)
        return;

    WAIT_SEM(&tli_compile_sem);
    tli_state = s1;
}

/// Give the state and the lock back.  Reporting an error counts as leaving,
/// whether or not the caller carries on afterwards.
PUB_FUNC void tli_exit_state(tli_State *s1)
{
    if (s1->error_set_jmp_enabled)
        return;

    tli_state = NULL;
    POST_SEM(&tli_compile_sem);
}

/// ---------------------------------------------------------------------------
/// Memory
///
/// Every allocation the linker makes goes through one function pointer, so an
/// embedder with its own arena can take the whole thing over with
/// tl_set_realloc().  realloc() alone covers all three operations: a null
/// pointer allocates and a zero size frees.

/// The real ones are wanted here, before the poisoning in tl_internal.h
/// applies.
#undef free
#undef realloc

/// The allocator in force until an embedder replaces it: the C library's, with
/// failure treated as fatal.
static void *default_reallocator(void *ptr, usize size)
{
    if (size == 0) {
        free(ptr);
        return NULL;
    }

    ptr = realloc(ptr, size);
    if (!ptr) {

        // There is no useful way to carry on, and no caller checks: an
        // allocation failure here means the link cannot be finished.
        fprintf(stderr, "tinylinker: out of memory\n");
        exit(1);
    }
    return ptr;
}

/// Put the poisoning back, so that nothing below reaches the C library direct.
#define free(p) use_tcc_free(p)
#define realloc(p, s) use_tcc_realloc(p, s)

/// The one function pointer everything the linker allocates goes through.
static void *(*reallocator)(void *, usize) = default_reallocator;

/// MEM_DEBUG redirects these to the checking versions below; the checking
/// versions are built on the plain ones, so undo that here.
#undef tli_free
#undef tli_malloc
#undef tli_realloc
#undef tli_mallocz
#undef tli_strdup

/// Release a block.  A null pointer is allowed, as free()'s is.
PUB_FUNC void tli_free(void *ptr)
{
    reallocator(ptr, 0);
}

/// Allocate.  There is no failure return: see default_reallocator().
PUB_FUNC void *tli_malloc(usize size)
{
    return reallocator(NULL, size);
}

/// Resize a block, or allocate one when 'ptr' is NULL.
PUB_FUNC void *tli_realloc(void *ptr, usize size)
{
    return reallocator(ptr, size);
}

/// Allocate and clear, which is what nearly every structure here wants.
PUB_FUNC void *tli_mallocz(usize size)
{
    void *ptr = tli_malloc(size);

    if (size)
        memset(ptr, 0, size);

    return ptr;
}

/// strdup() through the linker's allocator, so the copy can be tli_free()d.
PUB_FUNC char *tli_strdup(const char *str)
{
    usize len = strlen(str) + 1;

    return memcpy(tli_malloc(len), str, len);
}

#ifdef MEM_DEBUG
/// A checking allocator, for hunting leaks and overruns in the linker itself.
///
/// Each block gets a header holding its size, where it was allocated and a
/// marker either side of the payload, and joins a list of everything currently
/// live.  Freeing or resizing a block checks the markers first, so a write that
/// ran off the end is reported at the next operation on that block rather than
/// whenever the heap next notices.  What is still on the list when the last
/// state goes away is a leak, and is printed with the line that allocated it.

#define MEM_HEAD_MAGIC 0xFEEDDEB1u  /// Marker before the payload.
#define MEM_TAIL_MAGIC 0xFEEDDEB3u  /// Marker after it.
#define MEM_ORIGIN_LEN 40           /// Room for the tail of a file name.

/// The header in front of every block a checking build hands out.
typedef struct tli_MemBlock {
    struct tli_MemBlock *prev, *next;  /// Links in the list of live blocks.
    u32 size;                       /// The payload's size, as asked for.
    i32 line;                       /// Line that allocated it.
    char origin[MEM_ORIGIN_LEN];    /// The tail of that line's file name.
    u32 head;                       /// MEM_HEAD_MAGIC while the block is live.
    /// The payload starts here, aligned for anything the linker stores.
    ALIGNED(16) u8 body[4];
} tli_MemBlock;

#define MEM_TAIL(b)  ((b)->body + (b)->size)  /// The marker after the payload.
#define MEM_BODY(b)  ((void *)(b)->body)      /// What the caller was handed.

/// Back from a payload to its header, which is what every entry point gets.
#define MEM_BLOCK(p) ((tli_MemBlock *)((char *)(p) - offsetof(tli_MemBlock, body)))

/// Guards the list and the counters below against concurrent links.
TLI_SEM(mem_sem);

static tli_MemBlock *mem_live;  /// Everything allocated and not yet freed.
static u32 mem_in_use;       /// How much those blocks account for.
static u32 mem_peak;         /// The high-water mark, reported at the end.
static i32 mem_users;        /// Live states; the report waits for the last.

/// Check both markers and return the header, or report where the block was
/// allocated and give up.  A corrupt block cannot be reasoned about further.
static tli_MemBlock *mem_check(void *ptr, const char *what)
{
    tli_MemBlock *b = MEM_BLOCK(ptr);

    if (b->head == MEM_HEAD_MAGIC && read32le(MEM_TAIL(b)) == MEM_TAIL_MAGIC)
        return b;

    fprintf(stderr, "%s: block at %p is corrupt or already freed\n", what, ptr);
    if (b->head == MEM_HEAD_MAGIC)
        fprintf(stderr, "%s:%u: allocated here.\n", b->origin, b->line);

    exit(1);
}

/// Remember the tail of the path, which is the part that identifies the file.
static void mem_note_origin(tli_MemBlock *b, const char *file, i32 line)
{
    usize n = strlen(file);

    if (n >= MEM_ORIGIN_LEN)
        file += n - (MEM_ORIGIN_LEN - 1);

    pstrcpy(b->origin, MEM_ORIGIN_LEN, file);
    b->line = line;
}

/// Add to the running total and raise the high-water mark with it.
static void mem_account(u32 added)
{
    mem_in_use += added;
    if (mem_in_use > mem_peak)
        mem_peak = mem_in_use;
}

/// tli_malloc() with a header, markers and a place on the live list.
PUB_FUNC void *tli_malloc_debug(usize size, const char *file, i32 line)
{
    tli_MemBlock *b;

    if (!size)
        return NULL;

    b = tli_malloc(sizeof(tli_MemBlock) + size);
    b->size = size;
    b->head = MEM_HEAD_MAGIC;
    write32le(MEM_TAIL(b), MEM_TAIL_MAGIC);
    mem_note_origin(b, file, line);

    WAIT_SEM(&mem_sem);
    b->prev = NULL;
    b->next = mem_live;
    if (b->next)
        b->next->prev = b;

    mem_live = b;
    mem_account(size);
    POST_SEM(&mem_sem);
    return MEM_BODY(b);
}

/// tli_free() with the markers checked and the block taken off the list.
PUB_FUNC void tli_free_debug(void *ptr)
{
    tli_MemBlock *b;

    if (!ptr)
        return;

    b = mem_check(ptr, "tli_free");
    WAIT_SEM(&mem_sem);
    mem_in_use -= b->size;
    if (b->next)
        b->next->prev = b->prev;

    if (b->prev)
        b->prev->next = b->next;
    else
        mem_live = b->next;

    b->head = 0; // so a second free is caught rather than repeated
    POST_SEM(&mem_sem);
    tli_free(b);
}

/// tli_mallocz() over the checking allocator.
PUB_FUNC void *tli_mallocz_debug(usize size, const char *file, i32 line)
{
    void *ptr = tli_malloc_debug(size, file, line);

    if (size)
        memset(ptr, 0, size);

    return ptr;
}

/// tli_realloc() over the checking allocator, which has to mend the list when
/// the block moves.
PUB_FUNC void *tli_realloc_debug(void *ptr, usize size, const char *file, i32 line)
{
    tli_MemBlock *b;
    i32 was_head;

    if (!ptr)
        return tli_malloc_debug(size, file, line);

    if (!size) {
        tli_free_debug(ptr);
        return NULL;
    }

    b = mem_check(ptr, "tli_realloc");
    WAIT_SEM(&mem_sem);
    mem_in_use -= b->size;
    was_head = (b == mem_live);
    b = tli_realloc(b, sizeof(tli_MemBlock) + size);
    b->size = size;
    write32le(MEM_TAIL(b), MEM_TAIL_MAGIC);

    // the block moved, so everything pointing at it has to be told
    if (b->next)
        b->next->prev = b;

    if (b->prev)
        b->prev->next = b;

    if (was_head)
        mem_live = b;

    mem_account(size);
    POST_SEM(&mem_sem);
    return MEM_BODY(b);
}

/// tli_strdup() over the checking allocator.
PUB_FUNC char *tli_strdup_debug(const char *str, const char *file, i32 line)
{
    usize len = strlen(str) + 1;

    return memcpy(tli_malloc_debug(len, file, line), str, len);
}

/// Called with +1 when a state is created and -1 when one is deleted.  The
/// report waits for the last one to go, since a linker that is still holding a
/// state is entitled to be holding memory with it.
PUB_FUNC void tli_memcheck(i32 d)
{
    WAIT_SEM(&mem_sem);
    mem_users += d;

    // nothing is left that could legitimately still own a block
    if (mem_users == 0 && mem_in_use) {
        tli_MemBlock *b;

        fflush(stdout);
        fprintf(stderr, "MEM_DEBUG: mem_leak= %u bytes, mem_max_size= %u bytes\n",
                mem_in_use, mem_peak);
        for (b = mem_live; b; b = b->next)
            fprintf(stderr, "%s:%u: error: %u bytes leaked\n",
                    b->origin, b->line, b->size);

        fflush(stderr);
        mem_in_use = 0;
        mem_peak = 0;
        mem_live = NULL;
#if MEM_DEBUG-0 == 2
        exit(2);
#endif
    }
    POST_SEM(&mem_sem);
}

/// Put the redirection back for the rest of the core.
#define tli_free(ptr)           tli_free_debug(ptr)
#define tli_malloc(size)        tli_malloc_debug(size, __FILE__, __LINE__)
#define tli_mallocz(size)       tli_mallocz_debug(size, __FILE__, __LINE__)
#define tli_realloc(ptr,size)   tli_realloc_debug(ptr, size, __FILE__, __LINE__)
#define tli_strdup(str)         tli_strdup_debug(str, __FILE__, __LINE__)

#endif // MEM_DEBUG

/// ---------------------------------------------------------------------------
/// Strings and paths

/// Copy 's' into a buffer of a known size, truncating rather than overrunning.
/// Returns the buffer, so it can be used as an argument in place.
ST_FUNC char *pstrcpy(char *buf, usize buf_size, const char *s)
{
    if (buf_size) {
        usize n = strlen(s);

        if (n >= buf_size)
            n = buf_size - 1;

        memcpy(buf, s, n);
        buf[n] = '\0';
    }
    return buf;
}

/// The same, appending.  A buffer already full is left alone rather than
/// overrun.
ST_FUNC char *pstrcat(char *buf, usize buf_size, const char *s)
{
    usize n = strlen(buf);

    if (n < buf_size)
        pstrcpy(buf + n, buf_size - n, s);

    return buf;
}

/// The file name without its directory, or the whole thing if it has none.
/// The result points into 'name' rather than to a copy.
PUB_FUNC char *tli_basename(const char *name)
{
    const char *base = name, *p;

    for (p = name; *p; p++)
        if (IS_DIRSEP(*p))
            base = p + 1;

    return (char *)base;
}

/// The extension including its dot, or the end of the string if there is none.
/// Only the last component is considered, so a dot in a directory name does not
/// count.
PUB_FUNC char *tli_fileextension(const char *name)
{
    const char *p = tli_basename(name), *dot = NULL;

    for (; *p; p++)
        if (*p == '.')
            dot = p;

    return (char *)(dot ? dot : p);
}

/// Read an open file into a terminated buffer.  Used for the inputs that are
/// text rather than objects: linker scripts, .def files and .tbd files.
ST_FUNC char *tli_load_text(i32 fd)
{
    long len = lseek(fd, 0, SEEK_END);
    char *buf;

    if (len < 0)
        len = 0;

    buf = load_data(fd, 0, len + 1);
    buf[len] = '\0';
    return buf;
}

/// Replace *pp by a copy of 'str', or by nothing if it is NULL.  This is how
/// every string setting on the state is written.
static void tli_set_str(char **pp, const char *str)
{
    tli_free(*pp);
    *pp = str ? tli_strdup(str) : NULL;
}

/// Append 'str' to *pp, putting 'sep' between the old and new text unless the
/// old text is empty.  This is how repeated -rpath options accumulate.
static void tli_concat_str(char **pp, const char *str, i32 sep)
{
    usize had = *pp ? strlen(*pp) : 0;
    usize add = strlen(str);
    i32 join = *pp != NULL && sep != 0;
    char *p = tli_realloc(*pp, had + join + add + 1);

    if (join)
        p[had++] = sep;

    memcpy(p + had, str, add + 1);
    *pp = p;
}

/// ---------------------------------------------------------------------------
/// Dynamic arrays
///
/// A pointer array that grows: 'ptab' points at the array pointer and 'nb_ptr'
/// at its length.  Anything the linker collects an unknown number of, from
/// sections to command line files, lives in one.

/// Append one pointer, resizing the array when it is full.
ST_FUNC void dynarray_add(void *ptab, i32 *nb_ptr, void *data)
{
    void **pp = *(void ***)ptab;
    i32 nb = *nb_ptr;

    // The capacity is always a power of two, which makes it derivable from the
    // count: a resize is due exactly when the count is zero or is itself a
    // power of two.  That saves storing a capacity beside every array.
    if ((nb & (nb - 1)) == 0) {
        pp = tli_realloc(pp, (nb ? nb * 2 : 1) * sizeof *pp);
        *(void ***)ptab = pp;
    }
    pp[nb] = data;
    *nb_ptr = nb + 1;
}

/// Free every element, then the array itself, and leave it empty and reusable.
ST_FUNC void dynarray_reset(void *pp, i32 *n)
{
    void **p = *(void ***)pp;
    i32 i;

    for (i = 0; i < *n; i++)
        if (p[i])
            tli_free(p[i]);

    tli_free(p);
    *(void **)pp = NULL;
    *n = 0;
}

/// Split a command line into words, the way a shell would: runs of whitespace
/// separate, double quotes protect whitespace, and a backslash before a quote
/// or another backslash passes it through.  An unterminated quote runs to the
/// end rather than being an error, which is what makes '@listfile' forgiving.
/// Each word is added as its own allocation.
static void split_words(char ***argv, i32 *argc, const char *p)
{
    while (*p) {
        tli_CString word;
        i32 quoted = 0;

        while (*p && (u8)*p <= ' ')
            p++;

        if (!*p)
            break;

        cstr_new(&word);
        while (*p) {
            i32 c = (u8)*p;

            if (c == '\\' && (p[1] == '"' || p[1] == '\\')) {
                cstr_ccat(&word, p[1]);
                p += 2;
                continue;
            }

            if (c == '"') {
                quoted = !quoted;
                p++;
                continue;
            }

            // whitespace ends the word, unless a quote is protecting it
            if (c <= ' ' && !quoted) {
                p++;
                break;
            }

            cstr_ccat(&word, c);
            p++;
        }
        cstr_ccat(&word, '\0');
        dynarray_add(argv, argc, word.data);
    }
}

/// Split on a single separator, for the comma-separated tail of '-Wl,'.  There
/// is no quoting here: ld does not offer any, and an option's value is allowed
/// to contain spaces.  Whitespace before a word is dropped, so '-Wl,-e, main'
/// means what it looks like; an empty word between two separators is kept, so
/// that it can be rejected by name.
static void split_on(char ***argv, i32 *argc, const char *p, i32 sep)
{
    while (*p) {
        tli_CString word;

        while (*p && (u8)*p <= ' ')
            p++;

        if (!*p)
            break;

        cstr_new(&word);
        while (*p && *p != sep)
            cstr_ccat(&word, (u8)*p++);

        p += *p == sep;
        cstr_ccat(&word, '\0');
        dynarray_add(argv, argc, word.data);
    }
}

/// Split a search path on the platform's path separator and add each component
/// to an array.  '{B}' stands for the directory holding the target's support
/// files (CONFIG_TLIDIR, or -B) and '{R}' for the sysroot, so that the compiled
/// in defaults can be written once and still follow those options.  Empty
/// components are dropped rather than turning into the current directory.
static void tli_split_path(tli_State *s1, void *p_ary, i32 *p_nb_ary, const char *in)
{
    for (;;) {
        tli_CString path;
        const char *p;

        cstr_new(&path);
        for (p = in; *p && *p != PATHSEP[0]; p++) {

            // a two-letter name in braces stands for a directory
            if (*p == '{' && p[1] && p[2] == '}') {
                if (p[1] == 'B')
                    cstr_cat(&path, s1->tli_lib_path, -1);
                else if (p[1] == 'R')
                    cstr_cat(&path, CONFIG_SYSROOT, -1);

                p += 2;
            } else {
                cstr_ccat(&path, *p);
            }
        }

        if (path.size) {
            cstr_ccat(&path, '\0');
            dynarray_add(p_ary, p_nb_ary, path.data);
        } else {
            cstr_free(&path);
        }

        if (!*p)
            return;

        in = p + 1;
    }
}

/// ---------------------------------------------------------------------------
/// Diagnostics
///
/// Everything the linker has to say about an input goes through here, so that
/// an embedder can capture it all with one callback instead of watching stderr.

/// What a report turns out to be.  A warning stays one: there is no -W option
/// to promote or silence it, so nothing ever rewrites this on the way through.
enum { ERROR_WARN, ERROR_NOABORT, ERROR_ERROR };

/// How many errors are worth printing before the list stops helping.
#define TL_MAX_REPORTED_ERRORS 100

/// Format one report and deliver it, to the embedder's callback where there is
/// one and to stderr otherwise.  An ERROR_ERROR with a jump buffer installed
/// does not return.
static void error1(i32 mode, const char *fmt, va_list ap)
{
    tli_State *s1 = tli_state;
    tli_CString msg;

    // The macros in tl_internal.h took the lock on the way in; a report is the
    // end of that, whether or not the caller carries on afterwards.
    tli_exit_state(s1);

    // A damaged input can be wrong in as many places as it has symbols, and
    // reporting each one is no more useful than reporting the first hundred.
    // One corrupted object was measured at 9,895,933 identical lines, which
    // reads as a hung linker rather than a rejected file. Counting carries on
    // so the link still fails; only the printing stops.
    if (mode != ERROR_WARN && s1->nb_errors >= TL_MAX_REPORTED_ERRORS) {
        if (s1->nb_errors == TL_MAX_REPORTED_ERRORS) {
            const char *enough = "tinylinker: too many errors, not listing any more";

            if (s1->error_func)
                s1->error_func(s1->error_opaque, enough);
            else
                fprintf(stderr, "%s\n", enough), fflush(stderr);
        }
        s1->nb_errors++;
        return;
    }

    cstr_new(&msg);

    // Name the input being read, if there is one; otherwise the linker owns
    // the complaint itself.
    cstr_printf(&msg, "%s: ", s1->current_filename ? s1->current_filename
                                                   : "tinylinker");
    cstr_printf(&msg, mode == ERROR_WARN ? "warning: " : "error: ");
    cstr_vprintf(&msg, fmt, ap);

    if (s1->error_func) {
        s1->error_func(s1->error_opaque, msg.data);
    } else {
        fflush(stdout); // so that -v output stays in order with this
        fprintf(stderr, "%s\n", msg.data);
        fflush(stderr);
    }
    cstr_free(&msg);

    if (mode == ERROR_WARN)
        return;

    s1->nb_errors++;
    if (mode == ERROR_ERROR && s1->error_set_jmp_enabled)
        longjmp(s1->error_jmp_buf, 1);

}

/// Catch everything the linker reports, instead of watching stderr for it.
TLI_PUBLIC void tli_set_error_func(tli_State *s, void *error_opaque, TLIErrorFunc *error_func)
{
    s->error_opaque = error_opaque;
    s->error_func = error_func;
}

/// Report an error and carry on, so that one run can report every bad input
/// rather than the first.  Always returns -1, to be returned in turn.
PUB_FUNC i32 _tcc_error_noabort(const char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);
    error1(ERROR_NOABORT, fmt, ap);
    va_end(ap);
    return -1;
}

#undef _tcc_error
/// Report an error there is no continuing from.  If the caller installed a jump
/// buffer this does not return to it; otherwise the process ends.
PUB_FUNC void _tcc_error(const char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);
    error1(ERROR_ERROR, fmt, ap);
    exit(1);
}
#define _tcc_error use_tcc_error_noabort

/// Report something that need not stop the link.  Nothing counts it, so a run
/// that only warns still succeeds.
PUB_FUNC void _tcc_warning(const char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);
    error1(ERROR_WARN, fmt, ap);
    va_end(ap);
}

/// ---------------------------------------------------------------------------
/// The state

/// Make a linker for one architecture and object format.  Everything the link
/// will need hangs off the result, and nothing is shared with any other state,
/// so several can run at once.
TLI_PUBLIC tli_State *tli_new(const tli_Arch *arch, i32 fmt)
{
    tli_State *s1 = tli_mallocz(sizeof(tli_State));

    s1->arch = arch;
    s1->fmt = fmt;
    s1->native = TLI_IS_NATIVE(arch->em, fmt);

#ifdef MEM_DEBUG
    tli_memcheck(1);
#endif

    // Give a tentative definition an address in .bss rather than leaving it a
    // common symbol.  Producing a final image means placing it either way.
    s1->nocommon = 1;

    if (s1->fmt == TLI_FMT_MACHO)
        s1->leading_underscore = 1;

    if (s1->arch->em == EM_ARM)
        s1->float_abi = ARM_HARD_FLOAT;

    if (s1->arch->em == EM_AARCH64 && s1->fmt == TLI_FMT_PE)
        s1->pe_dll_characteristics = 0x8160;

#ifdef CONFIG_NEW_DTAGS
    s1->enable_new_dtags = 1;
#endif
    tli_set_lib_path(s1, CONFIG_TLIDIR);
#ifdef TLI_SWITCHES // options compiled in as if they were typed
    tli_set_options(s1, TLI_SWITCHES);
#endif
    return s1;
}

/// Release a state and everything it accumulated: the sections, the search
/// paths, the recorded files and the libraries opened on its behalf.
TLI_PUBLIC void tli_delete(tli_State *s1)
{
    tli_elf_delete(s1);

    dynarray_reset(&s1->library_paths, &s1->nb_library_paths);
    dynarray_reset(&s1->crt_paths, &s1->nb_crt_paths);
    dynarray_reset(&s1->files, &s1->nb_files);
    dynarray_reset(&s1->pragma_libs, &s1->nb_pragma_libs);
    dynarray_reset(&s1->link_argv, &s1->link_argc);

    tli_free(s1->tli_lib_path);
    tli_free(s1->soname);
    tli_free(s1->rpath);
    tli_free(s1->elfint);
    tli_free(s1->elf_entryname);
    tli_free(s1->init_symbol);
    tli_free(s1->fini_symbol);
    tli_free(s1->mapfile);
    tli_free(s1->outfile);
    tli_free(s1->install_name);
#ifdef TLI_HOST_RUN
    // This closes the libraries opened to resolve against the host process, so
    // it has to happen while loaded_dlls still holds their handles.
    tli_run_free(s1);
#endif
    dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls);
    dynarray_reset(&s1->coff_aliases, &s1->nb_coff_aliases);

    tli_free(s1);
#ifdef MEM_DEBUG
    tli_memcheck(-1);
#endif
}

/// Fix what kind of output is being produced.  This creates the sections and
/// settles the default search paths, so it has to happen before any input is
/// added and cannot be changed afterwards.
TLI_PUBLIC i32 tli_set_output_type(tli_State *s1, i32 output_type)
{
    s1->output_type = output_type;

    tli_elf_new(s1);

    // A writer refuses a machine it has no numbers for: PE needs the
    // machine's PE constants, and Mach-O its CPU type, which only x86_64 and
    // arm64 have.  The Mach-O code relies on this to know it is one of them.
    if (s1->fmt == TLI_FMT_PE && !s1->arch->pe_machine)
        return tli_error_noabort("PE output is not supported for this architecture");

    if (s1->fmt == TLI_FMT_MACHO && !s1->arch->macho_cputype)
        return tli_error_noabort("Mach-O output is not supported for this architecture");

    // A relocatable object keeps the section table it was given and needs no
    // libraries, so none of what follows applies to it.
    if (output_type == TLI_OUTPUT_OBJ) {
        s1->output_format = TLI_OUTPUT_FORMAT_ELF;
        return 0;
    }

    if (!s1->nostdlib_paths)
        tli_add_library_path(s1, s1->fmt == TLI_FMT_PE ? TLI_PE_LIBPATHS
                                                       : TLI_LIBPATHS);

    switch (s1->fmt) {
    case TLI_FMT_PE:
#if defined TLI_HOST_RUN && defined _WIN32
        // so that a system DLL can be named directly
        if (s1->native)
            tli_add_systemdir(s1);
#endif
        break;
    case TLI_FMT_MACHO:
#if defined TLI_HOST_RUN && defined __APPLE__
        if (s1->native)
            tli_add_macos_sdkpath(s1);
#endif
        break;
    default:
        tli_split_path(s1, &s1->crt_paths, &s1->nb_crt_paths, TLI_CRTPREFIX);

        // The startup objects come first in the link order, so they are added
        // now rather than when the output is written.  This can fail.
        if (output_type != TLI_OUTPUT_MEMORY && !s1->nostdlib)
            tli_elf_add_crtbegin(s1);
        break;
    }
    return s1->nb_errors ? -1 : 0;
}

/// Add one or more directories to search for libraries in, separated by the
/// platform's path separator.
TLI_PUBLIC i32 tli_add_library_path(tli_State *s1, const char *pathname)
{
    tli_split_path(s1, &s1->library_paths, &s1->nb_library_paths, pathname);
    return 0;
}

/// Say where this target's support files live, which is what '{B}' in a search
/// path expands to.
TLI_PUBLIC void tli_set_lib_path(tli_State *s1, const char *path)
{
    tli_set_str(&s1->tli_lib_path, path);
}

/// Look up a shared library by the name it is recorded under, adding it if it
/// is not there yet.  'level' is how far from the command line it was found: 0
/// for one that was asked for, higher for one pulled in by another.  A level of
/// -1 only looks.
ST_FUNC tli_DLLReference *tli_add_dllref(tli_State *s1, const char *dllname, i32 level)
{
    tli_DLLReference *ref = NULL;
    i32 i;

    for (i = 0; i < s1->nb_loaded_dlls; i++)
        if (0 == strcmp(s1->loaded_dlls[i]->name, dllname)) {
            ref = s1->loaded_dlls[i];
            break;
        }

    if (level == -1)
        return ref;

    // seen again, and possibly from closer in than before
    if (ref) {
        if (level < ref->level)
            ref->level = level;

        ref->found = 1;
        return ref;
    }

    ref = tli_mallocz(sizeof(tli_DLLReference) + strlen(dllname));
    strcpy(ref->name, dllname);
    ref->level = level;
    dynarray_add(&s1->loaded_dlls, &s1->nb_loaded_dlls, ref);
    ref->index = s1->nb_loaded_dlls; // one based: 0 means "no dll"
    return ref;
}

/// ---------------------------------------------------------------------------
/// Getting inputs in

/// Open an input, reporting the attempt when -v was asked for twice (which is
/// how a search through the library paths is followed).
static i32 tli_open_input(tli_State *s1, const char *filename)
{
    i32 fd;

    if (0 == strcmp(filename, "-")) {
        fd = 0;
        filename = "<stdin>";
    } else {
        fd = open(filename, O_RDONLY | O_BINARY);
    }

    if (s1->verbose == 3 || (s1->verbose == 2 && fd >= 0))
        printf("%s %s\n", fd < 0 ? "nf" : "->", filename);

    return fd;
}

/// Hand a file that is neither an object nor an archive to whatever else the
/// target understands.  Each of the three output formats has its own idea of
/// what that can be, so there is one of these per format.

/// On ELF systems that is a shared library or an ld script.
static i32 elf_load_other(tli_State *s1, i32 obj_type, i32 flags,
                          const char *filename, i32 fd)
{
    // As GNU ld does, anything unrecognised is tried as a linker script;
    // that is how a libc.so that is really a text file gets read.
    if (obj_type != AFF_BINTYPE_DYN)
        return tli_load_ldscript(s1, fd);

    if (s1->output_type != TLI_OUTPUT_MEMORY)
        return tli_load_dll(s1, fd, filename, (flags & AFF_REFERENCED_DLL) != 0);

#if defined TLI_HOST_RUN && !defined _WIN32
    // Linking into this process: let the dynamic loader do the work, so that
    // the symbols resolve to the copy already mapped.
    {
        void *dl = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY);

        if (!dl)
            return FILE_NOT_RECOGNIZED;

        tli_add_dllref(s1, filename, 0)->handle = dl;
    }
#endif
    return 0;
}

/// On Mach-O that is a dylib or the .tbd stub standing in for one.
static i32 macho_load_other(tli_State *s1, i32 obj_type, i32 flags,
                            const char *filename, i32 fd)
{
    i32 is_tbd;

    // A .tbd is a text stub standing in for a dylib that is not on disk
    // because it lives in the shared cache.  Neither kind is identifiable from
    // a magic number alone, so the extension has the last word.
    if (obj_type != AFF_BINTYPE_DYN) {
        const char *ext = tli_fileextension(filename);

        if (0 == strcmp(ext, ".dylib"))
            obj_type = AFF_BINTYPE_DYN;
        else if (0 != strcmp(ext, ".tbd"))
            return FILE_NOT_RECOGNIZED;
    }
    is_tbd = obj_type != AFF_BINTYPE_DYN;

    if (s1->output_type == TLI_OUTPUT_MEMORY) {
#if defined TLI_HOST_RUN && defined __APPLE__
        const char *soname = filename;
        char *tmp = NULL;
        void *dl;

        // dlopen cannot open a stub, but the stub says what to open.
        if (is_tbd && (tmp = macho_tbd_soname(fd)) != NULL)
            soname = tmp;

        dl = dlopen(soname, RTLD_GLOBAL | RTLD_LAZY);
        if (dl)
            tli_add_dllref(s1, soname, 0)->handle = dl;

        tli_free(tmp);
        if (!dl)
            return FILE_NOT_RECOGNIZED;
#endif
        return 0;
    }

    if (is_tbd
        ? macho_load_tbd(s1, fd, filename, (flags & AFF_REFERENCED_DLL) != 0)
        : macho_load_dll(s1, fd, filename, (flags & AFF_REFERENCED_DLL) != 0))
        return FILE_NOT_RECOGNIZED;

    return 0;
}

/// On PE that is a DLL or a .def file describing one.
static i32 pe_load_other(tli_State *s1, const char *filename, i32 fd)
{
    // .def files and DLLs alike: the PE reader tells them apart itself.
    return pe_load_file(s1, fd, filename) ? FILE_NOT_RECOGNIZED : 0;
}

/// Pass the file to the reader for this link's output format.
static i32 load_other(tli_State *s1, i32 obj_type, i32 flags,
                      const char *filename, i32 fd)
{
    switch (s1->fmt) {
    case TLI_FMT_PE:
        return pe_load_other(s1, filename, fd);
    case TLI_FMT_MACHO:
        return macho_load_other(s1, obj_type, flags, filename, fd);
    default:
        return elf_load_other(s1, obj_type, flags, filename, fd);
    }
}

/// Work out what an open file is and give it to the reader that wants it.
/// Closes the descriptor however it turns out.
static i32 tli_add_binary(tli_State *s1, i32 flags, const char *filename, i32 fd)
{
    const char *outer = s1->current_filename;
    ElfW(Ehdr) ehdr;
    i32 obj_type, ret;

    // Diagnostics from here down name this file rather than the one whose
    // load pulled it in.
    s1->current_filename = filename;

    obj_type = tli_object_type(s1, fd, &ehdr);
    lseek(fd, 0, SEEK_SET);

    switch (obj_type) {
    case AFF_BINTYPE_REL:
    case AFF_BINTYPE_MACHO_REL:
    case AFF_BINTYPE_COFF_REL:
        ret = tl_load_object(s1, fd, 0, obj_type, 0);
        break;
    case AFF_BINTYPE_AR:
        // Without --whole-archive an archive contributes only the members that
        // something already unresolved needs.
        ret = tli_load_archive(s1, fd, !(flags & AFF_WHOLE_ARCHIVE));
        break;
    default:
        ret = load_other(s1, obj_type, flags, filename, fd);
        break;
    }

    close(fd);
    s1->current_filename = outer;
    if (ret == FILE_NOT_RECOGNIZED)
        return tli_error_noabort("%s: unrecognized file type", filename);

    return ret;
}

#if defined TARGETOS_OpenBSD && !defined _WIN32
#include <glob.h>
/// OpenBSD shared libraries carry their version in the file name and there is
/// no unversioned symlink to follow, so '-lc' has to look at what is actually
/// there and take the newest.  'pattern' is the path with a '*' where the
/// version belongs; the result is written to 'buf' and its rank returned, or
/// -1 if nothing matched.
static i32 tli_glob_so(tli_State *s1, const char *pattern, char *buf, i32 size)
{
    const char *star = strchr(pattern, '*');
    i32 best = -1;
    glob_t g;
    usize i;

    if (!star || glob(pattern, 0, NULL, &g))
        return -1;

    // rank each match by the major.minor its name carries
    for (i = 0; i < g.gl_pathc; i++) {
        char *v = g.gl_pathv[i] + (star - pattern);
        char *end;
        long major, minor;

        major = strtol(v, &end, 10);
        if (end == v || *end != '.')
            continue;

        v = end + 1;
        minor = strtol(v, &end, 10);
        if (end == v)
            continue;

        if (major * 1000 + minor > best) {
            best = major * 1000 + minor;
            pstrcpy(buf, size, g.gl_pathv[i]);
        }
    }
    globfree(&g);
    return best;
}
#endif

/// Open a named file and read it, whatever it turns out to be.  Returns
/// FILE_NOT_FOUND rather than reporting, unless AFF_PRINT_ERROR was asked for,
/// because a caller searching a list of directories expects misses.
ST_FUNC i32 tli_add_file_internal(tli_State *s1, const char *filename, i32 flags)
{
    i32 fd;

#if defined TARGETOS_OpenBSD && !defined _WIN32
    char buf[1024];

    if (tli_glob_so(s1, filename, buf, sizeof buf) >= 0)
        filename = buf;
#endif

    // every input a linker takes is binary: an object, an archive, a shared
    // library, or (as GNU ld does) a text file interpreted as an ld script
    if (0 == (flags & AFF_TYPE_MASK))
        flags |= AFF_TYPE_BIN;

    fd = tli_open_input(s1, filename);
    if (fd < 0) {

        // A caller searching a list of directories expects this and tries the
        // next one, so it is only an error when it was asked to be.
        if (flags & AFF_PRINT_ERROR)
            tli_error_noabort("file '%s' not found", filename);

        return FILE_NOT_FOUND;
    }
    return tli_add_binary(s1, flags, filename, fd);
}

/// Add one named input, using the file type currently in force.  A file that
/// cannot be opened is reported here.
TLI_PUBLIC i32 tli_add_file(tli_State *s1, const char *filename)
{
    return tli_add_file_internal(s1, filename, s1->filetype | AFF_PRINT_ERROR);
}

/// Try 'fmt' filled in with each directory in turn and stop at the first hit.
/// A directory that does not have the file is not an error; running out of
/// directories is, if the caller wanted to hear about it.
static i32 tli_add_library_internal(tli_State *s1, const char *fmt,
    const char *filename, i32 flags, char **paths, i32 nb_paths)
{
    char buf[1024];
    i32 i, ret;

    for (i = 0; i < nb_paths; i++) {

        // A path too long for buf is tried cut short, and is simply not found.
        if (snprintf(buf, sizeof buf, fmt, paths[i], filename) < 0)
            continue;

        ret = tli_add_file_internal(s1, buf, flags & ~AFF_PRINT_ERROR);
        if (ret != FILE_NOT_FOUND)
            return ret;
    }

    if (flags & AFF_PRINT_ERROR)
        tli_error_noabort("%s '%s' not found",
            flags & AFF_TYPE_LIB ? "library" : "file", filename);

    return FILE_NOT_FOUND;
}

/// Find and load a file by its exact name, anywhere in the library path.
ST_FUNC i32 tli_add_dll(tli_State *s1, const char *filename, i32 flags)
{
    return tli_add_library_internal(s1, "%s/%s", filename, flags,
        s1->library_paths, s1->nb_library_paths);
}

/// Load a support object from the lib path. tinylinker ships no support
/// library of its own, so 'quiet' lets callers treat a missing one as a
/// normal outcome rather than an error.
ST_FUNC i32 tli_add_support(tli_State *s1, const char *filename, i32 quiet)
{
    char buf[100];

    // a cross build keeps its target's objects under a prefixed name
    if (TLI_CROSSPREFIX[0])
        filename = strcat(strcpy(buf, TLI_CROSSPREFIX), filename);

    return tli_add_dll(s1, filename, quiet ? 0 : AFF_PRINT_ERROR);
}

/// The startup objects, which live with the C library rather than with us and
/// so are searched for along their own path.
ST_FUNC i32 tli_add_crt(tli_State *s1, const char *filename)
{
    return tli_add_library_internal(s1, "%s/%s",
        filename, AFF_PRINT_ERROR, s1->crt_paths, s1->nb_crt_paths);
}

/// Resolve one -l name.  The spellings are tried in the order the platform
/// prefers, which puts the shared library ahead of the archive unless static
/// linking was asked for.
TLI_PUBLIC i32 tli_add_library(tli_State *s1, const char *libraryname)
{
    static const char * const pe_forms[] = {
        "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", NULL
    };
    static const char * const macho_forms[] = {
        "%s/lib%s.dylib", "%s/lib%s.tbd", NULL
    };
    static const char * const elf_forms[] = {
#if defined TARGETOS_OpenBSD
        "%s/lib%s.so.*",
#else
        "%s/lib%s.so",
#endif
        NULL
    };
    static const char static_form[] = "%s/lib%s.a";
    const char * const *shared_forms = s1->fmt == TLI_FMT_PE ? pe_forms
        : s1->fmt == TLI_FMT_MACHO ? macho_forms : elf_forms;
    i32 flags = AFF_TYPE_LIB | (s1->filetype & AFF_WHOLE_ARCHIVE);
    i32 i, ret;

    // A leading colon means the name is the file name, already complete.
    if (*libraryname == ':')
        return tli_add_dll(s1, libraryname + 1, flags | AFF_PRINT_ERROR);

    if (!s1->static_link)
        for (i = 0; shared_forms[i]; i++) {
            ret = tli_add_library_internal(s1, shared_forms[i], libraryname,
                flags, s1->library_paths, s1->nb_library_paths);
            if (ret != FILE_NOT_FOUND)
                return ret;
        }

    ret = tli_add_library_internal(s1, static_form, libraryname, flags,
                                   s1->library_paths, s1->nb_library_paths);
    if (ret != FILE_NOT_FOUND)
        return ret;

    // Last chance: the name as given, in case it is a file after all.
    return tli_add_dll(s1, libraryname, flags | AFF_PRINT_ERROR);
}

/// Libraries an input object asked for itself, via #pragma comment(lib,).
ST_FUNC void tli_add_pragma_libs(tli_State *s1)
{
    i32 i;

    for (i = 0; i < s1->nb_pragma_libs; i++)
        tli_add_library(s1, s1->pragma_libs[i]);
}

/// ---------------------------------------------------------------------------
/// Options
///
/// There are two grammars here.  The outer one is the compiler-driver spelling
/// a build system produces, of which the linker only cares about a fraction;
/// the inner one is what ld itself accepts, which arrives inside '-Wl,'.  The
/// two are parsed separately because they disagree: ld's long options take one
/// dash, values follow an '=' or a comma rather than a space, and several of
/// them can be negated by a prefix.
///
/// Nothing here links anything.  Files are collected in order and the settings
/// are recorded on the state; tli_add_parsed_files() does the work afterwards,
/// because an option late on the command line still applies to a file named
/// early on it.

/// If 'val' is a prefix of **str, step past it and say so.
static i32 strstart(const char *val, const char **str)
{
    const char *p = *str;

    while (*val) {
        if (*p != *val)
            return 0;

        p++, val++;
    }
    *str = p;
    return 1;
}

/// Record a file to be linked later.  The type says how it should be looked
/// for, and carries the flags in force when it was named, so that
/// --whole-archive applies to what follows it and not to everything.
static void args_parser_add_file(tli_State *s1, const char *filename, i32 filetype)
{
    struct tli_FileSpec *f = tli_malloc(sizeof *f + strlen(filename));

    f->type = filetype;
    strcpy(f->name, filename);
    dynarray_add(&s1->files, &s1->nb_files, f);
}

/// ---------------------------------------------------------------- -Wl,

/// How an ld option takes its value.  LD_NONE: the option is the whole word.
/// LD_VALUE: '-opt=value', or '-opt' with the value as the next word.
/// LD_JOINED: as LD_VALUE, but the value may also be run on: '-Ivalue'.
enum { LD_NONE, LD_VALUE, LD_JOINED };

#define LD_NEG    1 /// A 'no-' (or, on PE, 'disable-') prefix clears it.

/// An option row that only one output format understands.  The matcher skips
/// it for the others, exactly as though it were not in the table.
#define ONLY_PE    2
#define ONLY_MACHO 4

/// Whether an option row carrying these ONLY_* flags applies to this link.
static i32 opt_for_fmt(tli_State *s1, i32 flags)
{
    if (flags & ONLY_PE)
        return s1->fmt == TLI_FMT_PE;

    if (flags & ONLY_MACHO)
        return s1->fmt == TLI_FMT_MACHO;

    return 1;
}

/// What an ld option does, once matched.  The name is only a spelling: several
/// spellings share one of these.
enum {
    LDO_entry, LDO_text_addr, LDO_init, LDO_fini, LDO_mapfile, LDO_oformat,
    LDO_rdynamic, LDO_rpath, LDO_interp, LDO_new_dtags, LDO_section_align,
    LDO_soname, LDO_symbolic, LDO_no_std_paths, LDO_whole_archive,
    LDO_znodelete, LDO_libpath, LDO_library, LDO_nothing,
    LDO_pe_large_address, LDO_pe_dynamicbase, LDO_pe_high_entropy,
    LDO_pe_nxcompat, LDO_pe_tsaware, LDO_pe_file_align, LDO_pe_stack,
    LDO_pe_subsystem,
    LDO_all_load, LDO_force_load,
};

/// One row of the '-Wl,' option table.
typedef struct tli_LdOption {
    const char *name;   /// The spelling, without its leading dash or dashes.
    u8 kind;            /// Which LDO_ this is.
    u8 arg;             /// LD_NONE, LD_VALUE or LD_JOINED.
    u8 flags;           /// LD_NEG and ONLY_*, or zero.
} tli_LdOption;

/// The order matters where one name is a prefix of another: the run-on forms
/// ('-L', '-l', '-I') would swallow any longer name that starts with the same
/// letter, so they come last.
static const tli_LdOption ld_options[] = {
    { "Bsymbolic",           LDO_symbolic,        LD_NONE,   0 }, /// -Bsymbolic
    { "nostdlib",            LDO_no_std_paths,    LD_NONE,   0 }, /// no default paths
    { "e",                   LDO_entry,           LD_VALUE,  0 }, /// entry point
    { "entry",               LDO_entry,           LD_VALUE,  0 }, /// the long spelling
    { "image-base",          LDO_text_addr,       LD_VALUE,  0 }, /// where the text goes
    { "Ttext",               LDO_text_addr,       LD_VALUE,  0 }, /// the ld spelling of it
    { "init",                LDO_init,            LD_VALUE,  0 }, /// recorded, not called
    { "fini",                LDO_fini,            LD_VALUE,  0 }, /// recorded, not called
    { "Map",                 LDO_mapfile,         LD_VALUE,  0 }, /// recorded, not written
    { "oformat",             LDO_oformat,         LD_VALUE,  0 }, /// checked, not acted on
    { "export-all-symbols",  LDO_rdynamic,        LD_NONE,   0 }, /// keep the dynamic symbols
    { "export-dynamic",      LDO_rdynamic,        LD_NONE,   0 }, /// the same thing
    { "E",                   LDO_rdynamic,        LD_NONE,   0 }, /// and its short form
    { "rpath",               LDO_rpath,           LD_VALUE,  0 }, /// accumulates, colon joined
    { "dynamic-linker",      LDO_interp,          LD_VALUE,  0 }, /// the .interp string
    { "enable-new-dtags",    LDO_new_dtags,       LD_NONE,   0 }, /// RUNPATH rather than RPATH
    { "section-alignment",   LDO_section_align,   LD_VALUE,  0 }, /// hexadecimal
    { "soname",              LDO_soname,          LD_VALUE,  0 }, /// the name a dll records
    { "install_name",        LDO_soname,          LD_VALUE,  0 }, /// what Mach-O calls it
    { "whole-archive",       LDO_whole_archive,   LD_NONE,   LD_NEG }, /// take every member
    { "znodelete",           LDO_znodelete,       LD_NONE,   0 }, /// DF_1_NODELETE
    { "large-address-aware", LDO_pe_large_address, LD_NONE,  ONLY_PE }, /// past 2GB is fine
    { "dynamicbase",         LDO_pe_dynamicbase,  LD_NONE,   LD_NEG | ONLY_PE }, /// relocatable image
    { "high-entropy-va",     LDO_pe_high_entropy, LD_NONE,   LD_NEG | ONLY_PE }, /// 64-bit ASLR
    { "nxcompat",            LDO_pe_nxcompat,     LD_NONE,   LD_NEG | ONLY_PE }, /// no executable data
    { "tsaware",             LDO_pe_tsaware,      LD_NONE,   LD_NEG | ONLY_PE }, /// terminal-server ok
    { "file-alignment",      LDO_pe_file_align,   LD_VALUE,  ONLY_PE }, /// hexadecimal
    { "stack",               LDO_pe_stack,        LD_VALUE,  ONLY_PE }, /// decimal, unlike the rest
    { "subsystem",           LDO_pe_subsystem,    LD_VALUE,  ONLY_PE }, /// console, windows, ...
    { "all_load",            LDO_all_load,        LD_NONE,   ONLY_MACHO }, /// whole-archive, Mach-O
    { "force_load",          LDO_force_load,      LD_VALUE,  ONLY_MACHO }, /// the same, one archive
    { "single_module",       LDO_nothing,         LD_NONE,   ONLY_MACHO }, /// always true here
    { "as-needed",           LDO_nothing,         LD_NONE,   0 }, /// every dll is kept
    { "O",                   LDO_nothing,         LD_NONE,   0 }, /// nothing to tune
    { "z",                   LDO_nothing,         LD_VALUE,  0 }, /// eats its keyword
    { "I",                   LDO_interp,          LD_JOINED, 0 }, /// run-on: must stay late
    { "L",                   LDO_libpath,         LD_JOINED, 0 }, /// run-on: must stay late
    { "l",                   LDO_library,         LD_JOINED, 0 }, /// run-on: must stay late
    { NULL,                  0,                   0,         0 }, /// end of the table
};

/// Match one '-Wl,' word.  Returns the option and, through the out parameters,
/// its value and whether a negating prefix was in front of it.
///
/// A value that is a word of its own is taken from the list here, advancing
/// link_optind past it.  If there is no such word yet, *pending says so: a
/// driver is allowed to write '-Wl,-e -Wl,main', in which case the option waits
/// where it is until the next '-Wl,' appends more.
static const tli_LdOption *ld_match(tli_State *s1, const char *word,
                                const char **value, i32 *on, i32 *pending)
{
    const tli_LdOption *o;
    const char *name = word;

    *pending = 0;
    if (*name++ != '-')
        return NULL;

    if (*name == '-') // one dash or two, as ld accepts both
        name++;

    for (o = ld_options; o->name; o++) {
        const char *rest = name;
        i32 sign = 1;

        if (!opt_for_fmt(s1, o->flags))
            continue;

        // a negatable option may be spelled with a clearing prefix
        if (o->flags & LD_NEG) {
            if (0 == strncmp(rest, "no-", 3))
                rest += 3, sign = 0;
            else if (s1->fmt == TLI_FMT_PE && 0 == strncmp(rest, "disable-", 8))
                rest += 8, sign = 0;
        }

        if (!strstart(o->name, &rest))
            continue;

        // what is left after the name decides whether this row really matches,
        // and where its value comes from
        if (o->arg == LD_NONE) {
            if (*rest)
                continue; // a longer name starting the same way
        } else if (o->arg == LD_JOINED ? *rest != '\0' : *rest == '=') {
            rest += o->arg == LD_VALUE; // step over the '='
        } else if (*rest) {
            continue;
        } else if (s1->link_optind + 1 < s1->link_argc) {
            rest = s1->link_argv[++s1->link_optind];
        } else {
            *pending = 1;
            continue; // another name might still match outright
        }
        *value = rest;
        *on = sign;
        return o;
    }
    return NULL;
}

/// Set or clear bits according to 'on'.
#define SET_OR_CLEAR(on, v, f)   ((v) = (on) ? (v) | (f) : (v) & ~(f))

/// The same, where turning one PE characteristic on implies another, so that
/// clearing takes its own mask.
#define SET_OR_CLEAR_2(on, v, set_f, clr_f) \
                                 ((v) = (on) ? (v) | (set_f) : (v) & ~(clr_f))

/// Parse the tail of one -Wl, option.  Words accumulate in link_argv across
/// calls, because an option and its value can arrive in separate ones.
static i32 tli_set_linker(tli_State *s1, const char *optarg)
{
    split_on(&s1->link_argv, &s1->link_argc, optarg, ',');

    while (s1->link_optind < s1->link_argc) {
        const char *word = s1->link_argv[s1->link_optind];
        const char *val = "";
        const tli_LdOption *o;
        i32 on = 1, pending;

        o = ld_match(s1, word, &val, &on, &pending);
        if (!o) {
            if (pending)
                return 0; // the value is expected in the next -Wl,

            return tli_error_noabort("unsupported linker option '%s'", word);
        }

        switch (o->kind) {
        case LDO_symbolic:
            s1->symbolic = 1;
            break;
        case LDO_no_std_paths:
            s1->nostdlib_paths = 1;
            break;
        case LDO_entry:
            tli_set_str(&s1->elf_entryname, val);
            break;
        case LDO_text_addr:
            s1->text_addr = strtoull(val, NULL, 16);
            s1->has_text_addr = 1;
            break;
        case LDO_init:
            tli_set_str(&s1->init_symbol, val);
            break;
        case LDO_fini:
            tli_set_str(&s1->fini_symbol, val);
            break;
        case LDO_mapfile:
            tli_set_str(&s1->mapfile, val);
            break;
        case LDO_oformat: {
            const char *rest = val;

            // There is only one object format a link can write, so the name
            // is checked rather than acted on.  'binary' is the one real
            // choice: the image with no headers around it.  Mach-O accepts
            // the ELF name.
            if (strstart(s1->fmt == TLI_FMT_PE ? "pe-"
                         : PTR_SIZE == 8 ? "elf64-" : "elf32-", &rest))
                s1->output_format = TLI_OUTPUT_FORMAT_ELF;
            else if (0 == strcmp("binary", val))
                s1->output_format = TLI_OUTPUT_FORMAT_BINARY;
            else
                return tli_error_noabort("unsupported linker option '%s'", word);
            break;
        }
        case LDO_rdynamic:
            s1->rdynamic = 1;
            break;
        case LDO_rpath:
            // several -rpath options make one colon separated run path
            tli_concat_str(&s1->rpath, val, ':');
            break;
        case LDO_interp:
            tli_set_str(&s1->elfint, val);
            break;
        case LDO_new_dtags:
            s1->enable_new_dtags = 1;
            break;
        case LDO_section_align:
            s1->section_align = strtoul(val, NULL, 16);
            break;
        case LDO_soname:
            tli_set_str(&s1->soname, val);
            break;
        case LDO_whole_archive:
            SET_OR_CLEAR(on, s1->filetype, AFF_WHOLE_ARCHIVE);
            break;
        case LDO_znodelete:
            s1->znodelete = 1;
            break;
        case LDO_libpath:
            tli_add_library_path(s1, val);
            break;
        case LDO_library:
            args_parser_add_file(s1, val,
                AFF_TYPE_LIB | (s1->filetype & ~AFF_TYPE_MASK));
            break;
        case LDO_pe_large_address:
            s1->pe_characteristics |= 0x20;
            break;
        case LDO_pe_dynamicbase:
            SET_OR_CLEAR_2(on, s1->pe_dll_characteristics, 0x40, 0x60);
            break;
        case LDO_pe_high_entropy:
            // high entropy implies a relocatable base
            SET_OR_CLEAR_2(on, s1->pe_dll_characteristics, 0x60, 0x20);
            break;
        case LDO_pe_nxcompat:
            SET_OR_CLEAR(on, s1->pe_dll_characteristics, 0x100);
            break;
        case LDO_pe_tsaware:
            SET_OR_CLEAR(on, s1->pe_dll_characteristics, 0x8000);
            break;
        case LDO_pe_file_align:
            s1->pe_file_align = strtoul(val, NULL, 16);
            break;
        case LDO_pe_stack:
            s1->pe_stack_size = strtoul(val, NULL, 10);
            break;
        case LDO_pe_subsystem:
            if (pe_setsubsy(s1, val) < 0)
                return tli_error_noabort("unsupported linker option '%s'", word);
            break;
        case LDO_all_load:
            s1->filetype |= AFF_WHOLE_ARCHIVE;
            break;
        case LDO_force_load:
            args_parser_add_file(s1, val, AFF_TYPE_LIB | AFF_WHOLE_ARCHIVE);
            break;
        default:
            break;
        }

        ++s1->link_optind;
    }
    return 0;
}

/// ---------------------------------------------------------------- driver

/// 'a', 'a.b' or 'a.b.c', packed as Mach-O stores it: sixteen bits and then
/// two lots of eight.
static u32 parse_version(tli_State *s1, const char *version)
{
    u64 a, b = 0, c = 0;
    char *p;

    a = strtoul(version, &p, 10);
    if (*p == '.') {
        b = strtoul(p + 1, &p, 10);
        if (*p == '.')
            c = strtoul(p + 1, &p, 10);
    }

    // anything left over, or a field too wide to pack, is a bad version
    if (*p || a > 0xffff || b > 0xff || c > 0xff)
        tli_error_noabort("version a.b.c not correct: %s", version);

    return (u32)((a << 16) | (b << 8) | c);
}

/// How a driver option takes its value.
enum {
    NO_ARG,       /// The option is the whole word.
    ARG_JOINED,   /// The value is whatever follows, possibly nothing.
    ARG_SEPARATE  /// The value follows, or is the next word.
};

/// What a driver option does.  OPT_ignored is deliberately zero, so that a row
/// left blank does nothing.
enum {
    OPT_ignored = 0,
    OPT_libpath, OPT_library, OPT_supportdir, OPT_output,
    OPT_static, OPT_shared, OPT_soname, OPT_rdynamic, OPT_strip,
    OPT_relocatable, OPT_compile, OPT_nostdlib, OPT_linker, OPT_verbose,
    OPT_whole_archive, OPT_no_whole_archive,
    OPT_backtrace, OPT_bcheck,
    OPT_dynamiclib, OPT_install_name,
    OPT_compat_version, OPT_current_version,
};

/// One row of the driver option table.
typedef struct tli_DriverOption {
    const char *name;  /// The spelling, without its leading dash.
    u8 kind;           /// Which OPT_ this is.
    u8 arg;            /// NO_ARG, ARG_JOINED or ARG_SEPARATE.
    u8 flags;          /// ONLY_PE or ONLY_MACHO, or zero for every format.
} tli_DriverOption;

/// An entry that takes a value treats whatever follows the name as that value,
/// so such a name must not be a prefix of a later entry's, or the later one
/// would never be reached.  The exact-match entries are safe in any order.
static const tli_DriverOption driver_options[] = {
    { "L",         OPT_libpath,          ARG_SEPARATE, 0 }, /// library search path
    { "l",         OPT_library,          ARG_SEPARATE, 0 }, /// link a library
    { "B",         OPT_supportdir,       ARG_SEPARATE, 0 }, /// where '{B}' points
    { "o",         OPT_output,           ARG_SEPARATE, 0 }, /// output file
    { "static",    OPT_static,           NO_ARG,       0 }, /// archives only
    { "shared",    OPT_shared,           NO_ARG,       0 }, /// build a dll
    { "soname",    OPT_soname,           ARG_SEPARATE, 0 }, /// the name it records
    { "rdynamic",  OPT_rdynamic,         NO_ARG,       0 }, /// keep dynamic symbols
    { "s",         OPT_strip,            NO_ARG,       0 }, /// drop the symbol table
    { "r",         OPT_relocatable,      NO_ARG,       0 }, /// a relocatable object
    { "c",         OPT_compile,          NO_ARG,       0 }, /// same, for a driver
    { "nostdlib",  OPT_nostdlib,         NO_ARG,       0 }, /// no startup objects
    { "Wl,",       OPT_linker,           ARG_JOINED,   0 }, /// the inner grammar
    { "v",         OPT_verbose,          ARG_JOINED,   0 }, /// repeatable: -v, -vv
    { "-whole-archive",    OPT_whole_archive,    NO_ARG,       0 }, /// take every member
    { "-no-whole-archive", OPT_no_whole_archive, NO_ARG,       0 }, /// and stop again
#ifdef TLI_BACKTRACE
    { "bt",        OPT_backtrace,        ARG_JOINED,   0 }, /// frames to print
#endif
#ifdef TLI_BCHECK
    { "b",         OPT_bcheck,           NO_ARG,       0 }, /// bounds checking
#endif
    { "dynamiclib",            OPT_dynamiclib,      NO_ARG,       ONLY_MACHO }, /// -shared
    { "flat_namespace",        OPT_ignored,         NO_ARG,       ONLY_MACHO }, /// the only one here
    { "two_levelnamespace",    OPT_ignored,         NO_ARG,       ONLY_MACHO }, /// not implemented
    { "install_name",          OPT_install_name,    ARG_SEPARATE, ONLY_MACHO }, /// the soname
    { "compatibility_version", OPT_compat_version,  ARG_SEPARATE, ONLY_MACHO }, /// a.b.c
    { "current_version",       OPT_current_version, ARG_SEPARATE, ONLY_MACHO }, /// a.b.c
    { "undefined",             OPT_ignored,         ARG_SEPARATE, ONLY_MACHO }, /// eats its keyword
    /// accepted and ignored: harmless when a compiler driver passes them on
    { "g",         OPT_ignored,          ARG_JOINED,   0 },
    { "O",         OPT_ignored,          ARG_JOINED,   0 },
    { "pthread",   OPT_ignored,          NO_ARG,       0 },
    { "pie",       OPT_ignored,          NO_ARG,       0 },
    { "no-pie",    OPT_ignored,          NO_ARG,       0 },

    { "w",         OPT_ignored,          NO_ARG,       0 },
    { NULL,        OPT_ignored,          NO_ARG,       0 }, /// end of the table
};

/// Parse an argv[]; argv[0] is skipped.  Nothing is linked here: the settings
/// land on the state and the files are recorded for tli_add_parsed_files().
PUB_FUNC i32 tli_parse_args(tli_State *s1, i32 argc, char **argv)
{
    i32 optind = 1;

    // Words left over from an earlier call are not this call's business; start
    // reading '-Wl,' words from what is about to be added.
    s1->link_optind = s1->link_argc;

    while (optind < argc) {
        const char *word = argv[optind++];
        const tli_DriverOption *o;
        const char *val = "";

        // Anything that is not an option is an input, and so is a lone '-',
        // which names standard input.
        if (word[0] != '-' || word[1] == '\0') {
            args_parser_add_file(s1, word, s1->filetype);
            continue;
        }

        // find the row this word matches, and where its value comes from
        for (o = driver_options; ; o++) {
            const char *rest = word + 1;

            if (o->name == NULL)
                return tli_error_noabort("invalid option -- '%s'", word);

            if (!opt_for_fmt(s1, o->flags) || !strstart(o->name, &rest))
                continue;

            if (o->arg == NO_ARG && *rest)
                continue; // a longer option that starts the same way

            val = rest;
            if (o->arg == ARG_SEPARATE && *rest == '\0') {
                if (optind >= argc)
                    return tli_error_noabort("argument to '%s' is missing", word);

                val = argv[optind++];
            }
            break;
        }

        switch (o->kind) {
        case OPT_libpath:
            tli_add_library_path(s1, val);
            break;
        case OPT_library:
            args_parser_add_file(s1, val,
                AFF_TYPE_LIB | (s1->filetype & ~AFF_TYPE_MASK));
            break;
        case OPT_supportdir:
            tli_set_lib_path(s1, val);
            break;
        case OPT_output:
            if (s1->outfile)
                tli_warning("multiple -o option");

            tli_set_str(&s1->outfile, val);
            break;
        case OPT_static:
            s1->static_link = 1;
            break;
        case OPT_shared:
            s1->output_type = TLI_OUTPUT_DLL;
            break;
        case OPT_soname:
            tli_set_str(&s1->soname, val);
            break;
        case OPT_rdynamic:
            s1->rdynamic = 1;
            break;
        case OPT_strip:
            s1->strip = 1;
            break;
        case OPT_relocatable:
            // -r keeps the relocations; -c only says not to link, which for a
            // linker amounts to the same output
            s1->option_r = 1;
            s1->output_type = TLI_OUTPUT_OBJ;
            break;
        case OPT_compile:
            s1->output_type = TLI_OUTPUT_OBJ;
            break;
        case OPT_nostdlib:
            s1->nostdlib = 1;
            break;
        case OPT_linker:
            if (tli_set_linker(s1, val))
                return -1;
            break;
        case OPT_verbose:
            // '-v', '-vv', '-vvv': each further 'v' raises the level
            do
                ++s1->verbose;
            while (*val++ == 'v');
            break;
        case OPT_whole_archive:
            s1->filetype |= AFF_WHOLE_ARCHIVE;
            break;
        case OPT_no_whole_archive:
            s1->filetype &= ~AFF_WHOLE_ARCHIVE;
            break;
#ifdef TLI_BACKTRACE
        case OPT_backtrace:
            s1->rt_num_callers = atoi(val); // zero means the built-in default
            s1->do_backtrace = 1;
            if (!s1->do_debug)
                s1->do_debug = 1;
            break;
#endif
#ifdef TLI_BCHECK
        case OPT_bcheck:
            s1->do_bounds_check = 1;
            s1->do_backtrace = 1;
            if (!s1->do_debug)
                s1->do_debug = 1;
            break;
#endif
        case OPT_dynamiclib:
            s1->output_type = TLI_OUTPUT_DLL;
            break;
        case OPT_install_name:
            tli_set_str(&s1->install_name, val);
            break;
        case OPT_compat_version:
            s1->compatibility_version = parse_version(s1, val);
            break;
        case OPT_current_version:
            s1->current_version = parse_version(s1, val);
            break;
        default:
            // OPT_ignored
            break;
        }
    }
    return 0;
}

/// The same options, as one string.  This is what an embedder that has a
/// command line rather than an argument vector uses, and what carries the
/// defaults compiled in as TLI_SWITCHES.
TLI_PUBLIC i32 tli_set_options(tli_State *s1, const char *str)
{
    char **argv = NULL;
    i32 argc = 0, ret;

    dynarray_add(&argv, &argc, NULL); // stands in for the program name
    split_words(&argv, &argc, str);
    ret = tli_parse_args(s1, argc, argv);
    dynarray_reset(&argv, &argc);
    return ret;
}

/// Link every input recorded by tli_parse_args(), in the order it was named.
/// Order is the whole point: an archive only contributes what is unresolved by
/// the time it is reached.
PUB_FUNC i32 tli_add_parsed_files(tli_State *s1)
{
    i32 i, ret = 0;

    for (i = 0; i < s1->nb_files; i++) {
        struct tli_FileSpec *f = s1->files[i];

        s1->filetype = f->type;
        if (f->type & AFF_TYPE_LIB) {
            if (tli_add_library(s1, f->name) < 0)
                ret = -1;
        } else {
            if (tli_add_file(s1, f->name) < 0)
                ret = -1;
        }
    }
    s1->filetype = 0;
    return ret;
}

/// ---------------------------------------------------------------------------



/// ---------------------------------------------------------------------
/// Thunks. The vtable is typeless about the state it carries, so each of
/// these casts the void * back to a tli_State and calls the core. They are
/// shared by every vtable of the unit and listed in vtable order; the comment
/// on each names the core call, which is the part the thunk's own name does
/// not give away. The create thunks, the one per-target part, come last.

/// tli_delete(), including any image relocated into memory.
static void tlb_destroy(void *st)
{
    tli_delete((tli_State *)st);
}

/// tli_set_error_func(). The two callback types are laid out alike, so the
/// public one is simply cast rather than wrapped.
static void tlb_set_error_func(void *st, void *opaque, tl_ErrorFunc *func)
{
    tli_set_error_func((tli_State *)st, opaque, (TLIErrorFunc *)func);
}

/// A counter on the state, not a call.
static i32 tlb_error_count(void *st)
{
    return ((tli_State *)st)->nb_errors;
}

/// tli_set_output_type(), which is also what creates the sections.
static i32 tlb_set_output_type(void *st, i32 output_type)
{
    return tli_set_output_type((tli_State *)st, output_type);
}

/// A field on the state, so that an option that implied a type can be read
/// back before the type is committed to.
static i32 tlb_get_output_type(void *st)
{
    return ((tli_State *)st)->output_type;
}

/// The -o name a parse recorded, straight off the state.
static const char *tlb_get_output_file(void *st)
{
    return ((tli_State *)st)->outfile;
}

/// tli_set_lib_path().
static void tlb_set_lib_path(void *st, const char *path)
{
    tli_set_lib_path((tli_State *)st, path);
}

/// tli_add_library_path().
static i32 tlb_add_library_path(void *st, const char *path)
{
    return tli_add_library_path((tli_State *)st, path);
}

/// tli_set_options(), the ld-style string parser.
static i32 tlb_set_options(void *st, const char *options)
{
    return tli_set_options((tli_State *)st, options);
}

/// tl_settings_flags(), where the typed settings live.
static i32 tlb_set_flags(void *st, u32 flags, i32 on)
{
    return tl_settings_flags((tli_State *)st, flags, on);
}

/// tl_settings_get_flags().
static u32 tlb_get_flags(void *st)
{
    return tl_settings_get_flags((tli_State *)st);
}

/// tl_settings_set_str(); 'opt' is a tl_StrOpt the vtable carries as an int.
static i32 tlb_set_str(void *st, i32 opt, const char *value)
{
    return tl_settings_set_str((tli_State *)st, opt, value);
}

/// tl_settings_get_str().
static const char *tlb_get_str(void *st, i32 opt)
{
    return tl_settings_get_str((tli_State *)st, opt);
}

/// tl_settings_set_num(); 'opt' is a tl_NumOpt carried as an int.
static i32 tlb_set_num(void *st, i32 opt, u64 value)
{
    return tl_settings_set_num((tli_State *)st, opt, value);
}

/// tl_settings_get_num().
static u64 tlb_get_num(void *st, i32 opt)
{
    return tl_settings_get_num((tli_State *)st, opt);
}

/// tli_parse_args(), which only queues the input files it names.
static i32 tlb_parse_args(void *st, i32 argc, char **argv)
{
    return tli_parse_args((tli_State *)st, argc, argv);
}

/// tli_add_parsed_files(), the other half of tlb_parse_args().
static i32 tlb_link_parsed(void *st)
{
    return tli_add_parsed_files((tli_State *)st);
}

/// tli_add_file(), which works out the kind from the contents.
static i32 tlb_add_file(void *st, const char *filename)
{
    return tli_add_file((tli_State *)st, filename);
}

/// tli_add_library().
static i32 tlb_add_library(void *st, const char *name)
{
    return tli_add_library((tli_State *)st, name);
}

/// tli_add_symbol(), for a symbol pointing at host memory.
static i32 tlb_add_symbol(void *st, const char *name, const void *val)
{
    return tli_add_symbol((tli_State *)st, name, val);
}

/// ---------------------------------------------------------------------
/// Building an object in memory. These reach the tl_build_* half of the core
/// rather than the file readers.

/// tl_build_add_section().
static i32 tlb_add_section(void *st, const char *name, i32 kind, i32 align)
{
    return tl_build_add_section((tli_State *)st, name, kind, align);
}

/// tl_build_append().
static i64 tlb_section_append(void *st, i32 sec, const void *data, usize len)
{
    return tl_build_append((tli_State *)st, sec, data, len);
}

/// tl_build_zero().
static i64 tlb_section_zero(void *st, i32 sec, usize len)
{
    return tl_build_zero((tli_State *)st, sec, len);
}

/// tl_build_size().
static i64 tlb_section_size(void *st, i32 sec)
{
    return tl_build_size((tli_State *)st, sec);
}

/// tl_build_define().
static i32 tlb_define_symbol(void *st, const char *name, i32 sec,
                             u64 off, i32 flags)
{
    return tl_build_define((tli_State *)st, name, sec, off, flags);
}

/// tl_build_declare().
static i32 tlb_declare_symbol(void *st, const char *name)
{
    return tl_build_declare((tli_State *)st, name);
}

/// tl_build_reloc(), which turns the format-independent kind into this
/// target's own relocation type.
static i32 tlb_add_reloc(void *st, i32 sec, u64 off,
                         const char *sym, i32 kind, i64 addend)
{
    return tl_build_reloc((tli_State *)st, sec, off, sym, kind, addend);
}

/// tl_build_section_reloc(), for a relocation whose target is a section.
static i32 tlb_add_section_reloc(void *st, i32 sec, u64 off,
                                 i32 target, i32 kind, i64 addend)
{
    return tl_build_section_reloc((tli_State *)st, sec, off, target, kind,
                                  addend);
}

/// tli_output_file(): link, then write the result out.
static i32 tlb_output_file(void *st, const char *filename)
{
    return tli_output_file((tli_State *)st, filename);
}

/// ---------------------------------------------------------------------
/// Running the result. Only a core of the host's own width can do it, and of
/// its targets only the one this host runs: every other backend leaves the
/// four vtable slots NULL for the dispatcher to notice.
#ifdef TLI_HOST_RUN

/// tli_relocate(), which links into memory instead of into a file.
static i32 tlb_relocate(void *st)
{
    return tli_relocate((tli_State *)st);
}

/// tli_get_symbol().
static void *tlb_get_symbol(void *st, const char *name)
{
    return tli_get_symbol((tli_State *)st, name);
}

/// tli_list_symbols().
static void tlb_list_symbols(void *st, void *ctx,
                             void (*cb)(void *ctx, const char *name, const void *val))
{
    tli_list_symbols((tli_State *)st, ctx, cb);
}

/// tli_run(), which calls main() in the relocated image.
static i32 tlb_run(void *st, i32 argc, char **argv)
{
    return tli_run((tli_State *)st, argc, argv);
}
#endif

/// The slots a vtable fills with the thunks above, which are the same for
/// every target of the unit.
#define TLB_THUNKS         \
    tlb_destroy,           \
    tlb_set_error_func,    \
    tlb_error_count,       \
                           \
    tlb_set_output_type,   \
    tlb_get_output_type,   \
    tlb_get_output_file,   \
    tlb_set_lib_path,      \
    tlb_add_library_path,  \
    tlb_set_options,       \
    tlb_set_flags,         \
    tlb_get_flags,         \
    tlb_set_str,           \
    tlb_get_str,           \
    tlb_set_num,           \
    tlb_get_num,           \
    tlb_parse_args,        \
    tlb_link_parsed,       \
                           \
    tlb_add_file,          \
    tlb_add_library,       \
    tlb_add_symbol,        \
                           \
    tlb_add_section,       \
    tlb_section_append,    \
    tlb_section_zero,      \
    tlb_section_size,      \
    tlb_define_symbol,     \
    tlb_declare_symbol,    \
    tlb_add_reloc,         \
    tlb_add_section_reloc, \
                           \
    tlb_output_file

/// The four in-memory slots, filled only for the target this host runs.
/// 'native' is a constant, so each choice is made by the compiler.
#ifdef TLI_HOST_RUN
# define TLB_RUN_THUNKS(native)         \
    (native) ? tlb_relocate : NULL,     \
    (native) ? tlb_get_symbol : NULL,   \
    (native) ? tlb_list_symbols : NULL, \
    (native) ? tlb_run : NULL
#else
# define TLB_RUN_THUNKS(native) NULL, NULL, NULL, NULL
#endif

/// Every target of this unit's width: the vtable's symbol suffix and tl_Target
/// name, the command-line name, the architecture table, its ELF machine
/// number (a constant, where the table's field is not) and the object format.
#if PTR_SIZE == 8
# define TLB_TARGETS(X)                                                               \
    X(x86_64_elf,   X86_64_ELF,   "x86_64-elf",   x86_64,  EM_X86_64,  TLI_FMT_ELF)   \
    X(arm64_elf,    ARM64_ELF,    "arm64-elf",    arm64,   EM_AARCH64, TLI_FMT_ELF)   \
    X(riscv64_elf,  RISCV64_ELF,  "riscv64-elf",  riscv64, EM_RISCV,   TLI_FMT_ELF)   \
    X(x86_64_pe,    X86_64_PE,    "x86_64-pe",    x86_64,  EM_X86_64,  TLI_FMT_PE)    \
    X(arm64_pe,     ARM64_PE,     "arm64-pe",     arm64,   EM_AARCH64, TLI_FMT_PE)    \
    X(x86_64_macho, X86_64_MACHO, "x86_64-macho", x86_64,  EM_X86_64,  TLI_FMT_MACHO) \
    X(arm64_macho,  ARM64_MACHO,  "arm64-macho",  arm64,   EM_AARCH64, TLI_FMT_MACHO)
#else
# define TLB_TARGETS(X)                                          \
    X(i386_elf, I386_ELF, "i386-elf", i386, EM_386, TLI_FMT_ELF) \
    X(arm_elf,  ARM_ELF,  "arm-elf",  arm,  EM_ARM, TLI_FMT_ELF) \
    X(i386_pe,  I386_PE,  "i386-pe",  i386, EM_386, TLI_FMT_PE)
#endif

/// Defines one target's create thunk, which is tli_new() for its architecture
/// and format, and the vtable the dispatcher in tinylinker.c names
/// tl_backend_<suffix>.
#define TLB_BACKEND(sym, upper, name, arch, em, fmt) \
    static void *tlb_create_##sym(void)              \
    {                                                \
        return tli_new(&tli_arch_##arch, fmt);       \
    }                                                \
                                                     \
    const tl_Backend tl_backend_##sym = {            \
        name,                                        \
        TL_TARGET_##upper,                           \
        TLI_IS_NATIVE(em, fmt),                      \
        tlb_create_##sym,                            \
        TLB_THUNKS,                                  \
        TLB_RUN_THUNKS(TLI_IS_NATIVE(em, fmt))       \
    };

TLB_TARGETS(TLB_BACKEND)

/// ---- the core's macros end with it ----

#undef AFF_BINTYPE_AR
#undef AFF_BINTYPE_COFF_IMPORT
#undef AFF_BINTYPE_COFF_REL
#undef AFF_BINTYPE_DYN
#undef AFF_BINTYPE_MACHO_REL
#undef AFF_BINTYPE_REL
#undef AFF_PRINT_ERROR
#undef AFF_REFERENCED_DLL
#undef AFF_TYPE_ASM
#undef AFF_TYPE_ASMPP
#undef AFF_TYPE_BIN
#undef AFF_TYPE_C
#undef AFF_TYPE_LIB
#undef AFF_TYPE_MASK
#undef AFF_TYPE_NONE
#undef AFF_WHOLE_ARCHIVE
#undef ALIGNED
#undef ALSO_TRIPLET
#undef ARFMAG
#undef ARM64_ADD_IMM
#undef ARM64_ADRP
#undef ARM64_ADRP_IMM
#undef ARM64_B
#undef ARM64_BL
#undef ARM64_BR
#undef ARM64_BRANCH_MASK
#undef ARM64_IMM12
#undef ARM64_IMM14
#undef ARM64_IMM16
#undef ARM64_IMM19
#undef ARM64_IMM26
#undef ARM64_IMM7
#undef ARM64_IS_BRANCH
#undef ARM64_KEEP_ADRP
#undef ARM64_KEEP_IMM12
#undef ARM64_KEEP_IMM14
#undef ARM64_KEEP_IMM16
#undef ARM64_KEEP_IMM19
#undef ARM64_KEEP_IMM26
#undef ARM64_LDR_X
#undef ARM64_LO12
#undef ARM64_MOVZ_X
#undef ARM64_NOP
#undef ARM64_PLT0_SIZE
#undef ARM64_PLT_ENTRY_SIZE
#undef ARM64_RD
#undef ARM64_RN
#undef ARM64_RT
#undef ARM64_RT2
#undef ARM64_SF
#undef ARM64_STP_X_PRE
#undef ARMAG
#undef ARM_ADD_IMM
#undef ARM_ADD_LR_PC_LR
#undef ARM_HARD_FLOAT
#undef ARM_LDR_LR_PC4
#undef ARM_LDR_PC_IP
#undef ARM_LDR_PC_LR8
#undef ARM_PLT0_SIZE
#undef ARM_PLT_ENTRY_SIZE
#undef ARM_PUSH_LR
#undef ARM_SOFTFP_FLOAT
#undef AR_NAME_MAX
#undef ATTR_TAG_FILE
#undef BAND_DROPPED
#undef BAND_LOAD_RO
#undef BAND_LOAD_RW
#undef BAND_UNMAPPED
#undef BIND_SPECIAL_DYLIB_FLAT_LOOKUP
#undef CD32
#undef CD64
#undef CD8
#undef CHAIN_BIND
#undef CHAIN_HIGH8_MASK
#undef CHAIN_IS_BIND
#undef CHAIN_NEXT_MAX
#undef CHAIN_NEXT_SHIFT
#undef CHAIN_REBASE
#undef CHAIN_TARGET_MASK
#undef CH_EOF
#undef COFF_IMPORT_BY_ORDINAL
#undef COFF_IMPORT_CODE
#undef COFF_IMPORT_CONST
#undef COFF_IMPORT_DATA
#undef COFF_RELOC_SIZE
#undef COFF_SYM_SIZE
#undef CONFIG_NEW_MACHO
#undef CONFIG_TLIDIR
#undef CPU_ARCH_ABI64
#undef CPU_SUBTYPE_ARM64_ALL
#undef CPU_SUBTYPE_LIB64
#undef CPU_SUBTYPE_X86_ALL
#undef CPU_TYPE_ARM
#undef CPU_TYPE_ARM64
#undef CPU_TYPE_X86
#undef CPU_TYPE_X86_64
#undef CSMAGIC_CODEDIRECTORY
#undef CSMAGIC_EMBEDDED_SIGNATURE
#undef CSSLOT_CODEDIRECTORY
#undef CS_ADHOC
#undef CS_EXECSEG_MAIN_BINARY
#undef CS_HASHTYPE_SHA256
#undef CS_HASH_SIZE
#undef CS_LINKER_SIGNED
#undef CS_PAGE_SHIFT
#undef CS_PAGE_SIZE
#undef CS_SUPPORTS_EXEC_SEG
#undef DF_1_NODELETE
#undef DF_1_NOW
#undef DF_1_PIE
#undef DF_BIND_NOW
#undef DT_DEBUG
#undef DT_FINI
#undef DT_FINI_ARRAY
#undef DT_FINI_ARRAYSZ
#undef DT_FLAGS
#undef DT_FLAGS_1
#undef DT_GNU_HASH
#undef DT_HASH
#undef DT_INIT
#undef DT_INIT_ARRAY
#undef DT_INIT_ARRAYSZ
#undef DT_JMPREL
#undef DT_NEEDED
#undef DT_NULL
#undef DT_PLTGOT
#undef DT_PLTREL
#undef DT_PLTRELSZ
#undef DT_PREINIT_ARRAY
#undef DT_PREINIT_ARRAYSZ
#undef DT_REL
#undef DT_RELA
#undef DT_RELACOUNT
#undef DT_RELAENT
#undef DT_RELASZ
#undef DT_RELCOUNT
#undef DT_RELENT
#undef DT_RELSZ
#undef DT_RPATH
#undef DT_RUNPATH
#undef DT_SONAME
#undef DT_STRSZ
#undef DT_STRTAB
#undef DT_SYMBOLIC
#undef DT_SYMENT
#undef DT_SYMTAB
#undef DT_TEXTREL
#undef DT_VERNEED
#undef DT_VERNEEDNUM
#undef DT_VERSYM
#undef DW_EH_PE_absptr
#undef DW_EH_PE_datarel
#undef DW_EH_PE_omit
#undef DW_EH_PE_pcrel
#undef DW_EH_PE_sdata2
#undef DW_EH_PE_sdata4
#undef DW_EH_PE_sdata8
#undef DW_EH_PE_sleb128
#undef DW_EH_PE_udata2
#undef DW_EH_PE_udata4
#undef DW_EH_PE_udata8
#undef DW_EH_PE_uleb128
#undef DYLD_CHAINED_IMPORT
#undef DYLD_CHAINED_PTR_64
#undef DYLD_CHAINED_PTR_START_NONE
#undef EF_ARM_EABI_VER5
#undef EF_ARM_SOFT_FLOAT
#undef EF_ARM_VFP_FLOAT
#undef EF_RISCV_FLOAT_ABI_DOUBLE
#undef EH_HDR_COUNT_ENC
#undef EH_HDR_ENTRY
#undef EH_HDR_PREAMBLE
#undef EH_HDR_PTR_ENC
#undef EH_HDR_TABLE_ENC
#undef EI_NIDENT
#undef EI_OSABI
#undef ELF32_R_INFO
#undef ELF32_R_SYM
#undef ELF32_R_TYPE
#undef ELF32_ST_BIND
#undef ELF32_ST_INFO
#undef ELF32_ST_TYPE
#undef ELF32_ST_VISIBILITY
#undef ELF64_R_INFO
#undef ELF64_R_SYM
#undef ELF64_R_TYPE
#undef ELF64_ST_BIND
#undef ELF64_ST_INFO
#undef ELF64_ST_TYPE
#undef ELF64_ST_VISIBILITY
#undef ELFCLASS32
#undef ELFCLASS64
#undef ELFCLASSW
#undef ELFCLASS_BITS
#undef ELFDATA2LSB
#undef ELFMAG
#undef ELFMAG0
#undef ELFMAG1
#undef ELFMAG2
#undef ELFMAG3
#undef ELFOSABI_ARM
#undef ELFOSABI_FREEBSD
#undef ELFOSABI_NETBSD
#undef ELFOSABI_OPENBSD
#undef ELFW
#undef ELF_NOTE_OS_GNU
#undef EM_386
#undef EM_AARCH64
#undef EM_ARM
#undef EM_RISCV
#undef EM_X86_64
#undef ET_DYN
#undef ET_EXEC
#undef ET_REL
#undef EV_CURRENT
#undef EXPORT_SYMBOL_FLAGS_KIND_REGULAR
#undef EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION
#undef ElfSym
#undef ElfW
#undef ElfW_Rel
#undef FAT_CIGAM
#undef FAT_CIGAM_64
#undef FAT_MAGIC
#undef FAT_MAGIC_64
#undef FILE_NOT_FOUND
#undef FILE_NOT_RECOGNIZED
#undef I386_GRP5
#undef I386_JMP_REL
#undef I386_MODRM_ABS
#undef I386_MODRM_EBX
#undef I386_MODRM_PUSH
#undef I386_PLT_ENTRY_SIZE
#undef I386_PUSH_IMM
#undef IMAGE_FILE_MACHINE
#undef IMAGE_FILE_MACHINE_ARM64EC
#undef IMAGE_REL_AMD64_ABSOLUTE
#undef IMAGE_REL_AMD64_ADDR32
#undef IMAGE_REL_AMD64_ADDR32NB
#undef IMAGE_REL_AMD64_ADDR64
#undef IMAGE_REL_AMD64_REL32
#undef IMAGE_REL_AMD64_REL32_5
#undef IMAGE_REL_AMD64_SECREL
#undef IMAGE_REL_AMD64_SECTION
#undef IMAGE_REL_ARM64_ABSOLUTE
#undef IMAGE_REL_ARM64_ADDR32
#undef IMAGE_REL_ARM64_ADDR32NB
#undef IMAGE_REL_ARM64_ADDR64
#undef IMAGE_REL_ARM64_BRANCH14
#undef IMAGE_REL_ARM64_BRANCH19
#undef IMAGE_REL_ARM64_BRANCH26
#undef IMAGE_REL_ARM64_PAGEBASE_REL21
#undef IMAGE_REL_ARM64_PAGEOFFSET_12A
#undef IMAGE_REL_ARM64_PAGEOFFSET_12L
#undef IMAGE_REL_ARM64_REL32
#undef IMAGE_REL_ARM64_SECREL_HIGH12A
#undef IMAGE_REL_ARM64_SECREL_LOW12A
#undef IMAGE_REL_I386_ABSOLUTE
#undef IMAGE_REL_I386_DIR32
#undef IMAGE_REL_I386_REL32
#undef IMAGE_SCN_ALIGN_MASK
#undef IMAGE_SCN_CNT_CODE
#undef IMAGE_SCN_CNT_INITIALIZED_DATA
#undef IMAGE_SCN_CNT_UNINITIALIZED_DATA
#undef IMAGE_SCN_LNK_COMDAT
#undef IMAGE_SCN_LNK_INFO
#undef IMAGE_SCN_LNK_REMOVE
#undef IMAGE_SCN_MEM_DISCARDABLE
#undef IMAGE_SCN_MEM_EXECUTE
#undef IMAGE_SCN_MEM_READ
#undef IMAGE_SCN_MEM_WRITE
#undef IMAGE_SYM_ABSOLUTE
#undef IMAGE_SYM_CLASS_EXTERNAL
#undef IMAGE_SYM_CLASS_FILE
#undef IMAGE_SYM_CLASS_LABEL
#undef IMAGE_SYM_CLASS_STATIC
#undef IMAGE_SYM_CLASS_WEAK_EXTERNAL
#undef IMAGE_SYM_DEBUG
#undef IMAGE_SYM_DTYPE_FUNCTION
#undef IMAGE_SYM_UNDEFINED
#undef INDIRECT_SYMBOL_LOCAL
#undef IS_ABSPATH
#undef IS_DIRSEP
#undef LC_BUILD_VERSION
#undef LC_CODE_SIGNATURE
#undef LC_DYLD_CHAINED_FIXUPS
#undef LC_DYLD_EXPORTS_TRIE
#undef LC_DYSYMTAB
#undef LC_ID_DYLIB
#undef LC_LOAD_DYLIB
#undef LC_LOAD_DYLINKER
#undef LC_MAIN
#undef LC_REEXPORT_DYLIB
#undef LC_REQ_DYLD
#undef LC_RPATH
#undef LC_SEGMENT_64
#undef LC_SOURCE_VERSION
#undef LC_SYMTAB
#undef LD_NEG
#undef LD_TOK_EOF
#undef LD_TOK_NAME
#undef MACHO_HDR_PAGE
#undef MACHO_IMAGE_BASE
#undef MACHO_MAX_SEG
#undef MACHO_NO_STUB
#undef MACHO_ROR
#undef MACHO_SYM_EXTDEF
#undef MACHO_SYM_LOCAL
#undef MACHO_SYM_UNDEF
#undef MAP_ANONYMOUS
#undef MAX_SH_ALIGN
#undef MEM_BLOCK
#undef MEM_BODY
#undef MEM_HEAD_MAGIC
#undef MEM_ORIGIN_LEN
#undef MEM_TAIL
#undef MEM_TAIL_MAGIC
#undef MH_DYLDLINK
#undef MH_DYLIB
#undef MH_EXECUTE
#undef MH_HAS_TLV_DESCRIPTORS
#undef MH_MAGIC_64
#undef MH_OBJECT
#undef MH_PIE
#undef NORETURN
#undef NO_SECT
#undef N_ABS
#undef N_BINCL
#undef N_EINCL
#undef N_EXT
#undef N_FUN
#undef N_PEXT
#undef N_SECT
#undef N_SLINE
#undef N_SO
#undef N_SOL
#undef N_STAB
#undef N_TYPE
#undef N_UNDF
#undef N_WEAK_DEF
#undef N_WEAK_REF
#undef ONE_SOURCE
#undef ONLY_MACHO
#undef ONLY_PE
#undef PATHCMP
#undef PATHSEP
#undef PE_CHARACTERISTICS
#undef PE_COFF_C_EXT
#undef PE_DIR_BASERELOC
#undef PE_DIR_EXCEPTION
#undef PE_DIR_EXPORT
#undef PE_DIR_IAT
#undef PE_DIR_IMPORT
#undef PE_DIR_RESOURCE
#undef PE_DIR_TLS
#undef PE_DLL_DYNAMIC_BASE
#undef PE_EXPORT_DIR_SIZE
#undef PE_FILE_32BIT_MACHINE
#undef PE_FILE_DEBUG_STRIPPED
#undef PE_FILE_DLL
#undef PE_FILE_EXECUTABLE_IMAGE
#undef PE_FILE_LARGE_ADDRESS_AWARE
#undef PE_FILE_LINE_NUMS_STRIPPED
#undef PE_FILE_LOCAL_SYMS_STRIPPED
#undef PE_FILE_RELOCS_STRIPPED
#undef PE_HDR_OFFSET
#undef PE_IMAGE_BASE_DLL
#undef PE_IMAGE_BASE_EXE
#undef PE_IMPORT_DESC_SIZE
#undef PE_NAME_MAX
#undef PE_NAME_TRIES
#undef PE_NUM_DIRS
#undef PE_OPT_MAGIC
#undef PE_ORDINAL_FLAG
#undef PE_OS_VERSION
#undef PE_RELOC_BLOCK_SIZE
#undef PE_RELOC_KIND
#undef PE_RSRC_RELTYPE
#undef PE_SCN_CNT_CODE
#undef PE_SCN_CNT_INITIALIZED
#undef PE_SCN_CNT_UNINITIALIZED
#undef PE_SCN_MEM_DISCARDABLE
#undef PE_SCN_MEM_EXECUTE
#undef PE_SCN_MEM_READ
#undef PE_SCN_MEM_WRITE
#undef PE_STDSYM
#undef PE_TLS_DIR_SIZE
#undef PF_R
#undef PF_W
#undef PF_X
#undef PLATFORM_IOS
#undef PLATFORM_IOSSIMULATOR
#undef PLATFORM_MACCATALYST
#undef PLATFORM_MACOS
#undef PLATFORM_TVOS
#undef PLATFORM_TVOSSIMULATOR
#undef PLATFORM_WATCHOS
#undef PLATFORM_WATCHOSSIMULATOR
#undef POST_SEM
#undef PRINTF_LIKE
#undef PTR_SIZE
#undef PT_DYNAMIC
#undef PT_GNU_EH_FRAME
#undef PT_GNU_RELRO
#undef PT_INTERP
#undef PT_LOAD
#undef PT_NOTE
#undef PT_PHDR
#undef PT_TLS
#undef PUB_FUNC
#undef RANK_BSS
#undef RANK_DATA
#undef RANK_DYNAMIC
#undef RANK_FINI
#undef RANK_GOT
#undef RANK_HASH
#undef RANK_INIT
#undef RANK_INTERP
#undef RANK_NOTE
#undef RANK_PLT_RELOC
#undef RANK_PREINIT
#undef RANK_RELDATA
#undef RANK_RELOC
#undef RANK_RELRO_FIRST
#undef RANK_RELRO_LAST
#undef RANK_SHSTRTAB
#undef RANK_STRTAB
#undef RANK_SYMTAB
#undef RANK_TBSS
#undef RANK_TDATA
#undef RANK_TEXT
#undef RANK_VERSION
#undef RCE
#undef RC_CODE
#undef RC_DATA
#undef RC_INSN
#undef RC_KNOWN
#undef REL_SECTION_FMT
#undef RISCV64_PLT0_SIZE
#undef RISCV64_PLT_ENTRY_SIZE
#undef RISCV_DTP_OFFSET
#undef RISCV_IMM_I
#undef RISCV_IMM_S
#undef RISCV_IMM_U
#undef RISCV_KEEP_B
#undef RISCV_KEEP_CB
#undef RISCV_KEEP_CJ
#undef RISCV_KEEP_I
#undef RISCV_KEEP_J
#undef RISCV_KEEP_S
#undef RISCV_KEEP_U
#undef RTLD_DEFAULT
#undef RTLD_GLOBAL
#undef RTLD_LAZY
#undef RTLD_NOW
#undef RT_DEFAULT_CALLERS
#undef RT_EXIT_ZERO
#undef RT_SLOT_FP
#undef RT_SLOT_RA
#undef RT_STACK_FLOOR
#undef RT_SYM
#undef R_386_16
#undef R_386_32
#undef R_386_COPY
#undef R_386_GLOB_DAT
#undef R_386_GOT32
#undef R_386_GOT32X
#undef R_386_GOTOFF
#undef R_386_GOTPC
#undef R_386_JMP_SLOT
#undef R_386_NONE
#undef R_386_NUM
#undef R_386_PC16
#undef R_386_PC32
#undef R_386_PLT32
#undef R_386_RELATIVE
#undef R_386_TLS_GD
#undef R_386_TLS_LDM
#undef R_386_TLS_LDO_32
#undef R_386_TLS_LE
#undef R_AARCH64_ABS32
#undef R_AARCH64_ABS64
#undef R_AARCH64_ADD_ABS_LO12_NC
#undef R_AARCH64_ADR_GOT_PAGE
#undef R_AARCH64_ADR_PREL_PG_HI21
#undef R_AARCH64_CALL26
#undef R_AARCH64_CONDBR19
#undef R_AARCH64_COPY
#undef R_AARCH64_GLOB_DAT
#undef R_AARCH64_JUMP26
#undef R_AARCH64_JUMP_SLOT
#undef R_AARCH64_LD64_GOT_LO12_NC
#undef R_AARCH64_LDST128_ABS_LO12_NC
#undef R_AARCH64_LDST16_ABS_LO12_NC
#undef R_AARCH64_LDST32_ABS_LO12_NC
#undef R_AARCH64_LDST64_ABS_LO12_NC
#undef R_AARCH64_LDST8_ABS_LO12_NC
#undef R_AARCH64_MOVW_UABS_G0_NC
#undef R_AARCH64_MOVW_UABS_G1_NC
#undef R_AARCH64_MOVW_UABS_G2_NC
#undef R_AARCH64_MOVW_UABS_G3
#undef R_AARCH64_NUM
#undef R_AARCH64_PREL32
#undef R_AARCH64_RELATIVE
#undef R_AARCH64_TLSDESC_ADD_LO12
#undef R_AARCH64_TLSDESC_ADR_PAGE21
#undef R_AARCH64_TLSDESC_CALL
#undef R_AARCH64_TLSDESC_LD64_LO12
#undef R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21
#undef R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC
#undef R_AARCH64_TLSLE_ADD_TPREL_HI12
#undef R_AARCH64_TLSLE_ADD_TPREL_LO12
#undef R_AARCH64_TLSLE_ADD_TPREL_LO12_NC
#undef R_AARCH64_TLS_TPREL64
#undef R_AARCH64_TSTBR14
#undef R_ARM_ABS32
#undef R_ARM_CALL
#undef R_ARM_COPY
#undef R_ARM_GLOB_DAT
#undef R_ARM_GOT32
#undef R_ARM_GOTOFF
#undef R_ARM_GOTPC
#undef R_ARM_GOT_PREL
#undef R_ARM_JUMP24
#undef R_ARM_JUMP_SLOT
#undef R_ARM_MOVT_ABS
#undef R_ARM_MOVT_PREL
#undef R_ARM_MOVW_ABS_NC
#undef R_ARM_MOVW_PREL_NC
#undef R_ARM_NONE
#undef R_ARM_NUM
#undef R_ARM_PC24
#undef R_ARM_PLT32
#undef R_ARM_PREL31
#undef R_ARM_REL32
#undef R_ARM_RELATIVE
#undef R_ARM_TARGET1
#undef R_ARM_THM_JUMP24
#undef R_ARM_THM_MOVT_ABS
#undef R_ARM_THM_MOVW_ABS_NC
#undef R_ARM_THM_PC22
#undef R_ARM_TLS_LE32
#undef R_ARM_V4BX
#undef R_EXTERN
#undef R_LENGTH
#undef R_PE_ABS
#undef R_PE_RVA
#undef R_PE_THUNK
#undef R_RISCV_32
#undef R_RISCV_32_PCREL
#undef R_RISCV_64
#undef R_RISCV_ADD16
#undef R_RISCV_ADD32
#undef R_RISCV_ADD64
#undef R_RISCV_ALIGN
#undef R_RISCV_BRANCH
#undef R_RISCV_CALL
#undef R_RISCV_CALL_PLT
#undef R_RISCV_COPY
#undef R_RISCV_GOT_HI20
#undef R_RISCV_HI20
#undef R_RISCV_JAL
#undef R_RISCV_JUMP_SLOT
#undef R_RISCV_LO12_I
#undef R_RISCV_LO12_S
#undef R_RISCV_NUM
#undef R_RISCV_PCREL_HI20
#undef R_RISCV_PCREL_LO12_I
#undef R_RISCV_PCREL_LO12_S
#undef R_RISCV_RELATIVE
#undef R_RISCV_RELAX
#undef R_RISCV_RVC_BRANCH
#undef R_RISCV_RVC_JUMP
#undef R_RISCV_SET16
#undef R_RISCV_SET6
#undef R_RISCV_SET8
#undef R_RISCV_SET_ULEB128
#undef R_RISCV_SUB16
#undef R_RISCV_SUB32
#undef R_RISCV_SUB6
#undef R_RISCV_SUB64
#undef R_RISCV_SUB8
#undef R_RISCV_SUB_ULEB128
#undef R_RISCV_TLS_DTPMOD64
#undef R_RISCV_TLS_DTPREL64
#undef R_RISCV_TLS_GD_HI20
#undef R_RISCV_TLS_GOT_HI20
#undef R_RISCV_TLS_TPREL64
#undef R_RISCV_TPREL_ADD
#undef R_RISCV_TPREL_HI20
#undef R_RISCV_TPREL_LO12_I
#undef R_RISCV_TPREL_LO12_S
#undef R_SCATTERED
#undef R_SYMBOLNUM
#undef R_TYPE
#undef R_X86_64_32
#undef R_X86_64_32S
#undef R_X86_64_64
#undef R_X86_64_COPY
#undef R_X86_64_DTPOFF32
#undef R_X86_64_DTPOFF64
#undef R_X86_64_GLOB_DAT
#undef R_X86_64_GOT32
#undef R_X86_64_GOT64
#undef R_X86_64_GOTOFF64
#undef R_X86_64_GOTPC32
#undef R_X86_64_GOTPC64
#undef R_X86_64_GOTPCREL
#undef R_X86_64_GOTPCRELX
#undef R_X86_64_GOTTPOFF
#undef R_X86_64_JUMP_SLOT
#undef R_X86_64_NONE
#undef R_X86_64_NUM
#undef R_X86_64_PC32
#undef R_X86_64_PC64
#undef R_X86_64_PLT32
#undef R_X86_64_PLTOFF64
#undef R_X86_64_RELATIVE
#undef R_X86_64_REX_GOTPCRELX
#undef R_X86_64_TLSGD
#undef R_X86_64_TLSLD
#undef R_X86_64_TPOFF32
#undef R_X86_64_TPOFF64
#undef SECTION_TYPE
#undef SEG_IN_RELRO
#undef SEG_OPENS_LOAD
#undef SEG_PAGE_SIZE
#undef SET_OR_CLEAR
#undef SET_OR_CLEAR_2
#undef SG_READ_ONLY
#undef SHF_ALLOC
#undef SHF_COMPRESSED
#undef SHF_DYNSYM
#undef SHF_EXECINSTR
#undef SHF_GROUP
#undef SHF_PRIVATE
#undef SHF_TLS
#undef SHF_WRITE
#undef SHN_ABS
#undef SHN_COMMON
#undef SHN_FROMDLL
#undef SHN_HIRESERVE
#undef SHN_LOOS
#undef SHN_LORESERVE
#undef SHN_UNDEF
#undef SHN_XINDEX
#undef SHT_ARM_ATTRIBUTES
#undef SHT_ARM_EXIDX
#undef SHT_DYNAMIC
#undef SHT_DYNSYM
#undef SHT_FINI_ARRAY
#undef SHT_GNU_HASH
#undef SHT_GNU_verdef
#undef SHT_GNU_verneed
#undef SHT_GNU_versym
#undef SHT_HASH
#undef SHT_INIT_ARRAY
#undef SHT_LINKEDIT
#undef SHT_LOOS
#undef SHT_NOBITS
#undef SHT_NOTE
#undef SHT_PREINIT_ARRAY
#undef SHT_PROGBITS
#undef SHT_REL
#undef SHT_RELA
#undef SHT_RELX
#undef SHT_RISCV_ATTRIBUTES
#undef SHT_STRTAB
#undef SHT_SYMTAB
#undef SHT_SYMTAB_SHNDX
#undef SHT_X86_64_UNWIND
#undef SORT_RUN
#undef STB_GLOBAL
#undef STB_LOCAL
#undef STB_WEAK
#undef STT_FILE
#undef STT_FUNC
#undef STT_GNU_IFUNC
#undef STT_NOTYPE
#undef STT_OBJECT
#undef STT_SECTION
#undef STT_TLS
#undef STV_DEFAULT
#undef STV_HIDDEN
#undef STV_INTERNAL
#undef STV_PROTECTED
#undef ST_ASM_SET
#undef ST_DATA
#undef ST_FUNC
#undef ST_INLN
#undef ST_PE_EXPORT
#undef S_16BYTE_LITERALS
#undef S_4BYTE_LITERALS
#undef S_8BYTE_LITERALS
#undef S_ATTR_DEBUG
#undef S_ATTR_PURE_INSTRUCTIONS
#undef S_ATTR_SOME_INSTRUCTIONS
#undef S_CSTRING_LITERALS
#undef S_GB_ZEROFILL
#undef S_MOD_INIT_FUNC_POINTERS
#undef S_MOD_TERM_FUNC_POINTERS
#undef S_NON_LAZY_SYMBOL_POINTERS
#undef S_REGULAR
#undef S_SYMBOL_STUBS
#undef S_THREAD_LOCAL_REGULAR
#undef S_THREAD_LOCAL_VARIABLES
#undef S_THREAD_LOCAL_ZEROFILL
#undef S_ZEROFILL
#undef TARGETOS_BSD
#undef THUMB_BX_PC
#undef THUMB_NOP
#undef TLB_BACKEND
#undef TLB_RUN_THUNKS
#undef TLB_TARGETS
#undef TLB_THUNKS
#undef TLI_BACKTRACE
#undef TLI_BCHECK
#undef TLI_ELFINTERP
#undef TLI_HOST_EM
#undef TLI_HOST_FMT
#undef TLI_HOST_RUN
#undef TLI_IS_NATIVE
#undef TLI_OS_IS_BSD
#undef TLI_OUTPUT_DLL
#undef TLI_OUTPUT_DYN
#undef TLI_OUTPUT_EXE
#undef TLI_OUTPUT_FORMAT_BINARY
#undef TLI_OUTPUT_FORMAT_ELF
#undef TLI_OUTPUT_MEMORY
#undef TLI_OUTPUT_OBJ
#undef TLI_PUBLIC
#undef TLI_REL_SECREL32
#undef TLI_REL_SECTION
#undef TLI_SEM
#undef TLI_SET_STATE
#undef TLI_STATE_VAR
#undef TLI_VERSION
#undef TL_B32
#undef TL_B64
#undef TL_BACKENDS
#undef TL_DECLARE
#undef TL_ENTRY
#undef TL_MAX_REPORTED_ERRORS
#undef TL_WITH_CORE32
#undef TL_WITH_CORE64
#undef Tag_ABI_FP_denormal
#undef Tag_ABI_FP_exceptions
#undef Tag_ABI_FP_number_model
#undef Tag_ABI_PCS_wchar_t
#undef Tag_ABI_VFP_args
#undef Tag_ABI_align_needed
#undef Tag_ABI_align_preserved
#undef Tag_ABI_enum_size
#undef Tag_ABI_optimization_goals
#undef Tag_ARM_ISA_use
#undef Tag_CPU_arch
#undef Tag_CPU_name
#undef Tag_CPU_unaligned_access
#undef Tag_FP_arch
#undef Tag_RISCV_arch
#undef Tag_THUMB_ISA_use
#undef USE_TRIPLET
#undef VM_PROT_EXECUTE
#undef VM_PROT_READ
#undef VM_PROT_WRITE
#undef WAIT_SEM
#undef WIN32_LEAN_AND_MEAN
#undef X86_64_GRP5
#undef X86_64_JMP_REL
#undef X86_64_MODRM_PUSH
#undef X86_64_MODRM_RIP
#undef X86_64_PLT_ENTRY_SIZE
#undef X86_64_PUSH_IMM
#undef _tcc_error
#undef addr_t
#undef bounds_section
#undef bss_section
#undef common_section
#undef data_section
#undef dt_verneednum
#undef dwarf_abbrev_section
#undef dwarf_aranges_section
#undef dwarf_info_section
#undef dwarf_line_section
#undef dwarf_line_str_section
#undef dwarf_str_section
#undef for_each_elem
#undef free
#undef inline
#undef lbounds_section
#undef malloc
#undef nb_sym_to_version
#undef nb_sym_versions
#undef pe_push
#undef qrel
#undef rdata
#undef realloc
#undef rodata_section
#undef shf_RELRO
#undef snprintf
#undef ssize_t
#undef stab_section
#undef stabstr_section
#undef static
#undef strcasecmp
#undef strdup
#undef strtoull
#undef sym_to_version
#undef sym_versions
#undef symtab_section
#undef tbd_cut
#undef tbd_cut_quote
#undef tbd_cut_space
#undef tbd_seek
#undef tbd_seek_any
#undef tbd_skip_ws
#undef text_section
#undef tli_error
#undef tli_error_noabort
#undef tli_fclose
#undef tli_fopen
#undef tli_free
#undef tli_malloc
#undef tli_mallocz
#undef tli_module
#undef tli_realloc
#undef tli_setjmp
#undef tli_strdup
#undef tli_warning
#undef tli_warning_c
#undef ucontext_t
#undef verneed_section
#undef versym_section
#undef vsnprintf

#endif // TINYLINKER_PACKED_BODY
#endif
