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