@@ -390,7 +390,7 @@ def _handle_section(
390
390
relocation = wrapped_relocation ["Relocation" ]
391
391
hole = self ._handle_relocation (base , relocation , stencil .body )
392
392
stencil .holes .append (hole )
393
- elif section_type == "SHT_PROGBITS" :
393
+ elif section_type in { "SHT_PROGBITS" , "SHT_NOBITS" } :
394
394
if "SHF_ALLOC" not in flags :
395
395
return
396
396
if "SHF_EXECINSTR" in flags :
@@ -399,7 +399,12 @@ def _handle_section(
399
399
else :
400
400
value = _stencils .HoleValue .DATA
401
401
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" ]
403
408
if "SHF_WRITE" in flags :
404
409
assert value is _stencils .HoleValue .DATA
405
410
value = _stencils .HoleValue .WRITABLE
@@ -470,7 +475,11 @@ def _handle_section(
470
475
self , section : _schema .MachOSection , group : _stencils .StencilGroup
471
476
) -> None :
472
477
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" ]
474
483
flags = {flag ["Name" ] for flag in section ["Attributes" ]["Flags" ]}
475
484
name = section ["Name" ]["Value" ]
476
485
name = name .removeprefix (self .symbol_prefix )
@@ -479,23 +488,23 @@ def _handle_section(
479
488
if "PureInstructions" in flags :
480
489
value = _stencils .HoleValue .CODE
481
490
stencil = group .code
482
- start_address = 0
483
- group .symbols [name ] = value , section ["Address" ] - start_address
484
491
else :
485
492
value = _stencils .HoleValue .DATA
486
493
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
490
502
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 )
495
504
assert "Symbols" in section
496
505
for wrapped_symbol in section ["Symbols" ]:
497
506
symbol = wrapped_symbol ["Symbol" ]
498
- offset = symbol ["Value" ] - start_address
507
+ offset = symbol ["Value" ] - section [ "Address" ] + base
499
508
name = symbol ["Name" ]["Name" ]
500
509
name = name .removeprefix (self .symbol_prefix )
501
510
group .symbols [name ] = value , offset
0 commit comments