Index: head/contrib/elftoolchain/elfcopy/elfcopy.h =================================================================== --- head/contrib/elftoolchain/elfcopy/elfcopy.h +++ head/contrib/elftoolchain/elfcopy/elfcopy.h @@ -138,6 +138,8 @@ TAILQ_ENTRY(section) sec_list; /* next section */ }; +TAILQ_HEAD(sectionlist, section); + /* Internal data structure for segments. */ struct segment { uint64_t vaddr; /* virtual addr (VMA) */ Index: head/contrib/elftoolchain/elfcopy/sections.c =================================================================== --- head/contrib/elftoolchain/elfcopy/sections.c +++ head/contrib/elftoolchain/elfcopy/sections.c @@ -314,18 +314,18 @@ { struct section *s; - if (!tail) { + if (tail || TAILQ_EMPTY(&ecp->v_sec) || + TAILQ_LAST(&ecp->v_sec, sectionlist)->off <= sec->off) { + TAILQ_INSERT_TAIL(&ecp->v_sec, sec, sec_list); + } else { TAILQ_FOREACH(s, &ecp->v_sec, sec_list) { if (sec->off < s->off) { TAILQ_INSERT_BEFORE(s, sec, sec_list); - goto inc_nos; + break; } } } - TAILQ_INSERT_TAIL(&ecp->v_sec, sec, sec_list); - -inc_nos: if (sec->pseudo == 0) ecp->nos++; }