“Function-level linking” (i.e. COMDAT generation) in MASM assembly?

佐手、 提交于 2019-12-07 12:56:05

问题


Is there any way to make MASM generate COMDATs for functions, so that unused functions are removed by the linker?

(i.e. I'm looking for the equivalents of /Gy for MASM.)


回答1:


Not straightforward, but doable; discussed here and here.

The first step involves putting each function into a separate segment with names like .text$a, .text$b, etc. This way, the assembler won't unite them into a single .text section, but the linker eventually will; there's a special rule in Microsoft linkers regarding the stuff past the $ character in the section name. The assembler will emit an .obj file with multiple code sections. I've tried that, I can confirm that it does. At least one flavor of MASM does. :)

Then they suggest running an utility over an object file that will mark your sections as COMDATs. The said utility seems to be lost to time and bit decay, but its action can be roughly deduced. It reads and parses a COFF .obj file, goes through sections and slaps a COMDAT flag on all .text sections. I assume it's just a flag; could be more. As a first step to its recreation, I'd suggest compiling a C file with /Gy then without, and comparing the two .obj files via some low-level PE/COFF browser. I didn't go this far, since my scenario was rather different.



来源:https://stackoverflow.com/questions/9423576/function-level-linking-i-e-comdat-generation-in-masm-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!