9
  1. What is the purpose of the SHT_NULL section in ELF?
  2. Is it referenced by the OS or the loader?
  3. What is the size of this section?
  4. Does it have anything to do with the NULL pointer ?
  5. Additionally, why does this section not have an entry in the section-segments mapping?

From the ELF specification:

SHT_NULL : This value marks the section header as inactive; it does not have an associated section. Other members of the section header have undefined values.

Bulat M.
  • 680
  • 9
  • 25
daemon155
  • 123
  • 1
  • 8

2 Answers2

8

One "use" of SHT_NULL is that it is mandatory for the first header table entry (index 0, named SHN_UNDEF), which is magic.

This is documented on the System V ABI Update (recommended by the LSB):

If the number of sections is greater than or equal to SHN_LORESERVE (0xff00), e_shnum has the value SHN_UNDEF (0) and the actual number of section header table entries is contained in the sh_size field of the section header at index 0 (otherwise, the sh_size member of the initial entry contains 0).

and:

Other section type values are reserved. As mentioned before, the section header for index 0 (SHN_UNDEF) exists, even though the index marks undefined section references. This entry holds the following.

followed by a table that specifies what each entry of he section header means in that special case. In particular, sh_type must be SHT_NULL.

From the above, this first section seems to exist to allow a large number of sections to be encoded, but I haven't tested kernel and compiler support.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
7

What is the purpose of the SHT_NULL section in elf?

It serves as a sentinel.

Is it referenced by the operating system or the loader?

No.

What is the size of this section ?

Zero (you can see that in readelf -WS a.out output) [but see below].

why does this section not have an entry in the section-segments mapping ?

Because that section is not present in any segment.


Normally, the very first section in every ELF file is of type SHT_NULL. In theory, a post-link tool may set the type of some other section to SHT_NULL as well (e.g. if its data is no longer needed), and all subsequent tools are supposed to ignore that section. This is not common.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • It seems the SHT_NULL section has no real purpose in the process' image. Is there any part of the elf that can be used to indicate to the OS to reserve a non-allocatable and non RWX area in memory to handle NULL pointer exceptions ? – daemon155 Nov 10 '14 at 03:41
  • @daemon155 I don't understand what you mean by "to handle NULL pointer exceptions". It's likely best to ask a new question, and provide as much detail as you can. – Employed Russian Nov 10 '14 at 03:59
  • 1
    Why is this sentinel required? Aren't all offsets / sizes well specified? – Ciro Santilli OurBigBook.com May 24 '15 at 08:34
  • I don't think it has a real purpose or was required for something. My guess would be that it was rather defined by the ELF standart to be the first entry, maybe they wanted to have a "dummy section and section header" – clockw0rk Oct 22 '19 at 21:11