Skip to content

Commit

Permalink
Works for Linux Arm, Mac Arm broken
Browse files Browse the repository at this point in the history
  • Loading branch information
savannahostrowski committed Apr 25, 2024
1 parent 4b10e20 commit fef6cc0
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 18 deletions.
2 changes: 2 additions & 0 deletions Python/jit.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ patch(unsigned char *base, const Stencil *stencil, uintptr_t patches[])
case HoleKind_ARM64_RELOC_GOT_LOAD_PAGE21:
case HoleKind_IMAGE_REL_ARM64_PAGEBASE_REL21:
case HoleKind_R_AARCH64_ADR_GOT_PAGE:
case HoleKind_ARM64_RELOC_BRANCH26:
// 21-bit count of pages between this page and an absolute address's
// page... I know, I know, it's weird. Pairs nicely with
// ARM64_RELOC_GOT_LOAD_PAGEOFF12 (below).
Expand All @@ -287,6 +288,7 @@ patch(unsigned char *base, const Stencil *stencil, uintptr_t patches[])
if (i + 1 < stencil->holes_size &&
(next_hole->kind == HoleKind_ARM64_RELOC_GOT_LOAD_PAGEOFF12 ||
next_hole->kind == HoleKind_IMAGE_REL_ARM64_PAGEOFFSET_12L ||
next_hole->kind == HoleKind_ARM64_RELOC_BRANCH26 ||
next_hole->kind == HoleKind_R_AARCH64_LD64_GOT_LO12_NC) &&
next_hole->offset == hole->offset + 4 &&
next_hole->symbol == hole->symbol &&
Expand Down
10 changes: 7 additions & 3 deletions Tools/jit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,21 @@ sudo ./llvm.sh 16

### macOS

Install LLVM 16 with [Homebrew](https://brew.sh):
Install LLVM 18 with [Homebrew](https://brew.sh):

```sh
brew install llvm@16
brew install llvm@18
```

Homebrew won't add any of the tools to your `$PATH`. That's okay; the build script knows how to find them.

### Windows

Install LLVM 16 [by searching for it on LLVM's GitHub releases page](https://github.com/llvm/llvm-project/releases?q=16), clicking on "Assets", downloading the appropriate Windows installer for your platform (likely the file ending with `-win64.exe`), and running it. **When installing, be sure to select the option labeled "Add LLVM to the system PATH".**
Install LLVM 18 [by searching for it on LLVM's GitHub releases page](https://github.com/llvm/llvm-project/releases?q=18), clicking on "Assets", downloading the appropriate Windows installer for your platform (likely the file ending with `-win64.exe`), and running it. **When installing, be sure to select the option labeled "Add LLVM to the system PATH".**

## Devcontainers

If you are working CPython in a [Codespaces instance](https://devguide.python.org/getting-started/setup-building/#using-codespaces), there's no need to install LLVM as the Fedora 40 base image includes LLVM 18 out of the box.

## Building

Expand Down
2 changes: 1 addition & 1 deletion Tools/jit/_llvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import subprocess
import typing

_LLVM_VERSION = 16
_LLVM_VERSION = 18
_LLVM_VERSION_PATTERN = re.compile(rf"version\s+{_LLVM_VERSION}\.\d+\.\d+\s+")

_P = typing.ParamSpec("_P")
Expand Down
8 changes: 6 additions & 2 deletions Tools/jit/_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import typing

HoleKind: typing.TypeAlias = typing.Literal[
"ARM64_RELOC_BRANCH26",
"ARM64_RELOC_GOT_LOAD_PAGE21",
"ARM64_RELOC_GOT_LOAD_PAGEOFF12",
"ARM64_RELOC_PAGE21",
Expand Down Expand Up @@ -89,6 +90,10 @@ class COFFSection(typing.TypedDict):
SectionData: typing.NotRequired[dict[typing.Literal["Bytes"], list[int]]]
Symbols: list[dict[typing.Literal["Symbol"], _COFFSymbol]]

class _ElfSectionType(typing.TypedDict):
"""An ELF object file section type."""
Name: str
Value: int

class ELFSection(typing.TypedDict):
"""An ELF object file section."""
Expand All @@ -99,8 +104,7 @@ class ELFSection(typing.TypedDict):
Relocations: list[dict[typing.Literal["Relocation"], ELFRelocation]]
SectionData: dict[typing.Literal["Bytes"], list[int]]
Symbols: list[dict[typing.Literal["Symbol"], _ELFSymbol]]
Type: dict[typing.Literal["Value"], str]

Type: _ElfSectionType

class MachOSection(typing.TypedDict):
"""A Mach-O object file section."""
Expand Down
23 changes: 11 additions & 12 deletions Tools/jit/_targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ async def _compile(
# Emit relaxable 64-bit calls/jumps, so we don't have to worry about
# about emitting in-range trampolines for out-of-range targets.
# We can probably remove this and emit trampolines in the future:
"-fno-plt",
# "-fno-plt",
# Don't call stack-smashing canaries that we can't find or patch:
"-fno-stack-protector",
"-o",
Expand Down Expand Up @@ -262,7 +262,7 @@ class _ELF(
def _handle_section(
self, section: _schema.ELFSection, group: _stencils.StencilGroup
) -> None:
section_type = section["Type"]["Value"]
section_type = section["Type"]["Name"]
flags = {flag["Name"] for flag in section["Flags"]["Flags"]}
if section_type == "SHT_RELA":
assert "SHF_INFO_LINK" in flags, flags
Expand Down Expand Up @@ -290,7 +290,7 @@ def _handle_section(
for wrapped_symbol in section["Symbols"]:
symbol = wrapped_symbol["Symbol"]
offset = len(stencil.body) + symbol["Value"]
name = symbol["Name"]["Value"]
name = symbol["Name"]["Name"]
name = name.removeprefix(self.prefix)
group.symbols[name] = value, offset
stencil.body.extend(section["SectionData"]["Bytes"])
Expand All @@ -312,9 +312,9 @@ def _handle_relocation(
case {
"Addend": addend,
"Offset": offset,
"Symbol": {"Value": s},
"Symbol": {"Name": s},
"Type": {
"Value": "R_AARCH64_ADR_GOT_PAGE"
"Name": "R_AARCH64_ADR_GOT_PAGE"
| "R_AARCH64_LD64_GOT_LO12_NC"
| "R_X86_64_GOTPCREL"
| "R_X86_64_GOTPCRELX"
Expand All @@ -327,8 +327,8 @@ def _handle_relocation(
case {
"Addend": addend,
"Offset": offset,
"Symbol": {"Value": s},
"Type": {"Value": kind},
"Symbol": {"Name": s},
"Type": {"Name": kind},
}:
offset += base
s = s.removeprefix(self.prefix)
Expand Down Expand Up @@ -371,7 +371,7 @@ def _handle_section(
for wrapped_symbol in section["Symbols"]:
symbol = wrapped_symbol["Symbol"]
offset = symbol["Value"] - start_address
name = symbol["Name"]["Value"]
name = symbol["Name"]["Name"]
name = name.removeprefix(self.prefix)
group.symbols[name] = value, offset
assert "Relocations" in section
Expand Down Expand Up @@ -431,8 +431,8 @@ def _handle_relocation(
"Type": {"Value": kind},
} | {
"Offset": offset,
"Symbol": {"Value": s},
"Type": {"Value": kind},
"Symbol": {"Name": s},
"Type": {"Name": kind},
}:
offset += base
s = s.removeprefix(self.prefix)
Expand All @@ -446,8 +446,7 @@ def _handle_relocation(
def get_target(host: str) -> _COFF | _ELF | _MachO:
"""Build a _Target for the given host "triple" and options."""
if re.fullmatch(r"aarch64-apple-darwin.*", host):
args = ["-mcmodel=large"]
return _MachO(host, alignment=8, args=args, prefix="_")
return _MachO(host, alignment=8, prefix="_")
if re.fullmatch(r"aarch64-pc-windows-msvc", host):
args = ["-fms-runtime-lib=dll"]
return _COFF(host, alignment=8, args=args)
Expand Down

0 comments on commit fef6cc0

Please sign in to comment.