Skip to content
  • Tamar Christina's avatar
    Updated PE linker, section alignment and cleanup. · 58407343
    Tamar Christina authored
    Summary:
    This patch is to address a couple of short comings of the PE linker.
    
    The first thing it does is properly honor section alignments, so SSE code
    will work reliably.
    
    While doing this I've also changed how it reads and stores ObjectFile
    information. Previously the entire object file was read in and treated
    as one blob, including headers, symbol tables etc.
    
    Now the ObjectFile is read in but stored in chunks, tables go into a temporary
    info struct and code/data into a new private heap. This allows me to free all
    meta data once we're done relocating. Which means we can reclaim this memory.
    
    As I've mentioned above I've also moved from using VirtualAlloc to HeapAlloc.
    The reason is VirtualAlloc is meant to be used for more low level memory
    allocation, it's very fast because it can only allocate whole blocks,
    (64k) by default, and the memory must be paged (4k) aligned.
    
    So when you ask for e.g. 30k of memory, you're given a whole block where 34k
    will be wasted memory. Nothing else can ever access that untill you free the 30k.
    
    One downside of HeapAlloc is that you're not in control of how the heap grows,
    and heap memory is always committed. So it's harder to tell how much we're
    actually using now.
    
    Another big upside of splitting off the ObjectCode tables to info structs
    is that I can adjust them, so that later addressings can just use array
    subscripts to index into them. This simplifies the code a lot and a lot of
    complicated casts and indexing can be removed. Leaving less and more simple
    code.
    
    This patch doesn't fix the memprotection but it doesn't regress it either.
    It does however make the next changes smaller and fixes the alignments.
    
    Test Plan: ./validate , new test T13617
    
    Reviewers: bgamari, erikd, simonmar, hvr, angerman
    
    Reviewed By: angerman
    
    Subscribers: nickkuk, carter, RyanGlScott, rwbarton, thomie
    
    GHC Trac Issues: #13617
    
    Differential Revision: https://phabricator.haskell.org/D3915
    58407343