1d7e90bca34e14c627d7421b7ddd853906da5b4c
2 * Copyright (c) 2006 Joseph Koshy
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/lib/libelf/libelf_phdr.c,v 1.2 2006/12/25 02:22:22 jkoshy Exp $
37 _libelf_getphdr(Elf
*e
, int ec
)
39 size_t phnum
, phentsize
;
45 void (*xlator
)(char *_d
, char *_s
, size_t _c
, int _swap
);
47 assert(ec
== ELFCLASS32
|| ec
== ELFCLASS64
);
50 LIBELF_SET_ERROR(ARGUMENT
, 0);
54 if ((phdr
= (ec
== ELFCLASS32
?
55 (void *) e
->e_u
.e_elf
.e_phdr
.e_phdr32
:
56 (void *) e
->e_u
.e_elf
.e_phdr
.e_phdr64
)) != NULL
)
60 * Check the PHDR related fields in the EHDR for sanity.
63 if ((ehdr
= _libelf_ehdr(e
, ec
, 0)) == NULL
)
66 phnum
= e
->e_u
.e_elf
.e_nphdr
;
68 if (ec
== ELFCLASS32
) {
69 eh32
= (Elf32_Ehdr
*) ehdr
;
70 phentsize
= eh32
->e_phentsize
;
71 phoff
= (uint64_t) eh32
->e_phoff
;
73 eh64
= (Elf64_Ehdr
*) ehdr
;
74 phentsize
= eh64
->e_phentsize
;
75 phoff
= (uint64_t) eh64
->e_phoff
;
78 fsz
= gelf_fsize(e
, ELF_T_PHDR
, phnum
, e
->e_version
);
82 if ((uint64_t) e
->e_rawsize
< (phoff
+ fsz
)) {
83 LIBELF_SET_ERROR(HEADER
, 0);
87 msz
= _libelf_msize(ELF_T_PHDR
, ec
, EV_CURRENT
);
91 if ((phdr
= calloc(phnum
, msz
)) == NULL
) {
92 LIBELF_SET_ERROR(RESOURCE
, 0);
97 e
->e_u
.e_elf
.e_phdr
.e_phdr32
= phdr
;
99 e
->e_u
.e_elf
.e_phdr
.e_phdr64
= phdr
;
102 xlator
= _libelf_get_translator(ELF_T_PHDR
, ELF_TOMEMORY
, ec
);
103 (*xlator
)(phdr
, e
->e_rawfile
+ phoff
, phnum
,
104 e
->e_byteorder
!= LIBELF_PRIVATE(byteorder
));
110 _libelf_newphdr(Elf
*e
, int ec
, size_t count
)
112 void *ehdr
, *newphdr
, *oldphdr
;
116 LIBELF_SET_ERROR(ARGUMENT
, 0);
120 if ((ehdr
= _libelf_ehdr(e
, ec
, 0)) == NULL
) {
121 LIBELF_SET_ERROR(SEQUENCE
, 0);
125 assert(e
->e_class
== ec
);
126 assert(ec
== ELFCLASS32
|| ec
== ELFCLASS64
);
127 assert(e
->e_version
== EV_CURRENT
);
129 msz
= _libelf_msize(ELF_T_PHDR
, ec
, e
->e_version
);
134 if (count
> 0 && (newphdr
= calloc(count
, msz
)) == NULL
) {
135 LIBELF_SET_ERROR(RESOURCE
, 0);
139 if (ec
== ELFCLASS32
) {
140 if ((oldphdr
= (void *) e
->e_u
.e_elf
.e_phdr
.e_phdr32
) != NULL
)
142 e
->e_u
.e_elf
.e_phdr
.e_phdr32
= (Elf32_Phdr
*) newphdr
;
144 if ((oldphdr
= (void *) e
->e_u
.e_elf
.e_phdr
.e_phdr64
) != NULL
)
146 e
->e_u
.e_elf
.e_phdr
.e_phdr64
= (Elf64_Phdr
*) newphdr
;
149 e
->e_u
.e_elf
.e_nphdr
= count
;
151 elf_flagphdr(e
, ELF_C_SET
, ELF_F_DIRTY
);