Skip to content

Commit

Permalink
[native_toolchain_c] Only use nm to inspect symbols (#1473)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcharkes authored Aug 28, 2024
1 parent eec08d8 commit 87e8f92
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
14 changes: 7 additions & 7 deletions pkgs/native_toolchain_c/test/clinker/objects_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ Future<void> main() async {
expect(linkOutput.assets, hasLength(1));
final asset = linkOutput.assets.first;
expect(asset, isA<NativeCodeAsset>());
final file = (asset as NativeCodeAsset).file;
expect(file, isNotNull, reason: 'Asset $asset has a file');
final filePath = file!.toFilePath();
expect(filePath, endsWith(os.dylibFileName(name)));
final readelf = await readelfSymbols(filePath);
expect(readelf, contains('my_other_func'));
expect(readelf, contains('my_func'));
await expectSymbols(
asset: asset as NativeCodeAsset,
symbols: [
'my_func',
'my_other_func',
],
);
});
}
8 changes: 5 additions & 3 deletions pkgs/native_toolchain_c/test/clinker/treeshake_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,16 @@ Future<void> runTests(List<Architecture> architectures) async {
output: linkOutput,
logger: logger,
);
final filePath = linkOutput.assets.first.file!.toFilePath();

final asset = linkOutput.assets.first as NativeCodeAsset;
final filePath = asset.file!.toFilePath();

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

final symbols = await readelfSymbols(filePath);
final symbols = await nmReadSymbols(asset);
if (clinker.linker != linkerAutoEmpty) {
expect(symbols, matches(r'[0-9]+\smy_other_func'));
expect(symbols, contains('my_other_func'));
expect(symbols, isNot(contains('my_func')));
} else {
expect(symbols, contains('my_other_func'));
Expand Down
34 changes: 31 additions & 3 deletions pkgs/native_toolchain_c/test/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,6 @@ extension UnescapePath on String {
String unescape() => replaceAll('\\', '/');
}

Future<String> readelfSymbols(String filePath) async =>
readelf(filePath, 'WCs');

Future<String> readelfMachine(String path) async {
final result = await readelf(path, 'h');
return result.split('\n').firstWhere((e) => e.contains('Machine:'));
Expand All @@ -223,3 +220,34 @@ Future<String> readelf(String filePath, String flags) async {
expect(result.exitCode, 0);
return result.stdout;
}

Future<String> nmReadSymbols(NativeCodeAsset asset) async {
final assetUri = asset.file!;
final result = await runProcess(
executable: Uri(path: 'nm'),
arguments: [
'-D',
assetUri.toFilePath(),
],
logger: logger,
);

expect(result.exitCode, 0);
return result.stdout;
}

Future<void> expectSymbols({
required NativeCodeAsset asset,
required List<String> symbols,
}) async {
if (Platform.isLinux) {
final nmOutput = await nmReadSymbols(asset);

expect(
nmOutput,
stringContainsInOrder(symbols),
);
} else {
throw UnimplementedError();
}
}

0 comments on commit 87e8f92

Please sign in to comment.