What is the purpose of 00 segment?
Peng Yu
pengyu.ut@gmail.com
Sat Apr 24 00:23:52 GMT 2021
On Fri, Apr 23, 2021 at 5:57 PM Fangrui Song <i@maskray.me> wrote:
>
> On Fri, Apr 23, 2021 at 12:13 PM Peng Yu via Binutils
> <binutils@sourceware.org> wrote:
> >
> > On Fri, Apr 23, 2021 at 12:19 PM Peng Yu <pengyu.ut@gmail.com> wrote:
> > >
> > > Hi,
> > >
> > > The following example shows there are two segments. But only the 2nd
> > > segment is relevant to .text. The 1st segment just contains the ELF
> > > header and something (is the "something" relevant to running the
> > > program?). If the first segment is removed, the program still runs?
> > >
> > > I don't get why the first segment is there. Can anybody explain? Thanks.
> > >
> > > $ cat main.asm
> > > global _start
> > > _start:
> > > SYS_EXIT equ 60
> > > mov rax, SYS_EXIT
> > > mov rdi, 42
> > > syscall
> > > $ nasm -felf64 main.asm
> > > $ ld -o main.exe main.o
> > > $ readelf -lW main.exe
> > >
> > > Elf file type is EXEC (Executable file)
> > > Entry point 0x401000
> > > There are 2 program headers, starting at offset 64
> > >
> > > Program Headers:
> > > Type Offset VirtAddr PhysAddr
> > > FileSiz MemSiz Flg Align
> > > LOAD 0x000000 0x0000000000400000 0x0000000000400000
> > > 0x0000b0 0x0000b0 R 0x1000
> >
> > I edited the binary of the file to change LOAD to NULL for the above
> > segment. The binary still runs. So this segment is not necessarily.
> >
> > The question is still why this useless segment is in the generated
> > file. It seems to be entirely unnecessary.
>
> It's not useless. There is a convention that the ELF header and
> program headers need to be loaded.
> The information is at least used by loaders.
If the first segment is used by the loader, why when I change the
first segment type from LOAD to NULL, the prog still runs? Is that
NULL means that it is ignored by loader? If it can run with NULL, then
at least it is not essential. Why keeping a nonessential segment in
the ELF file?
> The first PT_LOAD serves this purpose.
>
> > > LOAD 0x001000 0x0000000000401000 0x0000000000401000
> > > 0x00000c 0x00000c R E 0x1000
> > >
> > > Section to Segment mapping:
> > > Segment Sections...
> > > 00
> > > 01 .text
> > > $ readelf -SW main.exe
> > > There are 5 section headers, starting at offset 0x1120:
> > >
> > > Section Headers:
> > > [Nr] Name Type Address Off Size
> > > ES Flg Lk Inf Al
> > > [ 0] NULL 0000000000000000 000000
> > > 000000 00 0 0 0
> > > [ 1] .text PROGBITS 0000000000401000 001000
> > > 00000c 00 AX 0 0 16
> > > [ 2] .symtab SYMTAB 0000000000000000 001010
> > > 0000c0 18 3 4 8
> > > [ 3] .strtab STRTAB 0000000000000000 0010d0
> > > 00002b 00 0 0 1
> > > [ 4] .shstrtab STRTAB 0000000000000000 0010fb
> > > 000021 00 0 0 1
> > > Key to Flags:
> > > W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
> > > L (link order), O (extra OS processing required), G (group), T (TLS),
> > > C (compressed), x (unknown), o (OS specific), E (exclude),
> > > l (large), p (processor specific)
> > >
> > > --
> > > Regards,
> > > Peng
> >
> >
> >
> > --
> > Regards,
> > Peng
--
Regards,
Peng
More information about the Binutils
mailing list