I want to figure out at runtime whether a particular address is in the .bss segment or the .code segment or what. Are there symbols of some sort called start_bss or BSS_START or anything of that sort that I can use to figure out what segment the address is in?
thanks
I believe it depends on which linker you use. As an example, GNU’s linker provides some symbols by default, such as __bss_start and _end. Although it’s easily configurable by providing different linker scripts. What linker do you use?
I suspect that your answer will be rather dependent on what compiler/linker you use. I don’t recall seeing anything standardized, but the POSIX C header list is admittedly larger than one would think and I don’t feign to have memorised it.
Not knowing what you’re doing such that you would need to know this, you might want to look at this site to see if it doesn’t suggest some techniques to prevent memory issues.
The Microsoft Visual Studio linker. I spent a long time poking around in the documentation on msdn.com and couldn’t find anything, but I’ve found that documentation to be woefully badly organized.
I’d probably just recommend having Visual Studio spit out the map file when you compile, and then read that in and parse it. You might need to figure out the offset that the addresses have been converted to when loaded into memory, but that should just be a matter of, for instance, looking at the position of main() in the map file and the address location in runtime and storing the difference.
Other things to try are:
- Use an assembler block and check if MASM has any way to access the address of the segments, and pass the results back.
void* bss_loc;
__asm {
mov bss_loc, @DATA
}
or something like that.
- See if the pragmas like bss_seg(), when you do a push command to a label, whether you can access that label outside of the pragma. (I suspect not, but who knows.)