Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

No wonder Clairol makes so much money selling shampoo. Lather, Rinse, Repeat is an infinite loop!


devel / comp.lang.asm.x86 / Re: aarch64 (64-Bit ARM) - return by value - GNU gcc assembler

SubjectAuthor
* aarch64 (64-Bit ARM) - return by value - GNU gcc assemblerFrederick Virchanza Gotham
`* aarch64 (64-Bit ARM) - return by value - GNU gcc assemblerThomas Koenig
 +- aarch64 (64-Bit ARM) - return by value - GNU gcc assemblerFrederick Virchanza Gotham
 `* aarch64 (64-Bit ARM) - return by value - GNU gcc assemblerFrederick Virchanza Gotham
  `* aarch64 (64-Bit ARM) - return by value - GNU gcc assemblerFrederick Virchanza Gotham
   `* aarch64 (64-Bit ARM) - return by value - GNU gcc assemblerFrederick Virchanza Gotham
    `- aarch64 (64-Bit ARM) - return by value - GNU gcc assemblerSingle Stage to Orbit

1
aarch64 (64-Bit ARM) - return by value - GNU gcc assembler

<bf18fd07-03e6-4b17-9d3f-d272b2ec03b3n@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=459&group=comp.lang.asm.x86#459

  copy link   Newsgroups: comp.lang.asm.x86
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: cauldwell.thomas@nospicedham.gmail.com (Frederick Virchanza Gotham)
Newsgroups: comp.lang.asm.x86
Subject: aarch64 (64-Bit ARM) - return by value - GNU gcc assembler
Date: Mon, 17 Jul 2023 07:21:34 -0700 (PDT)
Organization: A noiseless patient Spider
Lines: 72
Approved: fbkotler@myfairpoint.net - comp.lang.asm.x86 moderation team.
Message-ID: <bf18fd07-03e6-4b17-9d3f-d272b2ec03b3n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Injection-Date: Mon, 17 Jul 2023 14:21:34 +0000
Injection-Info: dont-email.me; posting-host="f7fae69f0d858b500773377552d6fdde";
logging-data="1345668"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+87ph7EulbCnfR4nDcdO3qp/0/3IzTTOc="
User-Agent: G2/1.0
Cancel-Lock: sha1:X0zJaApxMxCB1YVjnZlFDP9cVFQ=
 by: Frederick Virchanza - Mon, 17 Jul 2023 14:21 UTC

I realise this newsgroup has x86 in its name but I'm finding it difficult to find a decent ARM assembler group online. I figure someone here might be able to program in aarch64 assembler as well as x86.

I'm trying to write a very simple function in two or three aarch64 instructions as 'inline assembler' inside a C++ source file.

With the aarch64 calling convention on Linux, if a function returns a very large struct by value, then the address of where to store the return value is passed in the X8 register. This is out of the ordinary as far as calling conventions go. Every other calling convention, for example System V x86_64, Microsoft x64, cdecl, stdcall, arm32, pass the address of the return value in the first parameter. So for example with x86_64 on Linux, the RDI register contains the address of where to store the very large struct.

I want to try emulate this behaviour on aarch64 on Linux. When my assembler function is entered, I want it to do two things:
(1) Put the address of the indirect return object into the first parameter register, i.e. move X8 to X0
(2) Jump to a location specified by a global function pointer

So here's how I think my assembler function should look:

__asm("Invoke: \n"
" mov x0, x8 \n" // move return value address into 1st parameter
" mov x9, f \n" // Load address of code into register
" br x9 \n" // Jump to code
);

I don't know what's wrong here but it doesn't work. In the following complete C++ program, I use the class 'std::mutex' as it's a good example of a class that can't be copied or moved (I am relying on mandatory Return Value Optimisation).

Here is my entire program in one C++ file, could someone please help me write the assembler function properly? Am I supposed to be using the ADRP and LDR instructions instead of MOV?

#include <mutex> // mutex
#include <iostream> // cout, endl
using std::cout, std::endl;

void (*f)(void) = nullptr;

extern "C" void Invoke(void);

__asm("Invoke: \n"
" mov x0, x8 \n" // move return value address into 1st parameter
" mov x9, f \n" // Load address of code into register
" br x9 \n" // Jump to code
);

void Func(std::mutex *const p)
{ cout << "Address of return value: " << p << endl;
}

int main(void)
{ f = (void(*)(void))Func;

auto const p = reinterpret_cast<std::mutex (*)(void)>(Invoke);

auto retval = p();

cout << "Address of return value: " << &retval << endl;
}

Re: aarch64 (64-Bit ARM) - return by value - GNU gcc assembler

<u93moh$qr8m$1@newsreader4.netcologne.de>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=460&group=comp.lang.asm.x86#460

  copy link   Newsgroups: comp.lang.asm.x86
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tkoenig@nospicedham.netcologne.de (Thomas Koenig)
Newsgroups: comp.lang.asm.x86
Subject: Re: aarch64 (64-Bit ARM) - return by value - GNU gcc assembler
Date: Mon, 17 Jul 2023 15:29:21 -0000 (UTC)
Organization: news.netcologne.de
Lines: 29
Approved: fbkotler@myfairpoint.net - comp.lang.asm.x86 moderation team.
Distribution: world
Message-ID: <u93moh$qr8m$1@newsreader4.netcologne.de>
References: <bf18fd07-03e6-4b17-9d3f-d272b2ec03b3n@googlegroups.com>
Injection-Info: dont-email.me; posting-host="f7fae69f0d858b500773377552d6fdde";
logging-data="1374309"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18FARytMEwIbbNh2Cinad5gB7KWPx1jHQI="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:FPXyCQmQiQGE82hw2mQts6xO7Xw=
 by: Thomas Koenig - Mon, 17 Jul 2023 15:29 UTC

Frederick Virchanza Gotham <cauldwell.thomas@nospicedham.gmail.com> schrieb:

> I want to try emulate this behaviour on aarch64 on Linux. When my assembler function is entered, I want it to do two things:
> (1) Put the address of the indirect return object into the first parameter register, i.e. move X8 to X0
> (2) Jump to a location specified by a global function pointer
>
> So here's how I think my assembler function should look:
>
> __asm("Invoke: \n"
> " mov x0, x8 \n" // move return value address into 1st parameter
> " mov x9, f \n" // Load address of code into register
> " br x9 \n" // Jump to code
> );

If you're using gcc, you may need to specify volatile.

Do you see the assembly in the generated *.s file?

> I don't know what's wrong here but it doesn't work.

So, you need to debug. Some generic advice.

The best way is probably to the code with -S -fverbose-asm (NOT
with -g), then translate the code with "-g" and single-step (using
gdb and stepi) through your functions.

Hopefully, you'll see what is wrong then.

Re: aarch64 (64-Bit ARM) - return by value - GNU gcc assembler

<cc97d4bc-9e51-4e47-bdcb-65407901eb95n@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=461&group=comp.lang.asm.x86#461

  copy link   Newsgroups: comp.lang.asm.x86
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: cauldwell.thomas@nospicedham.gmail.com (Frederick Virchanza Gotham)
Newsgroups: comp.lang.asm.x86
Subject: Re: aarch64 (64-Bit ARM) - return by value - GNU gcc assembler
Date: Mon, 17 Jul 2023 10:06:30 -0700 (PDT)
Organization: A noiseless patient Spider
Lines: 24
Approved: fbkotler@myfairpoint.net - comp.lang.asm.x86 moderation team.
Message-ID: <cc97d4bc-9e51-4e47-bdcb-65407901eb95n@googlegroups.com>
References: <bf18fd07-03e6-4b17-9d3f-d272b2ec03b3n@googlegroups.com> <u93moh$qr8m$1@newsreader4.netcologne.de>
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Injection-Date: Mon, 17 Jul 2023 17:06:31 +0000
Injection-Info: dont-email.me; posting-host="f7fae69f0d858b500773377552d6fdde";
logging-data="1383248"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+FgRqugav3qsL3Ef38kozQWwUCIAa2VJQ="
User-Agent: G2/1.0
Cancel-Lock: sha1:tL1jZq1z1OvpIj5uXwdNKU2zcF4=
 by: Frederick Virchanza - Mon, 17 Jul 2023 17:06 UTC

On Monday, July 17, 2023, Thomas Koenig wrote:

> If you're using gcc, you may need to specify volatile.

I tried to reply to this a minute ago but nothing has shown up yet in Google Groups.

Instead of using "inline assembler" inside a C++ source file, I instead tried to make a separate assembler file.

Here's what I have, but it still doesn't work, it's still segfaulting inside 'detail_Invoke':

..text

..global tl_p
..Addr_tl_p:
.xword tl_p

..global detail_Invoke
detail_Invoke:
adrp x9, [.Addr_tl_p]
ldr x9, [x9]
mov x10, x9
br x10

Re: aarch64 (64-Bit ARM) - return by value - GNU gcc assembler

<b1161848-5511-4284-8f37-b2ca5017da77n@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=462&group=comp.lang.asm.x86#462

  copy link   Newsgroups: comp.lang.asm.x86
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: cauldwell.thomas@nospicedham.gmail.com (Frederick Virchanza Gotham)
Newsgroups: comp.lang.asm.x86
Subject: Re: aarch64 (64-Bit ARM) - return by value - GNU gcc assembler
Date: Mon, 17 Jul 2023 10:03:02 -0700 (PDT)
Organization: A noiseless patient Spider
Lines: 22
Approved: fbkotler@myfairpoint.net - comp.lang.asm.x86 moderation team.
Message-ID: <b1161848-5511-4284-8f37-b2ca5017da77n@googlegroups.com>
References: <bf18fd07-03e6-4b17-9d3f-d272b2ec03b3n@googlegroups.com> <u93moh$qr8m$1@newsreader4.netcologne.de>
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Injection-Date: Mon, 17 Jul 2023 17:03:03 +0000
Injection-Info: dont-email.me; posting-host="f7fae69f0d858b500773377552d6fdde";
logging-data="1383264"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/YTgvE3a/JMYwTmLg5HV5zTfjuiC3HK6U="
User-Agent: G2/1.0
Cancel-Lock: sha1:6apMgBJZv9ta1SFlLxjMv56bfYw=
 by: Frederick Virchanza - Mon, 17 Jul 2023 17:03 UTC

On Monday, July 17, 2023, Thomas Koenig wrote:
>
> If you're using gcc, you may need to specify volatile.

Instead of using "inline assembler" inside a C++ source file, I instead tried to make a separate assembler file.

Here's what I have, but it still doesn't work, it's still segfaulting inside 'detail_Invoke':

..text

..global tl_p
..Addr_tl_p:
.xword tl_p

..global detail_Invoke
detail_Invoke:
adrp x9, [.Addr_tl_p]
ldr x9, [x9]
mov x10, x9
br x10

Re: aarch64 (64-Bit ARM) - return by value - GNU gcc assembler

<45785e15-18ef-4ebf-b686-3a35e9f75853n@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=463&group=comp.lang.asm.x86#463

  copy link   Newsgroups: comp.lang.asm.x86
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: cauldwell.thomas@nospicedham.gmail.com (Frederick Virchanza Gotham)
Newsgroups: comp.lang.asm.x86
Subject: Re: aarch64 (64-Bit ARM) - return by value - GNU gcc assembler
Date: Tue, 18 Jul 2023 01:35:32 -0700 (PDT)
Organization: A noiseless patient Spider
Lines: 42
Approved: fbkotler@myfairpoint.net - comp.lang.asm.x86 moderation team.
Message-ID: <45785e15-18ef-4ebf-b686-3a35e9f75853n@googlegroups.com>
References: <bf18fd07-03e6-4b17-9d3f-d272b2ec03b3n@googlegroups.com>
<u93moh$qr8m$1@newsreader4.netcologne.de> <b1161848-5511-4284-8f37-b2ca5017da77n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Injection-Date: Tue, 18 Jul 2023 08:35:33 +0000
Injection-Info: dont-email.me; posting-host="8982f7ce85ecf6aa7119847ea742832d";
logging-data="1679719"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+VS/6mh7DXOhI8bnEx9pMnaVDh/3RjV/8="
User-Agent: G2/1.0
Cancel-Lock: sha1:Xfo/toFyH71aYyd3s5C+CAfztxY=
 by: Frederick Virchanza - Tue, 18 Jul 2023 08:35 UTC

Over on comp.unix.programmer, Adam Sampson gave me inline assembler that works:

__asm(".text\n"
"Invoke:\n"
" mov x1, x8\n"
" adr x9, f\n"
" ldr x9, [x9]\n"
" br x9\n"
);

For the sake of posting to this newsgroup, I simplified my problem just a tiny bit. Previously I told you that 'f' was a global variable defined as follows:

void (*f)(void);

but in actual fact it's:

thread_local void (*f)(void);

If I change it to thread_local then try to re-compile the inline assembler, I get a linker error:

R_AARCH64_ADR_PREL_LO21 used with TLS symbol f

Do you know what syntax I use to access the thread_local variable from assembler? Will I need to write a separate function as follows?

void (*getf(void))(void)
{ return f;
}

and then call that function from my assembler? I'm worried about corrupting the caller-saved registers, because I perform a 'br' rather than a 'blr' (i.e. I perform a jump rather than a function call). I suppose I could push all the caller-saved registers before invoking 'getf' and then pop afterward... which I know how to do on x86_64 but I'm new to all this aarch64 stuff..

Re: aarch64 (64-Bit ARM) - return by value - GNU gcc assembler

<0ea82c79-01c9-49db-9207-0a3855a45e5en@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=464&group=comp.lang.asm.x86#464

  copy link   Newsgroups: comp.lang.asm.x86
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: cauldwell.thomas@nospicedham.gmail.com (Frederick Virchanza Gotham)
Newsgroups: comp.lang.asm.x86
Subject: Re: aarch64 (64-Bit ARM) - return by value - GNU gcc assembler
Date: Tue, 18 Jul 2023 03:56:15 -0700 (PDT)
Organization: A noiseless patient Spider
Lines: 54
Approved: fbkotler@myfairpoint.net - comp.lang.asm.x86 moderation team.
Message-ID: <0ea82c79-01c9-49db-9207-0a3855a45e5en@googlegroups.com>
References: <bf18fd07-03e6-4b17-9d3f-d272b2ec03b3n@googlegroups.com>
<u93moh$qr8m$1@newsreader4.netcologne.de> <b1161848-5511-4284-8f37-b2ca5017da77n@googlegroups.com>
<45785e15-18ef-4ebf-b686-3a35e9f75853n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Injection-Date: Tue, 18 Jul 2023 10:56:16 +0000
Injection-Info: dont-email.me; posting-host="8982f7ce85ecf6aa7119847ea742832d";
logging-data="1713246"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19frkhs//Ey2UhEvVyJSrd7ohuVCpgOmAE="
User-Agent: G2/1.0
Cancel-Lock: sha1:AxdjvH5uHHX//yKKVMabW69QXWE=
 by: Frederick Virchanza - Tue, 18 Jul 2023 10:56 UTC

On Monday 17 July 2023, Frederick Gotham wrote:
>
> If I change it to thread_local then try to re-compile, I get a linker error:
>
> R_AARCH64_ADR_PREL_LO21 used with TLS symbol f
>
> Do you know what syntax I use to access the thread_local variable from assembler? Will I need to write a separate function as follows?

In order to try understand how thread_local variables are accessed from aarch64 assembler, I wrote the following dynamic shared library in C:

__thread void (*f)(void);

void (*g)(void);

void Func(void)
{
g = f;
}

I compiled this to 'libtest.so" and then used 'objdump' on it to see:

<Func>:
Line 01: stp x29, x30, [sp, #-16]!
Line 02: mrs x1, tpidr_el0
Line 03: mov x29, sp
Line 04: adrp x0, 20000 <__cxa_finalize>
Line 05: ldr x2, [x0, #16]
Line 06: add x0, x0, #0x10
Line 07: blr x2
Line 08: adrp x2, 1f000 <__FRAME_END__+0x1e8c8>
Line 09: ldr x2, [x2, #4032]
Line 10: ldr x0, [x1, x0]
Line 11: str x0, [x2]
Line 12: ldp x29, x30, [sp], #16
Line 13: ret

Line #2 appears to put the address of "thread local storage" inside the x1 register.
Lines #4-7 at first glance seem to call the function "__cxz_finalize" (which is the one that gets called at the end of a program to invoke all the destructors of global objects)... but really I just think that the number 0x20000 is being used as a base address to apply offsets to.
Lines #7 definitely is calling some function though, I don't know which one..
Lines #8-12, I'm not sure here... but I think they're moving the value of the thread_local variable 'f' into the global variable 'g'.

Can anyone please help me understand this? And explore how I would go about writing aarch64 to access a thread_local variable called 'f'?

Re: aarch64 (64-Bit ARM) - return by value - GNU gcc assembler

<fd6d419bd907314c4f1bde2525a13fa2a033dec0.camel@munted.eu>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=465&group=comp.lang.asm.x86#465

  copy link   Newsgroups: comp.lang.asm.x86
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: alex.buell@nospicedham.munted.eu (Single Stage to Orbit)
Newsgroups: comp.lang.asm.x86
Subject: Re: aarch64 (64-Bit ARM) - return by value - GNU gcc assembler
Date: Tue, 18 Jul 2023 12:18:34 +0100
Organization: One very high maintenance cat
Lines: 9
Approved: fbkotler@myfairpoint.net - comp.lang.asm.x86 moderation team.
Message-ID: <fd6d419bd907314c4f1bde2525a13fa2a033dec0.camel@munted.eu>
References: <bf18fd07-03e6-4b17-9d3f-d272b2ec03b3n@googlegroups.com>
<u93moh$qr8m$1@newsreader4.netcologne.de>
<b1161848-5511-4284-8f37-b2ca5017da77n@googlegroups.com>
<45785e15-18ef-4ebf-b686-3a35e9f75853n@googlegroups.com>
<0ea82c79-01c9-49db-9207-0a3855a45e5en@googlegroups.com>
Reply-To: alex.buell@munted.eu
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Injection-Info: dont-email.me; posting-host="8982f7ce85ecf6aa7119847ea742832d";
logging-data="1734755"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX191HvNxtJdjbdznBGQ3K7/hnpyyjZMy10Y="
User-Agent: Evolution 3.48.1
Cancel-Lock: sha1:YpLjKrqQ4dH5za/sJMr26nLbiig=
 by: Single Stage to Orbi - Tue, 18 Jul 2023 11:18 UTC

On Tue, 2023-07-18 at 03:56 -0700, Frederick Virchanza Gotham wrote:
> Can anyone please help me understand this? And explore how I would go
> about writing aarch64 to access a thread_local variable called 'f'?

Sorry, you must have the wrong newsgroup. This is for *INTEL* x86
assembly.
--
Tactical Nuclear Kittens

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor