LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How can i get the .data, .text and .bss segment addresses in C? (https://www.linuxquestions.org/questions/programming-9/how-can-i-get-the-data-text-and-bss-segment-addresses-in-c-4175719587/)

Racho 12-09-2022 09:31 AM

How can i get the .data, .text and .bss segment addresses in C?
 
Hi there!
I've made this program to show the memory map of a program in linux:

Code:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int una_funcion()
{
        return 0;
}
int global_uninitialized;
int global_initialized=1;

int main()
{
        char *mem;
        register void *stack1 asm("ebp");
        int i=100;
        int j;
        static int static_var=10;
        static int static_var2;
        mem = malloc(100);
        if (mem == NULL)
                return -1;
        printf("64 bit memory addresses are:\n");
        printf("        +-----------------------------------------------+  0xffff ffff ffff ffff\n");
        printf("        |                                              |\n");
        printf("        |              kernel segment                  |      kernel space\n");
        printf("        |                                              |\n");
        printf("        +-----------------------------------------------+  0xffff 8000 0000 0000\n");
        printf("        |                                              |\n");
        printf("        |            Non-canonical space              |      unused\n");
        printf("        |                                              |\n");
        printf("        +-------------------+--+-------------------+----+  0x0000 7fff ffff ffff\n");
        printf("        | stack 1          |  | stack 2          | ....\n");
        printf("        | ...              |  | ...              |\n");
        printf("        | library mapping1  |  | library mapping2  |\n");
        printf("        | ...              |  | ...              |\n");
        printf("        | Heap1            |  | Heap2            |            User space\n");
        printf("        | bss segment1      |  | bss segment2      |\n");
        printf("        | data segment1    |  | data segment2    |\n");
        printf("        | text1            |  | text2            |\n");
        printf("        +-------------------+  +-------------------+      0x0000 0000 0000 0000\n");
        printf("\n");

        printf("Stack begins at: ebp:\t\t\t%p\n", stack1);
        printf("In the stack, local variable:\t\t%p\n", &j);
        printf("In the stack, local variable:\t\t%p\n", &i);
        printf("In library mappings: printf:\t\t%p\n", printf);
        printf("In library mappings: fork:\t\t%p\n", fork);
        printf("In heap segment: reserva con malloc:\t%p\n", mem);
        printf("In .bss uninitialized global var:\t%p\n", &global_uninitialized);
        printf("In .data initialized gloval var:\t%p\n", &global_initialized);
        printf("In .data local static inizialized:\t%p\n", &static_var);
        printf("In .data local static uninizialized:\t%p\n", &static_var2);
        printf("In .text segment: main():\t\t%p\n", main);
        printf("In .text segment: funcion:\t\t%p\n", una_funcion);
        free(mem);

        return 0;
}

I would like to add the start address of .text, .data and .bss segments. And it would be great to add the ending addresses too!
Can anyone help me?

pan64 12-09-2022 09:40 AM

I'm afraid they do not exist. They are only used when compiling and building the executable, but you won't need them in the end.

NevemTeve 12-09-2022 10:03 AM

Might be useful: https://linux.die.net/man/3/etext

Racho 12-09-2022 10:08 AM

Ohhh... too bad!

Thanks anyway!

Racho 12-09-2022 10:25 AM

Thanks a lot NevemTeve!

I've added that addresses to my little program


All times are GMT -5. The time now is 06:29 PM.