feat(asm): emit linkable ELF and Mach-O objects with external symbols

Assisted-by: Qwen 3.8 Max Preview
This commit is contained in:
2026-07-16 20:52:20 +02:00
parent b914c0e390
commit 900c9772b1
9 changed files with 1165 additions and 56 deletions
+8 -3
View File
@@ -30,9 +30,12 @@ func Assemble(t *ast.Text) ([]byte, map[string]int, error) {
// linkInfo carries file-level symbol context into a single-function assembly:
// the set of static symbols a GLOBL in the same file defines. A nil link
// rejects SB operands outright (single-function assembly cannot resolve
// them).
// them). When allowExternal is set, a reference to a symbol no GLOBL in the
// file defines is recorded as an external relocation instead of failing —
// the object-file emitters resolve it at link time.
type linkInfo struct {
symbols map[string]bool
symbols map[string]bool
allowExternal bool
}
// sbPatch is a function-relative static-symbol relocation: the disp32 field
@@ -425,7 +428,9 @@ func operandFromAST(op *ast.Operand, size int, fi frameInfo, link *linkInfo) (Op
if a.Sym.Static {
return nil, fmt.Errorf("undefined symbol %q", a.Sym.Name)
}
return nil, fmt.Errorf("external symbol %q needs object-file emission", a.Sym.Name)
if !link.allowExternal {
return nil, fmt.Errorf("external symbol %q needs object-file emission", a.Sym.Name)
}
}
return sbMem{size: size, name: a.Sym.Name, addend: a.Sym.Offset}, nil
}