LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-09-2022, 09:31 AM   #1
Racho
Member
 
Registered: Oct 2021
Posts: 59

Rep: Reputation: Disabled
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?
 
Old 12-09-2022, 09:40 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
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.
 
1 members found this post helpful.
Old 12-09-2022, 10:03 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Might be useful: https://linux.die.net/man/3/etext
 
2 members found this post helpful.
Old 12-09-2022, 10:08 AM   #4
Racho
Member
 
Registered: Oct 2021
Posts: 59

Original Poster
Rep: Reputation: Disabled
Ohhh... too bad!

Thanks anyway!
 
Old 12-09-2022, 10:25 AM   #5
Racho
Member
 
Registered: Oct 2021
Posts: 59

Original Poster
Rep: Reputation: Disabled
Thanks a lot NevemTeve!

I've added that addresses to my little program
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to access base address and size of .text, .data, .bss segments using GNU as phoenix@guru Programming 0 04-22-2013 06:44 AM
[SOLVED] calculating average segment after segment using AWK vjramana Programming 2 03-21-2012 01:08 AM
bss david1941 General 2 11-01-2010 09:57 AM
section '.bss' type changed to PROGBITS timboar Linux - General 2 11-01-2010 09:07 AM
"relocation R_X86_64_32 against `.bss' can not be used when making a shared..." error aryan1 Programming 7 01-21-2010 08:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration