.text
.globl main
.ent main
I don't know what .globl
and .ent
do. What is the role? Do I need to use globl. main
globl. main
and .ent main
all the time?
在您的情况下, .globl
是一个汇编器指令,它告诉汇编器可以从当前文件外部访问main
符号(也就是说,它可以从其他文件中引用),而.ent
是一个调试器(伪)操作标记main
的入口。
It is going to be the same in any other ISA on the GNU GAS assembler, eg on x86_64 Linux:
main.S
.text
.global _start
_start:
/* exit syscall */
mov $60, %rax
mov exit_status, %rdi
syscall
exit_status.S
.data
.global exit_status
exit_status:
.quad 42
Assemble and run:
as -o main.o main.S
as -o exit_status.o exit_status.S
ls -o main.out exit_statis.o main.o
./main.out
echo $?
gives:
42
But if we remove the line:
.global exit_status
then ld
fails with:
main.o: In function `_start':
(.text+0xb): undefined reference to `exit_status'
because it cannot see the symbol exit_status
that it needs.
.globl
and .global
are synonyms as mentioned in the documentation: https://sourceware.org/binutils/docs/as/Global.html#Global so I just prefer to use the one with the correct spelling ;-)
We can observe what is going on by looking into the information contained in the ELF object files .
For the correct program:
nm hello_world.o mystring.o
gives:
main.o:
0000000000000000 T _start
U exit_status
exit_status.o:
0000000000000000 D exit_status
and for the failing one:
exit_status.o:
0000000000000000 d exit_status
And:
man nm
contains:
The symbol type. At least the following types are used; others are, as well, depending on the object file format. If lowercase, the symbol is usually local; if uppercase, the symbol is global (external). There are however a few lowercase symbols that are shown for special global symbols ("u", "v" and "w").
"D" "d" The symbol is in the initialized data section. "T" "t" The symbol is in the text (code) section. "U" The symbol is undefined.
On the C level, you can control symbol visibility with the static
keyword: What does "static" mean in C?
Tested in Ubuntu 16.04, Binutils 2.26.1.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.