What are .seh_* assembly commands that gcc outputs?

若如初见. 提交于 2019-12-18 14:53:47

问题


I use gcc -S for a hello world program. What are the 5 .seh_ commands? I can't seem to find much info at all about them when I search.

    .file   "hi.c"
    .def    __main; .scl    2;  .type   32; .endef
    .section .rdata,"dr"
.LC0:
    .ascii "Hello World\0"
    .text
    .globl  main
    .def    main;   .scl    2;  .type   32; .endef
    .seh_proc   main
main:
    pushq   %rbp
    .seh_pushreg    %rbp
    movq    %rsp, %rbp
    .seh_setframe   %rbp, 0
    subq    $32, %rsp
    .seh_stackalloc 32
    .seh_endprologue
    call    __main
    leaq    .LC0(%rip), %rcx
    call    puts
    movl    $0, %eax
    addq    $32, %rsp
    popq    %rbp
    ret
    .seh_endproc
    .ident  "GCC: (rubenvb-4.8.0) 4.8.0"
    .def    puts;   .scl    2;  .type   32; .endef

回答1:


These are gas's implementation of MASM's frame handling pseudos for generating an executable's .pdata and .xdata sections (structured exception handling stuff). Also check out Raw Pseudo Operations. Apparently if your code might be in the stack during an SEH unwind operation, you are expected to use these.

I found slightly more information at https://sourceware.org/ml/binutils/2009-08/msg00193.html. This thread seems to be the original checkin to gas to add support for all the .set_* pseudo ops.

I would like to show the patch for .pdata and .xdata generation of pe-coff targets via gas, and to get some feed-back. This patch includes support for arm, ppc, arm, sh (3&4), mips, and x64. As for x86 there is no OS support for runtime function information, I spared this part. It would just increase executable size for x86 PE and there is no real gain for this target.

Short overview:
There are at the moment three different function entry formats preset.

The first is the MIPS one. The second version is for ARM, PPC, SH3, and SH4 mainly for Windows CE. The third is the IA64 and x64 version. Note, the IA64 isn't implemented yet, but to find information about it, please see specification about IA64 on http://download.intel.com/design/Itanium/Downloads/245358.pdf file.

The first version has just entries in the pdata section: BeginAddress, EndAddress, ExceptionHandler, HandlerData, and PrologueEndAddress. Each value is a pointer to the corresponding data and has size of 4 bytes.

The second variant has the following entries in the pdata section. BeginAddress, PrologueLength (8 bits), EndAddress (22 bits), Use-32-bit-instruction (1 bit), and Exception-Handler-Exists (1 bit). If the FunctionLength is zero, or the Exception-Handler-Exists bit is true, a DATA_EH block is placed directly before function entry.

The third version has a function entry block of BeginAddress (RVA), EndAddress (RVA), and UnwindData (RVA). The description of the prologue, excepetion-handler, and additional SEH data is stored within the UNWIND_DATA field in the xdata section.

.seh_proc <fct_name>
This specifies, that a SEH block begins for the function <fct_name>. This is valid for all targets.

.seh_endprologue
By this pseudo the location of the prologue end-address (taken by the current code address of the appearance of this pseudo). Valid for all targets.

.seh_handler <handler>[,<handler-data>]
This pseudo specifies the handler function to be used. For version 2 the handler-data field specifies the user optional data block. For version 3 the handler-data field can be a rva to user-data (for FHANDLER), if the name is @unwind the UHANDLER unwind block is generated, and if it is @except (or not specified at all) EHANDLER exception block is generated.

.seh_eh
This pseudo is used for version 2 to indicate the location of the function begin in assembly. Here the PDATA_EH data is may stored to.

.seh_32/.seh_no32
This pseudos are just used for version 2 (see above for description). At the moment it defaults to no32, if not specified.

.seh_endproc
By this pseudo the end of the SEH block is specified.

.seh_setframe <reg>,<offset>
By this pseudo the frame-register and the offset (value between 0-240 with 16-byte alignment) can be specified. This is just used by version 3.

.seh_stackalloc <size>
By this stack allocation in code is described for version 3.

.seh_pushreg <reg>
By this a general register push in code is described for version 3.

.seh_savereg <reg>
By this a general register save to memory in code is described for version 3.

.seh_savemm <mm>
By this a mm register save to memory in code is described for version 3.

.seh_savexmm
By this a xmm register save to memory in code is described for version 3.

.seh_pushframe
By this information about entry kind can be described for version 3.

.seh_scope <begin>,<end>,<handler>,<jump>
By this SCOPED entries for unwind or exceptions can be specified for version 3. This is just valid for UHANDLE and EHANDLER xdata descriptor and a global handler has to be specified. For handler and jump arguments, names of @1,@0, and @null can be used and they are specifying that a constant instead of a rva has to be used.

There is also some hard-core discussion of .xdata and .pdata (along with a bunch of links) at https://sourceware.org/ml/binutils/2009-04/msg00181.html.




回答2:


I stopped them from being output by using:

gcc -S -fno-asynchronous-unwind-tables hi.c

so I can look that up. But I'm happy with just not having them output anymore.




回答3:


They seem related to exception handling. That's all I could find.

http://ftp.netbsd.org/pub/NetBSD/NetBSD-current/src/external/gpl3/binutils/dist/gas/config/obj-coff-seh.h



来源:https://stackoverflow.com/questions/20819927/what-are-seh-assembly-commands-that-gcc-outputs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!