Coverage Report

Created: 2025-11-13 06:36

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
21.9k
{
49
21.9k
  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
21.9k
  result = elf->state.ELFW(elf,LIBELFBITS).phdr;
55
21.9k
  if (likely (result != NULL))
56
0
    return result;
57
58
21.9k
  if (elf->class == 0)
59
0
    elf->class = ELFW(ELFCLASS,LIBELFBITS);
60
21.9k
  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
21.9k
  if (likely (result == NULL))
68
21.9k
    {
69
      /* Read the section header table.  */
70
21.9k
      ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
71
72
      /* If no program header exists return NULL.  */
73
21.9k
      size_t phnum;
74
21.9k
      if (__elf_getphdrnum_rdlock (elf, &phnum) != 0)
75
0
  goto out;
76
21.9k
      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
21.9k
      size_t size = phnum * sizeof (ElfW2(LIBELFBITS,Phdr));
84
85
21.9k
      if (phnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Phdr))
86
21.9k
    || ehdr->e_phoff > elf->maximum_size
87
21.9k
    || elf->maximum_size - ehdr->e_phoff < size)
88
292
  {
89
292
    __libelf_seterrno (ELF_E_INVALID_DATA);
90
292
    goto out;
91
292
  }
92
93
21.6k
      if (elf->map_address != NULL)
94
21.6k
  {
95
    /* First see whether the information in the ELF header is
96
       valid and it does not ask for too much.  */
97
21.6k
    if (unlikely (ehdr->e_phoff >= elf->maximum_size)
98
21.6k
        || 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
21.6k
    void *file_phdr = ((char *) elf->map_address
107
21.6k
           + elf->start_offset + ehdr->e_phoff);
108
21.6k
    if (ehdr->e_ident[EI_DATA] == MY_ELFDATA
109
11.2k
        && (ALLOW_UNALIGNED
110
0
      || ((uintptr_t) file_phdr
111
0
          & (__alignof__ (ElfW2(LIBELFBITS,Phdr)) - 1)) == 0))
112
      /* Simply use the mapped data.  */
113
11.2k
      elf->state.ELFW(elf,LIBELFBITS).phdr = file_phdr;
114
10.3k
    else
115
10.3k
      {
116
10.3k
        ElfW2(LIBELFBITS,Phdr) *notcvt;
117
10.3k
        ElfW2(LIBELFBITS,Phdr) *phdr;
118
119
        /* Allocate memory for the program headers.  We know the number
120
     of entries from the ELF header.  */
121
10.3k
        phdr = elf->state.ELFW(elf,LIBELFBITS).phdr =
122
10.3k
    (ElfW2(LIBELFBITS,Phdr) *) malloc (size);
123
10.3k
        if (elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
124
0
    {
125
0
      __libelf_seterrno (ELF_E_NOMEM);
126
0
      goto out;
127
0
    }
128
10.3k
        elf->state.ELFW(elf,LIBELFBITS).phdr_flags |=
129
10.3k
    ELF_F_MALLOCED | ELF_F_DIRTY;
130
131
        /* Now copy the data and at the same time convert the
132
     byte order.  */
133
134
10.3k
        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
10.3k
        else
140
10.3k
    {
141
10.3k
      bool copy = ! (ALLOW_UNALIGNED
142
0
         || ((uintptr_t) file_phdr
143
0
             & (__alignof__ (ElfW2(LIBELFBITS,Phdr))
144
0
          - 1)) == 0);
145
10.3k
      if (! copy)
146
10.3k
        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
3.21M
      for (size_t cnt = 0; cnt < phnum; ++cnt)
159
3.20M
        {
160
3.20M
          CONVERT_TO (phdr[cnt].p_type, notcvt[cnt].p_type);
161
3.20M
          CONVERT_TO (phdr[cnt].p_offset, notcvt[cnt].p_offset);
162
3.20M
          CONVERT_TO (phdr[cnt].p_vaddr, notcvt[cnt].p_vaddr);
163
3.20M
          CONVERT_TO (phdr[cnt].p_paddr, notcvt[cnt].p_paddr);
164
3.20M
          CONVERT_TO (phdr[cnt].p_filesz, notcvt[cnt].p_filesz);
165
3.20M
          CONVERT_TO (phdr[cnt].p_memsz, notcvt[cnt].p_memsz);
166
3.20M
          CONVERT_TO (phdr[cnt].p_flags, notcvt[cnt].p_flags);
167
3.20M
          CONVERT_TO (phdr[cnt].p_align, notcvt[cnt].p_align);
168
3.20M
        }
169
170
10.3k
      if (copy)
171
0
        free (notcvt);
172
10.3k
    }
173
10.3k
      }
174
21.6k
  }
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
21.6k
      result = elf->state.ELFW(elf,LIBELFBITS).phdr;
230
21.6k
    }
231
232
21.9k
 out:
233
21.9k
  return result;
234
21.9k
}
__elf32_getphdr_wrlock
Line
Count
Source
48
13.0k
{
49
13.0k
  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
13.0k
  result = elf->state.ELFW(elf,LIBELFBITS).phdr;
55
13.0k
  if (likely (result != NULL))
56
0
    return result;
57
58
13.0k
  if (elf->class == 0)
59
0
    elf->class = ELFW(ELFCLASS,LIBELFBITS);
60
13.0k
  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
13.0k
  if (likely (result == NULL))
68
13.0k
    {
69
      /* Read the section header table.  */
70
13.0k
      ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
71
72
      /* If no program header exists return NULL.  */
73
13.0k
      size_t phnum;
74
13.0k
      if (__elf_getphdrnum_rdlock (elf, &phnum) != 0)
75
0
  goto out;
76
13.0k
      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
13.0k
      size_t size = phnum * sizeof (ElfW2(LIBELFBITS,Phdr));
84
85
13.0k
      if (phnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Phdr))
86
13.0k
    || ehdr->e_phoff > elf->maximum_size
87
13.0k
    || elf->maximum_size - ehdr->e_phoff < size)
