@@ -400,6 +400,7 @@ def _handle_section(
400
400
value = _stencils .HoleValue .DATA
401
401
stencil = group .data
402
402
if section_type == "SHT_PROGBITS" :
403
+ assert "SectionData" in section
403
404
section_data_bytes = section ["SectionData" ]["Bytes" ]
404
405
else :
405
406
# Zeroed BSS data:
@@ -474,7 +475,11 @@ def _handle_section(
474
475
self , section : _schema .MachOSection , group : _stencils .StencilGroup
475
476
) -> None :
476
477
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" ]
478
483
flags = {flag ["Name" ] for flag in section ["Attributes" ]["Flags" ]}
479
484
name = section ["Name" ]["Value" ]
480
485
name = name .removeprefix (self .symbol_prefix )
@@ -483,23 +488,23 @@ def _handle_section(
483
488
if "PureInstructions" in flags :
484
489
value = _stencils .HoleValue .CODE
485
490
stencil = group .code
486
- start_address = 0
487
- group .symbols [name ] = value , section ["Address" ] - start_address
488
491
else :
489
492
value = _stencils .HoleValue .DATA
490
493
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
494
502
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 )
499
504
assert "Symbols" in section
500
505
for wrapped_symbol in section ["Symbols" ]:
501
506
symbol = wrapped_symbol ["Symbol" ]
502
- offset = symbol ["Value" ] - start_address
507
+ offset = symbol ["Value" ] - section [ "Address" ] + base
503
508
name = symbol ["Name" ]["Name" ]
504
509
name = name .removeprefix (self .symbol_prefix )
505
510
group .symbols [name ] = value , offset
0 commit comments