Skip to content

Instantly share code, notes, and snippets.

@rprichard
Created July 30, 2019 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rprichard/e5ad0cfa10f6b75925ded3709117500f to your computer and use it in GitHub Desktop.
Save rprichard/e5ad0cfa10f6b75925ded3709117500f to your computer and use it in GitHub Desktop.
Demonstrate misalignment when (p_vaddr % p_align) != 0
#!/bin/bash
CLANG=/x/llvm-upstream/stage1/bin/clang
$CLANG -target aarch64-linux-gnu -fpic -shared libtls.c -fuse-ld=lld -o libtls.so
$CLANG -target aarch64-linux-gnu main.c -ldl -o main -Wl,-rpath,'$ORIGIN'
#include <stdio.h>
__thread char tlsvar1[1] = { 1 };
__thread char tlsvar2[0x100] __attribute__((aligned(0x100)));
void dump(void) {
printf("&tlsvar1 = %p\n", &tlsvar1);
printf("&tlsvar2 = %p\n", &tlsvar2);
}
#include <assert.h>
#include <dlfcn.h>
int main() {
void* lib = dlopen("libtls.so", RTLD_NOW);
assert(lib);
void (*dump)(void) = dlsym(lib, "dump");
assert(dump);
dump();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment