Skip to content

Instantly share code, notes, and snippets.

@justinian
Last active August 11, 2023 04:18
Show Gist options
  • Save justinian/ed2b2b850051135a51a3d38888935f20 to your computer and use it in GitHub Desktop.
Save justinian/ed2b2b850051135a51a3d38888935f20 to your computer and use it in GitHub Desktop.
Clang and _GLOBAL_OFFSET_TABLE_
unsigned long SOME_GLOBAL = 123;
extern unsigned long _GLOBAL_OFFSET_TABLE_ [];
extern unsigned long SOME_GLOBAL;
int main() {
auto foo1 = &_GLOBAL_OFFSET_TABLE_;
auto foo2 = _GLOBAL_OFFSET_TABLE_;
auto bar1 = &SOME_GLOBAL;
auto bar2 = SOME_GLOBAL;
return 0;
}
#CC = /home/justin/.local/lib/jsix/toolchains/llvm-13/bin/clang++
#LD = /home/justin/.local/lib/jsix/toolchains/llvm-13/bin/ld.lld
#CC = g++
#LD = ld
#CC = clang++-13
#LD = clang++-13
#LD = ld.lld-13
CC = clang++-16
LD = ld.lld-16
got: foo.o bar.o
${LD} -pie -o $@ $^
%.o: %.cpp
${CC} -c -g -fpie -o $@ $^
dump: got
${CC} --version
objdump -M intel -DS -j .text $^
clean:
-rm *.o got
.PHONY: rip clean
g++ -c -g -fpie -o foo.o foo.cpp
g++ -c -g -fpie -o bar.o bar.cpp
ld -pie -o got foo.o bar.o
g++ --version
g++ (Debian 12.3.0-5) 12.3.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
objdump -M intel -DS -j .text got
got: file format elf64-x86-64
Disassembly of section .text:
0000000000001000 <main>:
extern unsigned long _GLOBAL_OFFSET_TABLE_ [];
extern unsigned long SOME_GLOBAL;
int main() {
1000: 55 push rbp
1001: 48 89 e5 mov rbp,rsp
auto foo1 = &_GLOBAL_OFFSET_TABLE_;
1004: 48 8d 05 dd 2f 00 00 lea rax,[rip+0x2fdd] # 3fe8 <_GLOBAL_OFFSET_TABLE_>
100b: 48 89 45 f8 mov QWORD PTR [rbp-0x8],rax
auto foo2 = _GLOBAL_OFFSET_TABLE_;
100f: 48 8d 05 d2 2f 00 00 lea rax,[rip+0x2fd2] # 3fe8 <_GLOBAL_OFFSET_TABLE_>
1016: 48 89 45 f0 mov QWORD PTR [rbp-0x10],rax
auto bar1 = &SOME_GLOBAL;
101a: 48 8d 05 df 2f 00 00 lea rax,[rip+0x2fdf] # 4000 <SOME_GLOBAL>
1021: 48 89 45 e8 mov QWORD PTR [rbp-0x18],rax
auto bar2 = SOME_GLOBAL;
1025: 48 8b 05 d4 2f 00 00 mov rax,QWORD PTR [rip+0x2fd4] # 4000 <SOME_GLOBAL>
102c: 48 89 45 e0 mov QWORD PTR [rbp-0x20],rax
return 0;
1030: b8 00 00 00 00 mov eax,0x0
}
1035: 5d pop rbp
1036: c3 ret
clang++-16 -c -g -fpie -o foo.o foo.cpp
clang++-16 -c -g -fpie -o bar.o bar.cpp
ld.lld-16 -pie -o got foo.o bar.o
clang++-16 --version
Debian clang version 16.0.6 (6)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
objdump -M intel -DS -j .text got
got: file format elf64-x86-64
Disassembly of section .text:
0000000000001290 <main>:
extern unsigned long _GLOBAL_OFFSET_TABLE_ [];
extern unsigned long SOME_GLOBAL;
int main() {
1290: 55 push rbp
1291: 48 89 e5 mov rbp,rsp
1294: c7 45 fc 00 00 00 00 mov DWORD PTR [rbp-0x4],0x0
auto foo1 = &_GLOBAL_OFFSET_TABLE_;
129b: 48 8b 05 c6 20 00 00 mov rax,QWORD PTR [rip+0x20c6] # 3368 <_GLOBAL_OFFSET_TABLE_>
12a2: 48 89 45 f0 mov QWORD PTR [rbp-0x10],rax
auto foo2 = _GLOBAL_OFFSET_TABLE_;
12a6: 48 8b 05 bb 20 00 00 mov rax,QWORD PTR [rip+0x20bb] # 3368 <_GLOBAL_OFFSET_TABLE_>
12ad: 48 89 45 e8 mov QWORD PTR [rbp-0x18],rax
auto bar1 = &SOME_GLOBAL;
12b1: 48 8d 05 a8 20 00 00 lea rax,[rip+0x20a8] # 3360 <SOME_GLOBAL>
12b8: 48 89 45 e0 mov QWORD PTR [rbp-0x20],rax
auto bar2 = SOME_GLOBAL;
12bc: 48 8d 05 9d 20 00 00 lea rax,[rip+0x209d] # 3360 <SOME_GLOBAL>
12c3: 48 8b 00 mov rax,QWORD PTR [rax]
12c6: 48 89 45 d8 mov QWORD PTR [rbp-0x28],rax
return 0;
12ca: 31 c0 xor eax,eax
12cc: 5d pop rbp
12cd: c3 ret
12ce: cc int3
12cf: cc int3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment