1164190Sjkoshy/*-
2164190Sjkoshy * Copyright (c) 2006 Joseph Koshy
3164190Sjkoshy * All rights reserved.
4164190Sjkoshy *
5164190Sjkoshy * Redistribution and use in source and binary forms, with or without
6164190Sjkoshy * modification, are permitted provided that the following conditions
7164190Sjkoshy * are met:
8164190Sjkoshy * 1. Redistributions of source code must retain the above copyright
9164190Sjkoshy *    notice, this list of conditions and the following disclaimer.
10164190Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright
11164190Sjkoshy *    notice, this list of conditions and the following disclaimer in the
12164190Sjkoshy *    documentation and/or other materials provided with the distribution.
13164190Sjkoshy *
14164190Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15164190Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16164190Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17164190Sjkoshy * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18164190Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19164190Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20164190Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21164190Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22164190Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23164190Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24164190Sjkoshy * SUCH DAMAGE.
25164190Sjkoshy *
26164190Sjkoshy * $FreeBSD: releng/10.3/lib/libelf/libelf_phdr.c 210338 2010-07-21 10:25:02Z kaiw $
27164190Sjkoshy */
28164190Sjkoshy
29164190Sjkoshy#include <sys/cdefs.h>
30164190Sjkoshy__FBSDID("$FreeBSD: releng/10.3/lib/libelf/libelf_phdr.c 210338 2010-07-21 10:25:02Z kaiw $");
31164190Sjkoshy
32164190Sjkoshy#include <assert.h>
33164190Sjkoshy#include <gelf.h>
34164190Sjkoshy#include <libelf.h>
35164190Sjkoshy#include <stdlib.h>
36164190Sjkoshy
37164190Sjkoshy#include "_libelf.h"
38164190Sjkoshy
39164190Sjkoshyvoid *
40164190Sjkoshy_libelf_getphdr(Elf *e, int ec)
41164190Sjkoshy{
42164190Sjkoshy	size_t phnum, phentsize;
43164190Sjkoshy	size_t fsz, msz;
44164190Sjkoshy	uint64_t phoff;
45164190Sjkoshy	Elf32_Ehdr *eh32;
46164190Sjkoshy	Elf64_Ehdr *eh64;
47164190Sjkoshy	void *ehdr, *phdr;
48210338Skaiw	int (*xlator)(char *_d, size_t _dsz, char *_s, size_t _c, int _swap);
49164190Sjkoshy
50164190Sjkoshy	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
51164190Sjkoshy
52164190Sjkoshy	if (e == NULL) {
53164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
54164190Sjkoshy		return (NULL);
55164190Sjkoshy	}
56164190Sjkoshy
57164190Sjkoshy	if ((phdr = (ec == ELFCLASS32 ?
58164190Sjkoshy		 (void *) e->e_u.e_elf.e_phdr.e_phdr32 :
59164190Sjkoshy		 (void *) e->e_u.e_elf.e_phdr.e_phdr64)) != NULL)
60164190Sjkoshy		return (phdr);
61164190Sjkoshy
62164190Sjkoshy	/*
63164190Sjkoshy	 * Check the PHDR related fields in the EHDR for sanity.
64164190Sjkoshy	 */
65164190Sjkoshy
66164190Sjkoshy	if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
67164190Sjkoshy		return (NULL);
68164190Sjkoshy
69165535Sjkoshy	phnum = e->e_u.e_elf.e_nphdr;
70165535Sjkoshy
71164190Sjkoshy	if (ec == ELFCLASS32) {
72164190Sjkoshy		eh32      = (Elf32_Ehdr *) ehdr;
73164190Sjkoshy		phentsize = eh32->e_phentsize;
74164190Sjkoshy		phoff     = (uint64_t) eh32->e_phoff;
75164190Sjkoshy	} else {
76164190Sjkoshy		eh64      = (Elf64_Ehdr *) ehdr;
77164190Sjkoshy		phentsize = eh64->e_phentsize;
78164190Sjkoshy		phoff     = (uint64_t) eh64->e_phoff;
79164190Sjkoshy	}
80164190Sjkoshy
81164190Sjkoshy	fsz = gelf_fsize(e, ELF_T_PHDR, phnum, e->e_version);
82164190Sjkoshy
83164190Sjkoshy	assert(fsz > 0);
84164190Sjkoshy
85164190Sjkoshy	if ((uint64_t) e->e_rawsize < (phoff + fsz)) {
86164190Sjkoshy		LIBELF_SET_ERROR(HEADER, 0);
87164190Sjkoshy		return (NULL);
88164190Sjkoshy	}
89164190Sjkoshy
90164190Sjkoshy	msz = _libelf_msize(ELF_T_PHDR, ec, EV_CURRENT);
91164190Sjkoshy
92164190Sjkoshy	assert(msz > 0);
93164190Sjkoshy
94164190Sjkoshy	if ((phdr = calloc(phnum, msz)) == NULL) {
95164190Sjkoshy		LIBELF_SET_ERROR(RESOURCE, 0);
96164190Sjkoshy		return (NULL);
97164190Sjkoshy	}
98164190Sjkoshy
99164190Sjkoshy	if (ec == ELFCLASS32)
100164190Sjkoshy		e->e_u.e_elf.e_phdr.e_phdr32 = phdr;
101164190Sjkoshy	else
102164190Sjkoshy		e->e_u.e_elf.e_phdr.e_phdr64 = phdr;
103164190Sjkoshy
104164190Sjkoshy
105164190Sjkoshy	xlator = _libelf_get_translator(ELF_T_PHDR, ELF_TOMEMORY, ec);
106210338Skaiw	(*xlator)(phdr, phnum * msz, e->e_rawfile + phoff, phnum,
107164190Sjkoshy	    e->e_byteorder != LIBELF_PRIVATE(byteorder));
108164190Sjkoshy
109164190Sjkoshy	return (phdr);
110164190Sjkoshy}
111164190Sjkoshy
112164190Sjkoshyvoid *
113164190Sjkoshy_libelf_newphdr(Elf *e, int ec, size_t count)
114164190Sjkoshy{
115165535Sjkoshy	void *ehdr, *newphdr, *oldphdr;
116164190Sjkoshy	size_t msz;
117164190Sjkoshy
118164190Sjkoshy	if (e == NULL) {
119164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
120164190Sjkoshy		return (NULL);
121164190Sjkoshy	}
122164190Sjkoshy
123164190Sjkoshy	if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) {
124164190Sjkoshy		LIBELF_SET_ERROR(SEQUENCE, 0);
125164190Sjkoshy		return (NULL);
126164190Sjkoshy	}
127164190Sjkoshy
128164190Sjkoshy	assert(e->e_class == ec);
129164190Sjkoshy	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
130164190Sjkoshy	assert(e->e_version == EV_CURRENT);
131164190Sjkoshy
132164190Sjkoshy	msz = _libelf_msize(ELF_T_PHDR, ec, e->e_version);
133164190Sjkoshy
134164190Sjkoshy	assert(msz > 0);
135164190Sjkoshy
136165535Sjkoshy	newphdr = NULL;
137165535Sjkoshy	if (count > 0 && (newphdr = calloc(count, msz)) == NULL) {
138164190Sjkoshy		LIBELF_SET_ERROR(RESOURCE, 0);
139164190Sjkoshy		return (NULL);
140164190Sjkoshy	}
141164190Sjkoshy
142164190Sjkoshy	if (ec == ELFCLASS32) {
143165535Sjkoshy		if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr32) != NULL)
144165535Sjkoshy			free(oldphdr);
145165535Sjkoshy		e->e_u.e_elf.e_phdr.e_phdr32 = (Elf32_Phdr *) newphdr;
146164190Sjkoshy	} else {
147165535Sjkoshy		if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr64) != NULL)
148165535Sjkoshy			free(oldphdr);
149165535Sjkoshy		e->e_u.e_elf.e_phdr.e_phdr64 = (Elf64_Phdr *) newphdr;
150164190Sjkoshy	}
151164190Sjkoshy
152165535Sjkoshy	e->e_u.e_elf.e_nphdr = count;
153164190Sjkoshy
154164190Sjkoshy	elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY);
155164190Sjkoshy
156165535Sjkoshy	return (newphdr);
157164190Sjkoshy}
158