This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Re: [PATCH] elf: Try not pointing empty PT_LOAD segment's offset past EOF
Hi Alan,
Alan Modra <amodra@gmail.com> ezt írta (időpont: 2019. dec. 8., V, 3:12):
>
> On Sat, Dec 07, 2019 at 05:22:13PM +0100, Bálint Réczey wrote:
> > diff --git a/bfd/elf.c b/bfd/elf.c
> > index 1aa2603ee8..e1a9a02eec 100644
> > --- a/bfd/elf.c
> > +++ b/bfd/elf.c
> > @@ -5752,7 +5752,12 @@ assign_file_positions_for_load_sections (bfd *abfd,
> > || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
> > {
> > if (!m->includes_filehdr && !m->includes_phdrs)
> > - p->p_offset = off;
> > + if (no_contents)
> > + /* Try avoiding pointing past the EOF with this empty segment's
> > + p_offset. */
> > + p->p_offset = p->p_offset % maxpagesize;
> > + else
> > + p->p_offset = off;
> > else
> > {
> > file_ptr adjust;
>
> How did you test this patch? I suspect you are just leaving p_offset
> at zero and therefore will cause failures on glibc systems.
I've recompiled gzip in a PPA [1] with the modified binutils and also
a few other packages since then and they seem to work.
Observing the generalted binary I indeed found that the offset is
zero, but it did not cause any problem and the first PT_LOAD's file
offset is also zero.
New gzip:
$ readelf --program-headers /bin/gzip
...
Program Headers:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags Align
PHDR 0x0000000000000040 0x0000000000000040 0x0000000000000040
0x0000000000000310 0x0000000000000310 R 0x8
INTERP 0x0000000000000350 0x0000000000000350 0x0000000000000350
0x000000000000001c 0x000000000000001c R 0x1
[Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
LOAD 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x00000000000027a0 0x00000000000027a0 R 0x1000
LOAD 0x0000000000003000 0x0000000000003000 0x0000000000003000
0x000000000000ea05 0x000000000000ea05 R E 0x1000
LOAD 0x0000000000012000 0x0000000000012000 0x0000000000012000
0x0000000000003e80 0x0000000000003e80 R 0x1000
LOAD 0x0000000000016690 0x0000000000017690 0x0000000000017690
0x0000000000000d70 0x0000000000000d70 RW 0x1000
LOAD 0x0000000000000000 0x000000000001a000 0x000000000001a000
0x0000000000000000 0x00000000000ca048 RW 0x1000
DYNAMIC 0x0000000000016b80 0x0000000000017b80 0x0000000000017b80
0x00000000000001f0 0x00000000000001f0 RW 0x8
...
The difference between the original gzip and the one built with the
patched binutils:
$ diff -Naur orig.elfdump new.elfdump
--- orig.elfdump 2019-12-08 11:59:57.544897124 +0000
+++ new.elfdump 2019-12-08 12:22:37.930722784 +0000
@@ -14,12 +14,12 @@
LOAD 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x00000000000027a0 0x00000000000027a0 R 0x1000
LOAD 0x0000000000003000 0x0000000000003000 0x0000000000003000
- 0x000000000000ea15 0x000000000000ea15 R E 0x1000
+ 0x000000000000ea05 0x000000000000ea05 R E 0x1000
LOAD 0x0000000000012000 0x0000000000012000 0x0000000000012000
0x0000000000003e80 0x0000000000003e80 R 0x1000
LOAD 0x0000000000016690 0x0000000000017690 0x0000000000017690
0x0000000000000d70 0x0000000000000d70 RW 0x1000
- LOAD 0x0000000000018000 0x000000000001a000 0x000000000001a000
+ LOAD 0x0000000000000000 0x000000000001a000 0x000000000001a000
0x0000000000000000 0x00000000000ca048 RW 0x1000
DYNAMIC 0x0000000000016b80 0x0000000000017b80 0x0000000000017b80
0x00000000000001f0 0x00000000000001f0 RW 0x8
$
Cheers,
Balint
[1] https://launchpad.net/~rbalint/+archive/ubuntu/scratch/+packages