libelf_phdr.c revision 260684
156545Smarkm/*-
256545Smarkm * Copyright (c) 2006,2008 Joseph Koshy
3125491Sru * All rights reserved.
4125491Sru *
5117728Smarkm * Redistribution and use in source and binary forms, with or without
6125491Sru * modification, are permitted provided that the following conditions
7125491Sru * are met:
8125491Sru * 1. Redistributions of source code must retain the above copyright
9233294Sstas *    notice, this list of conditions and the following disclaimer.
10125491Sru * 2. Redistributions in binary form must reproduce the above copyright
11125491Sru *    notice, this list of conditions and the following disclaimer in the
12178828Sdfr *    documentation and/or other materials provided with the distribution.
13178828Sdfr *
14125491Sru * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15125491Sru * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16125491Sru * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17125491Sru * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1872450Sassar * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19178828Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24117728Smarkm * SUCH DAMAGE.
2556545Smarkm */
2656545Smarkm
27236337Sobrien#include <sys/cdefs.h>
28236422Sobrien
29178828Sdfr#include <assert.h>
30236337Sobrien#include <gelf.h>
31236337Sobrien#include <libelf.h>
32236337Sobrien#include <stdlib.h>
33236337Sobrien
34125491Sru#include "_libelf.h"
35
36ELFTC_VCSID("$Id: libelf_phdr.c 2931 2013-03-23 11:41:07Z jkoshy $");
37
38void *
39_libelf_getphdr(Elf *e, int ec)
40{
41	size_t phnum;
42	size_t fsz, msz;
43	uint64_t phoff;
44	Elf32_Ehdr *eh32;
45	Elf64_Ehdr *eh64;
46	void *ehdr, *phdr;
47	int (*xlator)(char *_d, size_t _dsz, char *_s, size_t _c, int _swap);
48
49	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
50
51	if (e == NULL) {
52		LIBELF_SET_ERROR(ARGUMENT, 0);
53		return (NULL);
54	}
55
56	if ((phdr = (ec == ELFCLASS32 ?
57		 (void *) e->e_u.e_elf.e_phdr.e_phdr32 :
58		 (void *) e->e_u.e_elf.e_phdr.e_phdr64)) != NULL)
59		return (phdr);
60
61	/*
62	 * Check the PHDR related fields in the EHDR for sanity.
63	 */
64
65	if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
66		return (NULL);
67
68	phnum = e->e_u.e_elf.e_nphdr;
69
70	if (ec == ELFCLASS32) {
71		eh32      = (Elf32_Ehdr *) ehdr;
72		phoff     = (uint64_t) eh32->e_phoff;
73	} else {
74		eh64      = (Elf64_Ehdr *) ehdr;
75		phoff     = (uint64_t) eh64->e_phoff;
76	}
77
78	fsz = gelf_fsize(e, ELF_T_PHDR, phnum, e->e_version);
79
80	assert(fsz > 0);
81
82	if ((uint64_t) e->e_rawsize < (phoff + fsz)) {
83		LIBELF_SET_ERROR(HEADER, 0);
84		return (NULL);
85	}
86
87	msz = _libelf_msize(ELF_T_PHDR, ec, EV_CURRENT);
88
89	assert(msz > 0);
90
91	if ((phdr = calloc(phnum, msz)) == NULL) {
92		LIBELF_SET_ERROR(RESOURCE, 0);
93		return (NULL);
94	}
95
96	if (ec == ELFCLASS32)
97		e->e_u.e_elf.e_phdr.e_phdr32 = phdr;
98	else
99		e->e_u.e_elf.e_phdr.e_phdr64 = phdr;
100
101
102	xlator = _libelf_get_translator(ELF_T_PHDR, ELF_TOMEMORY, ec);
103	(*xlator)(phdr, phnum * msz, e->e_rawfile + phoff, phnum,
104	    e->e_byteorder != LIBELF_PRIVATE(byteorder));
105
106	return (phdr);
107}
108
109void *
110_libelf_newphdr(Elf *e, int ec, size_t count)
111{
112	void *ehdr, *newphdr, *oldphdr;
113	size_t msz;
114
115	if (e == NULL) {
116		LIBELF_SET_ERROR(ARGUMENT, 0);
117		return (NULL);
118	}
119
120	if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) {
121		LIBELF_SET_ERROR(SEQUENCE, 0);
122		return (NULL);
123	}
124
125	assert(e->e_class == ec);
126	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
127	assert(e->e_version == EV_CURRENT);
128
129	msz = _libelf_msize(ELF_T_PHDR, ec, e->e_version);
130
131	assert(msz > 0);
132
133	newphdr = NULL;
134	if (count > 0 && (newphdr = calloc(count, msz)) == NULL) {
135		LIBELF_SET_ERROR(RESOURCE, 0);
136		return (NULL);
137	}
138
139	if (ec == ELFCLASS32) {
140		if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr32) != NULL)
141			free(oldphdr);
142		e->e_u.e_elf.e_phdr.e_phdr32 = (Elf32_Phdr *) newphdr;
143	} else {
144		if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr64) != NULL)
145			free(oldphdr);
146		e->e_u.e_elf.e_phdr.e_phdr64 = (Elf64_Phdr *) newphdr;
147	}
148
149	e->e_u.e_elf.e_nphdr = count;
150
151	elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY);
152
153	return (newphdr);
154}
155