17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 55aefb655Srie * Common Development and Distribution License (the "License"). 65aefb655Srie * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 215aefb655Srie 227c478bd9Sstevel@tonic-gate /* 237e16fca0SAli Bahrami * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 265aefb655Srie 277c478bd9Sstevel@tonic-gate #include <stdio.h> 287c478bd9Sstevel@tonic-gate #include <dwarf.h> 297c478bd9Sstevel@tonic-gate #include <sys/types.h> 307c478bd9Sstevel@tonic-gate #include <sys/elf.h> 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate /* 337c478bd9Sstevel@tonic-gate * Little Endian Base 128 (LEB128) numbers. 347c478bd9Sstevel@tonic-gate * ---------------------------------------- 357c478bd9Sstevel@tonic-gate * 367c478bd9Sstevel@tonic-gate * LEB128 is a scheme for encoding integers densely that exploits the 377c478bd9Sstevel@tonic-gate * assumption that most integers are small in magnitude. (This encoding 387c478bd9Sstevel@tonic-gate * is equally suitable whether the target machine architecture represents 397c478bd9Sstevel@tonic-gate * data in big-endian or little- endian 407c478bd9Sstevel@tonic-gate * 417c478bd9Sstevel@tonic-gate * Unsigned LEB128 numbers are encoded as follows: start at the low order 427c478bd9Sstevel@tonic-gate * end of an unsigned integer and chop it into 7-bit chunks. Place each 437c478bd9Sstevel@tonic-gate * chunk into the low order 7 bits of a byte. Typically, several of the 447c478bd9Sstevel@tonic-gate * high order bytes will be zero; discard them. Emit the remaining bytes in 457c478bd9Sstevel@tonic-gate * a stream, starting with the low order byte; set the high order bit on 467c478bd9Sstevel@tonic-gate * each byte except the last emitted byte. The high bit of zero on the last 477c478bd9Sstevel@tonic-gate * byte indicates to the decoder that it has encountered the last byte. 487c478bd9Sstevel@tonic-gate * The integer zero is a special case, consisting of a single zero byte. 497c478bd9Sstevel@tonic-gate * 507c478bd9Sstevel@tonic-gate * Signed, 2s complement LEB128 numbers are encoded in a similar except 517c478bd9Sstevel@tonic-gate * that the criterion for discarding high order bytes is not whether they 527c478bd9Sstevel@tonic-gate * are zero, but whether they consist entirely of sign extension bits. 537c478bd9Sstevel@tonic-gate * Consider the 32-bit integer -2. The three high level bytes of the number 547c478bd9Sstevel@tonic-gate * are sign extension, thus LEB128 would represent it as a single byte 557c478bd9Sstevel@tonic-gate * containing the low order 7 bits, with the high order bit cleared to 567c478bd9Sstevel@tonic-gate * indicate the end of the byte stream. 577c478bd9Sstevel@tonic-gate * 587c478bd9Sstevel@tonic-gate * Note that there is nothing within the LEB128 representation that 597c478bd9Sstevel@tonic-gate * indicates whether an encoded number is signed or unsigned. The decoder 607c478bd9Sstevel@tonic-gate * must know what type of number to expect. 617c478bd9Sstevel@tonic-gate * 627c478bd9Sstevel@tonic-gate * DWARF Exception Header Encoding 637c478bd9Sstevel@tonic-gate * ------------------------------- 647c478bd9Sstevel@tonic-gate * 657c478bd9Sstevel@tonic-gate * The DWARF Exception Header Encoding is used to describe the type of data 667c478bd9Sstevel@tonic-gate * used in the .eh_frame_hdr section. The upper 4 bits indicate how the 677c478bd9Sstevel@tonic-gate * value is to be applied. The lower 4 bits indicate the format of the data. 687c478bd9Sstevel@tonic-gate * 697c478bd9Sstevel@tonic-gate * DWARF Exception Header value format 707c478bd9Sstevel@tonic-gate * 717c478bd9Sstevel@tonic-gate * Name Value Meaning 727c478bd9Sstevel@tonic-gate * DW_EH_PE_omit 0xff No value is present. 737c478bd9Sstevel@tonic-gate * DW_EH_PE_absptr 0x00 Value is a void* 747c478bd9Sstevel@tonic-gate * DW_EH_PE_uleb128 0x01 Unsigned value is encoded using the 757c478bd9Sstevel@tonic-gate * Little Endian Base 128 (LEB128) 767c478bd9Sstevel@tonic-gate * DW_EH_PE_udata2 0x02 A 2 bytes unsigned value. 777c478bd9Sstevel@tonic-gate * DW_EH_PE_udata4 0x03 A 4 bytes unsigned value. 787c478bd9Sstevel@tonic-gate * DW_EH_PE_udata8 0x04 An 8 bytes unsigned value. 797c478bd9Sstevel@tonic-gate * DW_EH_PE_signed 0x08 bit on for all signed encodings 807c478bd9Sstevel@tonic-gate * DW_EH_PE_sleb128 0x09 Signed value is encoded using the 817c478bd9Sstevel@tonic-gate * Little Endian Base 128 (LEB128) 827c478bd9Sstevel@tonic-gate * DW_EH_PE_sdata2 0x0A A 2 bytes signed value. 837c478bd9Sstevel@tonic-gate * DW_EH_PE_sdata4 0x0B A 4 bytes signed value. 847c478bd9Sstevel@tonic-gate * DW_EH_PE_sdata8 0x0C An 8 bytes signed value. 857c478bd9Sstevel@tonic-gate * 867c478bd9Sstevel@tonic-gate * DWARF Exception Header application 877c478bd9Sstevel@tonic-gate * 887c478bd9Sstevel@tonic-gate * Name Value Meaning 897c478bd9Sstevel@tonic-gate * DW_EH_PE_absptr 0x00 Value is used with no modification. 907c478bd9Sstevel@tonic-gate * DW_EH_PE_pcrel 0x10 Value is reletive to the location of itself 917c478bd9Sstevel@tonic-gate * DW_EH_PE_textrel 0x20 927c478bd9Sstevel@tonic-gate * DW_EH_PE_datarel 0x30 Value is reletive to the beginning of the 937c478bd9Sstevel@tonic-gate * eh_frame_hdr segment ( segment type 947c478bd9Sstevel@tonic-gate * PT_GNU_EH_FRAME ) 957c478bd9Sstevel@tonic-gate * DW_EH_PE_funcrel 0x40 967c478bd9Sstevel@tonic-gate * DW_EH_PE_aligned 0x50 value is an aligned void* 977c478bd9Sstevel@tonic-gate * DW_EH_PE_indirect 0x80 bit to signal indirection after relocation 987c478bd9Sstevel@tonic-gate * DW_EH_PE_omit 0xff No value is present. 997c478bd9Sstevel@tonic-gate * 1007c478bd9Sstevel@tonic-gate */ 1017c478bd9Sstevel@tonic-gate 102*37915d86SRichard Lowe dwarf_error_t 103*37915d86SRichard Lowe uleb_extract(unsigned char *data, uint64_t *dotp, size_t len, uint64_t *ret) 1047c478bd9Sstevel@tonic-gate { 1057c478bd9Sstevel@tonic-gate uint64_t dot = *dotp; 1067c478bd9Sstevel@tonic-gate uint64_t res = 0; 1077c478bd9Sstevel@tonic-gate int more = 1; 1087c478bd9Sstevel@tonic-gate int shift = 0; 1097c478bd9Sstevel@tonic-gate int val; 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate data += dot; 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate while (more) { 114*37915d86SRichard Lowe if (dot > len) 115*37915d86SRichard Lowe return (DW_OVERFLOW); 116*37915d86SRichard Lowe 1177c478bd9Sstevel@tonic-gate /* 1187c478bd9Sstevel@tonic-gate * Pull off lower 7 bits 1197c478bd9Sstevel@tonic-gate */ 1207c478bd9Sstevel@tonic-gate val = (*data) & 0x7f; 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate /* 1237c478bd9Sstevel@tonic-gate * Add prepend value to head of number. 1247c478bd9Sstevel@tonic-gate */ 1257c478bd9Sstevel@tonic-gate res = res | (val << shift); 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate /* 1287c478bd9Sstevel@tonic-gate * Increment shift & dot pointer 1297c478bd9Sstevel@tonic-gate */ 1307c478bd9Sstevel@tonic-gate shift += 7; 1317c478bd9Sstevel@tonic-gate dot++; 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate /* 1347c478bd9Sstevel@tonic-gate * Check to see if hi bit is set - if not, this 1357c478bd9Sstevel@tonic-gate * is the last byte. 1367c478bd9Sstevel@tonic-gate */ 1377c478bd9Sstevel@tonic-gate more = ((*data++) & 0x80) >> 7; 1387c478bd9Sstevel@tonic-gate } 1397c478bd9Sstevel@tonic-gate *dotp = dot; 140*37915d86SRichard Lowe *ret = res; 141*37915d86SRichard Lowe return (DW_SUCCESS); 1427c478bd9Sstevel@tonic-gate } 1437c478bd9Sstevel@tonic-gate 144*37915d86SRichard Lowe dwarf_error_t 145*37915d86SRichard Lowe sleb_extract(unsigned char *data, uint64_t *dotp, size_t len, int64_t *ret) 1467c478bd9Sstevel@tonic-gate { 1477c478bd9Sstevel@tonic-gate uint64_t dot = *dotp; 1487c478bd9Sstevel@tonic-gate int64_t res = 0; 1497c478bd9Sstevel@tonic-gate int more = 1; 1507c478bd9Sstevel@tonic-gate int shift = 0; 1517c478bd9Sstevel@tonic-gate int val; 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate data += dot; 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate while (more) { 156*37915d86SRichard Lowe if (dot > len) 157*37915d86SRichard Lowe return (DW_OVERFLOW); 158*37915d86SRichard Lowe 1597c478bd9Sstevel@tonic-gate /* 1607c478bd9Sstevel@tonic-gate * Pull off lower 7 bits 1617c478bd9Sstevel@tonic-gate */ 1627c478bd9Sstevel@tonic-gate val = (*data) & 0x7f; 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate /* 1657c478bd9Sstevel@tonic-gate * Add prepend value to head of number. 1667c478bd9Sstevel@tonic-gate */ 1677c478bd9Sstevel@tonic-gate res = res | (val << shift); 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate /* 1707c478bd9Sstevel@tonic-gate * Increment shift & dot pointer 1717c478bd9Sstevel@tonic-gate */ 1727c478bd9Sstevel@tonic-gate shift += 7; 1737c478bd9Sstevel@tonic-gate dot++; 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate /* 1767c478bd9Sstevel@tonic-gate * Check to see if hi bit is set - if not, this 1777c478bd9Sstevel@tonic-gate * is the last byte. 1787c478bd9Sstevel@tonic-gate */ 1797c478bd9Sstevel@tonic-gate more = ((*data++) & 0x80) >> 7; 1807c478bd9Sstevel@tonic-gate } 1817c478bd9Sstevel@tonic-gate *dotp = dot; 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate /* 1847c478bd9Sstevel@tonic-gate * Make sure value is properly sign extended. 1857c478bd9Sstevel@tonic-gate */ 1867c478bd9Sstevel@tonic-gate res = (res << (64 - shift)) >> (64 - shift); 187*37915d86SRichard Lowe *ret = res; 188*37915d86SRichard Lowe return (DW_SUCCESS); 1897c478bd9Sstevel@tonic-gate } 1907c478bd9Sstevel@tonic-gate 1917e16fca0SAli Bahrami /* 1927e16fca0SAli Bahrami * Extract a DWARF encoded datum 1937e16fca0SAli Bahrami * 1947e16fca0SAli Bahrami * entry: 1957e16fca0SAli Bahrami * data - Base of data buffer containing encoded bytes 1967e16fca0SAli Bahrami * dotp - Address of variable containing index within data 1977e16fca0SAli Bahrami * at which the desired datum starts. 1987e16fca0SAli Bahrami * ehe_flags - DWARF encoding 1997e16fca0SAli Bahrami * eident - ELF header e_ident[] array for object being processed 200965630c1SRichard Lowe * frame_hdr - Boolean, true if we're extracting from .eh_frame_hdr 2017e16fca0SAli Bahrami * sh_base - Base address of ELF section containing desired datum 2027e16fca0SAli Bahrami * sh_offset - Offset relative to sh_base of desired datum. 203965630c1SRichard Lowe * dbase - The base address to which DW_EH_PE_datarel is relative 204965630c1SRichard Lowe * (if frame_hdr is false) 2057e16fca0SAli Bahrami */ 206*37915d86SRichard Lowe dwarf_error_t 207*37915d86SRichard Lowe dwarf_ehe_extract(unsigned char *data, size_t len, uint64_t *dotp, 208*37915d86SRichard Lowe uint64_t *ret, uint_t ehe_flags, unsigned char *eident, 209*37915d86SRichard Lowe boolean_t frame_hdr, uint64_t sh_base, uint64_t sh_offset, 210*37915d86SRichard Lowe uint64_t dbase) 2117c478bd9Sstevel@tonic-gate { 2127c478bd9Sstevel@tonic-gate uint64_t dot = *dotp; 2137c478bd9Sstevel@tonic-gate uint_t lsb; 2147c478bd9Sstevel@tonic-gate uint_t wordsize; 2157c478bd9Sstevel@tonic-gate uint_t fsize; 2167c478bd9Sstevel@tonic-gate uint64_t result; 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate if (eident[EI_DATA] == ELFDATA2LSB) 2197c478bd9Sstevel@tonic-gate lsb = 1; 2207c478bd9Sstevel@tonic-gate else 2217c478bd9Sstevel@tonic-gate lsb = 0; 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate if (eident[EI_CLASS] == ELFCLASS64) 2247c478bd9Sstevel@tonic-gate wordsize = 8; 2257c478bd9Sstevel@tonic-gate else 2267c478bd9Sstevel@tonic-gate wordsize = 4; 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate switch (ehe_flags & 0x0f) { 2297c478bd9Sstevel@tonic-gate case DW_EH_PE_omit: 230*37915d86SRichard Lowe *ret = 0; 231*37915d86SRichard Lowe return (DW_SUCCESS); 2327c478bd9Sstevel@tonic-gate case DW_EH_PE_absptr: 2337c478bd9Sstevel@tonic-gate fsize = wordsize; 2347c478bd9Sstevel@tonic-gate break; 2357c478bd9Sstevel@tonic-gate case DW_EH_PE_udata8: 2367c478bd9Sstevel@tonic-gate case DW_EH_PE_sdata8: 2377c478bd9Sstevel@tonic-gate fsize = 8; 2387c478bd9Sstevel@tonic-gate break; 2397c478bd9Sstevel@tonic-gate case DW_EH_PE_udata4: 2407c478bd9Sstevel@tonic-gate case DW_EH_PE_sdata4: 2417c478bd9Sstevel@tonic-gate fsize = 4; 2427c478bd9Sstevel@tonic-gate break; 2437c478bd9Sstevel@tonic-gate case DW_EH_PE_udata2: 2447c478bd9Sstevel@tonic-gate case DW_EH_PE_sdata2: 2457c478bd9Sstevel@tonic-gate fsize = 2; 2467c478bd9Sstevel@tonic-gate break; 2477c478bd9Sstevel@tonic-gate case DW_EH_PE_uleb128: 248*37915d86SRichard Lowe return (uleb_extract(data, dotp, len, ret)); 2497c478bd9Sstevel@tonic-gate case DW_EH_PE_sleb128: 250*37915d86SRichard Lowe return (sleb_extract(data, dotp, len, (int64_t *)ret)); 2517c478bd9Sstevel@tonic-gate default: 252*37915d86SRichard Lowe *ret = 0; 253*37915d86SRichard Lowe return (DW_BAD_ENCODING); 2547c478bd9Sstevel@tonic-gate } 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate if (lsb) { 2577c478bd9Sstevel@tonic-gate /* 2587c478bd9Sstevel@tonic-gate * Extract unaligned LSB formated data 2597c478bd9Sstevel@tonic-gate */ 2607c478bd9Sstevel@tonic-gate uint_t cnt; 2617c478bd9Sstevel@tonic-gate 2627c478bd9Sstevel@tonic-gate result = 0; 2637c478bd9Sstevel@tonic-gate for (cnt = 0; cnt < fsize; 2647c478bd9Sstevel@tonic-gate cnt++, dot++) { 2657c478bd9Sstevel@tonic-gate uint64_t val; 266*37915d86SRichard Lowe 267*37915d86SRichard Lowe if (dot > len) 268*37915d86SRichard Lowe return (DW_OVERFLOW); 2697c478bd9Sstevel@tonic-gate val = data[dot]; 2707c478bd9Sstevel@tonic-gate result |= val << (cnt * 8); 2717c478bd9Sstevel@tonic-gate } 2727c478bd9Sstevel@tonic-gate } else { 2737c478bd9Sstevel@tonic-gate /* 2747c478bd9Sstevel@tonic-gate * Extract unaligned MSB formated data 2757c478bd9Sstevel@tonic-gate */ 2767c478bd9Sstevel@tonic-gate uint_t cnt; 2777c478bd9Sstevel@tonic-gate result = 0; 2787c478bd9Sstevel@tonic-gate for (cnt = 0; cnt < fsize; 2797c478bd9Sstevel@tonic-gate cnt++, dot++) { 2807c478bd9Sstevel@tonic-gate uint64_t val; 281*37915d86SRichard Lowe 282*37915d86SRichard Lowe if (dot > len) 283*37915d86SRichard Lowe return (DW_OVERFLOW); 2847c478bd9Sstevel@tonic-gate val = data[dot]; 2857c478bd9Sstevel@tonic-gate result |= val << ((fsize - cnt - 1) * 8); 2867c478bd9Sstevel@tonic-gate } 2877c478bd9Sstevel@tonic-gate } 2887c478bd9Sstevel@tonic-gate /* 2897c478bd9Sstevel@tonic-gate * perform sign extension 2907c478bd9Sstevel@tonic-gate */ 2917c478bd9Sstevel@tonic-gate if ((ehe_flags & DW_EH_PE_signed) && 2927c478bd9Sstevel@tonic-gate (fsize < sizeof (uint64_t))) { 2937c478bd9Sstevel@tonic-gate int64_t sresult; 2947c478bd9Sstevel@tonic-gate uint_t bitshift; 2957c478bd9Sstevel@tonic-gate sresult = result; 2967c478bd9Sstevel@tonic-gate bitshift = (sizeof (uint64_t) - fsize) * 8; 2977c478bd9Sstevel@tonic-gate sresult = (sresult << bitshift) >> bitshift; 2987c478bd9Sstevel@tonic-gate result = sresult; 2997c478bd9Sstevel@tonic-gate } 3007c478bd9Sstevel@tonic-gate 3017c478bd9Sstevel@tonic-gate /* 3027e16fca0SAli Bahrami * If value is relative to a base address, adjust it 3037c478bd9Sstevel@tonic-gate */ 3047e16fca0SAli Bahrami switch (ehe_flags & 0xf0) { 3057e16fca0SAli Bahrami case DW_EH_PE_pcrel: 3067e16fca0SAli Bahrami result += sh_base + sh_offset; 3077e16fca0SAli Bahrami break; 3087e16fca0SAli Bahrami 309965630c1SRichard Lowe /* 310965630c1SRichard Lowe * datarel is relative to .eh_frame_hdr if within .eh_frame, 311965630c1SRichard Lowe * but GOT if not. 312965630c1SRichard Lowe */ 3137e16fca0SAli Bahrami case DW_EH_PE_datarel: 314965630c1SRichard Lowe if (frame_hdr) 3157e16fca0SAli Bahrami result += sh_base; 316965630c1SRichard Lowe else 317965630c1SRichard Lowe result += dbase; 3187e16fca0SAli Bahrami break; 3197e16fca0SAli Bahrami } 320965630c1SRichard Lowe 321965630c1SRichard Lowe /* Truncate the result to its specified size */ 322965630c1SRichard Lowe result = (result << ((sizeof (uint64_t) - fsize) * 8)) >> 323965630c1SRichard Lowe ((sizeof (uint64_t) - fsize) * 8); 324965630c1SRichard Lowe 3257c478bd9Sstevel@tonic-gate *dotp = dot; 326*37915d86SRichard Lowe *ret = result; 327*37915d86SRichard Lowe return (DW_SUCCESS); 3287c478bd9Sstevel@tonic-gate } 329