Skip to content

Commit d7b5dec

Browse files
committed
update
1 parent c7c02ae commit d7b5dec

File tree

4 files changed

+105
-106
lines changed

4 files changed

+105
-106
lines changed

src/launchpad/parsers/apple/macho_parser.py

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import lief
1212
import sentry_sdk
1313

14-
from launchpad.size.models.apple import CodeSignatureInfo, DyldInfo, LinkEditInfo
14+
from launchpad.size.models.apple import LinkEditInfo
1515

1616
from ...utils.logging import get_logger
1717
from .binary_utils import parse_null_terminated_strings
@@ -299,57 +299,63 @@ def find_symbol(addr: int) -> lief.Symbol | None:
299299

300300
return symbols
301301

302-
@sentry_sdk.trace
303-
def extract_dyld_info(self) -> DyldInfo:
304-
"""Extract DYLD information from LC_DYLD_INFO load commands."""
305-
dyld_chained_fixups = self.binary.dyld_chained_fixups
306-
dyld_exports_trie = self.binary.dyld_exports_trie
307-
return DyldInfo(
308-
chained_fixups_size=(dyld_chained_fixups.data_size if dyld_chained_fixups else 0),
309-
export_trie_size=dyld_exports_trie.data_size if dyld_exports_trie else 0,
310-
)
311-
312-
@sentry_sdk.trace
313-
def extract_code_signature_info(self) -> CodeSignatureInfo | None:
314-
"""Extract code signature information from LC_CODE_SIGNATURE load command."""
315-
if not self.binary.has_code_signature:
316-
return None
317-
318-
cs = self.binary.code_signature
319-
return CodeSignatureInfo(
320-
size=cs.data_size,
321-
offset=cs.data_offset,
322-
)
323-
324302
@sentry_sdk.trace
325303
def extract_linkedit_info(self) -> LinkEditInfo:
326-
"""Extract __LINKEDIT segment component sizes from load commands."""
304+
"""Extract all __LINKEDIT segment component sizes from load commands.
305+
306+
Consolidates symbol tables, DYLD info, code signature, and segment size into one structure.
307+
"""
327308
symbol_table_size = 0
328309
string_table_size = 0
329310
function_starts_size = 0
330311
segment_size = 0
312+
chained_fixups_size = 0
313+
export_trie_size = 0
314+
code_signature_size = 0
315+
code_signature_offset = 0
331316

317+
# Determine if binary is 64-bit (nlist_64 is 16 bytes, nlist is 12 bytes)
332318
is_64bit = self.binary.header.magic in [
333319
lief.MachO.MACHO_TYPES.MAGIC_64,
334320
lief.MachO.MACHO_TYPES.CIGAM_64,
335321
]
336322
entry_size = 16 if is_64bit else 12
337323

324+
# Extract from load commands
338325
for cmd in self.binary.commands:
339326
if isinstance(cmd, lief.MachO.SymbolCommand):
327+
# LC_SYMTAB: symbol table + string table
340328
symbol_table_size = cmd.numberof_symbols * entry_size
341329
string_table_size = cmd.strings_size
342330
elif isinstance(cmd, lief.MachO.FunctionStarts):
331+
# LC_FUNCTION_STARTS
343332
function_starts_size = cmd.data_size
344333

334+
# Extract DYLD info
335+
dyld_chained_fixups = self.binary.dyld_chained_fixups
336+
dyld_exports_trie = self.binary.dyld_exports_trie
337+
chained_fixups_size = dyld_chained_fixups.data_size if dyld_chained_fixups else 0
338+
export_trie_size = dyld_exports_trie.data_size if dyld_exports_trie else 0
339+
340+
# Extract code signature
341+
if self.binary.has_code_signature:
342+
cs = self.binary.code_signature
343+
code_signature_size = cs.data_size
344+
code_signature_offset = cs.data_offset
345+
346+
# Get __LINKEDIT segment size
345347
for segment in self.binary.segments:
346348
if segment.name == "__LINKEDIT":
347349
segment_size = segment.file_size
348350
break
349351

350352
return LinkEditInfo(
353+
segment_size=segment_size,
351354
symbol_table_size=symbol_table_size,
352355
string_table_size=string_table_size,
353356
function_starts_size=function_starts_size,
354-
segment_size=segment_size,
357+
chained_fixups_size=chained_fixups_size,
358+
export_trie_size=export_trie_size,
359+
code_signature_size=code_signature_size,
360+
code_signature_offset=code_signature_offset,
355361
)

src/launchpad/size/analyzers/apple.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,6 @@ def _analyze_binary(
439439
objc_method_names = parser.parse_objc_method_names()
440440
segments = self._extract_segments_info(parser.binary)
441441
load_commands = self._extract_load_commands_info(parser.binary)
442-
dyld_info = parser.extract_dyld_info()
443-
code_signature_info = parser.extract_code_signature_info()
444442
linkedit_info = parser.extract_linkedit_info()
445443

446444
symbol_info = None
@@ -498,10 +496,8 @@ def _analyze_binary(
498496
segments=segments,
499497
load_commands=load_commands,
500498
header_size=parser.get_header_size(),
501-
dyld_info=dyld_info,
502499
dwarf_relocations=dwarf_relocations,
503500
strippable_symbols_size=strippable_symbols_size,
504-
code_signature_info=code_signature_info,
505501
linkedit_info=linkedit_info,
506502
)
507503

src/launchpad/size/models/apple.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -127,29 +127,29 @@ class LoadCommandInfo:
127127

128128

129129
@dataclass
130-
class DyldInfo:
131-
"""DYLD-specific information extracted from related DYLD load commands."""
132-
133-
chained_fixups_size: int = 0
134-
export_trie_size: int = 0
135-
136-
137-
@dataclass
138-
class CodeSignatureInfo:
139-
"""Code signature information extracted from LC_CODE_SIGNATURE load command."""
140-
141-
size: int = 0
142-
offset: int = 0
130+
class LinkEditInfo:
131+
"""Link edit segment components extracted from various load commands in __LINKEDIT.
143132
133+
Consolidates all __LINKEDIT segment data including symbol tables, DYLD info, and code signature.
134+
"""
144135

145-
@dataclass
146-
class LinkEditInfo:
147-
"""Link edit segment components extracted from various load commands in __LINKEDIT."""
136+
# Segment
137+
segment_size: int = 0
148138

139+
# Symbol tables (from LC_SYMTAB)
149140
symbol_table_size: int = 0
150141
string_table_size: int = 0
142+
143+
# Debugging (from LC_FUNCTION_STARTS)
151144
function_starts_size: int = 0
152-
segment_size: int = 0
145+
146+
# DYLD (from LC_DYLD_CHAINED_FIXUPS, LC_DYLD_EXPORTS_TRIE)
147+
chained_fixups_size: int = 0
148+
export_trie_size: int = 0
149+
150+
# Code signing (from LC_CODE_SIGNATURE)
151+
code_signature_size: int = 0
152+
code_signature_offset: int = 0
153153

154154

155155
@dataclass
@@ -168,10 +168,8 @@ class MachOBinaryAnalysis:
168168
swift_metadata: SwiftMetadata | None = None
169169
symbol_info: SymbolInfo | None = None
170170
header_size: int = 0
171-
dyld_info: DyldInfo | None = None
172171
dwarf_relocations: DwarfRelocationsData | None = None
173172
strippable_symbols_size: int = 0
174-
code_signature_info: CodeSignatureInfo | None = None
175173
linkedit_info: LinkEditInfo | None = None
176174

177175

src/launchpad/size/treemap/macho_element_builder.py

Lines changed: 57 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -510,79 +510,78 @@ def _build_linkedit_children(self, binary_analysis: MachOBinaryAnalysis) -> List
510510
"""
511511
linkedit_children: List[TreemapElement] = []
512512

513-
# Add link edit components (symbol table, string table, function starts)
514513
le = binary_analysis.linkedit_info
515-
if le is not None:
516-
if le.string_table_size > 0:
517-
linkedit_children.append(
518-
TreemapElement(
519-
name="String Table",
520-
size=le.string_table_size,
521-
type=TreemapType.EXECUTABLES,
522-
path=None,
523-
is_dir=False,
524-
children=[],
525-
)
514+
if le is None:
515+
return linkedit_children
516+
517+
# Add symbol table and string table
518+
if le.string_table_size > 0:
519+
linkedit_children.append(
520+
TreemapElement(
521+
name="String Table",
522+
size=le.string_table_size,
523+
type=TreemapType.EXECUTABLES,
524+
path=None,
525+
is_dir=False,
526+
children=[],
526527
)
528+
)
527529

528-
if le.symbol_table_size > 0:
529-
linkedit_children.append(
530-
TreemapElement(
531-
name="Symbol Table",
532-
size=le.symbol_table_size,
533-
type=TreemapType.EXECUTABLES,
534-
path=None,
535-
is_dir=False,
536-
children=[],
537-
)
530+
if le.symbol_table_size > 0:
531+
linkedit_children.append(
532+
TreemapElement(
533+
name="Symbol Table",
534+
size=le.symbol_table_size,
535+
type=TreemapType.EXECUTABLES,
536+
path=None,
537+
is_dir=False,
538+
children=[],
538539
)
540+
)
539541

540-
if le.function_starts_size > 0:
541-
linkedit_children.append(
542-
TreemapElement(
543-
name="Function Starts",
544-
size=le.function_starts_size,
545-
type=TreemapType.EXECUTABLES,
546-
path=None,
547-
is_dir=False,
548-
children=[],
549-
)
542+
if le.function_starts_size > 0:
543+
linkedit_children.append(
544+
TreemapElement(
545+
name="Function Starts",
546+
size=le.function_starts_size,
547+
type=TreemapType.EXECUTABLES,
548+
path=None,
549+
is_dir=False,
550+
children=[],
550551
)
552+
)
551553

552-
# Add DYLD info children
553-
di = binary_analysis.dyld_info
554-
if di is not None:
555-
if di.chained_fixups_size > 0:
556-
linkedit_children.append(
557-
TreemapElement(
558-
name="Chained Fixups",
559-
size=di.chained_fixups_size,
560-
type=TreemapType.DYLD,
561-
path=None,
562-
is_dir=False,
563-
children=[],
564-
)
554+
# Add DYLD info
555+
if le.chained_fixups_size > 0:
556+
linkedit_children.append(
557+
TreemapElement(
558+
name="Chained Fixups",
559+
size=le.chained_fixups_size,
560+
type=TreemapType.DYLD,
561+
path=None,
562+
is_dir=False,
563+
children=[],
565564
)
565+
)
566566

567-
if di.export_trie_size > 0:
568-
linkedit_children.append(
569-
TreemapElement(
570-
name="Export Trie",
571-
size=di.export_trie_size,
572-
type=TreemapType.DYLD,
573-
path=None,
574-
is_dir=False,
575-
children=[],
576-
)
567+
if le.export_trie_size > 0:
568+
linkedit_children.append(
569+
TreemapElement(
570+
name="Export Trie",
571+
size=le.export_trie_size,
572+
type=TreemapType.DYLD,
573+
path=None,
574+
is_dir=False,
575+
children=[],
577576
)
577+
)
578578

579579
# Add code signature
580-
cs = binary_analysis.code_signature_info
581-
if cs is not None and cs.size > 0:
580+
if le.code_signature_size > 0:
582581
linkedit_children.append(
583582
TreemapElement(
584583
name="Code Signature",
585-
size=cs.size,
584+
size=le.code_signature_size,
586585
type=TreemapType.CODE_SIGNATURE,
587586
path=None,
588587
is_dir=False,

0 commit comments

Comments
 (0)