Skip to content

Commit c4c3ccc

Browse files
committed
Ditto for Mach-O, and *way* simplify the parsing!
1 parent 9db0563 commit c4c3ccc

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

Tools/jit/_schema.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class ELFSection(typing.TypedDict):
101101
Index: int
102102
Info: int
103103
Relocations: list[dict[typing.Literal["Relocation"], ELFRelocation]]
104-
SectionData: dict[typing.Literal["Bytes"], list[int]]
104+
SectionData: typing.NotRequired[dict[typing.Literal["Bytes"], list[int]]]
105105
Size: int
106106
Symbols: list[dict[typing.Literal["Symbol"], _ELFSymbol]]
107107
Type: dict[typing.Literal["Name"], str]
@@ -118,4 +118,6 @@ class MachOSection(typing.TypedDict):
118118
list[dict[typing.Literal["Relocation"], MachORelocation]]
119119
]
120120
SectionData: typing.NotRequired[dict[typing.Literal["Bytes"], list[int]]]
121+
Segment: dict[typing.Literal["Value"], str]
122+
Size: int
121123
Symbols: typing.NotRequired[list[dict[typing.Literal["Symbol"], _MachOSymbol]]]

Tools/jit/_targets.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ def _handle_section(
400400
value = _stencils.HoleValue.DATA
401401
stencil = group.data
402402
if section_type == "SHT_PROGBITS":
403+
assert "SectionData" in section
403404
section_data_bytes = section["SectionData"]["Bytes"]
404405
else:
405406
# Zeroed BSS data:
@@ -474,7 +475,11 @@ def _handle_section(
474475
self, section: _schema.MachOSection, group: _stencils.StencilGroup
475476
) -> None:
476477
assert section["Address"] >= len(group.code.body)
477-
assert "SectionData" in section
478+
if "SectionData" in section:
479+
section_data_bytes = section["SectionData"]["Bytes"]
480+
else:
481+
# Zeroed BSS data:
482+
section_data_bytes = [0] * section["Size"]
478483
flags = {flag["Name"] for flag in section["Attributes"]["Flags"]}
479484
name = section["Name"]["Value"]
480485
name = name.removeprefix(self.symbol_prefix)
@@ -483,23 +488,23 @@ def _handle_section(
483488
if "PureInstructions" in flags:
484489
value = _stencils.HoleValue.CODE
485490
stencil = group.code
486-
start_address = 0
487-
group.symbols[name] = value, section["Address"] - start_address
488491
else:
489492
value = _stencils.HoleValue.DATA
490493
stencil = group.data
491-
start_address = len(group.code.body)
492-
group.symbols[name] = value, len(group.code.body)
493-
base = section["Address"] - start_address
494+
segment = section["Segment"]["Value"]
495+
if segment == "__DATA":
496+
value = _stencils.HoleValue.WRITABLE
497+
section_data_bytes = []
498+
else:
499+
assert segment == "__TEXT", segment
500+
base = len(stencil.body)
501+
group.symbols[name] = value, base
494502
group.symbols[section["Index"]] = value, base
495-
stencil.body.extend(
496-
[0] * (section["Address"] - len(group.code.body) - len(group.data.body))
497-
)
498-
stencil.body.extend(section["SectionData"]["Bytes"])
503+
stencil.body.extend(section_data_bytes)
499504
assert "Symbols" in section
500505
for wrapped_symbol in section["Symbols"]:
501506
symbol = wrapped_symbol["Symbol"]
502-
offset = symbol["Value"] - start_address
507+
offset = symbol["Value"] - section["Address"] + base
503508
name = symbol["Name"]["Name"]
504509
name = name.removeprefix(self.symbol_prefix)
505510
group.symbols[name] = value, offset

0 commit comments

Comments
 (0)