88
147
  {
89
147
    __libelf_seterrno (ELF_E_INVALID_DATA);
90
147
    goto out;
91
147
  }
92
93
12.9k
      if (elf->map_address != NULL)
94
12.9k
  {
95
    /* First see whether the information in the ELF header is
96
       valid and it does not ask for too much.  */
97
12.9k
    if (unlikely (ehdr->e_phoff >= elf->maximum_size)
98
12.9k
        || 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
12.9k
    void *file_phdr = ((char *) elf->map_address
107
12.9k
           + elf->start_offset + ehdr->e_phoff);
108
12.9k
    if (ehdr->e_ident[EI_DATA] == MY_ELFDATA
109
3.67k
        && (ALLOW_UNALIGNED
110
0
      || ((uintptr_t) file_phdr
111
0
          & (__alignof__ (ElfW2(LIBELFBITS,Phdr)) - 1)) == 0))
112
      /* Simply use the mapped data.  */
113
3.67k
      elf->state.ELFW(elf,LIBELFBITS).phdr = file_phdr;
114
9.27k
    else
115
9.27k
      {
116
9.27k
        ElfW2(LIBELFBITS,Phdr) *notcvt;
117
9.27k
        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.27k
        phdr = elf->state.ELFW(elf,LIBELFBITS).phdr =
122
9.27k
    (ElfW2(LIBELFBITS,Phdr) *) malloc (size);
123
9.27k
        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.27k
        elf->state.ELFW(elf,LIBELFBITS).phdr_flags |=
129
9.27k
    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.27k
        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.27k
        else
140
9.27k
    {
141
9.27k
      bool copy = ! (ALLOW_UNALIGNED
142
0
         || ((uintptr_t) file_phdr
143
0
             & (__alignof__ (ElfW2(LIBELFBITS,Phdr))
144
0
          - 1)) == 0);
145
9.27k
      if (! copy)
146
9.27k
        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
2.28M
      for (size_t cnt = 0; cnt < phnum; ++cnt)
159
2.27M
        {
160
2.27M
          CONVERT_TO (phdr[cnt].p_type, notcvt[cnt].p_type);
161
2.27M
          CONVERT_TO (phdr[cnt].p_offset, notcvt[cnt].p_offset);
162
2.27M
          CONVERT_TO (phdr[cnt].p_vaddr, notcvt[cnt].p_vaddr);
163
2.27M
          CONVERT_TO (phdr[cnt].p_paddr, notcvt[cnt].p_paddr);
164
2.27M
          CONVERT_TO (phdr[cnt].p_filesz, notcvt[cnt].p_filesz);
165
2.27M
          CONVERT_TO (phdr[cnt].p_memsz, notcvt[cnt].p_memsz);
166
2.27M
          CONVERT_TO (phdr[cnt].p_flags, notcvt[cnt].p_flags);
167
2.27M
          CONVERT_TO (phdr[cnt].p_align, notcvt[cnt].p_align);
168
2.27M
        }
169
170
9.27k
      if (copy)
171
0
        free (notcvt);
172
9.27k
    }
173
9.27k
      }
174
12.9k
  }
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
12.9k
      result = elf->state.ELFW(elf,LIBELFBITS).phdr;
230
12.9k
    }
231
232
13.0k
 out:
233
13.0k
  return result;
234
13.0k
}
__elf64_getphdr_wrlock
Line
Count
Source
48
8.81k
{
49
8.81k
  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
8.81k
  result = elf->state.ELFW(elf,LIBELFBITS).phdr;
55
8.81k
  if (likely (result != NULL))
56
0
    return result;
57
58
8.81k
  if (elf->class == 0)
59
0
    elf->class = ELFW(ELFCLASS,LIBELFBITS);
60
8.81k
  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
8.81k
  if (likely (result == NULL))
68
8.81k
    {
69
      /* Read the section header table.  */
70
8.81k
      ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
71
72
      /* If no program header exists return NULL.  */
73
8.81k
      size_t phnum;
74
8.81k
      if (__elf_getphdrnum_rdlock (elf, &phnum) != 0)
75
0
  goto out;
76
8.81k
      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
8.81k
      size_t size = phnum * sizeof (ElfW2(LIBELFBITS,Phdr));
84
85
8.81k
      if (phnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Phdr))
86
8.81k
    || ehdr->e_phoff > elf->maximum_size
87
8.81k
    || elf->maximum_size - ehdr->e_phoff < size)
88
145
  {
89
145
    __libelf_seterrno (ELF_E_INVALID_DATA);
90
145
    goto out;
91
145
  }
92
93
8.66k
      if (elf->map_address != NULL)
