485 Commits

Author SHA1 Message Date
Maksim Panchenko
3a0d894faf
[BOLT] Add support for BOLT-reserved space in a binary (#90300)
Allow the user to allocate space in a binary that could be used by BOLT
for allocating new sections. The reservation is specified by two special
symbols recognizable by BOLT: __bolt_reserved_{start,end}.

The reserved space will be useful for optimizing the Linux kernel where
we cannot allocate a new executable segment. However, the support is not
limited to kernel binaries as some user-space application may find it
useful too.
2024-04-29 14:44:04 -07:00
Amir Ayupov
a1e9608b0f
[BOLT] Use symbol table info in registerFragment (#89648)
Fragment matching relies on symbol names to identify and register split
function fragments. However, as split fragments are often local symbols,
name aliasing is possible. For such cases, use symbol table to resolve
ambiguities.

This requires the presence of FILE symbols in the input binary. As BOLT
requires non-stripped binary, this is a reasonable assumption. Note that
`strip -g` removes FILE symbols by default, but `--keep-file-symbols`
can be used to preserve them.

Depends on: https://github.com/llvm/llvm-project/pull/89861

Test Plan:
Updated X86/fragment-lite.s
2024-04-29 11:14:31 -07:00
Maksim Panchenko
3ec858bc5d
[BOLT] Refactor patchELFPHDRTable() (#90290)
Mostly NFC accept for one assertion that was converted into an error.
2024-04-26 16:29:42 -07:00
Maksim Panchenko
12d322db46
[BOLT][NFC] Use getEHFrameHdrSectionName() (#90257)
Reference section name via wrapper.
2024-04-26 14:13:23 -07:00
Fangrui Song
e982032199
[BOLT,RISCV] Remove empty name special case from #68977
The special case is unneeded after #89693.

Pull Request: https://github.com/llvm/llvm-project/pull/90004
2024-04-25 20:42:40 -07:00
Amir Ayupov
5fb59e7447
[BOLT] Print program stats in perf2bolt/aggregate-only mode (#89763) 2024-04-25 19:08:51 +02:00
Amir Ayupov
090c92e015
[BOLT] Emit synthetic FILE symbol for local cold fragments of global symbols (#89794) 2024-04-25 04:53:15 +02:00
Maksim Panchenko
418e4b0c4f
[BOLT] Detect incorrect update of dynamic relocations (#89681)
When we rewrite dynamic relocations, there could be cases where they
reference code locations inside functions that were rewritten. When this
happens, we need to precisely map old address to a new one. Until we can
reliably perform the mapping, detect such condition and issue an error
refusing to write a broken binary.
2024-04-24 14:03:33 -07:00
Maksim Panchenko
0af8caeb2f
[BOLT][NFC] Remove another unused function (#89011)
RewriteInstance::isKSymtabSection() is deprecated.
2024-04-16 17:58:47 -07:00
Nathan Sidwell
bd7b170e97
[BOLT][NFC] Remove extraneous braces (#88620)
A small cleanup -- no braces needed here.
2024-04-15 13:12:53 -04:00
Nathan Sidwell
603fa4c6b9
[BOLT][NFC] Be more obvious about selecting X86 (#88527)
Use `isX86()` rather than `!isAArch64() && !isRISCV()`, and similar.
2024-04-15 13:11:29 -04:00
Nathan Sidwell
4dd20b0728
[BOLT][NFC] Refactor relocation loop (#88424)
Use the std `if () continue;` idiom before falling into the
processing.
2024-04-12 11:35:23 -04:00
Nathan Sidwell
6ec467297d
[BOLT][NFC] Adjust misleading comment & formatting (#88409)
This originally dealt with tbss, but now handles any bss-like section.
So the comment is inaccurate. Also, the `{}` on the messaging seem
unnecessary.
2024-04-12 08:34:43 -04:00
Nathan Sidwell
5bed6afc21
[BOLT][NFC] Remove unneeded if (#88322)
No need need to special-case zero. Section 0 will map to section 0.
2024-04-11 14:44:11 -04:00
Nathan Sidwell
e2d4823959
[BOLT][NFC] Make RepRet X86-specific (#88286)
Bolt's RepRet pass is x86-specific, no need to add it for non-x86
targets.
2024-04-11 06:35:28 -04:00
Nathan Sidwell
364963a0a3
[BOLT][NFC] Do not assume text section name in more places (#88303)
Fixes a couple more places where ".text" is presumed for the main
code section name.
2024-04-11 06:29:51 -04:00
Amir Ayupov
fd38366e45
[BOLT][NFC] Clean includes, add license headers (#87200) 2024-03-31 19:29:45 -07:00
Amir Ayupov
c0febca3a6
[BOLT][NFC] Refactor BC::createBinaryContext for #81346 (#87172) 2024-03-30 20:43:23 -07:00
Maksim Panchenko
7de82ca369
[BOLT] Don't terminate on trap instruction for Linux kernel (#87021)
Under normal circumstances, we terminate basic blocks on a trap
instruction. However, Linux kernel may resume execution after hitting a
trap (ud2 on x86). Thus, we introduce "--terminal-trap" option that will
specify if the trap instruction should terminate the control flow. The
option is on by default except for the Linux kernel mode when it's off.
2024-03-29 16:41:15 -07:00
Maksim Panchenko
35e7d458c9
[BOLT] Add rewriting support for Linux kernel __bug_table (#86908)
Update instruction locations in the __bug_table section after new code
is emitted. If an instruction with associated bug ID was deleted,
overwrite its location with zero.
2024-03-28 10:30:27 -07:00
Maksim Panchenko
51268a57fd
[BOLT] Enable --keep-nops option for Linux kernel by default (#86349)
Preserve nop instructions in the Linux kernel since they could be used
for runtime patching.
2024-03-22 15:29:26 -07:00
Maksim Panchenko
56197d732e
[BOLT] Skip functions with unsupported Linux kernel features (#86345)
Do not overwrite functions with alternative and paravirtual instructions
until a proper update support is implemented.
2024-03-22 15:28:54 -07:00
Alexander Yermolovich
105feb9ac6
[BOLT][DWARF] Fix handling of DW_TAG_label (#86182)
For DWARF5 BOLT was not retreiving address and instead was setting an
index.
Changed so that an address is used, and added DWARF4 test because it was
missing.
2024-03-22 13:41:27 -07:00
Kazu Hirata
4865dab04c [BOLT] Fix unused variable warnings
This patch fixes:

  bolt/lib/Rewrite/LinuxKernelRewriter.cpp:1664:20: error: unused
  variable 'TargetAddress' [-Werror,-Wunused-variable]

  bolt/lib/Rewrite/LinuxKernelRewriter.cpp:1666:20: error: unused
  variable 'KeyAddress' [-Werror,-Wunused-variable]
2024-03-21 20:21:12 -07:00
Amir Ayupov
6280681137
[BOLT] Output basic YAML profile in BAT mode
Relax assumptions that YAML output is not supported in BAT mode.
Set up basic infrastructure for emitting YAML for functions not covered
by BAT, such as from `.bolt.org.text` section (code identical to input binary
sans external refs), or non-rewritten functions in non-relocation mode (where
the function stays in the same section but BAT mapping is not emitted).

This diff only produces YAML profile for non-BAT functions (skipped,
non-simple). YAML profile for BAT functions is added in follow-up diffs:
- https://github.com/llvm/llvm-project/pull/76911 emits YAML profile with
  internal control flow information only (branch profile),
- https://github.com/llvm/llvm-project/pull/76896 adds cross-function profile
  (calls profile).

Test Plan: Added bolt/test/X86/bolt-address-translation-yaml.test

Reviewers: ayermolo, dcci, maksfb, rafaelauler

Reviewed By: rafaelauler

Pull Request: https://github.com/llvm/llvm-project/pull/76910
2024-03-21 14:32:13 -07:00
Maksim Panchenko
6b1cf00400
[BOLT] Add support for Linux kernel static keys jump table (#86090)
Runtime code modification used by static keys is the most ubiquitous
self-modifying feature of the Linux kernel. The idea is to to eliminate
the condition check and associated conditional jump on a hot path if
that condition (based on a boolean value of a static key) does not
change often. Whenever they condition changes, the kernel runtime
modifies all code paths associated with that key flipping the code
between nop and (unconditional) jump.
2024-03-21 14:05:21 -07:00
Jonas Devlieghere
41283403f5
[BOLT] Update DWARFRewriter for 32a6e9d66945 2024-03-19 12:45:31 -07:00
Kazu Hirata
8af3f74294 Revert "[BOLT] Update DIEStreamer (#85818)"
This reverts commit e4f9175d23950ecaef32db075ed47dafe3be555c.

  commit 3176c157190c80b4279dec86c4b9b84472d8ccac
  Author: Andres Villegas <andresvi@google.com>
  Date:   Tue Mar 19 10:58:31 2024 -0700

reverted 43a2ec483fe08064b53a6293682e9bab97df61a0.
2024-03-19 12:37:32 -07:00
Kazu Hirata
e4f9175d23
[BOLT] Update DIEStreamer (#85818)
commit 43a2ec483fe08064b53a6293682e9bab97df61a0
  Author: Jonas Devlieghere <jonas@devlieghere.com>
  Date:   Tue Mar 19 08:30:47 2024 -0700

removed parameter Translator from the constructor of DwarfStreamer.
This patch fixes the build by updating the constructor of DIEStreamer
accordingly.
2024-03-19 12:21:18 -07:00
Maksim Panchenko
fd32e744a5
[BOLT] Add support for Linux kernel PCI fixup section (#84982)
.pci_fixup section contains a table with entries allowing to invoke a
fixup hook whenever a problem is encountered with a PCI device. The
hookup code typically points to the start of a function. As we are not
relocating functions in the kernel (at least not yet), verify this
assumption while reading the table and ignore any functions with a fixup
code in the middle.
2024-03-12 15:52:27 -07:00
Maksim Panchenko
a9b0d7590b
[BOLT] Properly propagate Cursor errors (#84378)
Handle out-of-bounds reading errors correctly in LinuxKernelRewriter.
2024-03-07 15:29:38 -08:00
Maksim Panchenko
143afb405a
[BOLT] Add reading support for Linux kernel .altinstructions section (#84283)
Read .altinstructions and annotate instructions that have alternative
sequences with "AltInst" annotation. Note that some instructions may
have more than one alternatives, in which case they will have multiple
annotations in the form "AltInst", "AltInst2", "AltInst3", etc.
2024-03-07 13:04:02 -08:00
Maksim Panchenko
02629793a4
[BOLT] Add reading support for Linux kernel __bug_table section (#84082)
Read __bug_table section and annotate ud2 instructions with a
corresponding bug entry ID.
2024-03-06 23:34:03 -08:00
Maksim Panchenko
f51ade25b9
[BOLT] Add reading support for Linux kernel .parainstructions section (#83965)
Read .parainstruction section and mark call instructions with ParaSite
annotations.
2024-03-05 13:57:55 -08:00
Mehdi Amini
4a4fb930a5
Use the new ThreadPoolInterface base class instead of the concrete implementation (NFC) (#84056) 2024-03-05 12:37:11 -08:00
Maksim Panchenko
ccf0c8da1a
[BOLT] Add reading support for Linux kernel exception table (#83100)
Read Linux exception table and ignore functions with exceptions for now.
Proper support requires an introduction of new control flow since some
instructions with memory access can cause a control flow change.

Hence looking at disassembly or CFG with exceptions annotations is
valuable for code analysis, delay marking functions with exceptions as
non-simple until immediately before emitting the code.
2024-03-04 17:24:16 -08:00
Maksim Panchenko
7c206c7812
[BOLT] Refactor interface for instruction labels. NFCI (#83209)
To avoid accidentally setting the label twice for the same instruction,
which can lead to a "lost" label, introduce getOrSetInstLabel()
function. Rename existing functions to getInstLabel()/setInstLabel() to
make it explicit that they operate on instruction labels. Add an
assertion in setInstLabel() that the instruction did not have a prior
label set.
2024-02-27 18:44:28 -08:00
Alexander Yermolovich
6de5fcc746
[BOLT][DWARF] Add support for .debug_names (#81062)
DWARF5 spec supports the .debug_names acceleration table. This is the
formalized version of combination of gdb-index/pubnames/types. Added
implementation of it to BOLT. It supports both monolothic and split
dwarf, with and without Type Units. It does not include parent indices.
This will be in followup PR. Unlike LLVM output this will put all the
CUs and TUs into one Module.
2024-02-26 14:00:31 -08:00
Alexander Yermolovich
640e781dc8
[BOLT][DWARF][NFC] Use SkeletonCU in place of IsDWO check (#82540)
Changed isDWO to a function that checks Skeleton CU that is passed in.
This is for preparation for
https://github.com/llvm/llvm-project/pull/81062.
2024-02-21 16:18:18 -08:00
Maksim Panchenko
5daf2001a1
[BOLT] Fix memory leak in BinarySection (#82520)
The change in #80950 exposed a memory leak in BinarySection. Let
BinarySection manage memory passed via updateContents() unless a valid
SectionID is set indicating that the contents are managed by JITLink.
2024-02-21 11:54:34 -08:00
Maksim Panchenko
0ce0171243
[BOLT][NFC] Switch logging in LinuxKernelRewriter (#82195)
Use journaling streams introduced in #81524 for LinuxKernelRewriter.
2024-02-19 03:24:04 +00:00
Maksim Panchenko
2646dccaa3
[BOLT] Add support for Linux kernel static calls table (#82072)
Static calls are calls that are getting patched during runtime. Hence,
for every such call the kernel runtime needs the location of the call or
jmp instruction that will be patched. Instruction locations together
with a corresponding key are stored in the static call site table. As
BOLT rewrites these instructions it needs to update the table.
2024-02-18 17:20:25 -08:00
Alexander Yermolovich
f81f7a5766
[BOLT][DWARF] Remove redundant code (#82118)
Removed some redundant code. Should be NFC change.
2024-02-17 12:37:07 -08:00
Maksim Panchenko
5a82daafc1
[BOLT][NFC] Remove redundant assertion (#82056)
processLKSections() used to be a member of RewriteInstance. Since now it
is part of the LinuxKernelRewriter, the assertion is no longer needed.
2024-02-16 15:37:54 -08:00
Maksim Panchenko
5a29887145
[BOLT] Add writing support for Linux kernel ORC (#80950)
Update ORC information based on the new code layout and emit
corresponding ORC sections for the Linux kernel.

We rewrite ORC sections in place, which puts a limit on the size of new
section contents. Since ORC info changes for the new code layout and the
number of ORC entries can become larger, we free up space in the tables
by removing redundant ORC terminators. As a result, we effectively emit
fewer entries and have to add duplicate terminators at the end to match
the original section sizes. Ideally, we need to update ORC boundaries to
reflect the reduced size and optimize runtime lookup, but we will need
relocations for this, and the benefits will be marginal, if any.
2024-02-16 14:25:59 -08:00
Alexander Yermolovich
5ff8b30327
[BOLT][DWARF] Do not emit zero low_pc address arange (#81955)
According to DWARF spec zero entires indicate end of arange. Changed so
that BOLT does not emit zero low_pc arange.
2024-02-16 11:23:28 -08:00
Amir Ayupov
d2c9a19dd8
[BOLT][NFC] Pass BF/BB hashes to BAT
Test Plan: NFC

Reviewers: dcci, rafaelauler, maksfb, ayermolo

Reviewed By: rafaelauler

Pull Request: https://github.com/llvm/llvm-project/pull/76906
2024-02-15 12:49:43 -08:00
Alexander Yermolovich
82ca752393
[BOLT][DWARF] Add test for DW_AT_ranges input without function output (#81794)
Added a test that relies on -fbasic-block-sections=all and --gc-sections
that exercises a code path that previously printed a warning.
2024-02-14 15:43:39 -08:00
Amir Ayupov
52cf07116b
[BOLT][NFC] Log through JournalingStreams (#81524)
Make core BOLT functionality more friendly to being used as a
library instead of in our standalone driver llvm-bolt. To
accomplish this, we augment BinaryContext with journaling streams
that are to be used by most BOLT code whenever something needs to
be logged to the screen. Users of the library can decide if logs
should be printed to a file, no file or to the screen, as
before. To illustrate this, this patch adds a new option
`--log-file` that allows the user to redirect BOLT logging to a
file on disk or completely hide it by using
`--log-file=/dev/null`. Future BOLT code should now use
`BinaryContext::outs()` for printing important messages instead of
`llvm::outs()`. A new test log.test enforces this by verifying that
no strings are print to screen once the `--log-file` option is
used.

In previous patches we also added a new BOLTError class to report
common and fatal errors, so code shouldn't call exit(1) now. To
easily handle problems as before (by quitting with exit(1)),
callers can now use
`BinaryContext::logBOLTErrorsAndQuitOnFatal(Error)` whenever code
needs to deal with BOLT errors. To test this, we have fatal.s
that checks we are correctly quitting and printing a fatal error
to the screen.

Because this is a significant change by itself, not all code was
yet ported. Code from Profiler libs (DataAggregator and friends)
still print errors directly to screen.

Co-authored-by: Rafael Auler <rafaelauler@fb.com>

Test Plan: NFC
2024-02-12 14:53:53 -08:00
Amir Ayupov
13d60ce2f2
[BOLT][NFC] Propagate BOLTErrors from Core, RewriteInstance, and passes (2/2) (#81523)
As part of the effort to refactor old error handling code that
would directly call exit(1), in this patch continue the migration
on libCore, libRewrite and libPasses to use the new BOLTError
class whenever a failure occurs.

Test Plan: NFC

Co-authored-by: Rafael Auler <rafaelauler@fb.com>
2024-02-12 14:51:15 -08:00