Re: [PATCH 2/3] x86/callthunks: Handle %rip-relative relocations in call thunk template

From: Uros Bizjak
Date: Thu Nov 02 2023 - 07:50:21 EST


On Thu, Nov 2, 2023 at 12:44 PM Peter Zijlstra <peterz@xxxxxxxxxxxxx> wrote:
>
> On Thu, Nov 02, 2023 at 12:25:47PM +0100, Uros Bizjak wrote:
>
> > @@ -166,13 +168,51 @@ static const u8 nops[] = {
> > 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
> > };
> >
> > +#define apply_reloc_n(n_, p_, d_) \
> > + do { \
> > + s32 v = *(s##n_ *)(p_); \
> > + v += (d_); \
> > + BUG_ON((v >> 31) != (v >> (n_-1))); \
> > + *(s##n_ *)(p_) = (s##n_)v; \
> > + } while (0)
> > +
> > +static __always_inline
> > +void apply_reloc(int n, void *ptr, uintptr_t diff)
> > +{
> > + switch (n) {
> > + case 4: apply_reloc_n(32, ptr, diff); break;
> > + default: BUG();
> > + }
> > +}
> > +
> > +static void apply_relocation(u8 *buf, size_t len, u8 *dest, u8 *src)
> > +{
> > + for (int next, i = 0; i < len; i = next) {
> > + struct insn insn;
> > +
> > + if (WARN_ON_ONCE(insn_decode_kernel(&insn, &buf[i])))
> > + return;
> > +
> > + next = i + insn.length;
> > +
> > + if (insn_rip_relative(&insn))
> > + apply_reloc(insn.displacement.nbytes,
> > + buf + i + insn_offset_displacement(&insn),
> > + src - dest);
> > + }
> > +}
>
> Isn't it simpler to use apply_relocation() from alternative.c?

Yes, I was looking at that function, but somehow thought that it is a
bit overkill here, since we just need a %rip-relative reloc.

> Remove static, add decl, stuff like that?

On second thought, you are right. Should I move the above function
somewhere (reloc.c?) , or can I just use it from alternative.c and add
decl (where?) ?

Thanks,
Uros.