Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

Whenever people agree with me, I always think I must be wrong. -- Oscar Wilde


devel / comp.lang.asm.x86 / Please explain exception handling in x86_64 Linux

SubjectAuthor
* Please explain exception handling in x86_64 LinuxFrederick Virchanza Gotham
`- Please explain exception handling in x86_64 LinuxThorsten Glaser

1
Please explain exception handling in x86_64 Linux

<b8fbb81e-a7cf-41f0-aa16-ab0ca87ea334n@googlegroups.com>

  copy mid

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

  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: Please explain exception handling in x86_64 Linux
Date: Mon, 24 Apr 2023 08:20:32 -0700 (PDT)
Organization: A noiseless patient Spider
Lines: 53
Approved: fbkotler@myfairpoint.net - comp.lang.asm.x86 moderation team.
Message-ID: <b8fbb81e-a7cf-41f0-aa16-ab0ca87ea334n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Injection-Date: Mon, 24 Apr 2023 15:20:32 +0000
Injection-Info: dont-email.me; posting-host="a5ec0f5803bd41f0ddf23453bc5a3b63";
logging-data="428401"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/iaDUMMBmm4874J7BiD5F6kQQ55B8ZulQ="
User-Agent: G2/1.0
Cancel-Lock: sha1:jJ3EC7VUrkhdTA1HBBQhKbFSmZg=
 by: Frederick Virchanza - Mon, 24 Apr 2023 15:20 UTC

I've written the following C++ code as an example:

#include <cstdio> // puts

extern void LibFunc(void) noexcept(false); // likely to throw an exception

void Func(void)
{ try
{
LibFunc();
}
catch (...)
{
std::puts("caught");
}
}

gets compiled to:

..LC0:
.string "caught"
Func():
push rbx
call LibFunc()
jmp .L1
mov rdi, rax
call __cxa_begin_catch
mov edi, OFFSET FLAT:.LC0
call puts
call __cxa_end_catch
..L1:
pop rbx
ret
mov rbx, rax
call __cxa_end_catch
mov rdi, rbx
call _Unwind_Resume

There's a few things I don't understand here:
(1) Why does Func save and restore the RBX register if it never changes it? I realise that RBX is callee-saved and so you must push it onto the stack before altering it, but I don't see anywhere where it's altered.
(2) If 'LibFunc' throws an exception, how does it know where to jump back to? In normal circumstances if the function returned normally, it would jump back to the 'jmp .L1' instruction, but instead it has to jump back to one instruction after that. How does it know what offset of the return address to jump back to?
(3) I don't know why those last four lines are there. They look like unreachable code to me.

Can anyone enlighten me please?

Re: Please explain exception handling in x86_64 Linux

<Pine.BSM.4.64L.2304261339240.3563@herc.mirbsd.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.asm.x86
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tg@nospicedham.mirbsd.de (Thorsten Glaser)
Newsgroups: comp.lang.asm.x86
Subject: Re: Please explain exception handling in x86_64 Linux
Date: Wed, 26 Apr 2023 13:43:27 +0000
Organization: A noiseless patient Spider
Lines: 39
Approved: fbkotler@myfairpoint.net - comp.lang.asm.x86 moderation team.
Message-ID: <Pine.BSM.4.64L.2304261339240.3563@herc.mirbsd.org>
References: <b8fbb81e-a7cf-41f0-aa16-ab0ca87ea334n@googlegroups.com>
MIME-Version: 1.0
Content-Type: TEXT/PLAIN; charset=utf-8
Content-Transfer-Encoding: QUOTED-PRINTABLE
Injection-Info: dont-email.me; posting-host="c5d332cf4920c74ef2eb43d6f1ec8254";
logging-data="1526879"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+BBccuzrogSZM75pS7paYuwfC1N5z5nJQ="
Cancel-Lock: sha1:v4PFpoql027XzMbIYgQc6Vz5kng=
 by: Thorsten Glaser - Wed, 26 Apr 2023 13:43 UTC

Frederick Virchanza Gotham dixit:

>(1) Why does Func save and restore the RBX register if it never changes it?

Compilers tend to do that (frame pointer things).

>(2) If 'LibFunc' throws an exception, how does it know where to jump
>back to? In normal circumstances if the function returned normally, it
>would jump back to the 'jmp .L1' instruction, but instead it has to
>jump back to one instruction after that. How does it know what offset
>of the return address to jump back to?

This is only one form of exception handling: it uses unwind information
from a separate part of the executable to know where to jump back to.

In normal operation, the call returns normally, then the jmp is
executed terminating the function. If an exception is thrown, the
handler walks back the call stack then changes the return address.

There’s also setjmp/longjmp-based exception handling (“sjlj” if you
ever compile GCC) which uses these libc functions instead of relying
on magic debugging information to unwind. That may be easier to
understand so I suggest having a look at that.

>(3) I don't know why those last four lines are there. They look like
>unreachable code to me.

As above, they’ll be reached by changing the return address. Your
compiler probably added some .cfi_* pseudo-ops for the assembler
there which would have the corresponding debugging info.

bye,
//mirabilos
--
15:41⎜<Lo-lan-do:#fusionforge> Somebody write a testsuite for helloworld :-)

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor