Skip to content

Commit 884780c

Browse files
committed
chore: add linker script
1 parent 941338e commit 884780c

File tree

3 files changed

+85
-14
lines changed

3 files changed

+85
-14
lines changed

attic/README.md

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,35 +89,43 @@ Some compilers/linker specific flags are needed to compile the shellcode.
8989
- Visibility: `-fvisibility=hidden -fvisibility-inlines-hidden`
9090
- Make linker complain about missing symbols: `-Wl,--no-allow-shlib-undefined,--no-undefined`
9191
- Disable lazy binding: `-Wl,-z,defs,-z,now,-z,relro`
92-
- Remove unused sections: `-Wl,--gc-sections`
92+
- Specify the linker script: `-T,shellcode.ld`
93+
- Remove unused code: `-Wl,--gc-sections`
9394

9495
An example command to compile the shellcode:
9596

9697
```shell
9798
/path/to/clang++ -shared -fPIC -std=c++14 -O3 \
9899
-fvisibility=hidden -fvisibility-inlines-hidden -fno-omit-frame-pointer -Wall \
99100
-fno-rtti -fno-exceptions -nostdlib \
100-
-Wl,-Bsymbolic,--no-allow-shlib-undefined,--no-undefined,-z,defs,-z,now,-z,relro,--gc-sections \
101+
-Wl,-Bsymbolic,--no-allow-shlib-undefined,--no-undefined,-z,defs,-z,now,-z,relro,--gc-sections,-T,shellcode.ld \
101102
-I/path/to/jni/include -I/path/to/linux-syscall-support \
102103
all_in_one.cc -o libcore_syscall.so
103104
```
104105

105-
The `file *.so` output may look like this:
106+
The `readelf --dynamic *.so` output may look like this:
106107

107108
```text
108-
shellcode-arm.so: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), static-pie linked, not stripped
109-
shellcode-arm64.so: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), static-pie linked, not stripped
110-
shellcode-mips.so: ELF 32-bit LSB shared object, MIPS, MIPS32 version 1 (SYSV), static-pie linked, not stripped
111-
shellcode-mips64.so: ELF 64-bit LSB shared object, MIPS, MIPS64 rel6 version 1 (SYSV), static-pie linked, not stripped
112-
shellcode-riscv64.so: ELF 64-bit LSB shared object, UCB RISC-V, RVC, double-float ABI, version 1 (SYSV), static-pie linked, not stripped
113-
shellcode-x86.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), static-pie linked, not stripped
114-
shellcode-x86_64.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), static-pie linked, not stripped
109+
Dynamic section at offset 0x1000 contains 9 entries:
110+
Tag Type Name/Value
111+
0x000000000000001e (FLAGS) SYMBOLIC BIND_NOW
112+
0x000000006ffffffb (FLAGS_1) Flags: NOW
113+
0x0000000000000006 (SYMTAB) 0x1090
114+
0x000000000000000b (SYMENT) 24 (bytes)
115+
0x0000000000000005 (STRTAB) 0x1258
116+
0x000000000000000a (STRSZ) 444 (bytes)
117+
0x000000006ffffef5 (GNU_HASH) 0x14b8
118+
0x0000000000000004 (HASH) 0x1414
119+
0x0000000000000000 (NULL) 0x0
115120
```
116121

117-
Note that they should be `static-pie linked`, not `dynamically linked`.
122+
Note that there should be no `DT_NEEDED` entry in the dynamic section.
118123

119124
Get the symbol table: `llvm-objdump -T libcore_syscall.so`
120125

121-
Dump the text section: `llvm-objcopy -O binary --only-section=.text libcore_syscall.so shellcode.bin`
126+
If you are using your own linker script, make sure that the `.text` and `.rodata` sections are in the right place.
127+
128+
Dump the .text and .rodata sections:
129+
`llvm-objcopy -O binary --only-section=.text --only-section=.rodata libcore_syscall.so shellcode.bin`
122130

123131
That's all for the shellcode.

attic/hook_info.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include "hook_info.h"
22

33
EXPORT volatile HookInfo* get_hook_info() {
4-
// place the hook info in the .text section, it will be filled before the shellcode is executed
5-
__attribute__((aligned(16), section(".text")))
4+
// place the hook info in the .rodata.hook_info section, it will be filled before the shellcode is executed
5+
__attribute__((aligned(16), section(".rodata.hook_info")))
66
static volatile HookInfo sHookInfo = {0xdeafbeef, {(int* (*)()) 0x114514}, {0x1000}};
77
return &sHookInfo;
88
}

attic/shellcode.ld

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
MEMORY {
3+
ramheader (r!wx) : ORIGIN = 0x0000, LENGTH = 1K
4+
ramnone (r!wx) : ORIGIN = 0x1000, LENGTH = 3K
5+
ramrx (rx!w) : ORIGIN = 0x2000, LENGTH = 4K
6+
ramrw (rw!x) : ORIGIN = 0x3000, LENGTH = 4K
7+
}
8+
9+
PHDRS {
10+
headers PT_PHDR PHDRS;
11+
phdr PT_LOAD FILEHDR PHDRS FLAGS(4);
12+
text PT_LOAD FLAGS(5);
13+
data PT_LOAD FLAGS(6);
14+
bss PT_LOAD FLAGS(6);
15+
dynamic PT_DYNAMIC;
16+
}
17+
18+
SECTIONS {
19+
20+
.dynamic : {
21+
*(.dynamic)
22+
} > ramnone : phdr
23+
24+
.dynsym : {
25+
*(.dynsym)
26+
} > ramnone : phdr
27+
28+
.dynstr : {
29+
*(.dynstr)
30+
} > ramnone : phdr
31+
32+
.hash : {
33+
*(.hash)
34+
} > ramnone : phdr
35+
36+
.gnu.hash : {
37+
*(.gnu.hash)
38+
} > ramnone : phdr
39+
40+
.eh_frame : {
41+
*(.eh_frame)
42+
} > ramnone : phdr
43+
44+
.text : {
45+
. = ALIGN(4096);
46+
PROVIDE( __text_start = . );
47+
*(.text.init) *(.text .text.*)
48+
PROVIDE( __text_end = . );
49+
} > ramrx : text
50+
51+
.rodata : {
52+
*(.rodata .rodata.*)
53+
} > ramrx : text
54+
55+
.data : {
56+
*(*.sdata .sdata*) *(.data .data.*)
57+
} > ramrw : data
58+
59+
.bss : {
60+
*(.sbss .sbss.*) *(.bss .bss.*)
61+
} > ramrw : bss
62+
63+
}

0 commit comments

Comments
 (0)