load_shdr_wrlock would sets the scn->shdr pointer for all the shnum
sections, where shnum is the total number of sections. But it indexed
state.scns.data[], which only holds the first ScnList block that
contains the sections read from the file. When load_shdr_wrlock is
called for the first time shnum equals the number of sections read
from the file. Except if elf_newscn has been called without ever
reading any Shdr. Then shnum could be larger. elf_newscn will create
an additional ScnList block that holds new sections and will have set
the shdr pointer already for these sections. So load_shdr_wrlock only
has to set the Shdrs for the sections in the first ScnList block.

        * libelf/elf32_getshdr.c (load_shdr_wrlock): Only set shdr for
        the sections in the first ScnList block (sections read from
        the file).

Signed-off-by: Mark Wielaard <[email protected]>
---
 libelf/elf32_getshdr.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/libelf/elf32_getshdr.c b/libelf/elf32_getshdr.c
index cb4bad11b884..cb88267ebbe8 100644
--- a/libelf/elf32_getshdr.c
+++ b/libelf/elf32_getshdr.c
@@ -193,10 +193,12 @@ load_shdr_wrlock (Elf_Scn *scn)
     }
 
   Elf_Scn *scns = elf->state.ELFW(elf,LIBELFBITS).scns.data;
+  size_t scns_cnt = elf->state.ELFW(elf,LIBELFBITS).scns.cnt;
 
   /* Set the pointers in the `scn's.  Set section 0 last to indicate that
-     all sections have been set.  */
-  for (size_t cnt = shnum; cnt > 0; --cnt)
+     all sections have been set.  Add a shdr pointer to all sections
+     read from the file.  */
+  for (size_t cnt = scns_cnt; cnt > 0; --cnt)
     atomic_store_release (&scns[cnt - 1].shdr.ELFW(e,LIBELFBITS),
                          &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt - 1]);
 
-- 
2.56.0

Reply via email to