Porting swift to FreeBSD

Hi,
I'm a FreeBSD developer who has been working on porting swift to FreeBSD.
I'm at a point where with a local patch (
https://people.freebsd.org/~davide/swift/build_freebsd.diff ) to fix
build errors I'm able to build the compiler itself on FreeBSD 11
(-CURRENT).

The compiler itself seems to work fine (at least semantic analysis is
able to produce the correct result for toy examples).
Example:

% cat hello.swift
let number = 4
println(number)

% ./swiftc hello.swift -o hello
hello.swift:2:1: error: 'println' has been renamed to 'print'
println(number)
^~~~~~~
[...]

The executables generated seem to have some problems, though.
This is what I see:

% cat hello2.swift
let number = 4
print(number)
% ./swiftc hello2.swift -o hello2
% ./hello2
Int(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(Stri
ng(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(Strin
g(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String
(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(
String(String(String(String(String(String(String(String(String(String(String(String(St
[...]

Rebuilding with symbols and trying to debug:

(gdb) down
#17750 0x0000000800d1f07e in
_TFs5printFTGSaP__9separatorSS10terminatorSS_T_ (items=...,
separator=..., terminator=...) at
/exps/swift/swift/stdlib/public/core/Print.swift:34
34 items, separator: separator, terminator: terminator,
toStream: &output)
(gdb)
#17749 0x0000000800d1f57b in
_TFs6_printuRxs16OutputStreamTyperFTGSaP__9separatorSS10terminatorSS8toStreamRx_T_
(items=..., separator=..., terminator=..., output=...)
    at /exps/swift/swift/stdlib/public/core/Print.swift:128
128 _print_unlocked(item, &output)
(gdb)
#17748 0x0000000800d1b099 in
_TFs15_print_unlockedu0_R_s16OutputStreamTyperFTxRq__T_ (value=...,
target=...) at /exps/swift/swift/stdlib/public/core/OutputStream.swift:193
193 _adHocPrint(value, &target, isDebugPrint: false)
(gdb)
#17747 0x0000000800d1960b in
_TFs11_adHocPrintu0_R_s16OutputStreamTyperFTxRq_12isDebugPrintSb_T_
(value=..., isDebugPrint=...) at
/exps/swift/swift/stdlib/public/core/OutputStream.swift:124
124 print(label, terminator: "", toStream: &target)
(gdb)
#17746 0x0000000800d1a7ed in
_TFs5printuRxs16OutputStreamTyperFTGSaP__9separatorSS10terminatorSS8toStreamRx_T_
(items=..., separator=..., terminator=..., output=...)
    at /exps/swift/swift/stdlib/public/core/Print.swift:91
91 _print(items, separator: separator, terminator: terminator,
toStream: &output)
(gdb)
#17745 0x0000000800d1f57b in
_TFs6_printuRxs16OutputStreamTyperFTGSaP__9separatorSS10terminatorSS8toStreamRx_T_
(items=..., separator=..., terminator=..., output=...)
    at /exps/swift/swift/stdlib/public/core/Print.swift:128
128 _print_unlocked(item, &output)
(gdb)
#17744 0x0000000800d1b099 in
_TFs15_print_unlockedu0_R_s16OutputStreamTyperFTxRq__T_ (value=...,
target=...) at /exps/swift/swift/stdlib/public/core/OutputStream.swift:193
193 _adHocPrint(value, &target, isDebugPrint: false)
(gdb)
#17743 0x0000000800d1960b in
_TFs11_adHocPrintu0_R_s16OutputStreamTyperFTxRq_12isDebugPrintSb_T_
(value=..., isDebugPrint=...) at
/exps/swift/swift/stdlib/public/core/OutputStream.swift:124
124 print(label, terminator: "", toStream: &target)

I have some suspicion this may be related to my local patch (although
I can't say that for sure).
I'm still new to the internals of the language so any help on how what
could it be the root cause and how to try to debug this will be
appreciated.

Thanks!

···

--
Davide

Hi,
I'm a FreeBSD developer who has been working on porting swift to FreeBSD.
I'm at a point where with a local patch (
https://people.freebsd.org/~davide/swift/build_freebsd.diff ) to fix
build errors I'm able to build the compiler itself on FreeBSD 11
(-CURRENT).

+Doug for this patch.

The compiler itself seems to work fine (at least semantic analysis is
able to produce the correct result for toy examples).
Example:

% cat hello.swift
let number = 4
println(number)

% ./swiftc hello.swift -o hello
hello.swift:2:1: error: 'println' has been renamed to 'print'
println(number)
^~~~~~~
[...]

The executables generated seem to have some problems, though.
This is what I see:

% cat hello2.swift
let number = 4
print(number)
% ./swiftc hello2.swift -o hello2
% ./hello2
Int(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(Stri

The reason is that the program can't find the protocol conformance
tables. On Linux, we are using a linker script to collect all
conformances into one section, and insert symbols at the beginning and
at the end, see stdlib/public/runtime/swift.ld. The code that reads
these sections is in stdlib/public/runtime/Casting.cpp.

Dmitri

···

On Sat, Dec 12, 2015 at 8:29 PM, Davide Italiano via swift-dev <swift-dev@swift.org> wrote:

--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr@gmail.com>*/

Thanks Dmitri.
The linker script is actually executed on FreeBSD as well (with a
recent version of GNU ld, with the old ld shipped with FreeBSD or gold
the parsing of the linker script fails)

and the section is created (as objdump -h witnesses):

20 .dtors 00000010 0000000000603218 0000000000603218 00003218 2**3
                  CONTENTS, ALLOC, LOAD, DATA
21 .swift2_protocol_conformances 00000008 0000000000603228
0000000000603228 00003228 2**0
                  CONTENTS, ALLOC, LOAD, DATA
22 .jcr 00000008 0000000000603230 0000000000603230 00003230 2**3
                  CONTENTS, ALLOC, LOAD, DATA

% objdump -t ./hello | grep conformances
0000000000603228 l d .swift2_protocol_conformances
0000000000000000 .swift2_protocol_conformances
0000000000603228 g .swift2_protocol_conformances
0000000000000000 .swift2_protocol_conformances_start

Although the section seems to be empty/corrupted:

Disassembly of section .dtors:

0000000000603218 <__DTOR_LIST__>:
  603218: ff (bad)
  603219: ff (bad)
  60321a: ff (bad)
  60321b: ff (bad)
  60321c: ff (bad)
  60321d: ff (bad)
  60321e: ff (bad)
  60321f: ff 00 incl (%rax)

0000000000603220 <__DTOR_END__>:
        ...
Disassembly of section .swift2_protocol_conformances:

0000000000603228 <.swift2_protocol_conformances_start>:
        ...
Disassembly of section .jcr:

0000000000603230 <__JCR_END__>:

Do you have an example on how that should look like (on Linux) so I
can investigate and try to adapt that to work on FreeBSD?

Thanks,

···

On Sat, Dec 12, 2015 at 11:45 PM, Dmitri Gribenko <gribozavr@gmail.com> wrote:

On Sat, Dec 12, 2015 at 8:29 PM, Davide Italiano via swift-dev > <swift-dev@swift.org> wrote:

Hi,
I'm a FreeBSD developer who has been working on porting swift to FreeBSD.
I'm at a point where with a local patch (
https://people.freebsd.org/~davide/swift/build_freebsd.diff ) to fix
build errors I'm able to build the compiler itself on FreeBSD 11
(-CURRENT).

+Doug for this patch.

The compiler itself seems to work fine (at least semantic analysis is
able to produce the correct result for toy examples).
Example:

% cat hello.swift
let number = 4
println(number)

% ./swiftc hello.swift -o hello
hello.swift:2:1: error: 'println' has been renamed to 'print'
println(number)
^~~~~~~
[...]

The executables generated seem to have some problems, though.
This is what I see:

% cat hello2.swift
let number = 4
print(number)
% ./swiftc hello2.swift -o hello2
% ./hello2
Int(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(Stri

The reason is that the program can't find the protocol conformance
tables. On Linux, we are using a linker script to collect all
conformances into one section, and insert symbols at the beginning and
at the end, see stdlib/public/runtime/swift.ld. The code that reads
these sections is in stdlib/public/runtime/Casting.cpp.

--
Davide

Patch LGTM, but please delete the “if 0” block.

  - Doug

···

On Dec 12, 2015, at 8:45 PM, Dmitri Gribenko <gribozavr@gmail.com> wrote:

On Sat, Dec 12, 2015 at 8:29 PM, Davide Italiano via swift-dev > <swift-dev@swift.org> wrote:

Hi,
I'm a FreeBSD developer who has been working on porting swift to FreeBSD.
I'm at a point where with a local patch (
https://people.freebsd.org/~davide/swift/build_freebsd.diff ) to fix
build errors I'm able to build the compiler itself on FreeBSD 11
(-CURRENT).

+Doug for this patch.

It should be empty in the binary (since this simple program does not
define any new conformances to protocols). Could you try checking the
standard library, libswiftCore.so:

$ objdump -t lib/swift/linux/libswiftCore.so | grep conformances
0000000000602b10 l d .swift2_protocol_conformances
0000000000000000 .swift2_protocol_conformances
0000000000602b18 l O .swift2_protocol_conformances
0000000000002eb0 l_protocol_conformances
0000000000602b10 g .swift2_protocol_conformances
0000000000000000 .swift2_protocol_conformances_start

$ objdump -s -j .swift2_protocol_conformances
lib/swift/linux/libswiftCore.so | head

lib/swift/linux/libswiftCore.so: file format elf64-x86-64

Contents of section .swift2_protocol_conformances:
602b10 b02e0000 00000000 00000000 00000000 ................
602b20 00000000 04000000 00000000 00000000 ................
602b30 00000000 04000000 00000000 00000000 ................
602b40 00000000 04000000 00000000 00000000 ................
602b50 00000000 04000000 00000000 00000000 ................
602b60 00000000 04000000 00000000 00000000 ................

I'd recommend that you add debug output to
_addImageProtocolConformances() to see if the section is being found
and iterated over (or just use a debugger, after building with
--debug-swift-stdlib).

Dmitri

···

On Sat, Dec 12, 2015 at 9:09 PM, Davide Italiano <dccitaliano@gmail.com> wrote:

On Sat, Dec 12, 2015 at 11:45 PM, Dmitri Gribenko <gribozavr@gmail.com> wrote:

On Sat, Dec 12, 2015 at 8:29 PM, Davide Italiano via swift-dev >> <swift-dev@swift.org> wrote:

Hi,
I'm a FreeBSD developer who has been working on porting swift to FreeBSD.
I'm at a point where with a local patch (
https://people.freebsd.org/~davide/swift/build_freebsd.diff ) to fix
build errors I'm able to build the compiler itself on FreeBSD 11
(-CURRENT).

+Doug for this patch.

The compiler itself seems to work fine (at least semantic analysis is
able to produce the correct result for toy examples).
Example:

% cat hello.swift
let number = 4
println(number)

% ./swiftc hello.swift -o hello
hello.swift:2:1: error: 'println' has been renamed to 'print'
println(number)
^~~~~~~
[...]

The executables generated seem to have some problems, though.
This is what I see:

% cat hello2.swift
let number = 4
print(number)
% ./swiftc hello2.swift -o hello2
% ./hello2
Int(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(Stri

The reason is that the program can't find the protocol conformance
tables. On Linux, we are using a linker script to collect all
conformances into one section, and insert symbols at the beginning and
at the end, see stdlib/public/runtime/swift.ld. The code that reads
these sections is in stdlib/public/runtime/Casting.cpp.

Thanks Dmitri.
The linker script is actually executed on FreeBSD as well (with a
recent version of GNU ld, with the old ld shipped with FreeBSD or gold
the parsing of the linker script fails)

and the section is created (as objdump -h witnesses):

20 .dtors 00000010 0000000000603218 0000000000603218 00003218 2**3
                  CONTENTS, ALLOC, LOAD, DATA
21 .swift2_protocol_conformances 00000008 0000000000603228
0000000000603228 00003228 2**0
                  CONTENTS, ALLOC, LOAD, DATA
22 .jcr 00000008 0000000000603230 0000000000603230 00003230 2**3
                  CONTENTS, ALLOC, LOAD, DATA

% objdump -t ./hello | grep conformances
0000000000603228 l d .swift2_protocol_conformances
0000000000000000 .swift2_protocol_conformances
0000000000603228 g .swift2_protocol_conformances
0000000000000000 .swift2_protocol_conformances_start

Although the section seems to be empty/corrupted:

--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr@gmail.com>*/

% objdump -t ./lib/swift/freebsd/x86_64/libswiftCore.so | grep conformances
0000000000833710 l d .swift2_protocol_conformances
0000000000000000 .swift2_protocol_conformances
0000000000833710 l O .swift2_protocol_conformances
0000000000002eb0 l_protocol_conformances
00000000008365c0 g .swift2_protocol_conformances
0000000000000000 _edata

% objdump -s -j .swift2_protocol_conformances
./lib/swift/freebsd/libswiftCore.so |head -n 10

./lib/swift/freebsd/libswiftCore.so: file format elf64-x86-64-freebsd

Contents of section .swift2_protocol_conformances:
833710 00000000 00000000 00000000 04000000 ................
833720 00000000 00000000 00000000 04000000 ................
833730 00000000 00000000 00000000 04000000 ................
833740 00000000 00000000 00000000 04000000 ................
833750 00000000 00000000 00000000 04000000 ................
833760 00000000 00000000 00000000 04000000 ................

Thanks,

···

On Sun, Dec 13, 2015 at 12:22 AM, Dmitri Gribenko <gribozavr@gmail.com> wrote:

On Sat, Dec 12, 2015 at 9:09 PM, Davide Italiano <dccitaliano@gmail.com> wrote:

On Sat, Dec 12, 2015 at 11:45 PM, Dmitri Gribenko <gribozavr@gmail.com> wrote:

On Sat, Dec 12, 2015 at 8:29 PM, Davide Italiano via swift-dev >>> <swift-dev@swift.org> wrote:

Hi,
I'm a FreeBSD developer who has been working on porting swift to FreeBSD.
I'm at a point where with a local patch (
https://people.freebsd.org/~davide/swift/build_freebsd.diff ) to fix
build errors I'm able to build the compiler itself on FreeBSD 11
(-CURRENT).

+Doug for this patch.

The compiler itself seems to work fine (at least semantic analysis is
able to produce the correct result for toy examples).
Example:

% cat hello.swift
let number = 4
println(number)

% ./swiftc hello.swift -o hello
hello.swift:2:1: error: 'println' has been renamed to 'print'
println(number)
^~~~~~~
[...]

The executables generated seem to have some problems, though.
This is what I see:

% cat hello2.swift
let number = 4
print(number)
% ./swiftc hello2.swift -o hello2
% ./hello2
Int(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(Stri

The reason is that the program can't find the protocol conformance
tables. On Linux, we are using a linker script to collect all
conformances into one section, and insert symbols at the beginning and
at the end, see stdlib/public/runtime/swift.ld. The code that reads
these sections is in stdlib/public/runtime/Casting.cpp.

Thanks Dmitri.
The linker script is actually executed on FreeBSD as well (with a
recent version of GNU ld, with the old ld shipped with FreeBSD or gold
the parsing of the linker script fails)

and the section is created (as objdump -h witnesses):

20 .dtors 00000010 0000000000603218 0000000000603218 00003218 2**3
                  CONTENTS, ALLOC, LOAD, DATA
21 .swift2_protocol_conformances 00000008 0000000000603228
0000000000603228 00003228 2**0
                  CONTENTS, ALLOC, LOAD, DATA
22 .jcr 00000008 0000000000603230 0000000000603230 00003230 2**3
                  CONTENTS, ALLOC, LOAD, DATA

% objdump -t ./hello | grep conformances
0000000000603228 l d .swift2_protocol_conformances
0000000000000000 .swift2_protocol_conformances
0000000000603228 g .swift2_protocol_conformances
0000000000000000 .swift2_protocol_conformances_start

Although the section seems to be empty/corrupted:

It should be empty in the binary (since this simple program does not
define any new conformances to protocols). Could you try checking the
standard library, libswiftCore.so:

--
Davide

The section should start with an 8-byte size of the conformances
section, seems to be missing here (compare to my output, which has
"b02e0000 00000000").

The linker script adds this size using:

QUAD(SIZEOF(.swift2_protocol_conformances) - 8) ;

Could you verify you have that line?

Dmitri

···

On Sat, Dec 12, 2015 at 9:26 PM, Davide Italiano <dccitaliano@gmail.com> wrote:

On Sun, Dec 13, 2015 at 12:22 AM, Dmitri Gribenko <gribozavr@gmail.com> wrote:

On Sat, Dec 12, 2015 at 9:09 PM, Davide Italiano <dccitaliano@gmail.com> wrote:

On Sat, Dec 12, 2015 at 11:45 PM, Dmitri Gribenko <gribozavr@gmail.com> wrote:

On Sat, Dec 12, 2015 at 8:29 PM, Davide Italiano via swift-dev >>>> <swift-dev@swift.org> wrote:

Hi,
I'm a FreeBSD developer who has been working on porting swift to FreeBSD.
I'm at a point where with a local patch (
https://people.freebsd.org/~davide/swift/build_freebsd.diff ) to fix
build errors I'm able to build the compiler itself on FreeBSD 11
(-CURRENT).

+Doug for this patch.

The compiler itself seems to work fine (at least semantic analysis is
able to produce the correct result for toy examples).
Example:

% cat hello.swift
let number = 4
println(number)

% ./swiftc hello.swift -o hello
hello.swift:2:1: error: 'println' has been renamed to 'print'
println(number)
^~~~~~~
[...]

The executables generated seem to have some problems, though.
This is what I see:

% cat hello2.swift
let number = 4
print(number)
% ./swiftc hello2.swift -o hello2
% ./hello2
Int(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(Stri

The reason is that the program can't find the protocol conformance
tables. On Linux, we are using a linker script to collect all
conformances into one section, and insert symbols at the beginning and
at the end, see stdlib/public/runtime/swift.ld. The code that reads
these sections is in stdlib/public/runtime/Casting.cpp.

Thanks Dmitri.
The linker script is actually executed on FreeBSD as well (with a
recent version of GNU ld, with the old ld shipped with FreeBSD or gold
the parsing of the linker script fails)

and the section is created (as objdump -h witnesses):

20 .dtors 00000010 0000000000603218 0000000000603218 00003218 2**3
                  CONTENTS, ALLOC, LOAD, DATA
21 .swift2_protocol_conformances 00000008 0000000000603228
0000000000603228 00003228 2**0
                  CONTENTS, ALLOC, LOAD, DATA
22 .jcr 00000008 0000000000603230 0000000000603230 00003230 2**3
                  CONTENTS, ALLOC, LOAD, DATA

% objdump -t ./hello | grep conformances
0000000000603228 l d .swift2_protocol_conformances
0000000000000000 .swift2_protocol_conformances
0000000000603228 g .swift2_protocol_conformances
0000000000000000 .swift2_protocol_conformances_start

Although the section seems to be empty/corrupted:

It should be empty in the binary (since this simple program does not
define any new conformances to protocols). Could you try checking the
standard library, libswiftCore.so:

% objdump -t ./lib/swift/freebsd/x86_64/libswiftCore.so | grep conformances
0000000000833710 l d .swift2_protocol_conformances
0000000000000000 .swift2_protocol_conformances
0000000000833710 l O .swift2_protocol_conformances
0000000000002eb0 l_protocol_conformances
00000000008365c0 g .swift2_protocol_conformances
0000000000000000 _edata

% objdump -s -j .swift2_protocol_conformances
./lib/swift/freebsd/libswiftCore.so |head -n 10

./lib/swift/freebsd/libswiftCore.so: file format elf64-x86-64-freebsd

Contents of section .swift2_protocol_conformances:
833710 00000000 00000000 00000000 04000000 ................
833720 00000000 00000000 00000000 04000000 ................

--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr@gmail.com>*/

The main/only difference seem to be in the first 8 bytes of the section.
I'll check why the linker script does not honour this.

Contents of section .swift2_protocol_conformances:
602b10 b02e0000 00000000 00000000 00000000 ................
602b20 00000000 04000000 00000000 00000000 ................

···

On Sun, Dec 13, 2015 at 12:26 AM, Davide Italiano <dccitaliano@gmail.com> wrote:

On Sun, Dec 13, 2015 at 12:22 AM, Dmitri Gribenko <gribozavr@gmail.com> wrote:

On Sat, Dec 12, 2015 at 9:09 PM, Davide Italiano <dccitaliano@gmail.com> wrote:

On Sat, Dec 12, 2015 at 11:45 PM, Dmitri Gribenko <gribozavr@gmail.com> wrote:

On Sat, Dec 12, 2015 at 8:29 PM, Davide Italiano via swift-dev >>>> <swift-dev@swift.org> wrote:

Hi,
I'm a FreeBSD developer who has been working on porting swift to FreeBSD.
I'm at a point where with a local patch (
https://people.freebsd.org/~davide/swift/build_freebsd.diff ) to fix
build errors I'm able to build the compiler itself on FreeBSD 11
(-CURRENT).

+Doug for this patch.

The compiler itself seems to work fine (at least semantic analysis is
able to produce the correct result for toy examples).
Example:

% cat hello.swift
let number = 4
println(number)

% ./swiftc hello.swift -o hello
hello.swift:2:1: error: 'println' has been renamed to 'print'
println(number)
^~~~~~~
[...]

The executables generated seem to have some problems, though.
This is what I see:

% cat hello2.swift
let number = 4
print(number)
% ./swiftc hello2.swift -o hello2
% ./hello2
Int(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(Stri

The reason is that the program can't find the protocol conformance
tables. On Linux, we are using a linker script to collect all
conformances into one section, and insert symbols at the beginning and
at the end, see stdlib/public/runtime/swift.ld. The code that reads
these sections is in stdlib/public/runtime/Casting.cpp.

Thanks Dmitri.
The linker script is actually executed on FreeBSD as well (with a
recent version of GNU ld, with the old ld shipped with FreeBSD or gold
the parsing of the linker script fails)

and the section is created (as objdump -h witnesses):

20 .dtors 00000010 0000000000603218 0000000000603218 00003218 2**3
                  CONTENTS, ALLOC, LOAD, DATA
21 .swift2_protocol_conformances 00000008 0000000000603228
0000000000603228 00003228 2**0
                  CONTENTS, ALLOC, LOAD, DATA
22 .jcr 00000008 0000000000603230 0000000000603230 00003230 2**3
                  CONTENTS, ALLOC, LOAD, DATA

% objdump -t ./hello | grep conformances
0000000000603228 l d .swift2_protocol_conformances
0000000000000000 .swift2_protocol_conformances
0000000000603228 g .swift2_protocol_conformances
0000000000000000 .swift2_protocol_conformances_start

Although the section seems to be empty/corrupted:

It should be empty in the binary (since this simple program does not
define any new conformances to protocols). Could you try checking the
standard library, libswiftCore.so:

% objdump -t ./lib/swift/freebsd/x86_64/libswiftCore.so | grep conformances
0000000000833710 l d .swift2_protocol_conformances
0000000000000000 .swift2_protocol_conformances
0000000000833710 l O .swift2_protocol_conformances
0000000000002eb0 l_protocol_conformances
00000000008365c0 g .swift2_protocol_conformances
0000000000000000 _edata

% objdump -s -j .swift2_protocol_conformances
./lib/swift/freebsd/libswiftCore.so |head -n 10

./lib/swift/freebsd/libswiftCore.so: file format elf64-x86-64-freebsd

Contents of section .swift2_protocol_conformances:
833710 00000000 00000000 00000000 04000000 ................
833720 00000000 00000000 00000000 04000000 ................
833730 00000000 00000000 00000000 04000000 ................
833740 00000000 00000000 00000000 04000000 ................
833750 00000000 00000000 00000000 04000000 ................
833760 00000000 00000000 00000000 04000000 ................

hmm, apparently we replied almost at the same time.
Anyway, yes, my linker script has the QUAD line.

SECTIONS
{
  .swift2_protocol_conformances :
  {
    .swift2_protocol_conformances_start = . ;
    QUAD(SIZEOF(.swift2_protocol_conformances) - 8) ;
    *(.swift2_protocol_conformances) ;
  }
}
INSERT AFTER .dtors

···

On Sun, Dec 13, 2015 at 12:30 AM, Dmitri Gribenko <gribozavr@gmail.com> wrote:

On Sat, Dec 12, 2015 at 9:26 PM, Davide Italiano <dccitaliano@gmail.com> wrote:

On Sun, Dec 13, 2015 at 12:22 AM, Dmitri Gribenko <gribozavr@gmail.com> wrote:

On Sat, Dec 12, 2015 at 9:09 PM, Davide Italiano <dccitaliano@gmail.com> wrote:

On Sat, Dec 12, 2015 at 11:45 PM, Dmitri Gribenko <gribozavr@gmail.com> wrote:

On Sat, Dec 12, 2015 at 8:29 PM, Davide Italiano via swift-dev >>>>> <swift-dev@swift.org> wrote:

Hi,
I'm a FreeBSD developer who has been working on porting swift to FreeBSD.
I'm at a point where with a local patch (
https://people.freebsd.org/~davide/swift/build_freebsd.diff ) to fix
build errors I'm able to build the compiler itself on FreeBSD 11
(-CURRENT).

+Doug for this patch.

The compiler itself seems to work fine (at least semantic analysis is
able to produce the correct result for toy examples).
Example:

% cat hello.swift
let number = 4
println(number)

% ./swiftc hello.swift -o hello
hello.swift:2:1: error: 'println' has been renamed to 'print'
println(number)
^~~~~~~
[...]

The executables generated seem to have some problems, though.
This is what I see:

% cat hello2.swift
let number = 4
print(number)
% ./swiftc hello2.swift -o hello2
% ./hello2
Int(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(Stri

The reason is that the program can't find the protocol conformance
tables. On Linux, we are using a linker script to collect all
conformances into one section, and insert symbols at the beginning and
at the end, see stdlib/public/runtime/swift.ld. The code that reads
these sections is in stdlib/public/runtime/Casting.cpp.

Thanks Dmitri.
The linker script is actually executed on FreeBSD as well (with a
recent version of GNU ld, with the old ld shipped with FreeBSD or gold
the parsing of the linker script fails)

and the section is created (as objdump -h witnesses):

20 .dtors 00000010 0000000000603218 0000000000603218 00003218 2**3
                  CONTENTS, ALLOC, LOAD, DATA
21 .swift2_protocol_conformances 00000008 0000000000603228
0000000000603228 00003228 2**0
                  CONTENTS, ALLOC, LOAD, DATA
22 .jcr 00000008 0000000000603230 0000000000603230 00003230 2**3
                  CONTENTS, ALLOC, LOAD, DATA

% objdump -t ./hello | grep conformances
0000000000603228 l d .swift2_protocol_conformances
0000000000000000 .swift2_protocol_conformances
0000000000603228 g .swift2_protocol_conformances
0000000000000000 .swift2_protocol_conformances_start

Although the section seems to be empty/corrupted:

It should be empty in the binary (since this simple program does not
define any new conformances to protocols). Could you try checking the
standard library, libswiftCore.so:

% objdump -t ./lib/swift/freebsd/x86_64/libswiftCore.so | grep conformances
0000000000833710 l d .swift2_protocol_conformances
0000000000000000 .swift2_protocol_conformances
0000000000833710 l O .swift2_protocol_conformances
0000000000002eb0 l_protocol_conformances
00000000008365c0 g .swift2_protocol_conformances
0000000000000000 _edata

% objdump -s -j .swift2_protocol_conformances
./lib/swift/freebsd/libswiftCore.so |head -n 10

./lib/swift/freebsd/libswiftCore.so: file format elf64-x86-64-freebsd

Contents of section .swift2_protocol_conformances:
833710 00000000 00000000 00000000 04000000 ................
833720 00000000 00000000 00000000 04000000 ................

The section should start with an 8-byte size of the conformances
section, seems to be missing here (compare to my output, which has
"b02e0000 00000000").

The linker script adds this size using:

QUAD(SIZEOF(.swift2_protocol_conformances) - 8) ;

Could you verify you have that line?

I found the problem. For the standard library the section weren't
coalesced because the correct flags weren't passed to the linker in
FreeBSD.
The following patch, which I'm going to submit soon, fixes the problem for me.

diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake
index 48288b7..37cb2ba 100644
--- a/cmake/modules/AddSwift.cmake
+++ b/cmake/modules/AddSwift.cmake
@@ -1099,10 +1099,11 @@ function(_add_swift_library_single target name)
     set(PLIST_INFO_BUILD_VERSION)
   endif()

- # On Linux add the linker script that coalesces protocol conformance
- # sections. This wouldn't be necessary if the link was done by the swift
- # binary: rdar://problem/19007002
- if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
+ # On Linux and FreeBSD add the linker script that coalesces protocol
+ # conformance sections. This wouldn't be necessary if the link was done by
+ # the swift binary: rdar://problem/19007002
+ if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux" OR
+ "${CMAKE_SYSTEM_NAME}" STREQUAL "FreeBSD")
     list(APPEND link_flags
         "-Xlinker" "-T"
         "-Xlinker" "${SWIFTLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}/swift.ld")

And now, swift compiled programs run correctly on FreeBSD!

% ./swiftc hello.swift -o hello
davide@rabbit1:/exps/swift/build/Ninja-ReleaseAssert/swift-freebsd-x86_64/bin
% ./hello
hello
% cat hello.swift
print ("hello")

Thank you for your assistance Dmitri.

···

On Sun, Dec 13, 2015 at 12:35 AM, Davide Italiano <dccitaliano@gmail.com> wrote:

On Sun, Dec 13, 2015 at 12:30 AM, Dmitri Gribenko <gribozavr@gmail.com> wrote:

On Sat, Dec 12, 2015 at 9:26 PM, Davide Italiano <dccitaliano@gmail.com> wrote:

On Sun, Dec 13, 2015 at 12:22 AM, Dmitri Gribenko <gribozavr@gmail.com> wrote:

On Sat, Dec 12, 2015 at 9:09 PM, Davide Italiano <dccitaliano@gmail.com> wrote:

On Sat, Dec 12, 2015 at 11:45 PM, Dmitri Gribenko <gribozavr@gmail.com> wrote:

On Sat, Dec 12, 2015 at 8:29 PM, Davide Italiano via swift-dev >>>>>> <swift-dev@swift.org> wrote:

Hi,
I'm a FreeBSD developer who has been working on porting swift to FreeBSD.
I'm at a point where with a local patch (
https://people.freebsd.org/~davide/swift/build_freebsd.diff ) to fix
build errors I'm able to build the compiler itself on FreeBSD 11
(-CURRENT).

+Doug for this patch.

The compiler itself seems to work fine (at least semantic analysis is
able to produce the correct result for toy examples).
Example:

% cat hello.swift
let number = 4
println(number)

% ./swiftc hello.swift -o hello
hello.swift:2:1: error: 'println' has been renamed to 'print'
println(number)
^~~~~~~
[...]

The executables generated seem to have some problems, though.
This is what I see:

% cat hello2.swift
let number = 4
print(number)
% ./swiftc hello2.swift -o hello2
% ./hello2
Int(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(String(Stri

The reason is that the program can't find the protocol conformance
tables. On Linux, we are using a linker script to collect all
conformances into one section, and insert symbols at the beginning and
at the end, see stdlib/public/runtime/swift.ld. The code that reads
these sections is in stdlib/public/runtime/Casting.cpp.

Thanks Dmitri.
The linker script is actually executed on FreeBSD as well (with a
recent version of GNU ld, with the old ld shipped with FreeBSD or gold
the parsing of the linker script fails)

and the section is created (as objdump -h witnesses):

20 .dtors 00000010 0000000000603218 0000000000603218 00003218 2**3
                  CONTENTS, ALLOC, LOAD, DATA
21 .swift2_protocol_conformances 00000008 0000000000603228
0000000000603228 00003228 2**0
                  CONTENTS, ALLOC, LOAD, DATA
22 .jcr 00000008 0000000000603230 0000000000603230 00003230 2**3
                  CONTENTS, ALLOC, LOAD, DATA

% objdump -t ./hello | grep conformances
0000000000603228 l d .swift2_protocol_conformances
0000000000000000 .swift2_protocol_conformances
0000000000603228 g .swift2_protocol_conformances
0000000000000000 .swift2_protocol_conformances_start

Although the section seems to be empty/corrupted:

It should be empty in the binary (since this simple program does not
define any new conformances to protocols). Could you try checking the
standard library, libswiftCore.so:

% objdump -t ./lib/swift/freebsd/x86_64/libswiftCore.so | grep conformances
0000000000833710 l d .swift2_protocol_conformances
0000000000000000 .swift2_protocol_conformances
0000000000833710 l O .swift2_protocol_conformances
0000000000002eb0 l_protocol_conformances
00000000008365c0 g .swift2_protocol_conformances
0000000000000000 _edata

% objdump -s -j .swift2_protocol_conformances
./lib/swift/freebsd/libswiftCore.so |head -n 10

./lib/swift/freebsd/libswiftCore.so: file format elf64-x86-64-freebsd

Contents of section .swift2_protocol_conformances:
833710 00000000 00000000 00000000 04000000 ................
833720 00000000 00000000 00000000 04000000 ................

The section should start with an 8-byte size of the conformances
section, seems to be missing here (compare to my output, which has
"b02e0000 00000000").

The linker script adds this size using:

QUAD(SIZEOF(.swift2_protocol_conformances) - 8) ;

Could you verify you have that line?

hmm, apparently we replied almost at the same time.
Anyway, yes, my linker script has the QUAD line.

SECTIONS
{
  .swift2_protocol_conformances :
  {
    .swift2_protocol_conformances_start = . ;
    QUAD(SIZEOF(.swift2_protocol_conformances) - 8) ;
    *(.swift2_protocol_conformances) ;
  }
}
INSERT AFTER .dtors

--
Davide

And now, swift compiled programs run correctly on FreeBSD!

This is great, thanks Davide!

% ./swiftc hello.swift -o hello
davide@rabbit1:/exps/swift/build/Ninja-ReleaseAssert/swift-freebsd-x86_64/bin
% ./hello
hello
% cat hello.swift
print ("hello")

How much of the testsuite passes?

Dmitri

···

On Sun, Dec 13, 2015 at 2:52 AM, Davide Italiano <dccitaliano@gmail.com> wrote:

--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr@gmail.com>*/

There isn't (yet) support for FreeBSD in lit.cfg so it fails.
I'll try to add those bits tomorrow and report how far it goes.

Thanks,

···

On Sun, Dec 13, 2015 at 6:15 AM, Dmitri Gribenko <gribozavr@gmail.com> wrote:

On Sun, Dec 13, 2015 at 2:52 AM, Davide Italiano <dccitaliano@gmail.com> wrote:

And now, swift compiled programs run correctly on FreeBSD!

This is great, thanks Davide!

% ./swiftc hello.swift -o hello
davide@rabbit1:/exps/swift/build/Ninja-ReleaseAssert/swift-freebsd-x86_64/bin
% ./hello
hello
% cat hello.swift
print ("hello")

How much of the testsuite passes?

--
Davide

This is very exciting Davide! Thanks for pushing forward on this,

-Chris

···

On Dec 13, 2015, at 3:34 AM, Davide Italiano via swift-dev <swift-dev@swift.org> wrote:

On Sun, Dec 13, 2015 at 6:15 AM, Dmitri Gribenko <gribozavr@gmail.com> wrote:

On Sun, Dec 13, 2015 at 2:52 AM, Davide Italiano <dccitaliano@gmail.com> wrote:

And now, swift compiled programs run correctly on FreeBSD!

This is great, thanks Davide!

% ./swiftc hello.swift -o hello
davide@rabbit1:/exps/swift/build/Ninja-ReleaseAssert/swift-freebsd-x86_64/bin
% ./hello
hello
% cat hello.swift
print ("hello")

How much of the testsuite passes?

There isn't (yet) support for FreeBSD in lit.cfg so it fails.
I'll try to add those bits tomorrow and report how far it goes.

Thank you. After some hacking (patches will come soon) I got the
testsuite to run on FreeBSD.
This is the current status.

  Expected Passes : 780
  Expected Failures : 5
  Unsupported Tests : 572
  Unexpected Failures: 994

Sure, there's still a lot to do but doesn't seem that bad for a first run =)

Thanks,

···

On Sun, Dec 13, 2015 at 4:41 PM, Chris Lattner <clattner@apple.com> wrote:

On Dec 13, 2015, at 3:34 AM, Davide Italiano via swift-dev <swift-dev@swift.org> wrote:

On Sun, Dec 13, 2015 at 6:15 AM, Dmitri Gribenko <gribozavr@gmail.com> wrote:

On Sun, Dec 13, 2015 at 2:52 AM, Davide Italiano <dccitaliano@gmail.com> wrote:

And now, swift compiled programs run correctly on FreeBSD!

This is great, thanks Davide!

% ./swiftc hello.swift -o hello
davide@rabbit1:/exps/swift/build/Ninja-ReleaseAssert/swift-freebsd-x86_64/bin
% ./hello
hello
% cat hello.swift
print ("hello")

How much of the testsuite passes?

There isn't (yet) support for FreeBSD in lit.cfg so it fails.
I'll try to add those bits tomorrow and report how far it goes.

This is very exciting Davide! Thanks for pushing forward on this,

-Chris

--
Davide