Skip to content

[lldb] Correctly resolve (discontinuous) function offsets when disassembling #126925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions lldb/source/Plugins/Disassembler/LLVMC/DisassemblerLLVMC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "lldb/Core/Address.h"
#include "lldb/Core/Module.h"
#include "lldb/Symbol/Function.h"
#include "lldb/Symbol/SymbolContext.h"
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Target/Process.h"
Expand Down Expand Up @@ -1806,10 +1807,13 @@ const char *DisassemblerLLVMC::SymbolLookup(uint64_t value, uint64_t *type_ptr,
bool format_omitting_current_func_name = false;
if (sym_ctx.symbol || sym_ctx.function) {
AddressRange range;
if (sym_ctx.GetAddressRange(resolve_scope, 0, false, range) &&
range.GetBaseAddress().IsValid() &&
range.ContainsLoadAddress(value_so_addr, target)) {
format_omitting_current_func_name = true;
for (uint32_t idx = 0;
sym_ctx.GetAddressRange(resolve_scope, idx, false, range);
++idx) {
if (range.ContainsLoadAddress(value_so_addr, target)) {
format_omitting_current_func_name = true;
break;
}
}
}

Expand Down
14 changes: 9 additions & 5 deletions lldb/source/Symbol/SymbolContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,19 @@ bool SymbolContext::DumpStopContext(

if (addr_t file_addr = addr.GetFileAddress();
file_addr != LLDB_INVALID_ADDRESS) {
const addr_t function_offset =
file_addr - function->GetAddress().GetFileAddress();
// Avoiding signed arithmetic due to UB in -INT_MAX.
const char sign =
file_addr >= function->GetAddress().GetFileAddress() ? '+' : '-';
addr_t offset = file_addr - function->GetAddress().GetFileAddress();
if (sign == '-')
offset = -offset;
if (!show_function_name) {
// Print +offset even if offset is 0
dumped_something = true;
s->Printf("+%" PRIu64 ">", function_offset);
} else if (function_offset) {
s->Format("{0}{1}>", sign, offset);
} else if (offset) {
dumped_something = true;
s->Printf(" + %" PRIu64, function_offset);
s->Format(" {0} {1}", sign, offset);
}
}

Expand Down
13 changes: 8 additions & 5 deletions lldb/test/Shell/Commands/command-disassemble.s
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
# CHECK-NEXT: command-disassemble.s.tmp[0x2044] <+0>: int $0x32
# CHECK-NEXT: warning: Not disassembling a function because it is very large [0x0000000000002046-0x0000000000004046). To disassemble specify an instruction count limit, start/stop addresses or use the --force option.
# CHECK-NEXT: (lldb) disassemble --name case3
# CHECK-NEXT: error: Not disassembling a function because it is very large [0x0000000000006046-0x0000000000007046)[0x0000000000009046-0x000000000000a046). To disassemble specify an instruction count limit, start/stop addresses or use the --force option.
# CHECK-NEXT: error: Not disassembling a function because it is very large [0x0000000000006046-0x0000000000007046)[0x0000000000009046-0x000000000000a050). To disassemble specify an instruction count limit, start/stop addresses or use the --force option.
# CHECK-NEXT: Not disassembling a function because it is very large [0x0000000000004046-0x0000000000006046). To disassemble specify an instruction count limit, start/stop addresses or use the --force option.
# CHECK-NEXT: (lldb) disassemble --name case3 --count 3
# CHECK-NEXT: command-disassemble.s.tmp`n2::case3:
Expand All @@ -93,9 +93,10 @@
# CHECK-NEXT: command-disassemble.s.tmp[0x604a] <-12284>: int $0x2a
# CHECK-EMPTY:
# CHECK-NEXT: command-disassemble.s.tmp`n2::case3:
# CHECK-NEXT: command-disassemble.s.tmp[0x9046] <+0>: int $0x2a
# CHECK-NEXT: command-disassemble.s.tmp[0x9048] <+2>: int $0x2a
# CHECK-NEXT: command-disassemble.s.tmp[0x904a] <+4>: int $0x2a
# CHECK-NEXT: command-disassemble.s.tmp[0x9046] <+0>: jmp 0x6046 ; <-12288>
## FIXME: This should resolve to `middle_of_case3`
# CHECK-NEXT: command-disassemble.s.tmp[0x904b] <+5>: jmp 0x7046 ; n2::case3 - 8192
# CHECK-NEXT: command-disassemble.s.tmp[0x9050] <+10>: int $0x2a
# CHECK-EMPTY:
# CHECK-NEXT: command-disassemble.s.tmp`n1::case3:
# CHECK-NEXT: command-disassemble.s.tmp[0x4046] <+0>: int $0x2a
Expand Down Expand Up @@ -171,12 +172,14 @@ _ZN2n15case3Ev:
.endr
.L_ZN2n25case3Ev.__part.1_end:

.Lpadding:
middle_of_case3:
.rept 0x1000
int $42
.endr

_ZN2n25case3Ev:
jmp .L_ZN2n25case3Ev.__part.1
jmp middle_of_case3
.rept 0x800
int $42
.endr
Expand Down