[llvm] [BOLT] Introduce binary analysis tool based on BOLT (PR #115330)

Anatoly Trosinenko via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 23 03:41:47 PST 2024


================
@@ -0,0 +1,122 @@
+//===- bolt/tools/binary-analysis/binary-analysis.cpp ---------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This is a generic binary analysis tool, where multiple different specific
+// binary analyses can be plugged in to. The binary analyses are mostly built
+// on top of BOLT components.
+//
+//===----------------------------------------------------------------------===//
+
+#include "bolt/Rewrite/RewriteInstance.h"
+#include "bolt/Utils/CommandLineOpts.h"
+#include "llvm/MC/TargetRegistry.h"
+#include "llvm/Object/Binary.h"
+#include "llvm/Object/ELFObjectFile.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/Errc.h"
+#include "llvm/Support/ManagedStatic.h"
+#include "llvm/Support/PrettyStackTrace.h"
+#include "llvm/Support/Program.h"
+#include "llvm/Support/Signals.h"
+#include "llvm/Support/TargetSelect.h"
+#include "llvm/Support/VirtualFileSystem.h"
+
+#define DEBUG_TYPE "bolt"
+
+using namespace llvm;
+using namespace object;
+using namespace bolt;
+
+namespace opts {
+
+static cl::OptionCategory *BinaryAnalysisCategories[] = {
+    &BinaryAnalysisCategory};
+
----------------
atrosinenko wrote:

Interestingly, it looks like most of the options relevant for the analysis use-case are only printed with `--help-hidden` instead of `--help`:

```
OVERVIEW: BinaryAnalysis

USAGE: llvm-bolt-binary-analysis [options] <executable>

OPTIONS:

BOLT generic options:

  --align-text=<uint>                                       - alignment of .text section
  --allow-stripped                                          - allow processing of stripped binaries
  --alt-inst-feature-size=<uint>                            - size of feature field in .altinstructions
  --alt-inst-has-padlen                                     - specify that .altinstructions has padlen field
  --asm-dump[=<dump folder>]                                  - dump function into assembly
  --bolt-id=<string>                                        - add any string to tag this execution in the output binary via bolt info section
  --break-funcs=<func1,func2,func3,...>                     - list of functions to core dump on (debugging)
  --check-encoding                                          - perform verification of LLVM instruction encoding/decoding. Every instruction in the input is decoded and re-encoded. If the resulting bytes do not match the input, a warning message is printed.
  --comp-dir-override=<string>                              - overrides DW_AT_comp_dir, and provides an alternative base location, which is used with DW_AT_dwo_name to construct a path to *.dwo files.
  --compact-code-model                                      - generate code for binaries <128MB on AArch64
  --create-debug-names-section                              - Creates .debug_names section, if the input binary doesn't have it already, for DWARF5 CU/TUs.
  --cu-processing-batch-size=<uint>                         - Specifies the size of batches for processing CUs. Higher number has better performance, but more memory usage. Default value is 1.
  --debug-skeleton-cu                                       - prints out offsets for abbrev and debug_info of Skeleton CUs that get patched.
  --debug-thread-count=<uint>                               - specifies thread count for the multithreading for updating DWO debug info
  --dot-tooltip-code                                        - add basic block instructions as tool tips on nodes
  --dump-alt-instructions                                   - dump Linux alternative instructions info
  --dump-cg=<string>                                        - dump callgraph to the given file
  --dump-data                                               - dump parsed bolt data for debugging
  --dump-dot-all                                            - dump function CFGs to graphviz format after each stage;enable '-print-loops' for color-coded blocks
  --dump-linux-exceptions                                   - dump Linux kernel exception table
  --dump-orc                                                - dump raw ORC unwind information (sorted)
  --dump-para-sites                                         - dump Linux kernel paravitual patch sites
  --dump-pci-fixups                                         - dump Linux kernel PCI fixup table
  --dump-smp-locks                                          - dump Linux kernel SMP locks
  --dump-static-calls                                       - dump Linux kernel static calls
  --dump-static-keys                                        - dump Linux kernel static keys jump table
  --dwarf-output-path=<string>                              - Path to where .dwo files will be written out to.
  --dwp=<string>                                            - Path and name to DWP file.
  --dyno-stats                                              - print execution info based on profile
  --dyno-stats-all                                          - print dyno stats after each stage
  --dyno-stats-scale=<uint>                                 - scale to be applied while reporting dyno stats
  --enable-bat                                              - write BOLT Address Translation tables
  --force-data-relocations                                  - force relocations to data sections to always be processed
  --force-patch                                             - force patching of original entry points
  --funcs=<func1,func2,func3,...>                           - limit optimizations to functions from the list
  --funcs-file=<string>                                     - file with list of functions to optimize
  --funcs-file-no-regex=<string>                            - file with list of functions to optimize (non-regex)
  --funcs-no-regex=<func1,func2,func3,...>                  - limit optimizations to functions from the list (non-regex)
  --hot-data                                                - hot data symbols support (relocation mode)
  --hot-functions-at-end                                    - if reorder-functions is used, order functions putting hottest last
  --hot-text                                                - Generate hot text symbols. Apply this option to a precompiled binary that manually calls into hugify, such that at runtime hugify call will put hot code into 2M pages. This requires relocation.
  --hot-text-move-sections=<sec1,sec2,sec3,...>             - list of sections containing functions used for hugifying hot text. BOLT makes sure these functions are not placed on the same page as the hot text. (default='.stub,.mover').
  --insert-retpolines                                       - run retpoline insertion pass
  --keep-aranges                                            - keep or generate .debug_aranges section if .gdb_index is written
  --keep-tmp                                                - preserve intermediate .o file
  --lite                                                    - skip processing of cold functions
  --long-jump-labels                                        - always use long jumps/nops for Linux kernel static keys
  --max-data-relocations=<uint>                             - maximum number of data relocations to process
  --max-funcs=<uint>                                        - maximum number of functions to process
  --no-huge-pages                                           - use regular size pages for code alignment
  --no-threads                                              - disable multithreading
  --pad-funcs=<func1:pad1,func2:pad2,func3:pad3,...>        - list of functions to pad with amount of bytes
  --pad-funcs-before=<func1:pad1,func2:pad2,func3:pad3,...> - list of functions to pad with amount of bytes
  --print-aliases                                           - print aliases when printing objects
  --print-all                                               - print functions after each stage
  --print-cfg                                               - print functions after CFG construction
  --print-debug-info                                        - print debug info when printing functions
  --print-disasm                                            - print function after disassembly
  --print-dyno-opcode-stats=<uint>                          - print per instruction opcode dyno stats and the functionnames:BB offsets of the nth highest execution counts
  --print-dyno-stats-only                                   - while printing functions output dyno-stats and skip instructions
  --print-exceptions                                        - print exception handling data
  --print-globals                                           - print global symbols after disassembly
  --print-jump-tables                                       - print jump tables
  --print-loops                                             - print loop related information
  --print-mem-data                                          - print memory data annotations when printing functions
  --print-normalized                                        - print functions after CFG is normalized
  --print-only=<func1,func2,func3,...>                      - list of functions to print
  --print-orc                                               - print ORC unwind information for instructions
  --print-profile                                           - print functions after attaching profile
  --print-profile-stats                                     - print profile quality/bias analysis
  --print-pseudo-probes=<value>                             - print pseudo probe info
    =decode                                                 -   decode probes section from binary
    =address_conversion                                     -   update address2ProbesMap with output block address
    =encoded_probes                                         -   display the encoded probes in binary section
    =all                                                    -   enable all debugging printout
  --print-relocations                                       - print relocations when printing functions/objects
  --print-reordered-data                                    - print section contents after reordering
  --print-retpoline-insertion                               - print functions after retpoline insertion pass
  --print-sdt                                               - print all SDT markers
  --print-sections                                          - print all registered sections
  --print-unknown                                           - print names of functions with unknown control flow
  --profile-format=<value>                                  - format to dump profile output in aggregation mode, default is fdata
    =fdata                                                  -   offset-based plaintext format
    =yaml                                                   -   dense YAML representation
  --r11-availability=<value>                                - determine the availability of r11 before indirect branches
    =never                                                  -   r11 not available
    =always                                                 -   r11 available before calls and jumps
    =abi                                                    -   r11 available before calls but not before jumps
  --relocs                                                  - use relocations in the binary (default=autodetect)
  --remove-symtab                                           - Remove .symtab section
  --reorder-skip-symbols=<symbol1,symbol2,symbol3,...>      - list of symbol names that cannot be reordered
  --reorder-symbols=<symbol1,symbol2,symbol3,...>           - list of symbol names that can be reordered
  --retpoline-lfence                                        - determine if lfence instruction should exist in the retpoline
  --skip-funcs=<func1,func2,func3,...>                      - list of functions to skip
  --skip-funcs-file=<string>                                - file with list of functions to skip
  --strict                                                  - trust the input to be from a well-formed source
  --tasks-per-thread=<uint>                                 - number of tasks to be created per thread
  --terminal-trap                                           - Assume that execution stops at trap instruction
  --thread-count=<uint>                                     - number of threads
  --time-build                                              - print time spent constructing binary functions
  --time-rewrite                                            - print time spent in rewriting passes
  --top-called-limit=<uint>                                 - maximum number of functions to print in top called functions section
  --trap-avx512                                             - in relocation mode trap upon entry to any function that uses AVX-512 instructions
  --trap-old-code                                           - insert traps in old function bodies (relocation mode)
  --update-debug-sections                                   - update DWARF debug sections of the executable
  --use-gnu-stack                                           - use GNU_STACK program header for new segment (workaround for issues with strip/objcopy)
  --use-old-text                                            - re-use space in old .text if possible (relocation mode)
  -v <uint>                                                 - set verbosity level for diagnostic output

Generic Options:

  -h                                                        - Alias for --help
  --help                                                    - Display available options (--help-hidden for more)
  --help-hidden                                             - Display all available options
  --help-list                                               - Display list of available options (--help-list-hidden for more)
  --help-list-hidden                                        - Display list of all available options
  --print-all-options                                       - Print all option values after command line parsing
  --print-options                                           - Print non-default options after command line parsing
  --version                                                 - Display the version of this program
```

Maybe creating an issue is a good idea - it should be rather straightforward to implement this but still requires some amount of familiarization with BOLT to better understand which option is relevant for the "read-only" use case and which only influences the code generation.

https://github.com/llvm/llvm-project/pull/115330


More information about the llvm-commits mailing list