@@ -3,12 +3,13 @@ mod def_visitor;
33use std:: io:: Write ;
44use std:: process:: Stdio ;
55
6+ use object:: { Object , ObjectSection , ObjectSymbol } ;
67use syn:: visit:: Visit ;
78use syn:: Ident ;
89
910use crate :: def_visitor:: { DefVisitor , LlvmIntrinsicDef } ;
1011
11- fn main ( ) {
12+ fn compile_object ( ) {
1213 println ! ( "Running rustc -Zunpretty=expanded --edition=2021 core_arch/src/lib.rs ..." ) ;
1314 let expanded_file = std:: process:: Command :: new ( "rustc" )
1415 . arg ( "-Zunpretty=expanded" )
@@ -89,12 +90,13 @@ fn main() {
8990 println ! ( "Compiling blob" ) ;
9091 let mut child = std:: process:: Command :: new ( "rustc" )
9192 . arg ( "-Copt-level=3" )
93+ . arg ( "-Cpanic=abort" )
9294 . arg ( "--crate-type" )
9395 . arg ( "cdylib" )
9496 . arg ( "--emit=obj" )
9597 //.arg("--target=x86_64-unknown-linux-gnu")
96- . arg ( "--out-dir " )
97- . arg ( "target" )
98+ . arg ( "-o " )
99+ . arg ( "target/rust_out.o " )
98100 . arg ( "-" )
99101 . stdin ( Stdio :: piped ( ) )
100102 . spawn ( )
@@ -105,3 +107,46 @@ fn main() {
105107 let status = child. wait ( ) . unwrap ( ) ;
106108 assert ! ( status. success( ) , "{status}" ) ;
107109}
110+
111+ fn main ( ) {
112+ if
113+ /*true || // */
114+ false {
115+ compile_object ( ) ;
116+ }
117+
118+ let obj = std:: fs:: read ( "target/rust_out.o" ) . unwrap ( ) ;
119+ let obj = object:: File :: parse ( & * obj) . unwrap ( ) ;
120+
121+ let imports = obj. symbols ( ) . filter ( |sym| sym. is_undefined ( ) ) . collect :: < Vec < _ > > ( ) ;
122+ assert ! ( imports. is_empty( ) , "{imports:?}" ) ;
123+
124+ for section in obj. sections ( ) {
125+ let section_name = section. name ( ) . unwrap ( ) ;
126+ if !section_name. starts_with ( ".text" ) {
127+ continue ;
128+ }
129+
130+ if section_name == ".text" {
131+ assert_eq ! ( section. size( ) , 0 ) ;
132+ continue ;
133+ }
134+
135+ let name = section_name. strip_prefix ( ".text.__rust_cranelift_" ) . unwrap ( ) . replace ( "__" , "." ) ;
136+
137+ // Sanity checks
138+ assert ! ( section. relocations( ) . next( ) . is_none( ) , "function {name} has relocations" ) ;
139+ assert ! (
140+ section. size( ) <= 0x14 ,
141+ "function {name} is too big. it is {} bytes" ,
142+ section. size( ) ,
143+ ) ;
144+
145+ let data = section. data ( ) . unwrap ( ) ;
146+ let ( code, ret) = data. split_at ( data. len ( ) - 4 ) ;
147+ assert_eq ! ( ret, [ 0xc0_u8 , 0x03 , 0x5f , 0xd6 ] ) ; // arm64 ret instruction
148+ println ! ( " \" {name}\" => {{" ) ;
149+ println ! ( " {:x?}" , code) ;
150+ println ! ( " }}" ) ;
151+ }
152+ }
0 commit comments