1 /* $NetBSD: libelf_phdr.c,v 1.2 2014/03/09 16:58:04 christos Exp $ */
4 * Copyright (c) 2006,2008 Joseph Koshy
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
29 #if HAVE_NBTOOL_CONFIG_H
30 # include "nbtool_config.h"
33 #include <sys/cdefs.h>
42 __RCSID("$NetBSD: libelf_phdr.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
43 ELFTC_VCSID("Id: libelf_phdr.c 2931 2013-03-23 11:41:07Z jkoshy ");
46 _libelf_getphdr(Elf
*e
, int ec
)
54 int (*xlator
)(char *_d
, size_t _dsz
, char *_s
, size_t _c
, int _swap
);
56 assert(ec
== ELFCLASS32
|| ec
== ELFCLASS64
);
59 LIBELF_SET_ERROR(ARGUMENT
, 0);
63 if ((phdr
= (ec
== ELFCLASS32
?
64 (void *) e
->e_u
.e_elf
.e_phdr
.e_phdr32
:
65 (void *) e
->e_u
.e_elf
.e_phdr
.e_phdr64
)) != NULL
)
69 * Check the PHDR related fields in the EHDR for sanity.
72 if ((ehdr
= _libelf_ehdr(e
, ec
, 0)) == NULL
)
75 phnum
= e
->e_u
.e_elf
.e_nphdr
;
77 if (ec
== ELFCLASS32
) {
78 eh32
= (Elf32_Ehdr
*) ehdr
;
79 phoff
= (uint64_t) eh32
->e_phoff
;
81 eh64
= (Elf64_Ehdr
*) ehdr
;
82 phoff
= (uint64_t) eh64
->e_phoff
;
85 fsz
= gelf_fsize(e
, ELF_T_PHDR
, phnum
, e
->e_version
);
89 if ((uint64_t) e
->e_rawsize
< (phoff
+ fsz
)) {
90 LIBELF_SET_ERROR(HEADER
, 0);
94 msz
= _libelf_msize(ELF_T_PHDR
, ec
, EV_CURRENT
);
98 if ((phdr
= calloc(phnum
, msz
)) == NULL
) {
99 LIBELF_SET_ERROR(RESOURCE
, 0);
103 if (ec
== ELFCLASS32
)
104 e
->e_u
.e_elf
.e_phdr
.e_phdr32
= phdr
;
106 e
->e_u
.e_elf
.e_phdr
.e_phdr64
= phdr
;
109 xlator
= _libelf_get_translator(ELF_T_PHDR
, ELF_TOMEMORY
, ec
);
110 (*xlator
)(phdr
, phnum
* msz
, e
->e_rawfile
+ phoff
, phnum
,
111 e
->e_byteorder
!= _libelf_host_byteorder());
117 _libelf_newphdr(Elf
*e
, int ec
, size_t count
)
119 void *ehdr
, *newphdr
, *oldphdr
;
123 LIBELF_SET_ERROR(ARGUMENT
, 0);
127 if ((ehdr
= _libelf_ehdr(e
, ec
, 0)) == NULL
) {
128 LIBELF_SET_ERROR(SEQUENCE
, 0);
132 assert(e
->e_class
== ec
);
133 assert(ec
== ELFCLASS32
|| ec
== ELFCLASS64
);
134 assert(e
->e_version
== EV_CURRENT
);
136 msz
= _libelf_msize(ELF_T_PHDR
, ec
, e
->e_version
);
141 if (count
> 0 && (newphdr
= calloc(count
, msz
)) == NULL
) {
142 LIBELF_SET_ERROR(RESOURCE
, 0);
146 if (ec
== ELFCLASS32
) {
147 if ((oldphdr
= (void *) e
->e_u
.e_elf
.e_phdr
.e_phdr32
) != NULL
)
149 e
->e_u
.e_elf
.e_phdr
.e_phdr32
= (Elf32_Phdr
*) newphdr
;
151 if ((oldphdr
= (void *) e
->e_u
.e_elf
.e_phdr
.e_phdr64
) != NULL
)
153 e
->e_u
.e_elf
.e_phdr
.e_phdr64
= (Elf64_Phdr
*) newphdr
;
156 e
->e_u
.e_elf
.e_nphdr
= count
;
158 elf_flagphdr(e
, ELF_C_SET
, ELF_F_DIRTY
);