Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / bsd / libelf / dist / gelf_ehdr.c
blobb786f8b0043f8f0d3812a511a4ee851e273448b2
1 /* $NetBSD: gelf_ehdr.c,v 1.2 2009/12/19 06:39:29 thorpej Exp $ */
3 /*-
4 * Copyright (c) 2006 Joseph Koshy
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 /* __FBSDID("$FreeBSD: src/lib/libelf/gelf_ehdr.c,v 1.1.10.1.2.1 2009/10/25 01:10:29 kensmith Exp $"); */
32 #include <assert.h>
33 #include <limits.h>
34 #include <gelf.h>
35 #include <libelf.h>
36 #include <string.h>
38 #include "_libelf.h"
40 Elf32_Ehdr *
41 elf32_getehdr(Elf *e)
43 return (_libelf_ehdr(e, ELFCLASS32, 0));
46 Elf64_Ehdr *
47 elf64_getehdr(Elf *e)
49 return (_libelf_ehdr(e, ELFCLASS64, 0));
52 GElf_Ehdr *
53 gelf_getehdr(Elf *e, GElf_Ehdr *d)
55 int ec;
56 Elf32_Ehdr *eh32;
57 Elf64_Ehdr *eh64;
59 if (d == NULL || e == NULL ||
60 ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
61 LIBELF_SET_ERROR(ARGUMENT, 0);
62 return (NULL);
65 if (ec == ELFCLASS32) {
66 if ((eh32 = _libelf_ehdr(e, ELFCLASS32, 0)) == NULL)
67 return (NULL);
69 (void) memcpy(d->e_ident, eh32->e_ident, sizeof(eh32->e_ident));
70 d->e_type = eh32->e_type;
71 d->e_machine = eh32->e_machine;
72 d->e_version = eh32->e_version;
73 d->e_entry = eh32->e_entry;
74 d->e_phoff = eh32->e_phoff;
75 d->e_shoff = eh32->e_shoff;
76 d->e_flags = eh32->e_flags;
77 d->e_ehsize = eh32->e_ehsize;
78 d->e_phentsize = eh32->e_phentsize;
79 d->e_phnum = eh32->e_phnum;
80 d->e_shentsize = eh32->e_shentsize;
81 d->e_shnum = eh32->e_shnum;
82 d->e_shstrndx = eh32->e_shstrndx;
84 return (d);
87 assert(ec == ELFCLASS64);
89 if ((eh64 = _libelf_ehdr(e, ELFCLASS64, 0)) == NULL)
90 return (NULL);
91 *d = *eh64;
93 return (d);
96 Elf32_Ehdr *
97 elf32_newehdr(Elf *e)
99 return (_libelf_ehdr(e, ELFCLASS32, 1));
102 Elf64_Ehdr *
103 elf64_newehdr(Elf *e)
105 return (_libelf_ehdr(e, ELFCLASS64, 1));
108 void *
109 gelf_newehdr(Elf *e, int ec)
111 if (e != NULL &&
112 (ec == ELFCLASS32 || ec == ELFCLASS64))
113 return (_libelf_ehdr(e, ec, 1));
115 LIBELF_SET_ERROR(ARGUMENT, 0);
116 return (NULL);
120 gelf_update_ehdr(Elf *e, GElf_Ehdr *s)
122 int ec;
123 void *ehdr;
124 Elf32_Ehdr *eh32;
125 Elf64_Ehdr *eh64;
127 if (s== NULL || e == NULL || e->e_kind != ELF_K_ELF ||
128 ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
129 LIBELF_SET_ERROR(ARGUMENT, 0);
130 return (0);
133 if (e->e_cmd == ELF_C_READ) {
134 LIBELF_SET_ERROR(MODE, 0);
135 return (0);
138 if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
139 return (0);
141 if (ec == ELFCLASS64) {
142 eh64 = (Elf64_Ehdr *) ehdr;
143 *eh64 = *s;
144 return (1);
147 eh32 = (Elf32_Ehdr *) ehdr;
149 (void) memcpy(eh32->e_ident, s->e_ident, sizeof(eh32->e_ident));
151 eh32->e_type = s->e_type;
152 eh32->e_machine = s->e_machine;
153 eh32->e_version = s->e_version;
154 LIBELF_COPY_U32(eh32, s, e_entry);
155 LIBELF_COPY_U32(eh32, s, e_phoff);
156 LIBELF_COPY_U32(eh32, s, e_shoff);
157 eh32->e_flags = s->e_flags;
158 eh32->e_ehsize = s->e_ehsize;
159 eh32->e_phentsize = s->e_phentsize;
160 eh32->e_phnum = s->e_phnum;
161 eh32->e_shentsize = s->e_shentsize;
162 eh32->e_shnum = s->e_shnum;
163 eh32->e_shstrndx = s->e_shstrndx;
165 (void) elf_flagehdr(e, ELF_C_SET, ELF_F_DIRTY);
167 return (1);