]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - lib/libelf/libelf_xlate.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / lib / libelf / libelf_xlate.c
1 /*-
2  * Copyright (c) 2006 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 <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <assert.h>
31 #include <libelf.h>
32
33 #include "_libelf.h"
34
35 /*
36  * Translate to/from the file representation of ELF objects.
37  *
38  * Translation could potentially involve the following
39  * transformations:
40  *
41  * - an endianness conversion,
42  * - a change of layout, as the file representation of ELF objects
43  *   can differ from their in-memory representation.
44  * - a change in representation due to a layout version change.
45  */
46
47 Elf_Data *
48 _libelf_xlate(Elf_Data *dst, const Elf_Data *src, unsigned int encoding,
49     int elfclass, int direction)
50 {
51         int byteswap;
52         size_t cnt, dsz, fsz, msz;
53         uintptr_t sb, se, db, de;
54
55         if (encoding == ELFDATANONE)
56                 encoding = LIBELF_PRIVATE(byteorder);
57
58         if ((encoding != ELFDATA2LSB && encoding != ELFDATA2MSB) ||
59             dst == NULL || src == NULL || dst == src)   {
60                 LIBELF_SET_ERROR(ARGUMENT, 0);
61                 return (NULL);
62         }
63
64         assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64);
65         assert(direction == ELF_TOFILE || direction == ELF_TOMEMORY);
66
67         if (dst->d_version != src->d_version) {
68                 LIBELF_SET_ERROR(UNIMPL, 0);
69                 return (NULL);
70         }
71
72         if  (src->d_buf == NULL || dst->d_buf == NULL) {
73                 LIBELF_SET_ERROR(DATA, 0);
74                 return (NULL);
75         }
76
77         if ((int) src->d_type < 0 || src->d_type >= ELF_T_NUM) {
78                 LIBELF_SET_ERROR(DATA, 0);
79                 return (NULL);
80         }
81
82         if ((fsz = (elfclass == ELFCLASS32 ? elf32_fsize : elf64_fsize)
83             (src->d_type, (size_t) 1, src->d_version)) == 0)
84                 return (NULL);
85
86         msz = _libelf_msize(src->d_type, elfclass, src->d_version);
87
88         assert(msz > 0);
89
90         if (src->d_size % (direction == ELF_TOMEMORY ? fsz : msz)) {
91                 LIBELF_SET_ERROR(DATA, 0);
92                 return (NULL);
93         }
94
95         /*
96          * Determine the number of objects that need to be converted, and
97          * the space required for the converted objects in the destination
98          * buffer.
99          */
100         if (direction == ELF_TOMEMORY) {
101                 cnt = src->d_size / fsz;
102                 dsz = cnt * msz;
103         } else {
104                 cnt = src->d_size / msz;
105                 dsz = cnt * fsz;
106         }
107
108         if (dst->d_size  <  dsz) {
109                 LIBELF_SET_ERROR(DATA, 0);
110                 return (NULL);
111         }
112
113         sb = (uintptr_t) src->d_buf;
114         se = sb + src->d_size;
115         db = (uintptr_t) dst->d_buf;
116         de = db + dst->d_size;
117
118         /*
119          * Check for overlapping buffers.  Note that db == sb is
120          * allowed.
121          */
122         if (db != sb && de > sb && se > db) {
123                 LIBELF_SET_ERROR(DATA, 0);
124                 return (NULL);
125         }
126
127         if ((direction == ELF_TOMEMORY ? db : sb) %
128             _libelf_malign(src->d_type, elfclass)) {
129                 LIBELF_SET_ERROR(DATA, 0);
130                 return (NULL);
131         }
132
133         dst->d_type = src->d_type;
134         dst->d_size = dsz;
135
136         byteswap = encoding != LIBELF_PRIVATE(byteorder);
137
138         if (src->d_size == 0 ||
139             (db == sb && !byteswap && fsz == msz))
140                 return (dst);   /* nothing more to do */
141
142         if (!(_libelf_get_translator(src->d_type, direction, elfclass))
143             (dst->d_buf, dsz, src->d_buf, cnt, byteswap)) {
144                 LIBELF_SET_ERROR(DATA, 0);
145                 return (NULL);
146         }
147
148         return (dst);
149 }