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 6071435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:02:48+00:00 2026-05-23T10:02:48+00:00

Here’s something less-than-important that I’ve been musing about recently: I know that my program’s

  • 0

Here’s something less-than-important that I’ve been musing about recently:

I know that my program’s virtual address space contains the stack (of each thread) and a heap and some statically allocated memory and all that. But does it also contain the program’s image with all the instructions? And is it possible somehow (no matter how platform dependent the trick) to find out the address range of my own image? Is the memory read-only?

In short: Can I make a program that prints itself?

If that can’t be done, a lesser question would be, can I print my own stack? I was thinking something like this:

const char * BASE;

void print_stack();

int main(int argc, char * argv[]) {
  BASE = &argc;
  /* do stuff */
  print_stack();
  return 0;
}

void print_stack() {
  int sentinel;
  const char * bottom = &sentinel;
  while (bottom < BASE)
    printf("%02X ", *bottom++);
}
  • 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-05-23T10:02:48+00:00Added an answer on May 23, 2026 at 10:02 am

    To answer your first question, of course it contains your program’s instructions: you can only execute what you can access. To get at the address of your instructions, you can take the address of a function and start printing from there. You can then use a library like udis86 to disassemble them. Note however, that your compiler isn’t required to order the functions in any specific way, so starting at main and reading from there isn’t guaranteed to get everything, might trample on un-allocated memory.

    To get at the entire instruction memory range (you’re looking for the .text segment), you can look up the address+size from your operating system (In Linux, that info will be in /proc/[pid]/maps, in OS X you can either use vmmap or ask the kernel via the mach_vm_region() kernel trap), and then just read the memory directly. You can also use nm to dump the symbols of your program, isolate all that point to the .text segment (They should be marked with T in nm output) and dump those. This is not a good method, since you’d have to disassemble everything to determine where they end in the case there’s padding between them.

    All the mapped memory is accessible, but not all of it will be writeable (The .text segment wouldn’t be). One thing to keep in mind, the addresses will probably not be stable invocation to invocation if your operating system implements ASLR.

    To address your second question, yes you can print your own stack and symbolicate it with the help of third-party libraries, but not the way you’re trying to do it. Stack typically grows down (i.e. Starts at a high address and moves towards lower addresses. As an exercise to the reader, disassemble one of your functions via gdb or another disassembler and look how memory on the stack gets allocated during your function prolog), so your for-loop will never run as BASE will probably always be larger than the address of sentinel.

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

Sidebar

Related Questions

Here's what I'm trying to accomplish with this program: a recursive method that checks
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here is a simplification of my database: Table: Property Fields: ID, Address Table: Quote
Here's a coding problem for those that like this kind of thing. Let's see
Here is the scenario: I'm writing an app that will watch for any changes
Here's an interesting problem. On a recently installed Server 2008 64bit I opened IE
Here is a great article about GC may occur at unexpected point of code

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.