Re: elf.c assign_file_positions_for_segments

Mark Kettenis
2004-09-23T15:51:30Z

Hi Alan,

The following patch:

2004-09-22 Alan Modra <amodra@bigpond.net.au>

* elf.c (IS_LOADED): Define.
(assign_file_positions_for_segments): Don't round up file offset of
PT_LOAD segments containing no SEC_LOAD sections, instead round down.
Delete code handling link script adjustment of lma. Do the adjust
in later code handling similar ajustments. Remove dead code error
check. Warn if section lma would require a negative offset
adjustment. Tweak lma adjustment to use p_filesz rather than p_memsz.
Use p_vaddr + p_memsz inside section loop in place of voff. Don't
update voff in section loop. Change voff in segment loop to be an
adjustment on top of "off". Set sec->filepos and update "off" later.
Test for loadable sections consistently using IS_LOADED. Similarly,
test for alloc-only sections other than .tbss consistently.
Don't bother checking SEC_ALLOC in PT_LOAD segments. Remove FIXME.
Tidy PT_NOTE handling. Use %B and %A in error messages.
(assign_file_positions_except_relocs): Use %B in error message.

breaks the GDB gcore command on sparc-sun-solaris2.9, and probably on
other systems too. Here's the output of readelf -l without:

---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---

Elf file type is CORE (Core file)
Entry point 0x0
There are 15 program headers, starting at offset 52

Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
NOTE 0x000214 0x00000000 0x00000000 0x00578 0x00000 R 0x1
LOAD 0x00078c 0x00010000 0x00000000 0x00000 0x02000 R E 0x1
LOAD 0x00278c 0x00020000 0x00000000 0x02000 0x02000 RWE 0x1
LOAD 0x00478c 0x00022000 0x00000000 0x02000 0x02000 RWE 0x1
LOAD 0x00678c 0xff280000 0x00000000 0x00000 0xaa000 R E 0x1
LOAD 0x0b078c 0xff33a000 0x00000000 0x06000 0x06000 RWE 0x1
LOAD 0x0b678c 0xff340000 0x00000000 0x02000 0x02000 RWE 0x1
LOAD 0x0b878c 0xff350000 0x00000000 0x00000 0x04000 R E 0x1
LOAD 0x0bc78c 0xff370000 0x00000000 0x00000 0x18000 R E 0x1
LOAD 0x0d478c 0xff396000 0x00000000 0x02000 0x02000 RWE 0x1
LOAD 0x0d678c 0xff3a0000 0x00000000 0x00000 0x02000 R E 0x1
LOAD 0x0d878c 0xff3b0000 0x00000000 0x02000 0x02000 RWE 0x1
LOAD 0x0da78c 0xff3c0000 0x00000000 0x26000 0x26000 R E 0x1
LOAD 0x10078c 0xff3f6000 0x00000000 0x02000 0x02000 RWE 0x1
LOAD 0x10278c 0xffbfe000 0x00000000 0x02000 0x02000 RWE 0x1

---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---

and with your patch:

---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---

Elf file type is CORE (Core file)
Entry point 0x0
There are 15 program headers, starting at offset 52

Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
NOTE 0x000214 0x00000000 0x00000000 0x00578 0x00000 R 0x1
LOAD 0x00078c 0x00010000 0x00000000 0x02000 0x02000 R E 0x1
LOAD 0x00278c 0x00020000 0x00000000 0x02000 0x02000 RWE 0x1
LOAD 0x00478c 0x00022000 0x00000000 0x02000 0x02000 RWE 0x1
LOAD 0x00678c 0xff280000 0x00000000 0xaa000 0xaa000 R E 0x1
LOAD 0x0b078c 0xff33a000 0x00000000 0x06000 0x06000 RWE 0x1
LOAD 0x0b678c 0xff340000 0x00000000 0x02000 0x02000 RWE 0x1
LOAD 0x0b878c 0xff350000 0x00000000 0x04000 0x04000 R E 0x1
LOAD 0x0bc78c 0xff370000 0x00000000 0x18000 0x18000 R E 0x1
LOAD 0x0d478c 0xff396000 0x00000000 0x02000 0x02000 RWE 0x1
LOAD 0x0d678c 0xff3a0000 0x00000000 0x02000 0x02000 R E 0x1
LOAD 0x0d878c 0xff3b0000 0x00000000 0x02000 0x02000 RWE 0x1
LOAD 0x0da78c 0xff3c0000 0x00000000 0x26000 0x26000 R E 0x1
LOAD 0x10078c 0xff3f6000 0x00000000 0x02000 0x02000 RWE 0x1
LOAD 0x10278c 0xffbfe000 0x00000000 0x02000 0x02000 RWE 0x1

---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---

Look at the first LOAD header. Before it had a FileSiz of 0,
afterwards it's 0x02000. Note that this is a read-only section. The
contents of the section in the core file is all zeroes. Presumably
the fact that its FileSiz is 0 causes GDB to read the contents from
the executable instead. However, with FileSiz 0x02000, the contents
get overwritten with all zeroes. GDB's disas command shows rather
dull output in that case.

I'm not sure where to fix this. What we'd like to avoid in GDB is
actually read the contents of read-only sections from memory when
we're synthesizing the core file.

Mark