This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Generating an ELF file with PT_NOTE before PT_LOAD


I need to read version information from a PT_NOTE segment in a
compressed ELF file. I'd like to be able to do this without
decompressing the entire file. Although I can ensure that the
PT_NOTE segment occurs as the first entry in the program header,
I'm unable to persuade GNU ld to place the note itself first in
the file. (The resulting ELF file is loaded by a bootloader which
doesn't care about the order of the segments.)

I've tested with binutils 2.28 (from Debian Stretch), 2.30 (from Debian
Buster) and the current state of master. All appeared to give the same
result.

Here's a shell script that puts a simulated binary in a PT_LOAD
segment and a note in a PT_NOTE segment to reproduce the problem:

--8<--
#!/bin/sh
set -e
dd if=/dev/zero of=binary bs=1M count=1
objcopy -I binary -O elf32-i386 -B i386 binary text.o

gcc -c -m32 -x assembler - -o note.o <<EOF
    .section ".note.my-version", ""
    .p2align 2
    .long 1f-0f
    .long 3f-2f
    .long 1
0:  .asciz "my-version"
1:  .p2align 2
2:  .asciz "1.2.3.4"
3:  .p2align 2
EOF

cat > test.lds <<EOF
SECTIONS
{
    .note : {
        *(.note.my-version)
    } :note
    .text : {
        *(.data)
    } :load
    _end = .;
}
PHDRS
{
  note PT_NOTE;
  load PT_LOAD;
}
EOF

ld.bfd -n -m elf_i386 -o out.elf -s -T test.lds note.o text.o
readelf -l out.elf
-->8--

The output is:

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  NOTE           0x100074 0x00000000 0x00000000 0x00020 0x00000 R   0x4
  LOAD           0x000074 0x00000000 0x00000000 0x100000 0x100000 R E 0x1

 Section to Segment mapping:
  Segment Sections...
   00     .note
   01     .text

It is clear that the NOTE segment starts at 0x100074 which is
after the LOAD segment at 0x000074. How do I ensure that the NOTE
segment appears at the start of the file so that I don't need to
decompress the entire file to read it?

(Using gold with the same script results in a binary that contains an empty
NOTE segment.)

Thanks.

Mike.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]