powerpc64 ELFv2 ABI and use of r2 for TOC Pointer Relative Accesses vs. FreeBSD (and gcc) use of other registers instead

Mark Millard marklmi at yahoo.com
Fri Jan 24 02:33:03 UTC 2020


[I experiment with trying both system-clang
with devel/binutils at powerpc64 and trying
devel/freebsd-gcc9 at powerpc64 with
devel/binutils at powerpc64 for buildworld
buildkernel .]

In searching around about tool complaints that
got/toc optimization is not supported for various
instruction encodings that are used, I ran into:

http://openpowerfoundation.org/wp-content/uploads/resources/leabi/content/dbdoclet.50655241_66700.html

QUOTE
Compilers may generate two-instruction medium code model
references (or, if selected, short displacement one-instruction
references) for all data items that are in the TOC for the
object file being compiled. Such references are relative to
the TOC pointer register, r2. (The linker may optimize
two-instruction forms to one instruction forms, replacing a
first instruction of the two instruction form with a nop and
rewriting the second instruction. Consequently, the TOC
pointer must be live during the first and second instruction
of a two-instruction reference.)
END QUOTE

And into:

http://openpowerfoundation.org/wp-content/uploads/resources/leabi/content/ch03s06s03.html

and its statement:

QUOTE
Compilers and programmers must ensure that r2 is live at
the actual data access point associated with extended
displacement addressing.
END QUOTE

This last was in a context about:

QUOTE
Assemblers and linkers may optimize TOC reference code that consists of two instructions with equivalent code when offset at ha is 0.

TOC reference code:

addis  rt, r2, offset at ha
lwz    rt, offset at l(rt)

Equivalent code:

NOP
lwz    rt, offset(r2)
END QUOTE


There was also:

QUOTE
The static linker must modify a nop instruction after a bl function
call to restore the TOC pointer in r2 from 24(r1) when an external
symbol that may use the TOC may be called, as in Section 2.3.6,
“Function Calls”. Object files must contain a nop slot after a bl
instruction to an external symbol.
END QUOTE

at: http://openpowerfoundation.org/wp-content/uploads/resources/leabi/content/dbdoclet.50655241_69294.html


These probably explains why I see a complaint for the
addis instruction in:

dbtrap:
      /* Write the trap vector to SPRG3 by computing LR & 0xff00 */
      mflr    %r1
      andi.   %r1,%r1,0xff00
      mtsprg3 %r1

      GET_TOCBASE(%r1)                        /* get new SP */
      addis   %r1,%r1,TOC_REF(trapstk)@ha
      ld      %r1,TOC_REF(trapstk)@l(%r1)
      addi    %r1,%r1,(TRAPSTKSZ-48)

The tools involved expect: addis ...,r2,... for
such a context instead.

Apparently, lack of r2 use also explains the long
list of such messages for zfs.ko.full when I'm
building via devel/freebsd-gcc9 at powerpc64 : the code
generated might not be sticking to use of r2 in that
spot for addis. (So far, I've yet to look at the
detail for zfs.)



===
Mark Millard
marklmi at yahoo.com
( dsl-only.net went
away in early 2018-Mar)



More information about the freebsd-ppc mailing list