fig2-33.png typo #102

Closed
opened 5 years ago by asurati · 5 comments
asurati commented 5 years ago (Migrated from github.com)

If the intention is to calculate the address of function, the 3rd instruction is likely an
la r9, function@l(r9) instead of an ld r9, function@l(r9).

If the intention is to calculate the address of _function_, the 3rd instruction is likely an _la r9, function@l(r9)_ instead of an _ld r9, function@l(r9)_.
wschmidt-ibm commented 5 years ago (Migrated from github.com)

No, the example is correct. Because "function" is compiled as a .quad containing an address, we just need to load it directly. You can compile the following example with current GCC to see this (the example is slightly more complex to avoid the bctrl being converted to a direct call):

extern void function0 ();
extern void function1 ();

extern void (*ptrfunc) ();

void foo (int x)
{
  ptrfunc = x % 2 ? function1 : function0;
  (*ptrfunc) ();
}

gcc -O2 -S fptr.c
No, the example is correct. Because "function" is compiled as a .quad containing an address, we just need to load it directly. You can compile the following example with current GCC to see this (the example is slightly more complex to avoid the bctrl being converted to a direct call): ``` extern void function0 (); extern void function1 (); extern void (*ptrfunc) (); void foo (int x) { ptrfunc = x % 2 ? function1 : function0; (*ptrfunc) (); } gcc -O2 -S fptr.c ```
asurati commented 5 years ago (Migrated from github.com)

Isn't "function" an external symbol, and thus it cannot be compiled as a .quad, since then it would cease to be a function and become just a data item storing a pointer?

ppc64le gcc 9.3.0 shows this:

	.file	"fptr.c"
	.machine power8
	.abiversion 2
	.section	".text"
	.section	".toc","aw"
	.align 3
.LC0:
	.quad	function1
.LC1:
	.quad	function0
.LC2:
	.quad	ptrfunc
. . .
	addis 12,2,.LC0@toc@ha
	ld 12,.LC0@toc@l(12)
. . .

To allow for absolute addressing one can perhaps imagine the last 2 instructions above as:

	lis 12,.LC0@ha
	ld 12,.LC0@l(12)

But that still doesn't change the fact that "function" isn't compiled as a .quad; ".LC0" is but that is a different symbol than "function".

Do you mean to say that the below construct is what is intended?

function1:
	.quad	function1

Edit: The same example in 32bit-abi (I know it is not normative, etc.) uses la.
See Figure 3-29. Absolute Indirect Function Call.

Isn't "function" an external symbol, and thus it cannot be compiled as a .quad, since then it would cease to be a function and become just a data item storing a pointer? ppc64le gcc 9.3.0 shows this: ``` .file "fptr.c" .machine power8 .abiversion 2 .section ".text" .section ".toc","aw" .align 3 .LC0: .quad function1 .LC1: .quad function0 .LC2: .quad ptrfunc . . . addis 12,2,.LC0@toc@ha ld 12,.LC0@toc@l(12) . . . ``` To allow for absolute addressing one can perhaps imagine the last 2 instructions above as: ``` lis 12,.LC0@ha ld 12,.LC0@l(12) ``` But that still doesn't change the fact that "function" isn't compiled as a .quad; ".LC0" is but that is a different symbol than "function". Do you mean to say that the below construct is what is intended? ``` function1: .quad function1 ``` Edit: The same example in 32bit-abi (I know it is not normative, etc.) uses la. See [Figure 3-29. Absolute Indirect Function Call](https://www.polyomino.org.uk/publications/2011/Power-Arch-32-bit-ABI-supp-1.0-Unified.pdf).
wschmidt-ibm commented 5 years ago (Migrated from github.com)

Sorry, I was too quick to respond here. It's not a .quad, but rather the value is directly filled in by the linker in the instructions.

The thing to note here is that this is an absolute addressing model, so there is no indirection through the TOC. I am not aware of any way to get either GCC or Clang to generate code for an absolute addressing model, so the whole example has always seemed somewhat pointless to me, but...

Having read through this again, I agree that this should be a la, because function is already a symbol containing the full 32-bit address of the medium absolute model. I'm sorry for the confusion on my part.

I'd love to just remove the example since nobody uses this addressing mode in practice, but I suppose it ought to be fixed. Thanks for your persistence!

Sorry, I was too quick to respond here. It's not a .quad, but rather the value is directly filled in by the linker in the instructions. The thing to note here is that this is an absolute addressing model, so there is no indirection through the TOC. I am not aware of any way to get either GCC or Clang to generate code for an absolute addressing model, so the whole example has always seemed somewhat pointless to me, but... Having read through this again, I agree that this should be a `la`, because `function` is already a symbol containing the full 32-bit address of the medium absolute model. I'm sorry for the confusion on my part. I'd love to just remove the example since nobody uses this addressing mode in practice, but I suppose it ought to be fixed. Thanks for your persistence!
wschmidt-ibm commented 5 years ago (Migrated from github.com)

BTW, I'm not a real fan of the la mnemonic anyway, so will likely replace this with the corresponding addi for clarity. (addi r9, r9, function@l)

BTW, I'm not a real fan of the `la` mnemonic anyway, so will likely replace this with the corresponding addi for clarity. (addi r9, r9, function@l)
wschmidt-ibm commented 5 years ago (Migrated from github.com)

Note to self: The extra extern appears in several tables.

Note to self: The extra `extern` appears in several tables.
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: systemsoftware/ELFv2-ABI#102
Loading…
There is no content yet.