05. C: Addressing and address arithmetic
(NB: Sugon IP is changed)
Previous lecture debt: calling a linker.
Strace — tool for system call tracing
-s size: parameter size limit
-f: follow subsequent process
-e syscall: limit to this syscall/group of syscalls
So try a little cc o.c with strace -f -eexecve -s1024 cc o.c
OMFG!
cc1 — C → Asm translator (aka cc -S)
cc -S o.c -o o.s
as — Asm → object compiler
-mips32r2 — exact architecture (ISA+ABI)
as -mips32r2 o.s -o o.o
ld — linker
- a lot of supplemental files + libc:
ld \ -dynamic-linker /lib/ld.so.1 \ /usr/lib/crt1.o \ /usr/lib/crti.o \ /usr/lib/gcc/mipsel-alt-linux/8/crtbegin.o \ /usr/lib/gcc/mipsel-alt-linux/8/crtend.o \ /usr/lib/crtn.o \ -lc \ o.o -o prog
- a lot of supplemental files + libc:
Addressing
#include <stdio.h> int N; char *Str = "The string"; char Carr[]= "Other string"; int Array[] = {1,3,5,7,9,12,12,34,45,4556,3,23,23}; int main(int argc, char *argv[]) { int L; int * A; N = L = 1; A = &L; *A = 7; *Array = 100; Array[2] = 5; *(Array+3) = 6; 4[Array] = 10500; printf("%p %p %p %p\n", &N, &L, Str, Array); return 0; }
(use cc -O0 -static -mno-explicit-relocs -mno-shared -mno-abicalls -mno-gpopt -g -Wa,-adhlns file.c or compile script)
Pointer type in C is just an address
- Array name in C is just a lable!
Pointer type variable holds an address
- Pointer is aligned on data size:
#include <stdio.h> struct STRU { int a, b; double f; }; struct STRU Array[10]; void main(void) { int i=3, j=4; printf("%d, %lf\n", Array[i].a, Array[j].f); }
TODO
Structs
Alignment
Arrays on stack
Function address