Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why the assembly output of a compiled rust code does not correspond to the rust code itself?

I wrote this snippet in the compiler explorer:


fn foo() -> u8 {
    54
}

fn bar() -> u128 {
    3423
}


fn main() {

    let a: (u8, u128) = (foo(), bar());

    println!("foo result: {}", a.0);
    println!("bar result: {}", a.1);

}

The assembly version is this:

        .text
        .file   "example.db4da305-cgu.0"
        .type   __rustc_debug_gdb_scripts_section__,@object
        .section        .debug_gdb_scripts,"aMS",@progbits,1,unique,1
        .weak   __rustc_debug_gdb_scripts_section__
__rustc_debug_gdb_scripts_section__:
        .asciz  "\001gdb_load_rust_pretty_printers.py"
        .size   __rustc_debug_gdb_scripts_section__, 34

        .section        .debug_aranges,"",@progbits
        .section        ".note.GNU-stack","",@progbits

I’m not super familiar with assembly but my little knowledge of it tells me that this code must have different assembly output. Am I correct? Am I using compiler explorer in a wrong way? (checking a flag or something)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is link of my snippet.

>Solution :

You are compiling a library by default. And since your library does not have any public symbol, it does nothing and your asm is empty.

The solution, either:

  • Declare your library main as public: pub fn main()
  • Compile a binary:

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading