xref: /netbsd-src/external/bsd/elftoolchain/dist/libelf/libelf_xlate.c (revision 5ac3bc719ce6e70593039505b491894133237d12)
1 /*	$NetBSD: libelf_xlate.c,v 1.5 2024/03/03 17:37:34 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006,2008 Joseph Koshy
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 #if HAVE_NBTOOL_CONFIG_H
30 # include "nbtool_config.h"
31 #endif
32 
33 #include <sys/cdefs.h>
34 
35 #include <assert.h>
36 #include <libelf.h>
37 
38 #include "_libelf.h"
39 
40 __RCSID("$NetBSD: libelf_xlate.c,v 1.5 2024/03/03 17:37:34 christos Exp $");
41 ELFTC_VCSID("Id: libelf_xlate.c 3977 2022-05-01 06:45:34Z jkoshy");
42 
43 /*
44  * Translate to/from the file representation of ELF objects.
45  *
46  * Translation could potentially involve the following
47  * transformations:
48  *
49  * - an endianness conversion,
50  * - a change of layout, as the file representation of ELF objects
51  *   can differ from their in-memory representation.
52  * - a change in representation due to a layout version change.
53  */
54 
55 Elf_Data *
_libelf_xlate(Elf_Data * dst,const Elf_Data * src,unsigned int encoding,int elfclass,int elfmachine,int direction)56 _libelf_xlate(Elf_Data *dst, const Elf_Data *src, unsigned int encoding,
57     int elfclass, int elfmachine, int direction)
58 {
59 	int byteswap;
60 	size_t cnt, dsz, fsz, msz;
61 	uintptr_t sb, se, db, de;
62 	_libelf_translator_function *xlator;
63 
64 	if (encoding == ELFDATANONE)
65 		encoding = LIBELF_PRIVATE(byteorder);
66 
67 	if ((encoding != ELFDATA2LSB && encoding != ELFDATA2MSB) ||
68 	    dst == NULL || src == NULL || dst == src)	{
69 		LIBELF_SET_ERROR(ARGUMENT, 0);
70 		return (NULL);
71 	}
72 
73 	assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64);
74 	assert(direction == ELF_TOFILE || direction == ELF_TOMEMORY);
75 
76 	if (dst->d_version != src->d_version) {
77 		LIBELF_SET_ERROR(UNIMPL, 0);
78 		return (NULL);
79 	}
80 
81 	if  (src->d_buf == NULL || dst->d_buf == NULL) {
82 		LIBELF_SET_ERROR(DATA, 0);
83 		return (NULL);
84 	}
85 
86 	if ((int) src->d_type < 0 || src->d_type >= ELF_T_NUM) {
87 		LIBELF_SET_ERROR(DATA, 0);
88 		return (NULL);
89 	}
90 
91 	if ((fsz = (elfclass == ELFCLASS32 ? elf32_fsize : elf64_fsize)
92 	    (src->d_type, (size_t) 1, src->d_version)) == 0)
93 		return (NULL);
94 
95 	if ((msz = _libelf_msize(src->d_type, elfclass, src->d_version)) == 0)
96 		return (NULL);
97 
98 	if (src->d_size % (direction == ELF_TOMEMORY ? fsz : msz)) {
99 		LIBELF_SET_ERROR(DATA, 0);
100 		return (NULL);
101 	}
102 
103 	/*
104 	 * Determine the number of objects that need to be converted, and
105 	 * the space required for the converted objects in the destination
106 	 * buffer.
107 	 */
108 	if (direction == ELF_TOMEMORY) {
109 		cnt = (size_t) src->d_size / fsz;
110 		dsz = cnt * msz;
111 	} else {
112 		cnt = (size_t) src->d_size / msz;
113 		dsz = cnt * fsz;
114 	}
115 
116 	if (dst->d_size  <  dsz) {
117 		LIBELF_SET_ERROR(DATA, 0);
118 		return (NULL);
119 	}
120 
121 	sb = (uintptr_t) src->d_buf;
122 	se = sb + (size_t) src->d_size;
123 	db = (uintptr_t) dst->d_buf;
124 	de = db + (size_t) dst->d_size;
125 
126 	/*
127 	 * Check for overlapping buffers.  Note that db == sb is
128 	 * allowed.
129 	 */
130 	if (db != sb && de > sb && se > db) {
131 		LIBELF_SET_ERROR(DATA, 0);
132 		return (NULL);
133 	}
134 
135 	if ((direction == ELF_TOMEMORY ? db : sb) %
136 	    _libelf_malign(src->d_type, elfclass)) {
137 		LIBELF_SET_ERROR(DATA, 0);
138 		return (NULL);
139 	}
140 
141 	dst->d_type = src->d_type;
142 	dst->d_size = dsz;
143 
144 	byteswap = encoding != LIBELF_PRIVATE(byteorder);
145 
146 	if (src->d_size == 0 ||
147 	    (db == sb && !byteswap && fsz == msz))
148 		return (dst);	/* nothing more to do */
149 
150 	xlator = _libelf_get_translator(src->d_type, direction, elfclass,
151 	    elfmachine);
152 	if (!xlator(dst->d_buf, dsz, src->d_buf, cnt, byteswap)) {
153 		LIBELF_SET_ERROR(DATA, 0);
154 		return (NULL);
155 	}
156 
157 	return (dst);
158 }
159