@@ -77,7 +77,6 @@ use value::Value;
77
77
use rustc:: util:: nodemap:: { NodeSet , FxHashMap , FxHashSet , DefIdSet } ;
78
78
use CrateInfo ;
79
79
80
- use libc:: c_uint;
81
80
use std:: any:: Any ;
82
81
use std:: cell:: RefCell ;
83
82
use std:: ffi:: { CStr , CString } ;
@@ -692,7 +691,8 @@ fn maybe_create_entry_wrapper(ccx: &CrateContext) {
692
691
sp : Span ,
693
692
rust_main : ValueRef ,
694
693
use_start_lang_item : bool ) {
695
- let llfty = Type :: func ( & [ ccx. isize_ty ( ) , Type :: i8p ( ccx) . ptr_to ( ) ] , & ccx. isize_ty ( ) ) ;
694
+ // Signature of native main(), corresponding to C's `int main(int, char **)`
695
+ let llfty = Type :: func ( & [ Type :: c_int ( ccx) , Type :: i8p ( ccx) . ptr_to ( ) ] , & Type :: c_int ( ccx) ) ;
696
696
697
697
if declare:: get_defined_value ( ccx, "main" ) . is_some ( ) {
698
698
// FIXME: We should be smart and show a better diagnostic here.
@@ -711,19 +711,27 @@ fn maybe_create_entry_wrapper(ccx: &CrateContext) {
711
711
712
712
debuginfo:: gdb:: insert_reference_to_gdb_debug_scripts_section_global ( ccx, & bld) ;
713
713
714
+ // Params from native main() used as args for rust start function
715
+ let param_argc = get_param ( llfn, 0 ) ;
716
+ let param_argv = get_param ( llfn, 1 ) ;
717
+ let arg_argc = bld. intcast ( param_argc, ccx. isize_ty ( ) , true ) ;
718
+ let arg_argv = param_argv;
719
+
714
720
let ( start_fn, args) = if use_start_lang_item {
715
721
let start_def_id = ccx. tcx ( ) . require_lang_item ( StartFnLangItem ) ;
716
722
let start_instance = Instance :: mono ( ccx. tcx ( ) , start_def_id) ;
717
723
let start_fn = callee:: get_fn ( ccx, start_instance) ;
718
- ( start_fn, vec ! [ bld. pointercast( rust_main, Type :: i8p( ccx) . ptr_to( ) ) , get_param ( llfn , 0 ) ,
719
- get_param ( llfn , 1 ) ] )
724
+ ( start_fn, vec ! [ bld. pointercast( rust_main, Type :: i8p( ccx) . ptr_to( ) ) ,
725
+ arg_argc , arg_argv ] )
720
726
} else {
721
727
debug ! ( "using user-defined start fn" ) ;
722
- ( rust_main, vec ! [ get_param ( llfn , 0 as c_uint ) , get_param ( llfn , 1 as c_uint ) ] )
728
+ ( rust_main, vec ! [ arg_argc , arg_argv ] )
723
729
} ;
724
730
725
731
let result = bld. call ( start_fn, & args, None ) ;
726
- bld. ret ( result) ;
732
+
733
+ // Return rust start function's result from native main()
734
+ bld. ret ( bld. intcast ( result, Type :: c_int ( ccx) , true ) ) ;
727
735
}
728
736
}
729
737
0 commit comments