@@ -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