|
|
|||
File indexing completed on 2026-05-10 08:44:48
0001 //===----------------------------------------------------------------------===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 // 0008 // Darwin's alternative to DWARF based unwind encodings. 0009 // 0010 //===----------------------------------------------------------------------===// 0011 0012 0013 #ifndef __COMPACT_UNWIND_ENCODING__ 0014 #define __COMPACT_UNWIND_ENCODING__ 0015 0016 #include <stdint.h> 0017 0018 // 0019 // Compilers can emit standard DWARF FDEs in the __TEXT,__eh_frame section 0020 // of object files. Or compilers can emit compact unwind information in 0021 // the __LD,__compact_unwind section. 0022 // 0023 // When the linker creates a final linked image, it will create a 0024 // __TEXT,__unwind_info section. This section is a small and fast way for the 0025 // runtime to access unwind info for any given function. If the compiler 0026 // emitted compact unwind info for the function, that compact unwind info will 0027 // be encoded in the __TEXT,__unwind_info section. If the compiler emitted 0028 // DWARF unwind info, the __TEXT,__unwind_info section will contain the offset 0029 // of the FDE in the __TEXT,__eh_frame section in the final linked image. 0030 // 0031 // Note: Previously, the linker would transform some DWARF unwind infos into 0032 // compact unwind info. But that is fragile and no longer done. 0033 0034 0035 // 0036 // The compact unwind encoding is a 32-bit value which encoded in an 0037 // architecture specific way, which registers to restore from where, and how 0038 // to unwind out of the function. 0039 // 0040 typedef uint32_t compact_unwind_encoding_t; 0041 0042 0043 // architecture independent bits 0044 enum { 0045 UNWIND_IS_NOT_FUNCTION_START = 0x80000000, 0046 UNWIND_HAS_LSDA = 0x40000000, 0047 UNWIND_PERSONALITY_MASK = 0x30000000, 0048 }; 0049 0050 0051 0052 0053 // 0054 // x86 0055 // 0056 // 1-bit: start 0057 // 1-bit: has lsda 0058 // 2-bit: personality index 0059 // 0060 // 4-bits: 0=old, 1=ebp based, 2=stack-imm, 3=stack-ind, 4=DWARF 0061 // ebp based: 0062 // 15-bits (5*3-bits per reg) register permutation 0063 // 8-bits for stack offset 0064 // frameless: 0065 // 8-bits stack size 0066 // 3-bits stack adjust 0067 // 3-bits register count 0068 // 10-bits register permutation 0069 // 0070 enum { 0071 UNWIND_X86_MODE_MASK = 0x0F000000, 0072 UNWIND_X86_MODE_EBP_FRAME = 0x01000000, 0073 UNWIND_X86_MODE_STACK_IMMD = 0x02000000, 0074 UNWIND_X86_MODE_STACK_IND = 0x03000000, 0075 UNWIND_X86_MODE_DWARF = 0x04000000, 0076 0077 UNWIND_X86_EBP_FRAME_REGISTERS = 0x00007FFF, 0078 UNWIND_X86_EBP_FRAME_OFFSET = 0x00FF0000, 0079 0080 UNWIND_X86_FRAMELESS_STACK_SIZE = 0x00FF0000, 0081 UNWIND_X86_FRAMELESS_STACK_ADJUST = 0x0000E000, 0082 UNWIND_X86_FRAMELESS_STACK_REG_COUNT = 0x00001C00, 0083 UNWIND_X86_FRAMELESS_STACK_REG_PERMUTATION = 0x000003FF, 0084 0085 UNWIND_X86_DWARF_SECTION_OFFSET = 0x00FFFFFF, 0086 }; 0087 0088 enum { 0089 UNWIND_X86_REG_NONE = 0, 0090 UNWIND_X86_REG_EBX = 1, 0091 UNWIND_X86_REG_ECX = 2, 0092 UNWIND_X86_REG_EDX = 3, 0093 UNWIND_X86_REG_EDI = 4, 0094 UNWIND_X86_REG_ESI = 5, 0095 UNWIND_X86_REG_EBP = 6, 0096 }; 0097 0098 // 0099 // For x86 there are four modes for the compact unwind encoding: 0100 // UNWIND_X86_MODE_EBP_FRAME: 0101 // EBP based frame where EBP is push on stack immediately after return address, 0102 // then ESP is moved to EBP. Thus, to unwind ESP is restored with the current 0103 // EPB value, then EBP is restored by popping off the stack, and the return 0104 // is done by popping the stack once more into the pc. 0105 // All non-volatile registers that need to be restored must have been saved 0106 // in a small range in the stack that starts EBP-4 to EBP-1020. The offset/4 0107 // is encoded in the UNWIND_X86_EBP_FRAME_OFFSET bits. The registers saved 0108 // are encoded in the UNWIND_X86_EBP_FRAME_REGISTERS bits as five 3-bit entries. 0109 // Each entry contains which register to restore. 0110 // UNWIND_X86_MODE_STACK_IMMD: 0111 // A "frameless" (EBP not used as frame pointer) function with a small 0112 // constant stack size. To return, a constant (encoded in the compact 0113 // unwind encoding) is added to the ESP. Then the return is done by 0114 // popping the stack into the pc. 0115 // All non-volatile registers that need to be restored must have been saved 0116 // on the stack immediately after the return address. The stack_size/4 is 0117 // encoded in the UNWIND_X86_FRAMELESS_STACK_SIZE (max stack size is 1024). 0118 // The number of registers saved is encoded in UNWIND_X86_FRAMELESS_STACK_REG_COUNT. 0119 // UNWIND_X86_FRAMELESS_STACK_REG_PERMUTATION contains which registers were 0120 // saved and their order. 0121 // UNWIND_X86_MODE_STACK_IND: 0122 // A "frameless" (EBP not used as frame pointer) function large constant 0123 // stack size. This case is like the previous, except the stack size is too 0124 // large to encode in the compact unwind encoding. Instead it requires that 0125 // the function contains "subl $nnnnnnnn,ESP" in its prolog. The compact 0126 // encoding contains the offset to the nnnnnnnn value in the function in 0127 // UNWIND_X86_FRAMELESS_STACK_SIZE. 0128 // UNWIND_X86_MODE_DWARF: 0129 // No compact unwind encoding is available. Instead the low 24-bits of the 0130 // compact encoding is the offset of the DWARF FDE in the __eh_frame section. 0131 // This mode is never used in object files. It is only generated by the 0132 // linker in final linked images which have only DWARF unwind info for a 0133 // function. 0134 // 0135 // The permutation encoding is a Lehmer code sequence encoded into a 0136 // single variable-base number so we can encode the ordering of up to 0137 // six registers in a 10-bit space. 0138 // 0139 // The following is the algorithm used to create the permutation encoding used 0140 // with frameless stacks. It is passed the number of registers to be saved and 0141 // an array of the register numbers saved. 0142 // 0143 //uint32_t permute_encode(uint32_t registerCount, const uint32_t registers[6]) 0144 //{ 0145 // uint32_t renumregs[6]; 0146 // for (int i=6-registerCount; i < 6; ++i) { 0147 // int countless = 0; 0148 // for (int j=6-registerCount; j < i; ++j) { 0149 // if ( registers[j] < registers[i] ) 0150 // ++countless; 0151 // } 0152 // renumregs[i] = registers[i] - countless -1; 0153 // } 0154 // uint32_t permutationEncoding = 0; 0155 // switch ( registerCount ) { 0156 // case 6: 0157 // permutationEncoding |= (120*renumregs[0] + 24*renumregs[1] 0158 // + 6*renumregs[2] + 2*renumregs[3] 0159 // + renumregs[4]); 0160 // break; 0161 // case 5: 0162 // permutationEncoding |= (120*renumregs[1] + 24*renumregs[2] 0163 // + 6*renumregs[3] + 2*renumregs[4] 0164 // + renumregs[5]); 0165 // break; 0166 // case 4: 0167 // permutationEncoding |= (60*renumregs[2] + 12*renumregs[3] 0168 // + 3*renumregs[4] + renumregs[5]); 0169 // break; 0170 // case 3: 0171 // permutationEncoding |= (20*renumregs[3] + 4*renumregs[4] 0172 // + renumregs[5]); 0173 // break; 0174 // case 2: 0175 // permutationEncoding |= (5*renumregs[4] + renumregs[5]); 0176 // break; 0177 // case 1: 0178 // permutationEncoding |= (renumregs[5]); 0179 // break; 0180 // } 0181 // return permutationEncoding; 0182 //} 0183 // 0184 0185 0186 0187 0188 // 0189 // x86_64 0190 // 0191 // 1-bit: start 0192 // 1-bit: has lsda 0193 // 2-bit: personality index 0194 // 0195 // 4-bits: 0=old, 1=rbp based, 2=stack-imm, 3=stack-ind, 4=DWARF 0196 // rbp based: 0197 // 15-bits (5*3-bits per reg) register permutation 0198 // 8-bits for stack offset 0199 // frameless: 0200 // 8-bits stack size 0201 // 3-bits stack adjust 0202 // 3-bits register count 0203 // 10-bits register permutation 0204 // 0205 enum { 0206 UNWIND_X86_64_MODE_MASK = 0x0F000000, 0207 UNWIND_X86_64_MODE_RBP_FRAME = 0x01000000, 0208 UNWIND_X86_64_MODE_STACK_IMMD = 0x02000000, 0209 UNWIND_X86_64_MODE_STACK_IND = 0x03000000, 0210 UNWIND_X86_64_MODE_DWARF = 0x04000000, 0211 0212 UNWIND_X86_64_RBP_FRAME_REGISTERS = 0x00007FFF, 0213 UNWIND_X86_64_RBP_FRAME_OFFSET = 0x00FF0000, 0214 0215 UNWIND_X86_64_FRAMELESS_STACK_SIZE = 0x00FF0000, 0216 UNWIND_X86_64_FRAMELESS_STACK_ADJUST = 0x0000E000, 0217 UNWIND_X86_64_FRAMELESS_STACK_REG_COUNT = 0x00001C00, 0218 UNWIND_X86_64_FRAMELESS_STACK_REG_PERMUTATION = 0x000003FF, 0219 0220 UNWIND_X86_64_DWARF_SECTION_OFFSET = 0x00FFFFFF, 0221 }; 0222 0223 enum { 0224 UNWIND_X86_64_REG_NONE = 0, 0225 UNWIND_X86_64_REG_RBX = 1, 0226 UNWIND_X86_64_REG_R12 = 2, 0227 UNWIND_X86_64_REG_R13 = 3, 0228 UNWIND_X86_64_REG_R14 = 4, 0229 UNWIND_X86_64_REG_R15 = 5, 0230 UNWIND_X86_64_REG_RBP = 6, 0231 }; 0232 // 0233 // For x86_64 there are four modes for the compact unwind encoding: 0234 // UNWIND_X86_64_MODE_RBP_FRAME: 0235 // RBP based frame where RBP is push on stack immediately after return address, 0236 // then RSP is moved to RBP. Thus, to unwind RSP is restored with the current 0237 // EPB value, then RBP is restored by popping off the stack, and the return 0238 // is done by popping the stack once more into the pc. 0239 // All non-volatile registers that need to be restored must have been saved 0240 // in a small range in the stack that starts RBP-8 to RBP-2040. The offset/8 0241 // is encoded in the UNWIND_X86_64_RBP_FRAME_OFFSET bits. The registers saved 0242 // are encoded in the UNWIND_X86_64_RBP_FRAME_REGISTERS bits as five 3-bit entries. 0243 // Each entry contains which register to restore. 0244 // UNWIND_X86_64_MODE_STACK_IMMD: 0245 // A "frameless" (RBP not used as frame pointer) function with a small 0246 // constant stack size. To return, a constant (encoded in the compact 0247 // unwind encoding) is added to the RSP. Then the return is done by 0248 // popping the stack into the pc. 0249 // All non-volatile registers that need to be restored must have been saved 0250 // on the stack immediately after the return address. The stack_size/8 is 0251 // encoded in the UNWIND_X86_64_FRAMELESS_STACK_SIZE (max stack size is 2048). 0252 // The number of registers saved is encoded in UNWIND_X86_64_FRAMELESS_STACK_REG_COUNT. 0253 // UNWIND_X86_64_FRAMELESS_STACK_REG_PERMUTATION contains which registers were 0254 // saved and their order. 0255 // UNWIND_X86_64_MODE_STACK_IND: 0256 // A "frameless" (RBP not used as frame pointer) function large constant 0257 // stack size. This case is like the previous, except the stack size is too 0258 // large to encode in the compact unwind encoding. Instead it requires that 0259 // the function contains "subq $nnnnnnnn,RSP" in its prolog. The compact 0260 // encoding contains the offset to the nnnnnnnn value in the function in 0261 // UNWIND_X86_64_FRAMELESS_STACK_SIZE. 0262 // UNWIND_X86_64_MODE_DWARF: 0263 // No compact unwind encoding is available. Instead the low 24-bits of the 0264 // compact encoding is the offset of the DWARF FDE in the __eh_frame section. 0265 // This mode is never used in object files. It is only generated by the 0266 // linker in final linked images which have only DWARF unwind info for a 0267 // function. 0268 // 0269 0270 0271 // ARM64 0272 // 0273 // 1-bit: start 0274 // 1-bit: has lsda 0275 // 2-bit: personality index 0276 // 0277 // 4-bits: 4=frame-based, 3=DWARF, 2=frameless 0278 // frameless: 0279 // 12-bits of stack size 0280 // frame-based: 0281 // 4-bits D reg pairs saved 0282 // 5-bits X reg pairs saved 0283 // DWARF: 0284 // 24-bits offset of DWARF FDE in __eh_frame section 0285 // 0286 enum { 0287 UNWIND_ARM64_MODE_MASK = 0x0F000000, 0288 UNWIND_ARM64_MODE_FRAMELESS = 0x02000000, 0289 UNWIND_ARM64_MODE_DWARF = 0x03000000, 0290 UNWIND_ARM64_MODE_FRAME = 0x04000000, 0291 0292 UNWIND_ARM64_FRAME_X19_X20_PAIR = 0x00000001, 0293 UNWIND_ARM64_FRAME_X21_X22_PAIR = 0x00000002, 0294 UNWIND_ARM64_FRAME_X23_X24_PAIR = 0x00000004, 0295 UNWIND_ARM64_FRAME_X25_X26_PAIR = 0x00000008, 0296 UNWIND_ARM64_FRAME_X27_X28_PAIR = 0x00000010, 0297 UNWIND_ARM64_FRAME_D8_D9_PAIR = 0x00000100, 0298 UNWIND_ARM64_FRAME_D10_D11_PAIR = 0x00000200, 0299 UNWIND_ARM64_FRAME_D12_D13_PAIR = 0x00000400, 0300 UNWIND_ARM64_FRAME_D14_D15_PAIR = 0x00000800, 0301 0302 UNWIND_ARM64_FRAMELESS_STACK_SIZE_MASK = 0x00FFF000, 0303 UNWIND_ARM64_DWARF_SECTION_OFFSET = 0x00FFFFFF, 0304 }; 0305 // For arm64 there are three modes for the compact unwind encoding: 0306 // UNWIND_ARM64_MODE_FRAME: 0307 // This is a standard arm64 prolog where FP/LR are immediately pushed on the 0308 // stack, then SP is copied to FP. If there are any non-volatile registers 0309 // saved, then are copied into the stack frame in pairs in a contiguous 0310 // range right below the saved FP/LR pair. Any subset of the five X pairs 0311 // and four D pairs can be saved, but the memory layout must be in register 0312 // number order. 0313 // UNWIND_ARM64_MODE_FRAMELESS: 0314 // A "frameless" leaf function, where FP/LR are not saved. The return address 0315 // remains in LR throughout the function. If any non-volatile registers 0316 // are saved, they must be pushed onto the stack before any stack space is 0317 // allocated for local variables. The stack sized (including any saved 0318 // non-volatile registers) divided by 16 is encoded in the bits 0319 // UNWIND_ARM64_FRAMELESS_STACK_SIZE_MASK. 0320 // UNWIND_ARM64_MODE_DWARF: 0321 // No compact unwind encoding is available. Instead the low 24-bits of the 0322 // compact encoding is the offset of the DWARF FDE in the __eh_frame section. 0323 // This mode is never used in object files. It is only generated by the 0324 // linker in final linked images which have only DWARF unwind info for a 0325 // function. 0326 // 0327 0328 0329 0330 0331 0332 //////////////////////////////////////////////////////////////////////////////// 0333 // 0334 // Relocatable Object Files: __LD,__compact_unwind 0335 // 0336 //////////////////////////////////////////////////////////////////////////////// 0337 0338 // 0339 // A compiler can generated compact unwind information for a function by adding 0340 // a "row" to the __LD,__compact_unwind section. This section has the 0341 // S_ATTR_DEBUG bit set, so the section will be ignored by older linkers. 0342 // It is removed by the new linker, so never ends up in final executables. 0343 // This section is a table, initially with one row per function (that needs 0344 // unwind info). The table columns and some conceptual entries are: 0345 // 0346 // range-start pointer to start of function/range 0347 // range-length 0348 // compact-unwind-encoding 32-bit encoding 0349 // personality-function or zero if no personality function 0350 // lsda or zero if no LSDA data 0351 // 0352 // The length and encoding fields are 32-bits. The other are all pointer sized. 0353 // 0354 // In x86_64 assembly, these entry would look like: 0355 // 0356 // .section __LD,__compact_unwind,regular,debug 0357 // 0358 // #compact unwind for _foo 0359 // .quad _foo 0360 // .set L1,LfooEnd-_foo 0361 // .long L1 0362 // .long 0x01010001 0363 // .quad 0 0364 // .quad 0 0365 // 0366 // #compact unwind for _bar 0367 // .quad _bar 0368 // .set L2,LbarEnd-_bar 0369 // .long L2 0370 // .long 0x01020011 0371 // .quad __gxx_personality 0372 // .quad except_tab1 0373 // 0374 // 0375 // Notes: There is no need for any labels in the __compact_unwind section. 0376 // The use of the .set directive is to force the evaluation of the 0377 // range-length at assembly time, instead of generating relocations. 0378 // 0379 // To support future compiler optimizations where which non-volatile registers 0380 // are saved changes within a function (e.g. delay saving non-volatiles until 0381 // necessary), there can by multiple lines in the __compact_unwind table for one 0382 // function, each with a different (non-overlapping) range and each with 0383 // different compact unwind encodings that correspond to the non-volatiles 0384 // saved at that range of the function. 0385 // 0386 // If a particular function is so wacky that there is no compact unwind way 0387 // to encode it, then the compiler can emit traditional DWARF unwind info. 0388 // The runtime will use which ever is available. 0389 // 0390 // Runtime support for compact unwind encodings are only available on 10.6 0391 // and later. So, the compiler should not generate it when targeting pre-10.6. 0392 0393 0394 0395 0396 //////////////////////////////////////////////////////////////////////////////// 0397 // 0398 // Final Linked Images: __TEXT,__unwind_info 0399 // 0400 //////////////////////////////////////////////////////////////////////////////// 0401 0402 // 0403 // The __TEXT,__unwind_info section is laid out for an efficient two level lookup. 0404 // The header of the section contains a coarse index that maps function address 0405 // to the page (4096 byte block) containing the unwind info for that function. 0406 // 0407 0408 #define UNWIND_SECTION_VERSION 1 0409 struct unwind_info_section_header 0410 { 0411 uint32_t version; // UNWIND_SECTION_VERSION 0412 uint32_t commonEncodingsArraySectionOffset; 0413 uint32_t commonEncodingsArrayCount; 0414 uint32_t personalityArraySectionOffset; 0415 uint32_t personalityArrayCount; 0416 uint32_t indexSectionOffset; 0417 uint32_t indexCount; 0418 // compact_unwind_encoding_t[] 0419 // uint32_t personalities[] 0420 // unwind_info_section_header_index_entry[] 0421 // unwind_info_section_header_lsda_index_entry[] 0422 }; 0423 0424 struct unwind_info_section_header_index_entry 0425 { 0426 uint32_t functionOffset; 0427 uint32_t secondLevelPagesSectionOffset; // section offset to start of regular or compress page 0428 uint32_t lsdaIndexArraySectionOffset; // section offset to start of lsda_index array for this range 0429 }; 0430 0431 struct unwind_info_section_header_lsda_index_entry 0432 { 0433 uint32_t functionOffset; 0434 uint32_t lsdaOffset; 0435 }; 0436 0437 // 0438 // There are two kinds of second level index pages: regular and compressed. 0439 // A compressed page can hold up to 1021 entries, but it cannot be used 0440 // if too many different encoding types are used. The regular page holds 0441 // 511 entries. 0442 // 0443 0444 struct unwind_info_regular_second_level_entry 0445 { 0446 uint32_t functionOffset; 0447 compact_unwind_encoding_t encoding; 0448 }; 0449 0450 #define UNWIND_SECOND_LEVEL_REGULAR 2 0451 struct unwind_info_regular_second_level_page_header 0452 { 0453 uint32_t kind; // UNWIND_SECOND_LEVEL_REGULAR 0454 uint16_t entryPageOffset; 0455 uint16_t entryCount; 0456 // entry array 0457 }; 0458 0459 #define UNWIND_SECOND_LEVEL_COMPRESSED 3 0460 struct unwind_info_compressed_second_level_page_header 0461 { 0462 uint32_t kind; // UNWIND_SECOND_LEVEL_COMPRESSED 0463 uint16_t entryPageOffset; 0464 uint16_t entryCount; 0465 uint16_t encodingsPageOffset; 0466 uint16_t encodingsCount; 0467 // 32-bit entry array 0468 // encodings array 0469 }; 0470 0471 #define UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET(entry) (entry & 0x00FFFFFF) 0472 #define UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX(entry) ((entry >> 24) & 0xFF) 0473 0474 0475 0476 #endif 0477
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|