Memory Consistency and Cache Coherence
1 | Disassembly of section .plt: |
.plt
节主要实现了使用过程链接表(Procedure Linkage Table)实现延迟绑定的功能。
问题:objdump 程序 有许多 类似 <.omp_outlined..16>: 的函数,但是main函数里并没有调用。实际openmp是怎么执行这些代码的呢?
在使用了OpenMP指令的C/C++程序编译后,编译器会自动生成一些名为.omp_outlined.
的函数。这些函数是OpenMP所需要的运行时支持函数,不是直接在main函数中调用的,其执行方式主要有以下几种:
.omp_outlined.
函数创建线程并发布工作。.omp_outlined.
函数,在循环分配工作时调用。所以.omp_outlined.
函数的执行是隐式通过运行时库触发和调度的,不需要用户代码直接调用。它们是OpenMP实现所必须的,由编译器和运行时库协调完成。用户只需要编写OpenMP指令,不必关心具体的调用细节。
总体来说,这是一种让并行执行透明化的实现机制,减少了用户的工作量。
不同平台不同,有GOMP_parallel_start开头的。也有如下x86平台的
1 | 405854: 48 c7 84 24 a0 00 00 movq $0x4293b9,0xa0(%rsp) |
这段汇编代码实现了OpenMP中的并行构造,主要执行了以下几个步骤:
__kmpc_fork_call
函数,这是OpenMP的runtime库函数,用来并行执行一个函数kmpc
fork multiple parallel call?所以这段代码实现了调用OpenMP runtime并行执行一个函数的操作,准备参数,调用runtime API,获取返回值的一个流程。
利用runtime库的支持函数可以实现汇编级别的OpenMP并行性。
各section位置以及含义,参考文档
1 | $ readelf -S bfs.inj |
[ 5] .dynsym
有关One section type, SHT_NOBITS
described below, occupies no
space in the file, and its sh_offset
member locates the conceptual placement in the
file.
so the number “2d258” remains unchanged.
1 | [25] .data PROGBITS 000000000042e258 0002d258 |
global offset table
This section holds the procedure linkage table. See ‘‘Special Sections’’ in Part 1 and ‘‘Procedure Linkage Table’’ in Part 2 for more information.
Function symbols (those with type STT_FUNC) in shared object files have special significance. When
another object file references a function from a shared object, the link editor automatically creates a procedure linkage table entry for the referenced symbol.
暂无
暂无
上面回答部分来自ChatGPT-3.5,没有进行正确性的交叉校验。
无
LLVM项目开始于一种比Java字节码更低层级的IR,因此,初始的首字母缩略词是Low Level Virtual Machine。它的想法是发掘低层优化的机会,采用链接时优化。
学过编译原理的人都知道,编译过程主要可以划分为前端与后端:
经典的编译器如gcc:在设计上前端到后端编写是强耦合的,你不需要知道,无法知道,也没有API来操作它的IR。
好处是:因为不需要暴露中间过程的接口,编译器可以在内部做任何想做的平台相关的优化。
坏处是,每当一个新的平台出现,这些编译器都要各自为政实现一个从自己的IR到新平台的后端。
LLVM的核心设计了一个叫 LLVM IR 的通用中间表示, 并以库(Library) 的方式提供一系列接口, 为你提供诸如操作IR、生成目标平台代码等等后端的功能。
在使用通用IR的情况下,如果有M种语言、N种目标平台,那么最优情况下我们只要实现 M+N 个前后端。
LVM IR实际上有三种表示:
各种格式是如何生成并相互转换:
格式 | 转换命令 |
---|---|
.c -> .ll | clang -emit-llvm -S a.c -o a.ll |
.c -> .bc | clang -emit-llvm -c a.c -o a.bc |
.ll -> .bc | llvm-as a.ll -o a.bc |
.bc -> .ll | llvm-dis a.bc -o a.ll |
.bc -> .s | llc a.bc -o a.s |
对于LLVM IR来说,.ll
文件就相当于汇编,.bc
文件就相当于机器码。 这也是llvm-as和llvm-dis指令为什么叫as和dis的缘故。
clang实现的前端包括
见 llvm Backend 一文
Clang 是 LLVM 项目中的一个 C/C++/Objective-C 编译器,它使用 LLVM 的前端和后端进行代码生成和优化。它可以将 C/C++/Objective-C 代码编译为 LLVM 的中间表示(LLVM IR),然后进一步将其转换为目标平台的机器码。Clang 拥有很好的错误信息展示和提示,支持多平台使用,是许多开发者的首选编译器之一。同时,Clang 也作为 LLVM 项目的一个前端,为 LLVM 的生态系统提供了广泛的支持和应用。
Clang 的开发起源于苹果公司的一个项目,即 LLVM/Clang 项目。在 2005 年,苹果公司希望能够使用一种更加灵活、可扩展、优化的编译器来替代 GCC 作为其操作系统 macOS (Mac OS X) 开发环境的默认编译器。由于当时的 GCC 开发被其维护者们认为变得缓慢和难以维护,苹果公司决定开发一款新的编译器,这就是 Clang 诞生的原因。Clang 的开发团队由该项目的创立者 Chris Lattner 领导,他带领团队将 Clang 发展为一款可扩展、模块化、高效的编译器,并成功地将其嵌入到苹果公司的开发工具链 Xcode 中,成为了 macOS 开发环境中默认的编译器之一。
Clang 是一个开源项目,在苹果公司的支持下,Clang 的开发得到了全球各地的开发者们的广泛参与和贡献。现在,Clang 成为了 LLVM 生态中的一个重要组成部分,被广泛地应用于多平台的编译器开发中。
Clang
and Clang++
“borrow” the header files from GCC
& G++
. It looks for the directories these usually live in and picks the latest one. If you’ve installed a later GCC without the corresponding G++, Clang++ gets confused and can’t find header files. In your instance, for example, if you’ve installed gcc 11 or 12.
You can use clang-10 -v -E
or clang++-10 -v -E
to get details on what versions of header files it’s trying to use.
安装g++-12解决
github/tools
目录下有许多实用工具
llvm-as
:把LLVM IR从人类能看懂的文本格式汇编成二进制格式。注意:此处得到的不是目标平台的机器码。llvm-dis
:llvm-as的逆过程,即反汇编。 不过这里的反汇编的对象是LLVM IR的二进制格式,而不是机器码。opt
:优化LLVM IR。输出新的LLVM IR。llc
:把LLVM IR编译成汇编码。需要用as进一步得到机器码。lli
:解释执行LLVM IR。暂无
暂无
文章部分内容来自ChatGPT-3.5,暂时没有校验其可靠性(看上去貌似说得通)。
关于X86 与 arm的寄存器的区别写在了arm那篇下
In x86 terminology/documentation, a “word” is 16 bits
x86 word = 2 bytes
x86 dword = 4 bytes (double word)
x86 qword = 8 bytes (quad word)
x86 double-quad or xmmword = 16 bytes, e.g. movdqa xmm0, [rdi].
https://en.wikipedia.org/wiki/X86_instruction_listings
https://www.felixcloutier.com/x86/
https://officedaytime.com/simd512e/
1 | SHR # Shift right (unsigned shift right) |
1 | lea -0xc(%ebp),%eax |
1 | // x is %rdi, result is %rax 就是计算地址,没有寻址操作 |
Call 地址
:返回地址入栈(等价于“Push %eip,mov 地址,%eip
”;注意eip指向下一条尚未执行的指令)ret
:从栈中弹出地址,并跳到那个地址(pop %eip
)leave
:使栈做好返回准备,等价于
1 | mov %ebp,%esp |
1 | cmpl $0x5,$0x1 |
X86 不像 ARM有专门的ldr
, str
指令。是通过mov实现的
movswl (%rdi), %eax
sign-extending load from word (w) to dword (l). Intel movsx eax, word [rdi]
https://docs.oracle.com/cd/E36784_01/html/E36859/gntbd.html
1 | vxorpd XORPD |
1 | test al, al |
The test
instruction performs a logical and of the two operands and sets the CPU flags register according to the result (which is not stored anywhere). If al
is zero, the anded result is zero and that sets the Z flag. If al
is nonzero, it clears the Z flag. (Other flags, such as Carry, oVerflow, Sign, Parity, etc. are affected too, but this code has no instruction testing them.)
The jne
instruction alters EIP if the Z flag is not set. There is another mnemonic for the same operation called jnz
.
1 | test %eax,%eax |
注意 cmp
不等于 test
The TEST
operation sets the flags CF
and OF
to zero.
The SF
is set to the MSB(most significant bit) of the result of the AND
.
If the result of the AND
is 0, the ZF
is set to 1, otherwise set to 0.
AT&T syntax jmpq *0x402390(,%rax,8)
into INTEL-syntax: jmp [RAX*8 + 0x402390]
.
JUMP IF ABOVE
AND JUMP IF GREATER
ja
jumps if CF = 0
and ZF = 0
(unsigned Above: no carry and not equal)
jg
jumps if SF = OF
and ZF = 0
(signed Greater, excluding equal)
cmp
performs a sub
(but does not keep the result).
cmp eax, ebx
Let’s do the same by hand:
1 | reg hex value binary value |
The flags are set as follows:
1 | OF (overflow) : did bit 31 change -> no |
Carry Flag is a flag set when:
a) two unsigned numbers were added and the result is larger than “capacity” of register where it is saved.
Ex: we wanna add two 8 bit numbers and save result in 8 bit register. In your example: 255 + 9 = 264 which is more that 8 bit register can store. So the value “8” will be saved there (264 & 255 = 8) and CF flag will be set.
b) two unsigned numbers were subtracted and we subtracted the bigger one from the smaller one.
Ex: 1-2 will give you 255 in result and CF flag will be set.
Auxiliary Flag is used as CF but when working with BCD. So AF will be set when we have overflow or underflow on in BCD calculations. For example: considering 8 bit ALU unit, Auxiliary flag is set when there is carry from 3rd bit to 4th bit i.e. carry from lower nibble to higher nibble. (Wiki link)
Overflow Flag is used as CF but when we work on signed numbers.
Ex we wanna add two 8 bit signed numbers: 127 + 2. the result is 129 but it is too much for 8bit signed number, so OF will be set.
Similar when the result is too small like -128 - 1 = -129 which is out of scope for 8 bit signed numbers.
Positive or negative
The CPU does not know (or care) whether a number is positive or negative. The only person who knows is you. If you test SF and OF, then you treat the number as signed. If you only test CF then you treat the number as unsigned.
In order to help you the processor keeps track of all flags at once. You decide which flags to test and by doing so, you decide how to interpret the numbers.
The computer makes use of binary multiplication(AND), followed by bit shift (in the direction in which the multiplication proceeds), followed by binary addition(OR).
1 | 1100100 |
for more:
How computer multiplies 2 numbers?
And:
Binary multiplier - Wikipedia
DB, DW, and DD can be used to declare one, two, and four byte data locations,
1 | # 基本例子 |
数组的声明,The DUP directive tells the assembler to duplicate an expression a given number of times. For example, 4 DUP(2) is equivalent to 2, 2, 2, 2.
1 |
|
32位X86机器寻址支持
1 | # right |
1 | mov BYTE PTR [ebx], 2 ; Move 2 into the single byte at the address stored in EBX. |
这和汇编器语法有关:
For instructions with two operands, the first (lefthand) operand is the source operand, and the second (righthand) operand is the destination operand (that is, source->destination).
1 | mov eax, ebx — copy the value in ebx into eax |
AT&T Syntax is an assembly syntax used in UNIX environments, that originates from AT&T Bell Labs. It is descended from the MIPS assembly syntax. (AT&T, American Telephone & Telegraph)
AT&T Syntax is an assembly syntax used mostly in UNIX environments or by tools like gcc that originated in that environment.
语法特点:https://stackoverflow.com/tags/att/info
需要注意的:
%
, and immediates are prefixed with $
sub $24, %rsp
reserves 24 bytes on the stack.b/w/l/q
suffix on the mnemonicaddb $1, byte_table(%rdi)
increment a byte in a static table.imul $13, 16(%rdi, %rcx, 4), %eax
32-bit load from rdi + rcx<<2 + 16
, multiply that by 13, put the result in %eax
. Intel imul eax, [16 + rdi + rcx*4], 13
.movswl (%rdi), %eax
sign-extending load from word (w) to dword (l). Intel movsx eax, word [rdi]
.The Intel assembler(icc,icpc我猜) uses the opposite order (destination<-source) for operands.
语法特点: https://stackoverflow.com/tags/intel-syntax/info
1 | beq rs1, rs2, Label #RISC-V |
但是这个语法不是很重要,因为decompiler有选项控制语法
objdump
has -Mintel
flag, gdb
has set disassembly-flavor intel
option.
gcc -masm=intel -S
or objdump -drwC -Mintel
.
暂无
暂无