From 15230bbb9b1f69def9b0e1b41a097638c0fda734 Mon Sep 17 00:00:00 2001 From: johannst Date: Tue, 28 Apr 2020 09:11:18 +0000 Subject: deploy: fef4d6ff2ad9f48e6dccde0f061453e6a3ac624e --- awk.html | 334 ---------------- bash.html | 360 ----------------- binary/index.html | 220 +++++++++++ binary/nm.html | 224 +++++++++++ binary/objdump.html | 233 +++++++++++ binary/od.html | 260 +++++++++++++ binary/readelf.html | 230 +++++++++++ binary/xxd.html | 245 ++++++++++++ c++.html | 227 ----------- c++filt.html | 227 ----------- development/c++.html | 227 +++++++++++ development/c++filt.html | 227 +++++++++++ development/gcc.html | 269 +++++++++++++ development/glibc.html | 249 ++++++++++++ development/index.html | 220 +++++++++++ development/ld.so.html | 322 +++++++++++++++ emacs.html | 283 -------------- fish.html | 231 ----------- gcc.html | 261 ------------- gdb.html | 358 ----------------- git.html | 324 ---------------- glibc.html | 249 ------------ index.html | 121 +----- ld.so.html | 322 --------------- lsof.html | 249 ------------ misc/awk.html | 334 ++++++++++++++++ misc/bash.html | 360 +++++++++++++++++ misc/emacs.html | 283 ++++++++++++++ misc/fish.html | 231 +++++++++++ misc/gdb.html | 358 +++++++++++++++++ misc/git.html | 324 ++++++++++++++++ misc/index.html | 211 ++++++++++ misc/radare2.html | 238 ++++++++++++ misc/tmux.html | 316 +++++++++++++++ misc/zsh.html | 320 +++++++++++++++ monitor/index.html | 220 +++++++++++ monitor/lsof.html | 249 ++++++++++++ monitor/pgrep.html | 230 +++++++++++ monitor/pidstat.html | 242 ++++++++++++ monitor/pmap.html | 224 +++++++++++ monitor/pstack.html | 223 +++++++++++ nm.html | 224 ----------- objdump.html | 233 ----------- od.html | 260 ------------- oprofile.html | 231 ----------- perf.html | 265 ------------- pgrep.html | 230 ----------- pidstat.html | 242 ------------ print.html | 927 ++++++++++++++++++++++---------------------- pstack.html | 223 ----------- radare2.html | 238 ------------ readelf.html | 230 ----------- searchindex.js | 2 +- searchindex.json | 2 +- strace.html | 242 ------------ time.html | 223 ----------- tmux.html | 316 --------------- trace_profile/index.html | 220 +++++++++++ trace_profile/ltrace.html | 231 +++++++++++ trace_profile/oprofile.html | 231 +++++++++++ trace_profile/perf.html | 265 +++++++++++++ trace_profile/strace.html | 242 ++++++++++++ trace_profile/time.html | 223 +++++++++++ xxd.html | 245 ------------ zsh.html | 320 --------------- 65 files changed, 9182 insertions(+), 7718 deletions(-) delete mode 100644 awk.html delete mode 100644 bash.html create mode 100644 binary/index.html create mode 100644 binary/nm.html create mode 100644 binary/objdump.html create mode 100644 binary/od.html create mode 100644 binary/readelf.html create mode 100644 binary/xxd.html delete mode 100644 c++.html delete mode 100644 c++filt.html create mode 100644 development/c++.html create mode 100644 development/c++filt.html create mode 100644 development/gcc.html create mode 100644 development/glibc.html create mode 100644 development/index.html create mode 100644 development/ld.so.html delete mode 100644 emacs.html delete mode 100644 fish.html delete mode 100644 gcc.html delete mode 100644 gdb.html delete mode 100644 git.html delete mode 100644 glibc.html delete mode 100644 ld.so.html delete mode 100644 lsof.html create mode 100644 misc/awk.html create mode 100644 misc/bash.html create mode 100644 misc/emacs.html create mode 100644 misc/fish.html create mode 100644 misc/gdb.html create mode 100644 misc/git.html create mode 100644 misc/index.html create mode 100644 misc/radare2.html create mode 100644 misc/tmux.html create mode 100644 misc/zsh.html create mode 100644 monitor/index.html create mode 100644 monitor/lsof.html create mode 100644 monitor/pgrep.html create mode 100644 monitor/pidstat.html create mode 100644 monitor/pmap.html create mode 100644 monitor/pstack.html delete mode 100644 nm.html delete mode 100644 objdump.html delete mode 100644 od.html delete mode 100644 oprofile.html delete mode 100644 perf.html delete mode 100644 pgrep.html delete mode 100644 pidstat.html delete mode 100644 pstack.html delete mode 100644 radare2.html delete mode 100644 readelf.html delete mode 100644 strace.html delete mode 100644 time.html delete mode 100644 tmux.html create mode 100644 trace_profile/index.html create mode 100644 trace_profile/ltrace.html create mode 100644 trace_profile/oprofile.html create mode 100644 trace_profile/perf.html create mode 100644 trace_profile/strace.html create mode 100644 trace_profile/time.html delete mode 100644 xxd.html delete mode 100644 zsh.html diff --git a/awk.html b/awk.html deleted file mode 100644 index d9542a8..0000000 --- a/awk.html +++ /dev/null @@ -1,334 +0,0 @@ - - - - - - awk - Notes - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -
- - - - - - - - - - -
-
-

awk(1)

-
awk [opt] program [input]
-    -F <sepstr>        field separator string (can be regex)
-    program            awk program
-    input              file or stdin if not file given
-
-

Input processing

-

Input is processed in two stages:

-
    -
  1. Splitting input into a sequence of records. -By default split at newline character, but can be changed via the -builtin RS variable.
  2. -
  3. Splitting a record into fields. By default strings without whitespace, -but can be changed via the builtin variable FS or command line option --F.
  4. -
-

Fields are accessed as follows:

-
    -
  • $0 whole record
  • -
  • $1 field one
  • -
  • $2 field two
  • -
  • ...
  • -
-

Program

-

An awk program is composed of pairs of the form:

-
pattern { action }
-
-

The program is run against each record in the input stream. If a pattern -matches a record the corresponding action is executed and can access the -fields.

