public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/66960] New: Add a builtin to get the address of the current stack frame
@ 2015-07-21 17:07 hjl.tools at gmail dot com
  2015-07-21 21:46 ` [Bug target/66960] " hjl.tools at gmail dot com
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: hjl.tools at gmail dot com @ 2015-07-21 17:07 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66960

            Bug ID: 66960
           Summary: Add a builtin to get the address of the current stack
                    frame
           Product: gcc
           Version: 6.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hjl.tools at gmail dot com
  Target Milestone: ---

The initial i386 stack layout is:

    environ     4*argc + 4 + %esp
    0                               4 bytes
    envp        8 + 4*argc + %esp   N * 4 bytes
    0           4 + 4*argc + %esp   4 bytes
    argv        %esp + 4            4*argc bytes
    argc        %esp                4 bytes

To get argc, argv and environ in C, we can use

---
extern char **environ;
extern void exit (int status);
extern int main (int argc, char **argv, char **envp);

void
_start (void)
{
  void *argc_p = __builtin_frame_address (0) + 4;
  char **argv = (char **) (argc_p + 4);
  int argc = *(int *) argc_p;
  int status;

  environ = argv + argc + 1;

  status = main (argc, argv, environ);

  exit (status);
}
---

With -O2 -m32 -miamcu, we generate

_start:
        pushl   %ebp
        movl    %esp, %ebp
        movl    4(%ebp), %eax
        leal    8(%ebp), %edx
        leal    4(%edx,%eax,4), %ecx
        movl    %ecx, environ
        call    main
        call    exit
        .size   _start, .-_start

%ebp is used since __builtin_frame_address always keeps the frame
pointer.  With a new builtin, __builtin_current_frame, which returns
the top of the stack frame, we can use

---

extern char **environ;
extern void exit (int status);
extern int main (int argc, char **argv, char **envp);

void
_start (void)
{
  void *argc_p = __builtin_current_frame ();
  char **argv = (char **) (argc_p + 4);
  int argc = *(int *) argc_p;
  int status;

  environ = argv + argc + 1;

  status = main (argc, argv, environ);

  exit (status);
}
---

and generate

_start:
        movl    (%esp), %eax
        leal    4(%esp), %edx
        leal    8(%esp,%eax,4), %ecx
        movl    %ecx, environ
        call    main
        call    exit
        .size   _start, .-_start

We can avoid using %ebp.

__builtin_current_frame may not be the best name.  We can use
__builtin_stack_top, __builtin_top_of_stack, which gives us the
stack address when the function is called.


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

end of thread, other threads:[~2021-08-10 21:37 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-21 17:07 [Bug middle-end/66960] New: Add a builtin to get the address of the current stack frame hjl.tools at gmail dot com
2015-07-21 21:46 ` [Bug target/66960] " hjl.tools at gmail dot com
2015-09-04 15:48 ` [Bug target/66960] Need a builtin function to access interrupt or exception data hjl.tools at gmail dot com
2015-09-29 22:17 ` [Bug target/66960] Add interrupt/exception attribute to x86 backend hjl.tools at gmail dot com
2016-07-04 10:39 ` [Bug target/66960] Add interrupt " goswin-v-b at web dot de
2021-08-10 21:37 ` pinskia at gcc dot gnu.org

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).