Skip to content

Instantly share code, notes, and snippets.

@8dcc
Created April 26, 2024 19:48
Show Gist options
  • Save 8dcc/0fe08d85255b374fed28497b2dbfb39a to your computer and use it in GitHub Desktop.
Save 8dcc/0fe08d85255b374fed28497b2dbfb39a to your computer and use it in GitHub Desktop.
Assembly PIC test
#include <stdio.h>
/* Assembly function, calls foo() */
extern int foo_proxy(int a, int b);
int myGlobalInteger = 0;
int foo(int a, int b) {
return a + b;
}
int main(void) {
int result = foo_proxy(5, 6);
printf("result: %d\n", result);
printf("global: %d\n", myGlobalInteger);
return 0;
}
CC=gcc
CFLAGS=-Wall -Wextra -Ofast -fPIC
LDFLAGS=
AS=nasm
ASFLAGS=-f elf64
.PHONY: all clean
all: asm-pic-test.out
clean:
rm -f asm-pic-test.out main.o test.o
asm-pic-test.out: main.o test.o
$(CC) $(CFLAGS) -shared -o $@ $^ $(LDFLAGS)
main.o: main.c
$(CC) $(CFLAGS) -c -o $@ $<
test.o: test.asm
$(AS) $(ASFLAGS) -o $@ $<
; Same output with and without this line
default REL
section .text
extern myGlobalInteger
extern foo
global foo_proxy
foo_proxy:
mov [myGlobalInteger], rdi
jmp foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment