njn | a9104c0 | 2005-06-30 00:54:02 +0000 | [diff] [blame] | 1 | |
| 2 | ----------------------------------------------------------------------------- |
| 3 | Info about the relationship between Segments and SegInfos |
| 4 | ----------------------------------------------------------------------------- |
| 5 | |
| 6 | SegInfo is from the very original Valgrind code, and so it predates |
| 7 | Segments. It's poorly named now; its really just a container for all |
| 8 | the object file metadata (symbols, debug info, etc). |
| 9 | |
| 10 | Segments describe memory mapped into the address space, and so any |
| 11 | address-space chaging operation needs to update the Segment structure. |
| 12 | After the process is initalized, this means one of: |
| 13 | |
| 14 | * mmap |
| 15 | * munmap |
| 16 | * mprotect |
| 17 | * brk |
| 18 | * stack growth |
| 19 | |
| 20 | A piece of address space may or may not be mmaped from a file. |
| 21 | |
| 22 | A SegInfo specifically describes memory mmaped from an ELF object file. |
| 23 | Because a single ELF file may be mmaped with multiple Segments, multiple |
| 24 | Segments can point to one Seginfo. A SegInfo can relate to a memory |
| 25 | range which is not yet mmaped. For example, if the process mmaps the |
| 26 | first page of an ELF file (the one containing the header), a SegInfo |
| 27 | will be created for that ELF file's mappings, which will include memory |
| 28 | which will be later mmaped by the client's ELF loader. If a new mmap |
| 29 | appears in the address range of an existing SegInfo, it will have that |
| 30 | SegInfo attached to it, presumably because its part of a .so file. |
| 31 | Similarly, if a Segment gets split (by mprotect, for example), the two |
| 32 | pieces will still be associated with the same SegInfo. For this reason, |
| 33 | the address/length info in a SegInfo is not a duplicate of the Segment |
| 34 | address/length. |
| 35 | |
| 36 | This is complex for several reasons: |
| 37 | |
| 38 | 1. We assume that if a process is mmaping a file which contains an |
| 39 | ELF header, it intends to use it as an ELF object. If a program |
| 40 | which just mmaps ELF files but just uses it as raw data (copy, for |
| 41 | example), we still treat it as a shared-library opening. |
| 42 | 2. Even if it is being loaded as a shared library/other ELF object, |
| 43 | Valgrind doesn't control the mmaps. It just observes the mmaps |
| 44 | being generated by the client and has to cope. One of the reasons |
| 45 | that Valgrind has to make its own mmap of each .so for reading |
| 46 | symtab information is because the client won't necessary mmap the |
| 47 | right pieces, or do so in the wrong order for us. |
| 48 | |
| 49 | SegInfos are reference counted, and freed when no Segments point to them any |
| 50 | more. |
| 51 | |
| 52 | > Aha. So the range of a SegInfo will always be equal to or greater |
| 53 | > than the range of its parent Segment? Or can you eg. mmap a whole |
| 54 | > file plus some extra pages, and then the SegInfo won't cover the extra |
| 55 | > part of the range? |
| 56 | |
| 57 | That would be unusual, but possible. You could imagine ld generating an |
| 58 | ELF file via a mapping this way (which would probably upset Valgrind no |
| 59 | end). |