1/*-
2 * Copyright (c) 2006 Joseph Koshy
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <limits.h>
28
29#include "gelf.h"
30#include "libelf.h"
31
32#include "_libelf.h"
33
34Elf32_Phdr *
35elf32_getphdr(Elf *e)
36{
37        return (_libelf_getphdr(e, ELFCLASS32));
38}
39
40Elf64_Phdr *
41elf64_getphdr(Elf *e)
42{
43        return (_libelf_getphdr(e, ELFCLASS64));
44}
45
46GElf_Phdr *
47gelf_getphdr(Elf *e, int index, GElf_Phdr *d)
48{
49        int ec;
50        Elf32_Ehdr *eh32;
51        Elf64_Ehdr *eh64;
52        Elf32_Phdr *ep32;
53        Elf64_Phdr *ep64;
54
55        if (d == NULL || e == NULL ||
56            ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) ||
57            (e->e_kind != ELF_K_ELF) || index < 0) {
58                LIBELF_SET_ERROR(ARGUMENT, 0);
59                return (NULL);
60        }
61
62        if (ec == ELFCLASS32) {
63                if ((eh32 = _libelf_ehdr(e, ELFCLASS32, 0)) == NULL ||
64                    ((ep32 = _libelf_getphdr(e, ELFCLASS32)) == NULL))
65                        return (NULL);
66
67                if (index >= eh32->e_phnum) {
68                        LIBELF_SET_ERROR(ARGUMENT, 0);
69                        return (NULL);
70                }
71
72                ep32 += index;
73
74                d->p_type   = ep32->p_type;
75                d->p_offset = ep32->p_offset;
76                d->p_vaddr  = (Elf64_Addr) ep32->p_vaddr;
77                d->p_paddr  = (Elf64_Addr) ep32->p_paddr;
78                d->p_filesz = (Elf64_Xword) ep32->p_filesz;
79                d->p_memsz  = (Elf64_Xword) ep32->p_memsz;
80                d->p_flags  = ep32->p_flags;
81                d->p_align  = (Elf64_Xword) ep32->p_align;
82
83        } else {
84                if ((eh64 = _libelf_ehdr(e, ELFCLASS64, 0)) == NULL ||
85                    (ep64 = _libelf_getphdr(e, ELFCLASS64)) == NULL)
86                        return (NULL);
87
88                if (index >= eh64->e_phnum) {
89                        LIBELF_SET_ERROR(ARGUMENT, 0);
90                        return (NULL);
91                }
92
93                ep64 += index;
94
95                *d = *ep64;
96        }
97
98        return (d);
99}
100
101Elf32_Phdr *
102elf32_newphdr(Elf *e, size_t count)
103{
104        return (_libelf_newphdr(e, ELFCLASS32, count));
105}
106
107Elf64_Phdr *
108elf64_newphdr(Elf *e, size_t count)
109{
110        return (_libelf_newphdr(e, ELFCLASS64, count));
111}
112
113void *
114gelf_newphdr(Elf *e, size_t count)
115{
116        if (e == NULL) {
117                LIBELF_SET_ERROR(ARGUMENT, 0);
118                return (NULL);
119        }
120        return (_libelf_newphdr(e, e->e_class, count));
121}
122
123int
124gelf_update_phdr(Elf *e, int ndx, GElf_Phdr *s)
125{
126        int ec, phnum;
127        void *ehdr;
128        Elf32_Phdr *ph32;
129        Elf64_Phdr *ph64;
130
131        if (s == NULL || e == NULL || e->e_kind != ELF_K_ELF ||
132            ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
133                LIBELF_SET_ERROR(ARGUMENT, 0);
134                return (0);
135        }
136
137        if (e->e_cmd == ELF_C_READ) {
138                LIBELF_SET_ERROR(MODE, 0);
139                return (0);
140        }
141
142        if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
143                return (0);
144
145        if (ec == ELFCLASS32)
146                phnum = ((Elf32_Ehdr *) ehdr)->e_phnum;
147        else
148                phnum = ((Elf64_Ehdr *) ehdr)->e_phnum;
149
150        if (ndx < 0 || ndx > phnum) {
151                LIBELF_SET_ERROR(ARGUMENT, 0);
152                return (0);
153        }
154
155        if (ec == ELFCLASS64) {
156                ph64 = e->e_u.e_elf.e_phdr.e_phdr64 + ndx;
157                *ph64 = *s;
158                return (1);
159        }
160
161        ph32 = e->e_u.e_elf.e_phdr.e_phdr32 + ndx;
162
163        ph32->p_type     =  s->p_type;
164        ph32->p_flags    =  s->p_flags;
165        LIBELF_COPY_U32(ph32, s, p_offset);
166        LIBELF_COPY_U32(ph32, s, p_vaddr);
167        LIBELF_COPY_U32(ph32, s, p_paddr);
168        LIBELF_COPY_U32(ph32, s, p_filesz);
169        LIBELF_COPY_U32(ph32, s, p_memsz);
170        LIBELF_COPY_U32(ph32, s, p_align);
171
172        (void) elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY);
173
174        return (1);
175}
176