Linux: Display Binary Object Section Size, Text & Data Segment Information

How do I find out the size of the bss, text segment and data segment for each object or binary file under Linux operating systems?

You need to use the size command which lists the section sizes and the total size for each of the object or archive files objfile. By default, one line of output is generated for each object file or each module in an archive. The syntax is as follows:

size /path/to/file
To display information about httpd file, enter:
size /usr/sbin/httpd
Sample outputs:

   text	   data	    bss	    dec	    hex	filename
 314213	  12376	  13304	 339893	  52fb5	/usr/sbin/httpd

Where,

  • text – Actual machine instructions that your CPU going to execute. Linux allows to share this data.
  • data – All initialized variables (declarations) declared in a program (e.g., float salary=123.45;).
  • bss – The BSS consists of uninitialized data such as arrays that you have not set any values to or null pointers.

The above information is useful for debugging problems. This is mainly used by C or systems programmers.

readelf Command

The readelf command displays information about ELF files:
readelf -a /path/to/binary
readelf -a /usr/sbin/lighttpd

Sample outputs:

ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           Advanced Micro Devices X86-64
  Version:                           0x1
  Entry point address:               0x407190
  Start of program headers:          64 (bytes into file)
  Start of section headers:          1087056 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)
  Number of program headers:         8
  Size of section headers:           64 (bytes)
  Number of section headers:         42
  Section header string table index: 39

Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
  [ 0]                   NULL             0000000000000000  00000000
...
.....
..
Notes at offset 0x0000021c with length 0x00000020:
  Owner		Data size	Description
  GNU		0x00000010	NT_VERSION (version)

Library list section '.gnu.liblist' contains 17 entries:
     Library              Time Stamp          Checksum   Version Flags
  0: libpcre.so.0         2010-01-21T10:04:47 0xa92f31ac 0       0      
  1: libdl.so.2           2010-01-21T10:04:34 0xe5302dd5 0       0      
  2: libssl.so.6          2010-03-26T09:03:45 0xdcc642fd 0       0      
  3: libcrypto.so.6       2010-03-26T09:03:45 0x75a9890c 0       0      
  4: libfam.so.0          2010-01-21T10:04:47 0xee6e4843 0       0      
  5: libc.so.6            2010-01-21T10:04:34 0x3d92b5fb 0       0      
  6: /lib64/ld-linux-x86- 2010-01-21T10:04:34 0xff573d14 0       0      
  7: libgssapi_krb5.so.2  2010-01-21T10:04:35 0xbbf0f499 0       0      
  8: libkrb5.so.3         2010-01-21T10:04:35 0xdf4c927c 0       0      
  9: libcom_err.so.2      2010-01-21T10:04:35 0x7743c016 0       0      
 10: libk5crypto.so.3     2010-01-21T10:04:35 0x67f0cba5 0       0      
 11: libz.so.1            2010-01-21T10:04:35 0xfb0fcfad 0       0      
 12: libkrb5support.so.0  2010-01-21T10:04:35 0xead7250b 0       0      
 13: libkeyutils.so.1     2010-01-21T10:04:35 0x0103e8ff 0       0      
 14: libresolv.so.2       2010-01-21T10:04:34 0xcaf7fb04 0       0      
 15: libselinux.so.1      2010-01-21T10:04:35 0xd8912c16 0       0      
 16: libsepol.so.1        2010-01-21T10:04:35 0xad9000b6 0       0      

The above output can be used to debug and build complex security chrooted jails for lighttpd, nginx, bind servers.

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.