Coverage Report

Created: 2025-11-13 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/elfutils/libdwfl/segment.c
Line
Count
Source
1
/* Manage address space lookup table for libdwfl.
2
   Copyright (C) 2008, 2009, 2010, 2013, 2015 Red Hat, Inc.
3
   This file is part of elfutils.
4
5
   This file is free software; you can redistribute it and/or modify
6
   it under the terms of either
7
8
     * the GNU Lesser General Public License as published by the Free
9
       Software Foundation; either version 3 of the License, or (at
10
       your option) any later version
11
12
   or
13
14
     * the GNU General Public License as published by the Free
15
       Software Foundation; either version 2 of the License, or (at
16
       your option) any later version
17
18
   or both in parallel, as here.
19
20
   elfutils is distributed in the hope that it will be useful, but
21
   WITHOUT ANY WARRANTY; without even the implied warranty of
22
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23
   General Public License for more details.
24
25
   You should have received copies of the GNU General Public License and
26
   the GNU Lesser General Public License along with this program.  If
27
   not, see <http://www.gnu.org/licenses/>.  */
28
29
#ifdef HAVE_CONFIG_H
30
# include <config.h>
31
#endif
32
33
#include "libdwflP.h"
34
35
GElf_Addr
36
internal_function
37
__libdwfl_segment_start (Dwfl *dwfl, GElf_Addr start)
38
49.4k
{
39
49.4k
  if (dwfl->segment_align > 1)
40
46.8k
    start &= -dwfl->segment_align;
41
49.4k
  return start;
42
49.4k
}
43
44
GElf_Addr
45
internal_function
46
__libdwfl_segment_end (Dwfl *dwfl, GElf_Addr end)
47
49.4k
{
48
49.4k
  if (dwfl->segment_align > 1)
49
46.8k
    end = (end + dwfl->segment_align - 1) & -dwfl->segment_align;
50
49.4k
  return end;
51
49.4k
}
52
53
static bool
54
insert (Dwfl *dwfl, size_t i, GElf_Addr start, GElf_Addr end, int segndx)
55
49.4k
{
56
49.4k
  bool need_start = (i == 0 || dwfl->lookup_addr[i - 1] != start);
57
49.4k
  bool need_end = (i + 1 >= dwfl->lookup_elts
58
27.4k
       || dwfl->lookup_addr[i + 1] != end);
59
49.4k
  size_t need = need_start + need_end;
60
49.4k
  if (need == 0)
61
2.06k
    return false;
62
63
47.3k
  if (dwfl->lookup_alloc - dwfl->lookup_elts < need)
64
10.7k
    {
65
10.7k
      size_t n = dwfl->lookup_alloc == 0 ? 16 : dwfl->lookup_alloc * 2;
66
10.7k
      GElf_Addr *naddr = realloc (dwfl->lookup_addr, sizeof naddr[0] * n);
67
10.7k
      if (unlikely (naddr == NULL))
68
0
  return true;
69
10.7k
      int *nsegndx = realloc (dwfl->lookup_segndx, sizeof nsegndx[0] * n);
70
10.7k
      if (unlikely (nsegndx == NULL))
71
0
  {
72
0
    if (naddr != dwfl->lookup_addr)
73
0
      free (naddr);
74
0
    return true;
75
0
  }
76
10.7k
      dwfl->lookup_alloc = n;
77
10.7k
      dwfl->lookup_addr = naddr;
78
10.7k
      dwfl->lookup_segndx = nsegndx;
79
80
10.7k
      if (dwfl->lookup_module != NULL)
81
0
  {
82
    /* Make sure this array is big enough too.  */
83
0
    Dwfl_Module **old = dwfl->lookup_module;
84
0
    dwfl->lookup_module = realloc (dwfl->lookup_module,
85
0
           sizeof dwfl->lookup_module[0] * n);
86
0
    if (unlikely (dwfl->lookup_module == NULL))
87
0
      {
88
0
        free (old);
89
0
        return true;
90
0
      }
91
0
  }
92
10.7k
    }
93
94
47.3k
  if (unlikely (i < dwfl->lookup_elts))
95
31.3k
    {
96
31.3k
      const size_t move = dwfl->lookup_elts - i;
97
31.3k
      memmove (&dwfl->lookup_addr[i + need], &dwfl->lookup_addr[i],
98
31.3k
         move * sizeof dwfl->lookup_addr[0]);
99
31.3k
      memmove (&dwfl->lookup_segndx[i + need], &dwfl->lookup_segndx[i],
100
31.3k
         move * sizeof dwfl->lookup_segndx[0]);
101
31.3k
      if (dwfl->lookup_module != NULL)
102
0
  memmove (&dwfl->lookup_module[i + need], &dwfl->lookup_module[i],
103
0
     move * sizeof dwfl->lookup_module[0]);
104
31.3k
    }
105
106
47.3k
  if (need_start)
107
41.4k
    {
108
41.4k
      dwfl->lookup_addr[i] = start;
109
41.4k
      dwfl->lookup_segndx[i] = segndx;
110
41.4k
      if (dwfl->lookup_module != NULL)
111
0
  dwfl->lookup_module[i] = NULL;
112
41.4k
      ++i;
113
41.4k
    }
114
5.94k
  else
115
5.94k
    dwfl->lookup_segndx[i - 1] = segndx;
116
117
47.3k
  if (need_end)
118
47.1k
    {
119
47.1k
      dwfl->lookup_addr[i] = end;
120
47.1k
      dwfl->lookup_segndx[i] = -1;
121
47.1k
      if (dwfl->lookup_module != NULL)
122
0
  dwfl->lookup_module[i] = NULL;
123
47.1k
    }
124
125
47.3k
  dwfl->lookup_elts += need;
126
127
47.3k
  return false;
128
47.3k
}
129
130
static int
131
lookup (Dwfl *dwfl, GElf_Addr address, int hint)
132
37.2k
{
133
37.2k
  if (hint >= 0
134
0
      && address >= dwfl->lookup_addr[hint]
135
0
      && ((size_t) hint + 1 == dwfl->lookup_elts
136
0
    || address < dwfl->lookup_addr[hint + 1]))
137
0
    return hint;
138
139
  /* Do binary search on the array indexed by module load address.  */
140
37.2k
  size_t l = 0, u = dwfl->lookup_elts;
141
164k
  while (l < u)
142
163k
    {
143
163k
      size_t idx = (l + u) / 2;
144
163k
      if (address < dwfl->lookup_addr[idx])
145
105k
  u = idx;
146
58.8k
      else
147
58.8k
  {
148
58.8k
    l = idx + 1;
149
58.8k
    if (l == dwfl->lookup_elts || address < dwfl->lookup_addr[l])
150
36.7k
      return idx;
151
58.8k
  }
152
163k
    }
153
154
502
  return -1;
155
37.2k
}
156
157
static bool
158
reify_segments (Dwfl *dwfl)
159
5.28k
{
160
5.28k
  int hint = -1;
161
5.28k
  int highest = -1;
162
5.28k
  bool fixup = false;
163
5.28k
  for (Dwfl_Module *mod = dwfl->modulelist; mod != NULL; mod = mod->next)
164
0
    if (! mod->gc)
165
0
      {
166
0
  const GElf_Addr start = __libdwfl_segment_start (dwfl, mod->low_addr);
167
0
  const GElf_Addr end = __libdwfl_segment_end (dwfl, mod->high_addr);
168
0
  bool resized = false;
169
170
0
  int idx = lookup (dwfl, start, hint);
171
0
  if (unlikely (idx < 0))
172
0
    {
173
      /* Module starts below any segment.  Insert a low one.  */
174
0
      if (unlikely (insert (dwfl, 0, start, end, -1)))
175
0
        return true;
176
0
      idx = 0;
177
0
      resized = true;
178
0
    }
179
0
  else if (dwfl->lookup_addr[idx] > start)
180
0
    {
181
      /* The module starts in the middle of this segment.  Split it.  */
182
0
      if (unlikely (insert (dwfl, idx + 1, start, end,
183
0
          dwfl->lookup_segndx[idx])))
184
0
        return true;
185
0
      ++idx;
186
0
      resized = true;
187
0
    }
188
0
  else if (dwfl->lookup_addr[idx] < start)
189
0
    {
190
      /* The module starts past the end of this segment.
191
         Add a new one.  */
192
0
      if (unlikely (insert (dwfl, idx + 1, start, end, -1)))
193
0
        return true;
194
0
      ++idx;
195
0
      resized = true;
196
0
    }
197
198
0
  if ((size_t) idx + 1 < dwfl->lookup_elts
199
0
      && end < dwfl->lookup_addr[idx + 1])
200
0
    {
201
      /* The module ends in the middle of this segment.  Split it.  */
202
0
      if (unlikely (insert (dwfl, idx + 1,
203
0
          end, dwfl->lookup_addr[idx + 1], -1)))
204
0
        return true;
205
0
      resized = true;
206
0
    }
207
208
0
  if (dwfl->lookup_module == NULL)
209
0
    {
210
0
      dwfl->lookup_module = calloc (dwfl->lookup_alloc,
211
0
            sizeof dwfl->lookup_module[0]);
212
0
      if (unlikely (dwfl->lookup_module == NULL))
213
0
        return true;
214
0
    }
215
216
  /* Cache a backpointer in the module.  */
217
0
  mod->segment = idx;
218
219
  /* Put MOD in the table for each segment that's inside it.  */
220
0
  do
221
0
    dwfl->lookup_module[idx++] = mod;
222
0
  while ((size_t) idx < dwfl->lookup_elts
223
0
         && dwfl->lookup_addr[idx] < end);
224
0
  assert (dwfl->lookup_module[mod->segment] == mod);
225
226
0
  if (resized && idx - 1 >= highest)
227
    /* Expanding the lookup tables invalidated backpointers
228
       we've already stored.  Reset those ones.  */
229
0
    fixup = true;
230
231
0
  highest = idx - 1;
232
0
  hint = (size_t) idx < dwfl->lookup_elts ? idx : -1;
233
0
      }
234
235
5.28k
  if (fixup)
236
    /* Reset backpointer indices invalidated by table insertions.  */
237
0
    for (size_t idx = 0; idx < dwfl->lookup_elts; ++idx)
238
0
      if (dwfl->lookup_module[idx] != NULL)
239
0
  dwfl->lookup_module[idx]->segment = idx;
240
241
5.28k
  return false;
242
5.28k
}
243
244
int
245
dwfl_addrsegment (Dwfl *dwfl, Dwarf_Addr address, Dwfl_Module **mod)
246
37.2k
{
247
37.2k
  if (unlikely (dwfl == NULL))
248
0
    return -1;
249
250
37.2k
  if (unlikely (dwfl->lookup_module == NULL)
251
37.2k
      && mod != NULL
252
5.28k
      && unlikely (reify_segments (dwfl)))
253
0
    {
254
0
      __libdwfl_seterrno (DWFL_E_NOMEM);
255
0
      return -1;
256
0
    }
257
258
37.2k
  int idx = lookup (dwfl, address, -1);
259
37.2k
  if (likely (mod != NULL))
260
5.28k
    {
261
5.28k
      if (unlikely (idx < 0) || unlikely (dwfl->lookup_module == NULL))
262
5.28k
  *mod = NULL;
263
0
      else
264
0
  {
265
0
    *mod = dwfl->lookup_module[idx];
266
267
    /* If this segment does not have a module, but the address is
268
       the upper boundary of the previous segment's module, use that.  */
269
0
    if (*mod == NULL && idx > 0 && dwfl->lookup_addr[idx] == address)
270
0
      {
271
0
        *mod = dwfl->lookup_module[idx - 1];
272
0
        if (*mod != NULL && (*mod)->high_addr != address)
273
0
    *mod = NULL;
274
0
      }
275
0
  }
276
5.28k
    }
277
278
37.2k
  if (likely (idx >= 0))
279
    /* Translate internal segment table index to user segment index.  */
280
36.7k
    idx = dwfl->lookup_segndx[idx];
281
282
37.2k
  return idx;
283
37.2k
}
284
INTDEF (dwfl_addrsegment)
285
286
int
287
dwfl_report_segment (Dwfl *dwfl, int ndx, const GElf_Phdr *phdr, GElf_Addr bias,
288
         const void *ident)
