Skip to content

Commit 558c83b

Browse files
committed
Merge branch 'jit-tests' of https://github.com/brandtbucher/cpython into jit-tests
2 parents c6fc7bd + c4c3ccc commit 558c83b

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

Tools/jit/_schema.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ 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]]]
105+
Size: int
105106
Symbols: list[dict[typing.Literal["Symbol"], _ELFSymbol]]
106107
Type: dict[typing.Literal["Name"], str]
107108

@@ -117,4 +118,6 @@ class MachOSection(typing.TypedDict):
117118
list[dict[typing.Literal["Relocation"], MachORelocation]]
118119
]
119120
SectionData: typing.NotRequired[dict[typing.Literal["Bytes"], list[int]]]
121+
Segment: dict[typing.Literal["Value"], str]
122+
Size: int
120123
Symbols: typing.NotRequired[list[dict[typing.Literal["Symbol"], _MachOSymbol]]]

Tools/jit/_targets.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ def _handle_section(
390390
relocation = wrapped_relocation["Relocation"]
391391
hole = self._handle_relocation(base, relocation, stencil.body)
392392
stencil.holes.append(hole)
393-
elif section_type == "SHT_PROGBITS":
393+
elif section_type in {"SHT_PROGBITS", "SHT_NOBITS"}:
394394
if "SHF_ALLOC" not in flags:
395395
return
396396
if "SHF_EXECINSTR" in flags:
@@ -399,7 +399,12 @@ def _handle_section(
399399
else:
400400
value = _stencils.HoleValue.DATA
401401
stencil = group.data
402-
section_data_bytes = section["SectionData"]["Bytes"]
402+
if section_type == "SHT_PROGBITS":
403+
assert "SectionData" in section
404+
section_data_bytes = section["SectionData"]["Bytes"]
405+
else:
406+
# Zeroed BSS data:
407+
section_data_bytes = [0] * section["Size"]
403408
if "SHF_WRITE" in flags:
404409
assert value is _stencils.HoleValue.DATA
405410
value = _stencils.HoleValue.WRITABLE
@@ -470,7 +475,11 @@ def _handle_section(
470475
self, section: _schema.MachOSection, group: _stencils.StencilGroup
471476
) -> None:
472477
assert section["Address"] >= len(group.code.body)
473-
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"]
474483
flags = {flag["Name"] for flag in section["Attributes"]["Flags"]}
475484
name = section["Name"]["Value"]
476485
name = name.removeprefix(self.symbol_prefix)
@@ -479,23 +488,23 @@ def _handle_section(
479488
if "PureInstructions" in flags:
480489
value = _stencils.HoleValue.CODE
481490
stencil = group.code
482-
start_address = 0
483-
group.symbols[name] = value, section["Address"] - start_address
484491
else:
485492
value = _stencils.HoleValue.DATA
486493
stencil = group.data
487-
start_address = len(group.code.body)
488-
group.symbols[name] = value, len(group.code.body)
489-
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
490502
group.symbols[section["Index"]] = value, base
491-
stencil.body.extend(
492-
[0] * (section["Address"] - len(group.code.body) - len(group.data.body))
493-
)
494-
stencil.body.extend(section["SectionData"]["Bytes"])
503+
stencil.body.extend(section_data_bytes)
495504
assert "Symbols" in section
496505
for wrapped_symbol in section["Symbols"]:
497506
symbol = wrapped_symbol["Symbol"]
498-
offset = symbol["Value"] - start_address
507+
offset = symbol["Value"] - section["Address"] + base
499508
name = symbol["Name"]["Name"]
500509
name = name.removeprefix(self.symbol_prefix)
501510
group.symbols[name] = value, offset

0 commit comments

Comments
 (0)