This has to be reworked, and below is the approach. 1) add the segment type to the key 2) sort the segments 3) pass1 : create all the segments as whatever it comes up with 4) pass2: go and fix the PT_LOAD segment headers 5) emit segment header information http://llvm-reviews.chandlerc.com/D458