Alignment and sizes of note sections in 64-bit ELF objects

158 views
Skip to first unread message

H.J. Lu

unread,
Apr 3, 2017, 11:16:42 AM4/3/17
to Generic System V Application Binary Interface
According to gABI, in 64-bit objects, each note entry is an array of 8-bye
words in the format of the target processor. But I got

[ 2] .note.ABI-tag NOTE 0000000000000254 000254
000020 00 A 0 0 4
[ 3] .note.gnu.build-id NOTE 0000000000000274 000274
000024 00 A 0 0 4

on Linux/x86-64. .note.ABI-tag size is 32, but it isn't aligned at 8 bytes.
.note.gnu.build-id size is 36, which isn't multiple of 8 bytes. Should
note sections in 64-bit ELF objects be multiple of 8 bytes as well as
aligned to 8 bytes?

--
H.J.

Ian Lance Taylor

unread,
Apr 3, 2017, 11:19:28 AM4/3/17
to gener...@googlegroups.com
This is the comment I wrote in gold when I looked into this:

// Authorities all agree that the values in a .note field should
// be aligned on 4-byte boundaries for 32-bit binaries. However,
// they differ on what the alignment is for 64-bit binaries.
// The GABI says unambiguously they take 8-byte alignment:
// http://sco.com/developers/gabi/latest/ch5.pheader.html#note_section
// Other documentation says alignment should always be 4 bytes:
// http://www.netbsd.org/docs/kernel/elf-notes.html#note-format
// GNU ld and GNU readelf both support the latter (at least as of
// version 2.16.91), and glibc always generates the latter for
// .note.ABI-tag (as of version 1.6), so that's the one we go with
// here.

Ian

Suprateeka R Hegde

unread,
Apr 3, 2017, 2:48:22 PM4/3/17
to gener...@googlegroups.com
On 03-Apr-2017 08:49 PM, Ian Lance Taylor wrote:
> On Mon, Apr 3, 2017 at 8:16 AM, H.J. Lu <hjl....@gmail.com> wrote:
>> According to gABI, in 64-bit objects, each note entry is an array of 8-bye
>> words in the format of the target processor. But I got
>>
>> [ 2] .note.ABI-tag NOTE 0000000000000254 000254
>> 000020 00 A 0 0 4
>> [ 3] .note.gnu.build-id NOTE 0000000000000274 000274
>> 000024 00 A 0 0 4
>>
>> on Linux/x86-64. .note.ABI-tag size is 32, but it isn't aligned at 8 bytes.
>> .note.gnu.build-id size is 36, which isn't multiple of 8 bytes. Should
>> note sections in 64-bit ELF objects be multiple of 8 bytes as well as
>> aligned to 8 bytes?

On HP-UX, it is 8 byte alignment. And the size becomes multiple of 8
bytes automatically, as we ensure 8 byte alignment for the next note
entry. This is as per the gABI.

> This is the comment I wrote in gold when I looked into this:
>
> // Authorities all agree that the values in a .note field should
> // be aligned on 4-byte boundaries for 32-bit binaries. However,
> // they differ on what the alignment is for 64-bit binaries.
> // The GABI says unambiguously they take 8-byte alignment:
> // http://sco.com/developers/gabi/latest/ch5.pheader.html#note_section
> // Other documentation says alignment should always be 4 bytes:
> // http://www.netbsd.org/docs/kernel/elf-notes.html#note-format

Thats interesting. But why should it differ from GABI?

May be because: In both ILP32 and LP64 model, integer is 4 bytes. And
the documentation says "integer" in parenthesis for the fields.

For example:
Name Size
4 bytes (integer)
Desc Size
4 bytes (integer)

In contrast to GABI, NetBSD seems to have attributed these fields with
"integer".

--
Supra

H.J. Lu

unread,
Nov 16, 2017, 11:41:35 AM11/16/17
to Generic System V Application Binary Interface
Linux has been using 4 byte alignment for .note.ABI-tag note and
.note.gnu.build-id note in 64-bit objects. We can't change their
alignment to 8 bytes. Can we add a footnote in gABI to address it?

In reality, this isn't a real issue since all notes in one PT_LOAD segment
must have the same alignment which equals to p_align. Note parser
can use p_align of PT_LOAD segment for note alignment, instead of
assuming alignment based on ELF file class.

BTW, should we document that all notes in one PT_LOAD segment
must have the same alignment which equals to p_align?

--
H.J.

Suprateeka R Hegde

unread,
Nov 20, 2017, 10:27:35 AM11/20/17
to gener...@googlegroups.com, H.J. Lu, gnu-...@sourceware.org
(I took a while to recollect the discussion)
It should be more clear than that. See below.

> In reality, this isn't a real issue since all notes in one PT_LOAD segment
> must have the same alignment which equals to p_align.

p_align of PT_LOAD or PT_NOTE?

> Note parser
> can use p_align of PT_LOAD segment for note alignment,

Why not p_align of PT_NOTE? p_align of PT_LOAD seems to be (on my
Ubuntu) set to the 2MiB pagesize value.

> instead of
> assuming alignment based on ELF file class.

The gABI description of ELF class based alignment may be because gABI
does not talk anything about PT_NOTE actually being part of PT_LOAD.
PT_NOTE could be a separate segment on its own outside PT_LOAD, though
almost all implementations make it part of PT_LOAD.

> BTW, should we document that all notes in one PT_LOAD segment
> must have the same alignment which equals to p_align?

Why not p_align of PT_NOTE? And p_align of PT_NOTE may be set to the
maximum of all sh_addralign of all SHT_NOTE that make up the PT_NOTE.

--
Supra

H.J. Lu

unread,
Nov 20, 2017, 10:51:11 AM11/20/17
to Suprateeka R Hegde, Generic System V Application Binary Interface, gnu-...@sourceware.org
On Mon, Nov 20, 2017 at 7:27 AM, Suprateeka R Hegde
Typo. It should be PT_NOTE, instead of PT_LOAD.


--
H.J.
Reply all
Reply to author
Forward
0 new messages