Apr 14, 2014

Shared libraries and dynamic linker

cc -g -c prog.c mod1.c mod2.c mod3.c
cc -g -o prog_nolib prog.o mod1.o mod2.o mod3.o

strip(1) - discard symbols from object files.

Static lib:
ar r libdemo.a mod1.o mod2.o mod3.o

ar tv libdemo.a
# rw-rw-r-- 1000/1000 1003352 Apr 14 11:24 2014 mod1.o
# rw-rw-r-- 1000/1000 403352 Apr 14 11:24 2014 mod2.o
# rw-rw-r-- 1000/1000  43352 Apr 14 11:24 2014 mod3.o

cc -g -o prog prog.o libdemo.a
# or
cc -g -o prog prog.o -Lmylibdir -ldemo

./prog

Dinamic lib:
gcc -g -c -fPIC -Wall mod1.c mod2.c mod3.c
gcc -g -shared -o libfoo.so mod1.o mod2.o mod3.o
# or 
gcc -g -fPIC -Wall mod1.c mod2.c mod3.c -shared -o libfoo.so

gcc -g -Wall -o prog prog.c libfoo.so
LD_LIBRARY_PATH=. ./prog

nm(1) - list symbols from object files
readelf(1) - displays information about ELF files
objdump(1) - display information from object files
ldd(1) - print shared library dependencies
ldconfig(1) - configure dynamic linker run-time bindings
nm mod1.o
readelf -s mod1.o

objdump --all-headers libfoo.so
readelf -d libfoo.so

Process shared libs:
cat /proc/PID/maps

Shared libs that prog required:
ldd /usr/sbin/tcpdump
# linux-vdso.so.1 =>  (0x00007fffe17ff000)
# libcrypto.so.1.0.0 => /lib/x86_64-linux-gnu/libcrypto.so.1.0.0 (0x00007ff285828000)
# libpcap.so.0.8 => /usr/lib/x86_64-linux-gnu/libpcap.so.0.8 (0x00007ff2855ed000)
# libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff285224000)
# libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007ff285020000)
# libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007ff284e07000)
# /lib64/ld-linux-x86-64.so.2 (0x00007ff28606f000)

Where function defined:
nm -A /usr/lib/lib*.so 2> /dev/null | grep ' crypt$'

All system shared libs:
ldconfig –p
#1768 libs found in cache `/etc/ld.so.cache'
# libzvbi.so.0 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libzvbi.so.0
# libzvbi-chains.so.0 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libzvbi-chains.so.0
# libzltext.so.0.13 (libc6,x86-64) => /usr/lib/libzltext.so.0.13
# libzlcore.so.0.13 (libc6,x86-64) => /usr/lib/libzlcore.so.0.13
# libzip.so.2 (libc6,x86-64) => /usr/lib/libzip.so.2
#...

Path: LD_LIBRARY_PATH, DT_RPATH, DT_RUNPATH, $ORIGIN
gcc -Wl,-rpath,'$ORIGIN'/lib ...

Run-time symbol resolution:
gcc -g -c -fPIC -Wall -c foo.c
gcc -g -shared -o libfoo.so foo.o
gcc -g -o prog prog.c libfoo.so
LD_LIBRARY_PATH=. ./prog
# main-xyz

Monitoring dynamic linker:
LD_DEBUG=help ./prog
LD_DEBUG=all ./prog

Mar 26, 2014

Unix as IDE. Memmory profiling

cat /proc/meminfo
#MemTotal:       16350248 kB
#MemFree:        14502016 kB
#Buffers:          133516 kB
#Cached:           738584 kB
#..
#Mapped:           106640 kB
#Shmem:             78664 kB
#Slab:             101588 kB
#SReclaimable:      81208 kB
#SUnreclaim:        20380 kB
#KernelStack:        3000 kB
#PageTables:        18616 kB
#..
cat /proc/iomem 
#00000000-00000fff : reserved
#00001000-0009d7ff : System RAM
#0009d800-0009ffff : reserved
#000a0000-000bffff : PCI Bus 0000:00
#000c0000-000c7fff : Video ROM
#...
cat /proc/pagetypeinfo
#Page block order: 9
#Pages per block:  512
#
# ...
#
#Number of blocks type     Unmovable  Reclaimable      Movable      Reserve      Isolate 
#Node 0, zone      DMA            1            0            6            1            0 
#Node 0, zone    DMA32            8           32         1742            2            0 
#Node 0, zone   Normal          222           94         6069            2            0

cat /proc/buddyinfo 
#Node 0, zone      DMA      0      0      0      0      0      0      0      0      1      1      3 
#Node 0, zone    DMA32   1734   1323    616    162     59     32     10     13      7      3    749 
#Node 0, zone   Normal   2712   3081   1760    654    320    144     51     27     27     12   2728

