ELF files in TLS and LOAD sections

int i; int main() { return i; } 

After -static compilation of readelf -l , the elf program headers are displayed:

 Elf file type is EXEC (Executable file) Entry point 0xxxxx30 There are 6 program headers, starting at offset 52 Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align LOAD 0x000000 0x08048000 0x08048000 0x79868 0x79868 RE 0x1000 > LOAD 0x079f94 0x080c2f94 0x080c2f94 0x0078c 0x02254 RW 0x1000 << NOTE 0x0000f4 0x080480f4 0x080480f4 0x00020 0x00020 R 0x4 > TLS 0x079f94 0x080c2f94 0x080c2f94 0x00010 0x0002c R 0x4 << GNU_STACK 0x000000 0x00000000 0x00000000 0x00000 0x00000 RW 0x4 PAX_FLAGS 0x000000 0x00000000 0x00000000 0x00000 0x00000 0x4 Section to Segment mapping: Segment Sections... 00 .note.ABI-tag .init .text __libc_freeres_fn .fini .rodata __libc_subfreeres __libc_atexit .eh_frame .gcc_except_table 01 .tdata .ctors .dtors .jcr .data.rel.ro .got .got.plt .data .bss __libc_freeres_ptrs 02 .note.ABI-tag 03 .tdata .tbss 

Can someone explain why the 2 and 4 program headers intersect (they start at the same offset 0x079f94 and VirtAddr 0x080c2f94).

In addition, the .tdata segment segment .tdata transmitted twice.

How will PT_TLS and PT_LOAD be loaded for the first stream (the program itself)? Where is .tbss in memory?

+4
linux glibc elf thread-local-storage
source share
3 answers

The first section of .tdata is the "initial image" of TLS data. These are the initial TLS vars values ​​that will be used in each thread (and in the main thread too). In crt (I assume), the original TLS image is copied to the TLS of the main stream. The same code is in pthread_create .

PT_TLS does not load because PT_LOAD does, and PT_LOAD already contains this PT_TLS. I think the PT_TLS for the original image is because it is shorter than all the local stream data (tbss + tdata> size (PT_TLS)).

+3
source share

TLS stands for "Thread-Local Storage".

To allow the merging of separate copies of the data allocated at compile time with separate execution threads, sections of the local thread storage can be used to indicate the size and initial content of such data. Implementations should not support local thread storage. The PT_TLS program entry has the following elements:

 Member Value p_offset File offset of the TLS initialization image p_vaddr Virtual memory address of the TLS initialization image p_paddr reserved p_filesz Size of the TLS initialization image p_memsz Total size of the TLS template p_flags PF_R p_align Alignment of the TLS template 

The TLS template is formed from a combination of all sections with the SHF_TLS flag. The part of the TLS template that contains the initialized data is a TLS initialization image. (The rest of the TLS template is one or more sections of type SHT_NOBITS.)

+2
source share

As for the memory areas, I think that the kernel looks only at the PT_LOAD segments and mmaps of them. (The kernel also looks at PT_GNU_STACK to find out if the stack should be mapped to Execute permission or not.) Look at binfmt_elf.c: load_elf_binary () for the corresponding code.

The PT_TLS segment is read by libc to determine the memory for thread-local storage configuration. Take a look at __libc_setup_tls () for the appropriate code.

The PT_TLS segment intersects with the PT_LOAD segment so that it is mapped to the process memory.

+2
source share

All Articles