diff --git a/scripts/build.pl b/scripts/build.pl index a89c2a9..441ea54 100755 --- a/scripts/build.pl +++ b/scripts/build.pl @@ -219,7 +219,6 @@ if ( grep { $_ eq "fail" } @selected_targets ) { # Build everything # ========================================================================================= # -# TODO: linux-baremetal target is broken system( "mkdir", "-p", "$local_builds_dir" ); foreach my $experiment (@selected_experiments) { foreach my $target (@selected_targets) { diff --git a/scripts/compile.pl b/scripts/compile.pl index 6681e2b..304c2b8 100644 --- a/scripts/compile.pl +++ b/scripts/compile.pl @@ -123,8 +123,7 @@ my @cross_ldflags_base = ( my @linux_ldflags_base = ( '-Wl,--build-id=none', '-m32', '-lm' ); my @baremetal_ldflags_base = ( '-Wl,--build-id=none', '-static', '-nostdlib', '-m32', - '-lc', '-lgcc', '-lm', '--entry', - 'main' + '-lc', '-lgcc', '-lm' ); my @cross_ldflags_nowasm = ( '-Wl,--build-id=none', '-static', '-nostdlib', '-m32', @@ -199,7 +198,8 @@ sub build { die "Unknown mode '$mode'; expected: aot, interp, c\n"; } - build_iso( $module, $bd ); + build_iso( $module, $bd ) + if $target eq 'fail' || $target eq 'linux-baremetal'; } # ========================================================================================= # @@ -373,8 +373,9 @@ sub compile_wasm_host { sub compile_c_module { my ( $module, $bd, $target ) = @_; my ( $cc, @flags ) = - ( $target eq 'linux' ) - ? ( $linux_cc, @linux_cflags, '-DTARGET_LINUX' ) + ( $target eq 'linux' ) ? ( $linux_cc, @linux_cflags, '-DTARGET_LINUX' ) + : ( $target eq 'linux-baremetal' ) + ? ( $cross_cc, @linux_baremetal_cflags, '-DTARGET_LINUX_BAREMETAL' ) : ( $cross_cc, @cross_cflags, '-DTARGET_FAIL' ); if ( Util::module_is_tacle($module) ) { @@ -404,8 +405,9 @@ sub compile_c_module { sub compile_c_host { my ( $module, $bd, $target ) = @_; my ( $cc, @flags ) = - ( $target eq 'linux' ) - ? ( $linux_cc, @linux_cflags, '-DTARGET_LINUX' ) + ( $target eq 'linux' ) ? ( $linux_cc, @linux_cflags, '-DTARGET_LINUX' ) + : ( $target eq 'linux-baremetal' ) + ? ( $cross_cc, @linux_baremetal_cflags, '-DTARGET_LINUX_BAREMETAL' ) : ( $cross_cc, @cross_cflags, '-DTARGET_FAIL' ); Util::run( $cc, @flags, '-c', 'targets/c-host/c_host.c', '-o', "$bd/c_host.o" ); @@ -414,7 +416,7 @@ sub compile_c_host { sub compile_startup { my ( $module, $bd, $target ) = @_; - return unless $target eq 'fail'; + return unless $target eq 'fail' || $target eq 'linux-baremetal'; Util::run( $cross_cc, 'targets/startup.s', '-I./targets/wasm-host', @cross_cflags, '-c', '-o', "$bd/startup.o" ); } @@ -450,8 +452,13 @@ sub link_wasm { @linux_ldflags_base, '-o', "$bd/system.elf" ); } elsif ( $target eq 'linux-baremetal' ) { - Util::run( $cross_cc, "$bd/system.o", "$bd/syscalls.o", - "$bd/libiwasm.a", @baremetal_ldflags_base, '-o', "$bd/system.elf" ); + Util::run( + $cross_cc, '-Wl,-T', + 'targets/linker.ld', "$bd/system.o", + "$bd/startup.o", "$bd/syscalls.o", + "$bd/libiwasm.a", @baremetal_ldflags_base, + '-o', "$bd/system.elf" + ); } else { die "Unknown target '$target'\n"; @@ -476,6 +483,16 @@ sub link_c { Util::run( $linux_cc, "$bd/c_host.o", "$bd/c_module.o", @tacle_objs, @linux_ldflags_nowasm, '-o', "$bd/system.elf" ); } + elsif ( $target eq 'linux-baremetal' ) { + Util::run( + $cross_cc, '-Wl,-T', + 'targets/linker.ld', "$bd/c_host.o", + "$bd/startup.o", "$bd/syscalls.o", + "$bd/c_module.o", @tacle_objs, + @baremetal_ldflags_base, '-o', + "$bd/system.elf" + ); + } else { die "C mode is not supported for target '$target'\n"; } diff --git a/targets/lib.h b/targets/lib.h index 5835ae7..208a9a2 100644 --- a/targets/lib.h +++ b/targets/lib.h @@ -31,11 +31,11 @@ #endif #ifdef TARGET_LINUX_BAREMETAL -#define MAIN int main(int argc, char *argv[]) +#define MAIN void os_main(void) #define PRINT(fmt, ...) #define PRINT_ERROR(fmt, ...) #define PRINT_SUCCESS(fmt, ...) -#define RET(val) return val +#define RET(val) return #endif #ifdef TARGET_LINUX