194e3ee44SDavid Chisnall /*
294e3ee44SDavid Chisnall * Copyright 2010-2011 PathScale, Inc. All rights reserved.
394e3ee44SDavid Chisnall *
494e3ee44SDavid Chisnall * Redistribution and use in source and binary forms, with or without
594e3ee44SDavid Chisnall * modification, are permitted provided that the following conditions are met:
694e3ee44SDavid Chisnall *
794e3ee44SDavid Chisnall * 1. Redistributions of source code must retain the above copyright notice,
894e3ee44SDavid Chisnall * this list of conditions and the following disclaimer.
994e3ee44SDavid Chisnall *
1094e3ee44SDavid Chisnall * 2. Redistributions in binary form must reproduce the above copyright notice,
1194e3ee44SDavid Chisnall * this list of conditions and the following disclaimer in the documentation
1294e3ee44SDavid Chisnall * and/or other materials provided with the distribution.
1394e3ee44SDavid Chisnall *
1494e3ee44SDavid Chisnall * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
1594e3ee44SDavid Chisnall * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1694e3ee44SDavid Chisnall * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1794e3ee44SDavid Chisnall * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
1894e3ee44SDavid Chisnall * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1994e3ee44SDavid Chisnall * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2094e3ee44SDavid Chisnall * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2194e3ee44SDavid Chisnall * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2294e3ee44SDavid Chisnall * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2394e3ee44SDavid Chisnall * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
2494e3ee44SDavid Chisnall * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2594e3ee44SDavid Chisnall */
267a984708SDavid Chisnall /**
277a984708SDavid Chisnall * dwarf_eh.h - Defines some helper functions for parsing DWARF exception
287a984708SDavid Chisnall * handling tables.
297a984708SDavid Chisnall *
307a984708SDavid Chisnall * This file contains various helper functions that are independent of the
317a984708SDavid Chisnall * language-specific code. It can be used in any personality function for the
327a984708SDavid Chisnall * Itanium ABI.
337a984708SDavid Chisnall */
347a984708SDavid Chisnall #include <assert.h>
357a984708SDavid Chisnall
367a984708SDavid Chisnall // TODO: Factor out Itanium / ARM differences. We probably want an itanium.h
377a984708SDavid Chisnall // and arm.h that can be included by this file depending on the target ABI.
387a984708SDavid Chisnall
397a984708SDavid Chisnall // _GNU_SOURCE must be defined for unwind.h to expose some of the functions
407a984708SDavid Chisnall // that we want. If it isn't, then we define it and undefine it to make sure
417a984708SDavid Chisnall // that it doesn't impact the rest of the program.
427a984708SDavid Chisnall #ifndef _GNU_SOURCE
437a984708SDavid Chisnall # define _GNU_SOURCE 1
447a984708SDavid Chisnall # include "unwind.h"
457a984708SDavid Chisnall # undef _GNU_SOURCE
467a984708SDavid Chisnall #else
477a984708SDavid Chisnall # include "unwind.h"
487a984708SDavid Chisnall #endif
497a984708SDavid Chisnall
507a984708SDavid Chisnall #include <stdint.h>
517a984708SDavid Chisnall
527a984708SDavid Chisnall /// Type used for pointers into DWARF data
537a984708SDavid Chisnall typedef unsigned char *dw_eh_ptr_t;
547a984708SDavid Chisnall
557a984708SDavid Chisnall // Flag indicating a signed quantity
567a984708SDavid Chisnall #define DW_EH_PE_signed 0x08
577a984708SDavid Chisnall /// DWARF data encoding types.
587a984708SDavid Chisnall enum dwarf_data_encoding
597a984708SDavid Chisnall {
604bab9fd9SDavid Chisnall /// Absolute pointer value
614bab9fd9SDavid Chisnall DW_EH_PE_absptr = 0x00,
627a984708SDavid Chisnall /// Unsigned, little-endian, base 128-encoded (variable length).
637a984708SDavid Chisnall DW_EH_PE_uleb128 = 0x01,
647a984708SDavid Chisnall /// Unsigned 16-bit integer.
657a984708SDavid Chisnall DW_EH_PE_udata2 = 0x02,
667a984708SDavid Chisnall /// Unsigned 32-bit integer.
677a984708SDavid Chisnall DW_EH_PE_udata4 = 0x03,
687a984708SDavid Chisnall /// Unsigned 64-bit integer.
697a984708SDavid Chisnall DW_EH_PE_udata8 = 0x04,
707a984708SDavid Chisnall /// Signed, little-endian, base 128-encoded (variable length)
717a984708SDavid Chisnall DW_EH_PE_sleb128 = DW_EH_PE_uleb128 | DW_EH_PE_signed,
727a984708SDavid Chisnall /// Signed 16-bit integer.
737a984708SDavid Chisnall DW_EH_PE_sdata2 = DW_EH_PE_udata2 | DW_EH_PE_signed,
747a984708SDavid Chisnall /// Signed 32-bit integer.
757a984708SDavid Chisnall DW_EH_PE_sdata4 = DW_EH_PE_udata4 | DW_EH_PE_signed,
767a984708SDavid Chisnall /// Signed 32-bit integer.
777a984708SDavid Chisnall DW_EH_PE_sdata8 = DW_EH_PE_udata8 | DW_EH_PE_signed
787a984708SDavid Chisnall };
797a984708SDavid Chisnall
807a984708SDavid Chisnall /**
817a984708SDavid Chisnall * Returns the encoding for a DWARF EH table entry. The encoding is stored in
827a984708SDavid Chisnall * the low four of an octet. The high four bits store the addressing mode.
837a984708SDavid Chisnall */
get_encoding(unsigned char x)847a984708SDavid Chisnall static inline enum dwarf_data_encoding get_encoding(unsigned char x)
857a984708SDavid Chisnall {
86f2dc4184SDimitry Andric return static_cast<enum dwarf_data_encoding>(x & 0xf);
877a984708SDavid Chisnall }
887a984708SDavid Chisnall
897a984708SDavid Chisnall /**
907a984708SDavid Chisnall * DWARF addressing mode constants. When reading a pointer value from a DWARF
917a984708SDavid Chisnall * exception table, you must know how it is stored and what the addressing mode
927a984708SDavid Chisnall * is. The low four bits tell you the encoding, allowing you to decode a
937a984708SDavid Chisnall * number. The high four bits tell you the addressing mode, allowing you to
947a984708SDavid Chisnall * turn that number into an address in memory.
957a984708SDavid Chisnall */
967a984708SDavid Chisnall enum dwarf_data_relative
977a984708SDavid Chisnall {
987a984708SDavid Chisnall /// Value is omitted
997a984708SDavid Chisnall DW_EH_PE_omit = 0xff,
1007a984708SDavid Chisnall /// Value relative to program counter
1017a984708SDavid Chisnall DW_EH_PE_pcrel = 0x10,
1027a984708SDavid Chisnall /// Value relative to the text segment
1037a984708SDavid Chisnall DW_EH_PE_textrel = 0x20,
1047a984708SDavid Chisnall /// Value relative to the data segment
1057a984708SDavid Chisnall DW_EH_PE_datarel = 0x30,
1067a984708SDavid Chisnall /// Value relative to the start of the function
1077a984708SDavid Chisnall DW_EH_PE_funcrel = 0x40,
1087a984708SDavid Chisnall /// Aligned pointer (Not supported yet - are they actually used?)
1097a984708SDavid Chisnall DW_EH_PE_aligned = 0x50,
1107a984708SDavid Chisnall /// Pointer points to address of real value
1117a984708SDavid Chisnall DW_EH_PE_indirect = 0x80
1127a984708SDavid Chisnall };
1137a984708SDavid Chisnall /**
1147a984708SDavid Chisnall * Returns the addressing mode component of this encoding.
1157a984708SDavid Chisnall */
get_base(unsigned char x)1167a984708SDavid Chisnall static inline enum dwarf_data_relative get_base(unsigned char x)
1177a984708SDavid Chisnall {
118f2dc4184SDimitry Andric return static_cast<enum dwarf_data_relative>(x & 0x70);
1197a984708SDavid Chisnall }
1207a984708SDavid Chisnall /**
1217a984708SDavid Chisnall * Returns whether an encoding represents an indirect address.
1227a984708SDavid Chisnall */
is_indirect(unsigned char x)1237a984708SDavid Chisnall static int is_indirect(unsigned char x)
1247a984708SDavid Chisnall {
1257a984708SDavid Chisnall return ((x & DW_EH_PE_indirect) == DW_EH_PE_indirect);
1267a984708SDavid Chisnall }
1277a984708SDavid Chisnall
1287a984708SDavid Chisnall /**
1297a984708SDavid Chisnall * Returns the size of a fixed-size encoding. This function will abort if
1307a984708SDavid Chisnall * called with a value that is not a fixed-size encoding.
1317a984708SDavid Chisnall */
dwarf_size_of_fixed_size_field(unsigned char type)1327a984708SDavid Chisnall static inline int dwarf_size_of_fixed_size_field(unsigned char type)
1337a984708SDavid Chisnall {
1347a984708SDavid Chisnall switch (get_encoding(type))
1357a984708SDavid Chisnall {
1367a984708SDavid Chisnall default: abort();
1377a984708SDavid Chisnall case DW_EH_PE_sdata2:
1387a984708SDavid Chisnall case DW_EH_PE_udata2: return 2;
1397a984708SDavid Chisnall case DW_EH_PE_sdata4:
1407a984708SDavid Chisnall case DW_EH_PE_udata4: return 4;
1417a984708SDavid Chisnall case DW_EH_PE_sdata8:
1427a984708SDavid Chisnall case DW_EH_PE_udata8: return 8;
1437a984708SDavid Chisnall case DW_EH_PE_absptr: return sizeof(void*);
1447a984708SDavid Chisnall }
1457a984708SDavid Chisnall }
1467a984708SDavid Chisnall
1477a984708SDavid Chisnall /**
1487a984708SDavid Chisnall * Read an unsigned, little-endian, base-128, DWARF value. Updates *data to
1497a984708SDavid Chisnall * point to the end of the value. Stores the number of bits read in the value
1507a984708SDavid Chisnall * pointed to by b, allowing you to determine the value of the highest bit, and
1517a984708SDavid Chisnall * therefore the sign of a signed value.
1527a984708SDavid Chisnall *
1537a984708SDavid Chisnall * This function is not intended to be called directly. Use read_sleb128() or
1547a984708SDavid Chisnall * read_uleb128() for reading signed and unsigned versions, respectively.
1557a984708SDavid Chisnall */
read_leb128(dw_eh_ptr_t * data,int * b)1567a984708SDavid Chisnall static uint64_t read_leb128(dw_eh_ptr_t *data, int *b)
1577a984708SDavid Chisnall {
1587a984708SDavid Chisnall uint64_t uleb = 0;
1597a984708SDavid Chisnall unsigned int bit = 0;
1607a984708SDavid Chisnall unsigned char digit = 0;
1617a984708SDavid Chisnall // We have to read at least one octet, and keep reading until we get to one
1627a984708SDavid Chisnall // with the high bit unset
1637a984708SDavid Chisnall do
1647a984708SDavid Chisnall {
1657a984708SDavid Chisnall // This check is a bit too strict - we should also check the highest
1667a984708SDavid Chisnall // bit of the digit.
1677a984708SDavid Chisnall assert(bit < sizeof(uint64_t) * 8);
1687a984708SDavid Chisnall // Get the base 128 digit
1697a984708SDavid Chisnall digit = (**data) & 0x7f;
1707a984708SDavid Chisnall // Add it to the current value
1717a984708SDavid Chisnall uleb += digit << bit;
1727a984708SDavid Chisnall // Increase the shift value
1737a984708SDavid Chisnall bit += 7;
1747a984708SDavid Chisnall // Proceed to the next octet
1757a984708SDavid Chisnall (*data)++;
1767a984708SDavid Chisnall // Terminate when we reach a value that does not have the high bit set
1777a984708SDavid Chisnall // (i.e. which was not modified when we mask it with 0x7f)
1787a984708SDavid Chisnall } while ((*(*data - 1)) != digit);
1797a984708SDavid Chisnall *b = bit;
1807a984708SDavid Chisnall
1817a984708SDavid Chisnall return uleb;
1827a984708SDavid Chisnall }
1837a984708SDavid Chisnall
1847a984708SDavid Chisnall /**
1857a984708SDavid Chisnall * Reads an unsigned little-endian base-128 value starting at the address
1867a984708SDavid Chisnall * pointed to by *data. Updates *data to point to the next byte after the end
1877a984708SDavid Chisnall * of the variable-length value.
1887a984708SDavid Chisnall */
read_uleb128(dw_eh_ptr_t * data)1897a984708SDavid Chisnall static int64_t read_uleb128(dw_eh_ptr_t *data)
1907a984708SDavid Chisnall {
1917a984708SDavid Chisnall int b;
1927a984708SDavid Chisnall return read_leb128(data, &b);
1937a984708SDavid Chisnall }
1947a984708SDavid Chisnall
1957a984708SDavid Chisnall /**
1967a984708SDavid Chisnall * Reads a signed little-endian base-128 value starting at the address pointed
1977a984708SDavid Chisnall * to by *data. Updates *data to point to the next byte after the end of the
1987a984708SDavid Chisnall * variable-length value.
1997a984708SDavid Chisnall */
read_sleb128(dw_eh_ptr_t * data)2007a984708SDavid Chisnall static int64_t read_sleb128(dw_eh_ptr_t *data)
2017a984708SDavid Chisnall {
2027a984708SDavid Chisnall int bits;
2037a984708SDavid Chisnall // Read as if it's signed
2047a984708SDavid Chisnall uint64_t uleb = read_leb128(data, &bits);
2057a984708SDavid Chisnall // If the most significant bit read is 1, then we need to sign extend it
2067a984708SDavid Chisnall if ((uleb >> (bits-1)) == 1)
2077a984708SDavid Chisnall {
2087a984708SDavid Chisnall // Sign extend by setting all bits in front of it to 1
209f2dc4184SDimitry Andric uleb |= static_cast<int64_t>(-1) << bits;
2107a984708SDavid Chisnall }
211f2dc4184SDimitry Andric return static_cast<int64_t>(uleb);
2127a984708SDavid Chisnall }
2137a984708SDavid Chisnall /**
2147a984708SDavid Chisnall * Reads a value using the specified encoding from the address pointed to by
2157a984708SDavid Chisnall * *data. Updates the value of *data to point to the next byte after the end
2167a984708SDavid Chisnall * of the data.
2177a984708SDavid Chisnall */
read_value(char encoding,dw_eh_ptr_t * data)2187a984708SDavid Chisnall static uint64_t read_value(char encoding, dw_eh_ptr_t *data)
2197a984708SDavid Chisnall {
2207a984708SDavid Chisnall enum dwarf_data_encoding type = get_encoding(encoding);
2217a984708SDavid Chisnall switch (type)
2227a984708SDavid Chisnall {
2237a984708SDavid Chisnall // Read fixed-length types
2247a984708SDavid Chisnall #define READ(dwarf, type) \
2257a984708SDavid Chisnall case dwarf:\
226*a0f4b91bSDimitry Andric {\
227*a0f4b91bSDimitry Andric type t;\
228*a0f4b91bSDimitry Andric memcpy(&t, *data, sizeof t);\
229*a0f4b91bSDimitry Andric *data += sizeof t;\
230*a0f4b91bSDimitry Andric return static_cast<uint64_t>(t);\
231*a0f4b91bSDimitry Andric }
2327a984708SDavid Chisnall READ(DW_EH_PE_udata2, uint16_t)
2337a984708SDavid Chisnall READ(DW_EH_PE_udata4, uint32_t)
2347a984708SDavid Chisnall READ(DW_EH_PE_udata8, uint64_t)
2357a984708SDavid Chisnall READ(DW_EH_PE_sdata2, int16_t)
2367a984708SDavid Chisnall READ(DW_EH_PE_sdata4, int32_t)
2377a984708SDavid Chisnall READ(DW_EH_PE_sdata8, int64_t)
2387a984708SDavid Chisnall READ(DW_EH_PE_absptr, intptr_t)
2397a984708SDavid Chisnall #undef READ
2407a984708SDavid Chisnall // Read variable-length types
2417a984708SDavid Chisnall case DW_EH_PE_sleb128:
242*a0f4b91bSDimitry Andric return read_sleb128(data);
2437a984708SDavid Chisnall case DW_EH_PE_uleb128:
244*a0f4b91bSDimitry Andric return read_uleb128(data);
2457a984708SDavid Chisnall default: abort();
2467a984708SDavid Chisnall }
2477a984708SDavid Chisnall }
2487a984708SDavid Chisnall
2497a984708SDavid Chisnall /**
2507a984708SDavid Chisnall * Resolves an indirect value. This expects an unwind context, an encoding, a
2517a984708SDavid Chisnall * decoded value, and the start of the region as arguments. The returned value
2527a984708SDavid Chisnall * is a pointer to the address identified by the encoded value.
2537a984708SDavid Chisnall *
2547a984708SDavid Chisnall * If the encoding does not specify an indirect value, then this returns v.
2557a984708SDavid Chisnall */
resolve_indirect_value(_Unwind_Context * c,unsigned char encoding,int64_t v,dw_eh_ptr_t start)2567a984708SDavid Chisnall static uint64_t resolve_indirect_value(_Unwind_Context *c,
2577a984708SDavid Chisnall unsigned char encoding,
2587a984708SDavid Chisnall int64_t v,
2597a984708SDavid Chisnall dw_eh_ptr_t start)
2607a984708SDavid Chisnall {
2617a984708SDavid Chisnall switch (get_base(encoding))
2627a984708SDavid Chisnall {
2637a984708SDavid Chisnall case DW_EH_PE_pcrel:
264f2dc4184SDimitry Andric v += reinterpret_cast<uint64_t>(start);
2657a984708SDavid Chisnall break;
2667a984708SDavid Chisnall case DW_EH_PE_textrel:
267f2dc4184SDimitry Andric v += static_cast<uint64_t>(static_cast<uintptr_t>(_Unwind_GetTextRelBase(c)));
2687a984708SDavid Chisnall break;
2697a984708SDavid Chisnall case DW_EH_PE_datarel:
270f2dc4184SDimitry Andric v += static_cast<uint64_t>(static_cast<uintptr_t>(_Unwind_GetDataRelBase(c)));
2717a984708SDavid Chisnall break;
2727a984708SDavid Chisnall case DW_EH_PE_funcrel:
273f2dc4184SDimitry Andric v += static_cast<uint64_t>(static_cast<uintptr_t>(_Unwind_GetRegionStart(c)));
2747a984708SDavid Chisnall default:
2757a984708SDavid Chisnall break;
2767a984708SDavid Chisnall }
2777a984708SDavid Chisnall // If this is an indirect value, then it is really the address of the real
2787a984708SDavid Chisnall // value
2797a984708SDavid Chisnall // TODO: Check whether this should really always be a pointer - it seems to
2807a984708SDavid Chisnall // be a GCC extensions, so not properly documented...
2817a984708SDavid Chisnall if (is_indirect(encoding))
2827a984708SDavid Chisnall {
283f2dc4184SDimitry Andric v = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(*reinterpret_cast<void**>(v)));
2847a984708SDavid Chisnall }
2857a984708SDavid Chisnall return v;
2867a984708SDavid Chisnall }
2877a984708SDavid Chisnall
2887a984708SDavid Chisnall
2897a984708SDavid Chisnall /**
2907a984708SDavid Chisnall * Reads an encoding and a value, updating *data to point to the next byte.
2917a984708SDavid Chisnall */
read_value_with_encoding(_Unwind_Context * context,dw_eh_ptr_t * data,uint64_t * out)2927a984708SDavid Chisnall static inline void read_value_with_encoding(_Unwind_Context *context,
2937a984708SDavid Chisnall dw_eh_ptr_t *data,
2947a984708SDavid Chisnall uint64_t *out)
2957a984708SDavid Chisnall {
2967a984708SDavid Chisnall dw_eh_ptr_t start = *data;
2977a984708SDavid Chisnall unsigned char encoding = *((*data)++);
2987a984708SDavid Chisnall // If this value is omitted, skip it and don't touch the output value
2997a984708SDavid Chisnall if (encoding == DW_EH_PE_omit) { return; }
3007a984708SDavid Chisnall
3017a984708SDavid Chisnall *out = read_value(encoding, data);
3027a984708SDavid Chisnall *out = resolve_indirect_value(context, encoding, *out, start);
3037a984708SDavid Chisnall }
3047a984708SDavid Chisnall
3057a984708SDavid Chisnall /**
3067a984708SDavid Chisnall * Structure storing a decoded language-specific data area. Use parse_lsda()
3077a984708SDavid Chisnall * to generate an instance of this structure from the address returned by the
3087a984708SDavid Chisnall * generic unwind library.
3097a984708SDavid Chisnall *
3107a984708SDavid Chisnall * You should not need to inspect the fields of this structure directly if you
3117a984708SDavid Chisnall * are just using this header. The structure stores the locations of the
3127a984708SDavid Chisnall * various tables used for unwinding exceptions and is used by the functions
3137a984708SDavid Chisnall * for reading values from these tables.
3147a984708SDavid Chisnall */
3157a984708SDavid Chisnall struct dwarf_eh_lsda
3167a984708SDavid Chisnall {
3177a984708SDavid Chisnall /// The start of the region. This is a cache of the value returned by
3187a984708SDavid Chisnall /// _Unwind_GetRegionStart().
3197a984708SDavid Chisnall dw_eh_ptr_t region_start;
3207a984708SDavid Chisnall /// The start of the landing pads table.
3217a984708SDavid Chisnall dw_eh_ptr_t landing_pads;
3227a984708SDavid Chisnall /// The start of the type table.
3237a984708SDavid Chisnall dw_eh_ptr_t type_table;
3247a984708SDavid Chisnall /// The encoding used for entries in the type tables.
3257a984708SDavid Chisnall unsigned char type_table_encoding;
3267a984708SDavid Chisnall /// The location of the call-site table.
3277a984708SDavid Chisnall dw_eh_ptr_t call_site_table;
3287a984708SDavid Chisnall /// The location of the action table.
3297a984708SDavid Chisnall dw_eh_ptr_t action_table;
3307a984708SDavid Chisnall /// The encoding used for entries in the call-site table.
3317a984708SDavid Chisnall unsigned char callsite_encoding;
3327a984708SDavid Chisnall };
3337a984708SDavid Chisnall
3347a984708SDavid Chisnall /**
3357a984708SDavid Chisnall * Parse the header on the language-specific data area and return a structure
3367a984708SDavid Chisnall * containing the addresses and encodings of the various tables.
3377a984708SDavid Chisnall */
parse_lsda(_Unwind_Context * context,unsigned char * data)3387a984708SDavid Chisnall static inline struct dwarf_eh_lsda parse_lsda(_Unwind_Context *context,
3397a984708SDavid Chisnall unsigned char *data)
3407a984708SDavid Chisnall {
3417a984708SDavid Chisnall struct dwarf_eh_lsda lsda;
3427a984708SDavid Chisnall
343f2dc4184SDimitry Andric lsda.region_start = reinterpret_cast<dw_eh_ptr_t>(_Unwind_GetRegionStart(context));
3447a984708SDavid Chisnall
3457a984708SDavid Chisnall // If the landing pads are relative to anything other than the start of
3467a984708SDavid Chisnall // this region, find out where. This is @LPStart in the spec, although the
3477a984708SDavid Chisnall // encoding that GCC uses does not quite match the spec.
348f2dc4184SDimitry Andric uint64_t v = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(lsda.region_start));
3497a984708SDavid Chisnall read_value_with_encoding(context, &data, &v);
350f2dc4184SDimitry Andric lsda.landing_pads = reinterpret_cast<dw_eh_ptr_t>(static_cast<uintptr_t>(v));
3517a984708SDavid Chisnall
3527a984708SDavid Chisnall // If there is a type table, find out where it is. This is @TTBase in the
3537a984708SDavid Chisnall // spec. Note: we find whether there is a type table pointer by checking
3547a984708SDavid Chisnall // whether the leading byte is DW_EH_PE_omit (0xff), which is not what the
3557a984708SDavid Chisnall // spec says, but does seem to be how G++ indicates this.
3567a984708SDavid Chisnall lsda.type_table = 0;
3577a984708SDavid Chisnall lsda.type_table_encoding = *data++;
3587a984708SDavid Chisnall if (lsda.type_table_encoding != DW_EH_PE_omit)
3597a984708SDavid Chisnall {
3607a984708SDavid Chisnall v = read_uleb128(&data);
3617a984708SDavid Chisnall dw_eh_ptr_t type_table = data;
3627a984708SDavid Chisnall type_table += v;
3637a984708SDavid Chisnall lsda.type_table = type_table;
3647a984708SDavid Chisnall //lsda.type_table = (uintptr_t*)(data + v);
3657a984708SDavid Chisnall }
366f2dc4184SDimitry Andric #if defined(__arm__) && !defined(__ARM_DWARF_EH__)
3677a984708SDavid Chisnall lsda.type_table_encoding = (DW_EH_PE_pcrel | DW_EH_PE_indirect);
3687a984708SDavid Chisnall #endif
3697a984708SDavid Chisnall
370f2dc4184SDimitry Andric lsda.callsite_encoding = static_cast<enum dwarf_data_encoding>(*(data++));
3717a984708SDavid Chisnall
3727a984708SDavid Chisnall // Action table is immediately after the call site table
3737a984708SDavid Chisnall lsda.action_table = data;
374f2dc4184SDimitry Andric uintptr_t callsite_size = static_cast<uintptr_t>(read_uleb128(&data));
3757a984708SDavid Chisnall lsda.action_table = data + callsite_size;
3767a984708SDavid Chisnall // Call site table is immediately after the header
377f2dc4184SDimitry Andric lsda.call_site_table = static_cast<dw_eh_ptr_t>(data);
3787a984708SDavid Chisnall
3797a984708SDavid Chisnall
3807a984708SDavid Chisnall return lsda;
3817a984708SDavid Chisnall }
3827a984708SDavid Chisnall
3837a984708SDavid Chisnall /**
3847a984708SDavid Chisnall * Structure representing an action to be performed while unwinding. This
3857a984708SDavid Chisnall * contains the address that should be unwound to and the action record that
3867a984708SDavid Chisnall * provoked this action.
3877a984708SDavid Chisnall */
3887a984708SDavid Chisnall struct dwarf_eh_action
3897a984708SDavid Chisnall {
3907a984708SDavid Chisnall /**
3917a984708SDavid Chisnall * The address that this action directs should be the new program counter
3927a984708SDavid Chisnall * value after unwinding.
3937a984708SDavid Chisnall */
3947a984708SDavid Chisnall dw_eh_ptr_t landing_pad;
3957a984708SDavid Chisnall /// The address of the action record.
3967a984708SDavid Chisnall dw_eh_ptr_t action_record;
3977a984708SDavid Chisnall };
3987a984708SDavid Chisnall
3997a984708SDavid Chisnall /**
4007a984708SDavid Chisnall * Look up the landing pad that corresponds to the current invoke.
4017a984708SDavid Chisnall * Returns true if record exists. The context is provided by the generic
4027a984708SDavid Chisnall * unwind library and the lsda should be the result of a call to parse_lsda().
4037a984708SDavid Chisnall *
4047a984708SDavid Chisnall * The action record is returned via the result parameter.
4057a984708SDavid Chisnall */
dwarf_eh_find_callsite(struct _Unwind_Context * context,struct dwarf_eh_lsda * lsda,struct dwarf_eh_action * result)4067a984708SDavid Chisnall static bool dwarf_eh_find_callsite(struct _Unwind_Context *context,
4077a984708SDavid Chisnall struct dwarf_eh_lsda *lsda,
4087a984708SDavid Chisnall struct dwarf_eh_action *result)
4097a984708SDavid Chisnall {
4107a984708SDavid Chisnall result->action_record = 0;
4117a984708SDavid Chisnall result->landing_pad = 0;
4127a984708SDavid Chisnall // The current instruction pointer offset within the region
4137a984708SDavid Chisnall uint64_t ip = _Unwind_GetIP(context) - _Unwind_GetRegionStart(context);
414f2dc4184SDimitry Andric unsigned char *callsite_table = static_cast<unsigned char*>(lsda->call_site_table);
4157a984708SDavid Chisnall
4167a984708SDavid Chisnall while (callsite_table <= lsda->action_table)
4177a984708SDavid Chisnall {
4187a984708SDavid Chisnall // Once again, the layout deviates from the spec.
4197a984708SDavid Chisnall uint64_t call_site_start, call_site_size, landing_pad, action;
4207a984708SDavid Chisnall call_site_start = read_value(lsda->callsite_encoding, &callsite_table);
4217a984708SDavid Chisnall call_site_size = read_value(lsda->callsite_encoding, &callsite_table);
4227a984708SDavid Chisnall
4237a984708SDavid Chisnall // Call site entries are sorted, so if we find a call site that's after
4247a984708SDavid Chisnall // the current instruction pointer then there is no action associated
4257a984708SDavid Chisnall // with this call and we should unwind straight through this frame
4267a984708SDavid Chisnall // without doing anything.
4277a984708SDavid Chisnall if (call_site_start > ip) { break; }
4287a984708SDavid Chisnall
4297a984708SDavid Chisnall // Read the address of the landing pad and the action from the call
4307a984708SDavid Chisnall // site table.
4317a984708SDavid Chisnall landing_pad = read_value(lsda->callsite_encoding, &callsite_table);
4327a984708SDavid Chisnall action = read_uleb128(&callsite_table);
4337a984708SDavid Chisnall
4347a984708SDavid Chisnall // We should not include the call_site_start (beginning of the region)
4357a984708SDavid Chisnall // address in the ip range. For each call site:
4367a984708SDavid Chisnall //
4377a984708SDavid Chisnall // address1: call proc
4387a984708SDavid Chisnall // address2: next instruction
4397a984708SDavid Chisnall //
4407a984708SDavid Chisnall // The call stack contains address2 and not address1, address1 can be
4417a984708SDavid Chisnall // at the end of another EH region.
4427a984708SDavid Chisnall if (call_site_start < ip && ip <= call_site_start + call_site_size)
4437a984708SDavid Chisnall {
4447a984708SDavid Chisnall if (action)
4457a984708SDavid Chisnall {
4467a984708SDavid Chisnall // Action records are 1-biased so both no-record and zeroth
4477a984708SDavid Chisnall // record can be stored.
4487a984708SDavid Chisnall result->action_record = lsda->action_table + action - 1;
4497a984708SDavid Chisnall }
4507a984708SDavid Chisnall // No landing pad means keep unwinding.
4517a984708SDavid Chisnall if (landing_pad)
4527a984708SDavid Chisnall {
4537a984708SDavid Chisnall // Landing pad is the offset from the value in the header
4547a984708SDavid Chisnall result->landing_pad = lsda->landing_pads + landing_pad;
4557a984708SDavid Chisnall }
4567a984708SDavid Chisnall return true;
4577a984708SDavid Chisnall }
4587a984708SDavid Chisnall }
4597a984708SDavid Chisnall return false;
4607a984708SDavid Chisnall }
4617a984708SDavid Chisnall
4627a984708SDavid Chisnall /// Defines an exception class from 8 bytes (endian independent)
4637a984708SDavid Chisnall #define EXCEPTION_CLASS(a,b,c,d,e,f,g,h) \
464f2dc4184SDimitry Andric ((static_cast<uint64_t>(a) << 56) +\
465f2dc4184SDimitry Andric (static_cast<uint64_t>(b) << 48) +\
466f2dc4184SDimitry Andric (static_cast<uint64_t>(c) << 40) +\
467f2dc4184SDimitry Andric (static_cast<uint64_t>(d) << 32) +\
468f2dc4184SDimitry Andric (static_cast<uint64_t>(e) << 24) +\
469f2dc4184SDimitry Andric (static_cast<uint64_t>(f) << 16) +\
470f2dc4184SDimitry Andric (static_cast<uint64_t>(g) << 8) +\
471f2dc4184SDimitry Andric (static_cast<uint64_t>(h)))
4727a984708SDavid Chisnall
4737a984708SDavid Chisnall #define GENERIC_EXCEPTION_CLASS(e,f,g,h) \
474f2dc4184SDimitry Andric (static_cast<uint32_t>(e) << 24) +\
475f2dc4184SDimitry Andric (static_cast<uint32_t>(f) << 16) +\
476f2dc4184SDimitry Andric (static_cast<uint32_t>(g) << 8) +\
477f2dc4184SDimitry Andric (static_cast<uint32_t>(h))
478