10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51618Srie * Common Development and Distribution License (the "License").
61618Srie * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
211618Srie
220Sstevel@tonic-gate /*
23*9085SAli.Bahrami@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
261618Srie
270Sstevel@tonic-gate #include <stdio.h>
280Sstevel@tonic-gate #include <dwarf.h>
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/elf.h>
310Sstevel@tonic-gate
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate * Little Endian Base 128 (LEB128) numbers.
340Sstevel@tonic-gate * ----------------------------------------
350Sstevel@tonic-gate *
360Sstevel@tonic-gate * LEB128 is a scheme for encoding integers densely that exploits the
370Sstevel@tonic-gate * assumption that most integers are small in magnitude. (This encoding
380Sstevel@tonic-gate * is equally suitable whether the target machine architecture represents
390Sstevel@tonic-gate * data in big-endian or little- endian
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * Unsigned LEB128 numbers are encoded as follows: start at the low order
420Sstevel@tonic-gate * end of an unsigned integer and chop it into 7-bit chunks. Place each
430Sstevel@tonic-gate * chunk into the low order 7 bits of a byte. Typically, several of the
440Sstevel@tonic-gate * high order bytes will be zero; discard them. Emit the remaining bytes in
450Sstevel@tonic-gate * a stream, starting with the low order byte; set the high order bit on
460Sstevel@tonic-gate * each byte except the last emitted byte. The high bit of zero on the last
470Sstevel@tonic-gate * byte indicates to the decoder that it has encountered the last byte.
480Sstevel@tonic-gate * The integer zero is a special case, consisting of a single zero byte.
490Sstevel@tonic-gate *
500Sstevel@tonic-gate * Signed, 2s complement LEB128 numbers are encoded in a similar except
510Sstevel@tonic-gate * that the criterion for discarding high order bytes is not whether they
520Sstevel@tonic-gate * are zero, but whether they consist entirely of sign extension bits.
530Sstevel@tonic-gate * Consider the 32-bit integer -2. The three high level bytes of the number
540Sstevel@tonic-gate * are sign extension, thus LEB128 would represent it as a single byte
550Sstevel@tonic-gate * containing the low order 7 bits, with the high order bit cleared to
560Sstevel@tonic-gate * indicate the end of the byte stream.
570Sstevel@tonic-gate *
580Sstevel@tonic-gate * Note that there is nothing within the LEB128 representation that
590Sstevel@tonic-gate * indicates whether an encoded number is signed or unsigned. The decoder
600Sstevel@tonic-gate * must know what type of number to expect.
610Sstevel@tonic-gate *
620Sstevel@tonic-gate * DWARF Exception Header Encoding
630Sstevel@tonic-gate * -------------------------------
640Sstevel@tonic-gate *
650Sstevel@tonic-gate * The DWARF Exception Header Encoding is used to describe the type of data
660Sstevel@tonic-gate * used in the .eh_frame_hdr section. The upper 4 bits indicate how the
670Sstevel@tonic-gate * value is to be applied. The lower 4 bits indicate the format of the data.
680Sstevel@tonic-gate *
690Sstevel@tonic-gate * DWARF Exception Header value format
700Sstevel@tonic-gate *
710Sstevel@tonic-gate * Name Value Meaning
720Sstevel@tonic-gate * DW_EH_PE_omit 0xff No value is present.
730Sstevel@tonic-gate * DW_EH_PE_absptr 0x00 Value is a void*
740Sstevel@tonic-gate * DW_EH_PE_uleb128 0x01 Unsigned value is encoded using the
750Sstevel@tonic-gate * Little Endian Base 128 (LEB128)
760Sstevel@tonic-gate * DW_EH_PE_udata2 0x02 A 2 bytes unsigned value.
770Sstevel@tonic-gate * DW_EH_PE_udata4 0x03 A 4 bytes unsigned value.
780Sstevel@tonic-gate * DW_EH_PE_udata8 0x04 An 8 bytes unsigned value.
790Sstevel@tonic-gate * DW_EH_PE_signed 0x08 bit on for all signed encodings
800Sstevel@tonic-gate * DW_EH_PE_sleb128 0x09 Signed value is encoded using the
810Sstevel@tonic-gate * Little Endian Base 128 (LEB128)
820Sstevel@tonic-gate * DW_EH_PE_sdata2 0x0A A 2 bytes signed value.
830Sstevel@tonic-gate * DW_EH_PE_sdata4 0x0B A 4 bytes signed value.
840Sstevel@tonic-gate * DW_EH_PE_sdata8 0x0C An 8 bytes signed value.
850Sstevel@tonic-gate *
860Sstevel@tonic-gate * DWARF Exception Header application
870Sstevel@tonic-gate *
880Sstevel@tonic-gate * Name Value Meaning
890Sstevel@tonic-gate * DW_EH_PE_absptr 0x00 Value is used with no modification.
900Sstevel@tonic-gate * DW_EH_PE_pcrel 0x10 Value is reletive to the location of itself
910Sstevel@tonic-gate * DW_EH_PE_textrel 0x20
920Sstevel@tonic-gate * DW_EH_PE_datarel 0x30 Value is reletive to the beginning of the
930Sstevel@tonic-gate * eh_frame_hdr segment ( segment type
940Sstevel@tonic-gate * PT_GNU_EH_FRAME )
950Sstevel@tonic-gate * DW_EH_PE_funcrel 0x40
960Sstevel@tonic-gate * DW_EH_PE_aligned 0x50 value is an aligned void*
970Sstevel@tonic-gate * DW_EH_PE_indirect 0x80 bit to signal indirection after relocation
980Sstevel@tonic-gate * DW_EH_PE_omit 0xff No value is present.
990Sstevel@tonic-gate *
1000Sstevel@tonic-gate */
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate uint64_t
uleb_extract(unsigned char * data,uint64_t * dotp)1030Sstevel@tonic-gate uleb_extract(unsigned char *data, uint64_t *dotp)
1040Sstevel@tonic-gate {
1050Sstevel@tonic-gate uint64_t dot = *dotp;
1060Sstevel@tonic-gate uint64_t res = 0;
1070Sstevel@tonic-gate int more = 1;
1080Sstevel@tonic-gate int shift = 0;
1090Sstevel@tonic-gate int val;
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate data += dot;
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate while (more) {
1140Sstevel@tonic-gate /*
1150Sstevel@tonic-gate * Pull off lower 7 bits
1160Sstevel@tonic-gate */
1170Sstevel@tonic-gate val = (*data) & 0x7f;
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate /*
1200Sstevel@tonic-gate * Add prepend value to head of number.
1210Sstevel@tonic-gate */
1220Sstevel@tonic-gate res = res | (val << shift);
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate /*
1250Sstevel@tonic-gate * Increment shift & dot pointer
1260Sstevel@tonic-gate */
1270Sstevel@tonic-gate shift += 7;
1280Sstevel@tonic-gate dot++;
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate /*
1310Sstevel@tonic-gate * Check to see if hi bit is set - if not, this
1320Sstevel@tonic-gate * is the last byte.
1330Sstevel@tonic-gate */
1340Sstevel@tonic-gate more = ((*data++) & 0x80) >> 7;
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate *dotp = dot;
1370Sstevel@tonic-gate return (res);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate int64_t
sleb_extract(unsigned char * data,uint64_t * dotp)1410Sstevel@tonic-gate sleb_extract(unsigned char *data, uint64_t *dotp)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate uint64_t dot = *dotp;
1440Sstevel@tonic-gate int64_t res = 0;
1450Sstevel@tonic-gate int more = 1;
1460Sstevel@tonic-gate int shift = 0;
1470Sstevel@tonic-gate int val;
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate data += dot;
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate while (more) {
1520Sstevel@tonic-gate /*
1530Sstevel@tonic-gate * Pull off lower 7 bits
1540Sstevel@tonic-gate */
1550Sstevel@tonic-gate val = (*data) & 0x7f;
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate /*
1580Sstevel@tonic-gate * Add prepend value to head of number.
1590Sstevel@tonic-gate */
1600Sstevel@tonic-gate res = res | (val << shift);
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate /*
1630Sstevel@tonic-gate * Increment shift & dot pointer
1640Sstevel@tonic-gate */
1650Sstevel@tonic-gate shift += 7;
1660Sstevel@tonic-gate dot++;
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate /*
1690Sstevel@tonic-gate * Check to see if hi bit is set - if not, this
1700Sstevel@tonic-gate * is the last byte.
1710Sstevel@tonic-gate */
1720Sstevel@tonic-gate more = ((*data++) & 0x80) >> 7;
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate *dotp = dot;
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate /*
1770Sstevel@tonic-gate * Make sure value is properly sign extended.
1780Sstevel@tonic-gate */
1790Sstevel@tonic-gate res = (res << (64 - shift)) >> (64 - shift);
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate return (res);
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate
184*9085SAli.Bahrami@Sun.COM /*
185*9085SAli.Bahrami@Sun.COM * Extract a DWARF encoded datum
186*9085SAli.Bahrami@Sun.COM *
187*9085SAli.Bahrami@Sun.COM * entry:
188*9085SAli.Bahrami@Sun.COM * data - Base of data buffer containing encoded bytes
189*9085SAli.Bahrami@Sun.COM * dotp - Address of variable containing index within data
190*9085SAli.Bahrami@Sun.COM * at which the desired datum starts.
191*9085SAli.Bahrami@Sun.COM * ehe_flags - DWARF encoding
192*9085SAli.Bahrami@Sun.COM * eident - ELF header e_ident[] array for object being processed
193*9085SAli.Bahrami@Sun.COM * sh_base - Base address of ELF section containing desired datum
194*9085SAli.Bahrami@Sun.COM * sh_offset - Offset relative to sh_base of desired datum.
195*9085SAli.Bahrami@Sun.COM */
1960Sstevel@tonic-gate uint64_t
dwarf_ehe_extract(unsigned char * data,uint64_t * dotp,uint_t ehe_flags,unsigned char * eident,uint64_t sh_base,uint64_t sh_offset)1970Sstevel@tonic-gate dwarf_ehe_extract(unsigned char *data, uint64_t *dotp, uint_t ehe_flags,
198*9085SAli.Bahrami@Sun.COM unsigned char *eident, uint64_t sh_base, uint64_t sh_offset)
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate uint64_t dot = *dotp;
2010Sstevel@tonic-gate uint_t lsb;
2020Sstevel@tonic-gate uint_t wordsize;
2030Sstevel@tonic-gate uint_t fsize;
2040Sstevel@tonic-gate uint64_t result;
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate if (eident[EI_DATA] == ELFDATA2LSB)
2070Sstevel@tonic-gate lsb = 1;
2080Sstevel@tonic-gate else
2090Sstevel@tonic-gate lsb = 0;
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate if (eident[EI_CLASS] == ELFCLASS64)
2120Sstevel@tonic-gate wordsize = 8;
2130Sstevel@tonic-gate else
2140Sstevel@tonic-gate wordsize = 4;
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate switch (ehe_flags & 0x0f) {
2170Sstevel@tonic-gate case DW_EH_PE_omit:
2180Sstevel@tonic-gate return (0);
2190Sstevel@tonic-gate case DW_EH_PE_absptr:
2200Sstevel@tonic-gate fsize = wordsize;
2210Sstevel@tonic-gate break;
2220Sstevel@tonic-gate case DW_EH_PE_udata8:
2230Sstevel@tonic-gate case DW_EH_PE_sdata8:
2240Sstevel@tonic-gate fsize = 8;
2250Sstevel@tonic-gate break;
2260Sstevel@tonic-gate case DW_EH_PE_udata4:
2270Sstevel@tonic-gate case DW_EH_PE_sdata4:
2280Sstevel@tonic-gate fsize = 4;
2290Sstevel@tonic-gate break;
2300Sstevel@tonic-gate case DW_EH_PE_udata2:
2310Sstevel@tonic-gate case DW_EH_PE_sdata2:
2320Sstevel@tonic-gate fsize = 2;
2330Sstevel@tonic-gate break;
2340Sstevel@tonic-gate case DW_EH_PE_uleb128:
2350Sstevel@tonic-gate return (uleb_extract(data, dotp));
2360Sstevel@tonic-gate case DW_EH_PE_sleb128:
2370Sstevel@tonic-gate return ((uint64_t)sleb_extract(data, dotp));
2380Sstevel@tonic-gate default:
2390Sstevel@tonic-gate return (0);
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate
2420Sstevel@tonic-gate if (lsb) {
2430Sstevel@tonic-gate /*
2440Sstevel@tonic-gate * Extract unaligned LSB formated data
2450Sstevel@tonic-gate */
2460Sstevel@tonic-gate uint_t cnt;
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate result = 0;
2490Sstevel@tonic-gate for (cnt = 0; cnt < fsize;
2500Sstevel@tonic-gate cnt++, dot++) {
2510Sstevel@tonic-gate uint64_t val;
2520Sstevel@tonic-gate val = data[dot];
2530Sstevel@tonic-gate result |= val << (cnt * 8);
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate } else {
2560Sstevel@tonic-gate /*
2570Sstevel@tonic-gate * Extract unaligned MSB formated data
2580Sstevel@tonic-gate */
2590Sstevel@tonic-gate uint_t cnt;
2600Sstevel@tonic-gate result = 0;
2610Sstevel@tonic-gate for (cnt = 0; cnt < fsize;
2620Sstevel@tonic-gate cnt++, dot++) {
2630Sstevel@tonic-gate uint64_t val;
2640Sstevel@tonic-gate val = data[dot];
2650Sstevel@tonic-gate result |= val << ((fsize - cnt - 1) * 8);
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate /*
2690Sstevel@tonic-gate * perform sign extension
2700Sstevel@tonic-gate */
2710Sstevel@tonic-gate if ((ehe_flags & DW_EH_PE_signed) &&
2720Sstevel@tonic-gate (fsize < sizeof (uint64_t))) {
2730Sstevel@tonic-gate int64_t sresult;
2740Sstevel@tonic-gate uint_t bitshift;
2750Sstevel@tonic-gate sresult = result;
2760Sstevel@tonic-gate bitshift = (sizeof (uint64_t) - fsize) * 8;
2770Sstevel@tonic-gate sresult = (sresult << bitshift) >> bitshift;
2780Sstevel@tonic-gate result = sresult;
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate
2810Sstevel@tonic-gate /*
282*9085SAli.Bahrami@Sun.COM * If value is relative to a base address, adjust it
2830Sstevel@tonic-gate */
284*9085SAli.Bahrami@Sun.COM if (result) {
285*9085SAli.Bahrami@Sun.COM switch (ehe_flags & 0xf0) {
286*9085SAli.Bahrami@Sun.COM case DW_EH_PE_pcrel:
287*9085SAli.Bahrami@Sun.COM result += sh_base + sh_offset;
288*9085SAli.Bahrami@Sun.COM break;
289*9085SAli.Bahrami@Sun.COM
290*9085SAli.Bahrami@Sun.COM case DW_EH_PE_datarel:
291*9085SAli.Bahrami@Sun.COM result += sh_base;
292*9085SAli.Bahrami@Sun.COM break;
293*9085SAli.Bahrami@Sun.COM }
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate *dotp = dot;
2960Sstevel@tonic-gate return (result);
2970Sstevel@tonic-gate }
298