[RFC][PATCH v4 00/32] objtool: Make recordmcount a subcommand

From: Matt Helsley
Date: Tue Jun 02 2020 - 15:50:51 EST

recordmcount has its own ELF wrapper code and could utilize
objtool's ELF code to more-portably handle architecture variations.
This series makes recordmcount a subcommand of objtool. It very
gradually convert recordmcount to become a subcommand of objtool and
then reuses parts of objtool's ELF code. recordmcount maps the file in
and collects simple information it needs to append a section to the
object file. The only part of the original file it modifies is the
address of new section tables -- interestingly enough this
resembles RCU in that we don't really trim the old tables so
much as unlink them via a critical offset and then rely on
future tooling, in this case, to drop the unused bits. Much of
the recordmcount ELF code is only reading and walking the data
structures to collect the mcount locations it records in a separate
area of memory. This means it's safe to mix access to the mapped
file with access to the objtool-style linked data
structures as we gradually convert it to using only the linked data
structures. Once the old ELF code is no longer in use we can drop it
and use objtool to take over the task of writing the results without
using the RCU-like trick any more.


I've been using scripts to test cross compilation and execution of
objtool, and mcount on objects built for x86, ppc64le, arm64, s390, and
sparc. I used PowerPC as a sample arch for fixing a bug (see Changes)
and confirmed it builds a full zImage with defconfig

Split out recordmcount cleanups and upstreamed.
[ https://lore.kernel.org/lkml/20190802134712.2d8cc63f@xxxxxxxxxxxxxxxxxx/ ]

Split out and iterated on objtool multi-arch support.
[ https://lore.kernel.org/lkml/cover.1586468801.git.mhelsley@xxxxxxxxxx/ ]

Split out expanded relocation support, renamed types, and functions
to reflect expanded relocation support, and posted.
[ https://lore.kernel.org/lkml/cover.1590785960.git.mhelsley@xxxxxxxxxx/ ]

This set is based on the patches sent upstream and posted above.

Adapted to renames by Ingo and Peter: s/elf_open/elf_open_read/

Added weak symbols for mcount subcommand
This nicely eliminated the need for the mcount.h header.

Added tools/objtool/Makefile per-arch SUBCMD_ blocks for each
arch recordmcount / mcount supports.

Moved ftrace/mcount/record.h from objtool_dep to recordmcount_dep
This keeps the dependencies better organized.

Fixed Makefile issue reported for PowerPC and a couple other archs
by kbuild test robot. The always-$(BUILD_C_RECORDMCOUNT)
line wasn't sufficiently replaced. Added to prepare-objtool
target in top level Makefile.

Split up dependencies to be independent of CONFIG_STACK_VALIDATION
and CONFIG_UNWINDER_ORC since these are x86-specific.
Now any arch which uses the C version of recordmcount
will build objtool if dynamic tracing is enabled.

Added a second rename at the end to be consistent with other
objtool subcommands.

Rebased on mainline. s/elf_open/elf_read/ in recordmcount.c

Fix whitespace before line continuation

Add ftrace/mcount/record.h to objtool_dep

Rename the Makefile variable BUILD_C_RECORDMCOUNT to
better reflect its purpose

Similar: rename recordmcount_source => recordmcount_dep
When using objtool we can just depend on the
binary rather than the source the binary is
built from. This should address Josh's feedback and
make the Makefile code a bit clearer

Add a comment to make reading the Makefile a little

Rebased to latest mainline -rc

Matt Helsley (32):
objtool: Prepare to merge recordmcount
objtool: Make recordmcount into mcount subcmd
objtool: recordmcount: Start using objtool's elf wrapper
objtool: recordmcount: Search for __mcount_loc before walking the
objtool: recordmcount: Convert do_func() relhdrs
objtool: mcount: Remove unused fname parameter
objtool: mcount: Use libelf for section header names
objtool: mcount: Walk objtool Elf structs in find_secsym_ndx
objtool: mcount: Use symbol structs to find mcount relocations
objtool: mcount: Walk relocation lists
objtool: mcount: Move get_mcountsym
objtool: mcount: Replace MIPS offset types
objtool: mcount: Move is_fake_mcount()
objtool: mcount: Stop using ehdr in find_section_sym_index
objtool: mcount: Move find_section_sym_index()
objtool: mcount: Restrict using ehdr in append_func()
objtool: mcount: Use objtool ELF to write
objtool: mcount: Move nop_mcount()
objtool: mcount: Move has_rel_mcount() and tot_relsize()
objtool: mcount: Move relocation entry size detection
objtool: mcount: Only keep ELF file size
objtool: mcount: Use ELF header from objtool
objtool: mcount: Remove unused file mapping
objtool: mcount: Reduce usage of _size wrapper
objtool: mcount: Move mcount_adjust out of wrapper
objtool: mcount: Pre-allocate new ELF sections
objtool: mcount: Generic location and relocation table types
objtool: mcount: Move sift_rel_mcount out of wrapper file
objtool: mcount: Remove wrapper for ELF relocation type
objtool: mcount: Remove wrapper double-include trick
objtool: mcount: Remove endian wrappers
objtool: mcount: Rename

Documentation/dontdiff | 2 +-
Documentation/trace/ftrace-design.rst | 4 +-
Documentation/trace/ftrace.rst | 6 +-
Makefile | 24 +-
arch/arm64/include/asm/ftrace.h | 2 +-
arch/x86/include/asm/ftrace.h | 2 +-
kernel/trace/Kconfig | 2 +-
scripts/.gitignore | 1 -
scripts/Makefile | 1 -
scripts/Makefile.build | 22 +-
scripts/recordmcount.c | 663 --------------------
scripts/recordmcount.h | 606 ------------------
scripts/sorttable.h | 2 +-
tools/objtool/.gitignore | 1 +
tools/objtool/Build | 2 +
tools/objtool/Makefile | 37 +-
tools/objtool/builtin-mcount.c | 50 ++
tools/objtool/builtin.h | 2 +
tools/objtool/elf.c | 5 +-
tools/objtool/elf.h | 2 +
tools/objtool/mcount.c | 682 +++++++++++++++++++++
tools/objtool/objtool.c | 1 +
tools/objtool/objtool.h | 1 +
{scripts => tools/objtool}/recordmcount.pl | 0
tools/objtool/weak.c | 5 +
25 files changed, 821 insertions(+), 1304 deletions(-)
delete mode 100644 scripts/recordmcount.c
delete mode 100644 scripts/recordmcount.h
create mode 100644 tools/objtool/builtin-mcount.c
create mode 100644 tools/objtool/mcount.c
rename {scripts => tools/objtool}/recordmcount.pl (100%)

base-commit: 2160e6958fc4fa0a70d5f8a2f32760c7ab3bfd6c