The result of “size” command in linux and the values of text&edata&end are different

The code is

/* test_etext.c */

#include <unistd.h>
#include <stdio.h>

extern char etext, edata, end;

main(int argc, char *argv[])
    pid_t pid, ppid;

    pid = getpid();
    ppid = getppid();

    printf("%ld\t%ld\t%ld\n", &etext, &edata-&etext, &end-&edata);

    return 0;

Then compile and run it like this

gcc -o test_etext test_etext.c

4196125 2099507 8

size ./test_etext
text       data     bss     dec     hex filename
1532        592       8    2132     854 ./test_etext

The question is that why the segment data between "&edata-&etext" and "data" are different?


Of course they are different! Did you even read the manpage, man 3 etext?

  • etext is the first address past the end of the text (code),
  • edata is the first address past the end of the initialized data, and
  • end is the first address past the end of the uninitialized data (bss)

Due to address space randomization, you should not assume anything about the layout of the above in memory. Therefore, their differences convey no reliable information.

To calculate the respective sizes of the text, initialized data, and uninitialized data, you'd need to find the initial address of the same first (and substract that from the corresponding end address).

Since you tagged your question Linux, I'd personally just query the kernel's opinion, by reading the /proc/self/statm pseudo-file. If you need more detailed information (like details for dynamically-loaded libraries), or the information that matches the size command output, read /proc/self/smaps or /proc/self/maps pseudo-files. These are all described in detail in man 5 proc.

For executable ELF files -- similar to how size or objdump work, for example --, use either existing utilities, or existing libraries at least.

Need Your Help

Can the ErrorCode & SQLState combination be considered a unique ID for some exception?

java sql database jdbc error-code

In general, considering ErrorCode &amp; SQLState combination to be a unique ID for a specific exception, is it a safe thing to base your code logic on?

Why do I get “ERROR: While executing gem … (Gem::FilePermissionError)”?

ruby permissions gem rvm bundler

I uninstalled RVM and re-installed using a single user installation using:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.