94
8.66k
  {
95
    /* First see whether the information in the ELF header is
96
       valid and it does not ask for too much.  */
97
8.66k
    if (unlikely (ehdr->e_phoff >= elf->maximum_size)
98
8.66k
        || 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
8.66k
    void *file_phdr = ((char *) elf->map_address
107
8.66k
           + elf->start_offset + ehdr->e_phoff);
108
8.66k
    if (ehdr->e_ident[EI_DATA] == MY_ELFDATA
109
7.56k
        && (ALLOW_UNALIGNED
110
0
      || ((uintptr_t) file_phdr
111
0
          & (__alignof__ (ElfW2(LIBELFBITS,Phdr)) - 1)) == 0))
112
      /* Simply use the mapped data.  */
113
7.56k
      elf->state.ELFW(elf,LIBELFBITS).phdr = file_phdr;
114
1.10k
    else
115
1.10k
      {
116
1.10k
        ElfW2(LIBELFBITS,Phdr) *notcvt;
117
1.10k
        ElfW2(LIBELFBITS,Phdr) *phdr;
118
119
        /* Allocate memory for the program headers.  We know the number
120
     of entries from the ELF header.  */
121
1.10k
        phdr = elf->state.ELFW(elf,LIBELFBITS).phdr =
122
1.10k
    (ElfW2(LIBELFBITS,Phdr) *) malloc (size);
123
1.10k
        if (elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
124
0
    {
125
0
      __libelf_seterrno (ELF_E_NOMEM);
126
0
      goto out;
127
0
    }
128
1.10k
        elf->state.ELFW(elf,LIBELFBITS).phdr_flags |=
129
1.10k
    ELF_F_MALLOCED | ELF_F_DIRTY;
130
131
        /* Now copy the data and at the same time convert the
132
     byte order.  */
133
134
1.10k
        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
1.10k
        else
140
1.10k
    {
141
1.10k
      bool copy = ! (ALLOW_UNALIGNED
142
0
         || ((uintptr_t) file_phdr
143
0
             & (__alignof__ (ElfW2(LIBELFBITS,Phdr))
144
0
          - 1)) == 0);
145
1.10k
      if (! copy)
146
1.10k
        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
931k
      for (size_t cnt = 0; cnt < phnum; ++cnt)
159
930k
        {
160
930k
          CONVERT_TO (phdr[cnt].p_type, notcvt[cnt].p_type);
161
930k
          CONVERT_TO (phdr[cnt].p_offset, notcvt[cnt].p_offset);
162
930k
          CONVERT_TO (phdr[cnt].p_vaddr, notcvt[cnt].p_vaddr);
163
930k
          CONVERT_TO (phdr[cnt].p_paddr, notcvt[cnt].p_paddr);
164
930k
          CONVERT_TO (phdr[cnt].p_filesz, notcvt[cnt].p_filesz);
165
930k
          CONVERT_TO (phdr[cnt].p_memsz, notcvt[cnt].p_memsz);
166
930k
          CONVERT_TO (phdr[cnt].p_flags, notcvt[cnt].p_flags);
167
930k
          CONVERT_TO (phdr[cnt].p_align, notcvt[cnt].p_align);
168
930k
        }
169
170
1.10k
      if (copy)
171
0
        free (notcvt);
172
1.10k
    }
173
1.10k
      }
174
8.66k
  }
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
8.66k
      result = elf->state.ELFW(elf,LIBELFBITS).phdr;
230
8.66k
    }
231
232
8.81k
 out:
233
8.81k
  return result;
234
8.81k
}
235
236
ElfW2(LIBELFBITS,Phdr) *
237
elfw2(LIBELFBITS,getphdr) (Elf *elf)
238
21.9k
{
239
21.9k
  ElfW2(LIBELFBITS,Phdr) *result;
240
241
21.9k
  if (elf == NULL)
242
0
    return NULL;
243
244
21.9k
  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
21.9k
  result = elf->state.ELFW(elf,LIBELFBITS).phdr;
254
21.9k
  if (likely (result != NULL))
255
0
    return result;
256
257
21.9k
  rwlock_wrlock (elf->lock);
258
21.9k
  result = __elfw2(LIBELFBITS,getphdr_wrlock) (elf);
259
21.9k
  rwlock_unlock (elf->lock);
260
261
21.9k
  return result;
262
21.9k
}
elf32_getphdr
Line
Count
Source
238
13.0k
{
239
13.0k
  ElfW2(LIBELFBITS,Phdr) *result;
240
241
13.0k
  if (elf == NULL)
242
0
    return NULL;
243
244
13.0k
  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
13.0k
  result = elf->state.ELFW(elf,LIBELFBITS).phdr;
254
13.0k
  if (likely (result != NULL))
255
0
    return result;
256
257
13.0k
  rwlock_wrlock (elf->lock);
258
13.0k
  result = __elfw2(LIBELFBITS,getphdr_wrlock) (elf);
259
13.0k
  rwlock_unlock (elf->lock);
260
261
13.0k
  return result;
262
13.0k
}
elf64_getphdr
Line
Count
Source
238
8.81k
{
239
8.81k
  ElfW2(LIBELFBITS,Phdr) *result;
240
241
8.81k
  if (elf == NULL)
242
0
    return NULL;
243
244
8.81k
  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
8.81k
  result = elf->state.ELFW(elf,LIBELFBITS).phdr;
254
8.81k
  if (likely (result != NULL))
255
0
    return result;
256
257
8.81k
  rwlock_wrlock (elf->lock);
258
8.81k
  result = __elfw2(LIBELFBITS,getphdr_wrlock) (elf);
259
8.81k
  rwlock_unlock (elf->lock);
260
261
8.81k
  return result;
262
8.81k
}
263
INTDEF(elfw2(LIBELFBITS,getphdr))