[all-commits] [llvm/llvm-project] 96b5e0: [BOLT] Support instrumentation hook via DT_FINI_AR...

Job Noorman via All-commits all-commits at lists.llvm.org
Wed Nov 8 03:01:24 PST 2023


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 96b5e092dcd624b494d769e47341e50b1028972e
      https://github.com/llvm/llvm-project/commit/96b5e092dcd624b494d769e47341e50b1028972e
  Author: Job Noorman <jnoorman at igalia.com>
  Date:   2023-11-08 (Wed, 08 Nov 2023)

  Changed paths:
    M bolt/include/bolt/Core/BinaryContext.h
    M bolt/include/bolt/Core/BinarySection.h
    M bolt/include/bolt/Rewrite/RewriteInstance.h
    M bolt/lib/Core/Relocation.cpp
    M bolt/lib/Rewrite/RewriteInstance.cpp
    M bolt/lib/RuntimeLibs/InstrumentationRuntimeLibrary.cpp
    A bolt/test/AArch64/hook-fini.s
    A bolt/test/runtime/AArch64/hook-fini.test

  Log Message:
  -----------
  [BOLT] Support instrumentation hook via DT_FINI_ARRAY (#67348)

BOLT currently hooks its its instrumentation finalization function via
`DT_FINI`. However, this method of calling finalization routines is not
supported anymore on newer ABIs like RISC-V. `DT_FINI_ARRAY` is
preferred there.

This patch adds support for hooking into `DT_FINI_ARRAY` instead if the
binary does not have a `DT_FINI` entry. If it does, `DT_FINI` takes
precedence so this patch should not change how the currently supported
instrumentation targets behave.

`DT_FINI_ARRAY` points to an array in memory of `DT_FINI_ARRAYSZ` bytes.
It consists of pointer-length entries that contain the addresses of
finalization functions. However, the addresses are only filled-in by the
dynamic linker at load time using relative relocations. This makes
hooking via `DT_FINI_ARRAY` a bit more complicated than via `DT_FINI`.

The implementation works as follows:
- While scanning the binary: find the section where `DT_FINI_ARRAY`
points to, read its first dynamic relocation and use its addend to find
the address of the fini function we will use to hook;
- While writing the output file: overwrite the addend of the dynamic
relocation with the address of the runtime library's fini function.

Updating the dynamic relocation required a bit of boiler plate: since
dynamic relocations are stored in a `std::multiset` which doesn't
support getting mutable references to its items, functions were added to
`BinarySection` to take an existing relocation and insert a new one.




More information about the All-commits mailing list