1

I understand an ELF file contains segments and sections, and headers that provide info about their contents. Additionally ELF sections are what organize data within the segments.

In the below example you can clearly see the .text and .data sections listed in the readelf output, but the program headers don't list "text" or "data" segments.

What is the difference between text and data segments vs .text and .data sections in ELF file?

example hello world program:

#include <stdio.h>
int main()
{
    puts("hello world");
    return 0;
}

Display ELF segment headers:

$ readelf -l hello

Elf file type is EXEC (Executable file)
Entry point 0x8049050
There are 11 program headers, starting at offset 52

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  PHDR           0x000034 0x08048034 0x08048034 0x00160 0x00160 R   0x4
  INTERP         0x000194 0x08048194 0x08048194 0x00013 0x00013 R   0x1
      [Requesting program interpreter: /lib/ld-linux.so.2]
  LOAD           0x000000 0x08048000 0x08048000 0x002e8 0x002e8 R   0x1000
  LOAD           0x001000 0x08049000 0x08049000 0x0022c 0x0022c R E 0x1000
  LOAD           0x002000 0x0804a000 0x0804a000 0x0019c 0x0019c R   0x1000
  LOAD           0x002f0c 0x0804bf0c 0x0804bf0c 0x00110 0x00114 RW  0x1000
  DYNAMIC        0x002f14 0x0804bf14 0x0804bf14 0x000e8 0x000e8 RW  0x4
  NOTE           0x0001a8 0x080481a8 0x080481a8 0x00044 0x00044 R   0x4
  GNU_EH_FRAME   0x002014 0x0804a014 0x0804a014 0x0004c 0x0004c R   0x4
  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0x10
  GNU_RELRO      0x002f0c 0x0804bf0c 0x0804bf0c 0x000f4 0x000f4 R   0x1

  Section to Segment mapping:
  Segment Sections...
  00     
  01     .interp 
  02     .interp .note.gnu.build-id .note.ABI-tag .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rel.dyn .rel.plt 
  03     .init .plt .text .fini 
  04     .rodata .eh_frame_hdr .eh_frame 
  05     .init_array .fini_array .dynamic .got .got.plt .data .bss 
  06     .dynamic 
  07     .note.gnu.build-id .note.ABI-tag 
  08     .eh_frame_hdr 
  09     
  10     .init_array .fini_array .dynamic .got
sf_admin
  • 577
  • 4
  • 11
  • Segments are mostly for runntime. Sections for linktime. Read [*linkers and loaders*](https://www.iecc.com/linker/). Inside your program, read `/proc/self/maps`. See [proc(5)](http://man7.org/linux/man-pages/man5/proc.5.html) and [pmap(1)](http://man7.org/linux/man-pages/man1/pmap.1.html) – Basile Starynkevitch Dec 29 '19 at 17:11
  • but is there a difference between .text and .data sections and text and data segments in an ELF file? or do people just mix up segments and sections? – sf_admin Dec 29 '19 at 17:25
  • Did you read wikipage on [Executable and Linkable Format](https://en.wikipedia.org/wiki/Executable_and_Linkable_Format) and [elf(5)](http://man7.org/linux/man-pages/man5/elf.5.html) man page ? – Basile Starynkevitch Dec 29 '19 at 18:01
  • My question wasn't about the general differences between segments and sections, but specifically what people refer to as the "text segment" and "data segment", as they are not listed in readelf output, only .text and .data "sections" are listed. – sf_admin Jan 02 '20 at 04:19
  • 1
    I found the answer to my question, and would like to post the answer. The upper LOAD segment has Read and Execute permission, This is a "text segment". A text segment contains read-only instructions and read-only data. The lower LOAD has Read and Write permission. This is a "data segment". – sf_admin Jan 02 '20 at 04:19

1 Answers1

-1

Segments are for execution of the file, when they run. Sections are data files for linking and relocation. Dot(.) is shortcut to directory(on Unix) Those comes out, when you are finding PATHs. On Linux, shell don't by defaoult, find from directory, if its not in PATH. But if you like, you can chance that with Vim, Nano etc. from your /Home/.profile

Lapasorsa
  • 11
  • 1