【发布时间】:2021-04-22 05:33:00
【问题描述】:
具有以下汇编源:
# hello_asm.s
# as hello_asm.s -o hello_asm.o
# ld hello_asm.o -e _main -o hello_asm
.section __DATA,__data
str:
.asciz "Hello world!\n"
.section __TEXT,__text
.globl _main
_main:
movl $0x2000004, %eax # preparing system call 4
movl $1, %edi # STDOUT file descriptor is 1
movq str@GOTPCREL(%rip), %rsi # The value to print
movq $100, %rdx # the size of the value to print
syscall
#
# EXITING
#
movl $0, %ebx
movl $0x2000001, %eax # exit 0
syscall
通过编译和链接以下指令:
as sum.s -g -o sum.o
ld -arch x86_64 -e main -L /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/lib -lSystem sum.o -o sum
通过尝试在 LLDB 上对其进行调试,我得到以下结果:
❯❯❯❯ lldb sum.o ~/D/test
(lldb) target create "sum.o"
Current executable set to '/Users/mbertamini/Downloads/test/sum.o' (x86_64).
(lldb) list
(lldb) b 16
error: No selected frame to use to find the default file.
error: No file supplied and no default file available.
(lldb)
这是侏儒:
❯❯❯❯ dwarfdump sum.o ~/D/t/summ
sum.o: file format Mach-O 64-bit x86-64
.debug_info contents:
0x00000000: Compile Unit: length = 0x00000094 version = 0x0004 abbr_offset = 0x0000 addr_size = 0x08 (next unit at 0x00000098)
0x0000000b: DW_TAG_compile_unit
DW_AT_stmt_list (0x00000000)
DW_AT_low_pc (0x0000000000000000)
DW_AT_high_pc (0x0000000000000026)
DW_AT_name ("sum.s")
DW_AT_comp_dir ("<filepath>")
DW_AT_producer ("Apple clang version 12.0.0 (clang-1200.0.32.27)")
DW_AT_language (DW_LANG_Mips_Assembler)
0x0000007e: DW_TAG_label
DW_AT_name ("main")
DW_AT_decl_file ("<filepath-file>")
DW_AT_decl_line (10)
DW_AT_low_pc (0x0000000000000000)
DW_AT_prototyped (0x00)
0x00000095: DW_TAG_unspecified_parameters
0x00000096: NULL
0x00000097: NULL
❯❯❯❯ as -v ~/D/t/summ
Apple clang version 12.0.0 (clang-1200.0.32.27)
Target: x86_64-apple-darwin20.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1as -triple x86_64-apple-macosx11.0.0 -filetype obj -main-file-name - -target-cpu penryn -fdebug-compilation-dir /Users/mbertamini/Downloads/test/summ -dwarf-debug-producer "Apple clang version 12.0.0 (clang-1200.0.32.27)" -dwarf-version=4 -mrelocation-model pic -o a.out -
有什么问题?我该怎么办?
【问题讨论】:
-
仅供参考,
movq str@GOTPCREL(%rip), %rsi是毫无意义的低效,而您可以简单地使用lea str(%rip), %rsi。我已经看到其他 SO 问题,人们为 x86-64 MacOS 执行此操作;是否有一些人们正在复制的坏例子? -
呃,声称它只能通过 GOT 访问。静态数据可以通过 RIP 相对寻址模式直接访问,除非您希望在共享库中支持符号插入。 (例如,使用 LD_PRELOAD 覆盖定义)。数据和文本之间的距离是一个链接时间常数,因此您可以轻松访问数据数据。就像您可以使用 RIP 相对寻址访问 GOT。
-
我已经发布了一条更改指令,
lea str(%rip), %rsi。我已经写了多个关于它的 SO 答案,包括 How to load address of function or label into register / Why are global variables in x86-64 accessed relative to the instruction pointer?,其他人也是如此:Why does this MOVSS instruction use RIP-relative addressing? -
@Bertuz: How do RIP-relative variable references like work? 接近(并且确实提到了 AT&T 语法)。也许还有Referencing the contents of a memory location. (x86 addressing modes) 关于机器可以做什么。和/或阅读英特尔或 AMD 的手册或继续学习教程。 wiki.osdev.org/… 还展示了它在机器代码中的工作方式。也相关:Why use RIP-relative addressing in NASM?
标签: macos assembly lldb macos-big-sur