Coverage Report

Created: 2025-11-09 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/elfutils/libelf/elf32_getphdr.c
Line
Count
Source
1
/* Get ELF program header table.
2
   Copyright (C) 1998-2010, 2014, 2015 Red Hat, Inc.
3
   This file is part of elfutils.
4
   Written 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 <errno.h>
35
#include <stdbool.h>
36
#include <stdlib.h>
37
#include <assert.h>
38
39
#include "libelfP.h"
40
#include "common.h"
41
42
#ifndef LIBELFBITS
43
# define LIBELFBITS 32
44
#endif
45
46
ElfW2(LIBELFBITS,Phdr) *
47
__elfw2(LIBELFBITS,getphdr_wrlock) (Elf *elf)
48
18.8k
{
49
18.8k
  ElfW2(LIBELFBITS,Phdr) *result;
50
51
  /* If the program header entry has already been filled in the code
52
     below must already have been run.  So the class is set, too.  No
53
     need to waste any more time here.  */
54
18.8k
  result = elf->state.ELFW(elf,LIBELFBITS).phdr;
55
18.8k
  if (likely (result != NULL))
56
0
    return result;
57
58
18.8k
  if (elf->class == 0)
59
0
    elf->class = ELFW(ELFCLASS,LIBELFBITS);
60
18.8k
  else if (elf->class != ELFW(ELFCLASS,LIBELFBITS))
61
0
    {
62
0
      __libelf_seterrno (ELF_E_INVALID_CLASS);
63
0
      result = NULL;
64
0
      goto out;
65
0
    }
66
67
18.8k
  if (likely (result == NULL))
68
18.8k
    {
69
      /* Read the section header table.  */
70
18.8k
      ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
71
72
      /* If no program header exists return NULL.  */
73
18.8k
      size_t phnum;
74
18.8k
      if (__elf_getphdrnum_rdlock (elf, &phnum) != 0)
75
0
  goto out;
76
18.8k
      if (phnum == 0 || ehdr->e_phoff == 0)
77
0
  {
78
0
    __libelf_seterrno (ELF_E_NO_PHDR);
79
0
    goto out;
80
0
  }
81
82
      /* Check this doesn't overflow.  */
83
18.8k
      size_t size = phnum * sizeof (ElfW2(LIBELFBITS,Phdr));
84
85
18.8k
      if (phnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Phdr))
86
18.8k
    || ehdr->e_phoff > elf->maximum_size
87
18.8k
    || elf->maximum_size - ehdr->e_phoff < size)
88
135
  {
89
135
    __libelf_seterrno (ELF_E_INVALID_DATA);
90
135
    goto out;
91
135
  }
92
93
18.7k
      if (elf->map_address != NULL)
94
18.7k
  {
95
    /* First see whether the information in the ELF header is
96
       valid and it does not ask for too much.  */
97
18.7k
    if (unlikely (ehdr->e_phoff >= elf->maximum_size)
98
18.7k
        || unlikely (elf->maximum_size - ehdr->e_phoff < size))
99
0
      {
100
        /* Something is wrong.  */
101
0
        __libelf_seterrno (ELF_E_INVALID_PHDR);
102
0
        goto out;
103
0
      }
104
105
    /* All the data is already mapped.  Use it.  */
106
18.7k
    void *file_phdr = ((char *) elf->map_address
107
18.7k
           + elf->start_offset + ehdr->e_phoff);
108
18.7k
    if (ehdr->e_ident[EI_DATA] == MY_ELFDATA
109
9.49k
        && (ALLOW_UNALIGNED
110
0
      || ((uintptr_t) file_phdr
111
0
          & (__alignof__ (ElfW2(LIBELFBITS,Phdr)) - 1)) == 0))
112
      /* Simply use the mapped data.  */
113
9.49k
      elf->state.ELFW(elf,LIBELFBITS).phdr = file_phdr;
114
9.21k
    else
115
9.21k
      {
116
9.21k
        ElfW2(LIBELFBITS,Phdr) *notcvt;
117
9.21k
        ElfW2(LIBELFBITS,Phdr) *phdr;
118
119
        /* Allocate memory for the program headers.  We know the number
120
     of entries from the ELF header.  */
121
9.21k
        phdr = elf->state.ELFW(elf,LIBELFBITS).phdr =
122
9.21k
    (ElfW2(LIBELFBITS,Phdr) *) malloc (size);
123
9.21k
        if (elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
124
0
    {
125
0
      __libelf_seterrno (ELF_E_NOMEM);
126
0
      goto out;
127
0
    }
128
9.21k
        elf->state.ELFW(elf,LIBELFBITS).phdr_flags |=
129
9.21k
    ELF_F_MALLOCED | ELF_F_DIRTY;
130
131
        /* Now copy the data and at the same time convert the
132
     byte order.  */
133
134
9.21k
        if (ehdr->e_ident[EI_DATA] == MY_ELFDATA)
135
0
    {
136
0
      assert (! ALLOW_UNALIGNED);
137
0
      memcpy (phdr, file_phdr, size);
138
0
    }
139
9.21k
        else
140
9.21k
    {
141
9.21k
      bool copy = ! (ALLOW_UNALIGNED
142
0
         || ((uintptr_t) file_phdr
143
0
             & (__alignof__ (ElfW2(LIBELFBITS,Phdr))
144
0
          - 1)) == 0);
145
9.21k
      if (! copy)
146
9.21k
        notcvt = file_phdr;
147
0
      else
148
0
        {
149
0
          notcvt = (ElfW2(LIBELFBITS,Phdr) *) malloc (size);
150
0
          if (unlikely (notcvt == NULL))
151
0
      {
152
0
        __libelf_seterrno (ELF_E_NOMEM);
153
0
        goto out;
154
0
      }
155
0
          memcpy (notcvt, file_phdr, size);
156
0
        }
157
158
306k
      for (size_t cnt = 0; cnt < phnum; ++cnt)
159
296k
        {
160
296k
          CONVERT_TO (phdr[cnt].p_type, notcvt[cnt].p_type);
161
296k
          CONVERT_TO (phdr[cnt].p_offset, notcvt[cnt].p_offset);
162
296k
          CONVERT_TO (phdr[cnt].p_vaddr, notcvt[cnt].p_vaddr);
163
296k
          CONVERT_TO (phdr[cnt].p_paddr, notcvt[cnt].p_paddr);
164
296k
          CONVERT_TO (phdr[cnt].p_filesz, notcvt[cnt].p_filesz);
165
296k
          CONVERT_TO (phdr[cnt].p_memsz, notcvt[cnt].p_memsz);
166
296k
          CONVERT_TO (phdr[cnt].p_flags, notcvt[cnt].p_flags);
167
296k
          CONVERT_TO (phdr[cnt].p_align, notcvt[cnt].p_align);
168
296k
        }
169
170
9.21k
      if (copy)
171
0
        free (notcvt);
172
9.21k
    }
173
9.21k
      }
174
18.7k
  }
175
0
      else if (likely (elf->fildes != -1))
176
0
  {
177
    /* Allocate memory for the program headers.  We know the number
178
       of entries from the ELF header.  */
179
0
    elf->state.ELFW(elf,LIBELFBITS).phdr =
180
0
      (ElfW2(LIBELFBITS,Phdr) *) malloc (size);
181
0
    if (elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
182
0
      {
183
0
        __libelf_seterrno (ELF_E_NOMEM);
184
0
        goto out;
185
0
      }
186
0
    elf->state.ELFW(elf,LIBELFBITS).phdr_flags |= ELF_F_MALLOCED;
187
188
    /* Read the header.  */
189
0
    ssize_t n = pread_retry (elf->fildes,
190
0
           elf->state.ELFW(elf,LIBELFBITS).phdr, size,
191
0
           elf->start_offset + ehdr->e_phoff);
192
0
    if (unlikely ((size_t) n != size))
193
0
      {
194
        /* Severe problems.  We cannot read the data.  */
195
0
        __libelf_seterrno (ELF_E_READ_ERROR);
196
0
        free (elf->state.ELFW(elf,LIBELFBITS).phdr);
197
0
        elf->state.ELFW(elf,LIBELFBITS).phdr = NULL;
198
0
        goto out;
199
0
      }
200
201
    /* If the byte order of the file is not the same as the one
202
       of the host convert the data now.  */
203
0
    if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
204
0
      {
205
0
        ElfW2(LIBELFBITS,Phdr) *phdr
206
0
    = elf->state.ELFW(elf,LIBELFBITS).phdr;
207
208
0
        for (size_t cnt = 0; cnt < phnum; ++cnt)
209
0
    {
210
0
      CONVERT (phdr[cnt].p_type);
211
0
      CONVERT (phdr[cnt].p_offset);
212
0
      CONVERT (phdr[cnt].p_vaddr);
213
0
      CONVERT (phdr[cnt].p_paddr);
214
0
      CONVERT (phdr[cnt].p_filesz);
215
0
      CONVERT (phdr[cnt].p_memsz);
216
0
      CONVERT (phdr[cnt].p_flags);
217
0
      CONVERT (phdr[cnt].p_align);
218
0
    }
219
0
      }
220
0
  }
221
0
      else
222
0
  {
223
    /* The file descriptor was already enabled and not all data was
224
       read.  */
225
0
    __libelf_seterrno (ELF_E_FD_DISABLED);
226
0
    goto out;
227
0
  }
228
229
18.7k
      result = elf->state.ELFW(elf,LIBELFBITS).phdr;
230
18.7k
    }
231
232
18.8k
 out:
233
18.8k
  return result;
234
18.8k
}
__elf32_getphdr_wrlock
Line
Count
Source
48
11.3k
{
49
11.3k
  ElfW2(LIBELFBITS,Phdr) *result;
50
51
  /* If the program header entry has already been filled in the code
52
     below must already have been run.  So the class is set, too.  No
53
     need to waste any more time here.  */
54
11.3k
  result = elf->state.ELFW(elf,LIBELFBITS).phdr;
55
11.3k
  if (likely (result != NULL))
56
0
    return result;
57
58
11.3k
  if (elf->class == 0)
59
0
    elf->class = ELFW(ELFCLASS,LIBELFBITS);
60
11.3k
  else if (elf->class != ELFW(ELFCLASS,LIBELFBITS))
61
0
    {
62
0
      __libelf_seterrno (ELF_E_INVALID_CLASS);
63
0
      result = NULL;
64
0
      goto out;
65
0
    }
66
67
11.3k
  if (likely (result == NULL))
68
11.3k
    {
69
      /* Read the section header table.  */
70
11.3k
      ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
71
72
      /* If no program header exists return NULL.  */
73
11.3k
      size_t phnum;
74
11.3k
      if (__elf_getphdrnum_rdlock (elf, &phnum) != 0)
75
0
  goto out;
76
11.3k
      if (phnum == 0 || ehdr->e_phoff == 0)
77
0
  {
78
0
    __libelf_seterrno (ELF_E_NO_PHDR);
79
0
    goto out;
80
0
  }
81
82
      /* Check this doesn't overflow.  */
83
11.3k
      size_t size = phnum * sizeof (ElfW2(LIBELFBITS,Phdr));
84
85
11.3k
      if (phnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Phdr))
86
11.3k
    || ehdr->e_phoff > elf->maximum_size
87
11.3k
    || elf->maximum_size - ehdr->e_phoff < size)
