public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* There Might a Bug in the Compiler: When Calling Weak Defined Function
@ 2023-08-07 15:51 Şahin Duran
  2023-08-07 15:56 ` Richard Earnshaw (lists)
  2023-08-07 15:56 ` Andrew Pinski
  0 siblings, 2 replies; 3+ messages in thread
From: Şahin Duran @ 2023-08-07 15:51 UTC (permalink / raw)
  To: gcc

[-- Attachment #1: Type: text/plain, Size: 1840 bytes --]

Dear GCC Developers,

I think I've just discovered a bug/ undefined situation in the compiler.
When I try to call a weakly defined function, compiler successfully
generates the code of calling procedure. However, this calling procedure is
nothing but branching to address 0 which results in segmentation fault. I
am not sure if this is the case for the latest version of GCC but it is for
GCC 4.9.2 and many online compilers. I just thought that maybe including a
rule that generates compilation error when the user defines a weak function
and calls it without actually implementing it. You may find the results in
the attachments.

Kind regards,
I am looking forward to hearing from you about this.
Şahin Duran


Attachments:

Source Code:
#include <stdio.h>
#include <stdlib.h>
#include "header.h"

__attribute__((weak)) int add(int,int);

int main(int argc, char *argv[]) {
printf("%x",add);
add(31,31);
return 0;
}
terminal result : 0

Disassembly (on a 64bit AMD Machine):
0x0000000000401530 <+0>: push   rbp
   0x0000000000401531 <+1>: mov    rbp,rsp
   0x0000000000401534 <+4>: sub    rsp,0x20
   0x0000000000401538 <+8>: mov    DWORD PTR [rbp+0x10],ecx
   0x000000000040153b <+11>: mov    QWORD PTR [rbp+0x18],rdx
   0x000000000040153f <+15>: call   0x402100 <__main>
   0x0000000000401544 <+20>: mov    rdx,QWORD PTR [rip+0x2ed5]        #
0x404420 <.refptr.add>
   0x000000000040154b <+27>: lea    rcx,[rip+0x2aae]        # 0x404000
   0x0000000000401552 <+34>: call   0x402b18 <printf>
=> 0x0000000000401557 <+39>: mov    edx,0x1f
   0x000000000040155c <+44>: mov    ecx,0x1f
   0x0000000000401561 <+49>: call   0x0
   0x0000000000401566 <+54>: mov    eax,0x0
   0x000000000040156b <+59>: add    rsp,0x20
   0x000000000040156f <+63>: pop    rbp
   0x0000000000401570 <+64>: ret

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: There Might a Bug in the Compiler: When Calling Weak Defined Function
  2023-08-07 15:51 There Might a Bug in the Compiler: When Calling Weak Defined Function Şahin Duran
@ 2023-08-07 15:56 ` Richard Earnshaw (lists)
  2023-08-07 15:56 ` Andrew Pinski
  1 sibling, 0 replies; 3+ messages in thread
From: Richard Earnshaw (lists) @ 2023-08-07 15:56 UTC (permalink / raw)
  To: Şahin Duran, gcc

On 07/08/2023 16:51, Şahin Duran via Gcc wrote:
> Dear GCC Developers,
> 
> I think I've just discovered a bug/ undefined situation in the compiler.
> When I try to call a weakly defined function, compiler successfully
> generates the code of calling procedure. However, this calling procedure is
> nothing but branching to address 0 which results in segmentation fault. I
> am not sure if this is the case for the latest version of GCC but it is for
> GCC 4.9.2 and many online compilers. I just thought that maybe including a
> rule that generates compilation error when the user defines a weak function
> and calls it without actually implementing it. You may find the results in
> the attachments.
> 
> Kind regards,
> I am looking forward to hearing from you about this.
> Şahin Duran
> 
> 

If a function might be weak, you need to test that it is defined before 
calling it.  So this is a bug in your program.  You need to write

   if (add)
     add (31, 31);

Or something like that which checks that the symbol has been defined.

R.

> Attachments:
> 
> Source Code:
> #include <stdio.h>
> #include <stdlib.h>
> #include "header.h"
> 
> __attribute__((weak)) int add(int,int);
> 
> int main(int argc, char *argv[]) {
> printf("%x",add);
> add(31,31);
> return 0;
> }
> terminal result : 0
> 
> Disassembly (on a 64bit AMD Machine):
> 0x0000000000401530 <+0>: push   rbp
>     0x0000000000401531 <+1>: mov    rbp,rsp
>     0x0000000000401534 <+4>: sub    rsp,0x20
>     0x0000000000401538 <+8>: mov    DWORD PTR [rbp+0x10],ecx
>     0x000000000040153b <+11>: mov    QWORD PTR [rbp+0x18],rdx
>     0x000000000040153f <+15>: call   0x402100 <__main>
>     0x0000000000401544 <+20>: mov    rdx,QWORD PTR [rip+0x2ed5]        #
> 0x404420 <.refptr.add>
>     0x000000000040154b <+27>: lea    rcx,[rip+0x2aae]        # 0x404000
>     0x0000000000401552 <+34>: call   0x402b18 <printf>
> => 0x0000000000401557 <+39>: mov    edx,0x1f
>     0x000000000040155c <+44>: mov    ecx,0x1f
>     0x0000000000401561 <+49>: call   0x0
>     0x0000000000401566 <+54>: mov    eax,0x0
>     0x000000000040156b <+59>: add    rsp,0x20
>     0x000000000040156f <+63>: pop    rbp
>     0x0000000000401570 <+64>: ret


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: There Might a Bug in the Compiler: When Calling Weak Defined Function
  2023-08-07 15:51 There Might a Bug in the Compiler: When Calling Weak Defined Function Şahin Duran
  2023-08-07 15:56 ` Richard Earnshaw (lists)
@ 2023-08-07 15:56 ` Andrew Pinski
  1 sibling, 0 replies; 3+ messages in thread
From: Andrew Pinski @ 2023-08-07 15:56 UTC (permalink / raw)
  To: Şahin Duran; +Cc: gcc

On Mon, Aug 7, 2023 at 8:52 AM Şahin Duran via Gcc <gcc@gcc.gnu.org> wrote:
>
> Dear GCC Developers,
>
> I think I've just discovered a bug/ undefined situation in the compiler.
> When I try to call a weakly defined function, compiler successfully
> generates the code of calling procedure. However, this calling procedure is
> nothing but branching to address 0 which results in segmentation fault. I
> am not sure if this is the case for the latest version of GCC but it is for
> GCC 4.9.2 and many online compilers. I just thought that maybe including a
> rule that generates compilation error when the user defines a weak function
> and calls it without actually implementing it. You may find the results in
> the attachments.

You need to check the address of weak defined symbol (function) to
make sure it is not a nullptr before calling it.
A weak defined symbol might have the address of nullptr if it is not
defined. And you are running into that.
This is a feature of elf and weak symbols.

Thanks,
Andrew

>
> Kind regards,
> I am looking forward to hearing from you about this.
> Şahin Duran
>
>
> Attachments:
>
> Source Code:
> #include <stdio.h>
> #include <stdlib.h>
> #include "header.h"
>
> __attribute__((weak)) int add(int,int);
>
> int main(int argc, char *argv[]) {
> printf("%x",add);
> add(31,31);
> return 0;
> }
> terminal result : 0
>
> Disassembly (on a 64bit AMD Machine):
> 0x0000000000401530 <+0>: push   rbp
>    0x0000000000401531 <+1>: mov    rbp,rsp
>    0x0000000000401534 <+4>: sub    rsp,0x20
>    0x0000000000401538 <+8>: mov    DWORD PTR [rbp+0x10],ecx
>    0x000000000040153b <+11>: mov    QWORD PTR [rbp+0x18],rdx
>    0x000000000040153f <+15>: call   0x402100 <__main>
>    0x0000000000401544 <+20>: mov    rdx,QWORD PTR [rip+0x2ed5]        #
> 0x404420 <.refptr.add>
>    0x000000000040154b <+27>: lea    rcx,[rip+0x2aae]        # 0x404000
>    0x0000000000401552 <+34>: call   0x402b18 <printf>
> => 0x0000000000401557 <+39>: mov    edx,0x1f
>    0x000000000040155c <+44>: mov    ecx,0x1f
>    0x0000000000401561 <+49>: call   0x0
>    0x0000000000401566 <+54>: mov    eax,0x0
>    0x000000000040156b <+59>: add    rsp,0x20
>    0x000000000040156f <+63>: pop    rbp
>    0x0000000000401570 <+64>: ret

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-08-07 15:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-07 15:51 There Might a Bug in the Compiler: When Calling Weak Defined Function Şahin Duran
2023-08-07 15:56 ` Richard Earnshaw (lists)
2023-08-07 15:56 ` Andrew Pinski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).