![]() |
|
[C] C++ compiler flags for function implementation arrangement in object file - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C] C++ compiler flags for function implementation arrangement in object file (/showthread.php?tid=5981396) |
C++ compiler flags for function implementation arrangement in object file - 6577657534735 - Jun 20, 2019 If I have func1() func2() and func3() and I want the compiler to arrange the implementations of func1() before func2() and func2() before func3() in-memory for instruction cache locality reasons, does anyone know if any compilers have flags to do this? RE: C++ compiler flags for function implementation arrangement in object file - YouthInAsia - Jun 20, 2019 The compiler should automatically do this as far as I'm aware but it might just be unspecified behavior. I do remember though marking functions with the "noinline" attribute seemed to keep them in place. So try just giving each function a noinline attribute and see if that does the trick. It might just vary compiler to compiler, but I honestly doubt anything like that would ever be standard. So I tried this code Code #include <stdio.h>Their placement in memory regardless of which order they were called was still f1 first, f2 second f3 third Code 0x100000f48 <+8>: call 0x100000f20 ; f3 at main.cpp:9So that does seem to work but when I remove the noinline attribute I get the same result Code 0x100000f48 <+8>: call 0x100000f20 ; f3 at main.cpp:9Which means the code section should just get compiled in the order you wrote it like it did in my case. But I think if you want to be extra careful then yes place noinline attribute. Another thing that could do the trick is marking a function as volatile/const. RE: C++ compiler flags for function implementation arrangement in object file - 6577657534735 - Jun 20, 2019 Thanks, it's worrying that they could randomly swap order if you switched compiler versions or modified optimization flags or something, but I will look into whether this applies to me RE: C++ compiler flags for function implementation arrangement in object file - YouthInAsia - Jun 20, 2019 (Jun 20, 2019 09:35 AM)Abraluna Wrote: Thanks, it's worrying that they could randomly swap order if you switched compiler versions or modified optimization flags or something, but I will look into whether this applies to me Yes one things for certain, even with compiler optimisation turned on a function with noinline attribute will still be separated. RE: C++ compiler flags for function implementation arrangement in object file - dracx - Jun 20, 2019 This gretly varies from compiler to compiler, plus I take note of the object files (even just of your headers), if your worried bout their placement too, you should try and research more. And judging from your subject, I suggest disabling ASLR and other protection mechanisms that makes your stuff go dynamic. RE: C++ compiler flags for function implementation arrangement in object file - doxerhacker - Jun 20, 2019 other than forcing no inline you should also make sure incremental linking is disabled. |