objtool query: section start/end symbols?

From: Linus Torvalds
Date: Thu Jun 06 2024 - 14:43:07 EST


So this is related to my currently very ugly hack at

https://lore.kernel.org/all/CAHk-=whFSz=usMPHHGAQBnJRVAFfuH4gFHtgyLe0YET75zYRzA@xxxxxxxxxxxxxx/

where I'm trying to do "runtime constants". That patch actually works,
but it's flawed in many ways, and one of the ways it is flawed is that
I really want to put the "this is the use for symbol X" in a section
of its own for each X.

Now, creating the sections is trivial, that's not the problem. I'd
just make the asm do

".pushsection .static_const." #sym ",\"a\"\n\t" \
...
".popsection"

and the linker script will just do

KEEP(*(.static_const.*))

and I'm done. Nice individual sections for each of the runtime constant symbols.

However, for the fixup part, I then really want the section start and
end addresses, so that I can iterate over those uses for a particular
named symbol.

And I am not finding any way to do that with a linker script. Sure, I
can trivially just do

. = ALIGN(8);
__static_const_start = . ;
KEEP(*(.static_const.*))
__static_const_end = . ;

and now I have the over-all start and end for those sections, but I
want it per section.

This is actually not even remotely a new thing: We do this manually
for a lot of sections, and we have macros to help do it, eg our
'BOUNDED_SECTION_BY()' macro in <asm/vmlinux.lds.h> does exactly this
for any named section.

But they very much do this on individually named sections, not on the
kind of "do it for this section pattern" that I want. Yes, you can do
it for patterns, and we do:

BOUNDED_SECTION_BY(.note.*, _notes)

but that creates exactly that same "bound the whole set of sections by
symbols", not "bound each individual section" thing.

I tried to find some way to do this at the linker script level, and
decided it's not possible. I may be wrong.

So then I said "we can just make objtool do it".

And I think objtool effectively almost has that already, but I'm not
really familiar enough with the code to start messing around with it
at that level.

It's kind of like elf_create_prefix_symbol(), except at a section
level, and needs to be both prefix and postfix.

Or kind of elf_create_section_symbol(), but making it a real symbol
with a real value..

Hmm? Am I barking up entirely the wrong tree? Or does this seem doable
and reasonable?

Linus