Skip to content

Commit 052077b

Browse files
authored
Merge pull request #68 from DisDis/aot
Implement AOT
2 parents 94f38d3 + 3089f1a commit 052077b

File tree

2 files changed

+122
-10
lines changed

2 files changed

+122
-10
lines changed

include/aot.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef _AOT_H
2+
#define _AOT_H
3+
4+
#if defined(__cplusplus)
5+
extern "C" {
6+
#endif
7+
/* AOT START*/
8+
void* _kDartIsolateSnapshotData = NULL;
9+
void* _kDartIsolateSnapshotInstructions = NULL;
10+
void* _kDartVmSnapshotData = NULL;
11+
void* _kDartVmSnapshotInstructions = NULL;
12+
13+
long _kDartIsolateSnapshotDataSize = 0;
14+
long _kDartIsolateSnapshotInstructionsSize = 0;
15+
long _kDartVmSnapshotDataSize = 0;
16+
long _kDartVmSnapshotInstructionsSize = 0;
17+
/* AOT END */
18+
#if defined(__cplusplus)
19+
}
20+
#endif
21+
22+
#endif

src/flutter-pi.c

Lines changed: 100 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
#include <plugins/text_input.h>
4141
#include <plugins/raw_keyboard.h>
4242

43+
#include <elf.h>
44+
#include <sys/mman.h>
45+
#include <aot.h>
4346

4447
char* usage ="\
4548
flutter-pi - run flutter apps on your Raspberry Pi.\n\
@@ -61,6 +64,7 @@ OPTIONS:\n\
6164
you use as a parameter so it isn't implicitly expanded\n\
6265
by your shell.\n\
6366
\n\
67+
-a <path libapp.so> Enable AOT\n\
6468
-h Show this help and exit.\n\
6569
\n\
6670
EXAMPLES:\n\
@@ -107,6 +111,9 @@ enum device_orientation orientation;
107111
/// is used to determine if width/height should be swapped when sending a WindowMetrics event to flutter)
108112
int rotation = 0;
109113