88
63
  {
89
63
    __libelf_seterrno (ELF_E_INVALID_DATA);
90
63
    goto out;
91
63
  }
92
93
11.2k
      if (elf->map_address != NULL)
94
11.2k
  {
95
    /* First see whether the information in the ELF header is
96
       valid and it does not ask for too much.  */
97
11.2k
    if (unlikely (ehdr->e_phoff >= elf->maximum_size)
98
11.2k
        || unlikely (elf->maximum_size - ehdr->e_phoff < size))
99
0
      {
100
        /* Something is wrong.  */
101
0
        __libelf_seterrno (ELF_E_INVALID_PHDR);
102
0
        goto out;
103
0
      }
104
105
    /* All the data is already mapped.  Use it.  */
106
11.2k
    void *file_phdr = ((char *) elf->map_address
107
11.2k
           + elf->start_offset + ehdr->e_phoff);
108
11.2k
    if (ehdr->e_ident[EI_DATA] == MY_ELFDATA
109
2.51k
        && (ALLOW_UNALIGNED
110
0
      || ((uintptr_t) file_phdr
111
0
          & (__alignof__ (ElfW2(LIBELFBITS,Phdr)) - 1)) == 0))
112
      /* Simply use the mapped data.  */
113
2.51k
      elf->state.ELFW(elf,LIBELFBITS).phdr = file_phdr;
114
8.73k
    else
115
8.73k
      {
116
8.73k
        ElfW2(LIBELFBITS,Phdr) *notcvt;
117
8.73k
        ElfW2(LIBELFBITS,Phdr) *phdr;
118
119
        /* Allocate memory for the program headers.  We know the number
120
     of entries from the ELF header.  */
121
8.73k
        phdr = elf->state.ELFW(elf,LIBELFBITS).phdr =
122
8.73k
    (ElfW2(LIBELFBITS,Phdr) *) malloc (size);
123
8.73k
        if (elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
124
0
    {
125
0
      __libelf_seterrno (ELF_E_NOMEM);
126
0
      goto out;
127
0
    }
128
8.73k
        elf->state.ELFW(elf,LIBELFBITS).phdr_flags |=
129
8.73k
    ELF_F_MALLOCED | ELF_F_DIRTY;
130
131
        /* Now copy the data and at the same time convert the
132
     byte order.  */
133
134
8.73k
        if (ehdr->e_ident[EI_DATA] == MY_ELFDATA)
135
0
    {
136
0
      assert (! ALLOW_UNALIGNED);
137
0
      memcpy (phdr, file_phdr, size);
138
0
    }
139
8.73k
        else
140
8.73k
    {
141
8.73k
      bool copy = ! (ALLOW_UNALIGNED
142
0
         || ((uintptr_t) file_phdr
143
0
             & (__alignof__ (ElfW2(LIBELFBITS,Phdr))
144
0
          - 1)) == 0);
145
8.73k
      if (! copy)
146
8.73k
        notcvt = file_phdr;
147
0
      else
148
0
        {
149
0
          notcvt = (ElfW2(LIBELFBITS,Phdr) *) malloc (size);
150
0
          if (unlikely (notcvt == NULL))
151
0
      {
152
0
        __libelf_seterrno (ELF_E_NOMEM);
153
0
        goto out;
154
0
      }
155
0
          memcpy (notcvt, file_phdr, size);
156
0
        }
157
158
304k
      for (size_t cnt = 0; cnt < phnum; ++cnt)
159
295k
        {
160
295k
          CONVERT_TO (phdr[cnt].p_type, notcvt[cnt].p_type);
161
295k
          CONVERT_TO (phdr[cnt].p_offset, notcvt[cnt].p_offset);
162
295k
          CONVERT_TO (phdr[cnt].p_vaddr, notcvt[cnt].p_vaddr);
163
295k
          CONVERT_TO (phdr[cnt].p_paddr, notcvt[cnt].p_paddr);
164
295k
          CONVERT_TO (phdr[cnt].p_filesz, notcvt[cnt].p_filesz);
165
295k
          CONVERT_TO (phdr[cnt].p_memsz, notcvt[cnt].p_memsz);
166
295k
          CONVERT_TO (phdr[cnt].p_flags, notcvt[cnt].p_flags);
167
295k
          CONVERT_TO (phdr[cnt].p_align, notcvt[cnt].p_align);
168
295k
        }
169
170
8.73k
      if (copy)
171
0
        free (notcvt);
172
8.73k
    }
173
8.73k
      }
174
11.2k
  }
175
0
      else if (likely (elf->fildes != -1))
176
0
  {
177
    /* Allocate memory for the program headers.  We know the number
178
       of entries from the ELF header.  */
179
0
    elf->state.ELFW(elf,LIBELFBITS).phdr =
180
0
      (ElfW2(LIBELFBITS,Phdr) *) malloc (size);
181
0
    if (elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
182
0
      {
183
0
        __libelf_seterrno (ELF_E_NOMEM);
184
0
        goto out;
185
0
      }
186
0
    elf->state.ELFW(elf,LIBELFBITS).phdr_flags |= ELF_F_MALLOCED;
187
188
    /* Read the header.  */
189
0
    ssize_t n = pread_retry (elf->fildes,
190
0
           elf->state.ELFW(elf,LIBELFBITS).phdr, size,
191
0
           elf->start_offset + ehdr->e_phoff);
192
0
    if (unlikely ((size_t) n != size))
193
0
      {
194
        /* Severe problems.  We cannot read the data.  */
195
0
        __libelf_seterrno (ELF_E_READ_ERROR);
196
0
        free (elf->state.ELFW(elf,LIBELFBITS).phdr);
197
0
        elf->state.ELFW(elf,LIBELFBITS).phdr = NULL;
198
0
        goto out;
199
0
      }
200
201
    /* If the byte order of the file is not the same as the one
202
       of the host convert the data now.  */
203
0
    if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
204
0
      {
205
0
        ElfW2(LIBELFBITS,Phdr) *phdr
206
0
    = elf->state.ELFW(elf,LIBELFBITS).phdr;
207
208
0
        for (size_t cnt = 0; cnt < phnum; ++cnt)
209
0
    {
210
0
      CONVERT (phdr[cnt].p_type);
211
0
      CONVERT (phdr[cnt].p_offset);
212
0
      CONVERT (phdr[cnt].p_vaddr);
213
0
      CONVERT (phdr[cnt].p_paddr);
214
0
      CONVERT (phdr[cnt].p_filesz);
215
0
      CONVERT (phdr[cnt].p_memsz);
216
0
      CONVERT (phdr[cnt].p_flags);
217
0
      CONVERT (phdr[cnt].p_align);
218
0
    }
219
0
      }
220
0
  }
221
0
      else
222
0
  {
223
    /* The file descriptor was already enabled and not all data was
224
       read.  */
225
0
    __libelf_seterrno (ELF_E_FD_DISABLED);
226
0
    goto out;
227
0
  }
228
229
11.2k
      result = elf->state.ELFW(elf,LIBELFBITS).phdr;
230
11.2k
    }
231
232
11.3k
 out:
233
11.3k
  return result;
234
11.3k
}
__elf64_getphdr_wrlock
Line
Count
Source
48
7.52k
{
49
7.52k
  ElfW2(LIBELFBITS,Phdr) *result;
50
51
  /* If the program header entry has already been filled in the code
52
     below must already have been run.  So the class is set, too.  No
53
     need to waste any more time here.  */
54
7.52k
  result = elf->state.ELFW(elf,LIBELFBITS).phdr;
55
7.52k
  if (likely (result != NULL))
56
0
    return result;
57
58
7.52k
  if (elf->class == 0)
59
0
    elf->class = ELFW(ELFCLASS,LIBELFBITS);
60
7.52k
  else if (elf->class != ELFW(ELFCLASS,LIBELFBITS))
61
0
    {
62
0
      __libelf_seterrno (ELF_E_INVALID_CLASS);
63
0
      result = NULL;
64
0
      goto out;
65
0
    }
66
67
7.52k
  if (likely (result == NULL))
68
7.52k
    {
69
      /* Read the section header table.  */
70
7.52k
      ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
71
72
      /* If no program header exists return NULL.  */
73
7.52k
      size_t phnum;
74
7.52k
      if (__elf_getphdrnum_rdlock (elf, &phnum) != 0)
75
0
  goto out;
76
7.52k
      if (phnum == 0 || ehdr->e_phoff == 0)
77
0
  {
78
0
    __libelf_seterrno (ELF_E_NO_PHDR);
79
0
    goto out;
80
0
  }
81
82
      /* Check this doesn't overflow.  */
83
7.52k
      size_t size = phnum * sizeof (ElfW2(LIBELFBITS,Phdr));
84
85
7.52k
      if (phnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Phdr))
86
7.52k
    || ehdr->e_phoff > elf->maximum_size
87
7.52k
    || elf->maximum_size - ehdr->e_phoff < size)
