aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Henderson <james.henderson@sony.com>2020-04-07 11:51:26 +0100
committerJames Henderson <james.henderson@sony.com>2020-04-08 10:57:12 +0100
commitabd335a339cb2c5aaf30463ff8451f1eb6e223c7 (patch)
treed87b18f7ede5a06ebc1b7d28712bd45c37d593f8
parent01d97a35493a8a306bfaa3ceb3e6fa49b05dea89 (diff)
downloadllvm-abd335a339cb2c5aaf30463ff8451f1eb6e223c7.zip
llvm-abd335a339cb2c5aaf30463ff8451f1eb6e223c7.tar.gz
llvm-abd335a339cb2c5aaf30463ff8451f1eb6e223c7.tar.bz2
[llvm-objdump] Fix unstable disassembly output for sections with same address
When two sections shared the same address, the disassembly code was using pointer values when sorting (see the SectionRef less than operator). Since those values aren't guaranteed to have a specific order, this meant the disassembly code would sometimes change which section to pick when finding symbols targeted by calls in fully linked objects. This change fixes the non-determinism, so that the same section is always picked. This might have a negative impact in that now a section without any symbol might be picked over a section with symbols, but this will be addressed in a later commit. Fixes https://bugs.llvm.org/show_bug.cgi?id=45411. Reviewed by: grimar, MaskRay Differential Revision: https://reviews.llvm.org/D77640
-rw-r--r--llvm/test/tools/llvm-objdump/X86/disassemble-same-section-addr.test39
-rw-r--r--llvm/tools/llvm-objdump/llvm-objdump.cpp2
2 files changed, 40 insertions, 1 deletions
diff --git a/llvm/test/tools/llvm-objdump/X86/disassemble-same-section-addr.test b/llvm/test/tools/llvm-objdump/X86/disassemble-same-section-addr.test
new file mode 100644
index 0000000..cbb7788
--- /dev/null
+++ b/llvm/test/tools/llvm-objdump/X86/disassemble-same-section-addr.test
@@ -0,0 +1,39 @@
+## This test shows that llvm-objdump can handle sections with the same address
+## when symbols in those sections are referenced. In the past, the section
+## picked was non-deterministic, resulting in different symbols being found for
+## the section. The test uses YAML for the input, as we need a fully linked ELF
+## to reproduce the original failure.
+
+# RUN: yaml2obj %s -o %t1 -D SECTION=.second
+# RUN: llvm-objdump -d %t1 | FileCheck %s
+# RUN: yaml2obj %s -o %t2 -D SECTION=.first
+## FIXME: this case should print "<target>" too.
+# RUN: llvm-objdump -d %t2 | FileCheck %s --check-prefix=FAIL
+
+# CHECK: callq 0x5 <target>
+# FAIL: callq 0x5{{$}}
+
+--- !ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_EXEC
+ Machine: EM_X86_64
+Sections:
+ - Name: .caller
+ Type: SHT_PROGBITS
+ Flags: [SHF_ALLOC, SHF_EXECINSTR]
+ Address: 0x0
+ Content: e800000000 # Call instruction to next address.
+ - Name: .first
+ Type: SHT_PROGBITS
+ Flags: [SHF_ALLOC, SHF_EXECINSTR]
+ Address: 0x5
+ - Name: .second
+ Type: SHT_PROGBITS
+ Flags: [SHF_ALLOC, SHF_EXECINSTR]
+ Address: 0x5
+Symbols:
+ - Name: target
+ Section: [[SECTION]]
+ Value: 0x5
diff --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp
index 06a4978..2f9c6f1 100644
--- a/llvm/tools/llvm-objdump/llvm-objdump.cpp
+++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp
@@ -1249,7 +1249,7 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj,
std::vector<std::pair<uint64_t, SectionRef>> SectionAddresses;
for (SectionRef Sec : Obj->sections())
SectionAddresses.emplace_back(Sec.getAddress(), Sec);
- stable_sort(SectionAddresses);
+ llvm::stable_sort(SectionAddresses, llvm::less_first());
// Linked executables (.exe and .dll files) typically don't include a real
// symbol table but they might contain an export table.