114+
/// AOT library path, for example './libapp.so'
115+
char* aot_library = NULL;
116+
110117
struct {
111118
char device[PATH_MAX];
112119
bool has_device;
@@ -1196,6 +1203,64 @@ void destroy_display(void) {
11961203
fprintf(stderr, "Deinitializing display not yet implemented\n");
11971204
}
11981205

1206+
bool init_aot(void){
1207+
int fd = open(aot_library, O_RDONLY);
1208+
if (!fd) {
1209+
printf("Error open '%s'\n", aot_library);
1210+
return false;
1211+
}
1212+
struct stat statbuf;
1213+
fstat(fd, & statbuf);
1214+
char * fbase = mmap(NULL, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
1215+
close(fd);
1216+
1217+
Elf32_Ehdr * ehdr = (Elf32_Ehdr * ) fbase;
1218+
Elf32_Shdr * sects = (Elf32_Shdr * )(fbase + ehdr -> e_shoff);
1219+
int shsize = ehdr -> e_shentsize;
1220+
int shnum = ehdr -> e_shnum;
1221+
int shstrndx = ehdr -> e_shstrndx;
1222+
1223+
Elf32_Shdr * shstrsect = & sects[shstrndx];
1224+
char * shstrtab = fbase + shstrsect -> sh_offset;
1225+
1226+
int i, ii;
1227+
Elf32_Shdr dynamic_str;
1228+
Elf32_Shdr dynamic_sym;
1229+
for (i = 0; i < shnum; i++) {
1230+
if (!strcmp(shstrtab + sects[i].sh_name, ".dynstr")) {
1231+
dynamic_str = sects[i]; // count == sects[i].sh_size
1232+
}else
1233+
if (!strcmp(shstrtab + sects[i].sh_name, ".dynsym")) {
1234+
dynamic_sym = sects[i]; //unsigned long number = dynamic_sym.sh_size / dynamic_sym.sh_entsize;
1235+
}
1236+
}
1237+
1238+
char * d_strs = (char * )(fbase + dynamic_str.sh_offset);
1239+
Elf32_Sym * d_syms = (Elf32_Sym * )(fbase + dynamic_sym.sh_offset);
1240+
unsigned long number = dynamic_sym.sh_size / dynamic_sym.sh_entsize;
1241+
1242+
for (ii = 0; ii < number; ii++) {
1243+
printf("Symbol value:%i, size:%i name:%s\n", d_syms[ii].st_value, d_syms[ii].st_size, d_strs + d_syms[ii].st_name);
1244+
if (!strcmp(d_strs + d_syms[ii].st_name, "_kDartIsolateSnapshotData")) {
1245+
_kDartIsolateSnapshotDataSize = d_syms[ii].st_size;
1246+
}
1247+
else if (!strcmp(d_strs + d_syms[ii].st_name, "_kDartIsolateSnapshotInstructions")) {
1248+
_kDartIsolateSnapshotInstructionsSize = d_syms[ii].st_size;
1249+
}
1250+
else if (!strcmp(d_strs + d_syms[ii].st_name, "_kDartVmSnapshotData")) {
1251+
_kDartVmSnapshotDataSize = d_syms[ii].st_size;
1252+
}
1253+
else if (!strcmp(d_strs + d_syms[ii].st_name, "_kDartVmSnapshotInstructions")) {
1254+
_kDartVmSnapshotInstructionsSize = d_syms[ii].st_size;
1255+
}
1256+
}
1257+
1258+
munmap(fbase,statbuf.st_size);
1259+
printf("Done\n");
1260+
return true;
1261+
}
1262+
1263+
11991264
bool init_application(void) {
12001265
int ok, _errno;
12011266

@@ -1205,6 +1270,27 @@ bool init_application(void) {
12051270
fprintf(stderr, "Could not initialize plugin registry: %s\n", strerror(ok));
12061271
return false;
12071272
}
1273+
1274+
if (aot_library){
1275+
printf("Initializing AOT...\n");
1276+
if (!init_aot()){
1277+
fprintf(stderr, "Could not initialize AOT\n");
1278+
return false;
1279+
}
1280+
void *library_handler;
1281+
1282+
library_handler = dlopen(aot_library,RTLD_LAZY);
1283+
if (!library_handler){
1284+
fprintf(stderr,"dlopen() error: %s\n", dlerror());
1285+
return false;
1286+
};
1287+
_kDartIsolateSnapshotData = dlsym(library_handler,"_kDartIsolateSnapshotData");
1288+
_kDartIsolateSnapshotInstructions = dlsym(library_handler,"_kDartIsolateSnapshotInstructions");
1289+
_kDartVmSnapshotData = dlsym(library_handler,"_kDartVmSnapshotData");
1290+
_kDartVmSnapshotInstructions = dlsym(library_handler,"_kDartVmSnapshotInstructions");
1291+
} else {
1292+
printf("AOT skipped\n");
1293+
}
12081294

12091295
// configure flutter rendering
12101296
flutter.renderer_config.type = kOpenGL;
@@ -1220,14 +1306,14 @@ bool init_application(void) {
12201306
flutter.args.struct_size = sizeof(FlutterProjectArgs);
12211307
flutter.args.assets_path = flutter.asset_bundle_path;
12221308
flutter.args.icu_data_path = flutter.icu_data_path;
1223-
flutter.args.isolate_snapshot_data_size = 0;
1224-
flutter.args.isolate_snapshot_data = NULL;
1225-
flutter.args.isolate_snapshot_instructions_size = 0;
1226-
flutter.args.isolate_snapshot_instructions = NULL;
1227-
flutter.args.vm_snapshot_data_size = 0;
1228-
flutter.args.vm_snapshot_data = NULL;
1229-
flutter.args.vm_snapshot_instructions_size = 0;
1230-
flutter.args.vm_snapshot_instructions = NULL;
1309+
flutter.args.isolate_snapshot_data_size = _kDartIsolateSnapshotDataSize;
1310+
flutter.args.isolate_snapshot_data = _kDartIsolateSnapshotData;
1311+
flutter.args.isolate_snapshot_instructions_size = _kDartIsolateSnapshotInstructionsSize;
1312+
flutter.args.isolate_snapshot_instructions = _kDartIsolateSnapshotInstructions;
1313+
flutter.args.vm_snapshot_data_size = _kDartVmSnapshotDataSize;
1314+
flutter.args.vm_snapshot_data = _kDartVmSnapshotData;
1315+
flutter.args.vm_snapshot_instructions_size = _kDartVmSnapshotInstructionsSize;
1316+
flutter.args.vm_snapshot_instructions = _kDartVmSnapshotInstructions;
12311317
flutter.args.command_line_argc = flutter.engine_argc;
12321318
flutter.args.command_line_argv = flutter.engine_argv;
12331319
flutter.args.platform_message_callback = on_platform_message;
@@ -1770,14 +1856,18 @@ bool parse_cmd_args(int argc, char **argv) {
17701856
int ok, opt, index = 0;
17711857
input_devices_glob = (glob_t) {0};
17721858

1773-
while ((opt = getopt(argc, (char *const *) argv, "+i:h")) != -1) {
1859+
while ((opt = getopt(argc, (char *const *) argv, "+i:a:h")) != -1) {
17741860
index++;
17751861
switch(opt) {
17761862
case 'i':
17771863
input_specified = true;
17781864
glob(optarg, GLOB_BRACE | GLOB_TILDE | (input_specified ? GLOB_APPEND : 0), NULL, &input_devices_glob);
17791865
index++;
17801866
break;
1867+
case 'a':
1868+
aot_library = optarg;
1869+
index++;
1870+
break;
17811871
case 'h':
17821872
default:
17831873
printf("%s", usage);
@@ -1848,4 +1938,4 @@ int main(int argc, char **argv) {
18481938
destroy_display();
18491939

18501940
return EXIT_SUCCESS;
1851-
}
1941+
}

0 commit comments

Comments
 (0)