Issue 75539
Summary llvm-objdump prints out unrelated relocations when disassembling specific symbols
Labels new issue
Assignees
Reporter mppf
    I'm running into a problem that `llvm-objdump` has (at least as of LLVM 17) that does not seem to be present in GNU objdump. It seems to be printing out too many relocation entries when disassembling a specific symbol.

For an example, let's compile this C program to a .o file:

``` c
#include <math.h>
#include <stdio.h>

void foo(void);
void foo(void) {
  printf("test1\n");
 printf("test2\n");
  printf("test3\n");
 printf("test4\n");
  putchar(0);
}

int main(int argc, char** argv) {
  return sqrt(argc);
}
```

```
$ clang-17 -c hello.c
```

Now, we can ask for `llvm-objdump-17` to disassemble only `main`:

```
$ llvm-objdump-17 --disassemble-symbols=main hello.o

hello.o:	file format elf64-x86-64

Disassembly of section .text:

0000000000000050 <main>:
      50: 55 	pushq	%rbp
      51: 48 89 e5 	movq	%rsp, %rbp
      54: 48 83 ec 10                  	subq	$0x10, %rsp
      58: c7 45 fc 00 00 00 00         	movl	$0x0, -0x4(%rbp)
 5f: 89 7d f8                     	movl	%edi, -0x8(%rbp)
      62: 48 89 75 f0                  	movq	%rsi, -0x10(%rbp)
      66: f2 0f 2a 45 f8               	cvtsi2sdl	-0x8(%rbp), %xmm0
      6b: e8 00 00 00 00 	callq	0x70 <main+0x20>
      70: f2 0f 2c c0 	cvttsd2si	%xmm0, %eax
      74: 48 83 c4 10 	addq	$0x10, %rsp
      78: 5d 	popq	%rbp
      79: c3 	retq
```

However, this is unsatisfying because the `callq` there gives no indication that it is calling `sqrt`. So, we can add the `-r` flag to show the relocations:

```
$ llvm-objdump-17 --disassemble-symbols=main -r hello.o

hello.o:	file format elf64-x86-64

Disassembly of section .text:

0000000000000050 <main>:
      50: 55 	pushq	%rbp
		0000000000000007: R_X86_64_PC32	.L.str-0x4
		000000000000000e: R_X86_64_PLT32	printf-0x4
		0000000000000015: R_X86_64_PC32	.L.str.1-0x4
		000000000000001c: R_X86_64_PLT32	printf-0x4
		0000000000000023: R_X86_64_PC32	.L.str.2-0x4
		000000000000002a: R_X86_64_PLT32	printf-0x4
		0000000000000031: R_X86_64_PC32	.L.str.3-0x4
		0000000000000038: R_X86_64_PLT32	printf-0x4
		000000000000003f: R_X86_64_PLT32	putchar-0x4
      51: 48 89 e5 	movq	%rsp, %rbp
      54: 48 83 ec 10                  	subq	$0x10, %rsp
      58: c7 45 fc 00 00 00 00         	movl	$0x0, -0x4(%rbp)
 5f: 89 7d f8                     	movl	%edi, -0x8(%rbp)
      62: 48 89 75 f0                  	movq	%rsi, -0x10(%rbp)
      66: f2 0f 2a 45 f8               	cvtsi2sdl	-0x8(%rbp), %xmm0
      6b: e8 00 00 00 00 	callq	0x70 <main+0x20>
		000000000000006c: R_X86_64_PLT32	sqrt-0x4
      70: f2 0f 2c c0 	cvttsd2si	%xmm0, %eax
      74: 48 83 c4 10 	addq	$0x10, %rsp
      78: 5d 	popq	%rbp
      79: c3 	retq
```

Now we can see the relocation printed just after the `callq` to show us it is calling `sqrt`.

However, there is a problem. It's showing a whole lot of unrelated relocations after the first instruction in `main`. It is just a few lines in the example above, but when disassembling a single symbol from a large .o file with `-r`, this turns into an ocean of text.

GNU objdump does not have this issue and it just shows the `sqrt` relocation in this case:

```
$ objdump --disassemble=main -r hello.o

hello.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000050 <main>:
  50:	55                   	push   %rbp
  51:	48 89 e5 	mov    %rsp,%rbp
  54:	48 83 ec 10          	sub $0x10,%rsp
  58:	c7 45 fc 00 00 00 00 	movl   $0x0,-0x4(%rbp)
 5f:	89 7d f8             	mov    %edi,-0x8(%rbp)
  62:	48 89 75 f0 	mov    %rsi,-0x10(%rbp)
  66:	f2 0f 2a 45 f8       	cvtsi2sdl -0x8(%rbp),%xmm0
  6b:	e8 00 00 00 00       	call   70 <main+0x20>
			6c: R_X86_64_PLT32	sqrt-0x4
  70:	f2 0f 2c c0 	cvttsd2si %xmm0,%eax
  74:	48 83 c4 10          	add $0x10,%rsp
  78:	5d                   	pop    %rbp
  79:	c3 	ret
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to