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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:07:05+00:00 2026-06-06T14:07:05+00:00

I have a linux C program that handles request sent to a TCP socket

  • 0

I have a linux C program that handles request sent to a TCP socket (bound to a particular port). I want to be able to query the internal state of the C program via a request to that port, but I dont want to hard code what global variables can be queried. Thus I want the query to contain the string name of a global and the C code to look that string up in the symbol table to find its address and then send its value back over the TCP socket. Of course the symbol table must not have been stripped. So can the C program even locate its own symbol table, and is there a library interface for looking up symbols given their name? This is an ELF executable C program built with gcc.

  • 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-06T14:07:07+00:00Added an answer on June 6, 2026 at 2:07 pm

    This is actually fairly easy. You use dlopen / dlsym to access symbols. In order for this to work, the symbols have to be present in the dynamic symbol table. There are multiple symbol tables!

    #include <dlfcn.h>
    #include <stdio.h>
    
    __attribute__((visibility("default")))
    const char A[] = "Value of A";
    
    __attribute__((visibility("hidden")))
    const char B[] = "Value of B";
    
    const char C[] = "Value of C";
    
    int main(int argc, char *argv[])
    {
        void *hdl;
        const char *ptr;
        int i;
    
        hdl = dlopen(NULL, 0);
        for (i = 1; i < argc; ++i) {
            ptr = dlsym(hdl, argv[i]);
            printf("%s = %s\n", argv[i], ptr);
        }
        return 0;
    }
    

    In order to add all symbols to the dynamic symbol table, use -Wl,--export-dynamic. If you want to remove most symbols from the symbol table (recommended), set -fvisibility=hidden and then explicitly add the symbols you want with __attribute__((visibility("default"))) or one of the other methods.

    ~ $ gcc dlopentest.c -Wall -Wextra -ldl
    ~ $ ./a.out A B C
    A = (null)
    B = (null)
    C = (null)
    ~ $ gcc dlopentest.c -Wall -Wextra -ldl -Wl,--export-dynamic
    ~ $ ./a.out A B C
    A = Value of A
    B = (null)
    C = Value of C
    ~ $ gcc dlopentest.c -Wall -Wextra -ldl -Wl,--export-dynamic -fvisibility=hidden
    ~ $ ./a.out A B C
    A = Value of A
    B = (null)
    C = (null)
    

    Safety

    Notice that there is a lot of room for bad behavior.

    $ ./a.out printf
    printf = ▯▯▯▯ (garbage)
    

    If you want this to be safe, you should create a whitelist of permissible symbols.

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

Sidebar

Related Questions

I have a program that links against libmysqld. Under linux I am able to
So I have a Linux program that runs in a while(true) loop, which waits
I have a program that I intend to install on Linux and Windows machines.
I have a python-based program that reads serial data off an a port connected
I have a linux console application - a scientific simulation program that I use.
I am trying to port a Windows program to Linux. I have never programmed
I have a program that accesses a device through the serial port and reads
Writing a program that should be portable in Linux and Windows enviroments I have
I have developped a C program (Linux), this program create a new file and
I have been asked to write a java program on linux platform. According to

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.