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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:05:14+00:00 2026-06-11T22:05:14+00:00

Would like to have an executable save its state by modifying its own global

  • 0

Would like to have an executable save its state by modifying its own global constants. Just for the kicks of having a totally self-contained executable.

A few solutions/hacks that come to mind:

  1. Use libelf and have the program parse itself to find the offset.
  2. Add a specific marker and just search for it in the executable file. I guess this might even be somewhat cross-platform?
  3. Use object dumping utils to determine the address in the executable file. This probably needs to be always done as a post-process to project build..

It would be neat to have the linker provide this info.

Is it possible to have the linker provide the offset of a read-only section in the executable file?

Thanks

  • 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-11T22:05:15+00:00Added an answer on June 11, 2026 at 10:05 pm

    You are essentially talking about binary rewriting. One method to achieve this without fiddling with the compile process is to map a virtual address to a physical one and then patch it. Interestingly, this is something I covered in my master’s thesis. The following images and text are pulled from that document:

    http://localhostr.com/file/hyB1iFuiL0nV/Loading_Binary.jpg

    Note that the concept behind my original project was to rewrite code in other binaries assuming that the compile process could not be modified. If your requirements and assumptions are different, this may well not be the easiest and best approach.

    The most important idea here is that a section in the disk representation is preserved (not split) when it is mapped into memory. This means data that is at a certain offset into the section in the disk representation will be offset by the same amount after loaded into memory.

    In libelf, similarly to libbfd, an executable contains a set of sections in which both code and data can reside. When the operating system loads the executable into memory, each section is based at some base address. We can reverse this to map a virtual memory address to a physical file offset. If a physical file offset can be found, the bytes can be patched as a regular file.

    • First, the section header of the executable is parsed with libelf.
      This allows us to obtain a set of sections and most importantly, for
      each section libelf can tell us three things:

      1. Section size The size of the section.
      2. Section base address The address that the section will be based at when the executable on disk is loaded into memory.
      3. Section disk offset The disk offset of the section.
    • By iterating through the section information extracted in the previous step, it is possible to find out what section an arbitrary virtual memory address is contained in. During patching, the memory address we are interested in is the address of the code at which a detour is to be written. The offset of the virtual memory address into the section can be calculated by (virtual_memory_address - section_base_address).
    • Therefore, the disk offset of the virtual memory address can be calculated by (section_disk_offset + (virtual_memory_address - section_base_address)).

    This process allows an arbitrary virtual memory address to be mapped to its corresponding disk file offset. This offset can then be patched with regular C file IO functions such as fopen/fseek/fwrite/fclose.

    This is my code for mapping a virtual address to a physical file offset using the above steps:

    /*
     * Returns the corresponding 32 bit executable file offset of a virtual memory
     * address.
     */
    uint32_t vaddr32_to_file_offset(char * filepath, uint32_t vaddr)
    {
        int      fd     = open(filepath, O_RDONLY);
        Elf *    e      = elf_begin(fd, ELF_C_READ, NULL);
        uint32_t offset = 0;
    
        Elf_Scn * scn = NULL;
        while((scn = elf_nextscn(e, scn)) != NULL) {
            Elf32_Shdr * shdr = elf32_getshdr(scn);
            if(vaddr >= shdr->sh_addr &&
                    (vaddr <= (shdr->sh_addr + shdr->sh_size))) {
                offset = shdr->sh_offset + (vaddr - shdr->sh_addr);
                break;
            }
        }
    
        elf_end(e);
        close(fd);
        return offset;
    }
    
    /*
     * Returns the corresponding 64 bit executable file offset of a virtual memory
     * address.
     */
    uint64_t vaddr64_to_file_offset(char * filepath, uint64_t vaddr)
    {
        int      fd     = open(filepath, O_RDONLY);
        Elf *    e      = elf_begin(fd, ELF_C_READ, NULL);
        uint64_t offset = 0;
    
        Elf_Scn * scn = NULL;
        while((scn = elf_nextscn(e, scn)) != NULL) {
            Elf64_Shdr * shdr = elf64_getshdr(scn);
            if(vaddr >= shdr->sh_addr &&
                    (vaddr <= (shdr->sh_addr + shdr->sh_size))) {
                offset = shdr->sh_offset + (vaddr - shdr->sh_addr);
                break;
            }
        }
    
        elf_end(e);
        close(fd);
        return offset;
    }
    

    This is the code to patch an ELF executable given an offset:

    /*
     * Sets the bytes at an arbitrary offset of a file to the contents of buffer.
     */
    static bool patch_file(char * filepath, uint64_t offset, void * buffer,
            size_t size)
    {
        FILE * pFile = fopen(filepath, "r+");
    
        if(pFile == NULL) {
            return FALSE;
        }
    
        fseek(pFile, offset, SEEK_SET);
        fwrite(buffer, 1, size, pFile);
        fclose(pFile);
        return TRUE;
    }
    

    More detailed information can be found in the report itself which is publicly available here.

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

Sidebar

Related Questions

I would like to have a more colorful Python prompt in the terminal, just
I have an executable module created by third party. I would like to inject
I would like to have a windows executable (.exe-file) which starts a given command.
I have an executable I compiled with C# and I would like to dump
Every executable must have an ELF header? Also i would like to know why
I have two .c files that I would like to compile into on executable.
So in my project i would like have a nice treeview that has images.
I would like to have an AppWidget that designed like this one . Image:
I would like to have the cursor in the JavaDoc area when creating interfaces,
I would like to have a field which is updated on every change(insertion, modification),

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.