Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8638549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:45:31+00:00 2026-06-12T10:45:31+00:00

I have this… weird problem and I’m desperately looking for a solution. We have

  • 0

I have this… weird problem and I’m desperately looking for a solution.

We have this example: (made for 32-bit systems)

#include <stdio.h>

//unsigned foo(unsigned arg_a, unsigned arg_b) {
unsigned foo(unsigned arg_a, ...) {
        unsigned *temp = (unsigned *)((unsigned)&arg_a + 4);
        return *temp + arg_a;
}

int main(void) {
        int i = foo(0xbe00, 0x00af);
        printf("We got a %x\n", i);
        return 0;
}

Function foo has 2 arguments. The goal is to “guess” the address of arg_b based on the address of the arg_a. This is based on the assumption that caller (main) will push arg_a and arg_b into the stack (so that (int)&arg_b – (int)&arg_a == 4 ).

Depending on the compiler and the optimization level, the output differs as follows:

 gcc -g -Wall -O0 test.c
 ./a.out 
We got a beaf
 gcc -g -Wall -O1 test.c
 ./a.out 
We got a beaf
 gcc -g -Wall -O2 test.c
 ./a.out 
We got a 80542b0
 gcc -g -Wall -O3 test.c
 ./a.out 
We got a 80542b0
 clang -g -Wall -O0 test.c
 ./a.out 
We got a 8054281
 clang -g -Wall -O1 test.c
 ./a.out 
We got a 805423f
 clang -g -Wall -O2 test.c
 ./a.out 
We got a b768d9d6
 clang -g -Wall -O3 test.c
 ./a.out 
We got a b76899d6

(The example is quite unstable and for example by putting a printf inside foo, gcc always prints “We got a beaf”. Clang.. not..)

The above example is just my way to try and be clear.

My REAL question (and my goal) is the following:
How can a extract the original address of arg_a, i.e. the address that the caller (main) pushed 0xbe00 in the stack by using CLANG from inside foo function? (gcc is not an option, although it’s still interesting)

Thanx for your time!

edit: made foo variadic in order for the question to make more sense…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T10:45:33+00:00Added an answer on June 12, 2026 at 10:45 am

    I don’t think this is going to work for you.

    First off, if gcc is your compiler, why don’t you just pull in the standard C library? If you don’t want to do that, what about pulling in just stdarg.h from the standard implementation?

    The version of stdarg.h I’m looking at (came with gcc 4.4.0 on Windows) is basically a bunch of macros that call compiler builtins – if you’ve got the right one for the compiler, then it should be easy, even if you don’t use the rest of the standard library (I can understand a reluctance to pull in that whole mess).

    If that doesn’t work for you, then try marking the function you are writing as variadic, and look in the compiler docs for how it builds the stack. You may well have to write some snippets of assembler depending on platform, but by declaring the function as variadic you’ll force the compiler to build the stack in a predictable way.

    If your compiler doesn’t support variadic arguments to functions, then you might want to reconsider your other options.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an element like this: <span class=tool_tip title=The full title>The ful&#8230;</span> This seems
Have this self-made slider: http://jsfiddle.net/wyc3P/4/ What it does: takes min and max values in
I have this managed bean: package com.DX_57.SR_57; /** include default packages for Beans */
I have this xml <entry id=1008 section=articles> <excerpt><p>&#8230; in Richtung „Aus für Tierversuche. Kosmetik-Fertigprodukte
have this problem with PCACompute in Android Opencv2.3.1 because when i call PCACompute my
Have this bit of code in my app... Request::instance()->directory After upgrading to 3.1 it
I have this idea for a free backup application. The largest problem I need
Have this query: SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt
I have this string 2012-06-27 16:17:06 and I want to convert it to GMT
I have this application where I implement the ActionBar Fragment interface. Underlying the interface,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.