88
72
  {
89
72
    __libelf_seterrno (ELF_E_INVALID_DATA);
90
72
    goto out;
91
72
  }
92
93
7.45k
      if (elf->map_address != NULL)
94
7.45k
  {
95
    /* First see whether the information in the ELF header is
96
       valid and it does not ask for too much.  */
97
7.45k
    if (unlikely (ehdr->e_phoff >= elf->maximum_size)
98
7.45k
        || unlikely (elf->maximum_size - ehdr->e_phoff < size))
99
0
      {
100
        /* Something is wrong.  */
101
0
        __libelf_seterrno (ELF_E_INVALID_PHDR);
102
0
        goto out;
103
0
      }
104
105
    /* All the data is already mapped.  Use it.  */
106
7.45k
    void *file_phdr = ((char *) elf->map_address
107
7.45k
           + elf->start_offset + ehdr->e_phoff);
108
7.45k
    if (ehdr->e_ident[EI_DATA] == MY_ELFDATA
109
6.97k
        && (ALLOW_UNALIGNED
110
0
      || ((uintptr_t) file_phdr
111
0
          & (__alignof__ (ElfW2(LIBELFBITS,Phdr)) - 1)) == 0))
112
      /* Simply use the mapped data.  */
113
6.97k
      elf->state.ELFW(elf,LIBELFBITS).phdr = file_phdr;
114
478
    else
115
478
      {
116
478
        ElfW2(LIBELFBITS,Phdr) *notcvt;
117
478
        ElfW2(LIBELFBITS,Phdr) *phdr;
118
119
        /* Allocate memory for the program headers.  We know the number
120
     of entries from the ELF header.  */
121
478
        phdr = elf->state.ELFW(elf,LIBELFBITS).phdr =
122
478
    (ElfW2(LIBELFBITS,Phdr) *) malloc (size);
123
478
        if (elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
124
0
    {
125
0
      __libelf_seterrno (ELF_E_NOMEM);
126
0
      goto out;
127
0
    }
128
478
        elf->state.ELFW(elf,LIBELFBITS).phdr_flags |=
129
478
    ELF_F_MALLOCED | ELF_F_DIRTY;
130
131
        /* Now copy the data and at the same time convert the
132
     byte order.  */
133
134
478
        if (ehdr->e_ident[EI_DATA] == MY_ELFDATA)
135
0
    {
136
0
      assert (! ALLOW_UNALIGNED);
137
0
      memcpy (phdr, file_phdr, size);
138
0
    }
139
478
        else
140
478
    {
141
478
      bool copy = ! (ALLOW_UNALIGNED
142
0
         || ((uintptr_t) file_phdr
143
0
             & (__alignof__ (ElfW2(LIBELFBITS,Phdr))
144
0
          - 1)) == 0);
145
478
      if (! copy)
146
478
        notcvt = file_phdr;
147
0
      else
148
0
        {
149
0
          notcvt = (ElfW2(LIBELFBITS,Phdr) *) malloc (size);
150
0
          if (unlikely (notcvt == NULL))
151
0
      {
152
0
        __libelf_seterrno (ELF_E_NOMEM);
153
0
        goto out;
154
0
      }
155
0
          memcpy (notcvt, file_phdr, size);
156
0
        }
157
158
1.74k
      for (size_t cnt = 0; cnt < phnum; ++cnt)
159
1.26k
        {
160
1.26k
          CONVERT_TO (phdr[cnt].p_type, notcvt[cnt].p_type);
161
1.26k
          CONVERT_TO (phdr[cnt].p_offset, notcvt[cnt].p_offset);
162
1.26k
          CONVERT_TO (phdr[cnt].p_vaddr, notcvt[cnt].p_vaddr);
163
1.26k
          CONVERT_TO (phdr[cnt].p_paddr, notcvt[cnt].p_paddr);
164
1.26k
          CONVERT_TO (phdr[cnt].p_filesz, notcvt[cnt].p_filesz);
165
1.26k
          CONVERT_TO (phdr[cnt].p_memsz, notcvt[cnt].p_memsz);
166
1.26k
          CONVERT_TO (phdr[cnt].p_flags, notcvt[cnt].p_flags);
167
1.26k
          CONVERT_TO (phdr[cnt].p_align, notcvt[cnt].p_align);
168
1.26k
        }
169
170
478
      if (copy)
171
0
        free (notcvt);
172
478
    }
173
478
      }
174
7.45k
  }
175
0
      else if (likely (elf->fildes != -1))
176
0
  {
177
    /* Allocate memory for the program headers.  We know the number
178
       of entries from the ELF header.  */
179
0
    elf->state.ELFW(elf,LIBELFBITS).phdr =
180
0
      (ElfW2(LIBELFBITS,Phdr) *) malloc (size);
181
0
    if (elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
182
0
      {
183
0
        __libelf_seterrno (ELF_E_NOMEM);
184
0
        goto out;
185
0
      }
186
0
    elf->state.ELFW(elf,LIBELFBITS).phdr_flags |= ELF_F_MALLOCED;
187
188
    /* Read the header.  */
189
0
    ssize_t n = pread_retry (elf->fildes,
190
0
           elf->state.ELFW(elf,LIBELFBITS).phdr, size,
191
0
           elf->start_offset + ehdr->e_phoff);
192
0
    if (unlikely ((size_t) n != size))
193
0
      {
194
        /* Severe problems.  We cannot read the data.  */
195
0
        __libelf_seterrno (ELF_E_READ_ERROR);
196
0
        free (elf->state.ELFW(elf,LIBELFBITS).phdr);
197
0
        elf->state.ELFW(elf,LIBELFBITS).phdr = NULL;
198
0
        goto out;
199
0
      }
200
201
    /* If the byte order of the file is not the same as the one
202
       of the host convert the data now.  */
203
0
    if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
204
0
      {
205
0
        ElfW2(LIBELFBITS,Phdr) *phdr
206
0
    = elf->state.ELFW(elf,LIBELFBITS).phdr;
207
208
0
        for (size_t cnt = 0; cnt < phnum; ++cnt)
209
0
    {
210
0
      CONVERT (phdr[cnt].p_type);
211
0
      CONVERT (phdr[cnt].p_offset);
212
0
      CONVERT (phdr[cnt].p_vaddr);
213
0
      CONVERT (phdr[cnt].p_paddr);
214
0
      CONVERT (phdr[cnt].p_filesz);
215
0
      CONVERT (phdr[cnt].p_memsz);
216
0
      CONVERT (phdr[cnt].p_flags);
217
0
      CONVERT (phdr[cnt].p_align);
218
0
    }
219
0
      }
220
0
  }
221
0
      else
222
0
  {
223
    /* The file descriptor was already enabled and not all data was
224
       read.  */
225
0
    __libelf_seterrno (ELF_E_FD_DISABLED);
226
0
    goto out;
227
0
  }
228
229
7.45k
      result = elf->state.ELFW(elf,LIBELFBITS).phdr;
230
7.45k
    }
231
232
7.52k
 out:
233
7.52k
  return result;
234
7.52k
}
235
236
ElfW2(LIBELFBITS,Phdr) *
237
elfw2(LIBELFBITS,getphdr) (Elf *elf)
238
18.8k
{
239
18.8k
  ElfW2(LIBELFBITS,Phdr) *result;
240
241
18.8k
  if (elf == NULL)
242
0
    return NULL;
243
244
18.8k
  if (unlikely (elf->kind != ELF_K_ELF))
245
0
    {
246
0
      __libelf_seterrno (ELF_E_INVALID_HANDLE);
247
0
      return NULL;
248
0
    }
249
250
  /* If the program header entry has already been filled in the code
251
   * in getphdr_wrlock must already have been run.  So the class is
252
   * set, too.  No need to waste any more time here.  */
253
18.8k
  result = elf->state.ELFW(elf,LIBELFBITS).phdr;
254
18.8k
  if (likely (result != NULL))
255
0
    return result;
256
257
18.8k
  rwlock_wrlock (elf->lock);
258
18.8k
  result = __elfw2(LIBELFBITS,getphdr_wrlock) (elf);
259
18.8k
  rwlock_unlock (elf->lock);
260
261
18.8k
  return result;
262
18.8k
}
elf32_getphdr
Line
Count
Source
238
11.3k
{
239
11.3k
  ElfW2(LIBELFBITS,Phdr) *result;
240
241
11.3k
  if (elf == NULL)
242
0
    return NULL;
243
244
11.3k
  if (unlikely (elf->kind != ELF_K_ELF))
245
0
    {
246
0
      __libelf_seterrno (ELF_E_INVALID_HANDLE);
247
0
      return NULL;
248
0
    }
249
250
  /* If the program header entry has already been filled in the code
251
   * in getphdr_wrlock must already have been run.  So the class is
252
   * set, too.  No need to waste any more time here.  */
253
11.3k
  result = elf->state.ELFW(elf,LIBELFBITS).phdr;
254
11.3k
  if (likely (result != NULL))
255
0
    return result;
256
257
11.3k
  rwlock_wrlock (elf->lock);
258
11.3k
  result = __elfw2(LIBELFBITS,getphdr_wrlock) (elf);
259
11.3k
  rwlock_unlock (elf->lock);
260
261
11.3k
  return result;
262
11.3k
}
elf64_getphdr
Line
Count
Source
238
7.52k
{
239
7.52k
  ElfW2(LIBELFBITS,Phdr) *result;
240
241
7.52k
  if (elf == NULL)
242
0
    return NULL;
243
244
7.52k
  if (unlikely (elf->kind != ELF_K_ELF))
245
0
    {
246
0
      __libelf_seterrno (ELF_E_INVALID_HANDLE);
247
0
      return NULL;
248
0
    }
249
250
  /* If the program header entry has already been filled in the code
251
   * in getphdr_wrlock must already have been run.  So the class is
252
   * set, too.  No need to waste any more time here.  */
253
7.52k
  result = elf->state.ELFW(elf,LIBELFBITS).phdr;
254
7.52k
  if (likely (result != NULL))
255
0
    return result;
256
257
7.52k
  rwlock_wrlock (elf->lock);
258
7.52k
  result = __elfw2(LIBELFBITS,getphdr_wrlock) (elf);
259
7.52k
  rwlock_unlock (elf->lock);
260
261
7.52k
  return result;
262
7.52k
}
263
INTDEF(elfw2(LIBELFBITS,getphdr))