3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

clangでプリプロセスとコンパイルのみを行い、アセンブリを確認する

Last updated at Posted at 2021-11-19
megu.c
#include <stdio.h>

void metyasugoi(int a, int b) {
  printf("Bye Bye.");
}
hoge.c
#include <stdio.h>

void metyasugoi(int a, int b, int c) {
  printf("Hello World.");
}
fuga.c
#include <stdio.h>

void metyasugoi(int a, int b);

int main(int argc, char **argv) {
  metyasugoi(1, 2);
}

参考: ソースコードのマシンコードへのコンパイルのステップ

アセンブリを確認する

$ ls
fuga.c  hoge.c  megu.c
$ clang fuga.c hoge.c megu.c -S
$ ls
fuga.c  fuga.s  hoge.c  hoge.s  megu.c  megu.s
megu.s
	.section	__TEXT,__text,regular,pure_instructions
	.build_version macos, 12, 0	sdk_version 12, 0
	.globl	_metyasugoi                     ## -- Begin function metyasugoi
	.p2align	4, 0x90
_metyasugoi:                            ## @metyasugoi
	.cfi_startproc
## %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset %rbp, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register %rbp
	subq	$16, %rsp
	movl	%edi, -4(%rbp)
	movl	%esi, -8(%rbp)
	leaq	L_.str(%rip), %rdi
	movb	$0, %al
	callq	_printf
	addq	$16, %rsp
	popq	%rbp
	retq
	.cfi_endproc
                                        ## -- End function
	.section	__TEXT,__cstring,cstring_literals
L_.str:                                 ## @.str
	.asciz	"Bye Bye."

.subsections_via_symbols
hoge.s
	.section	__TEXT,__text,regular,pure_instructions
	.build_version macos, 12, 0	sdk_version 12, 0
	.globl	_metyasugoi                     ## -- Begin function metyasugoi
	.p2align	4, 0x90
_metyasugoi:                            ## @metyasugoi
	.cfi_startproc
## %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset %rbp, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register %rbp
	subq	$16, %rsp
	movl	%edi, -4(%rbp)
	movl	%esi, -8(%rbp)
	movl	%edx, -12(%rbp)
	leaq	L_.str(%rip), %rdi
	movb	$0, %al
	callq	_printf
	addq	$16, %rsp
	popq	%rbp
	retq
	.cfi_endproc
                                        ## -- End function
	.section	__TEXT,__cstring,cstring_literals
L_.str:                                 ## @.str
	.asciz	"Hello World."

.subsections_via_symbols
fuga.s
	.section	__TEXT,__text,regular,pure_instructions
	.build_version macos, 12, 0	sdk_version 12, 0
	.globl	_main                           ## -- Begin function main
	.p2align	4, 0x90
_main:                                  ## @main
	.cfi_startproc
## %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset %rbp, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register %rbp
	subq	$16, %rsp
	movl	%edi, -4(%rbp)
	movq	%rsi, -16(%rbp)
	movl	$1, %edi
	movl	$2, %esi
	callq	_metyasugoi
	xorl	%eax, %eax
	addq	$16, %rsp
	popq	%rbp
	retq
	.cfi_endproc
                                        ## -- End function
.subsections_via_symbols

ちなみにわざとmetyasugoiシンボルが重複している為、-Sでアセンブルの前で止めない場合、duplicate symbolのErrorが出ます.

$ clang fuga.c hoge.c megu.c
duplicate symbol '_metyasugoi' in:
    /var/folders/zs/bd70xg051z111gt2nkr77q200000gn/T/hoge-c0cc77.o
    /var/folders/zs/bd70xg051z111gt2nkr77q200000gn/T/megu-f8552b.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?