gcc程序集输出标签表示什么? [英] What do the gcc assembly output labels signify?

查看:55
本文介绍了gcc程序集输出标签表示什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个简单的C程序test.c:

I've written a simple C program test.c:

#include <stdio.h>
#include <stdlib.h>
int add(int a, int b);
int main()
{
    int i=5,j=10;
    int result;
    result = add(i, j);
    printf("result is %d\n", result);
}
int add(int a, int b)
{
    return (a + b);
}

我编译了它:

gcc -S -Os -o test.s test.c 

我得到了汇编文件test.s:

        .file   "test3.c"
    .section    .rodata
.LC0:
    .string "result is %d\n"
    .text
.globl main
    .type   main, @function
main:
.LFB5:
    pushq   %rbp
.LCFI0:
    movq    %rsp, %rbp
.LCFI1:
    subq    $16, %rsp
.LCFI2:
    movl    $5, -12(%rbp)
    movl    $10, -8(%rbp)
    movl    -8(%rbp), %esi
    movl    -12(%rbp), %edi
    call    add
    movl    %eax, -4(%rbp)
    movl    -4(%rbp), %esi
    movl    $.LC0, %edi
    movl    $0, %eax
    call    printf
    leave
    ret
.LFE5:
    .size   main, .-main
.globl add
    .type   add, @function
add:
.LFB6:
    pushq   %rbp
.LCFI3:
    movq    %rsp, %rbp
.LCFI4:
    movl    %edi, -4(%rbp)
    movl    %esi, -8(%rbp)
    movl    -8(%rbp), %eax
    addl    -4(%rbp), %eax
    leave
    ret
.LFE6:
    .size   add, .-add
    .section    .eh_frame,"a",@progbits
.Lframe1:
    .long   .LECIE1-.LSCIE1
.LSCIE1:
    .long   0x0
    .byte   0x1
    .string "zR"
    .uleb128 0x1
    .sleb128 -8
    .byte   0x10
    .uleb128 0x1
    .byte   0x3
    .byte   0xc
    .uleb128 0x7
    .uleb128 0x8
    .byte   0x90
    .uleb128 0x1
    .align 8
.LECIE1:
.LSFDE1:
    .long   .LEFDE1-.LASFDE1
.LASFDE1:
    .long   .LASFDE1-.Lframe1
    .long   .LFB5
    .long   .LFE5-.LFB5
    .uleb128 0x0
    .byte   0x4
    .long   .LCFI0-.LFB5
    .byte   0xe
    .uleb128 0x10
    .byte   0x86
    .uleb128 0x2
    .byte   0x4
    .long   .LCFI1-.LCFI0
    .byte   0xd
    .uleb128 0x6
    .align 8
.LEFDE1:
.LSFDE3:
    .long   .LEFDE3-.LASFDE3
.LASFDE3:
    .long   .LASFDE3-.Lframe1
    .long   .LFB6
    .long   .LFE6-.LFB6
    .uleb128 0x0
    .byte   0x4
    .long   .LCFI3-.LFB6
    .byte   0xe
    .uleb128 0x10
    .byte   0x86
    .uleb128 0x2
    .byte   0x4
    .long   .LCFI4-.LCFI3
    .byte   0xd
    .uleb128 0x6
    .align 8
.LEFDE3:
    .ident  "GCC: (GNU) 4.1.2 20080704 (Red Hat 4.1.2-48)"
    .section    .note.GNU-stack,"",@progbits

我理解所有这些说明,但是我真的不理解这些标签的含义. .LC0.LFB5.LCFI0.LCFI1.LCFI2.LFE5,...这些标签由gcc自动生成.为什么需要这些标签?似乎有些标签是多余的.

I understand all these instructions, but I really don't understand what these labels mean. .LC0, .LFB5, .LCFI0, .LCFI1, .LCFI2, .LFE5, ... These labels are generated automatically by gcc. Why does it need these labels? It seems that some labels are redundant.

  • gcc版本:4.1.2
  • 计算机:x86_64

推荐答案

编译器将为需要引用地址的任何位置生成标签,无论是跳转指令,分支指令还是数据位置.

The compiler will generate a label for any place it needs to refer to an address, whether it be for a jump or branch instruction, or for a data location.

编译器无需创建直观命名的标签,因为它们仅由其生成的代码引用并且没有最终用户的可见性,因此它可以生成或多或少的顺序命名标签,并具有防止意外创建标签的方案.相同的标签用于两个不同的位置.

The compiler has no need to create intuitively named labels since they are only referenced by code it generates and has no end-user visibility, so it generates more-or-less sequentially named labels, with a scheme to prevent accidentally creating the same label for two different locations.

使用两个(或多个)标签标记同一位置绝对没有缺点,因此没有尝试避免这种情况.这就是为什么有几个位置带有两个连续标签而没有中间操作的原因.

There is absolutely no disadvantages to labelling the same location with two (or more) labels, so there is no attempt to avoid that. That is why there are a few locations with two sequential labels with no intervening ops.

例如,如果您真的想知道LCxLFBx系列标签的含义,请阅读编译器源代码.这是一个不平凡的代码库,因此期望花费数小时来查找相关模块.

If you really want to know what the, for example, LCx and LFBx series of labels mean, read the compiler source code. This is a non-trivial code base, so expect to spend hours just looking for the relevant module.

我迎接挑战,因此具有一些编译器编写经验,并且发现模块/trunk/gcc/dwarf2out.c ,它似乎使用相同的策略生成标签名称.在第250行周围查看有关符号含义的简洁线索.这个模块的大部分内容决定了标签,但是它的长度接近23,000行,因此可以很好地测试您的好奇心.

I rose to the challenge, so—having some compiler writing experience—I found module /trunk/gcc/dwarf2out.c which seems to generate label names using the same strategy. Look around line 250 for terse clues about what the symbols mean. Much of this module determines the labels, but it is nearly 23,000 lines long, so it could well test your curiosity.

这篇关于gcc程序集输出标签表示什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