Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / libelf / dist / libelf_phdr.c
blob29aba2ddcdc7626070d74ab245f532e0f48e7d42
1 /* $NetBSD: libelf_phdr.c,v 1.2 2009/12/20 23:23:46 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.
28 * $FreeBSD: src/lib/libelf/libelf_phdr.c,v 1.2.10.1.2.1 2009/10/25 01:10:29 kensmith Exp $
31 #include <sys/cdefs.h>
32 /* __FBSDID("$FreeBSD: src/lib/libelf/libelf_phdr.c,v 1.2.10.1.2.1 2009/10/25 01:10:29 kensmith Exp $"); */
34 #include <assert.h>
35 #include <gelf.h>
36 #include <libelf.h>
37 #include <stdlib.h>
39 #include "_libelf.h"
41 void *
42 _libelf_getphdr(Elf *e, int ec)
44 size_t phnum, phentsize;
45 size_t fsz, msz;
46 uint64_t phoff;
47 Elf32_Ehdr *eh32;
48 Elf64_Ehdr *eh64;
49 void *ehdr, *phdr;
50 void (*xlator)(char *_d, char *_s, size_t _c, int _swap);
52 assert(ec == ELFCLASS32 || ec == ELFCLASS64);
54 if (e == NULL) {
55 LIBELF_SET_ERROR(ARGUMENT, 0);
56 return (NULL);
59 if ((phdr = (ec == ELFCLASS32 ?
60 (void *) e->e_u.e_elf.e_phdr.e_phdr32 :
61 (void *) e->e_u.e_elf.e_phdr.e_phdr64)) != NULL)
62 return (phdr);
65 * Check the PHDR related fields in the EHDR for sanity.
68 if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
69 return (NULL);
71 phnum = e->e_u.e_elf.e_nphdr;
73 if (ec == ELFCLASS32) {
74 eh32 = (Elf32_Ehdr *) ehdr;
75 phentsize = eh32->e_phentsize;
76 phoff = (uint64_t) eh32->e_phoff;
77 } else {
78 eh64 = (Elf64_Ehdr *) ehdr;
79 phentsize = eh64->e_phentsize;
80 phoff = (uint64_t) eh64->e_phoff;
83 fsz = gelf_fsize(e, ELF_T_PHDR, phnum, e->e_version);
85 assert(fsz > 0);
87 if ((uint64_t) e->e_rawsize < (phoff + fsz)) {
88 LIBELF_SET_ERROR(HEADER, 0);
89 return (NULL);
92 msz = _libelf_msize(ELF_T_PHDR, ec, EV_CURRENT);
94 assert(msz > 0);
96 if ((phdr = calloc(phnum, msz)) == NULL) {
97 LIBELF_SET_ERROR(RESOURCE, 0);
98 return (NULL);
101 if (ec == ELFCLASS32)
102 e->e_u.e_elf.e_phdr.e_phdr32 = phdr;
103 else
104 e->e_u.e_elf.e_phdr.e_phdr64 = phdr;
107 xlator = _libelf_get_translator(ELF_T_PHDR, ELF_TOMEMORY, ec);
108 (*xlator)(phdr, e->e_rawfile + phoff, phnum,
109 e->e_byteorder != _libelf_host_byteorder());
111 return (phdr);
114 void *
115 _libelf_newphdr(Elf *e, int ec, size_t count)
117 void *ehdr, *newphdr, *oldphdr;
118 size_t msz;
120 if (e == NULL) {
121 LIBELF_SET_ERROR(ARGUMENT, 0);
122 return (NULL);
125 if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) {
126 LIBELF_SET_ERROR(SEQUENCE, 0);
127 return (NULL);
130 assert(e->e_class == ec);
131 assert(ec == ELFCLASS32 || ec == ELFCLASS64);
132 assert(e->e_version == EV_CURRENT);
134 msz = _libelf_msize(ELF_T_PHDR, ec, e->e_version);
136 assert(msz > 0);
138 newphdr = NULL;
139 if (count > 0 && (newphdr = calloc(count, msz)) == NULL) {
140 LIBELF_SET_ERROR(RESOURCE, 0);
141 return (NULL);
144 if (ec == ELFCLASS32) {
145 if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr32) != NULL)
146 free(oldphdr);
147 e->e_u.e_elf.e_phdr.e_phdr32 = (Elf32_Phdr *) newphdr;
148 } else {
149 if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr64) != NULL)
150 free(oldphdr);
151 e->e_u.e_elf.e_phdr.e_phdr64 = (Elf64_Phdr *) newphdr;
154 e->e_u.e_elf.e_nphdr = count;
156 elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY);
158 return (newphdr);