-
INPUT
-  |
-  v
-record ----> ∀ pattern matched
-  |                   |
-  v                   v
-fields ----> run associated action
-
-

Any valid awk expr can be a pattern.

-

Special pattern

-

awk provides two special patterns, BEGIN and END, which can be used -multiple times. Actions with those patterns are executed exactly once.

-
    -
  • BEGIN actions are run before processing the first record
  • -
  • END actions are run after processing the last record
  • -
-

Special variables

-
    -
  • RS record separator: first char is the record separator, by default -
  • -
  • FS field separator: regex to split records into fields, by default -
  • -
  • NR number record: number of current record
  • -
-

Special statements & functions

-
    -
  • -

    printf "fmt", args...

    -

    Print format string, args are comma separated.

    -
      -
    • %s string
    • -
    • %d decimal
    • -
    • %x hex
    • -
    • %f float
    • -
    -

    Width can be specified as %Ns, this reserves N chars for a string. -For floats one can use %N.Mf, N is the total number including . and -M.

    -
  • -
  • -

    strftime("fmt")

    -

    Print time stamp formatted by fmt.

    -
      -
    • %Y full year (eg 2020)
    • -
    • %m month (01-12)
    • -
    • %d day (01-31)
    • -
    • %F alias for %Y-%m-%d
    • -
    • %H hour (00-23)
    • -
    • %M minute (00-59)
    • -
    • %S second (00-59)
    • -
    • %T alias for %H:%M:%S
    • -
    -
  • -
-

Examples

-

Filter records

-
awk 'NR%2 == 0 { print $0 }' <file>
-
-

The pattern NR%2 == 0 matches every second record and the action { print $0 } -prints the whole record.

-

Capture in variables

