Branch data Line data Source code
1 : : /* Get the section index of the extended section index table.
2 : : Copyright (C) 2007 Red Hat, Inc.
3 : : Copyright (C) 2025 Mark J. Wielaard <mark@klomp.org>
4 : : This file is part of elfutils.
5 : : Contributed by Ulrich Drepper <drepper@redhat.com>, 2007.
6 : :
7 : : This file is free software; you can redistribute it and/or modify
8 : : it under the terms of either
9 : :
10 : : * the GNU Lesser General Public License as published by the Free
11 : : Software Foundation; either version 3 of the License, or (at
12 : : your option) any later version
13 : :
14 : : or
15 : :
16 : : * the GNU General Public License as published by the Free
17 : : Software Foundation; either version 2 of the License, or (at
18 : : your option) any later version
19 : :
20 : : or both in parallel, as here.
21 : :
22 : : elfutils is distributed in the hope that it will be useful, but
23 : : WITHOUT ANY WARRANTY; without even the implied warranty of
24 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 : : General Public License for more details.
26 : :
27 : : You should have received copies of the GNU General Public License and
28 : : the GNU Lesser General Public License along with this program. If
29 : : not, see <http://www.gnu.org/licenses/>. */
30 : :
31 : : #ifdef HAVE_CONFIG_H
32 : : # include <config.h>
33 : : #endif
34 : :
35 : : #include "libelfP.h"
36 : :
37 : :
38 : : int
39 : 96 : elf_scnshndx (Elf_Scn *scn)
40 : : {
41 : 96 : size_t scnndx;
42 : 96 : GElf_Shdr shdr_mem;
43 : 96 : GElf_Shdr *shdr;
44 : 96 : Elf *elf;
45 : 96 : Elf_Scn *nscn;
46 : :
47 [ + - ]: 96 : if (scn == NULL)
48 : : return -1;
49 : :
50 : 96 : scnndx = scn->index;
51 : 96 : elf = scn->elf;
52 : :
53 : 96 : shdr = gelf_getshdr (scn, &shdr_mem);
54 [ + - ]: 96 : if (shdr == NULL)
55 : : return -1;
56 : :
57 : : /* Only SYMTAB sections can have a SHNDX section. */
58 [ + + ]: 96 : if (shdr->sh_type != SHT_SYMTAB)
59 : : return 0;
60 : :
61 : : /* By convention the SHT_SYMTAB_SHNDX section is right after the the
62 : : SHT_SYMTAB section, so start there. */
63 : : nscn = scn;
64 [ + + ]: 116 : while ((nscn = elf_nextscn (elf, nscn)) != NULL)
65 : : {
66 : 68 : shdr = gelf_getshdr (nscn, &shdr_mem);
67 [ + - ]: 68 : if (shdr == NULL)
68 : : return -1;
69 : :
70 [ + + + - ]: 68 : if (shdr->sh_type == SHT_SYMTAB_SHNDX && shdr->sh_link == scnndx)
71 : 20 : return nscn->index;
72 : : }
73 : :
74 : : /* OK, not found, start from the top. */
75 : : nscn = NULL;
76 : 3744 : while ((nscn = elf_nextscn (elf, nscn)) != NULL
77 [ - + + + ]: 1872 : && nscn->index != scnndx)
78 : : {
79 : 1824 : shdr = gelf_getshdr (nscn, &shdr_mem);
80 [ + - ]: 1824 : if (shdr == NULL)
81 : : return -1;
82 : :
83 [ - + - - ]: 1824 : if (shdr->sh_type == SHT_SYMTAB_SHNDX && shdr->sh_link == scnndx)
84 : 0 : return nscn->index;
85 : : }
86 : :
87 : : /* No shndx found, but no errors. */
88 : : return 0;
89 : : }
90 : : INTDEF(elf_scnshndx)
|