Skip to content

Commit 87e8f92

Browse files
authored
[native_toolchain_c] Only use nm to inspect symbols (#1473)
1 parent eec08d8 commit 87e8f92

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

pkgs/native_toolchain_c/test/clinker/objects_test.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ Future<void> main() async {
5858
expect(linkOutput.assets, hasLength(1));
5959
final asset = linkOutput.assets.first;
6060
expect(asset, isA<NativeCodeAsset>());
61-
final file = (asset as NativeCodeAsset).file;
62-
expect(file, isNotNull, reason: 'Asset $asset has a file');
63-
final filePath = file!.toFilePath();
64-
expect(filePath, endsWith(os.dylibFileName(name)));
65-
final readelf = await readelfSymbols(filePath);
66-
expect(readelf, contains('my_other_func'));
67-
expect(readelf, contains('my_func'));
61+
await expectSymbols(
62+
asset: asset as NativeCodeAsset,
63+
symbols: [
64+
'my_func',
65+
'my_other_func',
66+
],
67+
);
6868
});
6969
}

pkgs/native_toolchain_c/test/clinker/treeshake_helper.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,16 @@ Future<void> runTests(List<Architecture> architectures) async {
7676
output: linkOutput,
7777
logger: logger,
7878
);
79-
final filePath = linkOutput.assets.first.file!.toFilePath();
79+
80+
final asset = linkOutput.assets.first as NativeCodeAsset;
81+
final filePath = asset.file!.toFilePath();
8082

8183
final machine = await readelfMachine(filePath);
8284
expect(machine, contains(readElfMachine[architecture]));
8385

84-
final symbols = await readelfSymbols(filePath);
86+
final symbols = await nmReadSymbols(asset);
8587
if (clinker.linker != linkerAutoEmpty) {
86-
expect(symbols, matches(r'[0-9]+\smy_other_func'));
88+
expect(symbols, contains('my_other_func'));
8789
expect(symbols, isNot(contains('my_func')));
8890
} else {
8991
expect(symbols, contains('my_other_func'));

pkgs/native_toolchain_c/test/helpers.dart

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,6 @@ extension UnescapePath on String {
197197
String unescape() => replaceAll('\\', '/');
198198
}
199199

200-
Future<String> readelfSymbols(String filePath) async =>
201-
readelf(filePath, 'WCs');
202-
203200
Future<String> readelfMachine(String path) async {
204201
final result = await readelf(path, 'h');
205202
return result.split('\n').firstWhere((e) => e.contains('Machine:'));
@@ -223,3 +220,34 @@ Future<String> readelf(String filePath, String flags) async {
223220
expect(result.exitCode, 0);
224221
return result.stdout;
225222
}
223+
224+
Future<String> nmReadSymbols(NativeCodeAsset asset) async {
225+
final assetUri = asset.file!;
226+
final result = await runProcess(
227+
executable: Uri(path: 'nm'),
228+
arguments: [
229+
'-D',
230+
assetUri.toFilePath(),
231+
],
232+
logger: logger,
233+
);
234+
235+
expect(result.exitCode, 0);
236+
return result.stdout;
237+
}
238+
239+
Future<void> expectSymbols({
240+
required NativeCodeAsset asset,
241+
required List<String> symbols,
242+
}) async {
243+
if (Platform.isLinux) {
244+
final nmOutput = await nmReadSymbols(asset);
245+
246+
expect(
247+
nmOutput,
248+
stringContainsInOrder(symbols),
249+
);
250+
} else {
251+
throw UnimplementedError();
252+
}
253+
}

0 commit comments

Comments
 (0)