22 ms·
Yep, I'm not even a hobbyist in this area, and immediately wondered if there was an alignment change because of it. With that said, it sounds like at least wit
by codezero 6y ago
Yep, I'm not even a hobbyist in this area, and immediately wondered if there was an alignment change because of it.
With that said, it sounds like at least with GCC, a global constant would go in the TEXT section of the binary and I can't think of why that would affect performance, especially since the variable was seemingly unused. Neat, I hope someone finds the thread to pull here :)
- kijiki 6y ago> ...a global constant would go in the TEXT section of the binary... Wouldn't it go into the .rodata section, not .text?
- codezero 6y agoI'm not sure, I am parroting what I thought I read on SO - so you are much more likely to be right, sorry about that!
- tom_mellior 6y ago> it sounds like at least with GCC, a global constant would go in the TEXT section of the binary I don't know why you think it sounds like that, but GCC (on Linux) puts global constants into the .rodata section (read-only data) and other global variables into the .data section. > I can't think of why that would affect performance, especially since the variable was seemingly unused. I'm guessing data alignment and corresponding changes in cache hits and misses. But I wonder if there's a scenario where this change changes the offsets of other variables from the base of the data section, which changes the encodings and therefore the sizes of the instructions accessing those variables, which overall changes code size and might cause code alignment issues. Probably too far-fetched.
- vivekseth 6y agoOn macOS, "gcc" puts strings that don't include a null byte in the TEXT section. I put quotes around gcc, since it seems that by default macOS aliases gcc to clang. You can run the program I built here to see for your self: https://github.com/vivekseth/blog-posts/tree/master/Jump-Address-Hacking-to-Execute-Code-Stored-as-a-String https://github.com/vivekseth/blog-posts/tree/master/Jump-Add... Since the string in the TEXT section, we can actually execute it as if it were code! After you build the program you can run `otool -t ./a.out` to verify that the string `execString` is indeed in the TEXT section.
- tom_mellior 6y agoInteresting. And if you put a null byte at the end of that character array, it would put it in a different section? I don't have access to MacOS, but I would be interested in seeing the output of clang -S on that code (with and without a null byte), maybe you could add it to the repository?
- vivekseth 6y agoIf I'm remembering correctly (this was from 3 years ago), the string was moved to a different section if it had a null byte in the middle of the string. That's why coming up with the assembly I used was kinda tricky. Here's the output as-is: https://gist.github.com/vivekseth/20f319d2a9978af57d926b649adc0bde https://gist.github.com/vivekseth/20f319d2a9978af57d926b649a... Here's the output with a null byte in the middle: https://gist.github.com/vivekseth/fc50319aaac24588bcf568209bf6df8e https://gist.github.com/vivekseth/fc50319aaac24588bcf568209b... From what I can tell, it seems like both strings are in the TEXT section now. Maybe something changed, or I'm remembering incorrectly.
- codezero 6y agoThanks for the correction, hopefully my statement that I'm hardly a hobbyist makes it clear I'm not super sure :) I guess I don't understand enough about how this variable sitting in memory might affect hit/miss/alignment especially if it's not used in the program as it's running. Would it be that some variable that is used is pushed off of a page in memory or something by the unused variable, so access it is slower? I guess that makes sense.