/src/libbpf/elfutils/libelf/elf_getscn.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Get section at specific index. |
2 | | Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2015 Red Hat, Inc. |
3 | | This file is part of elfutils. |
4 | | Contributed by Ulrich Drepper <drepper@redhat.com>, 1998. |
5 | | |
6 | | This file is free software; you can redistribute it and/or modify |
7 | | it under the terms of either |
8 | | |
9 | | * the GNU Lesser General Public License as published by the Free |
10 | | Software Foundation; either version 3 of the License, or (at |
11 | | your option) any later version |
12 | | |
13 | | or |
14 | | |
15 | | * the GNU General Public License as published by the Free |
16 | | Software Foundation; either version 2 of the License, or (at |
17 | | your option) any later version |
18 | | |
19 | | or both in parallel, as here. |
20 | | |
21 | | elfutils is distributed in the hope that it will be useful, but |
22 | | WITHOUT ANY WARRANTY; without even the implied warranty of |
23 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
24 | | General Public License for more details. |
25 | | |
26 | | You should have received copies of the GNU General Public License and |
27 | | the GNU Lesser General Public License along with this program. If |
28 | | not, see <http://www.gnu.org/licenses/>. */ |
29 | | |
30 | | #ifdef HAVE_CONFIG_H |
31 | | # include <config.h> |
32 | | #endif |
33 | | |
34 | | #include <assert.h> |
35 | | #include <stddef.h> |
36 | | #include <stdlib.h> |
37 | | |
38 | | #include "libelfP.h" |
39 | | |
40 | | |
41 | | Elf_Scn * |
42 | | elf_getscn (Elf *elf, size_t idx) |
43 | 23.6k | { |
44 | 23.6k | if (elf == NULL) |
45 | 0 | return NULL; |
46 | | |
47 | 23.6k | if (unlikely (elf->kind != ELF_K_ELF)) |
48 | 0 | { |
49 | 0 | __libelf_seterrno (ELF_E_INVALID_HANDLE); |
50 | 0 | return NULL; |
51 | 0 | } |
52 | | |
53 | 23.6k | rwlock_rdlock (elf->lock); |
54 | | |
55 | 23.6k | Elf_Scn *result = NULL; |
56 | | |
57 | | /* Find the section in the list. */ |
58 | 23.6k | Elf_ScnList *runp = (elf->class == ELFCLASS32 |
59 | 23.6k | || (offsetof (struct Elf, state.elf32.scns) |
60 | 23.6k | == offsetof (struct Elf, state.elf64.scns)) |
61 | 23.6k | ? &elf->state.elf32.scns : &elf->state.elf64.scns); |
62 | | |
63 | | /* Section zero is special. It always exists even if there is no |
64 | | "first" section. And it is needed to store "overflow" values |
65 | | from the Elf header. */ |
66 | 23.6k | if (idx == 0 && runp->cnt == 0 && runp->max > 0) |
67 | 0 | { |
68 | 0 | Elf_Scn *scn0 = &runp->data[0]; |
69 | 0 | if (elf->class == ELFCLASS32) |
70 | 0 | { |
71 | 0 | scn0->shdr.e32 = calloc (1, sizeof (Elf32_Shdr)); |
72 | 0 | if (scn0->shdr.e32 == NULL) |
73 | 0 | { |
74 | 0 | __libelf_seterrno (ELF_E_NOMEM); |
75 | 0 | goto out; |
76 | 0 | } |
77 | 0 | } |
78 | 0 | else |
79 | 0 | { |
80 | 0 | scn0->shdr.e64 = calloc (1, sizeof (Elf64_Shdr)); |
81 | 0 | if (scn0->shdr.e64 == NULL) |
82 | 0 | { |
83 | 0 | __libelf_seterrno (ELF_E_NOMEM); |
84 | 0 | goto out; |
85 | 0 | } |
86 | 0 | } |
87 | 0 | scn0->elf = elf; |
88 | 0 | scn0->shdr_flags = ELF_F_DIRTY | ELF_F_MALLOCED; |
89 | 0 | scn0->list = elf->state.elf.scns_last; |
90 | 0 | scn0->data_read = 1; |
91 | 0 | runp->cnt = 1; |
92 | 0 | } |
93 | | |
94 | 23.6k | while (1) |
95 | 23.6k | { |
96 | 23.6k | if (idx < runp->max) |
97 | 23.2k | { |
98 | 23.2k | if (idx < runp->cnt) |
99 | 23.2k | result = &runp->data[idx]; |
100 | 0 | else |
101 | 0 | __libelf_seterrno (ELF_E_INVALID_INDEX); |
102 | 23.2k | break; |
103 | 23.2k | } |
104 | | |
105 | 461 | idx -= runp->max; |
106 | | |
107 | 461 | runp = runp->next; |
108 | 461 | if (runp == NULL) |
109 | 461 | { |
110 | 461 | __libelf_seterrno (ELF_E_INVALID_INDEX); |
111 | 461 | break; |
112 | 461 | } |
113 | 461 | } |
114 | | |
115 | 23.6k | out: |
116 | 23.6k | rwlock_unlock (elf->lock); |
117 | | |
118 | 23.6k | return result; |
119 | 23.6k | } |
120 | | INTDEF(elf_getscn) |