sudo cat /proc/slabinfo
sudo slabtop
Shared Memory Segments
ipcs -m
#------ Shared Memory Segments --------
#key        shmid      owner      perms      bytes      nattch     status      
#0x00000000 0          alex       600        393216     2          dest         
#0x00000000 360449     alex       600        4194304    2          dest         
#0x00000000 196610     alex       600        2097152    2          dest         
#0x00000000 229379     alex       600        1048576    2          dest
ipcs -m -p
#
#------ Shared Memory Creator/Last-op PIDs --------
#shmid      owner      cpid       lpid      
#0          alex       3613       4418      
#360449     alex       3993       4418      
#196610     alex       3317       4418      
#229379     alex       3613       4418

Mar 23, 2014

Qemu. Mips cpu virtualization

About MIPS architecture

Download initrd.gz and vmlinux-3.2.0-4-4kc-malta from http://ftp.debian.org/debian/dists/stable/main/installer-mips/current/images/malta/netboot/
# install
qemu-system-mips -M malta -kernel vmlinux-3.2.0-4-4kc-malta -initrd initrd.gz \
 -hda debian74_mips.img  -append "root=/dev/sda1 console=ttyS0" -nographic

# boot
qemu-system-mips -M malta -kernel vmlinux-3.2.0-4-4kc-malta \
 -hda debian74_mips.img -append "root=/dev/sda1 console=ttyS0" -nographic

On guest host:
uname -a
# Linux debianmips 3.2.0-4-4kc-malta #1 Debian 3.2.54-2 mips GNU/Linux

cat /proc/cpuinfo 
# system type  : MIPS Malta
# processor  : 0
# cpu model  : MIPS 24Kc V0.0  FPU V0.0
mips hello world (http://en.wikipedia.org/wiki/MIPS_architecture#MIPS_assembly_language):
00400640 <main>:
#include <stdio.h>

int main() {
  400640: 27bdffe0  addiu sp,sp,-32
  400644: afbf001c  sw ra,28(sp)
  400648: afbe0018  sw s8,24(sp)
  40064c: 03a0f021  move s8,sp
    printf("Hello world!\n");
  400650: 3c020040  lui v0,0x40
  400654: 24440810  addiu a0,v0,2064
  400658: 0c100140  jal 400500 <puts@plt>
  40065c: 00200825  move at,at
    return 0;
  400660: 00001021  move v0,zero
}
  400664: 03c0e821  move sp,s8
  400668: 8fbf001c  lw ra,28(sp)
  40066c: 8fbe0018  lw s8,24(sp)
  400670: 27bd0020  addiu sp,sp,32
  400674: 03e00008  jr ra
  400678: 00200825  move at,at
  40067c: 00200825  move at,at
i386 hello world:
0804840c <main>:
#include <stdio.h>

int main() {
 804840c: 55                    push   ebp
 804840d: 89 e5                 mov    ebp,esp
 804840f: 83 e4 f0              and    esp,0xfffffff0
 8048412: 83 ec 10              sub    esp,0x10
    printf("Hello world!\n");
 8048415: c7 04 24 c0 84 04 08  mov    DWORD PTR [esp],0x80484c0
 804841c: e8 cf fe ff ff        call   80482f0 <puts@plt>
    return 0;
 8048421: b8 00 00 00 00        mov    eax,0x0
}
 8048426: c9                    leave  
 8048427: c3                    ret    
 8048428: 90                    nop
 8048429: 90                    nop
 804842a: 90                    nop
 804842b: 90                    nop
 804842c: 90                    nop
 804842d: 90                    nop
 804842e: 90                    nop
 804842f: 90                    nop
x64 hello world:
00000000004004fd <main>:
#include <stdio.h>

int main() {
  4004fd: 55                    push   rbp
  4004fe: 48 89 e5              mov    rbp,rsp
    printf("Hello world!\n");
  400501: bf a4 05 40 00        mov    edi,0x4005a4
  400506: e8 d5 fe ff ff        call   4003e0 <puts@plt>
    return 0;
  40050b: b8 00 00 00 00        mov    eax,0x0
}
  400510: 5d                    pop    rbp
  400511: c3                    ret    
  400512: 66 2e 0f 1f 84 00 00  nop    WORD PTR cs:[rax+rax*1+0x0]
  400519: 00 00 00 
  40051c: 0f 1f 40 00           nop    DWORD PTR [rax+0x0]

Instructions for other archs with qemu:
https://gmplib.org/~tege/qemu.html

Mar 18, 2014

Terminal


Oblivion
text color #D3D7CF
background color #2E3436

Inconsolata Medium 13

Tango
gconftool --set --type string /apps/gnome-terminal/profiles/Default/palette \ 
'#3b3b3b:#cf6a4c:#99ad6a:#d8ad4c:#597bc5:#a037b0:#71b9f8:#adadad:#636363:#f79274:#c1d592:#ffd574:#81a3ed:#c85fd8:#99e1ff:#d5d5d5'