-
# /proc/<pid>/status
-#   Name:    cat
-#   ...
-#   VmRSS:   516 kB
-#   ...
-
-for f in /proc/*/status; do
-    cat $f | awk '
-             /^VmRSS/ { rss = $2/1024 }
-             /^Name/ { name = $2 }
-             END { printf "%16s %6d MB\n", name, rss }';
-done | sort -k2 -n
-
-

We capture values from VmRSS and Name into variables and print them at the -END once processing all records is done.

-

Run shell command and capture output

-
cat /proc/1/status | awk '
-                     /^Pid/ {
-                        "ps --no-header -o user " $2 | getline user;
-                         print user
-                     }'
-
-

We build a ps command line and capture the first line of the processes output -in the user variable and then print it.

- -
- - -
-
- - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/bash.html b/bash.html deleted file mode 100644 index 87a909f..0000000 --- a/bash.html +++ /dev/null @@ -1,360 +0,0 @@ - - - - - - bash - Notes - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -
- - - - - - - - - - -
-
-

bash(1)

-

Expansion

-

Generator

-
# generate sequence from n to m
-{n..m}
-# generate sequence from n to m step by s
-{n..m..s}
-
-# expand cartesian product
-{a,b}{c,d}
-
-

Parameter

-
# default param
-bar=${foo:-some_val}  # if $foo set, then bar=$foo else bar=some_val
-
-# check param set
-bar=${foo:?msg}  # if $foo set, then bar=$foo else exit and print msg
-
-# indirect
-FOO=foo
-BAR=FOO
-bar=${!BAR}  # deref value of BAR -> bar=$FOO
-
-# prefix
-${foo#prefix}  # remove prefix when expanding $foo
-# suffix
-${foo%suffix}  # remove suffix when expanding $foo
-
-# substitute
-${foo/pattern/string}  # replace pattern with string when expanding foo
-# pattern starts with
-# '/'   replace all occurences of pattern
-# '#'   pattern match at beginning
-# '%'   pattern match at end
-
-
-

Note: prefix/suffix/pattern are expanded as pathnames.

-
-

Pathname

-
*           match any string
-?           match any single char
-\\          match backslash
-[abc]       match any char of 'a' 'b' 'c'
-[a-z]       match any char between 'a' - 'z'
-[^ab]       negate, match all not 'a' 'b'
-[:class:]   match any char in class, available:
-              alnum,alpha,ascii,blank,cntrl,digit,graph,lower,
-              print,punct,space,upper,word,xdigit
-
-

Wit extglob shell option enabled it is possible to have more powerful -patterns. In the following pattern-list is one ore more patterns separated -by | char.

-
?(pattern-list)   matches zero or one occurrence of the given patterns
-*(pattern-list)   matches zero or more occurrences of the given patterns
-+(pattern-list)   matches one or more occurrences of the given patterns
-@(pattern-list)   matches one of the given patterns
-!(pattern-list)   matches anything except one of the given patterns
-
-
-

Note: shopt -s extglob/shopt -u extglob to enable/disable extglob -option.

-
-

I/O redirection

-
-

Note: The trick with bash I/O redirection is to interpret from left-to-right.

-
-
# stdout & stderr to file
-command >file 2>&1
-# equivalent
-command &>file
-
-# stderr to stdout & stdout to file
-command 2>&1 >file
-
-

Explanation

-
j>&i
-
-

Duplicate fd i to fd j, making j a copy of i. See dup2(2).

-

Example:

-
command 2>&1 >file
-
-
    -
  1. duplicate fd 1 to fd 2, effectively redirecting stderr to stdout
  2. -
  3. redirect stdout to file
  4. -
-

Completion

-

The complete builtin is used to interact with the completion system.

-
complete                    # print currently installed completion handler
-complete -F <func> <cmd>    # install <func> as completion handler for <cmd>
-complete -r <cmd>           # uninstall completion handler for <cmd>
-
-

Variables available in completion functions:

-
# in
-$1              # <cmd>
-$2              # current word
-$3              # privous word
-
-COMP_WORDS      # array with current command line words
-COMP_CWORD      # index into COMP_WORDS with current cursor position
-
-# out
-COMPREPLY       # array with possible completions
-
-

The compgen builtin is used to generate possible matches by comparing word -against words generated by option.

-
compgen [option] [word]
-
-# usefule options:
-# -W <list>    specify list of possible completions
-# -d           generate list with dirs
-# -f           generate list with files
-# -u           generate list with users
-# -e           generate list with exported variables
-
-# compare "f" against words "foo" "foobar" "bar" and generate matches
-compgen -W "foo foobar bar" "f"
-
-# compare "hom" against file/dir names and generate matches
-compgen -d -f "hom"
-
-

Example

-

Skeleton to copy/paste for writing simple completions.

-

Assume a program foo with the following interface:

-
foo -c green|red|blue -s low|high -f <file> -h
-
-

The completion handler could be implemented as follows:

-
function _foo() {
-    local curr=$2
-    local prev=$3
-
-    local opts="-c -s -f -h"
-    case $prev in
-        -c) COMPREPLY=( $(compgen -W "green red blue" -- $curr) );;
-        -s) COMPREPLY=( $(compgen -W "low high" -- $curr) );;
-        -f) COMPREPLY=( $(compgen -f -- $curr) );;
-        *)  COMPREPLY=( $(compgen -W "$opts" -- $curr) );;
-    esac
-}
-
-complete -F _foo foo
-
- -
- - -
-
- - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/binary/index.html b/binary/index.html new file mode 100644 index 0000000..2b47b26 --- /dev/null +++ b/binary/index.html @@ -0,0 +1,220 @@ + + + + + + Binary - Notes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + + + + +
+
+

Binary

+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/binary/nm.html b/binary/nm.html new file mode 100644 index 0000000..fa3dab6 --- /dev/null +++ b/binary/nm.html @@ -0,0 +1,224 @@ + + + + + + nm - Notes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + + + + +
+
+

nm(1)

+
  nm [opts] <elf>
+    -C          demangle
+    -u          undefined only
+
+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/binary/objdump.html b/binary/objdump.html new file mode 100644 index 0000000..60b7529 --- /dev/null +++ b/binary/objdump.html @@ -0,0 +1,233 @@ + + + + + + objdump - Notes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + + + + +
+
+

objdump(1)

+
  objdump [opts] <elf>
+    -M intel                use intil syntax
+    -d                      disassemble text section
+    -D                      disassemble all sections
+    -S                      mix disassembly with source code
+    -C                      demangle
+    -j <section>            display info for section
+    --[no-]show-raw-insn    [dont] show object code next to disassembly
+
+

Disassemble section

+

For example .plt section:

+
  objdump -j .plt -d <elf>
+
+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/binary/od.html b/binary/od.html new file mode 100644 index 0000000..74599f1 --- /dev/null +++ b/binary/od.html @@ -0,0 +1,260 @@ + + + + + + od - Notes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + + + + +
+
+

od(1)

+
  od [opts] <file>
+    -An         don't print addr info
+    -tx4        print hex in 4 byte chunks
+    -ta         print as named character
+    -tc         printable chars or backslash escape
+    -w4         print 4 bytes per line
+    -j <n>      skip <n> bytes from <file> (hex if start with 0x)
+    -N <n>      dump <n> bytes (hex of start with 0x)
+
+

ASCII to hex string

+
  echo -n AAAABBBB | od -An -w4 -tx4
+    >> 41414141
+    >> 42424242
+
+  echo -n '\x7fELF\n' | od -tx1 -ta -tc
+    >> 0000000  7f  45  4c  46  0a      # tx1
+    >>         del   E   L   F  nl      # ta
+    >>         177   E   L   F  \n      # tc
+
+

Extract parts of file

+

For example .rodata section from an elf file. We can use readelf to get the +offset into the file where the .rodata section starts.

+
  readelf -W -S foo
+    >> Section Headers:
+    >> [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
+    >> ...
+    >> [15] .rodata           PROGBITS        00000000004009c0 0009c0 000030 00   A  0   0 16
+
+

With the offset of -j 0x0009c0 we can dump -N 0x30 bytes from the beginning of +the .rodata section as follows:

+
  od -j 0x0009c0 -N 0x30 -tx4 -w4 foo
+    >> 0004700 00020001
+    >> 0004704 00000000
+    >> *
+    >> 0004740 00000001
+    >> 0004744 00000002
+    >> 0004750 00000003
+    >> 0004754 00000004
+
+

Note: Numbers starting with 0x will be interpreted as hex by od.

+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/binary/readelf.html b/binary/readelf.html new file mode 100644 index 0000000..03a1049 --- /dev/null +++ b/binary/readelf.html @@ -0,0 +1,230 @@ + + + + + + readelf - Notes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + + + + +
+
+

readelf(1)

+
  readelf [opts] <elf>
+    -W|--wide     wide output, dont break output at 80 chars
+    -h            print ELF header
+    -S            print section headers
+    -l            print program headers + segment mapping
+    -d            print .dynamic section (dynamic link information)
+    --syms        print symbol tables (.symtab .dynsym)
+    --dyn-syms    print dynamic symbol table (exported symbols for dynamic linker)
+    -r            print relocation sections (.rel.*, .rela.*)
+
+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/binary/xxd.html b/binary/xxd.html new file mode 100644 index 0000000..ac248b4 --- /dev/null +++ b/binary/xxd.html @@ -0,0 +1,245 @@ + + + + + + xxd - Notes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + + + + +
+
+

xxd(1)

+
  xxd [opts]
+    -p          dump continuous hexdump
+    -r          convert hexdump into binary ('revert')
+    -e          dump as little endian mode
+    -i          output as C array
+
+

ASCII to hex stream

+
  echo -n 'aabb' | xxd -p
+    >> 61616262
+
+

Hex to binary stream

+
  echo -n '61616262' | xxd -p -r
+    >> aabb
+
+

ASCII to binary

+
  echo -n '\x7fELF' | xxd -p | xxd -p -r | file -p -
+    >> ELF
+
+

ASCII to C array (hex encoded)

+
  xxd -i <(echo -n '\x7fELF')
+    >> unsigned char _proc_self_fd_11[] = {
+    >>   0x7f, 0x45, 0x4c, 0x46
+    >> };
+    >> unsigned int _proc_self_fd_11_len = 4;
+
+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/c++.html b/c++.html deleted file mode 100644 index 8dc9708..0000000 --- a/c++.html +++ /dev/null @@ -1,227 +0,0 @@ - - - - - - c++ - Notes - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -
- - - - - - - - - - -
-
-

c++

-

Type deduction

-

Force compile error to see what auto is deduced to.

-
auto foo = bar();
-
-// force compile error
-typename decltype(foo)::_;
-
- -
- - -
-
- - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/c++filt.html b/c++filt.html deleted file mode 100644 index e8966ff..0000000 --- a/c++filt.html +++ /dev/null @@ -1,227 +0,0 @@ - - - - - - c++filt - Notes - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -
- - - - - - - - - - -
-
-

c++filt(1)

-

Demangle symbol

-
  c++-filt <symbol_str>
-
-

Demangle stream

-

For example dynamic symbol table:

-
  readelf -W --dyn-syms <elf> | c++filt
-
- -
- - -
-
- - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/development/c++.html b/development/c++.html new file mode 100644 index 0000000..e5d5e7c --- /dev/null +++ b/development/c++.html @@ -0,0 +1,227 @@ + + + + + + c++ - Notes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + + + + +
+
+

c++

+

Type deduction

+

Force compile error to see what auto is deduced to.

+
auto foo = bar();
+
+// force compile error
+typename decltype(foo)::_;
+
+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/development/c++filt.html b/development/c++filt.html new file mode 100644 index 0000000..24a7ac5 --- /dev/null +++ b/development/c++filt.html @@ -0,0 +1,227 @@ + + + + + + c++filt - Notes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + + + + +
+
+

c++filt(1)

+

Demangle symbol

+
  c++-filt <symbol_str>
+
+

Demangle stream

+

For example dynamic symbol table:

+
  readelf -W --dyn-syms <elf> | c++filt
+
+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/development/gcc.html b/development/gcc.html new file mode 100644 index 0000000..bf457aa --- /dev/null +++ b/development/gcc.html @@ -0,0 +1,269 @@ + + + + + + gcc - Notes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + + + + +
+
+

gcc(1)

+

CLI

+

Preprocessing

+

While debugging can be helpful to just pre-process files.

+
gcc -E [-dM] ...
+
+
    +
  • -E run only preprocessor
  • +
  • -dM list only #define statements
  • +
+

Builtins

+

__builtin_expect(expr, cond)

+

Give the compiler a hint which branch is hot, so it can lay out the code +accordingly to reduce number of jump instructions. +See on compiler explorer.

+
echo "
+extern void foo();
+extern void bar();
+void run0(int x) {
+  if (__builtin_expect(x,0)) { foo(); }
+  else { bar(); }
+}
+void run1(int x) {
+  if (__builtin_expect(x,1)) { foo(); }
+  else { bar(); }
+}
+" | gcc -O2 -S -masm=intel -o /dev/stdout -xc -
+
+

Will generate something similar to the following.

+
    +
  • run0: bar is on the path without branch
  • +
  • run1: foo is on the path without branch
  • +
+
run0:
+        test    edi, edi
+        jne     .L4
+        xor     eax, eax
+        jmp     bar
+.L4:
+        xor     eax, eax
+        jmp     foo
+run1:
+        test    edi, edi
+        je      .L6
+        xor     eax, eax
+        jmp     foo
+.L6:
+        xor     eax, eax
+        jmp     bar
+
+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/development/glibc.html b/development/glibc.html new file mode 100644 index 0000000..abf590f --- /dev/null +++ b/development/glibc.html @@ -0,0 +1,249 @@ + + + + + + glibc - Notes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + + + + +
+
+

glibc

+

malloc tracer mtrace(3)

+

Trace memory allocation and de-allocation to detect memory leaks. +Need to call mtrace(3) to install the tracing hooks.

+

If we can't modify the binary to call mtrace we can create a small shared +library and pre-load it.

+
// libmtrace.c
+#include <mcheck.h>
+__attribute__((constructor))  static void init_mtrace() { mtrace(); }
+
+

Compile as:

+
gcc -shared -fPIC -o libmtrace.so libmtrace.c
+
+

To generate the trace file run:

+
export MALLOC_TRACE=<file>
+LD_PRELOAD=./libmtrace.so <binary>
+
+

Note: If MALLOC_TRACE is not set mtrace won't install tracing hooks.

+

To get the results of the trace file:

+
mtrace <binary> $MALLOC_TRACE
+
+

malloc check mallopt(3)

+

Configure action when glibc detects memory error.

+
export MALLOC_CHECK_=<N>
+
+

Useful values:

+
1   print detailed error & continue
+3   print detailed error + stack trace + memory mappings & abort
+7   print simple error message + stack trace + memory mappings & abort
+
+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/development/index.html b/development/index.html new file mode 100644 index 0000000..56cf7c5 --- /dev/null +++ b/development/index.html @@ -0,0 +1,220 @@ + + + + + + Development - Notes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + + + + +
+
+

Development

+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/development/ld.so.html b/development/ld.so.html new file mode 100644 index 0000000..3a66986 --- /dev/null +++ b/development/ld.so.html @@ -0,0 +1,322 @@ + + + + + + ld.so - Notes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + + + + +
+
+

ld.so(8)

+

Environment Variables

+
  LD_PRELOAD=<l_so>       colon separated list of libso's to be pre loaded
+  LD_DEBUG=<opts>         comma separated list of debug options
+          =help           list available options
+          =libs           show library search path
+          =files          processing of input files
+          =symbols        show search path for symbol lookup
+          =bindings       show against which definition a symbol is bound
+
+

LD_PRELOAD: Initialization Order and Link Map

+

Libraries specified in LD_PRELOAD are loaded from left-to-right but +initialized from right-to-left.

+
  > ldd ./main
+    >> libc.so.6 => /usr/lib/libc.so.6
+
+  > LD_PRELOAD=liba.so:libb.so ./main
+             -->
+      preloaded in this order
+             <--
+      initialized in this order
+
+

The preload order determines:

+
    +
  • the order libraries are inserted into the link map
  • +
  • the initialization order for libraries
  • +
+

For the example listed above the resulting link map will look like the +following:

+
  +------+    +------+    +------+    +------+
+  | main | -> | liba | -> | libb | -> | libc |
+  +------+    +------+    +------+    +------+
+
+

This can be seen when running with LD_DEBUG=files:

+
  > LD_DEBUG=files LD_PRELOAD=liba.so:libb.so ./main
+    # load order (-> determines link map)
+    >> file=liba.so [0];  generating link map
+    >> file=libb.so [0];  generating link map
+    >> file=libc.so.6 [0];  generating link map
+
+    # init order
+    >> calling init: /usr/lib/libc.so.6
+    >> calling init: <path>/libb.so
+    >> calling init: <path>/liba.so
+    >> initialize program: ./main
+
+

To verify the link map order we let ld.so resolve the memcpy(3) libc +symbol (used in main) dynamically, while enabling LD_DEBUG=symbols,bindings +to see the resolving in action.

+
  > LD_DEBUG=symbols,bindings LD_PRELOAD=liba.so:libb.so ./main
+    >> symbol=memcpy;  lookup in file=./main [0]
+    >> symbol=memcpy;  lookup in file=<path>/liba.so [0]
+    >> symbol=memcpy;  lookup in file=<path>/libb.so [0]
+    >> symbol=memcpy;  lookup in file=/usr/lib/libc.so.6 [0]
+    >> binding file ./main [0] to /usr/lib/libc.so.6 [0]: normal symbol `memcpy' [GLIBC_2.14]
+
+

Dynamic Linking (x86_64)

+

Dynamic linking basically works via one indirect jump. It uses a combination of +function trampolines (.plt section) and a function pointer table (.got.plt +section). +On the first call the trampoline sets up some metadata and then jumps to the +ld.so runtime resolve function, which in turn patches the table with the +correct function pointer.

+
  .plt ....... procedure linkage table, contains function trampolines, usually
+               located in code segment (rx permission)
+  .got.plt ... global offset table for .plt, holds the function pointer table
+
+

Using radare2 we can analyze this in more detail:

+
  [0x00401040]> pd 4 @ section..got.plt
+              ;-- section..got.plt:
+              ;-- .got.plt:    ; [22] -rw- section size 32 named .got.plt
+              ;-- _GLOBAL_OFFSET_TABLE_:
+         [0]  0x00404000      .qword 0x0000000000403e10 ; section..dynamic
+         [1]  0x00404008      .qword 0x0000000000000000
+              ; CODE XREF from section..plt @ +0x6
+         [2]  0x00404010      .qword 0x0000000000000000
+              ;-- reloc.puts:
+              ; CODE XREF from sym.imp.puts @ 0x401030
+         [3]  0x00404018      .qword 0x0000000000401036 ; RELOC 64 puts
+
+  [0x00401040]> pd 6 @ section..plt
+              ;-- section..plt:
+              ;-- .plt:       ; [12] -r-x section size 32 named .plt
+          ┌─> 0x00401020      ff35e22f0000   push qword [0x00404008]
+          ╎   0x00401026      ff25e42f0000   jmp qword [0x00404010]
+          ╎   0x0040102c      0f1f4000       nop dword [rax]
+  ┌ 6: int sym.imp.puts (const char *s);
+  └       ╎   0x00401030      ff25e22f0000   jmp qword [reloc.puts]
+          ╎   0x00401036      6800000000     push 0
+          └─< 0x0040103b      e9e0ffffff     jmp sym..plt
+
+
    +
  • At address 0x00401030 in the .plt section we see the indirect jump for +puts using the function pointer in _GLOBAL_OFFSET_TABLE_[3] (GOT).
  • +
  • GOT[3] initially points to instruction after the puts trampoline +0x00401036.
  • +
  • This pushes the relocation index 0 and then jumps to the first trampoline +0x00401020.
  • +
  • The first trampoline jumps to GOT[2] which will be filled at program +startup by the ld.so with its resolve function.
  • +
  • The ld.so resolve function fixes the relocation referenced by the +relocation index pushed by the puts trampoline.
  • +
  • The relocation entry at index 0 tells the resolve function which symbol to +search for and where to put the function pointer: +
      > readelf -r <main>
    +    >> Relocation section '.rela.plt' at offset 0x4b8 contains 1 entry:
    +    >>   Offset          Info           Type           Sym. Value    Sym. Name + Addend
    +    >> 000000404018  000200000007 R_X86_64_JUMP_SLO 0000000000000000 puts@GLIBC_2.2.5 + 0
    +
    +As we can see the offset from relocation at index 0 points to GOT[3].
  • +
+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/emacs.html b/emacs.html deleted file mode 100644 index 382f68a..0000000 --- a/emacs.html +++ /dev/null @@ -1,283 +0,0 @@ - - - - - - emacs - Notes - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -
- - - - - - - - - - -
-
-

emacs(1)

-

help

-
  C-h ?         list available help modes
-  C-h f         describe function
-  C-h v         describe variable
-  C-h c <KEY>   print command bound to <KEY>
-  C-h k <KEY>   describe command bound to <KEY>
-  C-h b         list buffer local key-bindings
-  <kseq> C-h    list possible key-bindings with <kseq>
-                eg C-x C-h -> list key-bindings beginning with C-x
-
-

package manager

-
  package-refresh-contents    refresh package list
-  package-list-packages       list available/installed packages
-
-

window

-
  C-x 0         kill focused window
-  C-x 1         kill all other windows
-  C-x 2         split horizontal
-  C-x 3         split vertical
-
-

yank/paste

-
  C-<SPACE>  set start mark to select text
-  M-w        copy selected text
-  C-w        kill selected text
-  C-y        paste selected text
-  M-y        cycle through kill-ring
-
-

block/rect

-
  C-x <SPC>                     activate rectangle-mark-mode
-  M-x string-rectangle <RET>    insert text in marked rect
-
-

mass edit

-
  C-x h                                 mark whole buffer (mark-whole-buffer)
-  M-x delete-matching-line <RET>        delete lines matching regex
-  M-x %                                 search & replace region (query-replace)
-  C-M-x %                               search & replace regex (query-replace-regexp)
-
-

grep

-
  M-x find-grep <RET>           run find-grep result in *grep* buffer
-  n/p                           navigate next/previous match in *grep* buffer
-
-

lisp mode

-
  M-x lisp-interaction-mode     activate lisp mode
-  C-M-x                         evaluate top expr under cursor
-  C-x C-e                       eval-last-sexp
-  C-u C-x C-e                   eval-last-sexp and prints result in current buffer
-
-

narrow

-
  C-x n n               show only focused region (narrow)
-  C-x n w               show whole buffer (wide)
-
-

org

-
  M-up/M-down           re-arrange items in same hierarchy
-  M-left/M-right        change item hierarchy
-  C-RET                 create new item below current
-  C-S-RET               create new TODO item below current
-  S-left/S-right        cycle TODO states
-
-

org source

-
  <s TAB                generate a source block
-  C-c '                 edit source block (in lang specific buffer)
-  C-c C-c               eval source block
-
- -
- - -
-
- - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/fish.html b/fish.html deleted file mode 100644 index e9c64b7..0000000 --- a/fish.html +++ /dev/null @@ -1,231 +0,0 @@ - - - - - - fish - Notes - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -
- - - - - - - - - - -
-
-

fish(1)

-

keymaps

-
  Shift-Tab ........... tab-completion with search
-  Alt-Up / Alt-Down ... search history with token under the cursor
-  Alt-l ............... list content of dir under cursor
-  Alt-p ............... append '2>&1 | less;' to current cmdline
-
-

debug

-
  status print-stack-trace .. prints function stacktrace (can be used in scripts)
-  breakpoint ................ halt script execution and gives shell (C-d | exit
-                              to continue)
-
- -
- - -
-
- - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/gcc.html b/gcc.html deleted file mode 100644 index 196ab3a..0000000 --- a/gcc.html +++ /dev/null @@ -1,261 +0,0 @@ - - - - - - gcc - Notes - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -
- - - - - - - - - - -
-
-

gcc(1)

-

CLI

-

Preprocessing

-

While debugging can be helpful to just pre-process files.

-
gcc -E [-dM] ...
-
-
    -
  • -E run only preprocessor
  • -
  • -dM list only #define statements
  • -
-

Builtins

-

__builtin_expect(expr, cond)

-

Give the compiler a hint which branch is hot, so it can lay out the code -accordingly to reduce number of jump instructions. -See on compiler explorer.

-
echo "
-extern void foo();
-extern void bar();
-void run0(int x) {
-  if (__builtin_expect(x,0)) { foo(); }
-  else { bar(); }
-}
-void run1(int x) {
-  if (__builtin_expect(x,1)) { foo(); }
-  else { bar(); }
-}
-" | gcc -O2 -S -masm=intel -o /dev/stdout -xc -
-
-

Will generate something similar to the following.

-
    -
  • run0: bar is on the path without branch
  • -
  • run1: foo is on the path without branch
  • -
-
run0:
-        test    edi, edi
-        jne     .L4
-        xor     eax, eax
-        jmp     bar
-.L4:
-        xor     eax, eax
-        jmp     foo
-run1:
-        test    edi, edi
-        je      .L6
-        xor     eax, eax
-        jmp     foo
-.L6:
-        xor     eax, eax
-        jmp     bar
-
- -
- - -
-
- - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/gdb.html b/gdb.html deleted file mode 100644 index c1e69e2..0000000 --- a/gdb.html +++ /dev/null @@ -1,358 +0,0 @@ - - - - - - gdb - Notes - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -
- - - - - - - - - - -
-
-

gdb(1)

-

CLI

-
  gdb [opts] [prg [-c coredump | -p pid]]
-  gdb [opts] --args prg <prg-args>
-    opts:
-      -p <pid>        attach to pid
-      -c <coredump>   use <coredump>
-      -x <file>       execute script <file> before prompt
-      -ex <cmd>       execute command <cmd> before prompt
-      --tty <tty>     set I/O tty for debugee
-
-

Interactive usage

-
  tty <tty>
-          Set <tty> as tty for debugee.
-          Make sure nobody reads from target tty, easiest is to spawn a shell
-          and run following in target tty:
-          > while true; do sleep 1024; done
-
-  set follow-fork-mode <child | parent>
-          Specify which process to follow when debuggee makes a fork(2)
-          syscall.
-
-  sharedlibrary [<regex>]
-          Load symbols of shared libs loaded by debugee. Optionally use <regex>
-          to filter libs for symbol loading.
-
-  break [-qualified] <sym> thread <tnum>
-          Set a breakpoint only for a specific thread.
-          -qualified: Tred <sym> as fully qualified symbol (quiet handy to set
-          breakpoints on C symbols in C++ contexts)
-
-  rbreak <regex>
-          Set breakpoints matching <regex>, where matching internally is done
-          on: .*<regex>.*
-
-  command [<bp_list>]
-          Define commands to run after breakpoint hit. If <bp_list> is not
-          specified attach command to last created breakpoint. Command block
-          terminated with 'end' token.
-
-          <bp_list>: Space separates list, eg 'command 2 5-8' to run command
-          for breakpoints: 2,5,6,7,8.
-
-  info functions [<regex>]
-          List functions matching <regex>. List all functions if no <regex>
-          provided.
-
-  info variables [<regex>]
-          List variables matching <regex>. List all variables if no <regex>
-          provided.
-
-  info handle [<signal>]
-          Print how to handle <signal>. If no <signal> specified print for all
-          signals.
-
-  handle <signal> <action>
-          Configure how gdb handles <signal> sent to debugee.
-          <action>:
-            stop/nostop       Catch signal in gdb and break.
-            print/noprint     Print message when gdb catches signal.
-            pass/nopass       Pass signal down to debugee.
-
-  catch signal <signal>
-          Create a catchpoint for <signal>.
-
-

User commands (macros)

-

Gdb allows to create & document user commands as follows:

-
  define <cmd>
-    # cmds
-  end
-
-  document <cmd>
-    # docu
-  end
-
-

To get all user commands or documentations one can use:

-
  help user-defined
-  help <cmd>
-
-

Hooks

-

Gdb allows to create two types of command hooks

-
    -
  • hook- will be run before <cmd>
  • -
  • hookpost- will be run after <cmd>
  • -
-
  define hook-<cmd>
-    # cmds
-  end
-
-  define hookpost-<cmd>
-    # cmds
-  end
-
-

Examples

-

Catch SIGSEGV and execute commands

-

This creates a catchpoint for the SIGSEGV signal and attached the command -to it.

-
  catch signal SIGSEGV
-  command
-    bt
-    c
-  end
-
-

Run backtrace on thread 1 (batch mode)

-
  gdb --batch -ex 'thread 1' -ex 'bt' -p <pid>
-
-

Script gdb for automating debugging sessions

-

To script gdb add commands into a file and pass it to gdb via -x. -For example create run.gdb:

-
  set pagination off
-
-  break mmap
-  command
-    info reg rdi rsi rdx
-    bt
-    c
-  end
-
-  #initial drop
-  c
-
-

This script can be used as:

-
  gdb --batch -x ./run.gdb -p <pid>
-
-

Know Bugs

-

Workaround command + finish bug

-

When using finish inside a command block, commands after finish are not -executed. To workaround that bug one can create a wrapper function which calls -finish.

-
  define handler
-    bt
-    finish
-    info reg rax
-  end
-
-  command
-    handler
-  end
-
- -
- - -
-
- - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/git.html b/git.html deleted file mode 100644 index 1e67d95..0000000 --- a/git.html +++ /dev/null @@ -1,324 +0,0 @@ - - - - - - git - Notes - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -
- - - - - - - - - - -
-
-

git(1)

-

staging

-
  git add -p [<file>] ............ partial staging (interactive)
-
-

Remote

-
  git remote -v .................. list remotes verbose (with URLs)
-  git remote show [-n] <remote> .. list info for <remote> (like remote HEAD,
-                                   remote branches, tracking mapping)
-
-

Branching

-
  git branch [-a] ................ list available branches; -a to include
-                                   remote branches
-  git branch -vv ................. list branch & annotate with head sha1 &
-                                   remote tracking branch
-  git branch <bname> ............. create branch with name <bname>
-  git checkout <bname> ........... switch to branch with name <bname>
-  git push -u origin <rbname> .... push branch to origin (or other remote), and
-                                   setup <rbname> as tracking branch
-
-

Resetting

-
  git reset [opt] <ref|commit>
-    opt:
-      --mixed .................... resets index, but not working tree
-      --hard ..................... matches the working tree and index to that
-                                   of the tree being switched to any changes to
-                                   tracked files in the working tree since
-                                   <commit> are lost
-  git reset HEAD <file> .......... remove file from staging
-  git reset --soft HEAD~1 ........ delete most recent commit but keep work
-  git reset --hard HEAD~1 ........ delete most recent commit and delete work
-
-

Tags

-
  git tag -a <tname> -m "descr" ........ creates an annotated tag (full object
-                                         containing tagger, date, ...)
-  git tag -l ........................... list available tags
-  git checkout tag/<tname> ............. checkout specific tag
-  git checkout tag/<tname> -b <bname> .. checkout specific tag in a new branch
-
-

Diff

-
  git diff HEAD:<fname> origin/HEAD:<fname> ... diff files for different refs
-  git diff -U$(wc -l <fname>) <fname> ......... shows complete file with diffs
-                                                instead of usual diff snippets
-
-

Log

-
  git log --oneline .... shows log in single line per commit -> alias for
-                         '--pretty=oneline --abbrev-commit'
-  git log --graph ...... text based graph of commit history
-  git log --decorate ... decorate log with REFs
-
-

File history

-
  git log -p <file> ......... show commit history + diffs for <file>
-  git log --oneline <file> .. show commit history for <file> in compact format
-
-

Patching

-
  git format-patch <opt> <since>/<revision range>
-    opt:
-      -N ................... use [PATCH] instead [PATCH n/m] in subject when
-                             generating patch description (for patches spanning
-                             multiple commits)
-      --start-number <n> ... start output file generation with <n> as start
-                             number instead '1'
-    since spcifier:
-      -3 .................. e.g: create a patch from last three commits
-      <commit hash> ....... create patch with commits starting after <commit hash>
-
-  git am <patch> ......... apply patch and create a commit for it
-
-  git apply --stat <PATCH> ... see which files the patch would change
-  git apply --check <PATCH> .. see if the patch can be applied cleanly
-  git apply <PATCH> .......... apply the patch locally without creating a commit
-
-  # eg: generate patches for each commit from initial commit on
-  git format-patch -N $(git rev-list --max-parents=0 HEAD)
-
-  # generate single patch file from a certain commit/ref
-  git format-patch <COMMIT/REF> --stdout > my-patch.patch
-
-

Submodules

-
  git submodule add <url> [<path>] .......... add new submodule to current project
-  git clone --recursive <url> ............... clone project and recursively all
-                                              submodules (same as using
-                                              'git submodule update --init
-                                              --recursive' after clone)
-  git submodule update --init --recursive ... checkout submodules recursively
-                                              using the commit listed in the
-                                              super-project (in detached HEAD)
-  git submodule update --remote <submod> .... fetch & merge remote changes for
-                                              <submod>, this will pull
-                                              origin/HEAD or a branch specified
-                                              for the submodule
-  git diff --submodule ...................... show commits that are part of the
-                                              submodule diff
-
-

Inspection

-
  git ls-tree [-r] <ref> .... show git tree for <ref>, -r to recursively ls sub-trees
-  git show <obj> ............ show <obj>
-  git cat-file -p <obj> ..... print content of <obj>
-
-

Revision Specifier

-
  HEAD ........ last commit
-  HEAD~1 ...... last commit-1
-  HEAD~N ...... last commit-N (linear backwards when in tree structure, check
-                difference between HEAD^ and HEAD~)
-  git rev-list --max-parents=0 HEAD ........... first commit
-
- -
- - -
-
- - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/glibc.html b/glibc.html deleted file mode 100644 index cf2e12a..0000000 --- a/glibc.html +++ /dev/null @@ -1,249 +0,0 @@ - - - - - - glibc - Notes - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -
- - - - - - - - - - -
-
-

glibc

-

malloc tracer mtrace(3)

-

Trace memory allocation and de-allocation to detect memory leaks. -Need to call mtrace(3) to install the tracing hooks.

-

If we can't modify the binary to call mtrace we can create a small shared -library and pre-load it.

-
// libmtrace.c
-#include <mcheck.h>
-__attribute__((constructor))  static void init_mtrace() { mtrace(); }
-
-

Compile as:

-
gcc -shared -fPIC -o libmtrace.so libmtrace.c
-
-

To generate the trace file run:

-
export MALLOC_TRACE=<file>
-LD_PRELOAD=./libmtrace.so <binary>
-
-

Note: If MALLOC_TRACE is not set mtrace won't install tracing hooks.

-

To get the results of the trace file:

-
mtrace <binary> $MALLOC_TRACE
-
-

malloc check mallopt(3)

-

Configure action when glibc detects memory error.

-
export MALLOC_CHECK_=<N>
-
-

Useful values:

-
1   print detailed error & continue
-3   print detailed error + stack trace + memory mappings & abort
-7   print simple error message + stack trace + memory mappings & abort
-
- -
- - -
-
- - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/index.html b/index.html index 4a46822..8ca6bd8 100644 --- a/index.html +++ b/index.html @@ -3,7 +3,7 @@ - ld.so - Notes + Misc - Notes @@ -81,7 +81,7 @@ @@ -148,118 +148,7 @@
-

ld.so(8)

-

Environment Variables

-
  LD_PRELOAD=<l_so>       colon separated list of libso's to be pre loaded
-  LD_DEBUG=<opts>         comma separated list of debug options
-          =help           list available options
-          =libs           show library search path
-          =files          processing of input files
-          =symbols        show search path for symbol lookup
-          =bindings       show against which definition a symbol is bound
-
-

LD_PRELOAD: Initialization Order and Link Map

-

Libraries specified in LD_PRELOAD are loaded from left-to-right but -initialized from right-to-left.

-
  > ldd ./main
-    >> libc.so.6 => /usr/lib/libc.so.6
-
-  > LD_PRELOAD=liba.so:libb.so ./main
-             -->
-      preloaded in this order
-             <--
-      initialized in this order
-
-

The preload order determines:

-
    -
  • the order libraries are inserted into the link map
  • -
  • the initialization order for libraries
  • -
-

For the example listed above the resulting link map will look like the -following:

-
  +------+    +------+    +------+    +------+
-  | main | -> | liba | -> | libb | -> | libc |
-  +------+    +------+    +------+    +------+
-
-

This can be seen when running with LD_DEBUG=files:

-
  > LD_DEBUG=files LD_PRELOAD=liba.so:libb.so ./main
-    # load order (-> determines link map)
-    >> file=liba.so [0];  generating link map
-    >> file=libb.so [0];  generating link map
-    >> file=libc.so.6 [0];  generating link map
-
-    # init order
-    >> calling init: /usr/lib/libc.so.6
-    >> calling init: <path>/libb.so
-    >> calling init: <path>/liba.so
-    >> initialize program: ./main
-
-

To verify the link map order we let ld.so resolve the memcpy(3) libc -symbol (used in main) dynamically, while enabling LD_DEBUG=symbols,bindings -to see the resolving in action.

-
  > LD_DEBUG=symbols,bindings LD_PRELOAD=liba.so:libb.so ./main
-    >> symbol=memcpy;  lookup in file=./main [0]
-    >> symbol=memcpy;  lookup in file=<path>/liba.so [0]
-    >> symbol=memcpy;  lookup in file=<path>/libb.so [0]
-    >> symbol=memcpy;  lookup in file=/usr/lib/libc.so.6 [0]
-    >> binding file ./main [0] to /usr/lib/libc.so.6 [0]: normal symbol `memcpy' [GLIBC_2.14]
-
-

Dynamic Linking (x86_64)

-

Dynamic linking basically works via one indirect jump. It uses a combination of -function trampolines (.plt section) and a function pointer table (.got.plt -section). -On the first call the trampoline sets up some metadata and then jumps to the -ld.so runtime resolve function, which in turn patches the table with the -correct function pointer.

-
  .plt ....... procedure linkage table, contains function trampolines, usually
-               located in code segment (rx permission)
-  .got.plt ... global offset table for .plt, holds the function pointer table
-
-

Using radare2 we can analyze this in more detail:

-
  [0x00401040]> pd 4 @ section..got.plt
-              ;-- section..got.plt:
-              ;-- .got.plt:    ; [22] -rw- section size 32 named .got.plt
-              ;-- _GLOBAL_OFFSET_TABLE_:
-         [0]  0x00404000      .qword 0x0000000000403e10 ; section..dynamic
-         [1]  0x00404008      .qword 0x0000000000000000
-              ; CODE XREF from section..plt @ +0x6
-         [2]  0x00404010      .qword 0x0000000000000000
-              ;-- reloc.puts:
-              ; CODE XREF from sym.imp.puts @ 0x401030
-         [3]  0x00404018      .qword 0x0000000000401036 ; RELOC 64 puts
-
-  [0x00401040]> pd 6 @ section..plt
-              ;-- section..plt:
-              ;-- .plt:       ; [12] -r-x section size 32 named .plt
-          ┌─> 0x00401020      ff35e22f0000   push qword [0x00404008]
-          ╎   0x00401026      ff25e42f0000   jmp qword [0x00404010]
-          ╎   0x0040102c      0f1f4000       nop dword [rax]
-  ┌ 6: int sym.imp.puts (const char *s);
-  └       ╎   0x00401030      ff25e22f0000   jmp qword [reloc.puts]
-          ╎   0x00401036      6800000000     push 0
-          └─< 0x0040103b      e9e0ffffff     jmp sym..plt
-
-
    -
  • At address 0x00401030 in the .plt section we see the indirect jump for -puts using the function pointer in _GLOBAL_OFFSET_TABLE_[3] (GOT).
  • -
  • GOT[3] initially points to instruction after the puts trampoline -0x00401036.
  • -
  • This pushes the relocation index 0 and then jumps to the first trampoline -0x00401020.
  • -
  • The first trampoline jumps to GOT[2] which will be filled at program -startup by the ld.so with its resolve function.
  • -
  • The ld.so resolve function fixes the relocation referenced by the -relocation index pushed by the puts trampoline.
  • -
  • The relocation entry at index 0 tells the resolve function which symbol to -search for and where to put the function pointer: -
      > readelf -r <main>
    -    >> Relocation section '.rela.plt' at offset 0x4b8 contains 1 entry:
    -    >>   Offset          Info           Type           Sym. Value    Sym. Name + Addend
    -    >> 000000404018  000200000007 R_X86_64_JUMP_SLO 0000000000000000 puts@GLIBC_2.2.5 + 0
    -
    -As we can see the offset from relocation at index 0 points to GOT[3].
  • -
- +