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

Kristof Beyls via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 11 07:05:29 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};
+
----------------
kbeyls wrote:

I tried out adding the `BoltCategory` too.
Before doing so, the output of `./bin/llvm-bolt-binary-analysis --help` is:
```
OVERVIEW: BinaryAnalysis

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

OPTIONS:

Generic Options:

  --help      - Display available options (--help-hidden for more)
  --help-list - Display list of available options (--help-list-hidden for more)
  --version   - Display the version of this program
```

After doing so, the output for the same command is:
```
OVERVIEW: BinaryAnalysis

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

OPTIONS:

BOLT generic options:

  --bolt-id=<string>                            - add any string to tag this execution in the output binary via bolt info section
  --create-debug-names-section                  - Creates .debug_names section, if the input binary doesn't have it already, for DWARF5 CU/TUs.
  --debug-thread-count=<uint>                   - specifies thread count for the multithreading for updating DWO debug info
  --dump-cg=<string>                            - dump callgraph to the given file
  --dwarf-output-path=<string>                  - Path to where .dwo files will be written out to.
  --dyno-stats                                  - print execution info based on profile
  --enable-bat                                  - write BOLT Address Translation tables
  --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
  --lite                                        - skip processing of cold functions
  --no-threads                                  - disable multithreading
  --print-profile-stats                         - print profile quality/bias analysis
  --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
  --strict                                      - trust the input to be from a well-formed source
  --tasks-per-thread=<uint>                     - number of tasks to be created per thread
  --thread-count=<uint>                         - number of threads
  --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:

  --help                                        - Display available options (--help-hidden for more)
  --help-list                                   - Display list of available options (--help-list-hidden for more)
  --version                                     - Display the version of this program
```

It seems the majority of the options in the `BoltCategory` aren't applicable (at the moment).
But it is a good point that some options are applicable/useful, so ought to be shown with `--help`. I guess this will require splitting the `BoltCategory` into 2 sets: one with options useful for both `llvm-bolt` and `llvm-bolt-binary-analysis` and one with options that are only useful for `llvm-bolt`.
I think doing so is best left for a separate PR though....
Maybe we should create an issue with the "good first issue" label for this?

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


More information about the llvm-commits mailing list