3

I am looking to better understand assembly instructions pertaining to C++. I have written a simple .cc file to try to reverse engineer it, but I am having trouble understanding what is going on. Ultimately, I want to gain more insight into what is executed before main is called in the realm of global variables.

How are y1 and y2 variables initialized? What is the assembly doing?

Here's the code:

#include <iostream>
#include <array>

struct y {int i; int j;};

const y y1{7,2}, y2{6,4};

int k = 9;

int jy = k;

int main() {}

Here's the generated disassembly from objdump -D:

00000000004007e4 <_ZL2y1>:
  4007e4:   07                      (bad)
  4007e5:   00 00                   add    %al,(%rax)
  4007e7:   00 02                   add    %al,(%rdx)
  4007e9:   00 00                   add    %al,(%rax)
    ...

00000000004007ec <_ZL2y2>:
  4007ec:   06                      (bad)
  4007ed:   00 00                   add    %al,(%rax)
  4007ef:   00 04 00                add    %al,(%rax,%rax,1)
    ...
3
  • This isn't code and it doesn't make sense to disassemble it. It's just the bytes 07 00 00 00 02 00 00 00 in memory, which are the two little-endian ints 7 and 2 that are the two members of y1. Commented Dec 29, 2020 at 21:56
  • 2
    Looks like you are disasembling the data section of the application. Those are not instructions that is raw data. Commented Dec 29, 2020 at 22:13
  • I was using objdump -D <binary>. It greatly confused me until commenter below clarified. Commented Dec 29, 2020 at 22:21

1 Answer 1

5

The variables are initialized by static initialization, meaning before any code (necessarily) executes. The implementation accomplishes this by storing the memory image in the compiled binary.

Look at the hexadecimal values: they match the numbers you assigned in the initializations. Those aren't instructions at all. The disassembler just printed add out of ignorance.

Sign up to request clarification or add additional context in comments.

6 Comments

The disassembler just printed add out of ignorance. cracked me up haha
On the topic of static initialization, the jy variable in my example is NOT statically initialized? Because it depends on the k variable? (I see assembly instructions initializing it before main and it is located in the .bss section). Correct me if I am wrong.
Good question! No, it's not, but the compiler is anyway free to optimize away those instructions as if it were.
It's not exactly ignorance. Those are the instructions the CPU would execute if you used mprotect to make that page executable and jumped there. Like you would for testing shellcode; objdump -D is only useful when you do care about interpreting data in other sections as machine code. (Or as a quick hack if you want to look at the hexdump part and ignore the disassembly).
@PeterCordes aha, I didn't notice that -D forces disassembly for all sections whereas -d is the more common option.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.