Prevent file names from being included in the binary data section

When compiling a project in Xcode, the file names of linked against Swift PM modules are put into the data section of the binary as string literals.

E.g. having a module/package Monitoring which contains the file Monitoring.swift, the target binary linking the module will contain the string literal Monitoring/Monitoring.swift in the data section.

This makes reverse engineering of the binary much easier. Therefore the question, is there a way to prevent these paths from being written into the binary as string literals?

Just saw that also a couple of file names are included as strings which are part of the main binary, and not part of a linked module. Same question though.

Using Hopper Disassembler:

Depending on your build configuration this could be part of the debug info, or coverage data. Depending on the code in the module it could also be because of function calls that include #file as an argument for error messages

2 Likes

@Keith Thank you for throwing in some ideas! The build is a release build with deployment post-processing turned on which includes symbol stripping. I'm not using #file anywhere explicitly.

In hopper what are the references pointing to the strings? (In your screenshot sub_10007ed30+236 for example). Also what section of the binary are they in (should show in the bottom left of hopper if you're selected where your screenshot is)

1 Like

Seems like the file name string literals come from fatalError calls which would make sense as fatalError defines #file as a default argument fatalError(_:file:line:) | Apple Developer Documentation. Will try out a version with all fatalError calls removed.

1 Like

Solved this by setting file in fatalError to an empty StaticString and changing try! calls to try handled in do / catch blocks. @Keith Thanks for nudging me in the right direction!

1 Like