X Tutup
Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

16. Array Data Initialization

TL;DR

  • Have you ever looked at the assembly generated by C++ for initializing arrays?
  • Where do the values come from?

Video Overview

  • 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 +7 in %rip+7+0x4e (CPU NEEDS to read current 7 bytes of instructions BEFORE doing math)
  • Where are the values in %rip coming from (Why is it outside of program memory?)

  • Learn about objdump -d -s main

    • -d shows __TEXT,__text aka assembly code
    • -s shows __TEXT,__const aka 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,\_\_const address, 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 \_\_const data
    • Efficiency (shared memory) - The OS loads 1 copy of the \_\_TEXT segment into physical RAM, even if you run 10 instances at the same time

Helpful Commands

LLDB

# 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: 56

Bash

objdump -s -d main

Titles

  • 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?

Timestamps

  • 00:00 todo?

References

  • ChatGPT and Gemini
X Tutup