Building object file with `swiftc -emit-object` also emitting `_main` implementation

Viewed 292

I'm currently building object files using swiftc, with:

swiftc -emit-object bar.swift

where bar.swift is something simple like:

class Bar {
  var value: Int

  init(value: Int) {
    self.value = value
  }

  func plusValue(_ value: Int) -> Int {
    return self.value + value
  }
}

When I then move on to linking this against my main object to create an executable, I get the following error:

$ cc -o foobar foo.o bar.o
duplicate symbol '_main' in:
    foo.o
    bar.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

This indicates that swiftc is adding a main implementation to the object file, which can be confirmed with:

$ nm bar.o | grep _main
0000000000000000 T _main

As far as I can tell, this added function does very little:

$ otool -tV bar.o
bar.o:
(__TEXT,__text) section
_main:
0000000000000000    pushq   %rbp
0000000000000001    movq    %rsp, %rbp
0000000000000004    xorl    %eax, %eax
0000000000000006    movl    %edi, -0x4(%rbp)
0000000000000009    movq    %rsi, -0x10(%rbp)
000000000000000d    popq    %rbp
000000000000000e    retq
000000000000000f    nop
....snip...

Is there a way to tell swiftc -emit-object to not add this vestigial implementation of main?

1 Answers

Short answer

The command line argument that I was missing was:

-parse-as-library

Long answer

In my quest to find an answer, I resorted to looking at the Swift source code on Github, and with a bit of luck found the following compiler invocation in the test suite:

// RUN: %target-build-swift %S/Inputs/CommandLineStressTest/CommandLineStressTest.swift -parse-as-library -force-single-frontend-invocation -module-name CommandLineStressTestSwift -emit-object -o %t/CommandLineStressTestSwift.o

According to swiftc --help, -parse-as-library causes the compiler to:

Parse the input file(s) as libraries, not scripts

This has proven to work for me, and the only difference in exported symbols is the removal of _main:

$ diff -U1 <(swiftc -emit-object -module-name bar -o a.o bar.swift && nm a.o | cut -c18-) <(swiftc -emit-object -parse-as-library -module-name bar -o b.o bar.swift && nm b.o | cut -c18-)
--- /dev/fd/63  2020-03-28 17:13:08.000000000 +1100
+++ /dev/fd/62  2020-03-28 17:13:08.000000000 +1100
@@ -23,3 +23,2 @@
 U __objc_empty_cache
-T _main
 s _objc_classes

and in the generated assembly the only change is the removal of the _main code:

$ diff -U1 <(swiftc -emit-object -module-name bar -o a.o bar.swift && objdump -d -no-leading-addr -no-show-raw-insn a.o) <(swiftc -emit-object -parse-as-library -module-name bar -o b.o bar.swift && objdump -d -no-leading-addr -no-show-raw-insn b.o)
--- /dev/fd/63  2020-03-28 17:19:03.000000000 +1100
+++ /dev/fd/62  2020-03-28 17:19:03.000000000 +1100
@@ -1,15 +1,5 @@

-a.o:   file format Mach-O 64-bit x86-64
+b.o:   file format Mach-O 64-bit x86-64

 Disassembly of section __TEXT,__text:
-_main:
-   pushq   %rbp
-   movq    %rsp, %rbp
-   xorl    %eax, %eax
-   movl    %edi, -4(%rbp)
-   movq    %rsi, -16(%rbp)
-   popq    %rbp
-   retq
-   nop
-
 _$S3bar3BarC5valueSivg:
Related