- Have you ever looked at the assembly generated by C++ for initializing arrays?
- Where do the values come from?
-
C++ array code ints vs. arrays
-
How variables being initialized in assembly?
- Read assembly code for int init
- Compare/contrast with int-array init
- Explain how data is set via
%rip,%rax,%eax - Explain why
+7in%rip+7+0x4e(CPU NEEDS to read current 7 bytes of instructions BEFORE doing math)
-
Where are the values in
%ripcoming from (Why is it outside of program memory?) -
Learn about
objdump -d -s main-
-dshows__TEXT,__textaka assembly code -
-sshows__TEXT,__constaka constant strings, array values, global vars - Show array values stored in
Contents of section __TEXT,__const
-
-
Who came up with these seemingly complex rules?
- Defined by hardware architects (CPU designers), OS designers (MacOS, Windows, etc.), compiler devs
- Rules are enforced by compilers (Clang, GCC, etc.), OS loaders (kernel), and other systems
-
Why did they do it? (Thanks ChatGPT)
- Security - If CPU tries to write to
\_\_TEXT,\_\_constaddress, OS will segfault because it is marked as Read-Only - Speed (cache locality) - Data is stored physically right next to the code; when CPU loads main, it often grabs
\_\_constdata - Efficiency (shared memory) - The OS loads 1 copy of the
\_\_TEXTsegment into physical RAM, even if you run 10 instances at the same time
- Security - If CPU tries to write to
# curr addr + instruction len + assembly value
# 0x100000f3d + 7 + 0x4c
# find memory address of location
p/x (0x100000f3d+0x4c+7)
# (long) 0x0000000100000f90
# Printing values inside rax
(lldb) p/x $rax
# (unsigned long) 0x0000000b00000005 -> 11, 5
(lldb) p/d ((int*)&$rax)[0]
# (int) 5
(lldb) p/d ((int*)&$rax)[1]
# (int) 11
(lldb) register read $rax -f int32
# rax = {5 11}
# display 5 values at mem location
x/5xd (0x100000f3d+0x4c+7)
# 0x100000f90: 12
# 0x100000f94: 23
# 0x100000f98: 34
# 0x100000f9c: 45
# 0x100000fa0: 56objdump -s -d main
-
Why is the C++ Array "Hidden" in Assembly?
-
C++ Assembly Deep Dive: Chasing Where the Data Lives
-
Low Level C++: How the Compiler Stores Array Data
-
C++ Array Initialization vs. Integer Initialization in Assembly
-
Where are Global Variables and Constants Stored in C++?
-
How are Global Constants Stored in C++ Binaries?
-
How to use objdump -d -s to Debug C++ Binaries?
- 00:00 todo?
- ChatGPT and Gemini