1*0a6a1f1dSLionel Sambuc /* $NetBSD: gelf_ehdr.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 #if HAVE_NBTOOL_CONFIG_H
30*0a6a1f1dSLionel Sambuc # include "nbtool_config.h"
31*0a6a1f1dSLionel Sambuc #endif
32*0a6a1f1dSLionel Sambuc
33*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
34*0a6a1f1dSLionel Sambuc
35*0a6a1f1dSLionel Sambuc #include <assert.h>
36*0a6a1f1dSLionel Sambuc #include <gelf.h>
37*0a6a1f1dSLionel Sambuc #include <libelf.h>
38*0a6a1f1dSLionel Sambuc #include <limits.h>
39*0a6a1f1dSLionel Sambuc #include <string.h>
40*0a6a1f1dSLionel Sambuc
41*0a6a1f1dSLionel Sambuc #include "_libelf.h"
42*0a6a1f1dSLionel Sambuc
43*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: gelf_ehdr.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
44*0a6a1f1dSLionel Sambuc ELFTC_VCSID("Id: gelf_ehdr.c 2268 2011-12-03 17:05:11Z jkoshy ");
45*0a6a1f1dSLionel Sambuc
46*0a6a1f1dSLionel Sambuc Elf32_Ehdr *
elf32_getehdr(Elf * e)47*0a6a1f1dSLionel Sambuc elf32_getehdr(Elf *e)
48*0a6a1f1dSLionel Sambuc {
49*0a6a1f1dSLionel Sambuc return (_libelf_ehdr(e, ELFCLASS32, 0));
50*0a6a1f1dSLionel Sambuc }
51*0a6a1f1dSLionel Sambuc
52*0a6a1f1dSLionel Sambuc Elf64_Ehdr *
elf64_getehdr(Elf * e)53*0a6a1f1dSLionel Sambuc elf64_getehdr(Elf *e)
54*0a6a1f1dSLionel Sambuc {
55*0a6a1f1dSLionel Sambuc return (_libelf_ehdr(e, ELFCLASS64, 0));
56*0a6a1f1dSLionel Sambuc }
57*0a6a1f1dSLionel Sambuc
58*0a6a1f1dSLionel Sambuc GElf_Ehdr *
gelf_getehdr(Elf * e,GElf_Ehdr * d)59*0a6a1f1dSLionel Sambuc gelf_getehdr(Elf *e, GElf_Ehdr *d)
60*0a6a1f1dSLionel Sambuc {
61*0a6a1f1dSLionel Sambuc int ec;
62*0a6a1f1dSLionel Sambuc Elf32_Ehdr *eh32;
63*0a6a1f1dSLionel Sambuc Elf64_Ehdr *eh64;
64*0a6a1f1dSLionel Sambuc
65*0a6a1f1dSLionel Sambuc if (d == NULL || e == NULL ||
66*0a6a1f1dSLionel Sambuc ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
67*0a6a1f1dSLionel Sambuc LIBELF_SET_ERROR(ARGUMENT, 0);
68*0a6a1f1dSLionel Sambuc return (NULL);
69*0a6a1f1dSLionel Sambuc }
70*0a6a1f1dSLionel Sambuc
71*0a6a1f1dSLionel Sambuc if (ec == ELFCLASS32) {
72*0a6a1f1dSLionel Sambuc if ((eh32 = _libelf_ehdr(e, ELFCLASS32, 0)) == NULL)
73*0a6a1f1dSLionel Sambuc return (NULL);
74*0a6a1f1dSLionel Sambuc
75*0a6a1f1dSLionel Sambuc (void) memcpy(d->e_ident, eh32->e_ident,
76*0a6a1f1dSLionel Sambuc sizeof(eh32->e_ident));
77*0a6a1f1dSLionel Sambuc d->e_type = eh32->e_type;
78*0a6a1f1dSLionel Sambuc d->e_machine = eh32->e_machine;
79*0a6a1f1dSLionel Sambuc d->e_version = eh32->e_version;
80*0a6a1f1dSLionel Sambuc d->e_entry = eh32->e_entry;
81*0a6a1f1dSLionel Sambuc d->e_phoff = eh32->e_phoff;
82*0a6a1f1dSLionel Sambuc d->e_shoff = eh32->e_shoff;
83*0a6a1f1dSLionel Sambuc d->e_flags = eh32->e_flags;
84*0a6a1f1dSLionel Sambuc d->e_ehsize = eh32->e_ehsize;
85*0a6a1f1dSLionel Sambuc d->e_phentsize = eh32->e_phentsize;
86*0a6a1f1dSLionel Sambuc d->e_phnum = eh32->e_phnum;
87*0a6a1f1dSLionel Sambuc d->e_shentsize = eh32->e_shentsize;
88*0a6a1f1dSLionel Sambuc d->e_shnum = eh32->e_shnum;
89*0a6a1f1dSLionel Sambuc d->e_shstrndx = eh32->e_shstrndx;
90*0a6a1f1dSLionel Sambuc
91*0a6a1f1dSLionel Sambuc return (d);
92*0a6a1f1dSLionel Sambuc }
93*0a6a1f1dSLionel Sambuc
94*0a6a1f1dSLionel Sambuc assert(ec == ELFCLASS64);
95*0a6a1f1dSLionel Sambuc
96*0a6a1f1dSLionel Sambuc if ((eh64 = _libelf_ehdr(e, ELFCLASS64, 0)) == NULL)
97*0a6a1f1dSLionel Sambuc return (NULL);
98*0a6a1f1dSLionel Sambuc *d = *eh64;
99*0a6a1f1dSLionel Sambuc
100*0a6a1f1dSLionel Sambuc return (d);
101*0a6a1f1dSLionel Sambuc }
102*0a6a1f1dSLionel Sambuc
103*0a6a1f1dSLionel Sambuc Elf32_Ehdr *
elf32_newehdr(Elf * e)104*0a6a1f1dSLionel Sambuc elf32_newehdr(Elf *e)
105*0a6a1f1dSLionel Sambuc {
106*0a6a1f1dSLionel Sambuc return (_libelf_ehdr(e, ELFCLASS32, 1));
107*0a6a1f1dSLionel Sambuc }
108*0a6a1f1dSLionel Sambuc
109*0a6a1f1dSLionel Sambuc Elf64_Ehdr *
elf64_newehdr(Elf * e)110*0a6a1f1dSLionel Sambuc elf64_newehdr(Elf *e)
111*0a6a1f1dSLionel Sambuc {
112*0a6a1f1dSLionel Sambuc return (_libelf_ehdr(e, ELFCLASS64, 1));
113*0a6a1f1dSLionel Sambuc }
114*0a6a1f1dSLionel Sambuc
115*0a6a1f1dSLionel Sambuc void *
gelf_newehdr(Elf * e,int ec)116*0a6a1f1dSLionel Sambuc gelf_newehdr(Elf *e, int ec)
117*0a6a1f1dSLionel Sambuc {
118*0a6a1f1dSLionel Sambuc if (e != NULL &&
119*0a6a1f1dSLionel Sambuc (ec == ELFCLASS32 || ec == ELFCLASS64))
120*0a6a1f1dSLionel Sambuc return (_libelf_ehdr(e, ec, 1));
121*0a6a1f1dSLionel Sambuc
122*0a6a1f1dSLionel Sambuc LIBELF_SET_ERROR(ARGUMENT, 0);
123*0a6a1f1dSLionel Sambuc return (NULL);
124*0a6a1f1dSLionel Sambuc }
125*0a6a1f1dSLionel Sambuc
126*0a6a1f1dSLionel Sambuc int
gelf_update_ehdr(Elf * e,GElf_Ehdr * s)127*0a6a1f1dSLionel Sambuc gelf_update_ehdr(Elf *e, GElf_Ehdr *s)
128*0a6a1f1dSLionel Sambuc {
129*0a6a1f1dSLionel Sambuc int ec;
130*0a6a1f1dSLionel Sambuc void *ehdr;
131*0a6a1f1dSLionel Sambuc Elf32_Ehdr *eh32;
132*0a6a1f1dSLionel Sambuc Elf64_Ehdr *eh64;
133*0a6a1f1dSLionel Sambuc
134*0a6a1f1dSLionel Sambuc if (s== NULL || e == NULL || e->e_kind != ELF_K_ELF ||
135*0a6a1f1dSLionel Sambuc ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
136*0a6a1f1dSLionel Sambuc LIBELF_SET_ERROR(ARGUMENT, 0);
137*0a6a1f1dSLionel Sambuc return (0);
138*0a6a1f1dSLionel Sambuc }
139*0a6a1f1dSLionel Sambuc
140*0a6a1f1dSLionel Sambuc if (e->e_cmd == ELF_C_READ) {
141*0a6a1f1dSLionel Sambuc LIBELF_SET_ERROR(MODE, 0);
142*0a6a1f1dSLionel Sambuc return (0);
143*0a6a1f1dSLionel Sambuc }
144*0a6a1f1dSLionel Sambuc
145*0a6a1f1dSLionel Sambuc if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
146*0a6a1f1dSLionel Sambuc return (0);
147*0a6a1f1dSLionel Sambuc
148*0a6a1f1dSLionel Sambuc (void) elf_flagehdr(e, ELF_C_SET, ELF_F_DIRTY);
149*0a6a1f1dSLionel Sambuc
150*0a6a1f1dSLionel Sambuc if (ec == ELFCLASS64) {
151*0a6a1f1dSLionel Sambuc eh64 = (Elf64_Ehdr *) ehdr;
152*0a6a1f1dSLionel Sambuc *eh64 = *s;
153*0a6a1f1dSLionel Sambuc return (1);
154*0a6a1f1dSLionel Sambuc }
155*0a6a1f1dSLionel Sambuc
156*0a6a1f1dSLionel Sambuc eh32 = (Elf32_Ehdr *) ehdr;
157*0a6a1f1dSLionel Sambuc
158*0a6a1f1dSLionel Sambuc (void) memcpy(eh32->e_ident, s->e_ident, sizeof(eh32->e_ident));
159*0a6a1f1dSLionel Sambuc
160*0a6a1f1dSLionel Sambuc eh32->e_type = s->e_type;
161*0a6a1f1dSLionel Sambuc eh32->e_machine = s->e_machine;
162*0a6a1f1dSLionel Sambuc eh32->e_version = s->e_version;
163*0a6a1f1dSLionel Sambuc LIBELF_COPY_U32(eh32, s, e_entry);
164*0a6a1f1dSLionel Sambuc LIBELF_COPY_U32(eh32, s, e_phoff);
165*0a6a1f1dSLionel Sambuc LIBELF_COPY_U32(eh32, s, e_shoff);
166*0a6a1f1dSLionel Sambuc eh32->e_flags = s->e_flags;
167*0a6a1f1dSLionel Sambuc eh32->e_ehsize = s->e_ehsize;
168*0a6a1f1dSLionel Sambuc eh32->e_phentsize = s->e_phentsize;
169*0a6a1f1dSLionel Sambuc eh32->e_phnum = s->e_phnum;
170*0a6a1f1dSLionel Sambuc eh32->e_shentsize = s->e_shentsize;
171*0a6a1f1dSLionel Sambuc eh32->e_shnum = s->e_shnum;
172*0a6a1f1dSLionel Sambuc eh32->e_shstrndx = s->e_shstrndx;
173*0a6a1f1dSLionel Sambuc
174*0a6a1f1dSLionel Sambuc return (1);
175*0a6a1f1dSLionel Sambuc }
176