From 6628ac006d166a7e373846538a6097a3150946e9 Mon Sep 17 00:00:00 2001 From: Sandra Loosemore Date: Wed, 15 Dec 2021 16:05:41 -0700 Subject: [PATCH] Adjust compare_link_order for unstable qsort In a cross toolchain for nios2-elf target and x86_64-w64-mingw32 host using binutils 2.37, we observed a failure that didn't show up on x86_64-linux-gnu host: testcase pr25490-5.s was failing with C:\path\to\nios2-elf-ld.exe: looping in map_segments FAIL: __patchable_function_entries section 5 * ldelfgen.c (compare_link_order): Don't use section id in sorting. Keep original ordering instead. Update comments. --- ld/ldelfgen.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/ld/ldelfgen.c b/ld/ldelfgen.c index c456d4751e1..d86515a4892 100644 --- a/ld/ldelfgen.c +++ b/ld/ldelfgen.c @@ -173,8 +173,9 @@ compare_link_order (const void *a, const void *b) if (! bfd_link_relocatable (&link_info)) { - /* The only way we should get matching LMAs is when - the first of the two sections has zero size. */ + /* The only way we should get matching LMAs is when the first of + the two sections has zero size, or asec and bsec are the + same section. */ if (asec->size < bsec->size) return -1; else if (asec->size > bsec->size) @@ -183,7 +184,7 @@ compare_link_order (const void *a, const void *b) /* If they are both zero size then they almost certainly have the same VMA and thus are not ordered with respect to each other. Test VMA - anyway, and fall back to id to make the result reproducible across + anyway, and fall back to idx to make the result reproducible across qsort implementations. */ apos = asec->output_section->vma + asec->output_offset; bpos = bsec->output_section->vma + bsec->output_offset; @@ -191,8 +192,8 @@ compare_link_order (const void *a, const void *b) return -1; else if (apos > bpos) return 1; - - return asec->id - bsec->id; + else + return ai->idx - bi->idx; } /* Rearrange sections with SHF_LINK_ORDER into the same order as their -- 2.30.2