Closed HanabishiRecca closed 11 months ago
A really broken example is btop
.
Linked with default GCC ld
:
$ elfx86exts btop
File format and CPU architecture: Elf, X86_64
MODE64 (call)
AVX (vzeroupper)
CMOV (cmova)
NOVLX (vpxor)
BWI (vmovdqu8)
BMI (tzcnt)
BMI2 (shrx)
AVX2 (vinserti128)
AVX512 (vmovdqa64)
VLX (vpbroadcastq)
SSE2 (pause)
DQI (kshiftrd)
Instruction set extensions used: AVX, AVX2, AVX512, BMI, BMI2, BWI, CMOV, DQI, MODE64, NOVLX, SSE2, VLX
CPU Generation: Unknown
Linked with mold
:
$ elfx86exts btop
File format and CPU architecture: Elf, X86_64
MODE64 (ret)
Instruction set extensions used: MODE64
CPU Generation: Intel Core
Nothing is detected.
Strange! Indeed, mold must be constructing its executables in an unusual way.
At this point, I only have the capacity to maintain this package passively, so I'm afraid that I can't look into this problem myself. But I am happy to accept pull requests and put out new releases. If you look at the source code, this program is fairly simple, and I suspect that this problem might be fixable simply by teaching the program to consider a few additional types of executable-file sections.
Well, I started with simply printing sections and their instruction counts.
ld
$ ./elfx86exts ./elfx86exts
File format and CPU architecture: Elf, X86_64
.init: 8
MODE64 (call)
.plt: 42
.plt.got: 8
.text: 793504
BMI (tzcnt)
CMOV (cmovae)
SSE1 (movups)
SSE2 (pause)
AVX (vmovdqu)
NOVLX (vmovaps)
AVX2 (vpor)
.fini: 4
Instruction set extensions used: AVX, AVX2, BMI, CMOV, MODE64, NOVLX, SSE1, SSE2
CPU Generation: Unknown
mold
$ ./elfx86exts ./elfx86exts
File format and CPU architecture: Elf, X86_64
.plt: 90
MODE64 (push)
.plt.got: 12
.fini: 4
.init: 8
.text: 111
CMOV (cmovs)
Instruction set extensions used: CMOV, MODE64
CPU Generation: Intel Core
Instruction count on .text
is obviously very underestimated for binary produced by mold
.
BUT on the other hand reported sect.size()
is correct. So it has troubles not to find the section, but to disassembly instructions from it.
Inspecting the problem deeper, I found a possible reason.
I posted a detailed explanation here: https://github.com/rui314/mold/issues/1133#issuecomment-1773365655
Hey, I actually managed to develop a fix! I will make a PR.
elfx86exts
has problems reading binaries linked withmold
.Somehow it is unable to detect all or almost all instruction sets. This happens with all compilers and languages, but has some correlation with compiler flags used.
E.g. lets examine
elfx86exts
itself linked with the default Rust linker outputs:Seems legit and indeed I can find say
vpcmpeqb
inobjdump
:But when linked with
mold
(i.e.RUSTFLAGS='-C link-arg=-fuse-ld=mold' cargo build --release
):AVX is not detected, despite I still can find the same piece of code in
objdump
:I guess
mold
does something unusual, which somehow confuseselfx86exts
.