···11+/* GCC core type declarations.
22+ Copyright (C) 2002, 2004, 2007, 2008, 2009, 2010
33+ Free Software Foundation, Inc.
44+55+This file is part of GCC.
66+77+GCC is free software; you can redistribute it and/or modify it under
88+the terms of the GNU General Public License as published by the Free
99+Software Foundation; either version 3, or (at your option) any later
1010+version.
1111+1212+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
1313+WARRANTY; without even the implied warranty of MERCHANTABILITY or
1414+FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1515+for more details.
1616+1717+Under Section 7 of GPL version 3, you are granted additional
1818+permissions described in the GCC Runtime Library Exception, version
1919+3.1, as published by the Free Software Foundation.
2020+2121+You should have received a copy of the GNU General Public License and
2222+a copy of the GCC Runtime Library Exception along with this program;
2323+see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
2424+<http://www.gnu.org/licenses/>. */
2525+2626+/* Provide forward declarations of core types which are referred to by
2727+ most of the compiler. This allows header files to use these types
2828+ (e.g. in function prototypes) without concern for whether the full
2929+ definitions are visible. Some other declarations that need to be
3030+ universally visible are here, too.
3131+3232+ In the context of tconfig.h, most of these have special definitions
3333+ which prevent them from being used except in further type
3434+ declarations. This is a kludge; the right thing is to avoid
3535+ including the "tm.h" header set in the context of tconfig.h, but
3636+ we're not there yet. */
3737+3838+#ifndef GCC_CORETYPES_H
3939+#define GCC_CORETYPES_H
4040+4141+#ifndef GTY
4242+#define GTY(x) /* nothing - marker for gengtype */
4343+#endif
4444+4545+#ifndef USED_FOR_TARGET
4646+4747+struct bitmap_head_def;
4848+typedef struct bitmap_head_def *bitmap;
4949+typedef const struct bitmap_head_def *const_bitmap;
5050+struct simple_bitmap_def;
5151+typedef struct simple_bitmap_def *sbitmap;
5252+typedef const struct simple_bitmap_def *const_sbitmap;
5353+struct rtx_def;
5454+typedef struct rtx_def *rtx;
5555+typedef const struct rtx_def *const_rtx;
5656+struct rtvec_def;
5757+typedef struct rtvec_def *rtvec;
5858+typedef const struct rtvec_def *const_rtvec;
5959+union tree_node;
6060+typedef union tree_node *tree;
6161+union gimple_statement_d;
6262+typedef union gimple_statement_d *gimple;
6363+typedef const union tree_node *const_tree;
6464+typedef const union gimple_statement_d *const_gimple;
6565+union section;
6666+typedef union section section;
6767+struct gcc_options;
6868+struct cl_target_option;
6969+struct cl_optimization;
7070+struct cl_option;
7171+struct cl_decoded_option;
7272+struct cl_option_handlers;
7373+struct diagnostic_context;
7474+typedef struct diagnostic_context diagnostic_context;
7575+struct gimple_seq_d;
7676+typedef struct gimple_seq_d *gimple_seq;
7777+typedef const struct gimple_seq_d *const_gimple_seq;
7878+7979+/* Address space number for named address space support. */
8080+typedef unsigned char addr_space_t;
8181+8282+/* The value of addr_space_t that represents the generic address space. */
8383+#define ADDR_SPACE_GENERIC 0
8484+#define ADDR_SPACE_GENERIC_P(AS) ((AS) == ADDR_SPACE_GENERIC)
8585+8686+/* The major intermediate representations of GCC. */
8787+enum ir_type {
8888+ IR_GIMPLE,
8989+ IR_RTL_CFGRTL,
9090+ IR_RTL_CFGLAYOUT
9191+};
9292+9393+/* Provide forward struct declaration so that we don't have to include
9494+ all of cpplib.h whenever a random prototype includes a pointer.
9595+ Note that the cpp_reader and cpp_token typedefs remain part of
9696+ cpplib.h. */
9797+9898+struct cpp_reader;
9999+struct cpp_token;
100100+101101+/* The thread-local storage model associated with a given VAR_DECL
102102+ or SYMBOL_REF. This isn't used much, but both trees and RTL refer
103103+ to it, so it's here. */
104104+enum tls_model {
105105+ TLS_MODEL_NONE,
106106+ TLS_MODEL_EMULATED,
107107+ TLS_MODEL_REAL,
108108+ TLS_MODEL_GLOBAL_DYNAMIC = TLS_MODEL_REAL,
109109+ TLS_MODEL_LOCAL_DYNAMIC,
110110+ TLS_MODEL_INITIAL_EXEC,
111111+ TLS_MODEL_LOCAL_EXEC
112112+};
113113+114114+/* Types of unwind/exception handling info that can be generated. */
115115+116116+enum unwind_info_type
117117+{
118118+ UI_NONE,
119119+ UI_SJLJ,
120120+ UI_DWARF2,
121121+ UI_TARGET
122122+};
123123+124124+/* Callgraph node profile representation. */
125125+enum node_frequency {
126126+ /* This function most likely won't be executed at all.
127127+ (set only when profile feedback is available or via function attribute). */
128128+ NODE_FREQUENCY_UNLIKELY_EXECUTED,
129129+ /* For functions that are known to be executed once (i.e. constructors, destructors
130130+ and main function. */
131131+ NODE_FREQUENCY_EXECUTED_ONCE,
132132+ /* The default value. */
133133+ NODE_FREQUENCY_NORMAL,
134134+ /* Optimize this function hard
135135+ (set only when profile feedback is available or via function attribute). */
136136+ NODE_FREQUENCY_HOT
137137+};
138138+139139+140140+struct edge_def;
141141+typedef struct edge_def *edge;
142142+typedef const struct edge_def *const_edge;
143143+struct basic_block_def;
144144+typedef struct basic_block_def *basic_block;
145145+typedef const struct basic_block_def *const_basic_block;
146146+147147+#define obstack_chunk_alloc ((void *(*) (long)) xmalloc)
148148+#define obstack_chunk_free ((void (*) (void *)) free)
149149+#define OBSTACK_CHUNK_SIZE 0
150150+#define gcc_obstack_init(OBSTACK) \
151151+ _obstack_begin ((OBSTACK), OBSTACK_CHUNK_SIZE, 0, \
152152+ obstack_chunk_alloc, \
153153+ obstack_chunk_free)
154154+155155+/* enum reg_class is target specific, so it should not appear in
156156+ target-independent code or interfaces, like the target hook declarations
157157+ in target.h. */
158158+typedef int reg_class_t;
159159+160160+#else
161161+162162+struct _dont_use_rtx_here_;
163163+struct _dont_use_rtvec_here_;
164164+union _dont_use_tree_here_;
165165+#define rtx struct _dont_use_rtx_here_ *
166166+#define const_rtx struct _dont_use_rtx_here_ *
167167+#define rtvec struct _dont_use_rtvec_here *
168168+#define const_rtvec struct _dont_use_rtvec_here *
169169+#define tree union _dont_use_tree_here_ *
170170+#define const_tree union _dont_use_tree_here_ *
171171+172172+#endif
173173+174174+#endif /* coretypes.h */
175175+
···11+/* Definitions of target machine for GNU compiler,
22+ for Motorola M*CORE Processor.
33+ Copyright (C) 1993, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007,
44+ 2008, 2009, 2010 Free Software Foundation, Inc.
55+66+ This file is part of GCC.
77+88+ GCC is free software; you can redistribute it and/or modify it
99+ under the terms of the GNU General Public License as published
1010+ by the Free Software Foundation; either version 3, or (at your
1111+ option) any later version.
1212+1313+ GCC is distributed in the hope that it will be useful, but WITHOUT
1414+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1515+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
1616+ License for more details.
1717+1818+ You should have received a copy of the GNU General Public License
1919+ along with GCC; see the file COPYING3. If not see
2020+ <http://www.gnu.org/licenses/>. */
2121+2222+#ifndef GCC_MCORE_H
2323+#define GCC_MCORE_H
2424+2525+/* RBE: need to move these elsewhere. */
2626+#undef LIKE_PPC_ABI
2727+#define MCORE_STRUCT_ARGS
2828+/* RBE: end of "move elsewhere". */
2929+3030+/* Run-time Target Specification. */
3131+#define TARGET_MCORE
3232+3333+/* Get tree.c to declare a target-specific specialization of
3434+ merge_decl_attributes. */
3535+#define TARGET_DLLIMPORT_DECL_ATTRIBUTES 1
3636+3737+#define TARGET_CPU_CPP_BUILTINS() \
3838+ do \
3939+ { \
4040+ builtin_define ("__mcore__"); \
4141+ builtin_define ("__MCORE__"); \
4242+ if (TARGET_LITTLE_END) \
4343+ builtin_define ("__MCORELE__"); \
4444+ else \
4545+ builtin_define ("__MCOREBE__"); \
4646+ if (TARGET_M340) \
4747+ builtin_define ("__M340__"); \
4848+ else \
4949+ builtin_define ("__M210__"); \
5050+ } \
5151+ while (0)
5252+5353+#undef CPP_SPEC
5454+#define CPP_SPEC "%{m210:%{mlittle-endian:%ethe m210 does not have little endian support}}"
5555+5656+/* We don't have a -lg library, so don't put it in the list. */
5757+#undef LIB_SPEC
5858+#define LIB_SPEC "%{!shared: %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}}"
5959+6060+#undef ASM_SPEC
6161+#define ASM_SPEC "%{mbig-endian:-EB} %{m210:-cpu=210 -EB}"
6262+6363+#undef LINK_SPEC
6464+#define LINK_SPEC "%{mbig-endian:-EB} %{m210:-EB} -X"
6565+6666+#define TARGET_DEFAULT \
6767+ (MASK_HARDLIT \
6868+ | MASK_DIV \
6969+ | MASK_RELAX_IMM \
7070+ | MASK_M340 \
7171+ | MASK_LITTLE_END)
7272+7373+#ifndef MULTILIB_DEFAULTS
7474+#define MULTILIB_DEFAULTS { "mlittle-endian", "m340" }
7575+#endif
7676+7777+/* The ability to have 4 byte alignment is being suppressed for now.
7878+ If this ability is reenabled, you must disable the definition below
7979+ *and* edit t-mcore to enable multilibs for 4 byte alignment code. */
8080+#undef TARGET_8ALIGN
8181+#define TARGET_8ALIGN 1
8282+8383+extern char * mcore_current_function_name;
8484+8585+/* The MCore ABI says that bitfields are unsigned by default. */
8686+#define CC1_SPEC "-funsigned-bitfields"
8787+8888+/* Target machine storage Layout. */
8989+9090+#define PROMOTE_MODE(MODE,UNSIGNEDP,TYPE) \
9191+ if (GET_MODE_CLASS (MODE) == MODE_INT \
9292+ && GET_MODE_SIZE (MODE) < UNITS_PER_WORD) \
9393+ { \
9494+ (MODE) = SImode; \
9595+ (UNSIGNEDP) = 1; \
9696+ }
9797+9898+/* Define this if most significant bit is lowest numbered
9999+ in instructions that operate on numbered bit-fields. */
100100+#define BITS_BIG_ENDIAN 0
101101+102102+/* Define this if most significant byte of a word is the lowest numbered. */
103103+#define BYTES_BIG_ENDIAN (! TARGET_LITTLE_END)
104104+105105+/* Define this if most significant word of a multiword number is the lowest
106106+ numbered. */
107107+#define WORDS_BIG_ENDIAN (! TARGET_LITTLE_END)
108108+109109+#define MAX_BITS_PER_WORD 32
110110+111111+/* Width of a word, in units (bytes). */
112112+#define UNITS_PER_WORD 4
113113+114114+/* A C expression for the size in bits of the type `long long' on the
115115+ target machine. If you don't define this, the default is two
116116+ words. */
117117+#define LONG_LONG_TYPE_SIZE 64
118118+119119+/* Allocation boundary (in *bits*) for storing arguments in argument list. */
120120+#define PARM_BOUNDARY 32
121121+122122+/* Boundary (in *bits*) on which stack pointer should be aligned. */
123123+#define STACK_BOUNDARY (TARGET_8ALIGN ? 64 : 32)
124124+125125+/* Largest increment in UNITS we allow the stack to grow in a single operation. */
126126+#define STACK_UNITS_MAXSTEP 4096
127127+128128+/* Allocation boundary (in *bits*) for the code of a function. */
129129+#define FUNCTION_BOUNDARY ((TARGET_OVERALIGN_FUNC) ? 32 : 16)
130130+131131+/* Alignment of field after `int : 0' in a structure. */
132132+#define EMPTY_FIELD_BOUNDARY 32
133133+134134+/* No data type wants to be aligned rounder than this. */
135135+#define BIGGEST_ALIGNMENT (TARGET_8ALIGN ? 64 : 32)
136136+137137+/* The best alignment to use in cases where we have a choice. */
138138+#define FASTEST_ALIGNMENT 32
139139+140140+/* Every structures size must be a multiple of 8 bits. */
141141+#define STRUCTURE_SIZE_BOUNDARY 8
142142+143143+/* Look at the fundamental type that is used for a bit-field and use
144144+ that to impose alignment on the enclosing structure.
145145+ struct s {int a:8}; should have same alignment as "int", not "char". */
146146+#define PCC_BITFIELD_TYPE_MATTERS 1
147147+148148+/* Largest integer machine mode for structures. If undefined, the default
149149+ is GET_MODE_SIZE(DImode). */
150150+#define MAX_FIXED_MODE_SIZE 32
151151+152152+/* Make strings word-aligned so strcpy from constants will be faster. */
153153+#define CONSTANT_ALIGNMENT(EXP, ALIGN) \
154154+ ((TREE_CODE (EXP) == STRING_CST \
155155+ && (ALIGN) < FASTEST_ALIGNMENT) \
156156+ ? FASTEST_ALIGNMENT : (ALIGN))
157157+158158+/* Make arrays of chars word-aligned for the same reasons. */
159159+#define DATA_ALIGNMENT(TYPE, ALIGN) \
160160+ (TREE_CODE (TYPE) == ARRAY_TYPE \
161161+ && TYPE_MODE (TREE_TYPE (TYPE)) == QImode \
162162+ && (ALIGN) < FASTEST_ALIGNMENT ? FASTEST_ALIGNMENT : (ALIGN))
163163+164164+/* Set this nonzero if move instructions will actually fail to work
165165+ when given unaligned data. */
166166+#define STRICT_ALIGNMENT 1
167167+168168+/* Standard register usage. */
169169+170170+/* Register allocation for our first guess
171171+172172+ r0 stack pointer
173173+ r1 scratch, target reg for xtrb?
174174+ r2-r7 arguments.
175175+ r8-r14 call saved
176176+ r15 link register
177177+ ap arg pointer (doesn't really exist, always eliminated)
178178+ c c bit
179179+ fp frame pointer (doesn't really exist, always eliminated)
180180+ x19 two control registers. */
181181+182182+/* Number of actual hardware registers.
183183+ The hardware registers are assigned numbers for the compiler
184184+ from 0 to just below FIRST_PSEUDO_REGISTER.
185185+ All registers that the compiler knows about must be given numbers,
186186+ even those that are not normally considered general registers.
187187+188188+ MCore has 16 integer registers and 2 control registers + the arg
189189+ pointer. */
190190+191191+#define FIRST_PSEUDO_REGISTER 20
192192+193193+#define R1_REG 1 /* Where literals are forced. */
194194+#define LK_REG 15 /* Overloaded on general register. */
195195+#define AP_REG 16 /* Fake arg pointer register. */
196196+/* RBE: mcore.md depends on CC_REG being set to 17. */
197197+#define CC_REG 17 /* Can't name it C_REG. */
198198+#define FP_REG 18 /* Fake frame pointer register. */
199199+200200+/* Specify the registers used for certain standard purposes.
201201+ The values of these macros are register numbers. */
202202+203203+204204+#undef PC_REGNUM /* Define this if the program counter is overloaded on a register. */
205205+#define STACK_POINTER_REGNUM 0 /* Register to use for pushing function arguments. */
206206+#define FRAME_POINTER_REGNUM 8 /* When we need FP, use r8. */
207207+208208+/* The assembler's names for the registers. RFP need not always be used as
209209+ the Real framepointer; it can also be used as a normal general register.
210210+ Note that the name `fp' is horribly misleading since `fp' is in fact only
211211+ the argument-and-return-context pointer. */
212212+#define REGISTER_NAMES \
213213+{ \
214214+ "sp", "r1", "r2", "r3", "r4", "r5", "r6", "r7", \
215215+ "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", \
216216+ "apvirtual", "c", "fpvirtual", "x19" \
217217+}
218218+219219+/* 1 for registers that have pervasive standard uses
220220+ and are not available for the register allocator. */
221221+#define FIXED_REGISTERS \
222222+ /* r0 r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13 r14 r15 ap c fp x19 */ \
223223+ { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1}
224224+225225+/* 1 for registers not available across function calls.
226226+ These must include the FIXED_REGISTERS and also any
227227+ registers that can be used without being saved.
228228+ The latter must include the registers where values are returned
229229+ and the register where structure-value addresses are passed.
230230+ Aside from that, you can include as many other registers as you like. */
231231+232232+/* RBE: r15 {link register} not available across calls,
233233+ But we don't mark it that way here.... */
234234+#define CALL_USED_REGISTERS \
235235+ /* r0 r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13 r14 r15 ap c fp x19 */ \
236236+ { 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1}
237237+238238+/* The order in which register should be allocated. */
239239+#define REG_ALLOC_ORDER \
240240+ /* r7 r6 r5 r4 r3 r2 r15 r14 r13 r12 r11 r10 r9 r8 r1 r0 ap c fp x19*/ \
241241+ { 7, 6, 5, 4, 3, 2, 15, 14, 13, 12, 11, 10, 9, 8, 1, 0, 16, 17, 18, 19}
242242+243243+/* Return number of consecutive hard regs needed starting at reg REGNO
244244+ to hold something of mode MODE.
245245+ This is ordinarily the length in words of a value of mode MODE
246246+ but can be less for certain modes in special long registers.
247247+248248+ On the MCore regs are UNITS_PER_WORD bits wide; */
249249+#define HARD_REGNO_NREGS(REGNO, MODE) \
250250+ (((GET_MODE_SIZE (MODE) + UNITS_PER_WORD - 1) / UNITS_PER_WORD))
251251+252252+/* Value is 1 if hard register REGNO can hold a value of machine-mode MODE.
253253+ We may keep double values in even registers. */
254254+#define HARD_REGNO_MODE_OK(REGNO, MODE) \
255255+ ((TARGET_8ALIGN && GET_MODE_SIZE (MODE) > UNITS_PER_WORD) ? (((REGNO) & 1) == 0) : (REGNO < 18))
256256+257257+/* Value is 1 if it is a good idea to tie two pseudo registers
258258+ when one has mode MODE1 and one has mode MODE2.
259259+ If HARD_REGNO_MODE_OK could produce different values for MODE1 and MODE2,
260260+ for any hard reg, then this must be 0 for correct output. */
261261+#define MODES_TIEABLE_P(MODE1, MODE2) \
262262+ ((MODE1) == (MODE2) || GET_MODE_CLASS (MODE1) == GET_MODE_CLASS (MODE2))
263263+264264+/* Definitions for register eliminations.
265265+266266+ We have two registers that can be eliminated on the MCore. First, the
267267+ frame pointer register can often be eliminated in favor of the stack
268268+ pointer register. Secondly, the argument pointer register can always be
269269+ eliminated; it is replaced with either the stack or frame pointer. */
270270+271271+/* Base register for access to arguments of the function. */
272272+#define ARG_POINTER_REGNUM 16
273273+274274+/* Register in which the static-chain is passed to a function. */
275275+#define STATIC_CHAIN_REGNUM 1
276276+277277+/* This is an array of structures. Each structure initializes one pair
278278+ of eliminable registers. The "from" register number is given first,
279279+ followed by "to". Eliminations of the same "from" register are listed
280280+ in order of preference. */
281281+#define ELIMINABLE_REGS \
282282+{{ FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM}, \
283283+ { ARG_POINTER_REGNUM, STACK_POINTER_REGNUM}, \
284284+ { ARG_POINTER_REGNUM, FRAME_POINTER_REGNUM},}
285285+286286+/* Define the offset between two registers, one to be eliminated, and the other
287287+ its replacement, at the start of a routine. */
288288+#define INITIAL_ELIMINATION_OFFSET(FROM, TO, OFFSET) \
289289+ OFFSET = mcore_initial_elimination_offset (FROM, TO)
290290+291291+/* Define the classes of registers for register constraints in the
292292+ machine description. Also define ranges of constants.
293293+294294+ One of the classes must always be named ALL_REGS and include all hard regs.
295295+ If there is more than one class, another class must be named NO_REGS
296296+ and contain no registers.
297297+298298+ The name GENERAL_REGS must be the name of a class (or an alias for
299299+ another name such as ALL_REGS). This is the class of registers
300300+ that is allowed by "g" or "r" in a register constraint.
301301+ Also, registers outside this class are allocated only when
302302+ instructions express preferences for them.
303303+304304+ The classes must be numbered in nondecreasing order; that is,
305305+ a larger-numbered class must never be contained completely
306306+ in a smaller-numbered class.
307307+308308+ For any two classes, it is very desirable that there be another
309309+ class that represents their union. */
310310+311311+/* The MCore has only general registers. There are
312312+ also some special purpose registers: the T bit register, the
313313+ procedure Link and the Count Registers. */
314314+enum reg_class
315315+{
316316+ NO_REGS,
317317+ ONLYR1_REGS,
318318+ LRW_REGS,
319319+ GENERAL_REGS,
320320+ C_REGS,
321321+ ALL_REGS,
322322+ LIM_REG_CLASSES
323323+};
324324+325325+#define N_REG_CLASSES (int) LIM_REG_CLASSES
326326+327327+#define IRA_COVER_CLASSES \
328328+{ \
329329+ GENERAL_REGS, C_REGS, LIM_REG_CLASSES \
330330+}
331331+332332+333333+/* Give names of register classes as strings for dump file. */
334334+#define REG_CLASS_NAMES \
335335+{ \
336336+ "NO_REGS", \
337337+ "ONLYR1_REGS", \
338338+ "LRW_REGS", \
339339+ "GENERAL_REGS", \
340340+ "C_REGS", \
341341+ "ALL_REGS", \
342342+}
343343+344344+/* Define which registers fit in which classes.
345345+ This is an initializer for a vector of HARD_REG_SET
346346+ of length N_REG_CLASSES. */
347347+348348+/* ??? STACK_POINTER_REGNUM should be excluded from LRW_REGS. */
349349+#define REG_CLASS_CONTENTS \
350350+{ \
351351+ {0x000000}, /* NO_REGS */ \
352352+ {0x000002}, /* ONLYR1_REGS */ \
353353+ {0x007FFE}, /* LRW_REGS */ \
354354+ {0x01FFFF}, /* GENERAL_REGS */ \
355355+ {0x020000}, /* C_REGS */ \
356356+ {0x0FFFFF} /* ALL_REGS */ \
357357+}
358358+359359+/* The same information, inverted:
360360+ Return the class number of the smallest class containing
361361+ reg number REGNO. This could be a conditional expression
362362+ or could index an array. */
363363+364364+extern const enum reg_class regno_reg_class[FIRST_PSEUDO_REGISTER];
365365+#define REGNO_REG_CLASS(REGNO) regno_reg_class[REGNO]
366366+367367+/* When this hook returns true for MODE, the compiler allows
368368+ registers explicitly used in the rtl to be used as spill registers
369369+ but prevents the compiler from extending the lifetime of these
370370+ registers. */
371371+#define TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P hook_bool_mode_true
372372+373373+/* The class value for index registers, and the one for base regs. */
374374+#define INDEX_REG_CLASS NO_REGS
375375+#define BASE_REG_CLASS GENERAL_REGS
376376+377377+/* Convenience wrappers around insn_const_int_ok_for_constraint. */
378378+#define CONST_OK_FOR_I(VALUE) \
379379+ insn_const_int_ok_for_constraint (VALUE, CONSTRAINT_I)
380380+#define CONST_OK_FOR_J(VALUE) \
381381+ insn_const_int_ok_for_constraint (VALUE, CONSTRAINT_J)
382382+#define CONST_OK_FOR_L(VALUE) \
383383+ insn_const_int_ok_for_constraint (VALUE, CONSTRAINT_L)
384384+#define CONST_OK_FOR_K(VALUE) \
385385+ insn_const_int_ok_for_constraint (VALUE, CONSTRAINT_K)
386386+#define CONST_OK_FOR_M(VALUE) \
387387+ insn_const_int_ok_for_constraint (VALUE, CONSTRAINT_M)
388388+#define CONST_OK_FOR_N(VALUE) \
389389+ insn_const_int_ok_for_constraint (VALUE, CONSTRAINT_N)
390390+#define CONST_OK_FOR_O(VALUE) \
391391+ insn_const_int_ok_for_constraint (VALUE, CONSTRAINT_O)
392392+#define CONST_OK_FOR_P(VALUE) \
393393+ insn_const_int_ok_for_constraint (VALUE, CONSTRAINT_P)
394394+395395+/* Given an rtx X being reloaded into a reg required to be
396396+ in class CLASS, return the class of reg to actually use.
397397+ In general this is just CLASS; but on some machines
398398+ in some cases it is preferable to use a more restrictive class. */
399399+#define PREFERRED_RELOAD_CLASS(X, CLASS) mcore_reload_class (X, CLASS)
400400+401401+/* Return the register class of a scratch register needed to copy IN into
402402+ or out of a register in CLASS in MODE. If it can be done directly,
403403+ NO_REGS is returned. */
404404+#define SECONDARY_RELOAD_CLASS(CLASS, MODE, X) \
405405+ mcore_secondary_reload_class (CLASS, MODE, X)
406406+407407+/* Return the maximum number of consecutive registers
408408+ needed to represent mode MODE in a register of class CLASS.
409409+410410+ On MCore this is the size of MODE in words. */
411411+#define CLASS_MAX_NREGS(CLASS, MODE) \
412412+ (ROUND_ADVANCE (GET_MODE_SIZE (MODE)))
413413+414414+/* Stack layout; function entry, exit and calling. */
415415+416416+/* Define the number of register that can hold parameters.
417417+ These two macros are used only in other macro definitions below. */
418418+#define NPARM_REGS 6
419419+#define FIRST_PARM_REG 2
420420+#define FIRST_RET_REG 2
421421+422422+/* Define this if pushing a word on the stack
423423+ makes the stack pointer a smaller address. */
424424+#define STACK_GROWS_DOWNWARD
425425+426426+/* Offset within stack frame to start allocating local variables at.
427427+ If FRAME_GROWS_DOWNWARD, this is the offset to the END of the
428428+ first local allocated. Otherwise, it is the offset to the BEGINNING
429429+ of the first local allocated. */
430430+#define STARTING_FRAME_OFFSET 0
431431+432432+/* If defined, the maximum amount of space required for outgoing arguments
433433+ will be computed and placed into the variable
434434+ `crtl->outgoing_args_size'. No space will be pushed
435435+ onto the stack for each call; instead, the function prologue should
436436+ increase the stack frame size by this amount. */
437437+#define ACCUMULATE_OUTGOING_ARGS 1
438438+439439+/* Offset of first parameter from the argument pointer register value. */
440440+#define FIRST_PARM_OFFSET(FNDECL) 0
441441+442442+/* Define how to find the value returned by a function.
443443+ VALTYPE is the data type of the value (as a tree).
444444+ If the precise function being called is known, FUNC is its FUNCTION_DECL;
445445+ otherwise, FUNC is 0. */
446446+#define FUNCTION_VALUE(VALTYPE, FUNC) mcore_function_value (VALTYPE, FUNC)
447447+448448+/* Don't default to pcc-struct-return, because gcc is the only compiler, and
449449+ we want to retain compatibility with older gcc versions. */
450450+#define DEFAULT_PCC_STRUCT_RETURN 0
451451+452452+/* Define how to find the value returned by a library function
453453+ assuming the value has mode MODE. */
454454+#define LIBCALL_VALUE(MODE) gen_rtx_REG (MODE, FIRST_RET_REG)
455455+456456+/* 1 if N is a possible register number for a function value.
457457+ On the MCore, only r4 can return results. */
458458+#define FUNCTION_VALUE_REGNO_P(REGNO) ((REGNO) == FIRST_RET_REG)
459459+460460+/* 1 if N is a possible register number for function argument passing. */
461461+#define FUNCTION_ARG_REGNO_P(REGNO) \
462462+ ((REGNO) >= FIRST_PARM_REG && (REGNO) < (NPARM_REGS + FIRST_PARM_REG))
463463+464464+/* Define a data type for recording info about an argument list
465465+ during the scan of that argument list. This data type should
466466+ hold all necessary information about the function itself
467467+ and about the args processed so far, enough to enable macros
468468+ such as FUNCTION_ARG to determine where the next arg should go.
469469+470470+ On MCore, this is a single integer, which is a number of words
471471+ of arguments scanned so far (including the invisible argument,
472472+ if any, which holds the structure-value-address).
473473+ Thus NARGREGS or more means all following args should go on the stack. */
474474+#define CUMULATIVE_ARGS int
475475+476476+#define ROUND_ADVANCE(SIZE) \
477477+ ((SIZE + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
478478+479479+/* Round a register number up to a proper boundary for an arg of mode
480480+ MODE.
481481+482482+ We round to an even reg for things larger than a word. */
483483+#define ROUND_REG(X, MODE) \
484484+ ((TARGET_8ALIGN \
485485+ && GET_MODE_UNIT_SIZE ((MODE)) > UNITS_PER_WORD) \
486486+ ? ((X) + ((X) & 1)) : (X))
487487+488488+489489+/* Initialize a variable CUM of type CUMULATIVE_ARGS
490490+ for a call to a function whose data type is FNTYPE.
491491+ For a library call, FNTYPE is 0.
492492+493493+ On MCore, the offset always starts at 0: the first parm reg is always
494494+ the same reg. */
495495+#define INIT_CUMULATIVE_ARGS(CUM, FNTYPE, LIBNAME, INDIRECT, N_NAMED_ARGS) \
496496+ ((CUM) = 0)
497497+498498+/* Call the function profiler with a given profile label. */
499499+#define FUNCTION_PROFILER(STREAM,LABELNO) \
500500+{ \
501501+ fprintf (STREAM, " trap 1\n"); \
502502+ fprintf (STREAM, " .align 2\n"); \
503503+ fprintf (STREAM, " .long LP%d\n", (LABELNO)); \
504504+}
505505+506506+/* EXIT_IGNORE_STACK should be nonzero if, when returning from a function,
507507+ the stack pointer does not matter. The value is tested only in
508508+ functions that have frame pointers.
509509+ No definition is equivalent to always zero. */
510510+#define EXIT_IGNORE_STACK 0
511511+512512+/* Length in units of the trampoline for entering a nested function. */
513513+#define TRAMPOLINE_SIZE 12
514514+515515+/* Alignment required for a trampoline in bits. */
516516+#define TRAMPOLINE_ALIGNMENT 32
517517+518518+/* Macros to check register numbers against specific register classes. */
519519+520520+/* These assume that REGNO is a hard or pseudo reg number.
521521+ They give nonzero only if REGNO is a hard reg of the suitable class
522522+ or a pseudo reg currently allocated to a suitable hard reg.
523523+ Since they use reg_renumber, they are safe only once reg_renumber
524524+ has been allocated, which happens in local-alloc.c. */
525525+#define REGNO_OK_FOR_BASE_P(REGNO) \
526526+ ((REGNO) < AP_REG || (unsigned) reg_renumber[(REGNO)] < AP_REG)
527527+528528+#define REGNO_OK_FOR_INDEX_P(REGNO) 0
529529+530530+/* Maximum number of registers that can appear in a valid memory
531531+ address. */
532532+#define MAX_REGS_PER_ADDRESS 1
533533+534534+/* Recognize any constant value that is a valid address. */
535535+#define CONSTANT_ADDRESS_P(X) (GET_CODE (X) == LABEL_REF)
536536+537537+/* Nonzero if the constant value X is a legitimate general operand.
538538+ It is given that X satisfies CONSTANT_P or is a CONST_DOUBLE.
539539+540540+ On the MCore, allow anything but a double. */
541541+#define LEGITIMATE_CONSTANT_P(X) (GET_CODE(X) != CONST_DOUBLE \
542542+ && CONSTANT_P (X))
543543+544544+/* The macros REG_OK_FOR..._P assume that the arg is a REG rtx
545545+ and check its validity for a certain class.
546546+ We have two alternate definitions for each of them.
547547+ The usual definition accepts all pseudo regs; the other rejects
548548+ them unless they have been allocated suitable hard regs.
549549+ The symbol REG_OK_STRICT causes the latter definition to be used. */
550550+#ifndef REG_OK_STRICT
551551+552552+/* Nonzero if X is a hard reg that can be used as a base reg
553553+ or if it is a pseudo reg. */
554554+#define REG_OK_FOR_BASE_P(X) \
555555+ (REGNO (X) <= 16 || REGNO (X) >= FIRST_PSEUDO_REGISTER)
556556+557557+/* Nonzero if X is a hard reg that can be used as an index
558558+ or if it is a pseudo reg. */
559559+#define REG_OK_FOR_INDEX_P(X) 0
560560+561561+#else
562562+563563+/* Nonzero if X is a hard reg that can be used as a base reg. */
564564+#define REG_OK_FOR_BASE_P(X) \
565565+ REGNO_OK_FOR_BASE_P (REGNO (X))
566566+567567+/* Nonzero if X is a hard reg that can be used as an index. */
568568+#define REG_OK_FOR_INDEX_P(X) 0
569569+570570+#endif
571571+/* GO_IF_LEGITIMATE_ADDRESS recognizes an RTL expression
572572+ that is a valid memory address for an instruction.
573573+ The MODE argument is the machine mode for the MEM expression
574574+ that wants to use this address.
575575+576576+ The other macros defined here are used only in GO_IF_LEGITIMATE_ADDRESS. */
577577+#define BASE_REGISTER_RTX_P(X) \
578578+ (GET_CODE (X) == REG && REG_OK_FOR_BASE_P (X))
579579+580580+#define INDEX_REGISTER_RTX_P(X) \
581581+ (GET_CODE (X) == REG && REG_OK_FOR_INDEX_P (X))
582582+583583+584584+/* Jump to LABEL if X is a valid address RTX. This must also take
585585+ REG_OK_STRICT into account when deciding about valid registers, but it uses
586586+ the above macros so we are in luck.
587587+588588+ Allow REG
589589+ REG+disp
590590+591591+ A legitimate index for a QI is 0..15, for HI is 0..30, for SI is 0..60,
592592+ and for DI is 0..56 because we use two SI loads, etc. */
593593+#define GO_IF_LEGITIMATE_INDEX(MODE, REGNO, OP, LABEL) \
594594+ do \
595595+ { \
596596+ if (GET_CODE (OP) == CONST_INT) \
597597+ { \
598598+ if (GET_MODE_SIZE (MODE) >= 4 \
599599+ && (((unsigned HOST_WIDE_INT) INTVAL (OP)) % 4) == 0 \
600600+ && ((unsigned HOST_WIDE_INT) INTVAL (OP)) \
601601+ <= (unsigned HOST_WIDE_INT) 64 - GET_MODE_SIZE (MODE)) \
602602+ goto LABEL; \
603603+ if (GET_MODE_SIZE (MODE) == 2 \
604604+ && (((unsigned HOST_WIDE_INT) INTVAL (OP)) % 2) == 0 \
605605+ && ((unsigned HOST_WIDE_INT) INTVAL (OP)) <= 30) \
606606+ goto LABEL; \
607607+ if (GET_MODE_SIZE (MODE) == 1 \
608608+ && ((unsigned HOST_WIDE_INT) INTVAL (OP)) <= 15) \
609609+ goto LABEL; \
610610+ } \
611611+ } \
612612+ while (0)
613613+614614+#define GO_IF_LEGITIMATE_ADDRESS(MODE, X, LABEL) \
615615+{ \
616616+ if (BASE_REGISTER_RTX_P (X)) \
617617+ goto LABEL; \
618618+ else if (GET_CODE (X) == PLUS || GET_CODE (X) == LO_SUM) \
619619+ { \
620620+ rtx xop0 = XEXP (X,0); \
621621+ rtx xop1 = XEXP (X,1); \
622622+ if (BASE_REGISTER_RTX_P (xop0)) \
623623+ GO_IF_LEGITIMATE_INDEX (MODE, REGNO (xop0), xop1, LABEL); \
624624+ if (BASE_REGISTER_RTX_P (xop1)) \
625625+ GO_IF_LEGITIMATE_INDEX (MODE, REGNO (xop1), xop0, LABEL); \
626626+ } \
627627+}
628628+629629+/* Specify the machine mode that this machine uses
630630+ for the index in the tablejump instruction. */
631631+#define CASE_VECTOR_MODE SImode
632632+633633+/* 'char' is signed by default. */
634634+#define DEFAULT_SIGNED_CHAR 0
635635+636636+#undef SIZE_TYPE
637637+#define SIZE_TYPE "unsigned int"
638638+639639+#undef PTRDIFF_TYPE
640640+#define PTRDIFF_TYPE "int"
641641+642642+#undef WCHAR_TYPE
643643+#define WCHAR_TYPE "long int"
644644+645645+#undef WCHAR_TYPE_SIZE
646646+#define WCHAR_TYPE_SIZE BITS_PER_WORD
647647+648648+/* Max number of bytes we can move from memory to memory
649649+ in one reasonably fast instruction. */
650650+#define MOVE_MAX 4
651651+652652+/* Define if operations between registers always perform the operation
653653+ on the full register even if a narrower mode is specified. */
654654+#define WORD_REGISTER_OPERATIONS
655655+656656+/* Define if loading in MODE, an integral mode narrower than BITS_PER_WORD
657657+ will either zero-extend or sign-extend. The value of this macro should
658658+ be the code that says which one of the two operations is implicitly
659659+ done, UNKNOWN if none. */
660660+#define LOAD_EXTEND_OP(MODE) ZERO_EXTEND
661661+662662+/* Nonzero if access to memory by bytes is slow and undesirable. */
663663+#define SLOW_BYTE_ACCESS TARGET_SLOW_BYTES
664664+665665+/* Shift counts are truncated to 6-bits (0 to 63) instead of the expected
666666+ 5-bits, so we can not define SHIFT_COUNT_TRUNCATED to true for this
667667+ target. */
668668+#define SHIFT_COUNT_TRUNCATED 0
669669+670670+/* All integers have the same format so truncation is easy. */
671671+#define TRULY_NOOP_TRUNCATION(OUTPREC,INPREC) 1
672672+673673+/* Define this if addresses of constant functions
674674+ shouldn't be put through pseudo regs where they can be cse'd.
675675+ Desirable on machines where ordinary constants are expensive
676676+ but a CALL with constant address is cheap. */
677677+/* Why is this defined??? -- dac */
678678+#define NO_FUNCTION_CSE 1
679679+680680+/* The machine modes of pointers and functions. */
681681+#define Pmode SImode
682682+#define FUNCTION_MODE Pmode
683683+684684+/* Compute extra cost of moving data between one register class
685685+ and another. All register moves are cheap. */
686686+#define REGISTER_MOVE_COST(MODE, SRCCLASS, DSTCLASS) 2
687687+688688+#define WORD_REGISTER_OPERATIONS
689689+690690+/* Assembler output control. */
691691+#define ASM_COMMENT_START "\t//"
692692+693693+#define ASM_APP_ON "// inline asm begin\n"
694694+#define ASM_APP_OFF "// inline asm end\n"
695695+696696+#define FILE_ASM_OP "\t.file\n"
697697+698698+/* Switch to the text or data segment. */
699699+#define TEXT_SECTION_ASM_OP "\t.text"
700700+#define DATA_SECTION_ASM_OP "\t.data"
701701+702702+/* Switch into a generic section. */
703703+#undef TARGET_ASM_NAMED_SECTION
704704+#define TARGET_ASM_NAMED_SECTION mcore_asm_named_section
705705+706706+#define INCOMING_RETURN_ADDR_RTX gen_rtx_REG (SImode, LK_REG)
707707+708708+/* This is how to output an insn to push a register on the stack.
709709+ It need not be very fast code. */
710710+#define ASM_OUTPUT_REG_PUSH(FILE,REGNO) \
711711+ fprintf (FILE, "\tsubi\t %s,%d\n\tstw\t %s,(%s)\n", \
712712+ reg_names[STACK_POINTER_REGNUM], \
713713+ (STACK_BOUNDARY / BITS_PER_UNIT), \
714714+ reg_names[REGNO], \
715715+ reg_names[STACK_POINTER_REGNUM])
716716+717717+/* Length in instructions of the code output by ASM_OUTPUT_REG_PUSH. */
718718+#define REG_PUSH_LENGTH 2
719719+720720+/* This is how to output an insn to pop a register from the stack. */
721721+#define ASM_OUTPUT_REG_POP(FILE,REGNO) \
722722+ fprintf (FILE, "\tldw\t %s,(%s)\n\taddi\t %s,%d\n", \
723723+ reg_names[REGNO], \
724724+ reg_names[STACK_POINTER_REGNUM], \
725725+ reg_names[STACK_POINTER_REGNUM], \
726726+ (STACK_BOUNDARY / BITS_PER_UNIT))
727727+728728+729729+/* Output a reference to a label. */
730730+#undef ASM_OUTPUT_LABELREF
731731+#define ASM_OUTPUT_LABELREF(STREAM, NAME) \
732732+ fprintf (STREAM, "%s%s", USER_LABEL_PREFIX, \
733733+ (* targetm.strip_name_encoding) (NAME))
734734+735735+/* This is how to output an assembler line
736736+ that says to advance the location counter
737737+ to a multiple of 2**LOG bytes. */
738738+#define ASM_OUTPUT_ALIGN(FILE,LOG) \
739739+ if ((LOG) != 0) \
740740+ fprintf (FILE, "\t.align\t%d\n", LOG)
741741+742742+#ifndef ASM_DECLARE_RESULT
743743+#define ASM_DECLARE_RESULT(FILE, RESULT)
744744+#endif
745745+746746+#define MULTIPLE_SYMBOL_SPACES 1
747747+748748+#define SUPPORTS_ONE_ONLY 1
749749+750750+/* A pair of macros to output things for the callgraph data.
751751+ VALUE means (to the tools that reads this info later):
752752+ 0 a call from src to dst
753753+ 1 the call is special (e.g. dst is "unknown" or "alloca")
754754+ 2 the call is special (e.g., the src is a table instead of routine)
755755+756756+ Frame sizes are augmented with timestamps to help later tools
757757+ differentiate between static entities with same names in different
758758+ files. */
759759+extern long mcore_current_compilation_timestamp;
760760+#define ASM_OUTPUT_CG_NODE(FILE,SRCNAME,VALUE) \
761761+ do \
762762+ { \
763763+ if (mcore_current_compilation_timestamp == 0) \
764764+ mcore_current_compilation_timestamp = time (0); \
765765+ fprintf ((FILE),"\t.equ\t__$frame$size$_%s_$_%08lx,%d\n", \
766766+ (SRCNAME), mcore_current_compilation_timestamp, (VALUE)); \
767767+ } \
768768+ while (0)
769769+770770+#define ASM_OUTPUT_CG_EDGE(FILE,SRCNAME,DSTNAME,VALUE) \
771771+ do \
772772+ { \
773773+ fprintf ((FILE),"\t.equ\t__$function$call$_%s_$_%s,%d\n", \
774774+ (SRCNAME), (DSTNAME), (VALUE)); \
775775+ } \
776776+ while (0)
777777+778778+/* Globalizing directive for a label. */
779779+#define GLOBAL_ASM_OP "\t.export\t"
780780+781781+/* The prefix to add to user-visible assembler symbols. */
782782+#undef USER_LABEL_PREFIX
783783+#define USER_LABEL_PREFIX ""
784784+785785+/* Make an internal label into a string. */
786786+#undef ASM_GENERATE_INTERNAL_LABEL
787787+#define ASM_GENERATE_INTERNAL_LABEL(STRING, PREFIX, NUM) \
788788+ sprintf (STRING, "*.%s%ld", PREFIX, (long) NUM)
789789+790790+/* Jump tables must be 32 bit aligned. */
791791+#undef ASM_OUTPUT_CASE_LABEL
792792+#define ASM_OUTPUT_CASE_LABEL(STREAM,PREFIX,NUM,TABLE) \
793793+ fprintf (STREAM, "\t.align 2\n.%s%d:\n", PREFIX, NUM);
794794+795795+/* Output a relative address. Not needed since jump tables are absolute
796796+ but we must define it anyway. */
797797+#define ASM_OUTPUT_ADDR_DIFF_ELT(STREAM,BODY,VALUE,REL) \
798798+ fputs ("- - - ASM_OUTPUT_ADDR_DIFF_ELT called!\n", STREAM)
799799+800800+/* Output an element of a dispatch table. */
801801+#define ASM_OUTPUT_ADDR_VEC_ELT(STREAM,VALUE) \
802802+ fprintf (STREAM, "\t.long\t.L%d\n", VALUE)
803803+804804+/* Output various types of constants. */
805805+806806+/* This is how to output an assembler line
807807+ that says to advance the location counter by SIZE bytes. */
808808+#undef ASM_OUTPUT_SKIP
809809+#define ASM_OUTPUT_SKIP(FILE,SIZE) \
810810+ fprintf (FILE, "\t.fill %d, 1\n", (int)(SIZE))
811811+812812+/* This says how to output an assembler line
813813+ to define a global common symbol, with alignment information. */
814814+/* XXX - for now we ignore the alignment. */
815815+#undef ASM_OUTPUT_ALIGNED_COMMON
816816+#define ASM_OUTPUT_ALIGNED_COMMON(FILE, NAME, SIZE, ALIGN) \
817817+ do \
818818+ { \
819819+ if (mcore_dllexport_name_p (NAME)) \
820820+ MCORE_EXPORT_NAME (FILE, NAME) \
821821+ if (! mcore_dllimport_name_p (NAME)) \
822822+ { \
823823+ fputs ("\t.comm\t", FILE); \
824824+ assemble_name (FILE, NAME); \
825825+ fprintf (FILE, ",%lu\n", (unsigned long)(SIZE)); \
826826+ } \
827827+ } \
828828+ while (0)
829829+830830+/* This says how to output an assembler line
831831+ to define a local common symbol.... */
832832+#undef ASM_OUTPUT_LOCAL
833833+#define ASM_OUTPUT_LOCAL(FILE, NAME, SIZE, ROUNDED) \
834834+ (fputs ("\t.lcomm\t", FILE), \
835835+ assemble_name (FILE, NAME), \
836836+ fprintf (FILE, ",%d\n", (int)SIZE))
837837+838838+/* ... and how to define a local common symbol whose alignment
839839+ we wish to specify. ALIGN comes in as bits, we have to turn
840840+ it into bytes. */
841841+#undef ASM_OUTPUT_ALIGNED_LOCAL
842842+#define ASM_OUTPUT_ALIGNED_LOCAL(FILE, NAME, SIZE, ALIGN) \
843843+ do \
844844+ { \
845845+ fputs ("\t.bss\t", (FILE)); \
846846+ assemble_name ((FILE), (NAME)); \
847847+ fprintf ((FILE), ",%d,%d\n", (int)(SIZE), (ALIGN) / BITS_PER_UNIT);\
848848+ } \
849849+ while (0)
850850+851851+#endif /* ! GCC_MCORE_H */
···11+#ifndef GCC_TM_H
22+#define GCC_TM_H
33+#ifndef LIBC_GLIBC
44+# define LIBC_GLIBC 1
55+#endif
66+#ifndef LIBC_UCLIBC
77+# define LIBC_UCLIBC 2
88+#endif
99+#ifndef LIBC_BIONIC
1010+# define LIBC_BIONIC 3
1111+#endif
1212+#ifndef DEFAULT_LIBC
1313+# define DEFAULT_LIBC LIBC_GLIBC
1414+#endif
1515+#ifndef ANDROID_DEFAULT
1616+# define ANDROID_DEFAULT 0
1717+#endif
1818+#ifdef IN_GCC
1919+# include "options.h"
2020+# include "insn-constants.h"
2121+# include "config/vxworks-dummy.h"
2222+# include "config/i386/biarch64.h"
2323+# include "config/i386/i386.h"
2424+# include "config/linux-android.h"
2525+# include "config/i386/unix.h"
2626+# include "config/i386/att.h"
2727+# include "config/dbxelf.h"
2828+# include "config/elfos.h"
2929+# include "config/gnu-user.h"
3030+# include "config/linux.h"
3131+# include "config/glibc-stdint.h"
3232+# include "config/i386/x86-64.h"
3333+# include "config/i386/linux64.h"
3434+#endif
3535+#if defined IN_GCC && !defined GENERATOR_FILE && !defined USED_FOR_TARGET
3636+# include "insn-flags.h"
3737+#endif
3838+# include "defaults.h"
3939+#endif /* GCC_TM_H */
src/libunwind-darwin/tsystem.h
This is a binary file and will not be displayed.
+229
src/libunwind-darwin/unwind-c.c
···11+/* Supporting functions for C exception handling.
22+ Copyright (C) 2002, 2003, 2009 Free Software Foundation, Inc.
33+ Contributed by Aldy Hernandez <aldy@quesejoda.com>.
44+ Shamelessly stolen from the Java front end.
55+66+This file is part of GCC.
77+88+GCC is free software; you can redistribute it and/or modify it under
99+the terms of the GNU General Public License as published by the Free
1010+Software Foundation; either version 3, or (at your option) any later
1111+version.
1212+1313+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
1414+WARRANTY; without even the implied warranty of MERCHANTABILITY or
1515+FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1616+for more details.
1717+1818+Under Section 7 of GPL version 3, you are granted additional
1919+permissions described in the GCC Runtime Library Exception, version
2020+3.1, as published by the Free Software Foundation.
2121+2222+You should have received a copy of the GNU General Public License and
2323+a copy of the GCC Runtime Library Exception along with this program;
2424+see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
2525+<http://www.gnu.org/licenses/>. */
2626+2727+#include "tconfig.h"
2828+#include "tsystem.h"
2929+#include "unwind.h"
3030+#define NO_SIZE_OF_ENCODED_VALUE
3131+#include "unwind-pe.h"
3232+3333+typedef struct
3434+{
3535+ _Unwind_Ptr Start;
3636+ _Unwind_Ptr LPStart;
3737+ _Unwind_Ptr ttype_base;
3838+ const unsigned char *TType;
3939+ const unsigned char *action_table;
4040+ unsigned char ttype_encoding;
4141+ unsigned char call_site_encoding;
4242+} lsda_header_info;
4343+4444+static const unsigned char *
4545+parse_lsda_header (struct _Unwind_Context *context, const unsigned char *p,
4646+ lsda_header_info *info)
4747+{
4848+ _uleb128_t tmp;
4949+ unsigned char lpstart_encoding;
5050+5151+ info->Start = (context ? _Unwind_GetRegionStart (context) : 0);
5252+5353+ /* Find @LPStart, the base to which landing pad offsets are relative. */
5454+ lpstart_encoding = *p++;
5555+ if (lpstart_encoding != DW_EH_PE_omit)
5656+ p = read_encoded_value (context, lpstart_encoding, p, &info->LPStart);
5757+ else
5858+ info->LPStart = info->Start;
5959+6060+ /* Find @TType, the base of the handler and exception spec type data. */
6161+ info->ttype_encoding = *p++;
6262+ if (info->ttype_encoding != DW_EH_PE_omit)
6363+ {
6464+ p = read_uleb128 (p, &tmp);
6565+ info->TType = p + tmp;
6666+ }
6767+ else
6868+ info->TType = 0;
6969+7070+ /* The encoding and length of the call-site table; the action table
7171+ immediately follows. */
7272+ info->call_site_encoding = *p++;
7373+ p = read_uleb128 (p, &tmp);
7474+ info->action_table = p + tmp;
7575+7676+ return p;
7777+}
7878+7979+#ifdef __ARM_EABI_UNWINDER__
8080+/* ARM EABI personality routines must also unwind the stack. */
8181+#define CONTINUE_UNWINDING \
8282+ do \
8383+ { \
8484+ if (__gnu_unwind_frame (ue_header, context) != _URC_OK) \
8585+ return _URC_FAILURE; \
8686+ return _URC_CONTINUE_UNWIND; \
8787+ } \
8888+ while (0)
8989+#else
9090+#define CONTINUE_UNWINDING return _URC_CONTINUE_UNWIND
9191+#endif
9292+9393+#ifdef __USING_SJLJ_EXCEPTIONS__
9494+#define PERSONALITY_FUNCTION __gcc_personality_sj0
9595+#define __builtin_eh_return_data_regno(x) x
9696+#else
9797+#define PERSONALITY_FUNCTION __gcc_personality_v0
9898+#endif
9999+100100+#ifdef __ARM_EABI_UNWINDER__
101101+_Unwind_Reason_Code
102102+PERSONALITY_FUNCTION (_Unwind_State, struct _Unwind_Exception *,
103103+ struct _Unwind_Context *);
104104+105105+_Unwind_Reason_Code
106106+PERSONALITY_FUNCTION (_Unwind_State state,
107107+ struct _Unwind_Exception * ue_header,
108108+ struct _Unwind_Context * context)
109109+#else
110110+_Unwind_Reason_Code
111111+PERSONALITY_FUNCTION (int, _Unwind_Action, _Unwind_Exception_Class,
112112+ struct _Unwind_Exception *, struct _Unwind_Context *);
113113+114114+_Unwind_Reason_Code
115115+PERSONALITY_FUNCTION (int version,
116116+ _Unwind_Action actions,
117117+ _Unwind_Exception_Class exception_class ATTRIBUTE_UNUSED,
118118+ struct _Unwind_Exception *ue_header,
119119+ struct _Unwind_Context *context)
120120+#endif
121121+{
122122+ lsda_header_info info;
123123+ const unsigned char *language_specific_data, *p;
124124+ _Unwind_Ptr landing_pad, ip;
125125+ int ip_before_insn = 0;
126126+127127+#ifdef __ARM_EABI_UNWINDER__
128128+ if ((state & _US_ACTION_MASK) != _US_UNWIND_FRAME_STARTING)
129129+ CONTINUE_UNWINDING;
130130+131131+ /* The dwarf unwinder assumes the context structure holds things like the
132132+ function and LSDA pointers. The ARM implementation caches these in
133133+ the exception header (UCB). To avoid rewriting everything we make the
134134+ virtual IP register point at the UCB. */
135135+ ip = (_Unwind_Ptr) ue_header;
136136+ _Unwind_SetGR (context, 12, ip);
137137+#else
138138+ if (version != 1)
139139+ return _URC_FATAL_PHASE1_ERROR;
140140+141141+ /* Currently we only support cleanups for C. */
142142+ if ((actions & _UA_CLEANUP_PHASE) == 0)
143143+ CONTINUE_UNWINDING;
144144+#endif
145145+146146+ language_specific_data = (const unsigned char *)
147147+ _Unwind_GetLanguageSpecificData (context);
148148+149149+ /* If no LSDA, then there are no handlers or cleanups. */
150150+ if (! language_specific_data)
151151+ CONTINUE_UNWINDING;
152152+153153+ /* Parse the LSDA header. */
154154+ p = parse_lsda_header (context, language_specific_data, &info);
155155+#ifdef HAVE_GETIPINFO
156156+ ip = _Unwind_GetIPInfo (context, &ip_before_insn);
157157+#else
158158+ ip = _Unwind_GetIP (context);
159159+#endif
160160+ if (! ip_before_insn)
161161+ --ip;
162162+ landing_pad = 0;
163163+164164+#ifdef __USING_SJLJ_EXCEPTIONS__
165165+ /* The given "IP" is an index into the call-site table, with two
166166+ exceptions -- -1 means no-action, and 0 means terminate. But
167167+ since we're using uleb128 values, we've not got random access
168168+ to the array. */
169169+ if ((int) ip <= 0)
170170+ return _URC_CONTINUE_UNWIND;
171171+ else
172172+ {
173173+ _uleb128_t cs_lp, cs_action;
174174+ do
175175+ {
176176+ p = read_uleb128 (p, &cs_lp);
177177+ p = read_uleb128 (p, &cs_action);
178178+ }
179179+ while (--ip);
180180+181181+ /* Can never have null landing pad for sjlj -- that would have
182182+ been indicated by a -1 call site index. */
183183+ landing_pad = (_Unwind_Ptr)cs_lp + 1;
184184+ goto found_something;
185185+ }
186186+#else
187187+ /* Search the call-site table for the action associated with this IP. */
188188+ while (p < info.action_table)
189189+ {
190190+ _Unwind_Ptr cs_start, cs_len, cs_lp;
191191+ _uleb128_t cs_action;
192192+193193+ /* Note that all call-site encodings are "absolute" displacements. */
194194+ p = read_encoded_value (0, info.call_site_encoding, p, &cs_start);
195195+ p = read_encoded_value (0, info.call_site_encoding, p, &cs_len);
196196+ p = read_encoded_value (0, info.call_site_encoding, p, &cs_lp);
197197+ p = read_uleb128 (p, &cs_action);
198198+199199+ /* The table is sorted, so if we've passed the ip, stop. */
200200+ if (ip < info.Start + cs_start)
201201+ p = info.action_table;
202202+ else if (ip < info.Start + cs_start + cs_len)
203203+ {
204204+ if (cs_lp)
205205+ landing_pad = info.LPStart + cs_lp;
206206+ goto found_something;
207207+ }
208208+ }
209209+#endif
210210+211211+ /* IP is not in table. No associated cleanups. */
212212+ /* ??? This is where C++ calls std::terminate to catch throw
213213+ from a destructor. */
214214+ CONTINUE_UNWINDING;
215215+216216+ found_something:
217217+ if (landing_pad == 0)
218218+ {
219219+ /* IP is present, but has a null landing pad.
220220+ No handler to be run. */
221221+ CONTINUE_UNWINDING;
222222+ }
223223+224224+ _Unwind_SetGR (context, __builtin_eh_return_data_regno (0),
225225+ (_Unwind_Ptr) ue_header);
226226+ _Unwind_SetGR (context, __builtin_eh_return_data_regno (1), 0);
227227+ _Unwind_SetIP (context, landing_pad);
228228+ return _URC_INSTALL_CONTEXT;
229229+}
+210
src/libunwind-darwin/unwind-compat.c
···11+/* Backward compatibility unwind routines.
22+ Copyright (C) 2004, 2005, 2006, 2009
33+ Free Software Foundation, Inc.
44+55+ This file is part of GCC.
66+77+ GCC is free software; you can redistribute it and/or modify it
88+ under the terms of the GNU General Public License as published by
99+ the Free Software Foundation; either version 3, or (at your option)
1010+ any later version.
1111+1212+ GCC is distributed in the hope that it will be useful, but WITHOUT
1313+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1414+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
1515+ License for more details.
1616+1717+ Under Section 7 of GPL version 3, you are granted additional
1818+ permissions described in the GCC Runtime Library Exception, version
1919+ 3.1, as published by the Free Software Foundation.
2020+2121+ You should have received a copy of the GNU General Public License and
2222+ a copy of the GCC Runtime Library Exception along with this program;
2323+ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
2424+ <http://www.gnu.org/licenses/>. */
2525+2626+#if defined (USE_GAS_SYMVER) && defined (USE_LIBUNWIND_EXCEPTIONS)
2727+#include "tconfig.h"
2828+#include "tsystem.h"
2929+#include "unwind.h"
3030+#include "unwind-dw2-fde.h"
3131+#include "unwind-compat.h"
3232+3333+extern _Unwind_Reason_Code __libunwind_Unwind_Backtrace
3434+ (_Unwind_Trace_Fn, void *);
3535+3636+_Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
3737+_Unwind_Backtrace (_Unwind_Trace_Fn trace, void *trace_argument)
3838+{
3939+ return __libunwind_Unwind_Backtrace (trace, trace_argument);
4040+}
4141+symver (_Unwind_Backtrace, GCC_3.3);
4242+4343+extern void __libunwind_Unwind_DeleteException
4444+ (struct _Unwind_Exception *);
4545+4646+void
4747+_Unwind_DeleteException (struct _Unwind_Exception *exc)
4848+{
4949+ return __libunwind_Unwind_DeleteException (exc);
5050+}
5151+symver (_Unwind_DeleteException, GCC_3.0);
5252+5353+extern void * __libunwind_Unwind_FindEnclosingFunction (void *);
5454+5555+void *
5656+_Unwind_FindEnclosingFunction (void *pc)
5757+{
5858+ return __libunwind_Unwind_FindEnclosingFunction (pc);
5959+}
6060+symver (_Unwind_FindEnclosingFunction, GCC_3.3);
6161+6262+extern _Unwind_Reason_Code __libunwind_Unwind_ForcedUnwind
6363+ (struct _Unwind_Exception *, _Unwind_Stop_Fn, void *);
6464+6565+_Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
6666+_Unwind_ForcedUnwind (struct _Unwind_Exception *exc,
6767+ _Unwind_Stop_Fn stop, void * stop_argument)
6868+{
6969+ return __libunwind_Unwind_ForcedUnwind (exc, stop, stop_argument);
7070+}
7171+symver (_Unwind_ForcedUnwind, GCC_3.0);
7272+7373+extern _Unwind_Word __libunwind_Unwind_GetCFA
7474+ (struct _Unwind_Context *);
7575+7676+_Unwind_Word
7777+_Unwind_GetCFA (struct _Unwind_Context *context)
7878+{
7979+ return __libunwind_Unwind_GetCFA (context);
8080+}
8181+symver (_Unwind_GetCFA, GCC_3.3);
8282+8383+#ifdef __ia64__
8484+extern _Unwind_Word __libunwind_Unwind_GetBSP
8585+ (struct _Unwind_Context *);
8686+8787+_Unwind_Word
8888+_Unwind_GetBSP (struct _Unwind_Context * context)
8989+{
9090+ return __libunwind_Unwind_GetBSP (context);
9191+}
9292+symver (_Unwind_GetBSP, GCC_3.3.2);
9393+#else
9494+extern _Unwind_Ptr __libunwind_Unwind_GetDataRelBase
9595+ (struct _Unwind_Context *);
9696+9797+_Unwind_Ptr
9898+_Unwind_GetDataRelBase (struct _Unwind_Context *context)
9999+{
100100+ return __libunwind_Unwind_GetDataRelBase (context);
101101+}
102102+symver (_Unwind_GetDataRelBase, GCC_3.0);
103103+104104+extern _Unwind_Ptr __libunwind_Unwind_GetTextRelBase
105105+ (struct _Unwind_Context *);
106106+107107+_Unwind_Ptr
108108+_Unwind_GetTextRelBase (struct _Unwind_Context *context)
109109+{
110110+ return __libunwind_Unwind_GetTextRelBase (context);
111111+}
112112+symver (_Unwind_GetTextRelBase, GCC_3.0);
113113+#endif
114114+115115+extern _Unwind_Word __libunwind_Unwind_GetGR
116116+ (struct _Unwind_Context *, int );
117117+118118+_Unwind_Word
119119+_Unwind_GetGR (struct _Unwind_Context *context, int index)
120120+{
121121+ return __libunwind_Unwind_GetGR (context, index);
122122+}
123123+symver (_Unwind_GetGR, GCC_3.0);
124124+125125+extern _Unwind_Ptr __libunwind_Unwind_GetIP (struct _Unwind_Context *);
126126+127127+_Unwind_Ptr
128128+_Unwind_GetIP (struct _Unwind_Context *context)
129129+{
130130+ return __libunwind_Unwind_GetIP (context);
131131+}
132132+symver (_Unwind_GetIP, GCC_3.0);
133133+134134+_Unwind_Ptr
135135+_Unwind_GetIPInfo (struct _Unwind_Context *context, int *ip_before_insn)
136136+{
137137+ *ip_before_insn = 0;
138138+ return __libunwind_Unwind_GetIP (context);
139139+}
140140+141141+extern void *__libunwind_Unwind_GetLanguageSpecificData
142142+ (struct _Unwind_Context *);
143143+144144+void *
145145+_Unwind_GetLanguageSpecificData (struct _Unwind_Context *context)
146146+{
147147+ return __libunwind_Unwind_GetLanguageSpecificData (context);
148148+}
149149+symver (_Unwind_GetLanguageSpecificData, GCC_3.0);
150150+151151+extern _Unwind_Ptr __libunwind_Unwind_GetRegionStart
152152+ (struct _Unwind_Context *);
153153+154154+_Unwind_Ptr
155155+_Unwind_GetRegionStart (struct _Unwind_Context *context)
156156+{
157157+ return __libunwind_Unwind_GetRegionStart (context);
158158+}
159159+symver (_Unwind_GetRegionStart, GCC_3.0);
160160+161161+extern _Unwind_Reason_Code __libunwind_Unwind_RaiseException
162162+ (struct _Unwind_Exception *);
163163+164164+_Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
165165+_Unwind_RaiseException(struct _Unwind_Exception *exc)
166166+{
167167+ return __libunwind_Unwind_RaiseException (exc);
168168+}
169169+symver (_Unwind_RaiseException, GCC_3.0);
170170+171171+extern void __libunwind_Unwind_Resume (struct _Unwind_Exception *);
172172+173173+void LIBGCC2_UNWIND_ATTRIBUTE
174174+_Unwind_Resume (struct _Unwind_Exception *exc)
175175+{
176176+ __libunwind_Unwind_Resume (exc);
177177+}
178178+symver (_Unwind_Resume, GCC_3.0);
179179+180180+extern _Unwind_Reason_Code __libunwind_Unwind_Resume_or_Rethrow
181181+ (struct _Unwind_Exception *);
182182+183183+_Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
184184+_Unwind_Resume_or_Rethrow (struct _Unwind_Exception *exc)
185185+{
186186+ return __libunwind_Unwind_Resume_or_Rethrow (exc);
187187+}
188188+symver (_Unwind_Resume_or_Rethrow, GCC_3.3);
189189+190190+extern void __libunwind_Unwind_SetGR
191191+ (struct _Unwind_Context *, int, _Unwind_Word);
192192+193193+void
194194+_Unwind_SetGR (struct _Unwind_Context *context, int index,
195195+ _Unwind_Word val)
196196+{
197197+ __libunwind_Unwind_SetGR (context, index, val);
198198+}
199199+symver (_Unwind_SetGR, GCC_3.0);
200200+201201+extern void __libunwind_Unwind_SetIP
202202+ (struct _Unwind_Context *, _Unwind_Ptr);
203203+204204+void
205205+_Unwind_SetIP (struct _Unwind_Context *context, _Unwind_Ptr val)
206206+{
207207+ return __libunwind_Unwind_SetIP (context, val);
208208+}
209209+symver (_Unwind_SetIP, GCC_3.0);
210210+#endif
+30
src/libunwind-darwin/unwind-compat.h
···11+/* Backward compatibility unwind routines.
22+ Copyright (C) 2004, 2009
33+ Free Software Foundation, Inc.
44+55+ This file is part of GCC.
66+77+ GCC is free software; you can redistribute it and/or modify it
88+ under the terms of the GNU General Public License as published by
99+ the Free Software Foundation; either version 3, or (at your option)
1010+ any later version.
1111+1212+ GCC is distributed in the hope that it will be useful, but WITHOUT
1313+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1414+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
1515+ License for more details.
1616+1717+ Under Section 7 of GPL version 3, you are granted additional
1818+ permissions described in the GCC Runtime Library Exception, version
1919+ 3.1, as published by the Free Software Foundation.
2020+2121+ You should have received a copy of the GNU General Public License and
2222+ a copy of the GCC Runtime Library Exception along with this program;
2323+ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
2424+ <http://www.gnu.org/licenses/>. */
2525+2626+#define symver(name, version) \
2727+ __asm__ (".symver " #name"," #name "@" #version)
2828+2929+#define alias(name) \
3030+ __typeof(name) __libunwind##name __attribute__ ((alias (#name)))
+43
src/libunwind-darwin/unwind-dw2-fde-compat.c
···11+/* Backward compatibility unwind routines.
22+ Copyright (C) 2004, 2005, 2009
33+ Free Software Foundation, Inc.
44+55+ This file is part of GCC.
66+77+ GCC is free software; you can redistribute it and/or modify it
88+ under the terms of the GNU General Public License as published by
99+ the Free Software Foundation; either version 3, or (at your option)
1010+ any later version.
1111+1212+ GCC is distributed in the hope that it will be useful, but WITHOUT
1313+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1414+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
1515+ License for more details.
1616+1717+ Under Section 7 of GPL version 3, you are granted additional
1818+ permissions described in the GCC Runtime Library Exception, version
1919+ 3.1, as published by the Free Software Foundation.
2020+2121+ You should have received a copy of the GNU General Public License and
2222+ a copy of the GCC Runtime Library Exception along with this program;
2323+ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
2424+ <http://www.gnu.org/licenses/>. */
2525+2626+#if defined (USE_GAS_SYMVER) && defined (USE_LIBUNWIND_EXCEPTIONS)
2727+#include "tconfig.h"
2828+#include "tsystem.h"
2929+#include "unwind.h"
3030+#include "unwind-dw2-fde.h"
3131+#include "unwind-compat.h"
3232+3333+extern const fde * __libunwind__Unwind_Find_FDE
3434+ (void *, struct dwarf_eh_bases *);
3535+3636+const fde *
3737+_Unwind_Find_FDE (void *pc, struct dwarf_eh_bases *bases)
3838+{
3939+ __libunwind__Unwind_Find_FDE (pc, bases);
4040+}
4141+4242+symver (_Unwind_Find_FDE, GCC_3.0);
4343+#endif
+289
src/libunwind-darwin/unwind-dw2-fde-darwin.c
···11+/* Copyright (C) 2001, 2002, 2003, 2005, 2009, 2010
22+ Free Software Foundation, Inc.
33+44+ This file is part of GCC.
55+66+ GCC is free software; you can redistribute it and/or modify
77+ it under the terms of the GNU General Public License as published by
88+ the Free Software Foundation; either version 3, or (at your option)
99+ any later version.
1010+1111+ GCC is distributed in the hope that it will be useful,
1212+ but WITHOUT ANY WARRANTY; without even the implied warranty of
1313+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1414+ GNU General Public License for more details.
1515+1616+ Under Section 7 of GPL version 3, you are granted additional
1717+ permissions described in the GCC Runtime Library Exception, version
1818+ 3.1, as published by the Free Software Foundation.
1919+2020+ You should have received a copy of the GNU General Public License and
2121+ a copy of the GCC Runtime Library Exception along with this program;
2222+ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
2323+ <http://www.gnu.org/licenses/>. */
2424+2525+/* Locate the FDE entry for a given address, using Darwin's keymgr support. */
2626+2727+#include "tconfig.h"
2828+#include "tsystem.h"
2929+#include <string.h>
3030+#include <stdlib.h>
3131+#include "dwarf2.h"
3232+#include "unwind.h"
3333+#define NO_BASE_OF_ENCODED_VALUE
3434+#define DWARF2_OBJECT_END_PTR_EXTENSION
3535+#include "unwind-pe.h"
3636+#include "unwind-dw2-fde.h"
3737+/* Carefully don't include gthr.h. */
3838+3939+typedef int __gthread_mutex_t;
4040+#define __gthread_mutex_lock(x) (void)(x)
4141+#define __gthread_mutex_unlock(x) (void)(x)
4242+4343+static const fde * _Unwind_Find_registered_FDE (void *pc,
4444+ struct dwarf_eh_bases *bases);
4545+4646+#define _Unwind_Find_FDE _Unwind_Find_registered_FDE
4747+#include "unwind-dw2-fde.c"
4848+#undef _Unwind_Find_FDE
4949+5050+/* KeyMgr stuff. */
5151+#define KEYMGR_GCC3_LIVE_IMAGE_LIST 301 /* loaded images */
5252+#define KEYMGR_GCC3_DW2_OBJ_LIST 302 /* Dwarf2 object list */
5353+5454+extern void *_keymgr_get_and_lock_processwide_ptr (int);
5555+extern void _keymgr_set_and_unlock_processwide_ptr (int, void *);
5656+extern void _keymgr_unlock_processwide_ptr (int);
5757+5858+struct mach_header;
5959+struct mach_header_64;
6060+extern char *getsectdatafromheader (struct mach_header*, const char*,
6161+ const char *, unsigned long *);
6262+extern char *getsectdatafromheader_64 (struct mach_header_64*, const char*,
6363+ const char *, unsigned long *);
6464+6565+/* This is referenced from KEYMGR_GCC3_DW2_OBJ_LIST. */
6666+struct km_object_info {
6767+ struct object *seen_objects;
6868+ struct object *unseen_objects;
6969+ unsigned spare[2];
7070+};
7171+7272+/* Node of KEYMGR_GCC3_LIVE_IMAGE_LIST. Info about each resident image. */
7373+struct live_images {
7474+ unsigned long this_size; /* sizeof (live_images) */
7575+ struct mach_header *mh; /* the image info */
7676+ unsigned long vm_slide;
7777+ void (*destructor)(struct live_images *); /* destructor for this */
7878+ struct live_images *next;
7979+ unsigned int examined_p;
8080+ void *fde;
8181+ void *object_info;
8282+ unsigned long info[2]; /* Future use. */
8383+};
8484+8585+/* Bits in the examined_p field of struct live_images. */
8686+enum {
8787+ EXAMINED_IMAGE_MASK = 1, /* We've seen this one. */
8888+ ALLOCED_IMAGE_MASK = 2, /* The FDE entries were allocated by
8989+ malloc, and must be freed. This isn't
9090+ used by newer libgcc versions. */
9191+ IMAGE_IS_TEXT_MASK = 4, /* This image is in the TEXT segment. */
9292+ DESTRUCTOR_MAY_BE_CALLED_LIVE = 8 /* The destructor may be called on an
9393+ object that's part of the live
9494+ image list. */
9595+};
9696+9797+/* Delete any data we allocated on a live_images structure. Either
9898+ IMAGE has already been removed from the
9999+ KEYMGR_GCC3_LIVE_IMAGE_LIST and the struct will be deleted
100100+ after we return, or that list is locked and we're being called
101101+ because this object might be about to be unloaded. Called by
102102+ KeyMgr. */
103103+104104+static void
105105+live_image_destructor (struct live_images *image)
106106+{
107107+ if (image->object_info)
108108+ {
109109+ struct km_object_info *the_obj_info;
110110+111111+ the_obj_info =
112112+ _keymgr_get_and_lock_processwide_ptr (KEYMGR_GCC3_DW2_OBJ_LIST);
113113+ if (the_obj_info)
114114+ {
115115+ seen_objects = the_obj_info->seen_objects;
116116+ unseen_objects = the_obj_info->unseen_objects;
117117+118118+ /* Free any sorted arrays. */
119119+ __deregister_frame_info_bases (image->fde);
120120+121121+ the_obj_info->seen_objects = seen_objects;
122122+ the_obj_info->unseen_objects = unseen_objects;
123123+ }
124124+ _keymgr_set_and_unlock_processwide_ptr (KEYMGR_GCC3_DW2_OBJ_LIST,
125125+ the_obj_info);
126126+127127+ free (image->object_info);
128128+ image->object_info = NULL;
129129+ if (image->examined_p & ALLOCED_IMAGE_MASK)
130130+ free (image->fde);
131131+ image->fde = NULL;
132132+ }
133133+ image->examined_p = 0;
134134+ image->destructor = NULL;
135135+}
136136+137137+/* Run through the list of live images. If we can allocate memory,
138138+ give each unseen image a new `struct object'. Even if we can't,
139139+ check whether the PC is inside the FDE of each unseen image.
140140+ */
141141+142142+static inline const fde *
143143+examine_objects (void *pc, struct dwarf_eh_bases *bases, int dont_alloc)
144144+{
145145+ const fde *result = NULL;
146146+ struct live_images *image;
147147+148148+ image = _keymgr_get_and_lock_processwide_ptr (KEYMGR_GCC3_LIVE_IMAGE_LIST);
149149+150150+ for (; image != NULL; image = image->next)
151151+ if ((image->examined_p & EXAMINED_IMAGE_MASK) == 0)
152152+ {
153153+ char *fde = NULL;
154154+ unsigned long sz;
155155+156156+ /* For ppc only check whether or not we have __DATA eh frames. */
157157+#ifdef __ppc__
158158+ fde = getsectdatafromheader (image->mh, "__DATA", "__eh_frame", &sz);
159159+#endif
160160+161161+ if (fde == NULL)
162162+ {
163163+#if __LP64__
164164+ fde = getsectdatafromheader_64 ((struct mach_header_64 *) image->mh,
165165+ "__TEXT", "__eh_frame", &sz);
166166+#else
167167+ fde = getsectdatafromheader (image->mh, "__TEXT",
168168+ "__eh_frame", &sz);
169169+#endif
170170+ if (fde != NULL)
171171+ image->examined_p |= IMAGE_IS_TEXT_MASK;
172172+ }
173173+174174+ /* If .eh_frame is empty, don't register at all. */
175175+ if (fde != NULL && sz > 0)
176176+ {
177177+ char *real_fde = (fde + image->vm_slide);
178178+ struct object *ob = NULL;
179179+ struct object panicob;
180180+181181+ if (! dont_alloc)
182182+ ob = calloc (1, sizeof (struct object));
183183+ dont_alloc |= ob == NULL;
184184+ if (dont_alloc)
185185+ ob = &panicob;
186186+187187+ ob->pc_begin = (void *)-1;
188188+ ob->tbase = 0;
189189+ ob->dbase = 0;
190190+ ob->u.single = (struct dwarf_fde *)real_fde;
191191+ ob->s.i = 0;
192192+ ob->s.b.encoding = DW_EH_PE_omit;
193193+ ob->fde_end = real_fde + sz;
194194+195195+ image->fde = real_fde;
196196+197197+ result = search_object (ob, pc);
198198+199199+ if (! dont_alloc)
200200+ {
201201+ struct object **p;
202202+203203+ image->destructor = live_image_destructor;
204204+ image->object_info = ob;
205205+206206+ image->examined_p |= (EXAMINED_IMAGE_MASK
207207+ | DESTRUCTOR_MAY_BE_CALLED_LIVE);
208208+209209+ /* Insert the object into the classified list. */
210210+ for (p = &seen_objects; *p ; p = &(*p)->next)
211211+ if ((*p)->pc_begin < ob->pc_begin)
212212+ break;
213213+ ob->next = *p;
214214+ *p = ob;
215215+ }
216216+217217+ if (result)
218218+ {
219219+ int encoding;
220220+ _Unwind_Ptr func;
221221+222222+ bases->tbase = ob->tbase;
223223+ bases->dbase = ob->dbase;
224224+225225+ encoding = ob->s.b.encoding;
226226+ if (ob->s.b.mixed_encoding)
227227+ encoding = get_fde_encoding (result);
228228+ read_encoded_value_with_base (encoding,
229229+ base_from_object (encoding, ob),
230230+ result->pc_begin, &func);
231231+ bases->func = (void *) func;
232232+ break;
233233+ }
234234+ }
235235+ else
236236+ image->examined_p |= EXAMINED_IMAGE_MASK;
237237+ }
238238+239239+ _keymgr_unlock_processwide_ptr (KEYMGR_GCC3_LIVE_IMAGE_LIST);
240240+241241+ return result;
242242+}
243243+244244+const fde *
245245+_Unwind_Find_FDE (void *pc, struct dwarf_eh_bases *bases)
246246+{
247247+ struct km_object_info *the_obj_info;
248248+ const fde *ret = NULL;
249249+250250+ the_obj_info =
251251+ _keymgr_get_and_lock_processwide_ptr (KEYMGR_GCC3_DW2_OBJ_LIST);
252252+ if (! the_obj_info)
253253+ the_obj_info = calloc (1, sizeof (*the_obj_info));
254254+255255+ if (the_obj_info != NULL)
256256+ {
257257+ seen_objects = the_obj_info->seen_objects;
258258+ unseen_objects = the_obj_info->unseen_objects;
259259+260260+ ret = _Unwind_Find_registered_FDE (pc, bases);
261261+ }
262262+263263+ /* OK, didn't find it in the list of FDEs we've seen before,
264264+ so go through and look at the new ones. */
265265+ if (ret == NULL)
266266+ ret = examine_objects (pc, bases, the_obj_info == NULL);
267267+268268+ if (the_obj_info != NULL)
269269+ {
270270+ the_obj_info->seen_objects = seen_objects;
271271+ the_obj_info->unseen_objects = unseen_objects;
272272+ }
273273+ _keymgr_set_and_unlock_processwide_ptr (KEYMGR_GCC3_DW2_OBJ_LIST,
274274+ the_obj_info);
275275+ return ret;
276276+}
277277+278278+void *
279279+_darwin10_Unwind_FindEnclosingFunction (void *pc ATTRIBUTE_UNUSED)
280280+{
281281+#if __MACH__ && (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1060)
282282+ struct dwarf_eh_bases bases;
283283+ const struct dwarf_fde *fde = _Unwind_Find_FDE (pc-1, &bases);
284284+ if (fde)
285285+ return bases.func;
286286+#endif
287287+ return NULL;
288288+}
289289+
+1054
src/libunwind-darwin/unwind-dw2-fde.c
···11+/* Subroutines needed for unwinding stack frames for exception handling. */
22+/* Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2008,
33+ 2009, 2010 Free Software Foundation, Inc.
44+ Contributed by Jason Merrill <jason@cygnus.com>.
55+66+This file is part of GCC.
77+88+GCC is free software; you can redistribute it and/or modify it under
99+the terms of the GNU General Public License as published by the Free
1010+Software Foundation; either version 3, or (at your option) any later
1111+version.
1212+1313+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
1414+WARRANTY; without even the implied warranty of MERCHANTABILITY or
1515+FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1616+for more details.
1717+1818+Under Section 7 of GPL version 3, you are granted additional
1919+permissions described in the GCC Runtime Library Exception, version
2020+3.1, as published by the Free Software Foundation.
2121+2222+You should have received a copy of the GNU General Public License and
2323+a copy of the GCC Runtime Library Exception along with this program;
2424+see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
2525+<http://www.gnu.org/licenses/>. */
2626+2727+#ifndef _Unwind_Find_FDE
2828+#include "tconfig.h"
2929+#include "tsystem.h"
3030+#include "coretypes.h"
3131+#include "tm.h"
3232+#include "dwarf2.h"
3333+#include "unwind.h"
3434+#define NO_BASE_OF_ENCODED_VALUE
3535+#include "unwind-pe.h"
3636+#include "unwind-dw2-fde.h"
3737+#include "gthr.h"
3838+#endif
3939+4040+/* The unseen_objects list contains objects that have been registered
4141+ but not yet categorized in any way. The seen_objects list has had
4242+ its pc_begin and count fields initialized at minimum, and is sorted
4343+ by decreasing value of pc_begin. */
4444+static struct object *unseen_objects;
4545+static struct object *seen_objects;
4646+4747+#ifdef __GTHREAD_MUTEX_INIT
4848+static __gthread_mutex_t object_mutex = __GTHREAD_MUTEX_INIT;
4949+#else
5050+static __gthread_mutex_t object_mutex;
5151+#endif
5252+5353+#ifdef __GTHREAD_MUTEX_INIT_FUNCTION
5454+static void
5555+init_object_mutex (void)
5656+{
5757+ __GTHREAD_MUTEX_INIT_FUNCTION (&object_mutex);
5858+}
5959+6060+static void
6161+init_object_mutex_once (void)
6262+{
6363+ static __gthread_once_t once = __GTHREAD_ONCE_INIT;
6464+ __gthread_once (&once, init_object_mutex);
6565+}
6666+#else
6767+#define init_object_mutex_once()
6868+#endif
6969+7070+/* Called from crtbegin.o to register the unwind info for an object. */
7171+7272+void
7373+__register_frame_info_bases (const void *begin, struct object *ob,
7474+ void *tbase, void *dbase)
7575+{
7676+ /* If .eh_frame is empty, don't register at all. */
7777+ if ((const uword *) begin == 0 || *(const uword *) begin == 0)
7878+ return;
7979+8080+ ob->pc_begin = (void *)-1;
8181+ ob->tbase = tbase;
8282+ ob->dbase = dbase;
8383+ ob->u.single = begin;
8484+ ob->s.i = 0;
8585+ ob->s.b.encoding = DW_EH_PE_omit;
8686+#ifdef DWARF2_OBJECT_END_PTR_EXTENSION
8787+ ob->fde_end = NULL;
8888+#endif
8989+9090+ init_object_mutex_once ();
9191+ __gthread_mutex_lock (&object_mutex);
9292+9393+ ob->next = unseen_objects;
9494+ unseen_objects = ob;
9595+9696+ __gthread_mutex_unlock (&object_mutex);
9797+}
9898+9999+void
100100+__register_frame_info (const void *begin, struct object *ob)
101101+{
102102+ __register_frame_info_bases (begin, ob, 0, 0);
103103+}
104104+105105+void
106106+__register_frame (void *begin)
107107+{
108108+ struct object *ob;
109109+110110+ /* If .eh_frame is empty, don't register at all. */
111111+ if (*(uword *) begin == 0)
112112+ return;
113113+114114+ ob = malloc (sizeof (struct object));
115115+ __register_frame_info (begin, ob);
116116+}
117117+118118+/* Similar, but BEGIN is actually a pointer to a table of unwind entries
119119+ for different translation units. Called from the file generated by
120120+ collect2. */
121121+122122+void
123123+__register_frame_info_table_bases (void *begin, struct object *ob,
124124+ void *tbase, void *dbase)
125125+{
126126+ ob->pc_begin = (void *)-1;
127127+ ob->tbase = tbase;
128128+ ob->dbase = dbase;
129129+ ob->u.array = begin;
130130+ ob->s.i = 0;
131131+ ob->s.b.from_array = 1;
132132+ ob->s.b.encoding = DW_EH_PE_omit;
133133+134134+ init_object_mutex_once ();
135135+ __gthread_mutex_lock (&object_mutex);
136136+137137+ ob->next = unseen_objects;
138138+ unseen_objects = ob;
139139+140140+ __gthread_mutex_unlock (&object_mutex);
141141+}
142142+143143+void
144144+__register_frame_info_table (void *begin, struct object *ob)
145145+{
146146+ __register_frame_info_table_bases (begin, ob, 0, 0);
147147+}
148148+149149+void
150150+__register_frame_table (void *begin)
151151+{
152152+ struct object *ob = malloc (sizeof (struct object));
153153+ __register_frame_info_table (begin, ob);
154154+}
155155+156156+/* Called from crtbegin.o to deregister the unwind info for an object. */
157157+/* ??? Glibc has for a while now exported __register_frame_info and
158158+ __deregister_frame_info. If we call __register_frame_info_bases
159159+ from crtbegin (wherein it is declared weak), and this object does
160160+ not get pulled from libgcc.a for other reasons, then the
161161+ invocation of __deregister_frame_info will be resolved from glibc.
162162+ Since the registration did not happen there, we'll die.
163163+164164+ Therefore, declare a new deregistration entry point that does the
165165+ exact same thing, but will resolve to the same library as
166166+ implements __register_frame_info_bases. */
167167+168168+void *
169169+__deregister_frame_info_bases (const void *begin)
170170+{
171171+ struct object **p;
172172+ struct object *ob = 0;
173173+174174+ /* If .eh_frame is empty, we haven't registered. */
175175+ if ((const uword *) begin == 0 || *(const uword *) begin == 0)
176176+ return ob;
177177+178178+ init_object_mutex_once ();
179179+ __gthread_mutex_lock (&object_mutex);
180180+181181+ for (p = &unseen_objects; *p ; p = &(*p)->next)
182182+ if ((*p)->u.single == begin)
183183+ {
184184+ ob = *p;
185185+ *p = ob->next;
186186+ goto out;
187187+ }
188188+189189+ for (p = &seen_objects; *p ; p = &(*p)->next)
190190+ if ((*p)->s.b.sorted)
191191+ {
192192+ if ((*p)->u.sort->orig_data == begin)
193193+ {
194194+ ob = *p;
195195+ *p = ob->next;
196196+ free (ob->u.sort);
197197+ goto out;
198198+ }
199199+ }
200200+ else
201201+ {
202202+ if ((*p)->u.single == begin)
203203+ {
204204+ ob = *p;
205205+ *p = ob->next;
206206+ goto out;
207207+ }
208208+ }
209209+210210+ out:
211211+ __gthread_mutex_unlock (&object_mutex);
212212+ gcc_assert (ob);
213213+ return (void *) ob;
214214+}
215215+216216+void *
217217+__deregister_frame_info (const void *begin)
218218+{
219219+ return __deregister_frame_info_bases (begin);
220220+}
221221+222222+void
223223+__deregister_frame (void *begin)
224224+{
225225+ /* If .eh_frame is empty, we haven't registered. */
226226+ if (*(uword *) begin != 0)
227227+ free (__deregister_frame_info (begin));
228228+}
229229+230230+231231+/* Like base_of_encoded_value, but take the base from a struct object
232232+ instead of an _Unwind_Context. */
233233+234234+static _Unwind_Ptr
235235+base_from_object (unsigned char encoding, struct object *ob)
236236+{
237237+ if (encoding == DW_EH_PE_omit)
238238+ return 0;
239239+240240+ switch (encoding & 0x70)
241241+ {
242242+ case DW_EH_PE_absptr:
243243+ case DW_EH_PE_pcrel:
244244+ case DW_EH_PE_aligned:
245245+ return 0;
246246+247247+ case DW_EH_PE_textrel:
248248+ return (_Unwind_Ptr) ob->tbase;
249249+ case DW_EH_PE_datarel:
250250+ return (_Unwind_Ptr) ob->dbase;
251251+ default:
252252+ gcc_unreachable ();
253253+ }
254254+}
255255+256256+/* Return the FDE pointer encoding from the CIE. */
257257+/* ??? This is a subset of extract_cie_info from unwind-dw2.c. */
258258+259259+static int
260260+get_cie_encoding (const struct dwarf_cie *cie)
261261+{
262262+ const unsigned char *aug, *p;
263263+ _Unwind_Ptr dummy;
264264+ _uleb128_t utmp;
265265+ _sleb128_t stmp;
266266+267267+ aug = cie->augmentation;
268268+ p = aug + strlen ((const char *)aug) + 1; /* Skip the augmentation string. */
269269+ if (__builtin_expect (cie->version >= 4, 0))
270270+ {
271271+ if (p[0] != sizeof (void *) || p[1] != 0)
272272+ return DW_EH_PE_omit; /* We are not prepared to handle unexpected
273273+ address sizes or segment selectors. */
274274+ p += 2; /* Skip address size and segment size. */
275275+ }
276276+277277+ if (aug[0] != 'z')
278278+ return DW_EH_PE_absptr;
279279+280280+ p = read_uleb128 (p, &utmp); /* Skip code alignment. */
281281+ p = read_sleb128 (p, &stmp); /* Skip data alignment. */
282282+ if (cie->version == 1) /* Skip return address column. */
283283+ p++;
284284+ else
285285+ p = read_uleb128 (p, &utmp);
286286+287287+ aug++; /* Skip 'z' */
288288+ p = read_uleb128 (p, &utmp); /* Skip augmentation length. */
289289+ while (1)
290290+ {
291291+ /* This is what we're looking for. */
292292+ if (*aug == 'R')
293293+ return *p;
294294+ /* Personality encoding and pointer. */
295295+ else if (*aug == 'P')
296296+ {
297297+ /* ??? Avoid dereferencing indirect pointers, since we're
298298+ faking the base address. Gotta keep DW_EH_PE_aligned
299299+ intact, however. */
300300+ p = read_encoded_value_with_base (*p & 0x7F, 0, p + 1, &dummy);
301301+ }
302302+ /* LSDA encoding. */
303303+ else if (*aug == 'L')
304304+ p++;
305305+ /* Otherwise end of string, or unknown augmentation. */
306306+ else
307307+ return DW_EH_PE_absptr;
308308+ aug++;
309309+ }
310310+}
311311+312312+static inline int
313313+get_fde_encoding (const struct dwarf_fde *f)
314314+{
315315+ return get_cie_encoding (get_cie (f));
316316+}
317317+318318+319319+/* Sorting an array of FDEs by address.
320320+ (Ideally we would have the linker sort the FDEs so we don't have to do
321321+ it at run time. But the linkers are not yet prepared for this.) */
322322+323323+/* Comparison routines. Three variants of increasing complexity. */
324324+325325+static int
326326+fde_unencoded_compare (struct object *ob __attribute__((unused)),
327327+ const fde *x, const fde *y)
328328+{
329329+ _Unwind_Ptr x_ptr, y_ptr;
330330+ memcpy (&x_ptr, x->pc_begin, sizeof (_Unwind_Ptr));
331331+ memcpy (&y_ptr, y->pc_begin, sizeof (_Unwind_Ptr));
332332+333333+ if (x_ptr > y_ptr)
334334+ return 1;
335335+ if (x_ptr < y_ptr)
336336+ return -1;
337337+ return 0;
338338+}
339339+340340+static int
341341+fde_single_encoding_compare (struct object *ob, const fde *x, const fde *y)
342342+{
343343+ _Unwind_Ptr base, x_ptr, y_ptr;
344344+345345+ base = base_from_object (ob->s.b.encoding, ob);
346346+ read_encoded_value_with_base (ob->s.b.encoding, base, x->pc_begin, &x_ptr);
347347+ read_encoded_value_with_base (ob->s.b.encoding, base, y->pc_begin, &y_ptr);
348348+349349+ if (x_ptr > y_ptr)
350350+ return 1;
351351+ if (x_ptr < y_ptr)
352352+ return -1;
353353+ return 0;
354354+}
355355+356356+static int
357357+fde_mixed_encoding_compare (struct object *ob, const fde *x, const fde *y)
358358+{
359359+ int x_encoding, y_encoding;
360360+ _Unwind_Ptr x_ptr, y_ptr;
361361+362362+ x_encoding = get_fde_encoding (x);
363363+ read_encoded_value_with_base (x_encoding, base_from_object (x_encoding, ob),
364364+ x->pc_begin, &x_ptr);
365365+366366+ y_encoding = get_fde_encoding (y);
367367+ read_encoded_value_with_base (y_encoding, base_from_object (y_encoding, ob),
368368+ y->pc_begin, &y_ptr);
369369+370370+ if (x_ptr > y_ptr)
371371+ return 1;
372372+ if (x_ptr < y_ptr)
373373+ return -1;
374374+ return 0;
375375+}
376376+377377+typedef int (*fde_compare_t) (struct object *, const fde *, const fde *);
378378+379379+380380+/* This is a special mix of insertion sort and heap sort, optimized for
381381+ the data sets that actually occur. They look like
382382+ 101 102 103 127 128 105 108 110 190 111 115 119 125 160 126 129 130.
383383+ I.e. a linearly increasing sequence (coming from functions in the text
384384+ section), with additionally a few unordered elements (coming from functions
385385+ in gnu_linkonce sections) whose values are higher than the values in the
386386+ surrounding linear sequence (but not necessarily higher than the values
387387+ at the end of the linear sequence!).
388388+ The worst-case total run time is O(N) + O(n log (n)), where N is the
389389+ total number of FDEs and n is the number of erratic ones. */
390390+391391+struct fde_accumulator
392392+{
393393+ struct fde_vector *linear;
394394+ struct fde_vector *erratic;
395395+};
396396+397397+static inline int
398398+start_fde_sort (struct fde_accumulator *accu, size_t count)
399399+{
400400+ size_t size;
401401+ if (! count)
402402+ return 0;
403403+404404+ size = sizeof (struct fde_vector) + sizeof (const fde *) * count;
405405+ if ((accu->linear = malloc (size)))
406406+ {
407407+ accu->linear->count = 0;
408408+ if ((accu->erratic = malloc (size)))
409409+ accu->erratic->count = 0;
410410+ return 1;
411411+ }
412412+ else
413413+ return 0;
414414+}
415415+416416+static inline void
417417+fde_insert (struct fde_accumulator *accu, const fde *this_fde)
418418+{
419419+ if (accu->linear)
420420+ accu->linear->array[accu->linear->count++] = this_fde;
421421+}
422422+423423+/* Split LINEAR into a linear sequence with low values and an erratic
424424+ sequence with high values, put the linear one (of longest possible
425425+ length) into LINEAR and the erratic one into ERRATIC. This is O(N).
426426+427427+ Because the longest linear sequence we are trying to locate within the
428428+ incoming LINEAR array can be interspersed with (high valued) erratic
429429+ entries. We construct a chain indicating the sequenced entries.
430430+ To avoid having to allocate this chain, we overlay it onto the space of
431431+ the ERRATIC array during construction. A final pass iterates over the
432432+ chain to determine what should be placed in the ERRATIC array, and
433433+ what is the linear sequence. This overlay is safe from aliasing. */
434434+435435+static inline void
436436+fde_split (struct object *ob, fde_compare_t fde_compare,
437437+ struct fde_vector *linear, struct fde_vector *erratic)
438438+{
439439+ static const fde *marker;
440440+ size_t count = linear->count;
441441+ const fde *const *chain_end = ▮
442442+ size_t i, j, k;
443443+444444+ /* This should optimize out, but it is wise to make sure this assumption
445445+ is correct. Should these have different sizes, we cannot cast between
446446+ them and the overlaying onto ERRATIC will not work. */
447447+ gcc_assert (sizeof (const fde *) == sizeof (const fde **));
448448+449449+ for (i = 0; i < count; i++)
450450+ {
451451+ const fde *const *probe;
452452+453453+ for (probe = chain_end;
454454+ probe != &marker && fde_compare (ob, linear->array[i], *probe) < 0;
455455+ probe = chain_end)
456456+ {
457457+ chain_end = (const fde *const*) erratic->array[probe - linear->array];
458458+ erratic->array[probe - linear->array] = NULL;
459459+ }
460460+ erratic->array[i] = (const fde *) chain_end;
461461+ chain_end = &linear->array[i];
462462+ }
463463+464464+ /* Each entry in LINEAR which is part of the linear sequence we have
465465+ discovered will correspond to a non-NULL entry in the chain we built in
466466+ the ERRATIC array. */
467467+ for (i = j = k = 0; i < count; i++)
468468+ if (erratic->array[i])
469469+ linear->array[j++] = linear->array[i];
470470+ else
471471+ erratic->array[k++] = linear->array[i];
472472+ linear->count = j;
473473+ erratic->count = k;
474474+}
475475+476476+#define SWAP(x,y) do { const fde * tmp = x; x = y; y = tmp; } while (0)
477477+478478+/* Convert a semi-heap to a heap. A semi-heap is a heap except possibly
479479+ for the first (root) node; push it down to its rightful place. */
480480+481481+static void
482482+frame_downheap (struct object *ob, fde_compare_t fde_compare, const fde **a,
483483+ int lo, int hi)
484484+{
485485+ int i, j;
486486+487487+ for (i = lo, j = 2*i+1;
488488+ j < hi;
489489+ j = 2*i+1)
490490+ {
491491+ if (j+1 < hi && fde_compare (ob, a[j], a[j+1]) < 0)
492492+ ++j;
493493+494494+ if (fde_compare (ob, a[i], a[j]) < 0)
495495+ {
496496+ SWAP (a[i], a[j]);
497497+ i = j;
498498+ }
499499+ else
500500+ break;
501501+ }
502502+}
503503+504504+/* This is O(n log(n)). BSD/OS defines heapsort in stdlib.h, so we must
505505+ use a name that does not conflict. */
506506+507507+static void
508508+frame_heapsort (struct object *ob, fde_compare_t fde_compare,
509509+ struct fde_vector *erratic)
510510+{
511511+ /* For a description of this algorithm, see:
512512+ Samuel P. Harbison, Guy L. Steele Jr.: C, a reference manual, 2nd ed.,
513513+ p. 60-61. */
514514+ const fde ** a = erratic->array;
515515+ /* A portion of the array is called a "heap" if for all i>=0:
516516+ If i and 2i+1 are valid indices, then a[i] >= a[2i+1].
517517+ If i and 2i+2 are valid indices, then a[i] >= a[2i+2]. */
518518+ size_t n = erratic->count;
519519+ int m;
520520+521521+ /* Expand our heap incrementally from the end of the array, heapifying
522522+ each resulting semi-heap as we go. After each step, a[m] is the top
523523+ of a heap. */
524524+ for (m = n/2-1; m >= 0; --m)
525525+ frame_downheap (ob, fde_compare, a, m, n);
526526+527527+ /* Shrink our heap incrementally from the end of the array, first
528528+ swapping out the largest element a[0] and then re-heapifying the
529529+ resulting semi-heap. After each step, a[0..m) is a heap. */
530530+ for (m = n-1; m >= 1; --m)
531531+ {
532532+ SWAP (a[0], a[m]);
533533+ frame_downheap (ob, fde_compare, a, 0, m);
534534+ }
535535+#undef SWAP
536536+}
537537+538538+/* Merge V1 and V2, both sorted, and put the result into V1. */
539539+static inline void
540540+fde_merge (struct object *ob, fde_compare_t fde_compare,
541541+ struct fde_vector *v1, struct fde_vector *v2)
542542+{
543543+ size_t i1, i2;
544544+ const fde * fde2;
545545+546546+ i2 = v2->count;
547547+ if (i2 > 0)
548548+ {
549549+ i1 = v1->count;
550550+ do
551551+ {
552552+ i2--;
553553+ fde2 = v2->array[i2];
554554+ while (i1 > 0 && fde_compare (ob, v1->array[i1-1], fde2) > 0)
555555+ {
556556+ v1->array[i1+i2] = v1->array[i1-1];
557557+ i1--;
558558+ }
559559+ v1->array[i1+i2] = fde2;
560560+ }
561561+ while (i2 > 0);
562562+ v1->count += v2->count;
563563+ }
564564+}
565565+566566+static inline void
567567+end_fde_sort (struct object *ob, struct fde_accumulator *accu, size_t count)
568568+{
569569+ fde_compare_t fde_compare;
570570+571571+ gcc_assert (!accu->linear || accu->linear->count == count);
572572+573573+ if (ob->s.b.mixed_encoding)
574574+ fde_compare = fde_mixed_encoding_compare;
575575+ else if (ob->s.b.encoding == DW_EH_PE_absptr)
576576+ fde_compare = fde_unencoded_compare;
577577+ else
578578+ fde_compare = fde_single_encoding_compare;
579579+580580+ if (accu->erratic)
581581+ {
582582+ fde_split (ob, fde_compare, accu->linear, accu->erratic);
583583+ gcc_assert (accu->linear->count + accu->erratic->count == count);
584584+ frame_heapsort (ob, fde_compare, accu->erratic);
585585+ fde_merge (ob, fde_compare, accu->linear, accu->erratic);
586586+ free (accu->erratic);
587587+ }
588588+ else
589589+ {
590590+ /* We've not managed to malloc an erratic array,
591591+ so heap sort in the linear one. */
592592+ frame_heapsort (ob, fde_compare, accu->linear);
593593+ }
594594+}
595595+596596+597597+/* Update encoding, mixed_encoding, and pc_begin for OB for the
598598+ fde array beginning at THIS_FDE. Return the number of fdes
599599+ encountered along the way. */
600600+601601+static size_t
602602+classify_object_over_fdes (struct object *ob, const fde *this_fde)
603603+{
604604+ const struct dwarf_cie *last_cie = 0;
605605+ size_t count = 0;
606606+ int encoding = DW_EH_PE_absptr;
607607+ _Unwind_Ptr base = 0;
608608+609609+ for (; ! last_fde (ob, this_fde); this_fde = next_fde (this_fde))
610610+ {
611611+ const struct dwarf_cie *this_cie;
612612+ _Unwind_Ptr mask, pc_begin;
613613+614614+ /* Skip CIEs. */
615615+ if (this_fde->CIE_delta == 0)
616616+ continue;
617617+618618+ /* Determine the encoding for this FDE. Note mixed encoded
619619+ objects for later. */
620620+ this_cie = get_cie (this_fde);
621621+ if (this_cie != last_cie)
622622+ {
623623+ last_cie = this_cie;
624624+ encoding = get_cie_encoding (this_cie);
625625+ if (encoding == DW_EH_PE_omit)
626626+ return -1;
627627+ base = base_from_object (encoding, ob);
628628+ if (ob->s.b.encoding == DW_EH_PE_omit)
629629+ ob->s.b.encoding = encoding;
630630+ else if (ob->s.b.encoding != encoding)
631631+ ob->s.b.mixed_encoding = 1;
632632+ }
633633+634634+ read_encoded_value_with_base (encoding, base, this_fde->pc_begin,
635635+ &pc_begin);
636636+637637+ /* Take care to ignore link-once functions that were removed.
638638+ In these cases, the function address will be NULL, but if
639639+ the encoding is smaller than a pointer a true NULL may not
640640+ be representable. Assume 0 in the representable bits is NULL. */
641641+ mask = size_of_encoded_value (encoding);
642642+ if (mask < sizeof (void *))
643643+ mask = (((_Unwind_Ptr) 1) << (mask << 3)) - 1;
644644+ else
645645+ mask = -1;
646646+647647+ if ((pc_begin & mask) == 0)
648648+ continue;
649649+650650+ count += 1;
651651+ if ((void *) pc_begin < ob->pc_begin)
652652+ ob->pc_begin = (void *) pc_begin;
653653+ }
654654+655655+ return count;
656656+}
657657+658658+static void
659659+add_fdes (struct object *ob, struct fde_accumulator *accu, const fde *this_fde)
660660+{
661661+ const struct dwarf_cie *last_cie = 0;
662662+ int encoding = ob->s.b.encoding;
663663+ _Unwind_Ptr base = base_from_object (ob->s.b.encoding, ob);
664664+665665+ for (; ! last_fde (ob, this_fde); this_fde = next_fde (this_fde))
666666+ {
667667+ const struct dwarf_cie *this_cie;
668668+669669+ /* Skip CIEs. */
670670+ if (this_fde->CIE_delta == 0)
671671+ continue;
672672+673673+ if (ob->s.b.mixed_encoding)
674674+ {
675675+ /* Determine the encoding for this FDE. Note mixed encoded
676676+ objects for later. */
677677+ this_cie = get_cie (this_fde);
678678+ if (this_cie != last_cie)
679679+ {
680680+ last_cie = this_cie;
681681+ encoding = get_cie_encoding (this_cie);
682682+ base = base_from_object (encoding, ob);
683683+ }
684684+ }
685685+686686+ if (encoding == DW_EH_PE_absptr)
687687+ {
688688+ _Unwind_Ptr ptr;
689689+ memcpy (&ptr, this_fde->pc_begin, sizeof (_Unwind_Ptr));
690690+ if (ptr == 0)
691691+ continue;
692692+ }
693693+ else
694694+ {
695695+ _Unwind_Ptr pc_begin, mask;
696696+697697+ read_encoded_value_with_base (encoding, base, this_fde->pc_begin,
698698+ &pc_begin);
699699+700700+ /* Take care to ignore link-once functions that were removed.
701701+ In these cases, the function address will be NULL, but if
702702+ the encoding is smaller than a pointer a true NULL may not
703703+ be representable. Assume 0 in the representable bits is NULL. */
704704+ mask = size_of_encoded_value (encoding);
705705+ if (mask < sizeof (void *))
706706+ mask = (((_Unwind_Ptr) 1) << (mask << 3)) - 1;
707707+ else
708708+ mask = -1;
709709+710710+ if ((pc_begin & mask) == 0)
711711+ continue;
712712+ }
713713+714714+ fde_insert (accu, this_fde);
715715+ }
716716+}
717717+718718+/* Set up a sorted array of pointers to FDEs for a loaded object. We
719719+ count up the entries before allocating the array because it's likely to
720720+ be faster. We can be called multiple times, should we have failed to
721721+ allocate a sorted fde array on a previous occasion. */
722722+723723+static inline void
724724+init_object (struct object* ob)
725725+{
726726+ struct fde_accumulator accu;
727727+ size_t count;
728728+729729+ count = ob->s.b.count;
730730+ if (count == 0)
731731+ {
732732+ if (ob->s.b.from_array)
733733+ {
734734+ fde **p = ob->u.array;
735735+ for (count = 0; *p; ++p)
736736+ {
737737+ size_t cur_count = classify_object_over_fdes (ob, *p);
738738+ if (cur_count == (size_t) -1)
739739+ goto unhandled_fdes;
740740+ count += cur_count;
741741+ }
742742+ }
743743+ else
744744+ {
745745+ count = classify_object_over_fdes (ob, ob->u.single);
746746+ if (count == (size_t) -1)
747747+ {
748748+ static const fde terminator;
749749+ unhandled_fdes:
750750+ ob->s.i = 0;
751751+ ob->s.b.encoding = DW_EH_PE_omit;
752752+ ob->u.single = &terminator;
753753+ return;
754754+ }
755755+ }
756756+757757+ /* The count field we have in the main struct object is somewhat
758758+ limited, but should suffice for virtually all cases. If the
759759+ counted value doesn't fit, re-write a zero. The worst that
760760+ happens is that we re-count next time -- admittedly non-trivial
761761+ in that this implies some 2M fdes, but at least we function. */
762762+ ob->s.b.count = count;
763763+ if (ob->s.b.count != count)
764764+ ob->s.b.count = 0;
765765+ }
766766+767767+ if (!start_fde_sort (&accu, count))
768768+ return;
769769+770770+ if (ob->s.b.from_array)
771771+ {
772772+ fde **p;
773773+ for (p = ob->u.array; *p; ++p)
774774+ add_fdes (ob, &accu, *p);
775775+ }
776776+ else
777777+ add_fdes (ob, &accu, ob->u.single);
778778+779779+ end_fde_sort (ob, &accu, count);
780780+781781+ /* Save the original fde pointer, since this is the key by which the
782782+ DSO will deregister the object. */
783783+ accu.linear->orig_data = ob->u.single;
784784+ ob->u.sort = accu.linear;
785785+786786+ ob->s.b.sorted = 1;
787787+}
788788+789789+/* A linear search through a set of FDEs for the given PC. This is
790790+ used when there was insufficient memory to allocate and sort an
791791+ array. */
792792+793793+static const fde *
794794+linear_search_fdes (struct object *ob, const fde *this_fde, void *pc)
795795+{
796796+ const struct dwarf_cie *last_cie = 0;
797797+ int encoding = ob->s.b.encoding;
798798+ _Unwind_Ptr base = base_from_object (ob->s.b.encoding, ob);
799799+800800+ for (; ! last_fde (ob, this_fde); this_fde = next_fde (this_fde))
801801+ {
802802+ const struct dwarf_cie *this_cie;
803803+ _Unwind_Ptr pc_begin, pc_range;
804804+805805+ /* Skip CIEs. */
806806+ if (this_fde->CIE_delta == 0)
807807+ continue;
808808+809809+ if (ob->s.b.mixed_encoding)
810810+ {
811811+ /* Determine the encoding for this FDE. Note mixed encoded
812812+ objects for later. */
813813+ this_cie = get_cie (this_fde);
814814+ if (this_cie != last_cie)
815815+ {
816816+ last_cie = this_cie;
817817+ encoding = get_cie_encoding (this_cie);
818818+ base = base_from_object (encoding, ob);
819819+ }
820820+ }
821821+822822+ if (encoding == DW_EH_PE_absptr)
823823+ {
824824+ const _Unwind_Ptr *pc_array = (const _Unwind_Ptr *) this_fde->pc_begin;
825825+ pc_begin = pc_array[0];
826826+ pc_range = pc_array[1];
827827+ if (pc_begin == 0)
828828+ continue;
829829+ }
830830+ else
831831+ {
832832+ _Unwind_Ptr mask;
833833+ const unsigned char *p;
834834+835835+ p = read_encoded_value_with_base (encoding, base,
836836+ this_fde->pc_begin, &pc_begin);
837837+ read_encoded_value_with_base (encoding & 0x0F, 0, p, &pc_range);
838838+839839+ /* Take care to ignore link-once functions that were removed.
840840+ In these cases, the function address will be NULL, but if
841841+ the encoding is smaller than a pointer a true NULL may not
842842+ be representable. Assume 0 in the representable bits is NULL. */
843843+ mask = size_of_encoded_value (encoding);
844844+ if (mask < sizeof (void *))
845845+ mask = (((_Unwind_Ptr) 1) << (mask << 3)) - 1;
846846+ else
847847+ mask = -1;
848848+849849+ if ((pc_begin & mask) == 0)
850850+ continue;
851851+ }
852852+853853+ if ((_Unwind_Ptr) pc - pc_begin < pc_range)
854854+ return this_fde;
855855+ }
856856+857857+ return NULL;
858858+}
859859+860860+/* Binary search for an FDE containing the given PC. Here are three
861861+ implementations of increasing complexity. */
862862+863863+static inline const fde *
864864+binary_search_unencoded_fdes (struct object *ob, void *pc)
865865+{
866866+ struct fde_vector *vec = ob->u.sort;
867867+ size_t lo, hi;
868868+869869+ for (lo = 0, hi = vec->count; lo < hi; )
870870+ {
871871+ size_t i = (lo + hi) / 2;
872872+ const fde *const f = vec->array[i];
873873+ void *pc_begin;
874874+ uaddr pc_range;
875875+ memcpy (&pc_begin, (const void * const *) f->pc_begin, sizeof (void *));
876876+ memcpy (&pc_range, (const uaddr *) f->pc_begin + 1, sizeof (uaddr));
877877+878878+ if (pc < pc_begin)
879879+ hi = i;
880880+ else if (pc >= pc_begin + pc_range)
881881+ lo = i + 1;
882882+ else
883883+ return f;
884884+ }
885885+886886+ return NULL;
887887+}
888888+889889+static inline const fde *
890890+binary_search_single_encoding_fdes (struct object *ob, void *pc)
891891+{
892892+ struct fde_vector *vec = ob->u.sort;
893893+ int encoding = ob->s.b.encoding;
894894+ _Unwind_Ptr base = base_from_object (encoding, ob);
895895+ size_t lo, hi;
896896+897897+ for (lo = 0, hi = vec->count; lo < hi; )
898898+ {
899899+ size_t i = (lo + hi) / 2;
900900+ const fde *f = vec->array[i];
901901+ _Unwind_Ptr pc_begin, pc_range;
902902+ const unsigned char *p;
903903+904904+ p = read_encoded_value_with_base (encoding, base, f->pc_begin,
905905+ &pc_begin);
906906+ read_encoded_value_with_base (encoding & 0x0F, 0, p, &pc_range);
907907+908908+ if ((_Unwind_Ptr) pc < pc_begin)
909909+ hi = i;
910910+ else if ((_Unwind_Ptr) pc >= pc_begin + pc_range)
911911+ lo = i + 1;
912912+ else
913913+ return f;
914914+ }
915915+916916+ return NULL;
917917+}
918918+919919+static inline const fde *
920920+binary_search_mixed_encoding_fdes (struct object *ob, void *pc)
921921+{
922922+ struct fde_vector *vec = ob->u.sort;
923923+ size_t lo, hi;
924924+925925+ for (lo = 0, hi = vec->count; lo < hi; )
926926+ {
927927+ size_t i = (lo + hi) / 2;
928928+ const fde *f = vec->array[i];
929929+ _Unwind_Ptr pc_begin, pc_range;
930930+ const unsigned char *p;
931931+ int encoding;
932932+933933+ encoding = get_fde_encoding (f);
934934+ p = read_encoded_value_with_base (encoding,
935935+ base_from_object (encoding, ob),
936936+ f->pc_begin, &pc_begin);
937937+ read_encoded_value_with_base (encoding & 0x0F, 0, p, &pc_range);
938938+939939+ if ((_Unwind_Ptr) pc < pc_begin)
940940+ hi = i;
941941+ else if ((_Unwind_Ptr) pc >= pc_begin + pc_range)
942942+ lo = i + 1;
943943+ else
944944+ return f;
945945+ }
946946+947947+ return NULL;
948948+}
949949+950950+static const fde *
951951+search_object (struct object* ob, void *pc)
952952+{
953953+ /* If the data hasn't been sorted, try to do this now. We may have
954954+ more memory available than last time we tried. */
955955+ if (! ob->s.b.sorted)
956956+ {
957957+ init_object (ob);
958958+959959+ /* Despite the above comment, the normal reason to get here is
960960+ that we've not processed this object before. A quick range
961961+ check is in order. */
962962+ if (pc < ob->pc_begin)
963963+ return NULL;
964964+ }
965965+966966+ if (ob->s.b.sorted)
967967+ {
968968+ if (ob->s.b.mixed_encoding)
969969+ return binary_search_mixed_encoding_fdes (ob, pc);
970970+ else if (ob->s.b.encoding == DW_EH_PE_absptr)
971971+ return binary_search_unencoded_fdes (ob, pc);
972972+ else
973973+ return binary_search_single_encoding_fdes (ob, pc);
974974+ }
975975+ else
976976+ {
977977+ /* Long slow laborious linear search, cos we've no memory. */
978978+ if (ob->s.b.from_array)
979979+ {
980980+ fde **p;
981981+ for (p = ob->u.array; *p ; p++)
982982+ {
983983+ const fde *f = linear_search_fdes (ob, *p, pc);
984984+ if (f)
985985+ return f;
986986+ }
987987+ return NULL;
988988+ }
989989+ else
990990+ return linear_search_fdes (ob, ob->u.single, pc);
991991+ }
992992+}
993993+994994+const fde *
995995+_Unwind_Find_FDE (void *pc, struct dwarf_eh_bases *bases)
996996+{
997997+ struct object *ob;
998998+ const fde *f = NULL;
999999+10001000+ init_object_mutex_once ();
10011001+ __gthread_mutex_lock (&object_mutex);
10021002+10031003+ /* Linear search through the classified objects, to find the one
10041004+ containing the pc. Note that pc_begin is sorted descending, and
10051005+ we expect objects to be non-overlapping. */
10061006+ for (ob = seen_objects; ob; ob = ob->next)
10071007+ if (pc >= ob->pc_begin)
10081008+ {
10091009+ f = search_object (ob, pc);
10101010+ if (f)
10111011+ goto fini;
10121012+ break;
10131013+ }
10141014+10151015+ /* Classify and search the objects we've not yet processed. */
10161016+ while ((ob = unseen_objects))
10171017+ {
10181018+ struct object **p;
10191019+10201020+ unseen_objects = ob->next;
10211021+ f = search_object (ob, pc);
10221022+10231023+ /* Insert the object into the classified list. */
10241024+ for (p = &seen_objects; *p ; p = &(*p)->next)
10251025+ if ((*p)->pc_begin < ob->pc_begin)
10261026+ break;
10271027+ ob->next = *p;
10281028+ *p = ob;
10291029+10301030+ if (f)
10311031+ goto fini;
10321032+ }
10331033+10341034+ fini:
10351035+ __gthread_mutex_unlock (&object_mutex);
10361036+10371037+ if (f)
10381038+ {
10391039+ int encoding;
10401040+ _Unwind_Ptr func;
10411041+10421042+ bases->tbase = ob->tbase;
10431043+ bases->dbase = ob->dbase;
10441044+10451045+ encoding = ob->s.b.encoding;
10461046+ if (ob->s.b.mixed_encoding)
10471047+ encoding = get_fde_encoding (f);
10481048+ read_encoded_value_with_base (encoding, base_from_object (encoding, ob),
10491049+ f->pc_begin, &func);
10501050+ bases->func = (void *) func;
10511051+ }
10521052+10531053+ return f;
10541054+}
+183
src/libunwind-darwin/unwind-dw2-fde.h
···11+/* Subroutines needed for unwinding stack frames for exception handling. */
22+/* Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2004, 2009
33+ Free Software Foundation, Inc.
44+ Contributed by Jason Merrill <jason@cygnus.com>.
55+66+This file is part of GCC.
77+88+GCC is free software; you can redistribute it and/or modify it under
99+the terms of the GNU General Public License as published by the Free
1010+Software Foundation; either version 3, or (at your option) any later
1111+version.
1212+1313+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
1414+WARRANTY; without even the implied warranty of MERCHANTABILITY or
1515+FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1616+for more details.
1717+1818+Under Section 7 of GPL version 3, you are granted additional
1919+permissions described in the GCC Runtime Library Exception, version
2020+3.1, as published by the Free Software Foundation.
2121+2222+You should have received a copy of the GNU General Public License and
2323+a copy of the GCC Runtime Library Exception along with this program;
2424+see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
2525+<http://www.gnu.org/licenses/>. */
2626+2727+#ifndef GCC_UNWIND_DW2_FDE_H
2828+#define GCC_UNWIND_DW2_FDE_H
2929+3030+#ifndef HIDE_EXPORTS
3131+#pragma GCC visibility push(default)
3232+#endif
3333+3434+struct fde_vector
3535+{
3636+ const void *orig_data;
3737+ size_t count;
3838+ const struct dwarf_fde *array[];
3939+};
4040+4141+struct object
4242+{
4343+ void *pc_begin;
4444+ void *tbase;
4545+ void *dbase;
4646+ union {
4747+ const struct dwarf_fde *single;
4848+ struct dwarf_fde **array;
4949+ struct fde_vector *sort;
5050+ } u;
5151+5252+ union {
5353+ struct {
5454+ unsigned long sorted : 1;
5555+ unsigned long from_array : 1;
5656+ unsigned long mixed_encoding : 1;
5757+ unsigned long encoding : 8;
5858+ /* ??? Wish there was an easy way to detect a 64-bit host here;
5959+ we've got 32 bits left to play with... */
6060+ unsigned long count : 21;
6161+ } b;
6262+ size_t i;
6363+ } s;
6464+6565+#ifdef DWARF2_OBJECT_END_PTR_EXTENSION
6666+ char *fde_end;
6767+#endif
6868+6969+ struct object *next;
7070+};
7171+7272+/* This is the original definition of struct object. While the struct
7373+ itself was opaque to users, they did know how large it was, and
7474+ allocate one statically in crtbegin for each DSO. Keep this around
7575+ so that we're aware of the static size limitations for the new struct. */
7676+struct old_object
7777+{
7878+ void *pc_begin;
7979+ void *pc_end;
8080+ struct dwarf_fde *fde_begin;
8181+ struct dwarf_fde **fde_array;
8282+ size_t count;
8383+ struct old_object *next;
8484+};
8585+8686+struct dwarf_eh_bases
8787+{
8888+ void *tbase;
8989+ void *dbase;
9090+ void *func;
9191+};
9292+9393+9494+extern void __register_frame_info_bases (const void *, struct object *,
9595+ void *, void *);
9696+extern void __register_frame_info (const void *, struct object *);
9797+extern void __register_frame (void *);
9898+extern void __register_frame_info_table_bases (void *, struct object *,
9999+ void *, void *);
100100+extern void __register_frame_info_table (void *, struct object *);
101101+extern void __register_frame_table (void *);
102102+extern void *__deregister_frame_info (const void *);
103103+extern void *__deregister_frame_info_bases (const void *);
104104+extern void __deregister_frame (void *);
105105+106106+107107+typedef int sword __attribute__ ((mode (SI)));
108108+typedef unsigned int uword __attribute__ ((mode (SI)));
109109+typedef unsigned int uaddr __attribute__ ((mode (pointer)));
110110+typedef int saddr __attribute__ ((mode (pointer)));
111111+typedef unsigned char ubyte;
112112+113113+/* Terminology:
114114+ CIE - Common Information Element
115115+ FDE - Frame Descriptor Element
116116+117117+ There is one per function, and it describes where the function code
118118+ is located, and what the register lifetimes and stack layout are
119119+ within the function.
120120+121121+ The data structures are defined in the DWARF specification, although
122122+ not in a very readable way (see LITERATURE).
123123+124124+ Every time an exception is thrown, the code needs to locate the FDE
125125+ for the current function, and starts to look for exception regions
126126+ from that FDE. This works in a two-level search:
127127+ a) in a linear search, find the shared image (i.e. DLL) containing
128128+ the PC
129129+ b) using the FDE table for that shared object, locate the FDE using
130130+ binary search (which requires the sorting). */
131131+132132+/* The first few fields of a CIE. The CIE_id field is 0 for a CIE,
133133+ to distinguish it from a valid FDE. FDEs are aligned to an addressing
134134+ unit boundary, but the fields within are unaligned. */
135135+struct dwarf_cie
136136+{
137137+ uword length;
138138+ sword CIE_id;
139139+ ubyte version;
140140+ unsigned char augmentation[];
141141+} __attribute__ ((packed, aligned (__alignof__ (void *))));
142142+143143+/* The first few fields of an FDE. */
144144+struct dwarf_fde
145145+{
146146+ uword length;
147147+ sword CIE_delta;
148148+ unsigned char pc_begin[];
149149+} __attribute__ ((packed, aligned (__alignof__ (void *))));
150150+151151+typedef struct dwarf_fde fde;
152152+153153+/* Locate the CIE for a given FDE. */
154154+155155+static inline const struct dwarf_cie *
156156+get_cie (const struct dwarf_fde *f)
157157+{
158158+ return (const void *)&f->CIE_delta - f->CIE_delta;
159159+}
160160+161161+static inline const fde *
162162+next_fde (const fde *f)
163163+{
164164+ return (const fde *) ((const char *) f + f->length + sizeof (f->length));
165165+}
166166+167167+extern const fde * _Unwind_Find_FDE (void *, struct dwarf_eh_bases *);
168168+169169+static inline int
170170+last_fde (struct object *obj __attribute__ ((__unused__)), const fde *f)
171171+{
172172+#ifdef DWARF2_OBJECT_END_PTR_EXTENSION
173173+ return (char *)f == obj->fde_end || f->length == 0;
174174+#else
175175+ return f->length == 0;
176176+#endif
177177+}
178178+179179+#ifndef HIDE_EXPORTS
180180+#pragma GCC visibility pop
181181+#endif
182182+183183+#endif /* unwind-dw2-fde.h */
+1603
src/libunwind-darwin/unwind-dw2.c
···11+/* DWARF2 exception handling and frame unwind runtime interface routines.
22+ Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
33+ 2008, 2009, 2010 Free Software Foundation, Inc.
44+55+ This file is part of GCC.
66+77+ GCC is free software; you can redistribute it and/or modify it
88+ under the terms of the GNU General Public License as published by
99+ the Free Software Foundation; either version 3, or (at your option)
1010+ any later version.
1111+1212+ GCC is distributed in the hope that it will be useful, but WITHOUT
1313+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1414+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
1515+ License for more details.
1616+1717+ Under Section 7 of GPL version 3, you are granted additional
1818+ permissions described in the GCC Runtime Library Exception, version
1919+ 3.1, as published by the Free Software Foundation.
2020+2121+ You should have received a copy of the GNU General Public License and
2222+ a copy of the GCC Runtime Library Exception along with this program;
2323+ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
2424+ <http://www.gnu.org/licenses/>. */
2525+2626+#include "tconfig.h"
2727+#include "tsystem.h"
2828+#include "coretypes.h"
2929+#include "tm.h"
3030+#include "dwarf2.h"
3131+#include "unwind.h"
3232+#ifdef __USING_SJLJ_EXCEPTIONS__
3333+# define NO_SIZE_OF_ENCODED_VALUE
3434+#endif
3535+#include "unwind-pe.h"
3636+#include "unwind-dw2-fde.h"
3737+#include "gthr.h"
3838+#include "unwind-dw2.h"
3939+4040+#ifndef __USING_SJLJ_EXCEPTIONS__
4141+4242+#ifndef STACK_GROWS_DOWNWARD
4343+#define STACK_GROWS_DOWNWARD 0
4444+#else
4545+#undef STACK_GROWS_DOWNWARD
4646+#define STACK_GROWS_DOWNWARD 1
4747+#endif
4848+4949+/* Dwarf frame registers used for pre gcc 3.0 compiled glibc. */
5050+#ifndef PRE_GCC3_DWARF_FRAME_REGISTERS
5151+#define PRE_GCC3_DWARF_FRAME_REGISTERS DWARF_FRAME_REGISTERS
5252+#endif
5353+5454+#ifndef DWARF_REG_TO_UNWIND_COLUMN
5555+#define DWARF_REG_TO_UNWIND_COLUMN(REGNO) (REGNO)
5656+#endif
5757+5858+/* This is the register and unwind state for a particular frame. This
5959+ provides the information necessary to unwind up past a frame and return
6060+ to its caller. */
6161+struct _Unwind_Context
6262+{
6363+ void *reg[DWARF_FRAME_REGISTERS+1];
6464+ void *cfa;
6565+ void *ra;
6666+ void *lsda;
6767+ struct dwarf_eh_bases bases;
6868+ /* Signal frame context. */
6969+#define SIGNAL_FRAME_BIT ((~(_Unwind_Word) 0 >> 1) + 1)
7070+ /* Context which has version/args_size/by_value fields. */
7171+#define EXTENDED_CONTEXT_BIT ((~(_Unwind_Word) 0 >> 2) + 1)
7272+ _Unwind_Word flags;
7373+ /* 0 for now, can be increased when further fields are added to
7474+ struct _Unwind_Context. */
7575+ _Unwind_Word version;
7676+ _Unwind_Word args_size;
7777+ char by_value[DWARF_FRAME_REGISTERS+1];
7878+};
7979+8080+/* Byte size of every register managed by these routines. */
8181+static unsigned char dwarf_reg_size_table[DWARF_FRAME_REGISTERS+1];
8282+8383+8484+/* Read unaligned data from the instruction buffer. */
8585+8686+union unaligned
8787+{
8888+ void *p;
8989+ unsigned u2 __attribute__ ((mode (HI)));
9090+ unsigned u4 __attribute__ ((mode (SI)));
9191+ unsigned u8 __attribute__ ((mode (DI)));
9292+ signed s2 __attribute__ ((mode (HI)));
9393+ signed s4 __attribute__ ((mode (SI)));
9494+ signed s8 __attribute__ ((mode (DI)));
9595+} __attribute__ ((packed));
9696+9797+static void uw_update_context (struct _Unwind_Context *, _Unwind_FrameState *);
9898+static _Unwind_Reason_Code uw_frame_state_for (struct _Unwind_Context *,
9999+ _Unwind_FrameState *);
100100+101101+static inline void *
102102+read_pointer (const void *p) { const union unaligned *up = p; return up->p; }
103103+104104+static inline int
105105+read_1u (const void *p) { return *(const unsigned char *) p; }
106106+107107+static inline int
108108+read_1s (const void *p) { return *(const signed char *) p; }
109109+110110+static inline int
111111+read_2u (const void *p) { const union unaligned *up = p; return up->u2; }
112112+113113+static inline int
114114+read_2s (const void *p) { const union unaligned *up = p; return up->s2; }
115115+116116+static inline unsigned int
117117+read_4u (const void *p) { const union unaligned *up = p; return up->u4; }
118118+119119+static inline int
120120+read_4s (const void *p) { const union unaligned *up = p; return up->s4; }
121121+122122+static inline unsigned long
123123+read_8u (const void *p) { const union unaligned *up = p; return up->u8; }
124124+125125+static inline unsigned long
126126+read_8s (const void *p) { const union unaligned *up = p; return up->s8; }
127127+128128+static inline _Unwind_Word
129129+_Unwind_IsSignalFrame (struct _Unwind_Context *context)
130130+{
131131+ return (context->flags & SIGNAL_FRAME_BIT) ? 1 : 0;
132132+}
133133+134134+static inline void
135135+_Unwind_SetSignalFrame (struct _Unwind_Context *context, int val)
136136+{
137137+ if (val)
138138+ context->flags |= SIGNAL_FRAME_BIT;
139139+ else
140140+ context->flags &= ~SIGNAL_FRAME_BIT;
141141+}
142142+143143+static inline _Unwind_Word
144144+_Unwind_IsExtendedContext (struct _Unwind_Context *context)
145145+{
146146+ return context->flags & EXTENDED_CONTEXT_BIT;
147147+}
148148+149149+/* Get the value of register INDEX as saved in CONTEXT. */
150150+151151+inline _Unwind_Word
152152+_Unwind_GetGR (struct _Unwind_Context *context, int index)
153153+{
154154+ int size;
155155+ void *ptr;
156156+157157+#ifdef DWARF_ZERO_REG
158158+ if (index == DWARF_ZERO_REG)
159159+ return 0;
160160+#endif
161161+162162+ index = DWARF_REG_TO_UNWIND_COLUMN (index);
163163+ gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
164164+ size = dwarf_reg_size_table[index];
165165+ ptr = context->reg[index];
166166+167167+ if (_Unwind_IsExtendedContext (context) && context->by_value[index])
168168+ return (_Unwind_Word) (_Unwind_Internal_Ptr) ptr;
169169+170170+ /* This will segfault if the register hasn't been saved. */
171171+ if (size == sizeof(_Unwind_Ptr))
172172+ return * (_Unwind_Ptr *) ptr;
173173+ else
174174+ {
175175+ gcc_assert (size == sizeof(_Unwind_Word));
176176+ return * (_Unwind_Word *) ptr;
177177+ }
178178+}
179179+180180+static inline void *
181181+_Unwind_GetPtr (struct _Unwind_Context *context, int index)
182182+{
183183+ return (void *)(_Unwind_Ptr) _Unwind_GetGR (context, index);
184184+}
185185+186186+/* Get the value of the CFA as saved in CONTEXT. */
187187+188188+_Unwind_Word
189189+_Unwind_GetCFA (struct _Unwind_Context *context)
190190+{
191191+ return (_Unwind_Ptr) context->cfa;
192192+}
193193+194194+/* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
195195+196196+inline void
197197+_Unwind_SetGR (struct _Unwind_Context *context, int index, _Unwind_Word val)
198198+{
199199+ int size;
200200+ void *ptr;
201201+202202+ index = DWARF_REG_TO_UNWIND_COLUMN (index);
203203+ gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
204204+ size = dwarf_reg_size_table[index];
205205+206206+ if (_Unwind_IsExtendedContext (context) && context->by_value[index])
207207+ {
208208+ context->reg[index] = (void *) (_Unwind_Internal_Ptr) val;
209209+ return;
210210+ }
211211+212212+ ptr = context->reg[index];
213213+214214+ if (size == sizeof(_Unwind_Ptr))
215215+ * (_Unwind_Ptr *) ptr = val;
216216+ else
217217+ {
218218+ gcc_assert (size == sizeof(_Unwind_Word));
219219+ * (_Unwind_Word *) ptr = val;
220220+ }
221221+}
222222+223223+/* Get the pointer to a register INDEX as saved in CONTEXT. */
224224+225225+static inline void *
226226+_Unwind_GetGRPtr (struct _Unwind_Context *context, int index)
227227+{
228228+ index = DWARF_REG_TO_UNWIND_COLUMN (index);
229229+ if (_Unwind_IsExtendedContext (context) && context->by_value[index])
230230+ return &context->reg[index];
231231+ return context->reg[index];
232232+}
233233+234234+/* Set the pointer to a register INDEX as saved in CONTEXT. */
235235+236236+static inline void
237237+_Unwind_SetGRPtr (struct _Unwind_Context *context, int index, void *p)
238238+{
239239+ index = DWARF_REG_TO_UNWIND_COLUMN (index);
240240+ if (_Unwind_IsExtendedContext (context))
241241+ context->by_value[index] = 0;
242242+ context->reg[index] = p;
243243+}
244244+245245+/* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
246246+247247+static inline void
248248+_Unwind_SetGRValue (struct _Unwind_Context *context, int index,
249249+ _Unwind_Word val)
250250+{
251251+ index = DWARF_REG_TO_UNWIND_COLUMN (index);
252252+ gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
253253+ gcc_assert (dwarf_reg_size_table[index] == sizeof (_Unwind_Ptr));
254254+255255+ context->by_value[index] = 1;
256256+ context->reg[index] = (void *) (_Unwind_Internal_Ptr) val;
257257+}
258258+259259+/* Return nonzero if register INDEX is stored by value rather than
260260+ by reference. */
261261+262262+static inline int
263263+_Unwind_GRByValue (struct _Unwind_Context *context, int index)
264264+{
265265+ index = DWARF_REG_TO_UNWIND_COLUMN (index);
266266+ return context->by_value[index];
267267+}
268268+269269+/* Retrieve the return address for CONTEXT. */
270270+271271+inline _Unwind_Ptr
272272+_Unwind_GetIP (struct _Unwind_Context *context)
273273+{
274274+ return (_Unwind_Ptr) context->ra;
275275+}
276276+277277+/* Retrieve the return address and flag whether that IP is before
278278+ or after first not yet fully executed instruction. */
279279+280280+inline _Unwind_Ptr
281281+_Unwind_GetIPInfo (struct _Unwind_Context *context, int *ip_before_insn)
282282+{
283283+ *ip_before_insn = _Unwind_IsSignalFrame (context);
284284+ return (_Unwind_Ptr) context->ra;
285285+}
286286+287287+/* Overwrite the return address for CONTEXT with VAL. */
288288+289289+inline void
290290+_Unwind_SetIP (struct _Unwind_Context *context, _Unwind_Ptr val)
291291+{
292292+ context->ra = (void *) val;
293293+}
294294+295295+void *
296296+_Unwind_GetLanguageSpecificData (struct _Unwind_Context *context)
297297+{
298298+ return context->lsda;
299299+}
300300+301301+_Unwind_Ptr
302302+_Unwind_GetRegionStart (struct _Unwind_Context *context)
303303+{
304304+ return (_Unwind_Ptr) context->bases.func;
305305+}
306306+307307+void *
308308+_Unwind_FindEnclosingFunction (void *pc)
309309+{
310310+ struct dwarf_eh_bases bases;
311311+ const struct dwarf_fde *fde = _Unwind_Find_FDE (pc-1, &bases);
312312+ if (fde)
313313+ return bases.func;
314314+ else
315315+ return NULL;
316316+}
317317+318318+#ifndef __ia64__
319319+_Unwind_Ptr
320320+_Unwind_GetDataRelBase (struct _Unwind_Context *context)
321321+{
322322+ return (_Unwind_Ptr) context->bases.dbase;
323323+}
324324+325325+_Unwind_Ptr
326326+_Unwind_GetTextRelBase (struct _Unwind_Context *context)
327327+{
328328+ return (_Unwind_Ptr) context->bases.tbase;
329329+}
330330+#endif
331331+332332+#ifdef MD_UNWIND_SUPPORT
333333+#include MD_UNWIND_SUPPORT
334334+#endif
335335+336336+/* Extract any interesting information from the CIE for the translation
337337+ unit F belongs to. Return a pointer to the byte after the augmentation,
338338+ or NULL if we encountered an undecipherable augmentation. */
339339+340340+static const unsigned char *
341341+extract_cie_info (const struct dwarf_cie *cie, struct _Unwind_Context *context,
342342+ _Unwind_FrameState *fs)
343343+{
344344+ const unsigned char *aug = cie->augmentation;
345345+ const unsigned char *p = aug + strlen ((const char *)aug) + 1;
346346+ const unsigned char *ret = NULL;
347347+ _uleb128_t utmp;
348348+ _sleb128_t stmp;
349349+350350+ /* g++ v2 "eh" has pointer immediately following augmentation string,
351351+ so it must be handled first. */
352352+ if (aug[0] == 'e' && aug[1] == 'h')
353353+ {
354354+ fs->eh_ptr = read_pointer (p);
355355+ p += sizeof (void *);
356356+ aug += 2;
357357+ }
358358+359359+ /* After the augmentation resp. pointer for "eh" augmentation
360360+ follows for CIE version >= 4 address size byte and
361361+ segment size byte. */
362362+ if (__builtin_expect (cie->version >= 4, 0))
363363+ {
364364+ if (p[0] != sizeof (void *) || p[1] != 0)
365365+ return NULL;
366366+ p += 2;
367367+ }
368368+ /* Immediately following this are the code and
369369+ data alignment and return address column. */
370370+ p = read_uleb128 (p, &utmp);
371371+ fs->code_align = (_Unwind_Word)utmp;
372372+ p = read_sleb128 (p, &stmp);
373373+ fs->data_align = (_Unwind_Sword)stmp;
374374+ if (cie->version == 1)
375375+ fs->retaddr_column = *p++;
376376+ else
377377+ {
378378+ p = read_uleb128 (p, &utmp);
379379+ fs->retaddr_column = (_Unwind_Word)utmp;
380380+ }
381381+ fs->lsda_encoding = DW_EH_PE_omit;
382382+383383+ /* If the augmentation starts with 'z', then a uleb128 immediately
384384+ follows containing the length of the augmentation field following
385385+ the size. */
386386+ if (*aug == 'z')
387387+ {
388388+ p = read_uleb128 (p, &utmp);
389389+ ret = p + utmp;
390390+391391+ fs->saw_z = 1;
392392+ ++aug;
393393+ }
394394+395395+ /* Iterate over recognized augmentation subsequences. */
396396+ while (*aug != '\0')
397397+ {
398398+ /* "L" indicates a byte showing how the LSDA pointer is encoded. */
399399+ if (aug[0] == 'L')
400400+ {
401401+ fs->lsda_encoding = *p++;
402402+ aug += 1;
403403+ }
404404+405405+ /* "R" indicates a byte indicating how FDE addresses are encoded. */
406406+ else if (aug[0] == 'R')
407407+ {
408408+ fs->fde_encoding = *p++;
409409+ aug += 1;
410410+ }
411411+412412+ /* "P" indicates a personality routine in the CIE augmentation. */
413413+ else if (aug[0] == 'P')
414414+ {
415415+ _Unwind_Ptr personality;
416416+417417+ p = read_encoded_value (context, *p, p + 1, &personality);
418418+ fs->personality = (_Unwind_Personality_Fn) personality;
419419+ aug += 1;
420420+ }
421421+422422+ /* "S" indicates a signal frame. */
423423+ else if (aug[0] == 'S')
424424+ {
425425+ fs->signal_frame = 1;
426426+ aug += 1;
427427+ }
428428+429429+ /* Otherwise we have an unknown augmentation string.
430430+ Bail unless we saw a 'z' prefix. */
431431+ else
432432+ return ret;
433433+ }
434434+435435+ return ret ? ret : p;
436436+}
437437+438438+439439+/* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
440440+ onto the stack to start. */
441441+442442+static _Unwind_Word
443443+execute_stack_op (const unsigned char *op_ptr, const unsigned char *op_end,
444444+ struct _Unwind_Context *context, _Unwind_Word initial)
445445+{
446446+ _Unwind_Word stack[64]; /* ??? Assume this is enough. */
447447+ int stack_elt;
448448+449449+ stack[0] = initial;
450450+ stack_elt = 1;
451451+452452+ while (op_ptr < op_end)
453453+ {
454454+ enum dwarf_location_atom op = *op_ptr++;
455455+ _Unwind_Word result;
456456+ _uleb128_t reg, utmp;
457457+ _sleb128_t offset, stmp;
458458+459459+ switch (op)
460460+ {
461461+ case DW_OP_lit0:
462462+ case DW_OP_lit1:
463463+ case DW_OP_lit2:
464464+ case DW_OP_lit3:
465465+ case DW_OP_lit4:
466466+ case DW_OP_lit5:
467467+ case DW_OP_lit6:
468468+ case DW_OP_lit7:
469469+ case DW_OP_lit8:
470470+ case DW_OP_lit9:
471471+ case DW_OP_lit10:
472472+ case DW_OP_lit11:
473473+ case DW_OP_lit12:
474474+ case DW_OP_lit13:
475475+ case DW_OP_lit14:
476476+ case DW_OP_lit15:
477477+ case DW_OP_lit16:
478478+ case DW_OP_lit17:
479479+ case DW_OP_lit18:
480480+ case DW_OP_lit19:
481481+ case DW_OP_lit20:
482482+ case DW_OP_lit21:
483483+ case DW_OP_lit22:
484484+ case DW_OP_lit23:
485485+ case DW_OP_lit24:
486486+ case DW_OP_lit25:
487487+ case DW_OP_lit26:
488488+ case DW_OP_lit27:
489489+ case DW_OP_lit28:
490490+ case DW_OP_lit29:
491491+ case DW_OP_lit30:
492492+ case DW_OP_lit31:
493493+ result = op - DW_OP_lit0;
494494+ break;
495495+496496+ case DW_OP_addr:
497497+ result = (_Unwind_Word) (_Unwind_Ptr) read_pointer (op_ptr);
498498+ op_ptr += sizeof (void *);
499499+ break;
500500+501501+ case DW_OP_GNU_encoded_addr:
502502+ {
503503+ _Unwind_Ptr presult;
504504+ op_ptr = read_encoded_value (context, *op_ptr, op_ptr+1, &presult);
505505+ result = presult;
506506+ }
507507+ break;
508508+509509+ case DW_OP_const1u:
510510+ result = read_1u (op_ptr);
511511+ op_ptr += 1;
512512+ break;
513513+ case DW_OP_const1s:
514514+ result = read_1s (op_ptr);
515515+ op_ptr += 1;
516516+ break;
517517+ case DW_OP_const2u:
518518+ result = read_2u (op_ptr);
519519+ op_ptr += 2;
520520+ break;
521521+ case DW_OP_const2s:
522522+ result = read_2s (op_ptr);
523523+ op_ptr += 2;
524524+ break;
525525+ case DW_OP_const4u:
526526+ result = read_4u (op_ptr);
527527+ op_ptr += 4;
528528+ break;
529529+ case DW_OP_const4s:
530530+ result = read_4s (op_ptr);
531531+ op_ptr += 4;
532532+ break;
533533+ case DW_OP_const8u:
534534+ result = read_8u (op_ptr);
535535+ op_ptr += 8;
536536+ break;
537537+ case DW_OP_const8s:
538538+ result = read_8s (op_ptr);
539539+ op_ptr += 8;
540540+ break;
541541+ case DW_OP_constu:
542542+ op_ptr = read_uleb128 (op_ptr, &utmp);
543543+ result = (_Unwind_Word)utmp;
544544+ break;
545545+ case DW_OP_consts:
546546+ op_ptr = read_sleb128 (op_ptr, &stmp);
547547+ result = (_Unwind_Sword)stmp;
548548+ break;
549549+550550+ case DW_OP_reg0:
551551+ case DW_OP_reg1:
552552+ case DW_OP_reg2:
553553+ case DW_OP_reg3:
554554+ case DW_OP_reg4:
555555+ case DW_OP_reg5:
556556+ case DW_OP_reg6:
557557+ case DW_OP_reg7:
558558+ case DW_OP_reg8:
559559+ case DW_OP_reg9:
560560+ case DW_OP_reg10:
561561+ case DW_OP_reg11:
562562+ case DW_OP_reg12:
563563+ case DW_OP_reg13:
564564+ case DW_OP_reg14:
565565+ case DW_OP_reg15:
566566+ case DW_OP_reg16:
567567+ case DW_OP_reg17:
568568+ case DW_OP_reg18:
569569+ case DW_OP_reg19:
570570+ case DW_OP_reg20:
571571+ case DW_OP_reg21:
572572+ case DW_OP_reg22:
573573+ case DW_OP_reg23:
574574+ case DW_OP_reg24:
575575+ case DW_OP_reg25:
576576+ case DW_OP_reg26:
577577+ case DW_OP_reg27:
578578+ case DW_OP_reg28:
579579+ case DW_OP_reg29:
580580+ case DW_OP_reg30:
581581+ case DW_OP_reg31:
582582+ result = _Unwind_GetGR (context, op - DW_OP_reg0);
583583+ break;
584584+ case DW_OP_regx:
585585+ op_ptr = read_uleb128 (op_ptr, ®);
586586+ result = _Unwind_GetGR (context, reg);
587587+ break;
588588+589589+ case DW_OP_breg0:
590590+ case DW_OP_breg1:
591591+ case DW_OP_breg2:
592592+ case DW_OP_breg3:
593593+ case DW_OP_breg4:
594594+ case DW_OP_breg5:
595595+ case DW_OP_breg6:
596596+ case DW_OP_breg7:
597597+ case DW_OP_breg8:
598598+ case DW_OP_breg9:
599599+ case DW_OP_breg10:
600600+ case DW_OP_breg11:
601601+ case DW_OP_breg12:
602602+ case DW_OP_breg13:
603603+ case DW_OP_breg14:
604604+ case DW_OP_breg15:
605605+ case DW_OP_breg16:
606606+ case DW_OP_breg17:
607607+ case DW_OP_breg18:
608608+ case DW_OP_breg19:
609609+ case DW_OP_breg20:
610610+ case DW_OP_breg21:
611611+ case DW_OP_breg22:
612612+ case DW_OP_breg23:
613613+ case DW_OP_breg24:
614614+ case DW_OP_breg25:
615615+ case DW_OP_breg26:
616616+ case DW_OP_breg27:
617617+ case DW_OP_breg28:
618618+ case DW_OP_breg29:
619619+ case DW_OP_breg30:
620620+ case DW_OP_breg31:
621621+ op_ptr = read_sleb128 (op_ptr, &offset);
622622+ result = _Unwind_GetGR (context, op - DW_OP_breg0) + offset;
623623+ break;
624624+ case DW_OP_bregx:
625625+ op_ptr = read_uleb128 (op_ptr, ®);
626626+ op_ptr = read_sleb128 (op_ptr, &offset);
627627+ result = _Unwind_GetGR (context, reg) + (_Unwind_Word)offset;
628628+ break;
629629+630630+ case DW_OP_dup:
631631+ gcc_assert (stack_elt);
632632+ result = stack[stack_elt - 1];
633633+ break;
634634+635635+ case DW_OP_drop:
636636+ gcc_assert (stack_elt);
637637+ stack_elt -= 1;
638638+ goto no_push;
639639+640640+ case DW_OP_pick:
641641+ offset = *op_ptr++;
642642+ gcc_assert (offset < stack_elt - 1);
643643+ result = stack[stack_elt - 1 - offset];
644644+ break;
645645+646646+ case DW_OP_over:
647647+ gcc_assert (stack_elt >= 2);
648648+ result = stack[stack_elt - 2];
649649+ break;
650650+651651+ case DW_OP_swap:
652652+ {
653653+ _Unwind_Word t;
654654+ gcc_assert (stack_elt >= 2);
655655+ t = stack[stack_elt - 1];
656656+ stack[stack_elt - 1] = stack[stack_elt - 2];
657657+ stack[stack_elt - 2] = t;
658658+ goto no_push;
659659+ }
660660+661661+ case DW_OP_rot:
662662+ {
663663+ _Unwind_Word t1, t2, t3;
664664+665665+ gcc_assert (stack_elt >= 3);
666666+ t1 = stack[stack_elt - 1];
667667+ t2 = stack[stack_elt - 2];
668668+ t3 = stack[stack_elt - 3];
669669+ stack[stack_elt - 1] = t2;
670670+ stack[stack_elt - 2] = t3;
671671+ stack[stack_elt - 3] = t1;
672672+ goto no_push;
673673+ }
674674+675675+ case DW_OP_deref:
676676+ case DW_OP_deref_size:
677677+ case DW_OP_abs:
678678+ case DW_OP_neg:
679679+ case DW_OP_not:
680680+ case DW_OP_plus_uconst:
681681+ /* Unary operations. */
682682+ gcc_assert (stack_elt);
683683+ stack_elt -= 1;
684684+685685+ result = stack[stack_elt];
686686+687687+ switch (op)
688688+ {
689689+ case DW_OP_deref:
690690+ {
691691+ void *ptr = (void *) (_Unwind_Ptr) result;
692692+ result = (_Unwind_Ptr) read_pointer (ptr);
693693+ }
694694+ break;
695695+696696+ case DW_OP_deref_size:
697697+ {
698698+ void *ptr = (void *) (_Unwind_Ptr) result;
699699+ switch (*op_ptr++)
700700+ {
701701+ case 1:
702702+ result = read_1u (ptr);
703703+ break;
704704+ case 2:
705705+ result = read_2u (ptr);
706706+ break;
707707+ case 4:
708708+ result = read_4u (ptr);
709709+ break;
710710+ case 8:
711711+ result = read_8u (ptr);
712712+ break;
713713+ default:
714714+ gcc_unreachable ();
715715+ }
716716+ }
717717+ break;
718718+719719+ case DW_OP_abs:
720720+ if ((_Unwind_Sword) result < 0)
721721+ result = -result;
722722+ break;
723723+ case DW_OP_neg:
724724+ result = -result;
725725+ break;
726726+ case DW_OP_not:
727727+ result = ~result;
728728+ break;
729729+ case DW_OP_plus_uconst:
730730+ op_ptr = read_uleb128 (op_ptr, &utmp);
731731+ result += (_Unwind_Word)utmp;
732732+ break;
733733+734734+ default:
735735+ gcc_unreachable ();
736736+ }
737737+ break;
738738+739739+ case DW_OP_and:
740740+ case DW_OP_div:
741741+ case DW_OP_minus:
742742+ case DW_OP_mod:
743743+ case DW_OP_mul:
744744+ case DW_OP_or:
745745+ case DW_OP_plus:
746746+ case DW_OP_shl:
747747+ case DW_OP_shr:
748748+ case DW_OP_shra:
749749+ case DW_OP_xor:
750750+ case DW_OP_le:
751751+ case DW_OP_ge:
752752+ case DW_OP_eq:
753753+ case DW_OP_lt:
754754+ case DW_OP_gt:
755755+ case DW_OP_ne:
756756+ {
757757+ /* Binary operations. */
758758+ _Unwind_Word first, second;
759759+ gcc_assert (stack_elt >= 2);
760760+ stack_elt -= 2;
761761+762762+ second = stack[stack_elt];
763763+ first = stack[stack_elt + 1];
764764+765765+ switch (op)
766766+ {
767767+ case DW_OP_and:
768768+ result = second & first;
769769+ break;
770770+ case DW_OP_div:
771771+ result = (_Unwind_Sword) second / (_Unwind_Sword) first;
772772+ break;
773773+ case DW_OP_minus:
774774+ result = second - first;
775775+ break;
776776+ case DW_OP_mod:
777777+ result = second % first;
778778+ break;
779779+ case DW_OP_mul:
780780+ result = second * first;
781781+ break;
782782+ case DW_OP_or:
783783+ result = second | first;
784784+ break;
785785+ case DW_OP_plus:
786786+ result = second + first;
787787+ break;
788788+ case DW_OP_shl:
789789+ result = second << first;
790790+ break;
791791+ case DW_OP_shr:
792792+ result = second >> first;
793793+ break;
794794+ case DW_OP_shra:
795795+ result = (_Unwind_Sword) second >> first;
796796+ break;
797797+ case DW_OP_xor:
798798+ result = second ^ first;
799799+ break;
800800+ case DW_OP_le:
801801+ result = (_Unwind_Sword) second <= (_Unwind_Sword) first;
802802+ break;
803803+ case DW_OP_ge:
804804+ result = (_Unwind_Sword) second >= (_Unwind_Sword) first;
805805+ break;
806806+ case DW_OP_eq:
807807+ result = (_Unwind_Sword) second == (_Unwind_Sword) first;
808808+ break;
809809+ case DW_OP_lt:
810810+ result = (_Unwind_Sword) second < (_Unwind_Sword) first;
811811+ break;
812812+ case DW_OP_gt:
813813+ result = (_Unwind_Sword) second > (_Unwind_Sword) first;
814814+ break;
815815+ case DW_OP_ne:
816816+ result = (_Unwind_Sword) second != (_Unwind_Sword) first;
817817+ break;
818818+819819+ default:
820820+ gcc_unreachable ();
821821+ }
822822+ }
823823+ break;
824824+825825+ case DW_OP_skip:
826826+ offset = read_2s (op_ptr);
827827+ op_ptr += 2;
828828+ op_ptr += offset;
829829+ goto no_push;
830830+831831+ case DW_OP_bra:
832832+ gcc_assert (stack_elt);
833833+ stack_elt -= 1;
834834+835835+ offset = read_2s (op_ptr);
836836+ op_ptr += 2;
837837+ if (stack[stack_elt] != 0)
838838+ op_ptr += offset;
839839+ goto no_push;
840840+841841+ case DW_OP_nop:
842842+ goto no_push;
843843+844844+ default:
845845+ gcc_unreachable ();
846846+ }
847847+848848+ /* Most things push a result value. */
849849+ gcc_assert ((size_t) stack_elt < sizeof(stack)/sizeof(*stack));
850850+ stack[stack_elt++] = result;
851851+ no_push:;
852852+ }
853853+854854+ /* We were executing this program to get a value. It should be
855855+ at top of stack. */
856856+ gcc_assert (stack_elt);
857857+ stack_elt -= 1;
858858+ return stack[stack_elt];
859859+}
860860+861861+862862+/* Decode DWARF 2 call frame information. Takes pointers the
863863+ instruction sequence to decode, current register information and
864864+ CIE info, and the PC range to evaluate. */
865865+866866+static void
867867+execute_cfa_program (const unsigned char *insn_ptr,
868868+ const unsigned char *insn_end,
869869+ struct _Unwind_Context *context,
870870+ _Unwind_FrameState *fs)
871871+{
872872+ struct frame_state_reg_info *unused_rs = NULL;
873873+874874+ /* Don't allow remember/restore between CIE and FDE programs. */
875875+ fs->regs.prev = NULL;
876876+877877+ /* The comparison with the return address uses < rather than <= because
878878+ we are only interested in the effects of code before the call; for a
879879+ noreturn function, the return address may point to unrelated code with
880880+ a different stack configuration that we are not interested in. We
881881+ assume that the call itself is unwind info-neutral; if not, or if
882882+ there are delay instructions that adjust the stack, these must be
883883+ reflected at the point immediately before the call insn.
884884+ In signal frames, return address is after last completed instruction,
885885+ so we add 1 to return address to make the comparison <=. */
886886+ while (insn_ptr < insn_end
887887+ && fs->pc < context->ra + _Unwind_IsSignalFrame (context))
888888+ {
889889+ unsigned char insn = *insn_ptr++;
890890+ _uleb128_t reg, utmp;
891891+ _sleb128_t offset, stmp;
892892+893893+ if ((insn & 0xc0) == DW_CFA_advance_loc)
894894+ fs->pc += (insn & 0x3f) * fs->code_align;
895895+ else if ((insn & 0xc0) == DW_CFA_offset)
896896+ {
897897+ reg = insn & 0x3f;
898898+ insn_ptr = read_uleb128 (insn_ptr, &utmp);
899899+ offset = (_Unwind_Sword) utmp * fs->data_align;
900900+ fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
901901+ = REG_SAVED_OFFSET;
902902+ fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
903903+ }
904904+ else if ((insn & 0xc0) == DW_CFA_restore)
905905+ {
906906+ reg = insn & 0x3f;
907907+ fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_UNSAVED;
908908+ }
909909+ else switch (insn)
910910+ {
911911+ case DW_CFA_set_loc:
912912+ {
913913+ _Unwind_Ptr pc;
914914+915915+ insn_ptr = read_encoded_value (context, fs->fde_encoding,
916916+ insn_ptr, &pc);
917917+ fs->pc = (void *) pc;
918918+ }
919919+ break;
920920+921921+ case DW_CFA_advance_loc1:
922922+ fs->pc += read_1u (insn_ptr) * fs->code_align;
923923+ insn_ptr += 1;
924924+ break;
925925+ case DW_CFA_advance_loc2:
926926+ fs->pc += read_2u (insn_ptr) * fs->code_align;
927927+ insn_ptr += 2;
928928+ break;
929929+ case DW_CFA_advance_loc4:
930930+ fs->pc += read_4u (insn_ptr) * fs->code_align;
931931+ insn_ptr += 4;
932932+ break;
933933+934934+ case DW_CFA_offset_extended:
935935+ insn_ptr = read_uleb128 (insn_ptr, ®);
936936+ insn_ptr = read_uleb128 (insn_ptr, &utmp);
937937+ offset = (_Unwind_Sword) utmp * fs->data_align;
938938+ fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
939939+ = REG_SAVED_OFFSET;
940940+ fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
941941+ break;
942942+943943+ case DW_CFA_restore_extended:
944944+ insn_ptr = read_uleb128 (insn_ptr, ®);
945945+ /* FIXME, this is wrong; the CIE might have said that the
946946+ register was saved somewhere. */
947947+ fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN(reg)].how = REG_UNSAVED;
948948+ break;
949949+950950+ case DW_CFA_same_value:
951951+ insn_ptr = read_uleb128 (insn_ptr, ®);
952952+ fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN(reg)].how = REG_UNSAVED;
953953+ break;
954954+955955+ case DW_CFA_undefined:
956956+ insn_ptr = read_uleb128 (insn_ptr, ®);
957957+ fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN(reg)].how = REG_UNDEFINED;
958958+ break;
959959+960960+ case DW_CFA_nop:
961961+ break;
962962+963963+ case DW_CFA_register:
964964+ {
965965+ _uleb128_t reg2;
966966+ insn_ptr = read_uleb128 (insn_ptr, ®);
967967+ insn_ptr = read_uleb128 (insn_ptr, ®2);
968968+ fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_SAVED_REG;
969969+ fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.reg =
970970+ (_Unwind_Word)reg2;
971971+ }
972972+ break;
973973+974974+ case DW_CFA_remember_state:
975975+ {
976976+ struct frame_state_reg_info *new_rs;
977977+ if (unused_rs)
978978+ {
979979+ new_rs = unused_rs;
980980+ unused_rs = unused_rs->prev;
981981+ }
982982+ else
983983+ new_rs = alloca (sizeof (struct frame_state_reg_info));
984984+985985+ *new_rs = fs->regs;
986986+ fs->regs.prev = new_rs;
987987+ }
988988+ break;
989989+990990+ case DW_CFA_restore_state:
991991+ {
992992+ struct frame_state_reg_info *old_rs = fs->regs.prev;
993993+ fs->regs = *old_rs;
994994+ old_rs->prev = unused_rs;
995995+ unused_rs = old_rs;
996996+ }
997997+ break;
998998+999999+ case DW_CFA_def_cfa:
10001000+ insn_ptr = read_uleb128 (insn_ptr, &utmp);
10011001+ fs->regs.cfa_reg = (_Unwind_Word)utmp;
10021002+ insn_ptr = read_uleb128 (insn_ptr, &utmp);
10031003+ fs->regs.cfa_offset = (_Unwind_Word)utmp;
10041004+ fs->regs.cfa_how = CFA_REG_OFFSET;
10051005+ break;
10061006+10071007+ case DW_CFA_def_cfa_register:
10081008+ insn_ptr = read_uleb128 (insn_ptr, &utmp);
10091009+ fs->regs.cfa_reg = (_Unwind_Word)utmp;
10101010+ fs->regs.cfa_how = CFA_REG_OFFSET;
10111011+ break;
10121012+10131013+ case DW_CFA_def_cfa_offset:
10141014+ insn_ptr = read_uleb128 (insn_ptr, &utmp);
10151015+ fs->regs.cfa_offset = utmp;
10161016+ /* cfa_how deliberately not set. */
10171017+ break;
10181018+10191019+ case DW_CFA_def_cfa_expression:
10201020+ fs->regs.cfa_exp = insn_ptr;
10211021+ fs->regs.cfa_how = CFA_EXP;
10221022+ insn_ptr = read_uleb128 (insn_ptr, &utmp);
10231023+ insn_ptr += utmp;
10241024+ break;
10251025+10261026+ case DW_CFA_expression:
10271027+ insn_ptr = read_uleb128 (insn_ptr, ®);
10281028+ fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how = REG_SAVED_EXP;
10291029+ fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.exp = insn_ptr;
10301030+ insn_ptr = read_uleb128 (insn_ptr, &utmp);
10311031+ insn_ptr += utmp;
10321032+ break;
10331033+10341034+ /* Dwarf3. */
10351035+ case DW_CFA_offset_extended_sf:
10361036+ insn_ptr = read_uleb128 (insn_ptr, ®);
10371037+ insn_ptr = read_sleb128 (insn_ptr, &stmp);
10381038+ offset = stmp * fs->data_align;
10391039+ fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
10401040+ = REG_SAVED_OFFSET;
10411041+ fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
10421042+ break;
10431043+10441044+ case DW_CFA_def_cfa_sf:
10451045+ insn_ptr = read_uleb128 (insn_ptr, &utmp);
10461046+ fs->regs.cfa_reg = (_Unwind_Word)utmp;
10471047+ insn_ptr = read_sleb128 (insn_ptr, &stmp);
10481048+ fs->regs.cfa_offset = (_Unwind_Sword)stmp;
10491049+ fs->regs.cfa_how = CFA_REG_OFFSET;
10501050+ fs->regs.cfa_offset *= fs->data_align;
10511051+ break;
10521052+10531053+ case DW_CFA_def_cfa_offset_sf:
10541054+ insn_ptr = read_sleb128 (insn_ptr, &stmp);
10551055+ fs->regs.cfa_offset = (_Unwind_Sword)stmp;
10561056+ fs->regs.cfa_offset *= fs->data_align;
10571057+ /* cfa_how deliberately not set. */
10581058+ break;
10591059+10601060+ case DW_CFA_val_offset:
10611061+ insn_ptr = read_uleb128 (insn_ptr, ®);
10621062+ insn_ptr = read_uleb128 (insn_ptr, &utmp);
10631063+ offset = (_Unwind_Sword) utmp * fs->data_align;
10641064+ fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
10651065+ = REG_SAVED_VAL_OFFSET;
10661066+ fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
10671067+ break;
10681068+10691069+ case DW_CFA_val_offset_sf:
10701070+ insn_ptr = read_uleb128 (insn_ptr, ®);
10711071+ insn_ptr = read_sleb128 (insn_ptr, &stmp);
10721072+ offset = stmp * fs->data_align;
10731073+ fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
10741074+ = REG_SAVED_VAL_OFFSET;
10751075+ fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = offset;
10761076+ break;
10771077+10781078+ case DW_CFA_val_expression:
10791079+ insn_ptr = read_uleb128 (insn_ptr, ®);
10801080+ fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
10811081+ = REG_SAVED_VAL_EXP;
10821082+ fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.exp = insn_ptr;
10831083+ insn_ptr = read_uleb128 (insn_ptr, &utmp);
10841084+ insn_ptr += utmp;
10851085+ break;
10861086+10871087+ case DW_CFA_GNU_window_save:
10881088+ /* ??? Hardcoded for SPARC register window configuration. */
10891089+ for (reg = 16; reg < 32; ++reg)
10901090+ {
10911091+ fs->regs.reg[reg].how = REG_SAVED_OFFSET;
10921092+ fs->regs.reg[reg].loc.offset = (reg - 16) * sizeof (void *);
10931093+ }
10941094+ break;
10951095+10961096+ case DW_CFA_GNU_args_size:
10971097+ insn_ptr = read_uleb128 (insn_ptr, &utmp);
10981098+ context->args_size = (_Unwind_Word)utmp;
10991099+ break;
11001100+11011101+ case DW_CFA_GNU_negative_offset_extended:
11021102+ /* Obsoleted by DW_CFA_offset_extended_sf, but used by
11031103+ older PowerPC code. */
11041104+ insn_ptr = read_uleb128 (insn_ptr, ®);
11051105+ insn_ptr = read_uleb128 (insn_ptr, &utmp);
11061106+ offset = (_Unwind_Word) utmp * fs->data_align;
11071107+ fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].how
11081108+ = REG_SAVED_OFFSET;
11091109+ fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (reg)].loc.offset = -offset;
11101110+ break;
11111111+11121112+ default:
11131113+ gcc_unreachable ();
11141114+ }
11151115+ }
11161116+}
11171117+11181118+/* Given the _Unwind_Context CONTEXT for a stack frame, look up the FDE for
11191119+ its caller and decode it into FS. This function also sets the
11201120+ args_size and lsda members of CONTEXT, as they are really information
11211121+ about the caller's frame. */
11221122+11231123+static _Unwind_Reason_Code
11241124+uw_frame_state_for (struct _Unwind_Context *context, _Unwind_FrameState *fs)
11251125+{
11261126+ const struct dwarf_fde *fde;
11271127+ const struct dwarf_cie *cie;
11281128+ const unsigned char *aug, *insn, *end;
11291129+11301130+ memset (fs, 0, sizeof (*fs));
11311131+ context->args_size = 0;
11321132+ context->lsda = 0;
11331133+11341134+ if (context->ra == 0)
11351135+ return _URC_END_OF_STACK;
11361136+11371137+ fde = _Unwind_Find_FDE (context->ra + _Unwind_IsSignalFrame (context) - 1,
11381138+ &context->bases);
11391139+ if (fde == NULL)
11401140+ {
11411141+#ifdef MD_FALLBACK_FRAME_STATE_FOR
11421142+ /* Couldn't find frame unwind info for this function. Try a
11431143+ target-specific fallback mechanism. This will necessarily
11441144+ not provide a personality routine or LSDA. */
11451145+ return MD_FALLBACK_FRAME_STATE_FOR (context, fs);
11461146+#else
11471147+ return _URC_END_OF_STACK;
11481148+#endif
11491149+ }
11501150+11511151+ fs->pc = context->bases.func;
11521152+11531153+ cie = get_cie (fde);
11541154+ insn = extract_cie_info (cie, context, fs);
11551155+ if (insn == NULL)
11561156+ /* CIE contained unknown augmentation. */
11571157+ return _URC_FATAL_PHASE1_ERROR;
11581158+11591159+ /* First decode all the insns in the CIE. */
11601160+ end = (const unsigned char *) next_fde ((const struct dwarf_fde *) cie);
11611161+ execute_cfa_program (insn, end, context, fs);
11621162+11631163+ /* Locate augmentation for the fde. */
11641164+ aug = (const unsigned char *) fde + sizeof (*fde);
11651165+ aug += 2 * size_of_encoded_value (fs->fde_encoding);
11661166+ insn = NULL;
11671167+ if (fs->saw_z)
11681168+ {
11691169+ _uleb128_t i;
11701170+ aug = read_uleb128 (aug, &i);
11711171+ insn = aug + i;
11721172+ }
11731173+ if (fs->lsda_encoding != DW_EH_PE_omit)
11741174+ {
11751175+ _Unwind_Ptr lsda;
11761176+11771177+ aug = read_encoded_value (context, fs->lsda_encoding, aug, &lsda);
11781178+ context->lsda = (void *) lsda;
11791179+ }
11801180+11811181+ /* Then the insns in the FDE up to our target PC. */
11821182+ if (insn == NULL)
11831183+ insn = aug;
11841184+ end = (const unsigned char *) next_fde (fde);
11851185+ execute_cfa_program (insn, end, context, fs);
11861186+11871187+ return _URC_NO_REASON;
11881188+}
11891189+11901190+typedef struct frame_state
11911191+{
11921192+ void *cfa;
11931193+ void *eh_ptr;
11941194+ long cfa_offset;
11951195+ long args_size;
11961196+ long reg_or_offset[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
11971197+ unsigned short cfa_reg;
11981198+ unsigned short retaddr_column;
11991199+ char saved[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
12001200+} frame_state;
12011201+12021202+struct frame_state * __frame_state_for (void *, struct frame_state *);
12031203+12041204+/* Called from pre-G++ 3.0 __throw to find the registers to restore for
12051205+ a given PC_TARGET. The caller should allocate a local variable of
12061206+ `struct frame_state' and pass its address to STATE_IN. */
12071207+12081208+struct frame_state *
12091209+__frame_state_for (void *pc_target, struct frame_state *state_in)
12101210+{
12111211+ struct _Unwind_Context context;
12121212+ _Unwind_FrameState fs;
12131213+ int reg;
12141214+12151215+ memset (&context, 0, sizeof (struct _Unwind_Context));
12161216+ context.flags = EXTENDED_CONTEXT_BIT;
12171217+ context.ra = pc_target + 1;
12181218+12191219+ if (uw_frame_state_for (&context, &fs) != _URC_NO_REASON)
12201220+ return 0;
12211221+12221222+ /* We have no way to pass a location expression for the CFA to our
12231223+ caller. It wouldn't understand it anyway. */
12241224+ if (fs.regs.cfa_how == CFA_EXP)
12251225+ return 0;
12261226+12271227+ for (reg = 0; reg < PRE_GCC3_DWARF_FRAME_REGISTERS + 1; reg++)
12281228+ {
12291229+ state_in->saved[reg] = fs.regs.reg[reg].how;
12301230+ switch (state_in->saved[reg])
12311231+ {
12321232+ case REG_SAVED_REG:
12331233+ state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.reg;
12341234+ break;
12351235+ case REG_SAVED_OFFSET:
12361236+ state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.offset;
12371237+ break;
12381238+ default:
12391239+ state_in->reg_or_offset[reg] = 0;
12401240+ break;
12411241+ }
12421242+ }
12431243+12441244+ state_in->cfa_offset = fs.regs.cfa_offset;
12451245+ state_in->cfa_reg = fs.regs.cfa_reg;
12461246+ state_in->retaddr_column = fs.retaddr_column;
12471247+ state_in->args_size = context.args_size;
12481248+ state_in->eh_ptr = fs.eh_ptr;
12491249+12501250+ return state_in;
12511251+}
12521252+12531253+typedef union { _Unwind_Ptr ptr; _Unwind_Word word; } _Unwind_SpTmp;
12541254+12551255+static inline void
12561256+_Unwind_SetSpColumn (struct _Unwind_Context *context, void *cfa,
12571257+ _Unwind_SpTmp *tmp_sp)
12581258+{
12591259+ int size = dwarf_reg_size_table[__builtin_dwarf_sp_column ()];
12601260+12611261+ if (size == sizeof(_Unwind_Ptr))
12621262+ tmp_sp->ptr = (_Unwind_Ptr) cfa;
12631263+ else
12641264+ {
12651265+ gcc_assert (size == sizeof(_Unwind_Word));
12661266+ tmp_sp->word = (_Unwind_Ptr) cfa;
12671267+ }
12681268+ _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), tmp_sp);
12691269+}
12701270+12711271+static void
12721272+uw_update_context_1 (struct _Unwind_Context *context, _Unwind_FrameState *fs)
12731273+{
12741274+ struct _Unwind_Context orig_context = *context;
12751275+ void *cfa;
12761276+ long i;
12771277+12781278+#ifdef EH_RETURN_STACKADJ_RTX
12791279+ /* Special handling here: Many machines do not use a frame pointer,
12801280+ and track the CFA only through offsets from the stack pointer from
12811281+ one frame to the next. In this case, the stack pointer is never
12821282+ stored, so it has no saved address in the context. What we do
12831283+ have is the CFA from the previous stack frame.
12841284+12851285+ In very special situations (such as unwind info for signal return),
12861286+ there may be location expressions that use the stack pointer as well.
12871287+12881288+ Do this conditionally for one frame. This allows the unwind info
12891289+ for one frame to save a copy of the stack pointer from the previous
12901290+ frame, and be able to use much easier CFA mechanisms to do it.
12911291+ Always zap the saved stack pointer value for the next frame; carrying
12921292+ the value over from one frame to another doesn't make sense. */
12931293+12941294+ _Unwind_SpTmp tmp_sp;
12951295+12961296+ if (!_Unwind_GetGRPtr (&orig_context, __builtin_dwarf_sp_column ()))
12971297+ _Unwind_SetSpColumn (&orig_context, context->cfa, &tmp_sp);
12981298+ _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), NULL);
12991299+#endif
13001300+13011301+ /* Compute this frame's CFA. */
13021302+ switch (fs->regs.cfa_how)
13031303+ {
13041304+ case CFA_REG_OFFSET:
13051305+ cfa = _Unwind_GetPtr (&orig_context, fs->regs.cfa_reg);
13061306+ cfa += fs->regs.cfa_offset;
13071307+ break;
13081308+13091309+ case CFA_EXP:
13101310+ {
13111311+ const unsigned char *exp = fs->regs.cfa_exp;
13121312+ _uleb128_t len;
13131313+13141314+ exp = read_uleb128 (exp, &len);
13151315+ cfa = (void *) (_Unwind_Ptr)
13161316+ execute_stack_op (exp, exp + len, &orig_context, 0);
13171317+ break;
13181318+ }
13191319+13201320+ default:
13211321+ gcc_unreachable ();
13221322+ }
13231323+ context->cfa = cfa;
13241324+13251325+ /* Compute the addresses of all registers saved in this frame. */
13261326+ for (i = 0; i < DWARF_FRAME_REGISTERS + 1; ++i)
13271327+ switch (fs->regs.reg[i].how)
13281328+ {
13291329+ case REG_UNSAVED:
13301330+ case REG_UNDEFINED:
13311331+ break;
13321332+13331333+ case REG_SAVED_OFFSET:
13341334+ _Unwind_SetGRPtr (context, i,
13351335+ (void *) (cfa + fs->regs.reg[i].loc.offset));
13361336+ break;
13371337+13381338+ case REG_SAVED_REG:
13391339+ if (_Unwind_GRByValue (&orig_context, fs->regs.reg[i].loc.reg))
13401340+ _Unwind_SetGRValue (context, i,
13411341+ _Unwind_GetGR (&orig_context,
13421342+ fs->regs.reg[i].loc.reg));
13431343+ else
13441344+ _Unwind_SetGRPtr (context, i,
13451345+ _Unwind_GetGRPtr (&orig_context,
13461346+ fs->regs.reg[i].loc.reg));
13471347+ break;
13481348+13491349+ case REG_SAVED_EXP:
13501350+ {
13511351+ const unsigned char *exp = fs->regs.reg[i].loc.exp;
13521352+ _uleb128_t len;
13531353+ _Unwind_Ptr val;
13541354+13551355+ exp = read_uleb128 (exp, &len);
13561356+ val = execute_stack_op (exp, exp + len, &orig_context,
13571357+ (_Unwind_Ptr) cfa);
13581358+ _Unwind_SetGRPtr (context, i, (void *) val);
13591359+ }
13601360+ break;
13611361+13621362+ case REG_SAVED_VAL_OFFSET:
13631363+ _Unwind_SetGRValue (context, i,
13641364+ (_Unwind_Internal_Ptr)
13651365+ (cfa + fs->regs.reg[i].loc.offset));
13661366+ break;
13671367+13681368+ case REG_SAVED_VAL_EXP:
13691369+ {
13701370+ const unsigned char *exp = fs->regs.reg[i].loc.exp;
13711371+ _uleb128_t len;
13721372+ _Unwind_Ptr val;
13731373+13741374+ exp = read_uleb128 (exp, &len);
13751375+ val = execute_stack_op (exp, exp + len, &orig_context,
13761376+ (_Unwind_Ptr) cfa);
13771377+ _Unwind_SetGRValue (context, i, val);
13781378+ }
13791379+ break;
13801380+ }
13811381+13821382+ _Unwind_SetSignalFrame (context, fs->signal_frame);
13831383+13841384+#ifdef MD_FROB_UPDATE_CONTEXT
13851385+ MD_FROB_UPDATE_CONTEXT (context, fs);
13861386+#endif
13871387+}
13881388+13891389+/* CONTEXT describes the unwind state for a frame, and FS describes the FDE
13901390+ of its caller. Update CONTEXT to refer to the caller as well. Note
13911391+ that the args_size and lsda members are not updated here, but later in
13921392+ uw_frame_state_for. */
13931393+13941394+static void
13951395+uw_update_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
13961396+{
13971397+ uw_update_context_1 (context, fs);
13981398+13991399+ /* In general this unwinder doesn't make any distinction between
14001400+ undefined and same_value rule. Call-saved registers are assumed
14011401+ to have same_value rule by default and explicit undefined
14021402+ rule is handled like same_value. The only exception is
14031403+ DW_CFA_undefined on retaddr_column which is supposed to
14041404+ mark outermost frame in DWARF 3. */
14051405+ if (fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (fs->retaddr_column)].how
14061406+ == REG_UNDEFINED)
14071407+ /* uw_frame_state_for uses context->ra == 0 check to find outermost
14081408+ stack frame. */
14091409+ context->ra = 0;
14101410+ else
14111411+ /* Compute the return address now, since the return address column
14121412+ can change from frame to frame. */
14131413+ context->ra = __builtin_extract_return_addr
14141414+ (_Unwind_GetPtr (context, fs->retaddr_column));
14151415+}
14161416+14171417+static void
14181418+uw_advance_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
14191419+{
14201420+ uw_update_context (context, fs);
14211421+}
14221422+14231423+/* Fill in CONTEXT for top-of-stack. The only valid registers at this
14241424+ level will be the return address and the CFA. */
14251425+14261426+#define uw_init_context(CONTEXT) \
14271427+ do \
14281428+ { \
14291429+ /* Do any necessary initialization to access arbitrary stack frames. \
14301430+ On the SPARC, this means flushing the register windows. */ \
14311431+ __builtin_unwind_init (); \
14321432+ uw_init_context_1 (CONTEXT, __builtin_dwarf_cfa (), \
14331433+ __builtin_return_address (0)); \
14341434+ } \
14351435+ while (0)
14361436+14371437+static inline void
14381438+init_dwarf_reg_size_table (void)
14391439+{
14401440+ __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table);
14411441+}
14421442+14431443+static void __attribute__((noinline))
14441444+uw_init_context_1 (struct _Unwind_Context *context,
14451445+ void *outer_cfa, void *outer_ra)
14461446+{
14471447+ void *ra = __builtin_extract_return_addr (__builtin_return_address (0));
14481448+ _Unwind_FrameState fs;
14491449+ _Unwind_SpTmp sp_slot;
14501450+ _Unwind_Reason_Code code;
14511451+14521452+ memset (context, 0, sizeof (struct _Unwind_Context));
14531453+ context->ra = ra;
14541454+ context->flags = EXTENDED_CONTEXT_BIT;
14551455+14561456+ code = uw_frame_state_for (context, &fs);
14571457+ gcc_assert (code == _URC_NO_REASON);
14581458+14591459+#if __GTHREADS
14601460+ {
14611461+ static __gthread_once_t once_regsizes = __GTHREAD_ONCE_INIT;
14621462+ if (__gthread_once (&once_regsizes, init_dwarf_reg_size_table) != 0
14631463+ && dwarf_reg_size_table[0] == 0)
14641464+ init_dwarf_reg_size_table ();
14651465+ }
14661466+#else
14671467+ if (dwarf_reg_size_table[0] == 0)
14681468+ init_dwarf_reg_size_table ();
14691469+#endif
14701470+14711471+ /* Force the frame state to use the known cfa value. */
14721472+ _Unwind_SetSpColumn (context, outer_cfa, &sp_slot);
14731473+ fs.regs.cfa_how = CFA_REG_OFFSET;
14741474+ fs.regs.cfa_reg = __builtin_dwarf_sp_column ();
14751475+ fs.regs.cfa_offset = 0;
14761476+14771477+ uw_update_context_1 (context, &fs);
14781478+14791479+ /* If the return address column was saved in a register in the
14801480+ initialization context, then we can't see it in the given
14811481+ call frame data. So have the initialization context tell us. */
14821482+ context->ra = __builtin_extract_return_addr (outer_ra);
14831483+}
14841484+14851485+static void _Unwind_DebugHook (void *, void *)
14861486+ __attribute__ ((__noinline__, __used__, __noclone__));
14871487+14881488+/* This function is called during unwinding. It is intended as a hook
14891489+ for a debugger to intercept exceptions. CFA is the CFA of the
14901490+ target frame. HANDLER is the PC to which control will be
14911491+ transferred. */
14921492+static void
14931493+_Unwind_DebugHook (void *cfa __attribute__ ((__unused__)),
14941494+ void *handler __attribute__ ((__unused__)))
14951495+{
14961496+ asm ("");
14971497+}
14981498+14991499+/* Install TARGET into CURRENT so that we can return to it. This is a
15001500+ macro because __builtin_eh_return must be invoked in the context of
15011501+ our caller. */
15021502+15031503+#define uw_install_context(CURRENT, TARGET) \
15041504+ do \
15051505+ { \
15061506+ long offset = uw_install_context_1 ((CURRENT), (TARGET)); \
15071507+ void *handler = __builtin_frob_return_addr ((TARGET)->ra); \
15081508+ _Unwind_DebugHook ((TARGET)->cfa, handler); \
15091509+ __builtin_eh_return (offset, handler); \
15101510+ } \
15111511+ while (0)
15121512+15131513+static long
15141514+uw_install_context_1 (struct _Unwind_Context *current,
15151515+ struct _Unwind_Context *target)
15161516+{
15171517+ long i;
15181518+ _Unwind_SpTmp sp_slot;
15191519+15201520+ /* If the target frame does not have a saved stack pointer,
15211521+ then set up the target's CFA. */
15221522+ if (!_Unwind_GetGRPtr (target, __builtin_dwarf_sp_column ()))
15231523+ _Unwind_SetSpColumn (target, target->cfa, &sp_slot);
15241524+15251525+ for (i = 0; i < DWARF_FRAME_REGISTERS; ++i)
15261526+ {
15271527+ void *c = current->reg[i];
15281528+ void *t = target->reg[i];
15291529+15301530+ gcc_assert (current->by_value[i] == 0);
15311531+ if (target->by_value[i] && c)
15321532+ {
15331533+ _Unwind_Word w;
15341534+ _Unwind_Ptr p;
15351535+ if (dwarf_reg_size_table[i] == sizeof (_Unwind_Word))
15361536+ {
15371537+ w = (_Unwind_Internal_Ptr) t;
15381538+ memcpy (c, &w, sizeof (_Unwind_Word));
15391539+ }
15401540+ else
15411541+ {
15421542+ gcc_assert (dwarf_reg_size_table[i] == sizeof (_Unwind_Ptr));
15431543+ p = (_Unwind_Internal_Ptr) t;
15441544+ memcpy (c, &p, sizeof (_Unwind_Ptr));
15451545+ }
15461546+ }
15471547+ else if (t && c && t != c)
15481548+ memcpy (c, t, dwarf_reg_size_table[i]);
15491549+ }
15501550+15511551+ /* If the current frame doesn't have a saved stack pointer, then we
15521552+ need to rely on EH_RETURN_STACKADJ_RTX to get our target stack
15531553+ pointer value reloaded. */
15541554+ if (!_Unwind_GetGRPtr (current, __builtin_dwarf_sp_column ()))
15551555+ {
15561556+ void *target_cfa;
15571557+15581558+ target_cfa = _Unwind_GetPtr (target, __builtin_dwarf_sp_column ());
15591559+15601560+ /* We adjust SP by the difference between CURRENT and TARGET's CFA. */
15611561+ if (STACK_GROWS_DOWNWARD)
15621562+ return target_cfa - current->cfa + target->args_size;
15631563+ else
15641564+ return current->cfa - target_cfa - target->args_size;
15651565+ }
15661566+ return 0;
15671567+}
15681568+15691569+static inline _Unwind_Ptr
15701570+uw_identify_context (struct _Unwind_Context *context)
15711571+{
15721572+ /* The CFA is not sufficient to disambiguate the context of a function
15731573+ interrupted by a signal before establishing its frame and the context
15741574+ of the signal itself. */
15751575+ if (STACK_GROWS_DOWNWARD)
15761576+ return _Unwind_GetCFA (context) - _Unwind_IsSignalFrame (context);
15771577+ else
15781578+ return _Unwind_GetCFA (context) + _Unwind_IsSignalFrame (context);
15791579+}
15801580+15811581+15821582+#include "unwind.inc"
15831583+15841584+#if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
15851585+alias (_Unwind_Backtrace);
15861586+alias (_Unwind_DeleteException);
15871587+alias (_Unwind_FindEnclosingFunction);
15881588+alias (_Unwind_ForcedUnwind);
15891589+alias (_Unwind_GetDataRelBase);
15901590+alias (_Unwind_GetTextRelBase);
15911591+alias (_Unwind_GetCFA);
15921592+alias (_Unwind_GetGR);
15931593+alias (_Unwind_GetIP);
15941594+alias (_Unwind_GetLanguageSpecificData);
15951595+alias (_Unwind_GetRegionStart);
15961596+alias (_Unwind_RaiseException);
15971597+alias (_Unwind_Resume);
15981598+alias (_Unwind_Resume_or_Rethrow);
15991599+alias (_Unwind_SetGR);
16001600+alias (_Unwind_SetIP);
16011601+#endif
16021602+16031603+#endif /* !USING_SJLJ_EXCEPTIONS */
+87
src/libunwind-darwin/unwind-dw2.h
···11+/* DWARF2 frame unwind data structure.
22+ Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2009
33+ Free Software Foundation, Inc.
44+55+ This file is part of GCC.
66+77+ GCC is free software; you can redistribute it and/or modify it
88+ under the terms of the GNU General Public License as published by
99+ the Free Software Foundation; either version 3, or (at your option)
1010+ any later version.
1111+1212+ GCC is distributed in the hope that it will be useful, but WITHOUT
1313+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1414+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
1515+ License for more details.
1616+1717+ Under Section 7 of GPL version 3, you are granted additional
1818+ permissions described in the GCC Runtime Library Exception, version
1919+ 3.1, as published by the Free Software Foundation.
2020+2121+ You should have received a copy of the GNU General Public License and
2222+ a copy of the GCC Runtime Library Exception along with this program;
2323+ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
2424+ <http://www.gnu.org/licenses/>. */
2525+2626+/* A target can override (perhaps for backward compatibility) how
2727+ many dwarf2 columns are unwound. */
2828+#ifndef DWARF_FRAME_REGISTERS
2929+#define DWARF_FRAME_REGISTERS FIRST_PSEUDO_REGISTER
3030+#endif
3131+3232+/* The result of interpreting the frame unwind info for a frame.
3333+ This is all symbolic at this point, as none of the values can
3434+ be resolved until the target pc is located. */
3535+typedef struct
3636+{
3737+ /* Each register save state can be described in terms of a CFA slot,
3838+ another register, or a location expression. */
3939+ struct frame_state_reg_info
4040+ {
4141+ struct {
4242+ union {
4343+ _Unwind_Word reg;
4444+ _Unwind_Sword offset;
4545+ const unsigned char *exp;
4646+ } loc;
4747+ enum {
4848+ REG_UNSAVED,
4949+ REG_SAVED_OFFSET,
5050+ REG_SAVED_REG,
5151+ REG_SAVED_EXP,
5252+ REG_SAVED_VAL_OFFSET,
5353+ REG_SAVED_VAL_EXP,
5454+ REG_UNDEFINED
5555+ } how;
5656+ } reg[DWARF_FRAME_REGISTERS+1];
5757+5858+ /* Used to implement DW_CFA_remember_state. */
5959+ struct frame_state_reg_info *prev;
6060+6161+ /* The CFA can be described in terms of a reg+offset or a
6262+ location expression. */
6363+ _Unwind_Sword cfa_offset;
6464+ _Unwind_Word cfa_reg;
6565+ const unsigned char *cfa_exp;
6666+ enum {
6767+ CFA_UNSET,
6868+ CFA_REG_OFFSET,
6969+ CFA_EXP
7070+ } cfa_how;
7171+ } regs;
7272+7373+ /* The PC described by the current frame state. */
7474+ void *pc;
7575+7676+ /* The information we care about from the CIE/FDE. */
7777+ _Unwind_Personality_Fn personality;
7878+ _Unwind_Sword data_align;
7979+ _Unwind_Word code_align;
8080+ _Unwind_Word retaddr_column;
8181+ unsigned char fde_encoding;
8282+ unsigned char lsda_encoding;
8383+ unsigned char saw_z;
8484+ unsigned char signal_frame;
8585+ void *eh_ptr;
8686+} _Unwind_FrameState;
8787+
+276
src/libunwind-darwin/unwind-generic.h
···11+/* Exception handling and frame unwind runtime interface routines.
22+ Copyright (C) 2001, 2003, 2004, 2006, 2008, 2009 Free Software Foundation, Inc.
33+44+ This file is part of GCC.
55+66+ GCC is free software; you can redistribute it and/or modify it
77+ under the terms of the GNU General Public License as published by
88+ the Free Software Foundation; either version 3, or (at your option)
99+ any later version.
1010+1111+ GCC is distributed in the hope that it will be useful, but WITHOUT
1212+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1313+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
1414+ License for more details.
1515+1616+ Under Section 7 of GPL version 3, you are granted additional
1717+ permissions described in the GCC Runtime Library Exception, version
1818+ 3.1, as published by the Free Software Foundation.
1919+2020+ You should have received a copy of the GNU General Public License and
2121+ a copy of the GCC Runtime Library Exception along with this program;
2222+ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
2323+ <http://www.gnu.org/licenses/>. */
2424+2525+/* This is derived from the C++ ABI for IA-64. Where we diverge
2626+ for cross-architecture compatibility are noted with "@@@". */
2727+2828+#ifndef _UNWIND_H
2929+#define _UNWIND_H
3030+3131+#ifndef HIDE_EXPORTS
3232+#pragma GCC visibility push(default)
3333+#endif
3434+3535+#ifdef __cplusplus
3636+extern "C" {
3737+#endif
3838+3939+/* Level 1: Base ABI */
4040+4141+/* @@@ The IA-64 ABI uses uint64 throughout. Most places this is
4242+ inefficient for 32-bit and smaller machines. */
4343+typedef unsigned _Unwind_Word __attribute__((__mode__(__unwind_word__)));
4444+typedef signed _Unwind_Sword __attribute__((__mode__(__unwind_word__)));
4545+#if defined(__ia64__) && defined(__hpux__)
4646+typedef unsigned _Unwind_Ptr __attribute__((__mode__(__word__)));
4747+#else
4848+typedef unsigned _Unwind_Ptr __attribute__((__mode__(__pointer__)));
4949+#endif
5050+typedef unsigned _Unwind_Internal_Ptr __attribute__((__mode__(__pointer__)));
5151+5252+/* @@@ The IA-64 ABI uses a 64-bit word to identify the producer and
5353+ consumer of an exception. We'll go along with this for now even on
5454+ 32-bit machines. We'll need to provide some other option for
5555+ 16-bit machines and for machines with > 8 bits per byte. */
5656+typedef unsigned _Unwind_Exception_Class __attribute__((__mode__(__DI__)));
5757+5858+/* The unwind interface uses reason codes in several contexts to
5959+ identify the reasons for failures or other actions. */
6060+typedef enum
6161+{
6262+ _URC_NO_REASON = 0,
6363+ _URC_FOREIGN_EXCEPTION_CAUGHT = 1,
6464+ _URC_FATAL_PHASE2_ERROR = 2,
6565+ _URC_FATAL_PHASE1_ERROR = 3,
6666+ _URC_NORMAL_STOP = 4,
6767+ _URC_END_OF_STACK = 5,
6868+ _URC_HANDLER_FOUND = 6,
6969+ _URC_INSTALL_CONTEXT = 7,
7070+ _URC_CONTINUE_UNWIND = 8
7171+} _Unwind_Reason_Code;
7272+7373+7474+/* The unwind interface uses a pointer to an exception header object
7575+ as its representation of an exception being thrown. In general, the
7676+ full representation of an exception object is language- and
7777+ implementation-specific, but it will be prefixed by a header
7878+ understood by the unwind interface. */
7979+8080+struct _Unwind_Exception;
8181+8282+typedef void (*_Unwind_Exception_Cleanup_Fn) (_Unwind_Reason_Code,
8383+ struct _Unwind_Exception *);
8484+8585+struct _Unwind_Exception
8686+{
8787+ _Unwind_Exception_Class exception_class;
8888+ _Unwind_Exception_Cleanup_Fn exception_cleanup;
8989+ _Unwind_Word private_1;
9090+ _Unwind_Word private_2;
9191+9292+ /* @@@ The IA-64 ABI says that this structure must be double-word aligned.
9393+ Taking that literally does not make much sense generically. Instead we
9494+ provide the maximum alignment required by any type for the machine. */
9595+} __attribute__((__aligned__));
9696+9797+9898+/* The ACTIONS argument to the personality routine is a bitwise OR of one
9999+ or more of the following constants. */
100100+typedef int _Unwind_Action;
101101+102102+#define _UA_SEARCH_PHASE 1
103103+#define _UA_CLEANUP_PHASE 2
104104+#define _UA_HANDLER_FRAME 4
105105+#define _UA_FORCE_UNWIND 8
106106+#define _UA_END_OF_STACK 16
107107+108108+/* The target can override this macro to define any back-end-specific
109109+ attributes required for the lowest-level stack frame. */
110110+#ifndef LIBGCC2_UNWIND_ATTRIBUTE
111111+#define LIBGCC2_UNWIND_ATTRIBUTE
112112+#endif
113113+114114+/* This is an opaque type used to refer to a system-specific data
115115+ structure used by the system unwinder. This context is created and
116116+ destroyed by the system, and passed to the personality routine
117117+ during unwinding. */
118118+struct _Unwind_Context;
119119+120120+/* Raise an exception, passing along the given exception object. */
121121+extern _Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
122122+_Unwind_RaiseException (struct _Unwind_Exception *);
123123+124124+/* Raise an exception for forced unwinding. */
125125+126126+typedef _Unwind_Reason_Code (*_Unwind_Stop_Fn)
127127+ (int, _Unwind_Action, _Unwind_Exception_Class,
128128+ struct _Unwind_Exception *, struct _Unwind_Context *, void *);
129129+130130+extern _Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
131131+_Unwind_ForcedUnwind (struct _Unwind_Exception *, _Unwind_Stop_Fn, void *);
132132+133133+/* Helper to invoke the exception_cleanup routine. */
134134+extern void _Unwind_DeleteException (struct _Unwind_Exception *);
135135+136136+/* Resume propagation of an existing exception. This is used after
137137+ e.g. executing cleanup code, and not to implement rethrowing. */
138138+extern void LIBGCC2_UNWIND_ATTRIBUTE
139139+_Unwind_Resume (struct _Unwind_Exception *);
140140+141141+/* @@@ Resume propagation of a FORCE_UNWIND exception, or to rethrow
142142+ a normal exception that was handled. */
143143+extern _Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
144144+_Unwind_Resume_or_Rethrow (struct _Unwind_Exception *);
145145+146146+/* @@@ Use unwind data to perform a stack backtrace. The trace callback
147147+ is called for every stack frame in the call chain, but no cleanup
148148+ actions are performed. */
149149+typedef _Unwind_Reason_Code (*_Unwind_Trace_Fn)
150150+ (struct _Unwind_Context *, void *);
151151+152152+extern _Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
153153+_Unwind_Backtrace (_Unwind_Trace_Fn, void *);
154154+155155+/* These functions are used for communicating information about the unwind
156156+ context (i.e. the unwind descriptors and the user register state) between
157157+ the unwind library and the personality routine and landing pad. Only
158158+ selected registers may be manipulated. */
159159+160160+extern _Unwind_Word _Unwind_GetGR (struct _Unwind_Context *, int);
161161+extern void _Unwind_SetGR (struct _Unwind_Context *, int, _Unwind_Word);
162162+163163+extern _Unwind_Ptr _Unwind_GetIP (struct _Unwind_Context *);
164164+extern _Unwind_Ptr _Unwind_GetIPInfo (struct _Unwind_Context *, int *);
165165+extern void _Unwind_SetIP (struct _Unwind_Context *, _Unwind_Ptr);
166166+167167+/* @@@ Retrieve the CFA of the given context. */
168168+extern _Unwind_Word _Unwind_GetCFA (struct _Unwind_Context *);
169169+170170+extern void *_Unwind_GetLanguageSpecificData (struct _Unwind_Context *);
171171+172172+extern _Unwind_Ptr _Unwind_GetRegionStart (struct _Unwind_Context *);
173173+174174+175175+/* The personality routine is the function in the C++ (or other language)
176176+ runtime library which serves as an interface between the system unwind
177177+ library and language-specific exception handling semantics. It is
178178+ specific to the code fragment described by an unwind info block, and
179179+ it is always referenced via the pointer in the unwind info block, and
180180+ hence it has no ABI-specified name.
181181+182182+ Note that this implies that two different C++ implementations can
183183+ use different names, and have different contents in the language
184184+ specific data area. Moreover, that the language specific data
185185+ area contains no version info because name of the function invoked
186186+ provides more effective versioning by detecting at link time the
187187+ lack of code to handle the different data format. */
188188+189189+typedef _Unwind_Reason_Code (*_Unwind_Personality_Fn)
190190+ (int, _Unwind_Action, _Unwind_Exception_Class,
191191+ struct _Unwind_Exception *, struct _Unwind_Context *);
192192+193193+/* @@@ The following alternate entry points are for setjmp/longjmp
194194+ based unwinding. */
195195+196196+struct SjLj_Function_Context;
197197+extern void _Unwind_SjLj_Register (struct SjLj_Function_Context *);
198198+extern void _Unwind_SjLj_Unregister (struct SjLj_Function_Context *);
199199+200200+extern _Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
201201+_Unwind_SjLj_RaiseException (struct _Unwind_Exception *);
202202+extern _Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
203203+_Unwind_SjLj_ForcedUnwind (struct _Unwind_Exception *, _Unwind_Stop_Fn, void *);
204204+extern void LIBGCC2_UNWIND_ATTRIBUTE
205205+_Unwind_SjLj_Resume (struct _Unwind_Exception *);
206206+extern _Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
207207+_Unwind_SjLj_Resume_or_Rethrow (struct _Unwind_Exception *);
208208+209209+/* @@@ The following provide access to the base addresses for text
210210+ and data-relative addressing in the LDSA. In order to stay link
211211+ compatible with the standard ABI for IA-64, we inline these. */
212212+213213+#ifdef __ia64__
214214+#include <stdlib.h>
215215+216216+static inline _Unwind_Ptr
217217+_Unwind_GetDataRelBase (struct _Unwind_Context *_C)
218218+{
219219+ /* The GP is stored in R1. */
220220+ return _Unwind_GetGR (_C, 1);
221221+}
222222+223223+static inline _Unwind_Ptr
224224+_Unwind_GetTextRelBase (struct _Unwind_Context *_C __attribute__ ((__unused__)))
225225+{
226226+ abort ();
227227+ return 0;
228228+}
229229+230230+/* @@@ Retrieve the Backing Store Pointer of the given context. */
231231+extern _Unwind_Word _Unwind_GetBSP (struct _Unwind_Context *);
232232+#else
233233+extern _Unwind_Ptr _Unwind_GetDataRelBase (struct _Unwind_Context *);
234234+extern _Unwind_Ptr _Unwind_GetTextRelBase (struct _Unwind_Context *);
235235+#endif
236236+237237+/* @@@ Given an address, return the entry point of the function that
238238+ contains it. */
239239+extern void * _Unwind_FindEnclosingFunction (void *pc);
240240+241241+#ifndef __SIZEOF_LONG__
242242+ #error "__SIZEOF_LONG__ macro not defined"
243243+#endif
244244+245245+#ifndef __SIZEOF_POINTER__
246246+ #error "__SIZEOF_POINTER__ macro not defined"
247247+#endif
248248+249249+250250+/* leb128 type numbers have a potentially unlimited size.
251251+ The target of the following definitions of _sleb128_t and _uleb128_t
252252+ is to have efficient data types large enough to hold the leb128 type
253253+ numbers used in the unwind code.
254254+ Mostly these types will simply be defined to long and unsigned long
255255+ except when a unsigned long data type on the target machine is not
256256+ capable of storing a pointer. */
257257+258258+#if __SIZEOF_LONG__ >= __SIZEOF_POINTER__
259259+ typedef long _sleb128_t;
260260+ typedef unsigned long _uleb128_t;
261261+#elif __SIZEOF_LONG_LONG__ >= __SIZEOF_POINTER__
262262+ typedef long long _sleb128_t;
263263+ typedef unsigned long long _uleb128_t;
264264+#else
265265+# error "What type shall we use for _sleb128_t?"
266266+#endif
267267+268268+#ifdef __cplusplus
269269+}
270270+#endif
271271+272272+#ifndef HIDE_EXPORTS
273273+#pragma GCC visibility pop
274274+#endif
275275+276276+#endif /* unwind.h */
+289
src/libunwind-darwin/unwind-pe.h
···11+/* Exception handling and frame unwind runtime interface routines.
22+ Copyright (C) 2001, 2002, 2003, 2004, 2008, 2009 Free Software Foundation, Inc.
33+44+ This file is part of GCC.
55+66+ GCC is free software; you can redistribute it and/or modify it
77+ under the terms of the GNU General Public License as published by
88+ the Free Software Foundation; either version 3, or (at your option)
99+ any later version.
1010+1111+ GCC is distributed in the hope that it will be useful, but WITHOUT
1212+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1313+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
1414+ License for more details.
1515+1616+ Under Section 7 of GPL version 3, you are granted additional
1717+ permissions described in the GCC Runtime Library Exception, version
1818+ 3.1, as published by the Free Software Foundation.
1919+2020+ You should have received a copy of the GNU General Public License and
2121+ a copy of the GCC Runtime Library Exception along with this program;
2222+ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
2323+ <http://www.gnu.org/licenses/>. */
2424+2525+/* @@@ Really this should be out of line, but this also causes link
2626+ compatibility problems with the base ABI. This is slightly better
2727+ than duplicating code, however. */
2828+2929+#ifndef GCC_UNWIND_PE_H
3030+#define GCC_UNWIND_PE_H
3131+3232+/* If using C++, references to abort have to be qualified with std::. */
3333+#if __cplusplus
3434+#define __gxx_abort std::abort
3535+#else
3636+#define __gxx_abort abort
3737+#endif
3838+3939+/* Pointer encodings, from dwarf2.h. */
4040+#define DW_EH_PE_absptr 0x00
4141+#define DW_EH_PE_omit 0xff
4242+4343+#define DW_EH_PE_uleb128 0x01
4444+#define DW_EH_PE_udata2 0x02
4545+#define DW_EH_PE_udata4 0x03
4646+#define DW_EH_PE_udata8 0x04
4747+#define DW_EH_PE_sleb128 0x09
4848+#define DW_EH_PE_sdata2 0x0A
4949+#define DW_EH_PE_sdata4 0x0B
5050+#define DW_EH_PE_sdata8 0x0C
5151+#define DW_EH_PE_signed 0x08
5252+5353+#define DW_EH_PE_pcrel 0x10
5454+#define DW_EH_PE_textrel 0x20
5555+#define DW_EH_PE_datarel 0x30
5656+#define DW_EH_PE_funcrel 0x40
5757+#define DW_EH_PE_aligned 0x50
5858+5959+#define DW_EH_PE_indirect 0x80
6060+6161+6262+#ifndef NO_SIZE_OF_ENCODED_VALUE
6363+6464+/* Given an encoding, return the number of bytes the format occupies.
6565+ This is only defined for fixed-size encodings, and so does not
6666+ include leb128. */
6767+6868+static unsigned int
6969+size_of_encoded_value (unsigned char encoding) __attribute__ ((unused));
7070+7171+static unsigned int
7272+size_of_encoded_value (unsigned char encoding)
7373+{
7474+ if (encoding == DW_EH_PE_omit)
7575+ return 0;
7676+7777+ switch (encoding & 0x07)
7878+ {
7979+ case DW_EH_PE_absptr:
8080+ return sizeof (void *);
8181+ case DW_EH_PE_udata2:
8282+ return 2;
8383+ case DW_EH_PE_udata4:
8484+ return 4;
8585+ case DW_EH_PE_udata8:
8686+ return 8;
8787+ }
8888+ __gxx_abort ();
8989+}
9090+9191+#endif
9292+9393+#ifndef NO_BASE_OF_ENCODED_VALUE
9494+9595+/* Given an encoding and an _Unwind_Context, return the base to which
9696+ the encoding is relative. This base may then be passed to
9797+ read_encoded_value_with_base for use when the _Unwind_Context is
9898+ not available. */
9999+100100+static _Unwind_Ptr
101101+base_of_encoded_value (unsigned char encoding, struct _Unwind_Context *context)
102102+{
103103+ if (encoding == DW_EH_PE_omit)
104104+ return 0;
105105+106106+ switch (encoding & 0x70)
107107+ {
108108+ case DW_EH_PE_absptr:
109109+ case DW_EH_PE_pcrel:
110110+ case DW_EH_PE_aligned:
111111+ return 0;
112112+113113+ case DW_EH_PE_textrel:
114114+ return _Unwind_GetTextRelBase (context);
115115+ case DW_EH_PE_datarel:
116116+ return _Unwind_GetDataRelBase (context);
117117+ case DW_EH_PE_funcrel:
118118+ return _Unwind_GetRegionStart (context);
119119+ }
120120+ __gxx_abort ();
121121+}
122122+123123+#endif
124124+125125+/* Read an unsigned leb128 value from P, store the value in VAL, return
126126+ P incremented past the value. We assume that a word is large enough to
127127+ hold any value so encoded; if it is smaller than a pointer on some target,
128128+ pointers should not be leb128 encoded on that target. */
129129+130130+static const unsigned char *
131131+read_uleb128 (const unsigned char *p, _uleb128_t *val)
132132+{
133133+ unsigned int shift = 0;
134134+ unsigned char byte;
135135+ _uleb128_t result;
136136+137137+ result = 0;
138138+ do
139139+ {
140140+ byte = *p++;
141141+ result |= ((_uleb128_t)byte & 0x7f) << shift;
142142+ shift += 7;
143143+ }
144144+ while (byte & 0x80);
145145+146146+ *val = result;
147147+ return p;
148148+}
149149+150150+/* Similar, but read a signed leb128 value. */
151151+152152+static const unsigned char *
153153+read_sleb128 (const unsigned char *p, _sleb128_t *val)
154154+{
155155+ unsigned int shift = 0;
156156+ unsigned char byte;
157157+ _uleb128_t result;
158158+159159+ result = 0;
160160+ do
161161+ {
162162+ byte = *p++;
163163+ result |= ((_uleb128_t)byte & 0x7f) << shift;
164164+ shift += 7;
165165+ }
166166+ while (byte & 0x80);
167167+168168+ /* Sign-extend a negative value. */
169169+ if (shift < 8 * sizeof(result) && (byte & 0x40) != 0)
170170+ result |= -(((_uleb128_t)1L) << shift);
171171+172172+ *val = (_sleb128_t) result;
173173+ return p;
174174+}
175175+176176+/* Load an encoded value from memory at P. The value is returned in VAL;
177177+ The function returns P incremented past the value. BASE is as given
178178+ by base_of_encoded_value for this encoding in the appropriate context. */
179179+180180+static const unsigned char *
181181+read_encoded_value_with_base (unsigned char encoding, _Unwind_Ptr base,
182182+ const unsigned char *p, _Unwind_Ptr *val)
183183+{
184184+ union unaligned
185185+ {
186186+ void *ptr;
187187+ unsigned u2 __attribute__ ((mode (HI)));
188188+ unsigned u4 __attribute__ ((mode (SI)));
189189+ unsigned u8 __attribute__ ((mode (DI)));
190190+ signed s2 __attribute__ ((mode (HI)));
191191+ signed s4 __attribute__ ((mode (SI)));
192192+ signed s8 __attribute__ ((mode (DI)));
193193+ } __attribute__((__packed__));
194194+195195+ const union unaligned *u = (const union unaligned *) p;
196196+ _Unwind_Internal_Ptr result;
197197+198198+ if (encoding == DW_EH_PE_aligned)
199199+ {
200200+ _Unwind_Internal_Ptr a = (_Unwind_Internal_Ptr) p;
201201+ a = (a + sizeof (void *) - 1) & - sizeof(void *);
202202+ result = *(_Unwind_Internal_Ptr *) a;
203203+ p = (const unsigned char *) (_Unwind_Internal_Ptr) (a + sizeof (void *));
204204+ }
205205+ else
206206+ {
207207+ switch (encoding & 0x0f)
208208+ {
209209+ case DW_EH_PE_absptr:
210210+ result = (_Unwind_Internal_Ptr) u->ptr;
211211+ p += sizeof (void *);
212212+ break;
213213+214214+ case DW_EH_PE_uleb128:
215215+ {
216216+ _uleb128_t tmp;
217217+ p = read_uleb128 (p, &tmp);
218218+ result = (_Unwind_Internal_Ptr) tmp;
219219+ }
220220+ break;
221221+222222+ case DW_EH_PE_sleb128:
223223+ {
224224+ _sleb128_t tmp;
225225+ p = read_sleb128 (p, &tmp);
226226+ result = (_Unwind_Internal_Ptr) tmp;
227227+ }
228228+ break;
229229+230230+ case DW_EH_PE_udata2:
231231+ result = u->u2;
232232+ p += 2;
233233+ break;
234234+ case DW_EH_PE_udata4:
235235+ result = u->u4;
236236+ p += 4;
237237+ break;
238238+ case DW_EH_PE_udata8:
239239+ result = u->u8;
240240+ p += 8;
241241+ break;
242242+243243+ case DW_EH_PE_sdata2:
244244+ result = u->s2;
245245+ p += 2;
246246+ break;
247247+ case DW_EH_PE_sdata4:
248248+ result = u->s4;
249249+ p += 4;
250250+ break;
251251+ case DW_EH_PE_sdata8:
252252+ result = u->s8;
253253+ p += 8;
254254+ break;
255255+256256+ default:
257257+ __gxx_abort ();
258258+ }
259259+260260+ if (result != 0)
261261+ {
262262+ result += ((encoding & 0x70) == DW_EH_PE_pcrel
263263+ ? (_Unwind_Internal_Ptr) u : base);
264264+ if (encoding & DW_EH_PE_indirect)
265265+ result = *(_Unwind_Internal_Ptr *) result;
266266+ }
267267+ }
268268+269269+ *val = result;
270270+ return p;
271271+}
272272+273273+#ifndef NO_BASE_OF_ENCODED_VALUE
274274+275275+/* Like read_encoded_value_with_base, but get the base from the context
276276+ rather than providing it directly. */
277277+278278+static inline const unsigned char *
279279+read_encoded_value (struct _Unwind_Context *context, unsigned char encoding,
280280+ const unsigned char *p, _Unwind_Ptr *val)
281281+{
282282+ return read_encoded_value_with_base (encoding,
283283+ base_of_encoded_value (encoding, context),
284284+ p, val);
285285+}
286286+287287+#endif
288288+289289+#endif /* unwind-pe.h */
+326
src/libunwind-darwin/unwind-sjlj.c
···11+/* SJLJ exception handling and frame unwind runtime interface routines.
22+ Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006,
33+ 2009 Free Software Foundation, Inc.
44+55+ This file is part of GCC.
66+77+ GCC is free software; you can redistribute it and/or modify it
88+ under the terms of the GNU General Public License as published by
99+ the Free Software Foundation; either version 3, or (at your option)
1010+ any later version.
1111+1212+ GCC is distributed in the hope that it will be useful, but WITHOUT
1313+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1414+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
1515+ License for more details.
1616+1717+ Under Section 7 of GPL version 3, you are granted additional
1818+ permissions described in the GCC Runtime Library Exception, version
1919+ 3.1, as published by the Free Software Foundation.
2020+2121+ You should have received a copy of the GNU General Public License and
2222+ a copy of the GCC Runtime Library Exception along with this program;
2323+ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
2424+ <http://www.gnu.org/licenses/>. */
2525+2626+#include "tconfig.h"
2727+#include "tsystem.h"
2828+#include "coretypes.h"
2929+#include "tm.h"
3030+#include "unwind.h"
3131+#include "gthr.h"
3232+3333+#ifdef __USING_SJLJ_EXCEPTIONS__
3434+3535+#ifdef DONT_USE_BUILTIN_SETJMP
3636+#ifndef inhibit_libc
3737+#include <setjmp.h>
3838+#else
3939+typedef void *jmp_buf[JMP_BUF_SIZE];
4040+extern void longjmp(jmp_buf, int) __attribute__((noreturn));
4141+#endif
4242+#else
4343+#define longjmp __builtin_longjmp
4444+#endif
4545+4646+/* The setjmp side is dealt with in the except.c file. */
4747+#undef setjmp
4848+#define setjmp setjmp_should_not_be_used_in_this_file
4949+5050+5151+/* This structure is allocated on the stack of the target function.
5252+ This must match the definition created in except.c:init_eh. */
5353+struct SjLj_Function_Context
5454+{
5555+ /* This is the chain through all registered contexts. It is
5656+ filled in by _Unwind_SjLj_Register. */
5757+ struct SjLj_Function_Context *prev;
5858+5959+ /* This is assigned in by the target function before every call
6060+ to the index of the call site in the lsda. It is assigned by
6161+ the personality routine to the landing pad index. */
6262+ int call_site;
6363+6464+ /* This is how data is returned from the personality routine to
6565+ the target function's handler. */
6666+ _Unwind_Word data[4];
6767+6868+ /* These are filled in once by the target function before any
6969+ exceptions are expected to be handled. */
7070+ _Unwind_Personality_Fn personality;
7171+ void *lsda;
7272+7373+#ifdef DONT_USE_BUILTIN_SETJMP
7474+ /* We don't know what sort of alignment requirements the system
7575+ jmp_buf has. We over estimated in except.c, and now we have
7676+ to match that here just in case the system *didn't* have more
7777+ restrictive requirements. */
7878+ jmp_buf jbuf __attribute__((aligned));
7979+#else
8080+ void *jbuf[];
8181+#endif
8282+};
8383+8484+struct _Unwind_Context
8585+{
8686+ struct SjLj_Function_Context *fc;
8787+};
8888+8989+typedef struct
9090+{
9191+ _Unwind_Personality_Fn personality;
9292+} _Unwind_FrameState;
9393+9494+9595+/* Manage the chain of registered function contexts. */
9696+9797+/* Single threaded fallback chain. */
9898+static struct SjLj_Function_Context *fc_static;
9999+100100+#if __GTHREADS
101101+static __gthread_key_t fc_key;
102102+static int use_fc_key = -1;
103103+104104+static void
105105+fc_key_init (void)
106106+{
107107+ use_fc_key = __gthread_key_create (&fc_key, 0) == 0;
108108+}
109109+110110+static void
111111+fc_key_init_once (void)
112112+{
113113+ static __gthread_once_t once = __GTHREAD_ONCE_INIT;
114114+ if (__gthread_once (&once, fc_key_init) != 0 || use_fc_key < 0)
115115+ use_fc_key = 0;
116116+}
117117+#endif
118118+119119+void
120120+_Unwind_SjLj_Register (struct SjLj_Function_Context *fc)
121121+{
122122+#if __GTHREADS
123123+ if (use_fc_key < 0)
124124+ fc_key_init_once ();
125125+126126+ if (use_fc_key)
127127+ {
128128+ fc->prev = __gthread_getspecific (fc_key);
129129+ __gthread_setspecific (fc_key, fc);
130130+ }
131131+ else
132132+#endif
133133+ {
134134+ fc->prev = fc_static;
135135+ fc_static = fc;
136136+ }
137137+}
138138+139139+static inline struct SjLj_Function_Context *
140140+_Unwind_SjLj_GetContext (void)
141141+{
142142+#if __GTHREADS
143143+ if (use_fc_key < 0)
144144+ fc_key_init_once ();
145145+146146+ if (use_fc_key)
147147+ return __gthread_getspecific (fc_key);
148148+#endif
149149+ return fc_static;
150150+}
151151+152152+static inline void
153153+_Unwind_SjLj_SetContext (struct SjLj_Function_Context *fc)
154154+{
155155+#if __GTHREADS
156156+ if (use_fc_key < 0)
157157+ fc_key_init_once ();
158158+159159+ if (use_fc_key)
160160+ __gthread_setspecific (fc_key, fc);
161161+ else
162162+#endif
163163+ fc_static = fc;
164164+}
165165+166166+void
167167+_Unwind_SjLj_Unregister (struct SjLj_Function_Context *fc)
168168+{
169169+ _Unwind_SjLj_SetContext (fc->prev);
170170+}
171171+172172+173173+/* Get/set the return data value at INDEX in CONTEXT. */
174174+175175+_Unwind_Word
176176+_Unwind_GetGR (struct _Unwind_Context *context, int index)
177177+{
178178+ return context->fc->data[index];
179179+}
180180+181181+/* Get the value of the CFA as saved in CONTEXT. */
182182+183183+_Unwind_Word
184184+_Unwind_GetCFA (struct _Unwind_Context *context __attribute__((unused)))
185185+{
186186+ /* ??? Ideally __builtin_setjmp places the CFA in the jmpbuf. */
187187+188188+#ifndef DONT_USE_BUILTIN_SETJMP
189189+ /* This is a crude imitation of the CFA: the saved stack pointer.
190190+ This is roughly the CFA of the frame before CONTEXT. When using the
191191+ DWARF-2 unwinder _Unwind_GetCFA returns the CFA of the frame described
192192+ by CONTEXT instead; but for DWARF-2 the cleanups associated with
193193+ CONTEXT have already been run, and for SJLJ they have not yet been. */
194194+ if (context->fc != NULL)
195195+ return (_Unwind_Word) context->fc->jbuf[2];
196196+#endif
197197+198198+ /* Otherwise we're out of luck for now. */
199199+ return (_Unwind_Word) 0;
200200+}
201201+202202+void
203203+_Unwind_SetGR (struct _Unwind_Context *context, int index, _Unwind_Word val)
204204+{
205205+ context->fc->data[index] = val;
206206+}
207207+208208+/* Get the call-site index as saved in CONTEXT. */
209209+210210+_Unwind_Ptr
211211+_Unwind_GetIP (struct _Unwind_Context *context)
212212+{
213213+ return context->fc->call_site + 1;
214214+}
215215+216216+_Unwind_Ptr
217217+_Unwind_GetIPInfo (struct _Unwind_Context *context, int *ip_before_insn)
218218+{
219219+ *ip_before_insn = 0;
220220+ if (context->fc != NULL)
221221+ return context->fc->call_site + 1;
222222+ else
223223+ return 0;
224224+}
225225+226226+/* Set the return landing pad index in CONTEXT. */
227227+228228+void
229229+_Unwind_SetIP (struct _Unwind_Context *context, _Unwind_Ptr val)
230230+{
231231+ context->fc->call_site = val - 1;
232232+}
233233+234234+void *
235235+_Unwind_GetLanguageSpecificData (struct _Unwind_Context *context)
236236+{
237237+ return context->fc->lsda;
238238+}
239239+240240+_Unwind_Ptr
241241+_Unwind_GetRegionStart (struct _Unwind_Context *context __attribute__((unused)) )
242242+{
243243+ return 0;
244244+}
245245+246246+void *
247247+_Unwind_FindEnclosingFunction (void *pc __attribute__((unused)))
248248+{
249249+ return NULL;
250250+}
251251+252252+#ifndef __ia64__
253253+_Unwind_Ptr
254254+_Unwind_GetDataRelBase (struct _Unwind_Context *context __attribute__((unused)) )
255255+{
256256+ return 0;
257257+}
258258+259259+_Unwind_Ptr
260260+_Unwind_GetTextRelBase (struct _Unwind_Context *context __attribute__((unused)) )
261261+{
262262+ return 0;
263263+}
264264+#endif
265265+266266+static inline _Unwind_Reason_Code
267267+uw_frame_state_for (struct _Unwind_Context *context, _Unwind_FrameState *fs)
268268+{
269269+ if (context->fc == NULL)
270270+ {
271271+ fs->personality = NULL;
272272+ return _URC_END_OF_STACK;
273273+ }
274274+ else
275275+ {
276276+ fs->personality = context->fc->personality;
277277+ return _URC_NO_REASON;
278278+ }
279279+}
280280+281281+static inline void
282282+uw_update_context (struct _Unwind_Context *context,
283283+ _Unwind_FrameState *fs __attribute__((unused)) )
284284+{
285285+ context->fc = context->fc->prev;
286286+}
287287+288288+static void
289289+uw_advance_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
290290+{
291291+ _Unwind_SjLj_Unregister (context->fc);
292292+ uw_update_context (context, fs);
293293+}
294294+295295+static inline void
296296+uw_init_context (struct _Unwind_Context *context)
297297+{
298298+ context->fc = _Unwind_SjLj_GetContext ();
299299+}
300300+301301+static void __attribute__((noreturn))
302302+uw_install_context (struct _Unwind_Context *current __attribute__((unused)),
303303+ struct _Unwind_Context *target)
304304+{
305305+ _Unwind_SjLj_SetContext (target->fc);
306306+ longjmp (target->fc->jbuf, 1);
307307+}
308308+309309+static inline _Unwind_Ptr
310310+uw_identify_context (struct _Unwind_Context *context)
311311+{
312312+ return (_Unwind_Ptr) context->fc;
313313+}
314314+315315+316316+/* Play games with unwind symbols so that we can have call frame
317317+ and sjlj symbols in the same shared library. Not that you can
318318+ use them simultaneously... */
319319+#define _Unwind_RaiseException _Unwind_SjLj_RaiseException
320320+#define _Unwind_ForcedUnwind _Unwind_SjLj_ForcedUnwind
321321+#define _Unwind_Resume _Unwind_SjLj_Resume
322322+#define _Unwind_Resume_or_Rethrow _Unwind_SjLj_Resume_or_Rethrow
323323+324324+#include "unwind.inc"
325325+326326+#endif /* USING_SJLJ_EXCEPTIONS */
+307
src/libunwind-darwin/unwind.inc
···11+/* Exception handling and frame unwind runtime interface routines. -*- C -*-
22+ Copyright (C) 2001, 2003, 2008, 2009 Free Software Foundation, Inc.
33+44+ This file is part of GCC.
55+66+ GCC is free software; you can redistribute it and/or modify it
77+ under the terms of the GNU General Public License as published by
88+ the Free Software Foundation; either version 3, or (at your option)
99+ any later version.
1010+1111+ GCC is distributed in the hope that it will be useful, but WITHOUT
1212+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1313+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
1414+ License for more details.
1515+1616+ Under Section 7 of GPL version 3, you are granted additional
1717+ permissions described in the GCC Runtime Library Exception, version
1818+ 3.1, as published by the Free Software Foundation.
1919+2020+ You should have received a copy of the GNU General Public License and
2121+ a copy of the GCC Runtime Library Exception along with this program;
2222+ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
2323+ <http://www.gnu.org/licenses/>. */
2424+2525+/* This is derived from the C++ ABI for IA-64. Where we diverge
2626+ for cross-architecture compatibility are noted with "@@@".
2727+ This file is included from unwind-dw2.c, unwind-sjlj.c or
2828+ unwind-ia64.c. */
2929+3030+/* Subroutine of _Unwind_RaiseException also invoked from _Unwind_Resume.
3131+3232+ Unwind the stack calling the personality routine to find both the
3333+ exception handler and intermediary cleanup code. We'll only locate
3434+ the first such frame here. Cleanup code will call back into
3535+ _Unwind_Resume and we'll continue Phase 2 there. */
3636+3737+static _Unwind_Reason_Code
3838+_Unwind_RaiseException_Phase2(struct _Unwind_Exception *exc,
3939+ struct _Unwind_Context *context)
4040+{
4141+ _Unwind_Reason_Code code;
4242+4343+ while (1)
4444+ {
4545+ _Unwind_FrameState fs;
4646+ int match_handler;
4747+4848+ code = uw_frame_state_for (context, &fs);
4949+5050+ /* Identify when we've reached the designated handler context. */
5151+ match_handler = (uw_identify_context (context) == exc->private_2
5252+ ? _UA_HANDLER_FRAME : 0);
5353+5454+ if (code != _URC_NO_REASON)
5555+ /* Some error encountered. Usually the unwinder doesn't
5656+ diagnose these and merely crashes. */
5757+ return _URC_FATAL_PHASE2_ERROR;
5858+5959+ /* Unwind successful. Run the personality routine, if any. */
6060+ if (fs.personality)
6161+ {
6262+ code = (*fs.personality) (1, _UA_CLEANUP_PHASE | match_handler,
6363+ exc->exception_class, exc, context);
6464+ if (code == _URC_INSTALL_CONTEXT)
6565+ break;
6666+ if (code != _URC_CONTINUE_UNWIND)
6767+ return _URC_FATAL_PHASE2_ERROR;
6868+ }
6969+7070+ /* Don't let us unwind past the handler context. */
7171+ gcc_assert (!match_handler);
7272+7373+ uw_update_context (context, &fs);
7474+ }
7575+7676+ return code;
7777+}
7878+7979+/* Raise an exception, passing along the given exception object. */
8080+8181+_Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
8282+_Unwind_RaiseException(struct _Unwind_Exception *exc)
8383+{
8484+ struct _Unwind_Context this_context, cur_context;
8585+ _Unwind_Reason_Code code;
8686+8787+ /* Set up this_context to describe the current stack frame. */
8888+ uw_init_context (&this_context);
8989+ cur_context = this_context;
9090+9191+ /* Phase 1: Search. Unwind the stack, calling the personality routine
9292+ with the _UA_SEARCH_PHASE flag set. Do not modify the stack yet. */
9393+ while (1)
9494+ {
9595+ _Unwind_FrameState fs;
9696+9797+ /* Set up fs to describe the FDE for the caller of cur_context. The
9898+ first time through the loop, that means __cxa_throw. */
9999+ code = uw_frame_state_for (&cur_context, &fs);
100100+101101+ if (code == _URC_END_OF_STACK)
102102+ /* Hit end of stack with no handler found. */
103103+ return _URC_END_OF_STACK;
104104+105105+ if (code != _URC_NO_REASON)
106106+ /* Some error encountered. Usually the unwinder doesn't
107107+ diagnose these and merely crashes. */
108108+ return _URC_FATAL_PHASE1_ERROR;
109109+110110+ /* Unwind successful. Run the personality routine, if any. */
111111+ if (fs.personality)
112112+ {
113113+ code = (*fs.personality) (1, _UA_SEARCH_PHASE, exc->exception_class,
114114+ exc, &cur_context);
115115+ if (code == _URC_HANDLER_FOUND)
116116+ break;
117117+ else if (code != _URC_CONTINUE_UNWIND)
118118+ return _URC_FATAL_PHASE1_ERROR;
119119+ }
120120+121121+ /* Update cur_context to describe the same frame as fs. */
122122+ uw_update_context (&cur_context, &fs);
123123+ }
124124+125125+ /* Indicate to _Unwind_Resume and associated subroutines that this
126126+ is not a forced unwind. Further, note where we found a handler. */
127127+ exc->private_1 = 0;
128128+ exc->private_2 = uw_identify_context (&cur_context);
129129+130130+ cur_context = this_context;
131131+ code = _Unwind_RaiseException_Phase2 (exc, &cur_context);
132132+ if (code != _URC_INSTALL_CONTEXT)
133133+ return code;
134134+135135+ uw_install_context (&this_context, &cur_context);
136136+}
137137+138138+139139+/* Subroutine of _Unwind_ForcedUnwind also invoked from _Unwind_Resume. */
140140+141141+static _Unwind_Reason_Code
142142+_Unwind_ForcedUnwind_Phase2 (struct _Unwind_Exception *exc,
143143+ struct _Unwind_Context *context)
144144+{
145145+ _Unwind_Stop_Fn stop = (_Unwind_Stop_Fn) (_Unwind_Ptr) exc->private_1;
146146+ void *stop_argument = (void *) (_Unwind_Ptr) exc->private_2;
147147+ _Unwind_Reason_Code code, stop_code;
148148+149149+ while (1)
150150+ {
151151+ _Unwind_FrameState fs;
152152+ int action;
153153+154154+ /* Set up fs to describe the FDE for the caller of cur_context. */
155155+ code = uw_frame_state_for (context, &fs);
156156+ if (code != _URC_NO_REASON && code != _URC_END_OF_STACK)
157157+ return _URC_FATAL_PHASE2_ERROR;
158158+159159+ /* Unwind successful. */
160160+ action = _UA_FORCE_UNWIND | _UA_CLEANUP_PHASE;
161161+ if (code == _URC_END_OF_STACK)
162162+ action |= _UA_END_OF_STACK;
163163+ stop_code = (*stop) (1, action, exc->exception_class, exc,
164164+ context, stop_argument);
165165+ if (stop_code != _URC_NO_REASON)
166166+ return _URC_FATAL_PHASE2_ERROR;
167167+168168+ /* Stop didn't want to do anything. Invoke the personality
169169+ handler, if applicable, to run cleanups. */
170170+ if (code == _URC_END_OF_STACK)
171171+ break;
172172+173173+ if (fs.personality)
174174+ {
175175+ code = (*fs.personality) (1, _UA_FORCE_UNWIND | _UA_CLEANUP_PHASE,
176176+ exc->exception_class, exc, context);
177177+ if (code == _URC_INSTALL_CONTEXT)
178178+ break;
179179+ if (code != _URC_CONTINUE_UNWIND)
180180+ return _URC_FATAL_PHASE2_ERROR;
181181+ }
182182+183183+ /* Update cur_context to describe the same frame as fs, and discard
184184+ the previous context if necessary. */
185185+ uw_advance_context (context, &fs);
186186+ }
187187+188188+ return code;
189189+}
190190+191191+192192+/* Raise an exception for forced unwinding. */
193193+194194+_Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
195195+_Unwind_ForcedUnwind (struct _Unwind_Exception *exc,
196196+ _Unwind_Stop_Fn stop, void * stop_argument)
197197+{
198198+ struct _Unwind_Context this_context, cur_context;
199199+ _Unwind_Reason_Code code;
200200+201201+ uw_init_context (&this_context);
202202+ cur_context = this_context;
203203+204204+ exc->private_1 = (_Unwind_Ptr) stop;
205205+ exc->private_2 = (_Unwind_Ptr) stop_argument;
206206+207207+ code = _Unwind_ForcedUnwind_Phase2 (exc, &cur_context);
208208+ if (code != _URC_INSTALL_CONTEXT)
209209+ return code;
210210+211211+ uw_install_context (&this_context, &cur_context);
212212+}
213213+214214+215215+/* Resume propagation of an existing exception. This is used after
216216+ e.g. executing cleanup code, and not to implement rethrowing. */
217217+218218+void LIBGCC2_UNWIND_ATTRIBUTE
219219+_Unwind_Resume (struct _Unwind_Exception *exc)
220220+{
221221+ struct _Unwind_Context this_context, cur_context;
222222+ _Unwind_Reason_Code code;
223223+224224+ uw_init_context (&this_context);
225225+ cur_context = this_context;
226226+227227+ /* Choose between continuing to process _Unwind_RaiseException
228228+ or _Unwind_ForcedUnwind. */
229229+ if (exc->private_1 == 0)
230230+ code = _Unwind_RaiseException_Phase2 (exc, &cur_context);
231231+ else
232232+ code = _Unwind_ForcedUnwind_Phase2 (exc, &cur_context);
233233+234234+ gcc_assert (code == _URC_INSTALL_CONTEXT);
235235+236236+ uw_install_context (&this_context, &cur_context);
237237+}
238238+239239+240240+/* Resume propagation of an FORCE_UNWIND exception, or to rethrow
241241+ a normal exception that was handled. */
242242+243243+_Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
244244+_Unwind_Resume_or_Rethrow (struct _Unwind_Exception *exc)
245245+{
246246+ struct _Unwind_Context this_context, cur_context;
247247+ _Unwind_Reason_Code code;
248248+249249+ /* Choose between continuing to process _Unwind_RaiseException
250250+ or _Unwind_ForcedUnwind. */
251251+ if (exc->private_1 == 0)
252252+ return _Unwind_RaiseException (exc);
253253+254254+ uw_init_context (&this_context);
255255+ cur_context = this_context;
256256+257257+ code = _Unwind_ForcedUnwind_Phase2 (exc, &cur_context);
258258+259259+ gcc_assert (code == _URC_INSTALL_CONTEXT);
260260+261261+ uw_install_context (&this_context, &cur_context);
262262+}
263263+264264+265265+/* A convenience function that calls the exception_cleanup field. */
266266+267267+void
268268+_Unwind_DeleteException (struct _Unwind_Exception *exc)
269269+{
270270+ if (exc->exception_cleanup)
271271+ (*exc->exception_cleanup) (_URC_FOREIGN_EXCEPTION_CAUGHT, exc);
272272+}
273273+274274+275275+/* Perform stack backtrace through unwind data. */
276276+277277+_Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
278278+_Unwind_Backtrace(_Unwind_Trace_Fn trace, void * trace_argument)
279279+{
280280+ struct _Unwind_Context context;
281281+ _Unwind_Reason_Code code;
282282+283283+ uw_init_context (&context);
284284+285285+ while (1)
286286+ {
287287+ _Unwind_FrameState fs;
288288+289289+ /* Set up fs to describe the FDE for the caller of context. */
290290+ code = uw_frame_state_for (&context, &fs);
291291+ if (code != _URC_NO_REASON && code != _URC_END_OF_STACK)
292292+ return _URC_FATAL_PHASE1_ERROR;
293293+294294+ /* Call trace function. */
295295+ if ((*trace) (&context, trace_argument) != _URC_NO_REASON)
296296+ return _URC_FATAL_PHASE1_ERROR;
297297+298298+ /* We're done at end of stack. */
299299+ if (code == _URC_END_OF_STACK)
300300+ break;
301301+302302+ /* Update context to describe the same frame as fs. */
303303+ uw_update_context (&context, &fs);
304304+ }
305305+306306+ return code;
307307+}