Print this page
3265 link-editor builds bogus .eh_frame_hdr on ia32
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/sgs/tools/common/leb128.c
+++ new/usr/src/cmd/sgs/tools/common/leb128.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 26
27 27 #include <stdio.h>
28 28 #include <dwarf.h>
29 29 #include <sys/types.h>
30 30 #include <sys/elf.h>
31 31
32 32 /*
33 33 * Little Endian Base 128 (LEB128) numbers.
34 34 * ----------------------------------------
35 35 *
36 36 * LEB128 is a scheme for encoding integers densely that exploits the
37 37 * assumption that most integers are small in magnitude. (This encoding
38 38 * is equally suitable whether the target machine architecture represents
39 39 * data in big-endian or little- endian
40 40 *
41 41 * Unsigned LEB128 numbers are encoded as follows: start at the low order
42 42 * end of an unsigned integer and chop it into 7-bit chunks. Place each
43 43 * chunk into the low order 7 bits of a byte. Typically, several of the
44 44 * high order bytes will be zero; discard them. Emit the remaining bytes in
45 45 * a stream, starting with the low order byte; set the high order bit on
46 46 * each byte except the last emitted byte. The high bit of zero on the last
47 47 * byte indicates to the decoder that it has encountered the last byte.
48 48 * The integer zero is a special case, consisting of a single zero byte.
49 49 *
50 50 * Signed, 2s complement LEB128 numbers are encoded in a similar except
51 51 * that the criterion for discarding high order bytes is not whether they
52 52 * are zero, but whether they consist entirely of sign extension bits.
53 53 * Consider the 32-bit integer -2. The three high level bytes of the number
54 54 * are sign extension, thus LEB128 would represent it as a single byte
55 55 * containing the low order 7 bits, with the high order bit cleared to
56 56 * indicate the end of the byte stream.
57 57 *
58 58 * Note that there is nothing within the LEB128 representation that
59 59 * indicates whether an encoded number is signed or unsigned. The decoder
60 60 * must know what type of number to expect.
61 61 *
62 62 * DWARF Exception Header Encoding
63 63 * -------------------------------
64 64 *
65 65 * The DWARF Exception Header Encoding is used to describe the type of data
66 66 * used in the .eh_frame_hdr section. The upper 4 bits indicate how the
67 67 * value is to be applied. The lower 4 bits indicate the format of the data.
68 68 *
69 69 * DWARF Exception Header value format
70 70 *
71 71 * Name Value Meaning
72 72 * DW_EH_PE_omit 0xff No value is present.
73 73 * DW_EH_PE_absptr 0x00 Value is a void*
74 74 * DW_EH_PE_uleb128 0x01 Unsigned value is encoded using the
75 75 * Little Endian Base 128 (LEB128)
76 76 * DW_EH_PE_udata2 0x02 A 2 bytes unsigned value.
77 77 * DW_EH_PE_udata4 0x03 A 4 bytes unsigned value.
78 78 * DW_EH_PE_udata8 0x04 An 8 bytes unsigned value.
79 79 * DW_EH_PE_signed 0x08 bit on for all signed encodings
80 80 * DW_EH_PE_sleb128 0x09 Signed value is encoded using the
81 81 * Little Endian Base 128 (LEB128)
82 82 * DW_EH_PE_sdata2 0x0A A 2 bytes signed value.
83 83 * DW_EH_PE_sdata4 0x0B A 4 bytes signed value.
84 84 * DW_EH_PE_sdata8 0x0C An 8 bytes signed value.
85 85 *
86 86 * DWARF Exception Header application
87 87 *
88 88 * Name Value Meaning
89 89 * DW_EH_PE_absptr 0x00 Value is used with no modification.
90 90 * DW_EH_PE_pcrel 0x10 Value is reletive to the location of itself
91 91 * DW_EH_PE_textrel 0x20
92 92 * DW_EH_PE_datarel 0x30 Value is reletive to the beginning of the
93 93 * eh_frame_hdr segment ( segment type
94 94 * PT_GNU_EH_FRAME )
95 95 * DW_EH_PE_funcrel 0x40
96 96 * DW_EH_PE_aligned 0x50 value is an aligned void*
97 97 * DW_EH_PE_indirect 0x80 bit to signal indirection after relocation
98 98 * DW_EH_PE_omit 0xff No value is present.
99 99 *
100 100 */
101 101
102 102 uint64_t
103 103 uleb_extract(unsigned char *data, uint64_t *dotp)
104 104 {
105 105 uint64_t dot = *dotp;
106 106 uint64_t res = 0;
107 107 int more = 1;
108 108 int shift = 0;
109 109 int val;
110 110
111 111 data += dot;
112 112
113 113 while (more) {
114 114 /*
115 115 * Pull off lower 7 bits
116 116 */
117 117 val = (*data) & 0x7f;
118 118
119 119 /*
120 120 * Add prepend value to head of number.
121 121 */
122 122 res = res | (val << shift);
123 123
124 124 /*
125 125 * Increment shift & dot pointer
126 126 */
127 127 shift += 7;
128 128 dot++;
129 129
130 130 /*
131 131 * Check to see if hi bit is set - if not, this
132 132 * is the last byte.
133 133 */
134 134 more = ((*data++) & 0x80) >> 7;
135 135 }
136 136 *dotp = dot;
137 137 return (res);
138 138 }
139 139
140 140 int64_t
141 141 sleb_extract(unsigned char *data, uint64_t *dotp)
142 142 {
143 143 uint64_t dot = *dotp;
144 144 int64_t res = 0;
145 145 int more = 1;
146 146 int shift = 0;
147 147 int val;
148 148
149 149 data += dot;
150 150
151 151 while (more) {
152 152 /*
153 153 * Pull off lower 7 bits
154 154 */
155 155 val = (*data) & 0x7f;
156 156
157 157 /*
158 158 * Add prepend value to head of number.
159 159 */
160 160 res = res | (val << shift);
161 161
162 162 /*
163 163 * Increment shift & dot pointer
164 164 */
165 165 shift += 7;
166 166 dot++;
167 167
168 168 /*
169 169 * Check to see if hi bit is set - if not, this
170 170 * is the last byte.
171 171 */
172 172 more = ((*data++) & 0x80) >> 7;
173 173 }
174 174 *dotp = dot;
175 175
176 176 /*
177 177 * Make sure value is properly sign extended.
178 178 */
179 179 res = (res << (64 - shift)) >> (64 - shift);
180 180
181 181 return (res);
182 182 }
183 183
184 184 /*
185 185 * Extract a DWARF encoded datum
186 186 *
187 187 * entry:
↓ open down ↓ |
187 lines elided |
↑ open up ↑ |
188 188 * data - Base of data buffer containing encoded bytes
189 189 * dotp - Address of variable containing index within data
190 190 * at which the desired datum starts.
191 191 * ehe_flags - DWARF encoding
192 192 * eident - ELF header e_ident[] array for object being processed
193 193 * sh_base - Base address of ELF section containing desired datum
194 194 * sh_offset - Offset relative to sh_base of desired datum.
195 195 */
196 196 uint64_t
197 197 dwarf_ehe_extract(unsigned char *data, uint64_t *dotp, uint_t ehe_flags,
198 - unsigned char *eident, uint64_t sh_base, uint64_t sh_offset)
198 + unsigned char *eident, boolean_t frame_hdr, uint64_t sh_base,
199 + uint64_t sh_offset, uint64_t dbase)
199 200 {
200 201 uint64_t dot = *dotp;
201 202 uint_t lsb;
202 203 uint_t wordsize;
203 204 uint_t fsize;
204 205 uint64_t result;
205 206
206 207 if (eident[EI_DATA] == ELFDATA2LSB)
207 208 lsb = 1;
208 209 else
209 210 lsb = 0;
210 211
211 212 if (eident[EI_CLASS] == ELFCLASS64)
212 213 wordsize = 8;
213 214 else
214 215 wordsize = 4;
215 216
216 217 switch (ehe_flags & 0x0f) {
217 218 case DW_EH_PE_omit:
218 219 return (0);
219 220 case DW_EH_PE_absptr:
220 221 fsize = wordsize;
221 222 break;
222 223 case DW_EH_PE_udata8:
223 224 case DW_EH_PE_sdata8:
224 225 fsize = 8;
225 226 break;
226 227 case DW_EH_PE_udata4:
227 228 case DW_EH_PE_sdata4:
228 229 fsize = 4;
229 230 break;
230 231 case DW_EH_PE_udata2:
231 232 case DW_EH_PE_sdata2:
232 233 fsize = 2;
233 234 break;
234 235 case DW_EH_PE_uleb128:
235 236 return (uleb_extract(data, dotp));
236 237 case DW_EH_PE_sleb128:
237 238 return ((uint64_t)sleb_extract(data, dotp));
238 239 default:
239 240 return (0);
240 241 }
241 242
242 243 if (lsb) {
243 244 /*
244 245 * Extract unaligned LSB formated data
245 246 */
246 247 uint_t cnt;
247 248
248 249 result = 0;
249 250 for (cnt = 0; cnt < fsize;
250 251 cnt++, dot++) {
251 252 uint64_t val;
252 253 val = data[dot];
253 254 result |= val << (cnt * 8);
254 255 }
255 256 } else {
256 257 /*
257 258 * Extract unaligned MSB formated data
258 259 */
259 260 uint_t cnt;
260 261 result = 0;
261 262 for (cnt = 0; cnt < fsize;
262 263 cnt++, dot++) {
263 264 uint64_t val;
264 265 val = data[dot];
265 266 result |= val << ((fsize - cnt - 1) * 8);
266 267 }
267 268 }
268 269 /*
269 270 * perform sign extension
270 271 */
271 272 if ((ehe_flags & DW_EH_PE_signed) &&
272 273 (fsize < sizeof (uint64_t))) {
273 274 int64_t sresult;
↓ open down ↓ |
65 lines elided |
↑ open up ↑ |
274 275 uint_t bitshift;
275 276 sresult = result;
276 277 bitshift = (sizeof (uint64_t) - fsize) * 8;
277 278 sresult = (sresult << bitshift) >> bitshift;
278 279 result = sresult;
279 280 }
280 281
281 282 /*
282 283 * If value is relative to a base address, adjust it
283 284 */
284 - if (result) {
285 - switch (ehe_flags & 0xf0) {
286 - case DW_EH_PE_pcrel:
287 - result += sh_base + sh_offset;
288 - break;
285 + switch (ehe_flags & 0xf0) {
286 + case DW_EH_PE_pcrel:
287 + result += sh_base + sh_offset;
288 + break;
289 289
290 - case DW_EH_PE_datarel:
290 + /*
291 + * datarel is relative to .eh_frame_hdr if within .eh_frame,
292 + * but GOT if not.
293 + */
294 + case DW_EH_PE_datarel:
295 + if (frame_hdr)
291 296 result += sh_base;
292 - break;
293 - }
297 + else
298 + result += dbase;
299 + break;
294 300 }
301 +
302 + /* Truncate the result to its specified size */
303 + result = (result << ((sizeof (uint64_t) - fsize) * 8)) >>
304 + ((sizeof (uint64_t) - fsize) * 8);
305 +
295 306 *dotp = dot;
296 307 return (result);
297 308 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX