[PATCH] D129107: [BOLT][HUGIFY] adds huge pages support of PIE/no-PIE binaries

Alexey Moksyakov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 19 02:41:19 PDT 2022


yavtuk added a comment.

Hello Rafael, I am little bit confused with clang and pie binaries,
I use gcc mostly, my local env:
gcc (GCC) 7.3.0
linux 4.18.0 x86_64

cat test.c

#include <stdio.h>
#include <dlfcn.h>
#include <unistd.h>
#include <stdint.h>
#include "foo.h"

int main(void) {

  void (*foo_func)(uint64_t);
  
  void *fd = dlopen("./libfoo.so", RTLD_LAZY);
  if (!fd) {
      printf("libfoo.so load failed\n");
      return -1;
  }
  foo_func = dlsym(fd, "foo");
  if (!foo_func) {
      printf("function loading is failed\n");
      return -1;
  }
  
  for(size_t i = 0; i < 3; ++i) {
      foo_func(i);
  }
  
  dlclose(fd);
  return 0;

}

----------------------------------

cat foo.c

cat foo.c
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>

__attribute__((constructor)) void foo_init(void) {
	printf("foo_init ctr\n");
}

__attribute__((destructor)) void foo_fini(void) {
	printf("foo_init destr\n");
}

void foo(uint64_t num) {
	printf("iter %lld\n", num);
}

-------------------------------------------

cat foo.h

#ifndef FOO_H
#define FOO_H

void foo(uint64_t);

#endif //FOO_H

-------------------------------------------

$CC foo.c -O0 -g3 -fPIC -Wl,--emit-relocs -shared -o libfoo.so
$CC test.c -O0 -g3 -fPIE  -I. -Wl,-pie -Wl,--emit-relocs -ldl -Wl,-pie -o test_pie

llvm-bolt ./test_pie -instrument -o ./test_pie.inst -instrumentation-file=./test_pie.fdata -instrumentation-no-counters-clear -instrumentation-sleep-time=1

./test_pie.inst

llvm-bolt -hugify ./test_pie -o ./test_pie.hugify -data=./test_pie.fdata

file ./test_pie.hugify 
./test_pie.hugify: ELF 64-bit LSB shared object ....

./test_pie.hugify 
[hugify] hot start: 55a316b65000
[hugify] hot end: 55a316b65218
[hugify] aligned huge page from: 55a316a00000
[hugify] aligned huge page to: 55a316c00000
[hugify] workaround with memory alignment for kernel < 5.10
[hugify] allocated temporary space: 7f8321362000
foo_init ctr
iter 0
iter 1
iter 2
foo_init destr

As far as you can see the hot start address (0x55a316b65000) is 4KB aligned,
adding extra padding from left and right sides allows remap it correctly in runtime to
0x55a316a00000 address.

That why I need 4 arguments for hugifyForOldKernel(HotStart, HotEnd, AlignedFrom, AlignedTo)

HotStart, HotEnd are used to get real .text section size and copy it to temp memory, 0x218 bytes

AlignedFrom, AlignedTo are used to get the size from new area with 2MB address alignment,
it's needed because OS doesn't take huge page without it

I am just trying to reproduce the same for clang but can't get the output like this
[hugify] hot start: 55a316b65000
[hugify] hot end: 55a316b65218

I get the SEG_MAPPER error for src address during memcpy to temp area.

Can you give me piece of advice to do you get pie binaries using clang?
I would be appreciated for any help, thanks in advance.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D129107/new/

https://reviews.llvm.org/D129107



More information about the llvm-commits mailing list