From 91a32fe4a44406f8555c9be19c80fc38bb0116aa Mon Sep 17 00:00:00 2001 From: Kevin Enderby Date: Mon, 6 Feb 2017 18:43:18 +0000 Subject: [PATCH] Fix a bug in llvm-obdump(1) with the -macho and -disassemble options which caused it to not disassemble the bytes a the start of the section if the section had symbols and the first symbol was not at the start of the section. rdar://30143243 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@294212 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../X86/Inputs/nofirst-symbol.macho-x86_64 | Bin 0 -> 336 bytes .../X86/macho-nofirst-symbol-disassembly.test | 8 +++++ tools/llvm-objdump/MachODump.cpp | 29 +++++++++++++++++- 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 test/tools/llvm-objdump/X86/Inputs/nofirst-symbol.macho-x86_64 create mode 100644 test/tools/llvm-objdump/X86/macho-nofirst-symbol-disassembly.test diff --git a/test/tools/llvm-objdump/X86/Inputs/nofirst-symbol.macho-x86_64 b/test/tools/llvm-objdump/X86/Inputs/nofirst-symbol.macho-x86_64 new file mode 100644 index 0000000000000000000000000000000000000000..4d1ef25e67695697b5cffb2015596a976b55a325 GIT binary patch literal 336 zcmX^A>+L^w1_nlE1|R{%AR5d7(Lf*x#52Gw>;wx`SOKIN2w;4mnG7Hd6=sN!FG;N^ z0jYq1`1lalh!7YLLBsUJ%!A9YFf_o~Kn4>KO8~J7&;k(K1xWJ%X>K460AdgTiGwf* nz-S1Mcyhww35VH$%K3oi!~6oGL43HG@kxnA4Do6C`3wvIXW$Uk literal 0 HcmV?d00001 diff --git a/test/tools/llvm-objdump/X86/macho-nofirst-symbol-disassembly.test b/test/tools/llvm-objdump/X86/macho-nofirst-symbol-disassembly.test new file mode 100644 index 00000000000..98964ac8047 --- /dev/null +++ b/test/tools/llvm-objdump/X86/macho-nofirst-symbol-disassembly.test @@ -0,0 +1,8 @@ +// RUN: llvm-objdump -d -m %p/Inputs/nofirst-symbol.macho-x86_64 | FileCheck %s + +CHECK: 0: 90 nop +CHECK: _foo: +CHECK: 1: c3 retq +CHECK: _bar: +CHECK: 2: 90 nop +CHECK: 3: c3 retq diff --git a/tools/llvm-objdump/MachODump.cpp b/tools/llvm-objdump/MachODump.cpp index e4ddf5457fb..b70e8256625 100644 --- a/tools/llvm-objdump/MachODump.cpp +++ b/tools/llvm-objdump/MachODump.cpp @@ -6602,6 +6602,12 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, if (Bytes.size() == 0) return; + // If the section has symbols but no symbol at the start of the section + // these are used to make sure the bytes before the first symbol are + // disassembled. + bool FirstSymbol = true; + bool FirstSymbolAtSectionStart = true; + // Disassemble symbol by symbol. for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) { Expected SymNameOrErr = Symbols[SymIdx].getName(); @@ -6691,11 +6697,29 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, // (i.e. we're not targeting M-class) and the function is Thumb. bool UseThumbTarget = IsThumb && ThumbTarget; - outs() << SymName << ":\n"; + // If we are not specifying a symbol to start disassembly with and this + // is the first symbol in the section but not at the start of the section + // then move the disassembly index to the start of the section and + // don't print the symbol name just yet. This is so the bytes before the + // first symbol are disassembled. + uint64_t SymbolStart = Start; + if (DisSymName.empty() && FirstSymbol && Start != 0) { + FirstSymbolAtSectionStart = false; + Start = 0; + } + else + outs() << SymName << ":\n"; + DILineInfo lastLine; for (uint64_t Index = Start; Index < End; Index += Size) { MCInst Inst; + // If this is the first symbol in the section and it was not at the + // start of the section, see if we are at its Index now and if so print + // the symbol name. + if (FirstSymbol && !FirstSymbolAtSectionStart && Index == SymbolStart) + outs() << SymName << ":\n"; + uint64_t PC = SectAddress + Index; if (!NoLeadingAddr) { if (FullLeadingAddr) { @@ -6788,6 +6812,9 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, } } } + // Now that we are done disassembled the first symbol set the bool that + // were doing this to false. + FirstSymbol = false; } if (!symbolTableWorked) { // Reading the symbol table didn't work, disassemble the whole section. -- 2.50.1