PPC assembler and Darwin calling conventions vs ELF: advice needed #35
Replies: 4 comments 13 replies
-
Humm... yes, really it would be nice if we found some time to write down the differences between 'documented' and 'reality'. The situation with any system that is "in the wild" is that the ABI is defined by the compiler(s) that built it - independent of what the documented ABI was supposed to be. However, you should always start with the Apple ABI description - that is probably 80-90% of the story - and then consider adding notes where that is not clear (or was not implemented in reality). So, over the years, I've gradually found such differences - and usually incorporated a suitable change in GCC to try and match the [Darwin9] system ABIs for ppc32 and 64. This is not written down anywhere in a useful text form but, the test cases here: and the ABI description coded here: collect things together somewhat. Actually there was a pretty good match between GCC-4.0 and that branch (i.e. the two compilers had equivalent ABIs when running the compat testsuites in GCC). The ppc64 ABI description: I wish I had time to work on updating this stuff .. but not in the near future. |
Beta Was this translation helpful? Give feedback.
-
edit: splits some of the answer up.
The Darwin ppc32 ABI is completely different from the ppc64 one.
if you look in
– What should be done with code dealing with TOC, which is not used on MacOS? Nothing; R2 is a usable register - you can use it any way that fits with the ABI.
They are not supported (yet) because of
.align just tells the assembler to align the next item accordingly - it has nothing to do with what the arch pointer size is. Quite often items will need to be aligned according to their size (e.g. int to 4 bytes, double to 8 and long double / vector to 16) ..
those are local symbols, they do not appear in the assembler's symbol table, The bottom line is that it will take you some time, but you need to grok the differences between Mach-O (read the Apple doc) and sysV ABIs (read the baseline and then the ps [processor-specific] ABI) for x86 and ppc/ppc64. no short cuts .. unfortunately .. |
Beta Was this translation helpful? Give feedback.
-
@iains Thank you very much for detailed reply and references! I will go through your links first. |
Beta Was this translation helpful? Give feedback.
-
additional observation; non-"c-family" languages are, of course, free to choose a different ABI - and most likely do so (so much of what is written above might not apply) .. the only constraint being that they need to adhere to the c-family CC when calling system routines. Sometimes such languages use libffi to do that. There are some constraints on vectors, however; if the VMX (altivec) registers are used, then the VSAVE register must be updated to reflect the registers in use, or they will not be preserved across context switches. This applies regardless of the language ABI in use. |
Beta Was this translation helpful? Give feedback.
-
@iains Could you please refer me to some helpful materials to read beyond Apple documentation (which is far from being comprehensive) and IBM AIX docs (which do not exactly apply to Darwin)?
I have been in the process of fixing several compilers/langs for MacOS PPC:
ruby
,ghc
,sbcl
,ocaml
. While certain progress has been achieved, there are issues with PPC assembler which are not trivial, at least for me :)It is frequently the case that we have needed PPC code, but for Linux (ELF and/or ELFv2), sometimes LE-only, more often BE & LE. In many cases only
ppc64
code is present, whileppc32
is ether dropped or never existed.We can recall
jit
in TFF discussed in other thread here,v8
which I need very much forStan
language (AIX port ofv8
exists, but not Darwin),go
(notgcc-go
, which is also broken anyway, but Google version) etc. There are also numerous small instances of similar problems, few of which I have fixed on my own or with others’ help, many of alike still remain unfixed.To sum up the problem, everything above falls into either of the two cases:
ruby
,v8
,go
).ghc
,sbcl
,ocaml
).Could you suggest some reading and/or guidelines on how to go about such tasks? Beyond trivialities like slight differences between 32 and 64 bit instructions.
Few specific points to mention, which often pose a problem (I do not expect explanations here, but rather just wanna give some context):
– Apparently there are differences with respect to how certain registers are used between Darwin and ELF, but documentation offers little help. Are there some patterns here? There are some very old examples of a code that existed for both ABIs, but it is often not clear if register choice follows from ABI differences or just personal choices of those who wrote the code (likely at different time), or even errors.
– What should be done with code dealing with TOC, which is not used on MacOS?
– What is to be done with
.cfi_
directives? Apparently they aren’t supported on Darwin (or at least I got complaints re invalid code with them). Like here: http://stffrdhrn.github.io/software/toolchain/openrisc/2020/12/13/cxx-exception-unwinding.htmlThrowing them out together with other unsupported directives like
.type
removes build errors, but I am not sure that’s the way.– Often there is
.align
statement in beginning of a file (usually.align 2
or.align 3
), but while initially I assumed they simply correspond to 32 and 64 bit, respectively, I was wrong. This should be something simple, so I beg a pardon for a silly question.– How do symbols of a form
L
+number work (apparently used for branching)? When I compared Darwin PPC vs Linux PPC code in the last version ofocaml
which still had the latter, three-digit numbers after L looked arbitrary and did not fully match between the two.– Sometimes there are numerical constants defined which differ for ELF 32 bit, ELF 64 bit and ELFv2 64 bit. It is not always clear which are to be used for Darwin. Example pulled from
ocaml
:https://github.com/ocaml/ocaml/blob/trunk/runtime/power.S
Beta Was this translation helpful? Give feedback.
All reactions