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