Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[native_toolchain_c] Only use nm to inspect symbols #1473

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still used anywhere or can it be deleted?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's still used, the flags needed for that are available. (Was already used in cbuilder tests before as well.)

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();
}
}