289
49.4k
{
290
  /* This was previously used for coalescing segments, but it was buggy since
291
     day one.  We don't use it anymore.  */
292
49.4k
  (void)ident;
293
294
49.4k
  if (dwfl == NULL)
295
0
    return -1;
296
297
49.4k
  if (ndx < 0)
298
0
    ndx = dwfl->next_segndx;
299
300
49.4k
  if (phdr->p_align > 1 && (dwfl->segment_align <= 1 ||
301
35.3k
          phdr->p_align < dwfl->segment_align))
302
10.8k
    dwfl->segment_align = phdr->p_align;
303
304
49.4k
  if (unlikely (dwfl->lookup_module != NULL))
305
0
    {
306
0
      free (dwfl->lookup_module);
307
0
      dwfl->lookup_module = NULL;
308
0
    }
309
310
49.4k
  GElf_Addr start = __libdwfl_segment_start (dwfl, bias + phdr->p_vaddr);
311
49.4k
  GElf_Addr end = __libdwfl_segment_end (dwfl,
312
49.4k
           bias + phdr->p_vaddr + phdr->p_memsz);
313
314
  /* Normally just appending keeps us sorted.  */
315
316
49.4k
  size_t i = dwfl->lookup_elts;
317
1.24M
  while (i > 0 && unlikely (start < dwfl->lookup_addr[i - 1]))
318
1.19M
    --i;
319
320
49.4k
  if (unlikely (insert (dwfl, i, start, end, ndx)))
321
0
    {
322
0
      __libdwfl_seterrno (DWFL_E_NOMEM);
323
0
      return -1;
324
0
    }
325
326
49.4k
  dwfl->next_segndx = ndx + 1;
327
328
49.4k
  return ndx;
329
49.4k
}
330
INTDEF (dwfl_report_segment)