microsoft / mimalloc

mimalloc is a compact general purpose allocator with excellent performance.
MIT License
10.44k stars 842 forks source link

Is it ok that `ld-linux-x86-64.so.2` `Scrt1.o` `crti.o` and `crtbeginS.o` is located before `mimalloc.o` in the linker commandline when static override malloc? #902

Closed LXYan2333 closed 4 months ago

LXYan2333 commented 4 months ago

Hello:

I have a linux cmake project and I want to static override the malloc. After reading the readme I wrote this in my cmake project:

set(CMAKE_EXE_LINKER_FLAGS "${libmimalloc_BINARY_DIR}/mimalloc.o  ${CMAKE_EXE_LINKER_FLAGS} -v")

the -v flag tells the linker to print everything about linking. When I check the output I found the /lib64/ld-linux-x86-64.so.2 Scrt1.o crti.o and crtbeginS.o is located before mimalloc.o:

short version:

 /usr/lib/gcc/x86_64-linux-gnu/12/collect2 
# ……
/lib64/ld-linux-x86-64.so.2 
-pie -o my_binary_targe
Scrt1.o crti.o 
crtbeginS.o 
mimalloc.o  
# ……

full version:

 /usr/lib/gcc/x86_64-linux-gnu/12/collect2 -plugin /usr/lib/gcc/x86_64-linux-gnu/12/liblto_plugin.so -plugin-opt=/usr/lib/gcc/x86_64-linux-gnu/12/lto-wrapper -plugin-opt=-fresolution=/tmp/ccMdOkII.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -flto=auto --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=gnu --as-needed -dynamic-linker /lib64/ld-linux-x86-64.so.2 -pie -o my_binary_target /usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/Scrt1.o /usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/12/crtbeginS.o -L/usr/lib/gcc/x86_64-linux-gnu/12 -L/usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/12/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/12/../../.. /home/lxyan/Code/xscf/build/_deps/mimalloc-build/mimalloc.o 

the readme says

link it (mimalloc.o) as the first object file

I wonder if this is enough? /lib64/ld-linux-x86-64.so.2 Scrt1.o crti.o and crtbeginS.o seems related to setup C runtime and I don't know how to add linker flags before them……

LXYan2333 commented 4 months ago

https://github.com/microsoft/mimalloc/issues/532#issuecomment-1312569591

this answer my question.