14684ddb6SLionel Sambuc /*
24684ddb6SLionel Sambuc * Copyright 2010-2011 PathScale, Inc. All rights reserved.
34684ddb6SLionel Sambuc *
44684ddb6SLionel Sambuc * Redistribution and use in source and binary forms, with or without
54684ddb6SLionel Sambuc * modification, are permitted provided that the following conditions are met:
64684ddb6SLionel Sambuc *
74684ddb6SLionel Sambuc * 1. Redistributions of source code must retain the above copyright notice,
84684ddb6SLionel Sambuc * this list of conditions and the following disclaimer.
94684ddb6SLionel Sambuc *
104684ddb6SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright notice,
114684ddb6SLionel Sambuc * this list of conditions and the following disclaimer in the documentation
124684ddb6SLionel Sambuc * and/or other materials provided with the distribution.
134684ddb6SLionel Sambuc *
144684ddb6SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
154684ddb6SLionel Sambuc * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
164684ddb6SLionel Sambuc * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
174684ddb6SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
184684ddb6SLionel Sambuc * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
194684ddb6SLionel Sambuc * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
204684ddb6SLionel Sambuc * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
214684ddb6SLionel Sambuc * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
224684ddb6SLionel Sambuc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
234684ddb6SLionel Sambuc * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
244684ddb6SLionel Sambuc * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
254684ddb6SLionel Sambuc */
264684ddb6SLionel Sambuc /**
274684ddb6SLionel Sambuc * dwarf_eh.h - Defines some helper functions for parsing DWARF exception
284684ddb6SLionel Sambuc * handling tables.
294684ddb6SLionel Sambuc *
304684ddb6SLionel Sambuc * This file contains various helper functions that are independent of the
314684ddb6SLionel Sambuc * language-specific code. It can be used in any personality function for the
324684ddb6SLionel Sambuc * Itanium ABI.
334684ddb6SLionel Sambuc */
344684ddb6SLionel Sambuc #include <assert.h>
354684ddb6SLionel Sambuc
364684ddb6SLionel Sambuc // TODO: Factor out Itanium / ARM differences. We probably want an itanium.h
374684ddb6SLionel Sambuc // and arm.h that can be included by this file depending on the target ABI.
384684ddb6SLionel Sambuc
394684ddb6SLionel Sambuc // _GNU_SOURCE must be defined for unwind.h to expose some of the functions
404684ddb6SLionel Sambuc // that we want. If it isn't, then we define it and undefine it to make sure
414684ddb6SLionel Sambuc // that it doesn't impact the rest of the program.
424684ddb6SLionel Sambuc #ifndef _GNU_SOURCE
434684ddb6SLionel Sambuc # define _GNU_SOURCE 1
444684ddb6SLionel Sambuc # include "unwind.h"
454684ddb6SLionel Sambuc # undef _GNU_SOURCE
464684ddb6SLionel Sambuc #else
474684ddb6SLionel Sambuc # include "unwind.h"
484684ddb6SLionel Sambuc #endif
494684ddb6SLionel Sambuc
504684ddb6SLionel Sambuc #include <stdint.h>
514684ddb6SLionel Sambuc
524684ddb6SLionel Sambuc /// Type used for pointers into DWARF data
534684ddb6SLionel Sambuc typedef unsigned char *dw_eh_ptr_t;
544684ddb6SLionel Sambuc
554684ddb6SLionel Sambuc // Flag indicating a signed quantity
564684ddb6SLionel Sambuc #define DW_EH_PE_signed 0x08
574684ddb6SLionel Sambuc /// DWARF data encoding types.
584684ddb6SLionel Sambuc enum dwarf_data_encoding
594684ddb6SLionel Sambuc {
604684ddb6SLionel Sambuc /// Absolute pointer value
614684ddb6SLionel Sambuc DW_EH_PE_absptr = 0x00,
624684ddb6SLionel Sambuc /// Unsigned, little-endian, base 128-encoded (variable length).
634684ddb6SLionel Sambuc DW_EH_PE_uleb128 = 0x01,
644684ddb6SLionel Sambuc /// Unsigned 16-bit integer.
654684ddb6SLionel Sambuc DW_EH_PE_udata2 = 0x02,
664684ddb6SLionel Sambuc /// Unsigned 32-bit integer.
674684ddb6SLionel Sambuc DW_EH_PE_udata4 = 0x03,
684684ddb6SLionel Sambuc /// Unsigned 64-bit integer.
694684ddb6SLionel Sambuc DW_EH_PE_udata8 = 0x04,
704684ddb6SLionel Sambuc /// Signed, little-endian, base 128-encoded (variable length)
714684ddb6SLionel Sambuc DW_EH_PE_sleb128 = DW_EH_PE_uleb128 | DW_EH_PE_signed,
724684ddb6SLionel Sambuc /// Signed 16-bit integer.
734684ddb6SLionel Sambuc DW_EH_PE_sdata2 = DW_EH_PE_udata2 | DW_EH_PE_signed,
744684ddb6SLionel Sambuc /// Signed 32-bit integer.
754684ddb6SLionel Sambuc DW_EH_PE_sdata4 = DW_EH_PE_udata4 | DW_EH_PE_signed,
764684ddb6SLionel Sambuc /// Signed 32-bit integer.
774684ddb6SLionel Sambuc DW_EH_PE_sdata8 = DW_EH_PE_udata8 | DW_EH_PE_signed
784684ddb6SLionel Sambuc };
794684ddb6SLionel Sambuc
804684ddb6SLionel Sambuc /**
814684ddb6SLionel Sambuc * Returns the encoding for a DWARF EH table entry. The encoding is stored in
824684ddb6SLionel Sambuc * the low four of an octet. The high four bits store the addressing mode.
834684ddb6SLionel Sambuc */
get_encoding(unsigned char x)844684ddb6SLionel Sambuc static inline enum dwarf_data_encoding get_encoding(unsigned char x)
854684ddb6SLionel Sambuc {
86*0a6a1f1dSLionel Sambuc return static_cast<enum dwarf_data_encoding>(x & 0xf);
874684ddb6SLionel Sambuc }
884684ddb6SLionel Sambuc
894684ddb6SLionel Sambuc /**
904684ddb6SLionel Sambuc * DWARF addressing mode constants. When reading a pointer value from a DWARF
914684ddb6SLionel Sambuc * exception table, you must know how it is stored and what the addressing mode
924684ddb6SLionel Sambuc * is. The low four bits tell you the encoding, allowing you to decode a
934684ddb6SLionel Sambuc * number. The high four bits tell you the addressing mode, allowing you to
944684ddb6SLionel Sambuc * turn that number into an address in memory.
954684ddb6SLionel Sambuc */
964684ddb6SLionel Sambuc enum dwarf_data_relative
974684ddb6SLionel Sambuc {
984684ddb6SLionel Sambuc /// Value is omitted
994684ddb6SLionel Sambuc DW_EH_PE_omit = 0xff,
1004684ddb6SLionel Sambuc /// Value relative to program counter
1014684ddb6SLionel Sambuc DW_EH_PE_pcrel = 0x10,
1024684ddb6SLionel Sambuc /// Value relative to the text segment
1034684ddb6SLionel Sambuc DW_EH_PE_textrel = 0x20,
1044684ddb6SLionel Sambuc /// Value relative to the data segment
1054684ddb6SLionel Sambuc DW_EH_PE_datarel = 0x30,
1064684ddb6SLionel Sambuc /// Value relative to the start of the function
1074684ddb6SLionel Sambuc DW_EH_PE_funcrel = 0x40,
1084684ddb6SLionel Sambuc /// Aligned pointer (Not supported yet - are they actually used?)
1094684ddb6SLionel Sambuc DW_EH_PE_aligned = 0x50,
1104684ddb6SLionel Sambuc /// Pointer points to address of real value
1114684ddb6SLionel Sambuc DW_EH_PE_indirect = 0x80
1124684ddb6SLionel Sambuc };
1134684ddb6SLionel Sambuc /**
1144684ddb6SLionel Sambuc * Returns the addressing mode component of this encoding.
1154684ddb6SLionel Sambuc */
get_base(unsigned char x)1164684ddb6SLionel Sambuc static inline enum dwarf_data_relative get_base(unsigned char x)
1174684ddb6SLionel Sambuc {
118*0a6a1f1dSLionel Sambuc return static_cast<enum dwarf_data_relative>(x & 0x70);
1194684ddb6SLionel Sambuc }
1204684ddb6SLionel Sambuc /**
1214684ddb6SLionel Sambuc * Returns whether an encoding represents an indirect address.
1224684ddb6SLionel Sambuc */
is_indirect(unsigned char x)1234684ddb6SLionel Sambuc static int is_indirect(unsigned char x)
1244684ddb6SLionel Sambuc {
1254684ddb6SLionel Sambuc return ((x & DW_EH_PE_indirect) == DW_EH_PE_indirect);
1264684ddb6SLionel Sambuc }
1274684ddb6SLionel Sambuc
1284684ddb6SLionel Sambuc /**
1294684ddb6SLionel Sambuc * Returns the size of a fixed-size encoding. This function will abort if
1304684ddb6SLionel Sambuc * called with a value that is not a fixed-size encoding.
1314684ddb6SLionel Sambuc */
dwarf_size_of_fixed_size_field(unsigned char type)1324684ddb6SLionel Sambuc static inline int dwarf_size_of_fixed_size_field(unsigned char type)
1334684ddb6SLionel Sambuc {
1344684ddb6SLionel Sambuc switch (get_encoding(type))
1354684ddb6SLionel Sambuc {
1364684ddb6SLionel Sambuc default: abort();
1374684ddb6SLionel Sambuc case DW_EH_PE_sdata2:
1384684ddb6SLionel Sambuc case DW_EH_PE_udata2: return 2;
1394684ddb6SLionel Sambuc case DW_EH_PE_sdata4:
1404684ddb6SLionel Sambuc case DW_EH_PE_udata4: return 4;
1414684ddb6SLionel Sambuc case DW_EH_PE_sdata8:
1424684ddb6SLionel Sambuc case DW_EH_PE_udata8: return 8;
1434684ddb6SLionel Sambuc case DW_EH_PE_absptr: return sizeof(void*);
1444684ddb6SLionel Sambuc }
1454684ddb6SLionel Sambuc }
1464684ddb6SLionel Sambuc
1474684ddb6SLionel Sambuc /**
1484684ddb6SLionel Sambuc * Read an unsigned, little-endian, base-128, DWARF value. Updates *data to
1494684ddb6SLionel Sambuc * point to the end of the value. Stores the number of bits read in the value
1504684ddb6SLionel Sambuc * pointed to by b, allowing you to determine the value of the highest bit, and
1514684ddb6SLionel Sambuc * therefore the sign of a signed value.
1524684ddb6SLionel Sambuc *
1534684ddb6SLionel Sambuc * This function is not intended to be called directly. Use read_sleb128() or
1544684ddb6SLionel Sambuc * read_uleb128() for reading signed and unsigned versions, respectively.
1554684ddb6SLionel Sambuc */
read_leb128(dw_eh_ptr_t * data,int * b)1564684ddb6SLionel Sambuc static uint64_t read_leb128(dw_eh_ptr_t *data, int *b)
1574684ddb6SLionel Sambuc {
1584684ddb6SLionel Sambuc uint64_t uleb = 0;
1594684ddb6SLionel Sambuc unsigned int bit = 0;
1604684ddb6SLionel Sambuc unsigned char digit = 0;
1614684ddb6SLionel Sambuc // We have to read at least one octet, and keep reading until we get to one
1624684ddb6SLionel Sambuc // with the high bit unset
1634684ddb6SLionel Sambuc do
1644684ddb6SLionel Sambuc {
1654684ddb6SLionel Sambuc // This check is a bit too strict - we should also check the highest
1664684ddb6SLionel Sambuc // bit of the digit.
1674684ddb6SLionel Sambuc assert(bit < sizeof(uint64_t) * 8);
1684684ddb6SLionel Sambuc // Get the base 128 digit
1694684ddb6SLionel Sambuc digit = (**data) & 0x7f;
1704684ddb6SLionel Sambuc // Add it to the current value
1714684ddb6SLionel Sambuc uleb += digit << bit;
1724684ddb6SLionel Sambuc // Increase the shift value
1734684ddb6SLionel Sambuc bit += 7;
1744684ddb6SLionel Sambuc // Proceed to the next octet
1754684ddb6SLionel Sambuc (*data)++;
1764684ddb6SLionel Sambuc // Terminate when we reach a value that does not have the high bit set
1774684ddb6SLionel Sambuc // (i.e. which was not modified when we mask it with 0x7f)
1784684ddb6SLionel Sambuc } while ((*(*data - 1)) != digit);
1794684ddb6SLionel Sambuc *b = bit;
1804684ddb6SLionel Sambuc
1814684ddb6SLionel Sambuc return uleb;
1824684ddb6SLionel Sambuc }
1834684ddb6SLionel Sambuc
1844684ddb6SLionel Sambuc /**
1854684ddb6SLionel Sambuc * Reads an unsigned little-endian base-128 value starting at the address
1864684ddb6SLionel Sambuc * pointed to by *data. Updates *data to point to the next byte after the end
1874684ddb6SLionel Sambuc * of the variable-length value.
1884684ddb6SLionel Sambuc */
read_uleb128(dw_eh_ptr_t * data)1894684ddb6SLionel Sambuc static int64_t read_uleb128(dw_eh_ptr_t *data)
1904684ddb6SLionel Sambuc {
1914684ddb6SLionel Sambuc int b;
1924684ddb6SLionel Sambuc return read_leb128(data, &b);
1934684ddb6SLionel Sambuc }
1944684ddb6SLionel Sambuc
1954684ddb6SLionel Sambuc /**
1964684ddb6SLionel Sambuc * Reads a signed little-endian base-128 value starting at the address pointed
1974684ddb6SLionel Sambuc * to by *data. Updates *data to point to the next byte after the end of the
1984684ddb6SLionel Sambuc * variable-length value.
1994684ddb6SLionel Sambuc */
read_sleb128(dw_eh_ptr_t * data)2004684ddb6SLionel Sambuc static int64_t read_sleb128(dw_eh_ptr_t *data)
2014684ddb6SLionel Sambuc {
2024684ddb6SLionel Sambuc int bits;
2034684ddb6SLionel Sambuc // Read as if it's signed
2044684ddb6SLionel Sambuc uint64_t uleb = read_leb128(data, &bits);
2054684ddb6SLionel Sambuc // If the most significant bit read is 1, then we need to sign extend it
2064684ddb6SLionel Sambuc if ((uleb >> (bits-1)) == 1)
2074684ddb6SLionel Sambuc {
2084684ddb6SLionel Sambuc // Sign extend by setting all bits in front of it to 1
209*0a6a1f1dSLionel Sambuc uleb |= static_cast<int64_t>(-1) << bits;
2104684ddb6SLionel Sambuc }
211*0a6a1f1dSLionel Sambuc return static_cast<int64_t>(uleb);
2124684ddb6SLionel Sambuc }
2134684ddb6SLionel Sambuc /**
2144684ddb6SLionel Sambuc * Reads a value using the specified encoding from the address pointed to by
2154684ddb6SLionel Sambuc * *data. Updates the value of *data to point to the next byte after the end
2164684ddb6SLionel Sambuc * of the data.
2174684ddb6SLionel Sambuc */
read_value(char encoding,dw_eh_ptr_t * data)2184684ddb6SLionel Sambuc static uint64_t read_value(char encoding, dw_eh_ptr_t *data)
2194684ddb6SLionel Sambuc {
2204684ddb6SLionel Sambuc enum dwarf_data_encoding type = get_encoding(encoding);
2214684ddb6SLionel Sambuc switch (type)
2224684ddb6SLionel Sambuc {
2234684ddb6SLionel Sambuc // Read fixed-length types
2244684ddb6SLionel Sambuc #define READ(dwarf, type) \
2254684ddb6SLionel Sambuc case dwarf:\
226*0a6a1f1dSLionel Sambuc {\
227*0a6a1f1dSLionel Sambuc type t;\
228*0a6a1f1dSLionel Sambuc memcpy(&t, *data, sizeof t);\
229*0a6a1f1dSLionel Sambuc *data += sizeof t;\
230*0a6a1f1dSLionel Sambuc return static_cast<uint64_t>(t);\
231*0a6a1f1dSLionel Sambuc }
2324684ddb6SLionel Sambuc READ(DW_EH_PE_udata2, uint16_t)
2334684ddb6SLionel Sambuc READ(DW_EH_PE_udata4, uint32_t)
2344684ddb6SLionel Sambuc READ(DW_EH_PE_udata8, uint64_t)
2354684ddb6SLionel Sambuc READ(DW_EH_PE_sdata2, int16_t)
2364684ddb6SLionel Sambuc READ(DW_EH_PE_sdata4, int32_t)
2374684ddb6SLionel Sambuc READ(DW_EH_PE_sdata8, int64_t)
2384684ddb6SLionel Sambuc READ(DW_EH_PE_absptr, intptr_t)
2394684ddb6SLionel Sambuc #undef READ
2404684ddb6SLionel Sambuc // Read variable-length types
2414684ddb6SLionel Sambuc case DW_EH_PE_sleb128:
242*0a6a1f1dSLionel Sambuc return read_sleb128(data);
2434684ddb6SLionel Sambuc case DW_EH_PE_uleb128:
244*0a6a1f1dSLionel Sambuc return read_uleb128(data);
2454684ddb6SLionel Sambuc default: abort();
2464684ddb6SLionel Sambuc }
2474684ddb6SLionel Sambuc }
2484684ddb6SLionel Sambuc
2494684ddb6SLionel Sambuc /**
2504684ddb6SLionel Sambuc * Resolves an indirect value. This expects an unwind context, an encoding, a
2514684ddb6SLionel Sambuc * decoded value, and the start of the region as arguments. The returned value
2524684ddb6SLionel Sambuc * is a pointer to the address identified by the encoded value.
2534684ddb6SLionel Sambuc *
2544684ddb6SLionel Sambuc * If the encoding does not specify an indirect value, then this returns v.
2554684ddb6SLionel Sambuc */
resolve_indirect_value(_Unwind_Context * c,unsigned char encoding,int64_t v,dw_eh_ptr_t start)2564684ddb6SLionel Sambuc static uint64_t resolve_indirect_value(_Unwind_Context *c,
2574684ddb6SLionel Sambuc unsigned char encoding,
2584684ddb6SLionel Sambuc int64_t v,
2594684ddb6SLionel Sambuc dw_eh_ptr_t start)
2604684ddb6SLionel Sambuc {
2614684ddb6SLionel Sambuc switch (get_base(encoding))
2624684ddb6SLionel Sambuc {
2634684ddb6SLionel Sambuc case DW_EH_PE_pcrel:
264*0a6a1f1dSLionel Sambuc v += reinterpret_cast<uint64_t>(start);
2654684ddb6SLionel Sambuc break;
2664684ddb6SLionel Sambuc case DW_EH_PE_textrel:
267*0a6a1f1dSLionel Sambuc v += static_cast<uint64_t>(static_cast<uintptr_t>(_Unwind_GetTextRelBase(c)));
2684684ddb6SLionel Sambuc break;
2694684ddb6SLionel Sambuc case DW_EH_PE_datarel:
270*0a6a1f1dSLionel Sambuc v += static_cast<uint64_t>(static_cast<uintptr_t>(_Unwind_GetDataRelBase(c)));
2714684ddb6SLionel Sambuc break;
2724684ddb6SLionel Sambuc case DW_EH_PE_funcrel:
273*0a6a1f1dSLionel Sambuc v += static_cast<uint64_t>(static_cast<uintptr_t>(_Unwind_GetRegionStart(c)));
2744684ddb6SLionel Sambuc default:
2754684ddb6SLionel Sambuc break;
2764684ddb6SLionel Sambuc }
2774684ddb6SLionel Sambuc // If this is an indirect value, then it is really the address of the real
2784684ddb6SLionel Sambuc // value
2794684ddb6SLionel Sambuc // TODO: Check whether this should really always be a pointer - it seems to
2804684ddb6SLionel Sambuc // be a GCC extensions, so not properly documented...
2814684ddb6SLionel Sambuc if (is_indirect(encoding))
2824684ddb6SLionel Sambuc {
283*0a6a1f1dSLionel Sambuc v = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(*reinterpret_cast<void**>(v)));
2844684ddb6SLionel Sambuc }
2854684ddb6SLionel Sambuc return v;
2864684ddb6SLionel Sambuc }
2874684ddb6SLionel Sambuc
2884684ddb6SLionel Sambuc
2894684ddb6SLionel Sambuc /**
2904684ddb6SLionel Sambuc * Reads an encoding and a value, updating *data to point to the next byte.
2914684ddb6SLionel Sambuc */
read_value_with_encoding(_Unwind_Context * context,dw_eh_ptr_t * data,uint64_t * out)2924684ddb6SLionel Sambuc static inline void read_value_with_encoding(_Unwind_Context *context,
2934684ddb6SLionel Sambuc dw_eh_ptr_t *data,
2944684ddb6SLionel Sambuc uint64_t *out)
2954684ddb6SLionel Sambuc {
2964684ddb6SLionel Sambuc dw_eh_ptr_t start = *data;
2974684ddb6SLionel Sambuc unsigned char encoding = *((*data)++);
2984684ddb6SLionel Sambuc // If this value is omitted, skip it and don't touch the output value
2994684ddb6SLionel Sambuc if (encoding == DW_EH_PE_omit) { return; }
3004684ddb6SLionel Sambuc
3014684ddb6SLionel Sambuc *out = read_value(encoding, data);
3024684ddb6SLionel Sambuc *out = resolve_indirect_value(context, encoding, *out, start);
3034684ddb6SLionel Sambuc }
3044684ddb6SLionel Sambuc
3054684ddb6SLionel Sambuc /**
3064684ddb6SLionel Sambuc * Structure storing a decoded language-specific data area. Use parse_lsda()
3074684ddb6SLionel Sambuc * to generate an instance of this structure from the address returned by the
3084684ddb6SLionel Sambuc * generic unwind library.
3094684ddb6SLionel Sambuc *
3104684ddb6SLionel Sambuc * You should not need to inspect the fields of this structure directly if you
3114684ddb6SLionel Sambuc * are just using this header. The structure stores the locations of the
3124684ddb6SLionel Sambuc * various tables used for unwinding exceptions and is used by the functions
3134684ddb6SLionel Sambuc * for reading values from these tables.
3144684ddb6SLionel Sambuc */
3154684ddb6SLionel Sambuc struct dwarf_eh_lsda
3164684ddb6SLionel Sambuc {
3174684ddb6SLionel Sambuc /// The start of the region. This is a cache of the value returned by
3184684ddb6SLionel Sambuc /// _Unwind_GetRegionStart().
3194684ddb6SLionel Sambuc dw_eh_ptr_t region_start;
3204684ddb6SLionel Sambuc /// The start of the landing pads table.
3214684ddb6SLionel Sambuc dw_eh_ptr_t landing_pads;
3224684ddb6SLionel Sambuc /// The start of the type table.
3234684ddb6SLionel Sambuc dw_eh_ptr_t type_table;
3244684ddb6SLionel Sambuc /// The encoding used for entries in the type tables.
3254684ddb6SLionel Sambuc unsigned char type_table_encoding;
3264684ddb6SLionel Sambuc /// The location of the call-site table.
3274684ddb6SLionel Sambuc dw_eh_ptr_t call_site_table;
3284684ddb6SLionel Sambuc /// The location of the action table.
3294684ddb6SLionel Sambuc dw_eh_ptr_t action_table;
3304684ddb6SLionel Sambuc /// The encoding used for entries in the call-site table.
3314684ddb6SLionel Sambuc unsigned char callsite_encoding;
3324684ddb6SLionel Sambuc };
3334684ddb6SLionel Sambuc
3344684ddb6SLionel Sambuc /**
3354684ddb6SLionel Sambuc * Parse the header on the language-specific data area and return a structure
3364684ddb6SLionel Sambuc * containing the addresses and encodings of the various tables.
3374684ddb6SLionel Sambuc */
parse_lsda(_Unwind_Context * context,unsigned char * data)3384684ddb6SLionel Sambuc static inline struct dwarf_eh_lsda parse_lsda(_Unwind_Context *context,
3394684ddb6SLionel Sambuc unsigned char *data)
3404684ddb6SLionel Sambuc {
3414684ddb6SLionel Sambuc struct dwarf_eh_lsda lsda;
3424684ddb6SLionel Sambuc
343*0a6a1f1dSLionel Sambuc lsda.region_start = reinterpret_cast<dw_eh_ptr_t>(_Unwind_GetRegionStart(context));
3444684ddb6SLionel Sambuc
3454684ddb6SLionel Sambuc // If the landing pads are relative to anything other than the start of
3464684ddb6SLionel Sambuc // this region, find out where. This is @LPStart in the spec, although the
3474684ddb6SLionel Sambuc // encoding that GCC uses does not quite match the spec.
348*0a6a1f1dSLionel Sambuc uint64_t v = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(lsda.region_start));
3494684ddb6SLionel Sambuc read_value_with_encoding(context, &data, &v);
350*0a6a1f1dSLionel Sambuc lsda.landing_pads = reinterpret_cast<dw_eh_ptr_t>(static_cast<uintptr_t>(v));
3514684ddb6SLionel Sambuc
3524684ddb6SLionel Sambuc // If there is a type table, find out where it is. This is @TTBase in the
3534684ddb6SLionel Sambuc // spec. Note: we find whether there is a type table pointer by checking
3544684ddb6SLionel Sambuc // whether the leading byte is DW_EH_PE_omit (0xff), which is not what the
3554684ddb6SLionel Sambuc // spec says, but does seem to be how G++ indicates this.
3564684ddb6SLionel Sambuc lsda.type_table = 0;
3574684ddb6SLionel Sambuc lsda.type_table_encoding = *data++;
3584684ddb6SLionel Sambuc if (lsda.type_table_encoding != DW_EH_PE_omit)
3594684ddb6SLionel Sambuc {
3604684ddb6SLionel Sambuc v = read_uleb128(&data);
3614684ddb6SLionel Sambuc dw_eh_ptr_t type_table = data;
3624684ddb6SLionel Sambuc type_table += v;
3634684ddb6SLionel Sambuc lsda.type_table = type_table;
3644684ddb6SLionel Sambuc //lsda.type_table = (uintptr_t*)(data + v);
3654684ddb6SLionel Sambuc }
366*0a6a1f1dSLionel Sambuc #if defined(__arm__) && !defined(__ARM_DWARF_EH__)
3674684ddb6SLionel Sambuc lsda.type_table_encoding = (DW_EH_PE_pcrel | DW_EH_PE_indirect);
3684684ddb6SLionel Sambuc #endif
3694684ddb6SLionel Sambuc
370*0a6a1f1dSLionel Sambuc lsda.callsite_encoding = static_cast<enum dwarf_data_encoding>(*(data++));
3714684ddb6SLionel Sambuc
3724684ddb6SLionel Sambuc // Action table is immediately after the call site table
3734684ddb6SLionel Sambuc lsda.action_table = data;
374*0a6a1f1dSLionel Sambuc uintptr_t callsite_size = static_cast<uintptr_t>(read_uleb128(&data));
3754684ddb6SLionel Sambuc lsda.action_table = data + callsite_size;
3764684ddb6SLionel Sambuc // Call site table is immediately after the header
377*0a6a1f1dSLionel Sambuc lsda.call_site_table = static_cast<dw_eh_ptr_t>(data);
3784684ddb6SLionel Sambuc
3794684ddb6SLionel Sambuc
3804684ddb6SLionel Sambuc return lsda;
3814684ddb6SLionel Sambuc }
3824684ddb6SLionel Sambuc
3834684ddb6SLionel Sambuc /**
3844684ddb6SLionel Sambuc * Structure representing an action to be performed while unwinding. This
3854684ddb6SLionel Sambuc * contains the address that should be unwound to and the action record that
3864684ddb6SLionel Sambuc * provoked this action.
3874684ddb6SLionel Sambuc */
3884684ddb6SLionel Sambuc struct dwarf_eh_action
3894684ddb6SLionel Sambuc {
3904684ddb6SLionel Sambuc /**
3914684ddb6SLionel Sambuc * The address that this action directs should be the new program counter
3924684ddb6SLionel Sambuc * value after unwinding.
3934684ddb6SLionel Sambuc */
3944684ddb6SLionel Sambuc dw_eh_ptr_t landing_pad;
3954684ddb6SLionel Sambuc /// The address of the action record.
3964684ddb6SLionel Sambuc dw_eh_ptr_t action_record;
3974684ddb6SLionel Sambuc };
3984684ddb6SLionel Sambuc
3994684ddb6SLionel Sambuc /**
4004684ddb6SLionel Sambuc * Look up the landing pad that corresponds to the current invoke.
4014684ddb6SLionel Sambuc * Returns true if record exists. The context is provided by the generic
4024684ddb6SLionel Sambuc * unwind library and the lsda should be the result of a call to parse_lsda().
4034684ddb6SLionel Sambuc *
4044684ddb6SLionel Sambuc * The action record is returned via the result parameter.
4054684ddb6SLionel Sambuc */
dwarf_eh_find_callsite(struct _Unwind_Context * context,struct dwarf_eh_lsda * lsda,struct dwarf_eh_action * result)4064684ddb6SLionel Sambuc static bool dwarf_eh_find_callsite(struct _Unwind_Context *context,
4074684ddb6SLionel Sambuc struct dwarf_eh_lsda *lsda,
4084684ddb6SLionel Sambuc struct dwarf_eh_action *result)
4094684ddb6SLionel Sambuc {
4104684ddb6SLionel Sambuc result->action_record = 0;
4114684ddb6SLionel Sambuc result->landing_pad = 0;
4124684ddb6SLionel Sambuc // The current instruction pointer offset within the region
4134684ddb6SLionel Sambuc uint64_t ip = _Unwind_GetIP(context) - _Unwind_GetRegionStart(context);
414*0a6a1f1dSLionel Sambuc unsigned char *callsite_table = static_cast<unsigned char*>(lsda->call_site_table);
4154684ddb6SLionel Sambuc
4164684ddb6SLionel Sambuc while (callsite_table <= lsda->action_table)
4174684ddb6SLionel Sambuc {
4184684ddb6SLionel Sambuc // Once again, the layout deviates from the spec.
4194684ddb6SLionel Sambuc uint64_t call_site_start, call_site_size, landing_pad, action;
4204684ddb6SLionel Sambuc call_site_start = read_value(lsda->callsite_encoding, &callsite_table);
4214684ddb6SLionel Sambuc call_site_size = read_value(lsda->callsite_encoding, &callsite_table);
4224684ddb6SLionel Sambuc
4234684ddb6SLionel Sambuc // Call site entries are sorted, so if we find a call site that's after
4244684ddb6SLionel Sambuc // the current instruction pointer then there is no action associated
4254684ddb6SLionel Sambuc // with this call and we should unwind straight through this frame
4264684ddb6SLionel Sambuc // without doing anything.
4274684ddb6SLionel Sambuc if (call_site_start > ip) { break; }
4284684ddb6SLionel Sambuc
4294684ddb6SLionel Sambuc // Read the address of the landing pad and the action from the call
4304684ddb6SLionel Sambuc // site table.
4314684ddb6SLionel Sambuc landing_pad = read_value(lsda->callsite_encoding, &callsite_table);
4324684ddb6SLionel Sambuc action = read_uleb128(&callsite_table);
4334684ddb6SLionel Sambuc
4344684ddb6SLionel Sambuc // We should not include the call_site_start (beginning of the region)
4354684ddb6SLionel Sambuc // address in the ip range. For each call site:
4364684ddb6SLionel Sambuc //
4374684ddb6SLionel Sambuc // address1: call proc
4384684ddb6SLionel Sambuc // address2: next instruction
4394684ddb6SLionel Sambuc //
4404684ddb6SLionel Sambuc // The call stack contains address2 and not address1, address1 can be
4414684ddb6SLionel Sambuc // at the end of another EH region.
4424684ddb6SLionel Sambuc if (call_site_start < ip && ip <= call_site_start + call_site_size)
4434684ddb6SLionel Sambuc {
4444684ddb6SLionel Sambuc if (action)
4454684ddb6SLionel Sambuc {
4464684ddb6SLionel Sambuc // Action records are 1-biased so both no-record and zeroth
4474684ddb6SLionel Sambuc // record can be stored.
4484684ddb6SLionel Sambuc result->action_record = lsda->action_table + action - 1;
4494684ddb6SLionel Sambuc }
4504684ddb6SLionel Sambuc // No landing pad means keep unwinding.
4514684ddb6SLionel Sambuc if (landing_pad)
4524684ddb6SLionel Sambuc {
4534684ddb6SLionel Sambuc // Landing pad is the offset from the value in the header
4544684ddb6SLionel Sambuc result->landing_pad = lsda->landing_pads + landing_pad;
4554684ddb6SLionel Sambuc }
4564684ddb6SLionel Sambuc return true;
4574684ddb6SLionel Sambuc }
4584684ddb6SLionel Sambuc }
4594684ddb6SLionel Sambuc return false;
4604684ddb6SLionel Sambuc }
4614684ddb6SLionel Sambuc
4624684ddb6SLionel Sambuc /// Defines an exception class from 8 bytes (endian independent)
4634684ddb6SLionel Sambuc #define EXCEPTION_CLASS(a,b,c,d,e,f,g,h) \
464*0a6a1f1dSLionel Sambuc ((static_cast<uint64_t>(a) << 56) +\
465*0a6a1f1dSLionel Sambuc (static_cast<uint64_t>(b) << 48) +\
466*0a6a1f1dSLionel Sambuc (static_cast<uint64_t>(c) << 40) +\
467*0a6a1f1dSLionel Sambuc (static_cast<uint64_t>(d) << 32) +\
468*0a6a1f1dSLionel Sambuc (static_cast<uint64_t>(e) << 24) +\
469*0a6a1f1dSLionel Sambuc (static_cast<uint64_t>(f) << 16) +\
470*0a6a1f1dSLionel Sambuc (static_cast<uint64_t>(g) << 8) +\
471*0a6a1f1dSLionel Sambuc (static_cast<uint64_t>(h)))
4724684ddb6SLionel Sambuc
4734684ddb6SLionel Sambuc #define GENERIC_EXCEPTION_CLASS(e,f,g,h) \
474*0a6a1f1dSLionel Sambuc (static_cast<uint32_t>(e) << 24) +\
475*0a6a1f1dSLionel Sambuc (static_cast<uint32_t>(f) << 16) +\
476*0a6a1f1dSLionel Sambuc (static_cast<uint32_t>(g) << 8) +\
477*0a6a1f1dSLionel Sambuc (static_cast<uint32_t>(h))
478