Skip to content

Commit 374f788

Browse files
authored
Add support for symbol stripping (#70233)
1 parent 6a56aa3 commit 374f788

File tree

5 files changed

+67
-12
lines changed

5 files changed

+67
-12
lines changed

eng/native/functions.cmake

+4-3
Original file line numberDiff line numberDiff line change
@@ -390,11 +390,12 @@ function(strip_symbols targetName outputFilename)
390390
message(FATAL_ERROR "strip not found")
391391
endif()
392392

393+
set(strip_command ${STRIP} -no_code_signature_warning -S ${strip_source_file})
394+
395+
# codesign release build
393396
string(TOLOWER "${CMAKE_BUILD_TYPE}" LOWERCASE_CMAKE_BUILD_TYPE)
394397
if (LOWERCASE_CMAKE_BUILD_TYPE STREQUAL release)
395-
set(strip_command ${STRIP} -no_code_signature_warning -S ${strip_source_file} && codesign -f -s - ${strip_source_file})
396-
else ()
397-
set(strip_command)
398+
set(strip_command ${strip_command} && codesign -f -s - ${strip_source_file})
398399
endif ()
399400

400401
execute_process(

src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Publish.targets

+7-5
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,17 @@
8686
</Target>
8787

8888
<Target Name="CopyNativeBinary" AfterTargets="Publish">
89-
<!-- override apphost with binary we generated during native compilation -->
89+
<!-- replace apphost with binary we generated during native compilation -->
9090
<Delete Files="$(PublishDir)\$(TargetName)$(NativeBinaryExt)" />
9191
<Copy SourceFiles="$(NativeOutputPath)$(TargetName)$(NativeBinaryExt)" DestinationFolder="$(PublishDir)" />
92-
</Target>
9392

94-
<Target Name="CopyNativePdb" Condition="'$(DebugType)' != 'None' and '$(TargetOS)' == 'windows' and '$(NativeLib)' != 'Static'" AfterTargets="Publish">
95-
<!-- dotnet CLI produces managed debug symbols - substitute with those we generated during native compilation -->
93+
<!-- dotnet CLI produces managed debug symbols, which we will delete and copy native symbols instead -->
9694
<Delete Files="$(PublishDir)\$(TargetName).pdb" />
97-
<Copy SourceFiles="$(NativeOutputPath)$(TargetName).pdb" DestinationFolder="$(PublishDir)" />
95+
96+
<!-- replace native symbol file if it exists -->
97+
<Delete Files="$(PublishDir)\$(TargetName)$(NativeBinaryExt)$(NativeSymbolExt)" />
98+
<Copy SourceFiles="$(NativeOutputPath)$(TargetName)$(NativeBinaryExt)$(NativeSymbolExt)" DestinationFolder="$(PublishDir)"
99+
Condition="Exists('$(NativeOutputPath)$(TargetName)$(NativeBinaryExt)$(NativeSymbolExt)')" />
98100
</Target>
99101

100102
</Project>

src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Unix.props

+41-3
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ The .NET Foundation licenses this file to you under the MIT license.
1414
-->
1515
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
1616
<PropertyGroup>
17-
<CppCompilerAndLinker Condition="'$(CppCompilerAndLinker)' == ''">clang</CppCompilerAndLinker>
17+
<CustomToolchainSpecified Condition="'$(CppCompilerAndLinker)' != ''">true</CustomToolchainSpecified>
18+
<CppCompilerAndLinker Condition="'$(CustomToolchainSpecified)' != 'true'">clang</CppCompilerAndLinker>
19+
<CppCompilerAndLinkerAlternative Condition="'$(CustomToolchainSpecified)' != 'true' and '$(TargetOS)' != 'OSX'">gcc</CppCompilerAndLinkerAlternative>
1820
<CppCompiler>$(CppCompilerAndLinker)</CppCompiler>
1921
<CppLinker>$(CppCompilerAndLinker)</CppLinker>
2022
<CppLibCreator>ar</CppLibCreator>
23+
<DsymUtilOptions Condition="'$(TargetOS)' == 'OSX'">--flat</DsymUtilOptions>
2124
</PropertyGroup>
2225

2326
<Target Name="SetupOSSpecificProps" DependsOnTargets="$(IlcDynamicBuildPropertyDependencies)">
@@ -106,7 +109,42 @@ The .NET Foundation licenses this file to you under the MIT license.
106109
<Exec Command="command -v $(CppLinker)" IgnoreExitCode="true" StandardOutputImportance="Low">
107110
<Output TaskParameter="ExitCode" PropertyName="_WhereLinker" />
108111
</Exec>
109-
<Error Condition="'$(_WhereLinker)' != '0' and '$(TargetOS)' == 'OSX'" Text="Platform linker ('$(CppLinker)') not found. Try installing Xcode to resolve the problem." />
110-
<Error Condition="'$(_WhereLinker)' != '0' and '$(TargetOS)' != 'OSX'" Text="Platform linker ('$(CppLinker)') not found. Try installing $(CppLinker) or the appropriate package for your platform to resolve the problem." />
112+
113+
<Exec Command="command -v $(CppCompilerAndLinkerAlternative)" Condition="'$(CppCompilerAndLinkerAlternative)' != '' and '$(_WhereLinker)' != '0'" IgnoreExitCode="true" StandardOutputImportance="Low">
114+
<Output TaskParameter="ExitCode" PropertyName="_WhereLinkerAlt" />
115+
</Exec>
116+
117+
<PropertyGroup Condition="'$(CppCompilerAndLinkerAlternative)' != '' and '$(_WhereLinker)' != '0' and '$(_WhereLinkerAlt)' == '0'">
118+
<CppCompilerAndLinker>$(CppCompilerAndLinkerAlternative)</CppCompilerAndLinker>
119+
<CppCompiler>$(CppCompilerAndLinker)</CppCompiler>
120+
<CppLinker>$(CppCompilerAndLinker)</CppLinker>
121+
<_WhereLinker>0</_WhereLinker>
122+
</PropertyGroup>
123+
124+
<Error Condition="'$(_WhereLinker)' != '0' and '$(TargetOS)' == 'OSX'" Text="Platform linker ('$(CppLinker)') not found in PATH. Try installing Xcode to resolve the problem." />
125+
<Error Condition="'$(_WhereLinker)' != '0' and '$(CppCompilerAndLinkerAlternative)' != ''"
126+
Text="Platform linker ('$(CppLinker)' or '$(CppCompilerAndLinkerAlternative)') not found in PATH. Try installing appropriate package for $(CppLinker) or $(CppCompilerAndLinkerAlternative) to resolve the problem." />
127+
<Error Condition="'$(_WhereLinker)' != '0' and '$(CppCompilerAndLinkerAlternative)' == '' and '$(TargetOS)' != 'OSX'"
128+
Text="Requested linker ('$(CppLinker)') not found in PATH." />
129+
130+
<Exec Command="command -v objcopy" IgnoreExitCode="true" StandardOutputImportance="Low" Condition="'$(TargetOS)' != 'OSX' and '$(StripSymbols)' == 'true'">
131+
<Output TaskParameter="ExitCode" PropertyName="_WhereSymbolStripper" />
132+
</Exec>
133+
<Error Condition="'$(_WhereSymbolStripper)' != '0' and '$(StripSymbols)' == 'true' and '$(TargetOS)' != 'OSX'"
134+
Text="Symbol stripping tool ('objcopy') not found in PATH. Make sure 'objcopy' is available in PATH" />
135+
136+
<Exec Command="command -v dsymutil &amp;&amp; command -v strip" IgnoreExitCode="true" StandardOutputImportance="Low" Condition="'$(TargetOS)' == 'OSX' and '$(StripSymbols)' == 'true'">
137+
<Output TaskParameter="ExitCode" PropertyName="_WhereSymbolStripper" />
138+
</Exec>
139+
<Error Condition="'$(_WhereSymbolStripper)' != '0' and '$(StripSymbols)' == 'true' and '$(TargetOS)' != 'OSX'"
140+
Text="Symbol stripping tools ('dsymutil' and 'strip') not found in PATH. Make sure 'dsymutil' and 'strip' are available in PATH" />
141+
142+
<Exec Command="dsymutil --help" IgnoreExitCode="true" StandardOutputImportance="Low" Condition="'$(TargetOS)' == 'OSX' and '$(StripSymbols)' == 'true'">
143+
<Output TaskParameter="ExitCode" PropertyName="_DsymUtilOutput" />
144+
</Exec>
145+
146+
<PropertyGroup Condition="'$(TargetOS)' == 'OSX' and '$(StripSymbols)' == 'true' and $(_DsymUtilOutput.Contains('--minimize'))">
147+
<DsymUtilOptions>$(DsymUtilOptions) --minimize</DsymUtilOptions>
148+
</PropertyGroup>
111149
</Target>
112150
</Project>

src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets

+14
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ The .NET Foundation licenses this file to you under the MIT license.
7070
<NativeBinaryExt Condition="'$(IsNativeExecutable)' != 'true' and '$(TargetOS)' == 'windows' and $(NativeLib) == 'Static'">.lib</NativeBinaryExt>
7171
<NativeBinaryExt Condition="'$(IsNativeExecutable)' != 'true' and '$(TargetOS)' != 'windows' and $(NativeLib) == 'Static'">.a</NativeBinaryExt>
7272

73+
<NativeSymbolExt Condition="'$(TargetOS)' == 'OSX'">.dwarf</NativeSymbolExt>
74+
<NativeSymbolExt Condition="'$(TargetOS)' == 'windows'">.pdb</NativeSymbolExt>
75+
<NativeSymbolExt Condition="'$(TargetOS)' != 'OSX' and '$(TargetOS)' != 'windows'">.dbg</NativeSymbolExt>
76+
7377
<ExportsFileExt Condition="'$(TargetOS)' == 'windows'">.def</ExportsFileExt>
7478
<ExportsFileExt Condition="'$(TargetOS)' != 'windows'">.exports</ExportsFileExt>
7579

@@ -341,6 +345,16 @@ The .NET Foundation licenses this file to you under the MIT license.
341345
<Exec Command="$(CppLinker) @&quot;$(NativeIntermediateOutputPath)link.rsp&quot;" Condition="'$(TargetOS)' == 'windows' and '$(NativeLib)' != 'Static'" />
342346
<WriteLinesToFile File="$(NativeIntermediateOutputPath)lib.rsp" Lines="@(CustomLibArg)" Overwrite="true" Encoding="utf-8" Condition="'$(TargetOS)' == 'windows' and '$(NativeLib)' == 'Static'" />
343347
<Exec Command="$(CppLibCreator) @&quot;$(NativeIntermediateOutputPath)lib.rsp&quot;" Condition="'$(TargetOS)' == 'windows' and '$(NativeLib)' == 'Static'" />
348+
349+
<!-- strip symbols, see https://github.com/dotnet/runtime/blob/5d3288d/eng/native/functions.cmake#L374 -->
350+
<Exec Condition="'$(StripSymbols)' == 'true' and '$(TargetOS)' != 'windows' and '$(TargetOS)' != 'OSX'"
351+
Command="
352+
objcopy --only-keep-debug &quot;$(NativeBinary)&quot; &quot;$(NativeBinary)$(NativeSymbolExt)&quot; &amp;&amp;
353+
objcopy --strip-unneeded &quot;$(NativeBinary)&quot; &amp;&amp;
354+
objcopy --add-gnu-debuglink=&quot;$(NativeBinary)$(NativeSymbolExt)&quot; &quot;$(NativeBinary)&quot;" />
355+
356+
<Exec Condition="'$(StripSymbols)' == 'true' and '$(TargetOS)' == 'OSX'" Command="dsymutil $(DsymUtilOptions) &quot;$(NativeBinary)&quot;" />
357+
<Exec Condition="'$(StripSymbols)' == 'true' and '$(TargetOS)' == 'OSX'" Command="strip -no_code_signature_warning -S &quot;$(NativeBinary)&quot;" />
344358
</Target>
345359

346360
<Target Name="CreateLib"

src/coreclr/nativeaot/docs/optimizing.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ To aid in troubleshooting some of the most common problems related to trimming a
4444

4545
## Special considerations for Linux/macOS
4646

47-
Debugging symbols (data about your program required for debugging) is by default part of native executable files on Unix-like operating systems. To minimize the size of your native executable, you can run the `strip` tool to remove the debugging symbols.
47+
Debugging symbols (data about your program required for debugging) is by default part of native executable files on Unix-like operating systems. To strip symbols into a separate file (`*.dbg` on Linux and `*.dwarf` on macOS), set `<StripSymbols>true</StripSymbols>`.
4848

4949
No action is needed on Windows since the platform convention is to generate debug information into a separate file (`*.pdb`).

0 commit comments

Comments
 (0)