xref: /freebsd/contrib/elftoolchain/libelf/libelf_phdr.c (revision b9f654b163bce26de79705e77b872427c9f2afa1)
1 /*-
2  * Copyright (c) 2006,2008 Joseph Koshy
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 #include <assert.h>
28 #include <gelf.h>
29 #include <libelf.h>
30 #include <stdlib.h>
31 
32 #include "_libelf.h"
33 
34 ELFTC_VCSID("$Id: libelf_phdr.c 3632 2018-10-10 21:12:43Z jkoshy $");
35 
36 void *
37 _libelf_getphdr(Elf *e, int ec)
38 {
39 	size_t phnum;
40 	size_t fsz, msz;
41 	uint64_t phoff;
42 	Elf32_Ehdr *eh32;
43 	Elf64_Ehdr *eh64;
44 	void *ehdr, *phdr;
45 	_libelf_translator_function *xlator;
46 
47 	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
48 
49 	if (e == NULL) {
50 		LIBELF_SET_ERROR(ARGUMENT, 0);
51 		return (NULL);
52 	}
53 
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)
57 		return (phdr);
58 
59 	/*
60 	 * Check the PHDR related fields in the EHDR for sanity.
61 	 */
62 
63 	if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
64 		return (NULL);
65 
66 	phnum = e->e_u.e_elf.e_nphdr;
67 
68 	if (ec == ELFCLASS32) {
69 		eh32      = (Elf32_Ehdr *) ehdr;
70 		phoff     = (uint64_t) eh32->e_phoff;
71 	} else {
72 		eh64      = (Elf64_Ehdr *) ehdr;
73 		phoff     = (uint64_t) eh64->e_phoff;
74 	}
75 
76 	fsz = gelf_fsize(e, ELF_T_PHDR, phnum, e->e_version);
77 
78 	assert(fsz > 0);
79 
80 	if ((uint64_t) e->e_rawsize < (phoff + fsz)) {
81 		LIBELF_SET_ERROR(HEADER, 0);
82 		return (NULL);
83 	}
84 
85 	msz = _libelf_msize(ELF_T_PHDR, ec, EV_CURRENT);
86 
87 	assert(msz > 0);
88 
89 	if ((phdr = calloc(phnum, msz)) == NULL) {
90 		LIBELF_SET_ERROR(RESOURCE, 0);
91 		return (NULL);
92 	}
93 
94 	if (ec == ELFCLASS32)
95 		e->e_u.e_elf.e_phdr.e_phdr32 = phdr;
96 	else
97 		e->e_u.e_elf.e_phdr.e_phdr64 = phdr;
98 
99 
100 	xlator = _libelf_get_translator(ELF_T_PHDR, ELF_TOMEMORY, ec,
101 	    _libelf_elfmachine(e));
102 	(*xlator)(phdr, phnum * msz, e->e_rawfile + phoff, phnum,
103 	    e->e_byteorder != LIBELF_PRIVATE(byteorder));
104 
105 	return (phdr);
106 }
107 
108 void *
109 _libelf_newphdr(Elf *e, int ec, size_t count)
110 {
111 	void *ehdr, *newphdr, *oldphdr;
112 	size_t msz;
113 
114 	if (e == NULL) {
115 		LIBELF_SET_ERROR(ARGUMENT, 0);
116 		return (NULL);
117 	}
118 
119 	if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) {
120 		LIBELF_SET_ERROR(SEQUENCE, 0);
121 		return (NULL);
122 	}
123 
124 	assert(e->e_class == ec);
125 	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
126 	assert(e->e_version == EV_CURRENT);
127 
128 	msz = _libelf_msize(ELF_T_PHDR, ec, e->e_version);
129 
130 	assert(msz > 0);
131 
132 	newphdr = NULL;
133 	if (count > 0 && (newphdr = calloc(count, msz)) == NULL) {
134 		LIBELF_SET_ERROR(RESOURCE, 0);
135 		return (NULL);
136 	}
137 
138 	if (ec == ELFCLASS32) {
139 		if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr32) != NULL)
140 			free(oldphdr);
141 		e->e_u.e_elf.e_phdr.e_phdr32 = (Elf32_Phdr *) newphdr;
142 	} else {
143 		if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr64) != NULL)
144 			free(oldphdr);
145 		e->e_u.e_elf.e_phdr.e_phdr64 = (Elf64_Phdr *) newphdr;
146 	}
147 
148 	e->e_u.e_elf.e_nphdr = count;
149 
150 	elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY);
151 
152 	return (newphdr);
153 }
154