As a programming exercise, I am writing a mark-and-sweep garbage collector in C. I wish to scan the data segment (globals, etc.) for pointers to allocated memory, but I don't know how to get the range of the addresses of this segment. How could I do this?
相关问题
- Multiple sockets for clients to connect to
- What uses more memory in c++? An 2 ints or 2 funct
- What is the best way to do a search in a large fil
- C# GC not freeing memory [duplicate]
- glDrawElements only draws half a quad
The bounds for text (program code) and data for linux (and other unixes):
Where those symbols come from: Where are the symbols etext ,edata and end defined?
For iOS you can use this solution. It shows how to find the text segment range but you can easily change it to find any segment you like.
If you're working on Windows, then there are Windows API that would help you.
Define ImageSectionInfo as,
Here's a complete, minimal WIN32 console program you can run in Visual Studio that demonstrates the use of the Windows API:
This page may be helpful if you're interested in additional uses of the DbgHelp library.
You can read the PE image format here, to know it in details. Once you understand the PE format, you'll be able to work with the above code, and can even modify it to meet your need.
Peering Inside the PE: A Tour of the Win32 Portable Executable File Format
An In-Depth Look into the Win32 Portable Executable File Format, Part 1
An In-Depth Look into the Win32 Portable Executable File Format, Part 2
IMAGE_SECTION_HEADER Structure
ImageNtHeader Function
IMAGE_NT_HEADERS Structure
I think this would help you to great extent, and the rest you can research yourself :-)
By the way, you can also see this thread, as all of these are somehow related to this:
Scenario: Global variables in DLL which is used by Multi-threaded Application
Load the file that the executable came from and parse the PE headers, for Win32. I've no idea about on other OSes. Remember that if your program consists of multiple files (e.g. DLLs) you may have multiple data segments.
Since you'll probably have to make your garbage collector the environment in which the program runs, you can get it from the elf file directly.